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.

3578 lines
121KB

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