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:176
@ AOM_CODEC_STATS_PKT
Definition aom_encoder.h:109
Codec context structure.
Definition aom_codec.h:298
Encoder output packet.
Definition aom_encoder.h:120
enum aom_codec_cx_pkt_kind kind
Definition aom_encoder.h:121
aom_fixed_buf_t twopass_stats
Definition aom_encoder.h:138
union aom_codec_cx_pkt::@1 data
Encoder configuration structure.
Definition aom_encoder.h:385
enum aom_enc_pass g_pass
Multi-pass Encoding Mode.
Definition aom_encoder.h:502
size_t sz
Definition aom_encoder.h:88
void * buf
Definition aom_encoder.h:87
Image Descriptor.
Definition aom_image.h:182
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:179
@ AOM_CODEC_CX_FRAME_PKT
Definition aom_encoder.h:108
size_t sz
Definition aom_encoder.h:125
struct aom_codec_cx_pkt::@1::@2 frame
void * buf
Definition aom_encoder.h:124
◆ has_no_stats_stage()
static int has_no_stats_stage |
( |
const AV1_COMP *const | cpi | ) |
|
|
inlinestatic |
Check if the current stage has statistics.
- Parameters
-
[in] | cpi | Top - level encoder instance structure |
- Returns
- 0 if no stats for current stage else 1
References AOM_RC_ONE_PASS, AV1_COMP::compressor_stage, AV1_PRIMARY::lap_enabled, AV1_COMP::oxcf, AV1EncoderConfig::pass, and AV1_COMP::ppi.
Referenced by av1_encode_strategy(), av1_rc_pick_q_and_bounds(), calculate_gf_length(), define_gf_group(), encode_frame_to_data_rate(), encode_rd_sb(), find_next_key_frame(), rc_pick_q_and_bounds(), rc_pick_q_and_bounds_no_stats(), and rc_pick_q_and_bounds_no_stats_cbr().