[FFmpeg-devel] [PATCH 2/2 v2] avformat/flacenc: add flac_init() and flac_deinit()

James Almer jamrial at gmail.com
Fri Nov 24 05:38:25 EET 2017


Signed-off-by: James Almer <jamrial at gmail.com>
---
Simpler/smaller diff.

 libavformat/flacenc.c | 50 +++++++++++++++++++++++++++++++++-----------------
 1 file changed, 33 insertions(+), 17 deletions(-)

diff --git a/libavformat/flacenc.c b/libavformat/flacenc.c
index 84da54a1df..d5fcf96b6b 100644
--- a/libavformat/flacenc.c
+++ b/libavformat/flacenc.c
@@ -197,11 +197,11 @@ static int flac_finish_header(struct AVFormatContext *s)
     return 0;
 }
 
-static int flac_write_header(struct AVFormatContext *s)
+static int flac_init(struct AVFormatContext *s)
 {
     AVCodecParameters *par;
     FlacMuxerContext *c = s->priv_data;
-    int ret, i;
+    int i;
 
     c->audio_stream_idx = -1;
     for (i = 0; i < s->nb_streams; i++) {
@@ -233,14 +233,6 @@ static int flac_write_header(struct AVFormatContext *s)
     if (c->nb_pics && !(c->pics = av_calloc(c->nb_pics, sizeof(AVPacket))))
         return AVERROR(ENOMEM);
 
-    if (!c->write_header)
-        return 0;
-
-    ret = ff_flac_write_header(s->pb, par->extradata,
-                               par->extradata_size, 0);
-    if (ret)
-        return ret;
-
     /* add the channel layout tag */
     if (par->channel_layout &&
         !(par->channel_layout & ~0x3ffffULL) &&
@@ -258,6 +250,23 @@ static int flac_write_header(struct AVFormatContext *s)
         }
     }
 
+    return 0;
+}
+
+static int flac_write_header(struct AVFormatContext *s)
+{
+    FlacMuxerContext *c = s->priv_data;
+    AVCodecParameters *par = s->streams[c->audio_stream_idx]->codecpar;
+    int ret;
+
+    if (!c->write_header)
+        return 0;
+
+    ret = ff_flac_write_header(s->pb, par->extradata,
+                               par->extradata_size, 0);
+    if (ret < 0)
+        return ret;
+
     if (!c->waiting_pics)
         ret = flac_finish_header(s);
 
@@ -313,7 +322,6 @@ static int flac_write_trailer(struct AVFormatContext *s)
     FlacMuxerContext *c = s->priv_data;
     uint8_t *streaminfo = c->streaminfo ? c->streaminfo :
                                           s->streams[c->audio_stream_idx]->codecpar->extradata;
-    int i;
 
     if (c->waiting_pics) {
         av_log(s, AV_LOG_WARNING, "No packets were sent for some of the "
@@ -321,10 +329,6 @@ static int flac_write_trailer(struct AVFormatContext *s)
         flac_queue_flush(s);
     }
 
-    for (i = 0; i < c->nb_pics; i++)
-        av_packet_unref(&c->pics[i]);
-    av_freep(&c->pics);
-
     if (!c->write_header || !streaminfo)
         return 0;
 
@@ -339,11 +343,21 @@ static int flac_write_trailer(struct AVFormatContext *s)
         av_log(s, AV_LOG_WARNING, "unable to rewrite FLAC header.\n");
     }
 
-    av_freep(&c->streaminfo);
-
     return 0;
 }
 
+static void flac_deinit(struct AVFormatContext *s)
+{
+    FlacMuxerContext *c = s->priv_data;
+    int i;
+
+    for (i = 0; i < c->nb_pics; i++)
+        av_packet_unref(&c->pics[i]);
+    av_freep(&c->pics);
+
+    av_freep(&c->streaminfo);
+}
+
 static int flac_write_packet(struct AVFormatContext *s, AVPacket *pkt)
 {
     FlacMuxerContext *c = s->priv_data;
@@ -420,9 +434,11 @@ AVOutputFormat ff_flac_muxer = {
     .extensions        = "flac",
     .audio_codec       = AV_CODEC_ID_FLAC,
     .video_codec       = AV_CODEC_ID_PNG,
+    .init              = flac_init,
     .write_header      = flac_write_header,
     .write_packet      = flac_write_packet,
     .write_trailer     = flac_write_trailer,
+    .deinit            = flac_deinit,
     .flags             = AVFMT_NOTIMESTAMPS,
     .priv_class        = &flac_muxer_class,
 };
-- 
2.15.0



More information about the ffmpeg-devel mailing list