2
0

decode_with_drops.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. // Decode With Drops Example
  11. // =========================
  12. //
  13. // This is an example utility which drops a series of frames, as specified
  14. // on the command line. This is useful for observing the error recovery
  15. // features of the codec.
  16. //
  17. // Usage
  18. // -----
  19. // This example adds a single argument to the `simple_decoder` example,
  20. // which specifies the range or pattern of frames to drop. The parameter is
  21. // parsed as follows:
  22. //
  23. // Dropping A Range Of Frames
  24. // --------------------------
  25. // To drop a range of frames, specify the starting frame and the ending
  26. // frame to drop, separated by a dash. The following command will drop
  27. // frames 5 through 10 (base 1).
  28. //
  29. // $ ./decode_with_drops in.ivf out.i420 5-10
  30. //
  31. //
  32. // Dropping A Pattern Of Frames
  33. // ----------------------------
  34. // To drop a pattern of frames, specify the number of frames to drop and
  35. // the number of frames after which to repeat the pattern, separated by
  36. // a forward-slash. The following command will drop 3 of 7 frames.
  37. // Specifically, it will decode 4 frames, then drop 3 frames, and then
  38. // repeat.
  39. //
  40. // $ ./decode_with_drops in.ivf out.i420 3/7
  41. //
  42. //
  43. // Extra Variables
  44. // ---------------
  45. // This example maintains the pattern passed on the command line in the
  46. // `n`, `m`, and `is_range` variables:
  47. //
  48. //
  49. // Making The Drop Decision
  50. // ------------------------
  51. // The example decides whether to drop the frame based on the current
  52. // frame number, immediately before decoding the frame.
  53. #include <stdio.h>
  54. #include <stdlib.h>
  55. #include <string.h>
  56. #include "vpx/vp8dx.h"
  57. #include "vpx/vpx_decoder.h"
  58. #include "../tools_common.h"
  59. #include "../video_reader.h"
  60. #include "./vpx_config.h"
  61. static const char *exec_name;
  62. void usage_exit(void) {
  63. fprintf(stderr, "Usage: %s <infile> <outfile> <N-M|N/M>\n", exec_name);
  64. exit(EXIT_FAILURE);
  65. }
  66. int main(int argc, char **argv) {
  67. int frame_cnt = 0;
  68. FILE *outfile = NULL;
  69. vpx_codec_ctx_t codec;
  70. const VpxInterface *decoder = NULL;
  71. VpxVideoReader *reader = NULL;
  72. const VpxVideoInfo *info = NULL;
  73. int n = 0;
  74. int m = 0;
  75. int is_range = 0;
  76. char *nptr = NULL;
  77. exec_name = argv[0];
  78. if (argc != 4) die("Invalid number of arguments.");
  79. reader = vpx_video_reader_open(argv[1]);
  80. if (!reader) die("Failed to open %s for reading.", argv[1]);
  81. if (!(outfile = fopen(argv[2], "wb")))
  82. die("Failed to open %s for writing.", argv[2]);
  83. n = (int)strtol(argv[3], &nptr, 0);
  84. m = (int)strtol(nptr + 1, NULL, 0);
  85. is_range = (*nptr == '-');
  86. if (!n || !m || (*nptr != '-' && *nptr != '/'))
  87. die("Couldn't parse pattern %s.\n", argv[3]);
  88. info = vpx_video_reader_get_info(reader);
  89. decoder = get_vpx_decoder_by_fourcc(info->codec_fourcc);
  90. if (!decoder) die("Unknown input codec.");
  91. printf("Using %s\n", vpx_codec_iface_name(decoder->codec_interface()));
  92. if (vpx_codec_dec_init(&codec, decoder->codec_interface(), NULL, 0))
  93. die_codec(&codec, "Failed to initialize decoder.");
  94. while (vpx_video_reader_read_frame(reader)) {
  95. vpx_codec_iter_t iter = NULL;
  96. vpx_image_t *img = NULL;
  97. size_t frame_size = 0;
  98. int skip;
  99. const unsigned char *frame =
  100. vpx_video_reader_get_frame(reader, &frame_size);
  101. if (vpx_codec_decode(&codec, frame, (unsigned int)frame_size, NULL, 0))
  102. die_codec(&codec, "Failed to decode frame.");
  103. ++frame_cnt;
  104. skip = (is_range && frame_cnt >= n && frame_cnt <= m) ||
  105. (!is_range && m - (frame_cnt - 1) % m <= n);
  106. if (!skip) {
  107. putc('.', stdout);
  108. while ((img = vpx_codec_get_frame(&codec, &iter)) != NULL)
  109. vpx_img_write(img, outfile);
  110. } else {
  111. putc('X', stdout);
  112. }
  113. fflush(stdout);
  114. }
  115. printf("Processed %d frames.\n", frame_cnt);
  116. if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
  117. printf("Play: ffplay -f rawvideo -pix_fmt yuv420p -s %dx%d %s\n",
  118. info->frame_width, info->frame_height, argv[2]);
  119. vpx_video_reader_close(reader);
  120. fclose(outfile);
  121. return EXIT_SUCCESS;
  122. }