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.

743 lines
25KB

  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. for (idx = 0; idx < 64; idx++) {
  176. int max = 0;
  177. for (i = 0; i <= idx; i++) {
  178. int v = s->modelp->coeff_index_to_pos[i];
  179. if (v > max)
  180. max = v;
  181. }
  182. if (s->sub_version > 6)
  183. max++;
  184. s->modelp->coeff_index_to_idct_selector[idx] = max;
  185. }
  186. }
  187. static void vp6_default_models_init(VP56Context *s)
  188. {
  189. VP56Model *model = s->modelp;
  190. model->vector_dct[0] = 0xA2;
  191. model->vector_dct[1] = 0xA4;
  192. model->vector_sig[0] = 0x80;
  193. model->vector_sig[1] = 0x80;
  194. memcpy(model->mb_types_stats, ff_vp56_def_mb_types_stats, sizeof(model->mb_types_stats));
  195. memcpy(model->vector_fdv, vp6_def_fdv_vector_model, sizeof(model->vector_fdv));
  196. memcpy(model->vector_pdv, vp6_def_pdv_vector_model, sizeof(model->vector_pdv));
  197. memcpy(model->coeff_runv, vp6_def_runv_coeff_model, sizeof(model->coeff_runv));
  198. memcpy(model->coeff_reorder, vp6_def_coeff_reorder, sizeof(model->coeff_reorder));
  199. vp6_coeff_order_table_init(s);
  200. }
  201. static void vp6_parse_vector_models(VP56Context *s)
  202. {
  203. VP56RangeCoder *c = &s->c;
  204. VP56Model *model = s->modelp;
  205. int comp, node;
  206. for (comp=0; comp<2; comp++) {
  207. if (vp56_rac_get_prob_branchy(c, vp6_sig_dct_pct[comp][0]))
  208. model->vector_dct[comp] = vp56_rac_gets_nn(c, 7);
  209. if (vp56_rac_get_prob_branchy(c, vp6_sig_dct_pct[comp][1]))
  210. model->vector_sig[comp] = vp56_rac_gets_nn(c, 7);
  211. }
  212. for (comp=0; comp<2; comp++)
  213. for (node=0; node<7; node++)
  214. if (vp56_rac_get_prob_branchy(c, vp6_pdv_pct[comp][node]))
  215. model->vector_pdv[comp][node] = vp56_rac_gets_nn(c, 7);
  216. for (comp=0; comp<2; comp++)
  217. for (node=0; node<8; node++)
  218. if (vp56_rac_get_prob_branchy(c, vp6_fdv_pct[comp][node]))
  219. model->vector_fdv[comp][node] = vp56_rac_gets_nn(c, 7);
  220. }
  221. /* nodes must ascend by count, but with descending symbol order */
  222. static int vp6_huff_cmp(const void *va, const void *vb)
  223. {
  224. const Node *a = va, *b = vb;
  225. return (a->count - b->count)*16 + (b->sym - a->sym);
  226. }
  227. static int vp6_build_huff_tree(VP56Context *s, uint8_t coeff_model[],
  228. const uint8_t *map, unsigned size, VLC *vlc)
  229. {
  230. Node nodes[2*VP6_MAX_HUFF_SIZE], *tmp = &nodes[size];
  231. int a, b, i;
  232. /* first compute probabilities from model */
  233. tmp[0].count = 256;
  234. for (i=0; i<size-1; i++) {
  235. a = tmp[i].count * coeff_model[i] >> 8;
  236. b = tmp[i].count * (255 - coeff_model[i]) >> 8;
  237. nodes[map[2*i ]].count = a + !a;
  238. nodes[map[2*i+1]].count = b + !b;
  239. }
  240. ff_free_vlc(vlc);
  241. /* then build the huffman tree according to probabilities */
  242. return ff_huff_build_tree(s->avctx, vlc, size, FF_HUFFMAN_BITS,
  243. nodes, vp6_huff_cmp,
  244. FF_HUFFMAN_FLAG_HNODE_FIRST);
  245. }
  246. static int vp6_parse_coeff_models(VP56Context *s)
  247. {
  248. VP56RangeCoder *c = &s->c;
  249. VP56Model *model = s->modelp;
  250. int def_prob[11];
  251. int node, cg, ctx, pos;
  252. int ct; /* code type */
  253. int pt; /* plane type (0 for Y, 1 for U or V) */
  254. memset(def_prob, 0x80, sizeof(def_prob));
  255. for (pt=0; pt<2; pt++)
  256. for (node=0; node<11; node++)
  257. if (vp56_rac_get_prob_branchy(c, vp6_dccv_pct[pt][node])) {
  258. def_prob[node] = vp56_rac_gets_nn(c, 7);
  259. model->coeff_dccv[pt][node] = def_prob[node];
  260. } else if (s->frames[VP56_FRAME_CURRENT]->key_frame) {
  261. model->coeff_dccv[pt][node] = def_prob[node];
  262. }
  263. if (vp56_rac_get(c)) {
  264. for (pos=1; pos<64; pos++)
  265. if (vp56_rac_get_prob_branchy(c, vp6_coeff_reorder_pct[pos]))
  266. model->coeff_reorder[pos] = vp56_rac_gets(c, 4);
  267. vp6_coeff_order_table_init(s);
  268. }
  269. for (cg=0; cg<2; cg++)
  270. for (node=0; node<14; node++)
  271. if (vp56_rac_get_prob_branchy(c, vp6_runv_pct[cg][node]))
  272. model->coeff_runv[cg][node] = vp56_rac_gets_nn(c, 7);
  273. for (ct=0; ct<3; ct++)
  274. for (pt=0; pt<2; pt++)
  275. for (cg=0; cg<6; cg++)
  276. for (node=0; node<11; node++)
  277. if (vp56_rac_get_prob_branchy(c, vp6_ract_pct[ct][pt][cg][node])) {
  278. def_prob[node] = vp56_rac_gets_nn(c, 7);
  279. model->coeff_ract[pt][ct][cg][node] = def_prob[node];
  280. } else if (s->frames[VP56_FRAME_CURRENT]->key_frame) {
  281. model->coeff_ract[pt][ct][cg][node] = def_prob[node];
  282. }
  283. if (s->use_huffman) {
  284. for (pt=0; pt<2; pt++) {
  285. if (vp6_build_huff_tree(s, model->coeff_dccv[pt],
  286. vp6_huff_coeff_map, 12, &s->dccv_vlc[pt]))
  287. return -1;
  288. if (vp6_build_huff_tree(s, model->coeff_runv[pt],
  289. vp6_huff_run_map, 9, &s->runv_vlc[pt]))
  290. return -1;
  291. for (ct=0; ct<3; ct++)
  292. for (cg = 0; cg < 6; cg++)
  293. if (vp6_build_huff_tree(s, model->coeff_ract[pt][ct][cg],
  294. vp6_huff_coeff_map, 12,
  295. &s->ract_vlc[pt][ct][cg]))
  296. return -1;
  297. }
  298. memset(s->nb_null, 0, sizeof(s->nb_null));
  299. } else {
  300. /* coeff_dcct is a linear combination of coeff_dccv */
  301. for (pt=0; pt<2; pt++)
  302. for (ctx=0; ctx<3; ctx++)
  303. for (node=0; node<5; node++)
  304. 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);
  305. }
  306. return 0;
  307. }
  308. static void vp6_parse_vector_adjustment(VP56Context *s, VP56mv *vect)
  309. {
  310. VP56RangeCoder *c = &s->c;
  311. VP56Model *model = s->modelp;
  312. int comp;
  313. *vect = (VP56mv) {0,0};
  314. if (s->vector_candidate_pos < 2)
  315. *vect = s->vector_candidate[0];
  316. for (comp=0; comp<2; comp++) {
  317. int i, delta = 0;
  318. if (vp56_rac_get_prob_branchy(c, model->vector_dct[comp])) {
  319. static const uint8_t prob_order[] = {0, 1, 2, 7, 6, 5, 4};
  320. for (i=0; i<sizeof(prob_order); i++) {
  321. int j = prob_order[i];
  322. delta |= vp56_rac_get_prob(c, model->vector_fdv[comp][j])<<j;
  323. }
  324. if (delta & 0xF0)
  325. delta |= vp56_rac_get_prob(c, model->vector_fdv[comp][3])<<3;
  326. else
  327. delta |= 8;
  328. } else {
  329. delta = vp56_rac_get_tree(c, ff_vp56_pva_tree,
  330. model->vector_pdv[comp]);
  331. }
  332. if (delta && vp56_rac_get_prob_branchy(c, model->vector_sig[comp]))
  333. delta = -delta;
  334. if (!comp)
  335. vect->x += delta;
  336. else
  337. vect->y += delta;
  338. }
  339. }
  340. /**
  341. * Read number of consecutive blocks with null DC or AC.
  342. * This value is < 74.
  343. */
  344. static unsigned vp6_get_nb_null(VP56Context *s)
  345. {
  346. unsigned val = get_bits(&s->gb, 2);
  347. if (val == 2)
  348. val += get_bits(&s->gb, 2);
  349. else if (val == 3) {
  350. val = get_bits1(&s->gb) << 2;
  351. val = 6+val + get_bits(&s->gb, 2+val);
  352. }
  353. return val;
  354. }
  355. static int vp6_parse_coeff_huffman(VP56Context *s)
  356. {
  357. VP56Model *model = s->modelp;
  358. uint8_t *permute = s->idct_scantable;
  359. VLC *vlc_coeff;
  360. int coeff, sign, coeff_idx;
  361. int b, cg, idx;
  362. int pt = 0; /* plane type (0 for Y, 1 for U or V) */
  363. for (b=0; b<6; b++) {
  364. int ct = 0; /* code type */
  365. if (b > 3) pt = 1;
  366. vlc_coeff = &s->dccv_vlc[pt];
  367. for (coeff_idx = 0;;) {
  368. int run = 1;
  369. if (coeff_idx<2 && s->nb_null[coeff_idx][pt]) {
  370. s->nb_null[coeff_idx][pt]--;
  371. if (coeff_idx)
  372. break;
  373. } else {
  374. if (get_bits_left(&s->gb) <= 0)
  375. return AVERROR_INVALIDDATA;
  376. coeff = get_vlc2(&s->gb, vlc_coeff->table, FF_HUFFMAN_BITS, 3);
  377. if (coeff == 0) {
  378. if (coeff_idx) {
  379. int pt = (coeff_idx >= 6);
  380. run += get_vlc2(&s->gb, s->runv_vlc[pt].table, FF_HUFFMAN_BITS, 3);
  381. if (run >= 9)
  382. run += get_bits(&s->gb, 6);
  383. } else
  384. s->nb_null[0][pt] = vp6_get_nb_null(s);
  385. ct = 0;
  386. } else if (coeff == 11) { /* end of block */
  387. if (coeff_idx == 1) /* first AC coeff ? */
  388. s->nb_null[1][pt] = vp6_get_nb_null(s);
  389. break;
  390. } else {
  391. int coeff2 = ff_vp56_coeff_bias[coeff];
  392. if (coeff > 4)
  393. coeff2 += get_bits(&s->gb, coeff <= 9 ? coeff - 4 : 11);
  394. ct = 1 + (coeff2 > 1);
  395. sign = get_bits1(&s->gb);
  396. coeff2 = (coeff2 ^ -sign) + sign;
  397. if (coeff_idx)
  398. coeff2 *= s->dequant_ac;
  399. idx = model->coeff_index_to_pos[coeff_idx];
  400. s->block_coeff[b][permute[idx]] = coeff2;
  401. }
  402. }
  403. coeff_idx+=run;
  404. if (coeff_idx >= 64)
  405. break;
  406. cg = FFMIN(vp6_coeff_groups[coeff_idx], 3);
  407. vlc_coeff = &s->ract_vlc[pt][ct][cg];
  408. }
  409. s->idct_selector[b] = model->coeff_index_to_idct_selector[FFMIN(coeff_idx, 63)];
  410. }
  411. return 0;
  412. }
  413. static int vp6_parse_coeff(VP56Context *s)
  414. {
  415. VP56RangeCoder *c = s->ccp;
  416. VP56Model *model = s->modelp;
  417. uint8_t *permute = s->idct_scantable;
  418. uint8_t *model1, *model2, *model3;
  419. int coeff, sign, coeff_idx;
  420. int b, i, cg, idx, ctx;
  421. int pt = 0; /* plane type (0 for Y, 1 for U or V) */
  422. if (c->end <= c->buffer && c->bits >= 0) {
  423. av_log(s->avctx, AV_LOG_ERROR, "End of AC stream reached in vp6_parse_coeff\n");
  424. return AVERROR_INVALIDDATA;
  425. }
  426. for (b=0; b<6; b++) {
  427. int ct = 1; /* code type */
  428. int run = 1;
  429. if (b > 3) pt = 1;
  430. ctx = s->left_block[ff_vp56_b6to4[b]].not_null_dc
  431. + s->above_blocks[s->above_block_idx[b]].not_null_dc;
  432. model1 = model->coeff_dccv[pt];
  433. model2 = model->coeff_dcct[pt][ctx];
  434. coeff_idx = 0;
  435. for (;;) {
  436. if ((coeff_idx>1 && ct==0) || vp56_rac_get_prob_branchy(c, model2[0])) {
  437. /* parse a coeff */
  438. if (vp56_rac_get_prob_branchy(c, model2[2])) {
  439. if (vp56_rac_get_prob_branchy(c, model2[3])) {
  440. idx = vp56_rac_get_tree(c, ff_vp56_pc_tree, model1);
  441. coeff = ff_vp56_coeff_bias[idx+5];
  442. for (i=ff_vp56_coeff_bit_length[idx]; i>=0; i--)
  443. coeff += vp56_rac_get_prob(c, ff_vp56_coeff_parse_table[idx][i]) << i;
  444. } else {
  445. if (vp56_rac_get_prob_branchy(c, model2[4]))
  446. coeff = 3 + vp56_rac_get_prob(c, model1[5]);
  447. else
  448. coeff = 2;
  449. }
  450. ct = 2;
  451. } else {
  452. ct = 1;
  453. coeff = 1;
  454. }
  455. sign = vp56_rac_get(c);
  456. coeff = (coeff ^ -sign) + sign;
  457. if (coeff_idx)
  458. coeff *= s->dequant_ac;
  459. idx = model->coeff_index_to_pos[coeff_idx];
  460. s->block_coeff[b][permute[idx]] = coeff;
  461. run = 1;
  462. } else {
  463. /* parse a run */
  464. ct = 0;
  465. if (coeff_idx > 0) {
  466. if (!vp56_rac_get_prob_branchy(c, model2[1]))
  467. break;
  468. model3 = model->coeff_runv[coeff_idx >= 6];
  469. run = vp56_rac_get_tree(c, vp6_pcr_tree, model3);
  470. if (!run)
  471. for (run=9, i=0; i<6; i++)
  472. run += vp56_rac_get_prob(c, model3[i+8]) << i;
  473. }
  474. }
  475. coeff_idx += run;
  476. if (coeff_idx >= 64)
  477. break;
  478. cg = vp6_coeff_groups[coeff_idx];
  479. model1 = model2 = model->coeff_ract[pt][ct][cg];
  480. }
  481. s->left_block[ff_vp56_b6to4[b]].not_null_dc =
  482. s->above_blocks[s->above_block_idx[b]].not_null_dc = !!s->block_coeff[b][0];
  483. s->idct_selector[b] = model->coeff_index_to_idct_selector[FFMIN(coeff_idx, 63)];
  484. }
  485. return 0;
  486. }
  487. static int vp6_block_variance(uint8_t *src, ptrdiff_t stride)
  488. {
  489. int sum = 0, square_sum = 0;
  490. int y, x;
  491. for (y=0; y<8; y+=2) {
  492. for (x=0; x<8; x+=2) {
  493. sum += src[x];
  494. square_sum += src[x]*src[x];
  495. }
  496. src += 2*stride;
  497. }
  498. return (16*square_sum - sum*sum) >> 8;
  499. }
  500. static void vp6_filter_hv4(uint8_t *dst, uint8_t *src, ptrdiff_t stride,
  501. int delta, const int16_t *weights)
  502. {
  503. int x, y;
  504. for (y=0; y<8; y++) {
  505. for (x=0; x<8; x++) {
  506. dst[x] = av_clip_uint8(( src[x-delta ] * weights[0]
  507. + src[x ] * weights[1]
  508. + src[x+delta ] * weights[2]
  509. + src[x+2*delta] * weights[3] + 64) >> 7);
  510. }
  511. src += stride;
  512. dst += stride;
  513. }
  514. }
  515. static void vp6_filter_diag2(VP56Context *s, uint8_t *dst, uint8_t *src,
  516. ptrdiff_t stride, int h_weight, int v_weight)
  517. {
  518. uint8_t *tmp = s->edge_emu_buffer+16;
  519. s->h264chroma.put_h264_chroma_pixels_tab[0](tmp, src, stride, 9, h_weight, 0);
  520. s->h264chroma.put_h264_chroma_pixels_tab[0](dst, tmp, stride, 8, 0, v_weight);
  521. }
  522. static void vp6_filter(VP56Context *s, uint8_t *dst, uint8_t *src,
  523. int offset1, int offset2, ptrdiff_t stride,
  524. VP56mv mv, int mask, int select, int luma)
  525. {
  526. int filter4 = 0;
  527. int x8 = mv.x & mask;
  528. int y8 = mv.y & mask;
  529. if (luma) {
  530. x8 *= 2;
  531. y8 *= 2;
  532. filter4 = s->filter_mode;
  533. if (filter4 == 2) {
  534. if (s->max_vector_length &&
  535. (FFABS(mv.x) > s->max_vector_length ||
  536. FFABS(mv.y) > s->max_vector_length)) {
  537. filter4 = 0;
  538. } else if (s->sample_variance_threshold
  539. && (vp6_block_variance(src+offset1, stride)
  540. < s->sample_variance_threshold)) {
  541. filter4 = 0;
  542. }
  543. }
  544. }
  545. if ((y8 && (offset2-offset1)*s->flip<0) || (!y8 && offset1 > offset2)) {
  546. offset1 = offset2;
  547. }
  548. if (filter4) {
  549. if (!y8) { /* left or right combine */
  550. vp6_filter_hv4(dst, src+offset1, stride, 1,
  551. vp6_block_copy_filter[select][x8]);
  552. } else if (!x8) { /* above or below combine */
  553. vp6_filter_hv4(dst, src+offset1, stride, stride,
  554. vp6_block_copy_filter[select][y8]);
  555. } else {
  556. s->vp56dsp.vp6_filter_diag4(dst, src+offset1+((mv.x^mv.y)>>31), stride,
  557. vp6_block_copy_filter[select][x8],
  558. vp6_block_copy_filter[select][y8]);
  559. }
  560. } else {
  561. if (!x8 || !y8) {
  562. s->h264chroma.put_h264_chroma_pixels_tab[0](dst, src + offset1, stride, 8, x8, y8);
  563. } else {
  564. vp6_filter_diag2(s, dst, src+offset1 + ((mv.x^mv.y)>>31), stride, x8, y8);
  565. }
  566. }
  567. }
  568. static av_cold void vp6_decode_init_context(VP56Context *s);
  569. static av_cold int vp6_decode_init(AVCodecContext *avctx)
  570. {
  571. VP56Context *s = avctx->priv_data;
  572. int ret;
  573. if ((ret = ff_vp56_init(avctx, avctx->codec->id == AV_CODEC_ID_VP6,
  574. avctx->codec->id == AV_CODEC_ID_VP6A)) < 0)
  575. return ret;
  576. ff_vp6dsp_init(&s->vp56dsp);
  577. vp6_decode_init_context(s);
  578. if (s->has_alpha) {
  579. s->alpha_context = av_mallocz(sizeof(VP56Context));
  580. ff_vp56_init_context(avctx, s->alpha_context,
  581. s->flip == -1, s->has_alpha);
  582. ff_vp6dsp_init(&s->alpha_context->vp56dsp);
  583. vp6_decode_init_context(s->alpha_context);
  584. }
  585. return 0;
  586. }
  587. static av_cold void vp6_decode_init_context(VP56Context *s)
  588. {
  589. s->deblock_filtering = 0;
  590. s->vp56_coord_div = vp6_coord_div;
  591. s->parse_vector_adjustment = vp6_parse_vector_adjustment;
  592. s->filter = vp6_filter;
  593. s->default_models_init = vp6_default_models_init;
  594. s->parse_vector_models = vp6_parse_vector_models;
  595. s->parse_coeff_models = vp6_parse_coeff_models;
  596. s->parse_header = vp6_parse_header;
  597. }
  598. static av_cold void vp6_decode_free_context(VP56Context *s);
  599. static av_cold int vp6_decode_free(AVCodecContext *avctx)
  600. {
  601. VP56Context *s = avctx->priv_data;
  602. ff_vp56_free(avctx);
  603. vp6_decode_free_context(s);
  604. if (s->alpha_context) {
  605. ff_vp56_free_context(s->alpha_context);
  606. vp6_decode_free_context(s->alpha_context);
  607. av_freep(&s->alpha_context);
  608. }
  609. return 0;
  610. }
  611. static av_cold void vp6_decode_free_context(VP56Context *s)
  612. {
  613. int pt, ct, cg;
  614. for (pt=0; pt<2; pt++) {
  615. ff_free_vlc(&s->dccv_vlc[pt]);
  616. ff_free_vlc(&s->runv_vlc[pt]);
  617. for (ct=0; ct<3; ct++)
  618. for (cg=0; cg<6; cg++)
  619. ff_free_vlc(&s->ract_vlc[pt][ct][cg]);
  620. }
  621. }
  622. AVCodec ff_vp6_decoder = {
  623. .name = "vp6",
  624. .long_name = NULL_IF_CONFIG_SMALL("On2 VP6"),
  625. .type = AVMEDIA_TYPE_VIDEO,
  626. .id = AV_CODEC_ID_VP6,
  627. .priv_data_size = sizeof(VP56Context),
  628. .init = vp6_decode_init,
  629. .close = vp6_decode_free,
  630. .decode = ff_vp56_decode_frame,
  631. .capabilities = AV_CODEC_CAP_DR1,
  632. };
  633. /* flash version, not flipped upside-down */
  634. AVCodec ff_vp6f_decoder = {
  635. .name = "vp6f",
  636. .long_name = NULL_IF_CONFIG_SMALL("On2 VP6 (Flash version)"),
  637. .type = AVMEDIA_TYPE_VIDEO,
  638. .id = AV_CODEC_ID_VP6F,
  639. .priv_data_size = sizeof(VP56Context),
  640. .init = vp6_decode_init,
  641. .close = vp6_decode_free,
  642. .decode = ff_vp56_decode_frame,
  643. .capabilities = AV_CODEC_CAP_DR1,
  644. };
  645. /* flash version, not flipped upside-down, with alpha channel */
  646. AVCodec ff_vp6a_decoder = {
  647. .name = "vp6a",
  648. .long_name = NULL_IF_CONFIG_SMALL("On2 VP6 (Flash version, with alpha channel)"),
  649. .type = AVMEDIA_TYPE_VIDEO,
  650. .id = AV_CODEC_ID_VP6A,
  651. .priv_data_size = sizeof(VP56Context),
  652. .init = vp6_decode_init,
  653. .close = vp6_decode_free,
  654. .decode = ff_vp56_decode_frame,
  655. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS,
  656. };