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 }SPS;
208 
212 typedef struct PPS{
213  unsigned int sps_id;
214  int cabac;
218  unsigned int ref_count[2];
221  int init_qp;
222  int init_qs;
228  uint8_t scaling_matrix4[6][16];
229  uint8_t scaling_matrix8[6][64];
230  uint8_t chroma_qp_table[2][64];
232 }PPS;
233 
237 typedef enum MMCOOpcode{
245 } MMCOOpcode;
246 
250 typedef struct MMCO{
253  int long_arg;
254 } MMCO;
255 
259 typedef struct H264Context{
263  int chroma_qp[2]; //QPc
264 
265  int qp_thresh;
266 
269 
270  //prediction stuff
273 
278 
280  int top_type;
283 
284  const uint8_t * left_block;
286 
291  unsigned int top_samples_available;
294  uint8_t (*top_borders[2])[(16*3)*2];
295 
301 
302  uint8_t (*non_zero_count)[48];
303 
307  DECLARE_ALIGNED(16, int16_t, mv_cache)[2][5*8][2];
308  DECLARE_ALIGNED(8, int8_t, ref_cache)[2][5*8];
309 #define LIST_NOT_USED -1 //FIXME rename?
310 #define PART_NOT_AVAILABLE -2
311 
316 
321  int block_offset[2*(16*3)];
322 
323  uint32_t *mb2b_xy; //FIXME are these 4 a good idea?
324  uint32_t *mb2br_xy;
325  int b_stride; //FIXME use s->b4_stride
326 
329 
332 
334 
338  PPS pps; //FIXME move to Picture perhaps? (->no) do we need that?
339 
340  uint32_t dequant4_buffer[6][QP_MAX_NUM+1][16]; //FIXME should these be moved down?
341  uint32_t dequant8_buffer[6][QP_MAX_NUM+1][64];
342  uint32_t (*dequant4_coeff[6])[16];
343  uint32_t (*dequant8_coeff[6])[64];
344 
346  uint16_t *slice_table;
350 
351  //interlacing specific flags
354  int mb_mbaff;
355 
356  DECLARE_ALIGNED(8, uint16_t, sub_mb_type)[4];
357 
358  //Weighted pred stuff
363  //The following 2 can be changed to int8_t but that causes 10cpu cycles speedloss
364  int luma_weight[48][2][2];
365  int chroma_weight[48][2][2][2];
366  int implicit_weight[48][48][2];
367 
373  int map_col_to_list0[2][16+32];
374  int map_col_to_list0_field[2][2][16+32];
375 
379  unsigned int ref_count[2];
380  unsigned int list_count;
381  uint8_t *list_counts;
382  Picture ref_list[2][48];
385  int ref2frm[MAX_SLICES][2][64];
386 
387  //data partitioning
392 
393  DECLARE_ALIGNED(16, DCTELEM, mb)[16*48*2];
396 
401  uint8_t cabac_state[1024];
402 
403  /* 0x100 -> non null luma_dc, 0x80/0x40 -> non null chroma_dc (cb/cr), 0x?0 -> chroma_cbp(0,1,2), 0x0? luma_cbp */
404  uint16_t *cbp_table;
405  int cbp;
406  int top_cbp;
407  int left_cbp;
408  /* chroma_pred_mode for i4x4 or i16x16, else 0 */
411  uint8_t (*mvd_table[2])[2];
412  DECLARE_ALIGNED(16, uint8_t, mvd_cache)[2][5*8][2];
413  uint8_t *direct_table;
414  uint8_t direct_cache[5*8];
415 
416  uint8_t zigzag_scan[16];
417  uint8_t zigzag_scan8x8[64];
418  uint8_t zigzag_scan8x8_cavlc[64];
419  uint8_t field_scan[16];
420  uint8_t field_scan8x8[64];
421  uint8_t field_scan8x8_cavlc[64];
422  const uint8_t *zigzag_scan_q0;
423  const uint8_t *zigzag_scan8x8_q0;
424  const uint8_t *zigzag_scan8x8_cavlc_q0;
425  const uint8_t *field_scan_q0;
426  const uint8_t *field_scan8x8_q0;
427  const uint8_t *field_scan8x8_cavlc_q0;
428 
430 
431  int mb_xy;
432 
434 
435  //deblock
439 
440 //=============================================================
441  //Things below are not used in the MB or more inner code
442 
445  uint8_t *rbsp_buffer[2];
446  unsigned int rbsp_buffer_size[2];
447 
451  int is_avc;
453  int got_first;
454 
457 
459 
460  uint16_t *slice_table_base;
461 
462 
463  //POC stuff
464  int poc_lsb;
465  int poc_msb;
467  int delta_poc[2];
474 
479 
484 
486 
495 
502 
505 
507 
513 
518 
526 
532 
540 
548 
555 
560 
565 
574 
577 
578  // Timestamp stuff
581 
583 }H264Context;
584 
585 
586 extern const uint8_t ff_h264_chroma_qp[3][QP_MAX_NUM+1];
587 
592 
597 
602 
606 int ff_h264_decode_picture_parameter_set(H264Context *h, int bit_length);
607 
615 const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src, int *dst_length, int *consumed, int length);
616 
621 
625 int ff_h264_get_slice_type(const H264Context *h);
626 
632 
637 
641 
645 int ff_h264_execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count);
646 
648 
650 
651 
656 
660 int ff_h264_check_intra_pred_mode(H264Context *h, int mode, int is_chroma);
661 
668 
674 
680 
682 
685 void ff_h264_pred_direct_motion(H264Context * const h, int *mb_type);
686 
687 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);
688 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);
689 
696 
697 
698 /*
699 o-o o-o
700  / / /
701 o-o o-o
702  ,---'
703 o-o o-o
704  / / /
705 o-o o-o
706 */
707 
708 /* Scan8 organization:
709  * 0 1 2 3 4 5 6 7
710  * 0 DY y y y y y
711  * 1 y Y Y Y Y
712  * 2 y Y Y Y Y
713  * 3 y Y Y Y Y
714  * 4 y Y Y Y Y
715  * 5 DU u u u u u
716  * 6 u U U U U
717  * 7 u U U U U
718  * 8 u U U U U
719  * 9 u U U U U
720  * 10 DV v v v v v
721  * 11 v V V V V
722  * 12 v V V V V
723  * 13 v V V V V
724  * 14 v V V V V
725  * DY/DU/DV are for luma/chroma DC.
726  */
727 
728 #define LUMA_DC_BLOCK_INDEX 48
729 #define CHROMA_DC_BLOCK_INDEX 49
730 
731 //This table must be here because scan8[constant] must be known at compiletime
732 static const uint8_t scan8[16*3 + 3]={
733  4+ 1*8, 5+ 1*8, 4+ 2*8, 5+ 2*8,
734  6+ 1*8, 7+ 1*8, 6+ 2*8, 7+ 2*8,
735  4+ 3*8, 5+ 3*8, 4+ 4*8, 5+ 4*8,
736  6+ 3*8, 7+ 3*8, 6+ 4*8, 7+ 4*8,
737  4+ 6*8, 5+ 6*8, 4+ 7*8, 5+ 7*8,
738  6+ 6*8, 7+ 6*8, 6+ 7*8, 7+ 7*8,
739  4+ 8*8, 5+ 8*8, 4+ 9*8, 5+ 9*8,
740  6+ 8*8, 7+ 8*8, 6+ 9*8, 7+ 9*8,
741  4+11*8, 5+11*8, 4+12*8, 5+12*8,
742  6+11*8, 7+11*8, 6+12*8, 7+12*8,
743  4+13*8, 5+13*8, 4+14*8, 5+14*8,
744  6+13*8, 7+13*8, 6+14*8, 7+14*8,
745  0+ 0*8, 0+ 5*8, 0+10*8
746 };
747 
748 static av_always_inline uint32_t pack16to32(int a, int b){
749 #if HAVE_BIGENDIAN
750  return (b&0xFFFF) + (a<<16);
751 #else
752  return (a&0xFFFF) + (b<<16);
753 #endif
754 }
755 
756 static av_always_inline uint16_t pack8to16(int a, int b){
757 #if HAVE_BIGENDIAN
758  return (b&0xFF) + (a<<8);
759 #else
760  return (a&0xFF) + (b<<8);
761 #endif
762 }
763 
767 static av_always_inline int get_chroma_qp(H264Context *h, int t, int qscale){
768  return h->pps.chroma_qp_table[t][qscale];
769 }
770 
775  const int index8= scan8[n];
776  const int left= h->intra4x4_pred_mode_cache[index8 - 1];
777  const int top = h->intra4x4_pred_mode_cache[index8 - 8];
778  const int min= FFMIN(left, top);
779 
780  tprintf(h->s.avctx, "mode:%d %d min:%d\n", left ,top, min);
781 
782  if(min<0) return DC_PRED;
783  else return min;
784 }
785 
787  int8_t *i4x4= h->intra4x4_pred_mode + h->mb2br_xy[h->mb_xy];
788  int8_t *i4x4_cache= h->intra4x4_pred_mode_cache;
789 
790  AV_COPY32(i4x4, i4x4_cache + 4 + 8*4);
791  i4x4[4]= i4x4_cache[7+8*3];
792  i4x4[5]= i4x4_cache[7+8*2];
793  i4x4[6]= i4x4_cache[7+8*1];
794 }
795 
797  const int mb_xy= h->mb_xy;
798  uint8_t *nnz = h->non_zero_count[mb_xy];
799  uint8_t *nnz_cache = h->non_zero_count_cache;
800 
801  AV_COPY32(&nnz[ 0], &nnz_cache[4+8* 1]);
802  AV_COPY32(&nnz[ 4], &nnz_cache[4+8* 2]);
803  AV_COPY32(&nnz[ 8], &nnz_cache[4+8* 3]);
804  AV_COPY32(&nnz[12], &nnz_cache[4+8* 4]);
805  AV_COPY32(&nnz[16], &nnz_cache[4+8* 6]);
806  AV_COPY32(&nnz[20], &nnz_cache[4+8* 7]);
807  AV_COPY32(&nnz[32], &nnz_cache[4+8*11]);
808  AV_COPY32(&nnz[36], &nnz_cache[4+8*12]);
809 
810  if(!h->s.chroma_y_shift){
811  AV_COPY32(&nnz[24], &nnz_cache[4+8* 8]);
812  AV_COPY32(&nnz[28], &nnz_cache[4+8* 9]);
813  AV_COPY32(&nnz[40], &nnz_cache[4+8*13]);
814  AV_COPY32(&nnz[44], &nnz_cache[4+8*14]);
815  }
816 }
817 
819  int b_xy, int b8_xy, int mb_type, int list )
820 {
821  int16_t (*mv_dst)[2] = &s->current_picture.f.motion_val[list][b_xy];
822  int16_t (*mv_src)[2] = &h->mv_cache[list][scan8[0]];
823  AV_COPY128(mv_dst + 0*b_stride, mv_src + 8*0);
824  AV_COPY128(mv_dst + 1*b_stride, mv_src + 8*1);
825  AV_COPY128(mv_dst + 2*b_stride, mv_src + 8*2);
826  AV_COPY128(mv_dst + 3*b_stride, mv_src + 8*3);
827  if( CABAC ) {
828  uint8_t (*mvd_dst)[2] = &h->mvd_table[list][FMO ? 8*h->mb_xy : h->mb2br_xy[h->mb_xy]];
829  uint8_t (*mvd_src)[2] = &h->mvd_cache[list][scan8[0]];
830  if(IS_SKIP(mb_type))
831  AV_ZERO128(mvd_dst);
832  else{
833  AV_COPY64(mvd_dst, mvd_src + 8*3);
834  AV_COPY16(mvd_dst + 3 + 3, mvd_src + 3 + 8*0);
835  AV_COPY16(mvd_dst + 3 + 2, mvd_src + 3 + 8*1);
836  AV_COPY16(mvd_dst + 3 + 1, mvd_src + 3 + 8*2);
837  }
838  }
839 
840  {
841  int8_t *ref_index = &s->current_picture.f.ref_index[list][b8_xy];
842  int8_t *ref_cache = h->ref_cache[list];
843  ref_index[0+0*2]= ref_cache[scan8[0]];
844  ref_index[1+0*2]= ref_cache[scan8[4]];
845  ref_index[0+1*2]= ref_cache[scan8[8]];
846  ref_index[1+1*2]= ref_cache[scan8[12]];
847  }
848 }
849 
850 static av_always_inline void write_back_motion(H264Context *h, int mb_type){
851  MpegEncContext * const s = &h->s;
852  const int b_stride = h->b_stride;
853  const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride; //try mb2b(8)_xy
854  const int b8_xy= 4*h->mb_xy;
855 
856  if(USES_LIST(mb_type, 0)){
857  write_back_motion_list(h, s, b_stride, b_xy, b8_xy, mb_type, 0);
858  }else{
860  2, 2, 2, (uint8_t)LIST_NOT_USED, 1);
861  }
862  if(USES_LIST(mb_type, 1)){
863  write_back_motion_list(h, s, b_stride, b_xy, b8_xy, mb_type, 1);
864  }
865 
867  if(IS_8X8(mb_type)){
868  uint8_t *direct_table = &h->direct_table[4*h->mb_xy];
869  direct_table[1] = h->sub_mb_type[1]>>1;
870  direct_table[2] = h->sub_mb_type[2]>>1;
871  direct_table[3] = h->sub_mb_type[3]>>1;
872  }
873  }
874 }
875 
878  return !(AV_RN64A(h->sub_mb_type) & ((MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_8x8 )*0x0001000100010001ULL));
879  else
880  return !(AV_RN64A(h->sub_mb_type) & ((MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_8x8|MB_TYPE_DIRECT2)*0x0001000100010001ULL));
881 }
882 
883 #endif /* AVCODEC_H264_H */