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.

3153 lines
105KB

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