/*
- * Generic Segmenter
+ * Generic segmenter
* Copyright (c) 2011, Luca Barbato
*
* This file is part of Libav.
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
-#include "libavutil/avstring.h"
+#include <strings.h>
+#include <float.h>
+
+#include "avformat.h"
+#include "internal.h"
+
#include "libavutil/log.h"
#include "libavutil/opt.h"
-#include "libavutil/mathematics.h"
+#include "libavutil/avstring.h"
#include "libavutil/parseutils.h"
-#include "avformat.h"
-#include "internal.h"
-#include <strings.h>
-#include <float.h>
+#include "libavutil/mathematics.h"
typedef struct {
const AVClass *class; /**< Class for private options. */
int number;
AVFormatContext *avf;
char *format; /**< Set by a private option. */
- char *pattern; /**< Set by a private option. */
- char *path; /**< Set by a private option. */
+ char *list; /**< Set by a private option. */
float time; /**< Set by a private option. */
+ int size; /**< Set by a private option. */
int64_t offset_time;
int64_t recording_time;
+ int has_video;
+ AVIOContext *pb;
} SegmentContext;
-#if CONFIG_SEGMENT_MUXER
-
-static int segment_header(SegmentContext *s)
+static int segment_start(AVFormatContext *s)
{
- AVFormatContext *oc = s->avf;
+ SegmentContext *c = s->priv_data;
+ AVFormatContext *oc = c->avf;
int err = 0;
- av_strlcpy(oc->filename, s->path, sizeof(oc->filename));
+ if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
+ s->filename, c->number++) < 0)
+ return AVERROR(EINVAL);
- av_strlcatf(oc->filename, sizeof(oc->filename),
- s->pattern, s->number++);
-
- if ((err = avio_open(&oc->pb, oc->filename, AVIO_FLAG_WRITE)) < 0) {
+ if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
+ &s->interrupt_callback, NULL)) < 0)
return err;
- }
if (!oc->priv_data && oc->oformat->priv_data_size > 0) {
oc->priv_data = av_mallocz(oc->oformat->priv_data_size);
avio_close(oc->pb);
return AVERROR(ENOMEM);
}
+ if (oc->oformat->priv_class) {
+ *(const AVClass**)oc->priv_data = oc->oformat->priv_class;
+ av_opt_set_defaults(oc->priv_data);
+ }
}
if ((err = oc->oformat->write_header(oc)) < 0) {
- avio_close(oc->pb);
- av_freep(&oc->priv_data);
+ goto fail;
}
+ return 0;
+
+fail:
+ avio_close(oc->pb);
+ av_freep(&oc->priv_data);
+
return err;
}
-static int segment_trailer(AVFormatContext *oc)
+static int segment_end(AVFormatContext *oc)
{
int ret = 0;
- if(oc->oformat->write_trailer)
+ if (oc->oformat->write_trailer)
ret = oc->oformat->write_trailer(oc);
avio_close(oc->pb);
+ if (oc->oformat->priv_class)
+ av_opt_free(oc->priv_data);
av_freep(&oc->priv_data);
return ret;
{
SegmentContext *seg = s->priv_data;
AVFormatContext *oc;
- int ret;
+ int ret, i;
seg->number = 0;
- seg->recording_time = seg->time*1000000;
seg->offset_time = 0;
+ seg->recording_time = seg->time * 1000000;
- if (!seg->path) {
- char *t;
- seg->path = av_strdup(s->filename);
- t = strrchr(seg->path, '.');
- if (t) *t = '\0';
- }
+ if (seg->list)
+ if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
+ &s->interrupt_callback, NULL)) < 0)
+ return ret;
+
+ for (i = 0; i< s->nb_streams; i++)
+ seg->has_video +=
+ (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO);
+
+ if (seg->has_video > 1)
+ av_log(s, AV_LOG_WARNING,
+ "More than a single video stream present, "
+ "expect issues decoding it.\n");
oc = avformat_alloc_context();
if (!oc) {
- return AVERROR(ENOMEM);
+ ret = AVERROR(ENOMEM);
+ goto fail;
}
- oc->oformat = av_guess_format(seg->format, NULL, NULL);
+ oc->oformat = av_guess_format(seg->format, s->filename, NULL);
if (!oc->oformat) {
- avformat_free_context(oc);
- return AVERROR_MUXER_NOT_FOUND;
+ ret = AVERROR_MUXER_NOT_FOUND;
+ goto fail;
+ }
+ if (oc->oformat->flags & AVFMT_NOFILE) {
+ av_log(s, AV_LOG_ERROR, "format %s not supported.\n",
+ oc->oformat->name);
+ ret = AVERROR(EINVAL);
+ goto fail;
}
seg->avf = oc;
oc->streams = s->streams;
oc->nb_streams = s->nb_streams;
- av_strlcpy(oc->filename, seg->path, sizeof(oc->filename));
-
- av_strlcatf(oc->filename, sizeof(oc->filename),
- seg->pattern, seg->number++);
-
- if ((ret = avio_open(&oc->pb, oc->filename, AVIO_FLAG_WRITE)) < 0) {
- avformat_free_context(oc);
- return ret;
+ if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
+ s->filename, seg->number++) < 0) {
+ ret = AVERROR(EINVAL);
+ goto fail;
}
+ if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
+ &s->interrupt_callback, NULL)) < 0)
+ goto fail;
+
if ((ret = avformat_write_header(oc, NULL)) < 0) {
avio_close(oc->pb);
+ goto fail;
}
- if (ret)
- avformat_free_context(oc);
-
- avio_printf(s->pb, "%s\n", oc->filename);
- avio_flush(s->pb);
+ if (seg->list) {
+ avio_printf(seg->pb, "%s\n", oc->filename);
+ avio_flush(seg->pb);
+ }
+fail:
+ if (ret) {
+ oc->streams = NULL;
+ oc->nb_streams = 0;
+ if (seg->list)
+ avio_close(seg->pb);
+ avformat_free_context(oc);
+ }
return ret;
}
SegmentContext *seg = s->priv_data;
AVFormatContext *oc = seg->avf;
AVStream *st = oc->streams[pkt->stream_index];
+ int64_t end_pts = seg->recording_time * seg->number;
int ret;
- if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
+ if ((seg->has_video && st->codec->codec_type == AVMEDIA_TYPE_VIDEO) &&
av_compare_ts(pkt->pts, st->time_base,
- seg->recording_time*seg->number,
- (AVRational){1, 1000000}) >= 0 &&
+ end_pts, AV_TIME_BASE_Q) >= 0 &&
pkt->flags & AV_PKT_FLAG_KEY) {
- av_log(s, AV_LOG_INFO, "I'd reset at %d %"PRId64"\n",
+
+ av_log(s, AV_LOG_DEBUG, "Next segment starts at %d %"PRId64"\n",
pkt->stream_index, pkt->pts);
- ret = segment_trailer(oc);
+ ret = segment_end(oc);
+
if (!ret)
- ret = segment_header(seg);
+ ret = segment_start(s);
- if (ret) {
- avformat_free_context(oc);
- return ret;
+ if (ret)
+ goto fail;
+
+ if (seg->list) {
+ avio_printf(seg->pb, "%s\n", oc->filename);
+ avio_flush(seg->pb);
+ if (seg->size && !(seg->number % seg->size)) {
+ avio_close(seg->pb);
+ if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
+ &s->interrupt_callback, NULL)) < 0)
+ goto fail;
+
+ }
}
- avio_printf(s->pb, "%s\n", oc->filename);
- avio_flush(s->pb);
}
ret = oc->oformat->write_packet(oc, pkt);
+fail:
+ if (ret < 0) {
+ oc->streams = NULL;
+ oc->nb_streams = 0;
+ if (seg->list)
+ avio_close(seg->pb);
+ avformat_free_context(oc);
+ }
+
return ret;
}
{
SegmentContext *seg = s->priv_data;
AVFormatContext *oc = seg->avf;
-
- return segment_trailer(oc);
+ int ret = segment_end(oc);
+ if (seg->list)
+ avio_close(seg->pb);
+ oc->streams = NULL;
+ oc->nb_streams = 0;
+ avformat_free_context(oc);
+ return ret;
}
-#endif /* CONFIG_SEGMENT_MUXER */
-
#define OFFSET(x) offsetof(SegmentContext, x)
#define E AV_OPT_FLAG_ENCODING_PARAM
static const AVOption options[] = {
- { "container_format", "container format used for the segments", OFFSET(format), AV_OPT_TYPE_STRING, {.str = "nut"}, 0, 0, E },
- { "segment_time", "segment lenght in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E },
- { "segment_pattern", "pattern to use in segment files", OFFSET(pattern),AV_OPT_TYPE_STRING, {.str = "%03d"}, 0, 0, E },
- { "segment_basename", "basename to use in segment files", OFFSET(path ),AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
+ { "segment_format", "container format used for the segments", OFFSET(format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
+ { "segment_time", "segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E },
+ { "segment_list", "output the segment list", OFFSET(list), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
+ { "segment_list_size", "maximum number of playlist entries", OFFSET(size), AV_OPT_TYPE_INT, {.dbl = 5}, 0, INT_MAX, E },
{ NULL },
};
.version = LIBAVUTIL_VERSION_INT,
};
-/* input
-#if CONFIG_IMAGE2_DEMUXER
-AVInputFormat ff_image2_demuxer = {
- .name = "image2",
- .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
- .priv_data_size = sizeof(VideoData),
- .read_probe = read_probe,
- .read_header = read_header,
- .read_packet = read_packet,
- .flags = AVFMT_NOFILE,
- .priv_class = &img2_class,
-};
-#endif
-*/
-/* output */
-#if CONFIG_SEGMENT_MUXER
AVOutputFormat ff_segment_muxer = {
.name = "segment",
.long_name = NULL_IF_CONFIG_SMALL("segment muxer"),
- .extensions = "m3u8",
.priv_data_size = sizeof(SegmentContext),
- .flags = AVFMT_GLOBALHEADER,
+ .flags = AVFMT_GLOBALHEADER | AVFMT_NOFILE,
.write_header = seg_write_header,
.write_packet = seg_write_packet,
.write_trailer = seg_write_trailer,
.priv_class = &seg_class,
};
-#endif