mp_tags: move generic mp_tags stuff into its own .c/.h files in common/

rename add_metadata to the more genera/descriptive mp_tags_copy_items_from_av_dictionary

Signed-off-by: wm4 <wm4@nowhere>
This commit is contained in:
Kevin Mitchell 2014-04-13 05:01:55 -07:00 committed by wm4
parent 47972a0077
commit bc79ded75a
7 changed files with 99 additions and 65 deletions

70
common/tags.c Normal file
View File

@ -0,0 +1,70 @@
/*
* This file is part of mpv.
*
* mpv 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.
*
* mpv 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 mpv. If not, see <http://www.gnu.org/licenses/>.
*/
#include <libavutil/dict.h>
#include "tags.h"
#include "bstr/bstr.h"
void mp_tags_set_str(struct mp_tags *tags, const char *key, const char *value)
{
mp_tags_set_bstr(tags, bstr0(key), bstr0(value));
}
void mp_tags_set_bstr(struct mp_tags *tags, bstr key, bstr value)
{
for (int n = 0; n < tags->num_keys; n++) {
if (bstrcasecmp0(key, tags->keys[n]) == 0) {
talloc_free(tags->values[n]);
tags->values[n] = talloc_strndup(tags, value.start, value.len);
return;
}
}
MP_RESIZE_ARRAY(tags, tags->keys, tags->num_keys + 1);
MP_RESIZE_ARRAY(tags, tags->values, tags->num_keys + 1);
tags->keys[tags->num_keys] = talloc_strndup(tags, key.start, key.len);
tags->values[tags->num_keys] = talloc_strndup(tags, value.start, value.len);
tags->num_keys++;
}
char *mp_tags_get_str(struct mp_tags *tags, const char *key)
{
return mp_tags_get_bstr(tags, bstr0(key));
}
char *mp_tags_get_bstr(struct mp_tags *tags, bstr key)
{
for (int n = 0; n < tags->num_keys; n++) {
if (bstrcasecmp0(key, tags->keys[n]) == 0)
return tags->values[n];
}
return NULL;
}
void mp_tags_clear(struct mp_tags *tags)
{
*tags = (struct mp_tags){0};
talloc_free_children(tags);
}
void mp_tags_copy_from_av_dictionary(struct mp_tags *tags,
struct AVDictionary *av_dict)
{
AVDictionaryEntry *entry = NULL;
while ((entry = av_dict_get(av_dict, "", entry, AV_DICT_IGNORE_SUFFIX)))
mp_tags_set_str(tags, entry->key, entry->value);
}

21
common/tags.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef MP_TAGS_H
#define MP_TAGS_H
#include "bstr/bstr.h"
struct mp_tags {
char **keys;
char **values;
int num_keys;
};
void mp_tags_set_str(struct mp_tags *tags, const char *key, const char *value);
void mp_tags_set_bstr(struct mp_tags *tags, bstr key, bstr value);
char *mp_tags_get_str(struct mp_tags *tags, const char *key);
char *mp_tags_get_bstr(struct mp_tags *tags, bstr key);
void mp_tags_clear(struct mp_tags *tags);
struct AVDictionary;
void mp_tags_copy_from_av_dictionary(struct mp_tags *tags,
struct AVDictionary *av_dict);
#endif

View File

