| OLD | NEW |
| (Empty) | |
| 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 <stdarg.h> |
| 43 #include <stdio.h> |
| 44 #include <stdlib.h> |
| 45 #include <string.h> |
| 46 #define VPX_CODEC_DISABLE_COMPAT 1 |
| 47 #include "./vpx_config.h" |
| 48 #include "vpx/vp8dx.h" |
| 49 #include "vpx/vpx_decoder.h" |
| 50 #define interface (vpx_codec_vp8_dx()) |
| 51 |
| 52 |
| 53 #define IVF_FILE_HDR_SZ (32) |
| 54 #define IVF_FRAME_HDR_SZ (12) |
| 55 |
| 56 static unsigned int mem_get_le32(const unsigned char *mem) { |
| 57 return (mem[3] << 24)|(mem[2] << 16)|(mem[1] << 8)|(mem[0]); |
| 58 } |
| 59 |
| 60 static void die(const char *fmt, ...) { |
| 61 va_list ap; |
| 62 |
| 63 va_start(ap, fmt); |
| 64 vprintf(fmt, ap); |
| 65 if(fmt[strlen(fmt)-1] != '\n') |
| 66 printf("\n"); |
| 67 exit(EXIT_FAILURE); |
| 68 } |
| 69 |
| 70 static void die_codec(vpx_codec_ctx_t *ctx, const char *s) { |
| 71 const char *detail = vpx_codec_error_detail(ctx); |
| 72 |
| 73 printf("%s: %s\n", s, vpx_codec_error(ctx)); |
| 74 if(detail) |
| 75 printf(" %s\n",detail); |
| 76 exit(EXIT_FAILURE); |
| 77 } |
| 78 |
| 79 |
| 80 int main(int argc, char **argv) { |
| 81 FILE *infile, *outfile; |
| 82 vpx_codec_ctx_t codec; |
| 83 int flags = 0, frame_cnt = 0; |
| 84 unsigned char file_hdr[IVF_FILE_HDR_SZ]; |
| 85 unsigned char frame_hdr[IVF_FRAME_HDR_SZ]; |
| 86 unsigned char frame[256*1024]; |
| 87 vpx_codec_err_t res; |
| 88 |
| 89 (void)res; |
| 90 /* Open files */ |
| 91 if(argc!=3) |
| 92 die("Usage: %s <infile> <outfile>\n", argv[0]); |
| 93 if(!(infile = fopen(argv[1], "rb"))) |
| 94 die("Failed to open %s for reading", argv[1]); |
| 95 if(!(outfile = fopen(argv[2], "wb"))) |
| 96 die("Failed to open %s for writing", argv[2]); |
| 97 |
| 98 /* Read file header */ |
| 99 if(!(fread(file_hdr, 1, IVF_FILE_HDR_SZ, infile) == IVF_FILE_HDR_SZ |
| 100 && file_hdr[0]=='D' && file_hdr[1]=='K' && file_hdr[2]=='I' |
| 101 && file_hdr[3]=='F')) |
| 102 die("%s is not an IVF file.", argv[1]); |
| 103 |
| 104 printf("Using %s\n",vpx_codec_iface_name(interface)); |
| 105 /* Initialize codec */ |
| 106 res = vpx_codec_dec_init(&codec, interface, NULL, |
| 107 VPX_CODEC_USE_POSTPROC); |
| 108 if(res == VPX_CODEC_INCAPABLE) { |
| 109 printf("NOTICE: Postproc not supported by %s\n", |
| 110 vpx_codec_iface_name(interface)); |
| 111 res = vpx_codec_dec_init(&codec, interface, NULL, flags); |
| 112 } |
| 113 if(res) |
| 114 die_codec(&codec, "Failed to initialize decoder"); |
| 115 |
| 116 /* Read each frame */ |
| 117 while(fread(frame_hdr, 1, IVF_FRAME_HDR_SZ, infile) == IVF_FRAME_HDR_SZ) { |
| 118 int frame_sz = mem_get_le32(frame_hdr); |
| 119 vpx_codec_iter_t iter = NULL; |
| 120 vpx_image_t *img; |
| 121 |
| 122 |
| 123 frame_cnt++; |
| 124 if(frame_sz > sizeof(frame)) |
| 125 die("Frame %d data too big for example code buffer", frame_sz); |
| 126 if(fread(frame, 1, frame_sz, infile) != frame_sz) |
| 127 die("Frame %d failed to read complete frame", frame_cnt); |
| 128 |
| 129 #if CONFIG_VP9_DECODER |
| 130 if(frame_cnt%30 == 1) { |
| 131 vp8_postproc_cfg_t pp = {0, 0, 0}; |
| 132 |
| 133 if(vpx_codec_control(&codec, VP8_SET_POSTPROC, &pp)) |
| 134 die_codec(&codec, "Failed to turn off postproc"); |
| 135 } else if(frame_cnt%30 == 16) { |
| 136 vp8_postproc_cfg_t pp = {VP8_DEBLOCK | VP8_DEMACROBLOCK | VP8_MFQE,
4, 0}; |
| 137 |
| 138 if(vpx_codec_control(&codec, VP8_SET_POSTPROC, &pp)) |
| 139 die_codec(&codec, "Failed to turn on postproc"); |
| 140 }; |
| 141 #endif |
| 142 /* Decode the frame with 15ms deadline */ |
| 143 if(vpx_codec_decode(&codec, frame, frame_sz, NULL, 15000)) |
| 144 die_codec(&codec, "Failed to decode frame"); |
| 145 |
| 146 /* Write decoded data to disk */ |
| 147 while((img = vpx_codec_get_frame(&codec, &iter))) { |
| 148 unsigned int plane, y; |
| 149 |
| 150 for(plane=0; plane < 3; plane++) { |
| 151 unsigned char *buf =img->planes[plane]; |
| 152 |
| 153 for(y=0; y < (plane ? (img->d_h + 1) >> 1 : img->d_h); y++) { |
| 154 (void) fwrite(buf, 1, (plane ? (img->d_w + 1) >> 1 : img->d_
w), |
| 155 outfile); |
| 156 buf += img->stride[plane]; |
| 157 } |
| 158 } |
| 159 } |
| 160 } |
| 161 printf("Processed %d frames.\n",frame_cnt); |
| 162 if(vpx_codec_destroy(&codec)) |
| 163 die_codec(&codec, "Failed to destroy codec"); |
| 164 |
| 165 fclose(outfile); |
| 166 fclose(infile); |
| 167 return EXIT_SUCCESS; |
| 168 } |
| OLD | NEW |