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