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 
353  //interlacing specific flags
356  int mb_mbaff;
357 
358  DECLARE_ALIGNED(8, uint16_t, sub_mb_type)[4];
359 
360  //Weighted pred stuff
365  //The following 2 can be changed to int8_t but that causes 10cpu cycles speedloss
366  int luma_weight[48][2][2];
367  int chroma_weight[48][2][2][2];
368  int implicit_weight[48][48][2];
369 
375  int map_col_to_list0[2][16+32];
376  int map_col_to_list0_field[2][2][16+32];
377 
381  unsigned int ref_count[2];
382  unsigned int list_count;
383  uint8_t *list_counts;
384  Picture ref_list[2][48];
387  int ref2frm[MAX_SLICES][2][64];
388 
389  //data partitioning
394 
395  DECLARE_ALIGNED(16, DCTELEM, mb)[16*48*2];
398 
403  uint8_t cabac_state[1024];
404 
405  /* 0x100 -> non null luma_dc, 0x80/0x40 -> non null chroma_dc (cb/cr), 0x?0 -> chroma_cbp(0,1,2), 0x0? luma_cbp */
406  uint16_t *cbp_table;
407  int cbp;
408  int top_cbp;
409  int left_cbp;
410  /* chroma_pred_mode for i4x4 or i16x16, else 0 */
413  uint8_t (*mvd_table[2])[2];
414  DECLARE_ALIGNED(16, uint8_t, mvd_cache)[2][5*8][2];
415  uint8_t *direct_table;
416  uint8_t direct_cache[5*8];
417 
418  uint8_t zigzag_scan[16];
419  uint8_t zigzag_scan8x8[64];
420  uint8_t zigzag_scan8x8_cavlc[64];
421  uint8_t field_scan[16];
422  uint8_t field_scan8x8[64];
423  uint8_t field_scan8x8_cavlc[64];
424  const uint8_t *zigzag_scan_q0;
425  const uint8_t *zigzag_scan8x8_q0;
426  const uint8_t *zigzag_scan8x8_cavlc_q0;
427  const uint8_t *field_scan_q0;
428  const uint8_t *field_scan8x8_q0;
429  const uint8_t *field_scan8x8_cavlc_q0;
430 
432 
433  int mb_xy;
434 
436 
437  //deblock
441 
442 //=============================================================
443  //Things below are not used in the MB or more inner code
444 
447  uint8_t *rbsp_buffer[2];
448  unsigned int rbsp_buffer_size[2];
449 
453  int is_avc;
455  int got_first;
456 
459 
461 
462  uint16_t *slice_table_base;
463 
464 
465  //POC stuff
466  int poc_lsb;
467  int poc_msb;
469  int delta_poc[2];
476 
481 
486 
488 
497 
504 
507 
509 
515 
520 
528 
534 
542 
550 
557 
562 
567 
576 
579 
580  // Timestamp stuff
583 
585 }H264Context;
586 
587 
588 extern const uint8_t ff_h264_chroma_qp[3][QP_MAX_NUM+1];
589 
594 
599 
604 
608 int ff_h264_decode_picture_parameter_set(H264Context *h, int bit_length);
609 
617 const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src, int *dst_length, int *consumed, int length);
618 
623 
627 int ff_h264_get_slice_type(const H264Context *h);
628 
634 
639 
643 
647 int ff_h264_execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count);
648 
650 
652 
653 
658 
662 int ff_h264_check_intra_pred_mode(H264Context *h, int mode, int is_chroma);
663 
670 
676 
682 
684 
687 void ff_h264_pred_direct_motion(H264Context * const h, int *mb_type);
688 
689 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);
690 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);
691 
698 
699 
700 /*
701 o-o o-o
702  / / /
703 o-o o-o
704  ,---'
705 o-o o-o
706  / / /
707 o-o o-o
708 */
709 
710 /* Scan8 organization:
711  * 0 1 2 3 4 5 6 7
712  * 0 DY y y y y y
713  * 1 y Y Y Y Y
714  * 2 y Y Y Y Y
715  * 3 y Y Y Y Y
716  * 4 y Y Y Y Y
717  * 5 DU u u u u u
718  * 6 u U U U U
719  * 7 u U U U U
720  * 8 u U U U U
721  * 9 u U U U U
722  * 10 DV v v v v v
723  * 11 v V V V V
724  * 12 v V V V V
725  * 13 v V V V V
726  * 14 v V V V V
727  * DY/DU/DV are for luma/chroma DC.
728  */
729 
730 #define LUMA_DC_BLOCK_INDEX 48
731 #define CHROMA_DC_BLOCK_INDEX 49
732 
733 //This table must be here because scan8[constant] must be known at compiletime
734 static const uint8_t scan8[16*3 + 3]={
735  4+ 1*8, 5+ 1*8, 4+ 2*8, 5+ 2*8,
736  6+ 1*8, 7+ 1*8, 6+ 2*8, 7+ 2*8,
737  4+ 3*8, 5+ 3*8, 4+ 4*8, 5+ 4*8,
738  6+ 3*8, 7+ 3*8, 6+ 4*8, 7+ 4*8,
739  4+ 6*8, 5+ 6*8, 4+ 7*8, 5+ 7*8,
740  6+ 6*8, 7+ 6*8, 6+ 7*8, 7+ 7*8,
741  4+ 8*8, 5+ 8*8, 4+ 9*8, 5+ 9*8,
742  6+ 8*8, 7+ 8*8, 6+ 9*8, 7+ 9*8,
743  4+11*8, 5+11*8, 4+12*8, 5+12*8,
744  6+11*8, 7+11*8, 6+12*8, 7+12*8,
745  4+13*8, 5+13*8, 4+14*8, 5+14*8,
746  6+13*8, 7+13*8, 6+14*8, 7+14*8,
747  0+ 0*8, 0+ 5*8, 0+10*8
748 };
749 
750 static av_always_inline uint32_t pack16to32(int a, int b){
751 #if HAVE_BIGENDIAN
752  return (b&0xFFFF) + (a<<16);
753 #else
754  return (a&0xFFFF) + (b<<16);
755 #endif
756 }
757 
758 static av_always_inline uint16_t pack8to16(int a, int b){
759 #if HAVE_BIGENDIAN
760  return (b&0xFF) + (a<<8);
761 #else
762  return (a&0xFF) + (b<<8);
763 #endif
764 }
765 
769 static av_always_inline int get_chroma_qp(H264Context *h, int t, int qscale){
770  return h->pps.chroma_qp_table[t][qscale];
771 }
772 
777  const int index8= scan8[n];
778  const int left= h->intra4x4_pred_mode_cache[index8 - 1];
779  const int top = h->intra4x4_pred_mode_cache[index8 - 8];
780  const int min= FFMIN(left, top);
781 
782  tprintf(h->s.avctx, "mode:%d %d min:%d\n", left ,top, min);
783 
784  if(min<0) return DC_PRED;
785  else return min;
786 }
787 
789  int8_t *i4x4= h->intra4x4_pred_mode + h->mb2br_xy[h->mb_xy];
790  int8_t *i4x4_cache= h->intra4x4_pred_mode_cache;
791 
792  AV_COPY32(i4x4, i4x4_cache + 4 + 8*4);
793  i4x4[4]= i4x4_cache[7+8*3];
794  i4x4[5]= i4x4_cache[7+8*2];
795  i4x4[6]= i4x4_cache[7+8*1];
796 }
797 
799  const int mb_xy= h->mb_xy;
800  uint8_t *nnz = h->non_zero_count[mb_xy];
801  uint8_t *nnz_cache = h->non_zero_count_cache;
802 
803  AV_COPY32(&nnz[ 0], &nnz_cache[4+8* 1]);
804  AV_COPY32(&nnz[ 4], &nnz_cache[4+8* 2]);
805  AV_COPY32(&nnz[ 8], &nnz_cache[4+8* 3]);
806  AV_COPY32(&nnz[12], &nnz_cache[4+8* 4]);
807  AV_COPY32(&nnz[16], &nnz_cache[4+8* 6]);
808  AV_COPY32(&nnz[20], &nnz_cache[4+8* 7]);
809  AV_COPY32(&nnz[32], &nnz_cache[4+8*11]);
810  AV_COPY32(&nnz[36], &nnz_cache[4+8*12]);
811 
812  if(!h->s.chroma_y_shift){
813  AV_COPY32(&nnz[24], &nnz_cache[4+8* 8]);
814  AV_COPY32(&nnz[28], &nnz_cache[4+8* 9]);
815  AV_COPY32(&nnz[40], &nnz_cache[4+8*13]);
816  AV_COPY32(&nnz[44], &nnz_cache[4+8*14]);
817  }
818 }
819 
821  int b_xy, int b8_xy, int mb_type, int list )
822 {
823  int16_t (*mv_dst)[2] = &s->current_picture.f.motion_val[list][b_xy];
824  int16_t (*mv_src)[2] = &h->mv_cache[list][scan8[0]];
825  AV_COPY128(mv_dst + 0*b_stride, mv_src + 8*0);
826  AV_COPY128(mv_dst + 1*b_stride, mv_src + 8*1);
827  AV_COPY128(mv_dst + 2*b_stride, mv_src + 8*2);
828  AV_COPY128(mv_dst + 3*b_stride, mv_src + 8*3);
829  if( CABAC ) {
830  uint8_t (*mvd_dst)[2] = &h->mvd_table[list][FMO ? 8*h->mb_xy : h->mb2br_xy[h->mb_xy]];
831  uint8_t (*mvd_src)[2] = &h->mvd_cache[list][scan8[0]];
832  if(IS_SKIP(mb_type))
833  AV_ZERO128(mvd_dst);
834  else{
835  AV_COPY64(mvd_dst, mvd_src + 8*3);
836  AV_COPY16(mvd_dst + 3 + 3, mvd_src + 3 + 8*0);
837  AV_COPY16(mvd_dst + 3 + 2, mvd_src + 3 + 8*1);
838  AV_COPY16(mvd_dst + 3 + 1, mvd_src + 3 + 8*2);
839  }
840  }
841 
842  {
843  int8_t *ref_index = &s->current_picture.f.ref_index[list][b8_xy];
844  int8_t *ref_cache = h->ref_cache[list];
845  ref_index[0+0*2]= ref_cache[scan8[0]];
846  ref_index[1+0*2]= ref_cache[scan8[4]];
847  ref_index[0+1*2]= ref_cache[scan8[8]];
848  ref_index[1+1*2]= ref_cache[scan8[12]];
849  }
850 }
851 
852 static av_always_inline void write_back_motion(H264Context *h, int mb_type){
853  MpegEncContext * const s = &h->s;
854  const int b_stride = h->b_stride;
855  const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride; //try mb2b(8)_xy
856  const int b8_xy= 4*h->mb_xy;
857 
858  if(USES_LIST(mb_type, 0)){
859  write_back_motion_list(h, s, b_stride, b_xy, b8_xy, mb_type, 0);
860  }else{
862  2, 2, 2, (uint8_t)LIST_NOT_USED, 1);
863  }
864  if(USES_LIST(mb_type, 1)){
865  write_back_motion_list(h, s, b_stride, b_xy, b8_xy, mb_type, 1);
866  }
867 
869  if(IS_8X8(mb_type)){
870  uint8_t *direct_table = &h->direct_table[4*h->mb_xy];
871  direct_table[1] = h->sub_mb_type[1]>>1;
872  direct_table[2] = h->sub_mb_type[2]>>1;
873  direct_table[3] = h->sub_mb_type[3]>>1;
874  }
875  }
876 }
877 
880  return !(AV_RN64A(h->sub_mb_type) & ((MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_8x8 )*0x0001000100010001ULL));
881  else
882  return !(AV_RN64A(h->sub_mb_type) & ((MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_8x8|MB_TYPE_DIRECT2)*0x0001000100010001ULL));
883 }
884 
885 #endif /* AVCODEC_H264_H */