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.

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