Browse code

lavf/tee: add support for bitstream filtering

This allows to apply different bitstream filters to different outputs,
with no need to transcode.

Stefano Sabatini authored on 2013/07/06 01:33:30
Showing 3 changed files
... ...
@@ -852,11 +852,27 @@ leading or trailing spaces or any special character, it must be
852 852
 escaped (see the ``Quoting and escaping'' section in the ffmpeg-utils
853 853
 manual).
854 854
 
855
-Options can be specified for each slave by prepending them as a list of
855
+Muxer options can be specified for each slave by prepending them as a list of
856 856
 @var{key}=@var{value} pairs separated by ':', between square brackets. If
857 857
 the options values contain a special character or the ':' separator, they
858 858
 must be escaped; note that this is a second level escaping.
859 859
 
860
+The following special options are also recognized:
861
+@table @option
862
+@item f
863
+Specify the format name. Useful if it cannot be guessed from the
864
+output name suffix.
865
+
866
+@item bsfs[/@var{spec}]
867
+Specify a list of bitstream filters to apply to the specified
868
+output. It is possible to specify to which streams a given bitstream
869
+filter applies, by appending a stream specifier to the option
870
+separated by @code{/}. If the stream specifier is not specified, the
871
+bistream filters will be applied to all streams in the output.
872
+
873
+Several bitstream filters can be specified, separated by ",".
874
+@end table
875
+
860 876
 Example: encode something and both archive it in a WebM file and stream it
861 877
 as MPEG-TS over UDP (the streams need to be explicitly mapped):
862 878
 
... ...
@@ -27,16 +27,22 @@
27 27
 
28 28
 #define MAX_SLAVES 16
29 29
 
30
+typedef struct {
31
+    AVFormatContext *avf;
32
+    AVBitStreamFilterContext **bsfs; ///< bitstream filters per stream
33
+} TeeSlave;
34
+
30 35
 typedef struct TeeContext {
31 36
     const AVClass *class;
32 37
     unsigned nb_slaves;
33
-    AVFormatContext *slaves[MAX_SLAVES];
38
+    TeeSlave slaves[MAX_SLAVES];
34 39
 } TeeContext;
35 40
 
36 41
 static const char *const slave_delim     = "|";
37 42
 static const char *const slave_opt_open  = "[";
38 43
 static const char *const slave_opt_close = "]";
39 44
 static const char *const slave_opt_delim = ":]"; /* must have the close too */
45
+static const char *const slave_bsfs_spec_sep = "/";
40 46
 
41 47
 static const AVClass tee_muxer_class = {
42 48
     .class_name = "Tee muxer",
... ...
@@ -82,7 +88,47 @@ fail:
82 82
     return ret;
83 83
 }
84 84
 
