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.

2637 lines
94KB

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