这里使用的是AV1(Jun. 8, 2019version)。
aomenc是编码器工程,完成视频序列的编码。
运行时,首先调用aomenc.c中的main函数
main函数中完成的工作主要有初始化,解析输入文件,打印数据,并且对编码过程进行计时,因为AV1是用C编写的,所以有很多地方看上去不是那么简洁。
其中调用了函数encode_frame,是编码器的最上层的入口。
主要过程是:
int main(int argc, const char **argv_) {
int pass;
aom_image_t raw;
aom_image_t raw_shift;
int allocated_raw_shift = 0;
int use_16bit_internal = 0;
int input_shift = 0;
int frame_avail, got_data;
struct AvxInputContext input;
struct AvxEncoderConfig global;
struct stream_state *streams = NULL;
char **argv, **argi;
uint64_t cx_time = 0;
int stream_cnt = 0;
int res = 0;
int profile_updated = 0;
memset(&input, 0, sizeof(input));
exec_name = argv_[0];//exe文件的路径
/* Setup default input stream settings */
input.framerate.numerator = 30;
input.framerate.denominator = 1;
input.only_i420 = 1;
input.bit_depth = 0;
/* First parse the global configuration values, because we want to apply
* other parameters on top of the default configuration provided by the
* codec.
*/
argv = argv_dup(argc - 1, argv_ + 1);//argc为参数个数,将命令参数用新的指针保存下来
parse_global_config(&global, argc, &argv);//遍历命令行参数,存到AvxEncoderConfig结构体里,名字为global
#if CONFIG_FILEOPTIONS
if (argc < 2) usage_exit();
#else
if (argc < 3) usage_exit();
#endif
switch (global.color_type) {
case I420: input.fmt = AOM_IMG_FMT_I420; break;//默认是420格式
case I422: input.fmt = AOM_IMG_FMT_I422; break;
case I444: input.fmt = AOM_IMG_FMT_I444; break;
case YV12: input.fmt = AOM_IMG_FMT_YV12; break;
}
{
/* Now parse each stream's parameters. Using a local scope here
* due to the use of 'stream' as loop variable in FOREACH_STREAM
* loops
*/
struct stream_state *stream = NULL;
do {
stream = new_stream(&global, stream);
stream_cnt++;
if (!streams) streams = stream;
} while (parse_stream_params(&global, stream, argv));
}
/* Check for unrecognized options */
for (argi = argv; *argi; argi++)
if (argi[0][0] == '-' && argi[0][1])
die("Error: Unrecognized option %s\n", *argi);
FOREACH_STREAM(stream, streams) {//FOREACH_STREAM是用于遍历结构体内部的一些变量,用链表完成
check_encoder_config(global.disable_warning_prompt, &global,
&stream->config.cfg);
// If large_scale_tile = 1, only support to output to ivf format.
if (stream->config.cfg.large_scale_tile && !stream->config.write_ivf)
die("only support ivf output format while large-scale-tile=1\n");
}
/* Handle non-option arguments */
input.filename = argv[0];
if (!input.filename) {
fprintf(stderr, "No input file specified!\n");
usage_exit();
}
/* Decide if other chroma subsamplings than 4:2:0 are supported */
if (global.codec->fourcc == AV1_FOURCC) input.only_i420 = 0;
for (pass = global.pass ? global.pass - 1 : 0; pass < global.passes; pass++) {
int frames_in = 0, seen_frames = 0;
int64_t estimated_time_left = -1;
int64_t average_rate = -1;
int64_t lagged_count = 0;
open_input_file(&input, global.csp);//打开输入的yuv文件,input->file是用fopen创建的一个placeholder
/* If the input file doesn't specify its w/h (raw files), try to get
* the data from the first stream's configuration.
*/
if (!input.width || !input.height) {
FOREACH_STREAM(stream, streams) {
if (stream->config.cfg.g_w && stream->config.cfg.g_h) {
input.width = stream->config.cfg.g_w;
input.height = stream->config.cfg.g_h;
break;
}
};
}
/* Update stream configurations from the input file's parameters */
if (!input.width || !input.height)
fatal(
"Specify stream dimensions with --width (-w) "
" and --height (-h)");
/* If input file does not specify bit-depth but input-bit-depth parameter
* exists, assume that to be the input bit-depth. However, if the
* input-bit-depth paramter does not exist, assume the input bit-depth
* to be the same as the codec bit-depth.
*/
if (!input.bit_depth) {
FOREACH_STREAM(stream, streams) {
if (stream->config.cfg.g_input_bit_depth)
input.bit_depth = stream->config.cfg.g_input_bit_depth;
else
input.bit_depth = stream->config.cfg.g_input_bit_depth =
(int)stream->config.cfg.g_bit_depth;
}
if (input.bit_depth > 8) input.fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
} else {
FOREACH_STREAM(stream, streams) {
stream->config.cfg.g_input_bit_depth = input.bit_depth;
}
}
FOREACH_STREAM(stream, streams) {
if (input.fmt != AOM_IMG_FMT_I420 && input.fmt != AOM_IMG_FMT_I42016) {
/* Automatically upgrade if input is non-4:2:0 but a 4:2:0 profile
was selected. */
switch (stream->config.cfg.g_profile) {
case 0:
if (input.bit_depth < 12 && (input.fmt == AOM_IMG_FMT_I444 ||
input.fmt == AOM_IMG_FMT_I44416)) {
if (!stream->config.cfg.monochrome) {
stream->config.cfg.g_profile = 1;
profile_updated = 1;
}
} else if (input.bit_depth == 12 || input.fmt == AOM_IMG_FMT_I422 ||
input.fmt == AOM_IMG_FMT_I42216) {
stream->config.cfg.g_profile = 2;
profile_updated = 1;
}
break;
case 1:
if (input.bit_depth == 12 || input.fmt == AOM_IMG_FMT_I422 ||
input.fmt == AOM_IMG_FMT_I42216) {
stream->config.cfg.g_profile = 2;
profile_updated = 1;
} else if (input.bit_depth < 12 &&
(input.fmt == AOM_IMG_FMT_I420 ||
input.fmt == AOM_IMG_FMT_I42016)) {
stream->config.cfg.g_profile = 0;
profile_updated = 1;
}
break;
case 2:
if (input.bit_depth < 12 && (input.fmt == AOM_IMG_FMT_I444 ||
input.fmt == AOM_IMG_FMT_I44416)) {
stream->config.cfg.g_profile = 1;
profile_updated = 1;
} else if (input.bit_depth < 12 &&
(input.fmt == AOM_IMG_FMT_I420 ||
input.fmt == AOM_IMG_FMT_I42016)) {
stream->config.cfg.g_profile = 0;
profile_updated = 1;
} else if (input.bit_depth == 12 &&
input.file_type == FILE_TYPE_Y4M) {
// Note that here the input file values for chroma subsampling
// are used instead of those from the command line.
aom_codec_control(&stream->encoder, AV1E_SET_CHROMA_SUBSAMPLING_X,
input.y4m.dst_c_dec_h >> 1);
aom_codec_control(&stream->encoder, AV1E_SET_CHROMA_SUBSAMPLING_Y,
input.y4m.dst_c_dec_v >> 1);
} else if (input.bit_depth == 12 &&
input.file_type == FILE_TYPE_RAW) {
aom_codec_control(&stream->encoder, AV1E_SET_CHROMA_SUBSAMPLING_X,
stream->chroma_subsampling_x);
aom_codec_control(&stream->encoder, AV1E_SET_CHROMA_SUBSAMPLING_Y,
stream->chroma_subsampling_y);
}
break;
default: break;
}
}
/* Automatically set the codec bit depth to match the input bit depth.
* Upgrade the profile if required. */
if (stream->config.cfg.g_input_bit_depth >
(unsigned int)stream->config.cfg.g_bit_depth) {
stream->config.cfg.g_bit_depth = stream->config.cfg.g_input_bit_depth;
if (!global.quiet) {
fprintf(stderr,
"Warning: automatically updating bit depth to %d to "
"match input format.\n",
stream->config.cfg.g_input_bit_depth);
}
}
if (stream->config.cfg.g_bit_depth > 10) {
switch (stream->config.cfg.g_profile) {
case 0:
case 1:
stream->config.cfg.g_profile = 2;
profile_updated = 1;
break;
default: break;
}
}
if (stream->config.cfg.g_bit_depth > 8) {
stream->config.use_16bit_internal = 1;
}
if (profile_updated && !global.quiet) {
fprintf(stderr,
"Warning: automatically updating to profile %d to "
"match input format.\n",
stream->config.cfg.g_profile);
}
/* Set limit */
stream->config.cfg.g_limit = global.limit;
}
FOREACH_STREAM(stream, streams) {
set_stream_dimensions(stream, input.width, input.height);
}
FOREACH_STREAM(stream, streams) { validate_stream_config(stream, &global); }
/* Ensure that --passes and --pass are consistent. If --pass is set and
* --passes=2, ensure --fpf was set.
*/
if (global.pass && global.passes == 2) {
FOREACH_STREAM(stream, streams) {
if (!stream->config.stats_fn)
die("Stream %d: Must specify --fpf when --pass=%d"
" and --passes=2\n",
stream->index, global.pass);
}
}
#if !CONFIG_WEBM_IO
FOREACH_STREAM(stream, streams) {
if (stream->config.write_webm) {
stream->config.write_webm = 0;
stream->config.write_ivf = 0;
warn("aomenc compiled w/o WebM support. Writing OBU stream.");
}
}
#endif
/* Use the frame rate from the file only if none was specified
* on the command-line.
*/
if (!global.have_framerate) {
global.framerate.num = input.framerate.numerator;
global.framerate.den = input.framerate.denominator;
}
FOREACH_STREAM(stream, streams) {
stream->config.cfg.g_timebase.den = global.framerate.num;
stream->config.cfg.g_timebase.num = global.framerate.den;
}
/* Show configuration */
if (global.verbose && pass == 0) {
FOREACH_STREAM(stream, streams) {
show_stream_config(stream, &global, &input);//把命令行和默认的参数print出来
}
}
if (pass == (global.pass ? global.pass - 1 : 0)) {
if (input.file_type == FILE_TYPE_Y4M)
/*The Y4M reader does its own allocation.
Just initialize this here to avoid problems if we never read any
frames.*/
memset(&raw, 0, sizeof(raw));
else
aom_img_alloc(&raw, input.fmt, input.width, input.height, 32);//初始化原始帧的数据
FOREACH_STREAM(stream, streams) {
stream->rate_hist =
init_rate_histogram(&stream->config.cfg, &global.framerate);
}
}
FOREACH_STREAM(stream, streams) { setup_pass(stream, &global, pass); }//初始化pass的数据
FOREACH_STREAM(stream, streams) { initialize_encoder(stream, &global); }//检查一些开关是否开启
FOREACH_STREAM(stream, streams) {
open_output_file(stream, &global, &input.pixel_aspect_ratio);
}
if (strcmp(global.codec->name, "av1") == 0 ||
strcmp(global.codec->name, "av1") == 0) {
// Check to see if at least one stream uses 16 bit internal.
// Currently assume that the bit_depths for all streams using
// highbitdepth are the same.
FOREACH_STREAM(stream, streams) {
if (stream->config.use_16bit_internal) {
use_16bit_internal = 1;
}
input_shift = (int)stream->config.cfg.g_bit_depth -
stream->config.cfg.g_input_bit_depth;
};
}
frame_avail = 1;
got_data = 0;
//主要编码循环
while (frame_avail || got_data) {
struct aom_usec_timer timer;
if (!global.limit || frames_in < global.limit) {
frame_avail = read_frame(&input, &raw);//判断是否还有帧要缓冲,有的话缓冲并将frame_avail置为1
if (frame_avail) frames_in++;
seen_frames =
frames_in > global.skip_frames ? frames_in - global.skip_frames : 0;
if (!global.quiet) {
float fps = usec_to_fps(cx_time, seen_frames);
fprintf(stderr, "\rPass %d/%d ", pass + 1, global.passes);
if (stream_cnt == 1)
fprintf(stderr, "frame %4d/%-4d %7" PRId64 "B ", frames_in,
streams->frames_out, (int64_t)streams->nbytes);
else
fprintf(stderr, "frame %4d ", frames_in);
fprintf(stderr, "%7" PRId64 " %s %.2f %s ",
cx_time > 9999999 ? cx_time / 1000 : cx_time,
cx_time > 9999999 ? "ms" : "us", fps >= 1.0 ? fps : fps * 60,
fps >= 1.0 ? "fps" : "fpm");
print_time("ETA", estimated_time_left);
}
} else {
frame_avail = 0;
}
if (frames_in > global.skip_frames) {
aom_image_t *frame_to_encode;
if (input_shift || (use_16bit_internal && input.bit_depth == 8)) {
assert(use_16bit_internal);
// Input bit depth and stream bit depth do not match, so up
// shift frame to stream bit depth
if (!allocated_raw_shift) {
aom_img_alloc(&raw_shift, raw.fmt | AOM_IMG_FMT_HIGHBITDEPTH,
input.width, input.height, 32);
allocated_raw_shift = 1;
}
aom_img_upshift(&raw_shift, &raw, input_shift);
frame_to_encode = &raw_shift;
} else {
frame_to_encode = &raw;//把要编码的帧赋给frame_to_encode
}
aom_usec_timer_start(&timer);
if (use_16bit_internal) {
assert(frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH);
FOREACH_STREAM(stream, streams) {
if (stream->config.use_16bit_internal)
encode_frame(stream, &global,
frame_avail ? frame_to_encode : NULL, frames_in);
else
assert(0);
};
} else {
assert((frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH) == 0);
FOREACH_STREAM(stream, streams) {//8bit的编码循环
encode_frame(stream, &global, frame_avail ? frame_to_encode : NULL,
frames_in);
}
}
aom_usec_timer_mark(&timer);//更新计时器
cx_time += aom_usec_timer_elapsed(&timer);
FOREACH_STREAM(stream, streams) { update_quantizer_histogram(stream); }
got_data = 0;
FOREACH_STREAM(stream, streams) {
get_cx_data(stream, &global, &got_data);
}
if (!got_data && input.length && streams != NULL &&
!streams->frames_out) {
lagged_count = global.limit ? seen_frames : ftello(input.file);
} else if (input.length) {
int64_t remaining;
int64_t rate;
if (global.limit) {
const int64_t frame_in_lagged = (seen_frames - lagged_count) * 1000;
rate = cx_time ? frame_in_lagged * (int64_t)1000000 / cx_time : 0;
remaining = 1000 * (global.limit - global.skip_frames -
seen_frames + lagged_count);
} else {
const int64_t input_pos = ftello(input.file);
const int64_t input_pos_lagged = input_pos - lagged_count;
const int64_t input_limit = input.length;
rate = cx_time ? input_pos_lagged * (int64_t)1000000 / cx_time : 0;
remaining = input_limit - input_pos + lagged_count;
}
average_rate =
(average_rate <= 0) ? rate : (average_rate * 7 + rate) / 8;
estimated_time_left = average_rate ? remaining / average_rate : -1;
}
if (got_data && global.test_decode != TEST_DECODE_OFF) {
FOREACH_STREAM(stream, streams) {
test_decode(stream, global.test_decode);
}
}
}
fflush(stdout);//到这一次编码就结束了
if (!global.quiet) fprintf(stderr, "\033[K");
}
if (stream_cnt > 1) fprintf(stderr, "\n");
if (!global.quiet) {
FOREACH_STREAM(stream, streams) {
const int64_t bpf =
seen_frames ? (int64_t)(stream->nbytes * 8 / seen_frames) : 0;
const int64_t bps = bpf * global.framerate.num / global.framerate.den;
fprintf(stderr,
"\rPass %d/%d frame %4d/%-4d %7" PRId64 "B %7" PRId64
"b/f %7" PRId64
"b/s"
" %7" PRId64 " %s (%.2f fps)\033[K\n",
pass + 1, global.passes, frames_in, stream->frames_out,
(int64_t)stream->nbytes, bpf, bps,
stream->cx_time > 9999999 ? stream->cx_time / 1000
: stream->cx_time,
stream->cx_time > 9999999 ? "ms" : "us",
usec_to_fps(stream->cx_time, seen_frames));
}
}
if (global.show_psnr) {
if (global.codec->fourcc == AV1_FOURCC) {
FOREACH_STREAM(stream, streams) {
int64_t bps = 0;
if (stream->psnr_count && seen_frames && global.framerate.den) {
bps = (int64_t)stream->nbytes * 8 * (int64_t)global.framerate.num /
global.framerate.den / seen_frames;
}
show_psnr(stream, (1 << stream->config.cfg.g_input_bit_depth) - 1,
bps);
}
} else {
FOREACH_STREAM(stream, streams) { show_psnr(stream, 255.0, 0); }
}
}
FOREACH_STREAM(stream, streams) { aom_codec_destroy(&stream->encoder); }
if (global.test_decode != TEST_DECODE_OFF) {
FOREACH_STREAM(stream, streams) { aom_codec_destroy(&stream->decoder); }
}
close_input_file(&input);
if (global.test_decode == TEST_DECODE_FATAL) {
FOREACH_STREAM(stream, streams) { res |= stream->mismatch_seen; }
}
FOREACH_STREAM(stream, streams) {
close_output_file(stream, global.codec->fourcc);
}
FOREACH_STREAM(stream, streams) {
stats_close(&stream->stats, global.passes - 1);
}
if (global.pass) break;
}
if (global.show_q_hist_buckets) {
FOREACH_STREAM(stream, streams) {
show_q_histogram(stream->counts, global.show_q_hist_buckets);
}
}
if (global.show_rate_hist_buckets) {
FOREACH_STREAM(stream, streams) {
show_rate_histogram(stream->rate_hist, &stream->config.cfg,
global.show_rate_hist_buckets);
}
}
FOREACH_STREAM(stream, streams) { destroy_rate_histogram(stream->rate_hist); }
#if CONFIG_INTERNAL_STATS
/* TODO(jkoleszar): This doesn't belong in this executable. Do it for now,
* to match some existing utilities.
*/
if (!(global.pass == 1 && global.passes == 2)) {
FOREACH_STREAM(stream, streams) {
FILE *f = fopen("opsnr.stt", "a");
if (stream->mismatch_seen) {
fprintf(f, "First mismatch occurred in frame %d\n",
stream->mismatch_seen);
} else {
fprintf(f, "No mismatch detected in recon buffers\n");
}
fclose(f);
}
}
#endif
if (allocated_raw_shift) aom_img_free(&raw_shift);
aom_img_free(&raw);
free(argv);
free(streams);
return res ? EXIT_FAILURE : EXIT_SUCCESS;
}
来源:CSDN
作者:Alanqin13
链接:https://blog.csdn.net/qq_30945147/article/details/103424930