FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/utils.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 194 328 59.1%
Functions: 20 27 74.1%
Branches: 111 263 42.2%

Line Branch Exec Source
1 /*
2 * various utility functions for use within FFmpeg
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <stdint.h>
23
24 #include "config.h"
25
26 #include "libavutil/avstring.h"
27 #include "libavutil/bprint.h"
28 #include "libavutil/internal.h"
29 #include "libavutil/thread.h"
30 #include "libavutil/time.h"
31
32 #include "libavcodec/internal.h"
33
34 #include "avformat.h"
35 #include "avio_internal.h"
36 #include "internal.h"
37 #if CONFIG_NETWORK
38 #include "network.h"
39 #endif
40 #include "os_support.h"
41
42 static AVMutex avformat_mutex = AV_MUTEX_INITIALIZER;
43
44 /**
45 * @file
46 * various utility functions for use within FFmpeg
47 */
48
49 int ff_lock_avformat(void)
50 {
51 return ff_mutex_lock(&avformat_mutex) ? -1 : 0;
52 }
53
54 int ff_unlock_avformat(void)
55 {
56 return ff_mutex_unlock(&avformat_mutex) ? -1 : 0;
57 }
58
59 /* an arbitrarily chosen "sane" max packet size -- 50M */
60 #define SANE_CHUNK_SIZE (50000000)
61
62 /* Read the data in sane-sized chunks and append to pkt.
63 * Return the number of bytes read or an error. */
64 313431 static int append_packet_chunked(AVIOContext *s, AVPacket *pkt, int size)
65 {
66 313431 int orig_size = pkt->size;
67 int ret;
68
69 do {
70 313431 int prev_size = pkt->size;
71 int read_size;
72
73 /* When the caller requests a lot of data, limit it to the amount
74 * left in file or SANE_CHUNK_SIZE when it is not known. */
75 313431 read_size = size;
76
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 313431 times.
313431 if (read_size > SANE_CHUNK_SIZE/10) {
77 read_size = ffio_limit(s, read_size);
78 // If filesize/maxsize is unknown, limit to SANE_CHUNK_SIZE
79 if (ffiocontext(s)->maxsize < 0)
80 read_size = FFMIN(read_size, SANE_CHUNK_SIZE);
81 }
82
83 313431 ret = av_grow_packet(pkt, read_size);
84
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 313431 times.
313431 if (ret < 0)
85 break;
86
87 313431 ret = avio_read(s, pkt->data + prev_size, read_size);
88
2/2
✓ Branch 0 taken 855 times.
✓ Branch 1 taken 312576 times.
313431 if (ret != read_size) {
89 855 av_shrink_packet(pkt, prev_size + FFMAX(ret, 0));
90 855 break;
91 }
92
93 312576 size -= read_size;
94
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 312576 times.
312576 } while (size > 0);
95
2/2
✓ Branch 0 taken 792 times.
✓ Branch 1 taken 312639 times.
313431 if (size > 0)
96 792 pkt->flags |= AV_PKT_FLAG_CORRUPT;
97
98
2/2
✓ Branch 0 taken 2200 times.
✓ Branch 1 taken 311231 times.
313431 if (!pkt->size)
99 2200 av_packet_unref(pkt);
100
2/2
✓ Branch 0 taken 311226 times.
✓ Branch 1 taken 2205 times.
313431 return pkt->size > orig_size ? pkt->size - orig_size : ret;
101 }
102
103 302130 int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
104 {
105 #if FF_API_INIT_PACKET
106 FF_DISABLE_DEPRECATION_WARNINGS
107 302130 av_init_packet(pkt);
108 302130 pkt->data = NULL;
109 302130 pkt->size = 0;
110 FF_ENABLE_DEPRECATION_WARNINGS
111 #else
112 av_packet_unref(pkt);
113 #endif
114 302130 pkt->pos = avio_tell(s);
115
116 302130 return append_packet_chunked(s, pkt, size);
117 }
118
119 11654 int av_append_packet(AVIOContext *s, AVPacket *pkt, int size)
120 {
121
2/2
✓ Branch 0 taken 353 times.
✓ Branch 1 taken 11301 times.
11654 if (!pkt->size)
122 353 return av_get_packet(s, pkt, size);
123 11301 return append_packet_chunked(s, pkt, size);
124 }
125
126 984 int av_filename_number_test(const char *filename)
127 {
128 char buf[1024];
129
3/4
✓ Branch 0 taken 984 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 297 times.
✓ Branch 3 taken 687 times.
1968 return filename &&
130 984 (av_get_frame_filename(buf, sizeof(buf), filename, 1) >= 0);
131 }
132
133 /**********************************************************/
134
135 1109 unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
136 {
137
2/2
✓ Branch 0 taken 7619 times.
✓ Branch 1 taken 2 times.
7621 while (tags->id != AV_CODEC_ID_NONE) {
138
2/2
✓ Branch 0 taken 1107 times.
✓ Branch 1 taken 6512 times.
7619 if (tags->id == id)
139 1107 return tags->tag;
140 6512 tags++;
141 }
142 2 return 0;
143 }
144
145 2835 enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
146 {
147
2/2
✓ Branch 0 taken 241704 times.
✓ Branch 1 taken 794 times.
242498 for (int i = 0; tags[i].id != AV_CODEC_ID_NONE; i++)
148
2/2
✓ Branch 0 taken 2041 times.
✓ Branch 1 taken 239663 times.
241704 if (tag == tags[i].tag)
149 2041 return tags[i].id;
150
2/2
✓ Branch 0 taken 71789 times.
✓ Branch 1 taken 789 times.
72578 for (int i = 0; tags[i].id != AV_CODEC_ID_NONE; i++)
151
2/2
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 71784 times.
71789 if (ff_toupper4(tag) == ff_toupper4(tags[i].tag))
152 5 return tags[i].id;
153 789 return AV_CODEC_ID_NONE;
154 }
155
156 549 enum AVCodecID ff_get_pcm_codec_id(int bps, int flt, int be, int sflags)
157 {
158
2/4
✓ Branch 0 taken 549 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 549 times.
549 if (bps <= 0 || bps > 64)
159 return AV_CODEC_ID_NONE;
160
161
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 545 times.
549 if (flt) {
162
2/3
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 switch (bps) {
163 2 case 32:
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 return be ? AV_CODEC_ID_PCM_F32BE : AV_CODEC_ID_PCM_F32LE;
165 2 case 64:
166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 return be ? AV_CODEC_ID_PCM_F64BE : AV_CODEC_ID_PCM_F64LE;
167 default:
168 return AV_CODEC_ID_NONE;
169 }
170 } else {
171 545 bps += 7;
172 545 bps >>= 3;
173
2/2
✓ Branch 0 taken 527 times.
✓ Branch 1 taken 18 times.
545 if (sflags & (1 << (bps - 1))) {
174
3/6
✗ Branch 0 not taken.
✓ Branch 1 taken 512 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
527 switch (bps) {
175 case 1:
176 return AV_CODEC_ID_PCM_S8;
177 512 case 2:
178
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 510 times.
512 return be ? AV_CODEC_ID_PCM_S16BE : AV_CODEC_ID_PCM_S16LE;
179 12 case 3:
180
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 11 times.
12 return be ? AV_CODEC_ID_PCM_S24BE : AV_CODEC_ID_PCM_S24LE;
181 3 case 4:
182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 return be ? AV_CODEC_ID_PCM_S32BE : AV_CODEC_ID_PCM_S32LE;
183 case 8:
184 return be ? AV_CODEC_ID_PCM_S64BE : AV_CODEC_ID_PCM_S64LE;
185 default:
186 return AV_CODEC_ID_NONE;
187 }
188 } else {
189
1/5
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
18 switch (bps) {
190 18 case 1:
191 18 return AV_CODEC_ID_PCM_U8;
192 case 2:
193 return be ? AV_CODEC_ID_PCM_U16BE : AV_CODEC_ID_PCM_U16LE;
194 case 3:
195 return be ? AV_CODEC_ID_PCM_U24BE : AV_CODEC_ID_PCM_U24LE;
196 case 4:
197 return be ? AV_CODEC_ID_PCM_U32BE : AV_CODEC_ID_PCM_U32LE;
198 default:
199 return AV_CODEC_ID_NONE;
200 }
201 }
202 }
203 }
204
205 5413 unsigned int av_codec_get_tag(const AVCodecTag *const *tags, enum AVCodecID id)
206 {
207 unsigned int tag;
208
2/2
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 5402 times.
5413 if (!av_codec_get_tag2(tags, id, &tag))
209 11 return 0;
210 5402 return tag;
211 }
212
213 8352 int av_codec_get_tag2(const AVCodecTag * const *tags, enum AVCodecID id,
214 unsigned int *tag)
215 {
216
3/4
✓ Branch 0 taken 15674 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15648 times.
✓ Branch 3 taken 26 times.
15674 for (int i = 0; tags && tags[i]; i++) {
217 15648 const AVCodecTag *codec_tags = tags[i];
218
2/2
✓ Branch 0 taken 661182 times.
✓ Branch 1 taken 7322 times.
668504 while (codec_tags->id != AV_CODEC_ID_NONE) {
219
2/2
✓ Branch 0 taken 8326 times.
✓ Branch 1 taken 652856 times.
661182 if (codec_tags->id == id) {
220 8326 *tag = codec_tags->tag;
221 8326 return 1;
222 }
223 652856 codec_tags++;
224 }
225 }
226 26 return 0;
227 }
228
229 164 enum AVCodecID av_codec_get_id(const AVCodecTag *const *tags, unsigned int tag)
230 {
231
3/4
✓ Branch 0 taken 297 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 244 times.
✓ Branch 3 taken 53 times.
297 for (int i = 0; tags && tags[i]; i++) {
232 244 enum AVCodecID id = ff_codec_get_id(tags[i], tag);
233
2/2
✓ Branch 0 taken 111 times.
✓ Branch 1 taken 133 times.
244 if (id != AV_CODEC_ID_NONE)
234 111 return id;
235 }
236 53 return AV_CODEC_ID_NONE;
237 }
238
239 832 int ff_alloc_extradata(AVCodecParameters *par, int size)
240 {
241 832 av_freep(&par->extradata);
242 832 par->extradata_size = 0;
243
244
2/4
✓ Branch 0 taken 832 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 832 times.
832 if (size < 0 || size >= INT32_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
245 return AVERROR(EINVAL);
246
247 832 par->extradata = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
248
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 832 times.
832 if (!par->extradata)
249 return AVERROR(ENOMEM);
250
251 832 memset(par->extradata + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
252 832 par->extradata_size = size;
253
254 832 return 0;
255 }
256
257 /*******************************************************/
258
259 66 uint64_t ff_ntp_time(void)
260 {
261 66 return (av_gettime() / 1000) * 1000 + NTP_OFFSET_US;
262 }
263
264 uint64_t ff_get_formatted_ntp_time(uint64_t ntp_time_us)
265 {
266 uint64_t ntp_ts, frac_part, sec;
267 uint32_t usec;
268
269 //current ntp time in seconds and micro seconds
270 sec = ntp_time_us / 1000000;
271 usec = ntp_time_us % 1000000;
272
273 //encoding in ntp timestamp format
274 frac_part = usec * 0xFFFFFFFFULL;
275 frac_part /= 1000000;
276
277 if (sec > 0xFFFFFFFFULL)
278 av_log(NULL, AV_LOG_WARNING, "NTP time format roll over detected\n");
279
280 ntp_ts = sec << 32;
281 ntp_ts |= frac_part;
282
283 return ntp_ts;
284 }
285
286 uint64_t ff_parse_ntp_time(uint64_t ntp_ts)
287 {
288 uint64_t sec = ntp_ts >> 32;
289 uint64_t frac_part = ntp_ts & 0xFFFFFFFFULL;
290 uint64_t usec = (frac_part * 1000000) / 0xFFFFFFFFULL;
291
292 return (sec * 1000000) + usec;
293 }
294
295 135836 int av_get_frame_filename2(char *buf, int buf_size, const char *path, int number, int flags)
296 {
297 const char *p;
298 char *q, buf1[20], c;
299 int nd, len, percentd_found;
300
301 135836 q = buf;
302 135836 p = path;
303 135836 percentd_found = 0;
304 for (;;) {
305 10357283 c = *p++;
306
2/2
✓ Branch 0 taken 135836 times.
✓ Branch 1 taken 10221447 times.
10357283 if (c == '\0')
307 135836 break;
308
2/2
✓ Branch 0 taken 135134 times.
✓ Branch 1 taken 10086313 times.
10221447 if (c == '%') {
309 do {
310 135134 nd = 0;
311
2/2
✓ Branch 0 taken 270156 times.
✓ Branch 1 taken 135134 times.
405290 while (av_isdigit(*p)) {
312
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 270156 times.
270156 if (nd >= INT_MAX / 10 - 255)
313 goto fail;
314 270156 nd = nd * 10 + *p++ - '0';
315 }
316 135134 c = *p++;
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 135134 times.
135134 } while (av_isdigit(c));
318
319
1/3
✗ Branch 0 not taken.
✓ Branch 1 taken 135134 times.
✗ Branch 2 not taken.
135134 switch (c) {
320 case '%':
321 goto addchar;
322 135134 case 'd':
323
3/4
✓ Branch 0 taken 134471 times.
✓ Branch 1 taken 663 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 134471 times.
135134 if (!(flags & AV_FRAME_FILENAME_FLAGS_MULTIPLE) && percentd_found)
324 goto fail;
325 135134 percentd_found = 1;
326
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 135134 times.
135134 if (number < 0)
327 nd += 1;
328 135134 snprintf(buf1, sizeof(buf1), "%0*d", nd, number);
329 135134 len = strlen(buf1);
330
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 135134 times.
135134 if ((q - buf + len) > buf_size - 1)
331 goto fail;
332 135134 memcpy(q, buf1, len);
333 135134 q += len;
334 135134 break;
335 default:
336 goto fail;
337 }
338 } else {
339 10086313 addchar:
340
1/2
✓ Branch 0 taken 10086313 times.
✗ Branch 1 not taken.
10086313 if ((q - buf) < buf_size - 1)
341 10086313 *q++ = c;
342 }
343 }
344
2/2
✓ Branch 0 taken 702 times.
✓ Branch 1 taken 135134 times.
135836 if (!percentd_found)
345 702 goto fail;
346 135134 *q = '\0';
347 135134 return 0;
348 702 fail:
349 702 *q = '\0';
350 702 return -1;
351 }
352
353 135166 int av_get_frame_filename(char *buf, int buf_size, const char *path, int number)
354 {
355 135166 return av_get_frame_filename2(buf, buf_size, path, number, 0);
356 }
357
358 9 void av_url_split(char *proto, int proto_size,
359 char *authorization, int authorization_size,
360 char *hostname, int hostname_size,
361 int *port_ptr, char *path, int path_size, const char *url)
362 {
363 const char *p, *ls, *at, *at2, *col, *brk;
364
365
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (port_ptr)
366 9 *port_ptr = -1;
367
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (proto_size > 0)
368 9 proto[0] = 0;
369
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (authorization_size > 0)
370 9 authorization[0] = 0;
371
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (hostname_size > 0)
372 9 hostname[0] = 0;
373
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (path_size > 0)
374 9 path[0] = 0;
375
376 /* parse protocol */
377
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
9 if ((p = strchr(url, ':'))) {
378 8 av_strlcpy(proto, url, FFMIN(proto_size, p + 1 - url));
379 8 p++; /* skip ':' */
380
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (*p == '/')
381 8 p++;
382
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (*p == '/')
383 8 p++;
384 } else {
385 /* no protocol means plain filename */
386 1 av_strlcpy(path, url, path_size);
387 1 return;
388 }
389
390 /* separate path from hostname */
391 8 ls = p + strcspn(p, "/?#");
392 8 av_strlcpy(path, ls, path_size);
393
394 /* the rest is hostname, use that to parse auth/port */
395
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (ls != p) {
396 /* authorization (user[:pass]@hostname) */
397 8 at2 = p;
398
4/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 times.
10 while ((at = strchr(p, '@')) && at < ls) {
399 2 av_strlcpy(authorization, at2,
400 2 FFMIN(authorization_size, at + 1 - at2));
401 2 p = at + 1; /* skip '@' */
402 }
403
404
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
8 if (*p == '[' && (brk = strchr(p, ']')) && brk < ls) {
405 /* [host]:port */
406 av_strlcpy(hostname, p + 1,
407 FFMIN(hostname_size, brk - p));
408 if (brk[1] == ':' && port_ptr)
409 *port_ptr = atoi(brk + 2);
410
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
8 } else if ((col = strchr(p, ':')) && col < ls) {
411 1 av_strlcpy(hostname, p,
412 1 FFMIN(col + 1 - p, hostname_size));
413
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (port_ptr)
414 1 *port_ptr = atoi(col + 1);
415 } else
416 7 av_strlcpy(hostname, p,
417 7 FFMIN(ls + 1 - p, hostname_size));
418 }
419 }
420
421 int ff_mkdir_p(const char *path)
422 {
423 int ret = 0;
424 char *temp = av_strdup(path);
425 char *pos = temp;
426 char tmp_ch = '\0';
427
428 if (!path || !temp) {
429 return -1;
430 }
431
432 if (!av_strncasecmp(temp, "/", 1) || !av_strncasecmp(temp, "\\", 1)) {
433 pos++;
434 } else if (!av_strncasecmp(temp, "./", 2) || !av_strncasecmp(temp, ".\\", 2)) {
435 pos += 2;
436 }
437
438 for ( ; *pos != '\0'; ++pos) {
439 if (*pos == '/' || *pos == '\\') {
440 tmp_ch = *pos;
441 *pos = '\0';
442 ret = mkdir(temp, 0755);
443 *pos = tmp_ch;
444 }
445 }
446
447 if ((*(pos - 1) != '/') && (*(pos - 1) != '\\')) {
448 ret = mkdir(temp, 0755);
449 }
450
451 av_free(temp);
452 return ret;
453 }
454
455 3092 char *ff_data_to_hex(char *buff, const uint8_t *src, int s, int lowercase)
456 {
457 static const char hex_table_uc[16] = { '0', '1', '2', '3',
458 '4', '5', '6', '7',
459 '8', '9', 'A', 'B',
460 'C', 'D', 'E', 'F' };
461 static const char hex_table_lc[16] = { '0', '1', '2', '3',
462 '4', '5', '6', '7',
463 '8', '9', 'a', 'b',
464 'c', 'd', 'e', 'f' };
465
2/2
✓ Branch 0 taken 2395 times.
✓ Branch 1 taken 697 times.
3092 const char *hex_table = lowercase ? hex_table_lc : hex_table_uc;
466
467
2/2
✓ Branch 0 taken 49486 times.
✓ Branch 1 taken 3092 times.
52578 for (int i = 0; i < s; i++) {
468 49486 buff[i * 2] = hex_table[src[i] >> 4];
469 49486 buff[i * 2 + 1] = hex_table[src[i] & 0xF];
470 }
471 3092 buff[2 * s] = '\0';
472
473 3092 return buff;
474 }
475
476 int ff_hex_to_data(uint8_t *data, const char *p)
477 {
478 int c, len, v;
479
480 len = 0;
481 v = 1;
482 for (;;) {
483 p += strspn(p, SPACE_CHARS);
484 if (*p == '\0')
485 break;
486 c = av_toupper((unsigned char) *p++);
487 if (c >= '0' && c <= '9')
488 c = c - '0';
489 else if (c >= 'A' && c <= 'F')
490 c = c - 'A' + 10;
491 else
492 break;
493 v = (v << 4) | c;
494 if (v & 0x100) {
495 if (data)
496 data[len] = v;
497 len++;
498 v = 1;
499 }
500 }
501 return len;
502 }
503
504 void ff_parse_key_value(const char *str, ff_parse_key_val_cb callback_get_buf,
505 void *context)
506 {
507 const char *ptr = str;
508
509 /* Parse key=value pairs. */
510 for (;;) {
511 const char *key;
512 char *dest = NULL, *dest_end;
513 int key_len, dest_len = 0;
514
515 /* Skip whitespace and potential commas. */
516 while (*ptr && (av_isspace(*ptr) || *ptr == ','))
517 ptr++;
518 if (!*ptr)
519 break;
520
521 key = ptr;
522
523 if (!(ptr = strchr(key, '=')))
524 break;
525 ptr++;
526 key_len = ptr - key;
527
528 callback_get_buf(context, key, key_len, &dest, &dest_len);
529 dest_end = dest ? dest + dest_len - 1 : NULL;
530
531 if (*ptr == '\"') {
532 ptr++;
533 while (*ptr && *ptr != '\"') {
534 if (*ptr == '\\') {
535 if (!ptr[1])
536 break;
537 if (dest && dest < dest_end)
538 *dest++ = ptr[1];
539 ptr += 2;
540 } else {
541 if (dest && dest < dest_end)
542 *dest++ = *ptr;
543 ptr++;
544 }
545 }
546 if (*ptr == '\"')
547 ptr++;
548 } else {
549 for (; *ptr && !(av_isspace(*ptr) || *ptr == ','); ptr++)
550 if (dest && dest < dest_end)
551 *dest++ = *ptr;
552 }
553 if (dest)
554 *dest = 0;
555 }
556 }
557
558 6875 int avformat_network_init(void)
559 {
560 #if CONFIG_NETWORK
561 int ret;
562
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6875 times.
6875 if ((ret = ff_network_init()) < 0)
563 return ret;
564
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6875 times.
6875 if ((ret = ff_tls_init()) < 0)
565 return ret;
566 #endif
567 6875 return 0;
568 }
569
570 6875 int avformat_network_deinit(void)
571 {
572 #if CONFIG_NETWORK
573 6875 ff_network_close();
574 6875 ff_tls_deinit();
575 #endif
576 6875 return 0;
577 }
578
579 366 int ff_is_http_proto(const char *filename) {
580 366 const char *proto = avio_find_protocol_name(filename);
581
3/6
✓ Branch 0 taken 366 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 366 times.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 366 times.
366 return proto ? (!av_strcasecmp(proto, "http") || !av_strcasecmp(proto, "https")) : 0;
582 }
583
584 9 int ff_bprint_to_codecpar_extradata(AVCodecParameters *par, struct AVBPrint *buf)
585 {
586 int ret;
587 char *str;
588
589 9 ret = av_bprint_finalize(buf, &str);
590
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (ret < 0)
591 return ret;
592
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 if (!av_bprint_is_complete(buf)) {
593 av_free(str);
594 return AVERROR(ENOMEM);
595 }
596
597 9 par->extradata = str;
598 /* Note: the string is NUL terminated (so extradata can be read as a
599 * string), but the ending character is not accounted in the size (in
600 * binary formats you are likely not supposed to mux that character). When
601 * extradata is copied, it is also padded with AV_INPUT_BUFFER_PADDING_SIZE
602 * zeros. */
603 9 par->extradata_size = buf->len;
604 9 return 0;
605 }
606