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.

513 lines
17KB

  1. /**
  2. * @file vp6.c
  3. * VP6 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 <inttypes.h>
  23. #include "avcodec.h"
  24. #include "dsputil.h"
  25. #include "bitstream.h"
  26. #include "mpegvideo.h"
  27. #include "vp56.h"
  28. #include "vp56data.h"
  29. #include "vp6data.h"
  30. static int vp6_parse_header(vp56_context_t *s, uint8_t *buf, int buf_size,
  31. int *golden_frame)
  32. {
  33. vp56_range_coder_t *c = &s->c;
  34. int parse_filter_info;
  35. int rows, cols;
  36. int res = 1;
  37. if (buf[0] & 1)
  38. return 0;
  39. s->frames[VP56_FRAME_CURRENT].key_frame = !(buf[0] & 0x80);
  40. vp56_init_dequant(s, (buf[0] >> 1) & 0x3F);
  41. if (s->frames[VP56_FRAME_CURRENT].key_frame) {
  42. if ((buf[1] & 0xFE) != 0x46) /* would be 0x36 for VP61 */
  43. return 0;
  44. if (buf[1] & 1) {
  45. av_log(s->avctx, AV_LOG_ERROR, "interlacing not supported\n");
  46. return 0;
  47. }
  48. rows = buf[2]; /* number of stored macroblock rows */
  49. cols = buf[3]; /* number of stored macroblock cols */
  50. /* buf[4] is number of displayed macroblock rows */
  51. /* buf[5] is number of displayed macroblock cols */
  52. if (16*cols != s->avctx->coded_width ||
  53. 16*rows != s->avctx->coded_height) {
  54. avcodec_set_dimensions(s->avctx, 16*cols, 16*rows);
  55. res = 2;
  56. }
  57. vp56_init_range_decoder(c, buf+6, buf_size-6);
  58. vp56_rac_gets(c, 2);
  59. parse_filter_info = 1;
  60. } else {
  61. vp56_init_range_decoder(c, buf+1, buf_size-1);
  62. *golden_frame = vp56_rac_get(c);
  63. s->deblock_filtering = vp56_rac_get(c);
  64. if (s->deblock_filtering)
  65. vp56_rac_get(c);
  66. parse_filter_info = vp56_rac_get(c);
  67. }
  68. if (parse_filter_info) {
  69. if (vp56_rac_get(c)) {
  70. s->filter_mode = 2;
  71. s->sample_variance_threshold = vp56_rac_gets(c, 5);
  72. s->max_vector_length = 2 << vp56_rac_gets(c, 3);
  73. } else if (vp56_rac_get(c)) {
  74. s->filter_mode = 1;
  75. } else {
  76. s->filter_mode = 0;
  77. }
  78. s->filter_selection = vp56_rac_gets(c, 4);
  79. }
  80. vp56_rac_get(c);
  81. return res;
  82. }
  83. static void vp6_coeff_order_table_init(vp56_context_t *s)
  84. {
  85. int i, pos, idx = 1;
  86. s->coeff_index_to_pos[0] = 0;
  87. for (i=0; i<16; i++)
  88. for (pos=1; pos<64; pos++)
  89. if (s->coeff_reorder[pos] == i)
  90. s->coeff_index_to_pos[idx++] = pos;
  91. }
  92. static void vp6_default_models_init(vp56_context_t *s)
  93. {
  94. s->vector_model_dct[0] = 0xA2;
  95. s->vector_model_dct[1] = 0xA4;
  96. s->vector_model_sig[0] = 0x80;
  97. s->vector_model_sig[1] = 0x80;
  98. memcpy(s->mb_types_stats, vp56_def_mb_types_stats, sizeof(s->mb_types_stats));
  99. memcpy(s->vector_model_fdv, vp6_def_fdv_vector_model, sizeof(s->vector_model_fdv));
  100. memcpy(s->vector_model_pdv, vp6_def_pdv_vector_model, sizeof(s->vector_model_pdv));
  101. memcpy(s->coeff_model_runv, vp6_def_runv_coeff_model, sizeof(s->coeff_model_runv));
  102. memcpy(s->coeff_reorder, vp6_def_coeff_reorder, sizeof(s->coeff_reorder));
  103. vp6_coeff_order_table_init(s);
  104. }
  105. static void vp6_parse_vector_models(vp56_context_t *s)
  106. {
  107. vp56_range_coder_t *c = &s->c;
  108. int comp, node;
  109. for (comp=0; comp<2; comp++) {
  110. if (vp56_rac_get_prob(c, vp6_sig_dct_pct[comp][0]))
  111. s->vector_model_dct[comp] = vp56_rac_gets_nn(c, 7);
  112. if (vp56_rac_get_prob(c, vp6_sig_dct_pct[comp][1]))
  113. s->vector_model_sig[comp] = vp56_rac_gets_nn(c, 7);
  114. }
  115. for (comp=0; comp<2; comp++)
  116. for (node=0; node<7; node++)
  117. if (vp56_rac_get_prob(c, vp6_pdv_pct[comp][node]))
  118. s->vector_model_pdv[comp][node] = vp56_rac_gets_nn(c, 7);
  119. for (comp=0; comp<2; comp++)
  120. for (node=0; node<8; node++)
  121. if (vp56_rac_get_prob(c, vp6_fdv_pct[comp][node]))
  122. s->vector_model_fdv[comp][node] = vp56_rac_gets_nn(c, 7);
  123. }
  124. static void vp6_parse_coeff_models(vp56_context_t *s)
  125. {
  126. vp56_range_coder_t *c = &s->c;
  127. int def_prob[11];
  128. int node, cg, ctx, pos;
  129. int ct; /* code type */
  130. int pt; /* plane type (0 for Y, 1 for U or V) */
  131. memset(def_prob, 0x80, sizeof(def_prob));
  132. for (pt=0; pt<2; pt++)
  133. for (node=0; node<11; node++)
  134. if (vp56_rac_get_prob(c, vp6_dccv_pct[pt][node])) {
  135. def_prob[node] = vp56_rac_gets_nn(c, 7);
  136. s->coeff_model_dccv[pt][node] = def_prob[node];
  137. } else if (s->frames[VP56_FRAME_CURRENT].key_frame) {
  138. s->coeff_model_dccv[pt][node] = def_prob[node];
  139. }
  140. if (vp56_rac_get(c)) {
  141. for (pos=1; pos<64; pos++)
  142. if (vp56_rac_get_prob(c, vp6_coeff_reorder_pct[pos]))
  143. s->coeff_reorder[pos] = vp56_rac_gets(c, 4);
  144. vp6_coeff_order_table_init(s);
  145. }
  146. for (cg=0; cg<2; cg++)
  147. for (node=0; node<14; node++)
  148. if (vp56_rac_get_prob(c, vp6_runv_pct[cg][node]))
  149. s->coeff_model_runv[cg][node] = vp56_rac_gets_nn(c, 7);
  150. for (ct=0; ct<3; ct++)
  151. for (pt=0; pt<2; pt++)
  152. for (cg=0; cg<6; cg++)
  153. for (node=0; node<11; node++)
  154. if (vp56_rac_get_prob(c, vp6_ract_pct[ct][pt][cg][node])) {
  155. def_prob[node] = vp56_rac_gets_nn(c, 7);
  156. s->coeff_model_ract[pt][ct][cg][node] = def_prob[node];
  157. } else if (s->frames[VP56_FRAME_CURRENT].key_frame) {
  158. s->coeff_model_ract[pt][ct][cg][node] = def_prob[node];
  159. }
  160. /* coeff_model_dcct is a linear combination of coeff_model_dccv */
  161. for (pt=0; pt<2; pt++)
  162. for (ctx=0; ctx<3; ctx++)
  163. for (node=0; node<5; node++)
  164. s->coeff_model_dcct[pt][ctx][node] = clip(((s->coeff_model_dccv[pt][node] * vp6_dccv_lc[ctx][node][0] + 128) >> 8) + vp6_dccv_lc[ctx][node][1], 1, 255);
  165. }
  166. static void vp6_parse_vector_adjustment(vp56_context_t *s, vp56_mv_t *vect)
  167. {
  168. vp56_range_coder_t *c = &s->c;
  169. int comp;
  170. *vect = (vp56_mv_t) {0,0};
  171. if (s->vector_candidate_pos < 2)
  172. *vect = s->vector_candidate[0];
  173. for (comp=0; comp<2; comp++) {
  174. int i, delta = 0;
  175. if (vp56_rac_get_prob(c, s->vector_model_dct[comp])) {
  176. static const uint8_t prob_order[] = {0, 1, 2, 7, 6, 5, 4};
  177. for (i=0; i<sizeof(prob_order); i++) {
  178. int j = prob_order[i];
  179. delta |= vp56_rac_get_prob(c, s->vector_model_fdv[comp][j])<<j;
  180. }
  181. if (delta & 0xF0)
  182. delta |= vp56_rac_get_prob(c, s->vector_model_fdv[comp][3])<<3;
  183. else
  184. delta |= 8;
  185. } else {
  186. delta = vp56_rac_get_tree(c, vp56_pva_tree,
  187. s->vector_model_pdv[comp]);
  188. }
  189. if (delta && vp56_rac_get_prob(c, s->vector_model_sig[comp]))
  190. delta = -delta;
  191. if (!comp)
  192. vect->x += delta;
  193. else
  194. vect->y += delta;
  195. }
  196. }
  197. static void vp6_parse_coeff(vp56_context_t *s)
  198. {
  199. vp56_range_coder_t *c = &s->c;
  200. uint8_t *permute = s->scantable.permutated;
  201. uint8_t *model, *model2, *model3;
  202. int coeff, sign, coeff_idx;
  203. int b, i, cg, idx, ctx;
  204. int pt = 0; /* plane type (0 for Y, 1 for U or V) */
  205. for (b=0; b<6; b++) {
  206. int ct = 1; /* code type */
  207. int run = 1;
  208. if (b > 3) pt = 1;
  209. ctx = s->left_block[vp56_b6to4[b]].not_null_dc
  210. + s->above_blocks[s->above_block_idx[b]].not_null_dc;
  211. model = s->coeff_model_dccv[pt];
  212. model2 = s->coeff_model_dcct[pt][ctx];
  213. for (coeff_idx=0; coeff_idx<64; ) {
  214. if ((coeff_idx>1 && ct==0) || vp56_rac_get_prob(c, model2[0])) {
  215. /* parse a coeff */
  216. if (coeff_idx == 0) {
  217. s->left_block[vp56_b6to4[b]].not_null_dc = 1;
  218. s->above_blocks[s->above_block_idx[b]].not_null_dc = 1;
  219. }
  220. if (vp56_rac_get_prob(c, model2[2])) {
  221. if (vp56_rac_get_prob(c, model2[3])) {
  222. idx = vp56_rac_get_tree(c, vp56_pc_tree, model);
  223. coeff = vp56_coeff_bias[idx];
  224. for (i=vp56_coeff_bit_length[idx]; i>=0; i--)
  225. coeff += vp56_rac_get_prob(c, vp56_coeff_parse_table[idx][i]) << i;
  226. } else {
  227. if (vp56_rac_get_prob(c, model2[4]))
  228. coeff = 3 + vp56_rac_get_prob(c, model[5]);
  229. else
  230. coeff = 2;
  231. }
  232. ct = 2;
  233. } else {
  234. ct = 1;
  235. coeff = 1;
  236. }
  237. sign = vp56_rac_get(c);
  238. coeff = (coeff ^ -sign) + sign;
  239. if (coeff_idx)
  240. coeff *= s->dequant_ac;
  241. idx = s->coeff_index_to_pos[coeff_idx];
  242. s->block_coeff[b][permute[idx]] = coeff;
  243. run = 1;
  244. } else {
  245. /* parse a run */
  246. ct = 0;
  247. if (coeff_idx == 0) {
  248. s->left_block[vp56_b6to4[b]].not_null_dc = 0;
  249. s->above_blocks[s->above_block_idx[b]].not_null_dc = 0;
  250. } else {
  251. if (!vp56_rac_get_prob(c, model2[1]))
  252. break;
  253. model3 = s->coeff_model_runv[coeff_idx >= 6];
  254. run = vp56_rac_get_tree(c, vp6_pcr_tree, model3);
  255. if (!run)
  256. for (run=9, i=0; i<6; i++)
  257. run += vp56_rac_get_prob(c, model3[i+8]) << i;
  258. }
  259. }
  260. cg = vp6_coeff_groups[coeff_idx+=run];
  261. model = model2 = s->coeff_model_ract[pt][ct][cg];
  262. }
  263. }
  264. }
  265. static int vp6_adjust(int v, int t)
  266. {
  267. int V = v, s = v >> 31;
  268. V ^= s;
  269. V -= s;
  270. if (V-t-1 >= (unsigned)(t-1))
  271. return v;
  272. V = 2*t - V;
  273. V += s;
  274. V ^= s;
  275. return V;
  276. }
  277. static int vp6_block_variance(uint8_t *src, int stride)
  278. {
  279. int sum = 0, square_sum = 0;
  280. int y, x;
  281. for (y=0; y<8; y+=2) {
  282. for (x=0; x<8; x+=2) {
  283. sum += src[x];
  284. square_sum += src[x]*src[x];
  285. }
  286. src += 2*stride;
  287. }
  288. return (16*square_sum - sum*sum) / (16*16);
  289. }
  290. static void vp6_filter_hv2(vp56_context_t *s, uint8_t *dst, uint8_t *src,
  291. int stride, int delta, int16_t weight)
  292. {
  293. s->dsp.put_pixels_tab[1][0](dst, src, stride, 8);
  294. s->dsp.biweight_h264_pixels_tab[3](dst, src+delta, stride, 2,
  295. 8-weight, weight, 0);
  296. }
  297. static void vp6_filter_hv4(uint8_t *dst, uint8_t *src, int stride,
  298. int delta, const int16_t *weights)
  299. {
  300. int x, y;
  301. for (y=0; y<8; y++) {
  302. for (x=0; x<8; x++) {
  303. dst[x] = clip_uint8(( src[x-delta ] * weights[0]
  304. + src[x ] * weights[1]
  305. + src[x+delta ] * weights[2]
  306. + src[x+2*delta] * weights[3] + 64) >> 7);
  307. }
  308. src += stride;
  309. dst += stride;
  310. }
  311. }
  312. static void vp6_filter_diag2(vp56_context_t *s, uint8_t *dst, uint8_t *src,
  313. int stride, int h_weight, int v_weight)
  314. {
  315. uint8_t *tmp = s->edge_emu_buffer+16;
  316. int x, xmax;
  317. s->dsp.put_pixels_tab[1][0](tmp, src, stride, 8);
  318. s->dsp.biweight_h264_pixels_tab[3](tmp, src+1, stride, 2,
  319. 8-h_weight, h_weight, 0);
  320. /* we need a 8x9 block to do vertical filter, so compute one more line */
  321. for (x=8*stride, xmax=x+8; x<xmax; x++)
  322. tmp[x] = (src[x]*(8-h_weight) + src[x+1]*h_weight + 4) >> 3;
  323. s->dsp.put_pixels_tab[1][0](dst, tmp, stride, 8);
  324. s->dsp.biweight_h264_pixels_tab[3](dst, tmp+stride, stride, 2,
  325. 8-v_weight, v_weight, 0);
  326. }
  327. static void vp6_filter_diag4(uint8_t *dst, uint8_t *src, int stride,
  328. const int16_t *h_weights,const int16_t *v_weights)
  329. {
  330. int x, y;
  331. int tmp[8*11];
  332. int *t = tmp;
  333. src -= stride;
  334. for (y=0; y<11; y++) {
  335. for (x=0; x<8; x++) {
  336. t[x] = clip_uint8(( src[x-1] * h_weights[0]
  337. + src[x ] * h_weights[1]
  338. + src[x+1] * h_weights[2]
  339. + src[x+2] * h_weights[3] + 64) >> 7);
  340. }
  341. src += stride;
  342. t += 8;
  343. }
  344. t = tmp + 8;
  345. for (y=0; y<8; y++) {
  346. for (x=0; x<8; x++) {
  347. dst[x] = clip_uint8(( t[x-8 ] * v_weights[0]
  348. + t[x ] * v_weights[1]
  349. + t[x+8 ] * v_weights[2]
  350. + t[x+16] * v_weights[3] + 64) >> 7);
  351. }
  352. dst += stride;
  353. t += 8;
  354. }
  355. }
  356. static void vp6_filter(vp56_context_t *s, uint8_t *dst, uint8_t *src,
  357. int offset1, int offset2, int stride,
  358. vp56_mv_t mv, int mask, int select, int luma)
  359. {
  360. int filter4 = 0;
  361. int x8 = mv.x & mask;
  362. int y8 = mv.y & mask;
  363. if (luma) {
  364. x8 *= 2;
  365. y8 *= 2;
  366. filter4 = s->filter_mode;
  367. if (filter4 == 2) {
  368. if (s->max_vector_length &&
  369. (ABS(mv.x) > s->max_vector_length ||
  370. ABS(mv.y) > s->max_vector_length)) {
  371. filter4 = 0;
  372. } else if (!s->sample_variance_threshold
  373. || (vp6_block_variance(src+offset1, stride)
  374. < s->sample_variance_threshold)) {
  375. filter4 = 0;
  376. }
  377. }
  378. }
  379. if ((y8 && (offset2-offset1)*s->flip<0) || (!y8 && offset1 > offset2)) {
  380. offset1 = offset2;
  381. }
  382. if (filter4) {
  383. if (!y8) { /* left or right combine */
  384. vp6_filter_hv4(dst, src+offset1, stride, 1,
  385. vp6_block_copy_filter[select][x8]);
  386. } else if (!x8) { /* above or below combine */
  387. vp6_filter_hv4(dst, src+offset1, stride, stride,
  388. vp6_block_copy_filter[select][y8]);
  389. } else if ((mv.x^mv.y) >> 31) { /* lower-left or upper-right combine */
  390. vp6_filter_diag4(dst, src+offset1-1, stride,
  391. vp6_block_copy_filter[select][x8],
  392. vp6_block_copy_filter[select][y8]);
  393. } else { /* lower-right or upper-left combine */
  394. vp6_filter_diag4(dst, src+offset1, stride,
  395. vp6_block_copy_filter[select][x8],
  396. vp6_block_copy_filter[select][y8]);
  397. }
  398. } else {
  399. if (!y8) { /* left or right combine */
  400. vp6_filter_hv2(s, dst, src+offset1, stride, 1, x8);
  401. } else if (!x8) { /* above or below combine */
  402. vp6_filter_hv2(s, dst, src+offset1, stride, stride, y8);
  403. } else if ((mv.x^mv.y) >> 31) { /* lower-left or upper-right combine */
  404. vp6_filter_diag2(s, dst, src+offset1-1, stride, x8, y8);
  405. } else { /* lower-right or upper-left combine */
  406. vp6_filter_diag2(s, dst, src+offset1, stride, x8, y8);
  407. }
  408. }
  409. }
  410. static int vp6_decode_init(AVCodecContext *avctx)
  411. {
  412. vp56_context_t *s = avctx->priv_data;
  413. vp56_init(s, avctx, avctx->codec->id == CODEC_ID_VP6);
  414. s->vp56_coord_div = vp6_coord_div;
  415. s->parse_vector_adjustment = vp6_parse_vector_adjustment;
  416. s->adjust = vp6_adjust;
  417. s->filter = vp6_filter;
  418. s->parse_coeff = vp6_parse_coeff;
  419. s->default_models_init = vp6_default_models_init;
  420. s->parse_vector_models = vp6_parse_vector_models;
  421. s->parse_coeff_models = vp6_parse_coeff_models;
  422. s->parse_header = vp6_parse_header;
  423. return 0;
  424. }
  425. AVCodec vp6_decoder = {
  426. "vp6",
  427. CODEC_TYPE_VIDEO,
  428. CODEC_ID_VP6,
  429. sizeof(vp56_context_t),
  430. vp6_decode_init,
  431. NULL,
  432. vp56_free,
  433. vp56_decode_frame,
  434. };
  435. /* flash version, not flipped upside-down */
  436. AVCodec vp6f_decoder = {
  437. "vp6f",
  438. CODEC_TYPE_VIDEO,
  439. CODEC_ID_VP6F,
  440. sizeof(vp56_context_t),
  441. vp6_decode_init,
  442. NULL,
  443. vp56_free,
  444. vp56_decode_frame,
  445. };