FfmpegMetaData.cxx 2.67 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

20 21 22 23
/* necessary because libavutil/common.h uses UINT64_C */
#define __STDC_CONSTANT_MACROS

#include "FfmpegMetaData.hxx"
24 25
#include "tag/Table.hxx"
#include "tag/Handler.hxx"
26
#include "tag/Id3MusicBrainz.hxx"
27
#include "util/StringView.hxx"
28

29 30 31 32
extern "C" {
#include <libavutil/dict.h>
}

33 34 35 36
/**
 * FFmpeg specific tag name mappings, as supported by
 * libavformat/id3v2.c, libavformat/mov.c and others.
 */
37
static constexpr struct tag_table ffmpeg_tags[] = {
38
	/* from libavformat/id3v2.c, libavformat/mov.c */
39
	{ "album_artist", TAG_ALBUM_ARTIST },
40

41 42 43 44
	/* from libavformat/id3v2.c */
	{ "album-sort", TAG_ALBUM_SORT },
	{ "artist-sort", TAG_ARTIST_SORT },

45
	/* from libavformat/mov.c */
46
	{ "sort_album_artist", TAG_ALBUM_ARTIST_SORT },
47
	{ "sort_album", TAG_ALBUM_SORT },
48
	{ "sort_artist", TAG_ARTIST_SORT },
49 50

	/* sentinel */
51
	{ nullptr, TAG_NUM_OF_ITEM_TYPES }
52 53 54
};

static void
55 56
FfmpegScanTag(TagType type,
	      AVDictionary *m, const char *name,
57
	      TagHandler &handler) noexcept
58
{
59
	AVDictionaryEntry *mt = nullptr;
60

61
	while ((mt = av_dict_get(m, name, mt, 0)) != nullptr)
62
		handler.OnTag(type, mt->value);
63 64
}

65
static void
66
FfmpegScanPairs(AVDictionary *dict, TagHandler &handler) noexcept
67
{
68
	AVDictionaryEntry *i = nullptr;
69

70
	while ((i = av_dict_get(dict, "", i, AV_DICT_IGNORE_SUFFIX)) != nullptr)
71
		handler.OnPair(i->key, i->value);
72 73
}

74
void
75
FfmpegScanDictionary(AVDictionary *dict, TagHandler &handler) noexcept
76
{
77
	if (handler.WantTag()) {
78
		for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i)
79
			FfmpegScanTag(TagType(i), dict, tag_item_names[i],
80
				      handler);
81

82 83
		for (const struct tag_table *i = ffmpeg_tags;
		     i->name != nullptr; ++i)
84
			FfmpegScanTag(i->type, dict, i->name,
85
				      handler);
86 87 88 89

		for (const struct tag_table *i = musicbrainz_txxx_tags;
		     i->name != nullptr; ++i)
			FfmpegScanTag(i->type, dict, i->name,
90
				      handler);
91
	}
92

93 94
	if (handler.WantPair())
		FfmpegScanPairs(dict, handler);
95
}