[FFmpeg-devel] [RFC] avformat: access AVFormatContext.filename[] filed via getter/setter.

Zhang Rui bbcallen at gmail.com
Mon Sep 14 11:48:01 CEST 2015


URLs longer than 1024 could be truncated by AVFormatContext.
The field filename[1024] should be replaced with allocated string in future.
---
 ffmpeg.c                         |  8 +++----
 ffmpeg_opt.c                     |  8 +++----
 ffplay.c                         | 20 +++++++++---------
 ffprobe.c                        |  2 +-
 ffserver.c                       | 20 +++++++++++-------
 libavdevice/avfoundation.m       |  2 +-
 libavdevice/lavfi.c              |  2 +-
 libavdevice/qtkit.m              |  8 +++----
 libavdevice/sdl.c                |  2 +-
 libavformat/avformat.h           |  9 +++++---
 libavformat/concatdec.c          |  4 ++--
 libavformat/dashenc.c            | 12 +++++------
 libavformat/gxfenc.c             |  4 ++--
 libavformat/hdsenc.c             | 24 ++++++++++-----------
 libavformat/hls.c                |  2 +-
 libavformat/hlsenc.c             | 45 +++++++++++++++++++++-------------------
 libavformat/img2dec.c            |  4 ++--
 libavformat/img2enc.c            |  4 ++--
 libavformat/matroskadec.c        |  4 ++--
 libavformat/mlvdec.c             |  4 ++--
 libavformat/mov.c                |  2 +-
 libavformat/movenc.c             | 10 ++++-----
 libavformat/mpeg.c               |  4 ++--
 libavformat/mpegtsenc.c          |  2 +-
 libavformat/mux.c                |  2 +-
 libavformat/nsvdec.c             |  2 +-
 libavformat/rtsp.c               | 10 ++++-----
 libavformat/rtspdec.c            |  4 ++--
 libavformat/sapdec.c             |  2 +-
 libavformat/sapenc.c             |  4 ++--
 libavformat/sdp.c                |  4 ++--
 libavformat/segment.c            | 36 ++++++++++++++++++--------------
 libavformat/smoothstreamingenc.c | 12 +++++------
 libavformat/tee.c                |  6 +++---
 libavformat/utils.c              | 17 +++++++++++++--
 libavformat/webm_chunk.c         | 12 ++++++-----
 36 files changed, 174 insertions(+), 143 deletions(-)

diff --git a/ffmpeg.c b/ffmpeg.c
index e31a2c6..048a46d 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -1447,7 +1447,7 @@ static void print_final_stats(int64_t total_size)
         uint64_t total_packets = 0, total_size = 0;
 
         av_log(NULL, AV_LOG_VERBOSE, "Input file #%d (%s):\n",
-               i, f->ctx->filename);
+               i, av_format_get_filename(f->ctx));
 
         for (j = 0; j < f->nb_streams; j++) {
             InputStream *ist = input_streams[f->ist_index + j];
@@ -1481,7 +1481,7 @@ static void print_final_stats(int64_t total_size)
         uint64_t total_packets = 0, total_size = 0;
 
         av_log(NULL, AV_LOG_VERBOSE, "Output file #%d (%s):\n",
-               i, of->ctx->filename);
+               i, av_format_get_filename(of->ctx));
 
         for (j = 0; j < of->ctx->nb_streams; j++) {
             OutputStream *ost = output_streams[of->ost_index + j];
@@ -3220,7 +3220,7 @@ static int transcode_init(void)
     /* dump the file output parameters - cannot be done before in case
        of stream copy */
     for (i = 0; i < nb_output_files; i++) {
-        av_dump_format(output_files[i]->ctx, i, output_files[i]->ctx->filename, 1);
+        av_dump_format(output_files[i]->ctx, i, av_format_get_filename(output_files[i]->ctx), 1);
     }
 
     /* dump the stream mapping */
@@ -3636,7 +3636,7 @@ static int process_input(int file_index)
     }
     if (ret < 0) {
         if (ret != AVERROR_EOF) {
-            print_error(is->filename, ret);
+            print_error(av_format_get_filename(is), ret);
             if (exit_on_error)
                 exit_program(1);
         }
diff --git a/ffmpeg_opt.c b/ffmpeg_opt.c
index 55818e1..746dd50 100644
--- a/ffmpeg_opt.c
+++ b/ffmpeg_opt.c
@@ -1118,7 +1118,7 @@ static void choose_encoder(OptionsContext *o, AVFormatContext *s, OutputStream *
 
     MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, ost->st);
     if (!codec_name) {
-        ost->st->codec->codec_id = av_guess_codec(s->oformat, NULL, s->filename,
+        ost->st->codec->codec_id = av_guess_codec(s->oformat, NULL, av_format_get_filename(s),
                                                   NULL, ost->st->codec->codec_type);
         ost->enc = avcodec_find_encoder(ost->st->codec->codec_id);
     } else if (!strcmp(codec_name, "copy"))
@@ -2179,7 +2179,7 @@ loop_end:
     }
 
     if (!oc->nb_streams && !(oc->oformat->flags & AVFMT_NOSTREAMS)) {
-        av_dump_format(oc, nb_output_files - 1, oc->filename, 1);
+        av_dump_format(oc, nb_output_files - 1, av_format_get_filename(oc), 1);
         av_log(NULL, AV_LOG_ERROR, "Output file #%d does not contain any stream\n", nb_output_files - 1);
         exit_program(1);
     }
@@ -2239,8 +2239,8 @@ loop_end:
 
     /* check filename in case of an image number is expected */
     if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
-        if (!av_filename_number_test(oc->filename)) {
-            print_error(oc->filename, AVERROR(EINVAL));
+        if (!av_filename_number_test(av_format_get_filename(oc))) {
+            print_error(av_format_get_filename(oc), AVERROR(EINVAL));
             exit_program(1);
         }
     }
diff --git a/ffplay.c b/ffplay.c
index d302793..bc22edf 100644
--- a/ffplay.c
+++ b/ffplay.c
@@ -2753,8 +2753,8 @@ static int is_realtime(AVFormatContext *s)
     )
         return 1;
 
-    if(s->pb && (   !strncmp(s->filename, "rtp:", 4)
-                 || !strncmp(s->filename, "udp:", 4)
+    if(s->pb && (   !strncmp(av_format_get_filename(s), "rtp:", 4)
+                 || !strncmp(av_format_get_filename(s), "udp:", 4)
                 )
     )
         return 1;
@@ -2796,9 +2796,9 @@ static int read_thread(void *arg)
         av_dict_set(&format_opts, "scan_all_pmts", "1", AV_DICT_DONT_OVERWRITE);
         scan_all_pmts_set = 1;
     }
-    err = avformat_open_input(&ic, is->filename, is->iformat, &format_opts);
+    err = avformat_open_input(&ic, av_format_get_filename(is), is->iformat, &format_opts);
     if (err < 0) {
-        print_error(is->filename, err);
+        print_error(av_format_get_filename(is), err);
         ret = -1;
         goto fail;
     }
@@ -2828,7 +2828,7 @@ static int read_thread(void *arg)
 
     if (err < 0) {
         av_log(NULL, AV_LOG_WARNING,
-               "%s: could not find codec parameters\n", is->filename);
+               "%s: could not find codec parameters\n", av_format_get_filename(is));
         ret = -1;
         goto fail;
     }
@@ -2855,14 +2855,14 @@ static int read_thread(void *arg)
         ret = avformat_seek_file(ic, -1, INT64_MIN, timestamp, INT64_MAX, 0);
         if (ret < 0) {
             av_log(NULL, AV_LOG_WARNING, "%s: could not seek to position %0.3f\n",
-                    is->filename, (double)timestamp / AV_TIME_BASE);
+                    av_format_get_filename(is), (double)timestamp / AV_TIME_BASE);
         }
     }
 
     is->realtime = is_realtime(ic);
 
     if (show_status)
