AOMedia Codec SDK
decode_with_drops
1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 // Decode With Drops Example
13 // =========================
14 //
15 // This is an example utility which drops a series of frames, as specified
16 // on the command line. This is useful for observing the error recovery
17 // features of the codec.
18 //
19 // Usage
20 // -----
21 // This example adds a single argument to the `simple_decoder` example,
22 // which specifies the range or pattern of frames to drop. The parameter is
23 // parsed as follows:
24 //
25 // Dropping A Range Of Frames
26 // --------------------------
27 // To drop a range of frames, specify the starting frame and the ending
28 // frame to drop, separated by a dash. The following command will drop
29 // frames 5 through 10 (base 1).
30 //
31 // $ ./decode_with_drops in.ivf out.i420 5-10
32 //
33 //
34 // Dropping A Pattern Of Frames
35 // ----------------------------
36 // To drop a pattern of frames, specify the number of frames to drop and
37 // the number of frames after which to repeat the pattern, separated by
38 // a forward-slash. The following command will drop 3 of 7 frames.
39 // Specifically, it will decode 4 frames, then drop 3 frames, and then
40 // repeat.
41 //
42 // $ ./decode_with_drops in.ivf out.i420 3/7
43 //
44 //
45 // Extra Variables
46 // ---------------
47 // This example maintains the pattern passed on the command line in the
48 // `n`, `m`, and `is_range` variables:
49 //
50 //
51 // Making The Drop Decision
52 // ------------------------
53 // The example decides whether to drop the frame based on the current
54 // frame number, immediately before decoding the frame.
55 
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 
60 #include "aom/aom_decoder.h"
61 #include "aom/aomdx.h"
62 #include "common/tools_common.h"
63 #include "common/video_reader.h"
64 
65 static const char *exec_name;
66 
67 void usage_exit(void) {
68  fprintf(stderr, "Usage: %s <infile> <outfile> <N-M|N/M>\n", exec_name);
69  exit(EXIT_FAILURE);
70 }
71 
72 int main(int argc, char **argv) {
73  int frame_cnt = 0;
74  FILE *outfile = NULL;
75  aom_codec_ctx_t codec;
76  const AvxInterface *decoder = NULL;
77  AvxVideoReader *reader = NULL;
78  const AvxVideoInfo *info = NULL;
79  int n = 0;
80  int m = 0;
81  int is_range = 0;
82  char *nptr = NULL;
83 
84  exec_name = argv[0];
85 
86  if (argc != 4) die("Invalid number of arguments.");
87 
88  reader = aom_video_reader_open(argv[1]);
89  if (!reader) die("Failed to open %s for reading.", argv[1]);
90 
91  if (!(outfile = fopen(argv[2], "wb")))
92  die("Failed to open %s for writing.", argv[2]);
93 
94  n = (int)strtol(argv[3], &nptr, 0);
95  m = (int)strtol(nptr + 1, NULL, 0);
96  is_range = (*nptr == '-');
97  if (!n || !m || (*nptr != '-' && *nptr != '/'))
98  die("Couldn't parse pattern %s.\n", argv[3]);
99 
100  info = aom_video_reader_get_info(reader);
101 
102  decoder = get_aom_decoder_by_fourcc(info->codec_fourcc);
103  if (!decoder) die("Unknown input codec.");
104 
105  printf("Using %s\n", aom_codec_iface_name(decoder->codec_interface()));
106 
107  if (aom_codec_dec_init(&codec, decoder->codec_interface(), NULL, 0))
108  die_codec(&codec, "Failed to initialize decoder.");
109 
110  while (aom_video_reader_read_frame(reader)) {
111  aom_codec_iter_t iter = NULL;
112  aom_image_t *img = NULL;
113  size_t frame_size = 0;
114  int skip;
115  const unsigned char *frame =
116  aom_video_reader_get_frame(reader, &frame_size);
117  ++frame_cnt;
118 
119  skip = (is_range && frame_cnt >= n && frame_cnt <= m) ||
120  (!is_range && m - (frame_cnt - 1) % m <= n);
121 
122  if (!skip) {
123  putc('.', stdout);
124  if (aom_codec_decode(&codec, frame, frame_size, NULL))
125  die_codec(&codec, "Failed to decode frame.");
126 
127  while ((img = aom_codec_get_frame(&codec, &iter)) != NULL)
128  aom_img_write(img, outfile);
129  } else {
130  putc('X', stdout);
131  }
132 
133  fflush(stdout);
134  }
135 
136  printf("Processed %d frames.\n", frame_cnt);
137  if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
138 
139  printf("Play: ffplay -f rawvideo -pix_fmt yuv420p -s %dx%d %s\n",
140  info->frame_width, info->frame_height, argv[2]);
141 
142  aom_video_reader_close(reader);
143  fclose(outfile);
144 
145  return EXIT_SUCCESS;
146 }
aom_codec_decode
aom_codec_err_t aom_codec_decode(aom_codec_ctx_t *ctx, const uint8_t *data, size_t data_sz, void *user_priv)
Decode data.
aom_codec_ctx
Codec context structure.
Definition: aom_codec.h:204
aom_codec_iter_t
const typedef void * aom_codec_iter_t
Iterator.
Definition: aom_codec.h:194
aom_codec_iface_name
const char * aom_codec_iface_name(aom_codec_iface_t *iface)
Return the name for a given interface.
aom_codec_destroy
aom_codec_err_t aom_codec_destroy(aom_codec_ctx_t *ctx)
Destroy a codec instance.
aom_decoder.h
Describes the decoder algorithm interface to applications.
aom_codec_dec_init
#define aom_codec_dec_init(ctx, iface, cfg, flags)
Convenience macro for aom_codec_dec_init_ver()
Definition: aom_decoder.h:135
aom_codec_get_frame
aom_image_t * aom_codec_get_frame(aom_codec_ctx_t *ctx, aom_codec_iter_t *iter)
Decoded frames iterator.
aomdx.h
Provides definitions for using AOM or AV1 within the aom Decoder interface.
aom_image
Image Descriptor.
Definition: aom_image.h:141