Mercurial > mplayer.hg
annotate libmpdemux/demux_audio.c @ 16851:e5a3f78b9511
synced with 1.1145
author | gpoirier |
---|---|
date | Tue, 25 Oct 2005 15:15:46 +0000 |
parents | 67ce689b5764 |
children | 9081ae3a702c |
rev | line source |
---|---|
4694 | 1 |
2 #include "config.h" | |
4712 | 3 #include "../mp_msg.h" |
4694 | 4 |
5 #include <stdlib.h> | |
6 #include <stdio.h> | |
7 #include "stream.h" | |
8 #include "demuxer.h" | |
9 #include "stheader.h" | |
10 #include "genres.h" | |
6763 | 11 #include "mp3_hdr.h" |
4694 | 12 |
13 #include <string.h> | |
14 #ifdef MP_DEBUG | |
15 #include <assert.h> | |
16 #endif | |
17 | |
18 #define MP3 1 | |
19 #define WAV 2 | |
11004 | 20 #define fLaC 3 |
4694 | 21 |
22 | |
23 #define HDR_SIZE 4 | |
24 | |
25 typedef struct da_priv { | |
26 int frmt; | |
4764
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
27 float last_pts; |
4694 | 28 } da_priv_t; |
29 | |
13503
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
30 // how many valid frames in a row we need before accepting as valid MP3 |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
31 #define MIN_MP3_HDRS 5 |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
32 |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
33 //! Used to describe a potential (chain of) MP3 headers we found |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
34 typedef struct mp3_hdr { |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
35 off_t frame_pos; // start of first frame in this "chain" of headers |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
36 off_t next_frame_pos; // here we expect the next header with same parameters |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
37 int mp3_chans; |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
38 int mp3_freq; |
16162
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15215
diff
changeset
|
39 int mpa_spf; |
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15215
diff
changeset
|
40 int mpa_layer; |
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15215
diff
changeset
|
41 int mpa_br; |
13503
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
42 int cons_hdrs; // if this reaches MIN_MP3_HDRS we accept as MP3 file |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
43 struct mp3_hdr *next; |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
44 } mp3_hdr_t; |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
45 |
4694 | 46 extern void free_sh_audio(sh_audio_t* sh); |
4700
30131db5dfbb
Improved mp3 detection (don't detect mpeg1/2 as mp3 anymore)
albeu
parents:
4694
diff
changeset
|
47 extern void resync_audio_stream(sh_audio_t *sh_audio); |
8123
9fc45fe0d444
*HUGE* set of compiler warning fixes, unused variables removal
arpi
parents:
8116
diff
changeset
|
48 extern void print_wave_header(WAVEFORMATEX *h); |
4694 | 49 |
7867 | 50 int hr_mp3_seek = 0; |
4694 | 51 |
13503
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
52 /** |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
53 * \brief free a list of MP3 header descriptions |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
54 * \param list pointer to the head-of-list pointer |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
55 */ |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
56 static void free_mp3_hdrs(mp3_hdr_t **list) { |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
57 mp3_hdr_t *tmp; |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
58 while (*list) { |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
59 tmp = (*list)->next; |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
60 free(*list); |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
61 *list = tmp; |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
62 } |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
63 } |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
64 |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
65 /** |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
66 * \brief add another potential MP3 header to our list |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
67 * If it fits into an existing chain this one is expanded otherwise |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
68 * a new one is created. |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
69 * All entries that expected a MP3 header before the current position |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
70 * are discarded. |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
71 * The list is expected to be and will be kept sorted by next_frame_pos |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
72 * and when those are equal by frame_pos. |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
73 * \param list pointer to the head-of-list pointer |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
74 * \param st_pos stream position where the described header starts |
16162
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15215
diff
changeset
|
75 * \param mp3_chans number of channels as specified by the header (*) |
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15215
diff
changeset
|
76 * \param mp3_freq sampling frequency as specified by the header (*) |
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15215
diff
changeset
|
77 * \param mpa_spf frame size as specified by the header |
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15215
diff
changeset
|
78 * \param mpa_layer layer type ("version") as specified by the header (*) |
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15215
diff
changeset
|
79 * \param mpa_br bitrate as specified by the header |
13503
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
80 * \param mp3_flen length of the frame as specified by the header |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
81 * \return If non-null the current file is accepted as MP3 and the |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
82 * mp3_hdr struct describing the valid chain is returned. Must be |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
83 * freed independent of the list. |
16162
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15215
diff
changeset
|
84 * |
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15215
diff
changeset
|
85 * parameters marked by (*) must be the same for all headers in the same chain |
13503
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
86 */ |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
87 static mp3_hdr_t *add_mp3_hdr(mp3_hdr_t **list, off_t st_pos, |
16162
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15215
diff
changeset
|
88 int mp3_chans, int mp3_freq, int mpa_spf, |
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15215
diff
changeset
|
89 int mpa_layer, int mpa_br, int mp3_flen) { |
13503
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
90 mp3_hdr_t *tmp; |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
91 int in_list = 0; |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
92 while (*list && (*list)->next_frame_pos <= st_pos) { |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
93 if (((*list)->next_frame_pos < st_pos) || ((*list)->mp3_chans != mp3_chans) |
16162
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15215
diff
changeset
|
94 || ((*list)->mp3_freq != mp3_freq) || ((*list)->mpa_layer != mpa_layer) ) { |
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15215
diff
changeset
|
95 // wasn't valid! |
13503
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
96 tmp = (*list)->next; |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
97 free(*list); |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
98 *list = tmp; |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
99 } else { |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
100 (*list)->cons_hdrs++; |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
101 (*list)->next_frame_pos = st_pos + mp3_flen; |
16162
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15215
diff
changeset
|
102 (*list)->mpa_spf = mpa_spf; |
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15215
diff
changeset
|
103 (*list)->mpa_br = mpa_br; |
13503
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
104 if ((*list)->cons_hdrs >= MIN_MP3_HDRS) { |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
105 // copy the valid entry, so that the list can be easily freed |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
106 tmp = malloc(sizeof(mp3_hdr_t)); |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
107 memcpy(tmp, *list, sizeof(mp3_hdr_t)); |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
108 tmp->next = NULL; |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
109 return tmp; |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
110 } |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
111 in_list = 1; |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
112 list = &((*list)->next); |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
113 } |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
114 } |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
115 if (!in_list) { // does not belong into an existing chain, insert |
15215
db6286839af7
Fix MP3 detection (list of found MP3 headers was not kep sorted).
reimar
parents:
15205
diff
changeset
|
116 // find right position to insert to keep sorting |
db6286839af7
Fix MP3 detection (list of found MP3 headers was not kep sorted).
reimar
parents:
15205
diff
changeset
|
117 while (*list && (*list)->next_frame_pos <= st_pos + mp3_flen) |
db6286839af7
Fix MP3 detection (list of found MP3 headers was not kep sorted).
reimar
parents:
15205
diff
changeset
|
118 list = &((*list)->next); |
13503
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
119 tmp = malloc(sizeof(mp3_hdr_t)); |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
120 tmp->frame_pos = st_pos; |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
121 tmp->next_frame_pos = st_pos + mp3_flen; |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
122 tmp->mp3_chans = mp3_chans; |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
123 tmp->mp3_freq = mp3_freq; |
16162
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15215
diff
changeset
|
124 tmp->mpa_spf = mpa_spf; |
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15215
diff
changeset
|
125 tmp->mpa_layer = mpa_layer; |
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15215
diff
changeset
|
126 tmp->mpa_br = mpa_br; |
13503
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
127 tmp->cons_hdrs = 1; |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
128 tmp->next = *list; |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
129 *list = tmp; |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
130 } |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
131 return NULL; |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
132 } |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
133 |
16175 | 134 static int demux_audio_open(demuxer_t* demuxer) { |
4694 | 135 stream_t *s; |
136 sh_audio_t* sh_audio; | |
137 uint8_t hdr[HDR_SIZE]; | |
16162
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15215
diff
changeset
|
138 int frmt = 0, n = 0, step; |
13503
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
139 off_t st_pos = 0, next_frame_pos = 0; |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
140 // mp3_hdrs list is sorted first by next_frame_pos and then by frame_pos |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
141 mp3_hdr_t *mp3_hdrs = NULL, *mp3_found = NULL; |
4694 | 142 da_priv_t* priv; |
143 #ifdef MP_DEBUG | |
144 assert(demuxer != NULL); | |
145 assert(demuxer->stream != NULL); | |
146 #endif | |
147 | |
148 s = demuxer->stream; | |
149 | |
13503
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
150 stream_read(s, hdr, HDR_SIZE); |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
151 while(n < 30000 && !s->eof) { |
16162
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15215
diff
changeset
|
152 int mp3_freq, mp3_chans, mp3_flen, mpa_layer, mpa_spf, mpa_br; |
13503
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
153 st_pos = stream_tell(s) - HDR_SIZE; |
4694 | 154 step = 1; |
155 | |
156 if( hdr[0] == 'R' && hdr[1] == 'I' && hdr[2] == 'F' && hdr[3] == 'F' ) { | |
157 stream_skip(s,4); | |
158 if(s->eof) | |
159 break; | |
160 stream_read(s,hdr,4); | |
161 if(s->eof) | |
162 break; | |
163 if(hdr[0] != 'W' || hdr[1] != 'A' || hdr[2] != 'V' || hdr[3] != 'E' ) | |
164 stream_skip(s,-8); | |
165 else | |
166 // We found wav header. Now we can have 'fmt ' or a mp3 header | |
167 // empty the buffer | |
168 step = 4; | |
8116 | 169 } else if( hdr[0] == 'I' && hdr[1] == 'D' && hdr[2] == '3' && (hdr[3] >= 2)) { |
170 int len; | |
171 stream_skip(s,2); | |
172 stream_read(s,hdr,4); | |
173 len = (hdr[0]<<21) | (hdr[1]<<14) | (hdr[2]<<7) | hdr[3]; | |
174 stream_skip(s,len); | |
175 step = 4; | |
4694 | 176 } else if( hdr[0] == 'f' && hdr[1] == 'm' && hdr[2] == 't' && hdr[3] == ' ' ) { |
177 frmt = WAV; | |
178 break; | |
16162
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15215
diff
changeset
|
179 } else if((mp3_flen = mp_get_mp3_header(hdr, &mp3_chans, &mp3_freq, |
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15215
diff
changeset
|
180 &mpa_spf, &mpa_layer, &mpa_br)) > 0) { |
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15215
diff
changeset
|
181 mp3_found = add_mp3_hdr(&mp3_hdrs, st_pos, mp3_chans, mp3_freq, |
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15215
diff
changeset
|
182 mpa_spf, mpa_layer, mpa_br, mp3_flen); |
13503
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
183 if (mp3_found) { |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
184 frmt = MP3; |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
185 break; |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
186 } |
11004 | 187 } else if( hdr[0] == 'f' && hdr[1] == 'L' && hdr[2] == 'a' && hdr[3] == 'C' ) { |
188 frmt = fLaC; | |
189 stream_skip(s,-4); | |
190 break; | |
4694 | 191 } |
192 // Add here some other audio format detection | |
193 if(step < HDR_SIZE) | |
194 memmove(hdr,&hdr[step],HDR_SIZE-step); | |
13503
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
195 stream_read(s, &hdr[HDR_SIZE - step], step); |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
196 n++; |
4694 | 197 } |
198 | |
13503
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
199 free_mp3_hdrs(&mp3_hdrs); |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
200 |
4694 | 201 if(!frmt) |
202 return 0; | |
203 | |
204 sh_audio = new_sh_audio(demuxer,0); | |
205 | |
206 switch(frmt) { | |
207 case MP3: | |
16162
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15215
diff
changeset
|
208 sh_audio->format = (mp3_found->mpa_layer < 3 ? 0x50 : 0x55); |
13503
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
209 demuxer->movi_start = mp3_found->frame_pos; |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
210 next_frame_pos = mp3_found->next_frame_pos; |
6763 | 211 sh_audio->audio.dwSampleSize= 0; |
16162
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15215
diff
changeset
|
212 sh_audio->audio.dwScale = mp3_found->mpa_spf; |
13503
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
213 sh_audio->audio.dwRate = mp3_found->mp3_freq; |
6763 | 214 sh_audio->wf = malloc(sizeof(WAVEFORMATEX)); |
215 sh_audio->wf->wFormatTag = sh_audio->format; | |
13503
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
216 sh_audio->wf->nChannels = mp3_found->mp3_chans; |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
217 sh_audio->wf->nSamplesPerSec = mp3_found->mp3_freq; |
16162
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15215
diff
changeset
|
218 sh_audio->wf->nAvgBytesPerSec = mp3_found->mpa_br * (1000 / 8); |
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15215
diff
changeset
|
219 sh_audio->wf->nBlockAlign = mp3_found->mpa_spf; |
6763 | 220 sh_audio->wf->wBitsPerSample = 16; |
221 sh_audio->wf->cbSize = 0; | |
16162
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15215
diff
changeset
|
222 sh_audio->i_bps = sh_audio->wf->nAvgBytesPerSec; |
13503
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
223 free(mp3_found); |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
224 mp3_found = NULL; |
4694 | 225 if(s->end_pos) { |
226 char tag[4]; | |
227 stream_seek(s,s->end_pos-128); | |
228 stream_read(s,tag,3); | |
229 tag[3] = '\0'; | |
230 if(strcmp(tag,"TAG")) | |
231 demuxer->movi_end = s->end_pos; | |
232 else { | |
233 char buf[31]; | |
234 uint8_t g; | |
235 demuxer->movi_end = stream_tell(s)-3; | |
236 stream_read(s,buf,30); | |
237 buf[30] = '\0'; | |
238 demux_info_add(demuxer,"Title",buf); | |
239 stream_read(s,buf,30); | |
240 buf[30] = '\0'; | |
241 demux_info_add(demuxer,"Artist",buf); | |
242 stream_read(s,buf,30); | |
243 buf[30] = '\0'; | |
244 demux_info_add(demuxer,"Album",buf); | |
245 stream_read(s,buf,4); | |
4764
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
246 buf[4] = '\0'; |
4694 | 247 demux_info_add(demuxer,"Year",buf); |
248 stream_read(s,buf,30); | |
249 buf[30] = '\0'; | |
250 demux_info_add(demuxer,"Comment",buf); | |
251 if(buf[28] == 0 && buf[29] != 0) { | |
252 uint8_t trk = (uint8_t)buf[29]; | |
253 sprintf(buf,"%d",trk); | |
254 demux_info_add(demuxer,"Track",buf); | |
255 } | |
256 g = stream_read_char(s); | |
257 demux_info_add(demuxer,"Genre",genres[g]); | |
258 } | |
259 } | |
260 break; | |
261 case WAV: { | |
4720
10ad330c1733
fixed WAV demuxer so that it skips to the actual start of the audio data
melanson
parents:
4717
diff
changeset
|
262 unsigned int chunk_type; |
10ad330c1733
fixed WAV demuxer so that it skips to the actual start of the audio data
melanson
parents:
4717
diff
changeset
|
263 unsigned int chunk_size; |
4694 | 264 WAVEFORMATEX* w; |
265 int l; | |
266 l = stream_read_dword_le(s); | |
9596 | 267 if(l < 16) { |
9591
3904b95fce8f
Read extradata for wav files, based on patch by <dimakar(at)yahoo.com>. Required for truespeech files.
rtognimp
parents:
8359
diff
changeset
|
268 mp_msg(MSGT_DEMUX,MSGL_ERR,"[demux_audio] Bad wav header length: too short (%d)!!!\n",l); |
4694 | 269 free_sh_audio(sh_audio); |
270 return 0; | |
271 } | |
12213 | 272 sh_audio->wf = w = (WAVEFORMATEX*)malloc(l > sizeof(WAVEFORMATEX) ? l : sizeof(WAVEFORMATEX)); |
4694 | 273 w->wFormatTag = sh_audio->format = stream_read_word_le(s); |
274 w->nChannels = sh_audio->channels = stream_read_word_le(s); | |
275 w->nSamplesPerSec = sh_audio->samplerate = stream_read_dword_le(s); | |
276 w->nAvgBytesPerSec = stream_read_dword_le(s); | |
277 w->nBlockAlign = stream_read_word_le(s); | |
278 w->wBitsPerSample = sh_audio->samplesize = stream_read_word_le(s); | |
9616
a6bbab17a87f
10l cbSize not inited for l=16.\n Could give problem in print_wave_header
rtognimp
parents:
9596
diff
changeset
|
279 w->cbSize = 0; |
16162
b5c2254d13f8
set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents:
15215
diff
changeset
|
280 sh_audio->i_bps = sh_audio->wf->nAvgBytesPerSec; |
9596 | 281 l -= 16; |
282 if (l > 0) { | |
9591
3904b95fce8f
Read extradata for wav files, based on patch by <dimakar(at)yahoo.com>. Required for truespeech files.
rtognimp
parents:
8359
diff
changeset
|
283 w->cbSize = stream_read_word_le(s); |
9596 | 284 l -= 2; |
9655 | 285 if (w->cbSize > 0) { |
9591
3904b95fce8f
Read extradata for wav files, based on patch by <dimakar(at)yahoo.com>. Required for truespeech files.
rtognimp
parents:
8359
diff
changeset
|
286 if (l < w->cbSize) { |
3904b95fce8f
Read extradata for wav files, based on patch by <dimakar(at)yahoo.com>. Required for truespeech files.
rtognimp
parents:
8359
diff
changeset
|
287 mp_msg(MSGT_DEMUX,MSGL_ERR,"[demux_audio] truncated extradata (%d < %d)\n", |
3904b95fce8f
Read extradata for wav files, based on patch by <dimakar(at)yahoo.com>. Required for truespeech files.
rtognimp
parents:
8359
diff
changeset
|
288 l,w->cbSize); |
3904b95fce8f
Read extradata for wav files, based on patch by <dimakar(at)yahoo.com>. Required for truespeech files.
rtognimp
parents:
8359
diff
changeset
|
289 stream_read(s,(char*)((char*)(w)+sizeof(WAVEFORMATEX)),l); |
3904b95fce8f
Read extradata for wav files, based on patch by <dimakar(at)yahoo.com>. Required for truespeech files.
rtognimp
parents:
8359
diff
changeset
|
290 l = 0; |
3904b95fce8f
Read extradata for wav files, based on patch by <dimakar(at)yahoo.com>. Required for truespeech files.
rtognimp
parents:
8359
diff
changeset
|
291 } else { |
3904b95fce8f
Read extradata for wav files, based on patch by <dimakar(at)yahoo.com>. Required for truespeech files.
rtognimp
parents:
8359
diff
changeset
|
292 stream_read(s,(char*)((char*)(w)+sizeof(WAVEFORMATEX)),w->cbSize); |
3904b95fce8f
Read extradata for wav files, based on patch by <dimakar(at)yahoo.com>. Required for truespeech files.
rtognimp
parents:
8359
diff
changeset
|
293 l -= w->cbSize; |
3904b95fce8f
Read extradata for wav files, based on patch by <dimakar(at)yahoo.com>. Required for truespeech files.
rtognimp
parents:
8359
diff
changeset
|
294 } |
9655 | 295 } |
9596 | 296 } |
9591
3904b95fce8f
Read extradata for wav files, based on patch by <dimakar(at)yahoo.com>. Required for truespeech files.
rtognimp
parents:
8359
diff
changeset
|
297 |
7847 | 298 if(verbose>0) print_wave_header(w); |
4694 | 299 if(l) |
300 stream_skip(s,l); | |
4720
10ad330c1733
fixed WAV demuxer so that it skips to the actual start of the audio data
melanson
parents:
4717
diff
changeset
|
301 do |
10ad330c1733
fixed WAV demuxer so that it skips to the actual start of the audio data
melanson
parents:
4717
diff
changeset
|
302 { |
10ad330c1733
fixed WAV demuxer so that it skips to the actual start of the audio data
melanson
parents:
4717
diff
changeset
|
303 chunk_type = stream_read_fourcc(demuxer->stream); |
10ad330c1733
fixed WAV demuxer so that it skips to the actual start of the audio data
melanson
parents:
4717
diff
changeset
|
304 chunk_size = stream_read_dword_le(demuxer->stream); |
10ad330c1733
fixed WAV demuxer so that it skips to the actual start of the audio data
melanson
parents:
4717
diff
changeset
|
305 if (chunk_type != mmioFOURCC('d', 'a', 't', 'a')) |
10ad330c1733
fixed WAV demuxer so that it skips to the actual start of the audio data
melanson
parents:
4717
diff
changeset
|
306 stream_skip(demuxer->stream, chunk_size); |
10ad330c1733
fixed WAV demuxer so that it skips to the actual start of the audio data
melanson
parents:
4717
diff
changeset
|
307 } while (chunk_type != mmioFOURCC('d', 'a', 't', 'a')); |
4694 | 308 demuxer->movi_start = stream_tell(s); |
309 demuxer->movi_end = s->end_pos; | |
7847 | 310 // printf("wav: %X .. %X\n",(int)demuxer->movi_start,(int)demuxer->movi_end); |
13006 | 311 // Check if it contains dts audio |
312 if((w->wFormatTag == 0x01) && (w->nChannels == 2) && (w->nSamplesPerSec == 44100)) { | |
313 unsigned char buf[16384]; // vlc uses 16384*4 (4 dts frames) | |
314 unsigned int i; | |
315 stream_read(s, buf, sizeof(buf)); | |
316 for (i = 0; i < sizeof(buf); i += 2) { | |
317 // DTS, 14 bit, LE | |
318 if((buf[i] == 0xff) && (buf[i+1] == 0x1f) && (buf[i+2] == 0x00) && | |
319 (buf[i+3] == 0xe8) && ((buf[i+4] & 0xfe) == 0xf0) && (buf[i+5] == 0x07)) { | |
320 sh_audio->format = 0x2001; | |
321 mp_msg(MSGT_DEMUX,MSGL_V,"[demux_audio] DTS audio in wav, 14 bit, LE\n"); | |
322 break; | |
323 } | |
324 // DTS, 14 bit, BE | |
325 if((buf[i] == 0x1f) && (buf[i+1] == 0xff) && (buf[i+2] == 0xe8) && | |
326 (buf[i+3] == 0x00) && (buf[i+4] == 0x07) && ((buf[i+5] & 0xfe) == 0xf0)) { | |
327 sh_audio->format = 0x2001; | |
328 mp_msg(MSGT_DEMUX,MSGL_V,"[demux_audio] DTS audio in wav, 14 bit, BE\n"); | |
329 break; | |
330 } | |
331 // DTS, 16 bit, BE | |
332 if((buf[i] == 0x7f) && (buf[i+1] == 0xfe) && (buf[i+2] == 0x80) && | |
333 (buf[i+3] == 0x01)) { | |
334 sh_audio->format = 0x2001; | |
335 mp_msg(MSGT_DEMUX,MSGL_V,"[demux_audio] DTS audio in wav, 16 bit, BE\n"); | |
336 break; | |
337 } | |
338 // DTS, 16 bit, LE | |
339 if((buf[i] == 0xfe) && (buf[i+1] == 0x7f) && (buf[i+2] == 0x01) && | |
340 (buf[i+3] == 0x80)) { | |
341 sh_audio->format = 0x2001; | |
342 mp_msg(MSGT_DEMUX,MSGL_V,"[demux_audio] DTS audio in wav, 16 bit, LE\n"); | |
343 break; | |
344 } | |
345 } | |
346 if (sh_audio->format == 0x2001) | |
347 mp_msg(MSGT_DEMUX,MSGL_DBG2,"[demux_audio] DTS sync offset = %u\n", i); | |
348 | |
349 } | |
350 stream_seek(s,demuxer->movi_start); | |
4694 | 351 } break; |
11004 | 352 case fLaC: |
353 sh_audio->format = mmioFOURCC('f', 'L', 'a', 'C'); | |
354 demuxer->movi_start = stream_tell(s); | |
355 demuxer->movi_end = s->end_pos; | |
356 break; | |
4694 | 357 } |
358 | |
359 priv = (da_priv_t*)malloc(sizeof(da_priv_t)); | |
360 priv->frmt = frmt; | |
4764
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
361 priv->last_pts = -1; |
4694 | 362 demuxer->priv = priv; |
363 demuxer->audio->id = 0; | |
364 demuxer->audio->sh = sh_audio; | |
365 sh_audio->ds = demuxer->audio; | |
10244
a084597140af
don't set audio pts to infinity after seeking in mp3 files :)
rfelker
parents:
9655
diff
changeset
|
366 sh_audio->samplerate = sh_audio->audio.dwRate; |
4694 | 367 |
368 if(stream_tell(s) != demuxer->movi_start) | |
13503
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
369 { |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
370 mp_msg(MSGT_DEMUX, MSGL_V, "demux_audio: seeking from 0x%X to start pos 0x%X\n", |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
371 (int)stream_tell(s), (int)demuxer->movi_start); |
4694 | 372 stream_seek(s,demuxer->movi_start); |
13503
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
373 if (stream_tell(s) != demuxer->movi_start) { |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
374 mp_msg(MSGT_DEMUX, MSGL_V, "demux_audio: seeking failed, now at 0x%X!\n", |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
375 (int)stream_tell(s)); |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
376 if (next_frame_pos) { |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
377 mp_msg(MSGT_DEMUX, MSGL_V, "demux_audio: seeking to 0x%X instead\n", |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
378 (int)next_frame_pos); |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
379 stream_seek(s, next_frame_pos); |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
380 } |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
381 } |
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
382 } |
4694 | 383 |
13503
f8347c521898
better, tuneable (via #define) MP3 detection, limit demux_audio to scanning
reimar
parents:
13357
diff
changeset
|
384 mp_msg(MSGT_DEMUX,MSGL_V,"demux_audio: audio data 0x%X - 0x%X \n",(int)demuxer->movi_start,(int)demuxer->movi_end); |
4712 | 385 |
16175 | 386 return DEMUXER_TYPE_AUDIO; |
4694 | 387 } |
388 | |
389 | |
16175 | 390 static int demux_audio_fill_buffer(demuxer_t *demuxer, demux_stream_t *ds) { |
14608
9595f1de1393
Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents:
13503
diff
changeset
|
391 int l; |
9595f1de1393
Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents:
13503
diff
changeset
|
392 demux_packet_t* dp; |
4694 | 393 sh_audio_t* sh_audio; |
394 demuxer_t* demux; | |
395 da_priv_t* priv; | |
396 stream_t* s; | |
397 #ifdef MP_DEBUG | |
398 assert(ds != NULL); | |
399 assert(ds->sh != NULL); | |
400 assert(ds->demuxer != NULL); | |
401 #endif | |
402 sh_audio = ds->sh; | |
403 demux = ds->demuxer; | |
404 priv = demux->priv; | |
405 s = demux->stream; | |
406 | |
16438
871e94131840
Ignore movi_end (except on error) to allow playing growing files.
reimar
parents:
16346
diff
changeset
|
407 if(s->eof) |
4694 | 408 return 0; |
409 | |
410 switch(priv->frmt) { | |
411 case MP3 : | |
14608
9595f1de1393
Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents:
13503
diff
changeset
|
412 while(1) { |
4694 | 413 uint8_t hdr[4]; |
414 stream_read(s,hdr,4); | |
16438
871e94131840
Ignore movi_end (except on error) to allow playing growing files.
reimar
parents:
16346
diff
changeset
|
415 if (s->eof) |
14608
9595f1de1393
Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents:
13503
diff
changeset
|
416 return 0; |
9595f1de1393
Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents:
13503
diff
changeset
|
417 l = mp_decode_mp3_header(hdr); |
9595f1de1393
Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents:
13503
diff
changeset
|
418 if(l < 0) { |
16438
871e94131840
Ignore movi_end (except on error) to allow playing growing files.
reimar
parents:
16346
diff
changeset
|
419 if (demux->movi_end && stream_tell(s) >= demux->movi_end) |
871e94131840
Ignore movi_end (except on error) to allow playing growing files.
reimar
parents:
16346
diff
changeset
|
420 return 0; // might be ID3 tag, i.e. EOF |
4694 | 421 stream_skip(s,-3); |
422 } else { | |
14608
9595f1de1393
Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents:
13503
diff
changeset
|
423 dp = new_demux_packet(l); |
4694 | 424 memcpy(dp->buffer,hdr,4); |
16438
871e94131840
Ignore movi_end (except on error) to allow playing growing files.
reimar
parents:
16346
diff
changeset
|
425 if (stream_read(s,dp->buffer + 4,l-4) != l-4) |
871e94131840
Ignore movi_end (except on error) to allow playing growing files.
reimar
parents:
16346
diff
changeset
|
426 return 0; |
15199
980910eb6f0c
assign correct tag, dwScale and dwBlockAlign to mpeg audio; optionally assign layer and samples_per_frame when parsing mpa header
nicodvb
parents:
14910
diff
changeset
|
427 priv->last_pts = priv->last_pts < 0 ? 0 : priv->last_pts + sh_audio->audio.dwScale/(float)sh_audio->samplerate; |
14608
9595f1de1393
Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents:
13503
diff
changeset
|
428 break; |
4694 | 429 } |
4700
30131db5dfbb
Improved mp3 detection (don't detect mpeg1/2 as mp3 anymore)
albeu
parents:
4694
diff
changeset
|
430 } break; |
4694 | 431 case WAV : { |
14608
9595f1de1393
Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents:
13503
diff
changeset
|
432 l = sh_audio->wf->nAvgBytesPerSec; |
9595f1de1393
Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents:
13503
diff
changeset
|
433 dp = new_demux_packet(l); |
10891
65ed62e138fa
Fixed 'reading after EOF'. demuxers didn't check, how many they've read!
lumag
parents:
10882
diff
changeset
|
434 l = stream_read(s,dp->buffer,l); |
14910
07be3bc42d47
100l to reimar.. sh_audio->samplerate and sh_audio->i_bps are not the same
rfelker
parents:
14608
diff
changeset
|
435 priv->last_pts = priv->last_pts < 0 ? 0 : priv->last_pts + l/(float)sh_audio->i_bps; |
14608
9595f1de1393
Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents:
13503
diff
changeset
|
436 break; |
4694 | 437 } |
11004 | 438 case fLaC: { |
14608
9595f1de1393
Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents:
13503
diff
changeset
|
439 l = 65535; |
9595f1de1393
Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents:
13503
diff
changeset
|
440 dp = new_demux_packet(l); |
11004 | 441 l = stream_read(s,dp->buffer,l); |
14910
07be3bc42d47
100l to reimar.. sh_audio->samplerate and sh_audio->i_bps are not the same
rfelker
parents:
14608
diff
changeset
|
442 priv->last_pts = priv->last_pts < 0 ? 0 : priv->last_pts + l/(float)sh_audio->i_bps; |
14608
9595f1de1393
Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents:
13503
diff
changeset
|
443 break; |
11004 | 444 } |
4694 | 445 default: |
4720
10ad330c1733
fixed WAV demuxer so that it skips to the actual start of the audio data
melanson
parents:
4717
diff
changeset
|
446 printf("Audio demuxer : unknown format %d\n",priv->frmt); |
14608
9595f1de1393
Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents:
13503
diff
changeset
|
447 return 0; |
4694 | 448 } |
449 | |
14608
9595f1de1393
Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents:
13503
diff
changeset
|
450 resize_demux_packet(dp, l); |
9595f1de1393
Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents:
13503
diff
changeset
|
451 ds->pts = priv->last_pts - (ds_tell_pts(demux->audio) - |
9595f1de1393
Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents:
13503
diff
changeset
|
452 sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps; |
9595f1de1393
Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents:
13503
diff
changeset
|
453 ds_add_packet(ds, dp); |
9595f1de1393
Fix possible hang when playing broken MP3 from linear stream and remove
reimar
parents:
13503
diff
changeset
|
454 return 1; |
4694 | 455 } |
456 | |
4764
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
457 static void high_res_mp3_seek(demuxer_t *demuxer,float time) { |
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
458 uint8_t hdr[4]; |
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
459 int len,nf; |
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
460 da_priv_t* priv = demuxer->priv; |
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
461 sh_audio_t* sh = (sh_audio_t*)demuxer->audio->sh; |
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
462 |
15199
980910eb6f0c
assign correct tag, dwScale and dwBlockAlign to mpeg audio; optionally assign layer and samples_per_frame when parsing mpa header
nicodvb
parents:
14910
diff
changeset
|
463 nf = time*sh->samplerate/sh->audio.dwScale; |
4764
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
464 while(nf > 0) { |
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
465 stream_read(demuxer->stream,hdr,4); |
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
466 len = mp_decode_mp3_header(hdr); |
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
467 if(len < 0) { |
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
468 stream_skip(demuxer->stream,-3); |
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
469 continue; |
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
470 } |
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
471 stream_skip(demuxer->stream,len-4); |
15199
980910eb6f0c
assign correct tag, dwScale and dwBlockAlign to mpeg audio; optionally assign layer and samples_per_frame when parsing mpa header
nicodvb
parents:
14910
diff
changeset
|
472 priv->last_pts += sh->audio.dwScale/(float)sh->samplerate; |
4764
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
473 nf--; |
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
474 } |
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
475 } |
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
476 |
16175 | 477 static void demux_audio_seek(demuxer_t *demuxer,float rel_seek_secs,int flags){ |
4694 | 478 sh_audio_t* sh_audio; |
479 stream_t* s; | |
480 int base,pos; | |
481 float len; | |
482 da_priv_t* priv; | |
483 | |
4764
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
484 if(!(sh_audio = demuxer->audio->sh)) |
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
485 return; |
4694 | 486 s = demuxer->stream; |
487 priv = demuxer->priv; | |
488 | |
4764
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
489 if(priv->frmt == MP3 && hr_mp3_seek && !(flags & 2)) { |
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
490 len = (flags & 1) ? rel_seek_secs - priv->last_pts : rel_seek_secs; |
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
491 if(len < 0) { |
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
492 stream_seek(s,demuxer->movi_start); |
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
493 len = priv->last_pts + len; |
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
494 priv->last_pts = 0; |
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
495 } |
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
496 if(len > 0) |
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
497 high_res_mp3_seek(demuxer,len); |
8056
324b6e5387be
A-V sync cleanup: sh_audio->timer replaced by sh_audio->delay, it contains
arpi
parents:
7867
diff
changeset
|
498 sh_audio->delay = priv->last_pts - (ds_tell_pts(demuxer->audio)-sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps; |
4764
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
499 resync_audio_stream(sh_audio); |
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
500 return; |
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
501 } |
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
502 |
5796 | 503 base = flags&1 ? demuxer->movi_start : stream_tell(s); |
504 if(flags&2) | |
505 pos = base + ((demuxer->movi_end - demuxer->movi_start)*rel_seek_secs); | |
506 else | |
507 pos = base + (rel_seek_secs*sh_audio->i_bps); | |
4694 | 508 |
509 if(demuxer->movi_end && pos >= demuxer->movi_end) { | |
8359 | 510 pos = demuxer->movi_end; |
511 //sh_audio->delay = (stream_tell(s) - demuxer->movi_start)/(float)sh_audio->i_bps; | |
512 //return; | |
4694 | 513 } else if(pos < demuxer->movi_start) |
514 pos = demuxer->movi_start; | |
4764
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
515 |
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
516 priv->last_pts = (pos-demuxer->movi_start)/(float)sh_audio->i_bps; |
8056
324b6e5387be
A-V sync cleanup: sh_audio->timer replaced by sh_audio->delay, it contains
arpi
parents:
7867
diff
changeset
|
517 sh_audio->delay = priv->last_pts - (ds_tell_pts(demuxer->audio)-sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps; |
4694 | 518 |
519 switch(priv->frmt) { | |
520 case WAV: | |
16818
67ce689b5764
Fix seeking in wav files: align relative to start of data, not start of file
reimar
parents:
16438
diff
changeset
|
521 pos -= (pos - demuxer->movi_start) % |
67ce689b5764
Fix seeking in wav files: align relative to start of data, not start of file
reimar
parents:
16438
diff
changeset
|
522 (sh_audio->wf->nBlockAlign ? sh_audio->wf->nBlockAlign : |
67ce689b5764
Fix seeking in wav files: align relative to start of data, not start of file
reimar
parents:
16438
diff
changeset
|
523 (sh_audio->channels * sh_audio->samplesize)); |
4764
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
524 // We need to decrease the pts by one step to make it the "last one" |
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
525 priv->last_pts -= sh_audio->wf->nAvgBytesPerSec/(float)sh_audio->i_bps; |
4694 | 526 break; |
527 } | |
528 | |
529 stream_seek(s,pos); | |
530 | |
4700
30131db5dfbb
Improved mp3 detection (don't detect mpeg1/2 as mp3 anymore)
albeu
parents:
4694
diff
changeset
|
531 resync_audio_stream(sh_audio); |
30131db5dfbb
Improved mp3 detection (don't detect mpeg1/2 as mp3 anymore)
albeu
parents:
4694
diff
changeset
|
532 |
4694 | 533 } |
4764
9579496a91db
Added pts for mp3 and wav. And perfect seeking for mp3, unless flags & 2
albeu
parents:
4720
diff
changeset
|
534 |
16175 | 535 static void demux_close_audio(demuxer_t* demuxer) { |
5812 | 536 da_priv_t* priv = demuxer->priv; |
537 | |
538 if(!priv) | |
539 return; | |
540 free(priv); | |
541 } | |
542 | |
16175 | 543 static int demux_audio_control(demuxer_t *demuxer,int cmd, void *arg){ |
10882
e46e5a6c53b8
get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents:
10244
diff
changeset
|
544 sh_audio_t *sh_audio=demuxer->audio->sh; |
e46e5a6c53b8
get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents:
10244
diff
changeset
|
545 int audio_length = demuxer->movi_end / sh_audio->i_bps; |
e46e5a6c53b8
get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents:
10244
diff
changeset
|
546 da_priv_t* priv = demuxer->priv; |
e46e5a6c53b8
get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents:
10244
diff
changeset
|
547 |
e46e5a6c53b8
get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents:
10244
diff
changeset
|
548 switch(cmd) { |
e46e5a6c53b8
get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents:
10244
diff
changeset
|
549 case DEMUXER_CTRL_GET_TIME_LENGTH: |
e46e5a6c53b8
get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents:
10244
diff
changeset
|
550 if (audio_length<=0) return DEMUXER_CTRL_DONTKNOW; |
16346
6ff303d2876b
Make -identify's 'ID_LENGTH=' print a float and not an integer.. The
ods15
parents:
16175
diff
changeset
|
551 *((double *)arg)=(double)audio_length; |
10882
e46e5a6c53b8
get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents:
10244
diff
changeset
|
552 return DEMUXER_CTRL_GUESS; |
e46e5a6c53b8
get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents:
10244
diff
changeset
|
553 |
e46e5a6c53b8
get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents:
10244
diff
changeset
|
554 case DEMUXER_CTRL_GET_PERCENT_POS: |
e46e5a6c53b8
get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents:
10244
diff
changeset
|
555 if (audio_length<=0) |
e46e5a6c53b8
get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents:
10244
diff
changeset
|
556 return DEMUXER_CTRL_DONTKNOW; |
e46e5a6c53b8
get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents:
10244
diff
changeset
|
557 *((int *)arg)=(int)( (priv->last_pts*100) / audio_length); |
e46e5a6c53b8
get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents:
10244
diff
changeset
|
558 return DEMUXER_CTRL_OK; |
e46e5a6c53b8
get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents:
10244
diff
changeset
|
559 |
e46e5a6c53b8
get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents:
10244
diff
changeset
|
560 default: |
e46e5a6c53b8
get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents:
10244
diff
changeset
|
561 return DEMUXER_CTRL_NOTIMPL; |
e46e5a6c53b8
get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents:
10244
diff
changeset
|
562 } |
e46e5a6c53b8
get_percent_pos support in demux_audio, patch by ROSEN Jeremy <jeremy.rosen at c-s.fr>
faust3
parents:
10244
diff
changeset
|
563 } |
16175 | 564 |
565 | |
566 demuxer_desc_t demuxer_desc_audio = { | |
567 "Audio demuxer", | |
568 "audio", | |
569 "Audio file", | |
570 "?", | |
571 "Audio only files", | |
572 DEMUXER_TYPE_AUDIO, | |
573 0, // unsafe autodetect | |
574 demux_audio_open, | |
575 demux_audio_fill_buffer, | |
576 NULL, | |
577 demux_close_audio, | |
578 demux_audio_seek, | |
579 demux_audio_control | |
580 }; |