WebM Codec SDK

postproc
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
55 void usage_exit(void) {
56 fprintf(stderr, "Usage: %s <infile> <outfile>\n", exec_name);
57 exit(EXIT_FAILURE);
58}
59
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}
vpx_codec_ctx_t
struct vpx_codec_ctx vpx_codec_ctx_t
Codec context structure.
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:190
vpx_codec_iface_name
const char * vpx_codec_iface_name(vpx_codec_iface_t *iface)
Return the name for a given interface.
vpx_codec_control
#define vpx_codec_control(ctx, id, data)
vpx_codec_control wrapper macro
Definition vpx_codec.h:408
vpx_codec_err_t
vpx_codec_err_t
Algorithm return codes.
Definition vpx_codec.h:93
VPX_CODEC_INCAPABLE
@ VPX_CODEC_INCAPABLE
Algorithm does not have required capability.
Definition vpx_codec.h:107
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_USE_POSTPROC
#define VPX_CODEC_USE_POSTPROC
Initialization-time Feature Enabling.
Definition vpx_decoder.h:73
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:143
vpx_codec_get_frame
vpx_image_t * vpx_codec_get_frame(vpx_codec_ctx_t *ctx, vpx_codec_iter_t *iter)
Decoded frames iterator.
vp8_postproc_cfg_t
struct vp8_postproc_cfg vp8_postproc_cfg_t
post process flags
VP8_SET_POSTPROC
@ VP8_SET_POSTPROC
Definition vp8.h:49
vp8dx.h
Provides definitions for using VP8 or VP9 within the vpx Decoder interface.
vpx_decoder.h
Describes the decoder algorithm interface to applications.
vpx_image_t
struct vpx_image vpx_image_t
Image Descriptor.