add default:all target to plugin Makefile, fix ffmpeg load_filter buffer refs
[goodguy/history.git] / cinelerra-5.1 / cinelerra / ffmpeg.C
index b451c79780127ca1dbffe59f0f2a62dae583cc94..e031dcdbac7f19e184954f828683cb3eaf386a2a 100644 (file)
@@ -33,6 +33,7 @@
 #define AUDIO_INBUF_SIZE 0x10000
 #define VIDEO_REFILL_THRESH 0
 #define AUDIO_REFILL_THRESH 0x1000
+#define AUDIO_MIN_FRAME_SZ 128
 
 Mutex FFMPEG::fflock("FFMPEG::fflock");
 
@@ -395,9 +396,7 @@ 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);
+       int ret = av_buffersrc_add_frame_flags(buffersrc_ctx, frame, 0);
        if( ret < 0 )
                eprintf(_("av_buffersrc_add_frame_flags failed\n"));
        return ret;
@@ -511,7 +510,13 @@ int FFStream::seek(int64_t no, double rate)
        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;
+       if( !tstmp ) {
+               if( st->nb_index_entries > 0 ) tstmp = st->index_entries[0].timestamp;
+               else if( st->start_time != AV_NOPTS_VALUE ) tstmp = st->start_time;
+               else if( st->first_dts != AV_NOPTS_VALUE ) tstmp = st->first_dts;
+               else tstmp = INT64_MIN+1;
+       }
+       else if( nudge != AV_NOPTS_VALUE ) tstmp += nudge;
        int idx = st->index;
 #if 0
 // seek all streams using the default timebase.
@@ -531,7 +536,9 @@ int FFStream::seek(int64_t no, double rate)
        }
        int ret = avformat_seek_file(fmt_ctx, st->index, -INT64_MAX, seek, INT64_MAX, flags);
 #else
-       int ret = av_seek_frame(fmt_ctx, idx, tstmp, AVSEEK_FLAG_ANY);
+// finds the first index frame below the target time
+       int flags = AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_ANY;
+       int ret = av_seek_frame(fmt_ctx, idx, tstmp, flags);
 #endif
        int retry = MAX_RETRY;
        while( ret >= 0 ) {
@@ -580,6 +587,7 @@ FFAudioStream::FFAudioStream(FFMPEG *ffmpeg, AVStream *strm, int idx, int fidx)
        channel0 = channels = 0;
        sample_rate = 0;
        mbsz = 0;
+       frame_sz = AUDIO_MIN_FRAME_SZ;
        length = 0;
        resample_context = 0;
        swr_ichs = swr_ifmt = swr_irate = 0;
@@ -724,10 +732,10 @@ int FFAudioStream::load(int64_t pos, int len)
        }
        if( mbsz < len ) mbsz = len;
        int64_t end_pos = pos + len;
-       int ret = 0;
-       for( int i=0; ret>=0 && !flushed && curr_pos<end_pos && i<MAX_RETRY; ++i ) {
+       int ret = 0, i = len / frame_sz + MAX_RETRY;
+       while( ret>=0 && !flushed && curr_pos<end_pos && --i>=0 ) {
                ret = read_frame(frame);
-               if( ret > 0 ) {
+               if( ret > 0 && frame->nb_samples > 0 ) {
                        init_swr(frame->channels, frame->format, frame->sample_rate);
                        load_history(&frame->extended_data[0], frame->nb_samples);
                        curr_pos += frame->nb_samples;
@@ -858,7 +866,8 @@ int FFVideoStream::load(VFrame *vframe, int64_t pos)
                fprintf(stderr, "FFVideoStream::load: av_frame_alloc failed\n");
                return -1;
        }
-       for( int i=0; ret>=0 && !flushed && curr_pos<=pos && i<MAX_RETRY; ++i ) {
+       int i = MAX_RETRY + pos - curr_pos;
+       while( ret>=0 && !flushed && curr_pos<=pos && --i>=0 ) {
                ret = read_frame(frame);
                if( ret > 0 ) ++curr_pos;
        }
@@ -2104,7 +2113,8 @@ int FFMPEG::decode_activate()
                                if( st->start_time == AV_NOPTS_VALUE ) continue;
                                int vidx = ffvideo.size();
                                while( --vidx >= 0 && ffvideo[vidx]->fidx != i );
-                               if( vidx >= 0 && ffvideo[vidx]->nudge != AV_NOPTS_VALUE ) continue;
+                               if( vidx < 0 ) continue;
+                               if( ffvideo[vidx]->nudge != AV_NOPTS_VALUE ) continue;
                                if( vstart_time < st->start_time )
                                        vstart_time = st->start_time;
                                break; }
@@ -2112,7 +2122,10 @@ int FFMPEG::decode_activate()
                                if( st->start_time == AV_NOPTS_VALUE ) continue;
                                int aidx = ffaudio.size();
                                while( --aidx >= 0 && ffaudio[aidx]->fidx != i );
-                               if( aidx >= 0 && ffaudio[aidx]->nudge != AV_NOPTS_VALUE ) continue;
+                               if( aidx < 0 ) continue;
+                               if( ffaudio[aidx]->frame_sz < avpar->frame_size )
+                                       ffaudio[aidx]->frame_sz = avpar->frame_size;
+                               if( ffaudio[aidx]->nudge != AV_NOPTS_VALUE ) continue;
                                if( astart_time < st->start_time )
                                        astart_time = st->start_time;
                                break; }