Add missing error_resilience includes to files that use ER
[libav.git] / libavcodec / h264.c
1 /*
2 * H.26L/H.264/AVC/JVT/14496-10/... decoder
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4 *
5 * This file is part of Libav.
6 *
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * H.264 / AVC / MPEG4 part10 codec.
25 * @author Michael Niedermayer <michaelni@gmx.at>
26 */
27
28 #include "libavutil/imgutils.h"
29 #include "internal.h"
30 #include "cabac.h"
31 #include "cabac_functions.h"
32 #include "dsputil.h"
33 #include "error_resilience.h"
34 #include "avcodec.h"
35 #include "mpegvideo.h"
36 #include "h264.h"
37 #include "h264data.h"
38 #include "h264chroma.h"
39 #include "h264_mvpred.h"
40 #include "golomb.h"
41 #include "mathops.h"
42 #include "rectangle.h"
43 #include "svq3.h"
44 #include "thread.h"
45 #include "vdpau_internal.h"
46 #include "libavutil/avassert.h"
47
48 // #undef NDEBUG
49 #include <assert.h>
50
51 const uint16_t ff_h264_mb_sizes[4] = { 256, 384, 512, 768 };
52
53 static const uint8_t rem6[QP_MAX_NUM + 1] = {
54 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2,
55 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5,
56 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
57 };
58
59 static const uint8_t div6[QP_MAX_NUM + 1] = {
60 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3,
61 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6,
62 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10,
63 };
64
65 static const enum AVPixelFormat h264_hwaccel_pixfmt_list_420[] = {
66 #if CONFIG_H264_DXVA2_HWACCEL
67 AV_PIX_FMT_DXVA2_VLD,
68 #endif
69 #if CONFIG_H264_VAAPI_HWACCEL
70 AV_PIX_FMT_VAAPI_VLD,
71 #endif
72 #if CONFIG_H264_VDA_HWACCEL
73 AV_PIX_FMT_VDA_VLD,
74 #endif
75 #if CONFIG_H264_VDPAU_HWACCEL
76 AV_PIX_FMT_VDPAU,
77 #endif
78 AV_PIX_FMT_YUV420P,
79 AV_PIX_FMT_NONE
80 };
81
82 static const enum AVPixelFormat h264_hwaccel_pixfmt_list_jpeg_420[] = {
83 #if CONFIG_H264_DXVA2_HWACCEL
84 AV_PIX_FMT_DXVA2_VLD,
85 #endif
86 #if CONFIG_H264_VAAPI_HWACCEL
87 AV_PIX_FMT_VAAPI_VLD,
88 #endif
89 #if CONFIG_H264_VDA_HWACCEL
90 AV_PIX_FMT_VDA_VLD,
91 #endif
92 #if CONFIG_H264_VDPAU_HWACCEL
93 AV_PIX_FMT_VDPAU,
94 #endif
95 AV_PIX_FMT_YUVJ420P,
96 AV_PIX_FMT_NONE
97 };
98
99 static void h264_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type,
100 int (*mv)[2][4][2],
101 int mb_x, int mb_y, int mb_intra, int mb_skipped)
102 {
103 H264Context *h = opaque;
104
105 h->mb_x = mb_x;
106 h->mb_y = mb_y;
107 h->mb_xy = mb_x + mb_y * h->mb_stride;
108 memset(h->non_zero_count_cache, 0, sizeof(h->non_zero_count_cache));
109 assert(ref >= 0);
110 /* FIXME: It is possible albeit uncommon that slice references
111 * differ between slices. We take the easy approach and ignore
112 * it for now. If this turns out to have any relevance in
113 * practice then correct remapping should be added. */
114 if (ref >= h->ref_count[0])
115 ref = 0;
116 fill_rectangle(&h->cur_pic.f.ref_index[0][4 * h->mb_xy],
117 2, 2, 2, ref, 1);
118 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
119 fill_rectangle(h->mv_cache[0][scan8[0]], 4, 4, 8,
120 pack16to32((*mv)[0][0][0], (*mv)[0][0][1]), 4);
121 assert(!FRAME_MBAFF);
122 ff_h264_hl_decode_mb(h);
123 }
124
125 void ff_h264_draw_horiz_band(H264Context *h, int y, int height)
126 {
127 AVCodecContext *avctx = h->avctx;
128 Picture *cur = &h->cur_pic;
129 Picture *last = h->ref_list[0][0].f.data[0] ? &h->ref_list[0][0] : NULL;
130 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
131 int vshift = desc->log2_chroma_h;
132 const int field_pic = h->picture_structure != PICT_FRAME;
133 if (field_pic) {
134 height <<= 1;
135 y <<= 1;
136 }
137
138 height = FFMIN(height, avctx->height - y);
139
140 if (field_pic && h->first_field && !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD))
141 return;
142
143 if (avctx->draw_horiz_band) {
144 AVFrame *src;
145 int offset[AV_NUM_DATA_POINTERS];
146 int i;
147
148 if (cur->f.pict_type == AV_PICTURE_TYPE_B || h->low_delay ||
149 (avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
150 src = &cur->f;
151 else if (last)
152 src = &last->f;
153 else
154 return;
155
156 offset[0] = y * src->linesize[0];
157 offset[1] =
158 offset[2] = (y >> vshift) * src->linesize[1];
159 for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
160 offset[i] = 0;
161
162 emms_c();
163
164 avctx->draw_horiz_band(avctx, src, offset,
165 y, h->picture_structure, height);
166 }
167 }
168
169 static void free_frame_buffer(H264Context *h, Picture *pic)
170 {
171 ff_thread_release_buffer(h->avctx, &pic->f);
172 av_freep(&pic->f.hwaccel_picture_private);
173 }
174
175 static void free_picture(H264Context *h, Picture *pic)
176 {
177 int i;
178
179 if (pic->f.data[0])
180 free_frame_buffer(h, pic);
181
182 av_freep(&pic->qscale_table_base);
183 pic->f.qscale_table = NULL;
184 av_freep(&pic->mb_type_base);
185 pic->f.mb_type = NULL;
186 for (i = 0; i < 2; i++) {
187 av_freep(&pic->motion_val_base[i]);
188 av_freep(&pic->f.ref_index[i]);
189 pic->f.motion_val[i] = NULL;
190 }
191 }
192
193 static void release_unused_pictures(H264Context *h, int remove_current)
194 {
195 int i;
196
197 /* release non reference frames */
198 for (i = 0; i < h->picture_count; i++) {
199 if (h->DPB[i].f.data[0] && !h->DPB[i].f.reference &&
200 (!h->DPB[i].owner2 || h->DPB[i].owner2 == h) &&
201 (remove_current || &h->DPB[i] != h->cur_pic_ptr)) {
202 free_frame_buffer(h, &h->DPB[i]);
203 }
204 }
205 }
206
207 static int alloc_scratch_buffers(H264Context *h, int linesize)
208 {
209 int alloc_size = FFALIGN(FFABS(linesize) + 32, 32);
210
211 if (h->bipred_scratchpad)
212 return 0;
213
214 h->bipred_scratchpad = av_malloc(16 * 6 * alloc_size);
215 // edge emu needs blocksize + filter length - 1
216 // (= 21x21 for h264)
217 h->edge_emu_buffer = av_mallocz(alloc_size * 2 * 21);
218 h->me.scratchpad = av_mallocz(alloc_size * 2 * 16 * 2);
219
220 if (!h->bipred_scratchpad || !h->edge_emu_buffer || !h->me.scratchpad) {
221 av_freep(&h->bipred_scratchpad);
222 av_freep(&h->edge_emu_buffer);
223 av_freep(&h->me.scratchpad);
224 return AVERROR(ENOMEM);
225 }
226
227 h->me.temp = h->me.scratchpad;
228
229 return 0;
230 }
231
232 static int alloc_picture(H264Context *h, Picture *pic)
233 {
234 const int big_mb_num = h->mb_stride * (h->mb_height + 1) + 1;
235 const int mb_array_size = h->mb_stride * h->mb_height;
236 const int b4_stride = h->mb_width * 4 + 1;
237 const int b4_array_size = b4_stride * h->mb_height * 4;
238 int i, ret = 0;
239
240 av_assert0(!pic->f.data[0]);
241
242 if (h->avctx->hwaccel) {
243 const AVHWAccel *hwaccel = h->avctx->hwaccel;
244 av_assert0(!pic->f.hwaccel_picture_private);
245 if (hwaccel->priv_data_size) {
246 pic->f.hwaccel_picture_private = av_mallocz(hwaccel->priv_data_size);
247 if (!pic->f.hwaccel_picture_private)
248 return AVERROR(ENOMEM);
249 }
250 }
251 ret = ff_thread_get_buffer(h->avctx, &pic->f);
252 if (ret < 0)
253 goto fail;
254
255 h->linesize = pic->f.linesize[0];
256 h->uvlinesize = pic->f.linesize[1];
257
258 if (pic->f.qscale_table == NULL) {
259 FF_ALLOCZ_OR_GOTO(h->avctx, pic->qscale_table_base,
260 (big_mb_num + h->mb_stride) * sizeof(uint8_t),
261 fail)
262 FF_ALLOCZ_OR_GOTO(h->avctx, pic->mb_type_base,
263 (big_mb_num + h->mb_stride) * sizeof(uint32_t),
264 fail)
265 pic->f.mb_type = pic->mb_type_base + 2 * h->mb_stride + 1;
266 pic->f.qscale_table = pic->qscale_table_base + 2 * h->mb_stride + 1;
267
268 for (i = 0; i < 2; i++) {
269 FF_ALLOCZ_OR_GOTO(h->avctx, pic->motion_val_base[i],
270 2 * (b4_array_size + 4) * sizeof(int16_t),
271 fail)
272 pic->f.motion_val[i] = pic->motion_val_base[i] + 4;
273 FF_ALLOCZ_OR_GOTO(h->avctx, pic->f.ref_index[i],
274 4 * mb_array_size * sizeof(uint8_t), fail)
275 }
276 pic->f.motion_subsample_log2 = 2;
277
278 pic->f.qstride = h->mb_stride;
279 }
280
281 pic->owner2 = h;
282
283 return 0;
284 fail:
285 free_frame_buffer(h, pic);
286 return (ret < 0) ? ret : AVERROR(ENOMEM);
287 }
288
289 static inline int pic_is_unused(H264Context *h, Picture *pic)
290 {
291 if (pic->f.data[0] == NULL)
292 return 1;
293 if (pic->needs_realloc && !(pic->f.reference & DELAYED_PIC_REF))
294 if (!pic->owner2 || pic->owner2 == h)
295 return 1;
296 return 0;
297 }
298
299 static int find_unused_picture(H264Context *h)
300 {
301 int i;
302
303 for (i = h->picture_range_start; i < h->picture_range_end; i++) {
304 if (pic_is_unused(h, &h->DPB[i]))
305 break;
306 }
307 if (i == h->picture_range_end)
308 return AVERROR_INVALIDDATA;
309
310 if (h->DPB[i].needs_realloc) {
311 h->DPB[i].needs_realloc = 0;
312 free_picture(h, &h->DPB[i]);
313 avcodec_get_frame_defaults(&h->DPB[i].f);
314 }
315
316 return i;
317 }
318
319 /**
320 * Check if the top & left blocks are available if needed and
321 * change the dc mode so it only uses the available blocks.
322 */
323 int ff_h264_check_intra4x4_pred_mode(H264Context *h)
324 {
325 static const int8_t top[12] = {
326 -1, 0, LEFT_DC_PRED, -1, -1, -1, -1, -1, 0
327 };
328 static const int8_t left[12] = {
329 0, -1, TOP_DC_PRED, 0, -1, -1, -1, 0, -1, DC_128_PRED
330 };
331 int i;
332
333 if (!(h->top_samples_available & 0x8000)) {
334 for (i = 0; i < 4; i++) {
335 int status = top[h->intra4x4_pred_mode_cache[scan8[0] + i]];
336 if (status < 0) {
337 av_log(h->avctx, AV_LOG_ERROR,
338 "top block unavailable for requested intra4x4 mode %d at %d %d\n",
339 status, h->mb_x, h->mb_y);
340 return -1;
341 } else if (status) {
342 h->intra4x4_pred_mode_cache[scan8[0] + i] = status;
343 }
344 }
345 }
346
347 if ((h->left_samples_available & 0x8888) != 0x8888) {
348 static const int mask[4] = { 0x8000, 0x2000, 0x80, 0x20 };
349 for (i = 0; i < 4; i++)
350 if (!(h->left_samples_available & mask[i])) {
351 int status = left[h->intra4x4_pred_mode_cache[scan8[0] + 8 * i]];
352 if (status < 0) {
353 av_log(h->avctx, AV_LOG_ERROR,
354 "left block unavailable for requested intra4x4 mode %d at %d %d\n",
355 status, h->mb_x, h->mb_y);
356 return -1;
357 } else if (status) {
358 h->intra4x4_pred_mode_cache[scan8[0] + 8 * i] = status;
359 }
360 }
361 }
362
363 return 0;
364 } // FIXME cleanup like ff_h264_check_intra_pred_mode
365
366 /**
367 * Check if the top & left blocks are available if needed and
368 * change the dc mode so it only uses the available blocks.
369 */
370 int ff_h264_check_intra_pred_mode(H264Context *h, int mode, int is_chroma)
371 {
372 static const int8_t top[7] = { LEFT_DC_PRED8x8, 1, -1, -1 };
373 static const int8_t left[7] = { TOP_DC_PRED8x8, -1, 2, -1, DC_128_PRED8x8 };
374
375 if (mode > 6U) {
376 av_log(h->avctx, AV_LOG_ERROR,
377 "out of range intra chroma pred mode at %d %d\n",
378 h->mb_x, h->mb_y);
379 return -1;
380 }
381
382 if (!(h->top_samples_available & 0x8000)) {
383 mode = top[mode];
384 if (mode < 0) {
385 av_log(h->avctx, AV_LOG_ERROR,
386 "top block unavailable for requested intra mode at %d %d\n",
387 h->mb_x, h->mb_y);
388 return -1;
389 }
390 }
391
392 if ((h->left_samples_available & 0x8080) != 0x8080) {
393 mode = left[mode];
394 if (is_chroma && (h->left_samples_available & 0x8080)) {
395 // mad cow disease mode, aka MBAFF + constrained_intra_pred
396 mode = ALZHEIMER_DC_L0T_PRED8x8 +
397 (!(h->left_samples_available & 0x8000)) +
398 2 * (mode == DC_128_PRED8x8);
399 }
400 if (mode < 0) {
401 av_log(h->avctx, AV_LOG_ERROR,
402 "left block unavailable for requested intra mode at %d %d\n",
403 h->mb_x, h->mb_y);
404 return -1;
405 }
406 }
407
408 return mode;
409 }
410
411 const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src,
412 int *dst_length, int *consumed, int length)
413 {
414 int i, si, di;
415 uint8_t *dst;
416 int bufidx;
417
418 // src[0]&0x80; // forbidden bit
419 h->nal_ref_idc = src[0] >> 5;
420 h->nal_unit_type = src[0] & 0x1F;
421
422 src++;
423 length--;
424
425 #define STARTCODE_TEST \
426 if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
427 if (src[i + 2] != 3) { \
428 /* startcode, so we must be past the end */ \
429 length = i; \
430 } \
431 break; \
432 }
433 #if HAVE_FAST_UNALIGNED
434 #define FIND_FIRST_ZERO \
435 if (i > 0 && !src[i]) \
436 i--; \
437 while (src[i]) \
438 i++
439 #if HAVE_FAST_64BIT
440 for (i = 0; i + 1 < length; i += 9) {
441 if (!((~AV_RN64A(src + i) &
442 (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
443 0x8000800080008080ULL))
444 continue;
445 FIND_FIRST_ZERO;
446 STARTCODE_TEST;
447 i -= 7;
448 }
449 #else
450 for (i = 0; i + 1 < length; i += 5) {
451 if (!((~AV_RN32A(src + i) &
452 (AV_RN32A(src + i) - 0x01000101U)) &
453 0x80008080U))
454 continue;
455 FIND_FIRST_ZERO;
456 STARTCODE_TEST;
457 i -= 3;
458 }
459 #endif
460 #else
461 for (i = 0; i + 1 < length; i += 2) {
462 if (src[i])
463 continue;
464 if (i > 0 && src[i - 1] == 0)
465 i--;
466 STARTCODE_TEST;
467 }
468 #endif
469
470 if (i >= length - 1) { // no escaped 0
471 *dst_length = length;
472 *consumed = length + 1; // +1 for the header
473 return src;
474 }
475
476 // use second escape buffer for inter data
477 bufidx = h->nal_unit_type == NAL_DPC ? 1 : 0;
478 av_fast_malloc(&h->rbsp_buffer[bufidx], &h->rbsp_buffer_size[bufidx],
479 length + FF_INPUT_BUFFER_PADDING_SIZE);
480 dst = h->rbsp_buffer[bufidx];
481
482 if (dst == NULL)
483 return NULL;
484
485 memcpy(dst, src, i);
486 si = di = i;
487 while (si + 2 < length) {
488 // remove escapes (very rare 1:2^22)
489 if (src[si + 2] > 3) {
490 dst[di++] = src[si++];
491 dst[di++] = src[si++];
492 } else if (src[si] == 0 && src[si + 1] == 0) {
493 if (src[si + 2] == 3) { // escape
494 dst[di++] = 0;
495 dst[di++] = 0;
496 si += 3;
497 continue;
498 } else // next start code
499 goto nsc;
500 }
501
502 dst[di++] = src[si++];
503 }
504 while (si < length)
505 dst[di++] = src[si++];
506 nsc:
507
508 memset(dst + di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
509
510 *dst_length = di;
511 *consumed = si + 1; // +1 for the header
512 /* FIXME store exact number of bits in the getbitcontext
513 * (it is needed for decoding) */
514 return dst;
515 }
516
517 /**
518 * Identify the exact end of the bitstream
519 * @return the length of the trailing, or 0 if damaged
520 */
521 static int decode_rbsp_trailing(H264Context *h, const uint8_t *src)
522 {
523 int v = *src;
524 int r;
525
526 tprintf(h->avctx, "rbsp trailing %X\n", v);
527
528 for (r = 1; r < 9; r++) {
529 if (v & 1)
530 return r;
531 v >>= 1;
532 }
533 return 0;
534 }
535
536 static inline int get_lowest_part_list_y(H264Context *h, Picture *pic, int n,
537 int height, int y_offset, int list)
538 {
539 int raw_my = h->mv_cache[list][scan8[n]][1];
540 int filter_height_up = (raw_my & 3) ? 2 : 0;
541 int filter_height_down = (raw_my & 3) ? 3 : 0;
542 int full_my = (raw_my >> 2) + y_offset;
543 int top = full_my - filter_height_up;
544 int bottom = full_my + filter_height_down + height;
545
546 return FFMAX(abs(top), bottom);
547 }
548
549 static inline void get_lowest_part_y(H264Context *h, int refs[2][48], int n,
550 int height, int y_offset, int list0,
551 int list1, int *nrefs)
552 {
553 int my;
554
555 y_offset += 16 * (h->mb_y >> MB_FIELD);
556
557 if (list0) {
558 int ref_n = h->ref_cache[0][scan8[n]];
559 Picture *ref = &h->ref_list[0][ref_n];
560
561 // Error resilience puts the current picture in the ref list.
562 // Don't try to wait on these as it will cause a deadlock.
563 // Fields can wait on each other, though.
564 if (ref->f.thread_opaque != h->cur_pic.f.thread_opaque ||
565 (ref->f.reference & 3) != h->picture_structure) {
566 my = get_lowest_part_list_y(h, ref, n, height, y_offset, 0);
567 if (refs[0][ref_n] < 0)
568 nrefs[0] += 1;
569 refs[0][ref_n] = FFMAX(refs[0][ref_n], my);
570 }
571 }
572
573 if (list1) {
574 int ref_n = h->ref_cache[1][scan8[n]];
575 Picture *ref = &h->ref_list[1][ref_n];
576
577 if (ref->f.thread_opaque != h->cur_pic.f.thread_opaque ||
578 (ref->f.reference & 3) != h->picture_structure) {
579 my = get_lowest_part_list_y(h, ref, n, height, y_offset, 1);
580 if (refs[1][ref_n] < 0)
581 nrefs[1] += 1;
582 refs[1][ref_n] = FFMAX(refs[1][ref_n], my);
583 }
584 }
585 }
586
587 /**
588 * Wait until all reference frames are available for MC operations.
589 *
590 * @param h the H264 context
591 */
592 static void await_references(H264Context *h)
593 {
594 const int mb_xy = h->mb_xy;
595 const int mb_type = h->cur_pic.f.mb_type[mb_xy];
596 int refs[2][48];
597 int nrefs[2] = { 0 };
598 int ref, list;
599
600 memset(refs, -1, sizeof(refs));
601
602 if (IS_16X16(mb_type)) {
603 get_lowest_part_y(h, refs, 0, 16, 0,
604 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
605 } else if (IS_16X8(mb_type)) {
606 get_lowest_part_y(h, refs, 0, 8, 0,
607 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
608 get_lowest_part_y(h, refs, 8, 8, 8,
609 IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
610 } else if (IS_8X16(mb_type)) {
611 get_lowest_part_y(h, refs, 0, 16, 0,
612 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
613 get_lowest_part_y(h, refs, 4, 16, 0,
614 IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
615 } else {
616 int i;
617
618 assert(IS_8X8(mb_type));
619
620 for (i = 0; i < 4; i++) {
621 const int sub_mb_type = h->sub_mb_type[i];
622 const int n = 4 * i;
623 int y_offset = (i & 2) << 2;
624
625 if (IS_SUB_8X8(sub_mb_type)) {
626 get_lowest_part_y(h, refs, n, 8, y_offset,
627 IS_DIR(sub_mb_type, 0, 0),
628 IS_DIR(sub_mb_type, 0, 1),
629 nrefs);
630 } else if (IS_SUB_8X4(sub_mb_type)) {
631 get_lowest_part_y(h, refs, n, 4, y_offset,
632 IS_DIR(sub_mb_type, 0, 0),
633 IS_DIR(sub_mb_type, 0, 1),
634 nrefs);
635 get_lowest_part_y(h, refs, n + 2, 4, y_offset + 4,
636 IS_DIR(sub_mb_type, 0, 0),
637 IS_DIR(sub_mb_type, 0, 1),
638 nrefs);
639 } else if (IS_SUB_4X8(sub_mb_type)) {
640 get_lowest_part_y(h, refs, n, 8, y_offset,
641 IS_DIR(sub_mb_type, 0, 0),
642 IS_DIR(sub_mb_type, 0, 1),
643 nrefs);
644 get_lowest_part_y(h, refs, n + 1, 8, y_offset,
645 IS_DIR(sub_mb_type, 0, 0),
646 IS_DIR(sub_mb_type, 0, 1),
647 nrefs);
648 } else {
649 int j;
650 assert(IS_SUB_4X4(sub_mb_type));
651 for (j = 0; j < 4; j++) {
652 int sub_y_offset = y_offset + 2 * (j & 2);
653 get_lowest_part_y(h, refs, n + j, 4, sub_y_offset,
654 IS_DIR(sub_mb_type, 0, 0),
655 IS_DIR(sub_mb_type, 0, 1),
656 nrefs);
657 }
658 }
659 }
660 }
661
662 for (list = h->list_count - 1; list >= 0; list--)
663 for (ref = 0; ref < 48 && nrefs[list]; ref++) {
664 int row = refs[list][ref];
665 if (row >= 0) {
666 Picture *ref_pic = &h->ref_list[list][ref];
667 int ref_field = ref_pic->f.reference - 1;
668 int ref_field_picture = ref_pic->field_picture;
669 int pic_height = 16 * h->mb_height >> ref_field_picture;
670
671 row <<= MB_MBAFF;
672 nrefs[list]--;
673
674 if (!FIELD_PICTURE && ref_field_picture) { // frame referencing two fields
675 ff_thread_await_progress(&ref_pic->f,
676 FFMIN((row >> 1) - !(row & 1),
677 pic_height - 1),
678 1);
679 ff_thread_await_progress(&ref_pic->f,
680 FFMIN((row >> 1), pic_height - 1),
681 0);
682 } else if (FIELD_PICTURE && !ref_field_picture) { // field referencing one field of a frame
683 ff_thread_await_progress(&ref_pic->f,
684 FFMIN(row * 2 + ref_field,
685 pic_height - 1),
686 0);
687 } else if (FIELD_PICTURE) {
688 ff_thread_await_progress(&ref_pic->f,
689 FFMIN(row, pic_height - 1),
690 ref_field);
691 } else {
692 ff_thread_await_progress(&ref_pic->f,
693 FFMIN(row, pic_height - 1),
694 0);
695 }
696 }
697 }
698 }
699
700 static av_always_inline void mc_dir_part(H264Context *h, Picture *pic,
701 int n, int square, int height,
702 int delta, int list,
703 uint8_t *dest_y, uint8_t *dest_cb,
704 uint8_t *dest_cr,
705 int src_x_offset, int src_y_offset,
706 qpel_mc_func *qpix_op,
707 h264_chroma_mc_func chroma_op,
708 int pixel_shift, int chroma_idc)
709 {
710 const int mx = h->mv_cache[list][scan8[n]][0] + src_x_offset * 8;
711 int my = h->mv_cache[list][scan8[n]][1] + src_y_offset * 8;
712 const int luma_xy = (mx & 3) + ((my & 3) << 2);
713 int offset = ((mx >> 2) << pixel_shift) + (my >> 2) * h->mb_linesize;
714 uint8_t *src_y = pic->f.data[0] + offset;
715 uint8_t *src_cb, *src_cr;
716 int extra_width = 0;
717 int extra_height = 0;
718 int emu = 0;
719 const int full_mx = mx >> 2;
720 const int full_my = my >> 2;
721 const int pic_width = 16 * h->mb_width;
722 const int pic_height = 16 * h->mb_height >> MB_FIELD;
723 int ysh;
724
725 if (mx & 7)
726 extra_width -= 3;
727 if (my & 7)
728 extra_height -= 3;
729
730 if (full_mx < 0 - extra_width ||
731 full_my < 0 - extra_height ||
732 full_mx + 16 /*FIXME*/ > pic_width + extra_width ||
733 full_my + 16 /*FIXME*/ > pic_height + extra_height) {
734 h->vdsp.emulated_edge_mc(h->edge_emu_buffer,
735 src_y - (2 << pixel_shift) - 2 * h->mb_linesize,
736 h->mb_linesize,
737 16 + 5, 16 + 5 /*FIXME*/, full_mx - 2,
738 full_my - 2, pic_width, pic_height);
739 src_y = h->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize;
740 emu = 1;
741 }
742
743 qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); // FIXME try variable height perhaps?
744 if (!square)
745 qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
746
747 if (CONFIG_GRAY && h->flags & CODEC_FLAG_GRAY)
748 return;
749
750 if (chroma_idc == 3 /* yuv444 */) {
751 src_cb = pic->f.data[1] + offset;
752 if (emu) {
753 h->vdsp.emulated_edge_mc(h->edge_emu_buffer,
754 src_cb - (2 << pixel_shift) - 2 * h->mb_linesize,
755 h->mb_linesize,
756 16 + 5, 16 + 5 /*FIXME*/,
757 full_mx - 2, full_my - 2,
758 pic_width, pic_height);
759 src_cb = h->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize;
760 }
761 qpix_op[luma_xy](dest_cb, src_cb, h->mb_linesize); // FIXME try variable height perhaps?
762 if (!square)
763 qpix_op[luma_xy](dest_cb + delta, src_cb + delta, h->mb_linesize);
764
765 src_cr = pic->f.data[2] + offset;
766 if (emu) {
767 h->vdsp.emulated_edge_mc(h->edge_emu_buffer,
768 src_cr - (2 << pixel_shift) - 2 * h->mb_linesize,
769 h->mb_linesize,
770 16 + 5, 16 + 5 /*FIXME*/,
771 full_mx - 2, full_my - 2,
772 pic_width, pic_height);
773 src_cr = h->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize;
774 }
775 qpix_op[luma_xy](dest_cr, src_cr, h->mb_linesize); // FIXME try variable height perhaps?
776 if (!square)
777 qpix_op[luma_xy](dest_cr + delta, src_cr + delta, h->mb_linesize);
778 return;
779 }
780
781 ysh = 3 - (chroma_idc == 2 /* yuv422 */);
782 if (chroma_idc == 1 /* yuv420 */ && MB_FIELD) {
783 // chroma offset when predicting from a field of opposite parity
784 my += 2 * ((h->mb_y & 1) - (pic->f.reference - 1));
785 emu |= (my >> 3) < 0 || (my >> 3) + 8 >= (pic_height >> 1);
786 }
787
788 src_cb = pic->f.data[1] + ((mx >> 3) << pixel_shift) +
789 (my >> ysh) * h->mb_uvlinesize;
790 src_cr = pic->f.data[2] + ((mx >> 3) << pixel_shift) +
791 (my >> ysh) * h->mb_uvlinesize;
792
793 if (emu) {
794 h->vdsp.emulated_edge_mc(h->edge_emu_buffer, src_cb, h->mb_uvlinesize,
795 9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
796 pic_width >> 1, pic_height >> (chroma_idc == 1 /* yuv420 */));
797 src_cb = h->edge_emu_buffer;
798 }
799 chroma_op(dest_cb, src_cb, h->mb_uvlinesize,
800 height >> (chroma_idc == 1 /* yuv420 */),
801 mx & 7, (my << (chroma_idc == 2 /* yuv422 */)) & 7);
802
803 if (emu) {
804 h->vdsp.emulated_edge_mc(h->edge_emu_buffer, src_cr, h->mb_uvlinesize,
805 9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
806 pic_width >> 1, pic_height >> (chroma_idc == 1 /* yuv420 */));
807 src_cr = h->edge_emu_buffer;
808 }
809 chroma_op(dest_cr, src_cr, h->mb_uvlinesize, height >> (chroma_idc == 1 /* yuv420 */),
810 mx & 7, (my << (chroma_idc == 2 /* yuv422 */)) & 7);
811 }
812
813 static av_always_inline void mc_part_std(H264Context *h, int n, int square,
814 int height, int delta,
815 uint8_t *dest_y, uint8_t *dest_cb,
816 uint8_t *dest_cr,
817 int x_offset, int y_offset,
818 qpel_mc_func *qpix_put,
819 h264_chroma_mc_func chroma_put,
820 qpel_mc_func *qpix_avg,
821 h264_chroma_mc_func chroma_avg,
822 int list0, int list1,
823 int pixel_shift, int chroma_idc)
824 {
825 qpel_mc_func *qpix_op = qpix_put;
826 h264_chroma_mc_func chroma_op = chroma_put;
827
828 dest_y += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
829 if (chroma_idc == 3 /* yuv444 */) {
830 dest_cb += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
831 dest_cr += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
832 } else if (chroma_idc == 2 /* yuv422 */) {
833 dest_cb += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
834 dest_cr += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
835 } else { /* yuv420 */
836 dest_cb += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
837 dest_cr += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
838 }
839 x_offset += 8 * h->mb_x;
840 y_offset += 8 * (h->mb_y >> MB_FIELD);
841
842 if (list0) {
843 Picture *ref = &h->ref_list[0][h->ref_cache[0][scan8[n]]];
844 mc_dir_part(h, ref, n, square, height, delta, 0,
845 dest_y, dest_cb, dest_cr, x_offset, y_offset,
846 qpix_op, chroma_op, pixel_shift, chroma_idc);
847
848 qpix_op = qpix_avg;
849 chroma_op = chroma_avg;
850 }
851
852 if (list1) {
853 Picture *ref = &h->ref_list[1][h->ref_cache[1][scan8[n]]];
854 mc_dir_part(h, ref, n, square, height, delta, 1,
855 dest_y, dest_cb, dest_cr, x_offset, y_offset,
856 qpix_op, chroma_op, pixel_shift, chroma_idc);
857 }
858 }
859
860 static av_always_inline void mc_part_weighted(H264Context *h, int n, int square,
861 int height, int delta,
862 uint8_t *dest_y, uint8_t *dest_cb,
863 uint8_t *dest_cr,
864 int x_offset, int y_offset,
865 qpel_mc_func *qpix_put,
866 h264_chroma_mc_func chroma_put,
867 h264_weight_func luma_weight_op,
868 h264_weight_func chroma_weight_op,
869 h264_biweight_func luma_weight_avg,
870 h264_biweight_func chroma_weight_avg,
871 int list0, int list1,
872 int pixel_shift, int chroma_idc)
873 {
874 int chroma_height;
875
876 dest_y += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
877 if (chroma_idc == 3 /* yuv444 */) {
878 chroma_height = height;
879 chroma_weight_avg = luma_weight_avg;
880 chroma_weight_op = luma_weight_op;
881 dest_cb += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
882 dest_cr += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
883 } else if (chroma_idc == 2 /* yuv422 */) {
884 chroma_height = height;
885 dest_cb += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
886 dest_cr += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
887 } else { /* yuv420 */
888 chroma_height = height >> 1;
889 dest_cb += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
890 dest_cr += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
891 }
892 x_offset += 8 * h->mb_x;
893 y_offset += 8 * (h->mb_y >> MB_FIELD);
894
895 if (list0 && list1) {
896 /* don't optimize for luma-only case, since B-frames usually
897 * use implicit weights => chroma too. */
898 uint8_t *tmp_cb = h->bipred_scratchpad;
899 uint8_t *tmp_cr = h->bipred_scratchpad + (16 << pixel_shift);
900 uint8_t *tmp_y = h->bipred_scratchpad + 16 * h->mb_uvlinesize;
901 int refn0 = h->ref_cache[0][scan8[n]];
902 int refn1 = h->ref_cache[1][scan8[n]];
903
904 mc_dir_part(h, &h->ref_list[0][refn0], n, square, height, delta, 0,
905 dest_y, dest_cb, dest_cr,
906 x_offset, y_offset, qpix_put, chroma_put,
907 pixel_shift, chroma_idc);
908 mc_dir_part(h, &h->ref_list[1][refn1], n, square, height, delta, 1,
909 tmp_y, tmp_cb, tmp_cr,
910 x_offset, y_offset, qpix_put, chroma_put,
911 pixel_shift, chroma_idc);
912
913 if (h->use_weight == 2) {
914 int weight0 = h->implicit_weight[refn0][refn1][h->mb_y & 1];
915 int weight1 = 64 - weight0;
916 luma_weight_avg(dest_y, tmp_y, h->mb_linesize,
917 height, 5, weight0, weight1, 0);
918 chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize,
919 chroma_height, 5, weight0, weight1, 0);
920 chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize,
921 chroma_height, 5, weight0, weight1, 0);
922 } else {
923 luma_weight_avg(dest_y, tmp_y, h->mb_linesize, height,
924 h->luma_log2_weight_denom,
925 h->luma_weight[refn0][0][0],
926 h->luma_weight[refn1][1][0],
927 h->luma_weight[refn0][0][1] +
928 h->luma_weight[refn1][1][1]);
929 chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, chroma_height,
930 h->chroma_log2_weight_denom,
931 h->chroma_weight[refn0][0][0][0],
932 h->chroma_weight[refn1][1][0][0],
933 h->chroma_weight[refn0][0][0][1] +
934 h->chroma_weight[refn1][1][0][1]);
935 chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, chroma_height,
936 h->chroma_log2_weight_denom,
937 h->chroma_weight[refn0][0][1][0],
938 h->chroma_weight[refn1][1][1][0],
939 h->chroma_weight[refn0][0][1][1] +
940 h->chroma_weight[refn1][1][1][1]);
941 }
942 } else {
943 int list = list1 ? 1 : 0;
944 int refn = h->ref_cache[list][scan8[n]];
945 Picture *ref = &h->ref_list[list][refn];
946 mc_dir_part(h, ref, n, square, height, delta, list,
947 dest_y, dest_cb, dest_cr, x_offset, y_offset,
948 qpix_put, chroma_put, pixel_shift, chroma_idc);
949
950 luma_weight_op(dest_y, h->mb_linesize, height,
951 h->luma_log2_weight_denom,
952 h->luma_weight[refn][list][0],
953 h->luma_weight[refn][list][1]);
954 if (h->use_weight_chroma) {
955 chroma_weight_op(dest_cb, h->mb_uvlinesize, chroma_height,
956 h->chroma_log2_weight_denom,
957 h->chroma_weight[refn][list][0][0],
958 h->chroma_weight[refn][list][0][1]);
959 chroma_weight_op(dest_cr, h->mb_uvlinesize, chroma_height,
960 h->chroma_log2_weight_denom,
961 h->chroma_weight[refn][list][1][0],
962 h->chroma_weight[refn][list][1][1]);
963 }
964 }
965 }
966
967 static av_always_inline void prefetch_motion(H264Context *h, int list,
968 int pixel_shift, int chroma_idc)
969 {
970 /* fetch pixels for estimated mv 4 macroblocks ahead
971 * optimized for 64byte cache lines */
972 const int refn = h->ref_cache[list][scan8[0]];
973 if (refn >= 0) {
974 const int mx = (h->mv_cache[list][scan8[0]][0] >> 2) + 16 * h->mb_x + 8;
975 const int my = (h->mv_cache[list][scan8[0]][1] >> 2) + 16 * h->mb_y;
976 uint8_t **src = h->ref_list[list][refn].f.data;
977 int off = (mx << pixel_shift) +
978 (my + (h->mb_x & 3) * 4) * h->mb_linesize +
979 (64 << pixel_shift);
980 h->vdsp.prefetch(src[0] + off, h->linesize, 4);
981 if (chroma_idc == 3 /* yuv444 */) {
982 h->vdsp.prefetch(src[1] + off, h->linesize, 4);
983 h->vdsp.prefetch(src[2] + off, h->linesize, 4);
984 } else {
985 off = ((mx >> 1) << pixel_shift) +
986 ((my >> 1) + (h->mb_x & 7)) * h->uvlinesize +
987 (64 << pixel_shift);
988 h->vdsp.prefetch(src[1] + off, src[2] - src[1], 2);
989 }
990 }
991 }
992
993 static void free_tables(H264Context *h, int free_rbsp)
994 {
995 int i;
996 H264Context *hx;
997
998 av_freep(&h->intra4x4_pred_mode);
999 av_freep(&h->chroma_pred_mode_table);
1000 av_freep(&h->cbp_table);
1001 av_freep(&h->mvd_table[0]);
1002 av_freep(&h->mvd_table[1]);
1003 av_freep(&h->direct_table);
1004 av_freep(&h->non_zero_count);
1005 av_freep(&h->slice_table_base);
1006 h->slice_table = NULL;
1007 av_freep(&h->list_counts);
1008
1009 av_freep(&h->mb2b_xy);
1010 av_freep(&h->mb2br_xy);
1011
1012 if (free_rbsp) {
1013 for (i = 0; i < h->picture_count && !h->avctx->internal->is_copy; i++)
1014 free_picture(h, &h->DPB[i]);
1015 av_freep(&h->DPB);
1016 h->picture_count = 0;
1017 } else if (h->DPB) {
1018 for (i = 0; i < h->picture_count; i++)
1019 h->DPB[i].needs_realloc = 1;
1020 }
1021
1022 h->cur_pic_ptr = NULL;
1023
1024 for (i = 0; i < MAX_THREADS; i++) {
1025 hx = h->thread_context[i];
1026 if (!hx)
1027 continue;
1028 av_freep(&hx->top_borders[1]);
1029 av_freep(&hx->top_borders[0]);
1030 av_freep(&hx->bipred_scratchpad);
1031 av_freep(&hx->edge_emu_buffer);
1032 av_freep(&hx->dc_val_base);
1033 av_freep(&hx->me.scratchpad);
1034 av_freep(&hx->er.mb_index2xy);
1035 av_freep(&hx->er.error_status_table);
1036 av_freep(&hx->er.er_temp_buffer);
1037 av_freep(&hx->er.mbintra_table);
1038 av_freep(&hx->er.mbskip_table);
1039
1040 if (free_rbsp) {
1041 av_freep(&hx->rbsp_buffer[1]);
1042 av_freep(&hx->rbsp_buffer[0]);
1043 hx->rbsp_buffer_size[0] = 0;
1044 hx->rbsp_buffer_size[1] = 0;
1045 }
1046 if (i)
1047 av_freep(&h->thread_context[i]);
1048 }
1049 }
1050
1051 static void init_dequant8_coeff_table(H264Context *h)
1052 {
1053 int i, j, q, x;
1054 const int max_qp = 51 + 6 * (h->sps.bit_depth_luma - 8);
1055
1056 for (i = 0; i < 6; i++) {
1057 h->dequant8_coeff[i] = h->dequant8_buffer[i];
1058 for (j = 0; j < i; j++)
1059 if (!memcmp(h->pps.scaling_matrix8[j], h->pps.scaling_matrix8[i],
1060 64 * sizeof(uint8_t))) {
1061 h->dequant8_coeff[i] = h->dequant8_buffer[j];
1062 break;
1063 }
1064 if (j < i)
1065 continue;
1066
1067 for (q = 0; q < max_qp + 1; q++) {
1068 int shift = div6[q];
1069 int idx = rem6[q];
1070 for (x = 0; x < 64; x++)
1071 h->dequant8_coeff[i][q][(x >> 3) | ((x & 7) << 3)] =
1072 ((uint32_t)dequant8_coeff_init[idx][dequant8_coeff_init_scan[((x >> 1) & 12) | (x & 3)]] *
1073 h->pps.scaling_matrix8[i][x]) << shift;
1074 }
1075 }
1076 }
1077
1078 static void init_dequant4_coeff_table(H264Context *h)
1079 {
1080 int i, j, q, x;
1081 const int max_qp = 51 + 6 * (h->sps.bit_depth_luma - 8);
1082 for (i = 0; i < 6; i++) {
1083 h->dequant4_coeff[i] = h->dequant4_buffer[i];
1084 for (j = 0; j < i; j++)
1085 if (!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i],
1086 16 * sizeof(uint8_t))) {
1087 h->dequant4_coeff[i] = h->dequant4_buffer[j];
1088 break;
1089 }
1090 if (j < i)
1091 continue;
1092
1093 for (q = 0; q < max_qp + 1; q++) {
1094 int shift = div6[q] + 2;
1095 int idx = rem6[q];
1096 for (x = 0; x < 16; x++)
1097 h->dequant4_coeff[i][q][(x >> 2) | ((x << 2) & 0xF)] =
1098 ((uint32_t)dequant4_coeff_init[idx][(x & 1) + ((x >> 2) & 1)] *
1099 h->pps.scaling_matrix4[i][x]) << shift;
1100 }
1101 }
1102 }
1103
1104 static void init_dequant_tables(H264Context *h)
1105 {
1106 int i, x;
1107 init_dequant4_coeff_table(h);
1108 if (h->pps.transform_8x8_mode)
1109 init_dequant8_coeff_table(h);
1110 if (h->sps.transform_bypass) {
1111 for (i = 0; i < 6; i++)
1112 for (x = 0; x < 16; x++)
1113 h->dequant4_coeff[i][0][x] = 1 << 6;
1114 if (h->pps.transform_8x8_mode)
1115 for (i = 0; i < 6; i++)
1116 for (x = 0; x < 64; x++)
1117 h->dequant8_coeff[i][0][x] = 1 << 6;
1118 }
1119 }
1120
1121 int ff_h264_alloc_tables(H264Context *h)
1122 {
1123 const int big_mb_num = h->mb_stride * (h->mb_height + 1);
1124 const int row_mb_num = h->mb_stride * 2 * h->avctx->thread_count;
1125 int x, y, i;
1126
1127 FF_ALLOCZ_OR_GOTO(h->avctx, h->intra4x4_pred_mode,
1128 row_mb_num * 8 * sizeof(uint8_t), fail)
1129 FF_ALLOCZ_OR_GOTO(h->avctx, h->non_zero_count,
1130 big_mb_num * 48 * sizeof(uint8_t), fail)
1131 FF_ALLOCZ_OR_GOTO(h->avctx, h->slice_table_base,
1132 (big_mb_num + h->mb_stride) * sizeof(*h->slice_table_base), fail)
1133 FF_ALLOCZ_OR_GOTO(h->avctx, h->cbp_table,
1134 big_mb_num * sizeof(uint16_t), fail)
1135 FF_ALLOCZ_OR_GOTO(h->avctx, h->chroma_pred_mode_table,
1136 big_mb_num * sizeof(uint8_t), fail)
1137 FF_ALLOCZ_OR_GOTO(h->avctx, h->mvd_table[0],
1138 16 * row_mb_num * sizeof(uint8_t), fail);
1139 FF_ALLOCZ_OR_GOTO(h->avctx, h->mvd_table[1],
1140 16 * row_mb_num * sizeof(uint8_t), fail);
1141 FF_ALLOCZ_OR_GOTO(h->avctx, h->direct_table,
1142 4 * big_mb_num * sizeof(uint8_t), fail);
1143 FF_ALLOCZ_OR_GOTO(h->avctx, h->list_counts,
1144 big_mb_num * sizeof(uint8_t), fail)
1145
1146 memset(h->slice_table_base, -1,
1147 (big_mb_num + h->mb_stride) * sizeof(*h->slice_table_base));
1148 h->slice_table = h->slice_table_base + h->mb_stride * 2 + 1;
1149
1150 FF_ALLOCZ_OR_GOTO(h->avctx, h->mb2b_xy,
1151 big_mb_num * sizeof(uint32_t), fail);
1152 FF_ALLOCZ_OR_GOTO(h->avctx, h->mb2br_xy,
1153 big_mb_num * sizeof(uint32_t), fail);
1154 for (y = 0; y < h->mb_height; y++)
1155 for (x = 0; x < h->mb_width; x++) {
1156 const int mb_xy = x + y * h->mb_stride;
1157 const int b_xy = 4 * x + 4 * y * h->b_stride;
1158
1159 h->mb2b_xy[mb_xy] = b_xy;
1160 h->mb2br_xy[mb_xy] = 8 * (FMO ? mb_xy : (mb_xy % (2 * h->mb_stride)));
1161 }
1162
1163 if (!h->dequant4_coeff[0])
1164 init_dequant_tables(h);
1165
1166 if (!h->DPB) {
1167 h->picture_count = MAX_PICTURE_COUNT * FFMAX(1, h->avctx->thread_count);
1168 h->DPB = av_mallocz_array(h->picture_count, sizeof(*h->DPB));
1169 if (!h->DPB)
1170 return AVERROR(ENOMEM);
1171 for (i = 0; i < h->picture_count; i++)
1172 avcodec_get_frame_defaults(&h->DPB[i].f);
1173 avcodec_get_frame_defaults(&h->cur_pic.f);
1174 }
1175
1176 return 0;
1177
1178 fail:
1179 free_tables(h, 1);
1180 return -1;
1181 }
1182
1183 /**
1184 * Mimic alloc_tables(), but for every context thread.
1185 */
1186 static void clone_tables(H264Context *dst, H264Context *src, int i)
1187 {
1188 dst->intra4x4_pred_mode = src->intra4x4_pred_mode + i * 8 * 2 * src->mb_stride;
1189 dst->non_zero_count = src->non_zero_count;
1190 dst->slice_table = src->slice_table;
1191 dst->cbp_table = src->cbp_table;
1192 dst->mb2b_xy = src->mb2b_xy;
1193 dst->mb2br_xy = src->mb2br_xy;
1194 dst->chroma_pred_mode_table = src->chroma_pred_mode_table;
1195 dst->mvd_table[0] = src->mvd_table[0] + i * 8 * 2 * src->mb_stride;
1196 dst->mvd_table[1] = src->mvd_table[1] + i * 8 * 2 * src->mb_stride;
1197 dst->direct_table = src->direct_table;
1198 dst->list_counts = src->list_counts;
1199 dst->DPB = src->DPB;
1200 dst->cur_pic_ptr = src->cur_pic_ptr;
1201 dst->cur_pic = src->cur_pic;
1202 dst->bipred_scratchpad = NULL;
1203 dst->edge_emu_buffer = NULL;
1204 dst->me.scratchpad = NULL;
1205 ff_h264_pred_init(&dst->hpc, src->avctx->codec_id, src->sps.bit_depth_luma,
1206 src->sps.chroma_format_idc);
1207 }
1208
1209 /**
1210 * Init context
1211 * Allocate buffers which are not shared amongst multiple threads.
1212 */
1213 static int context_init(H264Context *h)
1214 {
1215 ERContext *er = &h->er;
1216 int mb_array_size = h->mb_height * h->mb_stride;
1217 int y_size = (2 * h->mb_width + 1) * (2 * h->mb_height + 1);
1218 int c_size = h->mb_stride * (h->mb_height + 1);
1219 int yc_size = y_size + 2 * c_size;
1220 int x, y, i;
1221
1222 FF_ALLOCZ_OR_GOTO(h->avctx, h->top_borders[0],
1223 h->mb_width * 16 * 3 * sizeof(uint8_t) * 2, fail)
1224 FF_ALLOCZ_OR_GOTO(h->avctx, h->top_borders[1],
1225 h->mb_width * 16 * 3 * sizeof(uint8_t) * 2, fail)
1226
1227 h->ref_cache[0][scan8[5] + 1] =
1228 h->ref_cache[0][scan8[7] + 1] =
1229 h->ref_cache[0][scan8[13] + 1] =
1230 h->ref_cache[1][scan8[5] + 1] =
1231 h->ref_cache[1][scan8[7] + 1] =
1232 h->ref_cache[1][scan8[13] + 1] = PART_NOT_AVAILABLE;
1233
1234 /* init ER */
1235 er->avctx = h->avctx;
1236 er->dsp = &h->dsp;
1237 er->decode_mb = h264_er_decode_mb;
1238 er->opaque = h;
1239 er->quarter_sample = 1;
1240
1241 er->mb_num = h->mb_num;
1242 er->mb_width = h->mb_width;
1243 er->mb_height = h->mb_height;
1244 er->mb_stride = h->mb_stride;
1245 er->b8_stride = h->mb_width * 2 + 1;
1246
1247 FF_ALLOCZ_OR_GOTO(h->avctx, er->mb_index2xy, (h->mb_num + 1) * sizeof(int),
1248 fail); // error ressilience code looks cleaner with this
1249 for (y = 0; y < h->mb_height; y++)
1250 for (x = 0; x < h->mb_width; x++)
1251 er->mb_index2xy[x + y * h->mb_width] = x + y * h->mb_stride;
1252
1253 er->mb_index2xy[h->mb_height * h->mb_width] = (h->mb_height - 1) *
1254 h->mb_stride + h->mb_width;
1255
1256 FF_ALLOCZ_OR_GOTO(h->avctx, er->error_status_table,
1257 mb_array_size * sizeof(uint8_t), fail);
1258
1259 FF_ALLOC_OR_GOTO(h->avctx, er->mbintra_table, mb_array_size, fail);
1260 memset(er->mbintra_table, 1, mb_array_size);
1261
1262 FF_ALLOCZ_OR_GOTO(h->avctx, er->mbskip_table, mb_array_size + 2, fail);
1263
1264 FF_ALLOC_OR_GOTO(h->avctx, er->er_temp_buffer, h->mb_height * h->mb_stride,
1265 fail);
1266
1267 FF_ALLOCZ_OR_GOTO(h->avctx, h->dc_val_base, yc_size * sizeof(int16_t), fail);
1268 er->dc_val[0] = h->dc_val_base + h->mb_width * 2 + 2;
1269 er->dc_val[1] = h->dc_val_base + y_size + h->mb_stride + 1;
1270 er->dc_val[2] = er->dc_val[1] + c_size;
1271 for (i = 0; i < yc_size; i++)
1272 h->dc_val_base[i] = 1024;
1273
1274 return 0;
1275
1276 fail:
1277 return -1; // free_tables will clean up for us
1278 }
1279
1280 static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size,
1281 int parse_extradata);
1282
1283 static av_cold void common_init(H264Context *h)
1284 {
1285
1286 h->width = h->avctx->width;
1287 h->height = h->avctx->height;
1288
1289 h->bit_depth_luma = 8;
1290 h->chroma_format_idc = 1;
1291
1292 ff_h264dsp_init(&h->h264dsp, 8, 1);
1293 ff_h264chroma_init(&h->h264chroma, h->sps.bit_depth_chroma);
1294 ff_h264qpel_init(&h->h264qpel, 8);
1295 ff_h264_pred_init(&h->hpc, h->avctx->codec_id, 8, 1);
1296
1297 h->dequant_coeff_pps = -1;
1298
1299 /* needed so that IDCT permutation is known early */
1300 ff_dsputil_init(&h->dsp, h->avctx);
1301 ff_videodsp_init(&h->vdsp, 8);
1302
1303 memset(h->pps.scaling_matrix4, 16, 6 * 16 * sizeof(uint8_t));
1304 memset(h->pps.scaling_matrix8, 16, 2 * 64 * sizeof(uint8_t));
1305 }
1306
1307 int ff_h264_decode_extradata(H264Context *h)
1308 {
1309 AVCodecContext *avctx = h->avctx;
1310
1311 if (avctx->extradata[0] == 1) {
1312 int i, cnt, nalsize;
1313 unsigned char *p = avctx->extradata;
1314
1315 h->is_avc = 1;
1316
1317 if (avctx->extradata_size < 7) {
1318 av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
1319 return -1;
1320 }
1321 /* sps and pps in the avcC always have length coded with 2 bytes,
1322 * so put a fake nal_length_size = 2 while parsing them */
1323 h->nal_length_size = 2;
1324 // Decode sps from avcC
1325 cnt = *(p + 5) & 0x1f; // Number of sps
1326 p += 6;
1327 for (i = 0; i < cnt; i++) {
1328 nalsize = AV_RB16(p) + 2;
1329 if (p - avctx->extradata + nalsize > avctx->extradata_size)
1330 return -1;
1331 if (decode_nal_units(h, p, nalsize, 1) < 0) {
1332 av_log(avctx, AV_LOG_ERROR,
1333 "Decoding sps %d from avcC failed\n", i);
1334 return -1;
1335 }
1336 p += nalsize;
1337 }
1338 // Decode pps from avcC
1339 cnt = *(p++); // Number of pps
1340 for (i = 0; i < cnt; i++) {
1341 nalsize = AV_RB16(p) + 2;
1342 if (p - avctx->extradata + nalsize > avctx->extradata_size)
1343 return -1;
1344 if (decode_nal_units(h, p, nalsize, 1) < 0) {
1345 av_log(avctx, AV_LOG_ERROR,
1346 "Decoding pps %d from avcC failed\n", i);
1347 return -1;
1348 }
1349 p += nalsize;
1350 }
1351 // Now store right nal length size, that will be used to parse all other nals
1352 h->nal_length_size = (avctx->extradata[4] & 0x03) + 1;
1353 } else {
1354 h->is_avc = 0;
1355 if (decode_nal_units(h, avctx->extradata, avctx->extradata_size, 1) < 0)
1356 return -1;
1357 }
1358 return 0;
1359 }
1360
1361 av_cold int ff_h264_decode_init(AVCodecContext *avctx)
1362 {
1363 H264Context *h = avctx->priv_data;
1364 int i;
1365
1366 h->avctx = avctx;
1367 common_init(h);
1368
1369 h->picture_structure = PICT_FRAME;
1370 h->picture_range_start = 0;
1371 h->picture_range_end = MAX_PICTURE_COUNT;
1372 h->slice_context_count = 1;
1373 h->workaround_bugs = avctx->workaround_bugs;
1374 h->flags = avctx->flags;
1375
1376 /* set defaults */
1377 // s->decode_mb = ff_h263_decode_mb;
1378 if (!avctx->has_b_frames)
1379 h->low_delay = 1;
1380
1381 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
1382
1383 ff_h264_decode_init_vlc();
1384
1385 h->pixel_shift = 0;
1386 h->sps.bit_depth_luma = avctx->bits_per_raw_sample = 8;
1387
1388 h->thread_context[0] = h;
1389 h->outputed_poc = h->next_outputed_poc = INT_MIN;
1390 for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
1391 h->last_pocs[i] = INT_MIN;
1392 h->prev_poc_msb = 1 << 16;
1393 h->x264_build = -1;
1394 ff_h264_reset_sei(h);
1395 if (avctx->codec_id == AV_CODEC_ID_H264) {
1396 if (avctx->ticks_per_frame == 1)
1397 h->avctx->time_base.den *= 2;
1398 avctx->ticks_per_frame = 2;
1399 }
1400
1401 if (avctx->extradata_size > 0 && avctx->extradata &&
1402 ff_h264_decode_extradata(h))
1403 return -1;
1404
1405 if (h->sps.bitstream_restriction_flag &&
1406 h->avctx->has_b_frames < h->sps.num_reorder_frames) {
1407 h->avctx->has_b_frames = h->sps.num_reorder_frames;
1408 h->low_delay = 0;
1409 }
1410
1411 return 0;
1412 }
1413
1414 #define IN_RANGE(a, b, size) (((a) >= (b)) && ((a) < ((b) + (size))))
1415 #undef REBASE_PICTURE
1416 #define REBASE_PICTURE(pic, new_ctx, old_ctx) \
1417 ((pic && pic >= old_ctx->DPB && \
1418 pic < old_ctx->DPB + old_ctx->picture_count) ? \
1419 &new_ctx->DPB[pic - old_ctx->DPB] : NULL)
1420
1421 static void copy_picture_range(Picture **to, Picture **from, int count,
1422 H264Context *new_base,
1423 H264Context *old_base)
1424 {
1425 int i;
1426
1427 for (i = 0; i < count; i++) {
1428 assert((IN_RANGE(from[i], old_base, sizeof(*old_base)) ||
1429 IN_RANGE(from[i], old_base->DPB,
1430 sizeof(Picture) * old_base->picture_count) ||
1431 !from[i]));
1432 to[i] = REBASE_PICTURE(from[i], new_base, old_base);
1433 }
1434 }
1435
1436 static void copy_parameter_set(void **to, void **from, int count, int size)
1437 {
1438 int i;
1439
1440 for (i = 0; i < count; i++) {
1441 if (to[i] && !from[i])
1442 av_freep(&to[i]);
1443 else if (from[i] && !to[i])
1444 to[i] = av_malloc(size);
1445
1446 if (from[i])
1447 memcpy(to[i], from[i], size);
1448 }
1449 }
1450
1451 static int decode_init_thread_copy(AVCodecContext *avctx)
1452 {
1453 H264Context *h = avctx->priv_data;
1454
1455 if (!avctx->internal->is_copy)
1456 return 0;
1457 memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
1458 memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
1459
1460 h->context_initialized = 0;
1461
1462 return 0;
1463 }
1464
1465 #define copy_fields(to, from, start_field, end_field) \
1466 memcpy(&to->start_field, &from->start_field, \
1467 (char *)&to->end_field - (char *)&to->start_field)
1468
1469 static int h264_slice_header_init(H264Context *, int);
1470
1471 static int h264_set_parameter_from_sps(H264Context *h);
1472
1473 static int decode_update_thread_context(AVCodecContext *dst,
1474 const AVCodecContext *src)
1475 {
1476 H264Context *h = dst->priv_data, *h1 = src->priv_data;
1477 int inited = h->context_initialized, err = 0;
1478 int context_reinitialized = 0;
1479 int i;
1480
1481 if (dst == src || !h1->context_initialized)
1482 return 0;
1483
1484 if (inited &&
1485 (h->width != h1->width ||
1486 h->height != h1->height ||
1487 h->mb_width != h1->mb_width ||
1488 h->mb_height != h1->mb_height ||
1489 h->sps.bit_depth_luma != h1->sps.bit_depth_luma ||
1490 h->sps.chroma_format_idc != h1->sps.chroma_format_idc ||
1491 h->sps.colorspace != h1->sps.colorspace)) {
1492
1493 av_freep(&h->bipred_scratchpad);
1494
1495 h->width = h1->width;
1496 h->height = h1->height;
1497 h->mb_height = h1->mb_height;
1498 h->mb_width = h1->mb_width;
1499 h->mb_num = h1->mb_num;
1500 h->mb_stride = h1->mb_stride;
1501 h->b_stride = h1->b_stride;
1502
1503 if ((err = h264_slice_header_init(h, 1)) < 0) {
1504 av_log(h->avctx, AV_LOG_ERROR, "h264_slice_header_init() failed");
1505 return err;
1506 }
1507 context_reinitialized = 1;
1508
1509 /* update linesize on resize. The decoder doesn't
1510 * necessarily call ff_h264_frame_start in the new thread */
1511 h->linesize = h1->linesize;
1512 h->uvlinesize = h1->uvlinesize;
1513
1514 /* copy block_offset since frame_start may not be called */
1515 memcpy(h->block_offset, h1->block_offset, sizeof(h->block_offset));
1516 }
1517
1518 if (!inited) {
1519 for (i = 0; i < MAX_SPS_COUNT; i++)
1520 av_freep(h->sps_buffers + i);
1521
1522 for (i = 0; i < MAX_PPS_COUNT; i++)
1523 av_freep(h->pps_buffers + i);
1524
1525 memcpy(h, h1, sizeof(*h1));
1526 memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
1527 memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
1528 memset(&h->er, 0, sizeof(h->er));
1529 memset(&h->me, 0, sizeof(h->me));
1530 h->context_initialized = 0;
1531
1532 h->picture_range_start += MAX_PICTURE_COUNT;
1533 h->picture_range_end += MAX_PICTURE_COUNT;
1534
1535 h->avctx = dst;
1536 h->DPB = NULL;
1537 h->cur_pic.f.extended_data = h->cur_pic.f.data;
1538
1539 if (ff_h264_alloc_tables(h) < 0) {
1540 av_log(dst, AV_LOG_ERROR, "Could not allocate memory for h264\n");
1541 return AVERROR(ENOMEM);
1542 }
1543 context_init(h);
1544
1545 for (i = 0; i < 2; i++) {
1546 h->rbsp_buffer[i] = NULL;
1547 h->rbsp_buffer_size[i] = 0;
1548 }
1549 h->bipred_scratchpad = NULL;
1550 h->edge_emu_buffer = NULL;
1551
1552 h->thread_context[0] = h;
1553
1554 h->dsp.clear_blocks(h->mb);
1555 h->dsp.clear_blocks(h->mb + (24 * 16 << h->pixel_shift));
1556 h->context_initialized = 1;
1557 }
1558
1559 h->avctx->coded_height = h1->avctx->coded_height;
1560 h->avctx->coded_width = h1->avctx->coded_width;
1561 h->avctx->width = h1->avctx->width;
1562 h->avctx->height = h1->avctx->height;
1563 h->coded_picture_number = h1->coded_picture_number;
1564 h->first_field = h1->first_field;
1565 h->picture_structure = h1->picture_structure;
1566 h->qscale = h1->qscale;
1567 h->droppable = h1->droppable;
1568 h->data_partitioning = h1->data_partitioning;
1569 h->low_delay = h1->low_delay;
1570
1571 memcpy(h->DPB, h1->DPB, h1->picture_count * sizeof(*h1->DPB));
1572
1573 // reset s->picture[].f.extended_data to s->picture[].f.data
1574 for (i = 0; i < h->picture_count; i++)
1575 h->DPB[i].f.extended_data = h->DPB[i].f.data;
1576
1577 h->cur_pic_ptr = REBASE_PICTURE(h1->cur_pic_ptr, h, h1);
1578 h->cur_pic = h1->cur_pic;
1579 h->cur_pic.f.extended_data = h->cur_pic.f.data;
1580
1581 h->workaround_bugs = h1->workaround_bugs;
1582 h->low_delay = h1->low_delay;
1583 h->droppable = h1->droppable;
1584
1585 /* frame_start may not be called for the next thread (if it's decoding
1586 * a bottom field) so this has to be allocated here */
1587 err = alloc_scratch_buffers(h, h1->linesize);
1588 if (err < 0)
1589 return err;
1590
1591 // extradata/NAL handling
1592 h->is_avc = h1->is_avc;
1593
1594 // SPS/PPS
1595 copy_parameter_set((void **)h->sps_buffers, (void **)h1->sps_buffers,
1596 MAX_SPS_COUNT, sizeof(SPS));
1597 h->sps = h1->sps;
1598 copy_parameter_set((void **)h->pps_buffers, (void **)h1->pps_buffers,
1599 MAX_PPS_COUNT, sizeof(PPS));
1600 h->pps = h1->pps;
1601
1602 // Dequantization matrices
1603 // FIXME these are big - can they be only copied when PPS changes?
1604 copy_fields(h, h1, dequant4_buffer, dequant4_coeff);
1605
1606 for (i = 0; i < 6; i++)
1607 h->dequant4_coeff[i] = h->dequant4_buffer[0] +
1608 (h1->dequant4_coeff[i] - h1->dequant4_buffer[0]);
1609
1610 for (i = 0; i < 6; i++)
1611 h->dequant8_coeff[i] = h->dequant8_buffer[0] +
1612 (h1->dequant8_coeff[i] - h1->dequant8_buffer[0]);
1613
1614 h->dequant_coeff_pps = h1->dequant_coeff_pps;
1615
1616 // POC timing
1617 copy_fields(h, h1, poc_lsb, redundant_pic_count);
1618
1619 // reference lists
1620 copy_fields(h, h1, short_ref, cabac_init_idc);
1621
1622 copy_picture_range(h->short_ref, h1->short_ref, 32, h, h1);
1623 copy_picture_range(h->long_ref, h1->long_ref, 32, h, h1);
1624 copy_picture_range(h->delayed_pic, h1->delayed_pic,
1625 MAX_DELAYED_PIC_COUNT + 2, h, h1);
1626
1627 h->last_slice_type = h1->last_slice_type;
1628
1629 if (context_reinitialized)
1630 h264_set_parameter_from_sps(h);
1631
1632 if (!h->cur_pic_ptr)
1633 return 0;
1634
1635 if (!h->droppable) {
1636 err = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
1637 h->prev_poc_msb = h->poc_msb;
1638 h->prev_poc_lsb = h->poc_lsb;
1639 }
1640 h->prev_frame_num_offset = h->frame_num_offset;
1641 h->prev_frame_num = h->frame_num;
1642 h->outputed_poc = h->next_outputed_poc;
1643
1644 return err;
1645 }
1646
1647 int ff_h264_frame_start(H264Context *h)
1648 {
1649 Picture *pic;
1650 int i, ret;
1651 const int pixel_shift = h->pixel_shift;
1652
1653 release_unused_pictures(h, 1);
1654 h->cur_pic_ptr = NULL;
1655
1656 i = find_unused_picture(h);
1657 if (i < 0) {
1658 av_log(h->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1659 return i;
1660 }
1661 pic = &h->DPB[i];
1662
1663 pic->f.reference = h->droppable ? 0 : h->picture_structure;
1664 pic->f.coded_picture_number = h->coded_picture_number++;
1665 pic->field_picture = h->picture_structure != PICT_FRAME;
1666 /*
1667 * Zero key_frame here; IDR markings per slice in frame or fields are ORed
1668 * in later.
1669 * See decode_nal_units().
1670 */
1671 pic->f.key_frame = 0;
1672 pic->mmco_reset = 0;
1673
1674 if ((ret = alloc_picture(h, pic)) < 0)
1675 return ret;
1676
1677 h->cur_pic_ptr = pic;
1678 h->cur_pic = *h->cur_pic_ptr;
1679 h->cur_pic.f.extended_data = h->cur_pic.f.data;
1680
1681 ff_er_frame_start(&h->er);
1682
1683 assert(h->linesize && h->uvlinesize);
1684
1685 for (i = 0; i < 16; i++) {
1686 h->block_offset[i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * h->linesize * ((scan8[i] - scan8[0]) >> 3);
1687 h->block_offset[48 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * h->linesize * ((scan8[i] - scan8[0]) >> 3);
1688 }
1689 for (i = 0; i < 16; i++) {
1690 h->block_offset[16 + i] =
1691 h->block_offset[32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * h->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
1692 h->block_offset[48 + 16 + i] =
1693 h->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * h->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
1694 }
1695
1696 /* can't be in alloc_tables because linesize isn't known there.
1697 * FIXME: redo bipred weight to not require extra buffer? */
1698 for (i = 0; i < h->slice_context_count; i++)
1699 if (h->thread_context[i]) {
1700 ret = alloc_scratch_buffers(h->thread_context[i], h->linesize);
1701 if (ret < 0)
1702 return ret;
1703 }
1704
1705 /* Some macroblocks can be accessed before they're available in case
1706 * of lost slices, MBAFF or threading. */
1707 memset(h->slice_table, -1,
1708 (h->mb_height * h->mb_stride - 1) * sizeof(*h->slice_table));
1709
1710 // s->decode = (s->flags & CODEC_FLAG_PSNR) || !s->encoding ||
1711 // s->current_picture.f.reference /* || h->contains_intra */ || 1;
1712
1713 /* We mark the current picture as non-reference after allocating it, so
1714 * that if we break out due to an error it can be released automatically
1715 * in the next ff_MPV_frame_start().
1716 * SVQ3 as well as most other codecs have only last/next/current and thus
1717 * get released even with set reference, besides SVQ3 and others do not
1718 * mark frames as reference later "naturally". */
1719 if (h->avctx->codec_id != AV_CODEC_ID_SVQ3)
1720 h->cur_pic_ptr->f.reference = 0;
1721
1722 h->cur_pic_ptr->field_poc[0] = h->cur_pic_ptr->field_poc[1] = INT_MAX;
1723
1724 h->next_output_pic = NULL;
1725
1726 assert(h->cur_pic_ptr->long_ref == 0);
1727
1728 return 0;
1729 }
1730
1731 /**
1732 * Run setup operations that must be run after slice header decoding.
1733 * This includes finding the next displayed frame.
1734 *
1735 * @param h h264 master context
1736 * @param setup_finished enough NALs have been read that we can call
1737 * ff_thread_finish_setup()
1738 */
1739 static void decode_postinit(H264Context *h, int setup_finished)
1740 {
1741 Picture *out = h->cur_pic_ptr;
1742 Picture *cur = h->cur_pic_ptr;
1743 int i, pics, out_of_order, out_idx;
1744 int invalid = 0, cnt = 0;
1745
1746 h->cur_pic_ptr->f.qscale_type = FF_QSCALE_TYPE_H264;
1747 h->cur_pic_ptr->f.pict_type = h->pict_type;
1748
1749 if (h->next_output_pic)
1750 return;
1751
1752 if (cur->field_poc[0] == INT_MAX || cur->field_poc[1] == INT_MAX) {
1753 /* FIXME: if we have two PAFF fields in one packet, we can't start
1754 * the next thread here. If we have one field per packet, we can.
1755 * The check in decode_nal_units() is not good enough to find this
1756 * yet, so we assume the worst for now. */
1757 // if (setup_finished)
1758 // ff_thread_finish_setup(h->avctx);
1759 return;
1760 }
1761
1762 cur->f.interlaced_frame = 0;
1763 cur->f.repeat_pict = 0;
1764
1765 /* Signal interlacing information externally. */
1766 /* Prioritize picture timing SEI information over used
1767 * decoding process if it exists. */
1768
1769 if (h->sps.pic_struct_present_flag) {
1770 switch (h->sei_pic_struct) {
1771 case SEI_PIC_STRUCT_FRAME:
1772 break;
1773 case SEI_PIC_STRUCT_TOP_FIELD:
1774 case SEI_PIC_STRUCT_BOTTOM_FIELD:
1775 cur->f.interlaced_frame = 1;
1776 break;
1777 case SEI_PIC_STRUCT_TOP_BOTTOM:
1778 case SEI_PIC_STRUCT_BOTTOM_TOP:
1779 if (FIELD_OR_MBAFF_PICTURE)
1780 cur->f.interlaced_frame = 1;
1781 else
1782 // try to flag soft telecine progressive
1783 cur->f.interlaced_frame = h->prev_interlaced_frame;
1784 break;
1785 case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
1786 case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
1787 /* Signal the possibility of telecined film externally
1788 * (pic_struct 5,6). From these hints, let the applications
1789 * decide if they apply deinterlacing. */
1790 cur->f.repeat_pict = 1;
1791 break;
1792 case SEI_PIC_STRUCT_FRAME_DOUBLING:
1793 cur->f.repeat_pict = 2;
1794 break;
1795 case SEI_PIC_STRUCT_FRAME_TRIPLING:
1796 cur->f.repeat_pict = 4;
1797 break;
1798 }
1799
1800 if ((h->sei_ct_type & 3) &&
1801 h->sei_pic_struct <= SEI_PIC_STRUCT_BOTTOM_TOP)
1802 cur->f.interlaced_frame = (h->sei_ct_type & (1 << 1)) != 0;
1803 } else {
1804 /* Derive interlacing flag from used decoding process. */
1805 cur->f.interlaced_frame = FIELD_OR_MBAFF_PICTURE;
1806 }
1807 h->prev_interlaced_frame = cur->f.interlaced_frame;
1808
1809 if (cur->field_poc[0] != cur->field_poc[1]) {
1810 /* Derive top_field_first from field pocs. */
1811 cur->f.top_field_first = cur->field_poc[0] < cur->field_poc[1];
1812 } else {
1813 if (cur->f.interlaced_frame || h->sps.pic_struct_present_flag) {
1814 /* Use picture timing SEI information. Even if it is a
1815 * information of a past frame, better than nothing. */
1816 if (h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM ||
1817 h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
1818 cur->f.top_field_first = 1;
1819 else
1820 cur->f.top_field_first = 0;
1821 } else {
1822 /* Most likely progressive */
1823 cur->f.top_field_first = 0;
1824 }
1825 }
1826
1827 // FIXME do something with unavailable reference frames
1828
1829 /* Sort B-frames into display order */
1830
1831 if (h->sps.bitstream_restriction_flag &&
1832 h->avctx->has_b_frames < h->sps.num_reorder_frames) {
1833 h->avctx->has_b_frames = h->sps.num_reorder_frames;
1834 h->low_delay = 0;
1835 }
1836
1837 if (h->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT &&
1838 !h->sps.bitstream_restriction_flag) {
1839 h->avctx->has_b_frames = MAX_DELAYED_PIC_COUNT - 1;
1840 h->low_delay = 0;
1841 }
1842
1843 pics = 0;
1844 while (h->delayed_pic[pics])
1845 pics++;
1846
1847 assert(pics <= MAX_DELAYED_PIC_COUNT);
1848
1849 h->delayed_pic[pics++] = cur;
1850 if (cur->f.reference == 0)
1851 cur->f.reference = DELAYED_PIC_REF;
1852
1853 /* Frame reordering. This code takes pictures from coding order and sorts
1854 * them by their incremental POC value into display order. It supports POC
1855 * gaps, MMCO reset codes and random resets.
1856 * A "display group" can start either with a IDR frame (f.key_frame = 1),
1857 * and/or can be closed down with a MMCO reset code. In sequences where
1858 * there is no delay, we can't detect that (since the frame was already
1859 * output to the user), so we also set h->mmco_reset to detect the MMCO
1860 * reset code.
1861 * FIXME: if we detect insufficient delays (as per h->avctx->has_b_frames),
1862 * we increase the delay between input and output. All frames affected by
1863 * the lag (e.g. those that should have been output before another frame
1864 * that we already returned to the user) will be dropped. This is a bug
1865 * that we will fix later. */
1866 for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++) {
1867 cnt += out->poc < h->last_pocs[i];
1868 invalid += out->poc == INT_MIN;
1869 }
1870 if (!h->mmco_reset && !cur->f.key_frame &&
1871 cnt + invalid == MAX_DELAYED_PIC_COUNT && cnt > 0) {
1872 h->mmco_reset = 2;
1873 if (pics > 1)
1874 h->delayed_pic[pics - 2]->mmco_reset = 2;
1875 }
1876 if (h->mmco_reset || cur->f.key_frame) {
1877 for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
1878 h->last_pocs[i] = INT_MIN;
1879 cnt = 0;
1880 invalid = MAX_DELAYED_PIC_COUNT;
1881 }
1882 out = h->delayed_pic[0];
1883 out_idx = 0;
1884 for (i = 1; i < MAX_DELAYED_PIC_COUNT &&
1885 h->delayed_pic[i] &&
1886 !h->delayed_pic[i - 1]->mmco_reset &&
1887 !h->delayed_pic[i]->f.key_frame;
1888 i++)
1889 if (h->delayed_pic[i]->poc < out->poc) {
1890 out = h->delayed_pic[i];
1891 out_idx = i;
1892 }
1893 if (h->avctx->has_b_frames == 0 &&
1894 (h->delayed_pic[0]->f.key_frame || h->mmco_reset))
1895 h->next_outputed_poc = INT_MIN;
1896 out_of_order = !out->f.key_frame && !h->mmco_reset &&
1897 (out->poc < h->next_outputed_poc);
1898
1899 if (h->sps.bitstream_restriction_flag &&
1900 h->avctx->has_b_frames >= h->sps.num_reorder_frames) {
1901 } else if (out_of_order && pics - 1 == h->avctx->has_b_frames &&
1902 h->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT) {
1903 if (invalid + cnt < MAX_DELAYED_PIC_COUNT) {
1904 h->avctx->has_b_frames = FFMAX(h->avctx->has_b_frames, cnt);
1905 }
1906 h->low_delay = 0;
1907 } else if (h->low_delay &&
1908 ((h->next_outputed_poc != INT_MIN &&
1909 out->poc > h->next_outputed_poc + 2) ||
1910 cur->f.pict_type == AV_PICTURE_TYPE_B)) {
1911 h->low_delay = 0;
1912 h->avctx->has_b_frames++;
1913 }
1914
1915 if (pics > h->avctx->has_b_frames) {
1916 out->f.reference &= ~DELAYED_PIC_REF;
1917 // for frame threading, the owner must be the second field's thread or
1918 // else the first thread can release the picture and reuse it unsafely
1919 out->owner2 = h;
1920 for (i = out_idx; h->delayed_pic[i]; i++)
1921 h->delayed_pic[i] = h->delayed_pic[i + 1];
1922 }
1923 memmove(h->last_pocs, &h->last_pocs[1],
1924 sizeof(*h->last_pocs) * (MAX_DELAYED_PIC_COUNT - 1));
1925 h->last_pocs[MAX_DELAYED_PIC_COUNT - 1] = cur->poc;
1926 if (!out_of_order && pics > h->avctx->has_b_frames) {
1927 h->next_output_pic = out;
1928 if (out->mmco_reset) {
1929 if (out_idx > 0) {
1930 h->next_outputed_poc = out->poc;
1931 h->delayed_pic[out_idx - 1]->mmco_reset = out->mmco_reset;
1932 } else {
1933 h->next_outputed_poc = INT_MIN;
1934 }
1935 } else {
1936 if (out_idx == 0 && pics > 1 && h->delayed_pic[0]->f.key_frame) {
1937 h->next_outputed_poc = INT_MIN;
1938 } else {
1939 h->next_outputed_poc = out->poc;
1940 }
1941 }
1942 h->mmco_reset = 0;
1943 } else {
1944 av_log(h->avctx, AV_LOG_DEBUG, "no picture\n");
1945 }
1946
1947 if (setup_finished)
1948 ff_thread_finish_setup(h->avctx);
1949 }
1950
1951 static av_always_inline void backup_mb_border(H264Context *h, uint8_t *src_y,
1952 uint8_t *src_cb, uint8_t *src_cr,
1953 int linesize, int uvlinesize,
1954 int simple)
1955 {
1956 uint8_t *top_border;
1957 int top_idx = 1;
1958 const int pixel_shift = h->pixel_shift;
1959 int chroma444 = CHROMA444;
1960 int chroma422 = CHROMA422;
1961
1962 src_y -= linesize;
1963 src_cb -= uvlinesize;
1964 src_cr -= uvlinesize;
1965
1966 if (!simple && FRAME_MBAFF) {
1967 if (h->mb_y & 1) {
1968 if (!MB_MBAFF) {
1969 top_border = h->top_borders[0][h->mb_x];
1970 AV_COPY128(top_border, src_y + 15 * linesize);
1971 if (pixel_shift)
1972 AV_COPY128(top_border + 16, src_y + 15 * linesize + 16);
1973 if (simple || !CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) {
1974 if (chroma444) {
1975 if (pixel_shift) {
1976 AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
1977 AV_COPY128(top_border + 48, src_cb + 15 * uvlinesize + 16);
1978 AV_COPY128(top_border + 64, src_cr + 15 * uvlinesize);
1979 AV_COPY128(top_border + 80, src_cr + 15 * uvlinesize + 16);
1980 } else {
1981 AV_COPY128(top_border + 16, src_cb + 15 * uvlinesize);
1982 AV_COPY128(top_border + 32, src_cr + 15 * uvlinesize);
1983 }
1984 } else if (chroma422) {
1985 if (pixel_shift) {
1986 AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
1987 AV_COPY128(top_border + 48, src_cr + 15 * uvlinesize);
1988 } else {
1989 AV_COPY64(top_border + 16, src_cb + 15 * uvlinesize);
1990 AV_COPY64(top_border + 24, src_cr + 15 * uvlinesize);
1991 }
1992 } else {
1993 if (pixel_shift) {
1994 AV_COPY128(top_border + 32, src_cb + 7 * uvlinesize);
1995 AV_COPY128(top_border + 48, src_cr + 7 * uvlinesize);
1996 } else {
1997 AV_COPY64(top_border + 16, src_cb + 7 * uvlinesize);
1998 AV_COPY64(top_border + 24, src_cr + 7 * uvlinesize);
1999 }
2000 }
2001 }
2002 }
2003 } else if (MB_MBAFF) {
2004 top_idx = 0;
2005 } else
2006 return;
2007 }
2008
2009 top_border = h->top_borders[top_idx][h->mb_x];
2010 /* There are two lines saved, the line above the top macroblock
2011 * of a pair, and the line above the bottom macroblock. */
2012 AV_COPY128(top_border, src_y + 16 * linesize);
2013 if (pixel_shift)
2014 AV_COPY128(top_border + 16, src_y + 16 * linesize + 16);
2015
2016 if (simple || !CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) {
2017 if (chroma444) {
2018 if (pixel_shift) {
2019 AV_COPY128(top_border + 32, src_cb + 16 * linesize);
2020 AV_COPY128(top_border + 48, src_cb + 16 * linesize + 16);
2021 AV_COPY128(top_border + 64, src_cr + 16 * linesize);
2022 AV_COPY128(top_border + 80, src_cr + 16 * linesize + 16);
2023 } else {
2024 AV_COPY128(top_border + 16, src_cb + 16 * linesize);
2025 AV_COPY128(top_border + 32, src_cr + 16 * linesize);
2026 }
2027 } else if (chroma422) {
2028 if (pixel_shift) {
2029 AV_COPY128(top_border + 32, src_cb + 16 * uvlinesize);
2030 AV_COPY128(top_border + 48, src_cr + 16 * uvlinesize);
2031 } else {
2032 AV_COPY64(top_border + 16, src_cb + 16 * uvlinesize);
2033 AV_COPY64(top_border + 24, src_cr + 16 * uvlinesize);
2034 }
2035 } else {
2036 if (pixel_shift) {
2037 AV_COPY128(top_border + 32, src_cb + 8 * uvlinesize);
2038 AV_COPY128(top_border + 48, src_cr + 8 * uvlinesize);
2039 } else {
2040 AV_COPY64(top_border + 16, src_cb + 8 * uvlinesize);
2041 AV_COPY64(top_border + 24, src_cr + 8 * uvlinesize);
2042 }
2043 }
2044 }
2045 }
2046
2047 static av_always_inline void xchg_mb_border(H264Context *h, uint8_t *src_y,
2048 uint8_t *src_cb, uint8_t *src_cr,
2049 int linesize, int uvlinesize,
2050 int xchg, int chroma444,
2051 int simple, int pixel_shift)
2052 {
2053 int deblock_topleft;
2054 int deblock_top;
2055 int top_idx = 1;
2056 uint8_t *top_border_m1;
2057 uint8_t *top_border;
2058
2059 if (!simple && FRAME_MBAFF) {
2060 if (h->mb_y & 1) {
2061 if (!MB_MBAFF)
2062 return;
2063 } else {
2064 top_idx = MB_MBAFF ? 0 : 1;
2065 }
2066 }
2067
2068 if (h->deblocking_filter == 2) {
2069 deblock_topleft = h->slice_table[h->mb_xy - 1 - h->mb_stride] == h->slice_num;
2070 deblock_top = h->top_type;
2071 } else {
2072 deblock_topleft = (h->mb_x > 0);
2073 deblock_top = (h->mb_y > !!MB_FIELD);
2074 }
2075
2076 src_y -= linesize + 1 + pixel_shift;
2077 src_cb -= uvlinesize + 1 + pixel_shift;
2078 src_cr -= uvlinesize + 1 + pixel_shift;
2079
2080 top_border_m1 = h->top_borders[top_idx][h->mb_x - 1];
2081 top_border = h->top_borders[top_idx][h->mb_x];
2082
2083 #define XCHG(a, b, xchg) \
2084 if (pixel_shift) { \
2085 if (xchg) { \
2086 AV_SWAP64(b + 0, a + 0); \
2087 AV_SWAP64(b + 8, a + 8); \
2088 } else { \
2089 AV_COPY128(b, a); \
2090 } \
2091 } else if (xchg) \
2092 AV_SWAP64(b, a); \
2093 else \
2094 AV_COPY64(b, a);
2095
2096 if (deblock_top) {
2097 if (deblock_topleft) {
2098 XCHG(top_border_m1 + (8 << pixel_shift),
2099 src_y - (7 << pixel_shift), 1);
2100 }
2101 XCHG(top_border + (0 << pixel_shift), src_y + (1 << pixel_shift), xchg);
2102 XCHG(top_border + (8 << pixel_shift), src_y + (9 << pixel_shift), 1);
2103 if (h->mb_x + 1 < h->mb_width) {
2104 XCHG(h->top_borders[top_idx][h->mb_x + 1],
2105 src_y + (17 << pixel_shift), 1);
2106 }
2107 }
2108 if (simple || !CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) {
2109 if (chroma444) {
2110 if (deblock_topleft) {
2111 XCHG(top_border_m1 + (24 << pixel_shift), src_cb - (7 << pixel_shift), 1);
2112 XCHG(top_border_m1 + (40 << pixel_shift), src_cr - (7 << pixel_shift), 1);
2113 }
2114 XCHG(top_border + (16 << pixel_shift), src_cb + (1 << pixel_shift), xchg);
2115 XCHG(top_border + (24 << pixel_shift), src_cb + (9 << pixel_shift), 1);
2116 XCHG(top_border + (32 << pixel_shift), src_cr + (1 << pixel_shift), xchg);
2117 XCHG(top_border + (40 << pixel_shift), src_cr + (9 << pixel_shift), 1);
2118 if (h->mb_x + 1 < h->mb_width) {
2119 XCHG(h->top_borders[top_idx][h->mb_x + 1] + (16 << pixel_shift), src_cb + (17 << pixel_shift), 1);
2120 XCHG(h->top_borders[top_idx][h->mb_x + 1] + (32 << pixel_shift), src_cr + (17 << pixel_shift), 1);
2121 }
2122 } else {
2123 if (deblock_top) {
2124 if (deblock_topleft) {
2125 XCHG(top_border_m1 + (16 << pixel_shift), src_cb - (7 << pixel_shift), 1);
2126 XCHG(top_border_m1 + (24 << pixel_shift), src_cr - (7 << pixel_shift), 1);
2127 }
2128 XCHG(top_border + (16 << pixel_shift), src_cb + 1 + pixel_shift, 1);
2129 XCHG(top_border + (24 << pixel_shift), src_cr + 1 + pixel_shift, 1);
2130 }
2131 }
2132 }
2133 }
2134
2135 static av_always_inline int dctcoef_get(int16_t *mb, int high_bit_depth,
2136 int index)
2137 {
2138 if (high_bit_depth) {
2139 return AV_RN32A(((int32_t *)mb) + index);
2140 } else
2141 return AV_RN16A(mb + index);
2142 }
2143
2144 static av_always_inline void dctcoef_set(int16_t *mb, int high_bit_depth,
2145 int index, int value)
2146 {
2147 if (high_bit_depth) {
2148 AV_WN32A(((int32_t *)mb) + index, value);
2149 } else
2150 AV_WN16A(mb + index, value);
2151 }
2152
2153 static av_always_inline void hl_decode_mb_predict_luma(H264Context *h,
2154 int mb_type, int is_h264,
2155 int simple,
2156 int transform_bypass,
2157 int pixel_shift,
2158 int *block_offset,
2159 int linesize,
2160 uint8_t *dest_y, int p)
2161 {
2162 void (*idct_add)(uint8_t *dst, int16_t *block, int stride);
2163 void (*idct_dc_add)(uint8_t *dst, int16_t *block, int stride);
2164 int i;
2165 int qscale = p == 0 ? h->qscale : h->chroma_qp[p - 1];
2166 block_offset += 16 * p;
2167 if (IS_INTRA4x4(mb_type)) {
2168 if (IS_8x8DCT(mb_type)) {
2169 if (transform_bypass) {
2170 idct_dc_add =
2171 idct_add = h->h264dsp.h264_add_pixels8;
2172 } else {
2173 idct_dc_add = h->h264dsp.h264_idct8_dc_add;
2174 idct_add = h->h264dsp.h264_idct8_add;
2175 }
2176 for (i = 0; i < 16; i += 4) {
2177 uint8_t *const ptr = dest_y + block_offset[i];
2178 const int dir = h->intra4x4_pred_mode_cache[scan8[i]];
2179 if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) {
2180 h->hpc.pred8x8l_add[dir](ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
2181 } else {
2182 const int nnz = h->non_zero_count_cache[scan8[i + p * 16]];
2183 h->hpc.pred8x8l[dir](ptr, (h->topleft_samples_available << i) & 0x8000,
2184 (h->topright_samples_available << i) & 0x4000, linesize);
2185 if (nnz) {
2186 if (nnz == 1 && dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
2187 idct_dc_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
2188 else
2189 idct_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
2190 }
2191 }
2192 }
2193 } else {
2194 if (transform_bypass) {
2195 idct_dc_add =
2196 idct_add = h->h264dsp.h264_add_pixels4;
2197 } else {
2198 idct_dc_add = h->h264dsp.h264_idct_dc_add;
2199 idct_add = h->h264dsp.h264_idct_add;
2200 }
2201 for (i = 0; i < 16; i++) {
2202 uint8_t *const ptr = dest_y + block_offset[i];
2203 const int dir = h->intra4x4_pred_mode_cache[scan8[i]];
2204
2205 if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) {
2206 h->hpc.pred4x4_add[dir](ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
2207 } else {
2208 uint8_t *topright;
2209 int nnz, tr;
2210 uint64_t tr_high;
2211 if (dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED) {
2212 const int topright_avail = (h->topright_samples_available << i) & 0x8000;
2213 assert(h->mb_y || linesize <= block_offset[i]);
2214 if (!topright_avail) {
2215 if (pixel_shift) {
2216 tr_high = ((uint16_t *)ptr)[3 - linesize / 2] * 0x0001000100010001ULL;
2217 topright = (uint8_t *)&tr_high;
2218 } else {
2219 tr = ptr[3 - linesize] * 0x01010101u;
2220 topright = (uint8_t *)&tr;
2221 }
2222 } else
2223 topright = ptr + (4 << pixel_shift) - linesize;
2224 } else
2225 topright = NULL;
2226
2227 h->hpc.pred4x4[dir](ptr, topright, linesize);
2228 nnz = h->non_zero_count_cache[scan8[i + p * 16]];
2229 if (nnz) {
2230 if (is_h264) {
2231 if (nnz == 1 && dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
2232 idct_dc_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
2233 else
2234 idct_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
2235 } else if (CONFIG_SVQ3_DECODER)
2236 ff_svq3_add_idct_c(ptr, h->mb + i * 16 + p * 256, linesize, qscale, 0);
2237 }
2238 }
2239 }
2240 }
2241 } else {
2242 h->hpc.pred16x16[h->intra16x16_pred_mode](dest_y, linesize);
2243 if (is_h264) {
2244 if (h->non_zero_count_cache[scan8[LUMA_DC_BLOCK_INDEX + p]]) {
2245 if (!transform_bypass)
2246 h->h264dsp.h264_luma_dc_dequant_idct(h->mb + (p * 256 << pixel_shift),
2247 h->mb_luma_dc[p],
2248 h->dequant4_coeff[p][qscale][0]);
2249 else {
2250 static const uint8_t dc_mapping[16] = {
2251 0 * 16, 1 * 16, 4 * 16, 5 * 16,
2252 2 * 16, 3 * 16, 6 * 16, 7 * 16,
2253 8 * 16, 9 * 16, 12 * 16, 13 * 16,
2254 10 * 16, 11 * 16, 14 * 16, 15 * 16 };
2255 for (i = 0; i < 16; i++)
2256 dctcoef_set(h->mb + (p * 256 << pixel_shift),
2257 pixel_shift, dc_mapping[i],
2258 dctcoef_get(h->mb_luma_dc[p],
2259 pixel_shift, i));
2260 }
2261 }
2262 } else if (CONFIG_SVQ3_DECODER)
2263 ff_svq3_luma_dc_dequant_idct_c(h->mb + p * 256,
2264 h->mb_luma_dc[p], qscale);
2265 }
2266 }
2267
2268 static av_always_inline void hl_decode_mb_idct_luma(H264Context *h, int mb_type,
2269 int is_h264, int simple,
2270 int transform_bypass,
2271 int pixel_shift,
2272 int *block_offset,
2273 int linesize,
2274 uint8_t *dest_y, int p)
2275 {
2276 void (*idct_add)(uint8_t *dst, int16_t *block, int stride);
2277 int i;
2278 block_offset += 16 * p;
2279 if (!IS_INTRA4x4(mb_type)) {
2280 if (is_h264) {
2281 if (IS_INTRA16x16(mb_type)) {
2282 if (transform_bypass) {
2283 if (h->sps.profile_idc == 244 &&
2284 (h->intra16x16_pred_mode == VERT_PRED8x8 ||
2285 h->intra16x16_pred_mode == HOR_PRED8x8)) {
2286 h->hpc.pred16x16_add[h->intra16x16_pred_mode](dest_y, block_offset,
2287 h->mb + (p * 256 << pixel_shift),
2288 linesize);
2289 } else {
2290 for (i = 0; i < 16; i++)
2291 if (h->non_zero_count_cache[scan8[i + p * 16]] ||
2292 dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
2293 h->h264dsp.h264_add_pixels4(dest_y + block_offset[i],
2294 h->mb + (i * 16 + p * 256 << pixel_shift),
2295 linesize);
2296 }
2297 } else {
2298 h->h264dsp.h264_idct_add16intra(dest_y, block_offset,
2299 h->mb + (p * 256 << pixel_shift),
2300 linesize,
2301 h->non_zero_count_cache + p * 5 * 8);
2302 }
2303 } else if (h->cbp & 15) {
2304 if (transform_bypass) {
2305 const int di = IS_8x8DCT(mb_type) ? 4 : 1;
2306 idct_add = IS_8x8DCT(mb_type) ? h->h264dsp.h264_add_pixels8
2307 : h->h264dsp.h264_add_pixels4;
2308 for (i = 0; i < 16; i += di)
2309 if (h->non_zero_count_cache[scan8[i + p * 16]])
2310 idct_add(dest_y + block_offset[i],
2311 h->mb + (i * 16 + p * 256 << pixel_shift),
2312 linesize);
2313 } else {
2314 if (IS_8x8DCT(mb_type))
2315 h->h264dsp.h264_idct8_add4(dest_y, block_offset,
2316 h->mb + (p * 256 << pixel_shift),
2317 linesize,
2318 h->non_zero_count_cache + p * 5 * 8);
2319 else
2320 h->h264dsp.h264_idct_add16(dest_y, block_offset,
2321 h->mb + (p * 256 << pixel_shift),
2322 linesize,
2323 h->non_zero_count_cache + p * 5 * 8);
2324 }
2325 }
2326 } else if (CONFIG_SVQ3_DECODER) {
2327 for (i = 0; i < 16; i++)
2328 if (h->non_zero_count_cache[scan8[i + p * 16]] || h->mb[i * 16 + p * 256]) {
2329 // FIXME benchmark weird rule, & below
2330 uint8_t *const ptr = dest_y + block_offset[i];
2331 ff_svq3_add_idct_c(ptr, h->mb + i * 16 + p * 256, linesize,
2332 h->qscale, IS_INTRA(mb_type) ? 1 : 0);
2333 }
2334 }
2335 }
2336 }
2337
2338 #define BITS 8
2339 #define SIMPLE 1
2340 #include "h264_mb_template.c"
2341
2342 #undef BITS
2343 #define BITS 16
2344 #include "h264_mb_template.c"
2345
2346 #undef SIMPLE
2347 #define SIMPLE 0
2348 #include "h264_mb_template.c"
2349
2350 void ff_h264_hl_decode_mb(H264Context *h)
2351 {
2352 const int mb_xy = h->mb_xy;
2353 const int mb_type = h->cur_pic.f.mb_type[mb_xy];
2354 int is_complex = CONFIG_SMALL || h->is_complex || IS_INTRA_PCM(mb_type) || h->qscale == 0;
2355
2356 if (CHROMA444) {
2357 if (is_complex || h->pixel_shift)
2358 hl_decode_mb_444_complex(h);
2359 else
2360 hl_decode_mb_444_simple_8(h);
2361 } else if (is_complex) {
2362 hl_decode_mb_complex(h);
2363 } else if (h->pixel_shift) {
2364 hl_decode_mb_simple_16(h);
2365 } else
2366 hl_decode_mb_simple_8(h);
2367 }
2368
2369 static int pred_weight_table(H264Context *h)
2370 {
2371 int list, i;
2372 int luma_def, chroma_def;
2373
2374 h->use_weight = 0;
2375 h->use_weight_chroma = 0;
2376 h->luma_log2_weight_denom = get_ue_golomb(&h->gb);
2377 if (h->sps.chroma_format_idc)
2378 h->chroma_log2_weight_denom = get_ue_golomb(&h->gb);
2379 luma_def = 1 << h->luma_log2_weight_denom;
2380 chroma_def = 1 << h->chroma_log2_weight_denom;
2381
2382 for (list = 0; list < 2; list++) {
2383 h->luma_weight_flag[list] = 0;
2384 h->chroma_weight_flag[list] = 0;
2385 for (i = 0; i < h->ref_count[list]; i++) {
2386 int luma_weight_flag, chroma_weight_flag;
2387
2388 luma_weight_flag = get_bits1(&h->gb);
2389 if (luma_weight_flag) {
2390 h->luma_weight[i][list][0] = get_se_golomb(&h->gb);
2391 h->luma_weight[i][list][1] = get_se_golomb(&h->gb);
2392 if (h->luma_weight[i][list][0] != luma_def ||
2393 h->luma_weight[i][list][1] != 0) {
2394 h->use_weight = 1;
2395 h->luma_weight_flag[list] = 1;
2396 }
2397 } else {
2398 h->luma_weight[i][list][0] = luma_def;
2399 h->luma_weight[i][list][1] = 0;
2400 }
2401
2402 if (h->sps.chroma_format_idc) {
2403 chroma_weight_flag = get_bits1(&h->gb);
2404 if (chroma_weight_flag) {
2405 int j;
2406 for (j = 0; j < 2; j++) {
2407 h->chroma_weight[i][list][j][0] = get_se_golomb(&h->gb);
2408 h->chroma_weight[i][list][j][1] = get_se_golomb(&h->gb);
2409 if (h->chroma_weight[i][list][j][0] != chroma_def ||
2410 h->chroma_weight[i][list][j][1] != 0) {
2411 h->use_weight_chroma = 1;
2412 h->chroma_weight_flag[list] = 1;
2413 }
2414 }
2415 } else {
2416 int j;
2417 for (j = 0; j < 2; j++) {
2418 h->chroma_weight[i][list][j][0] = chroma_def;
2419 h->chroma_weight[i][list][j][1] = 0;
2420 }
2421 }
2422 }
2423 }
2424 if (h->slice_type_nos != AV_PICTURE_TYPE_B)
2425 break;
2426 }
2427 h->use_weight = h->use_weight || h->use_weight_chroma;
2428 return 0;
2429 }
2430
2431 /**
2432 * Initialize implicit_weight table.
2433 * @param field 0/1 initialize the weight for interlaced MBAFF
2434 * -1 initializes the rest
2435 */
2436 static void implicit_weight_table(H264Context *h, int field)
2437 {
2438 int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
2439
2440 for (i = 0; i < 2; i++) {
2441 h->luma_weight_flag[i] = 0;
2442 h->chroma_weight_flag[i] = 0;
2443 }
2444
2445 if (field < 0) {
2446 if (h->picture_structure == PICT_FRAME) {
2447 cur_poc = h->cur_pic_ptr->poc;
2448 } else {
2449 cur_poc = h->cur_pic_ptr->field_poc[h->picture_structure - 1];
2450 }
2451 if (h->ref_count[0] == 1 && h->ref_count[1] == 1 && !FRAME_MBAFF &&
2452 h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2 * cur_poc) {
2453 h->use_weight = 0;
2454 h->use_weight_chroma = 0;
2455 return;
2456 }
2457 ref_start = 0;
2458 ref_count0 = h->ref_count[0];
2459 ref_count1 = h->ref_count[1];
2460 } else {
2461 cur_poc = h->cur_pic_ptr->field_poc[field];
2462 ref_start = 16;
2463 ref_count0 = 16 + 2 * h->ref_count[0];
2464 ref_count1 = 16 + 2 * h->ref_count[1];
2465 }
2466
2467 h->use_weight = 2;
2468 h->use_weight_chroma = 2;
2469 h->luma_log2_weight_denom = 5;
2470 h->chroma_log2_weight_denom = 5;
2471
2472 for (ref0 = ref_start; ref0 < ref_count0; ref0++) {
2473 int poc0 = h->ref_list[0][ref0].poc;
2474 for (ref1 = ref_start; ref1 < ref_count1; ref1++) {
2475 int w = 32;
2476 if (!h->ref_list[0][ref0].long_ref && !h->ref_list[1][ref1].long_ref) {
2477 int poc1 = h->ref_list[1][ref1].poc;
2478 int td = av_clip(poc1 - poc0, -128, 127);
2479 if (td) {
2480 int tb = av_clip(cur_poc - poc0, -128, 127);
2481 int tx = (16384 + (FFABS(td) >> 1)) / td;
2482 int dist_scale_factor = (tb * tx + 32) >> 8;
2483 if (dist_scale_factor >= -64 && dist_scale_factor <= 128)
2484 w = 64 - dist_scale_factor;
2485 }
2486 }
2487 if (field < 0) {
2488 h->implicit_weight[ref0][ref1][0] =
2489 h->implicit_weight[ref0][ref1][1] = w;
2490 } else {
2491 h->implicit_weight[ref0][ref1][field] = w;
2492 }
2493 }
2494 }
2495 }
2496
2497 /**
2498 * instantaneous decoder refresh.
2499 */
2500 static void idr(H264Context *h)
2501 {
2502 ff_h264_remove_all_refs(h);
2503 h->prev_frame_num = 0;
2504 h->prev_frame_num_offset = 0;
2505 h->prev_poc_msb =
2506 h->prev_poc_lsb = 0;
2507 }
2508
2509 /* forget old pics after a seek */
2510 static void flush_change(H264Context *h)
2511 {
2512 int i;
2513 for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
2514 h->last_pocs[i] = INT_MIN;
2515 h->outputed_poc = h->next_outputed_poc = INT_MIN;
2516 h->prev_interlaced_frame = 1;
2517 idr(h);
2518 if (h->cur_pic_ptr)
2519 h->cur_pic_ptr->f.reference = 0;
2520 h->first_field = 0;
2521 memset(h->ref_list[0], 0, sizeof(h->ref_list[0]));
2522 memset(h->ref_list[1], 0, sizeof(h->ref_list[1]));
2523 memset(h->default_ref_list[0], 0, sizeof(h->default_ref_list[0]));
2524 memset(h->default_ref_list[1], 0, sizeof(h->default_ref_list[1]));
2525 ff_h264_reset_sei(h);
2526 }
2527
2528 /* forget old pics after a seek */
2529 static void flush_dpb(AVCodecContext *avctx)
2530 {
2531 H264Context *h = avctx->priv_data;
2532 int i;
2533
2534 for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++) {
2535 if (h->delayed_pic[i])
2536 h->delayed_pic[i]->f.reference = 0;
2537 h->delayed_pic[i] = NULL;
2538 }
2539
2540 flush_change(h);
2541
2542 for (i = 0; i < h->picture_count; i++) {
2543 if (h->DPB[i].f.data[0])
2544 free_frame_buffer(h, &h->DPB[i]);
2545 }
2546 h->cur_pic_ptr = NULL;
2547
2548 h->mb_x = h->mb_y = 0;
2549
2550 h->parse_context.state = -1;
2551 h->parse_context.frame_start_found = 0;
2552 h->parse_context.overread = 0;
2553 h->parse_context.overread_index = 0;
2554 h->parse_context.index = 0;
2555 h->parse_context.last_index = 0;
2556 }
2557
2558 static int init_poc(H264Context *h)
2559 {
2560 const int max_frame_num = 1 << h->sps.log2_max_frame_num;
2561 int field_poc[2];
2562 Picture *cur = h->cur_pic_ptr;
2563
2564 h->frame_num_offset = h->prev_frame_num_offset;
2565 if (h->frame_num < h->prev_frame_num)
2566 h->frame_num_offset += max_frame_num;
2567
2568 if (h->sps.poc_type == 0) {
2569 const int max_poc_lsb = 1 << h->sps.log2_max_poc_lsb;
2570
2571 if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb / 2)
2572 h->poc_msb = h->prev_poc_msb + max_poc_lsb;
2573 else if (h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb / 2)
2574 h->poc_msb = h->prev_poc_msb - max_poc_lsb;
2575 else
2576 h->poc_msb = h->prev_poc_msb;
2577 field_poc[0] =
2578 field_poc[1] = h->poc_msb + h->poc_lsb;
2579 if (h->picture_structure == PICT_FRAME)
2580 field_poc[1] += h->delta_poc_bottom;
2581 } else if (h->sps.poc_type == 1) {
2582 int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
2583 int i;
2584
2585 if (h->sps.poc_cycle_length != 0)
2586 abs_frame_num = h->frame_num_offset + h->frame_num;
2587 else
2588 abs_frame_num = 0;
2589
2590 if (h->nal_ref_idc == 0 && abs_frame_num > 0)
2591 abs_frame_num--;
2592
2593 expected_delta_per_poc_cycle = 0;
2594 for (i = 0; i < h->sps.poc_cycle_length; i++)
2595 // FIXME integrate during sps parse
2596 expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[i];
2597
2598 if (abs_frame_num > 0) {
2599 int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
2600 int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
2601
2602 expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
2603 for (i = 0; i <= frame_num_in_poc_cycle; i++)
2604 expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[i];
2605 } else
2606 expectedpoc = 0;
2607
2608 if (h->nal_ref_idc == 0)
2609 expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
2610
2611 field_poc[0] = expectedpoc + h->delta_poc[0];
2612 field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
2613
2614 if (h->picture_structure == PICT_FRAME)
2615 field_poc[1] += h->delta_poc[1];
2616 } else {
2617 int poc = 2 * (h->frame_num_offset + h->frame_num);
2618
2619 if (!h->nal_ref_idc)
2620 poc--;
2621
2622 field_poc[0] = poc;
2623 field_poc[1] = poc;
2624 }
2625
2626 if (h->picture_structure != PICT_BOTTOM_FIELD)
2627 h->cur_pic_ptr->field_poc[0] = field_poc[0];
2628 if (h->picture_structure != PICT_TOP_FIELD)
2629 h->cur_pic_ptr->field_poc[1] = field_poc[1];
2630 cur->poc = FFMIN(cur->field_poc[0], cur->field_poc[1]);
2631
2632 return 0;
2633 }
2634
2635 /**
2636 * initialize scan tables
2637 */
2638 static void init_scan_tables(H264Context *h)
2639 {
2640 int i;
2641 for (i = 0; i < 16; i++) {
2642 #define T(x) (x >> 2) | ((x << 2) & 0xF)
2643 h->zigzag_scan[i] = T(zigzag_scan[i]);
2644 h->field_scan[i] = T(field_scan[i]);
2645 #undef T
2646 }
2647 for (i = 0; i < 64; i++) {
2648 #define T(x) (x >> 3) | ((x & 7) << 3)
2649 h->zigzag_scan8x8[i] = T(ff_zigzag_direct[i]);
2650 h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
2651 h->field_scan8x8[i] = T(field_scan8x8[i]);
2652 h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]);
2653 #undef T
2654 }
2655 if (h->sps.transform_bypass) { // FIXME same ugly
2656 h->zigzag_scan_q0 = zigzag_scan;
2657 h->zigzag_scan8x8_q0 = ff_zigzag_direct;
2658 h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
2659 h->field_scan_q0 = field_scan;
2660 h->field_scan8x8_q0 = field_scan8x8;
2661 h->field_scan8x8_cavlc_q0 = field_scan8x8_cavlc;
2662 } else {
2663 h->zigzag_scan_q0 = h->zigzag_scan;
2664 h->zigzag_scan8x8_q0 = h->zigzag_scan8x8;
2665 h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
2666 h->field_scan_q0 = h->field_scan;
2667 h->field_scan8x8_q0 = h->field_scan8x8;
2668 h->field_scan8x8_cavlc_q0 = h->field_scan8x8_cavlc;
2669 }
2670 }
2671
2672 static int field_end(H264Context *h, int in_setup)
2673 {
2674 AVCodecContext *const avctx = h->avctx;
2675 int err = 0;
2676 h->mb_y = 0;
2677
2678 if (!in_setup && !h->droppable)
2679 ff_thread_report_progress(&h->cur_pic_ptr->f, INT_MAX,
2680 h->picture_structure == PICT_BOTTOM_FIELD);
2681
2682 if (CONFIG_H264_VDPAU_DECODER &&
2683 h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
2684 ff_vdpau_h264_set_reference_frames(h);
2685
2686 if (in_setup || !(avctx->active_thread_type & FF_THREAD_FRAME)) {
2687 if (!h->droppable) {
2688 err = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
2689 h->prev_poc_msb = h->poc_msb;
2690 h->prev_poc_lsb = h->poc_lsb;
2691 }
2692 h->prev_frame_num_offset = h->frame_num_offset;
2693 h->prev_frame_num = h->frame_num;
2694 h->outputed_poc = h->next_outputed_poc;
2695 }
2696
2697 if (avctx->hwaccel) {
2698 if (avctx->hwaccel->end_frame(avctx) < 0)
2699 av_log(avctx, AV_LOG_ERROR,
2700 "hardware accelerator failed to decode picture\n");
2701 }
2702
2703 if (CONFIG_H264_VDPAU_DECODER &&
2704 h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
2705 ff_vdpau_h264_picture_complete(h);
2706
2707 /*
2708 * FIXME: Error handling code does not seem to support interlaced
2709 * when slices span multiple rows
2710 * The ff_er_add_slice calls don't work right for bottom
2711 * fields; they cause massive erroneous error concealing
2712 * Error marking covers both fields (top and bottom).
2713 * This causes a mismatched s->error_count
2714 * and a bad error table. Further, the error count goes to
2715 * INT_MAX when called for bottom field, because mb_y is
2716 * past end by one (callers fault) and resync_mb_y != 0
2717 * causes problems for the first MB line, too.
2718 */
2719 if (!FIELD_PICTURE) {
2720 h->er.cur_pic = h->cur_pic_ptr;
2721 h->er.last_pic = h->ref_count[0] ? &h->ref_list[0][0] : NULL;
2722 h->er.next_pic = h->ref_count[1] ? &h->ref_list[1][0] : NULL;
2723 ff_er_frame_end(&h->er);
2724 }
2725 emms_c();
2726
2727 h->current_slice = 0;
2728
2729 return err;
2730 }
2731
2732 /**
2733 * Replicate H264 "master" context to thread contexts.
2734 */
2735 static int clone_slice(H264Context *dst, H264Context *src)
2736 {
2737 memcpy(dst->block_offset, src->block_offset, sizeof(dst->block_offset));
2738 dst->cur_pic_ptr = src->cur_pic_ptr;
2739 dst->cur_pic = src->cur_pic;
2740 dst->linesize = src->linesize;
2741 dst->uvlinesize = src->uvlinesize;
2742 dst->first_field = src->first_field;
2743
2744 dst->prev_poc_msb = src->prev_poc_msb;
2745 dst->prev_poc_lsb = src->prev_poc_lsb;
2746 dst->prev_frame_num_offset = src->prev_frame_num_offset;
2747 dst->prev_frame_num = src->prev_frame_num;
2748 dst->short_ref_count = src->short_ref_count;
2749
2750 memcpy(dst->short_ref, src->short_ref, sizeof(dst->short_ref));
2751 memcpy(dst->long_ref, src->long_ref, sizeof(dst->long_ref));
2752 memcpy(dst->default_ref_list, src->default_ref_list, sizeof(dst->default_ref_list));
2753
2754 memcpy(dst->dequant4_coeff, src->dequant4_coeff, sizeof(src->dequant4_coeff));
2755 memcpy(dst->dequant8_coeff, src->dequant8_coeff, sizeof(src->dequant8_coeff));
2756
2757 return 0;
2758 }
2759
2760 /**
2761 * Compute profile from profile_idc and constraint_set?_flags.
2762 *
2763 * @param sps SPS
2764 *
2765 * @return profile as defined by FF_PROFILE_H264_*
2766 */
2767 int ff_h264_get_profile(SPS *sps)
2768 {
2769 int profile = sps->profile_idc;
2770
2771 switch (sps->profile_idc) {
2772 case FF_PROFILE_H264_BASELINE:
2773 // constraint_set1_flag set to 1
2774 profile |= (sps->constraint_set_flags & 1 << 1) ? FF_PROFILE_H264_CONSTRAINED : 0;
2775 break;
2776 case FF_PROFILE_H264_HIGH_10:
2777 case FF_PROFILE_H264_HIGH_422:
2778 case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
2779 // constraint_set3_flag set to 1
2780 profile |= (sps->constraint_set_flags & 1 << 3) ? FF_PROFILE_H264_INTRA : 0;
2781 break;
2782 }
2783
2784 return profile;
2785 }
2786
2787 static int h264_set_parameter_from_sps(H264Context *h)
2788 {
2789 if (h->flags & CODEC_FLAG_LOW_DELAY ||
2790 (h->sps.bitstream_restriction_flag &&
2791 !h->sps.num_reorder_frames)) {
2792 if (h->avctx->has_b_frames > 1 || h->delayed_pic[0])
2793 av_log(h->avctx, AV_LOG_WARNING, "Delayed frames seen. "
2794 "Reenabling low delay requires a codec flush.\n");
2795 else
2796 h->low_delay = 1;
2797 }
2798
2799 if (h->avctx->has_b_frames < 2)
2800 h->avctx->has_b_frames = !h->low_delay;
2801
2802 if (h->sps.bit_depth_luma != h->sps.bit_depth_chroma) {
2803 av_log_missing_feature(h->avctx,
2804 "Different bit depth between chroma and luma", 1);
2805 return AVERROR_PATCHWELCOME;
2806 }
2807
2808 if (h->avctx->bits_per_raw_sample != h->sps.bit_depth_luma ||
2809 h->cur_chroma_format_idc != h->sps.chroma_format_idc) {
2810 if (h->avctx->codec &&
2811 h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU &&
2812 (h->sps.bit_depth_luma != 8 || h->sps.chroma_format_idc > 1)) {
2813 av_log(h->avctx, AV_LOG_ERROR,
2814 "VDPAU decoding does not support video colorspace.\n");
2815 return AVERROR_INVALIDDATA;
2816 }
2817 if (h->sps.bit_depth_luma >= 8 && h->sps.bit_depth_luma <= 10) {
2818 h->avctx->bits_per_raw_sample = h->sps.bit_depth_luma;
2819 h->cur_chroma_format_idc = h->sps.chroma_format_idc;
2820 h->pixel_shift = h->sps.bit_depth_luma > 8;
2821
2822 ff_h264dsp_init(&h->h264dsp, h->sps.bit_depth_luma,
2823 h->sps.chroma_format_idc);
2824 ff_h264chroma_init(&h->h264chroma, h->sps.bit_depth_chroma);
2825 ff_h264qpel_init(&h->h264qpel, h->sps.bit_depth_luma);
2826 ff_h264_pred_init(&h->hpc, h->avctx->codec_id, h->sps.bit_depth_luma,
2827 h->sps.chroma_format_idc);
2828 h->dsp.dct_bits = h->sps.bit_depth_luma > 8 ? 32 : 16;
2829 ff_dsputil_init(&h->dsp, h->avctx);
2830 ff_videodsp_init(&h->vdsp, h->sps.bit_depth_luma);
2831 } else {
2832 av_log(h->avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n",
2833 h->sps.bit_depth_luma);
2834 return AVERROR_INVALIDDATA;
2835 }
2836 }
2837 return 0;
2838 }
2839
2840 static enum PixelFormat get_pixel_format(H264Context *h)
2841 {
2842 switch (h->sps.bit_depth_luma) {
2843 case 9:
2844 if (CHROMA444) {
2845 if (h->avctx->colorspace == AVCOL_SPC_RGB) {
2846 return AV_PIX_FMT_GBRP9;
2847 } else
2848 return AV_PIX_FMT_YUV444P9;
2849 } else if (CHROMA422)
2850 return AV_PIX_FMT_YUV422P9;
2851 else
2852 return AV_PIX_FMT_YUV420P9;
2853 break;
2854 case 10:
2855 if (CHROMA444) {
2856 if (h->avctx->colorspace == AVCOL_SPC_RGB) {
2857 return AV_PIX_FMT_GBRP10;
2858 } else
2859 return AV_PIX_FMT_YUV444P10;
2860 } else if (CHROMA422)
2861 return AV_PIX_FMT_YUV422P10;
2862 else
2863 return AV_PIX_FMT_YUV420P10;
2864 break;
2865 case 8:
2866 if (CHROMA444) {
2867 if (h->avctx->colorspace == AVCOL_SPC_RGB) {
2868 return AV_PIX_FMT_GBRP;
2869 } else
2870 return h->avctx->color_range == AVCOL_RANGE_JPEG ? AV_PIX_FMT_YUVJ444P
2871 : AV_PIX_FMT_YUV444P;
2872 } else if (CHROMA422) {
2873 return h->avctx->color_range == AVCOL_RANGE_JPEG ? AV_PIX_FMT_YUVJ422P
2874 : AV_PIX_FMT_YUV422P;
2875 } else {
2876 return h->avctx->get_format(h->avctx, h->avctx->codec->pix_fmts ?
2877 h->avctx->codec->pix_fmts :
2878 h->avctx->color_range == AVCOL_RANGE_JPEG ?
2879 h264_hwaccel_pixfmt_list_jpeg_420 :
2880 h264_hwaccel_pixfmt_list_420);
2881 }
2882 break;
2883 default:
2884 av_log(h->avctx, AV_LOG_ERROR,
2885 "Unsupported bit depth: %d\n", h->sps.bit_depth_luma);
2886 return AVERROR_INVALIDDATA;
2887 }
2888 }
2889
2890 static int h264_slice_header_init(H264Context *h, int reinit)
2891 {
2892 int nb_slices = (HAVE_THREADS &&
2893 h->avctx->active_thread_type & FF_THREAD_SLICE) ?
2894 h->avctx->thread_count : 1;
2895 int i;
2896
2897 avcodec_set_dimensions(h->avctx, h->width, h->height);
2898 h->avctx->sample_aspect_ratio = h->sps.sar;
2899 av_assert0(h->avctx->sample_aspect_ratio.den);
2900 av_pix_fmt_get_chroma_sub_sample(h->avctx->pix_fmt,
2901 &h->chroma_x_shift, &h->chroma_y_shift);
2902
2903 if (h->sps.timing_info_present_flag) {
2904 int64_t den = h->sps.time_scale;
2905 if (h->x264_build < 44U)