You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

272 lines
7.6KB

  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 "get_bits.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. const uint8_t *end;
  48. unsigned long code_word;
  49. } VP56RangeCoder;
  50. typedef struct {
  51. uint8_t not_null_dc;
  52. VP56Frame ref_frame;
  53. DCTELEM dc_coeff;
  54. } VP56RefDc;
  55. struct vp56_mv {
  56. int x;
  57. int y;
  58. };
  59. typedef struct {
  60. uint8_t type;
  61. VP56mv mv;
  62. } VP56Macroblock;
  63. typedef struct {
  64. uint8_t coeff_reorder[64]; /* used in vp6 only */
  65. uint8_t coeff_index_to_pos[64]; /* used in vp6 only */
  66. uint8_t vector_sig[2]; /* delta sign */
  67. uint8_t vector_dct[2]; /* delta coding types */
  68. uint8_t vector_pdi[2][2]; /* predefined delta init */
  69. uint8_t vector_pdv[2][7]; /* predefined delta values */
  70. uint8_t vector_fdv[2][8]; /* 8 bit delta value definition */
  71. uint8_t coeff_dccv[2][11]; /* DC coeff value */
  72. uint8_t coeff_ract[2][3][6][11]; /* Run/AC coding type and AC coeff value */
  73. uint8_t coeff_acct[2][3][3][6][5];/* vp5 only AC coding type for coding group < 3 */
  74. uint8_t coeff_dcct[2][36][5]; /* DC coeff coding type */
  75. uint8_t coeff_runv[2][14]; /* run value (vp6 only) */
  76. uint8_t mb_type[3][10][10]; /* model for decoding MB type */
  77. uint8_t mb_types_stats[3][10][2];/* contextual, next MB type stats */
  78. } VP56Model;
  79. struct vp56_context {
  80. AVCodecContext *avctx;
  81. DSPContext dsp;
  82. ScanTable scantable;
  83. AVFrame frames[4];
  84. AVFrame *framep[6];
  85. uint8_t *edge_emu_buffer_alloc;
  86. uint8_t *edge_emu_buffer;
  87. VP56RangeCoder c;
  88. VP56RangeCoder cc;
  89. VP56RangeCoder *ccp;
  90. int sub_version;
  91. /* frame info */
  92. int plane_width[4];
  93. int plane_height[4];
  94. int mb_width; /* number of horizontal MB */
  95. int mb_height; /* number of vertical MB */
  96. int block_offset[6];
  97. int quantizer;
  98. uint16_t dequant_dc;
  99. uint16_t dequant_ac;
  100. int8_t *qscale_table;
  101. /* DC predictors management */
  102. VP56RefDc *above_blocks;
  103. VP56RefDc left_block[4];
  104. int above_block_idx[6];
  105. DCTELEM prev_dc[3][3]; /* [plan][ref_frame] */
  106. /* blocks / macroblock */
  107. VP56mb mb_type;
  108. VP56Macroblock *macroblocks;
  109. DECLARE_ALIGNED_16(DCTELEM, block_coeff)[6][64];
  110. /* motion vectors */
  111. VP56mv mv[6]; /* vectors for each block in MB */
  112. VP56mv vector_candidate[2];
  113. int vector_candidate_pos;
  114. /* filtering hints */
  115. int filter_header; /* used in vp6 only */
  116. int deblock_filtering;
  117. int filter_selection;
  118. int filter_mode;
  119. int max_vector_length;
  120. int sample_variance_threshold;
  121. uint8_t coeff_ctx[4][64]; /* used in vp5 only */
  122. uint8_t coeff_ctx_last[4]; /* used in vp5 only */
  123. int has_alpha;
  124. /* upside-down flipping hints */
  125. int flip; /* are we flipping ? */
  126. int frbi; /* first row block index in MB */
  127. int srbi; /* second row block index in MB */
  128. int stride[4]; /* stride for each plan */
  129. const uint8_t *vp56_coord_div;
  130. VP56ParseVectorAdjustment parse_vector_adjustment;
  131. VP56Adjust adjust;
  132. VP56Filter filter;
  133. VP56ParseCoeff parse_coeff;
  134. VP56DefaultModelsInit default_models_init;
  135. VP56ParseVectorModels parse_vector_models;
  136. VP56ParseCoeffModels parse_coeff_models;
  137. VP56ParseHeader parse_header;
  138. VP56Model *modelp;
  139. VP56Model models[2];
  140. /* huffman decoding */
  141. int use_huffman;
  142. GetBitContext gb;
  143. VLC dccv_vlc[2];
  144. VLC runv_vlc[2];
  145. VLC ract_vlc[2][3][6];
  146. unsigned int nb_null[2][2]; /* number of consecutive NULL DC/AC */
  147. };
  148. void vp56_init(AVCodecContext *avctx, int flip, int has_alpha);
  149. int vp56_free(AVCodecContext *avctx);
  150. void vp56_init_dequant(VP56Context *s, int quantizer);
  151. int vp56_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  152. AVPacket *avpkt);
  153. /**
  154. * vp56 specific range coder implementation
  155. */
  156. static inline void vp56_init_range_decoder(VP56RangeCoder *c,
  157. const uint8_t *buf, int buf_size)
  158. {
  159. c->high = 255;
  160. c->bits = 8;
  161. c->buffer = buf;
  162. c->end = buf + buf_size;
  163. c->code_word = bytestream_get_be16(&c->buffer);
  164. }
  165. static inline int vp56_rac_get_prob(VP56RangeCoder *c, uint8_t prob)
  166. {
  167. unsigned int low = 1 + (((c->high - 1) * prob) / 256);
  168. unsigned int low_shift = low << 8;
  169. int bit = c->code_word >= low_shift;
  170. if (bit) {
  171. c->high -= low;
  172. c->code_word -= low_shift;
  173. } else {
  174. c->high = low;
  175. }
  176. /* normalize */
  177. while (c->high < 128) {
  178. c->high <<= 1;
  179. c->code_word <<= 1;
  180. if (--c->bits == 0 && c->buffer < c->end) {
  181. c->bits = 8;
  182. c->code_word |= *c->buffer++;
  183. }
  184. }
  185. return bit;
  186. }
  187. static inline int vp56_rac_get(VP56RangeCoder *c)
  188. {
  189. /* equiprobable */
  190. int low = (c->high + 1) >> 1;
  191. unsigned int low_shift = low << 8;
  192. int bit = c->code_word >= low_shift;
  193. if (bit) {
  194. c->high = (c->high - low) << 1;
  195. c->code_word -= low_shift;
  196. } else {
  197. c->high = low << 1;
  198. }
  199. /* normalize */
  200. c->code_word <<= 1;
  201. if (--c->bits == 0 && c->buffer < c->end) {
  202. c->bits = 8;
  203. c->code_word |= *c->buffer++;
  204. }
  205. return bit;
  206. }
  207. static inline int vp56_rac_gets(VP56RangeCoder *c, int bits)
  208. {
  209. int value = 0;
  210. while (bits--) {
  211. value = (value << 1) | vp56_rac_get(c);
  212. }
  213. return value;
  214. }
  215. static inline int vp56_rac_gets_nn(VP56RangeCoder *c, int bits)
  216. {
  217. int v = vp56_rac_gets(c, 7) << 1;
  218. return v + !v;
  219. }
  220. static inline int vp56_rac_get_tree(VP56RangeCoder *c,
  221. const VP56Tree *tree,
  222. const uint8_t *probs)
  223. {
  224. while (tree->val > 0) {
  225. if (vp56_rac_get_prob(c, probs[tree->prob_idx]))
  226. tree += tree->val;
  227. else
  228. tree++;
  229. }
  230. return -tree->val;
  231. }
  232. #endif /* AVCODEC_VP56_H */