annotate metadata.c @ 5958:ad447f146859 libavformat

oggdec: Move warning about missing granule to the correct place
author conrad
date Fri, 16 Apr 2010 12:21:38 +0000
parents 854b3ac40b8e
children f74198942337
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4150
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
1 /*
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
2 * copyright (c) 2009 Michael Niedermayer
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
3 *
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
4 * This file is part of FFmpeg.
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
5 *
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
6 * FFmpeg is free software; you can redistribute it and/or
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
7 * modify it under the terms of the GNU Lesser General Public
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
8 * License as published by the Free Software Foundation; either
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
9 * version 2.1 of the License, or (at your option) any later version.
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
10 *
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
11 * FFmpeg is distributed in the hope that it will be useful,
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
14 * Lesser General Public License for more details.
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
15 *
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
16 * You should have received a copy of the GNU Lesser General Public
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
17 * License along with FFmpeg; if not, write to the Free Software
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
19 */
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
20
4617
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
21 #include <strings.h>
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
22 #include "avformat.h"
4150
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
23 #include "metadata.h"
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
24
4154
bd4d3fee45d0 rename AVMetaData to AVMetadata and meta_data to metadata
aurel
parents: 4150
diff changeset
25 AVMetadataTag *
4157
017e68062073 add a typedef for AVMetadata
aurel
parents: 4154
diff changeset
26 av_metadata_get(AVMetadata *m, const char *key, const AVMetadataTag *prev, int flags)
4150
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
27 {
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
28 unsigned int i, j;
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
29
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
30 if(!m)
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
31 return NULL;
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
32
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
33 if(prev) i= prev - m->elems + 1;
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
34 else i= 0;
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
35
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
36 for(; i<m->count; i++){
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
37 const char *s= m->elems[i].key;
4250
2fc899894f5e replace AV_METADATA_IGNORE_CASE flag by a new AV_METADATA_MATCH_CASE flag
aurel
parents: 4180
diff changeset
38 if(flags & AV_METADATA_MATCH_CASE) for(j=0; s[j] == key[j] && key[j]; j++);
2fc899894f5e replace AV_METADATA_IGNORE_CASE flag by a new AV_METADATA_MATCH_CASE flag
aurel
parents: 4180
diff changeset
39 else for(j=0; toupper(s[j]) == toupper(key[j]) && key[j]; j++);
4150
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
40 if(key[j])
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
41 continue;
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
42 if(s[j] && !(flags & AV_METADATA_IGNORE_SUFFIX))
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
43 continue;
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
44 return &m->elems[i];
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
45 }
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
46 return NULL;
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
47 }
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
48
5445
d1b5bb2bfe92 Add flags to prevent strdup() on arguments to av_metadata_set2().
michael
parents: 5308
diff changeset
49 int av_metadata_set2(AVMetadata **pm, const char *key, const char *value, int flags)
4150
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
50 {
4157
017e68062073 add a typedef for AVMetadata
aurel
parents: 4154
diff changeset
51 AVMetadata *m= *pm;
4353
e4a1b568b313 modify the way to pass parameters to av_metadata_set()
aurel
parents: 4250
diff changeset
52 AVMetadataTag *tag= av_metadata_get(m, key, NULL, AV_METADATA_MATCH_CASE);
4150
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
53
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
54 if(!m)
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
55 m=*pm= av_mallocz(sizeof(*m));
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
56
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
57 if(tag){
5938
854b3ac40b8e Add a flag to not overwrite existing tags
rbultje
parents: 5661
diff changeset
58 if (flags & AV_METADATA_DONT_OVERWRITE)
854b3ac40b8e Add a flag to not overwrite existing tags
rbultje
parents: 5661
diff changeset
59 return 0;
4150
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
60 av_free(tag->value);
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
61 av_free(tag->key);
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
62 *tag= m->elems[--m->count];
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
63 }else{
4154
bd4d3fee45d0 rename AVMetaData to AVMetadata and meta_data to metadata
aurel
parents: 4150
diff changeset
64 AVMetadataTag *tmp= av_realloc(m->elems, (m->count+1) * sizeof(*m->elems));
4150
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
65 if(tmp){
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
66 m->elems= tmp;
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
67 }else
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
68 return AVERROR(ENOMEM);
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
69 }
4353
e4a1b568b313 modify the way to pass parameters to av_metadata_set()
aurel
parents: 4250
diff changeset
70 if(value){
5445
d1b5bb2bfe92 Add flags to prevent strdup() on arguments to av_metadata_set2().
michael
parents: 5308
diff changeset
71 if(flags & AV_METADATA_DONT_STRDUP_KEY){
d1b5bb2bfe92 Add flags to prevent strdup() on arguments to av_metadata_set2().
michael
parents: 5308
diff changeset
72 m->elems[m->count].key = key;
d1b5bb2bfe92 Add flags to prevent strdup() on arguments to av_metadata_set2().
michael
parents: 5308
diff changeset
73 }else
4353
e4a1b568b313 modify the way to pass parameters to av_metadata_set()
aurel
parents: 4250
diff changeset
74 m->elems[m->count].key = av_strdup(key );
5445
d1b5bb2bfe92 Add flags to prevent strdup() on arguments to av_metadata_set2().
michael
parents: 5308
diff changeset
75 if(flags & AV_METADATA_DONT_STRDUP_VAL){
d1b5bb2bfe92 Add flags to prevent strdup() on arguments to av_metadata_set2().
michael
parents: 5308
diff changeset
76 m->elems[m->count].value= value;
d1b5bb2bfe92 Add flags to prevent strdup() on arguments to av_metadata_set2().
michael
parents: 5308
diff changeset
77 }else
4353
e4a1b568b313 modify the way to pass parameters to av_metadata_set()
aurel
parents: 4250
diff changeset
78 m->elems[m->count].value= av_strdup(value);
e4a1b568b313 modify the way to pass parameters to av_metadata_set()
aurel
parents: 4250
diff changeset
79 m->count++;
4150
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
80 }
4591
e62a61d3e591 fix a memleak in av_metadata_set()
aurel
parents: 4353
diff changeset
81 if(!m->count) {
e62a61d3e591 fix a memleak in av_metadata_set()
aurel
parents: 4353
diff changeset
82 av_free(m->elems);
4150
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
83 av_freep(pm);
4591
e62a61d3e591 fix a memleak in av_metadata_set()
aurel
parents: 4353
diff changeset
84 }
4150
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
85
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
86 return 0;
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
87 }
4158
ea1e24175669 Add a metadata compatibility layer, so that when a user application set
aurel
parents: 4157
diff changeset
88
5445
d1b5bb2bfe92 Add flags to prevent strdup() on arguments to av_metadata_set2().
michael
parents: 5308
diff changeset
89 #if LIBAVFORMAT_VERSION_MAJOR == 52
d1b5bb2bfe92 Add flags to prevent strdup() on arguments to av_metadata_set2().
michael
parents: 5308
diff changeset
90 int av_metadata_set(AVMetadata **pm, const char *key, const char *value)
d1b5bb2bfe92 Add flags to prevent strdup() on arguments to av_metadata_set2().
michael
parents: 5308
diff changeset
91 {
d1b5bb2bfe92 Add flags to prevent strdup() on arguments to av_metadata_set2().
michael
parents: 5308
diff changeset
92 return av_metadata_set2(pm, key, value, 0);
d1b5bb2bfe92 Add flags to prevent strdup() on arguments to av_metadata_set2().
michael
parents: 5308
diff changeset
93 }
d1b5bb2bfe92 Add flags to prevent strdup() on arguments to av_metadata_set2().
michael
parents: 5308
diff changeset
94 #endif
d1b5bb2bfe92 Add flags to prevent strdup() on arguments to av_metadata_set2().
michael
parents: 5308
diff changeset
95
4161
6a7275621c02 free all allocated metadata structures
aurel
parents: 4159
diff changeset
96 void av_metadata_free(AVMetadata **pm)
6a7275621c02 free all allocated metadata structures
aurel
parents: 4159
diff changeset
97 {
6a7275621c02 free all allocated metadata structures
aurel
parents: 4159
diff changeset
98 AVMetadata *m= *pm;
6a7275621c02 free all allocated metadata structures
aurel
parents: 4159
diff changeset
99
6a7275621c02 free all allocated metadata structures
aurel
parents: 4159
diff changeset
100 if(m){
6a7275621c02 free all allocated metadata structures
aurel
parents: 4159
diff changeset
101 while(m->count--){
6a7275621c02 free all allocated metadata structures
aurel
parents: 4159
diff changeset
102 av_free(m->elems[m->count].key);
6a7275621c02 free all allocated metadata structures
aurel
parents: 4159
diff changeset
103 av_free(m->elems[m->count].value);
6a7275621c02 free all allocated metadata structures
aurel
parents: 4159
diff changeset
104 }
6a7275621c02 free all allocated metadata structures
aurel
parents: 4159
diff changeset
105 av_free(m->elems);
6a7275621c02 free all allocated metadata structures
aurel
parents: 4159
diff changeset
106 }
6a7275621c02 free all allocated metadata structures
aurel
parents: 4159
diff changeset
107 av_freep(pm);
6a7275621c02 free all allocated metadata structures
aurel
parents: 4159
diff changeset
108 }
4617
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
109
5661
6d530eb42996 Set lavf identification string globally in av_write_header(), rather
rbultje
parents: 5445
diff changeset
110 void metadata_conv(AVMetadata **pm, const AVMetadataConv *d_conv,
4617
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
111 const AVMetadataConv *s_conv)
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
112 {
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
113 /* TODO: use binary search to look up the two conversion tables
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
114 if the tables are getting big enough that it would matter speed wise */
4621
1874d71f6b19 simplify metadata conversion and fixes gcc-2.95 at the same time
aurel
parents: 4617
diff changeset
115 const AVMetadataConv *sc, *dc;
4617
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
116 AVMetadataTag *mtag = NULL;
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
117 AVMetadata *dst = NULL;
4621
1874d71f6b19 simplify metadata conversion and fixes gcc-2.95 at the same time
aurel
parents: 4617
diff changeset
118 const char *key;
4617
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
119
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
120 while((mtag=av_metadata_get(*pm, "", mtag, AV_METADATA_IGNORE_SUFFIX))) {
4621
1874d71f6b19 simplify metadata conversion and fixes gcc-2.95 at the same time
aurel
parents: 4617
diff changeset
121 key = mtag->key;
4617
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
122 if (s_conv != d_conv) {
4621
1874d71f6b19 simplify metadata conversion and fixes gcc-2.95 at the same time
aurel
parents: 4617
diff changeset
123 if (s_conv)
1874d71f6b19 simplify metadata conversion and fixes gcc-2.95 at the same time
aurel
parents: 4617
diff changeset
124 for (sc=s_conv; sc->native; sc++)
5308
c6028d6c93b0 fix indentation
bcoudurier
parents: 4621
diff changeset
125 if (!strcasecmp(key, sc->native)) {
c6028d6c93b0 fix indentation
bcoudurier
parents: 4621
diff changeset
126 key = sc->generic;
c6028d6c93b0 fix indentation
bcoudurier
parents: 4621
diff changeset
127 break;
c6028d6c93b0 fix indentation
bcoudurier
parents: 4621
diff changeset
128 }
4621
1874d71f6b19 simplify metadata conversion and fixes gcc-2.95 at the same time
aurel
parents: 4617
diff changeset
129 if (d_conv)
1874d71f6b19 simplify metadata conversion and fixes gcc-2.95 at the same time
aurel
parents: 4617
diff changeset
130 for (dc=d_conv; dc->native; dc++)
1874d71f6b19 simplify metadata conversion and fixes gcc-2.95 at the same time
aurel
parents: 4617
diff changeset
131 if (!strcasecmp(key, dc->generic)) {
5308
c6028d6c93b0 fix indentation
bcoudurier
parents: 4621
diff changeset
132 key = dc->native;
c6028d6c93b0 fix indentation
bcoudurier
parents: 4621
diff changeset
133 break;
c6028d6c93b0 fix indentation
bcoudurier
parents: 4621
diff changeset
134 }
4617
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
135 }
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
136 av_metadata_set(&dst, key, mtag->value);
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
137 }
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
138 av_metadata_free(pm);
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
139 *pm = dst;
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
140 }
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
141
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
142 void av_metadata_conv(AVFormatContext *ctx, const AVMetadataConv *d_conv,
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
143 const AVMetadataConv *s_conv)
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
144 {
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
145 int i;
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
146 metadata_conv(&ctx->metadata, d_conv, s_conv);
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
147 for (i=0; i<ctx->nb_streams ; i++)
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
148 metadata_conv(&ctx->streams [i]->metadata, d_conv, s_conv);
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
149 for (i=0; i<ctx->nb_chapters; i++)
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
150 metadata_conv(&ctx->chapters[i]->metadata, d_conv, s_conv);
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
151 for (i=0; i<ctx->nb_programs; i++)
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
152 metadata_conv(&ctx->programs[i]->metadata, d_conv, s_conv);
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
153 }