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.

3104 lines
103KB

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