• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavformat/metadata.c

Go to the documentation of this file.
00001 /*
00002  * copyright (c) 2009 Michael Niedermayer
00003  *
00004  * This file is part of FFmpeg.
00005  *
00006  * FFmpeg is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * FFmpeg is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with FFmpeg; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 
00021 #include <strings.h>
00022 #include "avformat.h"
00023 #include "metadata.h"
00024 
00025 AVMetadataTag *
00026 av_metadata_get(AVMetadata *m, const char *key, const AVMetadataTag *prev, int flags)
00027 {
00028     unsigned int i, j;
00029 
00030     if(!m)
00031         return NULL;
00032 
00033     if(prev) i= prev - m->elems + 1;
00034     else     i= 0;
00035 
00036     for(; i<m->count; i++){
00037         const char *s= m->elems[i].key;
00038         if(flags & AV_METADATA_MATCH_CASE) for(j=0;         s[j]  ==         key[j]  && key[j]; j++);
00039         else                               for(j=0; toupper(s[j]) == toupper(key[j]) && key[j]; j++);
00040         if(key[j])
00041             continue;
00042         if(s[j] && !(flags & AV_METADATA_IGNORE_SUFFIX))
00043             continue;
00044         return &m->elems[i];
00045     }
00046     return NULL;
00047 }
00048 
00049 int av_metadata_set2(AVMetadata **pm, const char *key, const char *value, int flags)
00050 {
00051     AVMetadata *m= *pm;
00052     AVMetadataTag *tag= av_metadata_get(m, key, NULL, AV_METADATA_MATCH_CASE);
00053 
00054     if(!m)
00055         m=*pm= av_mallocz(sizeof(*m));
00056 
00057     if(tag){
00058         if (flags & AV_METADATA_DONT_OVERWRITE)
00059             return 0;
00060         av_free(tag->value);
00061         av_free(tag->key);
00062         *tag= m->elems[--m->count];
00063     }else{
00064         AVMetadataTag *tmp= av_realloc(m->elems, (m->count+1) * sizeof(*m->elems));
00065         if(tmp){
00066             m->elems= tmp;
00067         }else
00068             return AVERROR(ENOMEM);
00069     }
00070     if(value){
00071         if(flags & AV_METADATA_DONT_STRDUP_KEY){
00072             m->elems[m->count].key  = key;
00073         }else
00074         m->elems[m->count].key  = av_strdup(key  );
00075         if(flags & AV_METADATA_DONT_STRDUP_VAL){
00076             m->elems[m->count].value= value;
00077         }else
00078         m->elems[m->count].value= av_strdup(value);
00079         m->count++;
00080     }
00081     if(!m->count) {
00082         av_free(m->elems);
00083         av_freep(pm);
00084     }
00085 
00086     return 0;
00087 }
00088 
00089 #if LIBAVFORMAT_VERSION_MAJOR == 52
00090 int av_metadata_set(AVMetadata **pm, const char *key, const char *value)
00091 {
00092     return av_metadata_set2(pm, key, value, 0);
00093 }
00094 #endif
00095 
00096 void av_metadata_free(AVMetadata **pm)
00097 {
00098     AVMetadata *m= *pm;
00099 
00100     if(m){
00101         while(m->count--){
00102             av_free(m->elems[m->count].key);
00103             av_free(m->elems[m->count].value);
00104         }
00105         av_free(m->elems);
00106     }
00107     av_freep(pm);
00108 }
00109 
00110 void metadata_conv(AVMetadata **pm, const AVMetadataConv *d_conv,
00111                                            const AVMetadataConv *s_conv)
00112 {
00113     /* TODO: use binary search to look up the two conversion tables
00114        if the tables are getting big enough that it would matter speed wise */
00115     const AVMetadataConv *sc, *dc;
00116     AVMetadataTag *mtag = NULL;
00117     AVMetadata *dst = NULL;
00118     const char *key;
00119 
00120     while((mtag=av_metadata_get(*pm, "", mtag, AV_METADATA_IGNORE_SUFFIX))) {
00121         key = mtag->key;
00122         if (s_conv != d_conv) {
00123             if (s_conv)
00124                 for (sc=s_conv; sc->native; sc++)
00125                     if (!strcasecmp(key, sc->native)) {
00126                         key = sc->generic;
00127                         break;
00128                     }
00129             if (d_conv)
00130                 for (dc=d_conv; dc->native; dc++)
00131                     if (!strcasecmp(key, dc->generic)) {
00132                         key = dc->native;
00133                         break;
00134                     }
00135         }
00136         av_metadata_set2(&dst, key, mtag->value, 0);
00137     }
00138     av_metadata_free(pm);
00139     *pm = dst;
00140 }
00141 
00142 void av_metadata_conv(AVFormatContext *ctx, const AVMetadataConv *d_conv,
00143                                             const AVMetadataConv *s_conv)
00144 {
00145     int i;
00146     metadata_conv(&ctx->metadata, d_conv, s_conv);
00147     for (i=0; i<ctx->nb_streams ; i++)
00148         metadata_conv(&ctx->streams [i]->metadata, d_conv, s_conv);
00149     for (i=0; i<ctx->nb_chapters; i++)
00150         metadata_conv(&ctx->chapters[i]->metadata, d_conv, s_conv);
00151     for (i=0; i<ctx->nb_programs; i++)
00152         metadata_conv(&ctx->programs[i]->metadata, d_conv, s_conv);
00153 }

Generated on Fri Sep 16 2011 17:17:48 for FFmpeg by  doxygen 1.7.1