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.

727 lines
24KB

  1. /*
  2. * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * VP6 compatible video decoder
  23. *
  24. * The VP6F decoder accepts an optional 1 byte extradata. It is composed of:
  25. * - upper 4 bits: difference between encoded width and visible width
  26. * - lower 4 bits: difference between encoded height and visible height
  27. */
  28. #include <stdlib.h>
  29. #include "avcodec.h"
  30. #include "get_bits.h"
  31. #include "huffman.h"
  32. #include "internal.h"
  33. #include "vp56.h"
  34. #include "vp56data.h"
  35. #include "vp6data.h"
  36. #define VP6_MAX_HUFF_SIZE 12
  37. static int vp6_parse_coeff(VP56Context *s);
  38. static int vp6_parse_coeff_huffman(VP56Context *s);
  39. static int vp6_parse_header(VP56Context *s, const uint8_t *buf, int buf_size)
  40. {
  41. VP56RangeCoder *c = &s->c;
  42. int parse_filter_info = 0;
  43. int coeff_offset = 0;
  44. int vrt_shift = 0;
  45. int sub_version;
  46. int rows, cols;
  47. int res = 0;
  48. int ret;
  49. int separated_coeff = buf[0] & 1;
  50. s->frames[VP56_FRAME_CURRENT]->key_frame = !(buf[0] & 0x80);
  51. ff_vp56_init_dequant(s, (buf[0] >> 1) & 0x3F);
  52. if (s->frames[VP56_FRAME_CURRENT]->key_frame) {
  53. sub_version = buf[1] >> 3;
  54. if (sub_version > 8)
  55. return AVERROR_INVALIDDATA;
  56. s->filter_header = buf[1] & 0x06;
  57. if (buf[1] & 1) {
  58. avpriv_report_missing_feature(s->avctx, "Interlacing");
  59. return AVERROR_PATCHWELCOME;
  60. }
  61. if (separated_coeff || !s->filter_header) {
  62. coeff_offset = AV_RB16(buf+2) - 2;
  63. buf += 2;
  64. buf_size -= 2;
  65. }
  66. rows = buf[2]; /* number of stored macroblock rows */
  67. cols = buf[3]; /* number of stored macroblock cols */
  68. /* buf[4] is number of displayed macroblock rows */
  69. /* buf[5] is number of displayed macroblock cols */
  70. if (!rows || !cols) {
  71. av_log(s->avctx, AV_LOG_ERROR, "Invalid size %dx%d\n", cols << 4, rows << 4);
  72. return AVERROR_INVALIDDATA;
  73. }
  74. if (!s->macroblocks || /* first frame */
  75. 16*cols != s->avctx->coded_width ||
  76. 16*rows != s->avctx->coded_height) {
  77. if (s->avctx->extradata_size == 0 &&
  78. FFALIGN(s->avctx->width, 16) == 16 * cols &&
  79. FFALIGN(s->avctx->height, 16) == 16 * rows) {
  80. // We assume this is properly signalled container cropping,
  81. // in an F4V file. Just set the coded_width/height, don't
  82. // touch the cropped ones.
  83. s->avctx->coded_width = 16 * cols;
  84. s->avctx->coded_height = 16 * rows;
  85. } else {
  86. ret = ff_set_dimensions(s->avctx, 16 * cols, 16 * rows);
  87. if (ret < 0)
  88. return ret;
  89. if (s->avctx->extradata_size == 1) {
  90. s->avctx->width -= s->avctx->extradata[0] >> 4;
  91. s->avctx->height -= s->avctx->extradata[0] & 0x0F;
  92. }
  93. }
  94. res = VP56_SIZE_CHANGE;
  95. }
  96. ret = ff_vp56_init_range_decoder(c, buf+6, buf_size-6);
  97. if (ret < 0)
  98. goto fail;
  99. vp56_rac_gets(c, 2);
  100. parse_filter_info = s->filter_header;
  101. if (sub_version < 8)
  102. vrt_shift = 5;
  103. s->sub_version = sub_version;
  104. s->golden_frame = 0;
  105. } else {
  106. if (!s->sub_version || !s->avctx->coded_width || !s->avctx->coded_height)
  107. return AVERROR_INVALIDDATA;
  108. if (separated_coeff || !s->filter_header) {
  109. coeff_offset = AV_RB16(buf+1) - 2;
  110. buf += 2;
  111. buf_size -= 2;
  112. }
  113. ret = ff_vp56_init_range_decoder(c, buf+1, buf_size-1);
  114. if (ret < 0)
  115. return ret;
  116. s->golden_frame = vp56_rac_get(c);
  117. if (s->filter_header) {
  118. s->deblock_filtering = vp56_rac_get(c);
  119. if (s->deblock_filtering)
  120. vp56_rac_get(c);
  121. if (s->sub_version > 7)
  122. parse_filter_info = vp56_rac_get(c);
  123. }
  124. }
  125. if (parse_filter_info) {
  126. if (vp56_rac_get(c)) {
  127. s->filter_mode = 2;
  128. s->sample_variance_threshold = vp56_rac_gets(c, 5) << vrt_shift;
  129. s->max_vector_length = 2 << vp56_rac_gets(c, 3);
  130. } else if (vp56_rac_get(c)) {
  131. s->filter_mode = 1;
  132. } else {
  133. s->filter_mode = 0;
  134. }
  135. if (s->sub_version > 7)
  136. s->filter_selection = vp56_rac_gets(c, 4);
  137. else
  138. s->filter_selection = 16;
  139. }
  140. s->use_huffman = vp56_rac_get(c);
  141. s->parse_coeff = vp6_parse_coeff;
  142. if (coeff_offset) {
  143. buf += coeff_offset;
  144. buf_size -= coeff_offset;
  145. if (buf_size < 0) {
  146. ret = AVERROR_INVALIDDATA;
  147. goto fail;
  148. }
  149. if (s->use_huffman) {
  150. s->parse_coeff = vp6_parse_coeff_huffman;
  151. init_get_bits(&s->gb, buf, buf_size<<3);
  152. } else {
  153. ret = ff_vp56_init_range_decoder(&s->cc, buf, buf_size);
  154. if (ret < 0)
  155. goto fail;
  156. s->ccp = &s->cc;
  157. }
  158. } else {
  159. s->ccp = &s->c;
  160. }
  161. return res;
  162. fail:
  163. if (res == VP56_SIZE_CHANGE)
  164. ff_set_dimensions(s->avctx, 0, 0);
  165. return ret;
  166. }
  167. static void vp6_coeff_order_table_init(VP56Context *s)
  168. {
  169. int i, pos, idx = 1;
  170. s->modelp->coeff_index_to_pos[0] = 0;
  171. for (i=0; i<16; i++)
  172. for (pos=1; pos<64; pos++)
  173. if (s->modelp->coeff_reorder[pos] == i)
  174. s->modelp->coeff_index_to_pos[idx++] = pos;
  175. }
  176. static void vp6_default_models_init(VP56Context *s)
  177. {
  178. VP56Model *model = s->modelp;
  179. model->vector_dct[0] = 0xA2;
  180. model->vector_dct[1] = 0xA4;
  181. model->vector_sig[0] = 0x80;
  182. model->vector_sig[1] = 0x80;
  183. memcpy(model->mb_types_stats, ff_vp56_def_mb_types_stats, sizeof(model->mb_types_stats));
  184. memcpy(model->vector_fdv, vp6_def_fdv_vector_model, sizeof(model->vector_fdv));
  185. memcpy(model->vector_pdv, vp6_def_pdv_vector_model, sizeof(model->vector_pdv));
  186. memcpy(model->coeff_runv, vp6_def_runv_coeff_model, sizeof(model->coeff_runv));
  187. memcpy(model->coeff_reorder, vp6_def_coeff_reorder, sizeof(model->coeff_reorder));
  188. vp6_coeff_order_table_init(s);
  189. }
  190. static void vp6_parse_vector_models(VP56Context *s)
  191. {
  192. VP56RangeCoder *c = &s->c;
  193. VP56Model *model = s->modelp;
  194. int comp, node;
  195. for (comp=0; comp<2; comp++) {
  196. if (vp56_rac_get_prob_branchy(c, vp6_sig_dct_pct[comp][0]))
  197. model->vector_dct[comp] = vp56_rac_gets_nn(c, 7);
  198. if (vp56_rac_get_prob_branchy(c, vp6_sig_dct_pct[comp][1]))
  199. model->vector_sig[comp] = vp56_rac_gets_nn(c, 7);
  200. }
  201. for (comp=0; comp<2; comp++)
  202. for (node=0; node<7; node++)
  203. if (vp56_rac_get_prob_branchy(c, vp6_pdv_pct[comp][node]))
  204. model->vector_pdv[comp][node] = vp56_rac_gets_nn(c, 7);
  205. for (comp=0; comp<2; comp++)
  206. for (node=0; node<8; node++)
  207. if (vp56_rac_get_prob_branchy(c, vp6_fdv_pct[comp][node]))
  208. model->vector_fdv[comp][node] = vp56_rac_gets_nn(c, 7);
  209. }
  210. /* nodes must ascend by count, but with descending symbol order */
  211. static int vp6_huff_cmp(const void *va, const void *vb)
  212. {
  213. const Node *a = va, *b = vb;
  214. return (a->count - b->count)*16 + (b->sym - a->sym);
  215. }
  216. static int vp6_build_huff_tree(VP56Context *s, uint8_t coeff_model[],
  217. const uint8_t *map, unsigned size, VLC *vlc)
  218. {
  219. Node nodes[2*VP6_MAX_HUFF_SIZE], *tmp = &nodes[size];
  220. int a, b, i;
  221. /* first compute probabilities from model */
  222. tmp[0].count = 256;
  223. for (i=0; i<size-1; i++) {
  224. a = tmp[i].count * coeff_model[i] >> 8;
  225. b = tmp[i].count * (255 - coeff_model[i]) >> 8;
  226. nodes[map[2*i ]].count = a + !a;
  227. nodes[map[2*i+1]].count = b + !b;
  228. }
  229. ff_free_vlc(vlc);
  230. /* then build the huffman tree according to probabilities */
  231. return ff_huff_build_tree(s->avctx, vlc, size, FF_HUFFMAN_BITS,
  232. nodes, vp6_huff_cmp,
  233. FF_HUFFMAN_FLAG_HNODE_FIRST);
  234. }
  235. static int vp6_parse_coeff_models(VP56Context *s)
  236. {
  237. VP56RangeCoder *c = &s->c;
  238. VP56Model *model = s->modelp;
  239. int def_prob[11];
  240. int node, cg, ctx, pos;
  241. int ct; /* code type */
  242. int pt; /* plane type (0 for Y, 1 for U or V) */
  243. memset(def_prob, 0x80, sizeof(def_prob));
  244. for (pt=0; pt<2; pt++)
  245. for (node=0; node<11; node++)
  246. if (vp56_rac_get_prob_branchy(c, vp6_dccv_pct[pt][node])) {
  247. def_prob[node] = vp56_rac_gets_nn(c, 7);
  248. model->coeff_dccv[pt][node] = def_prob[node];
  249. } else if (s->frames[VP56_FRAME_CURRENT]->key_frame) {
  250. model->coeff_dccv[pt][node] = def_prob[node];
  251. }
  252. if (vp56_rac_get(c)) {
  253. for (pos=1; pos<64; pos++)
  254. if (vp56_rac_get_prob_branchy(c, vp6_coeff_reorder_pct[pos]))
  255. model->coeff_reorder[pos] = vp56_rac_gets(c, 4);
  256. vp6_coeff_order_table_init(s);
  257. }
  258. for (cg=0; cg<2; cg++)
  259. for (node=0; node<14; node++)
  260. if (vp56_rac_get_prob_branchy(c, vp6_runv_pct[cg][node]))
  261. model->coeff_runv[cg][node] = vp56_rac_gets_nn(c, 7);
  262. for (ct=0; ct<3; ct++)
  263. for (pt=0; pt<2; pt++)
  264. for (cg=0; cg<6; cg++)
  265. for (node=0; node<11; node++)
  266. if (vp56_rac_get_prob_branchy(c, vp6_ract_pct[ct][pt][cg][node])) {
  267. def_prob[node] = vp56_rac_gets_nn(c, 7);
  268. model->coeff_ract[pt][ct][cg][node] = def_prob[node];
  269. } else if (s->frames[VP56_FRAME_CURRENT]->key_frame) {
  270. model->coeff_ract[pt][ct][cg][node] = def_prob[node];
  271. }
  272. if (s->use_huffman) {
  273. for (pt=0; pt<2; pt++) {
  274. if (vp6_build_huff_tree(s, model->coeff_dccv[pt],
  275. vp6_huff_coeff_map, 12, &s->dccv_vlc[pt]))
  276. return -1;
  277. if (vp6_build_huff_tree(s, model->coeff_runv[pt],
  278. vp6_huff_run_map, 9, &s->runv_vlc[pt]))
  279. return -1;
  280. for (ct=0; ct<3; ct++)
  281. for (cg = 0; cg < 6; cg++)
  282. if (vp6_build_huff_tree(s, model->coeff_ract[pt][ct][cg],
  283. vp6_huff_coeff_map, 12,
  284. &s->ract_vlc[pt][ct][cg]))
  285. return -1;
  286. }
  287. memset(s->nb_null, 0, sizeof(s->nb_null));
  288. } else {
  289. /* coeff_dcct is a linear combination of coeff_dccv */
  290. for (pt=0; pt<2; pt++)
  291. for (ctx=0; ctx<3; ctx++)
  292. for (node=0; node<5; node++)
  293. model->coeff_dcct[pt][ctx][node] = av_clip(((model->coeff_dccv[pt][node] * vp6_dccv_lc[ctx][node][0] + 128) >> 8) + vp6_dccv_lc[ctx][node][1], 1, 255);
  294. }
  295. return 0;
  296. }
  297. static void vp6_parse_vector_adjustment(VP56Context *s, VP56mv *vect)
  298. {
  299. VP56RangeCoder *c = &s->c;
  300. VP56Model *model = s->modelp;
  301. int comp;
  302. *vect = (VP56mv) {0,0};
  303. if (s->vector_candidate_pos < 2)
  304. *vect = s->vector_candidate[0];
  305. for (comp=0; comp<2; comp++) {
  306. int i, delta = 0;
  307. if (vp56_rac_get_prob_branchy(c, model->vector_dct[comp])) {
  308. static const uint8_t prob_order[] = {0, 1, 2, 7, 6, 5, 4};
  309. for (i=0; i<sizeof(prob_order); i++) {
  310. int j = prob_order[i];
  311. delta |= vp56_rac_get_prob(c, model->vector_fdv[comp][j])<<j;
  312. }
  313. if (delta & 0xF0)
  314. delta |= vp56_rac_get_prob(c, model->vector_fdv[comp][3])<<3;
  315. else
  316. delta |= 8;
  317. } else {
  318. delta = vp56_rac_get_tree(c, ff_vp56_pva_tree,
  319. model->vector_pdv[comp]);
  320. }
  321. if (delta && vp56_rac_get_prob_branchy(c, model->vector_sig[comp]))
  322. delta = -delta;
  323. if (!comp)
  324. vect->x += delta;
  325. else
  326. vect->y += delta;
  327. }
  328. }
  329. /**
  330. * Read number of consecutive blocks with null DC or AC.
  331. * This value is < 74.
  332. */
  333. static unsigned vp6_get_nb_null(VP56Context *s)
  334. {
  335. unsigned val = get_bits(&s->gb, 2);
  336. if (val == 2)
  337. val += get_bits(&s->gb, 2);
  338. else if (val == 3) {
  339. val = get_bits1(&s->gb) << 2;
  340. val = 6+val + get_bits(&s->gb, 2+val);
  341. }
  342. return val;
  343. }
  344. static int vp6_parse_coeff_huffman(VP56Context *s)
  345. {
  346. VP56Model *model = s->modelp;
  347. uint8_t *permute = s->idct_scantable;
  348. VLC *vlc_coeff;
  349. int coeff, sign, coeff_idx;
  350. int b, cg, idx;
  351. int pt = 0; /* plane type (0 for Y, 1 for U or V) */
  352. for (b=0; b<6; b++) {
  353. int ct = 0; /* code type */
  354. if (b > 3) pt = 1;
  355. vlc_coeff = &s->dccv_vlc[pt];
  356. for (coeff_idx = 0;;) {
  357. int run = 1;
  358. if (coeff_idx<2 && s->nb_null[coeff_idx][pt]) {
  359. s->nb_null[coeff_idx][pt]--;
  360. if (coeff_idx)
  361. break;
  362. } else {
  363. if (get_bits_left(&s->gb) <= 0)
  364. return AVERROR_INVALIDDATA;
  365. coeff = get_vlc2(&s->gb, vlc_coeff->table, FF_HUFFMAN_BITS, 3);
  366. if (coeff == 0) {
  367. if (coeff_idx) {
  368. int pt = (coeff_idx >= 6);
  369. run += get_vlc2(&s->gb, s->runv_vlc[pt].table, FF_HUFFMAN_BITS, 3);
  370. if (run >= 9)
  371. run += get_bits(&s->gb, 6);
  372. } else
  373. s->nb_null[0][pt] = vp6_get_nb_null(s);
  374. ct = 0;
  375. } else if (coeff == 11) { /* end of block */
  376. if (coeff_idx == 1) /* first AC coeff ? */
  377. s->nb_null[1][pt] = vp6_get_nb_null(s);
  378. break;
  379. } else {
  380. int coeff2 = ff_vp56_coeff_bias[coeff];
  381. if (coeff > 4)
  382. coeff2 += get_bits(&s->gb, coeff <= 9 ? coeff - 4 : 11);
  383. ct = 1 + (coeff2 > 1);
  384. sign = get_bits1(&s->gb);
  385. coeff2 = (coeff2 ^ -sign) + sign;
  386. if (coeff_idx)
  387. coeff2 *= s->dequant_ac;
  388. idx = model->coeff_index_to_pos[coeff_idx];
  389. s->block_coeff[b][permute[idx]] = coeff2;
  390. }
  391. }
  392. coeff_idx+=run;
  393. if (coeff_idx >= 64)
  394. break;
  395. cg = FFMIN(vp6_coeff_groups[coeff_idx], 3);
  396. vlc_coeff = &s->ract_vlc[pt][ct][cg];
  397. }
  398. }
  399. return 0;
  400. }
  401. static int vp6_parse_coeff(VP56Context *s)
  402. {
  403. VP56RangeCoder *c = s->ccp;
  404. VP56Model *model = s->modelp;
  405. uint8_t *permute = s->idct_scantable;
  406. uint8_t *model1, *model2, *model3;
  407. int coeff, sign, coeff_idx;
  408. int b, i, cg, idx, ctx;
  409. int pt = 0; /* plane type (0 for Y, 1 for U or V) */
  410. if (c->end <= c->buffer && c->bits >= 0) {
  411. av_log(s->avctx, AV_LOG_ERROR, "End of AC stream reached in vp6_parse_coeff\n");
  412. return AVERROR_INVALIDDATA;
  413. }
  414. for (b=0; b<6; b++) {
  415. int ct = 1; /* code type */
  416. int run = 1;
  417. if (b > 3) pt = 1;
  418. ctx = s->left_block[ff_vp56_b6to4[b]].not_null_dc
  419. + s->above_blocks[s->above_block_idx[b]].not_null_dc;
  420. model1 = model->coeff_dccv[pt];
  421. model2 = model->coeff_dcct[pt][ctx];
  422. coeff_idx = 0;
  423. for (;;) {
  424. if ((coeff_idx>1 && ct==0) || vp56_rac_get_prob_branchy(c, model2[0])) {
  425. /* parse a coeff */
  426. if (vp56_rac_get_prob_branchy(c, model2[2])) {
  427. if (vp56_rac_get_prob_branchy(c, model2[3])) {
  428. idx = vp56_rac_get_tree(c, ff_vp56_pc_tree, model1);
  429. coeff = ff_vp56_coeff_bias[idx+5];
  430. for (i=ff_vp56_coeff_bit_length[idx]; i>=0; i--)
  431. coeff += vp56_rac_get_prob(c, ff_vp56_coeff_parse_table[idx][i]) << i;
  432. } else {
  433. if (vp56_rac_get_prob_branchy(c, model2[4]))
  434. coeff = 3 + vp56_rac_get_prob(c, model1[5]);
  435. else
  436. coeff = 2;
  437. }
  438. ct = 2;
  439. } else {
  440. ct = 1;
  441. coeff = 1;
  442. }
  443. sign = vp56_rac_get(c);
  444. coeff = (coeff ^ -sign) + sign;
  445. if (coeff_idx)
  446. coeff *= s->dequant_ac;
  447. idx = model->coeff_index_to_pos[coeff_idx];
  448. s->block_coeff[b][permute[idx]] = coeff;
  449. run = 1;
  450. } else {
  451. /* parse a run */
  452. ct = 0;
  453. if (coeff_idx > 0) {
  454. if (!vp56_rac_get_prob_branchy(c, model2[1]))
  455. break;
  456. model3 = model->coeff_runv[coeff_idx >= 6];
  457. run = vp56_rac_get_tree(c, vp6_pcr_tree, model3);
  458. if (!run)
  459. for (run=9, i=0; i<6; i++)
  460. run += vp56_rac_get_prob(c, model3[i+8]) << i;
  461. }
  462. }
  463. coeff_idx += run;
  464. if (coeff_idx >= 64)
  465. break;
  466. cg = vp6_coeff_groups[coeff_idx];
  467. model1 = model2 = model->coeff_ract[pt][ct][cg];
  468. }
  469. s->left_block[ff_vp56_b6to4[b]].not_null_dc =
  470. s->above_blocks[s->above_block_idx[b]].not_null_dc = !!s->block_coeff[b][0];
  471. }
  472. return 0;
  473. }
  474. static int vp6_block_variance(uint8_t *src, int stride)
  475. {
  476. int sum = 0, square_sum = 0;
  477. int y, x;
  478. for (y=0; y<8; y+=2) {
  479. for (x=0; x<8; x+=2) {
  480. sum += src[x];
  481. square_sum += src[x]*src[x];
  482. }
  483. src += 2*stride;
  484. }
  485. return (16*square_sum - sum*sum) >> 8;
  486. }
  487. static void vp6_filter_hv4(uint8_t *dst, uint8_t *src, int stride,
  488. int delta, const int16_t *weights)
  489. {
  490. int x, y;
  491. for (y=0; y<8; y++) {
  492. for (x=0; x<8; x++) {
  493. dst[x] = av_clip_uint8(( src[x-delta ] * weights[0]
  494. + src[x ] * weights[1]
  495. + src[x+delta ] * weights[2]
  496. + src[x+2*delta] * weights[3] + 64) >> 7);
  497. }
  498. src += stride;
  499. dst += stride;
  500. }
  501. }
  502. static void vp6_filter_diag2(VP56Context *s, uint8_t *dst, uint8_t *src,
  503. int stride, int h_weight, int v_weight)
  504. {
  505. uint8_t *tmp = s->edge_emu_buffer+16;
  506. s->h264chroma.put_h264_chroma_pixels_tab[0](tmp, src, stride, 9, h_weight, 0);
  507. s->h264chroma.put_h264_chroma_pixels_tab[0](dst, tmp, stride, 8, 0, v_weight);
  508. }
  509. static void vp6_filter(VP56Context *s, uint8_t *dst, uint8_t *src,
  510. int offset1, int offset2, int stride,
  511. VP56mv mv, int mask, int select, int luma)
  512. {
  513. int filter4 = 0;
  514. int x8 = mv.x & mask;
  515. int y8 = mv.y & mask;
  516. if (luma) {
  517. x8 *= 2;
  518. y8 *= 2;
  519. filter4 = s->filter_mode;
  520. if (filter4 == 2) {
  521. if (s->max_vector_length &&
  522. (FFABS(mv.x) > s->max_vector_length ||
  523. FFABS(mv.y) > s->max_vector_length)) {
  524. filter4 = 0;
  525. } else if (s->sample_variance_threshold
  526. && (vp6_block_variance(src+offset1, stride)
  527. < s->sample_variance_threshold)) {
  528. filter4 = 0;
  529. }
  530. }
  531. }
  532. if ((y8 && (offset2-offset1)*s->flip<0) || (!y8 && offset1 > offset2)) {
  533. offset1 = offset2;
  534. }
  535. if (filter4) {
  536. if (!y8) { /* left or right combine */
  537. vp6_filter_hv4(dst, src+offset1, stride, 1,
  538. vp6_block_copy_filter[select][x8]);
  539. } else if (!x8) { /* above or below combine */
  540. vp6_filter_hv4(dst, src+offset1, stride, stride,
  541. vp6_block_copy_filter[select][y8]);
  542. } else {
  543. s->vp56dsp.vp6_filter_diag4(dst, src+offset1+((mv.x^mv.y)>>31), stride,
  544. vp6_block_copy_filter[select][x8],
  545. vp6_block_copy_filter[select][y8]);
  546. }
  547. } else {
  548. if (!x8 || !y8) {
  549. s->h264chroma.put_h264_chroma_pixels_tab[0](dst, src + offset1, stride, 8, x8, y8);
  550. } else {
  551. vp6_filter_diag2(s, dst, src+offset1 + ((mv.x^mv.y)>>31), stride, x8, y8);
  552. }
  553. }
  554. }
  555. static av_cold void vp6_decode_init_context(VP56Context *s);
  556. static av_cold int vp6_decode_init(AVCodecContext *avctx)
  557. {
  558. VP56Context *s = avctx->priv_data;
  559. int ret;
  560. if ((ret = ff_vp56_init(avctx, avctx->codec->id == AV_CODEC_ID_VP6,
  561. avctx->codec->id == AV_CODEC_ID_VP6A)) < 0)
  562. return ret;
  563. vp6_decode_init_context(s);
  564. if (s->has_alpha) {
  565. s->alpha_context = av_mallocz(sizeof(VP56Context));
  566. ff_vp56_init_context(avctx, s->alpha_context,
  567. s->flip == -1, s->has_alpha);
  568. vp6_decode_init_context(s->alpha_context);
  569. }
  570. return 0;
  571. }
  572. static av_cold void vp6_decode_init_context(VP56Context *s)
  573. {
  574. s->deblock_filtering = 0;
  575. s->vp56_coord_div = vp6_coord_div;
  576. s->parse_vector_adjustment = vp6_parse_vector_adjustment;
  577. s->filter = vp6_filter;
  578. s->default_models_init = vp6_default_models_init;
  579. s->parse_vector_models = vp6_parse_vector_models;
  580. s->parse_coeff_models = vp6_parse_coeff_models;
  581. s->parse_header = vp6_parse_header;
  582. }
  583. static av_cold void vp6_decode_free_context(VP56Context *s);
  584. static av_cold int vp6_decode_free(AVCodecContext *avctx)
  585. {
  586. VP56Context *s = avctx->priv_data;
  587. ff_vp56_free(avctx);
  588. vp6_decode_free_context(s);
  589. if (s->alpha_context) {
  590. ff_vp56_free_context(s->alpha_context);
  591. vp6_decode_free_context(s->alpha_context);
  592. av_freep(&s->alpha_context);
  593. }
  594. return 0;
  595. }
  596. static av_cold void vp6_decode_free_context(VP56Context *s)
  597. {
  598. int pt, ct, cg;
  599. for (pt=0; pt<2; pt++) {
  600. ff_free_vlc(&s->dccv_vlc[pt]);
  601. ff_free_vlc(&s->runv_vlc[pt]);
  602. for (ct=0; ct<3; ct++)
  603. for (cg=0; cg<6; cg++)
  604. ff_free_vlc(&s->ract_vlc[pt][ct][cg]);
  605. }
  606. }
  607. AVCodec ff_vp6_decoder = {
  608. .name = "vp6",
  609. .long_name = NULL_IF_CONFIG_SMALL("On2 VP6"),
  610. .type = AVMEDIA_TYPE_VIDEO,
  611. .id = AV_CODEC_ID_VP6,
  612. .priv_data_size = sizeof(VP56Context),
  613. .init = vp6_decode_init,
  614. .close = vp6_decode_free,
  615. .decode = ff_vp56_decode_frame,
  616. .capabilities = AV_CODEC_CAP_DR1,
  617. };
  618. /* flash version, not flipped upside-down */
  619. AVCodec ff_vp6f_decoder = {
  620. .name = "vp6f",
  621. .long_name = NULL_IF_CONFIG_SMALL("On2 VP6 (Flash version)"),
  622. .type = AVMEDIA_TYPE_VIDEO,
  623. .id = AV_CODEC_ID_VP6F,
  624. .priv_data_size = sizeof(VP56Context),
  625. .init = vp6_decode_init,
  626. .close = vp6_decode_free,
  627. .decode = ff_vp56_decode_frame,
  628. .capabilities = AV_CODEC_CAP_DR1,
  629. };
  630. /* flash version, not flipped upside-down, with alpha channel */
  631. AVCodec ff_vp6a_decoder = {
  632. .name = "vp6a",
  633. .long_name = NULL_IF_CONFIG_SMALL("On2 VP6 (Flash version, with alpha channel)"),
  634. .type = AVMEDIA_TYPE_VIDEO,
  635. .id = AV_CODEC_ID_VP6A,
  636. .priv_data_size = sizeof(VP56Context),
  637. .init = vp6_decode_init,
  638. .close = vp6_decode_free,
  639. .decode = ff_vp56_decode_frame,
  640. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS,
  641. };