3 * Copyright (c) 2006-2007 Konstantin Shishkov
4 * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
6 * This file is part of FFmpeg.
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.
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.
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
25 * VC-1 and WMV3 parser
37 static void vc1_extract_headers(AVCodecParserContext
*s
, AVCodecContext
*avctx
,
38 const uint8_t *buf
, int buf_size
)
40 VC1ParseContext
*vpc
= s
->priv_data
;
42 const uint8_t *start
, *end
, *next
;
43 uint8_t *buf2
= av_mallocz(buf_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
45 vpc
->v
.s
.avctx
= avctx
;
46 vpc
->v
.parse_only
= 1;
49 for(start
= buf
, end
= buf
+ buf_size
; next
< end
; start
= next
){
52 next
= find_next_marker(start
+ 4, end
);
53 size
= next
- start
- 4;
54 buf2_size
= vc1_unescape_buffer(start
+ 4, size
, buf2
);
55 init_get_bits(&gb
, buf2
, buf2_size
* 8);
56 if(size
<= 0) continue;
57 switch(AV_RB32(start
)){
59 vc1_decode_sequence_header(avctx
, &vpc
->v
, &gb
);
61 case VC1_CODE_ENTRYPOINT
:
62 vc1_decode_entry_point(avctx
, &vpc
->v
, &gb
);
65 if(vpc
->v
.profile
< PROFILE_ADVANCED
)
66 vc1_parse_frame_header (&vpc
->v
, &gb
);
68 vc1_parse_frame_header_adv(&vpc
->v
, &gb
);
70 /* keep FF_BI_TYPE internal to VC1 */
71 if (vpc
->v
.s
.pict_type
== FF_BI_TYPE
)
72 s
->pict_type
= FF_B_TYPE
;
74 s
->pict_type
= vpc
->v
.s
.pict_type
;
84 * finds the end of the current frame in the bitstream.
85 * @return the position of the first byte of the next frame, or -1
87 static int vc1_find_frame_end(ParseContext
*pc
, const uint8_t *buf
,
92 pic_found
= pc
->frame_start_found
;
97 for(i
=0; i
<buf_size
; i
++){
98 state
= (state
<<8) | buf
[i
];
99 if(state
== VC1_CODE_FRAME
|| state
== VC1_CODE_FIELD
){
108 /* EOF considered as end of frame */
111 for(; i
<buf_size
; i
++){
112 state
= (state
<<8) | buf
[i
];
113 if(IS_MARKER(state
) && state
!= VC1_CODE_FIELD
&& state
!= VC1_CODE_SLICE
){
114 pc
->frame_start_found
=0;
120 pc
->frame_start_found
= pic_found
;
122 return END_NOT_FOUND
;
125 static int vc1_parse(AVCodecParserContext
*s
,
126 AVCodecContext
*avctx
,
127 const uint8_t **poutbuf
, int *poutbuf_size
,
128 const uint8_t *buf
, int buf_size
)
130 VC1ParseContext
*vpc
= s
->priv_data
;
133 if(s
->flags
& PARSER_FLAG_COMPLETE_FRAMES
){
136 next
= vc1_find_frame_end(&vpc
->pc
, buf
, buf_size
);
138 if (ff_combine_frame(&vpc
->pc
, next
, &buf
, &buf_size
) < 0) {
145 vc1_extract_headers(s
, avctx
, buf
, buf_size
);
148 *poutbuf_size
= buf_size
;
152 static int vc1_split(AVCodecContext
*avctx
,
153 const uint8_t *buf
, int buf_size
)
159 for(i
=0; i
<buf_size
; i
++){
160 state
= (state
<<8) | buf
[i
];
161 if(IS_MARKER(state
)){
162 if(state
== VC1_CODE_SEQHDR
|| state
== VC1_CODE_ENTRYPOINT
){
172 AVCodecParser vc1_parser
= {
174 sizeof(VC1ParseContext
),