h264: disable ER by default
[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/avassert.h"
29 #include "libavutil/display.h"
30 #include "libavutil/imgutils.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/stereo3d.h"
33 #include "libavutil/timer.h"
34 #include "internal.h"
35 #include "cabac.h"
36 #include "cabac_functions.h"
37 #include "error_resilience.h"
38 #include "avcodec.h"
39 #include "h264.h"
40 #include "h264data.h"
41 #include "h264chroma.h"
42 #include "h264_mvpred.h"
43 #include "golomb.h"
44 #include "mathops.h"
45 #include "me_cmp.h"
46 #include "mpegutils.h"
47 #include "rectangle.h"
48 #include "svq3.h"
49 #include "thread.h"
50
51 #include <assert.h>
52
53 const uint16_t ff_h264_mb_sizes[4] = { 256, 384, 512, 768 };
54
55 static void h264_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type,
56 int (*mv)[2][4][2],
57 int mb_x, int mb_y, int mb_intra, int mb_skipped)
58 {
59 H264Context *h = opaque;
60 H264SliceContext *sl = &h->slice_ctx[0];
61
62 sl->mb_x = mb_x;
63 sl->mb_y = mb_y;
64 sl->mb_xy = mb_x + mb_y * h->mb_stride;
65 memset(sl->non_zero_count_cache, 0, sizeof(sl->non_zero_count_cache));
66 assert(ref >= 0);
67 /* FIXME: It is possible albeit uncommon that slice references
68 * differ between slices. We take the easy approach and ignore
69 * it for now. If this turns out to have any relevance in
70 * practice then correct remapping should be added. */
71 if (ref >= sl->ref_count[0])
72 ref = 0;
73 fill_rectangle(&h->cur_pic.ref_index[0][4 * sl->mb_xy],
74 2, 2, 2, ref, 1);
75 fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
76 fill_rectangle(sl->mv_cache[0][scan8[0]], 4, 4, 8,
77 pack16to32((*mv)[0][0][0], (*mv)[0][0][1]), 4);
78 assert(!FRAME_MBAFF(h));
79 ff_h264_hl_decode_mb(h, &h->slice_ctx[0]);
80 }
81
82 void ff_h264_draw_horiz_band(const H264Context *h, H264SliceContext *sl,
83 int y, int height)
84 {
85 AVCodecContext *avctx = h->avctx;
86 const AVFrame *src = &h->cur_pic.f;
87 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
88 int vshift = desc->log2_chroma_h;
89 const int field_pic = h->picture_structure != PICT_FRAME;
90 if (field_pic) {
91 height <<= 1;
92 y <<= 1;
93 }
94
95 height = FFMIN(height, avctx->height - y);
96
97 if (field_pic && h->first_field && !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD))
98 return;
99
100 if (avctx->draw_horiz_band) {
101 int offset[AV_NUM_DATA_POINTERS];
102 int i;
103
104 offset[0] = y * src->linesize[0];
105 offset[1] =
106 offset[2] = (y >> vshift) * src->linesize[1];
107 for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
108 offset[i] = 0;
109
110 emms_c();
111
112 avctx->draw_horiz_band(avctx, src, offset,
113 y, h->picture_structure, height);
114 }
115 }
116
117 /**
118 * Check if the top & left blocks are available if needed and
119 * change the dc mode so it only uses the available blocks.
120 */
121 int ff_h264_check_intra4x4_pred_mode(const H264Context *h, H264SliceContext *sl)
122 {
123 static const int8_t top[12] = {
124 -1, 0, LEFT_DC_PRED, -1, -1, -1, -1, -1, 0
125 };
126 static const int8_t left[12] = {
127 0, -1, TOP_DC_PRED, 0, -1, -1, -1, 0, -1, DC_128_PRED
128 };
129 int i;
130
131 if (!(sl->top_samples_available & 0x8000)) {
132 for (i = 0; i < 4; i++) {
133 int status = top[sl->intra4x4_pred_mode_cache[scan8[0] + i]];
134 if (status < 0) {
135 av_log(h->avctx, AV_LOG_ERROR,
136 "top block unavailable for requested intra4x4 mode %d at %d %d\n",
137 status, sl->mb_x, sl->mb_y);
138 return AVERROR_INVALIDDATA;
139 } else if (status) {
140 sl->intra4x4_pred_mode_cache[scan8[0] + i] = status;
141 }
142 }
143 }
144
145 if ((sl->left_samples_available & 0x8888) != 0x8888) {
146 static const int mask[4] = { 0x8000, 0x2000, 0x80, 0x20 };
147 for (i = 0; i < 4; i++)
148 if (!(sl->left_samples_available & mask[i])) {
149 int status = left[sl->intra4x4_pred_mode_cache[scan8[0] + 8 * i]];
150 if (status < 0) {
151 av_log(h->avctx, AV_LOG_ERROR,
152 "left block unavailable for requested intra4x4 mode %d at %d %d\n",
153 status, sl->mb_x, sl->mb_y);
154 return AVERROR_INVALIDDATA;
155 } else if (status) {
156 sl->intra4x4_pred_mode_cache[scan8[0] + 8 * i] = status;
157 }
158 }
159 }
160
161 return 0;
162 } // FIXME cleanup like ff_h264_check_intra_pred_mode
163
164 /**
165 * Check if the top & left blocks are available if needed and
166 * change the dc mode so it only uses the available blocks.
167 */
168 int ff_h264_check_intra_pred_mode(const H264Context *h, H264SliceContext *sl,
169 int mode, int is_chroma)
170 {
171 static const int8_t top[4] = { LEFT_DC_PRED8x8, 1, -1, -1 };
172 static const int8_t left[5] = { TOP_DC_PRED8x8, -1, 2, -1, DC_128_PRED8x8 };
173
174 if (mode > 3U) {
175 av_log(h->avctx, AV_LOG_ERROR,
176 "out of range intra chroma pred mode at %d %d\n",
177 sl->mb_x, sl->mb_y);
178 return AVERROR_INVALIDDATA;
179 }
180
181 if (!(sl->top_samples_available & 0x8000)) {
182 mode = top[mode];
183 if (mode < 0) {
184 av_log(h->avctx, AV_LOG_ERROR,
185 "top block unavailable for requested intra mode at %d %d\n",
186 sl->mb_x, sl->mb_y);
187 return AVERROR_INVALIDDATA;
188 }
189 }
190
191 if ((sl->left_samples_available & 0x8080) != 0x8080) {
192 mode = left[mode];
193 if (is_chroma && (sl->left_samples_available & 0x8080)) {
194 // mad cow disease mode, aka MBAFF + constrained_intra_pred
195 mode = ALZHEIMER_DC_L0T_PRED8x8 +
196 (!(sl->left_samples_available & 0x8000)) +
197 2 * (mode == DC_128_PRED8x8);
198 }
199 if (mode < 0) {
200 av_log(h->avctx, AV_LOG_ERROR,
201 "left block unavailable for requested intra mode at %d %d\n",
202 sl->mb_x, sl->mb_y);
203 return AVERROR_INVALIDDATA;
204 }
205 }
206
207 return mode;
208 }
209
210 const uint8_t *ff_h264_decode_nal(H264Context *h, H264SliceContext *sl,
211 const uint8_t *src,
212 int *dst_length, int *consumed, int length)
213 {
214 int i, si, di;
215 uint8_t *dst;
216
217 // src[0]&0x80; // forbidden bit
218 h->nal_ref_idc = src[0] >> 5;
219 h->nal_unit_type = src[0] & 0x1F;
220
221 src++;
222 length--;
223
224 #define STARTCODE_TEST \
225 if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
226 if (src[i + 2] != 3) { \
227 /* startcode, so we must be past the end */ \
228 length = i; \
229 } \
230 break; \
231 }
232
233 #if HAVE_FAST_UNALIGNED
234 #define FIND_FIRST_ZERO \
235 if (i > 0 && !src[i]) \
236 i--; \
237 while (src[i]) \
238 i++
239
240 #if HAVE_FAST_64BIT
241 for (i = 0; i + 1 < length; i += 9) {
242 if (!((~AV_RN64A(src + i) &
243 (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
244 0x8000800080008080ULL))
245 continue;
246 FIND_FIRST_ZERO;
247 STARTCODE_TEST;
248 i -= 7;
249 }
250 #else
251 for (i = 0; i + 1 < length; i += 5) {
252 if (!((~AV_RN32A(src + i) &
253 (AV_RN32A(src + i) - 0x01000101U)) &
254 0x80008080U))
255 continue;
256 FIND_FIRST_ZERO;
257 STARTCODE_TEST;
258 i -= 3;
259 }
260 #endif
261 #else
262 for (i = 0; i + 1 < length; i += 2) {
263 if (src[i])
264 continue;
265 if (i > 0 && src[i - 1] == 0)
266 i--;
267 STARTCODE_TEST;
268 }
269 #endif
270
271 if (i >= length - 1) { // no escaped 0
272 *dst_length = length;
273 *consumed = length + 1; // +1 for the header
274 return src;
275 }
276
277 av_fast_malloc(&sl->rbsp_buffer, &sl->rbsp_buffer_size,
278 length + FF_INPUT_BUFFER_PADDING_SIZE);
279 dst = sl->rbsp_buffer;
280
281 if (!dst)
282 return NULL;
283
284 memcpy(dst, src, i);
285 si = di = i;
286 while (si + 2 < length) {
287 // remove escapes (very rare 1:2^22)
288 if (src[si + 2] > 3) {
289 dst[di++] = src[si++];
290 dst[di++] = src[si++];
291 } else if (src[si] == 0 && src[si + 1] == 0) {
292 if (src[si + 2] == 3) { // escape
293 dst[di++] = 0;
294 dst[di++] = 0;
295 si += 3;
296 continue;
297 } else // next start code
298 goto nsc;
299 }
300
301 dst[di++] = src[si++];
302 }
303 while (si < length)
304 dst[di++] = src[si++];
305
306 nsc:
307 memset(dst + di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
308
309 *dst_length = di;
310 *consumed = si + 1; // +1 for the header
311 /* FIXME store exact number of bits in the getbitcontext
312 * (it is needed for decoding) */
313 return dst;
314 }
315
316 /**
317 * Identify the exact end of the bitstream
318 * @return the length of the trailing, or 0 if damaged
319 */
320 static int decode_rbsp_trailing(H264Context *h, const uint8_t *src)
321 {
322 int v = *src;
323 int r;
324
325 tprintf(h->avctx, "rbsp trailing %X\n", v);
326
327 for (r = 1; r < 9; r++) {
328 if (v & 1)
329 return r;
330 v >>= 1;
331 }
332 return 0;
333 }
334
335 void ff_h264_free_tables(H264Context *h, int free_rbsp)
336 {
337 int i;
338
339 av_freep(&h->intra4x4_pred_mode);
340 av_freep(&h->chroma_pred_mode_table);
341 av_freep(&h->cbp_table);
342 av_freep(&h->mvd_table[0]);
343 av_freep(&h->mvd_table[1]);
344 av_freep(&h->direct_table);
345 av_freep(&h->non_zero_count);
346 av_freep(&h->slice_table_base);
347 h->slice_table = NULL;
348 av_freep(&h->list_counts);
349
350 av_freep(&h->mb2b_xy);
351 av_freep(&h->mb2br_xy);
352
353 av_buffer_pool_uninit(&h->qscale_table_pool);
354 av_buffer_pool_uninit(&h->mb_type_pool);
355 av_buffer_pool_uninit(&h->motion_val_pool);
356 av_buffer_pool_uninit(&h->ref_index_pool);
357
358 if (free_rbsp && h->DPB) {
359 for (i = 0; i < H264_MAX_PICTURE_COUNT; i++)
360 ff_h264_unref_picture(h, &h->DPB[i]);
361 av_freep(&h->DPB);
362 } else if (h->DPB) {
363 for (i = 0; i < H264_MAX_PICTURE_COUNT; i++)
364 h->DPB[i].needs_realloc = 1;
365 }
366
367 h->cur_pic_ptr = NULL;
368
369 for (i = 0; i < h->nb_slice_ctx; i++) {
370 H264SliceContext *sl = &h->slice_ctx[i];
371
372 av_freep(&sl->dc_val_base);
373 av_freep(&sl->er.mb_index2xy);
374 av_freep(&sl->er.error_status_table);
375 av_freep(&sl->er.er_temp_buffer);
376
377 av_freep(&sl->bipred_scratchpad);
378 av_freep(&sl->edge_emu_buffer);
379 av_freep(&sl->top_borders[0]);
380 av_freep(&sl->top_borders[1]);
381
382 sl->bipred_scratchpad_allocated = 0;
383 sl->edge_emu_buffer_allocated = 0;
384 sl->top_borders_allocated[0] = 0;
385 sl->top_borders_allocated[1] = 0;
386
387 if (free_rbsp) {
388 av_freep(&sl->rbsp_buffer);
389 sl->rbsp_buffer_size = 0;
390 }
391 }
392 }
393
394 int ff_h264_alloc_tables(H264Context *h)
395 {
396 const int big_mb_num = h->mb_stride * (h->mb_height + 1);
397 const int row_mb_num = h->mb_stride * 2 * h->avctx->thread_count;
398 int x, y, i;
399
400 FF_ALLOCZ_OR_GOTO(h->avctx, h->intra4x4_pred_mode,
401 row_mb_num * 8 * sizeof(uint8_t), fail)
402 h->slice_ctx[0].intra4x4_pred_mode = h->intra4x4_pred_mode;
403
404 FF_ALLOCZ_OR_GOTO(h->avctx, h->non_zero_count,
405 big_mb_num * 48 * sizeof(uint8_t), fail)
406 FF_ALLOCZ_OR_GOTO(h->avctx, h->slice_table_base,
407 (big_mb_num + h->mb_stride) * sizeof(*h->slice_table_base), fail)
408 FF_ALLOCZ_OR_GOTO(h->avctx, h->cbp_table,
409 big_mb_num * sizeof(uint16_t), fail)
410 FF_ALLOCZ_OR_GOTO(h->avctx, h->chroma_pred_mode_table,
411 big_mb_num * sizeof(uint8_t), fail)
412 FF_ALLOCZ_OR_GOTO(h->avctx, h->mvd_table[0],
413 16 * row_mb_num * sizeof(uint8_t), fail);
414 FF_ALLOCZ_OR_GOTO(h->avctx, h->mvd_table[1],
415 16 * row_mb_num * sizeof(uint8_t), fail);
416 h->slice_ctx[0].mvd_table[0] = h->mvd_table[0];
417 h->slice_ctx[0].mvd_table[1] = h->mvd_table[1];
418
419 FF_ALLOCZ_OR_GOTO(h->avctx, h->direct_table,
420 4 * big_mb_num * sizeof(uint8_t), fail);
421 FF_ALLOCZ_OR_GOTO(h->avctx, h->list_counts,
422 big_mb_num * sizeof(uint8_t), fail)
423
424 memset(h->slice_table_base, -1,
425 (big_mb_num + h->mb_stride) * sizeof(*h->slice_table_base));
426 h->slice_table = h->slice_table_base + h->mb_stride * 2 + 1;
427
428 FF_ALLOCZ_OR_GOTO(h->avctx, h->mb2b_xy,
429 big_mb_num * sizeof(uint32_t), fail);
430 FF_ALLOCZ_OR_GOTO(h->avctx, h->mb2br_xy,
431 big_mb_num * sizeof(uint32_t), fail);
432 for (y = 0; y < h->mb_height; y++)
433 for (x = 0; x < h->mb_width; x++) {
434 const int mb_xy = x + y * h->mb_stride;
435 const int b_xy = 4 * x + 4 * y * h->b_stride;
436
437 h->mb2b_xy[mb_xy] = b_xy;
438 h->mb2br_xy[mb_xy] = 8 * (FMO ? mb_xy : (mb_xy % (2 * h->mb_stride)));
439 }
440
441 if (!h->dequant4_coeff[0])
442 h264_init_dequant_tables(h);
443
444 if (!h->DPB) {
445 h->DPB = av_mallocz_array(H264_MAX_PICTURE_COUNT, sizeof(*h->DPB));
446 if (!h->DPB)
447 goto fail;
448 for (i = 0; i < H264_MAX_PICTURE_COUNT; i++)
449 av_frame_unref(&h->DPB[i].f);
450 av_frame_unref(&h->cur_pic.f);
451 }
452
453 return 0;
454
455 fail:
456 ff_h264_free_tables(h, 1);
457 return AVERROR(ENOMEM);
458 }
459
460 /**
461 * Init context
462 * Allocate buffers which are not shared amongst multiple threads.
463 */
464 int ff_h264_slice_context_init(H264Context *h, H264SliceContext *sl)
465 {
466 ERContext *er = &sl->er;
467 int mb_array_size = h->mb_height * h->mb_stride;
468 int y_size = (2 * h->mb_width + 1) * (2 * h->mb_height + 1);
469 int c_size = h->mb_stride * (h->mb_height + 1);
470 int yc_size = y_size + 2 * c_size;
471 int x, y, i;
472
473 sl->ref_cache[0][scan8[5] + 1] =
474 sl->ref_cache[0][scan8[7] + 1] =
475 sl->ref_cache[0][scan8[13] + 1] =
476 sl->ref_cache[1][scan8[5] + 1] =
477 sl->ref_cache[1][scan8[7] + 1] =
478 sl->ref_cache[1][scan8[13] + 1] = PART_NOT_AVAILABLE;
479
480 if (CONFIG_ERROR_RESILIENCE) {
481 /* init ER */
482 er->avctx = h->avctx;
483 er->decode_mb = h264_er_decode_mb;
484 er->opaque = h;
485 er->quarter_sample = 1;
486
487 er->mb_num = h->mb_num;
488 er->mb_width = h->mb_width;
489 er->mb_height = h->mb_height;
490 er->mb_stride = h->mb_stride;
491 er->b8_stride = h->mb_width * 2 + 1;
492
493 // error resilience code looks cleaner with this
494 FF_ALLOCZ_OR_GOTO(h->avctx, er->mb_index2xy,
495 (h->mb_num + 1) * sizeof(int), fail);
496
497 for (y = 0; y < h->mb_height; y++)
498 for (x = 0; x < h->mb_width; x++)
499 er->mb_index2xy[x + y * h->mb_width] = x + y * h->mb_stride;
500
501 er->mb_index2xy[h->mb_height * h->mb_width] = (h->mb_height - 1) *
502 h->mb_stride + h->mb_width;
503
504 FF_ALLOCZ_OR_GOTO(h->avctx, er->error_status_table,
505 mb_array_size * sizeof(uint8_t), fail);
506
507 FF_ALLOC_OR_GOTO(h->avctx, er->er_temp_buffer,
508 h->mb_height * h->mb_stride, fail);
509
510 FF_ALLOCZ_OR_GOTO(h->avctx, sl->dc_val_base,
511 yc_size * sizeof(int16_t), fail);
512 er->dc_val[0] = sl->dc_val_base + h->mb_width * 2 + 2;
513 er->dc_val[1] = sl->dc_val_base + y_size + h->mb_stride + 1;
514 er->dc_val[2] = er->dc_val[1] + c_size;
515 for (i = 0; i < yc_size; i++)
516 sl->dc_val_base[i] = 1024;
517 }
518
519 return 0;
520
521 fail:
522 return AVERROR(ENOMEM); // ff_h264_free_tables will clean up for us
523 }
524
525 static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size,
526 int parse_extradata);
527
528 int ff_h264_decode_extradata(H264Context *h)
529 {
530 AVCodecContext *avctx = h->avctx;
531 int ret;
532
533 if (avctx->extradata[0] == 1) {
534 int i, cnt, nalsize;
535 unsigned char *p = avctx->extradata;
536
537 h->is_avc = 1;
538
539 if (avctx->extradata_size < 7) {
540 av_log(avctx, AV_LOG_ERROR,
541 "avcC %d too short\n", avctx->extradata_size);
542 return AVERROR_INVALIDDATA;
543 }
544 /* sps and pps in the avcC always have length coded with 2 bytes,
545 * so put a fake nal_length_size = 2 while parsing them */
546 h->nal_length_size = 2;
547 // Decode sps from avcC
548 cnt = *(p + 5) & 0x1f; // Number of sps
549 p += 6;
550 for (i = 0; i < cnt; i++) {
551 nalsize = AV_RB16(p) + 2;
552 if (p - avctx->extradata + nalsize > avctx->extradata_size)
553 return AVERROR_INVALIDDATA;
554 ret = decode_nal_units(h, p, nalsize, 1);
555 if (ret < 0) {
556 av_log(avctx, AV_LOG_ERROR,
557 "Decoding sps %d from avcC failed\n", i);
558 return ret;
559 }
560 p += nalsize;
561 }
562 // Decode pps from avcC
563 cnt = *(p++); // Number of pps
564 for (i = 0; i < cnt; i++) {
565 nalsize = AV_RB16(p) + 2;
566 if (p - avctx->extradata + nalsize > avctx->extradata_size)
567 return AVERROR_INVALIDDATA;
568 ret = decode_nal_units(h, p, nalsize, 1);
569 if (ret < 0) {
570 av_log(avctx, AV_LOG_ERROR,
571 "Decoding pps %d from avcC failed\n", i);
572 return ret;
573 }
574 p += nalsize;
575 }
576 // Store right nal length size that will be used to parse all other nals
577 h->nal_length_size = (avctx->extradata[4] & 0x03) + 1;
578 } else {
579 h->is_avc = 0;
580 ret = decode_nal_units(h, avctx->extradata, avctx->extradata_size, 1);
581 if (ret < 0)
582 return ret;
583 }
584 return 0;
585 }
586
587 av_cold int ff_h264_decode_init(AVCodecContext *avctx)
588 {
589 H264Context *h = avctx->priv_data;
590 int i;
591 int ret;
592
593 h->avctx = avctx;
594
595 h->bit_depth_luma = 8;
596 h->chroma_format_idc = 1;
597
598 ff_h264dsp_init(&h->h264dsp, 8, 1);
599 ff_h264chroma_init(&h->h264chroma, h->sps.bit_depth_chroma);
600 ff_h264qpel_init(&h->h264qpel, 8);
601 ff_h264_pred_init(&h->hpc, h->avctx->codec_id, 8, 1);
602
603 h->dequant_coeff_pps = -1;
604
605 /* needed so that IDCT permutation is known early */
606 ff_videodsp_init(&h->vdsp, 8);
607
608 memset(h->pps.scaling_matrix4, 16, 6 * 16 * sizeof(uint8_t));
609 memset(h->pps.scaling_matrix8, 16, 2 * 64 * sizeof(uint8_t));
610
611 h->picture_structure = PICT_FRAME;
612 h->slice_context_count = 1;
613 h->workaround_bugs = avctx->workaround_bugs;
614 h->flags = avctx->flags;
615
616 /* set defaults */
617 // s->decode_mb = ff_h263_decode_mb;
618 if (!avctx->has_b_frames)
619 h->low_delay = 1;
620
621 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
622
623 ff_h264_decode_init_vlc();
624
625 ff_init_cabac_states();
626
627 h->pixel_shift = 0;
628 h->sps.bit_depth_luma = avctx->bits_per_raw_sample = 8;
629
630 h->nb_slice_ctx = (avctx->active_thread_type & FF_THREAD_SLICE) ? H264_MAX_THREADS : 1;
631 h->slice_ctx = av_mallocz_array(h->nb_slice_ctx, sizeof(*h->slice_ctx));
632 if (!h->slice_ctx) {
633 h->nb_slice_ctx = 0;
634 return AVERROR(ENOMEM);
635 }
636
637 for (i = 0; i < h->nb_slice_ctx; i++)
638 h->slice_ctx[i].h264 = h;
639
640 h->outputed_poc = h->next_outputed_poc = INT_MIN;
641 for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
642 h->last_pocs[i] = INT_MIN;
643 h->prev_poc_msb = 1 << 16;
644 h->x264_build = -1;
645 ff_h264_reset_sei(h);
646 h->recovery_frame = -1;
647 h->frame_recovered = 0;
648 if (avctx->codec_id == AV_CODEC_ID_H264) {
649 if (avctx->ticks_per_frame == 1)
650 h->avctx->framerate.num *= 2;
651 avctx->ticks_per_frame = 2;
652 }
653
654 if (avctx->extradata_size > 0 && avctx->extradata) {
655 ret = ff_h264_decode_extradata(h);
656 if (ret < 0) {
657 ff_h264_free_context(h);
658 return ret;
659 }
660 }
661
662 if (h->sps.bitstream_restriction_flag &&
663 h->avctx->has_b_frames < h->sps.num_reorder_frames) {
664 h->avctx->has_b_frames = h->sps.num_reorder_frames;
665 h->low_delay = 0;
666 }
667
668 avctx->internal->allocate_progress = 1;
669
670 if (h->enable_er) {
671 av_log(avctx, AV_LOG_WARNING,
672 "Error resilience is enabled. It is unsafe and unsupported and may crash. "
673 "Use it at your own risk\n");
674 }
675
676 return 0;
677 }
678
679 static int decode_init_thread_copy(AVCodecContext *avctx)
680 {
681 H264Context *h = avctx->priv_data;
682 int i;
683
684 if (!avctx->internal->is_copy)
685 return 0;
686 memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
687 memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
688
689 h->nb_slice_ctx = (avctx->active_thread_type & FF_THREAD_SLICE) ? H264_MAX_THREADS : 1;
690 h->slice_ctx = av_mallocz_array(h->nb_slice_ctx, sizeof(*h->slice_ctx));
691 if (!h->slice_ctx) {
692 h->nb_slice_ctx = 0;
693 return AVERROR(ENOMEM);
694 }
695
696 for (i = 0; i < h->nb_slice_ctx; i++)
697 h->slice_ctx[i].h264 = h;
698
699 h->avctx = avctx;
700 h->context_initialized = 0;
701
702 return 0;
703 }
704
705 /**
706 * Run setup operations that must be run after slice header decoding.
707 * This includes finding the next displayed frame.
708 *
709 * @param h h264 master context
710 * @param setup_finished enough NALs have been read that we can call
711 * ff_thread_finish_setup()
712 */
713 static void decode_postinit(H264Context *h, int setup_finished)
714 {
715 H264Picture *out = h->cur_pic_ptr;
716 H264Picture *cur = h->cur_pic_ptr;
717 int i, pics, out_of_order, out_idx;
718 int invalid = 0, cnt = 0;
719
720 h->cur_pic_ptr->f.pict_type = h->pict_type;
721
722 if (h->next_output_pic)
723 return;
724
725 if (cur->field_poc[0] == INT_MAX || cur->field_poc[1] == INT_MAX) {
726 /* FIXME: if we have two PAFF fields in one packet, we can't start
727 * the next thread here. If we have one field per packet, we can.
728 * The check in decode_nal_units() is not good enough to find this
729 * yet, so we assume the worst for now. */
730 // if (setup_finished)
731 // ff_thread_finish_setup(h->avctx);
732 return;
733 }
734
735 cur->f.interlaced_frame = 0;
736 cur->f.repeat_pict = 0;
737
738 /* Signal interlacing information externally. */
739 /* Prioritize picture timing SEI information over used
740 * decoding process if it exists. */
741
742 if (h->sps.pic_struct_present_flag) {
743 switch (h->sei_pic_struct) {
744 case SEI_PIC_STRUCT_FRAME:
745 break;
746 case SEI_PIC_STRUCT_TOP_FIELD:
747 case SEI_PIC_STRUCT_BOTTOM_FIELD:
748 cur->f.interlaced_frame = 1;
749 break;
750 case SEI_PIC_STRUCT_TOP_BOTTOM:
751 case SEI_PIC_STRUCT_BOTTOM_TOP:
752 if (FIELD_OR_MBAFF_PICTURE(h))
753 cur->f.interlaced_frame = 1;
754 else
755 // try to flag soft telecine progressive
756 cur->f.interlaced_frame = h->prev_interlaced_frame;
757 break;
758 case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
759 case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
760 /* Signal the possibility of telecined film externally
761 * (pic_struct 5,6). From these hints, let the applications
762 * decide if they apply deinterlacing. */
763 cur->f.repeat_pict = 1;
764 break;
765 case SEI_PIC_STRUCT_FRAME_DOUBLING:
766 cur->f.repeat_pict = 2;
767 break;
768 case SEI_PIC_STRUCT_FRAME_TRIPLING:
769 cur->f.repeat_pict = 4;
770 break;
771 }
772
773 if ((h->sei_ct_type & 3) &&
774 h->sei_pic_struct <= SEI_PIC_STRUCT_BOTTOM_TOP)
775 cur->f.interlaced_frame = (h->sei_ct_type & (1 << 1)) != 0;
776 } else {
777 /* Derive interlacing flag from used decoding process. */
778 cur->f.interlaced_frame = FIELD_OR_MBAFF_PICTURE(h);
779 }
780 h->prev_interlaced_frame = cur->f.interlaced_frame;
781
782 if (cur->field_poc[0] != cur->field_poc[1]) {
783 /* Derive top_field_first from field pocs. */
784 cur->f.top_field_first = cur->field_poc[0] < cur->field_poc[1];
785 } else {
786 if (cur->f.interlaced_frame || h->sps.pic_struct_present_flag) {
787 /* Use picture timing SEI information. Even if it is a
788 * information of a past frame, better than nothing. */
789 if (h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM ||
790 h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
791 cur->f.top_field_first = 1;
792 else
793 cur->f.top_field_first = 0;
794 } else {
795 /* Most likely progressive */
796 cur->f.top_field_first = 0;
797 }
798 }
799
800 if (h->sei_frame_packing_present &&
801 h->frame_packing_arrangement_type >= 0 &&
802 h->frame_packing_arrangement_type <= 6 &&
803 h->content_interpretation_type > 0 &&
804 h->content_interpretation_type < 3) {
805 AVStereo3D *stereo = av_stereo3d_create_side_data(&cur->f);
806 if (!stereo)
807 return;
808
809 switch (h->frame_packing_arrangement_type) {
810 case 0:
811 stereo->type = AV_STEREO3D_CHECKERBOARD;
812 break;
813 case 1:
814 stereo->type = AV_STEREO3D_COLUMNS;
815 break;
816 case 2:
817 stereo->type = AV_STEREO3D_LINES;
818 break;
819 case 3:
820 if (h->quincunx_subsampling)
821 stereo->type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
822 else
823 stereo->type = AV_STEREO3D_SIDEBYSIDE;
824 break;
825 case 4:
826 stereo->type = AV_STEREO3D_TOPBOTTOM;
827 break;
828 case 5:
829 stereo->type = AV_STEREO3D_FRAMESEQUENCE;
830 break;
831 case 6:
832 stereo->type = AV_STEREO3D_2D;
833 break;
834 }
835
836 if (h->content_interpretation_type == 2)
837 stereo->flags = AV_STEREO3D_FLAG_INVERT;
838 }
839
840 if (h->sei_display_orientation_present &&
841 (h->sei_anticlockwise_rotation || h->sei_hflip || h->sei_vflip)) {
842 double angle = h->sei_anticlockwise_rotation * 360 / (double) (1 << 16);
843 AVFrameSideData *rotation = av_frame_new_side_data(&cur->f,
844 AV_FRAME_DATA_DISPLAYMATRIX,
845 sizeof(int32_t) * 9);
846 if (!rotation)
847 return;
848
849 av_display_rotation_set((int32_t *)rotation->data, angle);
850 av_display_matrix_flip((int32_t *)rotation->data,
851 h->sei_hflip, h->sei_vflip);
852 }
853
854 // FIXME do something with unavailable reference frames
855
856 /* Sort B-frames into display order */
857
858 if (h->sps.bitstream_restriction_flag &&
859 h->avctx->has_b_frames < h->sps.num_reorder_frames) {
860 h->avctx->has_b_frames = h->sps.num_reorder_frames;
861 h->low_delay = 0;
862 }
863
864 if (h->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT &&
865 !h->sps.bitstream_restriction_flag) {
866 h->avctx->has_b_frames = MAX_DELAYED_PIC_COUNT - 1;
867 h->low_delay = 0;
868 }
869
870 pics = 0;
871 while (h->delayed_pic[pics])
872 pics++;
873
874 assert(pics <= MAX_DELAYED_PIC_COUNT);
875
876 h->delayed_pic[pics++] = cur;
877 if (cur->reference == 0)
878 cur->reference = DELAYED_PIC_REF;
879
880 /* Frame reordering. This code takes pictures from coding order and sorts
881 * them by their incremental POC value into display order. It supports POC
882 * gaps, MMCO reset codes and random resets.
883 * A "display group" can start either with a IDR frame (f.key_frame = 1),
884 * and/or can be closed down with a MMCO reset code. In sequences where
885 * there is no delay, we can't detect that (since the frame was already
886 * output to the user), so we also set h->mmco_reset to detect the MMCO
887 * reset code.
888 * FIXME: if we detect insufficient delays (as per h->avctx->has_b_frames),
889 * we increase the delay between input and output. All frames affected by
890 * the lag (e.g. those that should have been output before another frame
891 * that we already returned to the user) will be dropped. This is a bug
892 * that we will fix later. */
893 for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++) {
894 cnt += out->poc < h->last_pocs[i];
895 invalid += out->poc == INT_MIN;
896 }
897 if (!h->mmco_reset && !cur->f.key_frame &&
898 cnt + invalid == MAX_DELAYED_PIC_COUNT && cnt > 0) {
899 h->mmco_reset = 2;
900 if (pics > 1)
901 h->delayed_pic[pics - 2]->mmco_reset = 2;
902 }
903 if (h->mmco_reset || cur->f.key_frame) {
904 for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
905 h->last_pocs[i] = INT_MIN;
906 cnt = 0;
907 invalid = MAX_DELAYED_PIC_COUNT;
908 }
909 out = h->delayed_pic[0];
910 out_idx = 0;
911 for (i = 1; i < MAX_DELAYED_PIC_COUNT &&
912 h->delayed_pic[i] &&
913 !h->delayed_pic[i - 1]->mmco_reset &&
914 !h->delayed_pic[i]->f.key_frame;
915 i++)
916 if (h->delayed_pic[i]->poc < out->poc) {
917 out = h->delayed_pic[i];
918 out_idx = i;
919 }
920 if (h->avctx->has_b_frames == 0 &&
921 (h->delayed_pic[0]->f.key_frame || h->mmco_reset))
922 h->next_outputed_poc = INT_MIN;
923 out_of_order = !out->f.key_frame && !h->mmco_reset &&
924 (out->poc < h->next_outputed_poc);
925
926 if (h->sps.bitstream_restriction_flag &&
927 h->avctx->has_b_frames >= h->sps.num_reorder_frames) {
928 } else if (out_of_order && pics - 1 == h->avctx->has_b_frames &&
929 h->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT) {
930 if (invalid + cnt < MAX_DELAYED_PIC_COUNT) {
931 h->avctx->has_b_frames = FFMAX(h->avctx->has_b_frames, cnt);
932 }
933 h->low_delay = 0;
934 } else if (h->low_delay &&
935 ((h->next_outputed_poc != INT_MIN &&
936 out->poc > h->next_outputed_poc + 2) ||
937 cur->f.pict_type == AV_PICTURE_TYPE_B)) {
938 h->low_delay = 0;
939 h->avctx->has_b_frames++;
940 }
941
942 if (pics > h->avctx->has_b_frames) {
943 out->reference &= ~DELAYED_PIC_REF;
944 // for frame threading, the owner must be the second field's thread or
945 // else the first thread can release the picture and reuse it unsafely
946 for (i = out_idx; h->delayed_pic[i]; i++)
947 h->delayed_pic[i] = h->delayed_pic[i + 1];
948 }
949 memmove(h->last_pocs, &h->last_pocs[1],
950 sizeof(*h->last_pocs) * (MAX_DELAYED_PIC_COUNT - 1));
951 h->last_pocs[MAX_DELAYED_PIC_COUNT - 1] = cur->poc;
952 if (!out_of_order && pics > h->avctx->has_b_frames) {
953 h->next_output_pic = out;
954 if (out->mmco_reset) {
955 if (out_idx > 0) {
956 h->next_outputed_poc = out->poc;
957 h->delayed_pic[out_idx - 1]->mmco_reset = out->mmco_reset;
958 } else {
959 h->next_outputed_poc = INT_MIN;
960 }
961 } else {
962 if (out_idx == 0 && pics > 1 && h->delayed_pic[0]->f.key_frame) {
963 h->next_outputed_poc = INT_MIN;
964 } else {
965 h->next_outputed_poc = out->poc;
966 }
967 }
968 h->mmco_reset = 0;
969 } else {
970 av_log(h->avctx, AV_LOG_DEBUG, "no picture\n");
971 }
972
973 if (h->next_output_pic) {
974 if (h->next_output_pic->recovered) {
975 // We have reached an recovery point and all frames after it in
976 // display order are "recovered".
977 h->frame_recovered |= FRAME_RECOVERED_SEI;
978 }
979 h->next_output_pic->recovered |= !!(h->frame_recovered & FRAME_RECOVERED_SEI);
980 }
981
982 if (setup_finished && !h->avctx->hwaccel)
983 ff_thread_finish_setup(h->avctx);
984 }
985
986 int ff_pred_weight_table(H264Context *h, H264SliceContext *sl)
987 {
988 int list, i;
989 int luma_def, chroma_def;
990
991 sl->use_weight = 0;
992 sl->use_weight_chroma = 0;
993 sl->luma_log2_weight_denom = get_ue_golomb(&sl->gb);
994 if (h->sps.chroma_format_idc)
995 sl->chroma_log2_weight_denom = get_ue_golomb(&sl->gb);
996 luma_def = 1 << sl->luma_log2_weight_denom;
997 chroma_def = 1 << sl->chroma_log2_weight_denom;
998
999 for (list = 0; list < 2; list++) {
1000 sl->luma_weight_flag[list] = 0;
1001 sl->chroma_weight_flag[list] = 0;
1002 for (i = 0; i < sl->ref_count[list]; i++) {
1003 int luma_weight_flag, chroma_weight_flag;
1004
1005 luma_weight_flag = get_bits1(&sl->gb);
1006 if (luma_weight_flag) {
1007 sl->luma_weight[i][list][0] = get_se_golomb(&sl->gb);
1008 sl->luma_weight[i][list][1] = get_se_golomb(&sl->gb);
1009 if (sl->luma_weight[i][list][0] != luma_def ||
1010 sl->luma_weight[i][list][1] != 0) {
1011 sl->use_weight = 1;
1012 sl->luma_weight_flag[list] = 1;
1013 }
1014 } else {
1015 sl->luma_weight[i][list][0] = luma_def;
1016 sl->luma_weight[i][list][1] = 0;
1017 }
1018
1019 if (h->sps.chroma_format_idc) {
1020 chroma_weight_flag = get_bits1(&sl->gb);
1021 if (chroma_weight_flag) {
1022 int j;
1023 for (j = 0; j < 2; j++) {
1024 sl->chroma_weight[i][list][j][0] = get_se_golomb(&sl->gb);
1025 sl->chroma_weight[i][list][j][1] = get_se_golomb(&sl->gb);
1026 if (sl->chroma_weight[i][list][j][0] != chroma_def ||
1027 sl->chroma_weight[i][list][j][1] != 0) {
1028 sl->use_weight_chroma = 1;
1029 sl->chroma_weight_flag[list] = 1;
1030 }
1031 }
1032 } else {
1033 int j;
1034 for (j = 0; j < 2; j++) {
1035 sl->chroma_weight[i][list][j][0] = chroma_def;
1036 sl->chroma_weight[i][list][j][1] = 0;
1037 }
1038 }
1039 }
1040 }
1041 if (sl->slice_type_nos != AV_PICTURE_TYPE_B)
1042 break;
1043 }
1044 sl->use_weight = sl->use_weight || sl->use_weight_chroma;
1045 return 0;
1046 }
1047
1048 /**
1049 * instantaneous decoder refresh.
1050 */
1051 static void idr(H264Context *h)
1052 {
1053 ff_h264_remove_all_refs(h);
1054 h->prev_frame_num =
1055 h->prev_frame_num_offset =
1056 h->prev_poc_msb =
1057 h->prev_poc_lsb = 0;
1058 }
1059
1060 /* forget old pics after a seek */
1061 void ff_h264_flush_change(H264Context *h)
1062 {
1063 int i;
1064 for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
1065 h->last_pocs[i] = INT_MIN;
1066 h->outputed_poc = h->next_outputed_poc = INT_MIN;
1067 h->prev_interlaced_frame = 1;
1068 idr(h);
1069 if (h->cur_pic_ptr)
1070 h->cur_pic_ptr->reference = 0;
1071 h->first_field = 0;
1072 ff_h264_reset_sei(h);
1073 h->recovery_frame = -1;
1074 h->frame_recovered = 0;
1075 }
1076
1077 /* forget old pics after a seek */
1078 static void flush_dpb(AVCodecContext *avctx)
1079 {
1080 H264Context *h = avctx->priv_data;
1081 int i;
1082
1083 memset(h->delayed_pic, 0, sizeof(h->delayed_pic));
1084
1085 ff_h264_flush_change(h);
1086
1087 if (h->DPB)
1088 for (i = 0; i < H264_MAX_PICTURE_COUNT; i++)
1089 ff_h264_unref_picture(h, &h->DPB[i]);
1090 h->cur_pic_ptr = NULL;
1091 ff_h264_unref_picture(h, &h->cur_pic);
1092
1093 h->mb_y = 0;
1094
1095 ff_h264_free_tables(h, 1);
1096 h->context_initialized = 0;
1097 }
1098
1099 int ff_init_poc(H264Context *h, int pic_field_poc[2], int *pic_poc)
1100 {
1101 const int max_frame_num = 1 << h->sps.log2_max_frame_num;
1102 int field_poc[2];
1103
1104 h->frame_num_offset = h->prev_frame_num_offset;
1105 if (h->frame_num < h->prev_frame_num)
1106 h->frame_num_offset += max_frame_num;
1107
1108 if (h->sps.poc_type == 0) {
1109 const int max_poc_lsb = 1 << h->sps.log2_max_poc_lsb;
1110
1111 if (h->poc_lsb < h->prev_poc_lsb &&
1112 h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb / 2)
1113 h->poc_msb = h->prev_poc_msb + max_poc_lsb;
1114 else if (h->poc_lsb > h->prev_poc_lsb &&
1115 h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb / 2)
1116 h->poc_msb = h->prev_poc_msb - max_poc_lsb;
1117 else
1118 h->poc_msb = h->prev_poc_msb;
1119 field_poc[0] =
1120 field_poc[1] = h->poc_msb + h->poc_lsb;
1121 if (h->picture_structure == PICT_FRAME)
1122 field_poc[1] += h->delta_poc_bottom;
1123 } else if (h->sps.poc_type == 1) {
1124 int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
1125 int i;
1126
1127 if (h->sps.poc_cycle_length != 0)
1128 abs_frame_num = h->frame_num_offset + h->frame_num;
1129 else
1130 abs_frame_num = 0;
1131
1132 if (h->nal_ref_idc == 0 && abs_frame_num > 0)
1133 abs_frame_num--;
1134
1135 expected_delta_per_poc_cycle = 0;
1136 for (i = 0; i < h->sps.poc_cycle_length; i++)
1137 // FIXME integrate during sps parse
1138 expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[i];
1139
1140 if (abs_frame_num > 0) {
1141 int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
1142 int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
1143
1144 expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
1145 for (i = 0; i <= frame_num_in_poc_cycle; i++)
1146 expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[i];
1147 } else
1148 expectedpoc = 0;
1149
1150 if (h->nal_ref_idc == 0)
1151 expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
1152
1153 field_poc[0] = expectedpoc + h->delta_poc[0];
1154 field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
1155
1156 if (h->picture_structure == PICT_FRAME)
1157 field_poc[1] += h->delta_poc[1];
1158 } else {
1159 int poc = 2 * (h->frame_num_offset + h->frame_num);
1160
1161 if (!h->nal_ref_idc)
1162 poc--;
1163
1164 field_poc[0] = poc;
1165 field_poc[1] = poc;
1166 }
1167
1168 if (h->picture_structure != PICT_BOTTOM_FIELD)
1169 pic_field_poc[0] = field_poc[0];
1170 if (h->picture_structure != PICT_TOP_FIELD)
1171 pic_field_poc[1] = field_poc[1];
1172 *pic_poc = FFMIN(pic_field_poc[0], pic_field_poc[1]);
1173
1174 return 0;
1175 }
1176
1177 /**
1178 * Compute profile from profile_idc and constraint_set?_flags.
1179 *
1180 * @param sps SPS
1181 *
1182 * @return profile as defined by FF_PROFILE_H264_*
1183 */
1184 int ff_h264_get_profile(SPS *sps)
1185 {
1186 int profile = sps->profile_idc;
1187
1188 switch (sps->profile_idc) {
1189 case FF_PROFILE_H264_BASELINE:
1190 // constraint_set1_flag set to 1
1191 profile |= (sps->constraint_set_flags & 1 << 1) ? FF_PROFILE_H264_CONSTRAINED : 0;
1192 break;
1193 case FF_PROFILE_H264_HIGH_10:
1194 case FF_PROFILE_H264_HIGH_422:
1195 case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
1196 // constraint_set3_flag set to 1
1197 profile |= (sps->constraint_set_flags & 1 << 3) ? FF_PROFILE_H264_INTRA : 0;
1198 break;
1199 }
1200
1201 return profile;
1202 }
1203
1204 int ff_h264_set_parameter_from_sps(H264Context *h)
1205 {
1206 if (h->flags & CODEC_FLAG_LOW_DELAY ||
1207 (h->sps.bitstream_restriction_flag &&
1208 !h->sps.num_reorder_frames)) {
1209 if (h->avctx->has_b_frames > 1 || h->delayed_pic[0])
1210 av_log(h->avctx, AV_LOG_WARNING, "Delayed frames seen. "
1211 "Reenabling low delay requires a codec flush.\n");
1212 else
1213 h->low_delay = 1;
1214 }
1215
1216 if (h->avctx->has_b_frames < 2)
1217 h->avctx->has_b_frames = !h->low_delay;
1218
1219 if (h->avctx->bits_per_raw_sample != h->sps.bit_depth_luma ||
1220 h->cur_chroma_format_idc != h->sps.chroma_format_idc) {
1221 if (h->sps.bit_depth_luma >= 8 && h->sps.bit_depth_luma <= 10) {
1222 h->avctx->bits_per_raw_sample = h->sps.bit_depth_luma;
1223 h->cur_chroma_format_idc = h->sps.chroma_format_idc;
1224 h->pixel_shift = h->sps.bit_depth_luma > 8;
1225
1226 ff_h264dsp_init(&h->h264dsp, h->sps.bit_depth_luma,
1227 h->sps.chroma_format_idc);
1228 ff_h264chroma_init(&h->h264chroma, h->sps.bit_depth_chroma);
1229 ff_h264qpel_init(&h->h264qpel, h->sps.bit_depth_luma);
1230 ff_h264_pred_init(&h->hpc, h->avctx->codec_id, h->sps.bit_depth_luma,
1231 h->sps.chroma_format_idc);
1232 ff_videodsp_init(&h->vdsp, h->sps.bit_depth_luma);
1233 } else {
1234 av_log(h->avctx, AV_LOG_ERROR, "Unsupported bit depth %d\n",
1235 h->sps.bit_depth_luma);
1236 return AVERROR_INVALIDDATA;
1237 }
1238 }
1239 return 0;
1240 }
1241
1242 int ff_set_ref_count(H264Context *h, H264SliceContext *sl)
1243 {
1244 int ref_count[2], list_count;
1245 int num_ref_idx_active_override_flag, max_refs;
1246
1247 // set defaults, might be overridden a few lines later
1248 ref_count[0] = h->pps.ref_count[0];
1249 ref_count[1] = h->pps.ref_count[1];
1250
1251 if (sl->slice_type_nos != AV_PICTURE_TYPE_I) {
1252 if (sl->slice_type_nos == AV_PICTURE_TYPE_B)
1253 sl->direct_spatial_mv_pred = get_bits1(&sl->gb);
1254 num_ref_idx_active_override_flag = get_bits1(&sl->gb);
1255
1256 if (num_ref_idx_active_override_flag) {
1257 ref_count[0] = get_ue_golomb(&sl->gb) + 1;
1258 if (ref_count[0] < 1)
1259 return AVERROR_INVALIDDATA;
1260 if (sl->slice_type_nos == AV_PICTURE_TYPE_B) {
1261 ref_count[1] = get_ue_golomb(&sl->gb) + 1;
1262 if (ref_count[1] < 1)
1263 return AVERROR_INVALIDDATA;
1264 }
1265 }
1266
1267 if (sl->slice_type_nos == AV_PICTURE_TYPE_B)
1268 list_count = 2;
1269 else
1270 list_count = 1;
1271 } else {
1272 list_count = 0;
1273 ref_count[0] = ref_count[1] = 0;
1274 }
1275
1276 max_refs = h->picture_structure == PICT_FRAME ? 16 : 32;
1277
1278 if (ref_count[0] > max_refs || ref_count[1] > max_refs) {
1279 av_log(h->avctx, AV_LOG_ERROR, "reference overflow\n");
1280 sl->ref_count[0] = sl->ref_count[1] = 0;
1281 return AVERROR_INVALIDDATA;
1282 }
1283
1284 if (list_count != sl->list_count ||
1285 ref_count[0] != sl->ref_count[0] ||
1286 ref_count[1] != sl->ref_count[1]) {
1287 sl->ref_count[0] = ref_count[0];
1288 sl->ref_count[1] = ref_count[1];
1289 sl->list_count = list_count;
1290 return 1;
1291 }
1292
1293 return 0;
1294 }
1295
1296 static int find_start_code(const uint8_t *buf, int buf_size,
1297 int buf_index, int next_avc)
1298 {
1299 // start code prefix search
1300 for (; buf_index + 3 < next_avc; buf_index++)
1301 // This should always succeed in the first iteration.
1302 if (buf[buf_index] == 0 &&
1303 buf[buf_index + 1] == 0 &&
1304 buf[buf_index + 2] == 1)
1305 break;
1306
1307 if (buf_index + 3 >= buf_size)
1308 return buf_size;
1309
1310 return buf_index + 3;
1311 }
1312
1313 static int get_avc_nalsize(H264Context *h, const uint8_t *buf,
1314 int buf_size, int *buf_index)
1315 {
1316 int i, nalsize = 0;
1317
1318 if (*buf_index >= buf_size - h->nal_length_size)
1319 return -1;
1320
1321 for (i = 0; i < h->nal_length_size; i++)
1322 nalsize = (nalsize << 8) | buf[(*buf_index)++];
1323 if (nalsize <= 0 || nalsize > buf_size - *buf_index) {
1324 av_log(h->avctx, AV_LOG_ERROR,
1325 "AVC: nal size %d\n", nalsize);
1326 return -1;
1327 }
1328 return nalsize;
1329 }
1330
1331 static int get_bit_length(H264Context *h, const uint8_t *buf,
1332 const uint8_t *ptr, int dst_length,
1333 int i, int next_avc)
1334 {
1335 if ((h->workaround_bugs & FF_BUG_AUTODETECT) && i + 3 < next_avc &&
1336 buf[i] == 0x00 && buf[i + 1] == 0x00 &&
1337 buf[i + 2] == 0x01 && buf[i + 3] == 0xE0)
1338 h->workaround_bugs |= FF_BUG_TRUNCATED;
1339
1340 if (!(h->workaround_bugs & FF_BUG_TRUNCATED))
1341 while (dst_length > 0 && ptr[dst_length - 1] == 0)
1342 dst_length--;
1343
1344 if (!dst_length)
1345 return 0;
1346
1347 return 8 * dst_length - decode_rbsp_trailing(h, ptr + dst_length - 1);
1348 }
1349
1350 static int get_last_needed_nal(H264Context *h, const uint8_t *buf, int buf_size)
1351 {
1352 int next_avc = h->is_avc ? 0 : buf_size;
1353 int nal_index = 0;
1354 int buf_index = 0;
1355 int nals_needed = 0;
1356
1357 while(1) {
1358 GetBitContext gb;
1359 int nalsize = 0;
1360 int dst_length, bit_length, consumed;
1361 const uint8_t *ptr;
1362
1363 if (buf_index >= next_avc) {
1364 nalsize = get_avc_nalsize(h, buf, buf_size, &buf_index);
1365 if (nalsize < 0)
1366 break;
1367 next_avc = buf_index + nalsize;
1368 } else {
1369 buf_index = find_start_code(buf, buf_size, buf_index, next_avc);
1370 if (buf_index >= buf_size)
1371 break;
1372 }
1373
1374 ptr = ff_h264_decode_nal(h, &h->slice_ctx[0], buf + buf_index, &dst_length, &consumed,
1375 next_avc - buf_index);
1376
1377 if (!ptr || dst_length < 0)
1378 return AVERROR_INVALIDDATA;
1379
1380 buf_index += consumed;
1381
1382 bit_length = get_bit_length(h, buf, ptr, dst_length,
1383 buf_index, next_avc);
1384 nal_index++;
1385
1386 /* packets can sometimes contain multiple PPS/SPS,
1387 * e.g. two PAFF field pictures in one packet, or a demuxer
1388 * which splits NALs strangely if so, when frame threading we
1389 * can't start the next thread until we've read all of them */
1390 switch (h->nal_unit_type) {
1391 case NAL_SPS:
1392 case NAL_PPS:
1393 nals_needed = nal_index;
1394 break;
1395 case NAL_DPA:
1396 case NAL_IDR_SLICE:
1397 case NAL_SLICE:
1398 init_get_bits(&gb, ptr, bit_length);
1399 if (!get_ue_golomb(&gb))
1400 nals_needed = nal_index;
1401 }
1402 }
1403
1404 return nals_needed;
1405 }
1406
1407 static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size,
1408 int parse_extradata)
1409 {
1410 AVCodecContext *const avctx = h->avctx;
1411 H264SliceContext *sl;
1412 int buf_index;
1413 unsigned context_count;
1414 int next_avc;
1415 int nals_needed = 0; ///< number of NALs that need decoding before the next frame thread starts
1416 int nal_index;
1417 int ret = 0;
1418
1419 h->max_contexts = h->slice_context_count;
1420 if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS)) {
1421 h->current_slice = 0;
1422 if (!h->first_field)
1423 h->cur_pic_ptr = NULL;
1424 ff_h264_reset_sei(h);
1425 }
1426
1427 if (avctx->active_thread_type & FF_THREAD_FRAME)
1428 nals_needed = get_last_needed_nal(h, buf, buf_size);
1429
1430 {
1431 buf_index = 0;
1432 context_count = 0;
1433 next_avc = h->is_avc ? 0 : buf_size;
1434 nal_index = 0;
1435 for (;;) {
1436 int consumed;
1437 int dst_length;
1438 int bit_length;
1439 const uint8_t *ptr;
1440 int nalsize = 0;
1441 int err;
1442
1443 if (buf_index >= next_avc) {
1444 nalsize = get_avc_nalsize(h, buf, buf_size, &buf_index);
1445 if (nalsize < 0)
1446 break;
1447 next_avc = buf_index + nalsize;
1448 } else {
1449 buf_index = find_start_code(buf, buf_size, buf_index, next_avc);
1450 if (buf_index >= buf_size)
1451 break;
1452 if (buf_index >= next_avc)
1453 continue;
1454 }
1455
1456 sl = &h->slice_ctx[context_count];
1457
1458 ptr = ff_h264_decode_nal(h, sl, buf + buf_index, &dst_length,
1459 &consumed, next_avc - buf_index);
1460 if (!ptr || dst_length < 0) {
1461 ret = -1;
1462 goto end;
1463 }
1464
1465 bit_length = get_bit_length(h, buf, ptr, dst_length,
1466 buf_index + consumed, next_avc);
1467
1468 if (h->avctx->debug & FF_DEBUG_STARTCODE)
1469 av_log(h->avctx, AV_LOG_DEBUG,
1470 "NAL %d at %d/%d length %d\n",
1471 h->nal_unit_type, buf_index, buf_size, dst_length);
1472
1473 if (h->is_avc && (nalsize != consumed) && nalsize)
1474 av_log(h->avctx, AV_LOG_DEBUG,
1475 "AVC: Consumed only %d bytes instead of %d\n",
1476 consumed, nalsize);
1477
1478 buf_index += consumed;
1479 nal_index++;
1480
1481 if (avctx->skip_frame >= AVDISCARD_NONREF &&
1482 h->nal_ref_idc == 0 &&
1483 h->nal_unit_type != NAL_SEI)
1484 continue;
1485
1486 again:
1487 /* Ignore every NAL unit type except PPS and SPS during extradata
1488 * parsing. Decoding slices is not possible in codec init
1489 * with frame-mt */
1490 if (parse_extradata && HAVE_THREADS &&
1491 (h->avctx->active_thread_type & FF_THREAD_FRAME) &&
1492 (h->nal_unit_type != NAL_PPS &&
1493 h->nal_unit_type != NAL_SPS)) {
1494 if (h->nal_unit_type < NAL_AUD ||
1495 h->nal_unit_type > NAL_AUXILIARY_SLICE)
1496 av_log(avctx, AV_LOG_INFO,
1497 "Ignoring NAL unit %d during extradata parsing\n",
1498 h->nal_unit_type);
1499 h->nal_unit_type = NAL_FF_IGNORE;
1500 }
1501 err = 0;
1502 switch (h->nal_unit_type) {
1503 case NAL_IDR_SLICE:
1504 if (h->nal_unit_type != NAL_IDR_SLICE) {
1505 av_log(h->avctx, AV_LOG_ERROR,
1506 "Invalid mix of idr and non-idr slices\n");
1507 ret = -1;
1508 goto end;
1509 }
1510 idr(h); // FIXME ensure we don't lose some frames if there is reordering
1511 case NAL_SLICE:
1512 init_get_bits(&sl->gb, ptr, bit_length);
1513
1514 if ((err = ff_h264_decode_slice_header(h, sl)))
1515 break;
1516
1517 if (h->sei_recovery_frame_cnt >= 0 && h->recovery_frame < 0) {
1518 h->recovery_frame = (h->frame_num + h->sei_recovery_frame_cnt) &
1519 ((1 << h->sps.log2_max_frame_num) - 1);
1520 }
1521
1522 h->cur_pic_ptr->f.key_frame |=
1523 (h->nal_unit_type == NAL_IDR_SLICE) ||
1524 (h->sei_recovery_frame_cnt >= 0);
1525
1526 if (h->nal_unit_type == NAL_IDR_SLICE ||
1527 h->recovery_frame == h->frame_num) {
1528 h->recovery_frame = -1;
1529 h->cur_pic_ptr->recovered = 1;
1530 }
1531 // If we have an IDR, all frames after it in decoded order are
1532 // "recovered".
1533 if (h->nal_unit_type == NAL_IDR_SLICE)
1534 h->frame_recovered |= FRAME_RECOVERED_IDR;
1535 h->cur_pic_ptr->recovered |= !!(h->frame_recovered & FRAME_RECOVERED_IDR);
1536
1537 if (h->current_slice == 1) {
1538 if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS))
1539 decode_postinit(h, nal_index >= nals_needed);
1540
1541 if (h->avctx->hwaccel &&
1542 (ret = h->avctx->hwaccel->start_frame(h->avctx, NULL, 0)) < 0)
1543 return ret;
1544 }
1545
1546 if (sl->redundant_pic_count == 0 &&
1547 (avctx->skip_frame < AVDISCARD_NONREF ||
1548 h->nal_ref_idc) &&
1549 (avctx->skip_frame < AVDISCARD_BIDIR ||
1550 sl->slice_type_nos != AV_PICTURE_TYPE_B) &&
1551 (avctx->skip_frame < AVDISCARD_NONKEY ||
1552 sl->slice_type_nos == AV_PICTURE_TYPE_I) &&
1553 avctx->skip_frame < AVDISCARD_ALL) {
1554 if (avctx->hwaccel) {
1555 ret = avctx->hwaccel->decode_slice(avctx,
1556 &buf[buf_index - consumed],
1557 consumed);
1558 if (ret < 0)
1559 return ret;
1560 } else
1561 context_count++;
1562 }
1563 break;
1564 case NAL_DPA:
1565 case NAL_DPB:
1566 case NAL_DPC:
1567 avpriv_request_sample(avctx, "data partitioning");
1568 ret = AVERROR(ENOSYS);
1569 goto end;
1570 break;
1571 case NAL_SEI:
1572 init_get_bits(&h->gb, ptr, bit_length);
1573 ret = ff_h264_decode_sei(h);
1574 if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1575 goto end;
1576 break;
1577 case NAL_SPS:
1578 init_get_bits(&h->gb, ptr, bit_length);
1579 ret = ff_h264_decode_seq_parameter_set(h);
1580 if (ret < 0 && h->is_avc && (nalsize != consumed) && nalsize) {
1581 av_log(h->avctx, AV_LOG_DEBUG,
1582 "SPS decoding failure, trying again with the complete NAL\n");
1583 init_get_bits(&h->gb, buf + buf_index + 1 - consumed,
1584 8 * (nalsize - 1));
1585 ff_h264_decode_seq_parameter_set(h);
1586 }
1587
1588 ret = ff_h264_set_parameter_from_sps(h);
1589 if (ret < 0)
1590 goto end;
1591
1592 break;
1593 case NAL_PPS:
1594 init_get_bits(&h->gb, ptr, bit_length);
1595 ret = ff_h264_decode_picture_parameter_set(h, bit_length);
1596 if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1597 goto end;
1598 break;
1599 case NAL_AUD:
1600 case NAL_END_SEQUENCE:
1601 case NAL_END_STREAM:
1602 case NAL_FILLER_DATA:
1603 case NAL_SPS_EXT:
1604 case NAL_AUXILIARY_SLICE:
1605 break;
1606 case NAL_FF_IGNORE:
1607 break;
1608 default:
1609 av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n",
1610 h->nal_unit_type, bit_length);
1611 }
1612
1613 if (context_count == h->max_contexts) {
1614 ret = ff_h264_execute_decode_slices(h, context_count);
1615 if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1616 goto end;
1617 context_count = 0;
1618 }
1619
1620 if (err < 0) {
1621 av_log(h->avctx, AV_LOG_ERROR, "decode_slice_header error\n");
1622 sl->ref_count[0] = sl->ref_count[1] = sl->list_count = 0;
1623 } else if (err == 1) {
1624 /* Slice could not be decoded in parallel mode, restart. Note
1625 * that rbsp_buffer is not transferred, but since we no longer
1626 * run in parallel mode this should not be an issue. */
1627 sl = &h->slice_ctx[0];
1628 goto again;
1629 }
1630 }
1631 }
1632 if (context_count) {
1633 ret = ff_h264_execute_decode_slices(h, context_count);
1634 if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1635 goto end;
1636 }
1637
1638 ret = 0;
1639 end:
1640 /* clean up */
1641 if (h->cur_pic_ptr && !h->droppable) {
1642 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
1643 h->picture_structure == PICT_BOTTOM_FIELD);
1644 }
1645
1646 return (ret < 0) ? ret : buf_index;
1647 }
1648
1649 /**
1650 * Return the number of bytes consumed for building the current frame.
1651 */
1652 static int get_consumed_bytes(int pos, int buf_size)
1653 {
1654 if (pos == 0)
1655 pos = 1; // avoid infinite loops (I doubt that is needed but...)
1656 if (pos + 10 > buf_size)
1657 pos = buf_size; // oops ;)
1658
1659 return pos;
1660 }
1661
1662 static int output_frame(H264Context *h, AVFrame *dst, AVFrame *src)
1663 {
1664 int i;
1665 int ret = av_frame_ref(dst, src);
1666 if (ret < 0)
1667 return ret;
1668
1669 if (!h->sps.crop)
1670 return 0;
1671
1672 for (i = 0; i < 3; i++) {
1673 int hshift = (i > 0) ? h->chroma_x_shift : 0;
1674 int vshift = (i > 0) ? h->chroma_y_shift : 0;
1675 int off = ((h->sps.crop_left >> hshift) << h->pixel_shift) +
1676 (h->sps.crop_top >> vshift) * dst->linesize[i];
1677 dst->data[i] += off;
1678 }
1679 return 0;
1680 }
1681
1682 static int h264_decode_frame(AVCodecContext *avctx, void *data,
1683 int *got_frame, AVPacket *avpkt)
1684 {
1685 const uint8_t *buf = avpkt->data;
1686 int buf_size = avpkt->size;
1687 H264Context *h = avctx->priv_data;
1688 AVFrame *pict = data;
1689 int buf_index = 0;
1690 int ret;
1691
1692 h->flags = avctx->flags;
1693
1694 /* end of stream, output what is still in the buffers */
1695 out:
1696 if (buf_size == 0) {
1697 H264Picture *out;
1698 int i, out_idx;
1699
1700 h->cur_pic_ptr = NULL;
1701
1702 // FIXME factorize this with the output code below
1703 out = h->delayed_pic[0];
1704 out_idx = 0;
1705 for (i = 1;
1706 h->delayed_pic[i] &&
1707 !h->delayed_pic[i]->f.key_frame &&
1708 !h->delayed_pic[i]->mmco_reset;
1709 i++)
1710 if (h->delayed_pic[i]->poc < out->poc) {
1711 out = h->delayed_pic[i];
1712 out_idx = i;
1713 }
1714
1715 for (i = out_idx; h->delayed_pic[i]; i++)
1716 h->delayed_pic[i] = h->delayed_pic[i + 1];
1717
1718 if (out) {
1719 ret = output_frame(h, pict, &out->f);
1720 if (ret < 0)
1721 return ret;
1722 *got_frame = 1;
1723 }
1724
1725 return buf_index;
1726 }
1727
1728 buf_index = decode_nal_units(h, buf, buf_size, 0);
1729 if (buf_index < 0)
1730 return AVERROR_INVALIDDATA;
1731
1732 if (!h->cur_pic_ptr && h->nal_unit_type == NAL_END_SEQUENCE) {
1733 buf_size = 0;
1734 goto out;
1735 }
1736
1737 if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS) && !h->cur_pic_ptr) {
1738 if (avctx->skip_frame >= AVDISCARD_NONREF)
1739 return 0;
1740 av_log(avctx, AV_LOG_ERROR, "no frame!\n");
1741 return AVERROR_INVALIDDATA;
1742 }
1743
1744 if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS) ||
1745 (h->mb_y >= h->mb_height && h->mb_height)) {
1746 if (avctx->flags2 & CODEC_FLAG2_CHUNKS)
1747 decode_postinit(h, 1);
1748
1749 ff_h264_field_end(h, &h->slice_ctx[0], 0);
1750
1751 *got_frame = 0;
1752 if (h->next_output_pic && ((avctx->flags & CODEC_FLAG_OUTPUT_CORRUPT) ||
1753 h->next_output_pic->recovered)) {
1754 if (!h->next_output_pic->recovered)
1755 h->next_output_pic->f.flags |= AV_FRAME_FLAG_CORRUPT;
1756
1757 ret = output_frame(h, pict, &h->next_output_pic->f);
1758 if (ret < 0)
1759 return ret;
1760 *got_frame = 1;
1761 }
1762 }
1763
1764 assert(pict->buf[0] || !*got_frame);
1765
1766 return get_consumed_bytes(buf_index, buf_size);
1767 }
1768
1769 av_cold void ff_h264_free_context(H264Context *h)
1770 {
1771 int i;
1772
1773 ff_h264_free_tables(h, 1); // FIXME cleanup init stuff perhaps
1774
1775 av_freep(&h->slice_ctx);
1776 h->nb_slice_ctx = 0;
1777
1778 for (i = 0; i < MAX_SPS_COUNT; i++)
1779 av_freep(h->sps_buffers + i);
1780
1781 for (i = 0; i < MAX_PPS_COUNT; i++)
1782 av_freep(h->pps_buffers + i);
1783 }
1784
1785 static av_cold int h264_decode_end(AVCodecContext *avctx)
1786 {
1787 H264Context *h = avctx->priv_data;
1788
1789 ff_h264_free_context(h);
1790
1791 ff_h264_unref_picture(h, &h->cur_pic);
1792
1793 return 0;
1794 }
1795
1796 #define OFFSET(x) offsetof(H264Context, x)
1797 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1798 static const AVOption h264_options[] = {
1799 { "enable_er", "Enable error resilience on damaged frames (unsafe)", OFFSET(enable_er), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VD },
1800 { NULL },
1801 };
1802
1803 static const AVClass h264_class = {
1804 .class_name = "h264",
1805 .item_name = av_default_item_name,
1806 .option = h264_options,
1807 .version = LIBAVUTIL_VERSION_INT,
1808 };
1809
1810 static const AVProfile profiles[] = {
1811 { FF_PROFILE_H264_BASELINE, "Baseline" },
1812 { FF_PROFILE_H264_CONSTRAINED_BASELINE, "Constrained Baseline" },
1813 { FF_PROFILE_H264_MAIN, "Main" },
1814 { FF_PROFILE_H264_EXTENDED, "Extended" },
1815 { FF_PROFILE_H264_HIGH, "High" },
1816 { FF_PROFILE_H264_HIGH_10, "High 10" },
1817 { FF_PROFILE_H264_HIGH_10_INTRA, "High 10 Intra" },
1818 { FF_PROFILE_H264_HIGH_422, "High 4:2:2" },
1819 { FF_PROFILE_H264_HIGH_422_INTRA, "High 4:2:2 Intra" },
1820 { FF_PROFILE_H264_HIGH_444, "High 4:4:4" },
1821 { FF_PROFILE_H264_HIGH_444_PREDICTIVE, "High 4:4:4 Predictive" },
1822 { FF_PROFILE_H264_HIGH_444_INTRA, "High 4:4:4 Intra" },
1823 { FF_PROFILE_H264_CAVLC_444, "CAVLC 4:4:4" },
1824 { FF_PROFILE_UNKNOWN },
1825 };
1826
1827 AVCodec ff_h264_decoder = {
1828 .name = "h264",
1829 .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
1830 .type = AVMEDIA_TYPE_VIDEO,
1831 .id = AV_CODEC_ID_H264,
1832 .priv_data_size = sizeof(H264Context),
1833 .init = ff_h264_decode_init,
1834 .close = h264_decode_end,
1835 .decode = h264_decode_frame,
1836 .capabilities = /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 |
1837 CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS |
1838 CODEC_CAP_FRAME_THREADS,
1839 .flush = flush_dpb,
1840 .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
1841 .update_thread_context = ONLY_IF_THREADS_ENABLED(ff_h264_update_thread_context),
1842 .profiles = NULL_IF_CONFIG_SMALL(profiles),
1843 .priv_class = &h264_class,
1844 };