MiniDLNA update: 1.0.19.1 to 1.0.20
[tomato.git] / release / src / router / minidlna / metadata.c
blob049b2e2d398d3325361fe71d172f276a815de455
1 /* MiniDLNA media server
2 * Copyright (C) 2008-2009 Justin Maggard
4 * This file is part of MiniDLNA.
6 * MiniDLNA is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * MiniDLNA is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with MiniDLNA. If not, see <http://www.gnu.org/licenses/>.
18 #include <stdio.h>
19 #include <ctype.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <sys/stat.h>
24 #include <unistd.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
29 #include <libexif/exif-loader.h>
30 #include "image_utils.h"
31 #include <jpeglib.h>
32 #include <setjmp.h>
33 #include <avutil.h>
34 #include <avcodec.h>
35 #include <avformat.h>
36 #include "tagutils/tagutils.h"
38 #include "upnpglobalvars.h"
39 #include "upnpreplyparse.h"
40 #include "metadata.h"
41 #include "albumart.h"
42 #include "utils.h"
43 #include "sql.h"
44 #include "log.h"
46 #ifndef FF_PROFILE_H264_BASELINE
47 #define FF_PROFILE_H264_BASELINE 66
48 #endif
49 #ifndef FF_PROFILE_H264_MAIN
50 #define FF_PROFILE_H264_MAIN 77
51 #endif
52 #ifndef FF_PROFILE_H264_HIGH
53 #define FF_PROFILE_H264_HIGH 100
54 #endif
56 #define FLAG_TITLE 0x00000001
57 #define FLAG_ARTIST 0x00000002
58 #define FLAG_ALBUM 0x00000004
59 #define FLAG_GENRE 0x00000008
60 #define FLAG_COMMENT 0x00000010
61 #define FLAG_CREATOR 0x00000020
62 #define FLAG_DATE 0x00000040
63 #define FLAG_DLNA_PN 0x00000080
64 #define FLAG_MIME 0x00000100
65 #define FLAG_DURATION 0x00000200
66 #define FLAG_RESOLUTION 0x00000400
67 #define FLAG_BITRATE 0x00000800
68 #define FLAG_FREQUENCY 0x00001000
69 #define FLAG_BPS 0x00002000
70 #define FLAG_CHANNELS 0x00004000
72 /* Audio profile flags */
73 enum audio_profiles {
74 PROFILE_AUDIO_UNKNOWN,
75 PROFILE_AUDIO_MP3,
76 PROFILE_AUDIO_AC3,
77 PROFILE_AUDIO_WMA_BASE,
78 PROFILE_AUDIO_WMA_FULL,
79 PROFILE_AUDIO_WMA_PRO,
80 PROFILE_AUDIO_MP2,
81 PROFILE_AUDIO_PCM,
82 PROFILE_AUDIO_AAC,
83 PROFILE_AUDIO_AAC_MULT5,
84 PROFILE_AUDIO_AMR
87 /* This function shamelessly copied from libdlna */
88 #define MPEG_TS_SYNC_CODE 0x47
89 #define MPEG_TS_PACKET_LENGTH 188 /* prepends 4 bytes to TS packet */
90 #define MPEG_TS_PACKET_LENGTH_DLNA 192 /* prepends 4 bytes to TS packet */
91 int
92 dlna_timestamp_is_present(const char * filename)
94 unsigned char buffer[2*MPEG_TS_PACKET_LENGTH_DLNA+1];
95 int fd, i;
97 /* read file header */
98 fd = open(filename, O_RDONLY);
99 read(fd, buffer, MPEG_TS_PACKET_LENGTH_DLNA*2);
100 close(fd);
101 for( i=0; i < MPEG_TS_PACKET_LENGTH_DLNA; i++ )
103 if( buffer[i] == MPEG_TS_SYNC_CODE )
105 if (buffer[i + MPEG_TS_PACKET_LENGTH_DLNA] == MPEG_TS_SYNC_CODE)
107 if (buffer[i+MPEG_TS_PACKET_LENGTH] == 0x00 &&
108 buffer[i+MPEG_TS_PACKET_LENGTH+1] == 0x00 &&
109 buffer[i+MPEG_TS_PACKET_LENGTH+2] == 0x00 &&
110 buffer[i+MPEG_TS_PACKET_LENGTH+3] == 0x00)
111 break;
112 else
113 return 1;
117 return 0;
120 #ifdef TIVO_SUPPORT
122 is_tivo_file(const char * path)
124 unsigned char buf[5];
125 unsigned char hdr[5] = { 'T','i','V','o','\0' };
126 int fd;
128 /* read file header */
129 fd = open(path, O_RDONLY);
130 read(fd, buf, 5);
131 close(fd);
133 return( !memcmp(buf, hdr, 5) );
135 #endif
137 void
138 check_for_captions(const char * path, sqlite_int64 detailID)
140 char *file = malloc(PATH_MAX);
141 char *id = NULL;
143 sprintf(file, "%s", path);
144 strip_ext(file);
146 /* If we weren't given a detail ID, look for one. */
147 if( !detailID )
149 id = sql_get_text_field(db, "SELECT ID from DETAILS where PATH glob '%q.*'"
150 " and MIME glob 'video/*' limit 1", file);
151 if( id )
153 //DEBUG DPRINTF(E_DEBUG, L_METADATA, "New file %s looks like a caption file.\n", path);
154 detailID = strtoll(id, NULL, 10);
156 else
158 //DPRINTF(E_DEBUG, L_METADATA, "No file found for caption %s.\n", path);
159 goto no_source_video;
163 strcat(file, ".srt");
164 if( access(file, R_OK) == 0 )
166 sql_exec(db, "INSERT into CAPTIONS"
167 " (ID, PATH) "
168 "VALUES"
169 " (%lld, %Q)", detailID, file);
171 no_source_video:
172 if( id )
173 sqlite3_free(id);
174 free(file);
177 void
178 parse_nfo(const char * path, metadata_t * m)
180 FILE *nfo;
181 char buf[65536];
182 struct NameValueParserData xml;
183 struct stat file;
184 size_t nread;
185 char *val, *val2;
187 if( stat(path, &file) != 0 ||
188 file.st_size > 65536 )
190 DPRINTF(E_INFO, L_METADATA, "Not parsing very large .nfo file %s\n", path);
191 return;
193 DPRINTF(E_DEBUG, L_METADATA, "Parsing .nfo file: %s\n", path);
194 nfo = fopen(path, "r");
195 if( !nfo )
196 return;
197 nread = fread(&buf, 1, sizeof(buf), nfo);
199 ParseNameValue(buf, nread, &xml);
201 //printf("\ttype: %s\n", GetValueFromNameValueList(&xml, "rootElement"));
202 val = GetValueFromNameValueList(&xml, "title");
203 if( val )
205 val2 = GetValueFromNameValueList(&xml, "episodetitle");
206 if( val2 )
207 asprintf(&m->title, "%s - %s", val, val2);
208 else
209 m->title = strdup(val);
212 val = GetValueFromNameValueList(&xml, "plot");
213 if( val )
214 m->comment = strdup(val);
216 val = GetValueFromNameValueList(&xml, "capturedate");
217 if( val )
218 m->date = strdup(val);
220 val = GetValueFromNameValueList(&xml, "genre");
221 if( val )
222 m->genre = strdup(val);
224 ClearNameValueList(&xml);
225 fclose(nfo);
228 void
229 free_metadata(metadata_t * m, uint32_t flags)
231 if( m->title && (flags & FLAG_TITLE) )
232 free(m->title);
233 if( m->artist && (flags & FLAG_ARTIST) )
234 free(m->artist);
235 if( m->album && (flags & FLAG_ALBUM) )
236 free(m->album);
237 if( m->genre && (flags & FLAG_GENRE) )
238 free(m->genre);
239 if( m->creator && (flags & FLAG_CREATOR) )
240 free(m->creator);
241 if( m->date && (flags & FLAG_DATE) )
242 free(m->date);
243 if( m->comment && (flags & FLAG_COMMENT) )
244 free(m->comment);
245 if( m->dlna_pn && (flags & FLAG_DLNA_PN) )
246 free(m->dlna_pn);
247 if( m->mime && (flags & FLAG_MIME) )
248 free(m->mime);
249 if( m->duration && (flags & FLAG_DURATION) )
250 free(m->duration);
251 if( m->resolution && (flags & FLAG_RESOLUTION) )
252 free(m->resolution);
253 if( m->bitrate && (flags & FLAG_BITRATE) )
254 free(m->bitrate);
255 if( m->frequency && (flags & FLAG_FREQUENCY) )
256 free(m->frequency);
257 if( m->bps && (flags & FLAG_BPS) )
258 free(m->bps);
259 if( m->channels && (flags & FLAG_CHANNELS) )
260 free(m->channels);
263 sqlite_int64
264 GetFolderMetadata(const char * name, const char * path, const char * artist, const char * genre, const char * album_art)
266 int ret;
268 ret = sql_exec(db, "INSERT into DETAILS"
269 " (TITLE, PATH, CREATOR, ARTIST, GENRE, ALBUM_ART) "
270 "VALUES"
271 " ('%q', %Q, %Q, %Q, %Q, %lld);",
272 name, path, artist, artist, genre,
273 album_art ? strtoll(album_art, NULL, 10) : 0);
274 if( ret != SQLITE_OK )
275 ret = 0;
276 else
277 ret = sqlite3_last_insert_rowid(db);
279 return ret;
282 sqlite_int64
283 GetAudioMetadata(const char * path, char * name)
285 char type[4];
286 static char lang[6] = { '\0' };
287 struct stat file;
288 sqlite_int64 ret;
289 char *esc_tag;
290 int i;
291 sqlite_int64 album_art = 0;
292 struct song_metadata song;
293 metadata_t m;
294 uint32_t free_flags = FLAG_MIME|FLAG_DURATION|FLAG_DLNA_PN|FLAG_DATE;
295 memset(&m, '\0', sizeof(metadata_t));
297 if ( stat(path, &file) != 0 )
298 return 0;
299 strip_ext(name);
301 if( ends_with(path, ".mp3") )
303 strcpy(type, "mp3");
304 m.mime = strdup("audio/mpeg");
306 else if( ends_with(path, ".m4a") || ends_with(path, ".mp4") ||
307 ends_with(path, ".aac") || ends_with(path, ".m4p") )
309 strcpy(type, "aac");
310 m.mime = strdup("audio/mp4");
312 else if( ends_with(path, ".3gp") )
314 strcpy(type, "aac");
315 m.mime = strdup("audio/3gpp");
317 else if( ends_with(path, ".wma") || ends_with(path, ".asf") )
319 strcpy(type, "asf");
320 m.mime = strdup("audio/x-ms-wma");
322 else if( ends_with(path, ".flac") || ends_with(path, ".fla") || ends_with(path, ".flc") )
324 strcpy(type, "flc");
325 m.mime = strdup("audio/x-flac");
327 else if( ends_with(path, ".wav") )
329 strcpy(type, "wav");
330 m.mime = strdup("audio/x-wav");
332 else if( ends_with(path, ".ogg") || ends_with(path, ".oga") )
334 strcpy(type, "ogg");
335 m.mime = strdup("audio/ogg");
337 else if( ends_with(path, ".pcm") )
339 strcpy(type, "pcm");
340 m.mime = strdup("audio/L16");
342 else
344 DPRINTF(E_WARN, L_GENERAL, "Unhandled file extension on %s\n", path);
345 return 0;
348 if( !(*lang) )
350 if( !getenv("LANG") )
351 strcpy(lang, "en_US");
352 else
353 strncpy(lang, getenv("LANG"), 5);
354 lang[5] = '\0';
357 if( readtags((char *)path, &song, &file, lang, type) != 0 )
359 DPRINTF(E_WARN, L_GENERAL, "Cannot extract tags from %s!\n", path);
360 freetags(&song);
361 free_metadata(&m, free_flags);
362 return 0;
365 if( song.dlna_pn )
366 asprintf(&m.dlna_pn, "%s;DLNA.ORG_OP=01;DLNA.ORG_CI=0", song.dlna_pn);
367 if( song.year )
368 asprintf(&m.date, "%04d-01-01", song.year);
369 asprintf(&m.duration, "%d:%02d:%02d.%03d",
370 (song.song_length/3600000),
371 (song.song_length/60000%60),
372 (song.song_length/1000%60),
373 (song.song_length%1000));
374 if( song.title && *song.title )
376 m.title = trim(song.title);
377 if( (esc_tag = escape_tag(m.title, 0)) )
379 free_flags |= FLAG_TITLE;
380 m.title = esc_tag;
383 else
385 m.title = name;
387 for( i=ROLE_START; i<N_ROLE; i++ )
389 if( song.contributor[i] && *song.contributor[i] )
391 m.creator = trim(song.contributor[i]);
392 if( strlen(m.creator) > 48 )
394 m.creator = strdup("Various Artists");
395 free_flags |= FLAG_CREATOR;
397 else if( (esc_tag = escape_tag(m.creator, 0)) )
399 m.creator = esc_tag;
400 free_flags |= FLAG_CREATOR;
402 m.artist = m.creator;
403 break;
406 /* If there is a band associated with the album, use it for virtual containers. */
407 if( (i != ROLE_BAND) && (i != ROLE_ALBUMARTIST) )
409 if( song.contributor[ROLE_BAND] && *song.contributor[ROLE_BAND] )
411 i = ROLE_BAND;
412 m.artist = trim(song.contributor[i]);
413 if( strlen(m.artist) > 48 )
415 m.artist = strdup("Various Artists");
416 free_flags |= FLAG_ARTIST;
418 else if( (esc_tag = escape_tag(m.artist, 0)) )
420 m.artist = esc_tag;
421 free_flags |= FLAG_ARTIST;
425 if( song.album && *song.album )
427 m.album = trim(song.album);
428 if( (esc_tag = escape_tag(m.album, 0)) )
430 free_flags |= FLAG_ALBUM;
431 m.album = esc_tag;
434 if( song.genre && *song.genre )
436 m.genre = trim(song.genre);
437 if( (esc_tag = escape_tag(m.genre, 0)) )
439 free_flags |= FLAG_GENRE;
440 m.genre = esc_tag;
443 if( song.comment && *song.comment )
445 m.comment = trim(song.comment);
446 if( (esc_tag = escape_tag(m.comment, 0)) )
448 free_flags |= FLAG_COMMENT;
449 m.comment = esc_tag;
453 album_art = find_album_art(path, song.image, song.image_size);
455 ret = sql_exec(db, "INSERT into DETAILS"
456 " (PATH, SIZE, TIMESTAMP, DURATION, CHANNELS, BITRATE, SAMPLERATE, DATE,"
457 " TITLE, CREATOR, ARTIST, ALBUM, GENRE, COMMENT, DISC, TRACK, DLNA_PN, MIME, ALBUM_ART) "
458 "VALUES"
459 " (%Q, %lld, %ld, '%s', %d, %d, %d, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %d, %d, %Q, '%s', %lld);",
460 path, file.st_size, file.st_mtime, m.duration, song.channels, song.bitrate, song.samplerate, m.date,
461 m.title, m.creator, m.artist, m.album, m.genre, m.comment, song.disc, song.track,
462 m.dlna_pn, song.mime?song.mime:m.mime, album_art);
463 if( ret != SQLITE_OK )
465 fprintf(stderr, "Error inserting details for '%s'!\n", path);
466 ret = 0;
468 else
470 ret = sqlite3_last_insert_rowid(db);
472 freetags(&song);
473 free_metadata(&m, free_flags);
475 return ret;
478 /* For libjpeg error handling */
479 jmp_buf setjmp_buffer;
480 static void
481 libjpeg_error_handler(j_common_ptr cinfo)
483 cinfo->err->output_message (cinfo);
484 longjmp(setjmp_buffer, 1);
485 return;
488 sqlite_int64
489 GetImageMetadata(const char * path, char * name)
491 ExifData *ed;
492 ExifEntry *e = NULL;
493 ExifLoader *l;
494 struct jpeg_decompress_struct cinfo;
495 struct jpeg_error_mgr jerr;
496 FILE *infile;
497 int width=0, height=0, thumb=0;
498 char make[32], model[64] = {'\0'};
499 char b[1024];
500 struct stat file;
501 sqlite_int64 ret;
502 image_s * imsrc;
503 metadata_t m;
504 uint32_t free_flags = 0xFFFFFFFF;
505 memset(&m, '\0', sizeof(metadata_t));
507 //DEBUG DPRINTF(E_DEBUG, L_METADATA, "Parsing %s...\n", path);
508 if ( stat(path, &file) != 0 )
509 return 0;
510 strip_ext(name);
511 //DEBUG DPRINTF(E_DEBUG, L_METADATA, " * size: %jd\n", file.st_size);
513 /* MIME hard-coded to JPEG for now, until we add PNG support */
514 asprintf(&m.mime, "image/jpeg");
516 l = exif_loader_new();
517 exif_loader_write_file(l, path);
518 ed = exif_loader_get_data(l);
519 exif_loader_unref(l);
520 if( !ed )
521 goto no_exifdata;
523 e = exif_content_get_entry (ed->ifd[EXIF_IFD_EXIF], EXIF_TAG_DATE_TIME_ORIGINAL);
524 if( e || (e = exif_content_get_entry(ed->ifd[EXIF_IFD_EXIF], EXIF_TAG_DATE_TIME_DIGITIZED)) ) {
525 m.date = strdup(exif_entry_get_value(e, b, sizeof(b)));
526 if( strlen(m.date) > 10 )
528 m.date[4] = '-';
529 m.date[7] = '-';
530 m.date[10] = 'T';
532 else {
533 free(m.date);
534 m.date = NULL;
537 else {
538 /* One last effort to get the date from XMP */
539 image_get_jpeg_date_xmp(path, &m.date);
541 //DEBUG DPRINTF(E_DEBUG, L_METADATA, " * date: %s\n", m.date);
543 e = exif_content_get_entry (ed->ifd[EXIF_IFD_0], EXIF_TAG_MAKE);
544 if( e )
546 strncpy(make, exif_entry_get_value(e, b, sizeof(b)), sizeof(make));
547 e = exif_content_get_entry (ed->ifd[EXIF_IFD_0], EXIF_TAG_MODEL);
548 if( e )
550 strncpy(model, exif_entry_get_value(e, b, sizeof(b)), sizeof(model));
551 if( !strcasestr(model, make) )
552 snprintf(model, sizeof(model), "%s %s", make, exif_entry_get_value(e, b, sizeof(b)));
553 m.creator = strdup(model);
556 //DEBUG DPRINTF(E_DEBUG, L_METADATA, " * model: %s\n", model);
558 if( ed->size )
560 /* We might need to verify that the thumbnail is 160x160 or smaller */
561 if( ed->size > 12000 )
563 imsrc = image_new_from_jpeg(NULL, 0, (char *)ed->data, ed->size, 1);
564 if( imsrc )
566 if( (imsrc->width <= 160) && (imsrc->height <= 160) )
567 thumb = 1;
568 image_free(imsrc);
571 else
572 thumb = 1;
574 //DEBUG DPRINTF(E_DEBUG, L_METADATA, " * thumbnail: %d\n", thumb);
576 exif_data_unref(ed);
578 no_exifdata:
579 /* If SOF parsing fails, then fall through to reading the JPEG data with libjpeg to get the resolution */
580 if( image_get_jpeg_resolution(path, &width, &height) != 0 || !width || !height )
582 infile = fopen(path, "r");
583 cinfo.err = jpeg_std_error(&jerr);
584 jerr.error_exit = libjpeg_error_handler;
585 jpeg_create_decompress(&cinfo);
586 if( setjmp(setjmp_buffer) )
587 goto error;
588 jpeg_stdio_src(&cinfo, infile);
589 jpeg_read_header(&cinfo, TRUE);
590 jpeg_start_decompress(&cinfo);
591 width = cinfo.output_width;
592 height = cinfo.output_height;
593 error:
594 jpeg_destroy_decompress(&cinfo);
595 fclose(infile);
597 //DEBUG DPRINTF(E_DEBUG, L_METADATA, " * resolution: %dx%d\n", width, height);
599 if( !width || !height )
601 free_metadata(&m, free_flags);
602 return 0;
604 if( width <= 640 && height <= 480 )
605 asprintf(&m.dlna_pn, "JPEG_SM;%s", dlna_no_conv);
606 else if( width <= 1024 && height <= 768 )
607 asprintf(&m.dlna_pn, "JPEG_MED;%s", dlna_no_conv);
608 else if( (width <= 4096 && height <= 4096) || !(GETFLAG(DLNA_STRICT_MASK)) )
609 asprintf(&m.dlna_pn, "JPEG_LRG;%s", dlna_no_conv);
610 asprintf(&m.resolution, "%dx%d", width, height);
612 ret = sql_exec(db, "INSERT into DETAILS"
613 " (PATH, TITLE, SIZE, TIMESTAMP, DATE, RESOLUTION, THUMBNAIL, CREATOR, DLNA_PN, MIME) "
614 "VALUES"
615 " (%Q, '%q', %lld, %ld, %Q, %Q, %d, %Q, %Q, %Q);",
616 path, name, file.st_size, file.st_mtime, m.date, m.resolution, thumb, m.creator, m.dlna_pn, m.mime);
617 if( ret != SQLITE_OK )
619 fprintf(stderr, "Error inserting details for '%s'!\n", path);
620 ret = 0;
622 else
624 ret = sqlite3_last_insert_rowid(db);
626 free_metadata(&m, free_flags);
628 return ret;
631 sqlite_int64
632 GetVideoMetadata(const char * path, char * name)
634 struct stat file;
635 int ret, i;
636 struct tm *modtime;
637 AVFormatContext *ctx;
638 AVCodecContext *ac = NULL, *vc = NULL;
639 int audio_stream = -1, video_stream = -1;
640 enum audio_profiles audio_profile = PROFILE_AUDIO_UNKNOWN;
641 tsinfo_t *ts;
642 char fourcc[4];
643 sqlite_int64 album_art = 0;
644 char nfo[PATH_MAX], *ext;
645 struct song_metadata video;
646 metadata_t m;
647 uint32_t free_flags = 0xFFFFFFFF;
648 memset(&m, '\0', sizeof(m));
649 memset(&video, '\0', sizeof(video));
651 //DEBUG DPRINTF(E_DEBUG, L_METADATA, "Parsing video %s...\n", name);
652 if ( stat(path, &file) != 0 )
653 return 0;
654 strip_ext(name);
655 //DEBUG DPRINTF(E_DEBUG, L_METADATA, " * size: %jd\n", file.st_size);
657 av_register_all();
658 if( av_open_input_file(&ctx, path, NULL, 0, NULL) != 0 )
660 DPRINTF(E_WARN, L_METADATA, "Opening %s failed!\n", path);
661 return 0;
663 av_find_stream_info(ctx);
664 //dump_format(ctx, 0, NULL, 0);
665 for( i=0; i<ctx->nb_streams; i++)
667 if( audio_stream == -1 &&
668 ctx->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO )
670 audio_stream = i;
671 ac = ctx->streams[audio_stream]->codec;
672 continue;
674 else if( video_stream == -1 &&
675 ctx->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO )
677 video_stream = i;
678 vc = ctx->streams[video_stream]->codec;
679 continue;
682 /* This must not be a video file. */
683 if( !vc )
685 av_close_input_file(ctx);
686 if( !is_audio(path) )
687 DPRINTF(E_DEBUG, L_METADATA, "File %s does not contain a video stream.\n", basename(path));
688 return 0;
691 strcpy(nfo, path);
692 ext = strrchr(nfo, '.');
693 if( ext )
695 strcpy(ext+1, "nfo");
696 if( access(nfo, F_OK) == 0 )
698 parse_nfo(nfo, &m);
702 if( !m.date )
704 m.date = malloc(20);
705 modtime = localtime(&file.st_mtime);
706 strftime(m.date, 20, "%FT%T", modtime);
709 if( ac )
711 aac_object_type_t aac_type = AAC_INVALID;
712 switch( ac->codec_id )
714 case CODEC_ID_MP3:
715 audio_profile = PROFILE_AUDIO_MP3;
716 break;
717 case CODEC_ID_AAC:
718 if( !ac->extradata_size ||
719 !ac->extradata )
721 DPRINTF(E_DEBUG, L_METADATA, "No AAC type\n");
723 else
725 uint8_t data;
726 memcpy(&data, ac->extradata, 1);
727 aac_type = data >> 3;
729 switch( aac_type )
731 /* AAC Low Complexity variants */
732 case AAC_LC:
733 case AAC_LC_ER:
734 if( ac->sample_rate < 8000 ||
735 ac->sample_rate > 48000 )
737 DPRINTF(E_DEBUG, L_METADATA, "Unsupported AAC: sample rate is not 8000 < %d < 48000\n",
738 ac->sample_rate);
739 break;
741 /* AAC @ Level 1/2 */
742 if( ac->channels <= 2 &&
743 ac->bit_rate <= 576000 )
744 audio_profile = PROFILE_AUDIO_AAC;
745 else if( ac->channels <= 6 &&
746 ac->bit_rate <= 1440000 )
747 audio_profile = PROFILE_AUDIO_AAC_MULT5;
748 else
749 DPRINTF(E_DEBUG, L_METADATA, "Unhandled AAC: %d channels, %d bitrate\n",
750 ac->channels,
751 ac->bit_rate);
752 break;
753 default:
754 DPRINTF(E_DEBUG, L_METADATA, "Unhandled AAC type [%d]\n", aac_type);
755 break;
757 break;
758 case CODEC_ID_AC3:
759 case CODEC_ID_DTS:
760 audio_profile = PROFILE_AUDIO_AC3;
761 break;
762 case CODEC_ID_WMAV1:
763 case CODEC_ID_WMAV2:
764 /* WMA Baseline: stereo, up to 48 KHz, up to 192,999 bps */
765 if ( ac->bit_rate <= 193000 )
766 audio_profile = PROFILE_AUDIO_WMA_BASE;
767 /* WMA Full: stereo, up to 48 KHz, up to 385 Kbps */
768 else if ( ac->bit_rate <= 385000 )
769 audio_profile = PROFILE_AUDIO_WMA_FULL;
770 break;
771 #if LIBAVCODEC_VERSION_INT > ((51<<16)+(50<<8)+1)
772 case CODEC_ID_WMAPRO:
773 audio_profile = PROFILE_AUDIO_WMA_PRO;
774 break;
775 #endif
776 case CODEC_ID_MP2:
777 audio_profile = PROFILE_AUDIO_MP2;
778 break;
779 case CODEC_ID_AMR_NB:
780 audio_profile = PROFILE_AUDIO_AMR;
781 break;
782 default:
783 if( (ac->codec_id >= CODEC_ID_PCM_S16LE) &&
784 (ac->codec_id < CODEC_ID_ADPCM_IMA_QT) )
785 audio_profile = PROFILE_AUDIO_PCM;
786 else
787 DPRINTF(E_DEBUG, L_METADATA, "Unhandled audio codec [0x%X]\n", ac->codec_id);
788 break;
790 asprintf(&m.frequency, "%u", ac->sample_rate);
791 #if LIBAVCODEC_VERSION_INT < (52<<16)
792 asprintf(&m.bps, "%u", ac->bits_per_sample);
793 #else
794 asprintf(&m.bps, "%u", ac->bits_per_coded_sample);
795 #endif
796 asprintf(&m.channels, "%u", ac->channels);
798 if( vc )
800 int off;
801 int duration, hours, min, sec, ms;
802 ts_timestamp_t ts_timestamp = NONE;
803 DPRINTF(E_DEBUG, L_METADATA, "Container: '%s' [%s]\n", ctx->iformat->name, basename(path));
804 asprintf(&m.resolution, "%dx%d", vc->width, vc->height);
805 if( ctx->bit_rate > 8 )
806 asprintf(&m.bitrate, "%u", ctx->bit_rate / 8);
807 if( ctx->duration > 0 ) {
808 duration = (int)(ctx->duration / AV_TIME_BASE);
809 hours = (int)(duration / 3600);
810 min = (int)(duration / 60 % 60);
811 sec = (int)(duration % 60);
812 ms = (int)(ctx->duration / (AV_TIME_BASE/1000) % 1000);
813 asprintf(&m.duration, "%d:%02d:%02d.%03d", hours, min, sec, ms);
816 /* NOTE: The DLNA spec only provides for ASF (WMV), TS, PS, and MP4 containers.
817 * Skip DLNA parsing for everything else. */
818 if( strcmp(ctx->iformat->name, "avi") == 0 )
820 asprintf(&m.mime, "video/x-msvideo");
821 if( vc->codec_id == CODEC_ID_MPEG4 )
823 fourcc[0] = vc->codec_tag & 0xff;
824 fourcc[1] = vc->codec_tag>>8 & 0xff;
825 fourcc[2] = vc->codec_tag>>16 & 0xff;
826 fourcc[3] = vc->codec_tag>>24 & 0xff;
827 if( memcmp(fourcc, "XVID", 4) == 0 ||
828 memcmp(fourcc, "DX50", 4) == 0 ||
829 memcmp(fourcc, "DIVX", 4) == 0 )
830 asprintf(&m.creator, "DiVX");
833 else if( strcmp(ctx->iformat->name, "mov,mp4,m4a,3gp,3g2,mj2") == 0 &&
834 ends_with(path, ".mov") )
835 asprintf(&m.mime, "video/quicktime");
836 else if( strncmp(ctx->iformat->name, "matroska", 8) == 0 )
837 asprintf(&m.mime, "video/x-matroska");
838 else if( strcmp(ctx->iformat->name, "flv") == 0 )
839 asprintf(&m.mime, "video/x-flv");
840 if( m.mime )
841 goto video_no_dlna;
843 switch( vc->codec_id )
845 case CODEC_ID_MPEG1VIDEO:
846 if( strcmp(ctx->iformat->name, "mpeg") == 0 )
848 if( (vc->width == 352) &&
849 (vc->height <= 288) )
851 asprintf(&m.dlna_pn, "MPEG1;%s", dlna_no_conv);
853 asprintf(&m.mime, "video/mpeg");
855 break;
856 case CODEC_ID_MPEG2VIDEO:
857 m.dlna_pn = malloc(64);
858 off = sprintf(m.dlna_pn, "MPEG_");
859 if( strcmp(ctx->iformat->name, "mpegts") == 0 )
861 DPRINTF(E_DEBUG, L_METADATA, "Stream %d of %s is %s MPEG2 TS\n",
862 video_stream, basename(path), m.resolution);
863 off += sprintf(m.dlna_pn+off, "TS_");
864 if( (vc->width >= 1280) &&
865 (vc->height >= 720) )
867 off += sprintf(m.dlna_pn+off, "HD_NA");
869 else
871 off += sprintf(m.dlna_pn+off, "SD_");
872 if( (vc->height == 576) ||
873 (vc->height == 288) )
874 off += sprintf(m.dlna_pn+off, "EU");
875 else
876 off += sprintf(m.dlna_pn+off, "NA");
878 ts = ctx->priv_data;
879 if( ts->packet_size == MPEG_TS_PACKET_LENGTH_DLNA )
881 if( dlna_timestamp_is_present(path) )
882 ts_timestamp = VALID;
883 else
884 ts_timestamp = EMPTY;
886 else if( ts->packet_size != MPEG_TS_PACKET_LENGTH )
888 DPRINTF(E_DEBUG, L_METADATA, "Unsupported DLNA TS packet size [%d] (%s)\n",
889 ts->packet_size, basename(path));
890 free(m.dlna_pn);
891 m.dlna_pn = NULL;
893 switch( ts_timestamp )
895 case NONE:
896 asprintf(&m.mime, "video/mpeg");
897 if( m.dlna_pn )
898 off += sprintf(m.dlna_pn+off, "_ISO");
899 break;
900 case VALID:
901 off += sprintf(m.dlna_pn+off, "_T");
902 case EMPTY:
903 asprintf(&m.mime, "video/vnd.dlna.mpeg-tts");
904 default:
905 break;
908 else if( strcmp(ctx->iformat->name, "mpeg") == 0 )
910 DPRINTF(E_DEBUG, L_METADATA, "Stream %d of %s is %s MPEG2 PS\n",
911 video_stream, basename(path), m.resolution);
912 off += sprintf(m.dlna_pn+off, "PS_");
913 if( (vc->height == 576) ||
914 (vc->height == 288) )
915 off += sprintf(m.dlna_pn+off, "PAL");
916 else
917 off += sprintf(m.dlna_pn+off, "NTSC");
918 asprintf(&m.mime, "video/mpeg");
920 else
922 DPRINTF(E_DEBUG, L_METADATA, "Stream %d of %s [%s] is %s non-DLNA MPEG2\n",
923 video_stream, basename(path), ctx->iformat->name, m.resolution);
924 free(m.dlna_pn);
925 m.dlna_pn = NULL;
927 if( m.dlna_pn )
928 sprintf(m.dlna_pn+off, ";%s", dlna_no_conv);
929 break;
930 case CODEC_ID_H264:
931 m.dlna_pn = malloc(128);
932 off = sprintf(m.dlna_pn, "AVC_");
934 if( strcmp(ctx->iformat->name, "mpegts") == 0 )
936 off += sprintf(m.dlna_pn+off, "TS_");
937 switch( vc->profile )
939 case FF_PROFILE_H264_BASELINE:
940 off += sprintf(m.dlna_pn+off, "BL_");
941 if( vc->width <= 352 &&
942 vc->height <= 288 &&
943 vc->bit_rate <= 384000 )
945 off += sprintf(m.dlna_pn+off, "CIF15_");
946 break;
948 else if( vc->width <= 352 &&
949 vc->height <= 288 &&
950 vc->bit_rate <= 3000000 )
952 off += sprintf(m.dlna_pn+off, "CIF30_");
953 break;
955 /* Fall back to Main Profile if it doesn't match a Baseline DLNA profile. */
956 else
957 off -= 3;
958 default:
959 case FF_PROFILE_H264_MAIN:
960 off += sprintf(m.dlna_pn+off, "MP_");
961 if( vc->profile != FF_PROFILE_H264_BASELINE &&
962 vc->profile != FF_PROFILE_H264_MAIN )
964 DPRINTF(E_DEBUG, L_METADATA, "Unknown AVC profile %d; assuming MP. [%s]\n",
965 vc->profile, basename(path));
967 if( vc->width <= 720 &&
968 vc->height <= 576 &&
969 vc->bit_rate <= 10000000 )
971 off += sprintf(m.dlna_pn+off, "SD_");
973 else if( vc->width <= 1920 &&
974 vc->height <= 1152 &&
975 vc->bit_rate <= 20000000 )
977 off += sprintf(m.dlna_pn+off, "HD_");
979 else
981 DPRINTF(E_DEBUG, L_METADATA, "Unsupported h.264 video profile! [%s, %dx%d, %dbps : %s]\n",
982 m.dlna_pn, vc->width, vc->height, vc->bit_rate, basename(path));
983 free(m.dlna_pn);
984 m.dlna_pn = NULL;
986 break;
987 case FF_PROFILE_H264_HIGH:
988 off += sprintf(m.dlna_pn+off, "HP_");
989 if( vc->width <= 1920 &&
990 vc->height <= 1152 &&
991 vc->bit_rate <= 30000000 &&
992 audio_profile == PROFILE_AUDIO_AC3 )
994 off += sprintf(m.dlna_pn+off, "HD_");
996 else
998 DPRINTF(E_DEBUG, L_METADATA, "Unsupported h.264 HP video profile! [%dbps, %d audio : %s]\n",
999 vc->bit_rate, audio_profile, basename(path));
1000 free(m.dlna_pn);
1001 m.dlna_pn = NULL;
1003 break;
1005 if( !m.dlna_pn )
1006 break;
1007 switch( audio_profile )
1009 case PROFILE_AUDIO_MP3:
1010 off += sprintf(m.dlna_pn+off, "MPEG1_L3");
1011 break;
1012 case PROFILE_AUDIO_AC3:
1013 off += sprintf(m.dlna_pn+off, "AC3");
1014 break;
1015 case PROFILE_AUDIO_AAC:
1016 case PROFILE_AUDIO_AAC_MULT5:
1017 off += sprintf(m.dlna_pn+off, "AAC_MULT5");
1018 break;
1019 default:
1020 DPRINTF(E_DEBUG, L_METADATA, "No DLNA profile found for %s file [%s]\n",
1021 m.dlna_pn, basename(path));
1022 free(m.dlna_pn);
1023 m.dlna_pn = NULL;
1024 break;
1026 if( !m.dlna_pn )
1027 break;
1028 ts = ctx->priv_data;
1029 if( ts->packet_size == MPEG_TS_PACKET_LENGTH_DLNA )
1031 if( vc->profile == FF_PROFILE_H264_HIGH ||
1032 dlna_timestamp_is_present(path) )
1033 ts_timestamp = VALID;
1034 else
1035 ts_timestamp = EMPTY;
1037 else if( ts->packet_size != MPEG_TS_PACKET_LENGTH )
1039 DPRINTF(E_DEBUG, L_METADATA, "Unsupported DLNA TS packet size [%d] (%s)\n",
1040 ts->packet_size, basename(path));
1041 free(m.dlna_pn);
1042 m.dlna_pn = NULL;
1044 switch( ts_timestamp )
1046 case NONE:
1047 if( m.dlna_pn )
1048 off += sprintf(m.dlna_pn+off, "_ISO");
1049 break;
1050 case VALID:
1051 off += sprintf(m.dlna_pn+off, "_T");
1052 case EMPTY:
1053 asprintf(&m.mime, "video/vnd.dlna.mpeg-tts");
1054 default:
1055 break;
1058 else if( strcmp(ctx->iformat->name, "mov,mp4,m4a,3gp,3g2,mj2") == 0 )
1060 off += sprintf(m.dlna_pn+off, "MP4_");
1062 switch( vc->profile ) {
1063 case FF_PROFILE_H264_BASELINE:
1064 if( vc->width <= 352 &&
1065 vc->height <= 288 )
1067 if( ctx->bit_rate < 600000 )
1068 off += sprintf(m.dlna_pn+off, "BL_CIF15_");
1069 else if( ctx->bit_rate < 5000000 )
1070 off += sprintf(m.dlna_pn+off, "BL_CIF30_");
1071 else
1072 goto mp4_mp_fallback;
1074 if( audio_profile == PROFILE_AUDIO_AMR )
1076 off += sprintf(m.dlna_pn+off, "AMR");
1078 else if( audio_profile == PROFILE_AUDIO_AAC )
1080 off += sprintf(m.dlna_pn+off, "AAC_");
1081 if( ctx->bit_rate < 520000 )
1083 off += sprintf(m.dlna_pn+off, "520");
1085 else if( ctx->bit_rate < 940000 )
1087 off += sprintf(m.dlna_pn+off, "940");
1089 else
1091 off -= 13;
1092 goto mp4_mp_fallback;
1095 else
1097 off -= 9;
1098 goto mp4_mp_fallback;
1101 else if( vc->width <= 720 &&
1102 vc->height <= 576 )
1104 if( vc->level == 30 &&
1105 audio_profile == PROFILE_AUDIO_AAC &&
1106 ctx->bit_rate <= 5000000 )
1107 off += sprintf(m.dlna_pn+off, "BL_L3L_SD_AAC");
1108 else if( vc->level <= 31 &&
1109 audio_profile == PROFILE_AUDIO_AAC &&
1110 ctx->bit_rate <= 15000000 )
1111 off += sprintf(m.dlna_pn+off, "BL_L31_HD_AAC");
1112 else
1113 goto mp4_mp_fallback;
1115 else if( vc->width <= 1280 &&
1116 vc->height <= 720 )
1118 if( vc->level <= 31 &&
1119 audio_profile == PROFILE_AUDIO_AAC &&
1120 ctx->bit_rate <= 15000000 )
1121 off += sprintf(m.dlna_pn+off, "BL_L31_HD_AAC");
1122 else if( vc->level <= 32 &&
1123 audio_profile == PROFILE_AUDIO_AAC &&
1124 ctx->bit_rate <= 21000000 )
1125 off += sprintf(m.dlna_pn+off, "BL_L32_HD_AAC");
1126 else
1127 goto mp4_mp_fallback;
1129 else
1130 goto mp4_mp_fallback;
1131 break;
1132 case FF_PROFILE_H264_MAIN:
1133 mp4_mp_fallback:
1134 off += sprintf(m.dlna_pn+off, "MP_");
1135 /* AVC MP4 SD profiles - 10 Mbps max */
1136 if( vc->width <= 720 &&
1137 vc->height <= 576 &&
1138 vc->bit_rate <= 10000000 )
1140 sprintf(m.dlna_pn+off, "SD_");
1141 if( audio_profile == PROFILE_AUDIO_AC3 )
1142 off += sprintf(m.dlna_pn+off, "AC3");
1143 else if( audio_profile == PROFILE_AUDIO_AAC ||
1144 audio_profile == PROFILE_AUDIO_AAC_MULT5 )
1145 off += sprintf(m.dlna_pn+off, "AAC_MULT5");
1146 else if( audio_profile == PROFILE_AUDIO_MP3 )
1147 off += sprintf(m.dlna_pn+off, "MPEG1_L3");
1148 else
1149 m.dlna_pn[10] = '\0';
1151 else if( vc->width <= 1280 &&
1152 vc->height <= 720 &&
1153 vc->bit_rate <= 15000000 &&
1154 audio_profile == PROFILE_AUDIO_AAC )
1156 off += sprintf(m.dlna_pn+off, "HD_720p_AAC");
1158 else if( vc->width <= 1920 &&
1159 vc->height <= 1080 &&
1160 vc->bit_rate <= 21000000 &&
1161 audio_profile == PROFILE_AUDIO_AAC )
1163 off += sprintf(m.dlna_pn+off, "HD_1080i_AAC");
1165 if( strlen(m.dlna_pn) <= 11 )
1167 DPRINTF(E_DEBUG, L_METADATA, "No DLNA profile found for %s file %s\n",
1168 m.dlna_pn, basename(path));
1169 free(m.dlna_pn);
1170 m.dlna_pn = NULL;
1172 break;
1173 case FF_PROFILE_H264_HIGH:
1174 if( vc->width <= 1920 &&
1175 vc->height <= 1080 &&
1176 vc->bit_rate <= 25000000 &&
1177 audio_profile == PROFILE_AUDIO_AAC )
1179 off += sprintf(m.dlna_pn+off, "HP_HD_AAC");
1181 break;
1182 default:
1183 DPRINTF(E_DEBUG, L_METADATA, "AVC profile [%d] not recognized for file %s\n",
1184 vc->profile, basename(path));
1185 free(m.dlna_pn);
1186 m.dlna_pn = NULL;
1187 break;
1190 else
1192 free(m.dlna_pn);
1193 m.dlna_pn = NULL;
1195 if( m.dlna_pn )
1196 sprintf(m.dlna_pn+off, ";%s", dlna_no_conv);
1197 DPRINTF(E_DEBUG, L_METADATA, "Stream %d of %s is h.264\n", video_stream, basename(path));
1198 break;
1199 case CODEC_ID_MPEG4:
1200 fourcc[0] = vc->codec_tag & 0xff;
1201 fourcc[1] = vc->codec_tag>>8 & 0xff;
1202 fourcc[2] = vc->codec_tag>>16 & 0xff;
1203 fourcc[3] = vc->codec_tag>>24 & 0xff;
1204 DPRINTF(E_DEBUG, L_METADATA, "Stream %d of %s is MPEG4 [%c%c%c%c/0x%X]\n",
1205 video_stream, basename(path),
1206 isprint(fourcc[0]) ? fourcc[0] : '_',
1207 isprint(fourcc[1]) ? fourcc[1] : '_',
1208 isprint(fourcc[2]) ? fourcc[2] : '_',
1209 isprint(fourcc[3]) ? fourcc[3] : '_',
1210 vc->codec_tag);
1212 if( strcmp(ctx->iformat->name, "mov,mp4,m4a,3gp,3g2,mj2") == 0 )
1214 m.dlna_pn = malloc(128);
1215 off = sprintf(m.dlna_pn, "MPEG4_P2_");
1217 if( ends_with(path, ".3gp") )
1219 asprintf(&m.mime, "video/3gpp");
1220 switch( audio_profile )
1222 case PROFILE_AUDIO_AAC:
1223 off += sprintf(m.dlna_pn+off, "3GPP_SP_L0B_AAC");
1224 break;
1225 case PROFILE_AUDIO_AMR:
1226 off += sprintf(m.dlna_pn+off, "3GPP_SP_L0B_AMR");
1227 break;
1228 default:
1229 DPRINTF(E_DEBUG, L_METADATA, "No DLNA profile found for MPEG4-P2 3GP/0x%X file %s\n",
1230 ac->codec_id, basename(path));
1231 free(m.dlna_pn);
1232 m.dlna_pn = NULL;
1233 break;
1236 else
1238 if( ctx->bit_rate <= 1000000 &&
1239 audio_profile == PROFILE_AUDIO_AAC )
1241 off += sprintf(m.dlna_pn+off, "MP4_ASP_AAC");
1243 else if( ctx->bit_rate <= 4000000 &&
1244 vc->width <= 640 &&
1245 vc->height <= 480 &&
1246 audio_profile == PROFILE_AUDIO_AAC )
1248 off += sprintf(m.dlna_pn+off, "MP4_SP_VGA_AAC");
1250 else
1252 DPRINTF(E_DEBUG, L_METADATA, "Unsupported h.264 video profile! [%dx%d, %dbps]\n",
1253 vc->width,
1254 vc->height,
1255 ctx->bit_rate);
1256 free(m.dlna_pn);
1257 m.dlna_pn = NULL;
1260 if( m.dlna_pn )
1261 sprintf(m.dlna_pn+off, ";%s", dlna_no_conv);
1263 break;
1264 case CODEC_ID_WMV3:
1265 /* I'm not 100% sure this is correct, but it works on everything I could get my hands on */
1266 if( vc->extradata_size > 0 )
1268 if( !((vc->extradata[0] >> 3) & 1) )
1269 vc->level = 0;
1270 if( !((vc->extradata[0] >> 6) & 1) )
1271 vc->profile = 0;
1273 case CODEC_ID_VC1:
1274 if( strcmp(ctx->iformat->name, "asf") != 0 )
1276 DPRINTF(E_DEBUG, L_METADATA, "Skipping DLNA parsing for non-ASF VC1 file %s\n", path);
1277 break;
1279 m.dlna_pn = malloc(64);
1280 off = sprintf(m.dlna_pn, "WMV");
1281 DPRINTF(E_DEBUG, L_METADATA, "Stream %d of %s is VC1\n", video_stream, basename(path));
1282 asprintf(&m.mime, "video/x-ms-wmv");
1283 if( (vc->width <= 176) &&
1284 (vc->height <= 144) &&
1285 (vc->level == 0) )
1287 off += sprintf(m.dlna_pn+off, "SPLL_");
1288 switch( audio_profile )
1290 case PROFILE_AUDIO_MP3:
1291 off += sprintf(m.dlna_pn+off, "MP3");
1292 break;
1293 case PROFILE_AUDIO_WMA_BASE:
1294 off += sprintf(m.dlna_pn+off, "BASE");
1295 break;
1296 default:
1297 DPRINTF(E_DEBUG, L_METADATA, "No DLNA profile found for WMVSPLL/0x%X file %s\n",
1298 audio_profile, basename(path));
1299 free(m.dlna_pn);
1300 m.dlna_pn = NULL;
1301 break;
1304 else if( (vc->width <= 352) &&
1305 (vc->height <= 288) &&
1306 (vc->profile == 0) &&
1307 (ctx->bit_rate/8 <= 384000) )
1309 off += sprintf(m.dlna_pn+off, "SPML_");
1310 switch( audio_profile )
1312 case PROFILE_AUDIO_MP3:
1313 off += sprintf(m.dlna_pn+off, "MP3");
1314 break;
1315 case PROFILE_AUDIO_WMA_BASE:
1316 off += sprintf(m.dlna_pn+off, "BASE");
1317 break;
1318 default:
1319 DPRINTF(E_DEBUG, L_METADATA, "No DLNA profile found for WMVSPML/0x%X file %s\n",
1320 audio_profile, basename(path));
1321 free(m.dlna_pn);
1322 m.dlna_pn = NULL;
1323 break;
1326 else if( (vc->width <= 720) &&
1327 (vc->height <= 576) &&
1328 (ctx->bit_rate/8 <= 10000000) )
1330 off += sprintf(m.dlna_pn+off, "MED_");
1331 switch( audio_profile )
1333 case PROFILE_AUDIO_WMA_PRO:
1334 off += sprintf(m.dlna_pn+off, "PRO");
1335 break;
1336 case PROFILE_AUDIO_WMA_FULL:
1337 off += sprintf(m.dlna_pn+off, "FULL");
1338 break;
1339 case PROFILE_AUDIO_WMA_BASE:
1340 off += sprintf(m.dlna_pn+off, "BASE");
1341 break;
1342 default:
1343 DPRINTF(E_DEBUG, L_METADATA, "No DLNA profile found for WMVMED/0x%X file %s\n",
1344 audio_profile, basename(path));
1345 free(m.dlna_pn);
1346 m.dlna_pn = NULL;
1347 break;
1350 else if( (vc->width <= 1920) &&
1351 (vc->height <= 1080) &&
1352 (ctx->bit_rate/8 <= 20000000) )
1354 off += sprintf(m.dlna_pn+off, "HIGH_");
1355 switch( audio_profile )
1357 case PROFILE_AUDIO_WMA_PRO:
1358 off += sprintf(m.dlna_pn+off, "PRO");
1359 break;
1360 case PROFILE_AUDIO_WMA_FULL:
1361 off += sprintf(m.dlna_pn+off, "FULL");
1362 break;
1363 default:
1364 DPRINTF(E_DEBUG, L_METADATA, "No DLNA profile found for WMVHIGH/0x%X file %s\n",
1365 audio_profile, basename(path));
1366 free(m.dlna_pn);
1367 m.dlna_pn = NULL;
1368 break;
1371 if( m.dlna_pn )
1372 sprintf(m.dlna_pn+off, ";%s", dlna_no_conv);
1373 break;
1374 case CODEC_ID_MSMPEG4V3:
1375 asprintf(&m.mime, "video/x-msvideo");
1376 default:
1377 DPRINTF(E_DEBUG, L_METADATA, "Stream %d of %s is %s [type %d]\n",
1378 video_stream, basename(path), m.resolution, vc->codec_id);
1379 break;
1382 if( !m.mime )
1384 if( strcmp(ctx->iformat->name, "avi") == 0 )
1385 asprintf(&m.mime, "video/x-msvideo");
1386 else if( strncmp(ctx->iformat->name, "mpeg", 4) == 0 )
1387 asprintf(&m.mime, "video/mpeg");
1388 else if( strcmp(ctx->iformat->name, "asf") == 0 )
1389 asprintf(&m.mime, "video/x-ms-wmv");
1390 else if( strcmp(ctx->iformat->name, "mov,mp4,m4a,3gp,3g2,mj2") == 0 )
1391 if( ends_with(path, ".mov") )
1392 asprintf(&m.mime, "video/quicktime");
1393 else
1394 asprintf(&m.mime, "video/mp4");
1395 else if( strncmp(ctx->iformat->name, "matroska", 8) == 0 )
1396 asprintf(&m.mime, "video/x-matroska");
1397 else if( strcmp(ctx->iformat->name, "flv") == 0 )
1398 asprintf(&m.mime, "video/x-flv");
1399 else
1400 DPRINTF(E_WARN, L_METADATA, "%s: Unhandled format: %s\n", path, ctx->iformat->name);
1403 if( strcmp(ctx->iformat->name, "asf") == 0 )
1405 if( readtags((char *)path, &video, &file, "en_US", "asf") == 0 )
1407 if( video.title && *video.title )
1409 m.title = escape_tag(trim(video.title), 1);
1411 if( video.genre && *video.genre )
1413 m.genre = escape_tag(trim(video.genre), 1);
1415 if( video.contributor[ROLE_TRACKARTIST] && *video.contributor[ROLE_TRACKARTIST] )
1417 m.artist = escape_tag(trim(video.contributor[ROLE_TRACKARTIST]), 1);
1419 if( video.contributor[ROLE_ALBUMARTIST] && *video.contributor[ROLE_ALBUMARTIST] )
1421 m.creator = escape_tag(trim(video.contributor[ROLE_ALBUMARTIST]), 1);
1423 else
1425 m.creator = m.artist;
1426 free_flags &= ~FLAG_CREATOR;
1430 #ifndef NETGEAR
1431 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(31<<8)+0)
1432 else if( strcmp(ctx->iformat->name, "mov,mp4,m4a,3gp,3g2,mj2") == 0 )
1434 if( ctx->metadata )
1436 AVMetadataTag *tag = NULL;
1438 //DEBUG DPRINTF(E_DEBUG, L_METADATA, "Metadata:\n");
1439 while( (tag = av_metadata_get(ctx->metadata, "", tag, AV_METADATA_IGNORE_SUFFIX)) )
1441 //DEBUG DPRINTF(E_DEBUG, L_METADATA, " %-16s: %s\n", tag->key, tag->value);
1442 if( strcmp(tag->key, "title") == 0 )
1443 m.title = escape_tag(trim(tag->value), 1);
1444 else if( strcmp(tag->key, "genre") == 0 )
1445 m.genre = escape_tag(trim(tag->value), 1);
1446 else if( strcmp(tag->key, "artist") == 0 )
1447 m.artist = escape_tag(trim(tag->value), 1);
1448 else if( strcmp(tag->key, "comment") == 0 )
1449 m.comment = escape_tag(trim(tag->value), 1);
1453 #endif
1454 #endif
1455 video_no_dlna:
1456 av_close_input_file(ctx);
1458 #ifdef TIVO_SUPPORT
1459 if( ends_with(path, ".TiVo") && is_tivo_file(path) )
1461 if( m.dlna_pn )
1463 free(m.dlna_pn);
1464 m.dlna_pn = NULL;
1466 m.mime = realloc(m.mime, 18);
1467 strcpy(m.mime, "video/x-tivo-mpeg");
1469 #endif
1470 if( !m.title )
1471 m.title = strdup(name);
1473 album_art = find_album_art(path, video.image, video.image_size);
1474 freetags(&video);
1476 ret = sql_exec(db, "INSERT into DETAILS"
1477 " (PATH, SIZE, TIMESTAMP, DURATION, DATE, CHANNELS, BITRATE, SAMPLERATE, RESOLUTION,"
1478 " TITLE, CREATOR, ARTIST, GENRE, COMMENT, DLNA_PN, MIME, ALBUM_ART) "
1479 "VALUES"
1480 " (%Q, %lld, %ld, %Q, %Q, %Q, %Q, %Q, %Q, '%q', %Q, %Q, %Q, %Q, %Q, '%q', %lld);",
1481 path, file.st_size, file.st_mtime, m.duration,
1482 m.date, m.channels, m.bitrate, m.frequency, m.resolution,
1483 m.title, m.creator, m.artist, m.genre, m.comment, m.dlna_pn,
1484 m.mime, album_art);
1485 if( ret != SQLITE_OK )
1487 fprintf(stderr, "Error inserting details for '%s'!\n", path);
1488 ret = 0;
1490 else
1492 ret = sqlite3_last_insert_rowid(db);
1493 check_for_captions(path, ret);
1495 free_metadata(&m, free_flags);
1497 return ret;