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