From patchwork Wed Jan 1 17:17:57 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alexandre Heitor Schmidt X-Patchwork-Id: 17118 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id DA8F7448E72 for ; Wed, 1 Jan 2020 19:18:06 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B328468AAAE; Wed, 1 Jan 2020 19:18:06 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ed1-f50.google.com (mail-ed1-f50.google.com [209.85.208.50]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7C59968AA1F for ; Wed, 1 Jan 2020 19:18:00 +0200 (EET) Received: by mail-ed1-f50.google.com with SMTP id r21so37354555edq.0 for ; Wed, 01 Jan 2020 09:18:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language; bh=aD32NM7WefbaT3Ut7D1P4bjn64N+hLSYjXNSM8k7UoM=; b=ALSFfr9nX89WzgXi7tX0q18fXk34bVbLQhanG93bcE6YrpnEio+4SkGXXl8v3eQBiu KX4r4xfqRCaXzvaXlhK8EOCDzx4mNnQ2hwJukNmC8Uc0GzQKjcmAdAGY5OXvh3RJ/Ji6 JCg9sOAAW6mf5rjJ176k11O5ocH7K85664NemfFVVVMwf+8DlMeTfMUmQ2KMxz0L9fG3 BhVxjYrADmWP9Qalq390vXeo/YxROs96O7h2oUnA0xUchJHAOqXaOXuuG8fU1dmTlaE8 u3rnTc8MeMykjkjOdAkKEUZYATNA6X3MWLO4GYPXTxSWEDHPEJP8oZUDpUuis0u9PNf2 j4+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=aD32NM7WefbaT3Ut7D1P4bjn64N+hLSYjXNSM8k7UoM=; b=UWQgo1CISVai8Jo4AP76rhwWQvvgDBBtFzt66Tj8tPUymmVaQ3nTQCORw6aMpWrRBQ jkVSUDoY6E0/eNvb1QlAN7gga6564+Gp+muFU/BwVnlz20X3QioNe/ikMseAx9x98A88 UTw6d0dqMhJzqcubgpkdl4XDZ8IF+Ra/AYvqshJpL3ZqmcyboqFuK9U6L+dvMuo7Dia6 3UkLLaS3RlvfQTX8N6yWEFjq2D30WsQB1Jlb+qzKImH7j0Ty1t22WM4cCNt1oIhGS4s3 4qPMM0239RHYINbmYk1WDajDxb04MHOKHKEq34T47xuHxBMKTkgG3+XneM0ZieKEYIg8 Sg/g== X-Gm-Message-State: APjAAAWrpVupjG/cObzExoTc7AsdqAQNH2GdVAnHbsC74ZjpB3AJ6Bvf TGufzMTIBEcYhmrdNWZKGU+rjSLuxYo= X-Google-Smtp-Source: APXvYqyahystcD/VjMZ4tYDy9LuVMnSohIZATvL4Aaa2yM0BmmypGLmkPI9BO2A2OiqlLegB/78n1w== X-Received: by 2002:aa7:d6d1:: with SMTP id x17mr46980281edr.57.1577899079552; Wed, 01 Jan 2020 09:17:59 -0800 (PST) Received: from [192.168.1.9] (a94-132-34-60.cpe.netcabo.pt. [94.132.34.60]) by smtp.gmail.com with ESMTPSA id e22sm6466200edq.75.2020.01.01.09.17.58 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 01 Jan 2020 09:17:58 -0800 (PST) To: FFmpeg development discussions and patches References: <20191216222608.19270-1-alexandre.schmidt@gmail.com> <16dd576f-d23c-f2b1-f475-9cbc5cb3be36@gmail.com> <20191219164651.GT3089@michaelspb> <01a8faa2-50c2-0fb1-0e13-147161a3c837@gyani.pro> <20191222235442.GB3089@michaelspb> <8732038c-7a32-6647-fedb-72b66d2852fd@gmail.com> <1e0814ca-78da-a053-8f5f-0632b4e6f333@gmail.com> From: Alexandre Heitor Schmidt Message-ID: Date: Wed, 1 Jan 2020 17:17:57 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.2.2 MIME-Version: 1.0 In-Reply-To: Content-Language: en-US Subject: [FFmpeg-devel] [PATCH] avformat/image2: Upon request, make available extra metadata, fields related to input path to be used by filters. X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" The patch follows attached, to avoid formatting issues. The commit message is as follows: avformat/image2: Upon request, make available extra metadata  fields related to input path to be used by filters. libavformat/img2.h: New field export_path_metadata to VideoDemuxData to only allow the use of the extra metadata upon explicit user request, for security reasons. 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. The metadata fields will only be available when explicitly enabled with image2 option -export_path_metadata 1. doc/demuxers.texi: Documented the new metadata fields available for image2 and how to use them. doc/filters.texi: Added an example on how to use the new metadata fields with drawtext filter, in order to plot the input file path to each output frame. Usage example: ffmpeg -f image2 -export_path_metadata 1 -pattern_type glob  -framerate 18 -i '/path/to/input/files/*.jpg'  -filter_complex drawtext="fontsize=40:fontcolor=white:  fontfile=FreeSans.ttf:borderw=2:bordercolor=black:  text='%{metadata\:lavf.image2dec.source_basename\:NA}':x=5:y=50"  output.avi Fixes #2874. Signed-off-by: Alexandre Heitor Schmidt From 6294e556c63b99f9b58494fb8013e9c616444ab0 Mon Sep 17 00:00:00 2001 From: Alexandre Heitor Schmidt Date: Wed, 1 Jan 2020 16:57:02 +0000 Subject: [PATCH] avformat/image2: Upon request, make available extra metadata fields related to input path to be used by filters. libavformat/img2.h: New field export_path_metadata to VideoDemuxData to only allow the use of the extra metadata upon explicit user request, for security reasons. 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. The metadata fields will only be available when explicitly enabled with image2 option -export_path_metadata 1. doc/demuxers.texi: Documented the new metadata fields available for image2 and how to use them. doc/filters.texi: Added an example on how to use the new metadata fields with drawtext filter, in order to plot the input file path to each output frame. Usage example: ffmpeg -f image2 -export_path_metadata 1 -pattern_type glob -framerate 18 -i '/path/to/input/files/*.jpg' -filter_complex drawtext="fontsize=40:fontcolor=white: fontfile=FreeSans.ttf:borderw=2:bordercolor=black: text='%{metadata\:lavf.image2dec.source_basename\:NA}':x=5:y=50" output.avi Fixes #2874. Signed-off-by: Alexandre Heitor Schmidt --- doc/demuxers.texi | 11 +++++++++++ doc/filters.texi | 9 +++++++++ libavformat/img2.h | 1 + libavformat/img2dec.c | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+) diff --git a/doc/demuxers.texi b/doc/demuxers.texi index 0d13bdd1b3..2de14b68d3 100644 --- a/doc/demuxers.texi +++ b/doc/demuxers.texi @@ -456,6 +456,17 @@ nanosecond precision. @item video_size Set the video size of the images to read. If not specified the video size is guessed from the first image file in the sequence. +@item export_path_metadata +If set to 1, will add two extra fields to the metadata found in input, making them +also available for other filters (see @var{drawtext} filter for examples). Default +value is 0. The extra fields are described below: +@table @option +@item lavf.image2dec.source_path +Corresponds to the full path to the input file being read. +@item lavf.image2dec.source_basename +Corresponds to the name of the file being read. +@end table + @end table @subsection Examples diff --git a/doc/filters.texi b/doc/filters.texi index ba00989987..85b55be1bb 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -9888,6 +9888,15 @@ 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". Note that image2 demuxer +must have option @option{-export_path_metadata 1} for the special metadata fields +to be available for filters. +@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/img2.h b/libavformat/img2.h index 0e5b374a6b..5fd8ff77fc 100644 --- a/libavformat/img2.h +++ b/libavformat/img2.h @@ -61,6 +61,7 @@ typedef struct VideoDemuxData { int start_number_range; int frame_size; int ts_from_file; + int export_path_metadata; /**< enabled when set to 1. */ } VideoDemuxData; typedef struct IdStrMap { diff --git a/libavformat/img2dec.c b/libavformat/img2dec.c index f8b4a655a5..4eb554debe 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,17 @@ int ff_img_read_packet(AVFormatContext *s1, AVPacket *pkt) if (s->is_pipe) pkt->pos = avio_tell(f[0]); + /* + * export_path_metadata must be explicitly enabled via + * command line options for path metadata to be exported + * as packet side_data. + */ + if (!s->is_pipe && s->export_path_metadata == 1) { + 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]) { @@ -570,6 +607,7 @@ static int img_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp { "pixel_format", "set video pixel format", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC }, \ { "video_size", "set video size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC }, \ { "loop", "force loop over input file sequence", OFFSET(loop), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, DEC }, \ + { "export_path_metadata", "enable metadata containing input path information", OFFSET(export_path_metadata), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, DEC }, \ { NULL }, #if CONFIG_IMAGE2_DEMUXER -- 2.17.1