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.

2250 lines
76KB

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