OSDN Git Service

test: add YUVImage class
[android-x86/hardware-intel-common-vaapi.git] / test / i965_jpeg_test_data.h
1 /*
2  * Copyright (C) 2016 Intel Corporation. All Rights Reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sub license, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial portions
14  * of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
19  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
20  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24
25 #ifndef I965_JPEG_TEST_DATA_H
26 #define I965_JPEG_TEST_DATA_H
27
28 #include <array>
29 #include <iostream>
30 #include <map>
31 #include <memory>
32 #include <va/va.h>
33 #include <vector>
34
35 namespace JPEG {
36     typedef std::vector<uint8_t> ByteData;
37
38     static const VAProfile profile = VAProfileJPEGBaseline;
39
40     static inline const ByteData generateSolid(
41         const std::array<uint8_t, 3>& yuv, const std::array<size_t, 2>& dim)
42     {
43         size_t count(dim[0] * dim[1]);
44         ByteData data(count, yuv[0]);
45         data.insert(data.end(), count, yuv[1]);
46         data.insert(data.end(), count, yuv[2]);
47         return data;
48     }
49 } // namespace JPEG
50
51 namespace JPEG {
52 namespace Decode {
53     typedef VAIQMatrixBufferJPEGBaseline                IQMatrix;
54     typedef VAHuffmanTableBufferJPEGBaseline            HuffmanTable;
55     typedef VAPictureParameterBufferJPEGBaseline        PictureParameter;
56     typedef VASliceParameterBufferJPEGBaseline          SliceParameter;
57
58     static const VAEntrypoint entrypoint = VAEntrypointVLD;
59
60     static const HuffmanTable defaultHuffmanTable = {
61         load_huffman_table: { 0x01, 0x01 },
62         huffman_table: {
63             { // luminance
64                 num_dc_codes: {
65                     0x00,0x01,0x05,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,
66                     0x00,0x00,0x00,0x00
67                 },
68                 dc_values:    {
69                     0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b
70                 },
71                 num_ac_codes: {
72                     0x00,0x02,0x01,0x03,0x03,0x02,0x04,0x03,0x05,0x05,0x04,0x04,
73                     0x00,0x00,0x01,0x7d
74                 },
75                 ac_values:    {
76                     0x01,0x02,0x03,0x00,0x04,0x11,0x05,0x12,0x21,0x31,0x41,0x06,
77                     0x13,0x51,0x61,0x07,0x22,0x71,0x14,0x32,0x81,0x91,0xa1,0x08,
78                     0x23,0x42,0xb1,0xc1,0x15,0x52,0xd1,0xf0,0x24,0x33,0x62,0x72,
79                     0x82,0x09,0x0a,0x16,0x17,0x18,0x19,0x1a,0x25,0x26,0x27,0x28,
80                     0x29,0x2a,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x43,0x44,0x45,
81                     0x46,0x47,0x48,0x49,0x4a,0x53,0x54,0x55,0x56,0x57,0x58,0x59,
82                     0x5a,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x73,0x74,0x75,
83                     0x76,0x77,0x78,0x79,0x7a,0x83,0x84,0x85,0x86,0x87,0x88,0x89,
84                     0x8a,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0xa2,0xa3,
85                     0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xb2,0xb3,0xb4,0xb5,0xb6,
86                     0xb7,0xb8,0xb9,0xba,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,
87                     0xca,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xe1,0xe2,
88                     0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xf1,0xf2,0xf3,0xf4,
89                     0xf5,0xf6,0xf7,0xf8,0xf9,0xfa
90                 },
91                 pad: { 0x00, 0x00 }
92             },
93             { // chrominance
94                 num_dc_codes: {
95                     0x00,0x03,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00,
96                     0x00,0x00,0x00,0x00
97                 },
98                 dc_values:    {
99                     0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b
100                 },
101                 num_ac_codes: {
102                     0x00,0x02,0x01,0x02,0x04,0x04,0x03,0x04,0x07,0x05,0x04,0x04,
103                     0x00,0x01,0x02,0x77
104                 },
105                 ac_values:    {
106                     0x00,0x01,0x02,0x03,0x11,0x04,0x05,0x21,0x31,0x06,0x12,0x41,
107                     0x51,0x07,0x61,0x71,0x13,0x22,0x32,0x81,0x08,0x14,0x42,0x91,
108                     0xa1,0xb1,0xc1,0x09,0x23,0x33,0x52,0xf0,0x15,0x62,0x72,0xd1,
109                     0x0a,0x16,0x24,0x34,0xe1,0x25,0xf1,0x17,0x18,0x19,0x1a,0x26,
110                     0x27,0x28,0x29,0x2a,0x35,0x36,0x37,0x38,0x39,0x3a,0x43,0x44,
111                     0x45,0x46,0x47,0x48,0x49,0x4a,0x53,0x54,0x55,0x56,0x57,0x58,
112                     0x59,0x5a,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x73,0x74,
113                     0x75,0x76,0x77,0x78,0x79,0x7a,0x82,0x83,0x84,0x85,0x86,0x87,
114                     0x88,0x89,0x8a,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,
115                     0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xb2,0xb3,0xb4,
116                     0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,
117                     0xc8,0xc9,0xca,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,
118                     0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xf2,0xf3,0xf4,
119                     0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,
120                 },
121                 pad: { 0x00, 0x00 }
122             }
123         }
124     };
125
126     static const IQMatrix defaultIQMatrix = { /* Quality 100 */
127         load_quantiser_table: { 0x1,0x1,0x0,0x0 },
128         quantiser_table: {
129             {
130                 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
131                 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
132                 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
133                 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
134                 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
135                 0x01,0x01,0x01,0x01,
136             },
137             {
138                 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
139                 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
140                 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
141                 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
142                 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
143                 0x01,0x01,0x01,0x01,
144             },
145         },
146     };
147
148     static const PictureParameter defaultPictureParameter = {
149         picture_width:  10,
150         picture_height: 10,
151         /* component_id, h_sampling_factor, v_sampling_factor, quantiser_table_selector */
152         components:     {{1,1,1,0}, {2,1,1,1}, {3,1,1,1}},
153         num_components: 3,
154     };
155
156     static const SliceParameter defaultSliceParameter = {
157         slice_data_size:                        0,
158         slice_data_offset:                      0,
159         slice_data_flag:                        VA_SLICE_DATA_FLAG_ALL,
160         slice_horizontal_position:              0,
161         slice_vertical_position:                0,
162
163         /* component_selector, dc_table_selector, ac_table_selector */
164         components: {{1,0,0},{2,1,1},{3,1,1}},
165
166         num_components:                         3,
167         restart_interval:                       0,
168         num_mcus:                               4,
169     };
170
171     class PictureData
172     {
173     public:
174         typedef std::shared_ptr<PictureData> Shared;
175         typedef std::shared_ptr<const PictureData> SharedConst;
176
177         template<const unsigned W, const unsigned H>
178         static SharedConst make(
179             const unsigned fourcc,
180             const ByteData& slice,
181             const SliceParameter& sparam = defaultSliceParameter,
182             const PictureParameter& pparam = defaultPictureParameter,
183             const HuffmanTable& huffman = defaultHuffmanTable,
184             const IQMatrix& iqmatrix = defaultIQMatrix)
185         {
186             return make(fourcc, slice, W, H, sparam, pparam, huffman, iqmatrix);
187         }
188
189         static SharedConst make(
190             const unsigned fourcc,
191             const ByteData& slice,
192             const unsigned w, const unsigned h,
193             const SliceParameter& sparam = defaultSliceParameter,
194             const PictureParameter& pparam = defaultPictureParameter,
195             const HuffmanTable& huffman = defaultHuffmanTable,
196             const IQMatrix& iqmatrix = defaultIQMatrix)
197         {
198             Shared pd(
199                 new PictureData {
200                     slice: slice,
201                     sparam: sparam,
202                     pparam: pparam,
203                     huffman: huffman,
204                     iqmatrix: iqmatrix,
205                     format: 0,
206                     fourcc: fourcc,
207                 }
208             );
209
210             pd->sparam.slice_data_size = slice.size();
211             pd->pparam.picture_width = w;
212             pd->pparam.picture_height = h;
213
214             switch(fourcc)
215             {
216             case VA_FOURCC_IMC3:
217                 pd->format = VA_RT_FORMAT_YUV420;
218                 pd->pparam.components[0].h_sampling_factor = 2;
219                 pd->pparam.components[0].v_sampling_factor = 2;
220                 break;
221             case VA_FOURCC_422H:
222                 pd->format = VA_RT_FORMAT_YUV422;
223                 pd->pparam.components[0].h_sampling_factor = 2;
224                 pd->pparam.components[0].v_sampling_factor = 1;
225                 break;
226             case VA_FOURCC_422V:
227                 pd->format = VA_RT_FORMAT_YUV422;
228                 pd->pparam.components[0].h_sampling_factor = 1;
229                 pd->pparam.components[0].v_sampling_factor = 2;
230                 break;
231             case VA_FOURCC_411P:
232                 pd->format = VA_RT_FORMAT_YUV411;
233                 pd->pparam.components[0].h_sampling_factor = 4;
234                 pd->pparam.components[0].v_sampling_factor = 1;
235                 break;
236             case VA_FOURCC_444P:
237                 pd->format = VA_RT_FORMAT_YUV444;
238                 pd->pparam.components[0].h_sampling_factor = 1;
239                 pd->pparam.components[0].v_sampling_factor = 1;
240                 break;
241             case VA_FOURCC_Y800:
242                 pd->format = VA_RT_FORMAT_YUV400;
243                 pd->pparam.components[0].h_sampling_factor = 1;
244                 pd->pparam.components[0].h_sampling_factor = 1;
245                 pd->pparam.num_components = 1;
246                 pd->sparam.num_components = 1;
247                 break;
248             default:
249                 break;
250             }
251
252             /* Calculate num_mcus */
253             int hfactor = pd->pparam.components[0].h_sampling_factor << 3;
254             int vfactor = pd->pparam.components[0].v_sampling_factor << 3;
255             int wmcu = (w + hfactor - 1) / hfactor;
256             int hmcu = (h + vfactor - 1) / vfactor;
257             pd->sparam.num_mcus = wmcu * hmcu;
258
259             return pd;
260         }
261
262         const ByteData          slice;
263         SliceParameter          sparam;
264         PictureParameter        pparam;
265         HuffmanTable            huffman;
266         IQMatrix                iqmatrix;
267         unsigned                format;
268         unsigned                fourcc;
269     };
270
271     class TestPattern
272     {
273     public:
274         typedef std::shared_ptr<TestPattern> Shared;
275         typedef std::shared_ptr<const TestPattern> SharedConst;
276
277         virtual const ByteData& decoded() const = 0;
278         virtual PictureData::SharedConst encoded(unsigned) const = 0;
279
280         virtual void repr(std::ostream&) const = 0;
281
282         friend std::ostream& operator <<(std::ostream& os, const TestPattern& t)
283         {
284             t.repr(os);
285             return os;
286         }
287     };
288
289     template <const unsigned N>
290     class TestPatternData
291         : public TestPattern
292     {
293     private:
294         typedef std::map<const unsigned, PictureData::SharedConst> EncodedMap;
295         typedef std::map<const unsigned, const ByteData> ByteDataMap;
296
297     public:
298         const ByteData& decoded() const { return getDecoded(); }
299
300         PictureData::SharedConst encoded(const unsigned fourcc) const
301         {
302             const EncodedMap& em = getEncodedMap();
303             const EncodedMap::const_iterator match(em.find(fourcc));
304             if (match == em.end())
305                 return PictureData::SharedConst();
306             return match->second;
307         }
308
309         void repr(std::ostream& os) const { os << N; }
310
311         template <const unsigned W, const unsigned H>
312         static bool initialize(ByteData d, const ByteDataMap& e)
313         {
314             getDecoded().swap(d);
315
316             EncodedMap& em = getEncodedMap();
317             bool result = true;
318             for (auto const &b : e) {
319                 auto pd(PictureData::make<W, H>(b.first, b.second));
320                 auto pair = std::make_pair(b.first, pd);
321                 result &= em.insert(pair).second;
322             }
323             return result;
324         }
325
326     private:
327         static ByteData& getDecoded()
328         {
329             static ByteData d;
330             return d;
331         }
332
333         static EncodedMap& getEncodedMap()
334         {
335             static EncodedMap em;
336             return em;
337         }
338
339         static const bool m_valid;
340     };
341 } // namespace Decode
342 } // namespace JPEG
343
344 namespace JPEG {
345 namespace Encode {
346     typedef VAQMatrixBufferJPEG                 IQMatrix;
347     typedef VAHuffmanTableBufferJPEGBaseline    HuffmanTable;
348     typedef VAEncPictureParameterBufferJPEG     PictureParameter;
349     typedef VAEncSliceParameterBufferJPEG       SliceParameter;
350
351     static const VAEntrypoint entrypoint = VAEntrypointEncPicture;
352
353     static const IQMatrix defaultIQMatrix = { /* Quality 50 */
354         load_lum_quantiser_matrix: 1,
355         load_chroma_quantiser_matrix: 1,
356         lum_quantiser_matrix: {
357             0x10,0x0b,0x0c,0x0e,0x0c,0x0a,0x10,0x0e,
358             0x0d,0x0e,0x12,0x11,0x10,0x13,0x18,0x28,
359             0x1a,0x18,0x16,0x16,0x18,0x31,0x23,0x25,
360             0x1d,0x28,0x3a,0x33,0x3d,0x3c,0x39,0x33,
361             0x38,0x37,0x40,0x48,0x5c,0x4e,0x40,0x44,
362             0x57,0x45,0x37,0x38,0x50,0x6d,0x51,0x57,
363             0x5f,0x62,0x67,0x68,0x67,0x3e,0x4d,0x71,
364             0x79,0x70,0x64,0x78,0x5c,0x65,0x67,0x63,
365         },
366         chroma_quantiser_matrix: {
367             0x11,0x12,0x12,0x18,0x15,0x18,0x2f,0x1a,
368             0x1a,0x2f,0x63,0x42,0x38,0x42,0x63,0x63,
369             0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x63,
370             0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x63,
371             0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x63,
372             0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x63,
373             0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x63,
374             0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x63,
375         },
376     };
377
378     static const HuffmanTable defaultHuffmanTable =
379         ::JPEG::Decode::defaultHuffmanTable;
380
381     static const PictureParameter defaultPictureParameter = {
382         reconstructed_picture:      VA_INVALID_ID,
383         picture_width:              10,
384         picture_height:             10,
385         coded_buf:                  VA_INVALID_ID,
386         pic_flags:                  {value: 0x00100},
387         sample_bit_depth:           8,
388         num_scan:                   1,
389         num_components:             3,
390         component_id:               {0, 1, 2, 0},
391         quantiser_table_selector:   {0, 1, 1, 0},
392         quality:                    100,
393     };
394
395     static const SliceParameter defaultSliceParameter = {
396         restart_interval:   0,
397         num_components:     3,
398         /* component_selector, dc_table_selector, ac_table_selector */
399         components:         {{1,0,0},{2,1,1},{3,1,1}},
400     };
401
402     class TestInput
403         : public std::enable_shared_from_this<TestInput>
404     {
405     public:
406         typedef std::shared_ptr<TestInput> Shared;
407         typedef std::shared_ptr<const TestInput> SharedConst;
408
409         static Shared create(const unsigned, const unsigned, const unsigned);
410
411         const unsigned width() const;
412         const unsigned height() const;
413         const uint8_t* plane(const size_t) const;
414         const SharedConst toOutputFourcc() const;
415
416         friend ::std::ostream& operator<<(::std::ostream&, const TestInput&);
417         friend ::std::ostream& operator<<(::std::ostream&, const Shared&);
418         friend ::std::ostream& operator<<(::std::ostream&, const SharedConst&);
419
420         ByteData            bytes;
421         PictureParameter    picture;
422         IQMatrix            matrix;
423         HuffmanTable        huffman;
424         SliceParameter      slice;
425         unsigned            fourcc;
426         unsigned            fourcc_output;
427         unsigned            format;
428         size_t              planes;
429         std::array<size_t, 3> widths;
430         std::array<size_t, 3> heights;
431         std::array<size_t, 3> offsets;
432         std::array<size_t, 3> sizes;
433
434     private:
435         TestInput();
436
437         /** get pointer to beginning of plane @param i **/
438         uint8_t* begin(const size_t i);
439         const uint8_t* begin(const size_t i) const;
440
441         /** get pointer to end of plane @param i **/
442         uint8_t* end(const size_t i);
443         const uint8_t* end(const size_t i) const;
444     };
445
446     class TestInputCreator
447     {
448     public:
449         typedef std::shared_ptr<TestInputCreator> Shared;
450         typedef std::shared_ptr<const TestInputCreator> SharedConst;
451
452         TestInput::Shared create(const unsigned) const;
453
454         friend ::std::ostream& operator<<(
455             ::std::ostream&, const TestInputCreator&);
456         friend ::std::ostream& operator<<(
457             ::std::ostream&, const TestInputCreator::Shared&);
458         friend ::std::ostream& operator<<(
459             ::std::ostream&, const TestInputCreator::SharedConst&);
460
461     protected:
462         virtual std::array<unsigned, 2> getResolution() const = 0;
463         virtual void repr(::std::ostream& os) const = 0;
464     };
465
466     class RandomSizeCreator
467         : public TestInputCreator
468     {
469     protected:
470         std::array<unsigned, 2> getResolution() const;
471         void repr(::std::ostream&) const;
472     };
473
474     class FixedSizeCreator
475         : public TestInputCreator
476     {
477     public:
478         FixedSizeCreator(const std::array<unsigned, 2>&);
479
480     protected:
481         std::array<unsigned, 2> getResolution() const;
482         void repr(::std::ostream& os) const;
483
484     private:
485         const std::array<unsigned, 2> res;
486     };
487
488     typedef std::vector<TestInputCreator::SharedConst> InputCreators;
489
490 } // namespace Encode
491 } // namespace JPEG
492
493 #endif