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

Submitted by James Almer on Nov. 23, 2017, 10:08 p.m.

Details

Message ID 20171123220843.804-2-jamrial@gmail.com
State New
Headers show

Commit Message

James Almer Nov. 23, 2017, 10:08 p.m.
Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavformat/flacenc.c | 58 +++++++++++++++++++++++++++++++++------------------
 1 file changed, 38 insertions(+), 20 deletions(-)

Patch hide | download patch | download mbox

diff --git a/libavformat/flacenc.c b/libavformat/flacenc.c
index 84da54a1df..7c94670f1d 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++) {
@@ -229,17 +229,6 @@  static int flac_write_header(struct AVFormatContext *s)
         av_log(s, AV_LOG_ERROR, "No audio stream present.\n");
         return AVERROR(EINVAL);
     }
-    c->waiting_pics = c->nb_pics = s->nb_streams - 1;
-    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 &&
@@ -258,6 +247,28 @@  static int flac_write_header(struct AVFormatContext *s)
         }
     }
 
+    c->waiting_pics = c->nb_pics = s->nb_streams - 1;
+    if (c->nb_pics && !(c->pics = av_calloc(c->nb_pics, sizeof(AVPacket))))
+        return AVERROR(ENOMEM);
+
+
+    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)
+        return ret;
+
     if (!c->waiting_pics)
         ret = flac_finish_header(s);
 
@@ -313,7 +324,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 +331,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 +345,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 +436,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,
 };