int file_is_ivf(struct VpxInputContext *input_ctx) { char raw_hdr[32]; int is_ivf = 0; if (fread(raw_hdr, 1, 32, input_ctx->file) == 32) { if (memcmp(IVF_SIGNATURE, raw_hdr, 4) == 0) { is_ivf = 1; if (mem_get_le16(raw_hdr + 4) != 0) { fprintf(stderr, "Error: Unrecognized IVF version! This file may not" " decode properly."); } input_ctx->fourcc = mem_get_le32(raw_hdr + 8); input_ctx->width = mem_get_le16(raw_hdr + 12); input_ctx->height = mem_get_le16(raw_hdr + 14); input_ctx->framerate.numerator = mem_get_le32(raw_hdr + 16); input_ctx->framerate.denominator = mem_get_le32(raw_hdr + 20); fix_framerate(&input_ctx->framerate.numerator, &input_ctx->framerate.denominator); } } if (!is_ivf) { rewind(input_ctx->file); input_ctx->detect.buf_read = 0; } else { input_ctx->detect.position = 4; } return is_ivf; }
unsigned int file_is_ivf(FILE *infile, unsigned int *fourcc, unsigned int *width, unsigned int *height, unsigned int *fps_den, unsigned int *fps_num) { char raw_hdr[32]; int is_ivf = 0; if (fread(raw_hdr, 1, 32, infile) == 32) { if (raw_hdr[0] == 'D' && raw_hdr[1] == 'K' && raw_hdr[2] == 'I' && raw_hdr[3] == 'F') { is_ivf = 1; if (mem_get_le16(raw_hdr + 4) != 0) fprintf(stderr, "Error: Unrecognized IVF version! This file may not" " decode properly."); *fourcc = mem_get_le32(raw_hdr + 8); *width = mem_get_le16(raw_hdr + 12); *height = mem_get_le16(raw_hdr + 14); *fps_num = mem_get_le32(raw_hdr + 16); *fps_den = mem_get_le32(raw_hdr + 20); if (*fps_num < 1000) { if (*fps_num & 1)*fps_den <<= 1; else *fps_num >>= 1; } else { *fps_num = 30; *fps_den = 1; } }
VpxVideoReader *vpx_video_reader_open(const char *filename) { char header[32]; VpxVideoReader *reader = NULL; FILE *const file = fopen(filename, "rb"); if (!file) return NULL; // Can't open file if (fread(header, 1, 32, file) != 32) return NULL; // Can't read file header if (memcmp(kIVFSignature, header, 4) != 0) return NULL; // Wrong IVF signature if (mem_get_le16(header + 4) != 0) return NULL; // Wrong IVF version reader = calloc(1, sizeof(*reader)); if (!reader) return NULL; // Can't allocate VpxVideoReader reader->file = file; reader->info.codec_fourcc = mem_get_le32(header + 8); reader->info.frame_width = mem_get_le16(header + 12); reader->info.frame_height = mem_get_le16(header + 14); reader->info.time_base.numerator = mem_get_le32(header + 16); reader->info.time_base.denominator = mem_get_le32(header + 20); return reader; }
int file_is_raw(struct VpxInputContext *input) { uint8_t buf[32]; int is_raw = 0; vpx_codec_stream_info_t si; si.sz = sizeof(si); if (fread(buf, 1, 32, input->file) == 32) { int i; if (mem_get_le32(buf) < 256 * 1024 * 1024) { for (i = 0; i < get_vpx_decoder_count(); ++i) { const VpxInterface *const decoder = get_vpx_decoder_by_index(i); if (!vpx_codec_peek_stream_info(decoder->interface(), buf + 4, 32 - 4, &si)) { is_raw = 1; input->fourcc = decoder->fourcc; input->width = si.w; input->height = si.h; input->framerate.numerator = 30; input->framerate.denominator = 1; break; } } } } rewind(input->file); return is_raw; }
static int is_raw (OMX_U8 * p_buf, unsigned int *fourcc, unsigned int *width, unsigned int *height, unsigned int *fps_den, unsigned int *fps_num) { unsigned char buf[32]; vpx_codec_stream_info_t si; int i = 0; int is_raw = 0; si.sz = sizeof (si); if (mem_get_le32 (buf) < 256 * 1024 * 1024) for (i = 0; i < sizeof (ifaces) / sizeof (ifaces[0]); i++) if (!vpx_codec_peek_stream_info (ifaces[i].iface, buf + 4, 32 - 4, &si)) { is_raw = 1; *fourcc = ifaces[i].fourcc; *width = si.w; *height = si.h; *fps_num = 30; *fps_den = 1; break; } return is_raw; }
int file_is_raw(struct VpxInputContext *input) { uint8_t buf[32]; int is_raw = 0; vpx_codec_stream_info_t si; si.sz = sizeof(si); if (fread(buf, 1, 32, input->file) == 32) { int i; if (mem_get_le32(buf) < 256 * 1024 * 1024) { for (i = 0; i < sizeof(ifaces) / sizeof(ifaces[0]); i++) { if (!vpx_codec_peek_stream_info(ifaces[i].iface(), buf + 4, 32 - 4, &si)) { is_raw = 1; input->fourcc = ifaces[i].fourcc; input->width = si.w; input->height = si.h; input->framerate.numerator = 30; input->framerate.denominator = 1; break; } } } } rewind(input->file); return is_raw; }
unsigned int file_is_ivf(FILE *infile, unsigned int *fourcc, unsigned int *width, unsigned int *height, unsigned int *fps_den, unsigned int *fps_num) { char raw_hdr[32]; int is_ivf = 0; if (fread(raw_hdr, 1, 32, infile) == 32) { if (raw_hdr[0] == 'D' && raw_hdr[1] == 'K' && raw_hdr[2] == 'I' && raw_hdr[3] == 'F') { is_ivf = 1; if (mem_get_le16(raw_hdr + 4) != 0) fprintf(stderr, "Error: Unrecognized IVF version! This file may not" " decode properly."); *fourcc = mem_get_le32(raw_hdr + 8); *width = mem_get_le16(raw_hdr + 12); *height = mem_get_le16(raw_hdr + 14); *fps_num = mem_get_le32(raw_hdr + 16); *fps_den = mem_get_le32(raw_hdr + 20); /* Some versions of vpxenc used 1/(2*fps) for the timebase, so * we can guess the framerate using only the timebase in this * case. Other files would require reading ahead to guess the * timebase, like we do for webm. */ if (*fps_num < 1000) { /* Correct for the factor of 2 applied to the timebase in the * encoder. */ if (*fps_num & 1)*fps_den <<= 1; else *fps_num >>= 1; } else { /* Don't know FPS for sure, and don't have readahead code * (yet?), so just default to 30fps. */ *fps_num = 30; *fps_den = 1; } }
static int is_ivf (OMX_U8 * p_buf, unsigned int *fourcc, unsigned int *width, unsigned int *height, unsigned int *fps_den, unsigned int *fps_num) { int is_ivf = 0; if (p_buf[0] == 'D' && p_buf[1] == 'K' && p_buf[2] == 'I' && p_buf[3] == 'F') { is_ivf = 1; if (mem_get_le16 (p_buf + 4) != 0) fprintf (stderr, "Error: Unrecognized IVF version! This file may not" " decode properly."); *fourcc = mem_get_le32 (p_buf + 8); *width = mem_get_le16 (p_buf + 12); *height = mem_get_le16 (p_buf + 14); *fps_num = mem_get_le32 (p_buf + 16); *fps_den = mem_get_le32 (p_buf + 20); /* Some versions of vpxenc used 1/(2*fps) for the timebase, so * we can guess the framerate using only the timebase in this * case. Other files would require reading ahead to guess the * timebase, like we do for webm. */ if (*fps_num < 1000) { /* Correct for the factor of 2 applied to the timebase in the * encoder. */ if (*fps_num & 1) *fps_den <<= 1; else *fps_num >>= 1; } else {
static int raw_read_frame(FILE *infile, uint8_t **buffer, size_t *bytes_read, size_t *buffer_size) { char raw_hdr[RAW_FRAME_HDR_SZ]; size_t frame_size = 0; if (fread(raw_hdr, RAW_FRAME_HDR_SZ, 1, infile) != 1) { if (!feof(infile)) warn("Failed to read RAW frame size\n"); } else { const size_t kCorruptFrameThreshold = 256 * 1024 * 1024; const size_t kFrameTooSmallThreshold = 256 * 1024; frame_size = mem_get_le32(raw_hdr); if (frame_size > kCorruptFrameThreshold) { warn("Read invalid frame size (%u)\n", (unsigned int)frame_size); frame_size = 0; } if (frame_size < kFrameTooSmallThreshold) { warn("Warning: Read invalid frame size (%u) - not a raw file?\n", (unsigned int)frame_size); } if (frame_size > *buffer_size) { uint8_t *new_buf = realloc(*buffer, 2 * frame_size); if (new_buf) { *buffer = new_buf; *buffer_size = 2 * frame_size; } else { warn("Failed to allocate compressed data buffer\n"); frame_size = 0; } } } if (!feof(infile)) { if (fread(*buffer, 1, frame_size, infile) != frame_size) { warn("Failed to read full frame\n"); return 1; } *bytes_read = frame_size; } return 0; }
int ivf_read_frame(FILE *infile, uint8_t **buffer, size_t *bytes_read, size_t *buffer_size) { char raw_header[IVF_FRAME_HDR_SZ] = {0}; size_t frame_size = 0; if (fread(raw_header, IVF_FRAME_HDR_SZ, 1, infile) != 1) { if (!feof(infile)) warn("Failed to read frame size\n"); } else { frame_size = mem_get_le32(raw_header); if (frame_size > 256 * 1024 * 1024) { warn("Read invalid frame size (%u)\n", (unsigned int)frame_size); frame_size = 0; } if (frame_size > *buffer_size) { uint8_t *new_buffer = realloc(*buffer, 2 * frame_size); if (new_buffer) { *buffer = new_buffer; *buffer_size = 2 * frame_size; } else { warn("Failed to allocate compressed data buffer\n"); frame_size = 0; } } } if (!feof(infile)) { if (fread(*buffer, 1, frame_size, infile) != frame_size) { warn("Failed to read full frame\n"); return 1; } *bytes_read = frame_size; return 0; } return 1; }
unsigned int file_is_ivf(FILE *infile, unsigned int *fourcc, unsigned int *width, unsigned int *height, char detect[4]) { char raw_hdr[IVF_FILE_HDR_SZ]; int is_ivf = 0; if(memcmp(detect, "DKIF", 4) != 0) return 0; /* See write_ivf_file_header() for more documentation on the file header * layout. */ if (fread(raw_hdr + 4, 1, IVF_FILE_HDR_SZ - 4, infile) == IVF_FILE_HDR_SZ - 4) { { is_ivf = 1; if (mem_get_le16(raw_hdr + 4) != 0) fprintf(stderr, "Error: Unrecognized IVF version! This file may not" " decode properly."); *fourcc = mem_get_le32(raw_hdr + 8); } } if (is_ivf) { *width = mem_get_le16(raw_hdr + 12); *height = mem_get_le16(raw_hdr + 14); } return is_ivf; }
static int read_frame(struct input_ctx *input, uint8_t **buf, size_t *buf_sz, size_t *buf_alloc_sz) { char raw_hdr[IVF_FRAME_HDR_SZ]; size_t new_buf_sz; FILE *infile = input->infile; enum file_kind kind = input->kind; if (kind == WEBM_FILE) { if (input->chunk >= input->chunks) { unsigned int track; do { /* End of this packet, get another. */ if (input->pkt) nestegg_free_packet(input->pkt); if (nestegg_read_packet(input->nestegg_ctx, &input->pkt) <= 0 || nestegg_packet_track(input->pkt, &track)) return 1; } while (track != input->video_track); if (nestegg_packet_count(input->pkt, &input->chunks)) return 1; input->chunk = 0; } if (nestegg_packet_data(input->pkt, input->chunk, buf, buf_sz)) return 1; input->chunk++; return 0; } /* For both the raw and ivf formats, the frame size is the first 4 bytes * of the frame header. We just need to special case on the header * size. */ else if (fread(raw_hdr, kind == IVF_FILE ? IVF_FRAME_HDR_SZ : RAW_FRAME_HDR_SZ, 1, infile) != 1) { if (!feof(infile)) fprintf(stderr, "Failed to read frame size\n"); new_buf_sz = 0; } else { new_buf_sz = mem_get_le32(raw_hdr); if (new_buf_sz > 256 * 1024 * 1024) { fprintf(stderr, "Error: Read invalid frame size (%u)\n", (unsigned int)new_buf_sz); new_buf_sz = 0; } if (kind == RAW_FILE && new_buf_sz > 256 * 1024) fprintf(stderr, "Warning: Read invalid frame size (%u)" " - not a raw file?\n", (unsigned int)new_buf_sz); if (new_buf_sz > *buf_alloc_sz) { uint8_t *new_buf = realloc(*buf, 2 * new_buf_sz); if (new_buf) { *buf = new_buf; *buf_alloc_sz = 2 * new_buf_sz; } else { fprintf(stderr, "Failed to allocate compressed data buffer\n"); new_buf_sz = 0; } } } *buf_sz = new_buf_sz; if (!feof(infile)) { if (fread(*buf, 1, *buf_sz, infile) != *buf_sz) { fprintf(stderr, "Failed to read full frame\n"); return 1; } return 0; } return 1; }
int main(int argc, char **argv) { FILE *infile, *outfile; vpx_codec_ctx_t codec; vpx_codec_iface_t *iface; int flags = 0, frame_cnt = 0; unsigned char file_hdr[IVF_FILE_HDR_SZ]; unsigned char frame_hdr[IVF_FRAME_HDR_SZ]; unsigned char frame[256 * 1024]; if (argc != 3) die("Usage: %s <infile> <outfile>\n", argv[0]); if (!(infile = fopen(argv[1], "rb"))) die("Failed to open %s for reading", argv[1]); if (!(outfile = fopen(argv[2], "wb"))) die("Failed to open %s for writing", argv[2]); if (!(fread(file_hdr, 1, IVF_FILE_HDR_SZ, infile) == IVF_FILE_HDR_SZ && file_hdr[0] == 'D' && file_hdr[1] == 'K' && file_hdr[2] == 'I' && file_hdr[3] == 'F')) die("%s is not an IVF file.", argv[1]); iface = get_codec_interface(mem_get_le32(file_hdr + 8)); if (!iface) die("Unknown FOURCC code."); printf("Using %s\n", vpx_codec_iface_name(iface)); if (vpx_codec_dec_init(&codec, iface, NULL, flags)) die_codec(&codec, "Failed to initialize decoder"); while (fread(frame_hdr, 1, IVF_FRAME_HDR_SZ, infile) == IVF_FRAME_HDR_SZ) { const int frame_size = mem_get_le32(frame_hdr); vpx_codec_iter_t iter = NULL; vpx_image_t *img; if (frame_size > sizeof(frame)) die("Frame %d data too big for example code buffer", frame_size); if (fread(frame, 1, frame_size, infile) != frame_size) die("Failed to read complete frame"); if (vpx_codec_decode(&codec, frame, frame_size, NULL, 0)) die_codec(&codec, "Failed to decode frame"); while ((img = vpx_codec_get_frame(&codec, &iter)) != NULL) { unsigned char digest[16]; get_image_md5(img, digest); print_md5(outfile, digest); fprintf(outfile, " img-%dx%d-%04d.i420\n", img->d_w, img->d_h, ++frame_cnt); } } printf("Processed %d frames.\n", frame_cnt); if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec"); fclose(outfile); fclose(infile); return EXIT_SUCCESS; }
int main(int argc, char **argv) { FILE *infile, *outfile; vpx_codec_ctx_t codec; int flags = 0, frame_cnt = 0; unsigned char file_hdr[IVF_FILE_HDR_SZ]; unsigned char frame_hdr[IVF_FRAME_HDR_SZ]; unsigned char frame[256*1024]; vpx_codec_err_t res; int n, m, is_range; // (void)res; /* Open files */ if(argc!=4) // die("Usage: %s <infile> <outfile> <N-M|N/M>\n", argv[0]); // { // char *nptr; // n = strtol(argv[3], &nptr, 0); // m = strtol(nptr+1, NULL, 0); // is_range = *nptr == '-'; // if(!n || !m || (*nptr != '-' && *nptr != '/')) // die("Couldn't parse pattern %s\n", argv[3]); // } // if(!(infile = fopen(argv[1], "rb"))) die("Failed to open %s for reading", argv[1]); if(!(outfile = fopen(argv[2], "wb"))) die("Failed to open %s for writing", argv[2]); /* Read file header */ if(!(fread(file_hdr, 1, IVF_FILE_HDR_SZ, infile) == IVF_FILE_HDR_SZ && file_hdr[0]=='D' && file_hdr[1]=='K' && file_hdr[2]=='I' && file_hdr[3]=='F')) die("%s is not an IVF file.", argv[1]); printf("Using %s\n",vpx_codec_iface_name(interface)); /* Initialize codec */ if(vpx_codec_dec_init(&codec, interface, NULL, flags)) die_codec(&codec, "Failed to initialize decoder"); /* Read each frame */ while(fread(frame_hdr, 1, IVF_FRAME_HDR_SZ, infile) == IVF_FRAME_HDR_SZ) { int frame_sz = mem_get_le32(frame_hdr); vpx_codec_iter_t iter = NULL; vpx_image_t *img; frame_cnt++; if(frame_sz > sizeof(frame)) die("Frame %d data too big for example code buffer", frame_sz); if(fread(frame, 1, frame_sz, infile) != frame_sz) die("Frame %d failed to read complete frame", frame_cnt); if((is_range && frame_cnt >= n && frame_cnt <= m) // ||(!is_range && m - (frame_cnt-1)%m <= n)) { // putc('X', stdout); // continue; // } // putc('.', stdout); // fflush(stdout); // /* Decode the frame */ if(vpx_codec_decode(&codec, frame, frame_sz, NULL, 0)) die_codec(&codec, "Failed to decode frame"); /* Write decoded data to disk */ while((img = vpx_codec_get_frame(&codec, &iter))) { unsigned int plane, y; for(plane=0; plane < 3; plane++) { unsigned char *buf =img->planes[plane]; for(y=0; y<img->d_h >> (plane?1:0); y++) { if(fwrite(buf, 1, img->d_w >> (plane?1:0), outfile)); buf += img->stride[plane]; } } } } printf("Processed %d frames.\n",frame_cnt); if(vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec"); fclose(outfile); fclose(infile); return EXIT_SUCCESS; }