33
34
# include "arm/vp8.h"
36
static void vp8_decode_flush(AVCodecContext *avctx)
37
static void free_buffers(VP8Context *s)
38
VP8Context *s = avctx->priv_data;
41
if (!avctx->is_copy) {
42
for (i = 0; i < 5; i++)
43
if (s->frames[i].data[0])
44
ff_thread_release_buffer(avctx, &s->frames[i]);
46
memset(s->framep, 0, sizeof(s->framep));
48
39
av_freep(&s->macroblocks_base);
49
40
av_freep(&s->filter_strength);
50
41
av_freep(&s->intra4x4_pred_mode_top);
51
42
av_freep(&s->top_nnz);
52
43
av_freep(&s->edge_emu_buffer);
53
44
av_freep(&s->top_border);
54
av_freep(&s->segmentation_map);
56
s->macroblocks = NULL;
46
s->macroblocks = NULL;
49
static int vp8_alloc_frame(VP8Context *s, AVFrame *f)
52
if ((ret = ff_thread_get_buffer(s->avctx, f)) < 0)
54
if (s->num_maps_to_be_freed && !s->maps_are_invalid) {
55
f->ref_index[0] = s->segmentation_maps[--s->num_maps_to_be_freed];
56
} else if (!(f->ref_index[0] = av_mallocz(s->mb_width * s->mb_height))) {
57
ff_thread_release_buffer(s->avctx, f);
58
return AVERROR(ENOMEM);
63
static void vp8_release_frame(VP8Context *s, AVFrame *f, int prefer_delayed_free, int can_direct_free)
65
if (f->ref_index[0]) {
66
if (prefer_delayed_free) {
67
/* Upon a size change, we want to free the maps but other threads may still
68
* be using them, so queue them. Upon a seek, all threads are inactive so
69
* we want to cache one to prevent re-allocation in the next decoding
70
* iteration, but the rest we can free directly. */
71
int max_queued_maps = can_direct_free ? 1 : FF_ARRAY_ELEMS(s->segmentation_maps);
72
if (s->num_maps_to_be_freed < max_queued_maps) {
73
s->segmentation_maps[s->num_maps_to_be_freed++] = f->ref_index[0];
74
} else if (can_direct_free) /* vp8_decode_flush(), but our queue is full */ {
75
av_free(f->ref_index[0]);
76
} /* else: MEMLEAK (should never happen, but better that than crash) */
77
f->ref_index[0] = NULL;
78
} else /* vp8_decode_free() */ {
79
av_free(f->ref_index[0]);
82
ff_thread_release_buffer(s->avctx, f);
85
static void vp8_decode_flush_impl(AVCodecContext *avctx,
86
int prefer_delayed_free, int can_direct_free, int free_mem)
88
VP8Context *s = avctx->priv_data;
91
if (!avctx->internal->is_copy) {
92
for (i = 0; i < 5; i++)
93
if (s->frames[i].data[0])
94
vp8_release_frame(s, &s->frames[i], prefer_delayed_free, can_direct_free);
96
memset(s->framep, 0, sizeof(s->framep));
100
s->maps_are_invalid = 1;
104
static void vp8_decode_flush(AVCodecContext *avctx)
106
vp8_decode_flush_impl(avctx, 1, 1, 0);
59
109
static int update_dimensions(VP8Context *s, int width, int height)
63
113
if (av_image_check_size(width, height, 0, s->avctx))
64
114
return AVERROR_INVALIDDATA;
66
vp8_decode_flush(s->avctx);
116
vp8_decode_flush_impl(s->avctx, 1, 0, 1);
68
118
avcodec_set_dimensions(s->avctx, width, height);
76
126
s->intra4x4_pred_mode_top = av_mallocz(s->mb_width*4);
77
127
s->top_nnz = av_mallocz(s->mb_width*sizeof(*s->top_nnz));
78
128
s->top_border = av_mallocz((s->mb_width+1)*sizeof(*s->top_border));
79
s->segmentation_map = av_mallocz(s->mb_width*s->mb_height);
81
130
if (!s->macroblocks_base || !s->filter_strength || !s->intra4x4_pred_mode_top ||
82
!s->top_nnz || !s->top_border || !s->segmentation_map)
131
!s->top_nnz || !s->top_border)
83
132
return AVERROR(ENOMEM);
85
134
s->macroblocks = s->macroblocks_base + 1;
641
691
* @param block destination for block coefficients
642
692
* @param probs probabilities to use when reading trees from the bitstream
643
693
* @param i initial coeff index, 0 unless a separate DC block is coded
644
* @param zero_nhood the initial prediction context for number of surrounding
645
* all-zero blocks (only left/top, so 0-2)
646
694
* @param qmul array holding the dc/ac dequant factor at position 0/1
647
695
* @return 0 if no coeffs were decoded
648
696
* otherwise, the index of the last coeff decoded plus one
753
* @param c arithmetic bitstream reader context
754
* @param block destination for block coefficients
755
* @param probs probabilities to use when reading trees from the bitstream
756
* @param i initial coeff index, 0 unless a separate DC block is coded
757
* @param zero_nhood the initial prediction context for number of surrounding
758
* all-zero blocks (only left/top, so 0-2)
759
* @param qmul array holding the dc/ac dequant factor at position 0/1
760
* @return 0 if no coeffs were decoded
761
* otherwise, the index of the last coeff decoded plus one
704
763
static av_always_inline
705
764
int decode_block_coeffs(VP56RangeCoder *c, DCTELEM block[16],
706
765
uint8_t probs[16][3][NUM_DCT_TOKENS-1],
1037
* Generic MC function.
1039
1099
* @param s VP8 decoding context
1040
* @param luma 1 for luma (Y) planes, 0 for chroma (Cb/Cr) planes
1041
1100
* @param dst target buffer for block data at block position
1042
* @param src reference picture buffer at origin (0, 0)
1101
* @param ref reference picture buffer at origin (0, 0)
1043
1102
* @param mv motion vector (relative to block position) to get pixel data from
1044
1103
* @param x_off horizontal position of block from origin (0, 0)
1045
1104
* @param y_off vertical position of block from origin (0, 0)
1146
* chroma MC function
1148
* @param s VP8 decoding context
1149
* @param dst1 target buffer for block data at block position (U plane)
1150
* @param dst2 target buffer for block data at block position (V plane)
1151
* @param ref reference picture buffer at origin (0, 0)
1152
* @param mv motion vector (relative to block position) to get pixel data from
1153
* @param x_off horizontal position of block from origin (0, 0)
1154
* @param y_off vertical position of block from origin (0, 0)
1155
* @param block_w width of block (16, 8 or 4)
1156
* @param block_h height of block (always same as block_w)
1157
* @param width width of src/dst plane data
1158
* @param height height of src/dst plane data
1159
* @param linesize size of a single line of plane data, including padding
1160
* @param mc_func motion compensation function pointers (bilinear or sixtap MC)
1086
1162
static av_always_inline
1087
1163
void vp8_mc_chroma(VP8Context *s, uint8_t *dst1, uint8_t *dst2, AVFrame *ref,
1088
1164
const VP56mv *mv, int x_off, int y_off,
1555
static void release_queued_segmaps(VP8Context *s, int is_close)
1557
int leave_behind = is_close ? 0 : !s->maps_are_invalid;
1558
while (s->num_maps_to_be_freed > leave_behind)
1559
av_freep(&s->segmentation_maps[--s->num_maps_to_be_freed]);
1560
s->maps_are_invalid = 0;
1479
1563
static int vp8_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
1480
1564
AVPacket *avpkt)
1482
1566
VP8Context *s = avctx->priv_data;
1483
1567
int ret, mb_x, mb_y, i, y, referenced;
1484
1568
enum AVDiscard skip_thresh;
1485
AVFrame *av_uninit(curframe), *prev_frame = s->framep[VP56_FRAME_CURRENT];
1569
AVFrame *av_uninit(curframe), *prev_frame;
1571
release_queued_segmaps(s, 0);
1487
1573
if ((ret = decode_frame_header(s, avpkt->data, avpkt->size)) < 0)
1576
prev_frame = s->framep[VP56_FRAME_CURRENT];
1490
1578
referenced = s->update_last || s->update_golden == VP56_FRAME_CURRENT
1491
1579
|| s->update_altref == VP56_FRAME_CURRENT;
1524
1612
if (curframe->data[0])
1525
ff_thread_release_buffer(avctx, curframe);
1613
vp8_release_frame(s, curframe, 1, 0);
1527
1615
curframe->key_frame = s->keyframe;
1528
1616
curframe->pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
1529
1617
curframe->reference = referenced ? 3 : 0;
1530
curframe->ref_index[0] = s->segmentation_map;
1531
if ((ret = ff_thread_get_buffer(avctx, curframe))) {
1618
if ((ret = vp8_alloc_frame(s, curframe))) {
1532
1619
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed!\n");
1620
1707
s->dsp.prefetch(dst[0] + (mb_x&3)*4*s->linesize + 64, s->linesize, 4);
1621
1708
s->dsp.prefetch(dst[1] + (mb_x&7)*s->uvlinesize + 64, dst[2] - dst[1], 2);
1623
decode_mb_mode(s, mb, mb_x, mb_y, s->segmentation_map + mb_xy,
1624
prev_frame ? prev_frame->ref_index[0] + mb_xy : NULL);
1710
decode_mb_mode(s, mb, mb_x, mb_y, curframe->ref_index[0] + mb_xy,
1711
prev_frame && prev_frame->ref_index[0] ? prev_frame->ref_index[0] + mb_xy : NULL);
1626
1713
prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP56_FRAME_PREVIOUS);
1741
1835
AVCodec ff_vp8_decoder = {
1750
CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
1837
.type = AVMEDIA_TYPE_VIDEO,
1839
.priv_data_size = sizeof(VP8Context),
1840
.init = vp8_decode_init,
1841
.close = vp8_decode_free,
1842
.decode = vp8_decode_frame,
1843
.capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
1751
1844
.flush = vp8_decode_flush,
1752
1845
.long_name = NULL_IF_CONFIG_SMALL("On2 VP8"),
1753
1846
.init_thread_copy = ONLY_IF_THREADS_ENABLED(vp8_decode_init_thread_copy),