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.

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