vp56.h 7.5 KB

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