aboutsummaryrefslogtreecommitdiff
path: root/webrtc/modules/video_coding/main/source/qm_select_unittest.cc
blob: 6abc0d309982929d451382a81494fe840d6107b8 (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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
/*
 *  Copyright (c) 2012 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.
 */

/*
 * This file includes unit tests the QmResolution class
 * In particular, for the selection of spatial and/or temporal down-sampling.
 */

#include "testing/gtest/include/gtest/gtest.h"

#include "webrtc/modules/interface/module_common_types.h"
#include "webrtc/modules/video_coding/main/source/qm_select.h"

namespace webrtc {

// Representative values of content metrics for: low/high/medium(default) state,
// based on parameters settings in qm_select_data.h.
const float kSpatialLow = 0.01f;
const float kSpatialMedium = 0.03f;
const float kSpatialHigh = 0.1f;
const float kTemporalLow = 0.01f;
const float kTemporalMedium = 0.06f;
const float kTemporalHigh = 0.1f;

class QmSelectTest : public ::testing::Test {
 protected:
  QmSelectTest()
      :  qm_resolution_(new VCMQmResolution()),
         content_metrics_(new VideoContentMetrics()),
         qm_scale_(NULL) {
  }
  VCMQmResolution* qm_resolution_;
  VideoContentMetrics* content_metrics_;
  VCMResolutionScale* qm_scale_;

  void InitQmNativeData(float initial_bit_rate,
                        int user_frame_rate,
                        int native_width,
                        int native_height,
                        int num_layers);

  void UpdateQmEncodedFrame(size_t* encoded_size, size_t num_updates);

  void UpdateQmRateData(int* target_rate,
                        int* encoder_sent_rate,
                        int* incoming_frame_rate,
                        uint8_t* fraction_lost,
                        int num_updates);

  void UpdateQmContentData(float motion_metric,
                           float spatial_metric,
                           float spatial_metric_horiz,
                           float spatial_metric_vert);

  bool IsSelectedActionCorrect(VCMResolutionScale* qm_scale,
                               float fac_width,
                               float fac_height,
                               float fac_temp,
                               uint16_t new_width,
                               uint16_t new_height,
                               float new_frame_rate);

  void TearDown() {
    delete qm_resolution_;
    delete content_metrics_;
  }
};

TEST_F(QmSelectTest, HandleInputs) {
  // Expect parameter error. Initialize with invalid inputs.
  EXPECT_EQ(-4, qm_resolution_->Initialize(1000, 0, 640, 480, 1));
  EXPECT_EQ(-4, qm_resolution_->Initialize(1000, 30, 640, 0, 1));
  EXPECT_EQ(-4, qm_resolution_->Initialize(1000, 30, 0, 480, 1));

  // Expect uninitialized error.: No valid initialization before selection.
  EXPECT_EQ(-7, qm_resolution_->SelectResolution(&qm_scale_));

  VideoContentMetrics* content_metrics = NULL;
  EXPECT_EQ(0, qm_resolution_->Initialize(1000, 30, 640, 480, 1));
  qm_resolution_->UpdateContent(content_metrics);
  // Content metrics are NULL: Expect success and no down-sampling action.
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 1.0, 1.0, 1.0, 640, 480,
                                      30.0f));
}

// TODO(marpan): Add a test for number of temporal layers > 1.

// No down-sampling action at high rates.
TEST_F(QmSelectTest, NoActionHighRate) {
  // Initialize with bitrate, frame rate, native system width/height, and
  // number of temporal layers.
  InitQmNativeData(800, 30, 640, 480, 1);

  // Update with encoder frame size.
  uint16_t codec_width = 640;
  uint16_t codec_height = 480;
  qm_resolution_->UpdateCodecParameters(30.0f, codec_width, codec_height);
  EXPECT_EQ(5, qm_resolution_->GetImageType(codec_width, codec_height));

  // Update rates for a sequence of intervals.
  int target_rate[] = {800, 800, 800};
  int encoder_sent_rate[] = {800, 800, 800};
  int incoming_frame_rate[] = {30, 30, 30};
  uint8_t fraction_lost[] = {10, 10, 10};
  UpdateQmRateData(target_rate, encoder_sent_rate, incoming_frame_rate,
                   fraction_lost, 3);

  // Update content: motion level, and 3 spatial prediction errors.
  UpdateQmContentData(kTemporalLow, kSpatialLow, kSpatialLow, kSpatialLow);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(0, qm_resolution_->ComputeContentClass());
  EXPECT_EQ(kStableEncoding, qm_resolution_->GetEncoderState());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 1.0f, 1.0f, 1.0f, 640, 480,
                                      30.0f));
}

// Rate is well below transition, down-sampling action is taken,
// depending on the content state.
TEST_F(QmSelectTest, DownActionLowRate) {
  // Initialize with bitrate, frame rate, native system width/height, and
  // number of temporal layers.
  InitQmNativeData(50, 30, 640, 480, 1);

  // Update with encoder frame size.
  uint16_t codec_width = 640;
  uint16_t codec_height = 480;
  qm_resolution_->UpdateCodecParameters(30.0f, codec_width, codec_height);
  EXPECT_EQ(5, qm_resolution_->GetImageType(codec_width, codec_height));

  // Update rates for a sequence of intervals.
  int target_rate[] = {50, 50, 50};
  int encoder_sent_rate[] = {50, 50, 50};
  int incoming_frame_rate[] = {30, 30, 30};
  uint8_t fraction_lost[] = {10, 10, 10};
  UpdateQmRateData(target_rate, encoder_sent_rate, incoming_frame_rate,
                   fraction_lost, 3);

  // Update content: motion level, and 3 spatial prediction errors.
  // High motion, low spatial: 2x2 spatial expected.
  UpdateQmContentData(kTemporalHigh, kSpatialLow, kSpatialLow, kSpatialLow);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(3, qm_resolution_->ComputeContentClass());
  EXPECT_EQ(kStableEncoding, qm_resolution_->GetEncoderState());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 2.0f, 2.0f, 1.0f, 320, 240,
                                      30.0f));

  qm_resolution_->ResetDownSamplingState();
  // Low motion, low spatial: 2/3 temporal is expected.
  UpdateQmContentData(kTemporalLow, kSpatialLow, kSpatialLow, kSpatialLow);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(0, qm_resolution_->ComputeContentClass());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 1.0f, 1.0f, 1.5f, 640, 480,
                                      20.5f));

  qm_resolution_->ResetDownSamplingState();
  // Medium motion, low spatial: 2x2 spatial expected.
  UpdateQmContentData(kTemporalMedium, kSpatialLow, kSpatialLow, kSpatialLow);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(6, qm_resolution_->ComputeContentClass());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 2.0f, 2.0f, 1.0f, 320, 240,
                                      30.0f));

  qm_resolution_->ResetDownSamplingState();
  // High motion, high spatial: 2/3 temporal expected.
  UpdateQmContentData(kTemporalHigh, kSpatialHigh, kSpatialHigh, kSpatialHigh);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(4, qm_resolution_->ComputeContentClass());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 1.0f, 1.0f, 1.5f, 640, 480,
                                      20.5f));

  qm_resolution_->ResetDownSamplingState();
  // Low motion, high spatial: 1/2 temporal expected.
  UpdateQmContentData(kTemporalLow, kSpatialHigh, kSpatialHigh, kSpatialHigh);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(1, qm_resolution_->ComputeContentClass());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 1.0f, 1.0f, 2.0f, 640, 480,
                                      15.5f));

  qm_resolution_->ResetDownSamplingState();
  // Medium motion, high spatial: 1/2 temporal expected.
  UpdateQmContentData(kTemporalMedium, kSpatialHigh, kSpatialHigh,
                      kSpatialHigh);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(7, qm_resolution_->ComputeContentClass());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 1.0f, 1.0f, 2.0f, 640, 480,
                                      15.5f));

  qm_resolution_->ResetDownSamplingState();
  // High motion, medium spatial: 2x2 spatial expected.
  UpdateQmContentData(kTemporalHigh, kSpatialMedium, kSpatialMedium,
                      kSpatialMedium);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(5, qm_resolution_->ComputeContentClass());
  // Target frame rate for frame dropper should be the same as previous == 15.
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 2.0f, 2.0f, 1.0f, 320, 240,
                                      30.0f));

  qm_resolution_->ResetDownSamplingState();
  // Low motion, medium spatial: high frame rate, so 1/2 temporal expected.
  UpdateQmContentData(kTemporalLow, kSpatialMedium, kSpatialMedium,
                      kSpatialMedium);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(2, qm_resolution_->ComputeContentClass());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 1.0f, 1.0f, 2.0f, 640, 480,
                                      15.5f));

  qm_resolution_->ResetDownSamplingState();
  // Medium motion, medium spatial: high frame rate, so 2/3 temporal expected.
  UpdateQmContentData(kTemporalMedium, kSpatialMedium, kSpatialMedium,
                      kSpatialMedium);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(8, qm_resolution_->ComputeContentClass());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 1.0f, 1.0f, 1.5f, 640, 480,
                                      20.5f));
}

