plugins/asf: Add channels, bitrate, sampling_rate
[platform/upstream/lightmediascanner.git] / src / plugins / asf / asf.c
1 /**
2  * Copyright (C) 2008 by INdT
3  *
4  * This program 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
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * @author Andre Moreira Magalhaes <andre.magalhaes@openbossa.org>
19  */
20
21 /**
22  * @brief
23  *
24  * asf/wma file parser.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #define _XOPEN_SOURCE 600
32 #define _BSD_SOURCE
33 #include <lightmediascanner_plugin.h>
34 #include <lightmediascanner_db.h>
35 #include <endian.h>
36 #include <errno.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <fcntl.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44
45 #define NSEC100_PER_SEC  10000000ULL
46 #define MSEC_PER_SEC  1000ULL
47
48 enum StreamTypes {
49     STREAM_TYPE_UNKNOWN = 0,
50     STREAM_TYPE_AUDIO,
51     STREAM_TYPE_VIDEO
52 };
53
54 enum AttributeTypes {
55     ATTR_TYPE_UNICODE = 0,
56     ATTR_TYPE_BYTES,
57     ATTR_TYPE_BOOL,
58     ATTR_TYPE_DWORD,
59     ATTR_TYPE_QWORD,
60     ATTR_TYPE_WORD,
61     ATTR_TYPE_GUID
62 };
63
64 struct asf_info {
65     struct lms_string_size title;
66     struct lms_string_size artist;
67     struct lms_string_size album;
68     struct lms_string_size genre;
69     unsigned char trackno;
70 };
71
72 struct plugin {
73     struct lms_plugin plugin;
74     lms_db_audio_t *audio_db;
75     lms_db_video_t *video_db;
76     lms_charset_conv_t *cs_conv;
77 };
78
79 static const char _name[] = "asf";
80 static const struct lms_string_size _container = LMS_STATIC_STRING_SIZE("asf");
81 static const struct lms_string_size _exts[] = {
82     LMS_STATIC_STRING_SIZE(".wma"),
83     LMS_STATIC_STRING_SIZE(".wmv"),
84     LMS_STATIC_STRING_SIZE(".asf")
85 };
86 static const char *_cats[] = {
87     "multimedia",
88     "audio",
89     NULL
90 };
91 static const char *_authors[] = {
92     "Andre Moreira Magalhaes",
93     NULL
94 };
95
96 struct stream {
97     int id;
98     enum StreamTypes type;
99     struct stream *next;
100     union {
101         struct {
102             uint16_t codec_id;
103             uint16_t channels;
104             uint32_t sampling_rate;
105             uint32_t byterate;
106         } audio;
107     };
108 };
109
110 /* ASF GUIDs
111  *
112  * Microsoft defines these 16-byte (128-bit) GUIDs as:
113  * first 8 bytes are in little-endian order
114  * next 8 bytes are in big-endian order
115  *
116  * Eg.: AaBbCcDd-EeFf-GgHh-IiJj-KkLlMmNnOoPp:
117  *
118  * to convert to byte string do as follow:
119  *
120  * $Dd $Cc $Bb $Aa $Ff $Ee $Hh $Gg $Ii $Jj $Kk $Ll $Mm $Nn $Oo $Pp
121  *
122  * See http://www.microsoft.com/windows/windowsmedia/forpros/format/asfspec.aspx
123  */
124 static const char header_guid[16] = "\x30\x26\xB2\x75\x8E\x66\xCF\x11\xA6\xD9\x00\xAA\x00\x62\xCE\x6C";
125 static const char file_properties_guid[16] = "\xA1\xDC\xAB\x8C\x47\xA9\xCF\x11\x8E\xE4\x00\xC0\x0C\x20\x53\x65";
126 static const char stream_properties_guid[16] = "\x91\x07\xDC\xB7\xB7\xA9\xCF\x11\x8E\xE6\x00\xC0\x0C\x20\x53\x65";
127 static const char stream_type_audio_guid[16] = "\x40\x9E\x69\xF8\x4D\x5B\xCF\x11\xA8\xFD\x00\x80\x5F\x5C\x44\x2B";
128 static const char stream_type_video_guid[16] = "\xC0\xEF\x19\xBC\x4D\x5B\xCF\x11\xA8\xFD\x00\x80\x5F\x5C\x44\x2B";
129 static const char content_description_guid[16] = "\x33\x26\xB2\x75\x8E\x66\xCF\x11\xA6\xD9\x00\xAA\x00\x62\xCE\x6C";
130 static const char extended_content_description_guid[16] = "\x40\xA4\xD0\xD2\x07\xE3\xD2\x11\x97\xF0\x00\xA0\xC9\x5E\xA8\x50";
131 static const char header_extension_guid[16] = "\xb5\x03\xbf_.\xa9\xcf\x11\x8e\xe3\x00\xc0\x0c Se";
132 static const char metadata_guid[16] = "\xEA\xCB\xF8\xC5\xAF[wH\204g\xAA\214D\xFAL\xCA";
133 static const char metadata_library_guid[16] = "\224\034#D\230\224\321I\241A\x1d\x13NEpT";
134 static const char content_encryption_object_guid[16] = "\xFB\xB3\x11\x22\x23\xBD\xD2\x11\xB4\xB7\x00\xA0\xC9\x55\xFC\x6E";
135 static const char extended_content_encryption_object_guid[16] = "\x14\xE6\x8A\x29\x22\x26\x17\x4C\xB9\x35\xDA\xE0\x7E\xE9\x28\x9C";
136
137 static const char attr_name_wm_album_artist[28] = "\x57\x00\x4d\x00\x2f\x00\x41\x00\x6c\x00\x62\x00\x75\x00\x6d\x00\x41\x00\x72\x00\x74\x00\x69\x00\x73\x00\x74\x00";
138 static const char attr_name_wm_album_title[26] = "\x57\x00\x4d\x00\x2f\x00\x41\x00\x6c\x00\x62\x00\x75\x00\x6d\x00\x54\x00\x69\x00\x74\x00\x6c\x00\x65\x00";
139 static const char attr_name_wm_genre[16] = "\x57\x00\x4d\x00\x2f\x00\x47\x00\x65\x00\x6e\x00\x72\x00\x65\x00";
140 static const char attr_name_wm_track_number[28] = "\x57\x00\x4d\x00\x2f\x00\x54\x00\x72\x00\x61\x00\x63\x00\x6b\x00\x4e\x00\x75\x00\x6d\x00\x62\x00\x65\x00\x72\x00";
141
142 static long long
143 _to_number(const char *data, unsigned int type_size, unsigned int data_size)
144 {
145     long long sum = 0;
146     unsigned int last, i;
147
148     last = data_size > type_size ? type_size : data_size;
149
150     for (i = 0; i < last; i++)
151         sum |= (unsigned char) (data[i]) << (i * 8);
152
153     return sum;
154 }
155
156 static short
157 _read_word(int fd)
158 {
159     char v[2];
160     if (read(fd, &v, 2) != 2)
161         return 0;
162     return (short) _to_number(v, sizeof(unsigned short), 2);
163 }
164
165 static unsigned int
166 _read_dword(int fd)
167 {
168     char v[4];
169     if (read(fd, &v, 4) != 4)
170         return 0;
171     return (unsigned int) _to_number(v, sizeof(unsigned int), 4);
172 }
173
174 static long long
175 _read_qword(int fd)
176 {
177     char v[8];
178     if (read(fd, &v, 8) != 8)
179         return 0;
180     return _to_number(v, sizeof(unsigned long long), 8);
181 }
182
183 static int
184 _read_string(int fd, size_t count, char **str, unsigned int *len)
185 {
186     char *data;
187     ssize_t data_size, size;
188
189     data = malloc(sizeof(char) * count);
190     data_size = read(fd, data, count);
191     if (data_size == -1) {
192         free(data);
193         return -1;
194     }
195
196     size = data_size;
197     while (size >= 2) {
198         if (data[size - 1] != '\0' || data[size - 2] != '\0')
199             break;
200         size -= 2;
201     }
202
203     *str = data;
204     *len = size;
205
206     return 0;
207 }
208
209 static int
210 _parse_file_properties(lms_charset_conv_t *cs_conv, int fd,
211                        struct lms_audio_info *info)
212 {
213     struct {
214         char fileid[16];
215         uint64_t file_size;
216         uint64_t creation_date;
217         uint64_t data_packets_count;
218         uint64_t play_duration;
219         uint64_t send_duration;
220         uint64_t preroll;
221         uint32_t flags;
222         uint32_t min_data_packet_size;
223         uint32_t max_data_packet_size;
224         uint32_t max_bitrate;
225     } __attribute__((packed)) props;
226     int r;
227
228     r = read(fd, &props, sizeof(props));
229     if (r != sizeof(props))
230         return r;
231
232     /* Broadcast flag */
233     if (le32toh(props.flags) & 0x1)
234         return r;
235
236     /* ASF spec 01.20.06 sec. 3.2: we need to subtract the preroll value from
237      * the duration in order to obtain the real duration */
238     info->length = (unsigned int)((le64toh(props.play_duration) / NSEC100_PER_SEC)
239                                   - le64toh(props.preroll) / MSEC_PER_SEC);
240
241     return r;
242 }
243
244 static int
245 _parse_stream_properties(int fd, struct stream **pstream)
246 {
247     struct {
248         char stream_type[16];
249         char error_correction_type[16];
250         uint64_t time_offset;
251         uint32_t type_specific_len;
252         uint32_t error_correction_data_len;
253         uint16_t flags;
254         uint32_t reserved; /* don't use, unaligned */
255     }  __attribute__((packed)) props;
256
257     int r;
258     struct stream *s;
259
260     *pstream = NULL;
261
262     s = calloc(1, sizeof(struct stream));
263     if (!s)
264         return -ENOMEM;
265
266     r = read(fd, &props, sizeof(props));
267     if (r != sizeof(props))
268         goto done;
269
270     if (memcmp(props.stream_type, stream_type_audio_guid, 16) == 0)
271         s->type = STREAM_TYPE_AUDIO;
272     else if (memcmp(props.stream_type, stream_type_video_guid, 16) == 0)
273         s->type = STREAM_TYPE_VIDEO;
274     else {
275         /* ignore stream */
276         goto done;
277     }
278
279     s->id = le16toh(props.flags) & 0x7F;
280     /* Not a valid stream */
281     if (!s->id)
282         goto done;
283
284     if (s->type == STREAM_TYPE_AUDIO) {
285         if (le32toh(props.type_specific_len) < 18)
286             goto done;
287
288         s->audio.codec_id = _read_word(fd);
289         s->audio.channels = _read_word(fd);
290         s->audio.sampling_rate = _read_dword(fd);
291         s->audio.byterate = _read_dword(fd);
292         r += 12;
293     }
294
295     *pstream = s;
296
297     return r;
298
299 done:
300     free(s);
301     return r;
302 }
303
304 static void
305 _parse_content_description(lms_charset_conv_t *cs_conv, int fd, struct asf_info *info)
306 {
307     int title_length = _read_word(fd);
308     int artist_length = _read_word(fd);
309     int copyright_length = _read_word(fd);
310     int comment_length = _read_word(fd);
311     int rating_length = _read_word(fd);
312
313     _read_string(fd, title_length, &info->title.str, &info->title.len);
314     lms_charset_conv_force(cs_conv, &info->title.str, &info->title.len);
315     _read_string(fd, artist_length, &info->artist.str, &info->artist.len);
316     lms_charset_conv_force(cs_conv, &info->artist.str, &info->artist.len);
317     /* ignore copyright, comment and rating */
318     lseek(fd, copyright_length + comment_length + rating_length, SEEK_CUR);
319 }
320
321 static void
322 _parse_attribute_name(int fd,
323                       char **attr_name,
324                       unsigned int *attr_name_len,
325                       int *attr_type,
326                       int *attr_size)
327 {
328     int attr_name_length;
329
330     attr_name_length = _read_word(fd);
331     _read_string(fd, attr_name_length, attr_name, attr_name_len);
332     *attr_type = _read_word(fd);
333     *attr_size = _read_word(fd);
334 }
335
336 static void
337 _parse_attribute_string_data(lms_charset_conv_t *cs_conv,
338                              int fd,
339                              int attr_size,
340                              char **attr_data,
341                              unsigned int *attr_data_len)
342 {
343     _read_string(fd, attr_size, attr_data, attr_data_len);
344     lms_charset_conv_force(cs_conv, attr_data, attr_data_len);
345 }
346
347 static void
348 _skip_attribute_data(int fd, int kind, int attr_type, int attr_size)
349 {
350     switch (attr_type) {
351     case ATTR_TYPE_WORD:
352         lseek(fd, 2, SEEK_CUR);
353         break;
354
355     case ATTR_TYPE_BOOL:
356         if (kind == 0)
357             lseek(fd, 4, SEEK_CUR);
358         else
359             lseek(fd, 2, SEEK_CUR);
360         break;
361
362     case ATTR_TYPE_DWORD:
363         lseek(fd, 4, SEEK_CUR);
364         break;
365
366     case ATTR_TYPE_QWORD:
367         lseek(fd, 8, SEEK_CUR);
368         break;
369
370     case ATTR_TYPE_UNICODE:
371     case ATTR_TYPE_BYTES:
372     case ATTR_TYPE_GUID:
373         lseek(fd, attr_size, SEEK_CUR);
374         break;
375
376     default:
377         break;
378     }
379 }
380
381 static void
382 _parse_extended_content_description_object(lms_charset_conv_t *cs_conv, int fd, struct asf_info *info)
383 {
384     int count = _read_word(fd);
385     char *attr_name;
386     unsigned int attr_name_len;
387     int attr_type, attr_size;
388     while (count--) {
389         attr_name = NULL;
390         _parse_attribute_name(fd,
391                               &attr_name, &attr_name_len,
392                               &attr_type, &attr_size);
393         if (attr_type == ATTR_TYPE_UNICODE) {
394             if (memcmp(attr_name, attr_name_wm_album_title, attr_name_len) == 0)
395                 _parse_attribute_string_data(cs_conv,
396                                              fd, attr_size,
397                                              &info->album.str,
398                                              &info->album.len);
399             else if (memcmp(attr_name, attr_name_wm_genre, attr_name_len) == 0)
400                 _parse_attribute_string_data(cs_conv,
401                                              fd, attr_size,
402                                              &info->genre.str,
403                                              &info->genre.len);
404             else if (memcmp(attr_name, attr_name_wm_album_artist, attr_name_len) == 0)
405                 _parse_attribute_string_data(cs_conv,
406                                              fd, attr_size,
407                                              &info->artist.str,
408                                              &info->artist.len);
409             else if (memcmp(attr_name, attr_name_wm_track_number, attr_name_len) == 0) {
410                 char *trackno;
411                 unsigned int trackno_len;
412                 _parse_attribute_string_data(cs_conv,
413                                              fd, attr_size,
414                                              &trackno,
415                                              &trackno_len);
416                 if (trackno) {
417                     info->trackno = atoi(trackno);
418                     free(trackno);
419                 }
420             }
421             else
422                 _skip_attribute_data(fd, 0, attr_type, attr_size);
423         }
424         else
425             _skip_attribute_data(fd, 0, attr_type, attr_size);
426         if (attr_name)
427             free(attr_name);
428     }
429 }
430
431 static void *
432 _match(struct plugin *p, const char *path, int len, int base)
433 {
434     long i;
435
436     i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
437     if (i < 0)
438       return NULL;
439     else
440       return (void*)(i + 1);
441 }
442
443 static int
444 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
445 {
446     struct asf_info info = { };
447     struct lms_audio_info audio_info = { };
448     struct lms_video_info video_info = { };
449     int r, fd, num_objects, i;
450     char guid[16];
451     unsigned int size;
452     int stream_type = STREAM_TYPE_UNKNOWN;
453     struct stream *streams = NULL;
454
455     fd = open(finfo->path, O_RDONLY);
456     if (fd < 0) {
457         perror("open");
458         return -1;
459     }
460
461     if (read(fd, &guid, 16) != 16) {
462         perror("read");
463         r = -2;
464         goto done;
465     }
466     if (memcmp(guid, header_guid, 16) != 0) {
467         fprintf(stderr, "ERROR: invalid header (%s).\n", finfo->path);
468         r = -3;
469         goto done;
470     }
471
472     size = _read_qword(fd);
473     num_objects = _read_dword(fd);
474
475     lseek(fd, 2, SEEK_CUR);
476
477     for (i = 0; i < num_objects; ++i) {
478         read(fd, &guid, 16);
479         size = _read_qword(fd);
480
481         if (memcmp(guid, file_properties_guid, 16) == 0) {
482             r = _parse_file_properties(plugin->cs_conv, fd, &audio_info);
483             if (r < 0)
484                 goto done;
485             lseek(fd, size - (24 + r), SEEK_CUR);
486         } else if (memcmp(guid, stream_properties_guid, 16) == 0) {
487             struct stream *s;
488             r = _parse_stream_properties(fd, &s);
489             if (r < 0)
490                 goto done;
491
492             lseek(fd, size - (24 + r), SEEK_CUR);
493             if (!s)
494                 continue;
495
496             if (stream_type != STREAM_TYPE_VIDEO)
497                 stream_type = s->type;
498
499             s->next = streams;
500             streams = s;
501         } else if (memcmp(guid, content_description_guid, 16) == 0)
502             _parse_content_description(plugin->cs_conv, fd, &info);
503         else if (memcmp(guid, extended_content_description_guid, 16) == 0)
504             _parse_extended_content_description_object(plugin->cs_conv, fd, &info);
505         else if (memcmp(guid, content_encryption_object_guid, 16) == 0 ||
506                  memcmp(guid, extended_content_encryption_object_guid, 16) == 0) {
507             /* ignore DRM'd files */
508             fprintf(stderr, "ERROR: ignoring DRM'd file %s\n", finfo->path);
509             r = -4;
510             goto done;
511         } else
512             lseek(fd, size - 24, SEEK_CUR);
513     }
514
515     /* try to define stream type by extension */
516     if (stream_type == STREAM_TYPE_UNKNOWN) {
517         long ext_idx = ((long)match) - 1;
518         if (strcmp(_exts[ext_idx].str, ".wma") == 0)
519             stream_type = STREAM_TYPE_AUDIO;
520         /* consider wmv and asf as video */
521         else
522             stream_type = STREAM_TYPE_VIDEO;
523     }
524
525     lms_string_size_strip_and_free(&info.title);
526     lms_string_size_strip_and_free(&info.artist);
527     lms_string_size_strip_and_free(&info.album);
528     lms_string_size_strip_and_free(&info.genre);
529
530     if (!info.title.str) {
531         long ext_idx;
532         ext_idx = ((long)match) - 1;
533         info.title.len = finfo->path_len - finfo->base - _exts[ext_idx].len;
534         info.title.str = malloc((info.title.len + 1) * sizeof(char));
535         memcpy(info.title.str, finfo->path + finfo->base, info.title.len);
536         info.title.str[info.title.len] = '\0';
537         lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
538     }
539
540 #if 0
541     fprintf(stderr, "file %s info\n", finfo->path);
542     fprintf(stderr, "\ttitle='%s'\n", info.title.str);
543     fprintf(stderr, "\tartist='%s'\n", info.artist.str);
544     fprintf(stderr, "\talbum='%s'\n", info.album.str);
545     fprintf(stderr, "\tgenre='%s'\n", info.genre.str);
546     fprintf(stderr, "\ttrackno=%d\n", info.trackno);
547 #endif
548
549     audio_info.container = _container;
550
551     if (stream_type == STREAM_TYPE_AUDIO) {
552         audio_info.id = finfo->id;
553         audio_info.title = info.title;
554         audio_info.artist = info.artist;
555         audio_info.album = info.album;
556         audio_info.genre = info.genre;
557         audio_info.trackno = info.trackno;
558
559         audio_info.channels = streams->audio.channels;
560         audio_info.bitrate = streams->audio.byterate * 8;
561         audio_info.sampling_rate = streams->audio.sampling_rate;
562
563         r = lms_db_audio_add(plugin->audio_db, &audio_info);
564     } else {
565         video_info.id = finfo->id;
566         video_info.title = info.title;
567         video_info.artist = info.artist;
568         r = lms_db_video_add(plugin->video_db, &video_info);
569     }
570
571 done:
572     while (streams) {
573         struct stream *s = streams;
574         streams = s->next;
575         free(s);
576     }
577
578     free(info.title.str);
579     free(info.artist.str);
580     free(info.album.str);
581     free(info.genre.str);
582
583     posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
584     close(fd);
585
586     return r;
587 }
588
589 static int
590 _setup(struct plugin *plugin, struct lms_context *ctxt)
591 {
592     plugin->audio_db = lms_db_audio_new(ctxt->db);
593     if (!plugin->audio_db)
594         return -1;
595     plugin->video_db = lms_db_video_new(ctxt->db);
596     if (!plugin->video_db)
597         return -1;
598     plugin->cs_conv = lms_charset_conv_new();
599     if (!plugin->cs_conv)
600         return -1;
601     lms_charset_conv_add(plugin->cs_conv, "UTF-16LE");
602
603     return 0;
604 }
605
606 static int
607 _start(struct plugin *plugin, struct lms_context *ctxt)
608 {
609     int r;
610     r = lms_db_audio_start(plugin->audio_db);
611     r |= lms_db_video_start(plugin->video_db);
612     return r;
613 }
614
615 static int
616 _finish(struct plugin *plugin, struct lms_context *ctxt)
617 {
618     if (plugin->audio_db)
619         lms_db_audio_free(plugin->audio_db);
620     if (plugin->video_db)
621         lms_db_video_free(plugin->video_db);
622     if (plugin->cs_conv)
623         lms_charset_conv_free(plugin->cs_conv);
624
625     return 0;
626 }
627
628 static int
629 _close(struct plugin *plugin)
630 {
631     free(plugin);
632     return 0;
633 }
634
635 API struct lms_plugin *
636 lms_plugin_open(void)
637 {
638     struct plugin *plugin;
639
640     plugin = (struct plugin *)malloc(sizeof(*plugin));
641     plugin->plugin.name = _name;
642     plugin->plugin.match = (lms_plugin_match_fn_t)_match;
643     plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
644     plugin->plugin.close = (lms_plugin_close_fn_t)_close;
645     plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
646     plugin->plugin.start = (lms_plugin_start_fn_t)_start;
647     plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
648
649     return (struct lms_plugin *)plugin;
650 }
651
652 API const struct lms_plugin_info *
653 lms_plugin_info(void)
654 {
655     static struct lms_plugin_info info = {
656         _name,
657         _cats,
658         "Microsoft WMA, WMV and ASF",
659         PACKAGE_VERSION,
660         _authors,
661         "http://lms.garage.maemo.org"
662     };
663
664     return &info;
665 }