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.

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