shared: Add constants for time conversions
[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  * Reference:
27  *   http://www.microsoft.com/en-us/download/details.aspx?id=14995
28  */
29
30 #include <lightmediascanner_plugin.h>
31 #include <lightmediascanner_db.h>
32 #include <shared/util.h>
33
34 #include <endian.h>
35 #include <errno.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <fcntl.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43
44 enum AttributeTypes {
45     ATTR_TYPE_UNICODE = 0,
46     ATTR_TYPE_BYTES,
47     ATTR_TYPE_BOOL,
48     ATTR_TYPE_DWORD,
49     ATTR_TYPE_QWORD,
50     ATTR_TYPE_WORD,
51     ATTR_TYPE_GUID
52 };
53
54 struct stream {
55     struct lms_stream base;
56     struct {
57         unsigned int sampling_rate;
58     } priv;
59 };
60
61 struct asf_info {
62     struct lms_string_size title;
63     struct lms_string_size artist;
64     struct lms_string_size album;
65     struct lms_string_size genre;
66     enum lms_stream_type type;
67     unsigned int length;
68     unsigned char trackno;
69
70     struct stream *streams;
71 };
72
73 struct plugin {
74     struct lms_plugin plugin;
75     lms_db_audio_t *audio_db;
76     lms_db_video_t *video_db;
77     lms_charset_conv_t *cs_conv;
78 };
79
80 static const char _name[] = "asf";
81 static const struct lms_string_size _container = LMS_STATIC_STRING_SIZE("asf");
82 static const struct lms_string_size _exts[] = {
83     LMS_STATIC_STRING_SIZE(".wma"),
84     LMS_STATIC_STRING_SIZE(".wmv"),
85     LMS_STATIC_STRING_SIZE(".asf")
86 };
87 static const char *_cats[] = {
88     "multimedia",
89     "audio",
90     NULL
91 };
92 static const char *_authors[] = {
93     "Andre Moreira Magalhaes",
94     NULL
95 };
96
97 /* TODO: Add the gazillion of possible codecs -- possibly a task to gperf */
98 static const struct {
99     uint16_t id;
100     struct lms_string_size name;
101 } _audio_codecs[] = {
102     /* id == 0  is special, check callers if it's needed */
103     { 0x0160, LMS_STATIC_STRING_SIZE("wmav1") },
104     { 0x0161, LMS_STATIC_STRING_SIZE("wmav2") },
105     { 0x0162, LMS_STATIC_STRING_SIZE("wmavpro") },
106     { 0x0163, LMS_STATIC_STRING_SIZE("wmavlossless") },
107     { 0x1600, LMS_STATIC_STRING_SIZE("aac") },
108     { 0x706d, LMS_STATIC_STRING_SIZE("aac") },
109     { 0x4143, LMS_STATIC_STRING_SIZE("aac") },
110     { 0xA106, LMS_STATIC_STRING_SIZE("aac") },
111     { 0xF1AC, LMS_STATIC_STRING_SIZE("flac") },
112     { 0x0055, LMS_STATIC_STRING_SIZE("mp3") },
113     { }
114 };
115
116 /* TODO: Add the gazillion of possible codecs -- possibly a task to gperf */
117 static const struct {
118     uint8_t id[4];
119     struct lms_string_size name;
120 } _video_codecs[] = {
121     /* id == 0  is special, check callers if it's needed */
122     { "WMV1", LMS_STATIC_STRING_SIZE("wmv1") },
123     { "WMV2", LMS_STATIC_STRING_SIZE("wmv2") },
124     { "WMV3", LMS_STATIC_STRING_SIZE("wmv3") },
125     { }
126 };
127
128
129 /* ASF GUIDs
130  *
131  * Microsoft defines these 16-byte (128-bit) GUIDs as:
132  * first 8 bytes are in little-endian order
133  * next 8 bytes are in big-endian order
134  *
135  * Eg.: AaBbCcDd-EeFf-GgHh-IiJj-KkLlMmNnOoPp:
136  *
137  * to convert to byte string do as follow:
138  *
139  * $Dd $Cc $Bb $Aa $Ff $Ee $Hh $Gg $Ii $Jj $Kk $Ll $Mm $Nn $Oo $Pp
140  *
141  * See http://www.microsoft.com/windows/windowsmedia/forpros/format/asfspec.aspx
142  */
143 static const char header_guid[16] = "\x30\x26\xB2\x75\x8E\x66\xCF\x11\xA6\xD9\x00\xAA\x00\x62\xCE\x6C";
144 static const char file_properties_guid[16] = "\xA1\xDC\xAB\x8C\x47\xA9\xCF\x11\x8E\xE4\x00\xC0\x0C\x20\x53\x65";
145 static const char stream_properties_guid[16] = "\x91\x07\xDC\xB7\xB7\xA9\xCF\x11\x8E\xE6\x00\xC0\x0C\x20\x53\x65";
146 static const char stream_type_audio_guid[16] = "\x40\x9E\x69\xF8\x4D\x5B\xCF\x11\xA8\xFD\x00\x80\x5F\x5C\x44\x2B";
147 static const char stream_type_video_guid[16] = "\xC0\xEF\x19\xBC\x4D\x5B\xCF\x11\xA8\xFD\x00\x80\x5F\x5C\x44\x2B";
148 static const char content_description_guid[16] = "\x33\x26\xB2\x75\x8E\x66\xCF\x11\xA6\xD9\x00\xAA\x00\x62\xCE\x6C";
149 static const char extended_content_description_guid[16] = "\x40\xA4\xD0\xD2\x07\xE3\xD2\x11\x97\xF0\x00\xA0\xC9\x5E\xA8\x50";
150 static const char header_extension_guid[16] = "\xb5\x03\xbf_.\xa9\xcf\x11\x8e\xe3\x00\xc0\x0c Se";
151 static const char metadata_guid[16] = "\xEA\xCB\xF8\xC5\xAF[wH\204g\xAA\214D\xFAL\xCA";
152 static const char metadata_library_guid[16] = "\224\034#D\230\224\321I\241A\x1d\x13NEpT";
153 static const char content_encryption_object_guid[16] = "\xFB\xB3\x11\x22\x23\xBD\xD2\x11\xB4\xB7\x00\xA0\xC9\x55\xFC\x6E";
154 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";
155
156 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";
157 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";
158 static const char attr_name_wm_genre[16] = "\x57\x00\x4d\x00\x2f\x00\x47\x00\x65\x00\x6e\x00\x72\x00\x65\x00";
159 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";
160
161 static long long
162 _to_number(const char *data, unsigned int type_size, unsigned int data_size)
163 {
164     long long sum = 0;
165     unsigned int last, i;
166
167     last = data_size > type_size ? type_size : data_size;
168
169     for (i = 0; i < last; i++)
170         sum |= (unsigned char) (data[i]) << (i * 8);
171
172     return sum;
173 }
174
175 static short
176 _read_word(int fd)
177 {
178     char v[2];
179     if (read(fd, &v, 2) != 2)
180         return 0;
181     return (short) _to_number(v, sizeof(unsigned short), 2);
182 }
183
184 static unsigned int
185 _read_dword(int fd)
186 {
187     char v[4];
188     if (read(fd, &v, 4) != 4)
189         return 0;
190     return (unsigned int) _to_number(v, sizeof(unsigned int), 4);
191 }
192
193 static long long
194 _read_qword(int fd)
195 {
196     char v[8];
197     if (read(fd, &v, 8) != 8)
198         return 0;
199     return _to_number(v, sizeof(unsigned long long), 8);
200 }
201
202 static int
203 _read_string(int fd, size_t count, char **str, unsigned int *len)
204 {
205     char *data;
206     ssize_t data_size, size;
207
208     data = malloc(sizeof(char) * count);
209     data_size = read(fd, data, count);
210     if (data_size == -1) {
211         free(data);
212         return -1;
213     }
214
215     size = data_size;
216     while (size >= 2) {
217         if (data[size - 1] != '\0' || data[size - 2] != '\0')
218             break;
219         size -= 2;
220     }
221
222     *str = data;
223     *len = size;
224
225     return 0;
226 }
227
228 static int
229 _parse_file_properties(int fd, struct asf_info *info)
230 {
231     struct {
232         char fileid[16];
233         uint64_t file_size;
234         uint64_t creation_date;
235         uint64_t data_packets_count;
236         uint64_t play_duration;
237         uint64_t send_duration;
238         uint64_t preroll;
239         uint32_t flags;
240         uint32_t min_data_packet_size;
241         uint32_t max_data_packet_size;
242         uint32_t max_bitrate;
243     } __attribute__((packed)) props;
244     int r;
245
246     r = read(fd, &props, sizeof(props));
247     if (r != sizeof(props))
248         return r;
249
250     /* Broadcast flag */
251     if (le32toh(props.flags) & 0x1)
252         return r;
253
254     /* ASF spec 01.20.06 sec. 3.2: we need to subtract the preroll value from
255      * the duration in order to obtain the real duration */
256     info->length = (unsigned int)(
257         (le64toh(props.play_duration) / NSEC100_PER_SEC) -
258         le64toh(props.preroll) / MSEC_PER_SEC);
259
260     return r;
261 }
262
263 static struct lms_string_size
264 _audio_codec_id_to_str(uint16_t id)
265 {
266     unsigned int i;
267
268     for (i = 0; _audio_codecs[i].name.str != NULL; i++)
269         if (_audio_codecs[i].id == id)
270             return _audio_codecs[i].name;
271
272     return _audio_codecs[i].name;
273 }
274
275 static struct lms_string_size
276 _video_codec_id_to_str(uint8_t id[4])
277 {
278     unsigned int i;
279
280     for (i = 0; _video_codecs[i].name.str != NULL; i++)
281         if (memcmp(id, _video_codecs[i].id, 4) == 0)
282             return _video_codecs[i].name;
283
284     return _video_codecs[i].name;
285 }
286
287 static int
288 _parse_stream_properties(int fd, struct stream **pstream)
289 {
290     struct {
291         char stream_type[16];
292         char error_correction_type[16];
293         uint64_t time_offset;
294         uint32_t type_specific_len;
295         uint32_t error_correction_data_len;
296         uint16_t flags;
297         uint32_t reserved; /* don't use, unaligned */
298     } __attribute__((packed)) props;
299
300     int r;
301     struct stream *s;
302
303     *pstream = NULL;
304
305     s = calloc(1, sizeof(struct stream));
306     if (!s)
307         return -ENOMEM;
308
309     r = read(fd, &props, sizeof(props));
310     if (r != sizeof(props))
311         goto fail;
312
313     if (memcmp(props.stream_type, stream_type_audio_guid, 16) == 0)
314         s->base.type = LMS_STREAM_TYPE_AUDIO;
315     else if (memcmp(props.stream_type, stream_type_video_guid, 16) == 0)
316         s->base.type = LMS_STREAM_TYPE_VIDEO;
317     else {
318         /* ignore stream */
319         goto fail;
320     }
321
322     s->base.stream_id = le16toh(props.flags) & 0x7F;
323     /* Not a valid stream */
324     if (!s->base.stream_id)
325         goto fail;
326
327     if (s->base.type == LMS_STREAM_TYPE_AUDIO) {
328         if (le32toh(props.type_specific_len) < 18)
329             goto done;
330
331         s->base.codec = _audio_codec_id_to_str(_read_word(fd));
332         s->base.audio.channels = _read_word(fd);
333         s->priv.sampling_rate = _read_dword(fd);
334         s->base.audio.bitrate = _read_dword(fd) * 8;
335         r += 12;
336     } else {
337         struct {
338             uint32_t width_unused;
339             uint32_t height_unused;
340             uint8_t reserved;
341             uint16_t data_size_unused;
342             /* data */
343             uint32_t size;
344             uint32_t width;
345             uint32_t height;
346             uint16_t reseved2;
347             uint16_t bits_per_pixel;
348             uint8_t compression_id[4];
349             uint32_t image_size;
350
351             /* other fields are ignored */
352         } __attribute__((packed)) video;
353         int r2 = read(fd, &video, sizeof(video));
354         unsigned int num, den;
355
356         if (r2 < 0)
357             goto done;
358
359         r += r2;
360         if ((unsigned int) r2 < get_le32(&video.size) -
361             (sizeof(video) - offsetof(typeof(video), width)))
362             goto done;
363
364         s->base.codec = _video_codec_id_to_str(video.compression_id);
365         s->base.video.width = get_le32(&video.width);
366         s->base.video.height = get_le32(&video.height);
367
368         reduce_gcd(s->base.video.width, s->base.video.height, &num, &den);
369         asprintf(&s->base.video.aspect_ratio.str, "%u:%u", num, den);
370         s->base.video.aspect_ratio.len = s->base.video.aspect_ratio.str ?
371             strlen(s->base.video.aspect_ratio.str) : 0;
372     }
373
374 done:
375     *pstream = s;
376     return r;
377
378 fail:
379     free(s);
380     return r;
381 }
382
383 static void
384 _parse_content_description(lms_charset_conv_t *cs_conv, int fd, struct asf_info *info)
385 {
386     int title_length = _read_word(fd);
387     int artist_length = _read_word(fd);
388     int copyright_length = _read_word(fd);
389     int comment_length = _read_word(fd);
390     int rating_length = _read_word(fd);
391
392     _read_string(fd, title_length, &info->title.str, &info->title.len);
393     lms_charset_conv_force(cs_conv, &info->title.str, &info->title.len);
394     _read_string(fd, artist_length, &info->artist.str, &info->artist.len);
395     lms_charset_conv_force(cs_conv, &info->artist.str, &info->artist.len);
396     /* ignore copyright, comment and rating */
397     lseek(fd, copyright_length + comment_length + rating_length, SEEK_CUR);
398 }
399
400 static void
401 _parse_attribute_name(int fd,
402                       char **attr_name,
403                       unsigned int *attr_name_len,
404                       int *attr_type,
405                       int *attr_size)
406 {
407     int attr_name_length;
408
409     attr_name_length = _read_word(fd);
410     _read_string(fd, attr_name_length, attr_name, attr_name_len);
411     *attr_type = _read_word(fd);
412     *attr_size = _read_word(fd);
413 }
414
415 static void
416 _parse_attribute_string_data(lms_charset_conv_t *cs_conv,
417                              int fd,
418                              int attr_size,
419                              char **attr_data,
420                              unsigned int *attr_data_len)
421 {
422     _read_string(fd, attr_size, attr_data, attr_data_len);
423     lms_charset_conv_force(cs_conv, attr_data, attr_data_len);
424 }
425
426 static void
427 _skip_attribute_data(int fd, int kind, int attr_type, int attr_size)
428 {
429     switch (attr_type) {
430     case ATTR_TYPE_WORD:
431         lseek(fd, 2, SEEK_CUR);
432         break;
433
434     case ATTR_TYPE_BOOL:
435         if (kind == 0)
436             lseek(fd, 4, SEEK_CUR);
437         else
438             lseek(fd, 2, SEEK_CUR);
439         break;
440
441     case ATTR_TYPE_DWORD:
442         lseek(fd, 4, SEEK_CUR);
443         break;
444
445     case ATTR_TYPE_QWORD:
446         lseek(fd, 8, SEEK_CUR);
447         break;
448
449     case ATTR_TYPE_UNICODE:
450     case ATTR_TYPE_BYTES:
451     case ATTR_TYPE_GUID:
452         lseek(fd, attr_size, SEEK_CUR);
453         break;
454
455     default:
456         break;
457     }
458 }
459
460 static void
461 _parse_extended_content_description_object(lms_charset_conv_t *cs_conv, int fd, struct asf_info *info)
462 {
463     int count = _read_word(fd);
464     char *attr_name;
465     unsigned int attr_name_len;
466     int attr_type, attr_size;
467     while (count--) {
468         attr_name = NULL;
469         _parse_attribute_name(fd,
470                               &attr_name, &attr_name_len,
471                               &attr_type, &attr_size);
472         if (attr_type == ATTR_TYPE_UNICODE) {
473             if (memcmp(attr_name, attr_name_wm_album_title, attr_name_len) == 0)
474                 _parse_attribute_string_data(cs_conv,
475                                              fd, attr_size,
476                                              &info->album.str,
477                                              &info->album.len);
478             else if (memcmp(attr_name, attr_name_wm_genre, attr_name_len) == 0)
479                 _parse_attribute_string_data(cs_conv,
480                                              fd, attr_size,
481                                              &info->genre.str,
482                                              &info->genre.len);
483             else if (memcmp(attr_name, attr_name_wm_album_artist, attr_name_len) == 0)
484                 _parse_attribute_string_data(cs_conv,
485                                              fd, attr_size,
486                                              &info->artist.str,
487                                              &info->artist.len);
488             else if (memcmp(attr_name, attr_name_wm_track_number, attr_name_len) == 0) {
489                 char *trackno;
490                 unsigned int trackno_len;
491                 _parse_attribute_string_data(cs_conv,
492                                              fd, attr_size,
493                                              &trackno,
494                                              &trackno_len);
495                 if (trackno) {
496                     info->trackno = atoi(trackno);
497                     free(trackno);
498                 }
499             }
500             else
501                 _skip_attribute_data(fd, 0, attr_type, attr_size);
502         }
503         else
504             _skip_attribute_data(fd, 0, attr_type, attr_size);
505         free(attr_name);
506     }
507 }
508
509 static void *
510 _match(struct plugin *p, const char *path, int len, int base)
511 {
512     long i;
513
514     i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
515     if (i < 0)
516       return NULL;
517     else
518       return (void*)(i + 1);
519 }
520
521 static void streams_free(struct stream *streams)
522 {
523     while (streams) {
524         struct stream *s = streams;
525         streams = (struct stream *) s->base.next;
526
527         switch (s->base.type) {
528         case LMS_STREAM_TYPE_VIDEO:
529             free(s->base.video.aspect_ratio.str);
530             break;
531         default:
532             break;
533         }
534
535         free(s);
536     }
537 }
538
539 /* TODO:
540  *   Parse the "Extended Stream Properties Object" (sec. 4.1). It contains some
541  *   missing fields: bitrate and language
542  *
543  *   It may also contain the "Stream Properties Object" embedded in it.
544  *   For language we also need to parse "Language List Object" (sec 4.6) which
545  *   contains an array with all the languages used (they are in UTF-16, so they
546  *   need to be properly converted).
547  *
548  *   Oh, well... there's also the optional "Stream Bitrate Properties Object"
549  *   which also may contain the bitrate. This property must be very important so
550  *   they duplicated it everywhere.
551  *
552  *   Apparently the length can also be obtained from the "Extended Stream
553  *   Properties Object": start_time, end_time (paying attention to the preroll
554  *   field in the header).
555  *
556  *   Knowing the length, frame rate can be calculated with the "AverageTime Per
557  *   Frame" field of the "Extended Stream Properties Object"
558  */
559 static int
560 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
561 {
562     struct asf_info info = { .type = LMS_STREAM_TYPE_UNKNOWN };
563     int r, fd, num_objects, i;
564     char guid[16];
565     unsigned int size;
566
567     fd = open(finfo->path, O_RDONLY);
568     if (fd < 0) {
569         perror("open");
570         return -1;
571     }
572
573     if (read(fd, &guid, 16) != 16) {
574         perror("read");
575         r = -2;
576         goto done;
577     }
578     if (memcmp(guid, header_guid, 16) != 0) {
579         fprintf(stderr, "ERROR: invalid header (%s).\n", finfo->path);
580         r = -3;
581         goto done;
582     }
583
584     size = _read_qword(fd);
585     num_objects = _read_dword(fd);
586
587     lseek(fd, 2, SEEK_CUR);
588
589     for (i = 0; i < num_objects; ++i) {
590         read(fd, &guid, 16);
591         size = _read_qword(fd);
592
593         if (memcmp(guid, file_properties_guid, 16) == 0) {
594             r = _parse_file_properties(fd, &info);
595             if (r < 0)
596                 goto done;
597             lseek(fd, size - (24 + r), SEEK_CUR);
598         } else if (memcmp(guid, stream_properties_guid, 16) == 0) {
599             struct stream *s = NULL;
600             r = _parse_stream_properties(fd, &s);
601             if (r < 0)
602                 goto done;
603
604             lseek(fd, size - (24 + r), SEEK_CUR);
605
606             if (s) {
607                 if (info.type != LMS_STREAM_TYPE_VIDEO)
608                     info.type = s->base.type;
609
610                 s->base.next = (struct lms_stream *) info.streams;
611                 info.streams = s;
612             }
613         } else if (memcmp(guid, content_description_guid, 16) == 0)
614             _parse_content_description(plugin->cs_conv, fd, &info);
615         else if (memcmp(guid, extended_content_description_guid, 16) == 0)
616             _parse_extended_content_description_object(plugin->cs_conv, fd,
617                                                        &info);
618         else if (memcmp(guid, content_encryption_object_guid, 16) == 0 ||
619                  memcmp(guid, extended_content_encryption_object_guid, 16) == 0) {
620             /* ignore DRM'd files */
621             fprintf(stderr, "ERROR: ignoring DRM'd file %s\n", finfo->path);
622             r = -4;
623             goto done;
624         } else
625             lseek(fd, size - 24, SEEK_CUR);
626     }
627
628     /* try to define stream type by extension */
629     if (info.type == LMS_STREAM_TYPE_UNKNOWN) {
630         long ext_idx = ((long)match) - 1;
631         if (strcmp(_exts[ext_idx].str, ".wma") == 0)
632             info.type = LMS_STREAM_TYPE_AUDIO;
633         /* consider wmv and asf as video */
634         else
635             info.type = LMS_STREAM_TYPE_VIDEO;
636     }
637
638     lms_string_size_strip_and_free(&info.title);
639     lms_string_size_strip_and_free(&info.artist);
640     lms_string_size_strip_and_free(&info.album);
641     lms_string_size_strip_and_free(&info.genre);
642
643     if (!info.title.str)
644         info.title = str_extract_name_from_path(finfo->path, finfo->path_len,
645                                                 finfo->base,
646                                                 &_exts[((long) match) - 1],
647                                                 ctxt->cs_conv);
648
649     if (info.type == LMS_STREAM_TYPE_AUDIO) {
650         struct lms_audio_info audio_info = { };
651
652         audio_info.id = finfo->id;
653         audio_info.title = info.title;
654         audio_info.artist = info.artist;
655         audio_info.album = info.album;
656         audio_info.genre = info.genre;
657         audio_info.trackno = info.trackno;
658         audio_info.length = info.length;
659         audio_info.container = _container;
660
661         /* ignore additional streams, use only the first one */
662         if (info.streams) {
663             struct stream *s = info.streams;
664             audio_info.channels = s->base.audio.channels;
665             audio_info.bitrate = s->base.audio.bitrate;
666             audio_info.sampling_rate = s->priv.sampling_rate;
667             audio_info.codec = s->base.codec;
668         }
669         r = lms_db_audio_add(plugin->audio_db, &audio_info);
670     } else {
671         struct lms_video_info video_info = { };
672
673         video_info.id = finfo->id;
674         video_info.title = info.title;
675         video_info.artist = info.artist;
676         video_info.streams = (struct lms_stream *) info.streams;
677         r = lms_db_video_add(plugin->video_db, &video_info);
678     }
679
680 done:
681     streams_free(info.streams);
682
683     free(info.title.str);
684     free(info.artist.str);
685     free(info.album.str);
686     free(info.genre.str);
687
688     posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
689     close(fd);
690
691     return r;
692 }
693
694 static int
695 _setup(struct plugin *plugin, struct lms_context *ctxt)
696 {
697     plugin->audio_db = lms_db_audio_new(ctxt->db);
698     if (!plugin->audio_db)
699         return -1;
700     plugin->video_db = lms_db_video_new(ctxt->db);
701     if (!plugin->video_db)
702         return -1;
703     plugin->cs_conv = lms_charset_conv_new();
704     if (!plugin->cs_conv)
705         return -1;
706     lms_charset_conv_add(plugin->cs_conv, "UTF-16LE");
707
708     return 0;
709 }
710
711 static int
712 _start(struct plugin *plugin, struct lms_context *ctxt)
713 {
714     int r;
715     r = lms_db_audio_start(plugin->audio_db);
716     r |= lms_db_video_start(plugin->video_db);
717     return r;
718 }
719
720 static int
721 _finish(struct plugin *plugin, struct lms_context *ctxt)
722 {
723     if (plugin->audio_db)
724         lms_db_audio_free(plugin->audio_db);
725     if (plugin->video_db)
726         lms_db_video_free(plugin->video_db);
727     if (plugin->cs_conv)
728         lms_charset_conv_free(plugin->cs_conv);
729
730     return 0;
731 }
732
733 static int
734 _close(struct plugin *plugin)
735 {
736     free(plugin);
737     return 0;
738 }
739
740 API struct lms_plugin *
741 lms_plugin_open(void)
742 {
743     struct plugin *plugin;
744
745     plugin = (struct plugin *)malloc(sizeof(*plugin));
746     plugin->plugin.name = _name;
747     plugin->plugin.match = (lms_plugin_match_fn_t)_match;
748     plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
749     plugin->plugin.close = (lms_plugin_close_fn_t)_close;
750     plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
751     plugin->plugin.start = (lms_plugin_start_fn_t)_start;
752     plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
753
754     return (struct lms_plugin *)plugin;
755 }
756
757 API const struct lms_plugin_info *
758 lms_plugin_info(void)
759 {
760     static struct lms_plugin_info info = {
761         _name,
762         _cats,
763         "Microsoft WMA, WMV and ASF",
764         PACKAGE_VERSION,
765         _authors,
766         "http://lms.garage.maemo.org"
767     };
768
769     return &info;
770 }