Merge commit '35c6ce76b107225a19eb33aea38857d2405882af'
[ffmpeg.git] / libavformat / wavenc.c
index 450018f..2345fc5 100644 (file)
@@ -38,6 +38,7 @@
 #include "libavutil/mathematics.h"
 #include "libavutil/opt.h"
 #include "libavutil/time.h"
 #include "libavutil/mathematics.h"
 #include "libavutil/opt.h"
 #include "libavutil/time.h"
+#include "libavutil/time_internal.h"
 
 #include "avformat.h"
 #include "avio.h"
 
 #include "avformat.h"
 #include "avio.h"
@@ -170,18 +171,13 @@ static av_cold int peak_init_writer(AVFormatContext *s)
     if (wav->peak_bps == 1 && wav->peak_format == PEAK_FORMAT_UINT16) {
         av_log(s, AV_LOG_ERROR,
                "Writing 16 bit peak for 8 bit audio does not make sense\n");
     if (wav->peak_bps == 1 && wav->peak_format == PEAK_FORMAT_UINT16) {
         av_log(s, AV_LOG_ERROR,
                "Writing 16 bit peak for 8 bit audio does not make sense\n");
-        return -1;
+        return AVERROR(EINVAL);
     }
 
     }
 
-    wav->peak_maxpos = av_mallocz(enc->channels * sizeof(*wav->peak_maxpos));
-    if (!wav->peak_maxpos)
-        goto nomem;
-    wav->peak_maxneg = av_mallocz(enc->channels * sizeof(*wav->peak_maxneg));
-    if (!wav->peak_maxneg)
-        goto nomem;
-
+    wav->peak_maxpos = av_mallocz_array(enc->channels, sizeof(*wav->peak_maxpos));
+    wav->peak_maxneg = av_mallocz_array(enc->channels, sizeof(*wav->peak_maxneg));
     wav->peak_output = av_malloc(PEAK_BUFFER_SIZE);
     wav->peak_output = av_malloc(PEAK_BUFFER_SIZE);
-    if (!wav->peak_output)
+    if (!wav->peak_maxpos || !wav->peak_maxneg || !wav->peak_output)
         goto nomem;
 
     wav->peak_outbuf_size = PEAK_BUFFER_SIZE;
         goto nomem;
 
     wav->peak_outbuf_size = PEAK_BUFFER_SIZE;
@@ -256,7 +252,7 @@ static void peak_write_frame(AVFormatContext *s)
     wav->peak_num_frames++;
 }
 
     wav->peak_num_frames++;
 }
 
-static void peak_write_chunk(AVFormatContext *s)
+static int peak_write_chunk(AVFormatContext *s)
 {
     WAVMuxContext *wav = s->priv_data;
     AVIOContext *pb = s->pb;
 {
     WAVMuxContext *wav = s->priv_data;
     AVIOContext *pb = s->pb;
@@ -270,13 +266,18 @@ static void peak_write_chunk(AVFormatContext *s)
     if (wav->peak_block_pos)
         peak_write_frame(s);
 
     if (wav->peak_block_pos)
         peak_write_frame(s);
 
-    memset(timestamp, 0, 28);
-    if (!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
+    memset(timestamp, 0, sizeof(timestamp));
+    if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
+        struct tm tmpbuf;
         av_log(s, AV_LOG_INFO, "Writing local time and date to Peak Envelope Chunk\n");
         now0 = av_gettime();
         now_secs = now0 / 1000000;
         av_log(s, AV_LOG_INFO, "Writing local time and date to Peak Envelope Chunk\n");
         now0 = av_gettime();
         now_secs = now0 / 1000000;
-        strftime(timestamp, 28, "%Y:%m:%d:%H:%M:%S:", localtime(&now_secs));
-        av_strlcatf(timestamp, 28, "%03d", (int)((now0 / 1000) % 1000));
+        if (strftime(timestamp, sizeof(timestamp), "%Y:%m:%d:%H:%M:%S:", localtime_r(&now_secs, &tmpbuf))) {
+            av_strlcatf(timestamp, sizeof(timestamp), "%03d", (int)((now0 / 1000) % 1000));
+        } else {
+            av_log(s, AV_LOG_ERROR, "Failed to write timestamp\n");
+            return -1;
+        }
     }
 
     avio_wl32(pb, 1);                           /* version */
     }
 
     avio_wl32(pb, 1);                           /* version */
@@ -296,6 +297,8 @@ static void peak_write_chunk(AVFormatContext *s)
 
     if (!wav->data)
         wav->data = peak;
 
     if (!wav->data)
         wav->data = peak;
+
+    return 0;
 }
 
 static int wav_write_header(AVFormatContext *s)
 }
 
 static int wav_write_header(AVFormatContext *s)
