summaryrefslogtreecommitdiff
path: root/extra/timidity++/TiMidity++-2.13.2+flac-1.1.3.patch
blob: 200d011a319f10a20a3314fd4b0ce2a9106a2b87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
diff -urN TiMidity++-2.13.2.orig/configure.in TiMidity++-2.13.2/configure.in
--- TiMidity++-2.13.2.orig/configure.in	2004-10-03 15:39:52 +0300
+++ TiMidity++-2.13.2/configure.in	2007-02-19 13:06:42 +0200
@@ -1216,11 +1216,39 @@
       EXTRACT_CPPFLAGS(CPPFLAGS,CFLAGS,$LIBFLAC_CFLAGS)
       LIBS="$LIBS $LIBFLAC_LIBS"
     ])
-    AM_PATH_LIBOGGFLAC([
-      EXTRADEFS="$EXTRADEFS -DAU_OGGFLAC"
-      EXTRACT_CPPFLAGS(CPPFLAGS,CFLAGS,$LIBOGGFLAC_CFLAGS)
-      LIBS="$LIBS $LIBOGGFLAC_LIBS"
-    ])
+    # in FLAC-1.1.3, libOggFLAC was merged into libFLAC
+    # FLAC_API_VERSION_CURRENT first appears in FLAC-1.1.3
+    AC_MSG_CHECKING(for legacy libFLAC (<1.1.13))
+    ac_save_LIBS="$LIBS"
+    LIBS=
+    AC_TRY_RUN([
+    #include <FLAC/export.h>
+    int main () { return FLAC_API_VERSION_CURRENT<8; }
+    ],legacy_flac=no,legacy_flac=yes,legacy_flac=no)
+    AC_MSG_RESULT($legacy_flac)
+    LIBS="$ac_save_LIBS"
+    if test "x$legacy_flac" = xyes; then
+      AM_PATH_LIBOGGFLAC([
+        EXTRADEFS="$EXTRADEFS -DAU_OGGFLAC"
+        EXTRACT_CPPFLAGS(CPPFLAGS,CFLAGS,$LIBOGGFLAC_CFLAGS)
+        LIBS="$LIBS $LIBOGGFLAC_LIBS"
+      ])
+    else
+      
+      AC_MSG_CHECKING(for Ogg support in libFLAC )
+      ac_save_LIBS="$LIBS"
+      LIBS="-lFLAC -lm -logg"
+      AC_TRY_RUN([
+      #include <FLAC/export.h>
+      int main () { return !FLAC_API_SUPPORTS_OGG_FLAC; }
+      ],ogg_flac=yes,ogg_flac=yes,ogg_flac=no)
+      AC_MSG_RESULT($ogg_flac)
+      LIBS="$ac_save_LIBS"
+      if test "x$ogg_flac" = xyes; then    
+        LIBS="$LIBS -logg"
+        CFLAGS="$CFLAGS -DAU_OGGFLAC"
+      fi
+    fi
   else
     AC_MSG_RESULT(no)
   fi
diff -urN TiMidity++-2.13.2.orig/timidity/flac_a.c TiMidity++-2.13.2/timidity/flac_a.c
--- TiMidity++-2.13.2.orig/timidity/flac_a.c	2004-05-23 07:35:44 +0300
+++ TiMidity++-2.13.2/timidity/flac_a.c	2007-02-19 13:06:55 +0200
@@ -38,14 +38,22 @@
 
 #if defined(AU_FLAC_DLL) || defined(AU_OGGFLAC_DLL)
 #include <windows.h>
-#define FLAC__EXPORT_H  /* don't include "OggFLAC/export.h" */
-#define FLAC_API
-#define OggFLAC__EXPORT_H  /* don't include "FLAC/export.h" */
-#define OggFLAC_API
+#include <FLAC/export.h> /* need export.h to figure out API version from FLAC_API_VERSION_CURRENT */
+#undef FLAC_API
+#undef OggFLAC_API
 #endif
 
 #include <FLAC/all.h>
