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.

271 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. /* DC predictors management */
  101. VP56RefDc *above_blocks;
  102. VP56RefDc left_block[4];
  103. int above_block_idx[6];
  104. DCTELEM prev_dc[3][3]; /* [plan][ref_frame] */
  105. /* blocks / macroblock */
  106. VP56mb mb_type;
  107. VP56Macroblock *macroblocks;
  108. DECLARE_ALIGNED_16(DCTELEM, block_coeff[6][64]);
  109. /* motion vectors */
  110. VP56mv mv[6]; /* vectors for each block in MB */
  111. VP56mv vector_candidate[2];
  112. int vector_candidate_pos;
  113. /* filtering hints */
  114. int filter_header; /* used in vp6 only */
  115. int deblock_filtering;
  116. int filter_selection;
  117. int filter_mode;
  118. int max_vector_length;
  119. int sample_variance_threshold;
  120. uint8_t coeff_ctx[4][64]; /* used in vp5 only */
  121. uint8_t coeff_ctx_last[4]; /* used in vp5 only */
  122. int has_alpha;
  123. /* upside-down flipping hints */
  124. int flip; /* are we flipping ? */
  125. int frbi; /* first row block index in MB */
  126. int srbi; /* second row block index in MB */
  127. int stride[4]; /* stride for each plan */
  128. const uint8_t *vp56_coord_div;
  129. VP56ParseVectorAdjustment parse_vector_adjustment;
  130. VP56Adjust adjust;
  131. VP56Filter filter;
  132. VP56ParseCoeff parse_coeff;
  133. VP56DefaultModelsInit default_models_init;
  134. VP56ParseVectorModels parse_vector_models;
  135. VP56ParseCoeffModels parse_coeff_models;
  136. VP56ParseHeader parse_header;
  137. VP56Model *modelp;
  138. VP56Model models[2];
  139. /* huffman decoding */
  140. int use_huffman;
  141. GetBitContext gb;
  142. VLC dccv_vlc[2];
  143. VLC runv_vlc[2];
  144. VLC ract_vlc[2][3][6];
  145. unsigned int nb_null[2][2]; /* number of consecutive NULL DC/AC */
  146. };
  147. void vp56_init(AVCodecContext *avctx, int flip, int has_alpha);
  148. int vp56_free(AVCodecContext *avctx);
  149. void vp56_init_dequant(VP56Context *s, int quantizer);
  150. int vp56_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  151. AVPacket *avpkt);
  152. /**
  153. * vp56 specific range coder implementation
  154. */
  155. static inline void vp56_init_range_decoder(VP56RangeCoder *c,
  156. const uint8_t *buf, int buf_size)
  157. {
  158. c->high = 255;
  159. c->bits = 8;
  160. c->buffer = buf;
  161. c->end = buf + buf_size;
  162. c->code_word = bytestream_get_be16(&c->buffer);
  163. }
  164. static inline int vp56_rac_get_prob(VP56RangeCoder *c, uint8_t prob)
  165. {
  166. unsigned int low = 1 + (((c->high - 1) * prob) / 256);
  167. unsigned int low_shift = low << 8;
  168. int bit = c->code_word >= low_shift;
  169. if (bit) {
  170. c->high -= low;
  171. c->code_word -= low_shift;
  172. } else {
  173. c->high = low;
  174. }
  175. /* normalize */
  176. while (c->high < 128) {
  177. c->high <<= 1;
  178. c->code_word <<= 1;
  179. if (--c->bits == 0 && c->buffer < c->end) {
  180. c->bits = 8;
  181. c->code_word |= *c->buffer++;
  182. }
  183. }
  184. return bit;
  185. }
  186. static inline int vp56_rac_get(VP56RangeCoder *c)
  187. {
  188. /* equiprobable */
  189. int low = (c->high + 1) >> 1;
  190. unsigned int low_shift = low << 8;
  191. int bit = c->code_word >= low_shift;
  192. if (bit) {
  193. c->high = (c->high - low) << 1;
  194. c->code_word -= low_shift;
  195. } else {
  196. c->high = low << 1;
  197. }
  198. /* normalize */
  199. c->code_word <<= 1;
  200. if (--c->bits == 0 && c->buffer < c->end) {
  201. c->bits = 8;
  202. c->code_word |= *c->buffer++;
  203. }
  204. return bit;
  205. }
  206. static inline int vp56_rac_gets(VP56RangeCoder *c, int bits)
  207. {
  208. int value = 0;
  209. while (bits--) {
  210. value = (value << 1) | vp56_rac_get(c);
  211. }
  212. return value;
  213. }
  214. static inline int vp56_rac_gets_nn(VP56RangeCoder *c, int bits)
  215. {
  216. int v = vp56_rac_gets(c, 7) << 1;
  217. return v + !v;
  218. }
  219. static inline int vp56_rac_get_tree(VP56RangeCoder *c,
  220. const VP56Tree *tree,
  221. const uint8_t *probs)
  222. {
  223. while (tree->val > 0) {
  224. if (vp56_rac_get_prob(c, probs[tree->prob_idx]))
  225. tree += tree->val;
  226. else
  227. tree++;
  228. }
  229. return -tree->val;
  230. }
  231. #endif /* AVCODEC_VP56_H */