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.

3005 lines
97KB

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