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.

2296 lines
81KB

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