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.

3089 lines
99KB

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