2 * H.26L/H.264/AVC/JVT/14496-10/... decoder
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
5 * This file is part of FFmpeg.
7 * FFmpeg 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.
12 * FFmpeg 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * H.264 / AVC / MPEG4 part10 codec.
25 * @author Michael Niedermayer <michaelni@gmx.at>
28 #define UNCHECKED_BITSTREAM_READER 1
30 #include "libavutil/avassert.h"
31 #include "libavutil/display.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/stereo3d.h"
35 #include "libavutil/timer.h"
38 #include "cabac_functions.h"
39 #include "error_resilience.h"
43 #include "h264chroma.h"
44 #include "h264_mvpred.h"
48 #include "mpegutils.h"
49 #include "rectangle.h"
52 #include "vdpau_internal.h"
56 const uint16_t ff_h264_mb_sizes
[4] = { 256, 384, 512, 768 };
58 int avpriv_h264_has_num_reorder_frames(AVCodecContext
*avctx
)
60 H264Context
*h
= avctx
->priv_data
;
61 return h
? h
->sps
.num_reorder_frames
: 0;
64 static void h264_er_decode_mb(void *opaque
, int ref
, int mv_dir
, int mv_type
,
66 int mb_x
, int mb_y
, int mb_intra
, int mb_skipped
)
68 H264Context
*h
= opaque
;
72 h
->mb_xy
= mb_x
+ mb_y
* h
->mb_stride
;
73 memset(h
->non_zero_count_cache
, 0, sizeof(h
->non_zero_count_cache
));
75 /* FIXME: It is possible albeit uncommon that slice references
76 * differ between slices. We take the easy approach and ignore
77 * it for now. If this turns out to have any relevance in
78 * practice then correct remapping should be added. */
79 if (ref
>= h
->ref_count
[0])
81 if (!h
->ref_list
[0][ref
].f
.data
[0]) {
82 av_log(h
->avctx
, AV_LOG_DEBUG
, "Reference not available for error concealing\n");
85 if ((h
->ref_list
[0][ref
].reference
&3) != 3) {
86 av_log(h
->avctx
, AV_LOG_DEBUG
, "Reference invalid\n");
89 fill_rectangle(&h
->cur_pic
.ref_index
[0][4 * h
->mb_xy
],
91 fill_rectangle(&h
->ref_cache
[0][scan8
[0]], 4, 4, 8, ref
, 1);
92 fill_rectangle(h
->mv_cache
[0][scan8
[0]], 4, 4, 8,
93 pack16to32((*mv
)[0][0][0], (*mv
)[0][0][1]), 4);
95 h
->mb_field_decoding_flag
= 0;
96 ff_h264_hl_decode_mb(h
);
99 void ff_h264_draw_horiz_band(H264Context
*h
, int y
, int height
)
101 AVCodecContext
*avctx
= h
->avctx
;
102 AVFrame
*cur
= &h
->cur_pic
.f
;
103 AVFrame
*last
= h
->ref_list
[0][0].f
.data
[0] ? &h
->ref_list
[0][0].f
: NULL
;
104 const AVPixFmtDescriptor
*desc
= av_pix_fmt_desc_get(avctx
->pix_fmt
);
105 int vshift
= desc
->log2_chroma_h
;
106 const int field_pic
= h
->picture_structure
!= PICT_FRAME
;
112 height
= FFMIN(height
, avctx
->height
- y
);
114 if (field_pic
&& h
->first_field
&& !(avctx
->slice_flags
& SLICE_FLAG_ALLOW_FIELD
))
117 if (avctx
->draw_horiz_band
) {
119 int offset
[AV_NUM_DATA_POINTERS
];
122 if (cur
->pict_type
== AV_PICTURE_TYPE_B
|| h
->low_delay
||
123 (avctx
->slice_flags
& SLICE_FLAG_CODED_ORDER
))
130 offset
[0] = y
* src
->linesize
[0];
132 offset
[2] = (y
>> vshift
) * src
->linesize
[1];
133 for (i
= 3; i
< AV_NUM_DATA_POINTERS
; i
++)
138 avctx
->draw_horiz_band(avctx
, src
, offset
,
139 y
, h
->picture_structure
, height
);
144 * Check if the top & left blocks are available if needed and
145 * change the dc mode so it only uses the available blocks.
147 int ff_h264_check_intra4x4_pred_mode(H264Context
*h
)
149 static const int8_t top
[12] = {
150 -1, 0, LEFT_DC_PRED
, -1, -1, -1, -1, -1, 0
152 static const int8_t left
[12] = {
153 0, -1, TOP_DC_PRED
, 0, -1, -1, -1, 0, -1, DC_128_PRED
157 if (!(h
->top_samples_available
& 0x8000)) {
158 for (i
= 0; i
< 4; i
++) {
159 int status
= top
[h
->intra4x4_pred_mode_cache
[scan8
[0] + i
]];
161 av_log(h
->avctx
, AV_LOG_ERROR
,
162 "top block unavailable for requested intra4x4 mode %d at %d %d\n",
163 status
, h
->mb_x
, h
->mb_y
);
164 return AVERROR_INVALIDDATA
;
166 h
->intra4x4_pred_mode_cache
[scan8
[0] + i
] = status
;
171 if ((h
->left_samples_available
& 0x8888) != 0x8888) {
172 static const int mask
[4] = { 0x8000, 0x2000, 0x80, 0x20 };
173 for (i
= 0; i
< 4; i
++)
174 if (!(h
->left_samples_available
& mask
[i
])) {
175 int status
= left
[h
->intra4x4_pred_mode_cache
[scan8
[0] + 8 * i
]];
177 av_log(h
->avctx
, AV_LOG_ERROR
,
178 "left block unavailable for requested intra4x4 mode %d at %d %d\n",
179 status
, h
->mb_x
, h
->mb_y
);
180 return AVERROR_INVALIDDATA
;
182 h
->intra4x4_pred_mode_cache
[scan8
[0] + 8 * i
] = status
;
188 } // FIXME cleanup like ff_h264_check_intra_pred_mode
191 * Check if the top & left blocks are available if needed and
192 * change the dc mode so it only uses the available blocks.
194 int ff_h264_check_intra_pred_mode(H264Context
*h
, int mode
, int is_chroma
)
196 static const int8_t top
[4] = { LEFT_DC_PRED8x8
, 1, -1, -1 };
197 static const int8_t left
[5] = { TOP_DC_PRED8x8
, -1, 2, -1, DC_128_PRED8x8
};
200 av_log(h
->avctx
, AV_LOG_ERROR
,
201 "out of range intra chroma pred mode at %d %d\n",
203 return AVERROR_INVALIDDATA
;
206 if (!(h
->top_samples_available
& 0x8000)) {
209 av_log(h
->avctx
, AV_LOG_ERROR
,
210 "top block unavailable for requested intra mode at %d %d\n",
212 return AVERROR_INVALIDDATA
;
216 if ((h
->left_samples_available
& 0x8080) != 0x8080) {
219 av_log(h
->avctx
, AV_LOG_ERROR
,
220 "left block unavailable for requested intra mode at %d %d\n",
222 return AVERROR_INVALIDDATA
;
224 if (is_chroma
&& (h
->left_samples_available
& 0x8080)) {
225 // mad cow disease mode, aka MBAFF + constrained_intra_pred
226 mode
= ALZHEIMER_DC_L0T_PRED8x8
+
227 (!(h
->left_samples_available
& 0x8000)) +
228 2 * (mode
== DC_128_PRED8x8
);
235 const uint8_t *ff_h264_decode_nal(H264Context
*h
, const uint8_t *src
,
236 int *dst_length
, int *consumed
, int length
)
242 // src[0]&0x80; // forbidden bit
243 h
->nal_ref_idc
= src
[0] >> 5;
244 h
->nal_unit_type
= src
[0] & 0x1F;
249 #define STARTCODE_TEST \
250 if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
251 if (src[i + 2] != 3 && src[i + 2] != 0) { \
252 /* startcode, so we must be past the end */ \
258 #if HAVE_FAST_UNALIGNED
259 #define FIND_FIRST_ZERO \
260 if (i > 0 && !src[i]) \
266 for (i
= 0; i
+ 1 < length
; i
+= 9) {
267 if (!((~AV_RN64A(src
+ i
) &
268 (AV_RN64A(src
+ i
) - 0x0100010001000101ULL
)) &
269 0x8000800080008080ULL
))
276 for (i
= 0; i
+ 1 < length
; i
+= 5) {
277 if (!((~AV_RN32A(src
+ i
) &
278 (AV_RN32A(src
+ i
) - 0x01000101U
)) &
287 for (i
= 0; i
+ 1 < length
; i
+= 2) {
290 if (i
> 0 && src
[i
- 1] == 0)
296 // use second escape buffer for inter data
297 bufidx
= h
->nal_unit_type
== NAL_DPC
? 1 : 0;
299 av_fast_padded_malloc(&h
->rbsp_buffer
[bufidx
], &h
->rbsp_buffer_size
[bufidx
], length
+MAX_MBPAIR_SIZE
);
300 dst
= h
->rbsp_buffer
[bufidx
];
305 if(i
>=length
-1){ //no escaped 0
307 *consumed
= length
+1; //+1 for the header
308 if(h
->avctx
->flags2
& CODEC_FLAG2_FAST
){
311 memcpy(dst
, src
, length
);
318 while (si
+ 2 < length
) {
319 // remove escapes (very rare 1:2^22)
320 if (src
[si
+ 2] > 3) {
321 dst
[di
++] = src
[si
++];
322 dst
[di
++] = src
[si
++];
323 } else if (src
[si
] == 0 && src
[si
+ 1] == 0 && src
[si
+ 2] != 0) {
324 if (src
[si
+ 2] == 3) { // escape
329 } else // next start code
333 dst
[di
++] = src
[si
++];
336 dst
[di
++] = src
[si
++];
339 memset(dst
+ di
, 0, FF_INPUT_BUFFER_PADDING_SIZE
);
342 *consumed
= si
+ 1; // +1 for the header
343 /* FIXME store exact number of bits in the getbitcontext
344 * (it is needed for decoding) */
349 * Identify the exact end of the bitstream
350 * @return the length of the trailing, or 0 if damaged
352 static int decode_rbsp_trailing(H264Context
*h
, const uint8_t *src
)
357 tprintf(h
->avctx
, "rbsp trailing %X\n", v
);
359 for (r
= 1; r
< 9; r
++) {
367 void ff_h264_free_tables(H264Context
*h
, int free_rbsp
)
372 av_freep(&h
->intra4x4_pred_mode
);
373 av_freep(&h
->chroma_pred_mode_table
);
374 av_freep(&h
->cbp_table
);
375 av_freep(&h
->mvd_table
[0]);
376 av_freep(&h
->mvd_table
[1]);
377 av_freep(&h
->direct_table
);
378 av_freep(&h
->non_zero_count
);
379 av_freep(&h
->slice_table_base
);
380 h
->slice_table
= NULL
;
381 av_freep(&h
->list_counts
);
383 av_freep(&h
->mb2b_xy
);
384 av_freep(&h
->mb2br_xy
);
386 av_buffer_pool_uninit(&h
->qscale_table_pool
);
387 av_buffer_pool_uninit(&h
->mb_type_pool
);
388 av_buffer_pool_uninit(&h
->motion_val_pool
);
389 av_buffer_pool_uninit(&h
->ref_index_pool
);
391 if (free_rbsp
&& h
->DPB
) {
392 for (i
= 0; i
< H264_MAX_PICTURE_COUNT
; i
++)
393 ff_h264_unref_picture(h
, &h
->DPB
[i
]);
394 memset(h
->delayed_pic
, 0, sizeof(h
->delayed_pic
));
397 for (i
= 0; i
< H264_MAX_PICTURE_COUNT
; i
++)
398 h
->DPB
[i
].needs_realloc
= 1;
401 h
->cur_pic_ptr
= NULL
;
403 for (i
= 0; i
< H264_MAX_THREADS
; i
++) {
404 hx
= h
->thread_context
[i
];
407 av_freep(&hx
->top_borders
[1]);
408 av_freep(&hx
->top_borders
[0]);
409 av_freep(&hx
->bipred_scratchpad
);
410 av_freep(&hx
->edge_emu_buffer
);
411 av_freep(&hx
->dc_val_base
);
412 av_freep(&hx
->er
.mb_index2xy
);
413 av_freep(&hx
->er
.error_status_table
);
414 av_freep(&hx
->er
.er_temp_buffer
);
415 av_freep(&hx
->er
.mbintra_table
);
416 av_freep(&hx
->er
.mbskip_table
);
419 av_freep(&hx
->rbsp_buffer
[1]);
420 av_freep(&hx
->rbsp_buffer
[0]);
421 hx
->rbsp_buffer_size
[0] = 0;
422 hx
->rbsp_buffer_size
[1] = 0;
425 av_freep(&h
->thread_context
[i
]);
429 int ff_h264_alloc_tables(H264Context
*h
)
431 const int big_mb_num
= h
->mb_stride
* (h
->mb_height
+ 1);
432 const int row_mb_num
= 2*h
->mb_stride
*FFMAX(h
->avctx
->thread_count
, 1);
435 FF_ALLOCZ_ARRAY_OR_GOTO(h
->avctx
, h
->intra4x4_pred_mode
,
436 row_mb_num
, 8 * sizeof(uint8_t), fail
)
437 FF_ALLOCZ_OR_GOTO(h
->avctx
, h
->non_zero_count
,
438 big_mb_num
* 48 * sizeof(uint8_t), fail
)
439 FF_ALLOCZ_OR_GOTO(h
->avctx
, h
->slice_table_base
,
440 (big_mb_num
+ h
->mb_stride
) * sizeof(*h
->slice_table_base
), fail
)
441 FF_ALLOCZ_OR_GOTO(h
->avctx
, h
->cbp_table
,
442 big_mb_num
* sizeof(uint16_t), fail
)
443 FF_ALLOCZ_OR_GOTO(h
->avctx
, h
->chroma_pred_mode_table
,
444 big_mb_num
* sizeof(uint8_t), fail
)
445 FF_ALLOCZ_ARRAY_OR_GOTO(h
->avctx
, h
->mvd_table
[0],
446 row_mb_num
, 16 * sizeof(uint8_t), fail
);
447 FF_ALLOCZ_ARRAY_OR_GOTO(h
->avctx
, h
->mvd_table
[1],
448 row_mb_num
, 16 * sizeof(uint8_t), fail
);
449 FF_ALLOCZ_OR_GOTO(h
->avctx
, h
->direct_table
,
450 4 * big_mb_num
* sizeof(uint8_t), fail
);
451 FF_ALLOCZ_OR_GOTO(h
->avctx
, h
->list_counts
,
452 big_mb_num
* sizeof(uint8_t), fail
)
454 memset(h
->slice_table_base
, -1,
455 (big_mb_num
+ h
->mb_stride
) * sizeof(*h
->slice_table_base
));
456 h
->slice_table
= h
->slice_table_base
+ h
->mb_stride
* 2 + 1;
458 FF_ALLOCZ_OR_GOTO(h
->avctx
, h
->mb2b_xy
,
459 big_mb_num
* sizeof(uint32_t), fail
);
460 FF_ALLOCZ_OR_GOTO(h
->avctx
, h
->mb2br_xy
,
461 big_mb_num
* sizeof(uint32_t), fail
);
462 for (y
= 0; y
< h
->mb_height
; y
++)
463 for (x
= 0; x
< h
->mb_width
; x
++) {
464 const int mb_xy
= x
+ y
* h
->mb_stride
;
465 const int b_xy
= 4 * x
+ 4 * y
* h
->b_stride
;
467 h
->mb2b_xy
[mb_xy
] = b_xy
;
468 h
->mb2br_xy
[mb_xy
] = 8 * (FMO
? mb_xy
: (mb_xy
% (2 * h
->mb_stride
)));
471 if (!h
->dequant4_coeff
[0])
472 h264_init_dequant_tables(h
);
475 h
->DPB
= av_mallocz_array(H264_MAX_PICTURE_COUNT
, sizeof(*h
->DPB
));
478 for (i
= 0; i
< H264_MAX_PICTURE_COUNT
; i
++)
479 av_frame_unref(&h
->DPB
[i
].f
);
480 av_frame_unref(&h
->cur_pic
.f
);
486 ff_h264_free_tables(h
, 1);
487 return AVERROR(ENOMEM
);
492 * Allocate buffers which are not shared amongst multiple threads.
494 int ff_h264_context_init(H264Context
*h
)
496 ERContext
*er
= &h
->er
;
497 int mb_array_size
= h
->mb_height
* h
->mb_stride
;
498 int y_size
= (2 * h
->mb_width
+ 1) * (2 * h
->mb_height
+ 1);
499 int c_size
= h
->mb_stride
* (h
->mb_height
+ 1);
500 int yc_size
= y_size
+ 2 * c_size
;
503 FF_ALLOCZ_ARRAY_OR_GOTO(h
->avctx
, h
->top_borders
[0],
504 h
->mb_width
, 16 * 3 * sizeof(uint8_t) * 2, fail
)
505 FF_ALLOCZ_ARRAY_OR_GOTO(h
->avctx
, h
->top_borders
[1],
506 h
->mb_width
, 16 * 3 * sizeof(uint8_t) * 2, fail
)
508 h
->ref_cache
[0][scan8
[5] + 1] =
509 h
->ref_cache
[0][scan8
[7] + 1] =
510 h
->ref_cache
[0][scan8
[13] + 1] =
511 h
->ref_cache
[1][scan8
[5] + 1] =
512 h
->ref_cache
[1][scan8
[7] + 1] =
513 h
->ref_cache
[1][scan8
[13] + 1] = PART_NOT_AVAILABLE
;
515 if (CONFIG_ERROR_RESILIENCE
) {
517 er
->avctx
= h
->avctx
;
519 er
->decode_mb
= h264_er_decode_mb
;
521 er
->quarter_sample
= 1;
523 er
->mb_num
= h
->mb_num
;
524 er
->mb_width
= h
->mb_width
;
525 er
->mb_height
= h
->mb_height
;
526 er
->mb_stride
= h
->mb_stride
;
527 er
->b8_stride
= h
->mb_width
* 2 + 1;
529 // error resilience code looks cleaner with this
530 FF_ALLOCZ_OR_GOTO(h
->avctx
, er
->mb_index2xy
,
531 (h
->mb_num
+ 1) * sizeof(int), fail
);
533 for (y
= 0; y
< h
->mb_height
; y
++)
534 for (x
= 0; x
< h
->mb_width
; x
++)
535 er
->mb_index2xy
[x
+ y
* h
->mb_width
] = x
+ y
* h
->mb_stride
;
537 er
->mb_index2xy
[h
->mb_height
* h
->mb_width
] = (h
->mb_height
- 1) *
538 h
->mb_stride
+ h
->mb_width
;
540 FF_ALLOCZ_OR_GOTO(h
->avctx
, er
->error_status_table
,
541 mb_array_size
* sizeof(uint8_t), fail
);
543 FF_ALLOC_OR_GOTO(h
->avctx
, er
->mbintra_table
, mb_array_size
, fail
);
544 memset(er
->mbintra_table
, 1, mb_array_size
);
546 FF_ALLOCZ_OR_GOTO(h
->avctx
, er
->mbskip_table
, mb_array_size
+ 2, fail
);
548 FF_ALLOC_OR_GOTO(h
->avctx
, er
->er_temp_buffer
,
549 h
->mb_height
* h
->mb_stride
, fail
);
551 FF_ALLOCZ_OR_GOTO(h
->avctx
, h
->dc_val_base
,
552 yc_size
* sizeof(int16_t), fail
);
553 er
->dc_val
[0] = h
->dc_val_base
+ h
->mb_width
* 2 + 2;
554 er
->dc_val
[1] = h
->dc_val_base
+ y_size
+ h
->mb_stride
+ 1;
555 er
->dc_val
[2] = er
->dc_val
[1] + c_size
;
556 for (i
= 0; i
< yc_size
; i
++)
557 h
->dc_val_base
[i
] = 1024;
563 return AVERROR(ENOMEM
); // ff_h264_free_tables will clean up for us
566 static int decode_nal_units(H264Context
*h
, const uint8_t *buf
, int buf_size
,
567 int parse_extradata
);
569 int ff_h264_decode_extradata(H264Context
*h
, const uint8_t *buf
, int size
)
571 AVCodecContext
*avctx
= h
->avctx
;
574 if (!buf
|| size
<= 0)
579 const unsigned char *p
= buf
;
584 av_log(avctx
, AV_LOG_ERROR
,
585 "avcC %d too short\n", size
);
586 return AVERROR_INVALIDDATA
;
588 /* sps and pps in the avcC always have length coded with 2 bytes,
589 * so put a fake nal_length_size = 2 while parsing them */
590 h
->nal_length_size
= 2;
591 // Decode sps from avcC
592 cnt
= *(p
+ 5) & 0x1f; // Number of sps
594 for (i
= 0; i
< cnt
; i
++) {
595 nalsize
= AV_RB16(p
) + 2;
596 if(nalsize
> size
- (p
-buf
))
597 return AVERROR_INVALIDDATA
;
598 ret
= decode_nal_units(h
, p
, nalsize
, 1);
600 av_log(avctx
, AV_LOG_ERROR
,
601 "Decoding sps %d from avcC failed\n", i
);
606 // Decode pps from avcC
607 cnt
= *(p
++); // Number of pps
608 for (i
= 0; i
< cnt
; i
++) {
609 nalsize
= AV_RB16(p
) + 2;
610 if(nalsize
> size
- (p
-buf
))
611 return AVERROR_INVALIDDATA
;
612 ret
= decode_nal_units(h
, p
, nalsize
, 1);
614 av_log(avctx
, AV_LOG_ERROR
,
615 "Decoding pps %d from avcC failed\n", i
);
620 // Store right nal length size that will be used to parse all other nals
621 h
->nal_length_size
= (buf
[4] & 0x03) + 1;
624 ret
= decode_nal_units(h
, buf
, size
, 1);
631 av_cold
int ff_h264_decode_init(AVCodecContext
*avctx
)
633 H264Context
*h
= avctx
->priv_data
;
639 h
->bit_depth_luma
= 8;
640 h
->chroma_format_idc
= 1;
642 h
->avctx
->bits_per_raw_sample
= 8;
643 h
->cur_chroma_format_idc
= 1;
645 ff_h264dsp_init(&h
->h264dsp
, 8, 1);
646 av_assert0(h
->sps
.bit_depth_chroma
== 0);
647 ff_h264chroma_init(&h
->h264chroma
, h
->sps
.bit_depth_chroma
);
648 ff_h264qpel_init(&h
->h264qpel
, 8);
649 ff_h264_pred_init(&h
->hpc
, h
->avctx
->codec_id
, 8, 1);
651 h
->dequant_coeff_pps
= -1;
652 h
->current_sps_id
= -1;
654 /* needed so that IDCT permutation is known early */
655 if (CONFIG_ERROR_RESILIENCE
)
656 ff_me_cmp_init(&h
->mecc
, h
->avctx
);
657 ff_videodsp_init(&h
->vdsp
, 8);
659 memset(h
->pps
.scaling_matrix4
, 16, 6 * 16 * sizeof(uint8_t));
660 memset(h
->pps
.scaling_matrix8
, 16, 2 * 64 * sizeof(uint8_t));
662 h
->picture_structure
= PICT_FRAME
;
663 h
->slice_context_count
= 1;
664 h
->workaround_bugs
= avctx
->workaround_bugs
;
665 h
->flags
= avctx
->flags
;
668 // s->decode_mb = ff_h263_decode_mb;
669 if (!avctx
->has_b_frames
)
672 avctx
->chroma_sample_location
= AVCHROMA_LOC_LEFT
;
674 ff_h264_decode_init_vlc();
676 ff_init_cabac_states();
679 h
->sps
.bit_depth_luma
= avctx
->bits_per_raw_sample
= 8;
681 h
->thread_context
[0] = h
;
682 h
->outputed_poc
= h
->next_outputed_poc
= INT_MIN
;
683 for (i
= 0; i
< MAX_DELAYED_PIC_COUNT
; i
++)
684 h
->last_pocs
[i
] = INT_MIN
;
685 h
->prev_poc_msb
= 1 << 16;
686 h
->prev_frame_num
= -1;
688 h
->sei_fpa
.frame_packing_arrangement_cancel_flag
= -1;
689 ff_h264_reset_sei(h
);
690 if (avctx
->codec_id
== AV_CODEC_ID_H264
) {
691 if (avctx
->ticks_per_frame
== 1) {
692 if(h
->avctx
->time_base
.den
< INT_MAX
/2) {
693 h
->avctx
->time_base
.den
*= 2;
695 h
->avctx
->time_base
.num
/= 2;
697 avctx
->ticks_per_frame
= 2;
700 if (avctx
->extradata_size
> 0 && avctx
->extradata
) {
701 ret
= ff_h264_decode_extradata(h
, avctx
->extradata
, avctx
->extradata_size
);
703 ff_h264_free_context(h
);
708 if (h
->sps
.bitstream_restriction_flag
&&
709 h
->avctx
->has_b_frames
< h
->sps
.num_reorder_frames
) {
710 h
->avctx
->has_b_frames
= h
->sps
.num_reorder_frames
;
714 avctx
->internal
->allocate_progress
= 1;
716 ff_h264_flush_change(h
);
721 static int decode_init_thread_copy(AVCodecContext
*avctx
)
723 H264Context
*h
= avctx
->priv_data
;
725 if (!avctx
->internal
->is_copy
)
727 memset(h
->sps_buffers
, 0, sizeof(h
->sps_buffers
));
728 memset(h
->pps_buffers
, 0, sizeof(h
->pps_buffers
));
730 h
->rbsp_buffer
[0] = NULL
;
731 h
->rbsp_buffer
[1] = NULL
;
732 h
->rbsp_buffer_size
[0] = 0;
733 h
->rbsp_buffer_size
[1] = 0;
734 h
->context_initialized
= 0;
740 * Run setup operations that must be run after slice header decoding.
741 * This includes finding the next displayed frame.
743 * @param h h264 master context
744 * @param setup_finished enough NALs have been read that we can call
745 * ff_thread_finish_setup()
747 static void decode_postinit(H264Context
*h
, int setup_finished
)
749 H264Picture
*out
= h
->cur_pic_ptr
;
750 H264Picture
*cur
= h
->cur_pic_ptr
;
751 int i
, pics
, out_of_order
, out_idx
;
753 h
->cur_pic_ptr
->f
.pict_type
= h
->pict_type
;
755 if (h
->next_output_pic
)
758 if (cur
->field_poc
[0] == INT_MAX
|| cur
->field_poc
[1] == INT_MAX
) {
759 /* FIXME: if we have two PAFF fields in one packet, we can't start
760 * the next thread here. If we have one field per packet, we can.
761 * The check in decode_nal_units() is not good enough to find this
762 * yet, so we assume the worst for now. */
763 // if (setup_finished)
764 // ff_thread_finish_setup(h->avctx);
768 cur
->f
.interlaced_frame
= 0;
769 cur
->f
.repeat_pict
= 0;
771 /* Signal interlacing information externally. */
772 /* Prioritize picture timing SEI information over used
773 * decoding process if it exists. */
775 if (h
->sps
.pic_struct_present_flag
) {
776 switch (h
->sei_pic_struct
) {
777 case SEI_PIC_STRUCT_FRAME
:
779 case SEI_PIC_STRUCT_TOP_FIELD
:
780 case SEI_PIC_STRUCT_BOTTOM_FIELD
:
781 cur
->f
.interlaced_frame
= 1;
783 case SEI_PIC_STRUCT_TOP_BOTTOM
:
784 case SEI_PIC_STRUCT_BOTTOM_TOP
:
785 if (FIELD_OR_MBAFF_PICTURE(h
))
786 cur
->f
.interlaced_frame
= 1;
788 // try to flag soft telecine progressive
789 cur
->f
.interlaced_frame
= h
->prev_interlaced_frame
;
791 case SEI_PIC_STRUCT_TOP_BOTTOM_TOP
:
792 case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM
:
793 /* Signal the possibility of telecined film externally
794 * (pic_struct 5,6). From these hints, let the applications
795 * decide if they apply deinterlacing. */
796 cur
->f
.repeat_pict
= 1;
798 case SEI_PIC_STRUCT_FRAME_DOUBLING
:
799 cur
->f
.repeat_pict
= 2;
801 case SEI_PIC_STRUCT_FRAME_TRIPLING
:
802 cur
->f
.repeat_pict
= 4;
806 if ((h
->sei_ct_type
& 3) &&
807 h
->sei_pic_struct
<= SEI_PIC_STRUCT_BOTTOM_TOP
)
808 cur
->f
.interlaced_frame
= (h
->sei_ct_type
& (1 << 1)) != 0;
810 /* Derive interlacing flag from used decoding process. */
811 cur
->f
.interlaced_frame
= FIELD_OR_MBAFF_PICTURE(h
);
813 h
->prev_interlaced_frame
= cur
->f
.interlaced_frame
;
815 if (cur
->field_poc
[0] != cur
->field_poc
[1]) {
816 /* Derive top_field_first from field pocs. */
817 cur
->f
.top_field_first
= cur
->field_poc
[0] < cur
->field_poc
[1];
819 if (cur
->f
.interlaced_frame
|| h
->sps
.pic_struct_present_flag
) {
820 /* Use picture timing SEI information. Even if it is a
821 * information of a past frame, better than nothing. */
822 if (h
->sei_pic_struct
== SEI_PIC_STRUCT_TOP_BOTTOM
||
823 h
->sei_pic_struct
== SEI_PIC_STRUCT_TOP_BOTTOM_TOP
)
824 cur
->f
.top_field_first
= 1;
826 cur
->f
.top_field_first
= 0;
828 /* Most likely progressive */
829 cur
->f
.top_field_first
= 0;
833 if (h
->sei_frame_packing_present
&&
834 h
->frame_packing_arrangement_type
>= 0 &&
835 h
->frame_packing_arrangement_type
<= 6 &&
836 h
->content_interpretation_type
> 0 &&
837 h
->content_interpretation_type
< 3) {
838 AVStereo3D
*stereo
= av_stereo3d_create_side_data(&cur
->f
);
840 switch (h
->frame_packing_arrangement_type
) {
842 stereo
->type
= AV_STEREO3D_CHECKERBOARD
;
845 stereo
->type
= AV_STEREO3D_COLUMNS
;
848 stereo
->type
= AV_STEREO3D_LINES
;
851 if (h
->quincunx_subsampling
)
852 stereo
->type
= AV_STEREO3D_SIDEBYSIDE_QUINCUNX
;
854 stereo
->type
= AV_STEREO3D_SIDEBYSIDE
;
857 stereo
->type
= AV_STEREO3D_TOPBOTTOM
;
860 stereo
->type
= AV_STEREO3D_FRAMESEQUENCE
;
863 stereo
->type
= AV_STEREO3D_2D
;
867 if (h
->content_interpretation_type
== 2)
868 stereo
->flags
= AV_STEREO3D_FLAG_INVERT
;
872 if (h
->sei_display_orientation_present
&&
873 (h
->sei_anticlockwise_rotation
|| h
->sei_hflip
|| h
->sei_vflip
)) {
874 double angle
= h
->sei_anticlockwise_rotation
* 360 / (double) (1 << 16);
875 AVFrameSideData
*rotation
= av_frame_new_side_data(&cur
->f
,
876 AV_FRAME_DATA_DISPLAYMATRIX
,
877 sizeof(int32_t) * 9);
879 av_display_rotation_set((int32_t *)rotation
->data
, angle
);
880 av_display_matrix_flip((int32_t *)rotation
->data
,
881 h
->sei_hflip
, h
->sei_vflip
);
885 cur
->mmco_reset
= h
->mmco_reset
;
888 // FIXME do something with unavailable reference frames
890 /* Sort B-frames into display order */
892 if (h
->sps
.bitstream_restriction_flag
&&
893 h
->avctx
->has_b_frames
< h
->sps
.num_reorder_frames
) {
894 h
->avctx
->has_b_frames
= h
->sps
.num_reorder_frames
;
898 if (h
->avctx
->strict_std_compliance
>= FF_COMPLIANCE_STRICT
&&
899 !h
->sps
.bitstream_restriction_flag
) {
900 h
->avctx
->has_b_frames
= MAX_DELAYED_PIC_COUNT
- 1;
904 for (i
= 0; 1; i
++) {
905 if(i
== MAX_DELAYED_PIC_COUNT
|| cur
->poc
< h
->last_pocs
[i
]){
907 h
->last_pocs
[i
-1] = cur
->poc
;
910 h
->last_pocs
[i
-1]= h
->last_pocs
[i
];
913 out_of_order
= MAX_DELAYED_PIC_COUNT
- i
;
914 if( cur
->f
.pict_type
== AV_PICTURE_TYPE_B
915 || (h
->last_pocs
[MAX_DELAYED_PIC_COUNT
-2] > INT_MIN
&& h
->last_pocs
[MAX_DELAYED_PIC_COUNT
-1] - h
->last_pocs
[MAX_DELAYED_PIC_COUNT
-2] > 2))
916 out_of_order
= FFMAX(out_of_order
, 1);
917 if (out_of_order
== MAX_DELAYED_PIC_COUNT
) {
918 av_log(h
->avctx
, AV_LOG_VERBOSE
, "Invalid POC %d<%d\n", cur
->poc
, h
->last_pocs
[0]);
919 for (i
= 1; i
< MAX_DELAYED_PIC_COUNT
; i
++)
920 h
->last_pocs
[i
] = INT_MIN
;
921 h
->last_pocs
[0] = cur
->poc
;
923 } else if(h
->avctx
->has_b_frames
< out_of_order
&& !h
->sps
.bitstream_restriction_flag
){
924 av_log(h
->avctx
, AV_LOG_VERBOSE
, "Increasing reorder buffer to %d\n", out_of_order
);
925 h
->avctx
->has_b_frames
= out_of_order
;
930 while (h
->delayed_pic
[pics
])
933 av_assert0(pics
<= MAX_DELAYED_PIC_COUNT
);
935 h
->delayed_pic
[pics
++] = cur
;
936 if (cur
->reference
== 0)
937 cur
->reference
= DELAYED_PIC_REF
;
939 out
= h
->delayed_pic
[0];
941 for (i
= 1; h
->delayed_pic
[i
] &&
942 !h
->delayed_pic
[i
]->f
.key_frame
&&
943 !h
->delayed_pic
[i
]->mmco_reset
;
945 if (h
->delayed_pic
[i
]->poc
< out
->poc
) {
946 out
= h
->delayed_pic
[i
];
949 if (h
->avctx
->has_b_frames
== 0 &&
950 (h
->delayed_pic
[0]->f
.key_frame
|| h
->delayed_pic
[0]->mmco_reset
))
951 h
->next_outputed_poc
= INT_MIN
;
952 out_of_order
= out
->poc
< h
->next_outputed_poc
;
954 if (out_of_order
|| pics
> h
->avctx
->has_b_frames
) {
955 out
->reference
&= ~DELAYED_PIC_REF
;
956 // for frame threading, the owner must be the second field's thread or
957 // else the first thread can release the picture and reuse it unsafely
958 for (i
= out_idx
; h
->delayed_pic
[i
]; i
++)
959 h
->delayed_pic
[i
] = h
->delayed_pic
[i
+ 1];
961 if (!out_of_order
&& pics
> h
->avctx
->has_b_frames
) {
962 h
->next_output_pic
= out
;
963 if (out_idx
== 0 && h
->delayed_pic
[0] && (h
->delayed_pic
[0]->f
.key_frame
|| h
->delayed_pic
[0]->mmco_reset
)) {
964 h
->next_outputed_poc
= INT_MIN
;
966 h
->next_outputed_poc
= out
->poc
;
968 av_log(h
->avctx
, AV_LOG_DEBUG
, "no picture %s\n", out_of_order
? "ooo" : "");
971 if (h
->next_output_pic
) {
972 if (h
->next_output_pic
->recovered
) {
973 // We have reached an recovery point and all frames after it in
974 // display order are "recovered".
975 h
->frame_recovered
|= FRAME_RECOVERED_SEI
;
977 h
->next_output_pic
->recovered
|= !!(h
->frame_recovered
& FRAME_RECOVERED_SEI
);
980 if (setup_finished
&& !h
->avctx
->hwaccel
)
981 ff_thread_finish_setup(h
->avctx
);
984 int ff_pred_weight_table(H264Context
*h
)
987 int luma_def
, chroma_def
;
990 h
->use_weight_chroma
= 0;
991 h
->luma_log2_weight_denom
= get_ue_golomb(&h
->gb
);
992 if (h
->sps
.chroma_format_idc
)
993 h
->chroma_log2_weight_denom
= get_ue_golomb(&h
->gb
);
995 if (h
->luma_log2_weight_denom
> 7U) {
996 av_log(h
->avctx
, AV_LOG_ERROR
, "luma_log2_weight_denom %d is out of range\n", h
->luma_log2_weight_denom
);
997 h
->luma_log2_weight_denom
= 0;
999 if (h
->chroma_log2_weight_denom
> 7U) {
1000 av_log(h
->avctx
, AV_LOG_ERROR
, "chroma_log2_weight_denom %d is out of range\n", h
->chroma_log2_weight_denom
);
1001 h
->chroma_log2_weight_denom
= 0;
1004 luma_def
= 1 << h
->luma_log2_weight_denom
;
1005 chroma_def
= 1 << h
->chroma_log2_weight_denom
;
1007 for (list
= 0; list
< 2; list
++) {
1008 h
->luma_weight_flag
[list
] = 0;
1009 h
->chroma_weight_flag
[list
] = 0;
1010 for (i
= 0; i
< h
->ref_count
[list
]; i
++) {
1011 int luma_weight_flag
, chroma_weight_flag
;
1013 luma_weight_flag
= get_bits1(&h
->gb
);
1014 if (luma_weight_flag
) {
1015 h
->luma_weight
[i
][list
][0] = get_se_golomb(&h
->gb
);
1016 h
->luma_weight
[i
][list
][1] = get_se_golomb(&h
->gb
);
1017 if (h
->luma_weight
[i
][list
][0] != luma_def
||
1018 h
->luma_weight
[i
][list
][1] != 0) {
1020 h
->luma_weight_flag
[list
] = 1;
1023 h
->luma_weight
[i
][list
][0] = luma_def
;
1024 h
->luma_weight
[i
][list
][1] = 0;
1027 if (h
->sps
.chroma_format_idc
) {
1028 chroma_weight_flag
= get_bits1(&h
->gb
);
1029 if (chroma_weight_flag
) {
1031 for (j
= 0; j
< 2; j
++) {
1032 h
->chroma_weight
[i
][list
][j
][0] = get_se_golomb(&h
->gb
);
1033 h
->chroma_weight
[i
][list
][j
][1] = get_se_golomb(&h
->gb
);
1034 if (h
->chroma_weight
[i
][list
][j
][0] != chroma_def
||
1035 h
->chroma_weight
[i
][list
][j
][1] != 0) {
1036 h
->use_weight_chroma
= 1;
1037 h
->chroma_weight_flag
[list
] = 1;
1042 for (j
= 0; j
< 2; j
++) {
1043 h
->chroma_weight
[i
][list
][j
][0] = chroma_def
;
1044 h
->chroma_weight
[i
][list
][j
][1] = 0;
1049 if (h
->slice_type_nos
!= AV_PICTURE_TYPE_B
)
1052 h
->use_weight
= h
->use_weight
|| h
->use_weight_chroma
;
1057 * instantaneous decoder refresh.
1059 static void idr(H264Context
*h
)
1062 ff_h264_remove_all_refs(h
);
1064 h
->prev_frame_num_offset
= 0;
1065 h
->prev_poc_msb
= 1<<16;
1066 h
->prev_poc_lsb
= 0;
1067 for (i
= 0; i
< MAX_DELAYED_PIC_COUNT
; i
++)
1068 h
->last_pocs
[i
] = INT_MIN
;
1071 /* forget old pics after a seek */
1072 void ff_h264_flush_change(H264Context
*h
)
1076 h
->outputed_poc
= h
->next_outputed_poc
= INT_MIN
;
1077 h
->prev_interlaced_frame
= 1;
1080 h
->prev_frame_num
= -1;
1081 if (h
->cur_pic_ptr
) {
1082 h
->cur_pic_ptr
->reference
= 0;
1083 for (j
=i
=0; h
->delayed_pic
[i
]; i
++)
1084 if (h
->delayed_pic
[i
] != h
->cur_pic_ptr
)
1085 h
->delayed_pic
[j
++] = h
->delayed_pic
[i
];
1086 h
->delayed_pic
[j
] = NULL
;
1089 memset(h
->ref_list
[0], 0, sizeof(h
->ref_list
[0]));
1090 memset(h
->ref_list
[1], 0, sizeof(h
->ref_list
[1]));
1091 memset(h
->default_ref_list
[0], 0, sizeof(h
->default_ref_list
[0]));
1092 memset(h
->default_ref_list
[1], 0, sizeof(h
->default_ref_list
[1]));
1093 ff_h264_reset_sei(h
);
1094 h
->recovery_frame
= -1;
1095 h
->frame_recovered
= 0;
1097 h
->current_slice
= 0;
1101 /* forget old pics after a seek */
1102 static void flush_dpb(AVCodecContext
*avctx
)
1104 H264Context
*h
= avctx
->priv_data
;
1107 for (i
= 0; i
<= MAX_DELAYED_PIC_COUNT
; i
++) {
1108 if (h
->delayed_pic
[i
])
1109 h
->delayed_pic
[i
]->reference
= 0;
1110 h
->delayed_pic
[i
] = NULL
;
1113 ff_h264_flush_change(h
);
1116 for (i
= 0; i
< H264_MAX_PICTURE_COUNT
; i
++)
1117 ff_h264_unref_picture(h
, &h
->DPB
[i
]);
1118 h
->cur_pic_ptr
= NULL
;
1119 ff_h264_unref_picture(h
, &h
->cur_pic
);
1121 h
->mb_x
= h
->mb_y
= 0;
1123 h
->parse_context
.state
= -1;
1124 h
->parse_context
.frame_start_found
= 0;
1125 h
->parse_context
.overread
= 0;
1126 h
->parse_context
.overread_index
= 0;
1127 h
->parse_context
.index
= 0;
1128 h
->parse_context
.last_index
= 0;
1130 ff_h264_free_tables(h
, 1);
1131 h
->context_initialized
= 0;
1134 int ff_init_poc(H264Context
*h
, int pic_field_poc
[2], int *pic_poc
)
1136 const int max_frame_num
= 1 << h
->sps
.log2_max_frame_num
;
1139 h
->frame_num_offset
= h
->prev_frame_num_offset
;
1140 if (h
->frame_num
< h
->prev_frame_num
)
1141 h
->frame_num_offset
+= max_frame_num
;
1143 if (h
->sps
.poc_type
== 0) {
1144 const int max_poc_lsb
= 1 << h
->sps
.log2_max_poc_lsb
;
1146 if (h
->poc_lsb
< h
->prev_poc_lsb
&&
1147 h
->prev_poc_lsb
- h
->poc_lsb
>= max_poc_lsb
/ 2)
1148 h
->poc_msb
= h
->prev_poc_msb
+ max_poc_lsb
;
1149 else if (h
->poc_lsb
> h
->prev_poc_lsb
&&
1150 h
->prev_poc_lsb
- h
->poc_lsb
< -max_poc_lsb
/ 2)
1151 h
->poc_msb
= h
->prev_poc_msb
- max_poc_lsb
;
1153 h
->poc_msb
= h
->prev_poc_msb
;
1155 field_poc
[1] = h
->poc_msb
+ h
->poc_lsb
;
1156 if (h
->picture_structure
== PICT_FRAME
)
1157 field_poc
[1] += h
->delta_poc_bottom
;
1158 } else if (h
->sps
.poc_type
== 1) {
1159 int abs_frame_num
, expected_delta_per_poc_cycle
, expectedpoc
;
1162 if (h
->sps
.poc_cycle_length
!= 0)
1163 abs_frame_num
= h
->frame_num_offset
+ h
->frame_num
;
1167 if (h
->nal_ref_idc
== 0 && abs_frame_num
> 0)
1170 expected_delta_per_poc_cycle
= 0;
1171 for (i
= 0; i
< h
->sps
.poc_cycle_length
; i
++)
1172 // FIXME integrate during sps parse
1173 expected_delta_per_poc_cycle
+= h
->sps
.offset_for_ref_frame
[i
];
1175 if (abs_frame_num
> 0) {
1176 int poc_cycle_cnt
= (abs_frame_num
- 1) / h
->sps
.poc_cycle_length
;
1177 int frame_num_in_poc_cycle
= (abs_frame_num
- 1) % h
->sps
.poc_cycle_length
;
1179 expectedpoc
= poc_cycle_cnt
* expected_delta_per_poc_cycle
;
1180 for (i
= 0; i
<= frame_num_in_poc_cycle
; i
++)
1181 expectedpoc
= expectedpoc
+ h
->sps
.offset_for_ref_frame
[i
];
1185 if (h
->nal_ref_idc
== 0)
1186 expectedpoc
= expectedpoc
+ h
->sps
.offset_for_non_ref_pic
;
1188 field_poc
[0] = expectedpoc
+ h
->delta_poc
[0];
1189 field_poc
[1] = field_poc
[0] + h
->sps
.offset_for_top_to_bottom_field
;
1191 if (h
->picture_structure
== PICT_FRAME
)
1192 field_poc
[1] += h
->delta_poc
[1];
1194 int poc
= 2 * (h
->frame_num_offset
+ h
->frame_num
);
1196 if (!h
->nal_ref_idc
)
1203 if (h
->picture_structure
!= PICT_BOTTOM_FIELD
)
1204 pic_field_poc
[0] = field_poc
[0];
1205 if (h
->picture_structure
!= PICT_TOP_FIELD
)
1206 pic_field_poc
[1] = field_poc
[1];
1207 *pic_poc
= FFMIN(pic_field_poc
[0], pic_field_poc
[1]);
1213 * Compute profile from profile_idc and constraint_set?_flags.
1217 * @return profile as defined by FF_PROFILE_H264_*
1219 int ff_h264_get_profile(SPS
*sps
)
1221 int profile
= sps
->profile_idc
;
1223 switch (sps
->profile_idc
) {
1224 case FF_PROFILE_H264_BASELINE
:
1225 // constraint_set1_flag set to 1
1226 profile
|= (sps
->constraint_set_flags
& 1 << 1) ? FF_PROFILE_H264_CONSTRAINED
: 0;
1228 case FF_PROFILE_H264_HIGH_10
:
1229 case FF_PROFILE_H264_HIGH_422
:
1230 case FF_PROFILE_H264_HIGH_444_PREDICTIVE
:
1231 // constraint_set3_flag set to 1
1232 profile
|= (sps
->constraint_set_flags
& 1 << 3) ? FF_PROFILE_H264_INTRA
: 0;
1239 int ff_h264_set_parameter_from_sps(H264Context
*h
)
1241 if (h
->flags
& CODEC_FLAG_LOW_DELAY
||
1242 (h
->sps
.bitstream_restriction_flag
&&
1243 !h
->sps
.num_reorder_frames
)) {
1244 if (h
->avctx
->has_b_frames
> 1 || h
->delayed_pic
[0])
1245 av_log(h
->avctx
, AV_LOG_WARNING
, "Delayed frames seen. "
1246 "Reenabling low delay requires a codec flush.\n");
1251 if (h
->avctx
->has_b_frames
< 2)
1252 h
->avctx
->has_b_frames
= !h
->low_delay
;
1254 if (h
->avctx
->bits_per_raw_sample
!= h
->sps
.bit_depth_luma
||
1255 h
->cur_chroma_format_idc
!= h
->sps
.chroma_format_idc
) {
1256 if (h
->avctx
->codec
&&
1257 h
->avctx
->codec
->capabilities
& CODEC_CAP_HWACCEL_VDPAU
&&
1258 (h
->sps
.bit_depth_luma
!= 8 || h
->sps
.chroma_format_idc
> 1)) {
1259 av_log(h
->avctx
, AV_LOG_ERROR
,
1260 "VDPAU decoding does not support video colorspace.\n");
1261 return AVERROR_INVALIDDATA
;
1263 if (h
->sps
.bit_depth_luma
>= 8 && h
->sps
.bit_depth_luma
<= 14 &&
1264 h
->sps
.bit_depth_luma
!= 11 && h
->sps
.bit_depth_luma
!= 13) {
1265 h
->avctx
->bits_per_raw_sample
= h
->sps
.bit_depth_luma
;
1266 h
->cur_chroma_format_idc
= h
->sps
.chroma_format_idc
;
1267 h
->pixel_shift
= h
->sps
.bit_depth_luma
> 8;
1269 ff_h264dsp_init(&h
->h264dsp
, h
->sps
.bit_depth_luma
,
1270 h
->sps
.chroma_format_idc
);
1271 ff_h264chroma_init(&h
->h264chroma
, h
->sps
.bit_depth_chroma
);
1272 ff_h264qpel_init(&h
->h264qpel
, h
->sps
.bit_depth_luma
);
1273 ff_h264_pred_init(&h
->hpc
, h
->avctx
->codec_id
, h
->sps
.bit_depth_luma
,
1274 h
->sps
.chroma_format_idc
);
1276 if (CONFIG_ERROR_RESILIENCE
)
1277 ff_me_cmp_init(&h
->mecc
, h
->avctx
);
1278 ff_videodsp_init(&h
->vdsp
, h
->sps
.bit_depth_luma
);
1280 av_log(h
->avctx
, AV_LOG_ERROR
, "Unsupported bit depth %d\n",
1281 h
->sps
.bit_depth_luma
);
1282 return AVERROR_INVALIDDATA
;
1288 int ff_set_ref_count(H264Context
*h
)
1290 int ref_count
[2], list_count
;
1291 int num_ref_idx_active_override_flag
;
1293 // set defaults, might be overridden a few lines later
1294 ref_count
[0] = h
->pps
.ref_count
[0];
1295 ref_count
[1] = h
->pps
.ref_count
[1];
1297 if (h
->slice_type_nos
!= AV_PICTURE_TYPE_I
) {
1299 max
[0] = max
[1] = h
->picture_structure
== PICT_FRAME
? 15 : 31;
1301 if (h
->slice_type_nos
== AV_PICTURE_TYPE_B
)
1302 h
->direct_spatial_mv_pred
= get_bits1(&h
->gb
);
1303 num_ref_idx_active_override_flag
= get_bits1(&h
->gb
);
1305 if (num_ref_idx_active_override_flag
) {
1306 ref_count
[0] = get_ue_golomb(&h
->gb
) + 1;
1307 if (h
->slice_type_nos
== AV_PICTURE_TYPE_B
) {
1308 ref_count
[1] = get_ue_golomb(&h
->gb
) + 1;
1310 // full range is spec-ok in this case, even for frames
1314 if (ref_count
[0]-1 > max
[0] || ref_count
[1]-1 > max
[1]){
1315 av_log(h
->avctx
, AV_LOG_ERROR
, "reference overflow %u > %u or %u > %u\n", ref_count
[0]-1, max
[0], ref_count
[1]-1, max
[1]);
1316 h
->ref_count
[0] = h
->ref_count
[1] = 0;
1318 return AVERROR_INVALIDDATA
;
1321 if (h
->slice_type_nos
== AV_PICTURE_TYPE_B
)
1327 ref_count
[0] = ref_count
[1] = 0;
1330 if (list_count
!= h
->list_count
||
1331 ref_count
[0] != h
->ref_count
[0] ||
1332 ref_count
[1] != h
->ref_count
[1]) {
1333 h
->ref_count
[0] = ref_count
[0];
1334 h
->ref_count
[1] = ref_count
[1];
1335 h
->list_count
= list_count
;
1342 static const uint8_t start_code
[] = { 0x00, 0x00, 0x01 };
1344 static int get_bit_length(H264Context
*h
, const uint8_t *buf
,
1345 const uint8_t *ptr
, int dst_length
,
1346 int i
, int next_avc
)
1348 if ((h
->workaround_bugs
& FF_BUG_AUTODETECT
) && i
+ 3 < next_avc
&&
1349 buf
[i
] == 0x00 && buf
[i
+ 1] == 0x00 &&
1350 buf
[i
+ 2] == 0x01 && buf
[i
+ 3] == 0xE0)
1351 h
->workaround_bugs
|= FF_BUG_TRUNCATED
;
1353 if (!(h
->workaround_bugs
& FF_BUG_TRUNCATED
))
1354 while (dst_length
> 0 && ptr
[dst_length
- 1] == 0)
1360 return 8 * dst_length
- decode_rbsp_trailing(h
, ptr
+ dst_length
- 1);
1363 static int get_last_needed_nal(H264Context
*h
, const uint8_t *buf
, int buf_size
)
1365 int next_avc
= h
->is_avc
? 0 : buf_size
;
1368 int nals_needed
= 0;
1369 int first_slice
= 0;
1373 int dst_length
, bit_length
, consumed
;
1376 if (buf_index
>= next_avc
) {
1377 nalsize
= get_avc_nalsize(h
, buf
, buf_size
, &buf_index
);
1380 next_avc
= buf_index
+ nalsize
;
1382 buf_index
= find_start_code(buf
, buf_size
, buf_index
, next_avc
);
1383 if (buf_index
>= buf_size
)
1385 if (buf_index
>= next_avc
)
1389 ptr
= ff_h264_decode_nal(h
, buf
+ buf_index
, &dst_length
, &consumed
,
1390 next_avc
- buf_index
);
1392 if (!ptr
|| dst_length
< 0)
1393 return AVERROR_INVALIDDATA
;
1395 buf_index
+= consumed
;
1397 bit_length
= get_bit_length(h
, buf
, ptr
, dst_length
,
1398 buf_index
, next_avc
);
1401 /* packets can sometimes contain multiple PPS/SPS,
1402 * e.g. two PAFF field pictures in one packet, or a demuxer
1403 * which splits NALs strangely if so, when frame threading we
1404 * can't start the next thread until we've read all of them */
1405 switch (h
->nal_unit_type
) {
1408 nals_needed
= nal_index
;
1413 init_get_bits(&h
->gb
, ptr
, bit_length
);
1414 if (!get_ue_golomb(&h
->gb
) ||
1416 first_slice
!= h
->nal_unit_type
)
1417 nals_needed
= nal_index
;
1419 first_slice
= h
->nal_unit_type
;
1426 static int decode_nal_units(H264Context
*h
, const uint8_t *buf
, int buf_size
,
1427 int parse_extradata
)
1429 AVCodecContext
*const avctx
= h
->avctx
;
1430 H264Context
*hx
; ///< thread context
1432 unsigned context_count
;
1434 int nals_needed
= 0; ///< number of NALs that need decoding before the next frame thread starts
1439 h
->nal_unit_type
= 0;
1441 if(!h
->slice_context_count
)
1442 h
->slice_context_count
= 1;
1443 h
->max_contexts
= h
->slice_context_count
;
1444 if (!(avctx
->flags2
& CODEC_FLAG2_CHUNKS
)) {
1445 h
->current_slice
= 0;
1446 if (!h
->first_field
)
1447 h
->cur_pic_ptr
= NULL
;
1448 ff_h264_reset_sei(h
);
1451 if (h
->nal_length_size
== 4) {
1452 if (buf_size
> 8 && AV_RB32(buf
) == 1 && AV_RB32(buf
+5) > (unsigned)buf_size
) {
1454 }else if(buf_size
> 3 && AV_RB32(buf
) > 1 && AV_RB32(buf
) <= (unsigned)buf_size
)
1458 if (avctx
->active_thread_type
& FF_THREAD_FRAME
)
1459 nals_needed
= get_last_needed_nal(h
, buf
, buf_size
);
1464 next_avc
= h
->is_avc
? 0 : buf_size
;
1474 if (buf_index
>= next_avc
) {
1475 nalsize
= get_avc_nalsize(h
, buf
, buf_size
, &buf_index
);
1478 next_avc
= buf_index
+ nalsize
;
1480 buf_index
= find_start_code(buf
, buf_size
, buf_index
, next_avc
);
1481 if (buf_index
>= buf_size
)
1483 if (buf_index
>= next_avc
)
1487 hx
= h
->thread_context
[context_count
];
1489 ptr
= ff_h264_decode_nal(hx
, buf
+ buf_index
, &dst_length
,
1490 &consumed
, next_avc
- buf_index
);
1491 if (!ptr
|| dst_length
< 0) {
1496 bit_length
= get_bit_length(h
, buf
, ptr
, dst_length
,
1497 buf_index
+ consumed
, next_avc
);
1499 if (h
->avctx
->debug
& FF_DEBUG_STARTCODE
)
1500 av_log(h
->avctx
, AV_LOG_DEBUG
,
1501 "NAL %d/%d at %d/%d length %d\n",
1502 hx
->nal_unit_type
, hx
->nal_ref_idc
, buf_index
, buf_size
, dst_length
);
1504 if (h
->is_avc
&& (nalsize
!= consumed
) && nalsize
)
1505 av_log(h
->avctx
, AV_LOG_DEBUG
,
1506 "AVC: Consumed only %d bytes instead of %d\n",
1509 buf_index
+= consumed
;
1512 if (avctx
->skip_frame
>= AVDISCARD_NONREF
&&
1513 h
->nal_ref_idc
== 0 &&
1514 h
->nal_unit_type
!= NAL_SEI
)
1518 if ( !(avctx
->active_thread_type
& FF_THREAD_FRAME
)
1519 || nals_needed
>= nal_index
)
1521 /* Ignore per frame NAL unit type during extradata
1522 * parsing. Decoding slices is not possible in codec init
1524 if (parse_extradata
) {
1525 switch (hx
->nal_unit_type
) {
1531 av_log(h
->avctx
, AV_LOG_WARNING
,
1532 "Ignoring NAL %d in global header/extradata\n",
1534 // fall through to next case
1535 case NAL_AUXILIARY_SLICE
:
1536 hx
->nal_unit_type
= NAL_FF_IGNORE
;
1542 switch (hx
->nal_unit_type
) {
1544 if ((ptr
[0] & 0xFC) == 0x98) {
1545 av_log(h
->avctx
, AV_LOG_ERROR
, "Invalid inter IDR frame\n");
1546 h
->next_outputed_poc
= INT_MIN
;
1550 if (h
->nal_unit_type
!= NAL_IDR_SLICE
) {
1551 av_log(h
->avctx
, AV_LOG_ERROR
,
1552 "Invalid mix of idr and non-idr slices\n");
1557 idr(h
); // FIXME ensure we don't lose some frames if there is reordering
1559 h
->has_recovery_point
= 1;
1561 init_get_bits(&hx
->gb
, ptr
, bit_length
);
1563 hx
->inter_gb_ptr
= &hx
->gb
;
1564 hx
->data_partitioning
= 0;
1566 if ((err
= ff_h264_decode_slice_header(hx
, h
)))
1569 if (h
->sei_recovery_frame_cnt
>= 0) {
1570 if (h
->frame_num
!= h
->sei_recovery_frame_cnt
|| hx
->slice_type_nos
!= AV_PICTURE_TYPE_I
)
1571 h
->valid_recovery_point
= 1;
1573 if ( h
->recovery_frame
< 0
1574 || ((h
->recovery_frame
- h
->frame_num
) & ((1 << h
->sps
.log2_max_frame_num
)-1)) > h
->sei_recovery_frame_cnt
) {
1575 h
->recovery_frame
= (h
->frame_num
+ h
->sei_recovery_frame_cnt
) &
1576 ((1 << h
->sps
.log2_max_frame_num
) - 1);
1578 if (!h
->valid_recovery_point
)
1579 h
->recovery_frame
= h
->frame_num
;
1583 h
->cur_pic_ptr
->f
.key_frame
|=
1584 (hx
->nal_unit_type
== NAL_IDR_SLICE
);
1586 if (hx
->nal_unit_type
== NAL_IDR_SLICE
||
1587 h
->recovery_frame
== h
->frame_num
) {
1588 h
->recovery_frame
= -1;
1589 h
->cur_pic_ptr
->recovered
= 1;
1591 // If we have an IDR, all frames after it in decoded order are
1593 if (hx
->nal_unit_type
== NAL_IDR_SLICE
)
1594 h
->frame_recovered
|= FRAME_RECOVERED_IDR
;
1595 h
->frame_recovered
|= 3*!!(avctx
->flags2
& CODEC_FLAG2_SHOW_ALL
);
1596 h
->frame_recovered
|= 3*!!(avctx
->flags
& CODEC_FLAG_OUTPUT_CORRUPT
);
1598 h
->cur_pic_ptr
->recovered
|= h
->frame_recovered
;
1600 h
->cur_pic_ptr
->recovered
|= !!(h
->frame_recovered
& FRAME_RECOVERED_IDR
);
1603 if (h
->current_slice
== 1) {
1604 if (!(avctx
->flags2
& CODEC_FLAG2_CHUNKS
))
1605 decode_postinit(h
, nal_index
>= nals_needed
);
1607 if (h
->avctx
->hwaccel
&&
1608 (ret
= h
->avctx
->hwaccel
->start_frame(h
->avctx
, NULL
, 0)) < 0)
1610 if (CONFIG_H264_VDPAU_DECODER
&&
1611 h
->avctx
->codec
->capabilities
& CODEC_CAP_HWACCEL_VDPAU
)
1612 ff_vdpau_h264_picture_start(h
);
1615 if (hx
->redundant_pic_count
== 0) {
1616 if (avctx
->hwaccel
) {
1617 ret
= avctx
->hwaccel
->decode_slice(avctx
,
1618 &buf
[buf_index
- consumed
],
1622 } else if (CONFIG_H264_VDPAU_DECODER
&&
1623 h
->avctx
->codec
->capabilities
& CODEC_CAP_HWACCEL_VDPAU
) {
1624 ff_vdpau_add_data_chunk(h
->cur_pic_ptr
->f
.data
[0],
1626 sizeof(start_code
));
1627 ff_vdpau_add_data_chunk(h
->cur_pic_ptr
->f
.data
[0],
1628 &buf
[buf_index
- consumed
],
1635 if (h
->avctx
->flags
& CODEC_FLAG2_CHUNKS
) {
1636 av_log(h
->avctx
, AV_LOG_ERROR
,
1637 "Decoding in chunks is not supported for "
1638 "partitioned slices.\n");
1639 return AVERROR(ENOSYS
);
1642 init_get_bits(&hx
->gb
, ptr
, bit_length
);
1644 hx
->inter_gb_ptr
= NULL
;
1646 if ((err
= ff_h264_decode_slice_header(hx
, h
))) {
1647 /* make sure data_partitioning is cleared if it was set
1648 * before, so we don't try decoding a slice without a valid
1649 * slice header later */
1650 h
->data_partitioning
= 0;
1654 hx
->data_partitioning
= 1;
1657 init_get_bits(&hx
->intra_gb
, ptr
, bit_length
);
1658 hx
->intra_gb_ptr
= &hx
->intra_gb
;
1661 init_get_bits(&hx
->inter_gb
, ptr
, bit_length
);
1662 hx
->inter_gb_ptr
= &hx
->inter_gb
;
1664 av_log(h
->avctx
, AV_LOG_ERROR
, "Partitioned H.264 support is incomplete\n");
1667 if (hx
->redundant_pic_count
== 0 &&
1669 hx
->data_partitioning
&&
1670 h
->cur_pic_ptr
&& h
->context_initialized
&&
1671 (avctx
->skip_frame
< AVDISCARD_NONREF
|| hx
->nal_ref_idc
) &&
1672 (avctx
->skip_frame
< AVDISCARD_BIDIR
||
1673 hx
->slice_type_nos
!= AV_PICTURE_TYPE_B
) &&
1674 (avctx
->skip_frame
< AVDISCARD_NONINTRA
||
1675 hx
->slice_type_nos
== AV_PICTURE_TYPE_I
) &&
1676 avctx
->skip_frame
< AVDISCARD_ALL
)
1680 init_get_bits(&h
->gb
, ptr
, bit_length
);
1681 ret
= ff_h264_decode_sei(h
);
1682 if (ret
< 0 && (h
->avctx
->err_recognition
& AV_EF_EXPLODE
))
1686 init_get_bits(&h
->gb
, ptr
, bit_length
);
1687 if (ff_h264_decode_seq_parameter_set(h
) < 0 && (h
->is_avc
? nalsize
: 1)) {
1688 av_log(h
->avctx
, AV_LOG_DEBUG
,
1689 "SPS decoding failure, trying again with the complete NAL\n");
1691 av_assert0(next_avc
- buf_index
+ consumed
== nalsize
);
1692 if ((next_avc
- buf_index
+ consumed
- 1) >= INT_MAX
/8)
1694 init_get_bits(&h
->gb
, &buf
[buf_index
+ 1 - consumed
],
1695 8*(next_avc
- buf_index
+ consumed
- 1));
1696 ff_h264_decode_seq_parameter_set(h
);
1701 init_get_bits(&h
->gb
, ptr
, bit_length
);
1702 ret
= ff_h264_decode_picture_parameter_set(h
, bit_length
);
1703 if (ret
< 0 && (h
->avctx
->err_recognition
& AV_EF_EXPLODE
))
1707 case NAL_END_SEQUENCE
:
1708 case NAL_END_STREAM
:
1709 case NAL_FILLER_DATA
:
1711 case NAL_AUXILIARY_SLICE
:
1716 av_log(avctx
, AV_LOG_DEBUG
, "Unknown NAL code: %d (%d bits)\n",
1717 hx
->nal_unit_type
, bit_length
);
1720 if (context_count
== h
->max_contexts
) {
1721 ret
= ff_h264_execute_decode_slices(h
, context_count
);
1722 if (ret
< 0 && (h
->avctx
->err_recognition
& AV_EF_EXPLODE
))
1727 if (err
< 0 || err
== SLICE_SKIPED
) {
1729 av_log(h
->avctx
, AV_LOG_ERROR
, "decode_slice_header error\n");
1730 h
->ref_count
[0] = h
->ref_count
[1] = h
->list_count
= 0;
1731 } else if (err
== SLICE_SINGLETHREAD
) {
1732 /* Slice could not be decoded in parallel mode, copy down
1733 * NAL unit stuff to context 0 and restart. Note that
1734 * rbsp_buffer is not transferred, but since we no longer
1735 * run in parallel mode this should not be an issue. */
1736 h
->nal_unit_type
= hx
->nal_unit_type
;
1737 h
->nal_ref_idc
= hx
->nal_ref_idc
;
1743 if (context_count
) {
1744 ret
= ff_h264_execute_decode_slices(h
, context_count
);
1745 if (ret
< 0 && (h
->avctx
->err_recognition
& AV_EF_EXPLODE
))
1752 if (h
->cur_pic_ptr
&& !h
->droppable
) {
1753 ff_thread_report_progress(&h
->cur_pic_ptr
->tf
, INT_MAX
,
1754 h
->picture_structure
== PICT_BOTTOM_FIELD
);
1757 return (ret
< 0) ? ret
: buf_index
;
1761 * Return the number of bytes consumed for building the current frame.
1763 static int get_consumed_bytes(int pos
, int buf_size
)
1766 pos
= 1; // avoid infinite loops (I doubt that is needed but...)
1767 if (pos
+ 10 > buf_size
)
1768 pos
= buf_size
; // oops ;)
1773 static int output_frame(H264Context
*h
, AVFrame
*dst
, H264Picture
*srcp
)
1775 AVFrame
*src
= &srcp
->f
;
1776 const AVPixFmtDescriptor
*desc
= av_pix_fmt_desc_get(src
->format
);
1778 int ret
= av_frame_ref(dst
, src
);
1782 av_dict_set(&dst
->metadata
, "stereo_mode", ff_h264_sei_stereo_mode(h
), 0);
1784 if (srcp
->sei_recovery_frame_cnt
== 0)
1789 for (i
= 0; i
< desc
->nb_components
; i
++) {
1790 int hshift
= (i
> 0) ? desc
->log2_chroma_w
: 0;
1791 int vshift
= (i
> 0) ? desc
->log2_chroma_h
: 0;
1792 int off
= ((srcp
->crop_left
>> hshift
) << h
->pixel_shift
) +
1793 (srcp
->crop_top
>> vshift
) * dst
->linesize
[i
];
1794 dst
->data
[i
] += off
;
1799 static int is_extra(const uint8_t *buf
, int buf_size
)
1801 int cnt
= buf
[5]&0x1f;
1802 const uint8_t *p
= buf
+6;
1804 int nalsize
= AV_RB16(p
) + 2;
1805 if(nalsize
> buf_size
- (p
-buf
) || p
[2]!=0x67)
1813 int nalsize
= AV_RB16(p
) + 2;
1814 if(nalsize
> buf_size
- (p
-buf
) || p
[2]!=0x68)
1821 static int h264_decode_frame(AVCodecContext
*avctx
, void *data
,
1822 int *got_frame
, AVPacket
*avpkt
)
1824 const uint8_t *buf
= avpkt
->data
;
1825 int buf_size
= avpkt
->size
;
1826 H264Context
*h
= avctx
->priv_data
;
1827 AVFrame
*pict
= data
;
1833 h
->flags
= avctx
->flags
;
1834 /* reset data partitioning here, to ensure GetBitContexts from previous
1835 * packets do not get used. */
1836 h
->data_partitioning
= 0;
1838 /* end of stream, output what is still in the buffers */
1839 if (buf_size
== 0) {
1842 h
->cur_pic_ptr
= NULL
;
1845 // FIXME factorize this with the output code below
1846 out
= h
->delayed_pic
[0];
1849 h
->delayed_pic
[i
] &&
1850 !h
->delayed_pic
[i
]->f
.key_frame
&&
1851 !h
->delayed_pic
[i
]->mmco_reset
;
1853 if (h
->delayed_pic
[i
]->poc
< out
->poc
) {
1854 out
= h
->delayed_pic
[i
];
1858 for (i
= out_idx
; h
->delayed_pic
[i
]; i
++)
1859 h
->delayed_pic
[i
] = h
->delayed_pic
[i
+ 1];
1862 out
->reference
&= ~DELAYED_PIC_REF
;
1863 ret
= output_frame(h
, pict
, out
);
1871 if (h
->is_avc
&& av_packet_get_side_data(avpkt
, AV_PKT_DATA_NEW_EXTRADATA
, NULL
)) {
1873 uint8_t *side
= av_packet_get_side_data(avpkt
, AV_PKT_DATA_NEW_EXTRADATA
, &side_size
);
1874 if (is_extra(side
, side_size
))
1875 ff_h264_decode_extradata(h
, side
, side_size
);
1877 if(h
->is_avc
&& buf_size
>= 9 && buf
[0]==1 && buf
[2]==0 && (buf
[4]&0xFC)==0xFC && (buf
[5]&0x1F) && buf
[8]==0x67){
1878 if (is_extra(buf
, buf_size
))
1879 return ff_h264_decode_extradata(h
, buf
, buf_size
);
1882 buf_index
= decode_nal_units(h
, buf
, buf_size
, 0);
1884 return AVERROR_INVALIDDATA
;
1886 if (!h
->cur_pic_ptr
&& h
->nal_unit_type
== NAL_END_SEQUENCE
) {
1887 av_assert0(buf_index
<= buf_size
);
1891 if (!(avctx
->flags2
& CODEC_FLAG2_CHUNKS
) && !h
->cur_pic_ptr
) {
1892 if (avctx
->skip_frame
>= AVDISCARD_NONREF
||
1893 buf_size
>= 4 && !memcmp("Q264", buf
, 4))
1895 av_log(avctx
, AV_LOG_ERROR
, "no frame!\n");
1896 return AVERROR_INVALIDDATA
;
1899 if (!(avctx
->flags2
& CODEC_FLAG2_CHUNKS
) ||
1900 (h
->mb_y
>= h
->mb_height
&& h
->mb_height
)) {
1901 if (avctx
->flags2
& CODEC_FLAG2_CHUNKS
)
1902 decode_postinit(h
, 1);
1904 ff_h264_field_end(h
, 0);
1906 /* Wait for second field. */
1908 if (h
->next_output_pic
&& (
1909 h
->next_output_pic
->recovered
)) {
1910 if (!h
->next_output_pic
->recovered
)
1911 h
->next_output_pic
->f
.flags
|= AV_FRAME_FLAG_CORRUPT
;
1913 ret
= output_frame(h
, pict
, h
->next_output_pic
);
1917 if (CONFIG_MPEGVIDEO
) {
1918 ff_print_debug_info2(h
->avctx
, pict
, h
->er
.mbskip_table
,
1919 h
->next_output_pic
->mb_type
,
1920 h
->next_output_pic
->qscale_table
,
1921 h
->next_output_pic
->motion_val
,
1923 h
->mb_width
, h
->mb_height
, h
->mb_stride
, 1);
1928 assert(pict
->buf
[0] || !*got_frame
);
1930 return get_consumed_bytes(buf_index
, buf_size
);
1933 av_cold
void ff_h264_free_context(H264Context
*h
)
1937 ff_h264_free_tables(h
, 1); // FIXME cleanup init stuff perhaps
1939 for (i
= 0; i
< MAX_SPS_COUNT
; i
++)
1940 av_freep(h
->sps_buffers
+ i
);
1942 for (i
= 0; i
< MAX_PPS_COUNT
; i
++)
1943 av_freep(h
->pps_buffers
+ i
);
1946 static av_cold
int h264_decode_end(AVCodecContext
*avctx
)
1948 H264Context
*h
= avctx
->priv_data
;
1950 ff_h264_remove_all_refs(h
);
1951 ff_h264_free_context(h
);
1953 ff_h264_unref_picture(h
, &h
->cur_pic
);
1958 static const AVProfile profiles
[] = {
1959 { FF_PROFILE_H264_BASELINE
, "Baseline" },
1960 { FF_PROFILE_H264_CONSTRAINED_BASELINE
, "Constrained Baseline" },
1961 { FF_PROFILE_H264_MAIN
, "Main" },
1962 { FF_PROFILE_H264_EXTENDED
, "Extended" },
1963 { FF_PROFILE_H264_HIGH
, "High" },
1964 { FF_PROFILE_H264_HIGH_10
, "High 10" },
1965 { FF_PROFILE_H264_HIGH_10_INTRA
, "High 10 Intra" },
1966 { FF_PROFILE_H264_HIGH_422
, "High 4:2:2" },
1967 { FF_PROFILE_H264_HIGH_422_INTRA
, "High 4:2:2 Intra" },
1968 { FF_PROFILE_H264_HIGH_444
, "High 4:4:4" },
1969 { FF_PROFILE_H264_HIGH_444_PREDICTIVE
, "High 4:4:4 Predictive" },
1970 { FF_PROFILE_H264_HIGH_444_INTRA
, "High 4:4:4 Intra" },
1971 { FF_PROFILE_H264_CAVLC_444
, "CAVLC 4:4:4" },
1972 { FF_PROFILE_UNKNOWN
},
1975 static const AVOption h264_options
[] = {
1976 {"is_avc", "is avc", offsetof(H264Context
, is_avc
), FF_OPT_TYPE_INT
, {.i64
= 0}, 0, 1, 0},
1977 {"nal_length_size", "nal_length_size", offsetof(H264Context
, nal_length_size
), FF_OPT_TYPE_INT
, {.i64
= 0}, 0, 4, 0},
1981 static const AVClass h264_class
= {
1982 .class_name
= "H264 Decoder",
1983 .item_name
= av_default_item_name
,
1984 .option
= h264_options
,
1985 .version
= LIBAVUTIL_VERSION_INT
,
1988 AVCodec ff_h264_decoder
= {
1990 .long_name
= NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
1991 .type
= AVMEDIA_TYPE_VIDEO
,
1992 .id
= AV_CODEC_ID_H264
,
1993 .priv_data_size
= sizeof(H264Context
),
1994 .init
= ff_h264_decode_init
,
1995 .close
= h264_decode_end
,
1996 .decode
= h264_decode_frame
,
1997 .capabilities
= /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1
|
1998 CODEC_CAP_DELAY
| CODEC_CAP_SLICE_THREADS
|
1999 CODEC_CAP_FRAME_THREADS
,
2001 .init_thread_copy
= ONLY_IF_THREADS_ENABLED(decode_init_thread_copy
),
2002 .update_thread_context
= ONLY_IF_THREADS_ENABLED(ff_h264_update_thread_context
),
2003 .profiles
= NULL_IF_CONFIG_SMALL(profiles
),
2004 .priv_class
= &h264_class
,
2007 #if CONFIG_H264_VDPAU_DECODER
2008 static const AVClass h264_vdpau_class
= {
2009 .class_name
= "H264 VDPAU Decoder",
2010 .item_name
= av_default_item_name
,
2011 .option
= h264_options
,
2012 .version
= LIBAVUTIL_VERSION_INT
,
2015 AVCodec ff_h264_vdpau_decoder
= {
2016 .name
= "h264_vdpau",
2017 .long_name
= NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (VDPAU acceleration)"),
2018 .type
= AVMEDIA_TYPE_VIDEO
,
2019 .id
= AV_CODEC_ID_H264
,
2020 .priv_data_size
= sizeof(H264Context
),
2021 .init
= ff_h264_decode_init
,
2022 .close
= h264_decode_end
,
2023 .decode
= h264_decode_frame
,
2024 .capabilities
= CODEC_CAP_DR1
| CODEC_CAP_DELAY
| CODEC_CAP_HWACCEL_VDPAU
,
2026 .pix_fmts
= (const enum AVPixelFormat
[]) { AV_PIX_FMT_VDPAU_H264
,
2028 .profiles
= NULL_IF_CONFIG_SMALL(profiles
),
2029 .priv_class
= &h264_vdpau_class
,