[go: up one dir, main page]

File: flac_a.c

package info (click to toggle)
timidity 2.13.2-40.1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 13,132 kB
  • sloc: ansic: 158,296; sh: 3,778; makefile: 1,156; tcl: 1,048; lisp: 499; perl: 285; ruby: 126
file content (815 lines) | stat: -rw-r--r-- 24,799 bytes parent folder | download | duplicates (4)
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
/*
    TiMidity++ -- MIDI to WAVE converter and player
    Copyright (C) 1999-2002 Masanao Izumo <mo@goice.co.jp>
    Copyright (C) 1995 Tuukka Toivonen <tt@cgs.fi>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    flac_a.c
	Written by Iwata <b6330015@kit.jp>
    Functions to output FLAC / OggFLAC  (*.flac, *.ogg).
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include <stdio.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#include <fcntl.h>

#ifdef __W32__
#include <io.h>
#include <time.h>
#endif

#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
#endif

#include <FLAC/all.h>

#ifdef AU_FLAC_DLL
#include "w32_libFLAC_dll_g.h"
#endif
#ifdef AU_OGGFLAC_DLL
#include "w32_libOGGFLAC_dll_g.h"
#endif

#include <time.h>

#include "timidity.h"
#include "common.h"
#include "output.h"
#include "controls.h"
#include "timer.h"
#include "instrum.h"
#include "playmidi.h"
#include "readmidi.h"
#include "miditrace.h"

static int open_output(void); /* 0=success, 1=warning, -1=fatal error */
static void close_output(void);
static int output_data(char *buf, int32 nbytes);
static int acntl(int request, void *arg);

/* export the playback mode */

#define dpm flac_play_mode

PlayMode dpm = {
  DEFAULT_RATE, PE_SIGNED|PE_16BIT, PF_PCM_STREAM,
  -1,
  {0}, /* default: get all the buffer fragments you can */
  "FLAC / OggFLAC", 'F',
  NULL,
  open_output,
  close_output,
  output_data,
  acntl
};

#ifdef __W32G__
extern char *w32g_output_dir;
extern int w32g_auto_output_mode;
#endif

typedef struct {
  unsigned long in_bytes;
  unsigned long out_bytes;
  union {
    FLAC__StreamEncoderState flac;
    FLAC__StreamEncoderState s_flac;
    FLAC__StreamEncoderState ogg;
  } state;
  union {
    union {
      FLAC__StreamEncoder *stream;
      FLAC__StreamEncoder *s_stream;
    } flac;
    union {
      FLAC__StreamEncoder *stream;
    } ogg;
  } encoder;
} FLAC_ctx;

typedef struct {
  int isogg;
  int verify;
  int padding;
  int blocksize;
  int mid_side;
  int adaptive_mid_side;
  int exhaustive_model_search;
  int max_lpc_order;
  int qlp_coeff_precision_search;
  int qlp_coeff_precision;
  int min_residual_partition_order;
  int max_residual_partition_order;
	int seekable;
} FLAC_options;

/* default compress level is 5 */
FLAC_options flac_options = {
  0,    /* isogg */
  0,    /* verify */
  4096, /* padding */
  4608, /* blocksize */
  1,    /* mid_side */
  0,    /* adaptive_mid_side */
  0,    /* exhaustive-model-search */
  8,    /* max_lpc_order */
  0,    /* qlp_coeff_precision_search */
  0,    /* qlp_coeff_precision */
  3,    /* min_residual_partition_order */
  3,     /* max_residual_partition_order */
	0,		/* seekable */
};

static long serial_number = 0;
FLAC_ctx *flac_ctx = NULL;

static FLAC__StreamEncoderWriteStatus
ogg_stream_encoder_write_callback(const FLAC__StreamEncoder *encoder,
				  const FLAC__byte buffer[],
				  size_t bytes, unsigned samples,
				  unsigned current_frame, void *client_data);
static FLAC__StreamEncoderWriteStatus
flac_stream_encoder_write_callback(const FLAC__StreamEncoder *encoder,
				   const FLAC__byte buffer[],
				   size_t bytes, unsigned samples,
				   unsigned current_frame, void *client_data);
