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.

253 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 St, 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. typedef struct vp56_context vp56_context_t;
  29. typedef struct vp56_mv vp56_mv_t;
  30. typedef void (*vp56_parse_vector_adjustment_t)(vp56_context_t *s,
  31. vp56_mv_t *vect);
  32. typedef int (*vp56_adjust_t)(int v, int t);
  33. typedef void (*vp56_filter_t)(vp56_context_t *s, uint8_t *dst, uint8_t *src,
  34. int offset1, int offset2, int stride,
  35. vp56_mv_t mv, int mask, int select, int luma);
  36. typedef void (*vp56_parse_coeff_t)(vp56_context_t *s);
  37. typedef void (*vp56_default_models_init_t)(vp56_context_t *s);
  38. typedef void (*vp56_parse_vector_models_t)(vp56_context_t *s);
  39. typedef void (*vp56_parse_coeff_models_t)(vp56_context_t *s);
  40. typedef int (*vp56_parse_header_t)(vp56_context_t *s, uint8_t *buf,
  41. int buf_size, int *golden_frame);
  42. typedef struct {
  43. int high;
  44. int bits;
  45. const uint8_t *buffer;
  46. unsigned long code_word;
  47. } vp56_range_coder_t;
  48. typedef struct {
  49. uint8_t not_null_dc;
  50. vp56_frame_t ref_frame;
  51. DCTELEM dc_coeff;
  52. } vp56_ref_dc_t;
  53. struct vp56_mv {
  54. int x;
  55. int y;
  56. };
  57. typedef struct {
  58. uint8_t type;
  59. vp56_mv_t mv;
  60. } vp56_macroblock_t;
  61. struct vp56_context {
  62. AVCodecContext *avctx;
  63. DSPContext dsp;
  64. ScanTable scantable;
  65. AVFrame frames[3];
  66. uint8_t *edge_emu_buffer_alloc;
  67. uint8_t *edge_emu_buffer;
  68. vp56_range_coder_t c;
  69. vp56_range_coder_t cc;
  70. vp56_range_coder_t *ccp;
  71. int sub_version;
  72. /* frame info */
  73. int plane_width[3];
  74. int plane_height[3];
  75. int mb_width; /* number of horizontal MB */
  76. int mb_height; /* number of vertical MB */
  77. int block_offset[6];
  78. int quantizer;
  79. uint16_t dequant_dc;
  80. uint16_t dequant_ac;
  81. /* DC predictors management */
  82. vp56_ref_dc_t *above_blocks;
  83. vp56_ref_dc_t left_block[4];
  84. int above_block_idx[6];
  85. DCTELEM prev_dc[3][3]; /* [plan][ref_frame] */
  86. /* blocks / macroblock */
  87. vp56_mb_t mb_type;
  88. vp56_macroblock_t *macroblocks;
  89. DECLARE_ALIGNED_16(DCTELEM, block_coeff[6][64]);
  90. uint8_t coeff_reorder[64]; /* used in vp6 only */
  91. uint8_t coeff_index_to_pos[64]; /* used in vp6 only */
  92. /* motion vectors */
  93. vp56_mv_t mv[6]; /* vectors for each block in MB */
  94. vp56_mv_t vector_candidate[2];
  95. int vector_candidate_pos;
  96. /* filtering hints */
  97. int filter_header; /* used in vp6 only */
  98. int deblock_filtering;
  99. int filter_selection;
  100. int filter_mode;
  101. int max_vector_length;
  102. int sample_variance_threshold;
  103. /* AC models */
  104. uint8_t vector_model_sig[2]; /* delta sign */
  105. uint8_t vector_model_dct[2]; /* delta coding types */
  106. uint8_t vector_model_pdi[2][2]; /* predefined delta init */
  107. uint8_t vector_model_pdv[2][7]; /* predefined delta values */
  108. uint8_t vector_model_fdv[2][8]; /* 8 bit delta value definition */
  109. uint8_t mb_type_model[3][10][10]; /* model for decoding MB type */
  110. uint8_t coeff_model_dccv[2][11]; /* DC coeff value */
  111. uint8_t coeff_model_ract[2][3][6][11]; /* Run/AC coding type and AC coeff value */
  112. uint8_t coeff_model_acct[2][3][3][6][5];/* vp5 only AC coding type for coding group < 3 */
  113. uint8_t coeff_model_dcct[2][36][5]; /* DC coeff coding type */
  114. uint8_t coeff_model_runv[2][14]; /* run value (vp6 only) */
  115. uint8_t mb_types_stats[3][10][2]; /* contextual, next MB type stats */
  116. uint8_t coeff_ctx[4][64]; /* used in vp5 only */
  117. uint8_t coeff_ctx_last[4]; /* used in vp5 only */
  118. /* upside-down flipping hints */
  119. int flip; /* are we flipping ? */
  120. int frbi; /* first row block index in MB */
  121. int srbi; /* second row block index in MB */
  122. int stride[3]; /* stride for each plan */
  123. const uint8_t *vp56_coord_div;
  124. vp56_parse_vector_adjustment_t parse_vector_adjustment;
  125. vp56_adjust_t adjust;
  126. vp56_filter_t filter;
  127. vp56_parse_coeff_t parse_coeff;
  128. vp56_default_models_init_t default_models_init;
  129. vp56_parse_vector_models_t parse_vector_models;
  130. vp56_parse_coeff_models_t parse_coeff_models;
  131. vp56_parse_header_t parse_header;
  132. };
  133. void vp56_init(vp56_context_t *s, AVCodecContext *avctx, int flip);
  134. int vp56_free(AVCodecContext *avctx);
  135. void vp56_init_dequant(vp56_context_t *s, int quantizer);
  136. int vp56_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  137. uint8_t *buf, int buf_size);
  138. /**
  139. * vp56 specific range coder implementation
  140. */
  141. static inline void vp56_init_range_decoder(vp56_range_coder_t *c,
  142. const uint8_t *buf, int buf_size)
  143. {
  144. c->high = 255;
  145. c->bits = 8;
  146. c->buffer = buf;
  147. c->code_word = *c->buffer++ << 8;
  148. c->code_word |= *c->buffer++;
  149. }
  150. static inline int vp56_rac_get_prob(vp56_range_coder_t *c, uint8_t prob)
  151. {
  152. unsigned int low = 1 + (((c->high - 1) * prob) / 256);
  153. unsigned int low_shift = low << 8;
  154. int bit = c->code_word >= low_shift;
  155. if (bit) {
  156. c->high -= low;
  157. c->code_word -= low_shift;
  158. } else {
  159. c->high = low;
  160. }
  161. /* normalize */
  162. while (c->high < 128) {
  163. c->high <<= 1;
  164. c->code_word <<= 1;
  165. if (--c->bits == 0) {
  166. c->bits = 8;
  167. c->code_word |= *c->buffer++;
  168. }
  169. }
  170. return bit;
  171. }
  172. static inline int vp56_rac_get(vp56_range_coder_t *c)
  173. {
  174. /* equiprobable */
  175. int low = (c->high + 1) >> 1;
  176. unsigned int low_shift = low << 8;
  177. int bit = c->code_word >= low_shift;
  178. if (bit) {
  179. c->high = (c->high - low) << 1;
  180. c->code_word -= low_shift;
  181. } else {
  182. c->high = low << 1;
  183. }
  184. /* normalize */
  185. c->code_word <<= 1;
  186. if (--c->bits == 0) {
  187. c->bits = 8;
  188. c->code_word |= *c->buffer++;
  189. }
  190. return bit;
  191. }
  192. static inline int vp56_rac_gets(vp56_range_coder_t *c, int bits)
  193. {
  194. int value = 0;
  195. while (bits--) {
  196. value = (value << 1) | vp56_rac_get(c);
  197. }
  198. return value;
  199. }
  200. static inline int vp56_rac_gets_nn(vp56_range_coder_t *c, int bits)
  201. {
  202. int v = vp56_rac_gets(c, 7) << 1;
  203. return v + !v;
  204. }
  205. static inline int vp56_rac_get_tree(vp56_range_coder_t *c,
  206. const vp56_tree_t *tree,
  207. const uint8_t *probs)
  208. {
  209. while (tree->val > 0) {
  210. if (vp56_rac_get_prob(c, probs[tree->prob_idx]))
  211. tree += tree->val;
  212. else
  213. tree++;
  214. }
  215. return -tree->val;
  216. }
  217. #endif /* VP56_H */