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 "dsputil.h"
33 #include "cabac.h"
34 #include "mpegvideo.h"
35 #include "h264dsp.h"
36 #include "h264pred.h"
37 #include "rectangle.h"
38 
39 #define interlaced_dct interlaced_dct_is_a_bad_name
40 #define mb_intra mb_intra_is_not_initialized_see_mb_type
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->mb_mbaff
63 #define MB_FIELD h->mb_field_decoding_flag
64 #define FRAME_MBAFF h->mb_aff_frame
65 #define FIELD_PICTURE (s->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 0
72 #define MB_FIELD 0
73 #define FRAME_MBAFF 0
74 #define FIELD_PICTURE 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 (FRAME_MBAFF || FIELD_PICTURE)
83 
84 #ifndef CABAC
85 #define CABAC h->pps.cabac
86 #endif
87 
88 #define CHROMA422 (h->sps.chroma_format_idc == 2)
89 #define CHROMA444 (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 
102 #define DELAYED_PIC_REF 4
103 
104 #define QP_MAX_NUM (51 + 2*6) // The maximum supported qp
105 
106 /* NAL unit types */
107 enum {
122 };
123 
127 typedef enum {
132 } SEI_Type;
133 
137 typedef enum {
148 
152 typedef struct SPS{
153 
159  int poc_type;
167  int mb_width;
168  int mb_height;
170  int mb_aff;
172  int crop;
173  unsigned int crop_left;
174  unsigned int crop_right;
175  unsigned int crop_top;
176  unsigned int crop_bottom;
187  uint32_t time_scale;
189  short offset_for_ref_frame[256]; //FIXME dyn aloc?
193  uint8_t scaling_matrix4[6][16];
194  uint8_t scaling_matrix8[6][64];
199  int cpb_cnt;
207  int new;
208 }SPS;
209 
213 typedef struct PPS{
214  unsigned int sps_id;
215  int cabac;
219  unsigned int ref_count[2];
222  int init_qp;
223  int init_qs;
229  uint8_t scaling_matrix4[6][16];
230  uint8_t scaling_matrix8[6][64];
231  uint8_t chroma_qp_table[2][64];
233 }PPS;
234 
238 typedef enum MMCOOpcode{
246 } MMCOOpcode;
247 
251 typedef struct MMCO{
254  int long_arg;
255 } MMCO;
256 
260 typedef struct H264Context{
264  int chroma_qp[2]; //QPc
265 
266  int qp_thresh;
267 
270 
271  //prediction stuff
274 
279 
281  int top_type;
284 
285  const uint8_t * left_block;
287 
292  unsigned int top_samples_available;
295  uint8_t (*top_borders[2])[(16*3)*2];
296 
302 
303  uint8_t (*non_zero_count)[48];
304 
308  DECLARE_ALIGNED(16, int16_t, mv_cache)[2][5*8][2];
309  DECLARE_ALIGNED(8, int8_t, ref_cache)[2][5*8];
310 #define LIST_NOT_USED -1 //FIXME rename?
311 #define PART_NOT_AVAILABLE -2
312 
317 
322  int block_offset[2*(16*3)];
323 
324  uint32_t *mb2b_xy; //FIXME are these 4 a good idea?
325  uint32_t *mb2br_xy;
326  int b_stride; //FIXME use s->b4_stride
327 
330 
333 
334  unsigned current_sps_id;
336 
340  PPS pps; //FIXME move to Picture perhaps? (->no) do we need that?
341 
342  uint32_t dequant4_buffer[6][QP_MAX_NUM+1][16]; //FIXME should these be moved down?
343  uint32_t dequant8_buffer[6][QP_MAX_NUM+1][64];
344  uint32_t (*dequant4_coeff[6])[16];
345  uint32_t (*dequant8_coeff[6])[64];
346 
348  uint16_t *slice_table;
352 
355 
356  //interlacing specific flags
359  int mb_mbaff;
360 
361  DECLARE_ALIGNED(8, uint16_t, sub_mb_type)[4];
362 
363  //Weighted pred stuff
368  //The following 2 can be changed to int8_t but that causes 10cpu cycles speedloss
369  int luma_weight[48][2][2];
370  int chroma_weight[48][2][2][2];
371  int implicit_weight[48][48][2];
372 
378  int map_col_to_list0[2][16+32];
379  int map_col_to_list0_field[2][2][16+32];
380 
384  unsigned int ref_count[2];
385  unsigned int list_count;
386  uint8_t *list_counts;
387  Picture ref_list[2][48];
390  int ref2frm[MAX_SLICES][2][64];
391 
392  //data partitioning
397 
398  DECLARE_ALIGNED(16, DCTELEM, mb)[16*48*2];
401 
406  uint8_t cabac_state[1024];
407 
408  /* 0x100 -> non null luma_dc, 0x80/0x40 -> non null chroma_dc (cb/cr), 0x?0 -> chroma_cbp(0,1,2), 0x0? luma_cbp */
409  uint16_t *cbp_table;
410  int cbp;
411  int top_cbp;
412  int left_cbp;
413  /* chroma_pred_mode for i4x4 or i16x16, else 0 */
416  uint8_t (*mvd_table[2])[2];
417  DECLARE_ALIGNED(16, uint8_t, mvd_cache)[2][5*8][2];
418  uint8_t *direct_table;
419  uint8_t direct_cache[5*8];
420 
421  uint8_t zigzag_scan[16];
422  uint8_t zigzag_scan8x8[64];
423  uint8_t zigzag_scan8x8_cavlc[64];
424  uint8_t field_scan[16];
425  uint8_t field_scan8x8[64];
426  uint8_t field_scan8x8_cavlc[64];
427  const uint8_t *zigzag_scan_q0;
428  const uint8_t *zigzag_scan8x8_q0;
429  const uint8_t *zigzag_scan8x8_cavlc_q0;
430  const uint8_t *field_scan_q0;
431  const uint8_t *field_scan8x8_q0;
432  const uint8_t *field_scan8x8_cavlc_q0;
433 
435 
436  int mb_xy;
437 
439 
440  //deblock
444 
445 //=============================================================
446  //Things below are not used in the MB or more inner code
447 
450  uint8_t *rbsp_buffer[2];
451  unsigned int rbsp_buffer_size[2];
452 
456  int is_avc;
458  int got_first;
459 
462 
464 
465  uint16_t *slice_table_base;
466 
467 
468  //POC stuff
469  int poc_lsb;
470  int poc_msb;
472  int delta_poc[2];
479 
484 
489 
491 
500 
507 
510 
512 
518 
523 
531 
537 
545 
553 
560 
565 
570 
579 
582 
583  // Timestamp stuff
586 
588 }H264Context;
589 
590 
591 extern const uint8_t ff_h264_chroma_qp[3][QP_MAX_NUM+1];
592 
597 
602 
607 
611 int ff_h264_decode_picture_parameter_set(H264Context *h, int bit_length);
612 
620 const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src, int *dst_length, int *consumed, int length);
621 
626 
630 int ff_h264_get_slice_type(const H264Context *h);
631 
637 
642 
646 
650 int ff_h264_execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count);
651 
653 
655 
656 
661 
665 int ff_h264_check_intra_pred_mode(H264Context *h, int mode, int is_chroma);
666 
673 
679 
685 
687 
690 void ff_h264_pred_direct_motion(H264Context * const h, int *mb_type);
691 
692 void ff_h264_filter_mb_fast( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize);
693 void ff_h264_filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize);
694 
701 
702 
703 /*
704 o-o o-o
705  / / /
706 o-o o-o
707  ,---'
708 o-o o-o
709  / / /
710 o-o o-o
711 */
712 
713 /* Scan8 organization:
714  * 0 1 2 3 4 5 6 7
715  * 0 DY y y y y y
716  * 1 y Y Y Y Y
717  * 2 y Y Y Y Y
718  * 3 y Y Y Y Y
719  * 4 y Y Y Y Y
720  * 5 DU u u u u u
721  * 6 u U U U U
722  * 7 u U U U U
723  * 8 u U U U U
724  * 9 u U U U U
725  * 10 DV v v v v v
726  * 11 v V V V V
727  * 12 v V V V V
728  * 13 v V V V V
729  * 14 v V V V V
730  * DY/DU/DV are for luma/chroma DC.
731  */
732 
733 #define LUMA_DC_BLOCK_INDEX 48
734 #define CHROMA_DC_BLOCK_INDEX 49
735 
736 //This table must be here because scan8[constant] must be known at compiletime
737 static const uint8_t scan8[16*3 + 3]={
738  4+ 1*8, 5+ 1*8, 4+ 2*8, 5+ 2*8,
739  6+ 1*8, 7+ 1*8, 6+ 2*8, 7+ 2*8,
740  4+ 3*8, 5+ 3*8, 4+ 4*8, 5+ 4*8,
741  6+ 3*8, 7+ 3*8, 6+ 4*8, 7+ 4*8,
742  4+ 6*8, 5+ 6*8, 4+ 7*8, 5+ 7*8,
743  6+ 6*8, 7+ 6*8, 6+ 7*8, 7+ 7*8,
744  4+ 8*8, 5+ 8*8, 4+ 9*8, 5+ 9*8,
745  6+ 8*8, 7+ 8*8, 6+ 9*8, 7+ 9*8,
746  4+11*8, 5+11*8, 4+12*8, 5+12*8,
747  6+11*8, 7+11*8, 6+12*8, 7+12*8,
748  4+13*8, 5+13*8, 4+14*8, 5+14*8,
749  6+13*8, 7+13*8, 6+14*8, 7+14*8,
750  0+ 0*8, 0+ 5*8, 0+10*8
751 };
752 
753 static av_always_inline uint32_t pack16to32(int a, int b){
754 #if HAVE_BIGENDIAN
755  return (b&0xFFFF) + (a<<16);
756 #else
757  return (a&0xFFFF) + (b<<16);
758 #endif
759 }
760 
761 static av_always_inline uint16_t pack8to16(int a, int b){
762 #if HAVE_BIGENDIAN
763  return (b&0xFF) + (a<<8);
764 #else
765  return (a&0xFF) + (b<<8);
766 #endif
767 }
768 
772 static av_always_inline int get_chroma_qp(H264Context *h, int t, int qscale){
773  return h->pps.chroma_qp_table[t][qscale];
774 }
775 
780  const int index8= scan8[n];
781  const int left= h->intra4x4_pred_mode_cache[index8 - 1];
782  const int top = h->intra4x4_pred_mode_cache[index8 - 8];
783  const int min= FFMIN(left, top);
784 
785  tprintf(h->s.avctx, "mode:%d %d min:%d\n", left ,top, min);
786 
787  if(min<0) return DC_PRED;
788  else return min;
789 }
790 
792  int8_t *i4x4= h->intra4x4_pred_mode + h->mb2br_xy[h->mb_xy];
793  int8_t *i4x4_cache= h->intra4x4_pred_mode_cache;
794 
795  AV_COPY32(i4x4, i4x4_cache + 4 + 8*4);
796  i4x4[4]= i4x4_cache[7+8*3];
797  i4x4[5]= i4x4_cache[7+8*2];
798  i4x4[6]= i4x4_cache[7+8*1];
799 }
800 
802  const int mb_xy= h->mb_xy;
803  uint8_t *nnz = h->non_zero_count[mb_xy];
804  uint8_t *nnz_cache = h->non_zero_count_cache;
805 
806  AV_COPY32(&nnz[ 0], &nnz_cache[4+8* 1]);
807  AV_COPY32(&nnz[ 4], &nnz_cache[4+8* 2]);
808  AV_COPY32(&nnz[ 8], &nnz_cache[4+8* 3]);
809  AV_COPY32(&nnz[12], &nnz_cache[4+8* 4]);
810  AV_COPY32(&nnz[16], &nnz_cache[4+8* 6]);
811  AV_COPY32(&nnz[20], &nnz_cache[4+8* 7]);
812  AV_COPY32(&nnz[32], &nnz_cache[4+8*11]);
813  AV_COPY32(&nnz[36], &nnz_cache[4+8*12]);
814 
815  if(!h->s.chroma_y_shift){
816  AV_COPY32(&nnz[24], &nnz_cache[4+8* 8]);
817  AV_COPY32(&nnz[28], &nnz_cache[4+8* 9]);
818  AV_COPY32(&nnz[40], &nnz_cache[4+8*13]);
819  AV_COPY32(&nnz[44], &nnz_cache[4+8*14]);
820  }
821 }
822 
824  int b_xy, int b8_xy, int mb_type, int list )
825 {
826  int16_t (*mv_dst)[2] = &s->current_picture.f.motion_val[list][b_xy];
827  int16_t (*mv_src)[2] = &h->mv_cache[list][scan8[0]];
828  AV_COPY128(mv_dst + 0*b_stride, mv_src + 8*0);
829  AV_COPY128(mv_dst + 1*b_stride, mv_src + 8*1);
830  AV_COPY128(mv_dst + 2*b_stride, mv_src + 8*2);
831  AV_COPY128(mv_dst + 3*b_stride, mv_src + 8*3);
832  if( CABAC ) {
833  uint8_t (*mvd_dst)[2] = &h->mvd_table[list][FMO ? 8*h->mb_xy : h->mb2br_xy[h->mb_xy]];
834  uint8_t (*mvd_src)[2] = &h->mvd_cache[list][scan8[0]];
835  if(IS_SKIP(mb_type))
836  AV_ZERO128(mvd_dst);
837  else{
838  AV_COPY64(mvd_dst, mvd_src + 8*3);
839  AV_COPY16(mvd_dst + 3 + 3, mvd_src + 3 + 8*0);
840  AV_COPY16(mvd_dst + 3 + 2, mvd_src + 3 + 8*1);
841  AV_COPY16(mvd_dst + 3 + 1, mvd_src + 3 + 8*2);
842  }
843  }
844 
845  {
846  int8_t *ref_index = &s->current_picture.f.ref_index[list][b8_xy];
847  int8_t *ref_cache = h->ref_cache[list];
848  ref_index[0+0*2]= ref_cache[scan8[0]];
849  ref_index[1+0*2]= ref_cache[scan8[4]];
850  ref_index[0+1*2]= ref_cache[scan8[8]];
851  ref_index[1+1*2]= ref_cache[scan8[12]];
852  }
853 }
854 
855 static av_always_inline void write_back_motion(H264Context *h, int mb_type){
856  MpegEncContext * const s = &h->s;
857  const int b_stride = h->b_stride;
858  const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride; //try mb2b(8)_xy
859  const int b8_xy= 4*h->mb_xy;
860 
861  if(USES_LIST(mb_type, 0)){
862  write_back_motion_list(h, s, b_stride, b_xy, b8_xy, mb_type, 0);
863  }else{
865  2, 2, 2, (uint8_t)LIST_NOT_USED, 1);
866  }
867  if(USES_LIST(mb_type, 1)){
868  write_back_motion_list(h, s, b_stride, b_xy, b8_xy, mb_type, 1);
869  }
870 
872  if(IS_8X8(mb_type)){
873  uint8_t *direct_table = &h->direct_table[4*h->mb_xy];
874  direct_table[1] = h->sub_mb_type[1]>>1;
875  direct_table[2] = h->sub_mb_type[2]>>1;
876  direct_table[3] = h->sub_mb_type[3]>>1;
877  }
878  }
879 }
880 
883  return !(AV_RN64A(h->sub_mb_type) & ((MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_8x8 )*0x0001000100010001ULL));
884  else
885  return !(AV_RN64A(h->sub_mb_type) & ((MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_8x8|MB_TYPE_DIRECT2)*0x0001000100010001ULL));
886 }
887 
888 #endif /* AVCODEC_H264_H */