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.

2336 lines
82KB

  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. } else {
  693. memset(motion_x, 0, 6 * sizeof(int));
  694. memset(motion_y, 0, 6 * sizeof(int));
  695. /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme */
  696. coding_mode = get_bits1(gb);
  697. /* iterate through all of the macroblocks that contain 1 or more
  698. * coded fragments */
  699. for (i = 0; i < s->u_superblock_start; i++) {
  700. for (j = 0; j < 4; j++) {
  701. current_macroblock = s->superblock_macroblocks[i * 4 + j];
  702. if ((current_macroblock == -1) ||
  703. (s->macroblock_coding[current_macroblock] == MODE_COPY))
  704. continue;
  705. if (current_macroblock >= s->macroblock_count) {
  706. av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vectors(): bad macroblock number (%d >= %d)\n",
  707. current_macroblock, s->macroblock_count);
  708. return 1;
  709. }
  710. current_fragment = s->macroblock_fragments[current_macroblock * 6];
  711. if (current_fragment >= s->fragment_count) {
  712. av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vectors(): bad fragment number (%d >= %d\n",
  713. current_fragment, s->fragment_count);
  714. return 1;
  715. }
  716. switch (s->macroblock_coding[current_macroblock]) {
  717. case MODE_INTER_PLUS_MV:
  718. case MODE_GOLDEN_MV:
  719. /* all 6 fragments use the same motion vector */
  720. if (coding_mode == 0) {
  721. motion_x[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
  722. motion_y[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
  723. } else {
  724. motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)];
  725. motion_y[0] = fixed_motion_vector_table[get_bits(gb, 6)];
  726. }
  727. for (k = 1; k < 6; k++) {
  728. motion_x[k] = motion_x[0];
  729. motion_y[k] = motion_y[0];
  730. }
  731. /* vector maintenance, only on MODE_INTER_PLUS_MV */
  732. if (s->macroblock_coding[current_macroblock] ==
  733. MODE_INTER_PLUS_MV) {
  734. prior_last_motion_x = last_motion_x;
  735. prior_last_motion_y = last_motion_y;
  736. last_motion_x = motion_x[0];
  737. last_motion_y = motion_y[0];
  738. }
  739. break;
  740. case MODE_INTER_FOURMV:
  741. /* vector maintenance */
  742. prior_last_motion_x = last_motion_x;
  743. prior_last_motion_y = last_motion_y;
  744. /* fetch 4 vectors from the bitstream, one for each
  745. * Y fragment, then average for the C fragment vectors */
  746. motion_x[4] = motion_y[4] = 0;
  747. for (k = 0; k < 4; k++) {
  748. for (l = 0; l < s->coded_fragment_list_index; l++)
  749. if (s->coded_fragment_list[l] == s->macroblock_fragments[6*current_macroblock + k])
  750. break;
  751. if (l < s->coded_fragment_list_index) {
  752. if (coding_mode == 0) {
  753. motion_x[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
  754. motion_y[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
  755. } else {
  756. motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)];
  757. motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)];
  758. }
  759. last_motion_x = motion_x[k];
  760. last_motion_y = motion_y[k];
  761. } else {
  762. motion_x[k] = 0;
  763. motion_y[k] = 0;
  764. }
  765. motion_x[4] += motion_x[k];
  766. motion_y[4] += motion_y[k];
  767. }
  768. motion_x[5]=
  769. motion_x[4]= RSHIFT(motion_x[4], 2);
  770. motion_y[5]=
  771. motion_y[4]= RSHIFT(motion_y[4], 2);
  772. break;
  773. case MODE_INTER_LAST_MV:
  774. /* all 6 fragments use the last motion vector */
  775. motion_x[0] = last_motion_x;
  776. motion_y[0] = last_motion_y;
  777. for (k = 1; k < 6; k++) {
  778. motion_x[k] = motion_x[0];
  779. motion_y[k] = motion_y[0];
  780. }
  781. /* no vector maintenance (last vector remains the
  782. * last vector) */
  783. break;
  784. case MODE_INTER_PRIOR_LAST:
  785. /* all 6 fragments use the motion vector prior to the
  786. * last motion vector */
  787. motion_x[0] = prior_last_motion_x;
  788. motion_y[0] = prior_last_motion_y;
  789. for (k = 1; k < 6; k++) {
  790. motion_x[k] = motion_x[0];
  791. motion_y[k] = motion_y[0];
  792. }
  793. /* vector maintenance */
  794. prior_last_motion_x = last_motion_x;
  795. prior_last_motion_y = last_motion_y;
  796. last_motion_x = motion_x[0];
  797. last_motion_y = motion_y[0];
  798. break;
  799. default:
  800. /* covers intra, inter without MV, golden without MV */
  801. memset(motion_x, 0, 6 * sizeof(int));
  802. memset(motion_y, 0, 6 * sizeof(int));
  803. /* no vector maintenance */
  804. break;
  805. }
  806. /* assign the motion vectors to the correct fragments */
  807. for (k = 0; k < 6; k++) {
  808. current_fragment =
  809. s->macroblock_fragments[current_macroblock * 6 + k];
  810. if (current_fragment == -1)
  811. continue;
  812. if (current_fragment >= s->fragment_count) {
  813. av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vectors(): bad fragment number (%d >= %d)\n",
  814. current_fragment, s->fragment_count);
  815. return 1;
  816. }
  817. s->all_fragments[current_fragment].motion_x = motion_x[k];
  818. s->all_fragments[current_fragment].motion_y = motion_y[k];
  819. }
  820. }
  821. }
  822. }
  823. return 0;
  824. }
  825. /*
  826. * This function is called by unpack_dct_coeffs() to extract the VLCs from
  827. * the bitstream. The VLCs encode tokens which are used to unpack DCT
  828. * data. This function unpacks all the VLCs for either the Y plane or both
  829. * C planes, and is called for DC coefficients or different AC coefficient
  830. * levels (since different coefficient types require different VLC tables.
  831. *
  832. * This function returns a residual eob run. E.g, if a particular token gave
  833. * instructions to EOB the next 5 fragments and there were only 2 fragments
  834. * left in the current fragment range, 3 would be returned so that it could
  835. * be passed into the next call to this same function.
  836. */
  837. static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
  838. VLC *table, int coeff_index,
  839. int first_fragment, int last_fragment,
  840. int eob_run)
  841. {
  842. int i;
  843. int token;
  844. int zero_run = 0;
  845. DCTELEM coeff = 0;
  846. Vp3Fragment *fragment;
  847. uint8_t *perm= s->scantable.permutated;
  848. int bits_to_get;
  849. if ((first_fragment >= s->fragment_count) ||
  850. (last_fragment >= s->fragment_count)) {
  851. av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vlcs(): bad fragment number (%d -> %d ?)\n",
  852. first_fragment, last_fragment);
  853. return 0;
  854. }
  855. for (i = first_fragment; i <= last_fragment; i++) {
  856. int fragment_num = s->coded_fragment_list[i];
  857. if (s->coeff_counts[fragment_num] > coeff_index)
  858. continue;
  859. fragment = &s->all_fragments[fragment_num];
  860. if (!eob_run) {
  861. /* decode a VLC into a token */
  862. token = get_vlc2(gb, table->table, 5, 3);
  863. /* use the token to get a zero run, a coefficient, and an eob run */
  864. if (token <= 6) {
  865. eob_run = eob_run_base[token];
  866. if (eob_run_get_bits[token])
  867. eob_run += get_bits(gb, eob_run_get_bits[token]);
  868. coeff = zero_run = 0;
  869. } else {
  870. bits_to_get = coeff_get_bits[token];
  871. if (!bits_to_get)
  872. coeff = coeff_tables[token][0];
  873. else
  874. coeff = coeff_tables[token][get_bits(gb, bits_to_get)];
  875. zero_run = zero_run_base[token];
  876. if (zero_run_get_bits[token])
  877. zero_run += get_bits(gb, zero_run_get_bits[token]);
  878. }
  879. }
  880. if (!eob_run) {
  881. s->coeff_counts[fragment_num] += zero_run;
  882. if (s->coeff_counts[fragment_num] < 64){
  883. fragment->next_coeff->coeff= coeff;
  884. fragment->next_coeff->index= perm[s->coeff_counts[fragment_num]++]; //FIXME perm here already?
  885. fragment->next_coeff->next= s->next_coeff;
  886. s->next_coeff->next=NULL;
  887. fragment->next_coeff= s->next_coeff++;
  888. }
  889. } else {
  890. s->coeff_counts[fragment_num] |= 128;
  891. eob_run--;
  892. }
  893. }
  894. return eob_run;
  895. }
  896. /*
  897. * This function unpacks all of the DCT coefficient data from the
  898. * bitstream.
  899. */
  900. static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
  901. {
  902. int i;
  903. int dc_y_table;
  904. int dc_c_table;
  905. int ac_y_table;
  906. int ac_c_table;
  907. int residual_eob_run = 0;
  908. /* fetch the DC table indexes */
  909. dc_y_table = get_bits(gb, 4);
  910. dc_c_table = get_bits(gb, 4);
  911. /* unpack the Y plane DC coefficients */
  912. residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
  913. s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
  914. /* unpack the C plane DC coefficients */
  915. residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
  916. s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
  917. /* fetch the AC table indexes */
  918. ac_y_table = get_bits(gb, 4);
  919. ac_c_table = get_bits(gb, 4);
  920. /* unpack the group 1 AC coefficients (coeffs 1-5) */
  921. for (i = 1; i <= 5; i++) {
  922. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_y_table], i,
  923. s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
  924. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_c_table], i,
  925. s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
  926. }
  927. /* unpack the group 2 AC coefficients (coeffs 6-14) */
  928. for (i = 6; i <= 14; i++) {
  929. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_y_table], i,
  930. s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
  931. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_c_table], i,
  932. s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
  933. }
  934. /* unpack the group 3 AC coefficients (coeffs 15-27) */
  935. for (i = 15; i <= 27; i++) {
  936. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_y_table], i,
  937. s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
  938. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_c_table], i,
  939. s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
  940. }
  941. /* unpack the group 4 AC coefficients (coeffs 28-63) */
  942. for (i = 28; i <= 63; i++) {
  943. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_y_table], i,
  944. s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
  945. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_c_table], i,
  946. s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
  947. }
  948. return 0;
  949. }
  950. /*
  951. * This function reverses the DC prediction for each coded fragment in
  952. * the frame. Much of this function is adapted directly from the original
  953. * VP3 source code.
  954. */
  955. #define COMPATIBLE_FRAME(x) \
  956. (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
  957. #define FRAME_CODED(x) (s->all_fragments[x].coding_method != MODE_COPY)
  958. #define DC_COEFF(u) (s->coeffs[u].index ? 0 : s->coeffs[u].coeff) //FIXME do somethin to simplify this
  959. static void reverse_dc_prediction(Vp3DecodeContext *s,
  960. int first_fragment,
  961. int fragment_width,
  962. int fragment_height)
  963. {
  964. #define PUL 8
  965. #define PU 4
  966. #define PUR 2
  967. #define PL 1
  968. int x, y;
  969. int i = first_fragment;
  970. int predicted_dc;
  971. /* DC values for the left, up-left, up, and up-right fragments */
  972. int vl, vul, vu, vur;
  973. /* indexes for the left, up-left, up, and up-right fragments */
  974. int l, ul, u, ur;
  975. /*
  976. * The 6 fields mean:
  977. * 0: up-left multiplier
  978. * 1: up multiplier
  979. * 2: up-right multiplier
  980. * 3: left multiplier
  981. */
  982. int predictor_transform[16][4] = {
  983. { 0, 0, 0, 0},
  984. { 0, 0, 0,128}, // PL
  985. { 0, 0,128, 0}, // PUR
  986. { 0, 0, 53, 75}, // PUR|PL
  987. { 0,128, 0, 0}, // PU
  988. { 0, 64, 0, 64}, // PU|PL
  989. { 0,128, 0, 0}, // PU|PUR
  990. { 0, 0, 53, 75}, // PU|PUR|PL
  991. {128, 0, 0, 0}, // PUL
  992. { 0, 0, 0,128}, // PUL|PL
  993. { 64, 0, 64, 0}, // PUL|PUR
  994. { 0, 0, 53, 75}, // PUL|PUR|PL
  995. { 0,128, 0, 0}, // PUL|PU
  996. {-104,116, 0,116}, // PUL|PU|PL
  997. { 24, 80, 24, 0}, // PUL|PU|PUR
  998. {-104,116, 0,116} // PUL|PU|PUR|PL
  999. };
  1000. /* This table shows which types of blocks can use other blocks for
  1001. * prediction. For example, INTRA is the only mode in this table to
  1002. * have a frame number of 0. That means INTRA blocks can only predict
  1003. * from other INTRA blocks. There are 2 golden frame coding types;
  1004. * blocks encoding in these modes can only predict from other blocks
  1005. * that were encoded with these 1 of these 2 modes. */
  1006. unsigned char compatible_frame[8] = {
  1007. 1, /* MODE_INTER_NO_MV */
  1008. 0, /* MODE_INTRA */
  1009. 1, /* MODE_INTER_PLUS_MV */
  1010. 1, /* MODE_INTER_LAST_MV */
  1011. 1, /* MODE_INTER_PRIOR_MV */
  1012. 2, /* MODE_USING_GOLDEN */
  1013. 2, /* MODE_GOLDEN_MV */
  1014. 1 /* MODE_INTER_FOUR_MV */
  1015. };
  1016. int current_frame_type;
  1017. /* there is a last DC predictor for each of the 3 frame types */
  1018. short last_dc[3];
  1019. int transform = 0;
  1020. vul = vu = vur = vl = 0;
  1021. last_dc[0] = last_dc[1] = last_dc[2] = 0;
  1022. /* for each fragment row... */
  1023. for (y = 0; y < fragment_height; y++) {
  1024. /* for each fragment in a row... */
  1025. for (x = 0; x < fragment_width; x++, i++) {
  1026. /* reverse prediction if this block was coded */
  1027. if (s->all_fragments[i].coding_method != MODE_COPY) {
  1028. current_frame_type =
  1029. compatible_frame[s->all_fragments[i].coding_method];
  1030. transform= 0;
  1031. if(x){
  1032. l= i-1;
  1033. vl = DC_COEFF(l);
  1034. if(FRAME_CODED(l) && COMPATIBLE_FRAME(l))
  1035. transform |= PL;
  1036. }
  1037. if(y){
  1038. u= i-fragment_width;
  1039. vu = DC_COEFF(u);
  1040. if(FRAME_CODED(u) && COMPATIBLE_FRAME(u))
  1041. transform |= PU;
  1042. if(x){
  1043. ul= i-fragment_width-1;
  1044. vul = DC_COEFF(ul);
  1045. if(FRAME_CODED(ul) && COMPATIBLE_FRAME(ul))
  1046. transform |= PUL;
  1047. }
  1048. if(x + 1 < fragment_width){
  1049. ur= i-fragment_width+1;
  1050. vur = DC_COEFF(ur);
  1051. if(FRAME_CODED(ur) && COMPATIBLE_FRAME(ur))
  1052. transform |= PUR;
  1053. }
  1054. }
  1055. if (transform == 0) {
  1056. /* if there were no fragments to predict from, use last
  1057. * DC saved */
  1058. predicted_dc = last_dc[current_frame_type];
  1059. } else {
  1060. /* apply the appropriate predictor transform */
  1061. predicted_dc =
  1062. (predictor_transform[transform][0] * vul) +
  1063. (predictor_transform[transform][1] * vu) +
  1064. (predictor_transform[transform][2] * vur) +
  1065. (predictor_transform[transform][3] * vl);
  1066. predicted_dc /= 128;
  1067. /* check for outranging on the [ul u l] and
  1068. * [ul u ur l] predictors */
  1069. if ((transform == 13) || (transform == 15)) {
  1070. if (FFABS(predicted_dc - vu) > 128)
  1071. predicted_dc = vu;
  1072. else if (FFABS(predicted_dc - vl) > 128)
  1073. predicted_dc = vl;
  1074. else if (FFABS(predicted_dc - vul) > 128)
  1075. predicted_dc = vul;
  1076. }
  1077. }
  1078. /* at long last, apply the predictor */
  1079. if(s->coeffs[i].index){
  1080. *s->next_coeff= s->coeffs[i];
  1081. s->coeffs[i].index=0;
  1082. s->coeffs[i].coeff=0;
  1083. s->coeffs[i].next= s->next_coeff++;
  1084. }
  1085. s->coeffs[i].coeff += predicted_dc;
  1086. /* save the DC */
  1087. last_dc[current_frame_type] = DC_COEFF(i);
  1088. if(DC_COEFF(i) && !(s->coeff_counts[i]&127)){
  1089. s->coeff_counts[i]= 129;
  1090. // s->all_fragments[i].next_coeff= s->next_coeff;
  1091. s->coeffs[i].next= s->next_coeff;
  1092. (s->next_coeff++)->next=NULL;
  1093. }
  1094. }
  1095. }
  1096. }
  1097. }
  1098. static void horizontal_filter(unsigned char *first_pixel, int stride,
  1099. int *bounding_values);
  1100. static void vertical_filter(unsigned char *first_pixel, int stride,
  1101. int *bounding_values);
  1102. /*
  1103. * Perform the final rendering for a particular slice of data.
  1104. * The slice number ranges from 0..(macroblock_height - 1).
  1105. */
  1106. static void render_slice(Vp3DecodeContext *s, int slice)
  1107. {
  1108. int x;
  1109. int16_t *dequantizer;
  1110. DECLARE_ALIGNED_16(DCTELEM, block[64]);
  1111. int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
  1112. int motion_halfpel_index;
  1113. uint8_t *motion_source;
  1114. int plane;
  1115. int current_macroblock_entry = slice * s->macroblock_width * 6;
  1116. if (slice >= s->macroblock_height)
  1117. return;
  1118. for (plane = 0; plane < 3; plane++) {
  1119. uint8_t *output_plane = s->current_frame.data [plane];
  1120. uint8_t * last_plane = s-> last_frame.data [plane];
  1121. uint8_t *golden_plane = s-> golden_frame.data [plane];
  1122. int stride = s->current_frame.linesize[plane];
  1123. int plane_width = s->width >> !!plane;
  1124. int plane_height = s->height >> !!plane;
  1125. int y = slice * FRAGMENT_PIXELS << !plane ;
  1126. int slice_height = y + (FRAGMENT_PIXELS << !plane);
  1127. int i = s->macroblock_fragments[current_macroblock_entry + plane + 3*!!plane];
  1128. if (!s->flipped_image) stride = -stride;
  1129. if(FFABS(stride) > 2048)
  1130. return; //various tables are fixed size
  1131. /* for each fragment row in the slice (both of them)... */
  1132. for (; y < slice_height; y += 8) {
  1133. /* for each fragment in a row... */
  1134. for (x = 0; x < plane_width; x += 8, i++) {
  1135. if ((i < 0) || (i >= s->fragment_count)) {
  1136. av_log(s->avctx, AV_LOG_ERROR, " vp3:render_slice(): bad fragment number (%d)\n", i);
  1137. return;
  1138. }
  1139. /* transform if this block was coded */
  1140. if ((s->all_fragments[i].coding_method != MODE_COPY) &&
  1141. !((s->avctx->flags & CODEC_FLAG_GRAY) && plane)) {
  1142. if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
  1143. (s->all_fragments[i].coding_method == MODE_GOLDEN_MV))
  1144. motion_source= golden_plane;
  1145. else
  1146. motion_source= last_plane;
  1147. motion_source += s->all_fragments[i].first_pixel;
  1148. motion_halfpel_index = 0;
  1149. /* sort out the motion vector if this fragment is coded
  1150. * using a motion vector method */
  1151. if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
  1152. (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) {
  1153. int src_x, src_y;
  1154. motion_x = s->all_fragments[i].motion_x;
  1155. motion_y = s->all_fragments[i].motion_y;
  1156. if(plane){
  1157. motion_x= (motion_x>>1) | (motion_x&1);
  1158. motion_y= (motion_y>>1) | (motion_y&1);
  1159. }
  1160. src_x= (motion_x>>1) + x;
  1161. src_y= (motion_y>>1) + y;
  1162. if ((motion_x == 127) || (motion_y == 127))
  1163. av_log(s->avctx, AV_LOG_ERROR, " help! got invalid motion vector! (%X, %X)\n", motion_x, motion_y);
  1164. motion_halfpel_index = motion_x & 0x01;
  1165. motion_source += (motion_x >> 1);
  1166. motion_halfpel_index |= (motion_y & 0x01) << 1;
  1167. motion_source += ((motion_y >> 1) * stride);
  1168. if(src_x<0 || src_y<0 || src_x + 9 >= plane_width || src_y + 9 >= plane_height){
  1169. uint8_t *temp= s->edge_emu_buffer;
  1170. if(stride<0) temp -= 9*stride;
  1171. else temp += 9*stride;
  1172. ff_emulated_edge_mc(temp, motion_source, stride, 9, 9, src_x, src_y, plane_width, plane_height);
  1173. motion_source= temp;
  1174. }
  1175. }
  1176. /* first, take care of copying a block from either the
  1177. * previous or the golden frame */
  1178. if (s->all_fragments[i].coding_method != MODE_INTRA) {
  1179. /* Note, it is possible to implement all MC cases with
  1180. put_no_rnd_pixels_l2 which would look more like the
  1181. VP3 source but this would be slower as
  1182. put_no_rnd_pixels_tab is better optimzed */
  1183. if(motion_halfpel_index != 3){
  1184. s->dsp.put_no_rnd_pixels_tab[1][motion_halfpel_index](
  1185. output_plane + s->all_fragments[i].first_pixel,
  1186. motion_source, stride, 8);
  1187. }else{
  1188. int d= (motion_x ^ motion_y)>>31; // d is 0 if motion_x and _y have the same sign, else -1
  1189. s->dsp.put_no_rnd_pixels_l2[1](
  1190. output_plane + s->all_fragments[i].first_pixel,
  1191. motion_source - d,
  1192. motion_source + stride + 1 + d,
  1193. stride, 8);
  1194. }
  1195. dequantizer = s->qmat[1][plane];
  1196. }else{
  1197. dequantizer = s->qmat[0][plane];
  1198. }
  1199. /* dequantize the DCT coefficients */
  1200. if(s->avctx->idct_algo==FF_IDCT_VP3){
  1201. Coeff *coeff= s->coeffs + i;
  1202. memset(block, 0, sizeof(block));
  1203. while(coeff->next){
  1204. block[coeff->index]= coeff->coeff * dequantizer[coeff->index];
  1205. coeff= coeff->next;
  1206. }
  1207. }else{
  1208. Coeff *coeff= s->coeffs + i;
  1209. memset(block, 0, sizeof(block));
  1210. while(coeff->next){
  1211. block[coeff->index]= (coeff->coeff * dequantizer[coeff->index] + 2)>>2;
  1212. coeff= coeff->next;
  1213. }
  1214. }
  1215. /* invert DCT and place (or add) in final output */
  1216. if (s->all_fragments[i].coding_method == MODE_INTRA) {
  1217. if(s->avctx->idct_algo!=FF_IDCT_VP3)
  1218. block[0] += 128<<3;
  1219. s->dsp.idct_put(
  1220. output_plane + s->all_fragments[i].first_pixel,
  1221. stride,
  1222. block);
  1223. } else {
  1224. s->dsp.idct_add(
  1225. output_plane + s->all_fragments[i].first_pixel,
  1226. stride,
  1227. block);
  1228. }
  1229. } else {
  1230. /* copy directly from the previous frame */
  1231. s->dsp.put_pixels_tab[1][0](
  1232. output_plane + s->all_fragments[i].first_pixel,
  1233. last_plane + s->all_fragments[i].first_pixel,
  1234. stride, 8);
  1235. }
  1236. #if 0
  1237. /* perform the left edge filter if:
  1238. * - the fragment is not on the left column
  1239. * - the fragment is coded in this frame
  1240. * - the fragment is not coded in this frame but the left
  1241. * fragment is coded in this frame (this is done instead
  1242. * of a right edge filter when rendering the left fragment
  1243. * since this fragment is not available yet) */
  1244. if ((x > 0) &&
  1245. ((s->all_fragments[i].coding_method != MODE_COPY) ||
  1246. ((s->all_fragments[i].coding_method == MODE_COPY) &&
  1247. (s->all_fragments[i - 1].coding_method != MODE_COPY)) )) {
  1248. horizontal_filter(
  1249. output_plane + s->all_fragments[i].first_pixel + 7*stride,
  1250. -stride, s->bounding_values_array + 127);
  1251. }
  1252. /* perform the top edge filter if:
  1253. * - the fragment is not on the top row
  1254. * - the fragment is coded in this frame
  1255. * - the fragment is not coded in this frame but the above
  1256. * fragment is coded in this frame (this is done instead
  1257. * of a bottom edge filter when rendering the above
  1258. * fragment since this fragment is not available yet) */
  1259. if ((y > 0) &&
  1260. ((s->all_fragments[i].coding_method != MODE_COPY) ||
  1261. ((s->all_fragments[i].coding_method == MODE_COPY) &&
  1262. (s->all_fragments[i - fragment_width].coding_method != MODE_COPY)) )) {
  1263. vertical_filter(
  1264. output_plane + s->all_fragments[i].first_pixel - stride,
  1265. -stride, s->bounding_values_array + 127);
  1266. }
  1267. #endif
  1268. }
  1269. }
  1270. }
  1271. /* this looks like a good place for slice dispatch... */
  1272. /* algorithm:
  1273. * if (slice == s->macroblock_height - 1)
  1274. * dispatch (both last slice & 2nd-to-last slice);
  1275. * else if (slice > 0)
  1276. * dispatch (slice - 1);
  1277. */
  1278. emms_c();
  1279. }
  1280. static void horizontal_filter(unsigned char *first_pixel, int stride,
  1281. int *bounding_values)
  1282. {
  1283. unsigned char *end;
  1284. int filter_value;
  1285. for (end= first_pixel + 8*stride; first_pixel != end; first_pixel += stride) {
  1286. filter_value =
  1287. (first_pixel[-2] - first_pixel[ 1])
  1288. +3*(first_pixel[ 0] - first_pixel[-1]);
  1289. filter_value = bounding_values[(filter_value + 4) >> 3];
  1290. first_pixel[-1] = av_clip_uint8(first_pixel[-1] + filter_value);
  1291. first_pixel[ 0] = av_clip_uint8(first_pixel[ 0] - filter_value);
  1292. }
  1293. }
  1294. static void vertical_filter(unsigned char *first_pixel, int stride,
  1295. int *bounding_values)
  1296. {
  1297. unsigned char *end;
  1298. int filter_value;
  1299. const int nstride= -stride;
  1300. for (end= first_pixel + 8; first_pixel < end; first_pixel++) {
  1301. filter_value =
  1302. (first_pixel[2 * nstride] - first_pixel[ stride])
  1303. +3*(first_pixel[0 ] - first_pixel[nstride]);
  1304. filter_value = bounding_values[(filter_value + 4) >> 3];
  1305. first_pixel[nstride] = av_clip_uint8(first_pixel[nstride] + filter_value);
  1306. first_pixel[0] = av_clip_uint8(first_pixel[0] - filter_value);
  1307. }
  1308. }
  1309. static void apply_loop_filter(Vp3DecodeContext *s)
  1310. {
  1311. int plane;
  1312. int x, y;
  1313. int *bounding_values= s->bounding_values_array+127;
  1314. #if 0
  1315. int bounding_values_array[256];
  1316. int filter_limit;
  1317. /* find the right loop limit value */
  1318. for (x = 63; x >= 0; x--) {
  1319. if (vp31_ac_scale_factor[x] >= s->quality_index)
  1320. break;
  1321. }
  1322. filter_limit = vp31_filter_limit_values[s->quality_index];
  1323. /* set up the bounding values */
  1324. memset(bounding_values_array, 0, 256 * sizeof(int));
  1325. for (x = 0; x < filter_limit; x++) {
  1326. bounding_values[-x - filter_limit] = -filter_limit + x;
  1327. bounding_values[-x] = -x;
  1328. bounding_values[x] = x;
  1329. bounding_values[x + filter_limit] = filter_limit - x;
  1330. }
  1331. #endif
  1332. for (plane = 0; plane < 3; plane++) {
  1333. int width = s->fragment_width >> !!plane;
  1334. int height = s->fragment_height >> !!plane;
  1335. int fragment = s->fragment_start [plane];
  1336. int stride = s->current_frame.linesize[plane];
  1337. uint8_t *plane_data = s->current_frame.data [plane];
  1338. if (!s->flipped_image) stride = -stride;
  1339. for (y = 0; y < height; y++) {
  1340. for (x = 0; x < width; x++) {
  1341. /* do not perform left edge filter for left columns frags */
  1342. if ((x > 0) &&
  1343. (s->all_fragments[fragment].coding_method != MODE_COPY)) {
  1344. horizontal_filter(
  1345. plane_data + s->all_fragments[fragment].first_pixel,
  1346. stride, bounding_values);
  1347. }
  1348. /* do not perform top edge filter for top row fragments */
  1349. if ((y > 0) &&
  1350. (s->all_fragments[fragment].coding_method != MODE_COPY)) {
  1351. vertical_filter(
  1352. plane_data + s->all_fragments[fragment].first_pixel,
  1353. stride, bounding_values);
  1354. }
  1355. /* do not perform right edge filter for right column
  1356. * fragments or if right fragment neighbor is also coded
  1357. * in this frame (it will be filtered in next iteration) */
  1358. if ((x < width - 1) &&
  1359. (s->all_fragments[fragment].coding_method != MODE_COPY) &&
  1360. (s->all_fragments[fragment + 1].coding_method == MODE_COPY)) {
  1361. horizontal_filter(
  1362. plane_data + s->all_fragments[fragment + 1].first_pixel,
  1363. stride, bounding_values);
  1364. }
  1365. /* do not perform bottom edge filter for bottom row
  1366. * fragments or if bottom fragment neighbor is also coded
  1367. * in this frame (it will be filtered in the next row) */
  1368. if ((y < height - 1) &&
  1369. (s->all_fragments[fragment].coding_method != MODE_COPY) &&
  1370. (s->all_fragments[fragment + width].coding_method == MODE_COPY)) {
  1371. vertical_filter(
  1372. plane_data + s->all_fragments[fragment + width].first_pixel,
  1373. stride, bounding_values);
  1374. }
  1375. fragment++;
  1376. }
  1377. }
  1378. }
  1379. }
  1380. /*
  1381. * This function computes the first pixel addresses for each fragment.
  1382. * This function needs to be invoked after the first frame is allocated
  1383. * so that it has access to the plane strides.
  1384. */
  1385. static void vp3_calculate_pixel_addresses(Vp3DecodeContext *s)
  1386. {
  1387. #define Y_INITIAL(chroma_shift) s->flipped_image ? 1 : s->fragment_height >> chroma_shift
  1388. #define Y_FINISHED(chroma_shift) s->flipped_image ? y <= s->fragment_height >> chroma_shift : y > 0
  1389. int i, x, y;
  1390. const int y_inc = s->flipped_image ? 1 : -1;
  1391. /* figure out the first pixel addresses for each of the fragments */
  1392. /* Y plane */
  1393. i = 0;
  1394. for (y = Y_INITIAL(0); Y_FINISHED(0); y += y_inc) {
  1395. for (x = 0; x < s->fragment_width; x++) {
  1396. s->all_fragments[i++].first_pixel =
  1397. s->golden_frame.linesize[0] * y * FRAGMENT_PIXELS -
  1398. s->golden_frame.linesize[0] +
  1399. x * FRAGMENT_PIXELS;
  1400. }
  1401. }
  1402. /* U plane */
  1403. i = s->fragment_start[1];
  1404. for (y = Y_INITIAL(1); Y_FINISHED(1); y += y_inc) {
  1405. for (x = 0; x < s->fragment_width / 2; x++) {
  1406. s->all_fragments[i++].first_pixel =
  1407. s->golden_frame.linesize[1] * y * FRAGMENT_PIXELS -
  1408. s->golden_frame.linesize[1] +
  1409. x * FRAGMENT_PIXELS;
  1410. }
  1411. }
  1412. /* V plane */
  1413. i = s->fragment_start[2];
  1414. for (y = Y_INITIAL(1); Y_FINISHED(1); y += y_inc) {
  1415. for (x = 0; x < s->fragment_width / 2; x++) {
  1416. s->all_fragments[i++].first_pixel =
  1417. s->golden_frame.linesize[2] * y * FRAGMENT_PIXELS -
  1418. s->golden_frame.linesize[2] +
  1419. x * FRAGMENT_PIXELS;
  1420. }
  1421. }
  1422. }
  1423. /*
  1424. * This is the ffmpeg/libavcodec API init function.
  1425. */
  1426. static av_cold int vp3_decode_init(AVCodecContext *avctx)
  1427. {
  1428. Vp3DecodeContext *s = avctx->priv_data;
  1429. int i, inter, plane;
  1430. int c_width;
  1431. int c_height;
  1432. int y_superblock_count;
  1433. int c_superblock_count;
  1434. if (avctx->codec_tag == MKTAG('V','P','3','0'))
  1435. s->version = 0;
  1436. else
  1437. s->version = 1;
  1438. s->avctx = avctx;
  1439. s->width = (avctx->width + 15) & 0xFFFFFFF0;
  1440. s->height = (avctx->height + 15) & 0xFFFFFFF0;
  1441. avctx->pix_fmt = PIX_FMT_YUV420P;
  1442. if(avctx->idct_algo==FF_IDCT_AUTO)
  1443. avctx->idct_algo=FF_IDCT_VP3;
  1444. dsputil_init(&s->dsp, avctx);
  1445. ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
  1446. /* initialize to an impossible value which will force a recalculation
  1447. * in the first frame decode */
  1448. s->quality_index = -1;
  1449. s->y_superblock_width = (s->width + 31) / 32;
  1450. s->y_superblock_height = (s->height + 31) / 32;
  1451. y_superblock_count = s->y_superblock_width * s->y_superblock_height;
  1452. /* work out the dimensions for the C planes */
  1453. c_width = s->width / 2;
  1454. c_height = s->height / 2;
  1455. s->c_superblock_width = (c_width + 31) / 32;
  1456. s->c_superblock_height = (c_height + 31) / 32;
  1457. c_superblock_count = s->c_superblock_width * s->c_superblock_height;
  1458. s->superblock_count = y_superblock_count + (c_superblock_count * 2);
  1459. s->u_superblock_start = y_superblock_count;
  1460. s->v_superblock_start = s->u_superblock_start + c_superblock_count;
  1461. s->superblock_coding = av_malloc(s->superblock_count);
  1462. s->macroblock_width = (s->width + 15) / 16;
  1463. s->macroblock_height = (s->height + 15) / 16;
  1464. s->macroblock_count = s->macroblock_width * s->macroblock_height;
  1465. s->fragment_width = s->width / FRAGMENT_PIXELS;
  1466. s->fragment_height = s->height / FRAGMENT_PIXELS;
  1467. /* fragment count covers all 8x8 blocks for all 3 planes */
  1468. s->fragment_count = s->fragment_width * s->fragment_height * 3 / 2;
  1469. s->fragment_start[1] = s->fragment_width * s->fragment_height;
  1470. s->fragment_start[2] = s->fragment_width * s->fragment_height * 5 / 4;
  1471. s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment));
  1472. s->coeff_counts = av_malloc(s->fragment_count * sizeof(*s->coeff_counts));
  1473. s->coeffs = av_malloc(s->fragment_count * sizeof(Coeff) * 65);
  1474. s->coded_fragment_list = av_malloc(s->fragment_count * sizeof(int));
  1475. s->pixel_addresses_initialized = 0;
  1476. if (!s->theora_tables)
  1477. {
  1478. for (i = 0; i < 64; i++) {
  1479. s->coded_dc_scale_factor[i] = vp31_dc_scale_factor[i];
  1480. s->coded_ac_scale_factor[i] = vp31_ac_scale_factor[i];
  1481. s->base_matrix[0][i] = vp31_intra_y_dequant[i];
  1482. s->base_matrix[1][i] = vp31_intra_c_dequant[i];
  1483. s->base_matrix[2][i] = vp31_inter_dequant[i];
  1484. s->filter_limit_values[i] = vp31_filter_limit_values[i];
  1485. }
  1486. for(inter=0; inter<2; inter++){
  1487. for(plane=0; plane<3; plane++){
  1488. s->qr_count[inter][plane]= 1;
  1489. s->qr_size [inter][plane][0]= 63;
  1490. s->qr_base [inter][plane][0]=
  1491. s->qr_base [inter][plane][1]= 2*inter + (!!plane)*!inter;
  1492. }
  1493. }
  1494. /* init VLC tables */
  1495. for (i = 0; i < 16; i++) {
  1496. /* DC histograms */
  1497. init_vlc(&s->dc_vlc[i], 5, 32,
  1498. &dc_bias[i][0][1], 4, 2,
  1499. &dc_bias[i][0][0], 4, 2, 0);
  1500. /* group 1 AC histograms */
  1501. init_vlc(&s->ac_vlc_1[i], 5, 32,
  1502. &ac_bias_0[i][0][1], 4, 2,
  1503. &ac_bias_0[i][0][0], 4, 2, 0);
  1504. /* group 2 AC histograms */
  1505. init_vlc(&s->ac_vlc_2[i], 5, 32,
  1506. &ac_bias_1[i][0][1], 4, 2,
  1507. &ac_bias_1[i][0][0], 4, 2, 0);
  1508. /* group 3 AC histograms */
  1509. init_vlc(&s->ac_vlc_3[i], 5, 32,
  1510. &ac_bias_2[i][0][1], 4, 2,
  1511. &ac_bias_2[i][0][0], 4, 2, 0);
  1512. /* group 4 AC histograms */
  1513. init_vlc(&s->ac_vlc_4[i], 5, 32,
  1514. &ac_bias_3[i][0][1], 4, 2,
  1515. &ac_bias_3[i][0][0], 4, 2, 0);
  1516. }
  1517. } else {
  1518. for (i = 0; i < 16; i++) {
  1519. /* DC histograms */
  1520. init_vlc(&s->dc_vlc[i], 5, 32,
  1521. &s->huffman_table[i][0][1], 4, 2,
  1522. &s->huffman_table[i][0][0], 4, 2, 0);
  1523. /* group 1 AC histograms */
  1524. init_vlc(&s->ac_vlc_1[i], 5, 32,
  1525. &s->huffman_table[i+16][0][1], 4, 2,
  1526. &s->huffman_table[i+16][0][0], 4, 2, 0);
  1527. /* group 2 AC histograms */
  1528. init_vlc(&s->ac_vlc_2[i], 5, 32,
  1529. &s->huffman_table[i+16*2][0][1], 4, 2,
  1530. &s->huffman_table[i+16*2][0][0], 4, 2, 0);
  1531. /* group 3 AC histograms */
  1532. init_vlc(&s->ac_vlc_3[i], 5, 32,
  1533. &s->huffman_table[i+16*3][0][1], 4, 2,
  1534. &s->huffman_table[i+16*3][0][0], 4, 2, 0);
  1535. /* group 4 AC histograms */
  1536. init_vlc(&s->ac_vlc_4[i], 5, 32,
  1537. &s->huffman_table[i+16*4][0][1], 4, 2,
  1538. &s->huffman_table[i+16*4][0][0], 4, 2, 0);
  1539. }
  1540. }
  1541. init_vlc(&s->superblock_run_length_vlc, 6, 34,
  1542. &superblock_run_length_vlc_table[0][1], 4, 2,
  1543. &superblock_run_length_vlc_table[0][0], 4, 2, 0);
  1544. init_vlc(&s->fragment_run_length_vlc, 5, 30,
  1545. &fragment_run_length_vlc_table[0][1], 4, 2,
  1546. &fragment_run_length_vlc_table[0][0], 4, 2, 0);
  1547. init_vlc(&s->mode_code_vlc, 3, 8,
  1548. &mode_code_vlc_table[0][1], 2, 1,
  1549. &mode_code_vlc_table[0][0], 2, 1, 0);
  1550. init_vlc(&s->motion_vector_vlc, 6, 63,
  1551. &motion_vector_vlc_table[0][1], 2, 1,
  1552. &motion_vector_vlc_table[0][0], 2, 1, 0);
  1553. /* work out the block mapping tables */
  1554. s->superblock_fragments = av_malloc(s->superblock_count * 16 * sizeof(int));
  1555. s->superblock_macroblocks = av_malloc(s->superblock_count * 4 * sizeof(int));
  1556. s->macroblock_fragments = av_malloc(s->macroblock_count * 6 * sizeof(int));
  1557. s->macroblock_coding = av_malloc(s->macroblock_count + 1);
  1558. init_block_mapping(s);
  1559. for (i = 0; i < 3; i++) {
  1560. s->current_frame.data[i] = NULL;
  1561. s->last_frame.data[i] = NULL;
  1562. s->golden_frame.data[i] = NULL;
  1563. }
  1564. return 0;
  1565. }
  1566. /*
  1567. * This is the ffmpeg/libavcodec API frame decode function.
  1568. */
  1569. static int vp3_decode_frame(AVCodecContext *avctx,
  1570. void *data, int *data_size,
  1571. const uint8_t *buf, int buf_size)
  1572. {
  1573. Vp3DecodeContext *s = avctx->priv_data;
  1574. GetBitContext gb;
  1575. static int counter = 0;
  1576. int i;
  1577. init_get_bits(&gb, buf, buf_size * 8);
  1578. if (s->theora && get_bits1(&gb))
  1579. {
  1580. av_log(avctx, AV_LOG_ERROR, "Header packet passed to frame decoder, skipping\n");
  1581. return -1;
  1582. }
  1583. s->keyframe = !get_bits1(&gb);
  1584. if (!s->theora)
  1585. skip_bits(&gb, 1);
  1586. s->last_quality_index = s->quality_index;
  1587. s->nqis=0;
  1588. do{
  1589. s->qis[s->nqis++]= get_bits(&gb, 6);
  1590. } while(s->theora >= 0x030200 && s->nqis<3 && get_bits1(&gb));
  1591. s->quality_index= s->qis[0];
  1592. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1593. av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n",
  1594. s->keyframe?"key":"", counter, s->quality_index);
  1595. counter++;
  1596. if (s->quality_index != s->last_quality_index) {
  1597. init_dequantizer(s);
  1598. init_loop_filter(s);
  1599. }
  1600. if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe)
  1601. return buf_size;
  1602. if (s->keyframe) {
  1603. if (!s->theora)
  1604. {
  1605. skip_bits(&gb, 4); /* width code */
  1606. skip_bits(&gb, 4); /* height code */
  1607. if (s->version)
  1608. {
  1609. s->version = get_bits(&gb, 5);
  1610. if (counter == 1)
  1611. av_log(s->avctx, AV_LOG_DEBUG, "VP version: %d\n", s->version);
  1612. }
  1613. }
  1614. if (s->version || s->theora)
  1615. {
  1616. if (get_bits1(&gb))
  1617. av_log(s->avctx, AV_LOG_ERROR, "Warning, unsupported keyframe coding type?!\n");
  1618. skip_bits(&gb, 2); /* reserved? */
  1619. }
  1620. if (s->last_frame.data[0] == s->golden_frame.data[0]) {
  1621. if (s->golden_frame.data[0])
  1622. avctx->release_buffer(avctx, &s->golden_frame);
  1623. s->last_frame= s->golden_frame; /* ensure that we catch any access to this released frame */
  1624. } else {
  1625. if (s->golden_frame.data[0])
  1626. avctx->release_buffer(avctx, &s->golden_frame);
  1627. if (s->last_frame.data[0])
  1628. avctx->release_buffer(avctx, &s->last_frame);
  1629. }
  1630. s->golden_frame.reference = 3;
  1631. if(avctx->get_buffer(avctx, &s->golden_frame) < 0) {
  1632. av_log(s->avctx, AV_LOG_ERROR, "vp3: get_buffer() failed\n");
  1633. return -1;
  1634. }
  1635. /* golden frame is also the current frame */
  1636. s->current_frame= s->golden_frame;
  1637. /* time to figure out pixel addresses? */
  1638. if (!s->pixel_addresses_initialized)
  1639. {
  1640. vp3_calculate_pixel_addresses(s);
  1641. s->pixel_addresses_initialized = 1;
  1642. }
  1643. } else {
  1644. /* allocate a new current frame */
  1645. s->current_frame.reference = 3;
  1646. if (!s->pixel_addresses_initialized) {
  1647. av_log(s->avctx, AV_LOG_ERROR, "vp3: first frame not a keyframe\n");
  1648. return -1;
  1649. }
  1650. if(avctx->get_buffer(avctx, &s->current_frame) < 0) {
  1651. av_log(s->avctx, AV_LOG_ERROR, "vp3: get_buffer() failed\n");
  1652. return -1;
  1653. }
  1654. }
  1655. s->current_frame.qscale_table= s->qscale_table; //FIXME allocate individual tables per AVFrame
  1656. s->current_frame.qstride= 0;
  1657. init_frame(s, &gb);
  1658. if (unpack_superblocks(s, &gb)){
  1659. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n");
  1660. return -1;
  1661. }
  1662. if (unpack_modes(s, &gb)){
  1663. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n");
  1664. return -1;
  1665. }
  1666. if (unpack_vectors(s, &gb)){
  1667. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n");
  1668. return -1;
  1669. }
  1670. if (unpack_dct_coeffs(s, &gb)){
  1671. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n");
  1672. return -1;
  1673. }
  1674. reverse_dc_prediction(s, 0, s->fragment_width, s->fragment_height);
  1675. if ((avctx->flags & CODEC_FLAG_GRAY) == 0) {
  1676. reverse_dc_prediction(s, s->fragment_start[1],
  1677. s->fragment_width / 2, s->fragment_height / 2);
  1678. reverse_dc_prediction(s, s->fragment_start[2],
  1679. s->fragment_width / 2, s->fragment_height / 2);
  1680. }
  1681. for (i = 0; i < s->macroblock_height; i++)
  1682. render_slice(s, i);
  1683. apply_loop_filter(s);
  1684. *data_size=sizeof(AVFrame);
  1685. *(AVFrame*)data= s->current_frame;
  1686. /* release the last frame, if it is allocated and if it is not the
  1687. * golden frame */
  1688. if ((s->last_frame.data[0]) &&
  1689. (s->last_frame.data[0] != s->golden_frame.data[0]))
  1690. avctx->release_buffer(avctx, &s->last_frame);
  1691. /* shuffle frames (last = current) */
  1692. s->last_frame= s->current_frame;
  1693. s->current_frame.data[0]= NULL; /* ensure that we catch any access to this released frame */
  1694. return buf_size;
  1695. }
  1696. /*
  1697. * This is the ffmpeg/libavcodec API module cleanup function.
  1698. */
  1699. static av_cold int vp3_decode_end(AVCodecContext *avctx)
  1700. {
  1701. Vp3DecodeContext *s = avctx->priv_data;
  1702. int i;
  1703. av_free(s->superblock_coding);
  1704. av_free(s->all_fragments);
  1705. av_free(s->coeff_counts);
  1706. av_free(s->coeffs);
  1707. av_free(s->coded_fragment_list);
  1708. av_free(s->superblock_fragments);
  1709. av_free(s->superblock_macroblocks);
  1710. av_free(s->macroblock_fragments);
  1711. av_free(s->macroblock_coding);
  1712. for (i = 0; i < 16; i++) {
  1713. free_vlc(&s->dc_vlc[i]);
  1714. free_vlc(&s->ac_vlc_1[i]);
  1715. free_vlc(&s->ac_vlc_2[i]);
  1716. free_vlc(&s->ac_vlc_3[i]);
  1717. free_vlc(&s->ac_vlc_4[i]);
  1718. }
  1719. free_vlc(&s->superblock_run_length_vlc);
  1720. free_vlc(&s->fragment_run_length_vlc);
  1721. free_vlc(&s->mode_code_vlc);
  1722. free_vlc(&s->motion_vector_vlc);
  1723. /* release all frames */
  1724. if (s->golden_frame.data[0] && s->golden_frame.data[0] != s->last_frame.data[0])
  1725. avctx->release_buffer(avctx, &s->golden_frame);
  1726. if (s->last_frame.data[0])
  1727. avctx->release_buffer(avctx, &s->last_frame);
  1728. /* no need to release the current_frame since it will always be pointing
  1729. * to the same frame as either the golden or last frame */
  1730. return 0;
  1731. }
  1732. static int read_huffman_tree(AVCodecContext *avctx, GetBitContext *gb)
  1733. {
  1734. Vp3DecodeContext *s = avctx->priv_data;
  1735. if (get_bits1(gb)) {
  1736. int token;
  1737. if (s->entries >= 32) { /* overflow */
  1738. av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
  1739. return -1;
  1740. }
  1741. token = get_bits(gb, 5);
  1742. //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);
  1743. s->huffman_table[s->hti][token][0] = s->hbits;
  1744. s->huffman_table[s->hti][token][1] = s->huff_code_size;
  1745. s->entries++;
  1746. }
  1747. else {
  1748. if (s->huff_code_size >= 32) {/* overflow */
  1749. av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
  1750. return -1;
  1751. }
  1752. s->huff_code_size++;
  1753. s->hbits <<= 1;
  1754. read_huffman_tree(avctx, gb);
  1755. s->hbits |= 1;
  1756. read_huffman_tree(avctx, gb);
  1757. s->hbits >>= 1;
  1758. s->huff_code_size--;
  1759. }
  1760. return 0;
  1761. }
  1762. #ifdef CONFIG_THEORA_DECODER
  1763. static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb)
  1764. {
  1765. Vp3DecodeContext *s = avctx->priv_data;
  1766. int visible_width, visible_height;
  1767. s->theora = get_bits_long(gb, 24);
  1768. av_log(avctx, AV_LOG_DEBUG, "Theora bitstream version %X\n", s->theora);
  1769. /* 3.2.0 aka alpha3 has the same frame orientation as original vp3 */
  1770. /* but previous versions have the image flipped relative to vp3 */
  1771. if (s->theora < 0x030200)
  1772. {
  1773. s->flipped_image = 1;
  1774. av_log(avctx, AV_LOG_DEBUG, "Old (<alpha3) Theora bitstream, flipped image\n");
  1775. }
  1776. visible_width = s->width = get_bits(gb, 16) << 4;
  1777. visible_height = s->height = get_bits(gb, 16) << 4;
  1778. if(avcodec_check_dimensions(avctx, s->width, s->height)){
  1779. av_log(avctx, AV_LOG_ERROR, "Invalid dimensions (%dx%d)\n", s->width, s->height);
  1780. s->width= s->height= 0;
  1781. return -1;
  1782. }
  1783. if (s->theora >= 0x030400)
  1784. {
  1785. skip_bits(gb, 32); /* total number of superblocks in a frame */
  1786. // fixme, the next field is 36bits long
  1787. skip_bits(gb, 32); /* total number of blocks in a frame */
  1788. skip_bits(gb, 4); /* total number of blocks in a frame */
  1789. skip_bits(gb, 32); /* total number of macroblocks in a frame */
  1790. }
  1791. if (s->theora >= 0x030200) {
  1792. visible_width = get_bits_long(gb, 24);
  1793. visible_height = get_bits_long(gb, 24);
  1794. skip_bits(gb, 8); /* offset x */
  1795. skip_bits(gb, 8); /* offset y */
  1796. }
  1797. skip_bits(gb, 32); /* fps numerator */
  1798. skip_bits(gb, 32); /* fps denumerator */
  1799. skip_bits(gb, 24); /* aspect numerator */
  1800. skip_bits(gb, 24); /* aspect denumerator */
  1801. if (s->theora < 0x030200)
  1802. skip_bits(gb, 5); /* keyframe frequency force */
  1803. skip_bits(gb, 8); /* colorspace */
  1804. if (s->theora >= 0x030400)
  1805. skip_bits(gb, 2); /* pixel format: 420,res,422,444 */
  1806. skip_bits(gb, 24); /* bitrate */
  1807. skip_bits(gb, 6); /* quality hint */
  1808. if (s->theora >= 0x030200)
  1809. {
  1810. skip_bits(gb, 5); /* keyframe frequency force */
  1811. if (s->theora < 0x030400)
  1812. skip_bits(gb, 5); /* spare bits */
  1813. }
  1814. // align_get_bits(gb);
  1815. if ( visible_width <= s->width && visible_width > s->width-16
  1816. && visible_height <= s->height && visible_height > s->height-16)
  1817. avcodec_set_dimensions(avctx, visible_width, visible_height);
  1818. else
  1819. avcodec_set_dimensions(avctx, s->width, s->height);
  1820. return 0;
  1821. }
  1822. static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb)
  1823. {
  1824. Vp3DecodeContext *s = avctx->priv_data;
  1825. int i, n, matrices, inter, plane;
  1826. if (s->theora >= 0x030200) {
  1827. n = get_bits(gb, 3);
  1828. /* loop filter limit values table */
  1829. for (i = 0; i < 64; i++)
  1830. s->filter_limit_values[i] = get_bits(gb, n);
  1831. }
  1832. if (s->theora >= 0x030200)
  1833. n = get_bits(gb, 4) + 1;
  1834. else
  1835. n = 16;
  1836. /* quality threshold table */
  1837. for (i = 0; i < 64; i++)
  1838. s->coded_ac_scale_factor[i] = get_bits(gb, n);
  1839. if (s->theora >= 0x030200)
  1840. n = get_bits(gb, 4) + 1;
  1841. else
  1842. n = 16;
  1843. /* dc scale factor table */
  1844. for (i = 0; i < 64; i++)
  1845. s->coded_dc_scale_factor[i] = get_bits(gb, n);
  1846. if (s->theora >= 0x030200)
  1847. matrices = get_bits(gb, 9) + 1;
  1848. else
  1849. matrices = 3;
  1850. if(matrices > 384){
  1851. av_log(avctx, AV_LOG_ERROR, "invalid number of base matrixes\n");
  1852. return -1;
  1853. }
  1854. for(n=0; n<matrices; n++){
  1855. for (i = 0; i < 64; i++)
  1856. s->base_matrix[n][i]= get_bits(gb, 8);
  1857. }
  1858. for (inter = 0; inter <= 1; inter++) {
  1859. for (plane = 0; plane <= 2; plane++) {
  1860. int newqr= 1;
  1861. if (inter || plane > 0)
  1862. newqr = get_bits1(gb);
  1863. if (!newqr) {
  1864. int qtj, plj;
  1865. if(inter && get_bits1(gb)){
  1866. qtj = 0;
  1867. plj = plane;
  1868. }else{
  1869. qtj= (3*inter + plane - 1) / 3;
  1870. plj= (plane + 2) % 3;
  1871. }
  1872. s->qr_count[inter][plane]= s->qr_count[qtj][plj];
  1873. memcpy(s->qr_size[inter][plane], s->qr_size[qtj][plj], sizeof(s->qr_size[0][0]));
  1874. memcpy(s->qr_base[inter][plane], s->qr_base[qtj][plj], sizeof(s->qr_base[0][0]));
  1875. } else {
  1876. int qri= 0;
  1877. int qi = 0;
  1878. for(;;){
  1879. i= get_bits(gb, av_log2(matrices-1)+1);
  1880. if(i>= matrices){
  1881. av_log(avctx, AV_LOG_ERROR, "invalid base matrix index\n");
  1882. return -1;
  1883. }
  1884. s->qr_base[inter][plane][qri]= i;
  1885. if(qi >= 63)
  1886. break;
  1887. i = get_bits(gb, av_log2(63-qi)+1) + 1;
  1888. s->qr_size[inter][plane][qri++]= i;
  1889. qi += i;
  1890. }
  1891. if (qi > 63) {
  1892. av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi);
  1893. return -1;
  1894. }
  1895. s->qr_count[inter][plane]= qri;
  1896. }
  1897. }
  1898. }
  1899. /* Huffman tables */
  1900. for (s->hti = 0; s->hti < 80; s->hti++) {
  1901. s->entries = 0;
  1902. s->huff_code_size = 1;
  1903. if (!get_bits1(gb)) {
  1904. s->hbits = 0;
  1905. read_huffman_tree(avctx, gb);
  1906. s->hbits = 1;
  1907. read_huffman_tree(avctx, gb);
  1908. }
  1909. }
  1910. s->theora_tables = 1;
  1911. return 0;
  1912. }
  1913. static int theora_decode_init(AVCodecContext *avctx)
  1914. {
  1915. Vp3DecodeContext *s = avctx->priv_data;
  1916. GetBitContext gb;
  1917. int ptype;
  1918. uint8_t *header_start[3];
  1919. int header_len[3];
  1920. int i;
  1921. s->theora = 1;
  1922. if (!avctx->extradata_size)
  1923. {
  1924. av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n");
  1925. return -1;
  1926. }
  1927. if (ff_split_xiph_headers(avctx->extradata, avctx->extradata_size,
  1928. 42, header_start, header_len) < 0) {
  1929. av_log(avctx, AV_LOG_ERROR, "Corrupt extradata\n");
  1930. return -1;
  1931. }
  1932. for(i=0;i<3;i++) {
  1933. init_get_bits(&gb, header_start[i], header_len[i]);
  1934. ptype = get_bits(&gb, 8);
  1935. if (!(ptype & 0x80))
  1936. {
  1937. av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n");
  1938. // return -1;
  1939. }
  1940. // FIXME: Check for this as well.
  1941. skip_bits(&gb, 6*8); /* "theora" */
  1942. switch(ptype)
  1943. {
  1944. case 0x80:
  1945. theora_decode_header(avctx, &gb);
  1946. break;
  1947. case 0x81:
  1948. // FIXME: is this needed? it breaks sometimes
  1949. // theora_decode_comments(avctx, gb);
  1950. break;
  1951. case 0x82:
  1952. theora_decode_tables(avctx, &gb);
  1953. break;
  1954. default:
  1955. av_log(avctx, AV_LOG_ERROR, "Unknown Theora config packet: %d\n", ptype&~0x80);
  1956. break;
  1957. }
  1958. if(ptype != 0x81 && 8*header_len[i] != get_bits_count(&gb))
  1959. av_log(avctx, AV_LOG_WARNING, "%d bits left in packet %X\n", 8*header_len[i] - get_bits_count(&gb), ptype);
  1960. if (s->theora < 0x030200)
  1961. break;
  1962. }
  1963. vp3_decode_init(avctx);
  1964. return 0;
  1965. }
  1966. AVCodec theora_decoder = {
  1967. "theora",
  1968. CODEC_TYPE_VIDEO,
  1969. CODEC_ID_THEORA,
  1970. sizeof(Vp3DecodeContext),
  1971. theora_decode_init,
  1972. NULL,
  1973. vp3_decode_end,
  1974. vp3_decode_frame,
  1975. 0,
  1976. NULL,
  1977. .long_name = NULL_IF_CONFIG_SMALL("Theora"),
  1978. };
  1979. #endif
  1980. AVCodec vp3_decoder = {
  1981. "vp3",
  1982. CODEC_TYPE_VIDEO,
  1983. CODEC_ID_VP3,
  1984. sizeof(Vp3DecodeContext),
  1985. vp3_decode_init,
  1986. NULL,
  1987. vp3_decode_end,
  1988. vp3_decode_frame,
  1989. 0,
  1990. NULL,
  1991. .long_name = NULL_IF_CONFIG_SMALL("On2 VP3"),
  1992. };