decode_with_drops

00001 /*
00002  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
00003  *
00004  *  Use of this source code is governed by a BSD-style license
00005  *  that can be found in the LICENSE file in the root of the source
00006  *  tree. An additional intellectual property rights grant can be found
00007  *  in the file PATENTS.  All contributing project authors may
00008  *  be found in the AUTHORS file in the root of the source tree.
00009  */
00010 
00011 // Decode With Drops Example
00012 // =========================
00013 //
00014 // This is an example utility which drops a series of frames, as specified
00015 // on the command line. This is useful for observing the error recovery
00016 // features of the codec.
00017 //
00018 // Usage
00019 // -----
00020 // This example adds a single argument to the `simple_decoder` example,
00021 // which specifies the range or pattern of frames to drop. The parameter is
00022 // parsed as follows:
00023 //
00024 // Dropping A Range Of Frames
00025 // --------------------------
00026 // To drop a range of frames, specify the starting frame and the ending
00027 // frame to drop, separated by a dash. The following command will drop
00028 // frames 5 through 10 (base 1).
00029 //
00030 //  $ ./decode_with_drops in.ivf out.i420 5-10
00031 //
00032 //
00033 // Dropping A Pattern Of Frames
00034 // ----------------------------
00035 // To drop a pattern of frames, specify the number of frames to drop and
00036 // the number of frames after which to repeat the pattern, separated by
00037 // a forward-slash. The following command will drop 3 of 7 frames.
00038 // Specifically, it will decode 4 frames, then drop 3 frames, and then
00039 // repeat.
00040 //
00041 //  $ ./decode_with_drops in.ivf out.i420 3/7
00042 //
00043 //
00044 // Extra Variables
00045 // ---------------
00046 // This example maintains the pattern passed on the command line in the
00047 // `n`, `m`, and `is_range` variables:
00048 //
00049 //
00050 // Making The Drop Decision
00051 // ------------------------
00052 // The example decides whether to drop the frame based on the current
00053 // frame number, immediately before decoding the frame.
00054 
00055 #include <stdio.h>
00056 #include <stdlib.h>
00057 #include <string.h>
00058 
00059 #include "vpx/vp8dx.h"
00060 #include "vpx/vpx_decoder.h"
00061 
00062 #include "../tools_common.h"
00063 #include "../video_reader.h"
00064 #include "./vpx_config.h"
00065 
00066 static const char *exec_name;
00067 
00068 void usage_exit(void) {
00069   fprintf(stderr, "Usage: %s <infile> <outfile> <N-M|N/M>\n", exec_name);
00070   exit(EXIT_FAILURE);
00071 }
00072 
00073 int main(int argc, char **argv) {
00074   int frame_cnt = 0;
00075   FILE *outfile = NULL;
00076   vpx_codec_ctx_t codec;
00077   const VpxInterface *decoder = NULL;
00078   VpxVideoReader *reader = NULL;
00079   const VpxVideoInfo *info = NULL;
00080   int n = 0;
00081   int m = 0;
00082   int is_range = 0;
00083   char *nptr = NULL;
00084 
00085   exec_name = argv[0];
00086 
00087   if (argc != 4) die("Invalid number of arguments.");
00088 
00089   reader = vpx_video_reader_open(argv[1]);
00090   if (!reader) die("Failed to open %s for reading.", argv[1]);
00091 
00092   if (!(outfile = fopen(argv[2], "wb")))
00093     die("Failed to open %s for writing.", argv[2]);
00094 
00095   n = (int)strtol(argv[3], &nptr, 0);
00096   m = (int)strtol(nptr + 1, NULL, 0);
00097   is_range = (*nptr == '-');
00098   if (!n || !m || (*nptr != '-' && *nptr != '/'))
00099     die("Couldn't parse pattern %s.\n", argv[3]);
00100 
00101   info = vpx_video_reader_get_info(reader);
00102 
00103   decoder = get_vpx_decoder_by_fourcc(info->codec_fourcc);
00104   if (!decoder) die("Unknown input codec.");
00105 
00106   printf("Using %s\n", vpx_codec_iface_name(decoder->codec_interface()));
00107 
00108   if (vpx_codec_dec_init(&codec, decoder->codec_interface(), NULL, 0))
00109     die_codec(&codec, "Failed to initialize decoder.");
00110 
00111   while (vpx_video_reader_read_frame(reader)) {
00112     vpx_codec_iter_t iter = NULL;
00113     vpx_image_t *img = NULL;
00114     size_t frame_size = 0;
00115     int skip;
00116     const unsigned char *frame =
00117         vpx_video_reader_get_frame(reader, &frame_size);
00118     if (vpx_codec_decode(&codec, frame, (unsigned int)frame_size, NULL, 0))
00119       die_codec(&codec, "Failed to decode frame.");
00120 
00121     ++frame_cnt;
00122 
00123     skip = (is_range && frame_cnt >= n && frame_cnt <= m) ||
00124            (!is_range && m - (frame_cnt - 1) % m <= n);
00125 
00126     if (!skip) {
00127       putc('.', stdout);
00128 
00129       while ((img = vpx_codec_get_frame(&codec, &iter)) != NULL)
00130         vpx_img_write(img, outfile);
00131     } else {
00132       putc('X', stdout);
00133     }
00134 
00135     fflush(stdout);
00136   }
00137 
00138   printf("Processed %d frames.\n", frame_cnt);
00139   if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
00140 
00141   printf("Play: ffplay -f rawvideo -pix_fmt yuv420p -s %dx%d %s\n",
00142          info->frame_width, info->frame_height, argv[2]);
00143 
00144   vpx_video_reader_close(reader);
00145   fclose(outfile);
00146 
00147   return EXIT_SUCCESS;
00148 }

Generated on 16 Jun 2017 for WebM Codec SDK by  doxygen 1.6.1