From: Gustavo Sverzut Barbieri Date: Tue, 10 Sep 2013 23:04:20 +0000 (-0300) Subject: utils refactor. X-Git-Tag: accepted/tizen/generic/20140106.140339~24 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=3f711094f0de7fcac95e33f6a7da23c0a3b098cf;hp=7658da95b48b3bd272878723dc64f296b7151af7;p=platform%2Fupstream%2Flightmediascanner.git utils refactor. move parser specific stuff from src/lib/shared/util.h to src/plugin/shared/util.h, still not to be installed and without "lms_" prefix. the aspect_ratio calculation was moved into lightmediascanner_util.h and lightmediascanner_db.h to be simpler to use. extract name from path is also too common, now in lightmediascanner_util.h as lms_name_from_path(). --- diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index a9d5b78..5368f40 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -29,9 +29,3 @@ liblightmediascanner_la_SOURCES = \ liblightmediascanner_la_LIBADD = -ldl @SQLITE3_LIBS@ @LTLIBICONV@ liblightmediascanner_la_LDFLAGS = -version-info @version_info@ - -# shared sources/headers -noinst_LTLIBRARIES = shared/libshared.la -shared_libshared_la_SOURCES = shared/util.c - -noinst_HEADERS += shared/util.h diff --git a/src/lib/lightmediascanner_db.h b/src/lib/lightmediascanner_db.h index 93f54e7..0b87b01 100644 --- a/src/lib/lightmediascanner_db.h +++ b/src/lib/lightmediascanner_db.h @@ -171,6 +171,8 @@ extern "C" { API int lms_db_video_free(lms_db_video_t *ldv) GNUC_NON_NULL(1); API int lms_db_video_add(lms_db_video_t *ldv, struct lms_video_info *info) GNUC_NON_NULL(1, 2); + API int lms_stream_video_info_aspect_ratio_guess(struct lms_stream_video_info *info) GNUC_NON_NULL(1); + /* Playlist Records */ struct lms_playlist_info { int64_t id; diff --git a/src/lib/lightmediascanner_db_video.c b/src/lib/lightmediascanner_db_video.c index c985292..8a7f00c 100644 --- a/src/lib/lightmediascanner_db_video.c +++ b/src/lib/lightmediascanner_db_video.c @@ -616,3 +616,25 @@ lms_db_video_add(lms_db_video_t *ldv, struct lms_video_info *info) return r; } + +/** + * If aspect ratio wasn't set yet, guess it from stream video info size. + * + * This function calls lms_aspect_ratio_guess() and thus the + * aspect_ratio string will be allocated with malloc(). Remember to + * free() it afterwards. + * + * @param info where to query width and height, then setting aspect_ratio string. + * + * @return 1 on success, 0 on failure. + */ +int +lms_stream_video_info_aspect_ratio_guess(struct lms_stream_video_info *info) +{ + if (info->aspect_ratio.len > 0) + return 1; + + return lms_aspect_ratio_guess(&info->aspect_ratio, + info->width, + info->height); +} diff --git a/src/lib/lightmediascanner_utils.c b/src/lib/lightmediascanner_utils.c index 12a8eef..b43393b 100644 --- a/src/lib/lightmediascanner_utils.c +++ b/src/lib/lightmediascanner_utils.c @@ -22,9 +22,11 @@ #include #include +#include #include #include #include +#include /** * Strips string, in place. @@ -192,6 +194,78 @@ lms_string_size_strndup(struct lms_string_size *dst, const char *src, int size) return 1; } +/* Euclidean algorithm + * http://en.wikipedia.org/wiki/Euclidean_algorithm */ +static unsigned int +gcd(unsigned int a, unsigned int b) +{ + unsigned int t; + + while (b) { + t = b; + b = a % t; + a = t; + } + + return a; +} + +/** + * Guess aspect ratio from known ratios or Greatest Common Divisor. + * + * @param ret where to store the newly allocated string with ratio. + * @param width frame width to guess aspect ratio. + * @param height frame height to guess aspect ratio. + * @return 1 on success and @c ret->str must be @c free()d, 0 on failure. + */ +int +lms_aspect_ratio_guess(struct lms_string_size *ret, int width, int height) +{ + static struct { + double ratio; + struct lms_string_size str; + } *itr, known_ratios[] = { + {16.0 / 9.0, LMS_STATIC_STRING_SIZE("16:9")}, + {4.0 / 3.0, LMS_STATIC_STRING_SIZE("4:3")}, + {3.0 / 2.0, LMS_STATIC_STRING_SIZE("3:2")}, + {5.0 / 3.0, LMS_STATIC_STRING_SIZE("5:3")}, + {8.0 / 5.0, LMS_STATIC_STRING_SIZE("8:5")}, + {1.85, LMS_STATIC_STRING_SIZE("1.85:1")}, + {1.4142, LMS_STATIC_STRING_SIZE("1.41:1")}, + {2.39, LMS_STATIC_STRING_SIZE("2.39:1")}, + {16.18 / 10.0, LMS_STATIC_STRING_SIZE("16.18:10")}, + {-1.0, {NULL, 0}} + }; + double ratio; + unsigned num, den, f; + + if (width <= 0 || height <= 0) { + ret->len = 0; + ret->str = NULL; + return 0; + } + + ratio = (double)width / (double)height; + for (itr = known_ratios; itr->ratio > 0.0; itr++) { + if (fabs(ratio - itr->ratio) <= 0.01) + return lms_string_size_dup(ret, &itr->str); + } + + f = gcd(width, height); + + num = width / f; + den = height / f; + ret->len = asprintf(&ret->str, "%u:%u", num, den); + if (ret->len == (unsigned int)-1) { + ret->len = 0; + ret->str = NULL; + return 0; + } + + return 1; +} + + /** * Find out which of the given extensions matches the given name. * @@ -243,3 +317,35 @@ lms_which_extension(const char *name, unsigned int name_len, const struct lms_st return -1; } + +/** + * Extract name from a path given its path string, length, base and extension. + * + * @param name where to store the result. + * @param path the input path to base the name on. + * @param pathlen the path size in bytes. + * @param baselen where (offset int bytes) in path starts the filename (base name). + * @param extlen the extension length in bytes. + * @param cs_conv charset conversion to use, if none use @c NULL. + * @return 1 on success, 0 on failure. + */ +int +lms_name_from_path(struct lms_string_size *name, const char *path, unsigned int pathlen, unsigned int baselen, unsigned int extlen, struct lms_charset_conv *cs_conv) +{ + int size = pathlen - baselen - extlen; + + name->str = malloc(size + 1); + if (!name->str) { + name->len = 0; + return 0; + } + + name->len = size; + memcpy(name->str, path + baselen, size); + name->str[size] = '\0'; + + if (cs_conv) + lms_charset_conv(cs_conv, &name->str, &name->len); + + return 1; +} diff --git a/src/lib/lightmediascanner_utils.h b/src/lib/lightmediascanner_utils.h index ef200bb..c5fd5f4 100644 --- a/src/lib/lightmediascanner_utils.h +++ b/src/lib/lightmediascanner_utils.h @@ -45,6 +45,7 @@ extern "C" { #endif +#include struct lms_string_size { char *str; @@ -62,8 +63,11 @@ extern "C" { API int lms_string_size_dup(struct lms_string_size *dst, const struct lms_string_size *src) GNUC_NON_NULL(1, 2); API int lms_string_size_strndup(struct lms_string_size *dst, const char *src, int size) GNUC_NON_NULL(1); + API int lms_aspect_ratio_guess(struct lms_string_size *ret, int width, int height) GNUC_NON_NULL(1); + API int lms_which_extension(const char *name, unsigned int name_len, const struct lms_string_size *exts, unsigned int exts_len) GNUC_NON_NULL(1, 3); + API int lms_name_from_path(struct lms_string_size *name, const char *path, unsigned int pathlen, unsigned int baselen, unsigned int extlen, struct lms_charset_conv *cs_conv) GNUC_NON_NULL(1, 2); #ifdef __cplusplus diff --git a/src/lib/shared/util.c b/src/lib/shared/util.c deleted file mode 100644 index 2e1555f..0000000 --- a/src/lib/shared/util.c +++ /dev/null @@ -1,74 +0,0 @@ -/** - * Copyright (C) 2013 Intel Corporation. All rights reserved. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License - * as published by the Free Software Foundation; either version 2.1 of - * the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA - * 02110-1301 USA - * - * @author Lucas De Marchi - */ - -#include -#include - -#include "util.h" - -struct lms_string_size str_extract_name_from_path( - const char *path, unsigned int pathlen, unsigned int baselen, - const struct lms_string_size *ext, struct lms_charset_conv *cs_conv) -{ - struct lms_string_size str; - - str.len = pathlen - baselen - ext->len; - str.str = malloc(str.len + 1); - if (!str.str) - return (struct lms_string_size) { }; - memcpy(str.str, path + baselen, str.len); - str.str[str.len] = '\0'; - if (cs_conv) - lms_charset_conv(cs_conv, &str.str, &str.len); - - return str; -} - -/* Euclidean algorithm - * http://en.wikipedia.org/wiki/Euclidean_algorithm */ -static unsigned int gcd(unsigned int a, unsigned int b) -{ - unsigned int t; - - while (b) { - t = b; - b = a % t; - a = t; - } - - return a; -} - -void reduce_gcd(unsigned int w, unsigned int h, unsigned int *dw, - unsigned int *dh) -{ - unsigned int f; - - *dw = w; - *dh = h; - - if (!w || !h) - return; - - f = gcd(w, h); - *dw /= f; - *dh /= f; -} diff --git a/src/plugins/Makefile.am b/src/plugins/Makefile.am index 9ef8bc8..d7c94ab 100644 --- a/src/plugins/Makefile.am +++ b/src/plugins/Makefile.am @@ -3,17 +3,16 @@ MAINTAINERCLEANFILES = Makefile.in AM_LDFLAGS = -module -avoid-version -shared $(GCLDFLAGS) AM_CFLAGS = -fvisibility=hidden $(WARNINGFLAGS) AM_CPPFLAGS = -include $(top_builddir)/config.h \ - -I$(top_srcdir)/src/lib @SQLITE3_CFLAGS@ + -I$(top_srcdir)/src/lib -I$(srcdir) @SQLITE3_CFLAGS@ pkgdir = $(pluginsdir) -PLUGINS_LIBADD = $(top_builddir)/src/lib/liblightmediascanner.la \ - $(top_builddir)/src/lib/shared/libshared.la +PLUGINS_LIBADD = $(top_builddir)/src/lib/liblightmediascanner.la pkg_LTLIBRARIES = BUILT_SOURCES = EXTRA_DIST = SUBDIRS = -noinst_HEADERS = +noinst_HEADERS = shared/util.h if USE_MODULE_DUMMY pkg_LTLIBRARIES += dummy/dummy.la diff --git a/src/plugins/asf/asf.c b/src/plugins/asf/asf.c index 1cb6dea..0c2c793 100644 --- a/src/plugins/asf/asf.c +++ b/src/plugins/asf/asf.c @@ -449,7 +449,6 @@ _parse_stream_properties(int fd, struct asf_info *info) /* other fields are ignored */ } __attribute__((packed)) video; - unsigned int num, den; r = read(fd, &video, sizeof(video)); if (r != sizeof(video)) @@ -462,11 +461,7 @@ _parse_stream_properties(int fd, struct asf_info *info) s->base.codec = *_video_codec_id_to_str(video.compression_id); s->base.video.width = get_le32(&video.width); s->base.video.height = get_le32(&video.height); - - reduce_gcd(s->base.video.width, s->base.video.height, &num, &den); - asprintf(&s->base.video.aspect_ratio.str, "%u:%u", num, den); - s->base.video.aspect_ratio.len = s->base.video.aspect_ratio.str ? - strlen(s->base.video.aspect_ratio.str) : 0; + lms_stream_video_info_aspect_ratio_guess(&s->base.video); } _stream_copy_extension_properties(s); @@ -796,10 +791,9 @@ _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_in lms_string_size_strip_and_free(&info.genre); if (!info.title.str) - info.title = str_extract_name_from_path(finfo->path, finfo->path_len, - finfo->base, - &_exts[((long) match) - 1], - ctxt->cs_conv); + lms_name_from_path(&info.title, finfo->path, finfo->path_len, + finfo->base, _exts[((long) match) - 1].len, + ctxt->cs_conv); if (info.type == LMS_STREAM_TYPE_AUDIO) { struct lms_audio_info audio_info = { }; diff --git a/src/plugins/flac/flac.c b/src/plugins/flac/flac.c index 4857b21..4175b4a 100644 --- a/src/plugins/flac/flac.c +++ b/src/plugins/flac/flac.c @@ -26,7 +26,6 @@ #include #include -#include #include #include @@ -121,10 +120,9 @@ _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_in title_fallback: if (!info.title.str) - info.title = str_extract_name_from_path(finfo->path, finfo->path_len, - finfo->base, - &_exts[((long) match) - 1], - NULL); + lms_name_from_path(&info.title, finfo->path, finfo->path_len, + finfo->base, _exts[((long) match) - 1].len, + NULL); if (info.title.str) lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len); diff --git a/src/plugins/id3/id3.c b/src/plugins/id3/id3.c index b4ba169..de56dfc 100644 --- a/src/plugins/id3/id3.c +++ b/src/plugins/id3/id3.c @@ -1269,10 +1269,9 @@ _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_in } if (!info.title.str) - info.title = str_extract_name_from_path(finfo->path, finfo->path_len, - finfo->base, - &_exts[((long) match) - 1], - ctxt->cs_conv); + lms_name_from_path(&info.title, finfo->path, finfo->path_len, + finfo->base, _exts[((long) match) - 1].len, + ctxt->cs_conv); if (info.trackno == -1) info.trackno = 0; diff --git a/src/plugins/jpeg/jpeg.c b/src/plugins/jpeg/jpeg.c index 0915409..23c7aaf 100644 --- a/src/plugins/jpeg/jpeg.c +++ b/src/plugins/jpeg/jpeg.c @@ -681,10 +681,9 @@ _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_in info.date = finfo->mtime; if (!info.title.str) - info.title = str_extract_name_from_path(finfo->path, finfo->path_len, - finfo->base, - &_exts[((long) match) - 1], - ctxt->cs_conv); + lms_name_from_path(&info.title, finfo->path, finfo->path_len, + finfo->base, _exts[((long) match) - 1].len, + ctxt->cs_conv); if (info.artist.str) lms_charset_conv(ctxt->cs_conv, &info.artist.str, &info.artist.len); diff --git a/src/plugins/mp4/mp4.c b/src/plugins/mp4/mp4.c index b7c3322..d287fe5 100644 --- a/src/plugins/mp4/mp4.c +++ b/src/plugins/mp4/mp4.c @@ -28,7 +28,6 @@ static const char PV[] = PACKAGE_VERSION; /* mp4.h screws PACKAGE_VERSION */ #include #include -#include #include #include @@ -499,38 +498,6 @@ _get_lang(MP4FileHandle mp4_fh, MP4TrackId id) return ret; } -static struct lms_string_size -_guess_aspect_ratio(const struct lms_stream_video_info *info) -{ - static struct { - double ratio; - struct lms_string_size str; - } *itr, known_ratios[] = { - {16.0 / 9.0, LMS_STATIC_STRING_SIZE("16:9")}, - {4.0 / 3.0, LMS_STATIC_STRING_SIZE("4:3")}, - {3.0 / 2.0, LMS_STATIC_STRING_SIZE("3:2")}, - {5.0 / 3.0, LMS_STATIC_STRING_SIZE("5:3")}, - {8.0 / 5.0, LMS_STATIC_STRING_SIZE("8:5")}, - {1.85, LMS_STATIC_STRING_SIZE("1.85:1")}, - {1.4142, LMS_STATIC_STRING_SIZE("1.41:1")}, - {2.39, LMS_STATIC_STRING_SIZE("2.39:1")}, - {16.18 / 10.0, LMS_STATIC_STRING_SIZE("16.18:10")}, - {-1.0, {NULL, 0}} - }; - double ratio; - - if (info->width == 0 || info->height == 0) - return nullstr; - - ratio = (double)info->width / (double)info->height; - for (itr = known_ratios; itr->ratio > 0.0; itr++) { - if (fabs(ratio - itr->ratio) <= 0.01) - return itr->str; - } - - return nullstr; -} - static int _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match) { @@ -613,7 +580,7 @@ _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_in s->video.width = MP4GetTrackVideoWidth(mp4_fh, id); s->video.height = MP4GetTrackVideoHeight(mp4_fh, id); s->video.framerate = MP4GetTrackVideoFrameRate(mp4_fh, id); - s->video.aspect_ratio = _guess_aspect_ratio(&s->video); + lms_stream_video_info_aspect_ratio_guess(&s->video); } s->next = video_info.streams; @@ -629,10 +596,9 @@ _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_in lms_string_size_strip_and_free(&info.genre); if (!info.title.str) - info.title = str_extract_name_from_path(finfo->path, finfo->path_len, - finfo->base, - &_exts[((long) match) - 1], - NULL); + lms_name_from_path(&info.title, finfo->path, finfo->path_len, + finfo->base, _exts[((long) match) - 1].len, + NULL); if (info.title.str) lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len); if (info.artist.str) @@ -670,8 +636,10 @@ fail: while (video_info.streams) { struct lms_stream *s = video_info.streams; video_info.streams = s->next; - if (s->type == LMS_STREAM_TYPE_VIDEO) + if (s->type == LMS_STREAM_TYPE_VIDEO) { free(s->codec.str); /* ugly, but h264 needs alloc */ + free(s->video.aspect_ratio.str); + } free(s->lang.str); free(s); } diff --git a/src/plugins/ogg/ogg.c b/src/plugins/ogg/ogg.c index bac1749..5eef05a 100644 --- a/src/plugins/ogg/ogg.c +++ b/src/plugins/ogg/ogg.c @@ -33,7 +33,6 @@ #include #include #include -#include #include #include @@ -334,13 +333,7 @@ static void _parse_theora_and_vorbis_streams(struct ogg_info *info, num = s->video.ti.aspect_numerator; den = s->video.ti.aspect_denominator; - if (num && den) { - reduce_gcd(num, den, &num, &den); - asprintf(&s->base.video.aspect_ratio.str, "%u:%u", num, den); - s->base.video.aspect_ratio.len = - s->base.video.aspect_ratio.str ? - strlen(s->base.video.aspect_ratio.str) : 0; - } + lms_aspect_ratio_guess(&s->base.video.aspect_ratio, num, den); } } @@ -500,10 +493,9 @@ _parse(struct plugin *plugin, struct lms_context *ctxt, goto done; if (!info.title.str) - info.title = str_extract_name_from_path(finfo->path, finfo->path_len, - finfo->base, - &_exts[((long) match) - 1], - NULL); + lms_name_from_path(&info.title, finfo->path, finfo->path_len, + finfo->base, _exts[((long) match) - 1].len, + NULL); if (info.title.str) lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len); if (info.artist.str) diff --git a/src/plugins/png/png.c b/src/plugins/png/png.c index b07dff9..fedd67f 100644 --- a/src/plugins/png/png.c +++ b/src/plugins/png/png.c @@ -30,7 +30,6 @@ #include #include #include -#include #include #include @@ -173,10 +172,9 @@ _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_in info.date = finfo->mtime; if (!info.title.str) - info.title = str_extract_name_from_path(finfo->path, finfo->path_len, - finfo->base, - &_exts[((long) match) - 1], - NULL); + lms_name_from_path(&info.title, finfo->path, finfo->path_len, + finfo->base, _exts[((long) match) - 1].len, + NULL); if (info.title.str) lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len); if (info.artist.str) diff --git a/src/plugins/rm/rm.c b/src/plugins/rm/rm.c index 7b8a85a..6277f93 100644 --- a/src/plugins/rm/rm.c +++ b/src/plugins/rm/rm.c @@ -29,7 +29,6 @@ #include #include -#include #include #include @@ -457,10 +456,9 @@ _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_in lms_string_size_strip_and_free(&info.artist); if (!info.title.str) - info.title = str_extract_name_from_path(finfo->path, finfo->path_len, - finfo->base, - &_exts[((long) match) - 1], - NULL); + lms_name_from_path(&info.title, finfo->path, finfo->path_len, + finfo->base, _exts[((long) match) - 1].len, + NULL); if (info.title.str) lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len); diff --git a/src/lib/shared/util.h b/src/plugins/shared/util.h similarity index 88% rename from src/lib/shared/util.h rename to src/plugins/shared/util.h index 23ba004..5eac471 100644 --- a/src/lib/shared/util.h +++ b/src/plugins/shared/util.h @@ -25,7 +25,6 @@ #include #include -#include #define NSEC100_PER_SEC 10000000ULL #define MSEC_PER_SEC 1000ULL @@ -103,11 +102,3 @@ static inline uint16_t get_be16(const void *ptr) #else #error "Unknown byte order" #endif - - -struct lms_string_size str_extract_name_from_path( - const char *path, unsigned int pathlen, unsigned int baselen, - const struct lms_string_size *ext, struct lms_charset_conv *cs_conv); - -void reduce_gcd(unsigned int w, unsigned int h, unsigned int *dw, - unsigned int *dh); diff --git a/src/plugins/wave/wave.c b/src/plugins/wave/wave.c index 78ae7f9..71008bb 100644 --- a/src/plugins/wave/wave.c +++ b/src/plugins/wave/wave.c @@ -254,10 +254,9 @@ _parse(struct plugin *plugin, struct lms_context *ctxt, _parse_info(fd, &info); if (!info.title.str) - info.title = str_extract_name_from_path(finfo->path, finfo->path_len, - finfo->base, - &_exts[((long) match) - 1], - ctxt->cs_conv); + lms_name_from_path(&info.title, finfo->path, finfo->path_len, + finfo->base, _exts[((long) match) - 1].len, + ctxt->cs_conv); info.id = finfo->id;