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