blob: cc925261775e7c4aeb5730e3b106da53ebdeffad [file] [log] [blame]
Danil Chapovalov49470c22019-11-14 17:33:55 +01001/*
2 * Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "modules/rtp_rtcp/source/rtp_depacketizer_av1.h"
12
13#include <stddef.h>
14#include <stdint.h>
15
16#include "modules/rtp_rtcp/source/rtp_video_header.h"
17#include "rtc_base/byte_buffer.h"
18#include "rtc_base/checks.h"
19#include "rtc_base/logging.h"
20
21namespace webrtc {
22namespace {
23// AV1 format:
24//
25// RTP payload syntax:
26// 0 1 2 3 4 5 6 7
27// +-+-+-+-+-+-+-+-+
28// |Z|Y| W |-|-|-|-| (REQUIRED)
29// +=+=+=+=+=+=+=+=+ (REPEATED W-1 times, or any times if W = 0)
30// |1| |
31// +-+ OBU fragment|
32// |1| | (REQUIRED, leb128 encoded)
33// +-+ size |
34// |0| |
35// +-+-+-+-+-+-+-+-+
36// | OBU fragment |
37// | ... |
38// +=+=+=+=+=+=+=+=+
39// | ... |
40// +=+=+=+=+=+=+=+=+ if W > 0, last fragment MUST NOT have size field
41// | OBU fragment |
42// | ... |
43// +=+=+=+=+=+=+=+=+
44//
45//
46// OBU syntax:
47// 0 1 2 3 4 5 6 7
48// +-+-+-+-+-+-+-+-+
49// |0| type |X|S|-| (REQUIRED)
50// +-+-+-+-+-+-+-+-+
51// X: | TID |SID|-|-|-| (OPTIONAL)
52// +-+-+-+-+-+-+-+-+
53// |1| |
54// +-+ OBU payload |
55// S: |1| | (OPTIONAL, variable length leb128 encoded)
56// +-+ size |
57// |0| |
58// +-+-+-+-+-+-+-+-+
59// | OBU payload |
60// | ... |
61constexpr int kObuTypeSequenceHeader = 1;
62
63int ObuType(uint8_t obu_header) {
64 return (obu_header & 0b0'1111'000u) >> 3;
65}
66
67bool RtpStartsWithFragment(uint8_t aggregation_header) {
68 return aggregation_header & 0b1000'0000u;
69}
70bool RtpEndsWithFragment(uint8_t aggregation_header) {
71 return aggregation_header & 0b0100'0000u;
72}
73int RtpNumObus(uint8_t aggregation_header) { // 0 for any number of obus.
74 return (aggregation_header & 0b0011'0000u) >> 4;
75}
76
77} // namespace
78
79bool RtpDepacketizerAv1::Parse(ParsedPayload* parsed_payload,
80 const uint8_t* payload_data,
81 size_t payload_data_length) {
82 RTC_DCHECK(parsed_payload);
83 if (payload_data_length == 0) {
84 RTC_DLOG(LS_ERROR) << "Empty rtp payload.";
85 return false;
86 }
87 // To assemble frame, all of the rtp payload is required, including
88 // aggregation header.
89 parsed_payload->payload = payload_data;
90 parsed_payload->payload_length = payload_data_length;
91
92 rtc::ByteBufferReader payload(reinterpret_cast<const char*>(payload_data),
93 payload_data_length);
94 uint8_t aggregation_header;
95 RTC_CHECK(payload.ReadUInt8(&aggregation_header));
96
97 // TODO(danilchap): Set AV1 codec when there is such enum value
98 parsed_payload->video.codec = VideoCodecType::kVideoCodecGeneric;
99 // These are not accurate since frame may consist of several packet aligned
100 // chunks of obus, but should be good enough for most cases. It might produce
101 // frame that do not map to any real frame, but av1 decoder should be able to
102 // handle it since it promise to handle individual obus rather than full
103 // frames.
104 parsed_payload->video.is_first_packet_in_frame =
105 !RtpStartsWithFragment(aggregation_header);
106 parsed_payload->video.is_last_packet_in_frame =
107 !RtpEndsWithFragment(aggregation_header);
108 parsed_payload->video.frame_type = VideoFrameType::kVideoFrameDelta;
109 // If packet starts a frame, check if it contains Sequence Header OBU.
110 // In that case treat it as key frame packet.
111 if (parsed_payload->video.is_first_packet_in_frame) {
112 int num_expected_obus = RtpNumObus(aggregation_header);
113
114 // The only OBU that can preceed SequenceHeader is a TemporalDelimiter OBU,
115 // so check no more than two OBUs while searching for SH.
116 for (int obu_index = 1; payload.Length() > 0 && obu_index <= 2;
117 ++obu_index) {
118 uint64_t fragment_size;
119 // When num_expected_obus > 0, last OBU (fragment) is not preceeded by
120 // the size field. See W field in
121 // https://aomediacodec.github.io/av1-rtp-spec/#43-av1-aggregation-header
122 bool has_fragment_size = (obu_index != num_expected_obus);
123 if (has_fragment_size) {
124 if (!payload.ReadUVarint(&fragment_size)) {
125 RTC_DLOG(LS_WARNING)
126 << "Failed to read OBU fragment size for OBU#" << obu_index;
127 return false;
128 }
129 if (fragment_size > payload.Length()) {
130 RTC_DLOG(LS_WARNING) << "OBU fragment size " << fragment_size
131 << " exceeds remaining payload size "
132 << payload.Length() << " for OBU#" << obu_index;
133 // Malformed input: written size is larger than remaining buffer.
134 return false;
135 }
136 } else {
137 fragment_size = payload.Length();
138 }
139 // Though it is inpractical to pass empty fragments, it is allowed.
140 if (fragment_size == 0) {
141 RTC_LOG(LS_WARNING)
142 << "Weird obu of size 0 at offset "
143 << (payload_data_length - payload.Length()) << ", skipping.";
144 continue;
145 }
146 uint8_t obu_header = *reinterpret_cast<const uint8_t*>(payload.Data());
147 if (ObuType(obu_header) == kObuTypeSequenceHeader) {
148 // TODO(bugs.webrtc.org/11042): Check frame_header OBU and/or frame OBU
149 // too for other conditions of the start of a new coded video sequence.
150 // For proper checks checking single packet might not be enough. See
151 // https://aomediacodec.github.io/av1-spec/av1-spec.pdf section 7.5
152 parsed_payload->video.frame_type = VideoFrameType::kVideoFrameKey;
153 break;
154 }
155 payload.Consume(fragment_size);
156 }
157 }
158
159 return true;
160}
161
162} // namespace webrtc