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.
11 // Postprocessing Decoder
12 // ======================
14 // This example adds postprocessing to the simple decoder loop.
16 // Initializing Postprocessing
17 // ---------------------------
18 // You must inform the codec that you might request postprocessing at
19 // initialization time. This is done by passing the VPX_CODEC_USE_POSTPROC
20 // flag to `vpx_codec_dec_init`. If the codec does not support
21 // postprocessing, this call will return VPX_CODEC_INCAPABLE. For
22 // demonstration purposes, we also fall back to default initialization if
23 // the codec does not provide support.
25 // Using Adaptive Postprocessing
26 // -----------------------------
27 // VP6 provides "adaptive postprocessing." It will automatically select the
28 // best postprocessing filter on a frame by frame basis based on the amount
29 // of time remaining before the user's specified deadline expires. The
30 // special value 0 indicates that the codec should take as long as
31 // necessary to provide the best quality frame. This example gives the
32 // codec 15ms (15000us) to return a frame. Remember that this is a soft
33 // deadline, and the codec may exceed it doing its regular processing. In
34 // these cases, no additional postprocessing will be done.
36 // Codec Specific Postprocessing Controls
37 // --------------------------------------
38 // Some codecs provide fine grained controls over their built-in
39 // postprocessors. VP8 is one example. The following sample code toggles
40 // postprocessing on and off every 15 frames.
46 #include "vpx/vp8dx.h"
47 #include "vpx/vpx_decoder.h"
49 #include "../tools_common.h"
50 #include "../video_reader.h"
51 #include "./vpx_config.h"
53 static const char *exec_name;
56 fprintf(stderr, "Usage: %s <infile> <outfile>\n", exec_name);
60 int main(int argc, char **argv) {
63 vpx_codec_ctx_t codec;
65 VpxVideoReader *reader = NULL;
66 const VpxInterface *decoder = NULL;
67 const VpxVideoInfo *info = NULL;
72 die("Invalid number of arguments.");
74 reader = vpx_video_reader_open(argv[1]);
76 die("Failed to open %s for reading.", argv[1]);
78 if (!(outfile = fopen(argv[2], "wb")))
79 die("Failed to open %s for writing", argv[2]);
81 info = vpx_video_reader_get_info(reader);
83 decoder = get_vpx_decoder_by_fourcc(info->codec_fourcc);
85 die("Unknown input codec.");
87 printf("Using %s\n", vpx_codec_iface_name(decoder->codec_interface()));
89 res = vpx_codec_dec_init(&codec, decoder->codec_interface(), NULL,
90 VPX_CODEC_USE_POSTPROC);
91 if (res == VPX_CODEC_INCAPABLE)
92 die_codec(&codec, "Postproc not supported by this decoder.");
95 die_codec(&codec, "Failed to initialize decoder.");
97 while (vpx_video_reader_read_frame(reader)) {
98 vpx_codec_iter_t iter = NULL;
99 vpx_image_t *img = NULL;
100 size_t frame_size = 0;
101 const unsigned char *frame = vpx_video_reader_get_frame(reader,
106 if (frame_cnt % 30 == 1) {
107 vp8_postproc_cfg_t pp = {0, 0, 0};
109 if (vpx_codec_control(&codec, VP8_SET_POSTPROC, &pp))
110 die_codec(&codec, "Failed to turn off postproc.");
111 } else if (frame_cnt % 30 == 16) {
112 vp8_postproc_cfg_t pp = {VP8_DEBLOCK | VP8_DEMACROBLOCK | VP8_MFQE,
114 if (vpx_codec_control(&codec, VP8_SET_POSTPROC, &pp))
115 die_codec(&codec, "Failed to turn on postproc.");
118 // Decode the frame with 15ms deadline
119 if (vpx_codec_decode(&codec, frame, (unsigned int)frame_size, NULL, 15000))
120 die_codec(&codec, "Failed to decode frame");
122 while ((img = vpx_codec_get_frame(&codec, &iter)) != NULL) {
123 vpx_img_write(img, outfile);
127 printf("Processed %d frames.\n", frame_cnt);
128 if (vpx_codec_destroy(&codec))
129 die_codec(&codec, "Failed to destroy codec");
131 printf("Play: ffplay -f rawvideo -pix_fmt yuv420p -s %dx%d %s\n",
132 info->frame_width, info->frame_height, argv[2]);
134 vpx_video_reader_close(reader);