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.

2453 lines
85KB

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