Mercurial > mplayer.hg
annotate libmpcodecs/ad_flac.c @ 12016:b962aaad2940
rawvideo muxer patch by John Earl <jwe21@cam.ac.uk>
author | ranma |
---|---|
date | Tue, 09 Mar 2004 14:46:34 +0000 |
parents | 8ab2028e6ed9 |
children |
rev | line source |
---|---|
11004 | 1 /* |
2 * This is FLAC decoder for MPlayer using stream_decoder from libFLAC | |
3 * (directly or from libmpflac). | |
4 * This file is part of MPlayer, see http://mplayerhq.hu/ for info. | |
5 * Copyright (C) 2003 Dmitry Baryshkov <mitya at school.ioffe.ru> | |
6 * | |
7 * This program is free software; you can redistribute it and/or modify | |
8 * it under the terms of the GNU General Public License as published by | |
9 * the Free Software Foundation; either version 2 of the License, or | |
10 * (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 * GNU General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU General Public License | |
18 * along with this program; if not, write to the Free Software | |
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 * | |
21 * parse_double_, grabbag__replaygain_load_from_vorbiscomment, grabbag__replaygain_compute_scale_factor | |
22 * functions are imported from FLAC project (from grabbag lib sources (replaygain.c)) and are | |
23 * Copyright (C) 2002,2003 Josh Coalson under the terms of GPL. | |
24 */ | |
25 | |
26 /* | |
27 * TODO: | |
28 * in demux_audio use data from seektable block for seeking. | |
29 * support FLAC-in-Ogg. | |
30 */ | |
31 | |
32 #include <stdio.h> | |
33 #include <stdlib.h> | |
34 #include <unistd.h> | |
35 #include <math.h> | |
36 | |
37 #include "config.h" | |
38 #ifdef HAVE_FLAC | |
39 #include "ad_internal.h" | |
40 #include "mp_msg.h" | |
41 | |
42 static ad_info_t info = { | |
43 "FLAC audio decoder", // name of the driver | |
44 "flac", // driver name. should be the same as filename without ad_ | |
45 "Dmitry Baryshkov", // writer/maintainer of _this_ file | |
46 "http://flac.sf.net/", // writer/maintainer/site of the _codec_ | |
47 "" // comments | |
48 }; | |
49 | |
50 LIBAD_EXTERN(flac) | |
51 | |
52 #ifdef USE_MPFLAC_DECODER | |
53 #include "FLAC_stream_decoder.h" | |
54 #include "FLAC_assert.h" | |
55 #include "FLAC_metadata.h" | |
56 #else | |
57 #include "FLAC/stream_decoder.h" | |
58 #include "FLAC/assert.h" | |
59 #include "FLAC/metadata.h" | |
60 #endif | |
61 | |
62 /* dithering & replaygain always from libmpflac */ | |
63 #include "dither.h" | |
64 #include "replaygain_synthesis.h" | |
65 | |
66 /* Some global constants. Thay have to be configurable, so leaved them as globals. */ | |
67 static const FLAC__bool album_mode = true; | |
68 static const int preamp = 0; | |
69 static const FLAC__bool hard_limit = false; | |
70 static const int noise_shaping = 1; | |
71 static const FLAC__bool dither = true; | |
72 typedef struct flac_struct_st | |
73 { | |
74 FLAC__StreamDecoder *flac_dec; /*decoder handle*/ | |
75 sh_audio_t *sh; /* link back to corresponding sh */ | |
76 | |
77 /* set this fields before calling FLAC__stream_decoder_process_single */ | |
78 unsigned char *buf; | |
79 int minlen; | |
80 int maxlen; | |
81 /* Here goes number written at write_callback */ | |
82 int written; | |
83 | |
84 /* replaygain and dithering via plugin_common */ | |
85 FLAC__bool has_replaygain; | |
86 double replay_scale; | |
87 DitherContext dither_context; | |
88 int bits_per_sample; | |
89 } flac_struct_t; | |
90 | |
91 FLAC__StreamDecoderReadStatus flac_read_callback (const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data) | |
92 { | |
11476
1188bf65b776
Made the FLAC decoder be less greedy resulting in much better A/V sync handling.
mosu
parents:
11004
diff
changeset
|
93 /* Don't be greedy. Try to read as few packets as possible. *bytes is often |
1188bf65b776
Made the FLAC decoder be less greedy resulting in much better A/V sync handling.
mosu
parents:
11004
diff
changeset
|
94 > 60kb big which is more than one second of data. Reading it all at |
1188bf65b776
Made the FLAC decoder be less greedy resulting in much better A/V sync handling.
mosu
parents:
11004
diff
changeset
|
95 once sucks in all packets available making d_audio->pts jump to the |
1188bf65b776
Made the FLAC decoder be less greedy resulting in much better A/V sync handling.
mosu
parents:
11004
diff
changeset
|
96 pts of the last packet read which is not what we want. We're decoging |
1188bf65b776
Made the FLAC decoder be less greedy resulting in much better A/V sync handling.
mosu
parents:
11004
diff
changeset
|
97 only one FLAC block anyway, so let's just read as few bytes as |
1188bf65b776
Made the FLAC decoder be less greedy resulting in much better A/V sync handling.
mosu
parents:
11004
diff
changeset
|
98 neccessary. */ |
1188bf65b776
Made the FLAC decoder be less greedy resulting in much better A/V sync handling.
mosu
parents:
11004
diff
changeset
|
99 int b = demux_read_data(((flac_struct_t*)client_data)->sh->ds, buffer, *bytes > 500 ? 500 : *bytes); |
1188bf65b776
Made the FLAC decoder be less greedy resulting in much better A/V sync handling.
mosu
parents:
11004
diff
changeset
|
100 mp_msg(MSGT_DECAUDIO, MSGL_DBG2, "\nFLAC READ CB read %d bytes\n", b); |
11004 | 101 *bytes = b; |
102 if (b <= 0) | |
103 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; | |
104 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; | |
105 } | |
106 | |
107 /*FIXME: we need to support format conversion:(flac specs allow bits/sample to be from 4 to 32. Not only 8 and 16 !!!)*/ | |
108 FLAC__StreamDecoderWriteStatus flac_write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data) | |
109 { | |
110 FLAC__byte *buf = ((flac_struct_t*)(client_data))->buf; | |
111 int bps = ((flac_struct_t*)(client_data))->sh->samplesize; | |
11701 | 112 int lowendian = (((flac_struct_t*)(client_data))->sh->sample_format == AFMT_S16_LE); |
113 int unsigned_data = (((flac_struct_t*)(client_data))->sh->sample_format == AFMT_U8); | |
11004 | 114 mp_msg(MSGT_DECAUDIO, MSGL_DBG2, "\nWrite callback (%d bytes)!!!!\n", bps*frame->header.blocksize*frame->header.channels); |
115 if (buf == NULL) | |
116 { | |
117 /* This is used in control for skipping 1 audio frame */ | |
118 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; | |
119 } | |
11701 | 120 FLAC__replaygain_synthesis__apply_gain( |
11004 | 121 buf, |
11701 | 122 lowendian, |
123 unsigned_data, | |
11004 | 124 buffer, |
125 frame->header.blocksize, | |
126 frame->header.channels, | |
127 ((flac_struct_t*)(client_data))->bits_per_sample, | |
128 ((flac_struct_t*)(client_data))->sh->samplesize * 8, | |
129 ((flac_struct_t*)(client_data))->replay_scale, | |
130 hard_limit, | |
131 dither, | |
132 &(((flac_struct_t*)(client_data))->dither_context) | |
133 ); | |
134 ((flac_struct_t*)(client_data))->written += bps*frame->header.blocksize*frame->header.channels; | |
135 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; | |
136 } | |
137 | |
138 #ifdef local_min | |
139 #undef local_min | |
140 #endif | |
141 #define local_min(a,b) ((a)<(b)?(a):(b)) | |
142 | |
143 static FLAC__bool parse_double_(const FLAC__StreamMetadata_VorbisComment_Entry *entry, double *val) | |
144 { | |
145 char s[32], *end; | |
146 const char *p, *q; | |
147 double v; | |
148 | |
149 FLAC__ASSERT(0 != entry); | |
150 FLAC__ASSERT(0 != val); | |
151 | |
152 p = (const char *)entry->entry; | |
153 q = strchr(p, '='); | |
154 if(0 == q) | |
155 return false; | |
156 q++; | |
157 memset(s, 0, sizeof(s)-1); | |
158 strncpy(s, q, local_min(sizeof(s)-1, entry->length - (q-p))); | |
159 | |
160 v = strtod(s, &end); | |
161 if(end == s) | |
162 return false; | |
163 | |
164 *val = v; | |
165 return true; | |
166 } | |
167 | |
168 FLAC__bool grabbag__replaygain_load_from_vorbiscomment(const FLAC__StreamMetadata *block, FLAC__bool album_mode, double *gain, double *peak) | |
169 { | |
170 int gain_offset, peak_offset; | |
171 static const FLAC__byte *tag_title_gain_ = "REPLAYGAIN_TRACK_GAIN"; | |
172 static const FLAC__byte *tag_title_peak_ = "REPLAYGAIN_TRACK_PEAK"; | |
173 static const FLAC__byte *tag_album_gain_ = "REPLAYGAIN_ALBUM_GAIN"; | |
174 static const FLAC__byte *tag_album_peak_ = "REPLAYGAIN_ALBUM_PEAK"; | |
175 | |
176 FLAC__ASSERT(0 != block); | |
177 FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); | |
178 | |
179 if(0 > (gain_offset = FLAC__metadata_object_vorbiscomment_find_entry_from(block, /*offset=*/0, (const char *)(album_mode? tag_album_gain_ : tag_title_gain_)))) | |
180 return false; | |
181 if(0 > (peak_offset = FLAC__metadata_object_vorbiscomment_find_entry_from(block, /*offset=*/0, (const char *)(album_mode? tag_album_peak_ : tag_title_peak_)))) | |
182 return false; | |
183 | |
184 if(!parse_double_(block->data.vorbis_comment.comments + gain_offset, gain)) | |
185 return false; | |
186 if(!parse_double_(block->data.vorbis_comment.comments + peak_offset, peak)) | |
187 return false; | |
188 | |
189 return true; | |
190 } | |
191 | |
192 double grabbag__replaygain_compute_scale_factor(double peak, double gain, double preamp, FLAC__bool prevent_clipping) | |
193 { | |
194 double scale; | |
195 FLAC__ASSERT(peak >= 0.0); | |
196 gain += preamp; | |
197 scale = (float) pow(10.0, gain * 0.05); | |
198 if(prevent_clipping && peak > 0.0) { | |
199 const double max_scale = (float)(1.0 / peak); | |
200 if(scale > max_scale) | |
201 scale = max_scale; | |
202 } | |
203 return scale; | |
204 } | |
205 | |
206 void flac_metadata_callback (const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data) | |
207 { | |
208 int i, j; | |
209 sh_audio_t *sh = ((flac_struct_t*)client_data)->sh; | |
210 mp_msg(MSGT_DECAUDIO, MSGL_DBG2, "Metadata received\n"); | |
211 switch (metadata->type) | |
212 { | |
213 case FLAC__METADATA_TYPE_STREAMINFO: | |
214 mp_msg(MSGT_DECAUDIO, MSGL_V, "STREAMINFO block (%u bytes):\n", metadata->length); | |
215 mp_msg(MSGT_DECAUDIO, MSGL_V, "min_blocksize: %u samples\n", metadata->data.stream_info.min_blocksize); | |
216 mp_msg(MSGT_DECAUDIO, MSGL_V, "max_blocksize: %u samples\n", metadata->data.stream_info.max_blocksize); | |
217 mp_msg(MSGT_DECAUDIO, MSGL_V, "min_framesize: %u bytes\n", metadata->data.stream_info.min_framesize); | |
218 mp_msg(MSGT_DECAUDIO, MSGL_V, "max_framesize: %u bytes\n", metadata->data.stream_info.max_framesize); | |
219 mp_msg(MSGT_DECAUDIO, MSGL_V, "sample_rate: %u Hz\n", metadata->data.stream_info.sample_rate); | |
220 sh->samplerate = metadata->data.stream_info.sample_rate; | |
221 mp_msg(MSGT_DECAUDIO, MSGL_V, "channels: %u\n", metadata->data.stream_info.channels); | |
222 sh->channels = metadata->data.stream_info.channels; | |
223 mp_msg(MSGT_DECAUDIO, MSGL_V, "bits_per_sample: %u\n", metadata->data.stream_info.bits_per_sample); | |
224 ((flac_struct_t*)client_data)->bits_per_sample = metadata->data.stream_info.bits_per_sample; | |
225 sh->samplesize = (metadata->data.stream_info.bits_per_sample<=8)?1:2; | |
226 /* FIXME: need to support dithering to samplesize 4 */ | |
11701 | 227 sh->sample_format=(sh->samplesize==1)?AFMT_U8: |
228 #ifdef WORDS_BIGENDIAN | |
229 AFMT_S16_BE; | |
230 #else | |
231 AFMT_S16_LE; | |
232 #endif | |
11004 | 233 sh->o_bps = sh->samplesize * metadata->data.stream_info.channels * metadata->data.stream_info.sample_rate; |
234 sh->i_bps = metadata->data.stream_info.bits_per_sample * metadata->data.stream_info.channels * metadata->data.stream_info.sample_rate / 8 / 2; | |
235 // input data rate (compressed bytes per second) | |
236 // Compression rate is near 0.5 | |
237 mp_msg(MSGT_DECAUDIO, MSGL_V, "total_samples: %llu\n", metadata->data.stream_info.total_samples); | |
238 mp_msg(MSGT_DECAUDIO, MSGL_V, "md5sum: "); | |
239 for (i = 0; i < 16; i++) | |
240 mp_msg(MSGT_DECAUDIO, MSGL_V, "%02hhx", metadata->data.stream_info.md5sum[i]); | |
241 mp_msg(MSGT_DECAUDIO, MSGL_V, "\n"); | |
242 | |
243 break; | |
244 case FLAC__METADATA_TYPE_PADDING: | |
245 mp_msg(MSGT_DECAUDIO, MSGL_V, "PADDING block (%u bytes)\n", metadata->length); | |
246 break; | |
247 case FLAC__METADATA_TYPE_APPLICATION: | |
248 mp_msg(MSGT_DECAUDIO, MSGL_V, "APPLICATION block (%u bytes):\n", metadata->length); | |
249 mp_msg(MSGT_DECAUDIO, MSGL_V, "Application id: 0x"); | |
250 for (i = 0; i < 4; i++) | |
251 mp_msg(MSGT_DECAUDIO, MSGL_V, "%02hhx", metadata->data.application.id[i]); | |
252 mp_msg(MSGT_DECAUDIO, MSGL_V, "\nData: \n"); | |
253 for (i = 0; i < (metadata->length-4)/8; i++) | |
254 { | |
255 for(j = 0; j < 8; j++) | |
256 mp_msg(MSGT_DECAUDIO, MSGL_V, "%c", (unsigned char)metadata->data.application.data[i*8+j]<0x20?'.':metadata->data.application.data[i*8+j]); | |
257 mp_msg(MSGT_DECAUDIO, MSGL_V, " | "); | |
258 for(j = 0; j < 8; j++) | |
259 mp_msg(MSGT_DECAUDIO, MSGL_V, "%#02hhx ", metadata->data.application.data[i*8+j]); | |
260 mp_msg(MSGT_DECAUDIO, MSGL_V, "\n"); | |
261 } | |
262 if (metadata->length-4-i*8 != 0) | |
263 { | |
264 for(j = 0; j < metadata->length-4-i*8; j++) | |
265 mp_msg(MSGT_DECAUDIO, MSGL_V, "%c", (unsigned char)metadata->data.application.data[i*8+j]<0x20?'.':metadata->data.application.data[i*8+j]); | |
266 for(; j <8; j++) | |
267 mp_msg(MSGT_DECAUDIO, MSGL_V, " "); | |
268 mp_msg(MSGT_DECAUDIO, MSGL_V, " | "); | |
269 for(j = 0; j < metadata->length-4-i*8; j++) | |
270 mp_msg(MSGT_DECAUDIO, MSGL_V, "%#02hhx ", metadata->data.application.data[i*8+j]); | |
271 mp_msg(MSGT_DECAUDIO, MSGL_V, "\n"); | |
272 } | |
273 break; | |
274 case FLAC__METADATA_TYPE_SEEKTABLE: | |
275 mp_msg(MSGT_DECAUDIO, MSGL_V, "SEEKTABLE block (%u bytes):\n", metadata->length); | |
276 mp_msg(MSGT_DECAUDIO, MSGL_V, "%d seekpoints:\n", metadata->data.seek_table.num_points); | |
277 for (i = 0; i < metadata->data.seek_table.num_points; i++) | |
278 if (metadata->data.seek_table.points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER) | |
279 mp_msg(MSGT_DECAUDIO, MSGL_V, " %3d) sample_number=%llu stream_offset=%llu frame_samples=%u\n", i, | |
280 metadata->data.seek_table.points[i].sample_number, | |
281 metadata->data.seek_table.points[i].stream_offset, | |
282 metadata->data.seek_table.points[i].frame_samples); | |
283 else | |
284 mp_msg(MSGT_DECAUDIO, MSGL_V, " %3d) PLACEHOLDER\n", i); | |
285 break; | |
286 case FLAC__METADATA_TYPE_VORBIS_COMMENT: | |
287 mp_msg(MSGT_DECAUDIO, MSGL_V, "VORBISCOMMENT block (%u bytes):\n", metadata->length); | |
288 { | |
289 char entry[metadata->data.vorbis_comment.vendor_string.length+1]; | |
290 memcpy(&entry, metadata->data.vorbis_comment.vendor_string.entry, metadata->data.vorbis_comment.vendor_string.length); | |
291 entry[metadata->data.vorbis_comment.vendor_string.length] = '\0'; | |
292 mp_msg(MSGT_DECAUDIO, MSGL_V, "vendor_string: %s\n", entry); | |
293 } | |
294 mp_msg(MSGT_DECAUDIO, MSGL_V, "%d comment(s):\n", metadata->data.vorbis_comment.num_comments); | |
295 for (i = 0; i < metadata->data.vorbis_comment.num_comments; i++) | |
296 { | |
297 char entry[metadata->data.vorbis_comment.comments[i].length]; | |
298 memcpy(&entry, metadata->data.vorbis_comment.comments[i].entry, metadata->data.vorbis_comment.comments[i].length); | |
299 entry[metadata->data.vorbis_comment.comments[i].length] = '\0'; | |
300 mp_msg(MSGT_DECAUDIO, MSGL_V, "%s\n", entry); | |
301 } | |
302 { | |
303 double gain, peak; | |
304 if(grabbag__replaygain_load_from_vorbiscomment(metadata, album_mode, &gain, &peak)) | |
305 { | |
306 ((flac_struct_t*)client_data)->has_replaygain = true; | |
307 ((flac_struct_t*)client_data)->replay_scale = grabbag__replaygain_compute_scale_factor(peak, gain, (double)preamp, /*prevent_clipping=*/!hard_limit); | |
308 mp_msg(MSGT_DECAUDIO, MSGL_V, "calculated replay_scale: %lf\n", ((flac_struct_t*)client_data)->replay_scale); | |
309 } | |
310 } | |
311 break; | |
312 case FLAC__METADATA_TYPE_CUESHEET: | |
313 mp_msg(MSGT_DECAUDIO, MSGL_V, "CUESHEET block (%u bytes):\n", metadata->length); | |
314 mp_msg(MSGT_DECAUDIO, MSGL_V, "mcn: '%s'\n", metadata->data.cue_sheet.media_catalog_number); | |
315 mp_msg(MSGT_DECAUDIO, MSGL_V, "lead_in: %llu\n", metadata->data.cue_sheet.lead_in); | |
316 mp_msg(MSGT_DECAUDIO, MSGL_V, "is_cd: %s\n", metadata->data.cue_sheet.is_cd?"true":"false"); | |
317 mp_msg(MSGT_DECAUDIO, MSGL_V, "num_tracks: %u\n", metadata->data.cue_sheet.num_tracks); | |
318 for (i = 0; i < metadata->data.cue_sheet.num_tracks; i++) | |
319 { | |
320 mp_msg(MSGT_DECAUDIO, MSGL_V, "track[%d]:\n", i); | |
321 mp_msg(MSGT_DECAUDIO, MSGL_V, "offset: %llu\n", metadata->data.cue_sheet.tracks[i].offset); | |
322 mp_msg(MSGT_DECAUDIO, MSGL_V, "number: %hhu%s\n", metadata->data.cue_sheet.tracks[i].number, metadata->data.cue_sheet.tracks[i].number==170?"(LEAD-OUT)":""); | |
323 mp_msg(MSGT_DECAUDIO, MSGL_V, "isrc: '%s'\n", metadata->data.cue_sheet.tracks[i].isrc); | |
324 mp_msg(MSGT_DECAUDIO, MSGL_V, "type: %s\n", metadata->data.cue_sheet.tracks[i].type?"non-audio":"audio"); | |
325 mp_msg(MSGT_DECAUDIO, MSGL_V, "pre_emphasis: %s\n", metadata->data.cue_sheet.tracks[i].pre_emphasis?"true":"false"); | |
326 mp_msg(MSGT_DECAUDIO, MSGL_V, "num_indices: %hhu\n", metadata->data.cue_sheet.tracks[i].num_indices); | |
327 for (j = 0; j < metadata->data.cue_sheet.tracks[i].num_indices; j++) | |
328 { | |
329 mp_msg(MSGT_DECAUDIO, MSGL_V, "index[%d]:\n", j); | |
330 mp_msg(MSGT_DECAUDIO, MSGL_V, "offset:%llu\n", metadata->data.cue_sheet.tracks[i].indices[j].offset); | |
331 mp_msg(MSGT_DECAUDIO, MSGL_V, "number:%hhu\n", metadata->data.cue_sheet.tracks[i].indices[j].number); | |
332 } | |
333 } | |
334 break; | |
335 default: if (metadata->type >= FLAC__METADATA_TYPE_UNDEFINED) | |
336 mp_msg(MSGT_DECAUDIO, MSGL_V, "UNKNOWN block (%u bytes):\n", metadata->length); | |
337 else | |
338 mp_msg(MSGT_DECAUDIO, MSGL_V, "Strange block: UNKNOWN #%d < FLAC__METADATA_TYPE_UNDEFINED (%u bytes):\n", metadata->type, metadata->length); | |
339 for (i = 0; i < (metadata->length)/8; i++) | |
340 { | |
341 for(j = 0; j < 8; j++) | |
342 mp_msg(MSGT_DECAUDIO, MSGL_V, "%c", (unsigned char)metadata->data.unknown.data[i*8+j]<0x20?'.':metadata->data.unknown.data[i*8+j]); | |
343 mp_msg(MSGT_DECAUDIO, MSGL_V, " | "); | |
344 for(j = 0; j < 8; j++) | |
345 mp_msg(MSGT_DECAUDIO, MSGL_V, "%#02hhx ", metadata->data.unknown.data[i*8+j]); | |
346 mp_msg(MSGT_DECAUDIO, MSGL_V, "\n"); | |
347 } | |
348 if (metadata->length-i*8 != 0) | |
349 { | |
350 for(j = 0; j < metadata->length-i*8; j++) | |
351 mp_msg(MSGT_DECAUDIO, MSGL_V, "%c", (unsigned char)metadata->data.unknown.data[i*8+j]<0x20?'.':metadata->data.unknown.data[i*8+j]); | |
352 for(; j <8; j++) | |
353 mp_msg(MSGT_DECAUDIO, MSGL_V, " "); | |
354 mp_msg(MSGT_DECAUDIO, MSGL_V, " | "); | |
355 for(j = 0; j < metadata->length-i*8; j++) | |
356 mp_msg(MSGT_DECAUDIO, MSGL_V, "%#02hhx ", metadata->data.unknown.data[i*8+j]); | |
357 mp_msg(MSGT_DECAUDIO, MSGL_V, "\n"); | |
358 } | |
359 break; | |
360 } | |
361 } | |
362 | |
363 void flac_error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) | |
364 { | |
365 if (status != FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC) | |
366 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "\nError callback called (%s)!!!\n", FLAC__StreamDecoderErrorStatusString[status]); | |
367 } | |
368 | |
369 static int preinit(sh_audio_t *sh){ | |
370 // there are default values set for buffering, but you can override them: | |
371 | |
372 sh->audio_out_minsize=8*4*65535; // due to specs: we assume max 8 channels, | |
373 // 4 bytes/sample and 65535 samples/frame | |
374 // So allocating 2Mbytes buffer :) | |
375 | |
376 // minimum input buffer size (set only if you need input buffering) | |
377 // (should be the max compressed frame size) | |
378 sh->audio_in_minsize=2048; // Default: 0 (no input buffer) | |
379 | |
380 // if you set audio_in_minsize non-zero, the buffer will be allocated | |
381 // before the init() call by the core, and you can access it via | |
382 // pointer: sh->audio_in_buffer | |
383 // it will free'd after uninit(), so you don't have to use malloc/free here! | |
384 | |
385 return 1; // return values: 1=OK 0=ERROR | |
386 } | |
387 | |
388 static int init(sh_audio_t *sh_audio){ | |
389 flac_struct_t *context = (flac_struct_t*)calloc(sizeof(flac_struct_t), 1); | |
390 | |
391 sh_audio->context = context; | |
392 context->sh = sh_audio; | |
393 if (context == NULL) | |
394 { | |
395 mp_msg(MSGT_DECAUDIO, MSGL_FATAL, "flac_init: error allocating context.\n"); | |
396 return 0; | |
397 } | |
398 | |
399 context->flac_dec = FLAC__stream_decoder_new(); | |
400 if (context->flac_dec == NULL) | |
401 { | |
402 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "flac_init: error allocaing FLAC decoder.\n"); | |
403 return 0; | |
404 } | |
405 | |
406 if (!FLAC__stream_decoder_set_client_data(context->flac_dec, context)) | |
407 { | |
408 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "error setting private data for callbacks.\n"); | |
409 return 0; | |
410 } | |
411 | |
412 if (!FLAC__stream_decoder_set_read_callback(context->flac_dec, &flac_read_callback)) | |
413 { | |
414 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "error setting read callback.\n"); | |
415 return 0; | |
416 } | |
417 | |
418 if (!FLAC__stream_decoder_set_write_callback(context->flac_dec, &flac_write_callback)) | |
419 { | |
420 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "error setting write callback.\n"); | |
421 return 0; | |
422 } | |
423 | |
424 if (!FLAC__stream_decoder_set_metadata_callback(context->flac_dec, &flac_metadata_callback)) | |
425 { | |
426 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "error setting metadata callback.\n"); | |
427 return 0; | |
428 } | |
429 | |
430 if (!FLAC__stream_decoder_set_error_callback(context->flac_dec, &flac_error_callback)) | |
431 { | |
432 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "error setting error callback.\n"); | |
433 return 0; | |
434 } | |
435 | |
436 if (!FLAC__stream_decoder_set_metadata_respond_all(context->flac_dec)) | |
437 { | |
438 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "error during setting metadata_respond_all.\n"); | |
439 return 0; | |
440 } | |
441 | |
442 if (FLAC__stream_decoder_init(context->flac_dec) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA) | |
443 { | |
444 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "Error initializing decoder!\n"); | |
445 return 0; | |
446 } | |
447 | |
448 context->buf = NULL; | |
449 context->minlen = context->maxlen = 0; | |
450 context->replay_scale = 1.0; | |
451 | |
452 FLAC__stream_decoder_process_until_end_of_metadata(context->flac_dec); | |
453 | |
11701 | 454 FLAC__replaygain_synthesis__init_dither_context(&(context->dither_context), sh_audio->samplesize * 8, noise_shaping); |
11004 | 455 |
456 return 1; // return values: 1=OK 0=ERROR | |
457 } | |
458 | |
459 static void uninit(sh_audio_t *sh){ | |
460 // uninit the decoder etc... | |
461 FLAC__stream_decoder_finish(((flac_struct_t*)(sh->context))->flac_dec); | |
462 FLAC__stream_decoder_delete(((flac_struct_t*)(sh->context))->flac_dec); | |
463 // again: you don't have to free() a_in_buffer here! it's done by the core. | |
464 } | |
465 | |
466 static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen){ | |
467 FLAC__StreamDecoderState decstate; | |
468 FLAC__bool status; | |
469 | |
470 // audio decoding. the most important thing :) | |
471 // parameters you get: | |
472 // buf = pointer to the output buffer, you have to store uncompressed | |
473 // samples there | |
474 // minlen = requested minimum size (in bytes!) of output. it's just a | |
475 // _recommendation_, you can decode more or less, it just tell you that | |
476 // the caller process needs 'minlen' bytes. if it gets less, it will | |
477 // call decode_audio() again. | |
478 // maxlen = maximum size (bytes) of output. you MUST NOT write more to the | |
479 // buffer, it's the upper-most limit! | |
480 // note: maxlen will be always greater or equal to sh->audio_out_minsize | |
481 | |
482 // Store params in private context for callback: | |
483 ((flac_struct_t*)(sh_audio->context))->buf = buf; | |
484 ((flac_struct_t*)(sh_audio->context))->minlen = minlen; | |
485 ((flac_struct_t*)(sh_audio->context))->maxlen = maxlen; | |
486 ((flac_struct_t*)(sh_audio->context))->written = 0; | |
487 | |
488 status = FLAC__stream_decoder_process_single(((flac_struct_t*)(sh_audio->context))->flac_dec); | |
489 decstate = FLAC__stream_decoder_get_state(((flac_struct_t*)(sh_audio->context))->flac_dec); | |
490 if (!status || ( | |
491 decstate != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA && | |
492 decstate != FLAC__STREAM_DECODER_READ_METADATA && | |
493 decstate != FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC && | |
494 decstate != FLAC__STREAM_DECODER_READ_FRAME | |
495 )) | |
496 { | |
497 if (decstate == FLAC__STREAM_DECODER_END_OF_STREAM) | |
498 { | |
499 /* return what we have decoded */ | |
500 if (((flac_struct_t*)(sh_audio->context))->written != 0) | |
501 return ((flac_struct_t*)(sh_audio->context))->written; | |
502 mp_msg(MSGT_DECAUDIO, MSGL_V, "End of stream.\n"); | |
503 return -1; | |
504 } | |
505 mp_msg(MSGT_DECAUDIO, MSGL_WARN, "process_single problem: returned %s, state is %s!\n", status?"true":"false", FLAC__StreamDecoderStateString[decstate]); | |
506 FLAC__stream_decoder_flush(((flac_struct_t*)(sh_audio->context))->flac_dec); | |
507 return -1; | |
508 } | |
509 | |
510 | |
511 return ((flac_struct_t*)(sh_audio->context))->written; // return value: number of _bytes_ written to output buffer, | |
512 // or -1 for EOF (or uncorrectable error) | |
513 } | |
514 | |
515 static int control(sh_audio_t *sh,int cmd,void* arg, ...){ | |
516 switch(cmd){ | |
517 case ADCTRL_RESYNC_STREAM: | |
518 // it is called once after seeking, to resync. | |
519 // Note: sh_audio->a_in_buffer_len=0; is done _before_ this call! | |
520 FLAC__stream_decoder_flush (((flac_struct_t*)(sh->context))->flac_dec); | |
521 return CONTROL_TRUE; | |
522 case ADCTRL_SKIP_FRAME: | |
523 // it is called to skip (jump over) small amount (1/10 sec or 1 frame) | |
524 // of audio data - used to sync audio to video after seeking | |
525 // if you don't return CONTROL_TRUE, it will defaults to: | |
526 // ds_fill_buffer(sh_audio->ds); // skip 1 demux packet | |
527 ((flac_struct_t*)(sh->context))->buf = NULL; | |
528 ((flac_struct_t*)(sh->context))->minlen = | |
529 ((flac_struct_t*)(sh->context))->maxlen = 0; | |
530 FLAC__stream_decoder_process_single(((flac_struct_t*)(sh->context))->flac_dec); | |
531 return CONTROL_TRUE; | |
532 } | |
533 return CONTROL_UNKNOWN; | |
534 } | |
535 #endif |