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.

2303 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 "get_bits.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 mapping_index = 0;
  218. int current_macroblock;
  219. int c_fragment;
  220. signed char travel_width[16] = {
  221. 1, 1, 0, -1,
  222. 0, 0, 1, 0,
  223. 1, 0, 1, 0,
  224. 0, -1, 0, 1
  225. };
  226. signed char travel_height[16] = {
  227. 0, 0, 1, 0,
  228. 1, 1, 0, -1,
  229. 0, 1, 0, -1,
  230. -1, 0, -1, 0
  231. };
  232. signed char travel_width_mb[4] = {
  233. 1, 0, 1, 0
  234. };
  235. signed char travel_height_mb[4] = {
  236. 0, 1, 0, -1
  237. };
  238. hilbert_walk_mb[0] = 1;
  239. hilbert_walk_mb[1] = s->macroblock_width;
  240. hilbert_walk_mb[2] = 1;
  241. hilbert_walk_mb[3] = -s->macroblock_width;
  242. /* iterate through each superblock (all planes) and map the fragments */
  243. for (i = 0; i < s->superblock_count; i++) {
  244. /* time to re-assign the limits? */
  245. if (i == 0) {
  246. /* start of Y superblocks */
  247. right_edge = s->fragment_width;
  248. bottom_edge = s->fragment_height;
  249. current_width = -1;
  250. current_height = 0;
  251. superblock_row_inc = 3 * s->fragment_width -
  252. (s->y_superblock_width * 4 - s->fragment_width);
  253. /* the first operation for this variable is to advance by 1 */
  254. current_fragment = -1;
  255. } else if (i == s->u_superblock_start) {
  256. /* start of U superblocks */
  257. right_edge = s->fragment_width / 2;
  258. bottom_edge = s->fragment_height / 2;
  259. current_width = -1;
  260. current_height = 0;
  261. superblock_row_inc = 3 * (s->fragment_width / 2) -
  262. (s->c_superblock_width * 4 - s->fragment_width / 2);
  263. /* the first operation for this variable is to advance by 1 */
  264. current_fragment = s->fragment_start[1] - 1;
  265. } else if (i == s->v_superblock_start) {
  266. /* start of V superblocks */
  267. right_edge = s->fragment_width / 2;
  268. bottom_edge = s->fragment_height / 2;
  269. current_width = -1;
  270. current_height = 0;
  271. superblock_row_inc = 3 * (s->fragment_width / 2) -
  272. (s->c_superblock_width * 4 - s->fragment_width / 2);
  273. /* the first operation for this variable is to advance by 1 */
  274. current_fragment = s->fragment_start[2] - 1;
  275. }
  276. if (current_width >= right_edge - 1) {
  277. /* reset width and move to next superblock row */
  278. current_width = -1;
  279. current_height += 4;
  280. /* fragment is now at the start of a new superblock row */
  281. current_fragment += superblock_row_inc;
  282. }
  283. /* iterate through all 16 fragments in a superblock */
  284. for (j = 0; j < 16; j++) {
  285. current_fragment += travel_width[j] + right_edge * travel_height[j];
  286. current_width += travel_width[j];
  287. current_height += travel_height[j];
  288. /* check if the fragment is in bounds */
  289. if ((current_width < right_edge) &&
  290. (current_height < bottom_edge)) {
  291. s->superblock_fragments[mapping_index] = current_fragment;
  292. } else {
  293. s->superblock_fragments[mapping_index] = -1;
  294. }
  295. mapping_index++;
  296. }
  297. }
  298. /* initialize the superblock <-> macroblock mapping; iterate through
  299. * all of the Y plane superblocks to build this mapping */
  300. right_edge = s->macroblock_width;
  301. bottom_edge = s->macroblock_height;
  302. current_width = -1;
  303. current_height = 0;
  304. superblock_row_inc = s->macroblock_width -
  305. (s->y_superblock_width * 2 - s->macroblock_width);
  306. mapping_index = 0;
  307. current_macroblock = -1;
  308. for (i = 0; i < s->u_superblock_start; i++) {
  309. if (current_width >= right_edge - 1) {
  310. /* reset width and move to next superblock row */
  311. current_width = -1;
  312. current_height += 2;
  313. /* macroblock is now at the start of a new superblock row */
  314. current_macroblock += superblock_row_inc;
  315. }
  316. /* iterate through each potential macroblock in the superblock */
  317. for (j = 0; j < 4; j++) {
  318. current_macroblock += hilbert_walk_mb[j];
  319. current_width += travel_width_mb[j];
  320. current_height += travel_height_mb[j];
  321. /* check if the macroblock is in bounds */
  322. if ((current_width < right_edge) &&
  323. (current_height < bottom_edge)) {
  324. s->superblock_macroblocks[mapping_index] = current_macroblock;
  325. } else {
  326. s->superblock_macroblocks[mapping_index] = -1;
  327. }
  328. mapping_index++;
  329. }
  330. }
  331. /* initialize the macroblock <-> fragment mapping */
  332. current_fragment = 0;
  333. current_macroblock = 0;
  334. mapping_index = 0;
  335. for (i = 0; i < s->fragment_height; i += 2) {
  336. for (j = 0; j < s->fragment_width; j += 2) {
  337. s->all_fragments[current_fragment].macroblock = current_macroblock;
  338. s->macroblock_fragments[mapping_index++] = current_fragment;
  339. if (j + 1 < s->fragment_width) {
  340. s->all_fragments[current_fragment + 1].macroblock = current_macroblock;
  341. s->macroblock_fragments[mapping_index++] = current_fragment + 1;
  342. } else
  343. s->macroblock_fragments[mapping_index++] = -1;
  344. if (i + 1 < s->fragment_height) {
  345. s->all_fragments[current_fragment + s->fragment_width].macroblock =
  346. current_macroblock;
  347. s->macroblock_fragments[mapping_index++] =
  348. current_fragment + s->fragment_width;
  349. } else
  350. s->macroblock_fragments[mapping_index++] = -1;
  351. if ((j + 1 < s->fragment_width) && (i + 1 < s->fragment_height)) {
  352. s->all_fragments[current_fragment + s->fragment_width + 1].macroblock =
  353. current_macroblock;
  354. s->macroblock_fragments[mapping_index++] =
  355. current_fragment + s->fragment_width + 1;
  356. } else
  357. s->macroblock_fragments[mapping_index++] = -1;
  358. /* C planes */
  359. c_fragment = s->fragment_start[1] +
  360. (i * s->fragment_width / 4) + (j / 2);
  361. s->all_fragments[c_fragment].macroblock = s->macroblock_count;
  362. s->macroblock_fragments[mapping_index++] = c_fragment;
  363. c_fragment = s->fragment_start[2] +
  364. (i * s->fragment_width / 4) + (j / 2);
  365. s->all_fragments[c_fragment].macroblock = s->macroblock_count;
  366. s->macroblock_fragments[mapping_index++] = c_fragment;
  367. if (j + 2 <= s->fragment_width)
  368. current_fragment += 2;
  369. else
  370. current_fragment++;
  371. current_macroblock++;
  372. }
  373. current_fragment += s->fragment_width;
  374. }
  375. return 0; /* successful path out */
  376. }
  377. /*
  378. * This function wipes out all of the fragment data.
  379. */
  380. static void init_frame(Vp3DecodeContext *s, GetBitContext *gb)
  381. {
  382. int i;
  383. /* zero out all of the fragment information */
  384. s->coded_fragment_list_index = 0;
  385. for (i = 0; i < s->fragment_count; i++) {
  386. s->coeff_counts[i] = 0;
  387. s->all_fragments[i].motion_x = 127;
  388. s->all_fragments[i].motion_y = 127;
  389. s->all_fragments[i].next_coeff= NULL;
  390. s->coeffs[i].index=
  391. s->coeffs[i].coeff=0;
  392. s->coeffs[i].next= NULL;
  393. }
  394. }
  395. /*
  396. * This function sets up the dequantization tables used for a particular
  397. * frame.
  398. */
  399. static void init_dequantizer(Vp3DecodeContext *s)
  400. {
  401. int ac_scale_factor = s->coded_ac_scale_factor[s->quality_index];
  402. int dc_scale_factor = s->coded_dc_scale_factor[s->quality_index];
  403. int i, plane, inter, qri, bmi, bmj, qistart;
  404. for(inter=0; inter<2; inter++){
  405. for(plane=0; plane<3; plane++){
  406. int sum=0;
  407. for(qri=0; qri<s->qr_count[inter][plane]; qri++){
  408. sum+= s->qr_size[inter][plane][qri];
  409. if(s->quality_index <= sum)
  410. break;
  411. }
  412. qistart= sum - s->qr_size[inter][plane][qri];
  413. bmi= s->qr_base[inter][plane][qri ];
  414. bmj= s->qr_base[inter][plane][qri+1];
  415. for(i=0; i<64; i++){
  416. int coeff= ( 2*(sum -s->quality_index)*s->base_matrix[bmi][i]
  417. - 2*(qistart-s->quality_index)*s->base_matrix[bmj][i]
  418. + s->qr_size[inter][plane][qri])
  419. / (2*s->qr_size[inter][plane][qri]);
  420. int qmin= 8<<(inter + !i);
  421. int qscale= i ? ac_scale_factor : dc_scale_factor;
  422. s->qmat[inter][plane][s->dsp.idct_permutation[i]]= av_clip((qscale * coeff)/100 * 4, qmin, 4096);
  423. }
  424. }
  425. }
  426. memset(s->qscale_table, (FFMAX(s->qmat[0][0][1], s->qmat[0][1][1])+8)/16, 512); //FIXME finetune
  427. }
  428. /*
  429. * This function initializes the loop filter boundary limits if the frame's
  430. * quality index is different from the previous frame's.
  431. */
  432. static void init_loop_filter(Vp3DecodeContext *s)
  433. {
  434. int *bounding_values= s->bounding_values_array+127;
  435. int filter_limit;
  436. int x;
  437. filter_limit = s->filter_limit_values[s->quality_index];
  438. /* set up the bounding values */
  439. memset(s->bounding_values_array, 0, 256 * sizeof(int));
  440. for (x = 0; x < filter_limit; x++) {
  441. bounding_values[-x - filter_limit] = -filter_limit + x;
  442. bounding_values[-x] = -x;
  443. bounding_values[x] = x;
  444. bounding_values[x + filter_limit] = filter_limit - x;
  445. }
  446. bounding_values[129] = bounding_values[130] = filter_limit * 0x02020202;
  447. }
  448. /*
  449. * This function unpacks all of the superblock/macroblock/fragment coding
  450. * information from the bitstream.
  451. */
  452. static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
  453. {
  454. int bit = 0;
  455. int current_superblock = 0;
  456. int current_run = 0;
  457. int decode_fully_flags = 0;
  458. int decode_partial_blocks = 0;
  459. int first_c_fragment_seen;
  460. int i, j;
  461. int current_fragment;
  462. if (s->keyframe) {
  463. memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);
  464. } else {
  465. /* unpack the list of partially-coded superblocks */
  466. bit = get_bits1(gb);
  467. /* toggle the bit because as soon as the first run length is
  468. * fetched the bit will be toggled again */
  469. bit ^= 1;
  470. while (current_superblock < s->superblock_count) {
  471. if (current_run-- == 0) {
  472. bit ^= 1;
  473. current_run = get_vlc2(gb,
  474. s->superblock_run_length_vlc.table, 6, 2);
  475. if (current_run == 33)
  476. current_run += get_bits(gb, 12);
  477. /* if any of the superblocks are not partially coded, flag
  478. * a boolean to decode the list of fully-coded superblocks */
  479. if (bit == 0) {
  480. decode_fully_flags = 1;
  481. } else {
  482. /* make a note of the fact that there are partially coded
  483. * superblocks */
  484. decode_partial_blocks = 1;
  485. }
  486. }
  487. s->superblock_coding[current_superblock++] = bit;
  488. }
  489. /* unpack the list of fully coded superblocks if any of the blocks were
  490. * not marked as partially coded in the previous step */
  491. if (decode_fully_flags) {
  492. current_superblock = 0;
  493. current_run = 0;
  494. bit = get_bits1(gb);
  495. /* toggle the bit because as soon as the first run length is
  496. * fetched the bit will be toggled again */
  497. bit ^= 1;
  498. while (current_superblock < s->superblock_count) {
  499. /* skip any superblocks already marked as partially coded */
  500. if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {
  501. if (current_run-- == 0) {
  502. bit ^= 1;
  503. current_run = get_vlc2(gb,
  504. s->superblock_run_length_vlc.table, 6, 2);
  505. if (current_run == 33)
  506. current_run += get_bits(gb, 12);
  507. }
  508. s->superblock_coding[current_superblock] = 2*bit;
  509. }
  510. current_superblock++;
  511. }
  512. }
  513. /* if there were partial blocks, initialize bitstream for
  514. * unpacking fragment codings */
  515. if (decode_partial_blocks) {
  516. current_run = 0;
  517. bit = get_bits1(gb);
  518. /* toggle the bit because as soon as the first run length is
  519. * fetched the bit will be toggled again */
  520. bit ^= 1;
  521. }
  522. }
  523. /* figure out which fragments are coded; iterate through each
  524. * superblock (all planes) */
  525. s->coded_fragment_list_index = 0;
  526. s->next_coeff= s->coeffs + s->fragment_count;
  527. s->first_coded_y_fragment = s->first_coded_c_fragment = 0;
  528. s->last_coded_y_fragment = s->last_coded_c_fragment = -1;
  529. first_c_fragment_seen = 0;
  530. memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
  531. for (i = 0; i < s->superblock_count; i++) {
  532. /* iterate through all 16 fragments in a superblock */
  533. for (j = 0; j < 16; j++) {
  534. /* if the fragment is in bounds, check its coding status */
  535. current_fragment = s->superblock_fragments[i * 16 + j];
  536. if (current_fragment >= s->fragment_count) {
  537. av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_superblocks(): bad fragment number (%d >= %d)\n",
  538. current_fragment, s->fragment_count);
  539. return 1;
  540. }
  541. if (current_fragment != -1) {
  542. if (s->superblock_coding[i] == SB_NOT_CODED) {
  543. /* copy all the fragments from the prior frame */
  544. s->all_fragments[current_fragment].coding_method =
  545. MODE_COPY;
  546. } else if (s->superblock_coding[i] == SB_PARTIALLY_CODED) {
  547. /* fragment may or may not be coded; this is the case
  548. * that cares about the fragment coding runs */
  549. if (current_run-- == 0) {
  550. bit ^= 1;
  551. current_run = get_vlc2(gb,
  552. s->fragment_run_length_vlc.table, 5, 2);
  553. }
  554. if (bit) {
  555. /* default mode; actual mode will be decoded in
  556. * the next phase */
  557. s->all_fragments[current_fragment].coding_method =
  558. MODE_INTER_NO_MV;
  559. s->all_fragments[current_fragment].next_coeff= s->coeffs + current_fragment;
  560. s->coded_fragment_list[s->coded_fragment_list_index] =
  561. current_fragment;
  562. if ((current_fragment >= s->fragment_start[1]) &&
  563. (s->last_coded_y_fragment == -1) &&
  564. (!first_c_fragment_seen)) {
  565. s->first_coded_c_fragment = s->coded_fragment_list_index;
  566. s->last_coded_y_fragment = s->first_coded_c_fragment - 1;
  567. first_c_fragment_seen = 1;
  568. }
  569. s->coded_fragment_list_index++;
  570. s->macroblock_coding[s->all_fragments[current_fragment].macroblock] = MODE_INTER_NO_MV;
  571. } else {
  572. /* not coded; copy this fragment from the prior frame */
  573. s->all_fragments[current_fragment].coding_method =
  574. MODE_COPY;
  575. }
  576. } else {
  577. /* fragments are fully coded in this superblock; actual
  578. * coding will be determined in next step */
  579. s->all_fragments[current_fragment].coding_method =
  580. MODE_INTER_NO_MV;
  581. s->all_fragments[current_fragment].next_coeff= s->coeffs + current_fragment;
  582. s->coded_fragment_list[s->coded_fragment_list_index] =
  583. current_fragment;
  584. if ((current_fragment >= s->fragment_start[1]) &&
  585. (s->last_coded_y_fragment == -1) &&
  586. (!first_c_fragment_seen)) {
  587. s->first_coded_c_fragment = s->coded_fragment_list_index;
  588. s->last_coded_y_fragment = s->first_coded_c_fragment - 1;
  589. first_c_fragment_seen = 1;
  590. }
  591. s->coded_fragment_list_index++;
  592. s->macroblock_coding[s->all_fragments[current_fragment].macroblock] = MODE_INTER_NO_MV;
  593. }
  594. }
  595. }
  596. }
  597. if (!first_c_fragment_seen)
  598. /* only Y fragments coded in this frame */
  599. s->last_coded_y_fragment = s->coded_fragment_list_index - 1;
  600. else
  601. /* end the list of coded C fragments */
  602. s->last_coded_c_fragment = s->coded_fragment_list_index - 1;
  603. return 0;
  604. }
  605. /*
  606. * This function unpacks all the coding mode data for individual macroblocks
  607. * from the bitstream.
  608. */
  609. static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
  610. {
  611. int i, j, k;
  612. int scheme;
  613. int current_macroblock;
  614. int current_fragment;
  615. int coding_mode;
  616. int custom_mode_alphabet[CODING_MODE_COUNT];
  617. if (s->keyframe) {
  618. for (i = 0; i < s->fragment_count; i++)
  619. s->all_fragments[i].coding_method = MODE_INTRA;
  620. } else {
  621. /* fetch the mode coding scheme for this frame */
  622. scheme = get_bits(gb, 3);
  623. /* is it a custom coding scheme? */
  624. if (scheme == 0) {
  625. for (i = 0; i < 8; i++)
  626. custom_mode_alphabet[i] = MODE_INTER_NO_MV;
  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 = FFALIGN(avctx->width, 16);
  1404. s->height = FFALIGN(avctx->height, 16);
  1405. avctx->pix_fmt = PIX_FMT_YUV420P;
  1406. avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
  1407. if(avctx->idct_algo==FF_IDCT_AUTO)
  1408. avctx->idct_algo=FF_IDCT_VP3;
  1409. dsputil_init(&s->dsp, avctx);
  1410. ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
  1411. /* initialize to an impossible value which will force a recalculation
  1412. * in the first frame decode */
  1413. s->quality_index = -1;
  1414. s->y_superblock_width = (s->width + 31) / 32;
  1415. s->y_superblock_height = (s->height + 31) / 32;
  1416. y_superblock_count = s->y_superblock_width * s->y_superblock_height;
  1417. /* work out the dimensions for the C planes */
  1418. c_width = s->width / 2;
  1419. c_height = s->height / 2;
  1420. s->c_superblock_width = (c_width + 31) / 32;
  1421. s->c_superblock_height = (c_height + 31) / 32;
  1422. c_superblock_count = s->c_superblock_width * s->c_superblock_height;
  1423. s->superblock_count = y_superblock_count + (c_superblock_count * 2);
  1424. s->u_superblock_start = y_superblock_count;
  1425. s->v_superblock_start = s->u_superblock_start + c_superblock_count;
  1426. s->superblock_coding = av_malloc(s->superblock_count);
  1427. s->macroblock_width = (s->width + 15) / 16;
  1428. s->macroblock_height = (s->height + 15) / 16;
  1429. s->macroblock_count = s->macroblock_width * s->macroblock_height;
  1430. s->fragment_width = s->width / FRAGMENT_PIXELS;
  1431. s->fragment_height = s->height / FRAGMENT_PIXELS;
  1432. /* fragment count covers all 8x8 blocks for all 3 planes */
  1433. s->fragment_count = s->fragment_width * s->fragment_height * 3 / 2;
  1434. s->fragment_start[1] = s->fragment_width * s->fragment_height;
  1435. s->fragment_start[2] = s->fragment_width * s->fragment_height * 5 / 4;
  1436. s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment));
  1437. s->coeff_counts = av_malloc(s->fragment_count * sizeof(*s->coeff_counts));
  1438. s->coeffs = av_malloc(s->fragment_count * sizeof(Coeff) * 65);
  1439. s->coded_fragment_list = av_malloc(s->fragment_count * sizeof(int));
  1440. s->pixel_addresses_initialized = 0;
  1441. if (!s->theora_tables)
  1442. {
  1443. for (i = 0; i < 64; i++) {
  1444. s->coded_dc_scale_factor[i] = vp31_dc_scale_factor[i];
  1445. s->coded_ac_scale_factor[i] = vp31_ac_scale_factor[i];
  1446. s->base_matrix[0][i] = vp31_intra_y_dequant[i];
  1447. s->base_matrix[1][i] = vp31_intra_c_dequant[i];
  1448. s->base_matrix[2][i] = vp31_inter_dequant[i];
  1449. s->filter_limit_values[i] = vp31_filter_limit_values[i];
  1450. }
  1451. for(inter=0; inter<2; inter++){
  1452. for(plane=0; plane<3; plane++){
  1453. s->qr_count[inter][plane]= 1;
  1454. s->qr_size [inter][plane][0]= 63;
  1455. s->qr_base [inter][plane][0]=
  1456. s->qr_base [inter][plane][1]= 2*inter + (!!plane)*!inter;
  1457. }
  1458. }
  1459. /* init VLC tables */
  1460. for (i = 0; i < 16; i++) {
  1461. /* DC histograms */
  1462. init_vlc(&s->dc_vlc[i], 5, 32,
  1463. &dc_bias[i][0][1], 4, 2,
  1464. &dc_bias[i][0][0], 4, 2, 0);
  1465. /* group 1 AC histograms */
  1466. init_vlc(&s->ac_vlc_1[i], 5, 32,
  1467. &ac_bias_0[i][0][1], 4, 2,
  1468. &ac_bias_0[i][0][0], 4, 2, 0);
  1469. /* group 2 AC histograms */
  1470. init_vlc(&s->ac_vlc_2[i], 5, 32,
  1471. &ac_bias_1[i][0][1], 4, 2,
  1472. &ac_bias_1[i][0][0], 4, 2, 0);
  1473. /* group 3 AC histograms */
  1474. init_vlc(&s->ac_vlc_3[i], 5, 32,
  1475. &ac_bias_2[i][0][1], 4, 2,
  1476. &ac_bias_2[i][0][0], 4, 2, 0);
  1477. /* group 4 AC histograms */
  1478. init_vlc(&s->ac_vlc_4[i], 5, 32,
  1479. &ac_bias_3[i][0][1], 4, 2,
  1480. &ac_bias_3[i][0][0], 4, 2, 0);
  1481. }
  1482. } else {
  1483. for (i = 0; i < 16; i++) {
  1484. /* DC histograms */
  1485. init_vlc(&s->dc_vlc[i], 5, 32,
  1486. &s->huffman_table[i][0][1], 4, 2,
  1487. &s->huffman_table[i][0][0], 4, 2, 0);
  1488. /* group 1 AC histograms */
  1489. init_vlc(&s->ac_vlc_1[i], 5, 32,
  1490. &s->huffman_table[i+16][0][1], 4, 2,
  1491. &s->huffman_table[i+16][0][0], 4, 2, 0);
  1492. /* group 2 AC histograms */
  1493. init_vlc(&s->ac_vlc_2[i], 5, 32,
  1494. &s->huffman_table[i+16*2][0][1], 4, 2,
  1495. &s->huffman_table[i+16*2][0][0], 4, 2, 0);
  1496. /* group 3 AC histograms */
  1497. init_vlc(&s->ac_vlc_3[i], 5, 32,
  1498. &s->huffman_table[i+16*3][0][1], 4, 2,
  1499. &s->huffman_table[i+16*3][0][0], 4, 2, 0);
  1500. /* group 4 AC histograms */
  1501. init_vlc(&s->ac_vlc_4[i], 5, 32,
  1502. &s->huffman_table[i+16*4][0][1], 4, 2,
  1503. &s->huffman_table[i+16*4][0][0], 4, 2, 0);
  1504. }
  1505. }
  1506. init_vlc(&s->superblock_run_length_vlc, 6, 34,
  1507. &superblock_run_length_vlc_table[0][1], 4, 2,
  1508. &superblock_run_length_vlc_table[0][0], 4, 2, 0);
  1509. init_vlc(&s->fragment_run_length_vlc, 5, 30,
  1510. &fragment_run_length_vlc_table[0][1], 4, 2,
  1511. &fragment_run_length_vlc_table[0][0], 4, 2, 0);
  1512. init_vlc(&s->mode_code_vlc, 3, 8,
  1513. &mode_code_vlc_table[0][1], 2, 1,
  1514. &mode_code_vlc_table[0][0], 2, 1, 0);
  1515. init_vlc(&s->motion_vector_vlc, 6, 63,
  1516. &motion_vector_vlc_table[0][1], 2, 1,
  1517. &motion_vector_vlc_table[0][0], 2, 1, 0);
  1518. /* work out the block mapping tables */
  1519. s->superblock_fragments = av_malloc(s->superblock_count * 16 * sizeof(int));
  1520. s->superblock_macroblocks = av_malloc(s->superblock_count * 4 * sizeof(int));
  1521. s->macroblock_fragments = av_malloc(s->macroblock_count * 6 * sizeof(int));
  1522. s->macroblock_coding = av_malloc(s->macroblock_count + 1);
  1523. init_block_mapping(s);
  1524. for (i = 0; i < 3; i++) {
  1525. s->current_frame.data[i] = NULL;
  1526. s->last_frame.data[i] = NULL;
  1527. s->golden_frame.data[i] = NULL;
  1528. }
  1529. return 0;
  1530. }
  1531. /*
  1532. * This is the ffmpeg/libavcodec API frame decode function.
  1533. */
  1534. static int vp3_decode_frame(AVCodecContext *avctx,
  1535. void *data, int *data_size,
  1536. AVPacket *avpkt)
  1537. {
  1538. const uint8_t *buf = avpkt->data;
  1539. int buf_size = avpkt->size;
  1540. Vp3DecodeContext *s = avctx->priv_data;
  1541. GetBitContext gb;
  1542. static int counter = 0;
  1543. int i;
  1544. init_get_bits(&gb, buf, buf_size * 8);
  1545. if (s->theora && get_bits1(&gb))
  1546. {
  1547. av_log(avctx, AV_LOG_ERROR, "Header packet passed to frame decoder, skipping\n");
  1548. return -1;
  1549. }
  1550. s->keyframe = !get_bits1(&gb);
  1551. if (!s->theora)
  1552. skip_bits(&gb, 1);
  1553. s->last_quality_index = s->quality_index;
  1554. s->nqis=0;
  1555. do{
  1556. s->qis[s->nqis++]= get_bits(&gb, 6);
  1557. } while(s->theora >= 0x030200 && s->nqis<3 && get_bits1(&gb));
  1558. s->quality_index= s->qis[0];
  1559. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1560. av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n",
  1561. s->keyframe?"key":"", counter, s->quality_index);
  1562. counter++;
  1563. if (s->quality_index != s->last_quality_index) {
  1564. init_dequantizer(s);
  1565. init_loop_filter(s);
  1566. }
  1567. if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe)
  1568. return buf_size;
  1569. if (s->keyframe) {
  1570. if (!s->theora)
  1571. {
  1572. skip_bits(&gb, 4); /* width code */
  1573. skip_bits(&gb, 4); /* height code */
  1574. if (s->version)
  1575. {
  1576. s->version = get_bits(&gb, 5);
  1577. if (counter == 1)
  1578. av_log(s->avctx, AV_LOG_DEBUG, "VP version: %d\n", s->version);
  1579. }
  1580. }
  1581. if (s->version || s->theora)
  1582. {
  1583. if (get_bits1(&gb))
  1584. av_log(s->avctx, AV_LOG_ERROR, "Warning, unsupported keyframe coding type?!\n");
  1585. skip_bits(&gb, 2); /* reserved? */
  1586. }
  1587. if (s->last_frame.data[0] == s->golden_frame.data[0]) {
  1588. if (s->golden_frame.data[0])
  1589. avctx->release_buffer(avctx, &s->golden_frame);
  1590. s->last_frame= s->golden_frame; /* ensure that we catch any access to this released frame */
  1591. } else {
  1592. if (s->golden_frame.data[0])
  1593. avctx->release_buffer(avctx, &s->golden_frame);
  1594. if (s->last_frame.data[0])
  1595. avctx->release_buffer(avctx, &s->last_frame);
  1596. }
  1597. s->golden_frame.reference = 3;
  1598. if(avctx->get_buffer(avctx, &s->golden_frame) < 0) {
  1599. av_log(s->avctx, AV_LOG_ERROR, "vp3: get_buffer() failed\n");
  1600. return -1;
  1601. }
  1602. /* golden frame is also the current frame */
  1603. s->current_frame= s->golden_frame;
  1604. /* time to figure out pixel addresses? */
  1605. if (!s->pixel_addresses_initialized)
  1606. {
  1607. vp3_calculate_pixel_addresses(s);
  1608. s->pixel_addresses_initialized = 1;
  1609. }
  1610. } else {
  1611. /* allocate a new current frame */
  1612. s->current_frame.reference = 3;
  1613. if (!s->pixel_addresses_initialized) {
  1614. av_log(s->avctx, AV_LOG_ERROR, "vp3: first frame not a keyframe\n");
  1615. return -1;
  1616. }
  1617. if(avctx->get_buffer(avctx, &s->current_frame) < 0) {
  1618. av_log(s->avctx, AV_LOG_ERROR, "vp3: get_buffer() failed\n");
  1619. return -1;
  1620. }
  1621. }
  1622. s->current_frame.qscale_table= s->qscale_table; //FIXME allocate individual tables per AVFrame
  1623. s->current_frame.qstride= 0;
  1624. init_frame(s, &gb);
  1625. if (unpack_superblocks(s, &gb)){
  1626. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n");
  1627. return -1;
  1628. }
  1629. if (unpack_modes(s, &gb)){
  1630. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n");
  1631. return -1;
  1632. }
  1633. if (unpack_vectors(s, &gb)){
  1634. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n");
  1635. return -1;
  1636. }
  1637. if (unpack_dct_coeffs(s, &gb)){
  1638. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n");
  1639. return -1;
  1640. }
  1641. reverse_dc_prediction(s, 0, s->fragment_width, s->fragment_height);
  1642. if ((avctx->flags & CODEC_FLAG_GRAY) == 0) {
  1643. reverse_dc_prediction(s, s->fragment_start[1],
  1644. s->fragment_width / 2, s->fragment_height / 2);
  1645. reverse_dc_prediction(s, s->fragment_start[2],
  1646. s->fragment_width / 2, s->fragment_height / 2);
  1647. }
  1648. for (i = 0; i < s->macroblock_height; i++)
  1649. render_slice(s, i);
  1650. apply_loop_filter(s);
  1651. *data_size=sizeof(AVFrame);
  1652. *(AVFrame*)data= s->current_frame;
  1653. /* release the last frame, if it is allocated and if it is not the
  1654. * golden frame */
  1655. if ((s->last_frame.data[0]) &&
  1656. (s->last_frame.data[0] != s->golden_frame.data[0]))
  1657. avctx->release_buffer(avctx, &s->last_frame);
  1658. /* shuffle frames (last = current) */
  1659. s->last_frame= s->current_frame;
  1660. s->current_frame.data[0]= NULL; /* ensure that we catch any access to this released frame */
  1661. return buf_size;
  1662. }
  1663. /*
  1664. * This is the ffmpeg/libavcodec API module cleanup function.
  1665. */
  1666. static av_cold int vp3_decode_end(AVCodecContext *avctx)
  1667. {
  1668. Vp3DecodeContext *s = avctx->priv_data;
  1669. int i;
  1670. av_free(s->superblock_coding);
  1671. av_free(s->all_fragments);
  1672. av_free(s->coeff_counts);
  1673. av_free(s->coeffs);
  1674. av_free(s->coded_fragment_list);
  1675. av_free(s->superblock_fragments);
  1676. av_free(s->superblock_macroblocks);
  1677. av_free(s->macroblock_fragments);
  1678. av_free(s->macroblock_coding);
  1679. for (i = 0; i < 16; i++) {
  1680. free_vlc(&s->dc_vlc[i]);
  1681. free_vlc(&s->ac_vlc_1[i]);
  1682. free_vlc(&s->ac_vlc_2[i]);
  1683. free_vlc(&s->ac_vlc_3[i]);
  1684. free_vlc(&s->ac_vlc_4[i]);
  1685. }
  1686. free_vlc(&s->superblock_run_length_vlc);
  1687. free_vlc(&s->fragment_run_length_vlc);
  1688. free_vlc(&s->mode_code_vlc);
  1689. free_vlc(&s->motion_vector_vlc);
  1690. /* release all frames */
  1691. if (s->golden_frame.data[0] && s->golden_frame.data[0] != s->last_frame.data[0])
  1692. avctx->release_buffer(avctx, &s->golden_frame);
  1693. if (s->last_frame.data[0])
  1694. avctx->release_buffer(avctx, &s->last_frame);
  1695. /* no need to release the current_frame since it will always be pointing
  1696. * to the same frame as either the golden or last frame */
  1697. return 0;
  1698. }
  1699. static int read_huffman_tree(AVCodecContext *avctx, GetBitContext *gb)
  1700. {
  1701. Vp3DecodeContext *s = avctx->priv_data;
  1702. if (get_bits1(gb)) {
  1703. int token;
  1704. if (s->entries >= 32) { /* overflow */
  1705. av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
  1706. return -1;
  1707. }
  1708. token = get_bits(gb, 5);
  1709. //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);
  1710. s->huffman_table[s->hti][token][0] = s->hbits;
  1711. s->huffman_table[s->hti][token][1] = s->huff_code_size;
  1712. s->entries++;
  1713. }
  1714. else {
  1715. if (s->huff_code_size >= 32) {/* overflow */
  1716. av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
  1717. return -1;
  1718. }
  1719. s->huff_code_size++;
  1720. s->hbits <<= 1;
  1721. if (read_huffman_tree(avctx, gb))
  1722. return -1;
  1723. s->hbits |= 1;
  1724. if (read_huffman_tree(avctx, gb))
  1725. return -1;
  1726. s->hbits >>= 1;
  1727. s->huff_code_size--;
  1728. }
  1729. return 0;
  1730. }
  1731. #if CONFIG_THEORA_DECODER
  1732. static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb)
  1733. {
  1734. Vp3DecodeContext *s = avctx->priv_data;
  1735. int visible_width, visible_height;
  1736. s->theora = get_bits_long(gb, 24);
  1737. av_log(avctx, AV_LOG_DEBUG, "Theora bitstream version %X\n", s->theora);
  1738. /* 3.2.0 aka alpha3 has the same frame orientation as original vp3 */
  1739. /* but previous versions have the image flipped relative to vp3 */
  1740. if (s->theora < 0x030200)
  1741. {
  1742. s->flipped_image = 1;
  1743. av_log(avctx, AV_LOG_DEBUG, "Old (<alpha3) Theora bitstream, flipped image\n");
  1744. }
  1745. visible_width = s->width = get_bits(gb, 16) << 4;
  1746. visible_height = s->height = get_bits(gb, 16) << 4;
  1747. if(avcodec_check_dimensions(avctx, s->width, s->height)){
  1748. av_log(avctx, AV_LOG_ERROR, "Invalid dimensions (%dx%d)\n", s->width, s->height);
  1749. s->width= s->height= 0;
  1750. return -1;
  1751. }
  1752. if (s->theora >= 0x030400)
  1753. {
  1754. skip_bits(gb, 32); /* total number of superblocks in a frame */
  1755. // fixme, the next field is 36bits long
  1756. skip_bits(gb, 32); /* total number of blocks in a frame */
  1757. skip_bits(gb, 4); /* total number of blocks in a frame */
  1758. skip_bits(gb, 32); /* total number of macroblocks in a frame */
  1759. }
  1760. if (s->theora >= 0x030200) {
  1761. visible_width = get_bits_long(gb, 24);
  1762. visible_height = get_bits_long(gb, 24);
  1763. skip_bits(gb, 8); /* offset x */
  1764. skip_bits(gb, 8); /* offset y */
  1765. }
  1766. skip_bits(gb, 32); /* fps numerator */
  1767. skip_bits(gb, 32); /* fps denumerator */
  1768. skip_bits(gb, 24); /* aspect numerator */
  1769. skip_bits(gb, 24); /* aspect denumerator */
  1770. if (s->theora < 0x030200)
  1771. skip_bits(gb, 5); /* keyframe frequency force */
  1772. skip_bits(gb, 8); /* colorspace */
  1773. if (s->theora >= 0x030400)
  1774. skip_bits(gb, 2); /* pixel format: 420,res,422,444 */
  1775. skip_bits(gb, 24); /* bitrate */
  1776. skip_bits(gb, 6); /* quality hint */
  1777. if (s->theora >= 0x030200)
  1778. {
  1779. skip_bits(gb, 5); /* keyframe frequency force */
  1780. if (s->theora < 0x030400)
  1781. skip_bits(gb, 5); /* spare bits */
  1782. }
  1783. // align_get_bits(gb);
  1784. if ( visible_width <= s->width && visible_width > s->width-16
  1785. && visible_height <= s->height && visible_height > s->height-16)
  1786. avcodec_set_dimensions(avctx, visible_width, visible_height);
  1787. else
  1788. avcodec_set_dimensions(avctx, s->width, s->height);
  1789. return 0;
  1790. }
  1791. static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb)
  1792. {
  1793. Vp3DecodeContext *s = avctx->priv_data;
  1794. int i, n, matrices, inter, plane;
  1795. if (s->theora >= 0x030200) {
  1796. n = get_bits(gb, 3);
  1797. /* loop filter limit values table */
  1798. for (i = 0; i < 64; i++)
  1799. s->filter_limit_values[i] = get_bits(gb, n);
  1800. }
  1801. if (s->theora >= 0x030200)
  1802. n = get_bits(gb, 4) + 1;
  1803. else
  1804. n = 16;
  1805. /* quality threshold table */
  1806. for (i = 0; i < 64; i++)
  1807. s->coded_ac_scale_factor[i] = get_bits(gb, n);
  1808. if (s->theora >= 0x030200)
  1809. n = get_bits(gb, 4) + 1;
  1810. else
  1811. n = 16;
  1812. /* dc scale factor table */
  1813. for (i = 0; i < 64; i++)
  1814. s->coded_dc_scale_factor[i] = get_bits(gb, n);
  1815. if (s->theora >= 0x030200)
  1816. matrices = get_bits(gb, 9) + 1;
  1817. else
  1818. matrices = 3;
  1819. if(matrices > 384){
  1820. av_log(avctx, AV_LOG_ERROR, "invalid number of base matrixes\n");
  1821. return -1;
  1822. }
  1823. for(n=0; n<matrices; n++){
  1824. for (i = 0; i < 64; i++)
  1825. s->base_matrix[n][i]= get_bits(gb, 8);
  1826. }
  1827. for (inter = 0; inter <= 1; inter++) {
  1828. for (plane = 0; plane <= 2; plane++) {
  1829. int newqr= 1;
  1830. if (inter || plane > 0)
  1831. newqr = get_bits1(gb);
  1832. if (!newqr) {
  1833. int qtj, plj;
  1834. if(inter && get_bits1(gb)){
  1835. qtj = 0;
  1836. plj = plane;
  1837. }else{
  1838. qtj= (3*inter + plane - 1) / 3;
  1839. plj= (plane + 2) % 3;
  1840. }
  1841. s->qr_count[inter][plane]= s->qr_count[qtj][plj];
  1842. memcpy(s->qr_size[inter][plane], s->qr_size[qtj][plj], sizeof(s->qr_size[0][0]));
  1843. memcpy(s->qr_base[inter][plane], s->qr_base[qtj][plj], sizeof(s->qr_base[0][0]));
  1844. } else {
  1845. int qri= 0;
  1846. int qi = 0;
  1847. for(;;){
  1848. i= get_bits(gb, av_log2(matrices-1)+1);
  1849. if(i>= matrices){
  1850. av_log(avctx, AV_LOG_ERROR, "invalid base matrix index\n");
  1851. return -1;
  1852. }
  1853. s->qr_base[inter][plane][qri]= i;
  1854. if(qi >= 63)
  1855. break;
  1856. i = get_bits(gb, av_log2(63-qi)+1) + 1;
  1857. s->qr_size[inter][plane][qri++]= i;
  1858. qi += i;
  1859. }
  1860. if (qi > 63) {
  1861. av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi);
  1862. return -1;
  1863. }
  1864. s->qr_count[inter][plane]= qri;
  1865. }
  1866. }
  1867. }
  1868. /* Huffman tables */
  1869. for (s->hti = 0; s->hti < 80; s->hti++) {
  1870. s->entries = 0;
  1871. s->huff_code_size = 1;
  1872. if (!get_bits1(gb)) {
  1873. s->hbits = 0;
  1874. if(read_huffman_tree(avctx, gb))
  1875. return -1;
  1876. s->hbits = 1;
  1877. if(read_huffman_tree(avctx, gb))
  1878. return -1;
  1879. }
  1880. }
  1881. s->theora_tables = 1;
  1882. return 0;
  1883. }
  1884. static av_cold int theora_decode_init(AVCodecContext *avctx)
  1885. {
  1886. Vp3DecodeContext *s = avctx->priv_data;
  1887. GetBitContext gb;
  1888. int ptype;
  1889. uint8_t *header_start[3];
  1890. int header_len[3];
  1891. int i;
  1892. s->theora = 1;
  1893. if (!avctx->extradata_size)
  1894. {
  1895. av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n");
  1896. return -1;
  1897. }
  1898. if (ff_split_xiph_headers(avctx->extradata, avctx->extradata_size,
  1899. 42, header_start, header_len) < 0) {
  1900. av_log(avctx, AV_LOG_ERROR, "Corrupt extradata\n");
  1901. return -1;
  1902. }
  1903. for(i=0;i<3;i++) {
  1904. init_get_bits(&gb, header_start[i], header_len[i]);
  1905. ptype = get_bits(&gb, 8);
  1906. if (!(ptype & 0x80))
  1907. {
  1908. av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n");
  1909. // return -1;
  1910. }
  1911. // FIXME: Check for this as well.
  1912. skip_bits_long(&gb, 6*8); /* "theora" */
  1913. switch(ptype)
  1914. {
  1915. case 0x80:
  1916. theora_decode_header(avctx, &gb);
  1917. break;
  1918. case 0x81:
  1919. // FIXME: is this needed? it breaks sometimes
  1920. // theora_decode_comments(avctx, gb);
  1921. break;
  1922. case 0x82:
  1923. if (theora_decode_tables(avctx, &gb))
  1924. return -1;
  1925. break;
  1926. default:
  1927. av_log(avctx, AV_LOG_ERROR, "Unknown Theora config packet: %d\n", ptype&~0x80);
  1928. break;
  1929. }
  1930. if(ptype != 0x81 && 8*header_len[i] != get_bits_count(&gb))
  1931. av_log(avctx, AV_LOG_WARNING, "%d bits left in packet %X\n", 8*header_len[i] - get_bits_count(&gb), ptype);
  1932. if (s->theora < 0x030200)
  1933. break;
  1934. }
  1935. vp3_decode_init(avctx);
  1936. return 0;
  1937. }
  1938. AVCodec theora_decoder = {
  1939. "theora",
  1940. CODEC_TYPE_VIDEO,
  1941. CODEC_ID_THEORA,
  1942. sizeof(Vp3DecodeContext),
  1943. theora_decode_init,
  1944. NULL,
  1945. vp3_decode_end,
  1946. vp3_decode_frame,
  1947. 0,
  1948. NULL,
  1949. .long_name = NULL_IF_CONFIG_SMALL("Theora"),
  1950. };
  1951. #endif
  1952. AVCodec vp3_decoder = {
  1953. "vp3",
  1954. CODEC_TYPE_VIDEO,
  1955. CODEC_ID_VP3,
  1956. sizeof(Vp3DecodeContext),
  1957. vp3_decode_init,
  1958. NULL,
  1959. vp3_decode_end,
  1960. vp3_decode_frame,
  1961. 0,
  1962. NULL,
  1963. .long_name = NULL_IF_CONFIG_SMALL("On2 VP3"),
  1964. };