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.

2445 lines
85KB

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