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 
98 #define QP_MAX_NUM (51 + 2 * 6) // The maximum supported qp
99 
100 /* NAL unit types */
101 enum {
116  NAL_FF_IGNORE = 0xff0f001,
117 };
118 
122 typedef enum {
127 } SEI_Type;
128 
132 typedef enum {
143 
147 typedef struct SPS {
153  int poc_type;
161  int mb_width;
162  int mb_height;
164  int mb_aff;
166  int crop;
167  unsigned int crop_left;
168  unsigned int crop_right;
169  unsigned int crop_top;
170  unsigned int crop_bottom;
181  uint32_t time_scale;
183  short offset_for_ref_frame[256]; // FIXME dyn aloc?
193  int cpb_cnt;
201  int new;
202 } SPS;
203 
207 typedef struct PPS {
208  unsigned int sps_id;
209  int cabac;
213  unsigned int ref_count[2];
216  int init_qp;
217  int init_qs;
227 } PPS;
228 
232 typedef enum MMCOOpcode {
233  MMCO_END = 0,
240 } MMCOOpcode;
241 
245 typedef struct MMCO {
248  int long_arg;
249 } MMCO;
250 
254 typedef struct H264Context {
258  int chroma_qp[2]; // QPc
259 
260  int qp_thresh;
261 
264 
265  // prediction stuff
268 
273 
275  int top_type;
278 
281 
286  unsigned int top_samples_available;
289  uint8_t (*top_borders[2])[(16 * 3) * 2];
290 
296 
298 
302  DECLARE_ALIGNED(16, int16_t, mv_cache)[2][5 * 8][2];
303  DECLARE_ALIGNED(8, int8_t, ref_cache)[2][5 * 8];
304 #define LIST_NOT_USED -1 // FIXME rename?
305 #define PART_NOT_AVAILABLE -2
306 
311 
316  int block_offset[2 * (16 * 3)];
317 
318  uint32_t *mb2b_xy; // FIXME are these 4 a good idea?
319  uint32_t *mb2br_xy;
320  int b_stride; // FIXME use s->b4_stride
321 
324 
327 
328  unsigned current_sps_id;
330 
334  PPS pps; // FIXME move to Picture perhaps? (->no) do we need that?
335 
336  uint32_t dequant4_buffer[6][QP_MAX_NUM + 1][16]; // FIXME should these be moved down?
337  uint32_t dequant8_buffer[6][QP_MAX_NUM + 1][64];
338  uint32_t(*dequant4_coeff[6])[16];
339  uint32_t(*dequant8_coeff[6])[64];
340 
342  uint16_t *slice_table;
346 
347  // interlacing specific flags
350  int mb_mbaff;
351 
352  DECLARE_ALIGNED(8, uint16_t, sub_mb_type)[4];
353 
354  // Weighted pred stuff
359  // The following 2 can be changed to int8_t but that causes 10cpu cycles speedloss
360  int luma_weight[48][2][2];
361  int chroma_weight[48][2][2][2];
362  int implicit_weight[48][48][2];
363 
369  int map_col_to_list0[2][16 + 32];
370  int map_col_to_list0_field[2][2][16 + 32];
371 
375  unsigned int ref_count[2];
376  unsigned int list_count;
378  Picture ref_list[2][48];
381  int ref2frm[MAX_SLICES][2][64];
382 
383  // data partitioning
388 
389  DECLARE_ALIGNED(16, DCTELEM, mb)[16 * 48 * 2];
391  DCTELEM mb_padding[256 * 2];
392 
398 
399  /* 0x100 -> non null luma_dc, 0x80/0x40 -> non null chroma_dc (cb/cr), 0x?0 -> chroma_cbp(0, 1, 2), 0x0? luma_cbp */
400  uint16_t *cbp_table;
401  int cbp;
402  int top_cbp;
403  int left_cbp;
404  /* chroma_pred_mode for i4x4 or i16x16, else 0 */
407  uint8_t (*mvd_table[2])[2];
408  DECLARE_ALIGNED(16, uint8_t, mvd_cache)[2][5 * 8][2];
411 
424 
426 
427  int mb_xy;
428 
430 
431  // deblock
435 
436  // =============================================================
437  // Things below are not used in the MB or more inner code
438 
442  unsigned int rbsp_buffer_size[2];
443 
447  int is_avc;
449  int got_first;
450 
452 
455 
457 
458  uint16_t *slice_table_base;
459 
460  // POC stuff
461  int poc_lsb;
462  int poc_msb;
464  int delta_poc[2];
471 
476 
481 
483 
492 
499 
502 
504 
510 
515 
523 
529 
537 
545 
552 
557 
562 
571 
574 
575  // Timestamp stuff
578 
581 } H264Context;
582 
583 extern const uint8_t ff_h264_chroma_qp[3][QP_MAX_NUM + 1];
584 extern const uint16_t ff_h264_mb_sizes[4];
585 
590 
595 
600 
604 int ff_h264_decode_picture_parameter_set(H264Context *h, int bit_length);
605 
614 const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src,
615  int *dst_length, int *consumed, int length);
616 
622 
626 int ff_h264_get_slice_type(const H264Context *h);
627 
633 
638 
642 
646 int ff_h264_execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count);
647 
649  int first_slice);
650 
651 int ff_generate_sliding_window_mmcos(H264Context *h, int first_slice);
652 
658 
663 int ff_h264_check_intra_pred_mode(H264Context *h, int mode, int is_chroma);
664 
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,
690  uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr,
691  unsigned int linesize, unsigned int uvlinesize);
692 void ff_h264_filter_mb(H264Context *h, int mb_x, int mb_y,
693  uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr,
694  unsigned int linesize, unsigned int uvlinesize);
695 
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 {
755 #if HAVE_BIGENDIAN
756  return (b & 0xFFFF) + (a << 16);
757 #else
758  return (a & 0xFFFF) + (b << 16);
759 #endif
760 }
761 
762 static av_always_inline uint16_t pack8to16(int a, int b)
763 {
764 #if HAVE_BIGENDIAN
765  return (b & 0xFF) + (a << 8);
766 #else
767  return (a & 0xFF) + (b << 8);
768 #endif
769 }
770 
774 static av_always_inline int get_chroma_qp(H264Context *h, int t, int qscale)
775 {
776  return h->pps.chroma_qp_table[t][qscale];
777 }
778 
783 {
784  const int index8 = scan8[n];
785  const int left = h->intra4x4_pred_mode_cache[index8 - 1];
786  const int top = h->intra4x4_pred_mode_cache[index8 - 8];
787  const int min = FFMIN(left, top);
788 
789  tprintf(h->s.avctx, "mode:%d %d min:%d\n", left, top, min);
790 
791  if (min < 0)
792  return DC_PRED;
793  else
794  return min;
795 }
796 
798 {
799  int8_t *i4x4 = h->intra4x4_pred_mode + h->mb2br_xy[h->mb_xy];
800  int8_t *i4x4_cache = h->intra4x4_pred_mode_cache;
801 
802  AV_COPY32(i4x4, i4x4_cache + 4 + 8 * 4);
803  i4x4[4] = i4x4_cache[7 + 8 * 3];
804  i4x4[5] = i4x4_cache[7 + 8 * 2];
805  i4x4[6] = i4x4_cache[7 + 8 * 1];
806 }
807 
809 {
810  const int mb_xy = h->mb_xy;
811  uint8_t *nnz = h->non_zero_count[mb_xy];
812  uint8_t *nnz_cache = h->non_zero_count_cache;
813 
814  AV_COPY32(&nnz[ 0], &nnz_cache[4 + 8 * 1]);
815  AV_COPY32(&nnz[ 4], &nnz_cache[4 + 8 * 2]);
816  AV_COPY32(&nnz[ 8], &nnz_cache[4 + 8 * 3]);
817  AV_COPY32(&nnz[12], &nnz_cache[4 + 8 * 4]);
818  AV_COPY32(&nnz[16], &nnz_cache[4 + 8 * 6]);
819  AV_COPY32(&nnz[20], &nnz_cache[4 + 8 * 7]);
820  AV_COPY32(&nnz[32], &nnz_cache[4 + 8 * 11]);
821  AV_COPY32(&nnz[36], &nnz_cache[4 + 8 * 12]);
822 
823  if (!h->s.chroma_y_shift) {
824  AV_COPY32(&nnz[24], &nnz_cache[4 + 8 * 8]);
825  AV_COPY32(&nnz[28], &nnz_cache[4 + 8 * 9]);
826  AV_COPY32(&nnz[40], &nnz_cache[4 + 8 * 13]);
827  AV_COPY32(&nnz[44], &nnz_cache[4 + 8 * 14]);
828  }
829 }
830 
832  MpegEncContext *const s,
833  int b_stride,
834  int b_xy, int b8_xy,
835  int mb_type, int list)
836 {
837  int16_t(*mv_dst)[2] = &s->current_picture.f.motion_val[list][b_xy];
838  int16_t(*mv_src)[2] = &h->mv_cache[list][scan8[0]];
839  AV_COPY128(mv_dst + 0 * b_stride, mv_src + 8 * 0);
840  AV_COPY128(mv_dst + 1 * b_stride, mv_src + 8 * 1);
841  AV_COPY128(mv_dst + 2 * b_stride, mv_src + 8 * 2);
842  AV_COPY128(mv_dst + 3 * b_stride, mv_src + 8 * 3);
843  if (CABAC) {
844  uint8_t (*mvd_dst)[2] = &h->mvd_table[list][FMO ? 8 * h->mb_xy
845  : h->mb2br_xy[h->mb_xy]];
846  uint8_t(*mvd_src)[2] = &h->mvd_cache[list][scan8[0]];
847  if (IS_SKIP(mb_type)) {
848  AV_ZERO128(mvd_dst);
849  } else {
850  AV_COPY64(mvd_dst, mvd_src + 8 * 3);
851  AV_COPY16(mvd_dst + 3 + 3, mvd_src + 3 + 8 * 0);
852  AV_COPY16(mvd_dst + 3 + 2, mvd_src + 3 + 8 * 1);
853  AV_COPY16(mvd_dst + 3 + 1, mvd_src + 3 + 8 * 2);
854  }
855  }
856 
857  {
858  int8_t *ref_index = &s->current_picture.f.ref_index[list][b8_xy];
859  int8_t *ref_cache = h->ref_cache[list];
860  ref_index[0 + 0 * 2] = ref_cache[scan8[0]];
861  ref_index[1 + 0 * 2] = ref_cache[scan8[4]];
862  ref_index[0 + 1 * 2] = ref_cache[scan8[8]];
863  ref_index[1 + 1 * 2] = ref_cache[scan8[12]];
864  }
865 }
866 
867 static av_always_inline void write_back_motion(H264Context *h, int mb_type)
868 {
869  MpegEncContext *const s = &h->s;
870  const int b_stride = h->b_stride;
871  const int b_xy = 4 * s->mb_x + 4 * s->mb_y * h->b_stride; // try mb2b(8)_xy
872  const int b8_xy = 4 * h->mb_xy;
873 
874  if (USES_LIST(mb_type, 0)) {
875  write_back_motion_list(h, s, b_stride, b_xy, b8_xy, mb_type, 0);
876  } else {
878  2, 2, 2, (uint8_t)LIST_NOT_USED, 1);
879  }
880  if (USES_LIST(mb_type, 1))
881  write_back_motion_list(h, s, b_stride, b_xy, b8_xy, mb_type, 1);
882 
883  if (h->slice_type_nos == AV_PICTURE_TYPE_B && CABAC) {
884  if (IS_8X8(mb_type)) {
885  uint8_t *direct_table = &h->direct_table[4 * h->mb_xy];
886  direct_table[1] = h->sub_mb_type[1] >> 1;
887  direct_table[2] = h->sub_mb_type[2] >> 1;
888  direct_table[3] = h->sub_mb_type[3] >> 1;
889  }
890  }
891 }
892 
894 {
896  return !(AV_RN64A(h->sub_mb_type) &
898  0x0001000100010001ULL));
899  else
900  return !(AV_RN64A(h->sub_mb_type) &
902  0x0001000100010001ULL));
903 }
904 
905 #endif /* AVCODEC_H264_H */