st/nine: Refactor how user constbufs sizes are calculated
[mesa.git] / src / gallium / state_trackers / omx / vid_dec_mpeg12.c
1 /**************************************************************************
2 *
3 * Copyright 2013 Advanced Micro Devices, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Christian König <christian.koenig@amd.com>
31 *
32 */
33
34 #include "pipe/p_video_codec.h"
35 #include "vl/vl_vlc.h"
36 #include "vl/vl_zscan.h"
37
38 #include "vid_dec.h"
39
40 static uint8_t default_intra_matrix[64] = {
41 8, 16, 19, 22, 26, 27, 29, 34,
42 16, 16, 19, 22, 22, 22, 22, 26,
43 26, 27, 22, 26, 26, 27, 29, 24,
44 27, 27, 29, 32, 27, 29, 29, 32,
45 35, 29, 34, 34, 35, 40, 34, 34,
46 37, 40, 48, 37, 38, 40, 48, 58,
47 26, 27, 29, 34, 38, 46, 56, 69,
48 27, 29, 35, 38, 46, 56, 69, 83
49 };
50
51 static uint8_t default_non_intra_matrix[64] = {
52 16, 16, 16, 16, 16, 16, 16, 16,
53 16, 16, 16, 16, 16, 16, 16, 16,
54 16, 16, 16, 16, 16, 16, 16, 16,
55 16, 16, 16, 16, 16, 16, 16, 16,
56 16, 16, 16, 16, 16, 16, 16, 16,
57 16, 16, 16, 16, 16, 16, 16, 16,
58 16, 16, 16, 16, 16, 16, 16, 16,
59 16, 16, 16, 16, 16, 16, 16, 16
60 };
61
62 static void vid_dec_mpeg12_Decode(vid_dec_PrivateType *priv, struct vl_vlc *vlc, unsigned min_bits_left);
63 static void vid_dec_mpeg12_EndFrame(vid_dec_PrivateType *priv);
64 static struct pipe_video_buffer *vid_dec_mpeg12_Flush(vid_dec_PrivateType *priv);
65
66 void vid_dec_mpeg12_Init(vid_dec_PrivateType *priv)
67 {
68 priv->picture.base.profile = PIPE_VIDEO_PROFILE_MPEG2_MAIN;
69 priv->picture.mpeg12.intra_matrix = default_intra_matrix;
70 priv->picture.mpeg12.non_intra_matrix = default_non_intra_matrix;
71
72 priv->Decode = vid_dec_mpeg12_Decode;
73 priv->EndFrame = vid_dec_mpeg12_EndFrame;
74 priv->Flush = vid_dec_mpeg12_Flush;
75 }
76
77 static void BeginFrame(vid_dec_PrivateType *priv)
78 {
79 if (priv->picture.mpeg12.picture_coding_type != PIPE_MPEG12_PICTURE_CODING_TYPE_B) {
80 priv->picture.mpeg12.ref[0] = priv->picture.mpeg12.ref[1];
81 priv->picture.mpeg12.ref[1] = NULL;
82 }
83
84 if (priv->target == priv->picture.mpeg12.ref[0]) {
85 struct pipe_video_buffer *tmp = priv->target;
86 priv->target = priv->shadow;
87 priv->shadow = tmp;
88 }
89
90 vid_dec_NeedTarget(priv);
91
92 priv->codec->begin_frame(priv->codec, priv->target, &priv->picture.base);
93 priv->frame_started = true;
94 }
95
96 static void vid_dec_mpeg12_EndFrame(vid_dec_PrivateType *priv)
97 {
98 struct pipe_video_buffer *done;
99
100 priv->codec->end_frame(priv->codec, priv->target, &priv->picture.base);
101 priv->frame_started = false;
102
103 if (priv->picture.mpeg12.picture_coding_type != PIPE_MPEG12_PICTURE_CODING_TYPE_B) {
104
105 priv->picture.mpeg12.ref[1] = priv->target;
106 done = priv->picture.mpeg12.ref[0];
107 if (!done) {
108 priv->target = NULL;
109 return;
110 }
111
112 } else
113 done = priv->target;
114
115 priv->frame_finished = true;
116 priv->target = priv->in_buffers[0]->pInputPortPrivate;
117 priv->in_buffers[0]->pInputPortPrivate = done;
118 }
119
120 static struct pipe_video_buffer *vid_dec_mpeg12_Flush(vid_dec_PrivateType *priv)
121 {
122 struct pipe_video_buffer *result = priv->picture.mpeg12.ref[1];
123 priv->picture.mpeg12.ref[1] = NULL;
124 return result;
125 }
126
127 static void vid_dec_mpeg12_Decode(vid_dec_PrivateType *priv, struct vl_vlc *vlc, unsigned min_bits_left)
128 {
129 uint8_t code;
130 unsigned i;
131
132 if (!vl_vlc_search_byte(vlc, vl_vlc_bits_left(vlc) - min_bits_left, 0x00))
133 return;
134
135 if (vl_vlc_peekbits(vlc, 24) != 0x000001) {
136 vl_vlc_eatbits(vlc, 8);
137 return;
138 }
139
140 if (priv->slice) {
141 unsigned bytes = priv->bytes_left - (vl_vlc_bits_left(vlc) / 8);
142 priv->codec->decode_bitstream(priv->codec, priv->target, &priv->picture.base,
143 1, &priv->slice, &bytes);
144 priv->slice = NULL;
145 }
146
147 vl_vlc_eatbits(vlc, 24);
148 code = vl_vlc_get_uimsbf(vlc, 8);
149
150 if (priv->frame_started && (code == 0x00 || code > 0xAF))
151 vid_dec_mpeg12_EndFrame(priv);
152
153 if (code == 0xB3) {
154 /* sequence header code */
155 vl_vlc_fillbits(vlc);
156
157 /* horizontal_size_value */
158 vl_vlc_get_uimsbf(vlc, 12);
159
160 /* vertical_size_value */
161 vl_vlc_get_uimsbf(vlc, 12);
162
163 /* aspect_ratio_information */
164 vl_vlc_get_uimsbf(vlc, 4);
165
166 /* frame_rate_code */
167 vl_vlc_get_uimsbf(vlc, 4);
168
169 vl_vlc_fillbits(vlc);
170
171 /* bit_rate_value */
172 vl_vlc_get_uimsbf(vlc, 18);
173
174 /* marker_bit */
175 vl_vlc_get_uimsbf(vlc, 1);
176
177 /* vbv_buffer_size_value */
178 vl_vlc_get_uimsbf(vlc, 10);
179
180 /* constrained_parameters_flag */
181 vl_vlc_get_uimsbf(vlc, 1);
182
183 vl_vlc_fillbits(vlc);
184
185 /* load_intra_quantiser_matrix */
186 if (vl_vlc_get_uimsbf(vlc, 1)) {
187 /* intra_quantiser_matrix */
188 priv->picture.mpeg12.intra_matrix = priv->codec_data.mpeg12.intra_matrix;
189 for (i = 0; i < 64; ++i) {
190 priv->codec_data.mpeg12.intra_matrix[vl_zscan_normal[i]] = vl_vlc_get_uimsbf(vlc, 8);
191 vl_vlc_fillbits(vlc);
192 }
193 } else
194 priv->picture.mpeg12.intra_matrix = default_intra_matrix;
195
196 /* load_non_intra_quantiser_matrix */
197 if (vl_vlc_get_uimsbf(vlc, 1)) {
198 /* non_intra_quantiser_matrix */
199 priv->picture.mpeg12.non_intra_matrix = priv->codec_data.mpeg12.non_intra_matrix;
200 for (i = 0; i < 64; ++i) {
201 priv->codec_data.mpeg12.non_intra_matrix[i] = vl_vlc_get_uimsbf(vlc, 8);
202 vl_vlc_fillbits(vlc);
203 }
204 } else
205 priv->picture.mpeg12.non_intra_matrix = default_non_intra_matrix;
206
207 } else if (code == 0x00) {
208 /* picture start code */
209 vl_vlc_fillbits(vlc);
210
211 /* temporal_reference */
212 vl_vlc_get_uimsbf(vlc, 10);
213
214 priv->picture.mpeg12.picture_coding_type = vl_vlc_get_uimsbf(vlc, 3);
215
216 /* vbv_delay */
217 vl_vlc_get_uimsbf(vlc, 16);
218
219 vl_vlc_fillbits(vlc);
220 if (priv->picture.mpeg12.picture_coding_type == 2 ||
221 priv->picture.mpeg12.picture_coding_type == 3) {
222 priv->picture.mpeg12.full_pel_forward_vector = vl_vlc_get_uimsbf(vlc, 1);
223 /* forward_f_code */
224 priv->picture.mpeg12.f_code[0][0] = vl_vlc_get_uimsbf(vlc, 3) - 1;
225 priv->picture.mpeg12.f_code[0][1] = priv->picture.mpeg12.f_code[0][0];
226 } else {
227 priv->picture.mpeg12.full_pel_forward_vector = 0;
228 priv->picture.mpeg12.f_code[0][1] = priv->picture.mpeg12.f_code[0][0] = 14;
229 }
230
231 if (priv->picture.mpeg12.picture_coding_type == 3) {
232 priv->picture.mpeg12.full_pel_backward_vector = vl_vlc_get_uimsbf(vlc, 1);
233 /* backward_f_code */
234 priv->picture.mpeg12.f_code[1][0] = vl_vlc_get_uimsbf(vlc, 3) - 1;
235 priv->picture.mpeg12.f_code[1][1] = priv->picture.mpeg12.f_code[1][0];
236 } else {
237 priv->picture.mpeg12.full_pel_backward_vector = 0;
238 priv->picture.mpeg12.f_code[0][1] = priv->picture.mpeg12.f_code[0][0] = 14;
239 }
240
241 /* extra_bit_picture */
242 while (vl_vlc_get_uimsbf(vlc, 1)) {
243 /* extra_information_picture */
244 vl_vlc_get_uimsbf(vlc, 8);
245 vl_vlc_fillbits(vlc);
246 }
247
248 } else if (code == 0xB5) {
249 /* extension start code */
250 vl_vlc_fillbits(vlc);
251
252 /* extension_start_code_identifier */
253 switch (vl_vlc_get_uimsbf(vlc, 4)) {
254 case 0x3: /* quant matrix extension */
255
256 /* load_intra_quantiser_matrix */
257 if (vl_vlc_get_uimsbf(vlc, 1)) {
258 /* intra_quantiser_matrix */
259 priv->picture.mpeg12.intra_matrix = priv->codec_data.mpeg12.intra_matrix;
260 for (i = 0; i < 64; ++i) {
261 priv->codec_data.mpeg12.intra_matrix[vl_zscan_normal[i]] = vl_vlc_get_uimsbf(vlc, 8);
262 vl_vlc_fillbits(vlc);
263 }
264 } else
265 priv->picture.mpeg12.intra_matrix = default_intra_matrix;
266
267 /* load_non_intra_quantiser_matrix */
268 if (vl_vlc_get_uimsbf(vlc, 1)) {
269 /* non_intra_quantiser_matrix */
270 priv->picture.mpeg12.non_intra_matrix = priv->codec_data.mpeg12.non_intra_matrix;
271 for (i = 0; i < 64; ++i) {
272 priv->codec_data.mpeg12.non_intra_matrix[i] = vl_vlc_get_uimsbf(vlc, 8);
273 vl_vlc_fillbits(vlc);
274 }
275 } else
276 priv->picture.mpeg12.intra_matrix = default_non_intra_matrix;
277
278 break;
279
280 case 0x8: /* picture coding extension */
281
282 priv->picture.mpeg12.f_code[0][0] = vl_vlc_get_uimsbf(vlc, 4) - 1;
283 priv->picture.mpeg12.f_code[0][1] = vl_vlc_get_uimsbf(vlc, 4) - 1;
284 priv->picture.mpeg12.f_code[1][0] = vl_vlc_get_uimsbf(vlc, 4) - 1;
285 priv->picture.mpeg12.f_code[1][1] = vl_vlc_get_uimsbf(vlc, 4) - 1;
286 priv->picture.mpeg12.intra_dc_precision = vl_vlc_get_uimsbf(vlc, 2);
287 priv->picture.mpeg12.picture_structure = vl_vlc_get_uimsbf(vlc, 2);
288 priv->picture.mpeg12.top_field_first = vl_vlc_get_uimsbf(vlc, 1);
289 priv->picture.mpeg12.frame_pred_frame_dct = vl_vlc_get_uimsbf(vlc, 1);
290 priv->picture.mpeg12.concealment_motion_vectors = vl_vlc_get_uimsbf(vlc, 1);
291 priv->picture.mpeg12.q_scale_type = vl_vlc_get_uimsbf(vlc, 1);
292 priv->picture.mpeg12.intra_vlc_format = vl_vlc_get_uimsbf(vlc, 1);
293 priv->picture.mpeg12.alternate_scan = vl_vlc_get_uimsbf(vlc, 1);
294
295 /* repeat_first_field */
296 vl_vlc_get_uimsbf(vlc, 1);
297
298 /* chroma_420_type */
299 vl_vlc_get_uimsbf(vlc, 1);
300
301 vl_vlc_fillbits(vlc);
302
303 /* progressive_frame */
304 vl_vlc_get_uimsbf(vlc, 1);
305
306 /* composite_display_flag */
307 if (vl_vlc_get_uimsbf(vlc, 1)) {
308
309 /* v_axis */
310 vl_vlc_get_uimsbf(vlc, 1);
311
312 /* field_sequence */
313 vl_vlc_get_uimsbf(vlc, 3);
314
315 /* sub_carrier */
316 vl_vlc_get_uimsbf(vlc, 1);
317
318 /* burst_amplitude */
319 vl_vlc_get_uimsbf(vlc, 7);
320
321 /* sub_carrier_phase */
322 vl_vlc_get_uimsbf(vlc, 8);
323 }
324 break;
325 }
326
327 } else if (code <= 0xAF) {
328 /* slice start */
329 unsigned bytes = (vl_vlc_valid_bits(vlc) / 8) + 4;
330 uint8_t buf[12];
331 const void *ptr = buf;
332 unsigned i;
333
334 if (!priv->frame_started)
335 BeginFrame(priv);
336
337 buf[0] = 0x00;
338 buf[1] = 0x00;
339 buf[2] = 0x01;
340 buf[3] = code;
341 for (i = 4; i < bytes; ++i)
342 buf[i] = vl_vlc_get_uimsbf(vlc, 8);
343
344 priv->codec->decode_bitstream(priv->codec, priv->target, &priv->picture.base,
345 1, &ptr, &bytes);
346
347 priv->bytes_left = vl_vlc_bits_left(vlc) / 8;
348 priv->slice = vlc->data;
349
350 } else if (code == 0xB2) {
351 /* user data start */
352
353 } else if (code == 0xB4) {
354 /* sequence error */
355 } else if (code == 0xB7) {
356 /* sequence end */
357 } else if (code == 0xB8) {
358 /* group start */
359 } else if (code >= 0xB9) {
360 /* system start */
361 } else {
362 /* reserved */
363 }
364
365 /* resync to byte boundary */
366 vl_vlc_eatbits(vlc, vl_vlc_valid_bits(vlc) % 8);
367 }