annotate libmpcodecs/ad_flac.c @ 11162:b08f2fa495a1

Punctuation fixes, better line breaks.
author diego
date Fri, 17 Oct 2003 12:12:35 +0000
parents d48eccbbb984
children 1188bf65b776
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 {
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
93 int b = demux_read_data(((flac_struct_t*)client_data)->sh->ds, buffer, *bytes);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
94 mp_msg(MSGT_DECAUDIO, MSGL_DBG2, "\nread %d bytes\n", b);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
95 *bytes = b;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
96 if (b <= 0)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
97 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
98 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
99 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
100
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
101 /*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
102 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
103 {
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
104 FLAC__byte *buf = ((flac_struct_t*)(client_data))->buf;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
105 int channel, sample;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
106 int bps = ((flac_struct_t*)(client_data))->sh->samplesize;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
107 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
108 if (buf == NULL)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
109 {
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
110 /* This is used in control for skipping 1 audio frame */
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
111 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
112 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
113 #if 0
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
114 for (sample = 0; sample < frame->header.blocksize; sample ++)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
115 for (channel = 0; channel < frame->header.channels; channel ++)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
116 switch (bps)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
117 {
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
118 case 3:
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
119 buf[bps*(sample*frame->header.channels+channel)+2] = (FLAC__byte)(buffer[channel][sample]>>16);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
120 case 2:
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
121 buf[bps*(sample*frame->header.channels+channel)+1] = (FLAC__byte)(buffer[channel][sample]>>8);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
122 buf[bps*(sample*frame->header.channels+channel)+0] = (FLAC__byte)(buffer[channel][sample]);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
123 break;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
124 case 1:
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
125 buf[bps*(sample*frame->header.channels+channel)] = buffer[channel][sample]^0x80;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
126 break;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
127 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
128 #else
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
129 FLAC__plugin_common__apply_gain(
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
130 buf,
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
131 buffer,
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
132 frame->header.blocksize,
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
133 frame->header.channels,
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
134 ((flac_struct_t*)(client_data))->bits_per_sample,
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
135 ((flac_struct_t*)(client_data))->sh->samplesize * 8,
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
136 ((flac_struct_t*)(client_data))->replay_scale,
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
137 hard_limit,
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
138 dither,
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
139 &(((flac_struct_t*)(client_data))->dither_context)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
140 );
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
141 #endif
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
142 ((flac_struct_t*)(client_data))->written += bps*frame->header.blocksize*frame->header.channels;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
143 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
144 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
145
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
146 #ifdef local_min
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
147 #undef local_min
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
148 #endif
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
149 #define local_min(a,b) ((a)<(b)?(a):(b))
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
150
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
151 static FLAC__bool parse_double_(const FLAC__StreamMetadata_VorbisComment_Entry *entry, double *val)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
152 {
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
153 char s[32], *end;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
154 const char *p, *q;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
155 double v;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
156
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
157 FLAC__ASSERT(0 != entry);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
158 FLAC__ASSERT(0 != val);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
159
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
160 p = (const char *)entry->entry;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
161 q = strchr(p, '=');
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
162 if(0 == q)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
163 return false;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
164 q++;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
165 memset(s, 0, sizeof(s)-1);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
166 strncpy(s, q, local_min(sizeof(s)-1, entry->length - (q-p)));
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
167
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
168 v = strtod(s, &end);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
169 if(end == s)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
170 return false;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
171
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
172 *val = v;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
173 return true;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
174 }
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__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
177 {
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
178 int gain_offset, peak_offset;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
179 static const FLAC__byte *tag_title_gain_ = "REPLAYGAIN_TRACK_GAIN";
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
180 static const FLAC__byte *tag_title_peak_ = "REPLAYGAIN_TRACK_PEAK";
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
181 static const FLAC__byte *tag_album_gain_ = "REPLAYGAIN_ALBUM_GAIN";
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
182 static const FLAC__byte *tag_album_peak_ = "REPLAYGAIN_ALBUM_PEAK";
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
183
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
184 FLAC__ASSERT(0 != block);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
185 FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
186
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
187 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
188 return false;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
189 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
190 return false;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
191
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
192 if(!parse_double_(block->data.vorbis_comment.comments + gain_offset, gain))
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
193 return false;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
194 if(!parse_double_(block->data.vorbis_comment.comments + peak_offset, peak))
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
195 return false;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
196
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
197 return true;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
198 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
199
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
200 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
201 {
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
202 double scale;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
203 FLAC__ASSERT(peak >= 0.0);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
204 gain += preamp;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
205 scale = (float) pow(10.0, gain * 0.05);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
206 if(prevent_clipping && peak > 0.0) {
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
207 const double max_scale = (float)(1.0 / peak);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
208 if(scale > max_scale)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
209 scale = max_scale;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
210 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
211 return scale;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
212 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
213
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
214 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
215 {
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
216 int i, j;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
217 sh_audio_t *sh = ((flac_struct_t*)client_data)->sh;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
218 mp_msg(MSGT_DECAUDIO, MSGL_DBG2, "Metadata received\n");
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
219 switch (metadata->type)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
220 {
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
221 case FLAC__METADATA_TYPE_STREAMINFO:
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
222 mp_msg(MSGT_DECAUDIO, MSGL_V, "STREAMINFO block (%u bytes):\n", metadata->length);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
223 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
224 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
225 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
226 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
227 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
228 sh->samplerate = metadata->data.stream_info.sample_rate;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
229 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
230 sh->channels = metadata->data.stream_info.channels;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
231 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
232 ((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
233 sh->samplesize = (metadata->data.stream_info.bits_per_sample<=8)?1:2;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
234 /* FIXME: need to support dithering to samplesize 4 */
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
235 sh->sample_format=(sh->samplesize==1)?AFMT_U8:AFMT_S16_LE; // sample format, see libao2/afmt.h
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
236 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
237 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
238 // input data rate (compressed bytes per second)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
239 // Compression rate is near 0.5
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
240 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
241 mp_msg(MSGT_DECAUDIO, MSGL_V, "md5sum: ");
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
242 for (i = 0; i < 16; i++)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
243 mp_msg(MSGT_DECAUDIO, MSGL_V, "%02hhx", metadata->data.stream_info.md5sum[i]);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
244 mp_msg(MSGT_DECAUDIO, MSGL_V, "\n");
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
245
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_PADDING:
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
248 mp_msg(MSGT_DECAUDIO, MSGL_V, "PADDING block (%u bytes)\n", metadata->length);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
249 break;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
250 case FLAC__METADATA_TYPE_APPLICATION:
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
251 mp_msg(MSGT_DECAUDIO, MSGL_V, "APPLICATION block (%u bytes):\n", metadata->length);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
252 mp_msg(MSGT_DECAUDIO, MSGL_V, "Application id: 0x");
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
253 for (i = 0; i < 4; i++)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
254 mp_msg(MSGT_DECAUDIO, MSGL_V, "%02hhx", metadata->data.application.id[i]);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
255 mp_msg(MSGT_DECAUDIO, MSGL_V, "\nData: \n");
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
256 for (i = 0; i < (metadata->length-4)/8; i++)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
257 {
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, "%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
260 mp_msg(MSGT_DECAUDIO, MSGL_V, " | ");
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
261 for(j = 0; j < 8; j++)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
262 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
263 mp_msg(MSGT_DECAUDIO, MSGL_V, "\n");
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
264 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
265 if (metadata->length-4-i*8 != 0)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
266 {
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
267 for(j = 0; j < metadata->length-4-i*8; j++)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
268 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
269 for(; j <8; j++)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
270 mp_msg(MSGT_DECAUDIO, MSGL_V, " ");
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
271 mp_msg(MSGT_DECAUDIO, MSGL_V, " | ");
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
272 for(j = 0; j < metadata->length-4-i*8; j++)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
273 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
274 mp_msg(MSGT_DECAUDIO, MSGL_V, "\n");
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
275 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
276 break;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
277 case FLAC__METADATA_TYPE_SEEKTABLE:
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
278 mp_msg(MSGT_DECAUDIO, MSGL_V, "SEEKTABLE block (%u bytes):\n", metadata->length);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
279 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
280 for (i = 0; i < metadata->data.seek_table.num_points; i++)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
281 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
282 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
283 metadata->data.seek_table.points[i].sample_number,
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
284 metadata->data.seek_table.points[i].stream_offset,
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
285 metadata->data.seek_table.points[i].frame_samples);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
286 else
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
287 mp_msg(MSGT_DECAUDIO, MSGL_V, " %3d) PLACEHOLDER\n", i);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
288 break;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
289 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
290 mp_msg(MSGT_DECAUDIO, MSGL_V, "VORBISCOMMENT block (%u bytes):\n", metadata->length);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
291 {
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
292 char entry[metadata->data.vorbis_comment.vendor_string.length+1];
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
293 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
294 entry[metadata->data.vorbis_comment.vendor_string.length] = '\0';
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
295 mp_msg(MSGT_DECAUDIO, MSGL_V, "vendor_string: %s\n", entry);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
296 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
297 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
298 for (i = 0; i < metadata->data.vorbis_comment.num_comments; i++)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
299 {
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
300 char entry[metadata->data.vorbis_comment.comments[i].length];
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
301 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
302 entry[metadata->data.vorbis_comment.comments[i].length] = '\0';
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
303 mp_msg(MSGT_DECAUDIO, MSGL_V, "%s\n", entry);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
304 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
305 {
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
306 double gain, peak;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
307 if(grabbag__replaygain_load_from_vorbiscomment(metadata, album_mode, &gain, &peak))
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
308 {
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
309 ((flac_struct_t*)client_data)->has_replaygain = true;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
310 ((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
311 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
312 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
313 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
314 break;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
315 case FLAC__METADATA_TYPE_CUESHEET:
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
316 mp_msg(MSGT_DECAUDIO, MSGL_V, "CUESHEET block (%u bytes):\n", metadata->length);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
317 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
318 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
319 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
320 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
321 for (i = 0; i < metadata->data.cue_sheet.num_tracks; i++)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
322 {
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
323 mp_msg(MSGT_DECAUDIO, MSGL_V, "track[%d]:\n", i);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
324 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
325 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
326 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
327 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
328 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
329 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
330 for (j = 0; j < metadata->data.cue_sheet.tracks[i].num_indices; j++)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
331 {
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
332 mp_msg(MSGT_DECAUDIO, MSGL_V, "index[%d]:\n", j);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
333 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
334 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
335 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
336 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
337 break;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
338 default: if (metadata->type >= FLAC__METADATA_TYPE_UNDEFINED)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
339 mp_msg(MSGT_DECAUDIO, MSGL_V, "UNKNOWN block (%u bytes):\n", metadata->length);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
340 else
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
341 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
342 for (i = 0; i < (metadata->length)/8; i++)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
343 {
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, "%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
346 mp_msg(MSGT_DECAUDIO, MSGL_V, " | ");
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
347 for(j = 0; j < 8; j++)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
348 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
349 mp_msg(MSGT_DECAUDIO, MSGL_V, "\n");
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
350 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
351 if (metadata->length-i*8 != 0)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
352 {
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
353 for(j = 0; j < metadata->length-i*8; j++)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
354 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
355 for(; j <8; j++)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
356 mp_msg(MSGT_DECAUDIO, MSGL_V, " ");
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
357 mp_msg(MSGT_DECAUDIO, MSGL_V, " | ");
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
358 for(j = 0; j < metadata->length-i*8; j++)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
359 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
360 mp_msg(MSGT_DECAUDIO, MSGL_V, "\n");
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
361 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
362 break;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
363 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
364 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
365
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
366 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
367 {
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
368 if (status != FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
369 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
370 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
371
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
372 static int preinit(sh_audio_t *sh){
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
373 // there are default values set for buffering, but you can override them:
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
374
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
375 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
376 // 4 bytes/sample and 65535 samples/frame
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
377 // So allocating 2Mbytes buffer :)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
378
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
379 // minimum input buffer size (set only if you need input buffering)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
380 // (should be the max compressed frame size)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
381 sh->audio_in_minsize=2048; // Default: 0 (no input buffer)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
382
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
383 // if you set audio_in_minsize non-zero, the buffer will be allocated
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
384 // before the init() call by the core, and you can access it via
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
385 // pointer: sh->audio_in_buffer
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
386 // 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
387
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
388 return 1; // return values: 1=OK 0=ERROR
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
389 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
390
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
391 static int init(sh_audio_t *sh_audio){
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
392 flac_struct_t *context = (flac_struct_t*)calloc(sizeof(flac_struct_t), 1);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
393
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
394 sh_audio->context = context;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
395 context->sh = sh_audio;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
396 if (context == NULL)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
397 {
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
398 mp_msg(MSGT_DECAUDIO, MSGL_FATAL, "flac_init: error allocating context.\n");
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
399 return 0;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
400 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
401
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
402 context->flac_dec = FLAC__stream_decoder_new();
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
403 if (context->flac_dec == NULL)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
404 {
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
405 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "flac_init: error allocaing FLAC decoder.\n");
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
406 return 0;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
407 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
408
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
409 if (!FLAC__stream_decoder_set_client_data(context->flac_dec, context))
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
410 {
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
411 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "error setting private data for callbacks.\n");
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
412 return 0;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
413 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
414
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
415 if (!FLAC__stream_decoder_set_read_callback(context->flac_dec, &flac_read_callback))
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
416 {
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
417 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "error setting read callback.\n");
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
418 return 0;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
419 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
420
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
421 if (!FLAC__stream_decoder_set_write_callback(context->flac_dec, &flac_write_callback))
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
422 {
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
423 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "error setting write callback.\n");
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
424 return 0;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
425 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
426
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
427 if (!FLAC__stream_decoder_set_metadata_callback(context->flac_dec, &flac_metadata_callback))
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
428 {
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
429 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "error setting metadata callback.\n");
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
430 return 0;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
431 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
432
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
433 if (!FLAC__stream_decoder_set_error_callback(context->flac_dec, &flac_error_callback))
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
434 {
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
435 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "error setting error callback.\n");
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
436 return 0;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
437 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
438
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
439 if (!FLAC__stream_decoder_set_metadata_respond_all(context->flac_dec))
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
440 {
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
441 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "error during setting metadata_respond_all.\n");
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
442 return 0;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
443 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
444
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
445 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
446 {
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
447 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "Error initializing decoder!\n");
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
448 return 0;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
449 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
450
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
451 context->buf = NULL;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
452 context->minlen = context->maxlen = 0;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
453 context->replay_scale = 1.0;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
454
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
455 FLAC__stream_decoder_process_until_end_of_metadata(context->flac_dec);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
456
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
457 FLAC__plugin_common__init_dither_context(&(context->dither_context), sh_audio->samplesize * 8, noise_shaping);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
458
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
459 return 1; // return values: 1=OK 0=ERROR
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
460 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
461
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
462 static void uninit(sh_audio_t *sh){
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
463 // uninit the decoder etc...
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
464 FLAC__stream_decoder_finish(((flac_struct_t*)(sh->context))->flac_dec);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
465 FLAC__stream_decoder_delete(((flac_struct_t*)(sh->context))->flac_dec);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
466 // 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
467 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
468
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
469 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
470 FLAC__StreamDecoderState decstate;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
471 FLAC__bool status;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
472
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
473 // audio decoding. the most important thing :)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
474 // parameters you get:
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
475 // buf = pointer to the output buffer, you have to store uncompressed
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
476 // samples there
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
477 // minlen = requested minimum size (in bytes!) of output. it's just a
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
478 // _recommendation_, you can decode more or less, it just tell you that
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
479 // the caller process needs 'minlen' bytes. if it gets less, it will
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
480 // call decode_audio() again.
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
481 // maxlen = maximum size (bytes) of output. you MUST NOT write more to the
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
482 // buffer, it's the upper-most limit!
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
483 // note: maxlen will be always greater or equal to sh->audio_out_minsize
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
484
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
485 // Store params in private context for callback:
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
486 ((flac_struct_t*)(sh_audio->context))->buf = buf;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
487 ((flac_struct_t*)(sh_audio->context))->minlen = minlen;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
488 ((flac_struct_t*)(sh_audio->context))->maxlen = maxlen;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
489 ((flac_struct_t*)(sh_audio->context))->written = 0;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
490
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
491 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
492 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
493 if (!status || (
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
494 decstate != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA &&
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
495 decstate != FLAC__STREAM_DECODER_READ_METADATA &&
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
496 decstate != FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC &&
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
497 decstate != FLAC__STREAM_DECODER_READ_FRAME
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
498 ))
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
499 {
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
500 if (decstate == FLAC__STREAM_DECODER_END_OF_STREAM)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
501 {
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
502 /* return what we have decoded */
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
503 if (((flac_struct_t*)(sh_audio->context))->written != 0)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
504 return ((flac_struct_t*)(sh_audio->context))->written;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
505 mp_msg(MSGT_DECAUDIO, MSGL_V, "End of stream.\n");
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
506 return -1;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
507 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
508 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
509 FLAC__stream_decoder_flush(((flac_struct_t*)(sh_audio->context))->flac_dec);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
510 return -1;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
511 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
512
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
513
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
514 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
515 // or -1 for EOF (or uncorrectable error)
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
516 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
517
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
518 static int control(sh_audio_t *sh,int cmd,void* arg, ...){
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
519 switch(cmd){
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
520 case ADCTRL_RESYNC_STREAM:
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
521 // it is called once after seeking, to resync.
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
522 // Note: sh_audio->a_in_buffer_len=0; is done _before_ this call!
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
523 FLAC__stream_decoder_flush (((flac_struct_t*)(sh->context))->flac_dec);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
524 return CONTROL_TRUE;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
525 case ADCTRL_SKIP_FRAME:
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
526 // 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
527 // of audio data - used to sync audio to video after seeking
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
528 // if you don't return CONTROL_TRUE, it will defaults to:
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
529 // ds_fill_buffer(sh_audio->ds); // skip 1 demux packet
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
530 ((flac_struct_t*)(sh->context))->buf = NULL;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
531 ((flac_struct_t*)(sh->context))->minlen =
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
532 ((flac_struct_t*)(sh->context))->maxlen = 0;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
533 FLAC__stream_decoder_process_single(((flac_struct_t*)(sh->context))->flac_dec);
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
534 return CONTROL_TRUE;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
535 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
536 return CONTROL_UNKNOWN;
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
537 }
d48eccbbb984 FLAC decoding support via imported libmpflac.
lumag
parents:
diff changeset
538 #endif