2 * various utility functions for use within FFmpeg
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
51 * various utility functions for use within FFmpeg
54/* an arbitrarily chosen "sane" max packet size -- 50M */
55 #define SANE_CHUNK_SIZE (50000000)
57/* Read the data in sane-sized chunks and append to pkt.
58 * Return the number of bytes read or an error. */
61 int orig_size =
pkt->size;
65 int prev_size =
pkt->size;
68 /* When the caller requests a lot of data, limit it to the amount
69 * left in file or SANE_CHUNK_SIZE when it is not known. */
73 // If filesize/maxsize is unknown, limit to SANE_CHUNK_SIZE
83 if (ret != read_size) {
95 return pkt->size > orig_size ?
pkt->size - orig_size : ret;
100#if FF_API_INIT_PACKET
131/**********************************************************/
171 if (sflags & (1 << (
bps - 1))) {
214 for (
int i = 0; tags && tags[
i];
i++) {
217 if (codec_tags->
id ==
id) {
229 for (
int i = 0; tags && tags[
i];
i++) {
255/*******************************************************/
264 uint64_t ntp_ts, frac_part, sec;
267 //current ntp time in seconds and microseconds
268 sec = ntp_time_us / 1000000;
269 usec = ntp_time_us % 1000000;
271 //encoding in ntp timestamp format
272 frac_part = usec * 0xFFFFFFFFULL;
273 frac_part /= 1000000;
275 if (sec > 0xFFFFFFFFULL)
286 uint64_t sec = ntp_ts >> 32;
287 uint64_t frac_part = ntp_ts & 0xFFFFFFFFULL;
288 uint64_t usec = (frac_part * 1000000) / 0xFFFFFFFFULL;
290 return (sec * 1000000) + usec;
297 int nd, percentd_found;
309 if (nd >= INT_MAX / 10 - 255)
311 nd = nd * 10 + *p++ -
'0';
362 char *authorization,
int authorization_size,
363 char *hostname,
int hostname_size,
364 int *port_ptr,
char *path,
int path_size,
const char *url)
366 const char *p, *ls, *at, *at2, *col, *brk;
372 if (authorization_size > 0)
373 authorization[0] = 0;
374 if (hostname_size > 0)
380 if ((p = strchr(url,
':'))) {
388 /* no protocol means plain filename */
393 /* separate path from hostname */
394 ls = p + strcspn(p,
"/?#");
397 /* the rest is hostname, use that to parse auth/port */
399 /* authorization (user[:pass]@hostname) */
401 while ((at = strchr(p,
'@')) && at < ls) {
403 FFMIN(authorization_size, at + 1 - at2));
404 p = at + 1;
/* skip '@' */
407 if (*p ==
'[' && (brk = strchr(p,
']')) && brk < ls) {
410 FFMIN(hostname_size, brk - p));
411 if (brk[1] ==
':' && port_ptr)
412 *port_ptr = atoi(brk + 2);
413 }
else if ((col = strchr(p,
':')) && col < ls) {
415 FFMIN(col + 1 - p, hostname_size));
417 *port_ptr = atoi(col + 1);
420 FFMIN(ls + 1 - p, hostname_size));
431 if (!path || !
temp) {
441 for ( ; *
pos !=
'0円'; ++
pos) {
442 if (*
pos ==
'/' || *
pos ==
'\\') {
445 ret = mkdir(
temp, 0755);
446 if (ret < 0 && errno != EEXIST) {
456 if ((*(
pos - 1) !=
'/') && (*(
pos - 1) !=
'\\')) {
457 ret = mkdir(
temp, 0755);
466 static const char hex_table_uc[16] = {
'0',
'1',
'2',
'3',
469 'C',
'D',
'E',
'F' };
470 static const char hex_table_lc[16] = {
'0',
'1',
'2',
'3',
473 'c',
'd',
'e',
'f' };
474 const char *hex_table = lowercase ? hex_table_lc : hex_table_uc;
477 for (
unsigned i = 0;
i <
s;
i++) {
478 buff[
i * 2] = hex_table[
src[
i] >> 4];
479 buff[
i * 2 + 1] = hex_table[
src[
i] & 0xF];
497 if (
c >=
'0' &&
c <=
'9')
499 else if (
c >=
'A' &&
c <=
'F')
517 const char *ptr = str;
519 /* Parse key=value pairs. */
522 char *dest =
NULL, *dest_end;
523 int key_len, dest_len = 0;
525 /* Skip whitespace and potential commas. */
526 while (*ptr && (
av_isspace(*ptr) || *ptr ==
','))
533 if (!(ptr = strchr(
key,
'=')))
538 callback_get_buf(context,
key, key_len, &dest, &dest_len);
539 dest_end = dest ? dest + dest_len - 1 :
NULL;
543 while (*ptr && *ptr !=
'\"') {
547 if (dest && dest < dest_end)
551 if (dest && dest < dest_end)
559 for (; *ptr && !(
av_isspace(*ptr) || *ptr ==
','); ptr++)
560 if (dest && dest < dest_end)
608 /* Note: the string is NUL terminated (so extradata can be read as a
609 * string), but the ending character is not accounted in the size (in
610 * binary formats you are likely not supposed to mux that character). When
611 * extradata is copied, it is also padded with AV_INPUT_BUFFER_PADDING_SIZE
619 int microsecs = timestamp % 1000000;
620 time_t seconds = timestamp / 1000000 - (microsecs < 0);
621 struct tm *ptm, tmbuf;
625 if (!strftime(buf,
sizeof(buf),
"%Y-%m-%dT%H:%M:%S", ptm))
627 av_strlcatf(buf,
sizeof(buf),
".%06dZ", microsecs + (microsecs < 0) * 1000000);
636 char decoded_name[128];
653 size_t len = strcspn(str,
"=&");
656 if (!allow_unknown) {
664 len = strcspn(str,
"&");
666 }
else if (*str ==
'&' || *str ==
'0円') {
667 /* Check for bool options without value, e.g. "?verify".
668 * Unfortunately "listen" is a tri-state INT for some protocols so
669 * we also have to allow that for backward compatibility. */
680 len = strcspn(str,
"&");
703 uint8_t *p =
av_malloc(fam_offset + bp->len);
708 memcpy(p, struct_ptr, fam_offset);
709 memcpy(p + fam_offset, bp->str, bp->len);
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
const char * avio_find_protocol_name(const char *url)
Return the name of the protocol that will handle the passed URL.
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
static av_always_inline FFIOContext * ffiocontext(AVIOContext *ctx)
int ffio_limit(AVIOContext *s, int size)
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
void av_bprintf(AVBPrint *buf, const char *fmt,...)
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
#define AV_BPRINT_SIZE_COUNT_ONLY
#define flags(name, subs,...)
#define i(width, name, range_min, range_max)
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
AVCodecID
Identify the syntax and semantics of the bitstream.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
int av_grow_packet(AVPacket *pkt, int grow_by)
Increase packet size, correctly zeroing padding.
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
int avformat_network_deinit(void)
Undo the initialization done by avformat_network_init.
int avformat_network_init(void)
Do global initialization of network libraries.
int av_get_frame_filename2(char *buf, int buf_size, const char *path, int number, int flags)
Return in 'buf' the path with 'd' replaced by a number.
#define AV_FRAME_FILENAME_FLAGS_IGNORE_TRUNCATION
Ignore truncated output instead of returning an error.
void av_url_split(char *proto, int proto_size, char *authorization, int authorization_size, char *hostname, int hostname_size, int *port_ptr, char *path, int path_size, const char *url)
Split a URL string into components.
int av_get_frame_filename(char *buf, int buf_size, const char *path, int number)
#define AV_FRAME_FILENAME_FLAGS_MULTIPLE
Allow multiple d.
int av_filename_number_test(const char *filename)
Check whether filename actually is a numbered sequence generator.
static int av_bprint_is_complete(const AVBPrint *buf)
Test if the print buffer is complete (not truncated).
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.
void av_bprint_init_for_buffer(AVBPrint *buf, char *buffer, unsigned size)
Init a print buffer using a pre-existing buffer.
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
#define AVERROR_EXTERNAL
Generic error in an external library.
#define AVERROR_OPTION_NOT_FOUND
Option not found.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_VERBOSE
Detailed information.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
static av_const int av_isdigit(int c)
Locale-independent conversion of ASCII isdigit.
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
static av_const int av_isspace(int c)
Locale-independent conversion of ASCII isspace.
static av_const int av_toupper(int c)
Locale-independent conversion of ASCII characters to uppercase.
int av_strncasecmp(const char *a, const char *b, size_t n)
Locale-independent case-insensitive compare.
const AVOption * av_opt_find(void *obj, const char *name, const char *unit, int opt_flags, int search_flags)
Look for an option in an object.
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
common internal api header.
unsigned int ff_toupper4(unsigned int x)
common internal API header
#define FF_DISABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Memory handling functions.
int ff_network_init(void)
Initialize the network subsystem.
void ff_network_close(void)
miscellaneous OS support macros and functions.
This struct describes the properties of an encoded stream.
int extradata_size
Size of the extradata content in bytes.
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
This structure stores compressed data.
int64_t av_gettime(void)
Get the current time in microseconds.
int ff_urldecode_len(char *dest, size_t dest_len, const char *url, size_t url_max_len, int decode_plus_sign)
Decodes an URL from its percent-encoded form back into normal representation.