]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/tee.c
Merge commit '8c929037ec75fbe9f367e0a31ee34839e92de481'
[ffmpeg] / libavformat / tee.c
index daddba5741c8978344c97bf91ff1d74a36b8c57a..d59ad4d183b21457093fa6552da5fcc0a643afb7 100644 (file)
@@ -26,6 +26,7 @@
 #include "internal.h"
 #include "avformat.h"
 #include "avio_internal.h"
+#include "tee_common.h"
 
 typedef enum {
     ON_SLAVE_FAILURE_ABORT  = 1,
@@ -36,7 +37,7 @@ typedef enum {
 
 typedef struct {
     AVFormatContext *avf;
-    AVBitStreamFilterContext **bsfs; ///< bitstream filters per stream
+    AVBSFContext **bsfs; ///< bitstream filters per stream
 
     SlaveFailurePolicy on_fail;
 
@@ -54,9 +55,6 @@ typedef struct TeeContext {
 } TeeContext;
 
 static const char *const slave_delim     = "|";
-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 */
 static const char *const slave_bsfs_spec_sep = "/";
 static const char *const slave_select_sep = ",";
 
@@ -66,84 +64,6 @@ static const AVClass tee_muxer_class = {
     .version    = LIBAVUTIL_VERSION_INT,
 };
 
-static int parse_slave_options(void *log, char *slave,
-                               AVDictionary **options, char **filename)
-{
-    const char *p;
-    char *key, *val;
-    int ret;
-
-    if (!strspn(slave, slave_opt_open)) {
-        *filename = slave;
-        return 0;
-    }
-    p = slave + 1;
-    if (strspn(p, slave_opt_close)) {
-        *filename = (char *)p + 1;
-        return 0;
-    }
-    while (1) {
-        ret = av_opt_get_key_value(&p, "=", slave_opt_delim, 0, &key, &val);
-        if (ret < 0) {
-            av_log(log, AV_LOG_ERROR, "No option found near \"%s\"\n", p);
-            goto fail;
-        }
-        ret = av_dict_set(options, key, val,
-                          AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
-        if (ret < 0)
-            goto fail;
-        if (strspn(p, slave_opt_close))
-            break;
-        p++;
-    }
-    *filename = (char *)p + 1;
-    return 0;
-
-fail:
-    av_dict_free(options);
-    return ret;
-}
-
-/**
- * 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, *dup, *saveptr;
-    int ret = 0;
-
-    if (!(dup = 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 */
-        *bsfs = bsf;
-        bsfs = &bsf->next;
-
-        buf = NULL;
-    }
-
-end:
-    av_free(dup);
-    return ret;
-}
-
 static inline int parse_slave_failure_policy_option(const char *opt, TeeSlave *tee_slave)
 {
     if (!opt) {
@@ -175,14 +95,8 @@ static int close_slave(TeeSlave *tee_slave)
         ret = av_write_trailer(avf);
 
     if (tee_slave->bsfs) {
-        for (i = 0; i < avf->nb_streams; ++i) {
-            AVBitStreamFilterContext *bsf_next, *bsf = tee_slave->bsfs[i];
-            while (bsf) {
-                bsf_next = bsf->next;
-                av_bitstream_filter_close(bsf);
-                bsf = bsf_next;
-            }
-        }
+        for (i = 0; i < avf->nb_streams; ++i)
+            av_bsf_free(&tee_slave->bsfs[i]);
     }
     av_freep(&tee_slave->stream_map);
     av_freep(&tee_slave->bsfs);
@@ -217,7 +131,7 @@ static int open_slave(AVFormatContext *avf, char *slave, TeeSlave *tee_slave)
     int fullret;
     char *subselect = NULL, *next_subselect = NULL, *first_subselect = NULL, *tmp_select = NULL;
 
-    if ((ret = parse_slave_options(avf, slave, &options, &filename)) < 0)
+    if ((ret = ff_tee_parse_slave_options(avf, slave, &options, &filename)) < 0)
         return ret;
 
 #define STEAL_OPTION(option, field) do {                                \
@@ -247,6 +161,8 @@ static int open_slave(AVFormatContext *avf, char *slave, TeeSlave *tee_slave)
     avf2->opaque   = avf->opaque;
     avf2->io_open  = avf->io_open;
     avf2->io_close = avf->io_close;
+    avf2->interrupt_callback = avf->interrupt_callback;
+    avf2->flags = avf->flags;
 
     tee_slave->stream_map = av_calloc(avf->nb_streams, sizeof(*tee_slave->stream_map));
     if (!tee_slave->stream_map) {
@@ -352,7 +268,7 @@ static int open_slave(AVFormatContext *avf, char *slave, TeeSlave *tee_slave)
                            "output '%s', filters will be ignored\n", i, filename);
                     continue;
                 }
-                ret = parse_bsfs(avf, entry->value, &tee_slave->bsfs[i]);
+                ret = av_bsf_list_parse_str(entry->value, &tee_slave->bsfs[i]);
                 if (ret < 0) {
                     av_log(avf, AV_LOG_ERROR,
                            "Error parsing bitstream filter sequence '%s' associated to "
@@ -365,6 +281,37 @@ static int open_slave(AVFormatContext *avf, char *slave, TeeSlave *tee_slave)
         av_dict_set(&options, entry->key, NULL, 0);
     }
 
+    for (i = 0; i < avf->nb_streams; i++){
+        int target_stream = tee_slave->stream_map[i];
+        if (target_stream < 0)
+            continue;
+
+        if (!tee_slave->bsfs[target_stream]) {
+            /* Add pass-through bitstream filter */
+            ret = av_bsf_get_null_filter(&tee_slave->bsfs[target_stream]);
+            if (ret < 0) {
+                av_log(avf, AV_LOG_ERROR,
+                       "Failed to create pass-through bitstream filter: %s\n",
+                       av_err2str(ret));
+                goto end;
+            }
+        }
+
+        tee_slave->bsfs[target_stream]->time_base_in = avf->streams[i]->time_base;
+        ret = avcodec_parameters_copy(tee_slave->bsfs[target_stream]->par_in,
+                                      avf->streams[i]->codecpar);
+        if (ret < 0)
+            goto end;
+
+        ret = av_bsf_init(tee_slave->bsfs[target_stream]);
+        if (ret < 0) {
+            av_log(avf, AV_LOG_ERROR,
+            "Failed to initialize bitstream filter(s): %s\n",
+            av_err2str(ret));
+            goto end;
+        }
+    }
+
     if (options) {
         entry = NULL;
         while ((entry = av_dict_get(options, "", entry, AV_DICT_IGNORE_SUFFIX)))
@@ -389,20 +336,16 @@ static void log_slave(TeeSlave *slave, void *log_ctx, int log_level)
            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];
+        AVBSFContext *bsf = slave->bsfs[i];
+        const char *bsf_name;
 
         av_log(log_ctx, log_level, "    stream:%d codec:%s type:%s",
                i, avcodec_get_name(st->codecpar->codec_id),
                av_get_media_type_string(st->codecpar->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");
+
+        bsf_name = bsf->filter->priv_class ?
+                   bsf->filter->priv_class->item_name(bsf) : bsf->filter->name;
+        av_log(log_ctx, log_level, " bsfs: %s\n", bsf_name);
     }
 }
 
@@ -509,11 +452,11 @@ static int tee_write_packet(AVFormatContext *avf, AVPacket *pkt)
 {
     TeeContext *tee = avf->priv_data;
     AVFormatContext *avf2;
+    AVBSFContext *bsfs;
     AVPacket pkt2;
     int ret_all = 0, ret;
     unsigned i, s;
     int s2;
-    AVRational tb, tb2;
 
     for (i = 0; i < tee->nb_slaves; i++) {
         if (!(avf2 = tee->slaves[i].avf))
@@ -541,16 +484,35 @@ static int tee_write_packet(AVFormatContext *avf, AVPacket *pkt)
                 ret_all = ret;
                 continue;
             }
-        tb  = avf ->streams[s ]->time_base;
-        tb2 = avf2->streams[s2]->time_base;
-        pkt2.pts      = av_rescale_q(pkt->pts,      tb, tb2);
-        pkt2.dts      = av_rescale_q(pkt->dts,      tb, tb2);
-        pkt2.duration = av_rescale_q(pkt->duration, tb, tb2);
+        bsfs = tee->slaves[i].bsfs[s2];
         pkt2.stream_index = s2;
 
-        if ((ret = av_apply_bitstream_filters(avf2->streams[s2]->codec, &pkt2,
-                                              tee->slaves[i].bsfs[s2])) < 0 ||
-            (ret = av_interleaved_write_frame(avf2, &pkt2)) < 0) {
+        ret = av_bsf_send_packet(bsfs, &pkt2);
+        if (ret < 0) {
+            av_log(avf, AV_LOG_ERROR, "Error while sending packet to bitstream filter: %s\n",
+                   av_err2str(ret));
+            ret = tee_process_slave_failure(avf, i, ret);
+            if (!ret_all && ret < 0)
+                ret_all = ret;
+        }
+
+        while(1) {
+            ret = av_bsf_receive_packet(bsfs, &pkt2);
+            if (ret == AVERROR(EAGAIN)) {
+                ret = 0;
+                break;
+            } else if (ret < 0) {
+                break;
+            }
+
+            av_packet_rescale_ts(&pkt2, bsfs->time_base_out,
+                                 avf2->streams[s2]->time_base);
+            ret = av_interleaved_write_frame(avf2, &pkt2);
+            if (ret < 0)
+                break;
+        };
+
+        if (ret < 0) {
             ret = tee_process_slave_failure(avf, i, ret);
             if (!ret_all && ret < 0)
                 ret_all = ret;