You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2653 lines
95KB

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