2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
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.
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.
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. The `VPX_CODEC_DISABLE_COMPAT` macro can be defined to ensure
33 // strict compliance with the latest SDK by disabling some backwards
34 // compatibility features. Defining this macro is encouraged.
36 // Initializing The Codec
37 // ----------------------
38 // The decoder is initialized by the following code. This is an example for
39 // the VP8 decoder, but the code is analogous for all algorithms. Replace
40 // `vpx_codec_vp8_dx()` with a pointer to the interface exposed by the
41 // algorithm you want to use. The `cfg` argument is left as NULL in this
42 // example, because we want the algorithm to determine the stream
43 // configuration (width/height) and allocate memory automatically. This
44 // parameter is generally only used if you need to preallocate memory,
45 // particularly in External Memory Allocation mode.
49 // Once the frame has been read into memory, it is decoded using the
50 // `vpx_codec_decode` function. The call takes a pointer to the data
51 // (`frame`) and the length of the data (`frame_sz`). No application data
52 // is associated with the frame in this example, so the `user_priv`
53 // parameter is NULL. The `deadline` parameter is left at zero for this
54 // example. This parameter is generally only used when doing adaptive
57 // Codecs may produce a variable number of output frames for every call to
58 // `vpx_codec_decode`. These frames are retrieved by the
59 // `vpx_codec_get_frame` iterator function. The iterator variable `iter` is
60 // initialized to NULL each time `vpx_codec_decode` is called.
61 // `vpx_codec_get_frame` is called in a loop, returning a pointer to a
62 // decoded image or NULL to indicate the end of list.
64 // Processing The Decoded Data
65 // ---------------------------
66 // In this example, we simply write the encoded data to disk. It is
67 // important to honor the image's `stride` values.
71 // The `vpx_codec_destroy` call frees any memory allocated by the codec.
75 // This example does not special case any error return codes. If there was
76 // an error, a descriptive message is printed and the program exits. With
77 // few exeptions, vpx_codec functions return an enumerated error status,
78 // with the value `0` indicating success.
84 #define VPX_CODEC_DISABLE_COMPAT 1
85 #include "./vpx_config.h"
86 #include "vpx/vp8dx.h"
87 #include "vpx/vpx_decoder.h"
88 #define interface (vpx_codec_vp8_dx())
91 #define IVF_FILE_HDR_SZ (32)
92 #define IVF_FRAME_HDR_SZ (12)
94 static unsigned int mem_get_le32(const unsigned char *mem) {
95 return (mem[3] << 24)|(mem[2] << 16)|(mem[1] << 8)|(mem[0]);
98 static void die(const char *fmt, ...) {
103 if(fmt[strlen(fmt)-1] != '\n')
108 static void die_codec(vpx_codec_ctx_t *ctx, const char *s) {
109 const char *detail = vpx_codec_error_detail(ctx);
111 printf("%s: %s\n", s, vpx_codec_error(ctx));
113 printf(" %s\n",detail);
118 int main(int argc, char **argv) {
119 FILE *infile, *outfile;
120 vpx_codec_ctx_t codec;
121 int flags = 0, frame_cnt = 0;
122 unsigned char file_hdr[IVF_FILE_HDR_SZ];
123 unsigned char frame_hdr[IVF_FRAME_HDR_SZ];
124 unsigned char frame[256*1024];
130 die("Usage: %s <infile> <outfile>\n", argv[0]);
131 if(!(infile = fopen(argv[1], "rb")))
132 die("Failed to open %s for reading", argv[1]);
133 if(!(outfile = fopen(argv[2], "wb")))
134 die("Failed to open %s for writing", argv[2]);
136 /* Read file header */
137 if(!(fread(file_hdr, 1, IVF_FILE_HDR_SZ, infile) == IVF_FILE_HDR_SZ
138 && file_hdr[0]=='D' && file_hdr[1]=='K' && file_hdr[2]=='I'
139 && file_hdr[3]=='F'))
140 die("%s is not an IVF file.", argv[1]);
142 printf("Using %s\n",vpx_codec_iface_name(interface));
143 /* Initialize codec */
144 if(vpx_codec_dec_init(&codec, interface, NULL, flags))
145 die_codec(&codec, "Failed to initialize decoder");
147 /* Read each frame */
148 while(fread(frame_hdr, 1, IVF_FRAME_HDR_SZ, infile) == IVF_FRAME_HDR_SZ) {
149 int frame_sz = mem_get_le32(frame_hdr);
150 vpx_codec_iter_t iter = NULL;
155 if(frame_sz > sizeof(frame))
156 die("Frame %d data too big for example code buffer", frame_sz);
157 if(fread(frame, 1, frame_sz, infile) != frame_sz)
158 die("Frame %d failed to read complete frame", frame_cnt);
160 /* Decode the frame */
161 if(vpx_codec_decode(&codec, frame, frame_sz, NULL, 0))
162 die_codec(&codec, "Failed to decode frame");
164 /* Write decoded data to disk */
165 while((img = vpx_codec_get_frame(&codec, &iter))) {
166 unsigned int plane, y;
168 for(plane=0; plane < 3; plane++) {
169 unsigned char *buf =img->planes[plane];
171 for(y=0; y < (plane ? (img->d_h + 1) >> 1 : img->d_h); y++) {
172 (void) fwrite(buf, 1, (plane ? (img->d_w + 1) >> 1 : img->d_w),
174 buf += img->stride[plane];
179 printf("Processed %d frames.\n",frame_cnt);
180 if(vpx_codec_destroy(&codec))
181 die_codec(&codec, "Failed to destroy codec");