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.

2326 lines
75KB

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