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.

2543 lines
84KB

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