vp56.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * VP5 and VP6 compatible video decoder (common features)
  23. */
  24. #ifndef AVCODEC_VP56_H
  25. #define AVCODEC_VP56_H
  26. #include "libavutil/mem_internal.h"
  27. #include "avcodec.h"
  28. #include "get_bits.h"
  29. #include "hpeldsp.h"
  30. #include "h264chroma.h"
  31. #include "videodsp.h"
  32. #include "vp3dsp.h"
  33. #include "vp56dsp.h"
  34. #include "vpx_rac.h"
  35. typedef struct vp56_context VP56Context;
  36. typedef enum {
  37. VP56_FRAME_NONE =-1,
  38. VP56_FRAME_CURRENT = 0,
  39. VP56_FRAME_PREVIOUS = 1,
  40. VP56_FRAME_GOLDEN = 2,
  41. } VP56Frame;
  42. typedef enum {
  43. VP56_MB_INTER_NOVEC_PF = 0, /**< Inter MB, no vector, from previous frame */
  44. VP56_MB_INTRA = 1, /**< Intra MB */
  45. VP56_MB_INTER_DELTA_PF = 2, /**< Inter MB, above/left vector + delta, from previous frame */
  46. VP56_MB_INTER_V1_PF = 3, /**< Inter MB, first vector, from previous frame */
  47. VP56_MB_INTER_V2_PF = 4, /**< Inter MB, second vector, from previous frame */
  48. VP56_MB_INTER_NOVEC_GF = 5, /**< Inter MB, no vector, from golden frame */
  49. VP56_MB_INTER_DELTA_GF = 6, /**< Inter MB, above/left vector + delta, from golden frame */
  50. VP56_MB_INTER_4V = 7, /**< Inter MB, 4 vectors, from previous frame */
  51. VP56_MB_INTER_V1_GF = 8, /**< Inter MB, first vector, from golden frame */
  52. VP56_MB_INTER_V2_GF = 9, /**< Inter MB, second vector, from golden frame */
  53. } VP56mb;
  54. typedef struct VP56Tree {
  55. int8_t val;
  56. int8_t prob_idx;
  57. } VP56Tree;
  58. typedef struct VP56mv {
  59. DECLARE_ALIGNED(4, int16_t, x);
  60. int16_t y;
  61. } VP56mv;
  62. #define VP56_SIZE_CHANGE 1
  63. typedef void (*VP56ParseVectorAdjustment)(VP56Context *s,
  64. VP56mv *vect);
  65. typedef void (*VP56Filter)(VP56Context *s, uint8_t *dst, uint8_t *src,
  66. int offset1, int offset2, ptrdiff_t stride,
  67. VP56mv mv, int mask, int select, int luma);
  68. typedef int (*VP56ParseCoeff)(VP56Context *s);
  69. typedef void (*VP56DefaultModelsInit)(VP56Context *s);
  70. typedef void (*VP56ParseVectorModels)(VP56Context *s);
  71. typedef int (*VP56ParseCoeffModels)(VP56Context *s);
  72. typedef int (*VP56ParseHeader)(VP56Context *s, const uint8_t *buf,
  73. int buf_size);
  74. typedef struct VP56RefDc {
  75. uint8_t not_null_dc;
  76. VP56Frame ref_frame;
  77. int16_t dc_coeff;
  78. } VP56RefDc;
  79. typedef struct VP56Macroblock {
  80. uint8_t type;
  81. VP56mv mv;
  82. } VP56Macroblock;
  83. typedef struct VP56Model {
  84. uint8_t coeff_reorder[64]; /* used in vp6 only */
  85. uint8_t coeff_index_to_pos[64]; /* used in vp6 only */
  86. uint8_t coeff_index_to_idct_selector[64]; /* used in vp6 only */
  87. uint8_t vector_sig[2]; /* delta sign */
  88. uint8_t vector_dct[2]; /* delta coding types */
  89. uint8_t vector_pdi[2][2]; /* predefined delta init */
  90. uint8_t vector_pdv[2][7]; /* predefined delta values */
  91. uint8_t vector_fdv[2][8]; /* 8 bit delta value definition */
  92. uint8_t coeff_dccv[2][11]; /* DC coeff value */
  93. uint8_t coeff_ract[2][3][6][11]; /* Run/AC coding type and AC coeff value */
  94. uint8_t coeff_acct[2][3][3][6][5];/* vp5 only AC coding type for coding group < 3 */
  95. uint8_t coeff_dcct[2][36][5]; /* DC coeff coding type */
  96. uint8_t coeff_runv[2][14]; /* run value (vp6 only) */
  97. uint8_t mb_type[3][10][10]; /* model for decoding MB type */
  98. uint8_t mb_types_stats[3][10][2];/* contextual, next MB type stats */
  99. } VP56Model;
  100. struct vp56_context {
  101. AVCodecContext *avctx;
  102. H264ChromaContext h264chroma;
  103. HpelDSPContext hdsp;
  104. VideoDSPContext vdsp;
  105. VP3DSPContext vp3dsp;
  106. VP56DSPContext vp56dsp;
  107. uint8_t idct_scantable[64];
  108. AVFrame *frames[4];
  109. uint8_t *edge_emu_buffer_alloc;
  110. uint8_t *edge_emu_buffer;
  111. VPXRangeCoder c;
  112. VPXRangeCoder cc;
  113. VPXRangeCoder *ccp;
  114. int sub_version;
  115. /* frame info */
  116. int golden_frame;
  117. int plane_width[4];
  118. int plane_height[4];
  119. int mb_width; /* number of horizontal MB */
  120. int mb_height; /* number of vertical MB */
  121. int block_offset[6];
  122. int quantizer;
  123. uint16_t dequant_dc;
  124. uint16_t dequant_ac;
  125. /* DC predictors management */
  126. VP56RefDc *above_blocks;
  127. VP56RefDc left_block[4];
  128. int above_block_idx[6];
  129. int16_t prev_dc[3][3]; /* [plan][ref_frame] */
  130. /* blocks / macroblock */
  131. VP56mb mb_type;
  132. VP56Macroblock *macroblocks;
  133. DECLARE_ALIGNED(16, int16_t, block_coeff)[6][64];
  134. int idct_selector[6];
  135. const uint8_t *def_coeff_reorder;/* used in vp6 only */
  136. /* motion vectors */
  137. VP56mv mv[6]; /* vectors for each block in MB */
  138. VP56mv vector_candidate[2];
  139. int vector_candidate_pos;
  140. /* filtering hints */
  141. int filter_header; /* used in vp6 only */
  142. int deblock_filtering;
  143. int filter_selection;
  144. int filter_mode;
  145. int max_vector_length;
  146. int sample_variance_threshold;
  147. DECLARE_ALIGNED(8, int, bounding_values_array)[256];
  148. uint8_t coeff_ctx[4][64]; /* used in vp5 only */
  149. uint8_t coeff_ctx_last[4]; /* used in vp5 only */
  150. int has_alpha;
  151. /* interlacing params */
  152. int interlaced;
  153. int il_prob;
  154. int il_block;
  155. /* upside-down flipping hints */
  156. int flip; /* are we flipping ? */
  157. int frbi; /* first row block index in MB */
  158. int srbi; /* second row block index in MB */
  159. ptrdiff_t stride[4]; /* stride for each plan */
  160. const uint8_t *vp56_coord_div;
  161. VP56ParseVectorAdjustment parse_vector_adjustment;
  162. VP56Filter filter;
  163. VP56ParseCoeff parse_coeff;
  164. VP56DefaultModelsInit default_models_init;
  165. VP56ParseVectorModels parse_vector_models;
  166. VP56ParseCoeffModels parse_coeff_models;
  167. VP56ParseHeader parse_header;
  168. /* for "slice" parallelism between YUV and A */
  169. VP56Context *alpha_context;
  170. VP56Model *modelp;
  171. VP56Model model;
  172. /* huffman decoding */
  173. int use_huffman;
  174. GetBitContext gb;
  175. VLC dccv_vlc[2];
  176. VLC runv_vlc[2];
  177. VLC ract_vlc[2][3][4];
  178. unsigned int nb_null[2][2]; /* number of consecutive NULL DC/AC */
  179. int have_undamaged_frame;
  180. int discard_frame;
  181. };
  182. /**
  183. * Initializes an VP56Context. Expects its caller to clean up
  184. * in case of error.
  185. */
  186. int ff_vp56_init_context(AVCodecContext *avctx, VP56Context *s,
  187. int flip, int has_alpha);
  188. int ff_vp56_free_context(VP56Context *s);
  189. void ff_vp56_init_dequant(VP56Context *s, int quantizer);
  190. int ff_vp56_decode_frame(AVCodecContext *avctx, AVFrame *frame,
  191. int *got_frame, AVPacket *avpkt);
  192. /**
  193. * vp56 specific range coder implementation
  194. */
  195. static int vp56_rac_gets(VPXRangeCoder *c, int bits)
  196. {
  197. int value = 0;
  198. while (bits--) {
  199. value = (value << 1) | vpx_rac_get(c);
  200. }
  201. return value;
  202. }
  203. // P(7)
  204. av_unused static int vp56_rac_gets_nn(VPXRangeCoder *c, int bits)
  205. {
  206. int v = vp56_rac_gets(c, 7) << 1;
  207. return v + !v;
  208. }
  209. static av_always_inline
  210. int vp56_rac_get_tree(VPXRangeCoder *c,
  211. const VP56Tree *tree,
  212. const uint8_t *probs)
  213. {
  214. while (tree->val > 0) {
  215. if (vpx_rac_get_prob_branchy(c, probs[tree->prob_idx]))
  216. tree += tree->val;
  217. else
  218. tree++;
  219. }
  220. return -tree->val;
  221. }
  222. #endif /* AVCODEC_VP56_H */