File: mpeg2_parser_unittest.cpp

package info (click to toggle)
libyami 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,152 kB
  • sloc: cpp: 44,247; ansic: 1,255; makefile: 728; lisp: 479; sh: 21; python: 19
file content (218 lines) | stat: -rw-r--r-- 7,717 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright 2016 Intel Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

// primary header
#include "mpeg2_parser.h"

// library headers
#include "common/Array.h"
#include "common/log.h"
#include "common/unittest.h"

// system headers
#include <limits>
#include <time.h> //using rand

namespace YamiParser {
namespace MPEG2 {

    const static std::array<const uint8_t, 9> SequenceHeader
        = { 0xb3, 0x20, 0x01, 0x20, 0x34, 0xff, 0xff, 0xe0, 0x18 };

    const static std::array<const uint8_t, 7> SequenceExtension
        = { 0xb5, 0x14, 0x8a, 0x00, 0x01, 0x00, 0x00 };

    const static std::array<const uint8_t, 5> GroupOfPicturesHeader
        = { 0xb8, 0x00, 0x08, 0x06, 0x00 };

    const static std::array<const uint8_t, 5> PictureHeaderArray
        = { 0x00, 0x00, 0x0f, 0xff, 0xf8 };

    const static std::array<const uint8_t, 6> PictureCodingExtensionArray
        = { 0xb5, 0x8f, 0xff, 0xf3, 0x41, 0x80 };

    const static std::array<const uint8_t, 124> SliceArray
        = { 0x01, 0x13, 0xf8, 0x7d, 0x29, 0x48, 0x8b, 0x94, 0xa5, 0x22, 0x2e,
            0x52, 0x94, 0x88, 0xb9, 0x4a, 0x52, 0x22, 0xe5, 0x29, 0x48, 0x8b,
            0x94, 0xa5, 0x22, 0x2e, 0x52, 0x94, 0x88, 0xb9, 0x4a, 0x52, 0x22,
            0xe5, 0x29, 0x48, 0x8b, 0x94, 0xa5, 0x22, 0x2e, 0x52, 0x94, 0x88,
            0xb9, 0x4a, 0x52, 0x22, 0xe5, 0x29, 0x48, 0x8b, 0x94, 0xa5, 0x22,
            0x2e, 0x52, 0x94, 0x88, 0xb9, 0x4a, 0x52, 0x22, 0xe5, 0x29, 0x48,
            0x8b, 0x94, 0xa5, 0x22, 0x2e, 0x52, 0x94, 0x88, 0xb9, 0x4a, 0x52,
            0x22, 0xe5, 0x29, 0x48, 0x8b, 0x94, 0xa5, 0x22, 0x2e, 0x52, 0x94,
            0x88, 0xb9, 0x4a, 0x52, 0x22, 0xe5, 0x29, 0x48, 0x8b, 0x94, 0xa5,
            0x22, 0x2e, 0x52, 0x94, 0x88, 0xb9, 0x4a, 0x52, 0x22, 0xe5, 0x29,
            0x48, 0x8b, 0x94, 0xa5, 0x22, 0x2e, 0x52, 0x94, 0x88, 0xb9, 0x4a,
            0x52, 0x22, 0x00 };

    class MPEG2ParserTest : public ::testing::Test {
    protected:

#define MEMBER_VARIABLE(result_type, var)                                      \
    result_type& var(Parser& p) { return p.var; }

        MEMBER_VARIABLE(SeqHeader, m_sequenceHdr);
        MEMBER_VARIABLE(SeqExtension, m_sequenceExtension);
        MEMBER_VARIABLE(GOPHeader, m_GOPHeader);
        MEMBER_VARIABLE(PictureHeader, m_pictureHeader);
        MEMBER_VARIABLE(PictureCodingExtension, m_pictureCodingExtension);

#undef MEMBER_VARIABLE

        void checkParamsSeqHeader(const Parser& parser)
        {
            EXPECT_EQ(0x200u, parser.m_sequenceHdr.horizontal_size_value);
            EXPECT_EQ(0x120u, parser.m_sequenceHdr.vertical_size_value);
            EXPECT_EQ(3u, parser.m_sequenceHdr.aspect_ratio_info);
            EXPECT_EQ(4u, parser.m_sequenceHdr.frame_rate_code);
            EXPECT_EQ(0x3ffffu, parser.m_sequenceHdr.bit_rate_value);
            EXPECT_EQ(3u, parser.m_sequenceHdr.vbv_buffer_size_value);
        }

        void checkParamsSeqExtension(const Parser& parser)
        {
            EXPECT_EQ(0x48u,
                      parser.m_sequenceExtension.profile_and_level_indication);
            EXPECT_EQ(1, parser.m_sequenceExtension.progressive_sequence);
            EXPECT_EQ(1u, parser.m_sequenceExtension.chroma_format);
        }

