You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2406 lines
83KB

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