diff mbox

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

Message ID 20171124033825.2616-1-jamrial@gmail.com
State New
Headers show

Commit Message

James Almer Nov. 24, 2017, 3:38 a.m. UTC
Signed-off-by: James Almer <jamrial@gmail.com>
---
Simpler/smaller diff.

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

Comments

Carl Eugen Hoyos Nov. 24, 2017, 11:19 a.m. UTC | #1
2017-11-24 4:38 GMT+01:00 James Almer <jamrial@gmail.com>:
> Signed-off-by: James Almer <jamrial@gmail.com>
> ---
> Simpler/smaller diff.

Sorry if this is obvious:
What is the advantage of having these new functions?

Carl Eugen
James Almer Nov. 24, 2017, 2:05 p.m. UTC | #2
On 11/24/2017 8:19 AM, Carl Eugen Hoyos wrote:
> 2017-11-24 4:38 GMT+01:00 James Almer <jamrial@gmail.com>:
>> Signed-off-by: James Almer <jamrial@gmail.com>
>> ---
>> Simpler/smaller diff.
> 
> Sorry if this is obvious:
> What is the advantage of having these new functions?
> 
> Carl Eugen

Muxers with an init() function can be initialized before the header is
written. deinit() is mainly meant to free any allocated buffer in init()
in case the latter fails, but should ideally also be used to free what
would otherwise be freed in write_trailer().
API users can know if muxing is possible and in some cases have the
output streams initialized with container specific parameters by calling
avformat_init_output() before avformat_write_header() with the above
functions.

ffmpeg.c should ideally be doing the above, so we can remove all the
delayed header writing code in libavformat/mux.c (see the patch i sent
for the latter some time ago).
diff mbox

Patch

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,
 };