static void flac_stream_encoder_metadata_callback(const FLAC__StreamEncoder *encoder,
						  const FLAC__StreamMetadata *metadata,
						  void *client_data);
static FLAC__StreamEncoderWriteStatus
flac_stream_encoder_write_callback(const FLAC__StreamEncoder *encoder,
				   const FLAC__byte buffer[],
				   size_t bytes, unsigned samples,
				   unsigned current_frame, void *client_data);

/* preset */
void flac_set_compression_level(int compression_level)
{
  switch (compression_level) {
  case 0:
    flac_options.max_lpc_order = 0;
    flac_options.blocksize = 1152;
    flac_options.mid_side = 0;
    flac_options.adaptive_mid_side = 0;
    flac_options.min_residual_partition_order = 2;
    flac_options.max_residual_partition_order = 2;
    flac_options.exhaustive_model_search = 0;
    break;
  case 1:
    flac_options.max_lpc_order = 0;
    flac_options.blocksize = 1152;
    flac_options.mid_side = 0;
    flac_options.adaptive_mid_side = 1;
    flac_options.min_residual_partition_order = 2;
    flac_options.max_residual_partition_order = 2;
    flac_options.exhaustive_model_search = 0;
    break;
  case 2:
    flac_options.max_lpc_order = 0;
    flac_options.blocksize = 1152;
    flac_options.mid_side = 1;
    flac_options.adaptive_mid_side = 0;
    flac_options.min_residual_partition_order = 0;
    flac_options.max_residual_partition_order = 3;
    flac_options.exhaustive_model_search = 0;
    break;
  case 3:
    flac_options.max_lpc_order = 6;
    flac_options.blocksize = 4608;
    flac_options.mid_side = 0;
    flac_options.adaptive_mid_side = 0;
    flac_options.min_residual_partition_order = 3;
    flac_options.max_residual_partition_order = 3;
    flac_options.exhaustive_model_search = 0;
    break;
  case 4:
    flac_options.max_lpc_order = 8;
    flac_options.blocksize = 4608;
    flac_options.mid_side = 0;
    flac_options.adaptive_mid_side = 1;
    flac_options.min_residual_partition_order = 3;
    flac_options.max_residual_partition_order = 3;
    flac_options.exhaustive_model_search = 0;
    break;
  case 6:
    flac_options.max_lpc_order = 8;
    flac_options.blocksize = 4608;
    flac_options.mid_side = 1;
    flac_options.adaptive_mid_side = 0;
    flac_options.min_residual_partition_order = 0;
    flac_options.max_residual_partition_order = 4;
    flac_options.exhaustive_model_search = 0;
    break;
  case 7:
    flac_options.max_lpc_order = 8;
    flac_options.blocksize = 4608;
    flac_options.mid_side = 1;
    flac_options.adaptive_mid_side = 0;
    flac_options.min_residual_partition_order = 0;
    flac_options.max_residual_partition_order = 6;
    flac_options.exhaustive_model_search = 1;
    break;
  case 8:
    flac_options.max_lpc_order = 12;
    flac_options.blocksize = 4608;
    flac_options.mid_side = 1;
    flac_options.adaptive_mid_side = 0;
    flac_options.min_residual_partition_order = 0;
    flac_options.max_residual_partition_order = 6;
    flac_options.exhaustive_model_search = 1;
    break;
  case 5:
  default:
    flac_options.max_lpc_order = 8;
    flac_options.blocksize = 4608;
    flac_options.mid_side = 1;
    flac_options.adaptive_mid_side = 0;
    flac_options.min_residual_partition_order = 3;
    flac_options.max_residual_partition_order = 3;
    flac_options.exhaustive_model_search = 0;
  }
}

void flac_set_option_padding(int padding)
{
  flac_options.padding = padding;
}
void flac_set_option_verify(int verify)
{
  flac_options.verify = verify;
}
void flac_set_option_oggflac(int isogg)
{
  flac_options.isogg = isogg;
}

