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.

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