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.

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