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.

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