[FFmpeg-devel] [PATCH 5/5] lavf/segment: write attached pictures to all segments by default

Rodger Combs rodger.combs at gmail.com
Mon Jul 4 20:33:20 EEST 2016


---
 doc/muxers.texi       |  4 ++++
 libavformat/segment.c | 33 +++++++++++++++++++++++++++++++++
 libavformat/version.h |  2 +-
 3 files changed, 38 insertions(+), 1 deletion(-)

diff --git a/doc/muxers.texi b/doc/muxers.texi
index adf853e..fe95cc6 100644
--- a/doc/muxers.texi
+++ b/doc/muxers.texi
@@ -1331,6 +1331,10 @@ argument must be a time duration specification, and defaults to 0.
 If enabled, write an empty segment if there are no packets during the period a
 segment would usually span. Otherwise, the segment will be filled with the next
 packet written. Defaults to @code{0}.
+
+ at item dup_attached_pics @var{1|0}
+If enabled, attached-picture packets will be written to all segments, rather
+than only the first. Defaults to @code{1}.
 @end table
 
 @subsection Examples
diff --git a/libavformat/segment.c b/libavformat/segment.c
index 5aec018..a0857c9 100644
--- a/libavformat/segment.c
+++ b/libavformat/segment.c
@@ -121,6 +121,7 @@ typedef struct SegmentContext {
     int   reference_stream_index;
     int   break_non_keyframes;
     int   write_empty;
+    int   dup_attached_pics;
 
     int use_rename;
     char temp_list_filename[1024];
@@ -128,6 +129,8 @@ typedef struct SegmentContext {
     SegmentListEntry cur_entry;
     SegmentListEntry *segment_list_entries;
     SegmentListEntry *segment_list_entries_end;
+
+    AVPacket *attached_pics;
 } SegmentContext;
 
 static void print_csv_escaped_str(AVIOContext *ctx, const char *str)
@@ -303,12 +306,20 @@ static int segment_start(AVFormatContext *s, int write_header)
         av_opt_set(oc->priv_data, "mpegts_flags", "+resend_headers", 0);
 
     if (write_header) {
+        int i;
         AVDictionary *options = NULL;
         av_dict_copy(&options, seg->format_options, 0);
         err = avformat_write_header(oc, &options);
         av_dict_free(&options);
         if (err < 0)
             return err;
+        for (i = 0; i < s->nb_streams; i++) {
+            if (seg->dup_attached_pics &&
+                s->streams[i]->disposition & AV_DISPOSITION_ATTACHED_PIC &&
+                seg->attached_pics[i].data) {
+                av_write_frame(oc, &seg->attached_pics[i]);
+            }
+        }
     }
 
     seg->segment_frame_count = 0;
@@ -826,6 +837,11 @@ static int seg_init(AVFormatContext *s)
         avpriv_set_pts_info(outer_st, inner_st->pts_wrap_bits, inner_st->time_base.num, inner_st->time_base.den);
     }
 
+    if (seg->dup_attached_pics && !(seg->attached_pics = av_calloc(s->nb_streams, sizeof(AVPacket)))) {
+        ret = AVERROR(ENOMEM);
+        goto fail;
+    }
+
     if (oc->avoid_negative_ts > 0 && s->avoid_negative_ts < 0)
         s->avoid_negative_ts = 1;
 
@@ -864,6 +880,9 @@ static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
     if (!seg->avf)
         return AVERROR(EINVAL);
 
+    if (seg->dup_attached_pics && st->disposition & AV_DISPOSITION_ATTACHED_PIC)
+        av_copy_packet(&seg->attached_pics[pkt->stream_index], pkt);
+
 calc_times:
     if (seg->times) {
         end_pts = seg->segment_count < seg->nb_times ?
@@ -1012,6 +1031,17 @@ fail:
     return ret;
 }
 
+static void seg_deinit(struct AVFormatContext *s)
+{
+    SegmentContext *seg = s->priv_data;
+    int i;
+    if (seg->attached_pics) {
+        for (i = 0; i < s->nb_streams; i++)
+            av_packet_unref(&seg->attached_pics[i]);
+        av_freep(&seg->attached_pics);
+    }
+}
+
 static int seg_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
 {
     SegmentContext *seg = s->priv_data;
@@ -1075,6 +1105,7 @@ static const AVOption options[] = {
     { "reset_timestamps", "reset timestamps at the begin of each segment", OFFSET(reset_timestamps), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, E },
     { "initial_offset", "set initial timestamp offset", OFFSET(initial_offset), AV_OPT_TYPE_DURATION, {.i64 = 0}, -INT64_MAX, INT64_MAX, E },
     { "write_empty_segments", "allow writing empty 'filler' segments", OFFSET(write_empty), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, E },
+    { "dup_attached_pics",  "write attached pictures to all segments", OFFSET(dup_attached_pics), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, E },
     { NULL },
 };
 
@@ -1093,6 +1124,7 @@ AVOutputFormat ff_segment_muxer = {
     .init           = seg_init,
     .write_packet   = seg_write_packet,
     .write_trailer  = seg_write_trailer,
+    .deinit         = seg_deinit,
     .check_bitstream = seg_check_bitstream,
     .priv_class     = &seg_class,
 };
@@ -1112,6 +1144,7 @@ AVOutputFormat ff_stream_segment_muxer = {
     .init           = seg_init,
     .write_packet   = seg_write_packet,
     .write_trailer  = seg_write_trailer,
+    .deinit         = seg_deinit,
     .check_bitstream = seg_check_bitstream,
     .priv_class     = &sseg_class,
 };
diff --git a/libavformat/version.h b/libavformat/version.h
index 1c398a4..badf95b 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -33,7 +33,7 @@
 // Also please add any ticket numbers that you belive might be affected here
 #define LIBAVFORMAT_VERSION_MAJOR  57
 #define LIBAVFORMAT_VERSION_MINOR  42
-#define LIBAVFORMAT_VERSION_MICRO 101
+#define LIBAVFORMAT_VERSION_MICRO 102
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
                                                LIBAVFORMAT_VERSION_MINOR, \
-- 
2.9.0



More information about the ffmpeg-devel mailing list