85
-static int open_slave(AVFormatContext *avf, char *slave, AVFormatContext **ravf)
85
+/**
86
+ * Parse list of bitstream filters and add them to the list of filters
87
+ * pointed to by bsfs.
88
+ *
89
+ * The list must be specified in the form:
90
+ * BSFS ::= BSF[,BSFS]
91
+ */
92
+static int parse_bsfs(void *log_ctx, const char *bsfs_spec,
93
+                      AVBitStreamFilterContext **bsfs)
94
+{
95
+    char *bsf_name, *buf, *saveptr;
96
+    int ret;
97
+
98
+    if (!(buf = av_strdup(bsfs_spec)))
99
+        return AVERROR(ENOMEM);
100
+
101
+    while (bsf_name = av_strtok(buf, ",", &saveptr)) {
102
+        AVBitStreamFilterContext *bsf = av_bitstream_filter_init(bsf_name);
103
+
104
+        if (!bsf) {
105
+            av_log(log_ctx, AV_LOG_ERROR,
106
+                   "Cannot initialize bitstream filter with name '%s', "
107
+                   "unknown filter or internal error happened\n",
108
+                   bsf_name);
109
+            ret = AVERROR_UNKNOWN;
110
+            goto end;
111
+        }
112
+
113
+        /* append bsf context to the list of bsf contexts */
114
+        *bsfs = bsf;
115
+        bsfs = &bsf->next;
116
+
117
+        buf = NULL;
118
+    }
119
+
120
+end:
121
+    av_free(buf);
122
+    return ret;
123
+}
124
+
125
+static int open_slave(AVFormatContext *avf, char *slave, TeeSlave *tee_slave)
86 126
 {
87 127
     int i, ret;
88 128
     AVDictionary *options = NULL;
... ...
@@ -102,14 +148,13 @@ static int open_slave(AVFormatContext *avf, char *slave, AVFormatContext **ravf)
102 102
 
103 103
     ret = avformat_alloc_output_context2(&avf2, NULL, format, filename);
104 104
     if (ret < 0)
105
-        goto fail;
106
-    av_free(format);
105
+        goto end;
107 106
 
108 107
     for (i = 0; i < avf->nb_streams; i++) {
109 108
         st = avf->streams[i];
110 109
         if (!(st2 = avformat_new_stream(avf2, NULL))) {
111 110
             ret = AVERROR(ENOMEM);
112
-            goto fail;
111
+            goto end;
113 112
         }
114 113
         st2->id = st->id;
115 114
         st2->r_frame_rate        = st->r_frame_rate;
... ...
@@ -122,34 +167,84 @@ static int open_slave(AVFormatContext *avf, char *slave, AVFormatContext **ravf)
122 122
         st2->avg_frame_rate      = st->avg_frame_rate;
123 123
         av_dict_copy(&st2->metadata, st->metadata, 0);
124 124
         if ((ret = avcodec_copy_context(st2->codec, st->codec)) < 0)
125
-            goto fail;
125
+            goto end;
126 126
     }
127 127
 
128 128
     if (!(avf2->oformat->flags & AVFMT_NOFILE)) {
129 129
         if ((ret = avio_open(&avf2->pb, filename, AVIO_FLAG_WRITE)) < 0) {
130 130
             av_log(avf, AV_LOG_ERROR, "Slave '%s': error opening: %s\n",
131 131
                    slave, av_err2str(ret));
132
-            goto fail;
132
+            goto end;
133 133
         }
134 134
     }
135 135
 
136 136
     if ((ret = avformat_write_header(avf2, &options)) < 0) {
137 137
         av_log(avf, AV_LOG_ERROR, "Slave '%s': error writing header: %s\n",
138 138
                slave, av_err2str(ret));
139
-        goto fail;
139
+        goto end;
140
+    }
141
+
142
+    tee_slave->avf = avf2;
143
+    tee_slave->bsfs = av_calloc(avf2->nb_streams, sizeof(TeeSlave));
144
+    if (!tee_slave->bsfs) {
145
+        ret = AVERROR(ENOMEM);
146
+        goto end;
147
+    }
148
+
149
+    entry = NULL;
150
+    while (entry = av_dict_get(options, "bsfs", NULL, AV_DICT_IGNORE_SUFFIX)) {
151
+        const char *spec = entry->key + strlen("bsfs");
152
+        if (*spec) {
153
+            if (strspn(spec, slave_bsfs_spec_sep) != 1) {
154
+                av_log(avf, AV_LOG_ERROR,
155
+                       "Specifier separator in '%s' is '%c', but only characters '%s' "
156
+                       "are allowed\n", entry->key, *spec, slave_bsfs_spec_sep);
157
+                return AVERROR(EINVAL);
158
+            }
159
+            spec++; /* consume separator */
160
+        }
161
+
162
+        for (i = 0; i < avf2->nb_streams; i++) {
163
+            ret = avformat_match_stream_specifier(avf2, avf2->streams[i], spec);
164
+            if (ret < 0) {
165
+                av_log(avf, AV_LOG_ERROR,
166
+                       "Invalid stream specifier '%s' in bsfs option '%s' for slave "
167
+                       "output '%s'\n", spec, entry->key, filename);
168
+                goto end;
169
+            }
170
+
171
+            if (ret > 0) {
172
+                av_log(avf, AV_LOG_DEBUG, "spec:%s bsfs:%s matches stream %d of slave "
173
+                       "output '%s'\n", spec, entry->value, i, filename);
174
+                if (tee_slave->bsfs[i]) {
175
+                    av_log(avf, AV_LOG_WARNING,
176
+                           "Duplicate bsfs specification associated to stream %d of slave "
177
+                           "output '%s', filters will be ignored\n", i, filename);
178
+                    continue;
179
+                }
180
+                ret = parse_bsfs(avf, entry->value, &tee_slave->bsfs[i]);
181
+                if (ret < 0) {
182
+                    av_log(avf, AV_LOG_ERROR,
183
+                           "Error parsing bitstream filter sequence '%s' associated to "
184
+                           "stream %d of slave output '%s'\n", entry->value, i, filename);
185
+                    goto end;
186
+                }
187
+            }
188
+        }
189
+
190
+        av_dict_set(&options, entry->key, NULL, 0);
140 191
     }
192
+
141 193
     if (options) {
142 194
         entry = NULL;
143 195
         while ((entry = av_dict_get(options, "", entry, AV_DICT_IGNORE_SUFFIX)))
144 196
             av_log(avf2, AV_LOG_ERROR, "Unknown option '%s'\n", entry->key);
145 197
         ret = AVERROR_OPTION_NOT_FOUND;
146
-        goto fail;
198
+        goto end;
147 199
     }
148 200
 
149
-    *ravf = avf2;
150
-    return 0;
151
-
152
-fail:
201
+end:
202
+    av_free(format);
153 203
     av_dict_free(&options);
154 204
     return ret;
155 205
 }
