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 // Postprocessing Decoder
12 // ======================
13 //
14 // This example adds postprocessing to the simple decoder loop.
15 //
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.
24 //
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.
35 //
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.
41
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46 #include "vpx/vp8dx.h"
47 #include "vpx/vpx_decoder.h"
48
49 #include "../tools_common.h"
50 #include "../video_reader.h"
51 #include "./vpx_config.h"
52
53 static const char *exec_name;
54
usage_exit(void)55 void usage_exit(void) {
56 fprintf(stderr, "Usage: %s <infile> <outfile>\n", exec_name);
57 exit(EXIT_FAILURE);
58 }
59
main(int argc,char ** argv)60 int main(int argc, char **argv) {
61 int frame_cnt = 0;
62 FILE *outfile = NULL;
63 vpx_codec_ctx_t codec;
64 vpx_codec_err_t res;
65 VpxVideoReader *reader = NULL;
66 const VpxInterface *decoder = NULL;
67 const VpxVideoInfo *info = NULL;
68
69 exec_name = argv[0];
70
71 if (argc != 3) die("Invalid number of arguments.");
72
73 reader = vpx_video_reader_open(argv[1]);
74 if (!reader) die("Failed to open %s for reading.", argv[1]);
75
76 if (!(outfile = fopen(argv[2], "wb")))
77 die("Failed to open %s for writing", argv[2]);
78
79 info = vpx_video_reader_get_info(reader);
80
81 decoder = get_vpx_decoder_by_fourcc(info->codec_fourcc);
82 if (!decoder) die("Unknown input codec.");
83
84 printf("Using %s\n", vpx_codec_iface_name(decoder->codec_interface()));
85
86 res = vpx_codec_dec_init(&codec, decoder->codec_interface(), NULL,
87 VPX_CODEC_USE_POSTPROC);
88 if (res == VPX_CODEC_INCAPABLE)
89 die("Postproc not supported by this decoder.");
90
91 if (res) die("Failed to initialize decoder.");
92
93 while (vpx_video_reader_read_frame(reader)) {
94 vpx_codec_iter_t iter = NULL;
95 vpx_image_t *img = NULL;
96 size_t frame_size = 0;
97 const unsigned char *frame =
98 vpx_video_reader_get_frame(reader, &frame_size);
99
100 ++frame_cnt;
101
102 if (frame_cnt % 30 == 1) {
103 vp8_postproc_cfg_t pp = { 0, 0, 0 };
104
105 if (vpx_codec_control(&codec, VP8_SET_POSTPROC, &pp))
106 die_codec(&codec, "Failed to turn off postproc.");
107 } else if (frame_cnt % 30 == 16) {
108 vp8_postproc_cfg_t pp = { VP8_DEBLOCK | VP8_DEMACROBLOCK | VP8_MFQE, 4,
109 0 };
110 if (vpx_codec_control(&codec, VP8_SET_POSTPROC, &pp))
111 die_codec(&codec, "Failed to turn on postproc.");
112 }
113
114 // Decode the frame with 15ms deadline
115 if (vpx_codec_decode(&codec, frame, (unsigned int)frame_size, NULL, 15000))
116 die_codec(&codec, "Failed to decode frame");
117
118 while ((img = vpx_codec_get_frame(&codec, &iter)) != NULL) {
119 vpx_img_write(img, outfile);
120 }
121 }
122
123 printf("Processed %d frames.\n", frame_cnt);
124 if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
125
126 printf("Play: ffplay -f rawvideo -pix_fmt yuv420p -s %dx%d %s\n",
127 info->frame_width, info->frame_height, argv[2]);
128
129 vpx_video_reader_close(reader);
130
131 fclose(outfile);
132 return EXIT_SUCCESS;
133 }
134