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.

2920 lines
103KB

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