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.

261 lines
7.4KB

  1. /**
  2. * @file 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 VP56_H
  24. #define VP56_H
  25. #include "vp56data.h"
  26. #include "dsputil.h"
  27. #include "mpegvideo.h"
  28. #include "bytestream.h"
  29. typedef struct vp56_context vp56_context_t;
  30. typedef struct vp56_mv vp56_mv_t;
  31. typedef void (*vp56_parse_vector_adjustment_t)(vp56_context_t *s,
  32. vp56_mv_t *vect);
  33. typedef int (*vp56_adjust_t)(int v, int t);
  34. typedef void (*vp56_filter_t)(vp56_context_t *s, uint8_t *dst, uint8_t *src,
  35. int offset1, int offset2, int stride,
  36. vp56_mv_t mv, int mask, int select, int luma);
  37. typedef void (*vp56_parse_coeff_t)(vp56_context_t *s);
  38. typedef void (*vp56_default_models_init_t)(vp56_context_t *s);
  39. typedef void (*vp56_parse_vector_models_t)(vp56_context_t *s);
  40. typedef void (*vp56_parse_coeff_models_t)(vp56_context_t *s);
  41. typedef int (*vp56_parse_header_t)(vp56_context_t *s, uint8_t *buf,
  42. int buf_size, int *golden_frame);
  43. typedef struct {
  44. int high;
  45. int bits;
  46. uint8_t *buffer;
  47. unsigned long code_word;
  48. } vp56_range_coder_t;
  49. typedef struct {
  50. uint8_t not_null_dc;
  51. vp56_frame_t ref_frame;
  52. DCTELEM dc_coeff;
  53. } vp56_ref_dc_t;
  54. struct vp56_mv {
  55. int x;
  56. int y;
  57. };
  58. typedef struct {
  59. uint8_t type;
  60. vp56_mv_t mv;
  61. } vp56_macroblock_t;
  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. } vp56_model_t;
  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. vp56_range_coder_t c;
  87. vp56_range_coder_t cc;
  88. vp56_range_coder_t *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. vp56_ref_dc_t *above_blocks;
  101. vp56_ref_dc_t left_block[4];
  102. int above_block_idx[6];
  103. DCTELEM prev_dc[3][3]; /* [plan][ref_frame] */
  104. /* blocks / macroblock */
  105. vp56_mb_t mb_type;
  106. vp56_macroblock_t *macroblocks;
  107. DECLARE_ALIGNED_16(DCTELEM, block_coeff[6][64]);
  108. /* motion vectors */
  109. vp56_mv_t mv[6]; /* vectors for each block in MB */
  110. vp56_mv_t 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. vp56_parse_vector_adjustment_t parse_vector_adjustment;
  129. vp56_adjust_t adjust;
  130. vp56_filter_t filter;
  131. vp56_parse_coeff_t parse_coeff;
  132. vp56_default_models_init_t default_models_init;
  133. vp56_parse_vector_models_t parse_vector_models;
  134. vp56_parse_coeff_models_t parse_coeff_models;
  135. vp56_parse_header_t parse_header;
  136. vp56_model_t *modelp;
  137. vp56_model_t models[2];
  138. };
  139. void vp56_init(AVCodecContext *avctx, int flip, int has_alpha);
  140. int vp56_free(AVCodecContext *avctx);
  141. void vp56_init_dequant(vp56_context_t *s, int quantizer);
  142. int vp56_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  143. uint8_t *buf, int buf_size);
  144. /**
  145. * vp56 specific range coder implementation
  146. */
  147. static inline void vp56_init_range_decoder(vp56_range_coder_t *c,
  148. uint8_t *buf, int buf_size)
  149. {
  150. c->high = 255;
  151. c->bits = 8;
  152. c->buffer = buf;
  153. c->code_word = bytestream_get_be16(&c->buffer);
  154. }
  155. static inline int vp56_rac_get_prob(vp56_range_coder_t *c, uint8_t prob)
  156. {
  157. unsigned int low = 1 + (((c->high - 1) * prob) / 256);
  158. unsigned int low_shift = low << 8;
  159. int bit = c->code_word >= low_shift;
  160. if (bit) {
  161. c->high -= low;
  162. c->code_word -= low_shift;
  163. } else {
  164. c->high = low;
  165. }
  166. /* normalize */
  167. while (c->high < 128) {
  168. c->high <<= 1;
  169. c->code_word <<= 1;
  170. if (--c->bits == 0) {
  171. c->bits = 8;
  172. c->code_word |= *c->buffer++;
  173. }
  174. }
  175. return bit;
  176. }
  177. static inline int vp56_rac_get(vp56_range_coder_t *c)
  178. {
  179. /* equiprobable */
  180. int low = (c->high + 1) >> 1;
  181. unsigned int low_shift = low << 8;
  182. int bit = c->code_word >= low_shift;
  183. if (bit) {
  184. c->high = (c->high - low) << 1;
  185. c->code_word -= low_shift;
  186. } else {
  187. c->high = low << 1;
  188. }
  189. /* normalize */
  190. c->code_word <<= 1;
  191. if (--c->bits == 0) {
  192. c->bits = 8;
  193. c->code_word |= *c->buffer++;
  194. }
  195. return bit;
  196. }
  197. static inline int vp56_rac_gets(vp56_range_coder_t *c, int bits)
  198. {
  199. int value = 0;
  200. while (bits--) {
  201. value = (value << 1) | vp56_rac_get(c);
  202. }
  203. return value;
  204. }
  205. static inline int vp56_rac_gets_nn(vp56_range_coder_t *c, int bits)
  206. {
  207. int v = vp56_rac_gets(c, 7) << 1;
  208. return v + !v;
  209. }
  210. static inline int vp56_rac_get_tree(vp56_range_coder_t *c,
  211. const vp56_tree_t *tree,
  212. const uint8_t *probs)
  213. {
  214. while (tree->val > 0) {
  215. if (vp56_rac_get_prob(c, probs[tree->prob_idx]))
  216. tree += tree->val;
  217. else
  218. tree++;
  219. }
  220. return -tree->val;
  221. }
  222. #endif /* VP56_H */