[PATCH] lavf/tee: add support for bitstream filtering
TODO: add documentation, bump minor --- libavformat/tee.c | 231 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 221 insertions(+), 10 deletions(-) diff --git a/libavformat/tee.c b/libavformat/tee.c index 7b8b371..96ced3e 100644 --- a/libavformat/tee.c +++ b/libavformat/tee.c @@ -27,10 +27,16 @@ #define MAX_SLAVES 16 +typedef struct { + AVFormatContext *fmt_ctx; + AVBitStreamFilterContext **bsf_ctxs; ///< bitstream filters per stream +} TeeSlave; + typedef struct TeeContext { const AVClass *class; unsigned nb_slaves; - AVFormatContext *slaves[MAX_SLAVES]; + TeeSlave slaves[MAX_SLAVES]; + char *bsfs; } TeeContext; static const char *const slave_delim = "|"; @@ -38,10 +44,18 @@ static const char *const slave_opt_open = "["; static const char *const slave_opt_close = "]"; static const char *const slave_opt_delim = ":]"; /* must have the close too */ +#define OFFSET(x) offsetof(TeeContext, x) +#define E AV_OPT_FLAG_ENCODING_PARAM +static const AVOption tee_options[] = { + { "tee_bsfs", "set bitstream filters for each slave", OFFSET(bsfs), AV_OPT_TYPE_STRING, {.str = NULL}, .flags=E }, + { NULL }, +}; + static const AVClass tee_muxer_class = { .class_name = "Tee muxer", .item_name = av_default_item_name, .version = LIBAVUTIL_VERSION_INT, + .option = tee_options, }; static int parse_slave_options(void *log, char *slave, @@ -82,7 +96,93 @@ fail: return ret; } -static int open_slave(AVFormatContext *avf, char *slave, AVFormatContext **ravf) +/** + * Parse bitstream filter, in the form: + * BSF ::= BSF_NAME[:STREAM_SPECIFIER] + */ +static int parse_bsf(void *log_ctx, char *bsf, + AVFormatContext *fmt_ctx, + AVBitStreamFilterContext **bsf_ctxs) +{ + int i, ret = 0; + char *bsf1 = av_strdup(bsf); + char *bsf_name, *spec; + + if (!bsf1) + return AVERROR(ENOMEM); + bsf_name = av_strtok(bsf1, ":", &spec); + + /* select the streams matched by the specifier */ + for (i = 0; i < fmt_ctx->nb_streams; i++) { + AVBitStreamFilterContext *bsf_ctx; + + if (spec && spec[0]) { + ret = avformat_match_stream_specifier(fmt_ctx, fmt_ctx->streams[i], spec); + if (ret < 0) { + av_log(log_ctx, AV_LOG_ERROR, + "Invalid stream specifier for bitstream filter '%s'\n", bsf); + goto end; + } + + if (ret == 0) /* no match */ + continue; + } + + bsf_ctx = av_bitstream_filter_init(bsf_name); + if (!bsf_ctx) { + av_log(log_ctx, AV_LOG_ERROR, + "Cannot create bitstream filter with name '%s' for stream %d, " + "unknown filter or internal error happened\n", + bsf_name, i); + ret = AVERROR_UNKNOWN; + goto end; + } + + /* append bsf context to the list of per-stream bsf contexts */ + if (!bsf_ctxs[i]) { + bsf_ctxs[i] = bsf_ctx; + } else { + AVBitStreamFilterContext *bsf_ctx1 = bsf_ctxs[i]; + while (bsf_ctx1->next) + bsf_ctx1 = bsf_ctx1->next; + bsf_ctx1->next = bsf_ctx; + } + } + +end: + av_free(bsf1); + return ret; +} + +/** + * Parse list of bitstream filters in the form: + * BSFS ::= BSF[,BSFS] + * BSF ::= BSF_NAME[:STREAM_SPECIFIER] + */ +static int parse_slave_bsfs(void *log_ctx, char *slave_bsfs, + AVFormatContext *fmt_ctx, + AVBitStreamFilterContext **bsf_ctx) +{ + char *bsf, *slave_bsfs1 = av_strdup(slave_bsfs); + char *saveptr, *buf = slave_bsfs1; + int ret; + + if (!slave_bsfs1) + return AVERROR(ENOMEM); + + while (bsf = av_strtok(buf, ",", &saveptr)) { + buf = NULL; + ret = parse_bsf(log_ctx, bsf, fmt_ctx, bsf_ctx); + if (ret < 0) + goto end; + } + +end: + av_free(slave_bsfs1); + return ret; +} + +static int open_slave(AVFormatContext *avf, char *slave, char *slave_bsfs, TeeSlave *tee_slave) { int i, ret; AVDictionary *options = NULL; @@ -138,6 +238,7 @@ static int open_slave(AVFormatContext *avf, char *slave, AVFormatContext **ravf) slave, av_err2str(ret)); goto fail; } + if (options) { entry = NULL; while ((entry = av_dict_get(options, "", entry, AV_DICT_IGNORE_SUFFIX))) @@ -146,7 +247,16 @@ static int open_slave(AVFormatContext *avf, char *slave, AVFormatContext **ravf) goto fail; } - *ravf = avf2; + tee_slave->fmt_ctx = avf2; + tee_slave->bsf_ctxs = av_mallocz(avf2->nb_streams * sizeof(*(tee_slave->bsf_ctxs))); + if (!tee_slave->bsf_ctxs) + return AVERROR(ENOMEM); + + /* generate the array of bitstream filters */ + if (slave_bsfs) { + if ((ret = parse_slave_bsfs(avf, slave_bsfs, avf2, tee_slave->bsf_ctxs)) < 0) + return ret; + } return 0; fail: @@ -158,14 +268,50 @@ static void close_slaves(AVFormatContext *avf) { TeeContext *tee = avf->priv_data; AVFormatContext *avf2; - unsigned i; + unsigned i, j; for (i = 0; i < tee->nb_slaves; i++) { - avf2 = tee->slaves[i]; + avf2 = tee->slaves[i].fmt_ctx; + + for (j = 0; j < avf2->nb_streams; j++) { + AVBitStreamFilterContext *bsf_ctx = tee->slaves[i].bsf_ctxs[j]; + while (bsf_ctx) { + bsf_ctx = bsf_ctx->next; + av_bitstream_filter_close(bsf_ctx); + } + } + avio_close(avf2->pb); avf2->pb = NULL; avformat_free_context(avf2); - tee->slaves[i] = NULL; + tee->slaves[i].fmt_ctx = NULL; + } +} + +static void log_slave(TeeSlave *slave, void *log_ctx, int log_level) +{ + int i; + av_log(log_ctx, log_level, "filename:'%s' format:%s\n", + slave->fmt_ctx->filename, slave->fmt_ctx->oformat->name); + for (i = 0; i < slave->fmt_ctx->nb_streams; i++) { + AVStream *st = slave->fmt_ctx->streams[i]; + AVBitStreamFilterContext *bsf_ctx = slave->bsf_ctxs[i]; + + av_log(log_ctx, log_level, " stream:%d codec:%s type:%s", + i, avcodec_get_name(st->codec->codec_id), + st->codec->codec ? + av_get_media_type_string(st->codec->codec->type) : "copy"); + if (bsf_ctx) { + int first = 1; + av_log(log_ctx, log_level, " bsfs:"); + while (bsf_ctx) { + av_log(log_ctx, log_level, "%s%s", first ? "" : ",", + bsf_ctx->filter->name); + bsf_ctx = bsf_ctx->next; + first = 0; + } + } + av_log(log_ctx, log_level, "\n"); } } @@ -174,7 +320,9 @@ static int tee_write_header(AVFormatContext *avf) TeeContext *tee = avf->priv_data; unsigned nb_slaves = 0, i; const char *filename = avf->filename; - char *slaves[MAX_SLAVES]; + const char *bsfs = tee->bsfs; + char *slaves [MAX_SLAVES]; + char *slave_bsfs[MAX_SLAVES] = { NULL }; int ret; while (*filename) { @@ -192,10 +340,29 @@ static int tee_write_header(AVFormatContext *avf) filename++; } + i = 0; + while (bsfs && *bsfs) { + if (i >= nb_slaves) { + av_log(avf, AV_LOG_ERROR, + "%d bitstream filter sequences are specified, but only %d slaves " + "were specified, aborting\n", i+1, nb_slaves); + ret = AVERROR(EINVAL); + goto fail; + } + if (!(slave_bsfs[i++] = av_get_token(&bsfs, slave_delim))) { + ret = AVERROR(ENOMEM); + goto fail; + } + if (strspn(bsfs, slave_delim)) + bsfs++; + } + for (i = 0; i < nb_slaves; i++) { - if ((ret = open_slave(avf, slaves[i], &tee->slaves[i])) < 0) + if ((ret = open_slave(avf, slaves[i], slave_bsfs[i], &tee->slaves[i])) < 0) goto fail; + log_slave(&tee->slaves[i], avf, AV_LOG_VERBOSE); av_freep(&slaves[i]); + av_freep(&slave_bsfs[i]); } tee->nb_slaves = nb_slaves; @@ -208,6 +375,48 @@ fail: return ret; } +static int filter_packet(void *log_ctx, AVPacket *pkt, + AVCodecContext *enc_ctx, AVBitStreamFilterContext *bsf_ctx) +{ + while (bsf_ctx) { + AVPacket new_pkt = *pkt; + int ret = av_bitstream_filter_filter(bsf_ctx, enc_ctx, NULL, + &new_pkt.data, &new_pkt.size, + pkt->data, pkt->size, + pkt->flags & AV_PKT_FLAG_KEY); + if (ret == 0 && new_pkt.data != pkt->data && new_pkt.destruct) { + uint8_t *buf = av_malloc(new_pkt.size + FF_INPUT_BUFFER_PADDING_SIZE); + if (buf) { + memcpy(buf, new_pkt.data, new_pkt.size); + memset(buf + new_pkt.size, 0, FF_INPUT_BUFFER_PADDING_SIZE); + new_pkt.data = buf; + new_pkt.buf = NULL; + ret = 1; + } else + ret = AVERROR(ENOMEM); + } + + if (ret > 0) { + av_free_packet(pkt); + new_pkt.buf = av_buffer_create(new_pkt.data, new_pkt.size, + av_buffer_default_free, NULL, 0); + if (!new_pkt.buf) + return AVERROR(ENOMEM); + } else if (ret < 0) { + av_log(log_ctx, AV_LOG_ERROR, + "Failed to filter bitstream %s for stream %d with codec %s", + bsf_ctx->filter->name, pkt->stream_index, + enc_ctx->codec ? enc_ctx->codec->name : "copy"); + return ret; + } + *pkt = new_pkt; + + bsf_ctx = bsf_ctx->next; + } + + return 0; +} + static int tee_write_trailer(AVFormatContext *avf) { TeeContext *tee = avf->priv_data; @@ -216,7 +425,7 @@ static int tee_write_trailer(AVFormatContext *avf) unsigned i; for (i = 0; i < tee->nb_slaves; i++) { - avf2 = tee->slaves[i]; + avf2 = tee->slaves[i].fmt_ctx; if ((ret = av_write_trailer(avf2)) < 0) if (!ret_all) ret_all = ret; @@ -241,7 +450,7 @@ static int tee_write_packet(AVFormatContext *avf, AVPacket *pkt) AVRational tb, tb2; for (i = 0; i < tee->nb_slaves; i++) { - avf2 = tee->slaves[i]; + avf2 = tee->slaves[i].fmt_ctx; s = pkt->stream_index; if (s >= avf2->nb_streams) { if (!ret_all) @@ -259,6 +468,8 @@ static int tee_write_packet(AVFormatContext *avf, AVPacket *pkt) 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); + + filter_packet(avf2, &pkt2, avf2->streams[s]->codec, tee->slaves[i].bsf_ctxs[s]); if ((ret = av_interleaved_write_frame(avf2, &pkt2)) < 0) if (!ret_all) ret_all = ret; -- 1.8.1.2
On date Saturday 2013-07-06 12:18:09 +0200, Stefano Sabatini encoded:
TODO: add documentation, bump minor --- libavformat/tee.c | 231 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 221 insertions(+), 10 deletions(-)
Ping. I'll provide some explanation if required, I'll update the patch with docs once we agree on the interface/design. -- FFmpeg = Fancy Funny Mythic Political Entertaining Gnome
L'octidi 18 messidor, an CCXXI, Stefano Sabatini a écrit :
TODO: add documentation, bump minor --- libavformat/tee.c | 231 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 221 insertions(+), 10 deletions(-)
diff --git a/libavformat/tee.c b/libavformat/tee.c index 7b8b371..96ced3e 100644 --- a/libavformat/tee.c +++ b/libavformat/tee.c @@ -27,10 +27,16 @@
#define MAX_SLAVES 16
+typedef struct { + AVFormatContext *fmt_ctx; + AVBitStreamFilterContext **bsf_ctxs; ///< bitstream filters per stream +} TeeSlave;
Do you insist on the _ctx suffix? To me, they only make the lines of code longer.
+ typedef struct TeeContext { const AVClass *class; unsigned nb_slaves; - AVFormatContext *slaves[MAX_SLAVES]; + TeeSlave slaves[MAX_SLAVES]; + char *bsfs; } TeeContext;
static const char *const slave_delim = "|"; @@ -38,10 +44,18 @@ static const char *const slave_opt_open = "["; static const char *const slave_opt_close = "]"; static const char *const slave_opt_delim = ":]"; /* must have the close too */
+#define OFFSET(x) offsetof(TeeContext, x) +#define E AV_OPT_FLAG_ENCODING_PARAM +static const AVOption tee_options[] = { + { "tee_bsfs", "set bitstream filters for each slave", OFFSET(bsfs), AV_OPT_TYPE_STRING, {.str = NULL}, .flags=E }, + { NULL }, +};
I believe you can make this feature much simpler and also more user-friendly by using the existing options system. Your suggestion currently looks like that: -f tee -tee_bsfs 'annexbtomp4|dump_extra' 'foo.mp4|foo.ts' it would look like that: -f tee '[bsfs=annexbtomp4]foo.mp4|[bsfs=dump_extra]foo.ts' I like it better because the bsfs are nearer the corresponding file: if you change the order of the slaves, you do not risk to forget changing the order of the filters. It is also nicer when only one slave has filters and it is not the first. You just have to imitate the av_dict_get(..."f") part. Also, you could put the streams specifiers on the bsfs option rather than the bsfs names (which is strange when there are several filters, what does "dump_extra:a,mp4toannexb:v" mean?), that would make the parsing easier.
+ static const AVClass tee_muxer_class = { .class_name = "Tee muxer", .item_name = av_default_item_name, .version = LIBAVUTIL_VERSION_INT, + .option = tee_options, };
static int parse_slave_options(void *log, char *slave, @@ -82,7 +96,93 @@ fail: return ret; }
-static int open_slave(AVFormatContext *avf, char *slave, AVFormatContext **ravf) +/** + * Parse bitstream filter, in the form: + * BSF ::= BSF_NAME[:STREAM_SPECIFIER] + */ +static int parse_bsf(void *log_ctx, char *bsf, + AVFormatContext *fmt_ctx, + AVBitStreamFilterContext **bsf_ctxs) +{ + int i, ret = 0; + char *bsf1 = av_strdup(bsf); + char *bsf_name, *spec; +
+ if (!bsf1) + return AVERROR(ENOMEM);
Maybe strdup the whole string once and for all, so you can butcher it all you want without bothering about it?
+ bsf_name = av_strtok(bsf1, ":", &spec); + + /* select the streams matched by the specifier */ + for (i = 0; i < fmt_ctx->nb_streams; i++) { + AVBitStreamFilterContext *bsf_ctx; + + if (spec && spec[0]) { + ret = avformat_match_stream_specifier(fmt_ctx, fmt_ctx->streams[i], spec); + if (ret < 0) { + av_log(log_ctx, AV_LOG_ERROR, + "Invalid stream specifier for bitstream filter '%s'\n", bsf); + goto end; + } + + if (ret == 0) /* no match */ + continue; + } +
+ bsf_ctx = av_bitstream_filter_init(bsf_name);
Your parser does not allow to specify options if stream specifiers are in place ("dump_extra:v=a"). That is the reason I suggest to put streams specifiers on the option rather than the filter.
+ if (!bsf_ctx) { + av_log(log_ctx, AV_LOG_ERROR, + "Cannot create bitstream filter with name '%s' for stream %d, " + "unknown filter or internal error happened\n", + bsf_name, i); + ret = AVERROR_UNKNOWN; + goto end; + } + + /* append bsf context to the list of per-stream bsf contexts */ + if (!bsf_ctxs[i]) { + bsf_ctxs[i] = bsf_ctx; + } else { + AVBitStreamFilterContext *bsf_ctx1 = bsf_ctxs[i]; + while (bsf_ctx1->next) + bsf_ctx1 = bsf_ctx1->next; + bsf_ctx1->next = bsf_ctx; + } + } + +end: + av_free(bsf1); + return ret; +} + +/** + * Parse list of bitstream filters in the form: + * BSFS ::= BSF[,BSFS] + * BSF ::= BSF_NAME[:STREAM_SPECIFIER] + */ +static int parse_slave_bsfs(void *log_ctx, char *slave_bsfs, + AVFormatContext *fmt_ctx, + AVBitStreamFilterContext **bsf_ctx) +{ + char *bsf, *slave_bsfs1 = av_strdup(slave_bsfs); + char *saveptr, *buf = slave_bsfs1; + int ret; + + if (!slave_bsfs1) + return AVERROR(ENOMEM); + + while (bsf = av_strtok(buf, ",", &saveptr)) { + buf = NULL; + ret = parse_bsf(log_ctx, bsf, fmt_ctx, bsf_ctx); + if (ret < 0) + goto end; + } + +end: + av_free(slave_bsfs1); + return ret; +} + +static int open_slave(AVFormatContext *avf, char *slave, char *slave_bsfs, TeeSlave *tee_slave) { int i, ret; AVDictionary *options = NULL; @@ -138,6 +238,7 @@ static int open_slave(AVFormatContext *avf, char *slave, AVFormatContext **ravf) slave, av_err2str(ret)); goto fail; } + if (options) { entry = NULL; while ((entry = av_dict_get(options, "", entry, AV_DICT_IGNORE_SUFFIX))) @@ -146,7 +247,16 @@ static int open_slave(AVFormatContext *avf, char *slave, AVFormatContext **ravf) goto fail; }
- *ravf = avf2; + tee_slave->fmt_ctx = avf2; + tee_slave->bsf_ctxs = av_mallocz(avf2->nb_streams * sizeof(*(tee_slave->bsf_ctxs))); + if (!tee_slave->bsf_ctxs) + return AVERROR(ENOMEM); + + /* generate the array of bitstream filters */ + if (slave_bsfs) { + if ((ret = parse_slave_bsfs(avf, slave_bsfs, avf2, tee_slave->bsf_ctxs)) < 0) + return ret; + } return 0;
fail: @@ -158,14 +268,50 @@ static void close_slaves(AVFormatContext *avf) { TeeContext *tee = avf->priv_data; AVFormatContext *avf2; - unsigned i; + unsigned i, j;
for (i = 0; i < tee->nb_slaves; i++) { - avf2 = tee->slaves[i]; + avf2 = tee->slaves[i].fmt_ctx; + + for (j = 0; j < avf2->nb_streams; j++) { + AVBitStreamFilterContext *bsf_ctx = tee->slaves[i].bsf_ctxs[j]; + while (bsf_ctx) { + bsf_ctx = bsf_ctx->next; + av_bitstream_filter_close(bsf_ctx); + } + } + avio_close(avf2->pb); avf2->pb = NULL; avformat_free_context(avf2); - tee->slaves[i] = NULL; + tee->slaves[i].fmt_ctx = NULL; + } +} + +static void log_slave(TeeSlave *slave, void *log_ctx, int log_level) +{ + int i; + av_log(log_ctx, log_level, "filename:'%s' format:%s\n", + slave->fmt_ctx->filename, slave->fmt_ctx->oformat->name); + for (i = 0; i < slave->fmt_ctx->nb_streams; i++) { + AVStream *st = slave->fmt_ctx->streams[i]; + AVBitStreamFilterContext *bsf_ctx = slave->bsf_ctxs[i]; + + av_log(log_ctx, log_level, " stream:%d codec:%s type:%s", + i, avcodec_get_name(st->codec->codec_id), + st->codec->codec ? + av_get_media_type_string(st->codec->codec->type) : "copy"); + if (bsf_ctx) {
+ int first = 1; + av_log(log_ctx, log_level, " bsfs:"); + while (bsf_ctx) { + av_log(log_ctx, log_level, "%s%s", first ? "" : ",", + bsf_ctx->filter->name); + bsf_ctx = bsf_ctx->next; + first = 0;
You could do slightly simpler with either these solutions: av_log(..., "%s%s", name, bsf_ctx->next ? "," : ""); const char *delim = ""; av_log(..., "%s%s", delim, name); delim = ",";
+ } + } + av_log(log_ctx, log_level, "\n"); } }
@@ -174,7 +320,9 @@ static int tee_write_header(AVFormatContext *avf) TeeContext *tee = avf->priv_data; unsigned nb_slaves = 0, i; const char *filename = avf->filename; - char *slaves[MAX_SLAVES]; + const char *bsfs = tee->bsfs; + char *slaves [MAX_SLAVES]; + char *slave_bsfs[MAX_SLAVES] = { NULL }; int ret;
while (*filename) { @@ -192,10 +340,29 @@ static int tee_write_header(AVFormatContext *avf) filename++; }
+ i = 0; + while (bsfs && *bsfs) { + if (i >= nb_slaves) { + av_log(avf, AV_LOG_ERROR, + "%d bitstream filter sequences are specified, but only %d slaves " + "were specified, aborting\n", i+1, nb_slaves); + ret = AVERROR(EINVAL); + goto fail; + } + if (!(slave_bsfs[i++] = av_get_token(&bsfs, slave_delim))) { + ret = AVERROR(ENOMEM); + goto fail; + } + if (strspn(bsfs, slave_delim)) + bsfs++; + } + for (i = 0; i < nb_slaves; i++) { - if ((ret = open_slave(avf, slaves[i], &tee->slaves[i])) < 0) + if ((ret = open_slave(avf, slaves[i], slave_bsfs[i], &tee->slaves[i])) < 0) goto fail; + log_slave(&tee->slaves[i], avf, AV_LOG_VERBOSE); av_freep(&slaves[i]); + av_freep(&slave_bsfs[i]); }
tee->nb_slaves = nb_slaves; @@ -208,6 +375,48 @@ fail: return ret; }
+static int filter_packet(void *log_ctx, AVPacket *pkt, + AVCodecContext *enc_ctx, AVBitStreamFilterContext *bsf_ctx) +{ + while (bsf_ctx) { + AVPacket new_pkt = *pkt; + int ret = av_bitstream_filter_filter(bsf_ctx, enc_ctx, NULL, + &new_pkt.data, &new_pkt.size, + pkt->data, pkt->size, + pkt->flags & AV_PKT_FLAG_KEY); + if (ret == 0 && new_pkt.data != pkt->data && new_pkt.destruct) { + uint8_t *buf = av_malloc(new_pkt.size + FF_INPUT_BUFFER_PADDING_SIZE); + if (buf) { + memcpy(buf, new_pkt.data, new_pkt.size); + memset(buf + new_pkt.size, 0, FF_INPUT_BUFFER_PADDING_SIZE); + new_pkt.data = buf; + new_pkt.buf = NULL; + ret = 1; + } else + ret = AVERROR(ENOMEM); + } + + if (ret > 0) { + av_free_packet(pkt); + new_pkt.buf = av_buffer_create(new_pkt.data, new_pkt.size, + av_buffer_default_free, NULL, 0); + if (!new_pkt.buf) + return AVERROR(ENOMEM); + } else if (ret < 0) { + av_log(log_ctx, AV_LOG_ERROR, + "Failed to filter bitstream %s for stream %d with codec %s", + bsf_ctx->filter->name, pkt->stream_index, + enc_ctx->codec ? enc_ctx->codec->name : "copy"); + return ret; + } + *pkt = new_pkt; + + bsf_ctx = bsf_ctx->next; + } + + return 0; +} + static int tee_write_trailer(AVFormatContext *avf) { TeeContext *tee = avf->priv_data; @@ -216,7 +425,7 @@ static int tee_write_trailer(AVFormatContext *avf) unsigned i;
for (i = 0; i < tee->nb_slaves; i++) { - avf2 = tee->slaves[i]; + avf2 = tee->slaves[i].fmt_ctx; if ((ret = av_write_trailer(avf2)) < 0) if (!ret_all) ret_all = ret; @@ -241,7 +450,7 @@ static int tee_write_packet(AVFormatContext *avf, AVPacket *pkt) AVRational tb, tb2;
for (i = 0; i < tee->nb_slaves; i++) { - avf2 = tee->slaves[i]; + avf2 = tee->slaves[i].fmt_ctx; s = pkt->stream_index; if (s >= avf2->nb_streams) { if (!ret_all) @@ -259,6 +468,8 @@ static int tee_write_packet(AVFormatContext *avf, AVPacket *pkt) 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); + + filter_packet(avf2, &pkt2, avf2->streams[s]->codec, tee->slaves[i].bsf_ctxs[s]); if ((ret = av_interleaved_write_frame(avf2, &pkt2)) < 0) if (!ret_all) ret_all = ret;
Now, from the design point of view, I am afraid this is the first step in a slippery slope that leads to reimplementing one third of ffmpeg inside the tee muxer, and I suspect we do not want that. Since you resurrected the discussion, I have been thinking again about support for data streams in lavfi, and I believe it is actually quite easy. If implemented, the tee muxer would be redundant with the split filter and muxer sink, and bitstream filters are just data filters. That also solves the problems you raise in another mail about selecting which streams go to which slave. Filtering data is less hype than filtering subtitles, but it is much easier. Also, filtering data is useful for subtitles (think embedded fonts). I can post the state of my thinking if you are interested. Regards, -- Nicolas George
On date Thursday 2013-07-18 12:30:23 +0200, Nicolas George encoded:
L'octidi 18 messidor, an CCXXI, Stefano Sabatini a écrit :
TODO: add documentation, bump minor --- libavformat/tee.c | 231 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 221 insertions(+), 10 deletions(-)
diff --git a/libavformat/tee.c b/libavformat/tee.c index 7b8b371..96ced3e 100644 --- a/libavformat/tee.c +++ b/libavformat/tee.c @@ -27,10 +27,16 @@
#define MAX_SLAVES 16
+typedef struct { + AVFormatContext *fmt_ctx; + AVBitStreamFilterContext **bsf_ctxs; ///< bitstream filters per stream +} TeeSlave;
Do you insist on the _ctx suffix? To me, they only make the lines of code longer.
What do you suggest instead? fctx? I personally hate the confusion between object/object context, but not that I mind so much (especially if the code is authored by someone else than me).
+ typedef struct TeeContext { const AVClass *class; unsigned nb_slaves; - AVFormatContext *slaves[MAX_SLAVES]; + TeeSlave slaves[MAX_SLAVES]; + char *bsfs; } TeeContext;
static const char *const slave_delim = "|"; @@ -38,10 +44,18 @@ static const char *const slave_opt_open = "["; static const char *const slave_opt_close = "]"; static const char *const slave_opt_delim = ":]"; /* must have the close too */
+#define OFFSET(x) offsetof(TeeContext, x) +#define E AV_OPT_FLAG_ENCODING_PARAM +static const AVOption tee_options[] = { + { "tee_bsfs", "set bitstream filters for each slave", OFFSET(bsfs), AV_OPT_TYPE_STRING, {.str = NULL}, .flags=E }, + { NULL }, +};
I believe you can make this feature much simpler and also more user-friendly by using the existing options system.
Your suggestion currently looks like that:
-f tee -tee_bsfs 'annexbtomp4|dump_extra' 'foo.mp4|foo.ts'
it would look like that:
-f tee '[bsfs=annexbtomp4]foo.mp4|[bsfs=dump_extra]foo.ts'
I like it better because the bsfs are nearer the corresponding file: if you change the order of the slaves, you do not risk to forget changing the order of the filters. It is also nicer when only one slave has filters and it is not the first.
You just have to imitate the av_dict_get(..."f") part.
Also, you could put the streams specifiers on the bsfs option rather than the bsfs names (which is strange when there are several filters, what does "dump_extra:a,mp4toannexb:v" mean?), that would make the parsing easier.
Yes, but... this leads to the namespace problem which was never properly handled, if we had a bsf option to any muxer then it will shadow the option. [...]
+ bsf_ctx = av_bitstream_filter_init(bsf_name);
Your parser does not allow to specify options if stream specifiers are in place ("dump_extra:v=a"). That is the reason I suggest to put streams specifiers on the option rather than the filter.
My idea was indeed: dump_extra:v=args Since we currently don't support bsf options in ffmpeg, I didn't bother to support them here. [...]
static int tee_write_trailer(AVFormatContext *avf) { TeeContext *tee = avf->priv_data; @@ -216,7 +425,7 @@ static int tee_write_trailer(AVFormatContext *avf) unsigned i;
for (i = 0; i < tee->nb_slaves; i++) { - avf2 = tee->slaves[i]; + avf2 = tee->slaves[i].fmt_ctx; if ((ret = av_write_trailer(avf2)) < 0) if (!ret_all) ret_all = ret; @@ -241,7 +450,7 @@ static int tee_write_packet(AVFormatContext *avf, AVPacket *pkt) AVRational tb, tb2;
for (i = 0; i < tee->nb_slaves; i++) { - avf2 = tee->slaves[i]; + avf2 = tee->slaves[i].fmt_ctx; s = pkt->stream_index; if (s >= avf2->nb_streams) { if (!ret_all) @@ -259,6 +468,8 @@ static int tee_write_packet(AVFormatContext *avf, AVPacket *pkt) 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); + + filter_packet(avf2, &pkt2, avf2->streams[s]->codec, tee->slaves[i].bsf_ctxs[s]); if ((ret = av_interleaved_write_frame(avf2, &pkt2)) < 0) if (!ret_all) ret_all = ret;
Now, from the design point of view, I am afraid this is the first step in a slippery slope that leads to reimplementing one third of ffmpeg inside the tee muxer, and I suspect we do not want that.
Since you resurrected the discussion, I have been thinking again about support for data streams in lavfi, and I believe it is actually quite easy. If implemented, the tee muxer would be redundant with the split filter and muxer sink, and bitstream filters are just data filters. That also solves the problems you raise in another mail about selecting which streams go to which slave.
Filtering data is less hype than filtering subtitles, but it is much easier. Also, filtering data is useful for subtitles (think embedded fonts).
I can post the state of my thinking if you are interested.
Sure, I'm aware of the problem. On the other hand I need a solution in the short term, and under these conditions I prefer the proverbial egg today rather than a chicken tomorrow, especially considering that a proper design/discussion/implementation would probably require weeks/months rather than days. You're welcome to share your thougths about the lavfi data path. -- FFmpeg = Fiendish & Foolish Minimal Perfectionist Earthshaking Guide
Le decadi 30 messidor, an CCXXI, Stefano Sabatini a écrit :
What do you suggest instead? fctx? I personally hate the confusion between object/object context, but not that I mind so much (especially if the code is authored by someone else than me).
Since contexts are used much more widely, I believe they should get the shorter name, especially when there is absolutely no risk of confusion. In this particular case, "mux" or "muxer" seems a nice name for the AVFormatContext, and "filters" maybe for the bitstream filters.
Yes, but...
this leads to the namespace problem which was never properly handled, if we had a bsf option to any muxer then it will shadow the option.
I am still not convinced this issue is really a problem, options are few, it is easy to be careful. But you can just use the name "tee.bsf", since the dot was precisely reserved for that use.
My idea was indeed: dump_extra:v=args
Since we currently don't support bsf options in ffmpeg, I didn't bother to support them here.
That is right. In fact, I was convinced that av_bitstream_filter_init() would split the name at the equal sign, but I was wrong. But the other arguments to use the existing options system still stand.
Sure, I'm aware of the problem. On the other hand I need a solution in the short term, and under these conditions I prefer the proverbial egg today rather than a chicken tomorrow, especially considering that a proper design/discussion/implementation would probably require weeks/months rather than days.
Of course, that is why I do not object to the patch.
You're welcome to share your thougths about the lavfi data path.
Will do. Regards, -- Nicolas George
On date Thursday 2013-07-18 12:30:23 +0200, Nicolas George encoded:
L'octidi 18 messidor, an CCXXI, Stefano Sabatini a écrit :
TODO: add documentation, bump minor --- libavformat/tee.c | 231 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 221 insertions(+), 10 deletions(-)
diff --git a/libavformat/tee.c b/libavformat/tee.c index 7b8b371..96ced3e 100644 --- a/libavformat/tee.c +++ b/libavformat/tee.c @@ -27,10 +27,16 @@
#define MAX_SLAVES 16
+typedef struct { + AVFormatContext *fmt_ctx; + AVBitStreamFilterContext **bsf_ctxs; ///< bitstream filters per stream +} TeeSlave;
Do you insist on the _ctx suffix? To me, they only make the lines of code longer.
Still kept, but I can change it if you insist (or you can change back later if that saves time).
+ typedef struct TeeContext { const AVClass *class; unsigned nb_slaves; - AVFormatContext *slaves[MAX_SLAVES]; + TeeSlave slaves[MAX_SLAVES]; + char *bsfs; } TeeContext;
static const char *const slave_delim = "|"; @@ -38,10 +44,18 @@ static const char *const slave_opt_open = "["; static const char *const slave_opt_close = "]"; static const char *const slave_opt_delim = ":]"; /* must have the close too */
+#define OFFSET(x) offsetof(TeeContext, x) +#define E AV_OPT_FLAG_ENCODING_PARAM +static const AVOption tee_options[] = { + { "tee_bsfs", "set bitstream filters for each slave", OFFSET(bsfs), AV_OPT_TYPE_STRING, {.str = NULL}, .flags=E }, + { NULL }, +};
I believe you can make this feature much simpler and also more user-friendly by using the existing options system.
Your suggestion currently looks like that:
-f tee -tee_bsfs 'annexbtomp4|dump_extra' 'foo.mp4|foo.ts'
it would look like that:
-f tee '[bsfs=annexbtomp4]foo.mp4|[bsfs=dump_extra]foo.ts'
I like it better because the bsfs are nearer the corresponding file: if you change the order of the slaves, you do not risk to forget changing the order of the filters. It is also nicer when only one slave has filters and it is not the first.
You just have to imitate the av_dict_get(..."f") part.
Yes and true, it is now simpler and easier.
Also, you could put the streams specifiers on the bsfs option rather than the bsfs names (which is strange when there are several filters, what does "dump_extra:a,mp4toannexb:v" mean?), that would make the parsing easier.
Changed to: dumo_extra+a,mp4toannexb+v.
+ static const AVClass tee_muxer_class = { .class_name = "Tee muxer", .item_name = av_default_item_name, .version = LIBAVUTIL_VERSION_INT, + .option = tee_options, };
static int parse_slave_options(void *log, char *slave, @@ -82,7 +96,93 @@ fail: return ret; }
-static int open_slave(AVFormatContext *avf, char *slave, AVFormatContext **ravf) +/** + * Parse bitstream filter, in the form: + * BSF ::= BSF_NAME[:STREAM_SPECIFIER] + */ +static int parse_bsf(void *log_ctx, char *bsf, + AVFormatContext *fmt_ctx, + AVBitStreamFilterContext **bsf_ctxs) +{ + int i, ret = 0; + char *bsf1 = av_strdup(bsf); + char *bsf_name, *spec; +
+ if (!bsf1) + return AVERROR(ENOMEM);
Maybe strdup the whole string once and for all, so you can butcher it all you want without bothering about it?
Not sure what you mean, I'm already duping it.
+ bsf_name = av_strtok(bsf1, ":", &spec); + + /* select the streams matched by the specifier */ + for (i = 0; i < fmt_ctx->nb_streams; i++) { + AVBitStreamFilterContext *bsf_ctx; + + if (spec && spec[0]) { + ret = avformat_match_stream_specifier(fmt_ctx, fmt_ctx->streams[i], spec); + if (ret < 0) { + av_log(log_ctx, AV_LOG_ERROR, + "Invalid stream specifier for bitstream filter '%s'\n", bsf); + goto end; + } + + if (ret == 0) /* no match */ + continue; + } +
+ bsf_ctx = av_bitstream_filter_init(bsf_name);
Your parser does not allow to specify options if stream specifiers are in place ("dump_extra:v=a"). That is the reason I suggest to put streams specifiers on the option rather than the filter.
[...]
+static void log_slave(TeeSlave *slave, void *log_ctx, int log_level) +{ + int i; + av_log(log_ctx, log_level, "filename:'%s' format:%s\n", + slave->fmt_ctx->filename, slave->fmt_ctx->oformat->name); + for (i = 0; i < slave->fmt_ctx->nb_streams; i++) { + AVStream *st = slave->fmt_ctx->streams[i]; + AVBitStreamFilterContext *bsf_ctx = slave->bsf_ctxs[i]; + + av_log(log_ctx, log_level, " stream:%d codec:%s type:%s", + i, avcodec_get_name(st->codec->codec_id), + st->codec->codec ? + av_get_media_type_string(st->codec->codec->type) : "copy"); + if (bsf_ctx) {
+ int first = 1; + av_log(log_ctx, log_level, " bsfs:"); + while (bsf_ctx) { + av_log(log_ctx, log_level, "%s%s", first ? "" : ",", + bsf_ctx->filter->name); + bsf_ctx = bsf_ctx->next; + first = 0;
You could do slightly simpler with either these solutions:
av_log(..., "%s%s", name, bsf_ctx->next ? "," : "");
const char *delim = ""; av_log(..., "%s%s", delim, name); delim = ",";
Done. [...] Patch updated with documentation. -- FFmpeg = Faithful Fancy Mastodontic Proud Elitarian Glue
Le tridi 13 thermidor, an CCXXI, Stefano Sabatini a écrit :
Changed to: dumo_extra+a,mp4toannexb+v.
I believe you missed my point: I meant: bsfs:a=dump_extra,bsfs:v=mp4toannexb But I realize that it is not possible as ':' is not an authorized option character. "bsfs.a=dump_extra" would be possible, though.
Not sure what you mean, I'm already duping it.
I'll explain later. (Run!)
diff --git a/doc/muxers.texi b/doc/muxers.texi index b0dc7e6..7c5a1c8 100644 --- a/doc/muxers.texi +++ b/doc/muxers.texi @@ -818,11 +818,34 @@ leading or trailing spaces or any special character, it must be escaped (see the ``Quoting and escaping'' section in the ffmpeg-utils manual).
-Options can be specified for each slave by prepending them as a list of +Muxer options can be specified for each slave by prepending them as a list of @var{key}=@var{value} pairs separated by ':', between square brackets. If the options values contain a special character or the ':' separator, they must be escaped; note that this is a second level escaping.
+The following special options are also recognized: +@table @option +@item f +Specify the format name. Useful if it cannot be guessed from the +output name suffix. + +@item bsfs +Specify a list of bitstream filters to apply to the specified +output. It is possible to specify to which streams a given bitstream +filter applies, by appending a stream specifier to a bitstream filter +followed by a "+". If the stream specifier is not specified, it will +be applied to all streams in the output. + +Several bitstream filters can be specified, separated each other by +",". + +The BNF description of the bitstream filters specification is given by: +@example +@var{BSF} ::= @var{BSF_NAME}[+@var{STREAM_SPECIFIER}] +@var{BSFS} ::= @var{BSF}[,@var{BSFS}] +@end example +@end table + Example: encode something and both archive it in a WebM file and stream it as MPEG-TS over UDP (the streams need to be explicitly mapped):
diff --git a/libavformat/tee.c b/libavformat/tee.c index 7b8b371..ebcee9f 100644 --- a/libavformat/tee.c +++ b/libavformat/tee.c @@ -27,10 +27,17 @@
#define MAX_SLAVES 16
+typedef struct { + AVFormatContext *fmt_ctx; + char *bsfs; + AVBitStreamFilterContext **bsf_ctxs; ///< bitstream filters per stream +} TeeSlave; + typedef struct TeeContext { const AVClass *class; unsigned nb_slaves; - AVFormatContext *slaves[MAX_SLAVES]; + TeeSlave slaves[MAX_SLAVES]; + char *bsfs; } TeeContext;
static const char *const slave_delim = "|"; @@ -82,13 +89,99 @@ fail: return ret; }
-static int open_slave(AVFormatContext *avf, char *slave, AVFormatContext **ravf) +/** + * Parse bitstream filter, in the form: + * BSF ::= BSF_NAME[+STREAM_SPECIFIER] + */ +static int parse_bsf(void *log_ctx, char *bsf, + AVFormatContext *fmt_ctx, + AVBitStreamFilterContext **bsf_ctxs) +{ + int i, ret = 0; + char *bsf1 = av_strdup(bsf); + char *bsf_name, *spec; +
+ if (!bsf1) + return AVERROR(ENOMEM);
Remove that hunk and work directly with bsf (see below).
+ bsf_name = av_strtok(bsf1, "+", &spec); + + /* select the streams matched by the specifier */ + for (i = 0; i < fmt_ctx->nb_streams; i++) { + AVBitStreamFilterContext *bsf_ctx; + + if (spec && spec[0]) { + ret = avformat_match_stream_specifier(fmt_ctx, fmt_ctx->streams[i], spec); + if (ret < 0) { + av_log(log_ctx, AV_LOG_ERROR, + "Invalid stream specifier for bitstream filter '%s'\n", bsf); + goto end; + } + + if (ret == 0) /* no match */ + continue; + } + + bsf_ctx = av_bitstream_filter_init(bsf_name); + if (!bsf_ctx) { + av_log(log_ctx, AV_LOG_ERROR, + "Cannot create bitstream filter with name '%s' for stream %d, " + "unknown filter or internal error happened\n", + bsf_name, i); + ret = AVERROR_UNKNOWN; + goto end; + } + + /* append bsf context to the list of per-stream bsf contexts */ + if (!bsf_ctxs[i]) { + bsf_ctxs[i] = bsf_ctx; + } else { + AVBitStreamFilterContext *bsf_ctx1 = bsf_ctxs[i]; + while (bsf_ctx1->next) + bsf_ctx1 = bsf_ctx1->next; + bsf_ctx1->next = bsf_ctx; + } + } + +end: + av_free(bsf1); + return ret; +} + +/** + * Parse list of bitstream filters in the form: + * BSFS ::= BSF[,BSFS]
+ * BSF ::= BSF_NAME[:STREAM_SPECIFIER]
Not relevant for this function (and obsolete).
+ */ +static int parse_slave_bsfs(void *log_ctx, char *slave_bsfs, + AVFormatContext *fmt_ctx, + AVBitStreamFilterContext **bsf_ctx) +{ + char *bsf, *slave_bsfs1 = av_strdup(slave_bsfs); + char *saveptr, *buf = slave_bsfs1; + int ret; +
+ if (!slave_bsfs1) + return AVERROR(ENOMEM);
The string is already strdupped here, there is no need to re-dup it again in parse_bsf().
+ + while (bsf = av_strtok(buf, ",", &saveptr)) { + buf = NULL; + ret = parse_bsf(log_ctx, bsf, fmt_ctx, bsf_ctx); + if (ret < 0) + goto end; + } + +end: + av_free(slave_bsfs1); + return ret; +} + +static int open_slave(AVFormatContext *avf, char *slave, TeeSlave *tee_slave) { int i, ret; AVDictionary *options = NULL; AVDictionaryEntry *entry; char *filename; - char *format = NULL; + char *format = NULL, *bsfs = NULL; AVFormatContext *avf2 = NULL; AVStream *st, *st2;
@@ -99,6 +192,11 @@ static int open_slave(AVFormatContext *avf, char *slave, AVFormatContext **ravf) entry->value = NULL; /* prevent it from being freed */ av_dict_set(&options, "f", NULL, 0); }
+ if ((entry = av_dict_get(options, "bsfs", NULL, 0))) { + bsfs = entry->value; + entry->value = NULL; /* prevent it from being freed */ + av_dict_set(&options, "bsfs", NULL, 0);
Could be factored with the "f" case into a single function steal_option(&options, "bsfs") or something (unless you go for the "bsfs.a=...:bsfs.v=..." syntax). But not really important.
+ }
ret = avformat_alloc_output_context2(&avf2, NULL, format, filename); if (ret < 0) @@ -138,6 +236,7 @@ static int open_slave(AVFormatContext *avf, char *slave, AVFormatContext **ravf) slave, av_err2str(ret)); goto fail; } + if (options) { entry = NULL; while ((entry = av_dict_get(options, "", entry, AV_DICT_IGNORE_SUFFIX))) @@ -146,7 +245,17 @@ static int open_slave(AVFormatContext *avf, char *slave, AVFormatContext **ravf) goto fail; }
- *ravf = avf2; + tee_slave->fmt_ctx = avf2;
+ tee_slave->bsf_ctxs = av_mallocz(avf2->nb_streams * sizeof(*(tee_slave->bsf_ctxs)));
av_calloc to avoid overflows, however unlikely they are. And please keep lines below 80 characters whenever conveniently possible.
+ if (!tee_slave->bsf_ctxs) + return AVERROR(ENOMEM); + + /* generate the array of bitstream filters */ + if (bsfs) {
+ tee_slave->bsfs = bsfs;
It seems to me all this string is sit there doing nothing waiting to be freed. Maybe drop the field and free it here. That would also allow to remove the strdup in parse_slave_bsfs().
+ if ((ret = parse_slave_bsfs(avf, bsfs, avf2, tee_slave->bsf_ctxs)) < 0) + return ret; + } return 0;
fail: @@ -158,14 +267,48 @@ static void close_slaves(AVFormatContext *avf) { TeeContext *tee = avf->priv_data; AVFormatContext *avf2; - unsigned i; + unsigned i, j;
for (i = 0; i < tee->nb_slaves; i++) { - avf2 = tee->slaves[i]; + avf2 = tee->slaves[i].fmt_ctx; + + for (j = 0; j < avf2->nb_streams; j++) { + AVBitStreamFilterContext *bsf_ctx = tee->slaves[i].bsf_ctxs[j]; + while (bsf_ctx) { + bsf_ctx = bsf_ctx->next; + av_bitstream_filter_close(bsf_ctx); + } + av_freep(&tee->slaves[i].bsfs); + } + avio_close(avf2->pb); avf2->pb = NULL; avformat_free_context(avf2); - tee->slaves[i] = NULL; + tee->slaves[i].fmt_ctx = NULL; + } +} + +static void log_slave(TeeSlave *slave, void *log_ctx, int log_level) +{ + int i; + av_log(log_ctx, log_level, "filename:'%s' format:%s\n", + slave->fmt_ctx->filename, slave->fmt_ctx->oformat->name); + for (i = 0; i < slave->fmt_ctx->nb_streams; i++) { + AVStream *st = slave->fmt_ctx->streams[i]; + AVBitStreamFilterContext *bsf_ctx = slave->bsf_ctxs[i]; + + av_log(log_ctx, log_level, " stream:%d codec:%s type:%s", + i, avcodec_get_name(st->codec->codec_id), + av_get_media_type_string(st->codec->codec_type)); + if (bsf_ctx) { + av_log(log_ctx, log_level, " bsfs:"); + while (bsf_ctx) { + av_log(log_ctx, log_level, "%s%s", + bsf_ctx->filter->name, bsf_ctx->next ? "," : ""); + bsf_ctx = bsf_ctx->next; + } + } + av_log(log_ctx, log_level, "\n"); } }
@@ -195,6 +338,7 @@ static int tee_write_header(AVFormatContext *avf) for (i = 0; i < nb_slaves; i++) { if ((ret = open_slave(avf, slaves[i], &tee->slaves[i])) < 0) goto fail; + log_slave(&tee->slaves[i], avf, AV_LOG_VERBOSE); av_freep(&slaves[i]); }
@@ -208,6 +352,50 @@ fail: return ret; }
+static int filter_packet(void *log_ctx, AVPacket *pkt, + AVFormatContext *fmt_ctx, AVBitStreamFilterContext *bsf_ctx) +{
I do not know this API very well.
+ AVCodecContext *enc_ctx = fmt_ctx->streams[pkt->stream_index]->codec; + + while (bsf_ctx) { + AVPacket new_pkt = *pkt; + int ret = av_bitstream_filter_filter(bsf_ctx, enc_ctx, NULL, + &new_pkt.data, &new_pkt.size, + pkt->data, pkt->size, + pkt->flags & AV_PKT_FLAG_KEY); + if (ret == 0 && new_pkt.data != pkt->data && new_pkt.destruct) { + uint8_t *buf = av_malloc(new_pkt.size + FF_INPUT_BUFFER_PADDING_SIZE); + if (buf) {
+ memcpy(buf, new_pkt.data, new_pkt.size); + memset(buf + new_pkt.size, 0, FF_INPUT_BUFFER_PADDING_SIZE); + new_pkt.data = buf; + new_pkt.buf = NULL; + ret = 1;
av_copy_packet?
+ } else
Nit: please either braces on both branches or none.
+ ret = AVERROR(ENOMEM);
The error path seems a bit convoluted. goto fail?
+ } + + if (ret > 0) { + av_free_packet(pkt); + new_pkt.buf = av_buffer_create(new_pkt.data, new_pkt.size, + av_buffer_default_free, NULL, 0); + if (!new_pkt.buf) + return AVERROR(ENOMEM); + } else if (ret < 0) { + av_log(log_ctx, AV_LOG_ERROR, + "Failed to filter bitstream with filter %s for stream %d in file '%s' with codec %s\n", + bsf_ctx->filter->name, pkt->stream_index, fmt_ctx->filename, + avcodec_get_name(enc_ctx->codec_id)); + return ret; + } + *pkt = new_pkt; + + bsf_ctx = bsf_ctx->next; + } + + return 0; +} + static int tee_write_trailer(AVFormatContext *avf) { TeeContext *tee = avf->priv_data; @@ -216,7 +404,7 @@ static int tee_write_trailer(AVFormatContext *avf) unsigned i;
for (i = 0; i < tee->nb_slaves; i++) { - avf2 = tee->slaves[i]; + avf2 = tee->slaves[i].fmt_ctx; if ((ret = av_write_trailer(avf2)) < 0) if (!ret_all) ret_all = ret; @@ -241,7 +429,7 @@ static int tee_write_packet(AVFormatContext *avf, AVPacket *pkt) AVRational tb, tb2;
for (i = 0; i < tee->nb_slaves; i++) { - avf2 = tee->slaves[i]; + avf2 = tee->slaves[i].fmt_ctx; s = pkt->stream_index; if (s >= avf2->nb_streams) { if (!ret_all) @@ -259,6 +447,8 @@ static int tee_write_packet(AVFormatContext *avf, AVPacket *pkt) 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); + + filter_packet(avf2, &pkt2, avf2, tee->slaves[i].bsf_ctxs[s]); if ((ret = av_interleaved_write_frame(avf2, &pkt2)) < 0) if (!ret_all) ret_all = ret;
Regards, -- Nicolas George
On date Thursday 2013-08-01 14:36:21 +0200, Nicolas George encoded:
Le tridi 13 thermidor, an CCXXI, Stefano Sabatini a écrit :
Changed to: dumo_extra+a,mp4toannexb+v.
I believe you missed my point: I meant:
bsfs:a=dump_extra,bsfs:v=mp4toannexb
But I realize that it is not possible as ':' is not an authorized option character. "bsfs.a=dump_extra" would be possible, though.
The problem is that such mechanism is awkward implementation-wise, I'd need to iterate the options and match all the options which look like bsfs.SPEC, store them and then open the filters after the various streams have been opened. One problem is that you need to specify the option multiple times, for example: bsf.a=dump_extra:bsf.v=mp4toannexb with my proposed syntax: bsfs=dump_extra+a,mp4annexb+v which is shorter and IMO at least as much as readable, the only advantage of your syntax is that it *resembles* specific ffmpeg syntax, which I consider a marginal gain in this case. The other problem is that once the dictionary has been parsed, you lose the order of application of the filters, which could be practically relevant. (Note also that my syntax can be easily extended in case the need arise to support filter arguments: filter+spec=args). What we need is a specification format to set a sequence of bitstream filters, each one of which can be applied to a selected subset of streams. If such specification is not exploded in multiple options, that's better from my point of view. [...] -- FFmpeg = Foolish & Fast Murdering Patchable Exxagerate Gymnast
On date Thursday 2013-08-01 18:32:59 +0200, Stefano Sabatini encoded:
On date Thursday 2013-08-01 14:36:21 +0200, Nicolas George encoded:
Le tridi 13 thermidor, an CCXXI, Stefano Sabatini a écrit :
Changed to: dumo_extra+a,mp4toannexb+v.
I believe you missed my point: I meant:
bsfs:a=dump_extra,bsfs:v=mp4toannexb
But I realize that it is not possible as ':' is not an authorized option character. "bsfs.a=dump_extra" would be possible, though.
The problem is that such mechanism is awkward implementation-wise, I'd need to iterate the options and match all the options which look like bsfs.SPEC, store them and then open the filters after the various streams have been opened.
Elaborating more on this. I'd need to store the couples specifiers+filters in a dictionary (this destroys the specification order and is a bit more complicate to implement). I leave the design choice to the maintainer.
One problem is that you need to specify the option multiple times, for example: bsf.a=dump_extra:bsf.v=mp4toannexb
with my proposed syntax: bsfs=dump_extra+a,mp4annexb+v
which is shorter and IMO at least as much as readable, the only advantage of your syntax is that it *resembles* specific ffmpeg syntax, which I consider a marginal gain in this case.
The other problem is that once the dictionary has been parsed, you lose the order of application of the filters, which could be practically relevant.
(Note also that my syntax can be easily extended in case the need arise to support filter arguments: filter+spec=args).
What we need is a specification format to set a sequence of bitstream filters, each one of which can be applied to a selected subset of streams. If such specification is not exploded in multiple options, that's better from my point of view. -- FFmpeg = Formidable and Fancy Moronic Purposeless Elfic Guide
On date Monday 2013-08-05 12:23:54 +0200, Stefano Sabatini encoded:
On date Thursday 2013-08-01 18:32:59 +0200, Stefano Sabatini encoded:
On date Thursday 2013-08-01 14:36:21 +0200, Nicolas George encoded:
Le tridi 13 thermidor, an CCXXI, Stefano Sabatini a écrit :
Changed to: dumo_extra+a,mp4toannexb+v.
I believe you missed my point: I meant:
bsfs:a=dump_extra,bsfs:v=mp4toannexb
But I realize that it is not possible as ':' is not an authorized option character. "bsfs.a=dump_extra" would be possible, though.
The problem is that such mechanism is awkward implementation-wise, I'd need to iterate the options and match all the options which look like bsfs.SPEC, store them and then open the filters after the various streams have been opened.
Elaborating more on this. I'd need to store the couples specifiers+filters in a dictionary (this destroys the specification order and is a bit more complicate to implement). I leave the design choice to the maintainer.
Ping. -- FFmpeg = Fundamentalist and Foolish Monstrous Philosofic Evil Gadget
L'octidi 18 thermidor, an CCXXI, Stefano Sabatini a écrit :
Elaborating more on this. I'd need to store the couples specifiers+filters in a dictionary (this destroys the specification order and is a bit more complicate to implement). I leave the design choice to the maintainer.
Sorry for the delay, I was assuming you wanted to rework the patch first. To answer that point, I do not see why you would need to store anything in a dictionary. I believe it can be done with something like that: while (entry = av_opt_get(options, "bsfs", IGNORE_SUFFIX)) { spec = entry->name + strlen("bsfs"); for (i = 0; i < nb_streams; i++) { if (stream matches stream specifier) { if (stream[i]->bsfs) { av_log(WARNING, "Duplicate bsfs specification\n"); continue; } parse_bsfs(stream[i], entry->value); } entry->value = NULL; av_opt_set(entry->name, NULL); } } Now that I think on it, I see the added benefit of matching the syntax of ffmpeg itself for specifying bitstream filters: ffmpeg ... -bsfs:a mp3_header_compress,noise -bsfs:v noise,mjpeg2jpeg ... and not: ffmpeg ... -bsfs mp3_header_compress:a,noise,mjpeg2jpeg:v ... It is a little more verbose when some filters are common to all streams and some are not, but I also believe it makes it less readable. Regards, -- Nicolas George
On date Wednesday 2013-08-07 19:39:16 +0200, Nicolas George encoded:
L'octidi 18 thermidor, an CCXXI, Stefano Sabatini a écrit :
Elaborating more on this. I'd need to store the couples specifiers+filters in a dictionary (this destroys the specification order and is a bit more complicate to implement). I leave the design choice to the maintainer.
Sorry for the delay, I was assuming you wanted to rework the patch first.
To answer that point, I do not see why you would need to store anything in a dictionary. I believe it can be done with something like that:
while (entry = av_opt_get(options, "bsfs", IGNORE_SUFFIX)) { spec = entry->name + strlen("bsfs"); for (i = 0; i < nb_streams; i++) { if (stream matches stream specifier) { if (stream[i]->bsfs) { av_log(WARNING, "Duplicate bsfs specification\n"); continue; } parse_bsfs(stream[i], entry->value); } entry->value = NULL; av_opt_set(entry->name, NULL); } }
Now that I think on it, I see the added benefit of matching the syntax of ffmpeg itself for specifying bitstream filters:
ffmpeg ... -bsfs:a mp3_header_compress,noise -bsfs:v noise,mjpeg2jpeg ...
Check updated patch. -- FFmpeg = Free Fierce Magical Political Ecstatic Gadget
On date Thursday 2013-08-08 12:21:51 +0200, Stefano Sabatini encoded:
On date Wednesday 2013-08-07 19:39:16 +0200, Nicolas George encoded:
L'octidi 18 thermidor, an CCXXI, Stefano Sabatini a écrit :
Elaborating more on this. I'd need to store the couples specifiers+filters in a dictionary (this destroys the specification order and is a bit more complicate to implement). I leave the design choice to the maintainer.
Sorry for the delay, I was assuming you wanted to rework the patch first.
To answer that point, I do not see why you would need to store anything in a dictionary. I believe it can be done with something like that:
while (entry = av_opt_get(options, "bsfs", IGNORE_SUFFIX)) { spec = entry->name + strlen("bsfs"); for (i = 0; i < nb_streams; i++) { if (stream matches stream specifier) { if (stream[i]->bsfs) { av_log(WARNING, "Duplicate bsfs specification\n"); continue; } parse_bsfs(stream[i], entry->value); } entry->value = NULL; av_opt_set(entry->name, NULL); } }
Now that I think on it, I see the added benefit of matching the syntax of ffmpeg itself for specifying bitstream filters:
ffmpeg ... -bsfs:a mp3_header_compress,noise -bsfs:v noise,mjpeg2jpeg ...
Check updated patch. -- FFmpeg = Free Fierce Magical Political Ecstatic Gadget
From 86c47df8afe3ee6b264272d25ec02da9814c3684 Mon Sep 17 00:00:00 2001 From: Stefano Sabatini <stefasab@gmail.com> Date: Fri, 5 Jul 2013 18:33:30 +0200 Subject: [PATCH] lavf/tee: add support for bitstream filtering
This allows to apply different bitstream filters to different outputs, with no need to trancode.
TODO: bump minor --- doc/muxers.texi | 19 ++++- libavformat/tee.c | 217 +++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 216 insertions(+), 20 deletions(-)
Ping. -- FFmpeg = Frenzy Friendly Mythic Power Erudite Gadget
Le primidi 21 thermidor, an CCXXI, Stefano Sabatini a écrit :
Check updated patch.
+ TeeSlave slaves[MAX_SLAVES]; + char *bsfs;
+ if ((entry = av_dict_get(options, "bsfs", NULL, 0))) { + bsfs = entry->value; + entry->value = NULL; /* prevent it from being freed */ + av_dict_set(&options, "bsfs", NULL, 0); + }
+ entry = NULL; + while (entry = av_dict_get(options, "bsfs", NULL, AV_DICT_IGNORE_SUFFIX)) { + const char *spec = entry->key + strlen("bsfs");
I am a bit confused: it looks to me like several versions of the patch were mixed together. Rebase conflicts? Regards, -- Nicolas George
On date Monday 2013-08-12 12:12:13 +0200, Nicolas George encoded:
Le primidi 21 thermidor, an CCXXI, Stefano Sabatini a écrit :
Check updated patch.
+ TeeSlave slaves[MAX_SLAVES]; + char *bsfs;
+ if ((entry = av_dict_get(options, "bsfs", NULL, 0))) { + bsfs = entry->value; + entry->value = NULL; /* prevent it from being freed */ + av_dict_set(&options, "bsfs", NULL, 0); + }
+ entry = NULL; + while (entry = av_dict_get(options, "bsfs", NULL, AV_DICT_IGNORE_SUFFIX)) { + const char *spec = entry->key + strlen("bsfs");
I am a bit confused: it looks to me like several versions of the patch were mixed together. Rebase conflicts?
Yes, this was partly working and thus I didn't noticed it. Patch updated, sorry. -- FFmpeg = Faithless & Fostering Muttering Purposeless Elaborated Gem
Le sextidi 26 thermidor, an CCXXI, Stefano Sabatini a écrit :
This allows to apply different bitstream filters to different outputs, with no need to trancode.
TODO: bump minor --- doc/muxers.texi | 19 ++++- libavformat/tee.c | 208 +++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 208 insertions(+), 19 deletions(-)
diff --git a/doc/muxers.texi b/doc/muxers.texi index d204188..0f4db3c 100644 --- a/doc/muxers.texi +++ b/doc/muxers.texi @@ -852,11 +852,28 @@ leading or trailing spaces or any special character, it must be escaped (see the ``Quoting and escaping'' section in the ffmpeg-utils manual).
-Options can be specified for each slave by prepending them as a list of +Muxer options can be specified for each slave by prepending them as a list of @var{key}=@var{value} pairs separated by ':', between square brackets. If the options values contain a special character or the ':' separator, they must be escaped; note that this is a second level escaping.
+The following special options are also recognized: +@table @option +@item f +Specify the format name. Useful if it cannot be guessed from the +output name suffix. + +@item bsfs[/@var{spec}] +Specify a list of bitstream filters to apply to the specified +output. It is possible to specify to which streams a given bitstream +filter applies, by appending a stream specifier to the option +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 ",". +@end example +@end table + Example: encode something and both archive it in a WebM file and stream it as MPEG-TS over UDP (the streams need to be explicitly mapped):
diff --git a/libavformat/tee.c b/libavformat/tee.c index 7b8b371..e959731 100644 --- a/libavformat/tee.c +++ b/libavformat/tee.c @@ -27,10 +27,15 @@
#define MAX_SLAVES 16
+typedef struct { + AVFormatContext *avf; + AVBitStreamFilterContext **bsfs; ///< bitstream filters per stream +} TeeSlave; + typedef struct TeeContext { const AVClass *class; unsigned nb_slaves; - AVFormatContext *slaves[MAX_SLAVES]; + TeeSlave slaves[MAX_SLAVES]; } TeeContext;
static const char *const slave_delim = "|"; @@ -82,7 +87,53 @@ fail: return ret; }
-static int open_slave(AVFormatContext *avf, char *slave, AVFormatContext **ravf) +/** + * Parse list of bitstream filters and add them to the list of filters + * pointed to by bsfs. + * + * The list must be specified in the form: + * BSFS ::= BSF[,BSFS] + */ +static int parse_bsfs(void *log_ctx, const char *bsfs_spec, + AVBitStreamFilterContext **bsfs) +{ + char *bsf_name, *buf, *saveptr; + int ret; + + if (!(buf = av_strdup(bsfs_spec))) + return AVERROR(ENOMEM); + + while (bsf_name = av_strtok(buf, ",", &saveptr)) { + AVBitStreamFilterContext *bsf = av_bitstream_filter_init(bsf_name); + + if (!bsf) { + av_log(log_ctx, AV_LOG_ERROR, + "Cannot initialize bitstream filter with name '%s', " + "unknown filter or internal error happened\n", + bsf_name); + ret = AVERROR_UNKNOWN; + goto end; + } +
+ /* append bsf context to the list of bsf contexts */ + if (!*bsfs) { + *bsfs = bsf; + } else { + AVBitStreamFilterContext *bsf1 = *bsfs; + while (bsf1->next) + bsf1 = bsf1->next; + bsf1->next = bsf; + }
Simpler: *bsfs = bsf; bsfs = &bsf->next;
+ + buf = NULL; + } + +end: + av_free(buf); + return ret; +} + +static int open_slave(AVFormatContext *avf, char *slave, TeeSlave *tee_slave) { int i, ret; AVDictionary *options = NULL; @@ -102,14 +153,13 @@ static int open_slave(AVFormatContext *avf, char *slave, AVFormatContext **ravf)
ret = avformat_alloc_output_context2(&avf2, NULL, format, filename); if (ret < 0) - goto fail; - av_free(format); + goto end;
for (i = 0; i < avf->nb_streams; i++) { st = avf->streams[i]; if (!(st2 = avformat_new_stream(avf2, NULL))) { ret = AVERROR(ENOMEM); - goto fail; + goto end; } st2->id = st->id; st2->r_frame_rate = st->r_frame_rate; @@ -122,34 +172,79 @@ static int open_slave(AVFormatContext *avf, char *slave, AVFormatContext **ravf) st2->avg_frame_rate = st->avg_frame_rate; av_dict_copy(&st2->metadata, st->metadata, 0); if ((ret = avcodec_copy_context(st2->codec, st->codec)) < 0) - goto fail; + goto end; }
if (!(avf2->oformat->flags & AVFMT_NOFILE)) { if ((ret = avio_open(&avf2->pb, filename, AVIO_FLAG_WRITE)) < 0) { av_log(avf, AV_LOG_ERROR, "Slave '%s': error opening: %s\n", slave, av_err2str(ret)); - goto fail; + goto end; } }
if ((ret = avformat_write_header(avf2, &options)) < 0) { av_log(avf, AV_LOG_ERROR, "Slave '%s': error writing header: %s\n", slave, av_err2str(ret)); - goto fail; + goto end; + } + + tee_slave->avf = avf2; + tee_slave->bsfs = av_calloc(avf2->nb_streams, sizeof(TeeSlave)); + if (!tee_slave->bsfs) { + ret = AVERROR(ENOMEM); + goto end; + }
+ memset(tee_slave->bsfs, 0, avf2->nb_streams * sizeof(TeeSlave));
Unnecessary, calloc already does it.
+ + entry = NULL; + while (entry = av_dict_get(options, "bsfs", NULL, AV_DICT_IGNORE_SUFFIX)) { + const char *spec = entry->key + strlen("bsfs");
+ if (*spec) + spec++; /* consume separator */
Possibly: check that this is indeed the separator.
+ + for (i = 0; i < avf2->nb_streams; i++) { + ret = avformat_match_stream_specifier(avf2, avf2->streams[i], spec); + if (ret < 0) { + av_log(avf, AV_LOG_ERROR, + "Invalid stream specifier '%s' in bsfs option '%s' for slave " + "output '%s'\n", spec, entry->key, filename); + goto end; + } + + if (ret > 0) { + av_log(avf, AV_LOG_DEBUG, "spec:%s bsfs:%s matches stream %d of slave " + "output '%s'\n", spec, entry->value, i, filename); + if (tee_slave->bsfs[i]) { + av_log(avf, AV_LOG_WARNING, + "Duplicate bsfs specification associated to stream %d of slave " + "output '%s', filters will be ignored\n", i, filename); + continue; + } + ret = parse_bsfs(avf, entry->value, &tee_slave->bsfs[i]); + if (ret < 0) { + av_log(avf, AV_LOG_ERROR, + "Error parsing bitstream filter sequence '%s' associated to " + "stream %d of slave output '%s'\n", entry->value, i, filename); + goto end; + } + } + } +
+ entry->value = NULL; + av_dict_set(&options, entry->key, NULL, 0);
I suspect entry->value is leaking now.
} + if (options) { entry = NULL; while ((entry = av_dict_get(options, "", entry, AV_DICT_IGNORE_SUFFIX))) av_log(avf2, AV_LOG_ERROR, "Unknown option '%s'\n", entry->key); ret = AVERROR_OPTION_NOT_FOUND; - goto fail; + goto end; }
- *ravf = avf2; - return 0; - -fail: +end: + av_free(format); av_dict_free(&options); return ret; } @@ -158,14 +253,48 @@ static void close_slaves(AVFormatContext *avf) { TeeContext *tee = avf->priv_data; AVFormatContext *avf2; - unsigned i; + unsigned i, j;
for (i = 0; i < tee->nb_slaves; i++) { - avf2 = tee->slaves[i]; + avf2 = tee->slaves[i].avf; + + for (j = 0; j < avf2->nb_streams; j++) { + AVBitStreamFilterContext *bsf_next, *bsf = tee->slaves[i].bsfs[j]; + while (bsf) { + bsf_next = bsf->next; + av_bitstream_filter_close(bsf); + bsf = bsf_next; + } + } + avio_close(avf2->pb); avf2->pb = NULL; avformat_free_context(avf2); - tee->slaves[i] = NULL; + tee->slaves[i].avf = NULL; + } +} + +static void log_slave(TeeSlave *slave, void *log_ctx, int log_level) +{ + int i; + av_log(log_ctx, log_level, "filename:'%s' format:%s\n", + slave->avf->filename, slave->avf->oformat->name); + for (i = 0; i < slave->avf->nb_streams; i++) { + AVStream *st = slave->avf->streams[i]; + AVBitStreamFilterContext *bsf = slave->bsfs[i]; + + av_log(log_ctx, log_level, " stream:%d codec:%s type:%s", + i, avcodec_get_name(st->codec->codec_id), + av_get_media_type_string(st->codec->codec_type)); + if (bsf) { + av_log(log_ctx, log_level, " bsfs:"); + while (bsf) { + av_log(log_ctx, log_level, "%s%s", + bsf->filter->name, bsf->next ? "," : ""); + bsf = bsf->next; + } + } + av_log(log_ctx, log_level, "\n"); } }
@@ -195,6 +324,7 @@ static int tee_write_header(AVFormatContext *avf) for (i = 0; i < nb_slaves; i++) { if ((ret = open_slave(avf, slaves[i], &tee->slaves[i])) < 0) goto fail; + log_slave(&tee->slaves[i], avf, AV_LOG_VERBOSE); av_freep(&slaves[i]); }
@@ -208,6 +338,46 @@ fail: return ret; }
+static int filter_packet(void *log_ctx, AVPacket *pkt, + AVFormatContext *fmt_ctx, AVBitStreamFilterContext *bsf_ctx) +{ + AVCodecContext *enc_ctx = fmt_ctx->streams[pkt->stream_index]->codec; + int ret; + + while (bsf_ctx) { + AVPacket new_pkt = *pkt; + ret = av_bitstream_filter_filter(bsf_ctx, enc_ctx, NULL, + &new_pkt.data, &new_pkt.size, + pkt->data, pkt->size, + pkt->flags & AV_PKT_FLAG_KEY); + if (ret == 0 && new_pkt.data != pkt->data && new_pkt.destruct) { + if ((ret = av_copy_packet(&new_pkt, pkt)) < 0) + break; + ret = 1; + } + + if (ret > 0) { + av_free_packet(pkt); + new_pkt.buf = av_buffer_create(new_pkt.data, new_pkt.size, + av_buffer_default_free, NULL, 0); + if (!new_pkt.buf) + break; + } + *pkt = new_pkt; + + bsf_ctx = bsf_ctx->next; + } + + if (ret < 0) { + av_log(log_ctx, AV_LOG_ERROR, + "Failed to filter bitstream with filter %s for stream %d in file '%s' with codec %s\n", + bsf_ctx->filter->name, pkt->stream_index, fmt_ctx->filename, + avcodec_get_name(enc_ctx->codec_id)); + } + + return ret; +} + static int tee_write_trailer(AVFormatContext *avf) { TeeContext *tee = avf->priv_data; @@ -216,7 +386,7 @@ static int tee_write_trailer(AVFormatContext *avf) unsigned i;
for (i = 0; i < tee->nb_slaves; i++) { - avf2 = tee->slaves[i]; + avf2 = tee->slaves[i].avf; if ((ret = av_write_trailer(avf2)) < 0) if (!ret_all) ret_all = ret; @@ -241,7 +411,7 @@ static int tee_write_packet(AVFormatContext *avf, AVPacket *pkt) AVRational tb, tb2;
for (i = 0; i < tee->nb_slaves; i++) { - avf2 = tee->slaves[i]; + avf2 = tee->slaves[i].avf; s = pkt->stream_index; if (s >= avf2->nb_streams) { if (!ret_all) @@ -259,6 +429,8 @@ static int tee_write_packet(AVFormatContext *avf, AVPacket *pkt) 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); + + filter_packet(avf2, &pkt2, avf2, tee->slaves[i].bsfs[s]); if ((ret = av_interleaved_write_frame(avf2, &pkt2)) < 0) if (!ret_all) ret_all = ret;
No other remarks, thanks. Regards, -- Nicolas George
On date Friday 2013-08-16 12:40:48 +0200, Nicolas George encoded:
Le sextidi 26 thermidor, an CCXXI, Stefano Sabatini a écrit :
This allows to apply different bitstream filters to different outputs, with no need to trancode.
TODO: bump minor --- doc/muxers.texi | 19 ++++- libavformat/tee.c | 208 +++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 208 insertions(+), 19 deletions(-)
diff --git a/doc/muxers.texi b/doc/muxers.texi index d204188..0f4db3c 100644 --- a/doc/muxers.texi +++ b/doc/muxers.texi @@ -852,11 +852,28 @@ leading or trailing spaces or any special character, it must be escaped (see the ``Quoting and escaping'' section in the ffmpeg-utils manual).
-Options can be specified for each slave by prepending them as a list of +Muxer options can be specified for each slave by prepending them as a list of @var{key}=@var{value} pairs separated by ':', between square brackets. If the options values contain a special character or the ':' separator, they must be escaped; note that this is a second level escaping.
+The following special options are also recognized: +@table @option +@item f +Specify the format name. Useful if it cannot be guessed from the +output name suffix. + +@item bsfs[/@var{spec}] +Specify a list of bitstream filters to apply to the specified +output. It is possible to specify to which streams a given bitstream +filter applies, by appending a stream specifier to the option +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 ",". +@end example +@end table + Example: encode something and both archive it in a WebM file and stream it as MPEG-TS over UDP (the streams need to be explicitly mapped):
diff --git a/libavformat/tee.c b/libavformat/tee.c index 7b8b371..e959731 100644 --- a/libavformat/tee.c +++ b/libavformat/tee.c @@ -27,10 +27,15 @@
#define MAX_SLAVES 16
+typedef struct { + AVFormatContext *avf; + AVBitStreamFilterContext **bsfs; ///< bitstream filters per stream +} TeeSlave; + typedef struct TeeContext { const AVClass *class; unsigned nb_slaves; - AVFormatContext *slaves[MAX_SLAVES]; + TeeSlave slaves[MAX_SLAVES]; } TeeContext;
static const char *const slave_delim = "|"; @@ -82,7 +87,53 @@ fail: return ret; }
-static int open_slave(AVFormatContext *avf, char *slave, AVFormatContext **ravf) +/** + * Parse list of bitstream filters and add them to the list of filters + * pointed to by bsfs. + * + * The list must be specified in the form: + * BSFS ::= BSF[,BSFS] + */ +static int parse_bsfs(void *log_ctx, const char *bsfs_spec, + AVBitStreamFilterContext **bsfs) +{ + char *bsf_name, *buf, *saveptr; + int ret; + + if (!(buf = av_strdup(bsfs_spec))) + return AVERROR(ENOMEM); + + while (bsf_name = av_strtok(buf, ",", &saveptr)) { + AVBitStreamFilterContext *bsf = av_bitstream_filter_init(bsf_name); + + if (!bsf) { + av_log(log_ctx, AV_LOG_ERROR, + "Cannot initialize bitstream filter with name '%s', " + "unknown filter or internal error happened\n", + bsf_name); + ret = AVERROR_UNKNOWN; + goto end; + } +
+ /* append bsf context to the list of bsf contexts */ + if (!*bsfs) { + *bsfs = bsf; + } else { + AVBitStreamFilterContext *bsf1 = *bsfs; + while (bsf1->next) + bsf1 = bsf1->next; + bsf1->next = bsf; + }
Simpler:
*bsfs = bsf; bsfs = &bsf->next;
+ + buf = NULL; + } + +end: + av_free(buf); + return ret; +} + +static int open_slave(AVFormatContext *avf, char *slave, TeeSlave *tee_slave) { int i, ret; AVDictionary *options = NULL; @@ -102,14 +153,13 @@ static int open_slave(AVFormatContext *avf, char *slave, AVFormatContext **ravf)
ret = avformat_alloc_output_context2(&avf2, NULL, format, filename); if (ret < 0) - goto fail; - av_free(format); + goto end;
for (i = 0; i < avf->nb_streams; i++) { st = avf->streams[i]; if (!(st2 = avformat_new_stream(avf2, NULL))) { ret = AVERROR(ENOMEM); - goto fail; + goto end; } st2->id = st->id; st2->r_frame_rate = st->r_frame_rate; @@ -122,34 +172,79 @@ static int open_slave(AVFormatContext *avf, char *slave, AVFormatContext **ravf) st2->avg_frame_rate = st->avg_frame_rate; av_dict_copy(&st2->metadata, st->metadata, 0); if ((ret = avcodec_copy_context(st2->codec, st->codec)) < 0) - goto fail; + goto end; }
if (!(avf2->oformat->flags & AVFMT_NOFILE)) { if ((ret = avio_open(&avf2->pb, filename, AVIO_FLAG_WRITE)) < 0) { av_log(avf, AV_LOG_ERROR, "Slave '%s': error opening: %s\n", slave, av_err2str(ret)); - goto fail; + goto end; } }
if ((ret = avformat_write_header(avf2, &options)) < 0) { av_log(avf, AV_LOG_ERROR, "Slave '%s': error writing header: %s\n", slave, av_err2str(ret)); - goto fail; + goto end; + } + + tee_slave->avf = avf2; + tee_slave->bsfs = av_calloc(avf2->nb_streams, sizeof(TeeSlave)); + if (!tee_slave->bsfs) { + ret = AVERROR(ENOMEM); + goto end; + }
+ memset(tee_slave->bsfs, 0, avf2->nb_streams * sizeof(TeeSlave));
Unnecessary, calloc already does it.
+ + entry = NULL; + while (entry = av_dict_get(options, "bsfs", NULL, AV_DICT_IGNORE_SUFFIX)) { + const char *spec = entry->key + strlen("bsfs");
+ if (*spec) + spec++; /* consume separator */
Possibly: check that this is indeed the separator.
Changed.
+ + for (i = 0; i < avf2->nb_streams; i++) { + ret = avformat_match_stream_specifier(avf2, avf2->streams[i], spec); + if (ret < 0) { + av_log(avf, AV_LOG_ERROR, + "Invalid stream specifier '%s' in bsfs option '%s' for slave " + "output '%s'\n", spec, entry->key, filename); + goto end; + } + + if (ret > 0) { + av_log(avf, AV_LOG_DEBUG, "spec:%s bsfs:%s matches stream %d of slave " + "output '%s'\n", spec, entry->value, i, filename); + if (tee_slave->bsfs[i]) { + av_log(avf, AV_LOG_WARNING, + "Duplicate bsfs specification associated to stream %d of slave " + "output '%s', filters will be ignored\n", i, filename); + continue; + } + ret = parse_bsfs(avf, entry->value, &tee_slave->bsfs[i]); + if (ret < 0) { + av_log(avf, AV_LOG_ERROR, + "Error parsing bitstream filter sequence '%s' associated to " + "stream %d of slave output '%s'\n", entry->value, i, filename); + goto end; + } + } + } +
+ entry->value = NULL; + av_dict_set(&options, entry->key, NULL, 0);
I suspect entry->value is leaking now.
Should be fixed now. [...]
No other remarks, thanks.
Thanks for the extensive review. I'll push the attached patch soon if I read no more comments. -- FFmpeg = Friendly and Fast Martial Purposeless EnGine
Le nonidi 29 thermidor, an CCXXI, Stefano Sabatini a écrit :
Thanks for the extensive review. I'll push the attached patch soon if I read no more comments.
Please go ahead, and thanks for the feature. Regards, -- Nicolas George
On date Friday 2013-08-16 16:51:52 +0200, Nicolas George encoded:
Le nonidi 29 thermidor, an CCXXI, Stefano Sabatini a écrit :
Thanks for the extensive review. I'll push the attached patch soon if I read no more comments.
Please go ahead, and thanks for the feature.
Thanks, pushed. -- FFmpeg = Furious & Faithful Murdering Purposeless Enhancing Ghost
On date Thursday 2013-08-01 14:36:21 +0200, Nicolas George encoded:
Le tridi 13 thermidor, an CCXXI, Stefano Sabatini a écrit :
Changed to: dumo_extra+a,mp4toannexb+v.
I believe you missed my point: I meant:
bsfs:a=dump_extra,bsfs:v=mp4toannexb
But I realize that it is not possible as ':' is not an authorized option character. "bsfs.a=dump_extra" would be possible, though.
Not sure what you mean, I'm already duping it.
I'll explain later. (Run!)
[...] Updated with various fixes. I'm still making my mind about adopting a different syntax. -- FFmpeg = Fast & Foolish Marvellous Powerful Everlasting Guru
Le sextidi 16 thermidor, an CCXXI, Stefano Sabatini a écrit :
Updated with various fixes. I'm still making my mind about adopting a different syntax. -- FFmpeg = Fast & Foolish Marvellous Powerful Everlasting Guru
From 35fed58f67befbdf9067a03110dc87d47be34b7c Mon Sep 17 00:00:00 2001 From: Stefano Sabatini <stefasab@gmail.com> Date: Fri, 5 Jul 2013 18:33:30 +0200 Subject: [PATCH] lavf/tee: add support for bitstream filtering
This allows to apply different bitstream filters to different outputs, with no need to trancode.
Note: if you are not transcoding, -f tee is not necessary, -c copy will work just fine. -f tee, IMHO, is most useful when transcoding with expensive codecs.
TODO: bump minor --- doc/muxers.texi | 25 ++++++- libavformat/tee.c | 201 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 216 insertions(+), 10 deletions(-)
diff --git a/doc/muxers.texi b/doc/muxers.texi index b0dc7e6..7c5a1c8 100644 --- a/doc/muxers.texi +++ b/doc/muxers.texi @@ -818,11 +818,34 @@ leading or trailing spaces or any special character, it must be escaped (see the ``Quoting and escaping'' section in the ffmpeg-utils manual).
-Options can be specified for each slave by prepending them as a list of +Muxer options can be specified for each slave by prepending them as a list of @var{key}=@var{value} pairs separated by ':', between square brackets. If the options values contain a special character or the ':' separator, they must be escaped; note that this is a second level escaping.
+The following special options are also recognized: +@table @option +@item f +Specify the format name. Useful if it cannot be guessed from the +output name suffix. + +@item bsfs +Specify a list of bitstream filters to apply to the specified +output. It is possible to specify to which streams a given bitstream +filter applies, by appending a stream specifier to a bitstream filter +followed by a "+". If the stream specifier is not specified, it will +be applied to all streams in the output. +
+Several bitstream filters can be specified, separated each other by
I am not completely sure, but I believe that it should be "separated from each other"; it could be simply "separated by".
+",". + +The BNF description of the bitstream filters specification is given by: +@example +@var{BSF} ::= @var{BSF_NAME}[+@var{STREAM_SPECIFIER}] +@var{BSFS} ::= @var{BSF}[,@var{BSFS}] +@end example +@end table + Example: encode something and both archive it in a WebM file and stream it as MPEG-TS over UDP (the streams need to be explicitly mapped):
diff --git a/libavformat/tee.c b/libavformat/tee.c index 7b8b371..90947c7 100644 --- a/libavformat/tee.c +++ b/libavformat/tee.c @@ -27,10 +27,16 @@
#define MAX_SLAVES 16
+typedef struct { + AVFormatContext *fmt_ctx; + AVBitStreamFilterContext **bsf_ctxs; ///< bitstream filters per stream +} TeeSlave; + typedef struct TeeContext { const AVClass *class; unsigned nb_slaves; - AVFormatContext *slaves[MAX_SLAVES]; + TeeSlave slaves[MAX_SLAVES]; + char *bsfs; } TeeContext;
static const char *const slave_delim = "|"; @@ -82,13 +88,98 @@ fail: return ret; }
-static int open_slave(AVFormatContext *avf, char *slave, AVFormatContext **ravf) +/** + * Parse bitstream filter, in the form: + * BSF ::= BSF_NAME[+STREAM_SPECIFIER] + */ +static int parse_bsf(void *log_ctx, char *bsf, + AVFormatContext *fmt_ctx, + AVBitStreamFilterContext **bsf_ctxs) +{ + int i, ret = 0; + char *bsf1 = av_strdup(bsf); + char *bsf_name, *spec; + + if (!bsf1) + return AVERROR(ENOMEM); + bsf_name = av_strtok(bsf1, "+", &spec); + + /* select the streams matched by the specifier */ + for (i = 0; i < fmt_ctx->nb_streams; i++) { + AVBitStreamFilterContext *bsf_ctx; + + if (spec && spec[0]) { + ret = avformat_match_stream_specifier(fmt_ctx, fmt_ctx->streams[i], spec); + if (ret < 0) { + av_log(log_ctx, AV_LOG_ERROR, + "Invalid stream specifier for bitstream filter '%s'\n", bsf); + goto end; + } + + if (ret == 0) /* no match */ + continue; + } + + bsf_ctx = av_bitstream_filter_init(bsf_name); + if (!bsf_ctx) { + av_log(log_ctx, AV_LOG_ERROR, + "Cannot create bitstream filter with name '%s' for stream %d, " + "unknown filter or internal error happened\n", + bsf_name, i); + ret = AVERROR_UNKNOWN; + goto end; + } + + /* append bsf context to the list of per-stream bsf contexts */ + if (!bsf_ctxs[i]) { + bsf_ctxs[i] = bsf_ctx; + } else { + AVBitStreamFilterContext *bsf_ctx1 = bsf_ctxs[i]; + while (bsf_ctx1->next) + bsf_ctx1 = bsf_ctx1->next; + bsf_ctx1->next = bsf_ctx; + } + } + +end: + av_free(bsf1); + return ret; +} + +/** + * Parse list of bitstream filters in the form: + * BSFS ::= BSF[,BSFS] + */ +static int parse_slave_bsfs(void *log_ctx, char *slave_bsfs, + AVFormatContext *fmt_ctx, + AVBitStreamFilterContext **bsf_ctx) +{ + char *bsf, *slave_bsfs1 = av_strdup(slave_bsfs); + char *saveptr, *buf = slave_bsfs1; + int ret; + + if (!slave_bsfs1) + return AVERROR(ENOMEM); + + while (bsf = av_strtok(buf, ",", &saveptr)) { + buf = NULL; + ret = parse_bsf(log_ctx, bsf, fmt_ctx, bsf_ctx); + if (ret < 0) + goto end; + } + +end: + av_free(slave_bsfs1); + return ret; +} + +static int open_slave(AVFormatContext *avf, char *slave, TeeSlave *tee_slave) { int i, ret; AVDictionary *options = NULL; AVDictionaryEntry *entry; char *filename; - char *format = NULL; + char *format = NULL, *bsfs = NULL; AVFormatContext *avf2 = NULL; AVStream *st, *st2;
@@ -99,6 +190,11 @@ static int open_slave(AVFormatContext *avf, char *slave, AVFormatContext **ravf) entry->value = NULL; /* prevent it from being freed */ av_dict_set(&options, "f", NULL, 0); } + if ((entry = av_dict_get(options, "bsfs", NULL, 0))) { + bsfs = entry->value; + entry->value = NULL; /* prevent it from being freed */ + av_dict_set(&options, "bsfs", NULL, 0); + }
ret = avformat_alloc_output_context2(&avf2, NULL, format, filename); if (ret < 0) @@ -138,6 +234,7 @@ static int open_slave(AVFormatContext *avf, char *slave, AVFormatContext **ravf) slave, av_err2str(ret)); goto fail; } + if (options) { entry = NULL; while ((entry = av_dict_get(options, "", entry, AV_DICT_IGNORE_SUFFIX))) @@ -146,7 +243,17 @@ static int open_slave(AVFormatContext *avf, char *slave, AVFormatContext **ravf) goto fail; }
- *ravf = avf2; + tee_slave->fmt_ctx = avf2; + tee_slave->bsf_ctxs = av_calloc(avf2->nb_streams, sizeof(TeeSlave)); + if (!tee_slave->bsf_ctxs) + return AVERROR(ENOMEM); + memset(tee_slave->bsf_ctxs, 0, avf2->nb_streams * sizeof(TeeSlave)); +
+ /* generate the array of bitstream filters */ + if (bsfs) { + if ((ret = parse_slave_bsfs(avf, bsfs, avf2, tee_slave->bsf_ctxs)) < 0) + return ret; + }
I suspect bsfs leaks here. And since it belongs exclusively to this code, you could remove all the av_strdup() in the parsing code, that would make it simpler (or do you have a reason not to?).
return 0;
fail: @@ -158,14 +265,47 @@ static void close_slaves(AVFormatContext *avf) { TeeContext *tee = avf->priv_data; AVFormatContext *avf2; - unsigned i; + unsigned i, j;
for (i = 0; i < tee->nb_slaves; i++) { - avf2 = tee->slaves[i]; + avf2 = tee->slaves[i].fmt_ctx; + + for (j = 0; j < avf2->nb_streams; j++) { + AVBitStreamFilterContext *bsf_ctx = tee->slaves[i].bsf_ctxs[j]; + while (bsf_ctx) { + bsf_ctx = bsf_ctx->next; + av_bitstream_filter_close(bsf_ctx); + } + } + avio_close(avf2->pb); avf2->pb = NULL; avformat_free_context(avf2); - tee->slaves[i] = NULL; + tee->slaves[i].fmt_ctx = NULL; + } +} + +static void log_slave(TeeSlave *slave, void *log_ctx, int log_level) +{ + int i; + av_log(log_ctx, log_level, "filename:'%s' format:%s\n", + slave->fmt_ctx->filename, slave->fmt_ctx->oformat->name); + for (i = 0; i < slave->fmt_ctx->nb_streams; i++) { + AVStream *st = slave->fmt_ctx->streams[i]; + AVBitStreamFilterContext *bsf_ctx = slave->bsf_ctxs[i]; + + av_log(log_ctx, log_level, " stream:%d codec:%s type:%s", + i, avcodec_get_name(st->codec->codec_id), + av_get_media_type_string(st->codec->codec_type)); + if (bsf_ctx) { + av_log(log_ctx, log_level, " bsfs:"); + while (bsf_ctx) { + av_log(log_ctx, log_level, "%s%s", + bsf_ctx->filter->name, bsf_ctx->next ? "," : ""); + bsf_ctx = bsf_ctx->next; + } + } + av_log(log_ctx, log_level, "\n"); } }
@@ -195,6 +335,7 @@ static int tee_write_header(AVFormatContext *avf) for (i = 0; i < nb_slaves; i++) { if ((ret = open_slave(avf, slaves[i], &tee->slaves[i])) < 0) goto fail; + log_slave(&tee->slaves[i], avf, AV_LOG_VERBOSE); av_freep(&slaves[i]); }
@@ -208,6 +349,46 @@ fail: return ret; }
+static int filter_packet(void *log_ctx, AVPacket *pkt, + AVFormatContext *fmt_ctx, AVBitStreamFilterContext *bsf_ctx) +{ + AVCodecContext *enc_ctx = fmt_ctx->streams[pkt->stream_index]->codec; + int ret; + + while (bsf_ctx) { + AVPacket new_pkt = *pkt; + ret = av_bitstream_filter_filter(bsf_ctx, enc_ctx, NULL, + &new_pkt.data, &new_pkt.size, + pkt->data, pkt->size, + pkt->flags & AV_PKT_FLAG_KEY); + if (ret == 0 && new_pkt.data != pkt->data && new_pkt.destruct) { + if ((ret = av_copy_packet(&new_pkt, pkt)) < 0) + break; + ret = 1; + } + + if (ret > 0) { + av_free_packet(pkt); + new_pkt.buf = av_buffer_create(new_pkt.data, new_pkt.size, + av_buffer_default_free, NULL, 0); + if (!new_pkt.buf) + break; + } + *pkt = new_pkt; + + bsf_ctx = bsf_ctx->next; + } + + if (ret < 0) { + av_log(log_ctx, AV_LOG_ERROR,
+ "Failed to filter bitstream with filter %s for stream %d in file '%s' with codec %s\n", + bsf_ctx->filter->name, pkt->stream_index, fmt_ctx->filename, + avcodec_get_name(enc_ctx->codec_id)); + } + + return ret; +} + static int tee_write_trailer(AVFormatContext *avf) { TeeContext *tee = avf->priv_data; @@ -216,7 +397,7 @@ static int tee_write_trailer(AVFormatContext *avf) unsigned i;
for (i = 0; i < tee->nb_slaves; i++) { - avf2 = tee->slaves[i]; + avf2 = tee->slaves[i].fmt_ctx; if ((ret = av_write_trailer(avf2)) < 0) if (!ret_all) ret_all = ret; @@ -241,7 +422,7 @@ static int tee_write_packet(AVFormatContext *avf, AVPacket *pkt) AVRational tb, tb2;
for (i = 0; i < tee->nb_slaves; i++) { - avf2 = tee->slaves[i]; + avf2 = tee->slaves[i].fmt_ctx; s = pkt->stream_index; if (s >= avf2->nb_streams) { if (!ret_all) @@ -259,6 +440,8 @@ static int tee_write_packet(AVFormatContext *avf, AVPacket *pkt) 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); + + filter_packet(avf2, &pkt2, avf2, tee->slaves[i].bsf_ctxs[s]); if ((ret = av_interleaved_write_frame(avf2, &pkt2)) < 0) if (!ret_all) ret_all = ret;
No more remarks for now. Regards, -- Nicolas George
On date Sunday 2013-08-04 11:53:36 +0200, Nicolas George encoded:
Le sextidi 16 thermidor, an CCXXI, Stefano Sabatini a écrit :
Updated with various fixes. I'm still making my mind about adopting a different syntax. -- FFmpeg = Fast & Foolish Marvellous Powerful Everlasting Guru
From 35fed58f67befbdf9067a03110dc87d47be34b7c Mon Sep 17 00:00:00 2001 From: Stefano Sabatini <stefasab@gmail.com> Date: Fri, 5 Jul 2013 18:33:30 +0200 Subject: [PATCH] lavf/tee: add support for bitstream filtering
This allows to apply different bitstream filters to different outputs, with no need to trancode.
Note: if you are not transcoding, -f tee is not necessary, -c copy will work just fine. -f tee, IMHO, is most useful when transcoding with expensive codecs.
Yes, this is indeed the use case I'm handling.
TODO: bump minor --- doc/muxers.texi | 25 ++++++- libavformat/tee.c | 201 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 216 insertions(+), 10 deletions(-)
diff --git a/doc/muxers.texi b/doc/muxers.texi index b0dc7e6..7c5a1c8 100644 --- a/doc/muxers.texi +++ b/doc/muxers.texi @@ -818,11 +818,34 @@ leading or trailing spaces or any special character, it must be escaped (see the ``Quoting and escaping'' section in the ffmpeg-utils manual).
-Options can be specified for each slave by prepending them as a list of +Muxer options can be specified for each slave by prepending them as a list of @var{key}=@var{value} pairs separated by ':', between square brackets. If the options values contain a special character or the ':' separator, they must be escaped; note that this is a second level escaping.
+The following special options are also recognized: +@table @option +@item f +Specify the format name. Useful if it cannot be guessed from the +output name suffix. + +@item bsfs +Specify a list of bitstream filters to apply to the specified +output. It is possible to specify to which streams a given bitstream +filter applies, by appending a stream specifier to a bitstream filter +followed by a "+". If the stream specifier is not specified, it will +be applied to all streams in the output. +
+Several bitstream filters can be specified, separated each other by
I am not completely sure, but I believe that it should be "separated from each other"; it could be simply "separated by".
Removed.
+",". + +The BNF description of the bitstream filters specification is given by: +@example +@var{BSF} ::= @var{BSF_NAME}[+@var{STREAM_SPECIFIER}] +@var{BSFS} ::= @var{BSF}[,@var{BSFS}] +@end example +@end table + Example: encode something and both archive it in a WebM file and stream it as MPEG-TS over UDP (the streams need to be explicitly mapped):
diff --git a/libavformat/tee.c b/libavformat/tee.c index 7b8b371..90947c7 100644 --- a/libavformat/tee.c +++ b/libavformat/tee.c @@ -27,10 +27,16 @@
#define MAX_SLAVES 16
+typedef struct { + AVFormatContext *fmt_ctx; + AVBitStreamFilterContext **bsf_ctxs; ///< bitstream filters per stream +} TeeSlave; + typedef struct TeeContext { const AVClass *class; unsigned nb_slaves; - AVFormatContext *slaves[MAX_SLAVES]; + TeeSlave slaves[MAX_SLAVES]; + char *bsfs; } TeeContext;
static const char *const slave_delim = "|"; @@ -82,13 +88,98 @@ fail: return ret; }
-static int open_slave(AVFormatContext *avf, char *slave, AVFormatContext **ravf) +/** + * Parse bitstream filter, in the form: + * BSF ::= BSF_NAME[+STREAM_SPECIFIER] + */ +static int parse_bsf(void *log_ctx, char *bsf, + AVFormatContext *fmt_ctx, + AVBitStreamFilterContext **bsf_ctxs) +{ + int i, ret = 0; + char *bsf1 = av_strdup(bsf); + char *bsf_name, *spec; + + if (!bsf1) + return AVERROR(ENOMEM); + bsf_name = av_strtok(bsf1, "+", &spec); + + /* select the streams matched by the specifier */ + for (i = 0; i < fmt_ctx->nb_streams; i++) { + AVBitStreamFilterContext *bsf_ctx; + + if (spec && spec[0]) { + ret = avformat_match_stream_specifier(fmt_ctx, fmt_ctx->streams[i], spec); + if (ret < 0) { + av_log(log_ctx, AV_LOG_ERROR, + "Invalid stream specifier for bitstream filter '%s'\n", bsf); + goto end; + } + + if (ret == 0) /* no match */ + continue; + } + + bsf_ctx = av_bitstream_filter_init(bsf_name); + if (!bsf_ctx) { + av_log(log_ctx, AV_LOG_ERROR, + "Cannot create bitstream filter with name '%s' for stream %d, " + "unknown filter or internal error happened\n", + bsf_name, i); + ret = AVERROR_UNKNOWN; + goto end; + } + + /* append bsf context to the list of per-stream bsf contexts */ + if (!bsf_ctxs[i]) { + bsf_ctxs[i] = bsf_ctx; + } else { + AVBitStreamFilterContext *bsf_ctx1 = bsf_ctxs[i]; + while (bsf_ctx1->next) + bsf_ctx1 = bsf_ctx1->next; + bsf_ctx1->next = bsf_ctx; + } + } + +end: + av_free(bsf1); + return ret; +} + +/** + * Parse list of bitstream filters in the form: + * BSFS ::= BSF[,BSFS] + */ +static int parse_slave_bsfs(void *log_ctx, char *slave_bsfs, + AVFormatContext *fmt_ctx, + AVBitStreamFilterContext **bsf_ctx) +{ + char *bsf, *slave_bsfs1 = av_strdup(slave_bsfs); + char *saveptr, *buf = slave_bsfs1; + int ret; + + if (!slave_bsfs1) + return AVERROR(ENOMEM); + + while (bsf = av_strtok(buf, ",", &saveptr)) { + buf = NULL; + ret = parse_bsf(log_ctx, bsf, fmt_ctx, bsf_ctx); + if (ret < 0) + goto end; + } + +end: + av_free(slave_bsfs1); + return ret; +} + +static int open_slave(AVFormatContext *avf, char *slave, TeeSlave *tee_slave) { int i, ret; AVDictionary *options = NULL; AVDictionaryEntry *entry; char *filename; - char *format = NULL; + char *format = NULL, *bsfs = NULL; AVFormatContext *avf2 = NULL; AVStream *st, *st2;
@@ -99,6 +190,11 @@ static int open_slave(AVFormatContext *avf, char *slave, AVFormatContext **ravf) entry->value = NULL; /* prevent it from being freed */ av_dict_set(&options, "f", NULL, 0); } + if ((entry = av_dict_get(options, "bsfs", NULL, 0))) { + bsfs = entry->value; + entry->value = NULL; /* prevent it from being freed */ + av_dict_set(&options, "bsfs", NULL, 0); + }
ret = avformat_alloc_output_context2(&avf2, NULL, format, filename); if (ret < 0) @@ -138,6 +234,7 @@ static int open_slave(AVFormatContext *avf, char *slave, AVFormatContext **ravf) slave, av_err2str(ret)); goto fail; } + if (options) { entry = NULL; while ((entry = av_dict_get(options, "", entry, AV_DICT_IGNORE_SUFFIX))) @@ -146,7 +243,17 @@ static int open_slave(AVFormatContext *avf, char *slave, AVFormatContext **ravf) goto fail; }
- *ravf = avf2; + tee_slave->fmt_ctx = avf2; + tee_slave->bsf_ctxs = av_calloc(avf2->nb_streams, sizeof(TeeSlave)); + if (!tee_slave->bsf_ctxs) + return AVERROR(ENOMEM); + memset(tee_slave->bsf_ctxs, 0, avf2->nb_streams * sizeof(TeeSlave)); +
+ /* generate the array of bitstream filters */ + if (bsfs) { + if ((ret = parse_slave_bsfs(avf, bsfs, avf2, tee_slave->bsf_ctxs)) < 0) + return ret; + }
I suspect bsfs leaks here.
Fixed.
And since it belongs exclusively to this code, you could remove all the av_strdup() in the parsing code, that would make it simpler (or do you have a reason not to?).
Complicates the logic, since I don't want to track if the function was called or not, dupping the string internally seems to me easier to handle. [...]
No more remarks for now.
I want to think more about the syntax thing. Updated patch in attachment. -- FFmpeg = Fabulous and Freak Monstrous Power Empowered Geek
participants (2)
-
Nicolas George -
Stefano Sabatini