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.

2658 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. #define FRAGMENT_PIXELS 8
  41. /*
  42. * Debugging Variables
  43. *
  44. * Define one or more of the following compile-time variables to 1 to obtain
  45. * elaborate information about certain aspects of the decoding process.
  46. *
  47. * KEYFRAMES_ONLY: set this to 1 to only see keyframes (VP3 slideshow mode)
  48. * DEBUG_VP3: high-level decoding flow
  49. * DEBUG_INIT: initialization parameters
  50. * DEBUG_DEQUANTIZERS: display how the dequanization tables are built
  51. * DEBUG_BLOCK_CODING: unpacking the superblock/macroblock/fragment coding
  52. * DEBUG_MODES: unpacking the coding modes for individual fragments
  53. * DEBUG_VECTORS: display the motion vectors
  54. * DEBUG_TOKEN: display exhaustive information about each DCT token
  55. * DEBUG_VLC: display the VLCs as they are extracted from the stream
  56. * DEBUG_DC_PRED: display the process of reversing DC prediction
  57. * DEBUG_IDCT: show every detail of the IDCT process
  58. */
  59. #define KEYFRAMES_ONLY 0
  60. #define DEBUG_VP3 0
  61. #define DEBUG_INIT 0
  62. #define DEBUG_DEQUANTIZERS 0
  63. #define DEBUG_BLOCK_CODING 0
  64. #define DEBUG_MODES 0
  65. #define DEBUG_VECTORS 0
  66. #define DEBUG_TOKEN 0
  67. #define DEBUG_VLC 0
  68. #define DEBUG_DC_PRED 0
  69. #define DEBUG_IDCT 0
  70. #if DEBUG_VP3
  71. #define debug_vp3(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
  72. #else
  73. static inline void debug_vp3(const char *format, ...) { }
  74. #endif
  75. #if DEBUG_INIT
  76. #define debug_init(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
  77. #else
  78. static inline void debug_init(const char *format, ...) { }
  79. #endif
  80. #if DEBUG_DEQUANTIZERS
  81. #define debug_dequantizers(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
  82. #else
  83. static inline void debug_dequantizers(const char *format, ...) { }
  84. #endif
  85. #if DEBUG_BLOCK_CODING
  86. #define debug_block_coding(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
  87. #else
  88. static inline void debug_block_coding(const char *format, ...) { }
  89. #endif
  90. #if DEBUG_MODES
  91. #define debug_modes(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
  92. #else
  93. static inline void debug_modes(const char *format, ...) { }
  94. #endif
  95. #if DEBUG_VECTORS
  96. #define debug_vectors(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
  97. #else
  98. static inline void debug_vectors(const char *format, ...) { }
  99. #endif
  100. #if DEBUG_TOKEN
  101. #define debug_token(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
  102. #else
  103. static inline void debug_token(const char *format, ...) { }
  104. #endif
  105. #if DEBUG_VLC
  106. #define debug_vlc(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
  107. #else
  108. static inline void debug_vlc(const char *format, ...) { }
  109. #endif
  110. #if DEBUG_DC_PRED
  111. #define debug_dc_pred(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
  112. #else
  113. static inline void debug_dc_pred(const char *format, ...) { }
  114. #endif
  115. #if DEBUG_IDCT
  116. #define debug_idct(args...) av_log(NULL, AV_LOG_DEBUG, ## args)
  117. #else
  118. static inline void debug_idct(const char *format, ...) { }
  119. #endif
  120. typedef struct Coeff {
  121. struct Coeff *next;
  122. DCTELEM coeff;
  123. uint8_t index;
  124. } Coeff;
  125. //FIXME split things out into their own arrays
  126. typedef struct Vp3Fragment {
  127. Coeff *next_coeff;
  128. /* address of first pixel taking into account which plane the fragment
  129. * lives on as well as the plane stride */
  130. int first_pixel;
  131. /* this is the macroblock that the fragment belongs to */
  132. uint16_t macroblock;
  133. uint8_t coding_method;
  134. uint8_t coeff_count;
  135. int8_t motion_x;
  136. int8_t motion_y;
  137. } Vp3Fragment;
  138. #define SB_NOT_CODED 0
  139. #define SB_PARTIALLY_CODED 1
  140. #define SB_FULLY_CODED 2
  141. #define MODE_INTER_NO_MV 0
  142. #define MODE_INTRA 1
  143. #define MODE_INTER_PLUS_MV 2
  144. #define MODE_INTER_LAST_MV 3
  145. #define MODE_INTER_PRIOR_LAST 4
  146. #define MODE_USING_GOLDEN 5
  147. #define MODE_GOLDEN_MV 6
  148. #define MODE_INTER_FOURMV 7
  149. #define CODING_MODE_COUNT 8
  150. /* special internal mode */
  151. #define MODE_COPY 8
  152. /* There are 6 preset schemes, plus a free-form scheme */
  153. static int ModeAlphabet[7][CODING_MODE_COUNT] =
  154. {
  155. /* this is the custom scheme */
  156. { 0, 0, 0, 0, 0, 0, 0, 0 },
  157. /* scheme 1: Last motion vector dominates */
  158. { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
  159. MODE_INTER_PLUS_MV, MODE_INTER_NO_MV,
  160. MODE_INTRA, MODE_USING_GOLDEN,
  161. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  162. /* scheme 2 */
  163. { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
  164. MODE_INTER_NO_MV, MODE_INTER_PLUS_MV,
  165. MODE_INTRA, MODE_USING_GOLDEN,
  166. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  167. /* scheme 3 */
  168. { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
  169. MODE_INTER_PRIOR_LAST, MODE_INTER_NO_MV,
  170. MODE_INTRA, MODE_USING_GOLDEN,
  171. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  172. /* scheme 4 */
  173. { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
  174. MODE_INTER_NO_MV, MODE_INTER_PRIOR_LAST,
  175. MODE_INTRA, MODE_USING_GOLDEN,
  176. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  177. /* scheme 5: No motion vector dominates */
  178. { MODE_INTER_NO_MV, MODE_INTER_LAST_MV,
  179. MODE_INTER_PRIOR_LAST, MODE_INTER_PLUS_MV,
  180. MODE_INTRA, MODE_USING_GOLDEN,
  181. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  182. /* scheme 6 */
  183. { MODE_INTER_NO_MV, MODE_USING_GOLDEN,
  184. MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
  185. MODE_INTER_PLUS_MV, MODE_INTRA,
  186. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  187. };
  188. #define MIN_DEQUANT_VAL 2
  189. typedef struct Vp3DecodeContext {
  190. AVCodecContext *avctx;
  191. int theora, theora_tables;
  192. int version;
  193. int width, height;
  194. AVFrame golden_frame;
  195. AVFrame last_frame;
  196. AVFrame current_frame;
  197. int keyframe;
  198. DSPContext dsp;
  199. int flipped_image;
  200. int qis[3];
  201. int nqis;
  202. int quality_index;
  203. int last_quality_index;
  204. int superblock_count;
  205. int superblock_width;
  206. int superblock_height;
  207. int y_superblock_width;
  208. int y_superblock_height;
  209. int c_superblock_width;
  210. int c_superblock_height;
  211. int u_superblock_start;
  212. int v_superblock_start;
  213. unsigned char *superblock_coding;
  214. int macroblock_count;
  215. int macroblock_width;
  216. int macroblock_height;
  217. int fragment_count;
  218. int fragment_width;
  219. int fragment_height;
  220. Vp3Fragment *all_fragments;
  221. Coeff *coeffs;
  222. Coeff *next_coeff;
  223. int fragment_start[3];
  224. ScanTable scantable;
  225. /* tables */
  226. uint16_t coded_dc_scale_factor[64];
  227. uint32_t coded_ac_scale_factor[64];
  228. uint8_t base_matrix[384][64];
  229. uint8_t qr_count[2][3];
  230. uint8_t qr_size [2][3][64];
  231. uint16_t qr_base[2][3][64];
  232. /* this is a list of indices into the all_fragments array indicating
  233. * which of the fragments are coded */
  234. int *coded_fragment_list;
  235. int coded_fragment_list_index;
  236. int pixel_addresses_inited;
  237. VLC dc_vlc[16];
  238. VLC ac_vlc_1[16];
  239. VLC ac_vlc_2[16];
  240. VLC ac_vlc_3[16];
  241. VLC ac_vlc_4[16];
  242. VLC superblock_run_length_vlc;
  243. VLC fragment_run_length_vlc;
  244. VLC mode_code_vlc;
  245. VLC motion_vector_vlc;
  246. /* these arrays need to be on 16-byte boundaries since SSE2 operations
  247. * index into them */
  248. DECLARE_ALIGNED_16(int16_t, qmat[2][4][64]); //<qmat[is_inter][plane]
  249. /* This table contains superblock_count * 16 entries. Each set of 16
  250. * numbers corresponds to the fragment indices 0..15 of the superblock.
  251. * An entry will be -1 to indicate that no entry corresponds to that
  252. * index. */
  253. int *superblock_fragments;
  254. /* This table contains superblock_count * 4 entries. Each set of 4
  255. * numbers corresponds to the macroblock indices 0..3 of the superblock.
  256. * An entry will be -1 to indicate that no entry corresponds to that
  257. * index. */
  258. int *superblock_macroblocks;
  259. /* This table contains macroblock_count * 6 entries. Each set of 6
  260. * numbers corresponds to the fragment indices 0..5 which comprise
  261. * the macroblock (4 Y fragments and 2 C fragments). */
  262. int *macroblock_fragments;
  263. /* This is an array that indicates how a particular macroblock
  264. * is coded. */
  265. unsigned char *macroblock_coding;
  266. int first_coded_y_fragment;
  267. int first_coded_c_fragment;
  268. int last_coded_y_fragment;
  269. int last_coded_c_fragment;
  270. uint8_t edge_emu_buffer[9*2048]; //FIXME dynamic alloc
  271. int8_t qscale_table[2048]; //FIXME dynamic alloc (width+15)/16
  272. /* Huffman decode */
  273. int hti;
  274. unsigned int hbits;
  275. int entries;
  276. int huff_code_size;
  277. uint16_t huffman_table[80][32][2];
  278. uint32_t filter_limit_values[64];
  279. int bounding_values_array[256];
  280. } Vp3DecodeContext;
  281. static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb);
  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]= 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] = clip_uint8(first_pixel[-1] + filter_value);
  1472. first_pixel[ 0] = 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] = clip_uint8(first_pixel[nstride] + filter_value);
  1487. first_pixel[0] = 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. avctx->has_b_frames = 0;
  1671. if(avctx->idct_algo==FF_IDCT_AUTO)
  1672. avctx->idct_algo=FF_IDCT_VP3;
  1673. dsputil_init(&s->dsp, avctx);
  1674. ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
  1675. /* initialize to an impossible value which will force a recalculation
  1676. * in the first frame decode */
  1677. s->quality_index = -1;
  1678. s->y_superblock_width = (s->width + 31) / 32;
  1679. s->y_superblock_height = (s->height + 31) / 32;
  1680. y_superblock_count = s->y_superblock_width * s->y_superblock_height;
  1681. /* work out the dimensions for the C planes */
  1682. c_width = s->width / 2;
  1683. c_height = s->height / 2;
  1684. s->c_superblock_width = (c_width + 31) / 32;
  1685. s->c_superblock_height = (c_height + 31) / 32;
  1686. c_superblock_count = s->c_superblock_width * s->c_superblock_height;
  1687. s->superblock_count = y_superblock_count + (c_superblock_count * 2);
  1688. s->u_superblock_start = y_superblock_count;
  1689. s->v_superblock_start = s->u_superblock_start + c_superblock_count;
  1690. s->superblock_coding = av_malloc(s->superblock_count);
  1691. s->macroblock_width = (s->width + 15) / 16;
  1692. s->macroblock_height = (s->height + 15) / 16;
  1693. s->macroblock_count = s->macroblock_width * s->macroblock_height;
  1694. s->fragment_width = s->width / FRAGMENT_PIXELS;
  1695. s->fragment_height = s->height / FRAGMENT_PIXELS;
  1696. /* fragment count covers all 8x8 blocks for all 3 planes */
  1697. s->fragment_count = s->fragment_width * s->fragment_height * 3 / 2;
  1698. s->fragment_start[1] = s->fragment_width * s->fragment_height;
  1699. s->fragment_start[2] = s->fragment_width * s->fragment_height * 5 / 4;
  1700. debug_init(" Y plane: %d x %d\n", s->width, s->height);
  1701. debug_init(" C plane: %d x %d\n", c_width, c_height);
  1702. debug_init(" Y superblocks: %d x %d, %d total\n",
  1703. s->y_superblock_width, s->y_superblock_height, y_superblock_count);
  1704. debug_init(" C superblocks: %d x %d, %d total\n",
  1705. s->c_superblock_width, s->c_superblock_height, c_superblock_count);
  1706. debug_init(" total superblocks = %d, U starts @ %d, V starts @ %d\n",
  1707. s->superblock_count, s->u_superblock_start, s->v_superblock_start);
  1708. debug_init(" macroblocks: %d x %d, %d total\n",
  1709. s->macroblock_width, s->macroblock_height, s->macroblock_count);
  1710. debug_init(" %d fragments, %d x %d, u starts @ %d, v starts @ %d\n",
  1711. s->fragment_count,
  1712. s->fragment_width,
  1713. s->fragment_height,
  1714. s->fragment_start[1],
  1715. s->fragment_start[2]);
  1716. s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment));
  1717. s->coeffs = av_malloc(s->fragment_count * sizeof(Coeff) * 65);
  1718. s->coded_fragment_list = av_malloc(s->fragment_count * sizeof(int));
  1719. s->pixel_addresses_inited = 0;
  1720. if (!s->theora_tables)
  1721. {
  1722. for (i = 0; i < 64; i++) {
  1723. s->coded_dc_scale_factor[i] = vp31_dc_scale_factor[i];
  1724. s->coded_ac_scale_factor[i] = vp31_ac_scale_factor[i];
  1725. s->base_matrix[0][i] = vp31_intra_y_dequant[i];
  1726. s->base_matrix[1][i] = vp31_intra_c_dequant[i];
  1727. s->base_matrix[2][i] = vp31_inter_dequant[i];
  1728. s->filter_limit_values[i] = vp31_filter_limit_values[i];
  1729. }
  1730. for(inter=0; inter<2; inter++){
  1731. for(plane=0; plane<3; plane++){
  1732. s->qr_count[inter][plane]= 1;
  1733. s->qr_size [inter][plane][0]= 63;
  1734. s->qr_base [inter][plane][0]=
  1735. s->qr_base [inter][plane][1]= 2*inter + (!!plane)*!inter;
  1736. }
  1737. }
  1738. /* init VLC tables */
  1739. for (i = 0; i < 16; i++) {
  1740. /* DC histograms */
  1741. init_vlc(&s->dc_vlc[i], 5, 32,
  1742. &dc_bias[i][0][1], 4, 2,
  1743. &dc_bias[i][0][0], 4, 2, 0);
  1744. /* group 1 AC histograms */
  1745. init_vlc(&s->ac_vlc_1[i], 5, 32,
  1746. &ac_bias_0[i][0][1], 4, 2,
  1747. &ac_bias_0[i][0][0], 4, 2, 0);
  1748. /* group 2 AC histograms */
  1749. init_vlc(&s->ac_vlc_2[i], 5, 32,
  1750. &ac_bias_1[i][0][1], 4, 2,
  1751. &ac_bias_1[i][0][0], 4, 2, 0);
  1752. /* group 3 AC histograms */
  1753. init_vlc(&s->ac_vlc_3[i], 5, 32,
  1754. &ac_bias_2[i][0][1], 4, 2,
  1755. &ac_bias_2[i][0][0], 4, 2, 0);
  1756. /* group 4 AC histograms */
  1757. init_vlc(&s->ac_vlc_4[i], 5, 32,
  1758. &ac_bias_3[i][0][1], 4, 2,
  1759. &ac_bias_3[i][0][0], 4, 2, 0);
  1760. }
  1761. } else {
  1762. for (i = 0; i < 16; i++) {
  1763. /* DC histograms */
  1764. init_vlc(&s->dc_vlc[i], 5, 32,
  1765. &s->huffman_table[i][0][1], 4, 2,
  1766. &s->huffman_table[i][0][0], 4, 2, 0);
  1767. /* group 1 AC histograms */
  1768. init_vlc(&s->ac_vlc_1[i], 5, 32,
  1769. &s->huffman_table[i+16][0][1], 4, 2,
  1770. &s->huffman_table[i+16][0][0], 4, 2, 0);
  1771. /* group 2 AC histograms */
  1772. init_vlc(&s->ac_vlc_2[i], 5, 32,
  1773. &s->huffman_table[i+16*2][0][1], 4, 2,
  1774. &s->huffman_table[i+16*2][0][0], 4, 2, 0);
  1775. /* group 3 AC histograms */
  1776. init_vlc(&s->ac_vlc_3[i], 5, 32,
  1777. &s->huffman_table[i+16*3][0][1], 4, 2,
  1778. &s->huffman_table[i+16*3][0][0], 4, 2, 0);
  1779. /* group 4 AC histograms */
  1780. init_vlc(&s->ac_vlc_4[i], 5, 32,
  1781. &s->huffman_table[i+16*4][0][1], 4, 2,
  1782. &s->huffman_table[i+16*4][0][0], 4, 2, 0);
  1783. }
  1784. }
  1785. init_vlc(&s->superblock_run_length_vlc, 6, 34,
  1786. &superblock_run_length_vlc_table[0][1], 4, 2,
  1787. &superblock_run_length_vlc_table[0][0], 4, 2, 0);
  1788. init_vlc(&s->fragment_run_length_vlc, 5, 30,
  1789. &fragment_run_length_vlc_table[0][1], 4, 2,
  1790. &fragment_run_length_vlc_table[0][0], 4, 2, 0);
  1791. init_vlc(&s->mode_code_vlc, 3, 8,
  1792. &mode_code_vlc_table[0][1], 2, 1,
  1793. &mode_code_vlc_table[0][0], 2, 1, 0);
  1794. init_vlc(&s->motion_vector_vlc, 6, 63,
  1795. &motion_vector_vlc_table[0][1], 2, 1,
  1796. &motion_vector_vlc_table[0][0], 2, 1, 0);
  1797. /* work out the block mapping tables */
  1798. s->superblock_fragments = av_malloc(s->superblock_count * 16 * sizeof(int));
  1799. s->superblock_macroblocks = av_malloc(s->superblock_count * 4 * sizeof(int));
  1800. s->macroblock_fragments = av_malloc(s->macroblock_count * 6 * sizeof(int));
  1801. s->macroblock_coding = av_malloc(s->macroblock_count + 1);
  1802. init_block_mapping(s);
  1803. for (i = 0; i < 3; i++) {
  1804. s->current_frame.data[i] = NULL;
  1805. s->last_frame.data[i] = NULL;
  1806. s->golden_frame.data[i] = NULL;
  1807. }
  1808. return 0;
  1809. }
  1810. /*
  1811. * This is the ffmpeg/libavcodec API frame decode function.
  1812. */
  1813. static int vp3_decode_frame(AVCodecContext *avctx,
  1814. void *data, int *data_size,
  1815. uint8_t *buf, int buf_size)
  1816. {
  1817. Vp3DecodeContext *s = avctx->priv_data;
  1818. GetBitContext gb;
  1819. static int counter = 0;
  1820. int i;
  1821. init_get_bits(&gb, buf, buf_size * 8);
  1822. if (s->theora && get_bits1(&gb))
  1823. {
  1824. #if 1
  1825. av_log(avctx, AV_LOG_ERROR, "Header packet passed to frame decoder, skipping\n");
  1826. return -1;
  1827. #else
  1828. int ptype = get_bits(&gb, 7);
  1829. skip_bits(&gb, 6*8); /* "theora" */
  1830. switch(ptype)
  1831. {
  1832. case 1:
  1833. theora_decode_comments(avctx, &gb);
  1834. break;
  1835. case 2:
  1836. theora_decode_tables(avctx, &gb);
  1837. init_dequantizer(s);
  1838. break;
  1839. default:
  1840. av_log(avctx, AV_LOG_ERROR, "Unknown Theora config packet: %d\n", ptype);
  1841. }
  1842. return buf_size;
  1843. #endif
  1844. }
  1845. s->keyframe = !get_bits1(&gb);
  1846. if (!s->theora)
  1847. skip_bits(&gb, 1);
  1848. s->last_quality_index = s->quality_index;
  1849. s->nqis=0;
  1850. do{
  1851. s->qis[s->nqis++]= get_bits(&gb, 6);
  1852. } while(s->theora >= 0x030200 && s->nqis<3 && get_bits1(&gb));
  1853. s->quality_index= s->qis[0];
  1854. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1855. av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n",
  1856. s->keyframe?"key":"", counter, s->quality_index);
  1857. counter++;
  1858. if (s->quality_index != s->last_quality_index) {
  1859. init_dequantizer(s);
  1860. init_loop_filter(s);
  1861. }
  1862. if (s->keyframe) {
  1863. if (!s->theora)
  1864. {
  1865. skip_bits(&gb, 4); /* width code */
  1866. skip_bits(&gb, 4); /* height code */
  1867. if (s->version)
  1868. {
  1869. s->version = get_bits(&gb, 5);
  1870. if (counter == 1)
  1871. av_log(s->avctx, AV_LOG_DEBUG, "VP version: %d\n", s->version);
  1872. }
  1873. }
  1874. if (s->version || s->theora)
  1875. {
  1876. if (get_bits1(&gb))
  1877. av_log(s->avctx, AV_LOG_ERROR, "Warning, unsupported keyframe coding type?!\n");
  1878. skip_bits(&gb, 2); /* reserved? */
  1879. }
  1880. if (s->last_frame.data[0] == s->golden_frame.data[0]) {
  1881. if (s->golden_frame.data[0])
  1882. avctx->release_buffer(avctx, &s->golden_frame);
  1883. s->last_frame= s->golden_frame; /* ensure that we catch any access to this released frame */
  1884. } else {
  1885. if (s->golden_frame.data[0])
  1886. avctx->release_buffer(avctx, &s->golden_frame);
  1887. if (s->last_frame.data[0])
  1888. avctx->release_buffer(avctx, &s->last_frame);
  1889. }
  1890. s->golden_frame.reference = 3;
  1891. if(avctx->get_buffer(avctx, &s->golden_frame) < 0) {
  1892. av_log(s->avctx, AV_LOG_ERROR, "vp3: get_buffer() failed\n");
  1893. return -1;
  1894. }
  1895. /* golden frame is also the current frame */
  1896. s->current_frame= s->golden_frame;
  1897. /* time to figure out pixel addresses? */
  1898. if (!s->pixel_addresses_inited)
  1899. {
  1900. if (!s->flipped_image)
  1901. vp3_calculate_pixel_addresses(s);
  1902. else
  1903. theora_calculate_pixel_addresses(s);
  1904. s->pixel_addresses_inited = 1;
  1905. }
  1906. } else {
  1907. /* allocate a new current frame */
  1908. s->current_frame.reference = 3;
  1909. if (!s->pixel_addresses_inited) {
  1910. av_log(s->avctx, AV_LOG_ERROR, "vp3: first frame not a keyframe\n");
  1911. return -1;
  1912. }
  1913. if(avctx->get_buffer(avctx, &s->current_frame) < 0) {
  1914. av_log(s->avctx, AV_LOG_ERROR, "vp3: get_buffer() failed\n");
  1915. return -1;
  1916. }
  1917. }
  1918. s->current_frame.qscale_table= s->qscale_table; //FIXME allocate individual tables per AVFrame
  1919. s->current_frame.qstride= 0;
  1920. {START_TIMER
  1921. init_frame(s, &gb);
  1922. STOP_TIMER("init_frame")}
  1923. #if KEYFRAMES_ONLY
  1924. if (!s->keyframe) {
  1925. memcpy(s->current_frame.data[0], s->golden_frame.data[0],
  1926. s->current_frame.linesize[0] * s->height);
  1927. memcpy(s->current_frame.data[1], s->golden_frame.data[1],
  1928. s->current_frame.linesize[1] * s->height / 2);
  1929. memcpy(s->current_frame.data[2], s->golden_frame.data[2],
  1930. s->current_frame.linesize[2] * s->height / 2);
  1931. } else {
  1932. #endif
  1933. {START_TIMER
  1934. if (unpack_superblocks(s, &gb)){
  1935. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n");
  1936. return -1;
  1937. }
  1938. STOP_TIMER("unpack_superblocks")}
  1939. {START_TIMER
  1940. if (unpack_modes(s, &gb)){
  1941. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n");
  1942. return -1;
  1943. }
  1944. STOP_TIMER("unpack_modes")}
  1945. {START_TIMER
  1946. if (unpack_vectors(s, &gb)){
  1947. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n");
  1948. return -1;
  1949. }
  1950. STOP_TIMER("unpack_vectors")}
  1951. {START_TIMER
  1952. if (unpack_dct_coeffs(s, &gb)){
  1953. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n");
  1954. return -1;
  1955. }
  1956. STOP_TIMER("unpack_dct_coeffs")}
  1957. {START_TIMER
  1958. reverse_dc_prediction(s, 0, s->fragment_width, s->fragment_height);
  1959. if ((avctx->flags & CODEC_FLAG_GRAY) == 0) {
  1960. reverse_dc_prediction(s, s->fragment_start[1],
  1961. s->fragment_width / 2, s->fragment_height / 2);
  1962. reverse_dc_prediction(s, s->fragment_start[2],
  1963. s->fragment_width / 2, s->fragment_height / 2);
  1964. }
  1965. STOP_TIMER("reverse_dc_prediction")}
  1966. {START_TIMER
  1967. for (i = 0; i < s->macroblock_height; i++)
  1968. render_slice(s, i);
  1969. STOP_TIMER("render_fragments")}
  1970. {START_TIMER
  1971. apply_loop_filter(s);
  1972. STOP_TIMER("apply_loop_filter")}
  1973. #if KEYFRAMES_ONLY
  1974. }
  1975. #endif
  1976. *data_size=sizeof(AVFrame);
  1977. *(AVFrame*)data= s->current_frame;
  1978. /* release the last frame, if it is allocated and if it is not the
  1979. * golden frame */
  1980. if ((s->last_frame.data[0]) &&
  1981. (s->last_frame.data[0] != s->golden_frame.data[0]))
  1982. avctx->release_buffer(avctx, &s->last_frame);
  1983. /* shuffle frames (last = current) */
  1984. s->last_frame= s->current_frame;
  1985. s->current_frame.data[0]= NULL; /* ensure that we catch any access to this released frame */
  1986. return buf_size;
  1987. }
  1988. /*
  1989. * This is the ffmpeg/libavcodec API module cleanup function.
  1990. */
  1991. static int vp3_decode_end(AVCodecContext *avctx)
  1992. {
  1993. Vp3DecodeContext *s = avctx->priv_data;
  1994. av_free(s->all_fragments);
  1995. av_free(s->coeffs);
  1996. av_free(s->coded_fragment_list);
  1997. av_free(s->superblock_fragments);
  1998. av_free(s->superblock_macroblocks);
  1999. av_free(s->macroblock_fragments);
  2000. av_free(s->macroblock_coding);
  2001. /* release all frames */
  2002. if (s->golden_frame.data[0] && s->golden_frame.data[0] != s->last_frame.data[0])
  2003. avctx->release_buffer(avctx, &s->golden_frame);
  2004. if (s->last_frame.data[0])
  2005. avctx->release_buffer(avctx, &s->last_frame);
  2006. /* no need to release the current_frame since it will always be pointing
  2007. * to the same frame as either the golden or last frame */
  2008. return 0;
  2009. }
  2010. static int read_huffman_tree(AVCodecContext *avctx, GetBitContext *gb)
  2011. {
  2012. Vp3DecodeContext *s = avctx->priv_data;
  2013. if (get_bits(gb, 1)) {
  2014. int token;
  2015. if (s->entries >= 32) { /* overflow */
  2016. av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
  2017. return -1;
  2018. }
  2019. token = get_bits(gb, 5);
  2020. //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);
  2021. s->huffman_table[s->hti][token][0] = s->hbits;
  2022. s->huffman_table[s->hti][token][1] = s->huff_code_size;
  2023. s->entries++;
  2024. }
  2025. else {
  2026. if (s->huff_code_size >= 32) {/* overflow */
  2027. av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
  2028. return -1;
  2029. }
  2030. s->huff_code_size++;
  2031. s->hbits <<= 1;
  2032. read_huffman_tree(avctx, gb);
  2033. s->hbits |= 1;
  2034. read_huffman_tree(avctx, gb);
  2035. s->hbits >>= 1;
  2036. s->huff_code_size--;
  2037. }
  2038. return 0;
  2039. }
  2040. static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb)
  2041. {
  2042. Vp3DecodeContext *s = avctx->priv_data;
  2043. s->theora = get_bits_long(gb, 24);
  2044. av_log(avctx, AV_LOG_INFO, "Theora bitstream version %X\n", s->theora);
  2045. /* 3.2.0 aka alpha3 has the same frame orientation as original vp3 */
  2046. /* but previous versions have the image flipped relative to vp3 */
  2047. if (s->theora < 0x030200)
  2048. {
  2049. s->flipped_image = 1;
  2050. av_log(avctx, AV_LOG_DEBUG, "Old (<alpha3) Theora bitstream, flipped image\n");
  2051. }
  2052. s->width = get_bits(gb, 16) << 4;
  2053. s->height = get_bits(gb, 16) << 4;
  2054. if(avcodec_check_dimensions(avctx, s->width, s->height)){
  2055. av_log(avctx, AV_LOG_ERROR, "Invalid dimensions (%dx%d)\n", s->width, s->height);
  2056. s->width= s->height= 0;
  2057. return -1;
  2058. }
  2059. if (s->theora >= 0x030400)
  2060. {
  2061. skip_bits(gb, 32); /* total number of superblocks in a frame */
  2062. // fixme, the next field is 36bits long
  2063. skip_bits(gb, 32); /* total number of blocks in a frame */
  2064. skip_bits(gb, 4); /* total number of blocks in a frame */
  2065. skip_bits(gb, 32); /* total number of macroblocks in a frame */
  2066. skip_bits(gb, 24); /* frame width */
  2067. skip_bits(gb, 24); /* frame height */
  2068. }
  2069. else
  2070. {
  2071. skip_bits(gb, 24); /* frame width */
  2072. skip_bits(gb, 24); /* frame height */
  2073. }
  2074. if (s->theora >= 0x030200) {
  2075. skip_bits(gb, 8); /* offset x */
  2076. skip_bits(gb, 8); /* offset y */
  2077. }
  2078. skip_bits(gb, 32); /* fps numerator */
  2079. skip_bits(gb, 32); /* fps denumerator */
  2080. skip_bits(gb, 24); /* aspect numerator */
  2081. skip_bits(gb, 24); /* aspect denumerator */
  2082. if (s->theora < 0x030200)
  2083. skip_bits(gb, 5); /* keyframe frequency force */
  2084. skip_bits(gb, 8); /* colorspace */
  2085. if (s->theora >= 0x030400)
  2086. skip_bits(gb, 2); /* pixel format: 420,res,422,444 */
  2087. skip_bits(gb, 24); /* bitrate */
  2088. skip_bits(gb, 6); /* quality hint */
  2089. if (s->theora >= 0x030200)
  2090. {
  2091. skip_bits(gb, 5); /* keyframe frequency force */
  2092. if (s->theora < 0x030400)
  2093. skip_bits(gb, 5); /* spare bits */
  2094. }
  2095. // align_get_bits(gb);
  2096. avctx->width = s->width;
  2097. avctx->height = s->height;
  2098. return 0;
  2099. }
  2100. static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb)
  2101. {
  2102. Vp3DecodeContext *s = avctx->priv_data;
  2103. int i, n, matrices, inter, plane;
  2104. if (s->theora >= 0x030200) {
  2105. n = get_bits(gb, 3);
  2106. /* loop filter limit values table */
  2107. for (i = 0; i < 64; i++)
  2108. s->filter_limit_values[i] = get_bits(gb, n);
  2109. }
  2110. if (s->theora >= 0x030200)
  2111. n = get_bits(gb, 4) + 1;
  2112. else
  2113. n = 16;
  2114. /* quality threshold table */
  2115. for (i = 0; i < 64; i++)
  2116. s->coded_ac_scale_factor[i] = get_bits(gb, n);
  2117. if (s->theora >= 0x030200)
  2118. n = get_bits(gb, 4) + 1;
  2119. else
  2120. n = 16;
  2121. /* dc scale factor table */
  2122. for (i = 0; i < 64; i++)
  2123. s->coded_dc_scale_factor[i] = get_bits(gb, n);
  2124. if (s->theora >= 0x030200)
  2125. matrices = get_bits(gb, 9) + 1;
  2126. else
  2127. matrices = 3;
  2128. if(matrices > 384){
  2129. av_log(avctx, AV_LOG_ERROR, "invalid number of base matrixes\n");
  2130. return -1;
  2131. }
  2132. for(n=0; n<matrices; n++){
  2133. for (i = 0; i < 64; i++)
  2134. s->base_matrix[n][i]= get_bits(gb, 8);
  2135. }
  2136. for (inter = 0; inter <= 1; inter++) {
  2137. for (plane = 0; plane <= 2; plane++) {
  2138. int newqr= 1;
  2139. if (inter || plane > 0)
  2140. newqr = get_bits(gb, 1);
  2141. if (!newqr) {
  2142. int qtj, plj;
  2143. if(inter && get_bits(gb, 1)){
  2144. qtj = 0;
  2145. plj = plane;
  2146. }else{
  2147. qtj= (3*inter + plane - 1) / 3;
  2148. plj= (plane + 2) % 3;
  2149. }
  2150. s->qr_count[inter][plane]= s->qr_count[qtj][plj];
  2151. memcpy(s->qr_size[inter][plane], s->qr_size[qtj][plj], sizeof(s->qr_size[0][0]));
  2152. memcpy(s->qr_base[inter][plane], s->qr_base[qtj][plj], sizeof(s->qr_base[0][0]));
  2153. } else {
  2154. int qri= 0;
  2155. int qi = 0;
  2156. for(;;){
  2157. i= get_bits(gb, av_log2(matrices-1)+1);
  2158. if(i>= matrices){
  2159. av_log(avctx, AV_LOG_ERROR, "invalid base matrix index\n");
  2160. return -1;
  2161. }
  2162. s->qr_base[inter][plane][qri]= i;
  2163. if(qi >= 63)
  2164. break;
  2165. i = get_bits(gb, av_log2(63-qi)+1) + 1;
  2166. s->qr_size[inter][plane][qri++]= i;
  2167. qi += i;
  2168. }
  2169. if (qi > 63) {
  2170. av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi);
  2171. return -1;
  2172. }
  2173. s->qr_count[inter][plane]= qri;
  2174. }
  2175. }
  2176. }
  2177. /* Huffman tables */
  2178. for (s->hti = 0; s->hti < 80; s->hti++) {
  2179. s->entries = 0;
  2180. s->huff_code_size = 1;
  2181. if (!get_bits(gb, 1)) {
  2182. s->hbits = 0;
  2183. read_huffman_tree(avctx, gb);
  2184. s->hbits = 1;
  2185. read_huffman_tree(avctx, gb);
  2186. }
  2187. }
  2188. s->theora_tables = 1;
  2189. return 0;
  2190. }
  2191. static int theora_decode_init(AVCodecContext *avctx)
  2192. {
  2193. Vp3DecodeContext *s = avctx->priv_data;
  2194. GetBitContext gb;
  2195. int ptype;
  2196. uint8_t *p= avctx->extradata;
  2197. int op_bytes, i;
  2198. s->theora = 1;
  2199. if (!avctx->extradata_size)
  2200. {
  2201. av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n");
  2202. return -1;
  2203. }
  2204. for(i=0;i<3;i++) {
  2205. op_bytes = *(p++)<<8;
  2206. op_bytes += *(p++);
  2207. init_get_bits(&gb, p, op_bytes);
  2208. p += op_bytes;
  2209. ptype = get_bits(&gb, 8);
  2210. debug_vp3("Theora headerpacket type: %x\n", ptype);
  2211. if (!(ptype & 0x80))
  2212. {
  2213. av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n");
  2214. // return -1;
  2215. }
  2216. // FIXME: check for this aswell
  2217. skip_bits(&gb, 6*8); /* "theora" */
  2218. switch(ptype)
  2219. {
  2220. case 0x80:
  2221. theora_decode_header(avctx, &gb);
  2222. break;
  2223. case 0x81:
  2224. // FIXME: is this needed? it breaks sometimes
  2225. // theora_decode_comments(avctx, gb);
  2226. break;
  2227. case 0x82:
  2228. theora_decode_tables(avctx, &gb);
  2229. break;
  2230. default:
  2231. av_log(avctx, AV_LOG_ERROR, "Unknown Theora config packet: %d\n", ptype&~0x80);
  2232. break;
  2233. }
  2234. if(8*op_bytes != get_bits_count(&gb))
  2235. av_log(avctx, AV_LOG_ERROR, "%d bits left in packet %X\n", 8*op_bytes - get_bits_count(&gb), ptype);
  2236. if (s->theora < 0x030200)
  2237. break;
  2238. }
  2239. vp3_decode_init(avctx);
  2240. return 0;
  2241. }
  2242. AVCodec vp3_decoder = {
  2243. "vp3",
  2244. CODEC_TYPE_VIDEO,
  2245. CODEC_ID_VP3,
  2246. sizeof(Vp3DecodeContext),
  2247. vp3_decode_init,
  2248. NULL,
  2249. vp3_decode_end,
  2250. vp3_decode_frame,
  2251. 0,
  2252. NULL
  2253. };
  2254. AVCodec theora_decoder = {
  2255. "theora",
  2256. CODEC_TYPE_VIDEO,
  2257. CODEC_ID_THEORA,
  2258. sizeof(Vp3DecodeContext),
  2259. theora_decode_init,
  2260. NULL,
  2261. vp3_decode_end,
  2262. vp3_decode_frame,
  2263. 0,
  2264. NULL
  2265. };