// Rate mis-match is high, and we have over-shooting.
// since target rate is below max for down-sampling, down-sampling is selected.
TEST_F(QmSelectTest, DownActionHighRateMMOvershoot) {
  // Initialize with bitrate, frame rate, native system width/height, and
  // number of temporal layers.
  InitQmNativeData(300, 30, 640, 480, 1);

  // Update with encoder frame size.
  uint16_t codec_width = 640;
  uint16_t codec_height = 480;
  qm_resolution_->UpdateCodecParameters(30.0f, codec_width, codec_height);
  EXPECT_EQ(5, qm_resolution_->GetImageType(codec_width, codec_height));

  // Update rates for a sequence of intervals.
  int target_rate[] = {300, 300, 300};
  int encoder_sent_rate[] = {900, 900, 900};
  int incoming_frame_rate[] = {30, 30, 30};
  uint8_t fraction_lost[] = {10, 10, 10};
  UpdateQmRateData(target_rate, encoder_sent_rate, incoming_frame_rate,
                   fraction_lost, 3);

  // Update content: motion level, and 3 spatial prediction errors.
  // High motion, low spatial.
  UpdateQmContentData(kTemporalHigh, kSpatialLow, kSpatialLow, kSpatialLow);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(3, qm_resolution_->ComputeContentClass());
  EXPECT_EQ(kStressedEncoding, qm_resolution_->GetEncoderState());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 4.0f / 3.0f, 4.0f / 3.0f,
                                      1.0f, 480, 360, 30.0f));

  qm_resolution_->ResetDownSamplingState();
  // Low motion, high spatial
  UpdateQmContentData(kTemporalLow, kSpatialHigh, kSpatialHigh, kSpatialHigh);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(1, qm_resolution_->ComputeContentClass());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 1.0f, 1.0f, 1.5f, 640, 480,
                                      20.5f));
}

// Rate mis-match is high, target rate is below max for down-sampling,
// but since we have consistent under-shooting, no down-sampling action.
TEST_F(QmSelectTest, NoActionHighRateMMUndershoot) {
  // Initialize with bitrate, frame rate, native system width/height, and
  // number of temporal layers.
  InitQmNativeData(300, 30, 640, 480, 1);

  // Update with encoder frame size.
  uint16_t codec_width = 640;
  uint16_t codec_height = 480;
  qm_resolution_->UpdateCodecParameters(30.0f, codec_width, codec_height);
  EXPECT_EQ(5, qm_resolution_->GetImageType(codec_width, codec_height));

  // Update rates for a sequence of intervals.
  int target_rate[] = {300, 300, 300};
  int encoder_sent_rate[] = {100, 100, 100};
  int incoming_frame_rate[] = {30, 30, 30};
  uint8_t fraction_lost[] = {10, 10, 10};
  UpdateQmRateData(target_rate, encoder_sent_rate, incoming_frame_rate,
                   fraction_lost, 3);

  // Update content: motion level, and 3 spatial prediction errors.
  // High motion, low spatial.
  UpdateQmContentData(kTemporalHigh, kSpatialLow, kSpatialLow, kSpatialLow);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(3, qm_resolution_->ComputeContentClass());
  EXPECT_EQ(kEasyEncoding, qm_resolution_->GetEncoderState());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 1.0f, 1.0f, 1.0f, 640, 480,
                                      30.0f));

  qm_resolution_->ResetDownSamplingState();
  // Low motion, high spatial
  UpdateQmContentData(kTemporalLow, kSpatialHigh, kSpatialHigh, kSpatialHigh);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(1, qm_resolution_->ComputeContentClass());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 1.0f, 1.0f, 1.0f, 640, 480,
                                      30.0f));
}

// Buffer is underflowing, and target rate is below max for down-sampling,
// so action is taken.
TEST_F(QmSelectTest, DownActionBufferUnderflow) {
  // Initialize with bitrate, frame rate, native system width/height, and
  // number of temporal layers.
  InitQmNativeData(300, 30, 640, 480, 1);

  // Update with encoder frame size.
  uint16_t codec_width = 640;
  uint16_t codec_height = 480;
  qm_resolution_->UpdateCodecParameters(30.0f, codec_width, codec_height);
  EXPECT_EQ(5, qm_resolution_->GetImageType(codec_width, codec_height));

  // Update with encoded size over a number of frames.
  // per-frame bandwidth = 15 = 450/30: simulate (decoder) buffer underflow:
  size_t encoded_size[] = {200, 100, 50, 30, 60, 40, 20, 30, 20, 40};
  UpdateQmEncodedFrame(encoded_size, GTEST_ARRAY_SIZE_(encoded_size));

  // Update rates for a sequence of intervals.
  int target_rate[] = {300, 300, 300};
  int encoder_sent_rate[] = {450, 450, 450};
  int incoming_frame_rate[] = {30, 30, 30};
  uint8_t fraction_lost[] = {10, 10, 10};
  UpdateQmRateData(target_rate, encoder_sent_rate, incoming_frame_rate,
                   fraction_lost, 3);

  // Update content: motion level, and 3 spatial prediction errors.
  // High motion, low spatial.
  UpdateQmContentData(kTemporalHigh, kSpatialLow, kSpatialLow, kSpatialLow);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(3, qm_resolution_->ComputeContentClass());
  EXPECT_EQ(kStressedEncoding, qm_resolution_->GetEncoderState());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 4.0f / 3.0f, 4.0f / 3.0f,
                                      1.0f, 480, 360, 30.0f));

  qm_resolution_->ResetDownSamplingState();
  // Low motion, high spatial
  UpdateQmContentData(kTemporalLow, kSpatialHigh, kSpatialHigh, kSpatialHigh);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(1, qm_resolution_->ComputeContentClass());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 1.0f, 1.0f, 1.5f, 640, 480,
                                      20.5f));
}

