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.

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