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.

2216 lines
76KB

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