rtp_format_vp8.cc revision f3e4ceee47d747c8868d919c179ecc640b9541f0
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 "webrtc/modules/rtp_rtcp/source/rtp_format_vp8.h"
12
13#include <string.h>  // memcpy
14
15#include <cassert>   // assert
16#include <vector>
17
18#include "webrtc/modules/rtp_rtcp/source/vp8_partition_aggregator.h"
19
20namespace webrtc {
21
22// Define how the VP8PacketizerModes are implemented.
23// Modes are: kStrict, kAggregate, kEqualSize.
24const RtpFormatVp8::AggregationMode RtpFormatVp8::aggr_modes_[kNumModes] =
25    { kAggrNone, kAggrPartitions, kAggrFragments };
26const bool RtpFormatVp8::balance_modes_[kNumModes] =
27    { true, true, true };
28const bool RtpFormatVp8::separate_first_modes_[kNumModes] =
29    { true, false, false };
30
31RtpFormatVp8::RtpFormatVp8(const uint8_t* payload_data,
32                           uint32_t payload_size,
33                           const RTPVideoHeaderVP8& hdr_info,
34                           int max_payload_len,
35                           const RTPFragmentationHeader& fragmentation,
36                           VP8PacketizerMode mode)
37    : payload_data_(payload_data),
38      payload_size_(static_cast<int>(payload_size)),
39      vp8_fixed_payload_descriptor_bytes_(1),
40      aggr_mode_(aggr_modes_[mode]),
41      balance_(balance_modes_[mode]),
42      separate_first_(separate_first_modes_[mode]),
43      hdr_info_(hdr_info),
44      num_partitions_(fragmentation.fragmentationVectorSize),
45      max_payload_len_(max_payload_len),
46      packets_calculated_(false) {
47  part_info_.CopyFrom(fragmentation);
48}
49
50RtpFormatVp8::RtpFormatVp8(const uint8_t* payload_data,
51                           uint32_t payload_size,
52                           const RTPVideoHeaderVP8& hdr_info,
53                           int max_payload_len)
54    : payload_data_(payload_data),
55      payload_size_(static_cast<int>(payload_size)),
56      part_info_(),
57      vp8_fixed_payload_descriptor_bytes_(1),
58      aggr_mode_(aggr_modes_[kEqualSize]),
59      balance_(balance_modes_[kEqualSize]),
60      separate_first_(separate_first_modes_[kEqualSize]),
61      hdr_info_(hdr_info),
62      num_partitions_(1),
63      max_payload_len_(max_payload_len),
64      packets_calculated_(false) {
65    part_info_.VerifyAndAllocateFragmentationHeader(1);
66    part_info_.fragmentationLength[0] = payload_size;
67    part_info_.fragmentationOffset[0] = 0;
68}
69
70RtpFormatVp8::~RtpFormatVp8() {}
71
72int RtpFormatVp8::NextPacket(uint8_t* buffer,
73                             int* bytes_to_send,
74                             bool* last_packet) {
75  if (!packets_calculated_) {
76    int ret = 0;
77    if (aggr_mode_ == kAggrPartitions && balance_) {
78      ret = GeneratePacketsBalancedAggregates();
79    } else {
80      ret = GeneratePackets();
81    }
82    if (ret < 0) {
83      return ret;
84    }
85  }
86  if (packets_.empty()) {
87    return -1;
88  }
89  InfoStruct packet_info = packets_.front();
90  packets_.pop();
91
92  *bytes_to_send = WriteHeaderAndPayload(packet_info, buffer, max_payload_len_);
93  if (*bytes_to_send < 0) {
94    return -1;
95  }
96
97  *last_packet = packets_.empty();
98  return packet_info.first_partition_ix;
99}
100
101int RtpFormatVp8::CalcNextSize(int max_payload_len, int remaining_bytes,
102                               bool split_payload) const {
103  if (max_payload_len == 0 || remaining_bytes == 0) {
104    return 0;
105  }
106  if (!split_payload) {
107    return max_payload_len >= remaining_bytes ? remaining_bytes : 0;
108  }
109
110  if (balance_) {
111    // Balance payload sizes to produce (almost) equal size
112    // fragments.
113    // Number of fragments for remaining_bytes:
114    int num_frags = remaining_bytes / max_payload_len + 1;
115    // Number of bytes in this fragment:
116    return static_cast<int>(static_cast<double>(remaining_bytes)
117                            / num_frags + 0.5);
118  } else {
119    return max_payload_len >= remaining_bytes ? remaining_bytes
120        : max_payload_len;
121  }
122}
123
124int RtpFormatVp8::GeneratePackets() {
125  if (max_payload_len_ < vp8_fixed_payload_descriptor_bytes_
126      + PayloadDescriptorExtraLength() + 1) {
127    // The provided payload length is not long enough for the payload
128    // descriptor and one payload byte. Return an error.
129    return -1;
130  }
131  int total_bytes_processed = 0;
132  bool start_on_new_fragment = true;
133  bool beginning = true;
134  int part_ix = 0;
135  while (total_bytes_processed < payload_size_) {
136    int packet_bytes = 0;  // How much data to send in this packet.
137    bool split_payload = true;  // Splitting of partitions is initially allowed.
138    int remaining_in_partition = part_info_.fragmentationOffset[part_ix] -
139        total_bytes_processed + part_info_.fragmentationLength[part_ix];
140    int rem_payload_len = max_payload_len_ -
141        (vp8_fixed_payload_descriptor_bytes_ + PayloadDescriptorExtraLength());
142    int first_partition_in_packet = part_ix;
143
144    while (int next_size = CalcNextSize(rem_payload_len, remaining_in_partition,
145                                        split_payload)) {
146      packet_bytes += next_size;
147      rem_payload_len -= next_size;
148      remaining_in_partition -= next_size;
149
150      if (remaining_in_partition == 0 && !(beginning && separate_first_)) {
151        // Advance to next partition?
152        // Check that there are more partitions; verify that we are either
153        // allowed to aggregate fragments, or that we are allowed to
154        // aggregate intact partitions and that we started this packet
155        // with an intact partition (indicated by first_fragment_ == true).
156        if (part_ix + 1 < num_partitions_ &&
157            ((aggr_mode_ == kAggrFragments) ||
158                (aggr_mode_ == kAggrPartitions && start_on_new_fragment))) {
159          assert(part_ix < num_partitions_);
160          remaining_in_partition = part_info_.fragmentationLength[++part_ix];
161          // Disallow splitting unless kAggrFragments. In kAggrPartitions,
162          // we can only aggregate intact partitions.
163          split_payload = (aggr_mode_ == kAggrFragments);
164        }
165      } else if (balance_ && remaining_in_partition > 0) {
166        break;
167      }
168    }
169    if (remaining_in_partition == 0) {
170      ++part_ix;  // Advance to next partition.
171    }
172    assert(packet_bytes > 0);
173
174    QueuePacket(total_bytes_processed, packet_bytes, first_partition_in_packet,
175                start_on_new_fragment);
176    total_bytes_processed += packet_bytes;
177    start_on_new_fragment = (remaining_in_partition == 0);
178    beginning = false;  // Next packet cannot be first packet in frame.
179  }
180  packets_calculated_ = true;
181  assert(total_bytes_processed == payload_size_);
182  return 0;
183}
184
185int RtpFormatVp8::GeneratePacketsBalancedAggregates() {
186  if (max_payload_len_ < vp8_fixed_payload_descriptor_bytes_
187      + PayloadDescriptorExtraLength() + 1) {
188    // The provided payload length is not long enough for the payload
189    // descriptor and one payload byte. Return an error.
190    return -1;
191  }
192  std::vector<int> partition_decision;
193  const int overhead = vp8_fixed_payload_descriptor_bytes_ +
194      PayloadDescriptorExtraLength();
195  const uint32_t max_payload_len = max_payload_len_ - overhead;
196  int min_size, max_size;
197  AggregateSmallPartitions(&partition_decision, &min_size, &max_size);
198
199  int total_bytes_processed = 0;
200  int part_ix = 0;
201  while (part_ix < num_partitions_) {
202    if (partition_decision[part_ix] == -1) {
203      // Split large partitions.
204      int remaining_partition = part_info_.fragmentationLength[part_ix];
205      int num_fragments = Vp8PartitionAggregator::CalcNumberOfFragments(
206          remaining_partition, max_payload_len, overhead, min_size, max_size);
207      const int packet_bytes =
208          (remaining_partition + num_fragments - 1) / num_fragments;
209      for (int n = 0; n < num_fragments; ++n) {
210        const int this_packet_bytes = packet_bytes < remaining_partition ?
211            packet_bytes : remaining_partition;
212        QueuePacket(total_bytes_processed, this_packet_bytes, part_ix,
213                    (n == 0));
214        remaining_partition -= this_packet_bytes;
215        total_bytes_processed += this_packet_bytes;
216        if (this_packet_bytes < min_size) {
217          min_size = this_packet_bytes;
218        }
219        if (this_packet_bytes > max_size) {
220          max_size = this_packet_bytes;
221        }
222      }
223      assert(remaining_partition == 0);
224      ++part_ix;
225    } else {
226      int this_packet_bytes = 0;
227      const int first_partition_in_packet = part_ix;
228      const int aggregation_index = partition_decision[part_ix];
229      while (static_cast<size_t>(part_ix) < partition_decision.size() &&
230          partition_decision[part_ix] == aggregation_index) {
231        // Collect all partitions that were aggregated into the same packet.
232        this_packet_bytes += part_info_.fragmentationLength[part_ix];
233        ++part_ix;
234      }
235      QueuePacket(total_bytes_processed, this_packet_bytes,
236                  first_partition_in_packet, true);
237      total_bytes_processed += this_packet_bytes;
238    }
239  }
240  packets_calculated_ = true;
241  return 0;
242}
243
244void RtpFormatVp8::AggregateSmallPartitions(std::vector<int>* partition_vec,
245                                            int* min_size,
246                                            int* max_size) {
247  assert(min_size && max_size);
248  *min_size = -1;
249  *max_size = -1;
250  assert(partition_vec);
251  partition_vec->assign(num_partitions_, -1);
252  const int overhead = vp8_fixed_payload_descriptor_bytes_ +
253      PayloadDescriptorExtraLength();
254  const uint32_t max_payload_len = max_payload_len_ - overhead;
255  int first_in_set = 0;
256  int last_in_set = 0;
257  int num_aggregate_packets = 0;
258  // Find sets of partitions smaller than max_payload_len_.
259  while (first_in_set < num_partitions_) {
260    if (part_info_.fragmentationLength[first_in_set] < max_payload_len) {
261      // Found start of a set.
262      last_in_set = first_in_set;
263      while (last_in_set + 1 < num_partitions_ &&
264          part_info_.fragmentationLength[last_in_set + 1] < max_payload_len) {
265        ++last_in_set;
266      }
267      // Found end of a set. Run optimized aggregator. It is ok if start == end.
268      Vp8PartitionAggregator aggregator(part_info_, first_in_set,
269                                        last_in_set);
270      if (*min_size >= 0 && *max_size >= 0) {
271        aggregator.SetPriorMinMax(*min_size, *max_size);
272      }
273      Vp8PartitionAggregator::ConfigVec optimal_config =
274          aggregator.FindOptimalConfiguration(max_payload_len, overhead);
275      aggregator.CalcMinMax(optimal_config, min_size, max_size);
276      for (int i = first_in_set, j = 0; i <= last_in_set; ++i, ++j) {
277        // Transfer configuration for this set of partitions to the joint
278        // partition vector representing all partitions in the frame.
279        (*partition_vec)[i] = num_aggregate_packets + optimal_config[j];
280      }
281      num_aggregate_packets += optimal_config.back() + 1;
282      first_in_set = last_in_set;
283    }
284    ++first_in_set;
285  }
286}
287
288void RtpFormatVp8::QueuePacket(int start_pos,
289                               int packet_size,
290                               int first_partition_in_packet,
291                               bool start_on_new_fragment) {
292  // Write info to packet info struct and store in packet info queue.
293  InfoStruct packet_info;
294  packet_info.payload_start_pos = start_pos;
295  packet_info.size = packet_size;
296  packet_info.first_partition_ix = first_partition_in_packet;
297  packet_info.first_fragment = start_on_new_fragment;
298  packets_.push(packet_info);
299}
300
301int RtpFormatVp8::WriteHeaderAndPayload(const InfoStruct& packet_info,
302                                        uint8_t* buffer,
303                                        int buffer_length) const {
304  // Write the VP8 payload descriptor.
305  //       0
306  //       0 1 2 3 4 5 6 7 8
307  //      +-+-+-+-+-+-+-+-+-+
308  //      |X| |N|S| PART_ID |
309  //      +-+-+-+-+-+-+-+-+-+
310  // X:   |I|L|T|K|         | (mandatory if any of the below are used)
311  //      +-+-+-+-+-+-+-+-+-+
312  // I:   |PictureID (8/16b)| (optional)
313  //      +-+-+-+-+-+-+-+-+-+
314  // L:   |   TL0PIC_IDX    | (optional)
315  //      +-+-+-+-+-+-+-+-+-+
316  // T/K: |TID:Y|  KEYIDX   | (optional)
317  //      +-+-+-+-+-+-+-+-+-+
318
319  assert(packet_info.size > 0);
320  buffer[0] = 0;
321  if (XFieldPresent())            buffer[0] |= kXBit;
322  if (hdr_info_.nonReference)     buffer[0] |= kNBit;
323  if (packet_info.first_fragment) buffer[0] |= kSBit;
324  buffer[0] |= (packet_info.first_partition_ix & kPartIdField);
325
326  const int extension_length = WriteExtensionFields(buffer, buffer_length);
327
328  memcpy(&buffer[vp8_fixed_payload_descriptor_bytes_ + extension_length],
329         &payload_data_[packet_info.payload_start_pos], packet_info.size);
330
331  // Return total length of written data.
332  return packet_info.size + vp8_fixed_payload_descriptor_bytes_
333      + extension_length;
334}
335
336int RtpFormatVp8::WriteExtensionFields(uint8_t* buffer,
337                                       int buffer_length) const {
338  int extension_length = 0;
339  if (XFieldPresent()) {
340    uint8_t* x_field = buffer + vp8_fixed_payload_descriptor_bytes_;
341    *x_field = 0;
342    extension_length = 1;  // One octet for the X field.
343    if (PictureIdPresent()) {
344      if (WritePictureIDFields(x_field, buffer, buffer_length,
345                               &extension_length) < 0) {
346        return -1;
347      }
348    }
349    if (TL0PicIdxFieldPresent()) {
350      if (WriteTl0PicIdxFields(x_field, buffer, buffer_length,
351                               &extension_length) < 0) {
352        return -1;
353      }
354    }
355    if (TIDFieldPresent() || KeyIdxFieldPresent()) {
356      if (WriteTIDAndKeyIdxFields(x_field, buffer, buffer_length,
357                                  &extension_length) < 0) {
358        return -1;
359      }
360    }
361    assert(extension_length == PayloadDescriptorExtraLength());
362  }
363  return extension_length;
364}
365
366int RtpFormatVp8::WritePictureIDFields(uint8_t* x_field,
367                                       uint8_t* buffer,
368                                       int buffer_length,
369                                       int* extension_length) const {
370  *x_field |= kIBit;
371  const int pic_id_length = WritePictureID(
372      buffer + vp8_fixed_payload_descriptor_bytes_ + *extension_length,
373      buffer_length - vp8_fixed_payload_descriptor_bytes_
374      - *extension_length);
375  if (pic_id_length < 0) return -1;
376  *extension_length += pic_id_length;
377  return 0;
378}
379
380int RtpFormatVp8::WritePictureID(uint8_t* buffer,
381                                 int buffer_length) const {
382  const uint16_t pic_id =
383      static_cast<uint16_t> (hdr_info_.pictureId);
384  int picture_id_len = PictureIdLength();
385  if (picture_id_len > buffer_length) return -1;
386  if (picture_id_len == 2) {
387    buffer[0] = 0x80 | ((pic_id >> 8) & 0x7F);
388    buffer[1] = pic_id & 0xFF;
389  } else if (picture_id_len == 1) {
390    buffer[0] = pic_id & 0x7F;
391  }
392  return picture_id_len;
393}
394
395int RtpFormatVp8::WriteTl0PicIdxFields(uint8_t* x_field,
396                                       uint8_t* buffer,
397                                       int buffer_length,
398                                       int* extension_length) const {
399  if (buffer_length < vp8_fixed_payload_descriptor_bytes_ + *extension_length
400      + 1) {
401    return -1;
402  }
403  *x_field |= kLBit;
404  buffer[vp8_fixed_payload_descriptor_bytes_
405         + *extension_length] = hdr_info_.tl0PicIdx;
406  ++*extension_length;
407  return 0;
408}
409
410int RtpFormatVp8::WriteTIDAndKeyIdxFields(uint8_t* x_field,
411                                          uint8_t* buffer,
412                                          int buffer_length,
413                                          int* extension_length) const {
414  if (buffer_length < vp8_fixed_payload_descriptor_bytes_ + *extension_length
415      + 1) {
416    return -1;
417  }
418  uint8_t* data_field =
419      &buffer[vp8_fixed_payload_descriptor_bytes_ + *extension_length];
420  *data_field = 0;
421  if (TIDFieldPresent()) {
422    *x_field |= kTBit;
423    assert(hdr_info_.temporalIdx >= 0 && hdr_info_.temporalIdx <= 3);
424    *data_field |= hdr_info_.temporalIdx << 6;
425    *data_field |= hdr_info_.layerSync ? kYBit : 0;
426  }
427  if (KeyIdxFieldPresent()) {
428    *x_field |= kKBit;
429    *data_field |= (hdr_info_.keyIdx & kKeyIdxField);
430  }
431  ++*extension_length;
432  return 0;
433}
434
435int RtpFormatVp8::PayloadDescriptorExtraLength() const {
436  int length_bytes = PictureIdLength();
437  if (TL0PicIdxFieldPresent()) ++length_bytes;
438  if (TIDFieldPresent() || KeyIdxFieldPresent()) ++length_bytes;
439  if (length_bytes > 0) ++length_bytes;  // Include the extension field.
440  return length_bytes;
441}
442
443int RtpFormatVp8::PictureIdLength() const {
444  if (hdr_info_.pictureId == kNoPictureId) {
445    return 0;
446  }
447  if (hdr_info_.pictureId <= 0x7F) {
448    return 1;
449  }
450  return 2;
451}
452
453bool RtpFormatVp8::XFieldPresent() const {
454  return (TIDFieldPresent() || TL0PicIdxFieldPresent() || PictureIdPresent()
455      || KeyIdxFieldPresent());
456}
457
458bool RtpFormatVp8::TIDFieldPresent() const {
459  assert((hdr_info_.layerSync == false) ||
460         (hdr_info_.temporalIdx != kNoTemporalIdx));
461  return (hdr_info_.temporalIdx != kNoTemporalIdx);
462}
463
464bool RtpFormatVp8::KeyIdxFieldPresent() const {
465  return (hdr_info_.keyIdx != kNoKeyIdx);
466}
467
468bool RtpFormatVp8::TL0PicIdxFieldPresent() const {
469  return (hdr_info_.tl0PicIdx != kNoTl0PicIdx);
470}
471}  // namespace webrtc
472