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.

2567 lines
85KB

  1. /*
  2. *
  3. * Copyright (C) 2003 the ffmpeg project
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * VP3 Video Decoder by Mike Melanson (melanson@pcisys.net)
  20. *
  21. */
  22. /**
  23. * @file vp3.c
  24. * On2 VP3 Video Decoder
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include "common.h"
  31. #include "avcodec.h"
  32. #include "dsputil.h"
  33. #include "mpegvideo.h"
  34. #include "dsputil.h"
  35. #include "bswap.h"
  36. #include "vp3data.h"
  37. #define FRAGMENT_PIXELS 8
  38. /*
  39. * Debugging Variables
  40. *
  41. * Define one or more of the following compile-time variables to 1 to obtain
  42. * elaborate information about certain aspects of the decoding process.
  43. *
  44. * KEYFRAMES_ONLY: set this to 1 to only see keyframes (VP3 slideshow mode)
  45. * DEBUG_VP3: high-level decoding flow
  46. * DEBUG_INIT: initialization parameters
  47. * DEBUG_DEQUANTIZERS: display how the dequanization tables are built
  48. * DEBUG_BLOCK_CODING: unpacking the superblock/macroblock/fragment coding
  49. * DEBUG_MODES: unpacking the coding modes for individual fragments
  50. * DEBUG_VECTORS: display the motion vectors
  51. * DEBUG_TOKEN: display exhaustive information about each DCT token
  52. * DEBUG_VLC: display the VLCs as they are extracted from the stream
  53. * DEBUG_DC_PRED: display the process of reversing DC prediction
  54. * DEBUG_IDCT: show every detail of the IDCT process
  55. */
  56. #define KEYFRAMES_ONLY 0
  57. #define DEBUG_VP3 0
  58. #define DEBUG_INIT 0
  59. #define DEBUG_DEQUANTIZERS 0
  60. #define DEBUG_BLOCK_CODING 0
  61. #define DEBUG_MODES 0
  62. #define DEBUG_VECTORS 0
  63. #define DEBUG_TOKEN 0
  64. #define DEBUG_VLC 0
  65. #define DEBUG_DC_PRED 0
  66. #define DEBUG_IDCT 0
  67. #if DEBUG_VP3
  68. #define debug_vp3 printf
  69. #else
  70. static inline void debug_vp3(const char *format, ...) { }
  71. #endif
  72. #if DEBUG_INIT
  73. #define debug_init printf
  74. #else
  75. static inline void debug_init(const char *format, ...) { }
  76. #endif
  77. #if DEBUG_DEQUANTIZERS
  78. #define debug_dequantizers printf
  79. #else
  80. static inline void debug_dequantizers(const char *format, ...) { }
  81. #endif
  82. #if DEBUG_BLOCK_CODING
  83. #define debug_block_coding printf
  84. #else
  85. static inline void debug_block_coding(const char *format, ...) { }
  86. #endif
  87. #if DEBUG_MODES
  88. #define debug_modes printf
  89. #else
  90. static inline void debug_modes(const char *format, ...) { }
  91. #endif
  92. #if DEBUG_VECTORS
  93. #define debug_vectors printf
  94. #else
  95. static inline void debug_vectors(const char *format, ...) { }
  96. #endif
  97. #if DEBUG_TOKEN
  98. #define debug_token printf
  99. #else
  100. static inline void debug_token(const char *format, ...) { }
  101. #endif
  102. #if DEBUG_VLC
  103. #define debug_vlc printf
  104. #else
  105. static inline void debug_vlc(const char *format, ...) { }
  106. #endif
  107. #if DEBUG_DC_PRED
  108. #define debug_dc_pred printf
  109. #else
  110. static inline void debug_dc_pred(const char *format, ...) { }
  111. #endif
  112. #if DEBUG_IDCT
  113. #define debug_idct printf
  114. #else
  115. static inline void debug_idct(const char *format, ...) { }
  116. #endif
  117. typedef struct Vp3Fragment {
  118. DCTELEM coeffs[64];
  119. int coding_method;
  120. int coeff_count;
  121. int last_coeff;
  122. int motion_x;
  123. int motion_y;
  124. /* this indicates which ffmpeg put_pixels() function to use:
  125. * 00b = no halfpel, 01b = x halfpel, 10b = y halfpel, 11b = both halfpel */
  126. int motion_halfpel_index;
  127. /* address of first pixel taking into account which plane the fragment
  128. * lives on as well as the plane stride */
  129. int first_pixel;
  130. /* this is the macroblock that the fragment belongs to */
  131. int macroblock;
  132. } Vp3Fragment;
  133. #define SB_NOT_CODED 0
  134. #define SB_PARTIALLY_CODED 1
  135. #define SB_FULLY_CODED 2
  136. #define MODE_INTER_NO_MV 0
  137. #define MODE_INTRA 1
  138. #define MODE_INTER_PLUS_MV 2
  139. #define MODE_INTER_LAST_MV 3
  140. #define MODE_INTER_PRIOR_LAST 4
  141. #define MODE_USING_GOLDEN 5
  142. #define MODE_GOLDEN_MV 6
  143. #define MODE_INTER_FOURMV 7
  144. #define CODING_MODE_COUNT 8
  145. /* special internal mode */
  146. #define MODE_COPY 8
  147. /* There are 6 preset schemes, plus a free-form scheme */
  148. static int ModeAlphabet[7][CODING_MODE_COUNT] =
  149. {
  150. /* this is the custom scheme */
  151. { 0, 0, 0, 0, 0, 0, 0, 0 },
  152. /* scheme 1: Last motion vector dominates */
  153. { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
  154. MODE_INTER_PLUS_MV, MODE_INTER_NO_MV,
  155. MODE_INTRA, MODE_USING_GOLDEN,
  156. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  157. /* scheme 2 */
  158. { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
  159. MODE_INTER_NO_MV, MODE_INTER_PLUS_MV,
  160. MODE_INTRA, MODE_USING_GOLDEN,
  161. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  162. /* scheme 3 */
  163. { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
  164. MODE_INTER_PRIOR_LAST, MODE_INTER_NO_MV,
  165. MODE_INTRA, MODE_USING_GOLDEN,
  166. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  167. /* scheme 4 */
  168. { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
  169. MODE_INTER_NO_MV, MODE_INTER_PRIOR_LAST,
  170. MODE_INTRA, MODE_USING_GOLDEN,
  171. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  172. /* scheme 5: No motion vector dominates */
  173. { MODE_INTER_NO_MV, MODE_INTER_LAST_MV,
  174. MODE_INTER_PRIOR_LAST, MODE_INTER_PLUS_MV,
  175. MODE_INTRA, MODE_USING_GOLDEN,
  176. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  177. /* scheme 6 */
  178. { MODE_INTER_NO_MV, MODE_USING_GOLDEN,
  179. MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
  180. MODE_INTER_PLUS_MV, MODE_INTRA,
  181. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  182. };
  183. #define MIN_DEQUANT_VAL 2
  184. typedef struct Vp3DecodeContext {
  185. AVCodecContext *avctx;
  186. int width, height;
  187. AVFrame golden_frame;
  188. AVFrame last_frame;
  189. AVFrame current_frame;
  190. int keyframe;
  191. DSPContext dsp;
  192. int quality_index;
  193. int last_quality_index;
  194. int superblock_count;
  195. int superblock_width;
  196. int superblock_height;
  197. int y_superblock_width;
  198. int y_superblock_height;
  199. int c_superblock_width;
  200. int c_superblock_height;
  201. int u_superblock_start;
  202. int v_superblock_start;
  203. unsigned char *superblock_coding;
  204. int macroblock_count;
  205. int macroblock_width;
  206. int macroblock_height;
  207. int fragment_count;
  208. int fragment_width;
  209. int fragment_height;
  210. Vp3Fragment *all_fragments;
  211. int u_fragment_start;
  212. int v_fragment_start;
  213. /* this is a list of indices into the all_fragments array indicating
  214. * which of the fragments are coded */
  215. int *coded_fragment_list;
  216. int coded_fragment_list_index;
  217. int pixel_addresses_inited;
  218. VLC dc_vlc[16];
  219. VLC ac_vlc_1[16];
  220. VLC ac_vlc_2[16];
  221. VLC ac_vlc_3[16];
  222. VLC ac_vlc_4[16];
  223. int16_t intra_y_dequant[64];
  224. int16_t intra_c_dequant[64];
  225. int16_t inter_dequant[64];
  226. /* This table contains superblock_count * 16 entries. Each set of 16
  227. * numbers corresponds to the fragment indices 0..15 of the superblock.
  228. * An entry will be -1 to indicate that no entry corresponds to that
  229. * index. */
  230. int *superblock_fragments;
  231. /* This table contains superblock_count * 4 entries. Each set of 4
  232. * numbers corresponds to the macroblock indices 0..3 of the superblock.
  233. * An entry will be -1 to indicate that no entry corresponds to that
  234. * index. */
  235. int *superblock_macroblocks;
  236. /* This table contains macroblock_count * 6 entries. Each set of 6
  237. * numbers corresponds to the fragment indices 0..5 which comprise
  238. * the macroblock (4 Y fragments and 2 C fragments). */
  239. int *macroblock_fragments;
  240. /* This is an array that indicates how a particular macroblock
  241. * is coded. */
  242. unsigned char *macroblock_coding;
  243. int first_coded_y_fragment;
  244. int first_coded_c_fragment;
  245. int last_coded_y_fragment;
  246. int last_coded_c_fragment;
  247. } Vp3DecodeContext;
  248. /************************************************************************
  249. * VP3 specific functions
  250. ************************************************************************/
  251. /*
  252. * This function sets up all of the various blocks mappings:
  253. * superblocks <-> fragments, macroblocks <-> fragments,
  254. * superblocks <-> macroblocks
  255. *
  256. * Returns 0 is successful; returns 1 if *anything* went wrong.
  257. */
  258. static int init_block_mapping(Vp3DecodeContext *s)
  259. {
  260. int i, j;
  261. signed int hilbert_walk_y[16];
  262. signed int hilbert_walk_c[16];
  263. signed int hilbert_walk_mb[4];
  264. int current_fragment = 0;
  265. int current_width = 0;
  266. int current_height = 0;
  267. int right_edge = 0;
  268. int bottom_edge = 0;
  269. int superblock_row_inc = 0;
  270. int *hilbert = NULL;
  271. int mapping_index = 0;
  272. int current_macroblock;
  273. int c_fragment;
  274. signed char travel_width[16] = {
  275. 1, 1, 0, -1,
  276. 0, 0, 1, 0,
  277. 1, 0, 1, 0,
  278. 0, -1, 0, 1
  279. };
  280. signed char travel_height[16] = {
  281. 0, 0, 1, 0,
  282. 1, 1, 0, -1,
  283. 0, 1, 0, -1,
  284. -1, 0, -1, 0
  285. };
  286. signed char travel_width_mb[4] = {
  287. 1, 0, 1, 0
  288. };
  289. signed char travel_height_mb[4] = {
  290. 0, 1, 0, -1
  291. };
  292. debug_vp3(" vp3: initialize block mapping tables\n");
  293. /* figure out hilbert pattern per these frame dimensions */
  294. hilbert_walk_y[0] = 1;
  295. hilbert_walk_y[1] = 1;
  296. hilbert_walk_y[2] = s->fragment_width;
  297. hilbert_walk_y[3] = -1;
  298. hilbert_walk_y[4] = s->fragment_width;
  299. hilbert_walk_y[5] = s->fragment_width;
  300. hilbert_walk_y[6] = 1;
  301. hilbert_walk_y[7] = -s->fragment_width;
  302. hilbert_walk_y[8] = 1;
  303. hilbert_walk_y[9] = s->fragment_width;
  304. hilbert_walk_y[10] = 1;
  305. hilbert_walk_y[11] = -s->fragment_width;
  306. hilbert_walk_y[12] = -s->fragment_width;
  307. hilbert_walk_y[13] = -1;
  308. hilbert_walk_y[14] = -s->fragment_width;
  309. hilbert_walk_y[15] = 1;
  310. hilbert_walk_c[0] = 1;
  311. hilbert_walk_c[1] = 1;
  312. hilbert_walk_c[2] = s->fragment_width / 2;
  313. hilbert_walk_c[3] = -1;
  314. hilbert_walk_c[4] = s->fragment_width / 2;
  315. hilbert_walk_c[5] = s->fragment_width / 2;
  316. hilbert_walk_c[6] = 1;
  317. hilbert_walk_c[7] = -s->fragment_width / 2;
  318. hilbert_walk_c[8] = 1;
  319. hilbert_walk_c[9] = s->fragment_width / 2;
  320. hilbert_walk_c[10] = 1;
  321. hilbert_walk_c[11] = -s->fragment_width / 2;
  322. hilbert_walk_c[12] = -s->fragment_width / 2;
  323. hilbert_walk_c[13] = -1;
  324. hilbert_walk_c[14] = -s->fragment_width / 2;
  325. hilbert_walk_c[15] = 1;
  326. hilbert_walk_mb[0] = 1;
  327. hilbert_walk_mb[1] = s->macroblock_width;
  328. hilbert_walk_mb[2] = 1;
  329. hilbert_walk_mb[3] = -s->macroblock_width;
  330. /* iterate through each superblock (all planes) and map the fragments */
  331. for (i = 0; i < s->superblock_count; i++) {
  332. debug_init(" superblock %d (u starts @ %d, v starts @ %d)\n",
  333. i, s->u_superblock_start, s->v_superblock_start);
  334. /* time to re-assign the limits? */
  335. if (i == 0) {
  336. /* start of Y superblocks */
  337. right_edge = s->fragment_width;
  338. bottom_edge = s->fragment_height;
  339. current_width = -1;
  340. current_height = 0;
  341. superblock_row_inc = 3 * s->fragment_width -
  342. (s->y_superblock_width * 4 - s->fragment_width);
  343. hilbert = hilbert_walk_y;
  344. /* the first operation for this variable is to advance by 1 */
  345. current_fragment = -1;
  346. } else if (i == s->u_superblock_start) {
  347. /* start of U superblocks */
  348. right_edge = s->fragment_width / 2;
  349. bottom_edge = s->fragment_height / 2;
  350. current_width = -1;
  351. current_height = 0;
  352. superblock_row_inc = 3 * (s->fragment_width / 2) -
  353. (s->c_superblock_width * 4 - s->fragment_width / 2);
  354. hilbert = hilbert_walk_c;
  355. /* the first operation for this variable is to advance by 1 */
  356. current_fragment = s->u_fragment_start - 1;
  357. } else if (i == s->v_superblock_start) {
  358. /* start of V superblocks */
  359. right_edge = s->fragment_width / 2;
  360. bottom_edge = s->fragment_height / 2;
  361. current_width = -1;
  362. current_height = 0;
  363. superblock_row_inc = 3 * (s->fragment_width / 2) -
  364. (s->c_superblock_width * 4 - s->fragment_width / 2);
  365. hilbert = hilbert_walk_c;
  366. /* the first operation for this variable is to advance by 1 */
  367. current_fragment = s->v_fragment_start - 1;
  368. }
  369. if (current_width >= right_edge - 1) {
  370. /* reset width and move to next superblock row */
  371. current_width = -1;
  372. current_height += 4;
  373. /* fragment is now at the start of a new superblock row */
  374. current_fragment += superblock_row_inc;
  375. }
  376. /* iterate through all 16 fragments in a superblock */
  377. for (j = 0; j < 16; j++) {
  378. current_fragment += hilbert[j];
  379. current_width += travel_width[j];
  380. current_height += travel_height[j];
  381. /* check if the fragment is in bounds */
  382. if ((current_width < right_edge) &&
  383. (current_height < bottom_edge)) {
  384. s->superblock_fragments[mapping_index] = current_fragment;
  385. debug_init(" mapping fragment %d to superblock %d, position %d (%d/%d x %d/%d)\n",
  386. s->superblock_fragments[mapping_index], i, j,
  387. current_width, right_edge, current_height, bottom_edge);
  388. } else {
  389. s->superblock_fragments[mapping_index] = -1;
  390. debug_init(" superblock %d, position %d has no fragment (%d/%d x %d/%d)\n",
  391. i, j,
  392. current_width, right_edge, current_height, bottom_edge);
  393. }
  394. mapping_index++;
  395. }
  396. }
  397. /* initialize the superblock <-> macroblock mapping; iterate through
  398. * all of the Y plane superblocks to build this mapping */
  399. right_edge = s->macroblock_width;
  400. bottom_edge = s->macroblock_height;
  401. current_width = -1;
  402. current_height = 0;
  403. superblock_row_inc = s->macroblock_width -
  404. (s->y_superblock_width * 2 - s->macroblock_width);;
  405. hilbert = hilbert_walk_mb;
  406. mapping_index = 0;
  407. current_macroblock = -1;
  408. for (i = 0; i < s->u_superblock_start; i++) {
  409. if (current_width >= right_edge - 1) {
  410. /* reset width and move to next superblock row */
  411. current_width = -1;
  412. current_height += 2;
  413. /* macroblock is now at the start of a new superblock row */
  414. current_macroblock += superblock_row_inc;
  415. }
  416. /* iterate through each potential macroblock in the superblock */
  417. for (j = 0; j < 4; j++) {
  418. current_macroblock += hilbert_walk_mb[j];
  419. current_width += travel_width_mb[j];
  420. current_height += travel_height_mb[j];
  421. /* check if the macroblock is in bounds */
  422. if ((current_width < right_edge) &&
  423. (current_height < bottom_edge)) {
  424. s->superblock_macroblocks[mapping_index] = current_macroblock;
  425. debug_init(" mapping macroblock %d to superblock %d, position %d (%d/%d x %d/%d)\n",
  426. s->superblock_macroblocks[mapping_index], i, j,
  427. current_width, right_edge, current_height, bottom_edge);
  428. } else {
  429. s->superblock_macroblocks[mapping_index] = -1;
  430. debug_init(" superblock %d, position %d has no macroblock (%d/%d x %d/%d)\n",
  431. i, j,
  432. current_width, right_edge, current_height, bottom_edge);
  433. }
  434. mapping_index++;
  435. }
  436. }
  437. /* initialize the macroblock <-> fragment mapping */
  438. current_fragment = 0;
  439. current_macroblock = 0;
  440. mapping_index = 0;
  441. for (i = 0; i < s->fragment_height; i += 2) {
  442. for (j = 0; j < s->fragment_width; j += 2) {
  443. debug_init(" macroblock %d contains fragments: ", current_macroblock);
  444. s->all_fragments[current_fragment].macroblock = current_macroblock;
  445. s->macroblock_fragments[mapping_index++] = current_fragment;
  446. debug_init("%d ", current_fragment);
  447. if (j + 1 < s->fragment_width) {
  448. s->all_fragments[current_fragment + 1].macroblock = current_macroblock;
  449. s->macroblock_fragments[mapping_index++] = current_fragment + 1;
  450. debug_init("%d ", current_fragment + 1);
  451. } else
  452. s->macroblock_fragments[mapping_index++] = -1;
  453. if (i + 1 < s->fragment_height) {
  454. s->all_fragments[current_fragment + s->fragment_width].macroblock =
  455. current_macroblock;
  456. s->macroblock_fragments[mapping_index++] =
  457. current_fragment + s->fragment_width;
  458. debug_init("%d ", current_fragment + s->fragment_width);
  459. } else
  460. s->macroblock_fragments[mapping_index++] = -1;
  461. if ((j + 1 < s->fragment_width) && (i + 1 < s->fragment_height)) {
  462. s->all_fragments[current_fragment + s->fragment_width + 1].macroblock =
  463. current_macroblock;
  464. s->macroblock_fragments[mapping_index++] =
  465. current_fragment + s->fragment_width + 1;
  466. debug_init("%d ", current_fragment + s->fragment_width + 1);
  467. } else
  468. s->macroblock_fragments[mapping_index++] = -1;
  469. /* C planes */
  470. c_fragment = s->u_fragment_start +
  471. (i * s->fragment_width / 4) + (j / 2);
  472. s->all_fragments[c_fragment].macroblock = s->macroblock_count;
  473. s->macroblock_fragments[mapping_index++] = c_fragment;
  474. debug_init("%d ", c_fragment);
  475. c_fragment = s->v_fragment_start +
  476. (i * s->fragment_width / 4) + (j / 2);
  477. s->all_fragments[c_fragment].macroblock = s->macroblock_count;
  478. s->macroblock_fragments[mapping_index++] = c_fragment;
  479. debug_init("%d ", c_fragment);
  480. debug_init("\n");
  481. if (j + 2 <= s->fragment_width)
  482. current_fragment += 2;
  483. else
  484. current_fragment++;
  485. current_macroblock++;
  486. }
  487. current_fragment += s->fragment_width;
  488. }
  489. return 0; /* successful path out */
  490. }
  491. /*
  492. * This function unpacks a single token (which should be in the range 0..31)
  493. * and returns a zero run (number of zero coefficients in current DCT matrix
  494. * before next non-zero coefficient), the next DCT coefficient, and the
  495. * number of consecutive, non-EOB'd DCT blocks to EOB.
  496. */
  497. static void unpack_token(GetBitContext *gb, int token, int *zero_run,
  498. DCTELEM *coeff, int *eob_run)
  499. {
  500. int sign;
  501. *zero_run = 0;
  502. *eob_run = 0;
  503. *coeff = 0;
  504. debug_token(" vp3 token %d: ", token);
  505. switch (token) {
  506. case 0:
  507. debug_token("DCT_EOB_TOKEN, EOB next block\n");
  508. *eob_run = 1;
  509. break;
  510. case 1:
  511. debug_token("DCT_EOB_PAIR_TOKEN, EOB next 2 blocks\n");
  512. *eob_run = 2;
  513. break;
  514. case 2:
  515. debug_token("DCT_EOB_TRIPLE_TOKEN, EOB next 3 blocks\n");
  516. *eob_run = 3;
  517. break;
  518. case 3:
  519. debug_token("DCT_REPEAT_RUN_TOKEN, ");
  520. *eob_run = get_bits(gb, 2) + 4;
  521. debug_token("EOB the next %d blocks\n", *eob_run);
  522. break;
  523. case 4:
  524. debug_token("DCT_REPEAT_RUN2_TOKEN, ");
  525. *eob_run = get_bits(gb, 3) + 8;
  526. debug_token("EOB the next %d blocks\n", *eob_run);
  527. break;
  528. case 5:
  529. debug_token("DCT_REPEAT_RUN3_TOKEN, ");
  530. *eob_run = get_bits(gb, 4) + 16;
  531. debug_token("EOB the next %d blocks\n", *eob_run);
  532. break;
  533. case 6:
  534. debug_token("DCT_REPEAT_RUN4_TOKEN, ");
  535. *eob_run = get_bits(gb, 12);
  536. debug_token("EOB the next %d blocks\n", *eob_run);
  537. break;
  538. case 7:
  539. debug_token("DCT_SHORT_ZRL_TOKEN, ");
  540. /* note that this token actually indicates that (3 extra bits) + 1 0s
  541. * should be output; this case specifies a run of (3 EBs) 0s and a
  542. * coefficient of 0. */
  543. *zero_run = get_bits(gb, 3);
  544. *coeff = 0;
  545. debug_token("skip the next %d positions in output matrix\n", *zero_run + 1);
  546. break;
  547. case 8:
  548. debug_token("DCT_ZRL_TOKEN, ");
  549. /* note that this token actually indicates that (6 extra bits) + 1 0s
  550. * should be output; this case specifies a run of (6 EBs) 0s and a
  551. * coefficient of 0. */
  552. *zero_run = get_bits(gb, 6);
  553. *coeff = 0;
  554. debug_token("skip the next %d positions in output matrix\n", *zero_run + 1);
  555. break;
  556. case 9:
  557. debug_token("ONE_TOKEN, output 1\n");
  558. *coeff = 1;
  559. break;
  560. case 10:
  561. debug_token("MINUS_ONE_TOKEN, output -1\n");
  562. *coeff = -1;
  563. break;
  564. case 11:
  565. debug_token("TWO_TOKEN, output 2\n");
  566. *coeff = 2;
  567. break;
  568. case 12:
  569. debug_token("MINUS_TWO_TOKEN, output -2\n");
  570. *coeff = -2;
  571. break;
  572. case 13:
  573. case 14:
  574. case 15:
  575. case 16:
  576. debug_token("LOW_VAL_TOKENS, ");
  577. if (get_bits(gb, 1))
  578. *coeff = -(3 + (token - 13));
  579. else
  580. *coeff = 3 + (token - 13);
  581. debug_token("output %d\n", *coeff);
  582. break;
  583. case 17:
  584. debug_token("DCT_VAL_CATEGORY3, ");
  585. sign = get_bits(gb, 1);
  586. *coeff = 7 + get_bits(gb, 1);
  587. if (sign)
  588. *coeff = -(*coeff);
  589. debug_token("output %d\n", *coeff);
  590. break;
  591. case 18:
  592. debug_token("DCT_VAL_CATEGORY4, ");
  593. sign = get_bits(gb, 1);
  594. *coeff = 9 + get_bits(gb, 2);
  595. if (sign)
  596. *coeff = -(*coeff);
  597. debug_token("output %d\n", *coeff);
  598. break;
  599. case 19:
  600. debug_token("DCT_VAL_CATEGORY5, ");
  601. sign = get_bits(gb, 1);
  602. *coeff = 13 + get_bits(gb, 3);
  603. if (sign)
  604. *coeff = -(*coeff);
  605. debug_token("output %d\n", *coeff);
  606. break;
  607. case 20:
  608. debug_token("DCT_VAL_CATEGORY6, ");
  609. sign = get_bits(gb, 1);
  610. *coeff = 21 + get_bits(gb, 4);
  611. if (sign)
  612. *coeff = -(*coeff);
  613. debug_token("output %d\n", *coeff);
  614. break;
  615. case 21:
  616. debug_token("DCT_VAL_CATEGORY7, ");
  617. sign = get_bits(gb, 1);
  618. *coeff = 37 + get_bits(gb, 5);
  619. if (sign)
  620. *coeff = -(*coeff);
  621. debug_token("output %d\n", *coeff);
  622. break;
  623. case 22:
  624. debug_token("DCT_VAL_CATEGORY8, ");
  625. sign = get_bits(gb, 1);
  626. *coeff = 69 + get_bits(gb, 9);
  627. if (sign)
  628. *coeff = -(*coeff);
  629. debug_token("output %d\n", *coeff);
  630. break;
  631. case 23:
  632. case 24:
  633. case 25:
  634. case 26:
  635. case 27:
  636. debug_token("DCT_RUN_CATEGORY1, ");
  637. *zero_run = token - 22;
  638. if (get_bits(gb, 1))
  639. *coeff = -1;
  640. else
  641. *coeff = 1;
  642. debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
  643. break;
  644. case 28:
  645. debug_token("DCT_RUN_CATEGORY1B, ");
  646. if (get_bits(gb, 1))
  647. *coeff = -1;
  648. else
  649. *coeff = 1;
  650. *zero_run = 6 + get_bits(gb, 2);
  651. debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
  652. break;
  653. case 29:
  654. debug_token("DCT_RUN_CATEGORY1C, ");
  655. if (get_bits(gb, 1))
  656. *coeff = -1;
  657. else
  658. *coeff = 1;
  659. *zero_run = 10 + get_bits(gb, 3);
  660. debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
  661. break;
  662. case 30:
  663. debug_token("DCT_RUN_CATEGORY2, ");
  664. sign = get_bits(gb, 1);
  665. *coeff = 2 + get_bits(gb, 1);
  666. if (sign)
  667. *coeff = -(*coeff);
  668. *zero_run = 1;
  669. debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
  670. break;
  671. case 31:
  672. debug_token("DCT_RUN_CATEGORY2, ");
  673. sign = get_bits(gb, 1);
  674. *coeff = 2 + get_bits(gb, 1);
  675. if (sign)
  676. *coeff = -(*coeff);
  677. *zero_run = 2 + get_bits(gb, 1);
  678. debug_token("output %d 0s, then %d\n", *zero_run, *coeff);
  679. break;
  680. default:
  681. printf (" vp3: help! Got a bad token: %d > 31\n", token);
  682. break;
  683. }
  684. }
  685. /*
  686. * This function wipes out all of the fragment data.
  687. */
  688. static void init_frame(Vp3DecodeContext *s, GetBitContext *gb)
  689. {
  690. int i;
  691. /* zero out all of the fragment information */
  692. s->coded_fragment_list_index = 0;
  693. for (i = 0; i < s->fragment_count; i++) {
  694. memset(s->all_fragments[i].coeffs, 0, 64 * sizeof(DCTELEM));
  695. s->all_fragments[i].coeff_count = 0;
  696. s->all_fragments[i].last_coeff = 0;
  697. }
  698. }
  699. /*
  700. * This function sets of the dequantization tables used for a particular
  701. * frame.
  702. */
  703. static void init_dequantizer(Vp3DecodeContext *s)
  704. {
  705. int quality_scale = vp31_quality_threshold[s->quality_index];
  706. int dc_scale_factor = vp31_dc_scale_factor[s->quality_index];
  707. int i, j;
  708. debug_vp3(" vp3: initializing dequantization tables\n");
  709. /*
  710. * Scale dequantizers:
  711. *
  712. * quantizer * sf
  713. * --------------
  714. * 100
  715. *
  716. * where sf = dc_scale_factor for DC quantizer
  717. * or quality_scale for AC quantizer
  718. *
  719. * Then, saturate the result to a lower limit of MIN_DEQUANT_VAL.
  720. */
  721. #define SCALER 1
  722. /* scale DC quantizers */
  723. s->intra_y_dequant[0] = vp31_intra_y_dequant[0] * dc_scale_factor / 100;
  724. if (s->intra_y_dequant[0] < MIN_DEQUANT_VAL * 2)
  725. s->intra_y_dequant[0] = MIN_DEQUANT_VAL * 2;
  726. s->intra_y_dequant[0] *= SCALER;
  727. s->intra_c_dequant[0] = vp31_intra_c_dequant[0] * dc_scale_factor / 100;
  728. if (s->intra_c_dequant[0] < MIN_DEQUANT_VAL * 2)
  729. s->intra_c_dequant[0] = MIN_DEQUANT_VAL * 2;
  730. s->intra_c_dequant[0] *= SCALER;
  731. s->inter_dequant[0] = vp31_inter_dequant[0] * dc_scale_factor / 100;
  732. if (s->inter_dequant[0] < MIN_DEQUANT_VAL * 4)
  733. s->inter_dequant[0] = MIN_DEQUANT_VAL * 4;
  734. s->inter_dequant[0] *= SCALER;
  735. /* scale AC quantizers, zigzag at the same time in preparation for
  736. * the dequantization phase */
  737. for (i = 1; i < 64; i++) {
  738. j = zigzag_index[i];
  739. s->intra_y_dequant[j] = vp31_intra_y_dequant[i] * quality_scale / 100;
  740. if (s->intra_y_dequant[j] < MIN_DEQUANT_VAL)
  741. s->intra_y_dequant[j] = MIN_DEQUANT_VAL;
  742. s->intra_y_dequant[j] *= SCALER;
  743. s->intra_c_dequant[j] = vp31_intra_c_dequant[i] * quality_scale / 100;
  744. if (s->intra_c_dequant[j] < MIN_DEQUANT_VAL)
  745. s->intra_c_dequant[j] = MIN_DEQUANT_VAL;
  746. s->intra_c_dequant[j] *= SCALER;
  747. s->inter_dequant[j] = vp31_inter_dequant[i] * quality_scale / 100;
  748. if (s->inter_dequant[j] < MIN_DEQUANT_VAL * 2)
  749. s->inter_dequant[j] = MIN_DEQUANT_VAL * 2;
  750. s->inter_dequant[j] *= SCALER;
  751. }
  752. /* print debug information as requested */
  753. debug_dequantizers("intra Y dequantizers:\n");
  754. for (i = 0; i < 8; i++) {
  755. for (j = i * 8; j < i * 8 + 8; j++) {
  756. debug_dequantizers(" %4d,", s->intra_y_dequant[j]);
  757. }
  758. debug_dequantizers("\n");
  759. }
  760. debug_dequantizers("\n");
  761. debug_dequantizers("intra C dequantizers:\n");
  762. for (i = 0; i < 8; i++) {
  763. for (j = i * 8; j < i * 8 + 8; j++) {
  764. debug_dequantizers(" %4d,", s->intra_c_dequant[j]);
  765. }
  766. debug_dequantizers("\n");
  767. }
  768. debug_dequantizers("\n");
  769. debug_dequantizers("interframe dequantizers:\n");
  770. for (i = 0; i < 8; i++) {
  771. for (j = i * 8; j < i * 8 + 8; j++) {
  772. debug_dequantizers(" %4d,", s->inter_dequant[j]);
  773. }
  774. debug_dequantizers("\n");
  775. }
  776. debug_dequantizers("\n");
  777. }
  778. /*
  779. * This function is used to fetch runs of 1s or 0s from the bitstream for
  780. * use in determining which superblocks are fully and partially coded.
  781. *
  782. * Codeword RunLength
  783. * 0 1
  784. * 10x 2-3
  785. * 110x 4-5
  786. * 1110xx 6-9
  787. * 11110xxx 10-17
  788. * 111110xxxx 18-33
  789. * 111111xxxxxxxxxxxx 34-4129
  790. */
  791. static int get_superblock_run_length(GetBitContext *gb)
  792. {
  793. if (get_bits(gb, 1) == 0)
  794. return 1;
  795. else if (get_bits(gb, 1) == 0)
  796. return (2 + get_bits(gb, 1));
  797. else if (get_bits(gb, 1) == 0)
  798. return (4 + get_bits(gb, 1));
  799. else if (get_bits(gb, 1) == 0)
  800. return (6 + get_bits(gb, 2));
  801. else if (get_bits(gb, 1) == 0)
  802. return (10 + get_bits(gb, 3));
  803. else if (get_bits(gb, 1) == 0)
  804. return (18 + get_bits(gb, 4));
  805. else
  806. return (34 + get_bits(gb, 12));
  807. }
  808. /*
  809. * This function is used to fetch runs of 1s or 0s from the bitstream for
  810. * use in determining which particular fragments are coded.
  811. *
  812. * Codeword RunLength
  813. * 0x 1-2
  814. * 10x 3-4
  815. * 110x 5-6
  816. * 1110xx 7-10
  817. * 11110xx 11-14
  818. * 11111xxxx 15-30
  819. */
  820. static int get_fragment_run_length(GetBitContext *gb)
  821. {
  822. if (get_bits(gb, 1) == 0)
  823. return (1 + get_bits(gb, 1));
  824. else if (get_bits(gb, 1) == 0)
  825. return (3 + get_bits(gb, 1));
  826. else if (get_bits(gb, 1) == 0)
  827. return (5 + get_bits(gb, 1));
  828. else if (get_bits(gb, 1) == 0)
  829. return (7 + get_bits(gb, 2));
  830. else if (get_bits(gb, 1) == 0)
  831. return (11 + get_bits(gb, 2));
  832. else
  833. return (15 + get_bits(gb, 4));
  834. }
  835. /*
  836. * This function decodes a VLC from the bitstream and returns a number
  837. * that ranges from 0..7. The number indicates which of the 8 coding
  838. * modes to use.
  839. *
  840. * VLC Number
  841. * 0 0
  842. * 10 1
  843. * 110 2
  844. * 1110 3
  845. * 11110 4
  846. * 111110 5
  847. * 1111110 6
  848. * 1111111 7
  849. *
  850. */
  851. static int get_mode_code(GetBitContext *gb)
  852. {
  853. if (get_bits(gb, 1) == 0)
  854. return 0;
  855. else if (get_bits(gb, 1) == 0)
  856. return 1;
  857. else if (get_bits(gb, 1) == 0)
  858. return 2;
  859. else if (get_bits(gb, 1) == 0)
  860. return 3;
  861. else if (get_bits(gb, 1) == 0)
  862. return 4;
  863. else if (get_bits(gb, 1) == 0)
  864. return 5;
  865. else if (get_bits(gb, 1) == 0)
  866. return 6;
  867. else
  868. return 7;
  869. }
  870. /*
  871. * This function extracts a motion vector from the bitstream using a VLC
  872. * scheme. 3 bits are fetched from the bitstream and 1 of 8 actions is
  873. * taken depending on the value on those 3 bits:
  874. *
  875. * 0: return 0
  876. * 1: return 1
  877. * 2: return -1
  878. * 3: if (next bit is 1) return -2, else return 2
  879. * 4: if (next bit is 1) return -3, else return 3
  880. * 5: return 4 + (next 2 bits), next bit is sign
  881. * 6: return 8 + (next 3 bits), next bit is sign
  882. * 7: return 16 + (next 4 bits), next bit is sign
  883. */
  884. static int get_motion_vector_vlc(GetBitContext *gb)
  885. {
  886. int bits;
  887. bits = get_bits(gb, 3);
  888. switch(bits) {
  889. case 0:
  890. bits = 0;
  891. break;
  892. case 1:
  893. bits = 1;
  894. break;
  895. case 2:
  896. bits = -1;
  897. break;
  898. case 3:
  899. if (get_bits(gb, 1) == 0)
  900. bits = 2;
  901. else
  902. bits = -2;
  903. break;
  904. case 4:
  905. if (get_bits(gb, 1) == 0)
  906. bits = 3;
  907. else
  908. bits = -3;
  909. break;
  910. case 5:
  911. bits = 4 + get_bits(gb, 2);
  912. if (get_bits(gb, 1) == 1)
  913. bits = -bits;
  914. break;
  915. case 6:
  916. bits = 8 + get_bits(gb, 3);
  917. if (get_bits(gb, 1) == 1)
  918. bits = -bits;
  919. break;
  920. case 7:
  921. bits = 16 + get_bits(gb, 4);
  922. if (get_bits(gb, 1) == 1)
  923. bits = -bits;
  924. break;
  925. }
  926. return bits;
  927. }
  928. /*
  929. * This function fetches a 5-bit number from the stream followed by
  930. * a sign and calls it a motion vector.
  931. */
  932. static int get_motion_vector_fixed(GetBitContext *gb)
  933. {
  934. int bits;
  935. bits = get_bits(gb, 5);
  936. if (get_bits(gb, 1) == 1)
  937. bits = -bits;
  938. return bits;
  939. }
  940. /*
  941. * This function unpacks all of the superblock/macroblock/fragment coding
  942. * information from the bitstream.
  943. */
  944. static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
  945. {
  946. int bit = 0;
  947. int current_superblock = 0;
  948. int current_run = 0;
  949. int decode_fully_flags = 0;
  950. int decode_partial_blocks = 0;
  951. int i, j;
  952. int current_fragment;
  953. debug_vp3(" vp3: unpacking superblock coding\n");
  954. if (s->keyframe) {
  955. debug_vp3(" keyframe-- all superblocks are fully coded\n");
  956. memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);
  957. } else {
  958. /* unpack the list of partially-coded superblocks */
  959. bit = get_bits(gb, 1);
  960. /* toggle the bit because as soon as the first run length is
  961. * fetched the bit will be toggled again */
  962. bit ^= 1;
  963. while (current_superblock < s->superblock_count) {
  964. if (current_run == 0) {
  965. bit ^= 1;
  966. current_run = get_superblock_run_length(gb);
  967. debug_block_coding(" setting superblocks %d..%d to %s\n",
  968. current_superblock,
  969. current_superblock + current_run - 1,
  970. (bit) ? "partially coded" : "not coded");
  971. /* if any of the superblocks are not partially coded, flag
  972. * a boolean to decode the list of fully-coded superblocks */
  973. if (bit == 0)
  974. decode_fully_flags = 1;
  975. } else {
  976. /* make a note of the fact that there are partially coded
  977. * superblocks */
  978. decode_partial_blocks = 1;
  979. }
  980. s->superblock_coding[current_superblock++] =
  981. (bit) ? SB_PARTIALLY_CODED : SB_NOT_CODED;
  982. current_run--;
  983. }
  984. /* unpack the list of fully coded superblocks if any of the blocks were
  985. * not marked as partially coded in the previous step */
  986. if (decode_fully_flags) {
  987. current_superblock = 0;
  988. current_run = 0;
  989. bit = get_bits(gb, 1);
  990. /* toggle the bit because as soon as the first run length is
  991. * fetched the bit will be toggled again */
  992. bit ^= 1;
  993. while (current_superblock < s->superblock_count) {
  994. /* skip any superblocks already marked as partially coded */
  995. if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {
  996. if (current_run == 0) {
  997. bit ^= 1;
  998. current_run = get_superblock_run_length(gb);
  999. }
  1000. debug_block_coding(" setting superblock %d to %s\n",
  1001. current_superblock,
  1002. (bit) ? "fully coded" : "not coded");
  1003. s->superblock_coding[current_superblock] =
  1004. (bit) ? SB_FULLY_CODED : SB_NOT_CODED;
  1005. current_run--;
  1006. }
  1007. current_superblock++;
  1008. }
  1009. }
  1010. /* if there were partial blocks, initialize bitstream for
  1011. * unpacking fragment codings */
  1012. if (decode_partial_blocks) {
  1013. current_run = 0;
  1014. bit = get_bits(gb, 1);
  1015. /* toggle the bit because as soon as the first run length is
  1016. * fetched the bit will be toggled again */
  1017. bit ^= 1;
  1018. }
  1019. }
  1020. /* figure out which fragments are coded; iterate through each
  1021. * superblock (all planes) */
  1022. s->coded_fragment_list_index = 0;
  1023. s->first_coded_y_fragment = s->first_coded_c_fragment = 0;
  1024. s->last_coded_y_fragment = s->last_coded_c_fragment = -1;
  1025. memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
  1026. for (i = 0; i < s->superblock_count; i++) {
  1027. /* iterate through all 16 fragments in a superblock */
  1028. for (j = 0; j < 16; j++) {
  1029. /* if the fragment is in bounds, check its coding status */
  1030. current_fragment = s->superblock_fragments[i * 16 + j];
  1031. if (current_fragment >= s->fragment_count) {
  1032. printf (" vp3:unpack_superblocks(): bad fragment number (%d >= %d)\n",
  1033. current_fragment, s->fragment_count);
  1034. return 1;
  1035. }
  1036. if (current_fragment != -1) {
  1037. if (s->superblock_coding[i] == SB_NOT_CODED) {
  1038. /* copy all the fragments from the prior frame */
  1039. s->all_fragments[current_fragment].coding_method =
  1040. MODE_COPY;
  1041. } else if (s->superblock_coding[i] == SB_PARTIALLY_CODED) {
  1042. /* fragment may or may not be coded; this is the case
  1043. * that cares about the fragment coding runs */
  1044. if (current_run == 0) {
  1045. bit ^= 1;
  1046. current_run = get_fragment_run_length(gb);
  1047. }
  1048. if (bit) {
  1049. /* mode will be decoded in the next phase */
  1050. s->all_fragments[current_fragment].coding_method =
  1051. MODE_INTER_NO_MV;
  1052. s->coded_fragment_list[s->coded_fragment_list_index] =
  1053. current_fragment;
  1054. if ((current_fragment >= s->u_fragment_start) &&
  1055. (s->last_coded_y_fragment == -1)) {
  1056. s->first_coded_c_fragment = s->coded_fragment_list_index;
  1057. s->last_coded_y_fragment = s->first_coded_c_fragment - 1;
  1058. }
  1059. s->coded_fragment_list_index++;
  1060. s->macroblock_coding[s->all_fragments[current_fragment].macroblock] = MODE_INTER_NO_MV;
  1061. debug_block_coding(" superblock %d is partially coded, fragment %d is coded\n",
  1062. i, current_fragment);
  1063. } else {
  1064. /* not coded; copy this fragment from the prior frame */
  1065. s->all_fragments[current_fragment].coding_method =
  1066. MODE_COPY;
  1067. debug_block_coding(" superblock %d is partially coded, fragment %d is not coded\n",
  1068. i, current_fragment);
  1069. }
  1070. current_run--;
  1071. } else {
  1072. /* fragments are fully coded in this superblock; actual
  1073. * coding will be determined in next step */
  1074. s->all_fragments[current_fragment].coding_method =
  1075. MODE_INTER_NO_MV;
  1076. s->coded_fragment_list[s->coded_fragment_list_index] =
  1077. current_fragment;
  1078. if ((current_fragment >= s->u_fragment_start) &&
  1079. (s->last_coded_y_fragment == -1)) {
  1080. s->first_coded_c_fragment = s->coded_fragment_list_index;
  1081. s->last_coded_y_fragment = s->first_coded_c_fragment - 1;
  1082. }
  1083. s->coded_fragment_list_index++;
  1084. s->macroblock_coding[s->all_fragments[current_fragment].macroblock] = MODE_INTER_NO_MV;
  1085. debug_block_coding(" superblock %d is fully coded, fragment %d is coded\n",
  1086. i, current_fragment);
  1087. }
  1088. }
  1089. }
  1090. }
  1091. if (s->first_coded_c_fragment == 0)
  1092. /* no C fragments coded */
  1093. s->last_coded_y_fragment = s->coded_fragment_list_index - 1;
  1094. else
  1095. s->last_coded_c_fragment = s->coded_fragment_list_index - 1;
  1096. debug_block_coding(" %d total coded fragments, y: %d -> %d, c: %d -> %d\n",
  1097. s->coded_fragment_list_index,
  1098. s->first_coded_y_fragment,
  1099. s->last_coded_y_fragment,
  1100. s->first_coded_c_fragment,
  1101. s->last_coded_c_fragment);
  1102. return 0;
  1103. }
  1104. /*
  1105. * This function unpacks all the coding mode data for individual macroblocks
  1106. * from the bitstream.
  1107. */
  1108. static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
  1109. {
  1110. int i, j, k;
  1111. int scheme;
  1112. int current_macroblock;
  1113. int current_fragment;
  1114. int coding_mode;
  1115. debug_vp3(" vp3: unpacking encoding modes\n");
  1116. if (s->keyframe) {
  1117. debug_vp3(" keyframe-- all blocks are coded as INTRA\n");
  1118. for (i = 0; i < s->fragment_count; i++)
  1119. s->all_fragments[i].coding_method = MODE_INTRA;
  1120. } else {
  1121. /* fetch the mode coding scheme for this frame */
  1122. scheme = get_bits(gb, 3);
  1123. debug_modes(" using mode alphabet %d\n", scheme);
  1124. /* is it a custom coding scheme? */
  1125. if (scheme == 0) {
  1126. debug_modes(" custom mode alphabet ahead:\n");
  1127. for (i = 0; i < 8; i++)
  1128. ModeAlphabet[scheme][get_bits(gb, 3)] = i;
  1129. }
  1130. for (i = 0; i < 8; i++)
  1131. debug_modes(" mode[%d][%d] = %d\n", scheme, i,
  1132. ModeAlphabet[scheme][i]);
  1133. /* iterate through all of the macroblocks that contain 1 or more
  1134. * coded fragments */
  1135. for (i = 0; i < s->u_superblock_start; i++) {
  1136. for (j = 0; j < 4; j++) {
  1137. current_macroblock = s->superblock_macroblocks[i * 4 + j];
  1138. if ((current_macroblock == -1) ||
  1139. (s->macroblock_coding[current_macroblock] == MODE_COPY))
  1140. continue;
  1141. if (current_macroblock >= s->macroblock_count) {
  1142. printf (" vp3:unpack_modes(): bad macroblock number (%d >= %d)\n",
  1143. current_macroblock, s->macroblock_count);
  1144. return 1;
  1145. }
  1146. /* mode 7 means get 3 bits for each coding mode */
  1147. if (scheme == 7)
  1148. coding_mode = get_bits(gb, 3);
  1149. else
  1150. coding_mode = ModeAlphabet[scheme][get_mode_code(gb)];
  1151. s->macroblock_coding[current_macroblock] = coding_mode;
  1152. for (k = 0; k < 6; k++) {
  1153. current_fragment =
  1154. s->macroblock_fragments[current_macroblock * 6 + k];
  1155. if (current_fragment == -1)
  1156. continue;
  1157. if (current_fragment >= s->fragment_count) {
  1158. printf (" vp3:unpack_modes(): bad fragment number (%d >= %d)\n",
  1159. current_fragment, s->fragment_count);
  1160. return 1;
  1161. }
  1162. if (s->all_fragments[current_fragment].coding_method !=
  1163. MODE_COPY)
  1164. s->all_fragments[current_fragment].coding_method =
  1165. coding_mode;
  1166. }
  1167. debug_modes(" coding method for macroblock starting @ fragment %d = %d\n",
  1168. s->macroblock_fragments[current_macroblock * 6], coding_mode);
  1169. }
  1170. }
  1171. }
  1172. return 0;
  1173. }
  1174. /*
  1175. * This function adjusts the components of a motion vector for the halfpel
  1176. * motion grid. c_plane indicates whether the vector applies to the U or V
  1177. * plane. The function returns the halfpel function index to be used in
  1178. * ffmpeg's put_pixels[]() array of functions.
  1179. */
  1180. static inline int adjust_vector(int *x, int *y, int c_plane)
  1181. {
  1182. int motion_halfpel_index = 0;
  1183. int x_halfpel;
  1184. int y_halfpel;
  1185. if (!c_plane) {
  1186. x_halfpel = *x & 1;
  1187. motion_halfpel_index |= x_halfpel;
  1188. if (*x >= 0)
  1189. *x >>= 1;
  1190. else
  1191. *x = -( (-(*x) >> 1) + x_halfpel);
  1192. y_halfpel = *y & 1;
  1193. motion_halfpel_index |= (y_halfpel << 1);
  1194. if (*y >= 0)
  1195. *y >>= 1;
  1196. else
  1197. *y = -( (-(*y) >> 1) + y_halfpel);
  1198. } else {
  1199. x_halfpel = ((*x & 0x03) != 0);
  1200. motion_halfpel_index |= x_halfpel;
  1201. if (*x >= 0)
  1202. *x >>= 2;
  1203. else
  1204. *x = -( (-(*x) >> 2) + x_halfpel);
  1205. y_halfpel = ((*y & 0x03) != 0);
  1206. motion_halfpel_index |= (y_halfpel << 1);
  1207. if (*y >= 0)
  1208. *y >>= 2;
  1209. else
  1210. *y = -( (-(*y) >> 2) + y_halfpel);
  1211. }
  1212. return motion_halfpel_index;
  1213. }
  1214. /*
  1215. * This function unpacks all the motion vectors for the individual
  1216. * macroblocks from the bitstream.
  1217. */
  1218. static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
  1219. {
  1220. int i, j, k;
  1221. int coding_mode;
  1222. int motion_x[6];
  1223. int motion_y[6];
  1224. int last_motion_x = 0;
  1225. int last_motion_y = 0;
  1226. int prior_last_motion_x = 0;
  1227. int prior_last_motion_y = 0;
  1228. int current_macroblock;
  1229. int current_fragment;
  1230. debug_vp3(" vp3: unpacking motion vectors\n");
  1231. if (s->keyframe) {
  1232. debug_vp3(" keyframe-- there are no motion vectors\n");
  1233. } else {
  1234. memset(motion_x, 0, 6 * sizeof(int));
  1235. memset(motion_y, 0, 6 * sizeof(int));
  1236. /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme */
  1237. coding_mode = get_bits(gb, 1);
  1238. debug_vectors(" using %s scheme for unpacking motion vectors\n",
  1239. (coding_mode == 0) ? "VLC" : "fixed-length");
  1240. /* iterate through all of the macroblocks that contain 1 or more
  1241. * coded fragments */
  1242. for (i = 0; i < s->u_superblock_start; i++) {
  1243. for (j = 0; j < 4; j++) {
  1244. current_macroblock = s->superblock_macroblocks[i * 4 + j];
  1245. if ((current_macroblock == -1) ||
  1246. (s->macroblock_coding[current_macroblock] == MODE_COPY))
  1247. continue;
  1248. if (current_macroblock >= s->macroblock_count) {
  1249. printf (" vp3:unpack_vectors(): bad macroblock number (%d >= %d)\n",
  1250. current_macroblock, s->macroblock_count);
  1251. return 1;
  1252. }
  1253. current_fragment = s->macroblock_fragments[current_macroblock * 6];
  1254. if (current_fragment >= s->fragment_count) {
  1255. printf (" vp3:unpack_vectors(): bad fragment number (%d >= %d\n",
  1256. current_fragment, s->fragment_count);
  1257. return 1;
  1258. }
  1259. switch (s->macroblock_coding[current_macroblock]) {
  1260. case MODE_INTER_PLUS_MV:
  1261. case MODE_GOLDEN_MV:
  1262. /* all 6 fragments use the same motion vector */
  1263. if (coding_mode == 0) {
  1264. motion_x[0] = get_motion_vector_vlc(gb);
  1265. motion_y[0] = get_motion_vector_vlc(gb);
  1266. } else {
  1267. motion_x[0] = get_motion_vector_fixed(gb);
  1268. motion_y[0] = get_motion_vector_fixed(gb);
  1269. }
  1270. for (k = 1; k < 6; k++) {
  1271. motion_x[k] = motion_x[0];
  1272. motion_y[k] = motion_y[0];
  1273. }
  1274. /* vector maintenance, only on MODE_INTER_PLUS_MV */
  1275. if (s->all_fragments[current_fragment].coding_method ==
  1276. MODE_INTER_PLUS_MV) {
  1277. prior_last_motion_x = last_motion_x;
  1278. prior_last_motion_y = last_motion_y;
  1279. last_motion_x = motion_x[0];
  1280. last_motion_y = motion_y[0];
  1281. }
  1282. break;
  1283. case MODE_INTER_FOURMV:
  1284. /* fetch 4 vectors from the bitstream, one for each
  1285. * Y fragment, then average for the C fragment vectors */
  1286. motion_x[4] = motion_y[4] = 0;
  1287. for (k = 0; k < 4; k++) {
  1288. if (coding_mode == 0) {
  1289. motion_x[k] = get_motion_vector_vlc(gb);
  1290. motion_y[k] = get_motion_vector_vlc(gb);
  1291. } else {
  1292. motion_x[k] = get_motion_vector_fixed(gb);
  1293. motion_y[k] = get_motion_vector_fixed(gb);
  1294. }
  1295. motion_x[4] += motion_x[k];
  1296. motion_y[4] += motion_y[k];
  1297. }
  1298. if (motion_x[4] >= 0)
  1299. motion_x[4] = (motion_x[4] + 2) / 4;
  1300. else
  1301. motion_x[4] = (motion_x[4] - 2) / 4;
  1302. motion_x[5] = motion_x[4];
  1303. if (motion_y[4] >= 0)
  1304. motion_y[4] = (motion_y[4] + 2) / 4;
  1305. else
  1306. motion_y[4] = (motion_y[4] - 2) / 4;
  1307. motion_y[5] = motion_y[4];
  1308. /* vector maintenance; vector[3] is treated as the
  1309. * last vector in this case */
  1310. prior_last_motion_x = last_motion_x;
  1311. prior_last_motion_y = last_motion_y;
  1312. last_motion_x = motion_x[3];
  1313. last_motion_y = motion_y[3];
  1314. break;
  1315. case MODE_INTER_LAST_MV:
  1316. /* all 6 fragments use the last motion vector */
  1317. motion_x[0] = last_motion_x;
  1318. motion_y[0] = last_motion_y;
  1319. for (k = 1; k < 6; k++) {
  1320. motion_x[k] = motion_x[0];
  1321. motion_y[k] = motion_y[0];
  1322. }
  1323. /* no vector maintenance (last vector remains the
  1324. * last vector) */
  1325. break;
  1326. case MODE_INTER_PRIOR_LAST:
  1327. /* all 6 fragments use the motion vector prior to the
  1328. * last motion vector */
  1329. motion_x[0] = prior_last_motion_x;
  1330. motion_y[0] = prior_last_motion_y;
  1331. for (k = 1; k < 6; k++) {
  1332. motion_x[k] = motion_x[0];
  1333. motion_y[k] = motion_y[0];
  1334. }
  1335. /* vector maintenance */
  1336. prior_last_motion_x = last_motion_x;
  1337. prior_last_motion_y = last_motion_y;
  1338. last_motion_x = motion_x[0];
  1339. last_motion_y = motion_y[0];
  1340. break;
  1341. default:
  1342. /* covers intra, inter without MV, golden without MV */
  1343. memset(motion_x, 0, 6 * sizeof(int));
  1344. memset(motion_y, 0, 6 * sizeof(int));
  1345. /* no vector maintenance */
  1346. break;
  1347. }
  1348. /* assign the motion vectors to the correct fragments */
  1349. debug_vectors(" vectors for macroblock starting @ fragment %d (coding method %d):\n",
  1350. current_fragment,
  1351. s->all_fragments[current_fragment].coding_method);
  1352. for (k = 0; k < 6; k++) {
  1353. current_fragment =
  1354. s->macroblock_fragments[current_macroblock * 6 + k];
  1355. if (current_fragment == -1)
  1356. continue;
  1357. if (current_fragment >= s->fragment_count) {
  1358. printf (" vp3:unpack_vectors(): bad fragment number (%d >= %d)\n",
  1359. current_fragment, s->fragment_count);
  1360. return 1;
  1361. }
  1362. s->all_fragments[current_fragment].motion_halfpel_index =
  1363. adjust_vector(&motion_x[k], &motion_y[k],
  1364. ((k == 4) || (k == 5)));
  1365. s->all_fragments[current_fragment].motion_x = motion_x[k];
  1366. s->all_fragments[current_fragment].motion_y = motion_y[k];
  1367. debug_vectors(" vector %d: fragment %d = (%d, %d), index %d\n",
  1368. k, current_fragment, motion_x[k], motion_y[k],
  1369. s->all_fragments[current_fragment].motion_halfpel_index);
  1370. }
  1371. }
  1372. }
  1373. }
  1374. return 0;
  1375. }
  1376. /*
  1377. * This function is called by unpack_dct_coeffs() to extract the VLCs from
  1378. * the bitstream. The VLCs encode tokens which are used to unpack DCT
  1379. * data. This function unpacks all the VLCs for either the Y plane or both
  1380. * C planes, and is called for DC coefficients or different AC coefficient
  1381. * levels (since different coefficient types require different VLC tables.
  1382. *
  1383. * This function returns a residual eob run. E.g, if a particular token gave
  1384. * instructions to EOB the next 5 fragments and there were only 2 fragments
  1385. * left in the current fragment range, 3 would be returned so that it could
  1386. * be passed into the next call to this same function.
  1387. */
  1388. static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
  1389. VLC *table, int coeff_index,
  1390. int first_fragment, int last_fragment,
  1391. int eob_run)
  1392. {
  1393. int i;
  1394. int token;
  1395. int zero_run;
  1396. DCTELEM coeff;
  1397. Vp3Fragment *fragment;
  1398. if ((first_fragment < 0) ||
  1399. (first_fragment >= s->fragment_count) ||
  1400. (last_fragment < 0) ||
  1401. (last_fragment >= s->fragment_count)) {
  1402. printf (" vp3:unpack_vlcs(): bad fragment number (%d -> %d ?)\n",
  1403. first_fragment, last_fragment);
  1404. return 1;
  1405. }
  1406. for (i = first_fragment; i <= last_fragment; i++) {
  1407. fragment = &s->all_fragments[s->coded_fragment_list[i]];
  1408. if (fragment->coeff_count > coeff_index)
  1409. continue;
  1410. if (!eob_run) {
  1411. /* decode a VLC into a token */
  1412. token = get_vlc2(gb, table->table, 5, 3);
  1413. debug_vlc(" token = %2d, ", token);
  1414. /* use the token to get a zero run, a coefficient, and an eob run */
  1415. unpack_token(gb, token, &zero_run, &coeff, &eob_run);
  1416. }
  1417. if (!eob_run) {
  1418. fragment->coeff_count += zero_run;
  1419. if (fragment->coeff_count < 64)
  1420. fragment->coeffs[fragment->coeff_count++] = coeff;
  1421. debug_vlc(" fragment %d coeff = %d\n",
  1422. s->coded_fragment_list[i], fragment->coeffs[coeff_index]);
  1423. } else {
  1424. fragment->last_coeff = fragment->coeff_count;
  1425. fragment->coeff_count = 64;
  1426. debug_vlc(" fragment %d eob with %d coefficients\n",
  1427. s->coded_fragment_list[i], fragment->last_coeff);
  1428. eob_run--;
  1429. }
  1430. }
  1431. return eob_run;
  1432. }
  1433. /*
  1434. * This function unpacks all of the DCT coefficient data from the
  1435. * bitstream.
  1436. */
  1437. static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
  1438. {
  1439. int i;
  1440. int dc_y_table;
  1441. int dc_c_table;
  1442. int ac_y_table;
  1443. int ac_c_table;
  1444. int residual_eob_run = 0;
  1445. /* fetch the DC table indices */
  1446. dc_y_table = get_bits(gb, 4);
  1447. dc_c_table = get_bits(gb, 4);
  1448. /* unpack the Y plane DC coefficients */
  1449. debug_vp3(" vp3: unpacking Y plane DC coefficients using table %d\n",
  1450. dc_y_table);
  1451. residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
  1452. s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
  1453. /* unpack the C plane DC coefficients */
  1454. debug_vp3(" vp3: unpacking C plane DC coefficients using table %d\n",
  1455. dc_c_table);
  1456. residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
  1457. s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
  1458. /* fetch the AC table indices */
  1459. ac_y_table = get_bits(gb, 4);
  1460. ac_c_table = get_bits(gb, 4);
  1461. /* unpack the group 1 AC coefficients (coeffs 1-5) */
  1462. for (i = 1; i <= 5; i++) {
  1463. debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
  1464. i, ac_y_table);
  1465. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_y_table], i,
  1466. s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
  1467. debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
  1468. i, ac_c_table);
  1469. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_c_table], i,
  1470. s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
  1471. }
  1472. /* unpack the group 2 AC coefficients (coeffs 6-14) */
  1473. for (i = 6; i <= 14; i++) {
  1474. debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
  1475. i, ac_y_table);
  1476. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_y_table], i,
  1477. s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
  1478. debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
  1479. i, ac_c_table);
  1480. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_c_table], i,
  1481. s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
  1482. }
  1483. /* unpack the group 3 AC coefficients (coeffs 15-27) */
  1484. for (i = 15; i <= 27; i++) {
  1485. debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
  1486. i, ac_y_table);
  1487. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_y_table], i,
  1488. s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
  1489. debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
  1490. i, ac_c_table);
  1491. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_c_table], i,
  1492. s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
  1493. }
  1494. /* unpack the group 4 AC coefficients (coeffs 28-63) */
  1495. for (i = 28; i <= 63; i++) {
  1496. debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
  1497. i, ac_y_table);
  1498. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_y_table], i,
  1499. s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
  1500. debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
  1501. i, ac_c_table);
  1502. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_c_table], i,
  1503. s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
  1504. }
  1505. return 0;
  1506. }
  1507. /*
  1508. * This function reverses the DC prediction for each coded fragment in
  1509. * the frame. Much of this function is adapted directly from the original
  1510. * VP3 source code.
  1511. */
  1512. #define COMPATIBLE_FRAME(x) \
  1513. (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
  1514. #define FRAME_CODED(x) (s->all_fragments[x].coding_method != MODE_COPY)
  1515. static inline int iabs (int x) { return ((x < 0) ? -x : x); }
  1516. static void reverse_dc_prediction(Vp3DecodeContext *s,
  1517. int first_fragment,
  1518. int fragment_width,
  1519. int fragment_height)
  1520. {
  1521. #define PUL 8
  1522. #define PU 4
  1523. #define PUR 2
  1524. #define PL 1
  1525. int x, y;
  1526. int i = first_fragment;
  1527. /*
  1528. * Fragment prediction groups:
  1529. *
  1530. * 32222222226
  1531. * 10000000004
  1532. * 10000000004
  1533. * 10000000004
  1534. * 10000000004
  1535. *
  1536. * Note: Groups 5 and 7 do not exist as it would mean that the
  1537. * fragment's x coordinate is both 0 and (width - 1) at the same time.
  1538. */
  1539. int predictor_group;
  1540. short predicted_dc;
  1541. /* validity flags for the left, up-left, up, and up-right fragments */
  1542. int fl, ful, fu, fur;
  1543. /* DC values for the left, up-left, up, and up-right fragments */
  1544. int vl, vul, vu, vur;
  1545. /* indices for the left, up-left, up, and up-right fragments */
  1546. int l, ul, u, ur;
  1547. /*
  1548. * The 6 fields mean:
  1549. * 0: up-left multiplier
  1550. * 1: up multiplier
  1551. * 2: up-right multiplier
  1552. * 3: left multiplier
  1553. * 4: mask
  1554. * 5: right bit shift divisor (e.g., 7 means >>=7, a.k.a. div by 128)
  1555. */
  1556. int predictor_transform[16][6] = {
  1557. { 0, 0, 0, 0, 0, 0 },
  1558. { 0, 0, 0, 1, 0, 0 }, // PL
  1559. { 0, 0, 1, 0, 0, 0 }, // PUR
  1560. { 0, 0, 53, 75, 127, 7 }, // PUR|PL
  1561. { 0, 1, 0, 0, 0, 0 }, // PU
  1562. { 0, 1, 0, 1, 1, 1 }, // PU|PL
  1563. { 0, 1, 0, 0, 0, 0 }, // PU|PUR
  1564. { 0, 0, 53, 75, 127, 7 }, // PU|PUR|PL
  1565. { 1, 0, 0, 0, 0, 0 }, // PUL
  1566. { 0, 0, 0, 1, 0, 0 }, // PUL|PL
  1567. { 1, 0, 1, 0, 1, 1 }, // PUL|PUR
  1568. { 0, 0, 53, 75, 127, 7 }, // PUL|PUR|PL
  1569. { 0, 1, 0, 0, 0, 0 }, // PUL|PU
  1570. {-26, 29, 0, 29, 31, 5 }, // PUL|PU|PL
  1571. { 3, 10, 3, 0, 15, 4 }, // PUL|PU|PUR
  1572. {-26, 29, 0, 29, 31, 5 } // PUL|PU|PUR|PL
  1573. };
  1574. /* This table shows which types of blocks can use other blocks for
  1575. * prediction. For example, INTRA is the only mode in this table to
  1576. * have a frame number of 0. That means INTRA blocks can only predict
  1577. * from other INTRA blocks. There are 2 golden frame coding types;
  1578. * blocks encoding in these modes can only predict from other blocks
  1579. * that were encoded with these 1 of these 2 modes. */
  1580. unsigned char compatible_frame[8] = {
  1581. 1, /* MODE_INTER_NO_MV */
  1582. 0, /* MODE_INTRA */
  1583. 1, /* MODE_INTER_PLUS_MV */
  1584. 1, /* MODE_INTER_LAST_MV */
  1585. 1, /* MODE_INTER_PRIOR_MV */
  1586. 2, /* MODE_USING_GOLDEN */
  1587. 2, /* MODE_GOLDEN_MV */
  1588. 1 /* MODE_INTER_FOUR_MV */
  1589. };
  1590. int current_frame_type;
  1591. /* there is a last DC predictor for each of the 3 frame types */
  1592. short last_dc[3];
  1593. int transform = 0;
  1594. debug_vp3(" vp3: reversing DC prediction\n");
  1595. vul = vu = vur = vl = 0;
  1596. last_dc[0] = last_dc[1] = last_dc[2] = 0;
  1597. /* for each fragment row... */
  1598. for (y = 0; y < fragment_height; y++) {
  1599. /* for each fragment in a row... */
  1600. for (x = 0; x < fragment_width; x++, i++) {
  1601. /* reverse prediction if this block was coded */
  1602. if (s->all_fragments[i].coding_method != MODE_COPY) {
  1603. current_frame_type =
  1604. compatible_frame[s->all_fragments[i].coding_method];
  1605. predictor_group = (x == 0) + ((y == 0) << 1) +
  1606. ((x + 1 == fragment_width) << 2);
  1607. debug_dc_pred(" frag %d: group %d, orig DC = %d, ",
  1608. i, predictor_group, s->all_fragments[i].coeffs[0]);
  1609. switch (predictor_group) {
  1610. case 0:
  1611. /* main body of fragments; consider all 4 possible
  1612. * fragments for prediction */
  1613. /* calculate the indices of the predicting fragments */
  1614. ul = i - fragment_width - 1;
  1615. u = i - fragment_width;
  1616. ur = i - fragment_width + 1;
  1617. l = i - 1;
  1618. /* fetch the DC values for the predicting fragments */
  1619. vul = s->all_fragments[ul].coeffs[0];
  1620. vu = s->all_fragments[u].coeffs[0];
  1621. vur = s->all_fragments[ur].coeffs[0];
  1622. vl = s->all_fragments[l].coeffs[0];
  1623. /* figure out which fragments are valid */
  1624. ful = FRAME_CODED(ul) && COMPATIBLE_FRAME(ul);
  1625. fu = FRAME_CODED(u) && COMPATIBLE_FRAME(u);
  1626. fur = FRAME_CODED(ur) && COMPATIBLE_FRAME(ur);
  1627. fl = FRAME_CODED(l) && COMPATIBLE_FRAME(l);
  1628. /* decide which predictor transform to use */
  1629. transform = (fl*PL) | (fu*PU) | (ful*PUL) | (fur*PUR);
  1630. break;
  1631. case 1:
  1632. /* left column of fragments, not including top corner;
  1633. * only consider up and up-right fragments */
  1634. /* calculate the indices of the predicting fragments */
  1635. u = i - fragment_width;
  1636. ur = i - fragment_width + 1;
  1637. /* fetch the DC values for the predicting fragments */
  1638. vu = s->all_fragments[u].coeffs[0];
  1639. vur = s->all_fragments[ur].coeffs[0];
  1640. /* figure out which fragments are valid */
  1641. fur = FRAME_CODED(ur) && COMPATIBLE_FRAME(ur);
  1642. fu = FRAME_CODED(u) && COMPATIBLE_FRAME(u);
  1643. /* decide which predictor transform to use */
  1644. transform = (fu*PU) | (fur*PUR);
  1645. break;
  1646. case 2:
  1647. case 6:
  1648. /* top row of fragments, not including top-left frag;
  1649. * only consider the left fragment for prediction */
  1650. /* calculate the indices of the predicting fragments */
  1651. l = i - 1;
  1652. /* fetch the DC values for the predicting fragments */
  1653. vl = s->all_fragments[l].coeffs[0];
  1654. /* figure out which fragments are valid */
  1655. fl = FRAME_CODED(l) && COMPATIBLE_FRAME(l);
  1656. /* decide which predictor transform to use */
  1657. transform = (fl*PL);
  1658. break;
  1659. case 3:
  1660. /* top-left fragment */
  1661. /* nothing to predict from in this case */
  1662. transform = 0;
  1663. break;
  1664. case 4:
  1665. /* right column of fragments, not including top corner;
  1666. * consider up-left, up, and left fragments for
  1667. * prediction */
  1668. /* calculate the indices of the predicting fragments */
  1669. ul = i - fragment_width - 1;
  1670. u = i - fragment_width;
  1671. l = i - 1;
  1672. /* fetch the DC values for the predicting fragments */
  1673. vul = s->all_fragments[ul].coeffs[0];
  1674. vu = s->all_fragments[u].coeffs[0];
  1675. vl = s->all_fragments[l].coeffs[0];
  1676. /* figure out which fragments are valid */
  1677. ful = FRAME_CODED(ul) && COMPATIBLE_FRAME(ul);
  1678. fu = FRAME_CODED(u) && COMPATIBLE_FRAME(u);
  1679. fl = FRAME_CODED(l) && COMPATIBLE_FRAME(l);
  1680. /* decide which predictor transform to use */
  1681. transform = (fl*PL) | (fu*PU) | (ful*PUL);
  1682. break;
  1683. }
  1684. debug_dc_pred("transform = %d, ", transform);
  1685. if (transform == 0) {
  1686. /* if there were no fragments to predict from, use last
  1687. * DC saved */
  1688. s->all_fragments[i].coeffs[0] += last_dc[current_frame_type];
  1689. debug_dc_pred("from last DC (%d) = %d\n",
  1690. current_frame_type, s->all_fragments[i].coeffs[0]);
  1691. } else {
  1692. /* apply the appropriate predictor transform */
  1693. predicted_dc =
  1694. (predictor_transform[transform][0] * vul) +
  1695. (predictor_transform[transform][1] * vu) +
  1696. (predictor_transform[transform][2] * vur) +
  1697. (predictor_transform[transform][3] * vl);
  1698. /* if there is a shift value in the transform, add
  1699. * the sign bit before the shift */
  1700. if (predictor_transform[transform][5] != 0) {
  1701. predicted_dc += ((predicted_dc >> 15) &
  1702. predictor_transform[transform][4]);
  1703. predicted_dc >>= predictor_transform[transform][5];
  1704. }
  1705. /* check for outranging on the [ul u l] and
  1706. * [ul u ur l] predictors */
  1707. if ((transform == 13) || (transform == 15)) {
  1708. if (iabs(predicted_dc - vu) > 128)
  1709. predicted_dc = vu;
  1710. else if (iabs(predicted_dc - vl) > 128)
  1711. predicted_dc = vl;
  1712. else if (iabs(predicted_dc - vul) > 128)
  1713. predicted_dc = vul;
  1714. }
  1715. /* at long last, apply the predictor */
  1716. s->all_fragments[i].coeffs[0] += predicted_dc;
  1717. debug_dc_pred("from pred DC = %d\n",
  1718. s->all_fragments[i].coeffs[0]);
  1719. }
  1720. /* save the DC */
  1721. last_dc[current_frame_type] = s->all_fragments[i].coeffs[0];
  1722. }
  1723. }
  1724. }
  1725. }
  1726. /*
  1727. * This function performs the final rendering of each fragment's data
  1728. * onto the output frame.
  1729. */
  1730. static void render_fragments(Vp3DecodeContext *s,
  1731. int first_fragment,
  1732. int width,
  1733. int height,
  1734. int plane /* 0 = Y, 1 = U, 2 = V */)
  1735. {
  1736. int x, y;
  1737. int m, n;
  1738. int i = first_fragment;
  1739. int j;
  1740. int16_t *dequantizer;
  1741. DCTELEM dequant_block[64];
  1742. DCTELEM dequant_block_permuted[64];
  1743. unsigned char *output_plane;
  1744. unsigned char *last_plane;
  1745. unsigned char *golden_plane;
  1746. int stride;
  1747. int motion_x, motion_y;
  1748. int upper_motion_limit, lower_motion_limit;
  1749. int motion_halfpel_index;
  1750. unsigned int motion_source;
  1751. debug_vp3(" vp3: rendering final fragments for %s\n",
  1752. (plane == 0) ? "Y plane" : (plane == 1) ? "U plane" : "V plane");
  1753. /* set up plane-specific parameters */
  1754. if (plane == 0) {
  1755. dequantizer = s->intra_y_dequant;
  1756. output_plane = s->current_frame.data[0];
  1757. last_plane = s->last_frame.data[0];
  1758. golden_plane = s->golden_frame.data[0];
  1759. stride = -s->current_frame.linesize[0];
  1760. upper_motion_limit = 7 * s->current_frame.linesize[0];
  1761. lower_motion_limit = height * s->current_frame.linesize[0] + width - 8;
  1762. } else if (plane == 1) {
  1763. dequantizer = s->intra_c_dequant;
  1764. output_plane = s->current_frame.data[1];
  1765. last_plane = s->last_frame.data[1];
  1766. golden_plane = s->golden_frame.data[1];
  1767. stride = -s->current_frame.linesize[1];
  1768. upper_motion_limit = 7 * s->current_frame.linesize[1];
  1769. lower_motion_limit = height * s->current_frame.linesize[1] + width - 8;
  1770. } else {
  1771. dequantizer = s->intra_c_dequant;
  1772. output_plane = s->current_frame.data[2];
  1773. last_plane = s->last_frame.data[2];
  1774. golden_plane = s->golden_frame.data[2];
  1775. stride = -s->current_frame.linesize[2];
  1776. upper_motion_limit = 7 * s->current_frame.linesize[2];
  1777. lower_motion_limit = height * s->current_frame.linesize[2] + width - 8;
  1778. }
  1779. /* for each fragment row... */
  1780. for (y = 0; y < height; y += 8) {
  1781. /* for each fragment in a row... */
  1782. for (x = 0; x < width; x += 8, i++) {
  1783. if ((i < 0) || (i >= s->fragment_count)) {
  1784. printf (" vp3:render_fragments(): bad fragment number (%d)\n", i);
  1785. return;
  1786. }
  1787. /* transform if this block was coded */
  1788. if (s->all_fragments[i].coding_method != MODE_COPY) {
  1789. /* sort out the motion vector */
  1790. motion_x = s->all_fragments[i].motion_x;
  1791. motion_y = s->all_fragments[i].motion_y;
  1792. motion_halfpel_index = s->all_fragments[i].motion_halfpel_index;
  1793. motion_source = s->all_fragments[i].first_pixel;
  1794. motion_source += motion_x;
  1795. motion_source += (motion_y * stride);
  1796. /* if the are any problems with a motion vector, refuse
  1797. * to render the block */
  1798. if ((motion_source < upper_motion_limit) ||
  1799. (motion_source > lower_motion_limit)) {
  1800. // printf (" vp3: help! motion source (%d) out of range (%d..%d)\n",
  1801. // motion_source, upper_motion_limit, lower_motion_limit);
  1802. continue;
  1803. }
  1804. /* first, take care of copying a block from either the
  1805. * previous or the golden frame */
  1806. if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
  1807. (s->all_fragments[i].coding_method == MODE_GOLDEN_MV)) {
  1808. s->dsp.put_pixels_tab[1][motion_halfpel_index](
  1809. output_plane + s->all_fragments[i].first_pixel,
  1810. golden_plane + motion_source,
  1811. stride, 8);
  1812. } else
  1813. if (s->all_fragments[i].coding_method != MODE_INTRA) {
  1814. s->dsp.put_pixels_tab[1][motion_halfpel_index](
  1815. output_plane + s->all_fragments[i].first_pixel,
  1816. last_plane + motion_source,
  1817. stride, 8);
  1818. }
  1819. /* dequantize the DCT coefficients */
  1820. debug_idct("fragment %d, coding mode %d, DC = %d, dequant = %d:\n",
  1821. i, s->all_fragments[i].coding_method,
  1822. s->all_fragments[i].coeffs[0], dequantizer[0]);
  1823. for (j = 0; j < 64; j++)
  1824. dequant_block[dezigzag_index[j]] =
  1825. s->all_fragments[i].coeffs[j] *
  1826. dequantizer[j];
  1827. for (j = 0; j < 64; j++)
  1828. dequant_block_permuted[s->dsp.idct_permutation[j]] =
  1829. dequant_block[j];
  1830. debug_idct("dequantized block:\n");
  1831. for (m = 0; m < 8; m++) {
  1832. for (n = 0; n < 8; n++) {
  1833. debug_idct(" %5d", dequant_block[m * 8 + n]);
  1834. }
  1835. debug_idct("\n");
  1836. }
  1837. debug_idct("\n");
  1838. /* invert DCT and place (or add) in final output */
  1839. if (s->all_fragments[i].coding_method == MODE_INTRA) {
  1840. dequant_block_permuted[0] += 1024;
  1841. s->dsp.idct_put(
  1842. output_plane + s->all_fragments[i].first_pixel,
  1843. stride, dequant_block_permuted);
  1844. } else {
  1845. s->dsp.idct_add(
  1846. output_plane + s->all_fragments[i].first_pixel,
  1847. stride, dequant_block_permuted);
  1848. }
  1849. debug_idct("block after idct_%s():\n",
  1850. (s->all_fragments[i].coding_method == MODE_INTRA)?
  1851. "put" : "add");
  1852. for (m = 0; m < 8; m++) {
  1853. for (n = 0; n < 8; n++) {
  1854. debug_idct(" %3d", *(output_plane +
  1855. s->all_fragments[i].first_pixel + (m * stride + n)));
  1856. }
  1857. debug_idct("\n");
  1858. }
  1859. debug_idct("\n");
  1860. } else {
  1861. /* copy directly from the previous frame */
  1862. s->dsp.put_pixels_tab[1][0](
  1863. output_plane + s->all_fragments[i].first_pixel,
  1864. last_plane + s->all_fragments[i].first_pixel,
  1865. stride, 8);
  1866. }
  1867. }
  1868. }
  1869. emms_c();
  1870. }
  1871. /*
  1872. * This function computes the first pixel addresses for each fragment.
  1873. * This function needs to be invoked after the first frame is allocated
  1874. * so that it has access to the plane strides.
  1875. */
  1876. static void vp3_calculate_pixel_addresses(Vp3DecodeContext *s)
  1877. {
  1878. int i, x, y;
  1879. /* figure out the first pixel addresses for each of the fragments */
  1880. /* Y plane */
  1881. i = 0;
  1882. for (y = s->fragment_height; y > 0; y--) {
  1883. for (x = 0; x < s->fragment_width; x++) {
  1884. s->all_fragments[i++].first_pixel =
  1885. s->golden_frame.linesize[0] * y * FRAGMENT_PIXELS -
  1886. s->golden_frame.linesize[0] +
  1887. x * FRAGMENT_PIXELS;
  1888. debug_init(" fragment %d, first pixel @ %d\n",
  1889. i-1, s->all_fragments[i-1].first_pixel);
  1890. }
  1891. }
  1892. /* U plane */
  1893. i = s->u_fragment_start;
  1894. for (y = s->fragment_height / 2; y > 0; y--) {
  1895. for (x = 0; x < s->fragment_width / 2; x++) {
  1896. s->all_fragments[i++].first_pixel =
  1897. s->golden_frame.linesize[1] * y * FRAGMENT_PIXELS -
  1898. s->golden_frame.linesize[1] +
  1899. x * FRAGMENT_PIXELS;
  1900. debug_init(" fragment %d, first pixel @ %d\n",
  1901. i-1, s->all_fragments[i-1].first_pixel);
  1902. }
  1903. }
  1904. /* V plane */
  1905. i = s->v_fragment_start;
  1906. for (y = s->fragment_height / 2; y > 0; y--) {
  1907. for (x = 0; x < s->fragment_width / 2; x++) {
  1908. s->all_fragments[i++].first_pixel =
  1909. s->golden_frame.linesize[2] * y * FRAGMENT_PIXELS -
  1910. s->golden_frame.linesize[2] +
  1911. x * FRAGMENT_PIXELS;
  1912. debug_init(" fragment %d, first pixel @ %d\n",
  1913. i-1, s->all_fragments[i-1].first_pixel);
  1914. }
  1915. }
  1916. }
  1917. /*
  1918. * This is the ffmpeg/libavcodec API init function.
  1919. */
  1920. static int vp3_decode_init(AVCodecContext *avctx)
  1921. {
  1922. Vp3DecodeContext *s = avctx->priv_data;
  1923. int i;
  1924. int c_width;
  1925. int c_height;
  1926. int y_superblock_count;
  1927. int c_superblock_count;
  1928. s->avctx = avctx;
  1929. s->width = avctx->width;
  1930. s->height = avctx->height;
  1931. avctx->pix_fmt = PIX_FMT_YUV420P;
  1932. avctx->has_b_frames = 0;
  1933. dsputil_init(&s->dsp, avctx);
  1934. /* initialize to an impossible value which will force a recalculation
  1935. * in the first frame decode */
  1936. s->quality_index = -1;
  1937. s->y_superblock_width = (s->width + 31) / 32;
  1938. s->y_superblock_height = (s->height + 31) / 32;
  1939. y_superblock_count = s->y_superblock_width * s->y_superblock_height;
  1940. /* work out the dimensions for the C planes */
  1941. c_width = s->width / 2;
  1942. c_height = s->height / 2;
  1943. s->c_superblock_width = (c_width + 31) / 32;
  1944. s->c_superblock_height = (c_height + 31) / 32;
  1945. c_superblock_count = s->c_superblock_width * s->c_superblock_height;
  1946. s->superblock_count = y_superblock_count + (c_superblock_count * 2);
  1947. s->u_superblock_start = y_superblock_count;
  1948. s->v_superblock_start = s->u_superblock_start + c_superblock_count;
  1949. s->superblock_coding = av_malloc(s->superblock_count);
  1950. s->macroblock_width = (s->width + 15) / 16;
  1951. s->macroblock_height = (s->height + 15) / 16;
  1952. s->macroblock_count = s->macroblock_width * s->macroblock_height;
  1953. s->fragment_width = s->width / FRAGMENT_PIXELS;
  1954. s->fragment_height = s->height / FRAGMENT_PIXELS;
  1955. /* fragment count covers all 8x8 blocks for all 3 planes */
  1956. s->fragment_count = s->fragment_width * s->fragment_height * 3 / 2;
  1957. s->u_fragment_start = s->fragment_width * s->fragment_height;
  1958. s->v_fragment_start = s->fragment_width * s->fragment_height * 5 / 4;
  1959. debug_init(" Y plane: %d x %d\n", s->width, s->height);
  1960. debug_init(" C plane: %d x %d\n", c_width, c_height);
  1961. debug_init(" Y superblocks: %d x %d, %d total\n",
  1962. s->y_superblock_width, s->y_superblock_height, y_superblock_count);
  1963. debug_init(" C superblocks: %d x %d, %d total\n",
  1964. s->c_superblock_width, s->c_superblock_height, c_superblock_count);
  1965. debug_init(" total superblocks = %d, U starts @ %d, V starts @ %d\n",
  1966. s->superblock_count, s->u_superblock_start, s->v_superblock_start);
  1967. debug_init(" macroblocks: %d x %d, %d total\n",
  1968. s->macroblock_width, s->macroblock_height, s->macroblock_count);
  1969. debug_init(" %d fragments, %d x %d, u starts @ %d, v starts @ %d\n",
  1970. s->fragment_count,
  1971. s->fragment_width,
  1972. s->fragment_height,
  1973. s->u_fragment_start,
  1974. s->v_fragment_start);
  1975. s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment));
  1976. s->coded_fragment_list = av_malloc(s->fragment_count * sizeof(int));
  1977. s->pixel_addresses_inited = 0;
  1978. /* init VLC tables */
  1979. for (i = 0; i < 16; i++) {
  1980. /* DC histograms */
  1981. init_vlc(&s->dc_vlc[i], 5, 32,
  1982. &dc_bias[i][0][1], 4, 2,
  1983. &dc_bias[i][0][0], 4, 2);
  1984. /* group 1 AC histograms */
  1985. init_vlc(&s->ac_vlc_1[i], 5, 32,
  1986. &ac_bias_0[i][0][1], 4, 2,
  1987. &ac_bias_0[i][0][0], 4, 2);
  1988. /* group 2 AC histograms */
  1989. init_vlc(&s->ac_vlc_2[i], 5, 32,
  1990. &ac_bias_1[i][0][1], 4, 2,
  1991. &ac_bias_1[i][0][0], 4, 2);
  1992. /* group 3 AC histograms */
  1993. init_vlc(&s->ac_vlc_3[i], 5, 32,
  1994. &ac_bias_2[i][0][1], 4, 2,
  1995. &ac_bias_2[i][0][0], 4, 2);
  1996. /* group 4 AC histograms */
  1997. init_vlc(&s->ac_vlc_4[i], 5, 32,
  1998. &ac_bias_3[i][0][1], 4, 2,
  1999. &ac_bias_3[i][0][0], 4, 2);
  2000. }
  2001. /* build quantization zigzag table */
  2002. for (i = 0; i < 64; i++)
  2003. zigzag_index[dezigzag_index[i]] = i;
  2004. /* work out the block mapping tables */
  2005. s->superblock_fragments = av_malloc(s->superblock_count * 16 * sizeof(int));
  2006. s->superblock_macroblocks = av_malloc(s->superblock_count * 4 * sizeof(int));
  2007. s->macroblock_fragments = av_malloc(s->macroblock_count * 6 * sizeof(int));
  2008. s->macroblock_coding = av_malloc(s->macroblock_count + 1);
  2009. init_block_mapping(s);
  2010. for (i = 0; i < 3; i++) {
  2011. s->current_frame.data[i] = NULL;
  2012. s->last_frame.data[i] = NULL;
  2013. s->golden_frame.data[i] = NULL;
  2014. }
  2015. return 0;
  2016. }
  2017. /*
  2018. * This is the ffmpeg/libavcodec API frame decode function.
  2019. */
  2020. static int vp3_decode_frame(AVCodecContext *avctx,
  2021. void *data, int *data_size,
  2022. uint8_t *buf, int buf_size)
  2023. {
  2024. Vp3DecodeContext *s = avctx->priv_data;
  2025. GetBitContext gb;
  2026. static int counter = 0;
  2027. *data_size = 0;
  2028. init_get_bits(&gb, buf, buf_size * 8);
  2029. s->keyframe = get_bits(&gb, 1);
  2030. s->keyframe ^= 1;
  2031. skip_bits(&gb, 1);
  2032. s->last_quality_index = s->quality_index;
  2033. s->quality_index = get_bits(&gb, 6);
  2034. if (s->quality_index != s->last_quality_index)
  2035. init_dequantizer(s);
  2036. debug_vp3(" VP3 frame #%d: Q index = %d", counter, s->quality_index);
  2037. counter++;
  2038. if (s->keyframe) {
  2039. if (s->last_frame.data[0] == s->golden_frame.data[0]) {
  2040. if (s->golden_frame.data[0])
  2041. avctx->release_buffer(avctx, &s->golden_frame);
  2042. } else {
  2043. if (s->golden_frame.data[0])
  2044. avctx->release_buffer(avctx, &s->golden_frame);
  2045. if (s->last_frame.data[0])
  2046. avctx->release_buffer(avctx, &s->last_frame);
  2047. }
  2048. s->golden_frame.reference = 0;
  2049. if(avctx->get_buffer(avctx, &s->golden_frame) < 0) {
  2050. printf("vp3: get_buffer() failed\n");
  2051. return -1;
  2052. }
  2053. /* golden frame is also the current frame */
  2054. memcpy(&s->current_frame, &s->golden_frame, sizeof(AVFrame));
  2055. /* time to figure out pixel addresses? */
  2056. if (!s->pixel_addresses_inited)
  2057. vp3_calculate_pixel_addresses(s);
  2058. } else {
  2059. /* allocate a new current frame */
  2060. s->current_frame.reference = 0;
  2061. if(avctx->get_buffer(avctx, &s->current_frame) < 0) {
  2062. printf("vp3: get_buffer() failed\n");
  2063. return -1;
  2064. }
  2065. }
  2066. if (s->keyframe) {
  2067. debug_vp3(", keyframe\n");
  2068. /* skip the other 2 header bytes for now */
  2069. skip_bits(&gb, 16);
  2070. } else
  2071. debug_vp3("\n");
  2072. init_frame(s, &gb);
  2073. #if KEYFRAMES_ONLY
  2074. if (!s->keyframe) {
  2075. memcpy(s->current_frame.data[0], s->golden_frame.data[0],
  2076. s->current_frame.linesize[0] * s->height);
  2077. memcpy(s->current_frame.data[1], s->golden_frame.data[1],
  2078. s->current_frame.linesize[1] * s->height / 2);
  2079. memcpy(s->current_frame.data[2], s->golden_frame.data[2],
  2080. s->current_frame.linesize[2] * s->height / 2);
  2081. } else {
  2082. #endif
  2083. if (unpack_superblocks(s, &gb) ||
  2084. unpack_modes(s, &gb) ||
  2085. unpack_vectors(s, &gb) ||
  2086. unpack_dct_coeffs(s, &gb)) {
  2087. printf(" vp3: could not decode frame\n");
  2088. return -1;
  2089. }
  2090. reverse_dc_prediction(s, 0, s->fragment_width, s->fragment_height);
  2091. reverse_dc_prediction(s, s->u_fragment_start,
  2092. s->fragment_width / 2, s->fragment_height / 2);
  2093. reverse_dc_prediction(s, s->v_fragment_start,
  2094. s->fragment_width / 2, s->fragment_height / 2);
  2095. render_fragments(s, 0, s->width, s->height, 0);
  2096. render_fragments(s, s->u_fragment_start, s->width / 2, s->height / 2, 1);
  2097. render_fragments(s, s->v_fragment_start, s->width / 2, s->height / 2, 2);
  2098. #if KEYFRAMES_ONLY
  2099. }
  2100. #endif
  2101. *data_size=sizeof(AVFrame);
  2102. *(AVFrame*)data= s->current_frame;
  2103. /* release the last frame, if it is allocated and if it is not the
  2104. * golden frame */
  2105. if ((s->last_frame.data[0]) &&
  2106. (s->last_frame.data[0] != s->golden_frame.data[0]))
  2107. avctx->release_buffer(avctx, &s->last_frame);
  2108. /* shuffle frames (last = current) */
  2109. memcpy(&s->last_frame, &s->current_frame, sizeof(AVFrame));
  2110. return buf_size;
  2111. }
  2112. /*
  2113. * This is the ffmpeg/libavcodec API module cleanup function.
  2114. */
  2115. static int vp3_decode_end(AVCodecContext *avctx)
  2116. {
  2117. Vp3DecodeContext *s = avctx->priv_data;
  2118. av_free(s->all_fragments);
  2119. av_free(s->coded_fragment_list);
  2120. av_free(s->superblock_fragments);
  2121. av_free(s->superblock_macroblocks);
  2122. av_free(s->macroblock_fragments);
  2123. av_free(s->macroblock_coding);
  2124. /* release all frames */
  2125. if (s->golden_frame.data[0])
  2126. avctx->release_buffer(avctx, &s->golden_frame);
  2127. if (s->last_frame.data[0])
  2128. avctx->release_buffer(avctx, &s->last_frame);
  2129. /* no need to release the current_frame since it will always be pointing
  2130. * to the same frame as either the golden or last frame */
  2131. return 0;
  2132. }
  2133. AVCodec vp3_decoder = {
  2134. "vp3",
  2135. CODEC_TYPE_VIDEO,
  2136. CODEC_ID_VP3,
  2137. sizeof(Vp3DecodeContext),
  2138. vp3_decode_init,
  2139. NULL,
  2140. vp3_decode_end,
  2141. vp3_decode_frame,
  2142. 0,
  2143. NULL
  2144. };