vp6.c 18 KB

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