static gboolean gst_av1_enc_set_format (GstVideoEncoder * encoder, GstVideoCodecState * state) { GstVideoCodecState *output_state; GstAV1Enc *av1enc = GST_AV1_ENC_CAST (encoder); GstAV1EncClass *av1enc_class = GST_AV1_ENC_GET_CLASS (av1enc); output_state = gst_video_encoder_set_output_state (encoder, gst_pad_get_pad_template_caps (GST_VIDEO_ENCODER_SRC_PAD (encoder)), state); gst_video_codec_state_unref (output_state); if (av1enc->input_state) { gst_video_codec_state_unref (av1enc->input_state); } av1enc->input_state = gst_video_codec_state_ref (state); g_mutex_lock (&av1enc->encoder_lock); if (aom_codec_enc_config_default (av1enc_class->codec_algo, &av1enc->aom_cfg, 0)) { gst_av1_codec_error (&av1enc->encoder, "Failed to get default codec config."); return FALSE; } GST_DEBUG_OBJECT (av1enc, "Got default encoder config"); gst_av1_enc_debug_encoder_cfg (&av1enc->aom_cfg); gst_av1_enc_set_latency (av1enc); av1enc->aom_cfg.g_w = av1enc->input_state->info.width; av1enc->aom_cfg.g_h = av1enc->input_state->info.height; av1enc->aom_cfg.g_timebase.num = av1enc->input_state->info.fps_d; av1enc->aom_cfg.g_timebase.den = av1enc->input_state->info.fps_n; /* FIXME : Make configuration properties */ av1enc->aom_cfg.rc_target_bitrate = 3000; av1enc->aom_cfg.g_error_resilient = AOM_ERROR_RESILIENT_DEFAULT; GST_DEBUG_OBJECT (av1enc, "Calling encoder init with config:"); gst_av1_enc_debug_encoder_cfg (&av1enc->aom_cfg); if (aom_codec_enc_init (&av1enc->encoder, av1enc_class->codec_algo, &av1enc->aom_cfg, 0)) { gst_av1_codec_error (&av1enc->encoder, "Failed to initialize encoder"); return FALSE; } av1enc->encoder_inited = TRUE; GST_AV1_ENC_APPLY_CODEC_CONTROL (av1enc, AOME_SET_CPUUSED, av1enc->cpu_used); g_mutex_unlock (&av1enc->encoder_lock); return TRUE; }
static void pass1(aom_image_t *raw, FILE *infile, const char *outfile_name, const AvxInterface *encoder, aom_codec_enc_cfg_t *cfg, int lf_width, int lf_height, int lf_blocksize, int flags, aom_image_t *raw_shift) { AvxVideoInfo info = { encoder->fourcc, cfg->g_w, cfg->g_h, { cfg->g_timebase.num, cfg->g_timebase.den }, 0 }; AvxVideoWriter *writer = NULL; aom_codec_ctx_t codec; int frame_count = 0; int image_size_bytes = aom_img_size_bytes(raw); int bu, bv; int u_blocks, v_blocks; aom_image_t *frame_to_encode; aom_image_t reference_images[MAX_EXTERNAL_REFERENCES]; int reference_image_num = 0; int i; writer = aom_video_writer_open(outfile_name, kContainerIVF, &info); if (!writer) die("Failed to open %s for writing", outfile_name); if (aom_codec_enc_init(&codec, encoder->codec_interface(), cfg, flags)) die_codec(&codec, "Failed to initialize encoder"); if (aom_codec_control(&codec, AOME_SET_ENABLEAUTOALTREF, 0)) die_codec(&codec, "Failed to turn off auto altref"); if (aom_codec_control(&codec, AV1E_SET_FRAME_PARALLEL_DECODING, 0)) die_codec(&codec, "Failed to set frame parallel decoding"); // Note: The superblock is a sequence parameter and has to be the same for 1 // sequence. In lightfield application, must choose the superblock size(either // 64x64 or 128x128) before the encoding starts. Otherwise, the default is // AOM_SUPERBLOCK_SIZE_DYNAMIC, and the superblock size will be set to 64x64 // internally. if (aom_codec_control(&codec, AV1E_SET_SUPERBLOCK_SIZE, AOM_SUPERBLOCK_SIZE_64X64)) die_codec(&codec, "Failed to set SB size"); u_blocks = (lf_width + lf_blocksize - 1) / lf_blocksize; v_blocks = (lf_height + lf_blocksize - 1) / lf_blocksize; reference_image_num = u_blocks * v_blocks; aom_img_fmt_t ref_fmt = AOM_IMG_FMT_I420; if (!CONFIG_LOWBITDEPTH) ref_fmt |= AOM_IMG_FMT_HIGHBITDEPTH; // Allocate memory with the border so that it can be used as a reference. for (i = 0; i < reference_image_num; i++) { if (!aom_img_alloc_with_border(&reference_images[i], ref_fmt, cfg->g_w, cfg->g_h, 32, 8, AOM_BORDER_IN_PIXELS)) { die("Failed to allocate image."); } } printf("\n Second pass: "******"Encoding Reference Images\n"); for (bv = 0; bv < v_blocks; ++bv) { for (bu = 0; bu < u_blocks; ++bu) { const int block_u_min = bu * lf_blocksize; const int block_v_min = bv * lf_blocksize; int block_u_end = (bu + 1) * lf_blocksize; int block_v_end = (bv + 1) * lf_blocksize; int u_block_size, v_block_size; int block_ref_u, block_ref_v; block_u_end = block_u_end < lf_width ? block_u_end : lf_width; block_v_end = block_v_end < lf_height ? block_v_end : lf_height; u_block_size = block_u_end - block_u_min; v_block_size = block_v_end - block_v_min; block_ref_u = block_u_min + u_block_size / 2; block_ref_v = block_v_min + v_block_size / 2; printf("A%d, ", (block_ref_u + block_ref_v * lf_width)); fseek(infile, (block_ref_u + block_ref_v * lf_width) * image_size_bytes, SEEK_SET); aom_img_read(raw, infile); get_raw_image(&frame_to_encode, raw, raw_shift); // Reference frames may be encoded without tiles. ++frame_count; printf("Encoding reference image %d of %d\n", bv * u_blocks + bu, u_blocks * v_blocks); encode_frame(&codec, frame_to_encode, frame_count, 1, AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 | AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_BWD | AOM_EFLAG_NO_REF_ARF2 | AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF | AOM_EFLAG_NO_UPD_ENTROPY, writer); if (aom_codec_control(&codec, AV1_COPY_NEW_FRAME_IMAGE, &reference_images[frame_count - 1])) die_codec(&codec, "Failed to copy decoder reference frame"); } } cfg->large_scale_tile = 1; // Fixed q encoding for camera frames. cfg->rc_end_usage = AOM_Q; if (aom_codec_enc_config_set(&codec, cfg)) die_codec(&codec, "Failed to configure encoder"); // The fixed q value used in encoding. if (aom_codec_control(&codec, AOME_SET_CQ_LEVEL, 36)) die_codec(&codec, "Failed to set cq level"); if (aom_codec_control(&codec, AV1E_SET_FRAME_PARALLEL_DECODING, 1)) die_codec(&codec, "Failed to set frame parallel decoding"); if (aom_codec_control(&codec, AV1E_SET_SINGLE_TILE_DECODING, 1)) die_codec(&codec, "Failed to turn on single tile decoding"); // Set tile_columns and tile_rows to MAX values, which guarantees the tile // size of 64 x 64 pixels(i.e. 1 SB) for <= 4k resolution. if (aom_codec_control(&codec, AV1E_SET_TILE_COLUMNS, 6)) die_codec(&codec, "Failed to set tile width"); if (aom_codec_control(&codec, AV1E_SET_TILE_ROWS, 6)) die_codec(&codec, "Failed to set tile height"); for (bv = 0; bv < v_blocks; ++bv) { for (bu = 0; bu < u_blocks; ++bu) { const int block_u_min = bu * lf_blocksize; const int block_v_min = bv * lf_blocksize; int block_u_end = (bu + 1) * lf_blocksize; int block_v_end = (bv + 1) * lf_blocksize; int u, v; block_u_end = block_u_end < lf_width ? block_u_end : lf_width; block_v_end = block_v_end < lf_height ? block_v_end : lf_height; for (v = block_v_min; v < block_v_end; ++v) { for (u = block_u_min; u < block_u_end; ++u) { av1_ref_frame_t ref; ref.idx = 0; ref.use_external_ref = 1; ref.img = reference_images[bv * u_blocks + bu]; if (aom_codec_control(&codec, AV1_SET_REFERENCE, &ref)) die_codec(&codec, "Failed to set reference frame"); printf("C%d, ", (u + v * lf_width)); fseek(infile, (u + v * lf_width) * image_size_bytes, SEEK_SET); aom_img_read(raw, infile); get_raw_image(&frame_to_encode, raw, raw_shift); ++frame_count; printf("Encoding image %d of %d\n", frame_count - (u_blocks * v_blocks), lf_width * lf_height); encode_frame(&codec, frame_to_encode, frame_count, 1, AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 | AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_BWD | AOM_EFLAG_NO_REF_ARF2 | AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF | AOM_EFLAG_NO_UPD_ENTROPY, writer); } } } } // Flush encoder. // No ARF, this should not be needed. while (encode_frame(&codec, NULL, -1, 1, 0, writer)) { } for (i = 0; i < reference_image_num; i++) aom_img_free(&reference_images[i]); if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec."); aom_video_writer_close(writer); printf("\nSecond pass complete. Processed %d frames.\n", frame_count); }
static aom_fixed_buf_t pass0(aom_image_t *raw, FILE *infile, const AvxInterface *encoder, const aom_codec_enc_cfg_t *cfg, int lf_width, int lf_height, int lf_blocksize, int flags, aom_image_t *raw_shift) { aom_codec_ctx_t codec; int frame_count = 0; int image_size_bytes = aom_img_size_bytes(raw); int u_blocks, v_blocks; int bu, bv; aom_fixed_buf_t stats = { NULL, 0 }; aom_image_t *frame_to_encode; if (aom_codec_enc_init(&codec, encoder->codec_interface(), cfg, flags)) die_codec(&codec, "Failed to initialize encoder"); if (aom_codec_control(&codec, AOME_SET_ENABLEAUTOALTREF, 0)) die_codec(&codec, "Failed to turn off auto altref"); if (aom_codec_control(&codec, AV1E_SET_FRAME_PARALLEL_DECODING, 0)) die_codec(&codec, "Failed to set frame parallel decoding"); // How many reference images we need to encode. u_blocks = (lf_width + lf_blocksize - 1) / lf_blocksize; v_blocks = (lf_height + lf_blocksize - 1) / lf_blocksize; printf("\n First pass: "******"A%d, ", (block_ref_u + block_ref_v * lf_width)); fseek(infile, (block_ref_u + block_ref_v * lf_width) * image_size_bytes, SEEK_SET); aom_img_read(raw, infile); get_raw_image(&frame_to_encode, raw, raw_shift); // Reference frames can be encoded encoded without tiles. ++frame_count; get_frame_stats(&codec, frame_to_encode, frame_count, 1, AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 | AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_BWD | AOM_EFLAG_NO_REF_ARF2 | AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF, &stats); } } if (aom_codec_control(&codec, AV1E_SET_FRAME_PARALLEL_DECODING, 1)) die_codec(&codec, "Failed to set frame parallel decoding"); for (bv = 0; bv < v_blocks; ++bv) { for (bu = 0; bu < u_blocks; ++bu) { const int block_u_min = bu * lf_blocksize; const int block_v_min = bv * lf_blocksize; int block_u_end = (bu + 1) * lf_blocksize; int block_v_end = (bv + 1) * lf_blocksize; int u, v; block_u_end = block_u_end < lf_width ? block_u_end : lf_width; block_v_end = block_v_end < lf_height ? block_v_end : lf_height; for (v = block_v_min; v < block_v_end; ++v) { for (u = block_u_min; u < block_u_end; ++u) { printf("C%d, ", (u + v * lf_width)); fseek(infile, (u + v * lf_width) * image_size_bytes, SEEK_SET); aom_img_read(raw, infile); get_raw_image(&frame_to_encode, raw, raw_shift); ++frame_count; get_frame_stats(&codec, frame_to_encode, frame_count, 1, AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 | AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_BWD | AOM_EFLAG_NO_REF_ARF2 | AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF | AOM_EFLAG_NO_UPD_ENTROPY, &stats); } } } } // Flush encoder. // No ARF, this should not be needed. while (get_frame_stats(&codec, NULL, frame_count, 1, 0, &stats)) { } if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec."); printf("\nFirst pass complete. Processed %d frames.\n", frame_count); return stats; }
// TODO(tomfinegan): Improve command line parsing and add args for bitrate/fps. int main(int argc, char **argv) { FILE *infile = NULL; aom_codec_ctx_t codec; aom_codec_enc_cfg_t cfg; int frame_count = 0; aom_image_t raw; aom_codec_err_t res; AvxVideoInfo info; AvxVideoWriter *writer = NULL; const AvxInterface *encoder = NULL; const int fps = 30; const int bitrate = 200; int keyframe_interval = 0; int max_frames = 0; int frames_encoded = 0; const char *codec_arg = NULL; const char *width_arg = NULL; const char *height_arg = NULL; const char *infile_arg = NULL; const char *outfile_arg = NULL; const char *keyframe_interval_arg = NULL; exec_name = argv[0]; // Clear explicitly, as simply assigning "{ 0 }" generates // "missing-field-initializers" warning in some compilers. memset(&info, 0, sizeof(info)); if (argc != 9) die("Invalid number of arguments"); codec_arg = argv[1]; width_arg = argv[2]; height_arg = argv[3]; infile_arg = argv[4]; outfile_arg = argv[5]; keyframe_interval_arg = argv[6]; max_frames = (int)strtol(argv[8], NULL, 0); encoder = get_aom_encoder_by_name(codec_arg); if (!encoder) die("Unsupported codec."); info.codec_fourcc = encoder->fourcc; info.frame_width = (int)strtol(width_arg, NULL, 0); info.frame_height = (int)strtol(height_arg, NULL, 0); info.time_base.numerator = 1; info.time_base.denominator = fps; if (info.frame_width <= 0 || info.frame_height <= 0 || (info.frame_width % 2) != 0 || (info.frame_height % 2) != 0) { die("Invalid frame size: %dx%d", info.frame_width, info.frame_height); } if (!aom_img_alloc(&raw, AOM_IMG_FMT_I420, info.frame_width, info.frame_height, 1)) { die("Failed to allocate image."); } keyframe_interval = (int)strtol(keyframe_interval_arg, NULL, 0); if (keyframe_interval < 0) die("Invalid keyframe interval value."); printf("Using %s\n", aom_codec_iface_name(encoder->codec_interface())); res = aom_codec_enc_config_default(encoder->codec_interface(), &cfg, 0); if (res) die_codec(&codec, "Failed to get default codec config."); cfg.g_w = info.frame_width; cfg.g_h = info.frame_height; cfg.g_timebase.num = info.time_base.numerator; cfg.g_timebase.den = info.time_base.denominator; cfg.rc_target_bitrate = bitrate; cfg.g_error_resilient = (aom_codec_er_flags_t)strtoul(argv[7], NULL, 0); writer = aom_video_writer_open(outfile_arg, kContainerIVF, &info); if (!writer) die("Failed to open %s for writing.", outfile_arg); if (!(infile = fopen(infile_arg, "rb"))) die("Failed to open %s for reading.", infile_arg); if (aom_codec_enc_init(&codec, encoder->codec_interface(), &cfg, 0)) die_codec(&codec, "Failed to initialize encoder"); // Encode frames. while (aom_img_read(&raw, infile)) { int flags = 0; if (keyframe_interval > 0 && frame_count % keyframe_interval == 0) flags |= AOM_EFLAG_FORCE_KF; encode_frame(&codec, &raw, frame_count++, flags, writer); frames_encoded++; if (max_frames > 0 && frames_encoded >= max_frames) break; } // Flush encoder. while (encode_frame(&codec, NULL, -1, 0, writer)) continue; printf("\n"); fclose(infile); printf("Processed %d frames.\n", frame_count); aom_img_free(&raw); if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec."); aom_video_writer_close(writer); return EXIT_SUCCESS; }