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() {
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)
72  die("Invalid number of arguments.");
73 
74  reader = vpx_video_reader_open(argv[1]);
75  if (!reader)
76  die("Failed to open %s for reading.", argv[1]);
77 
78  if (!(outfile = fopen(argv[2], "wb")))
79  die("Failed to open %s for writing", argv[2]);
80 
81  info = vpx_video_reader_get_info(reader);
82 
83  decoder = get_vpx_decoder_by_fourcc(info->codec_fourcc);
84  if (!decoder)
85  die("Unknown input codec.");
86 
87  printf("Using %s\n", vpx_codec_iface_name(decoder->codec_interface()));
88 
89  res = vpx_codec_dec_init(&codec, decoder->codec_interface(), NULL,
91  if (res == VPX_CODEC_INCAPABLE)
92  die_codec(&codec, "Postproc not supported by this decoder.");
93 
94  if (res)
95  die_codec(&codec, "Failed to initialize decoder.");
96 
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,
102  &frame_size);
103 
104  ++frame_cnt;
105 
106  if (frame_cnt % 30 == 1) {
107  vp8_postproc_cfg_t pp = {0, 0, 0};
108 
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,
113  4, 0};
114  if (vpx_codec_control(&codec, VP8_SET_POSTPROC, &pp))
115  die_codec(&codec, "Failed to turn on postproc.");
116  };
117 
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");
121 
122  while ((img = vpx_codec_get_frame(&codec, &iter)) != NULL) {
123  vpx_img_write(img, outfile);
124  }
125  }
126 
127  printf("Processed %d frames.\n", frame_cnt);
128  if (vpx_codec_destroy(&codec))
129  die_codec(&codec, "Failed to destroy codec");
130 
131  printf("Play: ffplay -f rawvideo -pix_fmt yuv420p -s %dx%d %s\n",
132  info->frame_width, info->frame_height, argv[2]);
133 
134  vpx_video_reader_close(reader);
135 
136  fclose(outfile);
137  return EXIT_SUCCESS;
138 }
Image Descriptor.
Definition: vpx_image.h:82
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 VP8 or VP9 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:407
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