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.

375 lines
10KB

  1. /*
  2. * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * VP5 and VP6 compatible video decoder (common features)
  23. */
  24. #ifndef AVCODEC_VP56_H
  25. #define AVCODEC_VP56_H
  26. #include "vp56data.h"
  27. #include "dsputil.h"
  28. #include "get_bits.h"
  29. #include "bytestream.h"
  30. #include "cabac.h"
  31. #include "vp56dsp.h"
  32. typedef struct vp56_context VP56Context;
  33. typedef struct {
  34. int16_t x;
  35. int16_t y;
  36. } DECLARE_ALIGNED(4, , VP56mv);
  37. typedef void (*VP56ParseVectorAdjustment)(VP56Context *s,
  38. VP56mv *vect);
  39. typedef void (*VP56Filter)(VP56Context *s, uint8_t *dst, uint8_t *src,
  40. int offset1, int offset2, int stride,
  41. VP56mv mv, int mask, int select, int luma);
  42. typedef void (*VP56ParseCoeff)(VP56Context *s);
  43. typedef void (*VP56DefaultModelsInit)(VP56Context *s);
  44. typedef void (*VP56ParseVectorModels)(VP56Context *s);
  45. typedef int (*VP56ParseCoeffModels)(VP56Context *s);
  46. typedef int (*VP56ParseHeader)(VP56Context *s, const uint8_t *buf,
  47. int buf_size, int *golden_frame);
  48. typedef struct {
  49. int high;
  50. int bits; /* stored negated (i.e. negative "bits" is a positive number of
  51. bits left) in order to eliminate a negate in cache refilling */
  52. const uint8_t *buffer;
  53. const uint8_t *end;
  54. unsigned int code_word;
  55. } VP56RangeCoder;
  56. typedef struct {
  57. uint8_t not_null_dc;
  58. VP56Frame ref_frame;
  59. DCTELEM dc_coeff;
  60. } VP56RefDc;
  61. typedef struct {
  62. uint8_t type;
  63. VP56mv mv;
  64. } VP56Macroblock;
  65. typedef struct {
  66. uint8_t coeff_reorder[64]; /* used in vp6 only */
  67. uint8_t coeff_index_to_pos[64]; /* used in vp6 only */
  68. uint8_t vector_sig[2]; /* delta sign */
  69. uint8_t vector_dct[2]; /* delta coding types */
  70. uint8_t vector_pdi[2][2]; /* predefined delta init */
  71. uint8_t vector_pdv[2][7]; /* predefined delta values */
  72. uint8_t vector_fdv[2][8]; /* 8 bit delta value definition */
  73. uint8_t coeff_dccv[2][11]; /* DC coeff value */
  74. uint8_t coeff_ract[2][3][6][11]; /* Run/AC coding type and AC coeff value */
  75. uint8_t coeff_acct[2][3][3][6][5];/* vp5 only AC coding type for coding group < 3 */
  76. uint8_t coeff_dcct[2][36][5]; /* DC coeff coding type */
  77. uint8_t coeff_runv[2][14]; /* run value (vp6 only) */
  78. uint8_t mb_type[3][10][10]; /* model for decoding MB type */
  79. uint8_t mb_types_stats[3][10][2];/* contextual, next MB type stats */
  80. } VP56Model;
  81. struct vp56_context {
  82. AVCodecContext *avctx;
  83. DSPContext dsp;
  84. VP56DSPContext vp56dsp;
  85. ScanTable scantable;
  86. AVFrame frames[4];
  87. AVFrame *framep[6];
  88. uint8_t *edge_emu_buffer_alloc;
  89. uint8_t *edge_emu_buffer;
  90. VP56RangeCoder c;
  91. VP56RangeCoder cc;
  92. VP56RangeCoder *ccp;
  93. int sub_version;
  94. /* frame info */
  95. int plane_width[4];
  96. int plane_height[4];
  97. int mb_width; /* number of horizontal MB */
  98. int mb_height; /* number of vertical MB */
  99. int block_offset[6];
  100. int quantizer;
  101. uint16_t dequant_dc;
  102. uint16_t dequant_ac;
  103. int8_t *qscale_table;
  104. /* DC predictors management */
  105. VP56RefDc *above_blocks;
  106. VP56RefDc left_block[4];
  107. int above_block_idx[6];
  108. DCTELEM prev_dc[3][3]; /* [plan][ref_frame] */
  109. /* blocks / macroblock */
  110. VP56mb mb_type;
  111. VP56Macroblock *macroblocks;
  112. DECLARE_ALIGNED(16, DCTELEM, block_coeff)[6][64];
  113. /* motion vectors */
  114. VP56mv mv[6]; /* vectors for each block in MB */
  115. VP56mv vector_candidate[2];
  116. int vector_candidate_pos;
  117. /* filtering hints */
  118. int filter_header; /* used in vp6 only */
  119. int deblock_filtering;
  120. int filter_selection;
  121. int filter_mode;
  122. int max_vector_length;
  123. int sample_variance_threshold;
  124. uint8_t coeff_ctx[4][64]; /* used in vp5 only */
  125. uint8_t coeff_ctx_last[4]; /* used in vp5 only */
  126. int has_alpha;
  127. /* upside-down flipping hints */
  128. int flip; /* are we flipping ? */
  129. int frbi; /* first row block index in MB */
  130. int srbi; /* second row block index in MB */
  131. int stride[4]; /* stride for each plan */
  132. const uint8_t *vp56_coord_div;
  133. VP56ParseVectorAdjustment parse_vector_adjustment;
  134. VP56Filter filter;
  135. VP56ParseCoeff parse_coeff;
  136. VP56DefaultModelsInit default_models_init;
  137. VP56ParseVectorModels parse_vector_models;
  138. VP56ParseCoeffModels parse_coeff_models;
  139. VP56ParseHeader parse_header;
  140. VP56Model *modelp;
  141. VP56Model models[2];
  142. /* huffman decoding */
  143. int use_huffman;
  144. GetBitContext gb;
  145. VLC dccv_vlc[2];
  146. VLC runv_vlc[2];
  147. VLC ract_vlc[2][3][6];
  148. unsigned int nb_null[2][2]; /* number of consecutive NULL DC/AC */
  149. };
  150. void ff_vp56_init(AVCodecContext *avctx, int flip, int has_alpha);
  151. int ff_vp56_free(AVCodecContext *avctx);
  152. void ff_vp56_init_dequant(VP56Context *s, int quantizer);
  153. int ff_vp56_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  154. AVPacket *avpkt);
  155. /**
  156. * vp56 specific range coder implementation
  157. */
  158. extern const uint8_t ff_vp56_norm_shift[256];
  159. void ff_vp56_init_range_decoder(VP56RangeCoder *c, const uint8_t *buf, int buf_size);
  160. static av_always_inline unsigned int vp56_rac_renorm(VP56RangeCoder *c)
  161. {
  162. int shift = ff_vp56_norm_shift[c->high];
  163. int bits = c->bits;
  164. unsigned int code_word = c->code_word;
  165. c->high <<= shift;
  166. code_word <<= shift;
  167. bits += shift;
  168. if(bits >= 0 && c->buffer < c->end) {
  169. code_word |= bytestream_get_be16(&c->buffer) << bits;
  170. bits -= 16;
  171. }
  172. c->bits = bits;
  173. return code_word;
  174. }
  175. #if ARCH_ARM
  176. #include "arm/vp56_arith.h"
  177. #elif ARCH_X86
  178. #include "x86/vp56_arith.h"
  179. #endif
  180. #ifndef vp56_rac_get_prob
  181. #define vp56_rac_get_prob vp56_rac_get_prob
  182. static av_always_inline int vp56_rac_get_prob(VP56RangeCoder *c, uint8_t prob)
  183. {
  184. unsigned int code_word = vp56_rac_renorm(c);
  185. unsigned int low = 1 + (((c->high - 1) * prob) >> 8);
  186. unsigned int low_shift = low << 16;
  187. int bit = code_word >= low_shift;
  188. c->high = bit ? c->high - low : low;
  189. c->code_word = bit ? code_word - low_shift : code_word;
  190. return bit;
  191. }
  192. #endif
  193. #ifndef vp56_rac_get_prob_branchy
  194. // branchy variant, to be used where there's a branch based on the bit decoded
  195. static av_always_inline int vp56_rac_get_prob_branchy(VP56RangeCoder *c, int prob)
  196. {
  197. unsigned long code_word = vp56_rac_renorm(c);
  198. unsigned low = 1 + (((c->high - 1) * prob) >> 8);
  199. unsigned low_shift = low << 16;
  200. if (code_word >= low_shift) {
  201. c->high -= low;
  202. c->code_word = code_word - low_shift;
  203. return 1;
  204. }
  205. c->high = low;
  206. c->code_word = code_word;
  207. return 0;
  208. }
  209. #endif
  210. static av_always_inline int vp56_rac_get(VP56RangeCoder *c)
  211. {
  212. unsigned int code_word = vp56_rac_renorm(c);
  213. /* equiprobable */
  214. int low = (c->high + 1) >> 1;
  215. unsigned int low_shift = low << 16;
  216. int bit = code_word >= low_shift;
  217. if (bit) {
  218. c->high -= low;
  219. code_word -= low_shift;
  220. } else {
  221. c->high = low;
  222. }
  223. c->code_word = code_word;
  224. return bit;
  225. }
  226. // rounding is different than vp56_rac_get, is vp56_rac_get wrong?
  227. static av_always_inline int vp8_rac_get(VP56RangeCoder *c)
  228. {
  229. return vp56_rac_get_prob(c, 128);
  230. }
  231. static av_unused int vp56_rac_gets(VP56RangeCoder *c, int bits)
  232. {
  233. int value = 0;
  234. while (bits--) {
  235. value = (value << 1) | vp56_rac_get(c);
  236. }
  237. return value;
  238. }
  239. static av_unused int vp8_rac_get_uint(VP56RangeCoder *c, int bits)
  240. {
  241. int value = 0;
  242. while (bits--) {
  243. value = (value << 1) | vp8_rac_get(c);
  244. }
  245. return value;
  246. }
  247. // fixme: add 1 bit to all the calls to this?
  248. static av_unused int vp8_rac_get_sint(VP56RangeCoder *c, int bits)
  249. {
  250. int v;
  251. if (!vp8_rac_get(c))
  252. return 0;
  253. v = vp8_rac_get_uint(c, bits);
  254. if (vp8_rac_get(c))
  255. v = -v;
  256. return v;
  257. }
  258. // P(7)
  259. static av_unused int vp56_rac_gets_nn(VP56RangeCoder *c, int bits)
  260. {
  261. int v = vp56_rac_gets(c, 7) << 1;
  262. return v + !v;
  263. }
  264. static av_unused int vp8_rac_get_nn(VP56RangeCoder *c)
  265. {
  266. int v = vp8_rac_get_uint(c, 7) << 1;
  267. return v + !v;
  268. }
  269. static av_always_inline
  270. int vp56_rac_get_tree(VP56RangeCoder *c,
  271. const VP56Tree *tree,
  272. const uint8_t *probs)
  273. {
  274. while (tree->val > 0) {
  275. if (vp56_rac_get_prob(c, probs[tree->prob_idx]))
  276. tree += tree->val;
  277. else
  278. tree++;
  279. }
  280. return -tree->val;
  281. }
  282. /**
  283. * This is identical to vp8_rac_get_tree except for the possibility of starting
  284. * on a node other than the root node, needed for coeff decode where this is
  285. * used to save a bit after a 0 token (by disallowing EOB to immediately follow.)
  286. */
  287. static av_always_inline
  288. int vp8_rac_get_tree_with_offset(VP56RangeCoder *c, const int8_t (*tree)[2],
  289. const uint8_t *probs, int i)
  290. {
  291. do {
  292. i = tree[i][vp56_rac_get_prob(c, probs[i])];
  293. } while (i > 0);
  294. return -i;
  295. }
  296. // how probabilities are associated with decisions is different I think
  297. // well, the new scheme fits in the old but this way has one fewer branches per decision
  298. static av_always_inline
  299. int vp8_rac_get_tree(VP56RangeCoder *c, const int8_t (*tree)[2],
  300. const uint8_t *probs)
  301. {
  302. return vp8_rac_get_tree_with_offset(c, tree, probs, 0);
  303. }
  304. // DCTextra
  305. static av_always_inline int vp8_rac_get_coeff(VP56RangeCoder *c, const uint8_t *prob)
  306. {
  307. int v = 0;
  308. do {
  309. v = (v<<1) + vp56_rac_get_prob(c, *prob++);
  310. } while (*prob);
  311. return v;
  312. }
  313. #endif /* AVCODEC_VP56_H */