blob: ec3e9757b368e05a3ecfeb1f3a4c3cc8cdc90fb0 [file] [log] [blame]
philipelc707ab72016-04-01 02:01:54 -07001/*
2 * Copyright (c) 2016 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
philipel266f0a42016-11-28 08:49:07 -080011#include "webrtc/base/checks.h"
philipelc707ab72016-04-01 02:01:54 -070012#include "webrtc/modules/video_coding/frame_object.h"
philipelc707ab72016-04-01 02:01:54 -070013#include "webrtc/modules/video_coding/packet_buffer.h"
14
15namespace webrtc {
16namespace video_coding {
17
philipela1059872016-05-09 11:41:48 +020018FrameObject::FrameObject()
19 : picture_id(0),
20 spatial_layer(0),
philipelbe7a9e52016-05-19 12:19:35 +020021 timestamp(0),
philipela1059872016-05-09 11:41:48 +020022 num_references(0),
23 inter_layer_predicted(false) {}
24
philipelc707ab72016-04-01 02:01:54 -070025RtpFrameObject::RtpFrameObject(PacketBuffer* packet_buffer,
philipel02447bc2016-05-13 06:01:03 -070026 uint16_t first_seq_num,
philipel5ceaaae2016-05-24 10:20:47 +020027 uint16_t last_seq_num,
28 size_t frame_size,
philipelb4d31082016-07-11 08:46:29 -070029 int times_nacked,
30 int64_t received_time)
philipelc707ab72016-04-01 02:01:54 -070031 : packet_buffer_(packet_buffer),
philipel02447bc2016-05-13 06:01:03 -070032 first_seq_num_(first_seq_num),
philipel5ceaaae2016-05-24 10:20:47 +020033 last_seq_num_(last_seq_num),
philipelb4d31082016-07-11 08:46:29 -070034 received_time_(received_time),
philipel5ceaaae2016-05-24 10:20:47 +020035 times_nacked_(times_nacked) {
philipel266f0a42016-11-28 08:49:07 -080036 VCMPacket* first_packet = packet_buffer_->GetPacket(first_seq_num);
37 RTC_DCHECK(first_packet);
philipel36928452016-11-07 10:42:36 +010038
philipel266f0a42016-11-28 08:49:07 -080039 // RtpFrameObject members
40 frame_type_ = first_packet->frameType;
41 codec_type_ = first_packet->codec;
philipel36928452016-11-07 10:42:36 +010042
philipel266f0a42016-11-28 08:49:07 -080043 // TODO(philipel): Remove when encoded image is replaced by FrameObject.
44 // VCMEncodedFrame members
45 CopyCodecSpecific(&first_packet->video_header);
46 _completeFrame = true;
47 _payloadType = first_packet->payloadType;
48 _timeStamp = first_packet->timestamp;
49 ntp_time_ms_ = first_packet->ntp_time_ms_;
philipel36928452016-11-07 10:42:36 +010050
gnishb2a318b2017-05-10 09:21:33 -070051 // Setting frame's playout delays to the same values
52 // as of the first packet's.
53 SetPlayoutDelay(first_packet->video_header.playout_delay);
54
philipel266f0a42016-11-28 08:49:07 -080055 // Since FFmpeg use an optimized bitstream reader that reads in chunks of
56 // 32/64 bits we have to add at least that much padding to the buffer
57 // to make sure the decoder doesn't read out of bounds.
58 // NOTE! EncodedImage::_size is the size of the buffer (think capacity of
59 // an std::vector) and EncodedImage::_length is the actual size of
60 // the bitstream (think size of an std::vector).
61 if (codec_type_ == kVideoCodecH264)
62 _size = frame_size + EncodedImage::kBufferPaddingBytesH264;
63 else
64 _size = frame_size;
philipel552866c2016-07-04 16:18:55 +020065
philipel266f0a42016-11-28 08:49:07 -080066 _buffer = new uint8_t[_size];
67 _length = frame_size;
68 _frameType = first_packet->frameType;
69 GetBitstream(_buffer);
philipel59e99b72017-01-12 03:26:04 -080070 _encodedWidth = first_packet->width;
71 _encodedHeight = first_packet->height;
philipel266f0a42016-11-28 08:49:07 -080072
73 // FrameObject members
74 timestamp = first_packet->timestamp;
75
76 VCMPacket* last_packet = packet_buffer_->GetPacket(last_seq_num);
77 RTC_DCHECK(last_packet && last_packet->markerBit);
78 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/
79 // ts_126114v120700p.pdf Section 7.4.5.
80 // The MTSI client shall add the payload bytes as defined in this clause
81 // onto the last RTP packet in each group of packets which make up a key
82 // frame (I-frame or IDR frame in H.264 (AVC), or an IRAP picture in H.265
83 // (HEVC)).
84 rotation_ = last_packet->video_header.rotation;
85 _rotation_set = true;
ilnik00d802b2017-04-11 10:34:31 -070086 content_type_ = last_packet->video_header.content_type;
philipel02447bc2016-05-13 06:01:03 -070087}
philipelc707ab72016-04-01 02:01:54 -070088
89RtpFrameObject::~RtpFrameObject() {
90 packet_buffer_->ReturnFrame(this);
91}
92
philipelf4139332016-04-20 10:26:34 +020093uint16_t RtpFrameObject::first_seq_num() const {
philipel02447bc2016-05-13 06:01:03 -070094 return first_seq_num_;
philipelc707ab72016-04-01 02:01:54 -070095}
96
philipelf4139332016-04-20 10:26:34 +020097uint16_t RtpFrameObject::last_seq_num() const {
philipel02447bc2016-05-13 06:01:03 -070098 return last_seq_num_;
99}
100
philipel5ceaaae2016-05-24 10:20:47 +0200101int RtpFrameObject::times_nacked() const {
102 return times_nacked_;
103}
104
philipel02447bc2016-05-13 06:01:03 -0700105FrameType RtpFrameObject::frame_type() const {
106 return frame_type_;
107}
108
109VideoCodecType RtpFrameObject::codec_type() const {
110 return codec_type_;
philipelc707ab72016-04-01 02:01:54 -0700111}
112
philipelc707ab72016-04-01 02:01:54 -0700113bool RtpFrameObject::GetBitstream(uint8_t* destination) const {
114 return packet_buffer_->GetBitstream(*this, destination);
115}
116
philipelb4d31082016-07-11 08:46:29 -0700117uint32_t RtpFrameObject::Timestamp() const {
118 return timestamp_;
119}
120
121int64_t RtpFrameObject::ReceivedTime() const {
122 return received_time_;
123}
124
125int64_t RtpFrameObject::RenderTime() const {
126 return _renderTimeMs;
127}
128
philipele0754302017-01-25 08:56:23 -0800129bool RtpFrameObject::delayed_by_retransmission() const {
130 return times_nacked() > 0;
131}
132
philipel88488282016-11-03 08:56:54 -0700133rtc::Optional<RTPVideoTypeHeader> RtpFrameObject::GetCodecHeader() const {
134 rtc::CritScope lock(&packet_buffer_->crit_);
philipel02447bc2016-05-13 06:01:03 -0700135 VCMPacket* packet = packet_buffer_->GetPacket(first_seq_num_);
136 if (!packet)
philipel88488282016-11-03 08:56:54 -0700137 return rtc::Optional<RTPVideoTypeHeader>();
138 return rtc::Optional<RTPVideoTypeHeader>(packet->video_header.codecHeader);
philipel02447bc2016-05-13 06:01:03 -0700139}
140
philipelc707ab72016-04-01 02:01:54 -0700141} // namespace video_coding
142} // namespace webrtc