]> git.sesse.net Git - ffmpeg/blob - libavcodec/vc1_parser.c
avcodec: Mark argument in av_{parser|hwaccel|bitstream_filter}_next as const
[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 typedef struct {
34     ParseContext pc;
35     VC1Context v;
36 } VC1ParseContext;
37
38 static void vc1_extract_headers(AVCodecParserContext *s, AVCodecContext *avctx,
39                                 const uint8_t *buf, int buf_size)
40 {
41     VC1ParseContext *vpc = s->priv_data;
42     GetBitContext gb;
43     const uint8_t *start, *end, *next;
44     uint8_t *buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
45
46     vpc->v.s.avctx = avctx;
47     vpc->v.parse_only = 1;
48     next = buf;
49     s->repeat_pict = 0;
50
51     for(start = buf, end = buf + buf_size; next < end; start = next){
52         int buf2_size, size;
53
54         next = find_next_marker(start + 4, end);
55         size = next - start - 4;
56         buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
57         init_get_bits(&gb, buf2, buf2_size * 8);
58         if(size <= 0) continue;
59         switch(AV_RB32(start)){
60         case VC1_CODE_SEQHDR:
61             ff_vc1_decode_sequence_header(avctx, &vpc->v, &gb);
62             break;
63         case VC1_CODE_ENTRYPOINT:
64             ff_vc1_decode_entry_point(avctx, &vpc->v, &gb);
65             break;
66         case VC1_CODE_FRAME:
67             if(vpc->v.profile < PROFILE_ADVANCED)
68                 ff_vc1_parse_frame_header    (&vpc->v, &gb);
69             else
70                 ff_vc1_parse_frame_header_adv(&vpc->v, &gb);
71
72             /* keep AV_PICTURE_TYPE_BI internal to VC1 */
73             if (vpc->v.s.pict_type == AV_PICTURE_TYPE_BI)
74                 s->pict_type = AV_PICTURE_TYPE_B;
75             else
76                 s->pict_type = vpc->v.s.pict_type;
77
78             if (avctx->ticks_per_frame > 1){
79                 // process pulldown flags
80                 s->repeat_pict = 1;
81                 // Pulldown flags are only valid when 'broadcast' has been set.
82                 // So ticks_per_frame will be 2
83                 if (vpc->v.rff){
84                     // repeat field
85                     s->repeat_pict = 2;
86                 }else if (vpc->v.rptfrm){
87                     // repeat frames
88                     s->repeat_pict = vpc->v.rptfrm * 2 + 1;
89                 }
90             }
91
92             if (vpc->v.broadcast && vpc->v.interlace && !vpc->v.psf)
93                 s->field_order = vpc->v.tff ? AV_FIELD_TT : AV_FIELD_BB;
94             else
95                 s->field_order = AV_FIELD_PROGRESSIVE;
96
97             break;
98         }
99     }
100
101     av_free(buf2);
102 }
103
104 /**
105  * Find the end of the current frame in the bitstream.
106  * @return the position of the first byte of the next frame, or -1
107  */
108 static int vc1_find_frame_end(ParseContext *pc, const uint8_t *buf,
109                                int buf_size) {
110     int pic_found, i;
111     uint32_t state;
112
113     pic_found= pc->frame_start_found;
114     state= pc->state;
115
116     i=0;
117     if(!pic_found){
118         for(i=0; i<buf_size; i++){
119             state= (state<<8) | buf[i];
120             if(state == VC1_CODE_FRAME || state == VC1_CODE_FIELD){
121                 i++;
122                 pic_found=1;
123                 break;
124             }
125         }
126     }
127
128     if(pic_found){
129         /* EOF considered as end of frame */
130         if (buf_size == 0)
131             return 0;
132         for(; i<buf_size; i++){
133             state= (state<<8) | buf[i];
134             if(IS_MARKER(state) && state != VC1_CODE_FIELD && state != VC1_CODE_SLICE){
135                 pc->frame_start_found=0;
136                 pc->state=-1;
137                 return i-3;
138             }
139         }
140     }
141     pc->frame_start_found= pic_found;
142     pc->state= state;
143     return END_NOT_FOUND;
144 }
145
146 static int vc1_parse(AVCodecParserContext *s,
147                            AVCodecContext *avctx,
148                            const uint8_t **poutbuf, int *poutbuf_size,
149                            const uint8_t *buf, int buf_size)
150 {
151     VC1ParseContext *vpc = s->priv_data;
152     int next;
153
154     if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
155         next= buf_size;
156     }else{
157         next= vc1_find_frame_end(&vpc->pc, buf, buf_size);
158
159         if (ff_combine_frame(&vpc->pc, next, &buf, &buf_size) < 0) {
160             *poutbuf = NULL;
161             *poutbuf_size = 0;
162             return buf_size;
163         }
164     }
165
166     vc1_extract_headers(s, avctx, buf, buf_size);
167
168     *poutbuf = buf;
169     *poutbuf_size = buf_size;
170     return next;
171 }
172
173 static int vc1_split(AVCodecContext *avctx,
174                            const uint8_t *buf, int buf_size)
175 {
176     int i;
177     uint32_t state= -1;
178     int charged=0;
179
180     for(i=0; i<buf_size; i++){
181         state= (state<<8) | buf[i];
182         if(IS_MARKER(state)){
183             if(state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT){
184                 charged=1;
185             }else if(charged){
186                 return i-3;
187             }
188         }
189     }
190     return 0;
191 }
192
193 static av_cold int vc1_parse_init(AVCodecParserContext *s)
194 {
195     VC1ParseContext *vpc = s->priv_data;
196     vpc->v.s.slice_context_count = 1;
197     return ff_vc1_init_common(&vpc->v);
198 }
199
200 AVCodecParser ff_vc1_parser = {
201     .codec_ids      = { AV_CODEC_ID_VC1 },
202     .priv_data_size = sizeof(VC1ParseContext),
203     .parser_init    = vc1_parse_init,
204     .parser_parse   = vc1_parse,
205     .parser_close   = ff_parse_close,
206     .split          = vc1_split,
207 };