diff options
author | Michael Niedermayer <michaelni@gmx.at> | 2011-12-20 01:54:41 +0100 |
---|---|---|
committer | Michael Niedermayer <michaelni@gmx.at> | 2011-12-20 04:12:09 +0100 |
commit | 0edf7ebcd6703e3eb3b12bf0818796574d661ae3 (patch) | |
tree | 3d0794954cf00af13f0e10a3cfe802264287700a /libavformat | |
parent | c8a441efe24ceb0446a09e82d648a5f4e76d264d (diff) | |
parent | 729ebb2f185244b0ff06d48edbbbbb02ceb4ed4e (diff) | |
download | ffmpeg-0edf7ebcd6703e3eb3b12bf0818796574d661ae3.tar.gz |
Merge remote-tracking branch 'qatar/master'
* qatar/master:
h264: clear trailing bits in partially parsed NAL units
vc1: Handle WVC1 interlaced stream
xl: Fix overreads
mpegts: rename payload_index to payload_size
segment: introduce segmented chain muxer
lavu: add AVERROR_BUG error value
avplay: clear pkt_temp when pkt is freed.
qcelpdec: K&R formatting cosmetics
qcelpdec: cosmetics: drop some pointless parentheses
x86: conditionally compile dnxhd encoder optimizations
Revert "h264: skip start code search if the size of the nal unit is known"
swscale: fix formatting and indentation of unscaled conversion routines.
h264: skip start code search if the size of the nal unit is known
cljr: fix buf_size sanity check
cljr: Check if width and height are positive integers
Conflicts:
libavcodec/cljr.c
libavcodec/vc1dec.c
libavformat/Makefile
libavformat/mpegtsenc.c
libavformat/segment.c
libswscale/swscale_unscaled.c
Merged-by: Michael Niedermayer <michaelni@gmx.at>
Diffstat (limited to 'libavformat')
-rw-r--r-- | libavformat/Makefile | 2 | ||||
-rw-r--r-- | libavformat/mpegtsenc.c | 20 | ||||
-rw-r--r-- | libavformat/segment.c | 211 |
3 files changed, 136 insertions, 97 deletions
diff --git a/libavformat/Makefile b/libavformat/Makefile index 155e389b26..7c02398d35 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -285,7 +285,7 @@ OBJS-$(CONFIG_SAP_MUXER) += sapenc.o rtpenc_chain.o OBJS-$(CONFIG_SBG_DEMUXER) += sbgdec.o OBJS-$(CONFIG_SDP_DEMUXER) += rtsp.o OBJS-$(CONFIG_SEGAFILM_DEMUXER) += segafilm.o -OBJS-$(CONFIG_SEGMENT_MUXER) += segment.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/mpegtsenc.c b/libavformat/mpegtsenc.c index 3ace64bdd5..29b679a948 100644 --- a/libavformat/mpegtsenc.c +++ b/libavformat/mpegtsenc.c @@ -206,7 +206,7 @@ typedef struct MpegTSWriteStream { struct MpegTSService *service; int pid; /* stream associated pid */ int cc; - int payload_index; + int payload_size; int first_pts_check; ///< first pts check needed int64_t payload_pts; int64_t payload_dts; @@ -1034,29 +1034,29 @@ static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt) } } - if (ts_st->payload_index && ts_st->payload_index + size > DEFAULT_PES_PAYLOAD_SIZE) { - mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_index, + if (ts_st->payload_size && ts_st->payload_size + size > DEFAULT_PES_PAYLOAD_SIZE) { + mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size, ts_st->payload_pts, ts_st->payload_dts, ts_st->payload_flags & AV_PKT_FLAG_KEY); - ts_st->payload_index = 0; + ts_st->payload_size = 0; } if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO || size > DEFAULT_PES_PAYLOAD_SIZE) { - av_assert0(!ts_st->payload_index); + av_assert0(!ts_st->payload_size); // for video and subtitle, write a single pes packet mpegts_write_pes(s, st, buf, size, pts, dts, pkt->flags & AV_PKT_FLAG_KEY); av_free(data); return 0; } - if (!ts_st->payload_index) { + if (!ts_st->payload_size) { ts_st->payload_pts = pts; ts_st->payload_dts = dts; ts_st->payload_flags = pkt->flags; } - memcpy(ts_st->payload + ts_st->payload_index, buf, size); - ts_st->payload_index += size; + memcpy(ts_st->payload + ts_st->payload_size, buf, size); + ts_st->payload_size += size; av_free(data); @@ -1075,8 +1075,8 @@ static int mpegts_write_end(AVFormatContext *s) for(i = 0; i < s->nb_streams; i++) { st = s->streams[i]; ts_st = st->priv_data; - if (ts_st->payload_index > 0) { - mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_index, + if (ts_st->payload_size > 0) { + mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size, ts_st->payload_pts, ts_st->payload_dts, ts_st->payload_flags & AV_PKT_FLAG_KEY); } diff --git a/libavformat/segment.c b/libavformat/segment.c index 80716bcf90..89ae62d312 100644 --- a/libavformat/segment.c +++ b/libavformat/segment.c @@ -1,5 +1,5 @@ /* - * Generic Segmenter + * Generic segmenter * Copyright (c) 2011, Luca Barbato * * This file is part of Libav. @@ -19,43 +19,45 @@ * 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); @@ -63,24 +65,35 @@ static int segment_header(SegmentContext *s) 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; @@ -90,30 +103,44 @@ static int seg_write_header(AVFormatContext *s) { 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; @@ -121,26 +148,34 @@ static int seg_write_header(AVFormatContext *s) 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; } @@ -149,30 +184,49 @@ 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]; + 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->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; } @@ -180,19 +234,22 @@ static int seg_write_trailer(struct AVFormatContext *s) { 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 }, }; @@ -203,32 +260,14 @@ static const AVClass seg_class = { .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 |