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