2 * Copyright (C) 2013 by Intel Corporation
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 2.1 of
7 * the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19 * @author Leandro Dorileo <leandro.maciel.dorileo@intel.com>
22 #include <libavformat/avformat.h>
23 #include <libavutil/dict.h>
24 #include "libavutil/opt.h"
26 #include <lightmediascanner_db.h>
27 #include <lightmediascanner_plugin.h>
29 #include <sys/types.h>
37 #define DECL_STR(cname, str) \
38 static const struct lms_string_size cname = LMS_STATIC_STRING_SIZE(str)
40 static const char _name[] = "generic";
42 static const struct lms_string_size _exts[] = {
43 LMS_STATIC_STRING_SIZE(".mpeg"),
44 LMS_STATIC_STRING_SIZE(".mpg"),
45 LMS_STATIC_STRING_SIZE(".mpeg2"),
46 LMS_STATIC_STRING_SIZE(".mp2"),
47 LMS_STATIC_STRING_SIZE(".mp3"),
48 LMS_STATIC_STRING_SIZE(".adts"),
49 LMS_STATIC_STRING_SIZE(".m3u"),
50 LMS_STATIC_STRING_SIZE(".mp4"),
51 LMS_STATIC_STRING_SIZE(".wma"),
54 DECL_STR(_codec_mpeg1layer3, "mpeg1layer3");
55 DECL_STR(_container_3gp, "3gp");
56 DECL_STR(_container_mp4, "mp4");
58 DECL_STR(_container_audio_wmav1, "wmav1");
59 DECL_STR(_container_audio_wmav2, "wmav2");
60 DECL_STR(_container_audio_wmavpro, "wmavpro");
62 DECL_STR(_codec_audio_asf, "asf");
63 DECL_STR(_codec_audio_mpeg4aac_main, "mpeg4aac-main");
64 DECL_STR(_codec_audio_mpeg4aac_lc, "mpeg4aac-lc");
65 DECL_STR(_codec_audio_mpeg4aac_ssr, "mpeg4aac-ssr");
66 DECL_STR(_codec_audio_mpeg4aac_ltp, "mpeg4aac-ltp");
67 DECL_STR(_codec_audio_mpeg4aac_he, "mpeg4aac-he");
68 DECL_STR(_codec_audio_mpeg4aac_scalable, "mpeg4aac-scalable");
70 typedef void (* generic_codec_container_cb)(AVStream *stream, struct lms_string_size *value);
72 struct codec_container {
74 generic_codec_container_cb get_codec;
75 generic_codec_container_cb get_container;
78 struct codec_container_descriptor {
80 const struct lms_string_size *desc;
83 static const struct codec_container_descriptor _codec_list[] = {
84 {CODEC_ID_MP3, &_codec_mpeg1layer3},
85 {AV_CODEC_ID_WMAV1, &_codec_audio_asf},
86 {AV_CODEC_ID_WMAV2, &_codec_audio_asf},
87 {AV_CODEC_ID_WMAPRO, &_codec_audio_asf},
90 static const struct codec_container_descriptor _container_list[] = {
91 {AV_CODEC_ID_MPEG2VIDEO, &_container_mp4},
92 {AV_CODEC_ID_AAC, &_container_3gp},
93 {AV_CODEC_ID_WMAV1, &_container_audio_wmav1},
94 {AV_CODEC_ID_WMAV2, &_container_audio_wmav2},
95 {AV_CODEC_ID_WMAPRO, &_container_audio_wmavpro},
96 {AV_CODEC_ID_H264, &_container_mp4},
100 _mp4_get_audio_codec(AVStream *stream, struct lms_string_size *value)
102 switch (stream->codec->profile) {
103 case FF_PROFILE_AAC_MAIN:
104 lms_string_size_dup(value, &_codec_audio_mpeg4aac_main);
106 case FF_PROFILE_AAC_LOW:
107 lms_string_size_dup(value, &_codec_audio_mpeg4aac_lc);
109 case FF_PROFILE_AAC_SSR:
110 lms_string_size_dup(value, &_codec_audio_mpeg4aac_ssr);
112 case FF_PROFILE_AAC_LTP:
113 lms_string_size_dup(value, &_codec_audio_mpeg4aac_ltp);
115 case FF_PROFILE_AAC_HE:
116 lms_string_size_dup(value, &_codec_audio_mpeg4aac_he);
119 lms_string_size_dup(value, &_codec_audio_mpeg4aac_scalable);
124 /** TODO: for mp4 we're parsing a smaller subset of codec than mp4 plugin itself */
126 _mp4_get_video_codec(AVStream *stream, struct lms_string_size *value)
128 struct lms_string_size ret = {};
129 char str_profile[64], str_level[64], buf[256];
132 profile = stream->codec->profile;
133 level = stream->codec->level;
137 memcpy(str_profile, "baseline", sizeof("baseline"));
140 memcpy(str_profile, "main", sizeof("main"));
143 memcpy(str_profile, "extended", sizeof("extended"));
146 memcpy(str_profile, "high", sizeof("high"));
149 memcpy(str_profile, "high-10", sizeof("high-10"));
152 memcpy(str_profile, "high-422", sizeof("high-422"));
155 memcpy(str_profile, "high-444", sizeof("high-444"));
158 snprintf(str_profile, sizeof(str_profile), "unknown-%d", profile);
162 snprintf(str_level, sizeof(str_level), "%u", level / 10);
164 snprintf(str_level, sizeof(str_level), "%u.%u", level / 10, level % 10);
166 ret.len = snprintf(buf, sizeof(buf), "h264-p%s-l%s", str_profile, str_level);
169 lms_string_size_dup(value, &ret);
173 _mpeg2_get_video_codec(AVStream *stream, struct lms_string_size *value)
175 const char *str_profile, *str_level;
176 const AVCodecDescriptor *codec;
179 codec = avcodec_descriptor_get(stream->codec->codec_id);
180 if (!codec || !codec->name) return;
185 switch (stream->codec->profile) {
187 str_profile = "simple";
190 str_profile = "main";
194 switch (stream->codec->level) {
208 snprintf(buf, sizeof(buf), "%s-p%s-l%s", codec->name, str_profile,
210 lms_string_size_strndup(value, buf, -1);
214 _get_common_codec(AVStream *stream, struct lms_string_size *value)
218 length = sizeof(_codec_list) / sizeof(struct codec_container_descriptor);
219 for (i = 0; i < length; i++) {
220 const struct codec_container_descriptor *curr = _codec_list + i;
221 if (curr->id == stream->codec->codec_id) {
222 lms_string_size_dup(value, curr->desc);
229 _get_common_container(AVStream *stream, struct lms_string_size *value)
233 length = sizeof(_container_list) / sizeof(struct codec_container_descriptor);
234 for (i = 0; i < length; i++) {
235 const struct codec_container_descriptor *curr = _container_list + i;
236 if (curr->id == stream->codec->codec_id) {
237 lms_string_size_dup(value, curr->desc);
243 static const struct codec_container _codecs[] = {
246 .get_codec = _get_common_codec,
247 .get_container = NULL,
250 .id = AV_CODEC_ID_AAC,
251 .get_codec = _mp4_get_audio_codec,
252 .get_container = _get_common_container,
255 .id = AV_CODEC_ID_MPEG2VIDEO,
256 .get_codec = _mpeg2_get_video_codec,
257 .get_container = NULL,
260 .id = AV_CODEC_ID_MPEG2VIDEO,
261 .get_codec = _mp4_get_video_codec,
262 .get_container = _get_common_container,
265 .id = AV_CODEC_ID_H264,
266 .get_codec = _mp4_get_video_codec,
267 .get_container = _get_common_container,
270 .id = AV_CODEC_ID_WMAV1,
271 .get_codec = _get_common_codec,
272 .get_container = _get_common_container,
275 .id = AV_CODEC_ID_WMAV2,
276 .get_codec = _get_common_codec,
277 .get_container = _get_common_container,
280 .id = AV_CODEC_ID_WMAPRO,
281 .get_codec = _get_common_codec,
282 .get_container = _get_common_container,
286 static const char *_cats[] = {
293 static const char *_authors[] = {
299 struct lms_plugin plugin;
300 lms_db_audio_t *audio_db;
301 lms_db_video_t *video_db;
305 struct lms_string_size title;
306 struct lms_string_size artist;
307 struct lms_string_size album;
308 struct lms_string_size genre;
312 _match(struct plugin *p, const char *path, int len, int base)
316 i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
320 return (void*)(i + 1);
324 _get_dict_value(AVDictionary *dict, const char *key)
326 AVDictionaryEntry *tag = NULL;
327 tag = av_dict_get(dict, key, tag, AV_DICT_IGNORE_SUFFIX);
328 if (!tag) return NULL;
332 static const struct codec_container *
333 _find_codec_container(unsigned int codec_id)
336 length = sizeof(_codecs) / sizeof(struct codec_container);
338 for (i = 0; i < length; i++) {
339 const struct codec_container *curr = &_codecs[i];
340 if (curr->id == codec_id) return curr;
347 _get_codec(AVStream *stream, struct lms_string_size *value)
349 const struct codec_container *cc;
351 cc = _find_codec_container(stream->codec->codec_id);
352 if (!cc || !cc->get_codec) return;
354 cc->get_codec(stream, value);
358 _get_container(AVStream *stream, struct lms_string_size *value)
360 const struct codec_container *cc;
362 cc = _find_codec_container(stream->codec->codec_id);
363 if (!cc || !cc->get_container) return;
365 cc->get_container(stream, value);
369 _get_stream_duration(AVFormatContext *fmt_ctx)
372 if (fmt_ctx->duration == AV_NOPTS_VALUE) return 0;
374 duration = fmt_ctx->duration + 5000;
375 return (duration / AV_TIME_BASE);
379 _parse_audio_stream(AVFormatContext *fmt_ctx, struct lms_audio_info *info, AVStream *stream)
381 AVCodecContext *ctx = stream->codec;
383 info->bitrate = ctx->bit_rate;
384 info->channels = ctx->channels;
387 info->channels = av_get_channel_layout_nb_channels(ctx->channel_layout);
389 info->sampling_rate = ctx->sample_rate;
390 info->length = _get_stream_duration(fmt_ctx);
392 _get_codec(stream, &info->codec);
396 _parse_video_stream(AVFormatContext *fmt_ctx, struct lms_video_info *info, AVStream *stream, char *language)
398 char aspect_ratio[256];
399 struct lms_stream *s;
400 AVCodecContext *ctx = stream->codec;
402 s = calloc(1, sizeof(*s));
405 s->stream_id = (unsigned int)stream->id;
406 lms_string_size_strndup(&s->lang, language, -1);
408 s->type = LMS_STREAM_TYPE_VIDEO;
410 _get_codec(stream, &s->codec);
412 s->video.bitrate = ctx->bit_rate;
413 if (!s->video.bitrate)
414 s->video.bitrate = ctx->rc_max_rate;
416 s->video.width = ctx->width;
417 s->video.height = ctx->height;
419 if (stream->r_frame_rate.den)
420 s->video.framerate = stream->r_frame_rate.num / stream->r_frame_rate.den;
422 snprintf(aspect_ratio, sizeof(aspect_ratio), "%d:%d",
423 ctx->sample_aspect_ratio.num, ctx->sample_aspect_ratio.den);
425 lms_string_size_strndup(&s->video.aspect_ratio, aspect_ratio, -1);
427 s->next = info->streams;
429 info->length = _get_stream_duration(fmt_ctx);
433 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
436 int64_t packet_size = 0;
438 AVFormatContext *fmt_ctx = NULL;
439 struct mpeg_info info = { };
440 char *metadata, *language;
441 struct lms_audio_info audio_info = { };
442 struct lms_video_info video_info = { };
443 struct lms_string_size container = { };
449 if ((ret = avformat_open_input(&fmt_ctx, finfo->path, NULL, NULL)))
452 if (avformat_find_stream_info(fmt_ctx, NULL) < 0)
455 metadata = _get_dict_value(fmt_ctx->metadata, "title");
457 lms_string_size_strndup(&info.title, metadata, -1);
459 metadata = _get_dict_value(fmt_ctx->metadata, "artist");
461 lms_string_size_strndup(&info.artist, metadata, -1);
463 metadata = _get_dict_value(fmt_ctx->metadata, "album");
465 lms_string_size_strndup(&info.album, metadata, -1);
467 metadata = _get_dict_value(fmt_ctx->metadata, "genre");
469 lms_string_size_strndup(&info.genre, metadata, -1);
471 av_opt_get_int(fmt_ctx, "ts_packetsize",
472 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ,
475 language = _get_dict_value(fmt_ctx->metadata, "language");
477 for (i = 0; i < fmt_ctx->nb_streams; i++) {
478 AVStream *stream = fmt_ctx->streams[i];
479 AVCodecContext *ctx = stream->codec;
482 if (ctx->codec_id == AV_CODEC_ID_PROBE) {
483 printf("Failed to probe codec for input stream %d\n", stream->index);
487 if (!(codec = avcodec_find_decoder(ctx->codec_id))) {
488 printf("Unsupported codec with id %d for input stream %d\n",
489 stream->codec->codec_id, stream->index);
493 _get_container(stream, &container);
495 if (ctx->codec_type == AVMEDIA_TYPE_AUDIO)
496 _parse_audio_stream(fmt_ctx, &audio_info, stream);
497 else if (ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
498 _parse_video_stream(fmt_ctx, &video_info, stream, language);
503 lms_string_size_strip_and_free(&info.title);
504 lms_string_size_strip_and_free(&info.artist);
505 lms_string_size_strip_and_free(&info.album);
506 lms_string_size_strip_and_free(&info.genre);
509 lms_name_from_path(&info.title, finfo->path, finfo->path_len,
510 finfo->base, _exts[((long) match) - 1].len,
514 lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
516 lms_charset_conv(ctxt->cs_conv, &info.artist.str, &info.artist.len);
518 lms_charset_conv(ctxt->cs_conv, &info.album.str, &info.album.len);
520 lms_charset_conv(ctxt->cs_conv, &info.genre.str, &info.genre.len);
524 lms_string_size_strndup(&container, fmt_ctx->iformat->name, -1);
527 video_info.id = finfo->id;
528 video_info.title = info.title;
529 video_info.artist = info.artist;
530 video_info.container = container;
531 video_info.packet_size = packet_size;
533 ret = lms_db_video_add(plugin->video_db, &video_info);
535 audio_info.id = finfo->id;
536 audio_info.title = info.title;
537 audio_info.artist = info.artist;
538 audio_info.album = info.album;
539 audio_info.genre = info.genre;
540 audio_info.container = container;
542 ret = lms_db_audio_add(plugin->audio_db, &audio_info);
543 lms_string_size_strip_and_free(&audio_info.codec);
546 free(info.title.str);
547 free(info.artist.str);
548 free(info.album.str);
549 free(info.genre.str);
552 while (video_info.streams) {
553 struct lms_stream *s = video_info.streams;
554 video_info.streams = s->next;
556 if (s->type == LMS_STREAM_TYPE_VIDEO) {
557 free(s->video.aspect_ratio.str);
564 avformat_close_input(&fmt_ctx);
569 _close(struct plugin *plugin)
576 _setup(struct plugin *plugin, struct lms_context *ctxt)
579 plugin->audio_db = lms_db_audio_new(ctxt->db);
580 if (!plugin->audio_db)
582 plugin->video_db = lms_db_video_new(ctxt->db);
583 if (!plugin->video_db)
590 _start(struct plugin *plugin, struct lms_context *ctxt)
593 r = lms_db_audio_start(plugin->audio_db);
594 r |= lms_db_video_start(plugin->video_db);
599 _finish(struct plugin *plugin, struct lms_context *ctxt)
601 if (plugin->audio_db)
602 lms_db_audio_free(plugin->audio_db);
603 if (plugin->video_db)
604 lms_db_video_free(plugin->video_db);
609 API struct lms_plugin *
610 lms_plugin_open(void)
612 struct plugin *plugin;
614 plugin = (struct plugin *)malloc(sizeof(*plugin));
615 plugin->plugin.name = _name;
616 plugin->plugin.match = (lms_plugin_match_fn_t)_match;
617 plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
618 plugin->plugin.close = (lms_plugin_close_fn_t)_close;
619 plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
620 plugin->plugin.start = (lms_plugin_start_fn_t)_start;
621 plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
622 plugin->plugin.order = INT32_MAX;
624 return (struct lms_plugin *)plugin;
627 API const struct lms_plugin_info *
628 lms_plugin_info(void)
630 static struct lms_plugin_info info = {
636 "http://github.com/profusion/lightmediascanner"