Ticket #4415: h264_part.c

File h264_part.c, 29.0 KB (added by mmorrison, 12 years ago)

Modified Sources

Line 
1/**
2 * Decode a slice header.
3 * This will also call ff_MPV_common_init() and frame_start() as needed.
4 *
5 * @param h h264context
6 * @param h0 h264 master context (differs from 'h' when doing sliced based
7 * parallel decoding)
8 *
9 * @return 0 if okay, <0 if an error occurred, 1 if decoding must not be multithreaded
10 */
11static int decode_slice_header(H264Context *h, H264Context *h0)
12{
13 unsigned int first_mb_in_slice;
14 unsigned int pps_id;
15 int ret;
16 unsigned int slice_type, tmp, i, j;
17 int last_pic_structure, last_pic_droppable;
18 int must_reinit;
19 int needs_reinit = 0;
20 int field_pic_flag, bottom_field_flag;
21
22 h->me.qpel_put = h->h264qpel.put_h264_qpel_pixels_tab;
23 h->me.qpel_avg = h->h264qpel.avg_h264_qpel_pixels_tab;
24
25 first_mb_in_slice = get_ue_golomb_long(&h->gb);
26
27 if (first_mb_in_slice == 0) { // FIXME better field boundary detection
28 if (h0->current_slice && FIELD_PICTURE(h)) {
29 field_end(h, 1);
30 }
31
32 h0->current_slice = 0;
33 if (!h0->first_field) {
34 if (h->cur_pic_ptr && !h->droppable) {
35 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
36 h->picture_structure == PICT_BOTTOM_FIELD);
37 }
38 h->cur_pic_ptr = NULL;
39 }
40 }
41
42 slice_type = get_ue_golomb_31(&h->gb);
43 if (slice_type > 9) {
44 av_log(h->avctx, AV_LOG_ERROR,
45 "slice type too large (%d) at %d %d\n",
46 slice_type, h->mb_x, h->mb_y);
47 return AVERROR_INVALIDDATA;
48 }
49 if (slice_type > 4) {
50 slice_type -= 5;
51 h->slice_type_fixed = 1;
52 } else
53 h->slice_type_fixed = 0;
54
55 slice_type = golomb_to_pict_type[slice_type];
56 h->slice_type = slice_type;
57 h->slice_type_nos = slice_type & 3;
58
59 // to make a few old functions happy, it's wrong though
60 h->pict_type = h->slice_type;
61
62 pps_id = get_ue_golomb(&h->gb);
63 if (pps_id >= MAX_PPS_COUNT) {
64 av_log(h->avctx, AV_LOG_ERROR, "pps_id %d out of range\n", pps_id);
65 return AVERROR_INVALIDDATA;
66 }
67 if (!h0->pps_buffers[pps_id]) {
68 av_log(h->avctx, AV_LOG_ERROR,
69 "non-existing PPS %u referenced\n",
70 pps_id);
71 return AVERROR_INVALIDDATA;
72 }
73 h->pps = *h0->pps_buffers[pps_id];
74
75 if (!h0->sps_buffers[h->pps.sps_id]) {
76 av_log(h->avctx, AV_LOG_ERROR,
77 "non-existing SPS %u referenced\n",
78 h->pps.sps_id);
79 return AVERROR_INVALIDDATA;
80 }
81
82 if (h->pps.sps_id != h->current_sps_id ||
83 h0->sps_buffers[h->pps.sps_id]->new) {
84 h0->sps_buffers[h->pps.sps_id]->new = 0;
85
86 h->current_sps_id = h->pps.sps_id;
87 h->sps = *h0->sps_buffers[h->pps.sps_id];
88
89 if (h->mb_width != h->sps.mb_width ||
90 h->mb_height != h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag) ||
91 h->avctx->bits_per_raw_sample != h->sps.bit_depth_luma ||
92 h->cur_chroma_format_idc != h->sps.chroma_format_idc
93 )
94 {
95 av_log(h->avctx, AV_LOG_INFO, "hw: %d sw: %d\n", h->mb_width, h->sps.mb_width );
96 av_log(h->avctx, AV_LOG_INFO, "hh: %d sh: %d\n", h->mb_height, h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag) );
97 av_log(h->avctx, AV_LOG_INFO, "hbpsw: %d sbdl: %d\n", h->avctx->bits_per_raw_sample, h->sps.bit_depth_luma );
98 av_log(h->avctx, AV_LOG_INFO, "hccfi: %d scfi: %d\n", h->cur_chroma_format_idc, h->sps.chroma_format_idc );
99 av_log(h->avctx, AV_LOG_INFO, "needs_reinit 0\n");
100 needs_reinit = 1;
101 }
102
103 if (h->bit_depth_luma != h->sps.bit_depth_luma ||
104 h->chroma_format_idc != h->sps.chroma_format_idc) {
105 av_log(h->avctx, AV_LOG_INFO, "hbdl: %d sbdl: %d\n", h->bit_depth_luma, h->sps.bit_depth_luma );
106 av_log(h->avctx, AV_LOG_INFO, "hcfi: %d scfi: %d\n", h->chroma_format_idc, h->sps.chroma_format_idc);
107 av_log(h->avctx, AV_LOG_INFO, "needs_reinit 1\n");
108
109 h->bit_depth_luma = h->sps.bit_depth_luma;
110 h->chroma_format_idc = h->sps.chroma_format_idc;
111 needs_reinit = 1;
112 }
113 if ((ret = h264_set_parameter_from_sps(h)) < 0)
114 return ret;
115 }
116
117 h->avctx->profile = ff_h264_get_profile(&h->sps);
118 h->avctx->level = h->sps.level_idc;
119 h->avctx->refs = h->sps.ref_frame_count;
120
121 must_reinit = (h->context_initialized &&
122 ( 16*h->sps.mb_width != h->avctx->coded_width
123 || 16*h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag) != h->avctx->coded_height
124 || h->avctx->bits_per_raw_sample != h->sps.bit_depth_luma
125 || h->cur_chroma_format_idc != h->sps.chroma_format_idc
126 || av_cmp_q(h->sps.sar, h->avctx->sample_aspect_ratio)
127 || h->mb_width != h->sps.mb_width
128 || h->mb_height != h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag)
129 ));
130 if (h0->avctx->pix_fmt != get_pixel_format(h0, 0))
131 must_reinit = 1;
132
133 h->mb_width = h->sps.mb_width;
134 h->mb_height = h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
135 h->mb_num = h->mb_width * h->mb_height;
136 h->mb_stride = h->mb_width + 1;
137
138 h->b_stride = h->mb_width * 4;
139
140 h->chroma_y_shift = h->sps.chroma_format_idc <= 1; // 400 uses yuv420p
141
142 h->width = 16 * h->mb_width;
143 h->height = 16 * h->mb_height;
144
145 ret = init_dimensions(h);
146 if (ret < 0)
147 return ret;
148
149 if (h->sps.video_signal_type_present_flag) {
150 h->avctx->color_range = h->sps.full_range>0 ? AVCOL_RANGE_JPEG
151 : AVCOL_RANGE_MPEG;
152 if (h->sps.colour_description_present_flag) {
153 if (h->avctx->colorspace != h->sps.colorspace)
154 {
155 av_log(h->avctx, AV_LOG_INFO, "needs_reinit 2\n");
156 needs_reinit = 1;
157 }
158 h->avctx->color_primaries = h->sps.color_primaries;
159 h->avctx->color_trc = h->sps.color_trc;
160 h->avctx->colorspace = h->sps.colorspace;
161 }
162 }
163
164 if (h->context_initialized &&
165 (h->width != h->avctx->coded_width ||
166 h->height != h->avctx->coded_height ||
167 must_reinit ||
168 needs_reinit)) {
169 if (h != h0) {
170 av_log(h->avctx, AV_LOG_INFO, "h: %p h0: %p\n", h, h0 );
171 av_log(h->avctx, AV_LOG_INFO, "%dx%d vs %dx%d\n", h->width, h->height, h->avctx->coded_width,
172 h->avctx->coded_height );
173 av_log(h->avctx, AV_LOG_INFO, "mr: %d nr: %d\n", must_reinit, needs_reinit );
174 av_log(h->avctx, AV_LOG_ERROR, "changing width/height on "
175 "slice %d\n", h0->current_slice + 1);
176 return AVERROR_INVALIDDATA;
177 }
178
179 flush_change(h);
180
181 if ((ret = get_pixel_format(h, 1)) < 0)
182 return ret;
183 h->avctx->pix_fmt = ret;
184
185 av_log(h->avctx, AV_LOG_INFO, "Reinit context to %dx%d, "
186 "pix_fmt: %s\n", h->width, h->height, av_get_pix_fmt_name(h->avctx->pix_fmt));
187
188 if ((ret = h264_slice_header_init(h, 1)) < 0) {
189 av_log(h->avctx, AV_LOG_ERROR,
190 "h264_slice_header_init() failed\n");
191 return ret;
192 }
193 }
194 if (!h->context_initialized) {
195 if (h != h0) {
196 av_log(h->avctx, AV_LOG_ERROR,
197 "Cannot (re-)initialize context during parallel decoding.\n");
198 return AVERROR_PATCHWELCOME;
199 }
200
201 if ((ret = get_pixel_format(h, 1)) < 0)
202 return ret;
203 h->avctx->pix_fmt = ret;
204
205 if ((ret = h264_slice_header_init(h, 0)) < 0) {
206 av_log(h->avctx, AV_LOG_ERROR,
207 "h264_slice_header_init() failed\n");
208 return ret;
209 }
210 }
211
212 if (h == h0 && h->dequant_coeff_pps != pps_id) {
213 h->dequant_coeff_pps = pps_id;
214 init_dequant_tables(h);
215 }
216
217 h->frame_num = get_bits(&h->gb, h->sps.log2_max_frame_num);
218
219 h->mb_mbaff = 0;
220 h->mb_aff_frame = 0;
221 last_pic_structure = h0->picture_structure;
222 last_pic_droppable = h0->droppable;
223 h->droppable = h->nal_ref_idc == 0;
224 if (h->sps.frame_mbs_only_flag) {
225 h->picture_structure = PICT_FRAME;
226 } else {
227 if (!h->sps.direct_8x8_inference_flag && slice_type == AV_PICTURE_TYPE_B) {
228 av_log(h->avctx, AV_LOG_ERROR, "This stream was generated by a broken encoder, invalid 8x8 inference\n");
229 return -1;
230 }
231 field_pic_flag = get_bits1(&h->gb);
232 if (field_pic_flag) {
233 bottom_field_flag = get_bits1(&h->gb);
234 h->picture_structure = PICT_TOP_FIELD + bottom_field_flag;
235 } else {
236 h->picture_structure = PICT_FRAME;
237 h->mb_aff_frame = h->sps.mb_aff;
238 }
239 }
240 h->mb_field_decoding_flag = h->picture_structure != PICT_FRAME;
241
242 if (h0->current_slice != 0) {
243 if (last_pic_structure != h->picture_structure ||
244 last_pic_droppable != h->droppable) {
245 av_log(h->avctx, AV_LOG_ERROR,
246 "Changing field mode (%d -> %d) between slices is not allowed\n",
247 last_pic_structure, h->picture_structure);
248 h->picture_structure = last_pic_structure;
249 h->droppable = last_pic_droppable;
250 return AVERROR_INVALIDDATA;
251 } else if (!h0->cur_pic_ptr) {
252 av_log(h->avctx, AV_LOG_ERROR,
253 "unset cur_pic_ptr on %d. slice\n",
254 h0->current_slice + 1);
255 return AVERROR_INVALIDDATA;
256 }
257 } else {
258 /* Shorten frame num gaps so we don't have to allocate reference
259 * frames just to throw them away */
260 if (h->frame_num != h->prev_frame_num) {
261 int unwrap_prev_frame_num = h->prev_frame_num;
262 int max_frame_num = 1 << h->sps.log2_max_frame_num;
263
264 if (unwrap_prev_frame_num > h->frame_num)
265 unwrap_prev_frame_num -= max_frame_num;
266
267 if ((h->frame_num - unwrap_prev_frame_num) > h->sps.ref_frame_count) {
268 unwrap_prev_frame_num = (h->frame_num - h->sps.ref_frame_count) - 1;
269 if (unwrap_prev_frame_num < 0)
270 unwrap_prev_frame_num += max_frame_num;
271
272 h->prev_frame_num = unwrap_prev_frame_num;
273 }
274 }
275
276 /* See if we have a decoded first field looking for a pair...
277 * Here, we're using that to see if we should mark previously
278 * decode frames as "finished".
279 * We have to do that before the "dummy" in-between frame allocation,
280 * since that can modify h->cur_pic_ptr. */
281 if (h0->first_field) {
282 assert(h0->cur_pic_ptr);
283 assert(h0->cur_pic_ptr->f.data[0]);
284 assert(h0->cur_pic_ptr->reference != DELAYED_PIC_REF);
285
286 /* Mark old field/frame as completed */
287 if (h0->cur_pic_ptr->tf.owner == h0->avctx) {
288 ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,
289 last_pic_structure == PICT_BOTTOM_FIELD);
290 }
291
292 /* figure out if we have a complementary field pair */
293 if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
294 /* Previous field is unmatched. Don't display it, but let it
295 * remain for reference if marked as such. */
296 if (last_pic_structure != PICT_FRAME) {
297 ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,
298 last_pic_structure == PICT_TOP_FIELD);
299 }
300 } else {
301 if (h0->cur_pic_ptr->frame_num != h->frame_num) {
302 /* This and previous field were reference, but had
303 * different frame_nums. Consider this field first in
304 * pair. Throw away previous field except for reference
305 * purposes. */
306 if (last_pic_structure != PICT_FRAME) {
307 ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,
308 last_pic_structure == PICT_TOP_FIELD);
309 }
310 } else {
311 /* Second field in complementary pair */
312 if (!((last_pic_structure == PICT_TOP_FIELD &&
313 h->picture_structure == PICT_BOTTOM_FIELD) ||
314 (last_pic_structure == PICT_BOTTOM_FIELD &&
315 h->picture_structure == PICT_TOP_FIELD))) {
316 av_log(h->avctx, AV_LOG_ERROR,
317 "Invalid field mode combination %d/%d\n",
318 last_pic_structure, h->picture_structure);
319 h->picture_structure = last_pic_structure;
320 h->droppable = last_pic_droppable;
321 return AVERROR_INVALIDDATA;
322 } else if (last_pic_droppable != h->droppable) {
323 avpriv_request_sample(h->avctx,
324 "Found reference and non-reference fields in the same frame, which");
325 h->picture_structure = last_pic_structure;
326 h->droppable = last_pic_droppable;
327 return AVERROR_PATCHWELCOME;
328 }
329 }
330 }
331 }
332
333 while (h->frame_num != h->prev_frame_num && !h0->first_field &&
334 h->frame_num != (h->prev_frame_num + 1) % (1 << h->sps.log2_max_frame_num)) {
335 Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
336 av_log(h->avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n",
337 h->frame_num, h->prev_frame_num);
338 if (!h->sps.gaps_in_frame_num_allowed_flag)
339 for(i=0; i<FF_ARRAY_ELEMS(h->last_pocs); i++)
340 h->last_pocs[i] = INT_MIN;
341 ret = h264_frame_start(h);
342 if (ret < 0)
343 return ret;
344 h->prev_frame_num++;
345 h->prev_frame_num %= 1 << h->sps.log2_max_frame_num;
346 h->cur_pic_ptr->frame_num = h->prev_frame_num;
347 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 0);
348 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 1);
349 ret = ff_generate_sliding_window_mmcos(h, 1);
350 if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
351 return ret;
352 ret = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
353 if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
354 return ret;
355 /* Error concealment: If a ref is missing, copy the previous ref
356 * in its place.
357 * FIXME: Avoiding a memcpy would be nice, but ref handling makes
358 * many assumptions about there being no actual duplicates.
359 * FIXME: This does not copy padding for out-of-frame motion
360 * vectors. Given we are concealing a lost frame, this probably
361 * is not noticeable by comparison, but it should be fixed. */
362 if (h->short_ref_count) {
363 if (prev) {
364 av_image_copy(h->short_ref[0]->f.data,
365 h->short_ref[0]->f.linesize,
366 (const uint8_t **)prev->f.data,
367 prev->f.linesize,
368 h->avctx->pix_fmt,
369 h->mb_width * 16,
370 h->mb_height * 16);
371 h->short_ref[0]->poc = prev->poc + 2;
372 }
373 h->short_ref[0]->frame_num = h->prev_frame_num;
374 }
375 }
376
377 /* See if we have a decoded first field looking for a pair...
378 * We're using that to see whether to continue decoding in that
379 * frame, or to allocate a new one. */
380 if (h0->first_field) {
381 assert(h0->cur_pic_ptr);
382 assert(h0->cur_pic_ptr->f.data[0]);
383 assert(h0->cur_pic_ptr->reference != DELAYED_PIC_REF);
384
385 /* figure out if we have a complementary field pair */
386 if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
387 /* Previous field is unmatched. Don't display it, but let it
388 * remain for reference if marked as such. */
389 h0->cur_pic_ptr = NULL;
390 h0->first_field = FIELD_PICTURE(h);
391 } else {
392 if (h0->cur_pic_ptr->frame_num != h->frame_num) {
393 ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,
394 h0->picture_structure==PICT_BOTTOM_FIELD);
395 /* This and the previous field had different frame_nums.
396 * Consider this field first in pair. Throw away previous
397 * one except for reference purposes. */
398 h0->first_field = 1;
399 h0->cur_pic_ptr = NULL;
400 } else {
401 /* Second field in complementary pair */
402 h0->first_field = 0;
403 }
404 }
405 } else {
406 /* Frame or first field in a potentially complementary pair */
407 h0->first_field = FIELD_PICTURE(h);
408 }
409
410 if (!FIELD_PICTURE(h) || h0->first_field) {
411 if (h264_frame_start(h) < 0) {
412 h0->first_field = 0;
413 return AVERROR_INVALIDDATA;
414 }
415 } else {
416 release_unused_pictures(h, 0);
417 }
418 /* Some macroblocks can be accessed before they're available in case
419 * of lost slices, MBAFF or threading. */
420 if (FIELD_PICTURE(h)) {
421 for(i = (h->picture_structure == PICT_BOTTOM_FIELD); i<h->mb_height; i++)
422 memset(h->slice_table + i*h->mb_stride, -1, (h->mb_stride - (i+1==h->mb_height)) * sizeof(*h->slice_table));
423 } else {
424 memset(h->slice_table, -1,
425 (h->mb_height * h->mb_stride - 1) * sizeof(*h->slice_table));
426 }
427 h0->last_slice_type = -1;
428 }
429 if (h != h0 && (ret = clone_slice(h, h0)) < 0)
430 return ret;
431
432 /* can't be in alloc_tables because linesize isn't known there.
433 * FIXME: redo bipred weight to not require extra buffer? */
434 for (i = 0; i < h->slice_context_count; i++)
435 if (h->thread_context[i]) {
436 ret = alloc_scratch_buffers(h->thread_context[i], h->linesize);
437 if (ret < 0)
438 return ret;
439 }
440
441 h->cur_pic_ptr->frame_num = h->frame_num; // FIXME frame_num cleanup
442
443 av_assert1(h->mb_num == h->mb_width * h->mb_height);
444 if (first_mb_in_slice << FIELD_OR_MBAFF_PICTURE(h) >= h->mb_num ||
445 first_mb_in_slice >= h->mb_num) {
446 av_log(h->avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
447 return AVERROR_INVALIDDATA;
448 }
449 h->resync_mb_x = h->mb_x = first_mb_in_slice % h->mb_width;
450 h->resync_mb_y = h->mb_y = (first_mb_in_slice / h->mb_width) <<
451 FIELD_OR_MBAFF_PICTURE(h);
452 if (h->picture_structure == PICT_BOTTOM_FIELD)
453 h->resync_mb_y = h->mb_y = h->mb_y + 1;
454 av_assert1(h->mb_y < h->mb_height);
455
456 if (h->picture_structure == PICT_FRAME) {
457 h->curr_pic_num = h->frame_num;
458 h->max_pic_num = 1 << h->sps.log2_max_frame_num;
459 } else {
460 h->curr_pic_num = 2 * h->frame_num + 1;
461 h->max_pic_num = 1 << (h->sps.log2_max_frame_num + 1);
462 }
463
464 if (h->nal_unit_type == NAL_IDR_SLICE)
465 get_ue_golomb(&h->gb); /* idr_pic_id */
466
467 if (h->sps.poc_type == 0) {
468 h->poc_lsb = get_bits(&h->gb, h->sps.log2_max_poc_lsb);
469
470 if (h->pps.pic_order_present == 1 && h->picture_structure == PICT_FRAME)
471 h->delta_poc_bottom = get_se_golomb(&h->gb);
472 }
473
474 if (h->sps.poc_type == 1 && !h->sps.delta_pic_order_always_zero_flag) {
475 h->delta_poc[0] = get_se_golomb(&h->gb);
476
477 if (h->pps.pic_order_present == 1 && h->picture_structure == PICT_FRAME)
478 h->delta_poc[1] = get_se_golomb(&h->gb);
479 }
480
481 ff_init_poc(h, h->cur_pic_ptr->field_poc, &h->cur_pic_ptr->poc);
482
483 if (h->pps.redundant_pic_cnt_present)
484 h->redundant_pic_count = get_ue_golomb(&h->gb);
485
486 ret = ff_set_ref_count(h);
487 if (ret < 0)
488 return ret;
489
490 if (slice_type != AV_PICTURE_TYPE_I &&
491 (h0->current_slice == 0 ||
492 slice_type != h0->last_slice_type ||
493 memcmp(h0->last_ref_count, h0->ref_count, sizeof(h0->ref_count)))) {
494
495 ff_h264_fill_default_ref_list(h);
496 }
497
498 if (h->slice_type_nos != AV_PICTURE_TYPE_I) {
499 ret = ff_h264_decode_ref_pic_list_reordering(h);
500 if (ret < 0) {
501 h->ref_count[1] = h->ref_count[0] = 0;
502 return ret;
503 }
504 }
505
506 if ((h->pps.weighted_pred && h->slice_type_nos == AV_PICTURE_TYPE_P) ||
507 (h->pps.weighted_bipred_idc == 1 &&
508 h->slice_type_nos == AV_PICTURE_TYPE_B))
509 ff_pred_weight_table(h);
510 else if (h->pps.weighted_bipred_idc == 2 &&
511 h->slice_type_nos == AV_PICTURE_TYPE_B) {
512 implicit_weight_table(h, -1);
513 } else {
514 h->use_weight = 0;
515 for (i = 0; i < 2; i++) {
516 h->luma_weight_flag[i] = 0;
517 h->chroma_weight_flag[i] = 0;
518 }
519 }
520
521 // If frame-mt is enabled, only update mmco tables for the first slice
522 // in a field. Subsequent slices can temporarily clobber h->mmco_index
523 // or h->mmco, which will cause ref list mix-ups and decoding errors
524 // further down the line. This may break decoding if the first slice is
525 // corrupt, thus we only do this if frame-mt is enabled.
526 if (h->nal_ref_idc) {
527 ret = ff_h264_decode_ref_pic_marking(h0, &h->gb,
528 !(h->avctx->active_thread_type & FF_THREAD_FRAME) ||
529 h0->current_slice == 0);
530 if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
531 return AVERROR_INVALIDDATA;
532 }
533
534 if (FRAME_MBAFF(h)) {
535 ff_h264_fill_mbaff_ref_list(h);
536
537 if (h->pps.weighted_bipred_idc == 2 && h->slice_type_nos == AV_PICTURE_TYPE_B) {
538 implicit_weight_table(h, 0);
539 implicit_weight_table(h, 1);
540 }
541 }
542
543 if (h->slice_type_nos == AV_PICTURE_TYPE_B && !h->direct_spatial_mv_pred)
544 ff_h264_direct_dist_scale_factor(h);
545 ff_h264_direct_ref_list_init(h);
546
547 if (h->slice_type_nos != AV_PICTURE_TYPE_I && h->pps.cabac) {
548 tmp = get_ue_golomb_31(&h->gb);
549 if (tmp > 2) {
550 av_log(h->avctx, AV_LOG_ERROR, "cabac_init_idc overflow\n");
551 return AVERROR_INVALIDDATA;
552 }
553 h->cabac_init_idc = tmp;
554 }
555
556 h->last_qscale_diff = 0;
557 tmp = h->pps.init_qp + get_se_golomb(&h->gb);
558 if (tmp > 51 + 6 * (h->sps.bit_depth_luma - 8)) {
559 av_log(h->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
560 return AVERROR_INVALIDDATA;
561 }
562 h->qscale = tmp;
563 h->chroma_qp[0] = get_chroma_qp(h, 0, h->qscale);
564 h->chroma_qp[1] = get_chroma_qp(h, 1, h->qscale);
565 // FIXME qscale / qp ... stuff
566 if (h->slice_type == AV_PICTURE_TYPE_SP)
567 get_bits1(&h->gb); /* sp_for_switch_flag */
568 if (h->slice_type == AV_PICTURE_TYPE_SP ||
569 h->slice_type == AV_PICTURE_TYPE_SI)
570 get_se_golomb(&h->gb); /* slice_qs_delta */
571
572 h->deblocking_filter = 1;
573 h->slice_alpha_c0_offset = 52;
574 h->slice_beta_offset = 52;
575 if (h->pps.deblocking_filter_parameters_present) {
576 tmp = get_ue_golomb_31(&h->gb);
577 if (tmp > 2) {
578 av_log(h->avctx, AV_LOG_ERROR,
579 "deblocking_filter_idc %u out of range\n", tmp);
580 return AVERROR_INVALIDDATA;
581 }
582 h->deblocking_filter = tmp;
583 if (h->deblocking_filter < 2)
584 h->deblocking_filter ^= 1; // 1<->0
585
586 if (h->deblocking_filter) {
587 h->slice_alpha_c0_offset += get_se_golomb(&h->gb) << 1;
588 h->slice_beta_offset += get_se_golomb(&h->gb) << 1;
589 if (h->slice_alpha_c0_offset > 104U ||
590 h->slice_beta_offset > 104U) {
591 av_log(h->avctx, AV_LOG_ERROR,
592 "deblocking filter parameters %d %d out of range\n",
593 h->slice_alpha_c0_offset, h->slice_beta_offset);
594 return AVERROR_INVALIDDATA;
595 }
596 }
597 }
598
599 if (h->avctx->skip_loop_filter >= AVDISCARD_ALL ||
600 (h->avctx->skip_loop_filter >= AVDISCARD_NONKEY &&
601 h->slice_type_nos != AV_PICTURE_TYPE_I) ||
602 (h->avctx->skip_loop_filter >= AVDISCARD_BIDIR &&
603 h->slice_type_nos == AV_PICTURE_TYPE_B) ||
604 (h->avctx->skip_loop_filter >= AVDISCARD_NONREF &&
605 h->nal_ref_idc == 0))
606 h->deblocking_filter = 0;
607
608 if (h->deblocking_filter == 1 && h0->max_contexts > 1) {
609 if (h->avctx->flags2 & CODEC_FLAG2_FAST) {
610 /* Cheat slightly for speed:
611 * Do not bother to deblock across slices. */
612 h->deblocking_filter = 2;
613 } else {
614 h0->max_contexts = 1;
615 if (!h0->single_decode_warning) {
616 av_log(h->avctx, AV_LOG_INFO,
617 "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");
618 h0->single_decode_warning = 1;
619 }
620 if (h != h0) {
621 av_log(h->avctx, AV_LOG_ERROR,
622 "Deblocking switched inside frame.\n");
623 return 1;
624 }
625 }
626 }
627 h->qp_thresh = 15 + 52 -
628 FFMIN(h->slice_alpha_c0_offset, h->slice_beta_offset) -
629 FFMAX3(0,
630 h->pps.chroma_qp_index_offset[0],
631 h->pps.chroma_qp_index_offset[1]) +
632 6 * (h->sps.bit_depth_luma - 8);
633
634 h0->last_slice_type = slice_type;
635 memcpy(h0->last_ref_count, h0->ref_count, sizeof(h0->last_ref_count));
636 h->slice_num = ++h0->current_slice;
637
638 if (h->slice_num)
639 h0->slice_row[(h->slice_num-1)&(MAX_SLICES-1)]= h->resync_mb_y;
640 if ( h0->slice_row[h->slice_num&(MAX_SLICES-1)] + 3 >= h->resync_mb_y
641 && h0->slice_row[h->slice_num&(MAX_SLICES-1)] <= h->resync_mb_y
642 && h->slice_num >= MAX_SLICES) {
643 //in case of ASO this check needs to be updated depending on how we decide to assign slice numbers in this case
644 av_log(h->avctx, AV_LOG_WARNING, "Possibly too many slices (%d >= %d), increase MAX_SLICES and recompile if there are artifacts\n", h->slice_num, MAX_SLICES);
645 }
646
647 for (j = 0; j < 2; j++) {
648 int id_list[16];
649 int *ref2frm = h->ref2frm[h->slice_num & (MAX_SLICES - 1)][j];
650 for (i = 0; i < 16; i++) {
651 id_list[i] = 60;
652 if (j < h->list_count && i < h->ref_count[j] &&
653 h->ref_list[j][i].f.buf[0]) {
654 int k;
655 AVBuffer *buf = h->ref_list[j][i].f.buf[0]->buffer;
656 for (k = 0; k < h->short_ref_count; k++)
657 if (h->short_ref[k]->f.buf[0]->buffer == buf) {
658 id_list[i] = k;
659 break;
660 }
661 for (k = 0; k < h->long_ref_count; k++)
662 if (h->long_ref[k] && h->long_ref[k]->f.buf[0]->buffer == buf) {
663 id_list[i] = h->short_ref_count + k;
664 break;
665 }
666 }
667 }
668
669 ref2frm[0] =
670 ref2frm[1] = -1;
671 for (i = 0; i < 16; i++)
672 ref2frm[i + 2] = 4 * id_list[i] + (h->ref_list[j][i].reference & 3);
673 ref2frm[18 + 0] =
674 ref2frm[18 + 1] = -1;
675 for (i = 16; i < 48; i++)
676 ref2frm[i + 4] = 4 * id_list[(i - 16) >> 1] +
677 (h->ref_list[j][i].reference & 3);
678 }
679
680 if (h->ref_count[0]) h->er.last_pic = &h->ref_list[0][0];
681 if (h->ref_count[1]) h->er.next_pic = &h->ref_list[1][0];
682 h->er.ref_count = h->ref_count[0];
683
684 if (h->avctx->debug & FF_DEBUG_PICT_INFO) {
685 av_log(h->avctx, AV_LOG_DEBUG,
686 "slice:%d %s mb:%d %c%s%s pps:%u frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d:%d:%d weight:%d%s %s\n",
687 h->slice_num,
688 (h->picture_structure == PICT_FRAME ? "F" : h->picture_structure == PICT_TOP_FIELD ? "T" : "B"),
689 first_mb_in_slice,
690 av_get_picture_type_char(h->slice_type),
691 h->slice_type_fixed ? " fix" : "",
692 h->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",
693 pps_id, h->frame_num,
694 h->cur_pic_ptr->field_poc[0],
695 h->cur_pic_ptr->field_poc[1],
696 h->ref_count[0], h->ref_count[1],
697 h->qscale,
698 h->deblocking_filter,
699 h->slice_alpha_c0_offset / 2 - 26, h->slice_beta_offset / 2 - 26,
700 h->use_weight,
701 h->use_weight == 1 && h->use_weight_chroma ? "c" : "",
702 h->slice_type == AV_PICTURE_TYPE_B ? (h->direct_spatial_mv_pred ? "SPAT" : "TEMP") : "");
703 }
704
705 return 0;
706}