8 #include "stream/stream.h"
13 #include "libavutil/intreadwrite.h"
24 typedef struct da_priv
{
29 //! rather arbitrary value for maximum length of wav-format headers
30 #define MAX_WAVHDR_LEN (1 * 1024 * 1024)
32 //! how many valid frames in a row we need before accepting as valid MP3
33 #define MIN_MP3_HDRS 12
35 //! Used to describe a potential (chain of) MP3 headers we found
36 typedef struct mp3_hdr
{
37 off_t frame_pos
; // start of first frame in this "chain" of headers
38 off_t next_frame_pos
; // here we expect the next header with same parameters
44 int cons_hdrs
; // if this reaches MIN_MP3_HDRS we accept as MP3 file
48 extern void print_wave_header(WAVEFORMATEX
*h
, int verbose_level
);
53 * \brief free a list of MP3 header descriptions
54 * \param list pointer to the head-of-list pointer
56 static void free_mp3_hdrs(mp3_hdr_t
**list
) {
66 * \brief add another potential MP3 header to our list
67 * If it fits into an existing chain this one is expanded otherwise
68 * a new one is created.
69 * All entries that expected a MP3 header before the current position
71 * The list is expected to be and will be kept sorted by next_frame_pos
72 * and when those are equal by frame_pos.
73 * \param list pointer to the head-of-list pointer
74 * \param st_pos stream position where the described header starts
75 * \param mp3_chans number of channels as specified by the header (*)
76 * \param mp3_freq sampling frequency as specified by the header (*)
77 * \param mpa_spf frame size as specified by the header
78 * \param mpa_layer layer type ("version") as specified by the header (*)
79 * \param mpa_br bitrate as specified by the header
80 * \param mp3_flen length of the frame as specified by the header
81 * \return If non-null the current file is accepted as MP3 and the
82 * mp3_hdr struct describing the valid chain is returned. Must be
83 * freed independent of the list.
85 * parameters marked by (*) must be the same for all headers in the same chain
87 static mp3_hdr_t
*add_mp3_hdr(mp3_hdr_t
**list
, off_t st_pos
,
88 int mp3_chans
, int mp3_freq
, int mpa_spf
,
89 int mpa_layer
, int mpa_br
, int mp3_flen
) {
92 while (*list
&& (*list
)->next_frame_pos
<= st_pos
) {
93 if (((*list
)->next_frame_pos
< st_pos
) || ((*list
)->mp3_chans
!= mp3_chans
)
94 || ((*list
)->mp3_freq
!= mp3_freq
) || ((*list
)->mpa_layer
!= mpa_layer
) ) {
100 (*list
)->cons_hdrs
++;
101 (*list
)->next_frame_pos
= st_pos
+ mp3_flen
;
102 (*list
)->mpa_spf
= mpa_spf
;
103 (*list
)->mpa_br
= mpa_br
;
104 if ((*list
)->cons_hdrs
>= MIN_MP3_HDRS
) {
105 // copy the valid entry, so that the list can be easily freed
106 tmp
= malloc(sizeof(mp3_hdr_t
));
107 memcpy(tmp
, *list
, sizeof(mp3_hdr_t
));
112 list
= &((*list
)->next
);
115 if (!in_list
) { // does not belong into an existing chain, insert
116 // find right position to insert to keep sorting
117 while (*list
&& (*list
)->next_frame_pos
<= st_pos
+ mp3_flen
)
118 list
= &((*list
)->next
);
119 tmp
= malloc(sizeof(mp3_hdr_t
));
120 tmp
->frame_pos
= st_pos
;
121 tmp
->next_frame_pos
= st_pos
+ mp3_flen
;
122 tmp
->mp3_chans
= mp3_chans
;
123 tmp
->mp3_freq
= mp3_freq
;
124 tmp
->mpa_spf
= mpa_spf
;
125 tmp
->mpa_layer
= mpa_layer
;
126 tmp
->mpa_br
= mpa_br
;
134 #if 0 /* this code is a mess, clean it up before reenabling */
135 #define FLAC_SIGNATURE_SIZE 4
136 #define FLAC_STREAMINFO_SIZE 34
137 #define FLAC_SEEKPOINT_SIZE 18
149 get_flac_metadata (demuxer_t
* demuxer
)
152 unsigned int blk_len
;
153 stream_t
*s
= demuxer
->stream
;
155 /* file is qualified; skip over the signature bytes in the stream */
158 /* loop through the metadata blocks; use a do-while construct since there
159 * will always be 1 metadata block */
163 r
= stream_read (s
, (char *) preamble
, FLAC_SIGNATURE_SIZE
);
164 if (r
!= FLAC_SIGNATURE_SIZE
)
167 blk_len
= AV_RB24(preamble
+ 1);
169 switch (preamble
[0] & 0x7F)
171 case FLAC_VORBIS_COMMENT
:
173 /* For a description of the format please have a look at */
174 /* http://www.xiph.org/vorbis/doc/v-comment.html */
176 uint32_t length
, comment_list_len
;
177 char comments
[blk_len
];
178 uint8_t *ptr
= comments
;
183 if (stream_read (s
, comments
, blk_len
) == blk_len
)
185 length
= AV_RL32(ptr
);
188 comment_list_len
= AV_RL32(ptr
);
192 for (; cn
< comment_list_len
; cn
++)
194 length
= AV_RL32(ptr
);
198 if (&comment
[length
] < comments
|| &comment
[length
] >= &comments
[blk_len
])
203 if (!strncasecmp ("TITLE=", comment
, 6) && (length
- 6 > 0))
204 demux_info_add (demuxer
, "Title", comment
+ 6);
205 else if (!strncasecmp ("ARTIST=", comment
, 7) && (length
- 7 > 0))
206 demux_info_add (demuxer
, "Artist", comment
+ 7);
207 else if (!strncasecmp ("ALBUM=", comment
, 6) && (length
- 6 > 0))
208 demux_info_add (demuxer
, "Album", comment
+ 6);
209 else if (!strncasecmp ("DATE=", comment
, 5) && (length
- 5 > 0))
210 demux_info_add (demuxer
, "Year", comment
+ 5);
211 else if (!strncasecmp ("GENRE=", comment
, 6) && (length
- 6 > 0))
212 demux_info_add (demuxer
, "Genre", comment
+ 6);
213 else if (!strncasecmp ("Comment=", comment
, 8) && (length
- 8 > 0))
214 demux_info_add (demuxer
, "Comment", comment
+ 8);
215 else if (!strncasecmp ("TRACKNUMBER=", comment
, 12)
216 && (length
- 12 > 0))
220 sprintf (buf
, "%d", atoi (comment
+ 12));
221 demux_info_add(demuxer
, "Track", buf
);
231 case FLAC_STREAMINFO
:
233 case FLAC_APPLICATION
:
237 /* 6-127 are presently reserved */
238 stream_skip (s
, blk_len
);
241 } while ((preamble
[0] & 0x80) == 0);
245 static int demux_audio_open(demuxer_t
* demuxer
) {
247 sh_audio_t
* sh_audio
;
248 uint8_t hdr
[HDR_SIZE
];
249 int frmt
= 0, n
= 0, step
;
250 off_t st_pos
= 0, next_frame_pos
= 0;
251 // mp3_hdrs list is sorted first by next_frame_pos and then by frame_pos
252 mp3_hdr_t
*mp3_hdrs
= NULL
, *mp3_found
= NULL
;
257 stream_read(s
, hdr
, HDR_SIZE
);
258 while(n
< 30000 && !s
->eof
) {
259 int mp3_freq
, mp3_chans
, mp3_flen
, mpa_layer
, mpa_spf
, mpa_br
;
260 st_pos
= stream_tell(s
) - HDR_SIZE
;
263 if( hdr
[0] == 'R' && hdr
[1] == 'I' && hdr
[2] == 'F' && hdr
[3] == 'F' ) {
267 stream_read(s
,hdr
,4);
270 if(hdr
[0] != 'W' || hdr
[1] != 'A' || hdr
[2] != 'V' || hdr
[3] != 'E' )
273 // We found wav header. Now we can have 'fmt ' or a mp3 header
276 } else if( hdr
[0] == 'I' && hdr
[1] == 'D' && hdr
[2] == '3' && (hdr
[3] >= 2)) {
279 stream_read(s
,hdr
,4);
280 len
= (hdr
[0]<<21) | (hdr
[1]<<14) | (hdr
[2]<<7) | hdr
[3];
283 } else if( hdr
[0] == 'f' && hdr
[1] == 'm' && hdr
[2] == 't' && hdr
[3] == ' ' ) {
286 } else if((mp3_flen
= mp_get_mp3_header(hdr
, &mp3_chans
, &mp3_freq
,
287 &mpa_spf
, &mpa_layer
, &mpa_br
)) > 0) {
288 mp3_found
= add_mp3_hdr(&mp3_hdrs
, st_pos
, mp3_chans
, mp3_freq
,
289 mpa_spf
, mpa_layer
, mpa_br
, mp3_flen
);
294 } else if( hdr
[0] == 'f' && hdr
[1] == 'L' && hdr
[2] == 'a' && hdr
[3] == 'C' ) {
298 // Add here some other audio format detection
300 memmove(hdr
,&hdr
[step
],HDR_SIZE
-step
);
301 stream_read(s
, &hdr
[HDR_SIZE
- step
], step
);
305 free_mp3_hdrs(&mp3_hdrs
);
310 sh_audio
= new_sh_audio(demuxer
,0);
314 sh_audio
->format
= (mp3_found
->mpa_layer
< 3 ? 0x50 : 0x55);
315 demuxer
->movi_start
= mp3_found
->frame_pos
;
316 next_frame_pos
= mp3_found
->next_frame_pos
;
317 sh_audio
->audio
.dwSampleSize
= 0;
318 sh_audio
->audio
.dwScale
= mp3_found
->mpa_spf
;
319 sh_audio
->audio
.dwRate
= mp3_found
->mp3_freq
;
320 sh_audio
->wf
= malloc(sizeof(WAVEFORMATEX
));
321 sh_audio
->wf
->wFormatTag
= sh_audio
->format
;
322 sh_audio
->wf
->nChannels
= mp3_found
->mp3_chans
;
323 sh_audio
->wf
->nSamplesPerSec
= mp3_found
->mp3_freq
;
324 sh_audio
->wf
->nAvgBytesPerSec
= mp3_found
->mpa_br
* (1000 / 8);
325 sh_audio
->wf
->nBlockAlign
= mp3_found
->mpa_spf
;
326 sh_audio
->wf
->wBitsPerSample
= 16;
327 sh_audio
->wf
->cbSize
= 0;
328 sh_audio
->i_bps
= sh_audio
->wf
->nAvgBytesPerSec
;
333 stream_seek(s
,s
->end_pos
-128);
334 stream_read(s
,tag
,3);
336 if(strcmp(tag
,"TAG"))
337 demuxer
->movi_end
= s
->end_pos
;
341 demuxer
->movi_end
= stream_tell(s
)-3;
342 stream_read(s
,buf
,30);
344 demux_info_add(demuxer
,"Title",buf
);
345 stream_read(s
,buf
,30);
347 demux_info_add(demuxer
,"Artist",buf
);
348 stream_read(s
,buf
,30);
350 demux_info_add(demuxer
,"Album",buf
);
351 stream_read(s
,buf
,4);
353 demux_info_add(demuxer
,"Year",buf
);
354 stream_read(s
,buf
,30);
356 demux_info_add(demuxer
,"Comment",buf
);
357 if(buf
[28] == 0 && buf
[29] != 0) {
358 uint8_t trk
= (uint8_t)buf
[29];
359 sprintf(buf
,"%d",trk
);
360 demux_info_add(demuxer
,"Track",buf
);
362 g
= stream_read_char(s
);
363 demux_info_add(demuxer
,"Genre",genres
[g
]);
368 unsigned int chunk_type
;
369 unsigned int chunk_size
;
372 l
= stream_read_dword_le(s
);
374 mp_msg(MSGT_DEMUX
,MSGL_ERR
,"[demux_audio] Bad wav header length: too short (%d)!!!\n",l
);
377 if(l
> MAX_WAVHDR_LEN
) {
378 mp_msg(MSGT_DEMUX
,MSGL_ERR
,"[demux_audio] Bad wav header length: too long (%d)!!!\n",l
);
381 sh_audio
->wf
= w
= malloc(l
> sizeof(WAVEFORMATEX
) ? l
: sizeof(WAVEFORMATEX
));
382 w
->wFormatTag
= sh_audio
->format
= stream_read_word_le(s
);
383 w
->nChannels
= sh_audio
->channels
= stream_read_word_le(s
);
384 w
->nSamplesPerSec
= sh_audio
->samplerate
= stream_read_dword_le(s
);
385 w
->nAvgBytesPerSec
= stream_read_dword_le(s
);
386 w
->nBlockAlign
= stream_read_word_le(s
);
387 w
->wBitsPerSample
= stream_read_word_le(s
);
388 sh_audio
->samplesize
= (w
->wBitsPerSample
+ 7) / 8;
390 sh_audio
->i_bps
= sh_audio
->wf
->nAvgBytesPerSec
;
393 w
->cbSize
= stream_read_word_le(s
);
397 mp_msg(MSGT_DEMUX
,MSGL_ERR
,"[demux_audio] truncated extradata (%d < %d)\n",
399 stream_read(s
,(char*)((char*)(w
)+sizeof(WAVEFORMATEX
)),l
);
402 stream_read(s
,(char*)((char*)(w
)+sizeof(WAVEFORMATEX
)),w
->cbSize
);
408 if( mp_msg_test(MSGT_DEMUX
,MSGL_V
) ) print_wave_header(w
, MSGL_V
);
413 chunk_type
= stream_read_fourcc(demuxer
->stream
);
414 chunk_size
= stream_read_dword_le(demuxer
->stream
);
415 if (chunk_type
!= mmioFOURCC('d', 'a', 't', 'a'))
416 stream_skip(demuxer
->stream
, chunk_size
);
417 } while (!s
->eof
&& chunk_type
!= mmioFOURCC('d', 'a', 't', 'a'));
418 demuxer
->movi_start
= stream_tell(s
);
419 demuxer
->movi_end
= chunk_size
? demuxer
->movi_start
+ chunk_size
: s
->end_pos
;
420 // printf("wav: %X .. %X\n",(int)demuxer->movi_start,(int)demuxer->movi_end);
421 // Check if it contains dts audio
422 if((w
->wFormatTag
== 0x01) && (w
->nChannels
== 2) && (w
->nSamplesPerSec
== 44100)) {
423 unsigned char buf
[16384]; // vlc uses 16384*4 (4 dts frames)
425 stream_read(s
, buf
, sizeof(buf
));
426 for (i
= 0; i
< sizeof(buf
) - 5; i
+= 2) {
428 if((buf
[i
] == 0xff) && (buf
[i
+1] == 0x1f) && (buf
[i
+2] == 0x00) &&
429 (buf
[i
+3] == 0xe8) && ((buf
[i
+4] & 0xfe) == 0xf0) && (buf
[i
+5] == 0x07)) {
430 sh_audio
->format
= 0x2001;
431 mp_msg(MSGT_DEMUX
,MSGL_V
,"[demux_audio] DTS audio in wav, 14 bit, LE\n");
435 if((buf
[i
] == 0x1f) && (buf
[i
+1] == 0xff) && (buf
[i
+2] == 0xe8) &&
436 (buf
[i
+3] == 0x00) && (buf
[i
+4] == 0x07) && ((buf
[i
+5] & 0xfe) == 0xf0)) {
437 sh_audio
->format
= 0x2001;
438 mp_msg(MSGT_DEMUX
,MSGL_V
,"[demux_audio] DTS audio in wav, 14 bit, BE\n");
442 if((buf
[i
] == 0x7f) && (buf
[i
+1] == 0xfe) && (buf
[i
+2] == 0x80) &&
443 (buf
[i
+3] == 0x01)) {
444 sh_audio
->format
= 0x2001;
445 mp_msg(MSGT_DEMUX
,MSGL_V
,"[demux_audio] DTS audio in wav, 16 bit, BE\n");
449 if((buf
[i
] == 0xfe) && (buf
[i
+1] == 0x7f) && (buf
[i
+2] == 0x01) &&
450 (buf
[i
+3] == 0x80)) {
451 sh_audio
->format
= 0x2001;
452 mp_msg(MSGT_DEMUX
,MSGL_V
,"[demux_audio] DTS audio in wav, 16 bit, LE\n");
456 if (sh_audio
->format
== 0x2001)
457 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"[demux_audio] DTS sync offset = %u\n", i
);
460 stream_seek(s
,demuxer
->movi_start
);
463 sh_audio
->format
= mmioFOURCC('f', 'L', 'a', 'C');
464 demuxer
->movi_start
= stream_tell(s
) - 4;
465 demuxer
->movi_end
= s
->end_pos
;
466 if (demuxer
->movi_end
> demuxer
->movi_start
) {
467 // try to find out approx. bitrate
468 int64_t size
= demuxer
->movi_end
- demuxer
->movi_start
;
469 int64_t num_samples
= 0;
472 stream_read(s
, (char *)&srate
, 3);
473 srate
= be2me_32(srate
) >> 12;
474 stream_read(s
, (char *)&num_samples
, 5);
475 num_samples
= (be2me_64(num_samples
) >> 24) & 0xfffffffffULL
;
476 if (num_samples
&& srate
)
477 sh_audio
->i_bps
= size
* srate
/ num_samples
;
479 if (sh_audio
->i_bps
< 1) // guess value to prevent crash
480 sh_audio
->i_bps
= 64 * 1024;
481 // get_flac_metadata (demuxer);
485 priv
= malloc(sizeof(da_priv_t
));
488 demuxer
->priv
= priv
;
489 demuxer
->audio
->id
= 0;
490 demuxer
->audio
->sh
= sh_audio
;
491 sh_audio
->ds
= demuxer
->audio
;
492 sh_audio
->samplerate
= sh_audio
->audio
.dwRate
;
494 if(stream_tell(s
) != demuxer
->movi_start
)
496 mp_msg(MSGT_DEMUX
, MSGL_V
, "demux_audio: seeking from 0x%X to start pos 0x%X\n",
497 (int)stream_tell(s
), (int)demuxer
->movi_start
);
498 stream_seek(s
,demuxer
->movi_start
);
499 if (stream_tell(s
) != demuxer
->movi_start
) {
500 mp_msg(MSGT_DEMUX
, MSGL_V
, "demux_audio: seeking failed, now at 0x%X!\n",
501 (int)stream_tell(s
));
502 if (next_frame_pos
) {
503 mp_msg(MSGT_DEMUX
, MSGL_V
, "demux_audio: seeking to 0x%X instead\n",
504 (int)next_frame_pos
);
505 stream_seek(s
, next_frame_pos
);
510 mp_msg(MSGT_DEMUX
,MSGL_V
,"demux_audio: audio data 0x%X - 0x%X \n",(int)demuxer
->movi_start
,(int)demuxer
->movi_end
);
512 return DEMUXER_TYPE_AUDIO
;
516 static int demux_audio_fill_buffer(demuxer_t
*demuxer
, demux_stream_t
*ds
) {
519 sh_audio_t
* sh_audio
= ds
->sh
;
520 demuxer_t
* demux
= ds
->demuxer
;
521 da_priv_t
* priv
= demux
->priv
;
522 double this_pts
= priv
->next_pts
;
523 stream_t
* s
= demux
->stream
;
532 stream_read(s
,hdr
,4);
535 l
= mp_decode_mp3_header(hdr
);
537 if (demux
->movi_end
&& stream_tell(s
) >= demux
->movi_end
)
538 return 0; // might be ID3 tag, i.e. EOF
541 dp
= new_demux_packet(l
);
542 memcpy(dp
->buffer
,hdr
,4);
543 if (stream_read(s
,dp
->buffer
+ 4,l
-4) != l
-4)
545 free_demux_packet(dp
);
548 priv
->next_pts
+= sh_audio
->audio
.dwScale
/(double)sh_audio
->samplerate
;
553 unsigned align
= sh_audio
->wf
->nBlockAlign
;
554 l
= sh_audio
->wf
->nAvgBytesPerSec
;
555 if (l
<= 0) l
= 65536;
556 if (demux
->movi_end
&& l
> demux
->movi_end
- stream_tell(s
)) {
557 // do not read beyond end, there might be junk after data chunk
558 l
= demux
->movi_end
- stream_tell(s
);
559 if (l
<= 0) return 0;
562 l
= (l
+ align
- 1) / align
* align
;
563 dp
= new_demux_packet(l
);
564 l
= stream_read(s
,dp
->buffer
,l
);
565 priv
->next_pts
+= l
/(double)sh_audio
->i_bps
;
570 dp
= new_demux_packet(l
);
571 l
= stream_read(s
,dp
->buffer
,l
);
572 /* FLAC is not a constant-bitrate codec. These values will be wrong. */
573 priv
->next_pts
+= l
/(double)sh_audio
->i_bps
;
577 mp_msg(MSGT_DEMUXER
,MSGL_WARN
,MSGTR_MPDEMUX_AUDIO_UnknownFormat
,priv
->frmt
);
581 resize_demux_packet(dp
, l
);
583 ds_add_packet(ds
, dp
);
587 static void high_res_mp3_seek(demuxer_t
*demuxer
,float time
) {
590 da_priv_t
* priv
= demuxer
->priv
;
591 sh_audio_t
* sh
= (sh_audio_t
*)demuxer
->audio
->sh
;
593 nf
= time
*sh
->samplerate
/sh
->audio
.dwScale
;
595 stream_read(demuxer
->stream
,hdr
,4);
596 len
= mp_decode_mp3_header(hdr
);
598 stream_skip(demuxer
->stream
,-3);
601 stream_skip(demuxer
->stream
,len
-4);
602 priv
->next_pts
+= sh
->audio
.dwScale
/(double)sh
->samplerate
;
607 static void demux_audio_seek(demuxer_t
*demuxer
,float rel_seek_secs
,float audio_delay
,int flags
){
608 sh_audio_t
* sh_audio
;
614 if(!(sh_audio
= demuxer
->audio
->sh
))
617 priv
= demuxer
->priv
;
619 if(priv
->frmt
== MP3
&& hr_mp3_seek
&& !(flags
& SEEK_FACTOR
)) {
620 len
= (flags
& SEEK_ABSOLUTE
) ? rel_seek_secs
- priv
->next_pts
: rel_seek_secs
;
622 stream_seek(s
,demuxer
->movi_start
);
623 len
= priv
->next_pts
+ len
;
627 high_res_mp3_seek(demuxer
,len
);
631 base
= flags
&SEEK_ABSOLUTE
? demuxer
->movi_start
: stream_tell(s
);
632 if(flags
&SEEK_FACTOR
)
633 pos
= base
+ ((demuxer
->movi_end
- demuxer
->movi_start
)*rel_seek_secs
);
635 pos
= base
+ (rel_seek_secs
*sh_audio
->i_bps
);
637 if(demuxer
->movi_end
&& pos
>= demuxer
->movi_end
) {
638 pos
= demuxer
->movi_end
;
639 } else if(pos
< demuxer
->movi_start
)
640 pos
= demuxer
->movi_start
;
642 priv
->next_pts
= (pos
-demuxer
->movi_start
)/(double)sh_audio
->i_bps
;
646 pos
-= (pos
- demuxer
->movi_start
) %
647 (sh_audio
->wf
->nBlockAlign
? sh_audio
->wf
->nBlockAlign
:
648 (sh_audio
->channels
* sh_audio
->samplesize
));
655 static void demux_close_audio(demuxer_t
* demuxer
) {
656 da_priv_t
* priv
= demuxer
->priv
;
663 static int demux_audio_control(demuxer_t
*demuxer
,int cmd
, void *arg
){
664 sh_audio_t
*sh_audio
=demuxer
->audio
->sh
;
665 int audio_length
= sh_audio
->i_bps
? demuxer
->movi_end
/ sh_audio
->i_bps
: 0;
666 da_priv_t
* priv
= demuxer
->priv
;
669 case DEMUXER_CTRL_GET_TIME_LENGTH
:
670 if (audio_length
<=0) return DEMUXER_CTRL_DONTKNOW
;
671 *((double *)arg
)=(double)audio_length
;
672 return DEMUXER_CTRL_GUESS
;
674 case DEMUXER_CTRL_GET_PERCENT_POS
:
676 return DEMUXER_CTRL_DONTKNOW
;
677 *((int *)arg
)=(int)( (priv
->next_pts
*100) / audio_length
);
678 return DEMUXER_CTRL_OK
;
681 return DEMUXER_CTRL_NOTIMPL
;
686 const demuxer_desc_t demuxer_desc_audio
= {
693 0, // unsafe autodetect
695 demux_audio_fill_buffer
,