21 #define VPX_CODEC_DISABLE_COMPAT 1
24 #define interface (vpx_codec_vp8_cx())
25 #define fourcc 0x30385056
27 #define IVF_FILE_HDR_SZ (32)
28 #define IVF_FRAME_HDR_SZ (12)
30 static void mem_put_le16(
char *mem,
unsigned int val) {
35 static void mem_put_le32(
char *mem,
unsigned int val) {
42 static void die(
const char *fmt, ...) {
47 if(fmt[strlen(fmt)-1] !=
'\n')
57 printf(
" %s\n",detail);
62 size_t nbytes, to_read;
65 to_read = img->
w*img->
h*3/2;
66 nbytes = fread(img->
planes[0], 1, to_read, f);
67 if(nbytes != to_read) {
70 printf(
"Warning: Read partial frame. Check your width & height!\n");
75 static void write_ivf_file_header(FILE *outfile,
86 mem_put_le16(header+4, 0);
87 mem_put_le16(header+6, 32);
88 mem_put_le32(header+8, fourcc);
89 mem_put_le16(header+12, cfg->
g_w);
90 mem_put_le16(header+14, cfg->
g_h);
93 mem_put_le32(header+24, frame_cnt);
94 mem_put_le32(header+28, 0);
96 if(fwrite(header, 1, 32, outfile));
100 static void write_ivf_frame_header(FILE *outfile,
110 mem_put_le32(header, pkt->
data.
frame.sz);
111 mem_put_le32(header+4, pts&0xFFFFFFFF);
112 mem_put_le32(header+8, pts >> 32);
114 if(fwrite(header, 1, 12, outfile));
117 static int mode_to_num_layers[7] = {2, 2, 3, 3, 3, 3, 5};
119 int main(
int argc,
char **argv) {
120 FILE *infile, *outfile[MAX_LAYERS];
133 int frame_duration = 1;
135 int layering_mode = 0;
136 int frames_in_layer[MAX_LAYERS] = {0};
137 int layer_flags[MAX_PERIODICITY] = {0};
141 die(
"Usage: %s <infile> <outfile> <width> <height> <rate_num> "
142 " <rate_den> <mode> <Rate_0> ... <Rate_nlayers-1>\n", argv[0]);
144 width = strtol (argv[3], NULL, 0);
145 height = strtol (argv[4], NULL, 0);
146 if (width < 16 || width%2 || height <16 || height%2)
147 die (
"Invalid resolution: %d x %d", width, height);
149 if (!sscanf(argv[7],
"%d", &layering_mode))
150 die (
"Invalid mode %s", argv[7]);
151 if (layering_mode<0 || layering_mode>6)
152 die (
"Invalid mode (0..6) %s", argv[7]);
154 if (argc != 8+mode_to_num_layers[layering_mode])
155 die (
"Invalid number of arguments");
158 die (
"Failed to allocate image", width, height);
175 die (
"Invalid timebase numerator %s", argv[5]);
177 die (
"Invalid timebase denominator %s", argv[6]);
179 for (i=8; i<8+mode_to_num_layers[layering_mode]; i++)
181 die (
"Invalid data rate %s", argv[i]);
208 switch (layering_mode)
242 int ids[3] = {0,1,1};
263 int ids[6] = {0,2,2,1,2,2};
287 int ids[4] = {0,2,1,2};
312 int ids[4] = {0,2,1,2};
337 int ids[4] = {0,2,1,2};
361 int ids[16] = {0,4,3,4,2,4,3,4,1,4,3,4,2,4,3,4};
399 if(!(infile = fopen(argv[1],
"rb")))
400 die(
"Failed to open %s for reading", argv[1]);
406 sprintf (file_name,
"%s_%d.ivf", argv[2], i);
407 if (!(outfile[i] = fopen(file_name,
"wb")))
408 die(
"Failed to open %s for writing", file_name);
409 write_ivf_file_header(outfile[i], &cfg, 0);
414 die_codec (&codec,
"Failed to initialize encoder");
421 while (frame_avail || got_data) {
427 frame_avail = read_frame(infile, &raw);
430 die_codec(&codec,
"Failed to encode frame");
443 write_ivf_frame_header(outfile[i], pkt);
446 frames_in_layer[i]++;
457 pts += frame_duration;
462 printf (
"Processed %d frames.\n",frame_cnt-1);
464 die_codec (&codec,
"Failed to destroy codec");
469 if (!fseek(outfile[i], 0, SEEK_SET))
470 write_ivf_file_header (outfile[i], &cfg, frames_in_layer[i]);
unsigned int rc_buf_initial_sz
Decoder Buffer Initial Size.
Definition: vpx_encoder.h:533
unsigned int ts_number_layers
Number of coding layers.
Definition: vpx_encoder.h:618
control function to set vp8 encoder cpuused
Definition: vp8cx.h:143
#define VP8_EFLAG_NO_REF_LAST
Don't reference the last frame.
Definition: vp8cx.h:48
#define VP8_EFLAG_NO_UPD_GF
Don't update the golden frame.
Definition: vp8cx.h:82
Image Descriptor.
Definition: vpx_image.h:97
Describes the encoder algorithm interface to applications.
const char * vpx_codec_iface_name(vpx_codec_iface_t *iface)
Return the name for a given interface.
const char * vpx_codec_err_to_string(vpx_codec_err_t err)
Convert error number to printable string.
struct vpx_rational g_timebase
Stream timebase units.
Definition: vpx_encoder.h:339
Definition: vpx_encoder.h:232
unsigned int rc_buf_sz
Decoder Buffer Size.
Definition: vpx_encoder.h:523
#define VP8_EFLAG_NO_REF_GF
Don't reference the golden frame.
Definition: vp8cx.h:57
enum vpx_kf_mode kf_mode
Keyframe placement mode.
Definition: vpx_encoder.h:588
int den
Definition: vpx_encoder.h:215
vpx_codec_err_t vpx_codec_encode(vpx_codec_ctx_t *ctx, const vpx_image_t *img, vpx_codec_pts_t pts, unsigned long duration, vpx_enc_frame_flags_t flags, unsigned long deadline)
Encode a frame.
unsigned int rc_max_quantizer
Maximum (Worst Quality) Quantizer.
Definition: vpx_encoder.h:475
unsigned int rc_min_quantizer
Minimum (Best Quality) Quantizer.
Definition: vpx_encoder.h:464
unsigned int kf_max_dist
Keyframe maximum interval.
Definition: vpx_encoder.h:608
unsigned int g_lag_in_frames
Allow lagged encoding.
Definition: vpx_encoder.h:371
Encoder configuration structure.
Definition: vpx_encoder.h:270
Max data rate for Intra frames.
Definition: vp8cx.h:180
Encoder output packet.
Definition: vpx_encoder.h:167
unsigned int rc_overshoot_pct
Rate control adaptation overshoot control.
Definition: vpx_encoder.h:506
unsigned int ts_rate_decimator[5]
Frame rate decimation factor for each layer.
Definition: vpx_encoder.h:631
unsigned int rc_buf_optimal_sz
Decoder Buffer Optimal Size.
Definition: vpx_encoder.h:543
unsigned int kf_min_dist
Keyframe minimum interval.
Definition: vpx_encoder.h:598
Definition: vpx_encoder.h:224
unsigned int ts_layer_id[16]
Template defining the membership of frames to coding layers.
Definition: vpx_encoder.h:649
struct vpx_codec_cx_pkt::@1::@2 frame
vpx_image_t * vpx_img_alloc(vpx_image_t *img, vpx_img_fmt_t fmt, unsigned int d_w, unsigned int d_h, unsigned int align)
Open a descriptor, allocating storage for the underlying image.
Definition: vpx_image.h:56
unsigned int g_w
Width of the frame.
Definition: vpx_encoder.h:314
unsigned int ts_target_bitrate[5]
Target bitrate for each layer.
Definition: vpx_encoder.h:624
unsigned int rc_undershoot_pct
Rate control adaptation undershoot control.
Definition: vpx_encoder.h:493
unsigned int g_h
Height of the frame.
Definition: vpx_encoder.h:324
enum vpx_codec_cx_pkt_kind kind
Definition: vpx_encoder.h:169
unsigned int rc_dropframe_thresh
Temporal resampling configuration, if supported by the codec.
Definition: vpx_encoder.h:394
#define VP8_EFLAG_NO_UPD_LAST
Don't update the last frame.
Definition: vp8cx.h:74
unsigned char * planes[4]
Definition: vpx_image.h:126
#define VPX_DL_REALTIME
Definition: vpx_encoder.h:788
int num
Definition: vpx_encoder.h:214
vpx_codec_err_t vpx_codec_enc_config_default(vpx_codec_iface_t *iface, vpx_codec_enc_cfg_t *cfg, unsigned int usage)
Get a default configuration.
enum vpx_enc_pass g_pass
Multi-pass Encoding Mode.
Definition: vpx_encoder.h:356
const char * vpx_codec_error_detail(vpx_codec_ctx_t *ctx)
Retrieve detailed error information for codec context.
Provides definitions for using the VP8 encoder algorithm within the vpx Codec Interface.
#define vpx_codec_enc_init(ctx, iface, cfg, flags)
Convenience macro for vpx_codec_enc_init_ver()
Definition: vpx_encoder.h:686
unsigned int rc_resize_allowed
Enable/disable spatial resampling, if supported by the codec.
Definition: vpx_encoder.h:404
unsigned int h
Definition: vpx_image.h:103
vpx_codec_err_t
Algorithm return codes.
Definition: vpx_codec.h:81
const vpx_codec_cx_pkt_t * vpx_codec_get_cx_data(vpx_codec_ctx_t *ctx, vpx_codec_iter_t *iter)
Encoded data iterator.
union vpx_codec_cx_pkt::@1 data
Definition: vpx_encoder.h:249
int64_t vpx_codec_pts_t
Time Stamp Type.
Definition: vpx_encoder.h:97
#define vpx_codec_control(ctx, id, data)
vpx_codec_control wrapper macro
Definition: vpx_codec.h:392
unsigned int ts_periodicity
Length of the sequence defining frame layer membership.
Definition: vpx_encoder.h:640
#define VP8_EFLAG_NO_REF_ARF
Don't reference the alternate reference frame.
Definition: vp8cx.h:66
vpx_codec_err_t vpx_codec_destroy(vpx_codec_ctx_t *ctx)
Destroy a codec instance.
unsigned int w
Definition: vpx_image.h:102
const char * vpx_codec_error(vpx_codec_ctx_t *ctx)
Retrieve error synopsis for codec context.
#define VPX_FRAME_IS_KEY
Definition: vpx_encoder.h:108
#define VPX_EFLAG_FORCE_KF
Definition: vpx_encoder.h:261
const void * vpx_codec_iter_t
Iterator.
Definition: vpx_codec.h:182
Definition: vpx_encoder.h:155
vpx_codec_er_flags_t g_error_resilient
Enable error resilient modes.
Definition: vpx_encoder.h:348
#define VP8_EFLAG_NO_UPD_ARF
Don't update the alternate reference frame.
Definition: vp8cx.h:90
#define VP8_EFLAG_NO_UPD_ENTROPY
Disable entropy update.
Definition: vp8cx.h:114
enum vpx_rc_mode rc_end_usage
Rate control algorithm to use.
Definition: vpx_encoder.h:433
Definition: vpx_encoder.h:222
Codec context structure.
Definition: vpx_codec.h:193