annotate metadata.c @ 5704:6b9c2a6d8fa4 libavformat

Introduce metadata conversion table for NUT muxer and demuxer. Patch by Anton Khirnov (wyskas, do no evil mail) Thread "[PATCH] nut metadata conversion table"
author kostya
date Wed, 24 Feb 2010 06:27:12 +0000
parents 6d530eb42996
children 854b3ac40b8e
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){
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
58 av_free(tag->value);
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
59 av_free(tag->key);
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
60 *tag= m->elems[--m->count];
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
61 }else{
4154
bd4d3fee45d0 rename AVMetaData to AVMetadata and meta_data to metadata
aurel
parents: 4150
diff changeset
62 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
63 if(tmp){
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
64 m->elems= tmp;
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
65 }else
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
66 return AVERROR(ENOMEM);
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
67 }
4353
e4a1b568b313 modify the way to pass parameters to av_metadata_set()
aurel
parents: 4250
diff changeset
68 if(value){
5445
d1b5bb2bfe92 Add flags to prevent strdup() on arguments to av_metadata_set2().
michael
parents: 5308
diff changeset
69 if(flags & AV_METADATA_DONT_STRDUP_KEY){
d1b5bb2bfe92 Add flags to prevent strdup() on arguments to av_metadata_set2().
michael
parents: 5308
diff changeset
70 m->elems[m->count].key = key;
d1b5bb2bfe92 Add flags to prevent strdup() on arguments to av_metadata_set2().
michael
parents: 5308
diff changeset
71 }else
4353
e4a1b568b313 modify the way to pass parameters to av_metadata_set()
aurel
parents: 4250
diff changeset
72 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
73 if(flags & AV_METADATA_DONT_STRDUP_VAL){
d1b5bb2bfe92 Add flags to prevent strdup() on arguments to av_metadata_set2().
michael
parents: 5308
diff changeset
74 m->elems[m->count].value= value;
d1b5bb2bfe92 Add flags to prevent strdup() on arguments to av_metadata_set2().
michael
parents: 5308
diff changeset
75 }else
4353
e4a1b568b313 modify the way to pass parameters to av_metadata_set()
aurel
parents: 4250
diff changeset
76 m->elems[m->count].value= av_strdup(value);
e4a1b568b313 modify the way to pass parameters to av_metadata_set()
aurel
parents: 4250
diff changeset
77 m->count++;
4150
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
78 }
4591
e62a61d3e591 fix a memleak in av_metadata_set()
aurel
parents: 4353
diff changeset
79 if(!m->count) {
e62a61d3e591 fix a memleak in av_metadata_set()
aurel
parents: 4353
diff changeset
80 av_free(m->elems);
4150
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
81 av_freep(pm);
4591
e62a61d3e591 fix a memleak in av_metadata_set()
aurel
parents: 4353
diff changeset
82 }
4150
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
83
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
84 return 0;
6f4c0d5f1e23 Move metadata API from avcodec to avformat.
michael
parents:
diff changeset
85 }
4158
ea1e24175669 Add a metadata compatibility layer, so that when a user application set
aurel
parents: 4157
diff changeset
86
5445
d1b5bb2bfe92 Add flags to prevent strdup() on arguments to av_metadata_set2().
michael
parents: 5308
diff changeset
87 #if LIBAVFORMAT_VERSION_MAJOR == 52
d1b5bb2bfe92 Add flags to prevent strdup() on arguments to av_metadata_set2().
michael
parents: 5308
diff changeset
88 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
89 {
d1b5bb2bfe92 Add flags to prevent strdup() on arguments to av_metadata_set2().
michael
parents: 5308
diff changeset
90 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
91 }
d1b5bb2bfe92 Add flags to prevent strdup() on arguments to av_metadata_set2().
michael
parents: 5308
diff changeset
92 #endif
d1b5bb2bfe92 Add flags to prevent strdup() on arguments to av_metadata_set2().
michael
parents: 5308
diff changeset
93
4161
6a7275621c02 free all allocated metadata structures
aurel
parents: 4159
diff changeset
94 void av_metadata_free(AVMetadata **pm)
6a7275621c02 free all allocated metadata structures
aurel
parents: 4159
diff changeset
95 {
6a7275621c02 free all allocated metadata structures
aurel
parents: 4159
diff changeset
96 AVMetadata *m= *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 if(m){
6a7275621c02 free all allocated metadata structures
aurel
parents: 4159
diff changeset
99 while(m->count--){
6a7275621c02 free all allocated metadata structures
aurel
parents: 4159
diff changeset
100 av_free(m->elems[m->count].key);
6a7275621c02 free all allocated metadata structures
aurel
parents: 4159
diff changeset
101 av_free(m->elems[m->count].value);
6a7275621c02 free all allocated metadata structures
aurel
parents: 4159
diff changeset
102 }
6a7275621c02 free all allocated metadata structures
aurel
parents: 4159
diff changeset
103 av_free(m->elems);
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_freep(pm);
6a7275621c02 free all allocated metadata structures
aurel
parents: 4159
diff changeset
106 }
4617
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
107
5661
6d530eb42996 Set lavf identification string globally in av_write_header(), rather
rbultje
parents: 5445
diff changeset
108 void metadata_conv(AVMetadata **pm, const AVMetadataConv *d_conv,
4617
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
109 const AVMetadataConv *s_conv)
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
110 {
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
111 /* TODO: use binary search to look up the two conversion tables
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
112 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
113 const AVMetadataConv *sc, *dc;
4617
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
114 AVMetadataTag *mtag = NULL;
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
115 AVMetadata *dst = NULL;
4621
1874d71f6b19 simplify metadata conversion and fixes gcc-2.95 at the same time
aurel
parents: 4617
diff changeset
116 const char *key;
4617
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
117
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
118 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
119 key = mtag->key;
4617
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
120 if (s_conv != d_conv) {
4621
1874d71f6b19 simplify metadata conversion and fixes gcc-2.95 at the same time
aurel
parents: 4617
diff changeset
121 if (s_conv)
1874d71f6b19 simplify metadata conversion and fixes gcc-2.95 at the same time
aurel
parents: 4617
diff changeset
122 for (sc=s_conv; sc->native; sc++)
5308
c6028d6c93b0 fix indentation
bcoudurier
parents: 4621
diff changeset
123 if (!strcasecmp(key, sc->native)) {
c6028d6c93b0 fix indentation
bcoudurier
parents: 4621
diff changeset
124 key = sc->generic;
c6028d6c93b0 fix indentation
bcoudurier
parents: 4621
diff changeset
125 break;
c6028d6c93b0 fix indentation
bcoudurier
parents: 4621
diff changeset
126 }
4621
1874d71f6b19 simplify metadata conversion and fixes gcc-2.95 at the same time
aurel
parents: 4617
diff changeset
127 if (d_conv)
1874d71f6b19 simplify metadata conversion and fixes gcc-2.95 at the same time
aurel
parents: 4617
diff changeset
128 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
129 if (!strcasecmp(key, dc->generic)) {
5308
c6028d6c93b0 fix indentation
bcoudurier
parents: 4621
diff changeset
130 key = dc->native;
c6028d6c93b0 fix indentation
bcoudurier
parents: 4621
diff changeset
131 break;
c6028d6c93b0 fix indentation
bcoudurier
parents: 4621
diff changeset
132 }
4617
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
133 }
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
134 av_metadata_set(&dst, key, mtag->value);
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_free(pm);
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
137 *pm = dst;
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
138 }
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
139
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
140 void av_metadata_conv(AVFormatContext *ctx, const AVMetadataConv *d_conv,
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
141 const AVMetadataConv *s_conv)
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
142 {
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
143 int i;
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
144 metadata_conv(&ctx->metadata, d_conv, s_conv);
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
145 for (i=0; i<ctx->nb_streams ; i++)
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
146 metadata_conv(&ctx->streams [i]->metadata, d_conv, s_conv);
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
147 for (i=0; i<ctx->nb_chapters; i++)
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
148 metadata_conv(&ctx->chapters[i]->metadata, d_conv, s_conv);
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
149 for (i=0; i<ctx->nb_programs; i++)
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
150 metadata_conv(&ctx->programs[i]->metadata, d_conv, s_conv);
f04bc8b70940 add a metadata conversion API
aurel
parents: 4591
diff changeset
151 }