// Target rate is below max for down-sampling, but buffer level is stable,
// so no action is taken.
TEST_F(QmSelectTest, NoActionBufferStable) {
  // Initialize with bitrate, frame rate, native system width/height, and
  // number of temporal layers.
  InitQmNativeData(350, 30, 640, 480, 1);

  // Update with encoder frame size.
  uint16_t codec_width = 640;
  uint16_t codec_height = 480;
  qm_resolution_->UpdateCodecParameters(30.0f, codec_width, codec_height);
  EXPECT_EQ(5, qm_resolution_->GetImageType(codec_width, codec_height));

  // Update with encoded size over a number of frames.
  // per-frame bandwidth = 15 = 450/30: simulate stable (decoder) buffer levels.
  size_t encoded_size[] = {40, 10, 10, 16, 18, 20, 17, 20, 16, 15};
  UpdateQmEncodedFrame(encoded_size, GTEST_ARRAY_SIZE_(encoded_size));

  // Update rates for a sequence of intervals.
  int target_rate[] = {350, 350, 350};
  int encoder_sent_rate[] = {350, 450, 450};
  int incoming_frame_rate[] = {30, 30, 30};
  uint8_t fraction_lost[] = {10, 10, 10};
  UpdateQmRateData(target_rate, encoder_sent_rate, incoming_frame_rate,
                   fraction_lost, 3);

  // Update content: motion level, and 3 spatial prediction errors.
  // High motion, low spatial.
  UpdateQmContentData(kTemporalHigh, kSpatialLow, kSpatialLow, kSpatialLow);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(3, qm_resolution_->ComputeContentClass());
  EXPECT_EQ(kStableEncoding, qm_resolution_->GetEncoderState());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 1.0f, 1.0f, 1.0f, 640, 480,
                                      30.0f));

  qm_resolution_->ResetDownSamplingState();
  // Low motion, high spatial
  UpdateQmContentData(kTemporalLow, kSpatialHigh, kSpatialHigh, kSpatialHigh);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(1, qm_resolution_->ComputeContentClass());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 1.0f, 1.0f, 1.0f, 640, 480,
                                      30.0f));
}

// Very low rate, but no spatial down-sampling below some size (QCIF).
TEST_F(QmSelectTest, LimitDownSpatialAction) {
  // Initialize with bitrate, frame rate, native system width/height, and
  // number of temporal layers.
  InitQmNativeData(10, 30, 176, 144, 1);

  // Update with encoder frame size.
  uint16_t codec_width = 176;
  uint16_t codec_height = 144;
  qm_resolution_->UpdateCodecParameters(30.0f, codec_width, codec_height);
  EXPECT_EQ(0, qm_resolution_->GetImageType(codec_width, codec_height));

  // Update rates for a sequence of intervals.
  int target_rate[] = {10, 10, 10};
  int encoder_sent_rate[] = {10, 10, 10};
  int incoming_frame_rate[] = {30, 30, 30};
  uint8_t fraction_lost[] = {10, 10, 10};
  UpdateQmRateData(target_rate, encoder_sent_rate, incoming_frame_rate,
                   fraction_lost, 3);

  // Update content: motion level, and 3 spatial prediction errors.
  // High motion, low spatial.
  UpdateQmContentData(kTemporalHigh, kSpatialLow, kSpatialLow, kSpatialLow);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(3, qm_resolution_->ComputeContentClass());
  EXPECT_EQ(kStableEncoding, qm_resolution_->GetEncoderState());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 1.0f, 1.0f, 1.0f, 176, 144,
                                      30.0f));
}

// Very low rate, but no frame reduction below some frame_rate (8fps).
TEST_F(QmSelectTest, LimitDownTemporalAction) {
  // Initialize with bitrate, frame rate, native system width/height, and
  // number of temporal layers.
  InitQmNativeData(10, 8, 640, 480, 1);

  // Update with encoder frame size.
  uint16_t codec_width = 640;
  uint16_t codec_height = 480;
  qm_resolution_->UpdateCodecParameters(8.0f, codec_width, codec_height);
  EXPECT_EQ(5, qm_resolution_->GetImageType(codec_width, codec_height));

  // Update rates for a sequence of intervals.
  int target_rate[] = {10, 10, 10};
  int encoder_sent_rate[] = {10, 10, 10};
  int incoming_frame_rate[] = {8, 8, 8};
  uint8_t fraction_lost[] = {10, 10, 10};
  UpdateQmRateData(target_rate, encoder_sent_rate, incoming_frame_rate,
                   fraction_lost, 3);

  // Update content: motion level, and 3 spatial prediction errors.
  // Low motion, medium spatial.
  UpdateQmContentData(kTemporalLow, kSpatialMedium, kSpatialMedium,
                      kSpatialMedium);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(2, qm_resolution_->ComputeContentClass());
  EXPECT_EQ(kStableEncoding, qm_resolution_->GetEncoderState());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 1.0f, 1.0f, 1.0f, 640, 480,
                                      8.0f));
}

// Two stages: spatial down-sample and then back up spatially,
// as rate as increased.
TEST_F(QmSelectTest, 2StageDownSpatialUpSpatial) {
  // Initialize with bitrate, frame rate, native system width/height, and
  // number of temporal layers.
  InitQmNativeData(50, 30, 640, 480, 1);

  // Update with encoder frame size.
  uint16_t codec_width = 640;
  uint16_t codec_height = 480;
  qm_resolution_->UpdateCodecParameters(30.0f, codec_width, codec_height);
  EXPECT_EQ(5, qm_resolution_->GetImageType(codec_width, codec_height));

  // Update rates for a sequence of intervals.
  int target_rate[] = {50, 50, 50};
  int encoder_sent_rate[] = {50, 50, 50};
  int incoming_frame_rate[] = {30, 30, 30};
  uint8_t fraction_lost[] = {10, 10, 10};
  UpdateQmRateData(target_rate, encoder_sent_rate, incoming_frame_rate,
                    fraction_lost, 3);

  // Update content: motion level, and 3 spatial prediction errors.
  // High motion, low spatial.
  UpdateQmContentData(kTemporalHigh, kSpatialLow, kSpatialLow, kSpatialLow);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(3, qm_resolution_->ComputeContentClass());
  EXPECT_EQ(kStableEncoding, qm_resolution_->GetEncoderState());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 2.0f, 2.0f, 1.0f, 320, 240,
                                      30.0f));

  // Reset and go up in rate: expected to go back up, in 2 stages of 3/4.
  qm_resolution_->ResetRates();
  qm_resolution_->UpdateCodecParameters(30.0f, 320, 240);
  EXPECT_EQ(2, qm_resolution_->GetImageType(320, 240));
  // Update rates for a sequence of intervals.
  int target_rate2[] = {400, 400, 400, 400, 400};
  int encoder_sent_rate2[] = {400, 400, 400, 400, 400};
  int incoming_frame_rate2[] = {30, 30, 30, 30, 30};
  uint8_t fraction_lost2[] = {10, 10, 10, 10, 10};
  UpdateQmRateData(target_rate2, encoder_sent_rate2, incoming_frame_rate2,
                   fraction_lost2, 5);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(kStableEncoding, qm_resolution_->GetEncoderState());
  float scale = (4.0f / 3.0f) / 2.0f;
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, scale, scale, 1.0f, 480, 360,
                                      30.0f));

  qm_resolution_->UpdateCodecParameters(30.0f, 480, 360);
  EXPECT_EQ(4, qm_resolution_->GetImageType(480, 360));
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 3.0f / 4.0f, 3.0f / 4.0f, 1.0f,
                                      640, 480, 30.0f));
}

