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.

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