/libavcodec/vp3.c

http://github.com/FFmpeg/FFmpeg · C · 3260 lines · 2458 code · 454 blank · 348 comment · 614 complexity · 503483eb2da5ccc94c391a2e91805d76 MD5 · raw file

Large files are truncated click here to view the full file

  1. /*
  2. * Copyright (C) 2003-2004 The FFmpeg project
  3. * Copyright (C) 2019 Peter Ross
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * On2 VP3/VP4 Video Decoder
  24. *
  25. * VP3 Video Decoder by Mike Melanson (mike at multimedia.cx)
  26. * For more information about the VP3 coding process, visit:
  27. * http://wiki.multimedia.cx/index.php?title=On2_VP3
  28. *
  29. * Theora decoder by Alex Beregszaszi
  30. */
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include "libavutil/imgutils.h"
  35. #include "avcodec.h"
  36. #include "get_bits.h"
  37. #include "hpeldsp.h"
  38. #include "internal.h"
  39. #include "mathops.h"
  40. #include "thread.h"
  41. #include "videodsp.h"
  42. #include "vp3data.h"
  43. #include "vp4data.h"
  44. #include "vp3dsp.h"
  45. #include "xiph.h"
  46. #define FRAGMENT_PIXELS 8
  47. // FIXME split things out into their own arrays
  48. typedef struct Vp3Fragment {
  49. int16_t dc;
  50. uint8_t coding_method;
  51. uint8_t qpi;
  52. } Vp3Fragment;
  53. #define SB_NOT_CODED 0
  54. #define SB_PARTIALLY_CODED 1
  55. #define SB_FULLY_CODED 2
  56. // This is the maximum length of a single long bit run that can be encoded
  57. // for superblock coding or block qps. Theora special-cases this to read a
  58. // bit instead of flipping the current bit to allow for runs longer than 4129.
  59. #define MAXIMUM_LONG_BIT_RUN 4129
  60. #define MODE_INTER_NO_MV 0
  61. #define MODE_INTRA 1
  62. #define MODE_INTER_PLUS_MV 2
  63. #define MODE_INTER_LAST_MV 3
  64. #define MODE_INTER_PRIOR_LAST 4
  65. #define MODE_USING_GOLDEN 5
  66. #define MODE_GOLDEN_MV 6
  67. #define MODE_INTER_FOURMV 7
  68. #define CODING_MODE_COUNT 8
  69. /* special internal mode */
  70. #define MODE_COPY 8
  71. static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb);
  72. static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb);
  73. /* There are 6 preset schemes, plus a free-form scheme */
  74. static const int ModeAlphabet[6][CODING_MODE_COUNT] = {
  75. /* scheme 1: Last motion vector dominates */
  76. { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
  77. MODE_INTER_PLUS_MV, MODE_INTER_NO_MV,
  78. MODE_INTRA, MODE_USING_GOLDEN,
  79. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  80. /* scheme 2 */
  81. { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
  82. MODE_INTER_NO_MV, MODE_INTER_PLUS_MV,
  83. MODE_INTRA, MODE_USING_GOLDEN,
  84. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  85. /* scheme 3 */
  86. { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
  87. MODE_INTER_PRIOR_LAST, MODE_INTER_NO_MV,
  88. MODE_INTRA, MODE_USING_GOLDEN,
  89. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  90. /* scheme 4 */
  91. { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
  92. MODE_INTER_NO_MV, MODE_INTER_PRIOR_LAST,
  93. MODE_INTRA, MODE_USING_GOLDEN,
  94. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  95. /* scheme 5: No motion vector dominates */
  96. { MODE_INTER_NO_MV, MODE_INTER_LAST_MV,
  97. MODE_INTER_PRIOR_LAST, MODE_INTER_PLUS_MV,
  98. MODE_INTRA, MODE_USING_GOLDEN,
  99. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  100. /* scheme 6 */
  101. { MODE_INTER_NO_MV, MODE_USING_GOLDEN,
  102. MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
  103. MODE_INTER_PLUS_MV, MODE_INTRA,
  104. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  105. };
  106. static const uint8_t hilbert_offset[16][2] = {
  107. { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 },
  108. { 0, 2 }, { 0, 3 }, { 1, 3 }, { 1, 2 },
  109. { 2, 2 }, { 2, 3 }, { 3, 3 }, { 3, 2 },
  110. { 3, 1 }, { 2, 1 }, { 2, 0 }, { 3, 0 }
  111. };
  112. enum {
  113. VP4_DC_INTRA = 0,
  114. VP4_DC_INTER = 1,
  115. VP4_DC_GOLDEN = 2,
  116. NB_VP4_DC_TYPES,
  117. VP4_DC_UNDEFINED = NB_VP4_DC_TYPES
  118. };
  119. static const uint8_t vp4_pred_block_type_map[8] = {
  120. [MODE_INTER_NO_MV] = VP4_DC_INTER,
  121. [MODE_INTRA] = VP4_DC_INTRA,
  122. [MODE_INTER_PLUS_MV] = VP4_DC_INTER,
  123. [MODE_INTER_LAST_MV] = VP4_DC_INTER,
  124. [MODE_INTER_PRIOR_LAST] = VP4_DC_INTER,
  125. [MODE_USING_GOLDEN] = VP4_DC_GOLDEN,
  126. [MODE_GOLDEN_MV] = VP4_DC_GOLDEN,
  127. [MODE_INTER_FOURMV] = VP4_DC_INTER,
  128. };
  129. typedef struct {
  130. int dc;
  131. int type;
  132. } VP4Predictor;
  133. #define MIN_DEQUANT_VAL 2
  134. typedef struct Vp3DecodeContext {
  135. AVCodecContext *avctx;
  136. int theora, theora_tables, theora_header;
  137. int version;
  138. int width, height;
  139. int chroma_x_shift, chroma_y_shift;
  140. ThreadFrame golden_frame;
  141. ThreadFrame last_frame;
  142. ThreadFrame current_frame;
  143. int keyframe;
  144. uint8_t idct_permutation[64];
  145. uint8_t idct_scantable[64];
  146. HpelDSPContext hdsp;
  147. VideoDSPContext vdsp;
  148. VP3DSPContext vp3dsp;
  149. DECLARE_ALIGNED(16, int16_t, block)[64];
  150. int flipped_image;
  151. int last_slice_end;
  152. int skip_loop_filter;
  153. int qps[3];
  154. int nqps;
  155. int last_qps[3];
  156. int superblock_count;
  157. int y_superblock_width;
  158. int y_superblock_height;
  159. int y_superblock_count;
  160. int c_superblock_width;
  161. int c_superblock_height;
  162. int c_superblock_count;
  163. int u_superblock_start;
  164. int v_superblock_start;
  165. unsigned char *superblock_coding;
  166. int macroblock_count; /* y macroblock count */
  167. int macroblock_width;
  168. int macroblock_height;
  169. int c_macroblock_count;
  170. int c_macroblock_width;
  171. int c_macroblock_height;
  172. int yuv_macroblock_count; /* y+u+v macroblock count */
  173. int fragment_count;
  174. int fragment_width[2];
  175. int fragment_height[2];
  176. Vp3Fragment *all_fragments;
  177. int fragment_start[3];
  178. int data_offset[3];
  179. uint8_t offset_x;
  180. uint8_t offset_y;
  181. int offset_x_warned;
  182. int8_t (*motion_val[2])[2];
  183. /* tables */
  184. uint16_t coded_dc_scale_factor[2][64];
  185. uint32_t coded_ac_scale_factor[64];
  186. uint8_t base_matrix[384][64];
  187. uint8_t qr_count[2][3];
  188. uint8_t qr_size[2][3][64];
  189. uint16_t qr_base[2][3][64];
  190. /**
  191. * This is a list of all tokens in bitstream order. Reordering takes place
  192. * by pulling from each level during IDCT. As a consequence, IDCT must be
  193. * in Hilbert order, making the minimum slice height 64 for 4:2:0 and 32
  194. * otherwise. The 32 different tokens with up to 12 bits of extradata are
  195. * collapsed into 3 types, packed as follows:
  196. * (from the low to high bits)
  197. *
  198. * 2 bits: type (0,1,2)
  199. * 0: EOB run, 14 bits for run length (12 needed)
  200. * 1: zero run, 7 bits for run length
  201. * 7 bits for the next coefficient (3 needed)
  202. * 2: coefficient, 14 bits (11 needed)
  203. *
  204. * Coefficients are signed, so are packed in the highest bits for automatic
  205. * sign extension.
  206. */
  207. int16_t *dct_tokens[3][64];
  208. int16_t *dct_tokens_base;
  209. #define TOKEN_EOB(eob_run) ((eob_run) << 2)
  210. #define TOKEN_ZERO_RUN(coeff, zero_run) (((coeff) * 512) + ((zero_run) << 2) + 1)
  211. #define TOKEN_COEFF(coeff) (((coeff) * 4) + 2)
  212. /**
  213. * number of blocks that contain DCT coefficients at
  214. * the given level or higher
  215. */
  216. int num_coded_frags[3][64];
  217. int total_num_coded_frags;
  218. /* this is a list of indexes into the all_fragments array indicating
  219. * which of the fragments are coded */
  220. int *coded_fragment_list[3];
  221. int *kf_coded_fragment_list;
  222. int *nkf_coded_fragment_list;
  223. int num_kf_coded_fragment[3];
  224. VLC dc_vlc[16];
  225. VLC ac_vlc_1[16];
  226. VLC ac_vlc_2[16];
  227. VLC ac_vlc_3[16];
  228. VLC ac_vlc_4[16];
  229. VLC superblock_run_length_vlc; /* version < 2 */
  230. VLC fragment_run_length_vlc; /* version < 2 */
  231. VLC block_pattern_vlc[2]; /* version >= 2*/
  232. VLC mode_code_vlc;
  233. VLC motion_vector_vlc; /* version < 2 */
  234. VLC vp4_mv_vlc[2][7]; /* version >=2 */
  235. /* these arrays need to be on 16-byte boundaries since SSE2 operations
  236. * index into them */
  237. DECLARE_ALIGNED(16, int16_t, qmat)[3][2][3][64]; ///< qmat[qpi][is_inter][plane]
  238. /* This table contains superblock_count * 16 entries. Each set of 16
  239. * numbers corresponds to the fragment indexes 0..15 of the superblock.
  240. * An entry will be -1 to indicate that no entry corresponds to that
  241. * index. */
  242. int *superblock_fragments;
  243. /* This is an array that indicates how a particular macroblock
  244. * is coded. */
  245. unsigned char *macroblock_coding;
  246. uint8_t *edge_emu_buffer;
  247. /* Huffman decode */
  248. int hti;
  249. unsigned int hbits;
  250. int entries;
  251. int huff_code_size;
  252. uint32_t huffman_table[80][32][2];
  253. uint8_t filter_limit_values[64];
  254. DECLARE_ALIGNED(8, int, bounding_values_array)[256 + 2];
  255. VP4Predictor * dc_pred_row; /* dc_pred_row[y_superblock_width * 4] */
  256. } Vp3DecodeContext;
  257. /************************************************************************
  258. * VP3 specific functions
  259. ************************************************************************/
  260. static av_cold void free_tables(AVCodecContext *avctx)
  261. {
  262. Vp3DecodeContext *s = avctx->priv_data;
  263. av_freep(&s->superblock_coding);
  264. av_freep(&s->all_fragments);
  265. av_freep(&s->nkf_coded_fragment_list);
  266. av_freep(&s->kf_coded_fragment_list);
  267. av_freep(&s->dct_tokens_base);
  268. av_freep(&s->superblock_fragments);
  269. av_freep(&s->macroblock_coding);
  270. av_freep(&s->dc_pred_row);
  271. av_freep(&s->motion_val[0]);
  272. av_freep(&s->motion_val[1]);
  273. }
  274. static void vp3_decode_flush(AVCodecContext *avctx)
  275. {
  276. Vp3DecodeContext *s = avctx->priv_data;
  277. if (s->golden_frame.f)
  278. ff_thread_release_buffer(avctx, &s->golden_frame);
  279. if (s->last_frame.f)
  280. ff_thread_release_buffer(avctx, &s->last_frame);
  281. if (s->current_frame.f)
  282. ff_thread_release_buffer(avctx, &s->current_frame);
  283. }
  284. static av_cold int vp3_decode_end(AVCodecContext *avctx)
  285. {
  286. Vp3DecodeContext *s = avctx->priv_data;
  287. int i, j;
  288. free_tables(avctx);
  289. av_freep(&s->edge_emu_buffer);
  290. s->theora_tables = 0;
  291. /* release all frames */
  292. vp3_decode_flush(avctx);
  293. av_frame_free(&s->current_frame.f);
  294. av_frame_free(&s->last_frame.f);
  295. av_frame_free(&s->golden_frame.f);
  296. for (i = 0; i < 16; i++) {
  297. ff_free_vlc(&s->dc_vlc[i]);
  298. ff_free_vlc(&s->ac_vlc_1[i]);
  299. ff_free_vlc(&s->ac_vlc_2[i]);
  300. ff_free_vlc(&s->ac_vlc_3[i]);
  301. ff_free_vlc(&s->ac_vlc_4[i]);
  302. }
  303. ff_free_vlc(&s->superblock_run_length_vlc);
  304. ff_free_vlc(&s->fragment_run_length_vlc);
  305. ff_free_vlc(&s->mode_code_vlc);
  306. ff_free_vlc(&s->motion_vector_vlc);
  307. for (j = 0; j < 2; j++)
  308. for (i = 0; i < 7; i++)
  309. ff_free_vlc(&s->vp4_mv_vlc[j][i]);
  310. for (i = 0; i < 2; i++)
  311. ff_free_vlc(&s->block_pattern_vlc[i]);
  312. return 0;
  313. }
  314. /**
  315. * This function sets up all of the various blocks mappings:
  316. * superblocks <-> fragments, macroblocks <-> fragments,
  317. * superblocks <-> macroblocks
  318. *
  319. * @return 0 is successful; returns 1 if *anything* went wrong.
  320. */
  321. static int init_block_mapping(Vp3DecodeContext *s)
  322. {
  323. int sb_x, sb_y, plane;
  324. int x, y, i, j = 0;
  325. for (plane = 0; plane < 3; plane++) {
  326. int sb_width = plane ? s->c_superblock_width
  327. : s->y_superblock_width;
  328. int sb_height = plane ? s->c_superblock_height
  329. : s->y_superblock_height;
  330. int frag_width = s->fragment_width[!!plane];
  331. int frag_height = s->fragment_height[!!plane];
  332. for (sb_y = 0; sb_y < sb_height; sb_y++)
  333. for (sb_x = 0; sb_x < sb_width; sb_x++)
  334. for (i = 0; i < 16; i++) {
  335. x = 4 * sb_x + hilbert_offset[i][0];
  336. y = 4 * sb_y + hilbert_offset[i][1];
  337. if (x < frag_width && y < frag_height)
  338. s->superblock_fragments[j++] = s->fragment_start[plane] +
  339. y * frag_width + x;
  340. else
  341. s->superblock_fragments[j++] = -1;
  342. }
  343. }
  344. return 0; /* successful path out */
  345. }
  346. /*
  347. * This function sets up the dequantization tables used for a particular
  348. * frame.
  349. */
  350. static void init_dequantizer(Vp3DecodeContext *s, int qpi)
  351. {
  352. int ac_scale_factor = s->coded_ac_scale_factor[s->qps[qpi]];
  353. int i, plane, inter, qri, bmi, bmj, qistart;
  354. for (inter = 0; inter < 2; inter++) {
  355. for (plane = 0; plane < 3; plane++) {
  356. int dc_scale_factor = s->coded_dc_scale_factor[!!plane][s->qps[qpi]];
  357. int sum = 0;
  358. for (qri = 0; qri < s->qr_count[inter][plane]; qri++) {
  359. sum += s->qr_size[inter][plane][qri];
  360. if (s->qps[qpi] <= sum)
  361. break;
  362. }
  363. qistart = sum - s->qr_size[inter][plane][qri];
  364. bmi = s->qr_base[inter][plane][qri];
  365. bmj = s->qr_base[inter][plane][qri + 1];
  366. for (i = 0; i < 64; i++) {
  367. int coeff = (2 * (sum - s->qps[qpi]) * s->base_matrix[bmi][i] -
  368. 2 * (qistart - s->qps[qpi]) * s->base_matrix[bmj][i] +
  369. s->qr_size[inter][plane][qri]) /
  370. (2 * s->qr_size[inter][plane][qri]);
  371. int qmin = 8 << (inter + !i);
  372. int qscale = i ? ac_scale_factor : dc_scale_factor;
  373. int qbias = (1 + inter) * 3;
  374. s->qmat[qpi][inter][plane][s->idct_permutation[i]] =
  375. (i == 0 || s->version < 2) ? av_clip((qscale * coeff) / 100 * 4, qmin, 4096)
  376. : (qscale * (coeff - qbias) / 100 + qbias) * 4;
  377. }
  378. /* all DC coefficients use the same quant so as not to interfere
  379. * with DC prediction */
  380. s->qmat[qpi][inter][plane][0] = s->qmat[0][inter][plane][0];
  381. }
  382. }
  383. }
  384. /*
  385. * This function initializes the loop filter boundary limits if the frame's
  386. * quality index is different from the previous frame's.
  387. *
  388. * The filter_limit_values may not be larger than 127.
  389. */
  390. static void init_loop_filter(Vp3DecodeContext *s)
  391. {
  392. ff_vp3dsp_set_bounding_values(s->bounding_values_array, s->filter_limit_values[s->qps[0]]);
  393. }
  394. /*
  395. * This function unpacks all of the superblock/macroblock/fragment coding
  396. * information from the bitstream.
  397. */
  398. static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
  399. {
  400. int superblock_starts[3] = {
  401. 0, s->u_superblock_start, s->v_superblock_start
  402. };
  403. int bit = 0;
  404. int current_superblock = 0;
  405. int current_run = 0;
  406. int num_partial_superblocks = 0;
  407. int i, j;
  408. int current_fragment;
  409. int plane;
  410. int plane0_num_coded_frags = 0;
  411. if (s->keyframe) {
  412. memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);
  413. } else {
  414. /* unpack the list of partially-coded superblocks */
  415. bit = get_bits1(gb) ^ 1;
  416. current_run = 0;
  417. while (current_superblock < s->superblock_count && get_bits_left(gb) > 0) {
  418. if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
  419. bit = get_bits1(gb);
  420. else
  421. bit ^= 1;
  422. current_run = get_vlc2(gb, s->superblock_run_length_vlc.table,
  423. 6, 2) + 1;
  424. if (current_run == 34)
  425. current_run += get_bits(gb, 12);
  426. if (current_run > s->superblock_count - current_superblock) {
  427. av_log(s->avctx, AV_LOG_ERROR,
  428. "Invalid partially coded superblock run length\n");
  429. return -1;
  430. }
  431. memset(s->superblock_coding + current_superblock, bit, current_run);
  432. current_superblock += current_run;
  433. if (bit)
  434. num_partial_superblocks += current_run;
  435. }
  436. /* unpack the list of fully coded superblocks if any of the blocks were
  437. * not marked as partially coded in the previous step */
  438. if (num_partial_superblocks < s->superblock_count) {
  439. int superblocks_decoded = 0;
  440. current_superblock = 0;
  441. bit = get_bits1(gb) ^ 1;
  442. current_run = 0;
  443. while (superblocks_decoded < s->superblock_count - num_partial_superblocks &&
  444. get_bits_left(gb) > 0) {
  445. if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
  446. bit = get_bits1(gb);
  447. else
  448. bit ^= 1;
  449. current_run = get_vlc2(gb, s->superblock_run_length_vlc.table,
  450. 6, 2) + 1;
  451. if (current_run == 34)
  452. current_run += get_bits(gb, 12);
  453. for (j = 0; j < current_run; current_superblock++) {
  454. if (current_superblock >= s->superblock_count) {
  455. av_log(s->avctx, AV_LOG_ERROR,
  456. "Invalid fully coded superblock run length\n");
  457. return -1;
  458. }
  459. /* skip any superblocks already marked as partially coded */
  460. if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {
  461. s->superblock_coding[current_superblock] = 2 * bit;
  462. j++;
  463. }
  464. }
  465. superblocks_decoded += current_run;
  466. }
  467. }
  468. /* if there were partial blocks, initialize bitstream for
  469. * unpacking fragment codings */
  470. if (num_partial_superblocks) {
  471. current_run = 0;
  472. bit = get_bits1(gb);
  473. /* toggle the bit because as soon as the first run length is
  474. * fetched the bit will be toggled again */
  475. bit ^= 1;
  476. }
  477. }
  478. /* figure out which fragments are coded; iterate through each
  479. * superblock (all planes) */
  480. s->total_num_coded_frags = 0;
  481. memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
  482. s->coded_fragment_list[0] = s->keyframe ? s->kf_coded_fragment_list
  483. : s->nkf_coded_fragment_list;
  484. for (plane = 0; plane < 3; plane++) {
  485. int sb_start = superblock_starts[plane];
  486. int sb_end = sb_start + (plane ? s->c_superblock_count
  487. : s->y_superblock_count);
  488. int num_coded_frags = 0;
  489. if (s->keyframe) {
  490. if (s->num_kf_coded_fragment[plane] == -1) {
  491. for (i = sb_start; i < sb_end; i++) {
  492. /* iterate through all 16 fragments in a superblock */
  493. for (j = 0; j < 16; j++) {
  494. /* if the fragment is in bounds, check its coding status */
  495. current_fragment = s->superblock_fragments[i * 16 + j];
  496. if (current_fragment != -1) {
  497. s->coded_fragment_list[plane][num_coded_frags++] =
  498. current_fragment;
  499. }
  500. }
  501. }
  502. s->num_kf_coded_fragment[plane] = num_coded_frags;
  503. } else
  504. num_coded_frags = s->num_kf_coded_fragment[plane];
  505. } else {
  506. for (i = sb_start; i < sb_end && get_bits_left(gb) > 0; i++) {
  507. if (get_bits_left(gb) < plane0_num_coded_frags >> 2) {
  508. return AVERROR_INVALIDDATA;
  509. }
  510. /* iterate through all 16 fragments in a superblock */
  511. for (j = 0; j < 16; j++) {
  512. /* if the fragment is in bounds, check its coding status */
  513. current_fragment = s->superblock_fragments[i * 16 + j];
  514. if (current_fragment != -1) {
  515. int coded = s->superblock_coding[i];
  516. if (coded == SB_PARTIALLY_CODED) {
  517. /* fragment may or may not be coded; this is the case
  518. * that cares about the fragment coding runs */
  519. if (current_run-- == 0) {
  520. bit ^= 1;
  521. current_run = get_vlc2(gb, s->fragment_run_length_vlc.table, 5, 2);
  522. }
  523. coded = bit;
  524. }
  525. if (coded) {
  526. /* default mode; actual mode will be decoded in
  527. * the next phase */
  528. s->all_fragments[current_fragment].coding_method =
  529. MODE_INTER_NO_MV;
  530. s->coded_fragment_list[plane][num_coded_frags++] =
  531. current_fragment;
  532. } else {
  533. /* not coded; copy this fragment from the prior frame */
  534. s->all_fragments[current_fragment].coding_method =
  535. MODE_COPY;
  536. }
  537. }
  538. }
  539. }
  540. }
  541. if (!plane)
  542. plane0_num_coded_frags = num_coded_frags;
  543. s->total_num_coded_frags += num_coded_frags;
  544. for (i = 0; i < 64; i++)
  545. s->num_coded_frags[plane][i] = num_coded_frags;
  546. if (plane < 2)
  547. s->coded_fragment_list[plane + 1] = s->coded_fragment_list[plane] +
  548. num_coded_frags;
  549. }
  550. return 0;
  551. }
  552. #define BLOCK_X (2 * mb_x + (k & 1))
  553. #define BLOCK_Y (2 * mb_y + (k >> 1))
  554. #if CONFIG_VP4_DECODER
  555. /**
  556. * @return number of blocks, or > yuv_macroblock_count on error.
  557. * return value is always >= 1.
  558. */
  559. static int vp4_get_mb_count(Vp3DecodeContext *s, GetBitContext *gb)
  560. {
  561. int v = 1;
  562. int bits;
  563. while ((bits = show_bits(gb, 9)) == 0x1ff) {
  564. skip_bits(gb, 9);
  565. v += 256;
  566. if (v > s->yuv_macroblock_count) {
  567. av_log(s->avctx, AV_LOG_ERROR, "Invalid run length\n");
  568. return v;
  569. }
  570. }
  571. #define body(n) { \
  572. skip_bits(gb, 2 + n); \
  573. v += (1 << n) + get_bits(gb, n); }
  574. #define thresh(n) (0x200 - (0x80 >> n))
  575. #define else_if(n) else if (bits < thresh(n)) body(n)
  576. if (bits < 0x100) {
  577. skip_bits(gb, 1);
  578. } else if (bits < thresh(0)) {
  579. skip_bits(gb, 2);
  580. v += 1;
  581. }
  582. else_if(1)
  583. else_if(2)
  584. else_if(3)
  585. else_if(4)
  586. else_if(5)
  587. else_if(6)
  588. else body(7)
  589. #undef body
  590. #undef thresh
  591. #undef else_if
  592. return v;
  593. }
  594. static int vp4_get_block_pattern(Vp3DecodeContext *s, GetBitContext *gb, int *next_block_pattern_table)
  595. {
  596. int v = get_vlc2(gb, s->block_pattern_vlc[*next_block_pattern_table].table, 3, 2);
  597. if (v == -1) {
  598. av_log(s->avctx, AV_LOG_ERROR, "Invalid block pattern\n");
  599. *next_block_pattern_table = 0;
  600. return 0;
  601. }
  602. *next_block_pattern_table = vp4_block_pattern_table_selector[v];
  603. return v + 1;
  604. }
  605. static int vp4_unpack_macroblocks(Vp3DecodeContext *s, GetBitContext *gb)
  606. {
  607. int plane, i, j, k, fragment;
  608. int next_block_pattern_table;
  609. int bit, current_run, has_partial;
  610. memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
  611. if (s->keyframe)
  612. return 0;
  613. has_partial = 0;
  614. bit = get_bits1(gb);
  615. for (i = 0; i < s->yuv_macroblock_count; i += current_run) {
  616. if (get_bits_left(gb) <= 0)
  617. return AVERROR_INVALIDDATA;
  618. current_run = vp4_get_mb_count(s, gb);
  619. if (current_run > s->yuv_macroblock_count - i)
  620. return -1;
  621. memset(s->superblock_coding + i, 2 * bit, current_run);
  622. bit ^= 1;
  623. has_partial |= bit;
  624. }
  625. if (has_partial) {
  626. if (get_bits_left(gb) <= 0)
  627. return AVERROR_INVALIDDATA;
  628. bit = get_bits1(gb);
  629. current_run = vp4_get_mb_count(s, gb);
  630. for (i = 0; i < s->yuv_macroblock_count; i++) {
  631. if (!s->superblock_coding[i]) {
  632. if (!current_run) {
  633. bit ^= 1;
  634. current_run = vp4_get_mb_count(s, gb);
  635. }
  636. s->superblock_coding[i] = bit;
  637. current_run--;
  638. }
  639. }
  640. if (current_run) /* handle situation when vp4_get_mb_count() fails */
  641. return -1;
  642. }
  643. next_block_pattern_table = 0;
  644. i = 0;
  645. for (plane = 0; plane < 3; plane++) {
  646. int sb_x, sb_y;
  647. int sb_width = plane ? s->c_superblock_width : s->y_superblock_width;
  648. int sb_height = plane ? s->c_superblock_height : s->y_superblock_height;
  649. int mb_width = plane ? s->c_macroblock_width : s->macroblock_width;
  650. int mb_height = plane ? s->c_macroblock_height : s->macroblock_height;
  651. int fragment_width = s->fragment_width[!!plane];
  652. int fragment_height = s->fragment_height[!!plane];
  653. for (sb_y = 0; sb_y < sb_height; sb_y++) {
  654. for (sb_x = 0; sb_x < sb_width; sb_x++) {
  655. for (j = 0; j < 4; j++) {
  656. int mb_x = 2 * sb_x + (j >> 1);
  657. int mb_y = 2 * sb_y + (j >> 1) ^ (j & 1);
  658. int mb_coded, pattern, coded;
  659. if (mb_x >= mb_width || mb_y >= mb_height)
  660. continue;
  661. mb_coded = s->superblock_coding[i++];
  662. if (mb_coded == SB_FULLY_CODED)
  663. pattern = 0xF;
  664. else if (mb_coded == SB_PARTIALLY_CODED)
  665. pattern = vp4_get_block_pattern(s, gb, &next_block_pattern_table);
  666. else
  667. pattern = 0;
  668. for (k = 0; k < 4; k++) {
  669. if (BLOCK_X >= fragment_width || BLOCK_Y >= fragment_height)
  670. continue;
  671. fragment = s->fragment_start[plane] + BLOCK_Y * fragment_width + BLOCK_X;
  672. coded = pattern & (8 >> k);
  673. /* MODE_INTER_NO_MV is the default for coded fragments.
  674. the actual method is decoded in the next phase. */
  675. s->all_fragments[fragment].coding_method = coded ? MODE_INTER_NO_MV : MODE_COPY;
  676. }
  677. }
  678. }
  679. }
  680. }
  681. return 0;
  682. }
  683. #endif
  684. /*
  685. * This function unpacks all the coding mode data for individual macroblocks
  686. * from the bitstream.
  687. */
  688. static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
  689. {
  690. int i, j, k, sb_x, sb_y;
  691. int scheme;
  692. int current_macroblock;
  693. int current_fragment;
  694. int coding_mode;
  695. int custom_mode_alphabet[CODING_MODE_COUNT];
  696. const int *alphabet;
  697. Vp3Fragment *frag;
  698. if (s->keyframe) {
  699. for (i = 0; i < s->fragment_count; i++)
  700. s->all_fragments[i].coding_method = MODE_INTRA;
  701. } else {
  702. /* fetch the mode coding scheme for this frame */
  703. scheme = get_bits(gb, 3);
  704. /* is it a custom coding scheme? */
  705. if (scheme == 0) {
  706. for (i = 0; i < 8; i++)
  707. custom_mode_alphabet[i] = MODE_INTER_NO_MV;
  708. for (i = 0; i < 8; i++)
  709. custom_mode_alphabet[get_bits(gb, 3)] = i;
  710. alphabet = custom_mode_alphabet;
  711. } else
  712. alphabet = ModeAlphabet[scheme - 1];
  713. /* iterate through all of the macroblocks that contain 1 or more
  714. * coded fragments */
  715. for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
  716. for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
  717. if (get_bits_left(gb) <= 0)
  718. return -1;
  719. for (j = 0; j < 4; j++) {
  720. int mb_x = 2 * sb_x + (j >> 1);
  721. int mb_y = 2 * sb_y + (((j >> 1) + j) & 1);
  722. current_macroblock = mb_y * s->macroblock_width + mb_x;
  723. if (mb_x >= s->macroblock_width ||
  724. mb_y >= s->macroblock_height)
  725. continue;
  726. /* coding modes are only stored if the macroblock has
  727. * at least one luma block coded, otherwise it must be
  728. * INTER_NO_MV */
  729. for (k = 0; k < 4; k++) {
  730. current_fragment = BLOCK_Y *
  731. s->fragment_width[0] + BLOCK_X;
  732. if (s->all_fragments[current_fragment].coding_method != MODE_COPY)
  733. break;
  734. }
  735. if (k == 4) {
  736. s->macroblock_coding[current_macroblock] = MODE_INTER_NO_MV;
  737. continue;
  738. }
  739. /* mode 7 means get 3 bits for each coding mode */
  740. if (scheme == 7)
  741. coding_mode = get_bits(gb, 3);
  742. else
  743. coding_mode = alphabet[get_vlc2(gb, s->mode_code_vlc.table, 3, 3)];
  744. s->macroblock_coding[current_macroblock] = coding_mode;
  745. for (k = 0; k < 4; k++) {
  746. frag = s->all_fragments + BLOCK_Y * s->fragment_width[0] + BLOCK_X;
  747. if (frag->coding_method != MODE_COPY)
  748. frag->coding_method = coding_mode;
  749. }
  750. #define SET_CHROMA_MODES \
  751. if (frag[s->fragment_start[1]].coding_method != MODE_COPY) \
  752. frag[s->fragment_start[1]].coding_method = coding_mode; \
  753. if (frag[s->fragment_start[2]].coding_method != MODE_COPY) \
  754. frag[s->fragment_start[2]].coding_method = coding_mode;
  755. if (s->chroma_y_shift) {
  756. frag = s->all_fragments + mb_y *
  757. s->fragment_width[1] + mb_x;
  758. SET_CHROMA_MODES
  759. } else if (s->chroma_x_shift) {
  760. frag = s->all_fragments +
  761. 2 * mb_y * s->fragment_width[1] + mb_x;
  762. for (k = 0; k < 2; k++) {
  763. SET_CHROMA_MODES
  764. frag += s->fragment_width[1];
  765. }
  766. } else {
  767. for (k = 0; k < 4; k++) {
  768. frag = s->all_fragments +
  769. BLOCK_Y * s->fragment_width[1] + BLOCK_X;
  770. SET_CHROMA_MODES
  771. }
  772. }
  773. }
  774. }
  775. }
  776. }
  777. return 0;
  778. }
  779. static int vp4_get_mv(Vp3DecodeContext *s, GetBitContext *gb, int axis, int last_motion)
  780. {
  781. int v = get_vlc2(gb, s->vp4_mv_vlc[axis][vp4_mv_table_selector[FFABS(last_motion)]].table, 6, 2) - 31;
  782. return last_motion < 0 ? -v : v;
  783. }
  784. /*
  785. * This function unpacks all the motion vectors for the individual
  786. * macroblocks from the bitstream.
  787. */
  788. static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
  789. {
  790. int j, k, sb_x, sb_y;
  791. int coding_mode;
  792. int motion_x[4];
  793. int motion_y[4];
  794. int last_motion_x = 0;
  795. int last_motion_y = 0;
  796. int prior_last_motion_x = 0;
  797. int prior_last_motion_y = 0;
  798. int last_gold_motion_x = 0;
  799. int last_gold_motion_y = 0;
  800. int current_macroblock;
  801. int current_fragment;
  802. int frag;
  803. if (s->keyframe)
  804. return 0;
  805. /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme; 2 is VP4 code scheme */
  806. coding_mode = s->version < 2 ? get_bits1(gb) : 2;
  807. /* iterate through all of the macroblocks that contain 1 or more
  808. * coded fragments */
  809. for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
  810. for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
  811. if (get_bits_left(gb) <= 0)
  812. return -1;
  813. for (j = 0; j < 4; j++) {
  814. int mb_x = 2 * sb_x + (j >> 1);
  815. int mb_y = 2 * sb_y + (((j >> 1) + j) & 1);
  816. current_macroblock = mb_y * s->macroblock_width + mb_x;
  817. if (mb_x >= s->macroblock_width ||
  818. mb_y >= s->macroblock_height ||
  819. s->macroblock_coding[current_macroblock] == MODE_COPY)
  820. continue;
  821. switch (s->macroblock_coding[current_macroblock]) {
  822. case MODE_GOLDEN_MV:
  823. if (coding_mode == 2) { /* VP4 */
  824. last_gold_motion_x = motion_x[0] = vp4_get_mv(s, gb, 0, last_gold_motion_x);
  825. last_gold_motion_y = motion_y[0] = vp4_get_mv(s, gb, 1, last_gold_motion_y);
  826. break;
  827. } /* otherwise fall through */
  828. case MODE_INTER_PLUS_MV:
  829. /* all 6 fragments use the same motion vector */
  830. if (coding_mode == 0) {
  831. motion_x[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
  832. motion_y[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
  833. } else if (coding_mode == 1) {
  834. motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)];
  835. motion_y[0] = fixed_motion_vector_table[get_bits(gb, 6)];
  836. } else { /* VP4 */
  837. motion_x[0] = vp4_get_mv(s, gb, 0, last_motion_x);
  838. motion_y[0] = vp4_get_mv(s, gb, 1, last_motion_y);
  839. }
  840. /* vector maintenance, only on MODE_INTER_PLUS_MV */
  841. if (s->macroblock_coding[current_macroblock] == MODE_INTER_PLUS_MV) {
  842. prior_last_motion_x = last_motion_x;
  843. prior_last_motion_y = last_motion_y;
  844. last_motion_x = motion_x[0];
  845. last_motion_y = motion_y[0];
  846. }
  847. break;
  848. case MODE_INTER_FOURMV:
  849. /* vector maintenance */
  850. prior_last_motion_x = last_motion_x;
  851. prior_last_motion_y = last_motion_y;
  852. /* fetch 4 vectors from the bitstream, one for each
  853. * Y fragment, then average for the C fragment vectors */
  854. for (k = 0; k < 4; k++) {
  855. current_fragment = BLOCK_Y * s->fragment_width[0] + BLOCK_X;
  856. if (s->all_fragments[current_fragment].coding_method != MODE_COPY) {
  857. if (coding_mode == 0) {
  858. motion_x[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
  859. motion_y[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
  860. } else if (coding_mode == 1) {
  861. motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)];
  862. motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)];
  863. } else { /* VP4 */
  864. motion_x[k] = vp4_get_mv(s, gb, 0, prior_last_motion_x);
  865. motion_y[k] = vp4_get_mv(s, gb, 1, prior_last_motion_y);
  866. }
  867. last_motion_x = motion_x[k];
  868. last_motion_y = motion_y[k];
  869. } else {
  870. motion_x[k] = 0;
  871. motion_y[k] = 0;
  872. }
  873. }
  874. break;
  875. case MODE_INTER_LAST_MV:
  876. /* all 6 fragments use the last motion vector */
  877. motion_x[0] = last_motion_x;
  878. motion_y[0] = last_motion_y;
  879. /* no vector maintenance (last vector remains the
  880. * last vector) */
  881. break;
  882. case MODE_INTER_PRIOR_LAST:
  883. /* all 6 fragments use the motion vector prior to the
  884. * last motion vector */
  885. motion_x[0] = prior_last_motion_x;
  886. motion_y[0] = prior_last_motion_y;
  887. /* vector maintenance */
  888. prior_last_motion_x = last_motion_x;
  889. prior_last_motion_y = last_motion_y;
  890. last_motion_x = motion_x[0];
  891. last_motion_y = motion_y[0];
  892. break;
  893. default:
  894. /* covers intra, inter without MV, golden without MV */
  895. motion_x[0] = 0;
  896. motion_y[0] = 0;
  897. /* no vector maintenance */
  898. break;
  899. }
  900. /* assign the motion vectors to the correct fragments */
  901. for (k = 0; k < 4; k++) {
  902. current_fragment =
  903. BLOCK_Y * s->fragment_width[0] + BLOCK_X;
  904. if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
  905. s->motion_val[0][current_fragment][0] = motion_x[k];
  906. s->motion_val[0][current_fragment][1] = motion_y[k];
  907. } else {
  908. s->motion_val[0][current_fragment][0] = motion_x[0];
  909. s->motion_val[0][current_fragment][1] = motion_y[0];
  910. }
  911. }
  912. if (s->chroma_y_shift) {
  913. if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
  914. motion_x[0] = RSHIFT(motion_x[0] + motion_x[1] +
  915. motion_x[2] + motion_x[3], 2);
  916. motion_y[0] = RSHIFT(motion_y[0] + motion_y[1] +
  917. motion_y[2] + motion_y[3], 2);
  918. }
  919. if (s->version <= 2) {
  920. motion_x[0] = (motion_x[0] >> 1) | (motion_x[0] & 1);
  921. motion_y[0] = (motion_y[0] >> 1) | (motion_y[0] & 1);
  922. }
  923. frag = mb_y * s->fragment_width[1] + mb_x;
  924. s->motion_val[1][frag][0] = motion_x[0];
  925. s->motion_val[1][frag][1] = motion_y[0];
  926. } else if (s->chroma_x_shift) {
  927. if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
  928. motion_x[0] = RSHIFT(motion_x[0] + motion_x[1], 1);
  929. motion_y[0] = RSHIFT(motion_y[0] + motion_y[1], 1);
  930. motion_x[1] = RSHIFT(motion_x[2] + motion_x[3], 1);
  931. motion_y[1] = RSHIFT(motion_y[2] + motion_y[3], 1);
  932. } else {
  933. motion_x[1] = motion_x[0];
  934. motion_y[1] = motion_y[0];
  935. }
  936. if (s->version <= 2) {
  937. motion_x[0] = (motion_x[0] >> 1) | (motion_x[0] & 1);
  938. motion_x[1] = (motion_x[1] >> 1) | (motion_x[1] & 1);
  939. }
  940. frag = 2 * mb_y * s->fragment_width[1] + mb_x;
  941. for (k = 0; k < 2; k++) {
  942. s->motion_val[1][frag][0] = motion_x[k];
  943. s->motion_val[1][frag][1] = motion_y[k];
  944. frag += s->fragment_width[1];
  945. }
  946. } else {
  947. for (k = 0; k < 4; k++) {
  948. frag = BLOCK_Y * s->fragment_width[1] + BLOCK_X;
  949. if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
  950. s->motion_val[1][frag][0] = motion_x[k];
  951. s->motion_val[1][frag][1] = motion_y[k];
  952. } else {
  953. s->motion_val[1][frag][0] = motion_x[0];
  954. s->motion_val[1][frag][1] = motion_y[0];
  955. }
  956. }
  957. }
  958. }
  959. }
  960. }
  961. return 0;
  962. }
  963. static int unpack_block_qpis(Vp3DecodeContext *s, GetBitContext *gb)
  964. {
  965. int qpi, i, j, bit, run_length, blocks_decoded, num_blocks_at_qpi;
  966. int num_blocks = s->total_num_coded_frags;
  967. for (qpi = 0; qpi < s->nqps - 1 && num_blocks > 0; qpi++) {
  968. i = blocks_decoded = num_blocks_at_qpi = 0;
  969. bit = get_bits1(gb) ^ 1;
  970. run_length = 0;
  971. do {
  972. if (run_length == MAXIMUM_LONG_BIT_RUN)
  973. bit = get_bits1(gb);
  974. else
  975. bit ^= 1;
  976. run_length = get_vlc2(gb, s->superblock_run_length_vlc.table, 6, 2) + 1;
  977. if (run_length == 34)
  978. run_length += get_bits(gb, 12);
  979. blocks_decoded += run_length;
  980. if (!bit)
  981. num_blocks_at_qpi += run_length;
  982. for (j = 0; j < run_length; i++) {
  983. if (i >= s->total_num_coded_frags)
  984. return -1;
  985. if (s->all_fragments[s->coded_fragment_list[0][i]].qpi == qpi) {
  986. s->all_fragments[s->coded_fragment_list[0][i]].qpi += bit;
  987. j++;
  988. }
  989. }
  990. } while (blocks_decoded < num_blocks && get_bits_left(gb) > 0);
  991. num_blocks -= num_blocks_at_qpi;
  992. }
  993. return 0;
  994. }
  995. static inline int get_eob_run(GetBitContext *gb, int token)
  996. {
  997. int v = eob_run_table[token].base;
  998. if (eob_run_table[token].bits)
  999. v += get_bits(gb, eob_run_table[token].bits);
  1000. return v;
  1001. }
  1002. static inline int get_coeff(GetBitContext *gb, int token, int16_t *coeff)
  1003. {
  1004. int bits_to_get, zero_run;
  1005. bits_to_get = coeff_get_bits[token];
  1006. if (bits_to_get)
  1007. bits_to_get = get_bits(gb, bits_to_get);
  1008. *coeff = coeff_tables[token][bits_to_get];
  1009. zero_run = zero_run_base[token];
  1010. if (zero_run_get_bits[token])
  1011. zero_run += get_bits(gb, zero_run_get_bits[token]);
  1012. return zero_run;
  1013. }
  1014. /*
  1015. * This function is called by unpack_dct_coeffs() to extract the VLCs from
  1016. * the bitstream. The VLCs encode tokens which are used to unpack DCT
  1017. * data. This function unpacks all the VLCs for either the Y plane or both
  1018. * C planes, and is called for DC coefficients or different AC coefficient
  1019. * levels (since different coefficient types require different VLC tables.
  1020. *
  1021. * This function returns a residual eob run. E.g, if a particular token gave
  1022. * instructions to EOB the next 5 fragments and there were only 2 fragments
  1023. * left in the current fragment range, 3 would be returned so that it could
  1024. * be passed into the next call to this same function.
  1025. */
  1026. static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
  1027. VLC *table, int coeff_index,
  1028. int plane,
  1029. int eob_run)
  1030. {
  1031. int i, j = 0;
  1032. int token;
  1033. int zero_run = 0;
  1034. int16_t coeff = 0;
  1035. int blocks_ended;
  1036. int coeff_i = 0;
  1037. int num_coeffs = s->num_coded_frags[plane][coeff_index];
  1038. int16_t *dct_tokens = s->dct_tokens[plane][coeff_index];
  1039. /* local references to structure members to avoid repeated dereferences */
  1040. int *coded_fragment_list = s->coded_fragment_list[plane];
  1041. Vp3Fragment *all_fragments = s->all_fragments;
  1042. VLC_TYPE(*vlc_table)[2] = table->table;
  1043. if (num_coeffs < 0) {
  1044. av_log(s->avctx, AV_LOG_ERROR,
  1045. "Invalid number of coefficients at level %d\n", coeff_index);
  1046. return AVERROR_INVALIDDATA;
  1047. }
  1048. if (eob_run > num_coeffs) {
  1049. coeff_i =
  1050. blocks_ended = num_coeffs;
  1051. eob_run -= num_coeffs;
  1052. } else {
  1053. coeff_i =
  1054. blocks_ended = eob_run;
  1055. eob_run = 0;
  1056. }
  1057. // insert fake EOB token to cover the split between planes or zzi
  1058. if (blocks_ended)
  1059. dct_tokens[j++] = blocks_ended << 2;
  1060. while (coeff_i < num_coeffs && get_bits_left(gb) > 0) {
  1061. /* decode a VLC into a token */
  1062. token = get_vlc2(gb, vlc_table, 11, 3);
  1063. /* use the token to get a zero run, a coefficient, and an eob run */
  1064. if ((unsigned) token <= 6U) {
  1065. eob_run = get_eob_run(gb, token);
  1066. if (!eob_run)
  1067. eob_run = INT_MAX;
  1068. // record only the number of blocks ended in this plane,
  1069. // any spill will be recorded in the next plane.
  1070. if (eob_run > num_coeffs - coeff_i) {
  1071. dct_tokens[j++] = TOKEN_EOB(num_coeffs - coeff_i);
  1072. blocks_ended += num_coeffs - coeff_i;
  1073. eob_run -= num_coeffs - coeff_i;
  1074. coeff_i = num_coeffs;
  1075. } else {
  1076. dct_tokens[j++] = TOKEN_EOB(eob_run);
  1077. blocks_ended += eob_run;
  1078. coeff_i += eob_run;
  1079. eob_run = 0;
  1080. }
  1081. } else if (token >= 0) {
  1082. zero_run = get_coeff(gb, token, &coeff);
  1083. if (zero_run) {
  1084. dct_tokens[j++] = TOKEN_ZERO_RUN(coeff, zero_run);
  1085. } else {
  1086. // Save DC into the fragment structure. DC prediction is
  1087. // done in raster order, so the actual DC can't be in with
  1088. // other tokens. We still need the token in dct_tokens[]
  1089. // however, or else the structure collapses on itself.
  1090. if (!coeff_index)
  1091. all_fragments[coded_fragment_list[coeff_i]].dc = coeff;
  1092. dct_tokens[j++] = TOKEN_COEFF(coeff);
  1093. }
  1094. if (coeff_index + zero_run > 64) {
  1095. av_log(s->avctx, AV_LOG_DEBUG,
  1096. "Invalid zero run of %d with %d coeffs left\n",
  1097. zero_run, 64 - coeff_index);
  1098. zero_run = 64 - coeff_index;
  1099. }
  1100. // zero runs code multiple coefficients,
  1101. // so don't try to decode coeffs for those higher levels
  1102. for (i = coeff_index + 1; i <= coeff_index + zero_run; i++)
  1103. s->num_coded_frags[plane][i]--;
  1104. coeff_i++;
  1105. } else {
  1106. av_log(s->avctx, AV_LOG_ERROR, "Invalid token %d\n", token);
  1107. return -1;
  1108. }
  1109. }
  1110. if (blocks_ended > s->num_coded_frags[plane][coeff_index])
  1111. av_log(s->avctx, AV_LOG_ERROR, "More blocks ended than coded!\n");
  1112. // decrement the number of blocks that have higher coefficients for each
  1113. // EOB run at this level
  1114. if (blocks_ended)
  1115. for (i = coeff_index + 1; i < 64; i++)
  1116. s->num_coded_frags[plane][i] -= blocks_ended;
  1117. // setup the next buffer
  1118. if (plane < 2)
  1119. s->dct_tokens[plane + 1][coeff_index] = dct_tokens + j;
  1120. else if (coeff_index < 63)
  1121. s->dct_tokens[0][coeff_index + 1] = dct_tokens + j;
  1122. return eob_run;
  1123. }
  1124. static void reverse_dc_prediction(Vp3DecodeContext *s,
  1125. int first_fragment,
  1126. int fragment_width,
  1127. int fragment_height);
  1128. /*
  1129. * This function unpacks all of the DCT coefficient data from the
  1130. * bitstream.
  1131. */
  1132. static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
  1133. {
  1134. int i;
  1135. int dc_y_table;
  1136. int dc_c_table;
  1137. int ac_y_table;
  1138. int ac_c_table;
  1139. int residual_eob_run = 0;
  1140. VLC *y_tables[64];
  1141. VLC *c_tables[64];
  1142. s->dct_tokens[0][0] = s->dct_tokens_base;
  1143. if (get_bits_left(gb) < 16)
  1144. return AVERROR_INVALIDDATA;
  1145. /* fetch the DC table indexes */
  1146. dc_y_table = get_bits(gb, 4);
  1147. dc_c_table = get_bits(gb, 4);
  1148. /* unpack the Y plane DC coefficients */
  1149. residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
  1150. 0, residual_eob_run);
  1151. if (residual_eob_run < 0)
  1152. return residual_eob_run;
  1153. if (get_bits_left(gb) < 8)
  1154. return AVERROR_INVALIDDATA;
  1155. /* reverse prediction of the Y-plane DC coefficients */
  1156. reverse_dc_prediction(s, 0, s->fragment_width[0], s->fragment_height[0]);
  1157. /* unpack the C plane DC coefficients */
  1158. residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
  1159. 1, residual_eob_run);
  1160. if (residual_eob_run < 0)
  1161. return residual_eob_run;
  1162. residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
  1163. 2, residual_eob_run);
  1164. if (residual_eob_run < 0)
  1165. return residual_eob_run;
  1166. /* reverse prediction of the C-plane DC coefficients */
  1167. if (!(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  1168. reverse_dc_prediction(s, s->fragment_start[1],
  1169. s->fragment_width[1], s->fragment_height[1]);
  1170. reverse_dc_prediction(s, s->fragment_start[2],
  1171. s->fragment_width[1], s->fragment_height[1]);
  1172. }
  1173. if (get_bits_left(gb) < 8)
  1174. return AVERROR_INVALIDDATA;
  1175. /* fetch the AC table indexes */
  1176. ac_y_table = get_bits(gb, 4);
  1177. ac_c_table = get_bits(gb, 4);
  1178. /* build tables of AC VLC tables */
  1179. for (i = 1; i <= 5; i++) {
  1180. y_tables[i] = &s->ac_vlc_1[ac_y_table];
  1181. c_tables[i] = &s->ac_vlc_1[ac_c_table];
  1182. }
  1183. for (i = 6; i <= 14; i++) {
  1184. y_tables[i] = &s->ac_vlc_2[ac_y_table];
  1185. c_tables[i] = &s->ac_vlc_2[ac_c_table];
  1186. }
  1187. for (i = 15; i <= 27; i++) {
  1188. y_tables[