1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
/* This file is part of the HandBrake source code.
Homepage: <http://handbrake.fr/>.
It may be used under the terms of the GNU General Public License. */
#include "hb.h"
#include "hb_dict.h"
hb_dict_t * hb_dict_init( int alloc )
{
hb_dict_t * dict = NULL;
dict = malloc( sizeof( hb_dict_t ) );
if( !dict )
{
hb_log( "ERROR: could not allocate hb_dict_t" );
return NULL;
}
dict->count = 0;
dict->objects = malloc( alloc * sizeof( hb_dict_entry_t ) );
if( !dict->objects )
{
hb_log( "ERROR: could not allocate hb_dict_t objects" );
dict->alloc = 0;
}
else
{
dict->alloc = alloc;
}
return dict;
}
void hb_dict_free( hb_dict_t ** dict_ptr )
{
hb_dict_t * dict = *dict_ptr;
if( dict )
{
if( dict->objects )
{
int i;
for( i = 0; i < dict->count; i++ )
{
if( dict->objects[i].key )
{
free( dict->objects[i].key );
}
if( dict->objects[i].value )
{
free( dict->objects[i].value );
}
}
free( dict->objects );
}
free( *dict_ptr );
*dict_ptr = NULL;
}
}
void hb_dict_set( hb_dict_t ** dict_ptr, const char * key, const char * value )
{
hb_dict_t * dict = *dict_ptr;
if( !dict )
{
hb_log( "hb_dict_set: NULL dictionary" );
return;
}
if( !key || !strlen( key ) )
return;
hb_dict_entry_t * entry = hb_dict_get( dict, key );
if( entry )
{
if( entry->value )
{
if( value && !strcmp( value, entry->value ) )
return;
else
{
free( entry->value );
entry->value = NULL;
}
}
if( value && strlen( value ) )
entry->value = strdup( value );
}
else
{
if( dict->alloc <= dict->count )
{
hb_dict_entry_t * tmp = NULL;
tmp = malloc( ( 2 * dict->alloc ) * sizeof( hb_dict_entry_t ) );
if( !tmp )
{
hb_log( "ERROR: could not realloc hb_dict_t objects" );
return;
}
if( dict->objects )
{
if( dict->count )
memcpy( tmp, dict->objects, dict->count * sizeof( hb_dict_entry_t ) );
free( dict->objects );
}
dict->objects = tmp;
dict->alloc *= 2;
}
dict->objects[dict->count].key = strdup( key );
if( value && strlen( value ) )
dict->objects[dict->count].value = strdup( value );
else
dict->objects[dict->count].value = NULL;
dict->count++;
}
}
void hb_dict_unset( hb_dict_t ** dict_ptr, const char * key )
{
hb_dict_t * dict = *dict_ptr;
if( !dict || !dict->objects || !key || !strlen( key ) )
return;
int i;
for( i = 0; i < dict->count; i++ )
if( !strcmp( key, dict->objects[i].key ) )
{
free( dict->objects[i].key );
if( dict->objects[i].value )
free( dict->objects[i].value );
if( i != --dict->count )
memmove( &dict->objects[i], &dict->objects[i+1],
sizeof( hb_dict_entry_t ) * ( dict->count - i ) );
}
}
hb_dict_entry_t * hb_dict_get( hb_dict_t * dict, const char * key )
{
if( !dict || !dict->objects || !key || !strlen( key ) )
return NULL;
int i;
for( i = 0; i < dict->count; i++ )
if( !strcmp( key, dict->objects[i].key ) )
return &dict->objects[i];
return NULL;
}
hb_dict_entry_t * hb_dict_next( hb_dict_t * dict, hb_dict_entry_t * previous )
{
if( !dict || !dict->objects )
return NULL;
if( !previous )
return &dict->objects[0];
unsigned int prev_index = previous - dict->objects;
if( prev_index + 1 < dict->count )
return &dict->objects[prev_index+1];
return NULL;
}
hb_dict_t * hb_encopts_to_dict( const char * encopts, int encoder )
{
hb_dict_t * dict = NULL;
if( encopts && *encopts )
{
char *cur_opt, *opts_start, *value;
const char *name;
dict = hb_dict_init( 10 );
if( !dict )
return NULL;
cur_opt = opts_start = strdup( encopts );
if( opts_start )
{
while( *cur_opt )
{
name = cur_opt;
cur_opt += strcspn( cur_opt, ":" );
if( *cur_opt )
{
*cur_opt = 0;
cur_opt++;
}
value = strchr( name, '=' );
if( value )
{
*value = 0;
value++;
}
// x264 has multiple names for some options
if( encoder == HB_VCODEC_X264 )
name = hb_x264_encopt_name( name );
hb_dict_set( &dict, name, value );
}
}
free( opts_start );
}
return dict;
}
char * hb_dict_to_encopts( hb_dict_t * dict )
{
int first_opt = 1;
char *tmp, *encopts_tmp, *encopts = NULL;
hb_dict_entry_t * entry = NULL;
while( ( entry = hb_dict_next( dict, entry ) ) )
{
tmp = hb_strdup_printf( "%s%s%s%s",
first_opt ? "" : ":",
entry->key,
entry->value ? "=" : "",
entry->value ? entry->value : "" );
if( tmp )
{
encopts_tmp = hb_strncat_dup( encopts, tmp, strlen( tmp ) );
if( encopts_tmp )
{
if( encopts )
free( encopts );
encopts = encopts_tmp;
}
first_opt = 0;
free( tmp );
}
}
return encopts;
}
|