[FFmpeg-devel] [PATCH 2/2] try
Michael Niedermayer
michaelni at gmx.at
Mon Jun 30 01:04:28 CEST 2014
---
libavformat/segment.c | 573 +++++++++++++++++++++++++++++++------------------
1 file changed, 362 insertions(+), 211 deletions(-)
diff --git a/libavformat/segment.c b/libavformat/segment.c
index fe84f27..20fa14c 100644
--- a/libavformat/segment.c
+++ b/libavformat/segment.c
@@ -48,6 +48,13 @@ typedef struct SegmentListEntry {
struct SegmentListEntry *next;
} SegmentListEntry;
+typedef struct SegmentEntryWrapper
+{
+ SegmentListEntry cur_entry;
+ SegmentListEntry *segment_list_entries;
+ SegmentListEntry *segment_list_entries_end;
+} SegmentEntryWrapper;
+
typedef enum {
LIST_TYPE_UNDEFINED = -1,
LIST_TYPE_FLAT = 0,
@@ -61,6 +68,9 @@ typedef enum {
#define SEGMENT_LIST_FLAG_CACHE 1
#define SEGMENT_LIST_FLAG_LIVE 2
+#define SEGMENT_ENTRY_FULL 0
+#define SEGMENT_ENTRY_AUDIO_ONLY 1
+
typedef struct {
const AVClass *class; /**< Class for private options. */
int segment_idx; ///< index of the segment file to write, starting from 0
@@ -68,14 +78,15 @@ typedef struct {
int segment_idx_wrap_nb; ///< number of time the index has wraped
int segment_count; ///< number of segment files already written
AVOutputFormat *oformat;
- AVFormatContext *avf;
+ AVFormatContext *avf[2];
+ int64_t stream_size[2];
char *format; ///< format to use for output segment files
char *list; ///< filename for the segment list file
int list_flags; ///< flags affecting list generation
int list_size; ///< number of entries for the segment list file
char *entry_prefix; ///< prefix to add to list entry filenames
ListType list_type; ///< set the list type
- AVIOContext *list_pb; ///< list file put-byte context
+ AVIOContext *list_pb[2]; ///< list file put-byte context
char *time_str; ///< segment duration specification string
int64_t time; ///< segment duration
@@ -97,9 +108,13 @@ typedef struct {
char *reference_stream_specifier; ///< reference stream specifier
int reference_stream_index;
- SegmentListEntry cur_entry;
- SegmentListEntry *segment_list_entries;
- SegmentListEntry *segment_list_entries_end;
+ int progressive;
+ char *audio_list;
+ char *root_m3u8;
+ char *vc_tag;
+ char *ac_tag;
+
+ SegmentEntryWrapper entry_wrapper[2];
int is_first_pkt; ///< tells if it is the first packet in the segment
} SegmentContext;
@@ -123,137 +138,205 @@ static void print_csv_escaped_str(AVIOContext *ctx, const char *str)
static int segment_mux_init(AVFormatContext *s)
{
SegmentContext *seg = s->priv_data;
- AVFormatContext *oc;
- int i;
-
- seg->avf = oc = avformat_alloc_context();
- if (!oc)
- return AVERROR(ENOMEM);
-
- oc->oformat = seg->oformat;
- oc->interrupt_callback = s->interrupt_callback;
- av_dict_copy(&oc->metadata, s->metadata, 0);
-
- for (i = 0; i < s->nb_streams; i++) {
- AVStream *st;
- AVCodecContext *icodec, *ocodec;
-
- if (!(st = avformat_new_stream(oc, NULL)))
+ int iCount = seg->progressive?2:1;
+ for (int formats_num = 0; formats_num < iCount; ++formats_num) {
+ AVFormatContext *oc;
+ int i;
+
+ seg->avf[formats_num] = oc = avformat_alloc_context();
+ if (!oc)
return AVERROR(ENOMEM);
- icodec = s->streams[i]->codec;
- ocodec = st->codec;
- avcodec_copy_context(ocodec, icodec);
- if (!oc->oformat->codec_tag ||
- av_codec_get_id (oc->oformat->codec_tag, icodec->codec_tag) == ocodec->codec_id ||
- av_codec_get_tag(oc->oformat->codec_tag, icodec->codec_id) <= 0) {
- ocodec->codec_tag = icodec->codec_tag;
- } else {
- ocodec->codec_tag = 0;
+
+ oc->oformat = seg->oformat;
+ oc->interrupt_callback = s->interrupt_callback;
+ av_dict_copy(&oc->metadata, s->metadata, 0);
+
+ for (i = 0; i < s->nb_streams; i++) {
+ if (formats_num == SEGMENT_ENTRY_AUDIO_ONLY && s->streams[i]->codec->codec_type != AVMEDIA_TYPE_AUDIO) {
+ continue;
+ }
+ AVStream *st;
+ AVCodecContext *icodec, *ocodec;
+
+ if (!(st = avformat_new_stream(oc, NULL)))
+ return AVERROR(ENOMEM);
+ icodec = s->streams[i]->codec;
+ ocodec = st->codec;
+ avcodec_copy_context(ocodec, icodec);
+ if (!oc->oformat->codec_tag ||
+ av_codec_get_id (oc->oformat->codec_tag, icodec->codec_tag) == ocodec->codec_id ||
+ av_codec_get_tag(oc->oformat->codec_tag, icodec->codec_id) <= 0) {
+ ocodec->codec_tag = icodec->codec_tag;
+ } else {
+ ocodec->codec_tag = 0;
+ }
+ st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
}
- st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
}
-
return 0;
}
static int set_segment_filename(AVFormatContext *s)
{
SegmentContext *seg = s->priv_data;
- AVFormatContext *oc = seg->avf;
- size_t size;
-
+ int iCount = seg->progressive?2:1;
if (seg->segment_idx_wrap)
seg->segment_idx %= seg->segment_idx_wrap;
- if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
- s->filename, seg->segment_idx) < 0) {
- av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s'\n", s->filename);
- return AVERROR(EINVAL);
+
+ for (int cnt = 0; cnt < iCount; ++cnt) {
+ AVFormatContext *oc = seg->avf[cnt];
+ size_t size;
+
+ if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
+ s->filename, seg->segment_idx) < 0) {
+ av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s'\n", s->filename);
+ return AVERROR(EINVAL);
+ }
+ if (seg->progressive) {
+ char buf[1024];
+ if (cnt==SEGMENT_ENTRY_AUDIO_ONLY) {
+ snprintf(buf, 1024, "audio-%s", av_basename(oc->filename));
+ } else {
+ snprintf(buf, 1024, "video-%s", av_basename(oc->filename));
+ }
+ snprintf(oc->filename, 1024, "%s/%s", av_dirname(oc->filename), buf);
+ }
+
+ /* copy modified name in list entry */
+ size = strlen(av_basename(oc->filename)) + 1;
+ if (seg->entry_prefix)
+ size += strlen(seg->entry_prefix);
+
+ seg->entry_wrapper[cnt].cur_entry.filename = av_mallocz(size);
+ if (!seg->entry_wrapper[cnt].cur_entry.filename)
+ return AVERROR(ENOMEM);
+ snprintf(seg->entry_wrapper[cnt].cur_entry.filename, size, "%s%s",
+ seg->entry_prefix ? seg->entry_prefix : "",
+ av_basename(oc->filename));
}
- /* copy modified name in list entry */
- size = strlen(av_basename(oc->filename)) + 1;
- if (seg->entry_prefix)
- size += strlen(seg->entry_prefix);
-
- seg->cur_entry.filename = av_mallocz(size);
- if (!seg->cur_entry.filename)
- return AVERROR(ENOMEM);
- snprintf(seg->cur_entry.filename, size, "%s%s",
- seg->entry_prefix ? seg->entry_prefix : "",
- av_basename(oc->filename));
-
return 0;
}
static int segment_start(AVFormatContext *s, int write_header)
{
SegmentContext *seg = s->priv_data;
- AVFormatContext *oc = seg->avf;
+ int iCount = seg->progressive?2:1;
int err = 0;
-
- if (write_header) {
- avformat_free_context(oc);
- seg->avf = NULL;
- if ((err = segment_mux_init(s)) < 0)
- return err;
- oc = seg->avf;
+ for (int cnt = 0; cnt < iCount; ++cnt) {
+ AVFormatContext *oc = seg->avf[cnt];
+
+ if (write_header) {
+ avformat_free_context(oc);
+ seg->avf[cnt] = NULL;
+ }
}
+ if ((err = segment_mux_init(s)) < 0)
+ return err;
+ if ((err = set_segment_filename(s)) < 0)
+ return err;
+
seg->segment_idx++;
if ((seg->segment_idx_wrap) && (seg->segment_idx%seg->segment_idx_wrap == 0))
seg->segment_idx_wrap_nb++;
-
- if ((err = set_segment_filename(s)) < 0)
- return err;
-
- if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
- &s->interrupt_callback, NULL)) < 0) {
- av_log(s, AV_LOG_ERROR, "Failed to open segment '%s'\n", oc->filename);
- return err;
- }
-
- if (oc->oformat->priv_class && oc->priv_data)
- av_opt_set(oc->priv_data, "resend_headers", "1", 0); /* mpegts specific */
-
- if (write_header) {
- if ((err = avformat_write_header(oc, NULL)) < 0)
+ for (int cnt = 0; cnt < iCount; ++cnt) {
+ AVFormatContext *oc = seg->avf[cnt];
+ int err = 0;
+ if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
+ &s->interrupt_callback, NULL)) < 0) {
+ av_log(s, AV_LOG_ERROR, "Failed to open segment '%s'\n", oc->filename);
return err;
+ }
+
+ if (oc->oformat->priv_class && oc->priv_data)
+ av_opt_set(oc->priv_data, "resend_headers", "1", 0); /* mpegts specific */
+
+ if (write_header) {
+ if ((err = avformat_write_header(oc, NULL)) < 0)
+ return err;
+ }
+
+ seg->is_first_pkt = 1;
}
-
- seg->is_first_pkt = 1;
return 0;
}
-static int segment_list_open(AVFormatContext *s)
+static int root_m3u8_create(AVFormatContext *s)
{
SegmentContext *seg = s->priv_data;
+ AVStream *video = 0;
+ AVStream *audio = 0;
+ for (int i = 0; i < s->nb_streams; i++) {
+ if (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
+ video = s->streams[i];
+ } else if (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
+ audio = s->streams[i];
+ }
+ }
+ if (seg->list_type != LIST_TYPE_M3U8 || seg->root_m3u8 == 0 || seg->progressive == 0) {
+ return 0;
+ }
int ret;
-
- ret = avio_open2(&seg->list_pb, seg->list, AVIO_FLAG_WRITE,
+ AVIOContext *pb = 0;
+ ret = avio_open2(&pb, seg->root_m3u8, AVIO_FLAG_WRITE,
&s->interrupt_callback, NULL);
if (ret < 0) {
- av_log(s, AV_LOG_ERROR, "Failed to open segment list '%s'\n", seg->list);
+ av_log(s, AV_LOG_ERROR, "Failed to open root segment list '%s'\n", seg->root_m3u8);
return ret;
}
+ double full_duration = (seg->entry_wrapper[SEGMENT_ENTRY_FULL].cur_entry.end_time - seg->entry_wrapper[SEGMENT_ENTRY_FULL].segment_list_entries->start_time);
+ double a_duration = (seg->entry_wrapper[SEGMENT_ENTRY_AUDIO_ONLY].cur_entry.end_time - seg->entry_wrapper[SEGMENT_ENTRY_FULL].segment_list_entries->start_time);
+ int full_br = seg->stream_size[SEGMENT_ENTRY_FULL] * 8 / full_duration;
+ int a_br = seg->stream_size[SEGMENT_ENTRY_AUDIO_ONLY] * 8 / a_duration;
+ int w = video ? video->codec->width : 0;
+ int h = video ? video->codec->height : 0;
+
+ avio_printf(pb, "#EXTM3U\n");
+ avio_printf(pb, "#EXT-X-VERSION:3\n");
+ avio_printf(pb, "#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=%d,CODECS=\"%s, %s\",RESOLUTION=%dx%d\n", full_br, seg->vc_tag, seg->ac_tag, w, h);
+ avio_printf(pb, "%s\n", av_basename(seg->list));
+ avio_printf(pb, "#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=%d,CODECS=\"%s\"\n", a_br, seg->ac_tag);
+ avio_printf(pb, "%s\n", av_basename(seg->audio_list));
+
+ avio_close(pb);
+ return ret;
+}
- if (seg->list_type == LIST_TYPE_M3U8 && seg->segment_list_entries) {
- SegmentListEntry *entry;
- double max_duration = 0;
-
- avio_printf(seg->list_pb, "#EXTM3U\n");
- avio_printf(seg->list_pb, "#EXT-X-VERSION:3\n");
- avio_printf(seg->list_pb, "#EXT-X-MEDIA-SEQUENCE:%d\n", seg->segment_list_entries->index);
- avio_printf(seg->list_pb, "#EXT-X-ALLOW-CACHE:%s\n",
- seg->list_flags & SEGMENT_LIST_FLAG_CACHE ? "YES" : "NO");
-
- av_log(s, AV_LOG_VERBOSE, "EXT-X-MEDIA-SEQUENCE:%d\n",
- seg->segment_list_entries->index);
+static int segment_list_open(AVFormatContext *s)
+{
+ SegmentContext *seg = s->priv_data;
+ int ret;
- for (entry = seg->segment_list_entries; entry; entry = entry->next)
- max_duration = FFMAX(max_duration, entry->end_time - entry->start_time);
- avio_printf(seg->list_pb, "#EXT-X-TARGETDURATION:%"PRId64"\n", (int64_t)ceil(max_duration));
- } else if (seg->list_type == LIST_TYPE_FFCONCAT) {
- avio_printf(seg->list_pb, "ffconcat version 1.0\n");
+ int iCount = seg->progressive?2:1;
+ for (int cnt = 0; cnt < iCount; ++cnt) {
+ char* list = cnt==SEGMENT_ENTRY_FULL?seg->list:seg->audio_list;
+ ret = avio_open2(&seg->list_pb[cnt], list, AVIO_FLAG_WRITE,
+ &s->interrupt_callback, NULL);
+ if (ret < 0) {
+ av_log(s, AV_LOG_ERROR, "Failed to open segment list '%s'\n", list);
+ return ret;
+ }
+
+ if (seg->list_type == LIST_TYPE_M3U8 && seg->entry_wrapper[cnt].segment_list_entries) {
+ SegmentListEntry *entry;
+ double max_duration = 0;
+
+ avio_printf(seg->list_pb[cnt], "#EXTM3U\n");
+ avio_printf(seg->list_pb[cnt], "#EXT-X-VERSION:3\n");
+ avio_printf(seg->list_pb[cnt], "#EXT-X-MEDIA-SEQUENCE:%d\n", seg->entry_wrapper[cnt].segment_list_entries->index);
+ avio_printf(seg->list_pb[cnt], "#EXT-X-ALLOW-CACHE:%s\n",
+ seg->list_flags & SEGMENT_LIST_FLAG_CACHE ? "YES" : "NO");
+
+ av_log(s, AV_LOG_VERBOSE, "EXT-X-MEDIA-SEQUENCE:%d\n",
+ seg->entry_wrapper[cnt].segment_list_entries->index);
+
+ for (entry = seg->entry_wrapper[cnt].segment_list_entries; entry; entry = entry->next)
+ max_duration = FFMAX(max_duration, entry->end_time - entry->start_time);
+ avio_printf(seg->list_pb[cnt], "#EXT-X-TARGETDURATION:%"PRId64"\n", (int64_t)ceil(max_duration));
+ } else if (seg->list_type == LIST_TYPE_FFCONCAT) {
+ avio_printf(seg->list_pb[cnt], "ffconcat version 1.0\n");
+ }
}
return ret;
@@ -297,61 +380,82 @@ static void segment_list_print_entry(AVIOContext *list_ioctx,
static int segment_end(AVFormatContext *s, int write_trailer, int is_last)
{
SegmentContext *seg = s->priv_data;
- AVFormatContext *oc = seg->avf;
+ int iCount = seg->progressive?2:1;
int ret = 0;
-
- av_write_frame(oc, NULL); /* Flush any buffered data (fragmented mp4) */
- if (write_trailer)
- ret = av_write_trailer(oc);
-
- if (ret < 0)
- av_log(s, AV_LOG_ERROR, "Failure occurred when ending segment '%s'\n",
- oc->filename);
-
+ int cnt;
+ for (cnt = 0; cnt < iCount; ++cnt) {
+ if (seg->list) {
+ avio_close(seg->list_pb[cnt]);
+ }
+ }
+
if (seg->list) {
- if (seg->list_size || seg->list_type == LIST_TYPE_M3U8) {
- SegmentListEntry *entry = av_mallocz(sizeof(*entry));
- if (!entry) {
- ret = AVERROR(ENOMEM);
- goto end;
+ if ((ret = segment_list_open(s)) < 0) {
+ for (cnt = 0; cnt < iCount; ++cnt) {
+ AVFormatContext *oc = seg->avf[cnt];
+ avio_close(oc->pb);
}
-
- /* append new element */
- memcpy(entry, &seg->cur_entry, sizeof(*entry));
- if (!seg->segment_list_entries)
- seg->segment_list_entries = seg->segment_list_entries_end = entry;
- else
- seg->segment_list_entries_end->next = entry;
- seg->segment_list_entries_end = entry;
-
- /* drop first item */
- if (seg->list_size && seg->segment_count > seg->list_size) {
- entry = seg->segment_list_entries;
- seg->segment_list_entries = seg->segment_list_entries->next;
- av_free(entry->filename);
- av_freep(&entry);
+ }
+ }
+
+ for (cnt = 0; cnt < iCount; ++cnt) {
+ AVFormatContext *oc = seg->avf[cnt];
+
+ av_write_frame(oc, NULL); /* Flush any buffered data (fragmented mp4) */
+ if (write_trailer)
+ ret = av_write_trailer(oc);
+
+ if (ret < 0)
+ av_log(s, AV_LOG_ERROR, "Failure occurred when ending segment '%s'\n",
+ oc->filename);
+
+ if (seg->list) {
+ if (seg->list_size || seg->list_type == LIST_TYPE_M3U8) {
+ SegmentListEntry *entry = av_mallocz(sizeof(*entry));
+ if (!entry) {
+ ret = AVERROR(ENOMEM);
+ goto end;
+ }
+
+ /* append new element */
+ memcpy(entry, &seg->entry_wrapper[cnt].cur_entry, sizeof(*entry));
+ if (!seg->entry_wrapper[cnt].segment_list_entries) {
+ seg->entry_wrapper[cnt].segment_list_entries = seg->entry_wrapper[cnt].segment_list_entries_end = entry;
+ } else {
+ seg->entry_wrapper[cnt].segment_list_entries_end->next = entry;
+ }
+ seg->entry_wrapper[cnt].segment_list_entries_end = entry;
+
+ /* drop first item */
+ if (seg->list_size && seg->segment_count > seg->list_size) {
+ entry = seg->entry_wrapper[cnt].segment_list_entries;
+ seg->entry_wrapper[cnt].segment_list_entries = seg->entry_wrapper[cnt].segment_list_entries->next;
+ av_free(entry->filename);
+ av_freep(&entry);
+ }
+
+ for (entry = seg->entry_wrapper[cnt].segment_list_entries; entry; entry = entry->next)
+ segment_list_print_entry(seg->list_pb[cnt], seg->list_type, entry, s);
+ if (seg->list_type == LIST_TYPE_M3U8 && is_last)
+ avio_printf(seg->list_pb[cnt], "#EXT-X-ENDLIST\n");
+ } else {
+ segment_list_print_entry(seg->list_pb[cnt], seg->list_type, &seg->entry_wrapper[cnt].cur_entry, s);
}
-
- avio_close(seg->list_pb);
- if ((ret = segment_list_open(s)) < 0)
- goto end;
- for (entry = seg->segment_list_entries; entry; entry = entry->next)
- segment_list_print_entry(seg->list_pb, seg->list_type, entry, s);
- if (seg->list_type == LIST_TYPE_M3U8 && is_last)
- avio_printf(seg->list_pb, "#EXT-X-ENDLIST\n");
- } else {
- segment_list_print_entry(seg->list_pb, seg->list_type, &seg->cur_entry, s);
+ avio_flush(seg->list_pb[cnt]);
}
- avio_flush(seg->list_pb);
+
+ av_log(s, AV_LOG_VERBOSE, "segment:'%s' count:%d ended\n",
+ seg->avf[cnt]->filename, seg->segment_count);
+
+ end:
+ seg->stream_size[cnt] += avio_size(oc->pb);
+ avio_close(oc->pb);
}
-
- av_log(s, AV_LOG_VERBOSE, "segment:'%s' count:%d ended\n",
- seg->avf->filename, seg->segment_count);
seg->segment_count++;
-end:
- avio_close(oc->pb);
-
+ if ( is_last ) {
+ ret = root_m3u8_create(s);
+ }
return ret;
}
@@ -594,6 +698,8 @@ static int seg_write_header(AVFormatContext *s)
}
if ((ret = segment_list_open(s)) < 0)
goto fail;
+ seg->stream_size[0] = 0;
+ seg->stream_size[1] = 0;
}
if (seg->list_type == LIST_TYPE_EXT)
av_log(s, AV_LOG_WARNING, "'ext' list type option is deprecated in favor of 'csv'\n");
@@ -617,46 +723,61 @@ static int seg_write_header(AVFormatContext *s)
goto fail;
}
+
if ((ret = segment_mux_init(s)) < 0)
goto fail;
- oc = seg->avf;
-
+
if ((ret = set_segment_filename(s)) < 0)
goto fail;
- if (seg->write_header_trailer) {
- if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
- &s->interrupt_callback, NULL)) < 0) {
- av_log(s, AV_LOG_ERROR, "Failed to open segment '%s'\n", oc->filename);
- goto fail;
+ seg->segment_idx++;
+ seg->is_first_pkt = 1;
+
+ int iCount = seg->progressive?2:1;
+ int cnt;
+ for (cnt = 0; cnt < iCount; ++cnt) {
+ oc = seg->avf[cnt];
+
+ if (seg->write_header_trailer) {
+ if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
+ &s->interrupt_callback, NULL)) < 0) {
+ av_log(s, AV_LOG_ERROR, "Failed to open segment '%s'\n", oc->filename);
+ goto fail;
+ }
+ } else {
+ if ((ret = open_null_ctx(&oc->pb)) < 0)
+ goto fail;
}
- } else {
- if ((ret = open_null_ctx(&oc->pb)) < 0)
+
+ if ((ret = avformat_write_header(oc, NULL)) < 0) {
+ avio_close(oc->pb);
goto fail;
- }
-
- if ((ret = avformat_write_header(oc, NULL)) < 0) {
- avio_close(oc->pb);
- goto fail;
- }
- seg->is_first_pkt = 1;
+ }
- if (oc->avoid_negative_ts > 0 && s->avoid_negative_ts < 0)
- s->avoid_negative_ts = 1;
+ if (!seg->write_header_trailer) {
+ close_null_ctx(oc->pb);
+ if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
+ &s->interrupt_callback, NULL)) < 0)
+ goto fail;
+ }
- if (!seg->write_header_trailer) {
- close_null_ctx(oc->pb);
- if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
- &s->interrupt_callback, NULL)) < 0)
- goto fail;
+ if (oc->avoid_negative_ts > 0 && s->avoid_negative_ts < 0)
+ s->avoid_negative_ts = 1;
}
+
fail:
if (ret) {
- if (seg->list)
- avio_close(seg->list_pb);
- if (seg->avf)
- avformat_free_context(seg->avf);
+ if (seg->list) {
+ if (seg->list_pb[0])
+ avio_close(seg->list_pb[0]);
+ if (seg->list_pb[1])
+ avio_close(seg->list_pb[1]);
+ }
+ if (seg->avf[0])
+ avformat_free_context(seg->avf[0]);
+ if (seg->avf[1])
+ avformat_free_context(seg->avf[1]);
}
return ret;
}
@@ -668,6 +789,8 @@ static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
int64_t end_pts = INT64_MAX, offset;
int start_frame = INT_MAX;
int ret;
+ int iCount = seg->progressive?2:1;
+ int cnt;
if (seg->times) {
end_pts = seg->segment_count < seg->nb_times ?
@@ -696,29 +819,37 @@ static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
if ((ret = segment_start(s, seg->individual_header_trailer)) < 0)
goto fail;
- seg->cur_entry.index = seg->segment_idx + seg->segment_idx_wrap*seg->segment_idx_wrap_nb;
- seg->cur_entry.start_time = (double)pkt->pts * av_q2d(st->time_base);
- seg->cur_entry.start_pts = av_rescale_q(pkt->pts, st->time_base, AV_TIME_BASE_Q);
+ for (cnt = 0; cnt < iCount; ++cnt) {
+ seg->entry_wrapper[cnt].cur_entry.index = seg->segment_idx + seg->segment_idx_wrap*seg->segment_idx_wrap_nb;
+ seg->entry_wrapper[cnt].cur_entry.start_time = (double)pkt->pts * av_q2d(st->time_base);
+ seg->entry_wrapper[cnt].cur_entry.start_pts = av_rescale_q(pkt->pts, st->time_base, AV_TIME_BASE_Q);
+ }
} else if (pkt->pts != AV_NOPTS_VALUE) {
- seg->cur_entry.end_time =
- FFMAX(seg->cur_entry.end_time, (double)(pkt->pts + pkt->duration) * av_q2d(st->time_base));
+ for (cnt = 0; cnt < iCount; ++cnt) {
+ seg->entry_wrapper[cnt].cur_entry.end_time =
+ FFMAX(seg->entry_wrapper[cnt].cur_entry.end_time, (double)(pkt->pts + pkt->duration) * av_q2d(st->time_base));
+ }
}
if (seg->is_first_pkt) {
- av_log(s, AV_LOG_VERBOSE, "segment:'%s' starts with packet stream:%d pts:%s pts_time:%s frame:%d\n",
- seg->avf->filename, pkt->stream_index,
- av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base), seg->frame_count);
+ for (cnt = 0; cnt < iCount; ++cnt) {
+ av_log(s, AV_LOG_VERBOSE, "segment:'%s' starts with packet stream:%d pts:%s pts_time:%s frame:%d\n",
+ seg->avf[cnt]->filename, pkt->stream_index,
+ av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base), seg->frame_count);
+ }
seg->is_first_pkt = 0;
}
- av_log(s, AV_LOG_DEBUG, "stream:%d start_pts_time:%s pts:%s pts_time:%s dts:%s dts_time:%s",
- pkt->stream_index,
- av_ts2timestr(seg->cur_entry.start_pts, &AV_TIME_BASE_Q),
- av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base),
- av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &st->time_base));
+ for (cnt = 0; cnt < iCount; ++cnt) {
+ av_log(s, AV_LOG_DEBUG, "stream:%d start_pts_time:%s pts:%s pts_time:%s dts:%s dts_time:%s",
+ pkt->stream_index,
+ av_ts2timestr(seg->entry_wrapper[cnt].cur_entry.start_pts, &AV_TIME_BASE_Q),
+ av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base),
+ av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &st->time_base));
+ }
/* compute new timestamps */
- offset = av_rescale_q(seg->initial_offset - (seg->reset_timestamps ? seg->cur_entry.start_pts : 0),
+ offset = av_rescale_q(seg->initial_offset - (seg->reset_timestamps ? seg->entry_wrapper[0].cur_entry.start_pts : 0),
AV_TIME_BASE_Q, st->time_base);
if (pkt->pts != AV_NOPTS_VALUE)
pkt->pts += offset;
@@ -729,8 +860,16 @@ static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base),
av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &st->time_base));
- ret = ff_write_chained(seg->avf, pkt->stream_index, pkt, s);
-
+ int stream_idx = pkt->stream_index;
+ for (cnt = 0; cnt < iCount; ++cnt) {
+ if (cnt == SEGMENT_ENTRY_AUDIO_ONLY && st->codec->codec_type!=AVMEDIA_TYPE_AUDIO) {
+ continue;
+ } else if (cnt == SEGMENT_ENTRY_AUDIO_ONLY) {
+ pkt->stream_index = 0;
+ }
+ ret = ff_write_chained(seg->avf[cnt], pkt->stream_index, pkt, s);
+ pkt->stream_index = stream_idx;
+ }
fail:
if (pkt->stream_index == seg->reference_stream_index)
seg->frame_count++;
@@ -741,36 +880,43 @@ fail:
static int seg_write_trailer(struct AVFormatContext *s)
{
SegmentContext *seg = s->priv_data;
- AVFormatContext *oc = seg->avf;
- SegmentListEntry *cur, *next;
-
- int ret;
- if (!seg->write_header_trailer) {
- if ((ret = segment_end(s, 0, 1)) < 0)
- goto fail;
- open_null_ctx(&oc->pb);
- ret = av_write_trailer(oc);
- close_null_ctx(oc->pb);
- } else {
- ret = segment_end(s, 1, 1);
+ int iCount = seg->progressive?2:1;
+ int ret = -1;
+ if ((ret = segment_end(s, seg->write_header_trailer?1:0, 1)) < 0)
+ goto fail;
+ for (int cnt = 0; cnt < iCount; ++cnt) {
+ AVFormatContext *oc = seg->avf[cnt];
+ if (!seg->write_header_trailer) {
+ open_null_ctx(&oc->pb);
+ ret = av_write_trailer(oc);
+ close_null_ctx(oc->pb);
+ }
}
fail:
- if (seg->list)
- avio_close(seg->list_pb);
-
+ if (seg->list) {
+ if (seg->list_pb[0])
+ avio_close(seg->list_pb[0]);
+ if (seg->list_pb[1])
+ avio_close(seg->list_pb[1]);
+ }
+
av_opt_free(seg);
av_freep(&seg->times);
av_freep(&seg->frames);
-
- cur = seg->segment_list_entries;
- while (cur) {
- next = cur->next;
- av_free(cur->filename);
- av_free(cur);
- cur = next;
+
+ for (int cnt = 0; cnt < iCount; ++cnt) {
+ SegmentListEntry *cur, *next;
+ AVFormatContext *oc = seg->avf[cnt];
+ cur = seg->entry_wrapper[cnt].segment_list_entries;
+ while (cur) {
+ next = cur->next;
+ av_free(cur->filename);
+ av_free(cur);
+ cur = next;
+ }
+
+ avformat_free_context(oc);
}
-
- avformat_free_context(oc);
return ret;
}
@@ -780,6 +926,7 @@ static const AVOption options[] = {
{ "reference_stream", "set reference stream", OFFSET(reference_stream_specifier), AV_OPT_TYPE_STRING, {.str = "auto"}, CHAR_MIN, CHAR_MAX, E },
{ "segment_format", "set container format used for the segments", OFFSET(format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
{ "segment_list", "set the segment list filename", OFFSET(list), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
+ { "a_segment_list", "set the audio only segment list filename", OFFSET(audio_list), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
{ "segment_list_flags","set flags affecting segment list generation", OFFSET(list_flags), AV_OPT_TYPE_FLAGS, {.i64 = SEGMENT_LIST_FLAG_CACHE }, 0, UINT_MAX, E, "list_flags"},
{ "cache", "allow list caching", 0, AV_OPT_TYPE_CONST, {.i64 = SEGMENT_LIST_FLAG_CACHE }, INT_MIN, INT_MAX, E, "list_flags"},
@@ -808,6 +955,10 @@ static const AVOption options[] = {
{ "write_header_trailer", "write a header to the first segment and a trailer to the last one", OFFSET(write_header_trailer), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, E },
{ "reset_timestamps", "reset timestamps at the begin of each segment", OFFSET(reset_timestamps), AV_OPT_TYPE_INT, {.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 },
+ { "progressive_hls", "set progressive hls support", OFFSET(progressive), AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, E },
+ { "root_m3u8", "root m3u8 file", OFFSET(root_m3u8), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
+ { "vc_tag", "video codec tag", OFFSET(vc_tag), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
+ { "ac_tag", "audio codec tag", OFFSET(ac_tag), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
{ NULL },
};
--
1.7.9.5
More information about the ffmpeg-devel
mailing list