convert.cc 13 KB

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