Newer
Older
static void estimate_timings(AVFormatContext *ic, int64_t old_offset)
Fabrice Bellard
committed
{
int64_t file_size;
/* get the file size, if possible */
if (ic->iformat->flags & AVFMT_NOFILE) {
file_size = 0;
} else {
Fabrice Bellard
committed
if (file_size < 0)
file_size = 0;
}
ic->file_size = file_size;
if ((!strcmp(ic->iformat->name, "mpeg") ||
!strcmp(ic->iformat->name, "mpegts")) &&
file_size && ic->pb->seekable) {
Fabrice Bellard
committed
/* get accurate estimate from the PTSes */
estimate_timings_from_pts(ic, old_offset);
} else if (has_duration(ic)) {
/* at least one component has timings - we use them for all
Fabrice Bellard
committed
the components */
fill_all_stream_timings(ic);
} else {
Michael Niedermayer
committed
av_log(ic, AV_LOG_WARNING, "Estimating duration from bitrate, this may be inaccurate\n");
estimate_timings_from_bit_rate(ic);
Fabrice Bellard
committed
}
update_stream_timings(ic);
Fabrice Bellard
committed
{
int i;
Fabrice Bellard
committed
for(i = 0;i < ic->nb_streams; i++) {
st = ic->streams[i];
Diego Biurrun
committed
av_dlog(ic, "%d: start_time: %0.3f duration: %0.3f\n", i,
(double) st->start_time / AV_TIME_BASE,
(double) st->duration / AV_TIME_BASE);
Fabrice Bellard
committed
}
Diego Biurrun
committed
av_dlog(ic, "stream: start_time: %0.3f duration: %0.3f bitrate=%d kb/s\n",
(double) ic->start_time / AV_TIME_BASE,
(double) ic->duration / AV_TIME_BASE,
ic->bit_rate / 1000);
Fabrice Bellard
committed
}
}
static int has_codec_parameters(AVCodecContext *avctx)
Fabrice Bellard
committed
{
int val;
switch (avctx->codec_type) {
case AVMEDIA_TYPE_AUDIO:
val = avctx->sample_rate && avctx->channels && avctx->sample_fmt != AV_SAMPLE_FMT_NONE;
if (!avctx->frame_size &&
(avctx->codec_id == CODEC_ID_VORBIS ||
avctx->codec_id == CODEC_ID_AAC ||
avctx->codec_id == CODEC_ID_MP1 ||
avctx->codec_id == CODEC_ID_MP2 ||
avctx->codec_id == CODEC_ID_MP3 ||
avctx->codec_id == CODEC_ID_SPEEX))
return 0;
Fabrice Bellard
committed
break;
case AVMEDIA_TYPE_VIDEO:
val = avctx->width && avctx->pix_fmt != PIX_FMT_NONE;
Fabrice Bellard
committed
break;
default:
val = 1;
break;
}
return avctx->codec_id != CODEC_ID_NONE && val != 0;
Fabrice Bellard
committed
}
Baptiste Coudurier
committed
static int has_decode_delay_been_guessed(AVStream *st)
{
return st->codec->codec_id != CODEC_ID_H264 ||
Baptiste Coudurier
committed
st->codec_info_nb_frames >= 6 + st->codec->has_b_frames;
Baptiste Coudurier
committed
}
static int try_decode_frame(AVStream *st, AVPacket *avpkt, AVDictionary **options)
{
int16_t *samples;
AVCodec *codec;
if(!st->codec->codec){
codec = avcodec_find_decoder(st->codec->codec_id);
if (!codec)
return -1;
ret = avcodec_open2(st->codec, codec, options);
if(!has_codec_parameters(st->codec) || !has_decode_delay_been_guessed(st) ||
(!st->codec_info_nb_frames && st->codec->codec->capabilities & CODEC_CAP_CHANNEL_CONF)) {
case AVMEDIA_TYPE_VIDEO:
avcodec_get_frame_defaults(&picture);
ret = avcodec_decode_video2(st->codec, &picture,
&got_picture, avpkt);
break;
case AVMEDIA_TYPE_AUDIO:
data_size = FFMAX(avpkt->size, AVCODEC_MAX_AUDIO_FRAME_SIZE);
samples = av_malloc(data_size);
if (!samples)
goto fail;
ret = avcodec_decode_audio3(st->codec, samples,
&data_size, avpkt);
av_free(samples);
break;
default:
break;
}
}
fail:
return ret;
}
unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum CodecID id)
{
while (tags->id != CODEC_ID_NONE) {
if (tags->id == id)
return tags->tag;
tags++;
}
return 0;
}
enum CodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
int i;
for(i=0; tags[i].id != CODEC_ID_NONE;i++) {
if(tag == tags[i].tag)
return tags[i].id;
}
for(i=0; tags[i].id != CODEC_ID_NONE; i++) {
if (ff_toupper4(tag) == ff_toupper4(tags[i].tag))
return tags[i].id;
}
return CODEC_ID_NONE;
}
unsigned int av_codec_get_tag(const AVCodecTag * const *tags, enum CodecID id)
{
int i;
for(i=0; tags && tags[i]; i++){
int tag= ff_codec_get_tag(tags[i], id);
if(tag) return tag;
}
return 0;
}
enum CodecID av_codec_get_id(const AVCodecTag * const *tags, unsigned int tag)
{
int i;
for(i=0; tags && tags[i]; i++){
enum CodecID id= ff_codec_get_id(tags[i], tag);
if(id!=CODEC_ID_NONE) return id;
}
return CODEC_ID_NONE;
}
Aurelien Jacobs
committed
static void compute_chapters_end(AVFormatContext *s)
{
int64_t max_time = s->duration + ((s->start_time == AV_NOPTS_VALUE) ? 0 : s->start_time);
Aurelien Jacobs
committed
for (i = 0; i < s->nb_chapters; i++)
Aurelien Jacobs
committed
if (s->chapters[i]->end == AV_NOPTS_VALUE) {
AVChapter *ch = s->chapters[i];
int64_t end = max_time ? av_rescale_q(max_time, AV_TIME_BASE_Q, ch->time_base)
: INT64_MAX;
for (j = 0; j < s->nb_chapters; j++) {
AVChapter *ch1 = s->chapters[j];
int64_t next_start = av_rescale_q(ch1->start, ch1->time_base, ch->time_base);
if (j != i && next_start > ch->start && next_start < end)
end = next_start;
}
ch->end = (end == INT64_MAX) ? ch->start : end;
Aurelien Jacobs
committed
}
}
static int get_std_framerate(int i){
if(i<60*12) return i*1001;
else return ((const int[]){24,30,60,12,15})[i-60*12]*1000*12;
/*
* Is the time base unreliable.
* This is a heuristic to balance between quick acceptance of the values in
* the headers vs. some extra checks.
* Old DivX and Xvid often have nonsense timebases like 1fps or 2fps.
* MPEG-2 commonly misuses field repeat flags to store different framerates.
* And there are "variable" fps files this needs to detect as well.
*/
static int tb_unreliable(AVCodecContext *c){
if( c->time_base.den >= 101L*c->time_base.num
|| c->time_base.den < 5L*c->time_base.num
/* || c->codec_tag == AV_RL32("DIVX")
|| c->codec_tag == AV_RL32("XVID")*/
|| c->codec_id == CODEC_ID_MPEG2VIDEO
|| c->codec_id == CODEC_ID_H264
)
return 1;
return 0;
}
Fabrice Bellard
committed
int av_find_stream_info(AVFormatContext *ic)
{
return avformat_find_stream_info(ic, NULL);
}
#endif
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Fabrice Bellard
committed
{
Fabrice Bellard
committed
AVStream *st;
AVPacket pkt1, *pkt;
int64_t old_offset = avio_tell(ic->pb);
int orig_nb_streams = ic->nb_streams; // new streams might appear, no options for those
AVCodec *codec;
if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
/* if(!st->time_base.num)
st->time_base= */
Michael Niedermayer
committed
if(!st->codec->time_base.num)
st->codec->time_base= st->time_base;
Michael Niedermayer
committed
//only for the split stuff
if (!st->parser && !(ic->flags & AVFMT_FLAG_NOPARSE)) {
Michael Niedermayer
committed
st->parser = av_parser_init(st->codec->codec_id);
if(st->need_parsing == AVSTREAM_PARSE_HEADERS && st->parser){
st->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
}
Michael Niedermayer
committed
}
Michael Niedermayer
committed
assert(!st->codec->codec);
codec = avcodec_find_decoder(st->codec->codec_id);
/* Ensure that subtitle_header is properly set. */
if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE
&& codec && !st->codec->codec)
avcodec_open2(st->codec, codec, options ? &options[i] : NULL);
Michael Niedermayer
committed
//try to just open decoders, in case this is enough to get parameters
if(!has_codec_parameters(st->codec)){
if (codec && !st->codec->codec)
avcodec_open2(st->codec, codec, options ? &options[i] : NULL);
Michael Niedermayer
committed
}
for (i=0; i<ic->nb_streams; i++) {
Aurelien Jacobs
committed
ic->streams[i]->info->last_dts = AV_NOPTS_VALUE;
Fabrice Bellard
committed
count = 0;
read_size = 0;
for(;;) {
if(url_interrupt_cb()){
Michael Niedermayer
committed
av_log(ic, AV_LOG_DEBUG, "interrupted\n");
Fabrice Bellard
committed
/* check if one codec still needs to be handled */
for(i=0;i<ic->nb_streams;i++) {
int fps_analyze_framecount = 20;
Fabrice Bellard
committed
st = ic->streams[i];
Michael Niedermayer
committed
if (!has_codec_parameters(st->codec))
Fabrice Bellard
committed
break;
/* if the timebase is coarse (like the usual millisecond precision
of mkv), we need to analyze more frames to reliably arrive at
the correct fps */
if (av_q2d(st->time_base) > 0.0005)
fps_analyze_framecount *= 2;
if (ic->fps_probe_size >= 0)
fps_analyze_framecount = ic->fps_probe_size;
/* variable fps and no guess at the real fps */
if( tb_unreliable(st->codec) && !(st->r_frame_rate.num && st->avg_frame_rate.num)
&& st->info->duration_count < fps_analyze_framecount
&& st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
Michael Niedermayer
committed
if(st->parser && st->parser->parser->split && !st->codec->extradata)
Michael Niedermayer
committed
break;
if(st->first_dts == AV_NOPTS_VALUE)
break;
Fabrice Bellard
committed
}
if (i == ic->nb_streams) {
/* NOTE: if the format has no header, then we need to read
some packets to get most of the streams, so we cannot
stop here */
if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) {
Fabrice Bellard
committed
/* if we found the info for all the codecs, we can stop */
ret = count;
Michael Niedermayer
committed
av_log(ic, AV_LOG_DEBUG, "All info found\n");
Fabrice Bellard
committed
break;
}
/* we did not get all the codec info, but we read too much data */
Baptiste Coudurier
committed
if (read_size >= ic->probesize) {
av_log(ic, AV_LOG_DEBUG, "Probe buffer size limit %d reached\n", ic->probesize);
Fabrice Bellard
committed
/* NOTE: a new stream can be added there if no header in file
(AVFMTCTX_NOHEADER) */
ret = read_frame_internal(ic, &pkt1);
if (ret == AVERROR(EAGAIN))
continue;
if (ret < 0) {
/* EOF or error */
ret = -1; /* we could not have all the codec parameters before EOF */
for(i=0;i<ic->nb_streams;i++) {
st = ic->streams[i];
if (!has_codec_parameters(st->codec)){
char buf[256];
avcodec_string(buf, sizeof(buf), st->codec, 0);
av_log(ic, AV_LOG_WARNING, "Could not find codec parameters (%s)\n", buf);
Måns Rullgård
committed
} else {
ret = 0;
}
break;
}
pkt= add_to_pktbuf(&ic->packet_buffer, &pkt1, &ic->packet_buffer_end);
Aurelien Jacobs
committed
if ((ret = av_dup_packet(pkt)) < 0)
goto find_stream_info_err;
Fabrice Bellard
committed
read_size += pkt->size;
Fabrice Bellard
committed
st = ic->streams[pkt->stream_index];
Aurelien Jacobs
committed
if (st->codec_info_nb_frames>1) {
if (st->time_base.den > 0 && av_rescale_q(st->info->codec_info_duration, st->time_base, AV_TIME_BASE_Q) >= ic->max_analyze_duration) {
av_log(ic, AV_LOG_WARNING, "max_analyze_duration reached\n");
break;
Michael Niedermayer
committed
}
Aurelien Jacobs
committed
st->info->codec_info_duration += pkt->duration;
Aurelien Jacobs
committed
int64_t last = st->info->last_dts;
int64_t duration= pkt->dts - last;
if(pkt->dts != AV_NOPTS_VALUE && last != AV_NOPTS_VALUE && duration>0){
double dur= duration * av_q2d(st->time_base);
// if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
// av_log(NULL, AV_LOG_ERROR, "%f\n", dur);
Aurelien Jacobs
committed
if (st->info->duration_count < 2)
memset(st->info->duration_error, 0, sizeof(st->info->duration_error));
for (i=1; i<FF_ARRAY_ELEMS(st->info->duration_error); i++) {
int framerate= get_std_framerate(i);
int ticks= lrintf(dur*framerate/(1001*12));
double error= dur - ticks*1001*12/(double)framerate;
Aurelien Jacobs
committed
st->info->duration_error[i] += error*error;
Aurelien Jacobs
committed
st->info->duration_count++;
Reimar Döffinger
committed
// ignore the first 4 values, they might have some random jitter
Aurelien Jacobs
committed
if (st->info->duration_count > 3)
st->info->duration_gcd = av_gcd(st->info->duration_gcd, duration);
Aurelien Jacobs
committed
if (last == AV_NOPTS_VALUE || st->info->duration_count <= 1)
st->info->last_dts = pkt->dts;
Michael Niedermayer
committed
if(st->parser && st->parser->parser->split && !st->codec->extradata){
int i= st->parser->parser->split(st->codec, pkt->data, pkt->size);
Michael Niedermayer
committed
if(i){
Michael Niedermayer
committed
st->codec->extradata_size= i;
st->codec->extradata= av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
Michael Niedermayer
committed
memcpy(st->codec->extradata, pkt->data, st->codec->extradata_size);
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 MPEG-4, we need to
decompress for QuickTime.
If CODEC_CAP_CHANNEL_CONF is set this will force decoding of at
least one frame of codec data, this makes sure the codec initializes
the channel configuration and does not only trust the values from the container.
*/
try_decode_frame(st, pkt, (options && i < orig_nb_streams )? &options[i] : NULL);
st->codec_info_nb_frames++;
Fabrice Bellard
committed
count++;
}
Michael Niedermayer
committed
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];
Aurelien Jacobs
committed
if (st->codec_info_nb_frames>2 && !st->avg_frame_rate.num && st->info->codec_info_duration)
av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
(st->codec_info_nb_frames-2)*(int64_t)st->time_base.den,
Aurelien Jacobs
committed
st->info->codec_info_duration*(int64_t)st->time_base.num, 60000);
if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
if(st->codec->codec_id == CODEC_ID_RAWVIDEO && !st->codec->codec_tag && !st->codec->bits_per_coded_sample)
Michael Niedermayer
committed
st->codec->codec_tag= avcodec_pix_fmt_to_codec_tag(st->codec->pix_fmt);
Reimar Döffinger
committed
// the check for tb_unreliable() is not completely correct, since this is not about handling
// a unreliable/inexact time base, but a time base that is finer than necessary, as e.g.
// ipmovie.c produces.
Aurelien Jacobs
committed
if (tb_unreliable(st->codec) && st->info->duration_count > 15 && st->info->duration_gcd > 1 && !st->r_frame_rate.num)
av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, st->time_base.den, st->time_base.num * st->info->duration_gcd, INT_MAX);
if (st->info->duration_count && !st->r_frame_rate.num
&& tb_unreliable(st->codec) /*&&
//FIXME we should not special-case MPEG-2, but this needs testing with non-MPEG-2 ...
Aurelien Jacobs
committed
st->time_base.num*duration_sum[i]/st->info->duration_count*101LL > st->time_base.den*/){
int num = 0;
double best_error= 2*av_q2d(st->time_base);
Aurelien Jacobs
committed
best_error = best_error*best_error*st->info->duration_count*1000*12*30;
Aurelien Jacobs
committed
for (j=1; j<FF_ARRAY_ELEMS(st->info->duration_error); j++) {
double error = st->info->duration_error[j] * get_std_framerate(j);
// if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
// av_log(NULL, AV_LOG_ERROR, "%f %f\n", get_std_framerate(j) / 12.0/1001, error);
if(error < best_error){
best_error= error;
num = get_std_framerate(j);
// do not increase frame rate by more than 1 % in order to match a standard rate.
if (num && (!st->r_frame_rate.num || (double)num/(12*1001) < 1.01 * av_q2d(st->r_frame_rate)))
av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, num, 12*1001, INT_MAX);
if( st->codec->time_base.den * (int64_t)st->time_base.num
<= st->codec->time_base.num * st->codec->ticks_per_frame * (int64_t)st->time_base.den){
st->r_frame_rate.num = st->codec->time_base.den;
st->r_frame_rate.den = st->codec->time_base.num * st->codec->ticks_per_frame;
}else{
st->r_frame_rate.num = st->time_base.den;
st->r_frame_rate.den = st->time_base.num;
}
Michael Niedermayer
committed
}
}else if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
if(!st->codec->bits_per_coded_sample)
st->codec->bits_per_coded_sample= av_get_bits_per_sample(st->codec->codec_id);
// set stream disposition based on audio service type
switch (st->codec->audio_service_type) {
case AV_AUDIO_SERVICE_TYPE_EFFECTS:
st->disposition = AV_DISPOSITION_CLEAN_EFFECTS; break;
case AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED:
st->disposition = AV_DISPOSITION_VISUAL_IMPAIRED; break;
case AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED:
st->disposition = AV_DISPOSITION_HEARING_IMPAIRED; break;
case AV_AUDIO_SERVICE_TYPE_COMMENTARY:
st->disposition = AV_DISPOSITION_COMMENT; break;
case AV_AUDIO_SERVICE_TYPE_KARAOKE:
st->disposition = AV_DISPOSITION_KARAOKE; break;
}
Fabrice Bellard
committed
}
Fabrice Bellard
committed
estimate_timings(ic, old_offset);
Aurelien Jacobs
committed
compute_chapters_end(ic);
/* correct DTS for B-frame streams with no timestamps */
for(i=0;i<ic->nb_streams;i++) {
st = ic->streams[i];
if (st->codec->codec_type == AVMEDIA_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
Aurelien Jacobs
committed
find_stream_info_err:
for (i=0; i < ic->nb_streams; i++)
av_freep(&ic->streams[i]->info);
Fabrice Bellard
committed
return ret;
static AVProgram *find_program_from_stream(AVFormatContext *ic, int s)
{
int i, j;
for (i = 0; i < ic->nb_programs; i++)
for (j = 0; j < ic->programs[i]->nb_stream_indexes; j++)
if (ic->programs[i]->stream_index[j] == s)
return ic->programs[i];
return NULL;
}
int av_find_best_stream(AVFormatContext *ic,
enum AVMediaType type,
int wanted_stream_nb,
int related_stream,
AVCodec **decoder_ret,
int flags)
{
int i, nb_streams = ic->nb_streams;
int ret = AVERROR_STREAM_NOT_FOUND, best_count = -1;
unsigned *program = NULL;
AVCodec *decoder = NULL, *best_decoder = NULL;
if (related_stream >= 0 && wanted_stream_nb < 0) {
AVProgram *p = find_program_from_stream(ic, related_stream);
if (p) {
program = p->stream_index;
nb_streams = p->nb_stream_indexes;
}
}
for (i = 0; i < nb_streams; i++) {
int real_stream_index = program ? program[i] : i;
AVStream *st = ic->streams[real_stream_index];
AVCodecContext *avctx = st->codec;
if (avctx->codec_type != type)
continue;
if (wanted_stream_nb >= 0 && real_stream_index != wanted_stream_nb)
Peter Ross
committed
if (st->disposition & (AV_DISPOSITION_HEARING_IMPAIRED|AV_DISPOSITION_VISUAL_IMPAIRED))
continue;
Marton Balint
committed
decoder = avcodec_find_decoder(st->codec->codec_id);
if (!decoder) {
if (ret < 0)
ret = AVERROR_DECODER_NOT_FOUND;
continue;
}
}
if (best_count >= st->codec_info_nb_frames)
continue;
best_count = st->codec_info_nb_frames;
ret = real_stream_index;
best_decoder = decoder;
if (program && i == nb_streams - 1 && ret < 0) {
program = NULL;
nb_streams = ic->nb_streams;
i = 0; /* no related stream found, try again with everything */
}
}
if (decoder_ret)
*decoder_ret = best_decoder;
return ret;
}
/*******************************************************/
int av_read_play(AVFormatContext *s)
{
Björn Axelsson
committed
if (s->iformat->read_play)
return s->iformat->read_play(s);
return avio_pause(s->pb, 0);
Björn Axelsson
committed
return AVERROR(ENOSYS);
}
int av_read_pause(AVFormatContext *s)
{
Björn Axelsson
committed
if (s->iformat->read_pause)
return s->iformat->read_pause(s);
return avio_pause(s->pb, 1);
Björn Axelsson
committed
return AVERROR(ENOSYS);
void av_close_input_stream(AVFormatContext *s)
flush_packet_queue(s);
Fabrice Bellard
committed
if (s->iformat->read_close)
s->iformat->read_close(s);
avformat_free_context(s);
}
void avformat_free_context(AVFormatContext *s)
{
int i;
AVStream *st;
if (s->iformat && s->iformat->priv_class && s->priv_data)
av_opt_free(s->priv_data);
/* free all data in a stream component */
st = s->streams[i];
if (st->parser) {
av_parser_close(st->parser);
Michael Niedermayer
committed
av_free_packet(&st->cur_pkt);
av_dict_free(&st->metadata);
av_free(st->index_entries);
av_free(st->codec->extradata);
av_free(st->codec->subtitle_header);
Michael Niedermayer
committed
av_free(st->codec);
av_free(st->priv_data);
Aurelien Jacobs
committed
av_free(st->info);
Nico Sabbi
committed
for(i=s->nb_programs-1; i>=0; i--) {
av_dict_free(&s->programs[i]->metadata);
av_freep(&s->programs[i]->stream_index);
Nico Sabbi
committed
av_freep(&s->programs[i]);
}
Philip Gladstone
committed
av_freep(&s->priv_data);
Michael Niedermayer
committed
while(s->nb_chapters--) {
av_dict_free(&s->chapters[s->nb_chapters]->metadata);
Michael Niedermayer
committed
av_free(s->chapters[s->nb_chapters]);
}
av_freep(&s->chapters);
av_dict_free(&s->metadata);
void av_close_input_file(AVFormatContext *s)
{
AVIOContext *pb = (s->iformat->flags & AVFMT_NOFILE) || (s->flags & AVFMT_FLAG_CUSTOM_IO) ?
NULL : s->pb;
av_close_input_stream(s);
if (pb)
Fabrice Bellard
committed
AVStream *av_new_stream(AVFormatContext *s, int id)
{
AVStream *st;
int i;
Fabrice Bellard
committed
if (s->nb_streams >= INT_MAX/sizeof(*streams))
return NULL;
streams = av_realloc(s->streams, (s->nb_streams + 1) * sizeof(*streams));
if (!streams)
return NULL;
s->streams = streams;
Fabrice Bellard
committed
st = av_mallocz(sizeof(AVStream));
if (!st)
return NULL;
Aurelien Jacobs
committed
if (!(st->info = av_mallocz(sizeof(*st->info)))) {
av_free(st);
return NULL;
}
st->codec = avcodec_alloc_context3(NULL);
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;
Michael Niedermayer
committed
/* we set the current DTS to 0 so that formats without any timestamps
but durations get some timestamps, formats with some unknown
timestamps have their first few packets buffered and the
timestamps corrected before they are returned to the user */
st->cur_dts = 0;
st->first_dts = AV_NOPTS_VALUE;
st->probe_packets = MAX_PROBE_PACKETS;
av_set_pts_info(st, 33, 1, 90000);
for(i=0; i<MAX_REORDER_DELAY+1; i++)
st->pts_buffer[i]= AV_NOPTS_VALUE;
st->reference_dts = AV_NOPTS_VALUE;
st->sample_aspect_ratio = (AVRational){0,1};
Fabrice Bellard
committed
s->streams[s->nb_streams++] = st;
return st;
}
Nico Sabbi
committed
AVProgram *av_new_program(AVFormatContext *ac, int id)
{
AVProgram *program=NULL;
int i;
av_dlog(ac, "new_program: id=0x%04x\n", id);
Nico Sabbi
committed
for(i=0; i<ac->nb_programs; i++)
if(ac->programs[i]->id == id)
program = ac->programs[i];
if(!program){
program = av_mallocz(sizeof(AVProgram));
if (!program)
return NULL;
dynarray_add(&ac->programs, &ac->nb_programs, program);
program->discard = AVDISCARD_NONE;
}
program->id = id;
return program;
}
AVChapter *ff_new_chapter(AVFormatContext *s, int id, AVRational time_base, int64_t start, int64_t end, const char *title)
Michael Niedermayer
committed
AVChapter *chapter = NULL;
int i;
Michael Niedermayer
committed
for(i=0; i<s->nb_chapters; i++)
Michael Niedermayer
committed
if(s->chapters[i]->id == id)
chapter = s->chapters[i];
if(!chapter){
chapter= av_mallocz(sizeof(AVChapter));
Michael Niedermayer
committed
return NULL;
Michael Niedermayer
committed
dynarray_add(&s->chapters, &s->nb_chapters, chapter);
Michael Niedermayer
committed
}
av_dict_set(&chapter->metadata, "title", title, 0);
Michael Niedermayer
committed
chapter->id = id;
chapter->time_base= time_base;
Michael Niedermayer
committed
return chapter;
Nico Sabbi
committed
Fabrice Bellard
committed
/************************************************************/
/* output media file */
#if FF_API_FORMAT_PARAMETERS
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(ENOMEM);
if (s->oformat->priv_class) {
*(const AVClass**)s->priv_data= s->oformat->priv_class;
av_opt_set_defaults(s->priv_data);
}
} else
s->priv_data = NULL;
if (s->oformat->set_parameters) {
ret = s->oformat->set_parameters(s, ap);
if (ret < 0)
return ret;
}
return 0;
}
#endif
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
static int validate_codec_tag(AVFormatContext *s, AVStream *st)
{
const AVCodecTag *avctag;
int n;
enum CodecID id = CODEC_ID_NONE;
unsigned int tag = 0;
/**
* Check that tag + id is in the table
* If neither is in the table -> OK
* If tag is in the table with another id -> FAIL
* If id is in the table with another tag -> FAIL unless strict < normal
*/
for (n = 0; s->oformat->codec_tag[n]; n++) {
avctag = s->oformat->codec_tag[n];
while (avctag->id != CODEC_ID_NONE) {
if (ff_toupper4(avctag->tag) == ff_toupper4(st->codec->codec_tag)) {
id = avctag->id;
if (id == st->codec->codec_id)
return 1;
}
if (avctag->id == st->codec->codec_id)
tag = avctag->tag;
avctag++;
}
}
if (id != CODEC_ID_NONE)
return 0;
if (tag && (st->codec->strict_std_compliance >= FF_COMPLIANCE_NORMAL))
return 0;
return 1;
}
#if FF_API_FORMAT_PARAMETERS
Fabrice Bellard
committed
int av_write_header(AVFormatContext *s)
{
return avformat_write_header(s, NULL);
}
#endif
int avformat_write_header(AVFormatContext *s, AVDictionary **options)
{
int ret = 0, i;
AVDictionary *tmp = NULL;
if (options)
av_dict_copy(&tmp, *options, 0);
if ((ret = av_opt_set_dict(s, &tmp)) < 0)
goto fail;
// some sanity checks
if (s->nb_streams == 0 && !(s->oformat->flags & AVFMT_NOSTREAMS)) {
av_log(s, AV_LOG_ERROR, "no streams\n");
ret = AVERROR(EINVAL);
goto fail;
for(i=0;i<s->nb_streams;i++) {
st = s->streams[i];
switch (st->codec->codec_type) {
case AVMEDIA_TYPE_AUDIO:
if(st->codec->sample_rate<=0){
av_log(s, AV_LOG_ERROR, "sample rate not set\n");
ret = AVERROR(EINVAL);
goto fail;
if(!st->codec->block_align)
st->codec->block_align = st->codec->channels *
av_get_bits_per_sample(st->codec->codec_id) >> 3;
case AVMEDIA_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");
ret = AVERROR(EINVAL);
goto fail;
if((st->codec->width<=0 || st->codec->height<=0) && !(s->oformat->flags & AVFMT_NODIMENSIONS)){
av_log(s, AV_LOG_ERROR, "dimensions not set\n");
ret = AVERROR(EINVAL);
goto fail;
if(av_cmp_q(st->sample_aspect_ratio, st->codec->sample_aspect_ratio)){
av_log(s, AV_LOG_ERROR, "Aspect ratio mismatch between encoder and muxer layer\n");
ret = AVERROR(EINVAL);
goto fail;
Michael Niedermayer
committed
if(s->oformat->codec_tag){
if(st->codec->codec_tag && st->codec->codec_id == CODEC_ID_RAWVIDEO && av_codec_get_tag(s->oformat->codec_tag, st->codec->codec_id) == 0 && !validate_codec_tag(s, st)){
//the current rawvideo encoding system ends up setting the wrong codec_tag for avi, we override it here
st->codec->codec_tag= 0;
}
Michael Niedermayer
committed
if(st->codec->codec_tag){
char tagbuf[32];
av_get_codec_tag_string(tagbuf, sizeof(tagbuf), st->codec->codec_tag);
Baptiste Coudurier
committed
"Tag %s/0x%08x incompatible with output codec id '%d'\n",
tagbuf, st->codec->codec_tag, st->codec->codec_id);
ret = AVERROR_INVALIDDATA;
goto fail;
Michael Niedermayer
committed
}else
st->codec->codec_tag= av_codec_get_tag(s->oformat->codec_tag, st->codec->codec_id);
}
if(s->oformat->flags & AVFMT_GLOBALHEADER &&
!(st->codec->flags & CODEC_FLAG_GLOBAL_HEADER))
av_log(s, AV_LOG_WARNING, "Codec for stream %d does not use global headers but container format requires global headers\n", i);
if (!s->priv_data && s->oformat->priv_data_size > 0) {
s->priv_data = av_mallocz(s->oformat->priv_data_size);
if (!s->priv_data) {
ret = AVERROR(ENOMEM);
goto fail;
}
if (s->oformat->priv_class) {
*(const AVClass**)s->priv_data= s->oformat->priv_class;
av_opt_set_defaults(s->priv_data);
if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
goto fail;
}
/* set muxer identification string */
if (s->nb_streams && !(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
av_dict_set(&s->metadata, "encoder", LIBAVFORMAT_IDENT, 0);
}
if(s->oformat->write_header){
ret = s->oformat->write_header(s);
if (ret < 0)
goto fail;
/* 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) {
case AVMEDIA_TYPE_AUDIO:
Wolfram Gloger
committed
den = (int64_t)st->time_base.num * st->codec->sample_rate;
case AVMEDIA_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) {
ret = AVERROR_INVALIDDATA;
goto fail;
}
frac_init(&st->pts, 0, 0, den);
Wolfram Gloger
committed
}
if (options) {
av_dict_free(options);
*options = tmp;
}
fail:
av_dict_free(&tmp);
return ret;
Fabrice Bellard
committed
}
//FIXME merge with compute_pkt_fields
Baptiste Coudurier
committed
static int compute_pkt_fields2(AVFormatContext *s, AVStream *st, AVPacket *pkt){
int delay = FFMAX(st->codec->has_b_frames, !!st->codec->max_b_frames);
int num, den, frame_size, i;
av_dlog(s, "compute_pkt_fields2: pts:%"PRId64" dts:%"PRId64" cur_dts:%"PRId64" b:%d size:%d st:%d\n",
pkt->pts, pkt->dts, st->cur_dts, delay, pkt->size, pkt->stream_index);
/* if(pkt->pts == AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE)
return AVERROR(EINVAL);*/
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 * st->codec->ticks_per_frame, den * (int64_t)st->time_base.num);
Michael Niedermayer
committed
if(pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && delay==0)
pkt->pts= pkt->dts;
//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 && !delay){
pkt->dts=
// pkt->pts= st->cur_dts;
pkt->pts= st->pts.val;
}
Baptiste Coudurier
committed
if(pkt->pts != AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY){
st->pts_buffer[0]= pkt->pts;
for(i=1; i<delay+1 && st->pts_buffer[i] == AV_NOPTS_VALUE; i++)
st->pts_buffer[i]= pkt->pts + (i-delay-1) * pkt->duration;
for(i=0; i<delay && st->pts_buffer[i] > st->pts_buffer[i+1]; i++)
FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i+1]);
pkt->dts= st->pts_buffer[0];
Michael Niedermayer
committed
if(st->cur_dts && st->cur_dts != AV_NOPTS_VALUE && st->cur_dts >= pkt->dts){
Baptiste Coudurier
committed
av_log(s, AV_LOG_ERROR,
"Application provided invalid, non monotonically increasing dts to muxer in stream %d: %"PRId64" >= %"PRId64"\n",
Baptiste Coudurier
committed
st->index, st->cur_dts, pkt->dts);
return AVERROR(EINVAL);
Michael Niedermayer
committed
}
if(pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts < pkt->dts){
av_log(s, AV_LOG_ERROR, "pts < dts in stream %d\n", st->index);
return AVERROR(EINVAL);
Michael Niedermayer
committed
}
Baptiste Coudurier
committed
// av_log(s, AV_LOG_DEBUG, "av_write_frame: pts2:%"PRId64" dts2:%"PRId64"\n", pkt->pts, pkt->dts);