7f50d4ac |
/*
* MPEG1 / MPEG2 video parser |
406792e7 |
* Copyright (c) 2000,2001 Fabrice Bellard |
7f50d4ac |
* Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "parser.h" |
eee2000b |
#include "mpeg12.h" |
f1e93986 |
#include "internal.h" |
7f50d4ac |
|
07554ace |
struct MpvParseContext {
ParseContext pc;
AVRational frame_rate;
int progressive_sequence;
int width, height;
};
|
7f50d4ac |
static void mpegvideo_extract_headers(AVCodecParserContext *s,
AVCodecContext *avctx,
const uint8_t *buf, int buf_size)
{ |
07554ace |
struct MpvParseContext *pc = s->priv_data; |
7debef33 |
const uint8_t *buf_end = buf + buf_size; |
7f50d4ac |
uint32_t start_code;
int frame_rate_index, ext_type, bytes_left;
int frame_rate_ext_n, frame_rate_ext_d; |
e65ab9d9 |
int top_field_first, repeat_first_field, progressive_frame; |
7f50d4ac |
int horiz_size_ext, vert_size_ext, bit_rate_ext; |
442d7dd8 |
int did_set_size=0; |
740959fd |
int bit_rate = 0; |
25b7aa98 |
int vbv_delay = 0; |
7f50d4ac |
//FIXME replace the crap with get_bits()
s->repeat_pict = 0; |
7debef33 |
|
7f50d4ac |
while (buf < buf_end) {
start_code= -1; |
f1e93986 |
buf= avpriv_find_start_code(buf, buf_end, &start_code); |
7f50d4ac |
bytes_left = buf_end - buf;
switch(start_code) {
case PICTURE_START_CODE:
if (bytes_left >= 2) {
s->pict_type = (buf[1] >> 3) & 7; |
92834609 |
if (bytes_left >= 4) |
25b7aa98 |
vbv_delay = ((buf[1] & 0x07) << 13) | (buf[2] << 5) | (buf[3] >> 3); |
7f50d4ac |
}
break;
case SEQ_START_CODE:
if (bytes_left >= 7) {
pc->width = (buf[0] << 4) | (buf[1] >> 4);
pc->height = ((buf[1] & 0x0f) << 8) | buf[2]; |
442d7dd8 |
if(!avctx->width || !avctx->height || !avctx->coded_width || !avctx->coded_height){ |
6da9c9d3 |
ff_set_dimensions(avctx, pc->width, pc->height); |
442d7dd8 |
did_set_size=1;
} |
7f50d4ac |
frame_rate_index = buf[3] & 0xf; |
7ea1b347 |
pc->frame_rate = avctx->framerate = ff_mpeg12_frame_rate_tab[frame_rate_index]; |
740959fd |
bit_rate = (buf[4]<<10) | (buf[5]<<2) | (buf[6]>>6); |
36ef5369 |
avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO; |
620e7f0f |
avctx->ticks_per_frame = 1; |
7f50d4ac |
}
break;
case EXT_START_CODE:
if (bytes_left >= 1) {
ext_type = (buf[0] >> 4);
switch(ext_type) {
case 0x1: /* sequence extension */
if (bytes_left >= 6) {
horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7);
vert_size_ext = (buf[2] >> 5) & 3;
bit_rate_ext = ((buf[2] & 0x1F)<<7) | (buf[3]>>1);
frame_rate_ext_n = (buf[5] >> 5) & 3;
frame_rate_ext_d = (buf[5] & 0x1f);
pc->progressive_sequence = buf[1] & (1 << 3);
avctx->has_b_frames= !(buf[5] >> 7);
pc->width |=(horiz_size_ext << 12);
pc->height |=( vert_size_ext << 12); |
740959fd |
bit_rate = (bit_rate&0x3FFFF) | (bit_rate_ext << 18); |
442d7dd8 |
if(did_set_size) |
6da9c9d3 |
ff_set_dimensions(avctx, pc->width, pc->height); |
620e7f0f |
avctx->framerate.num = pc->frame_rate.num * (frame_rate_ext_n + 1); |
7ea1b347 |
avctx->framerate.den = pc->frame_rate.den * (frame_rate_ext_d + 1); |
36ef5369 |
avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO; |
620e7f0f |
avctx->ticks_per_frame = 2; |
7f50d4ac |
}
break;
case 0x8: /* picture coding extension */
if (bytes_left >= 5) {
top_field_first = buf[3] & (1 << 7);
repeat_first_field = buf[3] & (1 << 1);
progressive_frame = buf[4] & (1 << 7);
/* check if we must repeat the frame */ |
edbd72ac |
s->repeat_pict = 1; |
7f50d4ac |
if (repeat_first_field) {
if (pc->progressive_sequence) {
if (top_field_first) |
edbd72ac |
s->repeat_pict = 5; |
7f50d4ac |
else |
edbd72ac |
s->repeat_pict = 3; |
7f50d4ac |
} else if (progressive_frame) { |
edbd72ac |
s->repeat_pict = 2; |
7f50d4ac |
}
} |
f43c14d0 |
if (!pc->progressive_sequence) {
if (top_field_first)
s->field_order = AV_FIELD_TT;
else
s->field_order = AV_FIELD_BB;
} else
s->field_order = AV_FIELD_PROGRESSIVE; |
7f50d4ac |
}
break;
}
}
break;
case -1:
goto the_end;
default:
/* we stop parsing when we encounter a slice. It ensures
that this function takes a negligible amount of time */
if (start_code >= SLICE_MIN_START_CODE &&
start_code <= SLICE_MAX_START_CODE)
goto the_end;
break;
}
}
the_end: ; |
25b7aa98 |
if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO && bit_rate) {
avctx->rc_max_rate = 400*bit_rate; |
d647ef0c |
}
if (bit_rate &&
((avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO && bit_rate != 0x3FFFF) || vbv_delay != 0xFFFF)) { |
25b7aa98 |
avctx->bit_rate = 400*bit_rate; |
740959fd |
} |
7ea1b347 |
#if FF_API_AVCTX_TIMEBASE
if (avctx->framerate.num) |
620e7f0f |
avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1})); |
7ea1b347 |
#endif |
7f50d4ac |
}
static int mpegvideo_parse(AVCodecParserContext *s,
AVCodecContext *avctx, |
c53d2d90 |
const uint8_t **poutbuf, int *poutbuf_size, |
7f50d4ac |
const uint8_t *buf, int buf_size)
{ |
07554ace |
struct MpvParseContext *pc1 = s->priv_data; |
7f50d4ac |
ParseContext *pc= &pc1->pc;
int next;
if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
next= buf_size;
}else{ |
a4c7a5ea |
next= ff_mpeg1_find_frame_end(pc, buf, buf_size, s); |
7f50d4ac |
|
c53d2d90 |
if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) { |
7f50d4ac |
*poutbuf = NULL;
*poutbuf_size = 0;
return buf_size;
}
}
/* we have a full frame : we just parse the first few MPEG headers
to have the full timing information. The time take by this
function should be negligible for uncorrupted streams */
mpegvideo_extract_headers(s, avctx, buf, buf_size); |
aebb56e1 |
av_dlog(NULL, "pict_type=%d frame_rate=%0.3f repeat_pict=%d\n", |
620e7f0f |
s->pict_type, av_q2d(avctx->framerate), s->repeat_pict); |
7f50d4ac |
|
c53d2d90 |
*poutbuf = buf; |
7f50d4ac |
*poutbuf_size = buf_size;
return next;
}
static int mpegvideo_split(AVCodecContext *avctx,
const uint8_t *buf, int buf_size)
{
int i;
uint32_t state= -1; |
73fb23dc |
int found=0; |
7f50d4ac |
for(i=0; i<buf_size; i++){
state= (state<<8) | buf[i]; |
73fb23dc |
if(state == 0x1B3){
found=1;
}else if(found && state != 0x1B5 && state < 0x200 && state >= 0x100) |
7f50d4ac |
return i-3;
}
return 0;
}
|
f8353d5f |
static int mpegvideo_parse_init(AVCodecParserContext *s)
{
s->pict_type = AV_PICTURE_TYPE_NONE; // first frame might be partial
return 0;
}
|
e7e2df27 |
AVCodecParser ff_mpegvideo_parser = { |
36ef5369 |
.codec_ids = { AV_CODEC_ID_MPEG1VIDEO, AV_CODEC_ID_MPEG2VIDEO }, |
07554ace |
.priv_data_size = sizeof(struct MpvParseContext), |
f8353d5f |
.parser_init = mpegvideo_parse_init, |
5511ad14 |
.parser_parse = mpegvideo_parse, |
07554ace |
.parser_close = ff_parse_close, |
5511ad14 |
.split = mpegvideo_split, |
7f50d4ac |
}; |