-        av_dump_format(ic, 0, is->filename, 0);
+        av_dump_format(ic, 0, av_format_get_filename(is), 0);
 
     for (i = 0; i < ic->nb_streams; i++) {
         AVStream *st = ic->streams[i];
@@ -2925,7 +2925,7 @@ static int read_thread(void *arg)
 
     if (is->video_stream < 0 && is->audio_stream < 0) {
         av_log(NULL, AV_LOG_FATAL, "Failed to open file '%s' or configure filtergraph\n",
-               is->filename);
+               av_format_get_filename(is));
         ret = -1;
         goto fail;
     }
@@ -2963,7 +2963,7 @@ static int read_thread(void *arg)
             ret = avformat_seek_file(is->ic, -1, seek_min, seek_target, seek_max, is->seek_flags);
             if (ret < 0) {
                 av_log(NULL, AV_LOG_ERROR,
-                       "%s: error while seeking\n", is->ic->filename);
+                       "%s: error while seeking\n", av_format_get_filename(is->ic));
             } else {
                 if (is->audio_stream >= 0) {
                     packet_queue_flush(&is->audioq);
@@ -3099,7 +3099,7 @@ static VideoState *stream_open(const char *filename, AVInputFormat *iformat)
     is = av_mallocz(sizeof(VideoState));
     if (!is)
         return NULL;
-    av_strlcpy(is->filename, filename, sizeof(is->filename));
+    av_format_set_filename(is, filename);
     is->iformat = iformat;
     is->ytop    = 0;
     is->xleft   = 0;
diff --git a/ffprobe.c b/ffprobe.c
index ac03689..b61a964 100644
--- a/ffprobe.c
+++ b/ffprobe.c
@@ -2426,7 +2426,7 @@ static int show_format(WriterContext *w, AVFormatContext *fmt_ctx)
     int ret = 0;
 
     writer_print_section_header(w, SECTION_ID_FORMAT);
-    print_str_validate("filename", fmt_ctx->filename);
+    print_str_validate("filename", av_format_get_filename(fmt_ctx));
     print_int("nb_streams",       fmt_ctx->nb_streams);
     print_int("nb_programs",      fmt_ctx->nb_programs);
     print_str("format_name",      fmt_ctx->iformat->name);
diff --git a/ffserver.c b/ffserver.c
index 73ede87..052a7a3 100644
--- a/ffserver.c
+++ b/ffserver.c
@@ -2845,6 +2845,7 @@ static int prepare_sdp_description(FFServerStream *stream, uint8_t **pbuffer,
     AVOutputFormat *rtp_format = av_guess_format("rtp", NULL, NULL);
     AVDictionaryEntry *entry = av_dict_get(stream->metadata, "title", NULL, 0);
     int i;
+    char filename_buf[1024];
 
     *pbuffer = NULL;
 
@@ -2857,11 +2858,13 @@ static int prepare_sdp_description(FFServerStream *stream, uint8_t **pbuffer,
                 entry ? entry->value : "No Title", 0);
     avc->nb_streams = stream->nb_streams;
     if (stream->is_multicast) {
-        snprintf(avc->filename, 1024, "rtp://%s:%d?multicast=1?ttl=%d",
+        snprintf(filename_buf, 1024, "rtp://%s:%d?multicast=1?ttl=%d",
                  inet_ntoa(stream->multicast_ip),
                  stream->multicast_port, stream->multicast_ttl);
-    } else
-        snprintf(avc->filename, 1024, "rtp://0.0.0.0");
+    } else {
+        snprintf(filename_buf, 1024, "rtp://0.0.0.0");
+        av_format_set_filename(avc, filename_buf);
+    }
 
     avc->streams = av_malloc_array(avc->nb_streams, sizeof(*avc->streams));
     if (!avc->streams)
@@ -3296,6 +3299,7 @@ static int rtp_new_av_stream(HTTPContext *c,
     URLContext *h = NULL;
     uint8_t *dummy_buf;
     int max_packet_size;
+    char filename_buf[1024];
 
     /* now we can open the relevant output stream */
     ctx = avformat_alloc_context();
@@ -3335,15 +3339,17 @@ static int rtp_new_av_stream(HTTPContext *c,
             ttl = c->stream->multicast_ttl;
             if (!ttl)
                 ttl = 16;
-            snprintf(ctx->filename, sizeof(ctx->filename),
+            snprintf(filename_buf, sizeof(filename_buf),
                      "rtp://%s:%d?multicast=1&ttl=%d",
                      ipaddr, ntohs(dest_addr->sin_port), ttl);
+            av_format_set_filename(ctx, filename_buf);
         } else {
-            snprintf(ctx->filename, sizeof(ctx->filename),
+            snprintf(filename_buf, sizeof(filename_buf),
                      "rtp://%s:%d", ipaddr, ntohs(dest_addr->sin_port));
+            av_format_set_filename(ctx, filename_buf);
         }
 
-        if (ffurl_open(&h, ctx->filename, AVIO_FLAG_WRITE, NULL, NULL) < 0)
+        if (ffurl_open(&h, av_format_get_filename(ctx), AVIO_FLAG_WRITE, NULL, NULL) < 0)
             goto fail;
         c->rtp_handles[stream_index] = h;
         max_packet_size = h->max_packet_size;
@@ -3494,7 +3500,7 @@ static void extract_mpeg4_header(AVFormatContext *infile)
         return;
 
     printf("MPEG4 without extra data: trying to find header in %s\n",
-           infile->filename);
+           av_format_get_filename(infile));
     while (mpeg4_count > 0) {
         if (av_read_frame(infile, &pkt) < 0)
             break;
diff --git a/libavdevice/avfoundation.m b/libavdevice/avfoundation.m
index 763e675..a9aed68 100644
--- a/libavdevice/avfoundation.m
+++ b/libavdevice/avfoundation.m
@@ -259,7 +259,7 @@ static void destroy_context(AVFContext* ctx)
 static void parse_device_name(AVFormatContext *s)
 {
     AVFContext *ctx = (AVFContext*)s->priv_data;
-    char *tmp = av_strdup(s->filename);
+    char *tmp = av_strdup(av_format_get_filename(s));
     char *save;
 
     if (tmp[0] != ':') {
diff --git a/libavdevice/lavfi.c b/libavdevice/lavfi.c
index 3453b4d..a730039 100644
--- a/libavdevice/lavfi.c
+++ b/libavdevice/lavfi.c
@@ -161,7 +161,7 @@ av_cold static int lavfi_read_header(AVFormatContext *avctx)
     }
 
     if (!lavfi->graph_str)
-        lavfi->graph_str = av_strdup(avctx->filename);
+        lavfi->graph_str = av_strdup(av_format_get_filename(avctx));
 
     /* parse the graph, create a stream for each open output */
     if (!(lavfi->graph = avfilter_graph_alloc()))
diff --git a/libavdevice/qtkit.m b/libavdevice/qtkit.m
index 22a94ca..3d8ccec 100644
--- a/libavdevice/qtkit.m
+++ b/libavdevice/qtkit.m
@@ -169,7 +169,7 @@ static int qtkit_read_header(AVFormatContext *s)
 
     // check for device index given in filename
     if (ctx->video_device_index == -1) {
-        sscanf(s->filename, "%d", &ctx->video_device_index);
+        sscanf(av_format_get_filename(s), "%d", &ctx->video_device_index);
     }
 
     if (ctx->video_device_index >= 0) {
@@ -181,12 +181,12 @@ static int qtkit_read_header(AVFormatContext *s)
         }
 
         video_device = [devices objectAtIndex:ctx->video_device_index];
-    } else if (strncmp(s->filename, "",        1) &&
-               strncmp(s->filename, "default", 7)) {
+    } else if (strncmp(av_format_get_filename(s), "",        1) &&
+               strncmp(av_format_get_filename(s), "default", 7)) {
         NSArray *devices = [QTCaptureDevice inputDevicesWithMediaType:QTMediaTypeVideo];
 
         for (QTCaptureDevice *device in devices) {
-            if (!strncmp(s->filename, [[device localizedDisplayName] UTF8String], strlen(s->filename))) {
+            if (!strncmp(av_format_get_filename(s), [[device localizedDisplayName] UTF8String], strlen(av_format_get_filename(s)))) {
                 video_device = device;
                 break;
             }
diff --git a/libavdevice/sdl.c b/libavdevice/sdl.c
index b98aae5..7c4ed89 100644
--- a/libavdevice/sdl.c
+++ b/libavdevice/sdl.c
@@ -237,7 +237,7 @@ static int sdl_write_header(AVFormatContext *s)
     int i, ret;
 
     if (!sdl->window_title)
-        sdl->window_title = av_strdup(s->filename);
+        sdl->window_title = av_strdup(av_format_get_filename(s));
     if (!sdl->icon_title)
         sdl->icon_title = av_strdup(sdl->window_title);
 
diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index b7f18c1..c6f163b 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -1327,10 +1327,11 @@ typedef struct AVFormatContext {
     AVStream **streams;
 
     /**
-     * input or output filename
+     * @deprecated deprecated in favor of filename2
      *
-     * - demuxing: set by avformat_open_input()
-     * - muxing: may be set by the caller before avformat_write_header()
+     * Must not be accessed directly from outside avformat.
+     * @See av_format_set_filename()
+     * @See av_format_get_filename()
      */
     char filename[1024];
 
@@ -1824,6 +1825,8 @@ av_format_control_message av_format_get_control_message_cb(const AVFormatContext
 void      av_format_set_control_message_cb(AVFormatContext *s, av_format_control_message callback);
 AVOpenCallback av_format_get_open_cb(const AVFormatContext *s);
 void      av_format_set_open_cb(AVFormatContext *s, AVOpenCallback callback);
+void      av_format_set_filename(AVFormatContext *s, const char *filename);
+const char *av_format_get_filename(AVFormatContext *s);
 
 /**
  * This function will cause global side data to be injected in the next packet
diff --git a/libavformat/concatdec.c b/libavformat/concatdec.c
index 88b6dbe..417c7fb9 100644
--- a/libavformat/concatdec.c
+++ b/libavformat/concatdec.c
@@ -124,10 +124,10 @@ static int add_file(AVFormatContext *avf, char *filename, ConcatFile **rfile,
         url = filename;
         filename = NULL;
     } else {
-        url_len = strlen(avf->filename) + strlen(filename) + 16;
+        url_len = strlen(av_format_get_filename(avf)) + strlen(filename) + 16;
         if (!(url = av_malloc(url_len)))
             FAIL(AVERROR(ENOMEM));
-        ff_make_absolute_url(url, url_len, avf->filename, filename);
+        ff_make_absolute_url(url, url_len, av_format_get_filename(avf), filename);
         av_freep(&filename);
     }
 
diff --git a/libavformat/dashenc.c b/libavformat/dashenc.c
index 7a93214..0d66115 100644
--- a/libavformat/dashenc.c
+++ b/libavformat/dashenc.c
@@ -443,7 +443,7 @@ static int write_manifest(AVFormatContext *s, int final)
     int ret, i;
     AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
 
-    snprintf(temp_filename, sizeof(temp_filename), "%s.tmp", s->filename);
+    snprintf(temp_filename, sizeof(temp_filename), "%s.tmp", av_format_get_filename(s));
     ret = avio_open2(&out, temp_filename, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL);
     if (ret < 0) {
         av_log(s, AV_LOG_ERROR, "Unable to open %s for writing\n", temp_filename);
@@ -537,7 +537,7 @@ static int write_manifest(AVFormatContext *s, int final)
     avio_printf(out, "</MPD>\n");
     avio_flush(out);
     avio_close(out);
-    return ff_rename(temp_filename, s->filename, s);
+    return ff_rename(temp_filename, av_format_get_filename(s), s);
 }
 
 static int dash_write_header(AVFormatContext *s)
@@ -553,14 +553,14 @@ static int dash_write_header(AVFormatContext *s)
     if (c->single_file)
         c->use_template = 0;
 
-    av_strlcpy(c->dirname, s->filename, sizeof(c->dirname));
+    av_strlcpy(c->dirname, av_format_get_filename(s), sizeof(c->dirname));
     ptr = strrchr(c->dirname, '/');
     if (ptr) {
         av_strlcpy(basename, &ptr[1], sizeof(basename));
         ptr[1] = '\0';
     } else {
         c->dirname[0] = '\0';
-        av_strlcpy(basename, s->filename, sizeof(basename));
+        av_strlcpy(basename, av_format_get_filename(s), sizeof(basename));
     }
 
     ptr = strrchr(basename, '.');
@@ -673,7 +673,7 @@ static int dash_write_header(AVFormatContext *s)
     }
     ret = write_manifest(s, 0);
     if (!ret)
-        av_log(s, AV_LOG_VERBOSE, "Manifest written to: %s\n", s->filename);
+        av_log(s, AV_LOG_VERBOSE, "Manifest written to: %s\n", av_format_get_filename(s));
 
 fail:
     if (ret)
@@ -968,7 +968,7 @@ static int dash_write_trailer(AVFormatContext *s)
             snprintf(filename, sizeof(filename), "%s%s", c->dirname, os->initfile);
             unlink(filename);
         }
-        unlink(s->filename);
+        unlink(av_format_get_filename(s));
     }
 
     dash_free(s);
diff --git a/libavformat/gxfenc.c b/libavformat/gxfenc.c
index 12031f7..b4538ac 100644
--- a/libavformat/gxfenc.c
+++ b/libavformat/gxfenc.c
@@ -311,7 +311,7 @@ static int gxf_write_material_data_section(AVFormatContext *s)
     AVIOContext *pb = s->pb;
     int64_t pos;
     int len;
-    const char *filename = strrchr(s->filename, '/');
+    const char *filename = strrchr(av_format_get_filename(s), '/');
 
     pos = avio_tell(pb);
     avio_wb16(pb, 0); /* size */
@@ -320,7 +320,7 @@ static int gxf_write_material_data_section(AVFormatContext *s)
     if (filename)
         filename++;
     else
-        filename = s->filename;
+        filename = av_format_get_filename(s);
     len = strlen(filename);
 
     avio_w8(pb, MAT_NAME);
diff --git a/libavformat/hdsenc.c b/libavformat/hdsenc.c
index 575cc20..45d5c29 100644
--- a/libavformat/hdsenc.c
+++ b/libavformat/hdsenc.c
@@ -167,8 +167,8 @@ static int write_manifest(AVFormatContext *s, int final)
     if (c->nb_streams > 0)
         duration = c->streams[0].last_ts * av_q2d(s->streams[0]->time_base);
 
-    snprintf(filename, sizeof(filename), "%s/index.f4m", s->filename);
-    snprintf(temp_filename, sizeof(temp_filename), "%s/index.f4m.tmp", s->filename);
+    snprintf(filename, sizeof(filename), "%s/index.f4m", av_format_get_filename(s));
+    snprintf(temp_filename, sizeof(temp_filename), "%s/index.f4m.tmp", av_format_get_filename(s));
     ret = avio_open2(&out, temp_filename, AVIO_FLAG_WRITE,
                      &s->interrupt_callback, NULL);
     if (ret < 0) {
@@ -177,7 +177,7 @@ static int write_manifest(AVFormatContext *s, int final)
     }
     avio_printf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
     avio_printf(out, "<manifest xmlns=\"http://ns.adobe.com/f4m/1.0\">\n");
-    avio_printf(out, "\t<id>%s</id>\n", av_basename(s->filename));
+    avio_printf(out, "\t<id>%s</id>\n", av_basename(av_format_get_filename(s)));
     avio_printf(out, "\t<streamType>%s</streamType>\n",
                      final ? "recorded" : "live");
     avio_printf(out, "\t<deliveryType>streaming</deliveryType>\n");
@@ -235,9 +235,9 @@ static int write_abst(AVFormatContext *s, OutputStream *os, int final)
         cur_media_time = os->fragments[os->nb_fragments - 1]->start_time;
 
     snprintf(filename, sizeof(filename),
-             "%s/stream%d.abst", s->filename, index);
+             "%s/stream%d.abst", av_format_get_filename(s), index);
     snprintf(temp_filename, sizeof(temp_filename),
-             "%s/stream%d.abst.tmp", s->filename, index);
+             "%s/stream%d.abst.tmp", av_format_get_filename(s), index);
     ret = avio_open2(&out, temp_filename, AVIO_FLAG_WRITE,
                      &s->interrupt_callback, NULL);
     if (ret < 0) {
@@ -318,9 +318,9 @@ static int hds_write_header(AVFormatContext *s)
     int ret = 0, i;
     AVOutputFormat *oformat;
 
-    if (mkdir(s->filename, 0777) == -1 && errno != EEXIST) {
+    if (mkdir(av_format_get_filename(s), 0777) == -1 && errno != EEXIST) {
         ret = AVERROR(errno);
-        av_log(s, AV_LOG_ERROR , "Failed to create directory %s\n", s->filename);
+        av_log(s, AV_LOG_ERROR , "Failed to create directory %s\n", av_format_get_filename(s));
         goto fail;
     }
 
@@ -412,7 +412,7 @@ static int hds_write_header(AVFormatContext *s)
             s->streams[os->first_stream + j]->time_base = os->ctx->streams[j]->time_base;
 
         snprintf(os->temp_filename, sizeof(os->temp_filename),
-                 "%s/stream%d_temp", s->filename, i);
+                 "%s/stream%d_temp", av_format_get_filename(s), i);
         ret = init_file(s, os, 0);
         if (ret < 0)
             goto fail;
@@ -477,7 +477,7 @@ static int hds_flush(AVFormatContext *s, OutputStream *os, int final,
     close_file(os);
 
     snprintf(target_filename, sizeof(target_filename),
-             "%s/stream%dSeg1-Frag%d", s->filename, index, os->fragment_index);
+             "%s/stream%dSeg1-Frag%d", av_format_get_filename(s), index, os->fragment_index);
     ret = ff_rename(os->temp_filename, target_filename, s);
     if (ret < 0)
         return ret;
@@ -550,13 +550,13 @@ static int hds_write_trailer(AVFormatContext *s)
 
     if (c->remove_at_exit) {
         char filename[1024];
-        snprintf(filename, sizeof(filename), "%s/index.f4m", s->filename);
+        snprintf(filename, sizeof(filename), "%s/index.f4m", av_format_get_filename(s));
         unlink(filename);
         for (i = 0; i < c->nb_streams; i++) {
-            snprintf(filename, sizeof(filename), "%s/stream%d.abst", s->filename, i);
+            snprintf(filename, sizeof(filename), "%s/stream%d.abst", av_format_get_filename(s), i);
             unlink(filename);
         }
-        rmdir(s->filename);
+        rmdir(av_format_get_filename(s));
     }
 
     hds_free(s);
diff --git a/libavformat/hls.c b/libavformat/hls.c
index c16c770..09d079a 100644
--- a/libavformat/hls.c
+++ b/libavformat/hls.c
@@ -1352,7 +1352,7 @@ static int hls_read_header(AVFormatContext *s)
         update_options(&c->headers, "headers", u->priv_data);
     }
 
-    if ((ret = parse_playlist(c, s->filename, NULL, s->pb)) < 0)
+    if ((ret = parse_playlist(c, av_format_get_filename(s), NULL, s->pb)) < 0)
         goto fail;
 
     if ((ret = save_avio_options(s)) < 0)
diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index d7bb0c1..56d2e4a 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -144,7 +144,7 @@ static int hls_delete_old_segments(HLSContext *hls) {
         if (hls->segment_filename) {
             dirname = av_strdup(hls->segment_filename);
         } else {
-            dirname = av_strdup(hls->avf->filename);
+            dirname = av_strdup(av_format_get_filename(hls->avf));
         }
         if (!dirname) {
             ret = AVERROR(ENOMEM);
@@ -306,10 +306,10 @@ static int hls_append_segment(HLSContext *hls, double duration, int64_t pos,
     if (!en)
         return AVERROR(ENOMEM);
 
-    av_strlcpy(en->filename, av_basename(hls->avf->filename), sizeof(en->filename));
+    av_strlcpy(en->filename, av_basename(av_format_get_filename(hls->avf)), sizeof(en->filename));
 
     if(hls->has_subtitle)
-        av_strlcpy(en->sub_filename, av_basename(hls->vtt_avf->filename), sizeof(en->sub_filename));
+        av_strlcpy(en->sub_filename, av_basename(av_format_get_filename(hls->vtt_avf)), sizeof(en->sub_filename));
 
     en->duration = duration;
     en->pos      = pos;
@@ -369,7 +369,7 @@ static int hls_window(AVFormatContext *s, int last)
     char temp_filename[1024];
     int64_t sequence = FFMAX(hls->start_sequence, hls->sequence - hls->nb_entries);
     int version = hls->flags & HLS_SINGLE_FILE ? 4 : 3;
-    const char *proto = avio_find_protocol_name(s->filename);
+    const char *proto = avio_find_protocol_name(av_format_get_filename(s));
     int use_rename = proto && !strcmp(proto, "file");
     static unsigned warned_non_file;
     char *key_uri = NULL;
@@ -378,7 +378,7 @@ static int hls_window(AVFormatContext *s, int last)
     if (!use_rename && !warned_non_file++)
         av_log(s, AV_LOG_ERROR, "Cannot use rename on non file protocol, this may lead to races and temporarly partial files\n");
 
-    snprintf(temp_filename, sizeof(temp_filename), use_rename ? "%s.tmp" : "%s", s->filename);
+    snprintf(temp_filename, sizeof(temp_filename), use_rename ? "%s.tmp" : "%s", av_format_get_filename(s));
     if ((ret = avio_open2(&out, temp_filename, AVIO_FLAG_WRITE,
                           &s->interrupt_callback, NULL)) < 0)
         goto fail;
@@ -463,7 +463,7 @@ fail:
     avio_closep(&out);
     avio_closep(&sub_out);
     if (ret >= 0 && use_rename)
-        ff_rename(temp_filename, s->filename, s);
+        ff_rename(temp_filename, av_format_get_filename(s), s);
     return ret;
 }
 
@@ -475,35 +475,38 @@ static int hls_start(AVFormatContext *s)
     AVDictionary *options = NULL;
     char *filename, iv_string[KEYSIZE*2 + 1];
     int err = 0;
+    char filename_buf[1024];
 
     if (c->flags & HLS_SINGLE_FILE) {
-        av_strlcpy(oc->filename, c->basename,
-                   sizeof(oc->filename));
+        av_format_set_filename(oc, c->basename);
         if (c->vtt_basename)
-            av_strlcpy(vtt_oc->filename, c->vtt_basename,
-                  sizeof(vtt_oc->filename));
+            av_format_set_filename(vtt_oc, c->vtt_basename);
     } else {
         if (c->use_localtime) {
             time_t now0;
             struct tm *tm, tmpbuf;
             time(&now0);
             tm = localtime_r(&now0, &tmpbuf);
-            if (!strftime(oc->filename, sizeof(oc->filename), c->basename, tm)) {
+            if (!strftime(filename_buf, sizeof(filename_buf), c->basename, tm)) {
                 av_log(oc, AV_LOG_ERROR, "Could not get segment filename with use_localtime\n");
                 return AVERROR(EINVAL);
             }
-       } else if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
+            av_format_set_filename(oc, filename_buf);
+       } else if (av_get_frame_filename(filename_buf, sizeof(filename_buf),
                                   c->basename, c->wrap ? c->sequence % c->wrap : c->sequence) < 0) {
             av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s' you can try use -use_localtime 1 with it\n", c->basename);
             return AVERROR(EINVAL);
+        } else {
+            av_format_set_filename(oc, filename_buf);
         }
         if( c->vtt_basename) {
-            if (av_get_frame_filename(vtt_oc->filename, sizeof(vtt_oc->filename),
+            if (av_get_frame_filename(filename_buf, sizeof(filename_buf),
                               c->vtt_basename, c->wrap ? c->sequence % c->wrap : c->sequence) < 0) {
                 av_log(vtt_oc, AV_LOG_ERROR, "Invalid segment filename template '%s'\n", c->vtt_basename);
                 return AVERROR(EINVAL);
             }
        }
+        av_format_set_filename(vtt_oc, filename_buf);
     }
     c->number++;
 
@@ -519,7 +522,7 @@ static int hls_start(AVFormatContext *s)
         if ((err = av_dict_set(&options, "encryption_iv", iv_string, 0)) < 0)
             return err;
 
-        filename = av_asprintf("crypto:%s", oc->filename);
+        filename = av_asprintf("crypto:%s", av_format_get_filename(oc));
         if (!filename) {
             av_dict_free(&options);
             return AVERROR(ENOMEM);
@@ -531,11 +534,11 @@ static int hls_start(AVFormatContext *s)
         if (err < 0)
             return err;
     } else
-        if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
+        if ((err = avio_open2(&oc->pb, av_format_get_filename(oc), AVIO_FLAG_WRITE,
                           &s->interrupt_callback, NULL)) < 0)
             return err;
     if (c->vtt_basename) {
-        if ((err = avio_open2(&vtt_oc->pb, vtt_oc->filename, AVIO_FLAG_WRITE,
+        if ((err = avio_open2(&vtt_oc->pb, av_format_get_filename(vtt_oc), AVIO_FLAG_WRITE,
                          &s->interrupt_callback, NULL)) < 0)
             return err;
     }
@@ -611,9 +614,9 @@ static int hls_write_header(AVFormatContext *s)
             pattern = ".ts";
 
         if (hls->use_localtime) {
-            basename_size = strlen(s->filename) + strlen(pattern_localtime_fmt) + 1;
+            basename_size = strlen(av_format_get_filename(s)) + strlen(pattern_localtime_fmt) + 1;
         } else {
-            basename_size = strlen(s->filename) + strlen(pattern) + 1;
+            basename_size = strlen(av_format_get_filename(s)) + strlen(pattern) + 1;
         }
         hls->basename = av_malloc(basename_size);
         if (!hls->basename) {
@@ -621,7 +624,7 @@ static int hls_write_header(AVFormatContext *s)
             goto fail;
         }
 
-        av_strlcpy(hls->basename, s->filename, basename_size);
+        av_strlcpy(hls->basename, av_format_get_filename(s), basename_size);
 
         p = strrchr(hls->basename, '.');
         if (p)
@@ -637,7 +640,7 @@ static int hls_write_header(AVFormatContext *s)
 
         if (hls->flags & HLS_SINGLE_FILE)
             vtt_pattern = ".vtt";
-        vtt_basename_size = strlen(s->filename) + strlen(vtt_pattern) + 1;
+        vtt_basename_size = strlen(av_format_get_filename(s)) + strlen(vtt_pattern) + 1;
         hls->vtt_basename = av_malloc(vtt_basename_size);
         if (!hls->vtt_basename) {
             ret = AVERROR(ENOMEM);
@@ -648,7 +651,7 @@ static int hls_write_header(AVFormatContext *s)
             ret = AVERROR(ENOMEM);
             goto fail;
         }
-        av_strlcpy(hls->vtt_basename, s->filename, vtt_basename_size);
+        av_strlcpy(hls->vtt_basename, av_format_get_filename(s), vtt_basename_size);
         p = strrchr(hls->vtt_basename, '.');
         if (p)
             *p = '\0';
diff --git a/libavformat/img2dec.c b/libavformat/img2dec.c
index 70f0b09..dd41609 100644
--- a/libavformat/img2dec.c
+++ b/libavformat/img2dec.c
@@ -195,7 +195,7 @@ int ff_img_read_header(AVFormatContext *s1)
         return AVERROR(EINVAL);
     }
 
-    av_strlcpy(s->path, s1->filename, sizeof(s->path));
+    av_strlcpy(s->path, av_format_get_filename(s1), sizeof(s->path));
     s->img_number = 0;
     s->img_count  = 0;
 
@@ -328,7 +328,7 @@ int ff_img_read_header(AVFormatContext *s1)
 
             pd.buf = probe_buffer;
             pd.buf_size = probe_buffer_size;
-            pd.filename = s1->filename;
+            pd.filename = av_format_get_filename(s1);
 
             while ((fmt = av_iformat_next(fmt))) {
                 if (fmt->read_header != ff_img_read_header ||
diff --git a/libavformat/img2enc.c b/libavformat/img2enc.c
index 48454fe..d53ccff 100644
--- a/libavformat/img2enc.c
+++ b/libavformat/img2enc.c
@@ -49,7 +49,7 @@ static int write_header(AVFormatContext *s)
     AVStream *st = s->streams[0];
     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(st->codec->pix_fmt);
 
-    av_strlcpy(img->path, s->filename, sizeof(img->path));
+    av_strlcpy(img->path, av_format_get_filename(s), sizeof(img->path));
 
     /* find format */
     if (s->oformat->flags & AVFMT_NOFILE)
@@ -138,7 +138,7 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt)
 
         av_assert0(!img->split_planes);
 
-        ret = avformat_alloc_output_context2(&fmt, NULL, img->muxer, s->filename);
+        ret = avformat_alloc_output_context2(&fmt, NULL, img->muxer, av_format_get_filename(s));
         if (ret < 0)
             return ret;
         st = avformat_new_stream(fmt, NULL);
diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c
index 43ad9af..ce590ed 100644
--- a/libavformat/matroskadec.c
+++ b/libavformat/matroskadec.c
@@ -3477,8 +3477,8 @@ static int webm_dash_manifest_read_header(AVFormatContext *s)
     }
 
     // basename of the file
-    buf = strrchr(s->filename, '/');
-    av_dict_set(&s->streams[0]->metadata, FILENAME, buf ? ++buf : s->filename, 0);
+    buf = strrchr(av_format_get_filename(s), '/');
+    av_dict_set(&s->streams[0]->metadata, FILENAME, buf ? ++buf : av_format_get_filename(s), 0);
 
     // track number
     tracks = matroska->tracks.elem;
diff --git a/libavformat/mlvdec.c b/libavformat/mlvdec.c
index 48a429e..d1ec896 100644
--- a/libavformat/mlvdec.c
+++ b/libavformat/mlvdec.c
@@ -329,9 +329,9 @@ static int read_header(AVFormatContext *avctx)
         return ret;
 
     /* scan secondary files */
-    if (strlen(avctx->filename) > 2) {
+    if (strlen(av_format_get_filename(avctx)) > 2) {
         int i;
-        char *filename = av_strdup(avctx->filename);
+        char *filename = av_strdup(av_format_get_filename(avctx));
         AVOpenCallback open_func = avctx->open_cb;
 
         if (!filename)
diff --git a/libavformat/mov.c b/libavformat/mov.c
index c57aaeb..e509904 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -2996,7 +2996,7 @@ static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom)
 
     if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
         MOVDref *dref = &sc->drefs[sc->dref_id - 1];
-        if (mov_open_dref(c, &sc->pb, c->fc->filename, dref,
+        if (mov_open_dref(c, &sc->pb, av_format_get_filename(c->fc), dref,
                           &c->fc->interrupt_callback) < 0)
             av_log(c->fc, AV_LOG_ERROR,
                    "stream %d, error opening alias: path='%s', dir='%s', "
diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index 08d0c2a..1d238a7 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -1162,9 +1162,9 @@ static int ipod_get_codec_tag(AVFormatContext *s, MOVTrack *track)
            tag == MKTAG('t', 'e', 'x', 't'))))
         tag = ff_codec_get_tag(codec_ipod_tags, track->enc->codec_id);
 
-    if (!av_match_ext(s->filename, "m4a") &&
-        !av_match_ext(s->filename, "m4b") &&
-        !av_match_ext(s->filename, "m4v"))
+    if (!av_match_ext(av_format_get_filename(s), "m4a") &&
+        !av_match_ext(av_format_get_filename(s), "m4b") &&
+        !av_match_ext(av_format_get_filename(s), "m4v"))
         av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a, .m4v nor  .m4b "
                "Quicktime/Ipod might not play the file\n");
 
@@ -5413,10 +5413,10 @@ static int shift_data(AVFormatContext *s)
      * writing, so we re-open the same output, but for reading. It also avoids
      * a read/seek/write/seek back and forth. */
     avio_flush(s->pb);
-    ret = avio_open(&read_pb, s->filename, AVIO_FLAG_READ);
+    ret = avio_open(&read_pb, av_format_get_filename(s), AVIO_FLAG_READ);
     if (ret < 0) {
         av_log(s, AV_LOG_ERROR, "Unable to re-open %s output file for "
-               "the second pass (faststart)\n", s->filename);
+               "the second pass (faststart)\n", av_format_get_filename(s));
         goto end;
     }
 
diff --git a/libavformat/mpeg.c b/libavformat/mpeg.c
index 223b690..e86bc59 100644
--- a/libavformat/mpeg.c
+++ b/libavformat/mpeg.c
@@ -703,7 +703,7 @@ static int vobsub_read_header(AVFormatContext *s)
 
     if (!vobsub->sub_name) {
         char *ext;
-        vobsub->sub_name = av_strdup(s->filename);
+        vobsub->sub_name = av_strdup(av_format_get_filename(s));
         if (!vobsub->sub_name) {
             ret = AVERROR(ENOMEM);
             goto end;
@@ -718,7 +718,7 @@ static int vobsub_read_header(AVFormatContext *s)
             goto end;
         }
         memcpy(ext, !strncmp(ext, "IDX", 3) ? "SUB" : "sub", 3);
-        av_log(s, AV_LOG_VERBOSE, "IDX/SUB: %s -> %s\n", s->filename, vobsub->sub_name);
+        av_log(s, AV_LOG_VERBOSE, "IDX/SUB: %s -> %s\n", av_format_get_filename(s), vobsub->sub_name);
     }
 
     if (!(iformat = av_find_input_format("mpeg"))) {
diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c
index 45bab1c..6ee3877 100644
--- a/libavformat/mpegtsenc.c
+++ b/libavformat/mpegtsenc.c
@@ -813,7 +813,7 @@ static int mpegts_write_header(AVFormatContext *s)
            ts->sdt_packet_period, ts->pat_packet_period);
 
     if (ts->m2ts_mode == -1) {
-        if (av_match_ext(s->filename, "m2ts")) {
+        if (av_match_ext(av_format_get_filename(s), "m2ts")) {
             ts->m2ts_mode = 1;
         } else {
             ts->m2ts_mode = 0;
diff --git a/libavformat/mux.c b/libavformat/mux.c
index 37b5976..b4eb449 100644
--- a/libavformat/mux.c
+++ b/libavformat/mux.c
@@ -187,7 +187,7 @@ int avformat_alloc_output_context2(AVFormatContext **avctx, AVOutputFormat *ofor
         s->priv_data = NULL;
 
     if (filename)
-        av_strlcpy(s->filename, filename, sizeof(s->filename));
+        av_format_set_filename(s, filename);
     *avctx = s;
     return 0;
 nomem:
diff --git a/libavformat/nsvdec.c b/libavformat/nsvdec.c
index de55396..b7fd6a1 100644
--- a/libavformat/nsvdec.c
+++ b/libavformat/nsvdec.c
@@ -497,7 +497,7 @@ static int nsv_read_header(AVFormatContext *s)
     int i, err;
 
     av_log(s, AV_LOG_TRACE, "%s()\n", __FUNCTION__);
-    av_log(s, AV_LOG_TRACE, "filename '%s'\n", s->filename);
+    av_log(s, AV_LOG_TRACE, "filename '%s'\n", av_format_get_filename(s));
 
     nsv->state = NSV_UNSYNC;
     nsv->ahead[0].data = nsv->ahead[1].data = NULL;
diff --git a/libavformat/rtsp.c b/libavformat/rtsp.c
index 9aa66d2..b1c5849 100644
--- a/libavformat/rtsp.c
+++ b/libavformat/rtsp.c
@@ -1676,7 +1676,7 @@ int ff_rtsp_connect(AVFormatContext *s)
 redirect:
     /* extract hostname and port */
     av_url_split(proto, sizeof(proto), auth, sizeof(auth),
-                 host, sizeof(host), &port, path, sizeof(path), s->filename);
+                 host, sizeof(host), &port, path, sizeof(path), av_format_get_filename(s));
 
     if (!strcmp(proto, "rtsps")) {
         lower_rtsp_proto         = "tls";
@@ -1887,11 +1887,11 @@ redirect:
     ff_rtsp_close_streams(s);
     ff_rtsp_close_connections(s);
     if (reply->status_code >=300 && reply->status_code < 400 && s->iformat) {
-        av_strlcpy(s->filename, reply->location, sizeof(s->filename));
+        av_format_set_filename(s, reply->location);
         rt->session_id[0] = '\0';
         av_log(s, AV_LOG_INFO, "Status %d: Redirecting to %s\n",
                reply->status_code,
-               s->filename);
+               av_format_get_filename(s));
         goto redirect;
     }
     ff_network_close();
@@ -2370,7 +2370,7 @@ static int rtp_read_header(AVFormatContext *s)
     if (!ff_network_init())
         return AVERROR(EIO);
 
-    ret = ffurl_open(&in, s->filename, AVIO_FLAG_READ,
+    ret = ffurl_open(&in, av_format_get_filename(s), AVIO_FLAG_READ,
                      &s->interrupt_callback, NULL);
     if (ret)
         goto fail;
@@ -2415,7 +2415,7 @@ static int rtp_read_header(AVFormatContext *s)
     }
 
     av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port,
-                 NULL, 0, s->filename);
+                 NULL, 0, av_format_get_filename(s));
 
     snprintf(sdp, sizeof(sdp),
              "v=0\r\nc=IN IP%d %s\r\nm=%s %d RTP/AVP %d\r\n",
diff --git a/libavformat/rtspdec.c b/libavformat/rtspdec.c
index 3c0010e..15fa407 100644
--- a/libavformat/rtspdec.c
+++ b/libavformat/rtspdec.c
@@ -644,7 +644,7 @@ static int rtsp_listen(AVFormatContext *s)
 
     /* extract hostname and port */
     av_url_split(proto, sizeof(proto), auth, sizeof(auth), host, sizeof(host),
-                 &port, path, sizeof(path), s->filename);
+                 &port, path, sizeof(path), av_format_get_filename(s));
 
     /* ff_url_join. No authorization by now (NULL) */
     ff_url_join(rt->control_uri, sizeof(rt->control_uri), proto, NULL, host,
@@ -804,7 +804,7 @@ static int resetup_tcp(AVFormatContext *s)
     int port;
 
     av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port, NULL, 0,
-                 s->filename);
+                 av_format_get_filename(s));
     ff_rtsp_undo_setup(s, 0);
     return ff_rtsp_make_setup_request(s, host, port, RTSP_LOWER_TRANSPORT_TCP,
                                       rt->real_challenge);
diff --git a/libavformat/sapdec.c b/libavformat/sapdec.c
index 2dd8524..001ed92 100644
--- a/libavformat/sapdec.c
+++ b/libavformat/sapdec.c
@@ -74,7 +74,7 @@ static int sap_read_header(AVFormatContext *s)
         return AVERROR(EIO);
 
     av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port,
-                 path, sizeof(path), s->filename);
+                 path, sizeof(path), av_format_get_filename(s));
     if (port < 0)
         port = 9875;
 
diff --git a/libavformat/sapenc.c b/libavformat/sapenc.c
index 07fbf48..5eefbca 100644
--- a/libavformat/sapenc.c
+++ b/libavformat/sapenc.c
@@ -84,7 +84,7 @@ static int sap_write_header(AVFormatContext *s)
 
     /* extract hostname and port */
     av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &base_port,
-                 path, sizeof(path), s->filename);
+                 path, sizeof(path), av_format_get_filename(s));
     if (base_port < 0)
         base_port = 5004;
 
@@ -159,7 +159,7 @@ static int sap_write_header(AVFormatContext *s)
             goto fail;
         s->streams[i]->priv_data = contexts[i];
         s->streams[i]->time_base = contexts[i]->streams[0]->time_base;
-        av_strlcpy(contexts[i]->filename, url, sizeof(contexts[i]->filename));
+        av_format_set_filename(contexts[i], url);
     }
 
     if (s->nb_streams > 0 && title)
diff --git a/libavformat/sdp.c b/libavformat/sdp.c
index 45974b3..b32e449 100644
--- a/libavformat/sdp.c
+++ b/libavformat/sdp.c
@@ -762,7 +762,7 @@ int av_sdp_create(AVFormatContext *ac[], int n_files, char *buf, int size)
     port = 0;
     ttl = 0;
     if (n_files == 1) {
-        port = sdp_get_address(dst, sizeof(dst), &ttl, ac[0]->filename);
+        port = sdp_get_address(dst, sizeof(dst), &ttl, av_format_get_filename(ac[0]));
         is_multicast = resolve_destination(dst, sizeof(dst), dst_type,
                                            sizeof(dst_type));
         if (!is_multicast)
@@ -782,7 +782,7 @@ int av_sdp_create(AVFormatContext *ac[], int n_files, char *buf, int size)
     dst[0] = 0;
     for (i = 0; i < n_files; i++) {
         if (n_files != 1) {
-            port = sdp_get_address(dst, sizeof(dst), &ttl, ac[i]->filename);
+            port = sdp_get_address(dst, sizeof(dst), &ttl, av_format_get_filename(ac[i]));
             is_multicast = resolve_destination(dst, sizeof(dst), dst_type,
                                                sizeof(dst_type));
             if (!is_multicast)
diff --git a/libavformat/segment.c b/libavformat/segment.c
index 36417f2..b09696b 100644
--- a/libavformat/segment.c
+++ b/libavformat/segment.c
@@ -183,6 +183,7 @@ static int set_segment_filename(AVFormatContext *s)
     SegmentContext *seg = s->priv_data;
     AVFormatContext *oc = seg->avf;
     size_t size;
+    char filename_buf[1024];
 
     if (seg->segment_idx_wrap)
         seg->segment_idx %= seg->segment_idx_wrap;
@@ -191,18 +192,21 @@ static int set_segment_filename(AVFormatContext *s)
         struct tm *tm, tmpbuf;
         time(&now0);
         tm = localtime_r(&now0, &tmpbuf);
-        if (!strftime(oc->filename, sizeof(oc->filename), s->filename, tm)) {
+        if (!strftime(filename_buf, sizeof(filename_buf), av_format_get_filename(s), tm)) {
             av_log(oc, AV_LOG_ERROR, "Could not get segment filename with strftime\n");
             return AVERROR(EINVAL);
         }
-    } else if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
-                                     s->filename, seg->segment_idx) < 0) {
-        av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s'\n", s->filename);
+        av_format_set_filename(oc, filename_buf);
+    } else if (av_get_frame_filename(filename_buf, sizeof(filename_buf),
+                                     av_format_get_filename(s), seg->segment_idx) < 0) {
+        av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s'\n", av_format_get_filename(s));
         return AVERROR(EINVAL);
+    } else {
+        av_format_set_filename(oc, filename_buf);
     }
 
     /* copy modified name in list entry */
-    size = strlen(av_basename(oc->filename)) + 1;
+    size = strlen(av_basename(av_format_get_filename(oc))) + 1;
     if (seg->entry_prefix)
         size += strlen(seg->entry_prefix);
 
@@ -211,7 +215,7 @@ static int set_segment_filename(AVFormatContext *s)
         return AVERROR(ENOMEM);
     snprintf(seg->cur_entry.filename, size, "%s%s",
              seg->entry_prefix ? seg->entry_prefix : "",
-             av_basename(oc->filename));
+             av_basename(av_format_get_filename(oc)));
 
     return 0;
 }
@@ -237,9 +241,9 @@ static int segment_start(AVFormatContext *s, int write_header)
     if ((err = set_segment_filename(s)) < 0)
         return err;
 
-    if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
+    if ((err = avio_open2(&oc->pb, av_format_get_filename(oc), AVIO_FLAG_WRITE,
                           &s->interrupt_callback, NULL)) < 0) {
-        av_log(s, AV_LOG_ERROR, "Failed to open segment '%s'\n", oc->filename);
+        av_log(s, AV_LOG_ERROR, "Failed to open segment '%s'\n", av_format_get_filename(oc));
         return err;
     }
     if (!seg->individual_header_trailer)
@@ -340,7 +344,7 @@ static int segment_end(AVFormatContext *s, int write_trailer, int is_last)
 
     if (ret < 0)
         av_log(s, AV_LOG_ERROR, "Failure occurred when ending segment '%s'\n",
-               oc->filename);
+               av_format_get_filename(oc));
 
     if (seg->list) {
         if (seg->list_size || seg->list_type == LIST_TYPE_M3U8) {
@@ -383,7 +387,7 @@ static int segment_end(AVFormatContext *s, int write_trailer, int is_last)
     }
 
     av_log(s, AV_LOG_VERBOSE, "segment:'%s' count:%d ended\n",
-           seg->avf->filename, seg->segment_count);
+           av_format_get_filename(seg->avf), seg->segment_count);
     seg->segment_count++;
 
 end:
@@ -656,7 +660,7 @@ static int seg_write_header(AVFormatContext *s)
             if ((ret = segment_list_open(s)) < 0)
                 goto fail;
         } else {
-            const char *proto = avio_find_protocol_name(s->filename);
+            const char *proto = avio_find_protocol_name(av_format_get_filename(s));
             seg->use_rename = proto && !strcmp(proto, "file");
         }
     }
@@ -669,7 +673,7 @@ static int seg_write_header(AVFormatContext *s)
            seg->reference_stream_index,
            av_get_media_type_string(s->streams[seg->reference_stream_index]->codec->codec_type));
 
-    seg->oformat = av_guess_format(seg->format, s->filename, NULL);
+    seg->oformat = av_guess_format(seg->format, av_format_get_filename(s), NULL);
 
     if (!seg->oformat) {
         ret = AVERROR_MUXER_NOT_FOUND;
@@ -690,9 +694,9 @@ static int seg_write_header(AVFormatContext *s)
         goto fail;
 
     if (seg->write_header_trailer) {
-        if ((ret = avio_open2(&oc->pb, seg->header_filename ? seg->header_filename : oc->filename, AVIO_FLAG_WRITE,
+        if ((ret = avio_open2(&oc->pb, seg->header_filename ? seg->header_filename : av_format_get_filename(oc), AVIO_FLAG_WRITE,
                               &s->interrupt_callback, NULL)) < 0) {
-            av_log(s, AV_LOG_ERROR, "Failed to open segment '%s'\n", oc->filename);
+            av_log(s, AV_LOG_ERROR, "Failed to open segment '%s'\n", av_format_get_filename(oc));
             goto fail;
         }
         if (!seg->individual_header_trailer)
@@ -734,7 +738,7 @@ static int seg_write_header(AVFormatContext *s)
         } else {
             close_null_ctxp(&oc->pb);
         }
-        if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
+        if ((ret = avio_open2(&oc->pb, av_format_get_filename(oc), AVIO_FLAG_WRITE,
                               &s->interrupt_callback, NULL)) < 0)
             goto fail;
         if (!seg->individual_header_trailer)
@@ -823,7 +827,7 @@ static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
 
     if (seg->segment_frame_count == 0) {
         av_log(s, AV_LOG_VERBOSE, "segment:'%s' starts with packet stream:%d pts:%s pts_time:%s frame:%d\n",
-               seg->avf->filename, pkt->stream_index,
+               av_format_get_filename(seg->avf), pkt->stream_index,
                av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base), seg->frame_count);
     }
 
diff --git a/libavformat/smoothstreamingenc.c b/libavformat/smoothstreamingenc.c
index 07173a9..1d738f2 100644
--- a/libavformat/smoothstreamingenc.c
+++ b/libavformat/smoothstreamingenc.c
@@ -218,8 +218,8 @@ static int write_manifest(AVFormatContext *s, int final)
     int ret, i, video_chunks = 0, audio_chunks = 0, video_streams = 0, audio_streams = 0;
     int64_t duration = 0;
 
-    snprintf(filename, sizeof(filename), "%s/Manifest", s->filename);
-    snprintf(temp_filename, sizeof(temp_filename), "%s/Manifest.tmp", s->filename);
+    snprintf(filename, sizeof(filename), "%s/Manifest", av_format_get_filename(s));
+    snprintf(temp_filename, sizeof(temp_filename), "%s/Manifest.tmp", av_format_get_filename(s));
     ret = avio_open2(&out, temp_filename, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL);
     if (ret < 0) {
         av_log(s, AV_LOG_ERROR, "Unable to open %s for writing\n", temp_filename);
@@ -292,7 +292,7 @@ static int ism_write_header(AVFormatContext *s)
     int ret = 0, i;
     AVOutputFormat *oformat;
 
-    if (mkdir(s->filename, 0777) == -1 && errno != EEXIST) {
+    if (mkdir(av_format_get_filename(s), 0777) == -1 && errno != EEXIST) {
         ret = AVERROR(errno);
         av_log(s, AV_LOG_ERROR, "mkdir failed\n");
         goto fail;
@@ -321,7 +321,7 @@ static int ism_write_header(AVFormatContext *s)
             ret = AVERROR(EINVAL);
             goto fail;
         }
-        snprintf(os->dirname, sizeof(os->dirname), "%s/QualityLevels(%d)", s->filename, s->streams[i]->codec->bit_rate);
+        snprintf(os->dirname, sizeof(os->dirname), "%s/QualityLevels(%d)", av_format_get_filename(s), s->streams[i]->codec->bit_rate);
         if (mkdir(os->dirname, 0777) == -1 && errno != EEXIST) {
             ret = AVERROR(errno);
             av_log(s, AV_LOG_ERROR, "mkdir failed\n");
@@ -606,9 +606,9 @@ static int ism_write_trailer(AVFormatContext *s)
 
     if (c->remove_at_exit) {
         char filename[1024];
-        snprintf(filename, sizeof(filename), "%s/Manifest", s->filename);
+        snprintf(filename, sizeof(filename), "%s/Manifest", av_format_get_filename(s));
         unlink(filename);
-        rmdir(s->filename);
+        rmdir(av_format_get_filename(s));
     }
 
     ism_free(s);
diff --git a/libavformat/tee.c b/libavformat/tee.c
index c619eae..b6fe4da 100644
--- a/libavformat/tee.c
+++ b/libavformat/tee.c
@@ -315,7 +315,7 @@ 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);
+           av_format_get_filename(slave->avf), slave->avf->oformat->name);
     for (i = 0; i < slave->avf->nb_streams; i++) {
         AVStream *st = slave->avf->streams[i];
         AVBitStreamFilterContext *bsf = slave->bsfs[i];
@@ -339,7 +339,7 @@ static int tee_write_header(AVFormatContext *avf)
 {
     TeeContext *tee = avf->priv_data;
     unsigned nb_slaves = 0, i;
-    const char *filename = avf->filename;
+    const char *filename = av_format_get_filename(avf);
     char *slaves[MAX_SLAVES];
     int ret;
 
@@ -412,7 +412,7 @@ static int filter_packet(void *log_ctx, AVPacket *pkt,
         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,
+                bsf_ctx->filter->name, pkt->stream_index, av_format_get_filename(fmt_ctx),
                 avcodec_get_name(enc_ctx->codec_id));
         }
         *pkt = new_pkt;
diff --git a/libavformat/utils.c b/libavformat/utils.c
index 24eacf3..87d0135 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -114,6 +114,19 @@ MAKE_ACCESSORS(AVFormatContext, format, void *, opaque)
 MAKE_ACCESSORS(AVFormatContext, format, av_format_control_message, control_message_cb)
 MAKE_ACCESSORS(AVFormatContext, format, AVOpenCallback, open_cb)
 
+void av_format_set_filename(AVFormatContext *s, const char *filename)
+{
+    if (filename)
+        av_strlcpy(s->filename, filename, sizeof(s->filename));
+    else
+        s->filename[0] = 0;
+}
+
+const char *av_format_get_filename(AVFormatContext *s)
+{
+    return s->filename;
+}
+
 int64_t av_stream_get_end_pts(const AVStream *st)
 {
     if (st->priv_pts) {
@@ -449,7 +462,7 @@ int avformat_open_input(AVFormatContext **ps, const char *filename,
     }
 
     s->duration = s->start_time = AV_NOPTS_VALUE;
-    av_strlcpy(s->filename, filename ? filename : "", sizeof(s->filename));
+    av_format_set_filename(s, filename);
 
     /* Allocate private data. */
     if (s->iformat->priv_data_size > 0) {
@@ -1784,7 +1797,7 @@ void ff_configure_buffers_for_index(AVFormatContext *s, int64_t time_tolerance)
     int64_t pos_delta = 0;
     int64_t skip = 0;
     //We could use URLProtocol flags here but as many user applications do not use URLProtocols this would be unreliable
-    const char *proto = avio_find_protocol_name(s->filename);
+    const char *proto = avio_find_protocol_name(av_format_get_filename(s));
 
     if (!proto) {
         av_log(s, AV_LOG_INFO,
diff --git a/libavformat/webm_chunk.c b/libavformat/webm_chunk.c
index 3dfef4b..ce093f8 100644
--- a/libavformat/webm_chunk.c
+++ b/libavformat/webm_chunk.c
@@ -96,8 +96,8 @@ static int get_chunk_filename(AVFormatContext *s, int is_header, char *filename)
         av_strlcpy(filename, wc->header_filename, strlen(wc->header_filename) + 1);
     } else {
         if (av_get_frame_filename(filename, MAX_FILENAME_SIZE,
-                                  s->filename, wc->chunk_index - 1) < 0) {
-            av_log(oc, AV_LOG_ERROR, "Invalid chunk filename template '%s'\n", s->filename);
+                                  av_format_get_filename(s), wc->chunk_index - 1) < 0) {
+            av_log(oc, AV_LOG_ERROR, "Invalid chunk filename template '%s'\n", av_format_get_filename(s));
             return AVERROR(EINVAL);
         }
     }
@@ -109,12 +109,13 @@ static int webm_chunk_write_header(AVFormatContext *s)
     WebMChunkContext *wc = s->priv_data;
     AVFormatContext *oc = NULL;
     int ret;
+    char filename_buf[MAX_FILENAME_SIZE];
 
     // DASH Streams can only have either one track per file.
     if (s->nb_streams != 1) { return AVERROR_INVALIDDATA; }
 
     wc->chunk_index = wc->chunk_start_index;
-    wc->oformat = av_guess_format("webm", s->filename, "video/webm");
+    wc->oformat = av_guess_format("webm", av_format_get_filename(s), "video/webm");
     if (!wc->oformat)
         return AVERROR_MUXER_NOT_FOUND;
 
@@ -122,10 +123,11 @@ static int webm_chunk_write_header(AVFormatContext *s)
     if (ret < 0)
         return ret;
     oc = wc->avf;
-    ret = get_chunk_filename(s, 1, oc->filename);
+    ret = get_chunk_filename(s, 1, filename_buf);
     if (ret < 0)
         return ret;
-    ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
+    av_format_set_filename(oc, filename_buf);
+    ret = avio_open2(&oc->pb, av_format_get_filename(oc), AVIO_FLAG_WRITE,
                      &s->interrupt_callback, NULL);
     if (ret < 0)
         return ret;
-- 
2.0.0



More information about the ffmpeg-devel mailing list