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.

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