From 673336297d58af9afd61ab5689cfc8a6e996e790 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@duempel.org>
Date: Thu, 11 Dec 2014 10:39:49 +0100
Subject: [PATCH] decoder/ffmpeg: support stream tags

---
 NEWS                                        |  1 +
 src/decoder/plugins/FfmpegDecoderPlugin.cxx | 39 +++++++++++++++++++++
 2 files changed, 40 insertions(+)

diff --git a/NEWS b/NEWS
index 68a160e08..767e8f41f 100644
--- a/NEWS
+++ b/NEWS
@@ -7,6 +7,7 @@ ver 0.20 (not yet released)
   - ape: drop support for non-standard tag "album artist"
 * decoder
   - ffmpeg: support ReplayGain and MixRamp
+  - ffmpeg: support stream tags
 * output
   - pulse: set channel map to WAVE-EX
 * mixer
diff --git a/src/decoder/plugins/FfmpegDecoderPlugin.cxx b/src/decoder/plugins/FfmpegDecoderPlugin.cxx
index 67c585b34..965a479ad 100644
--- a/src/decoder/plugins/FfmpegDecoderPlugin.cxx
+++ b/src/decoder/plugins/FfmpegDecoderPlugin.cxx
@@ -25,6 +25,7 @@
 #include "lib/ffmpeg/Domain.hxx"
 #include "../DecoderAPI.hxx"
 #include "FfmpegMetaData.hxx"
+#include "tag/TagBuilder.hxx"
 #include "tag/TagHandler.hxx"
 #include "tag/ReplayGain.hxx"
 #include "tag/MixRamp.hxx"
@@ -508,6 +509,40 @@ FfmpegScanMetadata(const AVFormatContext &format_context, int audio_stream,
 				   handler, handler_ctx);
 }
 
+#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(56, 1, 0)
+
+static void
+FfmpegScanTag(const AVFormatContext &format_context, int audio_stream,
+	      TagBuilder &tag)
+{
+	FfmpegScanMetadata(format_context, audio_stream,
+			   full_tag_handler, &tag);
+}
+
+/**
+ * Check if a new stream tag was received and pass it to
+ * decoder_tag().
+ */
+static void
+FfmpegCheckTag(Decoder &decoder, InputStream &is,
+	       AVFormatContext &format_context, int audio_stream)
+{
+	AVStream &stream = *format_context.streams[audio_stream];
+	if ((stream.event_flags & AVSTREAM_EVENT_FLAG_METADATA_UPDATED) == 0)
+		/* no new metadata */
+		return;
+
+	/* clear the flag */
+	stream.event_flags &= ~AVSTREAM_EVENT_FLAG_METADATA_UPDATED;
+
+	TagBuilder tag;
+	FfmpegScanTag(format_context, audio_stream, tag);
+	if (!tag.IsEmpty())
+		decoder_tag(decoder, is, tag.Commit());
+}
+
+#endif
+
 static void
 ffmpeg_decode(Decoder &decoder, InputStream &input)
 {
@@ -635,6 +670,10 @@ ffmpeg_decode(Decoder &decoder, InputStream &input)
 			/* end of file */
 			break;
 
+#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(56, 1, 0)
+		FfmpegCheckTag(decoder, input, *format_context, audio_stream);
+#endif
+
 		if (packet.stream_index == audio_stream)
 			cmd = ffmpeg_send_packet(decoder, input,
 						 &packet, codec_context,