// Two stages: spatial down-sample and then back up spatially, since encoder
// is under-shooting target even though rate has not increased much.
TEST_F(QmSelectTest, 2StageDownSpatialUpSpatialUndershoot) {
  // Initialize with bitrate, frame rate, native system width/height, and
  // number of temporal layers.
  InitQmNativeData(50, 30, 640, 480, 1);

  // Update with encoder frame size.
  uint16_t codec_width = 640;
  uint16_t codec_height = 480;
  qm_resolution_->UpdateCodecParameters(30.0f, codec_width, codec_height);
  EXPECT_EQ(5, qm_resolution_->GetImageType(codec_width, codec_height));

  // Update rates for a sequence of intervals.
  int target_rate[] = {50, 50, 50};
  int encoder_sent_rate[] = {50, 50, 50};
  int incoming_frame_rate[] = {30, 30, 30};
  uint8_t fraction_lost[] = {10, 10, 10};
  UpdateQmRateData(target_rate, encoder_sent_rate, incoming_frame_rate,
                    fraction_lost, 3);

  // Update content: motion level, and 3 spatial prediction errors.
  // High motion, low spatial.
  UpdateQmContentData(kTemporalHigh, kSpatialLow, kSpatialLow, kSpatialLow);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(3, qm_resolution_->ComputeContentClass());
  EXPECT_EQ(kStableEncoding, qm_resolution_->GetEncoderState());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 2.0f, 2.0f, 1.0f, 320, 240,
                                      30.0f));

  // Reset rates and simulate under-shooting scenario.: expect to go back up.
  // Goes up spatially in two stages for 1/2x1/2 down-sampling.
  qm_resolution_->ResetRates();
  qm_resolution_->UpdateCodecParameters(30.0f, 320, 240);
  EXPECT_EQ(2, qm_resolution_->GetImageType(320, 240));
  // Update rates for a sequence of intervals.
  int target_rate2[] = {200, 200, 200, 200, 200};
  int encoder_sent_rate2[] = {50, 50, 50, 50, 50};
  int incoming_frame_rate2[] = {30, 30, 30, 30, 30};
  uint8_t fraction_lost2[] = {10, 10, 10, 10, 10};
  UpdateQmRateData(target_rate2, encoder_sent_rate2, incoming_frame_rate2,
                   fraction_lost2, 5);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(kEasyEncoding, qm_resolution_->GetEncoderState());
  float scale = (4.0f / 3.0f) / 2.0f;
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, scale, scale, 1.0f, 480, 360,
                                      30.0f));

  qm_resolution_->UpdateCodecParameters(30.0f, 480, 360);
  EXPECT_EQ(4, qm_resolution_->GetImageType(480, 360));
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 3.0f / 4.0f, 3.0f / 4.0f, 1.0f,
                                      640, 480, 30.0f));
}

// Two stages: spatial down-sample and then no action to go up,
// as encoding rate mis-match is too high.
TEST_F(QmSelectTest, 2StageDownSpatialNoActionUp) {
  // Initialize with bitrate, frame rate, native system width/height, and
  // number of temporal layers.
  InitQmNativeData(50, 30, 640, 480, 1);

  // Update with encoder frame size.
  uint16_t codec_width = 640;
  uint16_t codec_height = 480;
  qm_resolution_->UpdateCodecParameters(30.0f, codec_width, codec_height);
  EXPECT_EQ(5, qm_resolution_->GetImageType(codec_width, codec_height));

  // Update rates for a sequence of intervals.
  int target_rate[] = {50, 50, 50};
  int encoder_sent_rate[] = {50, 50, 50};
  int incoming_frame_rate[] = {30, 30, 30};
  uint8_t fraction_lost[] = {10, 10, 10};
  UpdateQmRateData(target_rate, encoder_sent_rate, incoming_frame_rate,
                    fraction_lost, 3);

  // Update content: motion level, and 3 spatial prediction errors.
  // High motion, low spatial.
  UpdateQmContentData(kTemporalHigh, kSpatialLow, kSpatialLow, kSpatialLow);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(3, qm_resolution_->ComputeContentClass());
  EXPECT_EQ(kStableEncoding, qm_resolution_->GetEncoderState());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 2.0f, 2.0f, 1.0f, 320, 240,
                                      30.0f));

  // Reset and simulate large rate mis-match: expect no action to go back up.
  qm_resolution_->ResetRates();
  qm_resolution_->UpdateCodecParameters(30.0f, 320, 240);
  EXPECT_EQ(2, qm_resolution_->GetImageType(320, 240));
  // Update rates for a sequence of intervals.
  int target_rate2[] = {400, 400, 400, 400, 400};
  int encoder_sent_rate2[] = {1000, 1000, 1000, 1000, 1000};
  int incoming_frame_rate2[] = {30, 30, 30, 30, 30};
  uint8_t fraction_lost2[] = {10, 10, 10, 10, 10};
  UpdateQmRateData(target_rate2, encoder_sent_rate2, incoming_frame_rate2,
                   fraction_lost2, 5);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(kStressedEncoding, qm_resolution_->GetEncoderState());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 1.0f, 1.0f, 1.0f, 320, 240,
                                      30.0f));
}

// Two stages: temporally down-sample and then back up temporally,
// as rate as increased.
TEST_F(QmSelectTest, 2StatgeDownTemporalUpTemporal) {
  // Initialize with bitrate, frame rate, native system width/height, and
  // number of temporal layers.
  InitQmNativeData(50, 30, 640, 480, 1);

  // Update with encoder frame size.
  uint16_t codec_width = 640;
  uint16_t codec_height = 480;
  qm_resolution_->UpdateCodecParameters(30.0f, codec_width, codec_height);
  EXPECT_EQ(5, qm_resolution_->GetImageType(codec_width, codec_height));

  // Update rates for a sequence of intervals.
  int target_rate[] = {50, 50, 50};
  int encoder_sent_rate[] = {50, 50, 50};
  int incoming_frame_rate[] = {30, 30, 30};
  uint8_t fraction_lost[] = {10, 10, 10};
  UpdateQmRateData(target_rate, encoder_sent_rate, incoming_frame_rate,
                   fraction_lost, 3);

  // Update content: motion level, and 3 spatial prediction errors.
  // Low motion, high spatial.
  UpdateQmContentData(kTemporalLow, kSpatialHigh, kSpatialHigh, kSpatialHigh);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(1, qm_resolution_->ComputeContentClass());
  EXPECT_EQ(kStableEncoding, qm_resolution_->GetEncoderState());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 1.0f, 1.0f, 2.0f, 640, 480,
                                      15.5f));

  // Reset rates and go up in rate: expect to go back up.
  qm_resolution_->ResetRates();
  // Update rates for a sequence of intervals.
  int target_rate2[] = {400, 400, 400, 400, 400};
  int encoder_sent_rate2[] = {400, 400, 400, 400, 400};
  int incoming_frame_rate2[] = {15, 15, 15, 15, 15};
  uint8_t fraction_lost2[] = {10, 10, 10, 10, 10};
  UpdateQmRateData(target_rate2, encoder_sent_rate2, incoming_frame_rate2,
                   fraction_lost2, 5);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(kStableEncoding, qm_resolution_->GetEncoderState());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 1.0f, 1.0f, 0.5f, 640, 480,
                                      30.0f));
}

// Two stages: temporal down-sample and then back up temporally, since encoder
// is under-shooting target even though rate has not increased much.
TEST_F(QmSelectTest, 2StatgeDownTemporalUpTemporalUndershoot) {
  // Initialize with bitrate, frame rate, native system width/height, and
  // number of temporal layers.
  InitQmNativeData(50, 30, 640, 480, 1);

  // Update with encoder frame size.
  uint16_t codec_width = 640;
  uint16_t codec_height = 480;
  qm_resolution_->UpdateCodecParameters(30.0f, codec_width, codec_height);
  EXPECT_EQ(5, qm_resolution_->GetImageType(codec_width, codec_height));

  // Update rates for a sequence of intervals.
  int target_rate[] = {50, 50, 50};
  int encoder_sent_rate[] = {50, 50, 50};
  int incoming_frame_rate[] = {30, 30, 30};
  uint8_t fraction_lost[] = {10, 10, 10};
  UpdateQmRateData(target_rate, encoder_sent_rate, incoming_frame_rate,
                    fraction_lost, 3);

  // Update content: motion level, and 3 spatial prediction errors.
  // Low motion, high spatial.
  UpdateQmContentData(kTemporalLow, kSpatialHigh, kSpatialHigh, kSpatialHigh);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(1, qm_resolution_->ComputeContentClass());
  EXPECT_EQ(kStableEncoding, qm_resolution_->GetEncoderState());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 1.0f, 1.0f, 2.0f, 640, 480,
                                      15.5f));

  // Reset rates and simulate under-shooting scenario.: expect to go back up.
  qm_resolution_->ResetRates();
  // Update rates for a sequence of intervals.
  int target_rate2[] = {150, 150, 150, 150, 150};
  int encoder_sent_rate2[] = {50, 50, 50, 50, 50};
  int incoming_frame_rate2[] = {15, 15, 15, 15, 15};
  uint8_t fraction_lost2[] = {10, 10, 10, 10, 10};
  UpdateQmRateData(target_rate2, encoder_sent_rate2, incoming_frame_rate2,
                   fraction_lost2, 5);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(kEasyEncoding, qm_resolution_->GetEncoderState());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 1.0f, 1.0f, 0.5f, 640, 480,
                                      30.0f));
}

