postproc

00001 /*
00002  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
00003  *
00004  *  Use of this source code is governed by a BSD-style license
00005  *  that can be found in the LICENSE file in the root of the source
00006  *  tree. An additional intellectual property rights grant can be found
00007  *  in the file PATENTS.  All contributing project authors may
00008  *  be found in the AUTHORS file in the root of the source tree.
00009  */
00010 
00011 // Postprocessing Decoder
00012 // ======================
00013 //
00014 // This example adds postprocessing to the simple decoder loop.
00015 //
00016 // Initializing Postprocessing
00017 // ---------------------------
00018 // You must inform the codec that you might request postprocessing at
00019 // initialization time. This is done by passing the VPX_CODEC_USE_POSTPROC
00020 // flag to `vpx_codec_dec_init`. If the codec does not support
00021 // postprocessing, this call will return VPX_CODEC_INCAPABLE. For
00022 // demonstration purposes, we also fall back to default initialization if
00023 // the codec does not provide support.
00024 //
00025 // Using Adaptive Postprocessing
00026 // -----------------------------
00027 // VP6 provides "adaptive postprocessing." It will automatically select the
00028 // best postprocessing filter on a frame by frame basis based on the amount
00029 // of time remaining before the user's specified deadline expires. The
00030 // special value 0 indicates that the codec should take as long as
00031 // necessary to provide the best quality frame. This example gives the
00032 // codec 15ms (15000us) to return a frame. Remember that this is a soft
00033 // deadline, and the codec may exceed it doing its regular processing. In
00034 // these cases, no additional postprocessing will be done.
00035 //
00036 // Codec Specific Postprocessing Controls
00037 // --------------------------------------
00038 // Some codecs provide fine grained controls over their built-in
00039 // postprocessors. VP8 is one example. The following sample code toggles
00040 // postprocessing on and off every 15 frames.
00041 
00042 #include <stdio.h>
00043 #include <stdlib.h>
00044 #include <string.h>
00045 
00046 #include "vpx/vp8dx.h"
00047 #include "vpx/vpx_decoder.h"
00048 
00049 #include "../tools_common.h"
00050 #include "../video_reader.h"
00051 #include "./vpx_config.h"
00052 
00053 static const char *exec_name;
00054 
00055 void usage_exit(void) {
00056   fprintf(stderr, "Usage: %s <infile> <outfile>\n", exec_name);
00057   exit(EXIT_FAILURE);
00058 }
00059 
00060 int main(int argc, char **argv) {
00061   int frame_cnt = 0;
00062   FILE *outfile = NULL;
00063   vpx_codec_ctx_t codec;
00064   vpx_codec_err_t res;
00065   VpxVideoReader *reader = NULL;
00066   const VpxInterface *decoder = NULL;
00067   const VpxVideoInfo *info = NULL;
00068 
00069   exec_name = argv[0];
00070 
00071   if (argc != 3) die("Invalid number of arguments.");
00072 
00073   reader = vpx_video_reader_open(argv[1]);
00074   if (!reader) die("Failed to open %s for reading.", argv[1]);
00075 
00076   if (!(outfile = fopen(argv[2], "wb")))
00077     die("Failed to open %s for writing", argv[2]);
00078 
00079   info = vpx_video_reader_get_info(reader);
00080 
00081   decoder = get_vpx_decoder_by_fourcc(info->codec_fourcc);
00082   if (!decoder) die("Unknown input codec.");
00083 
00084   printf("Using %s\n", vpx_codec_iface_name(decoder->codec_interface()));
00085 
00086   res = vpx_codec_dec_init(&codec, decoder->codec_interface(), NULL,
00087                            VPX_CODEC_USE_POSTPROC);
00088   if (res == VPX_CODEC_INCAPABLE)
00089     die_codec(&codec, "Postproc not supported by this decoder.");
00090 
00091   if (res) die_codec(&codec, "Failed to initialize decoder.");
00092 
00093   while (vpx_video_reader_read_frame(reader)) {
00094     vpx_codec_iter_t iter = NULL;
00095     vpx_image_t *img = NULL;
00096     size_t frame_size = 0;
00097     const unsigned char *frame =
00098         vpx_video_reader_get_frame(reader, &frame_size);
00099 
00100     ++frame_cnt;
00101 
00102     if (frame_cnt % 30 == 1) {
00103       vp8_postproc_cfg_t pp = { 0, 0, 0 };
00104 
00105       if (vpx_codec_control(&codec, VP8_SET_POSTPROC, &pp))
00106         die_codec(&codec, "Failed to turn off postproc.");
00107     } else if (frame_cnt % 30 == 16) {
00108       vp8_postproc_cfg_t pp = { VP8_DEBLOCK | VP8_DEMACROBLOCK | VP8_MFQE, 4,
00109                                 0 };
00110       if (vpx_codec_control(&codec, VP8_SET_POSTPROC, &pp))
00111         die_codec(&codec, "Failed to turn on postproc.");
00112     };
00113 
00114     // Decode the frame with 15ms deadline
00115     if (vpx_codec_decode(&codec, frame, (unsigned int)frame_size, NULL, 15000))
00116       die_codec(&codec, "Failed to decode frame");
00117 
00118     while ((img = vpx_codec_get_frame(&codec, &iter)) != NULL) {
00119       vpx_img_write(img, outfile);
00120     }
00121   }
00122 
00123   printf("Processed %d frames.\n", frame_cnt);
00124   if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
00125 
00126   printf("Play: ffplay -f rawvideo -pix_fmt yuv420p -s %dx%d %s\n",
00127          info->frame_width, info->frame_height, argv[2]);
00128 
00129   vpx_video_reader_close(reader);
00130 
00131   fclose(outfile);
00132   return EXIT_SUCCESS;
00133 }

Generated on 16 Jun 2017 for WebM Codec SDK by  doxygen 1.6.1