-#ifdef AU_OGGFLAC
+
+/* by LEGACY_FLAC we mean before FLAC 1.1.3 */
+/* in FLAC 1.1.3, libOggFLAC is merged into libFLAC and all encoding layers are merged into the stream encoder */
+#if !defined(FLAC_API_VERSION_CURRENT) || FLAC_API_VERSION_CURRENT < 8
+#define LEGACY_FLAC
+#else
+#undef LEGACY_FLAC
+#endif
+
+#if defined(LEGACY_FLAC) && defined(AU_OGGFLAC)
 #include <OggFLAC/stream_encoder.h>
 #endif
 
@@ -100,17 +108,21 @@
   unsigned long out_bytes;
   union {
     FLAC__StreamEncoderState flac;
+#ifdef LEGACY_FLAC
     FLAC__SeekableStreamEncoderState s_flac;
 #ifdef AU_OGGFLAC
     OggFLAC__StreamEncoderState ogg;
 #endif
+#endif
   } state;
   union {
     union {
       FLAC__StreamEncoder *stream;
+#ifdef LEGACY_FLAC
       FLAC__SeekableStreamEncoder *s_stream;
+#endif
     } flac;
-#ifdef AU_OGGFLAC
+#if defined(LEGACY_FLAC) && defined(AU_OGGFLAC)
     union {
       OggFLAC__StreamEncoder *stream;
     } ogg;
@@ -158,7 +170,7 @@
 static long serial_number = 0;
 FLAC_ctx *flac_ctx = NULL;
 
-#ifdef AU_OGGFLAC
+#if defined(LEGACY_FLAC) && defined(AU_OGGFLAC)
 static FLAC__StreamEncoderWriteStatus
 ogg_stream_encoder_write_callback(const OggFLAC__StreamEncoder *encoder,
 				  const FLAC__byte buffer[],
@@ -168,8 +180,13 @@
 static FLAC__StreamEncoderWriteStatus
 flac_stream_encoder_write_callback(const FLAC__StreamEncoder *encoder,
 				   const FLAC__byte buffer[],
+#ifdef LEGACY_FLAC
 				   unsigned bytes, unsigned samples,
+#else
+				   size_t bytes, unsigned samples,
+#endif
 				   unsigned current_frame, void *client_data);
+#ifdef LEGACY_FLAC
 static void flac_stream_encoder_metadata_callback(const FLAC__StreamEncoder *encoder,
 						  const FLAC__StreamMetadata *metadata,
 						  void *client_data);
@@ -181,6 +198,7 @@
 static void flac_seekable_stream_encoder_metadata_callback(const FLAC__SeekableStreamEncoder *encoder,
 						  const FLAC__StreamMetadata *metadata,
 						  void *client_data);
+#endif
 
 /* preset */
 void flac_set_compression_level(int compression_level)
@@ -281,7 +299,11 @@
 #ifdef AU_OGGFLAC
 void flac_set_option_oggflac(int isogg)
 {
+#ifdef LEGACY_FLAC
   flac_options.isogg = isogg;
+#else
+  flac_options.isogg = (FLAC_API_SUPPORTS_OGG_FLAC && isogg);
+#endif
 }
 #endif
 
@@ -295,6 +317,7 @@
   dpm.fd = -1;
 
   if (ctx != NULL) {
+#ifdef LEGACY_FLAC
 #ifdef AU_OGGFLAC
     if (flac_options.isogg) {
       if (ctx->encoder.ogg.stream) {
@@ -317,6 +340,12 @@
 	FLAC__stream_encoder_delete(ctx->encoder.flac.stream);
       }
     }
+#else
+    if (ctx->encoder.flac.stream) {
+      FLAC__stream_encoder_finish(ctx->encoder.flac.stream);
+      FLAC__stream_encoder_delete(ctx->encoder.flac.stream);
+    }
+#endif
     free(ctx);
     flac_ctx = NULL;
   }
@@ -329,6 +358,9 @@
   FLAC__StreamMetadata padding;
   FLAC__StreamMetadata *metadata[4];
   int num_metadata = 0;
+#ifndef LEGACY_FLAC
+  FLAC__StreamEncoderInitStatus init_status;
+#endif
 
   FLAC_ctx *ctx;
 
@@ -371,6 +403,7 @@
     metadata[num_metadata++] = &padding;
   }
 
+#ifdef LEGACY_FLAC
 #ifdef AU_OGGFLAC
   if (flac_options.isogg) {
     if ((ctx->encoder.ogg.stream = OggFLAC__stream_encoder_new()) == NULL) {
@@ -542,6 +575,67 @@
       return -1;
     }
   }
+#else /* !LEGACY_FLAC */
+	if ((ctx->encoder.flac.stream = FLAC__stream_encoder_new()) == NULL) {
+		ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "cannot create FLAC stream");
+		flac_session_close();
+		return -1;
+	}
+
+#ifdef AU_OGGFLAC
+  if (flac_options.isogg) {
+    /* set sequential number for serial */
+    serial_number++;
+    if (serial_number == 1) {
+      srand(time(NULL));
+      serial_number = rand();
+    }
+    FLAC__stream_encoder_set_ogg_serial_number(ctx->encoder.flac.stream, serial_number);
+  }
+#endif /* AU_OGGFLAC */
+	FLAC__stream_encoder_set_channels(ctx->encoder.flac.stream, nch);
+	/* 16bps only */
+	FLAC__stream_encoder_set_bits_per_sample(ctx->encoder.flac.stream, 16);
+
+	FLAC__stream_encoder_set_verify(ctx->encoder.flac.stream, flac_options.verify);
+
+	if (!FLAC__format_sample_rate_is_valid(dpm.rate)) {
+		ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "invalid sampling rate %d", dpm.rate);
+		flac_session_close();
+		return -1;
+	}
+	FLAC__stream_encoder_set_sample_rate(ctx->encoder.flac.stream, dpm.rate);
+
+	FLAC__stream_encoder_set_qlp_coeff_precision(ctx->encoder.flac.stream, flac_options.qlp_coeff_precision);
+	/* expensive! */
+	FLAC__stream_encoder_set_do_qlp_coeff_prec_search(ctx->encoder.flac.stream, flac_options.qlp_coeff_precision_search);
+
+	if (nch == 2) {
+		FLAC__stream_encoder_set_do_mid_side_stereo(ctx->encoder.flac.stream, flac_options.mid_side);
+		FLAC__stream_encoder_set_loose_mid_side_stereo(ctx->encoder.flac.stream, flac_options.adaptive_mid_side);
+	}
+
+	FLAC__stream_encoder_set_max_lpc_order(ctx->encoder.flac.stream, flac_options.max_lpc_order);
+	FLAC__stream_encoder_set_min_residual_partition_order(ctx->encoder.flac.stream, flac_options.min_residual_partition_order);
+	FLAC__stream_encoder_set_max_residual_partition_order(ctx->encoder.flac.stream, flac_options.max_residual_partition_order);
+
+	FLAC__stream_encoder_set_blocksize(ctx->encoder.flac.stream, flac_options.blocksize);
+
+	if (0 < num_metadata)
+		FLAC__stream_encoder_set_metadata(ctx->encoder.flac.stream, metadata, num_metadata);
+
+#ifdef AU_OGGFLAC
+  if (flac_options.isogg)
+		init_status = FLAC__stream_encoder_init_ogg_stream(ctx->encoder.flac.stream, NULL, flac_stream_encoder_write_callback, NULL, NULL, NULL, ctx);
+  else
+#endif
+	init_status = FLAC__stream_encoder_init_stream(ctx->encoder.flac.stream, flac_stream_encoder_write_callback, NULL, NULL, NULL, ctx);
+	if (init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
+		ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "cannot create FLAC encoder (init status: %s)", FLAC__StreamEncoderInitStatusString[init_status]);
+		flac_session_close();
+		return -1;
+	}
+#endif
 
   return 0;
 }
