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.

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