[04/13] movenc: Allow the caller to decide on fragmentation

Message ID 1327082572-67935-3-git-send-email-martin@martin.st
State Superseded
Headers show

Commit Message

Martin Storsjö Jan. 20, 2012, 6:02 p.m.
---
Added a new movflag for indicating that the caller wants to
use this method (without having to enable fragmentation by choosing
any other fragmentation criteria).

 libavformat/movenc.c |   26 +++++++++++++++++---------
 libavformat/movenc.h |    1 +
 2 files changed, 18 insertions(+), 9 deletions(-)

Comments

Martin Storsjö Jan. 25, 2012, 9:31 a.m. | #1
On Fri, 20 Jan 2012, Martin Storsjö wrote:

> ---
> Added a new movflag for indicating that the caller wants to
> use this method (without having to enable fragmentation by choosing
> any other fragmentation criteria).
>
> libavformat/movenc.c |   26 +++++++++++++++++---------
> libavformat/movenc.h |    1 +
> 2 files changed, 18 insertions(+), 9 deletions(-)

This is missing a review.

// Martin
Ronald Bultje Jan. 25, 2012, 9:39 a.m. | #2
Hi,

On Sat, Jan 21, 2012 at 2:02 AM, Martin Storsjö <martin@martin.st> wrote:
> @@ -2370,13 +2371,19 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
>  {
>     MOVMuxContext *mov = s->priv_data;
>     AVIOContext *pb = s->pb;
> -    MOVTrack *trk = &mov->tracks[pkt->stream_index];
> +    MOVTrack *trk = &mov->tracks[pkt ? pkt->stream_index : 0];
>     AVCodecContext *enc = trk->enc;
>     unsigned int samplesInChunk = 0;
> -    int size= pkt->size;
> +    int size = pkt ? pkt->size : 0;
>     uint8_t *reformatted_data = NULL;
>
>     if (!s->pb->seekable) return 0; /* Can't handle that */
> +
> +    if (!pkt) {
> +        mov_flush_fragment(s);
> +        return 0;
> +    }

This is kinda ugly, I'd prefer a new function called
write_pkt_internal() which is the above ff_mov_...(), and then a new
function for that like this:

void ff_mov_write_packet(..)
{
    if (pkt) {
        write_pkt_internal(..);
    } else {
        mov_flush_fragment(..);
    }
}

Then the other ifs become unnecessary.

Ronald
Martin Storsjö Jan. 25, 2012, 9:49 a.m. | #3
On Wed, 25 Jan 2012, Ronald S. Bultje wrote:

> Hi,
>
> On Sat, Jan 21, 2012 at 2:02 AM, Martin Storsjö <martin@martin.st> wrote:
>> @@ -2370,13 +2371,19 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
>>  {
>>     MOVMuxContext *mov = s->priv_data;
>>     AVIOContext *pb = s->pb;
>> -    MOVTrack *trk = &mov->tracks[pkt->stream_index];
>> +    MOVTrack *trk = &mov->tracks[pkt ? pkt->stream_index : 0];
>>     AVCodecContext *enc = trk->enc;
>>     unsigned int samplesInChunk = 0;
>> -    int size= pkt->size;
>> +    int size = pkt ? pkt->size : 0;
>>     uint8_t *reformatted_data = NULL;
>>
>>     if (!s->pb->seekable) return 0; /* Can't handle that */
>> +
>> +    if (!pkt) {
>> +        mov_flush_fragment(s);
>> +        return 0;
>> +    }
>
> This is kinda ugly, I'd prefer a new function called
> write_pkt_internal() which is the above ff_mov_...(), and then a new
> function for that like this:
>
> void ff_mov_write_packet(..)
> {
>    if (pkt) {
>        write_pkt_internal(..);
>    } else {
>        mov_flush_fragment(..);
>    }
> }
>
> Then the other ifs become unnecessary.

Good point, rewrote it in this form.

// Martin

Patch

diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index 08d534a..5d92e6f 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -48,6 +48,7 @@  static const AVOption options[] = {
     { "empty_moov", "Make the initial moov atom empty (not supported by QuickTime)", 0, AV_OPT_TYPE_CONST, {.dbl = FF_MOV_FLAG_EMPTY_MOOV}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
     { "frag_keyframe", "Fragment at video keyframes", 0, AV_OPT_TYPE_CONST, {.dbl = FF_MOV_FLAG_FRAG_KEYFRAME}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
     { "separate_moof", "Write separate moof/mdat atoms for each track", 0, AV_OPT_TYPE_CONST, {.dbl = FF_MOV_FLAG_SEPARATE_MOOF}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
+    { "frag_custom", "Flush fragments on caller requests", 0, AV_OPT_TYPE_CONST, {.dbl = FF_MOV_FLAG_FRAG_CUSTOM}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
     FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags),
     { "skip_iods", "Skip writing iods atom.", offsetof(MOVMuxContext, iods_skip), AV_OPT_TYPE_INT, {.dbl = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
     { "iods_audio_profile", "iods audio profile atom.", offsetof(MOVMuxContext, iods_audio_profile), AV_OPT_TYPE_INT, {.dbl = -1}, -1, 255, AV_OPT_FLAG_ENCODING_PARAM},
@@ -2370,13 +2371,19 @@  int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
 {
     MOVMuxContext *mov = s->priv_data;
     AVIOContext *pb = s->pb;
-    MOVTrack *trk = &mov->tracks[pkt->stream_index];
+    MOVTrack *trk = &mov->tracks[pkt ? pkt->stream_index : 0];
     AVCodecContext *enc = trk->enc;
     unsigned int samplesInChunk = 0;
-    int size= pkt->size;
+    int size = pkt ? pkt->size : 0;
     uint8_t *reformatted_data = NULL;
 
     if (!s->pb->seekable) return 0; /* Can't handle that */
+
+    if (!pkt) {
+        mov_flush_fragment(s);
+        return 0;
+    }
+
     if (!size) return 0; /* Discard 0 sized packets */
 
     if ((mov->max_fragment_duration && trk->entry &&
@@ -2685,7 +2692,8 @@  static int mov_write_header(AVFormatContext *s)
      * enabled. */
     if (mov->max_fragment_duration || mov->max_fragment_size ||
         mov->flags & (FF_MOV_FLAG_EMPTY_MOOV |
-                      FF_MOV_FLAG_FRAG_KEYFRAME))
+                      FF_MOV_FLAG_FRAG_KEYFRAME |
+                      FF_MOV_FLAG_FRAG_CUSTOM))
         mov->flags |= FF_MOV_FLAG_FRAGMENT;
 
     if (!(mov->flags & FF_MOV_FLAG_EMPTY_MOOV))
@@ -2794,7 +2802,7 @@  AVOutputFormat ff_mov_muxer = {
     .write_header      = mov_write_header,
     .write_packet      = ff_mov_write_packet,
     .write_trailer     = mov_write_trailer,
-    .flags = AVFMT_GLOBALHEADER,
+    .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
     .codec_tag = (const AVCodecTag* const []){codec_movvideo_tags, codec_movaudio_tags, 0},
     .priv_class = &mov_muxer_class,
 };
@@ -2811,7 +2819,7 @@  AVOutputFormat ff_tgp_muxer = {
     .write_header      = mov_write_header,
     .write_packet      = ff_mov_write_packet,
     .write_trailer     = mov_write_trailer,
-    .flags = AVFMT_GLOBALHEADER,
+    .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
     .codec_tag = (const AVCodecTag* const []){codec_3gp_tags, 0},
     .priv_class = &tgp_muxer_class,
 };
@@ -2833,7 +2841,7 @@  AVOutputFormat ff_mp4_muxer = {
     .write_header      = mov_write_header,
     .write_packet      = ff_mov_write_packet,
     .write_trailer     = mov_write_trailer,
-    .flags = AVFMT_GLOBALHEADER,
+    .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
     .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
     .priv_class = &mp4_muxer_class,
 };
@@ -2854,7 +2862,7 @@  AVOutputFormat ff_psp_muxer = {
     .write_header      = mov_write_header,
     .write_packet      = ff_mov_write_packet,
     .write_trailer     = mov_write_trailer,
-    .flags = AVFMT_GLOBALHEADER,
+    .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
     .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
     .priv_class = &psp_muxer_class,
 };
@@ -2871,7 +2879,7 @@  AVOutputFormat ff_tg2_muxer = {
     .write_header      = mov_write_header,
     .write_packet      = ff_mov_write_packet,
     .write_trailer     = mov_write_trailer,
-    .flags = AVFMT_GLOBALHEADER,
+    .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
     .codec_tag = (const AVCodecTag* const []){codec_3gp_tags, 0},
     .priv_class = &tg2_muxer_class,
 };
@@ -2889,7 +2897,7 @@  AVOutputFormat ff_ipod_muxer = {
     .write_header      = mov_write_header,
     .write_packet      = ff_mov_write_packet,
     .write_trailer     = mov_write_trailer,
-    .flags = AVFMT_GLOBALHEADER,
+    .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
     .codec_tag = (const AVCodecTag* const []){codec_ipod_tags, 0},
     .priv_class = &ipod_muxer_class,
 };
diff --git a/libavformat/movenc.h b/libavformat/movenc.h
index 4347dd5..a415303 100644
--- a/libavformat/movenc.h
+++ b/libavformat/movenc.h
@@ -144,6 +144,7 @@  typedef struct MOVMuxContext {
 #define FF_MOV_FLAG_EMPTY_MOOV 4
 #define FF_MOV_FLAG_FRAG_KEYFRAME 8
 #define FF_MOV_FLAG_SEPARATE_MOOF 16
+#define FF_MOV_FLAG_FRAG_CUSTOM 32
 
 int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt);