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.

3123 lines
100KB

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