2 * Copyright (C) 2008 by INdT
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.
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.
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.
18 * @author Andre Moreira Magalhaes <andre.magalhaes@openbossa.org>
24 * asf/wma file parser.
31 #define _XOPEN_SOURCE 600
33 #include <lightmediascanner_plugin.h>
34 #include <lightmediascanner_db.h>
36 #include <sys/types.h>
44 #define NSEC100_PER_SEC 10000000ULL
45 #define MSEC_PER_SEC 1000ULL
48 STREAM_TYPE_UNKNOWN = 0,
54 ATTR_TYPE_UNICODE = 0,
64 struct lms_string_size title;
65 struct lms_string_size artist;
66 struct lms_string_size album;
67 struct lms_string_size genre;
68 unsigned char trackno;
72 struct lms_plugin plugin;
73 lms_db_audio_t *audio_db;
74 lms_db_video_t *video_db;
75 lms_charset_conv_t *cs_conv;
78 static const char _name[] = "asf";
79 static const struct lms_string_size _exts[] = {
80 LMS_STATIC_STRING_SIZE(".wma"),
81 LMS_STATIC_STRING_SIZE(".wmv"),
82 LMS_STATIC_STRING_SIZE(".asf")
84 static const char *_cats[] = {
89 static const char *_authors[] = {
90 "Andre Moreira Magalhaes",
96 * Microsoft defines these 16-byte (128-bit) GUIDs as:
97 * first 8 bytes are in little-endian order
98 * next 8 bytes are in big-endian order
100 * Eg.: AaBbCcDd-EeFf-GgHh-IiJj-KkLlMmNnOoPp:
102 * to convert to byte string do as follow:
104 * $Dd $Cc $Bb $Aa $Ff $Ee $Hh $Gg $Ii $Jj $Kk $Ll $Mm $Nn $Oo $Pp
106 * See http://www.microsoft.com/windows/windowsmedia/forpros/format/asfspec.aspx
108 static const char header_guid[16] = "\x30\x26\xB2\x75\x8E\x66\xCF\x11\xA6\xD9\x00\xAA\x00\x62\xCE\x6C";
109 static const char file_properties_guid[16] = "\xA1\xDC\xAB\x8C\x47\xA9\xCF\x11\x8E\xE4\x00\xC0\x0C\x20\x53\x65";
110 static const char stream_properties_guid[16] = "\x91\x07\xDC\xB7\xB7\xA9\xCF\x11\x8E\xE6\x00\xC0\x0C\x20\x53\x65";
111 static const char stream_type_audio_guid[16] = "\x40\x9E\x69\xF8\x4D\x5B\xCF\x11\xA8\xFD\x00\x80\x5F\x5C\x44\x2B";
112 static const char stream_type_video_guid[16] = "\xC0\xEF\x19\xBC\x4D\x5B\xCF\x11\xA8\xFD\x00\x80\x5F\x5C\x44\x2B";
113 static const char content_description_guid[16] = "\x33\x26\xB2\x75\x8E\x66\xCF\x11\xA6\xD9\x00\xAA\x00\x62\xCE\x6C";
114 static const char extended_content_description_guid[16] = "\x40\xA4\xD0\xD2\x07\xE3\xD2\x11\x97\xF0\x00\xA0\xC9\x5E\xA8\x50";
115 static const char header_extension_guid[16] = "\xb5\x03\xbf_.\xa9\xcf\x11\x8e\xe3\x00\xc0\x0c Se";
116 static const char metadata_guid[16] = "\xEA\xCB\xF8\xC5\xAF[wH\204g\xAA\214D\xFAL\xCA";
117 static const char metadata_library_guid[16] = "\224\034#D\230\224\321I\241A\x1d\x13NEpT";
118 static const char content_encryption_object_guid[16] = "\xFB\xB3\x11\x22\x23\xBD\xD2\x11\xB4\xB7\x00\xA0\xC9\x55\xFC\x6E";
119 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";
121 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";
122 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";
123 static const char attr_name_wm_genre[16] = "\x57\x00\x4d\x00\x2f\x00\x47\x00\x65\x00\x6e\x00\x72\x00\x65\x00";
124 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";
127 _to_number(const char *data, unsigned int type_size, unsigned int data_size)
130 unsigned int last, i;
132 last = data_size > type_size ? type_size : data_size;
134 for (i = 0; i < last; i++)
135 sum |= (unsigned char) (data[i]) << (i * 8);
144 if (read(fd, &v, 2) != 2)
146 return (short) _to_number(v, sizeof(unsigned short), 2);
153 if (read(fd, &v, 4) != 4)
155 return (unsigned int) _to_number(v, sizeof(unsigned int), 4);
162 if (read(fd, &v, 8) != 8)
164 return _to_number(v, sizeof(unsigned long long), 8);
168 _read_string(int fd, size_t count, char **str, unsigned int *len)
171 ssize_t data_size, size;
174 lseek(fd, count, SEEK_CUR);
178 data = malloc(sizeof(char) * count);
179 data_size = read(fd, data, count);
180 if (data_size == -1) {
187 if (data[size - 1] != '\0' || data[size - 2] != '\0')
199 _parse_file_properties(lms_charset_conv_t *cs_conv, int fd,
200 struct lms_audio_info *info)
205 uint64_t creation_date;
206 uint64_t data_packets_count;
207 uint64_t play_duration;
208 uint64_t send_duration;
211 uint32_t min_data_packet_size;
212 uint32_t max_data_packet_size;
213 uint32_t max_bitrate;
214 } __attribute__((packed)) props;
217 r = read(fd, &props, sizeof(props));
218 if (r != sizeof(props))
222 if (le32toh(props.flags) & 0x1)
225 /* ASF spec 01.20.06 sec. 3.2: we need to subtract the preroll value from
226 * the duration in order to obtain the real duration */
227 info->length = (unsigned int)((le64toh(props.play_duration) / NSEC100_PER_SEC)
228 - le64toh(props.preroll) / MSEC_PER_SEC);
234 _parse_content_description(lms_charset_conv_t *cs_conv, int fd, struct asf_info *info)
236 int title_length = _read_word(fd);
237 int artist_length = _read_word(fd);
238 int copyright_length = _read_word(fd);
239 int comment_length = _read_word(fd);
240 int rating_length = _read_word(fd);
242 _read_string(fd, title_length, &info->title.str, &info->title.len);
243 lms_charset_conv_force(cs_conv, &info->title.str, &info->title.len);
244 _read_string(fd, artist_length, &info->artist.str, &info->artist.len);
245 lms_charset_conv_force(cs_conv, &info->artist.str, &info->artist.len);
246 /* ignore copyright, comment and rating */
247 lseek(fd, copyright_length + comment_length + rating_length, SEEK_CUR);
251 _parse_attribute_name(int fd,
254 unsigned int *attr_name_len,
258 int attr_name_length;
260 /* extended content descriptor */
262 attr_name_length = _read_word(fd);
263 _read_string(fd, attr_name_length, attr_name, attr_name_len);
264 *attr_type = _read_word(fd);
265 *attr_size = _read_word(fd);
267 /* metadata & metadata library */
269 lseek(fd, 2 + 2, SEEK_CUR); /* language and stream */
270 attr_name_length = _read_word(fd);
271 *attr_type = _read_word(fd);
272 *attr_size = _read_dword(fd);
273 _read_string(fd, attr_name_length, attr_name, attr_name_len);
278 _parse_attribute_string_data(lms_charset_conv_t *cs_conv,
282 unsigned int *attr_data_len)
284 _read_string(fd, attr_size, attr_data, attr_data_len);
285 lms_charset_conv_force(cs_conv, attr_data, attr_data_len);
289 _skip_attribute_data(int fd, int kind, int attr_type, int attr_size)
293 lseek(fd, 2, SEEK_CUR);
298 lseek(fd, 4, SEEK_CUR);
300 lseek(fd, 2, SEEK_CUR);
303 case ATTR_TYPE_DWORD:
304 lseek(fd, 4, SEEK_CUR);
307 case ATTR_TYPE_QWORD:
308 lseek(fd, 8, SEEK_CUR);
311 case ATTR_TYPE_UNICODE:
312 case ATTR_TYPE_BYTES:
314 lseek(fd, attr_size, SEEK_CUR);
323 _skip_attribute(int fd, int kind)
325 int attr_type, attr_size;
326 _parse_attribute_name(fd, kind, NULL, NULL, &attr_type, &attr_size);
327 _skip_attribute_data(fd, kind, attr_type, attr_size);
331 _parse_extended_content_description_object(lms_charset_conv_t *cs_conv, int fd, struct asf_info *info)
333 int count = _read_word(fd);
335 unsigned int attr_name_len;
336 int attr_type, attr_size;
339 _parse_attribute_name(fd, 0,
340 &attr_name, &attr_name_len,
341 &attr_type, &attr_size);
342 if (attr_type == ATTR_TYPE_UNICODE) {
343 if (memcmp(attr_name, attr_name_wm_album_title, attr_name_len) == 0)
344 _parse_attribute_string_data(cs_conv,
348 else if (memcmp(attr_name, attr_name_wm_genre, attr_name_len) == 0)
349 _parse_attribute_string_data(cs_conv,
353 else if (memcmp(attr_name, attr_name_wm_album_artist, attr_name_len) == 0)
354 _parse_attribute_string_data(cs_conv,
358 else if (memcmp(attr_name, attr_name_wm_track_number, attr_name_len) == 0) {
360 unsigned int trackno_len;
361 _parse_attribute_string_data(cs_conv,
366 info->trackno = atoi(trackno);
371 _skip_attribute_data(fd, 0, attr_type, attr_size);
374 _skip_attribute_data(fd, 0, attr_type, attr_size);
381 _skip_metadata(int fd)
383 int count = _read_word(fd);
385 _skip_attribute(fd, 1);
389 _skip_metadata_library(int fd)
391 int count = _read_word(fd);
393 _skip_attribute(fd, 2);
397 _skip_header_extension(int fd)
400 long long size, data_size, data_pos;
402 lseek(fd, 18, SEEK_CUR);
403 data_size = _read_dword(fd);
405 while (data_pos < data_size) {
407 size = _read_qword(fd);
410 if (memcmp(guid, metadata_guid, 16) == 0)
412 else if (memcmp(guid, metadata_library_guid, 16) == 0)
413 _skip_metadata_library(fd);
415 lseek(fd, size - 24, SEEK_CUR);
421 _match(struct plugin *p, const char *path, int len, int base)
425 i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
429 return (void*)(i + 1);
433 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
435 struct asf_info info = { };
436 struct lms_audio_info audio_info = { };
437 struct lms_video_info video_info = { };
438 int r, fd, num_objects, i;
441 int stream_type = STREAM_TYPE_UNKNOWN;
443 fd = open(finfo->path, O_RDONLY);
449 if (read(fd, &guid, 16) != 16) {
454 if (memcmp(guid, header_guid, 16) != 0) {
455 fprintf(stderr, "ERROR: invalid header (%s).\n", finfo->path);
460 size = _read_qword(fd);
461 num_objects = _read_dword(fd);
463 lseek(fd, 2, SEEK_CUR);
465 for (i = 0; i < num_objects; ++i) {
467 size = _read_qword(fd);
469 if (memcmp(guid, file_properties_guid, 16) == 0) {
470 r = _parse_file_properties(plugin->cs_conv, fd, &audio_info);
473 lseek(fd, size - (24 + r), SEEK_CUR);
474 } else if (memcmp(guid, stream_properties_guid, 16) == 0) {
476 if (memcmp(guid, stream_type_audio_guid, 16) == 0)
477 stream_type = STREAM_TYPE_AUDIO;
478 else if (memcmp(guid, stream_type_video_guid, 16) == 0)
479 stream_type = STREAM_TYPE_VIDEO;
480 lseek(fd, size - 40, SEEK_CUR);
482 else if (memcmp(guid, content_description_guid, 16) == 0)
483 _parse_content_description(plugin->cs_conv, fd, &info);
484 else if (memcmp(guid, extended_content_description_guid, 16) == 0)
485 _parse_extended_content_description_object(plugin->cs_conv, fd, &info);
486 else if (memcmp(guid, header_extension_guid, 16) == 0)
487 _skip_header_extension(fd);
488 else if (memcmp(guid, content_encryption_object_guid, 16) == 0 ||
489 memcmp(guid, extended_content_encryption_object_guid, 16) == 0) {
490 /* ignore DRM'd files */
491 fprintf(stderr, "ERROR: ignoring DRM'd file %s\n", finfo->path);
496 lseek(fd, size - 24, SEEK_CUR);
499 /* try to define stream type by extension */
500 if (stream_type == STREAM_TYPE_UNKNOWN) {
501 long ext_idx = ((long)match) - 1;
502 if (strcmp(_exts[ext_idx].str, ".wma") == 0)
503 stream_type = STREAM_TYPE_AUDIO;
504 /* consider wmv and asf as video */
506 stream_type = STREAM_TYPE_VIDEO;
509 lms_string_size_strip_and_free(&info.title);
510 lms_string_size_strip_and_free(&info.artist);
511 lms_string_size_strip_and_free(&info.album);
512 lms_string_size_strip_and_free(&info.genre);
514 if (!info.title.str) {
516 ext_idx = ((long)match) - 1;
517 info.title.len = finfo->path_len - finfo->base - _exts[ext_idx].len;
518 info.title.str = malloc((info.title.len + 1) * sizeof(char));
519 memcpy(info.title.str, finfo->path + finfo->base, info.title.len);
520 info.title.str[info.title.len] = '\0';
521 lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
525 fprintf(stderr, "file %s info\n", finfo->path);
526 fprintf(stderr, "\ttitle='%s'\n", info.title.str);
527 fprintf(stderr, "\tartist='%s'\n", info.artist.str);
528 fprintf(stderr, "\talbum='%s'\n", info.album.str);
529 fprintf(stderr, "\tgenre='%s'\n", info.genre.str);
530 fprintf(stderr, "\ttrackno=%d\n", info.trackno);
533 if (stream_type == STREAM_TYPE_AUDIO) {
534 audio_info.id = finfo->id;
535 audio_info.title = info.title;
536 audio_info.artist = info.artist;
537 audio_info.album = info.album;
538 audio_info.genre = info.genre;
539 audio_info.trackno = info.trackno;
540 r = lms_db_audio_add(plugin->audio_db, &audio_info);
543 video_info.id = finfo->id;
544 video_info.title = info.title;
545 video_info.artist = info.artist;
546 r = lms_db_video_add(plugin->video_db, &video_info);
551 free(info.title.str);
553 free(info.artist.str);
555 free(info.album.str);
557 free(info.genre.str);
559 posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
566 _setup(struct plugin *plugin, struct lms_context *ctxt)
568 plugin->audio_db = lms_db_audio_new(ctxt->db);
569 if (!plugin->audio_db)
571 plugin->video_db = lms_db_video_new(ctxt->db);
572 if (!plugin->video_db)
574 plugin->cs_conv = lms_charset_conv_new();
575 if (!plugin->cs_conv)
577 lms_charset_conv_add(plugin->cs_conv, "UTF-16LE");
583 _start(struct plugin *plugin, struct lms_context *ctxt)
586 r = lms_db_audio_start(plugin->audio_db);
587 r |= lms_db_video_start(plugin->video_db);
592 _finish(struct plugin *plugin, struct lms_context *ctxt)
594 if (plugin->audio_db)
595 lms_db_audio_free(plugin->audio_db);
596 if (plugin->video_db)
597 lms_db_video_free(plugin->video_db);
599 lms_charset_conv_free(plugin->cs_conv);
605 _close(struct plugin *plugin)
611 API struct lms_plugin *
612 lms_plugin_open(void)
614 struct plugin *plugin;
616 plugin = (struct plugin *)malloc(sizeof(*plugin));
617 plugin->plugin.name = _name;
618 plugin->plugin.match = (lms_plugin_match_fn_t)_match;
619 plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
620 plugin->plugin.close = (lms_plugin_close_fn_t)_close;
621 plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
622 plugin->plugin.start = (lms_plugin_start_fn_t)_start;
623 plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
625 return (struct lms_plugin *)plugin;
628 API const struct lms_plugin_info *
629 lms_plugin_info(void)
631 static struct lms_plugin_info info = {
634 "Microsoft WMA, WMV and ASF",
637 "http://lms.garage.maemo.org"