• 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_set(AVMetadata **pm, const char *key, const char *value)
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         av_free(tag->value);
00059         av_free(tag->key);
00060         *tag= m->elems[--m->count];
00061     }else{
00062         AVMetadataTag *tmp= av_realloc(m->elems, (m->count+1) * sizeof(*m->elems));
00063         if(tmp){
00064             m->elems= tmp;
00065         }else
00066             return AVERROR(ENOMEM);
00067     }
00068     if(value){
00069         m->elems[m->count].key  = av_strdup(key  );
00070         m->elems[m->count].value= av_strdup(value);
00071         m->count++;
00072     }
00073     if(!m->count) {
00074         av_free(m->elems);
00075         av_freep(pm);
00076     }
00077 
00078     return 0;
00079 }
00080 
00081 void av_metadata_free(AVMetadata **pm)
00082 {
00083     AVMetadata *m= *pm;
00084 
00085     if(m){
00086         while(m->count--){
00087             av_free(m->elems[m->count].key);
00088             av_free(m->elems[m->count].value);
00089         }
00090         av_free(m->elems);
00091     }
00092     av_freep(pm);
00093 }
00094 
00095 static void metadata_conv(AVMetadata **pm, const AVMetadataConv *d_conv,
00096                                            const AVMetadataConv *s_conv)
00097 {
00098     /* TODO: use binary search to look up the two conversion tables
00099        if the tables are getting big enough that it would matter speed wise */
00100     const AVMetadataConv *sc, *dc;
00101     AVMetadataTag *mtag = NULL;
00102     AVMetadata *dst = NULL;
00103     const char *key;
00104 
00105     while((mtag=av_metadata_get(*pm, "", mtag, AV_METADATA_IGNORE_SUFFIX))) {
00106         key = mtag->key;
00107         if (s_conv != d_conv) {
00108             if (s_conv)
00109                 for (sc=s_conv; sc->native; sc++)
00110                 if (!strcasecmp(key, sc->native)) {
00111                     key = sc->generic;
00112                     break;
00113                 }
00114             if (d_conv)
00115                 for (dc=d_conv; dc->native; dc++)
00116                     if (!strcasecmp(key, dc->generic)) {
00117                     key = dc->native;
00118                     break;
00119                 }
00120         }
00121         av_metadata_set(&dst, key, mtag->value);
00122     }
00123     av_metadata_free(pm);
00124     *pm = dst;
00125 }
00126 
00127 void av_metadata_conv(AVFormatContext *ctx, const AVMetadataConv *d_conv,
00128                                             const AVMetadataConv *s_conv)
00129 {
00130     int i;
00131     metadata_conv(&ctx->metadata, d_conv, s_conv);
00132     for (i=0; i<ctx->nb_streams ; i++)
00133         metadata_conv(&ctx->streams [i]->metadata, d_conv, s_conv);
00134     for (i=0; i<ctx->nb_chapters; i++)
00135         metadata_conv(&ctx->chapters[i]->metadata, d_conv, s_conv);
00136     for (i=0; i<ctx->nb_programs; i++)
00137         metadata_conv(&ctx->programs[i]->metadata, d_conv, s_conv);
00138 }

Generated on Tue Nov 4 2014 12:59:23 for ffmpeg by  doxygen 1.7.1