| 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 // Error Resiliency Features | |
| 12 // ========================= | |
| 13 // | |
| 14 // This is an example demonstrating how to enable the error resiliency | |
| 15 // features of the codec. | |
| 16 // | |
| 17 // Configuration | |
| 18 // ------------- | |
| 19 // Error resiliency is controlled by the g_error_resilient member of the | |
| 20 // configuration structure. | |
| 21 // | |
| 22 // Observing The Effects | |
| 23 // --------------------- | |
| 24 // Use the `decode_with_drops` example to decode with frames 5-10 dropped. | |
| 25 // Compare the output for a file encoded with this example versus one | |
| 26 // encoded with the `simple_encoder` example. | |
| 27 | |
| 28 #include <stdio.h> | |
| 29 #include <stdlib.h> | |
| 30 #include <stdarg.h> | |
| 31 #include <string.h> | |
| 32 #define VPX_CODEC_DISABLE_COMPAT 1 | |
| 33 #include "vpx/vpx_encoder.h" | |
| 34 #include "vpx/vp8cx.h" | |
| 35 #define interface (vpx_codec_vp8_cx()) | |
| 36 #define fourcc 0x30385056 | |
| 37 | |
| 38 #define IVF_FILE_HDR_SZ (32) | |
| 39 #define IVF_FRAME_HDR_SZ (12) | |
| 40 | |
| 41 static void mem_put_le16(char *mem, unsigned int val) { | |
| 42 mem[0] = val; | |
| 43 mem[1] = val>>8; | |
| 44 } | |
| 45 | |
| 46 static void mem_put_le32(char *mem, unsigned int val) { | |
| 47 mem[0] = val; | |
| 48 mem[1] = val>>8; | |
| 49 mem[2] = val>>16; | |
| 50 mem[3] = val>>24; | |
| 51 } | |
| 52 | |
| 53 static void die(const char *fmt, ...) { | |
| 54 va_list ap; | |
| 55 | |
| 56 va_start(ap, fmt); | |
| 57 vprintf(fmt, ap); | |
| 58 if(fmt[strlen(fmt)-1] != '\n') | |
| 59 printf("\n"); | |
| 60 exit(EXIT_FAILURE); | |
| 61 } | |
| 62 | |
| 63 static void die_codec(vpx_codec_ctx_t *ctx, const char *s) { | |
| 64 const char *detail = vpx_codec_error_detail(ctx); | |
| 65 | |
| 66 printf("%s: %s\n", s, vpx_codec_error(ctx)); | |
| 67 if(detail) | |
| 68 printf(" %s\n",detail); | |
| 69 exit(EXIT_FAILURE); | |
| 70 } | |
| 71 | |
| 72 static int read_frame(FILE *f, vpx_image_t *img) { | |
| 73 size_t nbytes, to_read; | |
| 74 int res = 1; | |
| 75 | |
| 76 to_read = img->w*img->h*3/2; | |
| 77 nbytes = fread(img->planes[0], 1, to_read, f); | |
| 78 if(nbytes != to_read) { | |
| 79 res = 0; | |
| 80 if(nbytes > 0) | |
| 81 printf("Warning: Read partial frame. Check your width & height!\n"); | |
| 82 } | |
| 83 return res; | |
| 84 } | |
| 85 | |
| 86 static void write_ivf_file_header(FILE *outfile, | |
| 87 const vpx_codec_enc_cfg_t *cfg, | |
| 88 int frame_cnt) { | |
| 89 char header[32]; | |
| 90 | |
| 91 if(cfg->g_pass != VPX_RC_ONE_PASS && cfg->g_pass != VPX_RC_LAST_PASS) | |
| 92 return; | |
| 93 header[0] = 'D'; | |
| 94 header[1] = 'K'; | |
| 95 header[2] = 'I'; | |
| 96 header[3] = 'F'; | |
| 97 mem_put_le16(header+4, 0); /* version */ | |
| 98 mem_put_le16(header+6, 32); /* headersize */ | |
| 99 mem_put_le32(header+8, fourcc); /* headersize */ | |
| 100 mem_put_le16(header+12, cfg->g_w); /* width */ | |
| 101 mem_put_le16(header+14, cfg->g_h); /* height */ | |
| 102 mem_put_le32(header+16, cfg->g_timebase.den); /* rate */ | |
| 103 mem_put_le32(header+20, cfg->g_timebase.num); /* scale */ | |
| 104 mem_put_le32(header+24, frame_cnt); /* length */ | |
| 105 mem_put_le32(header+28, 0); /* unused */ | |
| 106 | |
| 107 (void) fwrite(header, 1, 32, outfile); | |
| 108 } | |
| 109 | |
| 110 | |
| 111 static void write_ivf_frame_header(FILE *outfile, | |
| 112 const vpx_codec_cx_pkt_t *pkt) | |
| 113 { | |
| 114 char header[12]; | |
| 115 vpx_codec_pts_t pts; | |
| 116 | |
| 117 if(pkt->kind != VPX_CODEC_CX_FRAME_PKT) | |
| 118 return; | |
| 119 | |
| 120 pts = pkt->data.frame.pts; | |
| 121 mem_put_le32(header, pkt->data.frame.sz); | |
| 122 mem_put_le32(header+4, pts&0xFFFFFFFF); | |
| 123 mem_put_le32(header+8, pts >> 32); | |
| 124 | |
| 125 (void) fwrite(header, 1, 12, outfile); | |
| 126 } | |
| 127 | |
| 128 int main(int argc, char **argv) { | |
| 129 FILE *infile, *outfile; | |
| 130 vpx_codec_ctx_t codec; | |
| 131 vpx_codec_enc_cfg_t cfg; | |
| 132 int frame_cnt = 0; | |
| 133 vpx_image_t raw; | |
| 134 vpx_codec_err_t res; | |
| 135 long width; | |
| 136 long height; | |
| 137 int frame_avail; | |
| 138 int got_data; | |
| 139 int flags = 0; | |
| 140 | |
| 141 /* Open files */ | |
| 142 if(argc!=5) | |
| 143 die("Usage: %s <width> <height> <infile> <outfile>\n", argv[0]); | |
| 144 width = strtol(argv[1], NULL, 0); | |
| 145 height = strtol(argv[2], NULL, 0); | |
| 146 if(width < 16 || width%2 || height <16 || height%2) | |
| 147 die("Invalid resolution: %ldx%ld", width, height); | |
| 148 if(!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, width, height, 1)) | |
| 149 die("Faile to allocate image", width, height); | |
| 150 if(!(outfile = fopen(argv[4], "wb"))) | |
| 151 die("Failed to open %s for writing", argv[4]); | |
| 152 | |
| 153 printf("Using %s\n",vpx_codec_iface_name(interface)); | |
| 154 | |
| 155 /* Populate encoder configuration */ | |
| 156 res = vpx_codec_enc_config_default(interface, &cfg, 0); | |
| 157 if(res) { | |
| 158 printf("Failed to get config: %s\n", vpx_codec_err_to_string(res)); | |
| 159 return EXIT_FAILURE; | |
| 160 } | |
| 161 | |
| 162 /* Update the default configuration with our settings */ | |
| 163 cfg.rc_target_bitrate = width * height * cfg.rc_target_bitrate | |
| 164 / cfg.g_w / cfg.g_h; | |
| 165 cfg.g_w = width; | |
| 166 cfg.g_h = height; | |
| 167 | |
| 168 /* Enable error resilient mode */ | |
| 169 cfg.g_error_resilient = 1; | |
| 170 | |
| 171 write_ivf_file_header(outfile, &cfg, 0); | |
| 172 | |
| 173 | |
| 174 /* Open input file for this encoding pass */ | |
| 175 if(!(infile = fopen(argv[3], "rb"))) | |
| 176 die("Failed to open %s for reading", argv[3]); | |
| 177 | |
| 178 /* Initialize codec */ | |
| 179 if(vpx_codec_enc_init(&codec, interface, &cfg, 0)) | |
| 180 die_codec(&codec, "Failed to initialize encoder"); | |
| 181 | |
| 182 frame_avail = 1; | |
| 183 got_data = 0; | |
| 184 while(frame_avail || got_data) { | |
| 185 vpx_codec_iter_t iter = NULL; | |
| 186 const vpx_codec_cx_pkt_t *pkt; | |
| 187 | |
| 188 frame_avail = read_frame(infile, &raw); | |
| 189 if(vpx_codec_encode(&codec, frame_avail? &raw : NULL, frame_cnt, | |
| 190 1, flags, VPX_DL_REALTIME)) | |
| 191 die_codec(&codec, "Failed to encode frame"); | |
| 192 got_data = 0; | |
| 193 while( (pkt = vpx_codec_get_cx_data(&codec, &iter)) ) { | |
| 194 got_data = 1; | |
| 195 switch(pkt->kind) { | |
| 196 case VPX_CODEC_CX_FRAME_PKT: | |
| 197 write_ivf_frame_header(outfile, pkt); | |
| 198 (void) fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz, | |
| 199 outfile); | |
| 200 break; | |
| 201 default: | |
| 202 break; | |
| 203 } | |
| 204 printf(pkt->kind == VPX_CODEC_CX_FRAME_PKT | |
| 205 && (pkt->data.frame.flags & VPX_FRAME_IS_KEY)? "K":"."); | |
| 206 fflush(stdout); | |
| 207 } | |
| 208 frame_cnt++; | |
| 209 } | |
| 210 printf("\n"); | |
| 211 fclose(infile); | |
| 212 | |
| 213 printf("Processed %d frames.\n",frame_cnt-1); | |
| 214 vpx_img_free(&raw); | |
| 215 if(vpx_codec_destroy(&codec)) | |
| 216 die_codec(&codec, "Failed to destroy codec"); | |
| 217 | |
| 218 /* Try to rewrite the file header with the actual frame count */ | |
| 219 if(!fseek(outfile, 0, SEEK_SET)) | |
| 220 write_ivf_file_header(outfile, &cfg, frame_cnt-1); | |
| 221 fclose(outfile); | |
| 222 return EXIT_SUCCESS; | |
| 223 } | |
| OLD | NEW |