1/*
2 * copyright (c) 2009 Michael Niedermayer
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <strings.h>
22#include "avformat.h"
23#include "metadata.h"
24
25AVMetadataTag *
26av_metadata_get(AVMetadata *m, const char *key, const AVMetadataTag *prev, int flags)
27{
28    unsigned int i, j;
29
30    if(!m)
31        return NULL;
32
33    if(prev) i= prev - m->elems + 1;
34    else     i= 0;
35
36    for(; i<m->count; i++){
37        const char *s= m->elems[i].key;
38        if(flags & AV_METADATA_MATCH_CASE) for(j=0;         s[j]  ==         key[j]  && key[j]; j++);
39        else                               for(j=0; toupper(s[j]) == toupper(key[j]) && key[j]; j++);
40        if(key[j])
41            continue;
42        if(s[j] && !(flags & AV_METADATA_IGNORE_SUFFIX))
43            continue;
44        return &m->elems[i];
45    }
46    return NULL;
47}
48
49int av_metadata_set2(AVMetadata **pm, const char *key, const char *value, int flags)
50{
51    AVMetadata *m= *pm;
52    AVMetadataTag *tag= av_metadata_get(m, key, NULL, AV_METADATA_MATCH_CASE);
53
54    if(!m)
55        m=*pm= av_mallocz(sizeof(*m));
56
57    if(tag){
58        if (flags & AV_METADATA_DONT_OVERWRITE)
59            return 0;
60        av_free(tag->value);
61        av_free(tag->key);
62        *tag= m->elems[--m->count];
63    }else{
64        AVMetadataTag *tmp= av_realloc(m->elems, (m->count+1) * sizeof(*m->elems));
65        if(tmp){
66            m->elems= tmp;
67        }else
68            return AVERROR(ENOMEM);
69    }
70    if(value){
71        if(flags & AV_METADATA_DONT_STRDUP_KEY){
72            m->elems[m->count].key  = key;
73        }else
74        m->elems[m->count].key  = av_strdup(key  );
75        if(flags & AV_METADATA_DONT_STRDUP_VAL){
76            m->elems[m->count].value= value;
77        }else
78        m->elems[m->count].value= av_strdup(value);
79        m->count++;
80    }
81    if(!m->count) {
82        av_free(m->elems);
83        av_freep(pm);
84    }
85
86    return 0;
87}
88
89#if LIBAVFORMAT_VERSION_MAJOR == 52
90int av_metadata_set(AVMetadata **pm, const char *key, const char *value)
91{
92    return av_metadata_set2(pm, key, value, 0);
93}
94#endif
95
96void av_metadata_free(AVMetadata **pm)
97{
98    AVMetadata *m= *pm;
99
100    if(m){
101        while(m->count--){
102            av_free(m->elems[m->count].key);
103            av_free(m->elems[m->count].value);
104        }
105        av_free(m->elems);
106    }
107    av_freep(pm);
108}
109
110void metadata_conv(AVMetadata **pm, const AVMetadataConv *d_conv,
111                                           const AVMetadataConv *s_conv)
112{
113    /* TODO: use binary search to look up the two conversion tables
114       if the tables are getting big enough that it would matter speed wise */
115    const AVMetadataConv *sc, *dc;
116    AVMetadataTag *mtag = NULL;
117    AVMetadata *dst = NULL;
118    const char *key;
119
120    while((mtag=av_metadata_get(*pm, "", mtag, AV_METADATA_IGNORE_SUFFIX))) {
121        key = mtag->key;
122        if (s_conv != d_conv) {
123            if (s_conv)
124                for (sc=s_conv; sc->native; sc++)
125                    if (!strcasecmp(key, sc->native)) {
126                        key = sc->generic;
127                        break;
128                    }
129            if (d_conv)
130                for (dc=d_conv; dc->native; dc++)
131                    if (!strcasecmp(key, dc->generic)) {
132                        key = dc->native;
133                        break;
134                    }
135        }
136        av_metadata_set2(&dst, key, mtag->value, 0);
137    }
138    av_metadata_free(pm);
139    *pm = dst;
140}
141
142void av_metadata_conv(AVFormatContext *ctx, const AVMetadataConv *d_conv,
143                                            const AVMetadataConv *s_conv)
144{
145    int i;
146    metadata_conv(&ctx->metadata, d_conv, s_conv);
147    for (i=0; i<ctx->nb_streams ; i++)
148        metadata_conv(&ctx->streams [i]->metadata, d_conv, s_conv);
149    for (i=0; i<ctx->nb_chapters; i++)
150        metadata_conv(&ctx->chapters[i]->metadata, d_conv, s_conv);
151    for (i=0; i<ctx->nb_programs; i++)
152        metadata_conv(&ctx->programs[i]->metadata, d_conv, s_conv);
153}
154