Skip to content

Commit

Permalink
#5 remove non h264 related code from the av_video implementation.
Browse files Browse the repository at this point in the history
Another way of specializing the video codec settings will be deviced in
the future.
  • Loading branch information
stevenhoving committed May 13, 2018
1 parent 0426438 commit e9ec25c
Showing 1 changed file with 5 additions and 89 deletions.
94 changes: 5 additions & 89 deletions CamEncoder/src/av_video.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,111 +172,39 @@ av_video::av_video(const av_video_codec &config, const av_video_meta &meta)
// either quality of bitrate must be set.
assert(!!meta.quality || !!meta.bitrate);

if (meta.quality)
{
if (config.id == AV_CODEC_ID_VP8 ||
config.id == AV_CODEC_ID_VP9)
{
// This value was chosen to make the bitrate high enough
// for libvpx to "turn off" the maximum bitrate feature
// that is normally applied to constant quality.
// \todo read http://wiki.webmproject.org/ffmpeg/vp9-encoding-guide and adapt.
context_->bit_rate = meta.width * meta.height * fps.num / fps.den;
}
}

// Note: The output will be delayed by max_b_frames + 1 relative to the input.
// Is this related to mpeg2?
//context_->max_b_frames = 1;

av_dict av_opts;
apply_preset(av_opts, meta.preset);
apply_tune(av_opts, meta.tune);
apply_profile(av_opts, meta.profile);

/* iterate through lavc_opts and have avutil parse the options for us */
//hb_dict_iter_t iter;
//for (iter = hb_dict_iter_init(lavc_opts);
// iter != HB_DICT_ITER_DONE;
// iter = hb_dict_iter_next(lavc_opts, iter))
//{
// const char *key = hb_dict_iter_key(iter);
// hb_value_t *value = hb_dict_iter_value(iter);
// char *str = hb_value_get_string_xform(value);
//
// /* Here's where the strings are passed to avutil for parsing. */
// av_dict_set(&av_opts, key, str, 0);
// free(str);
//}
//hb_dict_free(&lavc_opts);

// Now set the things in context that we don't want to allow
// the user to override.
if (meta.bitrate)
{
/* Average bitrate */
// Average bitrate
context_->bit_rate = static_cast<int64_t>(1000.0 * meta.bitrate.value());

// ffmpeg's mpeg2 encoder requires that the bit_rate_tolerance be >= bitrate * fps
context_->bit_rate_tolerance = static_cast<int>(context_->bit_rate * av_q2d(fps) + 1);
}
else
{
/* Constant quantizer */
// These settings produce better image quality than
// what was previously used
// These settings produce better image quality than what was previously used
context_->flags |= AV_CODEC_FLAG_QSCALE;
context_->global_quality = static_cast<int>(FF_QP2LAMBDA * meta.quality.value() + 0.5);
//Set constant quality for libvpx
if (config.id == AV_CODEC_ID_VP8 ||
config.id == AV_CODEC_ID_VP9)
{
char quality[7];
snprintf(quality, 7, "%.2f", meta.quality.value());
av_opts["crf"] = quality;
//This value was chosen to make the bitrate high enough
//for libvpx to "turn off" the maximum bitrate feature
//that is normally applied to constant quality.
context_->bit_rate = meta.width * meta.height * fps.num / fps.den;
printf("encavcodec: encoding at CQ %.2f", meta.quality.value());
}
else
{
printf("encavcodec: encoding at constant quantizer %d",
context_->global_quality);
}
fmt::print("av_video: encoding at constant quantizer {}", context_->global_quality);
}
context_->width = meta.width;
context_->height = meta.height;
context_->pix_fmt = AV_PIX_FMT_YUV420P;

//context_->sample_aspect_ratio.num = job->par.num;
//context_->sample_aspect_ratio.den = job->par.den;
//if (config.id == HB_VCODEC_FFMPEG_MPEG4)
//{
// // MPEG-4 Part 2 stores the PAR num/den as unsigned 8-bit fields,
// // and libavcodec's encoder fails to initialize if we don't
// // reduce it to fit 8-bits.
// hb_limit_rational(&context->sample_aspect_ratio.num,
// &context->sample_aspect_ratio.den,
// context->sample_aspect_ratio.num,
// context->sample_aspect_ratio.den, 255);
//}

//hb_log("encavcodec: encoding with stored aspect %d/%d", job->par.num, job->par.den);

//if (job->mux & HB_MUX_MASK_MP4)
//{
// context->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
//}
//

// \todo we have no grayscale settings for now
//if (job->grayscale)
//{
//if (grayscale)
// context->flags |= AV_CODEC_FLAG_GRAY;
//}

// we do not support two pass video encoding.

int ret = avcodec_open2(context_, codec_, av_opts);
if (ret)
Expand All @@ -303,18 +231,6 @@ av_video::av_video(const av_video_codec &config, const av_video_meta &meta)

frame_ = create_video_frame(context_->pix_fmt, context_->width, context_->height);
sws_context_ = create_software_scaler(AV_PIX_FMT_BGR24, context_->width, context_->height);

//job->areBframes = 0;
//if (context->has_b_frames)
//{
// job->areBframes = 1;
//}

//if ((job->mux & HB_MUX_MASK_MP4) && job->pass_id != HB_PASS_ENCODE_1ST)
//{
// w->config->mpeg4.length = context->extradata_size;
// memcpy(w->config->mpeg4.bytes, context->extradata, context->extradata_size);
//}
}

av_video::~av_video()
Expand Down

0 comments on commit e9ec25c

Please sign in to comment.