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.

2495 lines
86KB

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