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