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.

2396 lines
83KB

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