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.

2653 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://multimedia.cx/
  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. uint8_t coeff_count;
  134. int8_t motion_x;
  135. int8_t motion_y;
  136. } Vp3Fragment;
  137. #define SB_NOT_CODED 0
  138. #define SB_PARTIALLY_CODED 1
  139. #define SB_FULLY_CODED 2
  140. #define MODE_INTER_NO_MV 0
  141. #define MODE_INTRA 1
  142. #define MODE_INTER_PLUS_MV 2
  143. #define MODE_INTER_LAST_MV 3
  144. #define MODE_INTER_PRIOR_LAST 4
  145. #define MODE_USING_GOLDEN 5
  146. #define MODE_GOLDEN_MV 6
  147. #define MODE_INTER_FOURMV 7
  148. #define CODING_MODE_COUNT 8
  149. /* special internal mode */
  150. #define MODE_COPY 8
  151. /* There are 6 preset schemes, plus a free-form scheme */
  152. static int ModeAlphabet[7][CODING_MODE_COUNT] =
  153. {
  154. /* this is the custom scheme */
  155. { 0, 0, 0, 0, 0, 0, 0, 0 },
  156. /* scheme 1: Last motion vector dominates */
  157. { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
  158. MODE_INTER_PLUS_MV, MODE_INTER_NO_MV,
  159. MODE_INTRA, MODE_USING_GOLDEN,
  160. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  161. /* scheme 2 */
  162. { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
  163. MODE_INTER_NO_MV, MODE_INTER_PLUS_MV,
  164. MODE_INTRA, MODE_USING_GOLDEN,
  165. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  166. /* scheme 3 */
  167. { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
  168. MODE_INTER_PRIOR_LAST, MODE_INTER_NO_MV,
  169. MODE_INTRA, MODE_USING_GOLDEN,
  170. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  171. /* scheme 4 */
  172. { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
  173. MODE_INTER_NO_MV, MODE_INTER_PRIOR_LAST,
  174. MODE_INTRA, MODE_USING_GOLDEN,
  175. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  176. /* scheme 5: No motion vector dominates */
  177. { MODE_INTER_NO_MV, MODE_INTER_LAST_MV,
  178. MODE_INTER_PRIOR_LAST, MODE_INTER_PLUS_MV,
  179. MODE_INTRA, MODE_USING_GOLDEN,
  180. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  181. /* scheme 6 */
  182. { MODE_INTER_NO_MV, MODE_USING_GOLDEN,
  183. MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
  184. MODE_INTER_PLUS_MV, MODE_INTRA,
  185. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  186. };
  187. #define MIN_DEQUANT_VAL 2
  188. typedef struct Vp3DecodeContext {
  189. AVCodecContext *avctx;
  190. int theora, theora_tables;
  191. int version;
  192. int width, height;
  193. AVFrame golden_frame;
  194. AVFrame last_frame;
  195. AVFrame current_frame;
  196. int keyframe;
  197. DSPContext dsp;
  198. int flipped_image;
  199. int qis[3];
  200. int nqis;
  201. int quality_index;
  202. int last_quality_index;
  203. int superblock_count;
  204. int superblock_width;
  205. int superblock_height;
  206. int y_superblock_width;
  207. int y_superblock_height;
  208. int c_superblock_width;
  209. int c_superblock_height;
  210. int u_superblock_start;
  211. int v_superblock_start;
  212. unsigned char *superblock_coding;
  213. int macroblock_count;
  214. int macroblock_width;
  215. int macroblock_height;
  216. int fragment_count;
  217. int fragment_width;
  218. int fragment_height;
  219. Vp3Fragment *all_fragments;
  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 indices into the all_fragments array indicating
  232. * which of the fragments are coded */
  233. int *coded_fragment_list;
  234. int coded_fragment_list_index;
  235. int pixel_addresses_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 indices 0..15 of the superblock.
  250. * An entry will be -1 to indicate that no entry corresponds to that
  251. * index. */
  252. int *superblock_fragments;
  253. /* This table contains superblock_count * 4 entries. Each set of 4
  254. * numbers corresponds to the macroblock indices 0..3 of the superblock.
  255. * An entry will be -1 to indicate that no entry corresponds to that
  256. * index. */
  257. int *superblock_macroblocks;
  258. /* This table contains macroblock_count * 6 entries. Each set of 6
  259. * numbers corresponds to the fragment indices 0..5 which comprise
  260. * the macroblock (4 Y fragments and 2 C fragments). */
  261. int *macroblock_fragments;
  262. /* This is an array that indicates how a particular macroblock
  263. * is coded. */
  264. unsigned char *macroblock_coding;
  265. int first_coded_y_fragment;
  266. int first_coded_c_fragment;
  267. int last_coded_y_fragment;
  268. int last_coded_c_fragment;
  269. uint8_t edge_emu_buffer[9*2048]; //FIXME dynamic alloc
  270. 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->all_fragments[i].coeff_count = 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. fragment = &s->all_fragments[s->coded_fragment_list[i]];
  989. if (fragment->coeff_count > coeff_index)
  990. continue;
  991. if (!eob_run) {
  992. /* decode a VLC into a token */
  993. token = get_vlc2(gb, table->table, 5, 3);
  994. debug_vlc(" token = %2d, ", token);
  995. /* use the token to get a zero run, a coefficient, and an eob run */
  996. if (token <= 6) {
  997. eob_run = eob_run_base[token];
  998. if (eob_run_get_bits[token])
  999. eob_run += get_bits(gb, eob_run_get_bits[token]);
  1000. coeff = zero_run = 0;
  1001. } else {
  1002. bits_to_get = coeff_get_bits[token];
  1003. if (!bits_to_get)
  1004. coeff = coeff_tables[token][0];
  1005. else
  1006. coeff = coeff_tables[token][get_bits(gb, bits_to_get)];
  1007. zero_run = zero_run_base[token];
  1008. if (zero_run_get_bits[token])
  1009. zero_run += get_bits(gb, zero_run_get_bits[token]);
  1010. }
  1011. }
  1012. if (!eob_run) {
  1013. fragment->coeff_count += zero_run;
  1014. if (fragment->coeff_count < 64){
  1015. fragment->next_coeff->coeff= coeff;
  1016. fragment->next_coeff->index= perm[fragment->coeff_count++]; //FIXME perm here already?
  1017. fragment->next_coeff->next= s->next_coeff;
  1018. s->next_coeff->next=NULL;
  1019. fragment->next_coeff= s->next_coeff++;
  1020. }
  1021. debug_vlc(" fragment %d coeff = %d\n",
  1022. s->coded_fragment_list[i], fragment->next_coeff[coeff_index]);
  1023. } else {
  1024. fragment->coeff_count |= 128;
  1025. debug_vlc(" fragment %d eob with %d coefficients\n",
  1026. s->coded_fragment_list[i], fragment->coeff_count&127);
  1027. eob_run--;
  1028. }
  1029. }
  1030. return eob_run;
  1031. }
  1032. /*
  1033. * This function unpacks all of the DCT coefficient data from the
  1034. * bitstream.
  1035. */
  1036. static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
  1037. {
  1038. int i;
  1039. int dc_y_table;
  1040. int dc_c_table;
  1041. int ac_y_table;
  1042. int ac_c_table;
  1043. int residual_eob_run = 0;
  1044. /* fetch the DC table indices */
  1045. dc_y_table = get_bits(gb, 4);
  1046. dc_c_table = get_bits(gb, 4);
  1047. /* unpack the Y plane DC coefficients */
  1048. debug_vp3(" vp3: unpacking Y plane DC coefficients using table %d\n",
  1049. dc_y_table);
  1050. residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
  1051. s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
  1052. /* unpack the C plane DC coefficients */
  1053. debug_vp3(" vp3: unpacking C plane DC coefficients using table %d\n",
  1054. dc_c_table);
  1055. residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
  1056. s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
  1057. /* fetch the AC table indices */
  1058. ac_y_table = get_bits(gb, 4);
  1059. ac_c_table = get_bits(gb, 4);
  1060. /* unpack the group 1 AC coefficients (coeffs 1-5) */
  1061. for (i = 1; i <= 5; i++) {
  1062. debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
  1063. i, ac_y_table);
  1064. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_y_table], i,
  1065. s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
  1066. debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
  1067. i, ac_c_table);
  1068. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_c_table], i,
  1069. s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
  1070. }
  1071. /* unpack the group 2 AC coefficients (coeffs 6-14) */
  1072. for (i = 6; i <= 14; i++) {
  1073. debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
  1074. i, ac_y_table);
  1075. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_y_table], i,
  1076. s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
  1077. debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
  1078. i, ac_c_table);
  1079. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_c_table], i,
  1080. s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
  1081. }
  1082. /* unpack the group 3 AC coefficients (coeffs 15-27) */
  1083. for (i = 15; i <= 27; i++) {
  1084. debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
  1085. i, ac_y_table);
  1086. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_y_table], i,
  1087. s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
  1088. debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
  1089. i, ac_c_table);
  1090. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_c_table], i,
  1091. s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
  1092. }
  1093. /* unpack the group 4 AC coefficients (coeffs 28-63) */
  1094. for (i = 28; i <= 63; i++) {
  1095. debug_vp3(" vp3: unpacking level %d Y plane AC coefficients using table %d\n",
  1096. i, ac_y_table);
  1097. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_y_table], i,
  1098. s->first_coded_y_fragment, s->last_coded_y_fragment, residual_eob_run);
  1099. debug_vp3(" vp3: unpacking level %d C plane AC coefficients using table %d\n",
  1100. i, ac_c_table);
  1101. residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_c_table], i,
  1102. s->first_coded_c_fragment, s->last_coded_c_fragment, residual_eob_run);
  1103. }
  1104. return 0;
  1105. }
  1106. /*
  1107. * This function reverses the DC prediction for each coded fragment in
  1108. * the frame. Much of this function is adapted directly from the original
  1109. * VP3 source code.
  1110. */
  1111. #define COMPATIBLE_FRAME(x) \
  1112. (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
  1113. #define FRAME_CODED(x) (s->all_fragments[x].coding_method != MODE_COPY)
  1114. #define DC_COEFF(u) (s->coeffs[u].index ? 0 : s->coeffs[u].coeff) //FIXME do somethin to simplify this
  1115. static void reverse_dc_prediction(Vp3DecodeContext *s,
  1116. int first_fragment,
  1117. int fragment_width,
  1118. int fragment_height)
  1119. {
  1120. #define PUL 8
  1121. #define PU 4
  1122. #define PUR 2
  1123. #define PL 1
  1124. int x, y;
  1125. int i = first_fragment;
  1126. int predicted_dc;
  1127. /* DC values for the left, up-left, up, and up-right fragments */
  1128. int vl, vul, vu, vur;
  1129. /* indices for the left, up-left, up, and up-right fragments */
  1130. int l, ul, u, ur;
  1131. /*
  1132. * The 6 fields mean:
  1133. * 0: up-left multiplier
  1134. * 1: up multiplier
  1135. * 2: up-right multiplier
  1136. * 3: left multiplier
  1137. */
  1138. int predictor_transform[16][4] = {
  1139. { 0, 0, 0, 0},
  1140. { 0, 0, 0,128}, // PL
  1141. { 0, 0,128, 0}, // PUR
  1142. { 0, 0, 53, 75}, // PUR|PL
  1143. { 0,128, 0, 0}, // PU
  1144. { 0, 64, 0, 64}, // PU|PL
  1145. { 0,128, 0, 0}, // PU|PUR
  1146. { 0, 0, 53, 75}, // PU|PUR|PL
  1147. {128, 0, 0, 0}, // PUL
  1148. { 0, 0, 0,128}, // PUL|PL
  1149. { 64, 0, 64, 0}, // PUL|PUR
  1150. { 0, 0, 53, 75}, // PUL|PUR|PL
  1151. { 0,128, 0, 0}, // PUL|PU
  1152. {-104,116, 0,116}, // PUL|PU|PL
  1153. { 24, 80, 24, 0}, // PUL|PU|PUR
  1154. {-104,116, 0,116} // PUL|PU|PUR|PL
  1155. };
  1156. /* This table shows which types of blocks can use other blocks for
  1157. * prediction. For example, INTRA is the only mode in this table to
  1158. * have a frame number of 0. That means INTRA blocks can only predict
  1159. * from other INTRA blocks. There are 2 golden frame coding types;
  1160. * blocks encoding in these modes can only predict from other blocks
  1161. * that were encoded with these 1 of these 2 modes. */
  1162. unsigned char compatible_frame[8] = {
  1163. 1, /* MODE_INTER_NO_MV */
  1164. 0, /* MODE_INTRA */
  1165. 1, /* MODE_INTER_PLUS_MV */
  1166. 1, /* MODE_INTER_LAST_MV */
  1167. 1, /* MODE_INTER_PRIOR_MV */
  1168. 2, /* MODE_USING_GOLDEN */
  1169. 2, /* MODE_GOLDEN_MV */
  1170. 1 /* MODE_INTER_FOUR_MV */
  1171. };
  1172. int current_frame_type;
  1173. /* there is a last DC predictor for each of the 3 frame types */
  1174. short last_dc[3];
  1175. int transform = 0;
  1176. debug_vp3(" vp3: reversing DC prediction\n");
  1177. vul = vu = vur = vl = 0;
  1178. last_dc[0] = last_dc[1] = last_dc[2] = 0;
  1179. /* for each fragment row... */
  1180. for (y = 0; y < fragment_height; y++) {
  1181. /* for each fragment in a row... */
  1182. for (x = 0; x < fragment_width; x++, i++) {
  1183. /* reverse prediction if this block was coded */
  1184. if (s->all_fragments[i].coding_method != MODE_COPY) {
  1185. current_frame_type =
  1186. compatible_frame[s->all_fragments[i].coding_method];
  1187. debug_dc_pred(" frag %d: orig DC = %d, ",
  1188. i, DC_COEFF(i));
  1189. transform= 0;
  1190. if(x){
  1191. l= i-1;
  1192. vl = DC_COEFF(l);
  1193. if(FRAME_CODED(l) && COMPATIBLE_FRAME(l))
  1194. transform |= PL;
  1195. }
  1196. if(y){
  1197. u= i-fragment_width;
  1198. vu = DC_COEFF(u);
  1199. if(FRAME_CODED(u) && COMPATIBLE_FRAME(u))
  1200. transform |= PU;
  1201. if(x){
  1202. ul= i-fragment_width-1;
  1203. vul = DC_COEFF(ul);
  1204. if(FRAME_CODED(ul) && COMPATIBLE_FRAME(ul))
  1205. transform |= PUL;
  1206. }
  1207. if(x + 1 < fragment_width){
  1208. ur= i-fragment_width+1;
  1209. vur = DC_COEFF(ur);
  1210. if(FRAME_CODED(ur) && COMPATIBLE_FRAME(ur))
  1211. transform |= PUR;
  1212. }
  1213. }
  1214. debug_dc_pred("transform = %d, ", transform);
  1215. if (transform == 0) {
  1216. /* if there were no fragments to predict from, use last
  1217. * DC saved */
  1218. predicted_dc = last_dc[current_frame_type];
  1219. debug_dc_pred("from last DC (%d) = %d\n",
  1220. current_frame_type, DC_COEFF(i));
  1221. } else {
  1222. /* apply the appropriate predictor transform */
  1223. predicted_dc =
  1224. (predictor_transform[transform][0] * vul) +
  1225. (predictor_transform[transform][1] * vu) +
  1226. (predictor_transform[transform][2] * vur) +
  1227. (predictor_transform[transform][3] * vl);
  1228. predicted_dc /= 128;
  1229. /* check for outranging on the [ul u l] and
  1230. * [ul u ur l] predictors */
  1231. if ((transform == 13) || (transform == 15)) {
  1232. if (FFABS(predicted_dc - vu) > 128)
  1233. predicted_dc = vu;
  1234. else if (FFABS(predicted_dc - vl) > 128)
  1235. predicted_dc = vl;
  1236. else if (FFABS(predicted_dc - vul) > 128)
  1237. predicted_dc = vul;
  1238. }
  1239. debug_dc_pred("from pred DC = %d\n",
  1240. DC_COEFF(i));
  1241. }
  1242. /* at long last, apply the predictor */
  1243. if(s->coeffs[i].index){
  1244. *s->next_coeff= s->coeffs[i];
  1245. s->coeffs[i].index=0;
  1246. s->coeffs[i].coeff=0;
  1247. s->coeffs[i].next= s->next_coeff++;
  1248. }
  1249. s->coeffs[i].coeff += predicted_dc;
  1250. /* save the DC */
  1251. last_dc[current_frame_type] = DC_COEFF(i);
  1252. if(DC_COEFF(i) && !(s->all_fragments[i].coeff_count&127)){
  1253. s->all_fragments[i].coeff_count= 129;
  1254. // s->all_fragments[i].next_coeff= s->next_coeff;
  1255. s->coeffs[i].next= s->next_coeff;
  1256. (s->next_coeff++)->next=NULL;
  1257. }
  1258. }
  1259. }
  1260. }
  1261. }
  1262. static void horizontal_filter(unsigned char *first_pixel, int stride,
  1263. int *bounding_values);
  1264. static void vertical_filter(unsigned char *first_pixel, int stride,
  1265. int *bounding_values);
  1266. /*
  1267. * Perform the final rendering for a particular slice of data.
  1268. * The slice number ranges from 0..(macroblock_height - 1).
  1269. */
  1270. static void render_slice(Vp3DecodeContext *s, int slice)
  1271. {
  1272. int x;
  1273. int m, n;
  1274. int16_t *dequantizer;
  1275. DECLARE_ALIGNED_16(DCTELEM, block[64]);
  1276. int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
  1277. int motion_halfpel_index;
  1278. uint8_t *motion_source;
  1279. int plane;
  1280. int current_macroblock_entry = slice * s->macroblock_width * 6;
  1281. if (slice >= s->macroblock_height)
  1282. return;
  1283. for (plane = 0; plane < 3; plane++) {
  1284. uint8_t *output_plane = s->current_frame.data [plane];
  1285. uint8_t * last_plane = s-> last_frame.data [plane];
  1286. uint8_t *golden_plane = s-> golden_frame.data [plane];
  1287. int stride = s->current_frame.linesize[plane];
  1288. int plane_width = s->width >> !!plane;
  1289. int plane_height = s->height >> !!plane;
  1290. int y = slice * FRAGMENT_PIXELS << !plane ;
  1291. int slice_height = y + (FRAGMENT_PIXELS << !plane);
  1292. int i = s->macroblock_fragments[current_macroblock_entry + plane + 3*!!plane];
  1293. if (!s->flipped_image) stride = -stride;
  1294. if(FFABS(stride) > 2048)
  1295. return; //various tables are fixed size
  1296. /* for each fragment row in the slice (both of them)... */
  1297. for (; y < slice_height; y += 8) {
  1298. /* for each fragment in a row... */
  1299. for (x = 0; x < plane_width; x += 8, i++) {
  1300. if ((i < 0) || (i >= s->fragment_count)) {
  1301. av_log(s->avctx, AV_LOG_ERROR, " vp3:render_slice(): bad fragment number (%d)\n", i);
  1302. return;
  1303. }
  1304. /* transform if this block was coded */
  1305. if ((s->all_fragments[i].coding_method != MODE_COPY) &&
  1306. !((s->avctx->flags & CODEC_FLAG_GRAY) && plane)) {
  1307. if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
  1308. (s->all_fragments[i].coding_method == MODE_GOLDEN_MV))
  1309. motion_source= golden_plane;
  1310. else
  1311. motion_source= last_plane;
  1312. motion_source += s->all_fragments[i].first_pixel;
  1313. motion_halfpel_index = 0;
  1314. /* sort out the motion vector if this fragment is coded
  1315. * using a motion vector method */
  1316. if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
  1317. (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) {
  1318. int src_x, src_y;
  1319. motion_x = s->all_fragments[i].motion_x;
  1320. motion_y = s->all_fragments[i].motion_y;
  1321. if(plane){
  1322. motion_x= (motion_x>>1) | (motion_x&1);
  1323. motion_y= (motion_y>>1) | (motion_y&1);
  1324. }
  1325. src_x= (motion_x>>1) + x;
  1326. src_y= (motion_y>>1) + y;
  1327. if ((motion_x == 127) || (motion_y == 127))
  1328. av_log(s->avctx, AV_LOG_ERROR, " help! got invalid motion vector! (%X, %X)\n", motion_x, motion_y);
  1329. motion_halfpel_index = motion_x & 0x01;
  1330. motion_source += (motion_x >> 1);
  1331. motion_halfpel_index |= (motion_y & 0x01) << 1;
  1332. motion_source += ((motion_y >> 1) * stride);
  1333. if(src_x<0 || src_y<0 || src_x + 9 >= plane_width || src_y + 9 >= plane_height){
  1334. uint8_t *temp= s->edge_emu_buffer;
  1335. if(stride<0) temp -= 9*stride;
  1336. else temp += 9*stride;
  1337. ff_emulated_edge_mc(temp, motion_source, stride, 9, 9, src_x, src_y, plane_width, plane_height);
  1338. motion_source= temp;
  1339. }
  1340. }
  1341. /* first, take care of copying a block from either the
  1342. * previous or the golden frame */
  1343. if (s->all_fragments[i].coding_method != MODE_INTRA) {
  1344. /* Note, it is possible to implement all MC cases with
  1345. put_no_rnd_pixels_l2 which would look more like the
  1346. VP3 source but this would be slower as
  1347. put_no_rnd_pixels_tab is better optimzed */
  1348. if(motion_halfpel_index != 3){
  1349. s->dsp.put_no_rnd_pixels_tab[1][motion_halfpel_index](
  1350. output_plane + s->all_fragments[i].first_pixel,
  1351. motion_source, stride, 8);
  1352. }else{
  1353. int d= (motion_x ^ motion_y)>>31; // d is 0 if motion_x and _y have the same sign, else -1
  1354. s->dsp.put_no_rnd_pixels_l2[1](
  1355. output_plane + s->all_fragments[i].first_pixel,
  1356. motion_source - d,
  1357. motion_source + stride + 1 + d,
  1358. stride, 8);
  1359. }
  1360. dequantizer = s->qmat[1][plane];
  1361. }else{
  1362. dequantizer = s->qmat[0][plane];
  1363. }
  1364. /* dequantize the DCT coefficients */
  1365. debug_idct("fragment %d, coding mode %d, DC = %d, dequant = %d:\n",
  1366. i, s->all_fragments[i].coding_method,
  1367. DC_COEFF(i), dequantizer[0]);
  1368. if(s->avctx->idct_algo==FF_IDCT_VP3){
  1369. Coeff *coeff= s->coeffs + i;
  1370. memset(block, 0, sizeof(block));
  1371. while(coeff->next){
  1372. block[coeff->index]= coeff->coeff * dequantizer[coeff->index];
  1373. coeff= coeff->next;
  1374. }
  1375. }else{
  1376. Coeff *coeff= s->coeffs + i;
  1377. memset(block, 0, sizeof(block));
  1378. while(coeff->next){
  1379. block[coeff->index]= (coeff->coeff * dequantizer[coeff->index] + 2)>>2;
  1380. coeff= coeff->next;
  1381. }
  1382. }
  1383. /* invert DCT and place (or add) in final output */
  1384. if (s->all_fragments[i].coding_method == MODE_INTRA) {
  1385. if(s->avctx->idct_algo!=FF_IDCT_VP3)
  1386. block[0] += 128<<3;
  1387. s->dsp.idct_put(
  1388. output_plane + s->all_fragments[i].first_pixel,
  1389. stride,
  1390. block);
  1391. } else {
  1392. s->dsp.idct_add(
  1393. output_plane + s->all_fragments[i].first_pixel,
  1394. stride,
  1395. block);
  1396. }
  1397. debug_idct("block after idct_%s():\n",
  1398. (s->all_fragments[i].coding_method == MODE_INTRA)?
  1399. "put" : "add");
  1400. for (m = 0; m < 8; m++) {
  1401. for (n = 0; n < 8; n++) {
  1402. debug_idct(" %3d", *(output_plane +
  1403. s->all_fragments[i].first_pixel + (m * stride + n)));
  1404. }
  1405. debug_idct("\n");
  1406. }
  1407. debug_idct("\n");
  1408. } else {
  1409. /* copy directly from the previous frame */
  1410. s->dsp.put_pixels_tab[1][0](
  1411. output_plane + s->all_fragments[i].first_pixel,
  1412. last_plane + s->all_fragments[i].first_pixel,
  1413. stride, 8);
  1414. }
  1415. #if 0
  1416. /* perform the left edge filter if:
  1417. * - the fragment is not on the left column
  1418. * - the fragment is coded in this frame
  1419. * - the fragment is not coded in this frame but the left
  1420. * fragment is coded in this frame (this is done instead
  1421. * of a right edge filter when rendering the left fragment
  1422. * since this fragment is not available yet) */
  1423. if ((x > 0) &&
  1424. ((s->all_fragments[i].coding_method != MODE_COPY) ||
  1425. ((s->all_fragments[i].coding_method == MODE_COPY) &&
  1426. (s->all_fragments[i - 1].coding_method != MODE_COPY)) )) {
  1427. horizontal_filter(
  1428. output_plane + s->all_fragments[i].first_pixel + 7*stride,
  1429. -stride, s->bounding_values_array + 127);
  1430. }
  1431. /* perform the top edge filter if:
  1432. * - the fragment is not on the top row
  1433. * - the fragment is coded in this frame
  1434. * - the fragment is not coded in this frame but the above
  1435. * fragment is coded in this frame (this is done instead
  1436. * of a bottom edge filter when rendering the above
  1437. * fragment since this fragment is not available yet) */
  1438. if ((y > 0) &&
  1439. ((s->all_fragments[i].coding_method != MODE_COPY) ||
  1440. ((s->all_fragments[i].coding_method == MODE_COPY) &&
  1441. (s->all_fragments[i - fragment_width].coding_method != MODE_COPY)) )) {
  1442. vertical_filter(
  1443. output_plane + s->all_fragments[i].first_pixel - stride,
  1444. -stride, s->bounding_values_array + 127);
  1445. }
  1446. #endif
  1447. }
  1448. }
  1449. }
  1450. /* this looks like a good place for slice dispatch... */
  1451. /* algorithm:
  1452. * if (slice == s->macroblock_height - 1)
  1453. * dispatch (both last slice & 2nd-to-last slice);
  1454. * else if (slice > 0)
  1455. * dispatch (slice - 1);
  1456. */
  1457. emms_c();
  1458. }
  1459. static void horizontal_filter(unsigned char *first_pixel, int stride,
  1460. int *bounding_values)
  1461. {
  1462. unsigned char *end;
  1463. int filter_value;
  1464. for (end= first_pixel + 8*stride; first_pixel != end; first_pixel += stride) {
  1465. filter_value =
  1466. (first_pixel[-2] - first_pixel[ 1])
  1467. +3*(first_pixel[ 0] - first_pixel[-1]);
  1468. filter_value = bounding_values[(filter_value + 4) >> 3];
  1469. first_pixel[-1] = av_clip_uint8(first_pixel[-1] + filter_value);
  1470. first_pixel[ 0] = av_clip_uint8(first_pixel[ 0] - filter_value);
  1471. }
  1472. }
  1473. static void vertical_filter(unsigned char *first_pixel, int stride,
  1474. int *bounding_values)
  1475. {
  1476. unsigned char *end;
  1477. int filter_value;
  1478. const int nstride= -stride;
  1479. for (end= first_pixel + 8; first_pixel < end; first_pixel++) {
  1480. filter_value =
  1481. (first_pixel[2 * nstride] - first_pixel[ stride])
  1482. +3*(first_pixel[0 ] - first_pixel[nstride]);
  1483. filter_value = bounding_values[(filter_value + 4) >> 3];
  1484. first_pixel[nstride] = av_clip_uint8(first_pixel[nstride] + filter_value);
  1485. first_pixel[0] = av_clip_uint8(first_pixel[0] - filter_value);
  1486. }
  1487. }
  1488. static void apply_loop_filter(Vp3DecodeContext *s)
  1489. {
  1490. int plane;
  1491. int x, y;
  1492. int *bounding_values= s->bounding_values_array+127;
  1493. #if 0
  1494. int bounding_values_array[256];
  1495. int filter_limit;
  1496. /* find the right loop limit value */
  1497. for (x = 63; x >= 0; x--) {
  1498. if (vp31_ac_scale_factor[x] >= s->quality_index)
  1499. break;
  1500. }
  1501. filter_limit = vp31_filter_limit_values[s->quality_index];
  1502. /* set up the bounding values */
  1503. memset(bounding_values_array, 0, 256 * sizeof(int));
  1504. for (x = 0; x < filter_limit; x++) {
  1505. bounding_values[-x - filter_limit] = -filter_limit + x;
  1506. bounding_values[-x] = -x;
  1507. bounding_values[x] = x;
  1508. bounding_values[x + filter_limit] = filter_limit - x;
  1509. }
  1510. #endif
  1511. for (plane = 0; plane < 3; plane++) {
  1512. int width = s->fragment_width >> !!plane;
  1513. int height = s->fragment_height >> !!plane;
  1514. int fragment = s->fragment_start [plane];
  1515. int stride = s->current_frame.linesize[plane];
  1516. uint8_t *plane_data = s->current_frame.data [plane];
  1517. if (!s->flipped_image) stride = -stride;
  1518. for (y = 0; y < height; y++) {
  1519. for (x = 0; x < width; x++) {
  1520. START_TIMER
  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. STOP_TIMER("loop filter")
  1557. }
  1558. }
  1559. }
  1560. }
  1561. /*
  1562. * This function computes the first pixel addresses for each fragment.
  1563. * This function needs to be invoked after the first frame is allocated
  1564. * so that it has access to the plane strides.
  1565. */
  1566. static void vp3_calculate_pixel_addresses(Vp3DecodeContext *s)
  1567. {
  1568. int i, x, y;
  1569. /* figure out the first pixel addresses for each of the fragments */
  1570. /* Y plane */
  1571. i = 0;
  1572. for (y = s->fragment_height; y > 0; y--) {
  1573. for (x = 0; x < s->fragment_width; x++) {
  1574. s->all_fragments[i++].first_pixel =
  1575. s->golden_frame.linesize[0] * y * FRAGMENT_PIXELS -
  1576. s->golden_frame.linesize[0] +
  1577. x * FRAGMENT_PIXELS;
  1578. debug_init(" fragment %d, first pixel @ %d\n",
  1579. i-1, s->all_fragments[i-1].first_pixel);
  1580. }
  1581. }
  1582. /* U plane */
  1583. i = s->fragment_start[1];
  1584. for (y = s->fragment_height / 2; y > 0; y--) {
  1585. for (x = 0; x < s->fragment_width / 2; x++) {
  1586. s->all_fragments[i++].first_pixel =
  1587. s->golden_frame.linesize[1] * y * FRAGMENT_PIXELS -
  1588. s->golden_frame.linesize[1] +
  1589. x * FRAGMENT_PIXELS;
  1590. debug_init(" fragment %d, first pixel @ %d\n",
  1591. i-1, s->all_fragments[i-1].first_pixel);
  1592. }
  1593. }
  1594. /* V plane */
  1595. i = s->fragment_start[2];
  1596. for (y = s->fragment_height / 2; y > 0; y--) {
  1597. for (x = 0; x < s->fragment_width / 2; x++) {
  1598. s->all_fragments[i++].first_pixel =
  1599. s->golden_frame.linesize[2] * y * FRAGMENT_PIXELS -
  1600. s->golden_frame.linesize[2] +
  1601. x * FRAGMENT_PIXELS;
  1602. debug_init(" fragment %d, first pixel @ %d\n",
  1603. i-1, s->all_fragments[i-1].first_pixel);
  1604. }
  1605. }
  1606. }
  1607. /* FIXME: this should be merged with the above! */
  1608. static void theora_calculate_pixel_addresses(Vp3DecodeContext *s)
  1609. {
  1610. int i, x, y;
  1611. /* figure out the first pixel addresses for each of the fragments */
  1612. /* Y plane */
  1613. i = 0;
  1614. for (y = 1; y <= s->fragment_height; y++) {
  1615. for (x = 0; x < s->fragment_width; x++) {
  1616. s->all_fragments[i++].first_pixel =
  1617. s->golden_frame.linesize[0] * y * FRAGMENT_PIXELS -
  1618. s->golden_frame.linesize[0] +
  1619. x * FRAGMENT_PIXELS;
  1620. debug_init(" fragment %d, first pixel @ %d\n",
  1621. i-1, s->all_fragments[i-1].first_pixel);
  1622. }
  1623. }
  1624. /* U plane */
  1625. i = s->fragment_start[1];
  1626. for (y = 1; y <= s->fragment_height / 2; y++) {
  1627. for (x = 0; x < s->fragment_width / 2; x++) {
  1628. s->all_fragments[i++].first_pixel =
  1629. s->golden_frame.linesize[1] * y * FRAGMENT_PIXELS -
  1630. s->golden_frame.linesize[1] +
  1631. x * FRAGMENT_PIXELS;
  1632. debug_init(" fragment %d, first pixel @ %d\n",
  1633. i-1, s->all_fragments[i-1].first_pixel);
  1634. }
  1635. }
  1636. /* V plane */
  1637. i = s->fragment_start[2];
  1638. for (y = 1; y <= s->fragment_height / 2; y++) {
  1639. for (x = 0; x < s->fragment_width / 2; x++) {
  1640. s->all_fragments[i++].first_pixel =
  1641. s->golden_frame.linesize[2] * y * FRAGMENT_PIXELS -
  1642. s->golden_frame.linesize[2] +
  1643. x * FRAGMENT_PIXELS;
  1644. debug_init(" fragment %d, first pixel @ %d\n",
  1645. i-1, s->all_fragments[i-1].first_pixel);
  1646. }
  1647. }
  1648. }
  1649. /*
  1650. * This is the ffmpeg/libavcodec API init function.
  1651. */
  1652. static int vp3_decode_init(AVCodecContext *avctx)
  1653. {
  1654. Vp3DecodeContext *s = avctx->priv_data;
  1655. int i, inter, plane;
  1656. int c_width;
  1657. int c_height;
  1658. int y_superblock_count;
  1659. int c_superblock_count;
  1660. if (avctx->codec_tag == MKTAG('V','P','3','0'))
  1661. s->version = 0;
  1662. else
  1663. s->version = 1;
  1664. s->avctx = avctx;
  1665. s->width = (avctx->width + 15) & 0xFFFFFFF0;
  1666. s->height = (avctx->height + 15) & 0xFFFFFFF0;
  1667. avctx->pix_fmt = PIX_FMT_YUV420P;
  1668. if(avctx->idct_algo==FF_IDCT_AUTO)
  1669. avctx->idct_algo=FF_IDCT_VP3;
  1670. dsputil_init(&s->dsp, avctx);
  1671. ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
  1672. /* initialize to an impossible value which will force a recalculation
  1673. * in the first frame decode */
  1674. s->quality_index = -1;
  1675. s->y_superblock_width = (s->width + 31) / 32;
  1676. s->y_superblock_height = (s->height + 31) / 32;
  1677. y_superblock_count = s->y_superblock_width * s->y_superblock_height;
  1678. /* work out the dimensions for the C planes */
  1679. c_width = s->width / 2;
  1680. c_height = s->height / 2;
  1681. s->c_superblock_width = (c_width + 31) / 32;
  1682. s->c_superblock_height = (c_height + 31) / 32;
  1683. c_superblock_count = s->c_superblock_width * s->c_superblock_height;
  1684. s->superblock_count = y_superblock_count + (c_superblock_count * 2);
  1685. s->u_superblock_start = y_superblock_count;
  1686. s->v_superblock_start = s->u_superblock_start + c_superblock_count;
  1687. s->superblock_coding = av_malloc(s->superblock_count);
  1688. s->macroblock_width = (s->width + 15) / 16;
  1689. s->macroblock_height = (s->height + 15) / 16;
  1690. s->macroblock_count = s->macroblock_width * s->macroblock_height;
  1691. s->fragment_width = s->width / FRAGMENT_PIXELS;
  1692. s->fragment_height = s->height / FRAGMENT_PIXELS;
  1693. /* fragment count covers all 8x8 blocks for all 3 planes */
  1694. s->fragment_count = s->fragment_width * s->fragment_height * 3 / 2;
  1695. s->fragment_start[1] = s->fragment_width * s->fragment_height;
  1696. s->fragment_start[2] = s->fragment_width * s->fragment_height * 5 / 4;
  1697. debug_init(" Y plane: %d x %d\n", s->width, s->height);
  1698. debug_init(" C plane: %d x %d\n", c_width, c_height);
  1699. debug_init(" Y superblocks: %d x %d, %d total\n",
  1700. s->y_superblock_width, s->y_superblock_height, y_superblock_count);
  1701. debug_init(" C superblocks: %d x %d, %d total\n",
  1702. s->c_superblock_width, s->c_superblock_height, c_superblock_count);
  1703. debug_init(" total superblocks = %d, U starts @ %d, V starts @ %d\n",
  1704. s->superblock_count, s->u_superblock_start, s->v_superblock_start);
  1705. debug_init(" macroblocks: %d x %d, %d total\n",
  1706. s->macroblock_width, s->macroblock_height, s->macroblock_count);
  1707. debug_init(" %d fragments, %d x %d, u starts @ %d, v starts @ %d\n",
  1708. s->fragment_count,
  1709. s->fragment_width,
  1710. s->fragment_height,
  1711. s->fragment_start[1],
  1712. s->fragment_start[2]);
  1713. s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment));
  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. {START_TIMER
  1900. init_frame(s, &gb);
  1901. STOP_TIMER("init_frame")}
  1902. #if KEYFRAMES_ONLY
  1903. if (!s->keyframe) {
  1904. memcpy(s->current_frame.data[0], s->golden_frame.data[0],
  1905. s->current_frame.linesize[0] * s->height);
  1906. memcpy(s->current_frame.data[1], s->golden_frame.data[1],
  1907. s->current_frame.linesize[1] * s->height / 2);
  1908. memcpy(s->current_frame.data[2], s->golden_frame.data[2],
  1909. s->current_frame.linesize[2] * s->height / 2);
  1910. } else {
  1911. #endif
  1912. {START_TIMER
  1913. if (unpack_superblocks(s, &gb)){
  1914. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n");
  1915. return -1;
  1916. }
  1917. STOP_TIMER("unpack_superblocks")}
  1918. {START_TIMER
  1919. if (unpack_modes(s, &gb)){
  1920. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n");
  1921. return -1;
  1922. }
  1923. STOP_TIMER("unpack_modes")}
  1924. {START_TIMER
  1925. if (unpack_vectors(s, &gb)){
  1926. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n");
  1927. return -1;
  1928. }
  1929. STOP_TIMER("unpack_vectors")}
  1930. {START_TIMER
  1931. if (unpack_dct_coeffs(s, &gb)){
  1932. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n");
  1933. return -1;
  1934. }
  1935. STOP_TIMER("unpack_dct_coeffs")}
  1936. {START_TIMER
  1937. reverse_dc_prediction(s, 0, s->fragment_width, s->fragment_height);
  1938. if ((avctx->flags & CODEC_FLAG_GRAY) == 0) {
  1939. reverse_dc_prediction(s, s->fragment_start[1],
  1940. s->fragment_width / 2, s->fragment_height / 2);
  1941. reverse_dc_prediction(s, s->fragment_start[2],
  1942. s->fragment_width / 2, s->fragment_height / 2);
  1943. }
  1944. STOP_TIMER("reverse_dc_prediction")}
  1945. {START_TIMER
  1946. for (i = 0; i < s->macroblock_height; i++)
  1947. render_slice(s, i);
  1948. STOP_TIMER("render_fragments")}
  1949. {START_TIMER
  1950. apply_loop_filter(s);
  1951. STOP_TIMER("apply_loop_filter")}
  1952. #if KEYFRAMES_ONLY
  1953. }
  1954. #endif
  1955. *data_size=sizeof(AVFrame);
  1956. *(AVFrame*)data= s->current_frame;
  1957. /* release the last frame, if it is allocated and if it is not the
  1958. * golden frame */
  1959. if ((s->last_frame.data[0]) &&
  1960. (s->last_frame.data[0] != s->golden_frame.data[0]))
  1961. avctx->release_buffer(avctx, &s->last_frame);
  1962. /* shuffle frames (last = current) */
  1963. s->last_frame= s->current_frame;
  1964. s->current_frame.data[0]= NULL; /* ensure that we catch any access to this released frame */
  1965. return buf_size;
  1966. }
  1967. /*
  1968. * This is the ffmpeg/libavcodec API module cleanup function.
  1969. */
  1970. static int vp3_decode_end(AVCodecContext *avctx)
  1971. {
  1972. Vp3DecodeContext *s = avctx->priv_data;
  1973. int i;
  1974. av_free(s->superblock_coding);
  1975. av_free(s->all_fragments);
  1976. av_free(s->coeffs);
  1977. av_free(s->coded_fragment_list);
  1978. av_free(s->superblock_fragments);
  1979. av_free(s->superblock_macroblocks);
  1980. av_free(s->macroblock_fragments);
  1981. av_free(s->macroblock_coding);
  1982. for (i = 0; i < 16; i++) {
  1983. free_vlc(&s->dc_vlc[i]);
  1984. free_vlc(&s->ac_vlc_1[i]);
  1985. free_vlc(&s->ac_vlc_2[i]);
  1986. free_vlc(&s->ac_vlc_3[i]);
  1987. free_vlc(&s->ac_vlc_4[i]);
  1988. }
  1989. free_vlc(&s->superblock_run_length_vlc);
  1990. free_vlc(&s->fragment_run_length_vlc);
  1991. free_vlc(&s->mode_code_vlc);
  1992. free_vlc(&s->motion_vector_vlc);
  1993. /* release all frames */
  1994. if (s->golden_frame.data[0] && s->golden_frame.data[0] != s->last_frame.data[0])
  1995. avctx->release_buffer(avctx, &s->golden_frame);
  1996. if (s->last_frame.data[0])
  1997. avctx->release_buffer(avctx, &s->last_frame);
  1998. /* no need to release the current_frame since it will always be pointing
  1999. * to the same frame as either the golden or last frame */
  2000. return 0;
  2001. }
  2002. static int read_huffman_tree(AVCodecContext *avctx, GetBitContext *gb)
  2003. {
  2004. Vp3DecodeContext *s = avctx->priv_data;
  2005. if (get_bits1(gb)) {
  2006. int token;
  2007. if (s->entries >= 32) { /* overflow */
  2008. av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
  2009. return -1;
  2010. }
  2011. token = get_bits(gb, 5);
  2012. //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);
  2013. s->huffman_table[s->hti][token][0] = s->hbits;
  2014. s->huffman_table[s->hti][token][1] = s->huff_code_size;
  2015. s->entries++;
  2016. }
  2017. else {
  2018. if (s->huff_code_size >= 32) {/* overflow */
  2019. av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
  2020. return -1;
  2021. }
  2022. s->huff_code_size++;
  2023. s->hbits <<= 1;
  2024. read_huffman_tree(avctx, gb);
  2025. s->hbits |= 1;
  2026. read_huffman_tree(avctx, gb);
  2027. s->hbits >>= 1;
  2028. s->huff_code_size--;
  2029. }
  2030. return 0;
  2031. }
  2032. #ifdef CONFIG_THEORA_DECODER
  2033. static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb)
  2034. {
  2035. Vp3DecodeContext *s = avctx->priv_data;
  2036. int visible_width, visible_height;
  2037. s->theora = get_bits_long(gb, 24);
  2038. av_log(avctx, AV_LOG_DEBUG, "Theora bitstream version %X\n", s->theora);
  2039. /* 3.2.0 aka alpha3 has the same frame orientation as original vp3 */
  2040. /* but previous versions have the image flipped relative to vp3 */
  2041. if (s->theora < 0x030200)
  2042. {
  2043. s->flipped_image = 1;
  2044. av_log(avctx, AV_LOG_DEBUG, "Old (<alpha3) Theora bitstream, flipped image\n");
  2045. }
  2046. s->width = get_bits(gb, 16) << 4;
  2047. s->height = get_bits(gb, 16) << 4;
  2048. if(avcodec_check_dimensions(avctx, s->width, s->height)){
  2049. av_log(avctx, AV_LOG_ERROR, "Invalid dimensions (%dx%d)\n", s->width, s->height);
  2050. s->width= s->height= 0;
  2051. return -1;
  2052. }
  2053. if (s->theora >= 0x030400)
  2054. {
  2055. skip_bits(gb, 32); /* total number of superblocks in a frame */
  2056. // fixme, the next field is 36bits long
  2057. skip_bits(gb, 32); /* total number of blocks in a frame */
  2058. skip_bits(gb, 4); /* total number of blocks in a frame */
  2059. skip_bits(gb, 32); /* total number of macroblocks in a frame */
  2060. }
  2061. visible_width = get_bits_long(gb, 24);
  2062. visible_height = get_bits_long(gb, 24);
  2063. if (s->theora >= 0x030200) {
  2064. skip_bits(gb, 8); /* offset x */
  2065. skip_bits(gb, 8); /* offset y */
  2066. }
  2067. skip_bits(gb, 32); /* fps numerator */
  2068. skip_bits(gb, 32); /* fps denumerator */
  2069. skip_bits(gb, 24); /* aspect numerator */
  2070. skip_bits(gb, 24); /* aspect denumerator */
  2071. if (s->theora < 0x030200)
  2072. skip_bits(gb, 5); /* keyframe frequency force */
  2073. skip_bits(gb, 8); /* colorspace */
  2074. if (s->theora >= 0x030400)
  2075. skip_bits(gb, 2); /* pixel format: 420,res,422,444 */
  2076. skip_bits(gb, 24); /* bitrate */
  2077. skip_bits(gb, 6); /* quality hint */
  2078. if (s->theora >= 0x030200)
  2079. {
  2080. skip_bits(gb, 5); /* keyframe frequency force */
  2081. if (s->theora < 0x030400)
  2082. skip_bits(gb, 5); /* spare bits */
  2083. }
  2084. // align_get_bits(gb);
  2085. if ( visible_width <= s->width && visible_width > s->width-16
  2086. && visible_height <= s->height && visible_height > s->height-16)
  2087. avcodec_set_dimensions(avctx, visible_width, visible_height);
  2088. else
  2089. avcodec_set_dimensions(avctx, s->width, s->height);
  2090. return 0;
  2091. }
  2092. static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb)
  2093. {
  2094. Vp3DecodeContext *s = avctx->priv_data;
  2095. int i, n, matrices, inter, plane;
  2096. if (s->theora >= 0x030200) {
  2097. n = get_bits(gb, 3);
  2098. /* loop filter limit values table */
  2099. for (i = 0; i < 64; i++)
  2100. s->filter_limit_values[i] = get_bits(gb, n);
  2101. }
  2102. if (s->theora >= 0x030200)
  2103. n = get_bits(gb, 4) + 1;
  2104. else
  2105. n = 16;
  2106. /* quality threshold table */
  2107. for (i = 0; i < 64; i++)
  2108. s->coded_ac_scale_factor[i] = get_bits(gb, n);
  2109. if (s->theora >= 0x030200)
  2110. n = get_bits(gb, 4) + 1;
  2111. else
  2112. n = 16;
  2113. /* dc scale factor table */
  2114. for (i = 0; i < 64; i++)
  2115. s->coded_dc_scale_factor[i] = get_bits(gb, n);
  2116. if (s->theora >= 0x030200)
  2117. matrices = get_bits(gb, 9) + 1;
  2118. else
  2119. matrices = 3;
  2120. if(matrices > 384){
  2121. av_log(avctx, AV_LOG_ERROR, "invalid number of base matrixes\n");
  2122. return -1;
  2123. }
  2124. for(n=0; n<matrices; n++){
  2125. for (i = 0; i < 64; i++)
  2126. s->base_matrix[n][i]= get_bits(gb, 8);
  2127. }
  2128. for (inter = 0; inter <= 1; inter++) {
  2129. for (plane = 0; plane <= 2; plane++) {
  2130. int newqr= 1;
  2131. if (inter || plane > 0)
  2132. newqr = get_bits1(gb);
  2133. if (!newqr) {
  2134. int qtj, plj;
  2135. if(inter && get_bits1(gb)){
  2136. qtj = 0;
  2137. plj = plane;
  2138. }else{
  2139. qtj= (3*inter + plane - 1) / 3;
  2140. plj= (plane + 2) % 3;
  2141. }
  2142. s->qr_count[inter][plane]= s->qr_count[qtj][plj];
  2143. memcpy(s->qr_size[inter][plane], s->qr_size[qtj][plj], sizeof(s->qr_size[0][0]));
  2144. memcpy(s->qr_base[inter][plane], s->qr_base[qtj][plj], sizeof(s->qr_base[0][0]));
  2145. } else {
  2146. int qri= 0;
  2147. int qi = 0;
  2148. for(;;){
  2149. i= get_bits(gb, av_log2(matrices-1)+1);
  2150. if(i>= matrices){
  2151. av_log(avctx, AV_LOG_ERROR, "invalid base matrix index\n");
  2152. return -1;
  2153. }
  2154. s->qr_base[inter][plane][qri]= i;
  2155. if(qi >= 63)
  2156. break;
  2157. i = get_bits(gb, av_log2(63-qi)+1) + 1;
  2158. s->qr_size[inter][plane][qri++]= i;
  2159. qi += i;
  2160. }
  2161. if (qi > 63) {
  2162. av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi);
  2163. return -1;
  2164. }
  2165. s->qr_count[inter][plane]= qri;
  2166. }
  2167. }
  2168. }
  2169. /* Huffman tables */
  2170. for (s->hti = 0; s->hti < 80; s->hti++) {
  2171. s->entries = 0;
  2172. s->huff_code_size = 1;
  2173. if (!get_bits1(gb)) {
  2174. s->hbits = 0;
  2175. read_huffman_tree(avctx, gb);
  2176. s->hbits = 1;
  2177. read_huffman_tree(avctx, gb);
  2178. }
  2179. }
  2180. s->theora_tables = 1;
  2181. return 0;
  2182. }
  2183. static int theora_decode_init(AVCodecContext *avctx)
  2184. {
  2185. Vp3DecodeContext *s = avctx->priv_data;
  2186. GetBitContext gb;
  2187. int ptype;
  2188. uint8_t *header_start[3];
  2189. int header_len[3];
  2190. int i;
  2191. s->theora = 1;
  2192. if (!avctx->extradata_size)
  2193. {
  2194. av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n");
  2195. return -1;
  2196. }
  2197. if (ff_split_xiph_headers(avctx->extradata, avctx->extradata_size,
  2198. 42, header_start, header_len) < 0) {
  2199. av_log(avctx, AV_LOG_ERROR, "Corrupt extradata\n");
  2200. return -1;
  2201. }
  2202. for(i=0;i<3;i++) {
  2203. init_get_bits(&gb, header_start[i], header_len[i]);
  2204. ptype = get_bits(&gb, 8);
  2205. debug_vp3("Theora headerpacket type: %x\n", ptype);
  2206. if (!(ptype & 0x80))
  2207. {
  2208. av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n");
  2209. // return -1;
  2210. }
  2211. // FIXME: Check for this as well.
  2212. skip_bits(&gb, 6*8); /* "theora" */
  2213. switch(ptype)
  2214. {
  2215. case 0x80:
  2216. theora_decode_header(avctx, &gb);
  2217. break;
  2218. case 0x81:
  2219. // FIXME: is this needed? it breaks sometimes
  2220. // theora_decode_comments(avctx, gb);
  2221. break;
  2222. case 0x82:
  2223. theora_decode_tables(avctx, &gb);
  2224. break;
  2225. default:
  2226. av_log(avctx, AV_LOG_ERROR, "Unknown Theora config packet: %d\n", ptype&~0x80);
  2227. break;
  2228. }
  2229. if(8*header_len[i] != get_bits_count(&gb))
  2230. av_log(avctx, AV_LOG_ERROR, "%d bits left in packet %X\n", 8*header_len[i] - get_bits_count(&gb), ptype);
  2231. if (s->theora < 0x030200)
  2232. break;
  2233. }
  2234. vp3_decode_init(avctx);
  2235. return 0;
  2236. }
  2237. AVCodec theora_decoder = {
  2238. "theora",
  2239. CODEC_TYPE_VIDEO,
  2240. CODEC_ID_THEORA,
  2241. sizeof(Vp3DecodeContext),
  2242. theora_decode_init,
  2243. NULL,
  2244. vp3_decode_end,
  2245. vp3_decode_frame,
  2246. 0,
  2247. NULL
  2248. };
  2249. #endif
  2250. AVCodec vp3_decoder = {
  2251. "vp3",
  2252. CODEC_TYPE_VIDEO,
  2253. CODEC_ID_VP3,
  2254. sizeof(Vp3DecodeContext),
  2255. vp3_decode_init,
  2256. NULL,
  2257. vp3_decode_end,
  2258. vp3_decode_frame,
  2259. 0,
  2260. NULL
  2261. };