st/omx/dec/h264: fix corruption when scaling matrix present flag set
[mesa.git] / src / gallium / state_trackers / omx / vid_dec_h264.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 "util/u_memory.h"
36 #include "util/u_video.h"
37 #include "vl/vl_rbsp.h"
38 #include "vl/vl_zscan.h"
39
40 #include "entrypoint.h"
41 #include "vid_dec.h"
42
43 #define DPB_MAX_SIZE 5
44
45 struct dpb_list {
46 struct list_head list;
47 struct pipe_video_buffer *buffer;
48 unsigned poc;
49 };
50
51 static const uint8_t Default_4x4_Intra[16] = {
52 6, 13, 13, 20, 20, 20, 28, 28,
53 28, 28, 32, 32, 32, 37, 37, 42
54 };
55
56 static const uint8_t Default_4x4_Inter[16] = {
57 10, 14, 14, 20, 20, 20, 24, 24,
58 24, 24, 27, 27, 27, 30, 30, 34
59 };
60
61 static const uint8_t Default_8x8_Intra[64] = {
62 6, 10, 10, 13, 11, 13, 16, 16,
63 16, 16, 18, 18, 18, 18, 18, 23,
64 23, 23, 23, 23, 23, 25, 25, 25,
65 25, 25, 25, 25, 27, 27, 27, 27,
66 27, 27, 27, 27, 29, 29, 29, 29,
67 29, 29, 29, 31, 31, 31, 31, 31,
68 31, 33, 33, 33, 33, 33, 36, 36,
69 36, 36, 38, 38, 38, 40, 40, 42
70 };
71
72 static const uint8_t Default_8x8_Inter[64] = {
73 9, 13, 13, 15, 13, 15, 17, 17,
74 17, 17, 19, 19, 19, 19, 19, 21,
75 21, 21, 21, 21, 21, 22, 22, 22,
76 22, 22, 22, 22, 24, 24, 24, 24,
77 24, 24, 24, 24, 25, 25, 25, 25,
78 25, 25, 25, 27, 27, 27, 27, 27,
79 27, 28, 28, 28, 28, 28, 30, 30,
80 30, 30, 32, 32, 32, 33, 33, 35
81 };
82
83 static void vid_dec_h264_Decode(vid_dec_PrivateType *priv, struct vl_vlc *vlc, unsigned min_bits_left);
84 static void vid_dec_h264_EndFrame(vid_dec_PrivateType *priv);
85 static struct pipe_video_buffer *vid_dec_h264_Flush(vid_dec_PrivateType *priv);
86
87 void vid_dec_h264_Init(vid_dec_PrivateType *priv)
88 {
89 priv->picture.base.profile = PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH;
90
91 priv->Decode = vid_dec_h264_Decode;
92 priv->EndFrame = vid_dec_h264_EndFrame;
93 priv->Flush = vid_dec_h264_Flush;
94
95 LIST_INITHEAD(&priv->codec_data.h264.dpb_list);
96 priv->picture.h264.field_order_cnt[0] = priv->picture.h264.field_order_cnt[1] = INT_MAX;
97 }
98
99 static void vid_dec_h264_BeginFrame(vid_dec_PrivateType *priv)
100 {
101 //TODO: sane buffer handling
102
103 if (priv->frame_started)
104 return;
105
106 vid_dec_NeedTarget(priv);
107
108 priv->picture.h264.num_ref_frames = priv->picture.h264.pps->sps->max_num_ref_frames;
109
110 if (!priv->codec) {
111 struct pipe_video_codec templat = {};
112 omx_base_video_PortType *port;
113
114 port = (omx_base_video_PortType *)priv->ports[OMX_BASE_FILTER_INPUTPORT_INDEX];
115 templat.profile = priv->profile;
116 templat.entrypoint = PIPE_VIDEO_ENTRYPOINT_BITSTREAM;
117 templat.chroma_format = PIPE_VIDEO_CHROMA_FORMAT_420;
118 templat.max_references = priv->picture.h264.num_ref_frames;
119 templat.expect_chunked_decode = true;
120 templat.width = port->sPortParam.format.video.nFrameWidth;
121 templat.height = port->sPortParam.format.video.nFrameHeight;
122 templat.level = priv->picture.h264.pps->sps->level_idc;
123
124 priv->codec = priv->pipe->create_video_codec(priv->pipe, &templat);
125 }
126 priv->codec->begin_frame(priv->codec, priv->target, &priv->picture.base);
127 priv->frame_started = true;
128 }
129
130 static struct pipe_video_buffer *vid_dec_h264_Flush(vid_dec_PrivateType *priv)
131 {
132 struct dpb_list *entry, *result = NULL;
133 struct pipe_video_buffer *buf;
134
135 /* search for the lowest poc and break on zeros */
136 LIST_FOR_EACH_ENTRY(entry, &priv->codec_data.h264.dpb_list, list) {
137
138 if (result && entry->poc == 0)
139 break;
140
141 if (!result || entry->poc < result->poc)
142 result = entry;
143 }
144
145 if (!result)
146 return NULL;
147
148 buf = result->buffer;
149
150 --priv->codec_data.h264.dpb_num;
151 LIST_DEL(&result->list);
152 FREE(result);
153
154 return buf;
155 }
156
157 static void vid_dec_h264_EndFrame(vid_dec_PrivateType *priv)
158 {
159 struct dpb_list *entry;
160 struct pipe_video_buffer *tmp;
161 bool top_field_first;
162
163 if (!priv->frame_started)
164 return;
165
166 priv->codec->end_frame(priv->codec, priv->target, &priv->picture.base);
167 priv->frame_started = false;
168
169 // TODO: implement frame number handling
170 priv->picture.h264.frame_num_list[0] = priv->picture.h264.frame_num;
171 priv->picture.h264.field_order_cnt_list[0][0] = priv->picture.h264.frame_num;
172 priv->picture.h264.field_order_cnt_list[0][1] = priv->picture.h264.frame_num;
173
174 top_field_first = priv->picture.h264.field_order_cnt[0] < priv->picture.h264.field_order_cnt[1];
175
176 if (priv->picture.h264.field_pic_flag && priv->picture.h264.bottom_field_flag != top_field_first)
177 return;
178
179 /* add the decoded picture to the dpb list */
180 entry = CALLOC_STRUCT(dpb_list);
181 if (!entry)
182 return;
183
184 entry->buffer = priv->target;
185 entry->poc = MIN2(priv->picture.h264.field_order_cnt[0], priv->picture.h264.field_order_cnt[1]);
186 LIST_ADDTAIL(&entry->list, &priv->codec_data.h264.dpb_list);
187 ++priv->codec_data.h264.dpb_num;
188 priv->target = NULL;
189 priv->picture.h264.field_order_cnt[0] = priv->picture.h264.field_order_cnt[1] = INT_MAX;
190
191 if (priv->codec_data.h264.dpb_num <= DPB_MAX_SIZE)
192 return;
193
194 tmp = priv->in_buffers[0]->pInputPortPrivate;
195 priv->in_buffers[0]->pInputPortPrivate = vid_dec_h264_Flush(priv);
196 priv->target = tmp;
197 priv->frame_finished = priv->in_buffers[0]->pInputPortPrivate != NULL;
198 }
199
200 static void vui_parameters(struct vl_rbsp *rbsp)
201 {
202 // TODO
203 }
204
205 static void scaling_list(struct vl_rbsp *rbsp, uint8_t *scalingList, unsigned sizeOfScalingList,
206 const uint8_t *defaultList, const uint8_t *fallbackList)
207 {
208 unsigned lastScale = 8, nextScale = 8;
209 const int *list;
210 unsigned i;
211
212 /* (pic|seq)_scaling_list_present_flag[i] */
213 if (!vl_rbsp_u(rbsp, 1)) {
214 if (fallbackList)
215 memcpy(scalingList, fallbackList, sizeOfScalingList);
216 return;
217 }
218
219 list = (sizeOfScalingList == 16) ? vl_zscan_normal_16 : vl_zscan_normal;
220 for (i = 0; i < sizeOfScalingList; ++i ) {
221
222 if (nextScale != 0) {
223 signed delta_scale = vl_rbsp_se(rbsp);
224 nextScale = (lastScale + delta_scale + 256) % 256;
225 if (i == 0 && nextScale == 0) {
226 memcpy(scalingList, defaultList, sizeOfScalingList);
227 return;
228 }
229 }
230 scalingList[list[i]] = nextScale == 0 ? lastScale : nextScale;
231 lastScale = scalingList[list[i]];
232 }
233 }
234
235 static struct pipe_h264_sps *seq_parameter_set_id(vid_dec_PrivateType *priv, struct vl_rbsp *rbsp)
236 {
237 unsigned id = vl_rbsp_ue(rbsp);
238 if (id >= Elements(priv->codec_data.h264.sps))
239 return NULL; /* invalid seq_parameter_set_id */
240
241 return &priv->codec_data.h264.sps[id];
242 }
243
244 static void seq_parameter_set(vid_dec_PrivateType *priv, struct vl_rbsp *rbsp)
245 {
246 struct pipe_h264_sps *sps;
247 unsigned profile_idc, level_idc;
248 unsigned i;
249
250 /* Sequence parameter set */
251 profile_idc = vl_rbsp_u(rbsp, 8);
252
253 /* constraint_set0_flag */
254 vl_rbsp_u(rbsp, 1);
255
256 /* constraint_set1_flag */
257 vl_rbsp_u(rbsp, 1);
258
259 /* constraint_set2_flag */
260 vl_rbsp_u(rbsp, 1);
261
262 /* constraint_set3_flag */
263 vl_rbsp_u(rbsp, 1);
264
265 /* constraint_set4_flag */
266 vl_rbsp_u(rbsp, 1);
267
268 /* constraint_set5_flag */
269 vl_rbsp_u(rbsp, 1);
270
271 /* reserved_zero_2bits */
272 vl_rbsp_u(rbsp, 2);
273
274 /* level_idc */
275 level_idc = vl_rbsp_u(rbsp, 8);
276
277 sps = seq_parameter_set_id(priv, rbsp);
278 if (!sps)
279 return;
280
281 memset(sps, 0, sizeof(*sps));
282 memset(sps->ScalingList4x4, 16, sizeof(sps->ScalingList4x4));
283 memset(sps->ScalingList8x8, 16, sizeof(sps->ScalingList8x8));
284
285 sps->level_idc = level_idc;
286
287 if (profile_idc == 100 || profile_idc == 110 || profile_idc == 122 || profile_idc == 244 ||
288 profile_idc == 44 || profile_idc == 83 || profile_idc == 86 || profile_idc == 118 ||
289 profile_idc == 128 || profile_idc == 138) {
290
291 sps->chroma_format_idc = vl_rbsp_ue(rbsp);
292
293 if (sps->chroma_format_idc == 3)
294 sps->separate_colour_plane_flag = vl_rbsp_u(rbsp, 1);
295
296 sps->bit_depth_luma_minus8 = vl_rbsp_ue(rbsp);
297
298 sps->bit_depth_chroma_minus8 = vl_rbsp_ue(rbsp);
299
300 /* qpprime_y_zero_transform_bypass_flag */
301 vl_rbsp_u(rbsp, 1);
302
303 sps->seq_scaling_matrix_present_flag = vl_rbsp_u(rbsp, 1);
304 if (sps->seq_scaling_matrix_present_flag) {
305
306 scaling_list(rbsp, sps->ScalingList4x4[0], 16, Default_4x4_Intra, Default_4x4_Intra);
307 scaling_list(rbsp, sps->ScalingList4x4[1], 16, Default_4x4_Intra, sps->ScalingList4x4[0]);
308 scaling_list(rbsp, sps->ScalingList4x4[2], 16, Default_4x4_Intra, sps->ScalingList4x4[1]);
309 scaling_list(rbsp, sps->ScalingList4x4[3], 16, Default_4x4_Inter, Default_4x4_Inter);
310 scaling_list(rbsp, sps->ScalingList4x4[4], 16, Default_4x4_Inter, sps->ScalingList4x4[3]);
311 scaling_list(rbsp, sps->ScalingList4x4[5], 16, Default_4x4_Inter, sps->ScalingList4x4[4]);
312
313 scaling_list(rbsp, sps->ScalingList8x8[0], 64, Default_8x8_Intra, Default_8x8_Intra);
314 scaling_list(rbsp, sps->ScalingList8x8[1], 64, Default_8x8_Inter, Default_8x8_Inter);
315 if (sps->chroma_format_idc == 3) {
316 scaling_list(rbsp, sps->ScalingList8x8[2], 64, Default_8x8_Intra, sps->ScalingList8x8[0]);
317 scaling_list(rbsp, sps->ScalingList8x8[3], 64, Default_8x8_Inter, sps->ScalingList8x8[1]);
318 scaling_list(rbsp, sps->ScalingList8x8[4], 64, Default_8x8_Intra, sps->ScalingList8x8[2]);
319 scaling_list(rbsp, sps->ScalingList8x8[5], 64, Default_8x8_Inter, sps->ScalingList8x8[3]);
320 }
321 }
322 } else if (profile_idc == 183)
323 sps->chroma_format_idc = 0;
324 else
325 sps->chroma_format_idc = 1;
326
327 sps->log2_max_frame_num_minus4 = vl_rbsp_ue(rbsp);
328
329 sps->pic_order_cnt_type = vl_rbsp_ue(rbsp);
330
331 if (sps->pic_order_cnt_type == 0)
332 sps->log2_max_pic_order_cnt_lsb_minus4 = vl_rbsp_ue(rbsp);
333 else if (sps->pic_order_cnt_type == 1) {
334 sps->delta_pic_order_always_zero_flag = vl_rbsp_u(rbsp, 1);
335
336 sps->offset_for_non_ref_pic = vl_rbsp_se(rbsp);
337
338 sps->offset_for_top_to_bottom_field = vl_rbsp_se(rbsp);
339
340 sps->num_ref_frames_in_pic_order_cnt_cycle = vl_rbsp_ue(rbsp);
341
342 for (i = 0; i < sps->num_ref_frames_in_pic_order_cnt_cycle; ++i)
343 sps->offset_for_ref_frame[i] = vl_rbsp_se(rbsp);
344 }
345
346 sps->max_num_ref_frames = vl_rbsp_ue(rbsp);
347
348 /* gaps_in_frame_num_value_allowed_flag */
349 vl_rbsp_u(rbsp, 1);
350
351 /* pic_width_in_mbs_minus1 */
352 vl_rbsp_ue(rbsp);
353
354 /* pic_height_in_map_units_minus1 */
355 vl_rbsp_ue(rbsp);
356
357 sps->frame_mbs_only_flag = vl_rbsp_u(rbsp, 1);
358 if (!sps->frame_mbs_only_flag)
359 sps->mb_adaptive_frame_field_flag = vl_rbsp_u(rbsp, 1);
360
361 sps->direct_8x8_inference_flag = vl_rbsp_u(rbsp, 1);
362
363 /* frame_cropping_flag */
364 if (vl_rbsp_u(rbsp, 1)) {
365 /* frame_crop_left_offset */
366 vl_rbsp_ue(rbsp);
367
368 /* frame_crop_right_offset */
369 vl_rbsp_ue(rbsp);
370
371 /* frame_crop_top_offset */
372 vl_rbsp_ue(rbsp);
373
374 /* frame_crop_bottom_offset */
375 vl_rbsp_ue(rbsp);
376 }
377
378 /* vui_parameters_present_flag */
379 if (vl_rbsp_u(rbsp, 1))
380 vui_parameters(rbsp);
381 }
382
383 static struct pipe_h264_pps *pic_parameter_set_id(vid_dec_PrivateType *priv, struct vl_rbsp *rbsp)
384 {
385 unsigned id = vl_rbsp_ue(rbsp);
386 if (id >= Elements(priv->codec_data.h264.pps))
387 return NULL; /* invalid pic_parameter_set_id */
388
389 return &priv->codec_data.h264.pps[id];
390 }
391
392 static void picture_parameter_set(vid_dec_PrivateType *priv, struct vl_rbsp *rbsp)
393 {
394 struct pipe_h264_sps *sps;
395 struct pipe_h264_pps *pps;
396 unsigned i;
397
398 pps = pic_parameter_set_id(priv, rbsp);
399 if (!pps)
400 return;
401
402 memset(pps, 0, sizeof(*pps));
403
404 sps = pps->sps = seq_parameter_set_id(priv, rbsp);
405 if (!sps)
406 return;
407
408 memcpy(pps->ScalingList4x4, sps->ScalingList4x4, sizeof(pps->ScalingList4x4));
409 memcpy(pps->ScalingList8x8, sps->ScalingList8x8, sizeof(pps->ScalingList8x8));
410
411 pps->entropy_coding_mode_flag = vl_rbsp_u(rbsp, 1);
412
413 pps->bottom_field_pic_order_in_frame_present_flag = vl_rbsp_u(rbsp, 1);
414
415 pps->num_slice_groups_minus1 = vl_rbsp_ue(rbsp);
416 if (pps->num_slice_groups_minus1 > 0) {
417 pps->slice_group_map_type = vl_rbsp_ue(rbsp);
418
419 if (pps->slice_group_map_type == 0) {
420
421 for (i = 0; i <= pps->num_slice_groups_minus1; ++i)
422 /* run_length_minus1[i] */
423 vl_rbsp_ue(rbsp);
424
425 } else if (pps->slice_group_map_type == 2) {
426
427 for (i = 0; i <= pps->num_slice_groups_minus1; ++i) {
428 /* top_left[i] */
429 vl_rbsp_ue(rbsp);
430
431 /* bottom_right[i] */
432 vl_rbsp_ue(rbsp);
433 }
434
435 } else if (pps->slice_group_map_type >= 3 && pps->slice_group_map_type <= 5) {
436
437 /* slice_group_change_direction_flag */
438 vl_rbsp_u(rbsp, 1);
439
440 pps->slice_group_change_rate_minus1 = vl_rbsp_ue(rbsp);
441
442 } else if (pps->slice_group_map_type == 6) {
443
444 unsigned pic_size_in_map_units_minus1;
445
446 pic_size_in_map_units_minus1 = vl_rbsp_ue(rbsp);
447
448 for (i = 0; i <= pic_size_in_map_units_minus1; ++i)
449 /* slice_group_id[i] */
450 vl_rbsp_u(rbsp, log2(pps->num_slice_groups_minus1 + 1));
451 }
452 }
453
454 pps->num_ref_idx_l0_default_active_minus1 = vl_rbsp_ue(rbsp);
455
456 pps->num_ref_idx_l1_default_active_minus1 = vl_rbsp_ue(rbsp);
457
458 pps->weighted_pred_flag = vl_rbsp_u(rbsp, 1);
459
460 pps->weighted_bipred_idc = vl_rbsp_u(rbsp, 2);
461
462 pps->pic_init_qp_minus26 = vl_rbsp_se(rbsp);
463
464 /* pic_init_qs_minus26 */
465 vl_rbsp_se(rbsp);
466
467 pps->chroma_qp_index_offset = vl_rbsp_se(rbsp);
468
469 pps->deblocking_filter_control_present_flag = vl_rbsp_u(rbsp, 1);
470
471 pps->constrained_intra_pred_flag = vl_rbsp_u(rbsp, 1);
472
473 pps->redundant_pic_cnt_present_flag = vl_rbsp_u(rbsp, 1);
474
475 if (vl_rbsp_more_data(rbsp)) {
476 pps->transform_8x8_mode_flag = vl_rbsp_u(rbsp, 1);
477
478 /* pic_scaling_matrix_present_flag */
479 if (vl_rbsp_u(rbsp, 1)) {
480
481 scaling_list(rbsp, pps->ScalingList4x4[0], 16, Default_4x4_Intra,
482 sps->seq_scaling_matrix_present_flag ? NULL : Default_4x4_Intra);
483 scaling_list(rbsp, pps->ScalingList4x4[1], 16, Default_4x4_Intra, pps->ScalingList4x4[0]);
484 scaling_list(rbsp, pps->ScalingList4x4[2], 16, Default_4x4_Intra, pps->ScalingList4x4[1]);
485 scaling_list(rbsp, pps->ScalingList4x4[3], 16, Default_4x4_Inter,
486 sps->seq_scaling_matrix_present_flag ? NULL : Default_4x4_Inter);
487 scaling_list(rbsp, pps->ScalingList4x4[4], 16, Default_4x4_Inter, pps->ScalingList4x4[3]);
488 scaling_list(rbsp, pps->ScalingList4x4[5], 16, Default_4x4_Inter, pps->ScalingList4x4[4]);
489
490 if (pps->transform_8x8_mode_flag) {
491 scaling_list(rbsp, pps->ScalingList8x8[0], 64, Default_8x8_Intra,
492 sps->seq_scaling_matrix_present_flag ? NULL : Default_8x8_Intra);
493 scaling_list(rbsp, pps->ScalingList8x8[1], 64, Default_8x8_Inter,
494 sps->seq_scaling_matrix_present_flag ? NULL : Default_8x8_Inter);
495 if (sps->chroma_format_idc == 3) {
496 scaling_list(rbsp, pps->ScalingList8x8[2], 64, Default_8x8_Intra, pps->ScalingList8x8[0]);
497 scaling_list(rbsp, pps->ScalingList8x8[3], 64, Default_8x8_Inter, pps->ScalingList8x8[1]);
498 scaling_list(rbsp, pps->ScalingList8x8[4], 64, Default_8x8_Intra, pps->ScalingList8x8[2]);
499 scaling_list(rbsp, pps->ScalingList8x8[5], 64, Default_8x8_Inter, pps->ScalingList8x8[3]);
500 }
501 }
502 }
503
504 pps->second_chroma_qp_index_offset = vl_rbsp_se(rbsp);
505 }
506 }
507
508 static void ref_pic_list_mvc_modification(vid_dec_PrivateType *priv, struct vl_rbsp *rbsp)
509 {
510 // TODO
511 assert(0);
512 }
513
514 static void ref_pic_list_modification(vid_dec_PrivateType *priv, struct vl_rbsp *rbsp,
515 enum pipe_h264_slice_type slice_type)
516 {
517 unsigned modification_of_pic_nums_idc;
518
519 if (slice_type != 2 && slice_type != 4) {
520 /* ref_pic_list_modification_flag_l0 */
521 if (vl_rbsp_u(rbsp, 1)) {
522 do {
523 modification_of_pic_nums_idc = vl_rbsp_ue(rbsp);
524 if (modification_of_pic_nums_idc == 0 ||
525 modification_of_pic_nums_idc == 1)
526 /* abs_diff_pic_num_minus1 */
527 vl_rbsp_ue(rbsp);
528 else if (modification_of_pic_nums_idc == 2)
529 /* long_term_pic_num */
530 vl_rbsp_ue(rbsp);
531 } while (modification_of_pic_nums_idc != 3);
532 }
533 }
534
535 if (slice_type == 1) {
536 /* ref_pic_list_modification_flag_l1 */
537 if (vl_rbsp_u(rbsp, 1)) {
538 do {
539 modification_of_pic_nums_idc = vl_rbsp_ue(rbsp);
540 if (modification_of_pic_nums_idc == 0 ||
541 modification_of_pic_nums_idc == 1)
542 /* abs_diff_pic_num_minus1 */
543 vl_rbsp_ue(rbsp);
544 else if (modification_of_pic_nums_idc == 2)
545 /* long_term_pic_num */
546 vl_rbsp_ue(rbsp);
547 } while (modification_of_pic_nums_idc != 3);
548 }
549 }
550 }
551
552 static void pred_weight_table(vid_dec_PrivateType *priv, struct vl_rbsp *rbsp,
553 struct pipe_h264_sps *sps, enum pipe_h264_slice_type slice_type)
554 {
555 unsigned ChromaArrayType = sps->separate_colour_plane_flag ? 0 : sps->chroma_format_idc;
556 unsigned i, j;
557
558 /* luma_log2_weight_denom */
559 vl_rbsp_ue(rbsp);
560
561 if (ChromaArrayType != 0)
562 /* chroma_log2_weight_denom */
563 vl_rbsp_ue(rbsp);
564
565 for (i = 0; i <= priv->picture.h264.num_ref_idx_l0_active_minus1; ++i) {
566 /* luma_weight_l0_flag */
567 if (vl_rbsp_u(rbsp, 1)) {
568 /* luma_weight_l0[i] */
569 vl_rbsp_se(rbsp);
570 /* luma_offset_l0[i] */
571 vl_rbsp_se(rbsp);
572 }
573 if (ChromaArrayType != 0) {
574 /* chroma_weight_l0_flag */
575 if (vl_rbsp_u(rbsp, 1)) {
576 for (j = 0; j < 2; ++j) {
577 /* chroma_weight_l0[i][j] */
578 vl_rbsp_se(rbsp);
579 /* chroma_offset_l0[i][j] */
580 vl_rbsp_se(rbsp);
581 }
582 }
583 }
584 }
585
586 if (slice_type == 1) {
587 for (i = 0; i <= priv->picture.h264.num_ref_idx_l1_active_minus1; ++i) {
588 /* luma_weight_l1_flag */
589 if (vl_rbsp_u(rbsp, 1)) {
590 /* luma_weight_l1[i] */
591 vl_rbsp_se(rbsp);
592 /* luma_offset_l1[i] */
593 vl_rbsp_se(rbsp);
594 }
595 if (ChromaArrayType != 0) {
596 /* chroma_weight_l1_flag */
597 if (vl_rbsp_u(rbsp, 1)) {
598 for (j = 0; j < 2; ++j) {
599 /* chroma_weight_l1[i][j] */
600 vl_rbsp_se(rbsp);
601 /* chroma_offset_l1[i][j] */
602 vl_rbsp_se(rbsp);
603 }
604 }
605 }
606 }
607 }
608 }
609
610 static void dec_ref_pic_marking(vid_dec_PrivateType *priv, struct vl_rbsp *rbsp,
611 bool IdrPicFlag)
612 {
613 unsigned memory_management_control_operation;
614
615 if (IdrPicFlag) {
616 /* no_output_of_prior_pics_flag */
617 vl_rbsp_u(rbsp, 1);
618 /* long_term_reference_flag */
619 vl_rbsp_u(rbsp, 1);
620 } else {
621 /* adaptive_ref_pic_marking_mode_flag */
622 if (vl_rbsp_u(rbsp, 1)) {
623 do {
624 memory_management_control_operation = vl_rbsp_ue(rbsp);
625
626 if (memory_management_control_operation == 1 ||
627 memory_management_control_operation == 3)
628 /* difference_of_pic_nums_minus1 */
629 vl_rbsp_ue(rbsp);
630
631 if (memory_management_control_operation == 2)
632 /* long_term_pic_num */
633 vl_rbsp_ue(rbsp);
634
635 if (memory_management_control_operation == 3 ||
636 memory_management_control_operation == 6)
637 /* long_term_frame_idx */
638 vl_rbsp_ue(rbsp);
639
640 if (memory_management_control_operation == 4)
641 /* max_long_term_frame_idx_plus1 */
642 vl_rbsp_ue(rbsp);
643 } while (memory_management_control_operation != 0);
644 }
645 }
646 }
647
648 static void slice_header(vid_dec_PrivateType *priv, struct vl_rbsp *rbsp,
649 unsigned nal_ref_idc, unsigned nal_unit_type)
650 {
651 enum pipe_h264_slice_type slice_type;
652 struct pipe_h264_pps *pps;
653 struct pipe_h264_sps *sps;
654 unsigned frame_num, prevFrameNum;
655 bool IdrPicFlag = nal_unit_type == 5;
656
657 if (IdrPicFlag != priv->codec_data.h264.IdrPicFlag)
658 vid_dec_h264_EndFrame(priv);
659
660 priv->codec_data.h264.IdrPicFlag = IdrPicFlag;
661
662 /* first_mb_in_slice */
663 vl_rbsp_ue(rbsp);
664
665 slice_type = vl_rbsp_ue(rbsp) % 5;
666
667 pps = pic_parameter_set_id(priv, rbsp);
668 if (!pps)
669 return;
670
671 sps = pps->sps;
672 if (!sps)
673 return;
674
675 if (pps != priv->picture.h264.pps)
676 vid_dec_h264_EndFrame(priv);
677
678 priv->picture.h264.pps = pps;
679
680 if (sps->separate_colour_plane_flag == 1 )
681 /* colour_plane_id */
682 vl_rbsp_u(rbsp, 2);
683
684 frame_num = vl_rbsp_u(rbsp, sps->log2_max_frame_num_minus4 + 4);
685
686 if (frame_num != priv->picture.h264.frame_num)
687 vid_dec_h264_EndFrame(priv);
688
689 prevFrameNum = priv->picture.h264.frame_num;
690 priv->picture.h264.frame_num = frame_num;
691
692 priv->picture.h264.field_pic_flag = 0;
693 priv->picture.h264.bottom_field_flag = 0;
694
695 if (!sps->frame_mbs_only_flag) {
696 unsigned field_pic_flag = vl_rbsp_u(rbsp, 1);
697
698 if (!field_pic_flag && field_pic_flag != priv->picture.h264.field_pic_flag)
699 vid_dec_h264_EndFrame(priv);
700
701 priv->picture.h264.field_pic_flag = field_pic_flag;
702
703 if (priv->picture.h264.field_pic_flag) {
704 unsigned bottom_field_flag = vl_rbsp_u(rbsp, 1);
705
706 if (bottom_field_flag != priv->picture.h264.bottom_field_flag)
707 vid_dec_h264_EndFrame(priv);
708
709 priv->picture.h264.bottom_field_flag = bottom_field_flag;
710 }
711 }
712
713 if (IdrPicFlag) {
714 unsigned idr_pic_id = vl_rbsp_ue(rbsp);
715
716 if (idr_pic_id != priv->codec_data.h264.idr_pic_id)
717 vid_dec_h264_EndFrame(priv);
718
719 priv->codec_data.h264.idr_pic_id = idr_pic_id;
720 }
721
722 if (sps->pic_order_cnt_type == 0) {
723 unsigned log2_max_pic_order_cnt_lsb = sps->log2_max_pic_order_cnt_lsb_minus4 + 4;
724 unsigned max_pic_order_cnt_lsb = 1 << log2_max_pic_order_cnt_lsb;
725 unsigned pic_order_cnt_lsb = vl_rbsp_u(rbsp, log2_max_pic_order_cnt_lsb);
726 unsigned pic_order_cnt_msb;
727
728 if (pic_order_cnt_lsb != priv->codec_data.h264.pic_order_cnt_lsb)
729 vid_dec_h264_EndFrame(priv);
730
731 if (IdrPicFlag) {
732 priv->codec_data.h264.pic_order_cnt_msb = 0;
733 priv->codec_data.h264.pic_order_cnt_lsb = 0;
734 }
735
736 if ((pic_order_cnt_lsb < priv->codec_data.h264.pic_order_cnt_lsb) &&
737 (priv->codec_data.h264.pic_order_cnt_lsb - pic_order_cnt_lsb) >= (max_pic_order_cnt_lsb / 2))
738 pic_order_cnt_msb = priv->codec_data.h264.pic_order_cnt_msb + max_pic_order_cnt_lsb;
739
740 else if ((pic_order_cnt_lsb > priv->codec_data.h264.pic_order_cnt_lsb) &&
741 (pic_order_cnt_lsb - priv->codec_data.h264.pic_order_cnt_lsb) > (max_pic_order_cnt_lsb / 2))
742 pic_order_cnt_msb = priv->codec_data.h264.pic_order_cnt_msb - max_pic_order_cnt_lsb;
743
744 else
745 pic_order_cnt_msb = priv->codec_data.h264.pic_order_cnt_msb;
746
747 priv->codec_data.h264.pic_order_cnt_msb = pic_order_cnt_msb;
748 priv->codec_data.h264.pic_order_cnt_lsb = pic_order_cnt_lsb;
749
750 if (pps->bottom_field_pic_order_in_frame_present_flag && !priv->picture.h264.field_pic_flag) {
751 unsigned delta_pic_order_cnt_bottom = vl_rbsp_se(rbsp);
752
753 if (delta_pic_order_cnt_bottom != priv->codec_data.h264.delta_pic_order_cnt_bottom)
754 vid_dec_h264_EndFrame(priv);
755
756 priv->codec_data.h264.delta_pic_order_cnt_bottom = delta_pic_order_cnt_bottom;
757 }
758
759 if (!priv->picture.h264.field_pic_flag) {
760 priv->picture.h264.field_order_cnt[0] = pic_order_cnt_msb + pic_order_cnt_lsb;
761 priv->picture.h264.field_order_cnt[1] = priv->picture.h264.field_order_cnt [0] +
762 priv->codec_data.h264.delta_pic_order_cnt_bottom;
763 } else if (!priv->picture.h264.bottom_field_flag)
764 priv->picture.h264.field_order_cnt[0] = pic_order_cnt_msb + pic_order_cnt_lsb;
765 else
766 priv->picture.h264.field_order_cnt[1] = pic_order_cnt_msb + pic_order_cnt_lsb;
767
768 } else if (sps->pic_order_cnt_type == 1) {
769 unsigned MaxFrameNum = 1 << (sps->log2_max_frame_num_minus4 + 4);
770 unsigned FrameNumOffset, absFrameNum, expectedPicOrderCnt;
771
772 if (!sps->delta_pic_order_always_zero_flag) {
773 unsigned delta_pic_order_cnt[2];
774
775 delta_pic_order_cnt[0] = vl_rbsp_se(rbsp);
776
777 if (delta_pic_order_cnt[0] != priv->codec_data.h264.delta_pic_order_cnt[0])
778 vid_dec_h264_EndFrame(priv);
779
780 priv->codec_data.h264.delta_pic_order_cnt[0] = delta_pic_order_cnt[0];
781
782 if (pps->bottom_field_pic_order_in_frame_present_flag && !priv->picture.h264.field_pic_flag) {
783 delta_pic_order_cnt[1] = vl_rbsp_se(rbsp);
784
785 if (delta_pic_order_cnt[1] != priv->codec_data.h264.delta_pic_order_cnt[1])
786 vid_dec_h264_EndFrame(priv);
787
788 priv->codec_data.h264.delta_pic_order_cnt[1] = delta_pic_order_cnt[1];
789 }
790 }
791
792 if (IdrPicFlag)
793 FrameNumOffset = 0;
794 else if (prevFrameNum > frame_num)
795 FrameNumOffset = priv->codec_data.h264.prevFrameNumOffset + MaxFrameNum;
796 else
797 FrameNumOffset = priv->codec_data.h264.prevFrameNumOffset;
798
799 priv->codec_data.h264.prevFrameNumOffset = FrameNumOffset;
800
801 if (sps->num_ref_frames_in_pic_order_cnt_cycle != 0)
802 absFrameNum = FrameNumOffset + frame_num;
803 else
804 absFrameNum = 0;
805
806 if (nal_ref_idc == 0 && absFrameNum > 0)
807 absFrameNum = absFrameNum - 1;
808
809 if (absFrameNum > 0) {
810 unsigned picOrderCntCycleCnt = (absFrameNum - 1) / sps->num_ref_frames_in_pic_order_cnt_cycle;
811 unsigned frameNumInPicOrderCntCycle = (absFrameNum - 1) % sps->num_ref_frames_in_pic_order_cnt_cycle;
812 signed ExpectedDeltaPerPicOrderCntCycle = 0;
813 unsigned i;
814
815 for (i = 0; i < sps->num_ref_frames_in_pic_order_cnt_cycle; ++i)
816 ExpectedDeltaPerPicOrderCntCycle += sps->offset_for_ref_frame[i];
817
818 expectedPicOrderCnt = picOrderCntCycleCnt * ExpectedDeltaPerPicOrderCntCycle;
819 for (i = 0; i <= frameNumInPicOrderCntCycle; ++i)
820 expectedPicOrderCnt += sps->offset_for_ref_frame[i];
821
822 } else
823 expectedPicOrderCnt = 0;
824
825 if (nal_ref_idc == 0)
826 expectedPicOrderCnt += sps->offset_for_non_ref_pic;
827
828 if (!priv->picture.h264.field_pic_flag) {
829 priv->picture.h264.field_order_cnt[0] = expectedPicOrderCnt + priv->codec_data.h264.delta_pic_order_cnt[0];
830 priv->picture.h264.field_order_cnt[1] = priv->picture.h264.field_order_cnt[0] +
831 sps->offset_for_top_to_bottom_field + priv->codec_data.h264.delta_pic_order_cnt[1];
832
833 } else if (!priv->picture.h264.bottom_field_flag)
834 priv->picture.h264.field_order_cnt[0] = expectedPicOrderCnt + priv->codec_data.h264.delta_pic_order_cnt[0];
835 else
836 priv->picture.h264.field_order_cnt[1] = expectedPicOrderCnt + sps->offset_for_top_to_bottom_field +
837 priv->codec_data.h264.delta_pic_order_cnt[0];
838
839 } else if (sps->pic_order_cnt_type == 2) {
840 unsigned MaxFrameNum = 1 << (sps->log2_max_frame_num_minus4 + 4);
841 unsigned FrameNumOffset, tempPicOrderCnt;
842
843 if (IdrPicFlag)
844 FrameNumOffset = 0;
845 else if (prevFrameNum > frame_num)
846 FrameNumOffset = priv->codec_data.h264.prevFrameNumOffset + MaxFrameNum;
847 else
848 FrameNumOffset = priv->codec_data.h264.prevFrameNumOffset;
849
850 priv->codec_data.h264.prevFrameNumOffset = FrameNumOffset;
851
852 if (IdrPicFlag)
853 tempPicOrderCnt = 0;
854 else if (nal_ref_idc == 0)
855 tempPicOrderCnt = 2 * (FrameNumOffset + frame_num) - 1;
856 else
857 tempPicOrderCnt = 2 * (FrameNumOffset + frame_num);
858
859 if (!priv->picture.h264.field_pic_flag) {
860 priv->picture.h264.field_order_cnt[0] = tempPicOrderCnt;
861 priv->picture.h264.field_order_cnt[1] = tempPicOrderCnt;
862
863 } else if (!priv->picture.h264.bottom_field_flag)
864 priv->picture.h264.field_order_cnt[0] = tempPicOrderCnt;
865 else
866 priv->picture.h264.field_order_cnt[1] = tempPicOrderCnt;
867 }
868
869 if (pps->redundant_pic_cnt_present_flag)
870 /* redundant_pic_cnt */
871 vl_rbsp_ue(rbsp);
872
873 if (slice_type == PIPE_H264_SLICE_TYPE_B)
874 /* direct_spatial_mv_pred_flag */
875 vl_rbsp_u(rbsp, 1);
876
877 priv->picture.h264.num_ref_idx_l0_active_minus1 = pps->num_ref_idx_l0_default_active_minus1;
878 priv->picture.h264.num_ref_idx_l1_active_minus1 = pps->num_ref_idx_l1_default_active_minus1;
879
880 if (slice_type == PIPE_H264_SLICE_TYPE_P ||
881 slice_type == PIPE_H264_SLICE_TYPE_SP ||
882 slice_type == PIPE_H264_SLICE_TYPE_B) {
883
884 /* num_ref_idx_active_override_flag */
885 if (vl_rbsp_u(rbsp, 1)) {
886 priv->picture.h264.num_ref_idx_l0_active_minus1 = vl_rbsp_ue(rbsp);
887
888 if (slice_type == PIPE_H264_SLICE_TYPE_B)
889 priv->picture.h264.num_ref_idx_l1_active_minus1 = vl_rbsp_ue(rbsp);
890 }
891 }
892
893 if (nal_unit_type == 20 || nal_unit_type == 21)
894 ref_pic_list_mvc_modification(priv, rbsp);
895 else
896 ref_pic_list_modification(priv, rbsp, slice_type);
897
898 if ((pps->weighted_pred_flag && (slice_type == PIPE_H264_SLICE_TYPE_P || slice_type == PIPE_H264_SLICE_TYPE_SP)) ||
899 (pps->weighted_bipred_idc == 1 && slice_type == PIPE_H264_SLICE_TYPE_B))
900 pred_weight_table(priv, rbsp, sps, slice_type);
901
902 if (nal_ref_idc != 0)
903 dec_ref_pic_marking(priv, rbsp, IdrPicFlag);
904
905 if (pps->entropy_coding_mode_flag && slice_type != PIPE_H264_SLICE_TYPE_I && slice_type != PIPE_H264_SLICE_TYPE_SI)
906 /* cabac_init_idc */
907 vl_rbsp_ue(rbsp);
908
909 /* slice_qp_delta */
910 vl_rbsp_se(rbsp);
911
912 if (slice_type == PIPE_H264_SLICE_TYPE_SP || slice_type == PIPE_H264_SLICE_TYPE_SI) {
913 if (slice_type == PIPE_H264_SLICE_TYPE_SP)
914 /* sp_for_switch_flag */
915 vl_rbsp_u(rbsp, 1);
916
917 /*slice_qs_delta */
918 vl_rbsp_se(rbsp);
919 }
920
921 if (pps->deblocking_filter_control_present_flag) {
922 unsigned disable_deblocking_filter_idc = vl_rbsp_ue(rbsp);
923
924 if (disable_deblocking_filter_idc != 1) {
925 /* slice_alpha_c0_offset_div2 */
926 vl_rbsp_se(rbsp);
927
928 /* slice_beta_offset_div2 */
929 vl_rbsp_se(rbsp);
930 }
931 }
932
933 if (pps->num_slice_groups_minus1 > 0 && pps->slice_group_map_type >= 3 && pps->slice_group_map_type <= 5)
934 /* slice_group_change_cycle */
935 vl_rbsp_u(rbsp, 2);
936 }
937
938 static void vid_dec_h264_Decode(vid_dec_PrivateType *priv, struct vl_vlc *vlc, unsigned min_bits_left)
939 {
940 unsigned nal_ref_idc, nal_unit_type;
941
942 if (!vl_vlc_search_byte(vlc, vl_vlc_bits_left(vlc) - min_bits_left, 0x00))
943 return;
944
945 if (vl_vlc_peekbits(vlc, 24) != 0x000001) {
946 vl_vlc_eatbits(vlc, 8);
947 return;
948 }
949
950 if (priv->slice) {
951 unsigned bytes = priv->bytes_left - (vl_vlc_bits_left(vlc) / 8);
952 priv->codec->decode_bitstream(priv->codec, priv->target, &priv->picture.base,
953 1, &priv->slice, &bytes);
954 priv->slice = NULL;
955 }
956
957 vl_vlc_eatbits(vlc, 24);
958
959 /* forbidden_zero_bit */
960 vl_vlc_eatbits(vlc, 1);
961
962 nal_ref_idc = vl_vlc_get_uimsbf(vlc, 2);
963
964 if (nal_ref_idc != priv->codec_data.h264.nal_ref_idc &&
965 (nal_ref_idc * priv->codec_data.h264.nal_ref_idc) == 0)
966 vid_dec_h264_EndFrame(priv);
967
968 priv->codec_data.h264.nal_ref_idc = nal_ref_idc;
969
970 nal_unit_type = vl_vlc_get_uimsbf(vlc, 5);
971
972 if (nal_unit_type != 1 && nal_unit_type != 5)
973 vid_dec_h264_EndFrame(priv);
974
975 if (nal_unit_type == 7) {
976 struct vl_rbsp rbsp;
977 vl_rbsp_init(&rbsp, vlc, ~0);
978 seq_parameter_set(priv, &rbsp);
979
980 } else if (nal_unit_type == 8) {
981 struct vl_rbsp rbsp;
982 vl_rbsp_init(&rbsp, vlc, ~0);
983 picture_parameter_set(priv, &rbsp);
984
985 } else if (nal_unit_type == 1 || nal_unit_type == 5) {
986 /* Coded slice of a non-IDR or IDR picture */
987 unsigned bits = vl_vlc_valid_bits(vlc);
988 unsigned bytes = bits / 8 + 4;
989 struct vl_rbsp rbsp;
990 uint8_t buf[8];
991 const void *ptr = buf;
992 unsigned i;
993
994 buf[0] = 0x0;
995 buf[1] = 0x0;
996 buf[2] = 0x1;
997 buf[3] = (nal_ref_idc << 5) | nal_unit_type;
998 for (i = 4; i < bytes; ++i)
999 buf[i] = vl_vlc_peekbits(vlc, bits) >> ((bytes - i - 1) * 8);
1000
1001 priv->bytes_left = (vl_vlc_bits_left(vlc) - bits) / 8;
1002 priv->slice = vlc->data;
1003
1004 vl_rbsp_init(&rbsp, vlc, 128);
1005 slice_header(priv, &rbsp, nal_ref_idc, nal_unit_type);
1006
1007 vid_dec_h264_BeginFrame(priv);
1008
1009 priv->codec->decode_bitstream(priv->codec, priv->target, &priv->picture.base,
1010 1, &ptr, &bytes);
1011 }
1012
1013 /* resync to byte boundary */
1014 vl_vlc_eatbits(vlc, vl_vlc_valid_bits(vlc) % 8);
1015 }