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