[RFC] lavf/tee per-output stream selection
Hi, I need to be be able to send different streams to different tee outputs- In other words I need to select which streams should be routed to each tee output, while right now it is possible to specify the streams just once, and they will be the same for every output. This allows to create outputs with different streams, while avoiding duplicated encoding and the need to run different processes communicating through pipes. A possible design would involve a syntax like this: ffmpeg -i INPUT -c:v libx264 -c:a libvo-aacenc -tee_select "v|a" -f tee "out-v.ts|out-a.ts|out-av.ts" This would select the streams for each output, the non-matching streams won't be added to the corresponding output, and the corresponding packets will be ignored. This requires some somehow complex changes to the tee logic, but won't affect the rest of the library. At some point it would be nice to make possible to select streams on the base of their metadata (e.g. to select only streams in a certain language) using stream selection, but I can devise a simple syntax, not involving demonic escaping issues. Comments are welcome, especially from the maintainer of the tee virtual muxer. -- FFmpeg = Fanciful and Fiendish Minimalistic Patchable Exciting Game
Le nonidi 29 messidor, an CCXXI, Stefano Sabatini a écrit :
I need to be be able to send different streams to different tee outputs- In other words I need to select which streams should be routed to each tee output, while right now it is possible to specify the streams just once, and they will be the same for every output.
See the mail I just sent on the principle.
At some point it would be nice to make possible to select streams on the base of their metadata (e.g. to select only streams in a certain language) using stream selection, but I can devise a simple syntax, not involving demonic escaping issues.
For that, I suspect the really good solution would be to use a real scripting language to build filter graphs. And now, because of Clément, ffmpeg is linked with liblua anyway. Regards, -- Nicolas George
On 7/18/13, Nicolas George <nicolas.george@normalesup.org> wrote:
Le nonidi 29 messidor, an CCXXI, Stefano Sabatini a ecrit :
I need to be be able to send different streams to different tee outputs- In other words I need to select which streams should be routed to each tee output, while right now it is possible to specify the streams just once, and they will be the same for every output.
See the mail I just sent on the principle.
At some point it would be nice to make possible to select streams on the base of their metadata (e.g. to select only streams in a certain language) using stream selection, but I can devise a simple syntax, not involving demonic escaping issues.
For that, I suspect the really good solution would be to use a real scripting language to build filter graphs. And now, because of Clement, ffmpeg is linked with liblua anyway.
Really, first news to me, perhaps you are from alternate reality?
Regards,
-- Nicolas George
Le decadi 30 messidor, an CCXXI, Paul B Mahol a écrit :
Really, first news to me, perhaps you are from alternate reality?
commit 2a7f885fe1cfaaefe35252d68366d0d5709cd745 Author: Clément Bœsch <ubitux@gmail.com> Date: Thu Apr 4 08:22:32 2013 +0200 lavf: add libquvi demuxer. Dynamic Section: NEEDED libcurl-gnutls.so.4 NEEDED liblua5.1.so.0 NEEDED libc.so.6 SONAME libquvi.so.7 Regards, -- Nicolas George
On date Thursday 2013-07-18 11:07:28 +0000, Paul B Mahol encoded:
On 7/18/13, Nicolas George <nicolas.george@normalesup.org> wrote: [...]
For that, I suspect the really good solution would be to use a real scripting language to build filter graphs. And now, because of Clement, ffmpeg is linked with liblua anyway.
Really, first news to me, perhaps you are from alternate reality?
I think he refers libquvi, which internally links against liblua. -- FFmpeg = Foolish Fanciful Mysterious Portentous Ecstatic God
On date Thursday 2013-07-18 12:32:46 +0200, Nicolas George encoded:
Le nonidi 29 messidor, an CCXXI, Stefano Sabatini a écrit : [...]
At some point it would be nice to make possible to select streams on the base of their metadata (e.g. to select only streams in a certain language) using stream selection, but I can devise a simple syntax, not involving demonic escaping issues.
For that, I suspect the really good solution would be to use a real scripting language to build filter graphs. And now, because of Clément, ffmpeg is linked with liblua anyway.
After some thought my conclusion was that SWIG would be a better solution than a single scripting language, and that lavfi filtering requires libav* scripting. OTOH this requires a serious effort, the kind which is probably doomed to fail unless there is some concertated effort and an economic incentive, maybe a related crowd-funding project may do since SOC projects seem not to be an option anymore for a reason or another (not that they were ever particularly effective at creating regular contributors or sponsoring internal developers). -- FFmpeg = F***ing Free Martial Perfectionist Elected Guide
On 7/18/13, Stefano Sabatini <stefasab@gmail.com> wrote:
On date Thursday 2013-07-18 12:32:46 +0200, Nicolas George encoded:
Le nonidi 29 messidor, an CCXXI, Stefano Sabatini a ecrit : [...]
At some point it would be nice to make possible to select streams on the base of their metadata (e.g. to select only streams in a certain language) using stream selection, but I can devise a simple syntax, not involving demonic escaping issues.
For that, I suspect the really good solution would be to use a real scripting language to build filter graphs. And now, because of Clement, ffmpeg is linked with liblua anyway.
After some thought my conclusion was that SWIG would be a better solution than a single scripting language, and that lavfi filtering requires libav* scripting.
OTOH this requires a serious effort, the kind which is probably doomed to fail unless there is some concertated effort and an economic incentive, maybe a related crowd-funding project may do since SOC projects seem not to be an option anymore for a reason or another (not that they were ever particularly effective at creating regular contributors or sponsoring internal developers).
Real scripting is really wanted.
On date Thursday 2013-07-18 12:32:46 +0200, Nicolas George encoded:
Le nonidi 29 messidor, an CCXXI, Stefano Sabatini a écrit :
I need to be be able to send different streams to different tee outputs- In other words I need to select which streams should be routed to each tee output, while right now it is possible to specify the streams just once, and they will be the same for every output.
See the mail I just sent on the principle.
That said, assuming I would come with a patch to implement this, would you accept to integrate it into master?
At some point it would be nice to make possible to select streams on the base of their metadata (e.g. to select only streams in a certain language) using stream selection, but I can devise a simple syntax, not involving demonic escaping issues.
For that, I suspect the really good solution would be to use a real scripting language to build filter graphs. And now, because of Clément, ffmpeg is linked with liblua anyway.
Stream selection would operate at the muxing level, when mapping the input to the output (and thus it would be useful even when stream-copying). How could be this implemented through filter configuration, or in other words how would you use scripting to implement stream selection/mapping? -- FFmpeg = Fanciful and Funny Mystic Programmable Extended Gargoyle
Le tridi 3 thermidor, an CCXXI, Stefano Sabatini a écrit :
That said, assuming I would come with a patch to implement this, would you accept to integrate it into master?
Sure.
Stream selection would operate at the muxing level, when mapping the input to the output (and thus it would be useful even when stream-copying). How could be this implemented through filter configuration, or in other words how would you use scripting to implement stream selection/mapping?
I had something like that in mind: f = graph.create_filter("demux", { file: input_file, streams: "all" }); for (i = 0; i < f.nb_outputs; i++) { lang = f.outputs[i].get_meta("lang"); if (lang == "fr" || lang == "en") { var w = graph.create_filter("mux", { file: "out-" + lang + ".ogg", streams: "a" }); f.outputs[i].connect(w.inputs[0]); } else { f.outputs[i].close(); } } graph.run(); Since the filters are inited immediately, the information about what streams can be read in a movie/demuxer source is available to build the rest of the filter graph. Regards, -- Nicolas George
On date Wednesday 2013-07-17 21:30:11 +0200, Stefano Sabatini encoded:
Hi,
I need to be be able to send different streams to different tee outputs- In other words I need to select which streams should be routed to each tee output, while right now it is possible to specify the streams just once, and they will be the same for every output.
This allows to create outputs with different streams, while avoiding duplicated encoding and the need to run different processes communicating through pipes.
A possible design would involve a syntax like this: ffmpeg -i INPUT -c:v libx264 -c:a libvo-aacenc -tee_select "v|a" -f tee "out-v.ts|out-a.ts|out-av.ts"
Elaborating more on this, the syntax could be: -f tee "[select=v]out-v.ts|[select=a]out-a.ts|out.ts" There is the usual namespace problem. I'll try to provide an implementation soon. -- FFmpeg = Foolish and Frightening Mortal Pitiful Ecletic Ghost
On date Wednesday 2013-07-31 18:12:41 +0200, Stefano Sabatini encoded:
On date Wednesday 2013-07-17 21:30:11 +0200, Stefano Sabatini encoded:
Hi,
I need to be be able to send different streams to different tee outputs- In other words I need to select which streams should be routed to each tee output, while right now it is possible to specify the streams just once, and they will be the same for every output. gi This allows to create outputs with different streams, while avoiding duplicated encoding and the need to run different processes communicating through pipes.
A possible design would involve a syntax like this: ffmpeg -i INPUT -c:v libx264 -c:a libvo-aacenc -tee_select "v|a" -f tee "out-v.ts|out-a.ts|out-av.ts"
Elaborating more on this, the syntax could be: -f tee "[select=v]out-v.ts|[select=a]out-a.ts|out.ts"
There is the usual namespace problem. I'll try to provide an implementation soon.
In attachment. -- FFmpeg = Funny and Fascinating Marvellous Prodigious Emblematic Game
On Sat, Aug 03, 2013 at 12:22:35PM +0200, Stefano Sabatini wrote:
On date Wednesday 2013-07-31 18:12:41 +0200, Stefano Sabatini encoded:
On date Wednesday 2013-07-17 21:30:11 +0200, Stefano Sabatini encoded:
Hi,
I need to be be able to send different streams to different tee outputs- In other words I need to select which streams should be routed to each tee output, while right now it is possible to specify the streams just once, and they will be the same for every output. gi This allows to create outputs with different streams, while avoiding duplicated encoding and the need to run different processes communicating through pipes.
A possible design would involve a syntax like this: ffmpeg -i INPUT -c:v libx264 -c:a libvo-aacenc -tee_select "v|a" -f tee "out-v.ts|out-a.ts|out-av.ts"
Elaborating more on this, the syntax could be: -f tee "[select=v]out-v.ts|[select=a]out-a.ts|out.ts"
There is the usual namespace problem. I'll try to provide an implementation soon.
In attachment. -- FFmpeg = Funny and Fascinating Marvellous Prodigious Emblematic Game
doc/muxers.texi | 5 +++++ libavformat/tee.c | 43 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 42 insertions(+), 6 deletions(-) bd71c3240f2963c03669a59b26a2eb22f933fc52 0002-lavf-tee-add-special-select-option.patch From ce21456c8e778ddea3f4c2017c9c987978195c5f Mon Sep 17 00:00:00 2001 From: Stefano Sabatini <stefasab@gmail.com> Date: Sat, 3 Aug 2013 11:23:59 +0200 Subject: [PATCH] lavf/tee: add special select option
TODO: bump micro --- doc/muxers.texi | 5 +++++ libavformat/tee.c | 43 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 42 insertions(+), 6 deletions(-)
diff --git a/doc/muxers.texi b/doc/muxers.texi index 7c5a1c8..37a1734 100644 --- a/doc/muxers.texi +++ b/doc/muxers.texi @@ -844,6 +844,11 @@ The BNF description of the bitstream filters specification is given by: @var{BSF} ::= @var{BSF_NAME}[+@var{STREAM_SPECIFIER}] @var{BSFS} ::= @var{BSF}[,@var{BSFS}] @end example + +@item select +Select the streams that should be mapped to the slave output, +specified by a stream specifier. If not specified, this defaults to +all the input streams. @end table
might be a good idea to add an example too [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB DNS cache poisoning attacks, popular search engine, Google internet authority dont be evil, please
On date Saturday 2013-08-03 13:01:36 +0200, Michael Niedermayer encoded:
On Sat, Aug 03, 2013 at 12:22:35PM +0200, Stefano Sabatini wrote: [...]
doc/muxers.texi | 5 +++++ libavformat/tee.c | 43 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 42 insertions(+), 6 deletions(-) bd71c3240f2963c03669a59b26a2eb22f933fc52 0002-lavf-tee-add-special-select-option.patch From ce21456c8e778ddea3f4c2017c9c987978195c5f Mon Sep 17 00:00:00 2001 From: Stefano Sabatini <stefasab@gmail.com> Date: Sat, 3 Aug 2013 11:23:59 +0200 Subject: [PATCH] lavf/tee: add special select option
TODO: bump micro --- doc/muxers.texi | 5 +++++ libavformat/tee.c | 43 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 42 insertions(+), 6 deletions(-)
diff --git a/doc/muxers.texi b/doc/muxers.texi index 7c5a1c8..37a1734 100644 --- a/doc/muxers.texi +++ b/doc/muxers.texi @@ -844,6 +844,11 @@ The BNF description of the bitstream filters specification is given by: @var{BSF} ::= @var{BSF_NAME}[+@var{STREAM_SPECIFIER}] @var{BSFS} ::= @var{BSF}[,@var{BSFS}] @end example + +@item select +Select the streams that should be mapped to the slave output, +specified by a stream specifier. If not specified, this defaults to +all the input streams. @end table
might be a good idea to add an example too
Updated with a few minor fixes, and a documentation example patch. -- FFmpeg = Fascinating Free Miracolous Petty EnGine
Le sextidi 16 thermidor, an CCXXI, Stefano Sabatini a écrit :
From 7b08890d825abc7df66bd49e8cbb1f4ae5c888b1 Mon Sep 17 00:00:00 2001 From: Stefano Sabatini <stefasab@gmail.com> Date: Sat, 3 Aug 2013 11:23:59 +0200 Subject: [PATCH] lavf/tee: add special select option
TODO: bump micro --- doc/muxers.texi | 5 +++++ libavformat/tee.c | 48 ++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 45 insertions(+), 8 deletions(-)
diff --git a/doc/muxers.texi b/doc/muxers.texi index 7c5a1c8..37a1734 100644 --- a/doc/muxers.texi +++ b/doc/muxers.texi @@ -844,6 +844,11 @@ The BNF description of the bitstream filters specification is given by: @var{BSF} ::= @var{BSF_NAME}[+@var{STREAM_SPECIFIER}] @var{BSFS} ::= @var{BSF}[,@var{BSFS}] @end example + +@item select +Select the streams that should be mapped to the slave output, +specified by a stream specifier. If not specified, this defaults to +all the input streams.
Examples may be welcome. Is it possible to have several stream specifiers, i.e. map "video + English audio + French audio" to "video + English audio" and "video + French audio"?
@end table
Example: encode something and both archive it in a WebM file and stream it diff --git a/libavformat/tee.c b/libavformat/tee.c index 90947c7..4604ab4 100644 --- a/libavformat/tee.c +++ b/libavformat/tee.c @@ -30,6 +30,7 @@ typedef struct { AVFormatContext *fmt_ctx; AVBitStreamFilterContext **bsf_ctxs; ///< bitstream filters per stream
+ int *stream_map; ///< map between input and output streams, disabled streams are set to -1
Nit: please try to keep lines below 80 chars. Also, the comment is unclear about the direction of the map, it could be either: stream_num_in_slave = stream_map[stream_num_in_global] stream_num_in_global = stream_map[stream_num_in_slave]
} TeeSlave;
typedef struct TeeContext { @@ -179,9 +180,10 @@ static int open_slave(AVFormatContext *avf, char *slave, TeeSlave *tee_slave) AVDictionary *options = NULL; AVDictionaryEntry *entry; char *filename; - char *format = NULL, *bsfs = NULL; + char *format = NULL, *bsfs = NULL, *select = NULL; AVFormatContext *avf2 = NULL; AVStream *st, *st2; + int stream_count;
if ((ret = parse_slave_options(avf, slave, &options, &filename)) < 0) return ret; @@ -195,14 +197,42 @@ static int open_slave(AVFormatContext *avf, char *slave, TeeSlave *tee_slave) entry->value = NULL; /* prevent it from being freed */ av_dict_set(&options, "bsfs", NULL, 0); } + if ((entry = av_dict_get(options, "select", NULL, 0))) { + select = entry->value; + entry->value = NULL; /* prevent it from being freed */ + av_dict_set(&options, "select", NULL, 0); + }
This hunk really needs factoring.
ret = avformat_alloc_output_context2(&avf2, NULL, format, filename); if (ret < 0) goto fail; av_free(format);
+ tee_slave->stream_map = av_calloc(avf->nb_streams, sizeof(int));
sizeof(*tee_slave->stream_map)
+ if (!tee_slave->stream_map) { + ret = AVERROR(ENOMEM); + goto fail; + } + + stream_count = 0; for (i = 0; i < avf->nb_streams; i++) { st = avf->streams[i];
+ if (select && select[0]) {
Why select[0]?
+ ret = avformat_match_stream_specifier(avf, avf->streams[i], select); + if (ret < 0) { + av_log(avf, AV_LOG_ERROR, + "Invalid stream specifier '%s' for output '%s'\n", + select, slave); + goto fail; + } + + if (ret == 0) { /* no match */ + tee_slave->stream_map[i] = -1; + continue; + } + } + tee_slave->stream_map[i] = stream_count++; + if (!(st2 = avformat_new_stream(avf2, NULL))) { ret = AVERROR(ENOMEM); goto fail; @@ -277,6 +307,7 @@ static void close_slaves(AVFormatContext *avf) av_bitstream_filter_close(bsf_ctx); } } + av_freep(&tee->slaves[i].stream_map);
avio_close(avf2->pb); avf2->pb = NULL; @@ -419,29 +450,30 @@ static int tee_write_packet(AVFormatContext *avf, AVPacket *pkt) AVPacket pkt2; int ret_all = 0, ret; unsigned i, s; + int s2; AVRational tb, tb2;
for (i = 0; i < tee->nb_slaves; i++) { avf2 = tee->slaves[i].fmt_ctx; s = pkt->stream_index; - if (s >= avf2->nb_streams) { - if (!ret_all) - ret_all = AVERROR(EINVAL); + s2 = tee->slaves[i].stream_map[s]; + if (s2 < 0) continue; - } + if ((ret = av_copy_packet(&pkt2, pkt)) < 0 || (ret = av_dup_packet(&pkt2))< 0) if (!ret_all) { ret = ret_all; continue; } - tb = avf ->streams[s]->time_base; - tb2 = avf2->streams[s]->time_base; + tb = avf ->streams[s ]->time_base; + tb2 = avf2->streams[s2]->time_base; pkt2.pts = av_rescale_q(pkt->pts, tb, tb2); pkt2.dts = av_rescale_q(pkt->dts, tb, tb2); pkt2.duration = av_rescale_q(pkt->duration, tb, tb2); + pkt2.stream_index = s2;
- filter_packet(avf2, &pkt2, avf2, tee->slaves[i].bsf_ctxs[s]); + filter_packet(avf2, &pkt2, avf2, tee->slaves[i].bsf_ctxs[s2]); if ((ret = av_interleaved_write_frame(avf2, &pkt2)) < 0) if (!ret_all) ret_all = ret;
From 371a649c329f57b023fdfe6d9eedbd46b2d7991c Mon Sep 17 00:00:00 2001 From: Stefano Sabatini <stefasab@gmail.com> Date: Sat, 3 Aug 2013 14:05:13 +0200 Subject: [PATCH] doc/muxers: add elaborate example for the tee muxer
--- doc/muxers.texi | 10 ++++++++++ 1 file changed, 10 insertions(+)
diff --git a/doc/muxers.texi b/doc/muxers.texi index 37a1734..beceeab 100644 --- a/doc/muxers.texi +++ b/doc/muxers.texi @@ -859,6 +859,16 @@ ffmpeg -i ... -c:v libx264 -c:a mp2 -f tee -map 0:v -map 0:a "archive-20121107.mkv|[f=mpegts]udp://10.0.1.255:1234/" @end example
+Example: use @command{ffmpeg} to encode the input, and send the output +to three different destinations. The @code{dump_extra} bitstream +filter is used to add extradata information to all the video keyframes +in output, as requested by the MPEG-TS format. The @filename{out.aac} +output is filtered in order to contain only audio packets. +@example +ffmpeg -i ... -map 0 -flags +global_header -c:v libx264 -c:a aac -strict experimental + -f tee "[bsfs=dump_extra+v]out.ts|[movflags=+faststart]out.mp4|[select=a]out.aac" +@end example + Note: some codecs may need different options depending on the output format; the auto-detection of this can not work with the tee muxer. The main example is the @option{global_header} flag.
This one seems ok. Regards, -- Nicolas George
On date Sunday 2013-08-04 12:07:14 +0200, Nicolas George encoded:
Le sextidi 16 thermidor, an CCXXI, Stefano Sabatini a écrit :
From 7b08890d825abc7df66bd49e8cbb1f4ae5c888b1 Mon Sep 17 00:00:00 2001 From: Stefano Sabatini <stefasab@gmail.com> Date: Sat, 3 Aug 2013 11:23:59 +0200 Subject: [PATCH] lavf/tee: add special select option
TODO: bump micro --- doc/muxers.texi | 5 +++++ libavformat/tee.c | 48 ++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 45 insertions(+), 8 deletions(-)
diff --git a/doc/muxers.texi b/doc/muxers.texi index 7c5a1c8..37a1734 100644 --- a/doc/muxers.texi +++ b/doc/muxers.texi @@ -844,6 +844,11 @@ The BNF description of the bitstream filters specification is given by: @var{BSF} ::= @var{BSF_NAME}[+@var{STREAM_SPECIFIER}] @var{BSFS} ::= @var{BSF}[,@var{BSFS}] @end example + +@item select +Select the streams that should be mapped to the slave output, +specified by a stream specifier. If not specified, this defaults to +all the input streams.
Examples may be welcome. Is it possible to have several stream specifiers, i.e. map "video + English audio + French audio" to "video + English audio" and "video + French audio"?
This is something I considered, but requires either an extension of the specifier syntax or a local hack. For example we may extend the specifier to take into account compositions like a+v, and consider the language like in a[lang=en]+v. Composition is probably a good idea since it would extend other parts of the codebase for free (e.g. ffprobe -select_streams a0+v1). As for the language thing I'm open to suggestions.
@end table
Example: encode something and both archive it in a WebM file and stream it diff --git a/libavformat/tee.c b/libavformat/tee.c index 90947c7..4604ab4 100644 --- a/libavformat/tee.c +++ b/libavformat/tee.c @@ -30,6 +30,7 @@ typedef struct { AVFormatContext *fmt_ctx; AVBitStreamFilterContext **bsf_ctxs; ///< bitstream filters per stream
+ int *stream_map; ///< map between input and output streams, disabled streams are set to -1
Nit: please try to keep lines below 80 chars.
Also, the comment is unclear about the direction of the map, it could be either:
stream_num_in_slave = stream_map[stream_num_in_global] stream_num_in_global = stream_map[stream_num_in_slave]
} TeeSlave;
typedef struct TeeContext { @@ -179,9 +180,10 @@ static int open_slave(AVFormatContext *avf, char *slave, TeeSlave *tee_slave) AVDictionary *options = NULL; AVDictionaryEntry *entry; char *filename; - char *format = NULL, *bsfs = NULL; + char *format = NULL, *bsfs = NULL, *select = NULL; AVFormatContext *avf2 = NULL; AVStream *st, *st2; + int stream_count;
if ((ret = parse_slave_options(avf, slave, &options, &filename)) < 0) return ret; @@ -195,14 +197,42 @@ static int open_slave(AVFormatContext *avf, char *slave, TeeSlave *tee_slave) entry->value = NULL; /* prevent it from being freed */ av_dict_set(&options, "bsfs", NULL, 0); } + if ((entry = av_dict_get(options, "select", NULL, 0))) { + select = entry->value; + entry->value = NULL; /* prevent it from being freed */ + av_dict_set(&options, "select", NULL, 0); + }
This hunk really needs factoring.
ret = avformat_alloc_output_context2(&avf2, NULL, format, filename); if (ret < 0) goto fail; av_free(format);
+ tee_slave->stream_map = av_calloc(avf->nb_streams, sizeof(int));
sizeof(*tee_slave->stream_map)
+ if (!tee_slave->stream_map) { + ret = AVERROR(ENOMEM); + goto fail; + } + + stream_count = 0; for (i = 0; i < avf->nb_streams; i++) { st = avf->streams[i];
+ if (select && select[0]) {
Why select[0]?
The empty string is parsed as "all", I can drop the check though. [...] -- FFmpeg = Fundamental Fascinating Mastodontic Peaceless Evil Gadget
On date Sunday 2013-08-04 12:07:14 +0200, Nicolas George encoded:
Le sextidi 16 thermidor, an CCXXI, Stefano Sabatini a écrit :
From 7b08890d825abc7df66bd49e8cbb1f4ae5c888b1 Mon Sep 17 00:00:00 2001 From: Stefano Sabatini <stefasab@gmail.com> Date: Sat, 3 Aug 2013 11:23:59 +0200 Subject: [PATCH] lavf/tee: add special select option
TODO: bump micro --- doc/muxers.texi | 5 +++++ libavformat/tee.c | 48 ++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 45 insertions(+), 8 deletions(-)
diff --git a/doc/muxers.texi b/doc/muxers.texi index 7c5a1c8..37a1734 100644 --- a/doc/muxers.texi +++ b/doc/muxers.texi @@ -844,6 +844,11 @@ The BNF description of the bitstream filters specification is given by: @var{BSF} ::= @var{BSF_NAME}[+@var{STREAM_SPECIFIER}] @var{BSFS} ::= @var{BSF}[,@var{BSFS}] @end example + +@item select +Select the streams that should be mapped to the slave output, +specified by a stream specifier. If not specified, this defaults to +all the input streams.
Examples may be welcome. Is it possible to have several stream specifiers, i.e. map "video + English audio + French audio" to "video + English audio" and "video + French audio"?
Not at the moment, see my other reply.
@end table
Example: encode something and both archive it in a WebM file and stream it diff --git a/libavformat/tee.c b/libavformat/tee.c index 90947c7..4604ab4 100644 --- a/libavformat/tee.c +++ b/libavformat/tee.c @@ -30,6 +30,7 @@ typedef struct { AVFormatContext *fmt_ctx; AVBitStreamFilterContext **bsf_ctxs; ///< bitstream filters per stream
+ int *stream_map; ///< map between input and output streams, disabled streams are set to -1
Nit: please try to keep lines below 80 chars.
Also, the comment is unclear about the direction of the map, it could be either:
stream_num_in_slave = stream_map[stream_num_in_global] stream_num_in_global = stream_map[stream_num_in_slave]
Changed.
} TeeSlave;
typedef struct TeeContext { @@ -179,9 +180,10 @@ static int open_slave(AVFormatContext *avf, char *slave, TeeSlave *tee_slave) AVDictionary *options = NULL; AVDictionaryEntry *entry; char *filename; - char *format = NULL, *bsfs = NULL; + char *format = NULL, *bsfs = NULL, *select = NULL; AVFormatContext *avf2 = NULL; AVStream *st, *st2; + int stream_count;
if ((ret = parse_slave_options(avf, slave, &options, &filename)) < 0) return ret; @@ -195,14 +197,42 @@ static int open_slave(AVFormatContext *avf, char *slave, TeeSlave *tee_slave) entry->value = NULL; /* prevent it from being freed */ av_dict_set(&options, "bsfs", NULL, 0); } + if ((entry = av_dict_get(options, "select", NULL, 0))) { + select = entry->value; + entry->value = NULL; /* prevent it from being freed */ + av_dict_set(&options, "select", NULL, 0); + }
This hunk really needs factoring.
Done through a macro.
ret = avformat_alloc_output_context2(&avf2, NULL, format, filename); if (ret < 0) goto fail; av_free(format);
+ tee_slave->stream_map = av_calloc(avf->nb_streams, sizeof(int));
sizeof(*tee_slave->stream_map)
+ if (!tee_slave->stream_map) { + ret = AVERROR(ENOMEM); + goto fail; + } + + stream_count = 0; for (i = 0; i < avf->nb_streams; i++) { st = avf->streams[i];
+ if (select && select[0]) {
Why select[0]?
Changed. [...] Patches updated. They depend on the per-stream bitstream filtering patch. -- FFmpeg = Friendly and Faithless Mystic Proud Elitarian Game
On date Thursday 2013-08-08 12:36:37 +0200, Stefano Sabatini encoded: [...]
Patches updated. They depend on the per-stream bitstream filtering patch. -- FFmpeg = Friendly and Faithless Mystic Proud Elitarian Game
From 0636fdf2b93c3b7c84eda4a30abaf23efff91d41 Mon Sep 17 00:00:00 2001 From: Stefano Sabatini <stefasab@gmail.com> Date: Thu, 8 Aug 2013 12:11:59 +0200 Subject: [PATCH] lavf/tee: add special select option
TODO: bump micro --- doc/muxers.texi | 5 +++++ libavformat/tee.c | 62 ++++++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 50 insertions(+), 17 deletions(-)
Updated and rebased. -- FFmpeg = Faithless Fast Majestic Perennial Extravagant Ghost
On date Monday 2013-08-19 12:11:59 +0200, Stefano Sabatini encoded:
On date Thursday 2013-08-08 12:36:37 +0200, Stefano Sabatini encoded: [...]
Patches updated. They depend on the per-stream bitstream filtering patch. -- FFmpeg = Friendly and Faithless Mystic Proud Elitarian Game
From 0636fdf2b93c3b7c84eda4a30abaf23efff91d41 Mon Sep 17 00:00:00 2001 From: Stefano Sabatini <stefasab@gmail.com> Date: Thu, 8 Aug 2013 12:11:59 +0200 Subject: [PATCH] lavf/tee: add special select option
TODO: bump micro --- doc/muxers.texi | 5 +++++ libavformat/tee.c | 62 ++++++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 50 insertions(+), 17 deletions(-)
Updated and rebased.
Ping. -- FFmpeg = Fundamental & Foolish Meaningful Portable Exciting Gladiator
Le duodi 2 fructidor, an CCXXI, Stefano Sabatini a écrit :
Updated and rebased. -- FFmpeg = Faithless Fast Majestic Perennial Extravagant Ghost
From b49874d5420cc2c0d8c6b3933ae74b8bebb9fb8d Mon Sep 17 00:00:00 2001 From: Stefano Sabatini <stefasab@gmail.com> Date: Thu, 8 Aug 2013 12:11:59 +0200 Subject: [PATCH] lavf/tee: add special select option
TODO: bump micro --- doc/muxers.texi | 5 +++++ libavformat/tee.c | 57 +++++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 50 insertions(+), 12 deletions(-)
diff --git a/doc/muxers.texi b/doc/muxers.texi index 52da73e..c9c4416 100644 --- a/doc/muxers.texi +++ b/doc/muxers.texi @@ -871,6 +871,11 @@ separated by @code{/}. If the stream specifier is not specified, the bistream filters will be applied to all streams in the output.
Several bitstream filters can be specified, separated by ",". + +@item select +Select the streams that should be mapped to the slave output, +specified by a stream specifier. If not specified, this defaults to +all the input streams. @end table
Example: encode something and both archive it in a WebM file and stream it diff --git a/libavformat/tee.c b/libavformat/tee.c index f7a8f41..a04c7ef 100644 --- a/libavformat/tee.c +++ b/libavformat/tee.c @@ -30,6 +30,9 @@ typedef struct { AVFormatContext *avf; AVBitStreamFilterContext **bsfs; ///< bitstream filters per stream +
+ /** map from input to output streams indexes, disabled output streams are set to -1 */
Minor nit: please split this line.
+ int *stream_map; } TeeSlave;
typedef struct TeeContext { @@ -134,24 +137,52 @@ static int open_slave(AVFormatContext *avf, char *slave, TeeSlave *tee_slave) AVDictionary *options = NULL; AVDictionaryEntry *entry; char *filename; - char *format = NULL; + char *format = NULL, *select = NULL; AVFormatContext *avf2 = NULL; AVStream *st, *st2; + int stream_count;
if ((ret = parse_slave_options(avf, slave, &options, &filename)) < 0) return ret; - if ((entry = av_dict_get(options, "f", NULL, 0))) { - format = entry->value; - entry->value = NULL; /* prevent it from being freed */ - av_dict_set(&options, "f", NULL, 0); + +#define STEAL_OPTION(option, field) \ + if ((entry = av_dict_get(options, option, NULL, 0))) { \ + field = entry->value; \ + entry->value = NULL; /* prevent it from being freed */ \ + av_dict_set(&options, option, NULL, 0); \ } + STEAL_OPTION("f", format); + STEAL_OPTION("select", select);
ret = avformat_alloc_output_context2(&avf2, NULL, format, filename); if (ret < 0) goto end;
+ tee_slave->stream_map = av_calloc(avf->nb_streams, sizeof(*tee_slave->stream_map)); + if (!tee_slave->stream_map) { + ret = AVERROR(ENOMEM); + goto end; + } + + stream_count = 0; for (i = 0; i < avf->nb_streams; i++) { st = avf->streams[i]; + if (select) { + ret = avformat_match_stream_specifier(avf, avf->streams[i], select); + if (ret < 0) { + av_log(avf, AV_LOG_ERROR, + "Invalid stream specifier '%s' for output '%s'\n", + select, slave); + goto end; + } + + if (ret == 0) { /* no match */ + tee_slave->stream_map[i] = -1; + continue; + } + } + tee_slave->stream_map[i] = stream_count++;
Nit: add a warning if some input streams are not mapped at all. for (i = 0; i < avf->nb_streams; i++) int mapped = 0; for (j = 0; i < tee->nb_slaves; j++) mapped += tee->slaves[j].stream_map[i] >= 0; if (!mapped) av_log(avf, AV_LOG_WARNING, "Input stream #%d is not mapped " "to any slave.\n", i); }
+ if (!(st2 = avformat_new_stream(avf2, NULL))) { ret = AVERROR(ENOMEM); goto end; @@ -266,6 +297,7 @@ static void close_slaves(AVFormatContext *avf) bsf = bsf_next; } } + av_freep(&tee->slaves[i].stream_map);
avio_close(avf2->pb); avf2->pb = NULL; @@ -408,29 +440,30 @@ static int tee_write_packet(AVFormatContext *avf, AVPacket *pkt) AVPacket pkt2; int ret_all = 0, ret; unsigned i, s; + int s2; AVRational tb, tb2;
for (i = 0; i < tee->nb_slaves; i++) { avf2 = tee->slaves[i].avf; s = pkt->stream_index; - if (s >= avf2->nb_streams) { - if (!ret_all) - ret_all = AVERROR(EINVAL); + s2 = tee->slaves[i].stream_map[s]; + if (s2 < 0) continue; - } + if ((ret = av_copy_packet(&pkt2, pkt)) < 0 || (ret = av_dup_packet(&pkt2))< 0) if (!ret_all) { ret = ret_all; continue; } - tb = avf ->streams[s]->time_base; - tb2 = avf2->streams[s]->time_base; + tb = avf ->streams[s ]->time_base; + tb2 = avf2->streams[s2]->time_base; pkt2.pts = av_rescale_q(pkt->pts, tb, tb2); pkt2.dts = av_rescale_q(pkt->dts, tb, tb2); pkt2.duration = av_rescale_q(pkt->duration, tb, tb2); + pkt2.stream_index = s2;
- filter_packet(avf2, &pkt2, avf2, tee->slaves[i].bsfs[s]); + filter_packet(avf2, &pkt2, avf2, tee->slaves[i].bsfs[s2]); if ((ret = av_interleaved_write_frame(avf2, &pkt2)) < 0) if (!ret_all) ret_all = ret;
Apart from that, LGTM. Sorry for the delay.
From 3cf0413b8b660b46a4b21eac2992dd68ceced51d Mon Sep 17 00:00:00 2001 From: Stefano Sabatini <stefasab@gmail.com> Date: Sat, 3 Aug 2013 14:05:13 +0200 Subject: [PATCH] doc/muxers: add elaborated example for the tee muxer
The example shows how to combine bsfs and select options.
LGTM too. Regards, -- Nicolas George
On date Wednesday 2013-08-21 15:01:44 +0200, Nicolas George encoded:
Le duodi 2 fructidor, an CCXXI, Stefano Sabatini a écrit :
Updated and rebased. -- FFmpeg = Faithless Fast Majestic Perennial Extravagant Ghost
From b49874d5420cc2c0d8c6b3933ae74b8bebb9fb8d Mon Sep 17 00:00:00 2001 From: Stefano Sabatini <stefasab@gmail.com> Date: Thu, 8 Aug 2013 12:11:59 +0200 Subject: [PATCH] lavf/tee: add special select option
TODO: bump micro --- doc/muxers.texi | 5 +++++ libavformat/tee.c | 57 +++++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 50 insertions(+), 12 deletions(-)
diff --git a/doc/muxers.texi b/doc/muxers.texi index 52da73e..c9c4416 100644 --- a/doc/muxers.texi +++ b/doc/muxers.texi @@ -871,6 +871,11 @@ separated by @code{/}. If the stream specifier is not specified, the bistream filters will be applied to all streams in the output.
Several bitstream filters can be specified, separated by ",". + +@item select +Select the streams that should be mapped to the slave output, +specified by a stream specifier. If not specified, this defaults to +all the input streams. @end table
Example: encode something and both archive it in a WebM file and stream it diff --git a/libavformat/tee.c b/libavformat/tee.c index f7a8f41..a04c7ef 100644 --- a/libavformat/tee.c +++ b/libavformat/tee.c @@ -30,6 +30,9 @@ typedef struct { AVFormatContext *avf; AVBitStreamFilterContext **bsfs; ///< bitstream filters per stream +
+ /** map from input to output streams indexes, disabled output streams are set to -1 */
Minor nit: please split this line.
+ int *stream_map; } TeeSlave;
typedef struct TeeContext { @@ -134,24 +137,52 @@ static int open_slave(AVFormatContext *avf, char *slave, TeeSlave *tee_slave) AVDictionary *options = NULL; AVDictionaryEntry *entry; char *filename; - char *format = NULL; + char *format = NULL, *select = NULL; AVFormatContext *avf2 = NULL; AVStream *st, *st2; + int stream_count;
if ((ret = parse_slave_options(avf, slave, &options, &filename)) < 0) return ret; - if ((entry = av_dict_get(options, "f", NULL, 0))) { - format = entry->value; - entry->value = NULL; /* prevent it from being freed */ - av_dict_set(&options, "f", NULL, 0); + +#define STEAL_OPTION(option, field) \ + if ((entry = av_dict_get(options, option, NULL, 0))) { \ + field = entry->value; \ + entry->value = NULL; /* prevent it from being freed */ \ + av_dict_set(&options, option, NULL, 0); \ } + STEAL_OPTION("f", format); + STEAL_OPTION("select", select);
ret = avformat_alloc_output_context2(&avf2, NULL, format, filename); if (ret < 0) goto end;
+ tee_slave->stream_map = av_calloc(avf->nb_streams, sizeof(*tee_slave->stream_map)); + if (!tee_slave->stream_map) { + ret = AVERROR(ENOMEM); + goto end; + } + + stream_count = 0; for (i = 0; i < avf->nb_streams; i++) { st = avf->streams[i]; + if (select) { + ret = avformat_match_stream_specifier(avf, avf->streams[i], select); + if (ret < 0) { + av_log(avf, AV_LOG_ERROR, + "Invalid stream specifier '%s' for output '%s'\n", + select, slave); + goto end; + } + + if (ret == 0) { /* no match */ + tee_slave->stream_map[i] = -1; + continue; + } + } + tee_slave->stream_map[i] = stream_count++;
Nit: add a warning if some input streams are not mapped at all.
Not sure what you mean. Most containers will refuse no input stream, so this should not be strictly required. I'll provide a further patch if you can clarify. [...]
Apart from that, LGTM. Sorry for the delay.
From 3cf0413b8b660b46a4b21eac2992dd68ceced51d Mon Sep 17 00:00:00 2001 From: Stefano Sabatini <stefasab@gmail.com> Date: Sat, 3 Aug 2013 14:05:13 +0200 Subject: [PATCH] doc/muxers: add elaborated example for the tee muxer
The example shows how to combine bsfs and select options.
LGTM too.
Will push both patches soon, thanks. -- FFmpeg = Free Fantastic Monstrous Political Empowered Gospel
Le quartidi 4 fructidor, an CCXXI, Stefano Sabatini a écrit :
Not sure what you mean. Most containers will refuse no input stream, so this should not be strictly required. I'll provide a further patch if you can clarify.
You may have missed the code snippet I suggested, I believe it explains what I mean, but I can rephrase it more clearly if necessary: Imagine you have four streams coming to tee, and two slaves: slave #0 maps streams 0 and 1, slave #1 maps streams 0 and 2: stream 3 arrives to tee but is not transmitted to any slave; that means the application makes effort to generate stream 3, but does nothing at all with it, this is probably not something the user wants. Regards, -- Nicolas George
On date Wednesday 2013-08-21 17:22:19 +0200, Nicolas George encoded:
Le quartidi 4 fructidor, an CCXXI, Stefano Sabatini a écrit :
Not sure what you mean. Most containers will refuse no input stream, so this should not be strictly required. I'll provide a further patch if you can clarify.
You may have missed the code snippet I suggested, I believe it explains what I mean, but I can rephrase it more clearly if necessary:
Imagine you have four streams coming to tee, and two slaves: slave #0 maps streams 0 and 1, slave #1 maps streams 0 and 2: stream 3 arrives to tee but is not transmitted to any slave; that means the application makes effort to generate stream 3, but does nothing at all with it, this is probably not something the user wants.
Makes sense, and yes I missed your snippet. Will push the modified patch soon. -- FFmpeg = Fiendish and Fostering Merciful Portable Extreme Guide
participants (4)
-
Michael Niedermayer -
Nicolas George -
Paul B Mahol -
Stefano Sabatini