// Two stages: temporal down-sample and then no action to go up,
// as encoding rate mis-match is too high.
TEST_F(QmSelectTest, 2StageDownTemporalNoActionUp) {
  // Initialize with bitrate, frame rate, native system width/height, and
  // number of temporal layers.
  InitQmNativeData(50, 30, 640, 480, 1);

  // Update with encoder frame size.
  uint16_t codec_width = 640;
  uint16_t codec_height = 480;
  qm_resolution_->UpdateCodecParameters(30.0f, codec_width, codec_height);
  EXPECT_EQ(5, qm_resolution_->GetImageType(codec_width, codec_height));

  // Update rates for a sequence of intervals.
  int target_rate[] = {50, 50, 50};
  int encoder_sent_rate[] = {50, 50, 50};
  int incoming_frame_rate[] = {30, 30, 30};
  uint8_t fraction_lost[] = {10, 10, 10};
  UpdateQmRateData(target_rate, encoder_sent_rate, incoming_frame_rate,
                   fraction_lost, 3);

  // Update content: motion level, and 3 spatial prediction errors.
  // Low motion, high spatial.
  UpdateQmContentData(kTemporalLow, kSpatialHigh, kSpatialHigh, kSpatialHigh);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(1, qm_resolution_->ComputeContentClass());
  EXPECT_EQ(kStableEncoding, qm_resolution_->GetEncoderState());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 1, 1, 2, 640, 480, 15.5f));

  // Reset and simulate large rate mis-match: expect no action to go back up.
  qm_resolution_->UpdateCodecParameters(15.0f, codec_width, codec_height);
  qm_resolution_->ResetRates();
  // Update rates for a sequence of intervals.
  int target_rate2[] = {600, 600, 600, 600, 600};
  int encoder_sent_rate2[] = {1000, 1000, 1000, 1000, 1000};
  int incoming_frame_rate2[] = {15, 15, 15, 15, 15};
  uint8_t fraction_lost2[] = {10, 10, 10, 10, 10};
  UpdateQmRateData(target_rate2, encoder_sent_rate2, incoming_frame_rate2,
                   fraction_lost2, 5);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(kStressedEncoding, qm_resolution_->GetEncoderState());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 1.0f, 1.0f, 1.0f, 640, 480,
                                      15.0f));
}
// 3 stages: spatial down-sample, followed by temporal down-sample,
// and then go up to full state, as encoding rate has increased.
TEST_F(QmSelectTest, 3StageDownSpatialTemporlaUpSpatialTemporal) {
  // Initialize with bitrate, frame rate, native system width/height, and
  // number of temporal layers.
  InitQmNativeData(80, 30, 640, 480, 1);

  // Update with encoder frame size.
  uint16_t codec_width = 640;
  uint16_t codec_height = 480;
  qm_resolution_->UpdateCodecParameters(30.0f, codec_width, codec_height);
  EXPECT_EQ(5, qm_resolution_->GetImageType(codec_width, codec_height));

  // Update rates for a sequence of intervals.
  int target_rate[] = {80, 80, 80};
  int encoder_sent_rate[] = {80, 80, 80};
  int incoming_frame_rate[] = {30, 30, 30};
  uint8_t fraction_lost[] = {10, 10, 10};
  UpdateQmRateData(target_rate, encoder_sent_rate, incoming_frame_rate,
                   fraction_lost, 3);

  // Update content: motion level, and 3 spatial prediction errors.
  // High motion, low spatial.
  UpdateQmContentData(kTemporalHigh, kSpatialLow, kSpatialLow, kSpatialLow);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(3, qm_resolution_->ComputeContentClass());
  EXPECT_EQ(kStableEncoding, qm_resolution_->GetEncoderState());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 2.0f, 2.0f, 1.0f, 320, 240,
                                      30.0f));

  // Change content data: expect temporal down-sample.
  qm_resolution_->UpdateCodecParameters(30.0f, 320, 240);
  EXPECT_EQ(2, qm_resolution_->GetImageType(320, 240));

  // Reset rates and go lower in rate.
  qm_resolution_->ResetRates();
  int target_rate2[] = {40, 40, 40, 40, 40};
  int encoder_sent_rate2[] = {40, 40, 40, 40, 40};
  int incoming_frame_rate2[] = {30, 30, 30, 30, 30};
  uint8_t fraction_lost2[] = {10, 10, 10, 10, 10};
  UpdateQmRateData(target_rate2, encoder_sent_rate2, incoming_frame_rate2,
                    fraction_lost2, 5);

  // Update content: motion level, and 3 spatial prediction errors.
  // Low motion, high spatial.
  UpdateQmContentData(kTemporalLow, kSpatialHigh, kSpatialHigh, kSpatialHigh);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(1, qm_resolution_->ComputeContentClass());
  EXPECT_EQ(kStableEncoding, qm_resolution_->GetEncoderState());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 1.0f, 1.0f, 1.5f, 320, 240,
                                      20.5f));

  // Reset rates and go high up in rate: expect to go back up both spatial
  // and temporally. The 1/2x1/2 spatial is undone in two stages.
  qm_resolution_->ResetRates();
  // Update rates for a sequence of intervals.
  int target_rate3[] = {1000, 1000, 1000, 1000, 1000};
  int encoder_sent_rate3[] = {1000, 1000, 1000, 1000, 1000};
  int incoming_frame_rate3[] = {20, 20, 20, 20, 20};
  uint8_t fraction_lost3[] = {10, 10, 10, 10, 10};
  UpdateQmRateData(target_rate3, encoder_sent_rate3, incoming_frame_rate3,
                   fraction_lost3, 5);

  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(1, qm_resolution_->ComputeContentClass());
  EXPECT_EQ(kStableEncoding, qm_resolution_->GetEncoderState());
  float scale = (4.0f / 3.0f) / 2.0f;
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, scale, scale, 2.0f / 3.0f,
                                      480, 360, 30.0f));

  qm_resolution_->UpdateCodecParameters(30.0f, 480, 360);
  EXPECT_EQ(4, qm_resolution_->GetImageType(480, 360));
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 3.0f / 4.0f, 3.0f / 4.0f, 1.0f,
                                      640, 480, 30.0f));
}