static int flac_session_close()
{
  FLAC_ctx *ctx = flac_ctx;

  if (dpm.fd > 0) {
    close(dpm.fd);
  }
  dpm.fd = -1;

  if (ctx != NULL) {
    if (flac_options.isogg) {
      if (ctx->encoder.ogg.stream) {
	FLAC__stream_encoder_finish(ctx->encoder.ogg.stream);
	FLAC__stream_encoder_delete(ctx->encoder.ogg.stream);
      }
    }
    else
    if (flac_options.seekable) {
      if (ctx->encoder.flac.s_stream) {
	FLAC__stream_encoder_finish(ctx->encoder.flac.s_stream);
	FLAC__stream_encoder_delete(ctx->encoder.flac.s_stream);
      }
    }
    else
    {
      if (ctx->encoder.flac.stream) {
	FLAC__stream_encoder_finish(ctx->encoder.flac.stream);
	FLAC__stream_encoder_delete(ctx->encoder.flac.stream);
      }
    }
    free(ctx);
    flac_ctx = NULL;
  }
  return 0;
}

static int flac_output_open(const char *fname, const char *comment)
{
  int fd;
  int nch;
  FLAC__StreamMetadata padding;
  FLAC__StreamMetadata *metadata[4];
  int num_metadata = 0;

  FLAC_ctx *ctx;

  if (flac_ctx == NULL)
    flac_session_close();

  if (!(flac_ctx = (FLAC_ctx *)calloc(sizeof(FLAC_ctx), 1))) {
    ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s", strerror(errno));
    return -1;
  }

  ctx = flac_ctx;

  ctx->in_bytes = ctx->out_bytes = 0;

  if(strcmp(fname, "-") == 0) {
    fd = 1; /* data to stdout */
    if (comment == NULL)
      comment = "(stdout)";
  }
  else {
    /* Open the audio file */
    fd = open(fname, FILE_OUTPUT_MODE);
    if(fd < 0) {
      ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: %s",
		fname, strerror(errno));
      return -1;
    }
    if(comment == NULL)
      comment = fname;
  }

  dpm.fd = fd;
  nch = (dpm.encoding & PE_MONO) ? 1 : 2;

  if (0 < flac_options.padding) {
    padding.is_last = 0;
    padding.type = FLAC__METADATA_TYPE_PADDING;
    padding.length = flac_options.padding;
    metadata[num_metadata++] = &padding;
  }

  if (flac_options.isogg) {
    if ((ctx->encoder.ogg.stream = FLAC__stream_encoder_new()) == NULL) {
      ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "cannot create OggFLAC stream");
      flac_session_close();
      return -1;
    }

    FLAC__stream_encoder_set_channels(ctx->encoder.ogg.stream, nch);
    /* 16bps only */
    FLAC__stream_encoder_set_bits_per_sample(ctx->encoder.ogg.stream, 16);

    /* 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.ogg.stream, serial_number);

    FLAC__stream_encoder_set_verify(ctx->encoder.ogg.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.ogg.stream, dpm.rate);

    FLAC__stream_encoder_set_qlp_coeff_precision(ctx->encoder.ogg.stream, flac_options.qlp_coeff_precision);
    /* expensive! */
    FLAC__stream_encoder_set_do_qlp_coeff_prec_search(ctx->encoder.ogg.stream, flac_options.qlp_coeff_precision_search);

    if (nch == 2) {
      FLAC__stream_encoder_set_do_mid_side_stereo(ctx->encoder.ogg.stream, flac_options.mid_side);
      FLAC__stream_encoder_set_loose_mid_side_stereo(ctx->encoder.ogg.stream, flac_options.adaptive_mid_side);
    }

    FLAC__stream_encoder_set_max_lpc_order(ctx->encoder.ogg.stream, flac_options.max_lpc_order);
    FLAC__stream_encoder_set_min_residual_partition_order(ctx->encoder.ogg.stream, flac_options.min_residual_partition_order);
    FLAC__stream_encoder_set_max_residual_partition_order(ctx->encoder.ogg.stream, flac_options.max_residual_partition_order);

    FLAC__stream_encoder_set_blocksize(ctx->encoder.ogg.stream, flac_options.blocksize);

    if (0 < num_metadata)
      FLAC__stream_encoder_set_metadata(ctx->encoder.ogg.stream, metadata, num_metadata);

    ctx->state.ogg = FLAC__stream_encoder_init_ogg_stream(ctx->encoder.ogg.stream,
		0,
		ogg_stream_encoder_write_callback,
		0, 0, 0,
		ctx);
    if (ctx->state.ogg != FLAC__STREAM_ENCODER_OK) {
      ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "cannot create OggFLAC state (%s)",
		FLAC__StreamEncoderStateString[ctx->state.ogg]);
      flac_session_close();
      return -1;
    }
  }
  else
  if (flac_options.seekable) {
    /* FLAC SEEKABLE STREAM */
    if ((ctx->encoder.flac.s_stream = FLAC__stream_encoder_new()) == NULL) {
      ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "cannot create FLAC stream");
      flac_session_close();
      return -1;
    }

    FLAC__stream_encoder_set_channels(ctx->encoder.flac.s_stream, nch);
    /* 16bps only */
    FLAC__stream_encoder_set_bits_per_sample(ctx->encoder.flac.s_stream, 16);

    FLAC__stream_encoder_set_verify(ctx->encoder.flac.s_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.s_stream, dpm.rate);

    FLAC__stream_encoder_set_qlp_coeff_precision(ctx->encoder.flac.s_stream, flac_options.qlp_coeff_precision);
    /* expensive! */
    FLAC__stream_encoder_set_do_qlp_coeff_prec_search(ctx->encoder.flac.s_stream, flac_options.qlp_coeff_precision_search);

    if (nch == 2) {
      FLAC__stream_encoder_set_do_mid_side_stereo(ctx->encoder.flac.s_stream, flac_options.mid_side);
      FLAC__stream_encoder_set_loose_mid_side_stereo(ctx->encoder.flac.s_stream, flac_options.adaptive_mid_side);
    }

    FLAC__stream_encoder_set_max_lpc_order(ctx->encoder.flac.s_stream, flac_options.max_lpc_order);
    FLAC__stream_encoder_set_min_residual_partition_order(ctx->encoder.flac.s_stream, flac_options.min_residual_partition_order);
    FLAC__stream_encoder_set_max_residual_partition_order(ctx->encoder.flac.s_stream, flac_options.max_residual_partition_order);

    FLAC__stream_encoder_set_blocksize(ctx->encoder.flac.s_stream, flac_options.blocksize);

    if (0 < num_metadata)
      FLAC__stream_encoder_set_metadata(ctx->encoder.flac.s_stream, metadata, num_metadata);

    ctx->state.s_flac = FLAC__stream_encoder_init_stream(
		ctx->encoder.flac.s_stream,
		flac_stream_encoder_write_callback,
		0, 0, 0,
		ctx);

    if (ctx->state.s_flac != FLAC__STREAM_ENCODER_OK) {
      ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "cannot create FLAC state (%s)",
		FLAC__StreamEncoderStateString[ctx->state.s_flac]);
      flac_session_close();
      return -1;
    }
  } else {
    /* NON SEEKABLE STREAM */
    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;
    }

    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);

    ctx->state.flac = FLAC__stream_encoder_init_stream(ctx->encoder.flac.stream,
		flac_stream_encoder_write_callback,
		0,
		0,
		flac_stream_encoder_metadata_callback,
		ctx);
    if (ctx->state.flac != FLAC__STREAM_ENCODER_OK) {
      ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "cannot create FLAC state (%s)",
		FLAC__StreamEncoderStateString[ctx->state.flac]);
      flac_session_close();
      return -1;
    }
  }

  return 0;
}

