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.

2294 lines
80KB

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