You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

367 lines
12 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2011 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. #include "modules/video_coding/decoding_state.h"
  11. #include "common_video/h264/h264_common.h"
  12. #include "modules/include/module_common_types_public.h"
  13. #include "modules/video_coding/frame_buffer.h"
  14. #include "modules/video_coding/jitter_buffer_common.h"
  15. #include "modules/video_coding/packet.h"
  16. #include "rtc_base/logging.h"
  17. namespace webrtc {
  18. VCMDecodingState::VCMDecodingState()
  19. : sequence_num_(0),
  20. time_stamp_(0),
  21. picture_id_(kNoPictureId),
  22. temporal_id_(kNoTemporalIdx),
  23. tl0_pic_id_(kNoTl0PicIdx),
  24. full_sync_(true),
  25. in_initial_state_(true) {
  26. memset(frame_decoded_, 0, sizeof(frame_decoded_));
  27. }
  28. VCMDecodingState::~VCMDecodingState() {}
  29. void VCMDecodingState::Reset() {
  30. // TODO(mikhal): Verify - not always would want to reset the sync
  31. sequence_num_ = 0;
  32. time_stamp_ = 0;
  33. picture_id_ = kNoPictureId;
  34. temporal_id_ = kNoTemporalIdx;
  35. tl0_pic_id_ = kNoTl0PicIdx;
  36. full_sync_ = true;
  37. in_initial_state_ = true;
  38. memset(frame_decoded_, 0, sizeof(frame_decoded_));
  39. received_sps_.clear();
  40. received_pps_.clear();
  41. }
  42. uint32_t VCMDecodingState::time_stamp() const {
  43. return time_stamp_;
  44. }
  45. uint16_t VCMDecodingState::sequence_num() const {
  46. return sequence_num_;
  47. }
  48. bool VCMDecodingState::IsOldFrame(const VCMFrameBuffer* frame) const {
  49. assert(frame != NULL);
  50. if (in_initial_state_)
  51. return false;
  52. return !IsNewerTimestamp(frame->Timestamp(), time_stamp_);
  53. }
  54. bool VCMDecodingState::IsOldPacket(const VCMPacket* packet) const {
  55. assert(packet != NULL);
  56. if (in_initial_state_)
  57. return false;
  58. return !IsNewerTimestamp(packet->timestamp, time_stamp_);
  59. }
  60. void VCMDecodingState::SetState(const VCMFrameBuffer* frame) {
  61. assert(frame != NULL && frame->GetHighSeqNum() >= 0);
  62. if (!UsingFlexibleMode(frame))
  63. UpdateSyncState(frame);
  64. sequence_num_ = static_cast<uint16_t>(frame->GetHighSeqNum());
  65. time_stamp_ = frame->Timestamp();
  66. picture_id_ = frame->PictureId();
  67. temporal_id_ = frame->TemporalId();
  68. tl0_pic_id_ = frame->Tl0PicId();
  69. for (const NaluInfo& nalu : frame->GetNaluInfos()) {
  70. if (nalu.type == H264::NaluType::kPps) {
  71. if (nalu.pps_id < 0) {
  72. RTC_LOG(LS_WARNING) << "Received pps without pps id.";
  73. } else if (nalu.sps_id < 0) {
  74. RTC_LOG(LS_WARNING) << "Received pps without sps id.";
  75. } else {
  76. received_pps_[nalu.pps_id] = nalu.sps_id;
  77. }
  78. } else if (nalu.type == H264::NaluType::kSps) {
  79. if (nalu.sps_id < 0) {
  80. RTC_LOG(LS_WARNING) << "Received sps without sps id.";
  81. } else {
  82. received_sps_.insert(nalu.sps_id);
  83. }
  84. }
  85. }
  86. if (UsingFlexibleMode(frame)) {
  87. uint16_t frame_index = picture_id_ % kFrameDecodedLength;
  88. if (in_initial_state_) {
  89. frame_decoded_cleared_to_ = frame_index;
  90. } else if (frame->FrameType() == VideoFrameType::kVideoFrameKey) {
  91. memset(frame_decoded_, 0, sizeof(frame_decoded_));
  92. frame_decoded_cleared_to_ = frame_index;
  93. } else {
  94. if (AheadOfFramesDecodedClearedTo(frame_index)) {
  95. while (frame_decoded_cleared_to_ != frame_index) {
  96. frame_decoded_cleared_to_ =
  97. (frame_decoded_cleared_to_ + 1) % kFrameDecodedLength;
  98. frame_decoded_[frame_decoded_cleared_to_] = false;
  99. }
  100. }
  101. }
  102. frame_decoded_[frame_index] = true;
  103. }
  104. in_initial_state_ = false;
  105. }
  106. void VCMDecodingState::CopyFrom(const VCMDecodingState& state) {
  107. sequence_num_ = state.sequence_num_;
  108. time_stamp_ = state.time_stamp_;
  109. picture_id_ = state.picture_id_;
  110. temporal_id_ = state.temporal_id_;
  111. tl0_pic_id_ = state.tl0_pic_id_;
  112. full_sync_ = state.full_sync_;
  113. in_initial_state_ = state.in_initial_state_;
  114. frame_decoded_cleared_to_ = state.frame_decoded_cleared_to_;
  115. memcpy(frame_decoded_, state.frame_decoded_, sizeof(frame_decoded_));
  116. received_sps_ = state.received_sps_;
  117. received_pps_ = state.received_pps_;
  118. }
  119. bool VCMDecodingState::UpdateEmptyFrame(const VCMFrameBuffer* frame) {
  120. bool empty_packet = frame->GetHighSeqNum() == frame->GetLowSeqNum();
  121. if (in_initial_state_ && empty_packet) {
  122. // Drop empty packets as long as we are in the initial state.
  123. return true;
  124. }
  125. if ((empty_packet && ContinuousSeqNum(frame->GetHighSeqNum())) ||
  126. ContinuousFrame(frame)) {
  127. // Continuous empty packets or continuous frames can be dropped if we
  128. // advance the sequence number.
  129. sequence_num_ = frame->GetHighSeqNum();
  130. time_stamp_ = frame->Timestamp();
  131. return true;
  132. }
  133. return false;
  134. }
  135. void VCMDecodingState::UpdateOldPacket(const VCMPacket* packet) {
  136. assert(packet != NULL);
  137. if (packet->timestamp == time_stamp_) {
  138. // Late packet belonging to the last decoded frame - make sure we update the
  139. // last decoded sequence number.
  140. sequence_num_ = LatestSequenceNumber(packet->seqNum, sequence_num_);
  141. }
  142. }
  143. void VCMDecodingState::SetSeqNum(uint16_t new_seq_num) {
  144. sequence_num_ = new_seq_num;
  145. }
  146. bool VCMDecodingState::in_initial_state() const {
  147. return in_initial_state_;
  148. }
  149. bool VCMDecodingState::full_sync() const {
  150. return full_sync_;
  151. }
  152. void VCMDecodingState::UpdateSyncState(const VCMFrameBuffer* frame) {
  153. if (in_initial_state_)
  154. return;
  155. if (frame->TemporalId() == kNoTemporalIdx ||
  156. frame->Tl0PicId() == kNoTl0PicIdx) {
  157. full_sync_ = true;
  158. } else if (frame->FrameType() == VideoFrameType::kVideoFrameKey ||
  159. frame->LayerSync()) {
  160. full_sync_ = true;
  161. } else if (full_sync_) {
  162. // Verify that we are still in sync.
  163. // Sync will be broken if continuity is true for layers but not for the
  164. // other methods (PictureId and SeqNum).
  165. if (UsingPictureId(frame)) {
  166. // First check for a valid tl0PicId.
  167. if (frame->Tl0PicId() - tl0_pic_id_ > 1) {
  168. full_sync_ = false;
  169. } else {
  170. full_sync_ = ContinuousPictureId(frame->PictureId());
  171. }
  172. } else {
  173. full_sync_ =
  174. ContinuousSeqNum(static_cast<uint16_t>(frame->GetLowSeqNum()));
  175. }
  176. }
  177. }
  178. bool VCMDecodingState::ContinuousFrame(const VCMFrameBuffer* frame) const {
  179. // Check continuity based on the following hierarchy:
  180. // - Temporal layers (stop here if out of sync).
  181. // - Picture Id when available.
  182. // - Sequence numbers.
  183. // Return true when in initial state.
  184. // Note that when a method is not applicable it will return false.
  185. assert(frame != NULL);
  186. // A key frame is always considered continuous as it doesn't refer to any
  187. // frames and therefore won't introduce any errors even if prior frames are
  188. // missing.
  189. if (frame->FrameType() == VideoFrameType::kVideoFrameKey &&
  190. HaveSpsAndPps(frame->GetNaluInfos())) {
  191. return true;
  192. }
  193. // When in the initial state we always require a key frame to start decoding.
  194. if (in_initial_state_)
  195. return false;
  196. if (ContinuousLayer(frame->TemporalId(), frame->Tl0PicId()))
  197. return true;
  198. // tl0picId is either not used, or should remain unchanged.
  199. if (frame->Tl0PicId() != tl0_pic_id_)
  200. return false;
  201. // Base layers are not continuous or temporal layers are inactive.
  202. // In the presence of temporal layers, check for Picture ID/sequence number
  203. // continuity if sync can be restored by this frame.
  204. if (!full_sync_ && !frame->LayerSync())
  205. return false;
  206. if (UsingPictureId(frame)) {
  207. if (UsingFlexibleMode(frame)) {
  208. return ContinuousFrameRefs(frame);
  209. } else {
  210. return ContinuousPictureId(frame->PictureId());
  211. }
  212. } else {
  213. return ContinuousSeqNum(static_cast<uint16_t>(frame->GetLowSeqNum())) &&
  214. HaveSpsAndPps(frame->GetNaluInfos());
  215. }
  216. }
  217. bool VCMDecodingState::ContinuousPictureId(int picture_id) const {
  218. int next_picture_id = picture_id_ + 1;
  219. if (picture_id < picture_id_) {
  220. // Wrap
  221. if (picture_id_ >= 0x80) {
  222. // 15 bits used for picture id
  223. return ((next_picture_id & 0x7FFF) == picture_id);
  224. } else {
  225. // 7 bits used for picture id
  226. return ((next_picture_id & 0x7F) == picture_id);
  227. }
  228. }
  229. // No wrap
  230. return (next_picture_id == picture_id);
  231. }
  232. bool VCMDecodingState::ContinuousSeqNum(uint16_t seq_num) const {
  233. return seq_num == static_cast<uint16_t>(sequence_num_ + 1);
  234. }
  235. bool VCMDecodingState::ContinuousLayer(int temporal_id, int tl0_pic_id) const {
  236. // First, check if applicable.
  237. if (temporal_id == kNoTemporalIdx || tl0_pic_id == kNoTl0PicIdx)
  238. return false;
  239. // If this is the first frame to use temporal layers, make sure we start
  240. // from base.
  241. else if (tl0_pic_id_ == kNoTl0PicIdx && temporal_id_ == kNoTemporalIdx &&
  242. temporal_id == 0)
  243. return true;
  244. // Current implementation: Look for base layer continuity.
  245. if (temporal_id != 0)
  246. return false;
  247. return (static_cast<uint8_t>(tl0_pic_id_ + 1) == tl0_pic_id);
  248. }
  249. bool VCMDecodingState::ContinuousFrameRefs(const VCMFrameBuffer* frame) const {
  250. uint8_t num_refs = frame->CodecSpecific()->codecSpecific.VP9.num_ref_pics;
  251. for (uint8_t r = 0; r < num_refs; ++r) {
  252. uint16_t frame_ref = frame->PictureId() -
  253. frame->CodecSpecific()->codecSpecific.VP9.p_diff[r];
  254. uint16_t frame_index = frame_ref % kFrameDecodedLength;
  255. if (AheadOfFramesDecodedClearedTo(frame_index) ||
  256. !frame_decoded_[frame_index]) {
  257. return false;
  258. }
  259. }
  260. return true;
  261. }
  262. bool VCMDecodingState::UsingPictureId(const VCMFrameBuffer* frame) const {
  263. return (frame->PictureId() != kNoPictureId && picture_id_ != kNoPictureId);
  264. }
  265. bool VCMDecodingState::UsingFlexibleMode(const VCMFrameBuffer* frame) const {
  266. bool is_flexible_mode =
  267. frame->CodecSpecific()->codecType == kVideoCodecVP9 &&
  268. frame->CodecSpecific()->codecSpecific.VP9.flexible_mode;
  269. if (is_flexible_mode && frame->PictureId() == kNoPictureId) {
  270. RTC_LOG(LS_WARNING) << "Frame is marked as using flexible mode but no"
  271. << "picture id is set.";
  272. return false;
  273. }
  274. return is_flexible_mode;
  275. }
  276. // TODO(philipel): change how check work, this check practially
  277. // limits the max p_diff to 64.
  278. bool VCMDecodingState::AheadOfFramesDecodedClearedTo(uint16_t index) const {
  279. // No way of knowing for sure if we are actually ahead of
  280. // frame_decoded_cleared_to_. We just make the assumption
  281. // that we are not trying to reference back to a very old
  282. // index, but instead are referencing a newer index.
  283. uint16_t diff =
  284. index > frame_decoded_cleared_to_
  285. ? kFrameDecodedLength - (index - frame_decoded_cleared_to_)
  286. : frame_decoded_cleared_to_ - index;
  287. return diff > kFrameDecodedLength / 2;
  288. }
  289. bool VCMDecodingState::HaveSpsAndPps(const std::vector<NaluInfo>& nalus) const {
  290. std::set<int> new_sps;
  291. std::map<int, int> new_pps;
  292. for (const NaluInfo& nalu : nalus) {
  293. // Check if this nalu actually contains sps/pps information or dependencies.
  294. if (nalu.sps_id == -1 && nalu.pps_id == -1)
  295. continue;
  296. switch (nalu.type) {
  297. case H264::NaluType::kPps:
  298. if (nalu.pps_id < 0) {
  299. RTC_LOG(LS_WARNING) << "Received pps without pps id.";
  300. } else if (nalu.sps_id < 0) {
  301. RTC_LOG(LS_WARNING) << "Received pps without sps id.";
  302. } else {
  303. new_pps[nalu.pps_id] = nalu.sps_id;
  304. }
  305. break;
  306. case H264::NaluType::kSps:
  307. if (nalu.sps_id < 0) {
  308. RTC_LOG(LS_WARNING) << "Received sps without sps id.";
  309. } else {
  310. new_sps.insert(nalu.sps_id);
  311. }
  312. break;
  313. default: {
  314. int needed_sps = -1;
  315. auto pps_it = new_pps.find(nalu.pps_id);
  316. if (pps_it != new_pps.end()) {
  317. needed_sps = pps_it->second;
  318. } else {
  319. auto pps_it2 = received_pps_.find(nalu.pps_id);
  320. if (pps_it2 == received_pps_.end()) {
  321. return false;
  322. }
  323. needed_sps = pps_it2->second;
  324. }
  325. if (new_sps.find(needed_sps) == new_sps.end() &&
  326. received_sps_.find(needed_sps) == received_sps_.end()) {
  327. return false;
  328. }
  329. break;
  330. }
  331. }
  332. }
  333. return true;
  334. }
  335. } // namespace webrtc