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.

2492 lines
89KB

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