In two pass mode, the input file is passed into the encoder for a quick first pass, where statistics are gathered. These statistics and the input file are then passed back into the encoder for a second pass. The statistics help the encoder reach the desired bitrate without as much overshooting or undershooting.
During the first pass, the codec will return "stats" packets that contain information useful for the second pass. The caller should concatenate these packets as they are received. In the second pass, the concatenated packets are passed in, along with the frames to encode. During the second pass, "frame" packets are returned that represent the compressed video.
A complete example can be found in examples/twopass_encoder.c
. Pseudocode is provided below to illustrate the core parts.
During the first pass, the uncompressed frames are passed in and stats information is appended to a byte array.
size_t *stats_len, bool *got_data) {
*got_data = true;
}
}
void first_pass(char *stats, size_t *stats_len) {
...
...
while (frame_available) {
get_stats_data(&first_pass_encoder, stats, stats_len);
}
bool got_data;
do {
got_data = false;
get_stats_data(&first_pass_encoder, stats, stats_len, &got_data);
} while (got_data);
}
aom_codec_err_t aom_codec_destroy(aom_codec_ctx_t *ctx)
Destroy a codec instance.
const void * aom_codec_iter_t
Iterator.
Definition aom_codec.h:288
const aom_codec_cx_pkt_t * aom_codec_get_cx_data(aom_codec_ctx_t *ctx, aom_codec_iter_t *iter)
Encoded data iterator.
aom_codec_err_t aom_codec_encode(aom_codec_ctx_t *ctx, const aom_image_t *img, aom_codec_pts_t pts, unsigned long duration, aom_enc_frame_flags_t flags)
Encode a frame.
@ AOM_RC_FIRST_PASS
Definition aom_encoder.h:167
@ AOM_CODEC_STATS_PKT
Definition aom_encoder.h:100
Codec context structure.
Definition aom_codec.h:298
Encoder output packet.
Definition aom_encoder.h:111
enum aom_codec_cx_pkt_kind kind
Definition aom_encoder.h:112
aom_fixed_buf_t twopass_stats
Definition aom_encoder.h:129
union aom_codec_cx_pkt::@1 data
Encoder configuration structure.
Definition aom_encoder.h:376
enum aom_enc_pass g_pass
Multi-pass Encoding Mode.
Definition aom_encoder.h:488
size_t sz
Definition aom_encoder.h:79
void * buf
Definition aom_encoder.h:78
Image Descriptor.
Definition aom_image.h:171
During the second pass, the uncompressed frames and the stats are passed into the encoder.
bool *got_data) {
*got_data = true;
}
}
void second_pass(char *stats, size_t stats_len) {
...
cfg.rc_twopass_stats_in.buf = stats;
cfg.rc_twopass_stats_in.sz = stats_len;
...
FILE *output = fopen("output.obu", "wb");
while (frame_available) {
get_cx_data(&second_pass_encoder, output);
}
bool got_data;
do {
got_data = false;
get_cx_data(&second_pass_encoder, output, &got_data);
} while (got_data);
}
@ AOM_RC_LAST_PASS
Definition aom_encoder.h:170
@ AOM_CODEC_CX_FRAME_PKT
Definition aom_encoder.h:99
size_t sz
Definition aom_encoder.h:116
struct aom_codec_cx_pkt::@1::@2 frame
void * buf
Definition aom_encoder.h:115