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.

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