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.

538 lines
18KB

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