You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2834 lines
94KB

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