... ...
@@ -158,14 +253,48 @@ static void close_slaves(AVFormatContext *avf)
158 158
 {
159 159
     TeeContext *tee = avf->priv_data;
160 160
     AVFormatContext *avf2;
161
-    unsigned i;
161
+    unsigned i, j;
162 162
 
163 163
     for (i = 0; i < tee->nb_slaves; i++) {
164
-        avf2 = tee->slaves[i];
164
+        avf2 = tee->slaves[i].avf;
165
+
166
+        for (j = 0; j < avf2->nb_streams; j++) {
167
+            AVBitStreamFilterContext *bsf_next, *bsf = tee->slaves[i].bsfs[j];
168
+            while (bsf) {
169
+                bsf_next = bsf->next;
170
+                av_bitstream_filter_close(bsf);
171
+                bsf = bsf_next;
172
+            }
173
+        }
174
+
165 175
         avio_close(avf2->pb);
166 176
         avf2->pb = NULL;
167 177
         avformat_free_context(avf2);
168
-        tee->slaves[i] = NULL;
178
+        tee->slaves[i].avf = NULL;
179
+    }
180
+}
181
+
182
+static void log_slave(TeeSlave *slave, void *log_ctx, int log_level)
183
+{
184
+    int i;
185
+    av_log(log_ctx, log_level, "filename:'%s' format:%s\n",
186
+           slave->avf->filename, slave->avf->oformat->name);
187
+    for (i = 0; i < slave->avf->nb_streams; i++) {
188
+        AVStream *st = slave->avf->streams[i];
189
+        AVBitStreamFilterContext *bsf = slave->bsfs[i];
190
+
191
+        av_log(log_ctx, log_level, "    stream:%d codec:%s type:%s",
192
+               i, avcodec_get_name(st->codec->codec_id),
193
+               av_get_media_type_string(st->codec->codec_type));
194
+        if (bsf) {
195
+            av_log(log_ctx, log_level, " bsfs:");
196
+            while (bsf) {
197
+                av_log(log_ctx, log_level, "%s%s",
198
+                       bsf->filter->name, bsf->next ? "," : "");
199
+                bsf = bsf->next;
200
+            }
201
+        }
202
+        av_log(log_ctx, log_level, "\n");
169 203
     }
170 204
 }
171 205
 
... ...
@@ -195,6 +324,7 @@ static int tee_write_header(AVFormatContext *avf)
195 195
     for (i = 0; i < nb_slaves; i++) {
196 196
         if ((ret = open_slave(avf, slaves[i], &tee->slaves[i])) < 0)
197 197
             goto fail;
198
+        log_slave(&tee->slaves[i], avf, AV_LOG_VERBOSE);
198 199
         av_freep(&slaves[i]);
199 200
     }
200 201
 
... ...
@@ -208,6 +338,46 @@ fail:
208 208
     return ret;
