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.

2321 lines
80KB

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