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