Newer
Older
Fabrice Bellard
committed
ic->bit_rate / 1000);
}
#endif
}
Fabrice Bellard
committed
static int has_codec_parameters(AVCodecContext *enc)
{
int val;
switch(enc->codec_type) {
case AVMEDIA_TYPE_AUDIO:
Baptiste Coudurier
committed
val = enc->sample_rate && enc->channels && enc->sample_fmt != SAMPLE_FMT_NONE;
if(!enc->frame_size &&
(enc->codec_id == CODEC_ID_VORBIS ||
enc->codec_id == CODEC_ID_AAC ||
enc->codec_id == CODEC_ID_MP1 ||
enc->codec_id == CODEC_ID_MP2 ||
Baptiste Coudurier
committed
enc->codec_id == CODEC_ID_MP3 ||
enc->codec_id == CODEC_ID_SPEEX))
return 0;
Fabrice Bellard
committed
break;
case AVMEDIA_TYPE_VIDEO:
val = enc->width && enc->pix_fmt != PIX_FMT_NONE;
Fabrice Bellard
committed
break;
default:
val = 1;
break;
}
return enc->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 ||
st->codec_info_nb_frames >= 4 + st->codec->has_b_frames;
}
static int try_decode_frame(AVStream *st, AVPacket *avpkt)
{
int16_t *samples;
AVCodec *codec;
if(!st->codec->codec){
codec = avcodec_find_decoder(st->codec->codec_id);
if (!codec)
return -1;
ret = avcodec_open(st->codec, codec);
if (ret < 0)
return ret;
}
Baptiste Coudurier
committed
if(!has_codec_parameters(st->codec) || !has_decode_delay_been_guessed(st)){
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)
{
unsigned int i;
for (i=0; i+1<s->nb_chapters; i++)
if (s->chapters[i]->end == AV_NOPTS_VALUE) {
assert(s->chapters[i]->start <= s->chapters[i+1]->start);
assert(!av_cmp_q(s->chapters[i]->time_base, s->chapters[i+1]->time_base));
s->chapters[i]->end = s->chapters[i+1]->start;
}
if (s->nb_chapters && s->chapters[i]->end == AV_NOPTS_VALUE) {
assert(s->start_time != AV_NOPTS_VALUE);
assert(s->duration > 0);
s->chapters[i]->end = av_rescale_q(s->start_time + s->duration,
AV_TIME_BASE_Q,
s->chapters[i]->time_base);
}
}
#define MAX_STD_TIMEBASES (60*12+5)
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)
{
Fabrice Bellard
committed
AVStream *st;
AVPacket pkt1, *pkt;
int64_t old_offset = url_ftell(ic->pb);
unsigned int nb_streams = ic->nb_streams;
struct {
int64_t last_dts;
int64_t duration_gcd;
int duration_count;
double duration_error[MAX_STD_TIMEBASES];
int64_t codec_info_duration;
} *info, *tmp_info;
if (ic->nb_streams >= INT_MAX/sizeof(*info) ||
!(info = av_mallocz(ic->nb_streams * sizeof(*info))))
return AVERROR(ENOMEM);
AVCodec *codec;
Alex Converse
committed
if (st->codec->codec_id == CODEC_ID_AAC) {
st->codec->sample_rate = 0;
st->codec->frame_size = 0;
st->codec->channels = 0;
}
if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO){
/* 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);
/* Force decoding of at least one frame of codec data
* this makes sure the codec initializes the channel configuration
* and does not trust the values from the container.
*/
if (codec && codec->capabilities & CODEC_CAP_CHANNEL_CONF)
st->codec->channels = 0;
Michael Niedermayer
committed
//try to just open decoders, in case this is enough to get parameters
if(!has_codec_parameters(st->codec)){
if (codec)
avcodec_open(st->codec, codec);
}
for (i=0; i<ic->nb_streams; i++) {
info[i].last_dts= AV_NOPTS_VALUE;
Fabrice Bellard
committed
count = 0;
read_size = 0;
for(;;) {
if(url_interrupt_cb()){
ret= AVERROR(EINTR);
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++) {
st = ic->streams[i];
Michael Niedermayer
committed
if (!has_codec_parameters(st->codec))
Fabrice Bellard
committed
break;
/* variable fps and no guess at the real fps */
if( tb_unreliable(st->codec) && !(st->r_frame_rate.num && st->avg_frame_rate.num)
&& info[i].duration_count<20 && 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 = av_read_frame_internal(ic, &pkt1);
if (ret < 0 && ret != AVERROR(EAGAIN)) {
/* 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;
}
if (ic->nb_streams > nb_streams) {
if (ic->nb_streams >= INT_MAX/sizeof(*info) ||
!(tmp_info = av_realloc(info, ic->nb_streams*sizeof(*info)))) {
av_free(info);
return AVERROR(ENOMEM);
}
info = tmp_info;
memset(info + nb_streams, 0, (ic->nb_streams - nb_streams) * sizeof(*info));
nb_streams = ic->nb_streams;
}
if (ret == AVERROR(EAGAIN))
continue;
pkt= add_to_pktbuf(&ic->packet_buffer, &pkt1, &ic->packet_buffer_end);
av_free(info);
Michael Niedermayer
committed
return AVERROR(ENOMEM);
Fabrice Bellard
committed
read_size += pkt->size;
Fabrice Bellard
committed
st = ic->streams[pkt->stream_index];
if(st->codec_info_nb_frames>1) {
if (st->time_base.den > 0 && av_rescale_q(info[st->index].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
}
info[st->index].codec_info_duration += pkt->duration;
int64_t last= info[index].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);
if(info[index].duration_count < 2)
memset(info[index].duration_error, 0, sizeof(info[index].duration_error));
for(i=1; i<MAX_STD_TIMEBASES; i++){
int framerate= get_std_framerate(i);
int ticks= lrintf(dur*framerate/(1001*12));
double error= dur - ticks*1001*12/(double)framerate;
info[index].duration_error[i] += error*error;
info[index].duration_count++;
Reimar Döffinger
committed
// ignore the first 4 values, they might have some random jitter
if (info[index].duration_count > 3)
info[index].duration_gcd = av_gcd(info[index].duration_gcd, duration);
if(last == AV_NOPTS_VALUE || info[index].duration_count <= 1)
info[pkt->stream_index].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. */
Baptiste Coudurier
committed
if (!has_codec_parameters(st->codec) || !has_decode_delay_been_guessed(st))
try_decode_frame(st, pkt);
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];
if(st->codec_info_nb_frames>2 && !st->avg_frame_rate.num && info[i].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,
info[i].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.
if (tb_unreliable(st->codec) && info[i].duration_count > 15 && info[i].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 * info[i].duration_gcd, INT_MAX);
if(info[i].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 ...
st->time_base.num*duration_sum[i]/info[i].duration_count*101LL > st->time_base.den*/){
int num = 0;
double best_error= 2*av_q2d(st->time_base);
best_error= best_error*best_error*info[i].duration_count*1000*12*30;
for(j=1; j<MAX_STD_TIMEBASES; j++){
double error= info[i].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);
Fabrice Bellard
committed
}
Fabrice Bellard
committed
av_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
av_free(info);
Fabrice Bellard
committed
return ret;
/*******************************************************/
int av_read_play(AVFormatContext *s)
{
Björn Axelsson
committed
if (s->iformat->read_play)
return s->iformat->read_play(s);
return av_url_read_fpause(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 av_url_read_fpause(s->pb, 1);
Björn Axelsson
committed
return AVERROR(ENOSYS);
void av_close_input_stream(AVFormatContext *s)
int i;
AVStream *st;
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);
Michael Niedermayer
committed
av_free_packet(&st->cur_pkt);
av_metadata_free(&st->metadata);
av_free(st->index_entries);
av_free(st->codec->extradata);
Michael Niedermayer
committed
av_free(st->codec);
Aurelien Jacobs
committed
#if FF_API_OLD_METADATA
av_free(st->priv_data);
Nico Sabbi
committed
for(i=s->nb_programs-1; i>=0; i--) {
Aurelien Jacobs
committed
#if FF_API_OLD_METADATA
Nico Sabbi
committed
av_freep(&s->programs[i]->provider_name);
av_freep(&s->programs[i]->name);
av_metadata_free(&s->programs[i]->metadata);
av_freep(&s->programs[i]->stream_index);
Nico Sabbi
committed
av_freep(&s->programs[i]);
}
flush_packet_queue(s);
Philip Gladstone
committed
av_freep(&s->priv_data);
Michael Niedermayer
committed
while(s->nb_chapters--) {
Aurelien Jacobs
committed
#if FF_API_OLD_METADATA
Michael Niedermayer
committed
av_free(s->chapters[s->nb_chapters]->title);
av_metadata_free(&s->chapters[s->nb_chapters]->metadata);
Michael Niedermayer
committed
av_free(s->chapters[s->nb_chapters]);
}
av_freep(&s->chapters);
av_metadata_free(&s->metadata);
av_freep(&s->key);
void av_close_input_file(AVFormatContext *s)
{
ByteIOContext *pb = s->iformat->flags & AVFMT_NOFILE ? NULL : s->pb;
av_close_input_stream(s);
if (pb)
url_fclose(pb);
}
Fabrice Bellard
committed
AVStream *av_new_stream(AVFormatContext *s, int id)
{
AVStream *st;
int i;
Aurelien Jacobs
committed
#if FF_API_MAX_STREAMS
if (s->nb_streams >= MAX_STREAMS){
av_log(s, AV_LOG_ERROR, "Too many streams\n");
return NULL;
}
#else
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;
#endif
Fabrice Bellard
committed
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;
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
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
AVProgram *av_new_program(AVFormatContext *ac, int id)
{
AVProgram *program=NULL;
int i;
#ifdef DEBUG_SI
av_log(ac, AV_LOG_DEBUG, "new_program: id=0x%04x\n", id);
#endif
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
}
Aurelien Jacobs
committed
#if FF_API_OLD_METADATA
av_metadata_set2(&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 */
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);
} else
s->priv_data = NULL;
if (s->oformat->set_parameters) {
ret = s->oformat->set_parameters(s, ap);
if (ret < 0)
return ret;
}
return 0;
}
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
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;
}
Fabrice Bellard
committed
int av_write_header(AVFormatContext *s)
{
int ret, i;
AVStream *st;
// some sanity checks
if (s->nb_streams == 0) {
av_log(s, AV_LOG_ERROR, "no streams\n");
return AVERROR(EINVAL);
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");
return AVERROR(EINVAL);
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");
return AVERROR(EINVAL);
if((st->codec->width<=0 || st->codec->height<=0) && !(s->oformat->flags & AVFMT_NODIMENSIONS)){
av_log(s, AV_LOG_ERROR, "dimensions not set\n");
return AVERROR(EINVAL);
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");
return AVERROR(EINVAL);
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);
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)
return AVERROR(ENOMEM);
Aurelien Jacobs
committed
#if FF_API_OLD_METADATA
ff_metadata_mux_compat(s);
#endif
/* set muxer identification string */
if (!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
AVMetadata *m;
AVMetadataTag *t;
if (!(m = av_mallocz(sizeof(AVMetadata))))
return AVERROR(ENOMEM);
av_metadata_set2(&m, "encoder", LIBAVFORMAT_IDENT, 0);
metadata_conv(&m, s->oformat->metadata_conv, NULL);
if ((t = av_metadata_get(m, "", NULL, AV_METADATA_IGNORE_SUFFIX)))
av_metadata_set2(&s->metadata, t->key, t->value, 0);
av_metadata_free(&m);
}
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) {
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)
return AVERROR_INVALIDDATA;
av_frac_init(&st->pts, 0, 0, den);
}
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;
Baptiste Coudurier
committed
// av_log(s, AV_LOG_DEBUG, "av_write_frame: 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 -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 * 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,
"st:%d error, non monotone timestamps %"PRId64" >= %"PRId64"\n",
st->index, 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){
Baptiste Coudurier
committed
av_log(s, AV_LOG_ERROR, "st:%d error, pts < dts\n", st->index);
Michael Niedermayer
committed
return -1;
}
Baptiste Coudurier
committed
// av_log(s, AV_LOG_DEBUG, "av_write_frame: pts2:%"PRId64" dts2:%"PRId64"\n", pkt->pts, pkt->dts);
st->cur_dts= pkt->dts;
st->pts.val= pkt->dts;
Michael Niedermayer
committed
switch (st->codec->codec_type) {
case AVMEDIA_TYPE_AUDIO:
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
}
case AVMEDIA_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;
}
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
{
Baptiste Coudurier
committed
int ret = compute_pkt_fields2(s, s->streams[pkt->stream_index], pkt);
Michael Niedermayer
committed
if(ret<0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
Michael Niedermayer
committed
return ret;
ret= s->oformat->write_packet(s, pkt);
if(!ret)
ret= url_ferror(s->pb);
void ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt,
int (*compare)(AVFormatContext *, AVPacket *, AVPacket *))
{
AVPacketList **next_point, *this_pktl;
this_pktl = av_mallocz(sizeof(AVPacketList));
this_pktl->pkt= *pkt;
pkt->destruct= NULL; // do not free original but only the copy
av_dup_packet(&this_pktl->pkt); // duplicate the packet if it uses non-alloced memory
Michael Niedermayer
committed
if(s->streams[pkt->stream_index]->last_in_packet_buffer){
next_point = &(s->streams[pkt->stream_index]->last_in_packet_buffer->next);
}else
next_point = &s->packet_buffer;
Michael Niedermayer
committed
if(*next_point){
if(compare(s, &s->packet_buffer_end->pkt, pkt)){
while(!compare(s, &(*next_point)->pkt, pkt)){
next_point= &(*next_point)->next;
}
Michael Niedermayer
committed
goto next_non_null;
}else{
next_point = &(s->packet_buffer_end->next);
Michael Niedermayer
committed
}
}
s->packet_buffer_end= this_pktl;
Michael Niedermayer
committed
next_non_null:
Michael Niedermayer
committed
this_pktl->next= *next_point;
Michael Niedermayer
committed
s->streams[pkt->stream_index]->last_in_packet_buffer=
*next_point= this_pktl;
}
int ff_interleave_compare_dts(AVFormatContext *s, AVPacket *next, AVPacket *pkt)
{
AVStream *st = s->streams[ pkt ->stream_index];
AVStream *st2= s->streams[ next->stream_index];
Michael Niedermayer
committed
int64_t a= st2->time_base.num * (int64_t)st ->time_base.den;
int64_t b= st ->time_base.num * (int64_t)st2->time_base.den;
return av_rescale_rnd(pkt->dts, b, a, AV_ROUND_DOWN) < next->dts;
int av_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush){
AVPacketList *pktl;
Michael Niedermayer
committed
int stream_count=0;
int i;
Michael Niedermayer
committed
if(pkt){
ff_interleave_add_packet(s, pkt, ff_interleave_compare_dts);
Michael Niedermayer
committed
}
for(i=0; i < s->nb_streams; i++)
Michael Niedermayer
committed
stream_count+= !!s->streams[i]->last_in_packet_buffer;
if(stream_count && (s->nb_streams == stream_count || flush)){
Michael Niedermayer
committed
pktl= s->packet_buffer;
*out= pktl->pkt;
s->packet_buffer= pktl->next;
if(!s->packet_buffer)
s->packet_buffer_end= NULL;
Michael Niedermayer
committed
if(s->streams[out->stream_index]->last_in_packet_buffer == pktl)
s->streams[out->stream_index]->last_in_packet_buffer= NULL;
Michael Niedermayer
committed
av_freep(&pktl);
return 1;
}else{
av_init_packet(out);
return 0;
}
}
/**
* Interleave an AVPacket correctly so it can be muxed.
Michael Niedermayer
committed
* @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
*/
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);
}
int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt){
AVStream *st= s->streams[ pkt->stream_index];
//FIXME/XXX/HACK drop zero sized packets
if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO && pkt->size==0)
Michael Niedermayer
committed
//av_log(NULL, AV_LOG_DEBUG, "av_interleaved_write_frame %d %"PRId64" %"PRId64"\n", pkt->size, pkt->dts, pkt->pts);
Baptiste Coudurier
committed
if(compute_pkt_fields2(s, st, pkt) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))