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