webrtc/modules/rtp_rtcp/source/rtp_packetizer_av1.h
Philipp Hancke acfd279a14 av1: make packetization generate more evenly sized packets
Implements a two-pass approach to packetization which creates
packets of an even size similar to RtpPacketizer::SplitAboutEqually.
This improves the bandwidth estimation.

The algorithm does a first pass with the existing packetizer, then
iterates through the resulting packet sizes and sums up the bytes left unused in each packet.
It then calculates a new maximum packet length as
  configured_max_packet_len - ((unused_bytes - packets + 1) / packets)
adjusts for the overhead and re-runs the packetization algorithm.

For example, a list of OBUs with sizes
  {1206, 1476, 1431}
currently gets packetized greedily as payload sizes
  {1200, 1200, 1200, 523}
With this change, it gets packetized as
  {1032, 1032, 1032, 1028}

This change is guarded by the field trial
  WebRTC-Video-AV1EvenPayloadSizes
which is acting as a rollout flag.

BUG=webrtc:15927

Co-authored-by: Shyam Sadhwani <shyamsadhwani@meta.com>
Change-Id: I4f0b3c27de6f06104908dd769c4dd1f34115712c
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/348100
Commit-Queue: Philipp Hancke <phancke@meta.com>
Reviewed-by: Danil Chapovalov <danilchap@webrtc.org>
Reviewed-by: Per Kjellander <perkj@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#42203}
2024-04-30 15:46:06 +00:00

78 lines
2.7 KiB
C++

/*
* Copyright (c) 2019 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_RTP_RTCP_SOURCE_RTP_PACKETIZER_AV1_H_
#define MODULES_RTP_RTCP_SOURCE_RTP_PACKETIZER_AV1_H_
#include <stddef.h>
#include <stdint.h>
#include <vector>
#include "api/array_view.h"
#include "api/video/video_frame_type.h"
#include "modules/rtp_rtcp/source/rtp_format.h"
namespace webrtc {
class RtpPacketizerAv1 : public RtpPacketizer {
public:
RtpPacketizerAv1(rtc::ArrayView<const uint8_t> payload,
PayloadSizeLimits limits,
VideoFrameType frame_type,
bool is_last_frame_in_picture,
bool even_distribution);
~RtpPacketizerAv1() override = default;
size_t NumPackets() const override { return packets_.size() - packet_index_; }
bool NextPacket(RtpPacketToSend* packet) override;
private:
struct Obu {
uint8_t header;
uint8_t extension_header; // undefined if (header & kXbit) == 0
rtc::ArrayView<const uint8_t> payload;
int size; // size of the header and payload combined.
};
struct Packet {
explicit Packet(int first_obu_index) : first_obu(first_obu_index) {}
// Indexes into obus_ vector of the first and last obus that should put into
// the packet.
int first_obu;
int num_obu_elements = 0;
int first_obu_offset = 0;
int last_obu_size;
// Total size consumed by the packet.
int packet_size = 0;
};
// Parses the payload into serie of OBUs.
static std::vector<Obu> ParseObus(rtc::ArrayView<const uint8_t> payload);
// Returns the number of additional bytes needed to store the previous OBU
// element if an additonal OBU element is added to the packet.
static int AdditionalBytesForPreviousObuElement(const Packet& packet);
// Packetize and try to distribute the payload evenly across packets.
static std::vector<Packet> PacketizeAboutEqually(
rtc::ArrayView<const Obu> obus,
PayloadSizeLimits limits);
static std::vector<Packet> Packetize(rtc::ArrayView<const Obu> obus,
PayloadSizeLimits limits);
uint8_t AggregationHeader() const;
const VideoFrameType frame_type_;
const std::vector<Obu> obus_;
const std::vector<Packet> packets_;
const bool is_last_frame_in_picture_;
size_t packet_index_ = 0;
};
} // namespace webrtc
#endif // MODULES_RTP_RTCP_SOURCE_RTP_PACKETIZER_AV1_H_