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