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.

249 lines
7.3KB

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