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.

2406 lines
83KB

  1. /*
  2. * Copyright (C) 2003-2004 the ffmpeg project
  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. * On2 VP3 Video Decoder
  23. *
  24. * VP3 Video Decoder by Mike Melanson (mike at multimedia.cx)
  25. * For more information about the VP3 coding process, visit:
  26. * http://wiki.multimedia.cx/index.php?title=On2_VP3
  27. *
  28. * Theora decoder by Alex Beregszaszi
  29. */
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include "libavutil/imgutils.h"
  34. #include "avcodec.h"
  35. #include "internal.h"
  36. #include "dsputil.h"
  37. #include "get_bits.h"
  38. #include "vp3data.h"
  39. #include "vp3dsp.h"
  40. #include "xiph.h"
  41. #include "thread.h"
  42. #define FRAGMENT_PIXELS 8
  43. //FIXME split things out into their own arrays
  44. typedef struct Vp3Fragment {
  45. int16_t dc;
  46. uint8_t coding_method;
  47. uint8_t qpi;
  48. } Vp3Fragment;
  49. #define SB_NOT_CODED 0
  50. #define SB_PARTIALLY_CODED 1
  51. #define SB_FULLY_CODED 2
  52. // This is the maximum length of a single long bit run that can be encoded
  53. // for superblock coding or block qps. Theora special-cases this to read a
  54. // bit instead of flipping the current bit to allow for runs longer than 4129.
  55. #define MAXIMUM_LONG_BIT_RUN 4129
  56. #define MODE_INTER_NO_MV 0
  57. #define MODE_INTRA 1
  58. #define MODE_INTER_PLUS_MV 2
  59. #define MODE_INTER_LAST_MV 3
  60. #define MODE_INTER_PRIOR_LAST 4
  61. #define MODE_USING_GOLDEN 5
  62. #define MODE_GOLDEN_MV 6
  63. #define MODE_INTER_FOURMV 7
  64. #define CODING_MODE_COUNT 8
  65. /* special internal mode */
  66. #define MODE_COPY 8
  67. /* There are 6 preset schemes, plus a free-form scheme */
  68. static const int ModeAlphabet[6][CODING_MODE_COUNT] =
  69. {
  70. /* scheme 1: Last motion vector dominates */
  71. { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
  72. MODE_INTER_PLUS_MV, MODE_INTER_NO_MV,
  73. MODE_INTRA, MODE_USING_GOLDEN,
  74. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  75. /* scheme 2 */
  76. { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
  77. MODE_INTER_NO_MV, MODE_INTER_PLUS_MV,
  78. MODE_INTRA, MODE_USING_GOLDEN,
  79. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  80. /* scheme 3 */
  81. { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
  82. MODE_INTER_PRIOR_LAST, MODE_INTER_NO_MV,
  83. MODE_INTRA, MODE_USING_GOLDEN,
  84. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  85. /* scheme 4 */
  86. { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
  87. MODE_INTER_NO_MV, MODE_INTER_PRIOR_LAST,
  88. MODE_INTRA, MODE_USING_GOLDEN,
  89. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  90. /* scheme 5: No motion vector dominates */
  91. { MODE_INTER_NO_MV, MODE_INTER_LAST_MV,
  92. MODE_INTER_PRIOR_LAST, MODE_INTER_PLUS_MV,
  93. MODE_INTRA, MODE_USING_GOLDEN,
  94. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  95. /* scheme 6 */
  96. { MODE_INTER_NO_MV, MODE_USING_GOLDEN,
  97. MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
  98. MODE_INTER_PLUS_MV, MODE_INTRA,
  99. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  100. };
  101. static const uint8_t hilbert_offset[16][2] = {
  102. {0,0}, {1,0}, {1,1}, {0,1},
  103. {0,2}, {0,3}, {1,3}, {1,2},
  104. {2,2}, {2,3}, {3,3}, {3,2},
  105. {3,1}, {2,1}, {2,0}, {3,0}
  106. };
  107. #define MIN_DEQUANT_VAL 2
  108. typedef struct Vp3DecodeContext {
  109. AVCodecContext *avctx;
  110. int theora, theora_tables;
  111. int version;
  112. int width, height;
  113. int chroma_x_shift, chroma_y_shift;
  114. AVFrame golden_frame;
  115. AVFrame last_frame;
  116. AVFrame current_frame;
  117. int keyframe;
  118. DSPContext dsp;
  119. VP3DSPContext vp3dsp;
  120. int flipped_image;
  121. int last_slice_end;
  122. int skip_loop_filter;
  123. int qps[3];
  124. int nqps;
  125. int last_qps[3];
  126. int superblock_count;
  127. int y_superblock_width;
  128. int y_superblock_height;
  129. int y_superblock_count;
  130. int c_superblock_width;
  131. int c_superblock_height;
  132. int c_superblock_count;
  133. int u_superblock_start;
  134. int v_superblock_start;
  135. unsigned char *superblock_coding;
  136. int macroblock_count;
  137. int macroblock_width;
  138. int macroblock_height;
  139. int fragment_count;
  140. int fragment_width[2];
  141. int fragment_height[2];
  142. Vp3Fragment *all_fragments;
  143. int fragment_start[3];
  144. int data_offset[3];
  145. int8_t (*motion_val[2])[2];
  146. ScanTable scantable;
  147. /* tables */
  148. uint16_t coded_dc_scale_factor[64];
  149. uint32_t coded_ac_scale_factor[64];
  150. uint8_t base_matrix[384][64];
  151. uint8_t qr_count[2][3];
  152. uint8_t qr_size [2][3][64];
  153. uint16_t qr_base[2][3][64];
  154. /**
  155. * This is a list of all tokens in bitstream order. Reordering takes place
  156. * by pulling from each level during IDCT. As a consequence, IDCT must be
  157. * in Hilbert order, making the minimum slice height 64 for 4:2:0 and 32
  158. * otherwise. The 32 different tokens with up to 12 bits of extradata are
  159. * collapsed into 3 types, packed as follows:
  160. * (from the low to high bits)
  161. *
  162. * 2 bits: type (0,1,2)
  163. * 0: EOB run, 14 bits for run length (12 needed)
  164. * 1: zero run, 7 bits for run length
  165. * 7 bits for the next coefficient (3 needed)
  166. * 2: coefficient, 14 bits (11 needed)
  167. *
  168. * Coefficients are signed, so are packed in the highest bits for automatic
  169. * sign extension.
  170. */
  171. int16_t *dct_tokens[3][64];
  172. int16_t *dct_tokens_base;
  173. #define TOKEN_EOB(eob_run) ((eob_run) << 2)
  174. #define TOKEN_ZERO_RUN(coeff, zero_run) (((coeff) << 9) + ((zero_run) << 2) + 1)
  175. #define TOKEN_COEFF(coeff) (((coeff) << 2) + 2)
  176. /**
  177. * number of blocks that contain DCT coefficients at the given level or higher
  178. */
  179. int num_coded_frags[3][64];
  180. int total_num_coded_frags;
  181. /* this is a list of indexes into the all_fragments array indicating
  182. * which of the fragments are coded */
  183. int *coded_fragment_list[3];
  184. VLC dc_vlc[16];
  185. VLC ac_vlc_1[16];
  186. VLC ac_vlc_2[16];
  187. VLC ac_vlc_3[16];
  188. VLC ac_vlc_4[16];
  189. VLC superblock_run_length_vlc;
  190. VLC fragment_run_length_vlc;
  191. VLC mode_code_vlc;
  192. VLC motion_vector_vlc;
  193. /* these arrays need to be on 16-byte boundaries since SSE2 operations
  194. * index into them */
  195. DECLARE_ALIGNED(16, int16_t, qmat)[3][2][3][64]; ///< qmat[qpi][is_inter][plane]
  196. /* This table contains superblock_count * 16 entries. Each set of 16
  197. * numbers corresponds to the fragment indexes 0..15 of the superblock.
  198. * An entry will be -1 to indicate that no entry corresponds to that
  199. * index. */
  200. int *superblock_fragments;
  201. /* This is an array that indicates how a particular macroblock
  202. * is coded. */
  203. unsigned char *macroblock_coding;
  204. uint8_t *edge_emu_buffer;
  205. /* Huffman decode */
  206. int hti;
  207. unsigned int hbits;
  208. int entries;
  209. int huff_code_size;
  210. uint32_t huffman_table[80][32][2];
  211. uint8_t filter_limit_values[64];
  212. DECLARE_ALIGNED(8, int, bounding_values_array)[256+2];
  213. } Vp3DecodeContext;
  214. /************************************************************************
  215. * VP3 specific functions
  216. ************************************************************************/
  217. static void vp3_decode_flush(AVCodecContext *avctx)
  218. {
  219. Vp3DecodeContext *s = avctx->priv_data;
  220. if (s->golden_frame.data[0]) {
  221. if (s->golden_frame.data[0] == s->last_frame.data[0])
  222. memset(&s->last_frame, 0, sizeof(AVFrame));
  223. if (s->current_frame.data[0] == s->golden_frame.data[0])
  224. memset(&s->current_frame, 0, sizeof(AVFrame));
  225. ff_thread_release_buffer(avctx, &s->golden_frame);
  226. }
  227. if (s->last_frame.data[0]) {
  228. if (s->current_frame.data[0] == s->last_frame.data[0])
  229. memset(&s->current_frame, 0, sizeof(AVFrame));
  230. ff_thread_release_buffer(avctx, &s->last_frame);
  231. }
  232. if (s->current_frame.data[0])
  233. ff_thread_release_buffer(avctx, &s->current_frame);
  234. }
  235. static av_cold int vp3_decode_end(AVCodecContext *avctx)
  236. {
  237. Vp3DecodeContext *s = avctx->priv_data;
  238. int i;
  239. av_free(s->superblock_coding);
  240. av_free(s->all_fragments);
  241. av_free(s->coded_fragment_list[0]);
  242. av_free(s->dct_tokens_base);
  243. av_free(s->superblock_fragments);
  244. av_free(s->macroblock_coding);
  245. av_free(s->motion_val[0]);
  246. av_free(s->motion_val[1]);
  247. av_free(s->edge_emu_buffer);
  248. if (avctx->internal->is_copy)
  249. return 0;
  250. for (i = 0; i < 16; i++) {
  251. ff_free_vlc(&s->dc_vlc[i]);
  252. ff_free_vlc(&s->ac_vlc_1[i]);
  253. ff_free_vlc(&s->ac_vlc_2[i]);
  254. ff_free_vlc(&s->ac_vlc_3[i]);
  255. ff_free_vlc(&s->ac_vlc_4[i]);
  256. }
  257. ff_free_vlc(&s->superblock_run_length_vlc);
  258. ff_free_vlc(&s->fragment_run_length_vlc);
  259. ff_free_vlc(&s->mode_code_vlc);
  260. ff_free_vlc(&s->motion_vector_vlc);
  261. /* release all frames */
  262. vp3_decode_flush(avctx);
  263. return 0;
  264. }
  265. /*
  266. * This function sets up all of the various blocks mappings:
  267. * superblocks <-> fragments, macroblocks <-> fragments,
  268. * superblocks <-> macroblocks
  269. *
  270. * @return 0 is successful; returns 1 if *anything* went wrong.
  271. */
  272. static int init_block_mapping(Vp3DecodeContext *s)
  273. {
  274. int sb_x, sb_y, plane;
  275. int x, y, i, j = 0;
  276. for (plane = 0; plane < 3; plane++) {
  277. int sb_width = plane ? s->c_superblock_width : s->y_superblock_width;
  278. int sb_height = plane ? s->c_superblock_height : s->y_superblock_height;
  279. int frag_width = s->fragment_width[!!plane];
  280. int frag_height = s->fragment_height[!!plane];
  281. for (sb_y = 0; sb_y < sb_height; sb_y++)
  282. for (sb_x = 0; sb_x < sb_width; sb_x++)
  283. for (i = 0; i < 16; i++) {
  284. x = 4*sb_x + hilbert_offset[i][0];
  285. y = 4*sb_y + hilbert_offset[i][1];
  286. if (x < frag_width && y < frag_height)
  287. s->superblock_fragments[j++] = s->fragment_start[plane] + y*frag_width + x;
  288. else
  289. s->superblock_fragments[j++] = -1;
  290. }
  291. }
  292. return 0; /* successful path out */
  293. }
  294. /*
  295. * This function sets up the dequantization tables used for a particular
  296. * frame.
  297. */
  298. static void init_dequantizer(Vp3DecodeContext *s, int qpi)
  299. {
  300. int ac_scale_factor = s->coded_ac_scale_factor[s->qps[qpi]];
  301. int dc_scale_factor = s->coded_dc_scale_factor[s->qps[qpi]];
  302. int i, plane, inter, qri, bmi, bmj, qistart;
  303. for(inter=0; inter<2; inter++){
  304. for(plane=0; plane<3; plane++){
  305. int sum=0;
  306. for(qri=0; qri<s->qr_count[inter][plane]; qri++){
  307. sum+= s->qr_size[inter][plane][qri];
  308. if(s->qps[qpi] <= sum)
  309. break;
  310. }
  311. qistart= sum - s->qr_size[inter][plane][qri];
  312. bmi= s->qr_base[inter][plane][qri ];
  313. bmj= s->qr_base[inter][plane][qri+1];
  314. for(i=0; i<64; i++){
  315. int coeff= ( 2*(sum -s->qps[qpi])*s->base_matrix[bmi][i]
  316. - 2*(qistart-s->qps[qpi])*s->base_matrix[bmj][i]
  317. + s->qr_size[inter][plane][qri])
  318. / (2*s->qr_size[inter][plane][qri]);
  319. int qmin= 8<<(inter + !i);
  320. int qscale= i ? ac_scale_factor : dc_scale_factor;
  321. s->qmat[qpi][inter][plane][s->dsp.idct_permutation[i]]= av_clip((qscale * coeff)/100 * 4, qmin, 4096);
  322. }
  323. // all DC coefficients use the same quant so as not to interfere with DC prediction
  324. s->qmat[qpi][inter][plane][0] = s->qmat[0][inter][plane][0];
  325. }
  326. }
  327. }
  328. /*
  329. * This function initializes the loop filter boundary limits if the frame's
  330. * quality index is different from the previous frame's.
  331. *
  332. * The filter_limit_values may not be larger than 127.
  333. */
  334. static void init_loop_filter(Vp3DecodeContext *s)
  335. {
  336. int *bounding_values= s->bounding_values_array+127;
  337. int filter_limit;
  338. int x;
  339. int value;
  340. filter_limit = s->filter_limit_values[s->qps[0]];
  341. av_assert0(filter_limit < 128U);
  342. /* set up the bounding values */
  343. memset(s->bounding_values_array, 0, 256 * sizeof(int));
  344. for (x = 0; x < filter_limit; x++) {
  345. bounding_values[-x] = -x;
  346. bounding_values[x] = x;
  347. }
  348. for (x = value = filter_limit; x < 128 && value; x++, value--) {
  349. bounding_values[ x] = value;
  350. bounding_values[-x] = -value;
  351. }
  352. if (value)
  353. bounding_values[128] = value;
  354. bounding_values[129] = bounding_values[130] = filter_limit * 0x02020202;
  355. }
  356. /*
  357. * This function unpacks all of the superblock/macroblock/fragment coding
  358. * information from the bitstream.
  359. */
  360. static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
  361. {
  362. int superblock_starts[3] = { 0, s->u_superblock_start, s->v_superblock_start };
  363. int bit = 0;
  364. int current_superblock = 0;
  365. int current_run = 0;
  366. int num_partial_superblocks = 0;
  367. int i, j;
  368. int current_fragment;
  369. int plane;
  370. if (s->keyframe) {
  371. memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);
  372. } else {
  373. /* unpack the list of partially-coded superblocks */
  374. bit = get_bits1(gb) ^ 1;
  375. current_run = 0;
  376. while (current_superblock < s->superblock_count && get_bits_left(gb) > 0) {
  377. if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
  378. bit = get_bits1(gb);
  379. else
  380. bit ^= 1;
  381. current_run = get_vlc2(gb,
  382. s->superblock_run_length_vlc.table, 6, 2) + 1;
  383. if (current_run == 34)
  384. current_run += get_bits(gb, 12);
  385. if (current_superblock + current_run > s->superblock_count) {
  386. av_log(s->avctx, AV_LOG_ERROR, "Invalid partially coded superblock run length\n");
  387. return -1;
  388. }
  389. memset(s->superblock_coding + current_superblock, bit, current_run);
  390. current_superblock += current_run;
  391. if (bit)
  392. num_partial_superblocks += current_run;
  393. }
  394. /* unpack the list of fully coded superblocks if any of the blocks were
  395. * not marked as partially coded in the previous step */
  396. if (num_partial_superblocks < s->superblock_count) {
  397. int superblocks_decoded = 0;
  398. current_superblock = 0;
  399. bit = get_bits1(gb) ^ 1;
  400. current_run = 0;
  401. while (superblocks_decoded < s->superblock_count - num_partial_superblocks
  402. && get_bits_left(gb) > 0) {
  403. if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
  404. bit = get_bits1(gb);
  405. else
  406. bit ^= 1;
  407. current_run = get_vlc2(gb,
  408. s->superblock_run_length_vlc.table, 6, 2) + 1;
  409. if (current_run == 34)
  410. current_run += get_bits(gb, 12);
  411. for (j = 0; j < current_run; current_superblock++) {
  412. if (current_superblock >= s->superblock_count) {
  413. av_log(s->avctx, AV_LOG_ERROR, "Invalid fully coded superblock run length\n");
  414. return -1;
  415. }
  416. /* skip any superblocks already marked as partially coded */
  417. if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {
  418. s->superblock_coding[current_superblock] = 2*bit;
  419. j++;
  420. }
  421. }
  422. superblocks_decoded += current_run;
  423. }
  424. }
  425. /* if there were partial blocks, initialize bitstream for
  426. * unpacking fragment codings */
  427. if (num_partial_superblocks) {
  428. current_run = 0;
  429. bit = get_bits1(gb);
  430. /* toggle the bit because as soon as the first run length is
  431. * fetched the bit will be toggled again */
  432. bit ^= 1;
  433. }
  434. }
  435. /* figure out which fragments are coded; iterate through each
  436. * superblock (all planes) */
  437. s->total_num_coded_frags = 0;
  438. memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
  439. for (plane = 0; plane < 3; plane++) {
  440. int sb_start = superblock_starts[plane];
  441. int sb_end = sb_start + (plane ? s->c_superblock_count : s->y_superblock_count);
  442. int num_coded_frags = 0;
  443. for (i = sb_start; i < sb_end && get_bits_left(gb) > 0; i++) {
  444. /* iterate through all 16 fragments in a superblock */
  445. for (j = 0; j < 16; j++) {
  446. /* if the fragment is in bounds, check its coding status */
  447. current_fragment = s->superblock_fragments[i * 16 + j];
  448. if (current_fragment != -1) {
  449. int coded = s->superblock_coding[i];
  450. if (s->superblock_coding[i] == SB_PARTIALLY_CODED) {
  451. /* fragment may or may not be coded; this is the case
  452. * that cares about the fragment coding runs */
  453. if (current_run-- == 0) {
  454. bit ^= 1;
  455. current_run = get_vlc2(gb,
  456. s->fragment_run_length_vlc.table, 5, 2);
  457. }
  458. coded = bit;
  459. }
  460. if (coded) {
  461. /* default mode; actual mode will be decoded in
  462. * the next phase */
  463. s->all_fragments[current_fragment].coding_method =
  464. MODE_INTER_NO_MV;
  465. s->coded_fragment_list[plane][num_coded_frags++] =
  466. current_fragment;
  467. } else {
  468. /* not coded; copy this fragment from the prior frame */
  469. s->all_fragments[current_fragment].coding_method =
  470. MODE_COPY;
  471. }
  472. }
  473. }
  474. }
  475. s->total_num_coded_frags += num_coded_frags;
  476. for (i = 0; i < 64; i++)
  477. s->num_coded_frags[plane][i] = num_coded_frags;
  478. if (plane < 2)
  479. s->coded_fragment_list[plane+1] = s->coded_fragment_list[plane] + num_coded_frags;
  480. }
  481. return 0;
  482. }
  483. /*
  484. * This function unpacks all the coding mode data for individual macroblocks
  485. * from the bitstream.
  486. */
  487. static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
  488. {
  489. int i, j, k, sb_x, sb_y;
  490. int scheme;
  491. int current_macroblock;
  492. int current_fragment;
  493. int coding_mode;
  494. int custom_mode_alphabet[CODING_MODE_COUNT];
  495. const int *alphabet;
  496. Vp3Fragment *frag;
  497. if (s->keyframe) {
  498. for (i = 0; i < s->fragment_count; i++)
  499. s->all_fragments[i].coding_method = MODE_INTRA;
  500. } else {
  501. /* fetch the mode coding scheme for this frame */
  502. scheme = get_bits(gb, 3);
  503. /* is it a custom coding scheme? */
  504. if (scheme == 0) {
  505. for (i = 0; i < 8; i++)
  506. custom_mode_alphabet[i] = MODE_INTER_NO_MV;
  507. for (i = 0; i < 8; i++)
  508. custom_mode_alphabet[get_bits(gb, 3)] = i;
  509. alphabet = custom_mode_alphabet;
  510. } else
  511. alphabet = ModeAlphabet[scheme-1];
  512. /* iterate through all of the macroblocks that contain 1 or more
  513. * coded fragments */
  514. for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
  515. for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
  516. if (get_bits_left(gb) <= 0)
  517. return -1;
  518. for (j = 0; j < 4; j++) {
  519. int mb_x = 2*sb_x + (j>>1);
  520. int mb_y = 2*sb_y + (((j>>1)+j)&1);
  521. current_macroblock = mb_y * s->macroblock_width + mb_x;
  522. if (mb_x >= s->macroblock_width || mb_y >= s->macroblock_height)
  523. continue;
  524. #define BLOCK_X (2*mb_x + (k&1))
  525. #define BLOCK_Y (2*mb_y + (k>>1))
  526. /* coding modes are only stored if the macroblock has at least one
  527. * luma block coded, otherwise it must be INTER_NO_MV */
  528. for (k = 0; k < 4; k++) {
  529. current_fragment = BLOCK_Y*s->fragment_width[0] + BLOCK_X;
  530. if (s->all_fragments[current_fragment].coding_method != MODE_COPY)
  531. break;
  532. }
  533. if (k == 4) {
  534. s->macroblock_coding[current_macroblock] = MODE_INTER_NO_MV;
  535. continue;
  536. }
  537. /* mode 7 means get 3 bits for each coding mode */
  538. if (scheme == 7)
  539. coding_mode = get_bits(gb, 3);
  540. else
  541. coding_mode = alphabet
  542. [get_vlc2(gb, s->mode_code_vlc.table, 3, 3)];
  543. s->macroblock_coding[current_macroblock] = coding_mode;
  544. for (k = 0; k < 4; k++) {
  545. frag = s->all_fragments + BLOCK_Y*s->fragment_width[0] + BLOCK_X;
  546. if (frag->coding_method != MODE_COPY)
  547. frag->coding_method = coding_mode;
  548. }
  549. #define SET_CHROMA_MODES \
  550. if (frag[s->fragment_start[1]].coding_method != MODE_COPY) \
  551. frag[s->fragment_start[1]].coding_method = coding_mode;\
  552. if (frag[s->fragment_start[2]].coding_method != MODE_COPY) \
  553. frag[s->fragment_start[2]].coding_method = coding_mode;
  554. if (s->chroma_y_shift) {
  555. frag = s->all_fragments + mb_y*s->fragment_width[1] + mb_x;
  556. SET_CHROMA_MODES
  557. } else if (s->chroma_x_shift) {
  558. frag = s->all_fragments + 2*mb_y*s->fragment_width[1] + mb_x;
  559. for (k = 0; k < 2; k++) {
  560. SET_CHROMA_MODES
  561. frag += s->fragment_width[1];
  562. }
  563. } else {
  564. for (k = 0; k < 4; k++) {
  565. frag = s->all_fragments + BLOCK_Y*s->fragment_width[1] + BLOCK_X;
  566. SET_CHROMA_MODES
  567. }
  568. }
  569. }
  570. }
  571. }
  572. }
  573. return 0;
  574. }
  575. /*
  576. * This function unpacks all the motion vectors for the individual
  577. * macroblocks from the bitstream.
  578. */
  579. static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
  580. {
  581. int j, k, sb_x, sb_y;
  582. int coding_mode;
  583. int motion_x[4];
  584. int motion_y[4];
  585. int last_motion_x = 0;
  586. int last_motion_y = 0;
  587. int prior_last_motion_x = 0;
  588. int prior_last_motion_y = 0;
  589. int current_macroblock;
  590. int current_fragment;
  591. int frag;
  592. if (s->keyframe)
  593. return 0;
  594. /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme */
  595. coding_mode = get_bits1(gb);
  596. /* iterate through all of the macroblocks that contain 1 or more
  597. * coded fragments */
  598. for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
  599. for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
  600. if (get_bits_left(gb) <= 0)
  601. return -1;
  602. for (j = 0; j < 4; j++) {
  603. int mb_x = 2*sb_x + (j>>1);
  604. int mb_y = 2*sb_y + (((j>>1)+j)&1);
  605. current_macroblock = mb_y * s->macroblock_width + mb_x;
  606. if (mb_x >= s->macroblock_width || mb_y >= s->macroblock_height ||
  607. (s->macroblock_coding[current_macroblock] == MODE_COPY))
  608. continue;
  609. switch (s->macroblock_coding[current_macroblock]) {
  610. case MODE_INTER_PLUS_MV:
  611. case MODE_GOLDEN_MV:
  612. /* all 6 fragments use the same motion vector */
  613. if (coding_mode == 0) {
  614. motion_x[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
  615. motion_y[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
  616. } else {
  617. motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)];
  618. motion_y[0] = fixed_motion_vector_table[get_bits(gb, 6)];
  619. }
  620. /* vector maintenance, only on MODE_INTER_PLUS_MV */
  621. if (s->macroblock_coding[current_macroblock] ==
  622. MODE_INTER_PLUS_MV) {
  623. prior_last_motion_x = last_motion_x;
  624. prior_last_motion_y = last_motion_y;
  625. last_motion_x = motion_x[0];
  626. last_motion_y = motion_y[0];
  627. }
  628. break;
  629. case MODE_INTER_FOURMV:
  630. /* vector maintenance */
  631. prior_last_motion_x = last_motion_x;
  632. prior_last_motion_y = last_motion_y;
  633. /* fetch 4 vectors from the bitstream, one for each
  634. * Y fragment, then average for the C fragment vectors */
  635. for (k = 0; k < 4; k++) {
  636. current_fragment = BLOCK_Y*s->fragment_width[0] + BLOCK_X;
  637. if (s->all_fragments[current_fragment].coding_method != MODE_COPY) {
  638. if (coding_mode == 0) {
  639. motion_x[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
  640. motion_y[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
  641. } else {
  642. motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)];
  643. motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)];
  644. }
  645. last_motion_x = motion_x[k];
  646. last_motion_y = motion_y[k];
  647. } else {
  648. motion_x[k] = 0;
  649. motion_y[k] = 0;
  650. }
  651. }
  652. break;
  653. case MODE_INTER_LAST_MV:
  654. /* all 6 fragments use the last motion vector */
  655. motion_x[0] = last_motion_x;
  656. motion_y[0] = last_motion_y;
  657. /* no vector maintenance (last vector remains the
  658. * last vector) */
  659. break;
  660. case MODE_INTER_PRIOR_LAST:
  661. /* all 6 fragments use the motion vector prior to the
  662. * last motion vector */
  663. motion_x[0] = prior_last_motion_x;
  664. motion_y[0] = prior_last_motion_y;
  665. /* vector maintenance */
  666. prior_last_motion_x = last_motion_x;
  667. prior_last_motion_y = last_motion_y;
  668. last_motion_x = motion_x[0];
  669. last_motion_y = motion_y[0];
  670. break;
  671. default:
  672. /* covers intra, inter without MV, golden without MV */
  673. motion_x[0] = 0;
  674. motion_y[0] = 0;
  675. /* no vector maintenance */
  676. break;
  677. }
  678. /* assign the motion vectors to the correct fragments */
  679. for (k = 0; k < 4; k++) {
  680. current_fragment =
  681. BLOCK_Y*s->fragment_width[0] + BLOCK_X;
  682. if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
  683. s->motion_val[0][current_fragment][0] = motion_x[k];
  684. s->motion_val[0][current_fragment][1] = motion_y[k];
  685. } else {
  686. s->motion_val[0][current_fragment][0] = motion_x[0];
  687. s->motion_val[0][current_fragment][1] = motion_y[0];
  688. }
  689. }
  690. if (s->chroma_y_shift) {
  691. if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
  692. motion_x[0] = RSHIFT(motion_x[0] + motion_x[1] + motion_x[2] + motion_x[3], 2);
  693. motion_y[0] = RSHIFT(motion_y[0] + motion_y[1] + motion_y[2] + motion_y[3], 2);
  694. }
  695. motion_x[0] = (motion_x[0]>>1) | (motion_x[0]&1);
  696. motion_y[0] = (motion_y[0]>>1) | (motion_y[0]&1);
  697. frag = mb_y*s->fragment_width[1] + mb_x;
  698. s->motion_val[1][frag][0] = motion_x[0];
  699. s->motion_val[1][frag][1] = motion_y[0];
  700. } else if (s->chroma_x_shift) {
  701. if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
  702. motion_x[0] = RSHIFT(motion_x[0] + motion_x[1], 1);
  703. motion_y[0] = RSHIFT(motion_y[0] + motion_y[1], 1);
  704. motion_x[1] = RSHIFT(motion_x[2] + motion_x[3], 1);
  705. motion_y[1] = RSHIFT(motion_y[2] + motion_y[3], 1);
  706. } else {
  707. motion_x[1] = motion_x[0];
  708. motion_y[1] = motion_y[0];
  709. }
  710. motion_x[0] = (motion_x[0]>>1) | (motion_x[0]&1);
  711. motion_x[1] = (motion_x[1]>>1) | (motion_x[1]&1);
  712. frag = 2*mb_y*s->fragment_width[1] + mb_x;
  713. for (k = 0; k < 2; k++) {
  714. s->motion_val[1][frag][0] = motion_x[k];
  715. s->motion_val[1][frag][1] = motion_y[k];
  716. frag += s->fragment_width[1];
  717. }
  718. } else {
  719. for (k = 0; k < 4; k++) {
  720. frag = BLOCK_Y*s->fragment_width[1] + BLOCK_X;
  721. if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
  722. s->motion_val[1][frag][0] = motion_x[k];
  723. s->motion_val[1][frag][1] = motion_y[k];
  724. } else {
  725. s->motion_val[1][frag][0] = motion_x[0];
  726. s->motion_val[1][frag][1] = motion_y[0];
  727. }
  728. }
  729. }
  730. }
  731. }
  732. }
  733. return 0;
  734. }
  735. static int unpack_block_qpis(Vp3DecodeContext *s, GetBitContext *gb)
  736. {
  737. int qpi, i, j, bit, run_length, blocks_decoded, num_blocks_at_qpi;
  738. int num_blocks = s->total_num_coded_frags;
  739. for (qpi = 0; qpi < s->nqps-1 && num_blocks > 0; qpi++) {
  740. i = blocks_decoded = num_blocks_at_qpi = 0;
  741. bit = get_bits1(gb) ^ 1;
  742. run_length = 0;
  743. do {
  744. if (run_length == MAXIMUM_LONG_BIT_RUN)
  745. bit = get_bits1(gb);
  746. else
  747. bit ^= 1;
  748. run_length = get_vlc2(gb, s->superblock_run_length_vlc.table, 6, 2) + 1;
  749. if (run_length == 34)
  750. run_length += get_bits(gb, 12);
  751. blocks_decoded += run_length;
  752. if (!bit)
  753. num_blocks_at_qpi += run_length;
  754. for (j = 0; j < run_length; i++) {
  755. if (i >= s->total_num_coded_frags)
  756. return -1;
  757. if (s->all_fragments[s->coded_fragment_list[0][i]].qpi == qpi) {
  758. s->all_fragments[s->coded_fragment_list[0][i]].qpi += bit;
  759. j++;
  760. }
  761. }
  762. } while (blocks_decoded < num_blocks && get_bits_left(gb) > 0);
  763. num_blocks -= num_blocks_at_qpi;
  764. }
  765. return 0;
  766. }
  767. /*
  768. * This function is called by unpack_dct_coeffs() to extract the VLCs from
  769. * the bitstream. The VLCs encode tokens which are used to unpack DCT
  770. * data. This function unpacks all the VLCs for either the Y plane or both
  771. * C planes, and is called for DC coefficients or different AC coefficient
  772. * levels (since different coefficient types require different VLC tables.
  773. *
  774. * This function returns a residual eob run. E.g, if a particular token gave
  775. * instructions to EOB the next 5 fragments and there were only 2 fragments
  776. * left in the current fragment range, 3 would be returned so that it could
  777. * be passed into the next call to this same function.
  778. */
  779. static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
  780. VLC *table, int coeff_index,
  781. int plane,
  782. int eob_run)
  783. {
  784. int i, j = 0;
  785. int token;
  786. int zero_run = 0;
  787. DCTELEM coeff = 0;
  788. int bits_to_get;
  789. int blocks_ended;
  790. int coeff_i = 0;
  791. int num_coeffs = s->num_coded_frags[plane][coeff_index];
  792. int16_t *dct_tokens = s->dct_tokens[plane][coeff_index];
  793. /* local references to structure members to avoid repeated deferences */
  794. int *coded_fragment_list = s->coded_fragment_list[plane];
  795. Vp3Fragment *all_fragments = s->all_fragments;
  796. VLC_TYPE (*vlc_table)[2] = table->table;
  797. if (num_coeffs < 0)
  798. av_log(s->avctx, AV_LOG_ERROR, "Invalid number of coefficents at level %d\n", coeff_index);
  799. if (eob_run > num_coeffs) {
  800. coeff_i = blocks_ended = num_coeffs;
  801. eob_run -= num_coeffs;
  802. } else {
  803. coeff_i = blocks_ended = eob_run;
  804. eob_run = 0;
  805. }
  806. // insert fake EOB token to cover the split between planes or zzi
  807. if (blocks_ended)
  808. dct_tokens[j++] = blocks_ended << 2;
  809. while (coeff_i < num_coeffs && get_bits_left(gb) > 0) {
  810. /* decode a VLC into a token */
  811. token = get_vlc2(gb, vlc_table, 11, 3);
  812. /* use the token to get a zero run, a coefficient, and an eob run */
  813. if ((unsigned) token <= 6U) {
  814. eob_run = eob_run_base[token];
  815. if (eob_run_get_bits[token])
  816. eob_run += get_bits(gb, eob_run_get_bits[token]);
  817. // record only the number of blocks ended in this plane,
  818. // any spill will be recorded in the next plane.
  819. if (eob_run > num_coeffs - coeff_i) {
  820. dct_tokens[j++] = TOKEN_EOB(num_coeffs - coeff_i);
  821. blocks_ended += num_coeffs - coeff_i;
  822. eob_run -= num_coeffs - coeff_i;
  823. coeff_i = num_coeffs;
  824. } else {
  825. dct_tokens[j++] = TOKEN_EOB(eob_run);
  826. blocks_ended += eob_run;
  827. coeff_i += eob_run;
  828. eob_run = 0;
  829. }
  830. } else if (token >= 0) {
  831. bits_to_get = coeff_get_bits[token];
  832. if (bits_to_get)
  833. bits_to_get = get_bits(gb, bits_to_get);
  834. coeff = coeff_tables[token][bits_to_get];
  835. zero_run = zero_run_base[token];
  836. if (zero_run_get_bits[token])
  837. zero_run += get_bits(gb, zero_run_get_bits[token]);
  838. if (zero_run) {
  839. dct_tokens[j++] = TOKEN_ZERO_RUN(coeff, zero_run);
  840. } else {
  841. // Save DC into the fragment structure. DC prediction is
  842. // done in raster order, so the actual DC can't be in with
  843. // other tokens. We still need the token in dct_tokens[]
  844. // however, or else the structure collapses on itself.
  845. if (!coeff_index)
  846. all_fragments[coded_fragment_list[coeff_i]].dc = coeff;
  847. dct_tokens[j++] = TOKEN_COEFF(coeff);
  848. }
  849. if (coeff_index + zero_run > 64) {
  850. av_log(s->avctx, AV_LOG_DEBUG, "Invalid zero run of %d with"
  851. " %d coeffs left\n", zero_run, 64-coeff_index);
  852. zero_run = 64 - coeff_index;
  853. }
  854. // zero runs code multiple coefficients,
  855. // so don't try to decode coeffs for those higher levels
  856. for (i = coeff_index+1; i <= coeff_index+zero_run; i++)
  857. s->num_coded_frags[plane][i]--;
  858. coeff_i++;
  859. } else {
  860. av_log(s->avctx, AV_LOG_ERROR,
  861. "Invalid token %d\n", token);
  862. return -1;
  863. }
  864. }
  865. if (blocks_ended > s->num_coded_frags[plane][coeff_index])
  866. av_log(s->avctx, AV_LOG_ERROR, "More blocks ended than coded!\n");
  867. // decrement the number of blocks that have higher coeffecients for each
  868. // EOB run at this level
  869. if (blocks_ended)
  870. for (i = coeff_index+1; i < 64; i++)
  871. s->num_coded_frags[plane][i] -= blocks_ended;
  872. // setup the next buffer
  873. if (plane < 2)
  874. s->dct_tokens[plane+1][coeff_index] = dct_tokens + j;
  875. else if (coeff_index < 63)
  876. s->dct_tokens[0][coeff_index+1] = dct_tokens + j;
  877. return eob_run;
  878. }
  879. static void reverse_dc_prediction(Vp3DecodeContext *s,
  880. int first_fragment,
  881. int fragment_width,
  882. int fragment_height);
  883. /*
  884. * This function unpacks all of the DCT coefficient data from the
  885. * bitstream.
  886. */
  887. static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
  888. {
  889. int i;
  890. int dc_y_table;
  891. int dc_c_table;
  892. int ac_y_table;
  893. int ac_c_table;
  894. int residual_eob_run = 0;
  895. VLC *y_tables[64];
  896. VLC *c_tables[64];
  897. s->dct_tokens[0][0] = s->dct_tokens_base;
  898. /* fetch the DC table indexes */
  899. dc_y_table = get_bits(gb, 4);
  900. dc_c_table = get_bits(gb, 4);
  901. /* unpack the Y plane DC coefficients */
  902. residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
  903. 0, residual_eob_run);
  904. if (residual_eob_run < 0)
  905. return residual_eob_run;
  906. /* reverse prediction of the Y-plane DC coefficients */
  907. reverse_dc_prediction(s, 0, s->fragment_width[0], s->fragment_height[0]);
  908. /* unpack the C plane DC coefficients */
  909. residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
  910. 1, residual_eob_run);
  911. if (residual_eob_run < 0)
  912. return residual_eob_run;
  913. residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
  914. 2, residual_eob_run);
  915. if (residual_eob_run < 0)
  916. return residual_eob_run;
  917. /* reverse prediction of the C-plane DC coefficients */
  918. if (!(s->avctx->flags & CODEC_FLAG_GRAY))
  919. {
  920. reverse_dc_prediction(s, s->fragment_start[1],
  921. s->fragment_width[1], s->fragment_height[1]);
  922. reverse_dc_prediction(s, s->fragment_start[2],
  923. s->fragment_width[1], s->fragment_height[1]);
  924. }
  925. /* fetch the AC table indexes */
  926. ac_y_table = get_bits(gb, 4);
  927. ac_c_table = get_bits(gb, 4);
  928. /* build tables of AC VLC tables */
  929. for (i = 1; i <= 5; i++) {
  930. y_tables[i] = &s->ac_vlc_1[ac_y_table];
  931. c_tables[i] = &s->ac_vlc_1[ac_c_table];
  932. }
  933. for (i = 6; i <= 14; i++) {
  934. y_tables[i] = &s->ac_vlc_2[ac_y_table];
  935. c_tables[i] = &s->ac_vlc_2[ac_c_table];
  936. }
  937. for (i = 15; i <= 27; i++) {
  938. y_tables[i] = &s->ac_vlc_3[ac_y_table];
  939. c_tables[i] = &s->ac_vlc_3[ac_c_table];
  940. }
  941. for (i = 28; i <= 63; i++) {
  942. y_tables[i] = &s->ac_vlc_4[ac_y_table];
  943. c_tables[i] = &s->ac_vlc_4[ac_c_table];
  944. }
  945. /* decode all AC coefficents */
  946. for (i = 1; i <= 63; i++) {
  947. residual_eob_run = unpack_vlcs(s, gb, y_tables[i], i,
  948. 0, residual_eob_run);
  949. if (residual_eob_run < 0)
  950. return residual_eob_run;
  951. residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
  952. 1, residual_eob_run);
  953. if (residual_eob_run < 0)
  954. return residual_eob_run;
  955. residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
  956. 2, residual_eob_run);
  957. if (residual_eob_run < 0)
  958. return residual_eob_run;
  959. }
  960. return 0;
  961. }
  962. /*
  963. * This function reverses the DC prediction for each coded fragment in
  964. * the frame. Much of this function is adapted directly from the original
  965. * VP3 source code.
  966. */
  967. #define COMPATIBLE_FRAME(x) \
  968. (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
  969. #define DC_COEFF(u) s->all_fragments[u].dc
  970. static void reverse_dc_prediction(Vp3DecodeContext *s,
  971. int first_fragment,
  972. int fragment_width,
  973. int fragment_height)
  974. {
  975. #define PUL 8
  976. #define PU 4
  977. #define PUR 2
  978. #define PL 1
  979. int x, y;
  980. int i = first_fragment;
  981. int predicted_dc;
  982. /* DC values for the left, up-left, up, and up-right fragments */
  983. int vl, vul, vu, vur;
  984. /* indexes for the left, up-left, up, and up-right fragments */
  985. int l, ul, u, ur;
  986. /*
  987. * The 6 fields mean:
  988. * 0: up-left multiplier
  989. * 1: up multiplier
  990. * 2: up-right multiplier
  991. * 3: left multiplier
  992. */
  993. static const int predictor_transform[16][4] = {
  994. { 0, 0, 0, 0},
  995. { 0, 0, 0,128}, // PL
  996. { 0, 0,128, 0}, // PUR
  997. { 0, 0, 53, 75}, // PUR|PL
  998. { 0,128, 0, 0}, // PU
  999. { 0, 64, 0, 64}, // PU|PL
  1000. { 0,128, 0, 0}, // PU|PUR
  1001. { 0, 0, 53, 75}, // PU|PUR|PL
  1002. {128, 0, 0, 0}, // PUL
  1003. { 0, 0, 0,128}, // PUL|PL
  1004. { 64, 0, 64, 0}, // PUL|PUR
  1005. { 0, 0, 53, 75}, // PUL|PUR|PL
  1006. { 0,128, 0, 0}, // PUL|PU
  1007. {-104,116, 0,116}, // PUL|PU|PL
  1008. { 24, 80, 24, 0}, // PUL|PU|PUR
  1009. {-104,116, 0,116} // PUL|PU|PUR|PL
  1010. };
  1011. /* This table shows which types of blocks can use other blocks for
  1012. * prediction. For example, INTRA is the only mode in this table to
  1013. * have a frame number of 0. That means INTRA blocks can only predict
  1014. * from other INTRA blocks. There are 2 golden frame coding types;
  1015. * blocks encoding in these modes can only predict from other blocks
  1016. * that were encoded with these 1 of these 2 modes. */
  1017. static const unsigned char compatible_frame[9] = {
  1018. 1, /* MODE_INTER_NO_MV */
  1019. 0, /* MODE_INTRA */
  1020. 1, /* MODE_INTER_PLUS_MV */
  1021. 1, /* MODE_INTER_LAST_MV */
  1022. 1, /* MODE_INTER_PRIOR_MV */
  1023. 2, /* MODE_USING_GOLDEN */
  1024. 2, /* MODE_GOLDEN_MV */
  1025. 1, /* MODE_INTER_FOUR_MV */
  1026. 3 /* MODE_COPY */
  1027. };
  1028. int current_frame_type;
  1029. /* there is a last DC predictor for each of the 3 frame types */
  1030. short last_dc[3];
  1031. int transform = 0;
  1032. vul = vu = vur = vl = 0;
  1033. last_dc[0] = last_dc[1] = last_dc[2] = 0;
  1034. /* for each fragment row... */
  1035. for (y = 0; y < fragment_height; y++) {
  1036. /* for each fragment in a row... */
  1037. for (x = 0; x < fragment_width; x++, i++) {
  1038. /* reverse prediction if this block was coded */
  1039. if (s->all_fragments[i].coding_method != MODE_COPY) {
  1040. current_frame_type =
  1041. compatible_frame[s->all_fragments[i].coding_method];
  1042. transform= 0;
  1043. if(x){
  1044. l= i-1;
  1045. vl = DC_COEFF(l);
  1046. if(COMPATIBLE_FRAME(l))
  1047. transform |= PL;
  1048. }
  1049. if(y){
  1050. u= i-fragment_width;
  1051. vu = DC_COEFF(u);
  1052. if(COMPATIBLE_FRAME(u))
  1053. transform |= PU;
  1054. if(x){
  1055. ul= i-fragment_width-1;
  1056. vul = DC_COEFF(ul);
  1057. if(COMPATIBLE_FRAME(ul))
  1058. transform |= PUL;
  1059. }
  1060. if(x + 1 < fragment_width){
  1061. ur= i-fragment_width+1;
  1062. vur = DC_COEFF(ur);
  1063. if(COMPATIBLE_FRAME(ur))
  1064. transform |= PUR;
  1065. }
  1066. }
  1067. if (transform == 0) {
  1068. /* if there were no fragments to predict from, use last
  1069. * DC saved */
  1070. predicted_dc = last_dc[current_frame_type];
  1071. } else {
  1072. /* apply the appropriate predictor transform */
  1073. predicted_dc =
  1074. (predictor_transform[transform][0] * vul) +
  1075. (predictor_transform[transform][1] * vu) +
  1076. (predictor_transform[transform][2] * vur) +
  1077. (predictor_transform[transform][3] * vl);
  1078. predicted_dc /= 128;
  1079. /* check for outranging on the [ul u l] and
  1080. * [ul u ur l] predictors */
  1081. if ((transform == 15) || (transform == 13)) {
  1082. if (FFABS(predicted_dc - vu) > 128)
  1083. predicted_dc = vu;
  1084. else if (FFABS(predicted_dc - vl) > 128)
  1085. predicted_dc = vl;
  1086. else if (FFABS(predicted_dc - vul) > 128)
  1087. predicted_dc = vul;
  1088. }
  1089. }
  1090. /* at long last, apply the predictor */
  1091. DC_COEFF(i) += predicted_dc;
  1092. /* save the DC */
  1093. last_dc[current_frame_type] = DC_COEFF(i);
  1094. }
  1095. }
  1096. }
  1097. }
  1098. static void apply_loop_filter(Vp3DecodeContext *s, int plane, int ystart, int yend)
  1099. {
  1100. int x, y;
  1101. int *bounding_values= s->bounding_values_array+127;
  1102. int width = s->fragment_width[!!plane];
  1103. int height = s->fragment_height[!!plane];
  1104. int fragment = s->fragment_start [plane] + ystart * width;
  1105. int stride = s->current_frame.linesize[plane];
  1106. uint8_t *plane_data = s->current_frame.data [plane];
  1107. if (!s->flipped_image) stride = -stride;
  1108. plane_data += s->data_offset[plane] + 8*ystart*stride;
  1109. for (y = ystart; y < yend; y++) {
  1110. for (x = 0; x < width; x++) {
  1111. /* This code basically just deblocks on the edges of coded blocks.
  1112. * However, it has to be much more complicated because of the
  1113. * braindamaged deblock ordering used in VP3/Theora. Order matters
  1114. * because some pixels get filtered twice. */
  1115. if( s->all_fragments[fragment].coding_method != MODE_COPY )
  1116. {
  1117. /* do not perform left edge filter for left columns frags */
  1118. if (x > 0) {
  1119. s->vp3dsp.h_loop_filter(
  1120. plane_data + 8*x,
  1121. stride, bounding_values);
  1122. }
  1123. /* do not perform top edge filter for top row fragments */
  1124. if (y > 0) {
  1125. s->vp3dsp.v_loop_filter(
  1126. plane_data + 8*x,
  1127. stride, bounding_values);
  1128. }
  1129. /* do not perform right edge filter for right column
  1130. * fragments or if right fragment neighbor is also coded
  1131. * in this frame (it will be filtered in next iteration) */
  1132. if ((x < width - 1) &&
  1133. (s->all_fragments[fragment + 1].coding_method == MODE_COPY)) {
  1134. s->vp3dsp.h_loop_filter(
  1135. plane_data + 8*x + 8,
  1136. stride, bounding_values);
  1137. }
  1138. /* do not perform bottom edge filter for bottom row
  1139. * fragments or if bottom fragment neighbor is also coded
  1140. * in this frame (it will be filtered in the next row) */
  1141. if ((y < height - 1) &&
  1142. (s->all_fragments[fragment + width].coding_method == MODE_COPY)) {
  1143. s->vp3dsp.v_loop_filter(
  1144. plane_data + 8*x + 8*stride,
  1145. stride, bounding_values);
  1146. }
  1147. }
  1148. fragment++;
  1149. }
  1150. plane_data += 8*stride;
  1151. }
  1152. }
  1153. /**
  1154. * Pull DCT tokens from the 64 levels to decode and dequant the coefficients
  1155. * for the next block in coding order
  1156. */
  1157. static inline int vp3_dequant(Vp3DecodeContext *s, Vp3Fragment *frag,
  1158. int plane, int inter, DCTELEM block[64])
  1159. {
  1160. int16_t *dequantizer = s->qmat[frag->qpi][inter][plane];
  1161. uint8_t *perm = s->scantable.permutated;
  1162. int i = 0;
  1163. do {
  1164. int token = *s->dct_tokens[plane][i];
  1165. switch (token & 3) {
  1166. case 0: // EOB
  1167. if (--token < 4) // 0-3 are token types, so the EOB run must now be 0
  1168. s->dct_tokens[plane][i]++;
  1169. else
  1170. *s->dct_tokens[plane][i] = token & ~3;
  1171. goto end;
  1172. case 1: // zero run
  1173. s->dct_tokens[plane][i]++;
  1174. i += (token >> 2) & 0x7f;
  1175. if (i > 63) {
  1176. av_log(s->avctx, AV_LOG_ERROR, "Coefficient index overflow\n");
  1177. return i;
  1178. }
  1179. block[perm[i]] = (token >> 9) * dequantizer[perm[i]];
  1180. i++;
  1181. break;
  1182. case 2: // coeff
  1183. block[perm[i]] = (token >> 2) * dequantizer[perm[i]];
  1184. s->dct_tokens[plane][i++]++;
  1185. break;
  1186. default: // shouldn't happen
  1187. return i;
  1188. }
  1189. } while (i < 64);
  1190. // return value is expected to be a valid level
  1191. i--;
  1192. end:
  1193. // the actual DC+prediction is in the fragment structure
  1194. block[0] = frag->dc * s->qmat[0][inter][plane][0];
  1195. return i;
  1196. }
  1197. /**
  1198. * called when all pixels up to row y are complete
  1199. */
  1200. static void vp3_draw_horiz_band(Vp3DecodeContext *s, int y)
  1201. {
  1202. int h, cy, i;
  1203. int offset[AV_NUM_DATA_POINTERS];
  1204. if (HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
  1205. int y_flipped = s->flipped_image ? s->avctx->height-y : y;
  1206. // At the end of the frame, report INT_MAX instead of the height of the frame.
  1207. // This makes the other threads' ff_thread_await_progress() calls cheaper, because
  1208. // they don't have to clip their values.
  1209. ff_thread_report_progress(&s->current_frame, y_flipped==s->avctx->height ? INT_MAX : y_flipped-1, 0);
  1210. }
  1211. if(s->avctx->draw_horiz_band==NULL)
  1212. return;
  1213. h= y - s->last_slice_end;
  1214. s->last_slice_end= y;
  1215. y -= h;
  1216. if (!s->flipped_image) {
  1217. y = s->avctx->height - y - h;
  1218. }
  1219. cy = y >> s->chroma_y_shift;
  1220. offset[0] = s->current_frame.linesize[0]*y;
  1221. offset[1] = s->current_frame.linesize[1]*cy;
  1222. offset[2] = s->current_frame.linesize[2]*cy;
  1223. for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
  1224. offset[i] = 0;
  1225. emms_c();
  1226. s->avctx->draw_horiz_band(s->avctx, &s->current_frame, offset, y, 3, h);
  1227. }
  1228. /**
  1229. * Wait for the reference frame of the current fragment.
  1230. * The progress value is in luma pixel rows.
  1231. */
  1232. static void await_reference_row(Vp3DecodeContext *s, Vp3Fragment *fragment, int motion_y, int y)
  1233. {
  1234. AVFrame *ref_frame;
  1235. int ref_row;
  1236. int border = motion_y&1;
  1237. if (fragment->coding_method == MODE_USING_GOLDEN ||
  1238. fragment->coding_method == MODE_GOLDEN_MV)
  1239. ref_frame = &s->golden_frame;
  1240. else
  1241. ref_frame = &s->last_frame;
  1242. ref_row = y + (motion_y>>1);
  1243. ref_row = FFMAX(FFABS(ref_row), ref_row + 8 + border);
  1244. ff_thread_await_progress(ref_frame, ref_row, 0);
  1245. }
  1246. /*
  1247. * Perform the final rendering for a particular slice of data.
  1248. * The slice number ranges from 0..(c_superblock_height - 1).
  1249. */
  1250. static void render_slice(Vp3DecodeContext *s, int slice)
  1251. {
  1252. int x, y, i, j, fragment;
  1253. LOCAL_ALIGNED_16(DCTELEM, block, [64]);
  1254. int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
  1255. int motion_halfpel_index;
  1256. uint8_t *motion_source;
  1257. int plane, first_pixel;
  1258. if (slice >= s->c_superblock_height)
  1259. return;
  1260. for (plane = 0; plane < 3; plane++) {
  1261. uint8_t *output_plane = s->current_frame.data [plane] + s->data_offset[plane];
  1262. uint8_t * last_plane = s-> last_frame.data [plane] + s->data_offset[plane];
  1263. uint8_t *golden_plane = s-> golden_frame.data [plane] + s->data_offset[plane];
  1264. int stride = s->current_frame.linesize[plane];
  1265. int plane_width = s->width >> (plane && s->chroma_x_shift);
  1266. int plane_height = s->height >> (plane && s->chroma_y_shift);
  1267. int8_t (*motion_val)[2] = s->motion_val[!!plane];
  1268. int sb_x, sb_y = slice << (!plane && s->chroma_y_shift);
  1269. int slice_height = sb_y + 1 + (!plane && s->chroma_y_shift);
  1270. int slice_width = plane ? s->c_superblock_width : s->y_superblock_width;
  1271. int fragment_width = s->fragment_width[!!plane];
  1272. int fragment_height = s->fragment_height[!!plane];
  1273. int fragment_start = s->fragment_start[plane];
  1274. int do_await = !plane && HAVE_THREADS && (s->avctx->active_thread_type&FF_THREAD_FRAME);
  1275. if (!s->flipped_image) stride = -stride;
  1276. if (CONFIG_GRAY && plane && (s->avctx->flags & CODEC_FLAG_GRAY))
  1277. continue;
  1278. /* for each superblock row in the slice (both of them)... */
  1279. for (; sb_y < slice_height; sb_y++) {
  1280. /* for each superblock in a row... */
  1281. for (sb_x = 0; sb_x < slice_width; sb_x++) {
  1282. /* for each block in a superblock... */
  1283. for (j = 0; j < 16; j++) {
  1284. x = 4*sb_x + hilbert_offset[j][0];
  1285. y = 4*sb_y + hilbert_offset[j][1];
  1286. fragment = y*fragment_width + x;
  1287. i = fragment_start + fragment;
  1288. // bounds check
  1289. if (x >= fragment_width || y >= fragment_height)
  1290. continue;
  1291. first_pixel = 8*y*stride + 8*x;
  1292. if (do_await && s->all_fragments[i].coding_method != MODE_INTRA)
  1293. await_reference_row(s, &s->all_fragments[i], motion_val[fragment][1], (16*y) >> s->chroma_y_shift);
  1294. /* transform if this block was coded */
  1295. if (s->all_fragments[i].coding_method != MODE_COPY) {
  1296. if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
  1297. (s->all_fragments[i].coding_method == MODE_GOLDEN_MV))
  1298. motion_source= golden_plane;
  1299. else
  1300. motion_source= last_plane;
  1301. motion_source += first_pixel;
  1302. motion_halfpel_index = 0;
  1303. /* sort out the motion vector if this fragment is coded
  1304. * using a motion vector method */
  1305. if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
  1306. (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) {
  1307. int src_x, src_y;
  1308. motion_x = motion_val[fragment][0];
  1309. motion_y = motion_val[fragment][1];
  1310. src_x= (motion_x>>1) + 8*x;
  1311. src_y= (motion_y>>1) + 8*y;
  1312. motion_halfpel_index = motion_x & 0x01;
  1313. motion_source += (motion_x >> 1);
  1314. motion_halfpel_index |= (motion_y & 0x01) << 1;
  1315. motion_source += ((motion_y >> 1) * stride);
  1316. if(src_x<0 || src_y<0 || src_x + 9 >= plane_width || src_y + 9 >= plane_height){
  1317. uint8_t *temp= s->edge_emu_buffer;
  1318. if(stride<0) temp -= 8*stride;
  1319. s->dsp.emulated_edge_mc(temp, motion_source, stride, 9, 9, src_x, src_y, plane_width, plane_height);
  1320. motion_source= temp;
  1321. }
  1322. }
  1323. /* first, take care of copying a block from either the
  1324. * previous or the golden frame */
  1325. if (s->all_fragments[i].coding_method != MODE_INTRA) {
  1326. /* Note, it is possible to implement all MC cases with
  1327. put_no_rnd_pixels_l2 which would look more like the
  1328. VP3 source but this would be slower as
  1329. put_no_rnd_pixels_tab is better optimzed */
  1330. if(motion_halfpel_index != 3){
  1331. s->dsp.put_no_rnd_pixels_tab[1][motion_halfpel_index](
  1332. output_plane + first_pixel,
  1333. motion_source, stride, 8);
  1334. }else{
  1335. int d= (motion_x ^ motion_y)>>31; // d is 0 if motion_x and _y have the same sign, else -1
  1336. s->dsp.put_no_rnd_pixels_l2[1](
  1337. output_plane + first_pixel,
  1338. motion_source - d,
  1339. motion_source + stride + 1 + d,
  1340. stride, 8);
  1341. }
  1342. }
  1343. s->dsp.clear_block(block);
  1344. /* invert DCT and place (or add) in final output */
  1345. if (s->all_fragments[i].coding_method == MODE_INTRA) {
  1346. vp3_dequant(s, s->all_fragments + i, plane, 0, block);
  1347. s->vp3dsp.idct_put(
  1348. output_plane + first_pixel,
  1349. stride,
  1350. block);
  1351. } else {
  1352. if (vp3_dequant(s, s->all_fragments + i, plane, 1, block)) {
  1353. s->vp3dsp.idct_add(
  1354. output_plane + first_pixel,
  1355. stride,
  1356. block);
  1357. } else {
  1358. s->vp3dsp.idct_dc_add(output_plane + first_pixel, stride, block);
  1359. }
  1360. }
  1361. } else {
  1362. /* copy directly from the previous frame */
  1363. s->dsp.put_pixels_tab[1][0](
  1364. output_plane + first_pixel,
  1365. last_plane + first_pixel,
  1366. stride, 8);
  1367. }
  1368. }
  1369. }
  1370. // Filter up to the last row in the superblock row
  1371. if (!s->skip_loop_filter)
  1372. apply_loop_filter(s, plane, 4*sb_y - !!sb_y, FFMIN(4*sb_y+3, fragment_height-1));
  1373. }
  1374. }
  1375. /* this looks like a good place for slice dispatch... */
  1376. /* algorithm:
  1377. * if (slice == s->macroblock_height - 1)
  1378. * dispatch (both last slice & 2nd-to-last slice);
  1379. * else if (slice > 0)
  1380. * dispatch (slice - 1);
  1381. */
  1382. vp3_draw_horiz_band(s, FFMIN((32 << s->chroma_y_shift) * (slice + 1) -16, s->height-16));
  1383. }
  1384. /// Allocate tables for per-frame data in Vp3DecodeContext
  1385. static av_cold int allocate_tables(AVCodecContext *avctx)
  1386. {
  1387. Vp3DecodeContext *s = avctx->priv_data;
  1388. int y_fragment_count, c_fragment_count;
  1389. y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
  1390. c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
  1391. s->superblock_coding = av_malloc(s->superblock_count);
  1392. s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment));
  1393. s->coded_fragment_list[0] = av_malloc(s->fragment_count * sizeof(int));
  1394. s->dct_tokens_base = av_malloc(64*s->fragment_count * sizeof(*s->dct_tokens_base));
  1395. s->motion_val[0] = av_malloc(y_fragment_count * sizeof(*s->motion_val[0]));
  1396. s->motion_val[1] = av_malloc(c_fragment_count * sizeof(*s->motion_val[1]));
  1397. /* work out the block mapping tables */
  1398. s->superblock_fragments = av_malloc(s->superblock_count * 16 * sizeof(int));
  1399. s->macroblock_coding = av_malloc(s->macroblock_count + 1);
  1400. if (!s->superblock_coding || !s->all_fragments || !s->dct_tokens_base ||
  1401. !s->coded_fragment_list[0] || !s->superblock_fragments || !s->macroblock_coding ||
  1402. !s->motion_val[0] || !s->motion_val[1]) {
  1403. vp3_decode_end(avctx);
  1404. return -1;
  1405. }
  1406. init_block_mapping(s);
  1407. return 0;
  1408. }
  1409. static av_cold int vp3_decode_init(AVCodecContext *avctx)
  1410. {
  1411. Vp3DecodeContext *s = avctx->priv_data;
  1412. int i, inter, plane;
  1413. int c_width;
  1414. int c_height;
  1415. int y_fragment_count, c_fragment_count;
  1416. if (avctx->codec_tag == MKTAG('V','P','3','0'))
  1417. s->version = 0;
  1418. else
  1419. s->version = 1;
  1420. s->avctx = avctx;
  1421. s->width = FFALIGN(avctx->width, 16);
  1422. s->height = FFALIGN(avctx->height, 16);
  1423. if (avctx->codec_id != AV_CODEC_ID_THEORA)
  1424. avctx->pix_fmt = PIX_FMT_YUV420P;
  1425. avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
  1426. ff_dsputil_init(&s->dsp, avctx);
  1427. ff_vp3dsp_init(&s->vp3dsp, avctx->flags);
  1428. ff_init_scantable_permutation(s->dsp.idct_permutation, s->vp3dsp.idct_perm);
  1429. ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
  1430. /* initialize to an impossible value which will force a recalculation
  1431. * in the first frame decode */
  1432. for (i = 0; i < 3; i++)
  1433. s->qps[i] = -1;
  1434. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift);
  1435. s->y_superblock_width = (s->width + 31) / 32;
  1436. s->y_superblock_height = (s->height + 31) / 32;
  1437. s->y_superblock_count = s->y_superblock_width * s->y_superblock_height;
  1438. /* work out the dimensions for the C planes */
  1439. c_width = s->width >> s->chroma_x_shift;
  1440. c_height = s->height >> s->chroma_y_shift;
  1441. s->c_superblock_width = (c_width + 31) / 32;
  1442. s->c_superblock_height = (c_height + 31) / 32;
  1443. s->c_superblock_count = s->c_superblock_width * s->c_superblock_height;
  1444. s->superblock_count = s->y_superblock_count + (s->c_superblock_count * 2);
  1445. s->u_superblock_start = s->y_superblock_count;
  1446. s->v_superblock_start = s->u_superblock_start + s->c_superblock_count;
  1447. s->macroblock_width = (s->width + 15) / 16;
  1448. s->macroblock_height = (s->height + 15) / 16;
  1449. s->macroblock_count = s->macroblock_width * s->macroblock_height;
  1450. s->fragment_width[0] = s->width / FRAGMENT_PIXELS;
  1451. s->fragment_height[0] = s->height / FRAGMENT_PIXELS;
  1452. s->fragment_width[1] = s->fragment_width[0] >> s->chroma_x_shift;
  1453. s->fragment_height[1] = s->fragment_height[0] >> s->chroma_y_shift;
  1454. /* fragment count covers all 8x8 blocks for all 3 planes */
  1455. y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
  1456. c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
  1457. s->fragment_count = y_fragment_count + 2*c_fragment_count;
  1458. s->fragment_start[1] = y_fragment_count;
  1459. s->fragment_start[2] = y_fragment_count + c_fragment_count;
  1460. if (!s->theora_tables)
  1461. {
  1462. for (i = 0; i < 64; i++) {
  1463. s->coded_dc_scale_factor[i] = vp31_dc_scale_factor[i];
  1464. s->coded_ac_scale_factor[i] = vp31_ac_scale_factor[i];
  1465. s->base_matrix[0][i] = vp31_intra_y_dequant[i];
  1466. s->base_matrix[1][i] = vp31_intra_c_dequant[i];
  1467. s->base_matrix[2][i] = vp31_inter_dequant[i];
  1468. s->filter_limit_values[i] = vp31_filter_limit_values[i];
  1469. }
  1470. for(inter=0; inter<2; inter++){
  1471. for(plane=0; plane<3; plane++){
  1472. s->qr_count[inter][plane]= 1;
  1473. s->qr_size [inter][plane][0]= 63;
  1474. s->qr_base [inter][plane][0]=
  1475. s->qr_base [inter][plane][1]= 2*inter + (!!plane)*!inter;
  1476. }
  1477. }
  1478. /* init VLC tables */
  1479. for (i = 0; i < 16; i++) {
  1480. /* DC histograms */
  1481. init_vlc(&s->dc_vlc[i], 11, 32,
  1482. &dc_bias[i][0][1], 4, 2,
  1483. &dc_bias[i][0][0], 4, 2, 0);
  1484. /* group 1 AC histograms */
  1485. init_vlc(&s->ac_vlc_1[i], 11, 32,
  1486. &ac_bias_0[i][0][1], 4, 2,
  1487. &ac_bias_0[i][0][0], 4, 2, 0);
  1488. /* group 2 AC histograms */
  1489. init_vlc(&s->ac_vlc_2[i], 11, 32,
  1490. &ac_bias_1[i][0][1], 4, 2,
  1491. &ac_bias_1[i][0][0], 4, 2, 0);
  1492. /* group 3 AC histograms */
  1493. init_vlc(&s->ac_vlc_3[i], 11, 32,
  1494. &ac_bias_2[i][0][1], 4, 2,
  1495. &ac_bias_2[i][0][0], 4, 2, 0);
  1496. /* group 4 AC histograms */
  1497. init_vlc(&s->ac_vlc_4[i], 11, 32,
  1498. &ac_bias_3[i][0][1], 4, 2,
  1499. &ac_bias_3[i][0][0], 4, 2, 0);
  1500. }
  1501. } else {
  1502. for (i = 0; i < 16; i++) {
  1503. /* DC histograms */
  1504. if (init_vlc(&s->dc_vlc[i], 11, 32,
  1505. &s->huffman_table[i][0][1], 8, 4,
  1506. &s->huffman_table[i][0][0], 8, 4, 0) < 0)
  1507. goto vlc_fail;
  1508. /* group 1 AC histograms */
  1509. if (init_vlc(&s->ac_vlc_1[i], 11, 32,
  1510. &s->huffman_table[i+16][0][1], 8, 4,
  1511. &s->huffman_table[i+16][0][0], 8, 4, 0) < 0)
  1512. goto vlc_fail;
  1513. /* group 2 AC histograms */
  1514. if (init_vlc(&s->ac_vlc_2[i], 11, 32,
  1515. &s->huffman_table[i+16*2][0][1], 8, 4,
  1516. &s->huffman_table[i+16*2][0][0], 8, 4, 0) < 0)
  1517. goto vlc_fail;
  1518. /* group 3 AC histograms */
  1519. if (init_vlc(&s->ac_vlc_3[i], 11, 32,
  1520. &s->huffman_table[i+16*3][0][1], 8, 4,
  1521. &s->huffman_table[i+16*3][0][0], 8, 4, 0) < 0)
  1522. goto vlc_fail;
  1523. /* group 4 AC histograms */
  1524. if (init_vlc(&s->ac_vlc_4[i], 11, 32,
  1525. &s->huffman_table[i+16*4][0][1], 8, 4,
  1526. &s->huffman_table[i+16*4][0][0], 8, 4, 0) < 0)
  1527. goto vlc_fail;
  1528. }
  1529. }
  1530. init_vlc(&s->superblock_run_length_vlc, 6, 34,
  1531. &superblock_run_length_vlc_table[0][1], 4, 2,
  1532. &superblock_run_length_vlc_table[0][0], 4, 2, 0);
  1533. init_vlc(&s->fragment_run_length_vlc, 5, 30,
  1534. &fragment_run_length_vlc_table[0][1], 4, 2,
  1535. &fragment_run_length_vlc_table[0][0], 4, 2, 0);
  1536. init_vlc(&s->mode_code_vlc, 3, 8,
  1537. &mode_code_vlc_table[0][1], 2, 1,
  1538. &mode_code_vlc_table[0][0], 2, 1, 0);
  1539. init_vlc(&s->motion_vector_vlc, 6, 63,
  1540. &motion_vector_vlc_table[0][1], 2, 1,
  1541. &motion_vector_vlc_table[0][0], 2, 1, 0);
  1542. for (i = 0; i < 3; i++) {
  1543. s->current_frame.data[i] = NULL;
  1544. s->last_frame.data[i] = NULL;
  1545. s->golden_frame.data[i] = NULL;
  1546. }
  1547. return allocate_tables(avctx);
  1548. vlc_fail:
  1549. av_log(avctx, AV_LOG_FATAL, "Invalid huffman table\n");
  1550. return -1;
  1551. }
  1552. /// Release and shuffle frames after decode finishes
  1553. static void update_frames(AVCodecContext *avctx)
  1554. {
  1555. Vp3DecodeContext *s = avctx->priv_data;
  1556. /* release the last frame, if it is allocated and if it is not the
  1557. * golden frame */
  1558. if (s->last_frame.data[0] && s->last_frame.type != FF_BUFFER_TYPE_COPY)
  1559. ff_thread_release_buffer(avctx, &s->last_frame);
  1560. /* shuffle frames (last = current) */
  1561. s->last_frame= s->current_frame;
  1562. if (s->keyframe) {
  1563. if (s->golden_frame.data[0])
  1564. ff_thread_release_buffer(avctx, &s->golden_frame);
  1565. s->golden_frame = s->current_frame;
  1566. s->last_frame.type = FF_BUFFER_TYPE_COPY;
  1567. }
  1568. s->current_frame.data[0]= NULL; /* ensure that we catch any access to this released frame */
  1569. }
  1570. static int vp3_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
  1571. {
  1572. Vp3DecodeContext *s = dst->priv_data, *s1 = src->priv_data;
  1573. int qps_changed = 0, i, err;
  1574. #define copy_fields(to, from, start_field, end_field) memcpy(&to->start_field, &from->start_field, (char*)&to->end_field - (char*)&to->start_field)
  1575. if (!s1->current_frame.data[0]
  1576. ||s->width != s1->width
  1577. ||s->height!= s1->height) {
  1578. if (s != s1)
  1579. copy_fields(s, s1, golden_frame, keyframe);
  1580. return -1;
  1581. }
  1582. if (s != s1) {
  1583. // init tables if the first frame hasn't been decoded
  1584. if (!s->current_frame.data[0]) {
  1585. int y_fragment_count, c_fragment_count;
  1586. s->avctx = dst;
  1587. err = allocate_tables(dst);
  1588. if (err)
  1589. return err;
  1590. y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
  1591. c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
  1592. memcpy(s->motion_val[0], s1->motion_val[0], y_fragment_count * sizeof(*s->motion_val[0]));
  1593. memcpy(s->motion_val[1], s1->motion_val[1], c_fragment_count * sizeof(*s->motion_val[1]));
  1594. }
  1595. // copy previous frame data
  1596. copy_fields(s, s1, golden_frame, dsp);
  1597. // copy qscale data if necessary
  1598. for (i = 0; i < 3; i++) {
  1599. if (s->qps[i] != s1->qps[1]) {
  1600. qps_changed = 1;
  1601. memcpy(&s->qmat[i], &s1->qmat[i], sizeof(s->qmat[i]));
  1602. }
  1603. }
  1604. if (s->qps[0] != s1->qps[0])
  1605. memcpy(&s->bounding_values_array, &s1->bounding_values_array, sizeof(s->bounding_values_array));
  1606. if (qps_changed)
  1607. copy_fields(s, s1, qps, superblock_count);
  1608. #undef copy_fields
  1609. }
  1610. update_frames(dst);
  1611. return 0;
  1612. }
  1613. static int vp3_decode_frame(AVCodecContext *avctx,
  1614. void *data, int *data_size,
  1615. AVPacket *avpkt)
  1616. {
  1617. const uint8_t *buf = avpkt->data;
  1618. int buf_size = avpkt->size;
  1619. Vp3DecodeContext *s = avctx->priv_data;
  1620. GetBitContext gb;
  1621. int i;
  1622. init_get_bits(&gb, buf, buf_size * 8);
  1623. if (s->theora && get_bits1(&gb))
  1624. {
  1625. av_log(avctx, AV_LOG_ERROR, "Header packet passed to frame decoder, skipping\n");
  1626. return -1;
  1627. }
  1628. s->keyframe = !get_bits1(&gb);
  1629. if (!s->theora)
  1630. skip_bits(&gb, 1);
  1631. for (i = 0; i < 3; i++)
  1632. s->last_qps[i] = s->qps[i];
  1633. s->nqps=0;
  1634. do{
  1635. s->qps[s->nqps++]= get_bits(&gb, 6);
  1636. } while(s->theora >= 0x030200 && s->nqps<3 && get_bits1(&gb));
  1637. for (i = s->nqps; i < 3; i++)
  1638. s->qps[i] = -1;
  1639. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1640. av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n",
  1641. s->keyframe?"key":"", avctx->frame_number+1, s->qps[0]);
  1642. s->skip_loop_filter = !s->filter_limit_values[s->qps[0]] ||
  1643. avctx->skip_loop_filter >= (s->keyframe ? AVDISCARD_ALL : AVDISCARD_NONKEY);
  1644. if (s->qps[0] != s->last_qps[0])
  1645. init_loop_filter(s);
  1646. for (i = 0; i < s->nqps; i++)
  1647. // reinit all dequantizers if the first one changed, because
  1648. // the DC of the first quantizer must be used for all matrices
  1649. if (s->qps[i] != s->last_qps[i] || s->qps[0] != s->last_qps[0])
  1650. init_dequantizer(s, i);
  1651. if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe)
  1652. return buf_size;
  1653. s->current_frame.reference = 3;
  1654. s->current_frame.pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  1655. s->current_frame.key_frame = s->keyframe;
  1656. if (ff_thread_get_buffer(avctx, &s->current_frame) < 0) {
  1657. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1658. goto error;
  1659. }
  1660. if (!s->edge_emu_buffer)
  1661. s->edge_emu_buffer = av_malloc(9*FFABS(s->current_frame.linesize[0]));
  1662. if (s->keyframe) {
  1663. if (!s->theora)
  1664. {
  1665. skip_bits(&gb, 4); /* width code */
  1666. skip_bits(&gb, 4); /* height code */
  1667. if (s->version)
  1668. {
  1669. s->version = get_bits(&gb, 5);
  1670. if (avctx->frame_number == 0)
  1671. av_log(s->avctx, AV_LOG_DEBUG, "VP version: %d\n", s->version);
  1672. }
  1673. }
  1674. if (s->version || s->theora)
  1675. {
  1676. if (get_bits1(&gb))
  1677. av_log(s->avctx, AV_LOG_ERROR, "Warning, unsupported keyframe coding type?!\n");
  1678. skip_bits(&gb, 2); /* reserved? */
  1679. }
  1680. } else {
  1681. if (!s->golden_frame.data[0]) {
  1682. av_log(s->avctx, AV_LOG_WARNING, "vp3: first frame not a keyframe\n");
  1683. s->golden_frame.reference = 3;
  1684. s->golden_frame.pict_type = AV_PICTURE_TYPE_I;
  1685. if (ff_thread_get_buffer(avctx, &s->golden_frame) < 0) {
  1686. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1687. goto error;
  1688. }
  1689. s->last_frame = s->golden_frame;
  1690. s->last_frame.type = FF_BUFFER_TYPE_COPY;
  1691. ff_thread_report_progress(&s->last_frame, INT_MAX, 0);
  1692. }
  1693. }
  1694. memset(s->all_fragments, 0, s->fragment_count * sizeof(Vp3Fragment));
  1695. ff_thread_finish_setup(avctx);
  1696. if (unpack_superblocks(s, &gb)){
  1697. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n");
  1698. goto error;
  1699. }
  1700. if (unpack_modes(s, &gb)){
  1701. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n");
  1702. goto error;
  1703. }
  1704. if (unpack_vectors(s, &gb)){
  1705. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n");
  1706. goto error;
  1707. }
  1708. if (unpack_block_qpis(s, &gb)){
  1709. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_block_qpis\n");
  1710. goto error;
  1711. }
  1712. if (unpack_dct_coeffs(s, &gb)){
  1713. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n");
  1714. goto error;
  1715. }
  1716. for (i = 0; i < 3; i++) {
  1717. int height = s->height >> (i && s->chroma_y_shift);
  1718. if (s->flipped_image)
  1719. s->data_offset[i] = 0;
  1720. else
  1721. s->data_offset[i] = (height-1) * s->current_frame.linesize[i];
  1722. }
  1723. s->last_slice_end = 0;
  1724. for (i = 0; i < s->c_superblock_height; i++)
  1725. render_slice(s, i);
  1726. // filter the last row
  1727. for (i = 0; i < 3; i++) {
  1728. int row = (s->height >> (3+(i && s->chroma_y_shift))) - 1;
  1729. apply_loop_filter(s, i, row, row+1);
  1730. }
  1731. vp3_draw_horiz_band(s, s->avctx->height);
  1732. *data_size=sizeof(AVFrame);
  1733. *(AVFrame*)data= s->current_frame;
  1734. if (!HAVE_THREADS || !(s->avctx->active_thread_type&FF_THREAD_FRAME))
  1735. update_frames(avctx);
  1736. return buf_size;
  1737. error:
  1738. ff_thread_report_progress(&s->current_frame, INT_MAX, 0);
  1739. if (!HAVE_THREADS || !(s->avctx->active_thread_type&FF_THREAD_FRAME))
  1740. avctx->release_buffer(avctx, &s->current_frame);
  1741. return -1;
  1742. }
  1743. static int read_huffman_tree(AVCodecContext *avctx, GetBitContext *gb)
  1744. {
  1745. Vp3DecodeContext *s = avctx->priv_data;
  1746. if (get_bits1(gb)) {
  1747. int token;
  1748. if (s->entries >= 32) { /* overflow */
  1749. av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
  1750. return -1;
  1751. }
  1752. token = get_bits(gb, 5);
  1753. //av_log(avctx, AV_LOG_DEBUG, "hti %d hbits %x token %d entry : %d size %d\n", s->hti, s->hbits, token, s->entries, s->huff_code_size);
  1754. s->huffman_table[s->hti][token][0] = s->hbits;
  1755. s->huffman_table[s->hti][token][1] = s->huff_code_size;
  1756. s->entries++;
  1757. }
  1758. else {
  1759. if (s->huff_code_size >= 32) {/* overflow */
  1760. av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
  1761. return -1;
  1762. }
  1763. s->huff_code_size++;
  1764. s->hbits <<= 1;
  1765. if (read_huffman_tree(avctx, gb))
  1766. return -1;
  1767. s->hbits |= 1;
  1768. if (read_huffman_tree(avctx, gb))
  1769. return -1;
  1770. s->hbits >>= 1;
  1771. s->huff_code_size--;
  1772. }
  1773. return 0;
  1774. }
  1775. static int vp3_init_thread_copy(AVCodecContext *avctx)
  1776. {
  1777. Vp3DecodeContext *s = avctx->priv_data;
  1778. s->superblock_coding = NULL;
  1779. s->all_fragments = NULL;
  1780. s->coded_fragment_list[0] = NULL;
  1781. s->dct_tokens_base = NULL;
  1782. s->superblock_fragments = NULL;
  1783. s->macroblock_coding = NULL;
  1784. s->motion_val[0] = NULL;
  1785. s->motion_val[1] = NULL;
  1786. s->edge_emu_buffer = NULL;
  1787. return 0;
  1788. }
  1789. #if CONFIG_THEORA_DECODER
  1790. static const enum PixelFormat theora_pix_fmts[4] = {
  1791. PIX_FMT_YUV420P, PIX_FMT_NONE, PIX_FMT_YUV422P, PIX_FMT_YUV444P
  1792. };
  1793. static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb)
  1794. {
  1795. Vp3DecodeContext *s = avctx->priv_data;
  1796. int visible_width, visible_height, colorspace;
  1797. int offset_x = 0, offset_y = 0;
  1798. AVRational fps, aspect;
  1799. s->theora = get_bits_long(gb, 24);
  1800. av_log(avctx, AV_LOG_DEBUG, "Theora bitstream version %X\n", s->theora);
  1801. /* 3.2.0 aka alpha3 has the same frame orientation as original vp3 */
  1802. /* but previous versions have the image flipped relative to vp3 */
  1803. if (s->theora < 0x030200)
  1804. {
  1805. s->flipped_image = 1;
  1806. av_log(avctx, AV_LOG_DEBUG, "Old (<alpha3) Theora bitstream, flipped image\n");
  1807. }
  1808. visible_width = s->width = get_bits(gb, 16) << 4;
  1809. visible_height = s->height = get_bits(gb, 16) << 4;
  1810. if(av_image_check_size(s->width, s->height, 0, avctx)){
  1811. av_log(avctx, AV_LOG_ERROR, "Invalid dimensions (%dx%d)\n", s->width, s->height);
  1812. s->width= s->height= 0;
  1813. return -1;
  1814. }
  1815. if (s->theora >= 0x030200) {
  1816. visible_width = get_bits_long(gb, 24);
  1817. visible_height = get_bits_long(gb, 24);
  1818. offset_x = get_bits(gb, 8); /* offset x */
  1819. offset_y = get_bits(gb, 8); /* offset y, from bottom */
  1820. }
  1821. fps.num = get_bits_long(gb, 32);
  1822. fps.den = get_bits_long(gb, 32);
  1823. if (fps.num && fps.den) {
  1824. av_reduce(&avctx->time_base.num, &avctx->time_base.den,
  1825. fps.den, fps.num, 1<<30);
  1826. }
  1827. aspect.num = get_bits_long(gb, 24);
  1828. aspect.den = get_bits_long(gb, 24);
  1829. if (aspect.num && aspect.den) {
  1830. av_reduce(&avctx->sample_aspect_ratio.num,
  1831. &avctx->sample_aspect_ratio.den,
  1832. aspect.num, aspect.den, 1<<30);
  1833. }
  1834. if (s->theora < 0x030200)
  1835. skip_bits(gb, 5); /* keyframe frequency force */
  1836. colorspace = get_bits(gb, 8);
  1837. skip_bits(gb, 24); /* bitrate */
  1838. skip_bits(gb, 6); /* quality hint */
  1839. if (s->theora >= 0x030200)
  1840. {
  1841. skip_bits(gb, 5); /* keyframe frequency force */
  1842. avctx->pix_fmt = theora_pix_fmts[get_bits(gb, 2)];
  1843. skip_bits(gb, 3); /* reserved */
  1844. }
  1845. // align_get_bits(gb);
  1846. if ( visible_width <= s->width && visible_width > s->width-16
  1847. && visible_height <= s->height && visible_height > s->height-16
  1848. && !offset_x && (offset_y == s->height - visible_height))
  1849. avcodec_set_dimensions(avctx, visible_width, visible_height);
  1850. else
  1851. avcodec_set_dimensions(avctx, s->width, s->height);
  1852. if (colorspace == 1) {
  1853. avctx->color_primaries = AVCOL_PRI_BT470M;
  1854. } else if (colorspace == 2) {
  1855. avctx->color_primaries = AVCOL_PRI_BT470BG;
  1856. }
  1857. if (colorspace == 1 || colorspace == 2) {
  1858. avctx->colorspace = AVCOL_SPC_BT470BG;
  1859. avctx->color_trc = AVCOL_TRC_BT709;
  1860. }
  1861. return 0;
  1862. }
  1863. static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb)
  1864. {
  1865. Vp3DecodeContext *s = avctx->priv_data;
  1866. int i, n, matrices, inter, plane;
  1867. if (s->theora >= 0x030200) {
  1868. n = get_bits(gb, 3);
  1869. /* loop filter limit values table */
  1870. if (n)
  1871. for (i = 0; i < 64; i++)
  1872. s->filter_limit_values[i] = get_bits(gb, n);
  1873. }
  1874. if (s->theora >= 0x030200)
  1875. n = get_bits(gb, 4) + 1;
  1876. else
  1877. n = 16;
  1878. /* quality threshold table */
  1879. for (i = 0; i < 64; i++)
  1880. s->coded_ac_scale_factor[i] = get_bits(gb, n);
  1881. if (s->theora >= 0x030200)
  1882. n = get_bits(gb, 4) + 1;
  1883. else
  1884. n = 16;
  1885. /* dc scale factor table */
  1886. for (i = 0; i < 64; i++)
  1887. s->coded_dc_scale_factor[i] = get_bits(gb, n);
  1888. if (s->theora >= 0x030200)
  1889. matrices = get_bits(gb, 9) + 1;
  1890. else
  1891. matrices = 3;
  1892. if(matrices > 384){
  1893. av_log(avctx, AV_LOG_ERROR, "invalid number of base matrixes\n");
  1894. return -1;
  1895. }
  1896. for(n=0; n<matrices; n++){
  1897. for (i = 0; i < 64; i++)
  1898. s->base_matrix[n][i]= get_bits(gb, 8);
  1899. }
  1900. for (inter = 0; inter <= 1; inter++) {
  1901. for (plane = 0; plane <= 2; plane++) {
  1902. int newqr= 1;
  1903. if (inter || plane > 0)
  1904. newqr = get_bits1(gb);
  1905. if (!newqr) {
  1906. int qtj, plj;
  1907. if(inter && get_bits1(gb)){
  1908. qtj = 0;
  1909. plj = plane;
  1910. }else{
  1911. qtj= (3*inter + plane - 1) / 3;
  1912. plj= (plane + 2) % 3;
  1913. }
  1914. s->qr_count[inter][plane]= s->qr_count[qtj][plj];
  1915. memcpy(s->qr_size[inter][plane], s->qr_size[qtj][plj], sizeof(s->qr_size[0][0]));
  1916. memcpy(s->qr_base[inter][plane], s->qr_base[qtj][plj], sizeof(s->qr_base[0][0]));
  1917. } else {
  1918. int qri= 0;
  1919. int qi = 0;
  1920. for(;;){
  1921. i= get_bits(gb, av_log2(matrices-1)+1);
  1922. if(i>= matrices){
  1923. av_log(avctx, AV_LOG_ERROR, "invalid base matrix index\n");
  1924. return -1;
  1925. }
  1926. s->qr_base[inter][plane][qri]= i;
  1927. if(qi >= 63)
  1928. break;
  1929. i = get_bits(gb, av_log2(63-qi)+1) + 1;
  1930. s->qr_size[inter][plane][qri++]= i;
  1931. qi += i;
  1932. }
  1933. if (qi > 63) {
  1934. av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi);
  1935. return -1;
  1936. }
  1937. s->qr_count[inter][plane]= qri;
  1938. }
  1939. }
  1940. }
  1941. /* Huffman tables */
  1942. for (s->hti = 0; s->hti < 80; s->hti++) {
  1943. s->entries = 0;
  1944. s->huff_code_size = 1;
  1945. if (!get_bits1(gb)) {
  1946. s->hbits = 0;
  1947. if(read_huffman_tree(avctx, gb))
  1948. return -1;
  1949. s->hbits = 1;
  1950. if(read_huffman_tree(avctx, gb))
  1951. return -1;
  1952. }
  1953. }
  1954. s->theora_tables = 1;
  1955. return 0;
  1956. }
  1957. static av_cold int theora_decode_init(AVCodecContext *avctx)
  1958. {
  1959. Vp3DecodeContext *s = avctx->priv_data;
  1960. GetBitContext gb;
  1961. int ptype;
  1962. uint8_t *header_start[3];
  1963. int header_len[3];
  1964. int i;
  1965. avctx->pix_fmt = PIX_FMT_YUV420P;
  1966. s->theora = 1;
  1967. if (!avctx->extradata_size)
  1968. {
  1969. av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n");
  1970. return -1;
  1971. }
  1972. if (avpriv_split_xiph_headers(avctx->extradata, avctx->extradata_size,
  1973. 42, header_start, header_len) < 0) {
  1974. av_log(avctx, AV_LOG_ERROR, "Corrupt extradata\n");
  1975. return -1;
  1976. }
  1977. for(i=0;i<3;i++) {
  1978. init_get_bits(&gb, header_start[i], header_len[i] * 8);
  1979. ptype = get_bits(&gb, 8);
  1980. if (!(ptype & 0x80))
  1981. {
  1982. av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n");
  1983. // return -1;
  1984. }
  1985. // FIXME: Check for this as well.
  1986. skip_bits_long(&gb, 6*8); /* "theora" */
  1987. switch(ptype)
  1988. {
  1989. case 0x80:
  1990. theora_decode_header(avctx, &gb);
  1991. break;
  1992. case 0x81:
  1993. // FIXME: is this needed? it breaks sometimes
  1994. // theora_decode_comments(avctx, gb);
  1995. break;
  1996. case 0x82:
  1997. if (theora_decode_tables(avctx, &gb))
  1998. return -1;
  1999. break;
  2000. default:
  2001. av_log(avctx, AV_LOG_ERROR, "Unknown Theora config packet: %d\n", ptype&~0x80);
  2002. break;
  2003. }
  2004. if(ptype != 0x81 && 8*header_len[i] != get_bits_count(&gb))
  2005. av_log(avctx, AV_LOG_WARNING, "%d bits left in packet %X\n", 8*header_len[i] - get_bits_count(&gb), ptype);
  2006. if (s->theora < 0x030200)
  2007. break;
  2008. }
  2009. return vp3_decode_init(avctx);
  2010. }
  2011. AVCodec ff_theora_decoder = {
  2012. .name = "theora",
  2013. .type = AVMEDIA_TYPE_VIDEO,
  2014. .id = AV_CODEC_ID_THEORA,
  2015. .priv_data_size = sizeof(Vp3DecodeContext),
  2016. .init = theora_decode_init,
  2017. .close = vp3_decode_end,
  2018. .decode = vp3_decode_frame,
  2019. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND |
  2020. CODEC_CAP_FRAME_THREADS,
  2021. .flush = vp3_decode_flush,
  2022. .long_name = NULL_IF_CONFIG_SMALL("Theora"),
  2023. .init_thread_copy = ONLY_IF_THREADS_ENABLED(vp3_init_thread_copy),
  2024. .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context)
  2025. };
  2026. #endif
  2027. AVCodec ff_vp3_decoder = {
  2028. .name = "vp3",
  2029. .type = AVMEDIA_TYPE_VIDEO,
  2030. .id = AV_CODEC_ID_VP3,
  2031. .priv_data_size = sizeof(Vp3DecodeContext),
  2032. .init = vp3_decode_init,
  2033. .close = vp3_decode_end,
  2034. .decode = vp3_decode_frame,
  2035. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND |
  2036. CODEC_CAP_FRAME_THREADS,
  2037. .flush = vp3_decode_flush,
  2038. .long_name = NULL_IF_CONFIG_SMALL("On2 VP3"),
  2039. .init_thread_copy = ONLY_IF_THREADS_ENABLED(vp3_init_thread_copy),
  2040. .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context),
  2041. };