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.

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