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