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.

2864 lines
102KB

  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
  70. #else
  71. static inline void debug_vp3(const char *format, ...) { }
  72. #endif
  73. #if DEBUG_INIT
  74. #define debug_init(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
  75. #else
  76. static inline void debug_init(const char *format, ...) { }
  77. #endif
  78. #if DEBUG_DEQUANTIZERS
  79. #define debug_dequantizers(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
  80. #else
  81. static inline void debug_dequantizers(const char *format, ...) { }
  82. #endif
  83. #if DEBUG_BLOCK_CODING
  84. #define debug_block_coding(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
  85. #else
  86. static inline void debug_block_coding(const char *format, ...) { }
  87. #endif
  88. #if DEBUG_MODES
  89. #define debug_modes(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
  90. #else
  91. static inline void debug_modes(const char *format, ...) { }
  92. #endif
  93. #if DEBUG_VECTORS
  94. #define debug_vectors(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
  95. #else
  96. static inline void debug_vectors(const char *format, ...) { }
  97. #endif
  98. #if DEBUG_TOKEN
  99. #define debug_token(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
  100. #else
  101. static inline void debug_token(const char *format, ...) { }
  102. #endif
  103. #if DEBUG_VLC
  104. #define debug_vlc(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
  105. #else
  106. static inline void debug_vlc(const char *format, ...) { }
  107. #endif
  108. #if DEBUG_DC_PRED
  109. #define debug_dc_pred(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
  110. #else
  111. static inline void debug_dc_pred(const char *format, ...) { }
  112. #endif
  113. #if DEBUG_IDCT
  114. #define debug_idct(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
  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 qis[3];
  199. int nqis;
  200. int quality_index;
  201. int last_quality_index;
  202. int superblock_count;
  203. int superblock_width;
  204. int superblock_height;
  205. int y_superblock_width;
  206. int y_superblock_height;
  207. int c_superblock_width;
  208. int c_superblock_height;
  209. int u_superblock_start;
  210. int v_superblock_start;
  211. unsigned char *superblock_coding;
  212. int macroblock_count;
  213. int macroblock_width;
  214. int macroblock_height;
  215. int fragment_count;
  216. int fragment_width;
  217. int fragment_height;
  218. Vp3Fragment *all_fragments;
  219. Coeff *coeffs;
  220. Coeff *next_coeff;
  221. int u_fragment_start;
  222. int v_fragment_start;
  223. ScanTable scantable;
  224. /* tables */
  225. uint16_t coded_dc_scale_factor[64];
  226. uint32_t coded_ac_scale_factor[64];
  227. uint8_t base_matrix[384][64];
  228. uint8_t qr_count[2][3];
  229. uint8_t qr_size [2][3][64];
  230. uint16_t qr_base[2][3][64];
  231. /* this is a list of indices into the all_fragments array indicating
  232. * which of the fragments are coded */
  233. int *coded_fragment_list;
  234. int coded_fragment_list_index;
  235. int pixel_addresses_inited;
  236. VLC dc_vlc[16];
  237. VLC ac_vlc_1[16];
  238. VLC ac_vlc_2[16];
  239. VLC ac_vlc_3[16];
  240. VLC ac_vlc_4[16];
  241. VLC superblock_run_length_vlc;
  242. VLC fragment_run_length_vlc;
  243. VLC mode_code_vlc;
  244. VLC motion_vector_vlc;
  245. /* these arrays need to be on 16-byte boundaries since SSE2 operations
  246. * index into them */
  247. DECLARE_ALIGNED_16(int16_t, qmat[2][4][64]); //<qmat[is_inter][plane]
  248. /* This table contains superblock_count * 16 entries. Each set of 16
  249. * numbers corresponds to the fragment indices 0..15 of the superblock.
  250. * An entry will be -1 to indicate that no entry corresponds to that
  251. * index. */
  252. int *superblock_fragments;
  253. /* This table contains superblock_count * 4 entries. Each set of 4
  254. * numbers corresponds to the macroblock indices 0..3 of the superblock.
  255. * An entry will be -1 to indicate that no entry corresponds to that
  256. * index. */
  257. int *superblock_macroblocks;
  258. /* This table contains macroblock_count * 6 entries. Each set of 6
  259. * numbers corresponds to the fragment indices 0..5 which comprise
  260. * the macroblock (4 Y fragments and 2 C fragments). */
  261. int *macroblock_fragments;
  262. /* This is an array that indicates how a particular macroblock
  263. * is coded. */
  264. unsigned char *macroblock_coding;
  265. int first_coded_y_fragment;
  266. int first_coded_c_fragment;
  267. int last_coded_y_fragment;
  268. int last_coded_c_fragment;
  269. uint8_t edge_emu_buffer[9*2048]; //FIXME dynamic alloc
  270. uint8_t qscale_table[2048]; //FIXME dynamic alloc (width+15)/16
  271. /* Huffman decode */
  272. int hti;
  273. unsigned int hbits;
  274. int entries;
  275. int huff_code_size;
  276. uint16_t huffman_table[80][32][2];
  277. uint32_t filter_limit_values[64];
  278. int bounding_values_array[256];
  279. } Vp3DecodeContext;
  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 wipes out all of the fragment data.
  526. */
  527. static void init_frame(Vp3DecodeContext *s, GetBitContext *gb)
  528. {
  529. int i;
  530. /* zero out all of the fragment information */
  531. s->coded_fragment_list_index = 0;
  532. for (i = 0; i < s->fragment_count; i++) {
  533. s->all_fragments[i].coeff_count = 0;
  534. s->all_fragments[i].motion_x = 127;
  535. s->all_fragments[i].motion_y = 127;
  536. s->all_fragments[i].next_coeff= NULL;
  537. s->coeffs[i].index=
  538. s->coeffs[i].coeff=0;
  539. s->coeffs[i].next= NULL;
  540. }
  541. }
  542. /*
  543. * This function sets up the dequantization tables used for a particular
  544. * frame.
  545. */
  546. static void init_dequantizer(Vp3DecodeContext *s)
  547. {
  548. int ac_scale_factor = s->coded_ac_scale_factor[s->quality_index];
  549. int dc_scale_factor = s->coded_dc_scale_factor[s->quality_index];
  550. int i, j, plane, inter, qri, bmi, bmj, qistart;
  551. debug_vp3(" vp3: initializing dequantization tables\n");
  552. for(inter=0; inter<2; inter++){
  553. for(plane=0; plane<3; plane++){
  554. int sum=0;
  555. for(qri=0; qri<s->qr_count[inter][plane]; qri++){
  556. sum+= s->qr_size[inter][plane][qri];
  557. if(s->quality_index <= sum)
  558. break;
  559. }
  560. qistart= sum - s->qr_size[inter][plane][qri];
  561. bmi= s->qr_base[inter][plane][qri ];
  562. bmj= s->qr_base[inter][plane][qri+1];
  563. for(i=0; i<64; i++){
  564. int coeff= ( 2*(sum -s->quality_index)*s->base_matrix[bmi][i]
  565. - 2*(qistart-s->quality_index)*s->base_matrix[bmj][i]
  566. + s->qr_size[inter][plane][qri])
  567. / (2*s->qr_size[inter][plane][qri]);
  568. int qmin= 8<<(inter + !i);
  569. int qscale= i ? ac_scale_factor : dc_scale_factor;
  570. s->qmat[inter][plane][i]= clip((qscale * coeff)/100 * 4, qmin, 4096);
  571. }
  572. }
  573. }
  574. memset(s->qscale_table, (FFMAX(s->qmat[0][0][1], s->qmat[0][1][1])+8)/16, 512); //FIXME finetune
  575. }
  576. /*
  577. * This function initializes the loop filter boundary limits if the frame's
  578. * quality index is different from the previous frame's.
  579. */
  580. static void init_loop_filter(Vp3DecodeContext *s)
  581. {
  582. int *bounding_values= s->bounding_values_array+127;
  583. int filter_limit;
  584. int x;
  585. filter_limit = s->filter_limit_values[s->quality_index];
  586. /* set up the bounding values */
  587. memset(s->bounding_values_array, 0, 256 * sizeof(int));
  588. for (x = 0; x < filter_limit; x++) {
  589. bounding_values[-x - filter_limit] = -filter_limit + x;
  590. bounding_values[-x] = -x;
  591. bounding_values[x] = x;
  592. bounding_values[x + filter_limit] = filter_limit - x;
  593. }
  594. }
  595. /*
  596. * This function unpacks all of the superblock/macroblock/fragment coding
  597. * information from the bitstream.
  598. */
  599. static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
  600. {
  601. int bit = 0;
  602. int current_superblock = 0;
  603. int current_run = 0;
  604. int decode_fully_flags = 0;
  605. int decode_partial_blocks = 0;
  606. int first_c_fragment_seen;
  607. int i, j;
  608. int current_fragment;
  609. debug_vp3(" vp3: unpacking superblock coding\n");
  610. if (s->keyframe) {
  611. debug_vp3(" keyframe-- all superblocks are fully coded\n");
  612. memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);
  613. } else {
  614. /* unpack the list of partially-coded superblocks */
  615. bit = get_bits(gb, 1);
  616. /* toggle the bit because as soon as the first run length is
  617. * fetched the bit will be toggled again */
  618. bit ^= 1;
  619. while (current_superblock < s->superblock_count) {
  620. if (current_run-- == 0) {
  621. bit ^= 1;
  622. current_run = get_vlc2(gb,
  623. s->superblock_run_length_vlc.table, 6, 2);
  624. if (current_run == 33)
  625. current_run += get_bits(gb, 12);
  626. debug_block_coding(" setting superblocks %d..%d to %s\n",
  627. current_superblock,
  628. current_superblock + current_run - 1,
  629. (bit) ? "partially coded" : "not coded");
  630. /* if any of the superblocks are not partially coded, flag
  631. * a boolean to decode the list of fully-coded superblocks */
  632. if (bit == 0) {
  633. decode_fully_flags = 1;
  634. } else {
  635. /* make a note of the fact that there are partially coded
  636. * superblocks */
  637. decode_partial_blocks = 1;
  638. }
  639. }
  640. s->superblock_coding[current_superblock++] = bit;
  641. }
  642. /* unpack the list of fully coded superblocks if any of the blocks were
  643. * not marked as partially coded in the previous step */
  644. if (decode_fully_flags) {
  645. current_superblock = 0;
  646. current_run = 0;
  647. bit = get_bits(gb, 1);
  648. /* toggle the bit because as soon as the first run length is
  649. * fetched the bit will be toggled again */
  650. bit ^= 1;
  651. while (current_superblock < s->superblock_count) {
  652. /* skip any superblocks already marked as partially coded */
  653. if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {
  654. if (current_run-- == 0) {
  655. bit ^= 1;
  656. current_run = get_vlc2(gb,
  657. s->superblock_run_length_vlc.table, 6, 2);
  658. if (current_run == 33)
  659. current_run += get_bits(gb, 12);
  660. }
  661. debug_block_coding(" setting superblock %d to %s\n",
  662. current_superblock,
  663. (bit) ? "fully coded" : "not coded");
  664. s->superblock_coding[current_superblock] = 2*bit;
  665. }
  666. current_superblock++;
  667. }
  668. }
  669. /* if there were partial blocks, initialize bitstream for
  670. * unpacking fragment codings */
  671. if (decode_partial_blocks) {
  672. current_run = 0;
  673. bit = get_bits(gb, 1);
  674. /* toggle the bit because as soon as the first run length is
  675. * fetched the bit will be toggled again */
  676. bit ^= 1;
  677. }
  678. }
  679. /* figure out which fragments are coded; iterate through each
  680. * superblock (all planes) */
  681. s->coded_fragment_list_index = 0;
  682. s->next_coeff= s->coeffs + s->fragment_count;
  683. s->first_coded_y_fragment = s->first_coded_c_fragment = 0;
  684. s->last_coded_y_fragment = s->last_coded_c_fragment = -1;
  685. first_c_fragment_seen = 0;
  686. memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
  687. for (i = 0; i < s->superblock_count; i++) {
  688. /* iterate through all 16 fragments in a superblock */
  689. for (j = 0; j < 16; j++) {
  690. /* if the fragment is in bounds, check its coding status */
  691. current_fragment = s->superblock_fragments[i * 16 + j];
  692. if (current_fragment >= s->fragment_count) {
  693. av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_superblocks(): bad fragment number (%d >= %d)\n",
  694. current_fragment, s->fragment_count);
  695. return 1;
  696. }
  697. if (current_fragment != -1) {
  698. if (s->superblock_coding[i] == SB_NOT_CODED) {
  699. /* copy all the fragments from the prior frame */
  700. s->all_fragments[current_fragment].coding_method =
  701. MODE_COPY;
  702. } else if (s->superblock_coding[i] == SB_PARTIALLY_CODED) {
  703. /* fragment may or may not be coded; this is the case
  704. * that cares about the fragment coding runs */
  705. if (current_run-- == 0) {
  706. bit ^= 1;
  707. current_run = get_vlc2(gb,
  708. s->fragment_run_length_vlc.table, 5, 2);
  709. }
  710. if (bit) {
  711. /* default mode; actual mode will be decoded in
  712. * the next phase */
  713. s->all_fragments[current_fragment].coding_method =
  714. MODE_INTER_NO_MV;
  715. s->all_fragments[current_fragment].next_coeff= s->coeffs + current_fragment;
  716. s->coded_fragment_list[s->coded_fragment_list_index] =
  717. current_fragment;
  718. if ((current_fragment >= s->u_fragment_start) &&
  719. (s->last_coded_y_fragment == -1) &&
  720. (!first_c_fragment_seen)) {
  721. s->first_coded_c_fragment = s->coded_fragment_list_index;
  722. s->last_coded_y_fragment = s->first_coded_c_fragment - 1;
  723. first_c_fragment_seen = 1;
  724. }
  725. s->coded_fragment_list_index++;
  726. s->macroblock_coding[s->all_fragments[current_fragment].macroblock] = MODE_INTER_NO_MV;
  727. debug_block_coding(" superblock %d is partially coded, fragment %d is coded\n",
  728. i, current_fragment);
  729. } else {
  730. /* not coded; copy this fragment from the prior frame */
  731. s->all_fragments[current_fragment].coding_method =
  732. MODE_COPY;
  733. debug_block_coding(" superblock %d is partially coded, fragment %d is not coded\n",
  734. i, current_fragment);
  735. }
  736. } else {
  737. /* fragments are fully coded in this superblock; actual
  738. * coding will be determined in next step */
  739. s->all_fragments[current_fragment].coding_method =
  740. MODE_INTER_NO_MV;
  741. s->all_fragments[current_fragment].next_coeff= s->coeffs + current_fragment;
  742. s->coded_fragment_list[s->coded_fragment_list_index] =
  743. current_fragment;
  744. if ((current_fragment >= s->u_fragment_start) &&
  745. (s->last_coded_y_fragment == -1) &&
  746. (!first_c_fragment_seen)) {
  747. s->first_coded_c_fragment = s->coded_fragment_list_index;
  748. s->last_coded_y_fragment = s->first_coded_c_fragment - 1;
  749. first_c_fragment_seen = 1;
  750. }
  751. s->coded_fragment_list_index++;
  752. s->macroblock_coding[s->all_fragments[current_fragment].macroblock] = MODE_INTER_NO_MV;
  753. debug_block_coding(" superblock %d is fully coded, fragment %d is coded\n",
  754. i, current_fragment);
  755. }
  756. }
  757. }
  758. }
  759. if (!first_c_fragment_seen)
  760. /* only Y fragments coded in this frame */
  761. s->last_coded_y_fragment = s->coded_fragment_list_index - 1;
  762. else
  763. /* end the list of coded C fragments */
  764. s->last_coded_c_fragment = s->coded_fragment_list_index - 1;
  765. debug_block_coding(" %d total coded fragments, y: %d -> %d, c: %d -> %d\n",
  766. s->coded_fragment_list_index,
  767. s->first_coded_y_fragment,
  768. s->last_coded_y_fragment,
  769. s->first_coded_c_fragment,
  770. s->last_coded_c_fragment);
  771. return 0;
  772. }
  773. /*
  774. * This function unpacks all the coding mode data for individual macroblocks
  775. * from the bitstream.
  776. */
  777. static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
  778. {
  779. int i, j, k;
  780. int scheme;
  781. int current_macroblock;
  782. int current_fragment;
  783. int coding_mode;
  784. debug_vp3(" vp3: unpacking encoding modes\n");
  785. if (s->keyframe) {
  786. debug_vp3(" keyframe-- all blocks are coded as INTRA\n");
  787. for (i = 0; i < s->fragment_count; i++)
  788. s->all_fragments[i].coding_method = MODE_INTRA;
  789. } else {
  790. /* fetch the mode coding scheme for this frame */
  791. scheme = get_bits(gb, 3);
  792. debug_modes(" using mode alphabet %d\n", scheme);
  793. /* is it a custom coding scheme? */
  794. if (scheme == 0) {
  795. debug_modes(" custom mode alphabet ahead:\n");
  796. for (i = 0; i < 8; i++)
  797. ModeAlphabet[scheme][get_bits(gb, 3)] = i;
  798. }
  799. for (i = 0; i < 8; i++)
  800. debug_modes(" mode[%d][%d] = %d\n", scheme, i,
  801. ModeAlphabet[scheme][i]);
  802. /* iterate through all of the macroblocks that contain 1 or more
  803. * coded fragments */
  804. for (i = 0; i < s->u_superblock_start; i++) {
  805. for (j = 0; j < 4; j++) {
  806. current_macroblock = s->superblock_macroblocks[i * 4 + j];
  807. if ((current_macroblock == -1) ||
  808. (s->macroblock_coding[current_macroblock] == MODE_COPY))
  809. continue;
  810. if (current_macroblock >= s->macroblock_count) {
  811. av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_modes(): bad macroblock number (%d >= %d)\n",
  812. current_macroblock, s->macroblock_count);
  813. return 1;
  814. }
  815. /* mode 7 means get 3 bits for each coding mode */
  816. if (scheme == 7)
  817. coding_mode = get_bits(gb, 3);
  818. else
  819. coding_mode = ModeAlphabet[scheme]
  820. [get_vlc2(gb, s->mode_code_vlc.table, 3, 3)];
  821. s->macroblock_coding[current_macroblock] = coding_mode;
  822. for (k = 0; k < 6; k++) {
  823. current_fragment =
  824. s->macroblock_fragments[current_macroblock * 6 + k];
  825. if (current_fragment == -1)
  826. continue;
  827. if (current_fragment >= s->fragment_count) {
  828. av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_modes(): bad fragment number (%d >= %d)\n",
  829. current_fragment, s->fragment_count);
  830. return 1;
  831. }
  832. if (s->all_fragments[current_fragment].coding_method !=
  833. MODE_COPY)
  834. s->all_fragments[current_fragment].coding_method =
  835. coding_mode;
  836. }
  837. debug_modes(" coding method for macroblock starting @ fragment %d = %d\n",
  838. s->macroblock_fragments[current_macroblock * 6], coding_mode);
  839. }
  840. }
  841. }
  842. return 0;
  843. }
  844. /*
  845. * This function unpacks all the motion vectors for the individual
  846. * macroblocks from the bitstream.
  847. */
  848. static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
  849. {
  850. int i, j, k;
  851. int coding_mode;
  852. int motion_x[6];
  853. int motion_y[6];
  854. int last_motion_x = 0;
  855. int last_motion_y = 0;
  856. int prior_last_motion_x = 0;
  857. int prior_last_motion_y = 0;
  858. int current_macroblock;
  859. int current_fragment;
  860. debug_vp3(" vp3: unpacking motion vectors\n");
  861. if (s->keyframe) {
  862. debug_vp3(" keyframe-- there are no motion vectors\n");
  863. } else {
  864. memset(motion_x, 0, 6 * sizeof(int));
  865. memset(motion_y, 0, 6 * sizeof(int));
  866. /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme */
  867. coding_mode = get_bits(gb, 1);
  868. debug_vectors(" using %s scheme for unpacking motion vectors\n",
  869. (coding_mode == 0) ? "VLC" : "fixed-length");
  870. /* iterate through all of the macroblocks that contain 1 or more
  871. * coded fragments */
  872. for (i = 0; i < s->u_superblock_start; i++) {
  873. for (j = 0; j < 4; j++) {
  874. current_macroblock = s->superblock_macroblocks[i * 4 + j];
  875. if ((current_macroblock == -1) ||
  876. (s->macroblock_coding[current_macroblock] == MODE_COPY))
  877. continue;
  878. if (current_macroblock >= s->macroblock_count) {
  879. av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vectors(): bad macroblock number (%d >= %d)\n",
  880. current_macroblock, s->macroblock_count);
  881. return 1;
  882. }
  883. current_fragment = s->macroblock_fragments[current_macroblock * 6];
  884. if (current_fragment >= s->fragment_count) {
  885. av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vectors(): bad fragment number (%d >= %d\n",
  886. current_fragment, s->fragment_count);
  887. return 1;
  888. }
  889. switch (s->macroblock_coding[current_macroblock]) {
  890. case MODE_INTER_PLUS_MV:
  891. case MODE_GOLDEN_MV:
  892. /* all 6 fragments use the same motion vector */
  893. if (coding_mode == 0) {
  894. motion_x[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
  895. motion_y[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
  896. } else {
  897. motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)];
  898. motion_y[0] = fixed_motion_vector_table[get_bits(gb, 6)];
  899. }
  900. for (k = 1; k < 6; k++) {
  901. motion_x[k] = motion_x[0];
  902. motion_y[k] = motion_y[0];
  903. }
  904. /* vector maintenance, only on MODE_INTER_PLUS_MV */
  905. if (s->macroblock_coding[current_macroblock] ==
  906. MODE_INTER_PLUS_MV) {
  907. prior_last_motion_x = last_motion_x;
  908. prior_last_motion_y = last_motion_y;
  909. last_motion_x = motion_x[0];
  910. last_motion_y = motion_y[0];
  911. }
  912. break;
  913. case MODE_INTER_FOURMV:
  914. /* fetch 4 vectors from the bitstream, one for each
  915. * Y fragment, then average for the C fragment vectors */
  916. motion_x[4] = motion_y[4] = 0;
  917. for (k = 0; k < 4; k++) {
  918. if (coding_mode == 0) {
  919. motion_x[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
  920. motion_y[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
  921. } else {
  922. motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)];
  923. motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)];
  924. }
  925. motion_x[4] += motion_x[k];
  926. motion_y[4] += motion_y[k];
  927. }
  928. motion_x[5]=
  929. motion_x[4]= RSHIFT(motion_x[4], 2);
  930. motion_y[5]=
  931. motion_y[4]= RSHIFT(motion_y[4], 2);
  932. /* vector maintenance; vector[3] is treated as the
  933. * last vector in this case */
  934. prior_last_motion_x = last_motion_x;
  935. prior_last_motion_y = last_motion_y;
  936. last_motion_x = motion_x[3];
  937. last_motion_y = motion_y[3];
  938. break;
  939. case MODE_INTER_LAST_MV:
  940. /* all 6 fragments use the last motion vector */
  941. motion_x[0] = last_motion_x;
  942. motion_y[0] = last_motion_y;
  943. for (k = 1; k < 6; k++) {
  944. motion_x[k] = motion_x[0];
  945. motion_y[k] = motion_y[0];
  946. }
  947. /* no vector maintenance (last vector remains the
  948. * last vector) */
  949. break;
  950. case MODE_INTER_PRIOR_LAST:
  951. /* all 6 fragments use the motion vector prior to the
  952. * last motion vector */
  953. motion_x[0] = prior_last_motion_x;
  954. motion_y[0] = prior_last_motion_y;
  955. for (k = 1; k < 6; k++) {
  956. motion_x[k] = motion_x[0];
  957. motion_y[k] = motion_y[0];
  958. }
  959. /* vector maintenance */
  960. prior_last_motion_x = last_motion_x;
  961. prior_last_motion_y = last_motion_y;
  962. last_motion_x = motion_x[0];
  963. last_motion_y = motion_y[0];
  964. break;
  965. default:
  966. /* covers intra, inter without MV, golden without MV */
  967. memset(motion_x, 0, 6 * sizeof(int));
  968. memset(motion_y, 0, 6 * sizeof(int));
  969. /* no vector maintenance */
  970. break;
  971. }
  972. /* assign the motion vectors to the correct fragments */
  973. debug_vectors(" vectors for macroblock starting @ fragment %d (coding method %d):\n",
  974. current_fragment,
  975. s->macroblock_coding[current_macroblock]);
  976. for (k = 0; k < 6; k++) {
  977. current_fragment =
  978. s->macroblock_fragments[current_macroblock * 6 + k];
  979. if (current_fragment == -1)
  980. continue;
  981. if (current_fragment >= s->fragment_count) {
  982. av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vectors(): bad fragment number (%d >= %d)\n",
  983. current_fragment, s->fragment_count);
  984. return 1;
  985. }
  986. s->all_fragments[current_fragment].motion_x = motion_x[k];
  987. s->all_fragments[current_fragment].motion_y = motion_y[k];
  988. debug_vectors(" vector %d: fragment %d = (%d, %d)\n",
  989. k, current_fragment, motion_x[k], motion_y[k]);
  990. }
  991. }
  992. }
  993. }
  994. return 0;
  995. }
  996. /*
  997. * This function is called by unpack_dct_coeffs() to extract the VLCs from
  998. * the bitstream. The VLCs encode tokens which are used to unpack DCT
  999. * data. This function unpacks all the VLCs for either the Y plane or both
  1000. * C planes, and is called for DC coefficients or different AC coefficient
  1001. * levels (since different coefficient types require different VLC tables.
  1002. *
  1003. * This function returns a residual eob run. E.g, if a particular token gave
  1004. * instructions to EOB the next 5 fragments and there were only 2 fragments
  1005. * left in the current fragment range, 3 would be returned so that it could
  1006. * be passed into the next call to this same function.
  1007. */
  1008. static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
  1009. VLC *table, int coeff_index,
  1010. int first_fragment, int last_fragment,
  1011. int eob_run)
  1012. {
  1013. int i;
  1014. int token;
  1015. int zero_run = 0;
  1016. DCTELEM coeff = 0;
  1017. Vp3Fragment *fragment;
  1018. uint8_t *perm= s->scantable.permutated;
  1019. int bits_to_get;
  1020. if ((first_fragment >= s->fragment_count) ||
  1021. (last_fragment >= s->fragment_count)) {
  1022. av_log(s->avctx, AV_LOG_ERROR, " vp3:unpack_vlcs(): bad fragment number (%d -> %d ?)\n",
  1023. first_fragment, last_fragment);
  1024. return 0;
  1025. }
  1026. for (i = first_fragment; i <= last_fragment; i++) {
  1027. fragment = &s->all_fragments[s->coded_fragment_list[i]];
  1028. if (fragment->coeff_count > coeff_index)
  1029. continue;
  1030. if (!eob_run) {
  1031. /* decode a VLC into a token */
  1032. token = get_vlc2(gb, table->table, 5, 3);
  1033. debug_vlc(" token = %2d, ", token);
  1034. /* use the token to get a zero run, a coefficient, and an eob run */
  1035. if (token <= 6) {
  1036. eob_run = eob_run_base[token];
  1037. if (eob_run_get_bits[token])
  1038. eob_run += get_bits(gb, eob_run_get_bits[token]);
  1039. coeff = zero_run = 0;
  1040. } else {
  1041. bits_to_get = coeff_get_bits[token];
  1042. if (!bits_to_get)
  1043. coeff = coeff_tables[token][0];
  1044. else
  1045. coeff = coeff_tables[token][get_bits(gb, bits_to_get)];
  1046. zero_run = zero_run_base[token];
  1047. if (zero_run_get_bits[token])
  1048. zero_run += get_bits(gb, zero_run_get_bits[token]);
  1049. }
  1050. }
  1051. if (!eob_run) {
  1052. fragment->coeff_count += zero_run;
  1053. if (fragment->coeff_count < 64){
  1054. fragment->next_coeff->coeff= coeff;
  1055. fragment->next_coeff->index= perm[fragment->coeff_count++]; //FIXME perm here already?
  1056. fragment->next_coeff->next= s->next_coeff;
  1057. s->next_coeff->next=NULL;
  1058. fragment->next_coeff= s->next_coeff++;
  1059. }
  1060. debug_vlc(" fragment %d coeff = %d\n",
  1061. s->coded_fragment_list[i], fragment->next_coeff[coeff_index]);
  1062. } else {
  1063. fragment->coeff_count |= 128;
  1064. debug_vlc(" fragment %d eob with %d coefficients\n",
  1065. s->coded_fragment_list[i], fragment->coeff_count&127);
  1066. eob_run--;
  1067. }
  1068. }
  1069. return eob_run;
  1070. }
  1071. /*
  1072. * This function unpacks all of the DCT coefficient data from the
  1073. * bitstream.
  1074. */
  1075. static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
  1076. {
  1077. int i;
  1078. int dc_y_table;
  1079. int dc_c_table;
  1080. int ac_y_table;
  1081. int ac_c_table;
  1082. int residual_eob_run = 0;
  1083. /* fetch the DC table indices */
  1084. dc_y_table = get_bits(gb, 4);
  1085. dc_c_table = get_bits(gb, 4);
  1086. /* unpack the Y plane DC coefficients */
  1087. debug_vp3(" vp3: unpacking Y plane DC coefficients using table %d\n",
  1088. dc_y_table);
  1089. residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
  1090. s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
  1091. /* unpack the C plane DC coefficients */
  1092. debug_vp3(" vp3: unpacking C plane DC coefficients using table %d\n",
  1093. dc_c_table);
  1094. residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
  1095. s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
  1096. /* fetch the AC table indices */
  1097. ac_y_table = get_bits(gb, 4);
  1098. ac_c_table = get_bits(gb, 4);
  1099. /* unpack the group 1 AC coefficients (coeffs 1-5) */
  1100. for (i = 1; i <= 5; i++) {
  1101. debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
  1102. i, ac_y_table);
  1103. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_y_table], i,
  1104. s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
  1105. debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
  1106. i, ac_c_table);
  1107. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_c_table], i,
  1108. s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
  1109. }
  1110. /* unpack the group 2 AC coefficients (coeffs 6-14) */
  1111. for (i = 6; i <= 14; i++) {
  1112. debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
  1113. i, ac_y_table);
  1114. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_y_table], i,
  1115. s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
  1116. debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
  1117. i, ac_c_table);
  1118. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_c_table], i,
  1119. s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
  1120. }
  1121. /* unpack the group 3 AC coefficients (coeffs 15-27) */
  1122. for (i = 15; i <= 27; i++) {
  1123. debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
  1124. i, ac_y_table);
  1125. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_y_table], i,
  1126. s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
  1127. debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
  1128. i, ac_c_table);
  1129. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_c_table], i,
  1130. s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
  1131. }
  1132. /* unpack the group 4 AC coefficients (coeffs 28-63) */
  1133. for (i = 28; i <= 63; i++) {
  1134. debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
  1135. i, ac_y_table);
  1136. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_y_table], i,
  1137. s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
  1138. debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
  1139. i, ac_c_table);
  1140. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_c_table], i,
  1141. s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
  1142. }
  1143. return 0;
  1144. }
  1145. /*
  1146. * This function reverses the DC prediction for each coded fragment in
  1147. * the frame. Much of this function is adapted directly from the original
  1148. * VP3 source code.
  1149. */
  1150. #define COMPATIBLE_FRAME(x) \
  1151. (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
  1152. #define FRAME_CODED(x) (s->all_fragments[x].coding_method != MODE_COPY)
  1153. #define DC_COEFF(u) (s->coeffs[u].index ? 0 : s->coeffs[u].coeff) //FIXME do somethin to simplify this
  1154. static inline int iabs (int x) { return ((x < 0) ? -x : x); }
  1155. static void reverse_dc_prediction(Vp3DecodeContext *s,
  1156. int first_fragment,
  1157. int fragment_width,
  1158. int fragment_height)
  1159. {
  1160. #define PUL 8
  1161. #define PU 4
  1162. #define PUR 2
  1163. #define PL 1
  1164. int x, y;
  1165. int i = first_fragment;
  1166. /*
  1167. * Fragment prediction groups:
  1168. *
  1169. * 32222222226
  1170. * 10000000004
  1171. * 10000000004
  1172. * 10000000004
  1173. * 10000000004
  1174. *
  1175. * Note: Groups 5 and 7 do not exist as it would mean that the
  1176. * fragment's x coordinate is both 0 and (width - 1) at the same time.
  1177. */
  1178. int predictor_group;
  1179. short predicted_dc;
  1180. /* validity flags for the left, up-left, up, and up-right fragments */
  1181. int fl, ful, fu, fur;
  1182. /* DC values for the left, up-left, up, and up-right fragments */
  1183. int vl, vul, vu, vur;
  1184. /* indices for the left, up-left, up, and up-right fragments */
  1185. int l, ul, u, ur;
  1186. /*
  1187. * The 6 fields mean:
  1188. * 0: up-left multiplier
  1189. * 1: up multiplier
  1190. * 2: up-right multiplier
  1191. * 3: left multiplier
  1192. * 4: mask
  1193. * 5: right bit shift divisor (e.g., 7 means >>=7, a.k.a. div by 128)
  1194. */
  1195. int predictor_transform[16][6] = {
  1196. { 0, 0, 0, 0, 0, 0 },
  1197. { 0, 0, 0, 1, 0, 0 }, // PL
  1198. { 0, 0, 1, 0, 0, 0 }, // PUR
  1199. { 0, 0, 53, 75, 127, 7 }, // PUR|PL
  1200. { 0, 1, 0, 0, 0, 0 }, // PU
  1201. { 0, 1, 0, 1, 1, 1 }, // PU|PL
  1202. { 0, 1, 0, 0, 0, 0 }, // PU|PUR
  1203. { 0, 0, 53, 75, 127, 7 }, // PU|PUR|PL
  1204. { 1, 0, 0, 0, 0, 0 }, // PUL
  1205. { 0, 0, 0, 1, 0, 0 }, // PUL|PL
  1206. { 1, 0, 1, 0, 1, 1 }, // PUL|PUR
  1207. { 0, 0, 53, 75, 127, 7 }, // PUL|PUR|PL
  1208. { 0, 1, 0, 0, 0, 0 }, // PUL|PU
  1209. {-26, 29, 0, 29, 31, 5 }, // PUL|PU|PL
  1210. { 3, 10, 3, 0, 15, 4 }, // PUL|PU|PUR
  1211. {-26, 29, 0, 29, 31, 5 } // PUL|PU|PUR|PL
  1212. };
  1213. /* This table shows which types of blocks can use other blocks for
  1214. * prediction. For example, INTRA is the only mode in this table to
  1215. * have a frame number of 0. That means INTRA blocks can only predict
  1216. * from other INTRA blocks. There are 2 golden frame coding types;
  1217. * blocks encoding in these modes can only predict from other blocks
  1218. * that were encoded with these 1 of these 2 modes. */
  1219. unsigned char compatible_frame[8] = {
  1220. 1, /* MODE_INTER_NO_MV */
  1221. 0, /* MODE_INTRA */
  1222. 1, /* MODE_INTER_PLUS_MV */
  1223. 1, /* MODE_INTER_LAST_MV */
  1224. 1, /* MODE_INTER_PRIOR_MV */
  1225. 2, /* MODE_USING_GOLDEN */
  1226. 2, /* MODE_GOLDEN_MV */
  1227. 1 /* MODE_INTER_FOUR_MV */
  1228. };
  1229. int current_frame_type;
  1230. /* there is a last DC predictor for each of the 3 frame types */
  1231. short last_dc[3];
  1232. int transform = 0;
  1233. debug_vp3(" vp3: reversing DC prediction\n");
  1234. vul = vu = vur = vl = 0;
  1235. last_dc[0] = last_dc[1] = last_dc[2] = 0;
  1236. /* for each fragment row... */
  1237. for (y = 0; y < fragment_height; y++) {
  1238. /* for each fragment in a row... */
  1239. for (x = 0; x < fragment_width; x++, i++) {
  1240. /* reverse prediction if this block was coded */
  1241. if (s->all_fragments[i].coding_method != MODE_COPY) {
  1242. current_frame_type =
  1243. compatible_frame[s->all_fragments[i].coding_method];
  1244. predictor_group = (x == 0) + ((y == 0) << 1) +
  1245. ((x + 1 == fragment_width) << 2);
  1246. debug_dc_pred(" frag %d: group %d, orig DC = %d, ",
  1247. i, predictor_group, DC_COEFF(i));
  1248. switch (predictor_group) {
  1249. case 0:
  1250. /* main body of fragments; consider all 4 possible
  1251. * fragments for prediction */
  1252. /* calculate the indices of the predicting fragments */
  1253. ul = i - fragment_width - 1;
  1254. u = i - fragment_width;
  1255. ur = i - fragment_width + 1;
  1256. l = i - 1;
  1257. /* fetch the DC values for the predicting fragments */
  1258. vul = DC_COEFF(ul);
  1259. vu = DC_COEFF(u);
  1260. vur = DC_COEFF(ur);
  1261. vl = DC_COEFF(l);
  1262. /* figure out which fragments are valid */
  1263. ful = FRAME_CODED(ul) && COMPATIBLE_FRAME(ul);
  1264. fu = FRAME_CODED(u) && COMPATIBLE_FRAME(u);
  1265. fur = FRAME_CODED(ur) && COMPATIBLE_FRAME(ur);
  1266. fl = FRAME_CODED(l) && COMPATIBLE_FRAME(l);
  1267. /* decide which predictor transform to use */
  1268. transform = (fl*PL) | (fu*PU) | (ful*PUL) | (fur*PUR);
  1269. break;
  1270. case 1:
  1271. /* left column of fragments, not including top corner;
  1272. * only consider up and up-right fragments */
  1273. /* calculate the indices of the predicting fragments */
  1274. u = i - fragment_width;
  1275. ur = i - fragment_width + 1;
  1276. /* fetch the DC values for the predicting fragments */
  1277. vu = DC_COEFF(u);
  1278. vur = DC_COEFF(ur);
  1279. /* figure out which fragments are valid */
  1280. fur = FRAME_CODED(ur) && COMPATIBLE_FRAME(ur);
  1281. fu = FRAME_CODED(u) && COMPATIBLE_FRAME(u);
  1282. /* decide which predictor transform to use */
  1283. transform = (fu*PU) | (fur*PUR);
  1284. break;
  1285. case 2:
  1286. case 6:
  1287. /* top row of fragments, not including top-left frag;
  1288. * only consider the left fragment for prediction */
  1289. /* calculate the indices of the predicting fragments */
  1290. l = i - 1;
  1291. /* fetch the DC values for the predicting fragments */
  1292. vl = DC_COEFF(l);
  1293. /* figure out which fragments are valid */
  1294. fl = FRAME_CODED(l) && COMPATIBLE_FRAME(l);
  1295. /* decide which predictor transform to use */
  1296. transform = (fl*PL);
  1297. break;
  1298. case 3:
  1299. /* top-left fragment */
  1300. /* nothing to predict from in this case */
  1301. transform = 0;
  1302. break;
  1303. case 4:
  1304. /* right column of fragments, not including top corner;
  1305. * consider up-left, up, and left fragments for
  1306. * prediction */
  1307. /* calculate the indices of the predicting fragments */
  1308. ul = i - fragment_width - 1;
  1309. u = i - fragment_width;
  1310. l = i - 1;
  1311. /* fetch the DC values for the predicting fragments */
  1312. vul = DC_COEFF(ul);
  1313. vu = DC_COEFF(u);
  1314. vl = DC_COEFF(l);
  1315. /* figure out which fragments are valid */
  1316. ful = FRAME_CODED(ul) && COMPATIBLE_FRAME(ul);
  1317. fu = FRAME_CODED(u) && COMPATIBLE_FRAME(u);
  1318. fl = FRAME_CODED(l) && COMPATIBLE_FRAME(l);
  1319. /* decide which predictor transform to use */
  1320. transform = (fl*PL) | (fu*PU) | (ful*PUL);
  1321. break;
  1322. }
  1323. debug_dc_pred("transform = %d, ", transform);
  1324. if (transform == 0) {
  1325. /* if there were no fragments to predict from, use last
  1326. * DC saved */
  1327. predicted_dc = last_dc[current_frame_type];
  1328. debug_dc_pred("from last DC (%d) = %d\n",
  1329. current_frame_type, DC_COEFF(i));
  1330. } else {
  1331. /* apply the appropriate predictor transform */
  1332. predicted_dc =
  1333. (predictor_transform[transform][0] * vul) +
  1334. (predictor_transform[transform][1] * vu) +
  1335. (predictor_transform[transform][2] * vur) +
  1336. (predictor_transform[transform][3] * vl);
  1337. /* if there is a shift value in the transform, add
  1338. * the sign bit before the shift */
  1339. if (predictor_transform[transform][5] != 0) {
  1340. predicted_dc += ((predicted_dc >> 15) &
  1341. predictor_transform[transform][4]);
  1342. predicted_dc >>= predictor_transform[transform][5];
  1343. }
  1344. /* check for outranging on the [ul u l] and
  1345. * [ul u ur l] predictors */
  1346. if ((transform == 13) || (transform == 15)) {
  1347. if (iabs(predicted_dc - vu) > 128)
  1348. predicted_dc = vu;
  1349. else if (iabs(predicted_dc - vl) > 128)
  1350. predicted_dc = vl;
  1351. else if (iabs(predicted_dc - vul) > 128)
  1352. predicted_dc = vul;
  1353. }
  1354. debug_dc_pred("from pred DC = %d\n",
  1355. DC_COEFF(i));
  1356. }
  1357. /* at long last, apply the predictor */
  1358. if(s->coeffs[i].index){
  1359. *s->next_coeff= s->coeffs[i];
  1360. s->coeffs[i].index=0;
  1361. s->coeffs[i].coeff=0;
  1362. s->coeffs[i].next= s->next_coeff++;
  1363. }
  1364. s->coeffs[i].coeff += predicted_dc;
  1365. /* save the DC */
  1366. last_dc[current_frame_type] = DC_COEFF(i);
  1367. if(DC_COEFF(i) && !(s->all_fragments[i].coeff_count&127)){
  1368. s->all_fragments[i].coeff_count= 129;
  1369. // s->all_fragments[i].next_coeff= s->next_coeff;
  1370. s->coeffs[i].next= s->next_coeff;
  1371. (s->next_coeff++)->next=NULL;
  1372. }
  1373. }
  1374. }
  1375. }
  1376. }
  1377. static void horizontal_filter(unsigned char *first_pixel, int stride,
  1378. int *bounding_values);
  1379. static void vertical_filter(unsigned char *first_pixel, int stride,
  1380. int *bounding_values);
  1381. /*
  1382. * Perform the final rendering for a particular slice of data.
  1383. * The slice number ranges from 0..(macroblock_height - 1).
  1384. */
  1385. static void render_slice(Vp3DecodeContext *s, int slice)
  1386. {
  1387. int x, y;
  1388. int m, n;
  1389. int i; /* indicates current fragment */
  1390. int16_t *dequantizer;
  1391. DECLARE_ALIGNED_16(DCTELEM, block[64]);
  1392. unsigned char *output_plane;
  1393. unsigned char *last_plane;
  1394. unsigned char *golden_plane;
  1395. int stride;
  1396. int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
  1397. int upper_motion_limit, lower_motion_limit;
  1398. int motion_halfpel_index;
  1399. uint8_t *motion_source;
  1400. int plane;
  1401. int plane_width;
  1402. int plane_height;
  1403. int slice_height;
  1404. int current_macroblock_entry = slice * s->macroblock_width * 6;
  1405. int fragment_width;
  1406. if (slice >= s->macroblock_height)
  1407. return;
  1408. for (plane = 0; plane < 3; plane++) {
  1409. /* set up plane-specific parameters */
  1410. if (plane == 0) {
  1411. output_plane = s->current_frame.data[0];
  1412. last_plane = s->last_frame.data[0];
  1413. golden_plane = s->golden_frame.data[0];
  1414. stride = s->current_frame.linesize[0];
  1415. if (!s->flipped_image) stride = -stride;
  1416. upper_motion_limit = 7 * s->current_frame.linesize[0];
  1417. lower_motion_limit = s->height * s->current_frame.linesize[0] + s->width - 8;
  1418. y = slice * FRAGMENT_PIXELS * 2;
  1419. plane_width = s->width;
  1420. plane_height = s->height;
  1421. slice_height = y + FRAGMENT_PIXELS * 2;
  1422. i = s->macroblock_fragments[current_macroblock_entry + 0];
  1423. } else if (plane == 1) {
  1424. output_plane = s->current_frame.data[1];
  1425. last_plane = s->last_frame.data[1];
  1426. golden_plane = s->golden_frame.data[1];
  1427. stride = s->current_frame.linesize[1];
  1428. if (!s->flipped_image) stride = -stride;
  1429. upper_motion_limit = 7 * s->current_frame.linesize[1];
  1430. lower_motion_limit = (s->height / 2) * s->current_frame.linesize[1] + (s->width / 2) - 8;
  1431. y = slice * FRAGMENT_PIXELS;
  1432. plane_width = s->width / 2;
  1433. plane_height = s->height / 2;
  1434. slice_height = y + FRAGMENT_PIXELS;
  1435. i = s->macroblock_fragments[current_macroblock_entry + 4];
  1436. } else {
  1437. output_plane = s->current_frame.data[2];
  1438. last_plane = s->last_frame.data[2];
  1439. golden_plane = s->golden_frame.data[2];
  1440. stride = s->current_frame.linesize[2];
  1441. if (!s->flipped_image) stride = -stride;
  1442. upper_motion_limit = 7 * s->current_frame.linesize[2];
  1443. lower_motion_limit = (s->height / 2) * s->current_frame.linesize[2] + (s->width / 2) - 8;
  1444. y = slice * FRAGMENT_PIXELS;
  1445. plane_width = s->width / 2;
  1446. plane_height = s->height / 2;
  1447. slice_height = y + FRAGMENT_PIXELS;
  1448. i = s->macroblock_fragments[current_macroblock_entry + 5];
  1449. }
  1450. fragment_width = plane_width / FRAGMENT_PIXELS;
  1451. if(ABS(stride) > 2048)
  1452. return; //various tables are fixed size
  1453. /* for each fragment row in the slice (both of them)... */
  1454. for (; y < slice_height; y += 8) {
  1455. /* for each fragment in a row... */
  1456. for (x = 0; x < plane_width; x += 8, i++) {
  1457. if ((i < 0) || (i >= s->fragment_count)) {
  1458. av_log(s->avctx, AV_LOG_ERROR, " vp3:render_slice(): bad fragment number (%d)\n", i);
  1459. return;
  1460. }
  1461. /* transform if this block was coded */
  1462. if ((s->all_fragments[i].coding_method != MODE_COPY) &&
  1463. !((s->avctx->flags & CODEC_FLAG_GRAY) && plane)) {
  1464. if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
  1465. (s->all_fragments[i].coding_method == MODE_GOLDEN_MV))
  1466. motion_source= golden_plane;
  1467. else
  1468. motion_source= last_plane;
  1469. motion_source += s->all_fragments[i].first_pixel;
  1470. motion_halfpel_index = 0;
  1471. /* sort out the motion vector if this fragment is coded
  1472. * using a motion vector method */
  1473. if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
  1474. (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) {
  1475. int src_x, src_y;
  1476. motion_x = s->all_fragments[i].motion_x;
  1477. motion_y = s->all_fragments[i].motion_y;
  1478. if(plane){
  1479. motion_x= (motion_x>>1) | (motion_x&1);
  1480. motion_y= (motion_y>>1) | (motion_y&1);
  1481. }
  1482. src_x= (motion_x>>1) + x;
  1483. src_y= (motion_y>>1) + y;
  1484. if ((motion_x == 127) || (motion_y == 127))
  1485. av_log(s->avctx, AV_LOG_ERROR, " help! got invalid motion vector! (%X, %X)\n", motion_x, motion_y);
  1486. motion_halfpel_index = motion_x & 0x01;
  1487. motion_source += (motion_x >> 1);
  1488. motion_halfpel_index |= (motion_y & 0x01) << 1;
  1489. motion_source += ((motion_y >> 1) * stride);
  1490. if(src_x<0 || src_y<0 || src_x + 9 >= plane_width || src_y + 9 >= plane_height){
  1491. uint8_t *temp= s->edge_emu_buffer;
  1492. if(stride<0) temp -= 9*stride;
  1493. else temp += 9*stride;
  1494. ff_emulated_edge_mc(temp, motion_source, stride, 9, 9, src_x, src_y, plane_width, plane_height);
  1495. motion_source= temp;
  1496. }
  1497. }
  1498. /* first, take care of copying a block from either the
  1499. * previous or the golden frame */
  1500. if (s->all_fragments[i].coding_method != MODE_INTRA) {
  1501. /* Note, it is possible to implement all MC cases with
  1502. put_no_rnd_pixels_l2 which would look more like the
  1503. VP3 source but this would be slower as
  1504. put_no_rnd_pixels_tab is better optimzed */
  1505. if(motion_halfpel_index != 3){
  1506. s->dsp.put_no_rnd_pixels_tab[1][motion_halfpel_index](
  1507. output_plane + s->all_fragments[i].first_pixel,
  1508. motion_source, stride, 8);
  1509. }else{
  1510. int d= (motion_x ^ motion_y)>>31; // d is 0 if motion_x and _y have the same sign, else -1
  1511. s->dsp.put_no_rnd_pixels_l2[1](
  1512. output_plane + s->all_fragments[i].first_pixel,
  1513. motion_source - d,
  1514. motion_source + stride + 1 + d,
  1515. stride, 8);
  1516. }
  1517. dequantizer = s->qmat[1][plane];
  1518. }else{
  1519. dequantizer = s->qmat[0][plane];
  1520. }
  1521. /* dequantize the DCT coefficients */
  1522. debug_idct("fragment %d, coding mode %d, DC = %d, dequant = %d:\n",
  1523. i, s->all_fragments[i].coding_method,
  1524. DC_COEFF(i), dequantizer[0]);
  1525. if(s->avctx->idct_algo==FF_IDCT_VP3){
  1526. Coeff *coeff= s->coeffs + i;
  1527. memset(block, 0, sizeof(block));
  1528. while(coeff->next){
  1529. block[coeff->index]= coeff->coeff * dequantizer[coeff->index];
  1530. coeff= coeff->next;
  1531. }
  1532. }else{
  1533. Coeff *coeff= s->coeffs + i;
  1534. memset(block, 0, sizeof(block));
  1535. while(coeff->next){
  1536. block[coeff->index]= (coeff->coeff * dequantizer[coeff->index] + 2)>>2;
  1537. coeff= coeff->next;
  1538. }
  1539. }
  1540. /* invert DCT and place (or add) in final output */
  1541. if (s->all_fragments[i].coding_method == MODE_INTRA) {
  1542. if(s->avctx->idct_algo!=FF_IDCT_VP3)
  1543. block[0] += 128<<3;
  1544. s->dsp.idct_put(
  1545. output_plane + s->all_fragments[i].first_pixel,
  1546. stride,
  1547. block);
  1548. } else {
  1549. s->dsp.idct_add(
  1550. output_plane + s->all_fragments[i].first_pixel,
  1551. stride,
  1552. block);
  1553. }
  1554. debug_idct("block after idct_%s():\n",
  1555. (s->all_fragments[i].coding_method == MODE_INTRA)?
  1556. "put" : "add");
  1557. for (m = 0; m < 8; m++) {
  1558. for (n = 0; n < 8; n++) {
  1559. debug_idct(" %3d", *(output_plane +
  1560. s->all_fragments[i].first_pixel + (m * stride + n)));
  1561. }
  1562. debug_idct("\n");
  1563. }
  1564. debug_idct("\n");
  1565. } else {
  1566. /* copy directly from the previous frame */
  1567. s->dsp.put_pixels_tab[1][0](
  1568. output_plane + s->all_fragments[i].first_pixel,
  1569. last_plane + s->all_fragments[i].first_pixel,
  1570. stride, 8);
  1571. }
  1572. #if 0
  1573. /* perform the left edge filter if:
  1574. * - the fragment is not on the left column
  1575. * - the fragment is coded in this frame
  1576. * - the fragment is not coded in this frame but the left
  1577. * fragment is coded in this frame (this is done instead
  1578. * of a right edge filter when rendering the left fragment
  1579. * since this fragment is not available yet) */
  1580. if ((x > 0) &&
  1581. ((s->all_fragments[i].coding_method != MODE_COPY) ||
  1582. ((s->all_fragments[i].coding_method == MODE_COPY) &&
  1583. (s->all_fragments[i - 1].coding_method != MODE_COPY)) )) {
  1584. horizontal_filter(
  1585. output_plane + s->all_fragments[i].first_pixel + 7*stride,
  1586. -stride, bounding_values);
  1587. }
  1588. /* perform the top edge filter if:
  1589. * - the fragment is not on the top row
  1590. * - the fragment is coded in this frame
  1591. * - the fragment is not coded in this frame but the above
  1592. * fragment is coded in this frame (this is done instead
  1593. * of a bottom edge filter when rendering the above
  1594. * fragment since this fragment is not available yet) */
  1595. if ((y > 0) &&
  1596. ((s->all_fragments[i].coding_method != MODE_COPY) ||
  1597. ((s->all_fragments[i].coding_method == MODE_COPY) &&
  1598. (s->all_fragments[i - fragment_width].coding_method != MODE_COPY)) )) {
  1599. vertical_filter(
  1600. output_plane + s->all_fragments[i].first_pixel - stride,
  1601. -stride, bounding_values);
  1602. }
  1603. #endif
  1604. }
  1605. }
  1606. }
  1607. /* this looks like a good place for slice dispatch... */
  1608. /* algorithm:
  1609. * if (slice == s->macroblock_height - 1)
  1610. * dispatch (both last slice & 2nd-to-last slice);
  1611. * else if (slice > 0)
  1612. * dispatch (slice - 1);
  1613. */
  1614. emms_c();
  1615. }
  1616. static void horizontal_filter(unsigned char *first_pixel, int stride,
  1617. int *bounding_values)
  1618. {
  1619. unsigned char *end;
  1620. int filter_value;
  1621. for (end= first_pixel + 8*stride; first_pixel < end; first_pixel += stride) {
  1622. filter_value =
  1623. (first_pixel[-2] - first_pixel[ 1])
  1624. +3*(first_pixel[ 0] - first_pixel[-1]);
  1625. filter_value = bounding_values[(filter_value + 4) >> 3];
  1626. first_pixel[-1] = clip_uint8(first_pixel[-1] + filter_value);
  1627. first_pixel[ 0] = clip_uint8(first_pixel[ 0] - filter_value);
  1628. }
  1629. }
  1630. static void vertical_filter(unsigned char *first_pixel, int stride,
  1631. int *bounding_values)
  1632. {
  1633. unsigned char *end;
  1634. int filter_value;
  1635. const int nstride= -stride;
  1636. for (end= first_pixel + 8; first_pixel < end; first_pixel++) {
  1637. filter_value =
  1638. (first_pixel[2 * nstride] - first_pixel[ stride])
  1639. +3*(first_pixel[0 ] - first_pixel[nstride]);
  1640. filter_value = bounding_values[(filter_value + 4) >> 3];
  1641. first_pixel[nstride] = clip_uint8(first_pixel[nstride] + filter_value);
  1642. first_pixel[0] = clip_uint8(first_pixel[0] - filter_value);
  1643. }
  1644. }
  1645. static void apply_loop_filter(Vp3DecodeContext *s)
  1646. {
  1647. int x, y, plane;
  1648. int width, height;
  1649. int fragment;
  1650. int stride;
  1651. unsigned char *plane_data;
  1652. int *bounding_values= s->bounding_values_array+127;
  1653. #if 0
  1654. int bounding_values_array[256];
  1655. int filter_limit;
  1656. /* find the right loop limit value */
  1657. for (x = 63; x >= 0; x--) {
  1658. if (vp31_ac_scale_factor[x] >= s->quality_index)
  1659. break;
  1660. }
  1661. filter_limit = vp31_filter_limit_values[s->quality_index];
  1662. /* set up the bounding values */
  1663. memset(bounding_values_array, 0, 256 * sizeof(int));
  1664. for (x = 0; x < filter_limit; x++) {
  1665. bounding_values[-x - filter_limit] = -filter_limit + x;
  1666. bounding_values[-x] = -x;
  1667. bounding_values[x] = x;
  1668. bounding_values[x + filter_limit] = filter_limit - x;
  1669. }
  1670. #endif
  1671. for (plane = 0; plane < 3; plane++) {
  1672. if (plane == 0) {
  1673. /* Y plane parameters */
  1674. fragment = 0;
  1675. width = s->fragment_width;
  1676. height = s->fragment_height;
  1677. stride = s->current_frame.linesize[0];
  1678. plane_data = s->current_frame.data[0];
  1679. } else if (plane == 1) {
  1680. /* U plane parameters */
  1681. fragment = s->u_fragment_start;
  1682. width = s->fragment_width / 2;
  1683. height = s->fragment_height / 2;
  1684. stride = s->current_frame.linesize[1];
  1685. plane_data = s->current_frame.data[1];
  1686. } else {
  1687. /* V plane parameters */
  1688. fragment = s->v_fragment_start;
  1689. width = s->fragment_width / 2;
  1690. height = s->fragment_height / 2;
  1691. stride = s->current_frame.linesize[2];
  1692. plane_data = s->current_frame.data[2];
  1693. }
  1694. for (y = 0; y < height; y++) {
  1695. for (x = 0; x < width; x++) {
  1696. START_TIMER
  1697. /* do not perform left edge filter for left columns frags */
  1698. if ((x > 0) &&
  1699. (s->all_fragments[fragment].coding_method != MODE_COPY)) {
  1700. horizontal_filter(
  1701. plane_data + s->all_fragments[fragment].first_pixel - 7*stride,
  1702. stride, bounding_values);
  1703. }
  1704. /* do not perform top edge filter for top row fragments */
  1705. if ((y > 0) &&
  1706. (s->all_fragments[fragment].coding_method != MODE_COPY)) {
  1707. vertical_filter(
  1708. plane_data + s->all_fragments[fragment].first_pixel + stride,
  1709. stride, bounding_values);
  1710. }
  1711. /* do not perform right edge filter for right column
  1712. * fragments or if right fragment neighbor is also coded
  1713. * in this frame (it will be filtered in next iteration) */
  1714. if ((x < width - 1) &&
  1715. (s->all_fragments[fragment].coding_method != MODE_COPY) &&
  1716. (s->all_fragments[fragment + 1].coding_method == MODE_COPY)) {
  1717. horizontal_filter(
  1718. plane_data + s->all_fragments[fragment + 1].first_pixel - 7*stride,
  1719. stride, bounding_values);
  1720. }
  1721. /* do not perform bottom edge filter for bottom row
  1722. * fragments or if bottom fragment neighbor is also coded
  1723. * in this frame (it will be filtered in the next row) */
  1724. if ((y < height - 1) &&
  1725. (s->all_fragments[fragment].coding_method != MODE_COPY) &&
  1726. (s->all_fragments[fragment + width].coding_method == MODE_COPY)) {
  1727. vertical_filter(
  1728. plane_data + s->all_fragments[fragment + width].first_pixel + stride,
  1729. stride, bounding_values);
  1730. }
  1731. fragment++;
  1732. STOP_TIMER("loop filter")
  1733. }
  1734. }
  1735. }
  1736. }
  1737. /*
  1738. * This function computes the first pixel addresses for each fragment.
  1739. * This function needs to be invoked after the first frame is allocated
  1740. * so that it has access to the plane strides.
  1741. */
  1742. static void vp3_calculate_pixel_addresses(Vp3DecodeContext *s)
  1743. {
  1744. int i, x, y;
  1745. /* figure out the first pixel addresses for each of the fragments */
  1746. /* Y plane */
  1747. i = 0;
  1748. for (y = s->fragment_height; y > 0; y--) {
  1749. for (x = 0; x < s->fragment_width; x++) {
  1750. s->all_fragments[i++].first_pixel =
  1751. s->golden_frame.linesize[0] * y * FRAGMENT_PIXELS -
  1752. s->golden_frame.linesize[0] +
  1753. x * FRAGMENT_PIXELS;
  1754. debug_init(" fragment %d, first pixel @ %d\n",
  1755. i-1, s->all_fragments[i-1].first_pixel);
  1756. }
  1757. }
  1758. /* U plane */
  1759. i = s->u_fragment_start;
  1760. for (y = s->fragment_height / 2; y > 0; y--) {
  1761. for (x = 0; x < s->fragment_width / 2; x++) {
  1762. s->all_fragments[i++].first_pixel =
  1763. s->golden_frame.linesize[1] * y * FRAGMENT_PIXELS -
  1764. s->golden_frame.linesize[1] +
  1765. x * FRAGMENT_PIXELS;
  1766. debug_init(" fragment %d, first pixel @ %d\n",
  1767. i-1, s->all_fragments[i-1].first_pixel);
  1768. }
  1769. }
  1770. /* V plane */
  1771. i = s->v_fragment_start;
  1772. for (y = s->fragment_height / 2; y > 0; y--) {
  1773. for (x = 0; x < s->fragment_width / 2; x++) {
  1774. s->all_fragments[i++].first_pixel =
  1775. s->golden_frame.linesize[2] * y * FRAGMENT_PIXELS -
  1776. s->golden_frame.linesize[2] +
  1777. x * FRAGMENT_PIXELS;
  1778. debug_init(" fragment %d, first pixel @ %d\n",
  1779. i-1, s->all_fragments[i-1].first_pixel);
  1780. }
  1781. }
  1782. }
  1783. /* FIXME: this should be merged with the above! */
  1784. static void theora_calculate_pixel_addresses(Vp3DecodeContext *s)
  1785. {
  1786. int i, x, y;
  1787. /* figure out the first pixel addresses for each of the fragments */
  1788. /* Y plane */
  1789. i = 0;
  1790. for (y = 1; y <= s->fragment_height; y++) {
  1791. for (x = 0; x < s->fragment_width; x++) {
  1792. s->all_fragments[i++].first_pixel =
  1793. s->golden_frame.linesize[0] * y * FRAGMENT_PIXELS -
  1794. s->golden_frame.linesize[0] +
  1795. x * FRAGMENT_PIXELS;
  1796. debug_init(" fragment %d, first pixel @ %d\n",
  1797. i-1, s->all_fragments[i-1].first_pixel);
  1798. }
  1799. }
  1800. /* U plane */
  1801. i = s->u_fragment_start;
  1802. for (y = 1; y <= s->fragment_height / 2; y++) {
  1803. for (x = 0; x < s->fragment_width / 2; x++) {
  1804. s->all_fragments[i++].first_pixel =
  1805. s->golden_frame.linesize[1] * y * FRAGMENT_PIXELS -
  1806. s->golden_frame.linesize[1] +
  1807. x * FRAGMENT_PIXELS;
  1808. debug_init(" fragment %d, first pixel @ %d\n",
  1809. i-1, s->all_fragments[i-1].first_pixel);
  1810. }
  1811. }
  1812. /* V plane */
  1813. i = s->v_fragment_start;
  1814. for (y = 1; y <= s->fragment_height / 2; y++) {
  1815. for (x = 0; x < s->fragment_width / 2; x++) {
  1816. s->all_fragments[i++].first_pixel =
  1817. s->golden_frame.linesize[2] * y * FRAGMENT_PIXELS -
  1818. s->golden_frame.linesize[2] +
  1819. x * FRAGMENT_PIXELS;
  1820. debug_init(" fragment %d, first pixel @ %d\n",
  1821. i-1, s->all_fragments[i-1].first_pixel);
  1822. }
  1823. }
  1824. }
  1825. /*
  1826. * This is the ffmpeg/libavcodec API init function.
  1827. */
  1828. static int vp3_decode_init(AVCodecContext *avctx)
  1829. {
  1830. Vp3DecodeContext *s = avctx->priv_data;
  1831. int i, inter, plane;
  1832. int c_width;
  1833. int c_height;
  1834. int y_superblock_count;
  1835. int c_superblock_count;
  1836. if (avctx->codec_tag == MKTAG('V','P','3','0'))
  1837. s->version = 0;
  1838. else
  1839. s->version = 1;
  1840. s->avctx = avctx;
  1841. s->width = (avctx->width + 15) & 0xFFFFFFF0;
  1842. s->height = (avctx->height + 15) & 0xFFFFFFF0;
  1843. avctx->pix_fmt = PIX_FMT_YUV420P;
  1844. avctx->has_b_frames = 0;
  1845. if(avctx->idct_algo==FF_IDCT_AUTO)
  1846. avctx->idct_algo=FF_IDCT_VP3;
  1847. dsputil_init(&s->dsp, avctx);
  1848. ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
  1849. /* initialize to an impossible value which will force a recalculation
  1850. * in the first frame decode */
  1851. s->quality_index = -1;
  1852. s->y_superblock_width = (s->width + 31) / 32;
  1853. s->y_superblock_height = (s->height + 31) / 32;
  1854. y_superblock_count = s->y_superblock_width * s->y_superblock_height;
  1855. /* work out the dimensions for the C planes */
  1856. c_width = s->width / 2;
  1857. c_height = s->height / 2;
  1858. s->c_superblock_width = (c_width + 31) / 32;
  1859. s->c_superblock_height = (c_height + 31) / 32;
  1860. c_superblock_count = s->c_superblock_width * s->c_superblock_height;
  1861. s->superblock_count = y_superblock_count + (c_superblock_count * 2);
  1862. s->u_superblock_start = y_superblock_count;
  1863. s->v_superblock_start = s->u_superblock_start + c_superblock_count;
  1864. s->superblock_coding = av_malloc(s->superblock_count);
  1865. s->macroblock_width = (s->width + 15) / 16;
  1866. s->macroblock_height = (s->height + 15) / 16;
  1867. s->macroblock_count = s->macroblock_width * s->macroblock_height;
  1868. s->fragment_width = s->width / FRAGMENT_PIXELS;
  1869. s->fragment_height = s->height / FRAGMENT_PIXELS;
  1870. /* fragment count covers all 8x8 blocks for all 3 planes */
  1871. s->fragment_count = s->fragment_width * s->fragment_height * 3 / 2;
  1872. s->u_fragment_start = s->fragment_width * s->fragment_height;
  1873. s->v_fragment_start = s->fragment_width * s->fragment_height * 5 / 4;
  1874. debug_init(" Y plane: %d x %d\n", s->width, s->height);
  1875. debug_init(" C plane: %d x %d\n", c_width, c_height);
  1876. debug_init(" Y superblocks: %d x %d, %d total\n",
  1877. s->y_superblock_width, s->y_superblock_height, y_superblock_count);
  1878. debug_init(" C superblocks: %d x %d, %d total\n",
  1879. s->c_superblock_width, s->c_superblock_height, c_superblock_count);
  1880. debug_init(" total superblocks = %d, U starts @ %d, V starts @ %d\n",
  1881. s->superblock_count, s->u_superblock_start, s->v_superblock_start);
  1882. debug_init(" macroblocks: %d x %d, %d total\n",
  1883. s->macroblock_width, s->macroblock_height, s->macroblock_count);
  1884. debug_init(" %d fragments, %d x %d, u starts @ %d, v starts @ %d\n",
  1885. s->fragment_count,
  1886. s->fragment_width,
  1887. s->fragment_height,
  1888. s->u_fragment_start,
  1889. s->v_fragment_start);
  1890. s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment));
  1891. s->coeffs = av_malloc(s->fragment_count * sizeof(Coeff) * 65);
  1892. s->coded_fragment_list = av_malloc(s->fragment_count * sizeof(int));
  1893. s->pixel_addresses_inited = 0;
  1894. if (!s->theora_tables)
  1895. {
  1896. for (i = 0; i < 64; i++)
  1897. s->coded_dc_scale_factor[i] = vp31_dc_scale_factor[i];
  1898. for (i = 0; i < 64; i++)
  1899. s->coded_ac_scale_factor[i] = vp31_ac_scale_factor[i];
  1900. for (i = 0; i < 64; i++)
  1901. s->base_matrix[0][i] = vp31_intra_y_dequant[i];
  1902. for (i = 0; i < 64; i++)
  1903. s->base_matrix[1][i] = vp31_intra_c_dequant[i];
  1904. for (i = 0; i < 64; i++)
  1905. s->base_matrix[2][i] = vp31_inter_dequant[i];
  1906. for (i = 0; i < 64; i++)
  1907. s->filter_limit_values[i] = vp31_filter_limit_values[i];
  1908. for(inter=0; inter<2; inter++){
  1909. for(plane=0; plane<3; plane++){
  1910. s->qr_count[inter][plane]= 1;
  1911. s->qr_size [inter][plane][0]= 63;
  1912. s->qr_base [inter][plane][0]=
  1913. s->qr_base [inter][plane][1]= 2*inter + (!!plane)*!inter;
  1914. }
  1915. }
  1916. /* init VLC tables */
  1917. for (i = 0; i < 16; i++) {
  1918. /* DC histograms */
  1919. init_vlc(&s->dc_vlc[i], 5, 32,
  1920. &dc_bias[i][0][1], 4, 2,
  1921. &dc_bias[i][0][0], 4, 2, 0);
  1922. /* group 1 AC histograms */
  1923. init_vlc(&s->ac_vlc_1[i], 5, 32,
  1924. &ac_bias_0[i][0][1], 4, 2,
  1925. &ac_bias_0[i][0][0], 4, 2, 0);
  1926. /* group 2 AC histograms */
  1927. init_vlc(&s->ac_vlc_2[i], 5, 32,
  1928. &ac_bias_1[i][0][1], 4, 2,
  1929. &ac_bias_1[i][0][0], 4, 2, 0);
  1930. /* group 3 AC histograms */
  1931. init_vlc(&s->ac_vlc_3[i], 5, 32,
  1932. &ac_bias_2[i][0][1], 4, 2,
  1933. &ac_bias_2[i][0][0], 4, 2, 0);
  1934. /* group 4 AC histograms */
  1935. init_vlc(&s->ac_vlc_4[i], 5, 32,
  1936. &ac_bias_3[i][0][1], 4, 2,
  1937. &ac_bias_3[i][0][0], 4, 2, 0);
  1938. }
  1939. } else {
  1940. for (i = 0; i < 16; i++) {
  1941. /* DC histograms */
  1942. init_vlc(&s->dc_vlc[i], 5, 32,
  1943. &s->huffman_table[i][0][1], 4, 2,
  1944. &s->huffman_table[i][0][0], 4, 2, 0);
  1945. /* group 1 AC histograms */
  1946. init_vlc(&s->ac_vlc_1[i], 5, 32,
  1947. &s->huffman_table[i+16][0][1], 4, 2,
  1948. &s->huffman_table[i+16][0][0], 4, 2, 0);
  1949. /* group 2 AC histograms */
  1950. init_vlc(&s->ac_vlc_2[i], 5, 32,
  1951. &s->huffman_table[i+16*2][0][1], 4, 2,
  1952. &s->huffman_table[i+16*2][0][0], 4, 2, 0);
  1953. /* group 3 AC histograms */
  1954. init_vlc(&s->ac_vlc_3[i], 5, 32,
  1955. &s->huffman_table[i+16*3][0][1], 4, 2,
  1956. &s->huffman_table[i+16*3][0][0], 4, 2, 0);
  1957. /* group 4 AC histograms */
  1958. init_vlc(&s->ac_vlc_4[i], 5, 32,
  1959. &s->huffman_table[i+16*4][0][1], 4, 2,
  1960. &s->huffman_table[i+16*4][0][0], 4, 2, 0);
  1961. }
  1962. }
  1963. init_vlc(&s->superblock_run_length_vlc, 6, 34,
  1964. &superblock_run_length_vlc_table[0][1], 4, 2,
  1965. &superblock_run_length_vlc_table[0][0], 4, 2, 0);
  1966. init_vlc(&s->fragment_run_length_vlc, 5, 30,
  1967. &fragment_run_length_vlc_table[0][1], 4, 2,
  1968. &fragment_run_length_vlc_table[0][0], 4, 2, 0);
  1969. init_vlc(&s->mode_code_vlc, 3, 8,
  1970. &mode_code_vlc_table[0][1], 2, 1,
  1971. &mode_code_vlc_table[0][0], 2, 1, 0);
  1972. init_vlc(&s->motion_vector_vlc, 6, 63,
  1973. &motion_vector_vlc_table[0][1], 2, 1,
  1974. &motion_vector_vlc_table[0][0], 2, 1, 0);
  1975. /* work out the block mapping tables */
  1976. s->superblock_fragments = av_malloc(s->superblock_count * 16 * sizeof(int));
  1977. s->superblock_macroblocks = av_malloc(s->superblock_count * 4 * sizeof(int));
  1978. s->macroblock_fragments = av_malloc(s->macroblock_count * 6 * sizeof(int));
  1979. s->macroblock_coding = av_malloc(s->macroblock_count + 1);
  1980. init_block_mapping(s);
  1981. for (i = 0; i < 3; i++) {
  1982. s->current_frame.data[i] = NULL;
  1983. s->last_frame.data[i] = NULL;
  1984. s->golden_frame.data[i] = NULL;
  1985. }
  1986. return 0;
  1987. }
  1988. /*
  1989. * This is the ffmpeg/libavcodec API frame decode function.
  1990. */
  1991. static int vp3_decode_frame(AVCodecContext *avctx,
  1992. void *data, int *data_size,
  1993. uint8_t *buf, int buf_size)
  1994. {
  1995. Vp3DecodeContext *s = avctx->priv_data;
  1996. GetBitContext gb;
  1997. static int counter = 0;
  1998. int i;
  1999. init_get_bits(&gb, buf, buf_size * 8);
  2000. if (s->theora && get_bits1(&gb))
  2001. {
  2002. #if 1
  2003. av_log(avctx, AV_LOG_ERROR, "Header packet passed to frame decoder, skipping\n");
  2004. return -1;
  2005. #else
  2006. int ptype = get_bits(&gb, 7);
  2007. skip_bits(&gb, 6*8); /* "theora" */
  2008. switch(ptype)
  2009. {
  2010. case 1:
  2011. theora_decode_comments(avctx, &gb);
  2012. break;
  2013. case 2:
  2014. theora_decode_tables(avctx, &gb);
  2015. init_dequantizer(s);
  2016. break;
  2017. default:
  2018. av_log(avctx, AV_LOG_ERROR, "Unknown Theora config packet: %d\n", ptype);
  2019. }
  2020. return buf_size;
  2021. #endif
  2022. }
  2023. s->keyframe = !get_bits1(&gb);
  2024. if (!s->theora)
  2025. skip_bits(&gb, 1);
  2026. s->last_quality_index = s->quality_index;
  2027. s->nqis=0;
  2028. do{
  2029. s->qis[s->nqis++]= get_bits(&gb, 6);
  2030. } while(s->theora >= 0x030200 && s->nqis<3 && get_bits1(&gb));
  2031. s->quality_index= s->qis[0];
  2032. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  2033. av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n",
  2034. s->keyframe?"key":"", counter, s->quality_index);
  2035. counter++;
  2036. if (s->quality_index != s->last_quality_index) {
  2037. init_dequantizer(s);
  2038. init_loop_filter(s);
  2039. }
  2040. if (s->keyframe) {
  2041. if (!s->theora)
  2042. {
  2043. skip_bits(&gb, 4); /* width code */
  2044. skip_bits(&gb, 4); /* height code */
  2045. if (s->version)
  2046. {
  2047. s->version = get_bits(&gb, 5);
  2048. if (counter == 1)
  2049. av_log(s->avctx, AV_LOG_DEBUG, "VP version: %d\n", s->version);
  2050. }
  2051. }
  2052. if (s->version || s->theora)
  2053. {
  2054. if (get_bits1(&gb))
  2055. av_log(s->avctx, AV_LOG_ERROR, "Warning, unsupported keyframe coding type?!\n");
  2056. skip_bits(&gb, 2); /* reserved? */
  2057. }
  2058. if (s->last_frame.data[0] == s->golden_frame.data[0]) {
  2059. if (s->golden_frame.data[0])
  2060. avctx->release_buffer(avctx, &s->golden_frame);
  2061. s->last_frame= s->golden_frame; /* ensure that we catch any access to this released frame */
  2062. } else {
  2063. if (s->golden_frame.data[0])
  2064. avctx->release_buffer(avctx, &s->golden_frame);
  2065. if (s->last_frame.data[0])
  2066. avctx->release_buffer(avctx, &s->last_frame);
  2067. }
  2068. s->golden_frame.reference = 3;
  2069. if(avctx->get_buffer(avctx, &s->golden_frame) < 0) {
  2070. av_log(s->avctx, AV_LOG_ERROR, "vp3: get_buffer() failed\n");
  2071. return -1;
  2072. }
  2073. /* golden frame is also the current frame */
  2074. s->current_frame= s->golden_frame;
  2075. /* time to figure out pixel addresses? */
  2076. if (!s->pixel_addresses_inited)
  2077. {
  2078. if (!s->flipped_image)
  2079. vp3_calculate_pixel_addresses(s);
  2080. else
  2081. theora_calculate_pixel_addresses(s);
  2082. }
  2083. } else {
  2084. /* allocate a new current frame */
  2085. s->current_frame.reference = 3;
  2086. if(avctx->get_buffer(avctx, &s->current_frame) < 0) {
  2087. av_log(s->avctx, AV_LOG_ERROR, "vp3: get_buffer() failed\n");
  2088. return -1;
  2089. }
  2090. }
  2091. s->current_frame.qscale_table= s->qscale_table; //FIXME allocate individual tables per AVFrame
  2092. s->current_frame.qstride= 0;
  2093. {START_TIMER
  2094. init_frame(s, &gb);
  2095. STOP_TIMER("init_frame")}
  2096. #if KEYFRAMES_ONLY
  2097. if (!s->keyframe) {
  2098. memcpy(s->current_frame.data[0], s->golden_frame.data[0],
  2099. s->current_frame.linesize[0] * s->height);
  2100. memcpy(s->current_frame.data[1], s->golden_frame.data[1],
  2101. s->current_frame.linesize[1] * s->height / 2);
  2102. memcpy(s->current_frame.data[2], s->golden_frame.data[2],
  2103. s->current_frame.linesize[2] * s->height / 2);
  2104. } else {
  2105. #endif
  2106. {START_TIMER
  2107. if (unpack_superblocks(s, &gb)){
  2108. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n");
  2109. return -1;
  2110. }
  2111. STOP_TIMER("unpack_superblocks")}
  2112. {START_TIMER
  2113. if (unpack_modes(s, &gb)){
  2114. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n");
  2115. return -1;
  2116. }
  2117. STOP_TIMER("unpack_modes")}
  2118. {START_TIMER
  2119. if (unpack_vectors(s, &gb)){
  2120. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n");
  2121. return -1;
  2122. }
  2123. STOP_TIMER("unpack_vectors")}
  2124. {START_TIMER
  2125. if (unpack_dct_coeffs(s, &gb)){
  2126. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n");
  2127. return -1;
  2128. }
  2129. STOP_TIMER("unpack_dct_coeffs")}
  2130. {START_TIMER
  2131. reverse_dc_prediction(s, 0, s->fragment_width, s->fragment_height);
  2132. if ((avctx->flags & CODEC_FLAG_GRAY) == 0) {
  2133. reverse_dc_prediction(s, s->u_fragment_start,
  2134. s->fragment_width / 2, s->fragment_height / 2);
  2135. reverse_dc_prediction(s, s->v_fragment_start,
  2136. s->fragment_width / 2, s->fragment_height / 2);
  2137. }
  2138. STOP_TIMER("reverse_dc_prediction")}
  2139. {START_TIMER
  2140. for (i = 0; i < s->macroblock_height; i++)
  2141. render_slice(s, i);
  2142. STOP_TIMER("render_fragments")}
  2143. {START_TIMER
  2144. apply_loop_filter(s);
  2145. STOP_TIMER("apply_loop_filter")}
  2146. #if KEYFRAMES_ONLY
  2147. }
  2148. #endif
  2149. *data_size=sizeof(AVFrame);
  2150. *(AVFrame*)data= s->current_frame;
  2151. /* release the last frame, if it is allocated and if it is not the
  2152. * golden frame */
  2153. if ((s->last_frame.data[0]) &&
  2154. (s->last_frame.data[0] != s->golden_frame.data[0]))
  2155. avctx->release_buffer(avctx, &s->last_frame);
  2156. /* shuffle frames (last = current) */
  2157. s->last_frame= s->current_frame;
  2158. s->current_frame.data[0]= NULL; /* ensure that we catch any access to this released frame */
  2159. return buf_size;
  2160. }
  2161. /*
  2162. * This is the ffmpeg/libavcodec API module cleanup function.
  2163. */
  2164. static int vp3_decode_end(AVCodecContext *avctx)
  2165. {
  2166. Vp3DecodeContext *s = avctx->priv_data;
  2167. av_free(s->all_fragments);
  2168. av_free(s->coeffs);
  2169. av_free(s->coded_fragment_list);
  2170. av_free(s->superblock_fragments);
  2171. av_free(s->superblock_macroblocks);
  2172. av_free(s->macroblock_fragments);
  2173. av_free(s->macroblock_coding);
  2174. /* release all frames */
  2175. if (s->golden_frame.data[0] && s->golden_frame.data[0] != s->last_frame.data[0])
  2176. avctx->release_buffer(avctx, &s->golden_frame);
  2177. if (s->last_frame.data[0])
  2178. avctx->release_buffer(avctx, &s->last_frame);
  2179. /* no need to release the current_frame since it will always be pointing
  2180. * to the same frame as either the golden or last frame */
  2181. return 0;
  2182. }
  2183. static int read_huffman_tree(AVCodecContext *avctx, GetBitContext *gb)
  2184. {
  2185. Vp3DecodeContext *s = avctx->priv_data;
  2186. if (get_bits(gb, 1)) {
  2187. int token;
  2188. if (s->entries >= 32) { /* overflow */
  2189. av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
  2190. return -1;
  2191. }
  2192. token = get_bits(gb, 5);
  2193. //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);
  2194. s->huffman_table[s->hti][token][0] = s->hbits;
  2195. s->huffman_table[s->hti][token][1] = s->huff_code_size;
  2196. s->entries++;
  2197. }
  2198. else {
  2199. if (s->huff_code_size >= 32) {/* overflow */
  2200. av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
  2201. return -1;
  2202. }
  2203. s->huff_code_size++;
  2204. s->hbits <<= 1;
  2205. read_huffman_tree(avctx, gb);
  2206. s->hbits |= 1;
  2207. read_huffman_tree(avctx, gb);
  2208. s->hbits >>= 1;
  2209. s->huff_code_size--;
  2210. }
  2211. return 0;
  2212. }
  2213. static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb)
  2214. {
  2215. Vp3DecodeContext *s = avctx->priv_data;
  2216. s->theora = get_bits_long(gb, 24);
  2217. av_log(avctx, AV_LOG_INFO, "Theora bitstream version %X\n", s->theora);
  2218. /* 3.2.0 aka alpha3 has the same frame orientation as original vp3 */
  2219. /* but previous versions have the image flipped relative to vp3 */
  2220. if (s->theora < 0x030200)
  2221. {
  2222. s->flipped_image = 1;
  2223. av_log(avctx, AV_LOG_DEBUG, "Old (<alpha3) Theora bitstream, flipped image\n");
  2224. }
  2225. s->width = get_bits(gb, 16) << 4;
  2226. s->height = get_bits(gb, 16) << 4;
  2227. if(avcodec_check_dimensions(avctx, s->width, s->height)){
  2228. av_log(avctx, AV_LOG_ERROR, "Invalid dimensions (%dx%d)\n", s->width, s->height);
  2229. s->width= s->height= 0;
  2230. return -1;
  2231. }
  2232. if (s->theora >= 0x030400)
  2233. {
  2234. skip_bits(gb, 32); /* total number of superblocks in a frame */
  2235. // fixme, the next field is 36bits long
  2236. skip_bits(gb, 32); /* total number of blocks in a frame */
  2237. skip_bits(gb, 4); /* total number of blocks in a frame */
  2238. skip_bits(gb, 32); /* total number of macroblocks in a frame */
  2239. skip_bits(gb, 24); /* frame width */
  2240. skip_bits(gb, 24); /* frame height */
  2241. }
  2242. else
  2243. {
  2244. skip_bits(gb, 24); /* frame width */
  2245. skip_bits(gb, 24); /* frame height */
  2246. }
  2247. if (s->theora >= 0x030200) {
  2248. skip_bits(gb, 8); /* offset x */
  2249. skip_bits(gb, 8); /* offset y */
  2250. }
  2251. skip_bits(gb, 32); /* fps numerator */
  2252. skip_bits(gb, 32); /* fps denumerator */
  2253. skip_bits(gb, 24); /* aspect numerator */
  2254. skip_bits(gb, 24); /* aspect denumerator */
  2255. if (s->theora < 0x030200)
  2256. skip_bits(gb, 5); /* keyframe frequency force */
  2257. skip_bits(gb, 8); /* colorspace */
  2258. if (s->theora >= 0x030400)
  2259. skip_bits(gb, 2); /* pixel format: 420,res,422,444 */
  2260. skip_bits(gb, 24); /* bitrate */
  2261. skip_bits(gb, 6); /* quality hint */
  2262. if (s->theora >= 0x030200)
  2263. {
  2264. skip_bits(gb, 5); /* keyframe frequency force */
  2265. if (s->theora < 0x030400)
  2266. skip_bits(gb, 5); /* spare bits */
  2267. }
  2268. // align_get_bits(gb);
  2269. avctx->width = s->width;
  2270. avctx->height = s->height;
  2271. return 0;
  2272. }
  2273. static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb)
  2274. {
  2275. Vp3DecodeContext *s = avctx->priv_data;
  2276. int i, n, matrices, inter, plane;
  2277. if (s->theora >= 0x030200) {
  2278. n = get_bits(gb, 3);
  2279. /* loop filter limit values table */
  2280. for (i = 0; i < 64; i++)
  2281. s->filter_limit_values[i] = get_bits(gb, n);
  2282. }
  2283. if (s->theora >= 0x030200)
  2284. n = get_bits(gb, 4) + 1;
  2285. else
  2286. n = 16;
  2287. /* quality threshold table */
  2288. for (i = 0; i < 64; i++)
  2289. s->coded_ac_scale_factor[i] = get_bits(gb, n);
  2290. if (s->theora >= 0x030200)
  2291. n = get_bits(gb, 4) + 1;
  2292. else
  2293. n = 16;
  2294. /* dc scale factor table */
  2295. for (i = 0; i < 64; i++)
  2296. s->coded_dc_scale_factor[i] = get_bits(gb, n);
  2297. if (s->theora >= 0x030200)
  2298. matrices = get_bits(gb, 9) + 1;
  2299. else
  2300. matrices = 3;
  2301. if(matrices > 384){
  2302. av_log(avctx, AV_LOG_ERROR, "invalid number of base matrixes\n");
  2303. return -1;
  2304. }
  2305. for(n=0; n<matrices; n++){
  2306. for (i = 0; i < 64; i++)
  2307. s->base_matrix[n][i]= get_bits(gb, 8);
  2308. }
  2309. for (inter = 0; inter <= 1; inter++) {
  2310. for (plane = 0; plane <= 2; plane++) {
  2311. int newqr= 1;
  2312. if (inter || plane > 0)
  2313. newqr = get_bits(gb, 1);
  2314. if (!newqr) {
  2315. int qtj, plj;
  2316. if(inter && get_bits(gb, 1)){
  2317. qtj = 0;
  2318. plj = plane;
  2319. }else{
  2320. qtj= (3*inter + plane - 1) / 3;
  2321. plj= (plane + 2) % 3;
  2322. }
  2323. s->qr_count[inter][plane]= s->qr_count[qtj][plj];
  2324. memcpy(s->qr_size[inter][plane], s->qr_size[qtj][plj], sizeof(s->qr_size[0][0]));
  2325. memcpy(s->qr_base[inter][plane], s->qr_base[qtj][plj], sizeof(s->qr_base[0][0]));
  2326. } else {
  2327. int qri= 0;
  2328. int qi = 0;
  2329. for(;;){
  2330. i= get_bits(gb, av_log2(matrices-1)+1);
  2331. if(i>= matrices){
  2332. av_log(avctx, AV_LOG_ERROR, "invalid base matrix index\n");
  2333. return -1;
  2334. }
  2335. s->qr_base[inter][plane][qri]= i;
  2336. if(qi >= 63)
  2337. break;
  2338. i = get_bits(gb, av_log2(63-qi)+1) + 1;
  2339. s->qr_size[inter][plane][qri++]= i;
  2340. qi += i;
  2341. }
  2342. if (qi > 63) {
  2343. av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi);
  2344. return -1;
  2345. }
  2346. s->qr_count[inter][plane]= qri;
  2347. }
  2348. }
  2349. }
  2350. /* Huffman tables */
  2351. for (s->hti = 0; s->hti < 80; s->hti++) {
  2352. s->entries = 0;
  2353. s->huff_code_size = 1;
  2354. if (!get_bits(gb, 1)) {
  2355. s->hbits = 0;
  2356. read_huffman_tree(avctx, gb);
  2357. s->hbits = 1;
  2358. read_huffman_tree(avctx, gb);
  2359. }
  2360. }
  2361. s->theora_tables = 1;
  2362. return 0;
  2363. }
  2364. static int theora_decode_init(AVCodecContext *avctx)
  2365. {
  2366. Vp3DecodeContext *s = avctx->priv_data;
  2367. GetBitContext gb;
  2368. int ptype;
  2369. uint8_t *p= avctx->extradata;
  2370. int op_bytes, i;
  2371. s->theora = 1;
  2372. if (!avctx->extradata_size)
  2373. {
  2374. av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n");
  2375. return -1;
  2376. }
  2377. for(i=0;i<3;i++) {
  2378. op_bytes = *(p++)<<8;
  2379. op_bytes += *(p++);
  2380. init_get_bits(&gb, p, op_bytes);
  2381. p += op_bytes;
  2382. ptype = get_bits(&gb, 8);
  2383. debug_vp3("Theora headerpacket type: %x\n", ptype);
  2384. if (!(ptype & 0x80))
  2385. {
  2386. av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n");
  2387. // return -1;
  2388. }
  2389. // FIXME: check for this aswell
  2390. skip_bits(&gb, 6*8); /* "theora" */
  2391. switch(ptype)
  2392. {
  2393. case 0x80:
  2394. theora_decode_header(avctx, &gb);
  2395. break;
  2396. case 0x81:
  2397. // FIXME: is this needed? it breaks sometimes
  2398. // theora_decode_comments(avctx, gb);
  2399. break;
  2400. case 0x82:
  2401. theora_decode_tables(avctx, &gb);
  2402. break;
  2403. default:
  2404. av_log(avctx, AV_LOG_ERROR, "Unknown Theora config packet: %d\n", ptype&~0x80);
  2405. break;
  2406. }
  2407. if(8*op_bytes != get_bits_count(&gb))
  2408. av_log(avctx, AV_LOG_ERROR, "%d bits left in packet %X\n", 8*op_bytes - get_bits_count(&gb), ptype);
  2409. if (s->theora < 0x030200)
  2410. break;
  2411. }
  2412. vp3_decode_init(avctx);
  2413. return 0;
  2414. }
  2415. AVCodec vp3_decoder = {
  2416. "vp3",
  2417. CODEC_TYPE_VIDEO,
  2418. CODEC_ID_VP3,
  2419. sizeof(Vp3DecodeContext),
  2420. vp3_decode_init,
  2421. NULL,
  2422. vp3_decode_end,
  2423. vp3_decode_frame,
  2424. 0,
  2425. NULL
  2426. };
  2427. #ifndef CONFIG_LIBTHEORA
  2428. AVCodec theora_decoder = {
  2429. "theora",
  2430. CODEC_TYPE_VIDEO,
  2431. CODEC_ID_THEORA,
  2432. sizeof(Vp3DecodeContext),
  2433. theora_decode_init,
  2434. NULL,
  2435. vp3_decode_end,
  2436. vp3_decode_frame,
  2437. 0,
  2438. NULL
  2439. };
  2440. #endif