]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/img2enc.c
avfilter/vf_psnr: remove unnecessary check
[ffmpeg] / libavformat / img2enc.c
index a976d07accd1d37f3619842830ee6d2b651488e0..7b5133d30075b59f12e133fb3650fee13a232bd1 100644 (file)
@@ -23,6 +23,7 @@
 #include "libavutil/intreadwrite.h"
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
+#include "libavutil/dict.h"
 #include "libavutil/log.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
@@ -44,6 +45,7 @@ typedef struct VideoMuxData {
     int frame_pts;
     const char *muxer;
     int use_rename;
+    AVDictionary *protocol_opts;
 } VideoMuxData;
 
 static int write_header(AVFormatContext *s)
@@ -74,9 +76,9 @@ static int write_header(AVFormatContext *s)
 static int write_muxed_file(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
 {
     VideoMuxData *img = s->priv_data;
-    AVCodecParameters *par = s->streams[0]->codecpar;
+    AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar;
     AVStream *st;
-    AVPacket pkt2 = {0};
+    AVPacket pkt2;
     AVFormatContext *fmt = NULL;
     int ret;
 
@@ -86,24 +88,27 @@ static int write_muxed_file(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
         return ret;
     st = avformat_new_stream(fmt, NULL);
     if (!st) {
-        avformat_free_context(fmt);
-        return AVERROR(ENOMEM);
+        ret = AVERROR(ENOMEM);
+        goto out;
     }
     st->id = pkt->stream_index;
 
     fmt->pb = pb;
-    if ((ret = av_packet_ref(&pkt2, pkt))                      < 0 ||
-        (ret = avcodec_parameters_copy(st->codecpar, par))     < 0 ||
+
+    ret = av_packet_ref(&pkt2, pkt);
+    if (ret < 0)
+        goto out;
+    pkt2.stream_index = 0;
+
+    if ((ret = avcodec_parameters_copy(st->codecpar, par))     < 0 ||
         (ret = avformat_write_header(fmt, NULL))               < 0 ||
         (ret = av_interleaved_write_frame(fmt, &pkt2))         < 0 ||
-        (ret = av_write_trailer(fmt))                          < 0) {
-        av_packet_unref(&pkt2);
-        avformat_free_context(fmt);
-        return ret;
-    }
+        (ret = av_write_trailer(fmt))) {}
+
     av_packet_unref(&pkt2);
+out:
     avformat_free_context(fmt);
-    return 0;
+    return ret;
 }
 
 static int write_packet_pipe(AVFormatContext *s, AVPacket *pkt)
@@ -115,7 +120,6 @@ static int write_packet_pipe(AVFormatContext *s, AVPacket *pkt)
             return ret;
     } else {
         avio_write(s->pb, pkt->data, pkt->size);
-        avio_flush(s->pb);
     }
     img->img_number++;
     return 0;
@@ -124,12 +128,13 @@ static int write_packet_pipe(AVFormatContext *s, AVPacket *pkt)
 static int write_packet(AVFormatContext *s, AVPacket *pkt)
 {
     VideoMuxData *img = s->priv_data;
-    AVIOContext *pb[4];
+    AVIOContext *pb[4] = {0};
     char filename[1024];
     AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar;
     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(par->format);
     int ret, i;
     int nb_renames = 0;
+    AVDictionary *options = NULL;
 
     if (img->update) {
         av_strlcpy(filename, img->path, sizeof(filename));
@@ -158,11 +163,18 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt)
         return AVERROR(EINVAL);
     }
     for (i = 0; i < 4; i++) {
+        av_dict_copy(&options, img->protocol_opts, 0);
         snprintf(img->tmp[i], sizeof(img->tmp[i]), "%s.tmp", filename);
         av_strlcpy(img->target[i], filename, sizeof(img->target[i]));
-        if (s->io_open(s, &pb[i], img->use_rename ? img->tmp[i] : filename, AVIO_FLAG_WRITE, NULL) < 0) {
+        if (s->io_open(s, &pb[i], img->use_rename ? img->tmp[i] : filename, AVIO_FLAG_WRITE, &options) < 0) {
             av_log(s, AV_LOG_ERROR, "Could not open file : %s\n", img->use_rename ? img->tmp[i] : filename);
-            return AVERROR(EIO);
+            ret = AVERROR(EIO);
+            goto fail;
+        }
+        if (options) {
+            av_log(s, AV_LOG_ERROR, "Could not recognize some protocol options\n");
+            ret = AVERROR(EINVAL);
+            goto fail;
         }
 
         if (!img->split_planes || i+1 >= desc->nb_components)
@@ -191,7 +203,7 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt)
     } else if (img->muxer) {
         ret = write_muxed_file(s, pb[0], pkt);
         if (ret < 0)
-            return ret;
+            goto fail;
     } else {
         avio_write(pb[0], pkt->data, pkt->size);
     }
@@ -205,6 +217,13 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt)
 
     img->img_number++;
     return 0;
+
+fail:
+    av_dict_free(&options);
+    for (i = 0; i < FF_ARRAY_ELEMS(pb); i++)
+        if (pb[i])
+            ff_format_io_close(s, &pb[i]);
+    return ret;
 }
 
 static int query_codec(enum AVCodecID id, int std_compliance)
@@ -226,6 +245,7 @@ static const AVOption muxoptions[] = {
     { "strftime",     "use strftime for filename", OFFSET(use_strftime),  AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, ENC },
     { "frame_pts",    "use current frame pts for filename", OFFSET(frame_pts),  AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, ENC },
     { "atomic_writing", "write files atomically (using temporary files and renames)", OFFSET(use_rename), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, ENC },
+    { "protocol_opts", "specify protocol options for the opened files", OFFSET(protocol_opts), AV_OPT_TYPE_DICT, {0}, 0, 0, ENC },
     { NULL },
 };
 
@@ -237,10 +257,10 @@ static const AVClass img2mux_class = {
     .version    = LIBAVUTIL_VERSION_INT,
 };
 
-AVOutputFormat ff_image2_muxer = {
+const AVOutputFormat ff_image2_muxer = {
     .name           = "image2",
     .long_name      = NULL_IF_CONFIG_SMALL("image2 sequence"),
-    .extensions     = "bmp,dpx,jls,jpeg,jpg,ljpg,pam,pbm,pcx,pgm,pgmyuv,png,"
+    .extensions     = "bmp,dpx,exr,jls,jpeg,jpg,ljpg,pam,pbm,pcx,pfm,pgm,pgmyuv,png,"
                       "ppm,sgi,tga,tif,tiff,jp2,j2c,j2k,xwd,sun,ras,rs,im1,im8,im24,"
                       "sunras,xbm,xface,pix,y",
     .priv_data_size = sizeof(VideoMuxData),
@@ -253,7 +273,7 @@ AVOutputFormat ff_image2_muxer = {
 };
 #endif
 #if CONFIG_IMAGE2PIPE_MUXER
-AVOutputFormat ff_image2pipe_muxer = {
+const AVOutputFormat ff_image2pipe_muxer = {
     .name           = "image2pipe",
     .long_name      = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
     .priv_data_size = sizeof(VideoMuxData),