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.

365 lines
9.8KB

  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 "vp3dsp.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. VP3DSPContext vp3dsp;
  85. VP56DSPContext vp56dsp;
  86. ScanTable scantable;
  87. AVFrame frames[4];
  88. AVFrame *framep[6];
  89. uint8_t *edge_emu_buffer_alloc;
  90. uint8_t *edge_emu_buffer;
  91. VP56RangeCoder c;
  92. VP56RangeCoder cc;
  93. VP56RangeCoder *ccp;
  94. int sub_version;
  95. /* frame info */
  96. int plane_width[4];
  97. int plane_height[4];
  98. int mb_width; /* number of horizontal MB */
  99. int mb_height; /* number of vertical MB */
  100. int block_offset[6];
  101. int quantizer;
  102. uint16_t dequant_dc;
  103. uint16_t dequant_ac;
  104. int8_t *qscale_table;
  105. /* DC predictors management */
  106. VP56RefDc *above_blocks;
  107. VP56RefDc left_block[4];
  108. int above_block_idx[6];
  109. DCTELEM prev_dc[3][3]; /* [plan][ref_frame] */
  110. /* blocks / macroblock */
  111. VP56mb mb_type;
  112. VP56Macroblock *macroblocks;
  113. DECLARE_ALIGNED(16, DCTELEM, block_coeff)[6][64];
  114. /* motion vectors */
  115. VP56mv mv[6]; /* vectors for each block in MB */
  116. VP56mv vector_candidate[2];
  117. int vector_candidate_pos;
  118. /* filtering hints */
  119. int filter_header; /* used in vp6 only */
  120. int deblock_filtering;
  121. int filter_selection;
  122. int filter_mode;
  123. int max_vector_length;
  124. int sample_variance_threshold;
  125. uint8_t coeff_ctx[4][64]; /* used in vp5 only */
  126. uint8_t coeff_ctx_last[4]; /* used in vp5 only */
  127. int has_alpha;
  128. /* upside-down flipping hints */
  129. int flip; /* are we flipping ? */
  130. int frbi; /* first row block index in MB */
  131. int srbi; /* second row block index in MB */
  132. int stride[4]; /* stride for each plan */
  133. const uint8_t *vp56_coord_div;
  134. VP56ParseVectorAdjustment parse_vector_adjustment;
  135. VP56Filter filter;
  136. VP56ParseCoeff parse_coeff;
  137. VP56DefaultModelsInit default_models_init;
  138. VP56ParseVectorModels parse_vector_models;
  139. VP56ParseCoeffModels parse_coeff_models;
  140. VP56ParseHeader parse_header;
  141. VP56Model *modelp;
  142. VP56Model models[2];
  143. /* huffman decoding */
  144. int use_huffman;
  145. GetBitContext gb;
  146. VLC dccv_vlc[2];
  147. VLC runv_vlc[2];
  148. VLC ract_vlc[2][3][6];
  149. unsigned int nb_null[2][2]; /* number of consecutive NULL DC/AC */
  150. };
  151. void ff_vp56_init(AVCodecContext *avctx, int flip, int has_alpha);
  152. int ff_vp56_free(AVCodecContext *avctx);
  153. void ff_vp56_init_dequant(VP56Context *s, int quantizer);
  154. int ff_vp56_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  155. AVPacket *avpkt);
  156. /**
  157. * vp56 specific range coder implementation
  158. */
  159. extern const uint8_t ff_vp56_norm_shift[256];
  160. void ff_vp56_init_range_decoder(VP56RangeCoder *c, const uint8_t *buf, int buf_size);
  161. static av_always_inline unsigned int vp56_rac_renorm(VP56RangeCoder *c)
  162. {
  163. int shift = ff_vp56_norm_shift[c->high];
  164. int bits = c->bits;
  165. unsigned int code_word = c->code_word;
  166. c->high <<= shift;
  167. code_word <<= shift;
  168. bits += shift;
  169. if(bits >= 0 && c->buffer < c->end) {
  170. code_word |= bytestream_get_be16(&c->buffer) << bits;
  171. bits -= 16;
  172. }
  173. c->bits = bits;
  174. return code_word;
  175. }
  176. #if ARCH_ARM
  177. #include "arm/vp56_arith.h"
  178. #elif ARCH_X86
  179. #include "x86/vp56_arith.h"
  180. #endif
  181. #ifndef vp56_rac_get_prob
  182. #define vp56_rac_get_prob vp56_rac_get_prob
  183. static av_always_inline int vp56_rac_get_prob(VP56RangeCoder *c, uint8_t prob)
  184. {
  185. unsigned int code_word = vp56_rac_renorm(c);
  186. unsigned int low = 1 + (((c->high - 1) * prob) >> 8);
  187. unsigned int low_shift = low << 16;
  188. int bit = code_word >= low_shift;
  189. c->high = bit ? c->high - low : low;
  190. c->code_word = bit ? code_word - low_shift : code_word;
  191. return bit;
  192. }
  193. #endif
  194. #ifndef vp56_rac_get_prob_branchy
  195. // branchy variant, to be used where there's a branch based on the bit decoded
  196. static av_always_inline int vp56_rac_get_prob_branchy(VP56RangeCoder *c, int prob)
  197. {
  198. unsigned long code_word = vp56_rac_renorm(c);
  199. unsigned low = 1 + (((c->high - 1) * prob) >> 8);
  200. unsigned low_shift = low << 16;
  201. if (code_word >= low_shift) {
  202. c->high -= low;
  203. c->code_word = code_word - low_shift;
  204. return 1;
  205. }
  206. c->high = low;
  207. c->code_word = code_word;
  208. return 0;
  209. }
  210. #endif
  211. static av_always_inline int vp56_rac_get(VP56RangeCoder *c)
  212. {
  213. unsigned int code_word = vp56_rac_renorm(c);
  214. /* equiprobable */
  215. int low = (c->high + 1) >> 1;
  216. unsigned int low_shift = low << 16;
  217. int bit = code_word >= low_shift;
  218. if (bit) {
  219. c->high -= low;
  220. code_word -= low_shift;
  221. } else {
  222. c->high = low;
  223. }
  224. c->code_word = code_word;
  225. return bit;
  226. }
  227. // rounding is different than vp56_rac_get, is vp56_rac_get wrong?
  228. static av_always_inline int vp8_rac_get(VP56RangeCoder *c)
  229. {
  230. return vp56_rac_get_prob(c, 128);
  231. }
  232. static av_unused int vp56_rac_gets(VP56RangeCoder *c, int bits)
  233. {
  234. int value = 0;
  235. while (bits--) {
  236. value = (value << 1) | vp56_rac_get(c);
  237. }
  238. return value;
  239. }
  240. static av_unused int vp8_rac_get_uint(VP56RangeCoder *c, int bits)
  241. {
  242. int value = 0;
  243. while (bits--) {
  244. value = (value << 1) | vp8_rac_get(c);
  245. }
  246. return value;
  247. }
  248. // fixme: add 1 bit to all the calls to this?
  249. static av_unused int vp8_rac_get_sint(VP56RangeCoder *c, int bits)
  250. {
  251. int v;
  252. if (!vp8_rac_get(c))
  253. return 0;
  254. v = vp8_rac_get_uint(c, bits);
  255. if (vp8_rac_get(c))
  256. v = -v;
  257. return v;
  258. }
  259. // P(7)
  260. static av_unused int vp56_rac_gets_nn(VP56RangeCoder *c, int bits)
  261. {
  262. int v = vp56_rac_gets(c, 7) << 1;
  263. return v + !v;
  264. }
  265. static av_unused int vp8_rac_get_nn(VP56RangeCoder *c)
  266. {
  267. int v = vp8_rac_get_uint(c, 7) << 1;
  268. return v + !v;
  269. }
  270. static av_always_inline
  271. int vp56_rac_get_tree(VP56RangeCoder *c,
  272. const VP56Tree *tree,
  273. const uint8_t *probs)
  274. {
  275. while (tree->val > 0) {
  276. if (vp56_rac_get_prob(c, probs[tree->prob_idx]))
  277. tree += tree->val;
  278. else
  279. tree++;
  280. }
  281. return -tree->val;
  282. }
  283. // how probabilities are associated with decisions is different I think
  284. // well, the new scheme fits in the old but this way has one fewer branches per decision
  285. static av_always_inline int vp8_rac_get_tree(VP56RangeCoder *c, const int8_t (*tree)[2],
  286. const uint8_t *probs)
  287. {
  288. int i = 0;
  289. do {
  290. i = tree[i][vp56_rac_get_prob(c, probs[i])];
  291. } while (i > 0);
  292. return -i;
  293. }
  294. // DCTextra
  295. static av_always_inline int vp8_rac_get_coeff(VP56RangeCoder *c, const uint8_t *prob)
  296. {
  297. int v = 0;
  298. do {
  299. v = (v<<1) + vp56_rac_get_prob(c, *prob++);
  300. } while (*prob);
  301. return v;
  302. }
  303. #endif /* AVCODEC_VP56_H */