Libav
h264.h
Go to the documentation of this file.
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 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 
28 #ifndef AVCODEC_H264_H
29 #define AVCODEC_H264_H
30 
31 #include "libavutil/intreadwrite.h"
32 #include "cabac.h"
33 #include "error_resilience.h"
34 #include "get_bits.h"
35 #include "mpegvideo.h"
36 #include "h264chroma.h"
37 #include "h264dsp.h"
38 #include "h264pred.h"
39 #include "h264qpel.h"
40 #include "rectangle.h"
41 
42 #define MAX_SPS_COUNT 32
43 #define MAX_PPS_COUNT 256
44 
45 #define MAX_MMCO_COUNT 66
46 
47 #define MAX_DELAYED_PIC_COUNT 16
48 
49 /* Compiling in interlaced support reduces the speed
50  * of progressive decoding by about 2%. */
51 #define ALLOW_INTERLACE
52 
53 #define FMO 0
54 
59 #define MAX_SLICES 16
60 
61 #ifdef ALLOW_INTERLACE
62 #define MB_MBAFF(h) h->mb_mbaff
63 #define MB_FIELD(h) h->mb_field_decoding_flag
64 #define FRAME_MBAFF(h) h->mb_aff_frame
65 #define FIELD_PICTURE(h) (h->picture_structure != PICT_FRAME)
66 #define LEFT_MBS 2
67 #define LTOP 0
68 #define LBOT 1
69 #define LEFT(i) (i)
70 #else
71 #define MB_MBAFF(h) 0
72 #define MB_FIELD(h) 0
73 #define FRAME_MBAFF(h) 0
74 #define FIELD_PICTURE(h) 0
75 #undef IS_INTERLACED
76 #define IS_INTERLACED(mb_type) 0
77 #define LEFT_MBS 1
78 #define LTOP 0
79 #define LBOT 0
80 #define LEFT(i) 0
81 #endif
82 #define FIELD_OR_MBAFF_PICTURE(h) (FRAME_MBAFF(h) || FIELD_PICTURE(h))
83 
84 #ifndef CABAC
85 #define CABAC(h) h->pps.cabac
86 #endif
87 
88 #define CHROMA422(h) (h->sps.chroma_format_idc == 2)
89 #define CHROMA444(h) (h->sps.chroma_format_idc == 3)
90 
91 #define EXTENDED_SAR 255
92 
93 #define MB_TYPE_REF0 MB_TYPE_ACPRED // dirty but it fits in 16 bit
94 #define MB_TYPE_8x8DCT 0x01000000
95 #define IS_REF0(a) ((a) & MB_TYPE_REF0)
96 #define IS_8x8DCT(a) ((a) & MB_TYPE_8x8DCT)
97 
98 #define QP_MAX_NUM (51 + 2 * 6) // The maximum supported qp
99 
100 /* NAL unit types */
101 enum {
103  NAL_DPA = 2,
104  NAL_DPB = 3,
105  NAL_DPC = 4,
107  NAL_SEI = 6,
108  NAL_SPS = 7,
109  NAL_PPS = 8,
110  NAL_AUD = 9,
116  NAL_FF_IGNORE = 0xff0f001,
117 };
118 
122 typedef enum {
128 } SEI_Type;
129 
133 typedef enum {
144 
148 typedef struct SPS {
149  unsigned int sps_id;
155  int poc_type;
163  int mb_width;
164  int mb_height;
166  int mb_aff;
168  int crop;
169 
170  /* those 4 are already in luma samples */
171  unsigned int crop_left;
172  unsigned int crop_right;
173  unsigned int crop_top;
174  unsigned int crop_bottom;
185  uint32_t time_scale;
187  short offset_for_ref_frame[256]; // FIXME dyn aloc?
197  int cpb_cnt;
205  int new;
206 } SPS;
207 
211 typedef struct PPS {
212  unsigned int sps_id;
213  int cabac;
217  unsigned int ref_count[2];
220  int init_qp;
221  int init_qs;
231 } PPS;
232 
236 typedef enum MMCOOpcode {
237  MMCO_END = 0,
244 } MMCOOpcode;
245 
249 typedef struct MMCO {
252  int long_arg;
253 } MMCO;
254 
258 typedef struct H264Context {
269 
273 
275  int chroma_qp[2]; // QPc
276 
277  int qp_thresh;
278 
279  /* coded dimensions -- 16 * mb w/h */
280  int width, height;
281  ptrdiff_t linesize, uvlinesize;
283 
284  int qscale;
289 
291  int flags;
293 
296 
297  // prediction stuff
300 
305 
307  int top_type;
310 
313 
318  unsigned int top_samples_available;
321  uint8_t (*top_borders[2])[(16 * 3) * 2];
322 
328 
330 
334  DECLARE_ALIGNED(16, int16_t, mv_cache)[2][5 * 8][2];
335  DECLARE_ALIGNED(8, int8_t, ref_cache)[2][5 * 8];
336 #define LIST_NOT_USED -1 // FIXME rename?
337 #define PART_NOT_AVAILABLE -2
338 
343 
348  int block_offset[2 * (16 * 3)];
349 
350  uint32_t *mb2b_xy; // FIXME are these 4 a good idea?
351  uint32_t *mb2br_xy;
352  int b_stride; // FIXME use s->b4_stride
353 
354  ptrdiff_t mb_linesize;
355  ptrdiff_t mb_uvlinesize;
356 
359 
360  uint32_t dequant4_buffer[6][QP_MAX_NUM + 1][16]; // FIXME should these be moved down?
361  uint32_t dequant8_buffer[6][QP_MAX_NUM + 1][64];
362  uint32_t(*dequant4_coeff[6])[16];
363  uint32_t(*dequant8_coeff[6])[64];
364 
366  uint16_t *slice_table;
370 
371  // interlacing specific flags
374  int mb_mbaff;
377 
378  DECLARE_ALIGNED(8, uint16_t, sub_mb_type)[4];
379 
380  // Weighted pred stuff
385  // The following 2 can be changed to int8_t but that causes 10cpu cycles speedloss
386  int luma_weight[48][2][2];
387  int chroma_weight[48][2][2][2];
388  int implicit_weight[48][48][2];
389 
395  int map_col_to_list0[2][16 + 32];
396  int map_col_to_list0_field[2][2][16 + 32];
397 
401  unsigned int ref_count[2];
402  unsigned int list_count;
404  Picture ref_list[2][48];
407  int ref2frm[MAX_SLICES][2][64];
408 
409  // data partitioning
414 
416  DECLARE_ALIGNED(16, int16_t, mb)[16 * 48 * 2];
417  DECLARE_ALIGNED(16, int16_t, mb_luma_dc)[3][16 * 2];
418  int16_t mb_padding[256 * 2];
419 
425 
426  /* 0x100 -> non null luma_dc, 0x80/0x40 -> non null chroma_dc (cb/cr), 0x?0 -> chroma_cbp(0, 1, 2), 0x0? luma_cbp */
427  uint16_t *cbp_table;
428  int cbp;
429  int top_cbp;
430  int left_cbp;
431  /* chroma_pred_mode for i4x4 or i16x16, else 0 */
434  uint8_t (*mvd_table[2])[2];
435  DECLARE_ALIGNED(16, uint8_t, mvd_cache)[2][5 * 8][2];
438 
451 
453 
454  int mb_x, mb_y;
460  int mb_num;
461  int mb_xy;
462 
464 
465  // deblock
469 
470  // =============================================================
471  // Things below are not used in the MB or more inner code
472 
476  unsigned int rbsp_buffer_size[2];
477 
481  int is_avc;
483  int got_first;
484 
487 
490 
492 
493  uint16_t *slice_table_base;
494 
495  // POC stuff
496  int poc_lsb;
497  int poc_msb;
499  int delta_poc[2];
506 
511 
516 
518 
527 
534 
537 
539 
545 
550 
558 
560 
566 
568 
576 
584 
592 
599 
604 
609 
618 
626 
631 #define FRAME_RECOVERED_IDR (1 << 0)
632 
636 #define FRAME_RECOVERED_SEI (1 << 1)
637 
639 
642 
643  // Timestamp stuff
646 
650  int16_t *dc_val_base;
651 
656 } H264Context;
657 
658 extern const uint8_t ff_h264_chroma_qp[3][QP_MAX_NUM + 1];
659 extern const uint16_t ff_h264_mb_sizes[4];
660 
665 
670 
675 
679 int ff_h264_decode_picture_parameter_set(H264Context *h, int bit_length);
680 
689 const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src,
690  int *dst_length, int *consumed, int length);
691 
697 
701 int ff_h264_get_slice_type(const H264Context *h);
702 
708 
713 
717 
721 int ff_h264_execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count);
722 
724  int first_slice);
725 
726 int ff_generate_sliding_window_mmcos(H264Context *h, int first_slice);
727 
733 
738 int ff_h264_check_intra_pred_mode(H264Context *h, int mode, int is_chroma);
739 
743 void ff_h264_decode_init_vlc(void);
744 
750 
756 
758 
761 void ff_h264_pred_direct_motion(H264Context *const h, int *mb_type);
762 
763 void ff_h264_filter_mb_fast(H264Context *h, int mb_x, int mb_y,
764  uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr,
765  unsigned int linesize, unsigned int uvlinesize);
766 void ff_h264_filter_mb(H264Context *h, int mb_x, int mb_y,
767  uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr,
768  unsigned int linesize, unsigned int uvlinesize);
769 
776 
777 /*
778  * o-o o-o
779  * / / /
780  * o-o o-o
781  * ,---'
782  * o-o o-o
783  * / / /
784  * o-o o-o
785  */
786 
787 /* Scan8 organization:
788  * 0 1 2 3 4 5 6 7
789  * 0 DY y y y y y
790  * 1 y Y Y Y Y
791  * 2 y Y Y Y Y
792  * 3 y Y Y Y Y
793  * 4 y Y Y Y Y
794  * 5 DU u u u u u
795  * 6 u U U U U
796  * 7 u U U U U
797  * 8 u U U U U
798  * 9 u U U U U
799  * 10 DV v v v v v
800  * 11 v V V V V
801  * 12 v V V V V
802  * 13 v V V V V
803  * 14 v V V V V
804  * DY/DU/DV are for luma/chroma DC.
805  */
806 
807 #define LUMA_DC_BLOCK_INDEX 48
808 #define CHROMA_DC_BLOCK_INDEX 49
809 
810 // This table must be here because scan8[constant] must be known at compiletime
811 static const uint8_t scan8[16 * 3 + 3] = {
812  4 + 1 * 8, 5 + 1 * 8, 4 + 2 * 8, 5 + 2 * 8,
813  6 + 1 * 8, 7 + 1 * 8, 6 + 2 * 8, 7 + 2 * 8,
814  4 + 3 * 8, 5 + 3 * 8, 4 + 4 * 8, 5 + 4 * 8,
815  6 + 3 * 8, 7 + 3 * 8, 6 + 4 * 8, 7 + 4 * 8,
816  4 + 6 * 8, 5 + 6 * 8, 4 + 7 * 8, 5 + 7 * 8,
817  6 + 6 * 8, 7 + 6 * 8, 6 + 7 * 8, 7 + 7 * 8,
818  4 + 8 * 8, 5 + 8 * 8, 4 + 9 * 8, 5 + 9 * 8,
819  6 + 8 * 8, 7 + 8 * 8, 6 + 9 * 8, 7 + 9 * 8,
820  4 + 11 * 8, 5 + 11 * 8, 4 + 12 * 8, 5 + 12 * 8,
821  6 + 11 * 8, 7 + 11 * 8, 6 + 12 * 8, 7 + 12 * 8,
822  4 + 13 * 8, 5 + 13 * 8, 4 + 14 * 8, 5 + 14 * 8,
823  6 + 13 * 8, 7 + 13 * 8, 6 + 14 * 8, 7 + 14 * 8,
824  0 + 0 * 8, 0 + 5 * 8, 0 + 10 * 8
825 };
826 
827 static av_always_inline uint32_t pack16to32(int a, int b)
828 {
829 #if HAVE_BIGENDIAN
830  return (b & 0xFFFF) + (a << 16);
831 #else
832  return (a & 0xFFFF) + (b << 16);
833 #endif
834 }
835 
836 static av_always_inline uint16_t pack8to16(int a, int b)
837 {
838 #if HAVE_BIGENDIAN
839  return (b & 0xFF) + (a << 8);
840 #else
841  return (a & 0xFF) + (b << 8);
842 #endif
843 }
844 
849 {
850  return h->pps.chroma_qp_table[t][qscale];
851 }
852 
857 {
858  const int index8 = scan8[n];
859  const int left = h->intra4x4_pred_mode_cache[index8 - 1];
860  const int top = h->intra4x4_pred_mode_cache[index8 - 8];
861  const int min = FFMIN(left, top);
862 
863  tprintf(h->avctx, "mode:%d %d min:%d\n", left, top, min);
864 
865  if (min < 0)
866  return DC_PRED;
867  else
868  return min;
869 }
870 
872 {
873  int8_t *i4x4 = h->intra4x4_pred_mode + h->mb2br_xy[h->mb_xy];
874  int8_t *i4x4_cache = h->intra4x4_pred_mode_cache;
875 
876  AV_COPY32(i4x4, i4x4_cache + 4 + 8 * 4);
877  i4x4[4] = i4x4_cache[7 + 8 * 3];
878  i4x4[5] = i4x4_cache[7 + 8 * 2];
879  i4x4[6] = i4x4_cache[7 + 8 * 1];
880 }
881 
883 {
884  const int mb_xy = h->mb_xy;
885  uint8_t *nnz = h->non_zero_count[mb_xy];
886  uint8_t *nnz_cache = h->non_zero_count_cache;
887 
888  AV_COPY32(&nnz[ 0], &nnz_cache[4 + 8 * 1]);
889  AV_COPY32(&nnz[ 4], &nnz_cache[4 + 8 * 2]);
890  AV_COPY32(&nnz[ 8], &nnz_cache[4 + 8 * 3]);
891  AV_COPY32(&nnz[12], &nnz_cache[4 + 8 * 4]);
892  AV_COPY32(&nnz[16], &nnz_cache[4 + 8 * 6]);
893  AV_COPY32(&nnz[20], &nnz_cache[4 + 8 * 7]);
894  AV_COPY32(&nnz[32], &nnz_cache[4 + 8 * 11]);
895  AV_COPY32(&nnz[36], &nnz_cache[4 + 8 * 12]);
896 
897  if (!h->chroma_y_shift) {
898  AV_COPY32(&nnz[24], &nnz_cache[4 + 8 * 8]);
899  AV_COPY32(&nnz[28], &nnz_cache[4 + 8 * 9]);
900  AV_COPY32(&nnz[40], &nnz_cache[4 + 8 * 13]);
901  AV_COPY32(&nnz[44], &nnz_cache[4 + 8 * 14]);
902  }
903 }
904 
906  int b_stride,
907  int b_xy, int b8_xy,
908  int mb_type, int list)
909 {
910  int16_t(*mv_dst)[2] = &h->cur_pic.motion_val[list][b_xy];
911  int16_t(*mv_src)[2] = &h->mv_cache[list][scan8[0]];
912  AV_COPY128(mv_dst + 0 * b_stride, mv_src + 8 * 0);
913  AV_COPY128(mv_dst + 1 * b_stride, mv_src + 8 * 1);
914  AV_COPY128(mv_dst + 2 * b_stride, mv_src + 8 * 2);
915  AV_COPY128(mv_dst + 3 * b_stride, mv_src + 8 * 3);
916  if (CABAC(h)) {
917  uint8_t (*mvd_dst)[2] = &h->mvd_table[list][FMO ? 8 * h->mb_xy
918  : h->mb2br_xy[h->mb_xy]];
919  uint8_t(*mvd_src)[2] = &h->mvd_cache[list][scan8[0]];
920  if (IS_SKIP(mb_type)) {
921  AV_ZERO128(mvd_dst);
922  } else {
923  AV_COPY64(mvd_dst, mvd_src + 8 * 3);
924  AV_COPY16(mvd_dst + 3 + 3, mvd_src + 3 + 8 * 0);
925  AV_COPY16(mvd_dst + 3 + 2, mvd_src + 3 + 8 * 1);
926  AV_COPY16(mvd_dst + 3 + 1, mvd_src + 3 + 8 * 2);
927  }
928  }
929 
930  {
931  int8_t *ref_index = &h->cur_pic.ref_index[list][b8_xy];
932  int8_t *ref_cache = h->ref_cache[list];
933  ref_index[0 + 0 * 2] = ref_cache[scan8[0]];
934  ref_index[1 + 0 * 2] = ref_cache[scan8[4]];
935  ref_index[0 + 1 * 2] = ref_cache[scan8[8]];
936  ref_index[1 + 1 * 2] = ref_cache[scan8[12]];
937  }
938 }
939 
940 static av_always_inline void write_back_motion(H264Context *h, int mb_type)
941 {
942  const int b_stride = h->b_stride;
943  const int b_xy = 4 * h->mb_x + 4 * h->mb_y * h->b_stride; // try mb2b(8)_xy
944  const int b8_xy = 4 * h->mb_xy;
945 
946  if (USES_LIST(mb_type, 0)) {
947  write_back_motion_list(h, b_stride, b_xy, b8_xy, mb_type, 0);
948  } else {
949  fill_rectangle(&h->cur_pic.ref_index[0][b8_xy],
950  2, 2, 2, (uint8_t)LIST_NOT_USED, 1);
951  }
952  if (USES_LIST(mb_type, 1))
953  write_back_motion_list(h, b_stride, b_xy, b8_xy, mb_type, 1);
954 
955  if (h->slice_type_nos == AV_PICTURE_TYPE_B && CABAC(h)) {
956  if (IS_8X8(mb_type)) {
957  uint8_t *direct_table = &h->direct_table[4 * h->mb_xy];
958  direct_table[1] = h->sub_mb_type[1] >> 1;
959  direct_table[2] = h->sub_mb_type[2] >> 1;
960  direct_table[3] = h->sub_mb_type[3] >> 1;
961  }
962  }
963 }
964 
966 {
968  return !(AV_RN64A(h->sub_mb_type) &
970  0x0001000100010001ULL));
971  else
972  return !(AV_RN64A(h->sub_mb_type) &
974  0x0001000100010001ULL));
975 }
976 
977 void ff_h264_draw_horiz_band(H264Context *h, int y, int height);
978 int ff_init_poc(H264Context *h, int pic_field_poc[2], int *pic_poc);
981 
982 #endif /* AVCODEC_H264_H */