generic: remove the dlna stuff and add codec container handlers
[platform/upstream/lightmediascanner.git] / src / plugins / generic / generic.c
1 /**
2  * Copyright (C) 2013 by Intel Corporation
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301 USA
18  *
19  * @author Leandro Dorileo <leandro.maciel.dorileo@intel.com>
20  */
21
22 #include <libavformat/avformat.h>
23 #include <libavutil/dict.h>
24 #include "libavutil/opt.h"
25
26 #include <lightmediascanner_db.h>
27 #include <lightmediascanner_plugin.h>
28
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <limits.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36
37 #define DECL_STR(cname, str)                                            \
38     static const struct lms_string_size cname = LMS_STATIC_STRING_SIZE(str)
39
40 static const char _name[] = "generic";
41
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"),
52 };
53
54 DECL_STR(_codec_mpeg1layer3, "mpeg1layer3");
55 DECL_STR(_container_3gp, "3gp");
56 DECL_STR(_container_mp4, "mp4");
57
58 DECL_STR(_container_audio_wmav1, "wmav1");
59 DECL_STR(_container_audio_wmav2, "wmav2");
60 DECL_STR(_container_audio_wmavpro, "wmavpro");
61
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");
69
70 typedef void (* generic_codec_container_cb)(AVStream *stream, struct lms_string_size *value);
71
72 struct codec_container {
73     unsigned int id;
74     generic_codec_container_cb get_codec;
75     generic_codec_container_cb get_container;
76 };
77
78 struct codec_container_descriptor {
79     unsigned int id;
80     const struct lms_string_size *desc;
81 };
82
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},
88 };
89
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},
97 };
98
99 static void
100 _mp4_get_audio_codec(AVStream *stream, struct lms_string_size *value)
101 {
102     switch (stream->codec->profile) {
103     case FF_PROFILE_AAC_MAIN:
104         lms_string_size_dup(value, &_codec_audio_mpeg4aac_main);
105         break;
106     case FF_PROFILE_AAC_LOW:
107         lms_string_size_dup(value, &_codec_audio_mpeg4aac_lc);
108         break;
109     case FF_PROFILE_AAC_SSR:
110         lms_string_size_dup(value, &_codec_audio_mpeg4aac_ssr);
111         break;
112     case FF_PROFILE_AAC_LTP:
113         lms_string_size_dup(value, &_codec_audio_mpeg4aac_ltp);
114         break;
115     case FF_PROFILE_AAC_HE:
116         lms_string_size_dup(value, &_codec_audio_mpeg4aac_he);
117         break;
118     default:
119         lms_string_size_dup(value, &_codec_audio_mpeg4aac_scalable);
120         break;
121     }
122 }
123
124 /** TODO: for mp4 we're parsing a smaller subset of codec than mp4 plugin itself */
125 static void
126 _mp4_get_video_codec(AVStream *stream, struct lms_string_size *value)
127 {
128     struct lms_string_size ret = {};
129     char str_profile[64], str_level[64], buf[256];
130     int level, profile;
131
132     profile = stream->codec->profile;
133     level = stream->codec->level;
134
135     switch (profile) {
136     case 66:
137         memcpy(str_profile, "baseline", sizeof("baseline"));
138         break;
139     case 77:
140         memcpy(str_profile, "main", sizeof("main"));
141         break;
142     case 88:
143         memcpy(str_profile, "extended", sizeof("extended"));
144         break;
145     case 100:
146         memcpy(str_profile, "high", sizeof("high"));
147         break;
148     case 110:
149         memcpy(str_profile, "high-10", sizeof("high-10"));
150         break;
151     case 122:
152         memcpy(str_profile, "high-422", sizeof("high-422"));
153         break;
154     case 144:
155         memcpy(str_profile, "high-444", sizeof("high-444"));
156         break;
157     default:
158         snprintf(str_profile, sizeof(str_profile), "unknown-%d", profile);
159     }
160
161     if (level % 10 == 0)
162         snprintf(str_level, sizeof(str_level), "%u", level / 10);
163     else
164         snprintf(str_level, sizeof(str_level), "%u.%u", level / 10, level % 10);
165
166     ret.len = snprintf(buf, sizeof(buf), "h264-%s-l%s", str_profile, str_level);
167     ret.str = buf;
168
169     lms_string_size_dup(value, &ret);
170 }
171
172 static void
173 _mpeg2_get_video_codec(AVStream *stream, struct lms_string_size *value)
174 {
175     const char *codec_name, *str_profile, *str_level;
176     char buf[256];
177
178     codec_name = avcodec_get_name(stream->codec->codec_id);
179     if (!codec_name) return;
180
181     str_profile = NULL;
182     str_level = NULL;
183
184     switch (stream->codec->profile) {
185     case 6:
186         str_profile = "simple";
187         break;
188     case 4:
189         str_profile = "main";
190         break;
191     }
192
193     switch (stream->codec->level) {
194     case 5:
195     case 8:
196         str_level = "main";
197         break;
198     case 2:
199     case 4:
200         str_level = "high";
201         break;
202     case 6:
203         str_level = "high";
204         break;
205     }
206
207     snprintf(buf, sizeof(buf), "%s-p%s-l%s", codec_name, str_profile, str_level);
208     lms_string_size_strndup(value, buf, -1);
209 }
210
211 static void
212 _get_common_codec(AVStream *stream, struct lms_string_size *value)
213 {
214     int length, i;
215
216     length = sizeof(_codec_list) / sizeof(struct codec_container_descriptor);
217     for (i = 0; i < length; i++) {
218         const struct codec_container_descriptor *curr = _codec_list + i;
219         if (curr->id == stream->codec->codec_id) {
220             lms_string_size_dup(value, curr->desc);
221             break;
222         }
223     }
224 }
225
226 static void
227 _get_common_container(AVStream *stream, struct lms_string_size *value)
228 {
229     int length, i;
230
231     length = sizeof(_container_list) / sizeof(struct codec_container_descriptor);
232     for (i = 0; i < length; i++) {
233         const struct codec_container_descriptor *curr = _container_list + i;
234         if (curr->id == stream->codec->codec_id) {
235             lms_string_size_dup(value, curr->desc);
236             break;
237         }
238     }
239 }
240
241 static const struct codec_container _codecs[] = {
242     {
243         .id = CODEC_ID_MP3,
244         .get_codec = _get_common_codec,
245         .get_container = NULL,
246     },
247     {
248         .id = AV_CODEC_ID_AAC,
249         .get_codec = _mp4_get_audio_codec,
250         .get_container = _get_common_container,
251     },
252     {
253         .id = AV_CODEC_ID_MPEG2VIDEO,
254         .get_codec = _mpeg2_get_video_codec,
255         .get_container = NULL,
256     },
257     {
258         .id = AV_CODEC_ID_MPEG2VIDEO,
259         .get_codec = _mp4_get_video_codec,
260         .get_container = _get_common_container,
261     },
262     {
263         .id = AV_CODEC_ID_H264,
264         .get_codec = _mp4_get_video_codec,
265         .get_container = _get_common_container,
266     },
267     {
268         .id = AV_CODEC_ID_WMAV1,
269         .get_codec = _get_common_codec,
270         .get_container = _get_common_container,
271     },
272     {
273         .id = AV_CODEC_ID_WMAV2,
274         .get_codec = _get_common_codec,
275         .get_container = _get_common_container,
276     },
277     {
278         .id = AV_CODEC_ID_WMAPRO,
279         .get_codec = _get_common_codec,
280         .get_container = _get_common_container,
281     },
282 };
283
284 static const char *_cats[] = {
285     "multimedia",
286     "audio",
287     "video",
288     NULL
289 };
290
291 static const char *_authors[] = {
292     "Leandro Dorileo",
293     NULL
294 };
295
296 struct plugin {
297     struct lms_plugin plugin;
298     lms_db_audio_t *audio_db;
299     lms_db_video_t *video_db;
300 };
301
302 struct mpeg_info {
303     struct lms_string_size title;
304     struct lms_string_size artist;
305     struct lms_string_size album;
306     struct lms_string_size genre;
307 };
308
309 static void *
310 _match(struct plugin *p, const char *path, int len, int base)
311 {
312     long i;
313
314     i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
315     if (i < 0)
316       return NULL;
317     else
318       return (void*)(i + 1);
319 }
320
321 static char *
322 _get_dict_value(AVDictionary *dict, const char *key)
323 {
324     AVDictionaryEntry *tag = NULL;
325     tag = av_dict_get(dict, key, tag, AV_DICT_IGNORE_SUFFIX);
326     if (!tag) return NULL;
327     return tag->value;
328 }
329
330 static const struct codec_container *
331 _find_codec_container(unsigned int codec_id)
332 {
333     int i, length;
334     length = sizeof(_codecs) / sizeof(struct codec_container);
335
336     for (i = 0; i < length; i++) {
337       const struct codec_container *curr = &_codecs[i];
338       if (curr->id == codec_id) return curr;
339     }
340
341     return NULL;
342 }
343
344 static void
345 _get_codec(AVStream *stream, struct lms_string_size *value)
346 {
347     const struct codec_container *cc;
348
349     cc = _find_codec_container(stream->codec->codec_id);
350     if (!cc || !cc->get_codec) return;
351
352     cc->get_codec(stream, value);
353 }
354
355 static void
356 _get_container(AVStream *stream, struct lms_string_size *value)
357 {
358     const struct codec_container *cc;
359
360     cc = _find_codec_container(stream->codec->codec_id);
361     if (!cc || !cc->get_container) return;
362
363     cc->get_container(stream, value);
364 }
365
366 static int
367 _get_stream_duration(AVFormatContext *fmt_ctx)
368 {
369     int64_t duration;
370     if (fmt_ctx->duration == AV_NOPTS_VALUE) return 0;
371
372     duration = fmt_ctx->duration + 5000;
373     return (duration / AV_TIME_BASE);
374 }
375
376 static void
377 _parse_audio_stream(AVFormatContext *fmt_ctx, struct lms_audio_info *info, AVStream *stream)
378 {
379     AVCodecContext *ctx = stream->codec;
380
381     info->bitrate = ctx->bit_rate;
382     info->channels = av_get_channel_layout_nb_channels(ctx->channel_layout);
383     info->sampling_rate = ctx->sample_rate;
384     info->length = _get_stream_duration(fmt_ctx);
385
386     _get_codec(stream, &info->codec);
387 }
388
389 static void
390 _parse_video_stream(AVFormatContext *fmt_ctx, struct lms_video_info *info, AVStream *stream, char *language)
391 {
392     char aspect_ratio[256];
393     struct lms_stream *s;
394     AVCodecContext *ctx = stream->codec;
395
396     s = calloc(1, sizeof(*s));
397     if (!s) return;
398
399     s->stream_id = (unsigned int)stream->id;
400     lms_string_size_strndup(&s->lang, language, -1);
401
402     s->type = LMS_STREAM_TYPE_VIDEO;
403
404     _get_codec(stream, &s->codec);
405
406     s->video.bitrate = ctx->bit_rate;
407     if (!s->video.bitrate)
408         s->video.bitrate = ctx->rc_max_rate;
409
410     s->video.width = ctx->width;
411     s->video.height = ctx->height;
412
413     if (stream->r_frame_rate.den)
414         s->video.framerate = stream->r_frame_rate.num / stream->r_frame_rate.den;
415
416     snprintf(aspect_ratio, sizeof(aspect_ratio), "%d:%d",
417              ctx->sample_aspect_ratio.num, ctx->sample_aspect_ratio.den);
418
419     lms_string_size_strndup(&s->video.aspect_ratio, aspect_ratio, -1);
420
421     s->next = info->streams;
422     info->streams = s;
423     info->length = _get_stream_duration(fmt_ctx);
424 }
425
426 static int
427 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
428 {
429     int ret;
430     int64_t packet_size = 0;
431     unsigned int i;
432     AVFormatContext *fmt_ctx = NULL;
433     struct mpeg_info info = { };
434     char *metadata, *language;
435     struct lms_audio_info audio_info = { };
436     struct lms_video_info video_info = { };
437     struct lms_string_size container = { };
438     bool video = false;
439
440     if ((ret = avformat_open_input(&fmt_ctx, finfo->path, NULL, NULL)))
441         return ret;
442
443     if (avformat_find_stream_info(fmt_ctx, NULL) < 0)
444         goto fail;
445
446     metadata = _get_dict_value(fmt_ctx->metadata, "title");
447     if (metadata)
448         lms_string_size_strndup(&info.title, metadata, -1);
449
450     metadata = _get_dict_value(fmt_ctx->metadata, "artist");
451     if (metadata)
452         lms_string_size_strndup(&info.artist, metadata, -1);
453
454     metadata = _get_dict_value(fmt_ctx->metadata, "album");
455     if (metadata)
456         lms_string_size_strndup(&info.album, metadata, -1);
457
458     metadata = _get_dict_value(fmt_ctx->metadata, "genre");
459     if (metadata)
460         lms_string_size_strndup(&info.genre, metadata, -1);
461
462     av_opt_get_int(fmt_ctx, "ts_packetsize", AV_OPT_SEARCH_CHILDREN,
463                    &packet_size);
464
465     language = _get_dict_value(fmt_ctx->metadata, "language");
466
467     for (i = 0; i < fmt_ctx->nb_streams; i++) {
468         AVStream *stream = fmt_ctx->streams[i];
469         AVCodecContext *ctx = stream->codec;
470         AVCodec *codec;
471
472         if (ctx->codec_id == AV_CODEC_ID_PROBE) {
473             printf("Failed to probe codec for input stream %d\n", stream->index);
474             continue;
475         }
476         
477         if (!(codec = avcodec_find_decoder(ctx->codec_id))) {
478             printf("Unsupported codec with id %d for input stream %d\n",
479                    stream->codec->codec_id, stream->index);
480             continue;
481         }
482
483         _get_container(stream, &container);
484
485         if (ctx->codec_type == AVMEDIA_TYPE_AUDIO)
486             _parse_audio_stream(fmt_ctx, &audio_info, stream);
487         else if (ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
488             _parse_video_stream(fmt_ctx, &video_info, stream, language);
489             video = true;
490         }
491     }
492
493     lms_string_size_strip_and_free(&info.title);
494     lms_string_size_strip_and_free(&info.artist);
495     lms_string_size_strip_and_free(&info.album);
496     lms_string_size_strip_and_free(&info.genre);
497
498     if (!info.title.str)
499         lms_name_from_path(&info.title, finfo->path, finfo->path_len,
500                            finfo->base, _exts[((long) match) - 1].len,
501                            NULL);
502
503     if (info.title.str)
504         lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
505     if (info.artist.str)
506         lms_charset_conv(ctxt->cs_conv, &info.artist.str, &info.artist.len);
507     if (info.album.str)
508       lms_charset_conv(ctxt->cs_conv, &info.album.str, &info.album.len);
509     if (info.genre.str)
510         lms_charset_conv(ctxt->cs_conv, &info.genre.str, &info.genre.len);
511
512
513     if (!container.str)
514         lms_string_size_strndup(&container, fmt_ctx->iformat->name, -1);
515
516     if (video) {
517         video_info.id = finfo->id;
518         video_info.title = info.title;
519         video_info.artist = info.artist;
520         video_info.container = container;
521         video_info.packet_size = packet_size;
522
523         ret = lms_db_video_add(plugin->video_db, &video_info);
524     } else {
525         audio_info.id = finfo->id;
526         audio_info.title = info.title;
527         audio_info.artist = info.artist;
528         audio_info.album = info.album;
529         audio_info.genre = info.genre;
530         audio_info.container = container;
531
532         ret = lms_db_audio_add(plugin->audio_db, &audio_info);
533         lms_string_size_strip_and_free(&audio_info.codec);
534     }
535
536     free(info.title.str);
537     free(info.artist.str);
538     free(info.album.str);
539     free(info.genre.str);
540     free(container.str);
541
542     while (video_info.streams) {
543         struct lms_stream *s = video_info.streams;
544         video_info.streams = s->next;
545         free(s->codec.str);
546         if (s->type == LMS_STREAM_TYPE_VIDEO) {
547             free(s->video.aspect_ratio.str);
548         }
549         free(s->lang.str);
550         free(s);
551     }
552
553  fail:
554     avformat_close_input(&fmt_ctx);
555     return ret;
556 }
557
558 static int
559 _close(struct plugin *plugin)
560 {
561     free(plugin);
562     return 0;
563 }
564
565 static int
566 _setup(struct plugin *plugin,  struct lms_context *ctxt)
567 {
568     av_register_all();
569     plugin->audio_db = lms_db_audio_new(ctxt->db);
570     if (!plugin->audio_db)
571         return -1;
572     plugin->video_db = lms_db_video_new(ctxt->db);
573     if (!plugin->video_db)
574         return -1;
575
576     return 0;
577 }
578
579 static int
580 _start(struct plugin *plugin, struct lms_context *ctxt)
581 {
582     int r;
583     r = lms_db_audio_start(plugin->audio_db);
584     r |= lms_db_video_start(plugin->video_db);
585     return r;
586 }
587
588 static int
589 _finish(struct plugin *plugin, struct lms_context *ctxt)
590 {
591     if (plugin->audio_db)
592         lms_db_audio_free(plugin->audio_db);
593     if (plugin->video_db)
594         lms_db_video_free(plugin->video_db);
595
596     return 0;
597 }
598
599 API struct lms_plugin *
600 lms_plugin_open(void)
601 {
602     struct plugin *plugin;
603
604     plugin = (struct plugin *)malloc(sizeof(*plugin));
605     plugin->plugin.name = _name;
606     plugin->plugin.match = (lms_plugin_match_fn_t)_match;
607     plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
608     plugin->plugin.close = (lms_plugin_close_fn_t)_close;
609     plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
610     plugin->plugin.start = (lms_plugin_start_fn_t)_start;
611     plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
612     plugin->plugin.order = INT32_MAX;
613
614     return (struct lms_plugin *)plugin;
615 }
616
617 API const struct lms_plugin_info *
618 lms_plugin_info(void)
619 {
620     static struct lms_plugin_info info = {
621         _name,
622         _cats,
623         "libavcodec",
624         PACKAGE_VERSION,
625         _authors,
626         "http://github.com/profusion/lightmediascanner"
627     };
628
629     return &info;
630 }