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.

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