plugins/asf: Fix overriding type - parse multiple streams
[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 };
101
102 /* ASF GUIDs
103  *
104  * Microsoft defines these 16-byte (128-bit) GUIDs as:
105  * first 8 bytes are in little-endian order
106  * next 8 bytes are in big-endian order
107  *
108  * Eg.: AaBbCcDd-EeFf-GgHh-IiJj-KkLlMmNnOoPp:
109  *
110  * to convert to byte string do as follow:
111  *
112  * $Dd $Cc $Bb $Aa $Ff $Ee $Hh $Gg $Ii $Jj $Kk $Ll $Mm $Nn $Oo $Pp
113  *
114  * See http://www.microsoft.com/windows/windowsmedia/forpros/format/asfspec.aspx
115  */
116 static const char header_guid[16] = "\x30\x26\xB2\x75\x8E\x66\xCF\x11\xA6\xD9\x00\xAA\x00\x62\xCE\x6C";
117 static const char file_properties_guid[16] = "\xA1\xDC\xAB\x8C\x47\xA9\xCF\x11\x8E\xE4\x00\xC0\x0C\x20\x53\x65";
118 static const char stream_properties_guid[16] = "\x91\x07\xDC\xB7\xB7\xA9\xCF\x11\x8E\xE6\x00\xC0\x0C\x20\x53\x65";
119 static const char stream_type_audio_guid[16] = "\x40\x9E\x69\xF8\x4D\x5B\xCF\x11\xA8\xFD\x00\x80\x5F\x5C\x44\x2B";
120 static const char stream_type_video_guid[16] = "\xC0\xEF\x19\xBC\x4D\x5B\xCF\x11\xA8\xFD\x00\x80\x5F\x5C\x44\x2B";
121 static const char content_description_guid[16] = "\x33\x26\xB2\x75\x8E\x66\xCF\x11\xA6\xD9\x00\xAA\x00\x62\xCE\x6C";
122 static const char extended_content_description_guid[16] = "\x40\xA4\xD0\xD2\x07\xE3\xD2\x11\x97\xF0\x00\xA0\xC9\x5E\xA8\x50";
123 static const char header_extension_guid[16] = "\xb5\x03\xbf_.\xa9\xcf\x11\x8e\xe3\x00\xc0\x0c Se";
124 static const char metadata_guid[16] = "\xEA\xCB\xF8\xC5\xAF[wH\204g\xAA\214D\xFAL\xCA";
125 static const char metadata_library_guid[16] = "\224\034#D\230\224\321I\241A\x1d\x13NEpT";
126 static const char content_encryption_object_guid[16] = "\xFB\xB3\x11\x22\x23\xBD\xD2\x11\xB4\xB7\x00\xA0\xC9\x55\xFC\x6E";
127 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";
128
129 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";
130 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";
131 static const char attr_name_wm_genre[16] = "\x57\x00\x4d\x00\x2f\x00\x47\x00\x65\x00\x6e\x00\x72\x00\x65\x00";
132 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";
133
134 static long long
135 _to_number(const char *data, unsigned int type_size, unsigned int data_size)
136 {
137     long long sum = 0;
138     unsigned int last, i;
139
140     last = data_size > type_size ? type_size : data_size;
141
142     for (i = 0; i < last; i++)
143         sum |= (unsigned char) (data[i]) << (i * 8);
144
145     return sum;
146 }
147
148 static short
149 _read_word(int fd)
150 {
151     char v[2];
152     if (read(fd, &v, 2) != 2)
153         return 0;
154     return (short) _to_number(v, sizeof(unsigned short), 2);
155 }
156
157 static unsigned int
158 _read_dword(int fd)
159 {
160     char v[4];
161     if (read(fd, &v, 4) != 4)
162         return 0;
163     return (unsigned int) _to_number(v, sizeof(unsigned int), 4);
164 }
165
166 static long long
167 _read_qword(int fd)
168 {
169     char v[8];
170     if (read(fd, &v, 8) != 8)
171         return 0;
172     return _to_number(v, sizeof(unsigned long long), 8);
173 }
174
175 static int
176 _read_string(int fd, size_t count, char **str, unsigned int *len)
177 {
178     char *data;
179     ssize_t data_size, size;
180
181     data = malloc(sizeof(char) * count);
182     data_size = read(fd, data, count);
183     if (data_size == -1) {
184         free(data);
185         return -1;
186     }
187
188     size = data_size;
189     while (size >= 2) {
190         if (data[size - 1] != '\0' || data[size - 2] != '\0')
191             break;
192         size -= 2;
193     }
194
195     *str = data;
196     *len = size;
197
198     return 0;
199 }
200
201 static int
202 _parse_file_properties(lms_charset_conv_t *cs_conv, int fd,
203                        struct lms_audio_info *info)
204 {
205     struct {
206         char fileid[16];
207         uint64_t file_size;
208         uint64_t creation_date;
209         uint64_t data_packets_count;
210         uint64_t play_duration;
211         uint64_t send_duration;
212         uint64_t preroll;
213         uint32_t flags;
214         uint32_t min_data_packet_size;
215         uint32_t max_data_packet_size;
216         uint32_t max_bitrate;
217     } __attribute__((packed)) props;
218     int r;
219
220     r = read(fd, &props, sizeof(props));
221     if (r != sizeof(props))
222         return r;
223
224     /* Broadcast flag */
225     if (le32toh(props.flags) & 0x1)
226         return r;
227
228     /* ASF spec 01.20.06 sec. 3.2: we need to subtract the preroll value from
229      * the duration in order to obtain the real duration */
230     info->length = (unsigned int)((le64toh(props.play_duration) / NSEC100_PER_SEC)
231                                   - le64toh(props.preroll) / MSEC_PER_SEC);
232
233     return r;
234 }
235
236 static int
237 _parse_stream_properties(int fd, struct stream **pstream)
238 {
239     struct {
240         char stream_type[16];
241         char error_correction_type[16];
242         uint64_t time_offset;
243         uint32_t type_specific_len;
244         uint32_t error_correction_data_len;
245         uint16_t flags;
246         uint32_t reserved; /* don't use, unaligned */
247     }  __attribute__((packed)) props;
248
249     int r;
250     struct stream *s;
251
252     *pstream = NULL;
253
254     s = calloc(1, sizeof(struct stream));
255     if (!s)
256         return -ENOMEM;
257
258     r = read(fd, &props, sizeof(props));
259     if (r != sizeof(props))
260         goto done;
261
262     if (memcmp(props.stream_type, stream_type_audio_guid, 16) == 0)
263         s->type = STREAM_TYPE_AUDIO;
264     else if (memcmp(props.stream_type, stream_type_video_guid, 16) == 0)
265         s->type = STREAM_TYPE_VIDEO;
266     else {
267         /* ignore stream */
268         goto done;
269     }
270
271     s->id = le16toh(props.flags) & 0x7F;
272     *pstream = s;
273
274     return r;
275
276 done:
277     free(s);
278     return r;
279 }
280
281 static void
282 _parse_content_description(lms_charset_conv_t *cs_conv, int fd, struct asf_info *info)
283 {
284     int title_length = _read_word(fd);
285     int artist_length = _read_word(fd);
286     int copyright_length = _read_word(fd);
287     int comment_length = _read_word(fd);
288     int rating_length = _read_word(fd);
289
290     _read_string(fd, title_length, &info->title.str, &info->title.len);
291     lms_charset_conv_force(cs_conv, &info->title.str, &info->title.len);
292     _read_string(fd, artist_length, &info->artist.str, &info->artist.len);
293     lms_charset_conv_force(cs_conv, &info->artist.str, &info->artist.len);
294     /* ignore copyright, comment and rating */
295     lseek(fd, copyright_length + comment_length + rating_length, SEEK_CUR);
296 }
297
298 static void
299 _parse_attribute_name(int fd,
300                       char **attr_name,
301                       unsigned int *attr_name_len,
302                       int *attr_type,
303                       int *attr_size)
304 {
305     int attr_name_length;
306
307     attr_name_length = _read_word(fd);
308     _read_string(fd, attr_name_length, attr_name, attr_name_len);
309     *attr_type = _read_word(fd);
310     *attr_size = _read_word(fd);
311 }
312
313 static void
314 _parse_attribute_string_data(lms_charset_conv_t *cs_conv,
315                              int fd,
316                              int attr_size,
317                              char **attr_data,
318                              unsigned int *attr_data_len)
319 {
320     _read_string(fd, attr_size, attr_data, attr_data_len);
321     lms_charset_conv_force(cs_conv, attr_data, attr_data_len);
322 }
323
324 static void
325 _skip_attribute_data(int fd, int kind, int attr_type, int attr_size)
326 {
327     switch (attr_type) {
328     case ATTR_TYPE_WORD:
329         lseek(fd, 2, SEEK_CUR);
330         break;
331
332     case ATTR_TYPE_BOOL:
333         if (kind == 0)
334             lseek(fd, 4, SEEK_CUR);
335         else
336             lseek(fd, 2, SEEK_CUR);
337         break;
338
339     case ATTR_TYPE_DWORD:
340         lseek(fd, 4, SEEK_CUR);
341         break;
342
343     case ATTR_TYPE_QWORD:
344         lseek(fd, 8, SEEK_CUR);
345         break;
346
347     case ATTR_TYPE_UNICODE:
348     case ATTR_TYPE_BYTES:
349     case ATTR_TYPE_GUID:
350         lseek(fd, attr_size, SEEK_CUR);
351         break;
352
353     default:
354         break;
355     }
356 }
357
358 static void
359 _parse_extended_content_description_object(lms_charset_conv_t *cs_conv, int fd, struct asf_info *info)
360 {
361     int count = _read_word(fd);
362     char *attr_name;
363     unsigned int attr_name_len;
364     int attr_type, attr_size;
365     while (count--) {
366         attr_name = NULL;
367         _parse_attribute_name(fd,
368                               &attr_name, &attr_name_len,
369                               &attr_type, &attr_size);
370         if (attr_type == ATTR_TYPE_UNICODE) {
371             if (memcmp(attr_name, attr_name_wm_album_title, attr_name_len) == 0)
372                 _parse_attribute_string_data(cs_conv,
373                                              fd, attr_size,
374                                              &info->album.str,
375                                              &info->album.len);
376             else if (memcmp(attr_name, attr_name_wm_genre, attr_name_len) == 0)
377                 _parse_attribute_string_data(cs_conv,
378                                              fd, attr_size,
379                                              &info->genre.str,
380                                              &info->genre.len);
381             else if (memcmp(attr_name, attr_name_wm_album_artist, attr_name_len) == 0)
382                 _parse_attribute_string_data(cs_conv,
383                                              fd, attr_size,
384                                              &info->artist.str,
385                                              &info->artist.len);
386             else if (memcmp(attr_name, attr_name_wm_track_number, attr_name_len) == 0) {
387                 char *trackno;
388                 unsigned int trackno_len;
389                 _parse_attribute_string_data(cs_conv,
390                                              fd, attr_size,
391                                              &trackno,
392                                              &trackno_len);
393                 if (trackno) {
394                     info->trackno = atoi(trackno);
395                     free(trackno);
396                 }
397             }
398             else
399                 _skip_attribute_data(fd, 0, attr_type, attr_size);
400         }
401         else
402             _skip_attribute_data(fd, 0, attr_type, attr_size);
403         if (attr_name)
404             free(attr_name);
405     }
406 }
407
408 static void *
409 _match(struct plugin *p, const char *path, int len, int base)
410 {
411     long i;
412
413     i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
414     if (i < 0)
415       return NULL;
416     else
417       return (void*)(i + 1);
418 }
419
420 static int
421 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
422 {
423     struct asf_info info = { };
424     struct lms_audio_info audio_info = { };
425     struct lms_video_info video_info = { };
426     int r, fd, num_objects, i;
427     char guid[16];
428     unsigned int size;
429     int stream_type = STREAM_TYPE_UNKNOWN;
430     struct stream *streams = NULL;
431
432     fd = open(finfo->path, O_RDONLY);
433     if (fd < 0) {
434         perror("open");
435         return -1;
436     }
437
438     if (read(fd, &guid, 16) != 16) {
439         perror("read");
440         r = -2;
441         goto done;
442     }
443     if (memcmp(guid, header_guid, 16) != 0) {
444         fprintf(stderr, "ERROR: invalid header (%s).\n", finfo->path);
445         r = -3;
446         goto done;
447     }
448
449     size = _read_qword(fd);
450     num_objects = _read_dword(fd);
451
452     lseek(fd, 2, SEEK_CUR);
453
454     for (i = 0; i < num_objects; ++i) {
455         read(fd, &guid, 16);
456         size = _read_qword(fd);
457
458         if (memcmp(guid, file_properties_guid, 16) == 0) {
459             r = _parse_file_properties(plugin->cs_conv, fd, &audio_info);
460             if (r < 0)
461                 goto done;
462             lseek(fd, size - (24 + r), SEEK_CUR);
463         } else if (memcmp(guid, stream_properties_guid, 16) == 0) {
464             struct stream *s;
465             r = _parse_stream_properties(fd, &s);
466             if (r < 0)
467                 goto done;
468
469             lseek(fd, size - (24 + r), SEEK_CUR);
470             if (!s)
471                 continue;
472
473             if (stream_type != STREAM_TYPE_VIDEO)
474                 stream_type = s->type;
475
476             s->next = streams;
477             streams = s;
478         } else if (memcmp(guid, content_description_guid, 16) == 0)
479             _parse_content_description(plugin->cs_conv, fd, &info);
480         else if (memcmp(guid, extended_content_description_guid, 16) == 0)
481             _parse_extended_content_description_object(plugin->cs_conv, fd, &info);
482         else if (memcmp(guid, content_encryption_object_guid, 16) == 0 ||
483                  memcmp(guid, extended_content_encryption_object_guid, 16) == 0) {
484             /* ignore DRM'd files */
485             fprintf(stderr, "ERROR: ignoring DRM'd file %s\n", finfo->path);
486             r = -4;
487             goto done;
488         } else
489             lseek(fd, size - 24, SEEK_CUR);
490     }
491
492     /* try to define stream type by extension */
493     if (stream_type == STREAM_TYPE_UNKNOWN) {
494         long ext_idx = ((long)match) - 1;
495         if (strcmp(_exts[ext_idx].str, ".wma") == 0)
496             stream_type = STREAM_TYPE_AUDIO;
497         /* consider wmv and asf as video */
498         else
499             stream_type = STREAM_TYPE_VIDEO;
500     }
501
502     lms_string_size_strip_and_free(&info.title);
503     lms_string_size_strip_and_free(&info.artist);
504     lms_string_size_strip_and_free(&info.album);
505     lms_string_size_strip_and_free(&info.genre);
506
507     if (!info.title.str) {
508         long ext_idx;
509         ext_idx = ((long)match) - 1;
510         info.title.len = finfo->path_len - finfo->base - _exts[ext_idx].len;
511         info.title.str = malloc((info.title.len + 1) * sizeof(char));
512         memcpy(info.title.str, finfo->path + finfo->base, info.title.len);
513         info.title.str[info.title.len] = '\0';
514         lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
515     }
516
517 #if 0
518     fprintf(stderr, "file %s info\n", finfo->path);
519     fprintf(stderr, "\ttitle='%s'\n", info.title.str);
520     fprintf(stderr, "\tartist='%s'\n", info.artist.str);
521     fprintf(stderr, "\talbum='%s'\n", info.album.str);
522     fprintf(stderr, "\tgenre='%s'\n", info.genre.str);
523     fprintf(stderr, "\ttrackno=%d\n", info.trackno);
524 #endif
525
526     audio_info.container = _container;
527
528     if (stream_type == STREAM_TYPE_AUDIO) {
529         audio_info.id = finfo->id;
530         audio_info.title = info.title;
531         audio_info.artist = info.artist;
532         audio_info.album = info.album;
533         audio_info.genre = info.genre;
534         audio_info.trackno = info.trackno;
535         r = lms_db_audio_add(plugin->audio_db, &audio_info);
536     } else {
537         video_info.id = finfo->id;
538         video_info.title = info.title;
539         video_info.artist = info.artist;
540         r = lms_db_video_add(plugin->video_db, &video_info);
541     }
542
543 done:
544     while (streams) {
545         struct stream *s = streams;
546         streams = s->next;
547         free(s);
548     }
549
550     free(info.title.str);
551     free(info.artist.str);
552     free(info.album.str);
553     free(info.genre.str);
554
555     posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
556     close(fd);
557
558     return r;
559 }
560
561 static int
562 _setup(struct plugin *plugin, struct lms_context *ctxt)
563 {
564     plugin->audio_db = lms_db_audio_new(ctxt->db);
565     if (!plugin->audio_db)
566         return -1;
567     plugin->video_db = lms_db_video_new(ctxt->db);
568     if (!plugin->video_db)
569         return -1;
570     plugin->cs_conv = lms_charset_conv_new();
571     if (!plugin->cs_conv)
572         return -1;
573     lms_charset_conv_add(plugin->cs_conv, "UTF-16LE");
574
575     return 0;
576 }
577
578 static int
579 _start(struct plugin *plugin, struct lms_context *ctxt)
580 {
581     int r;
582     r = lms_db_audio_start(plugin->audio_db);
583     r |= lms_db_video_start(plugin->video_db);
584     return r;
585 }
586
587 static int
588 _finish(struct plugin *plugin, struct lms_context *ctxt)
589 {
590     if (plugin->audio_db)
591         lms_db_audio_free(plugin->audio_db);
592     if (plugin->video_db)
593         lms_db_video_free(plugin->video_db);
594     if (plugin->cs_conv)
595         lms_charset_conv_free(plugin->cs_conv);
596
597     return 0;
598 }
599
600 static int
601 _close(struct plugin *plugin)
602 {
603     free(plugin);
604     return 0;
605 }
606
607 API struct lms_plugin *
608 lms_plugin_open(void)
609 {
610     struct plugin *plugin;
611
612     plugin = (struct plugin *)malloc(sizeof(*plugin));
613     plugin->plugin.name = _name;
614     plugin->plugin.match = (lms_plugin_match_fn_t)_match;
615     plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
616     plugin->plugin.close = (lms_plugin_close_fn_t)_close;
617     plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
618     plugin->plugin.start = (lms_plugin_start_fn_t)_start;
619     plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
620
621     return (struct lms_plugin *)plugin;
622 }
623
624 API const struct lms_plugin_info *
625 lms_plugin_info(void)
626 {
627     static struct lms_plugin_info info = {
628         _name,
629         _cats,
630         "Microsoft WMA, WMV and ASF",
631         PACKAGE_VERSION,
632         _authors,
633         "http://lms.garage.maemo.org"
634     };
635
636     return &info;
637 }