From patchwork Mon Dec 16 22:26:08 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Heitor Schmidt X-Patchwork-Id: 16837 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 7CBB5449AF2 for ; Tue, 17 Dec 2019 00:32:09 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5E53D68ABC3; Tue, 17 Dec 2019 00:32:09 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f47.google.com (mail-wr1-f47.google.com [209.85.221.47]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9C8BC68A7AD for ; Tue, 17 Dec 2019 00:32:03 +0200 (EET) Received: by mail-wr1-f47.google.com with SMTP id w15so9220002wru.4 for ; Mon, 16 Dec 2019 14:32:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=yE0AK8aI4HMitz4ua3rC1H4l+YdOhQTIOwygF5Jt2b8=; b=Z/MDyBY5arVdF6X4LPs7vYnEKzNIvjJ79AtC+/QVCpnMVlmZyqnAn1oSpfQqRqy3H6 OL4aXL1FDE4WPtqhLz8bDSVVJVUD2YjYXvwykcp+pgsVtpVzpxlLtJXXkRhLHHWMel/L Cm1lOUFXI02cFuE9ZNmZghl5/X0GeOJ+Nm2CZcIJvU3dq6Y5MB/TeMMPLMR/v8CfXKtI lWhQ8beLWjKTE/AiOAk3ktDb4+It7yw1j8QBkQSU+qz5m8xHfmiFhehkqQyVhlsc8qEH geLWVGhn5/oldxKLyobyxWPws1/hZOKwlj5x0Uus2cydwrKhGnlIakBBI870deJYwYum N8lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=yE0AK8aI4HMitz4ua3rC1H4l+YdOhQTIOwygF5Jt2b8=; b=Xa6ikDUDKaz/nEjSSVGhrC4W7vRGItevxdSVnixhQ1rRGa+1NXdaC+Z5OaY0siIGoY kqaVc2r1mKcZVzLCPZGqJ3PKE7aWF5dmp66j7GEuPgvrBEd26xToNVpVP/8A2lTplze0 byBsF5E392ucUUbrkMaPM2wvb/K28encAwuxHl+jnZrZp588ZK6ECmXJYnhhasuaOXQx 32fkAOKUDQwXoQgBdBLn1hpNODQx8oSeqIB+ELZZCMeT8lQeBJgiznFHIz3sOf7J57lb vibgvDwxAfcQ2gg2b+f9MZSagdqEvV5E4XC5u86zkp+1UEhn5QbYlXGVHHvyQ8ezALGK Gjvg== X-Gm-Message-State: APjAAAV42GqB2mXMNdwp322MV9YHEYmiunwrtLkpynFn+nLSjwiwBWDH bJ6tPC8uDrjdDlPEjE9mCe9Ij448Q1Y= X-Google-Smtp-Source: APXvYqwJ83gf0loZtrPVQ7IEWqosni6SRC96jFi2vmg13Wa6mHIelGk906OLzHlypHcU3iD1jCw55g== X-Received: by 2002:adf:ea42:: with SMTP id j2mr33047790wrn.270.1576535187045; Mon, 16 Dec 2019 14:26:27 -0800 (PST) Received: from xps13.home (a94-132-34-60.cpe.netcabo.pt. [94.132.34.60]) by smtp.gmail.com with ESMTPSA id o66sm837215wmo.20.2019.12.16.14.26.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Dec 2019 14:26:26 -0800 (PST) From: Alexandre Heitor Schmidt To: ffmpeg-devel@ffmpeg.org Date: Mon, 16 Dec 2019 22:26:08 +0000 Message-Id: <20191216222608.19270-1-alexandre.schmidt@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH] avfilter/image2: Add source file path and basename to each packet side data. 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 Cc: Alexandre Heitor Schmidt MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" This is the second time I'm submiting this patch, now modified to be less intrusive, as sugested by Marton Balint, and hopefully without git send-email messing up with the diff by replacing '@' within documentation entries. The patch modifies image2 filter to make available two special metadata entries called source_path and source_file, 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/filters: The documentation for drawtext was also updated and an usage example was added. Fixes #2874. Signed-off-by: Alexandre Heitor Schmidt --- doc/filters.texi | 15 +++++++++++++-- libavformat/img2.h | 2 ++ libavformat/img2dec.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/doc/filters.texi b/doc/filters.texi index c543203ce3..15498aee0d 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -9723,8 +9723,12 @@ Available metadata can be identified by inspecting entries starting with TAG included within each frame section printed by running @code{ffprobe -show_frames}. -String metadata generated in filters leading to -the drawtext filter are also available. +String metadata generated in filters leading to the drawtext filter are also +available. For example, image2 filter generates two special metadata fields +called @var{source_path} and @var{source_basename} for each input frame +generated from a single image. @var{source_path} contains the entire path +to input filename, while @var{source_basename} contains the equivalent +to @code{basename(source_path)}. @item n, frame_num The frame number, starting from 0. @@ -9872,6 +9876,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{source_basename} metadata, extracted from each input frame, or +the string "NA" if the metadata is not defined. +@example +drawtext="fontsize=20:fontcolor=white:fontfile=FreeSans.ttf:text='%{metadata\: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..8208c1f58b 100644 --- a/libavformat/img2.h +++ b/libavformat/img2.h @@ -74,5 +74,7 @@ extern const AVOption ff_img_options[]; int ff_img_read_header(AVFormatContext *s1); +int add_filename_as_pkt_side_data(char *filename, AVPacket *pkt); + int ff_img_read_packet(AVFormatContext *s1, AVPacket *pkt); #endif diff --git a/libavformat/img2dec.c b/libavformat/img2dec.c index f8b4a655a5..3fb7aa7967 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'. + */ +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, "source_path", filename, 0); + av_dict_set(&d, "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; @@ -485,6 +511,8 @@ int ff_img_read_packet(AVFormatContext *s1, AVPacket *pkt) if (s->is_pipe) pkt->pos = avio_tell(f[0]); + else if ( (res = add_filename_as_pkt_side_data(filename, pkt)) != 0 ) + goto fail; pkt->size = 0; for (i = 0; i < 3; i++) {