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.

2450 lines
79KB

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