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.

2240 lines
77KB

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