WebM Codec SDK: simple_decoder

WebM Codec SDK

simple_decoder
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 // Simple Decoder
12 // ==============
13 //
14 // This is an example of a simple decoder loop. It takes an input file
15 // containing the compressed data (in IVF format), passes it through the
16 // decoder, and writes the decompressed frames to disk. Other decoder
17 // examples build upon this one.
18 //
19 // The details of the IVF format have been elided from this example for
20 // simplicity of presentation, as IVF files will not generally be used by
21 // your application. In general, an IVF file consists of a file header,
22 // followed by a variable number of frames. Each frame consists of a frame
23 // header followed by a variable length payload. The length of the payload
24 // is specified in the first four bytes of the frame header. The payload is
25 // the raw compressed data.
26 //
27 // Standard Includes
28 // -----------------
29 // For decoders, you only have to include `vpx_decoder.h` and then any
30 // header files for the specific codecs you use. In this case, we're using
31 // vp8.
32 //
33 // Initializing The Codec
34 // ----------------------
35 // The libvpx decoder is initialized by the call to vpx_codec_dec_init().
36 // Determining the codec interface to use is handled by VpxVideoReader and the
37 // functions prefixed with vpx_video_reader_. Discussion of those functions is
38 // beyond the scope of this example, but the main gist is to open the input file
39 // and parse just enough of it to determine if it's a VPx file and which VPx
40 // codec is contained within the file.
41 // Note the NULL pointer passed to vpx_codec_dec_init(). We do that in this
42 // example because we want the algorithm to determine the stream configuration
43 // (width/height) and allocate memory automatically.
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_size`). 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 post
53 // processing.
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 exceptions, 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/vpx_decoder.h"
83
84 #include "../tools_common.h"
85 #include "../video_reader.h"
86 #include "./vpx_config.h"
87
88 static const char *exec_name;
89
90 void usage_exit(void) {
91 fprintf(stderr, "Usage: %s <infile> <outfile>\n", exec_name);
92 exit(EXIT_FAILURE);
93 }
94
95 int main(int argc, char **argv) {
96 int frame_cnt = 0;
97 FILE *outfile = NULL;
98 vpx_codec_ctx_t codec;
99 VpxVideoReader *reader = NULL;
100 const VpxInterface *decoder = NULL;
101 const VpxVideoInfo *info = NULL;
102
103 exec_name = argv[0];
104
105 if (argc != 3) die("Invalid number of arguments.");
106
107 reader = vpx_video_reader_open(argv[1]);
108 if (!reader) die("Failed to open %s for reading.", argv[1]);
109
110 if (!(outfile = fopen(argv[2], "wb")))
111 die("Failed to open %s for writing.", argv[2]);
112
113 info = vpx_video_reader_get_info(reader);
114
115 decoder = get_vpx_decoder_by_fourcc(info->codec_fourcc);
116 if (!decoder) die("Unknown input codec.");
117
118 printf("Using %s\n", vpx_codec_iface_name(decoder->codec_interface()));
119
120 if (vpx_codec_dec_init(&codec, decoder->codec_interface(), NULL, 0))
121 die_codec(&codec, "Failed to initialize decoder.");
122
123 while (vpx_video_reader_read_frame(reader)) {
124 vpx_codec_iter_t iter = NULL;
125 vpx_image_t *img = NULL;
126 size_t frame_size = 0;
127 const unsigned char *frame =
128 vpx_video_reader_get_frame(reader, &frame_size);
129 if (vpx_codec_decode(&codec, frame, (unsigned int)frame_size, NULL, 0))
130 die_codec(&codec, "Failed to decode frame.");
131
132 while ((img = vpx_codec_get_frame(&codec, &iter)) != NULL) {
133 vpx_img_write(img, outfile);
134 ++frame_cnt;
135 }
136 }
137
138 printf("Processed %d frames.\n", frame_cnt);
139 if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
140
141 printf("Play: ffplay -f rawvideo -pix_fmt yuv420p -s %dx%d %s\n",
142 info->frame_width, info->frame_height, argv[2]);
143
144 vpx_video_reader_close(reader);
145
146 fclose(outfile);
147
148 return EXIT_SUCCESS;
149 }
vpx_image
Image Descriptor.
Definition: vpx_image.h:88
vpx_decoder.h
Describes the decoder algorithm interface to applications.
vpx_codec_iface_name
const char * vpx_codec_iface_name(vpx_codec_iface_t *iface)
Return the name for a given interface.
vpx_codec_dec_init
#define vpx_codec_dec_init(ctx, iface, cfg, flags)
Convenience macro for vpx_codec_dec_init_ver()
Definition: vpx_decoder.h:144
vpx_codec_decode
vpx_codec_err_t vpx_codec_decode(vpx_codec_ctx_t *ctx, const uint8_t *data, unsigned int data_sz, void *user_priv, long deadline)
Decode data.
vpx_codec_destroy
vpx_codec_err_t vpx_codec_destroy(vpx_codec_ctx_t *ctx)
Destroy a codec instance.
vpx_codec_iter_t
const void * vpx_codec_iter_t
Iterator.
Definition: vpx_codec.h:182
vpx_codec_get_frame
vpx_image_t * vpx_codec_get_frame(vpx_codec_ctx_t *ctx, vpx_codec_iter_t *iter)
Decoded frames iterator.
vpx_codec_ctx
Codec context structure.
Definition: vpx_codec.h:192