diff --git a/ffmpeg.c b/ffmpeg.c index 289223c5af639b5f103f52052e690b62ba9c70ed..366373d83314a51e19abdc47c487e75376017f04 100644 --- a/ffmpeg.c +++ b/ffmpeg.c @@ -230,9 +230,8 @@ typedef struct AVOutputStream { int frame_number; /* input pts and corresponding output pts for A/V sync */ - double sync_ipts; - double sync_ipts_offset; - int64_t sync_opts; + double sync_ipts; /* dts from the AVPacket of the demuxer in second units */ + int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ //FIXME look at frame_number /* video only */ int video_resample; /* video_resample and video_crop are mutually exclusive */ AVFrame pict_tmp; /* temporary image for resampling */ @@ -447,12 +446,23 @@ static void do_audio_out(AVFormatContext *s, while (fifo_read(&ost->fifo, audio_buf, frame_bytes, &ost->fifo.rptr) == 0) { + AVPacket pkt; + av_init_packet(&pkt); + ret = avcodec_encode_audio(enc, audio_out, audio_out_size, (short *)audio_buf); audio_size += ret; - av_write_frame(s, ost->index, audio_out, ret); + pkt.stream_index= ost->index; + pkt.data= audio_out; + pkt.size= ret; + if(enc->coded_frame) + pkt.pts= enc->coded_frame->pts; + pkt.flags |= PKT_FLAG_KEY; + av_write_frame(s, &pkt); } } else { + AVPacket pkt; + av_init_packet(&pkt); /* output a pcm frame */ /* XXX: change encoding codec API to avoid this ? */ switch(enc->codec->id) { @@ -468,7 +478,13 @@ static void do_audio_out(AVFormatContext *s, ret = avcodec_encode_audio(enc, audio_out, size_out, (short *)buftmp); audio_size += ret; - av_write_frame(s, ost->index, audio_out, ret); + pkt.stream_index= ost->index; + pkt.data= audio_out; + pkt.size= ret; + if(enc->coded_frame) + pkt.pts= enc->coded_frame->pts; + pkt.flags |= PKT_FLAG_KEY; + av_write_frame(s, &pkt); } } @@ -586,58 +602,17 @@ static void do_video_out(AVFormatContext *s, *frame_size = 0; - /* NOTE: the A/V sync is always done by considering the audio is - the master clock. It is suffisant for transcoding or playing, - but not for the general case */ - if(sync_method){ - if (audio_sync) { - /* compute the A-V delay and duplicate/remove frames if needed */ - double adelta, vdelta, av_delay; - - adelta = audio_sync->sync_ipts - ((double)audio_sync->sync_opts * - audio_sync->st->time_base.num / audio_sync->st->time_base.den); - - vdelta = ost->sync_ipts - ((double)ost->sync_opts * - ost->st->time_base.num / ost->st->time_base.den); - - av_delay = adelta - vdelta; - if (av_delay < -AV_DELAY_MAX) - nb_frames = 2; - else if (av_delay > AV_DELAY_MAX) - nb_frames = 0; -// printf("adelta=%f vdelta=%f delay=%f nb=%d (A)\n", adelta, vdelta, av_delay, nb_frames); - } else { + if(sync_method){ double vdelta; - - vdelta = (double)(ost->st->pts.val) * ost->st->time_base.num / ost->st->time_base.den - (ost->sync_ipts - ost->sync_ipts_offset); - if (vdelta < 100 && vdelta > -100 && ost->sync_ipts_offset) { - if (vdelta < -AV_DELAY_MAX) - nb_frames = 2; - else if (vdelta > AV_DELAY_MAX) - nb_frames = 0; - } else { - ost->sync_ipts_offset -= vdelta; - if (!ost->sync_ipts_offset) - ost->sync_ipts_offset = 0.000001; /* one microsecond */ - } -// printf("delay=%f nb=%d (V)\n",vdelta, nb_frames); - } - } - -#if defined(AVSYNC_DEBUG) - { - static char *action[] = { "drop frame", "copy frame", "dup frame" }; - if (audio_sync && verbose >=0) { - fprintf(stderr, "Input APTS %12.6f, output APTS %12.6f, ", - (double) audio_sync->sync_ipts, - (double) audio_sync->st->pts.val * st->time_base.num / st->time_base.den); - fprintf(stderr, "Input VPTS %12.6f, output VPTS %12.6f: %s\n", - (double) ost->sync_ipts, - (double) ost->st->pts.val * st->time_base.num / st->time_base.den, - action[nb_frames]); - } + vdelta = ost->sync_ipts * enc->frame_rate / enc->frame_rate_base - ost->sync_opts; + //FIXME set to 0.5 after we fix some dts/pts bugs like in avidec.c + if (vdelta < -1.1) + nb_frames = 0; + else if (vdelta > 1.1) + nb_frames = 2; +//printf("vdelta:%f, ost->sync_opts:%lld, ost->sync_ipts:%f nb_frames:%d\n", vdelta, ost->sync_opts, ost->sync_ipts, nb_frames); } -#endif + ost->sync_opts+= nb_frames; if (nb_frames <= 0) return; @@ -779,14 +754,24 @@ static void do_video_out(AVFormatContext *s, /* duplicates frame if needed */ /* XXX: pb because no interleaving */ for(i=0;iindex; + if (s->oformat->flags & AVFMT_RAWPICTURE) { /* raw pictures are written as AVPicture structure to avoid any copies. We support temorarily the older method. */ AVFrame* old_frame = enc->coded_frame; - enc->coded_frame = dec->coded_frame; - av_write_frame(s, ost->index, - (uint8_t *)final_picture, sizeof(AVPicture)); + enc->coded_frame = dec->coded_frame; //FIXME/XXX remove this hack + pkt.data= (uint8_t *)final_picture; + pkt.size= sizeof(AVPicture); + if(dec->coded_frame) + pkt.pts= dec->coded_frame->pts; + if(dec->coded_frame && dec->coded_frame->key_frame) + pkt.flags |= PKT_FLAG_KEY; + + av_write_frame(s, &pkt); enc->coded_frame = old_frame; } else { AVFrame big_picture; @@ -815,14 +800,22 @@ static void do_video_out(AVFormatContext *s, video_buffer, VIDEO_BUFFER_SIZE, &big_picture); //enc->frame_number = enc->real_pict_num; - av_write_frame(s, ost->index, video_buffer, ret); - *frame_size = ret; - //fprintf(stderr,"\nFrame: %3d %3d size: %5d type: %d", - // enc->frame_number-1, enc->real_pict_num, ret, - // enc->pict_type); - /* if two pass, output log */ - if (ost->logfile && enc->stats_out) { - fprintf(ost->logfile, "%s", enc->stats_out); + if(ret){ + pkt.data= video_buffer; + pkt.size= ret; + if(enc->coded_frame) + pkt.pts= enc->coded_frame->pts; + if(enc->coded_frame && enc->coded_frame->key_frame) + pkt.flags |= PKT_FLAG_KEY; + av_write_frame(s, &pkt); + *frame_size = ret; + //fprintf(stderr,"\nFrame: %3d %3d size: %5d type: %d", + // enc->frame_number-1, enc->real_pict_num, ret, + // enc->pict_type); + /* if two pass, output log */ + if (ost->logfile && enc->stats_out) { + fprintf(ost->logfile, "%s", enc->stats_out); + } } } ost->frame_number++; @@ -872,7 +865,7 @@ static void do_video_stats(AVFormatContext *os, AVOutputStream *ost, fprintf(fvstats,"f_size= %6d ", frame_size); /* compute pts value */ - ti1 = (double)ost->st->pts.val * ost->st->time_base.num / ost->st->time_base.den; + ti1 = (double)ost->sync_opts *enc->frame_rate_base / enc->frame_rate; if (ti1 < 0.01) ti1 = 0.01; @@ -1003,8 +996,8 @@ static int output_packet(AVInputStream *ist, int ist_index, short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2]; void *buffer_to_free; - if (pkt && pkt->pts != AV_NOPTS_VALUE) { - ist->next_pts = ist->pts = pkt->pts; + if (pkt && pkt->pts != AV_NOPTS_VALUE) { //FIXME seems redundant, as libavformat does this too + ist->next_pts = ist->pts = pkt->dts; } else { ist->pts = ist->next_pts; } @@ -1122,10 +1115,6 @@ static int output_packet(AVInputStream *ist, int ist_index, #endif /* set the input output pts pairs */ ost->sync_ipts = (double)ist->pts / AV_TIME_BASE; - /* XXX: take into account the various fifos, - in particular for audio */ - ost->sync_opts = ost->st->pts.val; - //printf("ipts=%lld sync_ipts=%f sync_opts=%lld pts.val=%lld pkt->pts=%lld\n", ist->pts, ost->sync_ipts, ost->sync_opts, ost->st->pts.val, pkt->pts); if (ost->encoding_needed) { switch(ost->st->codec.codec_type) { @@ -1157,22 +1146,29 @@ static int output_packet(AVInputStream *ist, int ist_index, av_abort(); } } else { - AVFrame avframe; - + AVFrame avframe; //FIXME/XXX remove this + AVPacket opkt; + av_init_packet(&opkt); + /* no reencoding needed : output the packet directly */ /* force the input stream PTS */ avcodec_get_frame_defaults(&avframe); ost->st->codec.coded_frame= &avframe; avframe.key_frame = pkt->flags & PKT_FLAG_KEY; - ost->st->pts.val= av_rescale(ist->pts, ost->st->time_base.den, ost->st->time_base.num*AV_TIME_BASE); if(ost->st->codec.codec_type == CODEC_TYPE_AUDIO) audio_size += data_size; else if (ost->st->codec.codec_type == CODEC_TYPE_VIDEO) video_size += data_size; - av_write_frame(os, ost->index, data_buf, data_size); + opkt.stream_index= ost->index; + opkt.data= data_buf; + opkt.size= data_size; + opkt.pts= ist->pts; //FIXME dts vs. pts + opkt.flags= pkt->flags; + + av_write_frame(os, &opkt); ost->st->codec.frame_number++; ost->frame_number++; } diff --git a/ffserver.c b/ffserver.c index b81b5c8865ace4ee3ab4714b640d6bd6a6d680a9..7423abc07b40f85c33f2aea064eb92ffde714929 100644 --- a/ffserver.c +++ b/ffserver.c @@ -2163,7 +2163,7 @@ static int http_prepare_data(HTTPContext *c) /* XXX: potential leak */ return -1; } - if (av_write_frame(ctx, pkt.stream_index, pkt.data, pkt.size)) { + if (av_write_frame(ctx, &pkt)) { c->state = HTTPSTATE_SEND_DATA_TRAILER; } diff --git a/libavformat/amr.c b/libavformat/amr.c index 67a7748bdab758474cb7254b6081dcc39e2a09c0..760eb995aee2e6a9517c4c266dd471d6bd2221ea 100644 --- a/libavformat/amr.c +++ b/libavformat/amr.c @@ -51,10 +51,9 @@ static int amr_write_header(AVFormatContext *s) return 0; } -static int amr_write_packet(AVFormatContext *s, int stream_index_ptr, - uint8_t *buf, int size, int force_pts) +static int amr_write_packet(AVFormatContext *s, AVPacket *pkt) { - put_buffer(&s->pb, buf, size); + put_buffer(&s->pb, pkt->data, pkt->size); put_flush_packet(&s->pb); return 0; } diff --git a/libavformat/asf-enc.c b/libavformat/asf-enc.c index 3dcf43e862f5c03e596fff0c6f223ad46035b761..9e879098581954f8cdc1573f5c36d0f33e37a3c8 100644 --- a/libavformat/asf-enc.c +++ b/libavformat/asf-enc.c @@ -310,7 +310,7 @@ static int asf_write_header1(AVFormatContext *s, int64_t file_size, int64_t data put_le64(pb, asf->nb_packets); /* number of packets */ put_le64(pb, asf->duration); /* end time stamp (in 100ns units) */ put_le64(pb, asf->duration); /* duration (in 100ns units) */ - put_le32(pb, 0); /* start time stamp */ + put_le32(pb, preroll_time); /* start time stamp */ put_le32(pb, 0); /* ??? */ put_le32(pb, asf->is_streamed ? 1 : 0); /* ??? */ put_le32(pb, asf->packet_size); /* packet size */ @@ -686,17 +686,17 @@ static void put_frame( stream->seq++; } -static int asf_write_packet(AVFormatContext *s, int stream_index, - const uint8_t *buf, int size, int64_t timestamp) +static int asf_write_packet(AVFormatContext *s, AVPacket *pkt) { ASFContext *asf = s->priv_data; ASFStream *stream; int64_t duration; AVCodecContext *codec; - codec = &s->streams[stream_index]->codec; - stream = &asf->streams[stream_index]; + codec = &s->streams[pkt->stream_index]->codec; + stream = &asf->streams[pkt->stream_index]; + //XXX /FIXME use duration from AVPacket if (codec->codec_type == CODEC_TYPE_AUDIO) { duration = (codec->frame_number * codec->frame_size * int64_t_C(10000000)) / codec->sample_rate; @@ -706,7 +706,7 @@ static int asf_write_packet(AVFormatContext *s, int stream_index, if (duration > asf->duration) asf->duration = duration; - put_frame(s, stream, timestamp, buf, size); + put_frame(s, stream, pkt->pts, pkt->data, pkt->size); return 0; } diff --git a/libavformat/au.c b/libavformat/au.c index c9837f7c76433093e342e1cbd525217570445c58..5515c3257b2e244cae7d313135efac4479784830 100644 --- a/libavformat/au.c +++ b/libavformat/au.c @@ -72,11 +72,10 @@ static int au_write_header(AVFormatContext *s) return 0; } -static int au_write_packet(AVFormatContext *s, int stream_index_ptr, - const uint8_t *buf, int size, int64_t pts) +static int au_write_packet(AVFormatContext *s, AVPacket *pkt) { ByteIOContext *pb = &s->pb; - put_buffer(pb, buf, size); + put_buffer(pb, pkt->data, pkt->size); return 0; } diff --git a/libavformat/audio.c b/libavformat/audio.c index 00f705c372257368e07f05217570772dbb1f9f89..e3e5888e9b6bff5b0d3e8e42ed90171e293c528d 100644 --- a/libavformat/audio.c +++ b/libavformat/audio.c @@ -164,11 +164,12 @@ static int audio_write_header(AVFormatContext *s1) } } -static int audio_write_packet(AVFormatContext *s1, int stream_index, - const uint8_t *buf, int size, int64_t pts) +static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt) { AudioData *s = s1->priv_data; int len, ret; + int size= pkt->size; + uint8_t *buf= pkt->data; while (size > 0) { len = AUDIO_BLOCK_SIZE - s->buffer_ptr; diff --git a/libavformat/avformat.h b/libavformat/avformat.h index 966d2fd0964979b41cf97c978e3d50b4e68b1129..ae1f9fe69ded5987daf040fead1158381bedd05e 100644 --- a/libavformat/avformat.h +++ b/libavformat/avformat.h @@ -5,7 +5,7 @@ extern "C" { #endif -#define LIBAVFORMAT_BUILD 4614 +#define LIBAVFORMAT_BUILD 4615 #define LIBAVFORMAT_VERSION_INT FFMPEG_VERSION_INT #define LIBAVFORMAT_VERSION FFMPEG_VERSION @@ -131,9 +131,7 @@ typedef struct AVOutputFormat { enum CodecID audio_codec; /* default audio codec */ enum CodecID video_codec; /* default video codec */ int (*write_header)(struct AVFormatContext *); - int (*write_packet)(struct AVFormatContext *, - int stream_index, - const uint8_t *buf, int size, int64_t pts); + int (*write_packet)(struct AVFormatContext *, AVPacket *pkt); int (*write_trailer)(struct AVFormatContext *); /* can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER */ int flags; @@ -558,8 +556,8 @@ int av_seek_frame_binary(AVFormatContext *s, int stream_index, int64_t target_ts /* media file output */ int av_set_parameters(AVFormatContext *s, AVFormatParameters *ap); int av_write_header(AVFormatContext *s); -int av_write_frame(AVFormatContext *s, int stream_index, const uint8_t *buf, - int size); +int av_write_frame(AVFormatContext *s, AVPacket *pkt); + int av_write_trailer(AVFormatContext *s); void dump_format(AVFormatContext *ic, diff --git a/libavformat/avienc.c b/libavformat/avienc.c index 7b3857fd3de9ba6d6cab78ada4f89fab118cd184..86f0e834466e57735bb92d2fd7f88878d17c433b 100644 --- a/libavformat/avienc.c +++ b/libavformat/avienc.c @@ -607,14 +607,15 @@ static int avi_write_idx1(AVFormatContext *s) return 0; } -static int avi_write_packet(AVFormatContext *s, int stream_index, - const uint8_t *buf, int size, int64_t pts) +static int avi_write_packet(AVFormatContext *s, AVPacket *pkt) { AVIContext *avi = s->priv_data; ByteIOContext *pb = &s->pb; unsigned char tag[5]; - unsigned int flags; + unsigned int flags=0; AVCodecContext *enc; + const int stream_index= pkt->stream_index; + int size= pkt->size; if (url_ftell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE) { avi_write_ix(s); @@ -629,11 +630,11 @@ static int avi_write_packet(AVFormatContext *s, int stream_index, enc = &s->streams[stream_index]->codec; avi_stream2fourcc(&tag[0], stream_index, enc->codec_type); + if(pkt->flags&PKT_FLAG_KEY) + flags = 0x10; if (enc->codec_type == CODEC_TYPE_AUDIO) { avi->audio_strm_length[stream_index] += size; - flags = 0x10; - } else - flags = enc->coded_frame->key_frame ? 0x10 : 0x00; + } if (!url_is_streamed(&s->pb)) { AVIIndex* idx = &avi->indexes[stream_index]; @@ -657,7 +658,7 @@ static int avi_write_packet(AVFormatContext *s, int stream_index, put_buffer(pb, tag, 4); put_le32(pb, size); - put_buffer(pb, buf, size); + put_buffer(pb, pkt->data, size); if (size & 1) put_byte(pb, 0); diff --git a/libavformat/crc.c b/libavformat/crc.c index 1b8d6a4df3a6945a6fbb6b5ab4de3a3edc612063..ee915debc5e1c8bb8a844abfe0af6f8504079db1 100644 --- a/libavformat/crc.c +++ b/libavformat/crc.c @@ -71,12 +71,10 @@ static int crc_write_header(struct AVFormatContext *s) return 0; } -static int crc_write_packet(struct AVFormatContext *s, - int stream_index, - const uint8_t *buf, int size, int64_t pts) +static int crc_write_packet(struct AVFormatContext *s, AVPacket *pkt) { CRCState *crc = s->priv_data; - crc->crcval = update_adler32(crc->crcval, buf, size); + crc->crcval = update_adler32(crc->crcval, pkt->data, pkt->size); return 0; } diff --git a/libavformat/dv.c b/libavformat/dv.c index ec87068a9fe7d63808c481ba3cc50e1b74b96302..8b029eb0e2cb0fd6b0d7d184625499991077a59d 100644 --- a/libavformat/dv.c +++ b/libavformat/dv.c @@ -888,15 +888,13 @@ static int dv_write_header(AVFormatContext *s) return 0; } -static int dv_write_packet(struct AVFormatContext *s, - int stream_index, - const uint8_t *buf, int size, int64_t pts) +static int dv_write_packet(struct AVFormatContext *s, AVPacket *pkt) { uint8_t* frame; int fsize; - fsize = dv_assemble_frame((DVMuxContext *)s->priv_data, s->streams[stream_index], - buf, size, &frame); + fsize = dv_assemble_frame((DVMuxContext *)s->priv_data, s->streams[pkt->stream_index], + pkt->data, pkt->size, &frame); if (fsize > 0) { put_buffer(&s->pb, frame, fsize); put_flush_packet(&s->pb); diff --git a/libavformat/ffm.c b/libavformat/ffm.c index 651abaa8f264cb1ef71a2b8b8afc93851f7e0fb2..fca46b170f22fabb667a0b7f2ffe1bfa515ce34c 100644 --- a/libavformat/ffm.c +++ b/libavformat/ffm.c @@ -222,15 +222,16 @@ static int ffm_write_header(AVFormatContext *s) return -1; } -static int ffm_write_packet(AVFormatContext *s, int stream_index, - const uint8_t *buf, int size, int64_t force_pts) +static int ffm_write_packet(AVFormatContext *s, AVPacket *pkt) { - AVStream *st = s->streams[stream_index]; + AVStream *st = s->streams[pkt->stream_index]; FFMStream *fst = st->priv_data; int64_t pts; uint8_t header[FRAME_HEADER_SIZE]; int duration; + int size= pkt->size; + //XXX/FIXME use duration from pkt if (st->codec.codec_type == CODEC_TYPE_AUDIO) { duration = ((float)st->codec.frame_size / st->codec.sample_rate * 1000000.0); } else { @@ -239,9 +240,9 @@ static int ffm_write_packet(AVFormatContext *s, int stream_index, pts = fst->pts; /* packet size & key_frame */ - header[0] = stream_index; + header[0] = pkt->stream_index; header[1] = 0; - if (st->codec.coded_frame->key_frame) //if st->codec.coded_frame==NULL then there is a bug somewhere else + if (pkt->flags & PKT_FLAG_KEY) header[1] |= FLAG_KEY_FRAME; header[2] = (size >> 16) & 0xff; header[3] = (size >> 8) & 0xff; @@ -250,7 +251,7 @@ static int ffm_write_packet(AVFormatContext *s, int stream_index, header[6] = (duration >> 8) & 0xff; header[7] = duration & 0xff; ffm_write_data(s, header, FRAME_HEADER_SIZE, pts, 1); - ffm_write_data(s, buf, size, pts, 0); + ffm_write_data(s, pkt->data, size, pts, 0); fst->pts += duration; return 0; @@ -467,6 +468,9 @@ static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap) fst = av_mallocz(sizeof(FFMStream)); if (!fst) goto fail; + + av_set_pts_info(st, 64, 1, 1000000); + st->priv_data = fst; codec = &st->codec; diff --git a/libavformat/flvenc.c b/libavformat/flvenc.c index a44ebd966d3db66c0e53e1d121f365274029f395..aa1308ce2d934a1a0b8c967e70dcbdd490f663e0 100644 --- a/libavformat/flvenc.c +++ b/libavformat/flvenc.c @@ -239,18 +239,19 @@ static int flv_write_trailer(AVFormatContext *s) return 0; } -static int flv_write_packet(AVFormatContext *s, int stream_index, - const uint8_t *buf, int size, int64_t timestamp) +static int flv_write_packet(AVFormatContext *s, AVPacket *pkt) { ByteIOContext *pb = &s->pb; - AVCodecContext *enc = &s->streams[stream_index]->codec; + AVCodecContext *enc = &s->streams[pkt->stream_index]->codec; FLVContext *flv = s->priv_data; FLVFrame *frame = av_malloc(sizeof(FLVFrame)); + int size= pkt->size; + uint8_t *buf= pkt->data; frame->next = 0; frame->size = size; frame->data = av_malloc(size); - frame->timestamp = timestamp; + frame->timestamp = pkt->pts; frame->reserved= flv->reserved; memcpy(frame->data,buf,size); @@ -259,7 +260,7 @@ static int flv_write_packet(AVFormatContext *s, int stream_index, if (enc->codec_type == CODEC_TYPE_VIDEO) { frame->type = 9; frame->flags = 2; // choose h263 - frame->flags |= enc->coded_frame->key_frame ? 0x10 : 0x20; // add keyframe indicator + frame->flags |= pkt->flags & PKT_FLAG_KEY ? 0x10 : 0x20; // add keyframe indicator //frame->timestamp = ( ( flv->frameCount * (int64_t)FRAME_RATE_BASE * (int64_t)1000 ) / (int64_t)enc->frame_rate ); //printf("%08x %f %f\n",frame->timestamp,(double)enc->frame_rate/(double)FRAME_RATE_BASE,1000*(double)FRAME_RATE_BASE/(double)enc->frame_rate); flv->hasVideo = 1; @@ -306,7 +307,7 @@ static int flv_write_packet(AVFormatContext *s, int stream_index, assert(size); if ( flv->initDelay == -1 ) { - flv->initDelay = timestamp; + flv->initDelay = pkt->pts; } frame->type = 8; diff --git a/libavformat/gif.c b/libavformat/gif.c index 7867913da231a65509960c46bb747f4fdd769f5e..3a5f041e615188505fd2ce6f35bb2dcb5ef222c1 100644 --- a/libavformat/gif.c +++ b/libavformat/gif.c @@ -364,14 +364,13 @@ static int gif_write_video(AVFormatContext *s, return 0; } -static int gif_write_packet(AVFormatContext *s, int stream_index, - const uint8_t *buf, int size, int64_t pts) +static int gif_write_packet(AVFormatContext *s, AVPacket *pkt) { - AVCodecContext *codec = &s->streams[stream_index]->codec; + AVCodecContext *codec = &s->streams[pkt->stream_index]->codec; if (codec->codec_type == CODEC_TYPE_AUDIO) return 0; /* just ignore audio */ else - return gif_write_video(s, codec, buf, size); + return gif_write_video(s, codec, pkt->data, pkt->size); } static int gif_write_trailer(AVFormatContext *s) diff --git a/libavformat/img.c b/libavformat/img.c index 9c47d386266e20efb88d05c42e0fc2d2efe9fea0..468cb6e2f0859df7ef16ab03389e3242e41c94df 100644 --- a/libavformat/img.c +++ b/libavformat/img.c @@ -300,11 +300,10 @@ static int img_write_header(AVFormatContext *s) return 0; } -static int img_write_packet(AVFormatContext *s, int stream_index, - const uint8_t *buf, int size, int64_t pts) +static int img_write_packet(AVFormatContext *s, AVPacket *pkt) { VideoData *img = s->priv_data; - AVStream *st = s->streams[stream_index]; + AVStream *st = s->streams[pkt->stream_index]; ByteIOContext pb1, *pb; AVPicture *picture; int width, height, ret; @@ -314,7 +313,7 @@ static int img_write_packet(AVFormatContext *s, int stream_index, width = st->codec.width; height = st->codec.height; - picture = (AVPicture *)buf; + picture = (AVPicture *)pkt->data; if (!img->is_pipe) { if (get_frame_filename(filename, sizeof(filename), diff --git a/libavformat/movenc.c b/libavformat/movenc.c index 0e3fb6a9de92ceafc0569dbcb6fd7533703e4b0c..0315d18b6933b05177fe228ccb0de43aa0dc0f7f 100644 --- a/libavformat/movenc.c +++ b/libavformat/movenc.c @@ -950,15 +950,15 @@ static int mov_write_header(AVFormatContext *s) return 0; } -static int mov_write_packet(AVFormatContext *s, int stream_index, - const uint8_t *buf, int size, int64_t pts) +static int mov_write_packet(AVFormatContext *s, AVPacket *pkt) { MOVContext *mov = s->priv_data; ByteIOContext *pb = &s->pb; - AVCodecContext *enc = &s->streams[stream_index]->codec; - MOVTrack* trk = &mov->tracks[stream_index]; + AVCodecContext *enc = &s->streams[pkt->stream_index]->codec; + MOVTrack* trk = &mov->tracks[pkt->stream_index]; int cl, id; unsigned int samplesInChunk = 0; + int size= pkt->size; if (url_is_streamed(&s->pb)) return 0; /* Can't handle that */ if (!size) return 0; /* Discard 0 sized packets */ @@ -974,7 +974,7 @@ static int mov_write_packet(AVFormatContext *s, int stream_index, int len = 0; while (len < size && samplesInChunk < 100) { - len += packed_size[(buf[len] >> 3) & 0x0F]; + len += packed_size[(pkt->data[len] >> 3) & 0x0F]; samplesInChunk++; } } @@ -1021,8 +1021,8 @@ static int mov_write_packet(AVFormatContext *s, int stream_index, trk->cluster[cl][id].size = size; trk->cluster[cl][id].entries = samplesInChunk; if(enc->codec_type == CODEC_TYPE_VIDEO) { - trk->cluster[cl][id].key_frame = enc->coded_frame->key_frame; - if(enc->coded_frame->pict_type == FF_I_TYPE) + trk->cluster[cl][id].key_frame = !!(pkt->flags & PKT_FLAG_KEY); + if(trk->cluster[cl][id].key_frame) trk->hasKeyframes = 1; } trk->enc = enc; @@ -1030,7 +1030,7 @@ static int mov_write_packet(AVFormatContext *s, int stream_index, trk->sampleCount += samplesInChunk; trk->mdat_size += size; - put_buffer(pb, buf, size); + put_buffer(pb, pkt->data, size); put_flush_packet(pb); return 0; diff --git a/libavformat/mp3.c b/libavformat/mp3.c index 0da8a400e9535a29510485e8d4ff042aa9fd2907..bb6deb2f3a39bd83d943aa4a5cb2e3d3430ac96f 100644 --- a/libavformat/mp3.c +++ b/libavformat/mp3.c @@ -324,10 +324,9 @@ static int mp3_write_header(struct AVFormatContext *s) return 0; } -static int mp3_write_packet(struct AVFormatContext *s, int stream_index, - const uint8_t *buf, int size, int64_t pts) +static int mp3_write_packet(struct AVFormatContext *s, AVPacket *pkt) { - put_buffer(&s->pb, buf, size); + put_buffer(&s->pb, pkt->data, pkt->size); put_flush_packet(&s->pb); return 0; } diff --git a/libavformat/mpeg.c b/libavformat/mpeg.c index e0060584c6d9d10094655e8ae5208cdf9f1f9ece..ffe19ea6ec10862887f4f6d89bed0afc41abdf0d 100644 --- a/libavformat/mpeg.c +++ b/libavformat/mpeg.c @@ -912,17 +912,22 @@ static int64_t update_scr(AVFormatContext *ctx,int stream_index,int64_t pts) } -static int mpeg_mux_write_packet(AVFormatContext *ctx, int stream_index, - const uint8_t *buf, int size, - int64_t timestamp) +static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt) { MpegMuxContext *s = ctx->priv_data; + int stream_index= pkt->stream_index; + int size= pkt->size; + uint8_t *buf= pkt->data; AVStream *st = ctx->streams[stream_index]; StreamInfo *stream = st->priv_data; int64_t pts, dts, new_start_pts, new_start_dts; int len, avail_size; - compute_pts_dts(st, &pts, &dts, timestamp); + //XXX/FIXME this is and always was broken +// compute_pts_dts(st, &pts, &dts, pkt->pts); + + pts= pkt->pts; + dts= pkt->dts; if(s->is_svcd) { /* offset pts and dts slightly into the future to be able diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c index 7a20f8525acc4917bdad7a3e52766051831d883f..fbbb83db03a381ed443fee6a1c4c7211f484d8dc 100644 --- a/libavformat/mpegtsenc.c +++ b/libavformat/mpegtsenc.c @@ -549,10 +549,11 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st, put_flush_packet(&s->pb); } -static int mpegts_write_packet(AVFormatContext *s, int stream_index, - const uint8_t *buf, int size, int64_t pts1) +static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt) { - AVStream *st = s->streams[stream_index]; + AVStream *st = s->streams[pkt->stream_index]; + int size= pkt->size; + uint8_t *buf= pkt->data; MpegTSWriteStream *ts_st = st->priv_data; int len; @@ -565,7 +566,7 @@ static int mpegts_write_packet(AVFormatContext *s, int stream_index, size -= len; ts_st->payload_index += len; if (ts_st->payload_pts == AV_NOPTS_VALUE) - ts_st->payload_pts = pts1; + ts_st->payload_pts = pkt->pts; if (ts_st->payload_index >= DEFAULT_PES_PAYLOAD_SIZE) { mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_index, ts_st->payload_pts); diff --git a/libavformat/mpjpeg.c b/libavformat/mpjpeg.c index 434e519d50fa497ca1c99e812f80a19995e25b5a..da61c0726a8c4eaf17bfa4b0691126481c88bcf2 100644 --- a/libavformat/mpjpeg.c +++ b/libavformat/mpjpeg.c @@ -33,14 +33,13 @@ static int mpjpeg_write_header(AVFormatContext *s) return 0; } -static int mpjpeg_write_packet(AVFormatContext *s, int stream_index, - const uint8_t *buf, int size, int64_t pts) +static int mpjpeg_write_packet(AVFormatContext *s, AVPacket *pkt) { uint8_t buf1[256]; snprintf(buf1, sizeof(buf1), "Content-type: image/jpeg\n\n"); put_buffer(&s->pb, buf1, strlen(buf1)); - put_buffer(&s->pb, buf, size); + put_buffer(&s->pb, pkt->data, pkt->size); snprintf(buf1, sizeof(buf1), "\n--%s\n", BOUNDARY_TAG); put_buffer(&s->pb, buf1, strlen(buf1)); @@ -75,10 +74,9 @@ static int single_jpeg_write_header(AVFormatContext *s) return 0; } -static int single_jpeg_write_packet(AVFormatContext *s, int stream_index, - const uint8_t *buf, int size, int64_t pts) +static int single_jpeg_write_packet(AVFormatContext *s, AVPacket *pkt) { - put_buffer(&s->pb, buf, size); + put_buffer(&s->pb, pkt->data, pkt->size); put_flush_packet(&s->pb); return 1; /* no more data can be sent */ } diff --git a/libavformat/nut.c b/libavformat/nut.c index cb327675fb74e9b9e6b20285a9496586cf26b241..22999ec97588c3a7c73949342b1c8f56eebe1cdb 100644 --- a/libavformat/nut.c +++ b/libavformat/nut.c @@ -691,25 +691,22 @@ static int64_t lsb2full(StreamContext *stream, int64_t lsb){ return ((lsb - delta)&mask) + delta; } -static int nut_write_packet(AVFormatContext *s, int stream_index, - const uint8_t *buf, int size, int64_t pts) +static int nut_write_packet(AVFormatContext *s, AVPacket *pkt) { NUTContext *nut = s->priv_data; - StreamContext *stream= &nut->stream[stream_index]; + StreamContext *stream= &nut->stream[pkt->stream_index]; ByteIOContext *bc = &s->pb; int key_frame = 0, full_pts=0; AVCodecContext *enc; int64_t coded_pts; int frame_type, best_length, frame_code, flags, i, size_mul, size_lsb, time_delta; const int64_t frame_start= url_ftell(bc); + int64_t pts= pkt->pts; + int size= pkt->size; + int stream_index= pkt->stream_index; - if (stream_index > s->nb_streams) - return 1; - enc = &s->streams[stream_index]->codec; - key_frame = enc->coded_frame->key_frame; - if(enc->coded_frame->pts != AV_NOPTS_VALUE) - pts= av_rescale(enc->coded_frame->pts, stream->rate_num, stream->rate_den*(int64_t)AV_TIME_BASE); //FIXME XXX HACK + key_frame = !!(pkt->flags & PKT_FLAG_KEY); frame_type=0; if(frame_start + size + 20 - FFMAX(nut->packet_start[1], nut->packet_start[2]) > MAX_DISTANCE) @@ -808,7 +805,7 @@ static int nut_write_packet(AVFormatContext *s, int stream_index, assert(frame_type > 1); } - put_buffer(bc, buf, size); + put_buffer(bc, pkt->data, size); update(nut, stream_index, frame_start, frame_type, frame_code, key_frame, size, pts); diff --git a/libavformat/ogg.c b/libavformat/ogg.c index 82354c6cdd77f50c5a83b6cc202c37450db04159..3d90868a0263b353448b0b7389e36b8ab072936d 100644 --- a/libavformat/ogg.c +++ b/libavformat/ogg.c @@ -62,16 +62,15 @@ static int ogg_write_header(AVFormatContext *avfcontext) return 0 ; } -static int ogg_write_packet(AVFormatContext *avfcontext, - int stream_index, - const uint8_t *buf, int size, int64_t pts) +static int ogg_write_packet(AVFormatContext *avfcontext, AVPacket *pkt) { OggContext *context = avfcontext->priv_data ; - AVCodecContext *avctx= &avfcontext->streams[stream_index]->codec; + AVCodecContext *avctx= &avfcontext->streams[pkt->stream_index]->codec; ogg_packet *op= &context->op; ogg_page og ; + int64_t pts; - pts= av_rescale(pts, avctx->sample_rate, AV_TIME_BASE); + pts= av_rescale(pkt->pts, avctx->sample_rate, AV_TIME_BASE); // av_log(avfcontext, AV_LOG_DEBUG, "M%d\n", size); @@ -86,8 +85,8 @@ static int ogg_write_packet(AVFormatContext *avfcontext, context->header_handled = 1 ; } - op->packet = (uint8_t*) buf; - op->bytes = size; + op->packet = (uint8_t*) pkt->data; + op->bytes = pkt->size; op->b_o_s = op->packetno == 0; op->granulepos= pts; diff --git a/libavformat/raw.c b/libavformat/raw.c index 1c65f3aaadf90e4fad94939fa6b287214f531e52..fbdbb7aa1c6b437c8e588191f04f26b142e85879 100644 --- a/libavformat/raw.c +++ b/libavformat/raw.c @@ -25,10 +25,9 @@ static int raw_write_header(struct AVFormatContext *s) return 0; } -static int raw_write_packet(struct AVFormatContext *s, int stream_index, - const uint8_t *buf, int size, int64_t pts) +static int raw_write_packet(struct AVFormatContext *s, AVPacket *pkt) { - put_buffer(&s->pb, buf, size); + put_buffer(&s->pb, pkt->data, pkt->size); put_flush_packet(&s->pb); return 0; } @@ -551,9 +550,7 @@ AVOutputFormat rawvideo_oformat = { #endif //CONFIG_ENCODERS #ifdef CONFIG_ENCODERS -static int null_write_packet(struct AVFormatContext *s, - int stream_index, - const uint8_t *buf, int size, int64_t pts) +static int null_write_packet(struct AVFormatContext *s, AVPacket *pkt) { return 0; } diff --git a/libavformat/rm.c b/libavformat/rm.c index 794233754102342538c55f081a0455e25097844a..828921822c6942fcc3d53cfbb32f487d1f8520c6 100644 --- a/libavformat/rm.c +++ b/libavformat/rm.c @@ -389,14 +389,13 @@ static int rm_write_video(AVFormatContext *s, const uint8_t *buf, int size) return 0; } -static int rm_write_packet(AVFormatContext *s, int stream_index, - const uint8_t *buf, int size, int64_t pts) +static int rm_write_packet(AVFormatContext *s, AVPacket *pkt) { - if (s->streams[stream_index]->codec.codec_type == + if (s->streams[pkt->stream_index]->codec.codec_type == CODEC_TYPE_AUDIO) - return rm_write_audio(s, buf, size); + return rm_write_audio(s, pkt->data, pkt->size); else - return rm_write_video(s, buf, size); + return rm_write_video(s, pkt->data, pkt->size); } static int rm_write_trailer(AVFormatContext *s) diff --git a/libavformat/rtp.c b/libavformat/rtp.c index 5d4884d8f7daa0613fcdb8b09816baf7217e089d..31e4a21dc1932de96d88b919b7ef3d3b37bbe8fc 100644 --- a/libavformat/rtp.c +++ b/libavformat/rtp.c @@ -669,16 +669,17 @@ static void rtp_send_mpegts_raw(AVFormatContext *s1, } /* write an RTP packet. 'buf1' must contain a single specific frame. */ -static int rtp_write_packet(AVFormatContext *s1, int stream_index, - const uint8_t *buf1, int size, int64_t pts) +static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt) { RTPDemuxContext *s = s1->priv_data; AVStream *st = s1->streams[0]; int rtcp_bytes; int64_t ntp_time; + int size= pkt->size; + uint8_t *buf1= pkt->data; #ifdef DEBUG - printf("%d: write len=%d\n", stream_index, size); + printf("%d: write len=%d\n", pkt->stream_index, size); #endif /* XXX: mpeg pts hardcoded. RTCP send every 0.5 seconds */ @@ -687,7 +688,7 @@ static int rtp_write_packet(AVFormatContext *s1, int stream_index, if (s->first_packet || rtcp_bytes >= 28) { /* compute NTP time */ /* XXX: 90 kHz timestamp hardcoded */ - ntp_time = (pts << 28) / 5625; + ntp_time = (pkt->pts << 28) / 5625; rtcp_send_sr(s1, ntp_time); s->last_octet_count = s->octet_count; s->first_packet = 0; diff --git a/libavformat/swf.c b/libavformat/swf.c index 3f5ddd1678543729888242fcb78ddbd69115751f..798c9b1ee3ec77ce3dd1d5533ea385b914e23f72 100644 --- a/libavformat/swf.c +++ b/libavformat/swf.c @@ -700,14 +700,13 @@ static int swf_write_audio(AVFormatContext *s, return 0; } -static int swf_write_packet(AVFormatContext *s, int stream_index, - const uint8_t *buf, int size, int64_t pts) +static int swf_write_packet(AVFormatContext *s, AVPacket *pkt) { - AVCodecContext *codec = &s->streams[stream_index]->codec; + AVCodecContext *codec = &s->streams[pkt->stream_index]->codec; if (codec->codec_type == CODEC_TYPE_AUDIO) - return swf_write_audio(s, codec, buf, size); + return swf_write_audio(s, codec, pkt->data, pkt->size); else - return swf_write_video(s, codec, buf, size); + return swf_write_video(s, codec, pkt->data, pkt->size); } static int swf_write_trailer(AVFormatContext *s) diff --git a/libavformat/utils.c b/libavformat/utils.c index b60367c090946fc46f5b20d869cd703499cd7c7e..3232f0276fb98dc55919cdc12e94930c8c2d73bf 100644 --- a/libavformat/utils.c +++ b/libavformat/utils.c @@ -569,10 +569,12 @@ static void compute_pkt_fields(AVFormatContext *s, AVStream *st, int num, den, presentation_delayed; /* handle wrapping */ - if(pkt->pts != AV_NOPTS_VALUE) - pkt->pts= lsb2full(pkt->pts, st->cur_dts, st->pts_wrap_bits); - if(pkt->dts != AV_NOPTS_VALUE) - pkt->dts= lsb2full(pkt->dts, st->cur_dts, st->pts_wrap_bits); + if(st->cur_dts != AV_NOPTS_VALUE){ + if(pkt->pts != AV_NOPTS_VALUE) + pkt->pts= lsb2full(pkt->pts, st->cur_dts, st->pts_wrap_bits); + if(pkt->dts != AV_NOPTS_VALUE) + pkt->dts= lsb2full(pkt->dts, st->cur_dts, st->pts_wrap_bits); + } if (pkt->duration == 0) { compute_frame_duration(&num, &den, s, st, pc, pkt); @@ -596,7 +598,13 @@ static void compute_pkt_fields(AVFormatContext *s, AVStream *st, if(pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts > pkt->dts) presentation_delayed = 1; } + + if(st->cur_dts == AV_NOPTS_VALUE){ + if(presentation_delayed) st->cur_dts = -pkt->duration; + else st->cur_dts = 0; + } +// av_log(NULL, AV_LOG_DEBUG, "IN delayed:%d pts:%lld, dts:%lld cur_dts:%lld\n", presentation_delayed, pkt->pts, pkt->dts, st->cur_dts); /* interpolate PTS and DTS if they are not present */ if (presentation_delayed) { /* DTS = decompression time stamp */ @@ -637,6 +645,7 @@ static void compute_pkt_fields(AVFormatContext *s, AVStream *st, } st->cur_dts += pkt->duration; } +// av_log(NULL, AV_LOG_DEBUG, "OUTdelayed:%d pts:%lld, dts:%lld cur_dts:%lld\n", presentation_delayed, pkt->pts, pkt->dts, st->cur_dts); /* update flags */ if (pc) { @@ -1672,6 +1681,30 @@ int av_find_stream_info(AVFormatContext *ic) } av_estimate_timings(ic); +#if 0 + /* correct DTS for b frame streams with no timestamps */ + for(i=0;inb_streams;i++) { + st = ic->streams[i]; + 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 return ret; } @@ -1764,6 +1797,7 @@ AVStream *av_new_stream(AVFormatContext *s, int id) st->id = id; 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); @@ -1836,27 +1870,68 @@ int av_write_header(AVFormatContext *s) * one audio or video frame. * * @param s media file handle - * @param stream_index stream index - * @param buf buffer containing the frame data - * @param size size of buffer + * @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, int stream_index, const uint8_t *buf, - int size) +int av_write_frame(AVFormatContext *s, AVPacket *pkt) { AVStream *st; int64_t pts_mask; int ret, frame_size; + int b_frames; - st = s->streams[stream_index]; - pts_mask = (1LL << st->pts_wrap_bits) - 1; + if(pkt->stream_index<0) + return -1; + st = s->streams[pkt->stream_index]; - /* HACK/FIXME we skip all zero size audio packets so a encoder can pass pts by outputing zero size packets */ - if(st->codec.codec_type==CODEC_TYPE_AUDIO && size==0) - ret = 0; - else - ret = s->oformat->write_packet(s, stream_index, buf, size, - st->pts.val & pts_mask); + b_frames = FFMAX(st->codec.has_b_frames, st->codec.max_b_frames); + + // av_log(s, AV_LOG_DEBUG, "av_write_frame: pts:%lld dts:%lld cur_dts:%lld b:%d size:%d\n", pkt->pts, pkt->dts, st->cur_dts, b_frames, pkt->size); + +/* if(pkt->pts == AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE) + return -1;*/ + + if(pkt->pts != AV_NOPTS_VALUE) + pkt->pts = av_rescale(pkt->pts, st->time_base.den, AV_TIME_BASE * (int64_t)st->time_base.num); + if(pkt->dts != AV_NOPTS_VALUE) + pkt->dts = av_rescale(pkt->dts, st->time_base.den, AV_TIME_BASE * (int64_t)st->time_base.num); + + /* duration field */ + pkt->duration = av_rescale(pkt->duration, st->time_base.den, AV_TIME_BASE * (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; + } + + //calculate dts from pts + 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= -av_rescale(1, + st->codec.frame_rate_base*(int64_t)st->time_base.den, + st->codec.frame_rate *(int64_t)st->time_base.num); + } + 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; + } + +// av_log(s, 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; + + pts_mask = (2LL << (st->pts_wrap_bits-1)) - 1; + pkt->pts &= pts_mask; + pkt->dts &= pts_mask; + + ret = s->oformat->write_packet(s, pkt); if (ret < 0) return ret; @@ -1864,11 +1939,11 @@ int av_write_frame(AVFormatContext *s, int stream_index, const uint8_t *buf, /* update pts */ switch (st->codec.codec_type) { case CODEC_TYPE_AUDIO: - frame_size = get_audio_frame_size(&st->codec, size); + 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 && (size || st->pts.num!=st->pts.den>>1 || st->pts.val)) { + 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); } break; diff --git a/libavformat/wav.c b/libavformat/wav.c index 495ae0f969e71f29cf0c9ad9f8d6cffe41a6be1a..1d18098339213cffadf8338be7cf0549e62fa81f 100644 --- a/libavformat/wav.c +++ b/libavformat/wav.c @@ -203,11 +203,10 @@ static int wav_write_header(AVFormatContext *s) return 0; } -static int wav_write_packet(AVFormatContext *s, int stream_index_ptr, - const uint8_t *buf, int size, int64_t pts) +static int wav_write_packet(AVFormatContext *s, AVPacket *pkt) { ByteIOContext *pb = &s->pb; - put_buffer(pb, buf, size); + put_buffer(pb, pkt->data, pkt->size); return 0; } diff --git a/libavformat/yuv4mpeg.c b/libavformat/yuv4mpeg.c index 1520b3463528c9c56285a61cb456596da1407a1e..1a18ad03ea6fe79ec789cfe03360c8381e4915a8 100644 --- a/libavformat/yuv4mpeg.c +++ b/libavformat/yuv4mpeg.c @@ -58,10 +58,9 @@ static int yuv4_generate_header(AVFormatContext *s, char* buf) return n; } -static int yuv4_write_packet(AVFormatContext *s, int stream_index, - const uint8_t *buf, int size, int64_t pts) +static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt) { - AVStream *st = s->streams[stream_index]; + AVStream *st = s->streams[pkt->stream_index]; ByteIOContext *pb = &s->pb; AVPicture *picture; int* first_pkt = s->priv_data; @@ -71,7 +70,7 @@ static int yuv4_write_packet(AVFormatContext *s, int stream_index, char buf1[20]; uint8_t *ptr, *ptr1, *ptr2; - picture = (AVPicture *)buf; + picture = (AVPicture *)pkt->data; /* for the first packet we have to output the header as well */ if (*first_pkt) { diff --git a/output_example.c b/output_example.c index 3b557b0f5be5612ac7a27918240cfdf656063380..6f3dcb73795850be34df73c17655ba4b8864f67d 100644 --- a/output_example.c +++ b/output_example.c @@ -142,16 +142,22 @@ void write_audio_frame(AVFormatContext *oc, AVStream *st) { int out_size; AVCodecContext *c; - - + AVPacket pkt; + av_init_packet(&pkt); + c = &st->codec; get_audio_frame(samples, audio_input_frame_size, c->channels); - out_size = avcodec_encode_audio(c, audio_outbuf, audio_outbuf_size, samples); + pkt.size= avcodec_encode_audio(c, audio_outbuf, audio_outbuf_size, samples); + + pkt.pts= c->coded_frame->pts; + pkt.flags |= PKT_FLAG_KEY; + pkt.stream_index= st->index; + pkt.data= audio_outbuf; /* write the compressed frame in the media file */ - if (av_write_frame(oc, st->index, audio_outbuf, out_size) != 0) { + if (av_write_frame(oc, &pkt) != 0) { fprintf(stderr, "Error while writing audio frame\n"); exit(1); } @@ -336,16 +342,32 @@ void write_video_frame(AVFormatContext *oc, AVStream *st) if (oc->oformat->flags & AVFMT_RAWPICTURE) { /* raw video case. The API will change slightly in the near futur for that */ - ret = av_write_frame(oc, st->index, - (uint8_t *)picture_ptr, sizeof(AVPicture)); + AVPacket pkt; + av_init_packet(&pkt); + + pkt.flags |= PKT_FLAG_KEY; + pkt.stream_index= st->index; + pkt.data= (uint8_t *)picture_ptr; + pkt.size= sizeof(AVPicture); + + ret = av_write_frame(oc, &pkt); } else { /* encode the image */ out_size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, picture_ptr); /* if zero size, it means the image was buffered */ if (out_size != 0) { + AVPacket pkt; + av_init_packet(&pkt); + + pkt.pts= c->coded_frame->pts; + if(c->coded_frame->key_frame) + pkt.flags |= PKT_FLAG_KEY; + pkt.stream_index= st->index; + pkt.data= video_outbuf; + pkt.size= out_size; + /* write the compressed frame in the media file */ - /* XXX: in case of B frames, the pts is not yet valid */ - ret = av_write_frame(oc, st->index, video_outbuf, out_size); + ret = av_write_frame(oc, &pkt); } else { ret = 0; } diff --git a/tests/ffmpeg.regression.ref b/tests/ffmpeg.regression.ref index f1a000c15578c1c783a2e275fd1ff0d15b569bee..a3fe4f45499445d3d9a9afaf8bc37115df792426 100644 --- a/tests/ffmpeg.regression.ref +++ b/tests/ffmpeg.regression.ref @@ -51,20 +51,20 @@ stddev: 8.05 PSNR:30.00 bytes:7602176 5986168 ./data/a-huffyuv.avi 799d3db687f6cdd7a837ec156efc171f *./data/out.yuv stddev: 0.00 PSNR:99.99 bytes:7602176 -a06eb02738bc67c61392fe2fced2afa9 *./data/a-mpeg4-rc.avi - 816398 ./data/a-mpeg4-rc.avi +a86d267dceb37beca514484ad5951675 *./data/a-mpeg4-rc.avi +816350 ./data/a-mpeg4-rc.avi 11efbbf01c8473ac5eabd775f1c0bec5 *./data/out.yuv stddev: 11.51 PSNR:26.89 bytes:7299072 451bd18acf3c169301c215a63a3b216d *./data/a-mpeg4-adv.avi 592570 ./data/a-mpeg4-adv.avi 9f4fbff593dd0bd7ad437ceee4afc11f *./data/out.yuv stddev: 10.31 PSNR:27.85 bytes:7602176 -3424145af47f83c77bd42f4707c1a722 *./data/a-mpeg4-thread.avi - 752408 ./data/a-mpeg4-thread.avi +b99438023ff9dda75494b4095c42affa *./data/a-mpeg4-thread.avi +752360 ./data/a-mpeg4-thread.avi 51b7bfb3cbf1580eefc61e610451e67e *./data/out.yuv stddev: 13.17 PSNR:25.72 bytes:7299072 -36688568f48f40163c338c6f3435c132 *./data/a-mpeg4-Q.avi -875964 ./data/a-mpeg4-Q.avi +4f1ca5927928b8433bfcb5f231239462 *./data/a-mpeg4-Q.avi +875916 ./data/a-mpeg4-Q.avi dca5adb5fca49a806e71266f1a04c3a5 *./data/out.yuv stddev: 8.15 PSNR:29.89 bytes:7299072 771f65ff24c0d32b37f2663892b2f433 *./data/a-error-mpeg4-adv.avi diff --git a/tests/libav.regression.ref b/tests/libav.regression.ref index 3fd792a870eb2ee40e393392729987c84e533d04..aebbbb706ebc1200de2a235d5a2cdd57813e99ac 100644 --- a/tests/libav.regression.ref +++ b/tests/libav.regression.ref @@ -2,9 +2,9 @@ ffmpeg regression test c23bbe3016afcb726a494858b7bb17e3 *./data/b-libav.avi 337732 ./data/b-libav.avi ./data/b-libav.avi CRC=658838d9 -b166e89a9ec8c707573329d883c1b6f9 *./data/b-libav.asf +a09d8460b207c4a67a26842c70fbb060 *./data/b-libav.asf 339767 ./data/b-libav.asf -./data/b-libav.asf CRC=525fdb22 +./data/b-libav.asf CRC=4b9f25a1 be8eb1b5705c8105e4727258e448cb24 *./data/b-libav.rm 356950 ./data/b-libav.rm e826aa1637ff15144ab484c1efca7fe7 *./data/b-libav.mpg diff --git a/tests/regression.sh b/tests/regression.sh index 8f5d2463fe847a158a8d695f2ee448ecfba72a20..981c2afc86c8129eef7992eb1c7fa46ae7af0495 100755 --- a/tests/regression.sh +++ b/tests/regression.sh @@ -477,7 +477,7 @@ do_ffmpeg_crc $file -i $file # asf file=${outfile}libav.asf do_ffmpeg $file -t 1 -y -qscale 10 -f pgmyuv -i $raw_src -f s16le -i $pcm_src -acodec mp2 $file -do_ffmpeg_crc $file -i $file +do_ffmpeg_crc $file -i $file -r 25 # rm file=${outfile}libav.rm diff --git a/tests/rotozoom.regression.ref b/tests/rotozoom.regression.ref index b693866c5a079b64f0f9313d8cbcaea5b4a63fb0..e55dc051c7dc0eff6679e70eaf530bcd9d576426 100644 --- a/tests/rotozoom.regression.ref +++ b/tests/rotozoom.regression.ref @@ -51,20 +51,20 @@ stddev: 5.44 PSNR:33.40 bytes:7602176 4987020 ./data/a-huffyuv.avi dde5895817ad9d219f79a52d0bdfb001 *./data/out.yuv stddev: 0.00 PSNR:99.99 bytes:7602176 -ca8f249763ca162cbee43cb32b7b165b *./data/a-mpeg4-rc.avi - 223932 ./data/a-mpeg4-rc.avi +55b7400e3abf9b24d4a0f5d2963ffa29 *./data/a-mpeg4-rc.avi +223884 ./data/a-mpeg4-rc.avi 5ad73d80df55d60384a43b8260174912 *./data/out.yuv stddev: 5.28 PSNR:33.66 bytes:7299072 8249cc83e341cd18e45adf589e1d254e *./data/a-mpeg4-adv.avi 180988 ./data/a-mpeg4-adv.avi a46b891f446aef2c025dc2847dc86ed6 *./data/out.yuv stddev: 4.95 PSNR:34.22 bytes:7602176 -f2f6e3841d4a5def6d70297b09a6df6c *./data/a-mpeg4-thread.avi - 249670 ./data/a-mpeg4-thread.avi +43411d9f33c8112a25d805f6edf88c17 *./data/a-mpeg4-thread.avi +249622 ./data/a-mpeg4-thread.avi e01d0d5a98d2624b107e13c6f50c533c *./data/out.yuv stddev: 5.09 PSNR:33.97 bytes:7299072 -96333905f57a579396c5121d6651fc7e *./data/a-mpeg4-Q.avi -159744 ./data/a-mpeg4-Q.avi +bfd6fcccc5aa8c3fa5222a33a0bb3b63 *./data/a-mpeg4-Q.avi +159696 ./data/a-mpeg4-Q.avi f44b074a83b177950055ddd6da7e518d *./data/out.yuv stddev: 5.02 PSNR:34.09 bytes:7299072 901ebdbc7438744f0832a9a0cc7678e3 *./data/a-error-mpeg4-adv.avi