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 library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #ifndef VP56_H
  22. #define VP56_H
  23. #include <stdint.h>
  24. #include "vp56data.h"
  25. #include "dsputil.h"
  26. #include "mpegvideo.h"
  27. typedef struct vp56_context vp56_context_t;
  28. typedef struct vp56_mv vp56_mv_t;
  29. typedef void (*vp56_parse_vector_adjustment_t)(vp56_context_t *s,
  30. vp56_mv_t *vector);
  31. typedef int (*vp56_adjust_t)(int v, int t);
  32. typedef void (*vp56_filter_t)(vp56_context_t *s, uint8_t *dst, uint8_t *src,
  33. int offset1, int offset2, int stride,
  34. vp56_mv_t mv, int mask, int select, int luma);
  35. typedef void (*vp56_parse_coeff_t)(vp56_context_t *s);
  36. typedef void (*vp56_default_models_init_t)(vp56_context_t *s);
  37. typedef void (*vp56_parse_vector_models_t)(vp56_context_t *s);
  38. typedef void (*vp56_parse_coeff_models_t)(vp56_context_t *s);
  39. typedef int (*vp56_parse_header_t)(vp56_context_t *s, uint8_t *buf,
  40. int buf_size, int *golden_frame);
  41. typedef struct {
  42. int high;
  43. int bits;
  44. const uint8_t *buffer;
  45. unsigned long code_word;
  46. } vp56_range_coder_t;
  47. typedef struct {
  48. uint8_t not_null_dc;
  49. vp56_frame_t ref_frame;
  50. DCTELEM dc_coeff;
  51. } vp56_ref_dc_t;
  52. struct vp56_mv {
  53. int x;
  54. int y;
  55. };
  56. typedef struct {
  57. uint8_t type;
  58. vp56_mv_t mv;
  59. } vp56_macroblock_t;
  60. struct vp56_context {
  61. AVCodecContext *avctx;
  62. DSPContext dsp;
  63. ScanTable scantable;
  64. AVFrame frames[3];
  65. uint8_t *edge_emu_buffer_alloc;
  66. uint8_t *edge_emu_buffer;
  67. vp56_range_coder_t c;
  68. /* frame info */
  69. int plane_width[3];
  70. int plane_height[3];
  71. int mb_width; /* number of horizontal MB */
  72. int mb_height; /* number of vertical MB */
  73. int block_offset[6];
  74. int quantizer;
  75. uint16_t dequant_dc;
  76. uint16_t dequant_ac;
  77. /* DC predictors management */
  78. vp56_ref_dc_t *above_blocks;
  79. vp56_ref_dc_t left_block[4];
  80. int above_block_idx[6];
  81. DCTELEM prev_dc[3][3]; /* [plan][ref_frame] */
  82. /* blocks / macroblock */
  83. vp56_mb_t mb_type;
  84. vp56_macroblock_t *macroblocks;
  85. DECLARE_ALIGNED_16(DCTELEM, block_coeff[6][64]);
  86. uint8_t coeff_reorder[64]; /* used in vp6 only */
  87. uint8_t coeff_index_to_pos[64]; /* used in vp6 only */
  88. /* motion vectors */
  89. vp56_mv_t mv[6]; /* vectors for each block in MB */
  90. vp56_mv_t vector_candidate[2];
  91. int vector_candidate_pos;
  92. /* filtering hints */
  93. int deblock_filtering;
  94. int filter_selection;
  95. int filter_mode;
  96. int max_vector_length;
  97. int sample_variance_threshold;
  98. /* AC models */
  99. uint8_t vector_model_sig[2]; /* delta sign */
  100. uint8_t vector_model_dct[2]; /* delta coding types */
  101. uint8_t vector_model_pdi[2][2]; /* predefined delta init */
  102. uint8_t vector_model_pdv[2][7]; /* predefined delta values */
  103. uint8_t vector_model_fdv[2][8]; /* 8 bit delta value definition */
  104. uint8_t mb_type_model[3][10][10]; /* model for decoding MB type */
  105. uint8_t coeff_model_dccv[2][11]; /* DC coeff value */
  106. uint8_t coeff_model_ract[2][3][6][11]; /* Run/AC coding type and AC coeff value */
  107. uint8_t coeff_model_acct[2][3][3][6][5];/* vp5 only AC coding type for coding group < 3 */
  108. uint8_t coeff_model_dcct[2][36][5]; /* DC coeff coding type */
  109. uint8_t coeff_model_runv[2][14]; /* run value (vp6 only) */
  110. uint8_t mb_types_stats[3][10][2]; /* contextual, next MB type stats */
  111. uint8_t coeff_ctx[4][64]; /* used in vp5 only */
  112. uint8_t coeff_ctx_last[4]; /* used in vp5 only */
  113. /* upside-down flipping hints */
  114. int flip; /* are we flipping ? */
  115. int frbi; /* first row block index in MB */
  116. int srbi; /* second row block index in MB */
  117. int stride[3]; /* stride for each plan */
  118. const uint8_t *vp56_coord_div;
  119. vp56_parse_vector_adjustment_t parse_vector_adjustment;
  120. vp56_adjust_t adjust;
  121. vp56_filter_t filter;
  122. vp56_parse_coeff_t parse_coeff;
  123. vp56_default_models_init_t default_models_init;
  124. vp56_parse_vector_models_t parse_vector_models;
  125. vp56_parse_coeff_models_t parse_coeff_models;
  126. vp56_parse_header_t parse_header;
  127. };
  128. void vp56_init(vp56_context_t *s, AVCodecContext *avctx, int flip);
  129. int vp56_free(AVCodecContext *avctx);
  130. void vp56_init_dequant(vp56_context_t *s, int quantizer);
  131. int vp56_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  132. uint8_t *buf, int buf_size);
  133. /**
  134. * vp56 specific range coder implementation
  135. */
  136. static inline void vp56_init_range_decoder(vp56_range_coder_t *c,
  137. const uint8_t *buf, int buf_size)
  138. {
  139. c->high = 255;
  140. c->bits = 8;
  141. c->buffer = buf;
  142. c->code_word = *c->buffer++ << 8;
  143. c->code_word |= *c->buffer++;
  144. }
  145. static inline int vp56_rac_get_prob(vp56_range_coder_t *c, uint8_t prob)
  146. {
  147. unsigned int low = 1 + (((c->high - 1) * prob) / 256);
  148. unsigned int low_shift = low << 8;
  149. int bit = c->code_word >= low_shift;
  150. if (bit) {
  151. c->high -= low;
  152. c->code_word -= low_shift;
  153. } else {
  154. c->high = low;
  155. }
  156. /* normalize */
  157. while (c->high < 128) {
  158. c->high <<= 1;
  159. c->code_word <<= 1;
  160. if (--c->bits == 0) {
  161. c->bits = 8;
  162. c->code_word |= *c->buffer++;
  163. }
  164. }
  165. return bit;
  166. }
  167. static inline int vp56_rac_get(vp56_range_coder_t *c)
  168. {
  169. /* equiprobable */
  170. int low = (c->high + 1) >> 1;
  171. unsigned int low_shift = low << 8;
  172. int bit = c->code_word >= low_shift;
  173. if (bit) {
  174. c->high = (c->high - low) << 1;
  175. c->code_word -= low_shift;
  176. } else {
  177. c->high = low << 1;
  178. }
  179. /* normalize */
  180. c->code_word <<= 1;
  181. if (--c->bits == 0) {
  182. c->bits = 8;
  183. c->code_word |= *c->buffer++;
  184. }
  185. return bit;
  186. }
  187. static inline int vp56_rac_gets(vp56_range_coder_t *c, int bits)
  188. {
  189. int value = 0;
  190. while (bits--) {
  191. value = (value << 1) | vp56_rac_get(c);
  192. }
  193. return value;
  194. }
  195. static inline int vp56_rac_gets_nn(vp56_range_coder_t *c, int bits)
  196. {
  197. int v = vp56_rac_gets(c, 7) << 1;
  198. return v + !v;
  199. }
  200. static inline int vp56_rac_get_tree(vp56_range_coder_t *c,
  201. const vp56_tree_t *tree,
  202. const uint8_t *probs)
  203. {
  204. while (tree->val > 0) {
  205. if (vp56_rac_get_prob(c, probs[tree->prob_idx]))
  206. tree += tree->val;
  207. else
  208. tree++;
  209. }
  210. return -tree->val;
  211. }
  212. #endif /* VP56_H */