blob: 611007afeb59a679d9d7920bb21fb16ed079e7ad [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
pwestin@webrtc.orgddab60b2012-04-23 14:52:15 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
Henrik Kjellander2557b862015-11-18 22:00:21 +010011#include "webrtc/modules/video_coding/include/video_coding_defines.h"
12#include "webrtc/modules/video_coding/encoded_frame.h"
13#include "webrtc/modules/video_coding/generic_encoder.h"
14#include "webrtc/modules/video_coding/jitter_buffer_common.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000015
16namespace webrtc {
17
18VCMEncodedFrame::VCMEncodedFrame()
guoweis@webrtc.org54d072e2015-03-17 21:54:50 +000019 : webrtc::EncodedImage(),
20 _renderTimeMs(-1),
21 _payloadType(0),
22 _missingFrame(false),
23 _codec(kVideoCodecUnknown),
24 _fragmentation(),
25 _rotation(kVideoRotation_0),
26 _rotation_set(false) {
henrik.lundin@webrtc.org473bac82011-08-17 09:47:33 +000027 _codecSpecificInfo.codecType = kVideoCodecUnknown;
niklase@google.com470e71d2011-07-07 08:21:25 +000028}
29
30VCMEncodedFrame::VCMEncodedFrame(const webrtc::EncodedImage& rhs)
guoweis@webrtc.org54d072e2015-03-17 21:54:50 +000031 : webrtc::EncodedImage(rhs),
32 _renderTimeMs(-1),
33 _payloadType(0),
34 _missingFrame(false),
35 _codec(kVideoCodecUnknown),
36 _fragmentation(),
37 _rotation(kVideoRotation_0),
38 _rotation_set(false) {
henrik.lundin@webrtc.org473bac82011-08-17 09:47:33 +000039 _codecSpecificInfo.codecType = kVideoCodecUnknown;
niklase@google.com470e71d2011-07-07 08:21:25 +000040 _buffer = NULL;
ajm@google.combb93f1d2011-07-27 23:58:56 +000041 _size = 0;
42 _length = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000043 if (rhs._buffer != NULL)
44 {
45 VerifyAndAllocate(rhs._length);
46 memcpy(_buffer, rhs._buffer, rhs._length);
47 }
48}
49
50VCMEncodedFrame::VCMEncodedFrame(const VCMEncodedFrame& rhs)
guoweis@webrtc.org54d072e2015-03-17 21:54:50 +000051 : webrtc::EncodedImage(rhs),
52 _renderTimeMs(rhs._renderTimeMs),
53 _payloadType(rhs._payloadType),
54 _missingFrame(rhs._missingFrame),
55 _codecSpecificInfo(rhs._codecSpecificInfo),
56 _codec(rhs._codec),
57 _fragmentation(),
58 _rotation(rhs._rotation),
59 _rotation_set(rhs._rotation_set) {
stefan@webrtc.orgc3d89102011-09-08 06:50:28 +000060 _buffer = NULL;
61 _size = 0;
62 _length = 0;
63 if (rhs._buffer != NULL)
64 {
mikhal@webrtc.org352ade72011-12-13 00:54:28 +000065 VerifyAndAllocate(rhs._length);
stefan@webrtc.orgc3d89102011-09-08 06:50:28 +000066 memcpy(_buffer, rhs._buffer, rhs._length);
henrik.lundin@webrtc.orgbaf6db52011-11-02 18:58:39 +000067 _length = rhs._length;
stefan@webrtc.orgc3d89102011-09-08 06:50:28 +000068 }
andrew@webrtc.org418443c2012-11-23 19:17:23 +000069 _fragmentation.CopyFrom(rhs._fragmentation);
niklase@google.com470e71d2011-07-07 08:21:25 +000070}
71
72VCMEncodedFrame::~VCMEncodedFrame()
73{
74 Free();
75}
76
77void VCMEncodedFrame::Free()
78{
79 Reset();
80 if (_buffer != NULL)
81 {
82 delete [] _buffer;
83 _buffer = NULL;
84 }
85}
86
87void VCMEncodedFrame::Reset()
88{
89 _renderTimeMs = -1;
90 _timeStamp = 0;
91 _payloadType = 0;
Peter Boström49e196a2015-10-23 15:58:18 +020092 _frameType = kVideoFrameDelta;
niklase@google.com470e71d2011-07-07 08:21:25 +000093 _encodedWidth = 0;
94 _encodedHeight = 0;
95 _completeFrame = false;
96 _missingFrame = false;
97 _length = 0;
henrik.lundin@webrtc.org473bac82011-08-17 09:47:33 +000098 _codecSpecificInfo.codecType = kVideoCodecUnknown;
niklase@google.com470e71d2011-07-07 08:21:25 +000099 _codec = kVideoCodecUnknown;
guoweis@webrtc.org54d072e2015-03-17 21:54:50 +0000100 _rotation = kVideoRotation_0;
101 _rotation_set = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000102}
103
henrik.lundin@webrtc.org473bac82011-08-17 09:47:33 +0000104void VCMEncodedFrame::CopyCodecSpecific(const RTPVideoHeader* header)
105{
stefan@webrtc.org2ec56062014-07-31 14:59:24 +0000106 if (header) {
107 switch (header->codec) {
108 case kRtpVideoVp8: {
109 if (_codecSpecificInfo.codecType != kVideoCodecVP8) {
110 // This is the first packet for this frame.
111 _codecSpecificInfo.codecSpecific.VP8.pictureId = -1;
112 _codecSpecificInfo.codecSpecific.VP8.temporalIdx = 0;
113 _codecSpecificInfo.codecSpecific.VP8.layerSync = false;
114 _codecSpecificInfo.codecSpecific.VP8.keyIdx = -1;
115 _codecSpecificInfo.codecType = kVideoCodecVP8;
henrik.lundin@webrtc.org473bac82011-08-17 09:47:33 +0000116 }
stefan@webrtc.org2ec56062014-07-31 14:59:24 +0000117 _codecSpecificInfo.codecSpecific.VP8.nonReference =
118 header->codecHeader.VP8.nonReference;
119 if (header->codecHeader.VP8.pictureId != kNoPictureId) {
120 _codecSpecificInfo.codecSpecific.VP8.pictureId =
121 header->codecHeader.VP8.pictureId;
122 }
123 if (header->codecHeader.VP8.temporalIdx != kNoTemporalIdx) {
124 _codecSpecificInfo.codecSpecific.VP8.temporalIdx =
125 header->codecHeader.VP8.temporalIdx;
126 _codecSpecificInfo.codecSpecific.VP8.layerSync =
127 header->codecHeader.VP8.layerSync;
128 }
129 if (header->codecHeader.VP8.keyIdx != kNoKeyIdx) {
130 _codecSpecificInfo.codecSpecific.VP8.keyIdx =
131 header->codecHeader.VP8.keyIdx;
132 }
133 break;
134 }
asaperssona9455ab2015-07-31 06:10:09 -0700135 case kRtpVideoVp9: {
136 if (_codecSpecificInfo.codecType != kVideoCodecVP9) {
137 // This is the first packet for this frame.
138 _codecSpecificInfo.codecSpecific.VP9.picture_id = -1;
139 _codecSpecificInfo.codecSpecific.VP9.temporal_idx = 0;
140 _codecSpecificInfo.codecSpecific.VP9.spatial_idx = 0;
141 _codecSpecificInfo.codecSpecific.VP9.gof_idx = 0;
142 _codecSpecificInfo.codecSpecific.VP9.inter_layer_predicted = false;
143 _codecSpecificInfo.codecSpecific.VP9.tl0_pic_idx = -1;
144 _codecSpecificInfo.codecType = kVideoCodecVP9;
145 }
146 _codecSpecificInfo.codecSpecific.VP9.inter_pic_predicted =
147 header->codecHeader.VP9.inter_pic_predicted;
148 _codecSpecificInfo.codecSpecific.VP9.flexible_mode =
149 header->codecHeader.VP9.flexible_mode;
philipelcfc319b2015-11-10 07:17:23 -0800150 _codecSpecificInfo.codecSpecific.VP9.num_ref_pics =
151 header->codecHeader.VP9.num_ref_pics;
152 for (uint8_t r = 0; r < header->codecHeader.VP9.num_ref_pics; ++r) {
153 _codecSpecificInfo.codecSpecific.VP9.p_diff[r] =
154 header->codecHeader.VP9.pid_diff[r];
155 }
asaperssona9455ab2015-07-31 06:10:09 -0700156 _codecSpecificInfo.codecSpecific.VP9.ss_data_available =
157 header->codecHeader.VP9.ss_data_available;
158 if (header->codecHeader.VP9.picture_id != kNoPictureId) {
159 _codecSpecificInfo.codecSpecific.VP9.picture_id =
160 header->codecHeader.VP9.picture_id;
161 }
162 if (header->codecHeader.VP9.tl0_pic_idx != kNoTl0PicIdx) {
163 _codecSpecificInfo.codecSpecific.VP9.tl0_pic_idx =
164 header->codecHeader.VP9.tl0_pic_idx;
165 }
166 if (header->codecHeader.VP9.temporal_idx != kNoTemporalIdx) {
167 _codecSpecificInfo.codecSpecific.VP9.temporal_idx =
168 header->codecHeader.VP9.temporal_idx;
169 _codecSpecificInfo.codecSpecific.VP9.temporal_up_switch =
170 header->codecHeader.VP9.temporal_up_switch;
171 }
172 if (header->codecHeader.VP9.spatial_idx != kNoSpatialIdx) {
173 _codecSpecificInfo.codecSpecific.VP9.spatial_idx =
174 header->codecHeader.VP9.spatial_idx;
175 _codecSpecificInfo.codecSpecific.VP9.inter_layer_predicted =
176 header->codecHeader.VP9.inter_layer_predicted;
177 }
178 if (header->codecHeader.VP9.gof_idx != kNoGofIdx) {
179 _codecSpecificInfo.codecSpecific.VP9.gof_idx =
180 header->codecHeader.VP9.gof_idx;
181 }
182 if (header->codecHeader.VP9.ss_data_available) {
183 _codecSpecificInfo.codecSpecific.VP9.num_spatial_layers =
184 header->codecHeader.VP9.num_spatial_layers;
185 _codecSpecificInfo.codecSpecific.VP9
186 .spatial_layer_resolution_present =
187 header->codecHeader.VP9.spatial_layer_resolution_present;
188 if (header->codecHeader.VP9.spatial_layer_resolution_present) {
189 for (size_t i = 0; i < header->codecHeader.VP9.num_spatial_layers;
190 ++i) {
191 _codecSpecificInfo.codecSpecific.VP9.width[i] =
192 header->codecHeader.VP9.width[i];
193 _codecSpecificInfo.codecSpecific.VP9.height[i] =
194 header->codecHeader.VP9.height[i];
195 }
196 }
197 _codecSpecificInfo.codecSpecific.VP9.gof.CopyGofInfoVP9(
198 header->codecHeader.VP9.gof);
199 }
200 break;
201 }
stefan@webrtc.org2ec56062014-07-31 14:59:24 +0000202 case kRtpVideoH264: {
203 _codecSpecificInfo.codecType = kVideoCodecH264;
204 break;
205 }
206 default: {
207 _codecSpecificInfo.codecType = kVideoCodecUnknown;
208 break;
209 }
henrik.lundin@webrtc.org473bac82011-08-17 09:47:33 +0000210 }
stefan@webrtc.org2ec56062014-07-31 14:59:24 +0000211 }
henrik.lundin@webrtc.org473bac82011-08-17 09:47:33 +0000212}
213
stefan@webrtc.orgc3d89102011-09-08 06:50:28 +0000214const RTPFragmentationHeader* VCMEncodedFrame::FragmentationHeader() const {
215 return &_fragmentation;
216}
217
Magnus Jedvertab004042015-05-05 11:37:12 +0200218void VCMEncodedFrame::VerifyAndAllocate(size_t minimumSize)
niklase@google.com470e71d2011-07-07 08:21:25 +0000219{
220 if(minimumSize > _size)
221 {
222 // create buffer of sufficient size
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +0000223 uint8_t* newBuffer = new uint8_t[minimumSize];
niklase@google.com470e71d2011-07-07 08:21:25 +0000224 if(_buffer)
225 {
226 // copy old data
227 memcpy(newBuffer, _buffer, _size);
228 delete [] _buffer;
229 }
230 _buffer = newBuffer;
231 _size = minimumSize;
232 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000233}
234
pbos22993e12015-10-19 02:39:06 -0700235} // namespace webrtc