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 / MPEG4 part10 codec.
25 * @author Michael Niedermayer <michaelni@gmx.at>
26 */
27
28 #ifndef AVCODEC_H264_H
29 #define AVCODEC_H264_H
30
39 #include "internal.h" // for avpriv_find_start_code()
45
46 #define H264_MAX_PICTURE_COUNT 36
47 #define H264_MAX_THREADS 32
48
49 #define MAX_SPS_COUNT 32
50 #define MAX_PPS_COUNT 256
51
52 #define MAX_MMCO_COUNT 66
53
54 #define MAX_DELAYED_PIC_COUNT 16
55
56 #define MAX_MBPAIR_SIZE (256*1024) // a tighter bound could be calculated if someone cares about a few bytes
57
58 /* Compiling in interlaced support reduces the speed
59 * of progressive decoding by about 2%. */
60 #define ALLOW_INTERLACE
61
63
64 /**
65 * The maximum number of slices supported by the decoder.
66 * must be a power of 2
67 */
69
70 #ifdef ALLOW_INTERLACE
71 #define MB_MBAFF(h) (h)->mb_mbaff
72 #define MB_FIELD(h) (h)->mb_field_decoding_flag
73 #define FRAME_MBAFF(h) (h)->mb_aff_frame
74 #define FIELD_PICTURE(h) ((h)->picture_structure != PICT_FRAME)
79 #else
80 #define MB_MBAFF(h) 0
81 #define MB_FIELD(h) 0
82 #define FRAME_MBAFF(h) 0
83 #define FIELD_PICTURE(h) 0
84 #undef IS_INTERLACED
85 #define IS_INTERLACED(mb_type) 0
86 #define LEFT_MBS 1
87 #define LTOP 0
88 #define LBOT 0
89 #define LEFT(i) 0
90 #endif
91 #define FIELD_OR_MBAFF_PICTURE(h) (FRAME_MBAFF(h) || FIELD_PICTURE(h))
92
93 #ifndef CABAC
94 #define CABAC(h) (h)->pps.cabac
95 #endif
96
97 #define CHROMA(h) ((h)->sps.chroma_format_idc)
98 #define CHROMA422(h) ((h)->sps.chroma_format_idc == 2)
99 #define CHROMA444(h) ((h)->sps.chroma_format_idc == 3)
100
101 #define EXTENDED_SAR 255
102
103 #define MB_TYPE_REF0 MB_TYPE_ACPRED // dirty but it fits in 16 bit
104 #define MB_TYPE_8x8DCT 0x01000000
105 #define IS_REF0(a) ((a) & MB_TYPE_REF0)
106 #define IS_8x8DCT(a) ((a) & MB_TYPE_8x8DCT)
107
108 #define QP_MAX_NUM (51 + 6*6) // The maximum supported qp
109
110 /* NAL unit types */
111 enum {
127 };
128
129 /**
130 * SEI message types
131 */
141
142 /**
143 * pic_struct in picture timing SEI message
144 */
156
157 /**
158 * frame_packing_arrangement types
159 */
169
170 /**
171 * Sequence parameter set
172 */
191 int mb_aff;
///< mb_adaptive_frame_field_flag
193 int crop;
///< frame_cropping_flag
194
195 /* those 4 are already in luma samples */
196 unsigned int crop_left;
///< frame_cropping_rect_left_offset
197 unsigned int crop_right;
///< frame_cropping_rect_right_offset
198 unsigned int crop_top;
///< frame_cropping_rect_top_offset
230 int new;
///< flag to keep track if the decoder context needs re-init due to changed SPS
232
233 /**
234 * Picture parameter set
235 */
238 int cabac;
///< entropy_coding_mode_flag
242 unsigned int ref_count[2];
///< num_ref_idx_l0/1_active_minus1 + 1
257
258 /**
259 * Frame Packing Arrangement Type
260 */
269
270 /**
271 * Memory management control operation opcode.
272 */
282
283 /**
284 * Memory management control operation.
285 */
289 int long_arg;
///< index, pic_num, or num long refs depending on opcode
291
296
299
302
305
308
311
314 int frame_num;
///< frame_num (raw frame_num from slice header)
315 int mmco_reset;
/**< MMCO_RESET set this 1. Reordering code must
316 not mix pictures before and after MMCO_RESET. */
317 int pic_id;
/**< pic_num (short -> no wrap version of pic_num,
318 pic_num & max_pic_num; long -> long_pic_num) */
319 int long_ref;
///< 1->long term reference 0->short term reference
320 int ref_poc[2][2][32];
///< POCs of the frames/fields used as reference (FIXME need per slice)
321 int ref_count[2][2];
///< number of entries in ref_poc (FIXME need per slice)
322 int mbaff;
///< 1 -> MBAFF frame 0-> not MBAFF
323 int field_picture;
///< whether or not picture was encoded in separate fields
324
325 int needs_realloc;
///< picture needs to be reallocated (eg due to a frame size change)
327 int recovered;
///< picture at IDR or recovery point + recovery count
330
335
336 /**
337 * H264Context
338 */
348
353
356
358
359 /* coded dimensions -- 16 * mb w/h */
363
368
372
375
376 // prediction stuff
379
384
389
392
401
402 /**
403 * non zero coeff count cache.
404 * is 64 if not available.
405 */
407
409
410 /**
411 * Motion vector cache.
412 */
415 #define LIST_NOT_USED -1 // FIXME rename?
416 #define PART_NOT_AVAILABLE -2
417
418 /**
419 * number of neighbors (top and/or left) that used 8x8 dct
420 */
422
423 /**
424 * block_offset[ 0..23] for frame macroblocks
425 * block_offset[24..47] for field macroblocks
426 */
428
429 uint32_t *
mb2b_xy;
// FIXME are these 4 a good idea?
432
433 ptrdiff_t
mb_linesize;
///< may be equal to s->linesize or s->linesize * 2, for mbaff
435
439
441
446
452
453 // interlacing specific flags
456 int mb_mbaff;
///< mb_aff_frame && mb_field_decoding_flag
459
461
462 // Weighted pred stuff
467 // The following 2 can be changed to int8_t but that causes 10cpu cycles speedloss
471
479
480 /**
481 * num_ref_idx_l0/1_active_minus1 + 1
482 */
483 unsigned int ref_count[2];
///< counts frames or fields, depending on current mb mode
487 * Reordered version of default_ref_list
488 * according to picture reordering in slice header */
489 int ref2frm[
MAX_SLICES][2][64];
///< reference to frame number lists, used in the loop filter, the first 2 are for -2,-1
490
491 // data partitioning
496
498 DECLARE_ALIGNED(16, int16_t,
mb)[16 * 48 * 2];
///< as a dct coefficient is int32_t in high depth, we need to reserve twice the space.
500 int16_t
mb_padding[256 * 2];
///< as mb is addressed by scantable[i] and scantable is uint8_t we can either check that i is not too large or ensure that there is some unused stuff after mb
501
502 /**
503 * Cabac
504 */
507
508 /* 0x100 -> non null luma_dc, 0x80/0x40 -> non null chroma_dc (cb/cr), 0x?0 -> chroma_cbp(0, 1, 2), 0x0? luma_cbp */
513 /* chroma_pred_mode for i4x4 or i16x16, else 0 */
520
533
535
544
546
547 // deblock
551
552 // =============================================================
553 // Things below are not used in the MB or more inner code
554
559
560 /**
561 * Used to parse AVC variant of h264
562 */
563 int is_avc;
///< this flag is != 0 if codec is avc1
565
568
571
573
575
576 // POC stuff
582 int prev_poc_msb;
///< poc_msb of the last reference pic for POC type 0
583 int prev_poc_lsb;
///< poc_lsb of the last reference pic for POC type 0
587
588 /**
589 * frame_num for frames or 2 * frame_num + 1 for field pics.
590 */
592
593 /**
594 * max_frame_num or 2 * max_frame_num for field pics.
595 */
597
599
608
609 /**
610 * memory management control operations buffer.
611 */
615
618
620
621 /**
622 * @name Members for slice based multithreading
623 * @{
624 */
626
627 /**
628 * current slice number, used to initialize slice_num of each thread/context
629 */
631
632 /**
633 * Max number of threads / contexts.
634 * This is equal to AVCodecContext.thread_count unless
635 * multithreaded decoding is impossible, in which case it is
636 * reduced to 1.
637 */
639
641
642 /**
643 * 1 if the single thread fallback warning has already been
644 * displayed, 0 otherwise.
645 */
647
649
652 /** @} */
653
654 /**
655 * pic_struct in picture timing SEI message
656 */
658
659 /**
660 * Complement sei_pic_struct
661 * SEI_PIC_STRUCT_TOP_BOTTOM and SEI_PIC_STRUCT_BOTTOM_TOP indicate interlaced frames.
662 * However, soft telecined frames may have these values.
663 * This is used in an attempt to flag soft telecine progressive.
664 */
666
667 /**
668 * frame_packing_arrangment SEI message
669 */
674
675 /**
676 * display orientation SEI message
677 */
681
682 /**
683 * Bit set of clock types for fields/frames in picture timing SEI message.
684 * For each found ct_type, appropriate bit is set (e.g., bit 1 for
685 * interlaced).
686 */
688
689 /**
690 * dpb_output_delay in picture timing SEI message, see H.264 C.2.2
691 */
693
694 /**
695 * cpb_removal_delay in picture timing SEI message, see H.264 C.1.2
696 */
698
699 /**
700 * recovery_frame_cnt from SEI message
701 *
702 * Set to -1 if no recovery point SEI message found or to number of frames
703 * before playback synchronizes. Frames having recovery point are key
704 * frames.
705 */
707
708 /**
709 * Are the SEI recovery points looking valid.
710 */
712
714
715 /**
716 * recovery_frame is the frame_num at which the next frame should
717 * be fully constructed.
718 *
719 * Set to -1 when not expecting a recovery point.
720 */
722
723 /**
724 * We have seen an IDR, so all the following frames in coded order are correctly
725 * decodable.
726 */
727 #define FRAME_RECOVERED_IDR (1 << 0)
728 /**
729 * Sufficient number of frames have been decoded since a SEI recovery point,
730 * so all the following frames in presentation order are correct.
731 */
732 #define FRAME_RECOVERED_SEI (1 << 1)
733
735
737
739
742
743 // Timestamp stuff
746
749
751
757
762
763 /* Motion Estimation */
767
770
771 /**
772 * Decode SEI
773 */
775
776 /**
777 * Decode SPS
778 */
780
781 /**
782 * compute profile from sps
783 */
785
786 /**
787 * Decode PPS
788 */
790
791 /**
792 * Decode a network abstraction layer unit.
793 * @param consumed is the number of bytes used as input
794 * @param length is the length of the array
795 * @param dst_length is the number of decoded bytes FIXME here
796 * or a decode rbsp tailing?
797 * @return decoded bytes, might be src+1 if no escapes
798 */
800 int *dst_length,
int *consumed,
int length);
801
802 /**
803 * Free any data that may have been allocated in the H264 context
804 * like SPS, PPS etc.
805 */
807
808 /**
809 * Reconstruct bitstream slice_type.
810 */
812
813 /**
814 * Allocate tables.
815 * needs width/height
816 */
818
819 /**
820 * Fill the default_ref_list.
821 */
823
827
828 /**
829 * Execute the reference picture marking (memory management control operations).
830 */
832
834 int first_slice);
835
837
838 /**
839 * Check if the top & left blocks are available if needed & change the
840 * dc mode so it only uses the available blocks.
841 */
843
844 /**
845 * Check if the top & left blocks are available if needed & change the
846 * dc mode so it only uses the available blocks.
847 */
849
854
855 /**
856 * Decode a macroblock
857 * @return 0 if OK, ER_AC_ERROR / ER_DC_ERROR / ER_MV_ERROR on error
858 */
860
861 /**
862 * Decode a CABAC coded macroblock
863 * @return 0 if OK, ER_AC_ERROR / ER_DC_ERROR / ER_MV_ERROR on error
864 */
866
868
870
874
881
882 /**
883 * Reset SEI values at the beginning of the frame.
884 *
885 * @param h H.264 context.
886 */
888
889 /**
890 * Get stereo_mode string from the h264 frame_packing_arrangement
891 * @param h H.264 context.
892 */
894
895 #define COPY_PICTURE(dst, src) \
896 do {\
897 *(dst) = *(src);\
898 (dst)->f.extended_data = (dst)->f.data;\
899 (dst)->tf.f = &(dst)->f;\
900 } while (0)
901
902 /*
903 * o-o o-o
904 * / / /
905 * o-o o-o
906 * ,---'
907 * o-o o-o
908 * / / /
909 * o-o o-o
910 */
911
912 /* Scan8 organization:
913 * 0 1 2 3 4 5 6 7
914 * 0 DY y y y y y
915 * 1 y Y Y Y Y
916 * 2 y Y Y Y Y
917 * 3 y Y Y Y Y
918 * 4 y Y Y Y Y
919 * 5 DU u u u u u
920 * 6 u U U U U
921 * 7 u U U U U
922 * 8 u U U U U
923 * 9 u U U U U
924 * 10 DV v v v v v
925 * 11 v V V V V
926 * 12 v V V V V
927 * 13 v V V V V
928 * 14 v V V V V
929 * DY/DU/DV are for luma/chroma DC.
930 */
931
932 #define LUMA_DC_BLOCK_INDEX 48
933 #define CHROMA_DC_BLOCK_INDEX 49
934
935 // This table must be here because scan8[constant] must be known at compiletime
937 4 + 1 * 8, 5 + 1 * 8, 4 + 2 * 8, 5 + 2 * 8,
938 6 + 1 * 8, 7 + 1 * 8, 6 + 2 * 8, 7 + 2 * 8,
939 4 + 3 * 8, 5 + 3 * 8, 4 + 4 * 8, 5 + 4 * 8,
940 6 + 3 * 8, 7 + 3 * 8, 6 + 4 * 8, 7 + 4 * 8,
941 4 + 6 * 8, 5 + 6 * 8, 4 + 7 * 8, 5 + 7 * 8,
942 6 + 6 * 8, 7 + 6 * 8, 6 + 7 * 8, 7 + 7 * 8,
943 4 + 8 * 8, 5 + 8 * 8, 4 + 9 * 8, 5 + 9 * 8,
944 6 + 8 * 8, 7 + 8 * 8, 6 + 9 * 8, 7 + 9 * 8,
945 4 + 11 * 8, 5 + 11 * 8, 4 + 12 * 8, 5 + 12 * 8,
946 6 + 11 * 8, 7 + 11 * 8, 6 + 12 * 8, 7 + 12 * 8,
947 4 + 13 * 8, 5 + 13 * 8, 4 + 14 * 8, 5 + 14 * 8,
948 6 + 13 * 8, 7 + 13 * 8, 6 + 14 * 8, 7 + 14 * 8,
949 0 + 0 * 8, 0 + 5 * 8, 0 + 10 * 8
950 };
951
953 {
954 #if HAVE_BIGENDIAN
955 return (b & 0xFFFF) + (a << 16);
956 #else
957 return (a & 0xFFFF) + (b << 16);
958 #endif
959 }
960
962 {
963 #if HAVE_BIGENDIAN
964 return (b & 0xFF) + (a << 8);
965 #else
966 return (a & 0xFF) + (b << 8);
967 #endif
968 }
969
970 /**
971 * Get the chroma qp.
972 */
974 {
976 }
977
978 /**
979 * Get the predicted intra4x4 prediction mode.
980 */
982 {
983 const int index8 =
scan8[
n];
987
988 tprintf(h->
avctx,
"mode:%d %d min:%d\n", left, top, min);
989
990 if (min < 0)
992 else
994 }
995
997 {
1000
1001 AV_COPY32(i4x4, i4x4_cache + 4 + 8 * 4);
1002 i4x4[4] = i4x4_cache[7 + 8 * 3];
1003 i4x4[5] = i4x4_cache[7 + 8 * 2];
1004 i4x4[6] = i4x4_cache[7 + 8 * 1];
1005 }
1006
1008 {
1012
1013 AV_COPY32(&nnz[ 0], &nnz_cache[4 + 8 * 1]);
1014 AV_COPY32(&nnz[ 4], &nnz_cache[4 + 8 * 2]);
1015 AV_COPY32(&nnz[ 8], &nnz_cache[4 + 8 * 3]);
1016 AV_COPY32(&nnz[12], &nnz_cache[4 + 8 * 4]);
1017 AV_COPY32(&nnz[16], &nnz_cache[4 + 8 * 6]);
1018 AV_COPY32(&nnz[20], &nnz_cache[4 + 8 * 7]);
1019 AV_COPY32(&nnz[32], &nnz_cache[4 + 8 * 11]);
1020 AV_COPY32(&nnz[36], &nnz_cache[4 + 8 * 12]);
1021
1023 AV_COPY32(&nnz[24], &nnz_cache[4 + 8 * 8]);
1024 AV_COPY32(&nnz[28], &nnz_cache[4 + 8 * 9]);
1025 AV_COPY32(&nnz[40], &nnz_cache[4 + 8 * 13]);
1026 AV_COPY32(&nnz[44], &nnz_cache[4 + 8 * 14]);
1027 }
1028 }
1029
1032 int b_xy, int b8_xy,
1033 int mb_type, int list)
1034 {
1037 AV_COPY128(mv_dst + 0 * b_stride, mv_src + 8 * 0);
1038 AV_COPY128(mv_dst + 1 * b_stride, mv_src + 8 * 1);
1039 AV_COPY128(mv_dst + 2 * b_stride, mv_src + 8 * 2);
1040 AV_COPY128(mv_dst + 3 * b_stride, mv_src + 8 * 3);
1047 } else {
1049 AV_COPY16(mvd_dst + 3 + 3, mvd_src + 3 + 8 * 0);
1050 AV_COPY16(mvd_dst + 3 + 2, mvd_src + 3 + 8 * 1);
1051 AV_COPY16(mvd_dst + 3 + 1, mvd_src + 3 + 8 * 2);
1052 }
1053 }
1054
1055 {
1058 ref_index[0 + 0 * 2] = ref_cache[
scan8[0]];
1059 ref_index[1 + 0 * 2] = ref_cache[scan8[4]];
1060 ref_index[0 + 1 * 2] = ref_cache[scan8[8]];
1061 ref_index[1 + 1 * 2] = ref_cache[scan8[12]];
1062 }
1063 }
1064
1066 {
1068 const int b_xy = 4 * h->
mb_x + 4 * h->
mb_y * h->
b_stride;
// try mb2b(8)_xy
1069 const int b8_xy = 4 * h->
mb_xy;
1070
1073 } else {
1076 }
1079
1086 }
1087 }
1088 }
1089
1091 {
1095 0x0001000100010001ULL));
1096 else
1099 0x0001000100010001ULL));
1100 }
1101
1103 int buf_index, int next_avc)
1104 {
1105 uint32_t
state = -1;
1106
1108
1109 return FFMIN(buf_index, buf_size);
1110 }
1111
1113 int buf_size, int *buf_index)
1114 {
1115 int i, nalsize = 0;
1116
1118 return -1;
1119
1121 nalsize = ((unsigned)nalsize << 8) | buf[(*buf_index)++];
1122 if (nalsize <= 0 || nalsize > buf_size - *buf_index) {
1124 "AVC: nal size %d\n", nalsize);
1125 return -1;
1126 }
1127 return nalsize;
1128 }
1129
1131
1134
1137
1142
1144 #define SLICE_SINGLETHREAD 1
1145 #define SLICE_SKIPED 2
1146
1150
1152
1154
1156
1157 #endif /* AVCODEC_H264_H */