1 /*
2  *  Copyright (c) 2012 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/video_coding/encoded_frame.h"
12 
13 #include <string.h>
14 
15 #include "absl/types/variant.h"
16 #include "api/video/video_timing.h"
17 #include "modules/video_coding/codecs/interface/common_constants.h"
18 #include "modules/video_coding/codecs/vp8/include/vp8_globals.h"
19 #include "modules/video_coding/codecs/vp9/include/vp9_globals.h"
20 
21 namespace webrtc {
22 
VCMEncodedFrame()23 VCMEncodedFrame::VCMEncodedFrame()
24     : webrtc::EncodedImage(),
25       _renderTimeMs(-1),
26       _payloadType(0),
27       _missingFrame(false),
28       _codec(kVideoCodecGeneric) {
29   _codecSpecificInfo.codecType = kVideoCodecGeneric;
30 }
31 
32 VCMEncodedFrame::VCMEncodedFrame(const VCMEncodedFrame&) = default;
33 
~VCMEncodedFrame()34 VCMEncodedFrame::~VCMEncodedFrame() {
35   Reset();
36 }
37 
Reset()38 void VCMEncodedFrame::Reset() {
39   SetTimestamp(0);
40   SetSpatialIndex(absl::nullopt);
41   _renderTimeMs = -1;
42   _payloadType = 0;
43   _frameType = VideoFrameType::kVideoFrameDelta;
44   _encodedWidth = 0;
45   _encodedHeight = 0;
46   _completeFrame = false;
47   _missingFrame = false;
48   set_size(0);
49   _codecSpecificInfo.codecType = kVideoCodecGeneric;
50   _codec = kVideoCodecGeneric;
51   rotation_ = kVideoRotation_0;
52   content_type_ = VideoContentType::UNSPECIFIED;
53   timing_.flags = VideoSendTiming::kInvalid;
54 }
55 
CopyCodecSpecific(const RTPVideoHeader * header)56 void VCMEncodedFrame::CopyCodecSpecific(const RTPVideoHeader* header) {
57   if (header) {
58     switch (header->codec) {
59       case kVideoCodecVP8: {
60         const auto& vp8_header =
61             absl::get<RTPVideoHeaderVP8>(header->video_type_header);
62         if (_codecSpecificInfo.codecType != kVideoCodecVP8) {
63           // This is the first packet for this frame.
64           _codecSpecificInfo.codecSpecific.VP8.temporalIdx = 0;
65           _codecSpecificInfo.codecSpecific.VP8.layerSync = false;
66           _codecSpecificInfo.codecSpecific.VP8.keyIdx = -1;
67           _codecSpecificInfo.codecType = kVideoCodecVP8;
68         }
69         _codecSpecificInfo.codecSpecific.VP8.nonReference =
70             vp8_header.nonReference;
71         if (vp8_header.temporalIdx != kNoTemporalIdx) {
72           _codecSpecificInfo.codecSpecific.VP8.temporalIdx =
73               vp8_header.temporalIdx;
74           _codecSpecificInfo.codecSpecific.VP8.layerSync = vp8_header.layerSync;
75         }
76         if (vp8_header.keyIdx != kNoKeyIdx) {
77           _codecSpecificInfo.codecSpecific.VP8.keyIdx = vp8_header.keyIdx;
78         }
79         break;
80       }
81       case kVideoCodecVP9: {
82         const auto& vp9_header =
83             absl::get<RTPVideoHeaderVP9>(header->video_type_header);
84         if (_codecSpecificInfo.codecType != kVideoCodecVP9) {
85           // This is the first packet for this frame.
86           _codecSpecificInfo.codecSpecific.VP9.temporal_idx = 0;
87           _codecSpecificInfo.codecSpecific.VP9.gof_idx = 0;
88           _codecSpecificInfo.codecSpecific.VP9.inter_layer_predicted = false;
89           _codecSpecificInfo.codecType = kVideoCodecVP9;
90         }
91         _codecSpecificInfo.codecSpecific.VP9.inter_pic_predicted =
92             vp9_header.inter_pic_predicted;
93         _codecSpecificInfo.codecSpecific.VP9.flexible_mode =
94             vp9_header.flexible_mode;
95         _codecSpecificInfo.codecSpecific.VP9.num_ref_pics =
96             vp9_header.num_ref_pics;
97         for (uint8_t r = 0; r < vp9_header.num_ref_pics; ++r) {
98           _codecSpecificInfo.codecSpecific.VP9.p_diff[r] =
99               vp9_header.pid_diff[r];
100         }
101         _codecSpecificInfo.codecSpecific.VP9.ss_data_available =
102             vp9_header.ss_data_available;
103         if (vp9_header.temporal_idx != kNoTemporalIdx) {
104           _codecSpecificInfo.codecSpecific.VP9.temporal_idx =
105               vp9_header.temporal_idx;
106           _codecSpecificInfo.codecSpecific.VP9.temporal_up_switch =
107               vp9_header.temporal_up_switch;
108         }
109         if (vp9_header.spatial_idx != kNoSpatialIdx) {
110           _codecSpecificInfo.codecSpecific.VP9.inter_layer_predicted =
111               vp9_header.inter_layer_predicted;
112           SetSpatialIndex(vp9_header.spatial_idx);
113         }
114         if (vp9_header.gof_idx != kNoGofIdx) {
115           _codecSpecificInfo.codecSpecific.VP9.gof_idx = vp9_header.gof_idx;
116         }
117         if (vp9_header.ss_data_available) {
118           _codecSpecificInfo.codecSpecific.VP9.num_spatial_layers =
119               vp9_header.num_spatial_layers;
120           _codecSpecificInfo.codecSpecific.VP9
121               .spatial_layer_resolution_present =
122               vp9_header.spatial_layer_resolution_present;
123           if (vp9_header.spatial_layer_resolution_present) {
124             for (size_t i = 0; i < vp9_header.num_spatial_layers; ++i) {
125               _codecSpecificInfo.codecSpecific.VP9.width[i] =
126                   vp9_header.width[i];
127               _codecSpecificInfo.codecSpecific.VP9.height[i] =
128                   vp9_header.height[i];
129             }
130           }
131           _codecSpecificInfo.codecSpecific.VP9.gof.CopyGofInfoVP9(
132               vp9_header.gof);
133         }
134         break;
135       }
136       case kVideoCodecH264: {
137         _codecSpecificInfo.codecType = kVideoCodecH264;
138 
139         // The following H264 codec specific data are not used elsewhere.
140         // Instead they are read directly from the frame marking extension.
141         // These codec specific data structures should be removed
142         // when frame marking is used.
143         _codecSpecificInfo.codecSpecific.H264.temporal_idx = kNoTemporalIdx;
144         if (header->frame_marking.temporal_id != kNoTemporalIdx) {
145           _codecSpecificInfo.codecSpecific.H264.temporal_idx =
146               header->frame_marking.temporal_id;
147           _codecSpecificInfo.codecSpecific.H264.base_layer_sync =
148               header->frame_marking.base_layer_sync;
149           _codecSpecificInfo.codecSpecific.H264.idr_frame =
150               header->frame_marking.independent_frame;
151         }
152         break;
153       }
154       default: {
155         _codecSpecificInfo.codecType = kVideoCodecGeneric;
156         break;
157       }
158     }
159   }
160 }
161 
162 }  // namespace webrtc
163