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.

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