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.

3254 lines
108KB

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