226
225
/* these arrays need to be on 16-byte boundaries since SSE2 operations
227
226
* index into them */
228
DECLARE_ALIGNED(16, int16_t, qmat)[3][2][3][64]; //<qmat[qpi][is_inter][plane]
227
DECLARE_ALIGNED(16, int16_t, qmat)[3][2][3][64]; ///< qmat[qpi][is_inter][plane]
230
229
/* This table contains superblock_count * 16 entries. Each set of 16
231
230
* numbers corresponds to the fragment indexes 0..15 of the superblock.
254
253
* VP3 specific functions
255
254
************************************************************************/
256
static void vp3_decode_flush(AVCodecContext *avctx)
258
Vp3DecodeContext *s = avctx->priv_data;
260
if (s->golden_frame.data[0]) {
261
if (s->golden_frame.data[0] == s->last_frame.data[0])
262
memset(&s->last_frame, 0, sizeof(AVFrame));
263
if (s->current_frame.data[0] == s->golden_frame.data[0])
264
memset(&s->current_frame, 0, sizeof(AVFrame));
265
ff_thread_release_buffer(avctx, &s->golden_frame);
267
if (s->last_frame.data[0]) {
268
if (s->current_frame.data[0] == s->last_frame.data[0])
269
memset(&s->current_frame, 0, sizeof(AVFrame));
270
ff_thread_release_buffer(avctx, &s->last_frame);
272
if (s->current_frame.data[0])
273
ff_thread_release_buffer(avctx, &s->current_frame);
276
static av_cold int vp3_decode_end(AVCodecContext *avctx)
278
Vp3DecodeContext *s = avctx->priv_data;
281
av_free(s->superblock_coding);
282
av_free(s->all_fragments);
283
av_free(s->coded_fragment_list[0]);
284
av_free(s->dct_tokens_base);
285
av_free(s->superblock_fragments);
286
av_free(s->macroblock_coding);
287
av_free(s->motion_val[0]);
288
av_free(s->motion_val[1]);
289
av_free(s->edge_emu_buffer);
291
if (avctx->internal->is_copy)
294
for (i = 0; i < 16; i++) {
295
free_vlc(&s->dc_vlc[i]);
296
free_vlc(&s->ac_vlc_1[i]);
297
free_vlc(&s->ac_vlc_2[i]);
298
free_vlc(&s->ac_vlc_3[i]);
299
free_vlc(&s->ac_vlc_4[i]);
302
free_vlc(&s->superblock_run_length_vlc);
303
free_vlc(&s->fragment_run_length_vlc);
304
free_vlc(&s->mode_code_vlc);
305
free_vlc(&s->motion_vector_vlc);
307
/* release all frames */
308
vp3_decode_flush(avctx);
258
314
* This function sets up all of the various blocks mappings:
259
315
* superblocks <-> fragments, macroblocks <-> fragments,
890
946
/* decode a VLC into a token */
891
947
token = get_vlc2(gb, vlc_table, 11, 3);
892
948
/* use the token to get a zero run, a coefficient, and an eob run */
949
if ((unsigned) token <= 6U) {
894
950
eob_run = eob_run_base[token];
895
951
if (eob_run_get_bits[token])
896
952
eob_run += get_bits(gb, eob_run_get_bits[token]);
942
998
for (i = coeff_index+1; i <= coeff_index+zero_run; i++)
943
999
s->num_coded_frags[plane][i]--;
1002
av_log(s->avctx, AV_LOG_ERROR,
1003
"Invalid token %d\n", token);
991
1051
/* unpack the Y plane DC coefficients */
992
1052
residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
993
1053
0, residual_eob_run);
1054
if (residual_eob_run < 0)
1055
return residual_eob_run;
995
1057
/* reverse prediction of the Y-plane DC coefficients */
996
1058
reverse_dc_prediction(s, 0, s->fragment_width[0], s->fragment_height[0]);
998
1060
/* unpack the C plane DC coefficients */
999
1061
residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
1000
1062
1, residual_eob_run);
1063
if (residual_eob_run < 0)
1064
return residual_eob_run;
1001
1065
residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
1002
1066
2, residual_eob_run);
1067
if (residual_eob_run < 0)
1068
return residual_eob_run;
1004
1070
/* reverse prediction of the C-plane DC coefficients */
1005
1071
if (!(s->avctx->flags & CODEC_FLAG_GRAY))
1036
1102
for (i = 1; i <= 63; i++) {
1037
1103
residual_eob_run = unpack_vlcs(s, gb, y_tables[i], i,
1038
1104
0, residual_eob_run);
1105
if (residual_eob_run < 0)
1106
return residual_eob_run;
1040
1108
residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
1041
1109
1, residual_eob_run);
1110
if (residual_eob_run < 0)
1111
return residual_eob_run;
1042
1112
residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
1043
1113
2, residual_eob_run);
1114
if (residual_eob_run < 0)
1115
return residual_eob_run;
1314
1392
static void vp3_draw_horiz_band(Vp3DecodeContext *s, int y)
1395
int offset[AV_NUM_DATA_POINTERS];
1319
if (HAVE_PTHREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
1397
if (HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
1320
1398
int y_flipped = s->flipped_image ? s->avctx->height-y : y;
1322
1400
// At the end of the frame, report INT_MAX instead of the height of the frame.
1340
1418
offset[0] = s->current_frame.linesize[0]*y;
1341
1419
offset[1] = s->current_frame.linesize[1]*cy;
1342
1420
offset[2] = s->current_frame.linesize[2]*cy;
1421
for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
1346
1425
s->avctx->draw_horiz_band(s->avctx, &s->current_frame, offset, y, 3, h);
1400
1479
int fragment_width = s->fragment_width[!!plane];
1401
1480
int fragment_height = s->fragment_height[!!plane];
1402
1481
int fragment_start = s->fragment_start[plane];
1403
int do_await = !plane && HAVE_PTHREADS && (s->avctx->active_thread_type&FF_THREAD_FRAME);
1482
int do_await = !plane && HAVE_THREADS && (s->avctx->active_thread_type&FF_THREAD_FRAME);
1405
1484
if (!s->flipped_image) stride = -stride;
1406
1485
if (CONFIG_GRAY && plane && (s->avctx->flags & CODEC_FLAG_GRAY))
1493
1572
/* invert DCT and place (or add) in final output */
1495
1574
if (s->all_fragments[i].coding_method == MODE_INTRA) {
1496
vp3_dequant(s, s->all_fragments + i, plane, 0, block);
1576
index = vp3_dequant(s, s->all_fragments + i, plane, 0, block);
1497
1579
if(s->avctx->idct_algo!=FF_IDCT_VP3)
1498
1580
block[0] += 128<<3;
1499
1581
s->dsp.idct_put(
1504
if (vp3_dequant(s, s->all_fragments + i, plane, 1, block)) {
1586
int index = vp3_dequant(s, s->all_fragments + i, plane, 1, block);
1505
1590
s->dsp.idct_add(
1506
1591
output_plane + first_pixel,
1777
1859
Vp3DecodeContext *s = dst->priv_data, *s1 = src->priv_data;
1778
1860
int qps_changed = 0, i, err;
1862
#define copy_fields(to, from, start_field, end_field) memcpy(&to->start_field, &from->start_field, (char*)&to->end_field - (char*)&to->start_field)
1780
1864
if (!s1->current_frame.data[0]
1781
1865
||s->width != s1->width
1782
||s->height!= s1->height)
1866
||s->height!= s1->height) {
1868
copy_fields(s, s1, golden_frame, current_frame);
1786
1873
// init tables if the first frame hasn't been decoded
1796
1883
memcpy(s->motion_val[1], s1->motion_val[1], c_fragment_count * sizeof(*s->motion_val[1]));
1799
#define copy_fields(to, from, start_field, end_field) memcpy(&to->start_field, &from->start_field, (char*)&to->end_field - (char*)&to->start_field)
1801
1886
// copy previous frame data
1802
1887
copy_fields(s, s1, golden_frame, dsp);
1974
2056
ff_thread_report_progress(&s->current_frame, INT_MAX, 0);
1976
if (!HAVE_PTHREADS || !(s->avctx->active_thread_type&FF_THREAD_FRAME))
2058
if (!HAVE_THREADS || !(s->avctx->active_thread_type&FF_THREAD_FRAME))
1977
2059
avctx->release_buffer(avctx, &s->current_frame);
1983
* This is the ffmpeg/libavcodec API module cleanup function.
1985
static av_cold int vp3_decode_end(AVCodecContext *avctx)
1987
Vp3DecodeContext *s = avctx->priv_data;
1990
if (avctx->is_copy && !s->current_frame.data[0])
1993
av_free(s->superblock_coding);
1994
av_free(s->all_fragments);
1995
av_free(s->coded_fragment_list[0]);
1996
av_free(s->dct_tokens_base);
1997
av_free(s->superblock_fragments);
1998
av_free(s->macroblock_coding);
1999
av_free(s->motion_val[0]);
2000
av_free(s->motion_val[1]);
2001
av_free(s->edge_emu_buffer);
2003
if (avctx->is_copy) return 0;
2005
for (i = 0; i < 16; i++) {
2006
free_vlc(&s->dc_vlc[i]);
2007
free_vlc(&s->ac_vlc_1[i]);
2008
free_vlc(&s->ac_vlc_2[i]);
2009
free_vlc(&s->ac_vlc_3[i]);
2010
free_vlc(&s->ac_vlc_4[i]);
2013
free_vlc(&s->superblock_run_length_vlc);
2014
free_vlc(&s->fragment_run_length_vlc);
2015
free_vlc(&s->mode_code_vlc);
2016
free_vlc(&s->motion_vector_vlc);
2018
/* release all frames */
2019
if (s->golden_frame.data[0])
2020
ff_thread_release_buffer(avctx, &s->golden_frame);
2021
if (s->last_frame.data[0] && s->last_frame.type != FF_BUFFER_TYPE_COPY)
2022
ff_thread_release_buffer(avctx, &s->last_frame);
2023
/* no need to release the current_frame since it will always be pointing
2024
* to the same frame as either the golden or last frame */
2029
2064
static int read_huffman_tree(AVCodecContext *avctx, GetBitContext *gb)
2031
2066
Vp3DecodeContext *s = avctx->priv_data;
2098
static int vp3_init_thread_copy(AVCodecContext *avctx)
2100
Vp3DecodeContext *s = avctx->priv_data;
2102
s->superblock_coding = NULL;
2103
s->all_fragments = NULL;
2104
s->coded_fragment_list[0] = NULL;
2105
s->dct_tokens_base = NULL;
2106
s->superblock_fragments = NULL;
2107
s->macroblock_coding = NULL;
2108
s->motion_val[0] = NULL;
2109
s->motion_val[1] = NULL;
2110
s->edge_emu_buffer = NULL;
2063
2115
#if CONFIG_THEORA_DECODER
2064
2116
static const enum PixelFormat theora_pix_fmts[4] = {
2065
2117
PIX_FMT_YUV420P, PIX_FMT_NONE, PIX_FMT_YUV422P, PIX_FMT_YUV444P
2278
if (ff_split_xiph_headers(avctx->extradata, avctx->extradata_size,
2330
if (avpriv_split_xiph_headers(avctx->extradata, avctx->extradata_size,
2279
2331
42, header_start, header_len) < 0) {
2280
2332
av_log(avctx, AV_LOG_ERROR, "Corrupt extradata\n");
2324
2376
AVCodec ff_theora_decoder = {
2328
sizeof(Vp3DecodeContext),
2333
CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_FRAME_THREADS,
2378
.type = AVMEDIA_TYPE_VIDEO,
2379
.id = CODEC_ID_THEORA,
2380
.priv_data_size = sizeof(Vp3DecodeContext),
2381
.init = theora_decode_init,
2382
.close = vp3_decode_end,
2383
.decode = vp3_decode_frame,
2384
.capabilities = CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_FRAME_THREADS,
2385
.flush = vp3_decode_flush,
2335
2386
.long_name = NULL_IF_CONFIG_SMALL("Theora"),
2387
.init_thread_copy = ONLY_IF_THREADS_ENABLED(vp3_init_thread_copy),
2336
2388
.update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context)
2340
2392
AVCodec ff_vp3_decoder = {
2344
sizeof(Vp3DecodeContext),
2349
CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_FRAME_THREADS,
2394
.type = AVMEDIA_TYPE_VIDEO,
2396
.priv_data_size = sizeof(Vp3DecodeContext),
2397
.init = vp3_decode_init,
2398
.close = vp3_decode_end,
2399
.decode = vp3_decode_frame,
2400
.capabilities = CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_FRAME_THREADS,
2401
.flush = vp3_decode_flush,
2351
2402
.long_name = NULL_IF_CONFIG_SMALL("On2 VP3"),
2403
.init_thread_copy = ONLY_IF_THREADS_ENABLED(vp3_init_thread_copy),
2352
2404
.update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context)