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.

2516 lines
90KB

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