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