annotate metadata.c @ 6105:c0336d3528e9 libavformat

matroska: move LFG state from muxer context to local variable The LFG is only used in one place, so there is no need to keep it in the context.
author mru
date Tue, 08 Jun 2010 10:44:27 +0000
parents 5ec1f78c30fe
children 2d51ca7714c3
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
6082
140e0b5fcea2 metadata: make conversion to the same format a noop.
mstorsjo
parents: 5982
diff changeset
120 if (d_conv == s_conv)
140e0b5fcea2 metadata: make conversion to the same format a noop.
mstorsjo
parents: 5982
diff changeset
121 return;
140e0b5fcea2 metadata: make conversion to the same format a noop.
mstorsjo
parents: 5982
diff changeset
122
4617
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
123 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
124 key = mtag->key;
6083
5ec1f78c30fe reindent after previous commit.
mstorsjo
parents: 6082
diff changeset
125 if (s_conv)
5ec1f78c30fe reindent after previous commit.
mstorsjo
parents: 6082
diff changeset
126 for (sc=s_conv; sc->native; sc++)
5ec1f78c30fe reindent after previous commit.
mstorsjo
parents: 6082
diff changeset
127 if (!strcasecmp(key, sc->native)) {
5ec1f78c30fe reindent after previous commit.
mstorsjo
parents: 6082
diff changeset
128 key = sc->generic;
5ec1f78c30fe reindent after previous commit.
mstorsjo
parents: 6082
diff changeset
129 break;
5ec1f78c30fe reindent after previous commit.
mstorsjo
parents: 6082
diff changeset
130 }
5ec1f78c30fe reindent after previous commit.
mstorsjo
parents: 6082
diff changeset
131 if (d_conv)
5ec1f78c30fe reindent after previous commit.
mstorsjo
parents: 6082
diff changeset
132 for (dc=d_conv; dc->native; dc++)
5ec1f78c30fe reindent after previous commit.
mstorsjo
parents: 6082
diff changeset
133 if (!strcasecmp(key, dc->generic)) {
5ec1f78c30fe reindent after previous commit.
mstorsjo
parents: 6082
diff changeset
134 key = dc->native;
5ec1f78c30fe reindent after previous commit.
mstorsjo
parents: 6082
diff changeset
135 break;
5ec1f78c30fe reindent after previous commit.
mstorsjo
parents: 6082
diff changeset
136 }
5982
f74198942337 Mark av_metadata_set() as deprecated, and use av_metadata_set2()
stefano
parents: 5938
diff changeset
137 av_metadata_set2(&dst, key, mtag->value, 0);
4617
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
138 }
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
139 av_metadata_free(pm);
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
140 *pm = dst;
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
141 }
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
142
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
143 void av_metadata_conv(AVFormatContext *ctx, const AVMetadataConv *d_conv,
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
144 const AVMetadataConv *s_conv)
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
145 {
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
146 int i;
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
147 metadata_conv(&ctx->metadata, d_conv, s_conv);
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
148 for (i=0; i<ctx->nb_streams ; i++)
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
149 metadata_conv(&ctx->streams [i]->metadata, d_conv, s_conv);
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
150 for (i=0; i<ctx->nb_chapters; i++)
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
151 metadata_conv(&ctx->chapters[i]->metadata, d_conv, s_conv);
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
152 for (i=0; i<ctx->nb_programs; i++)
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
153 metadata_conv(&ctx->programs[i]->metadata, d_conv, s_conv);
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
154 }