~ubuntu-branches/ubuntu/precise/flac/precise-updates

« back to all changes in this revision

Viewing changes to src/libFLAC++/stream_encoder.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Joshua Kwan
  • Date: 2007-05-29 22:56:36 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070529225636-ljeff8xxip09qaap
Tags: 1.1.4-1
* New upstream release. closes: #405167, #411311
  - libOggFLAC and libOggFLAC++ have been merged into libFLAC, so
    remove their corresponding packages.
  - Because of the API changes required to effect the above, there has
    been yet another soname bump. libflac7 -> libflac8 and
    libflac++5 -> libflac++6. Emails have been dispatched to the
    maintainers of dependent packages.
* Some notes on patches that were removed:
  - 02_stdin_stdout, 06_manpage_mention_utf8_convert: merged upstream
  - 08_manpage_warnings: Upstream has changed the manpage so it defintely
    can't fit in in 80 cols, so just forget about it. We'll live.
  - 05_eof_warnings_are_errors: Upstream decided to add a -w option to
    flac to treat all warnings as errors. I am going to defer to that
    for now, but if people think it's stupid let me know and I'll port
    the patch forward.
  - 04_stack_smasher: was a backport from 1.1.3, so it's obsolete.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* libFLAC++ - Free Lossless Audio Codec library
2
 
 * Copyright (C) 2002,2003,2004,2005  Josh Coalson
 
2
 * Copyright (C) 2002,2003,2004,2005,2006,2007  Josh Coalson
3
3
 *
4
4
 * Redistribution and use in source and binary forms, with or without
5
5
 * modification, are permitted provided that the following conditions
41
41
namespace FLAC {
42
42
        namespace Encoder {
43
43
 
 
44
                // ------------------------------------------------------------
 
45
                //
 
46
                // Stream
 
47
                //
 
48
                // ------------------------------------------------------------
 
49
 
44
50
                Stream::Stream():
45
51
                encoder_(::FLAC__stream_encoder_new())
46
52
                { }
48
54
                Stream::~Stream()
49
55
                {
50
56
                        if(0 != encoder_) {
51
 
                                ::FLAC__stream_encoder_finish(encoder_);
 
57
                                (void)::FLAC__stream_encoder_finish(encoder_);
52
58
                                ::FLAC__stream_encoder_delete(encoder_);
53
59
                        }
54
60
                }
58
64
                        return 0 != encoder_;
59
65
                }
60
66
 
 
67
                bool Stream::set_ogg_serial_number(long value)
 
68
                {
 
69
                        FLAC__ASSERT(is_valid());
 
70
                        return (bool)::FLAC__stream_encoder_set_ogg_serial_number(encoder_, value);
 
71
                }
 
72
 
61
73
                bool Stream::set_verify(bool value)
62
74
                {
63
75
                        FLAC__ASSERT(is_valid());
70
82
                        return (bool)::FLAC__stream_encoder_set_streamable_subset(encoder_, value);
71
83
                }
72
84
 
73
 
                bool Stream::set_do_mid_side_stereo(bool value)
74
 
                {
75
 
                        FLAC__ASSERT(is_valid());
76
 
                        return (bool)::FLAC__stream_encoder_set_do_mid_side_stereo(encoder_, value);
77
 
                }
78
 
 
79
 
                bool Stream::set_loose_mid_side_stereo(bool value)
80
 
