1 /*
2  *  Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "common_video/h264/h264_bitstream_parser.h"
12 
13 #include "test/gtest.h"
14 
15 namespace webrtc {
16 
17 // SPS/PPS part of below chunk.
18 uint8_t kH264SpsPps[] = {0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x80, 0x20, 0xda,
19                          0x01, 0x40, 0x16, 0xe8, 0x06, 0xd0, 0xa1, 0x35, 0x00,
20                          0x00, 0x00, 0x01, 0x68, 0xce, 0x06, 0xe2};
21 
22 // Contains enough of the image slice to contain slice QP.
23 uint8_t kH264BitstreamChunk[] = {
24     0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x80, 0x20, 0xda, 0x01, 0x40, 0x16,
25     0xe8, 0x06, 0xd0, 0xa1, 0x35, 0x00, 0x00, 0x00, 0x01, 0x68, 0xce, 0x06,
26     0xe2, 0x00, 0x00, 0x00, 0x01, 0x65, 0xb8, 0x40, 0xf0, 0x8c, 0x03, 0xf2,
27     0x75, 0x67, 0xad, 0x41, 0x64, 0x24, 0x0e, 0xa0, 0xb2, 0x12, 0x1e, 0xf8,
28 };
29 
30 uint8_t kH264BitstreamChunkCabac[] = {
31   0x00, 0x00, 0x00, 0x01, 0x27, 0x64, 0x00, 0x0d, 0xac, 0x52, 0x30, 0x50,
32   0x7e, 0xc0, 0x5a, 0x81, 0x01, 0x01, 0x18, 0x56, 0xbd, 0xef, 0x80, 0x80,
33   0x00, 0x00, 0x00, 0x01, 0x28, 0xfe, 0x09, 0x8b,
34 };
35 
36 // Contains enough of the image slice to contain slice QP.
37 uint8_t kH264BitstreamNextImageSliceChunk[] = {
38     0x00, 0x00, 0x00, 0x01, 0x41, 0xe2, 0x01, 0x16, 0x0e, 0x3e, 0x2b, 0x86,
39 };
40 
41 // Contains enough of the image slice to contain slice QP.
42 uint8_t kH264BitstreamNextImageSliceChunkCabac[] = {
43   0x00, 0x00, 0x00, 0x01, 0x21, 0xe1, 0x05, 0x11, 0x3f, 0x9a, 0xae, 0x46,
44   0x70, 0xbf, 0xc1, 0x4a, 0x16, 0x8f, 0x51, 0xf4, 0xca, 0xfb, 0xa3, 0x65,
45 };
46 
TEST(H264BitstreamParserTest,ReportsNoQpWithoutParsedSlices)47 TEST(H264BitstreamParserTest, ReportsNoQpWithoutParsedSlices) {
48   H264BitstreamParser h264_parser;
49   int qp;
50   EXPECT_FALSE(h264_parser.GetLastSliceQp(&qp));
51 }
52 
TEST(H264BitstreamParserTest,ReportsNoQpWithOnlyParsedPpsAndSpsSlices)53 TEST(H264BitstreamParserTest, ReportsNoQpWithOnlyParsedPpsAndSpsSlices) {
54   H264BitstreamParser h264_parser;
55   h264_parser.ParseBitstream(kH264SpsPps, sizeof(kH264SpsPps));
56   int qp;
57   EXPECT_FALSE(h264_parser.GetLastSliceQp(&qp));
58 }
59 
TEST(H264BitstreamParserTest,ReportsLastSliceQpForImageSlices)60 TEST(H264BitstreamParserTest, ReportsLastSliceQpForImageSlices) {
61   H264BitstreamParser h264_parser;
62   h264_parser.ParseBitstream(kH264BitstreamChunk, sizeof(kH264BitstreamChunk));
63   int qp;
64   ASSERT_TRUE(h264_parser.GetLastSliceQp(&qp));
65   EXPECT_EQ(35, qp);
66 
67   // Parse an additional image slice.
68   h264_parser.ParseBitstream(kH264BitstreamNextImageSliceChunk,
69                              sizeof(kH264BitstreamNextImageSliceChunk));
70   ASSERT_TRUE(h264_parser.GetLastSliceQp(&qp));
71   EXPECT_EQ(37, qp);
72 }
73 
TEST(H264BitstreamParserTest,ReportsLastSliceQpForCABACImageSlices)74 TEST(H264BitstreamParserTest, ReportsLastSliceQpForCABACImageSlices) {
75   H264BitstreamParser h264_parser;
76   h264_parser.ParseBitstream(kH264BitstreamChunkCabac,
77                              sizeof(kH264BitstreamChunkCabac));
78   int qp;
79   EXPECT_FALSE(h264_parser.GetLastSliceQp(&qp));
80 
81   // Parse an additional image slice.
82   h264_parser.ParseBitstream(kH264BitstreamNextImageSliceChunkCabac,
83                              sizeof(kH264BitstreamNextImageSliceChunkCabac));
84   ASSERT_TRUE(h264_parser.GetLastSliceQp(&qp));
85   EXPECT_EQ(24, qp);
86 }
87 
88 }  // namespace webrtc
89