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.

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