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.

2833 lines
92KB

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