209 209
 }
210 210
 
211
+static int filter_packet(void *log_ctx, AVPacket *pkt,
212
+                         AVFormatContext *fmt_ctx, AVBitStreamFilterContext *bsf_ctx)
213
+{
214
+    AVCodecContext *enc_ctx = fmt_ctx->streams[pkt->stream_index]->codec;
215
+    int ret;
216
+
217
+    while (bsf_ctx) {
218
+        AVPacket new_pkt = *pkt;
219
+        ret = av_bitstream_filter_filter(bsf_ctx, enc_ctx, NULL,
220
+                                             &new_pkt.data, &new_pkt.size,
221
+                                             pkt->data, pkt->size,
222
+                                             pkt->flags & AV_PKT_FLAG_KEY);
223
+        if (ret == 0 && new_pkt.data != pkt->data && new_pkt.destruct) {
224
+            if ((ret = av_copy_packet(&new_pkt, pkt)) < 0)
225
+                break;
226
+            ret = 1;
227
+        }
228
+
229
+        if (ret > 0) {
230
+            av_free_packet(pkt);
231
+            new_pkt.buf = av_buffer_create(new_pkt.data, new_pkt.size,
232
+                                           av_buffer_default_free, NULL, 0);
233
+            if (!new_pkt.buf)
234
+                break;
235
+        }
236
+        *pkt = new_pkt;
237
+
238
+        bsf_ctx = bsf_ctx->next;
239
+    }
240
+
241
+    if (ret < 0) {
242
+        av_log(log_ctx, AV_LOG_ERROR,
243
+               "Failed to filter bitstream with filter %s for stream %d in file '%s' with codec %s\n",
244
+               bsf_ctx->filter->name, pkt->stream_index, fmt_ctx->filename,
245
+               avcodec_get_name(enc_ctx->codec_id));
246
+    }
247
+
248
+    return ret;
249
+}
250
+
211 251
 static int tee_write_trailer(AVFormatContext *avf)
212 252
 {
213 253
     TeeContext *tee = avf->priv_data;
... ...
@@ -216,7 +386,7 @@ static int tee_write_trailer(AVFormatContext *avf)
216 216
     unsigned i;
217 217
 
218 218
     for (i = 0; i < tee->nb_slaves; i++) {
219
-        avf2 = tee->slaves[i];
219
+        avf2 = tee->slaves[i].avf;
220 220
         if ((ret = av_write_trailer(avf2)) < 0)
221 221
             if (!ret_all)
222 222
                 ret_all = ret;
... ...
@@ -241,7 +411,7 @@ static int tee_write_packet(AVFormatContext *avf, AVPacket *pkt)
241 241
     AVRational tb, tb2;
242 242
 
243 243
     for (i = 0; i < tee->nb_slaves; i++) {
244
-        avf2 = tee->slaves[i];
244
+        avf2 = tee->slaves[i].avf;
245 245
         s = pkt->stream_index;
246 246
         if (s >= avf2->nb_streams) {
247 247
             if (!ret_all)
... ...
@@ -259,6 +429,8 @@ static int tee_write_packet(AVFormatContext *avf, AVPacket *pkt)
259 259
         pkt2.pts      = av_rescale_q(pkt->pts,      tb, tb2);
260 260
         pkt2.dts      = av_rescale_q(pkt->dts,      tb, tb2);
261 261
         pkt2.duration = av_rescale_q(pkt->duration, tb, tb2);
262
+
263
+        filter_packet(avf2, &pkt2, avf2, tee->slaves[i].bsfs[s]);
262 264
         if ((ret = av_interleaved_write_frame(avf2, &pkt2)) < 0)
263 265
             if (!ret_all)
264 266
                 ret_all = ret;
... ...
@@ -31,7 +31,7 @@
31 31
 
32 32
 #define LIBAVFORMAT_VERSION_MAJOR 55
33 33
 #define LIBAVFORMAT_VERSION_MINOR 13
34
-#define LIBAVFORMAT_VERSION_MICRO 102
34
+#define LIBAVFORMAT_VERSION_MICRO 103
35 35
 
36 36
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
37 37
                                                LIBAVFORMAT_VERSION_MINOR, \