From fe896ec2f9c416da3313e605c67458ee3773d639 Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Fri, 16 Sep 2011 19:54:03 +0200 Subject: [PATCH 1/5] ratecontrol: estimate twice the qscale values This had been the fix used by Michael Niedermayer to solve the ratecontrol convergence issue. --- libavcodec/ratecontrol.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libavcodec/ratecontrol.c b/libavcodec/ratecontrol.c index e984ed5b29..64dab153f6 100644 --- a/libavcodec/ratecontrol.c +++ b/libavcodec/ratecontrol.c @@ -862,8 +862,8 @@ static int init_pass2(MpegEncContext *s) /* find qscale */ for(i=0; inum_entries; i++){ RateControlEntry *rce= &rcc->entry[i]; - qscale[i]= get_qscale(s, &rcc->entry[i], rate_factor, i); - rcc->last_qscale_for[rce->pict_type] = qscale[i]; + double q = get_qscale(s, &rcc->entry[i], rate_factor, i); + qscale[i] = get_diff_limited_q(s, rce, q); } assert(filter_size%2==1); From 8d80f3cb877b890889b1673029387229648901ed Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Sun, 2 Oct 2011 23:05:07 +0200 Subject: [PATCH 2/5] output-example: extend duration --- libavformat/output-example.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libavformat/output-example.c b/libavformat/output-example.c index 3b28b7c7c6..cb90f60f5d 100644 --- a/libavformat/output-example.c +++ b/libavformat/output-example.c @@ -41,7 +41,7 @@ #undef exit /* 5 seconds stream duration */ -#define STREAM_DURATION 5.0 +#define STREAM_DURATION 200.0 #define STREAM_FRAME_RATE 25 /* 25 images/s */ #define STREAM_NB_FRAMES ((int)(STREAM_DURATION * STREAM_FRAME_RATE)) #define STREAM_PIX_FMT PIX_FMT_YUV420P /* default pix_fmt */ From 82df8c39975e7a719b06a91f248b1b2a07244101 Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Sun, 2 Oct 2011 23:05:29 +0200 Subject: [PATCH 3/5] segment: introduce segmented chain muxer Initial test code, the filename isn't yet guessed and the file index isn't printed yet. --- libavformat/Makefile | 1 + libavformat/allformats.c | 1 + libavformat/segment.c | 220 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 222 insertions(+) create mode 100644 libavformat/segment.c diff --git a/libavformat/Makefile b/libavformat/Makefile index 0a30c6ec1a..ff20f7f695 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 8e89b2f4ff..72b4a985b7 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 0000000000..624ce6fce6 --- /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 +#include + +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 From 1786c7f844ee16462f61588d1dcaab6d7a2e6eb9 Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Mon, 10 Oct 2011 18:32:24 +0200 Subject: [PATCH 4/5] segment: basic pattern support and playlist output Yet another option half implemented, more will follow... --- libavformat/segment.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/libavformat/segment.c b/libavformat/segment.c index 624ce6fce6..41aceee826 100644 --- a/libavformat/segment.c +++ b/libavformat/segment.c @@ -35,6 +35,7 @@ typedef struct { char path[1024]; AVFormatContext *avf; char *format; /**< Set by a private option. */ + char *pattern; /**< Set by a private option. */ float time; /**< Set by a private option. */ int64_t offset_time; int64_t recording_time; @@ -49,7 +50,8 @@ static int segment_header(SegmentContext *s) av_strlcpy(oc->filename, s->path, sizeof(oc->filename)); - av_strlcatf(oc->filename, sizeof(oc->filename), "%03d", s->number++); + av_strlcatf(oc->filename, sizeof(oc->filename), + s->pattern, s->number++); if ((err = avio_open(&oc->pb, oc->filename, AVIO_FLAG_WRITE)) < 0) { return err; @@ -116,7 +118,8 @@ static int seg_write_header(AVFormatContext *s) av_strlcpy(oc->filename, seg->path, sizeof(oc->filename)); - av_strlcatf(oc->filename, sizeof(oc->filename), "%2d", seg->number++); + 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); @@ -130,6 +133,9 @@ static int seg_write_header(AVFormatContext *s) if (ret) avformat_free_context(oc); + avio_printf(s->pb, "%s\n", oc->filename); + avio_flush(s->pb); + return ret; } @@ -156,7 +162,8 @@ static int seg_write_packet(AVFormatContext *s, AVPacket *pkt) avformat_free_context(oc); return ret; } - + avio_printf(s->pb, "%s\n", oc->filename); + avio_flush(s->pb); } ret = oc->oformat->write_packet(oc, pkt); @@ -178,7 +185,9 @@ static int seg_write_trailer(struct AVFormatContext *s) #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 }, + { "segment_time", "segment lenght in seconds", OFFSET(time), FF_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E }, + { "segment_pattern", "pattern to use in segment files", OFFSET(pattern),FF_OPT_TYPE_STRING, {.str = "%03d"}, 0, 0, E }, + { NULL }, }; From 13c99587511bfc35acf3b9427ef4d48ae5aec312 Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Thu, 13 Oct 2011 11:51:07 +0200 Subject: [PATCH 5/5] segment: extend options let set an alternate path for the segment files. --- libavformat/segment.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/libavformat/segment.c b/libavformat/segment.c index 41aceee826..91905cf35f 100644 --- a/libavformat/segment.c +++ b/libavformat/segment.c @@ -32,10 +32,10 @@ typedef struct { const AVClass *class; /**< Class for private options. */ int number; - char path[1024]; AVFormatContext *avf; char *format; /**< Set by a private option. */ char *pattern; /**< Set by a private option. */ + char *path; /**< Set by a private option. */ float time; /**< Set by a private option. */ int64_t offset_time; int64_t recording_time; @@ -96,7 +96,12 @@ static int seg_write_header(AVFormatContext *s) seg->recording_time = seg->time*1000000; seg->offset_time = 0; - av_strlcpy(seg->path, "test", sizeof("test")); + if (!seg->path) { + char *t; + seg->path = strdup(s->filename); + t = rindex(seg->path, '.'); + if (t) t = '\0'; + } oc = avformat_alloc_context(); @@ -184,10 +189,10 @@ static int seg_write_trailer(struct AVFormatContext *s) #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 }, + { "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 }, { "segment_pattern", "pattern to use in segment files", OFFSET(pattern),FF_OPT_TYPE_STRING, {.str = "%03d"}, 0, 0, E }, - + { "segment_basename", "basename to use in segment files", OFFSET(pattern),FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E }, { NULL }, };