From ea6825fd090ef3f8edbce506d2fbf6e187f585fa Mon Sep 17 00:00:00 2001 From: Nicolas George Date: Thu, 20 Feb 2014 20:52:06 +0100 Subject: [PATCH 1/6] lavc/pthread: copy packet side data. --- libavcodec/pthread_frame.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/libavcodec/pthread_frame.c b/libavcodec/pthread_frame.c index d7479d2142..4e385e7136 100644 --- a/libavcodec/pthread_frame.c +++ b/libavcodec/pthread_frame.c @@ -317,6 +317,7 @@ static int submit_packet(PerThreadContext *p, AVPacket *avpkt) FrameThreadContext *fctx = p->parent; PerThreadContext *prev_thread = fctx->prev_thread; const AVCodec *codec = p->avctx->codec; + int ret; if (!avpkt->size && !(codec->capabilities & CODEC_CAP_DELAY)) return 0; @@ -340,6 +341,7 @@ static int submit_packet(PerThreadContext *p, AVPacket *avpkt) } } + av_packet_free_side_data(&p->avpkt); av_buffer_unref(&p->avpkt.buf); p->avpkt = *avpkt; if (avpkt->buf) @@ -354,6 +356,8 @@ static int submit_packet(PerThreadContext *p, AVPacket *avpkt) memcpy(p->buf, avpkt->data, avpkt->size); memset(p->buf + avpkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE); } + if ((ret = av_copy_packet_side_data(&p->avpkt, avpkt)) < 0) + return ret; p->state = STATE_SETTING_UP; pthread_cond_signal(&p->input_cond); @@ -592,6 +596,7 @@ void ff_frame_thread_free(AVCodecContext *avctx, int thread_count) pthread_cond_destroy(&p->input_cond); pthread_cond_destroy(&p->progress_cond); pthread_cond_destroy(&p->output_cond); + av_packet_free_side_data(&p->avpkt); av_buffer_unref(&p->avpkt.buf); av_freep(&p->buf); av_freep(&p->released_buffers); From c37bbe54f400edc8cb0070138328048943e90b1a Mon Sep 17 00:00:00 2001 From: Nicolas George Date: Mon, 25 Nov 2013 17:26:07 +0100 Subject: [PATCH 2/6] lavd/xv: take aspect ratio into account. --- libavdevice/xv.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/libavdevice/xv.c b/libavdevice/xv.c index ab08e3e0f7..2948e6d9b7 100644 --- a/libavdevice/xv.c +++ b/libavdevice/xv.c @@ -130,8 +130,15 @@ static int xv_write_header(AVFormatContext *s) xv->image_width = encctx->width; xv->image_height = encctx->height; if (!xv->window_width && !xv->window_height) { + AVRational sar = encctx->sample_aspect_ratio; xv->window_width = encctx->width; xv->window_height = encctx->height; + if (sar.num) { + if (sar.num > sar.den) + xv->window_width = av_rescale(xv->window_width, sar.num, sar.den); + if (sar.num < sar.den) + xv->window_height = av_rescale(xv->window_height, sar.den, sar.num); + } } xv->window = XCreateSimpleWindow(xv->display, DefaultRootWindow(xv->display), xv->window_x, xv->window_y, From bc6901c94944e4a81be49a6b11183cd0c55d2738 Mon Sep 17 00:00:00 2001 From: Nicolas George Date: Wed, 5 Mar 2014 17:59:38 +0100 Subject: [PATCH 3/6] lavfi/af_atempo: clear references before returning error. Once the frame has been given to ff_filter_frame(), it can no longer be used, even on error. Fix trac ticket #3430. --- libavfilter/af_atempo.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libavfilter/af_atempo.c b/libavfilter/af_atempo.c index c474d6a58c..6a3fd61e60 100644 --- a/libavfilter/af_atempo.c +++ b/libavfilter/af_atempo.c @@ -1058,11 +1058,11 @@ static int push_samples(ATempoContext *atempo, outlink->time_base); ret = ff_filter_frame(outlink, atempo->dst_buffer); - if (ret < 0) - return ret; atempo->dst_buffer = NULL; atempo->dst = NULL; atempo->dst_end = NULL; + if (ret < 0) + return ret; atempo->nsamples_out += n_out; return 0; From 97e87e09c841701798a27eb4f18e2fb6a612ec88 Mon Sep 17 00:00:00 2001 From: Nicolas George Date: Tue, 25 Feb 2014 21:50:54 +0100 Subject: [PATCH 4/6] lavf: add subfile protocol. --- Changelog | 1 + doc/protocols.texi | 24 +++++++ libavformat/Makefile | 1 + libavformat/allformats.c | 1 + libavformat/subfile.c | 147 +++++++++++++++++++++++++++++++++++++++ libavformat/version.h | 4 +- 6 files changed, 176 insertions(+), 2 deletions(-) create mode 100644 libavformat/subfile.c diff --git a/Changelog b/Changelog index e280e0a57e..7d50591de0 100644 --- a/Changelog +++ b/Changelog @@ -32,6 +32,7 @@ version 2.2: - Support DNx444 - libx265 encoder - dejudder filter +- subfile protocol version 2.1: diff --git a/doc/protocols.texi b/doc/protocols.texi index 304b216f43..c39ab614f3 100644 --- a/doc/protocols.texi +++ b/doc/protocols.texi @@ -951,6 +951,30 @@ this binary block are used as master key, the following 14 bytes are used as master salt. @end table +@section subfile + +Virtually extract a segment of a file or another stream. +The underlying stream must be seekable. + +Accepted options: +@table @option +@item start +Start offset of the extracted segment, in bytes. +@item end +End offset of the extracted segment, in bytes. +@end table + +Examples: + +Extract a chapter from a DVD VOB file (start and end sectors obtained +externally and multiplied by 2048): +@example +subfile,,start,153391104,end,268142592,,:/media/dvd/VIDEO_TS/VTS_08_1.VOB +@end example + +Play an AVI file directly from a TAR archive: +subfile,,start,183241728,end,366490624,,:archive.tar + @section tcp Transmission Control Protocol. diff --git a/libavformat/Makefile b/libavformat/Makefile index 9ef81ccc29..dd13fd0167 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -464,6 +464,7 @@ OBJS-$(CONFIG_RTMPTS_PROTOCOL) += rtmpproto.o rtmppkt.o OBJS-$(CONFIG_RTP_PROTOCOL) += rtpproto.o OBJS-$(CONFIG_SCTP_PROTOCOL) += sctp.o OBJS-$(CONFIG_SRTP_PROTOCOL) += srtpproto.o srtp.o +OBJS-$(CONFIG_SUBFILE_PROTOCOL) += subfile.o OBJS-$(CONFIG_TCP_PROTOCOL) += tcp.o OBJS-$(CONFIG_TLS_PROTOCOL) += tls.o OBJS-$(CONFIG_UDP_PROTOCOL) += udp.o diff --git a/libavformat/allformats.c b/libavformat/allformats.c index 0e70b6e42b..16a3ae3e3e 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -338,6 +338,7 @@ void av_register_all(void) REGISTER_PROTOCOL(RTP, rtp); REGISTER_PROTOCOL(SCTP, sctp); REGISTER_PROTOCOL(SRTP, srtp); + REGISTER_PROTOCOL(SUBFILE, subfile); REGISTER_PROTOCOL(TCP, tcp); REGISTER_PROTOCOL(TLS, tls); REGISTER_PROTOCOL(UDP, udp); diff --git a/libavformat/subfile.c b/libavformat/subfile.c new file mode 100644 index 0000000000..0e84384600 --- /dev/null +++ b/libavformat/subfile.c @@ -0,0 +1,147 @@ +/* + * Copyright (c) 2014 Nicolas George + * + * This file is part of FFmpeg. + * + * FFmpeg 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. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/avassert.h" +#include "libavutil/avstring.h" +#include "libavutil/opt.h" +#include "avformat.h" +#include "url.h" + +typedef struct SubfileContext { + const AVClass *class; + URLContext *h; + int64_t start; + int64_t end; + int64_t pos; +} SubfileContext; + +#define OFFSET(field) offsetof(SubfileContext, field) +#define D AV_OPT_FLAG_DECODING_PARAM + +static const AVOption subfile_options[] = { + { "start", "start offset", OFFSET(start), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, D }, + { "end", "end offset", OFFSET(end), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, D }, + { NULL } +}; + +#undef OFFSET +#undef D + +static const AVClass subfile_class = { + .class_name = "subfile", + .item_name = av_default_item_name, + .option = subfile_options, + .version = LIBAVUTIL_VERSION_INT, +}; + +static int slave_seek(URLContext *h) +{ + SubfileContext *c = h->priv_data; + int64_t ret; + + if ((ret = ffurl_seek(c->h, c->pos, SEEK_SET)) != c->pos) { + if (ret >= 0) + ret = AVERROR_BUG; + av_log(h, AV_LOG_ERROR, "Impossible to seek in file: %s\n", + av_err2str(ret)); + return ret; + } + return 0; +} + +static int subfile_open(URLContext *h, const char *filename, int flags, + AVDictionary **options) +{ + SubfileContext *c = h->priv_data; + int ret; + + if (c->end <= c->start) { + av_log(h, AV_LOG_ERROR, "end before start\n"); + return AVERROR(EINVAL); + } + av_strstart(filename, "subfile:", &filename); + ret = ffurl_open(&c->h, filename, flags, &h->interrupt_callback, options); + if (ret < 0) + return ret; + c->pos = c->start; + if ((ret = slave_seek(h)) < 0) { + ffurl_close(c->h); + return ret; + } + return 0; +} + +static int subfile_close(URLContext *h) +{ + SubfileContext *c = h->priv_data; + return ffurl_close(c->h); +} + +static int subfile_read(URLContext *h, unsigned char *buf, int size) +{ + SubfileContext *c = h->priv_data; + int64_t rest = c->end - c->pos; + int ret; + + if (rest <= 0) + return 0; + size = FFMIN(size, rest); + ret = ffurl_read(c->h, buf, size); + if (ret >= 0) + c->pos += ret; + return ret; +} + +static int64_t subfile_seek(URLContext *h, int64_t pos, int whence) +{ + SubfileContext *c = h->priv_data; + int64_t new_pos = -1; + int ret; + + if (whence == AVSEEK_SIZE) + return c->end - c->start; + switch (whence) { + case SEEK_SET: + new_pos = c->start + pos; + break; + case SEEK_CUR: + new_pos += pos; + break; + case SEEK_END: + new_pos = c->end + c->pos; + break; + } + if (new_pos < c->start) + return AVERROR(EINVAL); + c->pos = new_pos; + if ((ret = slave_seek(h)) < 0) + return ret; + return c->pos - c->start; +} + +URLProtocol ff_subfile_protocol = { + .name = "subfile", + .url_open2 = subfile_open, + .url_read = subfile_read, + .url_seek = subfile_seek, + .url_close = subfile_close, + .priv_data_size = sizeof(SubfileContext), + .priv_data_class = &subfile_class, +}; diff --git a/libavformat/version.h b/libavformat/version.h index 7b011b2734..f9fc9d8302 100644 --- a/libavformat/version.h +++ b/libavformat/version.h @@ -30,8 +30,8 @@ #include "libavutil/version.h" #define LIBAVFORMAT_VERSION_MAJOR 55 -#define LIBAVFORMAT_VERSION_MINOR 33 -#define LIBAVFORMAT_VERSION_MICRO 101 +#define LIBAVFORMAT_VERSION_MINOR 34 +#define LIBAVFORMAT_VERSION_MICRO 100 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ LIBAVFORMAT_VERSION_MINOR, \ From 8a670f52a56ba01bdb62b91b020573b217e40fb3 Mon Sep 17 00:00:00 2001 From: Nicolas George Date: Sat, 1 Mar 2014 12:13:11 +0100 Subject: [PATCH 5/6] lavf/concatdec: more reliable test for absolute URLs. ff_make_absolute_url() recognizes the "://" pattern usual in HTTP-like protocols, but consider relative URLs starting with just the protocol name or using the comma syntax for options. --- libavformat/concatdec.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/libavformat/concatdec.c b/libavformat/concatdec.c index 979499af0c..99f0b6881f 100644 --- a/libavformat/concatdec.c +++ b/libavformat/concatdec.c @@ -85,18 +85,29 @@ static int add_file(AVFormatContext *avf, char *filename, ConcatFile **rfile, ConcatContext *cat = avf->priv_data; ConcatFile *file; char *url = NULL; - size_t url_len; + const char *proto; + size_t url_len, proto_len; int ret; if (cat->safe > 0 && !safe_filename(filename)) { av_log(avf, AV_LOG_ERROR, "Unsafe file name '%s'\n", filename); FAIL(AVERROR(EPERM)); } + + proto = avio_find_protocol_name(filename); + proto_len = proto ? strlen(proto) : 0; + if (!memcmp(filename, proto, proto_len) && + (filename[proto_len] == ':' || filename[proto_len] == ',')) { + url = filename; + filename = NULL; + } else { + /* TODO reindent */ url_len = strlen(avf->filename) + strlen(filename) + 16; if (!(url = av_malloc(url_len))) FAIL(AVERROR(ENOMEM)); ff_make_absolute_url(url, url_len, avf->filename, filename); av_freep(&filename); + } if (cat->nb_files >= *nb_files_alloc) { size_t n = FFMAX(*nb_files_alloc * 2, 16); From 6bffa83bbeac4820f7705188e20f2e307e832fd7 Mon Sep 17 00:00:00 2001 From: Nicolas George Date: Sat, 8 Mar 2014 12:44:45 +0100 Subject: [PATCH 6/6] lavf/concatdec: reindent after last commit. --- libavformat/concatdec.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/libavformat/concatdec.c b/libavformat/concatdec.c index 99f0b6881f..71b9f7c6e6 100644 --- a/libavformat/concatdec.c +++ b/libavformat/concatdec.c @@ -101,12 +101,11 @@ static int add_file(AVFormatContext *avf, char *filename, ConcatFile **rfile, url = filename; filename = NULL; } else { - /* TODO reindent */ - url_len = strlen(avf->filename) + strlen(filename) + 16; - if (!(url = av_malloc(url_len))) - FAIL(AVERROR(ENOMEM)); - ff_make_absolute_url(url, url_len, avf->filename, filename); - av_freep(&filename); + url_len = strlen(avf->filename) + strlen(filename) + 16; + if (!(url = av_malloc(url_len))) + FAIL(AVERROR(ENOMEM)); + ff_make_absolute_url(url, url_len, avf->filename, filename); + av_freep(&filename); } if (cat->nb_files >= *nb_files_alloc) {