From patchwork Sat Dec 14 22:59:31 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: 16798 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 9BE544451FA for ; Sun, 15 Dec 2019 01:05:08 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7C6DB68925F; Sun, 15 Dec 2019 01:05:08 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f54.google.com (mail-wr1-f54.google.com [209.85.221.54]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9A45E6883E0 for ; Sun, 15 Dec 2019 01:05:02 +0200 (EET) Received: by mail-wr1-f54.google.com with SMTP id c9so2795618wrw.8 for ; Sat, 14 Dec 2019 15:05:02 -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=IN6vkMbcE2x7b3rOEzqTAF+0Cm1HjLFYgx329HE7Yr0=; b=DTxM8u1N9UivESE9cRhZbq/2dcB/iqIm7NoiZYnfBOk+qUEkipGZNLGJbbgnCekuwn BP1NQwA7UMLwwm6gjZNqMva534EkavsfZqYsYc50skHpQ1rNiYnt9iKCxnZqsQF99wfd bMnM/qQw6aAIVgpnc8mwJPwgp90ouGqaJcD0cMHpZBjcGerWXDDSgb2qFmoqbACzqEDN z7ExX/+a0OvOZ7MuRgThz8Z7CEHgm9WmREdxkoJcP835cX+66/cuiZeGXe/w1gIRHkVW RL5KHPf7YrNqxrP5qAU1PReUvSNRg1iVa468J4MpRiyu/huicHGt/49Fr9pmEWA6D/ll q06A== 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=IN6vkMbcE2x7b3rOEzqTAF+0Cm1HjLFYgx329HE7Yr0=; b=JU0E9HyMb+3cd4CX2WPXoiErb6GQnX4CkpZKaz9C9SbD8O+CBya+zQDO8osz3uxfEG jJBwc8Enf65hJkseBR+2LobbSEEYQ8P1gA+oxZiZhKHxzfA7D6Gz0uE82rf927wvJn3F cB9Jc1i9it7geb3L6gVU7I1HSozGtjF+r8Q++CC8kAGJnF9jIcqhnRYOTAWpQSUiw3lK +W4CqKlgvy6DYCfeQDCCNLVasLeN5uvfmM1Eo3JJbqaQDi+IRxWMF3OJ4nS4pA579qYI BqNNAHctW9eIDDQP4/ke+Q1GeapH08KrR/6q9t2H7YilAY7OIQBrdG5vXT8UT/Xtem6r 7GbQ== X-Gm-Message-State: APjAAAXH/mzqtqKXojsPW9JVLDdNBD770lXKI++bojxOul7YVDp2bE7v g5PdSW2LU5n3ebvq/n/Lhz69+qAqyQ4= X-Google-Smtp-Source: APXvYqxYHysjUfljeTB532INOXK8DR0NZkvGcr2LrMkTzKrHLJnlqYMaMzb2V1fkCcUrkXpOR51MpA== X-Received: by 2002:a5d:488c:: with SMTP id g12mr20794770wrq.67.1576364381240; Sat, 14 Dec 2019 14:59:41 -0800 (PST) Received: from xps13.home (a94-132-34-60.cpe.netcabo.pt. [94.132.34.60]) by smtp.gmail.com with ESMTPSA id c68sm3106198wme.13.2019.12.14.14.59.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 Dec 2019 14:59:40 -0800 (PST) From: Alexandre Heitor Schmidt To: ffmpeg-devel@ffmpeg.org Date: Sat, 14 Dec 2019 22:59:31 +0000 Message-Id: <20191214225931.28368-1-alexandre.schmidt@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH] avfilter/drawtext: special metadata fields to allow plotting source filename to output. 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" avfilter/drawtext: Added two special metadata tags which can, be used by 'drawtext' filter and others to have access to source file path, and its basename. The new field AVPacket->source_file will contain a path to its corresponding filename everytime the packet corresponds to a single file. This behavior is common for image2 filter. When this field is filled, frames passed to filters will contain two special metadata tags called source_path (the entire path to the file) and source_basename (only the file name). These metatags can be used by filters like drawtext to be able to plot the entire path or the basename of it respectively to the frame being processed. 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 | 13 +++++++++++++ fftools/ffmpeg.c | 12 ++++++++++++ libavcodec/avcodec.h | 8 ++++++++ libavcodec/avpacket.c | 1 + libavformat/img2dec.c | 9 +++++++++ 5 files changed, 43 insertions(+) diff --git a/doc/filters.texi b/doc/filters.texi index 93f54a2e1e..d984efb73b 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -9726,6 +9726,12 @@ printed by running @code{ffprobe -show_frames}. String metadata generated in filters leading to the drawtext filter are also available. +There are two special metadata fields (@var{source_path} and @var{source_basename}) +which can be used when input format has a file corresponding to each frame, such +as @var{image2}. @var{source_path} will output the entire path to the filename +which generated the frame in question, while @var{source_basename} outputs the +equivalent to @code{basename(source_path)}. + @item n, frame_num The frame number, starting from 0. @@ -9872,6 +9878,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/fftools/ffmpeg.c b/fftools/ffmpeg.c index 27f68933f8..9285f86a81 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -2375,6 +2375,18 @@ static int decode_video(InputStream *ist, AVPacket *pkt, int *got_output, int64_ if (ret < 0) *decode_failed = 1; + /** + * Set filename as a frame metadata, if it's not empty. This will allow filters + * like drawtext to use this information for cases like image2, where each frame + * corresponds to a file. + */ + if (pkt) { + if ( pkt->source_filename != NULL ) { + av_dict_set(&decoded_frame->metadata, "source_path", pkt->source_filename, 0); + av_dict_set(&decoded_frame->metadata, "source_basename", av_basename(pkt->source_filename), 0); + } + } + // The following line may be required in some cases where there is no parser // or the parser does not has_b_frames correctly if (ist->st->codecpar->video_delay < ist->dec_ctx->has_b_frames) { diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 119b32dc1f..4e285f4dd8 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -1511,6 +1511,14 @@ typedef struct AVPacket { attribute_deprecated int64_t convergence_duration; #endif + + /** + * In cases where the packet corresponds to the contents of one single + * file, like when image2 is used, this is set to the source filename, + * so it can be used by filters like drawtext, to plot the source + * filename on the frame. + */ + char *source_filename; } AVPacket; #define AV_PKT_FLAG_KEY 0x0001 ///< The packet contains a keyframe #define AV_PKT_FLAG_CORRUPT 0x0002 ///< The packet content is corrupted diff --git a/libavcodec/avpacket.c b/libavcodec/avpacket.c index 858f827a0a..c357e2fbac 100644 --- a/libavcodec/avpacket.c +++ b/libavcodec/avpacket.c @@ -46,6 +46,7 @@ FF_ENABLE_DEPRECATION_WARNINGS pkt->buf = NULL; pkt->side_data = NULL; pkt->side_data_elems = 0; + pkt->source_filename = NULL; } AVPacket *av_packet_alloc(void) diff --git a/libavformat/img2dec.c b/libavformat/img2dec.c index f8b4a655a5..02119d89fc 100644 --- a/libavformat/img2dec.c +++ b/libavformat/img2dec.c @@ -485,6 +485,15 @@ int ff_img_read_packet(AVFormatContext *s1, AVPacket *pkt) if (s->is_pipe) pkt->pos = avio_tell(f[0]); + else { + // Set the filename which corresponds to this packet. + pkt->source_filename = av_malloc(strlen(filename)); + if (!pkt->source_filename) { + av_log(NULL, AV_LOG_FATAL, "Failed to allocate source_filename\n"); + return AVERROR(ENOMEM); + } + pkt->source_filename = av_asprintf("%s", filename); + } pkt->size = 0; for (i = 0; i < 3; i++) {