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.

2698 lines
95KB

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