AVPKit
MetaData.cpp
1 /*******************************************************************************
2  * Copyright (c) 2024, 2026, Olivier Ayache. All rights reserved.
3  *
4  * This file is part of AVPKit.
5  *
6  * AVPKit is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * AVPKit is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with AVPKit. If not, see <http://www.gnu.org/licenses/>.
18  *******************************************************************************/
19 /*
20  * MetaData.cpp
21  *
22  * Created on: Jun 29, 2009
23  * Author: aclarke
24  */
25 
26 #include "MetaData.h"
27 
28 namespace com
29 {
30 
31 namespace avpkit
32 {
33 
34 namespace core
35 {
36 
37 MetaData :: MetaData()
38 {
39  mLocalMeta = 0;
40  mMetaData = &mLocalMeta;
41 }
42 
43 MetaData :: ~MetaData()
44 {
45  if (mMetaData && *mMetaData)
46  av_dict_free(mMetaData);
47 }
48 
49 int32_t
51 {
52  if (!mMetaData || !*mMetaData)
53  return 0;
54 
55  AVDictionaryEntry* tag=0;
56  int32_t retval=0;
57  do
58  {
59  tag = av_dict_get(*mMetaData, "", tag, AV_DICT_IGNORE_SUFFIX);
60  if (tag)
61  retval++;
62  } while(tag);
63  return retval;
64 }
65 
66 const char*
67 MetaData :: getKey(int32_t index)
68 {
69  if (!mMetaData || !*mMetaData || index < 0)
70  return 0;
71 
72  AVDictionaryEntry* tag=0;
73  int32_t position=-1;
74  do
75  {
76  tag = av_dict_get(*mMetaData, "", tag, AV_DICT_IGNORE_SUFFIX);
77  if (tag) {
78  position++;
79  if (position == index)
80  return tag->key;
81  }
82  } while(tag);
83  return 0;
84 }
85 const char*
86 MetaData :: getValue(const char *key, Flags flag)
87 {
88  if (!mMetaData || !*mMetaData || !key || !*key)
89  return 0;
90  AVDictionaryEntry* tag = av_dict_get(*mMetaData, key, 0, (int)flag);
91  if (tag)
92  return tag->value;
93  else
94  return 0;
95 }
96 
97 int32_t
98 MetaData :: setValue(const char* key, const char* value)
99 {
100  return setValue(key, value, METADATA_NONE);
101 }
102 
103 int32_t
104 MetaData :: setValue(const char* key, const char* value, Flags flag)
105 {
106  if (!key || !*key || !mMetaData)
107  return -1;
108  return (int32_t)av_dict_set(mMetaData, key, value, (int)flag);
109 }
110 
111 MetaData*
112 MetaData :: make(AVDictionary** metaToUse)
113 {
114  if (!metaToUse)
115  return 0;
116 
117  MetaData* retval = make();
118 
119  if (retval)
120  retval->mMetaData = metaToUse;
121 
122  return retval;
123 }
124 
125 MetaData*
126 MetaData :: make(AVDictionary* metaDataToCopy)
127 {
128  MetaData* retval = make();
129  if (retval && metaDataToCopy)
130  {
131  AVDictionaryEntry* tag = 0;
132  do {
133  tag = av_dict_get(metaDataToCopy, "", tag,
134  AV_DICT_IGNORE_SUFFIX);
135  if (tag)
136  if (av_dict_set(retval->mMetaData, tag->key, tag->value,0) < 0)
137  {
138  VS_REF_RELEASE(retval);
139  break;
140  }
141  } while(tag);
142  }
143  return retval;
144 }
145 
146 int32_t
147 MetaData :: copy(AVDictionary *data)
148 {
149  if (!data)
150  return -1;
151 
152  if (mMetaData) {
153  if (data == *mMetaData)
154  // copy the current data; just return
155  return 0;
156  av_dict_free(mMetaData);
157  *mMetaData = 0;
158  }
159  av_dict_copy(mMetaData, data, 0);
160  return 0;
161 }
162 
163 int32_t
165 {
166  MetaData* data = dynamic_cast<MetaData*>(dataToCopy);
167  if (!data)
168  return -1;
169 
170  return copy(data->getDictionary());
171 }
172 }}}
Get MetaData about a IContainer or IStream.
Definition: IMetaData.h:51
Flags
Different types of flags that can be passed to IMetaData#getValue.
Definition: IMetaData.h:56
@ METADATA_NONE
For getValue(String) case-insensitive match of key.
Definition: IMetaData.h:60
static IMetaData * make()
Create a new IMetaData bag of properties with no values set.
Definition: IMetaData.cpp:36
int32_t copy(IMetaData *copy)
Destroys the current data, and copies all data from copy.
Definition: MetaData.cpp:164
virtual const char * getKey(int32_t position)
Get the key at the given position, or null if no such key at that position.
Definition: MetaData.cpp:67
AVDictionary * getDictionary()
Returns the AVDictionary for passing to FFmpeg calls.
Definition: MetaData.h:76
virtual const char * getValue(const char *key, Flags flag)
Get the value for the given key.
Definition: MetaData.cpp:86
virtual int32_t getNumKeys()
Get the total number of keys currently in this IMetaData object.
Definition: MetaData.cpp:50
virtual int32_t setValue(const char *key, const char *value)
Sets the value for the given key to value.
Definition: MetaData.cpp:98
WARNING: Do not use logging in this class, and do not set any static file variables to values other t...