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