@@ -638,7 +732,7 @@
   return 0;
 }
 
-#ifdef AU_OGGFLAC
+#if defined(LEGACY_FLAC) && defined(AU_OGGFLAC)
 static FLAC__StreamEncoderWriteStatus
 ogg_stream_encoder_write_callback(const OggFLAC__StreamEncoder *encoder,
 				  const FLAC__byte buffer[],
@@ -658,7 +752,11 @@
 static FLAC__StreamEncoderWriteStatus
 flac_stream_encoder_write_callback(const FLAC__StreamEncoder *encoder,
 				   const FLAC__byte buffer[],
+#ifdef LEGACY_FLAC
 				   unsigned bytes, unsigned samples,
+#else
+				   size_t bytes, unsigned samples,
+#endif
 				   unsigned current_frame, void *client_data)
 {
   FLAC_ctx *ctx = (FLAC_ctx *)client_data;
@@ -670,6 +768,7 @@
   else
     return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
 }
+#ifdef LEGACY_FLAC
 static void flac_stream_encoder_metadata_callback(const FLAC__StreamEncoder *encoder,
 						  const FLAC__StreamMetadata *metadata,
 						  void *client_data)
@@ -695,6 +794,7 @@
 						  void *client_data)
 {
 }
+#endif
 
 static int output_data(char *buf, int32 nbytes)
 {
@@ -723,6 +823,7 @@
     oggbuf[i] = *s++;
   }
 
