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
12// Simple Decoder
13// ==============
14//
15// This is an example of a simple decoder loop. It takes an input file
16// containing the compressed data (in IVF format), passes it through the
17// decoder, and writes the decompressed frames to disk. Other decoder
18// examples build upon this one.
19//
20// The details of the IVF format have been elided from this example for
21// simplicity of presentation, as IVF files will not generally be used by
22// your application. In general, an IVF file consists of a file header,
23// followed by a variable number of frames. Each frame consists of a frame
24// header followed by a variable length payload. The length of the payload
25// is specified in the first four bytes of the frame header. The payload is
26// the raw compressed data.
27//
28// Standard Includes
29// -----------------
30// For decoders, you only have to include `vpx_decoder.h` and then any
31// header files for the specific codecs you use. In this case, we're using
32// vp8.
33//
34// Initializing The Codec
35// ----------------------
36// The decoder is initialized by the following code. This is an example for
37// the VP8 decoder, but the code is analogous for all algorithms. Replace
38// `vpx_codec_vp8_dx()` with a pointer to the interface exposed by the
39// algorithm you want to use. The `cfg` argument is left as NULL in this
40// example, because we want the algorithm to determine the stream
41// configuration (width/height) and allocate memory automatically. This
42// parameter is generally only used if you need to preallocate memory,
43// particularly in External Memory Allocation mode.
44//
45// Decoding A Frame
46// ----------------
47// Once the frame has been read into memory, it is decoded using the
48// `vpx_codec_decode` function. The call takes a pointer to the data
49// (`frame`) and the length of the data (`frame_sz`). No application data
50// is associated with the frame in this example, so the `user_priv`
51// parameter is NULL. The `deadline` parameter is left at zero for this
52// example. This parameter is generally only used when doing adaptive
53// postprocessing.
54//
55// Codecs may produce a variable number of output frames for every call to
56// `vpx_codec_decode`. These frames are retrieved by the
57// `vpx_codec_get_frame` iterator function. The iterator variable `iter` is
58// initialized to NULL each time `vpx_codec_decode` is called.
59// `vpx_codec_get_frame` is called in a loop, returning a pointer to a
60// decoded image or NULL to indicate the end of list.
61//
62// Processing The Decoded Data
63// ---------------------------
64// In this example, we simply write the encoded data to disk. It is
65// important to honor the image's `stride` values.
66//
67// Cleanup
68// -------
69// The `vpx_codec_destroy` call frees any memory allocated by the codec.
70//
71// Error Handling
72// --------------
73// This example does not special case any error return codes. If there was
74// an error, a descriptive message is printed and the program exits. With
75// few exeptions, vpx_codec functions return an enumerated error status,
76// with the value `0` indicating success.
77
78#include <stdio.h>
79#include <stdlib.h>
80#include <string.h>
81
82#include "vpx/vp8dx.h"
83#include "vpx/vpx_decoder.h"
84
85#include "./tools_common.h"
86#include "./video_reader.h"
87#include "./vpx_config.h"
88
89static const char *exec_name;
90
91void usage_exit() {
92  fprintf(stderr, "Usage: %s <infile> <outfile>\n", exec_name);
93  exit(EXIT_FAILURE);
94}
95
96int main(int argc, char **argv) {
97  int frame_cnt = 0;
98  FILE *outfile = NULL;
99  vpx_codec_ctx_t codec;
100  VpxVideoReader *reader = NULL;
101  const VpxInterface *decoder = NULL;
102  const VpxVideoInfo *info = NULL;
103
104  exec_name = argv[0];
105
106  if (argc != 3)
107    die("Invalid number of arguments.");
108
109  reader = vpx_video_reader_open(argv[1]);
110  if (!reader)
111    die("Failed to open %s for reading.", argv[1]);
112
113  if (!(outfile = fopen(argv[2], "wb")))
114    die("Failed to open %s for writing.", argv[2]);
115
116  info = vpx_video_reader_get_info(reader);
117
118  decoder = get_vpx_decoder_by_fourcc(info->codec_fourcc);
119  if (!decoder)
120    die("Unknown input codec.");
121
122  printf("Using %s\n", vpx_codec_iface_name(decoder->codec_interface()));
123
124  if (vpx_codec_dec_init(&codec, decoder->codec_interface(), NULL, 0))
125    die_codec(&codec, "Failed to initialize decoder.");
126
127  while (vpx_video_reader_read_frame(reader)) {
128    vpx_codec_iter_t iter = NULL;
129    vpx_image_t *img = NULL;
130    size_t frame_size = 0;
131    const unsigned char *frame = vpx_video_reader_get_frame(reader,
132                                                            &frame_size);
133    if (vpx_codec_decode(&codec, frame, (unsigned int)frame_size, NULL, 0))
134      die_codec(&codec, "Failed to decode frame.");
135
136    while ((img = vpx_codec_get_frame(&codec, &iter)) != NULL) {
137      vpx_img_write(img, outfile);
138      ++frame_cnt;
139    }
140  }
141
142  printf("Processed %d frames.\n", frame_cnt);
143  if (vpx_codec_destroy(&codec))
144    die_codec(&codec, "Failed to destroy codec");
145
146  printf("Play: ffplay -f rawvideo -pix_fmt yuv420p -s %dx%d %s\n",
147         info->frame_width, info->frame_height, argv[2]);
148
149  vpx_video_reader_close(reader);
150
151  fclose(outfile);
152
153  return EXIT_SUCCESS;
154}
155