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// Decode With Partial Drops Example 12// ========================= 13// 14// This is an example utility which drops a series of frames (or parts of 15// frames), as specified on the command line. This is useful for observing the 16// error recovery features of the codec. 17// 18// Usage 19// ----- 20// This example adds a single argument to the `simple_decoder` example, 21// which specifies the range or pattern of frames to drop. The parameter is 22// parsed as follows. 23// 24// Dropping A Range Of Frames 25// -------------------------- 26// To drop a range of frames, specify the starting frame and the ending 27// frame to drop, separated by a dash. The following command will drop 28// frames 5 through 10 (base 1). 29// 30// $ ./decode_with_partial_drops in.ivf out.i420 5-10 31// 32// 33// Dropping A Pattern Of Frames 34// ---------------------------- 35// To drop a pattern of frames, specify the number of frames to drop and 36// the number of frames after which to repeat the pattern, separated by 37// a forward-slash. The following command will drop 3 of 7 frames. 38// Specifically, it will decode 4 frames, then drop 3 frames, and then 39// repeat. 40// 41// $ ./decode_with_partial_drops in.ivf out.i420 3/7 42// 43// Dropping Random Parts Of Frames 44// ------------------------------- 45// A third argument tuple is available to split the frame into 1500 bytes pieces 46// and randomly drop pieces rather than frames. The frame will be split at 47// partition boundaries where possible. The following example will seed the RNG 48// with the seed 123 and drop approximately 5% of the pieces. Pieces which 49// are depending on an already dropped piece will also be dropped. 50// 51// $ ./decode_with_partial_drops in.ivf out.i420 5,123 52// 53// Extra Variables 54// --------------- 55// This example maintains the pattern passed on the command line in the 56// `n`, `m`, and `is_range` variables: 57// 58// Making The Drop Decision 59// ------------------------ 60// The example decides whether to drop the frame based on the current 61// frame number, immediately before decoding the frame. 62 63#include <stdarg.h> 64#include <stdio.h> 65#include <stdlib.h> 66#include <string.h> 67#define VPX_CODEC_DISABLE_COMPAT 1 68#include "./vpx_config.h" 69#include "vpx/vp8dx.h" 70#include "vpx/vpx_decoder.h" 71#define interface (vpx_codec_vp8_dx()) 72#include <time.h> 73 74 75#define IVF_FILE_HDR_SZ (32) 76#define IVF_FRAME_HDR_SZ (12) 77 78static unsigned int mem_get_le32(const unsigned char *mem) { 79 return (mem[3] << 24)|(mem[2] << 16)|(mem[1] << 8)|(mem[0]); 80} 81 82static void die(const char *fmt, ...) { 83 va_list ap; 84 85 va_start(ap, fmt); 86 vprintf(fmt, ap); 87 if(fmt[strlen(fmt)-1] != '\n') 88 printf("\n"); 89 exit(EXIT_FAILURE); 90} 91 92static void die_codec(vpx_codec_ctx_t *ctx, const char *s) { 93 const char *detail = vpx_codec_error_detail(ctx); 94 95 printf("%s: %s\n", s, vpx_codec_error(ctx)); 96 if(detail) 97 printf(" %s\n",detail); 98 exit(EXIT_FAILURE); 99} 100 101struct parsed_header 102{ 103 char key_frame; 104 int version; 105 char show_frame; 106 int first_part_size; 107}; 108 109int next_packet(struct parsed_header* hdr, int pos, int length, int mtu) 110{ 111 int size = 0; 112 int remaining = length - pos; 113 /* Uncompressed part is 3 bytes for P frames and 10 bytes for I frames */ 114 int uncomp_part_size = (hdr->key_frame ? 10 : 3); 115 /* number of bytes yet to send from header and the first partition */ 116 int remainFirst = uncomp_part_size + hdr->first_part_size - pos; 117 if (remainFirst > 0) 118 { 119 if (remainFirst <= mtu) 120 { 121 size = remainFirst; 122 } 123 else 124 { 125 size = mtu; 126 } 127 128 return size; 129 } 130 131 /* second partition; just slot it up according to MTU */ 132 if (remaining <= mtu) 133 { 134 size = remaining; 135 return size; 136 } 137 return mtu; 138} 139 140void throw_packets(unsigned char* frame, int* size, int loss_rate, 141 int* thrown, int* kept) 142{ 143 unsigned char loss_frame[256*1024]; 144 int pkg_size = 1; 145 int pos = 0; 146 int loss_pos = 0; 147 struct parsed_header hdr; 148 unsigned int tmp; 149 int mtu = 1500; 150 151 if (*size < 3) 152 { 153 return; 154 } 155 putc('|', stdout); 156 /* parse uncompressed 3 bytes */ 157 tmp = (frame[2] << 16) | (frame[1] << 8) | frame[0]; 158 hdr.key_frame = !(tmp & 0x1); /* inverse logic */ 159 hdr.version = (tmp >> 1) & 0x7; 160 hdr.show_frame = (tmp >> 4) & 0x1; 161 hdr.first_part_size = (tmp >> 5) & 0x7FFFF; 162 163 /* don't drop key frames */ 164 if (hdr.key_frame) 165 { 166 int i; 167 *kept = *size/mtu + ((*size % mtu > 0) ? 1 : 0); /* approximate */ 168 for (i=0; i < *kept; i++) 169 putc('.', stdout); 170 return; 171 } 172 173 while ((pkg_size = next_packet(&hdr, pos, *size, mtu)) > 0) 174 { 175 int loss_event = ((rand() + 1.0)/(RAND_MAX + 1.0) < loss_rate/100.0); 176 if (*thrown == 0 && !loss_event) 177 { 178 memcpy(loss_frame + loss_pos, frame + pos, pkg_size); 179 loss_pos += pkg_size; 180 (*kept)++; 181 putc('.', stdout); 182 } 183 else 184 { 185 (*thrown)++; 186 putc('X', stdout); 187 } 188 pos += pkg_size; 189 } 190 memcpy(frame, loss_frame, loss_pos); 191 memset(frame + loss_pos, 0, *size - loss_pos); 192 *size = loss_pos; 193} 194 195int main(int argc, char **argv) { 196 FILE *infile, *outfile; 197 vpx_codec_ctx_t codec; 198 int flags = 0, frame_cnt = 0; 199 unsigned char file_hdr[IVF_FILE_HDR_SZ]; 200 unsigned char frame_hdr[IVF_FRAME_HDR_SZ]; 201 unsigned char frame[256*1024]; 202 vpx_codec_err_t res; 203 int n, m, mode; 204 unsigned int seed; 205 int thrown=0, kept=0; 206 int thrown_frame=0, kept_frame=0; 207 vpx_codec_dec_cfg_t dec_cfg = {0}; 208 209 (void)res; 210 /* Open files */ 211 if(argc < 4 || argc > 6) 212 die("Usage: %s <infile> <outfile> [-t <num threads>] <N-M|N/M|L,S>\n", 213 argv[0]); 214 { 215 char *nptr; 216 int arg_num = 3; 217 if (argc == 6 && strncmp(argv[arg_num++], "-t", 2) == 0) 218 dec_cfg.threads = strtol(argv[arg_num++], NULL, 0); 219 n = strtol(argv[arg_num], &nptr, 0); 220 mode = (*nptr == '\0' || *nptr == ',') ? 2 : (*nptr == '-') ? 1 : 0; 221 222 m = strtol(nptr+1, NULL, 0); 223 if((!n && !m) || (*nptr != '-' && *nptr != '/' && 224 *nptr != '\0' && *nptr != ',')) 225 die("Couldn't parse pattern %s\n", argv[3]); 226 } 227 seed = (m > 0) ? m : (unsigned int)time(NULL); 228 srand(seed);thrown_frame = 0; 229 printf("Seed: %u\n", seed); 230 printf("Threads: %d\n", dec_cfg.threads); 231 if(!(infile = fopen(argv[1], "rb"))) 232 die("Failed to open %s for reading", argv[1]); 233 if(!(outfile = fopen(argv[2], "wb"))) 234 die("Failed to open %s for writing", argv[2]); 235 236 /* Read file header */ 237 if(!(fread(file_hdr, 1, IVF_FILE_HDR_SZ, infile) == IVF_FILE_HDR_SZ 238 && file_hdr[0]=='D' && file_hdr[1]=='K' && file_hdr[2]=='I' 239 && file_hdr[3]=='F')) 240 die("%s is not an IVF file.", argv[1]); 241 242 printf("Using %s\n",vpx_codec_iface_name(interface)); 243 /* Initialize codec */ 244 flags = VPX_CODEC_USE_ERROR_CONCEALMENT; 245 res = vpx_codec_dec_init(&codec, interface, &dec_cfg, flags); 246 if(res) 247 die_codec(&codec, "Failed to initialize decoder"); 248 249 250 /* Read each frame */ 251 while(fread(frame_hdr, 1, IVF_FRAME_HDR_SZ, infile) == IVF_FRAME_HDR_SZ) { 252 int frame_sz = mem_get_le32(frame_hdr); 253 vpx_codec_iter_t iter = NULL; 254 vpx_image_t *img; 255 256 257 frame_cnt++; 258 if(frame_sz > sizeof(frame)) 259 die("Frame %d data too big for example code buffer", frame_sz); 260 if(fread(frame, 1, frame_sz, infile) != frame_sz) 261 die("Frame %d failed to read complete frame", frame_cnt); 262 263 /* Decide whether to throw parts of the frame or the whole frame 264 depending on the drop mode */ 265 thrown_frame = 0; 266 kept_frame = 0; 267 switch (mode) 268 { 269 case 0: 270 if (m - (frame_cnt-1)%m <= n) 271 { 272 frame_sz = 0; 273 } 274 break; 275 case 1: 276 if (frame_cnt >= n && frame_cnt <= m) 277 { 278 frame_sz = 0; 279 } 280 break; 281 case 2: 282 throw_packets(frame, &frame_sz, n, &thrown_frame, &kept_frame); 283 break; 284 default: break; 285 } 286 if (mode < 2) 287 { 288 if (frame_sz == 0) 289 { 290 putc('X', stdout); 291 thrown_frame++; 292 } 293 else 294 { 295 putc('.', stdout); 296 kept_frame++; 297 } 298 } 299 thrown += thrown_frame; 300 kept += kept_frame; 301 fflush(stdout); 302 /* Decode the frame */ 303 if(vpx_codec_decode(&codec, frame, frame_sz, NULL, 0)) 304 die_codec(&codec, "Failed to decode frame"); 305 306 /* Write decoded data to disk */ 307 while((img = vpx_codec_get_frame(&codec, &iter))) { 308 unsigned int plane, y; 309 310 for(plane=0; plane < 3; plane++) { 311 unsigned char *buf =img->planes[plane]; 312 313 for(y=0; y < (plane ? (img->d_h + 1) >> 1 : img->d_h); y++) { 314 (void) fwrite(buf, 1, (plane ? (img->d_w + 1) >> 1 : img->d_w), 315 outfile); 316 buf += img->stride[plane]; 317 } 318 } 319 } 320 } 321 printf("Processed %d frames.\n",frame_cnt); 322 if(vpx_codec_destroy(&codec)) 323 die_codec(&codec, "Failed to destroy codec"); 324 325 fclose(outfile); 326 fclose(infile); 327 return EXIT_SUCCESS; 328} 329