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.

2458 lines
85KB

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