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 (void) 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 (void) fwrite(header, 1, 12, outfile);
117 static int mode_to_num_layers[12] = {1, 2, 2, 3, 3, 3, 3, 5, 2, 3, 3, 3};
119 int main(
int argc,
char **argv) {
133 int frame_duration = 1;
135 int layering_mode = 0;
138 int flag_periodicity;
139 int max_intra_size_pct;
143 die(
"Usage: %s <infile> <outfile> <width> <height> <rate_num> "
144 " <rate_den> <mode> <Rate_0> ... <Rate_nlayers-1>\n", argv[0]);
146 width = strtol (argv[3], NULL, 0);
147 height = strtol (argv[4], NULL, 0);
148 if (width < 16 || width%2 || height <16 || height%2)
149 die (
"Invalid resolution: %d x %d", width, height);
151 if (!sscanf(argv[7],
"%d", &layering_mode))
152 die (
"Invalid mode %s", argv[7]);
153 if (layering_mode<0 || layering_mode>11)
154 die (
"Invalid mode (0..11) %s", argv[7]);
156 if (argc != 8+mode_to_num_layers[layering_mode])
157 die (
"Invalid number of arguments");
160 die (
"Failed to allocate image", width, height);
177 die (
"Invalid timebase numerator %s", argv[5]);
179 die (
"Invalid timebase denominator %s", argv[6]);
181 for (i=8; i<8+mode_to_num_layers[layering_mode]; i++)
183 die (
"Invalid data rate %s", argv[i]);
214 switch (layering_mode)
264 int ids[3] = {0,1,1};
287 int ids[6] = {0,2,2,1,2,2};
313 int ids[4] = {0,2,1,2};
340 int ids[4] = {0,2,1,2};
368 int ids[4] = {0,2,1,2};
394 int ids[16] = {0,4,3,4,2,4,3,4,1,4,3,4,2,4,3,4};
438 flag_periodicity = 8;
465 layer_flags[4] = layer_flags[2];
468 layer_flags[5] = layer_flags[3];
471 layer_flags[6] = layer_flags[4];
474 layer_flags[7] = layer_flags[5];
483 int ids[4] = {0,2,1,2};
491 flag_periodicity = 8;
518 int ids[4] = {0,2,1,2};
526 flag_periodicity = 8;
559 layer_flags[5] = layer_flags[3];
566 layer_flags[7] = layer_flags[3];
575 int ids[4] = {0,2,1,2};
583 flag_periodicity = 8;
591 layer_flags[4] = layer_flags[0];
596 layer_flags[6] = layer_flags[2];
603 layer_flags[3] = layer_flags[1];
604 layer_flags[5] = layer_flags[1];
605 layer_flags[7] = layer_flags[1];
611 if(!(infile = fopen(argv[1],
"rb")))
612 die(
"Failed to open %s for reading", argv[1]);
618 sprintf (file_name,
"%s_%d.ivf", argv[2], i);
619 if (!(outfile[i] = fopen(file_name,
"wb")))
620 die(
"Failed to open %s for writing", file_name);
621 write_ivf_file_header(outfile[i], &cfg, 0);
626 die_codec (&codec,
"Failed to initialize encoder");
643 while (frame_avail || got_data) {
647 flags = layer_flags[frame_cnt % flag_periodicity];
649 frame_avail = read_frame(infile, &raw);
652 die_codec(&codec,
"Failed to encode frame");
655 if (layering_mode != 7)
666 write_ivf_frame_header(outfile[i], pkt);
669 frames_in_layer[i]++;
677 pts += frame_duration;
681 printf (
"Processed %d frames.\n",frame_cnt-1);
683 die_codec (&codec,
"Failed to destroy codec");
688 if (!fseek(outfile[i], 0, SEEK_SET))
689 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:544
unsigned int ts_number_layers
Number of coding layers.
Definition: vpx_encoder.h:629
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.
#define VPX_TS_MAX_LAYERS
Definition: vpx_encoder.h:41
struct vpx_rational g_timebase
Stream timebase units.
Definition: vpx_encoder.h:350
Definition: vpx_encoder.h:243
unsigned int rc_buf_sz
Decoder Buffer Size.
Definition: vpx_encoder.h:534
#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:599
int den
Definition: vpx_encoder.h:226
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:486
unsigned int rc_min_quantizer
Minimum (Best Quality) Quantizer.
Definition: vpx_encoder.h:475
unsigned int kf_max_dist
Keyframe maximum interval.
Definition: vpx_encoder.h:619
unsigned int g_lag_in_frames
Allow lagged encoding.
Definition: vpx_encoder.h:382
Encoder configuration structure.
Definition: vpx_encoder.h:281
Max data rate for Intra frames.
Definition: vp8cx.h:180
Encoder output packet.
Definition: vpx_encoder.h:178
unsigned int rc_overshoot_pct
Rate control adaptation overshoot control.
Definition: vpx_encoder.h:517
unsigned int ts_rate_decimator[5]
Frame rate decimation factor for each layer.
Definition: vpx_encoder.h:642
unsigned int rc_buf_optimal_sz
Decoder Buffer Optimal Size.
Definition: vpx_encoder.h:554
unsigned int kf_min_dist
Keyframe minimum interval.
Definition: vpx_encoder.h:609
Definition: vpx_encoder.h:235
unsigned int ts_layer_id[16]
Template defining the membership of frames to coding layers.
Definition: vpx_encoder.h:660
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:325
unsigned int ts_target_bitrate[5]
Target bitrate for each layer.
Definition: vpx_encoder.h:635
unsigned int rc_undershoot_pct
Rate control adaptation undershoot control.
Definition: vpx_encoder.h:504
unsigned int g_h
Height of the frame.
Definition: vpx_encoder.h:335
enum vpx_codec_cx_pkt_kind kind
Definition: vpx_encoder.h:180
unsigned int rc_dropframe_thresh
Temporal resampling configuration, if supported by the codec.
Definition: vpx_encoder.h:405
#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
unsigned int rc_target_bitrate
Target data rate.
Definition: vpx_encoder.h:459
#define VPX_DL_REALTIME
Definition: vpx_encoder.h:803
int num
Definition: vpx_encoder.h:225
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:367
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:701
unsigned int rc_resize_allowed
Enable/disable spatial resampling, if supported by the codec.
Definition: vpx_encoder.h:415
unsigned int h
Definition: vpx_image.h:103
vpx_codec_err_t
Algorithm return codes.
Definition: vpx_codec.h:88
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:260
int64_t vpx_codec_pts_t
Time Stamp Type.
Definition: vpx_encoder.h:108
#define VPX_TS_MAX_PERIODICITY
Definition: vpx_encoder.h:38
#define vpx_codec_control(ctx, id, data)
vpx_codec_control wrapper macro
Definition: vpx_codec.h:399
unsigned int ts_periodicity
Length of the sequence defining frame layer membership.
Definition: vpx_encoder.h:651
#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_EFLAG_FORCE_KF
Definition: vpx_encoder.h:272
const void * vpx_codec_iter_t
Iterator.
Definition: vpx_codec.h:189
Definition: vpx_encoder.h:166
vpx_codec_er_flags_t g_error_resilient
Enable error resilient modes.
Definition: vpx_encoder.h:359
#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:444
Definition: vpx_encoder.h:233
Codec context structure.
Definition: vpx_codec.h:200