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

media / parsers / h264_poc_unittest.cc [blame]

// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include <stdint.h>

#include "base/files/file_path.h"
#include "base/files/memory_mapped_file.h"
#include "media/base/test_data_util.h"
#include "media/parsers/h264_parser.h"
#include "media/parsers/h264_poc.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace media {

class H264POCTest : public testing::Test {
 public:
  H264POCTest() : sps_(), slice_hdr_() {
    // Default every frame to be a reference frame.
    slice_hdr_.nal_ref_idc = 1;
  }

  H264POCTest(const H264POCTest&) = delete;
  H264POCTest& operator=(const H264POCTest&) = delete;

 protected:
  void ComputePOC() {
    poc_ = h264_poc_.ComputePicOrderCnt(&sps_, slice_hdr_);

    // Clear MMCO5.
    slice_hdr_.adaptive_ref_pic_marking_mode_flag = false;
    slice_hdr_.ref_pic_marking[0].memory_mgmnt_control_operation = 0;
    slice_hdr_.ref_pic_marking[1].memory_mgmnt_control_operation = 0;
    slice_hdr_.ref_pic_marking[2].memory_mgmnt_control_operation = 0;
  }

  // Also sets as a reference frame and unsets IDR, which is required for
  // memory management control operations to be parsed.
  void SetMMCO5() {
    slice_hdr_.nal_ref_idc = 1;
    slice_hdr_.idr_pic_flag = false;
    slice_hdr_.adaptive_ref_pic_marking_mode_flag = true;
    slice_hdr_.ref_pic_marking[0].memory_mgmnt_control_operation = 6;
    slice_hdr_.ref_pic_marking[1].memory_mgmnt_control_operation = 5;
    slice_hdr_.ref_pic_marking[2].memory_mgmnt_control_operation = 0;
  }

  std::optional<int32_t> poc_;

  H264SPS sps_;
  H264SliceHeader slice_hdr_;
  H264POC h264_poc_;
};

TEST_F(H264POCTest, PicOrderCntType0) {
  sps_.pic_order_cnt_type = 0;
  sps_.log2_max_pic_order_cnt_lsb_minus4 = 0;  // 16

  // Initial IDR with POC 0.
  slice_hdr_.idr_pic_flag = true;
  slice_hdr_.frame_num = 0;
  ComputePOC();
  ASSERT_TRUE(poc_.has_value());
  ASSERT_EQ(0, *poc_);

  // Ref frame with POC lsb 8.
  slice_hdr_.idr_pic_flag = false;
  slice_hdr_.frame_num = 1;
  slice_hdr_.pic_order_cnt_lsb = 8;
  ComputePOC();
  ASSERT_TRUE(poc_.has_value());
  ASSERT_EQ(8, *poc_);

  // Ref frame with POC lsb 0. This should be detected as wrapping, as the
  // (negative) gap is at least half the maximum.
  slice_hdr_.pic_order_cnt_lsb = 0;
  slice_hdr_.frame_num = 2;
  ComputePOC();
  ASSERT_TRUE(poc_.has_value());
  ASSERT_EQ(16, *poc_);

  // Ref frame with POC lsb 9. This should be detected as negative wrapping,
  // as the (positive) gap is more than half the maximum.
  slice_hdr_.pic_order_cnt_lsb = 9;
  slice_hdr_.frame_num = 3;
  ComputePOC();
  ASSERT_TRUE(poc_.has_value());
  ASSERT_EQ(9, *poc_);
}

