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.

528 lines
17KB

  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 (vp56_rac_get_prob(c, model2[2])) {
  259. if (vp56_rac_get_prob(c, model2[3])) {
  260. idx = vp56_rac_get_tree(c, vp56_pc_tree, model);
  261. coeff = vp56_coeff_bias[idx];
  262. for (i=vp56_coeff_bit_length[idx]; i>=0; i--)
  263. coeff += vp56_rac_get_prob(c, vp56_coeff_parse_table[idx][i]) << i;
  264. } else {
  265. if (vp56_rac_get_prob(c, model2[4]))
  266. coeff = 3 + vp56_rac_get_prob(c, model[5]);
  267. else
  268. coeff = 2;
  269. }
  270. ct = 2;
  271. } else {
  272. ct = 1;
  273. coeff = 1;
  274. }
  275. sign = vp56_rac_get(c);
  276. coeff = (coeff ^ -sign) + sign;
  277. if (coeff_idx)
  278. coeff *= s->dequant_ac;
  279. idx = s->coeff_index_to_pos[coeff_idx];
  280. s->block_coeff[b][permute[idx]] = coeff;
  281. run = 1;
  282. } else {
  283. /* parse a run */
  284. ct = 0;
  285. if (coeff_idx > 0) {
  286. if (!vp56_rac_get_prob(c, model2[1]))
  287. break;
  288. model3 = s->coeff_model_runv[coeff_idx >= 6];
  289. run = vp56_rac_get_tree(c, vp6_pcr_tree, model3);
  290. if (!run)
  291. for (run=9, i=0; i<6; i++)
  292. run += vp56_rac_get_prob(c, model3[i+8]) << i;
  293. }
  294. }
  295. cg = vp6_coeff_groups[coeff_idx+=run];
  296. model = model2 = s->coeff_model_ract[pt][ct][cg];
  297. }
  298. s->left_block[vp56_b6to4[b]].not_null_dc =
  299. s->above_blocks[s->above_block_idx[b]].not_null_dc = !!s->block_coeff[b][0];
  300. }
  301. }
  302. static int vp6_adjust(int v, int t)
  303. {
  304. int V = v, s = v >> 31;
  305. V ^= s;
  306. V -= s;
  307. if (V-t-1 >= (unsigned)(t-1))
  308. return v;
  309. V = 2*t - V;
  310. V += s;
  311. V ^= s;
  312. return V;
  313. }
  314. static int vp6_block_variance(uint8_t *src, int stride)
  315. {
  316. int sum = 0, square_sum = 0;
  317. int y, x;
  318. for (y=0; y<8; y+=2) {
  319. for (x=0; x<8; x+=2) {
  320. sum += src[x];
  321. square_sum += src[x]*src[x];
  322. }
  323. src += 2*stride;
  324. }
  325. return (16*square_sum - sum*sum) >> 8;
  326. }
  327. static void vp6_filter_hv4(uint8_t *dst, uint8_t *src, int stride,
  328. int delta, const int16_t *weights)
  329. {
  330. int x, y;
  331. for (y=0; y<8; y++) {
  332. for (x=0; x<8; x++) {
  333. dst[x] = av_clip_uint8(( src[x-delta ] * weights[0]
  334. + src[x ] * weights[1]
  335. + src[x+delta ] * weights[2]
  336. + src[x+2*delta] * weights[3] + 64) >> 7);
  337. }
  338. src += stride;
  339. dst += stride;
  340. }
  341. }
  342. static void vp6_filter_diag2(vp56_context_t *s, uint8_t *dst, uint8_t *src,
  343. int stride, int h_weight, int v_weight)
  344. {
  345. uint8_t *tmp = s->edge_emu_buffer+16;
  346. s->dsp.put_h264_chroma_pixels_tab[0](tmp, src, stride, 9, h_weight, 0);
  347. s->dsp.put_h264_chroma_pixels_tab[0](dst, tmp, stride, 8, 0, v_weight);
  348. }
  349. static void vp6_filter_diag4(uint8_t *dst, uint8_t *src, int stride,
  350. const int16_t *h_weights,const int16_t *v_weights)
  351. {
  352. int x, y;
  353. int tmp[8*11];
  354. int *t = tmp;
  355. src -= stride;
  356. for (y=0; y<11; y++) {
  357. for (x=0; x<8; x++) {
  358. t[x] = av_clip_uint8(( src[x-1] * h_weights[0]
  359. + src[x ] * h_weights[1]
  360. + src[x+1] * h_weights[2]
  361. + src[x+2] * h_weights[3] + 64) >> 7);
  362. }
  363. src += stride;
  364. t += 8;
  365. }
  366. t = tmp + 8;
  367. for (y=0; y<8; y++) {
  368. for (x=0; x<8; x++) {
  369. dst[x] = av_clip_uint8(( t[x-8 ] * v_weights[0]
  370. + t[x ] * v_weights[1]
  371. + t[x+8 ] * v_weights[2]
  372. + t[x+16] * v_weights[3] + 64) >> 7);
  373. }
  374. dst += stride;
  375. t += 8;
  376. }
  377. }
  378. static void vp6_filter(vp56_context_t *s, uint8_t *dst, uint8_t *src,
  379. int offset1, int offset2, int stride,
  380. vp56_mv_t mv, int mask, int select, int luma)
  381. {
  382. int filter4 = 0;
  383. int x8 = mv.x & mask;
  384. int y8 = mv.y & mask;
  385. if (luma) {
  386. x8 *= 2;
  387. y8 *= 2;
  388. filter4 = s->filter_mode;
  389. if (filter4 == 2) {
  390. if (s->max_vector_length &&
  391. (FFABS(mv.x) > s->max_vector_length ||
  392. FFABS(mv.y) > s->max_vector_length)) {
  393. filter4 = 0;
  394. } else if (s->sample_variance_threshold
  395. && (vp6_block_variance(src+offset1, stride)
  396. < s->sample_variance_threshold)) {
  397. filter4 = 0;
  398. }
  399. }
  400. }
  401. if ((y8 && (offset2-offset1)*s->flip<0) || (!y8 && offset1 > offset2)) {
  402. offset1 = offset2;
  403. }
  404. if (filter4) {
  405. if (!y8) { /* left or right combine */
  406. vp6_filter_hv4(dst, src+offset1, stride, 1,
  407. vp6_block_copy_filter[select][x8]);
  408. } else if (!x8) { /* above or below combine */
  409. vp6_filter_hv4(dst, src+offset1, stride, stride,
  410. vp6_block_copy_filter[select][y8]);
  411. } else {
  412. vp6_filter_diag4(dst, src+offset1 + ((mv.x^mv.y)>>31), stride,
  413. vp6_block_copy_filter[select][x8],
  414. vp6_block_copy_filter[select][y8]);
  415. }
  416. } else {
  417. if (!x8 || !y8) {
  418. s->dsp.put_h264_chroma_pixels_tab[0](dst, src+offset1, stride, 8, x8, y8);
  419. } else {
  420. vp6_filter_diag2(s, dst, src+offset1 + ((mv.x^mv.y)>>31), stride, x8, y8);
  421. }
  422. }
  423. }
  424. static int vp6_decode_init(AVCodecContext *avctx)
  425. {
  426. vp56_context_t *s = avctx->priv_data;
  427. vp56_init(s, avctx, avctx->codec->id == CODEC_ID_VP6);
  428. s->vp56_coord_div = vp6_coord_div;
  429. s->parse_vector_adjustment = vp6_parse_vector_adjustment;
  430. s->adjust = vp6_adjust;
  431. s->filter = vp6_filter;
  432. s->parse_coeff = vp6_parse_coeff;
  433. s->default_models_init = vp6_default_models_init;
  434. s->parse_vector_models = vp6_parse_vector_models;
  435. s->parse_coeff_models = vp6_parse_coeff_models;
  436. s->parse_header = vp6_parse_header;
  437. return 0;
  438. }
  439. AVCodec vp6_decoder = {
  440. "vp6",
  441. CODEC_TYPE_VIDEO,
  442. CODEC_ID_VP6,
  443. sizeof(vp56_context_t),
  444. vp6_decode_init,
  445. NULL,
  446. vp56_free,
  447. vp56_decode_frame,
  448. CODEC_CAP_DR1,
  449. };
  450. /* flash version, not flipped upside-down */
  451. AVCodec vp6f_decoder = {
  452. "vp6f",
  453. CODEC_TYPE_VIDEO,
  454. CODEC_ID_VP6F,
  455. sizeof(vp56_context_t),
  456. vp6_decode_init,
  457. NULL,
  458. vp56_free,
  459. vp56_decode_frame,
  460. CODEC_CAP_DR1,
  461. };