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.

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