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