2
0

yuvconvert.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * Copyright 2013 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. // Convert an ARGB image to YUV.
  11. // Usage: yuvconvert src_argb.raw dst_yuv.raw
  12. #ifndef _CRT_SECURE_NO_WARNINGS
  13. #define _CRT_SECURE_NO_WARNINGS
  14. #endif
  15. #include <stddef.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "libyuv/convert.h"
  20. #include "libyuv/planar_functions.h"
  21. #include "libyuv/scale_argb.h"
  22. // options
  23. bool verbose = false;
  24. bool attenuate = false;
  25. bool unattenuate = false;
  26. int image_width = 0, image_height = 0; // original width and height
  27. int dst_width = 0, dst_height = 0; // new width and height
  28. int fileindex_org = 0; // argv argument contains the original file name.
  29. int fileindex_rec = 0; // argv argument contains the reconstructed file name.
  30. int num_rec = 0; // Number of reconstructed images.
  31. int num_skip_org = 0; // Number of frames to skip in original.
  32. int num_frames = 0; // Number of frames to convert.
  33. int filter = 1; // Bilinear filter for scaling.
  34. static __inline uint32_t Abs(int32_t v) {
  35. return v >= 0 ? v : -v;
  36. }
  37. // Parse PYUV format. ie name.1920x800_24Hz_P420.yuv
  38. bool ExtractResolutionFromFilename(const char* name,
  39. int* width_ptr,
  40. int* height_ptr) {
  41. // Isolate the .width_height. section of the filename by searching for a
  42. // dot or underscore followed by a digit.
  43. for (int i = 0; name[i]; ++i) {
  44. if ((name[i] == '.' || name[i] == '_') && name[i + 1] >= '0' &&
  45. name[i + 1] <= '9') {
  46. int n = sscanf(name + i + 1, "%dx%d", width_ptr, height_ptr); // NOLINT
  47. if (2 == n) {
  48. return true;
  49. }
  50. }
  51. }
  52. return false;
  53. }
  54. void PrintHelp(const char* program) {
  55. printf("%s [-options] src_argb.raw dst_yuv.raw\n", program);
  56. printf(
  57. " -s <width> <height> .... specify source resolution. "
  58. "Optional if name contains\n"
  59. " resolution (ie. "
  60. "name.1920x800_24Hz_P420.yuv)\n"
  61. " Negative value mirrors.\n");
  62. printf(" -d <width> <height> .... specify destination resolution.\n");
  63. printf(" -f <filter> ............ 0 = point, 1 = bilinear (default).\n");
  64. printf(" -skip <src_argb> ....... Number of frame to skip of src_argb\n");
  65. printf(" -frames <num> .......... Number of frames to convert\n");
  66. printf(" -attenuate ............. Attenuate the ARGB image\n");
  67. printf(" -unattenuate ........... Unattenuate the ARGB image\n");
  68. printf(" -v ..................... verbose\n");
  69. printf(" -h ..................... this help\n");
  70. exit(0);
  71. }
  72. void ParseOptions(int argc, const char* argv[]) {
  73. if (argc <= 1) {
  74. PrintHelp(argv[0]);
  75. }
  76. for (int c = 1; c < argc; ++c) {
  77. if (!strcmp(argv[c], "-v")) {
  78. verbose = true;
  79. } else if (!strcmp(argv[c], "-attenuate")) {
  80. attenuate = true;
  81. } else if (!strcmp(argv[c], "-unattenuate")) {
  82. unattenuate = true;
  83. } else if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
  84. PrintHelp(argv[0]);
  85. } else if (!strcmp(argv[c], "-s") && c + 2 < argc) {
  86. image_width = atoi(argv[++c]); // NOLINT
  87. image_height = atoi(argv[++c]); // NOLINT
  88. } else if (!strcmp(argv[c], "-d") && c + 2 < argc) {
  89. dst_width = atoi(argv[++c]); // NOLINT
  90. dst_height = atoi(argv[++c]); // NOLINT
  91. } else if (!strcmp(argv[c], "-skip") && c + 1 < argc) {
  92. num_skip_org = atoi(argv[++c]); // NOLINT
  93. } else if (!strcmp(argv[c], "-frames") && c + 1 < argc) {
  94. num_frames = atoi(argv[++c]); // NOLINT
  95. } else if (!strcmp(argv[c], "-f") && c + 1 < argc) {
  96. filter = atoi(argv[++c]); // NOLINT
  97. } else if (argv[c][0] == '-') {
  98. fprintf(stderr, "Unknown option. %s\n", argv[c]);
  99. } else if (fileindex_org == 0) {
  100. fileindex_org = c;
  101. } else if (fileindex_rec == 0) {
  102. fileindex_rec = c;
  103. num_rec = 1;
  104. } else {
  105. ++num_rec;
  106. }
  107. }
  108. if (fileindex_org == 0 || fileindex_rec == 0) {
  109. fprintf(stderr, "Missing filenames\n");
  110. PrintHelp(argv[0]);
  111. }
  112. if (num_skip_org < 0) {
  113. fprintf(stderr, "Skipped frames incorrect\n");
  114. PrintHelp(argv[0]);
  115. }
  116. if (num_frames < 0) {
  117. fprintf(stderr, "Number of frames incorrect\n");
  118. PrintHelp(argv[0]);
  119. }
  120. int org_width, org_height;
  121. int rec_width, rec_height;
  122. bool org_res_avail = ExtractResolutionFromFilename(argv[fileindex_org],
  123. &org_width, &org_height);
  124. bool rec_res_avail = ExtractResolutionFromFilename(argv[fileindex_rec],
  125. &rec_width, &rec_height);
  126. if (image_width == 0 || image_height == 0) {
  127. if (org_res_avail) {
  128. image_width = org_width;
  129. image_height = org_height;
  130. } else if (rec_res_avail) {
  131. image_width = rec_width;
  132. image_height = rec_height;
  133. } else {
  134. fprintf(stderr, "Missing dimensions.\n");
  135. PrintHelp(argv[0]);
  136. }
  137. }
  138. if (dst_width == 0 || dst_height == 0) {
  139. if (rec_res_avail) {
  140. dst_width = rec_width;
  141. dst_height = rec_height;
  142. } else {
  143. dst_width = Abs(image_width);
  144. dst_height = Abs(image_height);
  145. }
  146. }
  147. }
  148. static const int kTileX = 32;
  149. static const int kTileY = 32;
  150. static int TileARGBScale(const uint8_t* src_argb,
  151. int src_stride_argb,
  152. int src_width,
  153. int src_height,
  154. uint8_t* dst_argb,
  155. int dst_stride_argb,
  156. int dst_width,
  157. int dst_height,
  158. libyuv::FilterMode filtering) {
  159. for (int y = 0; y < dst_height; y += kTileY) {
  160. for (int x = 0; x < dst_width; x += kTileX) {
  161. int clip_width = kTileX;
  162. if (x + clip_width > dst_width) {
  163. clip_width = dst_width - x;
  164. }
  165. int clip_height = kTileY;
  166. if (y + clip_height > dst_height) {
  167. clip_height = dst_height - y;
  168. }
  169. int r = libyuv::ARGBScaleClip(src_argb, src_stride_argb, src_width,
  170. src_height, dst_argb, dst_stride_argb,
  171. dst_width, dst_height, x, y, clip_width,
  172. clip_height, filtering);
  173. if (r) {
  174. return r;
  175. }
  176. }
  177. }
  178. return 0;
  179. }
  180. int main(int argc, const char* argv[]) {
  181. ParseOptions(argc, argv);
  182. // Open original file (first file argument)
  183. FILE* const file_org = fopen(argv[fileindex_org], "rb");
  184. if (file_org == NULL) {
  185. fprintf(stderr, "Cannot open %s\n", argv[fileindex_org]);
  186. exit(1);
  187. }
  188. // Open all files to convert to
  189. FILE** file_rec = new FILE*[num_rec];
  190. memset(file_rec, 0, num_rec * sizeof(FILE*)); // NOLINT
  191. for (int cur_rec = 0; cur_rec < num_rec; ++cur_rec) {
  192. file_rec[cur_rec] = fopen(argv[fileindex_rec + cur_rec], "wb");
  193. if (file_rec[cur_rec] == NULL) {
  194. fprintf(stderr, "Cannot open %s\n", argv[fileindex_rec + cur_rec]);
  195. fclose(file_org);
  196. for (int i = 0; i < cur_rec; ++i) {
  197. fclose(file_rec[i]);
  198. }
  199. delete[] file_rec;
  200. exit(1);
  201. }
  202. }
  203. bool org_is_yuv = strstr(argv[fileindex_org], "_P420.") != NULL;
  204. bool org_is_argb = strstr(argv[fileindex_org], "_ARGB.") != NULL;
  205. if (!org_is_yuv && !org_is_argb) {
  206. fprintf(stderr, "Original format unknown %s\n", argv[fileindex_org]);
  207. exit(1);
  208. }
  209. int org_size = Abs(image_width) * Abs(image_height) * 4; // ARGB
  210. // Input is YUV
  211. if (org_is_yuv) {
  212. const int y_size = Abs(image_width) * Abs(image_height);
  213. const int uv_size =
  214. ((Abs(image_width) + 1) / 2) * ((Abs(image_height) + 1) / 2);
  215. org_size = y_size + 2 * uv_size; // YUV original.
  216. }
  217. const int dst_size = dst_width * dst_height * 4; // ARGB scaled
  218. const int y_size = dst_width * dst_height;
  219. const int uv_size = ((dst_width + 1) / 2) * ((dst_height + 1) / 2);
  220. const size_t total_size = y_size + 2 * uv_size;
  221. #if defined(_MSC_VER)
  222. _fseeki64(file_org,
  223. static_cast<__int64>(num_skip_org) * static_cast<__int64>(org_size),
  224. SEEK_SET);
  225. #else
  226. fseek(file_org, num_skip_org * total_size, SEEK_SET);
  227. #endif
  228. uint8_t* const ch_org = new uint8_t[org_size];
  229. uint8_t* const ch_dst = new uint8_t[dst_size];
  230. uint8_t* const ch_rec = new uint8_t[total_size];
  231. if (ch_org == NULL || ch_rec == NULL) {
  232. fprintf(stderr, "No memory available\n");
  233. fclose(file_org);
  234. for (int i = 0; i < num_rec; ++i) {
  235. fclose(file_rec[i]);
  236. }
  237. delete[] ch_org;
  238. delete[] ch_dst;
  239. delete[] ch_rec;
  240. delete[] file_rec;
  241. exit(1);
  242. }
  243. if (verbose) {
  244. printf("Size: %dx%d to %dx%d\n", image_width, image_height, dst_width,
  245. dst_height);
  246. }
  247. int number_of_frames;
  248. for (number_of_frames = 0;; ++number_of_frames) {
  249. if (num_frames && number_of_frames >= num_frames) {
  250. break;
  251. }
  252. // Load original YUV or ARGB frame.
  253. size_t bytes_org =
  254. fread(ch_org, sizeof(uint8_t), static_cast<size_t>(org_size), file_org);
  255. if (bytes_org < static_cast<size_t>(org_size)) {
  256. break;
  257. }
  258. // TODO(fbarchard): Attenuate doesnt need to know dimensions.
  259. // ARGB attenuate frame
  260. if (org_is_argb && attenuate) {
  261. libyuv::ARGBAttenuate(ch_org, 0, ch_org, 0, org_size / 4, 1);
  262. }
  263. // ARGB unattenuate frame
  264. if (org_is_argb && unattenuate) {
  265. libyuv::ARGBUnattenuate(ch_org, 0, ch_org, 0, org_size / 4, 1);
  266. }
  267. for (int cur_rec = 0; cur_rec < num_rec; ++cur_rec) {
  268. // Scale YUV or ARGB frame.
  269. if (org_is_yuv) {
  270. int src_width = Abs(image_width);
  271. int src_height = Abs(image_height);
  272. int half_src_width = (src_width + 1) / 2;
  273. int half_src_height = (src_height + 1) / 2;
  274. int half_dst_width = (dst_width + 1) / 2;
  275. int half_dst_height = (dst_height + 1) / 2;
  276. I420Scale(
  277. ch_org, src_width, ch_org + src_width * src_height, half_src_width,
  278. ch_org + src_width * src_height + half_src_width * half_src_height,
  279. half_src_width, image_width, image_height, ch_rec, dst_width,
  280. ch_rec + dst_width * dst_height, half_dst_width,
  281. ch_rec + dst_width * dst_height + half_dst_width * half_dst_height,
  282. half_dst_width, dst_width, dst_height,
  283. static_cast<libyuv::FilterMode>(filter));
  284. } else {
  285. TileARGBScale(ch_org, Abs(image_width) * 4, image_width, image_height,
  286. ch_dst, dst_width * 4, dst_width, dst_height,
  287. static_cast<libyuv::FilterMode>(filter));
  288. }
  289. bool rec_is_yuv = strstr(argv[fileindex_rec + cur_rec], "_P420.") != NULL;
  290. bool rec_is_argb =
  291. strstr(argv[fileindex_rec + cur_rec], "_ARGB.") != NULL;
  292. if (!rec_is_yuv && !rec_is_argb) {
  293. fprintf(stderr, "Output format unknown %s\n",
  294. argv[fileindex_rec + cur_rec]);
  295. continue; // Advance to next file.
  296. }
  297. // Convert ARGB to YUV.
  298. if (!org_is_yuv && rec_is_yuv) {
  299. int half_width = (dst_width + 1) / 2;
  300. int half_height = (dst_height + 1) / 2;
  301. libyuv::ARGBToI420(
  302. ch_dst, dst_width * 4, ch_rec, dst_width,
  303. ch_rec + dst_width * dst_height, half_width,
  304. ch_rec + dst_width * dst_height + half_width * half_height,
  305. half_width, dst_width, dst_height);
  306. }
  307. // Output YUV or ARGB frame.
  308. if (rec_is_yuv) {
  309. size_t bytes_rec =
  310. fwrite(ch_rec, sizeof(uint8_t), static_cast<size_t>(total_size),
  311. file_rec[cur_rec]);
  312. if (bytes_rec < static_cast<size_t>(total_size)) {
  313. break;
  314. }
  315. } else {
  316. size_t bytes_rec =
  317. fwrite(ch_dst, sizeof(uint8_t), static_cast<size_t>(dst_size),
  318. file_rec[cur_rec]);
  319. if (bytes_rec < static_cast<size_t>(dst_size)) {
  320. break;
  321. }
  322. }
  323. if (verbose) {
  324. printf("%5d", number_of_frames);
  325. }
  326. if (verbose) {
  327. printf("\t%s", argv[fileindex_rec + cur_rec]);
  328. printf("\n");
  329. }
  330. }
  331. }
  332. fclose(file_org);
  333. for (int cur_rec = 0; cur_rec < num_rec; ++cur_rec) {
  334. fclose(file_rec[cur_rec]);
  335. }
  336. delete[] ch_org;
  337. delete[] ch_dst;
  338. delete[] ch_rec;
  339. delete[] file_rec;
  340. return 0;
  341. }