From 1b82a0052ce1541a42de74e06890f3fecba96c99 Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <michael@niedermayer.cc>
Date: Tue, 27 Oct 2015 21:21:17 +0100
Subject: [PATCH] avformat/img2enc: Fix img2enc atomic implementation to work
 with split planes

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
 libavformat/img2enc.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/libavformat/img2enc.c b/libavformat/img2enc.c
index 339dfba4a5b..8a8e6e11735 100644
--- a/libavformat/img2enc.c
+++ b/libavformat/img2enc.c
@@ -38,7 +38,8 @@ typedef struct VideoMuxData {
     int is_pipe;
     int split_planes;       /**< use independent file for each Y, U, V plane */
     char path[1024];
-    char tmp[1024];
+    char tmp[4][1024];
+    char target[4][1024];
     int update;
     int use_strftime;
     const char *muxer;
@@ -53,7 +54,6 @@ static int write_header(AVFormatContext *s)
     const char *proto = avio_find_protocol_name(s->filename);
 
     av_strlcpy(img->path, s->filename, sizeof(img->path));
-    snprintf(img->tmp, sizeof(img->tmp), "%s.tmp", s->filename);
 
     /* find format */
     if (s->oformat->flags & AVFMT_NOFILE)
@@ -90,6 +90,7 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt)
     AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(codec->pix_fmt);
     int i;
+    int nb_renames = 0;
 
     if (!img->is_pipe) {
         if (img->update) {
@@ -111,9 +112,11 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt)
             return AVERROR(EINVAL);
         }
         for (i = 0; i < 4; i++) {
-            if (avio_open2(&pb[i], img->use_rename ? img->tmp : filename, AVIO_FLAG_WRITE,
+            snprintf(img->tmp[i], sizeof(img->tmp[i]), "%s.tmp", filename);
+            av_strlcpy(img->target[i], filename, sizeof(img->target[i]));
+            if (avio_open2(&pb[i], img->use_rename ? img->tmp[i] : filename, AVIO_FLAG_WRITE,
                            &s->interrupt_callback, NULL) < 0) {
-                av_log(s, AV_LOG_ERROR, "Could not open file : %s\n", img->use_rename ? img->tmp : filename);
+                av_log(s, AV_LOG_ERROR, "Could not open file : %s\n", img->use_rename ? img->tmp[i] : filename);
                 return AVERROR(EIO);
             }
 
@@ -121,6 +124,8 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt)
                 break;
             filename[strlen(filename) - 1] = "UVAx"[i];
         }
+        if (img->use_rename)
+            nb_renames = i + 1;
     } else {
         pb[0] = s->pb;
     }
@@ -178,8 +183,9 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt)
     avio_flush(pb[0]);
     if (!img->is_pipe) {
         avio_closep(&pb[0]);
-        if (img->use_rename)
-            ff_rename(img->tmp, filename, s);
+        for (i = 0; i < nb_renames; i++) {
+            ff_rename(img->tmp[i], img->target[i], s);
+        }
     }
 
     img->img_number++;
-- 
GitLab