add collect/paste effects, new videoscope graticules, boxblur update fix, theora...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / ffmpeg.C
index 857b7de740197ac8a749e4e18a5f55889d763d67..58d280c00edf60315dd93d383e05196d9ee04a49 100644 (file)
@@ -275,6 +275,7 @@ FFStream::FFStream(FFMPEG *ffmpeg, AVStream *st, int fidx)
        flushed = 0;
        need_packet = 1;
        frame = fframe = 0;
+       probe_frame = 0;
        bsfc = 0;
        stats_fp = 0;
        stats_filename = 0;
@@ -293,6 +294,7 @@ FFStream::~FFStream()
        if( filter_graph ) avfilter_graph_free(&filter_graph);
        if( frame ) av_frame_free(&frame);
        if( fframe ) av_frame_free(&fframe);
+       if( probe_frame ) av_frame_free(&probe_frame);
        delete frm_lock;
        if( stats_fp ) fclose(stats_fp);
        if( stats_in ) av_freep(&stats_in);
@@ -419,7 +421,6 @@ int FFStream::decode_activate()
                        }
                        if( ret >= 0 && hw_type != AV_HWDEVICE_TYPE_NONE ) {
                                ret = decode_hw_format(decoder, hw_type);
-                               if( !ret ) hw_type = AV_HWDEVICE_TYPE_NONE;
                        }
                        if( ret >= 0 ) {
                                avcodec_parameters_to_context(avctx, st->codecpar);
@@ -428,34 +429,34 @@ int FFStream::decode_activate()
                                ret = avcodec_open2(avctx, decoder, &copts);
                        }
                        if( ret >= 0 && hw_type != AV_HWDEVICE_TYPE_NONE ) {
-                               if( need_packet ) {
-                                       need_packet = 0;
-                                       ret = read_packet();
-                               }
-                               if( ret >= 0 ) {
-                                       AVPacket *pkt = (AVPacket*)ipkt;
-                                       ret = avcodec_send_packet(avctx, pkt);
-                                       if( ret < 0 || hw_pix_fmt == AV_PIX_FMT_NONE ) {
-                                               ff_err(ret, "HW device init failed, using SW decode.\nfile:%s\n",
-                                                       ffmpeg->fmt_ctx->url);
-                                               avcodec_close(avctx);
-                                               avcodec_free_context(&avctx);
-                                               av_buffer_unref(&hw_device_ctx);
-                                               hw_device_ctx = 0;
-                                               hw_type = AV_HWDEVICE_TYPE_NONE;
-                                               int flags = AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_ANY;
-                                               int idx = st->index;
-                                               av_seek_frame(fmt_ctx, idx, INT64_MIN, flags);
-                                               need_packet = 1;  flushed = 0;
-                                               seeked = 1;  st_eof(0);
-                                               ret = 0;
-                                               continue;
-                                       }
+                               AVFrame *frame = av_frame_alloc();
+                               if( !frame ) {
+                                       fprintf(stderr, "FFStream::decode_activate: av_frame_alloc failed\n");
+                                       ret = AVERROR(ENOMEM);
                                }
+                               if( ret >= 0 )
+                                       ret = decode(frame);
                        }
-                       if( ret >= 0 ) {
-                               reading = 1;
+                       if( ret < 0 && hw_type != AV_HWDEVICE_TYPE_NONE ) {
+                               ff_err(ret, "HW device init failed, using SW decode.\nfile:%s\n",
+                                       ffmpeg->fmt_ctx->url);
+                               avcodec_close(avctx);
+                               avcodec_free_context(&avctx);
+                               av_buffer_unref(&hw_device_ctx);
+                               hw_device_ctx = 0;
+                               av_frame_free(&frame);
+                               hw_type = AV_HWDEVICE_TYPE_NONE;
+                               int flags = AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_ANY;
+                               int idx = st->index;
+                               av_seek_frame(fmt_ctx, idx, 0, flags);
+                               need_packet = 1;  flushed = 0;
+                               seeked = 1;  st_eof(0);
+                               ret = 0;
+                               continue;
                        }
+                       probe_frame = frame;
+                       if( ret >= 0 )
+                               reading = 1;
                        else
                                eprintf(_("open decoder failed\n"));
                }
@@ -483,6 +484,11 @@ int FFStream::read_packet()
 
 int FFStream::decode(AVFrame *frame)
 {
+       if( probe_frame ) { // hw probe reads first frame
+               av_frame_ref(frame, probe_frame);
+               av_frame_free(&probe_frame);
+               return 1;
+       }
        int ret = 0;
        int retries = MAX_RETRY;
 
@@ -720,7 +726,7 @@ int FFStream::seek(int64_t no, double rate)
        tstmp = av_rescale_q(tstmp, time_base, AV_TIME_BASE_Q);
        idx = -1;
 #endif
-
+       av_frame_free(&probe_frame);
        avcodec_flush_buffers(avctx);
        avformat_flush(fmt_ctx);
 #if 0
@@ -1080,6 +1086,7 @@ int FFVideoStream::decode_hw_format(AVCodec *decoder, AVHWDeviceType type)
                if( !config ) {
                        fprintf(stderr, "Decoder %s does not support device type %s.\n",
                                decoder->name, av_hwdevice_get_type_name(type));
+                       ret = -1;
                        break;
                }
                if( (config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX) != 0 &&
@@ -1096,9 +1103,11 @@ int FFVideoStream::decode_hw_format(AVCodec *decoder, AVHWDeviceType type)
                        avctx->hw_device_ctx = av_buffer_ref(hw_device_ctx);
                        ret = 1;
                }
-               else
+               else {
                        ff_err(ret, "Failed HW device create.\ndev:%s\n",
                                av_hwdevice_get_type_name(type));
+                       ret = -1;
+               }
        }
        return ret;
 }
@@ -2965,7 +2974,20 @@ int FFMPEG::encode_activate()
                                fmt_ctx->url);
                        return -1;
                }
-
+               if( !strcmp(file_format, "image2") ) {
+                       Asset *asset = file_base->asset;
+                       const char *filename = asset->path;
+                       FILE *fp = fopen(filename,"w");
+                       if( !fp ) {
+                               eprintf(_("Cant write image2 header file: %s\n  %m"), filename);
+                               return 1;
+                       }
+                       fprintf(fp, "IMAGE2\n");
+                       fprintf(fp, "# Frame rate: %f\n", asset->frame_rate);
+                       fprintf(fp, "# Width: %d\n", asset->width);
+                       fprintf(fp, "# Height: %d\n", asset->height);
+                       fclose(fp);
+               }
                int prog_id = 1;
                AVProgram *prog = av_new_program(fmt_ctx, prog_id);
                for( int i=0; i< ffvideo.size(); ++i )