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.

2254 lines
76KB

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