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.

2269 lines
77KB

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