1 /*
2 * Copyright (c) 2011 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_format_vp8.h"
12
13 #include <stdint.h>
14 #include <string.h> // memcpy
15
16 #include <vector>
17
18 #include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
19 #include "modules/rtp_rtcp/source/video_rtp_depacketizer_vp8.h"
20 #include "modules/video_coding/codecs/interface/common_constants.h"
21 #include "rtc_base/checks.h"
22 #include "rtc_base/logging.h"
23
24 namespace webrtc {
25 namespace {
26
27 constexpr int kXBit = 0x80;
28 constexpr int kNBit = 0x20;
29 constexpr int kSBit = 0x10;
30 constexpr int kKeyIdxField = 0x1F;
31 constexpr int kIBit = 0x80;
32 constexpr int kLBit = 0x40;
33 constexpr int kTBit = 0x20;
34 constexpr int kKBit = 0x10;
35 constexpr int kYBit = 0x20;
36
ValidateHeader(const RTPVideoHeaderVP8 & hdr_info)37 bool ValidateHeader(const RTPVideoHeaderVP8& hdr_info) {
38 if (hdr_info.pictureId != kNoPictureId) {
39 RTC_DCHECK_GE(hdr_info.pictureId, 0);
40 RTC_DCHECK_LE(hdr_info.pictureId, 0x7FFF);
41 }
42 if (hdr_info.tl0PicIdx != kNoTl0PicIdx) {
43 RTC_DCHECK_GE(hdr_info.tl0PicIdx, 0);
44 RTC_DCHECK_LE(hdr_info.tl0PicIdx, 0xFF);
45 }
46 if (hdr_info.temporalIdx != kNoTemporalIdx) {
47 RTC_DCHECK_GE(hdr_info.temporalIdx, 0);
48 RTC_DCHECK_LE(hdr_info.temporalIdx, 3);
49 } else {
50 RTC_DCHECK(!hdr_info.layerSync);
51 }
52 if (hdr_info.keyIdx != kNoKeyIdx) {
53 RTC_DCHECK_GE(hdr_info.keyIdx, 0);
54 RTC_DCHECK_LE(hdr_info.keyIdx, 0x1F);
55 }
56 return true;
57 }
58
59 } // namespace
60
RtpPacketizerVp8(rtc::ArrayView<const uint8_t> payload,PayloadSizeLimits limits,const RTPVideoHeaderVP8 & hdr_info)61 RtpPacketizerVp8::RtpPacketizerVp8(rtc::ArrayView<const uint8_t> payload,
62 PayloadSizeLimits limits,
63 const RTPVideoHeaderVP8& hdr_info)
64 : hdr_(BuildHeader(hdr_info)), remaining_payload_(payload) {
65 limits.max_payload_len -= hdr_.size();
66 payload_sizes_ = SplitAboutEqually(payload.size(), limits);
67 current_packet_ = payload_sizes_.begin();
68 }
69
70 RtpPacketizerVp8::~RtpPacketizerVp8() = default;
71
NumPackets() const72 size_t RtpPacketizerVp8::NumPackets() const {
73 return payload_sizes_.end() - current_packet_;
74 }
75
NextPacket(RtpPacketToSend * packet)76 bool RtpPacketizerVp8::NextPacket(RtpPacketToSend* packet) {
77 RTC_DCHECK(packet);
78 if (current_packet_ == payload_sizes_.end()) {
79 return false;
80 }
81
82 size_t packet_payload_len = *current_packet_;
83 ++current_packet_;
84
85 uint8_t* buffer = packet->AllocatePayload(hdr_.size() + packet_payload_len);
86 RTC_CHECK(buffer);
87
88 memcpy(buffer, hdr_.data(), hdr_.size());
89 memcpy(buffer + hdr_.size(), remaining_payload_.data(), packet_payload_len);
90
91 remaining_payload_ = remaining_payload_.subview(packet_payload_len);
92 hdr_[0] &= (~kSBit); // Clear 'Start of partition' bit.
93 packet->SetMarker(current_packet_ == payload_sizes_.end());
94 return true;
95 }
96
BuildHeader(const RTPVideoHeaderVP8 & header)97 RtpPacketizerVp8::RawHeader RtpPacketizerVp8::BuildHeader(
98 const RTPVideoHeaderVP8& header) {
99 // VP8 payload descriptor
100 // https://datatracker.ietf.org/doc/html/rfc7741#section-4.2
101 //
102 // 0 1 2 3 4 5 6 7
103 // +-+-+-+-+-+-+-+-+
104 // |X|R|N|S|R| PID | (REQUIRED)
105 // +-+-+-+-+-+-+-+-+
106 // X: |I|L|T|K| RSV | (OPTIONAL)
107 // +-+-+-+-+-+-+-+-+
108 // I: |M| PictureID | (OPTIONAL)
109 // +-+-+-+-+-+-+-+-+
110 // | PictureID |
111 // +-+-+-+-+-+-+-+-+
112 // L: | TL0PICIDX | (OPTIONAL)
113 // +-+-+-+-+-+-+-+-+
114 // T/K: |TID|Y| KEYIDX | (OPTIONAL)
115 // +-+-+-+-+-+-+-+-+
116 RTC_DCHECK(ValidateHeader(header));
117
118 RawHeader result;
119 bool tid_present = header.temporalIdx != kNoTemporalIdx;
120 bool keyid_present = header.keyIdx != kNoKeyIdx;
121 bool tl0_pid_present = header.tl0PicIdx != kNoTl0PicIdx;
122 bool pid_present = header.pictureId != kNoPictureId;
123 uint8_t x_field = 0;
124 if (pid_present)
125 x_field |= kIBit;
126 if (tl0_pid_present)
127 x_field |= kLBit;
128 if (tid_present)
129 x_field |= kTBit;
130 if (keyid_present)
131 x_field |= kKBit;
132
133 uint8_t flags = 0;
134 if (x_field != 0)
135 flags |= kXBit;
136 if (header.nonReference)
137 flags |= kNBit;
138 // Create header as first packet in the frame. NextPacket() will clear it
139 // after first use.
140 flags |= kSBit;
141 result.push_back(flags);
142 if (x_field == 0) {
143 return result;
144 }
145 result.push_back(x_field);
146 if (pid_present) {
147 const uint16_t pic_id = static_cast<uint16_t>(header.pictureId);
148 result.push_back(0x80 | ((pic_id >> 8) & 0x7F));
149 result.push_back(pic_id & 0xFF);
150 }
151 if (tl0_pid_present) {
152 result.push_back(header.tl0PicIdx);
153 }
154 if (tid_present || keyid_present) {
155 uint8_t data_field = 0;
156 if (tid_present) {
157 data_field |= header.temporalIdx << 6;
158 if (header.layerSync)
159 data_field |= kYBit;
160 }
161 if (keyid_present) {
162 data_field |= (header.keyIdx & kKeyIdxField);
163 }
164 result.push_back(data_field);
165 }
166 return result;
167 }
168
169 } // namespace webrtc
170