@ -753,48 +753,6 @@ int demux_seek(demuxer_t *demuxer, float rel_seek_secs, int flags)
return 1;
}
void mp_tags_set_str(struct mp_tags *tags, const char *key, const char *value)
{
mp_tags_set_bstr(tags, bstr0(key), bstr0(value));
}
void mp_tags_set_bstr(struct mp_tags *tags, bstr key, bstr value)
{
for (int n = 0; n < tags->num_keys; n++) {
if (bstrcasecmp0(key, tags->keys[n]) == 0) {
talloc_free(tags->values[n]);
tags->values[n] = talloc_strndup(tags, value.start, value.len);
return;
}
}
MP_RESIZE_ARRAY(tags, tags->keys, tags->num_keys + 1);
MP_RESIZE_ARRAY(tags, tags->values, tags->num_keys + 1);
tags->keys[tags->num_keys] = talloc_strndup(tags, key.start, key.len);
tags->values[tags->num_keys] = talloc_strndup(tags, value.start, value.len);
tags->num_keys++;
}
char *mp_tags_get_str(struct mp_tags *tags, const char *key)
{
return mp_tags_get_bstr(tags, bstr0(key));
}
char *mp_tags_get_bstr(struct mp_tags *tags, bstr key)
{
for (int n = 0; n < tags->num_keys; n++) {
if (bstrcasecmp0(key, tags->keys[n]) == 0)
return tags->values[n];
}
return NULL;
}
void mp_tags_clear(struct mp_tags *tags)
{
*tags = (struct mp_tags){0};
talloc_free_children(tags);
}
static int demux_info_print(demuxer_t *demuxer)
{
struct mp_tags *info = demuxer->metadata;

View File

@ -27,6 +27,7 @@
#include "bstr/bstr.h"
#include "common/common.h"
#include "common/tags.h"
#include "packet.h"
#include "stheader.h"
@ -104,12 +105,6 @@ typedef struct demuxer_desc {
int (*control)(struct demuxer *demuxer, int cmd, void *arg);
} demuxer_desc_t;
struct mp_tags {
char **keys;
char **values;
int num_keys;
};
typedef struct demux_chapter
{
int original_index;
@ -292,12 +287,6 @@ double demux_packet_list_duration(struct demux_packet **pkts, int num_pkts);
struct demux_packet *demux_packet_list_fill(struct demux_packet **pkts,
int num_pkts, int *current);
void mp_tags_set_str(struct mp_tags *tags, const char *key, const char *value);
void mp_tags_set_bstr(struct mp_tags *tags, bstr key, bstr value);
char *mp_tags_get_str(struct mp_tags *tags, const char *key);
char *mp_tags_get_bstr(struct mp_tags *tags, bstr key);
void mp_tags_clear(struct mp_tags *tags);
bool demux_matroska_uid_cmp(struct matroska_segment_uid *a,
struct matroska_segment_uid *b);

View File

@ -41,6 +41,7 @@
#include "options/options.h"
#include "common/msg.h"
#include "common/tags.h"
#include "common/av_opts.h"
#include "common/av_common.h"
#include "bstr/bstr.h"
@ -560,13 +561,6 @@ static void add_new_streams(demuxer_t *demuxer)
handle_stream(demuxer, priv->num_streams);
}
static void add_metadata(struct mp_tags *tags, AVDictionary *metadata)
{
AVDictionaryEntry *t = NULL;
while ((t = av_dict_get(metadata, "", t, AV_DICT_IGNORE_SUFFIX)))
mp_tags_set_str(tags, t->key, t->value);
}
static void update_metadata(demuxer_t *demuxer, AVPacket *pkt)
{
#if HAVE_AVCODEC_METADATA_UPDATE_SIDE_DATA
@ -578,7 +572,7 @@ static void update_metadata(demuxer_t *demuxer, AVPacket *pkt)
av_packet_unpack_dictionary(md, md_size, &dict);
if (dict) {
mp_tags_clear(demuxer->metadata);
add_metadata(demuxer->metadata, dict);
mp_tags_copy_from_av_dictionary(demuxer->metadata, dict);
av_dict_free(&dict);
}
}
@ -706,19 +700,19 @@ static int demux_open_lavf(demuxer_t *demuxer, enum demux_check check)
t = av_dict_get(c->metadata, "title", NULL, 0);
int index = demuxer_add_chapter(demuxer, t ? bstr0(t->value) : bstr0(""),
start, end, i);
add_metadata(demuxer->chapters[index].metadata, c->metadata);
mp_tags_copy_from_av_dictionary(demuxer->chapters[index].metadata, c->metadata);
}
add_new_streams(demuxer);
add_metadata(demuxer->metadata, avfc->metadata);
mp_tags_copy_from_av_dictionary(demuxer->metadata, avfc->metadata);
// Often useful with OGG audio-only files, which have metadata in the audio
// track metadata instead of the main metadata.
if (demuxer->num_streams == 1) {
for (int n = 0; n < priv->num_streams; n++) {
if (priv->streams[n])
add_metadata(demuxer->metadata, avfc->streams[n]->metadata);
mp_tags_copy_from_av_dictionary(demuxer->metadata, avfc->streams[n]->metadata);
}
}

View File

@ -185,6 +185,7 @@ SOURCES = audio/audio.c \
common/common.c \
common/msg.c \
common/playlist.c \
common/tags.c \
common/version.c \
demux/codec_tags.c \
demux/demux.c \

View File

@ -161,6 +161,7 @@ def build(ctx):
( "common/cpudetect.c" ),
( "common/encode_lavc.c", "encoding" ),
( "common/common.c" ),
( "common/tags.c" ),
( "common/msg.c" ),
( "common/playlist.c" ),
( "common/version.c" ),