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.

404 lines
11KB

  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 "get_bits.h"
  27. #include "hpeldsp.h"
  28. #include "bytestream.h"
  29. #include "h264chroma.h"
  30. #include "videodsp.h"
  31. #include "vp3dsp.h"
  32. #include "vp56dsp.h"
  33. typedef struct vp56_context VP56Context;
  34. typedef enum {
  35. VP56_FRAME_NONE =-1,
  36. VP56_FRAME_CURRENT = 0,
  37. VP56_FRAME_PREVIOUS = 1,
  38. VP56_FRAME_GOLDEN = 2,
  39. VP56_FRAME_GOLDEN2 = 3,
  40. } VP56Frame;
  41. typedef enum {
  42. VP56_MB_INTER_NOVEC_PF = 0, /**< Inter MB, no vector, from previous frame */
  43. VP56_MB_INTRA = 1, /**< Intra MB */
  44. VP56_MB_INTER_DELTA_PF = 2, /**< Inter MB, above/left vector + delta, from previous frame */
  45. VP56_MB_INTER_V1_PF = 3, /**< Inter MB, first vector, from previous frame */
  46. VP56_MB_INTER_V2_PF = 4, /**< Inter MB, second vector, from previous frame */
  47. VP56_MB_INTER_NOVEC_GF = 5, /**< Inter MB, no vector, from golden frame */
  48. VP56_MB_INTER_DELTA_GF = 6, /**< Inter MB, above/left vector + delta, from golden frame */
  49. VP56_MB_INTER_4V = 7, /**< Inter MB, 4 vectors, from previous frame */
  50. VP56_MB_INTER_V1_GF = 8, /**< Inter MB, first vector, from golden frame */
  51. VP56_MB_INTER_V2_GF = 9, /**< Inter MB, second vector, from golden frame */
  52. } VP56mb;
  53. typedef struct VP56Tree {
  54. int8_t val;
  55. int8_t prob_idx;
  56. } VP56Tree;
  57. typedef struct VP56mv {
  58. DECLARE_ALIGNED(4, int16_t, x);
  59. int16_t y;
  60. } VP56mv;
  61. #define VP56_SIZE_CHANGE 1
  62. typedef void (*VP56ParseVectorAdjustment)(VP56Context *s,
  63. VP56mv *vect);
  64. typedef void (*VP56Filter)(VP56Context *s, uint8_t *dst, uint8_t *src,
  65. int offset1, int offset2, int stride,
  66. VP56mv mv, int mask, int select, int luma);
  67. typedef int (*VP56ParseCoeff)(VP56Context *s);
  68. typedef void (*VP56DefaultModelsInit)(VP56Context *s);
  69. typedef void (*VP56ParseVectorModels)(VP56Context *s);
  70. typedef int (*VP56ParseCoeffModels)(VP56Context *s);
  71. typedef int (*VP56ParseHeader)(VP56Context *s, const uint8_t *buf,
  72. int buf_size);
  73. typedef struct VP56RangeCoder {
  74. int high;
  75. int bits; /* stored negated (i.e. negative "bits" is a positive number of
  76. bits left) in order to eliminate a negate in cache refilling */
  77. const uint8_t *buffer;
  78. const uint8_t *end;
  79. unsigned int code_word;
  80. } VP56RangeCoder;
  81. typedef struct VP56RefDc {
  82. uint8_t not_null_dc;
  83. VP56Frame ref_frame;
  84. int16_t dc_coeff;
  85. } VP56RefDc;
  86. typedef struct VP56Macroblock {
  87. uint8_t type;
  88. VP56mv mv;
  89. } VP56Macroblock;
  90. typedef struct VP56Model {
  91. uint8_t coeff_reorder[64]; /* used in vp6 only */
  92. uint8_t coeff_index_to_pos[64]; /* used in vp6 only */
  93. uint8_t vector_sig[2]; /* delta sign */
  94. uint8_t vector_dct[2]; /* delta coding types */
  95. uint8_t vector_pdi[2][2]; /* predefined delta init */
  96. uint8_t vector_pdv[2][7]; /* predefined delta values */
  97. uint8_t vector_fdv[2][8]; /* 8 bit delta value definition */
  98. uint8_t coeff_dccv[2][11]; /* DC coeff value */
  99. uint8_t coeff_ract[2][3][6][11]; /* Run/AC coding type and AC coeff value */
  100. uint8_t coeff_acct[2][3][3][6][5];/* vp5 only AC coding type for coding group < 3 */
  101. uint8_t coeff_dcct[2][36][5]; /* DC coeff coding type */
  102. uint8_t coeff_runv[2][14]; /* run value (vp6 only) */
  103. uint8_t mb_type[3][10][10]; /* model for decoding MB type */
  104. uint8_t mb_types_stats[3][10][2];/* contextual, next MB type stats */
  105. } VP56Model;
  106. struct vp56_context {
  107. AVCodecContext *avctx;
  108. H264ChromaContext h264chroma;
  109. HpelDSPContext hdsp;
  110. VideoDSPContext vdsp;
  111. VP3DSPContext vp3dsp;
  112. VP56DSPContext vp56dsp;
  113. uint8_t idct_scantable[64];
  114. AVFrame *frames[4];
  115. uint8_t *edge_emu_buffer_alloc;
  116. uint8_t *edge_emu_buffer;
  117. VP56RangeCoder c;
  118. VP56RangeCoder cc;
  119. VP56RangeCoder *ccp;
  120. int sub_version;
  121. /* frame info */
  122. int golden_frame;
  123. int plane_width[4];
  124. int plane_height[4];
  125. int mb_width; /* number of horizontal MB */
  126. int mb_height; /* number of vertical MB */
  127. int block_offset[6];
  128. int quantizer;
  129. uint16_t dequant_dc;
  130. uint16_t dequant_ac;
  131. /* DC predictors management */
  132. VP56RefDc *above_blocks;
  133. VP56RefDc left_block[4];
  134. int above_block_idx[6];
  135. int16_t prev_dc[3][3]; /* [plan][ref_frame] */
  136. /* blocks / macroblock */
  137. VP56mb mb_type;
  138. VP56Macroblock *macroblocks;
  139. DECLARE_ALIGNED(16, int16_t, block_coeff)[6][64];
  140. /* motion vectors */
  141. VP56mv mv[6]; /* vectors for each block in MB */
  142. VP56mv vector_candidate[2];
  143. int vector_candidate_pos;
  144. /* filtering hints */
  145. int filter_header; /* used in vp6 only */
  146. int deblock_filtering;
  147. int filter_selection;
  148. int filter_mode;
  149. int max_vector_length;
  150. int sample_variance_threshold;
  151. uint8_t coeff_ctx[4][64]; /* used in vp5 only */
  152. uint8_t coeff_ctx_last[4]; /* used in vp5 only */
  153. int has_alpha;
  154. /* upside-down flipping hints */
  155. int flip; /* are we flipping ? */
  156. int frbi; /* first row block index in MB */
  157. int srbi; /* second row block index in MB */
  158. int stride[4]; /* stride for each plan */
  159. const uint8_t *vp56_coord_div;
  160. VP56ParseVectorAdjustment parse_vector_adjustment;
  161. VP56Filter filter;
  162. VP56ParseCoeff parse_coeff;
  163. VP56DefaultModelsInit default_models_init;
  164. VP56ParseVectorModels parse_vector_models;
  165. VP56ParseCoeffModels parse_coeff_models;
  166. VP56ParseHeader parse_header;
  167. /* for "slice" parallelism between YUV and A */
  168. VP56Context *alpha_context;
  169. VP56Model *modelp;
  170. VP56Model model;
  171. /* huffman decoding */
  172. int use_huffman;
  173. GetBitContext gb;
  174. VLC dccv_vlc[2];
  175. VLC runv_vlc[2];
  176. VLC ract_vlc[2][3][6];
  177. unsigned int nb_null[2][2]; /* number of consecutive NULL DC/AC */
  178. int have_undamaged_frame;
  179. int discard_frame;
  180. };
  181. int ff_vp56_init(AVCodecContext *avctx, int flip, int has_alpha);
  182. int ff_vp56_init_context(AVCodecContext *avctx, VP56Context *s,
  183. int flip, int has_alpha);
  184. int ff_vp56_free(AVCodecContext *avctx);
  185. int ff_vp56_free_context(VP56Context *s);
  186. void ff_vp56_init_dequant(VP56Context *s, int quantizer);
  187. int ff_vp56_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  188. AVPacket *avpkt);
  189. /**
  190. * vp56 specific range coder implementation
  191. */
  192. extern const uint8_t ff_vp56_norm_shift[256];
  193. int ff_vp56_init_range_decoder(VP56RangeCoder *c, const uint8_t *buf, int buf_size);
  194. static av_always_inline unsigned int vp56_rac_renorm(VP56RangeCoder *c)
  195. {
  196. int shift = ff_vp56_norm_shift[c->high];
  197. int bits = c->bits;
  198. unsigned int code_word = c->code_word;
  199. c->high <<= shift;
  200. code_word <<= shift;
  201. bits += shift;
  202. if(bits >= 0 && c->buffer < c->end) {
  203. code_word |= bytestream_get_be16(&c->buffer) << bits;
  204. bits -= 16;
  205. }
  206. c->bits = bits;
  207. return code_word;
  208. }
  209. #if ARCH_ARM
  210. #include "arm/vp56_arith.h"
  211. #elif ARCH_X86
  212. #include "x86/vp56_arith.h"
  213. #endif
  214. #ifndef vp56_rac_get_prob
  215. #define vp56_rac_get_prob vp56_rac_get_prob
  216. static av_always_inline int vp56_rac_get_prob(VP56RangeCoder *c, uint8_t prob)
  217. {
  218. unsigned int code_word = vp56_rac_renorm(c);
  219. unsigned int low = 1 + (((c->high - 1) * prob) >> 8);
  220. unsigned int low_shift = low << 16;
  221. int bit = code_word >= low_shift;
  222. c->high = bit ? c->high - low : low;
  223. c->code_word = bit ? code_word - low_shift : code_word;
  224. return bit;
  225. }
  226. #endif
  227. #ifndef vp56_rac_get_prob_branchy
  228. // branchy variant, to be used where there's a branch based on the bit decoded
  229. static av_always_inline int vp56_rac_get_prob_branchy(VP56RangeCoder *c, int prob)
  230. {
  231. unsigned long code_word = vp56_rac_renorm(c);
  232. unsigned low = 1 + (((c->high - 1) * prob) >> 8);
  233. unsigned low_shift = low << 16;
  234. if (code_word >= low_shift) {
  235. c->high -= low;
  236. c->code_word = code_word - low_shift;
  237. return 1;
  238. }
  239. c->high = low;
  240. c->code_word = code_word;
  241. return 0;
  242. }
  243. #endif
  244. static av_always_inline int vp56_rac_get(VP56RangeCoder *c)
  245. {
  246. unsigned int code_word = vp56_rac_renorm(c);
  247. /* equiprobable */
  248. int low = (c->high + 1) >> 1;
  249. unsigned int low_shift = low << 16;
  250. int bit = code_word >= low_shift;
  251. if (bit) {
  252. c->high -= low;
  253. code_word -= low_shift;
  254. } else {
  255. c->high = low;
  256. }
  257. c->code_word = code_word;
  258. return bit;
  259. }
  260. // rounding is different than vp56_rac_get, is vp56_rac_get wrong?
  261. static av_always_inline int vp8_rac_get(VP56RangeCoder *c)
  262. {
  263. return vp56_rac_get_prob(c, 128);
  264. }
  265. static int vp56_rac_gets(VP56RangeCoder *c, int bits)
  266. {
  267. int value = 0;
  268. while (bits--) {
  269. value = (value << 1) | vp56_rac_get(c);
  270. }
  271. return value;
  272. }
  273. static int vp8_rac_get_uint(VP56RangeCoder *c, int bits)
  274. {
  275. int value = 0;
  276. while (bits--) {
  277. value = (value << 1) | vp8_rac_get(c);
  278. }
  279. return value;
  280. }
  281. // fixme: add 1 bit to all the calls to this?
  282. static av_unused int vp8_rac_get_sint(VP56RangeCoder *c, int bits)
  283. {
  284. int v;
  285. if (!vp8_rac_get(c))
  286. return 0;
  287. v = vp8_rac_get_uint(c, bits);
  288. if (vp8_rac_get(c))
  289. v = -v;
  290. return v;
  291. }
  292. // P(7)
  293. static av_unused int vp56_rac_gets_nn(VP56RangeCoder *c, int bits)
  294. {
  295. int v = vp56_rac_gets(c, 7) << 1;
  296. return v + !v;
  297. }
  298. static av_unused int vp8_rac_get_nn(VP56RangeCoder *c)
  299. {
  300. int v = vp8_rac_get_uint(c, 7) << 1;
  301. return v + !v;
  302. }
  303. static av_always_inline
  304. int vp56_rac_get_tree(VP56RangeCoder *c,
  305. const VP56Tree *tree,
  306. const uint8_t *probs)
  307. {
  308. while (tree->val > 0) {
  309. if (vp56_rac_get_prob_branchy(c, probs[tree->prob_idx]))
  310. tree += tree->val;
  311. else
  312. tree++;
  313. }
  314. return -tree->val;
  315. }
  316. // how probabilities are associated with decisions is different I think
  317. // well, the new scheme fits in the old but this way has one fewer branches per decision
  318. static av_always_inline int vp8_rac_get_tree(VP56RangeCoder *c, const int8_t (*tree)[2],
  319. const uint8_t *probs)
  320. {
  321. int i = 0;
  322. do {
  323. i = tree[i][vp56_rac_get_prob(c, probs[i])];
  324. } while (i > 0);
  325. return -i;
  326. }
  327. // DCTextra
  328. static av_always_inline int vp8_rac_get_coeff(VP56RangeCoder *c, const uint8_t *prob)
  329. {
  330. int v = 0;
  331. do {
  332. v = (v<<1) + vp56_rac_get_prob(c, *prob++);
  333. } while (*prob);
  334. return v;
  335. }
  336. #endif /* AVCODEC_VP56_H */