]> git.sesse.net Git - ffmpeg/blob - libavcodec/vc1_parser.c
libopusdec: fix out-of-bounds read
[ffmpeg] / libavcodec / vc1_parser.c
1 /*
2  * VC-1 and WMV3 parser
3  * Copyright (c) 2006-2007 Konstantin Shishkov
4  * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
5  *
6  * This file is part of Libav.
7  *
8  * Libav is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * Libav is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 /**
24  * @file
25  * VC-1 and WMV3 parser
26  */
27
28 #include "libavutil/attributes.h"
29 #include "parser.h"
30 #include "vc1.h"
31 #include "get_bits.h"
32
33 /** The maximum number of bytes of a sequence, entry point or
34  *  frame header whose values we pay any attention to */
35 #define UNESCAPED_THRESHOLD 37
36
37 /** The maximum number of bytes of a sequence, entry point or
38  *  frame header which must be valid memory (because they are
39  *  used to update the bitstream cache in skip_bits() calls)
40  */
41 #define UNESCAPED_LIMIT 144
42
43 typedef enum {
44     NO_MATCH,
45     ONE_ZERO,
46     TWO_ZEROS,
47     ONE
48 } VC1ParseSearchState;
49
50 typedef struct VC1ParseContext {
51     ParseContext pc;
52     VC1Context v;
53     uint8_t prev_start_code;
54     size_t bytes_to_skip;
55     uint8_t unesc_buffer[UNESCAPED_LIMIT];
56     size_t unesc_index;
57     VC1ParseSearchState search_state;
58 } VC1ParseContext;
59
60 static void vc1_extract_header(AVCodecParserContext *s, AVCodecContext *avctx,
61                                const uint8_t *buf, int buf_size)
62 {
63     /* Parse the header we just finished unescaping */
64     VC1ParseContext *vpc = s->priv_data;
65     GetBitContext gb;
66     vpc->v.s.avctx = avctx;
67     vpc->v.parse_only = 1;
68     init_get_bits(&gb, buf, buf_size * 8);
69     switch (vpc->prev_start_code) {
70     case VC1_CODE_SEQHDR & 0xFF:
71         ff_vc1_decode_sequence_header(avctx, &vpc->v, &gb);
72         break;
73     case VC1_CODE_ENTRYPOINT & 0xFF:
74         ff_vc1_decode_entry_point(avctx, &vpc->v, &gb);
75         break;
76     case VC1_CODE_FRAME & 0xFF:
77         if(vpc->v.profile < PROFILE_ADVANCED)
78             ff_vc1_parse_frame_header    (&vpc->v, &gb);
79         else
80             ff_vc1_parse_frame_header_adv(&vpc->v, &gb);
81
82         /* keep AV_PICTURE_TYPE_BI internal to VC1 */
83         if (vpc->v.s.pict_type == AV_PICTURE_TYPE_BI)
84             s->pict_type = AV_PICTURE_TYPE_B;
85         else
86             s->pict_type = vpc->v.s.pict_type;
87
88         if (avctx->ticks_per_frame > 1){
89             // process pulldown flags
90             s->repeat_pict = 1;
91             // Pulldown flags are only valid when 'broadcast' has been set.
92             // So ticks_per_frame will be 2
93             if (vpc->v.rff){
94                 // repeat field
95                 s->repeat_pict = 2;
96             }else if (vpc->v.rptfrm){
97                 // repeat frames
98                 s->repeat_pict = vpc->v.rptfrm * 2 + 1;
99             }
100         }else{
101             s->repeat_pict = 0;
102         }
103
104         if (vpc->v.broadcast && vpc->v.interlace && !vpc->v.psf)
105             s->field_order = vpc->v.tff ? AV_FIELD_TT : AV_FIELD_BB;
106         else
107             s->field_order = AV_FIELD_PROGRESSIVE;
108
109         break;
110     }
111     s->format = vpc->v.chromaformat == 1 ? AV_PIX_FMT_YUV420P
112                                          : AV_PIX_FMT_NONE;
113     if (avctx->width && avctx->height) {
114         s->width        = avctx->width;
115         s->height       = avctx->height;
116         s->coded_width  = FFALIGN(avctx->coded_width,  16);
117         s->coded_height = FFALIGN(avctx->coded_height, 16);
118     }
119 }
120
121 static int vc1_parse(AVCodecParserContext *s,
122                            AVCodecContext *avctx,
123                            const uint8_t **poutbuf, int *poutbuf_size,
124                            const uint8_t *buf, int buf_size)
125 {
126     /* Here we do the searching for frame boundaries and headers at
127      * the same time. Only a minimal amount at the start of each
128      * header is unescaped. */
129     VC1ParseContext *vpc = s->priv_data;
130     int pic_found = vpc->pc.frame_start_found;
131     uint8_t *unesc_buffer = vpc->unesc_buffer;
132     size_t unesc_index = vpc->unesc_index;
133     VC1ParseSearchState search_state = vpc->search_state;
134     int start_code_found = 0;
135     int next = END_NOT_FOUND;
136     int i = vpc->bytes_to_skip;
137
138     if (pic_found && buf_size == 0) {
139         /* EOF considered as end of frame */
140         memset(unesc_buffer + unesc_index, 0, UNESCAPED_THRESHOLD - unesc_index);
141         vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
142         next = 0;
143     }
144     while (i < buf_size) {
145         uint8_t b;
146         start_code_found = 0;
147         while (i < buf_size && unesc_index < UNESCAPED_THRESHOLD) {
148             b = buf[i++];
149             unesc_buffer[unesc_index++] = b;
150             if (search_state <= ONE_ZERO)
151                 search_state = b ? NO_MATCH : search_state + 1;
152             else if (search_state == TWO_ZEROS) {
153                 if (b == 1)
154                     search_state = ONE;
155                 else if (b > 1) {
156                     if (b == 3)
157                         unesc_index--; // swallow emulation prevention byte
158                     search_state = NO_MATCH;
159                 }
160             }
161             else { // search_state == ONE
162                 // Header unescaping terminates early due to detection of next start code
163                 search_state = NO_MATCH;
164                 start_code_found = 1;
165                 break;
166             }
167         }
168         if ((s->flags & PARSER_FLAG_COMPLETE_FRAMES) &&
169                 unesc_index >= UNESCAPED_THRESHOLD &&
170                 vpc->prev_start_code == (VC1_CODE_FRAME & 0xFF))
171         {
172             // No need to keep scanning the rest of the buffer for
173             // start codes if we know it contains a complete frame and
174             // we've already unescaped all we need of the frame header
175             vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
176             break;
177         }
178         if (unesc_index >= UNESCAPED_THRESHOLD && !start_code_found) {
179             while (i < buf_size) {
180                 if (search_state == NO_MATCH) {
181                     i += vpc->v.vc1dsp.startcode_find_candidate(buf + i, buf_size - i);
182                     if (i < buf_size) {
183                         search_state = ONE_ZERO;
184                     }
185                     i++;
186                 } else {
187                     b = buf[i++];
188                     if (search_state == ONE_ZERO)
189                         search_state = b ? NO_MATCH : TWO_ZEROS;
190                     else if (search_state == TWO_ZEROS) {
191                         if (b >= 1)
192                             search_state = b == 1 ? ONE : NO_MATCH;
193                     }
194                     else { // search_state == ONE
195                         search_state = NO_MATCH;
196                         start_code_found = 1;
197                         break;
198                     }
199                 }
200             }
201         }
202         if (start_code_found) {
203             vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
204
205             vpc->prev_start_code = b;
206             unesc_index = 0;
207
208             if (!(s->flags & PARSER_FLAG_COMPLETE_FRAMES)) {
209                 if (!pic_found && (b == (VC1_CODE_FRAME & 0xFF) || b == (VC1_CODE_FIELD & 0xFF))) {
210                     pic_found = 1;
211                 }
212                 else if (pic_found && b != (VC1_CODE_FIELD & 0xFF) && b != (VC1_CODE_SLICE & 0xFF)) {
213                     next = i - 4;
214                     pic_found = b == (VC1_CODE_FRAME & 0xFF);
215                     break;
216                 }
217             }
218         }
219     }
220
221     vpc->pc.frame_start_found = pic_found;
222     vpc->unesc_index = unesc_index;
223     vpc->search_state = search_state;
224
225     if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
226         next = buf_size;
227     } else {
228         if (ff_combine_frame(&vpc->pc, next, &buf, &buf_size) < 0) {
229             vpc->bytes_to_skip = 0;
230             *poutbuf = NULL;
231             *poutbuf_size = 0;
232             return buf_size;
233         }
234     }
235
236     /* If we return with a valid pointer to a combined frame buffer
237      * then on the next call then we'll have been unhelpfully rewound
238      * by up to 4 bytes (depending upon whether the start code
239      * overlapped the input buffer, and if so by how much). We don't
240      * want this: it will either cause spurious second detections of
241      * the start code we've already seen, or cause extra bytes to be
242      * inserted at the start of the unescaped buffer. */
243     vpc->bytes_to_skip = 4;
244     if (next < 0 && start_code_found)
245         vpc->bytes_to_skip += next;
246
247     *poutbuf = buf;
248     *poutbuf_size = buf_size;
249     return next;
250 }
251
252 static int vc1_split(AVCodecContext *avctx,
253                            const uint8_t *buf, int buf_size)
254 {
255     int i;
256     uint32_t state= -1;
257     int charged=0;
258
259     for(i=0; i<buf_size; i++){
260         state= (state<<8) | buf[i];
261         if(IS_MARKER(state)){
262             if(state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT){
263                 charged=1;
264             }else if(charged){
265                 return i-3;
266             }
267         }
268     }
269     return 0;
270 }
271
272 static av_cold int vc1_parse_init(AVCodecParserContext *s)
273 {
274     VC1ParseContext *vpc = s->priv_data;
275     vpc->v.s.slice_context_count = 1;
276     vpc->prev_start_code = 0;
277     vpc->bytes_to_skip = 0;
278     vpc->unesc_index = 0;
279     vpc->search_state = NO_MATCH;
280     return ff_vc1_init_common(&vpc->v);
281 }
282
283 AVCodecParser ff_vc1_parser = {
284     .codec_ids      = { AV_CODEC_ID_VC1 },
285     .priv_data_size = sizeof(VC1ParseContext),
286     .parser_init    = vc1_parse_init,
287     .parser_parse   = vc1_parse,
288     .parser_close   = ff_parse_close,
289     .split          = vc1_split,
290 };