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.

527 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. vp56_rac_get(c);
  115. if (coeff_offset) {
  116. vp56_init_range_decoder(&s->cc, buf+coeff_offset,
  117. buf_size-coeff_offset);
  118. s->ccp = &s->cc;
  119. } else {
  120. s->ccp = &s->c;
  121. }
  122. return res;
  123. }
  124. static void vp6_coeff_order_table_init(vp56_context_t *s)
  125. {
  126. int i, pos, idx = 1;
  127. s->coeff_index_to_pos[0] = 0;
  128. for (i=0; i<16; i++)
  129. for (pos=1; pos<64; pos++)
  130. if (s->coeff_reorder[pos] == i)
  131. s->coeff_index_to_pos[idx++] = pos;
  132. }
  133. static void vp6_default_models_init(vp56_context_t *s)
  134. {
  135. s->vector_model_dct[0] = 0xA2;
  136. s->vector_model_dct[1] = 0xA4;
  137. s->vector_model_sig[0] = 0x80;
  138. s->vector_model_sig[1] = 0x80;
  139. memcpy(s->mb_types_stats, vp56_def_mb_types_stats, sizeof(s->mb_types_stats));
  140. memcpy(s->vector_model_fdv, vp6_def_fdv_vector_model, sizeof(s->vector_model_fdv));
  141. memcpy(s->vector_model_pdv, vp6_def_pdv_vector_model, sizeof(s->vector_model_pdv));
  142. memcpy(s->coeff_model_runv, vp6_def_runv_coeff_model, sizeof(s->coeff_model_runv));
  143. memcpy(s->coeff_reorder, vp6_def_coeff_reorder, sizeof(s->coeff_reorder));
  144. vp6_coeff_order_table_init(s);
  145. }
  146. static void vp6_parse_vector_models(vp56_context_t *s)
  147. {
  148. vp56_range_coder_t *c = &s->c;
  149. int comp, node;
  150. for (comp=0; comp<2; comp++) {
  151. if (vp56_rac_get_prob(c, vp6_sig_dct_pct[comp][0]))
  152. s->vector_model_dct[comp] = vp56_rac_gets_nn(c, 7);
  153. if (vp56_rac_get_prob(c, vp6_sig_dct_pct[comp][1]))
  154. s->vector_model_sig[comp] = vp56_rac_gets_nn(c, 7);
  155. }
  156. for (comp=0; comp<2; comp++)
  157. for (node=0; node<7; node++)
  158. if (vp56_rac_get_prob(c, vp6_pdv_pct[comp][node]))
  159. s->vector_model_pdv[comp][node] = vp56_rac_gets_nn(c, 7);
  160. for (comp=0; comp<2; comp++)
  161. for (node=0; node<8; node++)
  162. if (vp56_rac_get_prob(c, vp6_fdv_pct[comp][node]))
  163. s->vector_model_fdv[comp][node] = vp56_rac_gets_nn(c, 7);
  164. }
  165. static void vp6_parse_coeff_models(vp56_context_t *s)
  166. {
  167. vp56_range_coder_t *c = &s->c;
  168. int def_prob[11];
  169. int node, cg, ctx, pos;
  170. int ct; /* code type */
  171. int pt; /* plane type (0 for Y, 1 for U or V) */
  172. memset(def_prob, 0x80, sizeof(def_prob));
  173. for (pt=0; pt<2; pt++)
  174. for (node=0; node<11; node++)
  175. if (vp56_rac_get_prob(c, vp6_dccv_pct[pt][node])) {
  176. def_prob[node] = vp56_rac_gets_nn(c, 7);
  177. s->coeff_model_dccv[pt][node] = def_prob[node];
  178. } else if (s->framep[VP56_FRAME_CURRENT]->key_frame) {
  179. s->coeff_model_dccv[pt][node] = def_prob[node];
  180. }
  181. if (vp56_rac_get(c)) {
  182. for (pos=1; pos<64; pos++)
  183. if (vp56_rac_get_prob(c, vp6_coeff_reorder_pct[pos]))
  184. s->coeff_reorder[pos] = vp56_rac_gets(c, 4);
  185. vp6_coeff_order_table_init(s);
  186. }
  187. for (cg=0; cg<2; cg++)
  188. for (node=0; node<14; node++)
  189. if (vp56_rac_get_prob(c, vp6_runv_pct[cg][node]))
  190. s->coeff_model_runv[cg][node] = vp56_rac_gets_nn(c, 7);
  191. for (ct=0; ct<3; ct++)
  192. for (pt=0; pt<2; pt++)
  193. for (cg=0; cg<6; cg++)
  194. for (node=0; node<11; node++)
  195. if (vp56_rac_get_prob(c, vp6_ract_pct[ct][pt][cg][node])) {
  196. def_prob[node] = vp56_rac_gets_nn(c, 7);
  197. s->coeff_model_ract[pt][ct][cg][node] = def_prob[node];
  198. } else if (s->framep[VP56_FRAME_CURRENT]->key_frame) {
  199. s->coeff_model_ract[pt][ct][cg][node] = def_prob[node];
  200. }
  201. /* coeff_model_dcct is a linear combination of coeff_model_dccv */
  202. for (pt=0; pt<2; pt++)
  203. for (ctx=0; ctx<3; ctx++)
  204. for (node=0; node<5; node++)
  205. 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);
  206. }
  207. static void vp6_parse_vector_adjustment(vp56_context_t *s, vp56_mv_t *vect)
  208. {
  209. vp56_range_coder_t *c = &s->c;
  210. int comp;
  211. *vect = (vp56_mv_t) {0,0};
  212. if (s->vector_candidate_pos < 2)
  213. *vect = s->vector_candidate[0];
  214. for (comp=0; comp<2; comp++) {
  215. int i, delta = 0;
  216. if (vp56_rac_get_prob(c, s->vector_model_dct[comp])) {
  217. static const uint8_t prob_order[] = {0, 1, 2, 7, 6, 5, 4};
  218. for (i=0; i<sizeof(prob_order); i++) {
  219. int j = prob_order[i];
  220. delta |= vp56_rac_get_prob(c, s->vector_model_fdv[comp][j])<<j;
  221. }
  222. if (delta & 0xF0)
  223. delta |= vp56_rac_get_prob(c, s->vector_model_fdv[comp][3])<<3;
  224. else
  225. delta |= 8;
  226. } else {
  227. delta = vp56_rac_get_tree(c, vp56_pva_tree,
  228. s->vector_model_pdv[comp]);
  229. }
  230. if (delta && vp56_rac_get_prob(c, s->vector_model_sig[comp]))
  231. delta = -delta;
  232. if (!comp)
  233. vect->x += delta;
  234. else
  235. vect->y += delta;
  236. }
  237. }
  238. static void vp6_parse_coeff(vp56_context_t *s)
  239. {
  240. vp56_range_coder_t *c = s->ccp;
  241. uint8_t *permute = s->scantable.permutated;
  242. uint8_t *model, *model2, *model3;
  243. int coeff, sign, coeff_idx;
  244. int b, i, cg, idx, ctx;
  245. int pt = 0; /* plane type (0 for Y, 1 for U or V) */
  246. for (b=0; b<6; b++) {
  247. int ct = 1; /* code type */
  248. int run = 1;
  249. if (b > 3) pt = 1;
  250. ctx = s->left_block[vp56_b6to4[b]].not_null_dc
  251. + s->above_blocks[s->above_block_idx[b]].not_null_dc;
  252. model = s->coeff_model_dccv[pt];
  253. model2 = s->coeff_model_dcct[pt][ctx];
  254. for (coeff_idx=0; coeff_idx<64; ) {
  255. if ((coeff_idx>1 && ct==0) || vp56_rac_get_prob(c, model2[0])) {
  256. /* parse a coeff */
  257. if (vp56_rac_get_prob(c, model2[2])) {
  258. if (vp56_rac_get_prob(c, model2[3])) {
  259. idx = vp56_rac_get_tree(c, vp56_pc_tree, model);
  260. coeff = vp56_coeff_bias[idx];
  261. for (i=vp56_coeff_bit_length[idx]; i>=0; i--)
  262. coeff += vp56_rac_get_prob(c, vp56_coeff_parse_table[idx][i]) << i;
  263. } else {
  264. if (vp56_rac_get_prob(c, model2[4]))
  265. coeff = 3 + vp56_rac_get_prob(c, model[5]);
  266. else
  267. coeff = 2;
  268. }
  269. ct = 2;
  270. } else {
  271. ct = 1;
  272. coeff = 1;
  273. }
  274. sign = vp56_rac_get(c);
  275. coeff = (coeff ^ -sign) + sign;
  276. if (coeff_idx)
  277. coeff *= s->dequant_ac;
  278. idx = s->coeff_index_to_pos[coeff_idx];
  279. s->block_coeff[b][permute[idx]] = coeff;
  280. run = 1;
  281. } else {
  282. /* parse a run */
  283. ct = 0;
  284. if (coeff_idx > 0) {
  285. if (!vp56_rac_get_prob(c, model2[1]))
  286. break;
  287. model3 = s->coeff_model_runv[coeff_idx >= 6];
  288. run = vp56_rac_get_tree(c, vp6_pcr_tree, model3);
  289. if (!run)
  290. for (run=9, i=0; i<6; i++)
  291. run += vp56_rac_get_prob(c, model3[i+8]) << i;
  292. }
  293. }
  294. cg = vp6_coeff_groups[coeff_idx+=run];
  295. model = model2 = s->coeff_model_ract[pt][ct][cg];
  296. }
  297. s->left_block[vp56_b6to4[b]].not_null_dc =
  298. s->above_blocks[s->above_block_idx[b]].not_null_dc = !!s->block_coeff[b][0];
  299. }
  300. }
  301. static int vp6_adjust(int v, int t)
  302. {
  303. int V = v, s = v >> 31;
  304. V ^= s;
  305. V -= s;
  306. if (V-t-1 >= (unsigned)(t-1))
  307. return v;
  308. V = 2*t - V;
  309. V += s;
  310. V ^= s;
  311. return V;
  312. }
  313. static int vp6_block_variance(uint8_t *src, int stride)
  314. {
  315. int sum = 0, square_sum = 0;
  316. int y, x;
  317. for (y=0; y<8; y+=2) {
  318. for (x=0; x<8; x+=2) {
  319. sum += src[x];
  320. square_sum += src[x]*src[x];
  321. }
  322. src += 2*stride;
  323. }
  324. return (16*square_sum - sum*sum) >> 8;
  325. }
  326. static void vp6_filter_hv4(uint8_t *dst, uint8_t *src, int stride,
  327. int delta, const int16_t *weights)
  328. {
  329. int x, y;
  330. for (y=0; y<8; y++) {
  331. for (x=0; x<8; x++) {
  332. dst[x] = av_clip_uint8(( src[x-delta ] * weights[0]
  333. + src[x ] * weights[1]
  334. + src[x+delta ] * weights[2]
  335. + src[x+2*delta] * weights[3] + 64) >> 7);
  336. }
  337. src += stride;
  338. dst += stride;
  339. }
  340. }
  341. static void vp6_filter_diag2(vp56_context_t *s, uint8_t *dst, uint8_t *src,
  342. int stride, int h_weight, int v_weight)
  343. {
  344. uint8_t *tmp = s->edge_emu_buffer+16;
  345. s->dsp.put_h264_chroma_pixels_tab[0](tmp, src, stride, 9, h_weight, 0);
  346. s->dsp.put_h264_chroma_pixels_tab[0](dst, tmp, stride, 8, 0, v_weight);
  347. }
  348. static void vp6_filter_diag4(uint8_t *dst, uint8_t *src, int stride,
  349. const int16_t *h_weights,const int16_t *v_weights)
  350. {
  351. int x, y;
  352. int tmp[8*11];
  353. int *t = tmp;
  354. src -= stride;
  355. for (y=0; y<11; y++) {
  356. for (x=0; x<8; x++) {
  357. t[x] = av_clip_uint8(( src[x-1] * h_weights[0]
  358. + src[x ] * h_weights[1]
  359. + src[x+1] * h_weights[2]
  360. + src[x+2] * h_weights[3] + 64) >> 7);
  361. }
  362. src += stride;
  363. t += 8;
  364. }
  365. t = tmp + 8;
  366. for (y=0; y<8; y++) {
  367. for (x=0; x<8; x++) {
  368. dst[x] = av_clip_uint8(( t[x-8 ] * v_weights[0]
  369. + t[x ] * v_weights[1]
  370. + t[x+8 ] * v_weights[2]
  371. + t[x+16] * v_weights[3] + 64) >> 7);
  372. }
  373. dst += stride;
  374. t += 8;
  375. }
  376. }
  377. static void vp6_filter(vp56_context_t *s, uint8_t *dst, uint8_t *src,
  378. int offset1, int offset2, int stride,
  379. vp56_mv_t mv, int mask, int select, int luma)
  380. {
  381. int filter4 = 0;
  382. int x8 = mv.x & mask;
  383. int y8 = mv.y & mask;
  384. if (luma) {
  385. x8 *= 2;
  386. y8 *= 2;
  387. filter4 = s->filter_mode;
  388. if (filter4 == 2) {
  389. if (s->max_vector_length &&
  390. (FFABS(mv.x) > s->max_vector_length ||
  391. FFABS(mv.y) > s->max_vector_length)) {
  392. filter4 = 0;
  393. } else if (s->sample_variance_threshold
  394. && (vp6_block_variance(src+offset1, stride)
  395. < s->sample_variance_threshold)) {
  396. filter4 = 0;
  397. }
  398. }
  399. }
  400. if ((y8 && (offset2-offset1)*s->flip<0) || (!y8 && offset1 > offset2)) {
  401. offset1 = offset2;
  402. }
  403. if (filter4) {
  404. if (!y8) { /* left or right combine */
  405. vp6_filter_hv4(dst, src+offset1, stride, 1,
  406. vp6_block_copy_filter[select][x8]);
  407. } else if (!x8) { /* above or below combine */
  408. vp6_filter_hv4(dst, src+offset1, stride, stride,
  409. vp6_block_copy_filter[select][y8]);
  410. } else {
  411. vp6_filter_diag4(dst, src+offset1 + ((mv.x^mv.y)>>31), stride,
  412. vp6_block_copy_filter[select][x8],
  413. vp6_block_copy_filter[select][y8]);
  414. }
  415. } else {
  416. if (!x8 || !y8) {
  417. s->dsp.put_h264_chroma_pixels_tab[0](dst, src+offset1, stride, 8, x8, y8);
  418. } else {
  419. vp6_filter_diag2(s, dst, src+offset1 + ((mv.x^mv.y)>>31), stride, x8, y8);
  420. }
  421. }
  422. }
  423. static int vp6_decode_init(AVCodecContext *avctx)
  424. {
  425. vp56_context_t *s = avctx->priv_data;
  426. vp56_init(s, avctx, avctx->codec->id == CODEC_ID_VP6);
  427. s->vp56_coord_div = vp6_coord_div;
  428. s->parse_vector_adjustment = vp6_parse_vector_adjustment;
  429. s->adjust = vp6_adjust;
  430. s->filter = vp6_filter;
  431. s->parse_coeff = vp6_parse_coeff;
  432. s->default_models_init = vp6_default_models_init;
  433. s->parse_vector_models = vp6_parse_vector_models;
  434. s->parse_coeff_models = vp6_parse_coeff_models;
  435. s->parse_header = vp6_parse_header;
  436. return 0;
  437. }
  438. AVCodec vp6_decoder = {
  439. "vp6",
  440. CODEC_TYPE_VIDEO,
  441. CODEC_ID_VP6,
  442. sizeof(vp56_context_t),
  443. vp6_decode_init,
  444. NULL,
  445. vp56_free,
  446. vp56_decode_frame,
  447. CODEC_CAP_DR1,
  448. };
  449. /* flash version, not flipped upside-down */
  450. AVCodec vp6f_decoder = {
  451. "vp6f",
  452. CODEC_TYPE_VIDEO,
  453. CODEC_ID_VP6F,
  454. sizeof(vp56_context_t),
  455. vp6_decode_init,
  456. NULL,
  457. vp56_free,
  458. vp56_decode_frame,
  459. CODEC_CAP_DR1,
  460. };