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