TEST_F(H264POCTest, PicOrderCntType0_WithMMCO5) {
  sps_.pic_order_cnt_type = 0;
  sps_.log2_max_pic_order_cnt_lsb_minus4 = 0;  // 16

  // Initial IDR with POC 0.
  slice_hdr_.idr_pic_flag = true;
  slice_hdr_.frame_num = 0;
  ComputePOC();
  ASSERT_TRUE(poc_.has_value());
  ASSERT_EQ(0, *poc_);

  // Skip ahead.
  slice_hdr_.idr_pic_flag = false;
  slice_hdr_.frame_num = 1;
  slice_hdr_.pic_order_cnt_lsb = 8;
  ComputePOC();
  ASSERT_TRUE(poc_.has_value());
  ASSERT_EQ(8, *poc_);

  slice_hdr_.frame_num = 2;
  slice_hdr_.pic_order_cnt_lsb = 0;
  ComputePOC();
  ASSERT_TRUE(poc_.has_value());
  ASSERT_EQ(16, *poc_);

  slice_hdr_.frame_num = 3;
  slice_hdr_.pic_order_cnt_lsb = 8;
  ComputePOC();
  ASSERT_TRUE(poc_.has_value());
  ASSERT_EQ(24, *poc_);

  // MMCO5 resets to 0.
  slice_hdr_.frame_num = 4;
  slice_hdr_.pic_order_cnt_lsb = 0;
  SetMMCO5();
  ComputePOC();
  ASSERT_TRUE(poc_.has_value());
  ASSERT_EQ(0, *poc_);

  // Still detected as positive wrapping.
  slice_hdr_.frame_num = 5;
  slice_hdr_.pic_order_cnt_lsb = 8;
  ComputePOC();
  ASSERT_TRUE(poc_.has_value());
  ASSERT_EQ(24, *poc_);
}

TEST_F(H264POCTest, PicOrderCntType1) {
  sps_.pic_order_cnt_type = 1;
  sps_.log2_max_frame_num_minus4 = 0;  // 16
  sps_.num_ref_frames_in_pic_order_cnt_cycle = 2;
  sps_.expected_delta_per_pic_order_cnt_cycle = 3;
  sps_.offset_for_ref_frame[0] = 1;
  sps_.offset_for_ref_frame[1] = 2;

  // Initial IDR with POC 0.
  slice_hdr_.idr_pic_flag = true;
  slice_hdr_.frame_num = 0;
  ComputePOC();
  ASSERT_TRUE(poc_.has_value());
  ASSERT_EQ(0, *poc_);

  // Ref frame.
  slice_hdr_.idr_pic_flag = false;
  slice_hdr_.frame_num = 1;
  ComputePOC();
  ASSERT_TRUE(poc_.has_value());
  ASSERT_EQ(1, *poc_);

  // Ref frame.
  slice_hdr_.frame_num = 2;
  ComputePOC();
  ASSERT_TRUE(poc_.has_value());
  ASSERT_EQ(3, *poc_);

  // Ref frame.
  slice_hdr_.frame_num = 3;
  ComputePOC();
  ASSERT_TRUE(poc_.has_value());
  ASSERT_EQ(4, *poc_);

  // Ref frame.
  slice_hdr_.frame_num = 4;
  ComputePOC();
  ASSERT_TRUE(poc_.has_value());
  ASSERT_EQ(6, *poc_);

  // Ref frame, detected as wrapping (ie, this is frame 16).
  slice_hdr_.frame_num = 0;
  ComputePOC();
  ASSERT_TRUE(poc_.has_value());
  ASSERT_EQ(24, *poc_);
}

TEST_F(H264POCTest, PicOrderCntType1_WithMMCO5) {
  sps_.pic_order_cnt_type = 1;
  sps_.log2_max_frame_num_minus4 = 0;  // 16
  sps_.num_ref_frames_in_pic_order_cnt_cycle = 2;
  sps_.expected_delta_per_pic_order_cnt_cycle = 3;
  sps_.offset_for_ref_frame[0] = 1;
  sps_.offset_for_ref_frame[1] = 2;

  // Initial IDR with POC 0.
  slice_hdr_.idr_pic_flag = true;
  slice_hdr_.frame_num = 0;
  ComputePOC();
  ASSERT_TRUE(poc_.has_value());
  ASSERT_EQ(0, *poc_);

  // Ref frame.
  slice_hdr_.idr_pic_flag = false;
  slice_hdr_.frame_num = 1;
  ComputePOC();
  ASSERT_TRUE(poc_.has_value());
  ASSERT_EQ(1, *poc_);

  // Ref frame, detected as wrapping.
  SetMMCO5();
  slice_hdr_.frame_num = 0;
  ComputePOC();
  ASSERT_TRUE(poc_.has_value());
  ASSERT_EQ(0, *poc_);

  // Ref frame, wrapping from before has been cleared.
  slice_hdr_.frame_num = 1;
  ComputePOC();
  ASSERT_TRUE(poc_.has_value());
  ASSERT_EQ(1, *poc_);
}

