1 gst-ffmpeg: vp3: fix oob read for negative tokens and memleaks on error.
3 Upstream-Status: Backport
5 Signed-off-by: Yue.Tao <yue.tao@windriver.com>
8 libavcodec/vp3.c | 59 +++++++++++++++++++++++++++++++++++++++++------------
9 1 files changed, 45 insertions(+), 14 deletions(-)
11 diff --git a/libavcodec/vp3.c b/libavcodec/vp3.c
12 index 36715bb..ce14e63 100644
13 --- a/gst-libs/ext/libav/libavcodec/vp3.c
14 +++ b/gst-libs/ext/libav/libavcodec/vp3.c
16 #define FRAGMENT_PIXELS 8
18 static av_cold int vp3_decode_end(AVCodecContext *avctx);
19 +static void vp3_decode_flush(AVCodecContext *avctx);
21 //FIXME split things out into their own arrays
22 typedef struct Vp3Fragment {
23 @@ -890,7 +891,7 @@ static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
24 /* decode a VLC into a token */
25 token = get_vlc2(gb, vlc_table, 11, 3);
26 /* use the token to get a zero run, a coefficient, and an eob run */
28 + if ((unsigned) token <= 6U) {
29 eob_run = eob_run_base[token];
30 if (eob_run_get_bits[token])
31 eob_run += get_bits(gb, eob_run_get_bits[token]);
32 @@ -908,7 +909,7 @@ static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
37 + } else if (token >= 0) {
38 bits_to_get = coeff_get_bits[token];
40 bits_to_get = get_bits(gb, bits_to_get);
41 @@ -942,6 +943,10 @@ static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
42 for (i = coeff_index+1; i <= coeff_index+zero_run; i++)
43 s->num_coded_frags[plane][i]--;
46 + av_log(s->avctx, AV_LOG_ERROR,
47 + "Invalid token %d\n", token);
52 @@ -991,6 +996,8 @@ static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
53 /* unpack the Y plane DC coefficients */
54 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
56 + if (residual_eob_run < 0)
57 + return residual_eob_run;
59 /* reverse prediction of the Y-plane DC coefficients */
60 reverse_dc_prediction(s, 0, s->fragment_width[0], s->fragment_height[0]);
61 @@ -998,8 +1005,12 @@ static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
62 /* unpack the C plane DC coefficients */
63 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
65 + if (residual_eob_run < 0)
66 + return residual_eob_run;
67 residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
69 + if (residual_eob_run < 0)
70 + return residual_eob_run;
72 /* reverse prediction of the C-plane DC coefficients */
73 if (!(s->avctx->flags & CODEC_FLAG_GRAY))
74 @@ -1036,11 +1047,17 @@ static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
75 for (i = 1; i <= 63; i++) {
76 residual_eob_run = unpack_vlcs(s, gb, y_tables[i], i,
78 + if (residual_eob_run < 0)
79 + return residual_eob_run;
81 residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
83 + if (residual_eob_run < 0)
84 + return residual_eob_run;
85 residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
87 + if (residual_eob_run < 0)
88 + return residual_eob_run;
92 @@ -1777,10 +1794,15 @@ static int vp3_update_thread_context(AVCodecContext *dst, const AVCodecContext *
93 Vp3DecodeContext *s = dst->priv_data, *s1 = src->priv_data;
94 int qps_changed = 0, i, err;
96 +#define copy_fields(to, from, start_field, end_field) memcpy(&to->start_field, &from->start_field, (char*)&to->end_field - (char*)&to->start_field)
98 if (!s1->current_frame.data[0]
99 ||s->width != s1->width
100 - ||s->height!= s1->height)
101 + ||s->height!= s1->height) {
103 + copy_fields(s, s1, golden_frame, current_frame);
108 // init tables if the first frame hasn't been decoded
109 @@ -1796,8 +1818,6 @@ static int vp3_update_thread_context(AVCodecContext *dst, const AVCodecContext *
110 memcpy(s->motion_val[1], s1->motion_val[1], c_fragment_count * sizeof(*s->motion_val[1]));
113 -#define copy_fields(to, from, start_field, end_field) memcpy(&to->start_field, &from->start_field, (char*)&to->end_field - (char*)&to->start_field)
115 // copy previous frame data
116 copy_fields(s, s1, golden_frame, dsp);
118 @@ -1987,9 +2007,6 @@ static av_cold int vp3_decode_end(AVCodecContext *avctx)
119 Vp3DecodeContext *s = avctx->priv_data;
122 - if (avctx->is_copy && !s->current_frame.data[0])
125 av_free(s->superblock_coding);
126 av_free(s->all_fragments);
127 av_free(s->coded_fragment_list[0]);
128 @@ -2016,12 +2033,7 @@ static av_cold int vp3_decode_end(AVCodecContext *avctx)
129 free_vlc(&s->motion_vector_vlc);
131 /* release all frames */
132 - if (s->golden_frame.data[0])
133 - ff_thread_release_buffer(avctx, &s->golden_frame);
134 - if (s->last_frame.data[0] && s->last_frame.type != FF_BUFFER_TYPE_COPY)
135 - ff_thread_release_buffer(avctx, &s->last_frame);
136 - /* no need to release the current_frame since it will always be pointing
137 - * to the same frame as either the golden or last frame */
138 + vp3_decode_flush(avctx);
142 @@ -2341,6 +2353,23 @@ static void vp3_decode_flush(AVCodecContext *avctx)
143 ff_thread_release_buffer(avctx, &s->current_frame);
146 +static int vp3_init_thread_copy(AVCodecContext *avctx)
148 + Vp3DecodeContext *s = avctx->priv_data;
150 + s->superblock_coding = NULL;
151 + s->all_fragments = NULL;
152 + s->coded_fragment_list[0] = NULL;
153 + s->dct_tokens_base = NULL;
154 + s->superblock_fragments = NULL;
155 + s->macroblock_coding = NULL;
156 + s->motion_val[0] = NULL;
157 + s->motion_val[1] = NULL;
158 + s->edge_emu_buffer = NULL;
163 AVCodec ff_theora_decoder = {
165 .type = AVMEDIA_TYPE_VIDEO,
166 @@ -2352,6 +2381,7 @@ AVCodec ff_theora_decoder = {
167 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_FRAME_THREADS,
168 .flush = vp3_decode_flush,
169 .long_name = NULL_IF_CONFIG_SMALL("Theora"),
170 + .init_thread_copy = ONLY_IF_THREADS_ENABLED(vp3_init_thread_copy),
171 .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context)
174 @@ -2367,5 +2397,6 @@ AVCodec ff_vp3_decoder = {
175 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_FRAME_THREADS,
176 .flush = vp3_decode_flush,
177 .long_name = NULL_IF_CONFIG_SMALL("On2 VP3"),
178 + .init_thread_copy = ONLY_IF_THREADS_ENABLED(vp3_init_thread_copy),
179 .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context)