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.

2376 lines
83KB

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