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.

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