philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 11 | #include "webrtc/base/checks.h" |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 12 | #include "webrtc/modules/video_coding/frame_object.h" |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 13 | #include "webrtc/modules/video_coding/packet_buffer.h" |
| 14 | |
| 15 | namespace webrtc { |
| 16 | namespace video_coding { |
| 17 | |
philipel | a105987 | 2016-05-09 11:41:48 +0200 | [diff] [blame] | 18 | FrameObject::FrameObject() |
| 19 | : picture_id(0), |
| 20 | spatial_layer(0), |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 21 | timestamp(0), |
philipel | a105987 | 2016-05-09 11:41:48 +0200 | [diff] [blame] | 22 | num_references(0), |
| 23 | inter_layer_predicted(false) {} |
| 24 | |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 25 | RtpFrameObject::RtpFrameObject(PacketBuffer* packet_buffer, |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 26 | uint16_t first_seq_num, |
philipel | 5ceaaae | 2016-05-24 10:20:47 +0200 | [diff] [blame] | 27 | uint16_t last_seq_num, |
| 28 | size_t frame_size, |
philipel | b4d3108 | 2016-07-11 08:46:29 -0700 | [diff] [blame] | 29 | int times_nacked, |
| 30 | int64_t received_time) |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 31 | : packet_buffer_(packet_buffer), |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 32 | first_seq_num_(first_seq_num), |
philipel | 5ceaaae | 2016-05-24 10:20:47 +0200 | [diff] [blame] | 33 | last_seq_num_(last_seq_num), |
philipel | b4d3108 | 2016-07-11 08:46:29 -0700 | [diff] [blame] | 34 | received_time_(received_time), |
philipel | 5ceaaae | 2016-05-24 10:20:47 +0200 | [diff] [blame] | 35 | times_nacked_(times_nacked) { |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 36 | VCMPacket* first_packet = packet_buffer_->GetPacket(first_seq_num); |
| 37 | RTC_DCHECK(first_packet); |
philipel | 3692845 | 2016-11-07 10:42:36 +0100 | [diff] [blame] | 38 | |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 39 | // RtpFrameObject members |
| 40 | frame_type_ = first_packet->frameType; |
| 41 | codec_type_ = first_packet->codec; |
philipel | 3692845 | 2016-11-07 10:42:36 +0100 | [diff] [blame] | 42 | |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 43 | // 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_; |
philipel | 3692845 | 2016-11-07 10:42:36 +0100 | [diff] [blame] | 50 | |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 51 | // Since FFmpeg use an optimized bitstream reader that reads in chunks of |
| 52 | // 32/64 bits we have to add at least that much padding to the buffer |
| 53 | // to make sure the decoder doesn't read out of bounds. |
| 54 | // NOTE! EncodedImage::_size is the size of the buffer (think capacity of |
| 55 | // an std::vector) and EncodedImage::_length is the actual size of |
| 56 | // the bitstream (think size of an std::vector). |
| 57 | if (codec_type_ == kVideoCodecH264) |
| 58 | _size = frame_size + EncodedImage::kBufferPaddingBytesH264; |
| 59 | else |
| 60 | _size = frame_size; |
philipel | 552866c | 2016-07-04 16:18:55 +0200 | [diff] [blame] | 61 | |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 62 | _buffer = new uint8_t[_size]; |
| 63 | _length = frame_size; |
| 64 | _frameType = first_packet->frameType; |
| 65 | GetBitstream(_buffer); |
philipel | 59e99b7 | 2017-01-12 03:26:04 -0800 | [diff] [blame] | 66 | _encodedWidth = first_packet->width; |
| 67 | _encodedHeight = first_packet->height; |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 68 | |
| 69 | // FrameObject members |
| 70 | timestamp = first_packet->timestamp; |
| 71 | |
| 72 | VCMPacket* last_packet = packet_buffer_->GetPacket(last_seq_num); |
| 73 | RTC_DCHECK(last_packet && last_packet->markerBit); |
| 74 | // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ |
| 75 | // ts_126114v120700p.pdf Section 7.4.5. |
| 76 | // The MTSI client shall add the payload bytes as defined in this clause |
| 77 | // onto the last RTP packet in each group of packets which make up a key |
| 78 | // frame (I-frame or IDR frame in H.264 (AVC), or an IRAP picture in H.265 |
| 79 | // (HEVC)). |
| 80 | rotation_ = last_packet->video_header.rotation; |
| 81 | _rotation_set = true; |
ilnik | 00d802b | 2017-04-11 10:34:31 -0700 | [diff] [blame^] | 82 | content_type_ = last_packet->video_header.content_type; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 83 | } |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 84 | |
| 85 | RtpFrameObject::~RtpFrameObject() { |
| 86 | packet_buffer_->ReturnFrame(this); |
| 87 | } |
| 88 | |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 89 | uint16_t RtpFrameObject::first_seq_num() const { |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 90 | return first_seq_num_; |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 91 | } |
| 92 | |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 93 | uint16_t RtpFrameObject::last_seq_num() const { |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 94 | return last_seq_num_; |
| 95 | } |
| 96 | |
philipel | 5ceaaae | 2016-05-24 10:20:47 +0200 | [diff] [blame] | 97 | int RtpFrameObject::times_nacked() const { |
| 98 | return times_nacked_; |
| 99 | } |
| 100 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 101 | FrameType RtpFrameObject::frame_type() const { |
| 102 | return frame_type_; |
| 103 | } |
| 104 | |
| 105 | VideoCodecType RtpFrameObject::codec_type() const { |
| 106 | return codec_type_; |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 107 | } |
| 108 | |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 109 | bool RtpFrameObject::GetBitstream(uint8_t* destination) const { |
| 110 | return packet_buffer_->GetBitstream(*this, destination); |
| 111 | } |
| 112 | |
philipel | b4d3108 | 2016-07-11 08:46:29 -0700 | [diff] [blame] | 113 | uint32_t RtpFrameObject::Timestamp() const { |
| 114 | return timestamp_; |
| 115 | } |
| 116 | |
| 117 | int64_t RtpFrameObject::ReceivedTime() const { |
| 118 | return received_time_; |
| 119 | } |
| 120 | |
| 121 | int64_t RtpFrameObject::RenderTime() const { |
| 122 | return _renderTimeMs; |
| 123 | } |
| 124 | |
philipel | e075430 | 2017-01-25 08:56:23 -0800 | [diff] [blame] | 125 | bool RtpFrameObject::delayed_by_retransmission() const { |
| 126 | return times_nacked() > 0; |
| 127 | } |
| 128 | |
philipel | 8848828 | 2016-11-03 08:56:54 -0700 | [diff] [blame] | 129 | rtc::Optional<RTPVideoTypeHeader> RtpFrameObject::GetCodecHeader() const { |
| 130 | rtc::CritScope lock(&packet_buffer_->crit_); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 131 | VCMPacket* packet = packet_buffer_->GetPacket(first_seq_num_); |
| 132 | if (!packet) |
philipel | 8848828 | 2016-11-03 08:56:54 -0700 | [diff] [blame] | 133 | return rtc::Optional<RTPVideoTypeHeader>(); |
| 134 | return rtc::Optional<RTPVideoTypeHeader>(packet->video_header.codecHeader); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 135 | } |
| 136 | |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 137 | } // namespace video_coding |
| 138 | } // namespace webrtc |