static int auto_flac_output_open(const char *input_filename, const char *title)
{
  char *output_filename;

  if (flac_options.isogg) {
#ifndef __W32G__
  output_filename = create_auto_output_name(input_filename, "ogg", NULL, 0);
#else
  output_filename = create_auto_output_name(input_filename, "ogg", w32g_output_dir, w32g_auto_output_mode);
#endif
  }
  else
  {
#ifndef __W32G__
    output_filename = create_auto_output_name(input_filename, "flac", NULL, 0);
#else
    output_filename = create_auto_output_name(input_filename, "flac", w32g_output_dir, w32g_auto_output_mode);
#endif
  }
  if (output_filename == NULL) {
    ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "unknown output file name");
	  return -1;
  }
  if ((flac_output_open(output_filename, input_filename)) == -1) {
    ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "files open failed %s->%s", output_filename, input_filename);
    free(output_filename);
    return -1;
  }
  if (dpm.name != NULL)
    free(dpm.name);
  dpm.name = output_filename;
  ctl->cmsg(CMSG_INFO, VERB_NORMAL, "Output %s", dpm.name);
  return 0;
}

static int open_output(void)
{
  int include_enc, exclude_enc;  

#ifdef AU_FLAC_DLL
	if (g_load_libFLAC_dll("libFLAC.dll")) {
		return -1;
	}
#endif
#ifdef AU_OGGFLAC_DLL
	if (g_load_libOggFLAC_dll("libOggFLAC.dll")) {
#ifdef AU_FLAC_DLL
		g_free_libFLAC_dll ();
#endif
		return -1;
	}
#endif

  include_enc = exclude_enc = 0;

  /* only 16 bit is supported */
  include_enc |= PE_16BIT | PE_SIGNED;
  exclude_enc |= PE_BYTESWAP | PE_24BIT;
  dpm.encoding = validate_encoding(dpm.encoding, include_enc, exclude_enc);

  if (flac_options.isogg) {
    ctl->cmsg(CMSG_WARNING, VERB_NORMAL, "*** cannot write back seekpoints when encoding to Ogg yet ***");
    ctl->cmsg(CMSG_WARNING, VERB_NORMAL, "*** and stream end will not be written.                   ***");
  }

#ifndef __W32G__
  if(dpm.name == NULL) {
    dpm.flag |= PF_AUTO_SPLIT_FILE;
    dpm.name = NULL;
  } else {
    dpm.flag &= ~PF_AUTO_SPLIT_FILE;
    if(flac_output_open(dpm.name, NULL) == -1)
      return -1;
  }
#else
  if(w32g_auto_output_mode > 0) {
    dpm.flag |= PF_AUTO_SPLIT_FILE;
    dpm.name = NULL;
    } else {
    dpm.flag &= ~PF_AUTO_SPLIT_FILE;
    if (flac_output_open(dpm.name, NULL) == -1)
      return -1;
  }
#endif

  return 0;
}

