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.

2276 lines
78KB

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