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.

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