static FLAC__StreamEncoderWriteStatus
ogg_stream_encoder_write_callback(const FLAC__StreamEncoder *encoder,
				  const FLAC__byte buffer[],
				  size_t bytes, unsigned samples,
				  unsigned current_frame, void *client_data)
{
  FLAC_ctx *ctx = (FLAC_ctx *)client_data;

  ctx->out_bytes += bytes;

  if (write(dpm.fd, buffer, bytes) != -1)
    return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
  else
    return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
}
static FLAC__StreamEncoderWriteStatus
flac_stream_encoder_write_callback(const FLAC__StreamEncoder *encoder,
				   const FLAC__byte buffer[],
				   size_t bytes, unsigned samples,
				   unsigned current_frame, void *client_data)
{
  FLAC_ctx *ctx = (FLAC_ctx *)client_data;

  ctx->out_bytes += bytes;

  if (write(dpm.fd, buffer, bytes) == bytes)
    return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
  else
    return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
}
static void flac_stream_encoder_metadata_callback(const FLAC__StreamEncoder *encoder,
						  const FLAC__StreamMetadata *metadata,
						  void *client_data)
{
}

static int output_data(char *buf, int32 nbytes)
{
  FLAC__int32 *oggbuf;
  FLAC__int16 *s;
  int i;
  int nch = (dpm.encoding & PE_MONO) ? 1 : 2;

  FLAC_ctx *ctx = flac_ctx;

  if (dpm.fd < 0)
    return 0;

  if (ctx == NULL) {
    ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "FLAC stream is not initialized");
    return -1;
  }

  oggbuf = (FLAC__int32 *)safe_malloc(nbytes * sizeof(FLAC__int32) / nch);

  /*
    packing 16 -> 32 bit sample
  */
  s = (FLAC__int16 *)buf;
  for (i = 0; i < nbytes / nch; i++) {
    oggbuf[i] = *s++;
  }

  if (flac_options.isogg) {
    ctx->state.ogg = FLAC__stream_encoder_get_state(ctx->encoder.ogg.stream);
    if (ctx->state.ogg != FLAC__STREAM_ENCODER_OK) {
	ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "FLAC stream verify error (%s)",
		  FLAC__StreamEncoderStateString[FLAC__stream_encoder_get_verify_decoder_state(ctx->encoder.ogg.stream)]);
      ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "cannot encode OggFLAC stream (%s)",
		FLAC__StreamEncoderStateString[ctx->state.ogg]);
      flac_session_close();
      return -1;
    }

    if (!FLAC__stream_encoder_process_interleaved(ctx->encoder.ogg.stream, oggbuf,
						     nbytes / nch / 2)) {
      ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "cannot encode OggFLAC stream");
      flac_session_close();
      return -1;
    }
  }
  else
	if (flac_options.seekable) {
    ctx->state.s_flac = FLAC__stream_encoder_get_state(ctx->encoder.flac.s_stream);
    if (ctx->state.s_flac != FLAC__STREAM_ENCODER_OK) {
      if ((ctx->state.s_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.s_stream)]);
      }
      else {
	ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "cannot encode FLAC stream (%s)",
		  FLAC__StreamEncoderStateString[ctx->state.s_flac]);
      }
      flac_session_close();
      return -1;
    }

    if (!FLAC__stream_encoder_process_interleaved(ctx->encoder.flac.s_stream, oggbuf,
						  nbytes / nch / 2 )) {
      ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "cannot encode FLAC stream");
      flac_session_close();
      return -1;
    }
	}
  else
	{
    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;
    }
  }
  ctx->in_bytes += nbytes;

  free(oggbuf);
  return 0;
}

