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.

2255 lines
77KB

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