2 * MPEG-1 / MPEG-2 video parser
3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
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
23 #include "libavutil/avassert.h"
27 #include "mpeg12data.h"
28 #include "parser_internal.h"
29 #include "startcode.h"
31 struct MpvParseContext
{
33 AVRational frame_rate
;
34 int progressive_sequence
;
39 * Find the end of the current frame in the bitstream.
40 * @return the position of the first byte of the next frame, or -1
42 static int mpeg1_find_frame_end(ParseContext
*pc
, const uint8_t *buf
,
43 int buf_size
, AVCodecParserContext
*s
)
46 uint32_t state
= pc
->state
;
48 /* EOF considered as end of frame */
55 2 first field start -> 3/0
56 3 second_SEQEXT -> 2/0
60 for (i
= 0; i
< buf_size
; i
++) {
61 av_assert1(pc
->frame_start_found
>= 0 && pc
->frame_start_found
<= 4);
62 if (pc
->frame_start_found
& 1) {
63 if (state
== EXT_START_CODE
&& (buf
[i
] & 0xF0) != 0x80)
64 pc
->frame_start_found
--;
65 else if (state
== EXT_START_CODE
+ 2) {
66 if ((buf
[i
] & 3) == 3)
67 pc
->frame_start_found
= 0;
69 pc
->frame_start_found
= (pc
->frame_start_found
+ 1) & 3;
73 i
= avpriv_find_start_code(buf
+ i
, buf
+ buf_size
, &state
) - buf
- 1;
74 if (pc
->frame_start_found
== 0 && state
>= SLICE_MIN_START_CODE
&& state
<= SLICE_MAX_START_CODE
) {
76 pc
->frame_start_found
= 4;
78 if (state
== SEQ_END_CODE
) {
79 pc
->frame_start_found
= 0;
83 if (pc
->frame_start_found
== 2 && state
== SEQ_START_CODE
)
84 pc
->frame_start_found
= 0;
85 if (pc
->frame_start_found
< 4 && state
== EXT_START_CODE
)
86 pc
->frame_start_found
++;
87 if (pc
->frame_start_found
== 4 && (state
& 0xFFFFFF00) == 0x100) {
88 if (state
< SLICE_MIN_START_CODE
|| state
> SLICE_MAX_START_CODE
) {
89 pc
->frame_start_found
= 0;
94 if (pc
->frame_start_found
== 0 && s
&& state
== PICTURE_START_CODE
) {
95 ff_fetch_timestamp(s
, i
- 3, 1, i
> 3);
100 return END_NOT_FOUND
;
103 static void mpegvideo_extract_headers(AVCodecParserContext
*s
,
104 AVCodecContext
*avctx
,
105 const uint8_t *buf
, int buf_size
)
107 struct MpvParseContext
*pc
= s
->priv_data
;
108 const uint8_t *buf_end
= buf
+ buf_size
;
114 enum AVPixelFormat pix_fmt
= AV_PIX_FMT_NONE
;
116 // number of picture coding extensions (i.e. MPEG2 pictures)
117 // in this packet - should be 1 or 2
119 // when there are two pictures in the packet this indicates
120 // which field is in the first of them
121 int first_field
= AV_FIELD_UNKNOWN
;
123 //FIXME replace the crap with get_bits()
124 while (buf
< buf_end
) {
125 uint32_t start_code
= -1;
126 buf
= avpriv_find_start_code(buf
, buf_end
, &start_code
);
127 bytes_left
= buf_end
- buf
;
129 case PICTURE_START_CODE
:
130 if (bytes_left
>= 2) {
131 s
->pict_type
= (buf
[1] >> 3) & 7;
133 vbv_delay
= ((buf
[1] & 0x07) << 13) | (buf
[2] << 5) | (buf
[3] >> 3);
137 if (bytes_left
>= 7) {
138 int frame_rate_index
;
140 pc
->width
= (buf
[0] << 4) | (buf
[1] >> 4);
141 pc
->height
= ((buf
[1] & 0x0f) << 8) | buf
[2];
142 if(!avctx
->width
|| !avctx
->height
|| !avctx
->coded_width
|| !avctx
->coded_height
){
143 set_dim_ret
= ff_set_dimensions(avctx
, pc
->width
, pc
->height
);
146 pix_fmt
= AV_PIX_FMT_YUV420P
;
147 frame_rate_index
= buf
[3] & 0xf;
148 pc
->frame_rate
= avctx
->framerate
= ff_mpeg12_frame_rate_tab
[frame_rate_index
];
149 bit_rate
= (buf
[4]<<10) | (buf
[5]<<2) | (buf
[6]>>6);
150 avctx
->codec_id
= AV_CODEC_ID_MPEG1VIDEO
;
154 if (bytes_left
>= 1) {
155 switch (buf
[0] >> 4) { // ext_type
156 case 0x1: /* sequence extension */
157 if (bytes_left
>= 6) {
158 int horiz_size_ext
= ((buf
[1] & 1) << 1) | (buf
[2] >> 7);
159 int vert_size_ext
= (buf
[2] >> 5) & 3;
160 int bit_rate_ext
= ((buf
[2] & 0x1F)<<7) | (buf
[3]>>1);
161 int frame_rate_ext_n
= (buf
[5] >> 5) & 3;
162 int frame_rate_ext_d
= (buf
[5] & 0x1f);
163 pc
->progressive_sequence
= buf
[1] & (1 << 3);
164 avctx
->has_b_frames
= !(buf
[5] >> 7);
166 switch ((buf
[1] >> 1) & 3) { // chroma_format
167 case 1: pix_fmt
= AV_PIX_FMT_YUV420P
; break;
168 case 2: pix_fmt
= AV_PIX_FMT_YUV422P
; break;
169 case 3: pix_fmt
= AV_PIX_FMT_YUV444P
; break;
172 pc
->width
= (pc
->width
& 0xFFF) | (horiz_size_ext
<< 12);
173 pc
->height
= (pc
->height
& 0xFFF) | ( vert_size_ext
<< 12);
174 bit_rate
= (bit_rate
&0x3FFFF) | (bit_rate_ext
<< 18);
176 set_dim_ret
= ff_set_dimensions(avctx
, pc
->width
, pc
->height
);
177 avctx
->framerate
.num
= pc
->frame_rate
.num
* (frame_rate_ext_n
+ 1);
178 avctx
->framerate
.den
= pc
->frame_rate
.den
* (frame_rate_ext_d
+ 1);
179 avctx
->codec_id
= AV_CODEC_ID_MPEG2VIDEO
;
182 case 0x8: /* picture coding extension */
183 if (bytes_left
>= 5) {
184 int top_field_first
= buf
[3] & (1 << 7);
185 int repeat_first_field
= buf
[3] & (1 << 1);
186 int progressive_frame
= buf
[4] & (1 << 7);
188 /* check if we must repeat the frame */
190 if (repeat_first_field
) {
191 if (pc
->progressive_sequence
) {
196 } else if (progressive_frame
) {
201 if (!pc
->progressive_sequence
&& !progressive_frame
) {
203 s
->field_order
= AV_FIELD_TT
;
205 s
->field_order
= AV_FIELD_BB
;
207 s
->field_order
= AV_FIELD_PROGRESSIVE
;
209 s
->picture_structure
= buf
[2] & 3;
212 // remember parity of the first field for the case
213 // when there are 2 fields in packet
214 switch (s
->picture_structure
) {
215 case AV_PICTURE_STRUCTURE_BOTTOM_FIELD
: first_field
= AV_FIELD_BB
; break;
216 case AV_PICTURE_STRUCTURE_TOP_FIELD
: first_field
= AV_FIELD_TT
; break;
229 /* we stop parsing when we encounter a slice. It ensures
230 that this function takes a negligible amount of time */
231 if (start_code
>= SLICE_MIN_START_CODE
&&
232 start_code
<= SLICE_MAX_START_CODE
)
239 av_log(avctx
, AV_LOG_ERROR
, "Failed to set dimensions\n");
241 if (avctx
->codec_id
== AV_CODEC_ID_MPEG2VIDEO
&& bit_rate
&& bit_rate
!= 0x3FFFF) {
242 avctx
->rc_max_rate
= 400LL*bit_rate
;
245 ((avctx
->codec_id
== AV_CODEC_ID_MPEG1VIDEO
&& bit_rate
!= 0x3FFFF) || vbv_delay
!= 0xFFFF)) {
246 avctx
->bit_rate
= 400LL*bit_rate
;
249 if (pix_fmt
!= AV_PIX_FMT_NONE
) {
251 s
->width
= pc
->width
;
252 s
->height
= pc
->height
;
253 s
->coded_width
= FFALIGN(pc
->width
, 16);
254 s
->coded_height
= FFALIGN(pc
->height
, 16);
257 if (avctx
->codec_id
== AV_CODEC_ID_MPEG1VIDEO
|| nb_pic_ext
> 1) {
259 s
->picture_structure
= AV_PICTURE_STRUCTURE_FRAME
;
260 s
->field_order
= nb_pic_ext
> 1 ? first_field
: AV_FIELD_PROGRESSIVE
;
264 static int mpegvideo_parse(AVCodecParserContext
*s
,
265 AVCodecContext
*avctx
,
266 const uint8_t **poutbuf
, int *poutbuf_size
,
267 const uint8_t *buf
, int buf_size
)
269 struct MpvParseContext
*pc1
= s
->priv_data
;
270 ParseContext
*pc
= &pc1
->pc
;
273 if(s
->flags
& PARSER_FLAG_COMPLETE_FRAMES
){
276 next
= mpeg1_find_frame_end(pc
, buf
, buf_size
, s
);
278 if (ff_combine_frame(pc
, next
, &buf
, &buf_size
) < 0) {
285 /* we have a full frame : we just parse the first few MPEG headers
286 to have the full timing information. The time take by this
287 function should be negligible for uncorrupted streams */
288 mpegvideo_extract_headers(s
, avctx
, buf
, buf_size
);
289 ff_dlog(NULL
, "pict_type=%d frame_rate=%0.3f repeat_pict=%d\n",
290 s
->pict_type
, av_q2d(avctx
->framerate
), s
->repeat_pict
);
293 *poutbuf_size
= buf_size
;
297 static av_cold
int mpegvideo_parse_init(AVCodecParserContext
*s
)
299 s
->pict_type
= AV_PICTURE_TYPE_NONE
; // first frame might be partial
303 const FFCodecParser ff_mpegvideo_parser
= {
304 PARSER_CODEC_LIST(AV_CODEC_ID_MPEG1VIDEO
, AV_CODEC_ID_MPEG2VIDEO
),
305 .priv_data_size
= sizeof(struct MpvParseContext
),
306 .init
= mpegvideo_parse_init
,
307 .parse
= mpegvideo_parse
,
308 .close
= ff_parse_close
,