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