FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h264dec.h
Date: 2023-10-02 11:06:47
Exec Total Coverage
Lines: 2 2 100.0%
Functions: 1 1 100.0%
Branches: 0 0 -%

Line Branch Exec Source
1 /*
2 * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 /**
23 * @file
24 * H.264 / AVC / MPEG-4 part10 codec.
25 * @author Michael Niedermayer <michaelni@gmx.at>
26 */
27
28 #ifndef AVCODEC_H264DEC_H
29 #define AVCODEC_H264DEC_H
30
31 #include "libavutil/buffer.h"
32 #include "libavutil/mem_internal.h"
33
34 #include "cabac.h"
35 #include "error_resilience.h"
36 #include "h264_parse.h"
37 #include "h264_ps.h"
38 #include "h264_sei.h"
39 #include "h2645_parse.h"
40 #include "h264chroma.h"
41 #include "h264dsp.h"
42 #include "h264pred.h"
43 #include "h264qpel.h"
44 #include "h274.h"
45 #include "mpegutils.h"
46 #include "videodsp.h"
47
48 #define H264_MAX_PICTURE_COUNT 36
49
50 /* Compiling in interlaced support reduces the speed
51 * of progressive decoding by about 2%. */
52 #define ALLOW_INTERLACE
53
54 #define FMO 0
55
56 /**
57 * The maximum number of slices supported by the decoder.
58 * must be a power of 2
59 */
60 #define MAX_SLICES 32
61
62 #ifdef ALLOW_INTERLACE
63 #define MB_MBAFF(h) (h)->mb_mbaff
64 #define MB_FIELD(sl) (sl)->mb_field_decoding_flag
65 #define FRAME_MBAFF(h) (h)->mb_aff_frame
66 #define FIELD_PICTURE(h) ((h)->picture_structure != PICT_FRAME)
67 #define LEFT_MBS 2
68 #define LTOP 0
69 #define LBOT 1
70 #define LEFT(i) (i)
71 #else
72 #define MB_MBAFF(h) 0
73 #define MB_FIELD(sl) 0
74 #define FRAME_MBAFF(h) 0
75 #define FIELD_PICTURE(h) 0
76 #undef IS_INTERLACED
77 #define IS_INTERLACED(mb_type) 0
78 #define LEFT_MBS 1
79 #define LTOP 0
80 #define LBOT 0
81 #define LEFT(i) 0
82 #endif
83 #define FIELD_OR_MBAFF_PICTURE(h) (FRAME_MBAFF(h) || FIELD_PICTURE(h))
84
85 #ifndef CABAC
86 #define CABAC(h) (h)->ps.pps->cabac
87 #endif
88
89 #define CHROMA(h) ((h)->ps.sps->chroma_format_idc)
90 #define CHROMA422(h) ((h)->ps.sps->chroma_format_idc == 2)
91 #define CHROMA444(h) ((h)->ps.sps->chroma_format_idc == 3)
92
93 #define IS_REF0(a) ((a) & MB_TYPE_REF0)
94 #define IS_8x8DCT(a) ((a) & MB_TYPE_8x8DCT)
95
96 /**
97 * Memory management control operation.
98 */
99 typedef struct MMCO {
100 MMCOOpcode opcode;
101 int short_pic_num; ///< pic_num without wrapping (pic_num & max_pic_num)
102 int long_arg; ///< index, pic_num, or num long refs depending on opcode
103 } MMCO;
104
105 typedef struct H264Picture {
106 AVFrame *f;
107 ThreadFrame tf;
108
109 AVFrame *f_grain;
110
111 AVBufferRef *qscale_table_buf;
112 int8_t *qscale_table;
113
114 AVBufferRef *motion_val_buf[2];
115 int16_t (*motion_val[2])[2];
116
117 AVBufferRef *mb_type_buf;
118 uint32_t *mb_type;
119
120 AVBufferRef *hwaccel_priv_buf;
121 void *hwaccel_picture_private; ///< hardware accelerator private data
122
123 AVBufferRef *ref_index_buf[2];
124 int8_t *ref_index[2];
125
126 int field_poc[2]; ///< top/bottom POC
127 int poc; ///< frame POC
128 int frame_num; ///< frame_num (raw frame_num from slice header)
129 int mmco_reset; /**< MMCO_RESET set this 1. Reordering code must
130 not mix pictures before and after MMCO_RESET. */
131 int pic_id; /**< pic_num (short -> no wrap version of pic_num,
132 pic_num & max_pic_num; long -> long_pic_num) */
133 int long_ref; ///< 1->long term reference 0->short term reference
134 int ref_poc[2][2][32]; ///< POCs of the frames/fields used as reference (FIXME need per slice)
135 int ref_count[2][2]; ///< number of entries in ref_poc (FIXME need per slice)
136 int mbaff; ///< 1 -> MBAFF frame 0-> not MBAFF
137 int field_picture; ///< whether or not picture was encoded in separate fields
138
139 /**
140 * H264Picture.reference has this flag set,
141 * when the picture is held for delayed output.
142 */
143 #define DELAYED_PIC_REF (1 << 2)
144 int reference;
145 int recovered; ///< picture at IDR or recovery point + recovery count
146 int invalid_gap;
147 int sei_recovery_frame_cnt;
148 int needs_fg; ///< whether picture needs film grain synthesis (see `f_grain`)
149
150 AVBufferRef *pps_buf;
151 const PPS *pps;
152
153 int mb_width, mb_height;
154 int mb_stride;
155
156 /* data points to an atomic_int */
157 AVBufferRef *decode_error_flags;
158 } H264Picture;
159
160 typedef struct H264Ref {
161 uint8_t *data[3];
162 int linesize[3];
163
164 int reference;
165 int poc;
166 int pic_id;
167
168 H264Picture *parent;
169 } H264Ref;
170
171 typedef struct H264SliceContext {
172 const struct H264Context *h264;
173 GetBitContext gb;
174 ERContext *er;
175
176 int slice_num;
177 int slice_type;
178 int slice_type_nos; ///< S free slice type (SI/SP are remapped to I/P)
179 int slice_type_fixed;
180
181 int qscale;
182 int chroma_qp[2]; // QPc
183 int qp_thresh; ///< QP threshold to skip loopfilter
184 int last_qscale_diff;
185
186 // deblock
187 int deblocking_filter; ///< disable_deblocking_filter_idc with 1 <-> 0
188 int slice_alpha_c0_offset;
189 int slice_beta_offset;
190
191 H264PredWeightTable pwt;
192
193 int prev_mb_skipped;
194 int next_mb_skipped;
195
196 int chroma_pred_mode;
197 int intra16x16_pred_mode;
198
199 int8_t intra4x4_pred_mode_cache[5 * 8];
200 int8_t(*intra4x4_pred_mode);
201
202 int topleft_mb_xy;
203 int top_mb_xy;
204 int topright_mb_xy;
205 int left_mb_xy[LEFT_MBS];
206
207 int topleft_type;
208 int top_type;
209 int topright_type;
210 int left_type[LEFT_MBS];
211
212 const uint8_t *left_block;
213 int topleft_partition;
214
215 unsigned int topleft_samples_available;
216 unsigned int top_samples_available;
217 unsigned int topright_samples_available;
218 unsigned int left_samples_available;
219
220 ptrdiff_t linesize, uvlinesize;
221 ptrdiff_t mb_linesize; ///< may be equal to s->linesize or s->linesize * 2, for mbaff
222 ptrdiff_t mb_uvlinesize;
223
224 int mb_x, mb_y;
225 int mb_xy;
226 int resync_mb_x;
227 int resync_mb_y;
228 unsigned int first_mb_addr;
229 // index of the first MB of the next slice
230 int next_slice_idx;
231 int mb_skip_run;
232 int is_complex;
233
234 int picture_structure;
235 int mb_field_decoding_flag;
236 int mb_mbaff; ///< mb_aff_frame && mb_field_decoding_flag
237
238 int redundant_pic_count;
239
240 /**
241 * number of neighbors (top and/or left) that used 8x8 dct
242 */
243 int neighbor_transform_size;
244
245 int direct_spatial_mv_pred;
246 int col_parity;
247 int col_fieldoff;
248
249 int cbp;
250 int top_cbp;
251 int left_cbp;
252
253 int dist_scale_factor[32];
254 int dist_scale_factor_field[2][32];
255 int map_col_to_list0[2][16 + 32];
256 int map_col_to_list0_field[2][2][16 + 32];
257
258 /**
259 * num_ref_idx_l0/1_active_minus1 + 1
260 */
261 unsigned int ref_count[2]; ///< counts frames or fields, depending on current mb mode
262 unsigned int list_count;
263 H264Ref ref_list[2][48]; /**< 0..15: frame refs, 16..47: mbaff field refs.
264 * Reordered version of default_ref_list
265 * according to picture reordering in slice header */
266 struct {
267 uint8_t op;
268 uint32_t val;
269 } ref_modifications[2][32];
270 int nb_ref_modifications[2];
271
272 unsigned int pps_id;
273
274 const uint8_t *intra_pcm_ptr;
275
276 uint8_t *bipred_scratchpad;
277 uint8_t *edge_emu_buffer;
278 uint8_t (*top_borders[2])[(16 * 3) * 2];
279 int bipred_scratchpad_allocated;
280 int edge_emu_buffer_allocated;
281 int top_borders_allocated[2];
282
283 /**
284 * non zero coeff count cache.
285 * is 64 if not available.
286 */
287 DECLARE_ALIGNED(8, uint8_t, non_zero_count_cache)[15 * 8];
288
289 /**
290 * Motion vector cache.
291 */
292 DECLARE_ALIGNED(16, int16_t, mv_cache)[2][5 * 8][2];
293 DECLARE_ALIGNED(8, int8_t, ref_cache)[2][5 * 8];
294 DECLARE_ALIGNED(16, uint8_t, mvd_cache)[2][5 * 8][2];
295 uint8_t direct_cache[5 * 8];
296
297 DECLARE_ALIGNED(8, uint16_t, sub_mb_type)[4];
298
299 ///< as a DCT coefficient is int32_t in high depth, we need to reserve twice the space.
300 DECLARE_ALIGNED(16, int16_t, mb)[16 * 48 * 2];
301 DECLARE_ALIGNED(16, int16_t, mb_luma_dc)[3][16 * 2];
302 ///< as mb is addressed by scantable[i] and scantable is uint8_t we can either
303 ///< check that i is not too large or ensure that there is some unused stuff after mb
304 int16_t mb_padding[256 * 2];
305
306 uint8_t (*mvd_table[2])[2];
307
308 /**
309 * Cabac
310 */
311 CABACContext cabac;
312 uint8_t cabac_state[1024];
313 int cabac_init_idc;
314
315 MMCO mmco[H264_MAX_MMCO_COUNT];
316 int nb_mmco;
317 int explicit_ref_marking;
318
319 int frame_num;
320 int idr_pic_id;
321 int poc_lsb;
322 int delta_poc_bottom;
323 int delta_poc[2];
324 int curr_pic_num;
325 int max_pic_num;
326 } H264SliceContext;
327
328 /**
329 * H264Context
330 */
331 typedef struct H264Context {
332 const AVClass *class;
333 AVCodecContext *avctx;
334 VideoDSPContext vdsp;
335 H264DSPContext h264dsp;
336 H264ChromaContext h264chroma;
337 H264QpelContext h264qpel;
338 H274FilmGrainDatabase h274db;
339
340 H264Picture DPB[H264_MAX_PICTURE_COUNT];
341 H264Picture *cur_pic_ptr;
342 H264Picture cur_pic;
343 H264Picture last_pic_for_ec;
344
345 H264SliceContext *slice_ctx;
346 int nb_slice_ctx;
347 int nb_slice_ctx_queued;
348
349 H2645Packet pkt;
350
351 int pixel_shift; ///< 0 for 8-bit H.264, 1 for high-bit-depth H.264
352
353 /* coded dimensions -- 16 * mb w/h */
354 int width, height;
355 int chroma_x_shift, chroma_y_shift;
356
357 int droppable;
358 int coded_picture_number;
359
360 int context_initialized;
361 int flags;
362 int workaround_bugs;
363 int x264_build;
364 /* Set when slice threading is used and at least one slice uses deblocking
365 * mode 1 (i.e. across slice boundaries). Then we disable the loop filter
366 * during normal MB decoding and execute it serially at the end.
367 */
368 int postpone_filter;
369
370 /*
371 * Set to 1 when the current picture is IDR, 0 otherwise.
372 */
373 int picture_idr;
374
375 /*
376 * Set to 1 when the current picture contains only I slices, 0 otherwise.
377 */
378 int picture_intra_only;
379
380 int crop_left;
381 int crop_right;
382 int crop_top;
383 int crop_bottom;
384
385 int8_t(*intra4x4_pred_mode);
386 H264PredContext hpc;
387
388 uint8_t (*non_zero_count)[48];
389
390 #define LIST_NOT_USED -1 // FIXME rename?
391
392 /**
393 * block_offset[ 0..23] for frame macroblocks
394 * block_offset[24..47] for field macroblocks
395 */
396 int block_offset[2 * (16 * 3)];
397
398 uint32_t *mb2b_xy; // FIXME are these 4 a good idea?
399 uint32_t *mb2br_xy;
400 int b_stride; // FIXME use s->b4_stride
401
402 uint16_t *slice_table; ///< slice_table_base + 2*mb_stride + 1
403
404 // interlacing specific flags
405 int mb_aff_frame;
406 int picture_structure;
407 int first_field;
408
409 uint8_t *list_counts; ///< Array of list_count per MB specifying the slice type
410
411 /* 0x100 -> non null luma_dc, 0x80/0x40 -> non null chroma_dc (cb/cr), 0x?0 -> chroma_cbp(0, 1, 2), 0x0? luma_cbp */
412 uint16_t *cbp_table;
413
414 /* chroma_pred_mode for i4x4 or i16x16, else 0 */
415 uint8_t *chroma_pred_mode_table;
416 uint8_t (*mvd_table[2])[2];
417 uint8_t *direct_table;
418
419 uint8_t scan_padding[16];
420 uint8_t zigzag_scan[16];
421 uint8_t zigzag_scan8x8[64];
422 uint8_t zigzag_scan8x8_cavlc[64];
423 uint8_t field_scan[16];
424 uint8_t field_scan8x8[64];
425 uint8_t field_scan8x8_cavlc[64];
426 uint8_t zigzag_scan_q0[16];
427 uint8_t zigzag_scan8x8_q0[64];
428 uint8_t zigzag_scan8x8_cavlc_q0[64];
429 uint8_t field_scan_q0[16];
430 uint8_t field_scan8x8_q0[64];
431 uint8_t field_scan8x8_cavlc_q0[64];
432
433 int mb_y;
434 int mb_height, mb_width;
435 int mb_stride;
436 int mb_num;
437
438 // =============================================================
439 // Things below are not used in the MB or more inner code
440
441 int nal_ref_idc;
442 int nal_unit_type;
443
444 int has_slice; ///< slice NAL is found in the packet, set by decode_nal_units, its state does not need to be preserved outside h264_decode_frame()
445
446 /**
447 * Used to parse AVC variant of H.264
448 */
449 int is_avc; ///< this flag is != 0 if codec is avc1
450 int nal_length_size; ///< Number of bytes used for nal length (1, 2 or 4)
451
452 int bit_depth_luma; ///< luma bit depth from sps to detect changes
453 int chroma_format_idc; ///< chroma format from sps to detect changes
454
455 H264ParamSets ps;
456
457 uint16_t *slice_table_base;
458
459 H264POCContext poc;
460
461 H264Ref default_ref[2];
462 H264Picture *short_ref[32];
463 H264Picture *long_ref[32];
464 H264Picture *delayed_pic[H264_MAX_DPB_FRAMES + 2]; // FIXME size?
465 int last_pocs[H264_MAX_DPB_FRAMES];
466 H264Picture *next_output_pic;
467 int next_outputed_poc;
468 int poc_offset; ///< PicOrderCnt_offset from SMPTE RDD-2006
469
470 /**
471 * memory management control operations buffer.
472 */
473 MMCO mmco[H264_MAX_MMCO_COUNT];
474 int nb_mmco;
475 int mmco_reset;
476 int explicit_ref_marking;
477
478 int long_ref_count; ///< number of actual long term references
479 int short_ref_count; ///< number of actual short term references
480
481 /**
482 * @name Members for slice based multithreading
483 * @{
484 */
485 /**
486 * current slice number, used to initialize slice_num of each thread/context
487 */
488 int current_slice;
489
490 /** @} */
491
492 /**
493 * Complement sei_pic_struct
494 * SEI_PIC_STRUCT_TOP_BOTTOM and SEI_PIC_STRUCT_BOTTOM_TOP indicate interlaced frames.
495 * However, soft telecined frames may have these values.
496 * This is used in an attempt to flag soft telecine progressive.
497 */
498 int prev_interlaced_frame;
499
500 /**
501 * Are the SEI recovery points looking valid.
502 */
503 int valid_recovery_point;
504
505 /**
506 * recovery_frame is the frame_num at which the next frame should
507 * be fully constructed.
508 *
509 * Set to -1 when not expecting a recovery point.
510 */
511 int recovery_frame;
512
513 /**
514 * We have seen an IDR, so all the following frames in coded order are correctly
515 * decodable.
516 */
517 #define FRAME_RECOVERED_IDR (1 << 0)
518 /**
519 * Sufficient number of frames have been decoded since a SEI recovery point,
520 * so all the following frames in presentation order are correct.
521 */
522 #define FRAME_RECOVERED_SEI (1 << 1)
523
524 int frame_recovered; ///< Initial frame has been completely recovered
525
526 int has_recovery_point;
527
528 int missing_fields;
529
530 /* for frame threading, this is set to 1
531 * after finish_setup() has been called, so we cannot modify
532 * some context properties (which are supposed to stay constant between
533 * slices) anymore */
534 int setup_finished;
535
536 int cur_chroma_format_idc;
537 int cur_bit_depth_luma;
538 int16_t slice_row[MAX_SLICES]; ///< to detect when MAX_SLICES is too low
539
540 /* original AVCodecContext dimensions, used to handle container
541 * cropping */
542 int width_from_caller;
543 int height_from_caller;
544
545 int enable_er;
546 ERContext er;
547 int16_t *dc_val_base;
548
549 H264SEIContext sei;
550
551 AVBufferPool *qscale_table_pool;
552 AVBufferPool *mb_type_pool;
553 AVBufferPool *motion_val_pool;
554 AVBufferPool *ref_index_pool;
555 AVBufferPool *decode_error_flags_pool;
556 int ref2frm[MAX_SLICES][2][64]; ///< reference to frame number lists, used in the loop filter, the first 2 are for -2,-1
557 } H264Context;
558
559 extern const uint16_t ff_h264_mb_sizes[4];
560
561 /**
562 * Reconstruct bitstream slice_type.
563 */
564 int ff_h264_get_slice_type(const H264SliceContext *sl);
565
566 /**
567 * Allocate tables.
568 * needs width/height
569 */
570 int ff_h264_alloc_tables(H264Context *h);
571
572 int ff_h264_decode_ref_pic_list_reordering(H264SliceContext *sl, void *logctx);
573 int ff_h264_build_ref_list(H264Context *h, H264SliceContext *sl);
574 void ff_h264_remove_all_refs(H264Context *h);
575
576 /**
577 * Execute the reference picture marking (memory management control operations).
578 */
579 int ff_h264_execute_ref_pic_marking(H264Context *h);
580
581 int ff_h264_decode_ref_pic_marking(H264SliceContext *sl, GetBitContext *gb,
582 const H2645NAL *nal, void *logctx);
583
584 void ff_h264_hl_decode_mb(const H264Context *h, H264SliceContext *sl);
585 void ff_h264_decode_init_vlc(void);
586
587 /**
588 * Decode a macroblock
589 * @return 0 if OK, ER_AC_ERROR / ER_DC_ERROR / ER_MV_ERROR on error
590 */
591 int ff_h264_decode_mb_cavlc(const H264Context *h, H264SliceContext *sl);
592
593 /**
594 * Decode a CABAC coded macroblock
595 * @return 0 if OK, ER_AC_ERROR / ER_DC_ERROR / ER_MV_ERROR on error
596 */
597 int ff_h264_decode_mb_cabac(const H264Context *h, H264SliceContext *sl);
598
599 void ff_h264_init_cabac_states(const H264Context *h, H264SliceContext *sl);
600
601 void ff_h264_direct_dist_scale_factor(const H264Context *const h, H264SliceContext *sl);
602 void ff_h264_direct_ref_list_init(const H264Context *const h, H264SliceContext *sl);
603 void ff_h264_pred_direct_motion(const H264Context *const h, H264SliceContext *sl,
604 int *mb_type);
605
606 void ff_h264_filter_mb_fast(const H264Context *h, H264SliceContext *sl, int mb_x, int mb_y,
607 uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr,
608 unsigned int linesize, unsigned int uvlinesize);
609 void ff_h264_filter_mb(const H264Context *h, H264SliceContext *sl, int mb_x, int mb_y,
610 uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr,
611 unsigned int linesize, unsigned int uvlinesize);
612
613 /*
614 * o-o o-o
615 * / / /
616 * o-o o-o
617 * ,---'
618 * o-o o-o
619 * / / /
620 * o-o o-o
621 */
622
623 /* Scan8 organization:
624 * 0 1 2 3 4 5 6 7
625 * 0 DY y y y y y
626 * 1 y Y Y Y Y
627 * 2 y Y Y Y Y
628 * 3 y Y Y Y Y
629 * 4 y Y Y Y Y
630 * 5 DU u u u u u
631 * 6 u U U U U
632 * 7 u U U U U
633 * 8 u U U U U
634 * 9 u U U U U
635 * 10 DV v v v v v
636 * 11 v V V V V
637 * 12 v V V V V
638 * 13 v V V V V
639 * 14 v V V V V
640 * DY/DU/DV are for luma/chroma DC.
641 */
642
643 #define LUMA_DC_BLOCK_INDEX 48
644 #define CHROMA_DC_BLOCK_INDEX 49
645
646 /**
647 * Get the chroma qp.
648 */
649 62343666 static av_always_inline int get_chroma_qp(const PPS *pps, int t, int qscale)
650 {
651 62343666 return pps->chroma_qp_table[t][qscale];
652 }
653
654 int ff_h264_field_end(H264Context *h, H264SliceContext *sl, int in_setup);
655
656 int ff_h264_ref_picture(H264Context *h, H264Picture *dst, H264Picture *src);
657 int ff_h264_replace_picture(H264Context *h, H264Picture *dst, const H264Picture *src);
658 void ff_h264_unref_picture(H264Context *h, H264Picture *pic);
659
660 void ff_h264_slice_context_init(H264Context *h, H264SliceContext *sl);
661
662 void ff_h264_draw_horiz_band(const H264Context *h, H264SliceContext *sl, int y, int height);
663
664 /**
665 * Submit a slice for decoding.
666 *
667 * Parse the slice header, starting a new field/frame if necessary. If any
668 * slices are queued for the previous field, they are decoded.
669 */
670 int ff_h264_queue_decode_slice(H264Context *h, const H2645NAL *nal);
671 int ff_h264_execute_decode_slices(H264Context *h);
672 int ff_h264_update_thread_context(AVCodecContext *dst,
673 const AVCodecContext *src);
674 int ff_h264_update_thread_context_for_user(AVCodecContext *dst,
675 const AVCodecContext *src);
676
677 void ff_h264_flush_change(H264Context *h);
678
679 void ff_h264_free_tables(H264Context *h);
680
681 void ff_h264_set_erpic(ERPicture *dst, H264Picture *src);
682
683 #endif /* AVCODEC_H264DEC_H */
684