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