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