@@ -328,15 +331,15 @@ static int wav_write_header(AVFormatContext *s)
     }
 
     if (wav->write_peak != 2) {
     }
 
     if (wav->write_peak != 2) {
-    /* format header */
-    fmt = ff_start_tag(pb, "fmt ");
-    if (ff_put_wav_header(pb, s->streams[0]->codec, 0) < 0) {
-        const AVCodecDescriptor *desc = avcodec_descriptor_get(s->streams[0]->codec->codec_id);
-        av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
-               desc ? desc->name : "unknown");
-        return AVERROR(ENOSYS);
-    }
-    ff_end_tag(pb, fmt);
+        /* format header */
+        fmt = ff_start_tag(pb, "fmt ");
+        if (ff_put_wav_header(pb, s->streams[0]->codec, 0) < 0) {
+            const AVCodecDescriptor *desc = avcodec_descriptor_get(s->streams[0]->codec->codec_id);
+            av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
+                   desc ? desc->name : "unknown");
+            return AVERROR(ENOSYS);
+        }
+        ff_end_tag(pb, fmt);
     }
 
     if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
     }
 
     if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
@@ -360,11 +363,11 @@ static int wav_write_header(AVFormatContext *s)
     wav->minpts = INT64_MAX;
 
     if (wav->write_peak != 2) {
     wav->minpts = INT64_MAX;
 
     if (wav->write_peak != 2) {
-    /* info header */
-    ff_riff_write_info(s);
+        /* info header */
+        ff_riff_write_info(s);
 
 
-    /* data header */
-    wav->data = ff_start_tag(pb, "data");
+        /* data header */
+        wav->data = ff_start_tag(pb, "data");
     }
 
     avio_flush(pb);
     }
 
     avio_flush(pb);
@@ -378,7 +381,7 @@ static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
     WAVMuxContext    *wav = s->priv_data;
 
     if (wav->write_peak != 2)
     WAVMuxContext    *wav = s->priv_data;
 
     if (wav->write_peak != 2)
-    avio_write(pb, pkt->data, pkt->size);
+        avio_write(pb, pkt->data, pkt->size);
 
     if (wav->write_peak) {
         int c = 0;
 
     if (wav->write_peak) {
         int c = 0;
@@ -417,6 +420,7 @@ static int wav_write_trailer(AVFormatContext *s)
     int64_t file_size, data_size;
     int64_t number_of_samples = 0;
     int rf64 = 0;
     int64_t file_size, data_size;
     int64_t number_of_samples = 0;
     int rf64 = 0;
+    int ret = 0;
 
     avio_flush(pb);
 
 
     avio_flush(pb);
 
@@ -427,7 +431,7 @@ static int wav_write_trailer(AVFormatContext *s)
         }
 
         if (wav->write_peak && wav->peak_output) {
         }
 
         if (wav->write_peak && wav->peak_output) {
-            peak_write_chunk(s);
+            ret = peak_write_chunk(s);
             avio_flush(pb);
         }
 
             avio_flush(pb);
         }
 
@@ -488,7 +492,7 @@ static int wav_write_trailer(AVFormatContext *s)
     if (wav->write_peak)
         peak_free_buffers(s);
 
     if (wav->write_peak)
         peak_free_buffers(s);
 
-    return 0;
+    return ret;
 }
 
 #define OFFSET(x) offsetof(WAVMuxContext, x)
 }
 
 #define OFFSET(x) offsetof(WAVMuxContext, x)