WebM VP8 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 #define VPX_CODEC_DISABLE_COMPAT 1
47 
48 #include "vpx/vp8dx.h"
49 #include "vpx/vpx_decoder.h"
50 
51 #include "./tools_common.h"
52 #include "./video_reader.h"
53 #include "./vpx_config.h"
54 
55 static const char *exec_name;
56 
57 void usage_exit() {
58  fprintf(stderr, "Usage: %s <infile> <outfile>\n", exec_name);
59  exit(EXIT_FAILURE);
60 }
61 
62 int main(int argc, char **argv) {
63  int frame_cnt = 0;
64  FILE *outfile = NULL;
65  vpx_codec_ctx_t codec;
66  vpx_codec_err_t res;
67  VpxVideoReader *reader = NULL;
68  const VpxInterface *decoder = NULL;
69  const VpxVideoInfo *info = NULL;
70 
71  exec_name = argv[0];
72 
73  if (argc != 3)
74  die("Invalid number of arguments.");
75 
76  reader = vpx_video_reader_open(argv[1]);
77  if (!reader)
78  die("Failed to open %s for reading.", argv[1]);
79 
80  if (!(outfile = fopen(argv[2], "wb")))
81  die("Failed to open %s for writing", argv[2]);
82 
83  info = vpx_video_reader_get_info(reader);
84 
85  decoder = get_vpx_decoder_by_fourcc(info->codec_fourcc);
86  if (!decoder)
87  die("Unknown input codec.");
88 
89  printf("Using %s\n", vpx_codec_iface_name(decoder->codec_interface()));
90 
91  res = vpx_codec_dec_init(&codec, decoder->codec_interface(), NULL,
93  if (res == VPX_CODEC_INCAPABLE)
94  die_codec(&codec, "Postproc not supported by this decoder.");
95 
96  if (res)
97  die_codec(&codec, "Failed to initialize decoder.");
98 
99  while (vpx_video_reader_read_frame(reader)) {
100  vpx_codec_iter_t iter = NULL;
101  vpx_image_t *img = NULL;
102  size_t frame_size = 0;
103  const unsigned char *frame = vpx_video_reader_get_frame(reader,
104  &frame_size);
105 
106  ++frame_cnt;
107 
108  if (frame_cnt % 30 == 1) {
109  vp8_postproc_cfg_t pp = {0, 0, 0};
110 
111  if (vpx_codec_control(&codec, VP8_SET_POSTPROC, &pp))
112  die_codec(&codec, "Failed to turn off postproc.");
113  } else if (frame_cnt % 30 == 16) {
114  vp8_postproc_cfg_t pp = {VP8_DEBLOCK | VP8_DEMACROBLOCK | VP8_MFQE,
115  4, 0};
116  if (vpx_codec_control(&codec, VP8_SET_POSTPROC, &pp))
117  die_codec(&codec, "Failed to turn on postproc.");
118  };
119 
120  // Decode the frame with 15ms deadline
121  if (vpx_codec_decode(&codec, frame, (unsigned int)frame_size, NULL, 15000))
122  die_codec(&codec, "Failed to decode frame");
123 
124  while ((img = vpx_codec_get_frame(&codec, &iter)) != NULL) {
125  vpx_img_write(img, outfile);
126  }
127  }
128 
129  printf("Processed %d frames.\n", frame_cnt);
130  if (vpx_codec_destroy(&codec))
131  die_codec(&codec, "Failed to destroy codec");
132 
133  printf("Play: ffplay -f rawvideo -pix_fmt yuv420p -s %dx%d %s\n",
134  info->frame_width, info->frame_height, argv[2]);
135 
136  vpx_video_reader_close(reader);
137 
138  fclose(outfile);
139  return EXIT_SUCCESS;
140 }
Image Descriptor.
Definition: vpx_image.h:102
Describes the decoder algorithm interface to applications.
const char * vpx_codec_iface_name(vpx_codec_iface_t *iface)
Return the name for a given interface.
Provides definitions for using the VP8 algorithm within the vpx Decoder interface.
#define vpx_codec_dec_init(ctx, iface, cfg, flags)
Convenience macro for vpx_codec_dec_init_ver()
Definition: vpx_decoder.h:154
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.
Definition: vp8.h:47
#define VPX_CODEC_USE_POSTPROC
Definition: vpx_decoder.h:77
vpx_codec_err_t
Algorithm return codes.
Definition: vpx_codec.h:89
#define vpx_codec_control(ctx, id, data)
vpx_codec_control wrapper macro
Definition: vpx_codec.h:405
vpx_codec_err_t vpx_codec_destroy(vpx_codec_ctx_t *ctx)
Destroy a codec instance.
post process flags
Definition: vp8.h:85
Algorithm does not have required capability.
Definition: vpx_codec.h:103
const void * vpx_codec_iter_t
Iterator.
Definition: vpx_codec.h:188
vpx_image_t * vpx_codec_get_frame(vpx_codec_ctx_t *ctx, vpx_codec_iter_t *iter)
Decoded frames iterator.
Codec context structure.
Definition: vpx_codec.h:199