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.

2525 lines
90KB

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