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.

2425 lines
84KB

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