FFmpeg  1.2.4
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 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 
28 #ifndef AVCODEC_H264_H
29 #define AVCODEC_H264_H
30 
31 #include "libavutil/intreadwrite.h"
32 #include "cabac.h"
33 #include "get_bits.h"
34 #include "mpegvideo.h"
35 #include "h264chroma.h"
36 #include "h264dsp.h"
37 #include "h264pred.h"
38 #include "h264qpel.h"
39 #include "rectangle.h"
40 
41 #define MAX_SPS_COUNT 32
42 #define MAX_PPS_COUNT 256
43 
44 #define MAX_MMCO_COUNT 66
45 
46 #define MAX_DELAYED_PIC_COUNT 16
47 
48 #define MAX_MBPAIR_SIZE (256*1024) // a tighter bound could be calculated if someone cares about a few bytes
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 
60 #define MAX_SLICES 16
61 
62 #ifdef ALLOW_INTERLACE
63 #define MB_MBAFF h->mb_mbaff
64 #define MB_FIELD h->mb_field_decoding_flag
65 #define FRAME_MBAFF h->mb_aff_frame
66 #define FIELD_PICTURE (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 0
73 #define MB_FIELD 0
74 #define FRAME_MBAFF 0
75 #define FIELD_PICTURE 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 (FRAME_MBAFF || FIELD_PICTURE)
84 
85 #ifndef CABAC
86 #define CABAC h->pps.cabac
87 #endif
88 
89 #define CHROMA (h->sps.chroma_format_idc)
90 #define CHROMA422 (h->sps.chroma_format_idc == 2)
91 #define CHROMA444 (h->sps.chroma_format_idc == 3)
92 
93 #define EXTENDED_SAR 255
94 
95 #define MB_TYPE_REF0 MB_TYPE_ACPRED // dirty but it fits in 16 bit
96 #define MB_TYPE_8x8DCT 0x01000000
97 #define IS_REF0(a) ((a) & MB_TYPE_REF0)
98 #define IS_8x8DCT(a) ((a) & MB_TYPE_8x8DCT)
99 
100 #define QP_MAX_NUM (51 + 6*6) // The maximum supported qp
101 
102 /* NAL unit types */
103 enum {
118  NAL_FF_IGNORE = 0xff0f001,
119 };
120 
124 typedef enum {
130 } SEI_Type;
131 
135 typedef enum {
146 
150 typedef struct SPS {
156  int poc_type;
164  int mb_width;
165  int mb_height;
167  int mb_aff;
169  int crop;
170  unsigned int crop_left;
171  unsigned int crop_right;
172  unsigned int crop_top;
173  unsigned int crop_bottom;
184  uint32_t time_scale;
186  short offset_for_ref_frame[256]; // FIXME dyn aloc?
196  int cpb_cnt;
204  int new;
205 } SPS;
206 
210 typedef struct PPS {
211  unsigned int sps_id;
212  int cabac;
216  unsigned int ref_count[2];
219  int init_qp;
220  int init_qs;
230 } PPS;
231 
235 typedef enum MMCOOpcode {
236  MMCO_END = 0,
243 } MMCOOpcode;
244 
248 typedef struct MMCO {
251  int long_arg;
252 } MMCO;
253 
257 typedef struct H264Context {
268 
274 
276  int chroma_qp[2]; // QPc
277 
278  int qp_thresh;
279 
280  int width, height;
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 
356 
357  unsigned current_sps_id;
359 
363  PPS pps; // FIXME move to Picture perhaps? (->no) do we need that?
364 
365  uint32_t dequant4_buffer[6][QP_MAX_NUM + 1][16]; // FIXME should these be moved down?
366  uint32_t dequant8_buffer[6][QP_MAX_NUM + 1][64];
367  uint32_t(*dequant4_coeff[6])[16];
368  uint32_t(*dequant8_coeff[6])[64];
369 
371  uint16_t *slice_table;
375 
376  // interlacing specific flags
379  int mb_mbaff;
382 
383  DECLARE_ALIGNED(8, uint16_t, sub_mb_type)[4];
384 
385  // Weighted pred stuff
390  // The following 2 can be changed to int8_t but that causes 10cpu cycles speedloss
391  int luma_weight[48][2][2];
392  int chroma_weight[48][2][2][2];
393  int implicit_weight[48][48][2];
394 
400  int map_col_to_list0[2][16 + 32];
401  int map_col_to_list0_field[2][2][16 + 32];
402 
406  unsigned int ref_count[2];
407  unsigned int list_count;
409  Picture ref_list[2][48];
412  int ref2frm[MAX_SLICES][2][64];
413 
414  // data partitioning
419 
421  DECLARE_ALIGNED(16, int16_t, mb)[16 * 48 * 2];
422  DECLARE_ALIGNED(16, int16_t, mb_luma_dc)[3][16 * 2];
423  int16_t mb_padding[256 * 2];
424 
430 
431  /* 0x100 -> non null luma_dc, 0x80/0x40 -> non null chroma_dc (cb/cr), 0x?0 -> chroma_cbp(0, 1, 2), 0x0? luma_cbp */
432  uint16_t *cbp_table;
433  int cbp;
434  int top_cbp;
435  int left_cbp;
436  /* chroma_pred_mode for i4x4 or i16x16, else 0 */
439  uint8_t (*mvd_table[2])[2];
440  DECLARE_ALIGNED(16, uint8_t, mvd_cache)[2][5 * 8][2];
443 
456 
458 
459  int mb_x, mb_y;
465  int mb_num;
466  int mb_xy;
467 
469 
470  // deblock
474 
475  // =============================================================
476  // Things below are not used in the MB or more inner code
477 
481  unsigned int rbsp_buffer_size[2];
482 
486  int is_avc;
488  int got_first;
489 
492 
495 
497 
498  uint16_t *slice_table_base;
499 
500  // POC stuff
501  int poc_lsb;
502  int poc_msb;
504  int delta_poc[2];
511 
516 
521 
523 
532 
539 
542 
544 
550 
555 
563 
565 
571 
573 
575  unsigned int last_ref_count[2];
582 
590 
597 
602 
607 
623 
628 
631 
632  // Timestamp stuff
635 
638 
640 
641  int sync;
642 
647  int16_t *dc_val_base;
648 
650 } H264Context;
651 
652 extern const uint8_t ff_h264_chroma_qp[7][QP_MAX_NUM + 1];
653 extern const uint16_t ff_h264_mb_sizes[4];
654 
659 
664 
669 
673 int ff_h264_decode_picture_parameter_set(H264Context *h, int bit_length);
674 
683 const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src,
684  int *dst_length, int *consumed, int length);
685 
691 
695 int ff_h264_get_slice_type(const H264Context *h);
696 
702 
707 
711 
715 int ff_h264_execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count);
716 
718  int first_slice);
719 
720 int ff_generate_sliding_window_mmcos(H264Context *h, int first_slice);
721 
727 
732 int ff_h264_check_intra_pred_mode(H264Context *h, int mode, int is_chroma);
733 
736 int ff_h264_decode_extradata(H264Context *h, const uint8_t *buf, int size);
738 void ff_h264_decode_init_vlc(void);
739 
745 
751 
753 
756 void ff_h264_pred_direct_motion(H264Context *const h, int *mb_type);
757 
758 void ff_h264_filter_mb_fast(H264Context *h, int mb_x, int mb_y,
759  uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr,
760  unsigned int linesize, unsigned int uvlinesize);
761 void ff_h264_filter_mb(H264Context *h, int mb_x, int mb_y,
762  uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr,
763  unsigned int linesize, unsigned int uvlinesize);
764 
771 
772 /*
773  * o-o o-o
774  * / / /
775  * o-o o-o
776  * ,---'
777  * o-o o-o
778  * / / /
779  * o-o o-o
780  */
781 
782 /* Scan8 organization:
783  * 0 1 2 3 4 5 6 7
784  * 0 DY y y y y y
785  * 1 y Y Y Y Y
786  * 2 y Y Y Y Y
787  * 3 y Y Y Y Y
788  * 4 y Y Y Y Y
789  * 5 DU u u u u u
790  * 6 u U U U U
791  * 7 u U U U U
792  * 8 u U U U U
793  * 9 u U U U U
794  * 10 DV v v v v v
795  * 11 v V V V V
796  * 12 v V V V V
797  * 13 v V V V V
798  * 14 v V V V V
799  * DY/DU/DV are for luma/chroma DC.
800  */
801 
802 #define LUMA_DC_BLOCK_INDEX 48
803 #define CHROMA_DC_BLOCK_INDEX 49
804 
805 // This table must be here because scan8[constant] must be known at compiletime
806 static const uint8_t scan8[16 * 3 + 3] = {
807  4 + 1 * 8, 5 + 1 * 8, 4 + 2 * 8, 5 + 2 * 8,
808  6 + 1 * 8, 7 + 1 * 8, 6 + 2 * 8, 7 + 2 * 8,
809  4 + 3 * 8, 5 + 3 * 8, 4 + 4 * 8, 5 + 4 * 8,
810  6 + 3 * 8, 7 + 3 * 8, 6 + 4 * 8, 7 + 4 * 8,
811  4 + 6 * 8, 5 + 6 * 8, 4 + 7 * 8, 5 + 7 * 8,
812  6 + 6 * 8, 7 + 6 * 8, 6 + 7 * 8, 7 + 7 * 8,
813  4 + 8 * 8, 5 + 8 * 8, 4 + 9 * 8, 5 + 9 * 8,
814  6 + 8 * 8, 7 + 8 * 8, 6 + 9 * 8, 7 + 9 * 8,
815  4 + 11 * 8, 5 + 11 * 8, 4 + 12 * 8, 5 + 12 * 8,
816  6 + 11 * 8, 7 + 11 * 8, 6 + 12 * 8, 7 + 12 * 8,
817  4 + 13 * 8, 5 + 13 * 8, 4 + 14 * 8, 5 + 14 * 8,
818  6 + 13 * 8, 7 + 13 * 8, 6 + 14 * 8, 7 + 14 * 8,
819  0 + 0 * 8, 0 + 5 * 8, 0 + 10 * 8
820 };
821 
822 static av_always_inline uint32_t pack16to32(int a, int b)
823 {
824 #if HAVE_BIGENDIAN
825  return (b & 0xFFFF) + (a << 16);
826 #else
827  return (a & 0xFFFF) + (b << 16);
828 #endif
829 }
830 
831 static av_always_inline uint16_t pack8to16(int a, int b)
832 {
833 #if HAVE_BIGENDIAN
834  return (b & 0xFF) + (a << 8);
835 #else
836  return (a & 0xFF) + (b << 8);
837 #endif
838 }
839 
844 {
845  return h->pps.chroma_qp_table[t][qscale];
846 }
847 
852 {
853  const int index8 = scan8[n];
854  const int left = h->intra4x4_pred_mode_cache[index8 - 1];
855  const int top = h->intra4x4_pred_mode_cache[index8 - 8];
856  const int min = FFMIN(left, top);
857 
858  tprintf(h->avctx, "mode:%d %d min:%d\n", left, top, min);
859 
860  if (min < 0)
861  return DC_PRED;
862  else
863  return min;
864 }
865 
867 {
868  int8_t *i4x4 = h->intra4x4_pred_mode + h->mb2br_xy[h->mb_xy];
869  int8_t *i4x4_cache = h->intra4x4_pred_mode_cache;
870 
871  AV_COPY32(i4x4, i4x4_cache + 4 + 8 * 4);
872  i4x4[4] = i4x4_cache[7 + 8 * 3];
873  i4x4[5] = i4x4_cache[7 + 8 * 2];
874  i4x4[6] = i4x4_cache[7 + 8 * 1];
875 }
876 
878 {
879  const int mb_xy = h->mb_xy;
880  uint8_t *nnz = h->non_zero_count[mb_xy];
881  uint8_t *nnz_cache = h->non_zero_count_cache;
882 
883  AV_COPY32(&nnz[ 0], &nnz_cache[4 + 8 * 1]);
884  AV_COPY32(&nnz[ 4], &nnz_cache[4 + 8 * 2]);
885  AV_COPY32(&nnz[ 8], &nnz_cache[4 + 8 * 3]);
886  AV_COPY32(&nnz[12], &nnz_cache[4 + 8 * 4]);
887  AV_COPY32(&nnz[16], &nnz_cache[4 + 8 * 6]);
888  AV_COPY32(&nnz[20], &nnz_cache[4 + 8 * 7]);
889  AV_COPY32(&nnz[32], &nnz_cache[4 + 8 * 11]);
890  AV_COPY32(&nnz[36], &nnz_cache[4 + 8 * 12]);
891 
892  if (!h->chroma_y_shift) {
893  AV_COPY32(&nnz[24], &nnz_cache[4 + 8 * 8]);
894  AV_COPY32(&nnz[28], &nnz_cache[4 + 8 * 9]);
895  AV_COPY32(&nnz[40], &nnz_cache[4 + 8 * 13]);
896  AV_COPY32(&nnz[44], &nnz_cache[4 + 8 * 14]);
897  }
898 }
899 
901  int b_stride,
902  int b_xy, int b8_xy,
903  int mb_type, int list)
904 {
905  int16_t(*mv_dst)[2] = &h->cur_pic.f.motion_val[list][b_xy];
906  int16_t(*mv_src)[2] = &h->mv_cache[list][scan8[0]];
907  AV_COPY128(mv_dst + 0 * b_stride, mv_src + 8 * 0);
908  AV_COPY128(mv_dst + 1 * b_stride, mv_src + 8 * 1);
909  AV_COPY128(mv_dst + 2 * b_stride, mv_src + 8 * 2);
910  AV_COPY128(mv_dst + 3 * b_stride, mv_src + 8 * 3);
911  if (CABAC) {
912  uint8_t (*mvd_dst)[2] = &h->mvd_table[list][FMO ? 8 * h->mb_xy
913  : h->mb2br_xy[h->mb_xy]];
914  uint8_t(*mvd_src)[2] = &h->mvd_cache[list][scan8[0]];
915  if (IS_SKIP(mb_type)) {
916  AV_ZERO128(mvd_dst);
917  } else {
918  AV_COPY64(mvd_dst, mvd_src + 8 * 3);
919  AV_COPY16(mvd_dst + 3 + 3, mvd_src + 3 + 8 * 0);
920  AV_COPY16(mvd_dst + 3 + 2, mvd_src + 3 + 8 * 1);
921  AV_COPY16(mvd_dst + 3 + 1, mvd_src + 3 + 8 * 2);
922  }
923  }
924 
925  {
926  int8_t *ref_index = &h->cur_pic.f.ref_index[list][b8_xy];
927  int8_t *ref_cache = h->ref_cache[list];
928  ref_index[0 + 0 * 2] = ref_cache[scan8[0]];
929  ref_index[1 + 0 * 2] = ref_cache[scan8[4]];
930  ref_index[0 + 1 * 2] = ref_cache[scan8[8]];
931  ref_index[1 + 1 * 2] = ref_cache[scan8[12]];
932  }
933 }
934 
935 static av_always_inline void write_back_motion(H264Context *h, int mb_type)
936 {
937  const int b_stride = h->b_stride;
938  const int b_xy = 4 * h->mb_x + 4 * h->mb_y * h->b_stride; // try mb2b(8)_xy
939  const int b8_xy = 4 * h->mb_xy;
940 
941  if (USES_LIST(mb_type, 0)) {
942  write_back_motion_list(h, b_stride, b_xy, b8_xy, mb_type, 0);
943  } else {
944  fill_rectangle(&h->cur_pic.f.ref_index[0][b8_xy],
945  2, 2, 2, (uint8_t)LIST_NOT_USED, 1);
946  }
947  if (USES_LIST(mb_type, 1))
948  write_back_motion_list(h, b_stride, b_xy, b8_xy, mb_type, 1);
949 
950  if (h->slice_type_nos == AV_PICTURE_TYPE_B && CABAC) {
951  if (IS_8X8(mb_type)) {
952  uint8_t *direct_table = &h->direct_table[4 * h->mb_xy];
953  direct_table[1] = h->sub_mb_type[1] >> 1;
954  direct_table[2] = h->sub_mb_type[2] >> 1;
955  direct_table[3] = h->sub_mb_type[3] >> 1;
956  }
957  }
958 }
959 
961 {
963  return !(AV_RN64A(h->sub_mb_type) &
965  0x0001000100010001ULL));
966  else
967  return !(AV_RN64A(h->sub_mb_type) &
969  0x0001000100010001ULL));
970 }
971 
972 void ff_h264_draw_horiz_band(H264Context *h, int y, int height);
973 
974 #endif /* AVCODEC_H264_H */