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.

2961 lines
104KB

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