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.

2409 lines
84KB

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