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 
97 // Write the VP8 payload descriptor.
98 //       0
99 //       0 1 2 3 4 5 6 7 8
100 //      +-+-+-+-+-+-+-+-+-+
101 //      |X| |N|S| PART_ID |
102 //      +-+-+-+-+-+-+-+-+-+
103 // X:   |I|L|T|K|         | (mandatory if any of the below are used)
104 //      +-+-+-+-+-+-+-+-+-+
105 // I:   |PictureID   (16b)| (optional)
106 //      +-+-+-+-+-+-+-+-+-+
107 // L:   |   TL0PIC_IDX    | (optional)
108 //      +-+-+-+-+-+-+-+-+-+
109 // T/K: |TID:Y|  KEYIDX   | (optional)
110 //      +-+-+-+-+-+-+-+-+-+
BuildHeader(const RTPVideoHeaderVP8 & header)111 RtpPacketizerVp8::RawHeader RtpPacketizerVp8::BuildHeader(
112     const RTPVideoHeaderVP8& header) {
113   RTC_DCHECK(ValidateHeader(header));
114 
115   RawHeader result;
116   bool tid_present = header.temporalIdx != kNoTemporalIdx;
117   bool keyid_present = header.keyIdx != kNoKeyIdx;
118   bool tl0_pid_present = header.tl0PicIdx != kNoTl0PicIdx;
119   bool pid_present = header.pictureId != kNoPictureId;
120   uint8_t x_field = 0;
121   if (pid_present)
122     x_field |= kIBit;
123   if (tl0_pid_present)
124     x_field |= kLBit;
125   if (tid_present)
126     x_field |= kTBit;
127   if (keyid_present)
128     x_field |= kKBit;
129 
130   uint8_t flags = 0;
131   if (x_field != 0)
132     flags |= kXBit;
133   if (header.nonReference)
134     flags |= kNBit;
135   // Create header as first packet in the frame. NextPacket() will clear it
136   // after first use.
137   flags |= kSBit;
138   result.push_back(flags);
139   if (x_field == 0) {
140     return result;
141   }
142   result.push_back(x_field);
143   if (pid_present) {
144     const uint16_t pic_id = static_cast<uint16_t>(header.pictureId);
145     result.push_back(0x80 | ((pic_id >> 8) & 0x7F));
146     result.push_back(pic_id & 0xFF);
147   }
148   if (tl0_pid_present) {
149     result.push_back(header.tl0PicIdx);
150   }
151   if (tid_present || keyid_present) {
152     uint8_t data_field = 0;
153     if (tid_present) {
154       data_field |= header.temporalIdx << 6;
155       if (header.layerSync)
156         data_field |= kYBit;
157     }
158     if (keyid_present) {
159       data_field |= (header.keyIdx & kKeyIdxField);
160     }
161     result.push_back(data_field);
162   }
163   return result;
164 }
165 
166 }  // namespace webrtc
167