From d8fd06c37de94d78eb37af93177de7c3040cf1f2 Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Sun, 23 Dec 2012 21:25:24 +0100 Subject: [PATCH 1/3] avstring: add av_basename and av_dirname Thread safe version of the common basename and dirname. --- Changelog | 3 +++ doc/APIchanges | 3 +++ libavutil/avstring.c | 41 +++++++++++++++++++++++++++++++++++++++++ libavutil/avstring.h | 16 ++++++++++++++++ libavutil/version.h | 2 +- 5 files changed, 64 insertions(+), 1 deletion(-) diff --git a/Changelog b/Changelog index 8dfffcfcd6..b5950219a3 100644 --- a/Changelog +++ b/Changelog @@ -2,6 +2,9 @@ Entries are sorted chronologically from oldest to youngest within each release, releases are sorted from youngest to oldest. version : +- av_basename and av_dirname + +version 9_beta3: - ashowinfo audio filter - 24-bit FLAC encoding - audio volume filter diff --git a/doc/APIchanges b/doc/APIchanges index 1c6247ef56..a5a0bea1fc 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -13,6 +13,9 @@ libavutil: 2012-10-22 API changes, most recent first: +2012-xx-xx - xxxxxxx - lavu 52.2.1 - avstring.h + Add av_basename() and av_dirname(). + 2012-xx-xx - xxxxxxx - lavu 52.2.0 - audioconvert.h Rename audioconvert.h to channel_layout.h. audioconvert.h is now deprecated. diff --git a/libavutil/avstring.c b/libavutil/avstring.c index c14832e7b9..2c88bd3d5b 100644 --- a/libavutil/avstring.c +++ b/libavutil/avstring.c @@ -25,6 +25,8 @@ #include #include #include "avstring.h" +#include "config.h" +#include "common.h" #include "mem.h" int av_strstart(const char *str, const char *pfx, const char **ptr) @@ -156,6 +158,45 @@ int av_strncasecmp(const char *a, const char *b, size_t n) return c1 - c2; } +const char *av_basename(const char *path) +{ + char *p = strrchr(path, '/'); + +#if HAVE_DOS_PATHS + char *q = strrchr(path, '\\'); + char *d = strchr(path, ':'); + + p = FFMAX3(p, q, d); +#endif + + if (!p) + return path; + + return p + 1; +} + +const char *av_dirname(char *path) +{ + char *p = strrchr(path, '/'); + +#if HAVE_DOS_PATHS + char *q = strrchr(path, '\\'); + char *d = strchr(path, ':'); + + d = d ? d + 1 : d; + + p = FFMAX3(p, q, d); +#endif + + if (!p) + return "."; + + *p = '\0'; + + return path; +} + + #ifdef TEST #include "common.h" diff --git a/libavutil/avstring.h b/libavutil/avstring.h index ed4e465cbc..acd6610d38 100644 --- a/libavutil/avstring.h +++ b/libavutil/avstring.h @@ -168,6 +168,22 @@ int av_strcasecmp(const char *a, const char *b); */ int av_strncasecmp(const char *a, const char *b, size_t n); + +/** + * Thread safe basename. + * @param path the path, on DOS both \ and / are considered separators. + * @return pointer to the basename substring. + */ +const char *av_basename(const char *path); + +/** + * Thread safe dirname. + * @param path the path, on DOS both \ and / are considered separators. + * @return the path with the separator replaced by the string terminator or ".". + * @note the function may change the input string. + */ +const char *av_dirname(char *path); + /** * @} */ diff --git a/libavutil/version.h b/libavutil/version.h index f69c73e929..1dbb11ca21 100644 --- a/libavutil/version.h +++ b/libavutil/version.h @@ -37,7 +37,7 @@ */ #define LIBAVUTIL_VERSION_MAJOR 52 -#define LIBAVUTIL_VERSION_MINOR 2 +#define LIBAVUTIL_VERSION_MINOR 3 #define LIBAVUTIL_VERSION_MICRO 0 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \ From 66f7b4862fe9fd1828a6729df5d04a499af227db Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Mon, 24 Dec 2012 00:08:35 +0100 Subject: [PATCH 2/3] hlsenc: use the basename to generate the list entries The segment path is desumed from the playlist path, recording a relative path in the playlist while serving the file could lead to misleading results. --- libavformat/hlsenc.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c index eade740f07..418c87d2cc 100644 --- a/libavformat/hlsenc.c +++ b/libavformat/hlsenc.c @@ -85,7 +85,8 @@ static int append_entry(HLSContext *hls, uint64_t duration) if (!en) return AVERROR(ENOMEM); - av_get_frame_filename(en->name, sizeof(en->name), hls->basename, + av_get_frame_filename(en->name, sizeof(en->name), + av_basename(hls->basename), hls->number -1); en->duration = duration; From 6dd93ee6f1b050ad7c4b247899e83efa293ee405 Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Tue, 25 Dec 2012 08:59:38 +0100 Subject: [PATCH 3/3] hlsenc: check append_entry return value --- libavformat/hlsenc.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c index 418c87d2cc..e039687d1e 100644 --- a/libavformat/hlsenc.c +++ b/libavformat/hlsenc.c @@ -259,9 +259,12 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt) av_compare_ts(pkt->pts, st->time_base, end_pts, AV_TIME_BASE_Q) >= 0 && pkt->flags & AV_PKT_FLAG_KEY) { - append_entry(hls, av_rescale(pkt->pts - hls->end_pts, - st->time_base.num, - st->time_base.den)); + ret = append_entry(hls, av_rescale(pkt->pts - hls->end_pts, + st->time_base.num, + st->time_base.den)); + if (ret) + return ret; + hls->end_pts = pkt->pts; av_write_frame(oc, NULL); /* Flush any buffered data */