        void checkParamsPictureHeader(const Parser& parser)
        {
            EXPECT_EQ(1u, parser.m_pictureHeader.picture_coding_type);
            EXPECT_EQ(0xffffu, parser.m_pictureHeader.vbv_delay);
        }

        void checkParamsPictureCodingExtension(const Parser& parser)
        {
            EXPECT_EQ(0xfu, parser.m_pictureCodingExtension.f_code[0][0]);
            EXPECT_EQ(0xfu, parser.m_pictureCodingExtension.f_code[0][1]);
            EXPECT_EQ(0xfu, parser.m_pictureCodingExtension.f_code[1][0]);
            EXPECT_EQ(0xfu, parser.m_pictureCodingExtension.f_code[1][1]);
            EXPECT_EQ(0u, parser.m_pictureCodingExtension.intra_dc_precision);
            EXPECT_EQ(3u, parser.m_pictureCodingExtension.picture_structure);
            EXPECT_EQ(1, parser.m_pictureCodingExtension.frame_pred_frame_dct);
            EXPECT_EQ(1, parser.m_pictureCodingExtension.chrome_420_type);
            EXPECT_EQ(1, parser.m_pictureCodingExtension.progressive_frame);
        }

        void checkParamsSlice(const Parser& parser, Slice& slice)
        {
            EXPECT_EQ(6u, slice.sliceHeaderSize);
            EXPECT_EQ(0u, slice.macroblockRow);
            EXPECT_EQ(0u, slice.macroblockColumn);
        }
    };

    bool createDu(DecodeUnit& du, const uint8_t* data, size_t size, StartCodeType expected)
    {
        if (!du.parse(data, size))
            return false;
        return du.m_type == expected;
    }

    bool checkExtension(BitReader& br, ExtensionIdentifierType expected)
    {
        uint32_t id;
        if (!br.read(id, 4))
            return false;
        return id == expected;
    }

#define MPEG2_PARSER_TEST(name) TEST_F(MPEG2ParserTest, name)

    MPEG2_PARSER_TEST(ParseSequenceHeader)
    {
        Parser parser;

        DecodeUnit du;
        EXPECT_TRUE(createDu(du, &SequenceHeader[0], SequenceHeader.size(), MPEG2_SEQUENCE_HEADER_CODE));

        SharedPtr<QuantMatrices> m;

        EXPECT_TRUE(parser.parseSequenceHeader(du, m));
        EXPECT_TRUE(m);
        checkParamsSeqHeader(parser);
    }

    MPEG2_PARSER_TEST(ParseSequenceExtension)
    {
        DecodeUnit du;
        EXPECT_TRUE(createDu(du, &SequenceExtension[0], SequenceExtension.size(), MPEG2_EXTENSION_START_CODE));

        BitReader br(du.m_data, du.m_size);
        EXPECT_TRUE(checkExtension(br, kSequence));

        Parser parser;

        EXPECT_TRUE(parser.parseSequenceExtension(br));
        checkParamsSeqExtension(parser);
    }

    MPEG2_PARSER_TEST(ParseGOPHeader)
    {

        DecodeUnit du;
        EXPECT_TRUE(createDu(du, &GroupOfPicturesHeader[0], GroupOfPicturesHeader.size(), MPEG2_GROUP_START_CODE));

        Parser parser;

        EXPECT_TRUE(parser.parseGOPHeader(du));
        //all params will be zero for this unittest
    }

    MPEG2_PARSER_TEST(ParsePictureHeader)
    {
        DecodeUnit du;
        EXPECT_TRUE(createDu(du, &PictureHeaderArray[0], PictureHeaderArray.size(), MPEG2_PICTURE_START_CODE));

        Parser parser;
        EXPECT_TRUE(parser.parsePictureHeader(du));
        checkParamsPictureHeader(parser);
    }

    MPEG2_PARSER_TEST(ParsePictureCodingExtension)
    {

        DecodeUnit du;
        EXPECT_TRUE(createDu(du, &PictureCodingExtensionArray[0], PictureCodingExtensionArray.size(), MPEG2_EXTENSION_START_CODE));

        BitReader br(du.m_data, du.m_size);
        EXPECT_TRUE(checkExtension(br, kPictureCoding));

        Parser parser;

        EXPECT_TRUE(parser.parsePictureCodingExtension(br));
        checkParamsPictureCodingExtension(parser);
    }

    MPEG2_PARSER_TEST(ParseSlice)
    {
        DecodeUnit du;
        EXPECT_TRUE(createDu(du, &SliceArray[0], SliceArray.size(), MPEG2_SLICE_START_CODE_MIN));

        Parser parser;
        Slice slice;

        EXPECT_TRUE(parser.parseSlice(slice, du));
        checkParamsSlice(parser, slice);
    }
} // namespace MPEG2 
} // namespace YamiParser