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