// No down-sampling below some total amount.
TEST_F(QmSelectTest, NoActionTooMuchDownSampling) {
  // Initialize with bitrate, frame rate, native system width/height, and
  // number of temporal layers.
  InitQmNativeData(150, 30, 1280, 720, 1);

  // Update with encoder frame size.
  uint16_t codec_width = 1280;
  uint16_t codec_height = 720;
  qm_resolution_->UpdateCodecParameters(30.0f, codec_width, codec_height);
  EXPECT_EQ(7, qm_resolution_->GetImageType(codec_width, codec_height));

  // Update rates for a sequence of intervals.
  int target_rate[] = {150, 150, 150};
  int encoder_sent_rate[] = {150, 150, 150};
  int incoming_frame_rate[] = {30, 30, 30};
  uint8_t fraction_lost[] = {10, 10, 10};
  UpdateQmRateData(target_rate, encoder_sent_rate, incoming_frame_rate,
                   fraction_lost, 3);

  // Update content: motion level, and 3 spatial prediction errors.
  // High motion, low spatial.
  UpdateQmContentData(kTemporalHigh, kSpatialLow, kSpatialLow, kSpatialLow);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(3, qm_resolution_->ComputeContentClass());
  EXPECT_EQ(kStableEncoding, qm_resolution_->GetEncoderState());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 2.0f, 2.0f, 1.0f, 640, 360,
              30.0f));

  // Reset and lower rates to get another spatial action (3/4x3/4).
  // Lower the frame rate for spatial to be selected again.
  qm_resolution_->ResetRates();
  qm_resolution_->UpdateCodecParameters(10.0f, 640, 360);
  EXPECT_EQ(4, qm_resolution_->GetImageType(640, 360));
  // Update rates for a sequence of intervals.
  int target_rate2[] = {70, 70, 70, 70, 70};
  int encoder_sent_rate2[] = {70, 70, 70, 70, 70};
  int incoming_frame_rate2[] = {10, 10, 10, 10, 10};
  uint8_t fraction_lost2[] = {10, 10, 10, 10, 10};
  UpdateQmRateData(target_rate2, encoder_sent_rate2, incoming_frame_rate2,
                   fraction_lost2, 5);

  // Update content: motion level, and 3 spatial prediction errors.
  // High motion, medium spatial.
  UpdateQmContentData(kTemporalHigh, kSpatialMedium, kSpatialMedium,
                      kSpatialMedium);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(5, qm_resolution_->ComputeContentClass());
  EXPECT_EQ(kStableEncoding, qm_resolution_->GetEncoderState());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 4.0f / 3.0f, 4.0f / 3.0f,
                                      1.0f, 480, 270, 10.0f));

  // Reset and go to very low rate: no action should be taken,
  // we went down too much already.
  qm_resolution_->ResetRates();
  qm_resolution_->UpdateCodecParameters(10.0f, 480, 270);
  EXPECT_EQ(3, qm_resolution_->GetImageType(480, 270));
  // Update rates for a sequence of intervals.
  int target_rate3[] = {10, 10, 10, 10, 10};
  int encoder_sent_rate3[] = {10, 10, 10, 10, 10};
  int incoming_frame_rate3[] = {10, 10, 10, 10, 10};
  uint8_t fraction_lost3[] = {10, 10, 10, 10, 10};
  UpdateQmRateData(target_rate3, encoder_sent_rate3, incoming_frame_rate3,
                   fraction_lost3, 5);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(5, qm_resolution_->ComputeContentClass());
  EXPECT_EQ(kStableEncoding, qm_resolution_->GetEncoderState());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 1.0f, 1.0f, 1.0f, 480, 270,
                                      10.0f));
}

// Multiple down-sampling stages and then undo all of them.
// Spatial down-sample 3/4x3/4, followed by temporal down-sample 2/3,
// followed by spatial 3/4x3/4. Then go up to full state,
// as encoding rate has increased.
TEST_F(QmSelectTest, MultipleStagesCheckActionHistory1) {
  // Initialize with bitrate, frame rate, native system width/height, and
  // number of temporal layers.
  InitQmNativeData(150, 30, 640, 480, 1);

  // Update with encoder frame size.
  uint16_t codec_width = 640;
  uint16_t codec_height = 480;
  qm_resolution_->UpdateCodecParameters(30.0f, codec_width, codec_height);
  EXPECT_EQ(5, qm_resolution_->GetImageType(codec_width, codec_height));

  // Go down spatial 3/4x3/4.
  // Update rates for a sequence of intervals.
  int target_rate[] = {150, 150, 150};
  int encoder_sent_rate[] = {150, 150, 150};
  int incoming_frame_rate[] = {30, 30, 30};
  uint8_t fraction_lost[] = {10, 10, 10};
  UpdateQmRateData(target_rate, encoder_sent_rate, incoming_frame_rate,
                   fraction_lost, 3);

  // Update content: motion level, and 3 spatial prediction errors.
  // Medium motion, low spatial.
  UpdateQmContentData(kTemporalMedium, kSpatialLow, kSpatialLow, kSpatialLow);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(6, qm_resolution_->ComputeContentClass());
  EXPECT_EQ(kStableEncoding, qm_resolution_->GetEncoderState());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 4.0f / 3.0f, 4.0f / 3.0f,
                                      1.0f, 480, 360, 30.0f));
  // Go down 2/3 temporal.
  qm_resolution_->UpdateCodecParameters(30.0f, 480, 360);
  EXPECT_EQ(4, qm_resolution_->GetImageType(480, 360));
  qm_resolution_->ResetRates();
  int target_rate2[] = {100, 100, 100, 100, 100};
  int encoder_sent_rate2[] = {100, 100, 100, 100, 100};
  int incoming_frame_rate2[] = {30, 30, 30, 30, 30};
  uint8_t fraction_lost2[] = {10, 10, 10, 10, 10};
  UpdateQmRateData(target_rate2, encoder_sent_rate2, incoming_frame_rate2,
                   fraction_lost2, 5);

  // Update content: motion level, and 3 spatial prediction errors.
  // Low motion, high spatial.
  UpdateQmContentData(kTemporalLow, kSpatialHigh, kSpatialHigh, kSpatialHigh);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(1, qm_resolution_->ComputeContentClass());
  EXPECT_EQ(kStableEncoding, qm_resolution_->GetEncoderState());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 1.0f, 1.0f, 1.5f, 480, 360,
                                      20.5f));

  // Go down 3/4x3/4 spatial:
  qm_resolution_->UpdateCodecParameters(20.0f, 480, 360);
  qm_resolution_->ResetRates();
  int target_rate3[] = {80, 80, 80, 80, 80};
  int encoder_sent_rate3[] = {80, 80, 80, 80, 80};
  int incoming_frame_rate3[] = {20, 20, 20, 20, 20};
  uint8_t fraction_lost3[] = {10, 10, 10, 10, 10};
  UpdateQmRateData(target_rate3, encoder_sent_rate3, incoming_frame_rate3,
                    fraction_lost3, 5);

  // Update content: motion level, and 3 spatial prediction errors.
  // High motion, low spatial.
  UpdateQmContentData(kTemporalHigh, kSpatialLow, kSpatialLow, kSpatialLow);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(3, qm_resolution_->ComputeContentClass());
  EXPECT_EQ(kStableEncoding, qm_resolution_->GetEncoderState());
  // The two spatial actions of 3/4x3/4 are converted to 1/2x1/2,
  // so scale factor is 2.0.
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 2.0f, 2.0f, 1.0f, 320, 240,
                                      20.0f));

  // Reset rates and go high up in rate: expect to go up:
  // 1/2x1x2 spatial and 1/2 temporally.

  // Go up 1/2x1/2 spatially and 1/2 temporally. Spatial is done in 2 stages.
  qm_resolution_->UpdateCodecParameters(15.0f, 320, 240);
  EXPECT_EQ(2, qm_resolution_->GetImageType(320, 240));
  qm_resolution_->ResetRates();
  // Update rates for a sequence of intervals.
  int target_rate4[] = {1000, 1000, 1000, 1000, 1000};
  int encoder_sent_rate4[] = {1000, 1000, 1000, 1000, 1000};
  int incoming_frame_rate4[] = {15, 15, 15, 15, 15};
  uint8_t fraction_lost4[] = {10, 10, 10, 10, 10};
  UpdateQmRateData(target_rate4, encoder_sent_rate4, incoming_frame_rate4,
                   fraction_lost4, 5);

  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(3, qm_resolution_->ComputeContentClass());
  EXPECT_EQ(kStableEncoding, qm_resolution_->GetEncoderState());
  float scale = (4.0f / 3.0f) / 2.0f;
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, scale, scale, 2.0f / 3.0f, 480,
                                      360, 30.0f));

  qm_resolution_->UpdateCodecParameters(30.0f, 480, 360);
  EXPECT_EQ(4, qm_resolution_->GetImageType(480, 360));
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 3.0f / 4.0f, 3.0f / 4.0f, 1.0f,
                                      640, 480, 30.0f));
}

