convert_to_i420.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Copyright 2011 The LibYuv Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <stdlib.h>
  11. #include "libyuv/convert.h"
  12. #include "libyuv/video_common.h"
  13. #ifdef __cplusplus
  14. namespace libyuv {
  15. extern "C" {
  16. #endif
  17. // Convert camera sample to I420 with cropping, rotation and vertical flip.
  18. // src_width is used for source stride computation
  19. // src_height is used to compute location of planes, and indicate inversion
  20. // sample_size is measured in bytes and is the size of the frame.
  21. // With MJPEG it is the compressed size of the frame.
  22. LIBYUV_API
  23. int ConvertToI420(const uint8_t* sample,
  24. size_t sample_size,
  25. uint8_t* dst_y,
  26. int dst_stride_y,
  27. uint8_t* dst_u,
  28. int dst_stride_u,
  29. uint8_t* dst_v,
  30. int dst_stride_v,
  31. int crop_x,
  32. int crop_y,
  33. int src_width,
  34. int src_height,
  35. int crop_width,
  36. int crop_height,
  37. enum RotationMode rotation,
  38. uint32_t fourcc) {
  39. uint32_t format = CanonicalFourCC(fourcc);
  40. int aligned_src_width = (src_width + 1) & ~1;
  41. const uint8_t* src;
  42. const uint8_t* src_uv;
  43. const int abs_src_height = (src_height < 0) ? -src_height : src_height;
  44. // TODO(nisse): Why allow crop_height < 0?
  45. const int abs_crop_height = (crop_height < 0) ? -crop_height : crop_height;
  46. int r = 0;
  47. LIBYUV_BOOL need_buf =
  48. (rotation && format != FOURCC_I420 && format != FOURCC_NV12 &&
  49. format != FOURCC_NV21 && format != FOURCC_YV12) ||
  50. dst_y == sample;
  51. uint8_t* tmp_y = dst_y;
  52. uint8_t* tmp_u = dst_u;
  53. uint8_t* tmp_v = dst_v;
  54. int tmp_y_stride = dst_stride_y;
  55. int tmp_u_stride = dst_stride_u;
  56. int tmp_v_stride = dst_stride_v;
  57. uint8_t* rotate_buffer = NULL;
  58. const int inv_crop_height =
  59. (src_height < 0) ? -abs_crop_height : abs_crop_height;
  60. if (!dst_y || !dst_u || !dst_v || !sample || src_width <= 0 ||
  61. crop_width <= 0 || src_height == 0 || crop_height == 0) {
  62. return -1;
  63. }
  64. // One pass rotation is available for some formats. For the rest, convert
  65. // to I420 (with optional vertical flipping) into a temporary I420 buffer,
  66. // and then rotate the I420 to the final destination buffer.
  67. // For in-place conversion, if destination dst_y is same as source sample,
  68. // also enable temporary buffer.
  69. if (need_buf) {
  70. int y_size = crop_width * abs_crop_height;
  71. int uv_size = ((crop_width + 1) / 2) * ((abs_crop_height + 1) / 2);
  72. rotate_buffer = (uint8_t*)malloc(y_size + uv_size * 2); /* NOLINT */
  73. if (!rotate_buffer) {
  74. return 1; // Out of memory runtime error.
  75. }
  76. dst_y = rotate_buffer;
  77. dst_u = dst_y + y_size;
  78. dst_v = dst_u + uv_size;
  79. dst_stride_y = crop_width;
  80. dst_stride_u = dst_stride_v = ((crop_width + 1) / 2);
  81. }
  82. switch (format) {
  83. // Single plane formats
  84. case FOURCC_YUY2:
  85. src = sample + (aligned_src_width * crop_y + crop_x) * 2;
  86. r = YUY2ToI420(src, aligned_src_width * 2, dst_y, dst_stride_y, dst_u,
  87. dst_stride_u, dst_v, dst_stride_v, crop_width,
  88. inv_crop_height);
  89. break;
  90. case FOURCC_UYVY:
  91. src = sample + (aligned_src_width * crop_y + crop_x) * 2;
  92. r = UYVYToI420(src, aligned_src_width * 2, dst_y, dst_stride_y, dst_u,
  93. dst_stride_u, dst_v, dst_stride_v, crop_width,
  94. inv_crop_height);
  95. break;
  96. case FOURCC_RGBP:
  97. src = sample + (src_width * crop_y + crop_x) * 2;
  98. r = RGB565ToI420(src, src_width * 2, dst_y, dst_stride_y, dst_u,
  99. dst_stride_u, dst_v, dst_stride_v, crop_width,
  100. inv_crop_height);
  101. break;
  102. case FOURCC_RGBO:
  103. src = sample + (src_width * crop_y + crop_x) * 2;
  104. r = ARGB1555ToI420(src, src_width * 2, dst_y, dst_stride_y, dst_u,
  105. dst_stride_u, dst_v, dst_stride_v, crop_width,
  106. inv_crop_height);
  107. break;
  108. case FOURCC_R444:
  109. src = sample + (src_width * crop_y + crop_x) * 2;
  110. r = ARGB4444ToI420(src, src_width * 2, dst_y, dst_stride_y, dst_u,
  111. dst_stride_u, dst_v, dst_stride_v, crop_width,
  112. inv_crop_height);
  113. break;
  114. case FOURCC_24BG:
  115. src = sample + (src_width * crop_y + crop_x) * 3;
  116. r = RGB24ToI420(src, src_width * 3, dst_y, dst_stride_y, dst_u,
  117. dst_stride_u, dst_v, dst_stride_v, crop_width,
  118. inv_crop_height);
  119. break;
  120. case FOURCC_RAW:
  121. src = sample + (src_width * crop_y + crop_x) * 3;
  122. r = RAWToI420(src, src_width * 3, dst_y, dst_stride_y, dst_u,
  123. dst_stride_u, dst_v, dst_stride_v, crop_width,
  124. inv_crop_height);
  125. break;
  126. case FOURCC_ARGB:
  127. src = sample + (src_width * crop_y + crop_x) * 4;
  128. r = ARGBToI420(src, src_width * 4, dst_y, dst_stride_y, dst_u,
  129. dst_stride_u, dst_v, dst_stride_v, crop_width,
  130. inv_crop_height);
  131. break;
  132. case FOURCC_BGRA:
  133. src = sample + (src_width * crop_y + crop_x) * 4;
  134. r = BGRAToI420(src, src_width * 4, dst_y, dst_stride_y, dst_u,
  135. dst_stride_u, dst_v, dst_stride_v, crop_width,
  136. inv_crop_height);
  137. break;
  138. case FOURCC_ABGR:
  139. src = sample + (src_width * crop_y + crop_x) * 4;
  140. r = ABGRToI420(src, src_width * 4, dst_y, dst_stride_y, dst_u,
  141. dst_stride_u, dst_v, dst_stride_v, crop_width,
  142. inv_crop_height);
  143. break;
  144. case FOURCC_RGBA:
  145. src = sample + (src_width * crop_y + crop_x) * 4;
  146. r = RGBAToI420(src, src_width * 4, dst_y, dst_stride_y, dst_u,
  147. dst_stride_u, dst_v, dst_stride_v, crop_width,
  148. inv_crop_height);
  149. break;
  150. // TODO(fbarchard): Add AR30 and AB30
  151. case FOURCC_I400:
  152. src = sample + src_width * crop_y + crop_x;
  153. r = I400ToI420(src, src_width, dst_y, dst_stride_y, dst_u, dst_stride_u,
  154. dst_v, dst_stride_v, crop_width, inv_crop_height);
  155. break;
  156. // Biplanar formats
  157. case FOURCC_NV12:
  158. src = sample + (src_width * crop_y + crop_x);
  159. src_uv = sample + (src_width * abs_src_height) +
  160. ((crop_y / 2) * aligned_src_width) + ((crop_x / 2) * 2);
  161. r = NV12ToI420Rotate(src, src_width, src_uv, aligned_src_width, dst_y,
  162. dst_stride_y, dst_u, dst_stride_u, dst_v,
  163. dst_stride_v, crop_width, inv_crop_height, rotation);
  164. break;
  165. case FOURCC_NV21:
  166. src = sample + (src_width * crop_y + crop_x);
  167. src_uv = sample + (src_width * abs_src_height) +
  168. ((crop_y / 2) * aligned_src_width) + ((crop_x / 2) * 2);
  169. // Call NV12 but with dst_u and dst_v parameters swapped.
  170. r = NV12ToI420Rotate(src, src_width, src_uv, aligned_src_width, dst_y,
  171. dst_stride_y, dst_v, dst_stride_v, dst_u,
  172. dst_stride_u, crop_width, inv_crop_height, rotation);
  173. break;
  174. case FOURCC_M420:
  175. src = sample + (src_width * crop_y) * 12 / 8 + crop_x;
  176. r = M420ToI420(src, src_width, dst_y, dst_stride_y, dst_u, dst_stride_u,
  177. dst_v, dst_stride_v, crop_width, inv_crop_height);
  178. break;
  179. // Triplanar formats
  180. case FOURCC_I420:
  181. case FOURCC_YV12: {
  182. const uint8_t* src_y = sample + (src_width * crop_y + crop_x);
  183. const uint8_t* src_u;
  184. const uint8_t* src_v;
  185. int halfwidth = (src_width + 1) / 2;
  186. int halfheight = (abs_src_height + 1) / 2;
  187. if (format == FOURCC_YV12) {
  188. src_v = sample + src_width * abs_src_height + halfwidth * (crop_y / 2) +
  189. (crop_x / 2);
  190. src_u = sample + src_width * abs_src_height +
  191. halfwidth * (halfheight + (crop_y / 2)) + (crop_x / 2);
  192. } else {
  193. src_u = sample + src_width * abs_src_height + halfwidth * (crop_y / 2) +
  194. (crop_x / 2);
  195. src_v = sample + src_width * abs_src_height +
  196. halfwidth * (halfheight + (crop_y / 2)) + (crop_x / 2);
  197. }
  198. r = I420Rotate(src_y, src_width, src_u, halfwidth, src_v, halfwidth,
  199. dst_y, dst_stride_y, dst_u, dst_stride_u, dst_v,
  200. dst_stride_v, crop_width, inv_crop_height, rotation);
  201. break;
  202. }
  203. case FOURCC_I422:
  204. case FOURCC_YV16: {
  205. const uint8_t* src_y = sample + src_width * crop_y + crop_x;
  206. const uint8_t* src_u;
  207. const uint8_t* src_v;
  208. int halfwidth = (src_width + 1) / 2;
  209. if (format == FOURCC_YV16) {
  210. src_v = sample + src_width * abs_src_height + halfwidth * crop_y +
  211. (crop_x / 2);
  212. src_u = sample + src_width * abs_src_height +
  213. halfwidth * (abs_src_height + crop_y) + (crop_x / 2);
  214. } else {
  215. src_u = sample + src_width * abs_src_height + halfwidth * crop_y +
  216. (crop_x / 2);
  217. src_v = sample + src_width * abs_src_height +
  218. halfwidth * (abs_src_height + crop_y) + (crop_x / 2);
  219. }
  220. r = I422ToI420(src_y, src_width, src_u, halfwidth, src_v, halfwidth,
  221. dst_y, dst_stride_y, dst_u, dst_stride_u, dst_v,
  222. dst_stride_v, crop_width, inv_crop_height);
  223. break;
  224. }
  225. case FOURCC_I444:
  226. case FOURCC_YV24: {
  227. const uint8_t* src_y = sample + src_width * crop_y + crop_x;
  228. const uint8_t* src_u;
  229. const uint8_t* src_v;
  230. if (format == FOURCC_YV24) {
  231. src_v = sample + src_width * (abs_src_height + crop_y) + crop_x;
  232. src_u = sample + src_width * (abs_src_height * 2 + crop_y) + crop_x;
  233. } else {
  234. src_u = sample + src_width * (abs_src_height + crop_y) + crop_x;
  235. src_v = sample + src_width * (abs_src_height * 2 + crop_y) + crop_x;
  236. }
  237. r = I444ToI420(src_y, src_width, src_u, src_width, src_v, src_width,
  238. dst_y, dst_stride_y, dst_u, dst_stride_u, dst_v,
  239. dst_stride_v, crop_width, inv_crop_height);
  240. break;
  241. }
  242. #ifdef HAVE_JPEG
  243. case FOURCC_MJPG:
  244. r = MJPGToI420(sample, sample_size, dst_y, dst_stride_y, dst_u,
  245. dst_stride_u, dst_v, dst_stride_v, src_width,
  246. abs_src_height, crop_width, inv_crop_height);
  247. break;
  248. #endif
  249. default:
  250. r = -1; // unknown fourcc - return failure code.
  251. }
  252. if (need_buf) {
  253. if (!r) {
  254. r = I420Rotate(dst_y, dst_stride_y, dst_u, dst_stride_u, dst_v,
  255. dst_stride_v, tmp_y, tmp_y_stride, tmp_u, tmp_u_stride,
  256. tmp_v, tmp_v_stride, crop_width, abs_crop_height,
  257. rotation);
  258. }
  259. free(rotate_buffer);
  260. }
  261. return r;
  262. }
  263. #ifdef __cplusplus
  264. } // extern "C"
  265. } // namespace libyuv
  266. #endif