FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/utils.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 194 326 59.5%
Functions: 20 26 76.9%
Branches: 111 259 42.9%

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/mem.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 /**
43 * @file
44 * various utility functions for use within FFmpeg
45 */
46
47 /* an arbitrarily chosen "sane" max packet size -- 50M */
48 #define SANE_CHUNK_SIZE (50000000)
49
50 /* Read the data in sane-sized chunks and append to pkt.
51 * Return the number of bytes read or an error. */
52 273085 static int append_packet_chunked(AVIOContext *s, AVPacket *pkt, int size)
53 {
54 273085 int orig_size = pkt->size;
55 int ret;
56
57 do {
58 273085 int prev_size = pkt->size;
59 int read_size;
60
61 /* When the caller requests a lot of data, limit it to the amount
62 * left in file or SANE_CHUNK_SIZE when it is not known. */
63 273085 read_size = size;
64
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 273085 times.
273085 if (read_size > SANE_CHUNK_SIZE/10) {
65 read_size = ffio_limit(s, read_size);
66 // If filesize/maxsize is unknown, limit to SANE_CHUNK_SIZE
67 if (ffiocontext(s)->maxsize < 0)
68 read_size = FFMIN(read_size, SANE_CHUNK_SIZE);
69 }
70
71 273085 ret = av_grow_packet(pkt, read_size);
72
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 273085 times.
273085 if (ret < 0)
73 break;
74
75 273085 ret = avio_read(s, pkt->data + prev_size, read_size);
76
2/2
✓ Branch 0 taken 917 times.
✓ Branch 1 taken 272168 times.
273085 if (ret != read_size) {
77 917 av_shrink_packet(pkt, prev_size + FFMAX(ret, 0));
78 917 break;
79 }
80
81 272168 size -= read_size;
82
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 272168 times.
272168 } while (size > 0);
83
2/2
✓ Branch 0 taken 872 times.
✓ Branch 1 taken 272213 times.
273085 if (size > 0)
84 872 pkt->flags |= AV_PKT_FLAG_CORRUPT;
85
86
2/2
✓ Branch 0 taken 2247 times.
✓ Branch 1 taken 270838 times.
273085 if (!pkt->size)
87 2247 av_packet_unref(pkt);
88
2/2
✓ Branch 0 taken 270833 times.
✓ Branch 1 taken 2252 times.
273085 return pkt->size > orig_size ? pkt->size - orig_size : ret;
89 }
90
91 261579 int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
92 {
93 #if FF_API_INIT_PACKET
94 FF_DISABLE_DEPRECATION_WARNINGS
95 261579 av_init_packet(pkt);
96 261579 pkt->data = NULL;
97 261579 pkt->size = 0;
98 FF_ENABLE_DEPRECATION_WARNINGS
99 #else
100 av_packet_unref(pkt);
101 #endif
102 261579 pkt->pos = avio_tell(s);
103
104 261579 return append_packet_chunked(s, pkt, size);
105 }
106
107 12096 int av_append_packet(AVIOContext *s, AVPacket *pkt, int size)
108 {
109
2/2
✓ Branch 0 taken 590 times.
✓ Branch 1 taken 11506 times.
12096 if (!pkt->size)
110 590 return av_get_packet(s, pkt, size);
111 11506 return append_packet_chunked(s, pkt, size);
112 }
113
114 995 int av_filename_number_test(const char *filename)
115 {
116 char buf[1024];
117
3/4
✓ Branch 0 taken 995 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 300 times.
✓ Branch 3 taken 695 times.
1990 return filename &&
118 995 (av_get_frame_filename(buf, sizeof(buf), filename, 1) >= 0);
119 }
120
121 /**********************************************************/
122
123 1127 unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
124 {
125
2/2
✓ Branch 0 taken 7808 times.
✓ Branch 1 taken 2 times.
7810 while (tags->id != AV_CODEC_ID_NONE) {
126
2/2
✓ Branch 0 taken 1125 times.
✓ Branch 1 taken 6683 times.
7808 if (tags->id == id)
127 1125 return tags->tag;
128 6683 tags++;
129 }
130 2 return 0;
131 }
132
133 2972 enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
134 {
135
2/2
✓ Branch 0 taken 256579 times.
✓ Branch 1 taken 861 times.
257440 for (int i = 0; tags[i].id != AV_CODEC_ID_NONE; i++)
136
2/2
✓ Branch 0 taken 2111 times.
✓ Branch 1 taken 254468 times.
256579 if (tag == tags[i].tag)
137 2111 return tags[i].id;
138
2/2
✓ Branch 0 taken 77379 times.
✓ Branch 1 taken 856 times.
78235 for (int i = 0; tags[i].id != AV_CODEC_ID_NONE; i++)
139
2/2
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 77374 times.
77379 if (ff_toupper4(tag) == ff_toupper4(tags[i].tag))
140 5 return tags[i].id;
141 856 return AV_CODEC_ID_NONE;
142 }
143
144 550 enum AVCodecID ff_get_pcm_codec_id(int bps, int flt, int be, int sflags)
145 {
146
2/4
✓ Branch 0 taken 550 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 550 times.
550 if (bps <= 0 || bps > 64)
147 return AV_CODEC_ID_NONE;
148
149
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 546 times.
550 if (flt) {
150
2/3
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 switch (bps) {
151 2 case 32:
152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 return be ? AV_CODEC_ID_PCM_F32BE : AV_CODEC_ID_PCM_F32LE;
153 2 case 64:
154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 return be ? AV_CODEC_ID_PCM_F64BE : AV_CODEC_ID_PCM_F64LE;
155 default:
156 return AV_CODEC_ID_NONE;
157 }
158 } else {
159 546 bps += 7;
160 546 bps >>= 3;
161
2/2
✓ Branch 0 taken 528 times.
✓ Branch 1 taken 18 times.
546 if (sflags & (1 << (bps - 1))) {
162
3/6
✗ Branch 0 not taken.
✓ Branch 1 taken 513 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
528 switch (bps) {
163 case 1:
164 return AV_CODEC_ID_PCM_S8;
165 513 case 2:
166
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 511 times.
513 return be ? AV_CODEC_ID_PCM_S16BE : AV_CODEC_ID_PCM_S16LE;
167 12 case 3:
168
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;
169 3 case 4:
170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 return be ? AV_CODEC_ID_PCM_S32BE : AV_CODEC_ID_PCM_S32LE;
171 case 8:
172 return be ? AV_CODEC_ID_PCM_S64BE : AV_CODEC_ID_PCM_S64LE;
173 default:
174 return AV_CODEC_ID_NONE;
175 }
176 } else {
177
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) {
178 18 case 1:
179 18 return AV_CODEC_ID_PCM_U8;
180 case 2:
181 return be ? AV_CODEC_ID_PCM_U16BE : AV_CODEC_ID_PCM_U16LE;
182 case 3:
183 return be ? AV_CODEC_ID_PCM_U24BE : AV_CODEC_ID_PCM_U24LE;
184 case 4:
185 return be ? AV_CODEC_ID_PCM_U32BE : AV_CODEC_ID_PCM_U32LE;
186 default:
187 return AV_CODEC_ID_NONE;
188 }
189 }
190 }
191 }
192
193 5997 unsigned int av_codec_get_tag(const AVCodecTag *const *tags, enum AVCodecID id)
194 {
195 unsigned int tag;
196
2/2
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 5985 times.
5997 if (!av_codec_get_tag2(tags, id, &tag))
197 12 return 0;
198 5985 return tag;
199 }
200
201 9259 int av_codec_get_tag2(const AVCodecTag * const *tags, enum AVCodecID id,
202 unsigned int *tag)
203 {
204
3/4
✓ Branch 0 taken 17463 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17436 times.
✓ Branch 3 taken 27 times.
17463 for (int i = 0; tags && tags[i]; i++) {
205 17436 const AVCodecTag *codec_tags = tags[i];
206
2/2
✓ Branch 0 taken 734848 times.
✓ Branch 1 taken 8204 times.
743052 while (codec_tags->id != AV_CODEC_ID_NONE) {
207
2/2
✓ Branch 0 taken 9232 times.
✓ Branch 1 taken 725616 times.
734848 if (codec_tags->id == id) {
208 9232 *tag = codec_tags->tag;
209 9232 return 1;
210 }
211 725616 codec_tags++;
212 }
213 }
214 27 return 0;
215 }
216
217 175 enum AVCodecID av_codec_get_id(const AVCodecTag *const *tags, unsigned int tag)
218 {
219
3/4
✓ Branch 0 taken 319 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 259 times.
✓ Branch 3 taken 60 times.
319 for (int i = 0; tags && tags[i]; i++) {
220 259 enum AVCodecID id = ff_codec_get_id(tags[i], tag);
221
2/2
✓ Branch 0 taken 115 times.
✓ Branch 1 taken 144 times.
259 if (id != AV_CODEC_ID_NONE)
222 115 return id;
223 }
224 60 return AV_CODEC_ID_NONE;
225 }
226
227 882 int ff_alloc_extradata(AVCodecParameters *par, int size)
228 {
229 882 av_freep(&par->extradata);
230 882 par->extradata_size = 0;
231
232
2/4
✓ Branch 0 taken 882 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 882 times.
882 if (size < 0 || size >= INT32_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
233 return AVERROR(EINVAL);
234
235 882 par->extradata = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
236
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 882 times.
882 if (!par->extradata)
237 return AVERROR(ENOMEM);
238
239 882 memset(par->extradata + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
240 882 par->extradata_size = size;
241
242 882 return 0;
243 }
244
245 /*******************************************************/
246
247 37 uint64_t ff_ntp_time(void)
248 {
249 37 return (av_gettime() / 1000) * 1000 + NTP_OFFSET_US;
250 }
251
252 uint64_t ff_get_formatted_ntp_time(uint64_t ntp_time_us)
253 {
254 uint64_t ntp_ts, frac_part, sec;
255 uint32_t usec;
256
257 //current ntp time in seconds and micro seconds
258 sec = ntp_time_us / 1000000;
259 usec = ntp_time_us % 1000000;
260
261 //encoding in ntp timestamp format
262 frac_part = usec * 0xFFFFFFFFULL;
263 frac_part /= 1000000;
264
265 if (sec > 0xFFFFFFFFULL)
266 av_log(NULL, AV_LOG_WARNING, "NTP time format roll over detected\n");
267
268 ntp_ts = sec << 32;
269 ntp_ts |= frac_part;
270
271 return ntp_ts;
272 }
273
274 uint64_t ff_parse_ntp_time(uint64_t ntp_ts)
275 {
276 uint64_t sec = ntp_ts >> 32;
277 uint64_t frac_part = ntp_ts & 0xFFFFFFFFULL;
278 uint64_t usec = (frac_part * 1000000) / 0xFFFFFFFFULL;
279
280 return (sec * 1000000) + usec;
281 }
282
283 150931 int ff_get_frame_filename(char *buf, int buf_size, const char *path, int64_t number, int flags)
284 {
285 const char *p;
286 char *q, buf1[20], c;
287 int nd, len, percentd_found;
288
289 150931 q = buf;
290 150931 p = path;
291 150931 percentd_found = 0;
292 for (;;) {
293 11504495 c = *p++;
294
2/2
✓ Branch 0 taken 150931 times.
✓ Branch 1 taken 11353564 times.
11504495 if (c == '\0')
295 150931 break;
296
2/2
✓ Branch 0 taken 150221 times.
✓ Branch 1 taken 11203343 times.
11353564 if (c == '%') {
297 do {
298 150221 nd = 0;
299
2/2
✓ Branch 0 taken 300330 times.
✓ Branch 1 taken 150221 times.
450551 while (av_isdigit(*p)) {
300
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 300330 times.
300330 if (nd >= INT_MAX / 10 - 255)
301 goto fail;
302 300330 nd = nd * 10 + *p++ - '0';
303 }
304 150221 c = *p++;
305
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 150221 times.
150221 } while (av_isdigit(c));
306
307
1/3
✗ Branch 0 not taken.
✓ Branch 1 taken 150221 times.
✗ Branch 2 not taken.
150221 switch (c) {
308 case '%':
309 goto addchar;
310 150221 case 'd':
311
3/4
✓ Branch 0 taken 149558 times.
✓ Branch 1 taken 663 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 149558 times.
150221 if (!(flags & AV_FRAME_FILENAME_FLAGS_MULTIPLE) && percentd_found)
312 goto fail;
313 150221 percentd_found = 1;
314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 150221 times.
150221 if (number < 0)
315 nd += 1;
316 150221 snprintf(buf1, sizeof(buf1), "%0*" PRId64, nd, number);
317 150221 len = strlen(buf1);
318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 150221 times.
150221 if ((q - buf + len) > buf_size - 1)
319 goto fail;
320 150221 memcpy(q, buf1, len);
321 150221 q += len;
322 150221 break;
323 default:
324 goto fail;
325 }
326 } else {
327 11203343 addchar:
328
1/2
✓ Branch 0 taken 11203343 times.
✗ Branch 1 not taken.
11203343 if ((q - buf) < buf_size - 1)
329 11203343 *q++ = c;
330 }
331 }
332
2/2
✓ Branch 0 taken 710 times.
✓ Branch 1 taken 150221 times.
150931 if (!percentd_found)
333 710 goto fail;
334 150221 *q = '\0';
335 150221 return 0;
336 710 fail:
337 710 *q = '\0';
338 710 return -1;
339 }
340
341 int av_get_frame_filename2(char *buf, int buf_size, const char *path, int number, int flags)
342 {
343 return ff_get_frame_filename(buf, buf_size, path, number, flags);
344 }
345
346 150261 int av_get_frame_filename(char *buf, int buf_size, const char *path, int number)
347 {
348 150261 return ff_get_frame_filename(buf, buf_size, path, number, 0);
349 }
350
351 9 void av_url_split(char *proto, int proto_size,
352 char *authorization, int authorization_size,
353 char *hostname, int hostname_size,
354 int *port_ptr, char *path, int path_size, const char *url)
355 {
356 const char *p, *ls, *at, *at2, *col, *brk;
357
358
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (port_ptr)
359 9 *port_ptr = -1;
360
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (proto_size > 0)
361 9 proto[0] = 0;
362
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (authorization_size > 0)
363 9 authorization[0] = 0;
364
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (hostname_size > 0)
365 9 hostname[0] = 0;
366
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (path_size > 0)
367 9 path[0] = 0;
368
369 /* parse protocol */
370
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
9 if ((p = strchr(url, ':'))) {
371 8 av_strlcpy(proto, url, FFMIN(proto_size, p + 1 - url));
372 8 p++; /* skip ':' */
373
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (*p == '/')
374 8 p++;
375
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (*p == '/')
376 8 p++;
377 } else {
378 /* no protocol means plain filename */
379 1 av_strlcpy(path, url, path_size);
380 1 return;
381 }
382
383 /* separate path from hostname */
384 8 ls = p + strcspn(p, "/?#");
385 8 av_strlcpy(path, ls, path_size);
386
387 /* the rest is hostname, use that to parse auth/port */
388
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (ls != p) {
389 /* authorization (user[:pass]@hostname) */
390 8 at2 = p;
391
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) {
392 2 av_strlcpy(authorization, at2,
393 2 FFMIN(authorization_size, at + 1 - at2));
394 2 p = at + 1; /* skip '@' */
395 }
396
397
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) {
398 /* [host]:port */
399 av_strlcpy(hostname, p + 1,
400 FFMIN(hostname_size, brk - p));
401 if (brk[1] == ':' && port_ptr)
402 *port_ptr = atoi(brk + 2);
403
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) {
404 1 av_strlcpy(hostname, p,
405 1 FFMIN(col + 1 - p, hostname_size));
406
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (port_ptr)
407 1 *port_ptr = atoi(col + 1);
408 } else
409 7 av_strlcpy(hostname, p,
410 7 FFMIN(ls + 1 - p, hostname_size));
411 }
412 }
413
414 int ff_mkdir_p(const char *path)
415 {
416 int ret = 0;
417 char *temp = av_strdup(path);
418 char *pos = temp;
419 char tmp_ch = '\0';
420
421 if (!path || !temp) {
422 return -1;
423 }
424
425 if (!av_strncasecmp(temp, "/", 1) || !av_strncasecmp(temp, "\\", 1)) {
426 pos++;
427 } else if (!av_strncasecmp(temp, "./", 2) || !av_strncasecmp(temp, ".\\", 2)) {
428 pos += 2;
429 }
430
431 for ( ; *pos != '\0'; ++pos) {
432 if (*pos == '/' || *pos == '\\') {
433 tmp_ch = *pos;
434 *pos = '\0';
435 ret = mkdir(temp, 0755);
436 *pos = tmp_ch;
437 }
438 }
439
440 if ((*(pos - 1) != '/') && (*(pos - 1) != '\\')) {
441 ret = mkdir(temp, 0755);
442 }
443
444 av_free(temp);
445 return ret;
446 }
447
448 3382 char *ff_data_to_hex(char *buff, const uint8_t *src, int s, int lowercase)
449 {
450 static const char hex_table_uc[16] = { '0', '1', '2', '3',
451 '4', '5', '6', '7',
452 '8', '9', 'A', 'B',
453 'C', 'D', 'E', 'F' };
454 static const char hex_table_lc[16] = { '0', '1', '2', '3',
455 '4', '5', '6', '7',
456 '8', '9', 'a', 'b',
457 'c', 'd', 'e', 'f' };
458
2/2
✓ Branch 0 taken 2685 times.
✓ Branch 1 taken 697 times.
3382 const char *hex_table = lowercase ? hex_table_lc : hex_table_uc;
459
460
2/2
✓ Branch 0 taken 54126 times.
✓ Branch 1 taken 3382 times.
57508 for (int i = 0; i < s; i++) {
461 54126 buff[i * 2] = hex_table[src[i] >> 4];
462 54126 buff[i * 2 + 1] = hex_table[src[i] & 0xF];
463 }
464 3382 buff[2 * s] = '\0';
465
466 3382 return buff;
467 }
468
469 int ff_hex_to_data(uint8_t *data, const char *p)
470 {
471 int c, len, v;
472
473 len = 0;
474 v = 1;
475 for (;;) {
476 p += strspn(p, SPACE_CHARS);
477 if (*p == '\0')
478 break;
479 c = av_toupper((unsigned char) *p++);
480 if (c >= '0' && c <= '9')
481 c = c - '0';
482 else if (c >= 'A' && c <= 'F')
483 c = c - 'A' + 10;
484 else
485 break;
486 v = (v << 4) | c;
487 if (v & 0x100) {
488 if (data)
489 data[len] = v;
490 len++;
491 v = 1;
492 }
493 }
494 return len;
495 }
496
497 void ff_parse_key_value(const char *str, ff_parse_key_val_cb callback_get_buf,
498 void *context)
499 {
500 const char *ptr = str;
501
502 /* Parse key=value pairs. */
503 for (;;) {
504 const char *key;
505 char *dest = NULL, *dest_end;
506 int key_len, dest_len = 0;
507
508 /* Skip whitespace and potential commas. */
509 while (*ptr && (av_isspace(*ptr) || *ptr == ','))
510 ptr++;
511 if (!*ptr)
512 break;
513
514 key = ptr;
515
516 if (!(ptr = strchr(key, '=')))
517 break;
518 ptr++;
519 key_len = ptr - key;
520
521 callback_get_buf(context, key, key_len, &dest, &dest_len);
522 dest_end = dest ? dest + dest_len - 1 : NULL;
523
524 if (*ptr == '\"') {
525 ptr++;
526 while (*ptr && *ptr != '\"') {
527 if (*ptr == '\\') {
528 if (!ptr[1])
529 break;
530 if (dest && dest < dest_end)
531 *dest++ = ptr[1];
532 ptr += 2;
533 } else {
534 if (dest && dest < dest_end)
535 *dest++ = *ptr;
536 ptr++;
537 }
538 }
539 if (*ptr == '\"')
540 ptr++;
541 } else {
542 for (; *ptr && !(av_isspace(*ptr) || *ptr == ','); ptr++)
543 if (dest && dest < dest_end)
544 *dest++ = *ptr;
545 }
546 if (dest)
547 *dest = 0;
548 }
549 }
550
551 8068 int avformat_network_init(void)
552 {
553 #if CONFIG_NETWORK
554 int ret;
555
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8068 times.
8068 if ((ret = ff_network_init()) < 0)
556 return ret;
557
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8068 times.
8068 if ((ret = ff_tls_init()) < 0)
558 return ret;
559 #endif
560 8068 return 0;
561 }
562
563 8068 int avformat_network_deinit(void)
564 {
565 #if CONFIG_NETWORK
566 8068 ff_network_close();
567 8068 ff_tls_deinit();
568 #endif
569 8068 return 0;
570 }
571
572 366 int ff_is_http_proto(const char *filename) {
573 366 const char *proto = avio_find_protocol_name(filename);
574
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;
575 }
576
577 9 int ff_bprint_to_codecpar_extradata(AVCodecParameters *par, struct AVBPrint *buf)
578 {
579 int ret;
580 char *str;
581
582 9 ret = av_bprint_finalize(buf, &str);
583
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (ret < 0)
584 return ret;
585
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 if (!av_bprint_is_complete(buf)) {
586 av_free(str);
587 return AVERROR(ENOMEM);
588 }
589
590 9 par->extradata = str;
591 /* Note: the string is NUL terminated (so extradata can be read as a
592 * string), but the ending character is not accounted in the size (in
593 * binary formats you are likely not supposed to mux that character). When
594 * extradata is copied, it is also padded with AV_INPUT_BUFFER_PADDING_SIZE
595 * zeros. */
596 9 par->extradata_size = buf->len;
597 9 return 0;
598 }
599