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.

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