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.

292 lines
10KB

  1. /**
  2. * @file vp5.c
  3. * VP5 compatible video decoder
  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. #include <stdlib.h>
  24. #include <string.h>
  25. #include <inttypes.h>
  26. #include "avcodec.h"
  27. #include "dsputil.h"
  28. #include "bitstream.h"
  29. #include "mpegvideo.h"
  30. #include "vp56.h"
  31. #include "vp56data.h"
  32. #include "vp5data.h"
  33. static int vp5_parse_header(vp56_context_t *s, uint8_t *buf, int buf_size,
  34. int *golden_frame)
  35. {
  36. vp56_range_coder_t *c = &s->c;
  37. int rows, cols;
  38. vp56_init_range_decoder(&s->c, buf, buf_size);
  39. s->frames[VP56_FRAME_CURRENT].key_frame = !vp56_rac_get(c);
  40. vp56_rac_get(c);
  41. vp56_init_dequant(s, vp56_rac_gets(c, 6));
  42. if (s->frames[VP56_FRAME_CURRENT].key_frame)
  43. {
  44. vp56_rac_gets(c, 8);
  45. if(vp56_rac_gets(c, 5) > 5)
  46. return 0;
  47. vp56_rac_gets(c, 2);
  48. if (vp56_rac_get(c)) {
  49. av_log(s->avctx, AV_LOG_ERROR, "interlacing not supported\n");
  50. return 0;
  51. }
  52. rows = vp56_rac_gets(c, 8); /* number of stored macroblock rows */
  53. cols = vp56_rac_gets(c, 8); /* number of stored macroblock cols */
  54. vp56_rac_gets(c, 8); /* number of displayed macroblock rows */
  55. vp56_rac_gets(c, 8); /* number of displayed macroblock cols */
  56. vp56_rac_gets(c, 2);
  57. if (16*cols != s->avctx->coded_width ||
  58. 16*rows != s->avctx->coded_height) {
  59. avcodec_set_dimensions(s->avctx, 16*cols, 16*rows);
  60. return 2;
  61. }
  62. }
  63. return 1;
  64. }
  65. /* Gives very similar result than the vp6 version except in a few cases */
  66. static int vp5_adjust(int v, int t)
  67. {
  68. int s2, s1 = v >> 31;
  69. v ^= s1;
  70. v -= s1;
  71. v *= v < 2*t;
  72. v -= t;
  73. s2 = v >> 31;
  74. v ^= s2;
  75. v -= s2;
  76. v = t - v;
  77. v += s1;
  78. v ^= s1;
  79. return v;
  80. }
  81. static void vp5_parse_vector_adjustment(vp56_context_t *s, vp56_mv_t *vect)
  82. {
  83. vp56_range_coder_t *c = &s->c;
  84. int comp, di;
  85. for (comp=0; comp<2; comp++) {
  86. int delta = 0;
  87. if (vp56_rac_get_prob(c, s->vector_model_dct[comp])) {
  88. int sign = vp56_rac_get_prob(c, s->vector_model_sig[comp]);
  89. di = vp56_rac_get_prob(c, s->vector_model_pdi[comp][0]);
  90. di |= vp56_rac_get_prob(c, s->vector_model_pdi[comp][1]) << 1;
  91. delta = vp56_rac_get_tree(c, vp56_pva_tree,
  92. s->vector_model_pdv[comp]);
  93. delta = di | (delta << 2);
  94. delta = (delta ^ -sign) + sign;
  95. }
  96. if (!comp)
  97. vect->x = delta;
  98. else
  99. vect->y = delta;
  100. }
  101. }
  102. static void vp5_parse_vector_models(vp56_context_t *s)
  103. {
  104. vp56_range_coder_t *c = &s->c;
  105. int comp, node;
  106. for (comp=0; comp<2; comp++) {
  107. if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][0]))
  108. s->vector_model_dct[comp] = vp56_rac_gets_nn(c, 7);
  109. if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][1]))
  110. s->vector_model_sig[comp] = vp56_rac_gets_nn(c, 7);
  111. if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][2]))
  112. s->vector_model_pdi[comp][0] = vp56_rac_gets_nn(c, 7);
  113. if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][3]))
  114. s->vector_model_pdi[comp][1] = vp56_rac_gets_nn(c, 7);
  115. }
  116. for (comp=0; comp<2; comp++)
  117. for (node=0; node<7; node++)
  118. if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][4 + node]))
  119. s->vector_model_pdv[comp][node] = vp56_rac_gets_nn(c, 7);
  120. }
  121. static void vp5_parse_coeff_models(vp56_context_t *s)
  122. {
  123. vp56_range_coder_t *c = &s->c;
  124. uint8_t def_prob[11];
  125. int node, cg, ctx;
  126. int ct; /* code type */
  127. int pt; /* plane type (0 for Y, 1 for U or V) */
  128. memset(def_prob, 0x80, sizeof(def_prob));
  129. for (pt=0; pt<2; pt++)
  130. for (node=0; node<11; node++)
  131. if (vp56_rac_get_prob(c, vp5_dccv_pct[pt][node])) {
  132. def_prob[node] = vp56_rac_gets_nn(c, 7);
  133. s->coeff_model_dccv[pt][node] = def_prob[node];
  134. } else if (s->frames[VP56_FRAME_CURRENT].key_frame) {
  135. s->coeff_model_dccv[pt][node] = def_prob[node];
  136. }
  137. for (ct=0; ct<3; ct++)
  138. for (pt=0; pt<2; pt++)
  139. for (cg=0; cg<6; cg++)
  140. for (node=0; node<11; node++)
  141. if (vp56_rac_get_prob(c, vp5_ract_pct[ct][pt][cg][node])) {
  142. def_prob[node] = vp56_rac_gets_nn(c, 7);
  143. s->coeff_model_ract[pt][ct][cg][node] = def_prob[node];
  144. } else if (s->frames[VP56_FRAME_CURRENT].key_frame) {
  145. s->coeff_model_ract[pt][ct][cg][node] = def_prob[node];
  146. }
  147. /* coeff_model_dcct is a linear combination of coeff_model_dccv */
  148. for (pt=0; pt<2; pt++)
  149. for (ctx=0; ctx<36; ctx++)
  150. for (node=0; node<5; node++)
  151. s->coeff_model_dcct[pt][ctx][node] = clip(((s->coeff_model_dccv[pt][node] * vp5_dccv_lc[node][ctx][0] + 128) >> 8) + vp5_dccv_lc[node][ctx][1], 1, 254);
  152. /* coeff_model_acct is a linear combination of coeff_model_ract */
  153. for (ct=0; ct<3; ct++)
  154. for (pt=0; pt<2; pt++)
  155. for (cg=0; cg<3; cg++)
  156. for (ctx=0; ctx<6; ctx++)
  157. for (node=0; node<5; node++)
  158. s->coeff_model_acct[pt][ct][cg][ctx][node] = clip(((s->coeff_model_ract[pt][ct][cg][node] * vp5_ract_lc[ct][cg][node][ctx][0] + 128) >> 8) + vp5_ract_lc[ct][cg][node][ctx][1], 1, 254);
  159. }
  160. static void vp5_parse_coeff(vp56_context_t *s)
  161. {
  162. vp56_range_coder_t *c = &s->c;
  163. uint8_t *permute = s->scantable.permutated;
  164. uint8_t *model, *model2;
  165. int coeff, sign, coeff_idx;
  166. int b, i, cg, idx, ctx, ctx_last;
  167. int pt = 0; /* plane type (0 for Y, 1 for U or V) */
  168. for (b=0; b<6; b++) {
  169. int ct = 1; /* code type */
  170. if (b > 3) pt = 1;
  171. ctx = 6*s->coeff_ctx[vp56_b6to4[b]][0]
  172. + s->above_blocks[s->above_block_idx[b]].not_null_dc;
  173. model = s->coeff_model_dccv[pt];
  174. model2 = s->coeff_model_dcct[pt][ctx];
  175. for (coeff_idx=0; coeff_idx<64; ) {
  176. if (vp56_rac_get_prob(c, model2[0])) {
  177. if (vp56_rac_get_prob(c, model2[2])) {
  178. if (vp56_rac_get_prob(c, model2[3])) {
  179. s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 4;
  180. idx = vp56_rac_get_tree(c, vp56_pc_tree, model);
  181. sign = vp56_rac_get(c);
  182. coeff = vp56_coeff_bias[idx];
  183. for (i=vp56_coeff_bit_length[idx]; i>=0; i--)
  184. coeff += vp56_rac_get_prob(c, vp56_coeff_parse_table[idx][i]) << i;
  185. } else {
  186. if (vp56_rac_get_prob(c, model2[4])) {
  187. coeff = 3 + vp56_rac_get_prob(c, model[5]);
  188. s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 3;
  189. } else {
  190. coeff = 2;
  191. s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 2;
  192. }
  193. sign = vp56_rac_get(c);
  194. }
  195. ct = 2;
  196. } else {
  197. ct = 1;
  198. s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 1;
  199. sign = vp56_rac_get(c);
  200. coeff = 1;
  201. }
  202. coeff = (coeff ^ -sign) + sign;
  203. if (coeff_idx)
  204. coeff *= s->dequant_ac;
  205. s->block_coeff[b][permute[coeff_idx]] = coeff;
  206. } else {
  207. if (ct && !vp56_rac_get_prob(c, model2[1]))
  208. break;
  209. ct = 0;
  210. s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 0;
  211. }
  212. cg = vp5_coeff_groups[++coeff_idx];
  213. ctx = s->coeff_ctx[vp56_b6to4[b]][coeff_idx];
  214. model = s->coeff_model_ract[pt][ct][cg];
  215. model2 = cg > 2 ? model : s->coeff_model_acct[pt][ct][cg][ctx];
  216. }
  217. ctx_last = FFMIN(s->coeff_ctx_last[vp56_b6to4[b]], 24);
  218. s->coeff_ctx_last[vp56_b6to4[b]] = coeff_idx;
  219. if (coeff_idx < ctx_last)
  220. for (i=coeff_idx; i<=ctx_last; i++)
  221. s->coeff_ctx[vp56_b6to4[b]][i] = 5;
  222. s->above_blocks[s->above_block_idx[b]].not_null_dc = s->coeff_ctx[vp56_b6to4[b]][0];
  223. }
  224. }
  225. static void vp5_default_models_init(vp56_context_t *s)
  226. {
  227. int i;
  228. for (i=0; i<2; i++) {
  229. s->vector_model_sig[i] = 0x80;
  230. s->vector_model_dct[i] = 0x80;
  231. s->vector_model_pdi[i][0] = 0x55;
  232. s->vector_model_pdi[i][1] = 0x80;
  233. }
  234. memcpy(s->mb_types_stats, vp56_def_mb_types_stats, sizeof(s->mb_types_stats));
  235. memset(s->vector_model_pdv, 0x80, sizeof(s->vector_model_pdv));
  236. }
  237. static int vp5_decode_init(AVCodecContext *avctx)
  238. {
  239. vp56_context_t *s = avctx->priv_data;
  240. vp56_init(s, avctx, 1);
  241. s->vp56_coord_div = vp5_coord_div;
  242. s->parse_vector_adjustment = vp5_parse_vector_adjustment;
  243. s->adjust = vp5_adjust;
  244. s->parse_coeff = vp5_parse_coeff;
  245. s->default_models_init = vp5_default_models_init;
  246. s->parse_vector_models = vp5_parse_vector_models;
  247. s->parse_coeff_models = vp5_parse_coeff_models;
  248. s->parse_header = vp5_parse_header;
  249. return 0;
  250. }
  251. AVCodec vp5_decoder = {
  252. "vp5",
  253. CODEC_TYPE_VIDEO,
  254. CODEC_ID_VP5,
  255. sizeof(vp56_context_t),
  256. vp5_decode_init,
  257. NULL,
  258. vp56_free,
  259. vp56_decode_frame,
  260. };