convert_to_i420.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. // Triplanar formats
  175. case FOURCC_I420:
  176. case FOURCC_YV12: {
  177. const uint8_t* src_y = sample + (src_width * crop_y + crop_x);
  178. const uint8_t* src_u;
  179. const uint8_t* src_v;
  180. int halfwidth = (src_width + 1) / 2;
  181. int halfheight = (abs_src_height + 1) / 2;
  182. if (format == FOURCC_YV12) {
  183. src_v = sample + src_width * abs_src_height + halfwidth * (crop_y / 2) +
  184. (crop_x / 2);
  185. src_u = sample + src_width * abs_src_height +
  186. halfwidth * (halfheight + (crop_y / 2)) + (crop_x / 2);
  187. } else {
  188. src_u = sample + src_width * abs_src_height + halfwidth * (crop_y / 2) +
  189. (crop_x / 2);
  190. src_v = sample + src_width * abs_src_height +
  191. halfwidth * (halfheight + (crop_y / 2)) + (crop_x / 2);
  192. }
  193. r = I420Rotate(src_y, src_width, src_u, halfwidth, src_v, halfwidth,
  194. dst_y, dst_stride_y, dst_u, dst_stride_u, dst_v,
  195. dst_stride_v, crop_width, inv_crop_height, rotation);
  196. break;
  197. }
  198. case FOURCC_I422:
  199. case FOURCC_YV16: {
  200. const uint8_t* src_y = sample + src_width * crop_y + crop_x;
  201. const uint8_t* src_u;
  202. const uint8_t* src_v;
  203. int halfwidth = (src_width + 1) / 2;
  204. if (format == FOURCC_YV16) {
  205. src_v = sample + src_width * abs_src_height + halfwidth * crop_y +
  206. (crop_x / 2);
  207. src_u = sample + src_width * abs_src_height +
  208. halfwidth * (abs_src_height + crop_y) + (crop_x / 2);
  209. } else {
  210. src_u = sample + src_width * abs_src_height + halfwidth * crop_y +
  211. (crop_x / 2);
  212. src_v = sample + src_width * abs_src_height +
  213. halfwidth * (abs_src_height + crop_y) + (crop_x / 2);
  214. }
  215. r = I422ToI420(src_y, src_width, src_u, halfwidth, src_v, halfwidth,
  216. dst_y, dst_stride_y, dst_u, dst_stride_u, dst_v,
  217. dst_stride_v, crop_width, inv_crop_height);
  218. break;
  219. }
  220. case FOURCC_I444:
  221. case FOURCC_YV24: {
  222. const uint8_t* src_y = sample + src_width * crop_y + crop_x;
  223. const uint8_t* src_u;
  224. const uint8_t* src_v;
  225. if (format == FOURCC_YV24) {
  226. src_v = sample + src_width * (abs_src_height + crop_y) + crop_x;
  227. src_u = sample + src_width * (abs_src_height * 2 + crop_y) + crop_x;
  228. } else {
  229. src_u = sample + src_width * (abs_src_height + crop_y) + crop_x;
  230. src_v = sample + src_width * (abs_src_height * 2 + crop_y) + crop_x;
  231. }
  232. r = I444ToI420(src_y, src_width, src_u, src_width, src_v, src_width,
  233. dst_y, dst_stride_y, dst_u, dst_stride_u, dst_v,
  234. dst_stride_v, crop_width, inv_crop_height);
  235. break;
  236. }
  237. #ifdef HAVE_JPEG
  238. case FOURCC_MJPG:
  239. r = MJPGToI420(sample, sample_size, dst_y, dst_stride_y, dst_u,
  240. dst_stride_u, dst_v, dst_stride_v, src_width,
  241. abs_src_height, crop_width, inv_crop_height);
  242. break;
  243. #endif
  244. default:
  245. r = -1; // unknown fourcc - return failure code.
  246. }
  247. if (need_buf) {
  248. if (!r) {
  249. r = I420Rotate(dst_y, dst_stride_y, dst_u, dst_stride_u, dst_v,
  250. dst_stride_v, tmp_y, tmp_y_stride, tmp_u, tmp_u_stride,
  251. tmp_v, tmp_v_stride, crop_width, abs_crop_height,
  252. rotation);
  253. }
  254. free(rotate_buffer);
  255. }
  256. return r;
  257. }
  258. #ifdef __cplusplus
  259. } // extern "C"
  260. } // namespace libyuv
  261. #endif