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.

2302 lines
81KB

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