Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / 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 FFmpeg.
7  *
8  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 {
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     int ret;
67     vpc->v.s.avctx = avctx;
68     vpc->v.parse_only = 1;
69     init_get_bits(&gb, buf, buf_size * 8);
70     switch (vpc->prev_start_code) {
71     case VC1_CODE_SEQHDR & 0xFF:
72         ff_vc1_decode_sequence_header(avctx, &vpc->v, &gb);
73         break;
74     case VC1_CODE_ENTRYPOINT & 0xFF:
75         ff_vc1_decode_entry_point(avctx, &vpc->v, &gb);
76         break;
77     case VC1_CODE_FRAME & 0xFF:
78         if(vpc->v.profile < PROFILE_ADVANCED)
79             ret = ff_vc1_parse_frame_header    (&vpc->v, &gb);
80         else
81             ret = ff_vc1_parse_frame_header_adv(&vpc->v, &gb);
82
83         if (ret < 0)
84             break;
85
86         /* keep AV_PICTURE_TYPE_BI internal to VC1 */
87         if (vpc->v.s.pict_type == AV_PICTURE_TYPE_BI)
88             s->pict_type = AV_PICTURE_TYPE_B;
89         else
90             s->pict_type = vpc->v.s.pict_type;
91
92         if (avctx->ticks_per_frame > 1){
93             // process pulldown flags
94             s->repeat_pict = 1;
95             // Pulldown flags are only valid when 'broadcast' has been set.
96             // So ticks_per_frame will be 2
97             if (vpc->v.rff){
98                 // repeat field
99                 s->repeat_pict = 2;
100             }else if (vpc->v.rptfrm){
101                 // repeat frames
102                 s->repeat_pict = vpc->v.rptfrm * 2 + 1;
103             }
104         }else{
105             s->repeat_pict = 0;
106         }
107
108         if (vpc->v.broadcast && vpc->v.interlace && !vpc->v.psf)
109             s->field_order = vpc->v.tff ? AV_FIELD_TT : AV_FIELD_BB;
110         else
111             s->field_order = AV_FIELD_PROGRESSIVE;
112
113         break;
114     }
115 }
116
117 static int vc1_parse(AVCodecParserContext *s,
118                            AVCodecContext *avctx,
119                            const uint8_t **poutbuf, int *poutbuf_size,
120                            const uint8_t *buf, int buf_size)
121 {
122     /* Here we do the searching for frame boundaries and headers at
123      * the same time. Only a minimal amount at the start of each
124      * header is unescaped. */
125     VC1ParseContext *vpc = s->priv_data;
126     int pic_found = vpc->pc.frame_start_found;
127     uint8_t *unesc_buffer = vpc->unesc_buffer;
128     size_t unesc_index = vpc->unesc_index;
129     VC1ParseSearchState search_state = vpc->search_state;
130     int start_code_found = 0;
131     int next = END_NOT_FOUND;
132     int i = vpc->bytes_to_skip;
133
134     if (pic_found && buf_size == 0) {
135         /* EOF considered as end of frame */
136         memset(unesc_buffer + unesc_index, 0, UNESCAPED_THRESHOLD - unesc_index);
137         vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
138         next = 0;
139     }
140     while (i < buf_size) {
141         uint8_t b;
142         start_code_found = 0;
143         while (i < buf_size && unesc_index < UNESCAPED_THRESHOLD) {
144             b = buf[i++];
145             unesc_buffer[unesc_index++] = b;
146             if (search_state <= ONE_ZERO)
147                 search_state = b ? NO_MATCH : search_state + 1;
148             else if (search_state == TWO_ZEROS) {
149                 if (b == 1)
150                     search_state = ONE;
151                 else if (b > 1) {
152                     if (b == 3)
153                         unesc_index--; // swallow emulation prevention byte
154                     search_state = NO_MATCH;
155                 }
156             }
157             else { // search_state == ONE
158                 // Header unescaping terminates early due to detection of next start code
159                 search_state = NO_MATCH;
160                 start_code_found = 1;
161                 break;
162             }
163         }
164         if ((s->flags & PARSER_FLAG_COMPLETE_FRAMES) &&
165                 unesc_index >= UNESCAPED_THRESHOLD &&
166                 vpc->prev_start_code == (VC1_CODE_FRAME & 0xFF))
167         {
168             // No need to keep scanning the rest of the buffer for
169             // start codes if we know it contains a complete frame and
170             // we've already unescaped all we need of the frame header
171             vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
172             break;
173         }
174         if (unesc_index >= UNESCAPED_THRESHOLD && !start_code_found) {
175             while (i < buf_size) {
176                 if (search_state == NO_MATCH) {
177                     i += vpc->v.vc1dsp.startcode_find_candidate(buf + i, buf_size - i);
178                     if (i < buf_size) {
179                         search_state = ONE_ZERO;
180                     }
181                     i++;
182                 } else {
183                     b = buf[i++];
184                     if (search_state == ONE_ZERO)
185                         search_state = b ? NO_MATCH : TWO_ZEROS;
186                     else if (search_state == TWO_ZEROS) {
187                         if (b >= 1)
188                             search_state = b == 1 ? ONE : NO_MATCH;
189                     }
190                     else { // search_state == ONE
191                         search_state = NO_MATCH;
192                         start_code_found = 1;
193                         break;
194                     }
195                 }
196             }
197         }
198         if (start_code_found) {
199             vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
200
201             vpc->prev_start_code = b;
202             unesc_index = 0;
203
204             if (!(s->flags & PARSER_FLAG_COMPLETE_FRAMES)) {
205                 if (!pic_found && (b == (VC1_CODE_FRAME & 0xFF) || b == (VC1_CODE_FIELD & 0xFF))) {
206                     pic_found = 1;
207                 }
208                 else if (pic_found && b != (VC1_CODE_FIELD & 0xFF) && b != (VC1_CODE_SLICE & 0xFF)) {
209                     next = i - 4;
210                     pic_found = b == (VC1_CODE_FRAME & 0xFF);
211                     break;
212                 }
213             }
214         }
215     }
216
217     vpc->pc.frame_start_found = pic_found;
218     vpc->unesc_index = unesc_index;
219     vpc->search_state = search_state;
220
221     if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
222         next = buf_size;
223     } else {
224         if (ff_combine_frame(&vpc->pc, next, &buf, &buf_size) < 0) {
225             vpc->bytes_to_skip = 0;
226             *poutbuf = NULL;
227             *poutbuf_size = 0;
228             return buf_size;
229         }
230     }
231
232     /* If we return with a valid pointer to a combined frame buffer
233      * then on the next call then we'll have been unhelpfully rewound
234      * by up to 4 bytes (depending upon whether the start code
235      * overlapped the input buffer, and if so by how much). We don't
236      * want this: it will either cause spurious second detections of
237      * the start code we've already seen, or cause extra bytes to be
238      * inserted at the start of the unescaped buffer. */
239     vpc->bytes_to_skip = 4;
240     if (next < 0 && next != END_NOT_FOUND)
241         vpc->bytes_to_skip += next;
242
243     *poutbuf = buf;
244     *poutbuf_size = buf_size;
245     return next;
246 }
247
248 static int vc1_split(AVCodecContext *avctx,
249                            const uint8_t *buf, int buf_size)
250 {
251     int i;
252     uint32_t state= -1;
253     int charged=0;
254
255     for(i=0; i<buf_size; i++){
256         state= (state<<8) | buf[i];
257         if(IS_MARKER(state)){
258             if(state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT){
259                 charged=1;
260             }else if(charged){
261                 return i-3;
262             }
263         }
264     }
265     return 0;
266 }
267
268 static av_cold int vc1_parse_init(AVCodecParserContext *s)
269 {
270     VC1ParseContext *vpc = s->priv_data;
271     vpc->v.s.slice_context_count = 1;
272     vpc->v.first_pic_header_flag = 1;
273     vpc->prev_start_code = 0;
274     vpc->bytes_to_skip = 0;
275     vpc->unesc_index = 0;
276     vpc->search_state = NO_MATCH;
277     return ff_vc1_init_common(&vpc->v);
278 }
279
280 AVCodecParser ff_vc1_parser = {
281     .codec_ids      = { AV_CODEC_ID_VC1 },
282     .priv_data_size = sizeof(VC1ParseContext),
283     .parser_init    = vc1_parse_init,
284     .parser_parse   = vc1_parse,
285     .parser_close   = ff_parse_close,
286     .split          = vc1_split,
287 };