fred (fwd render enb displ) play pref. info text tweak
[goodguy/history.git] / cinelerra-5.1 / cinelerra / ffmpeg.C
index 243c4e15cf145693d83b363096b59f982b3dd344..b451c79780127ca1dbffe59f0f2a62dae583cc94 100644 (file)
@@ -395,12 +395,11 @@ int FFStream::decode(AVFrame *frame)
 
 int FFStream::load_filter(AVFrame *frame)
 {
+       av_frame_unref(frame);
        int ret = av_buffersrc_add_frame_flags(buffersrc_ctx,
                        frame, AV_BUFFERSRC_FLAG_KEEP_REF);
-       if( ret < 0 ) {
-               av_frame_unref(frame);
+       if( ret < 0 )
                eprintf(_("av_buffersrc_add_frame_flags failed\n"));
-       }
        return ret;
 }
 
@@ -418,6 +417,7 @@ int FFStream::read_filter(AVFrame *frame)
 
 int FFStream::read_frame(AVFrame *frame)
 {
+       av_frame_unref(frame);
        if( !filter_graph || !buffersrc_ctx || !buffersink_ctx )
                return decode(frame);
        if( !fframe && !(fframe=av_frame_alloc()) ) {
@@ -491,7 +491,6 @@ int FFStream::flush()
 
 int FFStream::seek(int64_t no, double rate)
 {
-       int64_t tstmp = -INT64_MAX+1;
 // default ffmpeg native seek
        int npkts = 1;
        int64_t pos = no, pkt_pos = -1;
@@ -509,11 +508,18 @@ int FFStream::seek(int64_t no, double rate)
                }
        }
        if( pos == curr_pos ) return 0;
-       if( pos > 0 && st->time_base.num > 0 ) {
-               double secs = pos / rate;
-               tstmp = secs * st->time_base.den / st->time_base.num;
-               if( nudge != AV_NOPTS_VALUE ) tstmp += nudge;
-       }
+       double secs = pos < 0 ? 0. : pos / rate;
+       AVRational time_base = st->time_base;
+       int64_t tstmp = time_base.num > 0 ? secs * time_base.den/time_base.num : 0;
+       if( nudge != AV_NOPTS_VALUE ) tstmp += nudge;
+       int idx = st->index;
+#if 0
+// seek all streams using the default timebase.
+//   this is how ffmpeg and ffplay work.  stream seeks are less tested.
+       tstmp = av_rescale_q(tstmp, time_base, AV_TIME_BASE_Q);
+       idx = -1;
+#endif
+
        avcodec_flush_buffers(avctx);
        avformat_flush(fmt_ctx);
 #if 0
@@ -523,9 +529,9 @@ int FFStream::seek(int64_t no, double rate)
                seek = pkt_pos;
                flags = AVSEEK_FLAG_BYTE;
        }
-        int ret = avformat_seek_file(fmt_ctx, st->index, -INT64_MAX, seek, INT64_MAX, flags);
+       int ret = avformat_seek_file(fmt_ctx, st->index, -INT64_MAX, seek, INT64_MAX, flags);
 #else
-       int ret = av_seek_frame(fmt_ctx, st->index, tstmp, AVSEEK_FLAG_ANY);
+       int ret = av_seek_frame(fmt_ctx, idx, tstmp, AVSEEK_FLAG_ANY);
 #endif
        int retry = MAX_RETRY;
        while( ret >= 0 ) {
@@ -557,9 +563,9 @@ int FFStream::seek(int64_t no, double rate)
                }
        }
        if( ret < 0 ) {
-printf("** seek fail %ld, %ld\n", pos, tstmp);
+printf("** seek fail %jd, %jd\n", pos, tstmp);
                seeked = need_packet = 0;
-               st_eof(flushed=1);
+               st_eof(flushed=1);
                return -1;
        }
 //printf("seeked pos = %ld, %ld\n", pos, tstmp);
@@ -791,6 +797,11 @@ int FFAudioStream::encode_frame(AVFrame *frame)
        return FFStream::encode_frame(frame);
 }
 
+int FFAudioStream::write_packet(FFPacket &pkt)
+{
+       return FFStream::write_packet(pkt);
+}
+
 void FFAudioStream::load_markers()
 {
        IndexState *index_state = ffmpeg->file_base->asset->index_state;
@@ -921,6 +932,13 @@ int FFVideoStream::encode_frame(AVFrame *frame)
        return FFStream::encode_frame(frame);
 }
 
+int FFVideoStream::write_packet(FFPacket &pkt)
+{
+       if( !(ffmpeg->fmt_ctx->oformat->flags & AVFMT_VARIABLE_FPS) )
+               pkt->duration = 1;
+       return FFStream::write_packet(pkt);
+}
+
 AVPixelFormat FFVideoConvert::color_model_to_pix_fmt(int color_model)
 {
        switch( color_model ) {