// Multiple down-sampling and up-sample stages, with partial undoing.
// Spatial down-sample 1/2x1/2, followed by temporal down-sample 2/3, undo the
// temporal, then another temporal, and then undo both spatial and temporal.
TEST_F(QmSelectTest, MultipleStagesCheckActionHistory2) {
  // Initialize with bitrate, frame rate, native system width/height, and
  // number of temporal layers.
  InitQmNativeData(80, 30, 640, 480, 1);

  // Update with encoder frame size.
  uint16_t codec_width = 640;
  uint16_t codec_height = 480;
  qm_resolution_->UpdateCodecParameters(30.0f, codec_width, codec_height);
  EXPECT_EQ(5, qm_resolution_->GetImageType(codec_width, codec_height));

  // Go down 1/2x1/2 spatial.
  // Update rates for a sequence of intervals.
  int target_rate[] = {80, 80, 80};
  int encoder_sent_rate[] = {80, 80, 80};
  int incoming_frame_rate[] = {30, 30, 30};
  uint8_t fraction_lost[] = {10, 10, 10};
  UpdateQmRateData(target_rate, encoder_sent_rate, incoming_frame_rate,
                   fraction_lost, 3);

  // Update content: motion level, and 3 spatial prediction errors.
  // Medium motion, low spatial.
  UpdateQmContentData(kTemporalMedium, kSpatialLow, kSpatialLow, kSpatialLow);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(6, qm_resolution_->ComputeContentClass());
  EXPECT_EQ(kStableEncoding, qm_resolution_->GetEncoderState());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 2.0f, 2.0f, 1.0f, 320, 240,
                                      30.0f));

  // Go down 2/3 temporal.
  qm_resolution_->UpdateCodecParameters(30.0f, 320, 240);
  EXPECT_EQ(2, qm_resolution_->GetImageType(320, 240));
  qm_resolution_->ResetRates();
  int target_rate2[] = {40, 40, 40, 40, 40};
  int encoder_sent_rate2[] = {40, 40, 40, 40, 40};
  int incoming_frame_rate2[] = {30, 30, 30, 30, 30};
  uint8_t fraction_lost2[] = {10, 10, 10, 10, 10};
  UpdateQmRateData(target_rate2, encoder_sent_rate2, incoming_frame_rate2,
                   fraction_lost2, 5);

  // Update content: motion level, and 3 spatial prediction errors.
  // Medium motion, high spatial.
  UpdateQmContentData(kTemporalMedium, kSpatialHigh, kSpatialHigh,
                      kSpatialHigh);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(7, qm_resolution_->ComputeContentClass());
  EXPECT_EQ(kStableEncoding, qm_resolution_->GetEncoderState());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 1.0f, 1.0f, 1.5f, 320, 240,
                                      20.5f));

  // Go up 2/3 temporally.
  qm_resolution_->UpdateCodecParameters(20.0f, 320, 240);
  qm_resolution_->ResetRates();
  // Update rates for a sequence of intervals.
  int target_rate3[] = {150, 150, 150, 150, 150};
  int encoder_sent_rate3[] = {150, 150, 150, 150, 150};
  int incoming_frame_rate3[] = {20, 20, 20, 20, 20};
  uint8_t fraction_lost3[] = {10, 10, 10, 10, 10};
  UpdateQmRateData(target_rate3, encoder_sent_rate3, incoming_frame_rate3,
                   fraction_lost3, 5);

  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(7, qm_resolution_->ComputeContentClass());
  EXPECT_EQ(kStableEncoding, qm_resolution_->GetEncoderState());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 1.0f, 1.0f, 2.0f / 3.0f, 320,
                                      240, 30.0f));

  // Go down 2/3 temporal.
  qm_resolution_->UpdateCodecParameters(30.0f, 320, 240);
  EXPECT_EQ(2, qm_resolution_->GetImageType(320, 240));
  qm_resolution_->ResetRates();
  int target_rate4[] = {40, 40, 40, 40, 40};
  int encoder_sent_rate4[] = {40, 40, 40, 40, 40};
  int incoming_frame_rate4[] = {30, 30, 30, 30, 30};
  uint8_t fraction_lost4[] = {10, 10, 10, 10, 10};
  UpdateQmRateData(target_rate4, encoder_sent_rate4, incoming_frame_rate4,
                   fraction_lost4, 5);

  // Update content: motion level, and 3 spatial prediction errors.
  // Low motion, high spatial.
  UpdateQmContentData(kTemporalLow, kSpatialHigh, kSpatialHigh, kSpatialHigh);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(1, qm_resolution_->ComputeContentClass());
  EXPECT_EQ(kStableEncoding, qm_resolution_->GetEncoderState());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 1.0f, 1.0f, 1.5f, 320, 240,
                                      20.5f));

  // Go up spatial and temporal. Spatial undoing is done in 2 stages.
  qm_resolution_->UpdateCodecParameters(20.5f, 320, 240);
  qm_resolution_->ResetRates();
  // Update rates for a sequence of intervals.
  int target_rate5[] = {1000, 1000, 1000, 1000, 1000};
  int encoder_sent_rate5[] = {1000, 1000, 1000, 1000, 1000};
  int incoming_frame_rate5[] = {20, 20, 20, 20, 20};
  uint8_t fraction_lost5[] = {10, 10, 10, 10, 10};
  UpdateQmRateData(target_rate5, encoder_sent_rate5, incoming_frame_rate5,
                   fraction_lost5, 5);

  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  float scale = (4.0f / 3.0f) / 2.0f;
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, scale, scale, 2.0f / 3.0f,
                                      480, 360, 30.0f));

  qm_resolution_->UpdateCodecParameters(30.0f, 480, 360);
  EXPECT_EQ(4, qm_resolution_->GetImageType(480, 360));
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 3.0f / 4.0f, 3.0f / 4.0f, 1.0f,
                                      640, 480, 30.0f));
}

