summaryrefslogtreecommitdiffstats
path: root/libavformat
diff options
context:
space:
mode:
authorLuca Barbato <lu_zero@gentoo.org>2011-10-02 23:05:29 +0200
committerLuca Barbato <lu_zero@gentoo.org>2011-10-12 17:03:52 +0200
commit82df8c39975e7a719b06a91f248b1b2a07244101 (patch)
tree7e2fe5c238d61493902d31d8130d9458643f7577 /libavformat
parent8d80f3cb877b890889b1673029387229648901ed (diff)
downloadffmpeg-streaming-82df8c39975e7a719b06a91f248b1b2a07244101.zip
ffmpeg-streaming-82df8c39975e7a719b06a91f248b1b2a07244101.tar.gz
segment: introduce segmented chain muxer
Initial test code, the filename isn't yet guessed and the file index isn't printed yet.
Diffstat (limited to 'libavformat')
-rw-r--r--libavformat/Makefile1
-rw-r--r--libavformat/allformats.c1
-rw-r--r--libavformat/segment.c220
3 files changed, 222 insertions, 0 deletions
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 0a30c6e..ff20f7f 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -260,6 +260,7 @@ OBJS-$(CONFIG_SAP_DEMUXER) += sapdec.o
OBJS-$(CONFIG_SAP_MUXER) += sapenc.o rtpenc_chain.o
OBJS-$(CONFIG_SDP_DEMUXER) += rtsp.o
OBJS-$(CONFIG_SEGAFILM_DEMUXER) += segafilm.o
+OBJS-$(CONFIG_SEGMENT_MUXER) += segment.o
OBJS-$(CONFIG_SHORTEN_DEMUXER) += rawdec.o
OBJS-$(CONFIG_SIFF_DEMUXER) += siff.o
OBJS-$(CONFIG_SMACKER_DEMUXER) += smacker.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 8e89b2f..72b4a98 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -194,6 +194,7 @@ void av_register_all(void)
av_register_rdt_dynamic_payload_handlers();
#endif
REGISTER_DEMUXER (SEGAFILM, segafilm);
+ REGISTER_MUXER (SEGMENT, segment);
REGISTER_DEMUXER (SHORTEN, shorten);
REGISTER_DEMUXER (SIFF, siff);
REGISTER_DEMUXER (SMACKER, smacker);
diff --git a/libavformat/segment.c b/libavformat/segment.c
new file mode 100644
index 0000000..624ce6f
--- /dev/null
+++ b/libavformat/segment.c
@@ -0,0 +1,220 @@
+/*
+ * Generic Segmenter
+ * Copyright (c) 2011, Luca Barbato
+ *
+ * This file is part of Libav.
+ *
+ * Libav is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * Libav is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Libav; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavutil/avstring.h"
+#include "libavutil/log.h"
+#include "libavutil/opt.h"
+#include "libavutil/mathematics.h"
+#include "libavutil/parseutils.h"
+#include "avformat.h"
+#include "internal.h"
+#include <strings.h>
+#include <float.h>
+
+typedef struct {
+ const AVClass *class; /**< Class for private options. */
+ int number;
+ char path[1024];
+ AVFormatContext *avf;
+ char *format; /**< Set by a private option. */
+ float time; /**< Set by a private option. */
+ int64_t offset_time;
+ int64_t recording_time;
+} SegmentContext;
+
+#if CONFIG_SEGMENT_MUXER
+
+static int segment_header(SegmentContext *s)
+{
+ AVFormatContext *oc = s->avf;
+ int err = 0;
+
+ av_strlcpy(oc->filename, s->path, sizeof(oc->filename));
+
+ av_strlcatf(oc->filename, sizeof(oc->filename), "%03d", s->number++);
+
+ if ((err = avio_open(&oc->pb, oc->filename, AVIO_FLAG_WRITE)) < 0) {
+ return err;
+ }
+
+ if (!oc->priv_data && oc->oformat->priv_data_size > 0) {
+ oc->priv_data = av_mallocz(oc->oformat->priv_data_size);
+ if (!oc->priv_data) {
+ avio_close(oc->pb);
+ return AVERROR(ENOMEM);
+ }
+ }
+
+ if ((err = oc->oformat->write_header(oc)) < 0) {
+ avio_close(oc->pb);
+ av_freep(&oc->priv_data);
+ }
+
+ return err;
+}
+
+static int segment_trailer(AVFormatContext *oc)
+{
+ int ret = 0;
+
+ if(oc->oformat->write_trailer)
+ ret = oc->oformat->write_trailer(oc);
+
+ avio_close(oc->pb);
+ av_freep(&oc->priv_data);
+
+ return ret;
+}
+
+static int seg_write_header(AVFormatContext *s)
+{
+ SegmentContext *seg = s->priv_data;
+ AVFormatContext *oc;
+ int ret;
+
+ seg->number = 0;
+ seg->recording_time = seg->time*1000000;
+ seg->offset_time = 0;
+
+ av_strlcpy(seg->path, "test", sizeof("test"));
+
+ oc = avformat_alloc_context();
+
+ if (!oc) {
+ return AVERROR(ENOMEM);
+ }
+
+ oc->oformat = av_guess_format(seg->format, NULL, NULL);
+
+ if (!oc->oformat) {
+ avformat_free_context(oc);
+ return AVERROR_MUXER_NOT_FOUND;
+ }
+
+ 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), "%2d", seg->number++);
+
+ if ((ret = avio_open(&oc->pb, oc->filename, AVIO_FLAG_WRITE)) < 0) {
+ avformat_free_context(oc);
+ return ret;
+ }
+
+ if ((ret = avformat_write_header(oc, NULL)) < 0) {
+ avio_close(oc->pb);
+ }
+
+ if (ret)
+ avformat_free_context(oc);
+
+ return ret;
+}
+
+static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
+{
+ SegmentContext *seg = s->priv_data;
+ AVFormatContext *oc = seg->avf;
+ AVStream *st = oc->streams[pkt->stream_index];
+ int ret;
+
+ if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
+ av_compare_ts(pkt->pts, st->time_base,
+ seg->recording_time*seg->number,
+ (AVRational){1, 1000000}) >= 0 &&
+ pkt->flags & AV_PKT_FLAG_KEY) {
+ av_log(s, AV_LOG_INFO, "I'd reset at %d %"PRId64"\n",
+ pkt->stream_index, pkt->pts);
+
+ ret = segment_trailer(oc);
+ if (!ret)
+ ret = segment_header(seg);
+
+ if (ret) {
+ avformat_free_context(oc);
+ return ret;
+ }
+
+ }
+
+ ret = oc->oformat->write_packet(oc, pkt);
+
+ return ret;
+}
+
+static int seg_write_trailer(struct AVFormatContext *s)
+{
+ SegmentContext *seg = s->priv_data;
+ AVFormatContext *oc = seg->avf;
+
+ return segment_trailer(oc);
+}
+
+#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), FF_OPT_TYPE_STRING, {.str = "nut"}, 0, 0, E },
+ { "segment_time", "segment lenght in seconds", OFFSET(time), FF_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E },
+ { NULL },
+};
+
+static const AVClass seg_class = {
+ .class_name = "segment muxer",
+ .item_name = av_default_item_name,
+ .option = options,
+ .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,
+ .write_header = seg_write_header,
+ .write_packet = seg_write_packet,
+ .write_trailer = seg_write_trailer,
+ .priv_class = &seg_class,
+};
+#endif
OpenPOWER on IntegriCloud