PageRenderTime 64ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/libavcodec/vp3.c

http://github.com/FFmpeg/FFmpeg
C | 3260 lines | 2458 code | 454 blank | 348 comment | 614 complexity | 503483eb2da5ccc94c391a2e91805d76 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, LGPL-2.1, LGPL-3.0, CC-BY-SA-3.0
  1. /*
  2. * Copyright (C) 2003-2004 The FFmpeg project
  3. * Copyright (C) 2019 Peter Ross
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * On2 VP3/VP4 Video Decoder
  24. *
  25. * VP3 Video Decoder by Mike Melanson (mike at multimedia.cx)
  26. * For more information about the VP3 coding process, visit:
  27. * http://wiki.multimedia.cx/index.php?title=On2_VP3
  28. *
  29. * Theora decoder by Alex Beregszaszi
  30. */
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include "libavutil/imgutils.h"
  35. #include "avcodec.h"
  36. #include "get_bits.h"
  37. #include "hpeldsp.h"
  38. #include "internal.h"
  39. #include "mathops.h"
  40. #include "thread.h"
  41. #include "videodsp.h"
  42. #include "vp3data.h"
  43. #include "vp4data.h"
  44. #include "vp3dsp.h"
  45. #include "xiph.h"
  46. #define FRAGMENT_PIXELS 8
  47. // FIXME split things out into their own arrays
  48. typedef struct Vp3Fragment {
  49. int16_t dc;
  50. uint8_t coding_method;
  51. uint8_t qpi;
  52. } Vp3Fragment;
  53. #define SB_NOT_CODED 0
  54. #define SB_PARTIALLY_CODED 1
  55. #define SB_FULLY_CODED 2
  56. // This is the maximum length of a single long bit run that can be encoded
  57. // for superblock coding or block qps. Theora special-cases this to read a
  58. // bit instead of flipping the current bit to allow for runs longer than 4129.
  59. #define MAXIMUM_LONG_BIT_RUN 4129
  60. #define MODE_INTER_NO_MV 0
  61. #define MODE_INTRA 1
  62. #define MODE_INTER_PLUS_MV 2
  63. #define MODE_INTER_LAST_MV 3
  64. #define MODE_INTER_PRIOR_LAST 4
  65. #define MODE_USING_GOLDEN 5
  66. #define MODE_GOLDEN_MV 6
  67. #define MODE_INTER_FOURMV 7
  68. #define CODING_MODE_COUNT 8
  69. /* special internal mode */
  70. #define MODE_COPY 8
  71. static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb);
  72. static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb);
  73. /* There are 6 preset schemes, plus a free-form scheme */
  74. static const int ModeAlphabet[6][CODING_MODE_COUNT] = {
  75. /* scheme 1: Last motion vector dominates */
  76. { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
  77. MODE_INTER_PLUS_MV, MODE_INTER_NO_MV,
  78. MODE_INTRA, MODE_USING_GOLDEN,
  79. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  80. /* scheme 2 */
  81. { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
  82. MODE_INTER_NO_MV, MODE_INTER_PLUS_MV,
  83. MODE_INTRA, MODE_USING_GOLDEN,
  84. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  85. /* scheme 3 */
  86. { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
  87. MODE_INTER_PRIOR_LAST, MODE_INTER_NO_MV,
  88. MODE_INTRA, MODE_USING_GOLDEN,
  89. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  90. /* scheme 4 */
  91. { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
  92. MODE_INTER_NO_MV, MODE_INTER_PRIOR_LAST,
  93. MODE_INTRA, MODE_USING_GOLDEN,
  94. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  95. /* scheme 5: No motion vector dominates */
  96. { MODE_INTER_NO_MV, MODE_INTER_LAST_MV,
  97. MODE_INTER_PRIOR_LAST, MODE_INTER_PLUS_MV,
  98. MODE_INTRA, MODE_USING_GOLDEN,
  99. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  100. /* scheme 6 */
  101. { MODE_INTER_NO_MV, MODE_USING_GOLDEN,
  102. MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
  103. MODE_INTER_PLUS_MV, MODE_INTRA,
  104. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  105. };
  106. static const uint8_t hilbert_offset[16][2] = {
  107. { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 },
  108. { 0, 2 }, { 0, 3 }, { 1, 3 }, { 1, 2 },
  109. { 2, 2 }, { 2, 3 }, { 3, 3 }, { 3, 2 },
  110. { 3, 1 }, { 2, 1 }, { 2, 0 }, { 3, 0 }
  111. };
  112. enum {
  113. VP4_DC_INTRA = 0,
  114. VP4_DC_INTER = 1,
  115. VP4_DC_GOLDEN = 2,
  116. NB_VP4_DC_TYPES,
  117. VP4_DC_UNDEFINED = NB_VP4_DC_TYPES
  118. };
  119. static const uint8_t vp4_pred_block_type_map[8] = {
  120. [MODE_INTER_NO_MV] = VP4_DC_INTER,
  121. [MODE_INTRA] = VP4_DC_INTRA,
  122. [MODE_INTER_PLUS_MV] = VP4_DC_INTER,
  123. [MODE_INTER_LAST_MV] = VP4_DC_INTER,
  124. [MODE_INTER_PRIOR_LAST] = VP4_DC_INTER,
  125. [MODE_USING_GOLDEN] = VP4_DC_GOLDEN,
  126. [MODE_GOLDEN_MV] = VP4_DC_GOLDEN,
  127. [MODE_INTER_FOURMV] = VP4_DC_INTER,
  128. };
  129. typedef struct {
  130. int dc;
  131. int type;
  132. } VP4Predictor;
  133. #define MIN_DEQUANT_VAL 2
  134. typedef struct Vp3DecodeContext {
  135. AVCodecContext *avctx;
  136. int theora, theora_tables, theora_header;
  137. int version;
  138. int width, height;
  139. int chroma_x_shift, chroma_y_shift;
  140. ThreadFrame golden_frame;
  141. ThreadFrame last_frame;
  142. ThreadFrame current_frame;
  143. int keyframe;
  144. uint8_t idct_permutation[64];
  145. uint8_t idct_scantable[64];
  146. HpelDSPContext hdsp;
  147. VideoDSPContext vdsp;
  148. VP3DSPContext vp3dsp;
  149. DECLARE_ALIGNED(16, int16_t, block)[64];
  150. int flipped_image;
  151. int last_slice_end;
  152. int skip_loop_filter;
  153. int qps[3];
  154. int nqps;
  155. int last_qps[3];
  156. int superblock_count;
  157. int y_superblock_width;
  158. int y_superblock_height;
  159. int y_superblock_count;
  160. int c_superblock_width;
  161. int c_superblock_height;
  162. int c_superblock_count;
  163. int u_superblock_start;
  164. int v_superblock_start;
  165. unsigned char *superblock_coding;
  166. int macroblock_count; /* y macroblock count */
  167. int macroblock_width;
  168. int macroblock_height;
  169. int c_macroblock_count;
  170. int c_macroblock_width;
  171. int c_macroblock_height;
  172. int yuv_macroblock_count; /* y+u+v macroblock count */
  173. int fragment_count;
  174. int fragment_width[2];
  175. int fragment_height[2];
  176. Vp3Fragment *all_fragments;
  177. int fragment_start[3];
  178. int data_offset[3];
  179. uint8_t offset_x;
  180. uint8_t offset_y;
  181. int offset_x_warned;
  182. int8_t (*motion_val[2])[2];
  183. /* tables */
  184. uint16_t coded_dc_scale_factor[2][64];
  185. uint32_t coded_ac_scale_factor[64];
  186. uint8_t base_matrix[384][64];
  187. uint8_t qr_count[2][3];
  188. uint8_t qr_size[2][3][64];
  189. uint16_t qr_base[2][3][64];
  190. /**
  191. * This is a list of all tokens in bitstream order. Reordering takes place
  192. * by pulling from each level during IDCT. As a consequence, IDCT must be
  193. * in Hilbert order, making the minimum slice height 64 for 4:2:0 and 32
  194. * otherwise. The 32 different tokens with up to 12 bits of extradata are
  195. * collapsed into 3 types, packed as follows:
  196. * (from the low to high bits)
  197. *
  198. * 2 bits: type (0,1,2)
  199. * 0: EOB run, 14 bits for run length (12 needed)
  200. * 1: zero run, 7 bits for run length
  201. * 7 bits for the next coefficient (3 needed)
  202. * 2: coefficient, 14 bits (11 needed)
  203. *
  204. * Coefficients are signed, so are packed in the highest bits for automatic
  205. * sign extension.
  206. */
  207. int16_t *dct_tokens[3][64];
  208. int16_t *dct_tokens_base;
  209. #define TOKEN_EOB(eob_run) ((eob_run) << 2)
  210. #define TOKEN_ZERO_RUN(coeff, zero_run) (((coeff) * 512) + ((zero_run) << 2) + 1)
  211. #define TOKEN_COEFF(coeff) (((coeff) * 4) + 2)
  212. /**
  213. * number of blocks that contain DCT coefficients at
  214. * the given level or higher
  215. */
  216. int num_coded_frags[3][64];
  217. int total_num_coded_frags;
  218. /* this is a list of indexes into the all_fragments array indicating
  219. * which of the fragments are coded */
  220. int *coded_fragment_list[3];
  221. int *kf_coded_fragment_list;
  222. int *nkf_coded_fragment_list;
  223. int num_kf_coded_fragment[3];
  224. VLC dc_vlc[16];
  225. VLC ac_vlc_1[16];
  226. VLC ac_vlc_2[16];
  227. VLC ac_vlc_3[16];
  228. VLC ac_vlc_4[16];
  229. VLC superblock_run_length_vlc; /* version < 2 */
  230. VLC fragment_run_length_vlc; /* version < 2 */
  231. VLC block_pattern_vlc[2]; /* version >= 2*/
  232. VLC mode_code_vlc;
  233. VLC motion_vector_vlc; /* version < 2 */
  234. VLC vp4_mv_vlc[2][7]; /* version >=2 */
  235. /* these arrays need to be on 16-byte boundaries since SSE2 operations
  236. * index into them */
  237. DECLARE_ALIGNED(16, int16_t, qmat)[3][2][3][64]; ///< qmat[qpi][is_inter][plane]
  238. /* This table contains superblock_count * 16 entries. Each set of 16
  239. * numbers corresponds to the fragment indexes 0..15 of the superblock.
  240. * An entry will be -1 to indicate that no entry corresponds to that
  241. * index. */
  242. int *superblock_fragments;
  243. /* This is an array that indicates how a particular macroblock
  244. * is coded. */
  245. unsigned char *macroblock_coding;
  246. uint8_t *edge_emu_buffer;
  247. /* Huffman decode */
  248. int hti;
  249. unsigned int hbits;
  250. int entries;
  251. int huff_code_size;
  252. uint32_t huffman_table[80][32][2];
  253. uint8_t filter_limit_values[64];
  254. DECLARE_ALIGNED(8, int, bounding_values_array)[256 + 2];
  255. VP4Predictor * dc_pred_row; /* dc_pred_row[y_superblock_width * 4] */
  256. } Vp3DecodeContext;
  257. /************************************************************************
  258. * VP3 specific functions
  259. ************************************************************************/
  260. static av_cold void free_tables(AVCodecContext *avctx)
  261. {
  262. Vp3DecodeContext *s = avctx->priv_data;
  263. av_freep(&s->superblock_coding);
  264. av_freep(&s->all_fragments);
  265. av_freep(&s->nkf_coded_fragment_list);
  266. av_freep(&s->kf_coded_fragment_list);
  267. av_freep(&s->dct_tokens_base);
  268. av_freep(&s->superblock_fragments);
  269. av_freep(&s->macroblock_coding);
  270. av_freep(&s->dc_pred_row);
  271. av_freep(&s->motion_val[0]);
  272. av_freep(&s->motion_val[1]);
  273. }
  274. static void vp3_decode_flush(AVCodecContext *avctx)
  275. {
  276. Vp3DecodeContext *s = avctx->priv_data;
  277. if (s->golden_frame.f)
  278. ff_thread_release_buffer(avctx, &s->golden_frame);
  279. if (s->last_frame.f)
  280. ff_thread_release_buffer(avctx, &s->last_frame);
  281. if (s->current_frame.f)
  282. ff_thread_release_buffer(avctx, &s->current_frame);
  283. }
  284. static av_cold int vp3_decode_end(AVCodecContext *avctx)
  285. {
  286. Vp3DecodeContext *s = avctx->priv_data;
  287. int i, j;
  288. free_tables(avctx);
  289. av_freep(&s->edge_emu_buffer);
  290. s->theora_tables = 0;
  291. /* release all frames */
  292. vp3_decode_flush(avctx);
  293. av_frame_free(&s->current_frame.f);
  294. av_frame_free(&s->last_frame.f);
  295. av_frame_free(&s->golden_frame.f);
  296. for (i = 0; i < 16; i++) {
  297. ff_free_vlc(&s->dc_vlc[i]);
  298. ff_free_vlc(&s->ac_vlc_1[i]);
  299. ff_free_vlc(&s->ac_vlc_2[i]);
  300. ff_free_vlc(&s->ac_vlc_3[i]);
  301. ff_free_vlc(&s->ac_vlc_4[i]);
  302. }
  303. ff_free_vlc(&s->superblock_run_length_vlc);
  304. ff_free_vlc(&s->fragment_run_length_vlc);
  305. ff_free_vlc(&s->mode_code_vlc);
  306. ff_free_vlc(&s->motion_vector_vlc);
  307. for (j = 0; j < 2; j++)
  308. for (i = 0; i < 7; i++)
  309. ff_free_vlc(&s->vp4_mv_vlc[j][i]);
  310. for (i = 0; i < 2; i++)
  311. ff_free_vlc(&s->block_pattern_vlc[i]);
  312. return 0;
  313. }
  314. /**
  315. * This function sets up all of the various blocks mappings:
  316. * superblocks <-> fragments, macroblocks <-> fragments,
  317. * superblocks <-> macroblocks
  318. *
  319. * @return 0 is successful; returns 1 if *anything* went wrong.
  320. */
  321. static int init_block_mapping(Vp3DecodeContext *s)
  322. {
  323. int sb_x, sb_y, plane;
  324. int x, y, i, j = 0;
  325. for (plane = 0; plane < 3; plane++) {
  326. int sb_width = plane ? s->c_superblock_width
  327. : s->y_superblock_width;
  328. int sb_height = plane ? s->c_superblock_height
  329. : s->y_superblock_height;
  330. int frag_width = s->fragment_width[!!plane];
  331. int frag_height = s->fragment_height[!!plane];
  332. for (sb_y = 0; sb_y < sb_height; sb_y++)
  333. for (sb_x = 0; sb_x < sb_width; sb_x++)
  334. for (i = 0; i < 16; i++) {
  335. x = 4 * sb_x + hilbert_offset[i][0];
  336. y = 4 * sb_y + hilbert_offset[i][1];
  337. if (x < frag_width && y < frag_height)
  338. s->superblock_fragments[j++] = s->fragment_start[plane] +
  339. y * frag_width + x;
  340. else
  341. s->superblock_fragments[j++] = -1;
  342. }
  343. }
  344. return 0; /* successful path out */
  345. }
  346. /*
  347. * This function sets up the dequantization tables used for a particular
  348. * frame.
  349. */
  350. static void init_dequantizer(Vp3DecodeContext *s, int qpi)
  351. {
  352. int ac_scale_factor = s->coded_ac_scale_factor[s->qps[qpi]];
  353. int i, plane, inter, qri, bmi, bmj, qistart;
  354. for (inter = 0; inter < 2; inter++) {
  355. for (plane = 0; plane < 3; plane++) {
  356. int dc_scale_factor = s->coded_dc_scale_factor[!!plane][s->qps[qpi]];
  357. int sum = 0;
  358. for (qri = 0; qri < s->qr_count[inter][plane]; qri++) {
  359. sum += s->qr_size[inter][plane][qri];
  360. if (s->qps[qpi] <= sum)
  361. break;
  362. }
  363. qistart = sum - s->qr_size[inter][plane][qri];
  364. bmi = s->qr_base[inter][plane][qri];
  365. bmj = s->qr_base[inter][plane][qri + 1];
  366. for (i = 0; i < 64; i++) {
  367. int coeff = (2 * (sum - s->qps[qpi]) * s->base_matrix[bmi][i] -
  368. 2 * (qistart - s->qps[qpi]) * s->base_matrix[bmj][i] +
  369. s->qr_size[inter][plane][qri]) /
  370. (2 * s->qr_size[inter][plane][qri]);
  371. int qmin = 8 << (inter + !i);
  372. int qscale = i ? ac_scale_factor : dc_scale_factor;
  373. int qbias = (1 + inter) * 3;
  374. s->qmat[qpi][inter][plane][s->idct_permutation[i]] =
  375. (i == 0 || s->version < 2) ? av_clip((qscale * coeff) / 100 * 4, qmin, 4096)
  376. : (qscale * (coeff - qbias) / 100 + qbias) * 4;
  377. }
  378. /* all DC coefficients use the same quant so as not to interfere
  379. * with DC prediction */
  380. s->qmat[qpi][inter][plane][0] = s->qmat[0][inter][plane][0];
  381. }
  382. }
  383. }
  384. /*
  385. * This function initializes the loop filter boundary limits if the frame's
  386. * quality index is different from the previous frame's.
  387. *
  388. * The filter_limit_values may not be larger than 127.
  389. */
  390. static void init_loop_filter(Vp3DecodeContext *s)
  391. {
  392. ff_vp3dsp_set_bounding_values(s->bounding_values_array, s->filter_limit_values[s->qps[0]]);
  393. }
  394. /*
  395. * This function unpacks all of the superblock/macroblock/fragment coding
  396. * information from the bitstream.
  397. */
  398. static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
  399. {
  400. int superblock_starts[3] = {
  401. 0, s->u_superblock_start, s->v_superblock_start
  402. };
  403. int bit = 0;
  404. int current_superblock = 0;
  405. int current_run = 0;
  406. int num_partial_superblocks = 0;
  407. int i, j;
  408. int current_fragment;
  409. int plane;
  410. int plane0_num_coded_frags = 0;
  411. if (s->keyframe) {
  412. memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);
  413. } else {
  414. /* unpack the list of partially-coded superblocks */
  415. bit = get_bits1(gb) ^ 1;
  416. current_run = 0;
  417. while (current_superblock < s->superblock_count && get_bits_left(gb) > 0) {
  418. if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
  419. bit = get_bits1(gb);
  420. else
  421. bit ^= 1;
  422. current_run = get_vlc2(gb, s->superblock_run_length_vlc.table,
  423. 6, 2) + 1;
  424. if (current_run == 34)
  425. current_run += get_bits(gb, 12);
  426. if (current_run > s->superblock_count - current_superblock) {
  427. av_log(s->avctx, AV_LOG_ERROR,
  428. "Invalid partially coded superblock run length\n");
  429. return -1;
  430. }
  431. memset(s->superblock_coding + current_superblock, bit, current_run);
  432. current_superblock += current_run;
  433. if (bit)
  434. num_partial_superblocks += current_run;
  435. }
  436. /* unpack the list of fully coded superblocks if any of the blocks were
  437. * not marked as partially coded in the previous step */
  438. if (num_partial_superblocks < s->superblock_count) {
  439. int superblocks_decoded = 0;
  440. current_superblock = 0;
  441. bit = get_bits1(gb) ^ 1;
  442. current_run = 0;
  443. while (superblocks_decoded < s->superblock_count - num_partial_superblocks &&
  444. get_bits_left(gb) > 0) {
  445. if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
  446. bit = get_bits1(gb);
  447. else
  448. bit ^= 1;
  449. current_run = get_vlc2(gb, s->superblock_run_length_vlc.table,
  450. 6, 2) + 1;
  451. if (current_run == 34)
  452. current_run += get_bits(gb, 12);
  453. for (j = 0; j < current_run; current_superblock++) {
  454. if (current_superblock >= s->superblock_count) {
  455. av_log(s->avctx, AV_LOG_ERROR,
  456. "Invalid fully coded superblock run length\n");
  457. return -1;
  458. }
  459. /* skip any superblocks already marked as partially coded */
  460. if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {
  461. s->superblock_coding[current_superblock] = 2 * bit;
  462. j++;
  463. }
  464. }
  465. superblocks_decoded += current_run;
  466. }
  467. }
  468. /* if there were partial blocks, initialize bitstream for
  469. * unpacking fragment codings */
  470. if (num_partial_superblocks) {
  471. current_run = 0;
  472. bit = get_bits1(gb);
  473. /* toggle the bit because as soon as the first run length is
  474. * fetched the bit will be toggled again */
  475. bit ^= 1;
  476. }
  477. }
  478. /* figure out which fragments are coded; iterate through each
  479. * superblock (all planes) */
  480. s->total_num_coded_frags = 0;
  481. memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
  482. s->coded_fragment_list[0] = s->keyframe ? s->kf_coded_fragment_list
  483. : s->nkf_coded_fragment_list;
  484. for (plane = 0; plane < 3; plane++) {
  485. int sb_start = superblock_starts[plane];
  486. int sb_end = sb_start + (plane ? s->c_superblock_count
  487. : s->y_superblock_count);
  488. int num_coded_frags = 0;
  489. if (s->keyframe) {
  490. if (s->num_kf_coded_fragment[plane] == -1) {
  491. for (i = sb_start; i < sb_end; i++) {
  492. /* iterate through all 16 fragments in a superblock */
  493. for (j = 0; j < 16; j++) {
  494. /* if the fragment is in bounds, check its coding status */
  495. current_fragment = s->superblock_fragments[i * 16 + j];
  496. if (current_fragment != -1) {
  497. s->coded_fragment_list[plane][num_coded_frags++] =
  498. current_fragment;
  499. }
  500. }
  501. }
  502. s->num_kf_coded_fragment[plane] = num_coded_frags;
  503. } else
  504. num_coded_frags = s->num_kf_coded_fragment[plane];
  505. } else {
  506. for (i = sb_start; i < sb_end && get_bits_left(gb) > 0; i++) {
  507. if (get_bits_left(gb) < plane0_num_coded_frags >> 2) {
  508. return AVERROR_INVALIDDATA;
  509. }
  510. /* iterate through all 16 fragments in a superblock */
  511. for (j = 0; j < 16; j++) {
  512. /* if the fragment is in bounds, check its coding status */
  513. current_fragment = s->superblock_fragments[i * 16 + j];
  514. if (current_fragment != -1) {
  515. int coded = s->superblock_coding[i];
  516. if (coded == SB_PARTIALLY_CODED) {
  517. /* fragment may or may not be coded; this is the case
  518. * that cares about the fragment coding runs */
  519. if (current_run-- == 0) {
  520. bit ^= 1;
  521. current_run = get_vlc2(gb, s->fragment_run_length_vlc.table, 5, 2);
  522. }
  523. coded = bit;
  524. }
  525. if (coded) {
  526. /* default mode; actual mode will be decoded in
  527. * the next phase */
  528. s->all_fragments[current_fragment].coding_method =
  529. MODE_INTER_NO_MV;
  530. s->coded_fragment_list[plane][num_coded_frags++] =
  531. current_fragment;
  532. } else {
  533. /* not coded; copy this fragment from the prior frame */
  534. s->all_fragments[current_fragment].coding_method =
  535. MODE_COPY;
  536. }
  537. }
  538. }
  539. }
  540. }
  541. if (!plane)
  542. plane0_num_coded_frags = num_coded_frags;
  543. s->total_num_coded_frags += num_coded_frags;
  544. for (i = 0; i < 64; i++)
  545. s->num_coded_frags[plane][i] = num_coded_frags;
  546. if (plane < 2)
  547. s->coded_fragment_list[plane + 1] = s->coded_fragment_list[plane] +
  548. num_coded_frags;
  549. }
  550. return 0;
  551. }
  552. #define BLOCK_X (2 * mb_x + (k & 1))
  553. #define BLOCK_Y (2 * mb_y + (k >> 1))
  554. #if CONFIG_VP4_DECODER
  555. /**
  556. * @return number of blocks, or > yuv_macroblock_count on error.
  557. * return value is always >= 1.
  558. */
  559. static int vp4_get_mb_count(Vp3DecodeContext *s, GetBitContext *gb)
  560. {
  561. int v = 1;
  562. int bits;
  563. while ((bits = show_bits(gb, 9)) == 0x1ff) {
  564. skip_bits(gb, 9);
  565. v += 256;
  566. if (v > s->yuv_macroblock_count) {
  567. av_log(s->avctx, AV_LOG_ERROR, "Invalid run length\n");
  568. return v;
  569. }
  570. }
  571. #define body(n) { \
  572. skip_bits(gb, 2 + n); \
  573. v += (1 << n) + get_bits(gb, n); }
  574. #define thresh(n) (0x200 - (0x80 >> n))
  575. #define else_if(n) else if (bits < thresh(n)) body(n)
  576. if (bits < 0x100) {
  577. skip_bits(gb, 1);
  578. } else if (bits < thresh(0)) {
  579. skip_bits(gb, 2);
  580. v += 1;
  581. }
  582. else_if(1)
  583. else_if(2)
  584. else_if(3)
  585. else_if(4)
  586. else_if(5)
  587. else_if(6)
  588. else body(7)
  589. #undef body
  590. #undef thresh
  591. #undef else_if
  592. return v;
  593. }
  594. static int vp4_get_block_pattern(Vp3DecodeContext *s, GetBitContext *gb, int *next_block_pattern_table)
  595. {
  596. int v = get_vlc2(gb, s->block_pattern_vlc[*next_block_pattern_table].table, 3, 2);
  597. if (v == -1) {
  598. av_log(s->avctx, AV_LOG_ERROR, "Invalid block pattern\n");
  599. *next_block_pattern_table = 0;
  600. return 0;
  601. }
  602. *next_block_pattern_table = vp4_block_pattern_table_selector[v];
  603. return v + 1;
  604. }
  605. static int vp4_unpack_macroblocks(Vp3DecodeContext *s, GetBitContext *gb)
  606. {
  607. int plane, i, j, k, fragment;
  608. int next_block_pattern_table;
  609. int bit, current_run, has_partial;
  610. memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
  611. if (s->keyframe)
  612. return 0;
  613. has_partial = 0;
  614. bit = get_bits1(gb);
  615. for (i = 0; i < s->yuv_macroblock_count; i += current_run) {
  616. if (get_bits_left(gb) <= 0)
  617. return AVERROR_INVALIDDATA;
  618. current_run = vp4_get_mb_count(s, gb);
  619. if (current_run > s->yuv_macroblock_count - i)
  620. return -1;
  621. memset(s->superblock_coding + i, 2 * bit, current_run);
  622. bit ^= 1;
  623. has_partial |= bit;
  624. }
  625. if (has_partial) {
  626. if (get_bits_left(gb) <= 0)
  627. return AVERROR_INVALIDDATA;
  628. bit = get_bits1(gb);
  629. current_run = vp4_get_mb_count(s, gb);
  630. for (i = 0; i < s->yuv_macroblock_count; i++) {
  631. if (!s->superblock_coding[i]) {
  632. if (!current_run) {
  633. bit ^= 1;
  634. current_run = vp4_get_mb_count(s, gb);
  635. }
  636. s->superblock_coding[i] = bit;
  637. current_run--;
  638. }
  639. }
  640. if (current_run) /* handle situation when vp4_get_mb_count() fails */
  641. return -1;
  642. }
  643. next_block_pattern_table = 0;
  644. i = 0;
  645. for (plane = 0; plane < 3; plane++) {
  646. int sb_x, sb_y;
  647. int sb_width = plane ? s->c_superblock_width : s->y_superblock_width;
  648. int sb_height = plane ? s->c_superblock_height : s->y_superblock_height;
  649. int mb_width = plane ? s->c_macroblock_width : s->macroblock_width;
  650. int mb_height = plane ? s->c_macroblock_height : s->macroblock_height;
  651. int fragment_width = s->fragment_width[!!plane];
  652. int fragment_height = s->fragment_height[!!plane];
  653. for (sb_y = 0; sb_y < sb_height; sb_y++) {
  654. for (sb_x = 0; sb_x < sb_width; sb_x++) {
  655. for (j = 0; j < 4; j++) {
  656. int mb_x = 2 * sb_x + (j >> 1);
  657. int mb_y = 2 * sb_y + (j >> 1) ^ (j & 1);
  658. int mb_coded, pattern, coded;
  659. if (mb_x >= mb_width || mb_y >= mb_height)
  660. continue;
  661. mb_coded = s->superblock_coding[i++];
  662. if (mb_coded == SB_FULLY_CODED)
  663. pattern = 0xF;
  664. else if (mb_coded == SB_PARTIALLY_CODED)
  665. pattern = vp4_get_block_pattern(s, gb, &next_block_pattern_table);
  666. else
  667. pattern = 0;
  668. for (k = 0; k < 4; k++) {
  669. if (BLOCK_X >= fragment_width || BLOCK_Y >= fragment_height)
  670. continue;
  671. fragment = s->fragment_start[plane] + BLOCK_Y * fragment_width + BLOCK_X;
  672. coded = pattern & (8 >> k);
  673. /* MODE_INTER_NO_MV is the default for coded fragments.
  674. the actual method is decoded in the next phase. */
  675. s->all_fragments[fragment].coding_method = coded ? MODE_INTER_NO_MV : MODE_COPY;
  676. }
  677. }
  678. }
  679. }
  680. }
  681. return 0;
  682. }
  683. #endif
  684. /*
  685. * This function unpacks all the coding mode data for individual macroblocks
  686. * from the bitstream.
  687. */
  688. static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
  689. {
  690. int i, j, k, sb_x, sb_y;
  691. int scheme;
  692. int current_macroblock;
  693. int current_fragment;
  694. int coding_mode;
  695. int custom_mode_alphabet[CODING_MODE_COUNT];
  696. const int *alphabet;
  697. Vp3Fragment *frag;
  698. if (s->keyframe) {
  699. for (i = 0; i < s->fragment_count; i++)
  700. s->all_fragments[i].coding_method = MODE_INTRA;
  701. } else {
  702. /* fetch the mode coding scheme for this frame */
  703. scheme = get_bits(gb, 3);
  704. /* is it a custom coding scheme? */
  705. if (scheme == 0) {
  706. for (i = 0; i < 8; i++)
  707. custom_mode_alphabet[i] = MODE_INTER_NO_MV;
  708. for (i = 0; i < 8; i++)
  709. custom_mode_alphabet[get_bits(gb, 3)] = i;
  710. alphabet = custom_mode_alphabet;
  711. } else
  712. alphabet = ModeAlphabet[scheme - 1];
  713. /* iterate through all of the macroblocks that contain 1 or more
  714. * coded fragments */
  715. for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
  716. for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
  717. if (get_bits_left(gb) <= 0)
  718. return -1;
  719. for (j = 0; j < 4; j++) {
  720. int mb_x = 2 * sb_x + (j >> 1);
  721. int mb_y = 2 * sb_y + (((j >> 1) + j) & 1);
  722. current_macroblock = mb_y * s->macroblock_width + mb_x;
  723. if (mb_x >= s->macroblock_width ||
  724. mb_y >= s->macroblock_height)
  725. continue;
  726. /* coding modes are only stored if the macroblock has
  727. * at least one luma block coded, otherwise it must be
  728. * INTER_NO_MV */
  729. for (k = 0; k < 4; k++) {
  730. current_fragment = BLOCK_Y *
  731. s->fragment_width[0] + BLOCK_X;
  732. if (s->all_fragments[current_fragment].coding_method != MODE_COPY)
  733. break;
  734. }
  735. if (k == 4) {
  736. s->macroblock_coding[current_macroblock] = MODE_INTER_NO_MV;
  737. continue;
  738. }
  739. /* mode 7 means get 3 bits for each coding mode */
  740. if (scheme == 7)
  741. coding_mode = get_bits(gb, 3);
  742. else
  743. coding_mode = alphabet[get_vlc2(gb, s->mode_code_vlc.table, 3, 3)];
  744. s->macroblock_coding[current_macroblock] = coding_mode;
  745. for (k = 0; k < 4; k++) {
  746. frag = s->all_fragments + BLOCK_Y * s->fragment_width[0] + BLOCK_X;
  747. if (frag->coding_method != MODE_COPY)
  748. frag->coding_method = coding_mode;
  749. }
  750. #define SET_CHROMA_MODES \
  751. if (frag[s->fragment_start[1]].coding_method != MODE_COPY) \
  752. frag[s->fragment_start[1]].coding_method = coding_mode; \
  753. if (frag[s->fragment_start[2]].coding_method != MODE_COPY) \
  754. frag[s->fragment_start[2]].coding_method = coding_mode;
  755. if (s->chroma_y_shift) {
  756. frag = s->all_fragments + mb_y *
  757. s->fragment_width[1] + mb_x;
  758. SET_CHROMA_MODES
  759. } else if (s->chroma_x_shift) {
  760. frag = s->all_fragments +
  761. 2 * mb_y * s->fragment_width[1] + mb_x;
  762. for (k = 0; k < 2; k++) {
  763. SET_CHROMA_MODES
  764. frag += s->fragment_width[1];
  765. }
  766. } else {
  767. for (k = 0; k < 4; k++) {
  768. frag = s->all_fragments +
  769. BLOCK_Y * s->fragment_width[1] + BLOCK_X;
  770. SET_CHROMA_MODES
  771. }
  772. }
  773. }
  774. }
  775. }
  776. }
  777. return 0;
  778. }
  779. static int vp4_get_mv(Vp3DecodeContext *s, GetBitContext *gb, int axis, int last_motion)
  780. {
  781. int v = get_vlc2(gb, s->vp4_mv_vlc[axis][vp4_mv_table_selector[FFABS(last_motion)]].table, 6, 2) - 31;
  782. return last_motion < 0 ? -v : v;
  783. }
  784. /*
  785. * This function unpacks all the motion vectors for the individual
  786. * macroblocks from the bitstream.
  787. */
  788. static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
  789. {
  790. int j, k, sb_x, sb_y;
  791. int coding_mode;
  792. int motion_x[4];
  793. int motion_y[4];
  794. int last_motion_x = 0;
  795. int last_motion_y = 0;
  796. int prior_last_motion_x = 0;
  797. int prior_last_motion_y = 0;
  798. int last_gold_motion_x = 0;
  799. int last_gold_motion_y = 0;
  800. int current_macroblock;
  801. int current_fragment;
  802. int frag;
  803. if (s->keyframe)
  804. return 0;
  805. /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme; 2 is VP4 code scheme */
  806. coding_mode = s->version < 2 ? get_bits1(gb) : 2;
  807. /* iterate through all of the macroblocks that contain 1 or more
  808. * coded fragments */
  809. for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
  810. for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
  811. if (get_bits_left(gb) <= 0)
  812. return -1;
  813. for (j = 0; j < 4; j++) {
  814. int mb_x = 2 * sb_x + (j >> 1);
  815. int mb_y = 2 * sb_y + (((j >> 1) + j) & 1);
  816. current_macroblock = mb_y * s->macroblock_width + mb_x;
  817. if (mb_x >= s->macroblock_width ||
  818. mb_y >= s->macroblock_height ||
  819. s->macroblock_coding[current_macroblock] == MODE_COPY)
  820. continue;
  821. switch (s->macroblock_coding[current_macroblock]) {
  822. case MODE_GOLDEN_MV:
  823. if (coding_mode == 2) { /* VP4 */
  824. last_gold_motion_x = motion_x[0] = vp4_get_mv(s, gb, 0, last_gold_motion_x);
  825. last_gold_motion_y = motion_y[0] = vp4_get_mv(s, gb, 1, last_gold_motion_y);
  826. break;
  827. } /* otherwise fall through */
  828. case MODE_INTER_PLUS_MV:
  829. /* all 6 fragments use the same motion vector */
  830. if (coding_mode == 0) {
  831. motion_x[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
  832. motion_y[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
  833. } else if (coding_mode == 1) {
  834. motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)];
  835. motion_y[0] = fixed_motion_vector_table[get_bits(gb, 6)];
  836. } else { /* VP4 */
  837. motion_x[0] = vp4_get_mv(s, gb, 0, last_motion_x);
  838. motion_y[0] = vp4_get_mv(s, gb, 1, last_motion_y);
  839. }
  840. /* vector maintenance, only on MODE_INTER_PLUS_MV */
  841. if (s->macroblock_coding[current_macroblock] == MODE_INTER_PLUS_MV) {
  842. prior_last_motion_x = last_motion_x;
  843. prior_last_motion_y = last_motion_y;
  844. last_motion_x = motion_x[0];
  845. last_motion_y = motion_y[0];
  846. }
  847. break;
  848. case MODE_INTER_FOURMV:
  849. /* vector maintenance */
  850. prior_last_motion_x = last_motion_x;
  851. prior_last_motion_y = last_motion_y;
  852. /* fetch 4 vectors from the bitstream, one for each
  853. * Y fragment, then average for the C fragment vectors */
  854. for (k = 0; k < 4; k++) {
  855. current_fragment = BLOCK_Y * s->fragment_width[0] + BLOCK_X;
  856. if (s->all_fragments[current_fragment].coding_method != MODE_COPY) {
  857. if (coding_mode == 0) {
  858. motion_x[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
  859. motion_y[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
  860. } else if (coding_mode == 1) {
  861. motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)];
  862. motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)];
  863. } else { /* VP4 */
  864. motion_x[k] = vp4_get_mv(s, gb, 0, prior_last_motion_x);
  865. motion_y[k] = vp4_get_mv(s, gb, 1, prior_last_motion_y);
  866. }
  867. last_motion_x = motion_x[k];
  868. last_motion_y = motion_y[k];
  869. } else {
  870. motion_x[k] = 0;
  871. motion_y[k] = 0;
  872. }
  873. }
  874. break;
  875. case MODE_INTER_LAST_MV:
  876. /* all 6 fragments use the last motion vector */
  877. motion_x[0] = last_motion_x;
  878. motion_y[0] = last_motion_y;
  879. /* no vector maintenance (last vector remains the
  880. * last vector) */
  881. break;
  882. case MODE_INTER_PRIOR_LAST:
  883. /* all 6 fragments use the motion vector prior to the
  884. * last motion vector */
  885. motion_x[0] = prior_last_motion_x;
  886. motion_y[0] = prior_last_motion_y;
  887. /* vector maintenance */
  888. prior_last_motion_x = last_motion_x;
  889. prior_last_motion_y = last_motion_y;
  890. last_motion_x = motion_x[0];
  891. last_motion_y = motion_y[0];
  892. break;
  893. default:
  894. /* covers intra, inter without MV, golden without MV */
  895. motion_x[0] = 0;
  896. motion_y[0] = 0;
  897. /* no vector maintenance */
  898. break;
  899. }
  900. /* assign the motion vectors to the correct fragments */
  901. for (k = 0; k < 4; k++) {
  902. current_fragment =
  903. BLOCK_Y * s->fragment_width[0] + BLOCK_X;
  904. if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
  905. s->motion_val[0][current_fragment][0] = motion_x[k];
  906. s->motion_val[0][current_fragment][1] = motion_y[k];
  907. } else {
  908. s->motion_val[0][current_fragment][0] = motion_x[0];
  909. s->motion_val[0][current_fragment][1] = motion_y[0];
  910. }
  911. }
  912. if (s->chroma_y_shift) {
  913. if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
  914. motion_x[0] = RSHIFT(motion_x[0] + motion_x[1] +
  915. motion_x[2] + motion_x[3], 2);
  916. motion_y[0] = RSHIFT(motion_y[0] + motion_y[1] +
  917. motion_y[2] + motion_y[3], 2);
  918. }
  919. if (s->version <= 2) {
  920. motion_x[0] = (motion_x[0] >> 1) | (motion_x[0] & 1);
  921. motion_y[0] = (motion_y[0] >> 1) | (motion_y[0] & 1);
  922. }
  923. frag = mb_y * s->fragment_width[1] + mb_x;
  924. s->motion_val[1][frag][0] = motion_x[0];
  925. s->motion_val[1][frag][1] = motion_y[0];
  926. } else if (s->chroma_x_shift) {
  927. if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
  928. motion_x[0] = RSHIFT(motion_x[0] + motion_x[1], 1);
  929. motion_y[0] = RSHIFT(motion_y[0] + motion_y[1], 1);
  930. motion_x[1] = RSHIFT(motion_x[2] + motion_x[3], 1);
  931. motion_y[1] = RSHIFT(motion_y[2] + motion_y[3], 1);
  932. } else {
  933. motion_x[1] = motion_x[0];
  934. motion_y[1] = motion_y[0];
  935. }
  936. if (s->version <= 2) {
  937. motion_x[0] = (motion_x[0] >> 1) | (motion_x[0] & 1);
  938. motion_x[1] = (motion_x[1] >> 1) | (motion_x[1] & 1);
  939. }
  940. frag = 2 * mb_y * s->fragment_width[1] + mb_x;
  941. for (k = 0; k < 2; k++) {
  942. s->motion_val[1][frag][0] = motion_x[k];
  943. s->motion_val[1][frag][1] = motion_y[k];
  944. frag += s->fragment_width[1];
  945. }
  946. } else {
  947. for (k = 0; k < 4; k++) {
  948. frag = BLOCK_Y * s->fragment_width[1] + BLOCK_X;
  949. if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
  950. s->motion_val[1][frag][0] = motion_x[k];
  951. s->motion_val[1][frag][1] = motion_y[k];
  952. } else {
  953. s->motion_val[1][frag][0] = motion_x[0];
  954. s->motion_val[1][frag][1] = motion_y[0];
  955. }
  956. }
  957. }
  958. }
  959. }
  960. }
  961. return 0;
  962. }
  963. static int unpack_block_qpis(Vp3DecodeContext *s, GetBitContext *gb)
  964. {
  965. int qpi, i, j, bit, run_length, blocks_decoded, num_blocks_at_qpi;
  966. int num_blocks = s->total_num_coded_frags;
  967. for (qpi = 0; qpi < s->nqps - 1 && num_blocks > 0; qpi++) {
  968. i = blocks_decoded = num_blocks_at_qpi = 0;
  969. bit = get_bits1(gb) ^ 1;
  970. run_length = 0;
  971. do {
  972. if (run_length == MAXIMUM_LONG_BIT_RUN)
  973. bit = get_bits1(gb);
  974. else
  975. bit ^= 1;
  976. run_length = get_vlc2(gb, s->superblock_run_length_vlc.table, 6, 2) + 1;
  977. if (run_length == 34)
  978. run_length += get_bits(gb, 12);
  979. blocks_decoded += run_length;
  980. if (!bit)
  981. num_blocks_at_qpi += run_length;
  982. for (j = 0; j < run_length; i++) {
  983. if (i >= s->total_num_coded_frags)
  984. return -1;
  985. if (s->all_fragments[s->coded_fragment_list[0][i]].qpi == qpi) {
  986. s->all_fragments[s->coded_fragment_list[0][i]].qpi += bit;
  987. j++;
  988. }
  989. }
  990. } while (blocks_decoded < num_blocks && get_bits_left(gb) > 0);
  991. num_blocks -= num_blocks_at_qpi;
  992. }
  993. return 0;
  994. }
  995. static inline int get_eob_run(GetBitContext *gb, int token)
  996. {
  997. int v = eob_run_table[token].base;
  998. if (eob_run_table[token].bits)
  999. v += get_bits(gb, eob_run_table[token].bits);
  1000. return v;
  1001. }
  1002. static inline int get_coeff(GetBitContext *gb, int token, int16_t *coeff)
  1003. {
  1004. int bits_to_get, zero_run;
  1005. bits_to_get = coeff_get_bits[token];
  1006. if (bits_to_get)
  1007. bits_to_get = get_bits(gb, bits_to_get);
  1008. *coeff = coeff_tables[token][bits_to_get];
  1009. zero_run = zero_run_base[token];
  1010. if (zero_run_get_bits[token])
  1011. zero_run += get_bits(gb, zero_run_get_bits[token]);
  1012. return zero_run;
  1013. }
  1014. /*
  1015. * This function is called by unpack_dct_coeffs() to extract the VLCs from
  1016. * the bitstream. The VLCs encode tokens which are used to unpack DCT
  1017. * data. This function unpacks all the VLCs for either the Y plane or both
  1018. * C planes, and is called for DC coefficients or different AC coefficient
  1019. * levels (since different coefficient types require different VLC tables.
  1020. *
  1021. * This function returns a residual eob run. E.g, if a particular token gave
  1022. * instructions to EOB the next 5 fragments and there were only 2 fragments
  1023. * left in the current fragment range, 3 would be returned so that it could
  1024. * be passed into the next call to this same function.
  1025. */
  1026. static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
  1027. VLC *table, int coeff_index,
  1028. int plane,
  1029. int eob_run)
  1030. {
  1031. int i, j = 0;
  1032. int token;
  1033. int zero_run = 0;
  1034. int16_t coeff = 0;
  1035. int blocks_ended;
  1036. int coeff_i = 0;
  1037. int num_coeffs = s->num_coded_frags[plane][coeff_index];
  1038. int16_t *dct_tokens = s->dct_tokens[plane][coeff_index];
  1039. /* local references to structure members to avoid repeated dereferences */
  1040. int *coded_fragment_list = s->coded_fragment_list[plane];
  1041. Vp3Fragment *all_fragments = s->all_fragments;
  1042. VLC_TYPE(*vlc_table)[2] = table->table;
  1043. if (num_coeffs < 0) {
  1044. av_log(s->avctx, AV_LOG_ERROR,
  1045. "Invalid number of coefficients at level %d\n", coeff_index);
  1046. return AVERROR_INVALIDDATA;
  1047. }
  1048. if (eob_run > num_coeffs) {
  1049. coeff_i =
  1050. blocks_ended = num_coeffs;
  1051. eob_run -= num_coeffs;
  1052. } else {
  1053. coeff_i =
  1054. blocks_ended = eob_run;
  1055. eob_run = 0;
  1056. }
  1057. // insert fake EOB token to cover the split between planes or zzi
  1058. if (blocks_ended)
  1059. dct_tokens[j++] = blocks_ended << 2;
  1060. while (coeff_i < num_coeffs && get_bits_left(gb) > 0) {
  1061. /* decode a VLC into a token */
  1062. token = get_vlc2(gb, vlc_table, 11, 3);
  1063. /* use the token to get a zero run, a coefficient, and an eob run */
  1064. if ((unsigned) token <= 6U) {
  1065. eob_run = get_eob_run(gb, token);
  1066. if (!eob_run)
  1067. eob_run = INT_MAX;
  1068. // record only the number of blocks ended in this plane,
  1069. // any spill will be recorded in the next plane.
  1070. if (eob_run > num_coeffs - coeff_i) {
  1071. dct_tokens[j++] = TOKEN_EOB(num_coeffs - coeff_i);
  1072. blocks_ended += num_coeffs - coeff_i;
  1073. eob_run -= num_coeffs - coeff_i;
  1074. coeff_i = num_coeffs;
  1075. } else {
  1076. dct_tokens[j++] = TOKEN_EOB(eob_run);
  1077. blocks_ended += eob_run;
  1078. coeff_i += eob_run;
  1079. eob_run = 0;
  1080. }
  1081. } else if (token >= 0) {
  1082. zero_run = get_coeff(gb, token, &coeff);
  1083. if (zero_run) {
  1084. dct_tokens[j++] = TOKEN_ZERO_RUN(coeff, zero_run);
  1085. } else {
  1086. // Save DC into the fragment structure. DC prediction is
  1087. // done in raster order, so the actual DC can't be in with
  1088. // other tokens. We still need the token in dct_tokens[]
  1089. // however, or else the structure collapses on itself.
  1090. if (!coeff_index)
  1091. all_fragments[coded_fragment_list[coeff_i]].dc = coeff;
  1092. dct_tokens[j++] = TOKEN_COEFF(coeff);
  1093. }
  1094. if (coeff_index + zero_run > 64) {
  1095. av_log(s->avctx, AV_LOG_DEBUG,
  1096. "Invalid zero run of %d with %d coeffs left\n",
  1097. zero_run, 64 - coeff_index);
  1098. zero_run = 64 - coeff_index;
  1099. }
  1100. // zero runs code multiple coefficients,
  1101. // so don't try to decode coeffs for those higher levels
  1102. for (i = coeff_index + 1; i <= coeff_index + zero_run; i++)
  1103. s->num_coded_frags[plane][i]--;
  1104. coeff_i++;
  1105. } else {
  1106. av_log(s->avctx, AV_LOG_ERROR, "Invalid token %d\n", token);
  1107. return -1;
  1108. }
  1109. }
  1110. if (blocks_ended > s->num_coded_frags[plane][coeff_index])
  1111. av_log(s->avctx, AV_LOG_ERROR, "More blocks ended than coded!\n");
  1112. // decrement the number of blocks that have higher coefficients for each
  1113. // EOB run at this level
  1114. if (blocks_ended)
  1115. for (i = coeff_index + 1; i < 64; i++)
  1116. s->num_coded_frags[plane][i] -= blocks_ended;
  1117. // setup the next buffer
  1118. if (plane < 2)
  1119. s->dct_tokens[plane + 1][coeff_index] = dct_tokens + j;
  1120. else if (coeff_index < 63)
  1121. s->dct_tokens[0][coeff_index + 1] = dct_tokens + j;
  1122. return eob_run;
  1123. }
  1124. static void reverse_dc_prediction(Vp3DecodeContext *s,
  1125. int first_fragment,
  1126. int fragment_width,
  1127. int fragment_height);
  1128. /*
  1129. * This function unpacks all of the DCT coefficient data from the
  1130. * bitstream.
  1131. */
  1132. static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
  1133. {
  1134. int i;
  1135. int dc_y_table;
  1136. int dc_c_table;
  1137. int ac_y_table;
  1138. int ac_c_table;
  1139. int residual_eob_run = 0;
  1140. VLC *y_tables[64];
  1141. VLC *c_tables[64];
  1142. s->dct_tokens[0][0] = s->dct_tokens_base;
  1143. if (get_bits_left(gb) < 16)
  1144. return AVERROR_INVALIDDATA;
  1145. /* fetch the DC table indexes */
  1146. dc_y_table = get_bits(gb, 4);
  1147. dc_c_table = get_bits(gb, 4);
  1148. /* unpack the Y plane DC coefficients */
  1149. residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
  1150. 0, residual_eob_run);
  1151. if (residual_eob_run < 0)
  1152. return residual_eob_run;
  1153. if (get_bits_left(gb) < 8)
  1154. return AVERROR_INVALIDDATA;
  1155. /* reverse prediction of the Y-plane DC coefficients */
  1156. reverse_dc_prediction(s, 0, s->fragment_width[0], s->fragment_height[0]);
  1157. /* unpack the C plane DC coefficients */
  1158. residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
  1159. 1, residual_eob_run);
  1160. if (residual_eob_run < 0)
  1161. return residual_eob_run;
  1162. residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
  1163. 2, residual_eob_run);
  1164. if (residual_eob_run < 0)
  1165. return residual_eob_run;
  1166. /* reverse prediction of the C-plane DC coefficients */
  1167. if (!(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  1168. reverse_dc_prediction(s, s->fragment_start[1],
  1169. s->fragment_width[1], s->fragment_height[1]);
  1170. reverse_dc_prediction(s, s->fragment_start[2],
  1171. s->fragment_width[1], s->fragment_height[1]);
  1172. }
  1173. if (get_bits_left(gb) < 8)
  1174. return AVERROR_INVALIDDATA;
  1175. /* fetch the AC table indexes */
  1176. ac_y_table = get_bits(gb, 4);
  1177. ac_c_table = get_bits(gb, 4);
  1178. /* build tables of AC VLC tables */
  1179. for (i = 1; i <= 5; i++) {
  1180. y_tables[i] = &s->ac_vlc_1[ac_y_table];
  1181. c_tables[i] = &s->ac_vlc_1[ac_c_table];
  1182. }
  1183. for (i = 6; i <= 14; i++) {
  1184. y_tables[i] = &s->ac_vlc_2[ac_y_table];
  1185. c_tables[i] = &s->ac_vlc_2[ac_c_table];
  1186. }
  1187. for (i = 15; i <= 27; i++) {
  1188. y_tables[i] = &s->ac_vlc_3[ac_y_table];
  1189. c_tables[i] = &s->ac_vlc_3[ac_c_table];
  1190. }
  1191. for (i = 28; i <= 63; i++) {
  1192. y_tables[i] = &s->ac_vlc_4[ac_y_table];
  1193. c_tables[i] = &s->ac_vlc_4[ac_c_table];
  1194. }
  1195. /* decode all AC coefficients */
  1196. for (i = 1; i <= 63; i++) {
  1197. residual_eob_run = unpack_vlcs(s, gb, y_tables[i], i,
  1198. 0, residual_eob_run);
  1199. if (residual_eob_run < 0)
  1200. return residual_eob_run;
  1201. residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
  1202. 1, residual_eob_run);
  1203. if (residual_eob_run < 0)
  1204. return residual_eob_run;
  1205. residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
  1206. 2, residual_eob_run);
  1207. if (residual_eob_run < 0)
  1208. return residual_eob_run;
  1209. }
  1210. return 0;
  1211. }
  1212. #if CONFIG_VP4_DECODER
  1213. /**
  1214. * eob_tracker[] is instead of TOKEN_EOB(value)
  1215. * a dummy TOKEN_EOB(0) value is used to make vp3_dequant work
  1216. *
  1217. * @return < 0 on error
  1218. */
  1219. static int vp4_unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
  1220. VLC *vlc_tables[64],
  1221. int plane, int eob_tracker[64], int fragment)
  1222. {
  1223. int token;
  1224. int zero_run = 0;
  1225. int16_t coeff = 0;
  1226. int coeff_i = 0;
  1227. int eob_run;
  1228. while (!eob_tracker[coeff_i]) {
  1229. if (get_bits_left(gb) < 1)
  1230. return AVERROR_INVALIDDATA;
  1231. token = get_vlc2(gb, vlc_tables[coeff_i]->table, 11, 3);
  1232. /* use the token to get a zero run, a coefficient, and an eob run */
  1233. if ((unsigned) token <= 6U) {
  1234. eob_run = get_eob_run(gb, token);
  1235. *s->dct_tokens[plane][coeff_i]++ = TOKEN_EOB(0);
  1236. eob_tracker[coeff_i] = eob_run - 1;
  1237. return 0;
  1238. } else if (token >= 0) {
  1239. zero_run = get_coeff(gb, token, &coeff);
  1240. if (zero_run) {
  1241. if (coeff_i + zero_run > 64) {
  1242. av_log(s->avctx, AV_LOG_DEBUG,
  1243. "Invalid zero run of %d with %d coeffs left\n",
  1244. zero_run, 64 - coeff_i);
  1245. zero_run = 64 - coeff_i;
  1246. }
  1247. *s->dct_tokens[plane][coeff_i]++ = TOKEN_ZERO_RUN(coeff, zero_run);
  1248. coeff_i += zero_run;
  1249. } else {
  1250. if (!coeff_i)
  1251. s->all_fragments[fragment].dc = coeff;
  1252. *s->dct_tokens[plane][coeff_i]++ = TOKEN_COEFF(coeff);
  1253. }
  1254. coeff_i++;
  1255. if (coeff_i >= 64) /* > 64 occurs when there is a zero_run overflow */
  1256. return 0; /* stop */
  1257. } else {
  1258. av_log(s->avctx, AV_LOG_ERROR, "Invalid token %d\n", token);
  1259. return -1;
  1260. }
  1261. }
  1262. *s->dct_tokens[plane][coeff_i]++ = TOKEN_EOB(0);
  1263. eob_tracker[coeff_i]--;
  1264. return 0;
  1265. }
  1266. static void vp4_dc_predictor_reset(VP4Predictor *p)
  1267. {
  1268. p->dc = 0;
  1269. p->type = VP4_DC_UNDEFINED;
  1270. }
  1271. static void vp4_dc_pred_before(const Vp3DecodeContext *s, VP4Predictor dc_pred[6][6], int sb_x)
  1272. {
  1273. int i, j;
  1274. for (i = 0; i < 4; i++)
  1275. dc_pred[0][i + 1] = s->dc_pred_row[sb_x * 4 + i];
  1276. for (j = 1; j < 5; j++)
  1277. for (i = 0; i < 4; i++)
  1278. vp4_dc_predictor_reset(&dc_pred[j][i + 1]);
  1279. }
  1280. static void vp4_dc_pred_after(Vp3DecodeContext *s, VP4Predictor dc_pred[6][6], int sb_x)
  1281. {
  1282. int i;
  1283. for (i = 0; i < 4; i++)
  1284. s->dc_pred_row[sb_x * 4 + i] = dc_pred[4][i + 1];
  1285. for (i = 1; i < 5; i++)
  1286. dc_pred[i][0] = dc_pred[i][4];
  1287. }
  1288. /* note: dc_pred points to the current block */
  1289. static int vp4_dc_pred(const Vp3DecodeContext *s, const VP4Predictor * dc_pred, const int * last_dc, int type, int plane)
  1290. {
  1291. int count = 0;
  1292. int dc = 0;
  1293. if (dc_pred[-6].type == type) {
  1294. dc += dc_pred[-6].dc;
  1295. count++;
  1296. }
  1297. if (dc_pred[6].type == type) {
  1298. dc += dc_pred[6].dc;
  1299. count++;
  1300. }
  1301. if (count != 2 && dc_pred[-1].type == type) {
  1302. dc += dc_pred[-1].dc;
  1303. count++;
  1304. }
  1305. if (count != 2 && dc_pred[1].type == type) {
  1306. dc += dc_pred[1].dc;
  1307. count++;
  1308. }
  1309. /* using division instead of shift to correctly handle negative values */
  1310. return count == 2 ? dc / 2 : last_dc[type];
  1311. }
  1312. static void vp4_set_tokens_base(Vp3DecodeContext *s)
  1313. {
  1314. int plane, i;
  1315. int16_t *base = s->dct_tokens_base;
  1316. for (plane = 0; plane < 3; plane++) {
  1317. for (i = 0; i < 64; i++) {
  1318. s->dct_tokens[plane][i] = base;
  1319. base += s->fragment_width[!!plane] * s->fragment_height[!!plane];
  1320. }
  1321. }
  1322. }
  1323. static int vp4_unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
  1324. {
  1325. int i, j;
  1326. int dc_y_table;
  1327. int dc_c_table;
  1328. int ac_y_table;
  1329. int ac_c_table;
  1330. VLC *tables[2][64];
  1331. int plane, sb_y, sb_x;
  1332. int eob_tracker[64];
  1333. VP4Predictor dc_pred[6][6];
  1334. int last_dc[NB_VP4_DC_TYPES];
  1335. if (get_bits_left(gb) < 16)
  1336. return AVERROR_INVALIDDATA;
  1337. /* fetch the DC table indexes */
  1338. dc_y_table = get_bits(gb, 4);
  1339. dc_c_table = get_bits(gb, 4);
  1340. ac_y_table = get_bits(gb, 4);
  1341. ac_c_table = get_bits(gb, 4);
  1342. /* build tables of DC/AC VLC tables */
  1343. tables[0][0] = &s->dc_vlc[dc_y_table];
  1344. tables[1][0] = &s->dc_vlc[dc_c_table];
  1345. for (i = 1; i <= 5; i++) {
  1346. tables[0][i] = &s->ac_vlc_1[ac_y_table];
  1347. tables[1][i] = &s->ac_vlc_1[ac_c_table];
  1348. }
  1349. for (i = 6; i <= 14; i++) {
  1350. tables[0][i] = &s->ac_vlc_2[ac_y_table];
  1351. tables[1][i] = &s->ac_vlc_2[ac_c_table];
  1352. }
  1353. for (i = 15; i <= 27; i++) {
  1354. tables[0][i] = &s->ac_vlc_3[ac_y_table];
  1355. tables[1][i] = &s->ac_vlc_3[ac_c_table];
  1356. }
  1357. for (i = 28; i <= 63; i++) {
  1358. tables[0][i] = &s->ac_vlc_4[ac_y_table];
  1359. tables[1][i] = &s->ac_vlc_4[ac_c_table];
  1360. }
  1361. vp4_set_tokens_base(s);
  1362. memset(last_dc, 0, sizeof(last_dc));
  1363. for (plane = 0; plane < ((s->avctx->flags & AV_CODEC_FLAG_GRAY) ? 1 : 3); plane++) {
  1364. memset(eob_tracker, 0, sizeof(eob_tracker));
  1365. /* initialise dc prediction */
  1366. for (i = 0; i < s->fragment_width[!!plane]; i++)
  1367. vp4_dc_predictor_reset(&s->dc_pred_row[i]);
  1368. for (j = 0; j < 6; j++)
  1369. for (i = 0; i < 6; i++)
  1370. vp4_dc_predictor_reset(&dc_pred[j][i]);
  1371. for (sb_y = 0; sb_y * 4 < s->fragment_height[!!plane]; sb_y++) {
  1372. for (sb_x = 0; sb_x *4 < s->fragment_width[!!plane]; sb_x++) {
  1373. vp4_dc_pred_before(s, dc_pred, sb_x);
  1374. for (j = 0; j < 16; j++) {
  1375. int hx = hilbert_offset[j][0];
  1376. int hy = hilbert_offset[j][1];
  1377. int x = 4 * sb_x + hx;
  1378. int y = 4 * sb_y + hy;
  1379. VP4Predictor *this_dc_pred = &dc_pred[hy + 1][hx + 1];
  1380. int fragment, dc_block_type;
  1381. if (x >= s->fragment_width[!!plane] || y >= s->fragment_height[!!plane])
  1382. continue;
  1383. fragment = s->fragment_start[plane] + y * s->fragment_width[!!plane] + x;
  1384. if (s->all_fragments[fragment].coding_method == MODE_COPY)
  1385. continue;
  1386. if (vp4_unpack_vlcs(s, gb, tables[!!plane], plane, eob_tracker, fragment) < 0)
  1387. return -1;
  1388. dc_block_type = vp4_pred_block_type_map[s->all_fragments[fragment].coding_method];
  1389. s->all_fragments[fragment].dc +=
  1390. vp4_dc_pred(s, this_dc_pred, last_dc, dc_block_type, plane);
  1391. this_dc_pred->type = dc_block_type,
  1392. this_dc_pred->dc = last_dc[dc_block_type] = s->all_fragments[fragment].dc;
  1393. }
  1394. vp4_dc_pred_after(s, dc_pred, sb_x);
  1395. }
  1396. }
  1397. }
  1398. vp4_set_tokens_base(s);
  1399. return 0;
  1400. }
  1401. #endif
  1402. /*
  1403. * This function reverses the DC prediction for each coded fragment in
  1404. * the frame. Much of this function is adapted directly from the original
  1405. * VP3 source code.
  1406. */
  1407. #define COMPATIBLE_FRAME(x) \
  1408. (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
  1409. #define DC_COEFF(u) s->all_fragments[u].dc
  1410. static void reverse_dc_prediction(Vp3DecodeContext *s,
  1411. int first_fragment,
  1412. int fragment_width,
  1413. int fragment_height)
  1414. {
  1415. #define PUL 8
  1416. #define PU 4
  1417. #define PUR 2
  1418. #define PL 1
  1419. int x, y;
  1420. int i = first_fragment;
  1421. int predicted_dc;
  1422. /* DC values for the left, up-left, up, and up-right fragments */
  1423. int vl, vul, vu, vur;
  1424. /* indexes for the left, up-left, up, and up-right fragments */
  1425. int l, ul, u, ur;
  1426. /*
  1427. * The 6 fields mean:
  1428. * 0: up-left multiplier
  1429. * 1: up multiplier
  1430. * 2: up-right multiplier
  1431. * 3: left multiplier
  1432. */
  1433. static const int predictor_transform[16][4] = {
  1434. { 0, 0, 0, 0 },
  1435. { 0, 0, 0, 128 }, // PL
  1436. { 0, 0, 128, 0 }, // PUR
  1437. { 0, 0, 53, 75 }, // PUR|PL
  1438. { 0, 128, 0, 0 }, // PU
  1439. { 0, 64, 0, 64 }, // PU |PL
  1440. { 0, 128, 0, 0 }, // PU |PUR
  1441. { 0, 0, 53, 75 }, // PU |PUR|PL
  1442. { 128, 0, 0, 0 }, // PUL
  1443. { 0, 0, 0, 128 }, // PUL|PL
  1444. { 64, 0, 64, 0 }, // PUL|PUR
  1445. { 0, 0, 53, 75 }, // PUL|PUR|PL
  1446. { 0, 128, 0, 0 }, // PUL|PU
  1447. { -104, 116, 0, 116 }, // PUL|PU |PL
  1448. { 24, 80, 24, 0 }, // PUL|PU |PUR
  1449. { -104, 116, 0, 116 } // PUL|PU |PUR|PL
  1450. };
  1451. /* This table shows which types of blocks can use other blocks for
  1452. * prediction. For example, INTRA is the only mode in this table to
  1453. * have a frame number of 0. That means INTRA blocks can only predict
  1454. * from other INTRA blocks. There are 2 golden frame coding types;
  1455. * blocks encoding in these modes can only predict from other blocks
  1456. * that were encoded with these 1 of these 2 modes. */
  1457. static const unsigned char compatible_frame[9] = {
  1458. 1, /* MODE_INTER_NO_MV */
  1459. 0, /* MODE_INTRA */
  1460. 1, /* MODE_INTER_PLUS_MV */
  1461. 1, /* MODE_INTER_LAST_MV */
  1462. 1, /* MODE_INTER_PRIOR_MV */
  1463. 2, /* MODE_USING_GOLDEN */
  1464. 2, /* MODE_GOLDEN_MV */
  1465. 1, /* MODE_INTER_FOUR_MV */
  1466. 3 /* MODE_COPY */
  1467. };
  1468. int current_frame_type;
  1469. /* there is a last DC predictor for each of the 3 frame types */
  1470. short last_dc[3];
  1471. int transform = 0;
  1472. vul =
  1473. vu =
  1474. vur =
  1475. vl = 0;
  1476. last_dc[0] =
  1477. last_dc[1] =
  1478. last_dc[2] = 0;
  1479. /* for each fragment row... */
  1480. for (y = 0; y < fragment_height; y++) {
  1481. /* for each fragment in a row... */
  1482. for (x = 0; x < fragment_width; x++, i++) {
  1483. /* reverse prediction if this block was coded */
  1484. if (s->all_fragments[i].coding_method != MODE_COPY) {
  1485. current_frame_type =
  1486. compatible_frame[s->all_fragments[i].coding_method];
  1487. transform = 0;
  1488. if (x) {
  1489. l = i - 1;
  1490. vl = DC_COEFF(l);
  1491. if (COMPATIBLE_FRAME(l))
  1492. transform |= PL;
  1493. }
  1494. if (y) {
  1495. u = i - fragment_width;
  1496. vu = DC_COEFF(u);
  1497. if (COMPATIBLE_FRAME(u))
  1498. transform |= PU;
  1499. if (x) {
  1500. ul = i - fragment_width - 1;
  1501. vul = DC_COEFF(ul);
  1502. if (COMPATIBLE_FRAME(ul))
  1503. transform |= PUL;
  1504. }
  1505. if (x + 1 < fragment_width) {
  1506. ur = i - fragment_width + 1;
  1507. vur = DC_COEFF(ur);
  1508. if (COMPATIBLE_FRAME(ur))
  1509. transform |= PUR;
  1510. }
  1511. }
  1512. if (transform == 0) {
  1513. /* if there were no fragments to predict from, use last
  1514. * DC saved */
  1515. predicted_dc = last_dc[current_frame_type];
  1516. } else {
  1517. /* apply the appropriate predictor transform */
  1518. predicted_dc =
  1519. (predictor_transform[transform][0] * vul) +
  1520. (predictor_transform[transform][1] * vu) +
  1521. (predictor_transform[transform][2] * vur) +
  1522. (predictor_transform[transform][3] * vl);
  1523. predicted_dc /= 128;
  1524. /* check for outranging on the [ul u l] and
  1525. * [ul u ur l] predictors */
  1526. if ((transform == 15) || (transform == 13)) {
  1527. if (FFABS(predicted_dc - vu) > 128)
  1528. predicted_dc = vu;
  1529. else if (FFABS(predicted_dc - vl) > 128)
  1530. predicted_dc = vl;
  1531. else if (FFABS(predicted_dc - vul) > 128)
  1532. predicted_dc = vul;
  1533. }
  1534. }
  1535. /* at long last, apply the predictor */
  1536. DC_COEFF(i) += predicted_dc;
  1537. /* save the DC */
  1538. last_dc[current_frame_type] = DC_COEFF(i);
  1539. }
  1540. }
  1541. }
  1542. }
  1543. static void apply_loop_filter(Vp3DecodeContext *s, int plane,
  1544. int ystart, int yend)
  1545. {
  1546. int x, y;
  1547. int *bounding_values = s->bounding_values_array + 127;
  1548. int width = s->fragment_width[!!plane];
  1549. int height = s->fragment_height[!!plane];
  1550. int fragment = s->fragment_start[plane] + ystart * width;
  1551. ptrdiff_t stride = s->current_frame.f->linesize[plane];
  1552. uint8_t *plane_data = s->current_frame.f->data[plane];
  1553. if (!s->flipped_image)
  1554. stride = -stride;
  1555. plane_data += s->data_offset[plane] + 8 * ystart * stride;
  1556. for (y = ystart; y < yend; y++) {
  1557. for (x = 0; x < width; x++) {
  1558. /* This code basically just deblocks on the edges of coded blocks.
  1559. * However, it has to be much more complicated because of the
  1560. * brain damaged deblock ordering used in VP3/Theora. Order matters
  1561. * because some pixels get filtered twice. */
  1562. if (s->all_fragments[fragment].coding_method != MODE_COPY) {
  1563. /* do not perform left edge filter for left columns frags */
  1564. if (x > 0) {
  1565. s->vp3dsp.h_loop_filter(
  1566. plane_data + 8 * x,
  1567. stride, bounding_values);
  1568. }
  1569. /* do not perform top edge filter for top row fragments */
  1570. if (y > 0) {
  1571. s->vp3dsp.v_loop_filter(
  1572. plane_data + 8 * x,
  1573. stride, bounding_values);
  1574. }
  1575. /* do not perform right edge filter for right column
  1576. * fragments or if right fragment neighbor is also coded
  1577. * in this frame (it will be filtered in next iteration) */
  1578. if ((x < width - 1) &&
  1579. (s->all_fragments[fragment + 1].coding_method == MODE_COPY)) {
  1580. s->vp3dsp.h_loop_filter(
  1581. plane_data + 8 * x + 8,
  1582. stride, bounding_values);
  1583. }
  1584. /* do not perform bottom edge filter for bottom row
  1585. * fragments or if bottom fragment neighbor is also coded
  1586. * in this frame (it will be filtered in the next row) */
  1587. if ((y < height - 1) &&
  1588. (s->all_fragments[fragment + width].coding_method == MODE_COPY)) {
  1589. s->vp3dsp.v_loop_filter(
  1590. plane_data + 8 * x + 8 * stride,
  1591. stride, bounding_values);
  1592. }
  1593. }
  1594. fragment++;
  1595. }
  1596. plane_data += 8 * stride;
  1597. }
  1598. }
  1599. /**
  1600. * Pull DCT tokens from the 64 levels to decode and dequant the coefficients
  1601. * for the next block in coding order
  1602. */
  1603. static inline int vp3_dequant(Vp3DecodeContext *s, Vp3Fragment *frag,
  1604. int plane, int inter, int16_t block[64])
  1605. {
  1606. int16_t *dequantizer = s->qmat[frag->qpi][inter][plane];
  1607. uint8_t *perm = s->idct_scantable;
  1608. int i = 0;
  1609. do {
  1610. int token = *s->dct_tokens[plane][i];
  1611. switch (token & 3) {
  1612. case 0: // EOB
  1613. if (--token < 4) // 0-3 are token types so the EOB run must now be 0
  1614. s->dct_tokens[plane][i]++;
  1615. else
  1616. *s->dct_tokens[plane][i] = token & ~3;
  1617. goto end;
  1618. case 1: // zero run
  1619. s->dct_tokens[plane][i]++;
  1620. i += (token >> 2) & 0x7f;
  1621. if (i > 63) {
  1622. av_log(s->avctx, AV_LOG_ERROR, "Coefficient index overflow\n");
  1623. return i;
  1624. }
  1625. block[perm[i]] = (token >> 9) * dequantizer[perm[i]];
  1626. i++;
  1627. break;
  1628. case 2: // coeff
  1629. block[perm[i]] = (token >> 2) * dequantizer[perm[i]];
  1630. s->dct_tokens[plane][i++]++;
  1631. break;
  1632. default: // shouldn't happen
  1633. return i;
  1634. }
  1635. } while (i < 64);
  1636. // return value is expected to be a valid level
  1637. i--;
  1638. end:
  1639. // the actual DC+prediction is in the fragment structure
  1640. block[0] = frag->dc * s->qmat[0][inter][plane][0];
  1641. return i;
  1642. }
  1643. /**
  1644. * called when all pixels up to row y are complete
  1645. */
  1646. static void vp3_draw_horiz_band(Vp3DecodeContext *s, int y)
  1647. {
  1648. int h, cy, i;
  1649. int offset[AV_NUM_DATA_POINTERS];
  1650. if (HAVE_THREADS && s->avctx->active_thread_type & FF_THREAD_FRAME) {
  1651. int y_flipped = s->flipped_image ? s->height - y : y;
  1652. /* At the end of the frame, report INT_MAX instead of the height of
  1653. * the frame. This makes the other threads' ff_thread_await_progress()
  1654. * calls cheaper, because they don't have to clip their values. */
  1655. ff_thread_report_progress(&s->current_frame,
  1656. y_flipped == s->height ? INT_MAX
  1657. : y_flipped - 1,
  1658. 0);
  1659. }
  1660. if (!s->avctx->draw_horiz_band)
  1661. return;
  1662. h = y - s->last_slice_end;
  1663. s->last_slice_end = y;
  1664. y -= h;
  1665. if (!s->flipped_image)
  1666. y = s->height - y - h;
  1667. cy = y >> s->chroma_y_shift;
  1668. offset[0] = s->current_frame.f->linesize[0] * y;
  1669. offset[1] = s->current_frame.f->linesize[1] * cy;
  1670. offset[2] = s->current_frame.f->linesize[2] * cy;
  1671. for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
  1672. offset[i] = 0;
  1673. emms_c();
  1674. s->avctx->draw_horiz_band(s->avctx, s->current_frame.f, offset, y, 3, h);
  1675. }
  1676. /**
  1677. * Wait for the reference frame of the current fragment.
  1678. * The progress value is in luma pixel rows.
  1679. */
  1680. static void await_reference_row(Vp3DecodeContext *s, Vp3Fragment *fragment,
  1681. int motion_y, int y)
  1682. {
  1683. ThreadFrame *ref_frame;
  1684. int ref_row;
  1685. int border = motion_y & 1;
  1686. if (fragment->coding_method == MODE_USING_GOLDEN ||
  1687. fragment->coding_method == MODE_GOLDEN_MV)
  1688. ref_frame = &s->golden_frame;
  1689. else
  1690. ref_frame = &s->last_frame;
  1691. ref_row = y + (motion_y >> 1);
  1692. ref_row = FFMAX(FFABS(ref_row), ref_row + 8 + border);
  1693. ff_thread_await_progress(ref_frame, ref_row, 0);
  1694. }
  1695. #if CONFIG_VP4_DECODER
  1696. /**
  1697. * @return non-zero if temp (edge_emu_buffer) was populated
  1698. */
  1699. static int vp4_mc_loop_filter(Vp3DecodeContext *s, int plane, int motion_x, int motion_y, int bx, int by,
  1700. uint8_t * motion_source, int stride, int src_x, int src_y, uint8_t *temp)
  1701. {
  1702. int motion_shift = plane ? 4 : 2;
  1703. int subpel_mask = plane ? 3 : 1;
  1704. int *bounding_values = s->bounding_values_array + 127;
  1705. int i;
  1706. int x, y;
  1707. int x2, y2;
  1708. int x_subpel, y_subpel;
  1709. int x_offset, y_offset;
  1710. int block_width = plane ? 8 : 16;
  1711. int plane_width = s->width >> (plane && s->chroma_x_shift);
  1712. int plane_height = s->height >> (plane && s->chroma_y_shift);
  1713. #define loop_stride 12
  1714. uint8_t loop[12 * loop_stride];
  1715. /* using division instead of shift to correctly handle negative values */
  1716. x = 8 * bx + motion_x / motion_shift;
  1717. y = 8 * by + motion_y / motion_shift;
  1718. x_subpel = motion_x & subpel_mask;
  1719. y_subpel = motion_y & subpel_mask;
  1720. if (x_subpel || y_subpel) {
  1721. x--;
  1722. y--;
  1723. if (x_subpel)
  1724. x = FFMIN(x, x + FFSIGN(motion_x));
  1725. if (y_subpel)
  1726. y = FFMIN(y, y + FFSIGN(motion_y));
  1727. x2 = x + block_width;
  1728. y2 = y + block_width;
  1729. if (x2 < 0 || x2 >= plane_width || y2 < 0 || y2 >= plane_height)
  1730. return 0;
  1731. x_offset = (-(x + 2) & 7) + 2;
  1732. y_offset = (-(y + 2) & 7) + 2;
  1733. if (x_offset > 8 + x_subpel && y_offset > 8 + y_subpel)
  1734. return 0;
  1735. s->vdsp.emulated_edge_mc(loop, motion_source - stride - 1,
  1736. loop_stride, stride,
  1737. 12, 12, src_x - 1, src_y - 1,
  1738. plane_width,
  1739. plane_height);
  1740. if (x_offset <= 8 + x_subpel)
  1741. ff_vp3dsp_h_loop_filter_12(loop + x_offset, loop_stride, bounding_values);
  1742. if (y_offset <= 8 + y_subpel)
  1743. ff_vp3dsp_v_loop_filter_12(loop + y_offset*loop_stride, loop_stride, bounding_values);
  1744. } else {
  1745. x_offset = -x & 7;
  1746. y_offset = -y & 7;
  1747. if (!x_offset && !y_offset)
  1748. return 0;
  1749. s->vdsp.emulated_edge_mc(loop, motion_source - stride - 1,
  1750. loop_stride, stride,
  1751. 12, 12, src_x - 1, src_y - 1,
  1752. plane_width,
  1753. plane_height);
  1754. #define safe_loop_filter(name, ptr, stride, bounding_values) \
  1755. if ((uintptr_t)(ptr) & 7) \
  1756. s->vp3dsp.name##_unaligned(ptr, stride, bounding_values); \
  1757. else \
  1758. s->vp3dsp.name(ptr, stride, bounding_values);
  1759. if (x_offset)
  1760. safe_loop_filter(h_loop_filter, loop + loop_stride + x_offset + 1, loop_stride, bounding_values);
  1761. if (y_offset)
  1762. safe_loop_filter(v_loop_filter, loop + (y_offset + 1)*loop_stride + 1, loop_stride, bounding_values);
  1763. }
  1764. for (i = 0; i < 9; i++)
  1765. memcpy(temp + i*stride, loop + (i + 1) * loop_stride + 1, 9);
  1766. return 1;
  1767. }
  1768. #endif
  1769. /*
  1770. * Perform the final rendering for a particular slice of data.
  1771. * The slice number ranges from 0..(c_superblock_height - 1).
  1772. */
  1773. static void render_slice(Vp3DecodeContext *s, int slice)
  1774. {
  1775. int x, y, i, j, fragment;
  1776. int16_t *block = s->block;
  1777. int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
  1778. int motion_halfpel_index;
  1779. uint8_t *motion_source;
  1780. int plane, first_pixel;
  1781. if (slice >= s->c_superblock_height)
  1782. return;
  1783. for (plane = 0; plane < 3; plane++) {
  1784. uint8_t *output_plane = s->current_frame.f->data[plane] +
  1785. s->data_offset[plane];
  1786. uint8_t *last_plane = s->last_frame.f->data[plane] +
  1787. s->data_offset[plane];
  1788. uint8_t *golden_plane = s->golden_frame.f->data[plane] +
  1789. s->data_offset[plane];
  1790. ptrdiff_t stride = s->current_frame.f->linesize[plane];
  1791. int plane_width = s->width >> (plane && s->chroma_x_shift);
  1792. int plane_height = s->height >> (plane && s->chroma_y_shift);
  1793. int8_t(*motion_val)[2] = s->motion_val[!!plane];
  1794. int sb_x, sb_y = slice << (!plane && s->chroma_y_shift);
  1795. int slice_height = sb_y + 1 + (!plane && s->chroma_y_shift);
  1796. int slice_width = plane ? s->c_superblock_width
  1797. : s->y_superblock_width;
  1798. int fragment_width = s->fragment_width[!!plane];
  1799. int fragment_height = s->fragment_height[!!plane];
  1800. int fragment_start = s->fragment_start[plane];
  1801. int do_await = !plane && HAVE_THREADS &&
  1802. (s->avctx->active_thread_type & FF_THREAD_FRAME);
  1803. if (!s->flipped_image)
  1804. stride = -stride;
  1805. if (CONFIG_GRAY && plane && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
  1806. continue;
  1807. /* for each superblock row in the slice (both of them)... */
  1808. for (; sb_y < slice_height; sb_y++) {
  1809. /* for each superblock in a row... */
  1810. for (sb_x = 0; sb_x < slice_width; sb_x++) {
  1811. /* for each block in a superblock... */
  1812. for (j = 0; j < 16; j++) {
  1813. x = 4 * sb_x + hilbert_offset[j][0];
  1814. y = 4 * sb_y + hilbert_offset[j][1];
  1815. fragment = y * fragment_width + x;
  1816. i = fragment_start + fragment;
  1817. // bounds check
  1818. if (x >= fragment_width || y >= fragment_height)
  1819. continue;
  1820. first_pixel = 8 * y * stride + 8 * x;
  1821. if (do_await &&
  1822. s->all_fragments[i].coding_method != MODE_INTRA)
  1823. await_reference_row(s, &s->all_fragments[i],
  1824. motion_val[fragment][1],
  1825. (16 * y) >> s->chroma_y_shift);
  1826. /* transform if this block was coded */
  1827. if (s->all_fragments[i].coding_method != MODE_COPY) {
  1828. if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
  1829. (s->all_fragments[i].coding_method == MODE_GOLDEN_MV))
  1830. motion_source = golden_plane;
  1831. else
  1832. motion_source = last_plane;
  1833. motion_source += first_pixel;
  1834. motion_halfpel_index = 0;
  1835. /* sort out the motion vector if this fragment is coded
  1836. * using a motion vector method */
  1837. if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
  1838. (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) {
  1839. int src_x, src_y;
  1840. int standard_mc = 1;
  1841. motion_x = motion_val[fragment][0];
  1842. motion_y = motion_val[fragment][1];
  1843. #if CONFIG_VP4_DECODER
  1844. if (plane && s->version >= 2) {
  1845. motion_x = (motion_x >> 1) | (motion_x & 1);
  1846. motion_y = (motion_y >> 1) | (motion_y & 1);
  1847. }
  1848. #endif
  1849. src_x = (motion_x >> 1) + 8 * x;
  1850. src_y = (motion_y >> 1) + 8 * y;
  1851. motion_halfpel_index = motion_x & 0x01;
  1852. motion_source += (motion_x >> 1);
  1853. motion_halfpel_index |= (motion_y & 0x01) << 1;
  1854. motion_source += ((motion_y >> 1) * stride);
  1855. #if CONFIG_VP4_DECODER
  1856. if (s->version >= 2) {
  1857. uint8_t *temp = s->edge_emu_buffer;
  1858. if (stride < 0)
  1859. temp -= 8 * stride;
  1860. if (vp4_mc_loop_filter(s, plane, motion_val[fragment][0], motion_val[fragment][1], x, y, motion_source, stride, src_x, src_y, temp)) {
  1861. motion_source = temp;
  1862. standard_mc = 0;
  1863. }
  1864. }
  1865. #endif
  1866. if (standard_mc && (
  1867. src_x < 0 || src_y < 0 ||
  1868. src_x + 9 >= plane_width ||
  1869. src_y + 9 >= plane_height)) {
  1870. uint8_t *temp = s->edge_emu_buffer;
  1871. if (stride < 0)
  1872. temp -= 8 * stride;
  1873. s->vdsp.emulated_edge_mc(temp, motion_source,
  1874. stride, stride,
  1875. 9, 9, src_x, src_y,
  1876. plane_width,
  1877. plane_height);
  1878. motion_source = temp;
  1879. }
  1880. }
  1881. /* first, take care of copying a block from either the
  1882. * previous or the golden frame */
  1883. if (s->all_fragments[i].coding_method != MODE_INTRA) {
  1884. /* Note, it is possible to implement all MC cases
  1885. * with put_no_rnd_pixels_l2 which would look more
  1886. * like the VP3 source but this would be slower as
  1887. * put_no_rnd_pixels_tab is better optimized */
  1888. if (motion_halfpel_index != 3) {
  1889. s->hdsp.put_no_rnd_pixels_tab[1][motion_halfpel_index](
  1890. output_plane + first_pixel,
  1891. motion_source, stride, 8);
  1892. } else {
  1893. /* d is 0 if motion_x and _y have the same sign,
  1894. * else -1 */
  1895. int d = (motion_x ^ motion_y) >> 31;
  1896. s->vp3dsp.put_no_rnd_pixels_l2(output_plane + first_pixel,
  1897. motion_source - d,
  1898. motion_source + stride + 1 + d,
  1899. stride, 8);
  1900. }
  1901. }
  1902. /* invert DCT and place (or add) in final output */
  1903. if (s->all_fragments[i].coding_method == MODE_INTRA) {
  1904. vp3_dequant(s, s->all_fragments + i,
  1905. plane, 0, block);
  1906. s->vp3dsp.idct_put(output_plane + first_pixel,
  1907. stride,
  1908. block);
  1909. } else {
  1910. if (vp3_dequant(s, s->all_fragments + i,
  1911. plane, 1, block)) {
  1912. s->vp3dsp.idct_add(output_plane + first_pixel,
  1913. stride,
  1914. block);
  1915. } else {
  1916. s->vp3dsp.idct_dc_add(output_plane + first_pixel,
  1917. stride, block);
  1918. }
  1919. }
  1920. } else {
  1921. /* copy directly from the previous frame */
  1922. s->hdsp.put_pixels_tab[1][0](
  1923. output_plane + first_pixel,
  1924. last_plane + first_pixel,
  1925. stride, 8);
  1926. }
  1927. }
  1928. }
  1929. // Filter up to the last row in the superblock row
  1930. if (s->version < 2 && !s->skip_loop_filter)
  1931. apply_loop_filter(s, plane, 4 * sb_y - !!sb_y,
  1932. FFMIN(4 * sb_y + 3, fragment_height - 1));
  1933. }
  1934. }
  1935. /* this looks like a good place for slice dispatch... */
  1936. /* algorithm:
  1937. * if (slice == s->macroblock_height - 1)
  1938. * dispatch (both last slice & 2nd-to-last slice);
  1939. * else if (slice > 0)
  1940. * dispatch (slice - 1);
  1941. */
  1942. vp3_draw_horiz_band(s, FFMIN((32 << s->chroma_y_shift) * (slice + 1) - 16,
  1943. s->height - 16));
  1944. }
  1945. /// Allocate tables for per-frame data in Vp3DecodeContext
  1946. static av_cold int allocate_tables(AVCodecContext *avctx)
  1947. {
  1948. Vp3DecodeContext *s = avctx->priv_data;
  1949. int y_fragment_count, c_fragment_count;
  1950. free_tables(avctx);
  1951. y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
  1952. c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
  1953. /* superblock_coding is used by unpack_superblocks (VP3/Theora) and vp4_unpack_macroblocks (VP4) */
  1954. s->superblock_coding = av_mallocz(FFMAX(s->superblock_count, s->yuv_macroblock_count));
  1955. s->all_fragments = av_mallocz_array(s->fragment_count, sizeof(Vp3Fragment));
  1956. s-> kf_coded_fragment_list = av_mallocz_array(s->fragment_count, sizeof(int));
  1957. s->nkf_coded_fragment_list = av_mallocz_array(s->fragment_count, sizeof(int));
  1958. memset(s-> num_kf_coded_fragment, -1, sizeof(s-> num_kf_coded_fragment));
  1959. s->dct_tokens_base = av_mallocz_array(s->fragment_count,
  1960. 64 * sizeof(*s->dct_tokens_base));
  1961. s->motion_val[0] = av_mallocz_array(y_fragment_count, sizeof(*s->motion_val[0]));
  1962. s->motion_val[1] = av_mallocz_array(c_fragment_count, sizeof(*s->motion_val[1]));
  1963. /* work out the block mapping tables */
  1964. s->superblock_fragments = av_mallocz_array(s->superblock_count, 16 * sizeof(int));
  1965. s->macroblock_coding = av_mallocz(s->macroblock_count + 1);
  1966. s->dc_pred_row = av_malloc_array(s->y_superblock_width * 4, sizeof(*s->dc_pred_row));
  1967. if (!s->superblock_coding || !s->all_fragments ||
  1968. !s->dct_tokens_base || !s->kf_coded_fragment_list ||
  1969. !s->nkf_coded_fragment_list ||
  1970. !s->superblock_fragments || !s->macroblock_coding ||
  1971. !s->dc_pred_row ||
  1972. !s->motion_val[0] || !s->motion_val[1]) {
  1973. vp3_decode_end(avctx);
  1974. return -1;
  1975. }
  1976. init_block_mapping(s);
  1977. return 0;
  1978. }
  1979. static av_cold int init_frames(Vp3DecodeContext *s)
  1980. {
  1981. s->current_frame.f = av_frame_alloc();
  1982. s->last_frame.f = av_frame_alloc();
  1983. s->golden_frame.f = av_frame_alloc();
  1984. if (!s->current_frame.f || !s->last_frame.f || !s->golden_frame.f) {
  1985. av_frame_free(&s->current_frame.f);
  1986. av_frame_free(&s->last_frame.f);
  1987. av_frame_free(&s->golden_frame.f);
  1988. return AVERROR(ENOMEM);
  1989. }
  1990. return 0;
  1991. }
  1992. static av_cold int vp3_decode_init(AVCodecContext *avctx)
  1993. {
  1994. Vp3DecodeContext *s = avctx->priv_data;
  1995. int i, inter, plane, ret;
  1996. int c_width;
  1997. int c_height;
  1998. int y_fragment_count, c_fragment_count;
  1999. #if CONFIG_VP4_DECODER
  2000. int j;
  2001. #endif
  2002. ret = init_frames(s);
  2003. if (ret < 0)
  2004. return ret;
  2005. if (avctx->codec_tag == MKTAG('V', 'P', '4', '0'))
  2006. s->version = 3;
  2007. else if (avctx->codec_tag == MKTAG('V', 'P', '3', '0'))
  2008. s->version = 0;
  2009. else
  2010. s->version = 1;
  2011. s->avctx = avctx;
  2012. s->width = FFALIGN(avctx->coded_width, 16);
  2013. s->height = FFALIGN(avctx->coded_height, 16);
  2014. if (avctx->codec_id != AV_CODEC_ID_THEORA)
  2015. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  2016. avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
  2017. ff_hpeldsp_init(&s->hdsp, avctx->flags | AV_CODEC_FLAG_BITEXACT);
  2018. ff_videodsp_init(&s->vdsp, 8);
  2019. ff_vp3dsp_init(&s->vp3dsp, avctx->flags);
  2020. for (i = 0; i < 64; i++) {
  2021. #define TRANSPOSE(x) (((x) >> 3) | (((x) & 7) << 3))
  2022. s->idct_permutation[i] = TRANSPOSE(i);
  2023. s->idct_scantable[i] = TRANSPOSE(ff_zigzag_direct[i]);
  2024. #undef TRANSPOSE
  2025. }
  2026. /* initialize to an impossible value which will force a recalculation
  2027. * in the first frame decode */
  2028. for (i = 0; i < 3; i++)
  2029. s->qps[i] = -1;
  2030. ret = av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift);
  2031. if (ret)
  2032. return ret;
  2033. s->y_superblock_width = (s->width + 31) / 32;
  2034. s->y_superblock_height = (s->height + 31) / 32;
  2035. s->y_superblock_count = s->y_superblock_width * s->y_superblock_height;
  2036. /* work out the dimensions for the C planes */
  2037. c_width = s->width >> s->chroma_x_shift;
  2038. c_height = s->height >> s->chroma_y_shift;
  2039. s->c_superblock_width = (c_width + 31) / 32;
  2040. s->c_superblock_height = (c_height + 31) / 32;
  2041. s->c_superblock_count = s->c_superblock_width * s->c_superblock_height;
  2042. s->superblock_count = s->y_superblock_count + (s->c_superblock_count * 2);
  2043. s->u_superblock_start = s->y_superblock_count;
  2044. s->v_superblock_start = s->u_superblock_start + s->c_superblock_count;
  2045. s->macroblock_width = (s->width + 15) / 16;
  2046. s->macroblock_height = (s->height + 15) / 16;
  2047. s->macroblock_count = s->macroblock_width * s->macroblock_height;
  2048. s->c_macroblock_width = (c_width + 15) / 16;
  2049. s->c_macroblock_height = (c_height + 15) / 16;
  2050. s->c_macroblock_count = s->c_macroblock_width * s->c_macroblock_height;
  2051. s->yuv_macroblock_count = s->macroblock_count + 2 * s->c_macroblock_count;
  2052. s->fragment_width[0] = s->width / FRAGMENT_PIXELS;
  2053. s->fragment_height[0] = s->height / FRAGMENT_PIXELS;
  2054. s->fragment_width[1] = s->fragment_width[0] >> s->chroma_x_shift;
  2055. s->fragment_height[1] = s->fragment_height[0] >> s->chroma_y_shift;
  2056. /* fragment count covers all 8x8 blocks for all 3 planes */
  2057. y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
  2058. c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
  2059. s->fragment_count = y_fragment_count + 2 * c_fragment_count;
  2060. s->fragment_start[1] = y_fragment_count;
  2061. s->fragment_start[2] = y_fragment_count + c_fragment_count;
  2062. if (!s->theora_tables) {
  2063. for (i = 0; i < 64; i++) {
  2064. s->coded_dc_scale_factor[0][i] = s->version < 2 ? vp31_dc_scale_factor[i] : vp4_y_dc_scale_factor[i];
  2065. s->coded_dc_scale_factor[1][i] = s->version < 2 ? vp31_dc_scale_factor[i] : vp4_uv_dc_scale_factor[i];
  2066. s->coded_ac_scale_factor[i] = s->version < 2 ? vp31_ac_scale_factor[i] : vp4_ac_scale_factor[i];
  2067. s->base_matrix[0][i] = s->version < 2 ? vp31_intra_y_dequant[i] : vp4_generic_dequant[i];
  2068. s->base_matrix[1][i] = s->version < 2 ? vp31_intra_c_dequant[i] : vp4_generic_dequant[i];
  2069. s->base_matrix[2][i] = s->version < 2 ? vp31_inter_dequant[i] : vp4_generic_dequant[i];
  2070. s->filter_limit_values[i] = s->version < 2 ? vp31_filter_limit_values[i] : vp4_filter_limit_values[i];
  2071. }
  2072. for (inter = 0; inter < 2; inter++) {
  2073. for (plane = 0; plane < 3; plane++) {
  2074. s->qr_count[inter][plane] = 1;
  2075. s->qr_size[inter][plane][0] = 63;
  2076. s->qr_base[inter][plane][0] =
  2077. s->qr_base[inter][plane][1] = 2 * inter + (!!plane) * !inter;
  2078. }
  2079. }
  2080. /* init VLC tables */
  2081. if (s->version < 2) {
  2082. for (i = 0; i < 16; i++) {
  2083. /* DC histograms */
  2084. init_vlc(&s->dc_vlc[i], 11, 32,
  2085. &dc_bias[i][0][1], 4, 2,
  2086. &dc_bias[i][0][0], 4, 2, 0);
  2087. /* group 1 AC histograms */
  2088. init_vlc(&s->ac_vlc_1[i], 11, 32,
  2089. &ac_bias_0[i][0][1], 4, 2,
  2090. &ac_bias_0[i][0][0], 4, 2, 0);
  2091. /* group 2 AC histograms */
  2092. init_vlc(&s->ac_vlc_2[i], 11, 32,
  2093. &ac_bias_1[i][0][1], 4, 2,
  2094. &ac_bias_1[i][0][0], 4, 2, 0);
  2095. /* group 3 AC histograms */
  2096. init_vlc(&s->ac_vlc_3[i], 11, 32,
  2097. &ac_bias_2[i][0][1], 4, 2,
  2098. &ac_bias_2[i][0][0], 4, 2, 0);
  2099. /* group 4 AC histograms */
  2100. init_vlc(&s->ac_vlc_4[i], 11, 32,
  2101. &ac_bias_3[i][0][1], 4, 2,
  2102. &ac_bias_3[i][0][0], 4, 2, 0);
  2103. }
  2104. #if CONFIG_VP4_DECODER
  2105. } else { /* version >= 2 */
  2106. for (i = 0; i < 16; i++) {
  2107. /* DC histograms */
  2108. init_vlc(&s->dc_vlc[i], 11, 32,
  2109. &vp4_dc_bias[i][0][1], 4, 2,
  2110. &vp4_dc_bias[i][0][0], 4, 2, 0);
  2111. /* group 1 AC histograms */
  2112. init_vlc(&s->ac_vlc_1[i], 11, 32,
  2113. &vp4_ac_bias_0[i][0][1], 4, 2,
  2114. &vp4_ac_bias_0[i][0][0], 4, 2, 0);
  2115. /* group 2 AC histograms */
  2116. init_vlc(&s->ac_vlc_2[i], 11, 32,
  2117. &vp4_ac_bias_1[i][0][1], 4, 2,
  2118. &vp4_ac_bias_1[i][0][0], 4, 2, 0);
  2119. /* group 3 AC histograms */
  2120. init_vlc(&s->ac_vlc_3[i], 11, 32,
  2121. &vp4_ac_bias_2[i][0][1], 4, 2,
  2122. &vp4_ac_bias_2[i][0][0], 4, 2, 0);
  2123. /* group 4 AC histograms */
  2124. init_vlc(&s->ac_vlc_4[i], 11, 32,
  2125. &vp4_ac_bias_3[i][0][1], 4, 2,
  2126. &vp4_ac_bias_3[i][0][0], 4, 2, 0);
  2127. }
  2128. #endif
  2129. }
  2130. } else {
  2131. for (i = 0; i < 16; i++) {
  2132. /* DC histograms */
  2133. if (init_vlc(&s->dc_vlc[i], 11, 32,
  2134. &s->huffman_table[i][0][1], 8, 4,
  2135. &s->huffman_table[i][0][0], 8, 4, 0) < 0)
  2136. goto vlc_fail;
  2137. /* group 1 AC histograms */
  2138. if (init_vlc(&s->ac_vlc_1[i], 11, 32,
  2139. &s->huffman_table[i + 16][0][1], 8, 4,
  2140. &s->huffman_table[i + 16][0][0], 8, 4, 0) < 0)
  2141. goto vlc_fail;
  2142. /* group 2 AC histograms */
  2143. if (init_vlc(&s->ac_vlc_2[i], 11, 32,
  2144. &s->huffman_table[i + 16 * 2][0][1], 8, 4,
  2145. &s->huffman_table[i + 16 * 2][0][0], 8, 4, 0) < 0)
  2146. goto vlc_fail;
  2147. /* group 3 AC histograms */
  2148. if (init_vlc(&s->ac_vlc_3[i], 11, 32,
  2149. &s->huffman_table[i + 16 * 3][0][1], 8, 4,
  2150. &s->huffman_table[i + 16 * 3][0][0], 8, 4, 0) < 0)
  2151. goto vlc_fail;
  2152. /* group 4 AC histograms */
  2153. if (init_vlc(&s->ac_vlc_4[i], 11, 32,
  2154. &s->huffman_table[i + 16 * 4][0][1], 8, 4,
  2155. &s->huffman_table[i + 16 * 4][0][0], 8, 4, 0) < 0)
  2156. goto vlc_fail;
  2157. }
  2158. }
  2159. init_vlc(&s->superblock_run_length_vlc, 6, 34,
  2160. &superblock_run_length_vlc_table[0][1], 4, 2,
  2161. &superblock_run_length_vlc_table[0][0], 4, 2, 0);
  2162. init_vlc(&s->fragment_run_length_vlc, 5, 30,
  2163. &fragment_run_length_vlc_table[0][1], 4, 2,
  2164. &fragment_run_length_vlc_table[0][0], 4, 2, 0);
  2165. init_vlc(&s->mode_code_vlc, 3, 8,
  2166. &mode_code_vlc_table[0][1], 2, 1,
  2167. &mode_code_vlc_table[0][0], 2, 1, 0);
  2168. init_vlc(&s->motion_vector_vlc, 6, 63,
  2169. &motion_vector_vlc_table[0][1], 2, 1,
  2170. &motion_vector_vlc_table[0][0], 2, 1, 0);
  2171. #if CONFIG_VP4_DECODER
  2172. for (j = 0; j < 2; j++)
  2173. for (i = 0; i < 7; i++)
  2174. init_vlc(&s->vp4_mv_vlc[j][i], 6, 63,
  2175. &vp4_mv_vlc[j][i][0][1], 4, 2,
  2176. &vp4_mv_vlc[j][i][0][0], 4, 2, 0);
  2177. /* version >= 2 */
  2178. for (i = 0; i < 2; i++)
  2179. init_vlc(&s->block_pattern_vlc[i], 3, 14,
  2180. &vp4_block_pattern_vlc[i][0][1], 2, 1,
  2181. &vp4_block_pattern_vlc[i][0][0], 2, 1, 0);
  2182. #endif
  2183. return allocate_tables(avctx);
  2184. vlc_fail:
  2185. av_log(avctx, AV_LOG_FATAL, "Invalid huffman table\n");
  2186. return -1;
  2187. }
  2188. /// Release and shuffle frames after decode finishes
  2189. static int update_frames(AVCodecContext *avctx)
  2190. {
  2191. Vp3DecodeContext *s = avctx->priv_data;
  2192. int ret = 0;
  2193. /* shuffle frames (last = current) */
  2194. ff_thread_release_buffer(avctx, &s->last_frame);
  2195. ret = ff_thread_ref_frame(&s->last_frame, &s->current_frame);
  2196. if (ret < 0)
  2197. goto fail;
  2198. if (s->keyframe) {
  2199. ff_thread_release_buffer(avctx, &s->golden_frame);
  2200. ret = ff_thread_ref_frame(&s->golden_frame, &s->current_frame);
  2201. }
  2202. fail:
  2203. ff_thread_release_buffer(avctx, &s->current_frame);
  2204. return ret;
  2205. }
  2206. #if HAVE_THREADS
  2207. static int ref_frame(Vp3DecodeContext *s, ThreadFrame *dst, ThreadFrame *src)
  2208. {
  2209. ff_thread_release_buffer(s->avctx, dst);
  2210. if (src->f->data[0])
  2211. return ff_thread_ref_frame(dst, src);
  2212. return 0;
  2213. }
  2214. static int ref_frames(Vp3DecodeContext *dst, Vp3DecodeContext *src)
  2215. {
  2216. int ret;
  2217. if ((ret = ref_frame(dst, &dst->current_frame, &src->current_frame)) < 0 ||
  2218. (ret = ref_frame(dst, &dst->golden_frame, &src->golden_frame)) < 0 ||
  2219. (ret = ref_frame(dst, &dst->last_frame, &src->last_frame)) < 0)
  2220. return ret;
  2221. return 0;
  2222. }
  2223. static int vp3_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
  2224. {
  2225. Vp3DecodeContext *s = dst->priv_data, *s1 = src->priv_data;
  2226. int qps_changed = 0, i, err;
  2227. if (!s1->current_frame.f->data[0] ||
  2228. s->width != s1->width || s->height != s1->height) {
  2229. if (s != s1)
  2230. ref_frames(s, s1);
  2231. return -1;
  2232. }
  2233. if (s != s1) {
  2234. // copy previous frame data
  2235. if ((err = ref_frames(s, s1)) < 0)
  2236. return err;
  2237. s->keyframe = s1->keyframe;
  2238. // copy qscale data if necessary
  2239. for (i = 0; i < 3; i++) {
  2240. if (s->qps[i] != s1->qps[1]) {
  2241. qps_changed = 1;
  2242. memcpy(&s->qmat[i], &s1->qmat[i], sizeof(s->qmat[i]));
  2243. }
  2244. }
  2245. if (s->qps[0] != s1->qps[0])
  2246. memcpy(&s->bounding_values_array, &s1->bounding_values_array,
  2247. sizeof(s->bounding_values_array));
  2248. if (qps_changed) {
  2249. memcpy(s->qps, s1->qps, sizeof(s->qps));
  2250. memcpy(s->last_qps, s1->last_qps, sizeof(s->last_qps));
  2251. s->nqps = s1->nqps;
  2252. }
  2253. }
  2254. return update_frames(dst);
  2255. }
  2256. #endif
  2257. static int vp3_decode_frame(AVCodecContext *avctx,
  2258. void *data, int *got_frame,
  2259. AVPacket *avpkt)
  2260. {
  2261. AVFrame *frame = data;
  2262. const uint8_t *buf = avpkt->data;
  2263. int buf_size = avpkt->size;
  2264. Vp3DecodeContext *s = avctx->priv_data;
  2265. GetBitContext gb;
  2266. int i, ret;
  2267. if ((ret = init_get_bits8(&gb, buf, buf_size)) < 0)
  2268. return ret;
  2269. #if CONFIG_THEORA_DECODER
  2270. if (s->theora && get_bits1(&gb)) {
  2271. int type = get_bits(&gb, 7);
  2272. skip_bits_long(&gb, 6*8); /* "theora" */
  2273. if (s->avctx->active_thread_type&FF_THREAD_FRAME) {
  2274. av_log(avctx, AV_LOG_ERROR, "midstream reconfiguration with multithreading is unsupported, try -threads 1\n");
  2275. return AVERROR_PATCHWELCOME;
  2276. }
  2277. if (type == 0) {
  2278. vp3_decode_end(avctx);
  2279. ret = theora_decode_header(avctx, &gb);
  2280. if (ret >= 0)
  2281. ret = vp3_decode_init(avctx);
  2282. if (ret < 0) {
  2283. vp3_decode_end(avctx);
  2284. return ret;
  2285. }
  2286. return buf_size;
  2287. } else if (type == 2) {
  2288. vp3_decode_end(avctx);
  2289. ret = theora_decode_tables(avctx, &gb);
  2290. if (ret >= 0)
  2291. ret = vp3_decode_init(avctx);
  2292. if (ret < 0) {
  2293. vp3_decode_end(avctx);
  2294. return ret;
  2295. }
  2296. return buf_size;
  2297. }
  2298. av_log(avctx, AV_LOG_ERROR,
  2299. "Header packet passed to frame decoder, skipping\n");
  2300. return -1;
  2301. }
  2302. #endif
  2303. s->keyframe = !get_bits1(&gb);
  2304. if (!s->all_fragments) {
  2305. av_log(avctx, AV_LOG_ERROR, "Data packet without prior valid headers\n");
  2306. return -1;
  2307. }
  2308. if (!s->theora)
  2309. skip_bits(&gb, 1);
  2310. for (i = 0; i < 3; i++)
  2311. s->last_qps[i] = s->qps[i];
  2312. s->nqps = 0;
  2313. do {
  2314. s->qps[s->nqps++] = get_bits(&gb, 6);
  2315. } while (s->theora >= 0x030200 && s->nqps < 3 && get_bits1(&gb));
  2316. for (i = s->nqps; i < 3; i++)
  2317. s->qps[i] = -1;
  2318. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  2319. av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n",
  2320. s->keyframe ? "key" : "", avctx->frame_number + 1, s->qps[0]);
  2321. s->skip_loop_filter = !s->filter_limit_values[s->qps[0]] ||
  2322. avctx->skip_loop_filter >= (s->keyframe ? AVDISCARD_ALL
  2323. : AVDISCARD_NONKEY);
  2324. if (s->qps[0] != s->last_qps[0])
  2325. init_loop_filter(s);
  2326. for (i = 0; i < s->nqps; i++)
  2327. // reinit all dequantizers if the first one changed, because
  2328. // the DC of the first quantizer must be used for all matrices
  2329. if (s->qps[i] != s->last_qps[i] || s->qps[0] != s->last_qps[0])
  2330. init_dequantizer(s, i);
  2331. if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe)
  2332. return buf_size;
  2333. s->current_frame.f->pict_type = s->keyframe ? AV_PICTURE_TYPE_I
  2334. : AV_PICTURE_TYPE_P;
  2335. s->current_frame.f->key_frame = s->keyframe;
  2336. if ((ret = ff_thread_get_buffer(avctx, &s->current_frame, AV_GET_BUFFER_FLAG_REF)) < 0)
  2337. goto error;
  2338. if (!s->edge_emu_buffer)
  2339. s->edge_emu_buffer = av_malloc(9 * FFABS(s->current_frame.f->linesize[0]));
  2340. if (s->keyframe) {
  2341. if (!s->theora) {
  2342. skip_bits(&gb, 4); /* width code */
  2343. skip_bits(&gb, 4); /* height code */
  2344. if (s->version) {
  2345. s->version = get_bits(&gb, 5);
  2346. if (avctx->frame_number == 0)
  2347. av_log(s->avctx, AV_LOG_DEBUG,
  2348. "VP version: %d\n", s->version);
  2349. }
  2350. }
  2351. if (s->version || s->theora) {
  2352. if (get_bits1(&gb))
  2353. av_log(s->avctx, AV_LOG_ERROR,
  2354. "Warning, unsupported keyframe coding type?!\n");
  2355. skip_bits(&gb, 2); /* reserved? */
  2356. #if CONFIG_VP4_DECODER
  2357. if (s->version >= 2) {
  2358. int mb_height, mb_width;
  2359. int mb_width_mul, mb_width_div, mb_height_mul, mb_height_div;
  2360. mb_height = get_bits(&gb, 8);
  2361. mb_width = get_bits(&gb, 8);
  2362. if (mb_height != s->macroblock_height ||
  2363. mb_width != s->macroblock_width)
  2364. avpriv_request_sample(s->avctx, "macroblock dimension mismatch");
  2365. mb_width_mul = get_bits(&gb, 5);
  2366. mb_width_div = get_bits(&gb, 3);
  2367. mb_height_mul = get_bits(&gb, 5);
  2368. mb_height_div = get_bits(&gb, 3);
  2369. if (mb_width_mul != 1 || mb_width_div != 1 || mb_height_mul != 1 || mb_height_div != 1)
  2370. avpriv_request_sample(s->avctx, "unexpected macroblock dimension multipler/divider");
  2371. if (get_bits(&gb, 2))
  2372. avpriv_request_sample(s->avctx, "unknown bits");
  2373. }
  2374. #endif
  2375. }
  2376. } else {
  2377. if (!s->golden_frame.f->data[0]) {
  2378. av_log(s->avctx, AV_LOG_WARNING,
  2379. "vp3: first frame not a keyframe\n");
  2380. s->golden_frame.f->pict_type = AV_PICTURE_TYPE_I;
  2381. if ((ret = ff_thread_get_buffer(avctx, &s->golden_frame,
  2382. AV_GET_BUFFER_FLAG_REF)) < 0)
  2383. goto error;
  2384. ff_thread_release_buffer(avctx, &s->last_frame);
  2385. if ((ret = ff_thread_ref_frame(&s->last_frame,
  2386. &s->golden_frame)) < 0)
  2387. goto error;
  2388. ff_thread_report_progress(&s->last_frame, INT_MAX, 0);
  2389. }
  2390. }
  2391. memset(s->all_fragments, 0, s->fragment_count * sizeof(Vp3Fragment));
  2392. ff_thread_finish_setup(avctx);
  2393. if (s->version < 2) {
  2394. if ((ret = unpack_superblocks(s, &gb)) < 0) {
  2395. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n");
  2396. goto error;
  2397. }
  2398. #if CONFIG_VP4_DECODER
  2399. } else {
  2400. if ((ret = vp4_unpack_macroblocks(s, &gb)) < 0) {
  2401. av_log(s->avctx, AV_LOG_ERROR, "error in vp4_unpack_macroblocks\n");
  2402. goto error;
  2403. }
  2404. #endif
  2405. }
  2406. if ((ret = unpack_modes(s, &gb)) < 0) {
  2407. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n");
  2408. goto error;
  2409. }
  2410. if (ret = unpack_vectors(s, &gb)) {
  2411. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n");
  2412. goto error;
  2413. }
  2414. if ((ret = unpack_block_qpis(s, &gb)) < 0) {
  2415. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_block_qpis\n");
  2416. goto error;
  2417. }
  2418. if (s->version < 2) {
  2419. if ((ret = unpack_dct_coeffs(s, &gb)) < 0) {
  2420. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n");
  2421. goto error;
  2422. }
  2423. #if CONFIG_VP4_DECODER
  2424. } else {
  2425. if ((ret = vp4_unpack_dct_coeffs(s, &gb)) < 0) {
  2426. av_log(s->avctx, AV_LOG_ERROR, "error in vp4_unpack_dct_coeffs\n");
  2427. goto error;
  2428. }
  2429. #endif
  2430. }
  2431. for (i = 0; i < 3; i++) {
  2432. int height = s->height >> (i && s->chroma_y_shift);
  2433. if (s->flipped_image)
  2434. s->data_offset[i] = 0;
  2435. else
  2436. s->data_offset[i] = (height - 1) * s->current_frame.f->linesize[i];
  2437. }
  2438. s->last_slice_end = 0;
  2439. for (i = 0; i < s->c_superblock_height; i++)
  2440. render_slice(s, i);
  2441. // filter the last row
  2442. if (s->version < 2)
  2443. for (i = 0; i < 3; i++) {
  2444. int row = (s->height >> (3 + (i && s->chroma_y_shift))) - 1;
  2445. apply_loop_filter(s, i, row, row + 1);
  2446. }
  2447. vp3_draw_horiz_band(s, s->height);
  2448. /* output frame, offset as needed */
  2449. if ((ret = av_frame_ref(data, s->current_frame.f)) < 0)
  2450. return ret;
  2451. frame->crop_left = s->offset_x;
  2452. frame->crop_right = avctx->coded_width - avctx->width - s->offset_x;
  2453. frame->crop_top = s->offset_y;
  2454. frame->crop_bottom = avctx->coded_height - avctx->height - s->offset_y;
  2455. *got_frame = 1;
  2456. if (!HAVE_THREADS || !(s->avctx->active_thread_type & FF_THREAD_FRAME)) {
  2457. ret = update_frames(avctx);
  2458. if (ret < 0)
  2459. return ret;
  2460. }
  2461. return buf_size;
  2462. error:
  2463. ff_thread_report_progress(&s->current_frame, INT_MAX, 0);
  2464. if (!HAVE_THREADS || !(s->avctx->active_thread_type & FF_THREAD_FRAME))
  2465. av_frame_unref(s->current_frame.f);
  2466. return ret;
  2467. }
  2468. static int read_huffman_tree(AVCodecContext *avctx, GetBitContext *gb)
  2469. {
  2470. Vp3DecodeContext *s = avctx->priv_data;
  2471. if (get_bits1(gb)) {
  2472. int token;
  2473. if (s->entries >= 32) { /* overflow */
  2474. av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
  2475. return -1;
  2476. }
  2477. token = get_bits(gb, 5);
  2478. ff_dlog(avctx, "hti %d hbits %x token %d entry : %d size %d\n",
  2479. s->hti, s->hbits, token, s->entries, s->huff_code_size);
  2480. s->huffman_table[s->hti][token][0] = s->hbits;
  2481. s->huffman_table[s->hti][token][1] = s->huff_code_size;
  2482. s->entries++;
  2483. } else {
  2484. if (s->huff_code_size >= 32) { /* overflow */
  2485. av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
  2486. return -1;
  2487. }
  2488. s->huff_code_size++;
  2489. s->hbits <<= 1;
  2490. if (read_huffman_tree(avctx, gb))
  2491. return -1;
  2492. s->hbits |= 1;
  2493. if (read_huffman_tree(avctx, gb))
  2494. return -1;
  2495. s->hbits >>= 1;
  2496. s->huff_code_size--;
  2497. }
  2498. return 0;
  2499. }
  2500. #if CONFIG_THEORA_DECODER
  2501. static const enum AVPixelFormat theora_pix_fmts[4] = {
  2502. AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P
  2503. };
  2504. static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb)
  2505. {
  2506. Vp3DecodeContext *s = avctx->priv_data;
  2507. int visible_width, visible_height, colorspace;
  2508. uint8_t offset_x = 0, offset_y = 0;
  2509. int ret;
  2510. AVRational fps, aspect;
  2511. s->theora_header = 0;
  2512. s->theora = get_bits(gb, 24);
  2513. av_log(avctx, AV_LOG_DEBUG, "Theora bitstream version %X\n", s->theora);
  2514. if (!s->theora) {
  2515. s->theora = 1;
  2516. avpriv_request_sample(s->avctx, "theora 0");
  2517. }
  2518. /* 3.2.0 aka alpha3 has the same frame orientation as original vp3
  2519. * but previous versions have the image flipped relative to vp3 */
  2520. if (s->theora < 0x030200) {
  2521. s->flipped_image = 1;
  2522. av_log(avctx, AV_LOG_DEBUG,
  2523. "Old (<alpha3) Theora bitstream, flipped image\n");
  2524. }
  2525. visible_width =
  2526. s->width = get_bits(gb, 16) << 4;
  2527. visible_height =
  2528. s->height = get_bits(gb, 16) << 4;
  2529. if (s->theora >= 0x030200) {
  2530. visible_width = get_bits(gb, 24);
  2531. visible_height = get_bits(gb, 24);
  2532. offset_x = get_bits(gb, 8); /* offset x */
  2533. offset_y = get_bits(gb, 8); /* offset y, from bottom */
  2534. }
  2535. /* sanity check */
  2536. if (av_image_check_size(visible_width, visible_height, 0, avctx) < 0 ||
  2537. visible_width + offset_x > s->width ||
  2538. visible_height + offset_y > s->height) {
  2539. av_log(avctx, AV_LOG_ERROR,
  2540. "Invalid frame dimensions - w:%d h:%d x:%d y:%d (%dx%d).\n",
  2541. visible_width, visible_height, offset_x, offset_y,
  2542. s->width, s->height);
  2543. return AVERROR_INVALIDDATA;
  2544. }
  2545. fps.num = get_bits_long(gb, 32);
  2546. fps.den = get_bits_long(gb, 32);
  2547. if (fps.num && fps.den) {
  2548. if (fps.num < 0 || fps.den < 0) {
  2549. av_log(avctx, AV_LOG_ERROR, "Invalid framerate\n");
  2550. return AVERROR_INVALIDDATA;
  2551. }
  2552. av_reduce(&avctx->framerate.den, &avctx->framerate.num,
  2553. fps.den, fps.num, 1 << 30);
  2554. }
  2555. aspect.num = get_bits(gb, 24);
  2556. aspect.den = get_bits(gb, 24);
  2557. if (aspect.num && aspect.den) {
  2558. av_reduce(&avctx->sample_aspect_ratio.num,
  2559. &avctx->sample_aspect_ratio.den,
  2560. aspect.num, aspect.den, 1 << 30);
  2561. ff_set_sar(avctx, avctx->sample_aspect_ratio);
  2562. }
  2563. if (s->theora < 0x030200)
  2564. skip_bits(gb, 5); /* keyframe frequency force */
  2565. colorspace = get_bits(gb, 8);
  2566. skip_bits(gb, 24); /* bitrate */
  2567. skip_bits(gb, 6); /* quality hint */
  2568. if (s->theora >= 0x030200) {
  2569. skip_bits(gb, 5); /* keyframe frequency force */
  2570. avctx->pix_fmt = theora_pix_fmts[get_bits(gb, 2)];
  2571. if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
  2572. av_log(avctx, AV_LOG_ERROR, "Invalid pixel format\n");
  2573. return AVERROR_INVALIDDATA;
  2574. }
  2575. skip_bits(gb, 3); /* reserved */
  2576. } else
  2577. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  2578. ret = ff_set_dimensions(avctx, s->width, s->height);
  2579. if (ret < 0)
  2580. return ret;
  2581. if (!(avctx->flags2 & AV_CODEC_FLAG2_IGNORE_CROP)) {
  2582. avctx->width = visible_width;
  2583. avctx->height = visible_height;
  2584. // translate offsets from theora axis ([0,0] lower left)
  2585. // to normal axis ([0,0] upper left)
  2586. s->offset_x = offset_x;
  2587. s->offset_y = s->height - visible_height - offset_y;
  2588. }
  2589. if (colorspace == 1)
  2590. avctx->color_primaries = AVCOL_PRI_BT470M;
  2591. else if (colorspace == 2)
  2592. avctx->color_primaries = AVCOL_PRI_BT470BG;
  2593. if (colorspace == 1 || colorspace == 2) {
  2594. avctx->colorspace = AVCOL_SPC_BT470BG;
  2595. avctx->color_trc = AVCOL_TRC_BT709;
  2596. }
  2597. s->theora_header = 1;
  2598. return 0;
  2599. }
  2600. static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb)
  2601. {
  2602. Vp3DecodeContext *s = avctx->priv_data;
  2603. int i, n, matrices, inter, plane;
  2604. if (!s->theora_header)
  2605. return AVERROR_INVALIDDATA;
  2606. if (s->theora >= 0x030200) {
  2607. n = get_bits(gb, 3);
  2608. /* loop filter limit values table */
  2609. if (n)
  2610. for (i = 0; i < 64; i++)
  2611. s->filter_limit_values[i] = get_bits(gb, n);
  2612. }
  2613. if (s->theora >= 0x030200)
  2614. n = get_bits(gb, 4) + 1;
  2615. else
  2616. n = 16;
  2617. /* quality threshold table */
  2618. for (i = 0; i < 64; i++)
  2619. s->coded_ac_scale_factor[i] = get_bits(gb, n);
  2620. if (s->theora >= 0x030200)
  2621. n = get_bits(gb, 4) + 1;
  2622. else
  2623. n = 16;
  2624. /* dc scale factor table */
  2625. for (i = 0; i < 64; i++)
  2626. s->coded_dc_scale_factor[0][i] =
  2627. s->coded_dc_scale_factor[1][i] = get_bits(gb, n);
  2628. if (s->theora >= 0x030200)
  2629. matrices = get_bits(gb, 9) + 1;
  2630. else
  2631. matrices = 3;
  2632. if (matrices > 384) {
  2633. av_log(avctx, AV_LOG_ERROR, "invalid number of base matrixes\n");
  2634. return -1;
  2635. }
  2636. for (n = 0; n < matrices; n++)
  2637. for (i = 0; i < 64; i++)
  2638. s->base_matrix[n][i] = get_bits(gb, 8);
  2639. for (inter = 0; inter <= 1; inter++) {
  2640. for (plane = 0; plane <= 2; plane++) {
  2641. int newqr = 1;
  2642. if (inter || plane > 0)
  2643. newqr = get_bits1(gb);
  2644. if (!newqr) {
  2645. int qtj, plj;
  2646. if (inter && get_bits1(gb)) {
  2647. qtj = 0;
  2648. plj = plane;
  2649. } else {
  2650. qtj = (3 * inter + plane - 1) / 3;
  2651. plj = (plane + 2) % 3;
  2652. }
  2653. s->qr_count[inter][plane] = s->qr_count[qtj][plj];
  2654. memcpy(s->qr_size[inter][plane], s->qr_size[qtj][plj],
  2655. sizeof(s->qr_size[0][0]));
  2656. memcpy(s->qr_base[inter][plane], s->qr_base[qtj][plj],
  2657. sizeof(s->qr_base[0][0]));
  2658. } else {
  2659. int qri = 0;
  2660. int qi = 0;
  2661. for (;;) {
  2662. i = get_bits(gb, av_log2(matrices - 1) + 1);
  2663. if (i >= matrices) {
  2664. av_log(avctx, AV_LOG_ERROR,
  2665. "invalid base matrix index\n");
  2666. return -1;
  2667. }
  2668. s->qr_base[inter][plane][qri] = i;
  2669. if (qi >= 63)
  2670. break;
  2671. i = get_bits(gb, av_log2(63 - qi) + 1) + 1;
  2672. s->qr_size[inter][plane][qri++] = i;
  2673. qi += i;
  2674. }
  2675. if (qi > 63) {
  2676. av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi);
  2677. return -1;
  2678. }
  2679. s->qr_count[inter][plane] = qri;
  2680. }
  2681. }
  2682. }
  2683. /* Huffman tables */
  2684. for (s->hti = 0; s->hti < 80; s->hti++) {
  2685. s->entries = 0;
  2686. s->huff_code_size = 1;
  2687. if (!get_bits1(gb)) {
  2688. s->hbits = 0;
  2689. if (read_huffman_tree(avctx, gb))
  2690. return -1;
  2691. s->hbits = 1;
  2692. if (read_huffman_tree(avctx, gb))
  2693. return -1;
  2694. }
  2695. }
  2696. s->theora_tables = 1;
  2697. return 0;
  2698. }
  2699. static av_cold int theora_decode_init(AVCodecContext *avctx)
  2700. {
  2701. Vp3DecodeContext *s = avctx->priv_data;
  2702. GetBitContext gb;
  2703. int ptype;
  2704. const uint8_t *header_start[3];
  2705. int header_len[3];
  2706. int i;
  2707. int ret;
  2708. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  2709. s->theora = 1;
  2710. if (!avctx->extradata_size) {
  2711. av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n");
  2712. return -1;
  2713. }
  2714. if (avpriv_split_xiph_headers(avctx->extradata, avctx->extradata_size,
  2715. 42, header_start, header_len) < 0) {
  2716. av_log(avctx, AV_LOG_ERROR, "Corrupt extradata\n");
  2717. return -1;
  2718. }
  2719. for (i = 0; i < 3; i++) {
  2720. if (header_len[i] <= 0)
  2721. continue;
  2722. ret = init_get_bits8(&gb, header_start[i], header_len[i]);
  2723. if (ret < 0)
  2724. return ret;
  2725. ptype = get_bits(&gb, 8);
  2726. if (!(ptype & 0x80)) {
  2727. av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n");
  2728. // return -1;
  2729. }
  2730. // FIXME: Check for this as well.
  2731. skip_bits_long(&gb, 6 * 8); /* "theora" */
  2732. switch (ptype) {
  2733. case 0x80:
  2734. if (theora_decode_header(avctx, &gb) < 0)
  2735. return -1;
  2736. break;
  2737. case 0x81:
  2738. // FIXME: is this needed? it breaks sometimes
  2739. // theora_decode_comments(avctx, gb);
  2740. break;
  2741. case 0x82:
  2742. if (theora_decode_tables(avctx, &gb))
  2743. return -1;
  2744. break;
  2745. default:
  2746. av_log(avctx, AV_LOG_ERROR,
  2747. "Unknown Theora config packet: %d\n", ptype & ~0x80);
  2748. break;
  2749. }
  2750. if (ptype != 0x81 && 8 * header_len[i] != get_bits_count(&gb))
  2751. av_log(avctx, AV_LOG_WARNING,
  2752. "%d bits left in packet %X\n",
  2753. 8 * header_len[i] - get_bits_count(&gb), ptype);
  2754. if (s->theora < 0x030200)
  2755. break;
  2756. }
  2757. return vp3_decode_init(avctx);
  2758. }
  2759. AVCodec ff_theora_decoder = {
  2760. .name = "theora",
  2761. .long_name = NULL_IF_CONFIG_SMALL("Theora"),
  2762. .type = AVMEDIA_TYPE_VIDEO,
  2763. .id = AV_CODEC_ID_THEORA,
  2764. .priv_data_size = sizeof(Vp3DecodeContext),
  2765. .init = theora_decode_init,
  2766. .close = vp3_decode_end,
  2767. .decode = vp3_decode_frame,
  2768. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DRAW_HORIZ_BAND |
  2769. AV_CODEC_CAP_FRAME_THREADS,
  2770. .flush = vp3_decode_flush,
  2771. .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context),
  2772. .caps_internal = FF_CODEC_CAP_EXPORTS_CROPPING | FF_CODEC_CAP_ALLOCATE_PROGRESS,
  2773. };
  2774. #endif
  2775. AVCodec ff_vp3_decoder = {
  2776. .name = "vp3",
  2777. .long_name = NULL_IF_CONFIG_SMALL("On2 VP3"),
  2778. .type = AVMEDIA_TYPE_VIDEO,
  2779. .id = AV_CODEC_ID_VP3,
  2780. .priv_data_size = sizeof(Vp3DecodeContext),
  2781. .init = vp3_decode_init,
  2782. .close = vp3_decode_end,
  2783. .decode = vp3_decode_frame,
  2784. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DRAW_HORIZ_BAND |
  2785. AV_CODEC_CAP_FRAME_THREADS,
  2786. .flush = vp3_decode_flush,
  2787. .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context),
  2788. .caps_internal = FF_CODEC_CAP_ALLOCATE_PROGRESS,
  2789. };
  2790. #if CONFIG_VP4_DECODER
  2791. AVCodec ff_vp4_decoder = {
  2792. .name = "vp4",
  2793. .long_name = NULL_IF_CONFIG_SMALL("On2 VP4"),
  2794. .type = AVMEDIA_TYPE_VIDEO,
  2795. .id = AV_CODEC_ID_VP4,
  2796. .priv_data_size = sizeof(Vp3DecodeContext),
  2797. .init = vp3_decode_init,
  2798. .close = vp3_decode_end,
  2799. .decode = vp3_decode_frame,
  2800. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DRAW_HORIZ_BAND |
  2801. AV_CODEC_CAP_FRAME_THREADS,
  2802. .flush = vp3_decode_flush,
  2803. .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context),
  2804. .caps_internal = FF_CODEC_CAP_ALLOCATE_PROGRESS,
  2805. };
  2806. #endif