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.

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