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.

3061 lines
102KB

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