blob: e44d8788d3914c40265d41b27bc8d31adc8795e3 [file]
/*
* Copyright (c) 2026 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef MODULES_VIDEO_CODING_CODECS_AV1_LIBAOM_AV1_ENCODER_V2_H_
#define MODULES_VIDEO_CODING_CODECS_AV1_LIBAOM_AV1_ENCODER_V2_H_
#include <array>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "api/scoped_refptr.h"
#include "api/video/resolution.h"
#include "api/video/video_codec_constants.h"
#include "api/video/video_frame_buffer.h"
#include "api/video_codecs/video_encoder_factory_interface.h"
#include "api/video_codecs/video_encoder_interface.h"
#include "modules/video_coding/utility/cbr_layer_rate_tracker.h"
#include "modules/video_coding/utility/reference_buffer_tracker.h"
#include "third_party/libaom/source/libaom/aom/aom_codec.h"
#include "third_party/libaom/source/libaom/aom/aom_encoder.h"
#include "third_party/libaom/source/libaom/aom/aom_image.h"
#include "third_party/libaom/source/libaom/aom/aomcx.h"
namespace webrtc {
class LibaomAv1EncoderV2 : public VideoEncoderInterface {
public:
static VideoEncoderFactoryInterface::Capabilities GetCapabilities();
LibaomAv1EncoderV2() = default;
~LibaomAv1EncoderV2() override;
bool InitEncode(
const VideoEncoderFactoryInterface::StaticEncoderSettings& settings,
const std::map<std::string, std::string>& encoder_specific_settings);
void Encode(scoped_refptr<VideoFrameBuffer> frame_buffer,
const TemporalUnitSettings& tu_settings,
std::vector<FrameEncodeSettings> frame_settings) override;
private:
static constexpr int kNumBuffers = 8;
using aom_img_ptr = std::unique_ptr<aom_image_t, decltype(&aom_img_free)>;
aom_svc_params_t GetSvcParams(
const VideoFrameBuffer& frame_buffer,
const std::vector<FrameEncodeSettings>& frame_settings) const;
aom_img_ptr image_to_encode_ = aom_img_ptr(nullptr, aom_img_free);
aom_codec_ctx_t ctx_{};
aom_codec_enc_cfg_t cfg_{};
// The configuration and SVC parameters last handed to libaom. Reapplying
// them has side effects even when nothing changed, e.g. the rate control of
// layers running at a lower frame rate than the last encoded one is reset,
// so they are only applied when they differ from these.
std::optional<aom_codec_enc_cfg_t> applied_cfg_;
std::optional<aom_svc_params_t> applied_svc_params_;
std::optional<ContentHint> content_type_;
std::array<std::optional<int>, kMaxSpatialLayers> effort_level_by_spatial_id_;
int max_number_of_threads_ = 0;
std::array<std::optional<Resolution>, kNumBuffers> last_resolution_in_buffer_;
ReferenceBufferTracker reference_buffer_tracker_{kNumBuffers};
// Recreated on every `InitEncode`, since what it has learned only describes
// the configuration it was fed.
std::unique_ptr<CbrLayerRateTracker> rate_tracker_;
};
} // namespace webrtc
#endif // MODULES_VIDEO_CODING_CODECS_AV1_LIBAOM_AV1_ENCODER_V2_H_