1bf215546Sopenharmony_ci/**************************************************************************
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright 2017 Advanced Micro Devices, Inc.
4bf215546Sopenharmony_ci * All Rights Reserved.
5bf215546Sopenharmony_ci *
6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the
8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
12bf215546Sopenharmony_ci * the following conditions:
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
16bf215546Sopenharmony_ci * of the Software.
17bf215546Sopenharmony_ci *
18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21bf215546Sopenharmony_ci * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR
22bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci **************************************************************************/
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#include "va_private.h"
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_civoid vlVaHandlePictureParameterBufferMJPEG(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
31bf215546Sopenharmony_ci{
32bf215546Sopenharmony_ci   VAPictureParameterBufferJPEGBaseline *mjpeg = buf->data;
33bf215546Sopenharmony_ci   unsigned sf;
34bf215546Sopenharmony_ci   int i;
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_ci   assert(buf->size >= sizeof(VAPictureParameterBufferJPEGBaseline) && buf->num_elements == 1);
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci   context->desc.mjpeg.picture_parameter.picture_width = mjpeg->picture_width;
39bf215546Sopenharmony_ci   context->desc.mjpeg.picture_parameter.picture_height = mjpeg->picture_height;
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_ci   for (i = 0; i < mjpeg->num_components; ++i) {
42bf215546Sopenharmony_ci      context->desc.mjpeg.picture_parameter.components[i].component_id =
43bf215546Sopenharmony_ci         mjpeg->components[i].component_id;
44bf215546Sopenharmony_ci      context->desc.mjpeg.picture_parameter.components[i].h_sampling_factor =
45bf215546Sopenharmony_ci         mjpeg->components[i].h_sampling_factor;
46bf215546Sopenharmony_ci      context->desc.mjpeg.picture_parameter.components[i].v_sampling_factor =
47bf215546Sopenharmony_ci         mjpeg->components[i].v_sampling_factor;
48bf215546Sopenharmony_ci      context->desc.mjpeg.picture_parameter.components[i].quantiser_table_selector =
49bf215546Sopenharmony_ci         mjpeg->components[i].quantiser_table_selector;
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci      sf = mjpeg->components[i].h_sampling_factor << 4 | mjpeg->components[i].v_sampling_factor;
52bf215546Sopenharmony_ci      context->mjpeg.sampling_factor <<= 8;
53bf215546Sopenharmony_ci      context->mjpeg.sampling_factor |= sf;
54bf215546Sopenharmony_ci   }
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci   context->desc.mjpeg.picture_parameter.num_components = mjpeg->num_components;
57bf215546Sopenharmony_ci}
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_civoid vlVaHandleIQMatrixBufferMJPEG(vlVaContext *context, vlVaBuffer *buf)
60bf215546Sopenharmony_ci{
61bf215546Sopenharmony_ci   VAIQMatrixBufferJPEGBaseline *mjpeg = buf->data;
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_ci   assert(buf->size >= sizeof(VAIQMatrixBufferJPEGBaseline) && buf->num_elements == 1);
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_ci   memcpy(&context->desc.mjpeg.quantization_table.load_quantiser_table, mjpeg->load_quantiser_table, 4);
66bf215546Sopenharmony_ci   memcpy(&context->desc.mjpeg.quantization_table.quantiser_table, mjpeg->quantiser_table, 4 * 64);
67bf215546Sopenharmony_ci}
68bf215546Sopenharmony_ci
69bf215546Sopenharmony_civoid vlVaHandleHuffmanTableBufferType(vlVaContext *context, vlVaBuffer *buf)
70bf215546Sopenharmony_ci{
71bf215546Sopenharmony_ci   VAHuffmanTableBufferJPEGBaseline *mjpeg = buf->data;
72bf215546Sopenharmony_ci   int i;
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci   assert(buf->size >= sizeof(VASliceParameterBufferJPEGBaseline) && buf->num_elements == 1);
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_ci   for (i = 0; i < 2; ++i) {
77bf215546Sopenharmony_ci      context->desc.mjpeg.huffman_table.load_huffman_table[i] = mjpeg->load_huffman_table[i];
78bf215546Sopenharmony_ci
79bf215546Sopenharmony_ci      memcpy(&context->desc.mjpeg.huffman_table.table[i].num_dc_codes,
80bf215546Sopenharmony_ci         mjpeg->huffman_table[i].num_dc_codes, 16);
81bf215546Sopenharmony_ci      memcpy(&context->desc.mjpeg.huffman_table.table[i].dc_values,
82bf215546Sopenharmony_ci         mjpeg->huffman_table[i].dc_values, 12);
83bf215546Sopenharmony_ci      memcpy(&context->desc.mjpeg.huffman_table.table[i].num_ac_codes,
84bf215546Sopenharmony_ci         mjpeg->huffman_table[i].num_ac_codes, 16);
85bf215546Sopenharmony_ci      memcpy(&context->desc.mjpeg.huffman_table.table[i].ac_values,
86bf215546Sopenharmony_ci         mjpeg->huffman_table[i].ac_values, 162);
87bf215546Sopenharmony_ci      memcpy(&context->desc.mjpeg.huffman_table.table[i].pad, mjpeg->huffman_table[i].pad, 2);
88bf215546Sopenharmony_ci   }
89bf215546Sopenharmony_ci}
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_civoid vlVaHandleSliceParameterBufferMJPEG(vlVaContext *context, vlVaBuffer *buf)
92bf215546Sopenharmony_ci{
93bf215546Sopenharmony_ci   VASliceParameterBufferJPEGBaseline *mjpeg = buf->data;
94bf215546Sopenharmony_ci   int i;
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_ci   assert(buf->size >= sizeof(VASliceParameterBufferJPEGBaseline) && buf->num_elements == 1);
97bf215546Sopenharmony_ci
98bf215546Sopenharmony_ci   context->desc.mjpeg.slice_parameter.slice_data_size = mjpeg->slice_data_size;
99bf215546Sopenharmony_ci   context->desc.mjpeg.slice_parameter.slice_data_offset = mjpeg->slice_data_offset;
100bf215546Sopenharmony_ci   context->desc.mjpeg.slice_parameter.slice_data_flag = mjpeg->slice_data_flag;
101bf215546Sopenharmony_ci   context->desc.mjpeg.slice_parameter.slice_horizontal_position = mjpeg->slice_horizontal_position;
102bf215546Sopenharmony_ci   context->desc.mjpeg.slice_parameter.slice_vertical_position = mjpeg->slice_vertical_position;
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ci   for (i = 0; i < mjpeg->num_components; ++i) {
105bf215546Sopenharmony_ci      context->desc.mjpeg.slice_parameter.components[i].component_selector =
106bf215546Sopenharmony_ci         mjpeg->components[i].component_selector;
107bf215546Sopenharmony_ci      context->desc.mjpeg.slice_parameter.components[i].dc_table_selector =
108bf215546Sopenharmony_ci         mjpeg->components[i].dc_table_selector;
109bf215546Sopenharmony_ci      context->desc.mjpeg.slice_parameter.components[i].ac_table_selector =
110bf215546Sopenharmony_ci         mjpeg->components[i].ac_table_selector;
111bf215546Sopenharmony_ci   }
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_ci   context->desc.mjpeg.slice_parameter.num_components = mjpeg->num_components;
114bf215546Sopenharmony_ci   context->desc.mjpeg.slice_parameter.restart_interval = mjpeg->restart_interval;
115bf215546Sopenharmony_ci   context->desc.mjpeg.slice_parameter.num_mcus = mjpeg->num_mcus;
116bf215546Sopenharmony_ci}
117bf215546Sopenharmony_ci
118bf215546Sopenharmony_civoid vlVaGetJpegSliceHeader(vlVaContext *context)
119bf215546Sopenharmony_ci{
120bf215546Sopenharmony_ci   int size = 0, saved_size, len_pos, i;
121bf215546Sopenharmony_ci   uint16_t *bs;
122bf215546Sopenharmony_ci   uint8_t *p = context->mjpeg.slice_header;
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_ci   /* SOI */
125bf215546Sopenharmony_ci   p[size++] = 0xff;
126bf215546Sopenharmony_ci   p[size++] = 0xd8;
127bf215546Sopenharmony_ci
128bf215546Sopenharmony_ci   /* DQT */
129bf215546Sopenharmony_ci   p[size++] = 0xff;
130bf215546Sopenharmony_ci   p[size++] = 0xdb;
131bf215546Sopenharmony_ci
132bf215546Sopenharmony_ci   len_pos = size++;
133bf215546Sopenharmony_ci   size++;
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_ci   for (i = 0; i < 4; ++i) {
136bf215546Sopenharmony_ci      if (context->desc.mjpeg.quantization_table.load_quantiser_table[i] == 0)
137bf215546Sopenharmony_ci         continue;
138bf215546Sopenharmony_ci
139bf215546Sopenharmony_ci      p[size++] = i;
140bf215546Sopenharmony_ci      memcpy((p + size), &context->desc.mjpeg.quantization_table.quantiser_table[i], 64);
141bf215546Sopenharmony_ci      size += 64;
142bf215546Sopenharmony_ci   }
143bf215546Sopenharmony_ci
144bf215546Sopenharmony_ci   bs = (uint16_t*)&p[len_pos];
145bf215546Sopenharmony_ci   *bs = util_bswap16(size - 4);
146bf215546Sopenharmony_ci
147bf215546Sopenharmony_ci   saved_size = size;
148bf215546Sopenharmony_ci
149bf215546Sopenharmony_ci   /* DHT */
150bf215546Sopenharmony_ci   p[size++] = 0xff;
151bf215546Sopenharmony_ci   p[size++] = 0xc4;
152bf215546Sopenharmony_ci
153bf215546Sopenharmony_ci   len_pos = size++;
154bf215546Sopenharmony_ci   size++;
155bf215546Sopenharmony_ci
156bf215546Sopenharmony_ci   for (i = 0; i < 2; ++i) {
157bf215546Sopenharmony_ci      int num = 0, j;
158bf215546Sopenharmony_ci
159bf215546Sopenharmony_ci      if (context->desc.mjpeg.huffman_table.load_huffman_table[i] == 0)
160bf215546Sopenharmony_ci         continue;
161bf215546Sopenharmony_ci
162bf215546Sopenharmony_ci      p[size++] = 0x00 | i;
163bf215546Sopenharmony_ci      memcpy((p + size), &context->desc.mjpeg.huffman_table.table[i].num_dc_codes, 16);
164bf215546Sopenharmony_ci      size += 16;
165bf215546Sopenharmony_ci      for (j = 0; j < 16; ++j)
166bf215546Sopenharmony_ci         num += context->desc.mjpeg.huffman_table.table[i].num_dc_codes[j];
167bf215546Sopenharmony_ci      assert(num <= 12);
168bf215546Sopenharmony_ci      memcpy((p + size), &context->desc.mjpeg.huffman_table.table[i].dc_values, num);
169bf215546Sopenharmony_ci      size += num;
170bf215546Sopenharmony_ci   }
171bf215546Sopenharmony_ci
172bf215546Sopenharmony_ci   for (i = 0; i < 2; ++i) {
173bf215546Sopenharmony_ci      int num = 0, j;
174bf215546Sopenharmony_ci
175bf215546Sopenharmony_ci      if (context->desc.mjpeg.huffman_table.load_huffman_table[i] == 0)
176bf215546Sopenharmony_ci         continue;
177bf215546Sopenharmony_ci
178bf215546Sopenharmony_ci      p[size++] = 0x10 | i;
179bf215546Sopenharmony_ci      memcpy((p + size), &context->desc.mjpeg.huffman_table.table[i].num_ac_codes, 16);
180bf215546Sopenharmony_ci      size += 16;
181bf215546Sopenharmony_ci      for (j = 0; j < 16; ++j)
182bf215546Sopenharmony_ci         num += context->desc.mjpeg.huffman_table.table[i].num_ac_codes[j];
183bf215546Sopenharmony_ci      assert(num <= 162);
184bf215546Sopenharmony_ci      memcpy((p + size), &context->desc.mjpeg.huffman_table.table[i].ac_values, num);
185bf215546Sopenharmony_ci      size += num;
186bf215546Sopenharmony_ci   }
187bf215546Sopenharmony_ci
188bf215546Sopenharmony_ci   bs = (uint16_t*)&p[len_pos];
189bf215546Sopenharmony_ci   *bs = util_bswap16(size - saved_size - 2);
190bf215546Sopenharmony_ci
191bf215546Sopenharmony_ci   saved_size = size;
192bf215546Sopenharmony_ci
193bf215546Sopenharmony_ci   /* DRI */
194bf215546Sopenharmony_ci   if (context->desc.mjpeg.slice_parameter.restart_interval) {
195bf215546Sopenharmony_ci      p[size++] = 0xff;
196bf215546Sopenharmony_ci      p[size++] = 0xdd;
197bf215546Sopenharmony_ci      p[size++] = 0x00;
198bf215546Sopenharmony_ci      p[size++] = 0x04;
199bf215546Sopenharmony_ci      bs = (uint16_t*)&p[size++];
200bf215546Sopenharmony_ci      *bs = util_bswap16(context->desc.mjpeg.slice_parameter.restart_interval);
201bf215546Sopenharmony_ci      saved_size = ++size;
202bf215546Sopenharmony_ci   }
203bf215546Sopenharmony_ci
204bf215546Sopenharmony_ci   /* SOF */
205bf215546Sopenharmony_ci   p[size++] = 0xff;
206bf215546Sopenharmony_ci   p[size++] = 0xc0;
207bf215546Sopenharmony_ci
208bf215546Sopenharmony_ci   len_pos = size++;
209bf215546Sopenharmony_ci   size++;
210bf215546Sopenharmony_ci
211bf215546Sopenharmony_ci   p[size++] = 0x08;
212bf215546Sopenharmony_ci
213bf215546Sopenharmony_ci   bs = (uint16_t*)&p[size++];
214bf215546Sopenharmony_ci   *bs = util_bswap16(context->desc.mjpeg.picture_parameter.picture_height);
215bf215546Sopenharmony_ci   size++;
216bf215546Sopenharmony_ci
217bf215546Sopenharmony_ci   bs = (uint16_t*)&p[size++];
218bf215546Sopenharmony_ci   *bs = util_bswap16(context->desc.mjpeg.picture_parameter.picture_width);
219bf215546Sopenharmony_ci   size++;
220bf215546Sopenharmony_ci
221bf215546Sopenharmony_ci   p[size++] = context->desc.mjpeg.picture_parameter.num_components;
222bf215546Sopenharmony_ci
223bf215546Sopenharmony_ci   for (i = 0; i < context->desc.mjpeg.picture_parameter.num_components; ++i) {
224bf215546Sopenharmony_ci      p[size++] = context->desc.mjpeg.picture_parameter.components[i].component_id;
225bf215546Sopenharmony_ci      p[size++] = context->desc.mjpeg.picture_parameter.components[i].h_sampling_factor << 4 |
226bf215546Sopenharmony_ci                 context->desc.mjpeg.picture_parameter.components[i].v_sampling_factor;
227bf215546Sopenharmony_ci      p[size++] = context->desc.mjpeg.picture_parameter.components[i].quantiser_table_selector;
228bf215546Sopenharmony_ci   }
229bf215546Sopenharmony_ci
230bf215546Sopenharmony_ci   bs = (uint16_t*)&p[len_pos];
231bf215546Sopenharmony_ci   *bs = util_bswap16(size - saved_size - 2);
232bf215546Sopenharmony_ci
233bf215546Sopenharmony_ci   saved_size = size;
234bf215546Sopenharmony_ci
235bf215546Sopenharmony_ci   /* SOS */
236bf215546Sopenharmony_ci   p[size++] = 0xff;
237bf215546Sopenharmony_ci   p[size++] = 0xda;
238bf215546Sopenharmony_ci
239bf215546Sopenharmony_ci   len_pos = size++;
240bf215546Sopenharmony_ci   size++;
241bf215546Sopenharmony_ci
242bf215546Sopenharmony_ci   p[size++] = context->desc.mjpeg.slice_parameter.num_components;
243bf215546Sopenharmony_ci
244bf215546Sopenharmony_ci   for (i = 0; i < context->desc.mjpeg.slice_parameter.num_components; ++i) {
245bf215546Sopenharmony_ci      p[size++] = context->desc.mjpeg.slice_parameter.components[i].component_selector;
246bf215546Sopenharmony_ci      p[size++] = context->desc.mjpeg.slice_parameter.components[i].dc_table_selector << 4 |
247bf215546Sopenharmony_ci                 context->desc.mjpeg.slice_parameter.components[i].ac_table_selector;
248bf215546Sopenharmony_ci   }
249bf215546Sopenharmony_ci
250bf215546Sopenharmony_ci   p[size++] = 0x00;
251bf215546Sopenharmony_ci   p[size++] = 0x3f;
252bf215546Sopenharmony_ci   p[size++] = 0x00;
253bf215546Sopenharmony_ci
254bf215546Sopenharmony_ci   bs = (uint16_t*)&p[len_pos];
255bf215546Sopenharmony_ci   *bs = util_bswap16(size - saved_size - 2);
256bf215546Sopenharmony_ci
257bf215546Sopenharmony_ci   context->mjpeg.slice_header_size = size;
258bf215546Sopenharmony_ci}
259