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.

2256 lines
78KB

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