vpxdec.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. /*
  2. * Copyright (c) 2010 The WebM 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 <assert.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <stdarg.h>
  14. #include <string.h>
  15. #include <limits.h>
  16. #include "./vpx_config.h"
  17. #if CONFIG_LIBYUV
  18. #include "third_party/libyuv/include/libyuv/scale.h"
  19. #endif
  20. #include "./args.h"
  21. #include "./ivfdec.h"
  22. #include "vpx/vpx_decoder.h"
  23. #include "vpx_ports/mem_ops.h"
  24. #include "vpx_ports/vpx_timer.h"
  25. #if CONFIG_VP8_DECODER || CONFIG_VP9_DECODER
  26. #include "vpx/vp8dx.h"
  27. #endif
  28. #include "./md5_utils.h"
  29. #include "./tools_common.h"
  30. #if CONFIG_WEBM_IO
  31. #include "./webmdec.h"
  32. #endif
  33. #include "./y4menc.h"
  34. static const char *exec_name;
  35. struct VpxDecInputContext {
  36. struct VpxInputContext *vpx_input_ctx;
  37. struct WebmInputContext *webm_ctx;
  38. };
  39. static const arg_def_t looparg =
  40. ARG_DEF(NULL, "loops", 1, "Number of times to decode the file");
  41. static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1, "Codec to use");
  42. static const arg_def_t use_yv12 =
  43. ARG_DEF(NULL, "yv12", 0, "Output raw YV12 frames");
  44. static const arg_def_t use_i420 =
  45. ARG_DEF(NULL, "i420", 0, "Output raw I420 frames");
  46. static const arg_def_t flipuvarg =
  47. ARG_DEF(NULL, "flipuv", 0, "Flip the chroma planes in the output");
  48. static const arg_def_t rawvideo =
  49. ARG_DEF(NULL, "rawvideo", 0, "Output raw YUV frames");
  50. static const arg_def_t noblitarg =
  51. ARG_DEF(NULL, "noblit", 0, "Don't process the decoded frames");
  52. static const arg_def_t progressarg =
  53. ARG_DEF(NULL, "progress", 0, "Show progress after each frame decodes");
  54. static const arg_def_t limitarg =
  55. ARG_DEF(NULL, "limit", 1, "Stop decoding after n frames");
  56. static const arg_def_t skiparg =
  57. ARG_DEF(NULL, "skip", 1, "Skip the first n input frames");
  58. static const arg_def_t postprocarg =
  59. ARG_DEF(NULL, "postproc", 0, "Postprocess decoded frames");
  60. static const arg_def_t summaryarg =
  61. ARG_DEF(NULL, "summary", 0, "Show timing summary");
  62. static const arg_def_t outputfile =
  63. ARG_DEF("o", "output", 1, "Output file name pattern (see below)");
  64. static const arg_def_t threadsarg =
  65. ARG_DEF("t", "threads", 1, "Max threads to use");
  66. static const arg_def_t frameparallelarg =
  67. ARG_DEF(NULL, "frame-parallel", 0, "Frame parallel decode");
  68. static const arg_def_t verbosearg =
  69. ARG_DEF("v", "verbose", 0, "Show version string");
  70. static const arg_def_t error_concealment =
  71. ARG_DEF(NULL, "error-concealment", 0, "Enable decoder error-concealment");
  72. static const arg_def_t scalearg =
  73. ARG_DEF("S", "scale", 0, "Scale output frames uniformly");
  74. static const arg_def_t continuearg =
  75. ARG_DEF("k", "keep-going", 0, "(debug) Continue decoding after error");
  76. static const arg_def_t fb_arg =
  77. ARG_DEF(NULL, "frame-buffers", 1, "Number of frame buffers to use");
  78. static const arg_def_t md5arg =
  79. ARG_DEF(NULL, "md5", 0, "Compute the MD5 sum of the decoded frame");
  80. #if CONFIG_VP9_HIGHBITDEPTH
  81. static const arg_def_t outbitdeptharg =
  82. ARG_DEF(NULL, "output-bit-depth", 1, "Output bit-depth for decoded frames");
  83. #endif
  84. static const arg_def_t *all_args[] = { &codecarg,
  85. &use_yv12,
  86. &use_i420,
  87. &flipuvarg,
  88. &rawvideo,
  89. &noblitarg,
  90. &progressarg,
  91. &limitarg,
  92. &skiparg,
  93. &postprocarg,
  94. &summaryarg,
  95. &outputfile,
  96. &threadsarg,
  97. &frameparallelarg,
  98. &verbosearg,
  99. &scalearg,
  100. &fb_arg,
  101. &md5arg,
  102. &error_concealment,
  103. &continuearg,
  104. #if CONFIG_VP9_HIGHBITDEPTH
  105. &outbitdeptharg,
  106. #endif
  107. NULL };
  108. #if CONFIG_VP8_DECODER
  109. static const arg_def_t addnoise_level =
  110. ARG_DEF(NULL, "noise-level", 1, "Enable VP8 postproc add noise");
  111. static const arg_def_t deblock =
  112. ARG_DEF(NULL, "deblock", 0, "Enable VP8 deblocking");
  113. static const arg_def_t demacroblock_level = ARG_DEF(
  114. NULL, "demacroblock-level", 1, "Enable VP8 demacroblocking, w/ level");
  115. static const arg_def_t mfqe =
  116. ARG_DEF(NULL, "mfqe", 0, "Enable multiframe quality enhancement");
  117. static const arg_def_t *vp8_pp_args[] = { &addnoise_level, &deblock,
  118. &demacroblock_level, &mfqe, NULL };
  119. #endif
  120. #if CONFIG_LIBYUV
  121. static INLINE int libyuv_scale(vpx_image_t *src, vpx_image_t *dst,
  122. FilterModeEnum mode) {
  123. #if CONFIG_VP9_HIGHBITDEPTH
  124. if (src->fmt == VPX_IMG_FMT_I42016) {
  125. assert(dst->fmt == VPX_IMG_FMT_I42016);
  126. return I420Scale_16(
  127. (uint16_t *)src->planes[VPX_PLANE_Y], src->stride[VPX_PLANE_Y] / 2,
  128. (uint16_t *)src->planes[VPX_PLANE_U], src->stride[VPX_PLANE_U] / 2,
  129. (uint16_t *)src->planes[VPX_PLANE_V], src->stride[VPX_PLANE_V] / 2,
  130. src->d_w, src->d_h, (uint16_t *)dst->planes[VPX_PLANE_Y],
  131. dst->stride[VPX_PLANE_Y] / 2, (uint16_t *)dst->planes[VPX_PLANE_U],
  132. dst->stride[VPX_PLANE_U] / 2, (uint16_t *)dst->planes[VPX_PLANE_V],
  133. dst->stride[VPX_PLANE_V] / 2, dst->d_w, dst->d_h, mode);
  134. }
  135. #endif
  136. assert(src->fmt == VPX_IMG_FMT_I420);
  137. assert(dst->fmt == VPX_IMG_FMT_I420);
  138. return I420Scale(src->planes[VPX_PLANE_Y], src->stride[VPX_PLANE_Y],
  139. src->planes[VPX_PLANE_U], src->stride[VPX_PLANE_U],
  140. src->planes[VPX_PLANE_V], src->stride[VPX_PLANE_V], src->d_w,
  141. src->d_h, dst->planes[VPX_PLANE_Y], dst->stride[VPX_PLANE_Y],
  142. dst->planes[VPX_PLANE_U], dst->stride[VPX_PLANE_U],
  143. dst->planes[VPX_PLANE_V], dst->stride[VPX_PLANE_V], dst->d_w,
  144. dst->d_h, mode);
  145. }
  146. #endif
  147. void usage_exit(void) {
  148. int i;
  149. fprintf(stderr,
  150. "Usage: %s <options> filename\n\n"
  151. "Options:\n",
  152. exec_name);
  153. arg_show_usage(stderr, all_args);
  154. #if CONFIG_VP8_DECODER
  155. fprintf(stderr, "\nVP8 Postprocessing Options:\n");
  156. arg_show_usage(stderr, vp8_pp_args);
  157. #endif
  158. fprintf(stderr,
  159. "\nOutput File Patterns:\n\n"
  160. " The -o argument specifies the name of the file(s) to "
  161. "write to. If the\n argument does not include any escape "
  162. "characters, the output will be\n written to a single file. "
  163. "Otherwise, the filename will be calculated by\n expanding "
  164. "the following escape characters:\n");
  165. fprintf(stderr,
  166. "\n\t%%w - Frame width"
  167. "\n\t%%h - Frame height"
  168. "\n\t%%<n> - Frame number, zero padded to <n> places (1..9)"
  169. "\n\n Pattern arguments are only supported in conjunction "
  170. "with the --yv12 and\n --i420 options. If the -o option is "
  171. "not specified, the output will be\n directed to stdout.\n");
  172. fprintf(stderr, "\nIncluded decoders:\n\n");
  173. for (i = 0; i < get_vpx_decoder_count(); ++i) {
  174. const VpxInterface *const decoder = get_vpx_decoder_by_index(i);
  175. fprintf(stderr, " %-6s - %s\n", decoder->name,
  176. vpx_codec_iface_name(decoder->codec_interface()));
  177. }
  178. exit(EXIT_FAILURE);
  179. }
  180. static int raw_read_frame(FILE *infile, uint8_t **buffer, size_t *bytes_read,
  181. size_t *buffer_size) {
  182. char raw_hdr[RAW_FRAME_HDR_SZ];
  183. size_t frame_size = 0;
  184. if (fread(raw_hdr, RAW_FRAME_HDR_SZ, 1, infile) != 1) {
  185. if (!feof(infile)) warn("Failed to read RAW frame size\n");
  186. } else {
  187. const size_t kCorruptFrameThreshold = 256 * 1024 * 1024;
  188. const size_t kFrameTooSmallThreshold = 256 * 1024;
  189. frame_size = mem_get_le32(raw_hdr);
  190. if (frame_size > kCorruptFrameThreshold) {
  191. warn("Read invalid frame size (%u)\n", (unsigned int)frame_size);
  192. frame_size = 0;
  193. }
  194. if (frame_size < kFrameTooSmallThreshold) {
  195. warn("Warning: Read invalid frame size (%u) - not a raw file?\n",
  196. (unsigned int)frame_size);
  197. }
  198. if (frame_size > *buffer_size) {
  199. uint8_t *new_buf = realloc(*buffer, 2 * frame_size);
  200. if (new_buf) {
  201. *buffer = new_buf;
  202. *buffer_size = 2 * frame_size;
  203. } else {
  204. warn("Failed to allocate compressed data buffer\n");
  205. frame_size = 0;
  206. }
  207. }
  208. }
  209. if (!feof(infile)) {
  210. if (fread(*buffer, 1, frame_size, infile) != frame_size) {
  211. warn("Failed to read full frame\n");
  212. return 1;
  213. }
  214. *bytes_read = frame_size;
  215. }
  216. return 0;
  217. }
  218. static int read_frame(struct VpxDecInputContext *input, uint8_t **buf,
  219. size_t *bytes_in_buffer, size_t *buffer_size) {
  220. switch (input->vpx_input_ctx->file_type) {
  221. #if CONFIG_WEBM_IO
  222. case FILE_TYPE_WEBM:
  223. return webm_read_frame(input->webm_ctx, buf, bytes_in_buffer);
  224. #endif
  225. case FILE_TYPE_RAW:
  226. return raw_read_frame(input->vpx_input_ctx->file, buf, bytes_in_buffer,
  227. buffer_size);
  228. case FILE_TYPE_IVF:
  229. return ivf_read_frame(input->vpx_input_ctx->file, buf, bytes_in_buffer,
  230. buffer_size);
  231. default: return 1;
  232. }
  233. }
  234. static void update_image_md5(const vpx_image_t *img, const int planes[3],
  235. MD5Context *md5) {
  236. int i, y;
  237. for (i = 0; i < 3; ++i) {
  238. const int plane = planes[i];
  239. const unsigned char *buf = img->planes[plane];
  240. const int stride = img->stride[plane];
  241. const int w = vpx_img_plane_width(img, plane) *
  242. ((img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
  243. const int h = vpx_img_plane_height(img, plane);
  244. for (y = 0; y < h; ++y) {
  245. MD5Update(md5, buf, w);
  246. buf += stride;
  247. }
  248. }
  249. }
  250. static void write_image_file(const vpx_image_t *img, const int planes[3],
  251. FILE *file) {
  252. int i, y;
  253. #if CONFIG_VP9_HIGHBITDEPTH
  254. const int bytes_per_sample = ((img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
  255. #else
  256. const int bytes_per_sample = 1;
  257. #endif
  258. for (i = 0; i < 3; ++i) {
  259. const int plane = planes[i];
  260. const unsigned char *buf = img->planes[plane];
  261. const int stride = img->stride[plane];
  262. const int w = vpx_img_plane_width(img, plane);
  263. const int h = vpx_img_plane_height(img, plane);
  264. for (y = 0; y < h; ++y) {
  265. fwrite(buf, bytes_per_sample, w, file);
  266. buf += stride;
  267. }
  268. }
  269. }
  270. static int file_is_raw(struct VpxInputContext *input) {
  271. uint8_t buf[32];
  272. int is_raw = 0;
  273. vpx_codec_stream_info_t si;
  274. si.sz = sizeof(si);
  275. if (fread(buf, 1, 32, input->file) == 32) {
  276. int i;
  277. if (mem_get_le32(buf) < 256 * 1024 * 1024) {
  278. for (i = 0; i < get_vpx_decoder_count(); ++i) {
  279. const VpxInterface *const decoder = get_vpx_decoder_by_index(i);
  280. if (!vpx_codec_peek_stream_info(decoder->codec_interface(), buf + 4,
  281. 32 - 4, &si)) {
  282. is_raw = 1;
  283. input->fourcc = decoder->fourcc;
  284. input->width = si.w;
  285. input->height = si.h;
  286. input->framerate.numerator = 30;
  287. input->framerate.denominator = 1;
  288. break;
  289. }
  290. }
  291. }
  292. }
  293. rewind(input->file);
  294. return is_raw;
  295. }
  296. static void show_progress(int frame_in, int frame_out, uint64_t dx_time) {
  297. fprintf(stderr,
  298. "%d decoded frames/%d showed frames in %" PRId64 " us (%.2f fps)\r",
  299. frame_in, frame_out, dx_time,
  300. (double)frame_out * 1000000.0 / (double)dx_time);
  301. }
  302. struct ExternalFrameBuffer {
  303. uint8_t *data;
  304. size_t size;
  305. int in_use;
  306. };
  307. struct ExternalFrameBufferList {
  308. int num_external_frame_buffers;
  309. struct ExternalFrameBuffer *ext_fb;
  310. };
  311. // Callback used by libvpx to request an external frame buffer. |cb_priv|
  312. // Application private data passed into the set function. |min_size| is the
  313. // minimum size in bytes needed to decode the next frame. |fb| pointer to the
  314. // frame buffer.
  315. static int get_vp9_frame_buffer(void *cb_priv, size_t min_size,
  316. vpx_codec_frame_buffer_t *fb) {
  317. int i;
  318. struct ExternalFrameBufferList *const ext_fb_list =
  319. (struct ExternalFrameBufferList *)cb_priv;
  320. if (ext_fb_list == NULL) return -1;
  321. // Find a free frame buffer.
  322. for (i = 0; i < ext_fb_list->num_external_frame_buffers; ++i) {
  323. if (!ext_fb_list->ext_fb[i].in_use) break;
  324. }
  325. if (i == ext_fb_list->num_external_frame_buffers) return -1;
  326. if (ext_fb_list->ext_fb[i].size < min_size) {
  327. free(ext_fb_list->ext_fb[i].data);
  328. ext_fb_list->ext_fb[i].data = (uint8_t *)calloc(min_size, sizeof(uint8_t));
  329. if (!ext_fb_list->ext_fb[i].data) return -1;
  330. ext_fb_list->ext_fb[i].size = min_size;
  331. }
  332. fb->data = ext_fb_list->ext_fb[i].data;
  333. fb->size = ext_fb_list->ext_fb[i].size;
  334. ext_fb_list->ext_fb[i].in_use = 1;
  335. // Set the frame buffer's private data to point at the external frame buffer.
  336. fb->priv = &ext_fb_list->ext_fb[i];
  337. return 0;
  338. }
  339. // Callback used by libvpx when there are no references to the frame buffer.
  340. // |cb_priv| user private data passed into the set function. |fb| pointer
  341. // to the frame buffer.
  342. static int release_vp9_frame_buffer(void *cb_priv,
  343. vpx_codec_frame_buffer_t *fb) {
  344. struct ExternalFrameBuffer *const ext_fb =
  345. (struct ExternalFrameBuffer *)fb->priv;
  346. (void)cb_priv;
  347. ext_fb->in_use = 0;
  348. return 0;
  349. }
  350. static void generate_filename(const char *pattern, char *out, size_t q_len,
  351. unsigned int d_w, unsigned int d_h,
  352. unsigned int frame_in) {
  353. const char *p = pattern;
  354. char *q = out;
  355. do {
  356. char *next_pat = strchr(p, '%');
  357. if (p == next_pat) {
  358. size_t pat_len;
  359. /* parse the pattern */
  360. q[q_len - 1] = '\0';
  361. switch (p[1]) {
  362. case 'w': snprintf(q, q_len - 1, "%d", d_w); break;
  363. case 'h': snprintf(q, q_len - 1, "%d", d_h); break;
  364. case '1': snprintf(q, q_len - 1, "%d", frame_in); break;
  365. case '2': snprintf(q, q_len - 1, "%02d", frame_in); break;
  366. case '3': snprintf(q, q_len - 1, "%03d", frame_in); break;
  367. case '4': snprintf(q, q_len - 1, "%04d", frame_in); break;
  368. case '5': snprintf(q, q_len - 1, "%05d", frame_in); break;
  369. case '6': snprintf(q, q_len - 1, "%06d", frame_in); break;
  370. case '7': snprintf(q, q_len - 1, "%07d", frame_in); break;
  371. case '8': snprintf(q, q_len - 1, "%08d", frame_in); break;
  372. case '9': snprintf(q, q_len - 1, "%09d", frame_in); break;
  373. default: die("Unrecognized pattern %%%c\n", p[1]); break;
  374. }
  375. pat_len = strlen(q);
  376. if (pat_len >= q_len - 1) die("Output filename too long.\n");
  377. q += pat_len;
  378. p += 2;
  379. q_len -= pat_len;
  380. } else {
  381. size_t copy_len;
  382. /* copy the next segment */
  383. if (!next_pat)
  384. copy_len = strlen(p);
  385. else
  386. copy_len = next_pat - p;
  387. if (copy_len >= q_len - 1) die("Output filename too long.\n");
  388. memcpy(q, p, copy_len);
  389. q[copy_len] = '\0';
  390. q += copy_len;
  391. p += copy_len;
  392. q_len -= copy_len;
  393. }
  394. } while (*p);
  395. }
  396. static int is_single_file(const char *outfile_pattern) {
  397. const char *p = outfile_pattern;
  398. do {
  399. p = strchr(p, '%');
  400. if (p && p[1] >= '1' && p[1] <= '9')
  401. return 0; // pattern contains sequence number, so it's not unique
  402. if (p) p++;
  403. } while (p);
  404. return 1;
  405. }
  406. static void print_md5(unsigned char digest[16], const char *filename) {
  407. int i;
  408. for (i = 0; i < 16; ++i) printf("%02x", digest[i]);
  409. printf(" %s\n", filename);
  410. }
  411. static FILE *open_outfile(const char *name) {
  412. if (strcmp("-", name) == 0) {
  413. set_binary_mode(stdout);
  414. return stdout;
  415. } else {
  416. FILE *file = fopen(name, "wb");
  417. if (!file) fatal("Failed to open output file '%s'", name);
  418. return file;
  419. }
  420. }
  421. #if CONFIG_VP9_HIGHBITDEPTH
  422. static int img_shifted_realloc_required(const vpx_image_t *img,
  423. const vpx_image_t *shifted,
  424. vpx_img_fmt_t required_fmt) {
  425. return img->d_w != shifted->d_w || img->d_h != shifted->d_h ||
  426. required_fmt != shifted->fmt;
  427. }
  428. #endif
  429. static int main_loop(int argc, const char **argv_) {
  430. vpx_codec_ctx_t decoder;
  431. char *fn = NULL;
  432. int i;
  433. uint8_t *buf = NULL;
  434. size_t bytes_in_buffer = 0, buffer_size = 0;
  435. FILE *infile;
  436. int frame_in = 0, frame_out = 0, flipuv = 0, noblit = 0;
  437. int do_md5 = 0, progress = 0, frame_parallel = 0;
  438. int stop_after = 0, postproc = 0, summary = 0, quiet = 1;
  439. int arg_skip = 0;
  440. int ec_enabled = 0;
  441. int keep_going = 0;
  442. const VpxInterface *interface = NULL;
  443. const VpxInterface *fourcc_interface = NULL;
  444. uint64_t dx_time = 0;
  445. struct arg arg;
  446. char **argv, **argi, **argj;
  447. int single_file;
  448. int use_y4m = 1;
  449. int opt_yv12 = 0;
  450. int opt_i420 = 0;
  451. vpx_codec_dec_cfg_t cfg = { 0, 0, 0 };
  452. #if CONFIG_VP9_HIGHBITDEPTH
  453. unsigned int output_bit_depth = 0;
  454. #endif
  455. #if CONFIG_VP8_DECODER
  456. vp8_postproc_cfg_t vp8_pp_cfg = { 0, 0, 0 };
  457. #endif
  458. int frames_corrupted = 0;
  459. int dec_flags = 0;
  460. int do_scale = 0;
  461. vpx_image_t *scaled_img = NULL;
  462. #if CONFIG_VP9_HIGHBITDEPTH
  463. vpx_image_t *img_shifted = NULL;
  464. #endif
  465. int frame_avail, got_data, flush_decoder = 0;
  466. int num_external_frame_buffers = 0;
  467. struct ExternalFrameBufferList ext_fb_list = { 0, NULL };
  468. const char *outfile_pattern = NULL;
  469. char outfile_name[PATH_MAX] = { 0 };
  470. FILE *outfile = NULL;
  471. MD5Context md5_ctx;
  472. unsigned char md5_digest[16];
  473. struct VpxDecInputContext input = { NULL, NULL };
  474. struct VpxInputContext vpx_input_ctx;
  475. #if CONFIG_WEBM_IO
  476. struct WebmInputContext webm_ctx;
  477. memset(&(webm_ctx), 0, sizeof(webm_ctx));
  478. input.webm_ctx = &webm_ctx;
  479. #endif
  480. input.vpx_input_ctx = &vpx_input_ctx;
  481. /* Parse command line */
  482. exec_name = argv_[0];
  483. argv = argv_dup(argc - 1, argv_ + 1);
  484. for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
  485. memset(&arg, 0, sizeof(arg));
  486. arg.argv_step = 1;
  487. if (arg_match(&arg, &codecarg, argi)) {
  488. interface = get_vpx_decoder_by_name(arg.val);
  489. if (!interface)
  490. die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
  491. } else if (arg_match(&arg, &looparg, argi)) {
  492. // no-op
  493. } else if (arg_match(&arg, &outputfile, argi))
  494. outfile_pattern = arg.val;
  495. else if (arg_match(&arg, &use_yv12, argi)) {
  496. use_y4m = 0;
  497. flipuv = 1;
  498. opt_yv12 = 1;
  499. } else if (arg_match(&arg, &use_i420, argi)) {
  500. use_y4m = 0;
  501. flipuv = 0;
  502. opt_i420 = 1;
  503. } else if (arg_match(&arg, &rawvideo, argi)) {
  504. use_y4m = 0;
  505. } else if (arg_match(&arg, &flipuvarg, argi))
  506. flipuv = 1;
  507. else if (arg_match(&arg, &noblitarg, argi))
  508. noblit = 1;
  509. else if (arg_match(&arg, &progressarg, argi))
  510. progress = 1;
  511. else if (arg_match(&arg, &limitarg, argi))
  512. stop_after = arg_parse_uint(&arg);
  513. else if (arg_match(&arg, &skiparg, argi))
  514. arg_skip = arg_parse_uint(&arg);
  515. else if (arg_match(&arg, &postprocarg, argi))
  516. postproc = 1;
  517. else if (arg_match(&arg, &md5arg, argi))
  518. do_md5 = 1;
  519. else if (arg_match(&arg, &summaryarg, argi))
  520. summary = 1;
  521. else if (arg_match(&arg, &threadsarg, argi))
  522. cfg.threads = arg_parse_uint(&arg);
  523. #if CONFIG_VP9_DECODER
  524. else if (arg_match(&arg, &frameparallelarg, argi))
  525. frame_parallel = 1;
  526. #endif
  527. else if (arg_match(&arg, &verbosearg, argi))
  528. quiet = 0;
  529. else if (arg_match(&arg, &scalearg, argi))
  530. do_scale = 1;
  531. else if (arg_match(&arg, &fb_arg, argi))
  532. num_external_frame_buffers = arg_parse_uint(&arg);
  533. else if (arg_match(&arg, &continuearg, argi))
  534. keep_going = 1;
  535. #if CONFIG_VP9_HIGHBITDEPTH
  536. else if (arg_match(&arg, &outbitdeptharg, argi)) {
  537. output_bit_depth = arg_parse_uint(&arg);
  538. }
  539. #endif
  540. #if CONFIG_VP8_DECODER
  541. else if (arg_match(&arg, &addnoise_level, argi)) {
  542. postproc = 1;
  543. vp8_pp_cfg.post_proc_flag |= VP8_ADDNOISE;
  544. vp8_pp_cfg.noise_level = arg_parse_uint(&arg);
  545. } else if (arg_match(&arg, &demacroblock_level, argi)) {
  546. postproc = 1;
  547. vp8_pp_cfg.post_proc_flag |= VP8_DEMACROBLOCK;
  548. vp8_pp_cfg.deblocking_level = arg_parse_uint(&arg);
  549. } else if (arg_match(&arg, &deblock, argi)) {
  550. postproc = 1;
  551. vp8_pp_cfg.post_proc_flag |= VP8_DEBLOCK;
  552. } else if (arg_match(&arg, &mfqe, argi)) {
  553. postproc = 1;
  554. vp8_pp_cfg.post_proc_flag |= VP8_MFQE;
  555. } else if (arg_match(&arg, &error_concealment, argi)) {
  556. ec_enabled = 1;
  557. }
  558. #endif // CONFIG_VP8_DECODER
  559. else
  560. argj++;
  561. }
  562. /* Check for unrecognized options */
  563. for (argi = argv; *argi; argi++)
  564. if (argi[0][0] == '-' && strlen(argi[0]) > 1)
  565. die("Error: Unrecognized option %s\n", *argi);
  566. /* Handle non-option arguments */
  567. fn = argv[0];
  568. if (!fn) {
  569. free(argv);
  570. usage_exit();
  571. }
  572. /* Open file */
  573. infile = strcmp(fn, "-") ? fopen(fn, "rb") : set_binary_mode(stdin);
  574. if (!infile) {
  575. fatal("Failed to open input file '%s'", strcmp(fn, "-") ? fn : "stdin");
  576. }
  577. #if CONFIG_OS_SUPPORT
  578. /* Make sure we don't dump to the terminal, unless forced to with -o - */
  579. if (!outfile_pattern && isatty(fileno(stdout)) && !do_md5 && !noblit) {
  580. fprintf(stderr,
  581. "Not dumping raw video to your terminal. Use '-o -' to "
  582. "override.\n");
  583. return EXIT_FAILURE;
  584. }
  585. #endif
  586. input.vpx_input_ctx->file = infile;
  587. if (file_is_ivf(input.vpx_input_ctx))
  588. input.vpx_input_ctx->file_type = FILE_TYPE_IVF;
  589. #if CONFIG_WEBM_IO
  590. else if (file_is_webm(input.webm_ctx, input.vpx_input_ctx))
  591. input.vpx_input_ctx->file_type = FILE_TYPE_WEBM;
  592. #endif
  593. else if (file_is_raw(input.vpx_input_ctx))
  594. input.vpx_input_ctx->file_type = FILE_TYPE_RAW;
  595. else {
  596. fprintf(stderr, "Unrecognized input file type.\n");
  597. #if !CONFIG_WEBM_IO
  598. fprintf(stderr, "vpxdec was built without WebM container support.\n");
  599. #endif
  600. return EXIT_FAILURE;
  601. }
  602. outfile_pattern = outfile_pattern ? outfile_pattern : "-";
  603. single_file = is_single_file(outfile_pattern);
  604. if (!noblit && single_file) {
  605. generate_filename(outfile_pattern, outfile_name, PATH_MAX,
  606. vpx_input_ctx.width, vpx_input_ctx.height, 0);
  607. if (do_md5)
  608. MD5Init(&md5_ctx);
  609. else
  610. outfile = open_outfile(outfile_name);
  611. }
  612. if (use_y4m && !noblit) {
  613. if (!single_file) {
  614. fprintf(stderr,
  615. "YUV4MPEG2 not supported with output patterns,"
  616. " try --i420 or --yv12 or --rawvideo.\n");
  617. return EXIT_FAILURE;
  618. }
  619. #if CONFIG_WEBM_IO
  620. if (vpx_input_ctx.file_type == FILE_TYPE_WEBM) {
  621. if (webm_guess_framerate(input.webm_ctx, input.vpx_input_ctx)) {
  622. fprintf(stderr,
  623. "Failed to guess framerate -- error parsing "
  624. "webm file?\n");
  625. return EXIT_FAILURE;
  626. }
  627. }
  628. #endif
  629. }
  630. fourcc_interface = get_vpx_decoder_by_fourcc(vpx_input_ctx.fourcc);
  631. if (interface && fourcc_interface && interface != fourcc_interface)
  632. warn("Header indicates codec: %s\n", fourcc_interface->name);
  633. else
  634. interface = fourcc_interface;
  635. if (!interface) interface = get_vpx_decoder_by_index(0);
  636. dec_flags = (postproc ? VPX_CODEC_USE_POSTPROC : 0) |
  637. (ec_enabled ? VPX_CODEC_USE_ERROR_CONCEALMENT : 0) |
  638. (frame_parallel ? VPX_CODEC_USE_FRAME_THREADING : 0);
  639. if (vpx_codec_dec_init(&decoder, interface->codec_interface(), &cfg,
  640. dec_flags)) {
  641. fprintf(stderr, "Failed to initialize decoder: %s\n",
  642. vpx_codec_error(&decoder));
  643. return EXIT_FAILURE;
  644. }
  645. if (!quiet) fprintf(stderr, "%s\n", decoder.name);
  646. #if CONFIG_VP8_DECODER
  647. if (vp8_pp_cfg.post_proc_flag &&
  648. vpx_codec_control(&decoder, VP8_SET_POSTPROC, &vp8_pp_cfg)) {
  649. fprintf(stderr, "Failed to configure postproc: %s\n",
  650. vpx_codec_error(&decoder));
  651. return EXIT_FAILURE;
  652. }
  653. #endif
  654. if (arg_skip) fprintf(stderr, "Skipping first %d frames.\n", arg_skip);
  655. while (arg_skip) {
  656. if (read_frame(&input, &buf, &bytes_in_buffer, &buffer_size)) break;
  657. arg_skip--;
  658. }
  659. if (num_external_frame_buffers > 0) {
  660. ext_fb_list.num_external_frame_buffers = num_external_frame_buffers;
  661. ext_fb_list.ext_fb = (struct ExternalFrameBuffer *)calloc(
  662. num_external_frame_buffers, sizeof(*ext_fb_list.ext_fb));
  663. if (vpx_codec_set_frame_buffer_functions(&decoder, get_vp9_frame_buffer,
  664. release_vp9_frame_buffer,
  665. &ext_fb_list)) {
  666. fprintf(stderr, "Failed to configure external frame buffers: %s\n",
  667. vpx_codec_error(&decoder));
  668. return EXIT_FAILURE;
  669. }
  670. }
  671. frame_avail = 1;
  672. got_data = 0;
  673. /* Decode file */
  674. while (frame_avail || got_data) {
  675. vpx_codec_iter_t iter = NULL;
  676. vpx_image_t *img;
  677. struct vpx_usec_timer timer;
  678. int corrupted = 0;
  679. frame_avail = 0;
  680. if (!stop_after || frame_in < stop_after) {
  681. if (!read_frame(&input, &buf, &bytes_in_buffer, &buffer_size)) {
  682. frame_avail = 1;
  683. frame_in++;
  684. vpx_usec_timer_start(&timer);
  685. if (vpx_codec_decode(&decoder, buf, (unsigned int)bytes_in_buffer, NULL,
  686. 0)) {
  687. const char *detail = vpx_codec_error_detail(&decoder);
  688. warn("Failed to decode frame %d: %s", frame_in,
  689. vpx_codec_error(&decoder));
  690. if (detail) warn("Additional information: %s", detail);
  691. if (!keep_going) goto fail;
  692. }
  693. vpx_usec_timer_mark(&timer);
  694. dx_time += vpx_usec_timer_elapsed(&timer);
  695. } else {
  696. flush_decoder = 1;
  697. }
  698. } else {
  699. flush_decoder = 1;
  700. }
  701. vpx_usec_timer_start(&timer);
  702. if (flush_decoder) {
  703. // Flush the decoder in frame parallel decode.
  704. if (vpx_codec_decode(&decoder, NULL, 0, NULL, 0)) {
  705. warn("Failed to flush decoder: %s", vpx_codec_error(&decoder));
  706. }
  707. }
  708. got_data = 0;
  709. if ((img = vpx_codec_get_frame(&decoder, &iter))) {
  710. ++frame_out;
  711. got_data = 1;
  712. }
  713. vpx_usec_timer_mark(&timer);
  714. dx_time += (unsigned int)vpx_usec_timer_elapsed(&timer);
  715. if (!frame_parallel &&
  716. vpx_codec_control(&decoder, VP8D_GET_FRAME_CORRUPTED, &corrupted)) {
  717. warn("Failed VP8_GET_FRAME_CORRUPTED: %s", vpx_codec_error(&decoder));
  718. if (!keep_going) goto fail;
  719. }
  720. frames_corrupted += corrupted;
  721. if (progress) show_progress(frame_in, frame_out, dx_time);
  722. if (!noblit && img) {
  723. const int PLANES_YUV[] = { VPX_PLANE_Y, VPX_PLANE_U, VPX_PLANE_V };
  724. const int PLANES_YVU[] = { VPX_PLANE_Y, VPX_PLANE_V, VPX_PLANE_U };
  725. const int *planes = flipuv ? PLANES_YVU : PLANES_YUV;
  726. if (do_scale) {
  727. if (frame_out == 1) {
  728. // If the output frames are to be scaled to a fixed display size then
  729. // use the width and height specified in the container. If either of
  730. // these is set to 0, use the display size set in the first frame
  731. // header. If that is unavailable, use the raw decoded size of the
  732. // first decoded frame.
  733. int render_width = vpx_input_ctx.width;
  734. int render_height = vpx_input_ctx.height;
  735. if (!render_width || !render_height) {
  736. int render_size[2];
  737. if (vpx_codec_control(&decoder, VP9D_GET_DISPLAY_SIZE,
  738. render_size)) {
  739. // As last resort use size of first frame as display size.
  740. render_width = img->d_w;
  741. render_height = img->d_h;
  742. } else {
  743. render_width = render_size[0];
  744. render_height = render_size[1];
  745. }
  746. }
  747. scaled_img =
  748. vpx_img_alloc(NULL, img->fmt, render_width, render_height, 16);
  749. scaled_img->bit_depth = img->bit_depth;
  750. }
  751. if (img->d_w != scaled_img->d_w || img->d_h != scaled_img->d_h) {
  752. #if CONFIG_LIBYUV
  753. libyuv_scale(img, scaled_img, kFilterBox);
  754. img = scaled_img;
  755. #else
  756. fprintf(stderr,
  757. "Failed to scale output frame: %s.\n"
  758. "Scaling is disabled in this configuration. "
  759. "To enable scaling, configure with --enable-libyuv\n",
  760. vpx_codec_error(&decoder));
  761. return EXIT_FAILURE;
  762. #endif
  763. }
  764. }
  765. #if CONFIG_VP9_HIGHBITDEPTH
  766. // Default to codec bit depth if output bit depth not set
  767. if (!output_bit_depth && single_file && !do_md5) {
  768. output_bit_depth = img->bit_depth;
  769. }
  770. // Shift up or down if necessary
  771. if (output_bit_depth != 0 && output_bit_depth != img->bit_depth) {
  772. const vpx_img_fmt_t shifted_fmt =
  773. output_bit_depth == 8
  774. ? img->fmt ^ (img->fmt & VPX_IMG_FMT_HIGHBITDEPTH)
  775. : img->fmt | VPX_IMG_FMT_HIGHBITDEPTH;
  776. if (img_shifted &&
  777. img_shifted_realloc_required(img, img_shifted, shifted_fmt)) {
  778. vpx_img_free(img_shifted);
  779. img_shifted = NULL;
  780. }
  781. if (!img_shifted) {
  782. img_shifted =
  783. vpx_img_alloc(NULL, shifted_fmt, img->d_w, img->d_h, 16);
  784. img_shifted->bit_depth = output_bit_depth;
  785. }
  786. if (output_bit_depth > img->bit_depth) {
  787. vpx_img_upshift(img_shifted, img, output_bit_depth - img->bit_depth);
  788. } else {
  789. vpx_img_downshift(img_shifted, img,
  790. img->bit_depth - output_bit_depth);
  791. }
  792. img = img_shifted;
  793. }
  794. #endif
  795. if (single_file) {
  796. if (use_y4m) {
  797. char buf[Y4M_BUFFER_SIZE] = { 0 };
  798. size_t len = 0;
  799. if (img->fmt == VPX_IMG_FMT_I440 || img->fmt == VPX_IMG_FMT_I44016) {
  800. fprintf(stderr, "Cannot produce y4m output for 440 sampling.\n");
  801. goto fail;
  802. }
  803. if (frame_out == 1) {
  804. // Y4M file header
  805. len = y4m_write_file_header(
  806. buf, sizeof(buf), vpx_input_ctx.width, vpx_input_ctx.height,
  807. &vpx_input_ctx.framerate, img->fmt, img->bit_depth);
  808. if (do_md5) {
  809. MD5Update(&md5_ctx, (md5byte *)buf, (unsigned int)len);
  810. } else {
  811. fputs(buf, outfile);
  812. }
  813. }
  814. // Y4M frame header
  815. len = y4m_write_frame_header(buf, sizeof(buf));
  816. if (do_md5) {
  817. MD5Update(&md5_ctx, (md5byte *)buf, (unsigned int)len);
  818. } else {
  819. fputs(buf, outfile);
  820. }
  821. } else {
  822. if (frame_out == 1) {
  823. // Check if --yv12 or --i420 options are consistent with the
  824. // bit-stream decoded
  825. if (opt_i420) {
  826. if (img->fmt != VPX_IMG_FMT_I420 &&
  827. img->fmt != VPX_IMG_FMT_I42016) {
  828. fprintf(stderr, "Cannot produce i420 output for bit-stream.\n");
  829. goto fail;
  830. }
  831. }
  832. if (opt_yv12) {
  833. if ((img->fmt != VPX_IMG_FMT_I420 &&
  834. img->fmt != VPX_IMG_FMT_YV12) ||
  835. img->bit_depth != 8) {
  836. fprintf(stderr, "Cannot produce yv12 output for bit-stream.\n");
  837. goto fail;
  838. }
  839. }
  840. }
  841. }
  842. if (do_md5) {
  843. update_image_md5(img, planes, &md5_ctx);
  844. } else {
  845. write_image_file(img, planes, outfile);
  846. }
  847. } else {
  848. generate_filename(outfile_pattern, outfile_name, PATH_MAX, img->d_w,
  849. img->d_h, frame_in);
  850. if (do_md5) {
  851. MD5Init(&md5_ctx);
  852. update_image_md5(img, planes, &md5_ctx);
  853. MD5Final(md5_digest, &md5_ctx);
  854. print_md5(md5_digest, outfile_name);
  855. } else {
  856. outfile = open_outfile(outfile_name);
  857. write_image_file(img, planes, outfile);
  858. fclose(outfile);
  859. }
  860. }
  861. }
  862. }
  863. if (summary || progress) {
  864. show_progress(frame_in, frame_out, dx_time);
  865. fprintf(stderr, "\n");
  866. }
  867. if (frames_corrupted)
  868. fprintf(stderr, "WARNING: %d frames corrupted.\n", frames_corrupted);
  869. fail:
  870. if (vpx_codec_destroy(&decoder)) {
  871. fprintf(stderr, "Failed to destroy decoder: %s\n",
  872. vpx_codec_error(&decoder));
  873. return EXIT_FAILURE;
  874. }
  875. if (!noblit && single_file) {
  876. if (do_md5) {
  877. MD5Final(md5_digest, &md5_ctx);
  878. print_md5(md5_digest, outfile_name);
  879. } else {
  880. fclose(outfile);
  881. }
  882. }
  883. #if CONFIG_WEBM_IO
  884. if (input.vpx_input_ctx->file_type == FILE_TYPE_WEBM)
  885. webm_free(input.webm_ctx);
  886. #endif
  887. if (input.vpx_input_ctx->file_type != FILE_TYPE_WEBM) free(buf);
  888. if (scaled_img) vpx_img_free(scaled_img);
  889. #if CONFIG_VP9_HIGHBITDEPTH
  890. if (img_shifted) vpx_img_free(img_shifted);
  891. #endif
  892. for (i = 0; i < ext_fb_list.num_external_frame_buffers; ++i) {
  893. free(ext_fb_list.ext_fb[i].data);
  894. }
  895. free(ext_fb_list.ext_fb);
  896. fclose(infile);
  897. free(argv);
  898. return frames_corrupted ? EXIT_FAILURE : EXIT_SUCCESS;
  899. }
  900. int main(int argc, const char **argv_) {
  901. unsigned int loops = 1, i;
  902. char **argv, **argi, **argj;
  903. struct arg arg;
  904. int error = 0;
  905. argv = argv_dup(argc - 1, argv_ + 1);
  906. for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
  907. memset(&arg, 0, sizeof(arg));
  908. arg.argv_step = 1;
  909. if (arg_match(&arg, &looparg, argi)) {
  910. loops = arg_parse_uint(&arg);
  911. break;
  912. }
  913. }
  914. free(argv);
  915. for (i = 0; !error && i < loops; i++) error = main_loop(argc, argv_);
  916. return error;
  917. }