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.

3050 lines
101KB

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