static void close_output(void)
{
  FLAC_ctx *ctx;

  ctx = flac_ctx;

  if (ctx == NULL)
    return;

  if (dpm.fd < 0) {
    flac_session_close();
    return;
  }

  if (flac_options.isogg) {
    if ((ctx->state.ogg = FLAC__stream_encoder_get_state(ctx->encoder.ogg.stream)) != FLAC__STREAM_ENCODER_OK) {
      ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "OggFLAC stream encoder is invalid (%s)",
		FLAC__StreamEncoderStateString[ctx->state.ogg]);
      /* fall through */
    }
  }
  else
  if (flac_options.seekable) {
    if ((ctx->state.s_flac = FLAC__stream_encoder_get_state(ctx->encoder.flac.s_stream)) != FLAC__STREAM_ENCODER_OK) {
      ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "FLAC stream encoder is invalid (%s)",
		FLAC__StreamEncoderStateString[ctx->state.s_flac]);
      /* fall through */
    }
	}
	else
  {
    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 */
    }
  }

  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.);

  flac_session_close();

#ifdef AU_FLAC_DLL
	g_free_libFLAC_dll ();
#endif
#ifdef AU_OGGFLAC_DLL
	g_free_libOggFLAC_dll ();
#endif

}

static int acntl(int request, void *arg)
{
  switch(request) {
  case PM_REQ_PLAY_START:
    if(dpm.flag & PF_AUTO_SPLIT_FILE)
      return auto_flac_output_open(current_file_info->filename, current_file_info->seq_name);
    break;
  case PM_REQ_PLAY_END:
    if(dpm.flag & PF_AUTO_SPLIT_FILE) {
      close_output();
      return 0;
    }
    break;
  case PM_REQ_DISCARD:
    return 0;
  }
  return -1;
}