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.

2295 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 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[get_bits(gb, 3)] = i;
  629. }
  630. /* iterate through all of the macroblocks that contain 1 or more
  631. * coded fragments */
  632. for (i = 0; i < s->u_superblock_start; i++) {
  633. for (j = 0; j < 4; j++) {
  634. current_macroblock = s->superblock_macroblocks[i * 4 + j];
  635. if ((current_macroblock == -1) ||
  636. (s->macroblock_coding[current_macroblock] == MODE_COPY))
  637. continue;
  638. if (current_macroblock >= s->macroblock_count) {
  639. av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_modes(): bad macroblock number (%d >= %d)\n",
  640. current_macroblock, s->macroblock_count);
  641. return 1;
  642. }
  643. /* mode 7 means get 3 bits for each coding mode */
  644. if (scheme == 7)
  645. coding_mode = get_bits(gb, 3);
  646. else if(scheme == 0)
  647. coding_mode = custom_mode_alphabet
  648. [get_vlc2(gb, s->mode_code_vlc.table, 3, 3)];
  649. else
  650. coding_mode = ModeAlphabet[scheme-1]
  651. [get_vlc2(gb, s->mode_code_vlc.table, 3, 3)];
  652. s->macroblock_coding[current_macroblock] = coding_mode;
  653. for (k = 0; k < 6; k++) {
  654. current_fragment =
  655. s->macroblock_fragments[current_macroblock * 6 + k];
  656. if (current_fragment == -1)
  657. continue;
  658. if (current_fragment >= s->fragment_count) {
  659. av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_modes(): bad fragment number (%d >= %d)\n",
  660. current_fragment, s->fragment_count);
  661. return 1;
  662. }
  663. if (s->all_fragments[current_fragment].coding_method !=
  664. MODE_COPY)
  665. s->all_fragments[current_fragment].coding_method =
  666. coding_mode;
  667. }
  668. }
  669. }
  670. }
  671. return 0;
  672. }
  673. /*
  674. * This function unpacks all the motion vectors for the individual
  675. * macroblocks from the bitstream.
  676. */
  677. static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
  678. {
  679. int i, j, k, l;
  680. int coding_mode;
  681. int motion_x[6];
  682. int motion_y[6];
  683. int last_motion_x = 0;
  684. int last_motion_y = 0;
  685. int prior_last_motion_x = 0;
  686. int prior_last_motion_y = 0;
  687. int current_macroblock;
  688. int current_fragment;
  689. if (s->keyframe)
  690. return 0;
  691. memset(motion_x, 0, 6 * sizeof(int));
  692. memset(motion_y, 0, 6 * sizeof(int));
  693. /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme */
  694. coding_mode = get_bits1(gb);
  695. /* iterate through all of the macroblocks that contain 1 or more
  696. * coded fragments */
  697. for (i = 0; i < s->u_superblock_start; i++) {
  698. for (j = 0; j < 4; j++) {
  699. current_macroblock = s->superblock_macroblocks[i * 4 + j];
  700. if ((current_macroblock == -1) ||
  701. (s->macroblock_coding[current_macroblock] == MODE_COPY))
  702. continue;
  703. if (current_macroblock >= s->macroblock_count) {
  704. av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vectors(): bad macroblock number (%d >= %d)\n",
  705. current_macroblock, s->macroblock_count);
  706. return 1;
  707. }
  708. current_fragment = s->macroblock_fragments[current_macroblock * 6];
  709. if (current_fragment >= s->fragment_count) {
  710. av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vectors(): bad fragment number (%d >= %d\n",
  711. current_fragment, s->fragment_count);
  712. return 1;
  713. }
  714. switch (s->macroblock_coding[current_macroblock]) {
  715. case MODE_INTER_PLUS_MV:
  716. case MODE_GOLDEN_MV:
  717. /* all 6 fragments use the same motion vector */
  718. if (coding_mode == 0) {
  719. motion_x[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
  720. motion_y[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
  721. } else {
  722. motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)];
  723. motion_y[0] = fixed_motion_vector_table[get_bits(gb, 6)];
  724. }
  725. for (k = 1; k < 6; k++) {
  726. motion_x[k] = motion_x[0];
  727. motion_y[k] = motion_y[0];
  728. }
  729. /* vector maintenance, only on MODE_INTER_PLUS_MV */
  730. if (s->macroblock_coding[current_macroblock] ==
  731. MODE_INTER_PLUS_MV) {
  732. prior_last_motion_x = last_motion_x;
  733. prior_last_motion_y = last_motion_y;
  734. last_motion_x = motion_x[0];
  735. last_motion_y = motion_y[0];
  736. }
  737. break;
  738. case MODE_INTER_FOURMV:
  739. /* vector maintenance */
  740. prior_last_motion_x = last_motion_x;
  741. prior_last_motion_y = last_motion_y;
  742. /* fetch 4 vectors from the bitstream, one for each
  743. * Y fragment, then average for the C fragment vectors */
  744. motion_x[4] = motion_y[4] = 0;
  745. for (k = 0; k < 4; k++) {
  746. for (l = 0; l < s->coded_fragment_list_index; l++)
  747. if (s->coded_fragment_list[l] == s->macroblock_fragments[6*current_macroblock + k])
  748. break;
  749. if (l < s->coded_fragment_list_index) {
  750. if (coding_mode == 0) {
  751. motion_x[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
  752. motion_y[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
  753. } else {
  754. motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)];
  755. motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)];
  756. }
  757. last_motion_x = motion_x[k];
  758. last_motion_y = motion_y[k];
  759. } else {
  760. motion_x[k] = 0;
  761. motion_y[k] = 0;
  762. }
  763. motion_x[4] += motion_x[k];
  764. motion_y[4] += motion_y[k];
  765. }
  766. motion_x[5]=
  767. motion_x[4]= RSHIFT(motion_x[4], 2);
  768. motion_y[5]=
  769. motion_y[4]= RSHIFT(motion_y[4], 2);
  770. break;
  771. case MODE_INTER_LAST_MV:
  772. /* all 6 fragments use the last motion vector */
  773. motion_x[0] = last_motion_x;
  774. motion_y[0] = last_motion_y;
  775. for (k = 1; k < 6; k++) {
  776. motion_x[k] = motion_x[0];
  777. motion_y[k] = motion_y[0];
  778. }
  779. /* no vector maintenance (last vector remains the
  780. * last vector) */
  781. break;
  782. case MODE_INTER_PRIOR_LAST:
  783. /* all 6 fragments use the motion vector prior to the
  784. * last motion vector */
  785. motion_x[0] = prior_last_motion_x;
  786. motion_y[0] = prior_last_motion_y;
  787. for (k = 1; k < 6; k++) {
  788. motion_x[k] = motion_x[0];
  789. motion_y[k] = motion_y[0];
  790. }
  791. /* vector maintenance */
  792. prior_last_motion_x = last_motion_x;
  793. prior_last_motion_y = last_motion_y;
  794. last_motion_x = motion_x[0];
  795. last_motion_y = motion_y[0];
  796. break;
  797. default:
  798. /* covers intra, inter without MV, golden without MV */
  799. memset(motion_x, 0, 6 * sizeof(int));
  800. memset(motion_y, 0, 6 * sizeof(int));
  801. /* no vector maintenance */
  802. break;
  803. }
  804. /* assign the motion vectors to the correct fragments */
  805. for (k = 0; k < 6; k++) {
  806. current_fragment =
  807. s->macroblock_fragments[current_macroblock * 6 + k];
  808. if (current_fragment == -1)
  809. continue;
  810. if (current_fragment >= s->fragment_count) {
  811. av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vectors(): bad fragment number (%d >= %d)\n",
  812. current_fragment, s->fragment_count);
  813. return 1;
  814. }
  815. s->all_fragments[current_fragment].motion_x = motion_x[k];
  816. s->all_fragments[current_fragment].motion_y = motion_y[k];
  817. }
  818. }
  819. }
  820. return 0;
  821. }
  822. /*
  823. * This function is called by unpack_dct_coeffs() to extract the VLCs from
  824. * the bitstream. The VLCs encode tokens which are used to unpack DCT
  825. * data. This function unpacks all the VLCs for either the Y plane or both
  826. * C planes, and is called for DC coefficients or different AC coefficient
  827. * levels (since different coefficient types require different VLC tables.
  828. *
  829. * This function returns a residual eob run. E.g, if a particular token gave
  830. * instructions to EOB the next 5 fragments and there were only 2 fragments
  831. * left in the current fragment range, 3 would be returned so that it could
  832. * be passed into the next call to this same function.
  833. */
  834. static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
  835. VLC *table, int coeff_index,
  836. int first_fragment, int last_fragment,
  837. int eob_run)
  838. {
  839. int i;
  840. int token;
  841. int zero_run = 0;
  842. DCTELEM coeff = 0;
  843. Vp3Fragment *fragment;
  844. uint8_t *perm= s->scantable.permutated;
  845. int bits_to_get;
  846. if ((first_fragment >= s->fragment_count) ||
  847. (last_fragment >= s->fragment_count)) {
  848. av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vlcs(): bad fragment number (%d -> %d ?)\n",
  849. first_fragment, last_fragment);
  850. return 0;
  851. }
  852. for (i = first_fragment; i <= last_fragment; i++) {
  853. int fragment_num = s->coded_fragment_list[i];
  854. if (s->coeff_counts[fragment_num] > coeff_index)
  855. continue;
  856. fragment = &s->all_fragments[fragment_num];
  857. if (!eob_run) {
  858. /* decode a VLC into a token */
  859. token = get_vlc2(gb, table->table, 5, 3);
  860. /* use the token to get a zero run, a coefficient, and an eob run */
  861. if (token <= 6) {
  862. eob_run = eob_run_base[token];
  863. if (eob_run_get_bits[token])
  864. eob_run += get_bits(gb, eob_run_get_bits[token]);
  865. coeff = zero_run = 0;
  866. } else {
  867. bits_to_get = coeff_get_bits[token];
  868. if (!bits_to_get)
  869. coeff = coeff_tables[token][0];
  870. else
  871. coeff = coeff_tables[token][get_bits(gb, bits_to_get)];
  872. zero_run = zero_run_base[token];
  873. if (zero_run_get_bits[token])
  874. zero_run += get_bits(gb, zero_run_get_bits[token]);
  875. }
  876. }
  877. if (!eob_run) {
  878. s->coeff_counts[fragment_num] += zero_run;
  879. if (s->coeff_counts[fragment_num] < 64){
  880. fragment->next_coeff->coeff= coeff;
  881. fragment->next_coeff->index= perm[s->coeff_counts[fragment_num]++]; //FIXME perm here already?
  882. fragment->next_coeff->next= s->next_coeff;
  883. s->next_coeff->next=NULL;
  884. fragment->next_coeff= s->next_coeff++;
  885. }
  886. } else {
  887. s->coeff_counts[fragment_num] |= 128;
  888. eob_run--;
  889. }
  890. }
  891. return eob_run;
  892. }
  893. /*
  894. * This function unpacks all of the DCT coefficient data from the
  895. * bitstream.
  896. */
  897. static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
  898. {
  899. int i;
  900. int dc_y_table;
  901. int dc_c_table;
  902. int ac_y_table;
  903. int ac_c_table;
  904. int residual_eob_run = 0;
  905. /* fetch the DC table indexes */
  906. dc_y_table = get_bits(gb, 4);
  907. dc_c_table = get_bits(gb, 4);
  908. /* unpack the Y plane DC coefficients */
  909. residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
  910. s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
  911. /* unpack the C plane DC coefficients */
  912. residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
  913. s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
  914. /* fetch the AC table indexes */
  915. ac_y_table = get_bits(gb, 4);
  916. ac_c_table = get_bits(gb, 4);
  917. /* unpack the group 1 AC coefficients (coeffs 1-5) */
  918. for (i = 1; i <= 5; i++) {
  919. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_y_table], i,
  920. s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
  921. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_c_table], i,
  922. s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
  923. }
  924. /* unpack the group 2 AC coefficients (coeffs 6-14) */
  925. for (i = 6; i <= 14; i++) {
  926. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_y_table], i,
  927. s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
  928. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_c_table], i,
  929. s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
  930. }
  931. /* unpack the group 3 AC coefficients (coeffs 15-27) */
  932. for (i = 15; i <= 27; i++) {
  933. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_y_table], i,
  934. s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
  935. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_c_table], i,
  936. s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
  937. }
  938. /* unpack the group 4 AC coefficients (coeffs 28-63) */
  939. for (i = 28; i <= 63; i++) {
  940. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_y_table], i,
  941. s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
  942. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_c_table], i,
  943. s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
  944. }
  945. return 0;
  946. }
  947. /*
  948. * This function reverses the DC prediction for each coded fragment in
  949. * the frame. Much of this function is adapted directly from the original
  950. * VP3 source code.
  951. */
  952. #define COMPATIBLE_FRAME(x) \
  953. (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
  954. #define FRAME_CODED(x) (s->all_fragments[x].coding_method != MODE_COPY)
  955. #define DC_COEFF(u) (s->coeffs[u].index ? 0 : s->coeffs[u].coeff) //FIXME do somethin to simplify this
  956. static void reverse_dc_prediction(Vp3DecodeContext *s,
  957. int first_fragment,
  958. int fragment_width,
  959. int fragment_height)
  960. {
  961. #define PUL 8
  962. #define PU 4
  963. #define PUR 2
  964. #define PL 1
  965. int x, y;
  966. int i = first_fragment;
  967. int predicted_dc;
  968. /* DC values for the left, up-left, up, and up-right fragments */
  969. int vl, vul, vu, vur;
  970. /* indexes for the left, up-left, up, and up-right fragments */
  971. int l, ul, u, ur;
  972. /*
  973. * The 6 fields mean:
  974. * 0: up-left multiplier
  975. * 1: up multiplier
  976. * 2: up-right multiplier
  977. * 3: left multiplier
  978. */
  979. int predictor_transform[16][4] = {
  980. { 0, 0, 0, 0},
  981. { 0, 0, 0,128}, // PL
  982. { 0, 0,128, 0}, // PUR
  983. { 0, 0, 53, 75}, // PUR|PL
  984. { 0,128, 0, 0}, // PU
  985. { 0, 64, 0, 64}, // PU|PL
  986. { 0,128, 0, 0}, // PU|PUR
  987. { 0, 0, 53, 75}, // PU|PUR|PL
  988. {128, 0, 0, 0}, // PUL
  989. { 0, 0, 0,128}, // PUL|PL
  990. { 64, 0, 64, 0}, // PUL|PUR
  991. { 0, 0, 53, 75}, // PUL|PUR|PL
  992. { 0,128, 0, 0}, // PUL|PU
  993. {-104,116, 0,116}, // PUL|PU|PL
  994. { 24, 80, 24, 0}, // PUL|PU|PUR
  995. {-104,116, 0,116} // PUL|PU|PUR|PL
  996. };
  997. /* This table shows which types of blocks can use other blocks for
  998. * prediction. For example, INTRA is the only mode in this table to
  999. * have a frame number of 0. That means INTRA blocks can only predict
  1000. * from other INTRA blocks. There are 2 golden frame coding types;
  1001. * blocks encoding in these modes can only predict from other blocks
  1002. * that were encoded with these 1 of these 2 modes. */
  1003. unsigned char compatible_frame[8] = {
  1004. 1, /* MODE_INTER_NO_MV */
  1005. 0, /* MODE_INTRA */
  1006. 1, /* MODE_INTER_PLUS_MV */
  1007. 1, /* MODE_INTER_LAST_MV */
  1008. 1, /* MODE_INTER_PRIOR_MV */
  1009. 2, /* MODE_USING_GOLDEN */
  1010. 2, /* MODE_GOLDEN_MV */
  1011. 1 /* MODE_INTER_FOUR_MV */
  1012. };
  1013. int current_frame_type;
  1014. /* there is a last DC predictor for each of the 3 frame types */
  1015. short last_dc[3];
  1016. int transform = 0;
  1017. vul = vu = vur = vl = 0;
  1018. last_dc[0] = last_dc[1] = last_dc[2] = 0;
  1019. /* for each fragment row... */
  1020. for (y = 0; y < fragment_height; y++) {
  1021. /* for each fragment in a row... */
  1022. for (x = 0; x < fragment_width; x++, i++) {
  1023. /* reverse prediction if this block was coded */
  1024. if (s->all_fragments[i].coding_method != MODE_COPY) {
  1025. current_frame_type =
  1026. compatible_frame[s->all_fragments[i].coding_method];
  1027. transform= 0;
  1028. if(x){
  1029. l= i-1;
  1030. vl = DC_COEFF(l);
  1031. if(FRAME_CODED(l) && COMPATIBLE_FRAME(l))
  1032. transform |= PL;
  1033. }
  1034. if(y){
  1035. u= i-fragment_width;
  1036. vu = DC_COEFF(u);
  1037. if(FRAME_CODED(u) && COMPATIBLE_FRAME(u))
  1038. transform |= PU;
  1039. if(x){
  1040. ul= i-fragment_width-1;
  1041. vul = DC_COEFF(ul);
  1042. if(FRAME_CODED(ul) && COMPATIBLE_FRAME(ul))
  1043. transform |= PUL;
  1044. }
  1045. if(x + 1 < fragment_width){
  1046. ur= i-fragment_width+1;
  1047. vur = DC_COEFF(ur);
  1048. if(FRAME_CODED(ur) && COMPATIBLE_FRAME(ur))
  1049. transform |= PUR;
  1050. }
  1051. }
  1052. if (transform == 0) {
  1053. /* if there were no fragments to predict from, use last
  1054. * DC saved */
  1055. predicted_dc = last_dc[current_frame_type];
  1056. } else {
  1057. /* apply the appropriate predictor transform */
  1058. predicted_dc =
  1059. (predictor_transform[transform][0] * vul) +
  1060. (predictor_transform[transform][1] * vu) +
  1061. (predictor_transform[transform][2] * vur) +
  1062. (predictor_transform[transform][3] * vl);
  1063. predicted_dc /= 128;
  1064. /* check for outranging on the [ul u l] and
  1065. * [ul u ur l] predictors */
  1066. if ((transform == 13) || (transform == 15)) {
  1067. if (FFABS(predicted_dc - vu) > 128)
  1068. predicted_dc = vu;
  1069. else if (FFABS(predicted_dc - vl) > 128)
  1070. predicted_dc = vl;
  1071. else if (FFABS(predicted_dc - vul) > 128)
  1072. predicted_dc = vul;
  1073. }
  1074. }
  1075. /* at long last, apply the predictor */
  1076. if(s->coeffs[i].index){
  1077. *s->next_coeff= s->coeffs[i];
  1078. s->coeffs[i].index=0;
  1079. s->coeffs[i].coeff=0;
  1080. s->coeffs[i].next= s->next_coeff++;
  1081. }
  1082. s->coeffs[i].coeff += predicted_dc;
  1083. /* save the DC */
  1084. last_dc[current_frame_type] = DC_COEFF(i);
  1085. if(DC_COEFF(i) && !(s->coeff_counts[i]&127)){
  1086. s->coeff_counts[i]= 129;
  1087. // s->all_fragments[i].next_coeff= s->next_coeff;
  1088. s->coeffs[i].next= s->next_coeff;
  1089. (s->next_coeff++)->next=NULL;
  1090. }
  1091. }
  1092. }
  1093. }
  1094. }
  1095. /*
  1096. * Perform the final rendering for a particular slice of data.
  1097. * The slice number ranges from 0..(macroblock_height - 1).
  1098. */
  1099. static void render_slice(Vp3DecodeContext *s, int slice)
  1100. {
  1101. int x;
  1102. int16_t *dequantizer;
  1103. DECLARE_ALIGNED_16(DCTELEM, block[64]);
  1104. int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
  1105. int motion_halfpel_index;
  1106. uint8_t *motion_source;
  1107. int plane;
  1108. int current_macroblock_entry = slice * s->macroblock_width * 6;
  1109. if (slice >= s->macroblock_height)
  1110. return;
  1111. for (plane = 0; plane < 3; plane++) {
  1112. uint8_t *output_plane = s->current_frame.data [plane];
  1113. uint8_t * last_plane = s-> last_frame.data [plane];
  1114. uint8_t *golden_plane = s-> golden_frame.data [plane];
  1115. int stride = s->current_frame.linesize[plane];
  1116. int plane_width = s->width >> !!plane;
  1117. int plane_height = s->height >> !!plane;
  1118. int y = slice * FRAGMENT_PIXELS << !plane ;
  1119. int slice_height = y + (FRAGMENT_PIXELS << !plane);
  1120. int i = s->macroblock_fragments[current_macroblock_entry + plane + 3*!!plane];
  1121. if (!s->flipped_image) stride = -stride;
  1122. if(FFABS(stride) > 2048)
  1123. return; //various tables are fixed size
  1124. /* for each fragment row in the slice (both of them)... */
  1125. for (; y < slice_height; y += 8) {
  1126. /* for each fragment in a row... */
  1127. for (x = 0; x < plane_width; x += 8, i++) {
  1128. if ((i < 0) || (i >= s->fragment_count)) {
  1129. av_log(s->avctx, AV_LOG_ERROR, " vp3:render_slice(): bad fragment number (%d)\n", i);
  1130. return;
  1131. }
  1132. /* transform if this block was coded */
  1133. if ((s->all_fragments[i].coding_method != MODE_COPY) &&
  1134. !((s->avctx->flags & CODEC_FLAG_GRAY) && plane)) {
  1135. if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
  1136. (s->all_fragments[i].coding_method == MODE_GOLDEN_MV))
  1137. motion_source= golden_plane;
  1138. else
  1139. motion_source= last_plane;
  1140. motion_source += s->all_fragments[i].first_pixel;
  1141. motion_halfpel_index = 0;
  1142. /* sort out the motion vector if this fragment is coded
  1143. * using a motion vector method */
  1144. if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
  1145. (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) {
  1146. int src_x, src_y;
  1147. motion_x = s->all_fragments[i].motion_x;
  1148. motion_y = s->all_fragments[i].motion_y;
  1149. if(plane){
  1150. motion_x= (motion_x>>1) | (motion_x&1);
  1151. motion_y= (motion_y>>1) | (motion_y&1);
  1152. }
  1153. src_x= (motion_x>>1) + x;
  1154. src_y= (motion_y>>1) + y;
  1155. if ((motion_x == 127) || (motion_y == 127))
  1156. av_log(s->avctx, AV_LOG_ERROR, " help! got invalid motion vector! (%X, %X)\n", motion_x, motion_y);
  1157. motion_halfpel_index = motion_x & 0x01;
  1158. motion_source += (motion_x >> 1);
  1159. motion_halfpel_index |= (motion_y & 0x01) << 1;
  1160. motion_source += ((motion_y >> 1) * stride);
  1161. if(src_x<0 || src_y<0 || src_x + 9 >= plane_width || src_y + 9 >= plane_height){
  1162. uint8_t *temp= s->edge_emu_buffer;
  1163. if(stride<0) temp -= 9*stride;
  1164. else temp += 9*stride;
  1165. ff_emulated_edge_mc(temp, motion_source, stride, 9, 9, src_x, src_y, plane_width, plane_height);
  1166. motion_source= temp;
  1167. }
  1168. }
  1169. /* first, take care of copying a block from either the
  1170. * previous or the golden frame */
  1171. if (s->all_fragments[i].coding_method != MODE_INTRA) {
  1172. /* Note, it is possible to implement all MC cases with
  1173. put_no_rnd_pixels_l2 which would look more like the
  1174. VP3 source but this would be slower as
  1175. put_no_rnd_pixels_tab is better optimzed */
  1176. if(motion_halfpel_index != 3){
  1177. s->dsp.put_no_rnd_pixels_tab[1][motion_halfpel_index](
  1178. output_plane + s->all_fragments[i].first_pixel,
  1179. motion_source, stride, 8);
  1180. }else{
  1181. int d= (motion_x ^ motion_y)>>31; // d is 0 if motion_x and _y have the same sign, else -1
  1182. s->dsp.put_no_rnd_pixels_l2[1](
  1183. output_plane + s->all_fragments[i].first_pixel,
  1184. motion_source - d,
  1185. motion_source + stride + 1 + d,
  1186. stride, 8);
  1187. }
  1188. dequantizer = s->qmat[1][plane];
  1189. }else{
  1190. dequantizer = s->qmat[0][plane];
  1191. }
  1192. /* dequantize the DCT coefficients */
  1193. if(s->avctx->idct_algo==FF_IDCT_VP3){
  1194. Coeff *coeff= s->coeffs + i;
  1195. s->dsp.clear_block(block);
  1196. while(coeff->next){
  1197. block[coeff->index]= coeff->coeff * dequantizer[coeff->index];
  1198. coeff= coeff->next;
  1199. }
  1200. }else{
  1201. Coeff *coeff= s->coeffs + i;
  1202. s->dsp.clear_block(block);
  1203. while(coeff->next){
  1204. block[coeff->index]= (coeff->coeff * dequantizer[coeff->index] + 2)>>2;
  1205. coeff= coeff->next;
  1206. }
  1207. }
  1208. /* invert DCT and place (or add) in final output */
  1209. if (s->all_fragments[i].coding_method == MODE_INTRA) {
  1210. if(s->avctx->idct_algo!=FF_IDCT_VP3)
  1211. block[0] += 128<<3;
  1212. s->dsp.idct_put(
  1213. output_plane + s->all_fragments[i].first_pixel,
  1214. stride,
  1215. block);
  1216. } else {
  1217. s->dsp.idct_add(
  1218. output_plane + s->all_fragments[i].first_pixel,
  1219. stride,
  1220. block);
  1221. }
  1222. } else {
  1223. /* copy directly from the previous frame */
  1224. s->dsp.put_pixels_tab[1][0](
  1225. output_plane + s->all_fragments[i].first_pixel,
  1226. last_plane + s->all_fragments[i].first_pixel,
  1227. stride, 8);
  1228. }
  1229. #if 0
  1230. /* perform the left edge filter if:
  1231. * - the fragment is not on the left column
  1232. * - the fragment is coded in this frame
  1233. * - the fragment is not coded in this frame but the left
  1234. * fragment is coded in this frame (this is done instead
  1235. * of a right edge filter when rendering the left fragment
  1236. * since this fragment is not available yet) */
  1237. if ((x > 0) &&
  1238. ((s->all_fragments[i].coding_method != MODE_COPY) ||
  1239. ((s->all_fragments[i].coding_method == MODE_COPY) &&
  1240. (s->all_fragments[i - 1].coding_method != MODE_COPY)) )) {
  1241. horizontal_filter(
  1242. output_plane + s->all_fragments[i].first_pixel + 7*stride,
  1243. -stride, s->bounding_values_array + 127);
  1244. }
  1245. /* perform the top edge filter if:
  1246. * - the fragment is not on the top row
  1247. * - the fragment is coded in this frame
  1248. * - the fragment is not coded in this frame but the above
  1249. * fragment is coded in this frame (this is done instead
  1250. * of a bottom edge filter when rendering the above
  1251. * fragment since this fragment is not available yet) */
  1252. if ((y > 0) &&
  1253. ((s->all_fragments[i].coding_method != MODE_COPY) ||
  1254. ((s->all_fragments[i].coding_method == MODE_COPY) &&
  1255. (s->all_fragments[i - fragment_width].coding_method != MODE_COPY)) )) {
  1256. vertical_filter(
  1257. output_plane + s->all_fragments[i].first_pixel - stride,
  1258. -stride, s->bounding_values_array + 127);
  1259. }
  1260. #endif
  1261. }
  1262. }
  1263. }
  1264. /* this looks like a good place for slice dispatch... */
  1265. /* algorithm:
  1266. * if (slice == s->macroblock_height - 1)
  1267. * dispatch (both last slice & 2nd-to-last slice);
  1268. * else if (slice > 0)
  1269. * dispatch (slice - 1);
  1270. */
  1271. emms_c();
  1272. }
  1273. static void apply_loop_filter(Vp3DecodeContext *s)
  1274. {
  1275. int plane;
  1276. int x, y;
  1277. int *bounding_values= s->bounding_values_array+127;
  1278. #if 0
  1279. int bounding_values_array[256];
  1280. int filter_limit;
  1281. /* find the right loop limit value */
  1282. for (x = 63; x >= 0; x--) {
  1283. if (vp31_ac_scale_factor[x] >= s->quality_index)
  1284. break;
  1285. }
  1286. filter_limit = vp31_filter_limit_values[s->quality_index];
  1287. /* set up the bounding values */
  1288. memset(bounding_values_array, 0, 256 * sizeof(int));
  1289. for (x = 0; x < filter_limit; x++) {
  1290. bounding_values[-x - filter_limit] = -filter_limit + x;
  1291. bounding_values[-x] = -x;
  1292. bounding_values[x] = x;
  1293. bounding_values[x + filter_limit] = filter_limit - x;
  1294. }
  1295. #endif
  1296. for (plane = 0; plane < 3; plane++) {
  1297. int width = s->fragment_width >> !!plane;
  1298. int height = s->fragment_height >> !!plane;
  1299. int fragment = s->fragment_start [plane];
  1300. int stride = s->current_frame.linesize[plane];
  1301. uint8_t *plane_data = s->current_frame.data [plane];
  1302. if (!s->flipped_image) stride = -stride;
  1303. for (y = 0; y < height; y++) {
  1304. for (x = 0; x < width; x++) {
  1305. /* do not perform left edge filter for left columns frags */
  1306. if ((x > 0) &&
  1307. (s->all_fragments[fragment].coding_method != MODE_COPY)) {
  1308. s->dsp.vp3_h_loop_filter(
  1309. plane_data + s->all_fragments[fragment].first_pixel,
  1310. stride, bounding_values);
  1311. }
  1312. /* do not perform top edge filter for top row fragments */
  1313. if ((y > 0) &&
  1314. (s->all_fragments[fragment].coding_method != MODE_COPY)) {
  1315. s->dsp.vp3_v_loop_filter(
  1316. plane_data + s->all_fragments[fragment].first_pixel,
  1317. stride, bounding_values);
  1318. }
  1319. /* do not perform right edge filter for right column
  1320. * fragments or if right fragment neighbor is also coded
  1321. * in this frame (it will be filtered in next iteration) */
  1322. if ((x < width - 1) &&
  1323. (s->all_fragments[fragment].coding_method != MODE_COPY) &&
  1324. (s->all_fragments[fragment + 1].coding_method == MODE_COPY)) {
  1325. s->dsp.vp3_h_loop_filter(
  1326. plane_data + s->all_fragments[fragment + 1].first_pixel,
  1327. stride, bounding_values);
  1328. }
  1329. /* do not perform bottom edge filter for bottom row
  1330. * fragments or if bottom fragment neighbor is also coded
  1331. * in this frame (it will be filtered in the next row) */
  1332. if ((y < height - 1) &&
  1333. (s->all_fragments[fragment].coding_method != MODE_COPY) &&
  1334. (s->all_fragments[fragment + width].coding_method == MODE_COPY)) {
  1335. s->dsp.vp3_v_loop_filter(
  1336. plane_data + s->all_fragments[fragment + width].first_pixel,
  1337. stride, bounding_values);
  1338. }
  1339. fragment++;
  1340. }
  1341. }
  1342. }
  1343. }
  1344. /*
  1345. * This function computes the first pixel addresses for each fragment.
  1346. * This function needs to be invoked after the first frame is allocated
  1347. * so that it has access to the plane strides.
  1348. */
  1349. static void vp3_calculate_pixel_addresses(Vp3DecodeContext *s)
  1350. {
  1351. #define Y_INITIAL(chroma_shift) s->flipped_image ? 1 : s->fragment_height >> chroma_shift
  1352. #define Y_FINISHED(chroma_shift) s->flipped_image ? y <= s->fragment_height >> chroma_shift : y > 0
  1353. int i, x, y;
  1354. const int y_inc = s->flipped_image ? 1 : -1;
  1355. /* figure out the first pixel addresses for each of the fragments */
  1356. /* Y plane */
  1357. i = 0;
  1358. for (y = Y_INITIAL(0); Y_FINISHED(0); y += y_inc) {
  1359. for (x = 0; x < s->fragment_width; x++) {
  1360. s->all_fragments[i++].first_pixel =
  1361. s->golden_frame.linesize[0] * y * FRAGMENT_PIXELS -
  1362. s->golden_frame.linesize[0] +
  1363. x * FRAGMENT_PIXELS;
  1364. }
  1365. }
  1366. /* U plane */
  1367. i = s->fragment_start[1];
  1368. for (y = Y_INITIAL(1); Y_FINISHED(1); y += y_inc) {
  1369. for (x = 0; x < s->fragment_width / 2; x++) {
  1370. s->all_fragments[i++].first_pixel =
  1371. s->golden_frame.linesize[1] * y * FRAGMENT_PIXELS -
  1372. s->golden_frame.linesize[1] +
  1373. x * FRAGMENT_PIXELS;
  1374. }
  1375. }
  1376. /* V plane */
  1377. i = s->fragment_start[2];
  1378. for (y = Y_INITIAL(1); Y_FINISHED(1); y += y_inc) {
  1379. for (x = 0; x < s->fragment_width / 2; x++) {
  1380. s->all_fragments[i++].first_pixel =
  1381. s->golden_frame.linesize[2] * y * FRAGMENT_PIXELS -
  1382. s->golden_frame.linesize[2] +
  1383. x * FRAGMENT_PIXELS;
  1384. }
  1385. }
  1386. }
  1387. /*
  1388. * This is the ffmpeg/libavcodec API init function.
  1389. */
  1390. static av_cold int vp3_decode_init(AVCodecContext *avctx)
  1391. {
  1392. Vp3DecodeContext *s = avctx->priv_data;
  1393. int i, inter, plane;
  1394. int c_width;
  1395. int c_height;
  1396. int y_superblock_count;
  1397. int c_superblock_count;
  1398. if (avctx->codec_tag == MKTAG('V','P','3','0'))
  1399. s->version = 0;
  1400. else
  1401. s->version = 1;
  1402. s->avctx = avctx;
  1403. s->width = (avctx->width + 15) & 0xFFFFFFF0;
  1404. s->height = (avctx->height + 15) & 0xFFFFFFF0;
  1405. avctx->pix_fmt = PIX_FMT_YUV420P;
  1406. if(avctx->idct_algo==FF_IDCT_AUTO)
  1407. avctx->idct_algo=FF_IDCT_VP3;
  1408. dsputil_init(&s->dsp, avctx);
  1409. ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
  1410. /* initialize to an impossible value which will force a recalculation
  1411. * in the first frame decode */
  1412. s->quality_index = -1;
  1413. s->y_superblock_width = (s->width + 31) / 32;
  1414. s->y_superblock_height = (s->height + 31) / 32;
  1415. y_superblock_count = s->y_superblock_width * s->y_superblock_height;
  1416. /* work out the dimensions for the C planes */
  1417. c_width = s->width / 2;
  1418. c_height = s->height / 2;
  1419. s->c_superblock_width = (c_width + 31) / 32;
  1420. s->c_superblock_height = (c_height + 31) / 32;
  1421. c_superblock_count = s->c_superblock_width * s->c_superblock_height;
  1422. s->superblock_count = y_superblock_count + (c_superblock_count * 2);
  1423. s->u_superblock_start = y_superblock_count;
  1424. s->v_superblock_start = s->u_superblock_start + c_superblock_count;
  1425. s->superblock_coding = av_malloc(s->superblock_count);
  1426. s->macroblock_width = (s->width + 15) / 16;
  1427. s->macroblock_height = (s->height + 15) / 16;
  1428. s->macroblock_count = s->macroblock_width * s->macroblock_height;
  1429. s->fragment_width = s->width / FRAGMENT_PIXELS;
  1430. s->fragment_height = s->height / FRAGMENT_PIXELS;
  1431. /* fragment count covers all 8x8 blocks for all 3 planes */
  1432. s->fragment_count = s->fragment_width * s->fragment_height * 3 / 2;
  1433. s->fragment_start[1] = s->fragment_width * s->fragment_height;
  1434. s->fragment_start[2] = s->fragment_width * s->fragment_height * 5 / 4;
  1435. s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment));
  1436. s->coeff_counts = av_malloc(s->fragment_count * sizeof(*s->coeff_counts));
  1437. s->coeffs = av_malloc(s->fragment_count * sizeof(Coeff) * 65);
  1438. s->coded_fragment_list = av_malloc(s->fragment_count * sizeof(int));
  1439. s->pixel_addresses_initialized = 0;
  1440. if (!s->theora_tables)
  1441. {
  1442. for (i = 0; i < 64; i++) {
  1443. s->coded_dc_scale_factor[i] = vp31_dc_scale_factor[i];
  1444. s->coded_ac_scale_factor[i] = vp31_ac_scale_factor[i];
  1445. s->base_matrix[0][i] = vp31_intra_y_dequant[i];
  1446. s->base_matrix[1][i] = vp31_intra_c_dequant[i];
  1447. s->base_matrix[2][i] = vp31_inter_dequant[i];
  1448. s->filter_limit_values[i] = vp31_filter_limit_values[i];
  1449. }
  1450. for(inter=0; inter<2; inter++){
  1451. for(plane=0; plane<3; plane++){
  1452. s->qr_count[inter][plane]= 1;
  1453. s->qr_size [inter][plane][0]= 63;
  1454. s->qr_base [inter][plane][0]=
  1455. s->qr_base [inter][plane][1]= 2*inter + (!!plane)*!inter;
  1456. }
  1457. }
  1458. /* init VLC tables */
  1459. for (i = 0; i < 16; i++) {
  1460. /* DC histograms */
  1461. init_vlc(&s->dc_vlc[i], 5, 32,
  1462. &dc_bias[i][0][1], 4, 2,
  1463. &dc_bias[i][0][0], 4, 2, 0);
  1464. /* group 1 AC histograms */
  1465. init_vlc(&s->ac_vlc_1[i], 5, 32,
  1466. &ac_bias_0[i][0][1], 4, 2,
  1467. &ac_bias_0[i][0][0], 4, 2, 0);
  1468. /* group 2 AC histograms */
  1469. init_vlc(&s->ac_vlc_2[i], 5, 32,
  1470. &ac_bias_1[i][0][1], 4, 2,
  1471. &ac_bias_1[i][0][0], 4, 2, 0);
  1472. /* group 3 AC histograms */
  1473. init_vlc(&s->ac_vlc_3[i], 5, 32,
  1474. &ac_bias_2[i][0][1], 4, 2,
  1475. &ac_bias_2[i][0][0], 4, 2, 0);
  1476. /* group 4 AC histograms */
  1477. init_vlc(&s->ac_vlc_4[i], 5, 32,
  1478. &ac_bias_3[i][0][1], 4, 2,
  1479. &ac_bias_3[i][0][0], 4, 2, 0);
  1480. }
  1481. } else {
  1482. for (i = 0; i < 16; i++) {
  1483. /* DC histograms */
  1484. init_vlc(&s->dc_vlc[i], 5, 32,
  1485. &s->huffman_table[i][0][1], 4, 2,
  1486. &s->huffman_table[i][0][0], 4, 2, 0);
  1487. /* group 1 AC histograms */
  1488. init_vlc(&s->ac_vlc_1[i], 5, 32,
  1489. &s->huffman_table[i+16][0][1], 4, 2,
  1490. &s->huffman_table[i+16][0][0], 4, 2, 0);
  1491. /* group 2 AC histograms */
  1492. init_vlc(&s->ac_vlc_2[i], 5, 32,
  1493. &s->huffman_table[i+16*2][0][1], 4, 2,
  1494. &s->huffman_table[i+16*2][0][0], 4, 2, 0);
  1495. /* group 3 AC histograms */
  1496. init_vlc(&s->ac_vlc_3[i], 5, 32,
  1497. &s->huffman_table[i+16*3][0][1], 4, 2,
  1498. &s->huffman_table[i+16*3][0][0], 4, 2, 0);
  1499. /* group 4 AC histograms */
  1500. init_vlc(&s->ac_vlc_4[i], 5, 32,
  1501. &s->huffman_table[i+16*4][0][1], 4, 2,
  1502. &s->huffman_table[i+16*4][0][0], 4, 2, 0);
  1503. }
  1504. }
  1505. init_vlc(&s->superblock_run_length_vlc, 6, 34,
  1506. &superblock_run_length_vlc_table[0][1], 4, 2,
  1507. &superblock_run_length_vlc_table[0][0], 4, 2, 0);
  1508. init_vlc(&s->fragment_run_length_vlc, 5, 30,
  1509. &fragment_run_length_vlc_table[0][1], 4, 2,
  1510. &fragment_run_length_vlc_table[0][0], 4, 2, 0);
  1511. init_vlc(&s->mode_code_vlc, 3, 8,
  1512. &mode_code_vlc_table[0][1], 2, 1,
  1513. &mode_code_vlc_table[0][0], 2, 1, 0);
  1514. init_vlc(&s->motion_vector_vlc, 6, 63,
  1515. &motion_vector_vlc_table[0][1], 2, 1,
  1516. &motion_vector_vlc_table[0][0], 2, 1, 0);
  1517. /* work out the block mapping tables */
  1518. s->superblock_fragments = av_malloc(s->superblock_count * 16 * sizeof(int));
  1519. s->superblock_macroblocks = av_malloc(s->superblock_count * 4 * sizeof(int));
  1520. s->macroblock_fragments = av_malloc(s->macroblock_count * 6 * sizeof(int));
  1521. s->macroblock_coding = av_malloc(s->macroblock_count + 1);
  1522. init_block_mapping(s);
  1523. for (i = 0; i < 3; i++) {
  1524. s->current_frame.data[i] = NULL;
  1525. s->last_frame.data[i] = NULL;
  1526. s->golden_frame.data[i] = NULL;
  1527. }
  1528. return 0;
  1529. }
  1530. /*
  1531. * This is the ffmpeg/libavcodec API frame decode function.
  1532. */
  1533. static int vp3_decode_frame(AVCodecContext *avctx,
  1534. void *data, int *data_size,
  1535. const uint8_t *buf, int buf_size)
  1536. {
  1537. Vp3DecodeContext *s = avctx->priv_data;
  1538. GetBitContext gb;
  1539. static int counter = 0;
  1540. int i;
  1541. init_get_bits(&gb, buf, buf_size * 8);
  1542. if (s->theora && get_bits1(&gb))
  1543. {
  1544. av_log(avctx, AV_LOG_ERROR, "Header packet passed to frame decoder, skipping\n");
  1545. return -1;
  1546. }
  1547. s->keyframe = !get_bits1(&gb);
  1548. if (!s->theora)
  1549. skip_bits(&gb, 1);
  1550. s->last_quality_index = s->quality_index;
  1551. s->nqis=0;
  1552. do{
  1553. s->qis[s->nqis++]= get_bits(&gb, 6);
  1554. } while(s->theora >= 0x030200 && s->nqis<3 && get_bits1(&gb));
  1555. s->quality_index= s->qis[0];
  1556. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1557. av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n",
  1558. s->keyframe?"key":"", counter, s->quality_index);
  1559. counter++;
  1560. if (s->quality_index != s->last_quality_index) {
  1561. init_dequantizer(s);
  1562. init_loop_filter(s);
  1563. }
  1564. if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe)
  1565. return buf_size;
  1566. if (s->keyframe) {
  1567. if (!s->theora)
  1568. {
  1569. skip_bits(&gb, 4); /* width code */
  1570. skip_bits(&gb, 4); /* height code */
  1571. if (s->version)
  1572. {
  1573. s->version = get_bits(&gb, 5);
  1574. if (counter == 1)
  1575. av_log(s->avctx, AV_LOG_DEBUG, "VP version: %d\n", s->version);
  1576. }
  1577. }
  1578. if (s->version || s->theora)
  1579. {
  1580. if (get_bits1(&gb))
  1581. av_log(s->avctx, AV_LOG_ERROR, "Warning, unsupported keyframe coding type?!\n");
  1582. skip_bits(&gb, 2); /* reserved? */
  1583. }
  1584. if (s->last_frame.data[0] == s->golden_frame.data[0]) {
  1585. if (s->golden_frame.data[0])
  1586. avctx->release_buffer(avctx, &s->golden_frame);
  1587. s->last_frame= s->golden_frame; /* ensure that we catch any access to this released frame */
  1588. } else {
  1589. if (s->golden_frame.data[0])
  1590. avctx->release_buffer(avctx, &s->golden_frame);
  1591. if (s->last_frame.data[0])
  1592. avctx->release_buffer(avctx, &s->last_frame);
  1593. }
  1594. s->golden_frame.reference = 3;
  1595. if(avctx->get_buffer(avctx, &s->golden_frame) < 0) {
  1596. av_log(s->avctx, AV_LOG_ERROR, "vp3: get_buffer() failed\n");
  1597. return -1;
  1598. }
  1599. /* golden frame is also the current frame */
  1600. s->current_frame= s->golden_frame;
  1601. /* time to figure out pixel addresses? */
  1602. if (!s->pixel_addresses_initialized)
  1603. {
  1604. vp3_calculate_pixel_addresses(s);
  1605. s->pixel_addresses_initialized = 1;
  1606. }
  1607. } else {
  1608. /* allocate a new current frame */
  1609. s->current_frame.reference = 3;
  1610. if (!s->pixel_addresses_initialized) {
  1611. av_log(s->avctx, AV_LOG_ERROR, "vp3: first frame not a keyframe\n");
  1612. return -1;
  1613. }
  1614. if(avctx->get_buffer(avctx, &s->current_frame) < 0) {
  1615. av_log(s->avctx, AV_LOG_ERROR, "vp3: get_buffer() failed\n");
  1616. return -1;
  1617. }
  1618. }
  1619. s->current_frame.qscale_table= s->qscale_table; //FIXME allocate individual tables per AVFrame
  1620. s->current_frame.qstride= 0;
  1621. init_frame(s, &gb);
  1622. if (unpack_superblocks(s, &gb)){
  1623. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n");
  1624. return -1;
  1625. }
  1626. if (unpack_modes(s, &gb)){
  1627. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n");
  1628. return -1;
  1629. }
  1630. if (unpack_vectors(s, &gb)){
  1631. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n");
  1632. return -1;
  1633. }
  1634. if (unpack_dct_coeffs(s, &gb)){
  1635. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n");
  1636. return -1;
  1637. }
  1638. reverse_dc_prediction(s, 0, s->fragment_width, s->fragment_height);
  1639. if ((avctx->flags & CODEC_FLAG_GRAY) == 0) {
  1640. reverse_dc_prediction(s, s->fragment_start[1],
  1641. s->fragment_width / 2, s->fragment_height / 2);
  1642. reverse_dc_prediction(s, s->fragment_start[2],
  1643. s->fragment_width / 2, s->fragment_height / 2);
  1644. }
  1645. for (i = 0; i < s->macroblock_height; i++)
  1646. render_slice(s, i);
  1647. apply_loop_filter(s);
  1648. *data_size=sizeof(AVFrame);
  1649. *(AVFrame*)data= s->current_frame;
  1650. /* release the last frame, if it is allocated and if it is not the
  1651. * golden frame */
  1652. if ((s->last_frame.data[0]) &&
  1653. (s->last_frame.data[0] != s->golden_frame.data[0]))
  1654. avctx->release_buffer(avctx, &s->last_frame);
  1655. /* shuffle frames (last = current) */
  1656. s->last_frame= s->current_frame;
  1657. s->current_frame.data[0]= NULL; /* ensure that we catch any access to this released frame */
  1658. return buf_size;
  1659. }
  1660. /*
  1661. * This is the ffmpeg/libavcodec API module cleanup function.
  1662. */
  1663. static av_cold int vp3_decode_end(AVCodecContext *avctx)
  1664. {
  1665. Vp3DecodeContext *s = avctx->priv_data;
  1666. int i;
  1667. av_free(s->superblock_coding);
  1668. av_free(s->all_fragments);
  1669. av_free(s->coeff_counts);
  1670. av_free(s->coeffs);
  1671. av_free(s->coded_fragment_list);
  1672. av_free(s->superblock_fragments);
  1673. av_free(s->superblock_macroblocks);
  1674. av_free(s->macroblock_fragments);
  1675. av_free(s->macroblock_coding);
  1676. for (i = 0; i < 16; i++) {
  1677. free_vlc(&s->dc_vlc[i]);
  1678. free_vlc(&s->ac_vlc_1[i]);
  1679. free_vlc(&s->ac_vlc_2[i]);
  1680. free_vlc(&s->ac_vlc_3[i]);
  1681. free_vlc(&s->ac_vlc_4[i]);
  1682. }
  1683. free_vlc(&s->superblock_run_length_vlc);
  1684. free_vlc(&s->fragment_run_length_vlc);
  1685. free_vlc(&s->mode_code_vlc);
  1686. free_vlc(&s->motion_vector_vlc);
  1687. /* release all frames */
  1688. if (s->golden_frame.data[0] && s->golden_frame.data[0] != s->last_frame.data[0])
  1689. avctx->release_buffer(avctx, &s->golden_frame);
  1690. if (s->last_frame.data[0])
  1691. avctx->release_buffer(avctx, &s->last_frame);
  1692. /* no need to release the current_frame since it will always be pointing
  1693. * to the same frame as either the golden or last frame */
  1694. return 0;
  1695. }
  1696. static int read_huffman_tree(AVCodecContext *avctx, GetBitContext *gb)
  1697. {
  1698. Vp3DecodeContext *s = avctx->priv_data;
  1699. if (get_bits1(gb)) {
  1700. int token;
  1701. if (s->entries >= 32) { /* overflow */
  1702. av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
  1703. return -1;
  1704. }
  1705. token = get_bits(gb, 5);
  1706. //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);
  1707. s->huffman_table[s->hti][token][0] = s->hbits;
  1708. s->huffman_table[s->hti][token][1] = s->huff_code_size;
  1709. s->entries++;
  1710. }
  1711. else {
  1712. if (s->huff_code_size >= 32) {/* overflow */
  1713. av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
  1714. return -1;
  1715. }
  1716. s->huff_code_size++;
  1717. s->hbits <<= 1;
  1718. read_huffman_tree(avctx, gb);
  1719. s->hbits |= 1;
  1720. read_huffman_tree(avctx, gb);
  1721. s->hbits >>= 1;
  1722. s->huff_code_size--;
  1723. }
  1724. return 0;
  1725. }
  1726. #if CONFIG_THEORA_DECODER
  1727. static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb)
  1728. {
  1729. Vp3DecodeContext *s = avctx->priv_data;
  1730. int visible_width, visible_height;
  1731. s->theora = get_bits_long(gb, 24);
  1732. av_log(avctx, AV_LOG_DEBUG, "Theora bitstream version %X\n", s->theora);
  1733. /* 3.2.0 aka alpha3 has the same frame orientation as original vp3 */
  1734. /* but previous versions have the image flipped relative to vp3 */
  1735. if (s->theora < 0x030200)
  1736. {
  1737. s->flipped_image = 1;
  1738. av_log(avctx, AV_LOG_DEBUG, "Old (<alpha3) Theora bitstream, flipped image\n");
  1739. }
  1740. visible_width = s->width = get_bits(gb, 16) << 4;
  1741. visible_height = s->height = get_bits(gb, 16) << 4;
  1742. if(avcodec_check_dimensions(avctx, s->width, s->height)){
  1743. av_log(avctx, AV_LOG_ERROR, "Invalid dimensions (%dx%d)\n", s->width, s->height);
  1744. s->width= s->height= 0;
  1745. return -1;
  1746. }
  1747. if (s->theora >= 0x030400)
  1748. {
  1749. skip_bits(gb, 32); /* total number of superblocks in a frame */
  1750. // fixme, the next field is 36bits long
  1751. skip_bits(gb, 32); /* total number of blocks in a frame */
  1752. skip_bits(gb, 4); /* total number of blocks in a frame */
  1753. skip_bits(gb, 32); /* total number of macroblocks in a frame */
  1754. }
  1755. if (s->theora >= 0x030200) {
  1756. visible_width = get_bits_long(gb, 24);
  1757. visible_height = get_bits_long(gb, 24);
  1758. skip_bits(gb, 8); /* offset x */
  1759. skip_bits(gb, 8); /* offset y */
  1760. }
  1761. skip_bits(gb, 32); /* fps numerator */
  1762. skip_bits(gb, 32); /* fps denumerator */
  1763. skip_bits(gb, 24); /* aspect numerator */
  1764. skip_bits(gb, 24); /* aspect denumerator */
  1765. if (s->theora < 0x030200)
  1766. skip_bits(gb, 5); /* keyframe frequency force */
  1767. skip_bits(gb, 8); /* colorspace */
  1768. if (s->theora >= 0x030400)
  1769. skip_bits(gb, 2); /* pixel format: 420,res,422,444 */
  1770. skip_bits(gb, 24); /* bitrate */
  1771. skip_bits(gb, 6); /* quality hint */
  1772. if (s->theora >= 0x030200)
  1773. {
  1774. skip_bits(gb, 5); /* keyframe frequency force */
  1775. if (s->theora < 0x030400)
  1776. skip_bits(gb, 5); /* spare bits */
  1777. }
  1778. // align_get_bits(gb);
  1779. if ( visible_width <= s->width && visible_width > s->width-16
  1780. && visible_height <= s->height && visible_height > s->height-16)
  1781. avcodec_set_dimensions(avctx, visible_width, visible_height);
  1782. else
  1783. avcodec_set_dimensions(avctx, s->width, s->height);
  1784. return 0;
  1785. }
  1786. static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb)
  1787. {
  1788. Vp3DecodeContext *s = avctx->priv_data;
  1789. int i, n, matrices, inter, plane;
  1790. if (s->theora >= 0x030200) {
  1791. n = get_bits(gb, 3);
  1792. /* loop filter limit values table */
  1793. for (i = 0; i < 64; i++)
  1794. s->filter_limit_values[i] = get_bits(gb, n);
  1795. }
  1796. if (s->theora >= 0x030200)
  1797. n = get_bits(gb, 4) + 1;
  1798. else
  1799. n = 16;
  1800. /* quality threshold table */
  1801. for (i = 0; i < 64; i++)
  1802. s->coded_ac_scale_factor[i] = get_bits(gb, n);
  1803. if (s->theora >= 0x030200)
  1804. n = get_bits(gb, 4) + 1;
  1805. else
  1806. n = 16;
  1807. /* dc scale factor table */
  1808. for (i = 0; i < 64; i++)
  1809. s->coded_dc_scale_factor[i] = get_bits(gb, n);
  1810. if (s->theora >= 0x030200)
  1811. matrices = get_bits(gb, 9) + 1;
  1812. else
  1813. matrices = 3;
  1814. if(matrices > 384){
  1815. av_log(avctx, AV_LOG_ERROR, "invalid number of base matrixes\n");
  1816. return -1;
  1817. }
  1818. for(n=0; n<matrices; n++){
  1819. for (i = 0; i < 64; i++)
  1820. s->base_matrix[n][i]= get_bits(gb, 8);
  1821. }
  1822. for (inter = 0; inter <= 1; inter++) {
  1823. for (plane = 0; plane <= 2; plane++) {
  1824. int newqr= 1;
  1825. if (inter || plane > 0)
  1826. newqr = get_bits1(gb);
  1827. if (!newqr) {
  1828. int qtj, plj;
  1829. if(inter && get_bits1(gb)){
  1830. qtj = 0;
  1831. plj = plane;
  1832. }else{
  1833. qtj= (3*inter + plane - 1) / 3;
  1834. plj= (plane + 2) % 3;
  1835. }
  1836. s->qr_count[inter][plane]= s->qr_count[qtj][plj];
  1837. memcpy(s->qr_size[inter][plane], s->qr_size[qtj][plj], sizeof(s->qr_size[0][0]));
  1838. memcpy(s->qr_base[inter][plane], s->qr_base[qtj][plj], sizeof(s->qr_base[0][0]));
  1839. } else {
  1840. int qri= 0;
  1841. int qi = 0;
  1842. for(;;){
  1843. i= get_bits(gb, av_log2(matrices-1)+1);
  1844. if(i>= matrices){
  1845. av_log(avctx, AV_LOG_ERROR, "invalid base matrix index\n");
  1846. return -1;
  1847. }
  1848. s->qr_base[inter][plane][qri]= i;
  1849. if(qi >= 63)
  1850. break;
  1851. i = get_bits(gb, av_log2(63-qi)+1) + 1;
  1852. s->qr_size[inter][plane][qri++]= i;
  1853. qi += i;
  1854. }
  1855. if (qi > 63) {
  1856. av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi);
  1857. return -1;
  1858. }
  1859. s->qr_count[inter][plane]= qri;
  1860. }
  1861. }
  1862. }
  1863. /* Huffman tables */
  1864. for (s->hti = 0; s->hti < 80; s->hti++) {
  1865. s->entries = 0;
  1866. s->huff_code_size = 1;
  1867. if (!get_bits1(gb)) {
  1868. s->hbits = 0;
  1869. read_huffman_tree(avctx, gb);
  1870. s->hbits = 1;
  1871. read_huffman_tree(avctx, gb);
  1872. }
  1873. }
  1874. s->theora_tables = 1;
  1875. return 0;
  1876. }
  1877. static int theora_decode_init(AVCodecContext *avctx)
  1878. {
  1879. Vp3DecodeContext *s = avctx->priv_data;
  1880. GetBitContext gb;
  1881. int ptype;
  1882. uint8_t *header_start[3];
  1883. int header_len[3];
  1884. int i;
  1885. s->theora = 1;
  1886. if (!avctx->extradata_size)
  1887. {
  1888. av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n");
  1889. return -1;
  1890. }
  1891. if (ff_split_xiph_headers(avctx->extradata, avctx->extradata_size,
  1892. 42, header_start, header_len) < 0) {
  1893. av_log(avctx, AV_LOG_ERROR, "Corrupt extradata\n");
  1894. return -1;
  1895. }
  1896. for(i=0;i<3;i++) {
  1897. init_get_bits(&gb, header_start[i], header_len[i]);
  1898. ptype = get_bits(&gb, 8);
  1899. if (!(ptype & 0x80))
  1900. {
  1901. av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n");
  1902. // return -1;
  1903. }
  1904. // FIXME: Check for this as well.
  1905. skip_bits(&gb, 6*8); /* "theora" */
  1906. switch(ptype)
  1907. {
  1908. case 0x80:
  1909. theora_decode_header(avctx, &gb);
  1910. break;
  1911. case 0x81:
  1912. // FIXME: is this needed? it breaks sometimes
  1913. // theora_decode_comments(avctx, gb);
  1914. break;
  1915. case 0x82:
  1916. theora_decode_tables(avctx, &gb);
  1917. break;
  1918. default:
  1919. av_log(avctx, AV_LOG_ERROR, "Unknown Theora config packet: %d\n", ptype&~0x80);
  1920. break;
  1921. }
  1922. if(ptype != 0x81 && 8*header_len[i] != get_bits_count(&gb))
  1923. av_log(avctx, AV_LOG_WARNING, "%d bits left in packet %X\n", 8*header_len[i] - get_bits_count(&gb), ptype);
  1924. if (s->theora < 0x030200)
  1925. break;
  1926. }
  1927. vp3_decode_init(avctx);
  1928. return 0;
  1929. }
  1930. AVCodec theora_decoder = {
  1931. "theora",
  1932. CODEC_TYPE_VIDEO,
  1933. CODEC_ID_THEORA,
  1934. sizeof(Vp3DecodeContext),
  1935. theora_decode_init,
  1936. NULL,
  1937. vp3_decode_end,
  1938. vp3_decode_frame,
  1939. 0,
  1940. NULL,
  1941. .long_name = NULL_IF_CONFIG_SMALL("Theora"),
  1942. };
  1943. #endif
  1944. AVCodec vp3_decoder = {
  1945. "vp3",
  1946. CODEC_TYPE_VIDEO,
  1947. CODEC_ID_VP3,
  1948. sizeof(Vp3DecodeContext),
  1949. vp3_decode_init,
  1950. NULL,
  1951. vp3_decode_end,
  1952. vp3_decode_frame,
  1953. 0,
  1954. NULL,
  1955. .long_name = NULL_IF_CONFIG_SMALL("On2 VP3"),
  1956. };