// |frame_num| values may be duplicated by non-reference frames.
TEST_F(H264POCTest, PicOrderCntType1_DupFrameNum) {
  sps_.pic_order_cnt_type = 1;
  sps_.log2_max_frame_num_minus4 = 0;  // 16
  sps_.num_ref_frames_in_pic_order_cnt_cycle = 2;
  sps_.expected_delta_per_pic_order_cnt_cycle = 3;
  sps_.offset_for_ref_frame[0] = 1;
  sps_.offset_for_ref_frame[1] = 2;

  // Initial IDR with POC 0.
  slice_hdr_.idr_pic_flag = true;
  slice_hdr_.frame_num = 0;
  ComputePOC();
  ASSERT_TRUE(poc_.has_value());
  ASSERT_EQ(0, *poc_);

  // Ref frame.
  slice_hdr_.idr_pic_flag = false;
  slice_hdr_.frame_num = 1;
  ComputePOC();
  ASSERT_TRUE(poc_.has_value());
  ASSERT_EQ(1, *poc_);

  // Duplicate |frame_num| frame.
  slice_hdr_.nal_ref_idc = 0;
  slice_hdr_.frame_num = 1;
  slice_hdr_.delta_pic_order_cnt0 = 2;
  ComputePOC();
  ASSERT_TRUE(poc_.has_value());
  ASSERT_EQ(2, *poc_);
}

TEST_F(H264POCTest, PicOrderCntType2) {
  sps_.pic_order_cnt_type = 2;

  // Initial IDR with POC 0.
  slice_hdr_.idr_pic_flag = true;
  slice_hdr_.frame_num = 0;
  ComputePOC();
  ASSERT_TRUE(poc_.has_value());
  ASSERT_EQ(0, *poc_);

  // Ref frame.
  slice_hdr_.idr_pic_flag = false;
  slice_hdr_.frame_num = 1;
  ComputePOC();
  ASSERT_TRUE(poc_.has_value());
  ASSERT_EQ(2, *poc_);

  // Ref frame.
  slice_hdr_.frame_num = 2;
  ComputePOC();
  ASSERT_TRUE(poc_.has_value());
  ASSERT_EQ(4, *poc_);

  // Ref frame.
  slice_hdr_.frame_num = 3;
  ComputePOC();
  ASSERT_TRUE(poc_.has_value());
  ASSERT_EQ(6, *poc_);

  // Ref frame.
  slice_hdr_.frame_num = 4;
  ComputePOC();
  ASSERT_TRUE(poc_.has_value());
  ASSERT_EQ(8, *poc_);

  // Ref frame, detected as wrapping (ie, this is frame 16).
  slice_hdr_.frame_num = 0;
  ComputePOC();
  ASSERT_TRUE(poc_.has_value());
  ASSERT_EQ(32, *poc_);
}

TEST_F(H264POCTest, PicOrderCntType2_WithMMCO5) {
  sps_.pic_order_cnt_type = 2;

  // Initial IDR with POC 0.
  slice_hdr_.idr_pic_flag = true;
  slice_hdr_.frame_num = 0;
  ComputePOC();
  ASSERT_TRUE(poc_.has_value());
  ASSERT_EQ(0, *poc_);

  // Ref frame.
  slice_hdr_.idr_pic_flag = false;
  slice_hdr_.frame_num = 1;
  ComputePOC();
  ASSERT_TRUE(poc_.has_value());
  ASSERT_EQ(2, *poc_);

  // Ref frame, detected as wrapping.
  SetMMCO5();
  slice_hdr_.frame_num = 0;
  ComputePOC();
  ASSERT_TRUE(poc_.has_value());
  ASSERT_EQ(0, *poc_);

  // Ref frame, wrapping from before has been cleared.
  slice_hdr_.frame_num = 1;
  ComputePOC();
  ASSERT_TRUE(poc_.has_value());
  ASSERT_EQ(2, *poc_);
}

}  // namespace media