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.

2164 lines
74KB

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