Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / rtp_rtcp / source / rtp_format_vp8.h
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 /*
12  * This file contains the declaration of the VP8 packetizer class.
13  * A packetizer object is created for each encoded video frame. The
14  * constructor is called with the payload data and size,
15  * together with the fragmentation information and a packetizer mode
16  * of choice. Alternatively, if no fragmentation info is available, the
17  * second constructor can be used with only payload data and size; in that
18  * case the mode kEqualSize is used.
19  *
20  * After creating the packetizer, the method NextPacket is called
21  * repeatedly to get all packets for the frame. The method returns
22  * false as long as there are more packets left to fetch.
23  */
24
25 #ifndef WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_FORMAT_VP8_H_
26 #define WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_FORMAT_VP8_H_
27
28 #include <queue>
29 #include <string>
30 #include <vector>
31
32 #include "webrtc/base/constructormagic.h"
33 #include "webrtc/modules/interface/module_common_types.h"
34 #include "webrtc/modules/rtp_rtcp/source/rtp_format.h"
35 #include "webrtc/typedefs.h"
36
37 namespace webrtc {
38
39 enum VP8PacketizerMode {
40   kStrict = 0,  // Split partitions if too large;
41                 // never aggregate, balance size.
42   kAggregate,   // Split partitions if too large; aggregate whole partitions.
43   kEqualSize,   // Split entire payload without considering partition limits.
44                 // This will produce equal size packets for the whole frame.
45   kNumModes,
46 };
47
48 // Packetizer for VP8.
49 class RtpPacketizerVp8 : public RtpPacketizer {
50  public:
51   // Initialize with payload from encoder and fragmentation info.
52   // The payload_data must be exactly one encoded VP8 frame.
53   RtpPacketizerVp8(const RTPVideoHeaderVP8& hdr_info,
54                    int max_payload_len,
55                    VP8PacketizerMode mode);
56
57   // Initialize without fragmentation info. Mode kEqualSize will be used.
58   // The payload_data must be exactly one encoded VP8 frame.
59   RtpPacketizerVp8(const RTPVideoHeaderVP8& hdr_info, int max_payload_len);
60
61   virtual ~RtpPacketizerVp8();
62
63   virtual void SetPayloadData(
64       const uint8_t* payload_data,
65       size_t payload_size,
66       const RTPFragmentationHeader* fragmentation) OVERRIDE;
67
68   // Get the next payload with VP8 payload header.
69   // max_payload_len limits the sum length of payload and VP8 payload header.
70   // buffer is a pointer to where the output will be written.
71   // bytes_to_send is an output variable that will contain number of bytes
72   // written to buffer. Parameter last_packet is true for the last packet of
73   // the frame, false otherwise (i.e., call the function again to get the
74   // next packet).
75   // For the kStrict and kAggregate mode: returns the partition index from which
76   // the first payload byte in the packet is taken, with the first partition
77   // having index 0; returns negative on error.
78   // For the kEqualSize mode: returns 0 on success, return negative on error.
79   virtual bool NextPacket(uint8_t* buffer,
80                           size_t* bytes_to_send,
81                           bool* last_packet) OVERRIDE;
82
83   virtual ProtectionType GetProtectionType() OVERRIDE;
84
85   virtual StorageType GetStorageType(uint32_t retransmission_settings) OVERRIDE;
86
87   virtual std::string ToString() OVERRIDE;
88
89  private:
90   typedef struct {
91     int payload_start_pos;
92     int size;
93     bool first_fragment;
94     int first_partition_ix;
95   } InfoStruct;
96   typedef std::queue<InfoStruct> InfoQueue;
97   enum AggregationMode {
98     kAggrNone = 0,    // No aggregation.
99     kAggrPartitions,  // Aggregate intact partitions.
100     kAggrFragments    // Aggregate intact and fragmented partitions.
101   };
102
103   static const AggregationMode aggr_modes_[kNumModes];
104   static const bool balance_modes_[kNumModes];
105   static const bool separate_first_modes_[kNumModes];
106   static const int kXBit = 0x80;
107   static const int kNBit = 0x20;
108   static const int kSBit = 0x10;
109   static const int kPartIdField = 0x0F;
110   static const int kKeyIdxField = 0x1F;
111   static const int kIBit = 0x80;
112   static const int kLBit = 0x40;
113   static const int kTBit = 0x20;
114   static const int kKBit = 0x10;
115   static const int kYBit = 0x20;
116
117   // Calculate size of next chunk to send. Returns 0 if none can be sent.
118   int CalcNextSize(int max_payload_len,
119                    int remaining_bytes,
120                    bool split_payload) const;
121
122   // Calculate all packet sizes and load to packet info queue.
123   int GeneratePackets();
124
125   // Calculate all packet sizes using Vp8PartitionAggregator and load to packet
126   // info queue.
127   int GeneratePacketsBalancedAggregates();
128
129   // Helper function to GeneratePacketsBalancedAggregates(). Find all
130   // continuous sets of partitions smaller than the max payload size (not
131   // max_size), and aggregate them into balanced packets. The result is written
132   // to partition_vec, which is of the same length as the number of partitions.
133   // A value of -1 indicates that the partition is too large and must be split.
134   // Aggregates are numbered 0, 1, 2, etc. For each set of small partitions,
135   // the aggregate numbers restart at 0. Output values min_size and max_size
136   // will hold the smallest and largest resulting aggregates (i.e., not counting
137   // those that must be split).
138   void AggregateSmallPartitions(std::vector<int>* partition_vec,
139                                 int* min_size,
140                                 int* max_size);
141
142   // Insert packet into packet queue.
143   void QueuePacket(int start_pos,
144                    int packet_size,
145                    int first_partition_in_packet,
146                    bool start_on_new_fragment);
147
148   // Write the payload header and copy the payload to the buffer.
149   // The info in packet_info determines which part of the payload is written
150   // and what to write in the header fields.
151   int WriteHeaderAndPayload(const InfoStruct& packet_info,
152                             uint8_t* buffer,
153                             int buffer_length) const;
154
155   // Write the X field and the appropriate extension fields to buffer.
156   // The function returns the extension length (including X field), or -1
157   // on error.
158   int WriteExtensionFields(uint8_t* buffer, int buffer_length) const;
159
160   // Set the I bit in the x_field, and write PictureID to the appropriate
161   // position in buffer. The function returns 0 on success, -1 otherwise.
162   int WritePictureIDFields(uint8_t* x_field,
163                            uint8_t* buffer,
164                            int buffer_length,
165                            int* extension_length) const;
166
167   // Set the L bit in the x_field, and write Tl0PicIdx to the appropriate
168   // position in buffer. The function returns 0 on success, -1 otherwise.
169   int WriteTl0PicIdxFields(uint8_t* x_field,
170                            uint8_t* buffer,
171                            int buffer_length,
172                            int* extension_length) const;
173
174   // Set the T and K bits in the x_field, and write TID, Y and KeyIdx to the
175   // appropriate position in buffer. The function returns 0 on success,
176   // -1 otherwise.
177   int WriteTIDAndKeyIdxFields(uint8_t* x_field,
178                               uint8_t* buffer,
179                               int buffer_length,
180                               int* extension_length) const;
181
182   // Write the PictureID from codec_specific_info_ to buffer. One or two
183   // bytes are written, depending on magnitude of PictureID. The function
184   // returns the number of bytes written.
185   int WritePictureID(uint8_t* buffer, int buffer_length) const;
186
187   // Calculate and return length (octets) of the variable header fields in
188   // the next header (i.e., header length in addition to vp8_header_bytes_).
189   int PayloadDescriptorExtraLength() const;
190
191   // Calculate and return length (octets) of PictureID field in the next
192   // header. Can be 0, 1, or 2.
193   int PictureIdLength() const;
194
195   // Check whether each of the optional fields will be included in the header.
196   bool XFieldPresent() const;
197   bool TIDFieldPresent() const;
198   bool KeyIdxFieldPresent() const;
199   bool TL0PicIdxFieldPresent() const;
200   bool PictureIdPresent() const { return (PictureIdLength() > 0); }
201
202   const uint8_t* payload_data_;
203   int payload_size_;
204   RTPFragmentationHeader part_info_;
205   const int vp8_fixed_payload_descriptor_bytes_;  // Length of VP8 payload
206                                                   // descriptors's fixed part.
207   const AggregationMode aggr_mode_;
208   const bool balance_;
209   const bool separate_first_;
210   const RTPVideoHeaderVP8 hdr_info_;
211   int num_partitions_;
212   const int max_payload_len_;
213   InfoQueue packets_;
214   bool packets_calculated_;
215
216   DISALLOW_COPY_AND_ASSIGN(RtpPacketizerVp8);
217 };
218
219 // Depacketizer for VP8.
220 class RtpDepacketizerVp8 : public RtpDepacketizer {
221  public:
222   virtual ~RtpDepacketizerVp8() {}
223
224   virtual bool Parse(ParsedPayload* parsed_payload,
225                      const uint8_t* payload_data,
226                      size_t payload_data_length) OVERRIDE;
227 };
228 }  // namespace webrtc
229 #endif  // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_FORMAT_VP8_H_