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