>From 95ebffed3784ba12a32966128136e8deae0d8386 Mon Sep 17 00:00:00 2001
From: Alexandre Heitor Schmidt <alexandre.schmidt@gmail.com>
Date: Sat, 21 Dec 2019 16:19:31 +0000
Subject: [PATCH] avformat/image2: Metadata identifying the source path of 
 input filename and documentation for its usage.

libavformat/img2dec.c: Modify image2 demuxer to make available
two special metadata entries called lavf.image2dec.source_path
and lavf.image2dec.source_basename, which represents, respectively,
the complete path to the source image for the current frame and
the basename i.e. the file name related to the current frame.
These can then be used by filters like drawtext and others.

doc/demuxers.texti: Documented the two special metadata tags.

doc/filters.texti: Added an usage example for these tags.

Fixes #2874.

Signed-off-by: Alexandre Heitor Schmidt <alexandre.schmidt@gmail.com>
---
 doc/demuxers.texi     | 11 +++++++++++
 doc/filters.texi      |  7 +++++++
 libavformat/img2dec.c | 32 ++++++++++++++++++++++++++++++++
 3 files changed, 50 insertions(+)

diff --git a/doc/demuxers.texi b/doc/demuxers.texi
index 0d13bdd1b3..96cff22267 100644
--- a/doc/demuxers.texi
+++ b/doc/demuxers.texi
@@ -362,6 +362,17 @@ determine the format of the images contained in the files.
 The size, the pixel format, and the format of each image must be the
 same for all the files in the sequence.
 
+Along with the metadata found within each file, two special metadata
+fields are made available for other filters (see @var{drawtext} filter
+for examples):
+
+@table @option
+@item lavf.image2dec.source_path
+Corresponds to the full path to the input image being read.
+@item lavf.image2dec.source_basename
+Corresponds to the name of the file being read.
+@end table
+
 This demuxer accepts the following options:
 @table @option
 @item framerate
diff --git a/doc/filters.texi b/doc/filters.texi
index 8c5d3a5760..6611d5977c 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -9872,6 +9872,13 @@ drawtext=fontfile=FreeSans.ttf:text=DOG:fontsize=24:x=10:y=20+24-max_glyph_a,
 drawtext=fontfile=FreeSans.ttf:text=cow:fontsize=24:x=80:y=20+24-max_glyph_a
 @end example
 
+@item
+Plot special @var{lavf.image2dec.source_basename} metadata onto each frame if
+such metadata exists. Otherwise, plot the string "NA".
+@example
+drawtext="fontsize=20:fontcolor=white:fontfile=FreeSans.ttf:text='%@{metadata\:lavf.image2dec.source_basename\:NA@}':x=10:y=10"
+@end example
+
 @end itemize
 
 For more information about libfreetype, check:
diff --git a/libavformat/img2dec.c b/libavformat/img2dec.c
index f8b4a655a5..86f9f1f657 100644
--- a/libavformat/img2dec.c
+++ b/libavformat/img2dec.c
@@ -374,6 +374,32 @@ int ff_img_read_header(AVFormatContext *s1)
     return 0;
 }
 
+/**
+ * Add this frame's source path and basename to packet's sidedata
+ * as a dictionary, so it can be used by filters like 'drawtext'.
+ */
+static int add_filename_as_pkt_side_data(char *filename, AVPacket *pkt) {
+    uint8_t* metadata;
+    int metadata_len;
+    AVDictionary *d = NULL;
+    char *packed_metadata = NULL;
+
+    av_dict_set(&d, "lavf.image2dec.source_path", filename, 0);
+    av_dict_set(&d, "lavf.image2dec.source_basename", av_basename(filename), 0);
+
+    packed_metadata = av_packet_pack_dictionary(d, &metadata_len);
+    if (!packed_metadata)
+        return AVERROR(ENOMEM);
+    if (!(metadata = av_packet_new_side_data(pkt, AV_PKT_DATA_STRINGS_METADATA, metadata_len))) {
+        av_freep(&packed_metadata);
+        return AVERROR(ENOMEM);
+    }
+    memcpy(metadata, packed_metadata, metadata_len);
+    av_freep(&packed_metadata);
+
+    return 0;
+}
+
 int ff_img_read_packet(AVFormatContext *s1, AVPacket *pkt)
 {
     VideoDemuxData *s = s1->priv_data;
@@ -486,6 +512,12 @@ int ff_img_read_packet(AVFormatContext *s1, AVPacket *pkt)
     if (s->is_pipe)
         pkt->pos = avio_tell(f[0]);
 
+    if (!s->is_pipe) {
+        res = add_filename_as_pkt_side_data(filename, pkt);
+        if (res < 0)
+            goto fail;
+    }
+
     pkt->size = 0;
     for (i = 0; i < 3; i++) {
         if (f[i]) {
-- 
2.17.1

