123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727 |
- // Copyright 2020 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #include "media/video/vpx_video_encoder.h"
- #include "base/cxx17_backports.h"
- #include "base/logging.h"
- #include "base/numerics/checked_math.h"
- #include "base/strings/stringprintf.h"
- #include "base/system/sys_info.h"
- #include "base/time/time.h"
- #include "base/trace_event/trace_event.h"
- #include "media/base/bind_to_current_loop.h"
- #include "media/base/svc_scalability_mode.h"
- #include "media/base/timestamp_constants.h"
- #include "media/base/video_frame.h"
- #include "media/base/video_util.h"
- #include "third_party/libvpx/source/libvpx/vpx/vp8cx.h"
- #include "third_party/libyuv/include/libyuv/convert.h"
- namespace media {
- namespace {
- constexpr vpx_enc_frame_flags_t VP8_UPDATE_NOTHING =
- VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_LAST;
- // Frame Pattern:
- // Layer Index 0: |0| |2| |4| |6| |8|
- // Layer Index 1: | |1| |3| |5| |7| |
- vpx_enc_frame_flags_t vp8_2layers_temporal_flags[] = {
- // Layer 0 : update and reference only last frame
- VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_GF |
- VP8_EFLAG_NO_UPD_ARF,
- // Layer 1: only reference last frame, no updates
- VP8_UPDATE_NOTHING | VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_REF_GF};
- // Frame Pattern:
- // Layer Index 0: |0| | | |4| | | |8| | | |12|
- // Layer Index 1: | | |2| | | |6| | | |10| | |
- // Layer Index 2: | |1| |3| |5| |7| |9| |11| |
- vpx_enc_frame_flags_t vp8_3layers_temporal_flags[] = {
- // Layer 0 : update and reference only last frame
- // It only depends on layer 0
- VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_GF |
- VP8_EFLAG_NO_UPD_ARF,
- // Layer 2: only reference last frame, no updates
- // It only depends on layer 0
- VP8_UPDATE_NOTHING | VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_REF_GF,
- // Layer 1: only reference last frame, update gold frame
- // It only depends on layer 0
- VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_ARF |
- VP8_EFLAG_NO_UPD_LAST,
- // Layer 2: reference last frame and gold frame, no updates
- // It depends on layer 0 and layer 1
- VP8_UPDATE_NOTHING | VP8_EFLAG_NO_REF_ARF,
- };
- // Returns the number of threads.
- int GetNumberOfThreads(int width) {
- // Default to 1 thread for less than VGA.
- int desired_threads = 1;
- if (width >= 3840)
- desired_threads = 16;
- else if (width >= 2560)
- desired_threads = 8;
- else if (width >= 1280)
- desired_threads = 4;
- else if (width >= 640)
- desired_threads = 2;
- // Clamp to the number of available logical processors/cores.
- desired_threads =
- std::min(desired_threads, base::SysInfo::NumberOfProcessors());
- return desired_threads;
- }
- EncoderStatus SetUpVpxConfig(const VideoEncoder::Options& opts,
- vpx_codec_enc_cfg_t* config) {
- if (opts.frame_size.width() <= 0 || opts.frame_size.height() <= 0)
- return EncoderStatus(EncoderStatus::Codes::kEncoderUnsupportedConfig,
- "Negative width or height values.");
- if (!opts.frame_size.GetCheckedArea().IsValid())
- return EncoderStatus(EncoderStatus::Codes::kEncoderUnsupportedConfig,
- "Frame is too large.");
- config->g_pass = VPX_RC_ONE_PASS;
- config->g_lag_in_frames = 0;
- config->rc_max_quantizer = 58;
- config->rc_min_quantizer = 2;
- config->rc_resize_allowed = 0;
- config->rc_dropframe_thresh = 0; // Don't drop frames
- config->g_timebase.num = 1;
- config->g_timebase.den = base::Time::kMicrosecondsPerSecond;
- // Set the number of threads based on the image width and num of cores.
- config->g_threads = GetNumberOfThreads(opts.frame_size.width());
- // Insert keyframes at will with a given max interval
- if (opts.keyframe_interval.has_value()) {
- config->kf_mode = VPX_KF_AUTO;
- config->kf_min_dist = 0;
- config->kf_max_dist = opts.keyframe_interval.value();
- }
- if (opts.bitrate.has_value()) {
- auto& bitrate = opts.bitrate.value();
- config->rc_target_bitrate = bitrate.target_bps() / 1000;
- switch (bitrate.mode()) {
- case Bitrate::Mode::kVariable:
- config->rc_end_usage = VPX_VBR;
- break;
- case Bitrate::Mode::kConstant:
- config->rc_end_usage = VPX_CBR;
- break;
- }
- } else {
- config->rc_target_bitrate = GetDefaultVideoEncodeBitrate(
- opts.frame_size, opts.framerate.value_or(30));
- }
- config->g_w = opts.frame_size.width();
- config->g_h = opts.frame_size.height();
- if (!opts.scalability_mode)
- return EncoderStatus::Codes::kOk;
- switch (opts.scalability_mode.value()) {
- case SVCScalabilityMode::kL1T2:
- // Frame Pattern:
- // Layer Index 0: |0| |2| |4| |6| |8|
- // Layer Index 1: | |1| |3| |5| |7| |
- config->ts_number_layers = 2;
- config->ts_periodicity = 2;
- DCHECK_EQ(config->ts_periodicity,
- sizeof(vp8_2layers_temporal_flags) /
- sizeof(vp8_2layers_temporal_flags[0]));
- config->ts_layer_id[0] = 0;
- config->ts_layer_id[1] = 1;
- config->ts_rate_decimator[0] = 2;
- config->ts_rate_decimator[1] = 1;
- // Bitrate allocation L0: 60% L1: 40%
- config->layer_target_bitrate[0] = config->ts_target_bitrate[0] =
- 60 * config->rc_target_bitrate / 100;
- config->layer_target_bitrate[1] = config->ts_target_bitrate[1] =
- config->rc_target_bitrate;
- config->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_0101;
- config->g_error_resilient = VPX_ERROR_RESILIENT_DEFAULT;
- break;
- case SVCScalabilityMode::kL1T3:
- // Frame Pattern:
- // Layer Index 0: |0| | | |4| | | |8| | | |12|
- // Layer Index 1: | | |2| | | |6| | | |10| | |
- // Layer Index 2: | |1| |3| |5| |7| |9| |11| |
- config->ts_number_layers = 3;
- config->ts_periodicity = 4;
- DCHECK_EQ(config->ts_periodicity,
- sizeof(vp8_3layers_temporal_flags) /
- sizeof(vp8_3layers_temporal_flags[0]));
- config->ts_layer_id[0] = 0;
- config->ts_layer_id[1] = 2;
- config->ts_layer_id[2] = 1;
- config->ts_layer_id[3] = 2;
- config->ts_rate_decimator[0] = 4;
- config->ts_rate_decimator[1] = 2;
- config->ts_rate_decimator[2] = 1;
- // Bitrate allocation L0: 50% L1: 20% L2: 30%
- config->layer_target_bitrate[0] = config->ts_target_bitrate[0] =
- 50 * config->rc_target_bitrate / 100;
- config->layer_target_bitrate[1] = config->ts_target_bitrate[1] =
- 70 * config->rc_target_bitrate / 100;
- config->layer_target_bitrate[2] = config->ts_target_bitrate[2] =
- config->rc_target_bitrate;
- config->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_0212;
- config->g_error_resilient = VPX_ERROR_RESILIENT_DEFAULT;
- break;
- default: {
- return EncoderStatus(EncoderStatus::Codes::kEncoderUnsupportedConfig,
- "Unsupported number of temporal layers.");
- }
- }
- return EncoderStatus::Codes::kOk;
- }
- vpx_svc_extra_cfg_t MakeSvcExtraConfig(const vpx_codec_enc_cfg_t& config) {
- vpx_svc_extra_cfg_t result = {};
- result.temporal_layering_mode = config.temporal_layering_mode;
- for (size_t i = 0; i < config.ts_number_layers; ++i) {
- result.scaling_factor_num[i] = 1;
- result.scaling_factor_den[i] = 1;
- result.max_quantizers[i] = config.rc_max_quantizer;
- result.min_quantizers[i] = config.rc_min_quantizer;
- }
- return result;
- }
- EncoderStatus ReallocateVpxImageIfNeeded(vpx_image_t* vpx_image,
- const vpx_img_fmt fmt,
- int width,
- int height) {
- if (vpx_image->fmt != fmt || static_cast<int>(vpx_image->w) != width ||
- static_cast<int>(vpx_image->h) != height) {
- vpx_img_free(vpx_image);
- if (vpx_image != vpx_img_alloc(vpx_image, fmt, width, height, 1)) {
- return EncoderStatus(EncoderStatus::Codes::kEncoderFailedEncode,
- "Invalid format or frame size.");
- }
- vpx_image->bit_depth = (fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 16 : 8;
- }
- // else no-op since the image don't need to change format.
- return EncoderStatus::Codes::kOk;
- }
- void FreeCodecCtx(vpx_codec_ctx_t* codec_ctx) {
- if (codec_ctx->name) {
- // Codec has been initialized, we need to destroy it.
- auto error = vpx_codec_destroy(codec_ctx);
- DCHECK_EQ(error, VPX_CODEC_OK);
- }
- delete codec_ctx;
- }
- } // namespace
- VpxVideoEncoder::VpxVideoEncoder() : codec_(nullptr, FreeCodecCtx) {}
- void VpxVideoEncoder::Initialize(VideoCodecProfile profile,
- const Options& options,
- OutputCB output_cb,
- EncoderStatusCB done_cb) {
- done_cb = BindToCurrentLoop(std::move(done_cb));
- if (codec_) {
- std::move(done_cb).Run(EncoderStatus::Codes::kEncoderInitializeTwice);
- return;
- }
- profile_ = profile;
- bool is_vp9 = false;
- vpx_codec_iface_t* iface = nullptr;
- if (profile == VP8PROFILE_ANY) {
- iface = vpx_codec_vp8_cx();
- } else if (profile == VP9PROFILE_PROFILE0 || profile == VP9PROFILE_PROFILE2) {
- // TODO(https://crbug.com/1116617): Consider support for profiles 1 and 3.
- is_vp9 = true;
- iface = vpx_codec_vp9_cx();
- } else {
- auto status =
- EncoderStatus(EncoderStatus::Codes::kEncoderUnsupportedProfile)
- .WithData("profile", profile);
- std::move(done_cb).Run(status);
- return;
- }
- auto vpx_error = vpx_codec_enc_config_default(iface, &codec_config_, 0);
- if (vpx_error != VPX_CODEC_OK) {
- auto status =
- EncoderStatus(EncoderStatus::Codes::kEncoderInitializationError,
- "Failed to get default VPX config.")
- .WithData("vpx_error", vpx_error);
- std::move(done_cb).Run(status);
- return;
- }
- vpx_img_fmt img_fmt = VPX_IMG_FMT_NONE;
- unsigned int bits_for_storage = 8;
- switch (profile) {
- case VP9PROFILE_PROFILE1:
- codec_config_.g_profile = 1;
- break;
- case VP9PROFILE_PROFILE2:
- codec_config_.g_profile = 2;
- img_fmt = VPX_IMG_FMT_I42016;
- bits_for_storage = 16;
- codec_config_.g_bit_depth = VPX_BITS_10;
- codec_config_.g_input_bit_depth = 10;
- break;
- case VP9PROFILE_PROFILE3:
- codec_config_.g_profile = 3;
- break;
- default:
- codec_config_.g_profile = 0;
- img_fmt = VPX_IMG_FMT_I420;
- bits_for_storage = 8;
- codec_config_.g_bit_depth = VPX_BITS_8;
- codec_config_.g_input_bit_depth = 8;
- break;
- }
- auto status = SetUpVpxConfig(options, &codec_config_);
- if (!status.is_ok()) {
- std::move(done_cb).Run(status);
- return;
- }
- vpx_codec_unique_ptr codec(new vpx_codec_ctx_t, FreeCodecCtx);
- codec->name = nullptr; // We are allowed to use vpx_codec_ctx_t.name
- vpx_error = vpx_codec_enc_init(
- codec.get(), iface, &codec_config_,
- codec_config_.g_bit_depth == VPX_BITS_8 ? 0 : VPX_CODEC_USE_HIGHBITDEPTH);
- if (vpx_error != VPX_CODEC_OK) {
- std::string msg = base::StringPrintf(
- "VPX encoder initialization error: %s %s",
- vpx_codec_err_to_string(vpx_error), codec->err_detail);
- DLOG(ERROR) << msg;
- std::move(done_cb).Run(
- EncoderStatus(EncoderStatus::Codes::kEncoderInitializationError, msg));
- return;
- }
- // For VP9 the values used for real-time encoding mode are 5, 6, 7,
- // 8, 9. Higher means faster encoding, but lower quality.
- // For VP8 typical values used for real-time encoding are -4, -6,
- // -8, -10. Again larger magnitude means faster encoding but lower
- // quality.
- int cpu_used = is_vp9 ? 7 : -6;
- vpx_error = vpx_codec_control(codec.get(), VP8E_SET_CPUUSED, cpu_used);
- if (vpx_error != VPX_CODEC_OK) {
- std::string msg =
- base::StringPrintf("VPX encoder VP8E_SET_CPUUSED error: %s",
- vpx_codec_err_to_string(vpx_error));
- DLOG(ERROR) << msg;
- std::move(done_cb).Run(
- EncoderStatus(EncoderStatus::Codes::kEncoderInitializationError, msg));
- return;
- }
- if (&vpx_image_ != vpx_img_alloc(&vpx_image_, img_fmt,
- options.frame_size.width(),
- options.frame_size.height(), 1)) {
- std::move(done_cb).Run(
- EncoderStatus(EncoderStatus::Codes::kEncoderInitializationError,
- "Invalid format or frame size."));
- return;
- }
- vpx_image_.bit_depth = bits_for_storage;
- if (is_vp9) {
- // Set the number of column tiles in encoding an input frame, with number of
- // tile columns (in Log2 unit) as the parameter.
- // The minimum width of a tile column is 256 pixels, the maximum is 4096.
- int log2_tile_columns =
- static_cast<int>(std::log2(codec_config_.g_w / 256));
- vpx_codec_control(codec.get(), VP9E_SET_TILE_COLUMNS, log2_tile_columns);
- // Turn on row level multi-threading.
- vpx_codec_control(codec.get(), VP9E_SET_ROW_MT, 1);
- if (codec_config_.ts_number_layers > 1) {
- vpx_svc_extra_cfg_t svc_conf = MakeSvcExtraConfig(codec_config_);
- // VP9 needs SVC to be turned on explicitly
- vpx_codec_control(codec.get(), VP9E_SET_SVC_PARAMETERS, &svc_conf);
- vpx_error = vpx_codec_control(codec.get(), VP9E_SET_SVC, 1);
- if (vpx_error != VPX_CODEC_OK) {
- std::string msg =
- base::StringPrintf("Can't activate SVC encoding: %s",
- vpx_codec_err_to_string(vpx_error));
- DLOG(ERROR) << msg;
- status = EncoderStatus(
- EncoderStatus::Codes::kEncoderInitializationError, msg);
- std::move(done_cb).Run(status);
- return;
- }
- }
- // In CBR mode use aq-mode=3 is enabled for quality improvement
- if (codec_config_.rc_end_usage == VPX_CBR)
- vpx_codec_control(codec.get(), VP9E_SET_AQ_MODE, 3);
- }
- options_ = options;
- originally_configured_size_ = options.frame_size;
- output_cb_ = BindToCurrentLoop(std::move(output_cb));
- codec_ = std::move(codec);
- std::move(done_cb).Run(EncoderStatus::Codes::kOk);
- }
- void VpxVideoEncoder::Encode(scoped_refptr<VideoFrame> frame,
- bool key_frame,
- EncoderStatusCB done_cb) {
- done_cb = BindToCurrentLoop(std::move(done_cb));
- if (!codec_) {
- std::move(done_cb).Run(
- EncoderStatus::Codes::kEncoderInitializeNeverCompleted);
- return;
- }
- if (!frame) {
- std::move(done_cb).Run(
- EncoderStatus(EncoderStatus::Codes::kEncoderFailedEncode,
- "No frame provided for encoding."));
- return;
- }
- bool supported_format = frame->format() == PIXEL_FORMAT_NV12 ||
- frame->format() == PIXEL_FORMAT_I420 ||
- frame->format() == PIXEL_FORMAT_XBGR ||
- frame->format() == PIXEL_FORMAT_XRGB ||
- frame->format() == PIXEL_FORMAT_ABGR ||
- frame->format() == PIXEL_FORMAT_ARGB;
- if ((!frame->IsMappable() && !frame->HasGpuMemoryBuffer()) ||
- !supported_format) {
- std::move(done_cb).Run(
- EncoderStatus(EncoderStatus::Codes::kEncoderFailedEncode,
- "Unexpected frame format.")
- .WithData("IsMappable", frame->IsMappable())
- .WithData("format", frame->format()));
- return;
- }
- if (frame->format() == PIXEL_FORMAT_NV12 && frame->HasGpuMemoryBuffer()) {
- frame = ConvertToMemoryMappedFrame(frame);
- if (!frame) {
- std::move(done_cb).Run(
- EncoderStatus(EncoderStatus::Codes::kEncoderFailedEncode,
- "Convert GMB frame to MemoryMappedFrame failed."));
- return;
- }
- }
- const bool is_yuv = IsYuvPlanar(frame->format());
- if (frame->visible_rect().size() != options_.frame_size || !is_yuv) {
- auto resized_frame = frame_pool_.CreateFrame(
- is_yuv ? frame->format() : PIXEL_FORMAT_I420, options_.frame_size,
- gfx::Rect(options_.frame_size), options_.frame_size,
- frame->timestamp());
- if (!resized_frame) {
- std::move(done_cb).Run(
- EncoderStatus(EncoderStatus::Codes::kEncoderFailedEncode,
- "Can't allocate a resized frame"));
- return;
- }
- auto convert_status =
- ConvertAndScaleFrame(*frame, *resized_frame, resize_buf_);
- if (!convert_status.is_ok()) {
- std::move(done_cb).Run(
- EncoderStatus(EncoderStatus::Codes::kEncoderFailedEncode)
- .AddCause(std::move(convert_status)));
- return;
- }
- frame = std::move(resized_frame);
- }
- switch (profile_) {
- case VP9PROFILE_PROFILE2:
- // Profile 2 uses 10bit color,
- libyuv::I420ToI010(
- frame->visible_data(VideoFrame::kYPlane),
- frame->stride(VideoFrame::kYPlane),
- frame->visible_data(VideoFrame::kUPlane),
- frame->stride(VideoFrame::kUPlane),
- frame->visible_data(VideoFrame::kVPlane),
- frame->stride(VideoFrame::kVPlane),
- reinterpret_cast<uint16_t*>(vpx_image_.planes[VPX_PLANE_Y]),
- vpx_image_.stride[VPX_PLANE_Y] / 2,
- reinterpret_cast<uint16_t*>(vpx_image_.planes[VPX_PLANE_U]),
- vpx_image_.stride[VPX_PLANE_U] / 2,
- reinterpret_cast<uint16_t*>(vpx_image_.planes[VPX_PLANE_V]),
- vpx_image_.stride[VPX_PLANE_V] / 2, frame->visible_rect().width(),
- frame->visible_rect().height());
- break;
- case VP9PROFILE_PROFILE1:
- case VP9PROFILE_PROFILE3:
- NOTREACHED();
- break;
- default:
- vpx_img_fmt_t fmt = frame->format() == PIXEL_FORMAT_NV12
- ? VPX_IMG_FMT_NV12
- : VPX_IMG_FMT_I420;
- EncoderStatus status = ReallocateVpxImageIfNeeded(
- &vpx_image_, fmt, codec_config_.g_w, codec_config_.g_h);
- if (!status.is_ok()) {
- std::move(done_cb).Run(status);
- return;
- }
- if (fmt == VPX_IMG_FMT_NV12) {
- vpx_image_.planes[VPX_PLANE_Y] =
- const_cast<uint8_t*>(frame->visible_data(VideoFrame::kYPlane));
- vpx_image_.planes[VPX_PLANE_U] =
- const_cast<uint8_t*>(frame->visible_data(VideoFrame::kUVPlane));
- // In NV12 Y and U samples are combined in one plane (bytes go YUYUYU),
- // but libvpx treats them as two planes with the same stride but shifted
- // by one byte.
- vpx_image_.planes[VPX_PLANE_V] = vpx_image_.planes[VPX_PLANE_U] + 1;
- vpx_image_.stride[VPX_PLANE_Y] = frame->stride(VideoFrame::kYPlane);
- vpx_image_.stride[VPX_PLANE_U] = frame->stride(VideoFrame::kUVPlane);
- vpx_image_.stride[VPX_PLANE_V] = frame->stride(VideoFrame::kUVPlane);
- } else {
- vpx_image_.planes[VPX_PLANE_Y] =
- const_cast<uint8_t*>(frame->visible_data(VideoFrame::kYPlane));
- vpx_image_.planes[VPX_PLANE_U] =
- const_cast<uint8_t*>(frame->visible_data(VideoFrame::kUPlane));
- vpx_image_.planes[VPX_PLANE_V] =
- const_cast<uint8_t*>(frame->visible_data(VideoFrame::kVPlane));
- vpx_image_.stride[VPX_PLANE_Y] = frame->stride(VideoFrame::kYPlane);
- vpx_image_.stride[VPX_PLANE_U] = frame->stride(VideoFrame::kUPlane);
- vpx_image_.stride[VPX_PLANE_V] = frame->stride(VideoFrame::kVPlane);
- }
- break;
- }
- // Use zero as a timestamp, so encoder will not use it for rate control.
- // In absence of timestamp libvpx uses duration.
- constexpr auto timestamp_us = 0;
- auto duration_us = GetFrameDuration(*frame).InMicroseconds();
- last_frame_timestamp_ = frame->timestamp();
- if (last_frame_color_space_ != frame->ColorSpace()) {
- last_frame_color_space_ = frame->ColorSpace();
- key_frame = true;
- }
- auto deadline = VPX_DL_REALTIME;
- vpx_codec_flags_t flags = key_frame ? VPX_EFLAG_FORCE_KF : 0;
- int temporal_id = 0;
- if (codec_config_.ts_number_layers > 1) {
- if (key_frame)
- temporal_svc_frame_index = 0;
- int index_in_temp_cycle =
- temporal_svc_frame_index % codec_config_.ts_periodicity;
- temporal_id = codec_config_.ts_layer_id[index_in_temp_cycle];
- temporal_svc_frame_index++;
- if (profile_ == VP8PROFILE_ANY) {
- auto* vp8_layers_flags = codec_config_.ts_number_layers == 2
- ? vp8_2layers_temporal_flags
- : vp8_3layers_temporal_flags;
- flags |= vp8_layers_flags[index_in_temp_cycle];
- vpx_codec_control(codec_.get(), VP8E_SET_TEMPORAL_LAYER_ID, temporal_id);
- }
- }
- TRACE_EVENT1("media", "vpx_codec_encode", "timestamp", frame->timestamp());
- auto vpx_error = vpx_codec_encode(codec_.get(), &vpx_image_, timestamp_us,
- duration_us, flags, deadline);
- if (vpx_error != VPX_CODEC_OK) {
- std::string msg = base::StringPrintf("VPX encoding error: %s (%s)",
- vpx_codec_err_to_string(vpx_error),
- vpx_codec_error_detail(codec_.get()));
- DLOG(ERROR) << msg;
- std::move(done_cb).Run(
- EncoderStatus(EncoderStatus::Codes::kEncoderFailedEncode, msg)
- .WithData("vpx_error", vpx_error));
- return;
- }
- DrainOutputs(temporal_id, frame->timestamp(), frame->ColorSpace());
- std::move(done_cb).Run(EncoderStatus::Codes::kOk);
- }
- void VpxVideoEncoder::ChangeOptions(const Options& options,
- OutputCB output_cb,
- EncoderStatusCB done_cb) {
- done_cb = BindToCurrentLoop(std::move(done_cb));
- if (!codec_) {
- std::move(done_cb).Run(
- EncoderStatus::Codes::kEncoderInitializeNeverCompleted);
- return;
- }
- // Libvpx is very peculiar about encoded frame size changes,
- // - VP8: As long as the frame area doesn't increase, internal codec
- // structures don't need to be reallocated and codec can be simply
- // reconfigured.
- // - VP9: The codec cannot increase encoded width or height larger than their
- // initial values.
- //
- // Mind the difference between old frame sizes:
- // - |originally_configured_size_| is set only once when the vpx_codec_ctx_t
- // is created.
- // - |options_.frame_size| changes every time ChangeOptions() is called.
- // More info can be found here:
- // https://bugs.chromium.org/p/webm/issues/detail?id=1642
- // https://bugs.chromium.org/p/webm/issues/detail?id=912
- if (profile_ == VP8PROFILE_ANY) {
- // VP8 resize restrictions
- auto old_area = originally_configured_size_.GetCheckedArea();
- auto new_area = options.frame_size.GetCheckedArea();
- DCHECK(old_area.IsValid());
- if (!new_area.IsValid() || new_area.ValueOrDie() > old_area.ValueOrDie()) {
- auto status = EncoderStatus(
- EncoderStatus::Codes::kEncoderUnsupportedConfig,
- "libvpx/VP8 doesn't support dynamically increasing frame area");
- std::move(done_cb).Run(std::move(status));
- return;
- }
- } else {
- // VP9 resize restrictions
- if (options.frame_size.width() > originally_configured_size_.width() ||
- options.frame_size.height() > originally_configured_size_.height()) {
- auto status = EncoderStatus(
- EncoderStatus::Codes::kEncoderUnsupportedConfig,
- "libvpx/VP9 doesn't support dynamically increasing frame dimentions");
- std::move(done_cb).Run(std::move(status));
- return;
- }
- }
- vpx_codec_enc_cfg_t new_config = codec_config_;
- auto status = SetUpVpxConfig(options, &new_config);
- if (!status.is_ok()) {
- std::move(done_cb).Run(status);
- return;
- }
- status = ReallocateVpxImageIfNeeded(&vpx_image_, vpx_image_.fmt,
- options.frame_size.width(),
- options.frame_size.height());
- if (!status.is_ok()) {
- std::move(done_cb).Run(status);
- return;
- }
- auto error = vpx_codec_enc_config_set(codec_.get(), &new_config);
- const bool is_vp9 = (profile_ != VP8PROFILE_ANY);
- if (is_vp9 && error == VPX_CODEC_OK && new_config.ts_number_layers > 1) {
- vpx_svc_extra_cfg_t svc_conf = MakeSvcExtraConfig(new_config);
- vpx_codec_control(codec_.get(), VP9E_SET_SVC_PARAMETERS, &svc_conf);
- error = vpx_codec_control(codec_.get(), VP9E_SET_SVC, 1);
- }
- if (error == VPX_CODEC_OK) {
- codec_config_ = new_config;
- options_ = options;
- if (!output_cb.is_null())
- output_cb_ = BindToCurrentLoop(std::move(output_cb));
- } else {
- status = EncoderStatus(EncoderStatus::Codes::kEncoderUnsupportedConfig,
- "Failed to set new VPX config")
- .WithData("vpx_error", error);
- }
- std::move(done_cb).Run(std::move(status));
- }
- base::TimeDelta VpxVideoEncoder::GetFrameDuration(const VideoFrame& frame) {
- // Frame has duration in metadata, use it.
- if (frame.metadata().frame_duration.has_value())
- return frame.metadata().frame_duration.value();
- // Options have framerate specified, use it.
- if (options_.framerate.has_value())
- return base::Seconds(1.0 / options_.framerate.value());
- // No real way to figure out duration, use time passed since the last frame
- // as an educated guess, but clamp it within a reasonable limits.
- constexpr auto min_duration = base::Seconds(1.0 / 60.0);
- constexpr auto max_duration = base::Seconds(1.0 / 24.0);
- auto duration = frame.timestamp() - last_frame_timestamp_;
- return base::clamp(duration, min_duration, max_duration);
- }
- VpxVideoEncoder::~VpxVideoEncoder() {
- if (!codec_)
- return;
- // It's safe to call vpx_img_free, even if vpx_image_ has never been
- // initialized. vpx_img_free is not going to deallocate the vpx_image_
- // itself, only internal buffers.
- vpx_img_free(&vpx_image_);
- }
- void VpxVideoEncoder::Flush(EncoderStatusCB done_cb) {
- done_cb = BindToCurrentLoop(std::move(done_cb));
- if (!codec_) {
- std::move(done_cb).Run(
- EncoderStatus::Codes::kEncoderInitializeNeverCompleted);
- return;
- }
- auto vpx_error = vpx_codec_encode(codec_.get(), nullptr, -1, 0, 0, 0);
- if (vpx_error != VPX_CODEC_OK) {
- std::string msg = base::StringPrintf("VPX flushing error: %s (%s)",
- vpx_codec_err_to_string(vpx_error),
- vpx_codec_error_detail(codec_.get()));
- DLOG(ERROR) << msg;
- auto status = EncoderStatus(EncoderStatus::Codes::kEncoderFailedEncode, msg)
- .WithData("vpx_error", vpx_error);
- std::move(done_cb).Run(std::move(status));
- return;
- }
- DrainOutputs(0, base::TimeDelta(), gfx::ColorSpace());
- std::move(done_cb).Run(EncoderStatus::Codes::kOk);
- }
- void VpxVideoEncoder::DrainOutputs(int temporal_id,
- base::TimeDelta ts,
- gfx::ColorSpace color_space) {
- vpx_codec_iter_t iter = nullptr;
- const vpx_codec_cx_pkt_t* pkt = nullptr;
- while ((pkt = vpx_codec_get_cx_data(codec_.get(), &iter))) {
- if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
- VideoEncoderOutput result;
- result.key_frame = (pkt->data.frame.flags & VPX_FRAME_IS_KEY) != 0;
- if (result.key_frame) {
- // If we got an unexpected key frame, temporal_svc_frame_index needs to
- // be adjusted, because the next frame should have index 1.
- temporal_svc_frame_index = 1;
- result.temporal_id = 0;
- } else {
- result.temporal_id = temporal_id;
- }
- // We don't given timestamps to vpx_codec_encode() that's why
- // pkt->data.frame.pts can't be used here.
- result.timestamp = ts;
- result.color_space = color_space;
- result.size = pkt->data.frame.sz;
- result.data = std::make_unique<uint8_t[]>(result.size);
- memcpy(result.data.get(), pkt->data.frame.buf, result.size);
- output_cb_.Run(std::move(result), {});
- }
- }
- }
- } // namespace media
|