// Multiple down-sampling and up-sample stages, with partial undoing.
// Spatial down-sample 3/4x3/4, followed by temporal down-sample 2/3,
// undo the temporal 2/3, and then undo the spatial.
TEST_F(QmSelectTest, MultipleStagesCheckActionHistory3) {
  // Initialize with bitrate, frame rate, native system width/height, and
  // number of temporal layers.
  InitQmNativeData(100, 30, 640, 480, 1);

  // Update with encoder frame size.
  uint16_t codec_width = 640;
  uint16_t codec_height = 480;
  qm_resolution_->UpdateCodecParameters(30.0f, codec_width, codec_height);
  EXPECT_EQ(5, qm_resolution_->GetImageType(codec_width, codec_height));

  // Go down 3/4x3/4 spatial.
  // Update rates for a sequence of intervals.
  int target_rate[] = {100, 100, 100};
  int encoder_sent_rate[] = {100, 100, 100};
  int incoming_frame_rate[] = {30, 30, 30};
  uint8_t fraction_lost[] = {10, 10, 10};
  UpdateQmRateData(target_rate, encoder_sent_rate, incoming_frame_rate,
                   fraction_lost, 3);

  // Update content: motion level, and 3 spatial prediction errors.
  // Medium motion, low spatial.
  UpdateQmContentData(kTemporalMedium, kSpatialLow, kSpatialLow, kSpatialLow);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(6, qm_resolution_->ComputeContentClass());
  EXPECT_EQ(kStableEncoding, qm_resolution_->GetEncoderState());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 4.0f / 3.0f, 4.0f / 3.0f,
                                      1.0f, 480, 360, 30.0f));

  // Go down 2/3 temporal.
  qm_resolution_->UpdateCodecParameters(30.0f, 480, 360);
  EXPECT_EQ(4, qm_resolution_->GetImageType(480, 360));
  qm_resolution_->ResetRates();
  int target_rate2[] = {100, 100, 100, 100, 100};
  int encoder_sent_rate2[] = {100, 100, 100, 100, 100};
  int incoming_frame_rate2[] = {30, 30, 30, 30, 30};
  uint8_t fraction_lost2[] = {10, 10, 10, 10, 10};
  UpdateQmRateData(target_rate2, encoder_sent_rate2, incoming_frame_rate2,
                   fraction_lost2, 5);

  // Update content: motion level, and 3 spatial prediction errors.
  // Low motion, high spatial.
  UpdateQmContentData(kTemporalLow, kSpatialHigh, kSpatialHigh, kSpatialHigh);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(1, qm_resolution_->ComputeContentClass());
  EXPECT_EQ(kStableEncoding, qm_resolution_->GetEncoderState());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 1.0f, 1.0f, 1.5f, 480, 360,
                                      20.5f));

  // Go up 2/3 temporal.
  qm_resolution_->UpdateCodecParameters(20.5f, 480, 360);
  qm_resolution_->ResetRates();
  // Update rates for a sequence of intervals.
  int target_rate3[] = {250, 250, 250, 250, 250};
  int encoder_sent_rate3[] = {250, 250, 250, 250, 250};
  int incoming_frame_rate3[] = {20, 20, 20, 20, 120};
  uint8_t fraction_lost3[] = {10, 10, 10, 10, 10};
  UpdateQmRateData(target_rate3, encoder_sent_rate3, incoming_frame_rate3,
                   fraction_lost3, 5);

  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(1, qm_resolution_->ComputeContentClass());
  EXPECT_EQ(kStableEncoding, qm_resolution_->GetEncoderState());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 1.0f, 1.0f, 2.0f / 3.0f, 480,
                                      360, 30.0f));

  // Go up spatial.
  qm_resolution_->UpdateCodecParameters(30.0f, 480, 360);
  EXPECT_EQ(4, qm_resolution_->GetImageType(480, 360));
  qm_resolution_->ResetRates();
  int target_rate4[] = {500, 500, 500, 500, 500};
  int encoder_sent_rate4[] = {500, 500, 500, 500, 500};
  int incoming_frame_rate4[] = {30, 30, 30, 30, 30};
  uint8_t fraction_lost4[] = {30, 30, 30, 30, 30};
  UpdateQmRateData(target_rate4, encoder_sent_rate4, incoming_frame_rate4,
                   fraction_lost4, 5);

  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(kStableEncoding, qm_resolution_->GetEncoderState());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 3.0f / 4.0f, 3.0f / 4.0f,
                                      1.0f, 640, 480, 30.0f));
}

// Two stages of 3/4x3/4 converted to one stage of 1/2x1/2.
TEST_F(QmSelectTest, ConvertThreeQuartersToOneHalf) {
  // Initialize with bitrate, frame rate, native system width/height, and
  // number of temporal layers.
  InitQmNativeData(150, 30, 640, 480, 1);

  // Update with encoder frame size.
  uint16_t codec_width = 640;
  uint16_t codec_height = 480;
  qm_resolution_->UpdateCodecParameters(30.0f, codec_width, codec_height);
  EXPECT_EQ(5, qm_resolution_->GetImageType(codec_width, codec_height));

  // Go down 3/4x3/4 spatial.
  // Update rates for a sequence of intervals.
  int target_rate[] = {150, 150, 150};
  int encoder_sent_rate[] = {150, 150, 150};
  int incoming_frame_rate[] = {30, 30, 30};
  uint8_t fraction_lost[] = {10, 10, 10};
  UpdateQmRateData(target_rate, encoder_sent_rate, incoming_frame_rate,
                   fraction_lost, 3);

  // Update content: motion level, and 3 spatial prediction errors.
  // Medium motion, low spatial.
  UpdateQmContentData(kTemporalMedium, kSpatialLow, kSpatialLow, kSpatialLow);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(6, qm_resolution_->ComputeContentClass());
  EXPECT_EQ(kStableEncoding, qm_resolution_->GetEncoderState());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 4.0f / 3.0f, 4.0f / 3.0f,
                                      1.0f, 480, 360, 30.0f));

  // Set rates to go down another 3/4 spatial. Should be converted ton 1/2.
  qm_resolution_->UpdateCodecParameters(30.0f, 480, 360);
  EXPECT_EQ(4, qm_resolution_->GetImageType(480, 360));
  qm_resolution_->ResetRates();
  int target_rate2[] = {100, 100, 100, 100, 100};
  int encoder_sent_rate2[] = {100, 100, 100, 100, 100};
  int incoming_frame_rate2[] = {30, 30, 30, 30, 30};
  uint8_t fraction_lost2[] = {10, 10, 10, 10, 10};
  UpdateQmRateData(target_rate2, encoder_sent_rate2, incoming_frame_rate2,
                   fraction_lost2, 5);

  // Update content: motion level, and 3 spatial prediction errors.
  // Medium motion, low spatial.
  UpdateQmContentData(kTemporalMedium, kSpatialLow, kSpatialLow, kSpatialLow);
  EXPECT_EQ(0, qm_resolution_->SelectResolution(&qm_scale_));
  EXPECT_EQ(6, qm_resolution_->ComputeContentClass());
  EXPECT_EQ(kStableEncoding, qm_resolution_->GetEncoderState());
  EXPECT_TRUE(IsSelectedActionCorrect(qm_scale_, 2.0f, 2.0f, 1.0f, 320, 240,
                                      30.0f));
}

void QmSelectTest::InitQmNativeData(float initial_bit_rate,
                                    int user_frame_rate,
                                    int native_width,
                                    int native_height,
                                    int num_layers) {
  EXPECT_EQ(0, qm_resolution_->Initialize(initial_bit_rate,
                                          user_frame_rate,
                                          native_width,
                                          native_height,
                                          num_layers));
}

void QmSelectTest::UpdateQmContentData(float motion_metric,
                                       float spatial_metric,
                                       float spatial_metric_horiz,
                                       float spatial_metric_vert) {
  content_metrics_->motion_magnitude = motion_metric;
  content_metrics_->spatial_pred_err = spatial_metric;
  content_metrics_->spatial_pred_err_h = spatial_metric_horiz;
  content_metrics_->spatial_pred_err_v = spatial_metric_vert;
  qm_resolution_->UpdateContent(content_metrics_);
}

void QmSelectTest::UpdateQmEncodedFrame(size_t* encoded_size,
                                        size_t num_updates) {
  for (size_t i = 0; i < num_updates; ++i) {
    // Convert to bytes.
    size_t encoded_size_update = 1000 * encoded_size[i] / 8;
    qm_resolution_->UpdateEncodedSize(encoded_size_update);
  }
}

void QmSelectTest::UpdateQmRateData(int* target_rate,
                                    int* encoder_sent_rate,
                                    int* incoming_frame_rate,
                                    uint8_t* fraction_lost,
                                    int num_updates) {
  for (int i = 0; i < num_updates; ++i) {
    float target_rate_update = target_rate[i];
    float encoder_sent_rate_update = encoder_sent_rate[i];
    float incoming_frame_rate_update = incoming_frame_rate[i];
    uint8_t fraction_lost_update = fraction_lost[i];
    qm_resolution_->UpdateRates(target_rate_update,
                                encoder_sent_rate_update,
                                incoming_frame_rate_update,
                                fraction_lost_update);
  }
}

// Check is the selected action from the QmResolution class is the same
// as the expected scales from |fac_width|, |fac_height|, |fac_temp|.
bool QmSelectTest::IsSelectedActionCorrect(VCMResolutionScale* qm_scale,
                                           float fac_width,
                                           float fac_height,
                                           float fac_temp,
                                           uint16_t new_width,
                                           uint16_t new_height,
                                           float new_frame_rate) {
  if (qm_scale->spatial_width_fact == fac_width &&
      qm_scale->spatial_height_fact == fac_height &&
      qm_scale->temporal_fact == fac_temp &&
      qm_scale->codec_width == new_width &&
      qm_scale->codec_height == new_height &&
      qm_scale->frame_rate == new_frame_rate) {
    return true;
  } else {
    return false;
  }
}
}  // namespace webrtc