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.

549 lines
17KB

  1. /**
  2. * @file vp6.c
  3. * VP6 compatible video decoder
  4. *
  5. * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
  6. *
  7. * The VP6F decoder accepts an optional 1 byte extradata. It is composed of:
  8. * - upper 4bits: difference between encoded width and visible width
  9. * - lower 4bits: difference between encoded height and visible height
  10. *
  11. * This file is part of FFmpeg.
  12. *
  13. * FFmpeg is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU Lesser General Public
  15. * License as published by the Free Software Foundation; either
  16. * version 2.1 of the License, or (at your option) any later version.
  17. *
  18. * FFmpeg is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public
  24. * License along with FFmpeg; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27. #include <stdlib.h>
  28. #include "avcodec.h"
  29. #include "dsputil.h"
  30. #include "bitstream.h"
  31. #include "mpegvideo.h"
  32. #include "vp56.h"
  33. #include "vp56data.h"
  34. #include "vp6data.h"
  35. static int vp6_parse_header(vp56_context_t *s, uint8_t *buf, int buf_size,
  36. int *golden_frame)
  37. {
  38. vp56_range_coder_t *c = &s->c;
  39. int parse_filter_info = 0;
  40. int coeff_offset = 0;
  41. int vrt_shift = 0;
  42. int sub_version;
  43. int rows, cols;
  44. int res = 1;
  45. int separated_coeff = buf[0] & 1;
  46. s->framep[VP56_FRAME_CURRENT]->key_frame = !(buf[0] & 0x80);
  47. vp56_init_dequant(s, (buf[0] >> 1) & 0x3F);
  48. if (s->framep[VP56_FRAME_CURRENT]->key_frame) {
  49. sub_version = buf[1] >> 3;
  50. if (sub_version > 8)
  51. return 0;
  52. s->filter_header = buf[1] & 0x06;
  53. if (buf[1] & 1) {
  54. av_log(s->avctx, AV_LOG_ERROR, "interlacing not supported\n");
  55. return 0;
  56. }
  57. if (separated_coeff || !s->filter_header) {
  58. coeff_offset = AV_RB16(buf+2) - 2;
  59. buf += 2;
  60. buf_size -= 2;
  61. }
  62. rows = buf[2]; /* number of stored macroblock rows */
  63. cols = buf[3]; /* number of stored macroblock cols */
  64. /* buf[4] is number of displayed macroblock rows */
  65. /* buf[5] is number of displayed macroblock cols */
  66. if (16*cols != s->avctx->coded_width ||
  67. 16*rows != s->avctx->coded_height) {
  68. avcodec_set_dimensions(s->avctx, 16*cols, 16*rows);
  69. if (s->avctx->extradata_size == 1) {
  70. s->avctx->width -= s->avctx->extradata[0] >> 4;
  71. s->avctx->height -= s->avctx->extradata[0] & 0x0F;
  72. }
  73. res = 2;
  74. }
  75. vp56_init_range_decoder(c, buf+6, buf_size-6);
  76. vp56_rac_gets(c, 2);
  77. parse_filter_info = s->filter_header;
  78. if (sub_version < 8)
  79. vrt_shift = 5;
  80. s->sub_version = sub_version;
  81. } else {
  82. if (!s->sub_version)
  83. return 0;
  84. if (separated_coeff || !s->filter_header) {
  85. coeff_offset = AV_RB16(buf+1) - 2;
  86. buf += 2;
  87. buf_size -= 2;
  88. }
  89. vp56_init_range_decoder(c, buf+1, buf_size-1);
  90. *golden_frame = vp56_rac_get(c);
  91. if (s->filter_header) {
  92. s->deblock_filtering = vp56_rac_get(c);
  93. if (s->deblock_filtering)
  94. vp56_rac_get(c);
  95. if (s->sub_version > 7)
  96. parse_filter_info = vp56_rac_get(c);
  97. }
  98. }
  99. if (parse_filter_info) {
  100. if (vp56_rac_get(c)) {
  101. s->filter_mode = 2;
  102. s->sample_variance_threshold = vp56_rac_gets(c, 5) << vrt_shift;
  103. s->max_vector_length = 2 << vp56_rac_gets(c, 3);
  104. } else if (vp56_rac_get(c)) {
  105. s->filter_mode = 1;
  106. } else {
  107. s->filter_mode = 0;
  108. }
  109. if (s->sub_version > 7)
  110. s->filter_selection = vp56_rac_gets(c, 4);
  111. else
  112. s->filter_selection = 16;
  113. }
  114. if (vp56_rac_get(c))
  115. av_log(s->avctx, AV_LOG_WARNING,
  116. "alternative entropy decoding not supported\n");
  117. if (coeff_offset) {
  118. vp56_init_range_decoder(&s->cc, buf+coeff_offset,
  119. buf_size-coeff_offset);
  120. s->ccp = &s->cc;
  121. } else {
  122. s->ccp = &s->c;
  123. }
  124. return res;
  125. }
  126. static void vp6_coeff_order_table_init(vp56_context_t *s)
  127. {
  128. int i, pos, idx = 1;
  129. s->modelp->coeff_index_to_pos[0] = 0;
  130. for (i=0; i<16; i++)
  131. for (pos=1; pos<64; pos++)
  132. if (s->modelp->coeff_reorder[pos] == i)
  133. s->modelp->coeff_index_to_pos[idx++] = pos;
  134. }
  135. static void vp6_default_models_init(vp56_context_t *s)
  136. {
  137. vp56_model_t *model = s->modelp;
  138. model->vector_dct[0] = 0xA2;
  139. model->vector_dct[1] = 0xA4;
  140. model->vector_sig[0] = 0x80;
  141. model->vector_sig[1] = 0x80;
  142. memcpy(model->mb_types_stats, vp56_def_mb_types_stats, sizeof(model->mb_types_stats));
  143. memcpy(model->vector_fdv, vp6_def_fdv_vector_model, sizeof(model->vector_fdv));
  144. memcpy(model->vector_pdv, vp6_def_pdv_vector_model, sizeof(model->vector_pdv));
  145. memcpy(model->coeff_runv, vp6_def_runv_coeff_model, sizeof(model->coeff_runv));
  146. memcpy(model->coeff_reorder, vp6_def_coeff_reorder, sizeof(model->coeff_reorder));
  147. vp6_coeff_order_table_init(s);
  148. }
  149. static void vp6_parse_vector_models(vp56_context_t *s)
  150. {
  151. vp56_range_coder_t *c = &s->c;
  152. vp56_model_t *model = s->modelp;
  153. int comp, node;
  154. for (comp=0; comp<2; comp++) {
  155. if (vp56_rac_get_prob(c, vp6_sig_dct_pct[comp][0]))
  156. model->vector_dct[comp] = vp56_rac_gets_nn(c, 7);
  157. if (vp56_rac_get_prob(c, vp6_sig_dct_pct[comp][1]))
  158. model->vector_sig[comp] = vp56_rac_gets_nn(c, 7);
  159. }
  160. for (comp=0; comp<2; comp++)
  161. for (node=0; node<7; node++)
  162. if (vp56_rac_get_prob(c, vp6_pdv_pct[comp][node]))
  163. model->vector_pdv[comp][node] = vp56_rac_gets_nn(c, 7);
  164. for (comp=0; comp<2; comp++)
  165. for (node=0; node<8; node++)
  166. if (vp56_rac_get_prob(c, vp6_fdv_pct[comp][node]))
  167. model->vector_fdv[comp][node] = vp56_rac_gets_nn(c, 7);
  168. }
  169. static void vp6_parse_coeff_models(vp56_context_t *s)
  170. {
  171. vp56_range_coder_t *c = &s->c;
  172. vp56_model_t *model = s->modelp;
  173. int def_prob[11];
  174. int node, cg, ctx, pos;
  175. int ct; /* code type */
  176. int pt; /* plane type (0 for Y, 1 for U or V) */
  177. memset(def_prob, 0x80, sizeof(def_prob));
  178. for (pt=0; pt<2; pt++)
  179. for (node=0; node<11; node++)
  180. if (vp56_rac_get_prob(c, vp6_dccv_pct[pt][node])) {
  181. def_prob[node] = vp56_rac_gets_nn(c, 7);
  182. model->coeff_dccv[pt][node] = def_prob[node];
  183. } else if (s->framep[VP56_FRAME_CURRENT]->key_frame) {
  184. model->coeff_dccv[pt][node] = def_prob[node];
  185. }
  186. if (vp56_rac_get(c)) {
  187. for (pos=1; pos<64; pos++)
  188. if (vp56_rac_get_prob(c, vp6_coeff_reorder_pct[pos]))
  189. model->coeff_reorder[pos] = vp56_rac_gets(c, 4);
  190. vp6_coeff_order_table_init(s);
  191. }
  192. for (cg=0; cg<2; cg++)
  193. for (node=0; node<14; node++)
  194. if (vp56_rac_get_prob(c, vp6_runv_pct[cg][node]))
  195. model->coeff_runv[cg][node] = vp56_rac_gets_nn(c, 7);
  196. for (ct=0; ct<3; ct++)
  197. for (pt=0; pt<2; pt++)
  198. for (cg=0; cg<6; cg++)
  199. for (node=0; node<11; node++)
  200. if (vp56_rac_get_prob(c, vp6_ract_pct[ct][pt][cg][node])) {
  201. def_prob[node] = vp56_rac_gets_nn(c, 7);
  202. model->coeff_ract[pt][ct][cg][node] = def_prob[node];
  203. } else if (s->framep[VP56_FRAME_CURRENT]->key_frame) {
  204. model->coeff_ract[pt][ct][cg][node] = def_prob[node];
  205. }
  206. /* coeff_dcct is a linear combination of coeff_dccv */
  207. for (pt=0; pt<2; pt++)
  208. for (ctx=0; ctx<3; ctx++)
  209. for (node=0; node<5; node++)
  210. 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);
  211. }
  212. static void vp6_parse_vector_adjustment(vp56_context_t *s, vp56_mv_t *vect)
  213. {
  214. vp56_range_coder_t *c = &s->c;
  215. vp56_model_t *model = s->modelp;
  216. int comp;
  217. *vect = (vp56_mv_t) {0,0};
  218. if (s->vector_candidate_pos < 2)
  219. *vect = s->vector_candidate[0];
  220. for (comp=0; comp<2; comp++) {
  221. int i, delta = 0;
  222. if (vp56_rac_get_prob(c, model->vector_dct[comp])) {
  223. static const uint8_t prob_order[] = {0, 1, 2, 7, 6, 5, 4};
  224. for (i=0; i<sizeof(prob_order); i++) {
  225. int j = prob_order[i];
  226. delta |= vp56_rac_get_prob(c, model->vector_fdv[comp][j])<<j;
  227. }
  228. if (delta & 0xF0)
  229. delta |= vp56_rac_get_prob(c, model->vector_fdv[comp][3])<<3;
  230. else
  231. delta |= 8;
  232. } else {
  233. delta = vp56_rac_get_tree(c, vp56_pva_tree,
  234. model->vector_pdv[comp]);
  235. }
  236. if (delta && vp56_rac_get_prob(c, model->vector_sig[comp]))
  237. delta = -delta;
  238. if (!comp)
  239. vect->x += delta;
  240. else
  241. vect->y += delta;
  242. }
  243. }
  244. static void vp6_parse_coeff(vp56_context_t *s)
  245. {
  246. vp56_range_coder_t *c = s->ccp;
  247. vp56_model_t *model = s->modelp;
  248. uint8_t *permute = s->scantable.permutated;
  249. uint8_t *model1, *model2, *model3;
  250. int coeff, sign, coeff_idx;
  251. int b, i, cg, idx, ctx;
  252. int pt = 0; /* plane type (0 for Y, 1 for U or V) */
  253. for (b=0; b<6; b++) {
  254. int ct = 1; /* code type */
  255. int run = 1;
  256. if (b > 3) pt = 1;
  257. ctx = s->left_block[vp56_b6to4[b]].not_null_dc
  258. + s->above_blocks[s->above_block_idx[b]].not_null_dc;
  259. model1 = model->coeff_dccv[pt];
  260. model2 = model->coeff_dcct[pt][ctx];
  261. for (coeff_idx=0; coeff_idx<64; ) {
  262. if ((coeff_idx>1 && ct==0) || vp56_rac_get_prob(c, model2[0])) {
  263. /* parse a coeff */
  264. if (vp56_rac_get_prob(c, model2[2])) {
  265. if (vp56_rac_get_prob(c, model2[3])) {
  266. idx = vp56_rac_get_tree(c, vp56_pc_tree, model1);
  267. coeff = vp56_coeff_bias[idx];
  268. for (i=vp56_coeff_bit_length[idx]; i>=0; i--)
  269. coeff += vp56_rac_get_prob(c, vp56_coeff_parse_table[idx][i]) << i;
  270. } else {
  271. if (vp56_rac_get_prob(c, model2[4]))
  272. coeff = 3 + vp56_rac_get_prob(c, model1[5]);
  273. else
  274. coeff = 2;
  275. }
  276. ct = 2;
  277. } else {
  278. ct = 1;
  279. coeff = 1;
  280. }
  281. sign = vp56_rac_get(c);
  282. coeff = (coeff ^ -sign) + sign;
  283. if (coeff_idx)
  284. coeff *= s->dequant_ac;
  285. idx = model->coeff_index_to_pos[coeff_idx];
  286. s->block_coeff[b][permute[idx]] = coeff;
  287. run = 1;
  288. } else {
  289. /* parse a run */
  290. ct = 0;
  291. if (coeff_idx > 0) {
  292. if (!vp56_rac_get_prob(c, model2[1]))
  293. break;
  294. model3 = model->coeff_runv[coeff_idx >= 6];
  295. run = vp56_rac_get_tree(c, vp6_pcr_tree, model3);
  296. if (!run)
  297. for (run=9, i=0; i<6; i++)
  298. run += vp56_rac_get_prob(c, model3[i+8]) << i;
  299. }
  300. }
  301. cg = vp6_coeff_groups[coeff_idx+=run];
  302. model1 = model2 = model->coeff_ract[pt][ct][cg];
  303. }
  304. s->left_block[vp56_b6to4[b]].not_null_dc =
  305. s->above_blocks[s->above_block_idx[b]].not_null_dc = !!s->block_coeff[b][0];
  306. }
  307. }
  308. static int vp6_adjust(int v, int t)
  309. {
  310. int V = v, s = v >> 31;
  311. V ^= s;
  312. V -= s;
  313. if (V-t-1 >= (unsigned)(t-1))
  314. return v;
  315. V = 2*t - V;
  316. V += s;
  317. V ^= s;
  318. return V;
  319. }
  320. static int vp6_block_variance(uint8_t *src, int stride)
  321. {
  322. int sum = 0, square_sum = 0;
  323. int y, x;
  324. for (y=0; y<8; y+=2) {
  325. for (x=0; x<8; x+=2) {
  326. sum += src[x];
  327. square_sum += src[x]*src[x];
  328. }
  329. src += 2*stride;
  330. }
  331. return (16*square_sum - sum*sum) >> 8;
  332. }
  333. static void vp6_filter_hv4(uint8_t *dst, uint8_t *src, int stride,
  334. int delta, const int16_t *weights)
  335. {
  336. int x, y;
  337. for (y=0; y<8; y++) {
  338. for (x=0; x<8; x++) {
  339. dst[x] = av_clip_uint8(( src[x-delta ] * weights[0]
  340. + src[x ] * weights[1]
  341. + src[x+delta ] * weights[2]
  342. + src[x+2*delta] * weights[3] + 64) >> 7);
  343. }
  344. src += stride;
  345. dst += stride;
  346. }
  347. }
  348. static void vp6_filter_diag2(vp56_context_t *s, uint8_t *dst, uint8_t *src,
  349. int stride, int h_weight, int v_weight)
  350. {
  351. uint8_t *tmp = s->edge_emu_buffer+16;
  352. s->dsp.put_h264_chroma_pixels_tab[0](tmp, src, stride, 9, h_weight, 0);
  353. s->dsp.put_h264_chroma_pixels_tab[0](dst, tmp, stride, 8, 0, v_weight);
  354. }
  355. static void vp6_filter_diag4(uint8_t *dst, uint8_t *src, int stride,
  356. const int16_t *h_weights,const int16_t *v_weights)
  357. {
  358. int x, y;
  359. int tmp[8*11];
  360. int *t = tmp;
  361. src -= stride;
  362. for (y=0; y<11; y++) {
  363. for (x=0; x<8; x++) {
  364. t[x] = av_clip_uint8(( src[x-1] * h_weights[0]
  365. + src[x ] * h_weights[1]
  366. + src[x+1] * h_weights[2]
  367. + src[x+2] * h_weights[3] + 64) >> 7);
  368. }
  369. src += stride;
  370. t += 8;
  371. }
  372. t = tmp + 8;
  373. for (y=0; y<8; y++) {
  374. for (x=0; x<8; x++) {
  375. dst[x] = av_clip_uint8(( t[x-8 ] * v_weights[0]
  376. + t[x ] * v_weights[1]
  377. + t[x+8 ] * v_weights[2]
  378. + t[x+16] * v_weights[3] + 64) >> 7);
  379. }
  380. dst += stride;
  381. t += 8;
  382. }
  383. }
  384. static void vp6_filter(vp56_context_t *s, uint8_t *dst, uint8_t *src,
  385. int offset1, int offset2, int stride,
  386. vp56_mv_t mv, int mask, int select, int luma)
  387. {
  388. int filter4 = 0;
  389. int x8 = mv.x & mask;
  390. int y8 = mv.y & mask;
  391. if (luma) {
  392. x8 *= 2;
  393. y8 *= 2;
  394. filter4 = s->filter_mode;
  395. if (filter4 == 2) {
  396. if (s->max_vector_length &&
  397. (FFABS(mv.x) > s->max_vector_length ||
  398. FFABS(mv.y) > s->max_vector_length)) {
  399. filter4 = 0;
  400. } else if (s->sample_variance_threshold
  401. && (vp6_block_variance(src+offset1, stride)
  402. < s->sample_variance_threshold)) {
  403. filter4 = 0;
  404. }
  405. }
  406. }
  407. if ((y8 && (offset2-offset1)*s->flip<0) || (!y8 && offset1 > offset2)) {
  408. offset1 = offset2;
  409. }
  410. if (filter4) {
  411. if (!y8) { /* left or right combine */
  412. vp6_filter_hv4(dst, src+offset1, stride, 1,
  413. vp6_block_copy_filter[select][x8]);
  414. } else if (!x8) { /* above or below combine */
  415. vp6_filter_hv4(dst, src+offset1, stride, stride,
  416. vp6_block_copy_filter[select][y8]);
  417. } else {
  418. vp6_filter_diag4(dst, src+offset1 + ((mv.x^mv.y)>>31), stride,
  419. vp6_block_copy_filter[select][x8],
  420. vp6_block_copy_filter[select][y8]);
  421. }
  422. } else {
  423. if (!x8 || !y8) {
  424. s->dsp.put_h264_chroma_pixels_tab[0](dst, src+offset1, stride, 8, x8, y8);
  425. } else {
  426. vp6_filter_diag2(s, dst, src+offset1 + ((mv.x^mv.y)>>31), stride, x8, y8);
  427. }
  428. }
  429. }
  430. static int vp6_decode_init(AVCodecContext *avctx)
  431. {
  432. vp56_context_t *s = avctx->priv_data;
  433. vp56_init(avctx, avctx->codec->id == CODEC_ID_VP6,
  434. avctx->codec->id == CODEC_ID_VP6A);
  435. s->vp56_coord_div = vp6_coord_div;
  436. s->parse_vector_adjustment = vp6_parse_vector_adjustment;
  437. s->adjust = vp6_adjust;
  438. s->filter = vp6_filter;
  439. s->parse_coeff = vp6_parse_coeff;
  440. s->default_models_init = vp6_default_models_init;
  441. s->parse_vector_models = vp6_parse_vector_models;
  442. s->parse_coeff_models = vp6_parse_coeff_models;
  443. s->parse_header = vp6_parse_header;
  444. return 0;
  445. }
  446. AVCodec vp6_decoder = {
  447. "vp6",
  448. CODEC_TYPE_VIDEO,
  449. CODEC_ID_VP6,
  450. sizeof(vp56_context_t),
  451. vp6_decode_init,
  452. NULL,
  453. vp56_free,
  454. vp56_decode_frame,
  455. CODEC_CAP_DR1,
  456. };
  457. /* flash version, not flipped upside-down */
  458. AVCodec vp6f_decoder = {
  459. "vp6f",
  460. CODEC_TYPE_VIDEO,
  461. CODEC_ID_VP6F,
  462. sizeof(vp56_context_t),
  463. vp6_decode_init,
  464. NULL,
  465. vp56_free,
  466. vp56_decode_frame,
  467. CODEC_CAP_DR1,
  468. };
  469. /* flash version, not flipped upside-down, with alpha channel */
  470. AVCodec vp6a_decoder = {
  471. "vp6a",
  472. CODEC_TYPE_VIDEO,
  473. CODEC_ID_VP6A,
  474. sizeof(vp56_context_t),
  475. vp6_decode_init,
  476. NULL,
  477. vp56_free,
  478. vp56_decode_frame,
  479. CODEC_CAP_DR1,
  480. };