Newer
Older
memset(st->codec->extradata + i, 0, FF_INPUT_BUFFER_PADDING_SIZE);
Michael Niedermayer
committed
}
}
/* if still no information, we try to open the codec and to
decompress the frame. We try to avoid that in most cases as
it takes longer and uses more memory. For MPEG4, we need to
decompress for Quicktime. */
Michael Niedermayer
committed
if (!has_codec_parameters(st->codec) /*&&
(st->codec->codec_id == CODEC_ID_FLV1 ||
st->codec->codec_id == CODEC_ID_H264 ||
st->codec->codec_id == CODEC_ID_H263 ||
st->codec->codec_id == CODEC_ID_H261 ||
st->codec->codec_id == CODEC_ID_VORBIS ||
st->codec->codec_id == CODEC_ID_MJPEG ||
st->codec->codec_id == CODEC_ID_PNG ||
st->codec->codec_id == CODEC_ID_PAM ||
st->codec->codec_id == CODEC_ID_PGM ||
st->codec->codec_id == CODEC_ID_PGMYUV ||
st->codec->codec_id == CODEC_ID_PBM ||
st->codec->codec_id == CODEC_ID_PPM ||
st->codec->codec_id == CODEC_ID_SHORTEN ||
(st->codec->codec_id == CODEC_ID_MPEG4 && !st->need_parsing))*/)
try_decode_frame(st, pkt->data, pkt->size);
if (av_rescale_q(st->codec_info_duration, st->time_base, AV_TIME_BASE_Q) >= MAX_STREAM_DURATION) {
Fabrice Bellard
committed
}
count++;
}
Michael Niedermayer
committed
// close codecs which where opened in try_decode_frame()
for(i=0;i<ic->nb_streams;i++) {
st = ic->streams[i];
Michael Niedermayer
committed
if(st->codec->codec)
avcodec_close(st->codec);
Michael Niedermayer
committed
}
Fabrice Bellard
committed
for(i=0;i<ic->nb_streams;i++) {
st = ic->streams[i];
Michael Niedermayer
committed
if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
if(st->codec->codec_id == CODEC_ID_RAWVIDEO && !st->codec->codec_tag && !st->codec->bits_per_sample)
st->codec->codec_tag= avcodec_pix_fmt_to_codec_tag(st->codec->pix_fmt);
if(duration_count[i] && st->codec->time_base.num*101LL <= st->codec->time_base.den &&
st->time_base.num*duration_sum[i]/duration_count[i]*101LL > st->time_base.den){
int64_t num, den, error, best_error;
num= st->time_base.den*duration_count[i];
den= st->time_base.num*duration_sum[i];
best_error= INT64_MAX;
for(j=1; j<60*12; j++){
error= ABS(1001*12*num - 1001*j*den);
if(error < best_error){
best_error= error;
av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, j, 12, INT_MAX);
}
}
for(j=24; j<=30; j+=6){
error= ABS(1001*12*num - 1000*12*j*den);
if(error < best_error){
best_error= error;
av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, j*1000, 1001, INT_MAX);
}
/* compute the real frame rate for telecine */
Michael Niedermayer
committed
if ((st->codec->codec_id == CODEC_ID_MPEG1VIDEO ||
st->codec->codec_id == CODEC_ID_MPEG2VIDEO) &&
st->codec->sub_id == 2) {
if (st->codec_info_nb_frames >= 20) {
float coded_frame_rate, est_frame_rate;
est_frame_rate = ((double)st->codec_info_nb_frames * AV_TIME_BASE) /
(double)st->codec_info_duration ;
Michael Niedermayer
committed
coded_frame_rate = 1.0/av_q2d(st->codec->time_base);
printf("telecine: coded_frame_rate=%0.3f est_frame_rate=%0.3f\n",
coded_frame_rate, est_frame_rate);
#endif
/* if we detect that it could be a telecine, we
signal it. It would be better to do it at a
higher level as it can change in a film */
if (coded_frame_rate >= 24.97 &&
(est_frame_rate >= 23.5 && est_frame_rate < 24.5)) {
st->r_frame_rate = (AVRational){24000, 1001};
}
}
}
/* if no real frame rate, use the codec one */
Michael Niedermayer
committed
st->r_frame_rate.num = st->codec->time_base.den;
st->r_frame_rate.den = st->codec->time_base.num;
Michael Niedermayer
committed
}
Fabrice Bellard
committed
}
Fabrice Bellard
committed
Fabrice Bellard
committed
av_estimate_timings(ic);
#if 0
/* correct DTS for b frame streams with no timestamps */
for(i=0;i<ic->nb_streams;i++) {
st = ic->streams[i];
Michael Niedermayer
committed
if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
if(b-frames){
ppktl = &ic->packet_buffer;
while(ppkt1){
if(ppkt1->stream_index != i)
continue;
if(ppkt1->pkt->dts < 0)
break;
if(ppkt1->pkt->pts != AV_NOPTS_VALUE)
break;
ppkt1->pkt->dts -= delta;
ppkt1= ppkt1->next;
}
if(ppkt1)
continue;
st->cur_dts -= delta;
}
}
}
#endif
Fabrice Bellard
committed
return ret;
/*******************************************************/
/**
* start playing a network based stream (e.g. RTSP stream) at the
*/
int av_read_play(AVFormatContext *s)
{
if (!s->iformat->read_play)
return AVERROR_NOTSUPP;
return s->iformat->read_play(s);
}
/**
Daniel Kristjansson
committed
* Pause a network based stream (e.g. RTSP stream).
*
* Use av_read_play() to resume it.
*/
int av_read_pause(AVFormatContext *s)
{
if (!s->iformat->read_pause)
return AVERROR_NOTSUPP;
return s->iformat->read_pause(s);
}
Fabrice Bellard
committed
/**
Daniel Kristjansson
committed
* Close a media file (but not its codecs).
Fabrice Bellard
committed
*
* @param s media file handle
*/
AVStream *st;
/* free previous packet */
if (s->cur_st && s->cur_st->parser)
av_free_packet(&s->cur_pkt);
Fabrice Bellard
committed
if (s->iformat->read_close)
s->iformat->read_close(s);
/* free all data in a stream component */
st = s->streams[i];
if (st->parser) {
av_parser_close(st->parser);
av_free(st->index_entries);
av_free(st->codec->extradata);
Michael Niedermayer
committed
av_free(st->codec);
flush_packet_queue(s);
if (s->iformat->flags & AVFMT_NOFILE) {
must_open_file = 0;
}
if (must_open_file) {
Philip Gladstone
committed
av_freep(&s->priv_data);
Fabrice Bellard
committed
/**
Daniel Kristjansson
committed
* Add a new stream to a media file.
Fabrice Bellard
committed
*
Daniel Kristjansson
committed
* Can only be called in the read_header() function. If the flag
* AVFMTCTX_NOHEADER is in the format context, then new streams
* can be added in read_packet too.
Fabrice Bellard
committed
*
* @param s media file handle
* @param id file format dependent stream id
Fabrice Bellard
committed
*/
AVStream *av_new_stream(AVFormatContext *s, int id)
{
AVStream *st;
if (s->nb_streams >= MAX_STREAMS)
return NULL;
st = av_mallocz(sizeof(AVStream));
if (!st)
return NULL;
Michael Niedermayer
committed
st->codec= avcodec_alloc_context();
if (s->iformat) {
/* no default bitrate if decoding */
Michael Niedermayer
committed
st->codec->bit_rate = 0;
Fabrice Bellard
committed
st->index = s->nb_streams;
st->id = id;
Fabrice Bellard
committed
st->start_time = AV_NOPTS_VALUE;
st->duration = AV_NOPTS_VALUE;
st->cur_dts = AV_NOPTS_VALUE;
/* default pts settings is MPEG like */
av_set_pts_info(st, 33, 1, 90000);
Fabrice Bellard
committed
s->streams[s->nb_streams++] = st;
return st;
}
/************************************************************/
/* output media file */
int av_set_parameters(AVFormatContext *s, AVFormatParameters *ap)
{
int ret;
if (s->oformat->priv_data_size > 0) {
s->priv_data = av_mallocz(s->oformat->priv_data_size);
if (!s->priv_data)
return AVERROR_NOMEM;
} else
s->priv_data = NULL;
if (s->oformat->set_parameters) {
ret = s->oformat->set_parameters(s, ap);
if (ret < 0)
return ret;
}
return 0;
}
Fabrice Bellard
committed
/**
* allocate the stream private data and write the stream header to an
* output media file
*
* @param s media file handle
* @return 0 if OK. AVERROR_xxx if error.
Fabrice Bellard
committed
*/
int av_write_header(AVFormatContext *s)
{
int ret, i;
AVStream *st;
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
// some sanity checks
for(i=0;i<s->nb_streams;i++) {
st = s->streams[i];
switch (st->codec->codec_type) {
case CODEC_TYPE_AUDIO:
if(st->codec->sample_rate<=0){
av_log(s, AV_LOG_ERROR, "sample rate not set\n");
return -1;
}
break;
case CODEC_TYPE_VIDEO:
if(st->codec->time_base.num<=0 || st->codec->time_base.den<=0){ //FIXME audio too?
av_log(s, AV_LOG_ERROR, "time base not set\n");
return -1;
}
if(st->codec->width<=0 || st->codec->height<=0){
av_log(s, AV_LOG_ERROR, "dimensions not set\n");
return -1;
}
break;
}
}
if(s->oformat->write_header){
ret = s->oformat->write_header(s);
if (ret < 0)
return ret;
}
/* init PTS generation */
for(i=0;i<s->nb_streams;i++) {
Wolfram Gloger
committed
int64_t den = AV_NOPTS_VALUE;
Michael Niedermayer
committed
switch (st->codec->codec_type) {
Wolfram Gloger
committed
den = (int64_t)st->time_base.num * st->codec->sample_rate;
break;
case CODEC_TYPE_VIDEO:
Wolfram Gloger
committed
den = (int64_t)st->time_base.num * st->codec->time_base.den;
break;
default:
break;
}
Wolfram Gloger
committed
if (den != AV_NOPTS_VALUE) {
if (den <= 0)
return AVERROR_INVALIDDATA;
av_frac_init(&st->pts, 0, 0, den);
}
Fabrice Bellard
committed
}
//FIXME merge with compute_pkt_fields
Michael Niedermayer
committed
static int compute_pkt_fields2(AVStream *st, AVPacket *pkt){
Michael Niedermayer
committed
int b_frames = FFMAX(st->codec->has_b_frames, st->codec->max_b_frames);
int num, den, frame_size;
// av_log(NULL, AV_LOG_DEBUG, "av_write_frame: pts:%lld dts:%lld cur_dts:%lld b:%d size:%d st:%d\n", pkt->pts, pkt->dts, st->cur_dts, b_frames, pkt->size, pkt->stream_index);
/* if(pkt->pts == AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE)
return -1;*/
if (pkt->duration == 0) {
compute_frame_duration(&num, &den, st, NULL, pkt);
if (den && num) {
pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den, den * (int64_t)st->time_base.num);
}
}
//XXX/FIXME this is a temporary hack until all encoders output pts
if((pkt->pts == 0 || pkt->pts == AV_NOPTS_VALUE) && pkt->dts == AV_NOPTS_VALUE && !b_frames){
pkt->dts=
// pkt->pts= st->cur_dts;
pkt->pts= st->pts.val;
}
if(pkt->pts != AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE){
if(b_frames){
if(st->last_IP_pts == AV_NOPTS_VALUE){
st->last_IP_pts= -pkt->duration;
}
if(st->last_IP_pts < pkt->pts){
pkt->dts= st->last_IP_pts;
st->last_IP_pts= pkt->pts;
}else
pkt->dts= pkt->pts;
}else
pkt->dts= pkt->pts;
}
Michael Niedermayer
committed
if(st->cur_dts && st->cur_dts != AV_NOPTS_VALUE && st->cur_dts >= pkt->dts){
av_log(NULL, AV_LOG_ERROR, "error, non monotone timestamps %"PRId64" >= %"PRId64"\n", st->cur_dts, pkt->dts);
Michael Niedermayer
committed
return -1;
}
if(pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts < pkt->dts){
av_log(NULL, AV_LOG_ERROR, "error, pts < dts\n");
return -1;
}
// av_log(NULL, AV_LOG_DEBUG, "av_write_frame: pts2:%lld dts2:%lld\n", pkt->pts, pkt->dts);
st->cur_dts= pkt->dts;
st->pts.val= pkt->dts;
Michael Niedermayer
committed
switch (st->codec->codec_type) {
Michael Niedermayer
committed
frame_size = get_audio_frame_size(st->codec, pkt->size);
/* HACK/FIXME, we skip the initial 0-size packets as they are most likely equal to the encoder delay,
but it would be better if we had the real timestamps from the encoder */
if (frame_size >= 0 && (pkt->size || st->pts.num!=st->pts.den>>1 || st->pts.val)) {
av_frac_add(&st->pts, (int64_t)st->time_base.den * frame_size);
Fabrice Bellard
committed
}
break;
case CODEC_TYPE_VIDEO:
Michael Niedermayer
committed
av_frac_add(&st->pts, (int64_t)st->time_base.den * st->codec->time_base.num);
break;
default:
break;
}
Michael Niedermayer
committed
return 0;
}
static void truncate_ts(AVStream *st, AVPacket *pkt){
int64_t pts_mask = (2LL << (st->pts_wrap_bits-1)) - 1;
// if(pkt->dts < 0)
// pkt->dts= 0; //this happens for low_delay=0 and b frames, FIXME, needs further invstigation about what we should do here
pkt->pts &= pts_mask;
pkt->dts &= pts_mask;
}
/**
Daniel Kristjansson
committed
* Write a packet to an output media file.
*
* The packet shall contain one audio or video frame.
*
* @param s media file handle
* @param pkt the packet, which contains the stream_index, buf/buf_size, dts/pts, ...
* @return < 0 if error, = 0 if OK, 1 if end of stream wanted.
*/
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
{
Michael Niedermayer
committed
ret=compute_pkt_fields2(s->streams[pkt->stream_index], pkt);
if(ret<0)
return ret;
truncate_ts(s->streams[pkt->stream_index], pkt);
ret= s->oformat->write_packet(s, pkt);
if(!ret)
ret= url_ferror(&s->pb);
return ret;
Michael Niedermayer
committed
/**
* interleave_packet implementation which will interleave per DTS.
* packets with pkt->destruct == av_destruct_packet will be freed inside this function.
* so they cannot be used after it, note calling av_free_packet() on them is still safe
Michael Niedermayer
committed
*/
static int av_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush){
AVPacketList *pktl, **next_point, *this_pktl;
int stream_count=0;
int streams[MAX_STREAMS];
if(pkt){
AVStream *st= s->streams[ pkt->stream_index];
// assert(pkt->destruct != av_destruct_packet); //FIXME
Michael Niedermayer
committed
this_pktl = av_mallocz(sizeof(AVPacketList));
this_pktl->pkt= *pkt;
if(pkt->destruct == av_destruct_packet)
pkt->destruct= NULL; // non shared -> must keep original from being freed
else
av_dup_packet(&this_pktl->pkt); //shared -> must dup
Michael Niedermayer
committed
next_point = &s->packet_buffer;
while(*next_point){
AVStream *st2= s->streams[ (*next_point)->pkt.stream_index];
int64_t left= st2->time_base.num * (int64_t)st ->time_base.den;
int64_t right= st ->time_base.num * (int64_t)st2->time_base.den;
if((*next_point)->pkt.dts * left > pkt->dts * right) //FIXME this can overflow
break;
next_point= &(*next_point)->next;
}
this_pktl->next= *next_point;
*next_point= this_pktl;
}
Michael Niedermayer
committed
memset(streams, 0, sizeof(streams));
pktl= s->packet_buffer;
while(pktl){
//av_log(s, AV_LOG_DEBUG, "show st:%d dts:%lld\n", pktl->pkt.stream_index, pktl->pkt.dts);
if(streams[ pktl->pkt.stream_index ] == 0)
stream_count++;
streams[ pktl->pkt.stream_index ]++;
pktl= pktl->next;
}
Michael Niedermayer
committed
if(s->nb_streams == stream_count || (flush && stream_count)){
pktl= s->packet_buffer;
*out= pktl->pkt;
s->packet_buffer= pktl->next;
Michael Niedermayer
committed
av_freep(&pktl);
return 1;
}else{
av_init_packet(out);
return 0;
}
}
/**
* Interleaves a AVPacket correctly so it can be muxed.
* @param out the interleaved packet will be output here
* @param in the input packet
* @param flush 1 if no further packets are available as input and all
* remaining packets should be output
* @return 1 if a packet was output, 0 if no packet could be output,
Michael Niedermayer
committed
* < 0 if an error occured
*/
static int av_interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, int flush){
if(s->oformat->interleave_packet)
return s->oformat->interleave_packet(s, out, in, flush);
else
return av_interleave_packet_per_dts(s, out, in, flush);
}
Daniel Kristjansson
committed
* Writes a packet to an output media file ensuring correct interleaving.
*
* The packet must contain one audio or video frame.
* If the packets are already correctly interleaved the application should
* call av_write_frame() instead as its slightly faster, its also important
Michael Niedermayer
committed
* to keep in mind that completly non interleaved input will need huge amounts
* of memory to interleave with this, so its prefereable to interleave at the
* demuxer level
*
* @param s media file handle
* @param pkt the packet, which contains the stream_index, buf/buf_size, dts/pts, ...
* @return < 0 if error, = 0 if OK, 1 if end of stream wanted.
*/
int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt){
AVStream *st= s->streams[ pkt->stream_index];
//FIXME/XXX/HACK drop zero sized packets
Michael Niedermayer
committed
if(st->codec->codec_type == CODEC_TYPE_AUDIO && pkt->size==0)
Michael Niedermayer
committed
//av_log(NULL, AV_LOG_DEBUG, "av_interleaved_write_frame %d %Ld %Ld\n", pkt->size, pkt->dts, pkt->pts);
if(compute_pkt_fields2(st, pkt) < 0)
return -1;
if(pkt->dts == AV_NOPTS_VALUE)
return -1;
Michael Niedermayer
committed
for(;;){
AVPacket opkt;
int ret= av_interleave_packet(s, &opkt, pkt, 0);
if(ret<=0) //FIXME cleanup needed for ret<0 ?
return ret;
Michael Niedermayer
committed
truncate_ts(s->streams[opkt.stream_index], &opkt);
ret= s->oformat->write_packet(s, &opkt);
Michael Niedermayer
committed
av_free_packet(&opkt);
pkt= NULL;
if(ret<0)
return ret;
if(url_ferror(&s->pb))
return url_ferror(&s->pb);
Fabrice Bellard
committed
}
/**
Daniel Kristjansson
committed
* @brief Write the stream trailer to an output media file and
* free the file private data.
Fabrice Bellard
committed
*
* @param s media file handle
Daniel Kristjansson
committed
* @return 0 if OK. AVERROR_xxx if error.
*/
Fabrice Bellard
committed
int av_write_trailer(AVFormatContext *s)
{
int ret, i;
Michael Niedermayer
committed
for(;;){
AVPacket pkt;
ret= av_interleave_packet(s, &pkt, NULL, 1);
if(ret<0) //FIXME cleanup needed for ret<0 ?
goto fail;
Michael Niedermayer
committed
if(!ret)
break;
Michael Niedermayer
committed
truncate_ts(s->streams[pkt.stream_index], &pkt);
ret= s->oformat->write_packet(s, &pkt);
Michael Niedermayer
committed
av_free_packet(&pkt);
goto fail;
if(s->oformat->write_trailer)
ret = s->oformat->write_trailer(s);
for(i=0;i<s->nb_streams;i++)
av_freep(&s->streams[i]->priv_data);
Fabrice Bellard
committed
av_freep(&s->priv_data);
return ret;
}
/* "user interface" functions */
void dump_format(AVFormatContext *ic,
Fabrice Bellard
committed
int i, flags;
av_log(NULL, AV_LOG_INFO, "%s #%d, %s, %s '%s':\n",
index,
is_output ? ic->oformat->name : ic->iformat->name,
Fabrice Bellard
committed
if (!is_output) {
av_log(NULL, AV_LOG_INFO, " Duration: ");
Fabrice Bellard
committed
if (ic->duration != AV_NOPTS_VALUE) {
int hours, mins, secs, us;
secs = ic->duration / AV_TIME_BASE;
us = ic->duration % AV_TIME_BASE;
mins = secs / 60;
secs %= 60;
hours = mins / 60;
mins %= 60;
av_log(NULL, AV_LOG_INFO, "%02d:%02d:%02d.%01d", hours, mins, secs,
Fabrice Bellard
committed
(10 * us) / AV_TIME_BASE);
} else {
av_log(NULL, AV_LOG_INFO, "N/A");
Fabrice Bellard
committed
}
Wolfram Gloger
committed
if (ic->start_time != AV_NOPTS_VALUE) {
int secs, us;
av_log(NULL, AV_LOG_INFO, ", start: ");
Wolfram Gloger
committed
secs = ic->start_time / AV_TIME_BASE;
us = ic->start_time % AV_TIME_BASE;
av_log(NULL, AV_LOG_INFO, "%d.%06d",
Wolfram Gloger
committed
secs, (int)av_rescale(us, 1000000, AV_TIME_BASE));
}
av_log(NULL, AV_LOG_INFO, ", bitrate: ");
Fabrice Bellard
committed
if (ic->bit_rate) {
av_log(NULL, AV_LOG_INFO,"%d kb/s", ic->bit_rate / 1000);
Fabrice Bellard
committed
} else {
av_log(NULL, AV_LOG_INFO, "N/A");
Fabrice Bellard
committed
}
av_log(NULL, AV_LOG_INFO, "\n");
Fabrice Bellard
committed
}
for(i=0;i<ic->nb_streams;i++) {
AVStream *st = ic->streams[i];
int g= ff_gcd(st->time_base.num, st->time_base.den);
Michael Niedermayer
committed
avcodec_string(buf, sizeof(buf), st->codec, is_output);
av_log(NULL, AV_LOG_INFO, " Stream #%d.%d", index, i);
Fabrice Bellard
committed
/* the pid is an important information, so we display it */
/* XXX: add a generic system */
if (is_output)
flags = ic->oformat->flags;
else
flags = ic->iformat->flags;
if (flags & AVFMT_SHOW_IDS) {
av_log(NULL, AV_LOG_INFO, "[0x%x]", st->id);
Fabrice Bellard
committed
}
if (strlen(st->language) > 0) {
av_log(NULL, AV_LOG_INFO, "(%s)", st->language);
}
av_log(NULL, AV_LOG_DEBUG, ", %d/%d", st->time_base.num/g, st->time_base.den/g);
av_log(NULL, AV_LOG_INFO, ": %s", buf);
if(st->codec->codec_type == CODEC_TYPE_VIDEO){
if(st->r_frame_rate.den && st->r_frame_rate.num)
av_log(NULL, AV_LOG_INFO, ", %5.2f fps(r)", av_q2d(st->r_frame_rate));
/* else if(st->time_base.den && st->time_base.num)
av_log(NULL, AV_LOG_INFO, ", %5.2f fps(m)", 1/av_q2d(st->time_base));*/
else
av_log(NULL, AV_LOG_INFO, ", %5.2f fps(c)", 1/av_q2d(st->codec->time_base));
}
av_log(NULL, AV_LOG_INFO, "\n");
int frame_rate, frame_rate_base;
} AbvEntry;
static AbvEntry frame_abvs[] = {
{ "ntsc", 720, 480, 30000, 1001 },
{ "pal", 720, 576, 25, 1 },
{ "qntsc", 352, 240, 30000, 1001 }, /* VCD compliant ntsc */
{ "qpal", 352, 288, 25, 1 }, /* VCD compliant pal */
{ "sntsc", 640, 480, 30000, 1001 }, /* square pixel ntsc */
{ "spal", 768, 576, 25, 1 }, /* square pixel pal */
{ "film", 352, 240, 24, 1 },
{ "ntsc-film", 352, 240, 24000, 1001 },
{ "sqcif", 128, 96, 0, 0 },
{ "qcif", 176, 144, 0, 0 },
{ "cif", 352, 288, 0, 0 },
{ "4cif", 704, 576, 0, 0 },
Daniel Kristjansson
committed
/**
* parses width and height out of string str.
*/
int parse_image_size(int *width_ptr, int *height_ptr, const char *str)
{
int i;
int n = sizeof(frame_abvs) / sizeof(AbvEntry);
const char *p;
int frame_width = 0, frame_height = 0;
for(i=0;i<n;i++) {
if (!strcmp(frame_abvs[i].abv, str)) {
frame_width = frame_abvs[i].width;
frame_height = frame_abvs[i].height;
break;
}
}
if (i == n) {
p = str;
frame_width = strtol(p, (char **)&p, 10);
if (*p)
p++;
frame_height = strtol(p, (char **)&p, 10);
}
if (frame_width <= 0 || frame_height <= 0)
return -1;
*width_ptr = frame_width;
*height_ptr = frame_height;
return 0;
}
Daniel Kristjansson
committed
/**
* Converts frame rate from string to a fraction.
*
* First we try to get an exact integer or fractional frame rate.
* If this fails we convert the frame rate to a double and return
* an approximate fraction using the DEFAULT_FRAME_RATE_BASE.
*/
int parse_frame_rate(int *frame_rate, int *frame_rate_base, const char *arg)
{
int i;
char* cp;
/* First, we check our abbreviation table */
for (i = 0; i < sizeof(frame_abvs)/sizeof(*frame_abvs); ++i)
if (!strcmp(frame_abvs[i].abv, arg)) {
*frame_rate = frame_abvs[i].frame_rate;
*frame_rate_base = frame_abvs[i].frame_rate_base;
return 0;
}
/* Then, we try to parse it as fraction */
cp = strchr(arg, '/');
Roine Gustafsson
committed
if (!cp)
cp = strchr(arg, ':');
*frame_rate = strtol(arg, &cpp, 10);
if (cpp != arg || cpp == cp)
*frame_rate_base = strtol(cp+1, &cpp, 10);
else
*frame_rate = 0;
else {
/* Finally we give up and parse it as double */
AVRational time_base = av_d2q(strtod(arg, 0), DEFAULT_FRAME_RATE_BASE);
*frame_rate_base = time_base.den;
*frame_rate = time_base.num;
}
if (!*frame_rate || !*frame_rate_base)
return -1;
else
return 0;
}
Daniel Kristjansson
committed
/**
* Converts date string to number of seconds since Jan 1st, 1970.
*
* @code
* Syntax:
* - If not a duration:
* [{YYYY-MM-DD|YYYYMMDD}]{T| }{HH[:MM[:SS[.m...]]][Z]|HH[MM[SS[.m...]]][Z]}
Philip Gladstone
committed
* Time is localtime unless Z is suffixed to the end. In this case GMT
* Return the date in micro seconds since 1970
Daniel Kristjansson
committed
*
* - If a duration:
Daniel Kristjansson
committed
* @endcode
int64_t parse_date(const char *datestr, int duration)
Philip Gladstone
committed
struct tm dt;
Philip Gladstone
committed
int i;
static const char *date_fmt[] = {
"%Y-%m-%d",
"%Y%m%d",
};
static const char *time_fmt[] = {
"%H:%M:%S",
"%H%M%S",
};
const char *q;
Philip Gladstone
committed
char lastch;
#undef time
Philip Gladstone
committed
time_t now = time(0);
len = strlen(datestr);
if (len > 0)
lastch = datestr[len - 1];
else
lastch = '\0';
Philip Gladstone
committed
is_utc = (lastch == 'z' || lastch == 'Z');
Philip Gladstone
committed
memset(&dt, 0, sizeof(dt));
Philip Gladstone
committed
for (i = 0; i < sizeof(date_fmt) / sizeof(date_fmt[0]); i++) {
Fabrice Bellard
committed
q = small_strptime(p, date_fmt[i], &dt);
Philip Gladstone
committed
if (q) {
break;
}
}
if (!q) {
if (is_utc) {
dt = *gmtime(&now);
} else {
dt = *localtime(&now);
}
dt.tm_hour = dt.tm_min = dt.tm_sec = 0;
Philip Gladstone
committed
p = q;
Philip Gladstone
committed
if (*p == 'T' || *p == 't' || *p == ' ')
p++;
for (i = 0; i < sizeof(time_fmt) / sizeof(time_fmt[0]); i++) {
Fabrice Bellard
committed
q = small_strptime(p, time_fmt[i], &dt);
if (q) {
break;
}
}
} else {
if (p[0] == '-') {
negative = 1;
++p;
}
Fabrice Bellard
committed
q = small_strptime(p, time_fmt[0], &dt);
if (!q) {
dt.tm_sec = strtol(p, (char **)&q, 10);
dt.tm_min = 0;
dt.tm_hour = 0;
Philip Gladstone
committed
}
}
/* Now we have all the fields that we can get */
if (!q) {
if (duration)
return 0;
else
Philip Gladstone
committed
if (duration) {
Philip Gladstone
committed
t = dt.tm_hour * 3600 + dt.tm_min * 60 + dt.tm_sec;
Philip Gladstone
committed
} else {
Philip Gladstone
committed
dt.tm_isdst = -1; /* unknown */
if (is_utc) {
t = mktimegm(&dt);
} else {
t = mktime(&dt);
}
Philip Gladstone
committed
Philip Gladstone
committed
t *= 1000000;
if (*q == '.') {
Philip Gladstone
committed
q++;
for (val = 0, n = 100000; n >= 1; n /= 10, q++) {
Philip Gladstone
committed
break;
val += n * (*q - '0');
Daniel Kristjansson
committed
/**
* Attempts to find a specific tag in a URL.
*
* syntax: '?tag1=val1&tag2=val2...'. Little URL decoding is done.
* Return 1 if found.
*/
int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info)
{
const char *p;
char tag[128], *q;
p = info;
if (*p == '?')
p++;
for(;;) {
q = tag;
while (*p != '\0' && *p != '=' && *p != '&') {
if ((q - tag) < sizeof(tag) - 1)
*q++ = *p;
p++;
}
*q = '\0';
q = arg;
if (*p == '=') {
p++;
while (*p != '&' && *p != '\0') {
Philip Gladstone
committed
if ((q - arg) < arg_size - 1) {
if (*p == '+')
*q++ = ' ';
else
*q++ = *p;
}
Daniel Kristjansson
committed
/**
* Returns in 'buf' the path with '%d' replaced by number.
*
* Also handles the '%0nd' format where 'n' is the total number
* of digits and '%%'. Return 0 if OK, and -1 if format error.
*/
int get_frame_filename(char *buf, int buf_size,
const char *path, int number)
{
const char *p;
Panagiotis Issaris
committed
char *q, buf1[20], c;
int nd, len, percentd_found;
q = buf;
p = path;
percentd_found = 0;
for(;;) {
c = *p++;
if (c == '\0')
break;
if (c == '%') {
Philip Gladstone
committed
do {
nd = 0;
while (isdigit(*p)) {
nd = nd * 10 + *p++ - '0';
}
c = *p++;
} while (isdigit(c));
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
switch(c) {
case '%':
goto addchar;
case 'd':
if (percentd_found)
goto fail;
percentd_found = 1;
snprintf(buf1, sizeof(buf1), "%0*d", nd, number);
len = strlen(buf1);
if ((q - buf + len) > buf_size - 1)
goto fail;
memcpy(q, buf1, len);
q += len;
break;
default:
goto fail;
}
} else {
addchar:
if ((q - buf) < buf_size - 1)
*q++ = c;
}
}
if (!percentd_found)
goto fail;
*q = '\0';
return 0;
fail:
*q = '\0';
return -1;
}
Fabrice Bellard
committed
/**
* Print nice hexa dump of a buffer
* @param f stream for output
Fabrice Bellard
committed
* @param buf buffer
* @param size buffer size
*/
void av_hex_dump(FILE *f, uint8_t *buf, int size)