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.

2573 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 first_c_fragment_seen;
  952. int i, j;
  953. int current_fragment;
  954. debug_vp3(" vp3: unpacking superblock coding\n");
  955. if (s->keyframe) {
  956. debug_vp3(" keyframe-- all superblocks are fully coded\n");
  957. memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);
  958. } else {
  959. /* unpack the list of partially-coded superblocks */
  960. bit = get_bits(gb, 1);
  961. /* toggle the bit because as soon as the first run length is
  962. * fetched the bit will be toggled again */
  963. bit ^= 1;
  964. while (current_superblock < s->superblock_count) {
  965. if (current_run == 0) {
  966. bit ^= 1;
  967. current_run = get_superblock_run_length(gb);
  968. debug_block_coding(" setting superblocks %d..%d to %s\n",
  969. current_superblock,
  970. current_superblock + current_run - 1,
  971. (bit) ? "partially coded" : "not coded");
  972. /* if any of the superblocks are not partially coded, flag
  973. * a boolean to decode the list of fully-coded superblocks */
  974. if (bit == 0)
  975. decode_fully_flags = 1;
  976. } else {
  977. /* make a note of the fact that there are partially coded
  978. * superblocks */
  979. decode_partial_blocks = 1;
  980. }
  981. s->superblock_coding[current_superblock++] =
  982. (bit) ? SB_PARTIALLY_CODED : SB_NOT_CODED;
  983. current_run--;
  984. }
  985. /* unpack the list of fully coded superblocks if any of the blocks were
  986. * not marked as partially coded in the previous step */
  987. if (decode_fully_flags) {
  988. current_superblock = 0;
  989. current_run = 0;
  990. bit = get_bits(gb, 1);
  991. /* toggle the bit because as soon as the first run length is
  992. * fetched the bit will be toggled again */
  993. bit ^= 1;
  994. while (current_superblock < s->superblock_count) {
  995. /* skip any superblocks already marked as partially coded */
  996. if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {
  997. if (current_run == 0) {
  998. bit ^= 1;
  999. current_run = get_superblock_run_length(gb);
  1000. }
  1001. debug_block_coding(" setting superblock %d to %s\n",
  1002. current_superblock,
  1003. (bit) ? "fully coded" : "not coded");
  1004. s->superblock_coding[current_superblock] =
  1005. (bit) ? SB_FULLY_CODED : SB_NOT_CODED;
  1006. current_run--;
  1007. }
  1008. current_superblock++;
  1009. }
  1010. }
  1011. /* if there were partial blocks, initialize bitstream for
  1012. * unpacking fragment codings */
  1013. if (decode_partial_blocks) {
  1014. current_run = 0;
  1015. bit = get_bits(gb, 1);
  1016. /* toggle the bit because as soon as the first run length is
  1017. * fetched the bit will be toggled again */
  1018. bit ^= 1;
  1019. }
  1020. }
  1021. /* figure out which fragments are coded; iterate through each
  1022. * superblock (all planes) */
  1023. s->coded_fragment_list_index = 0;
  1024. s->first_coded_y_fragment = s->first_coded_c_fragment = 0;
  1025. s->last_coded_y_fragment = s->last_coded_c_fragment = -1;
  1026. first_c_fragment_seen = 0;
  1027. memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
  1028. for (i = 0; i < s->superblock_count; i++) {
  1029. /* iterate through all 16 fragments in a superblock */
  1030. for (j = 0; j < 16; j++) {
  1031. /* if the fragment is in bounds, check its coding status */
  1032. current_fragment = s->superblock_fragments[i * 16 + j];
  1033. if (current_fragment >= s->fragment_count) {
  1034. printf (" vp3:unpack_superblocks(): bad fragment number (%d >= %d)\n",
  1035. current_fragment, s->fragment_count);
  1036. return 1;
  1037. }
  1038. if (current_fragment != -1) {
  1039. if (s->superblock_coding[i] == SB_NOT_CODED) {
  1040. /* copy all the fragments from the prior frame */
  1041. s->all_fragments[current_fragment].coding_method =
  1042. MODE_COPY;
  1043. } else if (s->superblock_coding[i] == SB_PARTIALLY_CODED) {
  1044. /* fragment may or may not be coded; this is the case
  1045. * that cares about the fragment coding runs */
  1046. if (current_run == 0) {
  1047. bit ^= 1;
  1048. current_run = get_fragment_run_length(gb);
  1049. }
  1050. if (bit) {
  1051. /* default mode; actual mode will be decoded in
  1052. * the next phase */
  1053. s->all_fragments[current_fragment].coding_method =
  1054. MODE_INTER_NO_MV;
  1055. s->coded_fragment_list[s->coded_fragment_list_index] =
  1056. current_fragment;
  1057. if ((current_fragment >= s->u_fragment_start) &&
  1058. (s->last_coded_y_fragment == -1) &&
  1059. (!first_c_fragment_seen)) {
  1060. s->first_coded_c_fragment = s->coded_fragment_list_index;
  1061. s->last_coded_y_fragment = s->first_coded_c_fragment - 1;
  1062. first_c_fragment_seen = 1;
  1063. }
  1064. s->coded_fragment_list_index++;
  1065. s->macroblock_coding[s->all_fragments[current_fragment].macroblock] = MODE_INTER_NO_MV;
  1066. debug_block_coding(" superblock %d is partially coded, fragment %d is coded\n",
  1067. i, current_fragment);
  1068. } else {
  1069. /* not coded; copy this fragment from the prior frame */
  1070. s->all_fragments[current_fragment].coding_method =
  1071. MODE_COPY;
  1072. debug_block_coding(" superblock %d is partially coded, fragment %d is not coded\n",
  1073. i, current_fragment);
  1074. }
  1075. current_run--;
  1076. } else {
  1077. /* fragments are fully coded in this superblock; actual
  1078. * coding will be determined in next step */
  1079. s->all_fragments[current_fragment].coding_method =
  1080. MODE_INTER_NO_MV;
  1081. s->coded_fragment_list[s->coded_fragment_list_index] =
  1082. current_fragment;
  1083. if ((current_fragment >= s->u_fragment_start) &&
  1084. (s->last_coded_y_fragment == -1) &&
  1085. (!first_c_fragment_seen)) {
  1086. s->first_coded_c_fragment = s->coded_fragment_list_index;
  1087. s->last_coded_y_fragment = s->first_coded_c_fragment - 1;
  1088. first_c_fragment_seen = 1;
  1089. }
  1090. s->coded_fragment_list_index++;
  1091. s->macroblock_coding[s->all_fragments[current_fragment].macroblock] = MODE_INTER_NO_MV;
  1092. debug_block_coding(" superblock %d is fully coded, fragment %d is coded\n",
  1093. i, current_fragment);
  1094. }
  1095. }
  1096. }
  1097. }
  1098. if (!first_c_fragment_seen)
  1099. /* only Y fragments coded in this frame */
  1100. s->last_coded_y_fragment = s->coded_fragment_list_index - 1;
  1101. else
  1102. /* end the list of coded fragments */
  1103. s->last_coded_c_fragment = s->coded_fragment_list_index - 1;
  1104. debug_block_coding(" %d total coded fragments, y: %d -> %d, c: %d -> %d\n",
  1105. s->coded_fragment_list_index,
  1106. s->first_coded_y_fragment,
  1107. s->last_coded_y_fragment,
  1108. s->first_coded_c_fragment,
  1109. s->last_coded_c_fragment);
  1110. return 0;
  1111. }
  1112. /*
  1113. * This function unpacks all the coding mode data for individual macroblocks
  1114. * from the bitstream.
  1115. */
  1116. static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
  1117. {
  1118. int i, j, k;
  1119. int scheme;
  1120. int current_macroblock;
  1121. int current_fragment;
  1122. int coding_mode;
  1123. debug_vp3(" vp3: unpacking encoding modes\n");
  1124. if (s->keyframe) {
  1125. debug_vp3(" keyframe-- all blocks are coded as INTRA\n");
  1126. for (i = 0; i < s->fragment_count; i++)
  1127. s->all_fragments[i].coding_method = MODE_INTRA;
  1128. } else {
  1129. /* fetch the mode coding scheme for this frame */
  1130. scheme = get_bits(gb, 3);
  1131. debug_modes(" using mode alphabet %d\n", scheme);
  1132. /* is it a custom coding scheme? */
  1133. if (scheme == 0) {
  1134. debug_modes(" custom mode alphabet ahead:\n");
  1135. for (i = 0; i < 8; i++)
  1136. ModeAlphabet[scheme][get_bits(gb, 3)] = i;
  1137. }
  1138. for (i = 0; i < 8; i++)
  1139. debug_modes(" mode[%d][%d] = %d\n", scheme, i,
  1140. ModeAlphabet[scheme][i]);
  1141. /* iterate through all of the macroblocks that contain 1 or more
  1142. * coded fragments */
  1143. for (i = 0; i < s->u_superblock_start; i++) {
  1144. for (j = 0; j < 4; j++) {
  1145. current_macroblock = s->superblock_macroblocks[i * 4 + j];
  1146. if ((current_macroblock == -1) ||
  1147. (s->macroblock_coding[current_macroblock] == MODE_COPY))
  1148. continue;
  1149. if (current_macroblock >= s->macroblock_count) {
  1150. printf (" vp3:unpack_modes(): bad macroblock number (%d >= %d)\n",
  1151. current_macroblock, s->macroblock_count);
  1152. return 1;
  1153. }
  1154. /* mode 7 means get 3 bits for each coding mode */
  1155. if (scheme == 7)
  1156. coding_mode = get_bits(gb, 3);
  1157. else
  1158. coding_mode = ModeAlphabet[scheme][get_mode_code(gb)];
  1159. s->macroblock_coding[current_macroblock] = coding_mode;
  1160. for (k = 0; k < 6; k++) {
  1161. current_fragment =
  1162. s->macroblock_fragments[current_macroblock * 6 + k];
  1163. if (current_fragment == -1)
  1164. continue;
  1165. if (current_fragment >= s->fragment_count) {
  1166. printf (" vp3:unpack_modes(): bad fragment number (%d >= %d)\n",
  1167. current_fragment, s->fragment_count);
  1168. return 1;
  1169. }
  1170. if (s->all_fragments[current_fragment].coding_method !=
  1171. MODE_COPY)
  1172. s->all_fragments[current_fragment].coding_method =
  1173. coding_mode;
  1174. }
  1175. debug_modes(" coding method for macroblock starting @ fragment %d = %d\n",
  1176. s->macroblock_fragments[current_macroblock * 6], coding_mode);
  1177. }
  1178. }
  1179. }
  1180. return 0;
  1181. }
  1182. /*
  1183. * This function adjusts the components of a motion vector for the halfpel
  1184. * motion grid. c_plane indicates whether the vector applies to the U or V
  1185. * plane. The function returns the halfpel function index to be used in
  1186. * ffmpeg's put_pixels[]() array of functions.
  1187. */
  1188. static inline int adjust_vector(int *x, int *y, int c_plane)
  1189. {
  1190. int motion_halfpel_index = 0;
  1191. int x_halfpel;
  1192. int y_halfpel;
  1193. if (!c_plane) {
  1194. x_halfpel = *x & 1;
  1195. motion_halfpel_index |= x_halfpel;
  1196. if (*x >= 0)
  1197. *x >>= 1;
  1198. else
  1199. *x = -( (-(*x) >> 1) + x_halfpel);
  1200. y_halfpel = *y & 1;
  1201. motion_halfpel_index |= (y_halfpel << 1);
  1202. if (*y >= 0)
  1203. *y >>= 1;
  1204. else
  1205. *y = -( (-(*y) >> 1) + y_halfpel);
  1206. } else {
  1207. x_halfpel = ((*x & 0x03) != 0);
  1208. motion_halfpel_index |= x_halfpel;
  1209. if (*x >= 0)
  1210. *x >>= 2;
  1211. else
  1212. *x = -( (-(*x) >> 2) + x_halfpel);
  1213. y_halfpel = ((*y & 0x03) != 0);
  1214. motion_halfpel_index |= (y_halfpel << 1);
  1215. if (*y >= 0)
  1216. *y >>= 2;
  1217. else
  1218. *y = -( (-(*y) >> 2) + y_halfpel);
  1219. }
  1220. return motion_halfpel_index;
  1221. }
  1222. /*
  1223. * This function unpacks all the motion vectors for the individual
  1224. * macroblocks from the bitstream.
  1225. */
  1226. static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
  1227. {
  1228. int i, j, k;
  1229. int coding_mode;
  1230. int motion_x[6];
  1231. int motion_y[6];
  1232. int last_motion_x = 0;
  1233. int last_motion_y = 0;
  1234. int prior_last_motion_x = 0;
  1235. int prior_last_motion_y = 0;
  1236. int current_macroblock;
  1237. int current_fragment;
  1238. debug_vp3(" vp3: unpacking motion vectors\n");
  1239. if (s->keyframe) {
  1240. debug_vp3(" keyframe-- there are no motion vectors\n");
  1241. } else {
  1242. memset(motion_x, 0, 6 * sizeof(int));
  1243. memset(motion_y, 0, 6 * sizeof(int));
  1244. /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme */
  1245. coding_mode = get_bits(gb, 1);
  1246. debug_vectors(" using %s scheme for unpacking motion vectors\n",
  1247. (coding_mode == 0) ? "VLC" : "fixed-length");
  1248. /* iterate through all of the macroblocks that contain 1 or more
  1249. * coded fragments */
  1250. for (i = 0; i < s->u_superblock_start; i++) {
  1251. for (j = 0; j < 4; j++) {
  1252. current_macroblock = s->superblock_macroblocks[i * 4 + j];
  1253. if ((current_macroblock == -1) ||
  1254. (s->macroblock_coding[current_macroblock] == MODE_COPY))
  1255. continue;
  1256. if (current_macroblock >= s->macroblock_count) {
  1257. printf (" vp3:unpack_vectors(): bad macroblock number (%d >= %d)\n",
  1258. current_macroblock, s->macroblock_count);
  1259. return 1;
  1260. }
  1261. current_fragment = s->macroblock_fragments[current_macroblock * 6];
  1262. if (current_fragment >= s->fragment_count) {
  1263. printf (" vp3:unpack_vectors(): bad fragment number (%d >= %d\n",
  1264. current_fragment, s->fragment_count);
  1265. return 1;
  1266. }
  1267. switch (s->macroblock_coding[current_macroblock]) {
  1268. case MODE_INTER_PLUS_MV:
  1269. case MODE_GOLDEN_MV:
  1270. /* all 6 fragments use the same motion vector */
  1271. if (coding_mode == 0) {
  1272. motion_x[0] = get_motion_vector_vlc(gb);
  1273. motion_y[0] = get_motion_vector_vlc(gb);
  1274. } else {
  1275. motion_x[0] = get_motion_vector_fixed(gb);
  1276. motion_y[0] = get_motion_vector_fixed(gb);
  1277. }
  1278. for (k = 1; k < 6; k++) {
  1279. motion_x[k] = motion_x[0];
  1280. motion_y[k] = motion_y[0];
  1281. }
  1282. /* vector maintenance, only on MODE_INTER_PLUS_MV */
  1283. if (s->all_fragments[current_fragment].coding_method ==
  1284. MODE_INTER_PLUS_MV) {
  1285. prior_last_motion_x = last_motion_x;
  1286. prior_last_motion_y = last_motion_y;
  1287. last_motion_x = motion_x[0];
  1288. last_motion_y = motion_y[0];
  1289. }
  1290. break;
  1291. case MODE_INTER_FOURMV:
  1292. /* fetch 4 vectors from the bitstream, one for each
  1293. * Y fragment, then average for the C fragment vectors */
  1294. motion_x[4] = motion_y[4] = 0;
  1295. for (k = 0; k < 4; k++) {
  1296. if (coding_mode == 0) {
  1297. motion_x[k] = get_motion_vector_vlc(gb);
  1298. motion_y[k] = get_motion_vector_vlc(gb);
  1299. } else {
  1300. motion_x[k] = get_motion_vector_fixed(gb);
  1301. motion_y[k] = get_motion_vector_fixed(gb);
  1302. }
  1303. motion_x[4] += motion_x[k];
  1304. motion_y[4] += motion_y[k];
  1305. }
  1306. if (motion_x[4] >= 0)
  1307. motion_x[4] = (motion_x[4] + 2) / 4;
  1308. else
  1309. motion_x[4] = (motion_x[4] - 2) / 4;
  1310. motion_x[5] = motion_x[4];
  1311. if (motion_y[4] >= 0)
  1312. motion_y[4] = (motion_y[4] + 2) / 4;
  1313. else
  1314. motion_y[4] = (motion_y[4] - 2) / 4;
  1315. motion_y[5] = motion_y[4];
  1316. /* vector maintenance; vector[3] is treated as the
  1317. * last vector in this case */
  1318. prior_last_motion_x = last_motion_x;
  1319. prior_last_motion_y = last_motion_y;
  1320. last_motion_x = motion_x[3];
  1321. last_motion_y = motion_y[3];
  1322. break;
  1323. case MODE_INTER_LAST_MV:
  1324. /* all 6 fragments use the last motion vector */
  1325. motion_x[0] = last_motion_x;
  1326. motion_y[0] = last_motion_y;
  1327. for (k = 1; k < 6; k++) {
  1328. motion_x[k] = motion_x[0];
  1329. motion_y[k] = motion_y[0];
  1330. }
  1331. /* no vector maintenance (last vector remains the
  1332. * last vector) */
  1333. break;
  1334. case MODE_INTER_PRIOR_LAST:
  1335. /* all 6 fragments use the motion vector prior to the
  1336. * last motion vector */
  1337. motion_x[0] = prior_last_motion_x;
  1338. motion_y[0] = prior_last_motion_y;
  1339. for (k = 1; k < 6; k++) {
  1340. motion_x[k] = motion_x[0];
  1341. motion_y[k] = motion_y[0];
  1342. }
  1343. /* vector maintenance */
  1344. prior_last_motion_x = last_motion_x;
  1345. prior_last_motion_y = last_motion_y;
  1346. last_motion_x = motion_x[0];
  1347. last_motion_y = motion_y[0];
  1348. break;
  1349. default:
  1350. /* covers intra, inter without MV, golden without MV */
  1351. memset(motion_x, 0, 6 * sizeof(int));
  1352. memset(motion_y, 0, 6 * sizeof(int));
  1353. /* no vector maintenance */
  1354. break;
  1355. }
  1356. /* assign the motion vectors to the correct fragments */
  1357. debug_vectors(" vectors for macroblock starting @ fragment %d (coding method %d):\n",
  1358. current_fragment,
  1359. s->all_fragments[current_fragment].coding_method);
  1360. for (k = 0; k < 6; k++) {
  1361. current_fragment =
  1362. s->macroblock_fragments[current_macroblock * 6 + k];
  1363. if (current_fragment == -1)
  1364. continue;
  1365. if (current_fragment >= s->fragment_count) {
  1366. printf (" vp3:unpack_vectors(): bad fragment number (%d >= %d)\n",
  1367. current_fragment, s->fragment_count);
  1368. return 1;
  1369. }
  1370. s->all_fragments[current_fragment].motion_halfpel_index =
  1371. adjust_vector(&motion_x[k], &motion_y[k],
  1372. ((k == 4) || (k == 5)));
  1373. s->all_fragments[current_fragment].motion_x = motion_x[k];
  1374. s->all_fragments[current_fragment].motion_y = motion_y[k];
  1375. debug_vectors(" vector %d: fragment %d = (%d, %d), index %d\n",
  1376. k, current_fragment, motion_x[k], motion_y[k],
  1377. s->all_fragments[current_fragment].motion_halfpel_index);
  1378. }
  1379. }
  1380. }
  1381. }
  1382. return 0;
  1383. }
  1384. /*
  1385. * This function is called by unpack_dct_coeffs() to extract the VLCs from
  1386. * the bitstream. The VLCs encode tokens which are used to unpack DCT
  1387. * data. This function unpacks all the VLCs for either the Y plane or both
  1388. * C planes, and is called for DC coefficients or different AC coefficient
  1389. * levels (since different coefficient types require different VLC tables.
  1390. *
  1391. * This function returns a residual eob run. E.g, if a particular token gave
  1392. * instructions to EOB the next 5 fragments and there were only 2 fragments
  1393. * left in the current fragment range, 3 would be returned so that it could
  1394. * be passed into the next call to this same function.
  1395. */
  1396. static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
  1397. VLC *table, int coeff_index,
  1398. int first_fragment, int last_fragment,
  1399. int eob_run)
  1400. {
  1401. int i;
  1402. int token;
  1403. int zero_run;
  1404. DCTELEM coeff;
  1405. Vp3Fragment *fragment;
  1406. if ((first_fragment >= s->fragment_count) ||
  1407. (last_fragment >= s->fragment_count)) {
  1408. printf (" vp3:unpack_vlcs(): bad fragment number (%d -> %d ?)\n",
  1409. first_fragment, last_fragment);
  1410. return 0;
  1411. }
  1412. for (i = first_fragment; i <= last_fragment; i++) {
  1413. fragment = &s->all_fragments[s->coded_fragment_list[i]];
  1414. if (fragment->coeff_count > coeff_index)
  1415. continue;
  1416. if (!eob_run) {
  1417. /* decode a VLC into a token */
  1418. token = get_vlc2(gb, table->table, 5, 3);
  1419. debug_vlc(" token = %2d, ", token);
  1420. /* use the token to get a zero run, a coefficient, and an eob run */
  1421. unpack_token(gb, token, &zero_run, &coeff, &eob_run);
  1422. }
  1423. if (!eob_run) {
  1424. fragment->coeff_count += zero_run;
  1425. if (fragment->coeff_count < 64)
  1426. fragment->coeffs[fragment->coeff_count++] = coeff;
  1427. debug_vlc(" fragment %d coeff = %d\n",
  1428. s->coded_fragment_list[i], fragment->coeffs[coeff_index]);
  1429. } else {
  1430. fragment->last_coeff = fragment->coeff_count;
  1431. fragment->coeff_count = 64;
  1432. debug_vlc(" fragment %d eob with %d coefficients\n",
  1433. s->coded_fragment_list[i], fragment->last_coeff);
  1434. eob_run--;
  1435. }
  1436. }
  1437. return eob_run;
  1438. }
  1439. /*
  1440. * This function unpacks all of the DCT coefficient data from the
  1441. * bitstream.
  1442. */
  1443. static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
  1444. {
  1445. int i;
  1446. int dc_y_table;
  1447. int dc_c_table;
  1448. int ac_y_table;
  1449. int ac_c_table;
  1450. int residual_eob_run = 0;
  1451. /* fetch the DC table indices */
  1452. dc_y_table = get_bits(gb, 4);
  1453. dc_c_table = get_bits(gb, 4);
  1454. /* unpack the Y plane DC coefficients */
  1455. debug_vp3(" vp3: unpacking Y plane DC coefficients using table %d\n",
  1456. dc_y_table);
  1457. residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
  1458. s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
  1459. /* unpack the C plane DC coefficients */
  1460. debug_vp3(" vp3: unpacking C plane DC coefficients using table %d\n",
  1461. dc_c_table);
  1462. residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
  1463. s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
  1464. /* fetch the AC table indices */
  1465. ac_y_table = get_bits(gb, 4);
  1466. ac_c_table = get_bits(gb, 4);
  1467. /* unpack the group 1 AC coefficients (coeffs 1-5) */
  1468. for (i = 1; i <= 5; i++) {
  1469. debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
  1470. i, ac_y_table);
  1471. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_y_table], i,
  1472. s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
  1473. debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
  1474. i, ac_c_table);
  1475. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_c_table], i,
  1476. s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
  1477. }
  1478. /* unpack the group 2 AC coefficients (coeffs 6-14) */
  1479. for (i = 6; i <= 14; i++) {
  1480. debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
  1481. i, ac_y_table);
  1482. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_y_table], i,
  1483. s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
  1484. debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
  1485. i, ac_c_table);
  1486. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_c_table], i,
  1487. s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
  1488. }
  1489. /* unpack the group 3 AC coefficients (coeffs 15-27) */
  1490. for (i = 15; i <= 27; i++) {
  1491. debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
  1492. i, ac_y_table);
  1493. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_y_table], i,
  1494. s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
  1495. debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
  1496. i, ac_c_table);
  1497. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_c_table], i,
  1498. s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
  1499. }
  1500. /* unpack the group 4 AC coefficients (coeffs 28-63) */
  1501. for (i = 28; i <= 63; i++) {
  1502. debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
  1503. i, ac_y_table);
  1504. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_y_table], i,
  1505. s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
  1506. debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
  1507. i, ac_c_table);
  1508. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_c_table], i,
  1509. s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
  1510. }
  1511. return 0;
  1512. }
  1513. /*
  1514. * This function reverses the DC prediction for each coded fragment in
  1515. * the frame. Much of this function is adapted directly from the original
  1516. * VP3 source code.
  1517. */
  1518. #define COMPATIBLE_FRAME(x) \
  1519. (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
  1520. #define FRAME_CODED(x) (s->all_fragments[x].coding_method != MODE_COPY)
  1521. static inline int iabs (int x) { return ((x < 0) ? -x : x); }
  1522. static void reverse_dc_prediction(Vp3DecodeContext *s,
  1523. int first_fragment,
  1524. int fragment_width,
  1525. int fragment_height)
  1526. {
  1527. #define PUL 8
  1528. #define PU 4
  1529. #define PUR 2
  1530. #define PL 1
  1531. int x, y;
  1532. int i = first_fragment;
  1533. /*
  1534. * Fragment prediction groups:
  1535. *
  1536. * 32222222226
  1537. * 10000000004
  1538. * 10000000004
  1539. * 10000000004
  1540. * 10000000004
  1541. *
  1542. * Note: Groups 5 and 7 do not exist as it would mean that the
  1543. * fragment's x coordinate is both 0 and (width - 1) at the same time.
  1544. */
  1545. int predictor_group;
  1546. short predicted_dc;
  1547. /* validity flags for the left, up-left, up, and up-right fragments */
  1548. int fl, ful, fu, fur;
  1549. /* DC values for the left, up-left, up, and up-right fragments */
  1550. int vl, vul, vu, vur;
  1551. /* indices for the left, up-left, up, and up-right fragments */
  1552. int l, ul, u, ur;
  1553. /*
  1554. * The 6 fields mean:
  1555. * 0: up-left multiplier
  1556. * 1: up multiplier
  1557. * 2: up-right multiplier
  1558. * 3: left multiplier
  1559. * 4: mask
  1560. * 5: right bit shift divisor (e.g., 7 means >>=7, a.k.a. div by 128)
  1561. */
  1562. int predictor_transform[16][6] = {
  1563. { 0, 0, 0, 0, 0, 0 },
  1564. { 0, 0, 0, 1, 0, 0 }, // PL
  1565. { 0, 0, 1, 0, 0, 0 }, // PUR
  1566. { 0, 0, 53, 75, 127, 7 }, // PUR|PL
  1567. { 0, 1, 0, 0, 0, 0 }, // PU
  1568. { 0, 1, 0, 1, 1, 1 }, // PU|PL
  1569. { 0, 1, 0, 0, 0, 0 }, // PU|PUR
  1570. { 0, 0, 53, 75, 127, 7 }, // PU|PUR|PL
  1571. { 1, 0, 0, 0, 0, 0 }, // PUL
  1572. { 0, 0, 0, 1, 0, 0 }, // PUL|PL
  1573. { 1, 0, 1, 0, 1, 1 }, // PUL|PUR
  1574. { 0, 0, 53, 75, 127, 7 }, // PUL|PUR|PL
  1575. { 0, 1, 0, 0, 0, 0 }, // PUL|PU
  1576. {-26, 29, 0, 29, 31, 5 }, // PUL|PU|PL
  1577. { 3, 10, 3, 0, 15, 4 }, // PUL|PU|PUR
  1578. {-26, 29, 0, 29, 31, 5 } // PUL|PU|PUR|PL
  1579. };
  1580. /* This table shows which types of blocks can use other blocks for
  1581. * prediction. For example, INTRA is the only mode in this table to
  1582. * have a frame number of 0. That means INTRA blocks can only predict
  1583. * from other INTRA blocks. There are 2 golden frame coding types;
  1584. * blocks encoding in these modes can only predict from other blocks
  1585. * that were encoded with these 1 of these 2 modes. */
  1586. unsigned char compatible_frame[8] = {
  1587. 1, /* MODE_INTER_NO_MV */
  1588. 0, /* MODE_INTRA */
  1589. 1, /* MODE_INTER_PLUS_MV */
  1590. 1, /* MODE_INTER_LAST_MV */
  1591. 1, /* MODE_INTER_PRIOR_MV */
  1592. 2, /* MODE_USING_GOLDEN */
  1593. 2, /* MODE_GOLDEN_MV */
  1594. 1 /* MODE_INTER_FOUR_MV */
  1595. };
  1596. int current_frame_type;
  1597. /* there is a last DC predictor for each of the 3 frame types */
  1598. short last_dc[3];
  1599. int transform = 0;
  1600. debug_vp3(" vp3: reversing DC prediction\n");
  1601. vul = vu = vur = vl = 0;
  1602. last_dc[0] = last_dc[1] = last_dc[2] = 0;
  1603. /* for each fragment row... */
  1604. for (y = 0; y < fragment_height; y++) {
  1605. /* for each fragment in a row... */
  1606. for (x = 0; x < fragment_width; x++, i++) {
  1607. /* reverse prediction if this block was coded */
  1608. if (s->all_fragments[i].coding_method != MODE_COPY) {
  1609. current_frame_type =
  1610. compatible_frame[s->all_fragments[i].coding_method];
  1611. predictor_group = (x == 0) + ((y == 0) << 1) +
  1612. ((x + 1 == fragment_width) << 2);
  1613. debug_dc_pred(" frag %d: group %d, orig DC = %d, ",
  1614. i, predictor_group, s->all_fragments[i].coeffs[0]);
  1615. switch (predictor_group) {
  1616. case 0:
  1617. /* main body of fragments; consider all 4 possible
  1618. * fragments for prediction */
  1619. /* calculate the indices of the predicting fragments */
  1620. ul = i - fragment_width - 1;
  1621. u = i - fragment_width;
  1622. ur = i - fragment_width + 1;
  1623. l = i - 1;
  1624. /* fetch the DC values for the predicting fragments */
  1625. vul = s->all_fragments[ul].coeffs[0];
  1626. vu = s->all_fragments[u].coeffs[0];
  1627. vur = s->all_fragments[ur].coeffs[0];
  1628. vl = s->all_fragments[l].coeffs[0];
  1629. /* figure out which fragments are valid */
  1630. ful = FRAME_CODED(ul) && COMPATIBLE_FRAME(ul);
  1631. fu = FRAME_CODED(u) && COMPATIBLE_FRAME(u);
  1632. fur = FRAME_CODED(ur) && COMPATIBLE_FRAME(ur);
  1633. fl = FRAME_CODED(l) && COMPATIBLE_FRAME(l);
  1634. /* decide which predictor transform to use */
  1635. transform = (fl*PL) | (fu*PU) | (ful*PUL) | (fur*PUR);
  1636. break;
  1637. case 1:
  1638. /* left column of fragments, not including top corner;
  1639. * only consider up and up-right fragments */
  1640. /* calculate the indices of the predicting fragments */
  1641. u = i - fragment_width;
  1642. ur = i - fragment_width + 1;
  1643. /* fetch the DC values for the predicting fragments */
  1644. vu = s->all_fragments[u].coeffs[0];
  1645. vur = s->all_fragments[ur].coeffs[0];
  1646. /* figure out which fragments are valid */
  1647. fur = FRAME_CODED(ur) && COMPATIBLE_FRAME(ur);
  1648. fu = FRAME_CODED(u) && COMPATIBLE_FRAME(u);
  1649. /* decide which predictor transform to use */
  1650. transform = (fu*PU) | (fur*PUR);
  1651. break;
  1652. case 2:
  1653. case 6:
  1654. /* top row of fragments, not including top-left frag;
  1655. * only consider the left fragment for prediction */
  1656. /* calculate the indices of the predicting fragments */
  1657. l = i - 1;
  1658. /* fetch the DC values for the predicting fragments */
  1659. vl = s->all_fragments[l].coeffs[0];
  1660. /* figure out which fragments are valid */
  1661. fl = FRAME_CODED(l) && COMPATIBLE_FRAME(l);
  1662. /* decide which predictor transform to use */
  1663. transform = (fl*PL);
  1664. break;
  1665. case 3:
  1666. /* top-left fragment */
  1667. /* nothing to predict from in this case */
  1668. transform = 0;
  1669. break;
  1670. case 4:
  1671. /* right column of fragments, not including top corner;
  1672. * consider up-left, up, and left fragments for
  1673. * prediction */
  1674. /* calculate the indices of the predicting fragments */
  1675. ul = i - fragment_width - 1;
  1676. u = i - fragment_width;
  1677. l = i - 1;
  1678. /* fetch the DC values for the predicting fragments */
  1679. vul = s->all_fragments[ul].coeffs[0];
  1680. vu = s->all_fragments[u].coeffs[0];
  1681. vl = s->all_fragments[l].coeffs[0];
  1682. /* figure out which fragments are valid */
  1683. ful = FRAME_CODED(ul) && COMPATIBLE_FRAME(ul);
  1684. fu = FRAME_CODED(u) && COMPATIBLE_FRAME(u);
  1685. fl = FRAME_CODED(l) && COMPATIBLE_FRAME(l);
  1686. /* decide which predictor transform to use */
  1687. transform = (fl*PL) | (fu*PU) | (ful*PUL);
  1688. break;
  1689. }
  1690. debug_dc_pred("transform = %d, ", transform);
  1691. if (transform == 0) {
  1692. /* if there were no fragments to predict from, use last
  1693. * DC saved */
  1694. s->all_fragments[i].coeffs[0] += last_dc[current_frame_type];
  1695. debug_dc_pred("from last DC (%d) = %d\n",
  1696. current_frame_type, s->all_fragments[i].coeffs[0]);
  1697. } else {
  1698. /* apply the appropriate predictor transform */
  1699. predicted_dc =
  1700. (predictor_transform[transform][0] * vul) +
  1701. (predictor_transform[transform][1] * vu) +
  1702. (predictor_transform[transform][2] * vur) +
  1703. (predictor_transform[transform][3] * vl);
  1704. /* if there is a shift value in the transform, add
  1705. * the sign bit before the shift */
  1706. if (predictor_transform[transform][5] != 0) {
  1707. predicted_dc += ((predicted_dc >> 15) &
  1708. predictor_transform[transform][4]);
  1709. predicted_dc >>= predictor_transform[transform][5];
  1710. }
  1711. /* check for outranging on the [ul u l] and
  1712. * [ul u ur l] predictors */
  1713. if ((transform == 13) || (transform == 15)) {
  1714. if (iabs(predicted_dc - vu) > 128)
  1715. predicted_dc = vu;
  1716. else if (iabs(predicted_dc - vl) > 128)
  1717. predicted_dc = vl;
  1718. else if (iabs(predicted_dc - vul) > 128)
  1719. predicted_dc = vul;
  1720. }
  1721. /* at long last, apply the predictor */
  1722. s->all_fragments[i].coeffs[0] += predicted_dc;
  1723. debug_dc_pred("from pred DC = %d\n",
  1724. s->all_fragments[i].coeffs[0]);
  1725. }
  1726. /* save the DC */
  1727. last_dc[current_frame_type] = s->all_fragments[i].coeffs[0];
  1728. }
  1729. }
  1730. }
  1731. }
  1732. /*
  1733. * This function performs the final rendering of each fragment's data
  1734. * onto the output frame.
  1735. */
  1736. static void render_fragments(Vp3DecodeContext *s,
  1737. int first_fragment,
  1738. int width,
  1739. int height,
  1740. int plane /* 0 = Y, 1 = U, 2 = V */)
  1741. {
  1742. int x, y;
  1743. int m, n;
  1744. int i = first_fragment;
  1745. int j;
  1746. int16_t *dequantizer;
  1747. DCTELEM dequant_block[64];
  1748. DCTELEM dequant_block_permuted[64];
  1749. unsigned char *output_plane;
  1750. unsigned char *last_plane;
  1751. unsigned char *golden_plane;
  1752. int stride;
  1753. int motion_x, motion_y;
  1754. int upper_motion_limit, lower_motion_limit;
  1755. int motion_halfpel_index;
  1756. unsigned int motion_source;
  1757. debug_vp3(" vp3: rendering final fragments for %s\n",
  1758. (plane == 0) ? "Y plane" : (plane == 1) ? "U plane" : "V plane");
  1759. /* set up plane-specific parameters */
  1760. if (plane == 0) {
  1761. dequantizer = s->intra_y_dequant;
  1762. output_plane = s->current_frame.data[0];
  1763. last_plane = s->last_frame.data[0];
  1764. golden_plane = s->golden_frame.data[0];
  1765. stride = -s->current_frame.linesize[0];
  1766. upper_motion_limit = 7 * s->current_frame.linesize[0];
  1767. lower_motion_limit = height * s->current_frame.linesize[0] + width - 8;
  1768. } else if (plane == 1) {
  1769. dequantizer = s->intra_c_dequant;
  1770. output_plane = s->current_frame.data[1];
  1771. last_plane = s->last_frame.data[1];
  1772. golden_plane = s->golden_frame.data[1];
  1773. stride = -s->current_frame.linesize[1];
  1774. upper_motion_limit = 7 * s->current_frame.linesize[1];
  1775. lower_motion_limit = height * s->current_frame.linesize[1] + width - 8;
  1776. } else {
  1777. dequantizer = s->intra_c_dequant;
  1778. output_plane = s->current_frame.data[2];
  1779. last_plane = s->last_frame.data[2];
  1780. golden_plane = s->golden_frame.data[2];
  1781. stride = -s->current_frame.linesize[2];
  1782. upper_motion_limit = 7 * s->current_frame.linesize[2];
  1783. lower_motion_limit = height * s->current_frame.linesize[2] + width - 8;
  1784. }
  1785. /* for each fragment row... */
  1786. for (y = 0; y < height; y += 8) {
  1787. /* for each fragment in a row... */
  1788. for (x = 0; x < width; x += 8, i++) {
  1789. if ((i < 0) || (i >= s->fragment_count)) {
  1790. printf (" vp3:render_fragments(): bad fragment number (%d)\n", i);
  1791. return;
  1792. }
  1793. /* transform if this block was coded */
  1794. if (s->all_fragments[i].coding_method != MODE_COPY) {
  1795. /* sort out the motion vector */
  1796. motion_x = s->all_fragments[i].motion_x;
  1797. motion_y = s->all_fragments[i].motion_y;
  1798. motion_halfpel_index = s->all_fragments[i].motion_halfpel_index;
  1799. motion_source = s->all_fragments[i].first_pixel;
  1800. motion_source += motion_x;
  1801. motion_source += (motion_y * stride);
  1802. /* if the are any problems with a motion vector, refuse
  1803. * to render the block */
  1804. if ((motion_source < upper_motion_limit) ||
  1805. (motion_source > lower_motion_limit)) {
  1806. // printf (" vp3: help! motion source (%d) out of range (%d..%d)\n",
  1807. // motion_source, upper_motion_limit, lower_motion_limit);
  1808. continue;
  1809. }
  1810. /* first, take care of copying a block from either the
  1811. * previous or the golden frame */
  1812. if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
  1813. (s->all_fragments[i].coding_method == MODE_GOLDEN_MV)) {
  1814. s->dsp.put_pixels_tab[1][motion_halfpel_index](
  1815. output_plane + s->all_fragments[i].first_pixel,
  1816. golden_plane + motion_source,
  1817. stride, 8);
  1818. } else
  1819. if (s->all_fragments[i].coding_method != MODE_INTRA) {
  1820. s->dsp.put_pixels_tab[1][motion_halfpel_index](
  1821. output_plane + s->all_fragments[i].first_pixel,
  1822. last_plane + motion_source,
  1823. stride, 8);
  1824. }
  1825. /* dequantize the DCT coefficients */
  1826. debug_idct("fragment %d, coding mode %d, DC = %d, dequant = %d:\n",
  1827. i, s->all_fragments[i].coding_method,
  1828. s->all_fragments[i].coeffs[0], dequantizer[0]);
  1829. for (j = 0; j < 64; j++)
  1830. dequant_block[dezigzag_index[j]] =
  1831. s->all_fragments[i].coeffs[j] *
  1832. dequantizer[j];
  1833. for (j = 0; j < 64; j++)
  1834. dequant_block_permuted[s->dsp.idct_permutation[j]] =
  1835. dequant_block[j];
  1836. debug_idct("dequantized block:\n");
  1837. for (m = 0; m < 8; m++) {
  1838. for (n = 0; n < 8; n++) {
  1839. debug_idct(" %5d", dequant_block[m * 8 + n]);
  1840. }
  1841. debug_idct("\n");
  1842. }
  1843. debug_idct("\n");
  1844. /* invert DCT and place (or add) in final output */
  1845. if (s->all_fragments[i].coding_method == MODE_INTRA) {
  1846. dequant_block_permuted[0] += 1024;
  1847. s->dsp.idct_put(
  1848. output_plane + s->all_fragments[i].first_pixel,
  1849. stride, dequant_block_permuted);
  1850. } else {
  1851. s->dsp.idct_add(
  1852. output_plane + s->all_fragments[i].first_pixel,
  1853. stride, dequant_block_permuted);
  1854. }
  1855. debug_idct("block after idct_%s():\n",
  1856. (s->all_fragments[i].coding_method == MODE_INTRA)?
  1857. "put" : "add");
  1858. for (m = 0; m < 8; m++) {
  1859. for (n = 0; n < 8; n++) {
  1860. debug_idct(" %3d", *(output_plane +
  1861. s->all_fragments[i].first_pixel + (m * stride + n)));
  1862. }
  1863. debug_idct("\n");
  1864. }
  1865. debug_idct("\n");
  1866. } else {
  1867. /* copy directly from the previous frame */
  1868. s->dsp.put_pixels_tab[1][0](
  1869. output_plane + s->all_fragments[i].first_pixel,
  1870. last_plane + s->all_fragments[i].first_pixel,
  1871. stride, 8);
  1872. }
  1873. }
  1874. }
  1875. emms_c();
  1876. }
  1877. /*
  1878. * This function computes the first pixel addresses for each fragment.
  1879. * This function needs to be invoked after the first frame is allocated
  1880. * so that it has access to the plane strides.
  1881. */
  1882. static void vp3_calculate_pixel_addresses(Vp3DecodeContext *s)
  1883. {
  1884. int i, x, y;
  1885. /* figure out the first pixel addresses for each of the fragments */
  1886. /* Y plane */
  1887. i = 0;
  1888. for (y = s->fragment_height; y > 0; y--) {
  1889. for (x = 0; x < s->fragment_width; x++) {
  1890. s->all_fragments[i++].first_pixel =
  1891. s->golden_frame.linesize[0] * y * FRAGMENT_PIXELS -
  1892. s->golden_frame.linesize[0] +
  1893. x * FRAGMENT_PIXELS;
  1894. debug_init(" fragment %d, first pixel @ %d\n",
  1895. i-1, s->all_fragments[i-1].first_pixel);
  1896. }
  1897. }
  1898. /* U plane */
  1899. i = s->u_fragment_start;
  1900. for (y = s->fragment_height / 2; y > 0; y--) {
  1901. for (x = 0; x < s->fragment_width / 2; x++) {
  1902. s->all_fragments[i++].first_pixel =
  1903. s->golden_frame.linesize[1] * y * FRAGMENT_PIXELS -
  1904. s->golden_frame.linesize[1] +
  1905. x * FRAGMENT_PIXELS;
  1906. debug_init(" fragment %d, first pixel @ %d\n",
  1907. i-1, s->all_fragments[i-1].first_pixel);
  1908. }
  1909. }
  1910. /* V plane */
  1911. i = s->v_fragment_start;
  1912. for (y = s->fragment_height / 2; y > 0; y--) {
  1913. for (x = 0; x < s->fragment_width / 2; x++) {
  1914. s->all_fragments[i++].first_pixel =
  1915. s->golden_frame.linesize[2] * y * FRAGMENT_PIXELS -
  1916. s->golden_frame.linesize[2] +
  1917. x * FRAGMENT_PIXELS;
  1918. debug_init(" fragment %d, first pixel @ %d\n",
  1919. i-1, s->all_fragments[i-1].first_pixel);
  1920. }
  1921. }
  1922. }
  1923. /*
  1924. * This is the ffmpeg/libavcodec API init function.
  1925. */
  1926. static int vp3_decode_init(AVCodecContext *avctx)
  1927. {
  1928. Vp3DecodeContext *s = avctx->priv_data;
  1929. int i;
  1930. int c_width;
  1931. int c_height;
  1932. int y_superblock_count;
  1933. int c_superblock_count;
  1934. s->avctx = avctx;
  1935. s->width = avctx->width;
  1936. s->height = avctx->height;
  1937. avctx->pix_fmt = PIX_FMT_YUV420P;
  1938. avctx->has_b_frames = 0;
  1939. dsputil_init(&s->dsp, avctx);
  1940. /* initialize to an impossible value which will force a recalculation
  1941. * in the first frame decode */
  1942. s->quality_index = -1;
  1943. s->y_superblock_width = (s->width + 31) / 32;
  1944. s->y_superblock_height = (s->height + 31) / 32;
  1945. y_superblock_count = s->y_superblock_width * s->y_superblock_height;
  1946. /* work out the dimensions for the C planes */
  1947. c_width = s->width / 2;
  1948. c_height = s->height / 2;
  1949. s->c_superblock_width = (c_width + 31) / 32;
  1950. s->c_superblock_height = (c_height + 31) / 32;
  1951. c_superblock_count = s->c_superblock_width * s->c_superblock_height;
  1952. s->superblock_count = y_superblock_count + (c_superblock_count * 2);
  1953. s->u_superblock_start = y_superblock_count;
  1954. s->v_superblock_start = s->u_superblock_start + c_superblock_count;
  1955. s->superblock_coding = av_malloc(s->superblock_count);
  1956. s->macroblock_width = (s->width + 15) / 16;
  1957. s->macroblock_height = (s->height + 15) / 16;
  1958. s->macroblock_count = s->macroblock_width * s->macroblock_height;
  1959. s->fragment_width = s->width / FRAGMENT_PIXELS;
  1960. s->fragment_height = s->height / FRAGMENT_PIXELS;
  1961. /* fragment count covers all 8x8 blocks for all 3 planes */
  1962. s->fragment_count = s->fragment_width * s->fragment_height * 3 / 2;
  1963. s->u_fragment_start = s->fragment_width * s->fragment_height;
  1964. s->v_fragment_start = s->fragment_width * s->fragment_height * 5 / 4;
  1965. debug_init(" Y plane: %d x %d\n", s->width, s->height);
  1966. debug_init(" C plane: %d x %d\n", c_width, c_height);
  1967. debug_init(" Y superblocks: %d x %d, %d total\n",
  1968. s->y_superblock_width, s->y_superblock_height, y_superblock_count);
  1969. debug_init(" C superblocks: %d x %d, %d total\n",
  1970. s->c_superblock_width, s->c_superblock_height, c_superblock_count);
  1971. debug_init(" total superblocks = %d, U starts @ %d, V starts @ %d\n",
  1972. s->superblock_count, s->u_superblock_start, s->v_superblock_start);
  1973. debug_init(" macroblocks: %d x %d, %d total\n",
  1974. s->macroblock_width, s->macroblock_height, s->macroblock_count);
  1975. debug_init(" %d fragments, %d x %d, u starts @ %d, v starts @ %d\n",
  1976. s->fragment_count,
  1977. s->fragment_width,
  1978. s->fragment_height,
  1979. s->u_fragment_start,
  1980. s->v_fragment_start);
  1981. s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment));
  1982. s->coded_fragment_list = av_malloc(s->fragment_count * sizeof(int));
  1983. s->pixel_addresses_inited = 0;
  1984. /* init VLC tables */
  1985. for (i = 0; i < 16; i++) {
  1986. /* DC histograms */
  1987. init_vlc(&s->dc_vlc[i], 5, 32,
  1988. &dc_bias[i][0][1], 4, 2,
  1989. &dc_bias[i][0][0], 4, 2);
  1990. /* group 1 AC histograms */
  1991. init_vlc(&s->ac_vlc_1[i], 5, 32,
  1992. &ac_bias_0[i][0][1], 4, 2,
  1993. &ac_bias_0[i][0][0], 4, 2);
  1994. /* group 2 AC histograms */
  1995. init_vlc(&s->ac_vlc_2[i], 5, 32,
  1996. &ac_bias_1[i][0][1], 4, 2,
  1997. &ac_bias_1[i][0][0], 4, 2);
  1998. /* group 3 AC histograms */
  1999. init_vlc(&s->ac_vlc_3[i], 5, 32,
  2000. &ac_bias_2[i][0][1], 4, 2,
  2001. &ac_bias_2[i][0][0], 4, 2);
  2002. /* group 4 AC histograms */
  2003. init_vlc(&s->ac_vlc_4[i], 5, 32,
  2004. &ac_bias_3[i][0][1], 4, 2,
  2005. &ac_bias_3[i][0][0], 4, 2);
  2006. }
  2007. /* build quantization zigzag table */
  2008. for (i = 0; i < 64; i++)
  2009. zigzag_index[dezigzag_index[i]] = i;
  2010. /* work out the block mapping tables */
  2011. s->superblock_fragments = av_malloc(s->superblock_count * 16 * sizeof(int));
  2012. s->superblock_macroblocks = av_malloc(s->superblock_count * 4 * sizeof(int));
  2013. s->macroblock_fragments = av_malloc(s->macroblock_count * 6 * sizeof(int));
  2014. s->macroblock_coding = av_malloc(s->macroblock_count + 1);
  2015. init_block_mapping(s);
  2016. for (i = 0; i < 3; i++) {
  2017. s->current_frame.data[i] = NULL;
  2018. s->last_frame.data[i] = NULL;
  2019. s->golden_frame.data[i] = NULL;
  2020. }
  2021. return 0;
  2022. }
  2023. /*
  2024. * This is the ffmpeg/libavcodec API frame decode function.
  2025. */
  2026. static int vp3_decode_frame(AVCodecContext *avctx,
  2027. void *data, int *data_size,
  2028. uint8_t *buf, int buf_size)
  2029. {
  2030. Vp3DecodeContext *s = avctx->priv_data;
  2031. GetBitContext gb;
  2032. static int counter = 0;
  2033. *data_size = 0;
  2034. init_get_bits(&gb, buf, buf_size * 8);
  2035. s->keyframe = get_bits(&gb, 1);
  2036. s->keyframe ^= 1;
  2037. skip_bits(&gb, 1);
  2038. s->last_quality_index = s->quality_index;
  2039. s->quality_index = get_bits(&gb, 6);
  2040. if (s->quality_index != s->last_quality_index)
  2041. init_dequantizer(s);
  2042. debug_vp3(" VP3 frame #%d: Q index = %d", counter, s->quality_index);
  2043. counter++;
  2044. if (s->keyframe) {
  2045. debug_vp3(", keyframe\n");
  2046. /* skip the other 2 header bytes for now */
  2047. skip_bits(&gb, 16);
  2048. if (s->last_frame.data[0] == s->golden_frame.data[0]) {
  2049. if (s->golden_frame.data[0])
  2050. avctx->release_buffer(avctx, &s->golden_frame);
  2051. } else {
  2052. if (s->golden_frame.data[0])
  2053. avctx->release_buffer(avctx, &s->golden_frame);
  2054. if (s->last_frame.data[0])
  2055. avctx->release_buffer(avctx, &s->last_frame);
  2056. }
  2057. s->golden_frame.reference = 0;
  2058. if(avctx->get_buffer(avctx, &s->golden_frame) < 0) {
  2059. printf("vp3: get_buffer() failed\n");
  2060. return -1;
  2061. }
  2062. /* golden frame is also the current frame */
  2063. memcpy(&s->current_frame, &s->golden_frame, sizeof(AVFrame));
  2064. /* time to figure out pixel addresses? */
  2065. if (!s->pixel_addresses_inited)
  2066. vp3_calculate_pixel_addresses(s);
  2067. } else {
  2068. debug_vp3("\n");
  2069. /* allocate a new current frame */
  2070. s->current_frame.reference = 0;
  2071. if(avctx->get_buffer(avctx, &s->current_frame) < 0) {
  2072. printf("vp3: get_buffer() failed\n");
  2073. return -1;
  2074. }
  2075. }
  2076. init_frame(s, &gb);
  2077. #if KEYFRAMES_ONLY
  2078. if (!s->keyframe) {
  2079. memcpy(s->current_frame.data[0], s->golden_frame.data[0],
  2080. s->current_frame.linesize[0] * s->height);
  2081. memcpy(s->current_frame.data[1], s->golden_frame.data[1],
  2082. s->current_frame.linesize[1] * s->height / 2);
  2083. memcpy(s->current_frame.data[2], s->golden_frame.data[2],
  2084. s->current_frame.linesize[2] * s->height / 2);
  2085. } else {
  2086. #endif
  2087. if (unpack_superblocks(s, &gb) ||
  2088. unpack_modes(s, &gb) ||
  2089. unpack_vectors(s, &gb) ||
  2090. unpack_dct_coeffs(s, &gb)) {
  2091. printf(" vp3: could not decode frame\n");
  2092. return -1;
  2093. }
  2094. reverse_dc_prediction(s, 0, s->fragment_width, s->fragment_height);
  2095. reverse_dc_prediction(s, s->u_fragment_start,
  2096. s->fragment_width / 2, s->fragment_height / 2);
  2097. reverse_dc_prediction(s, s->v_fragment_start,
  2098. s->fragment_width / 2, s->fragment_height / 2);
  2099. render_fragments(s, 0, s->width, s->height, 0);
  2100. render_fragments(s, s->u_fragment_start, s->width / 2, s->height / 2, 1);
  2101. render_fragments(s, s->v_fragment_start, s->width / 2, s->height / 2, 2);
  2102. #if KEYFRAMES_ONLY
  2103. }
  2104. #endif
  2105. *data_size=sizeof(AVFrame);
  2106. *(AVFrame*)data= s->current_frame;
  2107. /* release the last frame, if it is allocated and if it is not the
  2108. * golden frame */
  2109. if ((s->last_frame.data[0]) &&
  2110. (s->last_frame.data[0] != s->golden_frame.data[0]))
  2111. avctx->release_buffer(avctx, &s->last_frame);
  2112. /* shuffle frames (last = current) */
  2113. memcpy(&s->last_frame, &s->current_frame, sizeof(AVFrame));
  2114. return buf_size;
  2115. }
  2116. /*
  2117. * This is the ffmpeg/libavcodec API module cleanup function.
  2118. */
  2119. static int vp3_decode_end(AVCodecContext *avctx)
  2120. {
  2121. Vp3DecodeContext *s = avctx->priv_data;
  2122. av_free(s->all_fragments);
  2123. av_free(s->coded_fragment_list);
  2124. av_free(s->superblock_fragments);
  2125. av_free(s->superblock_macroblocks);
  2126. av_free(s->macroblock_fragments);
  2127. av_free(s->macroblock_coding);
  2128. /* release all frames */
  2129. if (s->golden_frame.data[0])
  2130. avctx->release_buffer(avctx, &s->golden_frame);
  2131. if (s->last_frame.data[0])
  2132. avctx->release_buffer(avctx, &s->last_frame);
  2133. /* no need to release the current_frame since it will always be pointing
  2134. * to the same frame as either the golden or last frame */
  2135. return 0;
  2136. }
  2137. AVCodec vp3_decoder = {
  2138. "vp3",
  2139. CODEC_TYPE_VIDEO,
  2140. CODEC_ID_VP3,
  2141. sizeof(Vp3DecodeContext),
  2142. vp3_decode_init,
  2143. NULL,
  2144. vp3_decode_end,
  2145. vp3_decode_frame,
  2146. 0,
  2147. NULL
  2148. };