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.

2323 lines
81KB

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