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.

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