OSDN Git Service

test: jpeg/enc: fix TestInput::SharedConst typedef
[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 "i965_test_fixture.h"
29
30 #include <array>
31 #include <iostream>
32 #include <map>
33 #include <memory>
34 #include <va/va.h>
35 #include <vector>
36
37 namespace JPEG {
38     typedef std::vector<uint8_t> ByteData;
39
40     static const VAProfile profile = VAProfileJPEGBaseline;
41
42     static inline const ByteData generateSolid(
43         const std::array<uint8_t, 3>& yuv, const std::array<size_t, 2>& dim)
44     {
45         size_t count(dim[0] * dim[1]);
46         ByteData data(count, yuv[0]);
47         data.insert(data.end(), count, yuv[1]);
48         data.insert(data.end(), count, yuv[2]);
49         return data;
50     }
51 } // namespace JPEG
52
53 namespace JPEG {
54 namespace Decode {
55     typedef VAIQMatrixBufferJPEGBaseline                IQMatrix;
56     typedef VAHuffmanTableBufferJPEGBaseline            HuffmanTable;
57     typedef VAPictureParameterBufferJPEGBaseline        PictureParameter;
58     typedef VASliceParameterBufferJPEGBaseline          SliceParameter;
59
60     static const VAEntrypoint entrypoint = VAEntrypointVLD;
61
62     static const HuffmanTable defaultHuffmanTable = {
63         load_huffman_table: { 0x01, 0x01 },
64         huffman_table: {
65             { // luminance
66                 num_dc_codes: {
67                     0x00,0x01,0x05,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,
68                     0x00,0x00,0x00,0x00
69                 },
70                 dc_values:    {
71                     0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b
72                 },
73                 num_ac_codes: {
74                     0x00,0x02,0x01,0x03,0x03,0x02,0x04,0x03,0x05,0x05,0x04,0x04,
75                     0x00,0x00,0x01,0x7d
76                 },
77                 ac_values:    {
78                     0x01,0x02,0x03,0x00,0x04,0x11,0x05,0x12,0x21,0x31,0x41,0x06,
79                     0x13,0x51,0x61,0x07,0x22,0x71,0x14,0x32,0x81,0x91,0xa1,0x08,
80                     0x23,0x42,0xb1,0xc1,0x15,0x52,0xd1,0xf0,0x24,0x33,0x62,0x72,
81                     0x82,0x09,0x0a,0x16,0x17,0x18,0x19,0x1a,0x25,0x26,0x27,0x28,
82                     0x29,0x2a,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x43,0x44,0x45,
83                     0x46,0x47,0x48,0x49,0x4a,0x53,0x54,0x55,0x56,0x57,0x58,0x59,
84                     0x5a,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x73,0x74,0x75,
85                     0x76,0x77,0x78,0x79,0x7a,0x83,0x84,0x85,0x86,0x87,0x88,0x89,
86                     0x8a,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0xa2,0xa3,
87                     0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xb2,0xb3,0xb4,0xb5,0xb6,
88                     0xb7,0xb8,0xb9,0xba,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,
89                     0xca,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xe1,0xe2,
90                     0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xf1,0xf2,0xf3,0xf4,
91                     0xf5,0xf6,0xf7,0xf8,0xf9,0xfa
92                 },
93                 pad: { 0x00, 0x00 }
94             },
95             { // chrominance
96                 num_dc_codes: {
97                     0x00,0x03,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00,
98                     0x00,0x00,0x00,0x00
99                 },
100                 dc_values:    {
101                     0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b
102                 },
103                 num_ac_codes: {
104                     0x00,0x02,0x01,0x02,0x04,0x04,0x03,0x04,0x07,0x05,0x04,0x04,
105                     0x00,0x01,0x02,0x77
106                 },
107                 ac_values:    {
108                     0x00,0x01,0x02,0x03,0x11,0x04,0x05,0x21,0x31,0x06,0x12,0x41,
109                     0x51,0x07,0x61,0x71,0x13,0x22,0x32,0x81,0x08,0x14,0x42,0x91,
110                     0xa1,0xb1,0xc1,0x09,0x23,0x33,0x52,0xf0,0x15,0x62,0x72,0xd1,
111                     0x0a,0x16,0x24,0x34,0xe1,0x25,0xf1,0x17,0x18,0x19,0x1a,0x26,
112                     0x27,0x28,0x29,0x2a,0x35,0x36,0x37,0x38,0x39,0x3a,0x43,0x44,
113                     0x45,0x46,0x47,0x48,0x49,0x4a,0x53,0x54,0x55,0x56,0x57,0x58,
114                     0x59,0x5a,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x73,0x74,
115                     0x75,0x76,0x77,0x78,0x79,0x7a,0x82,0x83,0x84,0x85,0x86,0x87,
116                     0x88,0x89,0x8a,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,
117                     0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xb2,0xb3,0xb4,
118                     0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,
119                     0xc8,0xc9,0xca,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,
120                     0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xf2,0xf3,0xf4,
121                     0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,
122                 },
123                 pad: { 0x00, 0x00 }
124             }
125         }
126     };
127
128     static const IQMatrix defaultIQMatrix = { /* Quality 100 */
129         load_quantiser_table: { 0x1,0x1,0x0,0x0 },
130         quantiser_table: {
131             {
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,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
136                 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
137                 0x01,0x01,0x01,0x01,
138             },
139             {
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,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
144                 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
145                 0x01,0x01,0x01,0x01,
146             },
147         },
148     };
149
150     static const PictureParameter defaultPictureParameter = {
151         picture_width:  10,
152         picture_height: 10,
153         /* component_id, h_sampling_factor, v_sampling_factor, quantiser_table_selector */
154         components:     {{1,1,1,0}, {2,1,1,1}, {3,1,1,1}},
155         num_components: 3,
156     };
157
158     static const SliceParameter defaultSliceParameter = {
159         slice_data_size:                        0,
160         slice_data_offset:                      0,
161         slice_data_flag:                        VA_SLICE_DATA_FLAG_ALL,
162         slice_horizontal_position:              0,
163         slice_vertical_position:                0,
164
165         /* component_selector, dc_table_selector, ac_table_selector */
166         components: {{1,0,0},{2,1,1},{3,1,1}},
167
168         num_components:                         3,
169         restart_interval:                       0,
170         num_mcus:                               4,
171     };
172
173     class PictureData
174     {
175     public:
176         typedef std::shared_ptr<PictureData> Shared;
177         typedef std::shared_ptr<const PictureData> SharedConst;
178
179         template<const unsigned W, const unsigned H>
180         static SharedConst make(
181             const unsigned fourcc,
182             const ByteData& slice,
183             const SliceParameter& sparam = defaultSliceParameter,
184             const PictureParameter& pparam = defaultPictureParameter,
185             const HuffmanTable& huffman = defaultHuffmanTable,
186             const IQMatrix& iqmatrix = defaultIQMatrix)
187         {
188             return make(fourcc, slice, W, H, sparam, pparam, huffman, iqmatrix);
189         }
190
191         static SharedConst make(
192             const unsigned fourcc,
193             const ByteData& slice,
194             const unsigned w, const unsigned h,
195             const SliceParameter& sparam = defaultSliceParameter,
196             const PictureParameter& pparam = defaultPictureParameter,
197             const HuffmanTable& huffman = defaultHuffmanTable,
198             const IQMatrix& iqmatrix = defaultIQMatrix)
199         {
200             Shared pd(
201                 new PictureData {
202                     slice: slice,
203                     sparam: sparam,
204                     pparam: pparam,
205                     huffman: huffman,
206                     iqmatrix: iqmatrix,
207                     format: 0,
208                     fourcc: fourcc,
209                 }
210             );
211
212             pd->sparam.slice_data_size = slice.size();
213             pd->pparam.picture_width = w;
214             pd->pparam.picture_height = h;
215
216             switch(fourcc)
217             {
218             case VA_FOURCC_IMC3:
219                 pd->format = VA_RT_FORMAT_YUV420;
220                 pd->pparam.components[0].h_sampling_factor = 2;
221                 pd->pparam.components[0].v_sampling_factor = 2;
222                 break;
223             case VA_FOURCC_422H:
224                 pd->format = VA_RT_FORMAT_YUV422;
225                 pd->pparam.components[0].h_sampling_factor = 2;
226                 pd->pparam.components[0].v_sampling_factor = 1;
227                 break;
228             case VA_FOURCC_422V:
229                 pd->format = VA_RT_FORMAT_YUV422;
230                 pd->pparam.components[0].h_sampling_factor = 1;
231                 pd->pparam.components[0].v_sampling_factor = 2;
232                 break;
233             case VA_FOURCC_411P:
234                 pd->format = VA_RT_FORMAT_YUV411;
235                 pd->pparam.components[0].h_sampling_factor = 4;
236                 pd->pparam.components[0].v_sampling_factor = 1;
237                 break;
238             case VA_FOURCC_444P:
239                 pd->format = VA_RT_FORMAT_YUV444;
240                 pd->pparam.components[0].h_sampling_factor = 1;
241                 pd->pparam.components[0].v_sampling_factor = 1;
242             default:
243                 break;
244             }
245
246             /* Calculate num_mcus */
247             int hfactor = pd->pparam.components[0].h_sampling_factor << 3;
248             int vfactor = pd->pparam.components[0].v_sampling_factor << 3;
249             int wmcu = (w + hfactor - 1) / hfactor;
250             int hmcu = (h + vfactor - 1) / vfactor;
251             pd->sparam.num_mcus = wmcu * hmcu;
252
253             return pd;
254         }
255
256         const ByteData          slice;
257         SliceParameter          sparam;
258         PictureParameter        pparam;
259         HuffmanTable            huffman;
260         IQMatrix                iqmatrix;
261         unsigned                format;
262         unsigned                fourcc;
263     };
264
265     class TestPattern
266     {
267     public:
268         typedef std::shared_ptr<TestPattern> Shared;
269         typedef std::shared_ptr<const TestPattern> SharedConst;
270
271         virtual const ByteData& decoded() const = 0;
272         virtual PictureData::SharedConst encoded(unsigned) const = 0;
273
274         virtual void repr(std::ostream&) const = 0;
275
276         friend std::ostream& operator <<(std::ostream& os, const TestPattern& t)
277         {
278             t.repr(os);
279             return os;
280         }
281     };
282
283     template <const unsigned N>
284     class TestPatternData
285         : public TestPattern
286     {
287     private:
288         typedef std::map<const unsigned, PictureData::SharedConst> EncodedMap;
289         typedef std::map<const unsigned, const ByteData> ByteDataMap;
290
291     public:
292         const ByteData& decoded() const { return getDecoded(); }
293
294         PictureData::SharedConst encoded(const unsigned fourcc) const
295         {
296             const EncodedMap& em = getEncodedMap();
297             const EncodedMap::const_iterator match(em.find(fourcc));
298             if (match == em.end())
299                 return PictureData::SharedConst();
300             return match->second;
301         }
302
303         void repr(std::ostream& os) const { os << N; }
304
305         template <const unsigned W, const unsigned H>
306         static bool initialize(ByteData d, const ByteDataMap& e)
307         {
308             getDecoded().swap(d);
309
310             EncodedMap& em = getEncodedMap();
311             bool result = true;
312             for (auto const &b : e) {
313                 auto pd(PictureData::make<W, H>(b.first, b.second));
314                 auto pair = std::make_pair(b.first, pd);
315                 result &= em.insert(pair).second;
316             }
317             return result;
318         }
319
320     private:
321         static ByteData& getDecoded()
322         {
323             static ByteData d;
324             return d;
325         }
326
327         static EncodedMap& getEncodedMap()
328         {
329             static EncodedMap em;
330             return em;
331         }
332
333         static const bool m_valid;
334     };
335 } // namespace Decode
336 } // namespace JPEG
337
338 namespace JPEG {
339 namespace Encode {
340     typedef VAQMatrixBufferJPEG                 IQMatrix;
341     typedef VAHuffmanTableBufferJPEGBaseline    HuffmanTable;
342     typedef VAEncPictureParameterBufferJPEG     PictureParameter;
343     typedef VAEncSliceParameterBufferJPEG       SliceParameter;
344
345     static const VAEntrypoint entrypoint = VAEntrypointEncPicture;
346
347     static const IQMatrix defaultIQMatrix = { /* Quality 50 */
348         load_lum_quantiser_matrix: 1,
349         load_chroma_quantiser_matrix: 1,
350         lum_quantiser_matrix: {
351             0x10,0x0b,0x0c,0x0e,0x0c,0x0a,0x10,0x0e,
352             0x0d,0x0e,0x12,0x11,0x10,0x13,0x18,0x28,
353             0x1a,0x18,0x16,0x16,0x18,0x31,0x23,0x25,
354             0x1d,0x28,0x3a,0x33,0x3d,0x3c,0x39,0x33,
355             0x38,0x37,0x40,0x48,0x5c,0x4e,0x40,0x44,
356             0x57,0x45,0x37,0x38,0x50,0x6d,0x51,0x57,
357             0x5f,0x62,0x67,0x68,0x67,0x3e,0x4d,0x71,
358             0x79,0x70,0x64,0x78,0x5c,0x65,0x67,0x63,
359         },
360         chroma_quantiser_matrix: {
361             0x11,0x12,0x12,0x18,0x15,0x18,0x2f,0x1a,
362             0x1a,0x2f,0x63,0x42,0x38,0x42,0x63,0x63,
363             0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x63,
364             0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x63,
365             0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x63,
366             0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x63,
367             0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x63,
368             0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x63,
369         },
370     };
371
372     static const HuffmanTable defaultHuffmanTable =
373         ::JPEG::Decode::defaultHuffmanTable;
374
375     static const PictureParameter defaultPictureParameter = {
376         reconstructed_picture:      VA_INVALID_ID,
377         picture_width:              10,
378         picture_height:             10,
379         coded_buf:                  VA_INVALID_ID,
380         pic_flags:                  {value: 0x00100},
381         sample_bit_depth:           8,
382         num_scan:                   1,
383         num_components:             3,
384         component_id:               {0, 1, 2, 0},
385         quantiser_table_selector:   {0, 1, 1, 0},
386         quality:                    100,
387     };
388
389     static const SliceParameter defaultSliceParameter = {
390         restart_interval:   0,
391         num_components:     3,
392         /* component_selector, dc_table_selector, ac_table_selector */
393         components:         {{1,0,0},{2,1,1},{3,1,1}},
394     };
395
396     class TestInput
397     {
398     public:
399         typedef std::shared_ptr<TestInput> Shared;
400         typedef std::shared_ptr<const TestInput> SharedConst;
401
402         TestInput(const unsigned fourcc, const unsigned w, const unsigned h)
403             : bytes() // caller must fill this in after instantiation
404             , picture(defaultPictureParameter)
405             , matrix(defaultIQMatrix)
406             , huffman(defaultHuffmanTable)
407             , slice(defaultSliceParameter)
408             , fourcc(fourcc)
409             , fourcc_output(fourcc)
410             , format(0)
411             , planes(0)
412             , widths{0,0,0}
413             , heights{0,0,0}
414             , offsets{0,0,0}
415             , sizes{0,0,0}
416         {
417             picture.picture_width = ALIGN(w,2);
418             picture.picture_height = ALIGN(h,2);
419
420             switch(fourcc) {
421             case VA_FOURCC('I', '4', '2', '0'):
422                 planes = 3;
423                 widths = {
424                     w +( w & 1),
425                     (w + 1) >> 1,
426                     (w + 1) >> 1
427                 };
428                 heights = {
429                     h + (h & 1),
430                     (h + 1) >> 1,
431                     (h + 1) >> 1
432                 };
433                 format = VA_RT_FORMAT_YUV420;
434                 fourcc_output = VA_FOURCC_IMC3;
435                 break;
436             case VA_FOURCC_NV12:
437                 planes = 2;
438                 widths = {
439                     w + (w & 1),
440                     w + (w & 1),
441                     0
442                 };
443                 heights = {
444                     h + (h & 1),
445                     (h + 1) >> 1,
446                     0
447                 };
448                 format = VA_RT_FORMAT_YUV420;
449                 fourcc_output = VA_FOURCC_IMC3;
450                 break;
451             default:
452                 return;
453             }
454
455             for (size_t i(0); i < planes; ++i) {
456                 sizes[i] = widths[i] * heights[i];
457             }
458
459             for (size_t i(1); i < planes; ++i) {
460                 offsets[i] = sizes[i - 1];
461                 offsets[i] += offsets[i - 1];
462             }
463         }
464
465         const unsigned width() const
466         {
467             return picture.picture_width;
468         }
469
470         const unsigned height() const
471         {
472             return picture.picture_height;
473         }
474
475         const uint8_t* plane(const size_t i) const
476         {
477             return bytes.data() + offsets[i];
478         }
479
480         friend ::std::ostream& operator<<(::std::ostream& os, const TestInput& t)
481         {
482             return os
483                 << std::string((char*)(&t.fourcc), 4)
484                 << " " << t.width() << "x" << t.height()
485                 << " " << t.widths << " " << t.heights
486                 << " " << t.sizes << " " << t.offsets
487             ;
488         }
489
490         friend ::std::ostream& operator<<(::std::ostream& os, const Shared& t)
491         {
492             return os << *t;
493         }
494
495         ByteData            bytes;
496         PictureParameter    picture;
497         IQMatrix            matrix;
498         HuffmanTable        huffman;
499         SliceParameter      slice;
500         unsigned            fourcc;
501         unsigned            fourcc_output;
502         unsigned            format;
503         size_t              planes;
504         std::array<size_t, 3> widths;
505         std::array<size_t, 3> heights;
506         std::array<size_t, 3> offsets;
507         std::array<size_t, 3> sizes;
508     };
509
510
511 } // namespace Encode
512 } // namespace JPEG
513
514 #endif