vpxdec.c 37 KB

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