vp6.c 25 KB

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