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