                {
81
 
                        FLAC__ASSERT(is_valid());
82
 
                        return (bool)::FLAC__stream_encoder_set_loose_mid_side_stereo(encoder_, value);
83
 
                }
84
 
 
85
85
                bool Stream::set_channels(unsigned value)
86
86
                {
87
87
                        FLAC__ASSERT(is_valid());
100
100
                        return (bool)::FLAC__stream_encoder_set_sample_rate(encoder_, value);
101
101
                }
102
102
 
 
103
                bool Stream::set_compression_level(unsigned value)
 
104
                {
 
105
                        FLAC__ASSERT(is_valid());
 
106
                        return (bool)::FLAC__stream_encoder_set_compression_level(encoder_, value);
 
107
                }
 
108
 
103
109
                bool Stream::set_blocksize(unsigned value)
104
110
                {
105
111
                        FLAC__ASSERT(is_valid());
106
112
                        return (bool)::FLAC__stream_encoder_set_blocksize(encoder_, value);
107
113
                }
108
114
 
 
115
                bool Stream::set_do_mid_side_stereo(bool value)
 
116
                {
 
117
                        FLAC__ASSERT(is_valid());
 
118
                        return (bool)::FLAC__stream_encoder_set_do_mid_side_stereo(encoder_, value);
 
119
                }
 
120
 
 
121
                bool Stream::set_loose_mid_side_stereo(bool value)
 
122
                {
 
123
                        FLAC__ASSERT(is_valid());
 
124
                        return (bool)::FLAC__stream_encoder_set_loose_mid_side_stereo(encoder_, value);
 
125
                }
 
126
 
 
127
                bool Stream::set_apodization(const char *specification)
 
128
                {
 
129
                        FLAC__ASSERT(is_valid());
 
130
                        return (bool)::FLAC__stream_encoder_set_apodization(encoder_, specification);
 
131
                }
 
132
 
109
133
                bool Stream::set_max_lpc_order(unsigned value)
110
134
                {
111
135
                        FLAC__ASSERT(is_valid());
169
193
                bool Stream::set_metadata(FLAC::Metadata::Prototype **metadata, unsigned num_blocks)
170
194
                {
171
195
                        FLAC__ASSERT(is_valid());
172
 
#if (defined _MSC_VER) || (defined __SUNPRO_CC)
 
196
#if (defined _MSC_VER) || (defined __BORLANDC__) || (defined __SUNPRO_CC)
173
197
                        // MSVC++ can't handle:
174
198
                        // ::FLAC__StreamMetadata *m[num_blocks];
175
199
                        // so we do this ugly workaround
178
202
                        ::FLAC__StreamMetadata *m[num_blocks];
179
203
#endif
180
204
                        for(unsigned i = 0; i < num_blocks; i++) {
181
 
                                // we can get away with this since we know the encoder will only correct the is_last flags
182
 
                                m[i] = const_cast< ::FLAC__StreamMetadata*>((::FLAC__StreamMetadata*)metadata[i]);
 
205
                                // we can get away with the const_cast since we know the encoder will only correct the is_last flags
 
206
                                m[i] = const_cast< ::FLAC__StreamMetadata*>((const ::FLAC__StreamMetadata*)metadata[i]);
183
207
                        }
184
 
#if (defined _MSC_VER) || (defined __SUNPRO_CC)
 
208
#if (defined _MSC_VER) || (defined __BORLANDC__) || (defined __SUNPRO_CC)
185
209
                        // complete the hack
186
210
                        const bool ok = (bool)::FLAC__stream_encoder_set_metadata(encoder_, m, num_blocks);
187
211
                        delete [] m;
311
335
                        return ::FLAC__stream_encoder_get_total_samples_estimate(encoder_);
312
336
                }
313
337
 
314
 
                Stream::State Stream::init()
315
 
                {
316
 
                        FLAC__ASSERT(is_valid());
317
 
                        ::FLAC__stream_encoder_set_write_callback(encoder_, write_callback_);
318
 
                        ::FLAC__stream_encoder_set_metadata_callback(encoder_, metadata_callback_);
319
 
                        ::FLAC__stream_encoder_set_client_data(encoder_, (void*)this);
320
 
                        return State(::FLAC__stream_encoder_init(encoder_));
321
 
                }
322
 
 
323
 
                void Stream::finish()
324
 
                {
325
 
                        FLAC__ASSERT(is_valid());
326
 
                        ::FLAC__stream_encoder_finish(encoder_);
 
338
                ::FLAC__StreamEncoderInitStatus Stream::init()
 
339
                {
 
340
                        FLAC__ASSERT(is_valid());
 
341
                        return ::FLAC__stream_encoder_init_stream(encoder_, write_callback_, seek_callback_, tell_callback_, metadata_callback_, /*client_data=*/(void*)this);
 
342
                }
 
343
 
 
344
                ::FLAC__StreamEncoderInitStatus Stream::init_ogg()
 
345
                {
 
346
                        FLAC__ASSERT(is_valid());
 
347
                        return ::FLAC__stream_encoder_init_ogg_stream(encoder_, read_callback_, write_callback_, seek_callback_, tell_callback_, metadata_callback_, /*client_data=*/(void*)this);
 
348
                }
 
349
 
 
350
                bool Stream::finish()
 
351
                {
 
352
                        FLAC__ASSERT(is_valid());
 
353
                        return (bool)::FLAC__stream_encoder_finish(encoder_);
327
354
                }
328
355
 
329
356
                bool Stream::process(const FLAC__int32 * const buffer[], unsigned samples)
338
365
                        return (bool)::FLAC__stream_encoder_process_interleaved(encoder_, buffer, samples);
339
366
                }
340
367
 
341
 
                ::FLAC__StreamEncoderWriteStatus Stream::write_callback_(const ::FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
 
368
                ::FLAC__StreamEncoderReadStatus Stream::read_callback(FLAC__byte buffer[], size_t *bytes)
 
369
                {
 
370
                        (void)buffer, (void)bytes;
 
371
                        return ::FLAC__STREAM_ENCODER_READ_STATUS_UNSUPPORTED;
 
372
                }
 
373
 
 
374
                ::FLAC__StreamEncoderSeekStatus Stream::seek_callback(FLAC__uint64 absolute_byte_offset)
 
375
                {
 
376
                        (void)absolute_byte_offset;
 
377
                        return ::FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED;
 
378
                }
 
379
 
 
380
                ::FLAC__StreamEncoderTellStatus Stream::tell_callback(FLAC__uint64 *absolute_byte_offset)
 
381
                {
 
382
                        (void)absolute_byte_offset;
 
383
                        return ::FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED;
 
384
                }
 
385
 
 
386
                void Stream::metadata_callback(const ::FLAC__StreamMetadata *metadata)
 
387
                {
 
388
                        (void)metadata;
 
389
                }
 
390
 
 
391
                ::FLAC__StreamEncoderReadStatus Stream::read_callback_(const ::FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
 
392
                {
 
393
                        (void)encoder;
 
394
                        FLAC__ASSERT(0 != client_data);
 
395
                        Stream *instance = reinterpret_cast<Stream *>(client_data);
 
396
                        FLAC__ASSERT(0 != instance);
 
397
                        return instance->read_callback(buffer, bytes);
 
398
                }
 
399
 
 
400
                ::FLAC__StreamEncoderWriteStatus Stream::write_callback_(const ::FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame, void *client_data)
342
401
                {
343
402
                        (void)encoder;
344
403
                        FLAC__ASSERT(0 != client_data);
347
406
                        return instance->write_callback(buffer, bytes, samples, current_frame);
348
407
                }
349
408
 
 
409
                ::FLAC__StreamEncoderSeekStatus Stream::seek_callback_(const ::FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
 
410
                {
 
411
                        (void)encoder;
 
412
                        FLAC__ASSERT(0 != client_data);
 
413
                        Stream *instance = reinterpret_cast<Stream *>(client_data);
 
414
                        FLAC__ASSERT(0 != instance);
 
415
                        return instance->seek_callback(absolute_byte_offset);
 
416
                }
 
417
 
 
418
                ::FLAC__StreamEncoderTellStatus Stream::tell_callback_(const ::FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
 
419
                {
 
420
                        (void)encoder;
 
421
                        FLAC__ASSERT(0 != client_data);
 
422
                        Stream *instance = reinterpret_cast<Stream *>(client_data);
 
423
                        FLAC__ASSERT(0 != instance);
 
424
                        return instance->tell_callback(absolute_byte_offset);
 
425
                }
 
426
 
350
427
                void Stream::metadata_callback_(const ::FLAC__StreamEncoder *encoder, const ::FLAC__StreamMetadata *metadata, void *client_data)
351
428
                {
352
429
                        (void)encoder;
356
433
                        instance->metadata_callback(metadata);
357
434
                }
358
435
 
 
436
                // ------------------------------------------------------------
 
437
                //
 
438
                // File
 
439
                //
 
440
                // ------------------------------------------------------------
 
441
 
 
442
                File::File():
 
443
                        Stream()
 
444
                { }
 
445
 
 
446
                File::~File()
 
447
                {
 
448
                }
 
449
 
 
450
                ::FLAC__StreamEncoderInitStatus File::init(FILE *file)
 
451
                {
 
452
                        FLAC__ASSERT(is_valid());
 
453
                        return ::FLAC__stream_encoder_init_FILE(encoder_, file, progress_callback_, /*client_data=*/(void*)this);
 
454
                }
 
455
 
 
456
                ::FLAC__StreamEncoderInitStatus File::init(const char *filename)
 
457
                {
 
458
                        FLAC__ASSERT(is_valid());
 
459
                        return ::FLAC__stream_encoder_init_file(encoder_, filename, progress_callback_, /*client_data=*/(void*)this);
 
460
                }
 
461
 
 
462
                ::FLAC__StreamEncoderInitStatus File::init(const std::string &filename)
 
463
                {
 
464
                        return init(filename.c_str());
 
465
                }
 
466
 
 
467
                ::FLAC__StreamEncoderInitStatus File::init_ogg(FILE *file)
 
468
                {
 
469
                        FLAC__ASSERT(is_valid());
 
470
                        return ::FLAC__stream_encoder_init_ogg_FILE(encoder_, file, progress_callback_, /*client_data=*/(void*)this);
 
471
                }
 
472
 
 
473
                ::FLAC__StreamEncoderInitStatus File::init_ogg(const char *filename)
 
474
                {
 
475
                        FLAC__ASSERT(is_valid());
 
476
                        return ::FLAC__stream_encoder_init_ogg_file(encoder_, filename, progress_callback_, /*client_data=*/(void*)this);
 
477
                }
 
478
 
 
479
                ::FLAC__StreamEncoderInitStatus File::init_ogg(const std::string &filename)
 
480
                {
 
481
                        return init_ogg(filename.c_str());
 
482
                }
 
483
 
 
484
                // This is a dummy to satisfy the pure virtual from Stream; the
 
485
                // read callback will never be called since we are initializing
 
486
                // with FLAC__stream_decoder_init_FILE() or
 
487
                // FLAC__stream_decoder_init_file() and those supply the read
 
488
                // callback internally.
 
489
                ::FLAC__StreamEncoderWriteStatus File::write_callback(const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame)
 
490
                {
 
491
                        (void)buffer, (void)bytes, (void)samples, (void)current_frame;
 
492
                        FLAC__ASSERT(false);
 
493
                        return ::FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR; // double protection
 
494
                }
 
495
 
 
496
                void File::progress_callback(FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate)
 
497
                {
 
498
                        (void)bytes_written, (void)samples_written, (void)frames_written, (void)total_frames_estimate;
 
499
                }
 
500
 
 
501
                void File::progress_callback_(const ::FLAC__StreamEncoder *encoder, FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate, void *client_data)
 
502
                {
 
503
                        (void)encoder;
 
504
                        FLAC__ASSERT(0 != client_data);
 
505
                        File *instance = reinterpret_cast<File *>(client_data);
 
506
                        FLAC__ASSERT(0 != instance);
 
507
                        instance->progress_callback(bytes_written, samples_written, frames_written, total_frames_estimate);
 
508
                }
 
509
 
359
510
        }
360
511
}