summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/modules/video_coding/decoding_state_unittest.cc
blob: bef7f81c62aa8e2c4efc7c1462af97adb85af73d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
/*
 *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "modules/video_coding/decoding_state.h"

#include "modules/rtp_rtcp/source/rtp_video_header.h"
#include "modules/video_coding/codecs/interface/common_constants.h"
#include "modules/video_coding/codecs/vp8/include/vp8_globals.h"
#include "modules/video_coding/codecs/vp9/include/vp9_globals.h"
#include "modules/video_coding/frame_buffer.h"
#include "modules/video_coding/include/video_coding.h"
#include "modules/video_coding/packet.h"
#include "modules/video_coding/session_info.h"
#include "test/gtest.h"

namespace webrtc {

TEST(TestDecodingState, Sanity) {
  VCMDecodingState dec_state;
  dec_state.Reset();
  EXPECT_TRUE(dec_state.in_initial_state());
  EXPECT_TRUE(dec_state.full_sync());
}

TEST(TestDecodingState, FrameContinuity) {
  VCMDecodingState dec_state;
  // Check that makes decision based on correct method.
  VCMFrameBuffer frame;
  VCMFrameBuffer frame_key;
  VCMPacket packet;
  packet.video_header.is_first_packet_in_frame = true;
  packet.timestamp = 1;
  packet.seqNum = 0xffff;
  packet.video_header.frame_type = VideoFrameType::kVideoFrameDelta;
  packet.video_header.codec = kVideoCodecVP8;
  auto& vp8_header =
      packet.video_header.video_type_header.emplace<RTPVideoHeaderVP8>();
  vp8_header.pictureId = 0x007F;
  FrameData frame_data;
  frame_data.rtt_ms = 0;
  frame_data.rolling_average_packets_per_frame = -1;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  // Always start with a key frame.
  dec_state.Reset();
  EXPECT_FALSE(dec_state.ContinuousFrame(&frame));
  packet.video_header.frame_type = VideoFrameType::kVideoFrameKey;
  EXPECT_LE(0, frame_key.InsertPacket(packet, 0, frame_data));
  EXPECT_TRUE(dec_state.ContinuousFrame(&frame_key));
  dec_state.SetState(&frame);
  frame.Reset();
  packet.video_header.frame_type = VideoFrameType::kVideoFrameDelta;
  // Use pictureId
  packet.video_header.is_first_packet_in_frame = false;
  vp8_header.pictureId = 0x0002;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_FALSE(dec_state.ContinuousFrame(&frame));
  frame.Reset();
  vp8_header.pictureId = 0;
  packet.seqNum = 10;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_TRUE(dec_state.ContinuousFrame(&frame));

  // Use sequence numbers.
  vp8_header.pictureId = kNoPictureId;
  frame.Reset();
  packet.seqNum = dec_state.sequence_num() - 1u;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_FALSE(dec_state.ContinuousFrame(&frame));
  frame.Reset();
  packet.seqNum = dec_state.sequence_num() + 1u;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  // Insert another packet to this frame
  packet.seqNum++;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  // Verify wrap.
  EXPECT_LE(dec_state.sequence_num(), 0xffff);
  EXPECT_TRUE(dec_state.ContinuousFrame(&frame));
  dec_state.SetState(&frame);

  // Insert packet with temporal info.
  dec_state.Reset();
  frame.Reset();
  vp8_header.tl0PicIdx = 0;
  vp8_header.temporalIdx = 0;
  vp8_header.pictureId = 0;
  packet.seqNum = 1;
  packet.timestamp = 1;
  EXPECT_TRUE(dec_state.full_sync());
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  dec_state.SetState(&frame);
  EXPECT_TRUE(dec_state.full_sync());
  frame.Reset();
  // 1 layer up - still good.
  vp8_header.tl0PicIdx = 0;
  vp8_header.temporalIdx = 1;
  vp8_header.pictureId = 1;
  packet.seqNum = 2;
  packet.timestamp = 2;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_TRUE(dec_state.ContinuousFrame(&frame));
  dec_state.SetState(&frame);
  EXPECT_TRUE(dec_state.full_sync());
  frame.Reset();
  // Lost non-base layer packet => should update sync parameter.
  vp8_header.tl0PicIdx = 0;
  vp8_header.temporalIdx = 3;
  vp8_header.pictureId = 3;
  packet.seqNum = 4;
  packet.timestamp = 4;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_FALSE(dec_state.ContinuousFrame(&frame));
  // Now insert the next non-base layer (belonging to a next tl0PicId).
  frame.Reset();
  vp8_header.tl0PicIdx = 1;
  vp8_header.temporalIdx = 2;
  vp8_header.pictureId = 4;
  packet.seqNum = 5;
  packet.timestamp = 5;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  // Checking continuity and not updating the state - this should not trigger
  // an update of sync state.
  EXPECT_FALSE(dec_state.ContinuousFrame(&frame));
  EXPECT_TRUE(dec_state.full_sync());
  // Next base layer (dropped interim non-base layers) - should update sync.
  frame.Reset();
  vp8_header.tl0PicIdx = 1;
  vp8_header.temporalIdx = 0;
  vp8_header.pictureId = 5;
  packet.seqNum = 6;
  packet.timestamp = 6;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_TRUE(dec_state.ContinuousFrame(&frame));
  dec_state.SetState(&frame);
  EXPECT_FALSE(dec_state.full_sync());

  // Check wrap for temporal layers.
  frame.Reset();
  vp8_header.tl0PicIdx = 0x00FF;
  vp8_header.temporalIdx = 0;
  vp8_header.pictureId = 6;
  packet.seqNum = 7;
  packet.timestamp = 7;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  dec_state.SetState(&frame);
  EXPECT_FALSE(dec_state.full_sync());
  frame.Reset();
  vp8_header.tl0PicIdx = 0x0000;
  vp8_header.temporalIdx = 0;
  vp8_header.pictureId = 7;
  packet.seqNum = 8;
  packet.timestamp = 8;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_TRUE(dec_state.ContinuousFrame(&frame));
  // The current frame is not continuous
  dec_state.SetState(&frame);
  EXPECT_FALSE(dec_state.ContinuousFrame(&frame));
}

TEST(TestDecodingState, UpdateOldPacket) {
  VCMDecodingState dec_state;
  // Update only if zero size and newer than previous.
  // Should only update if the timeStamp match.
  VCMFrameBuffer frame;
  VCMPacket packet;
  packet.timestamp = 1;
  packet.seqNum = 1;
  packet.video_header.frame_type = VideoFrameType::kVideoFrameDelta;
  FrameData frame_data;
  frame_data.rtt_ms = 0;
  frame_data.rolling_average_packets_per_frame = -1;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  dec_state.SetState(&frame);
  EXPECT_EQ(dec_state.sequence_num(), 1);
  // Insert an empty packet that does not belong to the same frame.
  // => Sequence num should be the same.
  packet.timestamp = 2;
  dec_state.UpdateOldPacket(&packet);
  EXPECT_EQ(dec_state.sequence_num(), 1);
  // Now insert empty packet belonging to the same frame.
  packet.timestamp = 1;
  packet.seqNum = 2;
  packet.video_header.frame_type = VideoFrameType::kEmptyFrame;
  packet.sizeBytes = 0;
  dec_state.UpdateOldPacket(&packet);
  EXPECT_EQ(dec_state.sequence_num(), 2);
  // Now insert delta packet belonging to the same frame.
  packet.timestamp = 1;
  packet.seqNum = 3;
  packet.video_header.frame_type = VideoFrameType::kVideoFrameDelta;
  packet.sizeBytes = 1400;
  dec_state.UpdateOldPacket(&packet);
  EXPECT_EQ(dec_state.sequence_num(), 3);
  // Insert a packet belonging to an older timestamp - should not update the
  // sequence number.
  packet.timestamp = 0;
  packet.seqNum = 4;
  packet.video_header.frame_type = VideoFrameType::kEmptyFrame;
  packet.sizeBytes = 0;
  dec_state.UpdateOldPacket(&packet);
  EXPECT_EQ(dec_state.sequence_num(), 3);
}

TEST(TestDecodingState, MultiLayerBehavior) {
  // Identify sync/non-sync when more than one layer.
  VCMDecodingState dec_state;
  // Identify packets belonging to old frames/packets.
  // Set state for current frames.
  // tl0PicIdx 0, temporal id 0.
  VCMFrameBuffer frame;
  VCMPacket packet;
  packet.video_header.frame_type = VideoFrameType::kVideoFrameDelta;
  packet.video_header.codec = kVideoCodecVP8;
  packet.timestamp = 0;
  packet.seqNum = 0;
  auto& vp8_header =
      packet.video_header.video_type_header.emplace<RTPVideoHeaderVP8>();
  vp8_header.tl0PicIdx = 0;
  vp8_header.temporalIdx = 0;
  vp8_header.pictureId = 0;
  FrameData frame_data;
  frame_data.rtt_ms = 0;
  frame_data.rolling_average_packets_per_frame = -1;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  dec_state.SetState(&frame);
  // tl0PicIdx 0, temporal id 1.
  frame.Reset();
  packet.timestamp = 1;
  packet.seqNum = 1;
  vp8_header.tl0PicIdx = 0;
  vp8_header.temporalIdx = 1;
  vp8_header.pictureId = 1;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_TRUE(dec_state.ContinuousFrame(&frame));
  dec_state.SetState(&frame);
  EXPECT_TRUE(dec_state.full_sync());
  // Lost tl0PicIdx 0, temporal id 2.
  // Insert tl0PicIdx 0, temporal id 3.
  frame.Reset();
  packet.timestamp = 3;
  packet.seqNum = 3;
  vp8_header.tl0PicIdx = 0;
  vp8_header.temporalIdx = 3;
  vp8_header.pictureId = 3;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_FALSE(dec_state.ContinuousFrame(&frame));
  dec_state.SetState(&frame);
  EXPECT_FALSE(dec_state.full_sync());
  // Insert next base layer
  frame.Reset();
  packet.timestamp = 4;
  packet.seqNum = 4;
  vp8_header.tl0PicIdx = 1;
  vp8_header.temporalIdx = 0;
  vp8_header.pictureId = 4;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_TRUE(dec_state.ContinuousFrame(&frame));
  dec_state.SetState(&frame);
  EXPECT_FALSE(dec_state.full_sync());
  // Insert key frame - should update sync value.
  // A key frame is always a base layer.
  frame.Reset();
  packet.video_header.frame_type = VideoFrameType::kVideoFrameKey;
  packet.video_header.is_first_packet_in_frame = true;
  packet.timestamp = 5;
  packet.seqNum = 5;
  vp8_header.tl0PicIdx = 2;
  vp8_header.temporalIdx = 0;
  vp8_header.pictureId = 5;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_TRUE(dec_state.ContinuousFrame(&frame));
  dec_state.SetState(&frame);
  EXPECT_TRUE(dec_state.full_sync());
  // After sync, a continuous PictureId is required
  // (continuous base layer is not enough )
  frame.Reset();
  packet.video_header.frame_type = VideoFrameType::kVideoFrameDelta;
  packet.timestamp = 6;
  packet.seqNum = 6;
  vp8_header.tl0PicIdx = 3;
  vp8_header.temporalIdx = 0;
  vp8_header.pictureId = 6;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_TRUE(dec_state.ContinuousFrame(&frame));
  EXPECT_TRUE(dec_state.full_sync());
  frame.Reset();
  packet.video_header.frame_type = VideoFrameType::kVideoFrameDelta;
  packet.video_header.is_first_packet_in_frame = true;
  packet.timestamp = 8;
  packet.seqNum = 8;
  vp8_header.tl0PicIdx = 4;
  vp8_header.temporalIdx = 0;
  vp8_header.pictureId = 8;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_FALSE(dec_state.ContinuousFrame(&frame));
  EXPECT_TRUE(dec_state.full_sync());
  dec_state.SetState(&frame);
  EXPECT_FALSE(dec_state.full_sync());

  // Insert a non-ref frame - should update sync value.
  frame.Reset();
  packet.video_header.frame_type = VideoFrameType::kVideoFrameDelta;
  packet.video_header.is_first_packet_in_frame = true;
  packet.timestamp = 9;
  packet.seqNum = 9;
  vp8_header.tl0PicIdx = 4;
  vp8_header.temporalIdx = 2;
  vp8_header.pictureId = 9;
  vp8_header.layerSync = true;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  dec_state.SetState(&frame);
  EXPECT_TRUE(dec_state.full_sync());

  // The following test will verify the sync flag behavior after a loss.
  // Create the following pattern:
  // Update base layer, lose packet 1 (sync flag on, layer 2), insert packet 3
  // (sync flag on, layer 2) check continuity and sync flag after inserting
  // packet 2 (sync flag on, layer 1).
  // Base layer.
  frame.Reset();
  dec_state.Reset();
  packet.video_header.frame_type = VideoFrameType::kVideoFrameDelta;
  packet.video_header.is_first_packet_in_frame = true;
  packet.markerBit = 1;
  packet.timestamp = 0;
  packet.seqNum = 0;
  vp8_header.tl0PicIdx = 0;
  vp8_header.temporalIdx = 0;
  vp8_header.pictureId = 0;
  vp8_header.layerSync = false;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  dec_state.SetState(&frame);
  EXPECT_TRUE(dec_state.full_sync());
  // Layer 2 - 2 packets (insert one, lose one).
  frame.Reset();
  packet.video_header.frame_type = VideoFrameType::kVideoFrameDelta;
  packet.video_header.is_first_packet_in_frame = true;
  packet.markerBit = 0;
  packet.timestamp = 1;
  packet.seqNum = 1;
  vp8_header.tl0PicIdx = 0;
  vp8_header.temporalIdx = 2;
  vp8_header.pictureId = 1;
  vp8_header.layerSync = true;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_TRUE(dec_state.ContinuousFrame(&frame));
  // Layer 1
  frame.Reset();
  packet.video_header.frame_type = VideoFrameType::kVideoFrameDelta;
  packet.video_header.is_first_packet_in_frame = true;
  packet.markerBit = 1;
  packet.timestamp = 2;
  packet.seqNum = 3;
  vp8_header.tl0PicIdx = 0;
  vp8_header.temporalIdx = 1;
  vp8_header.pictureId = 2;
  vp8_header.layerSync = true;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_FALSE(dec_state.ContinuousFrame(&frame));
  EXPECT_TRUE(dec_state.full_sync());
}

TEST(TestDecodingState, DiscontinuousPicIdContinuousSeqNum) {
  VCMDecodingState dec_state;
  VCMFrameBuffer frame;
  VCMPacket packet;
  frame.Reset();
  packet.video_header.frame_type = VideoFrameType::kVideoFrameKey;
  packet.video_header.codec = kVideoCodecVP8;
  packet.timestamp = 0;
  packet.seqNum = 0;
  auto& vp8_header =
      packet.video_header.video_type_header.emplace<RTPVideoHeaderVP8>();
  vp8_header.tl0PicIdx = 0;
  vp8_header.temporalIdx = 0;
  vp8_header.pictureId = 0;
  FrameData frame_data;
  frame_data.rtt_ms = 0;
  frame_data.rolling_average_packets_per_frame = -1;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  dec_state.SetState(&frame);
  EXPECT_TRUE(dec_state.full_sync());

  // Continuous sequence number but discontinuous picture id. This implies a
  // a loss and we have to fall back to only decoding the base layer.
  frame.Reset();
  packet.video_header.frame_type = VideoFrameType::kVideoFrameDelta;
  packet.timestamp += 3000;
  ++packet.seqNum;
  vp8_header.temporalIdx = 1;
  vp8_header.pictureId = 2;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_FALSE(dec_state.ContinuousFrame(&frame));
  dec_state.SetState(&frame);
  EXPECT_FALSE(dec_state.full_sync());
}

TEST(TestDecodingState, OldInput) {
  VCMDecodingState dec_state;
  // Identify packets belonging to old frames/packets.
  // Set state for current frames.
  VCMFrameBuffer frame;
  VCMPacket packet;
  packet.timestamp = 10;
  packet.seqNum = 1;
  FrameData frame_data;
  frame_data.rtt_ms = 0;
  frame_data.rolling_average_packets_per_frame = -1;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  dec_state.SetState(&frame);
  packet.timestamp = 9;
  EXPECT_TRUE(dec_state.IsOldPacket(&packet));
  // Check for old frame
  frame.Reset();
  frame.InsertPacket(packet, 0, frame_data);
  EXPECT_TRUE(dec_state.IsOldFrame(&frame));
}

TEST(TestDecodingState, PictureIdRepeat) {
  VCMDecodingState dec_state;
  VCMFrameBuffer frame;
  VCMPacket packet;
  packet.video_header.frame_type = VideoFrameType::kVideoFrameDelta;
  packet.video_header.codec = kVideoCodecVP8;
  packet.timestamp = 0;
  packet.seqNum = 0;
  auto& vp8_header =
      packet.video_header.video_type_header.emplace<RTPVideoHeaderVP8>();
  vp8_header.tl0PicIdx = 0;
  vp8_header.temporalIdx = 0;
  vp8_header.pictureId = 0;
  FrameData frame_data;
  frame_data.rtt_ms = 0;
  frame_data.rolling_average_packets_per_frame = -1;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  dec_state.SetState(&frame);
  // tl0PicIdx 0, temporal id 1.
  frame.Reset();
  ++packet.timestamp;
  ++packet.seqNum;
  vp8_header.temporalIdx++;
  vp8_header.pictureId++;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_TRUE(dec_state.ContinuousFrame(&frame));
  frame.Reset();
  // Testing only gap in tl0PicIdx when tl0PicIdx in continuous.
  vp8_header.tl0PicIdx += 3;
  vp8_header.temporalIdx++;
  vp8_header.tl0PicIdx = 1;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_FALSE(dec_state.ContinuousFrame(&frame));
}

TEST(TestDecodingState, FrameContinuityFlexibleModeKeyFrame) {
  VCMDecodingState dec_state;
  VCMFrameBuffer frame;
  VCMPacket packet;
  packet.video_header.is_first_packet_in_frame = true;
  packet.timestamp = 1;
  packet.seqNum = 0xffff;
  uint8_t data[] = "I need a data pointer for this test!";
  packet.sizeBytes = sizeof(data);
  packet.dataPtr = data;
  packet.video_header.codec = kVideoCodecVP9;

  auto& vp9_hdr =
      packet.video_header.video_type_header.emplace<RTPVideoHeaderVP9>();
  vp9_hdr.picture_id = 10;
  vp9_hdr.flexible_mode = true;

  FrameData frame_data;
  frame_data.rtt_ms = 0;
  frame_data.rolling_average_packets_per_frame = -1;

  // Key frame as first frame
  packet.video_header.frame_type = VideoFrameType::kVideoFrameKey;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_TRUE(dec_state.ContinuousFrame(&frame));
  dec_state.SetState(&frame);

  // Key frame again
  vp9_hdr.picture_id = 11;
  frame.Reset();
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_TRUE(dec_state.ContinuousFrame(&frame));
  dec_state.SetState(&frame);

  // Ref to 11, continuous
  frame.Reset();
  packet.video_header.frame_type = VideoFrameType::kVideoFrameDelta;
  vp9_hdr.picture_id = 12;
  vp9_hdr.num_ref_pics = 1;
  vp9_hdr.pid_diff[0] = 1;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_TRUE(dec_state.ContinuousFrame(&frame));
}

TEST(TestDecodingState, FrameContinuityFlexibleModeOutOfOrderFrames) {
  VCMDecodingState dec_state;
  VCMFrameBuffer frame;
  VCMPacket packet;
  packet.video_header.is_first_packet_in_frame = true;
  packet.timestamp = 1;
  packet.seqNum = 0xffff;
  uint8_t data[] = "I need a data pointer for this test!";
  packet.sizeBytes = sizeof(data);
  packet.dataPtr = data;
  packet.video_header.codec = kVideoCodecVP9;

  auto& vp9_hdr =
      packet.video_header.video_type_header.emplace<RTPVideoHeaderVP9>();
  vp9_hdr.picture_id = 10;
  vp9_hdr.flexible_mode = true;

  FrameData frame_data;
  frame_data.rtt_ms = 0;
  frame_data.rolling_average_packets_per_frame = -1;

  // Key frame as first frame
  packet.video_header.frame_type = VideoFrameType::kVideoFrameKey;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_TRUE(dec_state.ContinuousFrame(&frame));
  dec_state.SetState(&frame);

  // Ref to 10, continuous
  frame.Reset();
  packet.video_header.frame_type = VideoFrameType::kVideoFrameDelta;
  vp9_hdr.picture_id = 15;
  vp9_hdr.num_ref_pics = 1;
  vp9_hdr.pid_diff[0] = 5;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_TRUE(dec_state.ContinuousFrame(&frame));
  dec_state.SetState(&frame);

  // Out of order, last id 15, this id 12, ref to 10, continuous
  frame.Reset();
  vp9_hdr.picture_id = 12;
  vp9_hdr.pid_diff[0] = 2;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_TRUE(dec_state.ContinuousFrame(&frame));
  dec_state.SetState(&frame);

  // Ref 10, 12, 15, continuous
  frame.Reset();
  vp9_hdr.picture_id = 20;
  vp9_hdr.num_ref_pics = 3;
  vp9_hdr.pid_diff[0] = 10;
  vp9_hdr.pid_diff[1] = 8;
  vp9_hdr.pid_diff[2] = 5;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_TRUE(dec_state.ContinuousFrame(&frame));
}

TEST(TestDecodingState, FrameContinuityFlexibleModeGeneral) {
  VCMDecodingState dec_state;
  VCMFrameBuffer frame;
  VCMPacket packet;
  packet.video_header.is_first_packet_in_frame = true;
  packet.timestamp = 1;
  packet.seqNum = 0xffff;
  uint8_t data[] = "I need a data pointer for this test!";
  packet.sizeBytes = sizeof(data);
  packet.dataPtr = data;
  packet.video_header.codec = kVideoCodecVP9;

  auto& vp9_hdr =
      packet.video_header.video_type_header.emplace<RTPVideoHeaderVP9>();
  vp9_hdr.picture_id = 10;
  vp9_hdr.flexible_mode = true;

  FrameData frame_data;
  frame_data.rtt_ms = 0;
  frame_data.rolling_average_packets_per_frame = -1;

  // Key frame as first frame
  packet.video_header.frame_type = VideoFrameType::kVideoFrameKey;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_TRUE(dec_state.ContinuousFrame(&frame));

  // Delta frame as first frame
  frame.Reset();
  packet.video_header.frame_type = VideoFrameType::kVideoFrameDelta;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_FALSE(dec_state.ContinuousFrame(&frame));

  // Key frame then delta frame
  frame.Reset();
  packet.video_header.frame_type = VideoFrameType::kVideoFrameKey;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  dec_state.SetState(&frame);
  frame.Reset();
  packet.video_header.frame_type = VideoFrameType::kVideoFrameDelta;
  vp9_hdr.num_ref_pics = 1;
  vp9_hdr.picture_id = 15;
  vp9_hdr.pid_diff[0] = 5;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_TRUE(dec_state.ContinuousFrame(&frame));
  dec_state.SetState(&frame);

  // Ref to 11, not continuous
  frame.Reset();
  vp9_hdr.picture_id = 16;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_FALSE(dec_state.ContinuousFrame(&frame));

  // Ref to 15, continuous
  frame.Reset();
  vp9_hdr.picture_id = 16;
  vp9_hdr.pid_diff[0] = 1;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_TRUE(dec_state.ContinuousFrame(&frame));
  dec_state.SetState(&frame);

  // Ref to 11 and 15, not continuous
  frame.Reset();
  vp9_hdr.picture_id = 20;
  vp9_hdr.num_ref_pics = 2;
  vp9_hdr.pid_diff[0] = 9;
  vp9_hdr.pid_diff[1] = 5;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_FALSE(dec_state.ContinuousFrame(&frame));

  // Ref to 10, 15 and 16, continuous
  frame.Reset();
  vp9_hdr.picture_id = 22;
  vp9_hdr.num_ref_pics = 3;
  vp9_hdr.pid_diff[0] = 12;
  vp9_hdr.pid_diff[1] = 7;
  vp9_hdr.pid_diff[2] = 6;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_TRUE(dec_state.ContinuousFrame(&frame));
  dec_state.SetState(&frame);

  // Key Frame, continuous
  frame.Reset();
  packet.video_header.frame_type = VideoFrameType::kVideoFrameKey;
  vp9_hdr.picture_id = VCMDecodingState::kFrameDecodedLength - 2;
  vp9_hdr.num_ref_pics = 0;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_TRUE(dec_state.ContinuousFrame(&frame));
  dec_state.SetState(&frame);

  // Frame at last index, ref to KF, continuous
  frame.Reset();
  packet.video_header.frame_type = VideoFrameType::kVideoFrameDelta;
  vp9_hdr.picture_id = VCMDecodingState::kFrameDecodedLength - 1;
  vp9_hdr.num_ref_pics = 1;
  vp9_hdr.pid_diff[0] = 1;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_TRUE(dec_state.ContinuousFrame(&frame));
  dec_state.SetState(&frame);

  // Frame after wrapping buffer length, ref to last index, continuous
  frame.Reset();
  vp9_hdr.picture_id = 0;
  vp9_hdr.num_ref_pics = 1;
  vp9_hdr.pid_diff[0] = 1;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_TRUE(dec_state.ContinuousFrame(&frame));
  dec_state.SetState(&frame);

  // Frame after wrapping start frame, ref to 0, continuous
  frame.Reset();
  vp9_hdr.picture_id = 20;
  vp9_hdr.num_ref_pics = 1;
  vp9_hdr.pid_diff[0] = 20;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_TRUE(dec_state.ContinuousFrame(&frame));
  dec_state.SetState(&frame);

  // Frame after wrapping start frame, ref to 10, not continuous
  frame.Reset();
  vp9_hdr.picture_id = 23;
  vp9_hdr.num_ref_pics = 1;
  vp9_hdr.pid_diff[0] = 13;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_FALSE(dec_state.ContinuousFrame(&frame));

  // Key frame, continuous
  frame.Reset();
  packet.video_header.frame_type = VideoFrameType::kVideoFrameKey;
  vp9_hdr.picture_id = 25;
  vp9_hdr.num_ref_pics = 0;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_TRUE(dec_state.ContinuousFrame(&frame));
  dec_state.SetState(&frame);

  // Ref to KF, continuous
  frame.Reset();
  packet.video_header.frame_type = VideoFrameType::kVideoFrameDelta;
  vp9_hdr.picture_id = 26;
  vp9_hdr.num_ref_pics = 1;
  vp9_hdr.pid_diff[0] = 1;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_TRUE(dec_state.ContinuousFrame(&frame));
  dec_state.SetState(&frame);

  // Ref to frame previous to KF, not continuous
  frame.Reset();
  vp9_hdr.picture_id = 30;
  vp9_hdr.num_ref_pics = 1;
  vp9_hdr.pid_diff[0] = 30;
  EXPECT_LE(0, frame.InsertPacket(packet, 0, frame_data));
  EXPECT_FALSE(dec_state.ContinuousFrame(&frame));
}

}  // namespace webrtc