+#ifdef LEGACY_FLAC
 #ifdef AU_OGGFLAC
   if (flac_options.isogg) {
     ctx->state.ogg = OggFLAC__stream_encoder_get_state(ctx->encoder.ogg.stream);
@@ -793,6 +894,29 @@
       return -1;
     }
   }
+#else /* !LEGACY_FLAC */
+  ctx->state.flac = FLAC__stream_encoder_get_state(ctx->encoder.flac.stream);
+  if (ctx->state.flac != FLAC__STREAM_ENCODER_OK) {
+    if (ctx->state.flac == FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR |
+	FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA) {
+      ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "FLAC stream verify error (%s)",
+		FLAC__StreamDecoderStateString[FLAC__stream_encoder_get_verify_decoder_state(ctx->encoder.flac.stream)]);
+    }
+    else {
+      ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "cannot encode FLAC stream (%s)",
+		FLAC__StreamEncoderStateString[ctx->state.flac]);
+    }
+    flac_session_close();
+    return -1;
+  }
+
+  if (!FLAC__stream_encoder_process_interleaved(ctx->encoder.flac.stream, oggbuf,
+						nbytes / nch / 2 )) {
+    ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "cannot encode FLAC stream");
+    flac_session_close();
+    return -1;
+  }
+#endif
   ctx->in_bytes += nbytes;
 
   free(oggbuf);
@@ -813,6 +937,7 @@
     return;
   }
 
+#ifdef LEGACY_FLAC
   if (flac_options.isogg) {
 #ifdef AU_OGGFLAC
     if ((ctx->state.ogg = OggFLAC__stream_encoder_get_state(ctx->encoder.ogg.stream)) != OggFLAC__STREAM_ENCODER_OK) {
@@ -838,6 +963,13 @@
       /* fall through */
     }
   }
+#else /* !LEGACY_FLAC */
+  if ((ctx->state.flac = FLAC__stream_encoder_get_state(ctx->encoder.flac.stream)) != FLAC__STREAM_ENCODER_OK) {
+    ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "FLAC stream encoder is invalid (%s)",
+	      FLAC__StreamEncoderStateString[ctx->state.flac]);
+    /* fall through */
+  }
+#endif
 
   ctl->cmsg(CMSG_INFO, VERB_NORMAL, "Wrote %lu/%lu bytes(%g%% compressed)",
             ctx->out_bytes, ctx->in_bytes, ((double)ctx->out_bytes / (double)ctx->in_bytes) * 100.);