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.

2376 lines
82KB

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