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