src/lib/libflac/libflac++/metadata.cpp
| r0 | r14135 | |
| 1 | /* libFLAC++ - Free Lossless Audio Codec library
|
| 2 | * Copyright (C) 2002,2003,2004,2005,2006,2007 Josh Coalson
|
| 3 | *
|
| 4 | * Redistribution and use in source and binary forms, with or without
|
| 5 | * modification, are permitted provided that the following conditions
|
| 6 | * are met:
|
| 7 | *
|
| 8 | * - Redistributions of source code must retain the above copyright
|
| 9 | * notice, this list of conditions and the following disclaimer.
|
| 10 | *
|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
| 13 | * documentation and/or other materials provided with the distribution.
|
| 14 | *
|
| 15 | * - Neither the name of the Xiph.org Foundation nor the names of its
|
| 16 | * contributors may be used to endorse or promote products derived from
|
| 17 | * this software without specific prior written permission.
|
| 18 | *
|
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
| 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
| 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
| 22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
|
| 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
| 24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
| 25 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
| 26 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
| 27 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
| 28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
| 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
| 30 | */
|
| 31 | |
| 32 | #define __STDC_LIMIT_MACROS 1 /* otherwise SIZE_MAX is not defined for c++ */
|
| 33 | #include "share/alloc.h"
|
| 34 | #include "flac++/metadata.h"
|
| 35 | #include "flac/assert.h"
|
| 36 | #include <stdlib.h> // for malloc(), free()
|
| 37 | #include <string.h> // for memcpy() etc.
|
| 38 | |
| 39 | #ifdef _MSC_VER
|
| 40 | // warning C4800: 'int' : forcing to bool 'true' or 'false' (performance warning)
|
| 41 | #pragma warning ( disable : 4800 )
|
| 42 | #endif
|
| 43 | |
| 44 | namespace FLAC {
|
| 45 | namespace Metadata {
|
| 46 | |
| 47 | // local utility routines
|
| 48 | |
| 49 | namespace local {
|
| 50 | |
| 51 | Prototype *construct_block(::FLAC__StreamMetadata *object)
|
| 52 | {
|
| 53 | Prototype *ret = 0;
|
| 54 | switch(object->type) {
|
| 55 | case FLAC__METADATA_TYPE_STREAMINFO:
|
| 56 | ret = new StreamInfo(object, /*copy=*/false);
|
| 57 | break;
|
| 58 | case FLAC__METADATA_TYPE_PADDING:
|
| 59 | ret = new Padding(object, /*copy=*/false);
|
| 60 | break;
|
| 61 | case FLAC__METADATA_TYPE_APPLICATION:
|
| 62 | ret = new Application(object, /*copy=*/false);
|
| 63 | break;
|
| 64 | case FLAC__METADATA_TYPE_SEEKTABLE:
|
| 65 | ret = new SeekTable(object, /*copy=*/false);
|
| 66 | break;
|
| 67 | case FLAC__METADATA_TYPE_VORBIS_COMMENT:
|
| 68 | ret = new VorbisComment(object, /*copy=*/false);
|
| 69 | break;
|
| 70 | case FLAC__METADATA_TYPE_CUESHEET:
|
| 71 | ret = new CueSheet(object, /*copy=*/false);
|
| 72 | break;
|
| 73 | case FLAC__METADATA_TYPE_PICTURE:
|
| 74 | ret = new Picture(object, /*copy=*/false);
|
| 75 | break;
|
| 76 | default:
|
| 77 | ret = new Unknown(object, /*copy=*/false);
|
| 78 | break;
|
| 79 | }
|
| 80 | return ret;
|
| 81 | }
|
| 82 | |
| 83 | }
|
| 84 | |
| 85 | FLACPP_API Prototype *clone(const Prototype *object)
|
| 86 | {
|
| 87 | FLAC__ASSERT(0 != object);
|
| 88 | |
| 89 | const StreamInfo *streaminfo = dynamic_cast<const StreamInfo *>(object);
|
| 90 | const Padding *padding = dynamic_cast<const Padding *>(object);
|
| 91 | const Application *application = dynamic_cast<const Application *>(object);
|
| 92 | const SeekTable *seektable = dynamic_cast<const SeekTable *>(object);
|
| 93 | const VorbisComment *vorbiscomment = dynamic_cast<const VorbisComment *>(object);
|
| 94 | const CueSheet *cuesheet = dynamic_cast<const CueSheet *>(object);
|
| 95 | const Picture *picture = dynamic_cast<const Picture *>(object);
|
| 96 | const Unknown *unknown = dynamic_cast<const Unknown *>(object);
|
| 97 | |
| 98 | if(0 != streaminfo)
|
| 99 | return new StreamInfo(*streaminfo);
|
| 100 | else if(0 != padding)
|
| 101 | return new Padding(*padding);
|
| 102 | else if(0 != application)
|
| 103 | return new Application(*application);
|
| 104 | else if(0 != seektable)
|
| 105 | return new SeekTable(*seektable);
|
| 106 | else if(0 != vorbiscomment)
|
| 107 | return new VorbisComment(*vorbiscomment);
|
| 108 | else if(0 != cuesheet)
|
| 109 | return new CueSheet(*cuesheet);
|
| 110 | else if(0 != picture)
|
| 111 | return new Picture(*picture);
|
| 112 | else if(0 != unknown)
|
| 113 | return new Unknown(*unknown);
|
| 114 | else {
|
| 115 | FLAC__ASSERT(0);
|
| 116 | return 0;
|
| 117 | }
|
| 118 | }
|
| 119 | |
| 120 | //
|
| 121 | // Prototype
|
| 122 | //
|
| 123 | |
| 124 | Prototype::Prototype(const Prototype &object):
|
| 125 | object_(::FLAC__metadata_object_clone(object.object_)),
|
| 126 | is_reference_(false)
|
| 127 | {
|
| 128 | FLAC__ASSERT(object.is_valid());
|
| 129 | }
|
| 130 | |
| 131 | Prototype::Prototype(const ::FLAC__StreamMetadata &object):
|
| 132 | object_(::FLAC__metadata_object_clone(&object)),
|
| 133 | is_reference_(false)
|
| 134 | {
|
| 135 | }
|
| 136 | |
| 137 | Prototype::Prototype(const ::FLAC__StreamMetadata *object):
|
| 138 | object_(::FLAC__metadata_object_clone(object)),
|
| 139 | is_reference_(false)
|
| 140 | {
|
| 141 | FLAC__ASSERT(0 != object);
|
| 142 | }
|
| 143 | |
| 144 | Prototype::Prototype(::FLAC__StreamMetadata *object, bool copy):
|
| 145 | object_(copy? ::FLAC__metadata_object_clone(object) : object),
|
| 146 | is_reference_(false)
|
| 147 | {
|
| 148 | FLAC__ASSERT(0 != object);
|
| 149 | }
|
| 150 | |
| 151 | Prototype::~Prototype()
|
| 152 | {
|
| 153 | clear();
|
| 154 | }
|
| 155 | |
| 156 | void Prototype::clear()
|
| 157 | {
|
| 158 | if(0 != object_ && !is_reference_)
|
| 159 | FLAC__metadata_object_delete(object_);
|
| 160 | object_ = 0;
|
| 161 | }
|
| 162 | |
| 163 | Prototype &Prototype::operator=(const Prototype &object)
|
| 164 | {
|
| 165 | FLAC__ASSERT(object.is_valid());
|
| 166 | clear();
|
| 167 | is_reference_ = false;
|
| 168 | object_ = ::FLAC__metadata_object_clone(object.object_);
|
| 169 | return *this;
|
| 170 | }
|
| 171 | |
| 172 | Prototype &Prototype::operator=(const ::FLAC__StreamMetadata &object)
|
| 173 | {
|
| 174 | clear();
|
| 175 | is_reference_ = false;
|
| 176 | object_ = ::FLAC__metadata_object_clone(&object);
|
| 177 | return *this;
|
| 178 | }
|
| 179 | |
| 180 | Prototype &Prototype::operator=(const ::FLAC__StreamMetadata *object)
|
| 181 | {
|
| 182 | FLAC__ASSERT(0 != object);
|
| 183 | clear();
|
| 184 | is_reference_ = false;
|
| 185 | object_ = ::FLAC__metadata_object_clone(object);
|
| 186 | return *this;
|
| 187 | }
|
| 188 | |
| 189 | Prototype &Prototype::assign_object(::FLAC__StreamMetadata *object, bool copy)
|
| 190 | {
|
| 191 | FLAC__ASSERT(0 != object);
|
| 192 | clear();
|
| 193 | object_ = (copy? ::FLAC__metadata_object_clone(object) : object);
|
| 194 | is_reference_ = false;
|
| 195 | return *this;
|
| 196 | }
|
| 197 | |
| 198 | bool Prototype::get_is_last() const
|
| 199 | {
|
| 200 | FLAC__ASSERT(is_valid());
|
| 201 | return (bool)object_->is_last;
|
| 202 | }
|
| 203 | |
| 204 | FLAC__MetadataType Prototype::get_type() const
|
| 205 | {
|
| 206 | FLAC__ASSERT(is_valid());
|
| 207 | return object_->type;
|
| 208 | }
|
| 209 | |
| 210 | unsigned Prototype::get_length() const
|
| 211 | {
|
| 212 | FLAC__ASSERT(is_valid());
|
| 213 | return object_->length;
|
| 214 | }
|
| 215 | |
| 216 | void Prototype::set_is_last(bool value)
|
| 217 | {
|
| 218 | FLAC__ASSERT(is_valid());
|
| 219 | object_->is_last = value;
|
| 220 | }
|
| 221 | |
| 222 | |
| 223 | //
|
| 224 | // StreamInfo
|
| 225 | //
|
| 226 | |
| 227 | StreamInfo::StreamInfo():
|
| 228 | Prototype(FLAC__metadata_object_new(FLAC__METADATA_TYPE_STREAMINFO), /*copy=*/false)
|
| 229 | { }
|
| 230 | |
| 231 | StreamInfo::~StreamInfo()
|
| 232 | { }
|
| 233 | |
| 234 | unsigned StreamInfo::get_min_blocksize() const
|
| 235 | {
|
| 236 | FLAC__ASSERT(is_valid());
|
| 237 | return object_->data.stream_info.min_blocksize;
|
| 238 | }
|
| 239 | |
| 240 | unsigned StreamInfo::get_max_blocksize() const
|
| 241 | {
|
| 242 | FLAC__ASSERT(is_valid());
|
| 243 | return object_->data.stream_info.max_blocksize;
|
| 244 | }
|
| 245 | |
| 246 | unsigned StreamInfo::get_min_framesize() const
|
| 247 | {
|
| 248 | FLAC__ASSERT(is_valid());
|
| 249 | return object_->data.stream_info.min_framesize;
|
| 250 | }
|
| 251 | |
| 252 | unsigned StreamInfo::get_max_framesize() const
|
| 253 | {
|
| 254 | FLAC__ASSERT(is_valid());
|
| 255 | return object_->data.stream_info.max_framesize;
|
| 256 | }
|
| 257 | |
| 258 | unsigned StreamInfo::get_sample_rate() const
|
| 259 | {
|
| 260 | FLAC__ASSERT(is_valid());
|
| 261 | return object_->data.stream_info.sample_rate;
|
| 262 | }
|
| 263 | |
| 264 | unsigned StreamInfo::get_channels() const
|
| 265 | {
|
| 266 | FLAC__ASSERT(is_valid());
|
| 267 | return object_->data.stream_info.channels;
|
| 268 | }
|
| 269 | |
| 270 | unsigned StreamInfo::get_bits_per_sample() const
|
| 271 | {
|
| 272 | FLAC__ASSERT(is_valid());
|
| 273 | return object_->data.stream_info.bits_per_sample;
|
| 274 | }
|
| 275 | |
| 276 | FLAC__uint64 StreamInfo::get_total_samples() const
|
| 277 | {
|
| 278 | FLAC__ASSERT(is_valid());
|
| 279 | return object_->data.stream_info.total_samples;
|
| 280 | }
|
| 281 | |
| 282 | const FLAC__byte *StreamInfo::get_md5sum() const
|
| 283 | {
|
| 284 | FLAC__ASSERT(is_valid());
|
| 285 | return object_->data.stream_info.md5sum;
|
| 286 | }
|
| 287 | |
| 288 | void StreamInfo::set_min_blocksize(unsigned value)
|
| 289 | {
|
| 290 | FLAC__ASSERT(is_valid());
|
| 291 | FLAC__ASSERT(value >= FLAC__MIN_BLOCK_SIZE);
|
| 292 | FLAC__ASSERT(value <= FLAC__MAX_BLOCK_SIZE);
|
| 293 | object_->data.stream_info.min_blocksize = value;
|
| 294 | }
|
| 295 | |
| 296 | void StreamInfo::set_max_blocksize(unsigned value)
|
| 297 | {
|
| 298 | FLAC__ASSERT(is_valid());
|
| 299 | FLAC__ASSERT(value >= FLAC__MIN_BLOCK_SIZE);
|
| 300 | FLAC__ASSERT(value <= FLAC__MAX_BLOCK_SIZE);
|
| 301 | object_->data.stream_info.max_blocksize = value;
|
| 302 | }
|
| 303 | |
| 304 | void StreamInfo::set_min_framesize(unsigned value)
|
| 305 | {
|
| 306 | FLAC__ASSERT(is_valid());
|
| 307 | FLAC__ASSERT(value < (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN));
|
| 308 | object_->data.stream_info.min_framesize = value;
|
| 309 | }
|
| 310 | |
| 311 | void StreamInfo::set_max_framesize(unsigned value)
|
| 312 | {
|
| 313 | FLAC__ASSERT(is_valid());
|
| 314 | FLAC__ASSERT(value < (1u << FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN));
|
| 315 | object_->data.stream_info.max_framesize = value;
|
| 316 | }
|
| 317 | |
| 318 | void StreamInfo::set_sample_rate(unsigned value)
|
| 319 | {
|
| 320 | FLAC__ASSERT(is_valid());
|
| 321 | FLAC__ASSERT(FLAC__format_sample_rate_is_valid(value));
|
| 322 | object_->data.stream_info.sample_rate = value;
|
| 323 | }
|
| 324 | |
| 325 | void StreamInfo::set_channels(unsigned value)
|
| 326 | {
|
| 327 | FLAC__ASSERT(is_valid());
|
| 328 | FLAC__ASSERT(value > 0);
|
| 329 | FLAC__ASSERT(value <= FLAC__MAX_CHANNELS);
|
| 330 | object_->data.stream_info.channels = value;
|
| 331 | }
|
| 332 | |
| 333 | void StreamInfo::set_bits_per_sample(unsigned value)
|
| 334 | {
|
| 335 | FLAC__ASSERT(is_valid());
|
| 336 | FLAC__ASSERT(value >= FLAC__MIN_BITS_PER_SAMPLE);
|
| 337 | FLAC__ASSERT(value <= FLAC__MAX_BITS_PER_SAMPLE);
|
| 338 | object_->data.stream_info.bits_per_sample = value;
|
| 339 | }
|
| 340 | |
| 341 | void StreamInfo::set_total_samples(FLAC__uint64 value)
|
| 342 | {
|
| 343 | FLAC__ASSERT(is_valid());
|
| 344 | FLAC__ASSERT(value < (((FLAC__uint64)1) << FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN));
|
| 345 | object_->data.stream_info.total_samples = value;
|
| 346 | }
|
| 347 | |
| 348 | void StreamInfo::set_md5sum(const FLAC__byte value[16])
|
| 349 | {
|
| 350 | FLAC__ASSERT(is_valid());
|
| 351 | FLAC__ASSERT(0 != value);
|
| 352 | memcpy(object_->data.stream_info.md5sum, value, 16);
|
| 353 | }
|
| 354 | |
| 355 | |
| 356 | //
|
| 357 | // Padding
|
| 358 | //
|
| 359 | |
| 360 | Padding::Padding():
|
| 361 | Prototype(FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING), /*copy=*/false)
|
| 362 | { }
|
| 363 | |
| 364 | Padding::~Padding()
|
| 365 | { }
|
| 366 | |
| 367 | void Padding::set_length(unsigned length)
|
| 368 | {
|
| 369 | FLAC__ASSERT(is_valid());
|
| 370 | object_->length = length;
|
| 371 | }
|
| 372 | |
| 373 | |
| 374 | //
|
| 375 | // Application
|
| 376 | //
|
| 377 | |
| 378 | Application::Application():
|
| 379 | Prototype(FLAC__metadata_object_new(FLAC__METADATA_TYPE_APPLICATION), /*copy=*/false)
|
| 380 | { }
|
| 381 | |
| 382 | Application::~Application()
|
| 383 | { }
|
| 384 | |
| 385 | const FLAC__byte *Application::get_id() const
|
| 386 | {
|
| 387 | FLAC__ASSERT(is_valid());
|
| 388 | return object_->data.application.id;
|
| 389 | }
|
| 390 | |
| 391 | const FLAC__byte *Application::get_data() const
|
| 392 | {
|
| 393 | FLAC__ASSERT(is_valid());
|
| 394 | return object_->data.application.data;
|
| 395 | }
|
| 396 | |
| 397 | void Application::set_id(const FLAC__byte value[4])
|
| 398 | {
|
| 399 | FLAC__ASSERT(is_valid());
|
| 400 | FLAC__ASSERT(0 != value);
|
| 401 | memcpy(object_->data.application.id, value, 4);
|
| 402 | }
|
| 403 | |
| 404 | bool Application::set_data(const FLAC__byte *data, unsigned length)
|
| 405 | {
|
| 406 | FLAC__ASSERT(is_valid());
|
| 407 | return (bool)::FLAC__metadata_object_application_set_data(object_, (FLAC__byte*)data, length, true);
|
| 408 | }
|
| 409 | |
| 410 | bool Application::set_data(FLAC__byte *data, unsigned length, bool copy)
|
| 411 | {
|
| 412 | FLAC__ASSERT(is_valid());
|
| 413 | return (bool)::FLAC__metadata_object_application_set_data(object_, data, length, copy);
|
| 414 | }
|
| 415 | |
| 416 | |
| 417 | //
|
| 418 | // SeekTable
|
| 419 | //
|
| 420 | |
| 421 | SeekTable::SeekTable():
|
| 422 | Prototype(FLAC__metadata_object_new(FLAC__METADATA_TYPE_SEEKTABLE), /*copy=*/false)
|
| 423 | { }
|
| 424 | |
| 425 | SeekTable::~SeekTable()
|
| 426 | { }
|
| 427 | |
| 428 | unsigned SeekTable::get_num_points() const
|
| 429 | {
|
| 430 | FLAC__ASSERT(is_valid());
|
| 431 | return object_->data.seek_table.num_points;
|
| 432 | }
|
| 433 | |
| 434 | ::FLAC__StreamMetadata_SeekPoint SeekTable::get_point(unsigned index) const
|
| 435 | {
|
| 436 | FLAC__ASSERT(is_valid());
|
| 437 | FLAC__ASSERT(index < object_->data.seek_table.num_points);
|
| 438 | return object_->data.seek_table.points[index];
|
| 439 | }
|
| 440 | |
| 441 | void SeekTable::set_point(unsigned index, const ::FLAC__StreamMetadata_SeekPoint &point)
|
| 442 | {
|
| 443 | FLAC__ASSERT(is_valid());
|
| 444 | FLAC__ASSERT(index < object_->data.seek_table.num_points);
|
| 445 | ::FLAC__metadata_object_seektable_set_point(object_, index, point);
|
| 446 | }
|
| 447 | |
| 448 | bool SeekTable::insert_point(unsigned index, const ::FLAC__StreamMetadata_SeekPoint &point)
|
| 449 | {
|
| 450 | FLAC__ASSERT(is_valid());
|
| 451 | FLAC__ASSERT(index <= object_->data.seek_table.num_points);
|
| 452 | return (bool)::FLAC__metadata_object_seektable_insert_point(object_, index, point);
|
| 453 | }
|
| 454 | |
| 455 | bool SeekTable::delete_point(unsigned index)
|
| 456 | {
|
| 457 | FLAC__ASSERT(is_valid());
|
| 458 | FLAC__ASSERT(index < object_->data.seek_table.num_points);
|
| 459 | return (bool)::FLAC__metadata_object_seektable_delete_point(object_, index);
|
| 460 | }
|
| 461 | |
| 462 | bool SeekTable::is_legal() const
|
| 463 | {
|
| 464 | FLAC__ASSERT(is_valid());
|
| 465 | return (bool)::FLAC__metadata_object_seektable_is_legal(object_);
|
| 466 | }
|
| 467 | |
| 468 | |
| 469 | //
|
| 470 | // VorbisComment::Entry
|
| 471 | //
|
| 472 | |
| 473 | VorbisComment::Entry::Entry()
|
| 474 | {
|
| 475 | zero();
|
| 476 | }
|
| 477 | |
| 478 | VorbisComment::Entry::Entry(const char *field, unsigned field_length)
|
| 479 | {
|
| 480 | zero();
|
| 481 | construct(field, field_length);
|
| 482 | }
|
| 483 | |
| 484 | VorbisComment::Entry::Entry(const char *field)
|
| 485 | {
|
| 486 | zero();
|
| 487 | construct(field);
|
| 488 | }
|
| 489 | |
| 490 | VorbisComment::Entry::Entry(const char *field_name, const char *field_value, unsigned field_value_length)
|
| 491 | {
|
| 492 | zero();
|
| 493 | construct(field_name, field_value, field_value_length);
|
| 494 | }
|
| 495 | |
| 496 | VorbisComment::Entry::Entry(const char *field_name, const char *field_value)
|
| 497 | {
|
| 498 | zero();
|
| 499 | construct(field_name, field_value);
|
| 500 | }
|
| 501 | |
| 502 | VorbisComment::Entry::Entry(const Entry &entry)
|
| 503 | {
|
| 504 | FLAC__ASSERT(entry.is_valid());
|
| 505 | zero();
|
| 506 | construct((const char *)entry.entry_.entry, entry.entry_.length);
|
| 507 | }
|
| 508 | |
| 509 | VorbisComment::Entry &VorbisComment::Entry::operator=(const Entry &entry)
|
| 510 | {
|
| 511 | FLAC__ASSERT(entry.is_valid());
|
| 512 | clear();
|
| 513 | construct((const char *)entry.entry_.entry, entry.entry_.length);
|
| 514 | return *this;
|
| 515 | }
|
| 516 | |
| 517 | VorbisComment::Entry::~Entry()
|
| 518 | {
|
| 519 | clear();
|
| 520 | }
|
| 521 | |
| 522 | bool VorbisComment::Entry::is_valid() const
|
| 523 | {
|
| 524 | return is_valid_;
|
| 525 | }
|
| 526 | |
| 527 | unsigned VorbisComment::Entry::get_field_length() const
|
| 528 | {
|
| 529 | FLAC__ASSERT(is_valid());
|
| 530 | return entry_.length;
|
| 531 | }
|
| 532 | |
| 533 | unsigned VorbisComment::Entry::get_field_name_length() const
|
| 534 | {
|
| 535 | FLAC__ASSERT(is_valid());
|
| 536 | return field_name_length_;
|
| 537 | }
|
| 538 | |
| 539 | unsigned VorbisComment::Entry::get_field_value_length() const
|
| 540 | {
|
| 541 | FLAC__ASSERT(is_valid());
|
| 542 | return field_value_length_;
|
| 543 | }
|
| 544 | |
| 545 | ::FLAC__StreamMetadata_VorbisComment_Entry VorbisComment::Entry::get_entry() const
|
| 546 | {
|
| 547 | FLAC__ASSERT(is_valid());
|
| 548 | return entry_;
|
| 549 | }
|
| 550 | |
| 551 | const char *VorbisComment::Entry::get_field() const
|
| 552 | {
|
| 553 | FLAC__ASSERT(is_valid());
|
| 554 | return (const char *)entry_.entry;
|
| 555 | }
|
| 556 | |
| 557 | const char *VorbisComment::Entry::get_field_name() const
|
| 558 | {
|
| 559 | FLAC__ASSERT(is_valid());
|
| 560 | return field_name_;
|
| 561 | }
|
| 562 | |
| 563 | const char *VorbisComment::Entry::get_field_value() const
|
| 564 | {
|
| 565 | FLAC__ASSERT(is_valid());
|
| 566 | return field_value_;
|
| 567 | }
|
| 568 | |
| 569 | bool VorbisComment::Entry::set_field(const char *field, unsigned field_length)
|
| 570 | {
|
| 571 | FLAC__ASSERT(is_valid());
|
| 572 | FLAC__ASSERT(0 != field);
|
| 573 | |
| 574 | if(!::FLAC__format_vorbiscomment_entry_is_legal((const ::FLAC__byte*)field, field_length))
|
| 575 | return is_valid_ = false;
|
| 576 | |
| 577 | clear_entry();
|
| 578 | |
| 579 | if(0 == (entry_.entry = (FLAC__byte*)safe_malloc_add_2op_(field_length, /*+*/1))) {
|
| 580 | is_valid_ = false;
|
| 581 | }
|
| 582 | else {
|
| 583 | entry_.length = field_length;
|
| 584 | memcpy(entry_.entry, field, field_length);
|
| 585 | entry_.entry[field_length] = '\0';
|
| 586 | (void) parse_field();
|
| 587 | }
|
| 588 | |
| 589 | return is_valid_;
|
| 590 | }
|
| 591 | |
| 592 | bool VorbisComment::Entry::set_field(const char *field)
|
| 593 | {
|
| 594 | return set_field(field, strlen(field));
|
| 595 | }
|
| 596 | |
| 597 | bool VorbisComment::Entry::set_field_name(const char *field_name)
|
| 598 | {
|
| 599 | FLAC__ASSERT(is_valid());
|
| 600 | FLAC__ASSERT(0 != field_name);
|
| 601 | |
| 602 | if(!::FLAC__format_vorbiscomment_entry_name_is_legal(field_name))
|
| 603 | return is_valid_ = false;
|
| 604 | |
| 605 | clear_field_name();
|
| 606 | |
| 607 | if(0 == (field_name_ = strdup(field_name))) {
|
| 608 | is_valid_ = false;
|
| 609 | }
|
| 610 | else {
|
| 611 | field_name_length_ = strlen(field_name_);
|
| 612 | compose_field();
|
| 613 | }
|
| 614 | |
| 615 | return is_valid_;
|
| 616 | }
|
| 617 | |
| 618 | bool VorbisComment::Entry::set_field_value(const char *field_value, unsigned field_value_length)
|
| 619 | {
|
| 620 | FLAC__ASSERT(is_valid());
|
| 621 | FLAC__ASSERT(0 != field_value);
|
| 622 | |
| 623 | if(!::FLAC__format_vorbiscomment_entry_value_is_legal((const FLAC__byte*)field_value, field_value_length))
|
| 624 | return is_valid_ = false;
|
| 625 | |
| 626 | clear_field_value();
|
| 627 | |
| 628 | if(0 == (field_value_ = (char *)safe_malloc_add_2op_(field_value_length, /*+*/1))) {
|
| 629 | is_valid_ = false;
|
| 630 | }
|
| 631 | else {
|
| 632 | field_value_length_ = field_value_length;
|
| 633 | memcpy(field_value_, field_value, field_value_length);
|
| 634 | field_value_[field_value_length] = '\0';
|
| 635 | compose_field();
|
| 636 | }
|
| 637 | |
| 638 | return is_valid_;
|
| 639 | }
|
| 640 | |
| 641 | bool VorbisComment::Entry::set_field_value(const char *field_value)
|
| 642 | {
|
| 643 | return set_field_value(field_value, strlen(field_value));
|
| 644 | }
|
| 645 | |
| 646 | void VorbisComment::Entry::zero()
|
| 647 | {
|
| 648 | is_valid_ = true;
|
| 649 | entry_.length = 0;
|
| 650 | entry_.entry = 0;
|
| 651 | field_name_ = 0;
|
| 652 | field_name_length_ = 0;
|
| 653 | field_value_ = 0;
|
| 654 | field_value_length_ = 0;
|
| 655 | }
|
| 656 | |
| 657 | void VorbisComment::Entry::clear()
|
| 658 | {
|
| 659 | clear_entry();
|
| 660 | clear_field_name();
|
| 661 | clear_field_value();
|
| 662 | is_valid_ = true;
|
| 663 | }
|
| 664 | |
| 665 | void VorbisComment::Entry::clear_entry()
|
| 666 | {
|
| 667 | if(0 != entry_.entry) {
|
| 668 | free(entry_.entry);
|
| 669 | entry_.entry = 0;
|
| 670 | entry_.length = 0;
|
| 671 | }
|
| 672 | }
|
| 673 | |
| 674 | void VorbisComment::Entry::clear_field_name()
|
| 675 | {
|
| 676 | if(0 != field_name_) {
|
| 677 | free(field_name_);
|
| 678 | field_name_ = 0;
|
| 679 | field_name_length_ = 0;
|
| 680 | }
|
| 681 | }
|
| 682 | |
| 683 | void VorbisComment::Entry::clear_field_value()
|
| 684 | {
|
| 685 | if(0 != field_value_) {
|
| 686 | free(field_value_);
|
| 687 | field_value_ = 0;
|
| 688 | field_value_length_ = 0;
|
| 689 | }
|
| 690 | }
|
| 691 | |
| 692 | void VorbisComment::Entry::construct(const char *field, unsigned field_length)
|
| 693 | {
|
| 694 | if(set_field(field, field_length))
|
| 695 | parse_field();
|
| 696 | }
|
| 697 | |
| 698 | void VorbisComment::Entry::construct(const char *field)
|
| 699 | {
|
| 700 | construct(field, strlen(field));
|
| 701 | }
|
| 702 | |
| 703 | void VorbisComment::Entry::construct(const char *field_name, const char *field_value, unsigned field_value_length)
|
| 704 | {
|
| 705 | if(set_field_name(field_name) && set_field_value(field_value, field_value_length))
|
| 706 | compose_field();
|
| 707 | }
|
| 708 | |
| 709 | void VorbisComment::Entry::construct(const char *field_name, const char *field_value)
|
| 710 | {
|
| 711 | construct(field_name, field_value, strlen(field_value));
|
| 712 | }
|
| 713 | |
| 714 | void VorbisComment::Entry::compose_field()
|
| 715 | {
|
| 716 | clear_entry();
|
| 717 | |
| 718 | if(0 == (entry_.entry = (FLAC__byte*)safe_malloc_add_4op_(field_name_length_, /*+*/1, /*+*/field_value_length_, /*+*/1))) {
|
| 719 | is_valid_ = false;
|
| 720 | }
|
| 721 | else {
|
| 722 | memcpy(entry_.entry, field_name_, field_name_length_);
|
| 723 | entry_.length += field_name_length_;
|
| 724 | memcpy(entry_.entry + entry_.length, "=", 1);
|
| 725 | entry_.length += 1;
|
| 726 | memcpy(entry_.entry + entry_.length, field_value_, field_value_length_);
|
| 727 | entry_.length += field_value_length_;
|
| 728 | entry_.entry[entry_.length] = '\0';
|
| 729 | is_valid_ = true;
|
| 730 | }
|
| 731 | }
|
| 732 | |
| 733 | void VorbisComment::Entry::parse_field()
|
| 734 | {
|
| 735 | clear_field_name();
|
| 736 | clear_field_value();
|
| 737 | |
| 738 | const char *p = (const char *)memchr(entry_.entry, '=', entry_.length);
|
| 739 | |
| 740 | if(0 == p)
|
| 741 | p = (const char *)entry_.entry + entry_.length;
|
| 742 | |
| 743 | field_name_length_ = (unsigned)(p - (const char *)entry_.entry);
|
| 744 | if(0 == (field_name_ = (char *)safe_malloc_add_2op_(field_name_length_, /*+*/1))) { // +1 for the trailing \0
|
| 745 | is_valid_ = false;
|
| 746 | return;
|
| 747 | }
|
| 748 | memcpy(field_name_, entry_.entry, field_name_length_);
|
| 749 | field_name_[field_name_length_] = '\0';
|
| 750 | |
| 751 | if(entry_.length - field_name_length_ == 0) {
|
| 752 | field_value_length_ = 0;
|
| 753 | if(0 == (field_value_ = (char *)safe_malloc_(0))) {
|
| 754 | is_valid_ = false;
|
| 755 | return;
|
| 756 | }
|
| 757 | }
|
| 758 | else {
|
| 759 | field_value_length_ = entry_.length - field_name_length_ - 1;
|
| 760 | if(0 == (field_value_ = (char *)safe_malloc_add_2op_(field_value_length_, /*+*/1))) { // +1 for the trailing \0
|
| 761 | is_valid_ = false;
|
| 762 | return;
|
| 763 | }
|
| 764 | memcpy(field_value_, ++p, field_value_length_);
|
| 765 | field_value_[field_value_length_] = '\0';
|
| 766 | }
|
| 767 | |
| 768 | is_valid_ = true;
|
| 769 | }
|
| 770 | |
| 771 | |
| 772 | //
|
| 773 | // VorbisComment
|
| 774 | //
|
| 775 | |
| 776 | VorbisComment::VorbisComment():
|
| 777 | Prototype(FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT), /*copy=*/false)
|
| 778 | { }
|
| 779 | |
| 780 | VorbisComment::~VorbisComment()
|
| 781 | { }
|
| 782 | |
| 783 | unsigned VorbisComment::get_num_comments() const
|
| 784 | {
|
| 785 | FLAC__ASSERT(is_valid());
|
| 786 | return object_->data.vorbis_comment.num_comments;
|
| 787 | }
|
| 788 | |
| 789 | const FLAC__byte *VorbisComment::get_vendor_string() const
|
| 790 | {
|
| 791 | FLAC__ASSERT(is_valid());
|
| 792 | return object_->data.vorbis_comment.vendor_string.entry;
|
| 793 | }
|
| 794 | |
| 795 | VorbisComment::Entry VorbisComment::get_comment(unsigned index) const
|
| 796 | {
|
| 797 | FLAC__ASSERT(is_valid());
|
| 798 | FLAC__ASSERT(index < object_->data.vorbis_comment.num_comments);
|
| 799 | return Entry((const char *)object_->data.vorbis_comment.comments[index].entry, object_->data.vorbis_comment.comments[index].length);
|
| 800 | }
|
| 801 | |
| 802 | bool VorbisComment::set_vendor_string(const FLAC__byte *string)
|
| 803 | {
|
| 804 | FLAC__ASSERT(is_valid());
|
| 805 | // vendor_string is a special kind of entry
|
| 806 | const ::FLAC__StreamMetadata_VorbisComment_Entry vendor_string = { strlen((const char *)string), (FLAC__byte*)string }; // we can cheat on const-ness because we make a copy below:
|
| 807 | return (bool)::FLAC__metadata_object_vorbiscomment_set_vendor_string(object_, vendor_string, /*copy=*/true);
|
| 808 | }
|
| 809 | |
| 810 | bool VorbisComment::set_comment(unsigned index, const VorbisComment::Entry &entry)
|
| 811 | {
|
| 812 | FLAC__ASSERT(is_valid());
|
| 813 | FLAC__ASSERT(index < object_->data.vorbis_comment.num_comments);
|
| 814 | return (bool)::FLAC__metadata_object_vorbiscomment_set_comment(object_, index, entry.get_entry(), /*copy=*/true);
|
| 815 | }
|
| 816 | |
| 817 | bool VorbisComment::insert_comment(unsigned index, const VorbisComment::Entry &entry)
|
| 818 | {
|
| 819 | FLAC__ASSERT(is_valid());
|
| 820 | FLAC__ASSERT(index <= object_->data.vorbis_comment.num_comments);
|
| 821 | return (bool)::FLAC__metadata_object_vorbiscomment_insert_comment(object_, index, entry.get_entry(), /*copy=*/true);
|
| 822 | }
|
| 823 | |
| 824 | bool VorbisComment::append_comment(const VorbisComment::Entry &entry)
|
| 825 | {
|
| 826 | FLAC__ASSERT(is_valid());
|
| 827 | return (bool)::FLAC__metadata_object_vorbiscomment_append_comment(object_, entry.get_entry(), /*copy=*/true);
|
| 828 | }
|
| 829 | |
| 830 | bool VorbisComment::delete_comment(unsigned index)
|
| 831 | {
|
| 832 | FLAC__ASSERT(is_valid());
|
| 833 | FLAC__ASSERT(index < object_->data.vorbis_comment.num_comments);
|
| 834 | return (bool)::FLAC__metadata_object_vorbiscomment_delete_comment(object_, index);
|
| 835 | }
|
| 836 | |
| 837 | |
| 838 | //
|
| 839 | // CueSheet::Track
|
| 840 | //
|
| 841 | |
| 842 | CueSheet::Track::Track():
|
| 843 | object_(::FLAC__metadata_object_cuesheet_track_new())
|
| 844 | { }
|
| 845 | |
| 846 | CueSheet::Track::Track(const ::FLAC__StreamMetadata_CueSheet_Track *track):
|
| 847 | object_(::FLAC__metadata_object_cuesheet_track_clone(track))
|
| 848 | { }
|
| 849 | |
| 850 | CueSheet::Track::Track(const Track &track):
|
| 851 | object_(::FLAC__metadata_object_cuesheet_track_clone(track.object_))
|
| 852 | { }
|
| 853 | |
| 854 | CueSheet::Track &CueSheet::Track::operator=(const Track &track)
|
| 855 | {
|
| 856 | if(0 != object_)
|
| 857 | ::FLAC__metadata_object_cuesheet_track_delete(object_);
|
| 858 | object_ = ::FLAC__metadata_object_cuesheet_track_clone(track.object_);
|
| 859 | return *this;
|
| 860 | }
|
| 861 | |
| 862 | CueSheet::Track::~Track()
|
| 863 | {
|
| 864 | if(0 != object_)
|
| 865 | ::FLAC__metadata_object_cuesheet_track_delete(object_);
|
| 866 | }
|
| 867 | |
| 868 | bool CueSheet::Track::is_valid() const
|
| 869 | {
|
| 870 | return(0 != object_);
|
| 871 | }
|
| 872 | |
| 873 | ::FLAC__StreamMetadata_CueSheet_Index CueSheet::Track::get_index(unsigned i) const
|
| 874 | {
|
| 875 | FLAC__ASSERT(is_valid());
|
| 876 | FLAC__ASSERT(i < object_->num_indices);
|
| 877 | return object_->indices[i];
|
| 878 | }
|
| 879 | |
| 880 | void CueSheet::Track::set_isrc(const char value[12])
|
| 881 | {
|
| 882 | FLAC__ASSERT(is_valid());
|
| 883 | FLAC__ASSERT(0 != value);
|
| 884 | memcpy(object_->isrc, value, 12);
|
| 885 | object_->isrc[12] = '\0';
|
| 886 | }
|
| 887 | |
| 888 | void CueSheet::Track::set_type(unsigned value)
|
| 889 | {
|
| 890 | FLAC__ASSERT(is_valid());
|
| 891 | FLAC__ASSERT(value <= 1);
|
| 892 | object_->type = value;
|
| 893 | }
|
| 894 | |
| 895 | void CueSheet::Track::set_index(unsigned i, const ::FLAC__StreamMetadata_CueSheet_Index &index)
|
| 896 | {
|
| 897 | FLAC__ASSERT(is_valid());
|
| 898 | FLAC__ASSERT(i < object_->num_indices);
|
| 899 | object_->indices[i] = index;
|
| 900 | }
|
| 901 | |
| 902 | |
| 903 | //
|
| 904 | // CueSheet
|
| 905 | //
|
| 906 | |
| 907 | CueSheet::CueSheet():
|
| 908 | Prototype(FLAC__metadata_object_new(FLAC__METADATA_TYPE_CUESHEET), /*copy=*/false)
|
| 909 | { }
|
| 910 | |
| 911 | CueSheet::~CueSheet()
|
| 912 | { }
|
| 913 | |
| 914 | const char *CueSheet::get_media_catalog_number() const
|
| 915 | {
|
| 916 | FLAC__ASSERT(is_valid());
|
| 917 | return object_->data.cue_sheet.media_catalog_number;
|
| 918 | }
|
| 919 | |
| 920 | FLAC__uint64 CueSheet::get_lead_in() const
|
| 921 | {
|
| 922 | FLAC__ASSERT(is_valid());
|
| 923 | return object_->data.cue_sheet.lead_in;
|
| 924 | }
|
| 925 | |
| 926 | bool CueSheet::get_is_cd() const
|
| 927 | {
|
| 928 | FLAC__ASSERT(is_valid());
|
| 929 | return object_->data.cue_sheet.is_cd? true : false;
|
| 930 | }
|
| 931 | |
| 932 | unsigned CueSheet::get_num_tracks() const
|
| 933 | {
|
| 934 | FLAC__ASSERT(is_valid());
|
| 935 | return object_->data.cue_sheet.num_tracks;
|
| 936 | }
|
| 937 | |
| 938 | CueSheet::Track CueSheet::get_track(unsigned i) const
|
| 939 | {
|
| 940 | FLAC__ASSERT(is_valid());
|
| 941 | FLAC__ASSERT(i < object_->data.cue_sheet.num_tracks);
|
| 942 | return Track(object_->data.cue_sheet.tracks + i);
|
| 943 | }
|
| 944 | |
| 945 | void CueSheet::set_media_catalog_number(const char value[128])
|
| 946 | {
|
| 947 | FLAC__ASSERT(is_valid());
|
| 948 | FLAC__ASSERT(0 != value);
|
| 949 | memcpy(object_->data.cue_sheet.media_catalog_number, value, 128);
|
| 950 | object_->data.cue_sheet.media_catalog_number[128] = '\0';
|
| 951 | }
|
| 952 | |
| 953 | void CueSheet::set_lead_in(FLAC__uint64 value)
|
| 954 | {
|
| 955 | FLAC__ASSERT(is_valid());
|
| 956 | object_->data.cue_sheet.lead_in = value;
|
| 957 | }
|
| 958 | |
| 959 | void CueSheet::set_is_cd(bool value)
|
| 960 | {
|
| 961 | FLAC__ASSERT(is_valid());
|
| 962 | object_->data.cue_sheet.is_cd = value;
|
| 963 | }
|
| 964 | |
| 965 | void CueSheet::set_index(unsigned track_num, unsigned index_num, const ::FLAC__StreamMetadata_CueSheet_Index &index)
|
| 966 | {
|
| 967 | FLAC__ASSERT(is_valid());
|
| 968 | FLAC__ASSERT(track_num < object_->data.cue_sheet.num_tracks);
|
| 969 | FLAC__ASSERT(index_num < object_->data.cue_sheet.tracks[track_num].num_indices);
|
| 970 | object_->data.cue_sheet.tracks[track_num].indices[index_num] = index;
|
| 971 | }
|
| 972 | |
| 973 | bool CueSheet::insert_index(unsigned track_num, unsigned index_num, const ::FLAC__StreamMetadata_CueSheet_Index &index)
|
| 974 | {
|
| 975 | FLAC__ASSERT(is_valid());
|
| 976 | FLAC__ASSERT(track_num < object_->data.cue_sheet.num_tracks);
|
| 977 | FLAC__ASSERT(index_num <= object_->data.cue_sheet.tracks[track_num].num_indices);
|
| 978 | return (bool)::FLAC__metadata_object_cuesheet_track_insert_index(object_, track_num, index_num, index);
|
| 979 | }
|
| 980 | |
| 981 | bool CueSheet::delete_index(unsigned track_num, unsigned index_num)
|
| 982 | {
|
| 983 | FLAC__ASSERT(is_valid());
|
| 984 | FLAC__ASSERT(track_num < object_->data.cue_sheet.num_tracks);
|
| 985 | FLAC__ASSERT(index_num < object_->data.cue_sheet.tracks[track_num].num_indices);
|
| 986 | return (bool)::FLAC__metadata_object_cuesheet_track_delete_index(object_, track_num, index_num);
|
| 987 | }
|
| 988 | |
| 989 | bool CueSheet::set_track(unsigned i, const CueSheet::Track &track)
|
| 990 | {
|
| 991 | FLAC__ASSERT(is_valid());
|
| 992 | FLAC__ASSERT(i < object_->data.cue_sheet.num_tracks);
|
| 993 | // We can safely const_cast since copy=true
|
| 994 | return (bool)::FLAC__metadata_object_cuesheet_set_track(object_, i, const_cast< ::FLAC__StreamMetadata_CueSheet_Track*>(track.get_track()), /*copy=*/true);
|
| 995 | }
|
| 996 | |
| 997 | bool CueSheet::insert_track(unsigned i, const CueSheet::Track &track)
|
| 998 | {
|
| 999 | FLAC__ASSERT(is_valid());
|
| 1000 | FLAC__ASSERT(i <= object_->data.cue_sheet.num_tracks);
|
| 1001 | // We can safely const_cast since copy=true
|
| 1002 | return (bool)::FLAC__metadata_object_cuesheet_insert_track(object_, i, const_cast< ::FLAC__StreamMetadata_CueSheet_Track*>(track.get_track()), /*copy=*/true);
|
| 1003 | }
|
| 1004 | |
| 1005 | bool CueSheet::delete_track(unsigned i)
|
| 1006 | {
|
| 1007 | FLAC__ASSERT(is_valid());
|
| 1008 | FLAC__ASSERT(i < object_->data.cue_sheet.num_tracks);
|
| 1009 | return (bool)::FLAC__metadata_object_cuesheet_delete_track(object_, i);
|
| 1010 | }
|
| 1011 | |
| 1012 | bool CueSheet::is_legal(bool check_cd_da_subset, const char **violation) const
|
| 1013 | {
|
| 1014 | FLAC__ASSERT(is_valid());
|
| 1015 | return (bool)::FLAC__metadata_object_cuesheet_is_legal(object_, check_cd_da_subset, violation);
|
| 1016 | }
|
| 1017 | |
| 1018 | FLAC__uint32 CueSheet::calculate_cddb_id() const
|
| 1019 | {
|
| 1020 | FLAC__ASSERT(is_valid());
|
| 1021 | return ::FLAC__metadata_object_cuesheet_calculate_cddb_id(object_);
|
| 1022 | }
|
| 1023 | |
| 1024 | |
| 1025 | //
|
| 1026 | // Picture
|
| 1027 | //
|
| 1028 | |
| 1029 | Picture::Picture():
|
| 1030 | Prototype(FLAC__metadata_object_new(FLAC__METADATA_TYPE_PICTURE), /*copy=*/false)
|
| 1031 | { }
|
| 1032 | |
| 1033 | Picture::~Picture()
|
| 1034 | { }
|
| 1035 | |
| 1036 | ::FLAC__StreamMetadata_Picture_Type Picture::get_type() const
|
| 1037 | {
|
| 1038 | FLAC__ASSERT(is_valid());
|
| 1039 | return object_->data.picture.type;
|
| 1040 | }
|
| 1041 | |
| 1042 | const char *Picture::get_mime_type() const
|
| 1043 | {
|
| 1044 | FLAC__ASSERT(is_valid());
|
| 1045 | return object_->data.picture.mime_type;
|
| 1046 | }
|
| 1047 | |
| 1048 | const FLAC__byte *Picture::get_description() const
|
| 1049 | {
|
| 1050 | FLAC__ASSERT(is_valid());
|
| 1051 | return object_->data.picture.description;
|
| 1052 | }
|
| 1053 | |
| 1054 | FLAC__uint32 Picture::get_width() const
|
| 1055 | {
|
| 1056 | FLAC__ASSERT(is_valid());
|
| 1057 | return object_->data.picture.width;
|
| 1058 | }
|
| 1059 | |
| 1060 | FLAC__uint32 Picture::get_height() const
|
| 1061 | {
|
| 1062 | FLAC__ASSERT(is_valid());
|
| 1063 | return object_->data.picture.height;
|
| 1064 | }
|
| 1065 | |
| 1066 | FLAC__uint32 Picture::get_depth() const
|
| 1067 | {
|
| 1068 | FLAC__ASSERT(is_valid());
|
| 1069 | return object_->data.picture.depth;
|
| 1070 | }
|
| 1071 | |
| 1072 | FLAC__uint32 Picture::get_colors() const
|
| 1073 | {
|
| 1074 | FLAC__ASSERT(is_valid());
|
| 1075 | return object_->data.picture.colors;
|
| 1076 | }
|
| 1077 | |
| 1078 | FLAC__uint32 Picture::get_data_length() const
|
| 1079 | {
|
| 1080 | FLAC__ASSERT(is_valid());
|
| 1081 | return object_->data.picture.data_length;
|
| 1082 | }
|
| 1083 | |
| 1084 | const FLAC__byte *Picture::get_data() const
|
| 1085 | {
|
| 1086 | FLAC__ASSERT(is_valid());
|
| 1087 | return object_->data.picture.data;
|
| 1088 | }
|
| 1089 | |
| 1090 | void Picture::set_type(::FLAC__StreamMetadata_Picture_Type type)
|
| 1091 | {
|
| 1092 | FLAC__ASSERT(is_valid());
|
| 1093 | object_->data.picture.type = type;
|
| 1094 | }
|
| 1095 | |
| 1096 | bool Picture::set_mime_type(const char *string)
|
| 1097 | {
|
| 1098 | FLAC__ASSERT(is_valid());
|
| 1099 | // We can safely const_cast since copy=true
|
| 1100 | return (bool)::FLAC__metadata_object_picture_set_mime_type(object_, const_cast<char*>(string), /*copy=*/true);
|
| 1101 | }
|
| 1102 | |
| 1103 | bool Picture::set_description(const FLAC__byte *string)
|
| 1104 | {
|
| 1105 | FLAC__ASSERT(is_valid());
|
| 1106 | // We can safely const_cast since copy=true
|
| 1107 | return (bool)::FLAC__metadata_object_picture_set_description(object_, const_cast<FLAC__byte*>(string), /*copy=*/true);
|
| 1108 | }
|
| 1109 | |
| 1110 | void Picture::set_width(FLAC__uint32 value) const
|
| 1111 | {
|
| 1112 | FLAC__ASSERT(is_valid());
|
| 1113 | object_->data.picture.width = value;
|
| 1114 | }
|
| 1115 | |
| 1116 | void Picture::set_height(FLAC__uint32 value) const
|
| 1117 | {
|
| 1118 | FLAC__ASSERT(is_valid());
|
| 1119 | object_->data.picture.height = value;
|
| 1120 | }
|
| 1121 | |
| 1122 | void Picture::set_depth(FLAC__uint32 value) const
|
| 1123 | {
|
| 1124 | FLAC__ASSERT(is_valid());
|
| 1125 | object_->data.picture.depth = value;
|
| 1126 | }
|
| 1127 | |
| 1128 | void Picture::set_colors(FLAC__uint32 value) const
|
| 1129 | {
|
| 1130 | FLAC__ASSERT(is_valid());
|
| 1131 | object_->data.picture.colors = value;
|
| 1132 | }
|
| 1133 | |
| 1134 | bool Picture::set_data(const FLAC__byte *data, FLAC__uint32 data_length)
|
| 1135 | {
|
| 1136 | FLAC__ASSERT(is_valid());
|
| 1137 | // We can safely const_cast since copy=true
|
| 1138 | return (bool)::FLAC__metadata_object_picture_set_data(object_, const_cast<FLAC__byte*>(data), data_length, /*copy=*/true);
|
| 1139 | }
|
| 1140 | |
| 1141 | |
| 1142 | //
|
| 1143 | // Unknown
|
| 1144 | //
|
| 1145 | |
| 1146 | Unknown::Unknown():
|
| 1147 | Prototype(FLAC__metadata_object_new(FLAC__METADATA_TYPE_APPLICATION), /*copy=*/false)
|
| 1148 | { }
|
| 1149 | |
| 1150 | Unknown::~Unknown()
|
| 1151 | { }
|
| 1152 | |
| 1153 | const FLAC__byte *Unknown::get_data() const
|
| 1154 | {
|
| 1155 | FLAC__ASSERT(is_valid());
|
| 1156 | return object_->data.application.data;
|
| 1157 | }
|
| 1158 | |
| 1159 | bool Unknown::set_data(const FLAC__byte *data, unsigned length)
|
| 1160 | {
|
| 1161 | FLAC__ASSERT(is_valid());
|
| 1162 | return (bool)::FLAC__metadata_object_application_set_data(object_, (FLAC__byte*)data, length, true);
|
| 1163 | }
|
| 1164 | |
| 1165 | bool Unknown::set_data(FLAC__byte *data, unsigned length, bool copy)
|
| 1166 | {
|
| 1167 | FLAC__ASSERT(is_valid());
|
| 1168 | return (bool)::FLAC__metadata_object_application_set_data(object_, data, length, copy);
|
| 1169 | }
|
| 1170 | |
| 1171 | |
| 1172 | // ============================================================
|
| 1173 | //
|
| 1174 | // Level 0
|
| 1175 | //
|
| 1176 | // ============================================================
|
| 1177 | |
| 1178 | FLACPP_API bool get_streaminfo(const char *filename, StreamInfo &streaminfo)
|
| 1179 | {
|
| 1180 | FLAC__ASSERT(0 != filename);
|
| 1181 | |
| 1182 | ::FLAC__StreamMetadata object;
|
| 1183 | |
| 1184 | if(::FLAC__metadata_get_streaminfo(filename, &object)) {
|
| 1185 | streaminfo = object;
|
| 1186 | return true;
|
| 1187 | }
|
| 1188 | else
|
| 1189 | return false;
|
| 1190 | }
|
| 1191 | |
| 1192 | FLACPP_API bool get_tags(const char *filename, VorbisComment *&tags)
|
| 1193 | {
|
| 1194 | FLAC__ASSERT(0 != filename);
|
| 1195 | |
| 1196 | ::FLAC__StreamMetadata *object;
|
| 1197 | |
| 1198 | tags = 0;
|
| 1199 | |
| 1200 | if(::FLAC__metadata_get_tags(filename, &object)) {
|
| 1201 | tags = new VorbisComment(object, /*copy=*/false);
|
| 1202 | return true;
|
| 1203 | }
|
| 1204 | else
|
| 1205 | return false;
|
| 1206 | }
|
| 1207 | |
| 1208 | FLACPP_API bool get_tags(const char *filename, VorbisComment &tags)
|
| 1209 | {
|
| 1210 | FLAC__ASSERT(0 != filename);
|
| 1211 | |
| 1212 | ::FLAC__StreamMetadata *object;
|
| 1213 | |
| 1214 | if(::FLAC__metadata_get_tags(filename, &object)) {
|
| 1215 | tags.assign(object, /*copy=*/false);
|
| 1216 | return true;
|
| 1217 | }
|
| 1218 | else
|
| 1219 | return false;
|
| 1220 | }
|
| 1221 | |
| 1222 | FLACPP_API bool get_cuesheet(const char *filename, CueSheet *&cuesheet)
|
| 1223 | {
|
| 1224 | FLAC__ASSERT(0 != filename);
|
| 1225 | |
| 1226 | ::FLAC__StreamMetadata *object;
|
| 1227 | |
| 1228 | cuesheet = 0;
|
| 1229 | |
| 1230 | if(::FLAC__metadata_get_cuesheet(filename, &object)) {
|
| 1231 | cuesheet = new CueSheet(object, /*copy=*/false);
|
| 1232 | return true;
|
| 1233 | }
|
| 1234 | else
|
| 1235 | return false;
|
| 1236 | }
|
| 1237 | |
| 1238 | FLACPP_API bool get_cuesheet(const char *filename, CueSheet &cuesheet)
|
| 1239 | {
|
| 1240 | FLAC__ASSERT(0 != filename);
|
| 1241 | |
| 1242 | ::FLAC__StreamMetadata *object;
|
| 1243 | |
| 1244 | if(::FLAC__metadata_get_cuesheet(filename, &object)) {
|
| 1245 | cuesheet.assign(object, /*copy=*/false);
|
| 1246 | return true;
|
| 1247 | }
|
| 1248 | else
|
| 1249 | return false;
|
| 1250 | }
|
| 1251 | |
| 1252 | FLACPP_API bool get_picture(const char *filename, Picture *&picture, ::FLAC__StreamMetadata_Picture_Type type, const char *mime_type, const FLAC__byte *description, unsigned max_width, unsigned max_height, unsigned max_depth, unsigned max_colors)
|
| 1253 | {
|
| 1254 | FLAC__ASSERT(0 != filename);
|
| 1255 | |
| 1256 | ::FLAC__StreamMetadata *object;
|
| 1257 | |
| 1258 | picture = 0;
|
| 1259 | |
| 1260 | if(::FLAC__metadata_get_picture(filename, &object, type, mime_type, description, max_width, max_height, max_depth, max_colors)) {
|
| 1261 | picture = new Picture(object, /*copy=*/false);
|
| 1262 | return true;
|
| 1263 | }
|
| 1264 | else
|
| 1265 | return false;
|
| 1266 | }
|
| 1267 | |
| 1268 | FLACPP_API bool get_picture(const char *filename, Picture &picture, ::FLAC__StreamMetadata_Picture_Type type, const char *mime_type, const FLAC__byte *description, unsigned max_width, unsigned max_height, unsigned max_depth, unsigned max_colors)
|
| 1269 | {
|
| 1270 | FLAC__ASSERT(0 != filename);
|
| 1271 | |
| 1272 | ::FLAC__StreamMetadata *object;
|
| 1273 | |
| 1274 | if(::FLAC__metadata_get_picture(filename, &object, type, mime_type, description, max_width, max_height, max_depth, max_colors)) {
|
| 1275 | picture.assign(object, /*copy=*/false);
|
| 1276 | return true;
|
| 1277 | }
|
| 1278 | else
|
| 1279 | return false;
|
| 1280 | }
|
| 1281 | |
| 1282 | |
| 1283 | // ============================================================
|
| 1284 | //
|
| 1285 | // Level 1
|
| 1286 | //
|
| 1287 | // ============================================================
|
| 1288 | |
| 1289 | SimpleIterator::SimpleIterator():
|
| 1290 | iterator_(::FLAC__metadata_simple_iterator_new())
|
| 1291 | { }
|
| 1292 | |
| 1293 | SimpleIterator::~SimpleIterator()
|
| 1294 | {
|
| 1295 | clear();
|
| 1296 | }
|
| 1297 | |
| 1298 | void SimpleIterator::clear()
|
| 1299 | {
|
| 1300 | if(0 != iterator_)
|
| 1301 | FLAC__metadata_simple_iterator_delete(iterator_);
|
| 1302 | iterator_ = 0;
|
| 1303 | }
|
| 1304 | |
| 1305 | bool SimpleIterator::init(const char *filename, bool read_only, bool preserve_file_stats)
|
| 1306 | {
|
| 1307 | FLAC__ASSERT(0 != filename);
|
| 1308 | FLAC__ASSERT(is_valid());
|
| 1309 | return (bool)::FLAC__metadata_simple_iterator_init(iterator_, filename, read_only, preserve_file_stats);
|
| 1310 | }
|
| 1311 | |
| 1312 | bool SimpleIterator::is_valid() const
|
| 1313 | {
|
| 1314 | return 0 != iterator_;
|
| 1315 | }
|
| 1316 | |
| 1317 | SimpleIterator::Status SimpleIterator::status()
|
| 1318 | {
|
| 1319 | FLAC__ASSERT(is_valid());
|
| 1320 | return Status(::FLAC__metadata_simple_iterator_status(iterator_));
|
| 1321 | }
|
| 1322 | |
| 1323 | bool SimpleIterator::is_writable() const
|
| 1324 | {
|
| 1325 | FLAC__ASSERT(is_valid());
|
| 1326 | return (bool)::FLAC__metadata_simple_iterator_is_writable(iterator_);
|
| 1327 | }
|
| 1328 | |
| 1329 | bool SimpleIterator::next()
|
| 1330 | {
|
| 1331 | FLAC__ASSERT(is_valid());
|
| 1332 | return (bool)::FLAC__metadata_simple_iterator_next(iterator_);
|
| 1333 | }
|
| 1334 | |
| 1335 | bool SimpleIterator::prev()
|
| 1336 | {
|
| 1337 | FLAC__ASSERT(is_valid());
|
| 1338 | return (bool)::FLAC__metadata_simple_iterator_prev(iterator_);
|
| 1339 | }
|
| 1340 | |
| 1341 | //@@@@ add to tests
|
| 1342 | bool SimpleIterator::is_last() const
|
| 1343 | {
|
| 1344 | FLAC__ASSERT(is_valid());
|
| 1345 | return (bool)::FLAC__metadata_simple_iterator_is_last(iterator_);
|
| 1346 | }
|
| 1347 | |
| 1348 | //@@@@ add to tests
|
| 1349 | off_t SimpleIterator::get_block_offset() const
|
| 1350 | {
|
| 1351 | FLAC__ASSERT(is_valid());
|
| 1352 | return ::FLAC__metadata_simple_iterator_get_block_offset(iterator_);
|
| 1353 | }
|
| 1354 | |
| 1355 | ::FLAC__MetadataType SimpleIterator::get_block_type() const
|
| 1356 | {
|
| 1357 | FLAC__ASSERT(is_valid());
|
| 1358 | return ::FLAC__metadata_simple_iterator_get_block_type(iterator_);
|
| 1359 | }
|
| 1360 | |
| 1361 | //@@@@ add to tests
|
| 1362 | unsigned SimpleIterator::get_block_length() const
|
| 1363 | {
|
| 1364 | FLAC__ASSERT(is_valid());
|
| 1365 | return ::FLAC__metadata_simple_iterator_get_block_length(iterator_);
|
| 1366 | }
|
| 1367 | |
| 1368 | //@@@@ add to tests
|
| 1369 | bool SimpleIterator::get_application_id(FLAC__byte *id)
|
| 1370 | {
|
| 1371 | FLAC__ASSERT(is_valid());
|
| 1372 | return (bool)::FLAC__metadata_simple_iterator_get_application_id(iterator_, id);
|
| 1373 | }
|
| 1374 | |
| 1375 | Prototype *SimpleIterator::get_block()
|
| 1376 | {
|
| 1377 | FLAC__ASSERT(is_valid());
|
| 1378 | return local::construct_block(::FLAC__metadata_simple_iterator_get_block(iterator_));
|
| 1379 | }
|
| 1380 | |
| 1381 | bool SimpleIterator::set_block(Prototype *block, bool use_padding)
|
| 1382 | {
|
| 1383 | FLAC__ASSERT(0 != block);
|
| 1384 | FLAC__ASSERT(is_valid());
|
| 1385 | return (bool)::FLAC__metadata_simple_iterator_set_block(iterator_, block->object_, use_padding);
|
| 1386 | }
|
| 1387 | |
| 1388 | bool SimpleIterator::insert_block_after(Prototype *block, bool use_padding)
|
| 1389 | {
|
| 1390 | FLAC__ASSERT(0 != block);
|
| 1391 | FLAC__ASSERT(is_valid());
|
| 1392 | return (bool)::FLAC__metadata_simple_iterator_insert_block_after(iterator_, block->object_, use_padding);
|
| 1393 | }
|
| 1394 | |
| 1395 | bool SimpleIterator::delete_block(bool use_padding)
|
| 1396 | {
|
| 1397 | FLAC__ASSERT(is_valid());
|
| 1398 | return (bool)::FLAC__metadata_simple_iterator_delete_block(iterator_, use_padding);
|
| 1399 | }
|
| 1400 | |
| 1401 | |
| 1402 | // ============================================================
|
| 1403 | //
|
| 1404 | // Level 2
|
| 1405 | //
|
| 1406 | // ============================================================
|
| 1407 | |
| 1408 | Chain::Chain():
|
| 1409 | chain_(::FLAC__metadata_chain_new())
|
| 1410 | { }
|
| 1411 | |
| 1412 | Chain::~Chain()
|
| 1413 | {
|
| 1414 | clear();
|
| 1415 | }
|
| 1416 | |
| 1417 | void Chain::clear()
|
| 1418 | {
|
| 1419 | if(0 != chain_)
|
| 1420 | FLAC__metadata_chain_delete(chain_);
|
| 1421 | chain_ = 0;
|
| 1422 | }
|
| 1423 | |
| 1424 | bool Chain::is_valid() const
|
| 1425 | {
|
| 1426 | return 0 != chain_;
|
| 1427 | }
|
| 1428 | |
| 1429 | Chain::Status Chain::status()
|
| 1430 | {
|
| 1431 | FLAC__ASSERT(is_valid());
|
| 1432 | return Status(::FLAC__metadata_chain_status(chain_));
|
| 1433 | }
|
| 1434 | |
| 1435 | bool Chain::read(const char *filename, bool is_ogg)
|
| 1436 | {
|
| 1437 | FLAC__ASSERT(0 != filename);
|
| 1438 | FLAC__ASSERT(is_valid());
|
| 1439 | return is_ogg?
|
| 1440 | (bool)::FLAC__metadata_chain_read_ogg(chain_, filename) :
|
| 1441 | (bool)::FLAC__metadata_chain_read(chain_, filename)
|
| 1442 | ;
|
| 1443 | }
|
| 1444 | |
| 1445 | bool Chain::read(FLAC__IOHandle handle, ::FLAC__IOCallbacks callbacks, bool is_ogg)
|
| 1446 | {
|
| 1447 | FLAC__ASSERT(is_valid());
|
| 1448 | return is_ogg?
|
| 1449 | (bool)::FLAC__metadata_chain_read_ogg_with_callbacks(chain_, handle, callbacks) :
|
| 1450 | (bool)::FLAC__metadata_chain_read_with_callbacks(chain_, handle, callbacks)
|
| 1451 | ;
|
| 1452 | }
|
| 1453 | |
| 1454 | bool Chain::check_if_tempfile_needed(bool use_padding)
|
| 1455 | {
|
| 1456 | FLAC__ASSERT(is_valid());
|
| 1457 | return (bool)::FLAC__metadata_chain_check_if_tempfile_needed(chain_, use_padding);
|
| 1458 | }
|
| 1459 | |
| 1460 | bool Chain::write(bool use_padding, bool preserve_file_stats)
|
| 1461 | {
|
| 1462 | FLAC__ASSERT(is_valid());
|
| 1463 | return (bool)::FLAC__metadata_chain_write(chain_, use_padding, preserve_file_stats);
|
| 1464 | }
|
| 1465 | |
| 1466 | bool Chain::write(bool use_padding, ::FLAC__IOHandle handle, ::FLAC__IOCallbacks callbacks)
|
| 1467 | {
|
| 1468 | FLAC__ASSERT(is_valid());
|
| 1469 | return (bool)::FLAC__metadata_chain_write_with_callbacks(chain_, use_padding, handle, callbacks);
|
| 1470 | }
|
| 1471 | |
| 1472 | bool Chain::write(bool use_padding, ::FLAC__IOHandle handle, ::FLAC__IOCallbacks callbacks, ::FLAC__IOHandle temp_handle, ::FLAC__IOCallbacks temp_callbacks)
|
| 1473 | {
|
| 1474 | FLAC__ASSERT(is_valid());
|
| 1475 | return (bool)::FLAC__metadata_chain_write_with_callbacks_and_tempfile(chain_, use_padding, handle, callbacks, temp_handle, temp_callbacks);
|
| 1476 | }
|
| 1477 | |
| 1478 | void Chain::merge_padding()
|
| 1479 | {
|
| 1480 | FLAC__ASSERT(is_valid());
|
| 1481 | ::FLAC__metadata_chain_merge_padding(chain_);
|
| 1482 | }
|
| 1483 | |
| 1484 | void Chain::sort_padding()
|
| 1485 | {
|
| 1486 | FLAC__ASSERT(is_valid());
|
| 1487 | ::FLAC__metadata_chain_sort_padding(chain_);
|
| 1488 | }
|
| 1489 | |
| 1490 | |
| 1491 | Iterator::Iterator():
|
| 1492 | iterator_(::FLAC__metadata_iterator_new())
|
| 1493 | { }
|
| 1494 | |
| 1495 | Iterator::~Iterator()
|
| 1496 | {
|
| 1497 | clear();
|
| 1498 | }
|
| 1499 | |
| 1500 | void Iterator::clear()
|
| 1501 | {
|
| 1502 | if(0 != iterator_)
|
| 1503 | FLAC__metadata_iterator_delete(iterator_);
|
| 1504 | iterator_ = 0;
|
| 1505 | }
|
| 1506 | |
| 1507 | bool Iterator::is_valid() const
|
| 1508 | {
|
| 1509 | return 0 != iterator_;
|
| 1510 | }
|
| 1511 | |
| 1512 | void Iterator::init(Chain &chain)
|
| 1513 | {
|
| 1514 | FLAC__ASSERT(is_valid());
|
| 1515 | FLAC__ASSERT(chain.is_valid());
|
| 1516 | ::FLAC__metadata_iterator_init(iterator_, chain.chain_);
|
| 1517 | }
|
| 1518 | |
| 1519 | bool Iterator::next()
|
| 1520 | {
|
| 1521 | FLAC__ASSERT(is_valid());
|
| 1522 | return (bool)::FLAC__metadata_iterator_next(iterator_);
|
| 1523 | }
|
| 1524 | |
| 1525 | bool Iterator::prev()
|
| 1526 | {
|
| 1527 | FLAC__ASSERT(is_valid());
|
| 1528 | return (bool)::FLAC__metadata_iterator_prev(iterator_);
|
| 1529 | }
|
| 1530 | |
| 1531 | ::FLAC__MetadataType Iterator::get_block_type() const
|
| 1532 | {
|
| 1533 | FLAC__ASSERT(is_valid());
|
| 1534 | return ::FLAC__metadata_iterator_get_block_type(iterator_);
|
| 1535 | }
|
| 1536 | |
| 1537 | Prototype *Iterator::get_block()
|
| 1538 | {
|
| 1539 | FLAC__ASSERT(is_valid());
|
| 1540 | Prototype *block = local::construct_block(::FLAC__metadata_iterator_get_block(iterator_));
|
| 1541 | if(0 != block)
|
| 1542 | block->set_reference(true);
|
| 1543 | return block;
|
| 1544 | }
|
| 1545 | |
| 1546 | bool Iterator::set_block(Prototype *block)
|
| 1547 | {
|
| 1548 | FLAC__ASSERT(0 != block);
|
| 1549 | FLAC__ASSERT(is_valid());
|
| 1550 | bool ret = (bool)::FLAC__metadata_iterator_set_block(iterator_, block->object_);
|
| 1551 | if(ret) {
|
| 1552 | block->set_reference(true);
|
| 1553 | delete block;
|
| 1554 | }
|
| 1555 | return ret;
|
| 1556 | }
|
| 1557 | |
| 1558 | bool Iterator::delete_block(bool replace_with_padding)
|
| 1559 | {
|
| 1560 | FLAC__ASSERT(is_valid());
|
| 1561 | return (bool)::FLAC__metadata_iterator_delete_block(iterator_, replace_with_padding);
|
| 1562 | }
|
| 1563 | |
| 1564 | bool Iterator::insert_block_before(Prototype *block)
|
| 1565 | {
|
| 1566 | FLAC__ASSERT(0 != block);
|
| 1567 | FLAC__ASSERT(is_valid());
|
| 1568 | bool ret = (bool)::FLAC__metadata_iterator_insert_block_before(iterator_, block->object_);
|
| 1569 | if(ret) {
|
| 1570 | block->set_reference(true);
|
| 1571 | delete block;
|
| 1572 | }
|
| 1573 | return ret;
|
| 1574 | }
|
| 1575 | |
| 1576 | bool Iterator::insert_block_after(Prototype *block)
|
| 1577 | {
|
| 1578 | FLAC__ASSERT(0 != block);
|
| 1579 | FLAC__ASSERT(is_valid());
|
| 1580 | bool ret = (bool)::FLAC__metadata_iterator_insert_block_after(iterator_, block->object_);
|
| 1581 | if(ret) {
|
| 1582 | block->set_reference(true);
|
| 1583 | delete block;
|
| 1584 | }
|
| 1585 | return ret;
|
| 1586 | }
|
| 1587 | |
| 1588 | }
|
| 1589 | }
|
src/lib/libflac/include/flac/stream_encoder.h
| r0 | r14135 | |
| 1 | /* libFLAC - Free Lossless Audio Codec library |
| 2 | * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * |
| 8 | * - Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * - Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * - Neither the name of the Xiph.org Foundation nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived from |
| 17 | * this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 26 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 27 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #ifndef FLAC__STREAM_ENCODER_H |
| 33 | #define FLAC__STREAM_ENCODER_H |
| 34 | |
| 35 | #include <stdio.h> /* for FILE */ |
| 36 | #include "export.h" |
| 37 | #include "format.h" |
| 38 | #include "stream_decoder.h" |
| 39 | |
| 40 | #ifdef __cplusplus |
| 41 | extern "C" { |
| 42 | #endif |
| 43 | |
| 44 | |
| 45 | /** \file include/FLAC/stream_encoder.h |
| 46 | * |
| 47 | * \brief |
| 48 | * This module contains the functions which implement the stream |
| 49 | * encoder. |
| 50 | * |
| 51 | * See the detailed documentation in the |
| 52 | * \link flac_stream_encoder stream encoder \endlink module. |
| 53 | */ |
| 54 | |
| 55 | /** \defgroup flac_encoder FLAC/ \*_encoder.h: encoder interfaces |
| 56 | * \ingroup flac |
| 57 | * |
| 58 | * \brief |
| 59 | * This module describes the encoder layers provided by libFLAC. |
| 60 | * |
| 61 | * The stream encoder can be used to encode complete streams either to the |
| 62 | * client via callbacks, or directly to a file, depending on how it is |
| 63 | * initialized. When encoding via callbacks, the client provides a write |
| 64 | * callback which will be called whenever FLAC data is ready to be written. |
| 65 | * If the client also supplies a seek callback, the encoder will also |
| 66 | * automatically handle the writing back of metadata discovered while |
| 67 | * encoding, like stream info, seek points offsets, etc. When encoding to |
| 68 | * a file, the client needs only supply a filename or open \c FILE* and an |
| 69 | * optional progress callback for periodic notification of progress; the |
| 70 | * write and seek callbacks are supplied internally. For more info see the |
| 71 | * \link flac_stream_encoder stream encoder \endlink module. |
| 72 | */ |
| 73 | |
| 74 | /** \defgroup flac_stream_encoder FLAC/stream_encoder.h: stream encoder interface |
| 75 | * \ingroup flac_encoder |
| 76 | * |
| 77 | * \brief |
| 78 | * This module contains the functions which implement the stream |
| 79 | * encoder. |
| 80 | * |
| 81 | * The stream encoder can encode to native FLAC, and optionally Ogg FLAC |
| 82 | * (check FLAC_API_SUPPORTS_OGG_FLAC) streams and files. |
| 83 | * |
| 84 | * The basic usage of this encoder is as follows: |
| 85 | * - The program creates an instance of an encoder using |
| 86 | * FLAC__stream_encoder_new(). |
| 87 | * - The program overrides the default settings using |
| 88 | * FLAC__stream_encoder_set_*() functions. At a minimum, the following |
| 89 | * functions should be called: |
| 90 | * - FLAC__stream_encoder_set_channels() |
| 91 | * - FLAC__stream_encoder_set_bits_per_sample() |
| 92 | * - FLAC__stream_encoder_set_sample_rate() |
| 93 | * - FLAC__stream_encoder_set_ogg_serial_number() (if encoding to Ogg FLAC) |
| 94 | * - FLAC__stream_encoder_set_total_samples_estimate() (if known) |
| 95 | * - If the application wants to control the compression level or set its own |
| 96 | * metadata, then the following should also be called: |
| 97 | * - FLAC__stream_encoder_set_compression_level() |
| 98 | * - FLAC__stream_encoder_set_verify() |
| 99 | * - FLAC__stream_encoder_set_metadata() |
| 100 | * - The rest of the set functions should only be called if the client needs |
| 101 | * exact control over how the audio is compressed; thorough understanding |
| 102 | * of the FLAC format is necessary to achieve good results. |
| 103 | * - The program initializes the instance to validate the settings and |
| 104 | * prepare for encoding using |
| 105 | * - FLAC__stream_encoder_init_stream() or FLAC__stream_encoder_init_FILE() |
| 106 | * or FLAC__stream_encoder_init_file() for native FLAC |
| 107 | * - FLAC__stream_encoder_init_ogg_stream() or FLAC__stream_encoder_init_ogg_FILE() |
| 108 | * or FLAC__stream_encoder_init_ogg_file() for Ogg FLAC |
| 109 | * - The program calls FLAC__stream_encoder_process() or |
| 110 | * FLAC__stream_encoder_process_interleaved() to encode data, which |
| 111 | * subsequently calls the callbacks when there is encoder data ready |
| 112 | * to be written. |
| 113 | * - The program finishes the encoding with FLAC__stream_encoder_finish(), |
| 114 | * which causes the encoder to encode any data still in its input pipe, |
| 115 | * update the metadata with the final encoding statistics if output |
| 116 | * seeking is possible, and finally reset the encoder to the |
| 117 | * uninitialized state. |
| 118 | * - The instance may be used again or deleted with |
| 119 | * FLAC__stream_encoder_delete(). |
| 120 | * |
| 121 | * In more detail, the stream encoder functions similarly to the |
| 122 | * \link flac_stream_decoder stream decoder \endlink, but has fewer |
| 123 | * callbacks and more options. Typically the client will create a new |
| 124 | * instance by calling FLAC__stream_encoder_new(), then set the necessary |
| 125 | * parameters with FLAC__stream_encoder_set_*(), and initialize it by |
| 126 | * calling one of the FLAC__stream_encoder_init_*() functions. |
| 127 | * |
| 128 | * Unlike the decoders, the stream encoder has many options that can |
| 129 | * affect the speed and compression ratio. When setting these parameters |
| 130 | * you should have some basic knowledge of the format (see the |
| 131 | * <A HREF="../documentation.html#format">user-level documentation</A> |
| 132 | * or the <A HREF="../format.html">formal description</A>). The |
| 133 | * FLAC__stream_encoder_set_*() functions themselves do not validate the |
| 134 | * values as many are interdependent. The FLAC__stream_encoder_init_*() |
| 135 | * functions will do this, so make sure to pay attention to the state |
| 136 | * returned by FLAC__stream_encoder_init_*() to make sure that it is |
| 137 | * FLAC__STREAM_ENCODER_INIT_STATUS_OK. Any parameters that are not set |
| 138 | * before FLAC__stream_encoder_init_*() will take on the defaults from |
| 139 | * the constructor. |
| 140 | * |
| 141 | * There are three initialization functions for native FLAC, one for |
| 142 | * setting up the encoder to encode FLAC data to the client via |
| 143 | * callbacks, and two for encoding directly to a file. |
| 144 | * |
| 145 | * For encoding via callbacks, use FLAC__stream_encoder_init_stream(). |
| 146 | * You must also supply a write callback which will be called anytime |
| 147 | * there is raw encoded data to write. If the client can seek the output |
| 148 | * it is best to also supply seek and tell callbacks, as this allows the |
| 149 | * encoder to go back after encoding is finished to write back |
| 150 | * information that was collected while encoding, like seek point offsets, |
| 151 | * frame sizes, etc. |
| 152 | * |
| 153 | * For encoding directly to a file, use FLAC__stream_encoder_init_FILE() |
| 154 | * or FLAC__stream_encoder_init_file(). Then you must only supply a |
| 155 | * filename or open \c FILE*; the encoder will handle all the callbacks |
| 156 | * internally. You may also supply a progress callback for periodic |
| 157 | * notification of the encoding progress. |
| 158 | * |
| 159 | * There are three similarly-named init functions for encoding to Ogg |
| 160 | * FLAC streams. Check \c FLAC_API_SUPPORTS_OGG_FLAC to find out if the |
| 161 | * library has been built with Ogg support. |
| 162 | * |
| 163 | * The call to FLAC__stream_encoder_init_*() currently will also immediately |
| 164 | * call the write callback several times, once with the \c fLaC signature, |
| 165 | * and once for each encoded metadata block. Note that for Ogg FLAC |
| 166 | * encoding you will usually get at least twice the number of callbacks than |
| 167 | * with native FLAC, one for the Ogg page header and one for the page body. |
| 168 | * |
| 169 | * After initializing the instance, the client may feed audio data to the |
| 170 | * encoder in one of two ways: |
| 171 | * |
| 172 | * - Channel separate, through FLAC__stream_encoder_process() - The client |
| 173 | * will pass an array of pointers to buffers, one for each channel, to |
| 174 | * the encoder, each of the same length. The samples need not be |
| 175 | * block-aligned, but each channel should have the same number of samples. |
| 176 | * - Channel interleaved, through |
| 177 | * FLAC__stream_encoder_process_interleaved() - The client will pass a single |
| 178 | * pointer to data that is channel-interleaved (i.e. channel0_sample0, |
| 179 | * channel1_sample0, ... , channelN_sample0, channel0_sample1, ...). |
| 180 | * Again, the samples need not be block-aligned but they must be |
| 181 | * sample-aligned, i.e. the first value should be channel0_sample0 and |
| 182 | * the last value channelN_sampleM. |
| 183 | * |
| 184 | * Note that for either process call, each sample in the buffers should be a |
| 185 | * signed integer, right-justified to the resolution set by |
| 186 | * FLAC__stream_encoder_set_bits_per_sample(). For example, if the resolution |
| 187 | * is 16 bits per sample, the samples should all be in the range [-32768,32767]. |
| 188 | * |
| 189 | * When the client is finished encoding data, it calls |
| 190 | * FLAC__stream_encoder_finish(), which causes the encoder to encode any |
| 191 | * data still in its input pipe, and call the metadata callback with the |
| 192 | * final encoding statistics. Then the instance may be deleted with |
| 193 | * FLAC__stream_encoder_delete() or initialized again to encode another |
| 194 | * stream. |
| 195 | * |
| 196 | * For programs that write their own metadata, but that do not know the |
| 197 | * actual metadata until after encoding, it is advantageous to instruct |
| 198 | * the encoder to write a PADDING block of the correct size, so that |
| 199 | * instead of rewriting the whole stream after encoding, the program can |
| 200 | * just overwrite the PADDING block. If only the maximum size of the |
| 201 | * metadata is known, the program can write a slightly larger padding |
| 202 | * block, then split it after encoding. |
| 203 | * |
| 204 | * Make sure you understand how lengths are calculated. All FLAC metadata |
| 205 | * blocks have a 4 byte header which contains the type and length. This |
| 206 | * length does not include the 4 bytes of the header. See the format page |
| 207 | * for the specification of metadata blocks and their lengths. |
| 208 | * |
| 209 | * \note |
| 210 | * If you are writing the FLAC data to a file via callbacks, make sure it |
| 211 | * is open for update (e.g. mode "w+" for stdio streams). This is because |
| 212 | * after the first encoding pass, the encoder will try to seek back to the |
| 213 | * beginning of the stream, to the STREAMINFO block, to write some data |
| 214 | * there. (If using FLAC__stream_encoder_init*_file() or |
| 215 | * FLAC__stream_encoder_init*_FILE(), the file is managed internally.) |
| 216 | * |
| 217 | * \note |
| 218 | * The "set" functions may only be called when the encoder is in the |
| 219 | * state FLAC__STREAM_ENCODER_UNINITIALIZED, i.e. after |
| 220 | * FLAC__stream_encoder_new() or FLAC__stream_encoder_finish(), but |
| 221 | * before FLAC__stream_encoder_init_*(). If this is the case they will |
| 222 | * return \c true, otherwise \c false. |
| 223 | * |
| 224 | * \note |
| 225 | * FLAC__stream_encoder_finish() resets all settings to the constructor |
| 226 | * defaults. |
| 227 | * |
| 228 | * \{ |
| 229 | */ |
| 230 | |
| 231 | |
| 232 | /** State values for a FLAC__StreamEncoder. |
| 233 | * |
| 234 | * The encoder's state can be obtained by calling FLAC__stream_encoder_get_state(). |
| 235 | * |
| 236 | * If the encoder gets into any other state besides \c FLAC__STREAM_ENCODER_OK |
| 237 | * or \c FLAC__STREAM_ENCODER_UNINITIALIZED, it becomes invalid for encoding and |
| 238 | * must be deleted with FLAC__stream_encoder_delete(). |
| 239 | */ |
| 240 | typedef enum { |
| 241 | |
| 242 | FLAC__STREAM_ENCODER_OK = 0, |
| 243 | /**< The encoder is in the normal OK state and samples can be processed. */ |
| 244 | |
| 245 | FLAC__STREAM_ENCODER_UNINITIALIZED, |
| 246 | /**< The encoder is in the uninitialized state; one of the |
| 247 | * FLAC__stream_encoder_init_*() functions must be called before samples |
| 248 | * can be processed. |
| 249 | */ |
| 250 | |
| 251 | FLAC__STREAM_ENCODER_OGG_ERROR, |
| 252 | /**< An error occurred in the underlying Ogg layer. */ |
| 253 | |
| 254 | FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR, |
| 255 | /**< An error occurred in the underlying verify stream decoder; |
| 256 | * check FLAC__stream_encoder_get_verify_decoder_state(). |
| 257 | */ |
| 258 | |
| 259 | FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA, |
| 260 | /**< The verify decoder detected a mismatch between the original |
| 261 | * audio signal and the decoded audio signal. |
| 262 | */ |
| 263 | |
| 264 | FLAC__STREAM_ENCODER_CLIENT_ERROR, |
| 265 | /**< One of the callbacks returned a fatal error. */ |
| 266 | |
| 267 | FLAC__STREAM_ENCODER_IO_ERROR, |
| 268 | /**< An I/O error occurred while opening/reading/writing a file. |
| 269 | * Check \c errno. |
| 270 | */ |
| 271 | |
| 272 | FLAC__STREAM_ENCODER_FRAMING_ERROR, |
| 273 | /**< An error occurred while writing the stream; usually, the |
| 274 | * write_callback returned an error. |
| 275 | */ |
| 276 | |
| 277 | FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR |
| 278 | /**< Memory allocation failed. */ |
| 279 | |
| 280 | } FLAC__StreamEncoderState; |
| 281 | |
| 282 | /** Maps a FLAC__StreamEncoderState to a C string. |
| 283 | * |
| 284 | * Using a FLAC__StreamEncoderState as the index to this array |
| 285 | * will give the string equivalent. The contents should not be modified. |
| 286 | */ |
| 287 | extern FLAC_API const char * const FLAC__StreamEncoderStateString[]; |
| 288 | |
| 289 | |
| 290 | /** Possible return values for the FLAC__stream_encoder_init_*() functions. |
| 291 | */ |
| 292 | typedef enum { |
| 293 | |
| 294 | FLAC__STREAM_ENCODER_INIT_STATUS_OK = 0, |
| 295 | /**< Initialization was successful. */ |
| 296 | |
| 297 | FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR, |
| 298 | /**< General failure to set up encoder; call FLAC__stream_encoder_get_state() for cause. */ |
| 299 | |
| 300 | FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER, |
| 301 | /**< The library was not compiled with support for the given container |
| 302 | * format. |
| 303 | */ |
| 304 | |
| 305 | FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS, |
| 306 | /**< A required callback was not supplied. */ |
| 307 | |
| 308 | FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS, |
| 309 | /**< The encoder has an invalid setting for number of channels. */ |
| 310 | |
| 311 | FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE, |
| 312 | /**< The encoder has an invalid setting for bits-per-sample. |
| 313 | * FLAC supports 4-32 bps but the reference encoder currently supports |
| 314 | * only up to 24 bps. |
| 315 | */ |
| 316 | |
| 317 | FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE, |
| 318 | /**< The encoder has an invalid setting for the input sample rate. */ |
| 319 | |
| 320 | FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE, |
| 321 | /**< The encoder has an invalid setting for the block size. */ |
| 322 | |
| 323 | FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER, |
| 324 | /**< The encoder has an invalid setting for the maximum LPC order. */ |
| 325 | |
| 326 | FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION, |
| 327 | /**< The encoder has an invalid setting for the precision of the quantized linear predictor coefficients. */ |
| 328 | |
| 329 | FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER, |
| 330 | /**< The specified block size is less than the maximum LPC order. */ |
| 331 | |
| 332 | FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE, |
| 333 | /**< The encoder is bound to the <A HREF="../format.html#subset">Subset</A> but other settings violate it. */ |
| 334 | |
| 335 | FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA, |
| 336 | /**< The metadata input to the encoder is invalid, in one of the following ways: |
| 337 | * - FLAC__stream_encoder_set_metadata() was called with a null pointer but a block count > 0 |
| 338 | * - One of the metadata blocks contains an undefined type |
| 339 | * - It contains an illegal CUESHEET as checked by FLAC__format_cuesheet_is_legal() |
| 340 | * - It contains an illegal SEEKTABLE as checked by FLAC__format_seektable_is_legal() |
| 341 | * - It contains more than one SEEKTABLE block or more than one VORBIS_COMMENT block |
| 342 | */ |
| 343 | |
| 344 | FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED |
| 345 | /**< FLAC__stream_encoder_init_*() was called when the encoder was |
| 346 | * already initialized, usually because |
| 347 | * FLAC__stream_encoder_finish() was not called. |
| 348 | */ |
| 349 | |
| 350 | } FLAC__StreamEncoderInitStatus; |
| 351 | |
| 352 | /** Maps a FLAC__StreamEncoderInitStatus to a C string. |
| 353 | * |
| 354 | * Using a FLAC__StreamEncoderInitStatus as the index to this array |
| 355 | * will give the string equivalent. The contents should not be modified. |
| 356 | */ |
| 357 | extern FLAC_API const char * const FLAC__StreamEncoderInitStatusString[]; |
| 358 | |
| 359 | |
| 360 | /** Return values for the FLAC__StreamEncoder read callback. |
| 361 | */ |
| 362 | typedef enum { |
| 363 | |
| 364 | FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE, |
| 365 | /**< The read was OK and decoding can continue. */ |
| 366 | |
| 367 | FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM, |
| 368 | /**< The read was attempted at the end of the stream. */ |
| 369 | |
| 370 | FLAC__STREAM_ENCODER_READ_STATUS_ABORT, |
| 371 | /**< An unrecoverable error occurred. */ |
| 372 | |
| 373 | FLAC__STREAM_ENCODER_READ_STATUS_UNSUPPORTED |
| 374 | /**< Client does not support reading back from the output. */ |
| 375 | |
| 376 | } FLAC__StreamEncoderReadStatus; |
| 377 | |
| 378 | /** Maps a FLAC__StreamEncoderReadStatus to a C string. |
| 379 | * |
| 380 | * Using a FLAC__StreamEncoderReadStatus as the index to this array |
| 381 | * will give the string equivalent. The contents should not be modified. |
| 382 | */ |
| 383 | extern FLAC_API const char * const FLAC__StreamEncoderReadStatusString[]; |
| 384 | |
| 385 | |
| 386 | /** Return values for the FLAC__StreamEncoder write callback. |
| 387 | */ |
| 388 | typedef enum { |
| 389 | |
| 390 | FLAC__STREAM_ENCODER_WRITE_STATUS_OK = 0, |
| 391 | /**< The write was OK and encoding can continue. */ |
| 392 | |
| 393 | FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR |
| 394 | /**< An unrecoverable error occurred. The encoder will return from the process call. */ |
| 395 | |
| 396 | } FLAC__StreamEncoderWriteStatus; |
| 397 | |
| 398 | /** Maps a FLAC__StreamEncoderWriteStatus to a C string. |
| 399 | * |
| 400 | * Using a FLAC__StreamEncoderWriteStatus as the index to this array |
| 401 | * will give the string equivalent. The contents should not be modified. |
| 402 | */ |
| 403 | extern FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[]; |
| 404 | |
| 405 | |
| 406 | /** Return values for the FLAC__StreamEncoder seek callback. |
| 407 | */ |
| 408 | typedef enum { |
| 409 | |
| 410 | FLAC__STREAM_ENCODER_SEEK_STATUS_OK, |
| 411 | /**< The seek was OK and encoding can continue. */ |
| 412 | |
| 413 | FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR, |
| 414 | /**< An unrecoverable error occurred. */ |
| 415 | |
| 416 | FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED |
| 417 | /**< Client does not support seeking. */ |
| 418 | |
| 419 | } FLAC__StreamEncoderSeekStatus; |
| 420 | |
| 421 | /** Maps a FLAC__StreamEncoderSeekStatus to a C string. |
| 422 | * |
| 423 | * Using a FLAC__StreamEncoderSeekStatus as the index to this array |
| 424 | * will give the string equivalent. The contents should not be modified. |
| 425 | */ |
| 426 | extern FLAC_API const char * const FLAC__StreamEncoderSeekStatusString[]; |
| 427 | |
| 428 | |
| 429 | /** Return values for the FLAC__StreamEncoder tell callback. |
| 430 | */ |
| 431 | typedef enum { |
| 432 | |
| 433 | FLAC__STREAM_ENCODER_TELL_STATUS_OK, |
| 434 | /**< The tell was OK and encoding can continue. */ |
| 435 | |
| 436 | FLAC__STREAM_ENCODER_TELL_STATUS_ERROR, |
| 437 | /**< An unrecoverable error occurred. */ |
| 438 | |
| 439 | FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED |
| 440 | /**< Client does not support seeking. */ |
| 441 | |
| 442 | } FLAC__StreamEncoderTellStatus; |
| 443 | |
| 444 | /** Maps a FLAC__StreamEncoderTellStatus to a C string. |
| 445 | * |
| 446 | * Using a FLAC__StreamEncoderTellStatus as the index to this array |
| 447 | * will give the string equivalent. The contents should not be modified. |
| 448 | */ |
| 449 | extern FLAC_API const char * const FLAC__StreamEncoderTellStatusString[]; |
| 450 | |
| 451 | |
| 452 | /*********************************************************************** |
| 453 | * |
| 454 | * class FLAC__StreamEncoder |
| 455 | * |
| 456 | ***********************************************************************/ |
| 457 | |
| 458 | struct FLAC__StreamEncoderProtected; |
| 459 | struct FLAC__StreamEncoderPrivate; |
| 460 | /** The opaque structure definition for the stream encoder type. |
| 461 | * See the \link flac_stream_encoder stream encoder module \endlink |
| 462 | * for a detailed description. |
| 463 | */ |
| 464 | typedef struct { |
| 465 | struct FLAC__StreamEncoderProtected *protected_; /* avoid the C++ keyword 'protected' */ |
| 466 | struct FLAC__StreamEncoderPrivate *private_; /* avoid the C++ keyword 'private' */ |
| 467 | } FLAC__StreamEncoder; |
| 468 | |
| 469 | /** Signature for the read callback. |
| 470 | * |
| 471 | * A function pointer matching this signature must be passed to |
| 472 | * FLAC__stream_encoder_init_ogg_stream() if seeking is supported. |
| 473 | * The supplied function will be called when the encoder needs to read back |
| 474 | * encoded data. This happens during the metadata callback, when the encoder |
| 475 | * has to read, modify, and rewrite the metadata (e.g. seekpoints) gathered |
| 476 | * while encoding. The address of the buffer to be filled is supplied, along |
| 477 | * with the number of bytes the buffer can hold. The callback may choose to |
| 478 | * supply less data and modify the byte count but must be careful not to |
| 479 | * overflow the buffer. The callback then returns a status code chosen from |
| 480 | * FLAC__StreamEncoderReadStatus. |
| 481 | * |
| 482 | * Here is an example of a read callback for stdio streams: |
| 483 | * \code |
| 484 | * FLAC__StreamEncoderReadStatus read_cb(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data) |
| 485 | * { |
| 486 | * FILE *file = ((MyClientData*)client_data)->file; |
| 487 | * if(*bytes > 0) { |
| 488 | * *bytes = fread(buffer, sizeof(FLAC__byte), *bytes, file); |
| 489 | * if(ferror(file)) |
| 490 | * return FLAC__STREAM_ENCODER_READ_STATUS_ABORT; |
| 491 | * else if(*bytes == 0) |
| 492 | * return FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM; |
| 493 | * else |
| 494 | * return FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE; |
| 495 | * } |
| 496 | * else |
| 497 | * return FLAC__STREAM_ENCODER_READ_STATUS_ABORT; |
| 498 | * } |
| 499 | * \endcode |
| 500 | * |
| 501 | * \note In general, FLAC__StreamEncoder functions which change the |
| 502 | * state should not be called on the \a encoder while in the callback. |
| 503 | * |
| 504 | * \param encoder The encoder instance calling the callback. |
| 505 | * \param buffer A pointer to a location for the callee to store |
| 506 | * data to be encoded. |
| 507 | * \param bytes A pointer to the size of the buffer. On entry |
| 508 | * to the callback, it contains the maximum number |
| 509 | * of bytes that may be stored in \a buffer. The |
| 510 | * callee must set it to the actual number of bytes |
| 511 | * stored (0 in case of error or end-of-stream) before |
| 512 | * returning. |
| 513 | * \param client_data The callee's client data set through |
| 514 | * FLAC__stream_encoder_set_client_data(). |
| 515 | * \retval FLAC__StreamEncoderReadStatus |
| 516 | * The callee's return status. |
| 517 | */ |
| 518 | typedef FLAC__StreamEncoderReadStatus (*FLAC__StreamEncoderReadCallback)(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data); |
| 519 | |
| 520 | /** Signature for the write callback. |
| 521 | * |
| 522 | * A function pointer matching this signature must be passed to |
| 523 | * FLAC__stream_encoder_init*_stream(). The supplied function will be called |
| 524 | * by the encoder anytime there is raw encoded data ready to write. It may |
| 525 | * include metadata mixed with encoded audio frames and the data is not |
| 526 | * guaranteed to be aligned on frame or metadata block boundaries. |
| 527 | * |
| 528 | * The only duty of the callback is to write out the \a bytes worth of data |
| 529 | * in \a buffer to the current position in the output stream. The arguments |
| 530 | * \a samples and \a current_frame are purely informational. If \a samples |
| 531 | * is greater than \c 0, then \a current_frame will hold the current frame |
| 532 | * number that is being written; otherwise it indicates that the write |
| 533 | * callback is being called to write metadata. |
| 534 | * |
| 535 | * \note |
| 536 | * Unlike when writing to native FLAC, when writing to Ogg FLAC the |
| 537 | * write callback will be called twice when writing each audio |
| 538 | * frame; once for the page header, and once for the page body. |
| 539 | * When writing the page header, the \a samples argument to the |
| 540 | * write callback will be \c 0. |
| 541 | * |
| 542 | * \note In general, FLAC__StreamEncoder functions which change the |
| 543 | * state should not be called on the \a encoder while in the callback. |
| 544 | * |
| 545 | * \param encoder The encoder instance calling the callback. |
| 546 | * \param buffer An array of encoded data of length \a bytes. |
| 547 | * \param bytes The byte length of \a buffer. |
| 548 | * \param samples The number of samples encoded by \a buffer. |
| 549 | * \c 0 has a special meaning; see above. |
| 550 | * \param current_frame The number of the current frame being encoded. |
| 551 | * \param client_data The callee's client data set through |
| 552 | * FLAC__stream_encoder_init_*(). |
| 553 | * \retval FLAC__StreamEncoderWriteStatus |
| 554 | * The callee's return status. |
| 555 | */ |
| 556 | typedef FLAC__StreamEncoderWriteStatus (*FLAC__StreamEncoderWriteCallback)(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame, void *client_data); |
| 557 | |
| 558 | /** Signature for the seek callback. |
| 559 | * |
| 560 | * A function pointer matching this signature may be passed to |
| 561 | * FLAC__stream_encoder_init*_stream(). The supplied function will be called |
| 562 | * when the encoder needs to seek the output stream. The encoder will pass |
| 563 | * the absolute byte offset to seek to, 0 meaning the beginning of the stream. |
| 564 | * |
| 565 | * Here is an example of a seek callback for stdio streams: |
| 566 | * \code |
| 567 | * FLAC__StreamEncoderSeekStatus seek_cb(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data) |
| 568 | * { |
| 569 | * FILE *file = ((MyClientData*)client_data)->file; |
| 570 | * if(file == stdin) |
| 571 | * return FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED; |
| 572 | * else if(fseeko(file, (off_t)absolute_byte_offset, SEEK_SET) < 0) |
| 573 | * return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR; |
| 574 | * else |
| 575 | * return FLAC__STREAM_ENCODER_SEEK_STATUS_OK; |
| 576 | * } |
| 577 | * \endcode |
| 578 | * |
| 579 | * \note In general, FLAC__StreamEncoder functions which change the |
| 580 | * state should not be called on the \a encoder while in the callback. |
| 581 | * |
| 582 | * \param encoder The encoder instance calling the callback. |
| 583 | * \param absolute_byte_offset The offset from the beginning of the stream |
| 584 | * to seek to. |
| 585 | * \param client_data The callee's client data set through |
| 586 | * FLAC__stream_encoder_init_*(). |
| 587 | * \retval FLAC__StreamEncoderSeekStatus |
| 588 | * The callee's return status. |
| 589 | */ |
| 590 | typedef FLAC__StreamEncoderSeekStatus (*FLAC__StreamEncoderSeekCallback)(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data); |
| 591 | |
| 592 | /** Signature for the tell callback. |
| 593 | * |
| 594 | * A function pointer matching this signature may be passed to |
| 595 | * FLAC__stream_encoder_init*_stream(). The supplied function will be called |
| 596 | * when the encoder needs to know the current position of the output stream. |
| 597 | * |
| 598 | * \warning |
| 599 | * The callback must return the true current byte offset of the output to |
| 600 | * which the encoder is writing. If you are buffering the output, make |
| 601 | * sure and take this into account. If you are writing directly to a |
| 602 | * FILE* from your write callback, ftell() is sufficient. If you are |
| 603 | * writing directly to a file descriptor from your write callback, you |
| 604 | * can use lseek(fd, SEEK_CUR, 0). The encoder may later seek back to |
| 605 | * these points to rewrite metadata after encoding. |
| 606 | * |
| 607 | * Here is an example of a tell callback for stdio streams: |
| 608 | * \code |
| 609 | * FLAC__StreamEncoderTellStatus tell_cb(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data) |
| 610 | * { |
| 611 | * FILE *file = ((MyClientData*)client_data)->file; |
| 612 | * off_t pos; |
| 613 | * if(file == stdin) |
| 614 | * return FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED; |
| 615 | * else if((pos = ftello(file)) < 0) |
| 616 | * return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR; |
| 617 | * else { |
| 618 | * *absolute_byte_offset = (FLAC__uint64)pos; |
| 619 | * return FLAC__STREAM_ENCODER_TELL_STATUS_OK; |
| 620 | * } |
| 621 | * } |
| 622 | * \endcode |
| 623 | * |
| 624 | * \note In general, FLAC__StreamEncoder functions which change the |
| 625 | * state should not be called on the \a encoder while in the callback. |
| 626 | * |
| 627 | * \param encoder The encoder instance calling the callback. |
| 628 | * \param absolute_byte_offset The address at which to store the current |
| 629 | * position of the output. |
| 630 | * \param client_data The callee's client data set through |
| 631 | * FLAC__stream_encoder_init_*(). |
| 632 | * \retval FLAC__StreamEncoderTellStatus |
| 633 | * The callee's return status. |
| 634 | */ |
| 635 | typedef FLAC__StreamEncoderTellStatus (*FLAC__StreamEncoderTellCallback)(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data); |
| 636 | |
| 637 | /** Signature for the metadata callback. |
| 638 | * |
| 639 | * A function pointer matching this signature may be passed to |
| 640 | * FLAC__stream_encoder_init*_stream(). The supplied function will be called |
| 641 | * once at the end of encoding with the populated STREAMINFO structure. This |
| 642 | * is so the client can seek back to the beginning of the file and write the |
| 643 | * STREAMINFO block with the correct statistics after encoding (like |
| 644 | * minimum/maximum frame size and total samples). |
| 645 | * |
| 646 | * \note In general, FLAC__StreamEncoder functions which change the |
| 647 | * state should not be called on the \a encoder while in the callback. |
| 648 | * |
| 649 | * \param encoder The encoder instance calling the callback. |
| 650 | * \param metadata The final populated STREAMINFO block. |
| 651 | * \param client_data The callee's client data set through |
| 652 | * FLAC__stream_encoder_init_*(). |
| 653 | */ |
| 654 | typedef void (*FLAC__StreamEncoderMetadataCallback)(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data); |
| 655 | |
| 656 | /** Signature for the progress callback. |
| 657 | * |
| 658 | * A function pointer matching this signature may be passed to |
| 659 | * FLAC__stream_encoder_init*_file() or FLAC__stream_encoder_init*_FILE(). |
| 660 | * The supplied function will be called when the encoder has finished |
| 661 | * writing a frame. The \c total_frames_estimate argument to the |
| 662 | * callback will be based on the value from |
| 663 | * FLAC__stream_encoder_set_total_samples_estimate(). |
| 664 | * |
| 665 | * \note In general, FLAC__StreamEncoder functions which change the |
| 666 | * state should not be called on the \a encoder while in the callback. |
| 667 | * |
| 668 | * \param encoder The encoder instance calling the callback. |
| 669 | * \param bytes_written Bytes written so far. |
| 670 | * \param samples_written Samples written so far. |
| 671 | * \param frames_written Frames written so far. |
| 672 | * \param total_frames_estimate The estimate of the total number of |
| 673 | * frames to be written. |
| 674 | * \param client_data The callee's client data set through |
| 675 | * FLAC__stream_encoder_init_*(). |
| 676 | */ |
| 677 | typedef void (*FLAC__StreamEncoderProgressCallback)(const FLAC__StreamEncoder *encoder, FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate, void *client_data); |
| 678 | |
| 679 | |
| 680 | /*********************************************************************** |
| 681 | * |
| 682 | * Class constructor/destructor |
| 683 | * |
| 684 | ***********************************************************************/ |
| 685 | |
| 686 | /** Create a new stream encoder instance. The instance is created with |
| 687 | * default settings; see the individual FLAC__stream_encoder_set_*() |
| 688 | * functions for each setting's default. |
| 689 | * |
| 690 | * \retval FLAC__StreamEncoder* |
| 691 | * \c NULL if there was an error allocating memory, else the new instance. |
| 692 | */ |
| 693 | FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new(void); |
| 694 | |
| 695 | /** Free an encoder instance. Deletes the object pointed to by \a encoder. |
| 696 | * |
| 697 | * \param encoder A pointer to an existing encoder. |
| 698 | * \assert |
| 699 | * \code encoder != NULL \endcode |
| 700 | */ |
| 701 | FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder); |
| 702 | |
| 703 | |
| 704 | /*********************************************************************** |
| 705 | * |
| 706 | * Public class method prototypes |
| 707 | * |
| 708 | ***********************************************************************/ |
| 709 | |
| 710 | /** Set the serial number for the FLAC stream to use in the Ogg container. |
| 711 | * |
| 712 | * \note |
| 713 | * This does not need to be set for native FLAC encoding. |
| 714 | * |
| 715 | * \note |
| 716 | * It is recommended to set a serial number explicitly as the default of '0' |
| 717 | * may collide with other streams. |
| 718 | * |
| 719 | * \default \c 0 |
| 720 | * \param encoder An encoder instance to set. |
| 721 | * \param serial_number See above. |
| 722 | * \assert |
| 723 | * \code encoder != NULL \endcode |
| 724 | * \retval FLAC__bool |
| 725 | * \c false if the encoder is already initialized, else \c true. |
| 726 | */ |
| 727 | FLAC_API FLAC__bool FLAC__stream_encoder_set_ogg_serial_number(FLAC__StreamEncoder *encoder, long serial_number); |
| 728 | |
| 729 | /** Set the "verify" flag. If \c true, the encoder will verify it's own |
| 730 | * encoded output by feeding it through an internal decoder and comparing |
| 731 | * the original signal against the decoded signal. If a mismatch occurs, |
| 732 | * the process call will return \c false. Note that this will slow the |
| 733 | * encoding process by the extra time required for decoding and comparison. |
| 734 | * |
| 735 | * \default \c false |
| 736 | * \param encoder An encoder instance to set. |
| 737 | * \param value Flag value (see above). |
| 738 | * \assert |
| 739 | * \code encoder != NULL \endcode |
| 740 | * \retval FLAC__bool |
| 741 | * \c false if the encoder is already initialized, else \c true. |
| 742 | */ |
| 743 | FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value); |
| 744 | |
| 745 | /** Set the <A HREF="../format.html#subset">Subset</A> flag. If \c true, |
| 746 | * the encoder will comply with the Subset and will check the |
| 747 | * settings during FLAC__stream_encoder_init_*() to see if all settings |
| 748 | * comply. If \c false, the settings may take advantage of the full |
| 749 | * range that the format allows. |
| 750 | * |
| 751 | * Make sure you know what it entails before setting this to \c false. |
| 752 | * |
| 753 | * \default \c true |
| 754 | * \param encoder An encoder instance to set. |
| 755 | * \param value Flag value (see above). |
| 756 | * \assert |
| 757 | * \code encoder != NULL \endcode |
| 758 | * \retval FLAC__bool |
| 759 | * \c false if the encoder is already initialized, else \c true. |
| 760 | */ |
| 761 | FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value); |
| 762 | |
| 763 | /** Set the number of channels to be encoded. |
| 764 | * |
| 765 | * \default \c 2 |
| 766 | * \param encoder An encoder instance to set. |
| 767 | * \param value See above. |
| 768 | * \assert |
| 769 | * \code encoder != NULL \endcode |
| 770 | * \retval FLAC__bool |
| 771 | * \c false if the encoder is already initialized, else \c true. |
| 772 | */ |
| 773 | FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value); |
| 774 | |
| 775 | /** Set the sample resolution of the input to be encoded. |
| 776 | * |
| 777 | * \warning |
| 778 | * Do not feed the encoder data that is wider than the value you |
| 779 | * set here or you will generate an invalid stream. |
| 780 | * |
| 781 | * \default \c 16 |
| 782 | * \param encoder An encoder instance to set. |
| 783 | * \param value See above. |
| 784 | * \assert |
| 785 | * \code encoder != NULL \endcode |
| 786 | * \retval FLAC__bool |
| 787 | * \c false if the encoder is already initialized, else \c true. |
| 788 | */ |
| 789 | FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value); |
| 790 | |
| 791 | /** Set the sample rate (in Hz) of the input to be encoded. |
| 792 | * |
| 793 | * \default \c 44100 |
| 794 | * \param encoder An encoder instance to set. |
| 795 | * \param value See above. |
| 796 | * \assert |
| 797 | * \code encoder != NULL \endcode |
| 798 | * \retval FLAC__bool |
| 799 | * \c false if the encoder is already initialized, else \c true. |
| 800 | */ |
| 801 | FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value); |
| 802 | |
| 803 | /** Set the compression level |
| 804 | * |
| 805 | * The compression level is roughly proportional to the amount of effort |
| 806 | * the encoder expends to compress the file. A higher level usually |
| 807 | * means more computation but higher compression. The default level is |
| 808 | * suitable for most applications. |
| 809 | * |
| 810 | * Currently the levels range from \c 0 (fastest, least compression) to |
| 811 | * \c 8 (slowest, most compression). A value larger than \c 8 will be |
| 812 | * treated as \c 8. |
| 813 | * |
| 814 | * This function automatically calls the following other \c _set_ |
| 815 | * functions with appropriate values, so the client does not need to |
| 816 | * unless it specifically wants to override them: |
| 817 | * - FLAC__stream_encoder_set_do_mid_side_stereo() |
| 818 | * - FLAC__stream_encoder_set_loose_mid_side_stereo() |
| 819 | * - FLAC__stream_encoder_set_apodization() |
| 820 | * - FLAC__stream_encoder_set_max_lpc_order() |
| 821 | * - FLAC__stream_encoder_set_qlp_coeff_precision() |
| 822 | * - FLAC__stream_encoder_set_do_qlp_coeff_prec_search() |
| 823 | * - FLAC__stream_encoder_set_do_escape_coding() |
| 824 | * - FLAC__stream_encoder_set_do_exhaustive_model_search() |
| 825 | * - FLAC__stream_encoder_set_min_residual_partition_order() |
| 826 | * - FLAC__stream_encoder_set_max_residual_partition_order() |
| 827 | * - FLAC__stream_encoder_set_rice_parameter_search_dist() |
| 828 | * |
| 829 | * The actual values set for each level are: |
| 830 | * <table> |
| 831 | * <tr> |
| 832 | * <td><b>level</b><td> |
| 833 | * <td>do mid-side stereo<td> |
| 834 | * <td>loose mid-side stereo<td> |
| 835 | * <td>apodization<td> |
| 836 | * <td>max lpc order<td> |
| 837 | * <td>qlp coeff precision<td> |
| 838 | * <td>qlp coeff prec search<td> |
| 839 | * <td>escape coding<td> |
| 840 | * <td>exhaustive model search<td> |
| 841 | * <td>min residual partition order<td> |
| 842 | * <td>max residual partition order<td> |
| 843 | * <td>rice parameter search dist<td> |
| 844 | * </tr> |
| 845 | * <tr> <td><b>0</b><td> <td>false<td> <td>false<td> <td>tukey(0.5)<td> <td>0<td> <td>0<td> <td>false<td> <td>false<td> <td>false<td> <td>0<td> <td>3<td> <td>0<td> </tr> |
| 846 | * <tr> <td><b>1</b><td> <td>true<td> <td>true<td> <td>tukey(0.5)<td> <td>0<td> <td>0<td> <td>false<td> <td>false<td> <td>false<td> <td>0<td> <td>3<td> <td>0<td> </tr> |
| 847 | * <tr> <td><b>2</b><td> <td>true<td> <td>false<td> <td>tukey(0.5)<td> <td>0<td> <td>0<td> <td>false<td> <td>false<td> <td>false<td> <td>0<td> <td>3<td> <td>0<td> </tr> |
| 848 | * <tr> <td><b>3</b><td> <td>false<td> <td>false<td> <td>tukey(0.5)<td> <td>6<td> <td>0<td> <td>false<td> <td>false<td> <td>false<td> <td>0<td> <td>4<td> <td>0<td> </tr> |
| 849 | * <tr> <td><b>4</b><td> <td>true<td> <td>true<td> <td>tukey(0.5)<td> <td>8<td> <td>0<td> <td>false<td> <td>false<td> <td>false<td> <td>0<td> <td>4<td> <td>0<td> </tr> |
| 850 | * <tr> <td><b>5</b><td> <td>true<td> <td>false<td> <td>tukey(0.5)<td> <td>8<td> <td>0<td> <td>false<td> <td>false<td> <td>false<td> <td>0<td> <td>5<td> <td>0<td> </tr> |
| 851 | * <tr> <td><b>6</b><td> <td>true<td> <td>false<td> <td>tukey(0.5)<td> <td>8<td> <td>0<td> <td>false<td> <td>false<td> <td>false<td> <td>0<td> <td>6<td> <td>0<td> </tr> |
| 852 | * <tr> <td><b>7</b><td> <td>true<td> <td>false<td> <td>tukey(0.5)<td> <td>8<td> <td>0<td> <td>false<td> <td>false<td> <td>true<td> <td>0<td> <td>6<td> <td>0<td> </tr> |
| 853 | * <tr> <td><b>8</b><td> <td>true<td> <td>false<td> <td>tukey(0.5)<td> <td>12<td> <td>0<td> <td>false<td> <td>false<td> <td>true<td> <td>0<td> <td>6<td> <td>0<td> </tr> |
| 854 | * </table> |
| 855 | * |
| 856 | * \default \c 5 |
| 857 | * \param encoder An encoder instance to set. |
| 858 | * \param value See above. |
| 859 | * \assert |
| 860 | * \code encoder != NULL \endcode |
| 861 | * \retval FLAC__bool |
| 862 | * \c false if the encoder is already initialized, else \c true. |
| 863 | */ |
| 864 | FLAC_API FLAC__bool FLAC__stream_encoder_set_compression_level(FLAC__StreamEncoder *encoder, unsigned value); |
| 865 | |
| 866 | /** Set the blocksize to use while encoding. |
| 867 | * |
| 868 | * The number of samples to use per frame. Use \c 0 to let the encoder |
| 869 | * estimate a blocksize; this is usually best. |
| 870 | * |
| 871 | * \default \c 0 |
| 872 | * \param encoder An encoder instance to set. |
| 873 | * \param value See above. |
| 874 | * \assert |
| 875 | * \code encoder != NULL \endcode |
| 876 | * \retval FLAC__bool |
| 877 | * \c false if the encoder is already initialized, else \c true. |
| 878 | */ |
| 879 | FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value); |
| 880 | |
| 881 | /** Set to \c true to enable mid-side encoding on stereo input. The |
| 882 | * number of channels must be 2 for this to have any effect. Set to |
| 883 | * \c false to use only independent channel coding. |
| 884 | * |
| 885 | * \default \c false |
| 886 | * \param encoder An encoder instance to set. |
| 887 | * \param value Flag value (see above). |
| 888 | * \assert |
| 889 | * \code encoder != NULL \endcode |
| 890 | * \retval FLAC__bool |
| 891 | * \c false if the encoder is already initialized, else \c true. |
| 892 | */ |
| 893 | FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value); |
| 894 | |
| 895 | /** Set to \c true to enable adaptive switching between mid-side and |
| 896 | * left-right encoding on stereo input. Set to \c false to use |
| 897 | * exhaustive searching. Setting this to \c true requires |
| 898 | * FLAC__stream_encoder_set_do_mid_side_stereo() to also be set to |
| 899 | * \c true in order to have any effect. |
| 900 | * |
| 901 | * \default \c false |
| 902 | * \param encoder An encoder instance to set. |
| 903 | * \param value Flag value (see above). |
| 904 | * \assert |
| 905 | * \code encoder != NULL \endcode |
| 906 | * \retval FLAC__bool |
| 907 | * \c false if the encoder is already initialized, else \c true. |
| 908 | */ |
| 909 | FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value); |
| 910 | |
| 911 | /** Sets the apodization function(s) the encoder will use when windowing |
| 912 | * audio data for LPC analysis. |
| 913 | * |
| 914 | * The \a specification is a plain ASCII string which specifies exactly |
| 915 | * which functions to use. There may be more than one (up to 32), |
| 916 | * separated by \c ';' characters. Some functions take one or more |
| 917 | * comma-separated arguments in parentheses. |
| 918 | * |
| 919 | * The available functions are \c bartlett, \c bartlett_hann, |
| 920 | * \c blackman, \c blackman_harris_4term_92db, \c connes, \c flattop, |
| 921 | * \c gauss(STDDEV), \c hamming, \c hann, \c kaiser_bessel, \c nuttall, |
| 922 | * \c rectangle, \c triangle, \c tukey(P), \c welch. |
| 923 | * |
| 924 | * For \c gauss(STDDEV), STDDEV specifies the standard deviation |
| 925 | * (0<STDDEV<=0.5). |
| 926 | * |
| 927 | * For \c tukey(P), P specifies the fraction of the window that is |
| 928 | * tapered (0<=P<=1). P=0 corresponds to \c rectangle and P=1 |
| 929 | * corresponds to \c hann. |
| 930 | * |
| 931 | * Example specifications are \c "blackman" or |
| 932 | * \c "hann;triangle;tukey(0.5);tukey(0.25);tukey(0.125)" |
| 933 | * |
| 934 | * Any function that is specified erroneously is silently dropped. Up |
| 935 | * to 32 functions are kept, the rest are dropped. If the specification |
| 936 | * is empty the encoder defaults to \c "tukey(0.5)". |
| 937 | * |
| 938 | * When more than one function is specified, then for every subframe the |
| 939 | * encoder will try each of them separately and choose the window that |
| 940 | * results in the smallest compressed subframe. |
| 941 | * |
| 942 | * Note that each function specified causes the encoder to occupy a |
| 943 | * floating point array in which to store the window. |
| 944 | * |
| 945 | * \default \c "tukey(0.5)" |
| 946 | * \param encoder An encoder instance to set. |
| 947 | * \param specification See above. |
| 948 | * \assert |
| 949 | * \code encoder != NULL \endcode |
| 950 | * \code specification != NULL \endcode |
| 951 | * \retval FLAC__bool |
| 952 | * \c false if the encoder is already initialized, else \c true. |
| 953 | */ |
| 954 | FLAC_API FLAC__bool FLAC__stream_encoder_set_apodization(FLAC__StreamEncoder *encoder, const char *specification); |
| 955 | |
| 956 | /** Set the maximum LPC order, or \c 0 to use only the fixed predictors. |
| 957 | * |
| 958 | * \default \c 0 |
| 959 | * \param encoder An encoder instance to set. |
| 960 | * \param value See above. |
| 961 | * \assert |
| 962 | * \code encoder != NULL \endcode |
| 963 | * \retval FLAC__bool |
| 964 | * \c false if the encoder is already initialized, else \c true. |
| 965 | */ |
| 966 | FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value); |
| 967 | |
| 968 | /** Set the precision, in bits, of the quantized linear predictor |
| 969 | * coefficients, or \c 0 to let the encoder select it based on the |
| 970 | * blocksize. |
| 971 | * |
| 972 | * \note |
| 973 | * In the current implementation, qlp_coeff_precision + bits_per_sample must |
| 974 | * be less than 32. |
| 975 | * |
| 976 | * \default \c 0 |
| 977 | * \param encoder An encoder instance to set. |
| 978 | * \param value See above. |
| 979 | * \assert |
| 980 | * \code encoder != NULL \endcode |
| 981 | * \retval FLAC__bool |
| 982 | * \c false if the encoder is already initialized, else \c true. |
| 983 | */ |
| 984 | FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value); |
| 985 | |
| 986 | /** Set to \c false to use only the specified quantized linear predictor |
| 987 | * coefficient precision, or \c true to search neighboring precision |
| 988 | * values and use the best one. |
| 989 | * |
| 990 | * \default \c false |
| 991 | * \param encoder An encoder instance to set. |
| 992 | * \param value See above. |
| 993 | * \assert |
| 994 | * \code encoder != NULL \endcode |
| 995 | * \retval FLAC__bool |
| 996 | * \c false if the encoder is already initialized, else \c true. |
| 997 | */ |
| 998 | FLAC_API FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value); |
| 999 | |
| 1000 | /** Deprecated. Setting this value has no effect. |
| 1001 | * |
| 1002 | * \default \c false |
| 1003 | * \param encoder An encoder instance to set. |
| 1004 | * \param value See above. |
| 1005 | * \assert |
| 1006 | * \code encoder != NULL \endcode |
| 1007 | * \retval FLAC__bool |
| 1008 | * \c false if the encoder is already initialized, else \c true. |
| 1009 | */ |
| 1010 | FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value); |
| 1011 | |
| 1012 | /** Set to \c false to let the encoder estimate the best model order |
| 1013 | * based on the residual signal energy, or \c true to force the |
| 1014 | * encoder to evaluate all order models and select the best. |
| 1015 | * |
| 1016 | * \default \c false |
| 1017 | * \param encoder An encoder instance to set. |
| 1018 | * \param value See above. |
| 1019 | * \assert |
| 1020 | * \code encoder != NULL \endcode |
| 1021 | * \retval FLAC__bool |
| 1022 | * \c false if the encoder is already initialized, else \c true. |
| 1023 | */ |
| 1024 | FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value); |
| 1025 | |
| 1026 | /** Set the minimum partition order to search when coding the residual. |
| 1027 | * This is used in tandem with |
| 1028 | * FLAC__stream_encoder_set_max_residual_partition_order(). |
| 1029 | * |
| 1030 | * The partition order determines the context size in the residual. |
| 1031 | * The context size will be approximately <tt>blocksize / (2 ^ order)</tt>. |
| 1032 | * |
| 1033 | * Set both min and max values to \c 0 to force a single context, |
| 1034 | * whose Rice parameter is based on the residual signal variance. |
| 1035 | * Otherwise, set a min and max order, and the encoder will search |
| 1036 | * all orders, using the mean of each context for its Rice parameter, |
| 1037 | * and use the best. |
| 1038 | * |
| 1039 | * \default \c 0 |
| 1040 | * \param encoder An encoder instance to set. |
| 1041 | * \param value See above. |
| 1042 | * \assert |
| 1043 | * \code encoder != NULL \endcode |
| 1044 | * \retval FLAC__bool |
| 1045 | * \c false if the encoder is already initialized, else \c true. |
| 1046 | */ |
| 1047 | FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value); |
| 1048 | |
| 1049 | /** Set the maximum partition order to search when coding the residual. |
| 1050 | * This is used in tandem with |
| 1051 | * FLAC__stream_encoder_set_min_residual_partition_order(). |
| 1052 | * |
| 1053 | * The partition order determines the context size in the residual. |
| 1054 | * The context size will be approximately <tt>blocksize / (2 ^ order)</tt>. |
| 1055 | * |
| 1056 | * Set both min and max values to \c 0 to force a single context, |
| 1057 | * whose Rice parameter is based on the residual signal variance. |
| 1058 | * Otherwise, set a min and max order, and the encoder will search |
| 1059 | * all orders, using the mean of each context for its Rice parameter, |
| 1060 | * and use the best. |
| 1061 | * |
| 1062 | * \default \c 0 |
| 1063 | * \param encoder An encoder instance to set. |
| 1064 | * \param value See above. |
| 1065 | * \assert |
| 1066 | * \code encoder != NULL \endcode |
| 1067 | * \retval FLAC__bool |
| 1068 | * \c false if the encoder is already initialized, else \c true. |
| 1069 | */ |
| 1070 | FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value); |
| 1071 | |
| 1072 | /** Deprecated. Setting this value has no effect. |
| 1073 | * |
| 1074 | * \default \c 0 |
| 1075 | * \param encoder An encoder instance to set. |
| 1076 | * \param value See above. |
| 1077 | * \assert |
| 1078 | * \code encoder != NULL \endcode |
| 1079 | * \retval FLAC__bool |
| 1080 | * \c false if the encoder is already initialized, else \c true. |
| 1081 | */ |
| 1082 | FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value); |
| 1083 | |
| 1084 | /** Set an estimate of the total samples that will be encoded. |
| 1085 | * This is merely an estimate and may be set to \c 0 if unknown. |
| 1086 | * This value will be written to the STREAMINFO block before encoding, |
| 1087 | * and can remove the need for the caller to rewrite the value later |
| 1088 | * if the value is known before encoding. |
| 1089 | * |
| 1090 | * \default \c 0 |
| 1091 | * \param encoder An encoder instance to set. |
| 1092 | * \param value See above. |
| 1093 | * \assert |
| 1094 | * \code encoder != NULL \endcode |
| 1095 | * \retval FLAC__bool |
| 1096 | * \c false if the encoder is already initialized, else \c true. |
| 1097 | */ |
| 1098 | FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value); |
| 1099 | |
| 1100 | /** Set the metadata blocks to be emitted to the stream before encoding. |
| 1101 | * A value of \c NULL, \c 0 implies no metadata; otherwise, supply an |
| 1102 | * array of pointers to metadata blocks. The array is non-const since |
| 1103 | * the encoder may need to change the \a is_last flag inside them, and |
| 1104 | * in some cases update seek point offsets. Otherwise, the encoder will |
| 1105 | * not modify or free the blocks. It is up to the caller to free the |
| 1106 | * metadata blocks after encoding finishes. |
| 1107 | * |
| 1108 | * \note |
| 1109 | * The encoder stores only copies of the pointers in the \a metadata array; |
| 1110 | * the metadata blocks themselves must survive at least until after |
| 1111 | * FLAC__stream_encoder_finish() returns. Do not free the blocks until then. |
| 1112 | * |
| 1113 | * \note |
| 1114 | * The STREAMINFO block is always written and no STREAMINFO block may |
| 1115 | * occur in the supplied array. |
| 1116 | * |
| 1117 | * \note |
| 1118 | * By default the encoder does not create a SEEKTABLE. If one is supplied |
| 1119 | * in the \a metadata array, but the client has specified that it does not |
| 1120 | * support seeking, then the SEEKTABLE will be written verbatim. However |
| 1121 | * by itself this is not very useful as the client will not know the stream |
| 1122 | * offsets for the seekpoints ahead of time. In order to get a proper |
| 1123 | * seektable the client must support seeking. See next note. |
| 1124 | * |
| 1125 | * \note |
| 1126 | * SEEKTABLE blocks are handled specially. Since you will not know |
| 1127 | * the values for the seek point stream offsets, you should pass in |
| 1128 | * a SEEKTABLE 'template', that is, a SEEKTABLE object with the |
| 1129 | * required sample numbers (or placeholder points), with \c 0 for the |
| 1130 | * \a frame_samples and \a stream_offset fields for each point. If the |
| 1131 | * client has specified that it supports seeking by providing a seek |
| 1132 | * callback to FLAC__stream_encoder_init_stream() or both seek AND read |
| 1133 | * callback to FLAC__stream_encoder_init_ogg_stream() (or by using |
| 1134 | * FLAC__stream_encoder_init*_file() or FLAC__stream_encoder_init*_FILE()), |
| 1135 | * then while it is encoding the encoder will fill the stream offsets in |
| 1136 | * for you and when encoding is finished, it will seek back and write the |
| 1137 | * real values into the SEEKTABLE block in the stream. There are helper |
| 1138 | * routines for manipulating seektable template blocks; see metadata.h: |
| 1139 | * FLAC__metadata_object_seektable_template_*(). If the client does |
| 1140 | * not support seeking, the SEEKTABLE will have inaccurate offsets which |
| 1141 | * will slow down or remove the ability to seek in the FLAC stream. |
| 1142 | * |
| 1143 | * \note |
| 1144 | * The encoder instance \b will modify the first \c SEEKTABLE block |
| 1145 | * as it transforms the template to a valid seektable while encoding, |
| 1146 | * but it is still up to the caller to free all metadata blocks after |
| 1147 | * encoding. |
| 1148 | * |
| 1149 | * \note |
| 1150 | * A VORBIS_COMMENT block may be supplied. The vendor string in it |
| 1151 | * will be ignored. libFLAC will use it's own vendor string. libFLAC |
| 1152 | * will not modify the passed-in VORBIS_COMMENT's vendor string, it |
| 1153 | * will simply write it's own into the stream. If no VORBIS_COMMENT |
| 1154 | * block is present in the \a metadata array, libFLAC will write an |
| 1155 | * empty one, containing only the vendor string. |
| 1156 | * |
| 1157 | * \note The Ogg FLAC mapping requires that the VORBIS_COMMENT block be |
| 1158 | * the second metadata block of the stream. The encoder already supplies |
| 1159 | * the STREAMINFO block automatically. If \a metadata does not contain a |
| 1160 | * VORBIS_COMMENT block, the encoder will supply that too. Otherwise, if |
| 1161 | * \a metadata does contain a VORBIS_COMMENT block and it is not the |
| 1162 | * first, the init function will reorder \a metadata by moving the |
| 1163 | * VORBIS_COMMENT block to the front; the relative ordering of the other |
| 1164 | * blocks will remain as they were. |
| 1165 | * |
| 1166 | * \note The Ogg FLAC mapping limits the number of metadata blocks per |
| 1167 | * stream to \c 65535. If \a num_blocks exceeds this the function will |
| 1168 | * return \c false. |
| 1169 | * |
| 1170 | * \default \c NULL, 0 |
| 1171 | * \param encoder An encoder instance to set. |
| 1172 | * \param metadata See above. |
| 1173 | * \param num_blocks See above. |
| 1174 | * \assert |
| 1175 | * \code encoder != NULL \endcode |
| 1176 | * \retval FLAC__bool |
| 1177 | * \c false if the encoder is already initialized, else \c true. |
| 1178 | * \c false if the encoder is already initialized, or if |
| 1179 | * \a num_blocks > 65535 if encoding to Ogg FLAC, else \c true. |
| 1180 | */ |
| 1181 | FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks); |
| 1182 | |
| 1183 | /** Get the current encoder state. |
| 1184 | * |
| 1185 | * \param encoder An encoder instance to query. |
| 1186 | * \assert |
| 1187 | * \code encoder != NULL \endcode |
| 1188 | * \retval FLAC__StreamEncoderState |
| 1189 | * The current encoder state. |
| 1190 | */ |
| 1191 | FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder); |
| 1192 | |
| 1193 | /** Get the state of the verify stream decoder. |
| 1194 | * Useful when the stream encoder state is |
| 1195 | * \c FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR. |
| 1196 | * |
| 1197 | * \param encoder An encoder instance to query. |
| 1198 | * \assert |
| 1199 | * \code encoder != NULL \endcode |
| 1200 | * \retval FLAC__StreamDecoderState |
| 1201 | * The verify stream decoder state. |
| 1202 | */ |
| 1203 | FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder); |
| 1204 | |
| 1205 | /** Get the current encoder state as a C string. |
| 1206 | * This version automatically resolves |
| 1207 | * \c FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR by getting the |
| 1208 | * verify decoder's state. |
| 1209 | * |
| 1210 | * \param encoder A encoder instance to query. |
| 1211 | * \assert |
| 1212 | * \code encoder != NULL \endcode |
| 1213 | * \retval const char * |
| 1214 | * The encoder state as a C string. Do not modify the contents. |
| 1215 | */ |
| 1216 | FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder); |
| 1217 | |
| 1218 | /** Get relevant values about the nature of a verify decoder error. |
| 1219 | * Useful when the stream encoder state is |
| 1220 | * \c FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR. The arguments should |
| 1221 | * be addresses in which the stats will be returned, or NULL if value |
| 1222 | * is not desired. |
| 1223 | * |
| 1224 | * \param encoder An encoder instance to query. |
| 1225 | * \param absolute_sample The absolute sample number of the mismatch. |
| 1226 | * \param frame_number The number of the frame in which the mismatch occurred. |
| 1227 | * \param channel The channel in which the mismatch occurred. |
| 1228 | * \param sample The number of the sample (relative to the frame) in |
| 1229 | * which the mismatch occurred. |
| 1230 | * \param expected The expected value for the sample in question. |
| 1231 | * \param got The actual value returned by the decoder. |
| 1232 | * \assert |
| 1233 | * \code encoder != NULL \endcode |
| 1234 | */ |
| 1235 | FLAC_API void FLAC__stream_encoder_get_verify_decoder_error_stats(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_sample, unsigned *frame_number, unsigned *channel, unsigned *sample, FLAC__int32 *expected, FLAC__int32 *got); |
| 1236 | |
| 1237 | /** Get the "verify" flag. |
| 1238 | * |
| 1239 | * \param encoder An encoder instance to query. |
| 1240 | * \assert |
| 1241 | * \code encoder != NULL \endcode |
| 1242 | * \retval FLAC__bool |
| 1243 | * See FLAC__stream_encoder_set_verify(). |
| 1244 | */ |
| 1245 | FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder); |
| 1246 | |
| 1247 | /** Get the <A HREF="../format.html#subset>Subset</A> flag. |
| 1248 | * |
| 1249 | * \param encoder An encoder instance to query. |
| 1250 | * \assert |
| 1251 | * \code encoder != NULL \endcode |
| 1252 | * \retval FLAC__bool |
| 1253 | * See FLAC__stream_encoder_set_streamable_subset(). |
| 1254 | */ |
| 1255 | FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder); |
| 1256 | |
| 1257 | /** Get the number of input channels being processed. |
| 1258 | * |
| 1259 | * \param encoder An encoder instance to query. |
| 1260 | * \assert |
| 1261 | * \code encoder != NULL \endcode |
| 1262 | * \retval unsigned |
| 1263 | * See FLAC__stream_encoder_set_channels(). |
| 1264 | */ |
| 1265 | FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder); |
| 1266 | |
| 1267 | /** Get the input sample resolution setting. |
| 1268 | * |
| 1269 | * \param encoder An encoder instance to query. |
| 1270 | * \assert |
| 1271 | * \code encoder != NULL \endcode |
| 1272 | * \retval unsigned |
| 1273 | * See FLAC__stream_encoder_set_bits_per_sample(). |
| 1274 | */ |
| 1275 | FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder); |
| 1276 | |
| 1277 | /** Get the input sample rate setting. |
| 1278 | * |
| 1279 | * \param encoder An encoder instance to query. |
| 1280 | * \assert |
| 1281 | * \code encoder != NULL \endcode |
| 1282 | * \retval unsigned |
| 1283 | * See FLAC__stream_encoder_set_sample_rate(). |
| 1284 | */ |
| 1285 | FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder); |
| 1286 | |
| 1287 | /** Get the blocksize setting. |
| 1288 | * |
| 1289 | * \param encoder An encoder instance to query. |
| 1290 | * \assert |
| 1291 | * \code encoder != NULL \endcode |
| 1292 | * \retval unsigned |
| 1293 | * See FLAC__stream_encoder_set_blocksize(). |
| 1294 | */ |
| 1295 | FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder); |
| 1296 | |
| 1297 | /** Get the "mid/side stereo coding" flag. |
| 1298 | * |
| 1299 | * \param encoder An encoder instance to query. |
| 1300 | * \assert |
| 1301 | * \code encoder != NULL \endcode |
| 1302 | * \retval FLAC__bool |
| 1303 | * See FLAC__stream_encoder_get_do_mid_side_stereo(). |
| 1304 | */ |
| 1305 | FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder); |
| 1306 | |
| 1307 | /** Get the "adaptive mid/side switching" flag. |
| 1308 | * |
| 1309 | * \param encoder An encoder instance to query. |
| 1310 | * \assert |
| 1311 | * \code encoder != NULL \endcode |
| 1312 | * \retval FLAC__bool |
| 1313 | * See FLAC__stream_encoder_set_loose_mid_side_stereo(). |
| 1314 | */ |
| 1315 | FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder); |
| 1316 | |
| 1317 | /** Get the maximum LPC order setting. |
| 1318 | * |
| 1319 | * \param encoder An encoder instance to query. |
| 1320 | * \assert |
| 1321 | * \code encoder != NULL \endcode |
| 1322 | * \retval unsigned |
| 1323 | * See FLAC__stream_encoder_set_max_lpc_order(). |
| 1324 | */ |
| 1325 | FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder); |
| 1326 | |
| 1327 | /** Get the quantized linear predictor coefficient precision setting. |
| 1328 | * |
| 1329 | * \param encoder An encoder instance to query. |
| 1330 | * \assert |
| 1331 | * \code encoder != NULL \endcode |
| 1332 | * \retval unsigned |
| 1333 | * See FLAC__stream_encoder_set_qlp_coeff_precision(). |
| 1334 | */ |
| 1335 | FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder); |
| 1336 | |
| 1337 | /** Get the qlp coefficient precision search flag. |
| 1338 | * |
| 1339 | * \param encoder An encoder instance to query. |
| 1340 | * \assert |
| 1341 | * \code encoder != NULL \endcode |
| 1342 | * \retval FLAC__bool |
| 1343 | * See FLAC__stream_encoder_set_do_qlp_coeff_prec_search(). |
| 1344 | */ |
| 1345 | FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder); |
| 1346 | |
| 1347 | /** Get the "escape coding" flag. |
| 1348 | * |
| 1349 | * \param encoder An encoder instance to query. |
| 1350 | * \assert |
| 1351 | * \code encoder != NULL \endcode |
| 1352 | * \retval FLAC__bool |
| 1353 | * See FLAC__stream_encoder_set_do_escape_coding(). |
| 1354 | */ |
| 1355 | FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder); |
| 1356 | |
| 1357 | /** Get the exhaustive model search flag. |
| 1358 | * |
| 1359 | * \param encoder An encoder instance to query. |
| 1360 | * \assert |
| 1361 | * \code encoder != NULL \endcode |
| 1362 | * \retval FLAC__bool |
| 1363 | * See FLAC__stream_encoder_set_do_exhaustive_model_search(). |
| 1364 | */ |
| 1365 | FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder); |
| 1366 | |
| 1367 | /** Get the minimum residual partition order setting. |
| 1368 | * |
| 1369 | * \param encoder An encoder instance to query. |
| 1370 | * \assert |
| 1371 | * \code encoder != NULL \endcode |
| 1372 | * \retval unsigned |
| 1373 | * See FLAC__stream_encoder_set_min_residual_partition_order(). |
| 1374 | */ |
| 1375 | FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder); |
| 1376 | |
| 1377 | /** Get maximum residual partition order setting. |
| 1378 | * |
| 1379 | * \param encoder An encoder instance to query. |
| 1380 | * \assert |
| 1381 | * \code encoder != NULL \endcode |
| 1382 | * \retval unsigned |
| 1383 | * See FLAC__stream_encoder_set_max_residual_partition_order(). |
| 1384 | */ |
| 1385 | FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder); |
| 1386 | |
| 1387 | /** Get the Rice parameter search distance setting. |
| 1388 | * |
| 1389 | * \param encoder An encoder instance to query. |
| 1390 | * \assert |
| 1391 | * \code encoder != NULL \endcode |
| 1392 | * \retval unsigned |
| 1393 | * See FLAC__stream_encoder_set_rice_parameter_search_dist(). |
| 1394 | */ |
| 1395 | FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder); |
| 1396 | |
| 1397 | /** Get the previously set estimate of the total samples to be encoded. |
| 1398 | * The encoder merely mimics back the value given to |
| 1399 | * FLAC__stream_encoder_set_total_samples_estimate() since it has no |
| 1400 | * other way of knowing how many samples the client will encode. |
| 1401 | * |
| 1402 | * \param encoder An encoder instance to set. |
| 1403 | * \assert |
| 1404 | * \code encoder != NULL \endcode |
| 1405 | * \retval FLAC__uint64 |
| 1406 | * See FLAC__stream_encoder_get_total_samples_estimate(). |
| 1407 | */ |
| 1408 | FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder); |
| 1409 | |
| 1410 | /** Initialize the encoder instance to encode native FLAC streams. |
| 1411 | * |
| 1412 | * This flavor of initialization sets up the encoder to encode to a |
| 1413 | * native FLAC stream. I/O is performed via callbacks to the client. |
| 1414 | * For encoding to a plain file via filename or open \c FILE*, |
| 1415 | * FLAC__stream_encoder_init_file() and FLAC__stream_encoder_init_FILE() |
| 1416 | * provide a simpler interface. |
| 1417 | * |
| 1418 | * This function should be called after FLAC__stream_encoder_new() and |
| 1419 | * FLAC__stream_encoder_set_*() but before FLAC__stream_encoder_process() |
| 1420 | * or FLAC__stream_encoder_process_interleaved(). |
| 1421 | * initialization succeeded. |
| 1422 | * |
| 1423 | * The call to FLAC__stream_encoder_init_stream() currently will also |
| 1424 | * immediately call the write callback several times, once with the \c fLaC |
| 1425 | * signature, and once for each encoded metadata block. |
| 1426 | * |
| 1427 | * \param encoder An uninitialized encoder instance. |
| 1428 | * \param write_callback See FLAC__StreamEncoderWriteCallback. This |
| 1429 | * pointer must not be \c NULL. |
| 1430 | * \param seek_callback See FLAC__StreamEncoderSeekCallback. This |
| 1431 | * pointer may be \c NULL if seeking is not |
| 1432 | * supported. The encoder uses seeking to go back |
| 1433 | * and write some some stream statistics to the |
| 1434 | * STREAMINFO block; this is recommended but not |
| 1435 | * necessary to create a valid FLAC stream. If |
| 1436 | * \a seek_callback is not \c NULL then a |
| 1437 | * \a tell_callback must also be supplied. |
| 1438 | * Alternatively, a dummy seek callback that just |
| 1439 | * returns \c FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED |
| 1440 | * may also be supplied, all though this is slightly |
| 1441 | * less efficient for the encoder. |
| 1442 | * \param tell_callback See FLAC__StreamEncoderTellCallback. This |
| 1443 | * pointer may be \c NULL if seeking is not |
| 1444 | * supported. If \a seek_callback is \c NULL then |
| 1445 | * this argument will be ignored. If |
| 1446 | * \a seek_callback is not \c NULL then a |
| 1447 | * \a tell_callback must also be supplied. |
| 1448 | * Alternatively, a dummy tell callback that just |
| 1449 | * returns \c FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED |
| 1450 | * may also be supplied, all though this is slightly |
| 1451 | * less efficient for the encoder. |
| 1452 | * \param metadata_callback See FLAC__StreamEncoderMetadataCallback. This |
| 1453 | * pointer may be \c NULL if the callback is not |
| 1454 | * desired. If the client provides a seek callback, |
| 1455 | * this function is not necessary as the encoder |
| 1456 | * will automatically seek back and update the |
| 1457 | * STREAMINFO block. It may also be \c NULL if the |
| 1458 | * client does not support seeking, since it will |
| 1459 | * have no way of going back to update the |
| 1460 | * STREAMINFO. However the client can still supply |
| 1461 | * a callback if it would like to know the details |
| 1462 | * from the STREAMINFO. |
| 1463 | * \param client_data This value will be supplied to callbacks in their |
| 1464 | * \a client_data argument. |
| 1465 | * \assert |
| 1466 | * \code encoder != NULL \endcode |
| 1467 | * \retval FLAC__StreamEncoderInitStatus |
| 1468 | * \c FLAC__STREAM_ENCODER_INIT_STATUS_OK if initialization was successful; |
| 1469 | * see FLAC__StreamEncoderInitStatus for the meanings of other return values. |
| 1470 | */ |
| 1471 | FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_stream(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteCallback write_callback, FLAC__StreamEncoderSeekCallback seek_callback, FLAC__StreamEncoderTellCallback tell_callback, FLAC__StreamEncoderMetadataCallback metadata_callback, void *client_data); |
| 1472 | |
| 1473 | /** Initialize the encoder instance to encode Ogg FLAC streams. |
| 1474 | * |
| 1475 | * This flavor of initialization sets up the encoder to encode to a FLAC |
| 1476 | * stream in an Ogg container. I/O is performed via callbacks to the |
| 1477 | * client. For encoding to a plain file via filename or open \c FILE*, |
| 1478 | * FLAC__stream_encoder_init_ogg_file() and FLAC__stream_encoder_init_ogg_FILE() |
| 1479 | * provide a simpler interface. |
| 1480 | * |
| 1481 | * This function should be called after FLAC__stream_encoder_new() and |
| 1482 | * FLAC__stream_encoder_set_*() but before FLAC__stream_encoder_process() |
| 1483 | * or FLAC__stream_encoder_process_interleaved(). |
| 1484 | * initialization succeeded. |
| 1485 | * |
| 1486 | * The call to FLAC__stream_encoder_init_ogg_stream() currently will also |
| 1487 | * immediately call the write callback several times to write the metadata |
| 1488 | * packets. |
| 1489 | * |
| 1490 | * \param encoder An uninitialized encoder instance. |
| 1491 | * \param read_callback See FLAC__StreamEncoderReadCallback. This |
| 1492 | * pointer must not be \c NULL if \a seek_callback |
| 1493 | * is non-NULL since they are both needed to be |
| 1494 | * able to write data back to the Ogg FLAC stream |
| 1495 | * in the post-encode phase. |
| 1496 | * \param write_callback See FLAC__StreamEncoderWriteCallback. This |
| 1497 | * pointer must not be \c NULL. |
| 1498 | * \param seek_callback See FLAC__StreamEncoderSeekCallback. This |
| 1499 | * pointer may be \c NULL if seeking is not |
| 1500 | * supported. The encoder uses seeking to go back |
| 1501 | * and write some some stream statistics to the |
| 1502 | * STREAMINFO block; this is recommended but not |
| 1503 | * necessary to create a valid FLAC stream. If |
| 1504 | * \a seek_callback is not \c NULL then a |
| 1505 | * \a tell_callback must also be supplied. |
| 1506 | * Alternatively, a dummy seek callback that just |
| 1507 | * returns \c FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED |
| 1508 | * may also be supplied, all though this is slightly |
| 1509 | * less efficient for the encoder. |
| 1510 | * \param tell_callback See FLAC__StreamEncoderTellCallback. This |
| 1511 | * pointer may be \c NULL if seeking is not |
| 1512 | * supported. If \a seek_callback is \c NULL then |
| 1513 | * this argument will be ignored. If |
| 1514 | * \a seek_callback is not \c NULL then a |
| 1515 | * \a tell_callback must also be supplied. |
| 1516 | * Alternatively, a dummy tell callback that just |
| 1517 | * returns \c FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED |
| 1518 | * may also be supplied, all though this is slightly |
| 1519 | * less efficient for the encoder. |
| 1520 | * \param metadata_callback See FLAC__StreamEncoderMetadataCallback. This |
| 1521 | * pointer may be \c NULL if the callback is not |
| 1522 | * desired. If the client provides a seek callback, |
| 1523 | * this function is not necessary as the encoder |
| 1524 | * will automatically seek back and update the |
| 1525 | * STREAMINFO block. It may also be \c NULL if the |
| 1526 | * client does not support seeking, since it will |
| 1527 | * have no way of going back to update the |
| 1528 | * STREAMINFO. However the client can still supply |
| 1529 | * a callback if it would like to know the details |
| 1530 | * from the STREAMINFO. |
| 1531 | * \param client_data This value will be supplied to callbacks in their |
| 1532 | * \a client_data argument. |
| 1533 | * \assert |
| 1534 | * \code encoder != NULL \endcode |
| 1535 | * \retval FLAC__StreamEncoderInitStatus |
| 1536 | * \c FLAC__STREAM_ENCODER_INIT_STATUS_OK if initialization was successful; |
| 1537 | * see FLAC__StreamEncoderInitStatus for the meanings of other return values. |
| 1538 | */ |
| 1539 | FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_stream(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderReadCallback read_callback, FLAC__StreamEncoderWriteCallback write_callback, FLAC__StreamEncoderSeekCallback seek_callback, FLAC__StreamEncoderTellCallback tell_callback, FLAC__StreamEncoderMetadataCallback metadata_callback, void *client_data); |
| 1540 | |
| 1541 | /** Initialize the encoder instance to encode native FLAC files. |
| 1542 | * |
| 1543 | * This flavor of initialization sets up the encoder to encode to a |
| 1544 | * plain native FLAC file. For non-stdio streams, you must use |
| 1545 | * FLAC__stream_encoder_init_stream() and provide callbacks for the I/O. |
| 1546 | * |
| 1547 | * This function should be called after FLAC__stream_encoder_new() and |
| 1548 | * FLAC__stream_encoder_set_*() but before FLAC__stream_encoder_process() |
| 1549 | * or FLAC__stream_encoder_process_interleaved(). |
| 1550 | * initialization succeeded. |
| 1551 | * |
| 1552 | * \param encoder An uninitialized encoder instance. |
| 1553 | * \param file An open file. The file should have been opened |
| 1554 | * with mode \c "w+b" and rewound. The file |
| 1555 | * becomes owned by the encoder and should not be |
| 1556 | * manipulated by the client while encoding. |
| 1557 | * Unless \a file is \c stdout, it will be closed |
| 1558 | * when FLAC__stream_encoder_finish() is called. |
| 1559 | * Note however that a proper SEEKTABLE cannot be |
| 1560 | * created when encoding to \c stdout since it is |
| 1561 | * not seekable. |
| 1562 | * \param progress_callback See FLAC__StreamEncoderProgressCallback. This |
| 1563 | * pointer may be \c NULL if the callback is not |
| 1564 | * desired. |
| 1565 | * \param client_data This value will be supplied to callbacks in their |
| 1566 | * \a client_data argument. |
| 1567 | * \assert |
| 1568 | * \code encoder != NULL \endcode |
| 1569 | * \code file != NULL \endcode |
| 1570 | * \retval FLAC__StreamEncoderInitStatus |
| 1571 | * \c FLAC__STREAM_ENCODER_INIT_STATUS_OK if initialization was successful; |
| 1572 | * see FLAC__StreamEncoderInitStatus for the meanings of other return values. |
| 1573 | */ |
| 1574 | FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_FILE(FLAC__StreamEncoder *encoder, FILE *file, FLAC__StreamEncoderProgressCallback progress_callback, void *client_data); |
| 1575 | |
| 1576 | /** Initialize the encoder instance to encode Ogg FLAC files. |
| 1577 | * |
| 1578 | * This flavor of initialization sets up the encoder to encode to a |
| 1579 | * plain Ogg FLAC file. For non-stdio streams, you must use |
| 1580 | * FLAC__stream_encoder_init_ogg_stream() and provide callbacks for the I/O. |
| 1581 | * |
| 1582 | * This function should be called after FLAC__stream_encoder_new() and |
| 1583 | * FLAC__stream_encoder_set_*() but before FLAC__stream_encoder_process() |
| 1584 | * or FLAC__stream_encoder_process_interleaved(). |
| 1585 | * initialization succeeded. |
| 1586 | * |
| 1587 | * \param encoder An uninitialized encoder instance. |
| 1588 | * \param file An open file. The file should have been opened |
| 1589 | * with mode \c "w+b" and rewound. The file |
| 1590 | * becomes owned by the encoder and should not be |
| 1591 | * manipulated by the client while encoding. |
| 1592 | * Unless \a file is \c stdout, it will be closed |
| 1593 | * when FLAC__stream_encoder_finish() is called. |
| 1594 | * Note however that a proper SEEKTABLE cannot be |
| 1595 | * created when encoding to \c stdout since it is |
| 1596 | * not seekable. |
| 1597 | * \param progress_callback See FLAC__StreamEncoderProgressCallback. This |
| 1598 | * pointer may be \c NULL if the callback is not |
| 1599 | * desired. |
| 1600 | * \param client_data This value will be supplied to callbacks in their |
| 1601 | * \a client_data argument. |
| 1602 | * \assert |
| 1603 | * \code encoder != NULL \endcode |
| 1604 | * \code file != NULL \endcode |
| 1605 | * \retval FLAC__StreamEncoderInitStatus |
| 1606 | * \c FLAC__STREAM_ENCODER_INIT_STATUS_OK if initialization was successful; |
| 1607 | * see FLAC__StreamEncoderInitStatus for the meanings of other return values. |
| 1608 | */ |
| 1609 | FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_FILE(FLAC__StreamEncoder *encoder, FILE *file, FLAC__StreamEncoderProgressCallback progress_callback, void *client_data); |
| 1610 | |
| 1611 | /** Initialize the encoder instance to encode native FLAC files. |
| 1612 | * |
| 1613 | * This flavor of initialization sets up the encoder to encode to a plain |
| 1614 | * FLAC file. If POSIX fopen() semantics are not sufficient (for example, |
| 1615 | * with Unicode filenames on Windows), you must use |
| 1616 | * FLAC__stream_encoder_init_FILE(), or FLAC__stream_encoder_init_stream() |
| 1617 | * and provide callbacks for the I/O. |
| 1618 | * |
| 1619 | * This function should be called after FLAC__stream_encoder_new() and |
| 1620 | * FLAC__stream_encoder_set_*() but before FLAC__stream_encoder_process() |
| 1621 | * or FLAC__stream_encoder_process_interleaved(). |
| 1622 | * initialization succeeded. |
| 1623 | * |
| 1624 | * \param encoder An uninitialized encoder instance. |
| 1625 | * \param filename The name of the file to encode to. The file will |
| 1626 | * be opened with fopen(). Use \c NULL to encode to |
| 1627 | * \c stdout. Note however that a proper SEEKTABLE |
| 1628 | * cannot be created when encoding to \c stdout since |
| 1629 | * it is not seekable. |
| 1630 | * \param progress_callback See FLAC__StreamEncoderProgressCallback. This |
| 1631 | * pointer may be \c NULL if the callback is not |
| 1632 | * desired. |
| 1633 | * \param client_data This value will be supplied to callbacks in their |
| 1634 | * \a client_data argument. |
| 1635 | * \assert |
| 1636 | * \code encoder != NULL \endcode |
| 1637 | * \retval FLAC__StreamEncoderInitStatus |
| 1638 | * \c FLAC__STREAM_ENCODER_INIT_STATUS_OK if initialization was successful; |
| 1639 | * see FLAC__StreamEncoderInitStatus for the meanings of other return values. |
| 1640 | */ |
| 1641 | FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_file(FLAC__StreamEncoder *encoder, const char *filename, FLAC__StreamEncoderProgressCallback progress_callback, void *client_data); |
| 1642 | |
| 1643 | /** Initialize the encoder instance to encode Ogg FLAC files. |
| 1644 | * |
| 1645 | * This flavor of initialization sets up the encoder to encode to a plain |
| 1646 | * Ogg FLAC file. If POSIX fopen() semantics are not sufficient (for example, |
| 1647 | * with Unicode filenames on Windows), you must use |
| 1648 | * FLAC__stream_encoder_init_ogg_FILE(), or FLAC__stream_encoder_init_ogg_stream() |
| 1649 | * and provide callbacks for the I/O. |
| 1650 | * |
| 1651 | * This function should be called after FLAC__stream_encoder_new() and |
| 1652 | * FLAC__stream_encoder_set_*() but before FLAC__stream_encoder_process() |
| 1653 | * or FLAC__stream_encoder_process_interleaved(). |
| 1654 | * initialization succeeded. |
| 1655 | * |
| 1656 | * \param encoder An uninitialized encoder instance. |
| 1657 | * \param filename The name of the file to encode to. The file will |
| 1658 | * be opened with fopen(). Use \c NULL to encode to |
| 1659 | * \c stdout. Note however that a proper SEEKTABLE |
| 1660 | * cannot be created when encoding to \c stdout since |
| 1661 | * it is not seekable. |
| 1662 | * \param progress_callback See FLAC__StreamEncoderProgressCallback. This |
| 1663 | * pointer may be \c NULL if the callback is not |
| 1664 | * desired. |
| 1665 | * \param client_data This value will be supplied to callbacks in their |
| 1666 | * \a client_data argument. |
| 1667 | * \assert |
| 1668 | * \code encoder != NULL \endcode |
| 1669 | * \retval FLAC__StreamEncoderInitStatus |
| 1670 | * \c FLAC__STREAM_ENCODER_INIT_STATUS_OK if initialization was successful; |
| 1671 | * see FLAC__StreamEncoderInitStatus for the meanings of other return values. |
| 1672 | */ |
| 1673 | FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_file(FLAC__StreamEncoder *encoder, const char *filename, FLAC__StreamEncoderProgressCallback progress_callback, void *client_data); |
| 1674 | |
| 1675 | /** Finish the encoding process. |
| 1676 | * Flushes the encoding buffer, releases resources, resets the encoder |
| 1677 | * settings to their defaults, and returns the encoder state to |
| 1678 | * FLAC__STREAM_ENCODER_UNINITIALIZED. Note that this can generate |
| 1679 | * one or more write callbacks before returning, and will generate |
| 1680 | * a metadata callback. |
| 1681 | * |
| 1682 | * Note that in the course of processing the last frame, errors can |
| 1683 | * occur, so the caller should be sure to check the return value to |
| 1684 | * ensure the file was encoded properly. |
| 1685 | * |
| 1686 | * In the event of a prematurely-terminated encode, it is not strictly |
| 1687 | * necessary to call this immediately before FLAC__stream_encoder_delete() |
| 1688 | * but it is good practice to match every FLAC__stream_encoder_init_*() |
| 1689 | * with a FLAC__stream_encoder_finish(). |
| 1690 | * |
| 1691 | * \param encoder An uninitialized encoder instance. |
| 1692 | * \assert |
| 1693 | * \code encoder != NULL \endcode |
| 1694 | * \retval FLAC__bool |
| 1695 | * \c false if an error occurred processing the last frame; or if verify |
| 1696 | * mode is set (see FLAC__stream_encoder_set_verify()), there was a |
| 1697 | * verify mismatch; else \c true. If \c false, caller should check the |
| 1698 | * state with FLAC__stream_encoder_get_state() for more information |
| 1699 | * about the error. |
| 1700 | */ |
| 1701 | FLAC_API FLAC__bool FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder); |
| 1702 | |
| 1703 | /** Submit data for encoding. |
| 1704 | * This version allows you to supply the input data via an array of |
| 1705 | * pointers, each pointer pointing to an array of \a samples samples |
| 1706 | * representing one channel. The samples need not be block-aligned, |
| 1707 | * but each channel should have the same number of samples. Each sample |
| 1708 | * should be a signed integer, right-justified to the resolution set by |
| 1709 | * FLAC__stream_encoder_set_bits_per_sample(). For example, if the |
| 1710 | * resolution is 16 bits per sample, the samples should all be in the |
| 1711 | * range [-32768,32767]. |
| 1712 | * |
| 1713 | * For applications where channel order is important, channels must |
| 1714 | * follow the order as described in the |
| 1715 | * <A HREF="../format.html#frame_header">frame header</A>. |
| 1716 | * |
| 1717 | * \param encoder An initialized encoder instance in the OK state. |
| 1718 | * \param buffer An array of pointers to each channel's signal. |
| 1719 | * \param samples The number of samples in one channel. |
| 1720 | * \assert |
| 1721 | * \code encoder != NULL \endcode |
| 1722 | * \code FLAC__stream_encoder_get_state(encoder) == FLAC__STREAM_ENCODER_OK \endcode |
| 1723 | * \retval FLAC__bool |
| 1724 | * \c true if successful, else \c false; in this case, check the |
| 1725 | * encoder state with FLAC__stream_encoder_get_state() to see what |
| 1726 | * went wrong. |
| 1727 | */ |
| 1728 | FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples); |
| 1729 | |
| 1730 | /** Submit data for encoding. |
| 1731 | * This version allows you to supply the input data where the channels |
| 1732 | * are interleaved into a single array (i.e. channel0_sample0, |
| 1733 | * channel1_sample0, ... , channelN_sample0, channel0_sample1, ...). |
| 1734 | * The samples need not be block-aligned but they must be |
| 1735 | * sample-aligned, i.e. the first value should be channel0_sample0 |
| 1736 | * and the last value channelN_sampleM. Each sample should be a signed |
| 1737 | * integer, right-justified to the resolution set by |
| 1738 | * FLAC__stream_encoder_set_bits_per_sample(). For example, if the |
| 1739 | * resolution is 16 bits per sample, the samples should all be in the |
| 1740 | * range [-32768,32767]. |
| 1741 | * |
| 1742 | * For applications where channel order is important, channels must |
| 1743 | * follow the order as described in the |
| 1744 | * <A HREF="../format.html#frame_header">frame header</A>. |
| 1745 | * |
| 1746 | * \param encoder An initialized encoder instance in the OK state. |
| 1747 | * \param buffer An array of channel-interleaved data (see above). |
| 1748 | * \param samples The number of samples in one channel, the same as for |
| 1749 | * FLAC__stream_encoder_process(). For example, if |
| 1750 | * encoding two channels, \c 1000 \a samples corresponds |
| 1751 | * to a \a buffer of 2000 values. |
| 1752 | * \assert |
| 1753 | * \code encoder != NULL \endcode |
| 1754 | * \code FLAC__stream_encoder_get_state(encoder) == FLAC__STREAM_ENCODER_OK \endcode |
| 1755 | * \retval FLAC__bool |
| 1756 | * \c true if successful, else \c false; in this case, check the |
| 1757 | * encoder state with FLAC__stream_encoder_get_state() to see what |
| 1758 | * went wrong. |
| 1759 | */ |
| 1760 | FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples); |
| 1761 | |
| 1762 | /* \} */ |
| 1763 | |
| 1764 | #ifdef __cplusplus |
| 1765 | } |
| 1766 | #endif |
| 1767 | |
| 1768 | #endif |
src/lib/libflac/include/flac/metadata.h
| r0 | r14135 | |
| 1 | /* libFLAC - Free Lossless Audio Codec library |
| 2 | * Copyright (C) 2001,2002,2003,2004,2005,2006,2007 Josh Coalson |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * |
| 8 | * - Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * - Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * - Neither the name of the Xiph.org Foundation nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived from |
| 17 | * this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 26 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 27 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #ifndef FLAC__METADATA_H |
| 33 | #define FLAC__METADATA_H |
| 34 | |
| 35 | #include <sys/types.h> /* for off_t */ |
| 36 | #include "export.h" |
| 37 | #include "callback.h" |
| 38 | #include "format.h" |
| 39 | |
| 40 | /* -------------------------------------------------------------------- |
| 41 | (For an example of how all these routines are used, see the source |
| 42 | code for the unit tests in src/test_libFLAC/metadata_*.c, or |
| 43 | metaflac in src/metaflac/) |
| 44 | ------------------------------------------------------------------*/ |
| 45 | |
| 46 | /** \file include/FLAC/metadata.h |
| 47 | * |
| 48 | * \brief |
| 49 | * This module provides functions for creating and manipulating FLAC |
| 50 | * metadata blocks in memory, and three progressively more powerful |
| 51 | * interfaces for traversing and editing metadata in FLAC files. |
| 52 | * |
| 53 | * See the detailed documentation for each interface in the |
| 54 | * \link flac_metadata metadata \endlink module. |
| 55 | */ |
| 56 | |
| 57 | /** \defgroup flac_metadata FLAC/metadata.h: metadata interfaces |
| 58 | * \ingroup flac |
| 59 | * |
| 60 | * \brief |
| 61 | * This module provides functions for creating and manipulating FLAC |
| 62 | * metadata blocks in memory, and three progressively more powerful |
| 63 | * interfaces for traversing and editing metadata in native FLAC files. |
| 64 | * Note that currently only the Chain interface (level 2) supports Ogg |
| 65 | * FLAC files, and it is read-only i.e. no writing back changed |
| 66 | * metadata to file. |
| 67 | * |
| 68 | * There are three metadata interfaces of increasing complexity: |
| 69 | * |
| 70 | * Level 0: |
| 71 | * Read-only access to the STREAMINFO, VORBIS_COMMENT, CUESHEET, and |
| 72 | * PICTURE blocks. |
| 73 | * |
| 74 | * Level 1: |
| 75 | * Read-write access to all metadata blocks. This level is write- |
| 76 | * efficient in most cases (more on this below), and uses less memory |
| 77 | * than level 2. |
| 78 | * |
| 79 | * Level 2: |
| 80 | * Read-write access to all metadata blocks. This level is write- |
| 81 | * efficient in all cases, but uses more memory since all metadata for |
| 82 | * the whole file is read into memory and manipulated before writing |
| 83 | * out again. |
| 84 | * |
| 85 | * What do we mean by efficient? Since FLAC metadata appears at the |
| 86 | * beginning of the file, when writing metadata back to a FLAC file |
| 87 | * it is possible to grow or shrink the metadata such that the entire |
| 88 | * file must be rewritten. However, if the size remains the same during |
| 89 | * changes or PADDING blocks are utilized, only the metadata needs to be |
| 90 | * overwritten, which is much faster. |
| 91 | * |
| 92 | * Efficient means the whole file is rewritten at most one time, and only |
| 93 | * when necessary. Level 1 is not efficient only in the case that you |
| 94 | * cause more than one metadata block to grow or shrink beyond what can |
| 95 | * be accomodated by padding. In this case you should probably use level |
| 96 | * 2, which allows you to edit all the metadata for a file in memory and |
| 97 | * write it out all at once. |
| 98 | * |
| 99 | * All levels know how to skip over and not disturb an ID3v2 tag at the |
| 100 | * front of the file. |
| 101 | * |
| 102 | * All levels access files via their filenames. In addition, level 2 |
| 103 | * has additional alternative read and write functions that take an I/O |
| 104 | * handle and callbacks, for situations where access by filename is not |
| 105 | * possible. |
| 106 | * |
| 107 | * In addition to the three interfaces, this module defines functions for |
| 108 | * creating and manipulating various metadata objects in memory. As we see |
| 109 | * from the Format module, FLAC metadata blocks in memory are very primitive |
| 110 | * structures for storing information in an efficient way. Reading |
| 111 | * information from the structures is easy but creating or modifying them |
| 112 | * directly is more complex. The metadata object routines here facilitate |
| 113 | * this by taking care of the consistency and memory management drudgery. |
| 114 | * |
| 115 | * Unless you will be using the level 1 or 2 interfaces to modify existing |
| 116 | * metadata however, you will not probably not need these. |
| 117 | * |
| 118 | * From a dependency standpoint, none of the encoders or decoders require |
| 119 | * the metadata module. This is so that embedded users can strip out the |
| 120 | * metadata module from libFLAC to reduce the size and complexity. |
| 121 | */ |
| 122 | |
| 123 | #ifdef __cplusplus |
| 124 | extern "C" { |
| 125 | #endif |
| 126 | |
| 127 | |
| 128 | /** \defgroup flac_metadata_level0 FLAC/metadata.h: metadata level 0 interface |
| 129 | * \ingroup flac_metadata |
| 130 | * |
| 131 | * \brief |
| 132 | * The level 0 interface consists of individual routines to read the |
| 133 | * STREAMINFO, VORBIS_COMMENT, CUESHEET, and PICTURE blocks, requiring |
| 134 | * only a filename. |
| 135 | * |
| 136 | * They try to skip any ID3v2 tag at the head of the file. |
| 137 | * |
| 138 | * \{ |
| 139 | */ |
| 140 | |
| 141 | /** Read the STREAMINFO metadata block of the given FLAC file. This function |
| 142 | * will try to skip any ID3v2 tag at the head of the file. |
| 143 | * |
| 144 | * \param filename The path to the FLAC file to read. |
| 145 | * \param streaminfo A pointer to space for the STREAMINFO block. Since |
| 146 | * FLAC__StreamMetadata is a simple structure with no |
| 147 | * memory allocation involved, you pass the address of |
| 148 | * an existing structure. It need not be initialized. |
| 149 | * \assert |
| 150 | * \code filename != NULL \endcode |
| 151 | * \code streaminfo != NULL \endcode |
| 152 | * \retval FLAC__bool |
| 153 | * \c true if a valid STREAMINFO block was read from \a filename. Returns |
| 154 | * \c false if there was a memory allocation error, a file decoder error, |
| 155 | * or the file contained no STREAMINFO block. (A memory allocation error |
| 156 | * is possible because this function must set up a file decoder.) |
| 157 | */ |
| 158 | FLAC_API FLAC__bool FLAC__metadata_get_streaminfo(const char *filename, FLAC__StreamMetadata *streaminfo); |
| 159 | |
| 160 | /** Read the VORBIS_COMMENT metadata block of the given FLAC file. This |
| 161 | * function will try to skip any ID3v2 tag at the head of the file. |
| 162 | * |
| 163 | * \param filename The path to the FLAC file to read. |
| 164 | * \param tags The address where the returned pointer will be |
| 165 | * stored. The \a tags object must be deleted by |
| 166 | * the caller using FLAC__metadata_object_delete(). |
| 167 | * \assert |
| 168 | * \code filename != NULL \endcode |
| 169 | * \code tags != NULL \endcode |
| 170 | * \retval FLAC__bool |
| 171 | * \c true if a valid VORBIS_COMMENT block was read from \a filename, |
| 172 | * and \a *tags will be set to the address of the metadata structure. |
| 173 | * Returns \c false if there was a memory allocation error, a file |
| 174 | * decoder error, or the file contained no VORBIS_COMMENT block, and |
| 175 | * \a *tags will be set to \c NULL. |
| 176 | */ |
| 177 | FLAC_API FLAC__bool FLAC__metadata_get_tags(const char *filename, FLAC__StreamMetadata **tags); |
| 178 | |
| 179 | /** Read the CUESHEET metadata block of the given FLAC file. This |
| 180 | * function will try to skip any ID3v2 tag at the head of the file. |
| 181 | * |
| 182 | * \param filename The path to the FLAC file to read. |
| 183 | * \param cuesheet The address where the returned pointer will be |
| 184 | * stored. The \a cuesheet object must be deleted by |
| 185 | * the caller using FLAC__metadata_object_delete(). |
| 186 | * \assert |
| 187 | * \code filename != NULL \endcode |
| 188 | * \code cuesheet != NULL \endcode |
| 189 | * \retval FLAC__bool |
| 190 | * \c true if a valid CUESHEET block was read from \a filename, |
| 191 | * and \a *cuesheet will be set to the address of the metadata |
| 192 | * structure. Returns \c false if there was a memory allocation |
| 193 | * error, a file decoder error, or the file contained no CUESHEET |
| 194 | * block, and \a *cuesheet will be set to \c NULL. |
| 195 | */ |
| 196 | FLAC_API FLAC__bool FLAC__metadata_get_cuesheet(const char *filename, FLAC__StreamMetadata **cuesheet); |
| 197 | |
| 198 | /** Read a PICTURE metadata block of the given FLAC file. This |
| 199 | * function will try to skip any ID3v2 tag at the head of the file. |
| 200 | * Since there can be more than one PICTURE block in a file, this |
| 201 | * function takes a number of parameters that act as constraints to |
| 202 | * the search. The PICTURE block with the largest area matching all |
| 203 | * the constraints will be returned, or \a *picture will be set to |
| 204 | * \c NULL if there was no such block. |
| 205 | * |
| 206 | * \param filename The path to the FLAC file to read. |
| 207 | * \param picture The address where the returned pointer will be |
| 208 | * stored. The \a picture object must be deleted by |
| 209 | * the caller using FLAC__metadata_object_delete(). |
| 210 | * \param type The desired picture type. Use \c -1 to mean |
| 211 | * "any type". |
| 212 | * \param mime_type The desired MIME type, e.g. "image/jpeg". The |
| 213 | * string will be matched exactly. Use \c NULL to |
| 214 | * mean "any MIME type". |
| 215 | * \param description The desired description. The string will be |
| 216 | * matched exactly. Use \c NULL to mean "any |
| 217 | * description". |
| 218 | * \param max_width The maximum width in pixels desired. Use |
| 219 | * \c (unsigned)(-1) to mean "any width". |
| 220 | * \param max_height The maximum height in pixels desired. Use |
| 221 | * \c (unsigned)(-1) to mean "any height". |
| 222 | * \param max_depth The maximum color depth in bits-per-pixel desired. |
| 223 | * Use \c (unsigned)(-1) to mean "any depth". |
| 224 | * \param max_colors The maximum number of colors desired. Use |
| 225 | * \c (unsigned)(-1) to mean "any number of colors". |
| 226 | * \assert |
| 227 | * \code filename != NULL \endcode |
| 228 | * \code picture != NULL \endcode |
| 229 | * \retval FLAC__bool |
| 230 | * \c true if a valid PICTURE block was read from \a filename, |
| 231 | * and \a *picture will be set to the address of the metadata |
| 232 | * structure. Returns \c false if there was a memory allocation |
| 233 | * error, a file decoder error, or the file contained no PICTURE |
| 234 | * block, and \a *picture will be set to \c NULL. |
| 235 | */ |
| 236 | FLAC_API FLAC__bool FLAC__metadata_get_picture(const char *filename, FLAC__StreamMetadata **picture, FLAC__StreamMetadata_Picture_Type type, const char *mime_type, const FLAC__byte *description, unsigned max_width, unsigned max_height, unsigned max_depth, unsigned max_colors); |
| 237 | |
| 238 | /* \} */ |
| 239 | |
| 240 | |
| 241 | /** \defgroup flac_metadata_level1 FLAC/metadata.h: metadata level 1 interface |
| 242 | * \ingroup flac_metadata |
| 243 | * |
| 244 | * \brief |
| 245 | * The level 1 interface provides read-write access to FLAC file metadata and |
| 246 | * operates directly on the FLAC file. |
| 247 | * |
| 248 | * The general usage of this interface is: |
| 249 | * |
| 250 | * - Create an iterator using FLAC__metadata_simple_iterator_new() |
| 251 | * - Attach it to a file using FLAC__metadata_simple_iterator_init() and check |
| 252 | * the exit code. Call FLAC__metadata_simple_iterator_is_writable() to |
| 253 | * see if the file is writable, or only read access is allowed. |
| 254 | * - Use FLAC__metadata_simple_iterator_next() and |
| 255 | * FLAC__metadata_simple_iterator_prev() to traverse the blocks. |
| 256 | * This is does not read the actual blocks themselves. |
| 257 | * FLAC__metadata_simple_iterator_next() is relatively fast. |
| 258 | * FLAC__metadata_simple_iterator_prev() is slower since it needs to search |
| 259 | * forward from the front of the file. |
| 260 | * - Use FLAC__metadata_simple_iterator_get_block_type() or |
| 261 | * FLAC__metadata_simple_iterator_get_block() to access the actual data at |
| 262 | * the current iterator position. The returned object is yours to modify |
| 263 | * and free. |
| 264 | * - Use FLAC__metadata_simple_iterator_set_block() to write a modified block |
| 265 | * back. You must have write permission to the original file. Make sure to |
| 266 | * read the whole comment to FLAC__metadata_simple_iterator_set_block() |
| 267 | * below. |
| 268 | * - Use FLAC__metadata_simple_iterator_insert_block_after() to add new blocks. |
| 269 | * Use the object creation functions from |
| 270 | * \link flac_metadata_object here \endlink to generate new objects. |
| 271 | * - Use FLAC__metadata_simple_iterator_delete_block() to remove the block |
| 272 | * currently referred to by the iterator, or replace it with padding. |
| 273 | * - Destroy the iterator with FLAC__metadata_simple_iterator_delete() when |
| 274 | * finished. |
| 275 | * |
| 276 | * \note |
| 277 | * The FLAC file remains open the whole time between |
| 278 | * FLAC__metadata_simple_iterator_init() and |
| 279 | * FLAC__metadata_simple_iterator_delete(), so make sure you are not altering |
| 280 | * the file during this time. |
| 281 | * |
| 282 | * \note |
| 283 | * Do not modify the \a is_last, \a length, or \a type fields of returned |
| 284 | * FLAC__StreamMetadata objects. These are managed automatically. |
| 285 | * |
| 286 | * \note |
| 287 | * If any of the modification functions |
| 288 | * (FLAC__metadata_simple_iterator_set_block(), |
| 289 | * FLAC__metadata_simple_iterator_delete_block(), |
| 290 | * FLAC__metadata_simple_iterator_insert_block_after(), etc.) return \c false, |
| 291 | * you should delete the iterator as it may no longer be valid. |
| 292 | * |
| 293 | * \{ |
| 294 | */ |
| 295 | |
| 296 | struct FLAC__Metadata_SimpleIterator; |
| 297 | /** The opaque structure definition for the level 1 iterator type. |
| 298 | * See the |
| 299 | * \link flac_metadata_level1 metadata level 1 module \endlink |
| 300 | * for a detailed description. |
| 301 | */ |
| 302 | typedef struct FLAC__Metadata_SimpleIterator FLAC__Metadata_SimpleIterator; |
| 303 | |
| 304 | /** Status type for FLAC__Metadata_SimpleIterator. |
| 305 | * |
| 306 | * The iterator's current status can be obtained by calling FLAC__metadata_simple_iterator_status(). |
| 307 | */ |
| 308 | typedef enum { |
| 309 | |
| 310 | FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK = 0, |
| 311 | /**< The iterator is in the normal OK state */ |
| 312 | |
| 313 | FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT, |
| 314 | /**< The data passed into a function violated the function's usage criteria */ |
| 315 | |
| 316 | FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE, |
| 317 | /**< The iterator could not open the target file */ |
| 318 | |
| 319 | FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE, |
| 320 | /**< The iterator could not find the FLAC signature at the start of the file */ |
| 321 | |
| 322 | FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE, |
| 323 | /**< The iterator tried to write to a file that was not writable */ |
| 324 | |
| 325 | FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA, |
| 326 | /**< The iterator encountered input that does not conform to the FLAC metadata specification */ |
| 327 | |
| 328 | FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR, |
| 329 | /**< The iterator encountered an error while reading the FLAC file */ |
| 330 | |
| 331 | FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR, |
| 332 | /**< The iterator encountered an error while seeking in the FLAC file */ |
| 333 | |
| 334 | FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR, |
| 335 | /**< The iterator encountered an error while writing the FLAC file */ |
| 336 | |
| 337 | FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR, |
| 338 | /**< The iterator encountered an error renaming the FLAC file */ |
| 339 | |
| 340 | FLAC__METADATA_SIMPLE_ITERATOR_STATUS_UNLINK_ERROR, |
| 341 | /**< The iterator encountered an error removing the temporary file */ |
| 342 | |
| 343 | FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR, |
| 344 | /**< Memory allocation failed */ |
| 345 | |
| 346 | FLAC__METADATA_SIMPLE_ITERATOR_STATUS_INTERNAL_ERROR |
| 347 | /**< The caller violated an assertion or an unexpected error occurred */ |
| 348 | |
| 349 | } FLAC__Metadata_SimpleIteratorStatus; |
| 350 | |
| 351 | /** Maps a FLAC__Metadata_SimpleIteratorStatus to a C string. |
| 352 | * |
| 353 | * Using a FLAC__Metadata_SimpleIteratorStatus as the index to this array |
| 354 | * will give the string equivalent. The contents should not be modified. |
| 355 | */ |
| 356 | extern FLAC_API const char * const FLAC__Metadata_SimpleIteratorStatusString[]; |
| 357 | |
| 358 | |
| 359 | /** Create a new iterator instance. |
| 360 | * |
| 361 | * \retval FLAC__Metadata_SimpleIterator* |
| 362 | * \c NULL if there was an error allocating memory, else the new instance. |
| 363 | */ |
| 364 | FLAC_API FLAC__Metadata_SimpleIterator *FLAC__metadata_simple_iterator_new(void); |
| 365 | |
| 366 | /** Free an iterator instance. Deletes the object pointed to by \a iterator. |
| 367 | * |
| 368 | * \param iterator A pointer to an existing iterator. |
| 369 | * \assert |
| 370 | * \code iterator != NULL \endcode |
| 371 | */ |
| 372 | FLAC_API void FLAC__metadata_simple_iterator_delete(FLAC__Metadata_SimpleIterator *iterator); |
| 373 | |
| 374 | /** Get the current status of the iterator. Call this after a function |
| 375 | * returns \c false to get the reason for the error. Also resets the status |
| 376 | * to FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK. |
| 377 | * |
| 378 | * \param iterator A pointer to an existing iterator. |
| 379 | * \assert |
| 380 | * \code iterator != NULL \endcode |
| 381 | * \retval FLAC__Metadata_SimpleIteratorStatus |
| 382 | * The current status of the iterator. |
| 383 | */ |
| 384 | FLAC_API FLAC__Metadata_SimpleIteratorStatus FLAC__metadata_simple_iterator_status(FLAC__Metadata_SimpleIterator *iterator); |
| 385 | |
| 386 | /** Initialize the iterator to point to the first metadata block in the |
| 387 | * given FLAC file. |
| 388 | * |
| 389 | * \param iterator A pointer to an existing iterator. |
| 390 | * \param filename The path to the FLAC file. |
| 391 | * \param read_only If \c true, the FLAC file will be opened |
| 392 | * in read-only mode; if \c false, the FLAC |
| 393 | * file will be opened for edit even if no |
| 394 | * edits are performed. |
| 395 | * \param preserve_file_stats If \c true, the owner and modification |
| 396 | * time will be preserved even if the FLAC |
| 397 | * file is written to. |
| 398 | * \assert |
| 399 | * \code iterator != NULL \endcode |
| 400 | * \code filename != NULL \endcode |
| 401 | * \retval FLAC__bool |
| 402 | * \c false if a memory allocation error occurs, the file can't be |
| 403 | * opened, or another error occurs, else \c true. |
| 404 | */ |
| 405 | FLAC_API FLAC__bool FLAC__metadata_simple_iterator_init(FLAC__Metadata_SimpleIterator *iterator, const char *filename, FLAC__bool read_only, FLAC__bool preserve_file_stats); |
| 406 | |
| 407 | /** Returns \c true if the FLAC file is writable. If \c false, calls to |
| 408 | * FLAC__metadata_simple_iterator_set_block() and |
| 409 | * FLAC__metadata_simple_iterator_insert_block_after() will fail. |
| 410 | * |
| 411 | * \param iterator A pointer to an existing iterator. |
| 412 | * \assert |
| 413 | * \code iterator != NULL \endcode |
| 414 | * \retval FLAC__bool |
| 415 | * See above. |
| 416 | */ |
| 417 | FLAC_API FLAC__bool FLAC__metadata_simple_iterator_is_writable(const FLAC__Metadata_SimpleIterator *iterator); |
| 418 | |
| 419 | /** Moves the iterator forward one metadata block, returning \c false if |
| 420 | * already at the end. |
| 421 | * |
| 422 | * \param iterator A pointer to an existing initialized iterator. |
| 423 | * \assert |
| 424 | * \code iterator != NULL \endcode |
| 425 | * \a iterator has been successfully initialized with |
| 426 | * FLAC__metadata_simple_iterator_init() |
| 427 | * \retval FLAC__bool |
| 428 | * \c false if already at the last metadata block of the chain, else |
| 429 | * \c true. |
| 430 | */ |
| 431 | FLAC_API FLAC__bool FLAC__metadata_simple_iterator_next(FLAC__Metadata_SimpleIterator *iterator); |
| 432 | |
| 433 | /** Moves the iterator backward one metadata block, returning \c false if |
| 434 | * already at the beginning. |
| 435 | * |
| 436 | * \param iterator A pointer to an existing initialized iterator. |
| 437 | * \assert |
| 438 | * \code iterator != NULL \endcode |
| 439 | * \a iterator has been successfully initialized with |
| 440 | * FLAC__metadata_simple_iterator_init() |
| 441 | * \retval FLAC__bool |
| 442 | * \c false if already at the first metadata block of the chain, else |
| 443 | * \c true. |
| 444 | */ |
| 445 | FLAC_API FLAC__bool FLAC__metadata_simple_iterator_prev(FLAC__Metadata_SimpleIterator *iterator); |
| 446 | |
| 447 | /** Returns a flag telling if the current metadata block is the last. |
| 448 | * |
| 449 | * \param iterator A pointer to an existing initialized iterator. |
| 450 | * \assert |
| 451 | * \code iterator != NULL \endcode |
| 452 | * \a iterator has been successfully initialized with |
| 453 | * FLAC__metadata_simple_iterator_init() |
| 454 | * \retval FLAC__bool |
| 455 | * \c true if the current metadata block is the last in the file, |
| 456 | * else \c false. |
| 457 | */ |
| 458 | FLAC_API FLAC__bool FLAC__metadata_simple_iterator_is_last(const FLAC__Metadata_SimpleIterator *iterator); |
| 459 | |
| 460 | /** Get the offset of the metadata block at the current position. This |
| 461 | * avoids reading the actual block data which can save time for large |
| 462 | * blocks. |
| 463 | * |
| 464 | * \param iterator A pointer to an existing initialized iterator. |
| 465 | * \assert |
| 466 | * \code iterator != NULL \endcode |
| 467 | * \a iterator has been successfully initialized with |
| 468 | * FLAC__metadata_simple_iterator_init() |
| 469 | * \retval off_t |
| 470 | * The offset of the metadata block at the current iterator position. |
| 471 | * This is the byte offset relative to the beginning of the file of |
| 472 | * the current metadata block's header. |
| 473 | */ |
| 474 | FLAC_API off_t FLAC__metadata_simple_iterator_get_block_offset(const FLAC__Metadata_SimpleIterator *iterator); |
| 475 | |
| 476 | /** Get the type of the metadata block at the current position. This |
| 477 | * avoids reading the actual block data which can save time for large |
| 478 | * blocks. |
| 479 | * |
| 480 | * \param iterator A pointer to an existing initialized iterator. |
| 481 | * \assert |
| 482 | * \code iterator != NULL \endcode |
| 483 | * \a iterator has been successfully initialized with |
| 484 | * FLAC__metadata_simple_iterator_init() |
| 485 | * \retval FLAC__MetadataType |
| 486 | * The type of the metadata block at the current iterator position. |
| 487 | */ |
| 488 | FLAC_API FLAC__MetadataType FLAC__metadata_simple_iterator_get_block_type(const FLAC__Metadata_SimpleIterator *iterator); |
| 489 | |
| 490 | /** Get the length of the metadata block at the current position. This |
| 491 | * avoids reading the actual block data which can save time for large |
| 492 | * blocks. |
| 493 | * |
| 494 | * \param iterator A pointer to an existing initialized iterator. |
| 495 | * \assert |
| 496 | * \code iterator != NULL \endcode |
| 497 | * \a iterator has been successfully initialized with |
| 498 | * FLAC__metadata_simple_iterator_init() |
| 499 | * \retval unsigned |
| 500 | * The length of the metadata block at the current iterator position. |
| 501 | * The is same length as that in the |
| 502 | * <a href="http://flac.sourceforge.net/format.html#metadata_block_header">metadata block header</a>, |
| 503 | * i.e. the length of the metadata body that follows the header. |
| 504 | */ |
| 505 | FLAC_API unsigned FLAC__metadata_simple_iterator_get_block_length(const FLAC__Metadata_SimpleIterator *iterator); |
| 506 | |
| 507 | /** Get the application ID of the \c APPLICATION block at the current |
| 508 | * position. This avoids reading the actual block data which can save |
| 509 | * time for large blocks. |
| 510 | * |
| 511 | * \param iterator A pointer to an existing initialized iterator. |
| 512 | * \param id A pointer to a buffer of at least \c 4 bytes where |
| 513 | * the ID will be stored. |
| 514 | * \assert |
| 515 | * \code iterator != NULL \endcode |
| 516 | * \code id != NULL \endcode |
| 517 | * \a iterator has been successfully initialized with |
| 518 | * FLAC__metadata_simple_iterator_init() |
| 519 | * \retval FLAC__bool |
| 520 | * \c true if the ID was successfully read, else \c false, in which |
| 521 | * case you should check FLAC__metadata_simple_iterator_status() to |
| 522 | * find out why. If the status is |
| 523 | * \c FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT, then the |
| 524 | * current metadata block is not an \c APPLICATION block. Otherwise |
| 525 | * if the status is |
| 526 | * \c FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR or |
| 527 | * \c FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR, an I/O error |
| 528 | * occurred and the iterator can no longer be used. |
| 529 | */ |
| 530 | FLAC_API FLAC__bool FLAC__metadata_simple_iterator_get_application_id(FLAC__Metadata_SimpleIterator *iterator, FLAC__byte *id); |
| 531 | |
| 532 | /** Get the metadata block at the current position. You can modify the |
| 533 | * block but must use FLAC__metadata_simple_iterator_set_block() to |
| 534 | * write it back to the FLAC file. |
| 535 | * |
| 536 | * You must call FLAC__metadata_object_delete() on the returned object |
| 537 | * when you are finished with it. |
| 538 | * |
| 539 | * \param iterator A pointer to an existing initialized iterator. |
| 540 | * \assert |
| 541 | * \code iterator != NULL \endcode |
| 542 | * \a iterator has been successfully initialized with |
| 543 | * FLAC__metadata_simple_iterator_init() |
| 544 | * \retval FLAC__StreamMetadata* |
| 545 | * The current metadata block, or \c NULL if there was a memory |
| 546 | * allocation error. |
| 547 | */ |
| 548 | FLAC_API FLAC__StreamMetadata *FLAC__metadata_simple_iterator_get_block(FLAC__Metadata_SimpleIterator *iterator); |
| 549 | |
| 550 | /** Write a block back to the FLAC file. This function tries to be |
| 551 | * as efficient as possible; how the block is actually written is |
| 552 | * shown by the following: |
| 553 | * |
| 554 | * Existing block is a STREAMINFO block and the new block is a |
| 555 | * STREAMINFO block: the new block is written in place. Make sure |
| 556 | * you know what you're doing when changing the values of a |
| 557 | * STREAMINFO block. |
| 558 | * |
| 559 | * Existing block is a STREAMINFO block and the new block is a |
| 560 | * not a STREAMINFO block: this is an error since the first block |
| 561 | * must be a STREAMINFO block. Returns \c false without altering the |
| 562 | * file. |
| 563 | * |
| 564 | * Existing block is not a STREAMINFO block and the new block is a |
| 565 | * STREAMINFO block: this is an error since there may be only one |
| 566 | * STREAMINFO block. Returns \c false without altering the file. |
| 567 | * |
| 568 | * Existing block and new block are the same length: the existing |
| 569 | * block will be replaced by the new block, written in place. |
| 570 | * |
| 571 | * Existing block is longer than new block: if use_padding is \c true, |
| 572 | * the existing block will be overwritten in place with the new |
| 573 | * block followed by a PADDING block, if possible, to make the total |
| 574 | * size the same as the existing block. Remember that a padding |
| 575 | * block requires at least four bytes so if the difference in size |
| 576 | * between the new block and existing block is less than that, the |
| 577 | * entire file will have to be rewritten, using the new block's |
| 578 | * exact size. If use_padding is \c false, the entire file will be |
| 579 | * rewritten, replacing the existing block by the new block. |
| 580 | * |
| 581 | * Existing block is shorter than new block: if use_padding is \c true, |
| 582 | * the function will try and expand the new block into the following |
| 583 | * PADDING block, if it exists and doing so won't shrink the PADDING |
| 584 | * block to less than 4 bytes. If there is no following PADDING |
| 585 | * block, or it will shrink to less than 4 bytes, or use_padding is |
| 586 | * \c false, the entire file is rewritten, replacing the existing block |
| 587 | * with the new block. Note that in this case any following PADDING |
| 588 | * block is preserved as is. |
| 589 | * |
| 590 | * After writing the block, the iterator will remain in the same |
| 591 | * place, i.e. pointing to the new block. |
| 592 | * |
| 593 | * \param iterator A pointer to an existing initialized iterator. |
| 594 | * \param block The block to set. |
| 595 | * \param use_padding See above. |
| 596 | * \assert |
| 597 | * \code iterator != NULL \endcode |
| 598 | * \a iterator has been successfully initialized with |
| 599 | * FLAC__metadata_simple_iterator_init() |
| 600 | * \code block != NULL \endcode |
| 601 | * \retval FLAC__bool |
| 602 | * \c true if successful, else \c false. |
| 603 | */ |
| 604 | FLAC_API FLAC__bool FLAC__metadata_simple_iterator_set_block(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool use_padding); |
| 605 | |
| 606 | /** This is similar to FLAC__metadata_simple_iterator_set_block() |
| 607 | * except that instead of writing over an existing block, it appends |
| 608 | * a block after the existing block. \a use_padding is again used to |
| 609 | * tell the function to try an expand into following padding in an |
| 610 | * attempt to avoid rewriting the entire file. |
| 611 | * |
| 612 | * This function will fail and return \c false if given a STREAMINFO |
| 613 | * block. |
| 614 | * |
| 615 | * After writing the block, the iterator will be pointing to the |
| 616 | * new block. |
| 617 | * |
| 618 | * \param iterator A pointer to an existing initialized iterator. |
| 619 | * \param block The block to set. |
| 620 | * \param use_padding See above. |
| 621 | * \assert |
| 622 | * \code iterator != NULL \endcode |
| 623 | * \a iterator has been successfully initialized with |
| 624 | * FLAC__metadata_simple_iterator_init() |
| 625 | * \code block != NULL \endcode |
| 626 | * \retval FLAC__bool |
| 627 | * \c true if successful, else \c false. |
| 628 | */ |
| 629 | FLAC_API FLAC__bool FLAC__metadata_simple_iterator_insert_block_after(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool use_padding); |
| 630 | |
| 631 | /** Deletes the block at the current position. This will cause the |
| 632 | * entire FLAC file to be rewritten, unless \a use_padding is \c true, |
| 633 | * in which case the block will be replaced by an equal-sized PADDING |
| 634 | * block. The iterator will be left pointing to the block before the |
| 635 | * one just deleted. |
| 636 | * |
| 637 | * You may not delete the STREAMINFO block. |
| 638 | * |
| 639 | * \param iterator A pointer to an existing initialized iterator. |
| 640 | * \param use_padding See above. |
| 641 | * \assert |
| 642 | * \code iterator != NULL \endcode |
| 643 | * \a iterator has been successfully initialized with |
| 644 | * FLAC__metadata_simple_iterator_init() |
| 645 | * \retval FLAC__bool |
| 646 | * \c true if successful, else \c false. |
| 647 | */ |
| 648 | FLAC_API FLAC__bool FLAC__metadata_simple_iterator_delete_block(FLAC__Metadata_SimpleIterator *iterator, FLAC__bool use_padding); |
| 649 | |
| 650 | /* \} */ |
| 651 | |
| 652 | |
| 653 | /** \defgroup flac_metadata_level2 FLAC/metadata.h: metadata level 2 interface |
| 654 | * \ingroup flac_metadata |
| 655 | * |
| 656 | * \brief |
| 657 | * The level 2 interface provides read-write access to FLAC file metadata; |
| 658 | * all metadata is read into memory, operated on in memory, and then written |
| 659 | * to file, which is more efficient than level 1 when editing multiple blocks. |
| 660 | * |
| 661 | * Currently Ogg FLAC is supported for read only, via |
| 662 | * FLAC__metadata_chain_read_ogg() but a subsequent |
| 663 | * FLAC__metadata_chain_write() will fail. |
| 664 | * |
| 665 | * The general usage of this interface is: |
| 666 | * |
| 667 | * - Create a new chain using FLAC__metadata_chain_new(). A chain is a |
| 668 | * linked list of FLAC metadata blocks. |
| 669 | * - Read all metadata into the the chain from a FLAC file using |
| 670 | * FLAC__metadata_chain_read() or FLAC__metadata_chain_read_ogg() and |
| 671 | * check the status. |
| 672 | * - Optionally, consolidate the padding using |
| 673 | * FLAC__metadata_chain_merge_padding() or |
| 674 | * FLAC__metadata_chain_sort_padding(). |
| 675 | * - Create a new iterator using FLAC__metadata_iterator_new() |
| 676 | * - Initialize the iterator to point to the first element in the chain |
| 677 | * using FLAC__metadata_iterator_init() |
| 678 | * - Traverse the chain using FLAC__metadata_iterator_next and |
| 679 | * FLAC__metadata_iterator_prev(). |
| 680 | * - Get a block for reading or modification using |
| 681 | * FLAC__metadata_iterator_get_block(). The pointer to the object |
| 682 | * inside the chain is returned, so the block is yours to modify. |
| 683 | * Changes will be reflected in the FLAC file when you write the |
| 684 | * chain. You can also add and delete blocks (see functions below). |
| 685 | * - When done, write out the chain using FLAC__metadata_chain_write(). |
| 686 | * Make sure to read the whole comment to the function below. |
| 687 | * - Delete the chain using FLAC__metadata_chain_delete(). |
| 688 | * |
| 689 | * \note |
| 690 | * Even though the FLAC file is not open while the chain is being |
| 691 | * manipulated, you must not alter the file externally during |
| 692 | * this time. The chain assumes the FLAC file will not change |
| 693 | * between the time of FLAC__metadata_chain_read()/FLAC__metadata_chain_read_ogg() |
| 694 | * and FLAC__metadata_chain_write(). |
| 695 | * |
| 696 | * \note |
| 697 | * Do not modify the is_last, length, or type fields of returned |
| 698 | * FLAC__StreamMetadata objects. These are managed automatically. |
| 699 | * |
| 700 | * \note |
| 701 | * The metadata objects returned by FLAC__metadata_iterator_get_block() |
| 702 | * are owned by the chain; do not FLAC__metadata_object_delete() them. |
| 703 | * In the same way, blocks passed to FLAC__metadata_iterator_set_block() |
| 704 | * become owned by the chain and they will be deleted when the chain is |
| 705 | * deleted. |
| 706 | * |
| 707 | * \{ |
| 708 | */ |
| 709 | |
| 710 | struct FLAC__Metadata_Chain; |
| 711 | /** The opaque structure definition for the level 2 chain type. |
| 712 | */ |
| 713 | typedef struct FLAC__Metadata_Chain FLAC__Metadata_Chain; |
| 714 | |
| 715 | struct FLAC__Metadata_Iterator; |
| 716 | /** The opaque structure definition for the level 2 iterator type. |
| 717 | */ |
| 718 | typedef struct FLAC__Metadata_Iterator FLAC__Metadata_Iterator; |
| 719 | |
| 720 | typedef enum { |
| 721 | FLAC__METADATA_CHAIN_STATUS_OK = 0, |
| 722 | /**< The chain is in the normal OK state */ |
| 723 | |
| 724 | FLAC__METADATA_CHAIN_STATUS_ILLEGAL_INPUT, |
| 725 | /**< The data passed into a function violated the function's usage criteria */ |
| 726 | |
| 727 | FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE, |
| 728 | /**< The chain could not open the target file */ |
| 729 | |
| 730 | FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE, |
| 731 | /**< The chain could not find the FLAC signature at the start of the file */ |
| 732 | |
| 733 | FLAC__METADATA_CHAIN_STATUS_NOT_WRITABLE, |
| 734 | /**< The chain tried to write to a file that was not writable */ |
| 735 | |
| 736 | FLAC__METADATA_CHAIN_STATUS_BAD_METADATA, |
| 737 | /**< The chain encountered input that does not conform to the FLAC metadata specification */ |
| 738 | |
| 739 | FLAC__METADATA_CHAIN_STATUS_READ_ERROR, |
| 740 | /**< The chain encountered an error while reading the FLAC file */ |
| 741 | |
| 742 | FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR, |
| 743 | /**< The chain encountered an error while seeking in the FLAC file */ |
| 744 | |
| 745 | FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR, |
| 746 | /**< The chain encountered an error while writing the FLAC file */ |
| 747 | |
| 748 | FLAC__METADATA_CHAIN_STATUS_RENAME_ERROR, |
| 749 | /**< The chain encountered an error renaming the FLAC file */ |
| 750 | |
| 751 | FLAC__METADATA_CHAIN_STATUS_UNLINK_ERROR, |
| 752 | /**< The chain encountered an error removing the temporary file */ |
| 753 | |
| 754 | FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR, |
| 755 | /**< Memory allocation failed */ |
| 756 | |
| 757 | FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR, |
| 758 | /**< The caller violated an assertion or an unexpected error occurred */ |
| 759 | |
| 760 | FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS, |
| 761 | /**< One or more of the required callbacks was NULL */ |
| 762 | |
| 763 | FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH, |
| 764 | /**< FLAC__metadata_chain_write() was called on a chain read by |
| 765 | * FLAC__metadata_chain_read_with_callbacks()/FLAC__metadata_chain_read_ogg_with_callbacks(), |
| 766 | * or |
| 767 | * FLAC__metadata_chain_write_with_callbacks()/FLAC__metadata_chain_write_with_callbacks_and_tempfile() |
| 768 | * was called on a chain read by |
| 769 | * FLAC__metadata_chain_read()/FLAC__metadata_chain_read_ogg(). |
| 770 | * Matching read/write methods must always be used. */ |
| 771 | |
| 772 | FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL |
| 773 | /**< FLAC__metadata_chain_write_with_callbacks() was called when the |
| 774 | * chain write requires a tempfile; use |
| 775 | * FLAC__metadata_chain_write_with_callbacks_and_tempfile() instead. |
| 776 | * Or, FLAC__metadata_chain_write_with_callbacks_and_tempfile() was |
| 777 | * called when the chain write does not require a tempfile; use |
| 778 | * FLAC__metadata_chain_write_with_callbacks() instead. |
| 779 | * Always check FLAC__metadata_chain_check_if_tempfile_needed() |
| 780 | * before writing via callbacks. */ |
| 781 | |
| 782 | } FLAC__Metadata_ChainStatus; |
| 783 | |
| 784 | /** Maps a FLAC__Metadata_ChainStatus to a C string. |
| 785 | * |
| 786 | * Using a FLAC__Metadata_ChainStatus as the index to this array |
| 787 | * will give the string equivalent. The contents should not be modified. |
| 788 | */ |
| 789 | extern FLAC_API const char * const FLAC__Metadata_ChainStatusString[]; |
| 790 | |
| 791 | /*********** FLAC__Metadata_Chain ***********/ |
| 792 | |
| 793 | /** Create a new chain instance. |
| 794 | * |
| 795 | * \retval FLAC__Metadata_Chain* |
| 796 | * \c NULL if there was an error allocating memory, else the new instance. |
| 797 | */ |
| 798 | FLAC_API FLAC__Metadata_Chain *FLAC__metadata_chain_new(void); |
| 799 | |
| 800 | /** Free a chain instance. Deletes the object pointed to by \a chain. |
| 801 | * |
| 802 | * \param chain A pointer to an existing chain. |
| 803 | * \assert |
| 804 | * \code chain != NULL \endcode |
| 805 | */ |
| 806 | FLAC_API void FLAC__metadata_chain_delete(FLAC__Metadata_Chain *chain); |
| 807 | |
| 808 | /** Get the current status of the chain. Call this after a function |
| 809 | * returns \c false to get the reason for the error. Also resets the |
| 810 | * status to FLAC__METADATA_CHAIN_STATUS_OK. |
| 811 | * |
| 812 | * \param chain A pointer to an existing chain. |
| 813 | * \assert |
| 814 | * \code chain != NULL \endcode |
| 815 | * \retval FLAC__Metadata_ChainStatus |
| 816 | * The current status of the chain. |
| 817 | */ |
| 818 | FLAC_API FLAC__Metadata_ChainStatus FLAC__metadata_chain_status(FLAC__Metadata_Chain *chain); |
| 819 | |
| 820 | /** Read all metadata from a FLAC file into the chain. |
| 821 | * |
| 822 | * \param chain A pointer to an existing chain. |
| 823 | * \param filename The path to the FLAC file to read. |
| 824 | * \assert |
| 825 | * \code chain != NULL \endcode |
| 826 | * \code filename != NULL \endcode |
| 827 | * \retval FLAC__bool |
| 828 | * \c true if a valid list of metadata blocks was read from |
| 829 | * \a filename, else \c false. On failure, check the status with |
| 830 | * FLAC__metadata_chain_status(). |
| 831 | */ |
| 832 | FLAC_API FLAC__bool FLAC__metadata_chain_read(FLAC__Metadata_Chain *chain, const char *filename); |
| 833 | |
| 834 | /** Read all metadata from an Ogg FLAC file into the chain. |
| 835 | * |
| 836 | * \note Ogg FLAC metadata data writing is not supported yet and |
| 837 | * FLAC__metadata_chain_write() will fail. |
| 838 | * |
| 839 | * \param chain A pointer to an existing chain. |
| 840 | * \param filename The path to the Ogg FLAC file to read. |
| 841 | * \assert |
| 842 | * \code chain != NULL \endcode |
| 843 | * \code filename != NULL \endcode |
| 844 | * \retval FLAC__bool |
| 845 | * \c true if a valid list of metadata blocks was read from |
| 846 | * \a filename, else \c false. On failure, check the status with |
| 847 | * FLAC__metadata_chain_status(). |
| 848 | */ |
| 849 | FLAC_API FLAC__bool FLAC__metadata_chain_read_ogg(FLAC__Metadata_Chain *chain, const char *filename); |
| 850 | |
| 851 | /** Read all metadata from a FLAC stream into the chain via I/O callbacks. |
| 852 | * |
| 853 | * The \a handle need only be open for reading, but must be seekable. |
| 854 | * The equivalent minimum stdio fopen() file mode is \c "r" (or \c "rb" |
| 855 | * for Windows). |
| 856 | * |
| 857 | * \param chain A pointer to an existing chain. |
| 858 | * \param handle The I/O handle of the FLAC stream to read. The |
| 859 | * handle will NOT be closed after the metadata is read; |
| 860 | * that is the duty of the caller. |
| 861 | * \param callbacks |
| 862 | * A set of callbacks to use for I/O. The mandatory |
| 863 | * callbacks are \a read, \a seek, and \a tell. |
| 864 | * \assert |
| 865 | * \code chain != NULL \endcode |
| 866 | * \retval FLAC__bool |
| 867 | * \c true if a valid list of metadata blocks was read from |
| 868 | * \a handle, else \c false. On failure, check the status with |
| 869 | * FLAC__metadata_chain_status(). |
| 870 | */ |
| 871 | FLAC_API FLAC__bool FLAC__metadata_chain_read_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks); |
| 872 | |
| 873 | /** Read all metadata from an Ogg FLAC stream into the chain via I/O callbacks. |
| 874 | * |
| 875 | * The \a handle need only be open for reading, but must be seekable. |
| 876 | * The equivalent minimum stdio fopen() file mode is \c "r" (or \c "rb" |
| 877 | * for Windows). |
| 878 | * |
| 879 | * \note Ogg FLAC metadata data writing is not supported yet and |
| 880 | * FLAC__metadata_chain_write() will fail. |
| 881 | * |
| 882 | * \param chain A pointer to an existing chain. |
| 883 | * \param handle The I/O handle of the Ogg FLAC stream to read. The |
| 884 | * handle will NOT be closed after the metadata is read; |
| 885 | * that is the duty of the caller. |
| 886 | * \param callbacks |
| 887 | * A set of callbacks to use for I/O. The mandatory |
| 888 | * callbacks are \a read, \a seek, and \a tell. |
| 889 | * \assert |
| 890 | * \code chain != NULL \endcode |
| 891 | * \retval FLAC__bool |
| 892 | * \c true if a valid list of metadata blocks was read from |
| 893 | * \a handle, else \c false. On failure, check the status with |
| 894 | * FLAC__metadata_chain_status(). |
| 895 | */ |
| 896 | FLAC_API FLAC__bool FLAC__metadata_chain_read_ogg_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks); |
| 897 | |
| 898 | /** Checks if writing the given chain would require the use of a |
| 899 | * temporary file, or if it could be written in place. |
| 900 | * |
| 901 | * Under certain conditions, padding can be utilized so that writing |
| 902 | * edited metadata back to the FLAC file does not require rewriting the |
| 903 | * entire file. If rewriting is required, then a temporary workfile is |
| 904 | * required. When writing metadata using callbacks, you must check |
| 905 | * this function to know whether to call |
| 906 | * FLAC__metadata_chain_write_with_callbacks() or |
| 907 | * FLAC__metadata_chain_write_with_callbacks_and_tempfile(). When |
| 908 | * writing with FLAC__metadata_chain_write(), the temporary file is |
| 909 | * handled internally. |
| 910 | * |
| 911 | * \param chain A pointer to an existing chain. |
| 912 | * \param use_padding |
| 913 | * Whether or not padding will be allowed to be used |
| 914 | * during the write. The value of \a use_padding given |
| 915 | * here must match the value later passed to |
| 916 | * FLAC__metadata_chain_write_with_callbacks() or |
| 917 | * FLAC__metadata_chain_write_with_callbacks_with_tempfile(). |
| 918 | * \assert |
| 919 | * \code chain != NULL \endcode |
| 920 | * \retval FLAC__bool |
| 921 | * \c true if writing the current chain would require a tempfile, or |
| 922 | * \c false if metadata can be written in place. |
| 923 | */ |
| 924 | FLAC_API FLAC__bool FLAC__metadata_chain_check_if_tempfile_needed(FLAC__Metadata_Chain *chain, FLAC__bool use_padding); |
| 925 | |
| 926 | /** Write all metadata out to the FLAC file. This function tries to be as |
| 927 | * efficient as possible; how the metadata is actually written is shown by |
| 928 | * the following: |
| 929 | * |
| 930 | * If the current chain is the same size as the existing metadata, the new |
| 931 | * data is written in place. |
| 932 | * |
| 933 | * If the current chain is longer than the existing metadata, and |
| 934 | * \a use_padding is \c true, and the last block is a PADDING block of |
| 935 | * sufficient length, the function will truncate the final padding block |
| 936 | * so that the overall size of the metadata is the same as the existing |
| 937 | * metadata, and then just rewrite the metadata. Otherwise, if not all of |
| 938 | * the above conditions are met, the entire FLAC file must be rewritten. |
| 939 | * If you want to use padding this way it is a good idea to call |
| 940 | * FLAC__metadata_chain_sort_padding() first so that you have the maximum |
| 941 | * amount of padding to work with, unless you need to preserve ordering |
| 942 | * of the PADDING blocks for some reason. |
| 943 | * |
| 944 | * If the current chain is shorter than the existing metadata, and |
| 945 | * \a use_padding is \c true, and the final block is a PADDING block, the padding |
| 946 | * is extended to make the overall size the same as the existing data. If |
| 947 | * \a use_padding is \c true and the last block is not a PADDING block, a new |
| 948 | * PADDING block is added to the end of the new data to make it the same |
| 949 | * size as the existing data (if possible, see the note to |
| 950 | * FLAC__metadata_simple_iterator_set_block() about the four byte limit) |
| 951 | * and the new data is written in place. If none of the above apply or |
| 952 | * \a use_padding is \c false, the entire FLAC file is rewritten. |
| 953 | * |
| 954 | * If \a preserve_file_stats is \c true, the owner and modification time will |
| 955 | * be preserved even if the FLAC file is written. |
| 956 | * |
| 957 | * For this write function to be used, the chain must have been read with |
| 958 | * FLAC__metadata_chain_read()/FLAC__metadata_chain_read_ogg(), not |
| 959 | * FLAC__metadata_chain_read_with_callbacks()/FLAC__metadata_chain_read_ogg_with_callbacks(). |
| 960 | * |
| 961 | * \param chain A pointer to an existing chain. |
| 962 | * \param use_padding See above. |
| 963 | * \param preserve_file_stats See above. |
| 964 | * \assert |
| 965 | * \code chain != NULL \endcode |
| 966 | * \retval FLAC__bool |
| 967 | * \c true if the write succeeded, else \c false. On failure, |
| 968 | * check the status with FLAC__metadata_chain_status(). |
| 969 | */ |
| 970 | FLAC_API FLAC__bool FLAC__metadata_chain_write(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__bool preserve_file_stats); |
| 971 | |
| 972 | /** Write all metadata out to a FLAC stream via callbacks. |
| 973 | * |
| 974 | * (See FLAC__metadata_chain_write() for the details on how padding is |
| 975 | * used to write metadata in place if possible.) |
| 976 | * |
| 977 | * The \a handle must be open for updating and be seekable. The |
| 978 | * equivalent minimum stdio fopen() file mode is \c "r+" (or \c "r+b" |
| 979 | * for Windows). |
| 980 | * |
| 981 | * For this write function to be used, the chain must have been read with |
| 982 | * FLAC__metadata_chain_read_with_callbacks()/FLAC__metadata_chain_read_ogg_with_callbacks(), |
| 983 | * not FLAC__metadata_chain_read()/FLAC__metadata_chain_read_ogg(). |
| 984 | * Also, FLAC__metadata_chain_check_if_tempfile_needed() must have returned |
| 985 | * \c false. |
| 986 | * |
| 987 | * \param chain A pointer to an existing chain. |
| 988 | * \param use_padding See FLAC__metadata_chain_write() |
| 989 | * \param handle The I/O handle of the FLAC stream to write. The |
| 990 | * handle will NOT be closed after the metadata is |
| 991 | * written; that is the duty of the caller. |
| 992 | * \param callbacks A set of callbacks to use for I/O. The mandatory |
| 993 | * callbacks are \a write and \a seek. |
| 994 | * \assert |
| 995 | * \code chain != NULL \endcode |
| 996 | * \retval FLAC__bool |
| 997 | * \c true if the write succeeded, else \c false. On failure, |
| 998 | * check the status with FLAC__metadata_chain_status(). |
| 999 | */ |
| 1000 | FLAC_API FLAC__bool FLAC__metadata_chain_write_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks); |
| 1001 | |
| 1002 | /** Write all metadata out to a FLAC stream via callbacks. |
| 1003 | * |
| 1004 | * (See FLAC__metadata_chain_write() for the details on how padding is |
| 1005 | * used to write metadata in place if possible.) |
| 1006 | * |
| 1007 | * This version of the write-with-callbacks function must be used when |
| 1008 | * FLAC__metadata_chain_check_if_tempfile_needed() returns true. In |
| 1009 | * this function, you must supply an I/O handle corresponding to the |
| 1010 | * FLAC file to edit, and a temporary handle to which the new FLAC |
| 1011 | * file will be written. It is the caller's job to move this temporary |
| 1012 | * FLAC file on top of the original FLAC file to complete the metadata |
| 1013 | * edit. |
| 1014 | * |
| 1015 | * The \a handle must be open for reading and be seekable. The |
| 1016 | * equivalent minimum stdio fopen() file mode is \c "r" (or \c "rb" |
| 1017 | * for Windows). |
| 1018 | * |
| 1019 | * The \a temp_handle must be open for writing. The |
| 1020 | * equivalent minimum stdio fopen() file mode is \c "w" (or \c "wb" |
| 1021 | * for Windows). It should be an empty stream, or at least positioned |
| 1022 | * at the start-of-file (in which case it is the caller's duty to |
| 1023 | * truncate it on return). |
| 1024 | * |
| 1025 | * For this write function to be used, the chain must have been read with |
| 1026 | * FLAC__metadata_chain_read_with_callbacks()/FLAC__metadata_chain_read_ogg_with_callbacks(), |
| 1027 | * not FLAC__metadata_chain_read()/FLAC__metadata_chain_read_ogg(). |
| 1028 | * Also, FLAC__metadata_chain_check_if_tempfile_needed() must have returned |
| 1029 | * \c true. |
| 1030 | * |
| 1031 | * \param chain A pointer to an existing chain. |
| 1032 | * \param use_padding See FLAC__metadata_chain_write() |
| 1033 | * \param handle The I/O handle of the original FLAC stream to read. |
| 1034 | * The handle will NOT be closed after the metadata is |
| 1035 | * written; that is the duty of the caller. |
| 1036 | * \param callbacks A set of callbacks to use for I/O on \a handle. |
| 1037 | * The mandatory callbacks are \a read, \a seek, and |
| 1038 | * \a eof. |
| 1039 | * \param temp_handle The I/O handle of the FLAC stream to write. The |
| 1040 | * handle will NOT be closed after the metadata is |
| 1041 | * written; that is the duty of the caller. |
| 1042 | * \param temp_callbacks |
| 1043 | * A set of callbacks to use for I/O on temp_handle. |
| 1044 | * The only mandatory callback is \a write. |
| 1045 | * \assert |
| 1046 | * \code chain != NULL \endcode |
| 1047 | * \retval FLAC__bool |
| 1048 | * \c true if the write succeeded, else \c false. On failure, |
| 1049 | * check the status with FLAC__metadata_chain_status(). |
| 1050 | */ |
| 1051 | FLAC_API FLAC__bool FLAC__metadata_chain_write_with_callbacks_and_tempfile(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks, FLAC__IOHandle temp_handle, FLAC__IOCallbacks temp_callbacks); |
| 1052 | |
| 1053 | /** Merge adjacent PADDING blocks into a single block. |
| 1054 | * |
| 1055 | * \note This function does not write to the FLAC file, it only |
| 1056 | * modifies the chain. |
| 1057 | * |
| 1058 | * \warning Any iterator on the current chain will become invalid after this |
| 1059 | * call. You should delete the iterator and get a new one. |
| 1060 | * |
| 1061 | * \param chain A pointer to an existing chain. |
| 1062 | * \assert |
| 1063 | * \code chain != NULL \endcode |
| 1064 | */ |
| 1065 | FLAC_API void FLAC__metadata_chain_merge_padding(FLAC__Metadata_Chain *chain); |
| 1066 | |
| 1067 | /** This function will move all PADDING blocks to the end on the metadata, |
| 1068 | * then merge them into a single block. |
| 1069 | * |
| 1070 | * \note This function does not write to the FLAC file, it only |
| 1071 | * modifies the chain. |
| 1072 | * |
| 1073 | * \warning Any iterator on the current chain will become invalid after this |
| 1074 | * call. You should delete the iterator and get a new one. |
| 1075 | * |
| 1076 | * \param chain A pointer to an existing chain. |
| 1077 | * \assert |
| 1078 | * \code chain != NULL \endcode |
| 1079 | */ |
| 1080 | FLAC_API void FLAC__metadata_chain_sort_padding(FLAC__Metadata_Chain *chain); |
| 1081 | |
| 1082 | |
| 1083 | /*********** FLAC__Metadata_Iterator ***********/ |
| 1084 | |
| 1085 | /** Create a new iterator instance. |
| 1086 | * |
| 1087 | * \retval FLAC__Metadata_Iterator* |
| 1088 | * \c NULL if there was an error allocating memory, else the new instance. |
| 1089 | */ |
| 1090 | FLAC_API FLAC__Metadata_Iterator *FLAC__metadata_iterator_new(void); |
| 1091 | |
| 1092 | /** Free an iterator instance. Deletes the object pointed to by \a iterator. |
| 1093 | * |
| 1094 | * \param iterator A pointer to an existing iterator. |
| 1095 | * \assert |
| 1096 | * \code iterator != NULL \endcode |
| 1097 | */ |
| 1098 | FLAC_API void FLAC__metadata_iterator_delete(FLAC__Metadata_Iterator *iterator); |
| 1099 | |
| 1100 | /** Initialize the iterator to point to the first metadata block in the |
| 1101 | * given chain. |
| 1102 | * |
| 1103 | * \param iterator A pointer to an existing iterator. |
| 1104 | * \param chain A pointer to an existing and initialized (read) chain. |
| 1105 | * \assert |
| 1106 | * \code iterator != NULL \endcode |
| 1107 | * \code chain != NULL \endcode |
| 1108 | */ |
| 1109 | FLAC_API void FLAC__metadata_iterator_init(FLAC__Metadata_Iterator *iterator, FLAC__Metadata_Chain *chain); |
| 1110 | |
| 1111 | /** Moves the iterator forward one metadata block, returning \c false if |
| 1112 | * already at the end. |
| 1113 | * |
| 1114 | * \param iterator A pointer to an existing initialized iterator. |
| 1115 | * \assert |
| 1116 | * \code iterator != NULL \endcode |
| 1117 | * \a iterator has been successfully initialized with |
| 1118 | * FLAC__metadata_iterator_init() |
| 1119 | * \retval FLAC__bool |
| 1120 | * \c false if already at the last metadata block of the chain, else |
| 1121 | * \c true. |
| 1122 | */ |
| 1123 | FLAC_API FLAC__bool FLAC__metadata_iterator_next(FLAC__Metadata_Iterator *iterator); |
| 1124 | |
| 1125 | /** Moves the iterator backward one metadata block, returning \c false if |
| 1126 | * already at the beginning. |
| 1127 | * |
| 1128 | * \param iterator A pointer to an existing initialized iterator. |
| 1129 | * \assert |
| 1130 | * \code iterator != NULL \endcode |
| 1131 | * \a iterator has been successfully initialized with |
| 1132 | * FLAC__metadata_iterator_init() |
| 1133 | * \retval FLAC__bool |
| 1134 | * \c false if already at the first metadata block of the chain, else |
| 1135 | * \c true. |
| 1136 | */ |
| 1137 | FLAC_API FLAC__bool FLAC__metadata_iterator_prev(FLAC__Metadata_Iterator *iterator); |
| 1138 | |
| 1139 | /** Get the type of the metadata block at the current position. |
| 1140 | * |
| 1141 | * \param iterator A pointer to an existing initialized iterator. |
| 1142 | * \assert |
| 1143 | * \code iterator != NULL \endcode |
| 1144 | * \a iterator has been successfully initialized with |
| 1145 | * FLAC__metadata_iterator_init() |
| 1146 | * \retval FLAC__MetadataType |
| 1147 | * The type of the metadata block at the current iterator position. |
| 1148 | */ |
| 1149 | FLAC_API FLAC__MetadataType FLAC__metadata_iterator_get_block_type(const FLAC__Metadata_Iterator *iterator); |
| 1150 | |
| 1151 | /** Get the metadata block at the current position. You can modify |
| 1152 | * the block in place but must write the chain before the changes |
| 1153 | * are reflected to the FLAC file. You do not need to call |
| 1154 | * FLAC__metadata_iterator_set_block() to reflect the changes; |
| 1155 | * the pointer returned by FLAC__metadata_iterator_get_block() |
| 1156 | * points directly into the chain. |
| 1157 | * |
| 1158 | * \warning |
| 1159 | * Do not call FLAC__metadata_object_delete() on the returned object; |
| 1160 | * to delete a block use FLAC__metadata_iterator_delete_block(). |
| 1161 | * |
| 1162 | * \param iterator A pointer to an existing initialized iterator. |
| 1163 | * \assert |
| 1164 | * \code iterator != NULL \endcode |
| 1165 | * \a iterator has been successfully initialized with |
| 1166 | * FLAC__metadata_iterator_init() |
| 1167 | * \retval FLAC__StreamMetadata* |
| 1168 | * The current metadata block. |
| 1169 | */ |
| 1170 | FLAC_API FLAC__StreamMetadata *FLAC__metadata_iterator_get_block(FLAC__Metadata_Iterator *iterator); |
| 1171 | |
| 1172 | /** Set the metadata block at the current position, replacing the existing |
| 1173 | * block. The new block passed in becomes owned by the chain and it will be |
| 1174 | * deleted when the chain is deleted. |
| 1175 | * |
| 1176 | * \param iterator A pointer to an existing initialized iterator. |
| 1177 | * \param block A pointer to a metadata block. |
| 1178 | * \assert |
| 1179 | * \code iterator != NULL \endcode |
| 1180 | * \a iterator has been successfully initialized with |
| 1181 | * FLAC__metadata_iterator_init() |
| 1182 | * \code block != NULL \endcode |
| 1183 | * \retval FLAC__bool |
| 1184 | * \c false if the conditions in the above description are not met, or |
| 1185 | * a memory allocation error occurs, otherwise \c true. |
| 1186 | */ |
| 1187 | FLAC_API FLAC__bool FLAC__metadata_iterator_set_block(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block); |
| 1188 | |
| 1189 | /** Removes the current block from the chain. If \a replace_with_padding is |
| 1190 | * \c true, the block will instead be replaced with a padding block of equal |
| 1191 | * size. You can not delete the STREAMINFO block. The iterator will be |
| 1192 | * left pointing to the block before the one just "deleted", even if |
| 1193 | * \a replace_with_padding is \c true. |
| 1194 | * |
| 1195 | * \param iterator A pointer to an existing initialized iterator. |
| 1196 | * \param replace_with_padding See above. |
| 1197 | * \assert |
| 1198 | * \code iterator != NULL \endcode |
| 1199 | * \a iterator has been successfully initialized with |
| 1200 | * FLAC__metadata_iterator_init() |
| 1201 | * \retval FLAC__bool |
| 1202 | * \c false if the conditions in the above description are not met, |
| 1203 | * otherwise \c true. |
| 1204 | */ |
| 1205 | FLAC_API FLAC__bool FLAC__metadata_iterator_delete_block(FLAC__Metadata_Iterator *iterator, FLAC__bool replace_with_padding); |
| 1206 | |
| 1207 | /** Insert a new block before the current block. You cannot insert a block |
| 1208 | * before the first STREAMINFO block. You cannot insert a STREAMINFO block |
| 1209 | * as there can be only one, the one that already exists at the head when you |
| 1210 | * read in a chain. The chain takes ownership of the new block and it will be |
| 1211 | * deleted when the chain is deleted. The iterator will be left pointing to |
| 1212 | * the new block. |
| 1213 | * |
| 1214 | * \param iterator A pointer to an existing initialized iterator. |
| 1215 | * \param block A pointer to a metadata block to insert. |
| 1216 | * \assert |
| 1217 | * \code iterator != NULL \endcode |
| 1218 | * \a iterator has been successfully initialized with |
| 1219 | * FLAC__metadata_iterator_init() |
| 1220 | * \retval FLAC__bool |
| 1221 | * \c false if the conditions in the above description are not met, or |
| 1222 | * a memory allocation error occurs, otherwise \c true. |
| 1223 | */ |
| 1224 | FLAC_API FLAC__bool FLAC__metadata_iterator_insert_block_before(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block); |
| 1225 | |
| 1226 | /** Insert a new block after the current block. You cannot insert a STREAMINFO |
| 1227 | * block as there can be only one, the one that already exists at the head when |
| 1228 | * you read in a chain. The chain takes ownership of the new block and it will |
| 1229 | * be deleted when the chain is deleted. The iterator will be left pointing to |
| 1230 | * the new block. |
| 1231 | * |
| 1232 | * \param iterator A pointer to an existing initialized iterator. |
| 1233 | * \param block A pointer to a metadata block to insert. |
| 1234 | * \assert |
| 1235 | * \code iterator != NULL \endcode |
| 1236 | * \a iterator has been successfully initialized with |
| 1237 | * FLAC__metadata_iterator_init() |
| 1238 | * \retval FLAC__bool |
| 1239 | * \c false if the conditions in the above description are not met, or |
| 1240 | * a memory allocation error occurs, otherwise \c true. |
| 1241 | */ |
| 1242 | FLAC_API FLAC__bool FLAC__metadata_iterator_insert_block_after(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block); |
| 1243 | |
| 1244 | /* \} */ |
| 1245 | |
| 1246 | |
| 1247 | /** \defgroup flac_metadata_object FLAC/metadata.h: metadata object methods |
| 1248 | * \ingroup flac_metadata |
| 1249 | * |
| 1250 | * \brief |
| 1251 | * This module contains methods for manipulating FLAC metadata objects. |
| 1252 | * |
| 1253 | * Since many are variable length we have to be careful about the memory |
| 1254 | * management. We decree that all pointers to data in the object are |
| 1255 | * owned by the object and memory-managed by the object. |
| 1256 | * |
| 1257 | * Use the FLAC__metadata_object_new() and FLAC__metadata_object_delete() |
| 1258 | * functions to create all instances. When using the |
| 1259 | * FLAC__metadata_object_set_*() functions to set pointers to data, set |
| 1260 | * \a copy to \c true to have the function make it's own copy of the data, or |
| 1261 | * to \c false to give the object ownership of your data. In the latter case |
| 1262 | * your pointer must be freeable by free() and will be free()d when the object |
| 1263 | * is FLAC__metadata_object_delete()d. It is legal to pass a null pointer as |
| 1264 | * the data pointer to a FLAC__metadata_object_set_*() function as long as |
| 1265 | * the length argument is 0 and the \a copy argument is \c false. |
| 1266 | * |
| 1267 | * The FLAC__metadata_object_new() and FLAC__metadata_object_clone() function |
| 1268 | * will return \c NULL in the case of a memory allocation error, otherwise a new |
| 1269 | * object. The FLAC__metadata_object_set_*() functions return \c false in the |
| 1270 | * case of a memory allocation error. |
| 1271 | * |
| 1272 | * We don't have the convenience of C++ here, so note that the library relies |
| 1273 | * on you to keep the types straight. In other words, if you pass, for |
| 1274 | * example, a FLAC__StreamMetadata* that represents a STREAMINFO block to |
| 1275 | * FLAC__metadata_object_application_set_data(), you will get an assertion |
| 1276 | * failure. |
| 1277 | * |
| 1278 | * For convenience the FLAC__metadata_object_vorbiscomment_*() functions |
| 1279 | * maintain a trailing NUL on each Vorbis comment entry. This is not counted |
| 1280 | * toward the length or stored in the stream, but it can make working with plain |
| 1281 | * comments (those that don't contain embedded-NULs in the value) easier. |
| 1282 | * Entries passed into these functions have trailing NULs added if missing, and |
| 1283 | * returned entries are guaranteed to have a trailing NUL. |
| 1284 | * |
| 1285 | * The FLAC__metadata_object_vorbiscomment_*() functions that take a Vorbis |
| 1286 | * comment entry/name/value will first validate that it complies with the Vorbis |
| 1287 | * comment specification and return false if it does not. |
| 1288 | * |
| 1289 | * There is no need to recalculate the length field on metadata blocks you |
| 1290 | * have modified. They will be calculated automatically before they are |
| 1291 | * written back to a file. |
| 1292 | * |
| 1293 | * \{ |
| 1294 | */ |
| 1295 | |
| 1296 | |
| 1297 | /** Create a new metadata object instance of the given type. |
| 1298 | * |
| 1299 | * The object will be "empty"; i.e. values and data pointers will be \c 0, |
| 1300 | * with the exception of FLAC__METADATA_TYPE_VORBIS_COMMENT, which will have |
| 1301 | * the vendor string set (but zero comments). |
| 1302 | * |
| 1303 | * Do not pass in a value greater than or equal to |
| 1304 | * \a FLAC__METADATA_TYPE_UNDEFINED unless you really know what you're |
| 1305 | * doing. |
| 1306 | * |
| 1307 | * \param type Type of object to create |
| 1308 | * \retval FLAC__StreamMetadata* |
| 1309 | * \c NULL if there was an error allocating memory or the type code is |
| 1310 | * greater than FLAC__MAX_METADATA_TYPE_CODE, else the new instance. |
| 1311 | */ |
| 1312 | FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_new(FLAC__MetadataType type); |
| 1313 | |
| 1314 | /** Create a copy of an existing metadata object. |
| 1315 | * |
| 1316 | * The copy is a "deep" copy, i.e. dynamically allocated data within the |
| 1317 | * object is also copied. The caller takes ownership of the new block and |
| 1318 | * is responsible for freeing it with FLAC__metadata_object_delete(). |
| 1319 | * |
| 1320 | * \param object Pointer to object to copy. |
| 1321 | * \assert |
| 1322 | * \code object != NULL \endcode |
| 1323 | * \retval FLAC__StreamMetadata* |
| 1324 | * \c NULL if there was an error allocating memory, else the new instance. |
| 1325 | */ |
| 1326 | FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_clone(const FLAC__StreamMetadata *object); |
| 1327 | |
| 1328 | /** Free a metadata object. Deletes the object pointed to by \a object. |
| 1329 | * |
| 1330 | * The delete is a "deep" delete, i.e. dynamically allocated data within the |
| 1331 | * object is also deleted. |
| 1332 | * |
| 1333 | * \param object A pointer to an existing object. |
| 1334 | * \assert |
| 1335 | * \code object != NULL \endcode |
| 1336 | */ |
| 1337 | FLAC_API void FLAC__metadata_object_delete(FLAC__StreamMetadata *object); |
| 1338 | |
| 1339 | /** Compares two metadata objects. |
| 1340 | * |
| 1341 | * The compare is "deep", i.e. dynamically allocated data within the |
| 1342 | * object is also compared. |
| 1343 | * |
| 1344 | * \param block1 A pointer to an existing object. |
| 1345 | * \param block2 A pointer to an existing object. |
| 1346 | * \assert |
| 1347 | * \code block1 != NULL \endcode |
| 1348 | * \code block2 != NULL \endcode |
| 1349 | * \retval FLAC__bool |
| 1350 | * \c true if objects are identical, else \c false. |
| 1351 | */ |
| 1352 | FLAC_API FLAC__bool FLAC__metadata_object_is_equal(const FLAC__StreamMetadata *block1, const FLAC__StreamMetadata *block2); |
| 1353 | |
| 1354 | /** Sets the application data of an APPLICATION block. |
| 1355 | * |
| 1356 | * If \a copy is \c true, a copy of the data is stored; otherwise, the object |
| 1357 | * takes ownership of the pointer. The existing data will be freed if this |
| 1358 | * function is successful, otherwise the original data will remain if \a copy |
| 1359 | * is \c true and malloc() fails. |
| 1360 | * |
| 1361 | * \note It is safe to pass a const pointer to \a data if \a copy is \c true. |
| 1362 | * |
| 1363 | * \param object A pointer to an existing APPLICATION object. |
| 1364 | * \param data A pointer to the data to set. |
| 1365 | * \param length The length of \a data in bytes. |
| 1366 | * \param copy See above. |
| 1367 | * \assert |
| 1368 | * \code object != NULL \endcode |
| 1369 | * \code object->type == FLAC__METADATA_TYPE_APPLICATION \endcode |
| 1370 | * \code (data != NULL && length > 0) || |
| 1371 | * (data == NULL && length == 0 && copy == false) \endcode |
| 1372 | * \retval FLAC__bool |
| 1373 | * \c false if \a copy is \c true and malloc() fails, else \c true. |
| 1374 | */ |
| 1375 | FLAC_API FLAC__bool FLAC__metadata_object_application_set_data(FLAC__StreamMetadata *object, FLAC__byte *data, unsigned length, FLAC__bool copy); |
| 1376 | |
| 1377 | /** Resize the seekpoint array. |
| 1378 | * |
| 1379 | * If the size shrinks, elements will truncated; if it grows, new placeholder |
| 1380 | * points will be added to the end. |
| 1381 | * |
| 1382 | * \param object A pointer to an existing SEEKTABLE object. |
| 1383 | * \param new_num_points The desired length of the array; may be \c 0. |
| 1384 | * \assert |
| 1385 | * \code object != NULL \endcode |
| 1386 | * \code object->type == FLAC__METADATA_TYPE_SEEKTABLE \endcode |
| 1387 | * \code (object->data.seek_table.points == NULL && object->data.seek_table.num_points == 0) || |
| 1388 | * (object->data.seek_table.points != NULL && object->data.seek_table.num_points > 0) \endcode |
| 1389 | * \retval FLAC__bool |
| 1390 | * \c false if memory allocation error, else \c true. |
| 1391 | */ |
| 1392 | FLAC_API FLAC__bool FLAC__metadata_object_seektable_resize_points(FLAC__StreamMetadata *object, unsigned new_num_points); |
| 1393 | |
| 1394 | /** Set a seekpoint in a seektable. |
| 1395 | * |
| 1396 | * \param object A pointer to an existing SEEKTABLE object. |
| 1397 | * \param point_num Index into seekpoint array to set. |
| 1398 | * \param point The point to set. |
| 1399 | * \assert |
| 1400 | * \code object != NULL \endcode |
| 1401 | * \code object->type == FLAC__METADATA_TYPE_SEEKTABLE \endcode |
| 1402 | * \code object->data.seek_table.num_points > point_num \endcode |
| 1403 | */ |
| 1404 | FLAC_API void FLAC__metadata_object_seektable_set_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point); |
| 1405 | |
| 1406 | /** Insert a seekpoint into a seektable. |
| 1407 | * |
| 1408 | * \param object A pointer to an existing SEEKTABLE object. |
| 1409 | * \param point_num Index into seekpoint array to set. |
| 1410 | * \param point The point to set. |
| 1411 | * \assert |
| 1412 | * \code object != NULL \endcode |
| 1413 | * \code object->type == FLAC__METADATA_TYPE_SEEKTABLE \endcode |
| 1414 | * \code object->data.seek_table.num_points >= point_num \endcode |
| 1415 | * \retval FLAC__bool |
| 1416 | * \c false if memory allocation error, else \c true. |
| 1417 | */ |
| 1418 | FLAC_API FLAC__bool FLAC__metadata_object_seektable_insert_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point); |
| 1419 | |
| 1420 | /** Delete a seekpoint from a seektable. |
| 1421 | * |
| 1422 | * \param object A pointer to an existing SEEKTABLE object. |
| 1423 | * \param point_num Index into seekpoint array to set. |
| 1424 | * \assert |
| 1425 | * \code object != NULL \endcode |
| 1426 | * \code object->type == FLAC__METADATA_TYPE_SEEKTABLE \endcode |
| 1427 | * \code object->data.seek_table.num_points > point_num \endcode |
| 1428 | * \retval FLAC__bool |
| 1429 | * \c false if memory allocation error, else \c true. |
| 1430 | */ |
| 1431 | FLAC_API FLAC__bool FLAC__metadata_object_seektable_delete_point(FLAC__StreamMetadata *object, unsigned point_num); |
| 1432 | |
| 1433 | /** Check a seektable to see if it conforms to the FLAC specification. |
| 1434 | * See the format specification for limits on the contents of the |
| 1435 | * seektable. |
| 1436 | * |
| 1437 | * \param object A pointer to an existing SEEKTABLE object. |
| 1438 | * \assert |
| 1439 | * \code object != NULL \endcode |
| 1440 | * \code object->type == FLAC__METADATA_TYPE_SEEKTABLE \endcode |
| 1441 | * \retval FLAC__bool |
| 1442 | * \c false if seek table is illegal, else \c true. |
| 1443 | */ |
| 1444 | FLAC_API FLAC__bool FLAC__metadata_object_seektable_is_legal(const FLAC__StreamMetadata *object); |
| 1445 | |
| 1446 | /** Append a number of placeholder points to the end of a seek table. |
| 1447 | * |
| 1448 | * \note |
| 1449 | * As with the other ..._seektable_template_... functions, you should |
| 1450 | * call FLAC__metadata_object_seektable_template_sort() when finished |
| 1451 | * to make the seek table legal. |
| 1452 | * |
| 1453 | * \param object A pointer to an existing SEEKTABLE object. |
| 1454 | * \param num The number of placeholder points to append. |
| 1455 | * \assert |
| 1456 | * \code object != NULL \endcode |
| 1457 | * \code object->type == FLAC__METADATA_TYPE_SEEKTABLE \endcode |
| 1458 | * \retval FLAC__bool |
| 1459 | * \c false if memory allocation fails, else \c true. |
| 1460 | */ |
| 1461 | FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_placeholders(FLAC__StreamMetadata *object, unsigned num); |
| 1462 | |
| 1463 | /** Append a specific seek point template to the end of a seek table. |
| 1464 | * |
| 1465 | * \note |
| 1466 | * As with the other ..._seektable_template_... functions, you should |
| 1467 | * call FLAC__metadata_object_seektable_template_sort() when finished |
| 1468 | * to make the seek table legal. |
| 1469 | * |
| 1470 | * \param object A pointer to an existing SEEKTABLE object. |
| 1471 | * \param sample_number The sample number of the seek point template. |
| 1472 | * \assert |
| 1473 | * \code object != NULL \endcode |
| 1474 | * \code object->type == FLAC__METADATA_TYPE_SEEKTABLE \endcode |
| 1475 | * \retval FLAC__bool |
| 1476 | * \c false if memory allocation fails, else \c true. |
| 1477 | */ |
| 1478 | FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_point(FLAC__StreamMetadata *object, FLAC__uint64 sample_number); |
| 1479 | |
| 1480 | /** Append specific seek point templates to the end of a seek table. |
| 1481 | * |
| 1482 | * \note |
| 1483 | * As with the other ..._seektable_template_... functions, you should |
| 1484 | * call FLAC__metadata_object_seektable_template_sort() when finished |
| 1485 | * to make the seek table legal. |
| 1486 | * |
| 1487 | * \param object A pointer to an existing SEEKTABLE object. |
| 1488 | * \param sample_numbers An array of sample numbers for the seek points. |
| 1489 | * \param num The number of seek point templates to append. |
| 1490 | * \assert |
| 1491 | * \code object != NULL \endcode |
| 1492 | * \code object->type == FLAC__METADATA_TYPE_SEEKTABLE \endcode |
| 1493 | * \retval FLAC__bool |
| 1494 | * \c false if memory allocation fails, else \c true. |
| 1495 | */ |
| 1496 | FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_points(FLAC__StreamMetadata *object, FLAC__uint64 sample_numbers[], unsigned num); |
| 1497 | |
| 1498 | /** Append a set of evenly-spaced seek point templates to the end of a |
| 1499 | * seek table. |
| 1500 | * |
| 1501 | * \note |
| 1502 | * As with the other ..._seektable_template_... functions, you should |
| 1503 | * call FLAC__metadata_object_seektable_template_sort() when finished |
| 1504 | * to make the seek table legal. |
| 1505 | * |
| 1506 | * \param object A pointer to an existing SEEKTABLE object. |
| 1507 | * \param num The number of placeholder points to append. |
| 1508 | * \param total_samples The total number of samples to be encoded; |
| 1509 | * the seekpoints will be spaced approximately |
| 1510 | * \a total_samples / \a num samples apart. |
| 1511 | * \assert |
| 1512 | * \code object != NULL \endcode |
| 1513 | * \code object->type == FLAC__METADATA_TYPE_SEEKTABLE \endcode |
| 1514 | * \code total_samples > 0 \endcode |
| 1515 | * \retval FLAC__bool |
| 1516 | * \c false if memory allocation fails, else \c true. |
| 1517 | */ |
| 1518 | FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_spaced_points(FLAC__StreamMetadata *object, unsigned num, FLAC__uint64 total_samples); |
| 1519 | |
| 1520 | /** Append a set of evenly-spaced seek point templates to the end of a |
| 1521 | * seek table. |
| 1522 | * |
| 1523 | * \note |
| 1524 | * As with the other ..._seektable_template_... functions, you should |
| 1525 | * call FLAC__metadata_object_seektable_template_sort() when finished |
| 1526 | * to make the seek table legal. |
| 1527 | * |
| 1528 | * \param object A pointer to an existing SEEKTABLE object. |
| 1529 | * \param samples The number of samples apart to space the placeholder |
| 1530 | * points. The first point will be at sample \c 0, the |
| 1531 | * second at sample \a samples, then 2*\a samples, and |
| 1532 | * so on. As long as \a samples and \a total_samples |
| 1533 | * are greater than \c 0, there will always be at least |
| 1534 | * one seekpoint at sample \c 0. |
| 1535 | * \param total_samples The total number of samples to be encoded; |
| 1536 | * the seekpoints will be spaced |
| 1537 | * \a samples samples apart. |
| 1538 | * \assert |
| 1539 | * \code object != NULL \endcode |
| 1540 | * \code object->type == FLAC__METADATA_TYPE_SEEKTABLE \endcode |
| 1541 | * \code samples > 0 \endcode |
| 1542 | * \code total_samples > 0 \endcode |
| 1543 | * \retval FLAC__bool |
| 1544 | * \c false if memory allocation fails, else \c true. |
| 1545 | */ |
| 1546 | FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_spaced_points_by_samples(FLAC__StreamMetadata *object, unsigned samples, FLAC__uint64 total_samples); |
| 1547 | |
| 1548 | /** Sort a seek table's seek points according to the format specification, |
| 1549 | * removing duplicates. |
| 1550 | * |
| 1551 | * \param object A pointer to a seek table to be sorted. |
| 1552 | * \param compact If \c false, behaves like FLAC__format_seektable_sort(). |
| 1553 | * If \c true, duplicates are deleted and the seek table is |
| 1554 | * shrunk appropriately; the number of placeholder points |
| 1555 | * present in the seek table will be the same after the call |
| 1556 | * as before. |
| 1557 | * \assert |
| 1558 | * \code object != NULL \endcode |
| 1559 | * \code object->type == FLAC__METADATA_TYPE_SEEKTABLE \endcode |
| 1560 | * \retval FLAC__bool |
| 1561 | * \c false if realloc() fails, else \c true. |
| 1562 | */ |
| 1563 | FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_sort(FLAC__StreamMetadata *object, FLAC__bool compact); |
| 1564 | |
| 1565 | /** Sets the vendor string in a VORBIS_COMMENT block. |
| 1566 | * |
| 1567 | * For convenience, a trailing NUL is added to the entry if it doesn't have |
| 1568 | * one already. |
| 1569 | * |
| 1570 | * If \a copy is \c true, a copy of the entry is stored; otherwise, the object |
| 1571 | * takes ownership of the \c entry.entry pointer. |
| 1572 | * |
| 1573 | * \note If this function returns \c false, the caller still owns the |
| 1574 | * pointer. |
| 1575 | * |
| 1576 | * \param object A pointer to an existing VORBIS_COMMENT object. |
| 1577 | * \param entry The entry to set the vendor string to. |
| 1578 | * \param copy See above. |
| 1579 | * \assert |
| 1580 | * \code object != NULL \endcode |
| 1581 | * \code object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT \endcode |
| 1582 | * \code (entry.entry != NULL && entry.length > 0) || |
| 1583 | * (entry.entry == NULL && entry.length == 0) \endcode |
| 1584 | * \retval FLAC__bool |
| 1585 | * \c false if memory allocation fails or \a entry does not comply with the |
| 1586 | * Vorbis comment specification, else \c true. |
| 1587 | */ |
| 1588 | FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_set_vendor_string(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy); |
| 1589 | |
| 1590 | /** Resize the comment array. |
| 1591 | * |
| 1592 | * If the size shrinks, elements will truncated; if it grows, new empty |
| 1593 | * fields will be added to the end. |
| 1594 | * |
| 1595 | * \param object A pointer to an existing VORBIS_COMMENT object. |
| 1596 | * \param new_num_comments The desired length of the array; may be \c 0. |
| 1597 | * \assert |
| 1598 | * \code object != NULL \endcode |
| 1599 | * \code object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT \endcode |
| 1600 | * \code (object->data.vorbis_comment.comments == NULL && object->data.vorbis_comment.num_comments == 0) || |
| 1601 | * (object->data.vorbis_comment.comments != NULL && object->data.vorbis_comment.num_comments > 0) \endcode |
| 1602 | * \retval FLAC__bool |
| 1603 | * \c false if memory allocation fails, else \c true. |
| 1604 | */ |
| 1605 | FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__StreamMetadata *object, unsigned new_num_comments); |
| 1606 | |
| 1607 | /** Sets a comment in a VORBIS_COMMENT block. |
| 1608 | * |
| 1609 | * For convenience, a trailing NUL is added to the entry if it doesn't have |
| 1610 | * one already. |
| 1611 | * |
| 1612 | * If \a copy is \c true, a copy of the entry is stored; otherwise, the object |
| 1613 | * takes ownership of the \c entry.entry pointer. |
| 1614 | * |
| 1615 | * \note If this function returns \c false, the caller still owns the |
| 1616 | * pointer. |
| 1617 | * |
| 1618 | * \param object A pointer to an existing VORBIS_COMMENT object. |
| 1619 | * \param comment_num Index into comment array to set. |
| 1620 | * \param entry The entry to set the comment to. |
| 1621 | * \param copy See above. |
| 1622 | * \assert |
| 1623 | * \code object != NULL \endcode |
| 1624 | * \code object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT \endcode |
| 1625 | * \code comment_num < object->data.vorbis_comment.num_comments \endcode |
| 1626 | * \code (entry.entry != NULL && entry.length > 0) || |
| 1627 | * (entry.entry == NULL && entry.length == 0) \endcode |
| 1628 | * \retval FLAC__bool |
| 1629 | * \c false if memory allocation fails or \a entry does not comply with the |
| 1630 | * Vorbis comment specification, else \c true. |
| 1631 | */ |
| 1632 | FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_set_comment(FLAC__StreamMetadata *object, unsigned comment_num, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy); |
| 1633 | |
| 1634 | /** Insert a comment in a VORBIS_COMMENT block at the given index. |
| 1635 | * |
| 1636 | * For convenience, a trailing NUL is added to the entry if it doesn't have |
| 1637 | * one already. |
| 1638 | * |
| 1639 | * If \a copy is \c true, a copy of the entry is stored; otherwise, the object |
| 1640 | * takes ownership of the \c entry.entry pointer. |
| 1641 | * |
| 1642 | * \note If this function returns \c false, the caller still owns the |
| 1643 | * pointer. |
| 1644 | * |
| 1645 | * \param object A pointer to an existing VORBIS_COMMENT object. |
| 1646 | * \param comment_num The index at which to insert the comment. The comments |
| 1647 | * at and after \a comment_num move right one position. |
| 1648 | * To append a comment to the end, set \a comment_num to |
| 1649 | * \c object->data.vorbis_comment.num_comments . |
| 1650 | * \param entry The comment to insert. |
| 1651 | * \param copy See above. |
| 1652 | * \assert |
| 1653 | * \code object != NULL \endcode |
| 1654 | * \code object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT \endcode |
| 1655 | * \code object->data.vorbis_comment.num_comments >= comment_num \endcode |
| 1656 | * \code (entry.entry != NULL && entry.length > 0) || |
| 1657 | * (entry.entry == NULL && entry.length == 0 && copy == false) \endcode |
| 1658 | * \retval FLAC__bool |
| 1659 | * \c false if memory allocation fails or \a entry does not comply with the |
| 1660 | * Vorbis comment specification, else \c true. |
| 1661 | */ |
| 1662 | FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_insert_comment(FLAC__StreamMetadata *object, unsigned comment_num, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy); |
| 1663 | |
| 1664 | /** Appends a comment to a VORBIS_COMMENT block. |
| 1665 | * |
| 1666 | * For convenience, a trailing NUL is added to the entry if it doesn't have |
| 1667 | * one already. |
| 1668 | * |
| 1669 | * If \a copy is \c true, a copy of the entry is stored; otherwise, the object |
| 1670 | * takes ownership of the \c entry.entry pointer. |
| 1671 | * |
| 1672 | * \note If this function returns \c false, the caller still owns the |
| 1673 | * pointer. |
| 1674 | * |
| 1675 | * \param object A pointer to an existing VORBIS_COMMENT object. |
| 1676 | * \param entry The comment to insert. |
| 1677 | * \param copy See above. |
| 1678 | * \assert |
| 1679 | * \code object != NULL \endcode |
| 1680 | * \code object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT \endcode |
| 1681 | * \code (entry.entry != NULL && entry.length > 0) || |
| 1682 | * (entry.entry == NULL && entry.length == 0 && copy == false) \endcode |
| 1683 | * \retval FLAC__bool |
| 1684 | * \c false if memory allocation fails or \a entry does not comply with the |
| 1685 | * Vorbis comment specification, else \c true. |
| 1686 | */ |
| 1687 | FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_append_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy); |
| 1688 | |
| 1689 | /** Replaces comments in a VORBIS_COMMENT block with a new one. |
| 1690 | * |
| 1691 | * For convenience, a trailing NUL is added to the entry if it doesn't have |
| 1692 | * one already. |
| 1693 | * |
| 1694 | * Depending on the the value of \a all, either all or just the first comment |
| 1695 | * whose field name(s) match the given entry's name will be replaced by the |
| 1696 | * given entry. If no comments match, \a entry will simply be appended. |
| 1697 | * |
| 1698 | * If \a copy is \c true, a copy of the entry is stored; otherwise, the object |
| 1699 | * takes ownership of the \c entry.entry pointer. |
| 1700 | * |
| 1701 | * \note If this function returns \c false, the caller still owns the |
| 1702 | * pointer. |
| 1703 | * |
| 1704 | * \param object A pointer to an existing VORBIS_COMMENT object. |
| 1705 | * \param entry The comment to insert. |
| 1706 | * \param all If \c true, all comments whose field name matches |
| 1707 | * \a entry's field name will be removed, and \a entry will |
| 1708 | * be inserted at the position of the first matching |
| 1709 | * comment. If \c false, only the first comment whose |
| 1710 | * field name matches \a entry's field name will be |
| 1711 | * replaced with \a entry. |
| 1712 | * \param copy See above. |
| 1713 | * \assert |
| 1714 | * \code object != NULL \endcode |
| 1715 | * \code object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT \endcode |
| 1716 | * \code (entry.entry != NULL && entry.length > 0) || |
| 1717 | * (entry.entry == NULL && entry.length == 0 && copy == false) \endcode |
| 1718 | * \retval FLAC__bool |
| 1719 | * \c false if memory allocation fails or \a entry does not comply with the |
| 1720 | * Vorbis comment specification, else \c true. |
| 1721 | */ |
| 1722 | FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_replace_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool all, FLAC__bool copy); |
| 1723 | |
| 1724 | /** Delete a comment in a VORBIS_COMMENT block at the given index. |
| 1725 | * |
| 1726 | * \param object A pointer to an existing VORBIS_COMMENT object. |
| 1727 | * \param comment_num The index of the comment to delete. |
| 1728 | * \assert |
| 1729 | * \code object != NULL \endcode |
| 1730 | * \code object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT \endcode |
| 1731 | * \code object->data.vorbis_comment.num_comments > comment_num \endcode |
| 1732 | * \retval FLAC__bool |
| 1733 | * \c false if realloc() fails, else \c true. |
| 1734 | */ |
| 1735 | FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_delete_comment(FLAC__StreamMetadata *object, unsigned comment_num); |
| 1736 | |
| 1737 | /** Creates a Vorbis comment entry from NUL-terminated name and value strings. |
| 1738 | * |
| 1739 | * On return, the filled-in \a entry->entry pointer will point to malloc()ed |
| 1740 | * memory and shall be owned by the caller. For convenience the entry will |
| 1741 | * have a terminating NUL. |
| 1742 | * |
| 1743 | * \param entry A pointer to a Vorbis comment entry. The entry's |
| 1744 | * \c entry pointer should not point to allocated |
| 1745 | * memory as it will be overwritten. |
| 1746 | * \param field_name The field name in ASCII, \c NUL terminated. |
| 1747 | * \param field_value The field value in UTF-8, \c NUL terminated. |
| 1748 | * \assert |
| 1749 | * \code entry != NULL \endcode |
| 1750 | * \code field_name != NULL \endcode |
| 1751 | * \code field_value != NULL \endcode |
| 1752 | * \retval FLAC__bool |
| 1753 | * \c false if malloc() fails, or if \a field_name or \a field_value does |
| 1754 | * not comply with the Vorbis comment specification, else \c true. |
| 1755 | */ |
| 1756 | FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(FLAC__StreamMetadata_VorbisComment_Entry *entry, const char *field_name, const char *field_value); |
| 1757 | |
| 1758 | /** Splits a Vorbis comment entry into NUL-terminated name and value strings. |
| 1759 | * |
| 1760 | * The returned pointers to name and value will be allocated by malloc() |
| 1761 | * and shall be owned by the caller. |
| 1762 | * |
| 1763 | * \param entry An existing Vorbis comment entry. |
| 1764 | * \param field_name The address of where the returned pointer to the |
| 1765 | * field name will be stored. |
| 1766 | * \param field_value The address of where the returned pointer to the |
| 1767 | * field value will be stored. |
| 1768 | * \assert |
| 1769 | * \code (entry.entry != NULL && entry.length > 0) \endcode |
| 1770 | * \code memchr(entry.entry, '=', entry.length) != NULL \endcode |
| 1771 | * \code field_name != NULL \endcode |
| 1772 | * \code field_value != NULL \endcode |
| 1773 | * \retval FLAC__bool |
| 1774 | * \c false if memory allocation fails or \a entry does not comply with the |
| 1775 | * Vorbis comment specification, else \c true. |
| 1776 | */ |
| 1777 | FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_to_name_value_pair(const FLAC__StreamMetadata_VorbisComment_Entry entry, char **field_name, char **field_value); |
| 1778 | |
| 1779 | /** Check if the given Vorbis comment entry's field name matches the given |
| 1780 | * field name. |
| 1781 | * |
| 1782 | * \param entry An existing Vorbis comment entry. |
| 1783 | * \param field_name The field name to check. |
| 1784 | * \param field_name_length The length of \a field_name, not including the |
| 1785 | * terminating \c NUL. |
| 1786 | * \assert |
| 1787 | * \code (entry.entry != NULL && entry.length > 0) \endcode |
| 1788 | * \retval FLAC__bool |
| 1789 | * \c true if the field names match, else \c false |
| 1790 | */ |
| 1791 | FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_matches(const FLAC__StreamMetadata_VorbisComment_Entry entry, const char *field_name, unsigned field_name_length); |
| 1792 | |
| 1793 | /** Find a Vorbis comment with the given field name. |
| 1794 | * |
| 1795 | * The search begins at entry number \a offset; use an offset of 0 to |
| 1796 | * search from the beginning of the comment array. |
| 1797 | * |
| 1798 | * \param object A pointer to an existing VORBIS_COMMENT object. |
| 1799 | * \param offset The offset into the comment array from where to start |
| 1800 | * the search. |
| 1801 | * \param field_name The field name of the comment to find. |
| 1802 | * \assert |
| 1803 | * \code object != NULL \endcode |
| 1804 | * \code object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT \endcode |
| 1805 | * \code field_name != NULL \endcode |
| 1806 | * \retval int |
| 1807 | * The offset in the comment array of the first comment whose field |
| 1808 | * name matches \a field_name, or \c -1 if no match was found. |
| 1809 | */ |
| 1810 | FLAC_API int FLAC__metadata_object_vorbiscomment_find_entry_from(const FLAC__StreamMetadata *object, unsigned offset, const char *field_name); |
| 1811 | |
| 1812 | /** Remove first Vorbis comment matching the given field name. |
| 1813 | * |
| 1814 | * \param object A pointer to an existing VORBIS_COMMENT object. |
| 1815 | * \param field_name The field name of comment to delete. |
| 1816 | * \assert |
| 1817 | * \code object != NULL \endcode |
| 1818 | * \code object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT \endcode |
| 1819 | * \retval int |
| 1820 | * \c -1 for memory allocation error, \c 0 for no matching entries, |
| 1821 | * \c 1 for one matching entry deleted. |
| 1822 | */ |
| 1823 | FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entry_matching(FLAC__StreamMetadata *object, const char *field_name); |
| 1824 | |
| 1825 | /** Remove all Vorbis comments matching the given field name. |
| 1826 | * |
| 1827 | * \param object A pointer to an existing VORBIS_COMMENT object. |
| 1828 | * \param field_name The field name of comments to delete. |
| 1829 | * \assert |
| 1830 | * \code object != NULL \endcode |
| 1831 | * \code object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT \endcode |
| 1832 | * \retval int |
| 1833 | * \c -1 for memory allocation error, \c 0 for no matching entries, |
| 1834 | * else the number of matching entries deleted. |
| 1835 | */ |
| 1836 | FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entries_matching(FLAC__StreamMetadata *object, const char *field_name); |
| 1837 | |
| 1838 | /** Create a new CUESHEET track instance. |
| 1839 | * |
| 1840 | * The object will be "empty"; i.e. values and data pointers will be \c 0. |
| 1841 | * |
| 1842 | * \retval FLAC__StreamMetadata_CueSheet_Track* |
| 1843 | * \c NULL if there was an error allocating memory, else the new instance. |
| 1844 | */ |
| 1845 | FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_new(void); |
| 1846 | |
| 1847 | /** Create a copy of an existing CUESHEET track object. |
| 1848 | * |
| 1849 | * The copy is a "deep" copy, i.e. dynamically allocated data within the |
| 1850 | * object is also copied. The caller takes ownership of the new object and |
| 1851 | * is responsible for freeing it with |
| 1852 | * FLAC__metadata_object_cuesheet_track_delete(). |
| 1853 | * |
| 1854 | * \param object Pointer to object to copy. |
| 1855 | * \assert |
| 1856 | * \code object != NULL \endcode |
| 1857 | * \retval FLAC__StreamMetadata_CueSheet_Track* |
| 1858 | * \c NULL if there was an error allocating memory, else the new instance. |
| 1859 | */ |
| 1860 | FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_clone(const FLAC__StreamMetadata_CueSheet_Track *object); |
| 1861 | |
| 1862 | /** Delete a CUESHEET track object |
| 1863 | * |
| 1864 | * \param object A pointer to an existing CUESHEET track object. |
| 1865 | * \assert |
| 1866 | * \code object != NULL \endcode |
| 1867 | */ |
| 1868 | FLAC_API void FLAC__metadata_object_cuesheet_track_delete(FLAC__StreamMetadata_CueSheet_Track *object); |
| 1869 | |
| 1870 | /** Resize a track's index point array. |
| 1871 | * |
| 1872 | * If the size shrinks, elements will truncated; if it grows, new blank |
| 1873 | * indices will be added to the end. |
| 1874 | * |
| 1875 | * \param object A pointer to an existing CUESHEET object. |
| 1876 | * \param track_num The index of the track to modify. NOTE: this is not |
| 1877 | * necessarily the same as the track's \a number field. |
| 1878 | * \param new_num_indices The desired length of the array; may be \c 0. |
| 1879 | * \assert |
| 1880 | * \code object != NULL \endcode |
| 1881 | * \code object->type == FLAC__METADATA_TYPE_CUESHEET \endcode |
| 1882 | * \code object->data.cue_sheet.num_tracks > track_num \endcode |
| 1883 | * \code (object->data.cue_sheet.tracks[track_num].indices == NULL && object->data.cue_sheet.tracks[track_num].num_indices == 0) || |
| 1884 | * (object->data.cue_sheet.tracks[track_num].indices != NULL && object->data.cue_sheet.tracks[track_num].num_indices > 0) \endcode |
| 1885 | * \retval FLAC__bool |
| 1886 | * \c false if memory allocation error, else \c true. |
| 1887 | */ |
| 1888 | FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_resize_indices(FLAC__StreamMetadata *object, unsigned track_num, unsigned new_num_indices); |
| 1889 | |
| 1890 | /** Insert an index point in a CUESHEET track at the given index. |
| 1891 | * |
| 1892 | * \param object A pointer to an existing CUESHEET object. |
| 1893 | * \param track_num The index of the track to modify. NOTE: this is not |
| 1894 | * necessarily the same as the track's \a number field. |
| 1895 | * \param index_num The index into the track's index array at which to |
| 1896 | * insert the index point. NOTE: this is not necessarily |
| 1897 | * the same as the index point's \a number field. The |
| 1898 | * indices at and after \a index_num move right one |
| 1899 | * position. To append an index point to the end, set |
| 1900 | * \a index_num to |
| 1901 | * \c object->data.cue_sheet.tracks[track_num].num_indices . |
| 1902 | * \param index The index point to insert. |
| 1903 | * \assert |
| 1904 | * \code object != NULL \endcode |
| 1905 | * \code object->type == FLAC__METADATA_TYPE_CUESHEET \endcode |
| 1906 | * \code object->data.cue_sheet.num_tracks > track_num \endcode |
| 1907 | * \code object->data.cue_sheet.tracks[track_num].num_indices >= index_num \endcode |
| 1908 | * \retval FLAC__bool |
| 1909 | * \c false if realloc() fails, else \c true. |
| 1910 | */ |
| 1911 | FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_insert_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num, FLAC__StreamMetadata_CueSheet_Index index); |
| 1912 | |
| 1913 | /** Insert a blank index point in a CUESHEET track at the given index. |
| 1914 | * |
| 1915 | * A blank index point is one in which all field values are zero. |
| 1916 | * |
| 1917 | * \param object A pointer to an existing CUESHEET object. |
| 1918 | * \param track_num The index of the track to modify. NOTE: this is not |
| 1919 | * necessarily the same as the track's \a number field. |
| 1920 | * \param index_num The index into the track's index array at which to |
| 1921 | * insert the index point. NOTE: this is not necessarily |
| 1922 | * the same as the index point's \a number field. The |
| 1923 | * indices at and after \a index_num move right one |
| 1924 | * position. To append an index point to the end, set |
| 1925 | * \a index_num to |
| 1926 | * \c object->data.cue_sheet.tracks[track_num].num_indices . |
| 1927 | * \assert |
| 1928 | * \code object != NULL \endcode |
| 1929 | * \code object->type == FLAC__METADATA_TYPE_CUESHEET \endcode |
| 1930 | * \code object->data.cue_sheet.num_tracks > track_num \endcode |
| 1931 | * \code object->data.cue_sheet.tracks[track_num].num_indices >= index_num \endcode |
| 1932 | * \retval FLAC__bool |
| 1933 | * \c false if realloc() fails, else \c true. |
| 1934 | */ |
| 1935 | FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_insert_blank_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num); |
| 1936 | |
| 1937 | /** Delete an index point in a CUESHEET track at the given index. |
| 1938 | * |
| 1939 | * \param object A pointer to an existing CUESHEET object. |
| 1940 | * \param track_num The index into the track array of the track to |
| 1941 | * modify. NOTE: this is not necessarily the same |
| 1942 | * as the track's \a number field. |
| 1943 | * \param index_num The index into the track's index array of the index |
| 1944 | * to delete. NOTE: this is not necessarily the same |
| 1945 | * as the index's \a number field. |
| 1946 | * \assert |
| 1947 | * \code object != NULL \endcode |
| 1948 | * \code object->type == FLAC__METADATA_TYPE_CUESHEET \endcode |
| 1949 | * \code object->data.cue_sheet.num_tracks > track_num \endcode |
| 1950 | * \code object->data.cue_sheet.tracks[track_num].num_indices > index_num \endcode |
| 1951 | * \retval FLAC__bool |
| 1952 | * \c false if realloc() fails, else \c true. |
| 1953 | */ |
| 1954 | FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_delete_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num); |
| 1955 | |
| 1956 | /** Resize the track array. |
| 1957 | * |
| 1958 | * If the size shrinks, elements will truncated; if it grows, new blank |
| 1959 | * tracks will be added to the end. |
| 1960 | * |
| 1961 | * \param object A pointer to an existing CUESHEET object. |
| 1962 | * \param new_num_tracks The desired length of the array; may be \c 0. |
| 1963 | * \assert |
| 1964 | * \code object != NULL \endcode |
| 1965 | * \code object->type == FLAC__METADATA_TYPE_CUESHEET \endcode |
| 1966 | * \code (object->data.cue_sheet.tracks == NULL && object->data.cue_sheet.num_tracks == 0) || |
| 1967 | * (object->data.cue_sheet.tracks != NULL && object->data.cue_sheet.num_tracks > 0) \endcode |
| 1968 | * \retval FLAC__bool |
| 1969 | * \c false if memory allocation error, else \c true. |
| 1970 | */ |
| 1971 | FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_resize_tracks(FLAC__StreamMetadata *object, unsigned new_num_tracks); |
| 1972 | |
| 1973 | /** Sets a track in a CUESHEET block. |
| 1974 | * |
| 1975 | * If \a copy is \c true, a copy of the track is stored; otherwise, the object |
| 1976 | * takes ownership of the \a track pointer. |
| 1977 | * |
| 1978 | * \param object A pointer to an existing CUESHEET object. |
| 1979 | * \param track_num Index into track array to set. NOTE: this is not |
| 1980 | * necessarily the same as the track's \a number field. |
| 1981 | * \param track The track to set the track to. You may safely pass in |
| 1982 | * a const pointer if \a copy is \c true. |
| 1983 | * \param copy See above. |
| 1984 | * \assert |
| 1985 | * \code object != NULL \endcode |
| 1986 | * \code object->type == FLAC__METADATA_TYPE_CUESHEET \endcode |
| 1987 | * \code track_num < object->data.cue_sheet.num_tracks \endcode |
| 1988 | * \code (track->indices != NULL && track->num_indices > 0) || |
| 1989 | * (track->indices == NULL && track->num_indices == 0) |
| 1990 | * \retval FLAC__bool |
| 1991 | * \c false if \a copy is \c true and malloc() fails, else \c true. |
| 1992 | */ |
| 1993 | FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_set_track(FLAC__StreamMetadata *object, unsigned track_num, FLAC__StreamMetadata_CueSheet_Track *track, FLAC__bool copy); |
| 1994 | |
| 1995 | /** Insert a track in a CUESHEET block at the given index. |
| 1996 | * |
| 1997 | * If \a copy is \c true, a copy of the track is stored; otherwise, the object |
| 1998 | * takes ownership of the \a track pointer. |
| 1999 | * |
| 2000 | * \param object A pointer to an existing CUESHEET object. |
| 2001 | * \param track_num The index at which to insert the track. NOTE: this |
| 2002 | * is not necessarily the same as the track's \a number |
| 2003 | * field. The tracks at and after \a track_num move right |
| 2004 | * one position. To append a track to the end, set |
| 2005 | * \a track_num to \c object->data.cue_sheet.num_tracks . |
| 2006 | * \param track The track to insert. You may safely pass in a const |
| 2007 | * pointer if \a copy is \c true. |
| 2008 | * \param copy See above. |
| 2009 | * \assert |
| 2010 | * \code object != NULL \endcode |
| 2011 | * \code object->type == FLAC__METADATA_TYPE_CUESHEET \endcode |
| 2012 | * \code object->data.cue_sheet.num_tracks >= track_num \endcode |
| 2013 | * \retval FLAC__bool |
| 2014 | * \c false if \a copy is \c true and malloc() fails, else \c true. |
| 2015 | */ |
| 2016 | FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_insert_track(FLAC__StreamMetadata *object, unsigned track_num, FLAC__StreamMetadata_CueSheet_Track *track, FLAC__bool copy); |
| 2017 | |
| 2018 | /** Insert a blank track in a CUESHEET block at the given index. |
| 2019 | * |
| 2020 | * A blank track is one in which all field values are zero. |
| 2021 | * |
| 2022 | * \param object A pointer to an existing CUESHEET object. |
| 2023 | * \param track_num The index at which to insert the track. NOTE: this |
| 2024 | * is not necessarily the same as the track's \a number |
| 2025 | * field. The tracks at and after \a track_num move right |
| 2026 | * one position. To append a track to the end, set |
| 2027 | * \a track_num to \c object->data.cue_sheet.num_tracks . |
| 2028 | * \assert |
| 2029 | * \code object != NULL \endcode |
| 2030 | * \code object->type == FLAC__METADATA_TYPE_CUESHEET \endcode |
| 2031 | * \code object->data.cue_sheet.num_tracks >= track_num \endcode |
| 2032 | * \retval FLAC__bool |
| 2033 | * \c false if \a copy is \c true and malloc() fails, else \c true. |
| 2034 | */ |
| 2035 | FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_insert_blank_track(FLAC__StreamMetadata *object, unsigned track_num); |
| 2036 | |
| 2037 | /** Delete a track in a CUESHEET block at the given index. |
| 2038 | * |
| 2039 | * \param object A pointer to an existing CUESHEET object. |
| 2040 | * \param track_num The index into the track array of the track to |
| 2041 | * delete. NOTE: this is not necessarily the same |
| 2042 | * as the track's \a number field. |
| 2043 | * \assert |
| 2044 | * \code object != NULL \endcode |
| 2045 | * \code object->type == FLAC__METADATA_TYPE_CUESHEET \endcode |
| 2046 | * \code object->data.cue_sheet.num_tracks > track_num \endcode |
| 2047 | * \retval FLAC__bool |
| 2048 | * \c false if realloc() fails, else \c true. |
| 2049 | */ |
| 2050 | FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_delete_track(FLAC__StreamMetadata *object, unsigned track_num); |
| 2051 | |
| 2052 | /** Check a cue sheet to see if it conforms to the FLAC specification. |
| 2053 | * See the format specification for limits on the contents of the |
| 2054 | * cue sheet. |
| 2055 | * |
| 2056 | * \param object A pointer to an existing CUESHEET object. |
| 2057 | * \param check_cd_da_subset If \c true, check CUESHEET against more |
| 2058 | * stringent requirements for a CD-DA (audio) disc. |
| 2059 | * \param violation Address of a pointer to a string. If there is a |
| 2060 | * violation, a pointer to a string explanation of the |
| 2061 | * violation will be returned here. \a violation may be |
| 2062 | * \c NULL if you don't need the returned string. Do not |
| 2063 | * free the returned string; it will always point to static |
| 2064 | * data. |
| 2065 | * \assert |
| 2066 | * \code object != NULL \endcode |
| 2067 | * \code object->type == FLAC__METADATA_TYPE_CUESHEET \endcode |
| 2068 | * \retval FLAC__bool |
| 2069 | * \c false if cue sheet is illegal, else \c true. |
| 2070 | */ |
| 2071 | FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_is_legal(const FLAC__StreamMetadata *object, FLAC__bool check_cd_da_subset, const char **violation); |
| 2072 | |
| 2073 | /** Calculate and return the CDDB/freedb ID for a cue sheet. The function |
| 2074 | * assumes the cue sheet corresponds to a CD; the result is undefined |
| 2075 | * if the cuesheet's is_cd bit is not set. |
| 2076 | * |
| 2077 | * \param object A pointer to an existing CUESHEET object. |
| 2078 | * \assert |
| 2079 | * \code object != NULL \endcode |
| 2080 | * \code object->type == FLAC__METADATA_TYPE_CUESHEET \endcode |
| 2081 | * \retval FLAC__uint32 |
| 2082 | * The unsigned integer representation of the CDDB/freedb ID |
| 2083 | */ |
| 2084 | FLAC_API FLAC__uint32 FLAC__metadata_object_cuesheet_calculate_cddb_id(const FLAC__StreamMetadata *object); |
| 2085 | |
| 2086 | /** Sets the MIME type of a PICTURE block. |
| 2087 | * |
| 2088 | * If \a copy is \c true, a copy of the string is stored; otherwise, the object |
| 2089 | * takes ownership of the pointer. The existing string will be freed if this |
| 2090 | * function is successful, otherwise the original string will remain if \a copy |
| 2091 | * is \c true and malloc() fails. |
| 2092 | * |
| 2093 | * \note It is safe to pass a const pointer to \a mime_type if \a copy is \c true. |
| 2094 | * |
| 2095 | * \param object A pointer to an existing PICTURE object. |
| 2096 | * \param mime_type A pointer to the MIME type string. The string must be |
| 2097 | * ASCII characters 0x20-0x7e, NUL-terminated. No validation |
| 2098 | * is done. |
| 2099 | * \param copy See above. |
| 2100 | * \assert |
| 2101 | * \code object != NULL \endcode |
| 2102 | * \code object->type == FLAC__METADATA_TYPE_PICTURE \endcode |
| 2103 | * \code (mime_type != NULL) \endcode |
| 2104 | * \retval FLAC__bool |
| 2105 | * \c false if \a copy is \c true and malloc() fails, else \c true. |
| 2106 | */ |
| 2107 | FLAC_API FLAC__bool FLAC__metadata_object_picture_set_mime_type(FLAC__StreamMetadata *object, char *mime_type, FLAC__bool copy); |
| 2108 | |
| 2109 | /** Sets the description of a PICTURE block. |
| 2110 | * |
| 2111 | * If \a copy is \c true, a copy of the string is stored; otherwise, the object |
| 2112 | * takes ownership of the pointer. The existing string will be freed if this |
| 2113 | * function is successful, otherwise the original string will remain if \a copy |
| 2114 | * is \c true and malloc() fails. |
| 2115 | * |
| 2116 | * \note It is safe to pass a const pointer to \a description if \a copy is \c true. |
| 2117 | * |
| 2118 | * \param object A pointer to an existing PICTURE object. |
| 2119 | * \param description A pointer to the description string. The string must be |
| 2120 | * valid UTF-8, NUL-terminated. No validation is done. |
| 2121 | * \param copy See above. |
| 2122 | * \assert |
| 2123 | * \code object != NULL \endcode |
| 2124 | * \code object->type == FLAC__METADATA_TYPE_PICTURE \endcode |
| 2125 | * \code (description != NULL) \endcode |
| 2126 | * \retval FLAC__bool |
| 2127 | * \c false if \a copy is \c true and malloc() fails, else \c true. |
| 2128 | */ |
| 2129 | FLAC_API FLAC__bool FLAC__metadata_object_picture_set_description(FLAC__StreamMetadata *object, FLAC__byte *description, FLAC__bool copy); |
| 2130 | |
| 2131 | /** Sets the picture data of a PICTURE block. |
| 2132 | * |
| 2133 | * If \a copy is \c true, a copy of the data is stored; otherwise, the object |
| 2134 | * takes ownership of the pointer. Also sets the \a data_length field of the |
| 2135 | * metadata object to what is passed in as the \a length parameter. The |
| 2136 | * existing data will be freed if this function is successful, otherwise the |
| 2137 | * original data and data_length will remain if \a copy is \c true and |
| 2138 | * malloc() fails. |
| 2139 | * |
| 2140 | * \note It is safe to pass a const pointer to \a data if \a copy is \c true. |
| 2141 | * |
| 2142 | * \param object A pointer to an existing PICTURE object. |
| 2143 | * \param data A pointer to the data to set. |
| 2144 | * \param length The length of \a data in bytes. |
| 2145 | * \param copy See above. |
| 2146 | * \assert |
| 2147 | * \code object != NULL \endcode |
| 2148 | * \code object->type == FLAC__METADATA_TYPE_PICTURE \endcode |
| 2149 | * \code (data != NULL && length > 0) || |
| 2150 | * (data == NULL && length == 0 && copy == false) \endcode |
| 2151 | * \retval FLAC__bool |
| 2152 | * \c false if \a copy is \c true and malloc() fails, else \c true. |
| 2153 | */ |
| 2154 | FLAC_API FLAC__bool FLAC__metadata_object_picture_set_data(FLAC__StreamMetadata *object, FLAC__byte *data, FLAC__uint32 length, FLAC__bool copy); |
| 2155 | |
| 2156 | /** Check a PICTURE block to see if it conforms to the FLAC specification. |
| 2157 | * See the format specification for limits on the contents of the |
| 2158 | * PICTURE block. |
| 2159 | * |
| 2160 | * \param object A pointer to existing PICTURE block to be checked. |
| 2161 | * \param violation Address of a pointer to a string. If there is a |
| 2162 | * violation, a pointer to a string explanation of the |
| 2163 | * violation will be returned here. \a violation may be |
| 2164 | * \c NULL if you don't need the returned string. Do not |
| 2165 | * free the returned string; it will always point to static |
| 2166 | * data. |
| 2167 | * \assert |
| 2168 | * \code object != NULL \endcode |
| 2169 | * \code object->type == FLAC__METADATA_TYPE_PICTURE \endcode |
| 2170 | * \retval FLAC__bool |
| 2171 | * \c false if PICTURE block is illegal, else \c true. |
| 2172 | */ |
| 2173 | FLAC_API FLAC__bool FLAC__metadata_object_picture_is_legal(const FLAC__StreamMetadata *object, const char **violation); |
| 2174 | |
| 2175 | /* \} */ |
| 2176 | |
| 2177 | #ifdef __cplusplus |
| 2178 | } |
| 2179 | #endif |
| 2180 | |
| 2181 | #endif |
src/lib/libflac/include/flac/stream_decoder.h
| r0 | r14135 | |
| 1 | /* libFLAC - Free Lossless Audio Codec library |
| 2 | * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * |
| 8 | * - Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * - Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * - Neither the name of the Xiph.org Foundation nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived from |
| 17 | * this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 26 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 27 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #ifndef FLAC__STREAM_DECODER_H |
| 33 | #define FLAC__STREAM_DECODER_H |
| 34 | |
| 35 | #include <stdio.h> /* for FILE */ |
| 36 | #include "export.h" |
| 37 | #include "format.h" |
| 38 | |
| 39 | #ifdef __cplusplus |
| 40 | extern "C" { |
| 41 | #endif |
| 42 | |
| 43 | |
| 44 | /** \file include/FLAC/stream_decoder.h |
| 45 | * |
| 46 | * \brief |
| 47 | * This module contains the functions which implement the stream |
| 48 | * decoder. |
| 49 | * |
| 50 | * See the detailed documentation in the |
| 51 | * \link flac_stream_decoder stream decoder \endlink module. |
| 52 | */ |
| 53 | |
| 54 | /** \defgroup flac_decoder FLAC/ \*_decoder.h: decoder interfaces |
| 55 | * \ingroup flac |
| 56 | * |
| 57 | * \brief |
| 58 | * This module describes the decoder layers provided by libFLAC. |
| 59 | * |
| 60 | * The stream decoder can be used to decode complete streams either from |
| 61 | * the client via callbacks, or directly from a file, depending on how |
| 62 | * it is initialized. When decoding via callbacks, the client provides |
| 63 | * callbacks for reading FLAC data and writing decoded samples, and |
| 64 | * handling metadata and errors. If the client also supplies seek-related |
| 65 | * callback, the decoder function for sample-accurate seeking within the |
| 66 | * FLAC input is also available. When decoding from a file, the client |
| 67 | * needs only supply a filename or open \c FILE* and write/metadata/error |
| 68 | * callbacks; the rest of the callbacks are supplied internally. For more |
| 69 | * info see the \link flac_stream_decoder stream decoder \endlink module. |
| 70 | */ |
| 71 | |
| 72 | /** \defgroup flac_stream_decoder FLAC/stream_decoder.h: stream decoder interface |
| 73 | * \ingroup flac_decoder |
| 74 | * |
| 75 | * \brief |
| 76 | * This module contains the functions which implement the stream |
| 77 | * decoder. |
| 78 | * |
| 79 | * The stream decoder can decode native FLAC, and optionally Ogg FLAC |
| 80 | * (check FLAC_API_SUPPORTS_OGG_FLAC) streams and files. |
| 81 | * |
| 82 | * The basic usage of this decoder is as follows: |
| 83 | * - The program creates an instance of a decoder using |
| 84 | * FLAC__stream_decoder_new(). |
| 85 | * - The program overrides the default settings using |
| 86 | * FLAC__stream_decoder_set_*() functions. |
| 87 | * - The program initializes the instance to validate the settings and |
| 88 | * prepare for decoding using |
| 89 | * - FLAC__stream_decoder_init_stream() or FLAC__stream_decoder_init_FILE() |
| 90 | * or FLAC__stream_decoder_init_file() for native FLAC, |
| 91 | * - FLAC__stream_decoder_init_ogg_stream() or FLAC__stream_decoder_init_ogg_FILE() |
| 92 | * or FLAC__stream_decoder_init_ogg_file() for Ogg FLAC |
| 93 | * - The program calls the FLAC__stream_decoder_process_*() functions |
| 94 | * to decode data, which subsequently calls the callbacks. |
| 95 | * - The program finishes the decoding with FLAC__stream_decoder_finish(), |
| 96 | * which flushes the input and output and resets the decoder to the |
| 97 | * uninitialized state. |
| 98 | * - The instance may be used again or deleted with |
| 99 | * FLAC__stream_decoder_delete(). |
| 100 | * |
| 101 | * In more detail, the program will create a new instance by calling |
| 102 | * FLAC__stream_decoder_new(), then call FLAC__stream_decoder_set_*() |
| 103 | * functions to override the default decoder options, and call |
| 104 | * one of the FLAC__stream_decoder_init_*() functions. |
| 105 | * |
| 106 | * There are three initialization functions for native FLAC, one for |
| 107 | * setting up the decoder to decode FLAC data from the client via |
| 108 | * callbacks, and two for decoding directly from a FLAC file. |
| 109 | * |
| 110 | * For decoding via callbacks, use FLAC__stream_decoder_init_stream(). |
| 111 | * You must also supply several callbacks for handling I/O. Some (like |
| 112 | * seeking) are optional, depending on the capabilities of the input. |
| 113 | * |
| 114 | * For decoding directly from a file, use FLAC__stream_decoder_init_FILE() |
| 115 | * or FLAC__stream_decoder_init_file(). Then you must only supply an open |
| 116 | * \c FILE* or filename and fewer callbacks; the decoder will handle |
| 117 | * the other callbacks internally. |
| 118 | * |
| 119 | * There are three similarly-named init functions for decoding from Ogg |
| 120 | * FLAC streams. Check \c FLAC_API_SUPPORTS_OGG_FLAC to find out if the |
| 121 | * library has been built with Ogg support. |
| 122 | * |
| 123 | * Once the decoder is initialized, your program will call one of several |
| 124 | * functions to start the decoding process: |
| 125 | * |
| 126 | * - FLAC__stream_decoder_process_single() - Tells the decoder to process at |
| 127 | * most one metadata block or audio frame and return, calling either the |
| 128 | * metadata callback or write callback, respectively, once. If the decoder |
| 129 | * loses sync it will return with only the error callback being called. |
| 130 | * - FLAC__stream_decoder_process_until_end_of_metadata() - Tells the decoder |
| 131 | * to process the stream from the current location and stop upon reaching |
| 132 | * the first audio frame. The client will get one metadata, write, or error |
| 133 | * callback per metadata block, audio frame, or sync error, respectively. |
| 134 | * - FLAC__stream_decoder_process_until_end_of_stream() - Tells the decoder |
| 135 | * to process the stream from the current location until the read callback |
| 136 | * returns FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM or |
| 137 | * FLAC__STREAM_DECODER_READ_STATUS_ABORT. The client will get one metadata, |
| 138 | * write, or error callback per metadata block, audio frame, or sync error, |
| 139 | * respectively. |
| 140 | * |
| 141 | * When the decoder has finished decoding (normally or through an abort), |
| 142 | * the instance is finished by calling FLAC__stream_decoder_finish(), which |
| 143 | * ensures the decoder is in the correct state and frees memory. Then the |
| 144 | * instance may be deleted with FLAC__stream_decoder_delete() or initialized |
| 145 | * again to decode another stream. |
| 146 | * |
| 147 | * Seeking is exposed through the FLAC__stream_decoder_seek_absolute() method. |
| 148 | * At any point after the stream decoder has been initialized, the client can |
| 149 | * call this function to seek to an exact sample within the stream. |
| 150 | * Subsequently, the first time the write callback is called it will be |
| 151 | * passed a (possibly partial) block starting at that sample. |
| 152 | * |
| 153 | * If the client cannot seek via the callback interface provided, but still |
| 154 | * has another way of seeking, it can flush the decoder using |
| 155 | * FLAC__stream_decoder_flush() and start feeding data from the new position |
| 156 | * through the read callback. |
| 157 | * |
| 158 | * The stream decoder also provides MD5 signature checking. If this is |
| 159 | * turned on before initialization, FLAC__stream_decoder_finish() will |
| 160 | * report when the decoded MD5 signature does not match the one stored |
| 161 | * in the STREAMINFO block. MD5 checking is automatically turned off |
| 162 | * (until the next FLAC__stream_decoder_reset()) if there is no signature |
| 163 | * in the STREAMINFO block or when a seek is attempted. |
| 164 | * |
| 165 | * The FLAC__stream_decoder_set_metadata_*() functions deserve special |
| 166 | * attention. By default, the decoder only calls the metadata_callback for |
| 167 | * the STREAMINFO block. These functions allow you to tell the decoder |
| 168 | * explicitly which blocks to parse and return via the metadata_callback |
| 169 | * and/or which to skip. Use a FLAC__stream_decoder_set_metadata_respond_all(), |
| 170 | * FLAC__stream_decoder_set_metadata_ignore() ... or FLAC__stream_decoder_set_metadata_ignore_all(), |
| 171 | * FLAC__stream_decoder_set_metadata_respond() ... sequence to exactly specify |
| 172 | * which blocks to return. Remember that metadata blocks can potentially |
| 173 | * be big (for example, cover art) so filtering out the ones you don't |
| 174 | * use can reduce the memory requirements of the decoder. Also note the |
| 175 | * special forms FLAC__stream_decoder_set_metadata_respond_application(id) |
| 176 | * and FLAC__stream_decoder_set_metadata_ignore_application(id) for |
| 177 | * filtering APPLICATION blocks based on the application ID. |
| 178 | * |
| 179 | * STREAMINFO and SEEKTABLE blocks are always parsed and used internally, but |
| 180 | * they still can legally be filtered from the metadata_callback. |
| 181 | * |
| 182 | * \note |
| 183 | * The "set" functions may only be called when the decoder is in the |
| 184 | * state FLAC__STREAM_DECODER_UNINITIALIZED, i.e. after |
| 185 | * FLAC__stream_decoder_new() or FLAC__stream_decoder_finish(), but |
| 186 | * before FLAC__stream_decoder_init_*(). If this is the case they will |
| 187 | * return \c true, otherwise \c false. |
| 188 | * |
| 189 | * \note |
| 190 | * FLAC__stream_decoder_finish() resets all settings to the constructor |
| 191 | * defaults, including the callbacks. |
| 192 | * |
| 193 | * \{ |
| 194 | */ |
| 195 | |
| 196 | |
| 197 | /** State values for a FLAC__StreamDecoder |
| 198 | * |
| 199 | * The decoder's state can be obtained by calling FLAC__stream_decoder_get_state(). |
| 200 | */ |
| 201 | typedef enum { |
| 202 | |
| 203 | FLAC__STREAM_DECODER_SEARCH_FOR_METADATA = 0, |
| 204 | /**< The decoder is ready to search for metadata. */ |
| 205 | |
| 206 | FLAC__STREAM_DECODER_READ_METADATA, |
| 207 | /**< The decoder is ready to or is in the process of reading metadata. */ |
| 208 | |
| 209 | FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC, |
| 210 | /**< The decoder is ready to or is in the process of searching for the |
| 211 | * frame sync code. |
| 212 | */ |
| 213 | |
| 214 | FLAC__STREAM_DECODER_READ_FRAME, |
| 215 | /**< The decoder is ready to or is in the process of reading a frame. */ |
| 216 | |
| 217 | FLAC__STREAM_DECODER_END_OF_STREAM, |
| 218 | /**< The decoder has reached the end of the stream. */ |
| 219 | |
| 220 | FLAC__STREAM_DECODER_OGG_ERROR, |
| 221 | /**< An error occurred in the underlying Ogg layer. */ |
| 222 | |
| 223 | FLAC__STREAM_DECODER_SEEK_ERROR, |
| 224 | /**< An error occurred while seeking. The decoder must be flushed |
| 225 | * with FLAC__stream_decoder_flush() or reset with |
| 226 | * FLAC__stream_decoder_reset() before decoding can continue. |
| 227 | */ |
| 228 | |
| 229 | FLAC__STREAM_DECODER_ABORTED, |
| 230 | /**< The decoder was aborted by the read callback. */ |
| 231 | |
| 232 | FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR, |
| 233 | /**< An error occurred allocating memory. The decoder is in an invalid |
| 234 | * state and can no longer be used. |
| 235 | */ |
| 236 | |
| 237 | FLAC__STREAM_DECODER_UNINITIALIZED |
| 238 | /**< The decoder is in the uninitialized state; one of the |
| 239 | * FLAC__stream_decoder_init_*() functions must be called before samples |
| 240 | * can be processed. |
| 241 | */ |
| 242 | |
| 243 | } FLAC__StreamDecoderState; |
| 244 | |
| 245 | /** Maps a FLAC__StreamDecoderState to a C string. |
| 246 | * |
| 247 | * Using a FLAC__StreamDecoderState as the index to this array |
| 248 | * will give the string equivalent. The contents should not be modified. |
| 249 | */ |
| 250 | extern FLAC_API const char * const FLAC__StreamDecoderStateString[]; |
| 251 | |
| 252 | |
| 253 | /** Possible return values for the FLAC__stream_decoder_init_*() functions. |
| 254 | */ |
| 255 | typedef enum { |
| 256 | |
| 257 | FLAC__STREAM_DECODER_INIT_STATUS_OK = 0, |
| 258 | /**< Initialization was successful. */ |
| 259 | |
| 260 | FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER, |
| 261 | /**< The library was not compiled with support for the given container |
| 262 | * format. |
| 263 | */ |
| 264 | |
| 265 | FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS, |
| 266 | /**< A required callback was not supplied. */ |
| 267 | |
| 268 | FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR, |
| 269 | /**< An error occurred allocating memory. */ |
| 270 | |
| 271 | FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE, |
| 272 | /**< fopen() failed in FLAC__stream_decoder_init_file() or |
| 273 | * FLAC__stream_decoder_init_ogg_file(). */ |
| 274 | |
| 275 | FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED |
| 276 | /**< FLAC__stream_decoder_init_*() was called when the decoder was |
| 277 | * already initialized, usually because |
| 278 | * FLAC__stream_decoder_finish() was not called. |
| 279 | */ |
| 280 | |
| 281 | } FLAC__StreamDecoderInitStatus; |
| 282 | |
| 283 | /** Maps a FLAC__StreamDecoderInitStatus to a C string. |
| 284 | * |
| 285 | * Using a FLAC__StreamDecoderInitStatus as the index to this array |
| 286 | * will give the string equivalent. The contents should not be modified. |
| 287 | */ |
| 288 | extern FLAC_API const char * const FLAC__StreamDecoderInitStatusString[]; |
| 289 | |
| 290 | |
| 291 | /** Return values for the FLAC__StreamDecoder read callback. |
| 292 | */ |
| 293 | typedef enum { |
| 294 | |
| 295 | FLAC__STREAM_DECODER_READ_STATUS_CONTINUE, |
| 296 | /**< The read was OK and decoding can continue. */ |
| 297 | |
| 298 | FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM, |
| 299 | /**< The read was attempted while at the end of the stream. Note that |
| 300 | * the client must only return this value when the read callback was |
| 301 | * called when already at the end of the stream. Otherwise, if the read |
| 302 | * itself moves to the end of the stream, the client should still return |
| 303 | * the data and \c FLAC__STREAM_DECODER_READ_STATUS_CONTINUE, and then on |
| 304 | * the next read callback it should return |
| 305 | * \c FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM with a byte count |
| 306 | * of \c 0. |
| 307 | */ |
| 308 | |
| 309 | FLAC__STREAM_DECODER_READ_STATUS_ABORT |
| 310 | /**< An unrecoverable error occurred. The decoder will return from the process call. */ |
| 311 | |
| 312 | } FLAC__StreamDecoderReadStatus; |
| 313 | |
| 314 | /** Maps a FLAC__StreamDecoderReadStatus to a C string. |
| 315 | * |
| 316 | * Using a FLAC__StreamDecoderReadStatus as the index to this array |
| 317 | * will give the string equivalent. The contents should not be modified. |
| 318 | */ |
| 319 | extern FLAC_API const char * const FLAC__StreamDecoderReadStatusString[]; |
| 320 | |
| 321 | |
| 322 | /** Return values for the FLAC__StreamDecoder seek callback. |
| 323 | */ |
| 324 | typedef enum { |
| 325 | |
| 326 | FLAC__STREAM_DECODER_SEEK_STATUS_OK, |
| 327 | /**< The seek was OK and decoding can continue. */ |
| 328 | |
| 329 | FLAC__STREAM_DECODER_SEEK_STATUS_ERROR, |
| 330 | /**< An unrecoverable error occurred. The decoder will return from the process call. */ |
| 331 | |
| 332 | FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED |
| 333 | /**< Client does not support seeking. */ |
| 334 | |
| 335 | } FLAC__StreamDecoderSeekStatus; |
| 336 | |
| 337 | /** Maps a FLAC__StreamDecoderSeekStatus to a C string. |
| 338 | * |
| 339 | * Using a FLAC__StreamDecoderSeekStatus as the index to this array |
| 340 | * will give the string equivalent. The contents should not be modified. |
| 341 | */ |
| 342 | extern FLAC_API const char * const FLAC__StreamDecoderSeekStatusString[]; |
| 343 | |
| 344 | |
| 345 | /** Return values for the FLAC__StreamDecoder tell callback. |
| 346 | */ |
| 347 | typedef enum { |
| 348 | |
| 349 | FLAC__STREAM_DECODER_TELL_STATUS_OK, |
| 350 | /**< The tell was OK and decoding can continue. */ |
| 351 | |
| 352 | FLAC__STREAM_DECODER_TELL_STATUS_ERROR, |
| 353 | /**< An unrecoverable error occurred. The decoder will return from the process call. */ |
| 354 | |
| 355 | FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED |
| 356 | /**< Client does not support telling the position. */ |
| 357 | |
| 358 | } FLAC__StreamDecoderTellStatus; |
| 359 | |
| 360 | /** Maps a FLAC__StreamDecoderTellStatus to a C string. |
| 361 | * |
| 362 | * Using a FLAC__StreamDecoderTellStatus as the index to this array |
| 363 | * will give the string equivalent. The contents should not be modified. |
| 364 | */ |
| 365 | extern FLAC_API const char * const FLAC__StreamDecoderTellStatusString[]; |
| 366 | |
| 367 | |
| 368 | /** Return values for the FLAC__StreamDecoder length callback. |
| 369 | */ |
| 370 | typedef enum { |
| 371 | |
| 372 | FLAC__STREAM_DECODER_LENGTH_STATUS_OK, |
| 373 | /**< The length call was OK and decoding can continue. */ |
| 374 | |
| 375 | FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR, |
| 376 | /**< An unrecoverable error occurred. The decoder will return from the process call. */ |
| 377 | |
| 378 | FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED |
| 379 | /**< Client does not support reporting the length. */ |
| 380 | |
| 381 | } FLAC__StreamDecoderLengthStatus; |
| 382 | |
| 383 | /** Maps a FLAC__StreamDecoderLengthStatus to a C string. |
| 384 | * |
| 385 | * Using a FLAC__StreamDecoderLengthStatus as the index to this array |
| 386 | * will give the string equivalent. The contents should not be modified. |
| 387 | */ |
| 388 | extern FLAC_API const char * const FLAC__StreamDecoderLengthStatusString[]; |
| 389 | |
| 390 | |
| 391 | /** Return values for the FLAC__StreamDecoder write callback. |
| 392 | */ |
| 393 | typedef enum { |
| 394 | |
| 395 | FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE, |
| 396 | /**< The write was OK and decoding can continue. */ |
| 397 | |
| 398 | FLAC__STREAM_DECODER_WRITE_STATUS_ABORT |
| 399 | /**< An unrecoverable error occurred. The decoder will return from the process call. */ |
| 400 | |
| 401 | } FLAC__StreamDecoderWriteStatus; |
| 402 | |
| 403 | /** Maps a FLAC__StreamDecoderWriteStatus to a C string. |
| 404 | * |
| 405 | * Using a FLAC__StreamDecoderWriteStatus as the index to this array |
| 406 | * will give the string equivalent. The contents should not be modified. |
| 407 | */ |
| 408 | extern FLAC_API const char * const FLAC__StreamDecoderWriteStatusString[]; |
| 409 | |
| 410 | |
| 411 | /** Possible values passed back to the FLAC__StreamDecoder error callback. |
| 412 | * \c FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC is the generic catch- |
| 413 | * all. The rest could be caused by bad sync (false synchronization on |
| 414 | * data that is not the start of a frame) or corrupted data. The error |
| 415 | * itself is the decoder's best guess at what happened assuming a correct |
| 416 | * sync. For example \c FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER |
| 417 | * could be caused by a correct sync on the start of a frame, but some |
| 418 | * data in the frame header was corrupted. Or it could be the result of |
| 419 | * syncing on a point the stream that looked like the starting of a frame |
| 420 | * but was not. \c FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM |
| 421 | * could be because the decoder encountered a valid frame made by a future |
| 422 | * version of the encoder which it cannot parse, or because of a false |
| 423 | * sync making it appear as though an encountered frame was generated by |
| 424 | * a future encoder. |
| 425 | */ |
| 426 | typedef enum { |
| 427 | |
| 428 | FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, |
| 429 | /**< An error in the stream caused the decoder to lose synchronization. */ |
| 430 | |
| 431 | FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, |
| 432 | /**< The decoder encountered a corrupted frame header. */ |
| 433 | |
| 434 | FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH, |
| 435 | /**< The frame's data did not match the CRC in the footer. */ |
| 436 | |
| 437 | FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM |
| 438 | /**< The decoder encountered reserved fields in use in the stream. */ |
| 439 | |
| 440 | } FLAC__StreamDecoderErrorStatus; |
| 441 | |
| 442 | /** Maps a FLAC__StreamDecoderErrorStatus to a C string. |
| 443 | * |
| 444 | * Using a FLAC__StreamDecoderErrorStatus as the index to this array |
| 445 | * will give the string equivalent. The contents should not be modified. |
| 446 | */ |
| 447 | extern FLAC_API const char * const FLAC__StreamDecoderErrorStatusString[]; |
| 448 | |
| 449 | |
| 450 | /*********************************************************************** |
| 451 | * |
| 452 | * class FLAC__StreamDecoder |
| 453 | * |
| 454 | ***********************************************************************/ |
| 455 | |
| 456 | struct FLAC__StreamDecoderProtected; |
| 457 | struct FLAC__StreamDecoderPrivate; |
| 458 | /** The opaque structure definition for the stream decoder type. |
| 459 | * See the \link flac_stream_decoder stream decoder module \endlink |
| 460 | * for a detailed description. |
| 461 | */ |
| 462 | typedef struct { |
| 463 | struct FLAC__StreamDecoderProtected *protected_; /* avoid the C++ keyword 'protected' */ |
| 464 | struct FLAC__StreamDecoderPrivate *private_; /* avoid the C++ keyword 'private' */ |
| 465 | } FLAC__StreamDecoder; |
| 466 | |
| 467 | /** Signature for the read callback. |
| 468 | * |
| 469 | * A function pointer matching this signature must be passed to |
| 470 | * FLAC__stream_decoder_init*_stream(). The supplied function will be |
| 471 | * called when the decoder needs more input data. The address of the |
| 472 | * buffer to be filled is supplied, along with the number of bytes the |
| 473 | * buffer can hold. The callback may choose to supply less data and |
| 474 | * modify the byte count but must be careful not to overflow the buffer. |
| 475 | * The callback then returns a status code chosen from |
| 476 | * FLAC__StreamDecoderReadStatus. |
| 477 | * |
| 478 | * Here is an example of a read callback for stdio streams: |
| 479 | * \code |
| 480 | * FLAC__StreamDecoderReadStatus read_cb(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data) |
| 481 | * { |
| 482 | * FILE *file = ((MyClientData*)client_data)->file; |
| 483 | * if(*bytes > 0) { |
| 484 | * *bytes = fread(buffer, sizeof(FLAC__byte), *bytes, file); |
| 485 | * if(ferror(file)) |
| 486 | * return FLAC__STREAM_DECODER_READ_STATUS_ABORT; |
| 487 | * else if(*bytes == 0) |
| 488 | * return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; |
| 489 | * else |
| 490 | * return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; |
| 491 | * } |
| 492 | * else |
| 493 | * return FLAC__STREAM_DECODER_READ_STATUS_ABORT; |
| 494 | * } |
| 495 | * \endcode |
| 496 | * |
| 497 | * \note In general, FLAC__StreamDecoder functions which change the |
| 498 | * state should not be called on the \a decoder while in the callback. |
| 499 | * |
| 500 | * \param decoder The decoder instance calling the callback. |
| 501 | * \param buffer A pointer to a location for the callee to store |
| 502 | * data to be decoded. |
| 503 | * \param bytes A pointer to the size of the buffer. On entry |
| 504 | * to the callback, it contains the maximum number |
| 505 | * of bytes that may be stored in \a buffer. The |
| 506 | * callee must set it to the actual number of bytes |
| 507 | * stored (0 in case of error or end-of-stream) before |
| 508 | * returning. |
| 509 | * \param client_data The callee's client data set through |
| 510 | * FLAC__stream_decoder_init_*(). |
| 511 | * \retval FLAC__StreamDecoderReadStatus |
| 512 | * The callee's return status. Note that the callback should return |
| 513 | * \c FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM if and only if |
| 514 | * zero bytes were read and there is no more data to be read. |
| 515 | */ |
| 516 | typedef FLAC__StreamDecoderReadStatus (*FLAC__StreamDecoderReadCallback)(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data); |
| 517 | |
| 518 | /** Signature for the seek callback. |
| 519 | * |
| 520 | * A function pointer matching this signature may be passed to |
| 521 | * FLAC__stream_decoder_init*_stream(). The supplied function will be |
| 522 | * called when the decoder needs to seek the input stream. The decoder |
| 523 | * will pass the absolute byte offset to seek to, 0 meaning the |
| 524 | * beginning of the stream. |
| 525 | * |
| 526 | * Here is an example of a seek callback for stdio streams: |
| 527 | * \code |
| 528 | * FLAC__StreamDecoderSeekStatus seek_cb(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data) |
| 529 | * { |
| 530 | * FILE *file = ((MyClientData*)client_data)->file; |
| 531 | * if(file == stdin) |
| 532 | * return FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED; |
| 533 | * else if(fseeko(file, (off_t)absolute_byte_offset, SEEK_SET) < 0) |
| 534 | * return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR; |
| 535 | * else |
| 536 | * return FLAC__STREAM_DECODER_SEEK_STATUS_OK; |
| 537 | * } |
| 538 | * \endcode |
| 539 | * |
| 540 | * \note In general, FLAC__StreamDecoder functions which change the |
| 541 | * state should not be called on the \a decoder while in the callback. |
| 542 | * |
| 543 | * \param decoder The decoder instance calling the callback. |
| 544 | * \param absolute_byte_offset The offset from the beginning of the stream |
| 545 | * to seek to. |
| 546 | * \param client_data The callee's client data set through |
| 547 | * FLAC__stream_decoder_init_*(). |
| 548 | * \retval FLAC__StreamDecoderSeekStatus |
| 549 | * The callee's return status. |
| 550 | */ |
| 551 | typedef FLAC__StreamDecoderSeekStatus (*FLAC__StreamDecoderSeekCallback)(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data); |
| 552 | |
| 553 | /** Signature for the tell callback. |
| 554 | * |
| 555 | * A function pointer matching this signature may be passed to |
| 556 | * FLAC__stream_decoder_init*_stream(). The supplied function will be |
| 557 | * called when the decoder wants to know the current position of the |
| 558 | * stream. The callback should return the byte offset from the |
| 559 | * beginning of the stream. |
| 560 | * |
| 561 | * Here is an example of a tell callback for stdio streams: |
| 562 | * \code |
| 563 | * FLAC__StreamDecoderTellStatus tell_cb(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data) |
| 564 | * { |
| 565 | * FILE *file = ((MyClientData*)client_data)->file; |
| 566 | * off_t pos; |
| 567 | * if(file == stdin) |
| 568 | * return FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED; |
| 569 | * else if((pos = ftello(file)) < 0) |
| 570 | * return FLAC__STREAM_DECODER_TELL_STATUS_ERROR; |
| 571 | * else { |
| 572 | * *absolute_byte_offset = (FLAC__uint64)pos; |
| 573 | * return FLAC__STREAM_DECODER_TELL_STATUS_OK; |
| 574 | * } |
| 575 | * } |
| 576 | * \endcode |
| 577 | * |
| 578 | * \note In general, FLAC__StreamDecoder functions which change the |
| 579 | * state should not be called on the \a decoder while in the callback. |
| 580 | * |
| 581 | * \param decoder The decoder instance calling the callback. |
| 582 | * \param absolute_byte_offset A pointer to storage for the current offset |
| 583 | * from the beginning of the stream. |
| 584 | * \param client_data The callee's client data set through |
| 585 | * FLAC__stream_decoder_init_*(). |
| 586 | * \retval FLAC__StreamDecoderTellStatus |
| 587 | * The callee's return status. |
| 588 | */ |
| 589 | typedef FLAC__StreamDecoderTellStatus (*FLAC__StreamDecoderTellCallback)(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data); |
| 590 | |
| 591 | /** Signature for the length callback. |
| 592 | * |
| 593 | * A function pointer matching this signature may be passed to |
| 594 | * FLAC__stream_decoder_init*_stream(). The supplied function will be |
| 595 | * called when the decoder wants to know the total length of the stream |
| 596 | * in bytes. |
| 597 | * |
| 598 | * Here is an example of a length callback for stdio streams: |
| 599 | * \code |
| 600 | * FLAC__StreamDecoderLengthStatus length_cb(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data) |
| 601 | * { |
| 602 | * FILE *file = ((MyClientData*)client_data)->file; |
| 603 | * struct stat filestats; |
| 604 | * |
| 605 | * if(file == stdin) |
| 606 | * return FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED; |
| 607 | * else if(fstat(fileno(file), &filestats) != 0) |
| 608 | * return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR; |
| 609 | * else { |
| 610 | * *stream_length = (FLAC__uint64)filestats.st_size; |
| 611 | * return FLAC__STREAM_DECODER_LENGTH_STATUS_OK; |
| 612 | * } |
| 613 | * } |
| 614 | * \endcode |
| 615 | * |
| 616 | * \note In general, FLAC__StreamDecoder functions which change the |
| 617 | * state should not be called on the \a decoder while in the callback. |
| 618 | * |
| 619 | * \param decoder The decoder instance calling the callback. |
| 620 | * \param stream_length A pointer to storage for the length of the stream |
| 621 | * in bytes. |
| 622 | * \param client_data The callee's client data set through |
| 623 | * FLAC__stream_decoder_init_*(). |
| 624 | * \retval FLAC__StreamDecoderLengthStatus |
| 625 | * The callee's return status. |
| 626 | */ |
| 627 | typedef FLAC__StreamDecoderLengthStatus (*FLAC__StreamDecoderLengthCallback)(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data); |
| 628 | |
| 629 | /** Signature for the EOF callback. |
| 630 | * |
| 631 | * A function pointer matching this signature may be passed to |
| 632 | * FLAC__stream_decoder_init*_stream(). The supplied function will be |
| 633 | * called when the decoder needs to know if the end of the stream has |
| 634 | * been reached. |
| 635 | * |
| 636 | * Here is an example of a EOF callback for stdio streams: |
| 637 | * FLAC__bool eof_cb(const FLAC__StreamDecoder *decoder, void *client_data) |
| 638 | * \code |
| 639 | * { |
| 640 | * FILE *file = ((MyClientData*)client_data)->file; |
| 641 | * return feof(file)? true : false; |
| 642 | * } |
| 643 | * \endcode |
| 644 | * |
| 645 | * \note In general, FLAC__StreamDecoder functions which change the |
| 646 | * state should not be called on the \a decoder while in the callback. |
| 647 | * |
| 648 | * \param decoder The decoder instance calling the callback. |
| 649 | * \param client_data The callee's client data set through |
| 650 | * FLAC__stream_decoder_init_*(). |
| 651 | * \retval FLAC__bool |
| 652 | * \c true if the currently at the end of the stream, else \c false. |
| 653 | */ |
| 654 | typedef FLAC__bool (*FLAC__StreamDecoderEofCallback)(const FLAC__StreamDecoder *decoder, void *client_data); |
| 655 | |
| 656 | /** Signature for the write callback. |
| 657 | * |
| 658 | * A function pointer matching this signature must be passed to one of |
| 659 | * the FLAC__stream_decoder_init_*() functions. |
| 660 | * The supplied function will be called when the decoder has decoded a |
| 661 | * single audio frame. The decoder will pass the frame metadata as well |
| 662 | * as an array of pointers (one for each channel) pointing to the |
| 663 | * decoded audio. |
| 664 | * |
| 665 | * \note In general, FLAC__StreamDecoder functions which change the |
| 666 | * state should not be called on the \a decoder while in the callback. |
| 667 | * |
| 668 | * \param decoder The decoder instance calling the callback. |
| 669 | * \param frame The description of the decoded frame. See |
| 670 | * FLAC__Frame. |
| 671 | * \param buffer An array of pointers to decoded channels of data. |
| 672 | * Each pointer will point to an array of signed |
| 673 | * samples of length \a frame->header.blocksize. |
| 674 | * Channels will be ordered according to the FLAC |
| 675 | * specification; see the documentation for the |
| 676 | * <A HREF="../format.html#frame_header">frame header</A>. |
| 677 | * \param client_data The callee's client data set through |
| 678 | * FLAC__stream_decoder_init_*(). |
| 679 | * \retval FLAC__StreamDecoderWriteStatus |
| 680 | * The callee's return status. |
| 681 | */ |
| 682 | typedef FLAC__StreamDecoderWriteStatus (*FLAC__StreamDecoderWriteCallback)(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data); |
| 683 | |
| 684 | /** Signature for the metadata callback. |
| 685 | * |
| 686 | * A function pointer matching this signature must be passed to one of |
| 687 | * the FLAC__stream_decoder_init_*() functions. |
| 688 | * The supplied function will be called when the decoder has decoded a |
| 689 | * metadata block. In a valid FLAC file there will always be one |
| 690 | * \c STREAMINFO block, followed by zero or more other metadata blocks. |
| 691 | * These will be supplied by the decoder in the same order as they |
| 692 | * appear in the stream and always before the first audio frame (i.e. |
| 693 | * write callback). The metadata block that is passed in must not be |
| 694 | * modified, and it doesn't live beyond the callback, so you should make |
| 695 | * a copy of it with FLAC__metadata_object_clone() if you will need it |
| 696 | * elsewhere. Since metadata blocks can potentially be large, by |
| 697 | * default the decoder only calls the metadata callback for the |
| 698 | * \c STREAMINFO block; you can instruct the decoder to pass or filter |
| 699 | * other blocks with FLAC__stream_decoder_set_metadata_*() calls. |
| 700 | * |
| 701 | * \note In general, FLAC__StreamDecoder functions which change the |
| 702 | * state should not be called on the \a decoder while in the callback. |
| 703 | * |
| 704 | * \param decoder The decoder instance calling the callback. |
| 705 | * \param metadata The decoded metadata block. |
| 706 | * \param client_data The callee's client data set through |
| 707 | * FLAC__stream_decoder_init_*(). |
| 708 | */ |
| 709 | typedef void (*FLAC__StreamDecoderMetadataCallback)(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data); |
| 710 | |
| 711 | /** Signature for the error callback. |
| 712 | * |
| 713 | * A function pointer matching this signature must be passed to one of |
| 714 | * the FLAC__stream_decoder_init_*() functions. |
| 715 | * The supplied function will be called whenever an error occurs during |
| 716 | * decoding. |
| 717 | * |
| 718 | * \note In general, FLAC__StreamDecoder functions which change the |
| 719 | * state should not be called on the \a decoder while in the callback. |
| 720 | * |
| 721 | * \param decoder The decoder instance calling the callback. |
| 722 | * \param status The error encountered by the decoder. |
| 723 | * \param client_data The callee's client data set through |
| 724 | * FLAC__stream_decoder_init_*(). |
| 725 | */ |
| 726 | typedef void (*FLAC__StreamDecoderErrorCallback)(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data); |
| 727 | |
| 728 | |
| 729 | /*********************************************************************** |
| 730 | * |
| 731 | * Class constructor/destructor |
| 732 | * |
| 733 | ***********************************************************************/ |
| 734 | |
| 735 | /** Create a new stream decoder instance. The instance is created with |
| 736 | * default settings; see the individual FLAC__stream_decoder_set_*() |
| 737 | * functions for each setting's default. |
| 738 | * |
| 739 | * \retval FLAC__StreamDecoder* |
| 740 | * \c NULL if there was an error allocating memory, else the new instance. |
| 741 | */ |
| 742 | FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new(void); |
| 743 | |
| 744 | /** Free a decoder instance. Deletes the object pointed to by \a decoder. |
| 745 | * |
| 746 | * \param decoder A pointer to an existing decoder. |
| 747 | * \assert |
| 748 | * \code decoder != NULL \endcode |
| 749 | */ |
| 750 | FLAC_API void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder); |
| 751 | |
| 752 | |
| 753 | /*********************************************************************** |
| 754 | * |
| 755 | * Public class method prototypes |
| 756 | * |
| 757 | ***********************************************************************/ |
| 758 | |
| 759 | /** Set the serial number for the FLAC stream within the Ogg container. |
| 760 | * The default behavior is to use the serial number of the first Ogg |
| 761 | * page. Setting a serial number here will explicitly specify which |
| 762 | * stream is to be decoded. |
| 763 | * |
| 764 | * \note |
| 765 | * This does not need to be set for native FLAC decoding. |
| 766 | * |
| 767 | * \default \c use serial number of first page |
| 768 | * \param decoder A decoder instance to set. |
| 769 | * \param serial_number See above. |
| 770 | * \assert |
| 771 | * \code decoder != NULL \endcode |
| 772 | * \retval FLAC__bool |
| 773 | * \c false if the decoder is already initialized, else \c true. |
| 774 | */ |
| 775 | FLAC_API FLAC__bool FLAC__stream_decoder_set_ogg_serial_number(FLAC__StreamDecoder *decoder, long serial_number); |
| 776 | |
| 777 | /** Set the "MD5 signature checking" flag. If \c true, the decoder will |
| 778 | * compute the MD5 signature of the unencoded audio data while decoding |
| 779 | * and compare it to the signature from the STREAMINFO block, if it |
| 780 | * exists, during FLAC__stream_decoder_finish(). |
| 781 | * |
| 782 | * MD5 signature checking will be turned off (until the next |
| 783 | * FLAC__stream_decoder_reset()) if there is no signature in the |
| 784 | * STREAMINFO block or when a seek is attempted. |
| 785 | * |
| 786 | * Clients that do not use the MD5 check should leave this off to speed |
| 787 | * up decoding. |
| 788 | * |
| 789 | * \default \c false |
| 790 | * \param decoder A decoder instance to set. |
| 791 | * \param value Flag value (see above). |
| 792 | * \assert |
| 793 | * \code decoder != NULL \endcode |
| 794 | * \retval FLAC__bool |
| 795 | * \c false if the decoder is already initialized, else \c true. |
| 796 | */ |
| 797 | FLAC_API FLAC__bool FLAC__stream_decoder_set_md5_checking(FLAC__StreamDecoder *decoder, FLAC__bool value); |
| 798 | |
| 799 | /** Direct the decoder to pass on all metadata blocks of type \a type. |
| 800 | * |
| 801 | * \default By default, only the \c STREAMINFO block is returned via the |
| 802 | * metadata callback. |
| 803 | * \param decoder A decoder instance to set. |
| 804 | * \param type See above. |
| 805 | * \assert |
| 806 | * \code decoder != NULL \endcode |
| 807 | * \a type is valid |
| 808 | * \retval FLAC__bool |
| 809 | * \c false if the decoder is already initialized, else \c true. |
| 810 | */ |
| 811 | FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type); |
| 812 | |
| 813 | /** Direct the decoder to pass on all APPLICATION metadata blocks of the |
| 814 | * given \a id. |
| 815 | * |
| 816 | * \default By default, only the \c STREAMINFO block is returned via the |
| 817 | * metadata callback. |
| 818 | * \param decoder A decoder instance to set. |
| 819 | * \param id See above. |
| 820 | * \assert |
| 821 | * \code decoder != NULL \endcode |
| 822 | * \code id != NULL \endcode |
| 823 | * \retval FLAC__bool |
| 824 | * \c false if the decoder is already initialized, else \c true. |
| 825 | */ |
| 826 | FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4]); |
| 827 | |
| 828 | /** Direct the decoder to pass on all metadata blocks of any type. |
| 829 | * |
| 830 | * \default By default, only the \c STREAMINFO block is returned via the |
| 831 | * metadata callback. |
| 832 | * \param decoder A decoder instance to set. |
| 833 | * \assert |
| 834 | * \code decoder != NULL \endcode |
| 835 | * \retval FLAC__bool |
| 836 | * \c false if the decoder is already initialized, else \c true. |
| 837 | */ |
| 838 | FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder); |
| 839 | |
| 840 | /** Direct the decoder to filter out all metadata blocks of type \a type. |
| 841 | * |
| 842 | * \default By default, only the \c STREAMINFO block is returned via the |
| 843 | * metadata callback. |
| 844 | * \param decoder A decoder instance to set. |
| 845 | * \param type See above. |
| 846 | * \assert |
| 847 | * \code decoder != NULL \endcode |
| 848 | * \a type is valid |
| 849 | * \retval FLAC__bool |
| 850 | * \c false if the decoder is already initialized, else \c true. |
| 851 | */ |
| 852 | FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type); |
| 853 | |
| 854 | /** Direct the decoder to filter out all APPLICATION metadata blocks of |
| 855 | * the given \a id. |
| 856 | * |
| 857 | * \default By default, only the \c STREAMINFO block is returned via the |
| 858 | * metadata callback. |
| 859 | * \param decoder A decoder instance to set. |
| 860 | * \param id See above. |
| 861 | * \assert |
| 862 | * \code decoder != NULL \endcode |
| 863 | * \code id != NULL \endcode |
| 864 | * \retval FLAC__bool |
| 865 | * \c false if the decoder is already initialized, else \c true. |
| 866 | */ |
| 867 | FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4]); |
| 868 | |
| 869 | /** Direct the decoder to filter out all metadata blocks of any type. |
| 870 | * |
| 871 | * \default By default, only the \c STREAMINFO block is returned via the |
| 872 | * metadata callback. |
| 873 | * \param decoder A decoder instance to set. |
| 874 | * \assert |
| 875 | * \code decoder != NULL \endcode |
| 876 | * \retval FLAC__bool |
| 877 | * \c false if the decoder is already initialized, else \c true. |
| 878 | */ |
| 879 | FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder); |
| 880 | |
| 881 | /** Get the current decoder state. |
| 882 | * |
| 883 | * \param decoder A decoder instance to query. |
| 884 | * \assert |
| 885 | * \code decoder != NULL \endcode |
| 886 | * \retval FLAC__StreamDecoderState |
| 887 | * The current decoder state. |
| 888 | */ |
| 889 | FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder); |
| 890 | |
| 891 | /** Get the current decoder state as a C string. |
| 892 | * |
| 893 | * \param decoder A decoder instance to query. |
| 894 | * \assert |
| 895 | * \code decoder != NULL \endcode |
| 896 | * \retval const char * |
| 897 | * The decoder state as a C string. Do not modify the contents. |
| 898 | */ |
| 899 | FLAC_API const char *FLAC__stream_decoder_get_resolved_state_string(const FLAC__StreamDecoder *decoder); |
| 900 | |
| 901 | /** Get the "MD5 signature checking" flag. |
| 902 | * This is the value of the setting, not whether or not the decoder is |
| 903 | * currently checking the MD5 (remember, it can be turned off automatically |
| 904 | * by a seek). When the decoder is reset the flag will be restored to the |
| 905 | * value returned by this function. |
| 906 | * |
| 907 | * \param decoder A decoder instance to query. |
| 908 | * \assert |
| 909 | * \code decoder != NULL \endcode |
| 910 | * \retval FLAC__bool |
| 911 | * See above. |
| 912 | */ |
| 913 | FLAC_API FLAC__bool FLAC__stream_decoder_get_md5_checking(const FLAC__StreamDecoder *decoder); |
| 914 | |
| 915 | /** Get the total number of samples in the stream being decoded. |
| 916 | * Will only be valid after decoding has started and will contain the |
| 917 | * value from the \c STREAMINFO block. A value of \c 0 means "unknown". |
| 918 | * |
| 919 | * \param decoder A decoder instance to query. |
| 920 | * \assert |
| 921 | * \code decoder != NULL \endcode |
| 922 | * \retval unsigned |
| 923 | * See above. |
| 924 | */ |
| 925 | FLAC_API FLAC__uint64 FLAC__stream_decoder_get_total_samples(const FLAC__StreamDecoder *decoder); |
| 926 | |
| 927 | /** Get the current number of channels in the stream being decoded. |
| 928 | * Will only be valid after decoding has started and will contain the |
| 929 | * value from the most recently decoded frame header. |
| 930 | * |
| 931 | * \param decoder A decoder instance to query. |
| 932 | * \assert |
| 933 | * \code decoder != NULL \endcode |
| 934 | * \retval unsigned |
| 935 | * See above. |
| 936 | */ |
| 937 | FLAC_API unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder); |
| 938 | |
| 939 | /** Get the current channel assignment in the stream being decoded. |
| 940 | * Will only be valid after decoding has started and will contain the |
| 941 | * value from the most recently decoded frame header. |
| 942 | * |
| 943 | * \param decoder A decoder instance to query. |
| 944 | * \assert |
| 945 | * \code decoder != NULL \endcode |
| 946 | * \retval FLAC__ChannelAssignment |
| 947 | * See above. |
| 948 | */ |
| 949 | FLAC_API FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder); |
| 950 | |
| 951 | /** Get the current sample resolution in the stream being decoded. |
| 952 | * Will only be valid after decoding has started and will contain the |
| 953 | * value from the most recently decoded frame header. |
| 954 | * |
| 955 | * \param decoder A decoder instance to query. |
| 956 | * \assert |
| 957 | * \code decoder != NULL \endcode |
| 958 | * \retval unsigned |
| 959 | * See above. |
| 960 | */ |
| 961 | FLAC_API unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder); |
| 962 | |
| 963 | /** Get the current sample rate in Hz of the stream being decoded. |
| 964 | * Will only be valid after decoding has started and will contain the |
| 965 | * value from the most recently decoded frame header. |
| 966 | * |
| 967 | * \param decoder A decoder instance to query. |
| 968 | * \assert |
| 969 | * \code decoder != NULL \endcode |
| 970 | * \retval unsigned |
| 971 | * See above. |
| 972 | */ |
| 973 | FLAC_API unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder); |
| 974 | |
| 975 | /** Get the current blocksize of the stream being decoded. |
| 976 | * Will only be valid after decoding has started and will contain the |
| 977 | * value from the most recently decoded frame header. |
| 978 | * |
| 979 | * \param decoder A decoder instance to query. |
| 980 | * \assert |
| 981 | * \code decoder != NULL \endcode |
| 982 | * \retval unsigned |
| 983 | * See above. |
| 984 | */ |
| 985 | FLAC_API unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder); |
| 986 | |
| 987 | /** Returns the decoder's current read position within the stream. |
| 988 | * The position is the byte offset from the start of the stream. |
| 989 | * Bytes before this position have been fully decoded. Note that |
| 990 | * there may still be undecoded bytes in the decoder's read FIFO. |
| 991 | * The returned position is correct even after a seek. |
| 992 | * |
| 993 | * \warning This function currently only works for native FLAC, |
| 994 | * not Ogg FLAC streams. |
| 995 | * |
| 996 | * \param decoder A decoder instance to query. |
| 997 | * \param position Address at which to return the desired position. |
| 998 | * \assert |
| 999 | * \code decoder != NULL \endcode |
| 1000 | * \code position != NULL \endcode |
| 1001 | * \retval FLAC__bool |
| 1002 | * \c true if successful, \c false if the stream is not native FLAC, |
| 1003 | * or there was an error from the 'tell' callback or it returned |
| 1004 | * \c FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED. |
| 1005 | */ |
| 1006 | FLAC_API FLAC__bool FLAC__stream_decoder_get_decode_position(const FLAC__StreamDecoder *decoder, FLAC__uint64 *position); |
| 1007 | |
| 1008 | /** Initialize the decoder instance to decode native FLAC streams. |
| 1009 | * |
| 1010 | * This flavor of initialization sets up the decoder to decode from a |
| 1011 | * native FLAC stream. I/O is performed via callbacks to the client. |
| 1012 | * For decoding from a plain file via filename or open FILE*, |
| 1013 | * FLAC__stream_decoder_init_file() and FLAC__stream_decoder_init_FILE() |
| 1014 | * provide a simpler interface. |
| 1015 | * |
| 1016 | * This function should be called after FLAC__stream_decoder_new() and |
| 1017 | * FLAC__stream_decoder_set_*() but before any of the |
| 1018 | * FLAC__stream_decoder_process_*() functions. Will set and return the |
| 1019 | * decoder state, which will be FLAC__STREAM_DECODER_SEARCH_FOR_METADATA |
| 1020 | * if initialization succeeded. |
| 1021 | * |
| 1022 | * \param decoder An uninitialized decoder instance. |
| 1023 | * \param read_callback See FLAC__StreamDecoderReadCallback. This |
| 1024 | * pointer must not be \c NULL. |
| 1025 | * \param seek_callback See FLAC__StreamDecoderSeekCallback. This |
| 1026 | * pointer may be \c NULL if seeking is not |
| 1027 | * supported. If \a seek_callback is not \c NULL then a |
| 1028 | * \a tell_callback, \a length_callback, and \a eof_callback must also be supplied. |
| 1029 | * Alternatively, a dummy seek callback that just |
| 1030 | * returns \c FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED |
| 1031 | * may also be supplied, all though this is slightly |
| 1032 | * less efficient for the decoder. |
| 1033 | * \param tell_callback See FLAC__StreamDecoderTellCallback. This |
| 1034 | * pointer may be \c NULL if not supported by the client. If |
| 1035 | * \a seek_callback is not \c NULL then a |
| 1036 | * \a tell_callback must also be supplied. |
| 1037 | * Alternatively, a dummy tell callback that just |
| 1038 | * returns \c FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED |
| 1039 | * may also be supplied, all though this is slightly |
| 1040 | * less efficient for the decoder. |
| 1041 | * \param length_callback See FLAC__StreamDecoderLengthCallback. This |
| 1042 | * pointer may be \c NULL if not supported by the client. If |
| 1043 | * \a seek_callback is not \c NULL then a |
| 1044 | * \a length_callback must also be supplied. |
| 1045 | * Alternatively, a dummy length callback that just |
| 1046 | * returns \c FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED |
| 1047 | * may also be supplied, all though this is slightly |
| 1048 | * less efficient for the decoder. |
| 1049 | * \param eof_callback See FLAC__StreamDecoderEofCallback. This |
| 1050 | * pointer may be \c NULL if not supported by the client. If |
| 1051 | * \a seek_callback is not \c NULL then a |
| 1052 | * \a eof_callback must also be supplied. |
| 1053 | * Alternatively, a dummy length callback that just |
| 1054 | * returns \c false |
| 1055 | * may also be supplied, all though this is slightly |
| 1056 | * less efficient for the decoder. |
| 1057 | * \param write_callback See FLAC__StreamDecoderWriteCallback. This |
| 1058 | * pointer must not be \c NULL. |
| 1059 | * \param metadata_callback See FLAC__StreamDecoderMetadataCallback. This |
| 1060 | * pointer may be \c NULL if the callback is not |
| 1061 | * desired. |
| 1062 | * \param error_callback See FLAC__StreamDecoderErrorCallback. This |
| 1063 | * pointer must not be \c NULL. |
| 1064 | * \param client_data This value will be supplied to callbacks in their |
| 1065 | * \a client_data argument. |
| 1066 | * \assert |
| 1067 | * \code decoder != NULL \endcode |
| 1068 | * \retval FLAC__StreamDecoderInitStatus |
| 1069 | * \c FLAC__STREAM_DECODER_INIT_STATUS_OK if initialization was successful; |
| 1070 | * see FLAC__StreamDecoderInitStatus for the meanings of other return values. |
| 1071 | */ |
| 1072 | FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_stream( |
| 1073 | FLAC__StreamDecoder *decoder, |
| 1074 | FLAC__StreamDecoderReadCallback read_callback, |
| 1075 | FLAC__StreamDecoderSeekCallback seek_callback, |
| 1076 | FLAC__StreamDecoderTellCallback tell_callback, |
| 1077 | FLAC__StreamDecoderLengthCallback length_callback, |
| 1078 | FLAC__StreamDecoderEofCallback eof_callback, |
| 1079 | FLAC__StreamDecoderWriteCallback write_callback, |
| 1080 | FLAC__StreamDecoderMetadataCallback metadata_callback, |
| 1081 | FLAC__StreamDecoderErrorCallback error_callback, |
| 1082 | void *client_data |
| 1083 | ); |
| 1084 | |
| 1085 | /** Initialize the decoder instance to decode Ogg FLAC streams. |
| 1086 | * |
| 1087 | * This flavor of initialization sets up the decoder to decode from a |
| 1088 | * FLAC stream in an Ogg container. I/O is performed via callbacks to the |
| 1089 | * client. For decoding from a plain file via filename or open FILE*, |
| 1090 | * FLAC__stream_decoder_init_ogg_file() and FLAC__stream_decoder_init_ogg_FILE() |
| 1091 | * provide a simpler interface. |
| 1092 | * |
| 1093 | * This function should be called after FLAC__stream_decoder_new() and |
| 1094 | * FLAC__stream_decoder_set_*() but before any of the |
| 1095 | * FLAC__stream_decoder_process_*() functions. Will set and return the |
| 1096 | * decoder state, which will be FLAC__STREAM_DECODER_SEARCH_FOR_METADATA |
| 1097 | * if initialization succeeded. |
| 1098 | * |
| 1099 | * \note Support for Ogg FLAC in the library is optional. If this |
| 1100 | * library has been built without support for Ogg FLAC, this function |
| 1101 | * will return \c FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER. |
| 1102 | * |
| 1103 | * \param decoder An uninitialized decoder instance. |
| 1104 | * \param read_callback See FLAC__StreamDecoderReadCallback. This |
| 1105 | * pointer must not be \c NULL. |
| 1106 | * \param seek_callback See FLAC__StreamDecoderSeekCallback. This |
| 1107 | * pointer may be \c NULL if seeking is not |
| 1108 | * supported. If \a seek_callback is not \c NULL then a |
| 1109 | * \a tell_callback, \a length_callback, and \a eof_callback must also be supplied. |
| 1110 | * Alternatively, a dummy seek callback that just |
| 1111 | * returns \c FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED |
| 1112 | * may also be supplied, all though this is slightly |
| 1113 | * less efficient for the decoder. |
| 1114 | * \param tell_callback See FLAC__StreamDecoderTellCallback. This |
| 1115 | * pointer may be \c NULL if not supported by the client. If |
| 1116 | * \a seek_callback is not \c NULL then a |
| 1117 | * \a tell_callback must also be supplied. |
| 1118 | * Alternatively, a dummy tell callback that just |
| 1119 | * returns \c FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED |
| 1120 | * may also be supplied, all though this is slightly |
| 1121 | * less efficient for the decoder. |
| 1122 | * \param length_callback See FLAC__StreamDecoderLengthCallback. This |
| 1123 | * pointer may be \c NULL if not supported by the client. If |
| 1124 | * \a seek_callback is not \c NULL then a |
| 1125 | * \a length_callback must also be supplied. |
| 1126 | * Alternatively, a dummy length callback that just |
| 1127 | * returns \c FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED |
| 1128 | * may also be supplied, all though this is slightly |
| 1129 | * less efficient for the decoder. |
| 1130 | * \param eof_callback See FLAC__StreamDecoderEofCallback. This |
| 1131 | * pointer may be \c NULL if not supported by the client. If |
| 1132 | * \a seek_callback is not \c NULL then a |
| 1133 | * \a eof_callback must also be supplied. |
| 1134 | * Alternatively, a dummy length callback that just |
| 1135 | * returns \c false |
| 1136 | * may also be supplied, all though this is slightly |
| 1137 | * less efficient for the decoder. |
| 1138 | * \param write_callback See FLAC__StreamDecoderWriteCallback. This |
| 1139 | * pointer must not be \c NULL. |
| 1140 | * \param metadata_callback See FLAC__StreamDecoderMetadataCallback. This |
| 1141 | * pointer may be \c NULL if the callback is not |
| 1142 | * desired. |
| 1143 | * \param error_callback See FLAC__StreamDecoderErrorCallback. This |
| 1144 | * pointer must not be \c NULL. |
| 1145 | * \param client_data This value will be supplied to callbacks in their |
| 1146 | * \a client_data argument. |
| 1147 | * \assert |
| 1148 | * \code decoder != NULL \endcode |
| 1149 | * \retval FLAC__StreamDecoderInitStatus |
| 1150 | * \c FLAC__STREAM_DECODER_INIT_STATUS_OK if initialization was successful; |
| 1151 | * see FLAC__StreamDecoderInitStatus for the meanings of other return values. |
| 1152 | */ |
| 1153 | FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_stream( |
| 1154 | FLAC__StreamDecoder *decoder, |
| 1155 | FLAC__StreamDecoderReadCallback read_callback, |
| 1156 | FLAC__StreamDecoderSeekCallback seek_callback, |
| 1157 | FLAC__StreamDecoderTellCallback tell_callback, |
| 1158 | FLAC__StreamDecoderLengthCallback length_callback, |
| 1159 | FLAC__StreamDecoderEofCallback eof_callback, |
| 1160 | FLAC__StreamDecoderWriteCallback write_callback, |
| 1161 | FLAC__StreamDecoderMetadataCallback metadata_callback, |
| 1162 | FLAC__StreamDecoderErrorCallback error_callback, |
| 1163 | void *client_data |
| 1164 | ); |
| 1165 | |
| 1166 | /** Initialize the decoder instance to decode native FLAC files. |
| 1167 | * |
| 1168 | * This flavor of initialization sets up the decoder to decode from a |
| 1169 | * plain native FLAC file. For non-stdio streams, you must use |
| 1170 | * FLAC__stream_decoder_init_stream() and provide callbacks for the I/O. |
| 1171 | * |
| 1172 | * This function should be called after FLAC__stream_decoder_new() and |
| 1173 | * FLAC__stream_decoder_set_*() but before any of the |
| 1174 | * FLAC__stream_decoder_process_*() functions. Will set and return the |
| 1175 | * decoder state, which will be FLAC__STREAM_DECODER_SEARCH_FOR_METADATA |
| 1176 | * if initialization succeeded. |
| 1177 | * |
| 1178 | * \param decoder An uninitialized decoder instance. |
| 1179 | * \param file An open FLAC file. The file should have been |
| 1180 | * opened with mode \c "rb" and rewound. The file |
| 1181 | * becomes owned by the decoder and should not be |
| 1182 | * manipulated by the client while decoding. |
| 1183 | * Unless \a file is \c stdin, it will be closed |
| 1184 | * when FLAC__stream_decoder_finish() is called. |
| 1185 | * Note however that seeking will not work when |
| 1186 | * decoding from \c stdout since it is not seekable. |
| 1187 | * \param write_callback See FLAC__StreamDecoderWriteCallback. This |
| 1188 | * pointer must not be \c NULL. |
| 1189 | * \param metadata_callback See FLAC__StreamDecoderMetadataCallback. This |
| 1190 | * pointer may be \c NULL if the callback is not |
| 1191 | * desired. |
| 1192 | * \param error_callback See FLAC__StreamDecoderErrorCallback. This |
| 1193 | * pointer must not be \c NULL. |
| 1194 | * \param client_data This value will be supplied to callbacks in their |
| 1195 | * \a client_data argument. |
| 1196 | * \assert |
| 1197 | * \code decoder != NULL \endcode |
| 1198 | * \code file != NULL \endcode |
| 1199 | * \retval FLAC__StreamDecoderInitStatus |
| 1200 | * \c FLAC__STREAM_DECODER_INIT_STATUS_OK if initialization was successful; |
| 1201 | * see FLAC__StreamDecoderInitStatus for the meanings of other return values. |
| 1202 | */ |
| 1203 | FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_FILE( |
| 1204 | FLAC__StreamDecoder *decoder, |
| 1205 | FILE *file, |
| 1206 | FLAC__StreamDecoderWriteCallback write_callback, |
| 1207 | FLAC__StreamDecoderMetadataCallback metadata_callback, |
| 1208 | FLAC__StreamDecoderErrorCallback error_callback, |
| 1209 | void *client_data |
| 1210 | ); |
| 1211 | |
| 1212 | /** Initialize the decoder instance to decode Ogg FLAC files. |
| 1213 | * |
| 1214 | * This flavor of initialization sets up the decoder to decode from a |
| 1215 | * plain Ogg FLAC file. For non-stdio streams, you must use |
| 1216 | * FLAC__stream_decoder_init_ogg_stream() and provide callbacks for the I/O. |
| 1217 | * |
| 1218 | * This function should be called after FLAC__stream_decoder_new() and |
| 1219 | * FLAC__stream_decoder_set_*() but before any of the |
| 1220 | * FLAC__stream_decoder_process_*() functions. Will set and return the |
| 1221 | * decoder state, which will be FLAC__STREAM_DECODER_SEARCH_FOR_METADATA |
| 1222 | * if initialization succeeded. |
| 1223 | * |
| 1224 | * \note Support for Ogg FLAC in the library is optional. If this |
| 1225 | * library has been built without support for Ogg FLAC, this function |
| 1226 | * will return \c FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER. |
| 1227 | * |
| 1228 | * \param decoder An uninitialized decoder instance. |
| 1229 | * \param file An open FLAC file. The file should have been |
| 1230 | * opened with mode \c "rb" and rewound. The file |
| 1231 | * becomes owned by the decoder and should not be |
| 1232 | * manipulated by the client while decoding. |
| 1233 | * Unless \a file is \c stdin, it will be closed |
| 1234 | * when FLAC__stream_decoder_finish() is called. |
| 1235 | * Note however that seeking will not work when |
| 1236 | * decoding from \c stdout since it is not seekable. |
| 1237 | * \param write_callback See FLAC__StreamDecoderWriteCallback. This |
| 1238 | * pointer must not be \c NULL. |
| 1239 | * \param metadata_callback See FLAC__StreamDecoderMetadataCallback. This |
| 1240 | * pointer may be \c NULL if the callback is not |
| 1241 | * desired. |
| 1242 | * \param error_callback See FLAC__StreamDecoderErrorCallback. This |
| 1243 | * pointer must not be \c NULL. |
| 1244 | * \param client_data This value will be supplied to callbacks in their |
| 1245 | * \a client_data argument. |
| 1246 | * \assert |
| 1247 | * \code decoder != NULL \endcode |
| 1248 | * \code file != NULL \endcode |
| 1249 | * \retval FLAC__StreamDecoderInitStatus |
| 1250 | * \c FLAC__STREAM_DECODER_INIT_STATUS_OK if initialization was successful; |
| 1251 | * see FLAC__StreamDecoderInitStatus for the meanings of other return values. |
| 1252 | */ |
| 1253 | FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_FILE( |
| 1254 | FLAC__StreamDecoder *decoder, |
| 1255 | FILE *file, |
| 1256 | FLAC__StreamDecoderWriteCallback write_callback, |
| 1257 | FLAC__StreamDecoderMetadataCallback metadata_callback, |
| 1258 | FLAC__StreamDecoderErrorCallback error_callback, |
| 1259 | void *client_data |
| 1260 | ); |
| 1261 | |
| 1262 | /** Initialize the decoder instance to decode native FLAC files. |
| 1263 | * |
| 1264 | * This flavor of initialization sets up the decoder to decode from a plain |
| 1265 | * native FLAC file. If POSIX fopen() semantics are not sufficient, (for |
| 1266 | * example, with Unicode filenames on Windows), you must use |
| 1267 | * FLAC__stream_decoder_init_FILE(), or FLAC__stream_decoder_init_stream() |
| 1268 | * and provide callbacks for the I/O. |
| 1269 | * |
| 1270 | * This function should be called after FLAC__stream_decoder_new() and |
| 1271 | * FLAC__stream_decoder_set_*() but before any of the |
| 1272 | * FLAC__stream_decoder_process_*() functions. Will set and return the |
| 1273 | * decoder state, which will be FLAC__STREAM_DECODER_SEARCH_FOR_METADATA |
| 1274 | * if initialization succeeded. |
| 1275 | * |
| 1276 | * \param decoder An uninitialized decoder instance. |
| 1277 | * \param filename The name of the file to decode from. The file will |
| 1278 | * be opened with fopen(). Use \c NULL to decode from |
| 1279 | * \c stdin. Note that \c stdin is not seekable. |
| 1280 | * \param write_callback See FLAC__StreamDecoderWriteCallback. This |
| 1281 | * pointer must not be \c NULL. |
| 1282 | * \param metadata_callback See FLAC__StreamDecoderMetadataCallback. This |
| 1283 | * pointer may be \c NULL if the callback is not |
| 1284 | * desired. |
| 1285 | * \param error_callback See FLAC__StreamDecoderErrorCallback. This |
| 1286 | * pointer must not be \c NULL. |
| 1287 | * \param client_data This value will be supplied to callbacks in their |
| 1288 | * \a client_data argument. |
| 1289 | * \assert |
| 1290 | * \code decoder != NULL \endcode |
| 1291 | * \retval FLAC__StreamDecoderInitStatus |
| 1292 | * \c FLAC__STREAM_DECODER_INIT_STATUS_OK if initialization was successful; |
| 1293 | * see FLAC__StreamDecoderInitStatus for the meanings of other return values. |
| 1294 | */ |
| 1295 | FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_file( |
| 1296 | FLAC__StreamDecoder *decoder, |
| 1297 | const char *filename, |
| 1298 | FLAC__StreamDecoderWriteCallback write_callback, |
| 1299 | FLAC__StreamDecoderMetadataCallback metadata_callback, |
| 1300 | FLAC__StreamDecoderErrorCallback error_callback, |
| 1301 | void *client_data |
| 1302 | ); |
| 1303 | |
| 1304 | /** Initialize the decoder instance to decode Ogg FLAC files. |
| 1305 | * |
| 1306 | * This flavor of initialization sets up the decoder to decode from a plain |
| 1307 | * Ogg FLAC file. If POSIX fopen() semantics are not sufficient, (for |
| 1308 | * example, with Unicode filenames on Windows), you must use |
| 1309 | * FLAC__stream_decoder_init_ogg_FILE(), or FLAC__stream_decoder_init_ogg_stream() |
| 1310 | * and provide callbacks for the I/O. |
| 1311 | * |
| 1312 | * This function should be called after FLAC__stream_decoder_new() and |
| 1313 | * FLAC__stream_decoder_set_*() but before any of the |
| 1314 | * FLAC__stream_decoder_process_*() functions. Will set and return the |
| 1315 | * decoder state, which will be FLAC__STREAM_DECODER_SEARCH_FOR_METADATA |
| 1316 | * if initialization succeeded. |
| 1317 | * |
| 1318 | * \note Support for Ogg FLAC in the library is optional. If this |
| 1319 | * library has been built without support for Ogg FLAC, this function |
| 1320 | * will return \c FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER. |
| 1321 | * |
| 1322 | * \param decoder An uninitialized decoder instance. |
| 1323 | * \param filename The name of the file to decode from. The file will |
| 1324 | * be opened with fopen(). Use \c NULL to decode from |
| 1325 | * \c stdin. Note that \c stdin is not seekable. |
| 1326 | * \param write_callback See FLAC__StreamDecoderWriteCallback. This |
| 1327 | * pointer must not be \c NULL. |
| 1328 | * \param metadata_callback See FLAC__StreamDecoderMetadataCallback. This |
| 1329 | * pointer may be \c NULL if the callback is not |
| 1330 | * desired. |
| 1331 | * \param error_callback See FLAC__StreamDecoderErrorCallback. This |
| 1332 | * pointer must not be \c NULL. |
| 1333 | * \param client_data This value will be supplied to callbacks in their |
| 1334 | * \a client_data argument. |
| 1335 | * \assert |
| 1336 | * \code decoder != NULL \endcode |
| 1337 | * \retval FLAC__StreamDecoderInitStatus |
| 1338 | * \c FLAC__STREAM_DECODER_INIT_STATUS_OK if initialization was successful; |
| 1339 | * see FLAC__StreamDecoderInitStatus for the meanings of other return values. |
| 1340 | */ |
| 1341 | FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_file( |
| 1342 | FLAC__StreamDecoder *decoder, |
| 1343 | const char *filename, |
| 1344 | FLAC__StreamDecoderWriteCallback write_callback, |
| 1345 | FLAC__StreamDecoderMetadataCallback metadata_callback, |
| 1346 | FLAC__StreamDecoderErrorCallback error_callback, |
| 1347 | void *client_data |
| 1348 | ); |
| 1349 | |
| 1350 | /** Finish the decoding process. |
| 1351 | * Flushes the decoding buffer, releases resources, resets the decoder |
| 1352 | * settings to their defaults, and returns the decoder state to |
| 1353 | * FLAC__STREAM_DECODER_UNINITIALIZED. |
| 1354 | * |
| 1355 | * In the event of a prematurely-terminated decode, it is not strictly |
| 1356 | * necessary to call this immediately before FLAC__stream_decoder_delete() |
| 1357 | * but it is good practice to match every FLAC__stream_decoder_init_*() |
| 1358 | * with a FLAC__stream_decoder_finish(). |
| 1359 | * |
| 1360 | * \param decoder An uninitialized decoder instance. |
| 1361 | * \assert |
| 1362 | * \code decoder != NULL \endcode |
| 1363 | * \retval FLAC__bool |
| 1364 | * \c false if MD5 checking is on AND a STREAMINFO block was available |
| 1365 | * AND the MD5 signature in the STREAMINFO block was non-zero AND the |
| 1366 | * signature does not match the one computed by the decoder; else |
| 1367 | * \c true. |
| 1368 | */ |
| 1369 | FLAC_API FLAC__bool FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder); |
| 1370 | |
| 1371 | /** Flush the stream input. |
| 1372 | * The decoder's input buffer will be cleared and the state set to |
| 1373 | * \c FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC. This will also turn |
| 1374 | * off MD5 checking. |
| 1375 | * |
| 1376 | * \param decoder A decoder instance. |
| 1377 | * \assert |
| 1378 | * \code decoder != NULL \endcode |
| 1379 | * \retval FLAC__bool |
| 1380 | * \c true if successful, else \c false if a memory allocation |
| 1381 | * error occurs (in which case the state will be set to |
| 1382 | * \c FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR). |
| 1383 | */ |
| 1384 | FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder); |
| 1385 | |
| 1386 | /** Reset the decoding process. |
| 1387 | * The decoder's input buffer will be cleared and the state set to |
| 1388 | * \c FLAC__STREAM_DECODER_SEARCH_FOR_METADATA. This is similar to |
| 1389 | * FLAC__stream_decoder_finish() except that the settings are |
| 1390 | * preserved; there is no need to call FLAC__stream_decoder_init_*() |
| 1391 | * before decoding again. MD5 checking will be restored to its original |
| 1392 | * setting. |
| 1393 | * |
| 1394 | * If the decoder is seekable, or was initialized with |
| 1395 | * FLAC__stream_decoder_init*_FILE() or FLAC__stream_decoder_init*_file(), |
| 1396 | * the decoder will also attempt to seek to the beginning of the file. |
| 1397 | * If this rewind fails, this function will return \c false. It follows |
| 1398 | * that FLAC__stream_decoder_reset() cannot be used when decoding from |
| 1399 | * \c stdin. |
| 1400 | * |
| 1401 | * If the decoder was initialized with FLAC__stream_encoder_init*_stream() |
| 1402 | * and is not seekable (i.e. no seek callback was provided or the seek |
| 1403 | * callback returns \c FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED), it |
| 1404 | * is the duty of the client to start feeding data from the beginning of |
| 1405 | * the stream on the next FLAC__stream_decoder_process() or |
| 1406 | * FLAC__stream_decoder_process_interleaved() call. |
| 1407 | * |
| 1408 | * \param decoder A decoder instance. |
| 1409 | * \assert |
| 1410 | * \code decoder != NULL \endcode |
| 1411 | * \retval FLAC__bool |
| 1412 | * \c true if successful, else \c false if a memory allocation occurs |
| 1413 | * (in which case the state will be set to |
| 1414 | * \c FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR) or a seek error |
| 1415 | * occurs (the state will be unchanged). |
| 1416 | */ |
| 1417 | FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder); |
| 1418 | |
| 1419 | /** Decode one metadata block or audio frame. |
| 1420 | * This version instructs the decoder to decode a either a single metadata |
| 1421 | * block or a single frame and stop, unless the callbacks return a fatal |
| 1422 | * error or the read callback returns |
| 1423 | * \c FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM. |
| 1424 | * |
| 1425 | * As the decoder needs more input it will call the read callback. |
| 1426 | * Depending on what was decoded, the metadata or write callback will be |
| 1427 | * called with the decoded metadata block or audio frame. |
| 1428 | * |
| 1429 | * Unless there is a fatal read error or end of stream, this function |
| 1430 | * will return once one whole frame is decoded. In other words, if the |
| 1431 | * stream is not synchronized or points to a corrupt frame header, the |
| 1432 | * decoder will continue to try and resync until it gets to a valid |
| 1433 | * frame, then decode one frame, then return. If the decoder points to |
| 1434 | * a frame whose frame CRC in the frame footer does not match the |
| 1435 | * computed frame CRC, this function will issue a |
| 1436 | * FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH error to the |
| 1437 | * error callback, and return, having decoded one complete, although |
| 1438 | * corrupt, frame. (Such corrupted frames are sent as silence of the |
| 1439 | * correct length to the write callback.) |
| 1440 | * |
| 1441 | * \param decoder An initialized decoder instance. |
| 1442 | * \assert |
| 1443 | * \code decoder != NULL \endcode |
| 1444 | * \retval FLAC__bool |
| 1445 | * \c false if any fatal read, write, or memory allocation error |
| 1446 | * occurred (meaning decoding must stop), else \c true; for more |
| 1447 | * information about the decoder, check the decoder state with |
| 1448 | * FLAC__stream_decoder_get_state(). |
| 1449 | */ |
| 1450 | FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder); |
| 1451 | |
| 1452 | /** Decode until the end of the metadata. |
| 1453 | * This version instructs the decoder to decode from the current position |
| 1454 | * and continue until all the metadata has been read, or until the |
| 1455 | * callbacks return a fatal error or the read callback returns |
| 1456 | * \c FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM. |
| 1457 | * |
| 1458 | * As the decoder needs more input it will call the read callback. |
| 1459 | * As each metadata block is decoded, the metadata callback will be called |
| 1460 | * with the decoded metadata. |
| 1461 | * |
| 1462 | * \param decoder An initialized decoder instance. |
| 1463 | * \assert |
| 1464 | * \code decoder != NULL \endcode |
| 1465 | * \retval FLAC__bool |
| 1466 | * \c false if any fatal read, write, or memory allocation error |
| 1467 | * occurred (meaning decoding must stop), else \c true; for more |
| 1468 | * information about the decoder, check the decoder state with |
| 1469 | * FLAC__stream_decoder_get_state(). |
| 1470 | */ |
| 1471 | FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder); |
| 1472 | |
| 1473 | /** Decode until the end of the stream. |
| 1474 | * This version instructs the decoder to decode from the current position |
| 1475 | * and continue until the end of stream (the read callback returns |
| 1476 | * \c FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM), or until the |
| 1477 | * callbacks return a fatal error. |
| 1478 | * |
| 1479 | * As the decoder needs more input it will call the read callback. |
| 1480 | * As each metadata block and frame is decoded, the metadata or write |
| 1481 | * callback will be called with the decoded metadata or frame. |
| 1482 | * |
| 1483 | * \param decoder An initialized decoder instance. |
| 1484 | * \assert |
| 1485 | * \code decoder != NULL \endcode |
| 1486 | * \retval FLAC__bool |
| 1487 | * \c false if any fatal read, write, or memory allocation error |
| 1488 | * occurred (meaning decoding must stop), else \c true; for more |
| 1489 | * information about the decoder, check the decoder state with |
| 1490 | * FLAC__stream_decoder_get_state(). |
| 1491 | */ |
| 1492 | FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder); |
| 1493 | |
| 1494 | /** Skip one audio frame. |
| 1495 | * This version instructs the decoder to 'skip' a single frame and stop, |
| 1496 | * unless the callbacks return a fatal error or the read callback returns |
| 1497 | * \c FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM. |
| 1498 | * |
| 1499 | * The decoding flow is the same as what occurs when |
| 1500 | * FLAC__stream_decoder_process_single() is called to process an audio |
| 1501 | * frame, except that this function does not decode the parsed data into |
| 1502 | * PCM or call the write callback. The integrity of the frame is still |
| 1503 | * checked the same way as in the other process functions. |
| 1504 | * |
| 1505 | * This function will return once one whole frame is skipped, in the |
| 1506 | * same way that FLAC__stream_decoder_process_single() will return once |
| 1507 | * one whole frame is decoded. |
| 1508 | * |
| 1509 | * This function can be used in more quickly determining FLAC frame |
| 1510 | * boundaries when decoding of the actual data is not needed, for |
| 1511 | * example when an application is separating a FLAC stream into frames |
| 1512 | * for editing or storing in a container. To do this, the application |
| 1513 | * can use FLAC__stream_decoder_skip_single_frame() to quickly advance |
| 1514 | * to the next frame, then use |
| 1515 | * FLAC__stream_decoder_get_decode_position() to find the new frame |
| 1516 | * boundary. |
| 1517 | * |
| 1518 | * This function should only be called when the stream has advanced |
| 1519 | * past all the metadata, otherwise it will return \c false. |
| 1520 | * |
| 1521 | * \param decoder An initialized decoder instance not in a metadata |
| 1522 | * state. |
| 1523 | * \assert |
| 1524 | * \code decoder != NULL \endcode |
| 1525 | * \retval FLAC__bool |
| 1526 | * \c false if any fatal read, write, or memory allocation error |
| 1527 | * occurred (meaning decoding must stop), or if the decoder |
| 1528 | * is in the FLAC__STREAM_DECODER_SEARCH_FOR_METADATA or |
| 1529 | * FLAC__STREAM_DECODER_READ_METADATA state, else \c true; for more |
| 1530 | * information about the decoder, check the decoder state with |
| 1531 | * FLAC__stream_decoder_get_state(). |
| 1532 | */ |
| 1533 | FLAC_API FLAC__bool FLAC__stream_decoder_skip_single_frame(FLAC__StreamDecoder *decoder); |
| 1534 | |
| 1535 | /** Flush the input and seek to an absolute sample. |
| 1536 | * Decoding will resume at the given sample. Note that because of |
| 1537 | * this, the next write callback may contain a partial block. The |
| 1538 | * client must support seeking the input or this function will fail |
| 1539 | * and return \c false. Furthermore, if the decoder state is |
| 1540 | * \c FLAC__STREAM_DECODER_SEEK_ERROR, then the decoder must be flushed |
| 1541 | * with FLAC__stream_decoder_flush() or reset with |
| 1542 | * FLAC__stream_decoder_reset() before decoding can continue. |
| 1543 | * |
| 1544 | * \param decoder A decoder instance. |
| 1545 | * \param sample The target sample number to seek to. |
| 1546 | * \assert |
| 1547 | * \code decoder != NULL \endcode |
| 1548 | * \retval FLAC__bool |
| 1549 | * \c true if successful, else \c false. |
| 1550 | */ |
| 1551 | FLAC_API FLAC__bool FLAC__stream_decoder_seek_absolute(FLAC__StreamDecoder *decoder, FLAC__uint64 sample); |
| 1552 | |
| 1553 | /* \} */ |
| 1554 | |
| 1555 | #ifdef __cplusplus |
| 1556 | } |
| 1557 | #endif |
| 1558 | |
| 1559 | #endif |
src/lib/libflac/include/flac/format.h
| r0 | r14135 | |
| 1 | /* libFLAC - Free Lossless Audio Codec library |
| 2 | * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * |
| 8 | * - Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * - Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * - Neither the name of the Xiph.org Foundation nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived from |
| 17 | * this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 26 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 27 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #ifndef FLAC__FORMAT_H |
| 33 | #define FLAC__FORMAT_H |
| 34 | |
| 35 | #include "export.h" |
| 36 | #include "ordinals.h" |
| 37 | |
| 38 | #ifdef __cplusplus |
| 39 | extern "C" { |
| 40 | #endif |
| 41 | |
| 42 | /** \file include/FLAC/format.h |
| 43 | * |
| 44 | * \brief |
| 45 | * This module contains structure definitions for the representation |
| 46 | * of FLAC format components in memory. These are the basic |
| 47 | * structures used by the rest of the interfaces. |
| 48 | * |
| 49 | * See the detailed documentation in the |
| 50 | * \link flac_format format \endlink module. |
| 51 | */ |
| 52 | |
| 53 | /** \defgroup flac_format FLAC/format.h: format components |
| 54 | * \ingroup flac |
| 55 | * |
| 56 | * \brief |
| 57 | * This module contains structure definitions for the representation |
| 58 | * of FLAC format components in memory. These are the basic |
| 59 | * structures used by the rest of the interfaces. |
| 60 | * |
| 61 | * First, you should be familiar with the |
| 62 | * <A HREF="../format.html">FLAC format</A>. Many of the values here |
| 63 | * follow directly from the specification. As a user of libFLAC, the |
| 64 | * interesting parts really are the structures that describe the frame |
| 65 | * header and metadata blocks. |
| 66 | * |
| 67 | * The format structures here are very primitive, designed to store |
| 68 | * information in an efficient way. Reading information from the |
| 69 | * structures is easy but creating or modifying them directly is |
| 70 | * more complex. For the most part, as a user of a library, editing |
| 71 | * is not necessary; however, for metadata blocks it is, so there are |
| 72 | * convenience functions provided in the \link flac_metadata metadata |
| 73 | * module \endlink to simplify the manipulation of metadata blocks. |
| 74 | * |
| 75 | * \note |
| 76 | * It's not the best convention, but symbols ending in _LEN are in bits |
| 77 | * and _LENGTH are in bytes. _LENGTH symbols are \#defines instead of |
| 78 | * global variables because they are usually used when declaring byte |
| 79 | * arrays and some compilers require compile-time knowledge of array |
| 80 | * sizes when declared on the stack. |
| 81 | * |
| 82 | * \{ |
| 83 | */ |
| 84 | |
| 85 | |
| 86 | /* |
| 87 | Most of the values described in this file are defined by the FLAC |
| 88 | format specification. There is nothing to tune here. |
| 89 | */ |
| 90 | |
| 91 | /** The largest legal metadata type code. */ |
| 92 | #define FLAC__MAX_METADATA_TYPE_CODE (126u) |
| 93 | |
| 94 | /** The minimum block size, in samples, permitted by the format. */ |
| 95 | #define FLAC__MIN_BLOCK_SIZE (16u) |
| 96 | |
| 97 | /** The maximum block size, in samples, permitted by the format. */ |
| 98 | #define FLAC__MAX_BLOCK_SIZE (65535u) |
| 99 | |
| 100 | /** The maximum block size, in samples, permitted by the FLAC subset for |
| 101 | * sample rates up to 48kHz. */ |
| 102 | #define FLAC__SUBSET_MAX_BLOCK_SIZE_48000HZ (4608u) |
| 103 | |
| 104 | /** The maximum number of channels permitted by the format. */ |
| 105 | #define FLAC__MAX_CHANNELS (8u) |
| 106 | |
| 107 | /** The minimum sample resolution permitted by the format. */ |
| 108 | #define FLAC__MIN_BITS_PER_SAMPLE (4u) |
| 109 | |
| 110 | /** The maximum sample resolution permitted by the format. */ |
| 111 | #define FLAC__MAX_BITS_PER_SAMPLE (32u) |
| 112 | |
| 113 | /** The maximum sample resolution permitted by libFLAC. |
| 114 | * |
| 115 | * \warning |
| 116 | * FLAC__MAX_BITS_PER_SAMPLE is the limit of the FLAC format. However, |
| 117 | * the reference encoder/decoder is currently limited to 24 bits because |
| 118 | * of prevalent 32-bit math, so make sure and use this value when |
| 119 | * appropriate. |
| 120 | */ |
| 121 | #define FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE (24u) |
| 122 | |
| 123 | /** The maximum sample rate permitted by the format. The value is |
| 124 | * ((2 ^ 16) - 1) * 10; see <A HREF="../format.html">FLAC format</A> |
| 125 | * as to why. |
| 126 | */ |
| 127 | #define FLAC__MAX_SAMPLE_RATE (655350u) |
| 128 | |
| 129 | /** The maximum LPC order permitted by the format. */ |
| 130 | #define FLAC__MAX_LPC_ORDER (32u) |
| 131 | |
| 132 | /** The maximum LPC order permitted by the FLAC subset for sample rates |
| 133 | * up to 48kHz. */ |
| 134 | #define FLAC__SUBSET_MAX_LPC_ORDER_48000HZ (12u) |
| 135 | |
| 136 | /** The minimum quantized linear predictor coefficient precision |
| 137 | * permitted by the format. |
| 138 | */ |
| 139 | #define FLAC__MIN_QLP_COEFF_PRECISION (5u) |
| 140 | |
| 141 | /** The maximum quantized linear predictor coefficient precision |
| 142 | * permitted by the format. |
| 143 | */ |
| 144 | #define FLAC__MAX_QLP_COEFF_PRECISION (15u) |
| 145 | |
| 146 | /** The maximum order of the fixed predictors permitted by the format. */ |
| 147 | #define FLAC__MAX_FIXED_ORDER (4u) |
| 148 | |
| 149 | /** The maximum Rice partition order permitted by the format. */ |
| 150 | #define FLAC__MAX_RICE_PARTITION_ORDER (15u) |
| 151 | |
| 152 | /** The maximum Rice partition order permitted by the FLAC Subset. */ |
| 153 | #define FLAC__SUBSET_MAX_RICE_PARTITION_ORDER (8u) |
| 154 | |
| 155 | /** The version string of the release, stamped onto the libraries and binaries. |
| 156 | * |
| 157 | * \note |
| 158 | * This does not correspond to the shared library version number, which |
| 159 | * is used to determine binary compatibility. |
| 160 | */ |
| 161 | extern FLAC_API const char *FLAC__VERSION_STRING; |
| 162 | |
| 163 | /** The vendor string inserted by the encoder into the VORBIS_COMMENT block. |
| 164 | * This is a NUL-terminated ASCII string; when inserted into the |
| 165 | * VORBIS_COMMENT the trailing null is stripped. |
| 166 | */ |
| 167 | extern FLAC_API const char *FLAC__VENDOR_STRING; |
| 168 | |
| 169 | /** The byte string representation of the beginning of a FLAC stream. */ |
| 170 | extern FLAC_API const FLAC__byte FLAC__STREAM_SYNC_STRING[4]; /* = "fLaC" */ |
| 171 | |
| 172 | /** The 32-bit integer big-endian representation of the beginning of |
| 173 | * a FLAC stream. |
| 174 | */ |
| 175 | extern FLAC_API const unsigned FLAC__STREAM_SYNC; /* = 0x664C6143 */ |
| 176 | |
| 177 | /** The length of the FLAC signature in bits. */ |
| 178 | extern FLAC_API const unsigned FLAC__STREAM_SYNC_LEN; /* = 32 bits */ |
| 179 | |
| 180 | /** The length of the FLAC signature in bytes. */ |
| 181 | #define FLAC__STREAM_SYNC_LENGTH (4u) |
| 182 | |
| 183 | |
| 184 | /***************************************************************************** |
| 185 | * |
| 186 | * Subframe structures |
| 187 | * |
| 188 | *****************************************************************************/ |
| 189 | |
| 190 | /*****************************************************************************/ |
| 191 | |
| 192 | /** An enumeration of the available entropy coding methods. */ |
| 193 | typedef enum { |
| 194 | FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE = 0, |
| 195 | /**< Residual is coded by partitioning into contexts, each with it's own |
| 196 | * 4-bit Rice parameter. */ |
| 197 | |
| 198 | FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2 = 1 |
| 199 | /**< Residual is coded by partitioning into contexts, each with it's own |
| 200 | * 5-bit Rice parameter. */ |
| 201 | } FLAC__EntropyCodingMethodType; |
| 202 | |
| 203 | /** Maps a FLAC__EntropyCodingMethodType to a C string. |
| 204 | * |
| 205 | * Using a FLAC__EntropyCodingMethodType as the index to this array will |
| 206 | * give the string equivalent. The contents should not be modified. |
| 207 | */ |
| 208 | extern FLAC_API const char * const FLAC__EntropyCodingMethodTypeString[]; |
| 209 | |
| 210 | |
| 211 | /** Contents of a Rice partitioned residual |
| 212 | */ |
| 213 | typedef struct { |
| 214 | |
| 215 | unsigned *parameters; |
| 216 | /**< The Rice parameters for each context. */ |
| 217 | |
| 218 | unsigned *raw_bits; |
| 219 | /**< Widths for escape-coded partitions. Will be non-zero for escaped |
| 220 | * partitions and zero for unescaped partitions. |
| 221 | */ |
| 222 | |
| 223 | unsigned capacity_by_order; |
| 224 | /**< The capacity of the \a parameters and \a raw_bits arrays |
| 225 | * specified as an order, i.e. the number of array elements |
| 226 | * allocated is 2 ^ \a capacity_by_order. |
| 227 | */ |
| 228 | } FLAC__EntropyCodingMethod_PartitionedRiceContents; |
| 229 | |
| 230 | /** Header for a Rice partitioned residual. (c.f. <A HREF="../format.html#partitioned_rice">format specification</A>) |
| 231 | */ |
| 232 | typedef struct { |
| 233 | |
| 234 | unsigned order; |
| 235 | /**< The partition order, i.e. # of contexts = 2 ^ \a order. */ |
| 236 | |
| 237 | const FLAC__EntropyCodingMethod_PartitionedRiceContents *contents; |
| 238 | /**< The context's Rice parameters and/or raw bits. */ |
| 239 | |
| 240 | } FLAC__EntropyCodingMethod_PartitionedRice; |
| 241 | |
| 242 | extern FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN; /**< == 4 (bits) */ |
| 243 | extern FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; /**< == 4 (bits) */ |
| 244 | extern FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN; /**< == 5 (bits) */ |
| 245 | extern FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN; /**< == 5 (bits) */ |
| 246 | |
| 247 | extern FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER; |
| 248 | /**< == (1<<FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN)-1 */ |
| 249 | extern FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER; |
| 250 | /**< == (1<<FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN)-1 */ |
| 251 | |
| 252 | /** Header for the entropy coding method. (c.f. <A HREF="../format.html#residual">format specification</A>) |
| 253 | */ |
| 254 | typedef struct { |
| 255 | FLAC__EntropyCodingMethodType type; |
| 256 | union { |
| 257 | FLAC__EntropyCodingMethod_PartitionedRice partitioned_rice; |
| 258 | } data; |
| 259 | } FLAC__EntropyCodingMethod; |
| 260 | |
| 261 | extern FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_TYPE_LEN; /**< == 2 (bits) */ |
| 262 | |
| 263 | /*****************************************************************************/ |
| 264 | |
| 265 | /** An enumeration of the available subframe types. */ |
| 266 | typedef enum { |
| 267 | FLAC__SUBFRAME_TYPE_CONSTANT = 0, /**< constant signal */ |
| 268 | FLAC__SUBFRAME_TYPE_VERBATIM = 1, /**< uncompressed signal */ |
| 269 | FLAC__SUBFRAME_TYPE_FIXED = 2, /**< fixed polynomial prediction */ |
| 270 | FLAC__SUBFRAME_TYPE_LPC = 3 /**< linear prediction */ |
| 271 | } FLAC__SubframeType; |
| 272 | |
| 273 | /** Maps a FLAC__SubframeType to a C string. |
| 274 | * |
| 275 | * Using a FLAC__SubframeType as the index to this array will |
| 276 | * give the string equivalent. The contents should not be modified. |
| 277 | */ |
| 278 | extern FLAC_API const char * const FLAC__SubframeTypeString[]; |
| 279 | |
| 280 | |
| 281 | /** CONSTANT subframe. (c.f. <A HREF="../format.html#subframe_constant">format specification</A>) |
| 282 | */ |
| 283 | typedef struct { |
| 284 | FLAC__int32 value; /**< The constant signal value. */ |
| 285 | } FLAC__Subframe_Constant; |
| 286 | |
| 287 | |
| 288 | /** VERBATIM subframe. (c.f. <A HREF="../format.html#subframe_verbatim">format specification</A>) |
| 289 | */ |
| 290 | typedef struct { |
| 291 | const FLAC__int32 *data; /**< A pointer to verbatim signal. */ |
| 292 | } FLAC__Subframe_Verbatim; |
| 293 | |
| 294 | |
| 295 | /** FIXED subframe. (c.f. <A HREF="../format.html#subframe_fixed">format specification</A>) |
| 296 | */ |
| 297 | typedef struct { |
| 298 | FLAC__EntropyCodingMethod entropy_coding_method; |
| 299 | /**< The residual coding method. */ |
| 300 | |
| 301 | unsigned order; |
| 302 | /**< The polynomial order. */ |
| 303 | |
| 304 | FLAC__int32 warmup[FLAC__MAX_FIXED_ORDER]; |
| 305 | /**< Warmup samples to prime the predictor, length == order. */ |
| 306 | |
| 307 | const FLAC__int32 *residual; |
| 308 | /**< The residual signal, length == (blocksize minus order) samples. */ |
| 309 | } FLAC__Subframe_Fixed; |
| 310 | |
| 311 | |
| 312 | /** LPC subframe. (c.f. <A HREF="../format.html#subframe_lpc">format specification</A>) |
| 313 | */ |
| 314 | typedef struct { |
| 315 | FLAC__EntropyCodingMethod entropy_coding_method; |
| 316 | /**< The residual coding method. */ |
| 317 | |
| 318 | unsigned order; |
| 319 | /**< The FIR order. */ |
| 320 | |
| 321 | unsigned qlp_coeff_precision; |
| 322 | /**< Quantized FIR filter coefficient precision in bits. */ |
| 323 | |
| 324 | int quantization_level; |
| 325 | /**< The qlp coeff shift needed. */ |
| 326 | |
| 327 | FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER]; |
| 328 | /**< FIR filter coefficients. */ |
| 329 | |
| 330 | FLAC__int32 warmup[FLAC__MAX_LPC_ORDER]; |
| 331 | /**< Warmup samples to prime the predictor, length == order. */ |
| 332 | |
| 333 | const FLAC__int32 *residual; |
| 334 | /**< The residual signal, length == (blocksize minus order) samples. */ |
| 335 | } FLAC__Subframe_LPC; |
| 336 | |
| 337 | extern FLAC_API const unsigned FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN; /**< == 4 (bits) */ |
| 338 | extern FLAC_API const unsigned FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN; /**< == 5 (bits) */ |
| 339 | |
| 340 | |
| 341 | /** FLAC subframe structure. (c.f. <A HREF="../format.html#subframe">format specification</A>) |
| 342 | */ |
| 343 | typedef struct { |
| 344 | FLAC__SubframeType type; |
| 345 | union { |
| 346 | FLAC__Subframe_Constant constant; |
| 347 | FLAC__Subframe_Fixed fixed; |
| 348 | FLAC__Subframe_LPC lpc; |
| 349 | FLAC__Subframe_Verbatim verbatim; |
| 350 | } data; |
| 351 | unsigned wasted_bits; |
| 352 | } FLAC__Subframe; |
| 353 | |
| 354 | /** == 1 (bit) |
| 355 | * |
| 356 | * This used to be a zero-padding bit (hence the name |
| 357 | * FLAC__SUBFRAME_ZERO_PAD_LEN) but is now a reserved bit. It still has a |
| 358 | * mandatory value of \c 0 but in the future may take on the value \c 0 or \c 1 |
| 359 | * to mean something else. |
| 360 | */ |
| 361 | extern FLAC_API const unsigned FLAC__SUBFRAME_ZERO_PAD_LEN; |
| 362 | extern FLAC_API const unsigned FLAC__SUBFRAME_TYPE_LEN; /**< == 6 (bits) */ |
| 363 | extern FLAC_API const unsigned FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN; /**< == 1 (bit) */ |
| 364 | |
| 365 | extern FLAC_API const unsigned FLAC__SUBFRAME_TYPE_CONSTANT_BYTE_ALIGNED_MASK; /**< = 0x00 */ |
| 366 | extern FLAC_API const unsigned FLAC__SUBFRAME_TYPE_VERBATIM_BYTE_ALIGNED_MASK; /**< = 0x02 */ |
| 367 | extern FLAC_API const unsigned FLAC__SUBFRAME_TYPE_FIXED_BYTE_ALIGNED_MASK; /**< = 0x10 */ |
| 368 | extern FLAC_API const unsigned FLAC__SUBFRAME_TYPE_LPC_BYTE_ALIGNED_MASK; /**< = 0x40 */ |
| 369 | |
| 370 | /*****************************************************************************/ |
| 371 | |
| 372 | |
| 373 | /***************************************************************************** |
| 374 | * |
| 375 | * Frame structures |
| 376 | * |
| 377 | *****************************************************************************/ |
| 378 | |
| 379 | /** An enumeration of the available channel assignments. */ |
| 380 | typedef enum { |
| 381 | FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT = 0, /**< independent channels */ |
| 382 | FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE = 1, /**< left+side stereo */ |
| 383 | FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE = 2, /**< right+side stereo */ |
| 384 | FLAC__CHANNEL_ASSIGNMENT_MID_SIDE = 3 /**< mid+side stereo */ |
| 385 | } FLAC__ChannelAssignment; |
| 386 | |
| 387 | /** Maps a FLAC__ChannelAssignment to a C string. |
| 388 | * |
| 389 | * Using a FLAC__ChannelAssignment as the index to this array will |
| 390 | * give the string equivalent. The contents should not be modified. |
| 391 | */ |
| 392 | extern FLAC_API const char * const FLAC__ChannelAssignmentString[]; |
| 393 | |
| 394 | /** An enumeration of the possible frame numbering methods. */ |
| 395 | typedef enum { |
| 396 | FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER, /**< number contains the frame number */ |
| 397 | FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER /**< number contains the sample number of first sample in frame */ |
| 398 | } FLAC__FrameNumberType; |
| 399 | |
| 400 | /** Maps a FLAC__FrameNumberType to a C string. |
| 401 | * |
| 402 | * Using a FLAC__FrameNumberType as the index to this array will |
| 403 | * give the string equivalent. The contents should not be modified. |
| 404 | */ |
| 405 | extern FLAC_API const char * const FLAC__FrameNumberTypeString[]; |
| 406 | |
| 407 | |
| 408 | /** FLAC frame header structure. (c.f. <A HREF="../format.html#frame_header">format specification</A>) |
| 409 | */ |
| 410 | typedef struct { |
| 411 | unsigned blocksize; |
| 412 | /**< The number of samples per subframe. */ |
| 413 | |
| 414 | unsigned sample_rate; |
| 415 | /**< The sample rate in Hz. */ |
| 416 | |
| 417 | unsigned channels; |
| 418 | /**< The number of channels (== number of subframes). */ |
| 419 | |
| 420 | FLAC__ChannelAssignment channel_assignment; |
| 421 | /**< The channel assignment for the frame. */ |
| 422 | |
| 423 | unsigned bits_per_sample; |
| 424 | /**< The sample resolution. */ |
| 425 | |
| 426 | FLAC__FrameNumberType number_type; |
| 427 | /**< The numbering scheme used for the frame. As a convenience, the |
| 428 | * decoder will always convert a frame number to a sample number because |
| 429 | * the rules are complex. */ |
| 430 | |
| 431 | union { |
| 432 | FLAC__uint32 frame_number; |
| 433 | FLAC__uint64 sample_number; |
| 434 | } number; |
| 435 | /**< The frame number or sample number of first sample in frame; |
| 436 | * use the \a number_type value to determine which to use. */ |
| 437 | |
| 438 | FLAC__uint8 crc; |
| 439 | /**< CRC-8 (polynomial = x^8 + x^2 + x^1 + x^0, initialized with 0) |
| 440 | * of the raw frame header bytes, meaning everything before the CRC byte |
| 441 | * including the sync code. |
| 442 | */ |
| 443 | } FLAC__FrameHeader; |
| 444 | |
| 445 | extern FLAC_API const unsigned FLAC__FRAME_HEADER_SYNC; /**< == 0x3ffe; the frame header sync code */ |
| 446 | extern FLAC_API const unsigned FLAC__FRAME_HEADER_SYNC_LEN; /**< == 14 (bits) */ |
| 447 | extern FLAC_API const unsigned FLAC__FRAME_HEADER_RESERVED_LEN; /**< == 1 (bits) */ |
| 448 | extern FLAC_API const unsigned FLAC__FRAME_HEADER_BLOCKING_STRATEGY_LEN; /**< == 1 (bits) */ |
| 449 | extern FLAC_API const unsigned FLAC__FRAME_HEADER_BLOCK_SIZE_LEN; /**< == 4 (bits) */ |
| 450 | extern FLAC_API const unsigned FLAC__FRAME_HEADER_SAMPLE_RATE_LEN; /**< == 4 (bits) */ |
| 451 | extern FLAC_API const unsigned FLAC__FRAME_HEADER_CHANNEL_ASSIGNMENT_LEN; /**< == 4 (bits) */ |
| 452 | extern FLAC_API const unsigned FLAC__FRAME_HEADER_BITS_PER_SAMPLE_LEN; /**< == 3 (bits) */ |
| 453 | extern FLAC_API const unsigned FLAC__FRAME_HEADER_ZERO_PAD_LEN; /**< == 1 (bit) */ |
| 454 | extern FLAC_API const unsigned FLAC__FRAME_HEADER_CRC_LEN; /**< == 8 (bits) */ |
| 455 | |
| 456 | |
| 457 | /** FLAC frame footer structure. (c.f. <A HREF="../format.html#frame_footer">format specification</A>) |
| 458 | */ |
| 459 | typedef struct { |
| 460 | FLAC__uint16 crc; |
| 461 | /**< CRC-16 (polynomial = x^16 + x^15 + x^2 + x^0, initialized with |
| 462 | * 0) of the bytes before the crc, back to and including the frame header |
| 463 | * sync code. |
| 464 | */ |
| 465 | } FLAC__FrameFooter; |
| 466 | |
| 467 | extern FLAC_API const unsigned FLAC__FRAME_FOOTER_CRC_LEN; /**< == 16 (bits) */ |
| 468 | |
| 469 | |
| 470 | /** FLAC frame structure. (c.f. <A HREF="../format.html#frame">format specification</A>) |
| 471 | */ |
| 472 | typedef struct { |
| 473 | FLAC__FrameHeader header; |
| 474 | FLAC__Subframe subframes[FLAC__MAX_CHANNELS]; |
| 475 | FLAC__FrameFooter footer; |
| 476 | } FLAC__Frame; |
| 477 | |
| 478 | /*****************************************************************************/ |
| 479 | |
| 480 | |
| 481 | /***************************************************************************** |
| 482 | * |
| 483 | * Meta-data structures |
| 484 | * |
| 485 | *****************************************************************************/ |
| 486 | |
| 487 | /** An enumeration of the available metadata block types. */ |
| 488 | typedef enum { |
| 489 | |
| 490 | FLAC__METADATA_TYPE_STREAMINFO = 0, |
| 491 | /**< <A HREF="../format.html#metadata_block_streaminfo">STREAMINFO</A> block */ |
| 492 | |
| 493 | FLAC__METADATA_TYPE_PADDING = 1, |
| 494 | /**< <A HREF="../format.html#metadata_block_padding">PADDING</A> block */ |
| 495 | |
| 496 | FLAC__METADATA_TYPE_APPLICATION = 2, |
| 497 | /**< <A HREF="../format.html#metadata_block_application">APPLICATION</A> block */ |
| 498 | |
| 499 | FLAC__METADATA_TYPE_SEEKTABLE = 3, |
| 500 | /**< <A HREF="../format.html#metadata_block_seektable">SEEKTABLE</A> block */ |
| 501 | |
| 502 | FLAC__METADATA_TYPE_VORBIS_COMMENT = 4, |
| 503 | /**< <A HREF="../format.html#metadata_block_vorbis_comment">VORBISCOMMENT</A> block (a.k.a. FLAC tags) */ |
| 504 | |
| 505 | FLAC__METADATA_TYPE_CUESHEET = 5, |
| 506 | /**< <A HREF="../format.html#metadata_block_cuesheet">CUESHEET</A> block */ |
| 507 | |
| 508 | FLAC__METADATA_TYPE_PICTURE = 6, |
| 509 | /**< <A HREF="../format.html#metadata_block_picture">PICTURE</A> block */ |
| 510 | |
| 511 | FLAC__METADATA_TYPE_UNDEFINED = 7 |
| 512 | /**< marker to denote beginning of undefined type range; this number will increase as new metadata types are added */ |
| 513 | |
| 514 | } FLAC__MetadataType; |
| 515 | |
| 516 | /** Maps a FLAC__MetadataType to a C string. |
| 517 | * |
| 518 | * Using a FLAC__MetadataType as the index to this array will |
| 519 | * give the string equivalent. The contents should not be modified. |
| 520 | */ |
| 521 | extern FLAC_API const char * const FLAC__MetadataTypeString[]; |
| 522 | |
| 523 | |
| 524 | /** FLAC STREAMINFO structure. (c.f. <A HREF="../format.html#metadata_block_streaminfo">format specification</A>) |
| 525 | */ |
| 526 | typedef struct { |
| 527 | unsigned min_blocksize, max_blocksize; |
| 528 | unsigned min_framesize, max_framesize; |
| 529 | unsigned sample_rate; |
| 530 | unsigned channels; |
| 531 | unsigned bits_per_sample; |
| 532 | FLAC__uint64 total_samples; |
| 533 | FLAC__byte md5sum[16]; |
| 534 | } FLAC__StreamMetadata_StreamInfo; |
| 535 | |
| 536 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN; /**< == 16 (bits) */ |
| 537 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN; /**< == 16 (bits) */ |
| 538 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN; /**< == 24 (bits) */ |
| 539 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN; /**< == 24 (bits) */ |
| 540 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN; /**< == 20 (bits) */ |
| 541 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN; /**< == 3 (bits) */ |
| 542 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN; /**< == 5 (bits) */ |
| 543 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN; /**< == 36 (bits) */ |
| 544 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_MD5SUM_LEN; /**< == 128 (bits) */ |
| 545 | |
| 546 | /** The total stream length of the STREAMINFO block in bytes. */ |
| 547 | #define FLAC__STREAM_METADATA_STREAMINFO_LENGTH (34u) |
| 548 | |
| 549 | /** FLAC PADDING structure. (c.f. <A HREF="../format.html#metadata_block_padding">format specification</A>) |
| 550 | */ |
| 551 | typedef struct { |
| 552 | int dummy; |
| 553 | /**< Conceptually this is an empty struct since we don't store the |
| 554 | * padding bytes. Empty structs are not allowed by some C compilers, |
| 555 | * hence the dummy. |
| 556 | */ |
| 557 | } FLAC__StreamMetadata_Padding; |
| 558 | |
| 559 | |
| 560 | /** FLAC APPLICATION structure. (c.f. <A HREF="../format.html#metadata_block_application">format specification</A>) |
| 561 | */ |
| 562 | typedef struct { |
| 563 | FLAC__byte id[4]; |
| 564 | FLAC__byte *data; |
| 565 | } FLAC__StreamMetadata_Application; |
| 566 | |
| 567 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_APPLICATION_ID_LEN; /**< == 32 (bits) */ |
| 568 | |
| 569 | /** SeekPoint structure used in SEEKTABLE blocks. (c.f. <A HREF="../format.html#seekpoint">format specification</A>) |
| 570 | */ |
| 571 | typedef struct { |
| 572 | FLAC__uint64 sample_number; |
| 573 | /**< The sample number of the target frame. */ |
| 574 | |
| 575 | FLAC__uint64 stream_offset; |
| 576 | /**< The offset, in bytes, of the target frame with respect to |
| 577 | * beginning of the first frame. */ |
| 578 | |
| 579 | unsigned frame_samples; |
| 580 | /**< The number of samples in the target frame. */ |
| 581 | } FLAC__StreamMetadata_SeekPoint; |
| 582 | |
| 583 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN; /**< == 64 (bits) */ |
| 584 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN; /**< == 64 (bits) */ |
| 585 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN; /**< == 16 (bits) */ |
| 586 | |
| 587 | /** The total stream length of a seek point in bytes. */ |
| 588 | #define FLAC__STREAM_METADATA_SEEKPOINT_LENGTH (18u) |
| 589 | |
| 590 | /** The value used in the \a sample_number field of |
| 591 | * FLAC__StreamMetadataSeekPoint used to indicate a placeholder |
| 592 | * point (== 0xffffffffffffffff). |
| 593 | */ |
| 594 | extern FLAC_API const FLAC__uint64 FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER; |
| 595 | |
| 596 | |
| 597 | /** FLAC SEEKTABLE structure. (c.f. <A HREF="../format.html#metadata_block_seektable">format specification</A>) |
| 598 | * |
| 599 | * \note From the format specification: |
| 600 | * - The seek points must be sorted by ascending sample number. |
| 601 | * - Each seek point's sample number must be the first sample of the |
| 602 | * target frame. |
| 603 | * - Each seek point's sample number must be unique within the table. |
| 604 | * - Existence of a SEEKTABLE block implies a correct setting of |
| 605 | * total_samples in the stream_info block. |
| 606 | * - Behavior is undefined when more than one SEEKTABLE block is |
| 607 | * present in a stream. |
| 608 | */ |
| 609 | typedef struct { |
| 610 | unsigned num_points; |
| 611 | FLAC__StreamMetadata_SeekPoint *points; |
| 612 | } FLAC__StreamMetadata_SeekTable; |
| 613 | |
| 614 | |
| 615 | /** Vorbis comment entry structure used in VORBIS_COMMENT blocks. (c.f. <A HREF="../format.html#metadata_block_vorbis_comment">format specification</A>) |
| 616 | * |
| 617 | * For convenience, the APIs maintain a trailing NUL character at the end of |
| 618 | * \a entry which is not counted toward \a length, i.e. |
| 619 | * \code strlen(entry) == length \endcode |
| 620 | */ |
| 621 | typedef struct { |
| 622 | FLAC__uint32 length; |
| 623 | FLAC__byte *entry; |
| 624 | } FLAC__StreamMetadata_VorbisComment_Entry; |
| 625 | |
| 626 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN; /**< == 32 (bits) */ |
| 627 | |
| 628 | |
| 629 | /** FLAC VORBIS_COMMENT structure. (c.f. <A HREF="../format.html#metadata_block_vorbis_comment">format specification</A>) |
| 630 | */ |
| 631 | typedef struct { |
| 632 | FLAC__StreamMetadata_VorbisComment_Entry vendor_string; |
| 633 | FLAC__uint32 num_comments; |
| 634 | FLAC__StreamMetadata_VorbisComment_Entry *comments; |
| 635 | } FLAC__StreamMetadata_VorbisComment; |
| 636 | |
| 637 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN; /**< == 32 (bits) */ |
| 638 | |
| 639 | |
| 640 | /** FLAC CUESHEET track index structure. (See the |
| 641 | * <A HREF="../format.html#cuesheet_track_index">format specification</A> for |
| 642 | * the full description of each field.) |
| 643 | */ |
| 644 | typedef struct { |
| 645 | FLAC__uint64 offset; |
| 646 | /**< Offset in samples, relative to the track offset, of the index |
| 647 | * point. |
| 648 | */ |
| 649 | |
| 650 | FLAC__byte number; |
| 651 | /**< The index point number. */ |
| 652 | } FLAC__StreamMetadata_CueSheet_Index; |
| 653 | |
| 654 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN; /**< == 64 (bits) */ |
| 655 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN; /**< == 8 (bits) */ |
| 656 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN; /**< == 3*8 (bits) */ |
| 657 | |
| 658 | |
| 659 | /** FLAC CUESHEET track structure. (See the |
| 660 | * <A HREF="../format.html#cuesheet_track">format specification</A> for |
| 661 | * the full description of each field.) |
| 662 | */ |
| 663 | typedef struct { |
| 664 | FLAC__uint64 offset; |
| 665 | /**< Track offset in samples, relative to the beginning of the FLAC audio stream. */ |
| 666 | |
| 667 | FLAC__byte number; |
| 668 | /**< The track number. */ |
| 669 | |
| 670 | char isrc[13]; |
| 671 | /**< Track ISRC. This is a 12-digit alphanumeric code plus a trailing \c NUL byte */ |
| 672 | |
| 673 | unsigned type:1; |
| 674 | /**< The track type: 0 for audio, 1 for non-audio. */ |
| 675 | |
| 676 | unsigned pre_emphasis:1; |
| 677 | /**< The pre-emphasis flag: 0 for no pre-emphasis, 1 for pre-emphasis. */ |
| 678 | |
| 679 | FLAC__byte num_indices; |
| 680 | /**< The number of track index points. */ |
| 681 | |
| 682 | FLAC__StreamMetadata_CueSheet_Index *indices; |
| 683 | /**< NULL if num_indices == 0, else pointer to array of index points. */ |
| 684 | |
| 685 | } FLAC__StreamMetadata_CueSheet_Track; |
| 686 | |
| 687 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN; /**< == 64 (bits) */ |
| 688 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN; /**< == 8 (bits) */ |
| 689 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN; /**< == 12*8 (bits) */ |
| 690 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN; /**< == 1 (bit) */ |
| 691 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN; /**< == 1 (bit) */ |
| 692 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN; /**< == 6+13*8 (bits) */ |
| 693 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN; /**< == 8 (bits) */ |
| 694 | |
| 695 | |
| 696 | /** FLAC CUESHEET structure. (See the |
| 697 | * <A HREF="../format.html#metadata_block_cuesheet">format specification</A> |
| 698 | * for the full description of each field.) |
| 699 | */ |
| 700 | typedef struct { |
| 701 | char media_catalog_number[129]; |
| 702 | /**< Media catalog number, in ASCII printable characters 0x20-0x7e. In |
| 703 | * general, the media catalog number may be 0 to 128 bytes long; any |
| 704 | * unused characters should be right-padded with NUL characters. |
| 705 | */ |
| 706 | |
| 707 | FLAC__uint64 lead_in; |
| 708 | /**< The number of lead-in samples. */ |
| 709 | |
| 710 | FLAC__bool is_cd; |
| 711 | /**< \c true if CUESHEET corresponds to a Compact Disc, else \c false. */ |
| 712 | |
| 713 | unsigned num_tracks; |
| 714 | /**< The number of tracks. */ |
| 715 | |
| 716 | FLAC__StreamMetadata_CueSheet_Track *tracks; |
| 717 | /**< NULL if num_tracks == 0, else pointer to array of tracks. */ |
| 718 | |
| 719 | } FLAC__StreamMetadata_CueSheet; |
| 720 | |
| 721 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN; /**< == 128*8 (bits) */ |
| 722 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN; /**< == 64 (bits) */ |
| 723 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN; /**< == 1 (bit) */ |
| 724 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN; /**< == 7+258*8 (bits) */ |
| 725 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN; /**< == 8 (bits) */ |
| 726 | |
| 727 | |
| 728 | /** An enumeration of the PICTURE types (see FLAC__StreamMetadataPicture and id3 v2.4 APIC tag). */ |
| 729 | typedef enum { |
| 730 | FLAC__STREAM_METADATA_PICTURE_TYPE_OTHER = 0, /**< Other */ |
| 731 | FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD = 1, /**< 32x32 pixels 'file icon' (PNG only) */ |
| 732 | FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON = 2, /**< Other file icon */ |
| 733 | FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER = 3, /**< Cover (front) */ |
| 734 | FLAC__STREAM_METADATA_PICTURE_TYPE_BACK_COVER = 4, /**< Cover (back) */ |
| 735 | FLAC__STREAM_METADATA_PICTURE_TYPE_LEAFLET_PAGE = 5, /**< Leaflet page */ |
| 736 | FLAC__STREAM_METADATA_PICTURE_TYPE_MEDIA = 6, /**< Media (e.g. label side of CD) */ |
| 737 | FLAC__STREAM_METADATA_PICTURE_TYPE_LEAD_ARTIST = 7, /**< Lead artist/lead performer/soloist */ |
| 738 | FLAC__STREAM_METADATA_PICTURE_TYPE_ARTIST = 8, /**< Artist/performer */ |
| 739 | FLAC__STREAM_METADATA_PICTURE_TYPE_CONDUCTOR = 9, /**< Conductor */ |
| 740 | FLAC__STREAM_METADATA_PICTURE_TYPE_BAND = 10, /**< Band/Orchestra */ |
| 741 | FLAC__STREAM_METADATA_PICTURE_TYPE_COMPOSER = 11, /**< Composer */ |
| 742 | FLAC__STREAM_METADATA_PICTURE_TYPE_LYRICIST = 12, /**< Lyricist/text writer */ |
| 743 | FLAC__STREAM_METADATA_PICTURE_TYPE_RECORDING_LOCATION = 13, /**< Recording Location */ |
| 744 | FLAC__STREAM_METADATA_PICTURE_TYPE_DURING_RECORDING = 14, /**< During recording */ |
| 745 | FLAC__STREAM_METADATA_PICTURE_TYPE_DURING_PERFORMANCE = 15, /**< During performance */ |
| 746 | FLAC__STREAM_METADATA_PICTURE_TYPE_VIDEO_SCREEN_CAPTURE = 16, /**< Movie/video screen capture */ |
| 747 | FLAC__STREAM_METADATA_PICTURE_TYPE_FISH = 17, /**< A bright coloured fish */ |
| 748 | FLAC__STREAM_METADATA_PICTURE_TYPE_ILLUSTRATION = 18, /**< Illustration */ |
| 749 | FLAC__STREAM_METADATA_PICTURE_TYPE_BAND_LOGOTYPE = 19, /**< Band/artist logotype */ |
| 750 | FLAC__STREAM_METADATA_PICTURE_TYPE_PUBLISHER_LOGOTYPE = 20, /**< Publisher/Studio logotype */ |
| 751 | FLAC__STREAM_METADATA_PICTURE_TYPE_UNDEFINED |
| 752 | } FLAC__StreamMetadata_Picture_Type; |
| 753 | |
| 754 | /** Maps a FLAC__StreamMetadata_Picture_Type to a C string. |
| 755 | * |
| 756 | * Using a FLAC__StreamMetadata_Picture_Type as the index to this array |
| 757 | * will give the string equivalent. The contents should not be |
| 758 | * modified. |
| 759 | */ |
| 760 | extern FLAC_API const char * const FLAC__StreamMetadata_Picture_TypeString[]; |
| 761 | |
| 762 | /** FLAC PICTURE structure. (See the |
| 763 | * <A HREF="../format.html#metadata_block_picture">format specification</A> |
| 764 | * for the full description of each field.) |
| 765 | */ |
| 766 | typedef struct { |
| 767 | FLAC__StreamMetadata_Picture_Type type; |
| 768 | /**< The kind of picture stored. */ |
| 769 | |
| 770 | char *mime_type; |
| 771 | /**< Picture data's MIME type, in ASCII printable characters |
| 772 | * 0x20-0x7e, NUL terminated. For best compatibility with players, |
| 773 | * use picture data of MIME type \c image/jpeg or \c image/png. A |
| 774 | * MIME type of '-->' is also allowed, in which case the picture |
| 775 | * data should be a complete URL. In file storage, the MIME type is |
| 776 | * stored as a 32-bit length followed by the ASCII string with no NUL |
| 777 | * terminator, but is converted to a plain C string in this structure |
| 778 | * for convenience. |
| 779 | */ |
| 780 | |
| 781 | FLAC__byte *description; |
| 782 | /**< Picture's description in UTF-8, NUL terminated. In file storage, |
| 783 | * the description is stored as a 32-bit length followed by the UTF-8 |
| 784 | * string with no NUL terminator, but is converted to a plain C string |
| 785 | * in this structure for convenience. |
| 786 | */ |
| 787 | |
| 788 | FLAC__uint32 width; |
| 789 | /**< Picture's width in pixels. */ |
| 790 | |
| 791 | FLAC__uint32 height; |
| 792 | /**< Picture's height in pixels. */ |
| 793 | |
| 794 | FLAC__uint32 depth; |
| 795 | /**< Picture's color depth in bits-per-pixel. */ |
| 796 | |
| 797 | FLAC__uint32 colors; |
| 798 | /**< For indexed palettes (like GIF), picture's number of colors (the |
| 799 | * number of palette entries), or \c 0 for non-indexed (i.e. 2^depth). |
| 800 | */ |
| 801 | |
| 802 | FLAC__uint32 data_length; |
| 803 | /**< Length of binary picture data in bytes. */ |
| 804 | |
| 805 | FLAC__byte *data; |
| 806 | /**< Binary picture data. */ |
| 807 | |
| 808 | } FLAC__StreamMetadata_Picture; |
| 809 | |
| 810 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_TYPE_LEN; /**< == 32 (bits) */ |
| 811 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN; /**< == 32 (bits) */ |
| 812 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN; /**< == 32 (bits) */ |
| 813 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN; /**< == 32 (bits) */ |
| 814 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN; /**< == 32 (bits) */ |
| 815 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN; /**< == 32 (bits) */ |
| 816 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_COLORS_LEN; /**< == 32 (bits) */ |
| 817 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN; /**< == 32 (bits) */ |
| 818 | |
| 819 | |
| 820 | /** Structure that is used when a metadata block of unknown type is loaded. |
| 821 | * The contents are opaque. The structure is used only internally to |
| 822 | * correctly handle unknown metadata. |
| 823 | */ |
| 824 | typedef struct { |
| 825 | FLAC__byte *data; |
| 826 | } FLAC__StreamMetadata_Unknown; |
| 827 | |
| 828 | |
| 829 | /** FLAC metadata block structure. (c.f. <A HREF="../format.html#metadata_block">format specification</A>) |
| 830 | */ |
| 831 | typedef struct { |
| 832 | FLAC__MetadataType type; |
| 833 | /**< The type of the metadata block; used determine which member of the |
| 834 | * \a data union to dereference. If type >= FLAC__METADATA_TYPE_UNDEFINED |
| 835 | * then \a data.unknown must be used. */ |
| 836 | |
| 837 | FLAC__bool is_last; |
| 838 | /**< \c true if this metadata block is the last, else \a false */ |
| 839 | |
| 840 | unsigned length; |
| 841 | /**< Length, in bytes, of the block data as it appears in the stream. */ |
| 842 | |
| 843 | union { |
| 844 | FLAC__StreamMetadata_StreamInfo stream_info; |
| 845 | FLAC__StreamMetadata_Padding padding; |
| 846 | FLAC__StreamMetadata_Application application; |
| 847 | FLAC__StreamMetadata_SeekTable seek_table; |
| 848 | FLAC__StreamMetadata_VorbisComment vorbis_comment; |
| 849 | FLAC__StreamMetadata_CueSheet cue_sheet; |
| 850 | FLAC__StreamMetadata_Picture picture; |
| 851 | FLAC__StreamMetadata_Unknown unknown; |
| 852 | } data; |
| 853 | /**< Polymorphic block data; use the \a type value to determine which |
| 854 | * to use. */ |
| 855 | } FLAC__StreamMetadata; |
| 856 | |
| 857 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_IS_LAST_LEN; /**< == 1 (bit) */ |
| 858 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_TYPE_LEN; /**< == 7 (bits) */ |
| 859 | extern FLAC_API const unsigned FLAC__STREAM_METADATA_LENGTH_LEN; /**< == 24 (bits) */ |
| 860 | |
| 861 | /** The total stream length of a metadata block header in bytes. */ |
| 862 | #define FLAC__STREAM_METADATA_HEADER_LENGTH (4u) |
| 863 | |
| 864 | /*****************************************************************************/ |
| 865 | |
| 866 | |
| 867 | /***************************************************************************** |
| 868 | * |
| 869 | * Utility functions |
| 870 | * |
| 871 | *****************************************************************************/ |
| 872 | |
| 873 | /** Tests that a sample rate is valid for FLAC. |
| 874 | * |
| 875 | * \param sample_rate The sample rate to test for compliance. |
| 876 | * \retval FLAC__bool |
| 877 | * \c true if the given sample rate conforms to the specification, else |
| 878 | * \c false. |
| 879 | */ |
| 880 | FLAC_API FLAC__bool FLAC__format_sample_rate_is_valid(unsigned sample_rate); |
| 881 | |
| 882 | /** Tests that a sample rate is valid for the FLAC subset. The subset rules |
| 883 | * for valid sample rates are slightly more complex since the rate has to |
| 884 | * be expressible completely in the frame header. |
| 885 | * |
| 886 | * \param sample_rate The sample rate to test for compliance. |
| 887 | * \retval FLAC__bool |
| 888 | * \c true if the given sample rate conforms to the specification for the |
| 889 | * subset, else \c false. |
| 890 | */ |
| 891 | FLAC_API FLAC__bool FLAC__format_sample_rate_is_subset(unsigned sample_rate); |
| 892 | |
| 893 | /** Check a Vorbis comment entry name to see if it conforms to the Vorbis |
| 894 | * comment specification. |
| 895 | * |
| 896 | * Vorbis comment names must be composed only of characters from |
| 897 | * [0x20-0x3C,0x3E-0x7D]. |
| 898 | * |
| 899 | * \param name A NUL-terminated string to be checked. |
| 900 | * \assert |
| 901 | * \code name != NULL \endcode |
| 902 | * \retval FLAC__bool |
| 903 | * \c false if entry name is illegal, else \c true. |
| 904 | */ |
| 905 | FLAC_API FLAC__bool FLAC__format_vorbiscomment_entry_name_is_legal(const char *name); |
| 906 | |
| 907 | /** Check a Vorbis comment entry value to see if it conforms to the Vorbis |
| 908 | * comment specification. |
| 909 | * |
| 910 | * Vorbis comment values must be valid UTF-8 sequences. |
| 911 | * |
| 912 | * \param value A string to be checked. |
| 913 | * \param length A the length of \a value in bytes. May be |
| 914 | * \c (unsigned)(-1) to indicate that \a value is a plain |
| 915 | * UTF-8 NUL-terminated string. |
| 916 | * \assert |
| 917 | * \code value != NULL \endcode |
| 918 | * \retval FLAC__bool |
| 919 | * \c false if entry name is illegal, else \c true. |
| 920 | */ |
| 921 | FLAC_API FLAC__bool FLAC__format_vorbiscomment_entry_value_is_legal(const FLAC__byte *value, unsigned length); |
| 922 | |
| 923 | /** Check a Vorbis comment entry to see if it conforms to the Vorbis |
| 924 | * comment specification. |
| 925 | * |
| 926 | * Vorbis comment entries must be of the form 'name=value', and 'name' and |
| 927 | * 'value' must be legal according to |
| 928 | * FLAC__format_vorbiscomment_entry_name_is_legal() and |
| 929 | * FLAC__format_vorbiscomment_entry_value_is_legal() respectively. |
| 930 | * |
| 931 | * \param entry An entry to be checked. |
| 932 | * \param length The length of \a entry in bytes. |
| 933 | * \assert |
| 934 | * \code value != NULL \endcode |
| 935 | * \retval FLAC__bool |
| 936 | * \c false if entry name is illegal, else \c true. |
| 937 | */ |
| 938 | FLAC_API FLAC__bool FLAC__format_vorbiscomment_entry_is_legal(const FLAC__byte *entry, unsigned length); |
| 939 | |
| 940 | /** Check a seek table to see if it conforms to the FLAC specification. |
| 941 | * See the format specification for limits on the contents of the |
| 942 | * seek table. |
| 943 | * |
| 944 | * \param seek_table A pointer to a seek table to be checked. |
| 945 | * \assert |
| 946 | * \code seek_table != NULL \endcode |
| 947 | * \retval FLAC__bool |
| 948 | * \c false if seek table is illegal, else \c true. |
| 949 | */ |
| 950 | FLAC_API FLAC__bool FLAC__format_seektable_is_legal(const FLAC__StreamMetadata_SeekTable *seek_table); |
| 951 | |
| 952 | /** Sort a seek table's seek points according to the format specification. |
| 953 | * This includes a "unique-ification" step to remove duplicates, i.e. |
| 954 | * seek points with identical \a sample_number values. Duplicate seek |
| 955 | * points are converted into placeholder points and sorted to the end of |
| 956 | * the table. |
| 957 | * |
| 958 | * \param seek_table A pointer to a seek table to be sorted. |
| 959 | * \assert |
| 960 | * \code seek_table != NULL \endcode |
| 961 | * \retval unsigned |
| 962 | * The number of duplicate seek points converted into placeholders. |
| 963 | */ |
| 964 | FLAC_API unsigned FLAC__format_seektable_sort(FLAC__StreamMetadata_SeekTable *seek_table); |
| 965 | |
| 966 | /** Check a cue sheet to see if it conforms to the FLAC specification. |
| 967 | * See the format specification for limits on the contents of the |
| 968 | * cue sheet. |
| 969 | * |
| 970 | * \param cue_sheet A pointer to an existing cue sheet to be checked. |
| 971 | * \param check_cd_da_subset If \c true, check CUESHEET against more |
| 972 | * stringent requirements for a CD-DA (audio) disc. |
| 973 | * \param violation Address of a pointer to a string. If there is a |
| 974 | * violation, a pointer to a string explanation of the |
| 975 | * violation will be returned here. \a violation may be |
| 976 | * \c NULL if you don't need the returned string. Do not |
| 977 | * free the returned string; it will always point to static |
| 978 | * data. |
| 979 | * \assert |
| 980 | * \code cue_sheet != NULL \endcode |
| 981 | * \retval FLAC__bool |
| 982 | * \c false if cue sheet is illegal, else \c true. |
| 983 | */ |
| 984 | FLAC_API FLAC__bool FLAC__format_cuesheet_is_legal(const FLAC__StreamMetadata_CueSheet *cue_sheet, FLAC__bool check_cd_da_subset, const char **violation); |
| 985 | |
| 986 | /** Check picture data to see if it conforms to the FLAC specification. |
| 987 | * See the format specification for limits on the contents of the |
| 988 | * PICTURE block. |
| 989 | * |
| 990 | * \param picture A pointer to existing picture data to be checked. |
| 991 | * \param violation Address of a pointer to a string. If there is a |
| 992 | * violation, a pointer to a string explanation of the |
| 993 | * violation will be returned here. \a violation may be |
| 994 | * \c NULL if you don't need the returned string. Do not |
| 995 | * free the returned string; it will always point to static |
| 996 | * data. |
| 997 | * \assert |
| 998 | * \code picture != NULL \endcode |
| 999 | * \retval FLAC__bool |
| 1000 | * \c false if picture data is illegal, else \c true. |
| 1001 | */ |
| 1002 | FLAC_API FLAC__bool FLAC__format_picture_is_legal(const FLAC__StreamMetadata_Picture *picture, const char **violation); |
| 1003 | |
| 1004 | /* \} */ |
| 1005 | |
| 1006 | #ifdef __cplusplus |
| 1007 | } |
| 1008 | #endif |
| 1009 | |
| 1010 | #endif |
src/lib/libflac/include/flac++/metadata.h
| r0 | r14135 | |
| 1 | /* libFLAC++ - Free Lossless Audio Codec library |
| 2 | * Copyright (C) 2002,2003,2004,2005,2006,2007 Josh Coalson |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * |
| 8 | * - Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * - Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * - Neither the name of the Xiph.org Foundation nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived from |
| 17 | * this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 26 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 27 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #ifndef FLACPP__METADATA_H |
| 33 | #define FLACPP__METADATA_H |
| 34 | |
| 35 | #include "export.h" |
| 36 | |
| 37 | #include "flac/metadata.h" |
| 38 | |
| 39 | // =============================================================== |
| 40 | // |
| 41 | // Full documentation for the metadata interface can be found |
| 42 | // in the C layer in include/FLAC/metadata.h |
| 43 | // |
| 44 | // =============================================================== |
| 45 | |
| 46 | /** \file include/FLAC++/metadata.h |
| 47 | * |
| 48 | * \brief |
| 49 | * This module provides classes for creating and manipulating FLAC |
| 50 | * metadata blocks in memory, and three progressively more powerful |
| 51 | * interfaces for traversing and editing metadata in FLAC files. |
| 52 | * |
| 53 | * See the detailed documentation for each interface in the |
| 54 | * \link flacpp_metadata metadata \endlink module. |
| 55 | */ |
| 56 | |
| 57 | /** \defgroup flacpp_metadata FLAC++/metadata.h: metadata interfaces |
| 58 | * \ingroup flacpp |
| 59 | * |
| 60 | * \brief |
| 61 | * This module provides classes for creating and manipulating FLAC |
| 62 | * metadata blocks in memory, and three progressively more powerful |
| 63 | * interfaces for traversing and editing metadata in FLAC files. |
| 64 | * |
| 65 | * The behavior closely mimics the C layer interface; be sure to read |
| 66 | * the detailed description of the |
| 67 | * \link flac_metadata C metadata module \endlink. Note that like the |
| 68 | * C layer, currently only the Chain interface (level 2) supports Ogg |
| 69 | * FLAC files, and it is read-only i.e. no writing back changed |
| 70 | * metadata to file. |
| 71 | */ |
| 72 | |
| 73 | |
| 74 | namespace FLAC { |
| 75 | namespace Metadata { |
| 76 | |
| 77 | // ============================================================ |
| 78 | // |
| 79 | // Metadata objects |
| 80 | // |
| 81 | // ============================================================ |
| 82 | |
| 83 | /** \defgroup flacpp_metadata_object FLAC++/metadata.h: metadata object classes |
| 84 | * \ingroup flacpp_metadata |
| 85 | * |
| 86 | * This module contains classes representing FLAC metadata |
| 87 | * blocks in memory. |
| 88 | * |
| 89 | * The behavior closely mimics the C layer interface; be |
| 90 | * sure to read the detailed description of the |
| 91 | * \link flac_metadata_object C metadata object module \endlink. |
| 92 | * |
| 93 | * Any time a metadata object is constructed or assigned, you |
| 94 | * should check is_valid() to make sure the underlying |
| 95 | * ::FLAC__StreamMetadata object was able to be created. |
| 96 | * |
| 97 | * \warning |
| 98 | * When the get_*() methods of any metadata object method |
| 99 | * return you a const pointer, DO NOT disobey and write into it. |
| 100 | * Always use the set_*() methods. |
| 101 | * |
| 102 | * \{ |
| 103 | */ |
| 104 | |
| 105 | /** Base class for all metadata block types. |
| 106 | * See the \link flacpp_metadata_object overview \endlink for more. |
| 107 | */ |
| 108 | class FLACPP_API Prototype { |
| 109 | protected: |
| 110 | //@{ |
| 111 | /** Constructs a copy of the given object. This form |
| 112 | * always performs a deep copy. |
| 113 | */ |
| 114 | Prototype(const Prototype &); |
| 115 | Prototype(const ::FLAC__StreamMetadata &); |
| 116 | Prototype(const ::FLAC__StreamMetadata *); |
| 117 | //@} |
| 118 | |
| 119 | /** Constructs an object with copy control. When \a copy |
| 120 | * is \c true, behaves identically to |
| 121 | * FLAC::Metadata::Prototype::Prototype(const ::FLAC__StreamMetadata *object). |
| 122 | * When \a copy is \c false, the instance takes ownership of |
| 123 | * the pointer and the ::FLAC__StreamMetadata object will |
| 124 | * be freed by the destructor. |
| 125 | * |
| 126 | * \assert |
| 127 | * \code object != NULL \endcode |
| 128 | */ |
| 129 | Prototype(::FLAC__StreamMetadata *object, bool copy); |
| 130 | |
| 131 | //@{ |
| 132 | /** Assign from another object. Always performs a deep copy. */ |
| 133 | Prototype &operator=(const Prototype &); |
| 134 | Prototype &operator=(const ::FLAC__StreamMetadata &); |
| 135 | Prototype &operator=(const ::FLAC__StreamMetadata *); |
| 136 | //@} |
| 137 | |
| 138 | /** Assigns an object with copy control. See |
| 139 | * Prototype(::FLAC__StreamMetadata *object, bool copy). |
| 140 | */ |
| 141 | Prototype &assign_object(::FLAC__StreamMetadata *object, bool copy); |
| 142 | |
| 143 | /** Deletes the underlying ::FLAC__StreamMetadata object. |
| 144 | */ |
| 145 | virtual void clear(); |
| 146 | |
| 147 | ::FLAC__StreamMetadata *object_; |
| 148 | public: |
| 149 | /** Deletes the underlying ::FLAC__StreamMetadata object. |
| 150 | */ |
| 151 | virtual ~Prototype(); |
| 152 | |
| 153 | //@{ |
| 154 | /** Check for equality, performing a deep compare by following pointers. |
| 155 | */ |
| 156 | inline bool operator==(const Prototype &) const; |
| 157 | inline bool operator==(const ::FLAC__StreamMetadata &) const; |
| 158 | inline bool operator==(const ::FLAC__StreamMetadata *) const; |
| 159 | //@} |
| 160 | |
| 161 | //@{ |
| 162 | /** Check for inequality, performing a deep compare by following pointers. */ |
| 163 | inline bool operator!=(const Prototype &) const; |
| 164 | inline bool operator!=(const ::FLAC__StreamMetadata &) const; |
| 165 | inline bool operator!=(const ::FLAC__StreamMetadata *) const; |
| 166 | //@} |
| 167 | |
| 168 | friend class SimpleIterator; |
| 169 | friend class Iterator; |
| 170 | |
| 171 | /** Returns \c true if the object was correctly constructed |
| 172 | * (i.e. the underlying ::FLAC__StreamMetadata object was |
| 173 | * properly allocated), else \c false. |
| 174 | */ |
| 175 | inline bool is_valid() const; |
| 176 | |
| 177 | /** Returns \c true if this block is the last block in a |
| 178 | * stream, else \c false. |
| 179 | * |
| 180 | * \assert |
| 181 | * \code is_valid() \endcode |
| 182 | */ |
| 183 | bool get_is_last() const; |
| 184 | |
| 185 | /** Returns the type of the block. |
| 186 | * |
| 187 | * \assert |
| 188 | * \code is_valid() \endcode |
| 189 | */ |
| 190 | ::FLAC__MetadataType get_type() const; |
| 191 | |
| 192 | /** Returns the stream length of the metadata block. |
| 193 | * |
| 194 | * \note |
| 195 | * The length does not include the metadata block header, |
| 196 | * per spec. |
| 197 | * |
| 198 | * \assert |
| 199 | * \code is_valid() \endcode |
| 200 | */ |
| 201 | unsigned get_length() const; |
| 202 | |
| 203 | /** Sets the "is_last" flag for the block. When using the iterators |
| 204 | * it is not necessary to set this flag; they will do it for you. |
| 205 | * |
| 206 | * \assert |
| 207 | * \code is_valid() \endcode |
| 208 | */ |
| 209 | void set_is_last(bool); |
| 210 | |
| 211 | /** Returns a pointer to the underlying ::FLAC__StreamMetadata |
| 212 | * object. This can be useful for plugging any holes between |
| 213 | * the C++ and C interfaces. |
| 214 | * |
| 215 | * \assert |
| 216 | * \code is_valid() \endcode |
| 217 | */ |
| 218 | inline operator const ::FLAC__StreamMetadata *() const; |
| 219 | private: |
| 220 | /** Private and undefined so you can't use it. */ |
| 221 | Prototype(); |
| 222 | |
| 223 | // These are used only by Iterator |
| 224 | bool is_reference_; |
| 225 | inline void set_reference(bool x) { is_reference_ = x; } |
| 226 | }; |
| 227 | |
| 228 | #ifdef _MSC_VER |
| 229 | // warning C4800: 'int' : forcing to bool 'true' or 'false' (performance warning) |
| 230 | #pragma warning ( disable : 4800 ) |
| 231 | #endif |
| 232 | |
| 233 | inline bool Prototype::operator==(const Prototype &object) const |
| 234 | { return (bool)::FLAC__metadata_object_is_equal(object_, object.object_); } |
| 235 | |
| 236 | inline bool Prototype::operator==(const ::FLAC__StreamMetadata &object) const |
| 237 | { return (bool)::FLAC__metadata_object_is_equal(object_, &object); } |
| 238 | |
| 239 | inline bool Prototype::operator==(const ::FLAC__StreamMetadata *object) const |
| 240 | { return (bool)::FLAC__metadata_object_is_equal(object_, object); } |
| 241 | |
| 242 | #ifdef _MSC_VER |
| 243 | // @@@ how to re-enable? the following doesn't work |
| 244 | // #pragma warning ( enable : 4800 ) |
| 245 | #endif |
| 246 | |
| 247 | inline bool Prototype::operator!=(const Prototype &object) const |
| 248 | { return !operator==(object); } |
| 249 | |
| 250 | inline bool Prototype::operator!=(const ::FLAC__StreamMetadata &object) const |
| 251 | { return !operator==(object); } |
| 252 | |
| 253 | inline bool Prototype::operator!=(const ::FLAC__StreamMetadata *object) const |
| 254 | { return !operator==(object); } |
| 255 | |
| 256 | inline bool Prototype::is_valid() const |
| 257 | { return 0 != object_; } |
| 258 | |
| 259 | inline Prototype::operator const ::FLAC__StreamMetadata *() const |
| 260 | { return object_; } |
| 261 | |
| 262 | /** Create a deep copy of an object and return it. */ |
| 263 | FLACPP_API Prototype *clone(const Prototype *); |
| 264 | |
| 265 | |
| 266 | /** STREAMINFO metadata block. |
| 267 | * See the \link flacpp_metadata_object overview \endlink for more, |
| 268 | * and the <A HREF="../format.html#metadata_block_streaminfo">format specification</A>. |
| 269 | */ |
| 270 | class FLACPP_API StreamInfo : public Prototype { |
| 271 | public: |
| 272 | StreamInfo(); |
| 273 | |
| 274 | //@{ |
| 275 | /** Constructs a copy of the given object. This form |
| 276 | * always performs a deep copy. |
| 277 | */ |
| 278 | inline StreamInfo(const StreamInfo &object): Prototype(object) { } |
| 279 | inline StreamInfo(const ::FLAC__StreamMetadata &object): Prototype(object) { } |
| 280 | inline StreamInfo(const ::FLAC__StreamMetadata *object): Prototype(object) { } |
| 281 | //@} |
| 282 | |
| 283 | /** Constructs an object with copy control. See |
| 284 | * Prototype(::FLAC__StreamMetadata *object, bool copy). |
| 285 | */ |
| 286 | inline StreamInfo(::FLAC__StreamMetadata *object, bool copy): Prototype(object, copy) { } |
| 287 | |
| 288 | ~StreamInfo(); |
| 289 | |
| 290 | //@{ |
| 291 | /** Assign from another object. Always performs a deep copy. */ |
| 292 | inline StreamInfo &operator=(const StreamInfo &object) { Prototype::operator=(object); return *this; } |
| 293 | inline StreamInfo &operator=(const ::FLAC__StreamMetadata &object) { Prototype::operator=(object); return *this; } |
| 294 | inline StreamInfo &operator=(const ::FLAC__StreamMetadata *object) { Prototype::operator=(object); return *this; } |
| 295 | //@} |
| 296 | |
| 297 | /** Assigns an object with copy control. See |
| 298 | * Prototype::assign_object(::FLAC__StreamMetadata *object, bool copy). |
| 299 | */ |
| 300 | inline StreamInfo &assign(::FLAC__StreamMetadata *object, bool copy) { Prototype::assign_object(object, copy); return *this; } |
| 301 | |
| 302 | //@{ |
| 303 | /** Check for equality, performing a deep compare by following pointers. */ |
| 304 | inline bool operator==(const StreamInfo &object) const { return Prototype::operator==(object); } |
| 305 | inline bool operator==(const ::FLAC__StreamMetadata &object) const { return Prototype::operator==(object); } |
| 306 | inline bool operator==(const ::FLAC__StreamMetadata *object) const { return Prototype::operator==(object); } |
| 307 | //@} |
| 308 | |
| 309 | //@{ |
| 310 | /** Check for inequality, performing a deep compare by following pointers. */ |
| 311 | inline bool operator!=(const StreamInfo &object) const { return Prototype::operator!=(object); } |
| 312 | inline bool operator!=(const ::FLAC__StreamMetadata &object) const { return Prototype::operator!=(object); } |
| 313 | inline bool operator!=(const ::FLAC__StreamMetadata *object) const { return Prototype::operator!=(object); } |
| 314 | //@} |
| 315 | |
| 316 | //@{ |
| 317 | /** See <A HREF="../format.html#metadata_block_streaminfo">format specification</A>. */ |
| 318 | unsigned get_min_blocksize() const; |
| 319 | unsigned get_max_blocksize() const; |
| 320 | unsigned get_min_framesize() const; |
| 321 | unsigned get_max_framesize() const; |
| 322 | unsigned get_sample_rate() const; |
| 323 | unsigned get_channels() const; |
| 324 | unsigned get_bits_per_sample() const; |
| 325 | FLAC__uint64 get_total_samples() const; |
| 326 | const FLAC__byte *get_md5sum() const; |
| 327 | |
| 328 | void set_min_blocksize(unsigned value); |
| 329 | void set_max_blocksize(unsigned value); |
| 330 | void set_min_framesize(unsigned value); |
| 331 | void set_max_framesize(unsigned value); |
| 332 | void set_sample_rate(unsigned value); |
| 333 | void set_channels(unsigned value); |
| 334 | void set_bits_per_sample(unsigned value); |
| 335 | void set_total_samples(FLAC__uint64 value); |
| 336 | void set_md5sum(const FLAC__byte value[16]); |
| 337 | //@} |
| 338 | }; |
| 339 | |
| 340 | /** PADDING metadata block. |
| 341 | * See the \link flacpp_metadata_object overview \endlink for more, |
| 342 | * and the <A HREF="../format.html#metadata_block_padding">format specification</A>. |
| 343 | */ |
| 344 | class FLACPP_API Padding : public Prototype { |
| 345 | public: |
| 346 | Padding(); |
| 347 | |
| 348 | //@{ |
| 349 | /** Constructs a copy of the given object. This form |
| 350 | * always performs a deep copy. |
| 351 | */ |
| 352 | inline Padding(const Padding &object): Prototype(object) { } |
| 353 | inline Padding(const ::FLAC__StreamMetadata &object): Prototype(object) { } |
| 354 | inline Padding(const ::FLAC__StreamMetadata *object): Prototype(object) { } |
| 355 | //@} |
| 356 | |
| 357 | /** Constructs an object with copy control. See |
| 358 | * Prototype(::FLAC__StreamMetadata *object, bool copy). |
| 359 | */ |
| 360 | inline Padding(::FLAC__StreamMetadata *object, bool copy): Prototype(object, copy) { } |
| 361 | |
| 362 | ~Padding(); |
| 363 | |
| 364 | //@{ |
| 365 | /** Assign from another object. Always performs a deep copy. */ |
| 366 | inline Padding &operator=(const Padding &object) { Prototype::operator=(object); return *this; } |
| 367 | inline Padding &operator=(const ::FLAC__StreamMetadata &object) { Prototype::operator=(object); return *this; } |
| 368 | inline Padding &operator=(const ::FLAC__StreamMetadata *object) { Prototype::operator=(object); return *this; } |
| 369 | //@} |
| 370 | |
| 371 | /** Assigns an object with copy control. See |
| 372 | * Prototype::assign_object(::FLAC__StreamMetadata *object, bool copy). |
| 373 | */ |
| 374 | inline Padding &assign(::FLAC__StreamMetadata *object, bool copy) { Prototype::assign_object(object, copy); return *this; } |
| 375 | |
| 376 | //@{ |
| 377 | /** Check for equality, performing a deep compare by following pointers. */ |
| 378 | inline bool operator==(const Padding &object) const { return Prototype::operator==(object); } |
| 379 | inline bool operator==(const ::FLAC__StreamMetadata &object) const { return Prototype::operator==(object); } |
| 380 | inline bool operator==(const ::FLAC__StreamMetadata *object) const { return Prototype::operator==(object); } |
| 381 | //@} |
| 382 | |
| 383 | //@{ |
| 384 | /** Check for inequality, performing a deep compare by following pointers. */ |
| 385 | inline bool operator!=(const Padding &object) const { return Prototype::operator!=(object); } |
| 386 | inline bool operator!=(const ::FLAC__StreamMetadata &object) const { return Prototype::operator!=(object); } |
| 387 | inline bool operator!=(const ::FLAC__StreamMetadata *object) const { return Prototype::operator!=(object); } |
| 388 | //@} |
| 389 | |
| 390 | void set_length(unsigned length); |
| 391 | }; |
| 392 | |
| 393 | /** APPLICATION metadata block. |
| 394 | * See the \link flacpp_metadata_object overview \endlink for more, |
| 395 | * and the <A HREF="../format.html#metadata_block_application">format specification</A>. |
| 396 | */ |
| 397 | class FLACPP_API Application : public Prototype { |
| 398 | public: |
| 399 | Application(); |
| 400 | // |
| 401 | //@{ |
| 402 | /** Constructs a copy of the given object. This form |
| 403 | * always performs a deep copy. |
| 404 | */ |
| 405 | inline Application(const Application &object): Prototype(object) { } |
| 406 | inline Application(const ::FLAC__StreamMetadata &object): Prototype(object) { } |
| 407 | inline Application(const ::FLAC__StreamMetadata *object): Prototype(object) { } |
| 408 | //@} |
| 409 | |
| 410 | /** Constructs an object with copy control. See |
| 411 | * Prototype(::FLAC__StreamMetadata *object, bool copy). |
| 412 | */ |
| 413 | inline Application(::FLAC__StreamMetadata *object, bool copy): Prototype(object, copy) { } |
| 414 | |
| 415 | ~Application(); |
| 416 | |
| 417 | //@{ |
| 418 | /** Assign from another object. Always performs a deep copy. */ |
| 419 | inline Application &operator=(const Application &object) { Prototype::operator=(object); return *this; } |
| 420 | inline Application &operator=(const ::FLAC__StreamMetadata &object) { Prototype::operator=(object); return *this; } |
| 421 | inline Application &operator=(const ::FLAC__StreamMetadata *object) { Prototype::operator=(object); return *this; } |
| 422 | //@} |
| 423 | |
| 424 | /** Assigns an object with copy control. See |
| 425 | * Prototype::assign_object(::FLAC__StreamMetadata *object, bool copy). |
| 426 | */ |
| 427 | inline Application &assign(::FLAC__StreamMetadata *object, bool copy) { Prototype::assign_object(object, copy); return *this; } |
| 428 | |
| 429 | //@{ |
| 430 | /** Check for equality, performing a deep compare by following pointers. */ |
| 431 | inline bool operator==(const Application &object) const { return Prototype::operator==(object); } |
| 432 | inline bool operator==(const ::FLAC__StreamMetadata &object) const { return Prototype::operator==(object); } |
| 433 | inline bool operator==(const ::FLAC__StreamMetadata *object) const { return Prototype::operator==(object); } |
| 434 | //@} |
| 435 | |
| 436 | //@{ |
| 437 | /** Check for inequality, performing a deep compare by following pointers. */ |
| 438 | inline bool operator!=(const Application &object) const { return Prototype::operator!=(object); } |
| 439 | inline bool operator!=(const ::FLAC__StreamMetadata &object) const { return Prototype::operator!=(object); } |
| 440 | inline bool operator!=(const ::FLAC__StreamMetadata *object) const { return Prototype::operator!=(object); } |
| 441 | //@} |
| 442 | |
| 443 | const FLAC__byte *get_id() const; |
| 444 | const FLAC__byte *get_data() const; |
| 445 | |
| 446 | void set_id(const FLAC__byte value[4]); |
| 447 | //! This form always copies \a data |
| 448 | bool set_data(const FLAC__byte *data, unsigned length); |
| 449 | bool set_data(FLAC__byte *data, unsigned length, bool copy); |
| 450 | }; |
| 451 | |
| 452 | /** SEEKTABLE metadata block. |
| 453 | * See the \link flacpp_metadata_object overview \endlink for more, |
| 454 | * and the <A HREF="../format.html#metadata_block_seektable">format specification</A>. |
| 455 | */ |
| 456 | class FLACPP_API SeekTable : public Prototype { |
| 457 | public: |
| 458 | SeekTable(); |
| 459 | |
| 460 | //@{ |
| 461 | /** Constructs a copy of the given object. This form |
| 462 | * always performs a deep copy. |
| 463 | */ |
| 464 | inline SeekTable(const SeekTable &object): Prototype(object) { } |
| 465 | inline SeekTable(const ::FLAC__StreamMetadata &object): Prototype(object) { } |
| 466 | inline SeekTable(const ::FLAC__StreamMetadata *object): Prototype(object) { } |
| 467 | //@} |
| 468 | |
| 469 | /** Constructs an object with copy control. See |
| 470 | * Prototype(::FLAC__StreamMetadata *object, bool copy). |
| 471 | */ |
| 472 | inline SeekTable(::FLAC__StreamMetadata *object, bool copy): Prototype(object, copy) { } |
| 473 | |
| 474 | ~SeekTable(); |
| 475 | |
| 476 | //@{ |
| 477 | /** Assign from another object. Always performs a deep copy. */ |
| 478 | inline SeekTable &operator=(const SeekTable &object) { Prototype::operator=(object); return *this; } |
| 479 | inline SeekTable &operator=(const ::FLAC__StreamMetadata &object) { Prototype::operator=(object); return *this; } |
| 480 | inline SeekTable &operator=(const ::FLAC__StreamMetadata *object) { Prototype::operator=(object); return *this; } |
| 481 | //@} |
| 482 | |
| 483 | /** Assigns an object with copy control. See |
| 484 | * Prototype::assign_object(::FLAC__StreamMetadata *object, bool copy). |
| 485 | */ |
| 486 | inline SeekTable &assign(::FLAC__StreamMetadata *object, bool copy) { Prototype::assign_object(object, copy); return *this; } |
| 487 | |
| 488 | //@{ |
| 489 | /** Check for equality, performing a deep compare by following pointers. */ |
| 490 | inline bool operator==(const SeekTable &object) const { return Prototype::operator==(object); } |
| 491 | inline bool operator==(const ::FLAC__StreamMetadata &object) const { return Prototype::operator==(object); } |
| 492 | inline bool operator==(const ::FLAC__StreamMetadata *object) const { return Prototype::operator==(object); } |
| 493 | //@} |
| 494 | |
| 495 | //@{ |
| 496 | /** Check for inequality, performing a deep compare by following pointers. */ |
| 497 | inline bool operator!=(const SeekTable &object) const { return Prototype::operator!=(object); } |
| 498 | inline bool operator!=(const ::FLAC__StreamMetadata &object) const { return Prototype::operator!=(object); } |
| 499 | inline bool operator!=(const ::FLAC__StreamMetadata *object) const { return Prototype::operator!=(object); } |
| 500 | //@} |
| 501 | |
| 502 | unsigned get_num_points() const; |
| 503 | ::FLAC__StreamMetadata_SeekPoint get_point(unsigned index) const; |
| 504 | |
| 505 | //! See FLAC__metadata_object_seektable_set_point() |
| 506 | void set_point(unsigned index, const ::FLAC__StreamMetadata_SeekPoint &point); |
| 507 | |
| 508 | //! See FLAC__metadata_object_seektable_insert_point() |
| 509 | bool insert_point(unsigned index, const ::FLAC__StreamMetadata_SeekPoint &point); |
| 510 | |
| 511 | //! See FLAC__metadata_object_seektable_delete_point() |
| 512 | bool delete_point(unsigned index); |
| 513 | |
| 514 | //! See FLAC__metadata_object_seektable_is_legal() |
| 515 | bool is_legal() const; |
| 516 | }; |
| 517 | |
| 518 | /** VORBIS_COMMENT metadata block. |
| 519 | * See the \link flacpp_metadata_object overview \endlink for more, |
| 520 | * and the <A HREF="../format.html#metadata_block_vorbis_comment">format specification</A>. |
| 521 | */ |
| 522 | class FLACPP_API VorbisComment : public Prototype { |
| 523 | public: |
| 524 | /** Convenience class for encapsulating Vorbis comment |
| 525 | * entries. An entry is a vendor string or a comment |
| 526 | * field. In the case of a vendor string, the field |
| 527 | * name is undefined; only the field value is relevant. |
| 528 | * |
| 529 | * A \a field as used in the methods refers to an |
| 530 | * entire 'NAME=VALUE' string; for convenience the |
| 531 | * string is NUL-terminated. A length field is |
| 532 | * required in the unlikely event that the value |
| 533 | * contains contain embedded NULs. |
| 534 | * |
| 535 | * A \a field_name is what is on the left side of the |
| 536 | * first '=' in the \a field. By definition it is ASCII |
| 537 | * and so is NUL-terminated and does not require a |
| 538 | * length to describe it. \a field_name is undefined |
| 539 | * for a vendor string entry. |
| 540 | * |
| 541 | * A \a field_value is what is on the right side of the |
| 542 | * first '=' in the \a field. By definition, this may |
| 543 | * contain embedded NULs and so a \a field_value_length |
| 544 | * is required to describe it. However in practice, |
| 545 | * embedded NULs are not known to be used, so it is |
| 546 | * generally safe to treat field values as NUL- |
| 547 | * terminated UTF-8 strings. |
| 548 | * |
| 549 | * Always check is_valid() after the constructor or operator= |
| 550 | * to make sure memory was properly allocated and that the |
| 551 | * Entry conforms to the Vorbis comment specification. |
| 552 | */ |
| 553 | class FLACPP_API Entry { |
| 554 | public: |
| 555 | Entry(); |
| 556 | |
| 557 | Entry(const char *field, unsigned field_length); |
| 558 | Entry(const char *field); // assumes \a field is NUL-terminated |
| 559 | |
| 560 | Entry(const char *field_name, const char *field_value, unsigned field_value_length); |
| 561 | Entry(const char *field_name, const char *field_value); // assumes \a field_value is NUL-terminated |
| 562 | |
| 563 | Entry(const Entry &entry); |
| 564 | |
| 565 | Entry &operator=(const Entry &entry); |
| 566 | |
| 567 | virtual ~Entry(); |
| 568 | |
| 569 | virtual bool is_valid() const; ///< Returns \c true iff object was properly constructed. |
| 570 | |
| 571 | unsigned get_field_length() const; |
| 572 | unsigned get_field_name_length() const; |
| 573 | unsigned get_field_value_length() const; |
| 574 | |
| 575 | ::FLAC__StreamMetadata_VorbisComment_Entry get_entry() const; |
| 576 | const char *get_field() const; |
| 577 | const char *get_field_name() const; |
| 578 | const char *get_field_value() const; |
| 579 | |
| 580 | bool set_field(const char *field, unsigned field_length); |
| 581 | bool set_field(const char *field); // assumes \a field is NUL-terminated |
| 582 | bool set_field_name(const char *field_name); |
| 583 | bool set_field_value(const char *field_value, unsigned field_value_length); |
| 584 | bool set_field_value(const char *field_value); // assumes \a field_value is NUL-terminated |
| 585 | protected: |
| 586 | bool is_valid_; |
| 587 | ::FLAC__StreamMetadata_VorbisComment_Entry entry_; |
| 588 | char *field_name_; |
| 589 | unsigned field_name_length_; |
| 590 | char *field_value_; |
| 591 | unsigned field_value_length_; |
| 592 | private: |
| 593 | void zero(); |
| 594 | void clear(); |
| 595 | void clear_entry(); |
| 596 | void clear_field_name(); |
| 597 | void clear_field_value(); |
| 598 | void construct(const char *field, unsigned field_length); |
| 599 | void construct(const char *field); // assumes \a field is NUL-terminated |
| 600 | void construct(const char *field_name, const char *field_value, unsigned field_value_length); |
| 601 | void construct(const char *field_name, const char *field_value); // assumes \a field_value is NUL-terminated |
| 602 | void compose_field(); |
| 603 | void parse_field(); |
| 604 | }; |
| 605 | |
| 606 | VorbisComment(); |
| 607 | |
| 608 | //@{ |
| 609 | /** Constructs a copy of the given object. This form |
| 610 | * always performs a deep copy. |
| 611 | */ |
| 612 | inline VorbisComment(const VorbisComment &object): Prototype(object) { } |
| 613 | inline VorbisComment(const ::FLAC__StreamMetadata &object): Prototype(object) { } |
| 614 | inline VorbisComment(const ::FLAC__StreamMetadata *object): Prototype(object) { } |
| 615 | //@} |
| 616 | |
| 617 | /** Constructs an object with copy control. See |
| 618 | * Prototype(::FLAC__StreamMetadata *object, bool copy). |
| 619 | */ |
| 620 | inline VorbisComment(::FLAC__StreamMetadata *object, bool copy): Prototype(object, copy) { } |
| 621 | |
| 622 | ~VorbisComment(); |
| 623 | |
| 624 | //@{ |
| 625 | /** Assign from another object. Always performs a deep copy. */ |
| 626 | inline VorbisComment &operator=(const VorbisComment &object) { Prototype::operator=(object); return *this; } |
| 627 | inline VorbisComment &operator=(const ::FLAC__StreamMetadata &object) { Prototype::operator=(object); return *this; } |
| 628 | inline VorbisComment &operator=(const ::FLAC__StreamMetadata *object) { Prototype::operator=(object); return *this; } |
| 629 | //@} |
| 630 | |
| 631 | /** Assigns an object with copy control. See |
| 632 | * Prototype::assign_object(::FLAC__StreamMetadata *object, bool copy). |
| 633 | */ |
| 634 | inline VorbisComment &assign(::FLAC__StreamMetadata *object, bool copy) { Prototype::assign_object(object, copy); return *this; } |
| 635 | |
| 636 | //@{ |
| 637 | /** Check for equality, performing a deep compare by following pointers. */ |
| 638 | inline bool operator==(const VorbisComment &object) const { return Prototype::operator==(object); } |
| 639 | inline bool operator==(const ::FLAC__StreamMetadata &object) const { return Prototype::operator==(object); } |
| 640 | inline bool operator==(const ::FLAC__StreamMetadata *object) const { return Prototype::operator==(object); } |
| 641 | //@} |
| 642 | |
| 643 | //@{ |
| 644 | /** Check for inequality, performing a deep compare by following pointers. */ |
| 645 | inline bool operator!=(const VorbisComment &object) const { return Prototype::operator!=(object); } |
| 646 | inline bool operator!=(const ::FLAC__StreamMetadata &object) const { return Prototype::operator!=(object); } |
| 647 | inline bool operator!=(const ::FLAC__StreamMetadata *object) const { return Prototype::operator!=(object); } |
| 648 | //@} |
| 649 | |
| 650 | unsigned get_num_comments() const; |
| 651 | const FLAC__byte *get_vendor_string() const; // NUL-terminated UTF-8 string |
| 652 | Entry get_comment(unsigned index) const; |
| 653 | |
| 654 | //! See FLAC__metadata_object_vorbiscomment_set_vendor_string() |
| 655 | bool set_vendor_string(const FLAC__byte *string); // NUL-terminated UTF-8 string |
| 656 | |
| 657 | //! See FLAC__metadata_object_vorbiscomment_set_comment() |
| 658 | bool set_comment(unsigned index, const Entry &entry); |
| 659 | |
| 660 | //! See FLAC__metadata_object_vorbiscomment_insert_comment() |
| 661 | bool insert_comment(unsigned index, const Entry &entry); |
| 662 | |
| 663 | //! See FLAC__metadata_object_vorbiscomment_append_comment() |
| 664 | bool append_comment(const Entry &entry); |
| 665 | |
| 666 | //! See FLAC__metadata_object_vorbiscomment_delete_comment() |
| 667 | bool delete_comment(unsigned index); |
| 668 | }; |
| 669 | |
| 670 | /** CUESHEET metadata block. |
| 671 | * See the \link flacpp_metadata_object overview \endlink for more, |
| 672 | * and the <A HREF="../format.html#metadata_block_cuesheet">format specification</A>. |
| 673 | */ |
| 674 | class FLACPP_API CueSheet : public Prototype { |
| 675 | public: |
| 676 | /** Convenience class for encapsulating a cue sheet |
| 677 | * track. |
| 678 | * |
| 679 | * Always check is_valid() after the constructor or operator= |
| 680 | * to make sure memory was properly allocated. |
| 681 | */ |
| 682 | class FLACPP_API Track { |
| 683 | protected: |
| 684 | ::FLAC__StreamMetadata_CueSheet_Track *object_; |
| 685 | public: |
| 686 | Track(); |
| 687 | Track(const ::FLAC__StreamMetadata_CueSheet_Track *track); |
| 688 | Track(const Track &track); |
| 689 | Track &operator=(const Track &track); |
| 690 | |
| 691 | virtual ~Track(); |
| 692 | |
| 693 | virtual bool is_valid() const; ///< Returns \c true iff object was properly constructed. |
| 694 | |
| 695 | |
| 696 | inline FLAC__uint64 get_offset() const { return object_->offset; } |
| 697 | inline FLAC__byte get_number() const { return object_->number; } |
| 698 | inline const char *get_isrc() const { return object_->isrc; } |
| 699 | inline unsigned get_type() const { return object_->type; } |
| 700 | inline bool get_pre_emphasis() const { return object_->pre_emphasis; } |
| 701 | |
| 702 | inline FLAC__byte get_num_indices() const { return object_->num_indices; } |
| 703 | ::FLAC__StreamMetadata_CueSheet_Index get_index(unsigned i) const; |
| 704 | |
| 705 | inline const ::FLAC__StreamMetadata_CueSheet_Track *get_track() const { return object_; } |
| 706 | |
| 707 | inline void set_offset(FLAC__uint64 value) { object_->offset = value; } |
| 708 | inline void set_number(FLAC__byte value) { object_->number = value; } |
| 709 | void set_isrc(const char value[12]); |
| 710 | void set_type(unsigned value); |
| 711 | inline void set_pre_emphasis(bool value) { object_->pre_emphasis = value? 1 : 0; } |
| 712 | |
| 713 | void set_index(unsigned i, const ::FLAC__StreamMetadata_CueSheet_Index &index); |
| 714 | //@@@ It's awkward but to insert/delete index points |
| 715 | //@@@ you must use the routines in the CueSheet class. |
| 716 | }; |
| 717 | |
| 718 | CueSheet(); |
| 719 | |
| 720 | //@{ |
| 721 | /** Constructs a copy of the given object. This form |
| 722 | * always performs a deep copy. |
| 723 | */ |
| 724 | inline CueSheet(const CueSheet &object): Prototype(object) { } |
| 725 | inline CueSheet(const ::FLAC__StreamMetadata &object): Prototype(object) { } |
| 726 | inline CueSheet(const ::FLAC__StreamMetadata *object): Prototype(object) { } |
| 727 | //@} |
| 728 | |
| 729 | /** Constructs an object with copy control. See |
| 730 | * Prototype(::FLAC__StreamMetadata *object, bool copy). |
| 731 | */ |
| 732 | inline CueSheet(::FLAC__StreamMetadata *object, bool copy): Prototype(object, copy) { } |
| 733 | |
| 734 | ~CueSheet(); |
| 735 | |
| 736 | //@{ |
| 737 | /** Assign from another object. Always performs a deep copy. */ |
| 738 | inline CueSheet &operator=(const CueSheet &object) { Prototype::operator=(object); return *this; } |
| 739 | inline CueSheet &operator=(const ::FLAC__StreamMetadata &object) { Prototype::operator=(object); return *this; } |
| 740 | inline CueSheet &operator=(const ::FLAC__StreamMetadata *object) { Prototype::operator=(object); return *this; } |
| 741 | //@} |
| 742 | |
| 743 | /** Assigns an object with copy control. See |
| 744 | * Prototype::assign_object(::FLAC__StreamMetadata *object, bool copy). |
| 745 | */ |
| 746 | inline CueSheet &assign(::FLAC__StreamMetadata *object, bool copy) { Prototype::assign_object(object, copy); return *this; } |
| 747 | |
| 748 | //@{ |
| 749 | /** Check for equality, performing a deep compare by following pointers. */ |
| 750 | inline bool operator==(const CueSheet &object) const { return Prototype::operator==(object); } |
| 751 | inline bool operator==(const ::FLAC__StreamMetadata &object) const { return Prototype::operator==(object); } |
| 752 | inline bool operator==(const ::FLAC__StreamMetadata *object) const { return Prototype::operator==(object); } |
| 753 | //@} |
| 754 | |
| 755 | //@{ |
| 756 | /** Check for inequality, performing a deep compare by following pointers. */ |
| 757 | inline bool operator!=(const CueSheet &object) const { return Prototype::operator!=(object); } |
| 758 | inline bool operator!=(const ::FLAC__StreamMetadata &object) const { return Prototype::operator!=(object); } |
| 759 | inline bool operator!=(const ::FLAC__StreamMetadata *object) const { return Prototype::operator!=(object); } |
| 760 | //@} |
| 761 | |
| 762 | const char *get_media_catalog_number() const; |
| 763 | FLAC__uint64 get_lead_in() const; |
| 764 | bool get_is_cd() const; |
| 765 | |
| 766 | unsigned get_num_tracks() const; |
| 767 | Track get_track(unsigned i) const; |
| 768 | |
| 769 | void set_media_catalog_number(const char value[128]); |
| 770 | void set_lead_in(FLAC__uint64 value); |
| 771 | void set_is_cd(bool value); |
| 772 | |
| 773 | void set_index(unsigned track_num, unsigned index_num, const ::FLAC__StreamMetadata_CueSheet_Index &index); |
| 774 | |
| 775 | //! See FLAC__metadata_object_cuesheet_track_insert_index() |
| 776 | bool insert_index(unsigned track_num, unsigned index_num, const ::FLAC__StreamMetadata_CueSheet_Index &index); |
| 777 | |
| 778 | //! See FLAC__metadata_object_cuesheet_track_delete_index() |
| 779 | bool delete_index(unsigned track_num, unsigned index_num); |
| 780 | |
| 781 | //! See FLAC__metadata_object_cuesheet_set_track() |
| 782 | bool set_track(unsigned i, const Track &track); |
| 783 | |
| 784 | //! See FLAC__metadata_object_cuesheet_insert_track() |
| 785 | bool insert_track(unsigned i, const Track &track); |
| 786 | |
| 787 | //! See FLAC__metadata_object_cuesheet_delete_track() |
| 788 | bool delete_track(unsigned i); |
| 789 | |
| 790 | //! See FLAC__metadata_object_cuesheet_is_legal() |
| 791 | bool is_legal(bool check_cd_da_subset = false, const char **violation = 0) const; |
| 792 | |
| 793 | //! See FLAC__metadata_object_cuesheet_calculate_cddb_id() |
| 794 | FLAC__uint32 calculate_cddb_id() const; |
| 795 | }; |
| 796 | |
| 797 | /** PICTURE metadata block. |
| 798 | * See the \link flacpp_metadata_object overview \endlink for more, |
| 799 | * and the <A HREF="../format.html#metadata_block_picture">format specification</A>. |
| 800 | */ |
| 801 | class FLACPP_API Picture : public Prototype { |
| 802 | public: |
| 803 | Picture(); |
| 804 | |
| 805 | //@{ |
| 806 | /** Constructs a copy of the given object. This form |
| 807 | * always performs a deep copy. |
| 808 | */ |
| 809 | inline Picture(const Picture &object): Prototype(object) { } |
| 810 | inline Picture(const ::FLAC__StreamMetadata &object): Prototype(object) { } |
| 811 | inline Picture(const ::FLAC__StreamMetadata *object): Prototype(object) { } |
| 812 | //@} |
| 813 | |
| 814 | /** Constructs an object with copy control. See |
| 815 | * Prototype(::FLAC__StreamMetadata *object, bool copy). |
| 816 | */ |
| 817 | inline Picture(::FLAC__StreamMetadata *object, bool copy): Prototype(object, copy) { } |
| 818 | |
| 819 | ~Picture(); |
| 820 | |
| 821 | //@{ |
| 822 | /** Assign from another object. Always performs a deep copy. */ |
| 823 | inline Picture &operator=(const Picture &object) { Prototype::operator=(object); return *this; } |
| 824 | inline Picture &operator=(const ::FLAC__StreamMetadata &object) { Prototype::operator=(object); return *this; } |
| 825 | inline Picture &operator=(const ::FLAC__StreamMetadata *object) { Prototype::operator=(object); return *this; } |
| 826 | //@} |
| 827 | |
| 828 | /** Assigns an object with copy control. See |
| 829 | * Prototype::assign_object(::FLAC__StreamMetadata *object, bool copy). |
| 830 | */ |
| 831 | inline Picture &assign(::FLAC__StreamMetadata *object, bool copy) { Prototype::assign_object(object, copy); return *this; } |
| 832 | |
| 833 | //@{ |
| 834 | /** Check for equality, performing a deep compare by following pointers. */ |
| 835 | inline bool operator==(const Picture &object) const { return Prototype::operator==(object); } |
| 836 | inline bool operator==(const ::FLAC__StreamMetadata &object) const { return Prototype::operator==(object); } |
| 837 | inline bool operator==(const ::FLAC__StreamMetadata *object) const { return Prototype::operator==(object); } |
| 838 | //@} |
| 839 | |
| 840 | //@{ |
| 841 | /** Check for inequality, performing a deep compare by following pointers. */ |
| 842 | inline bool operator!=(const Picture &object) const { return Prototype::operator!=(object); } |
| 843 | inline bool operator!=(const ::FLAC__StreamMetadata &object) const { return Prototype::operator!=(object); } |
| 844 | inline bool operator!=(const ::FLAC__StreamMetadata *object) const { return Prototype::operator!=(object); } |
| 845 | //@} |
| 846 | |
| 847 | ::FLAC__StreamMetadata_Picture_Type get_type() const; |
| 848 | const char *get_mime_type() const; // NUL-terminated printable ASCII string |
| 849 | const FLAC__byte *get_description() const; // NUL-terminated UTF-8 string |
| 850 | FLAC__uint32 get_width() const; |
| 851 | FLAC__uint32 get_height() const; |
| 852 | FLAC__uint32 get_depth() const; |
| 853 | FLAC__uint32 get_colors() const; ///< a return value of \c 0 means true-color, i.e. 2^depth colors |
| 854 | FLAC__uint32 get_data_length() const; |
| 855 | const FLAC__byte *get_data() const; |
| 856 | |
| 857 | void set_type(::FLAC__StreamMetadata_Picture_Type type); |
| 858 | |
| 859 | //! See FLAC__metadata_object_picture_set_mime_type() |
| 860 | bool set_mime_type(const char *string); // NUL-terminated printable ASCII string |
| 861 | |
| 862 | //! See FLAC__metadata_object_picture_set_description() |
| 863 | bool set_description(const FLAC__byte *string); // NUL-terminated UTF-8 string |
| 864 | |
| 865 | void set_width(FLAC__uint32 value) const; |
| 866 | void set_height(FLAC__uint32 value) const; |
| 867 | void set_depth(FLAC__uint32 value) const; |
| 868 | void set_colors(FLAC__uint32 value) const; ///< a value of \c 0 means true-color, i.e. 2^depth colors |
| 869 | |
| 870 | //! See FLAC__metadata_object_picture_set_data() |
| 871 | bool set_data(const FLAC__byte *data, FLAC__uint32 data_length); |
| 872 | }; |
| 873 | |
| 874 | /** Opaque metadata block for storing unknown types. |
| 875 | * This should not be used unless you know what you are doing; |
| 876 | * it is currently used only internally to support forward |
| 877 | * compatibility of metadata blocks. |
| 878 | * See the \link flacpp_metadata_object overview \endlink for more, |
| 879 | */ |
| 880 | class FLACPP_API Unknown : public Prototype { |
| 881 | public: |
| 882 | Unknown(); |
| 883 | // |
| 884 | //@{ |
| 885 | /** Constructs a copy of the given object. This form |
| 886 | * always performs a deep copy. |
| 887 | */ |
| 888 | inline Unknown(const Unknown &object): Prototype(object) { } |
| 889 | inline Unknown(const ::FLAC__StreamMetadata &object): Prototype(object) { } |
| 890 | inline Unknown(const ::FLAC__StreamMetadata *object): Prototype(object) { } |
| 891 | //@} |
| 892 | |
| 893 | /** Constructs an object with copy control. See |
| 894 | * Prototype(::FLAC__StreamMetadata *object, bool copy). |
| 895 | */ |
| 896 | inline Unknown(::FLAC__StreamMetadata *object, bool copy): Prototype(object, copy) { } |
| 897 | |
| 898 | ~Unknown(); |
| 899 | |
| 900 | //@{ |
| 901 | /** Assign from another object. Always performs a deep copy. */ |
| 902 | inline Unknown &operator=(const Unknown &object) { Prototype::operator=(object); return *this; } |
| 903 | inline Unknown &operator=(const ::FLAC__StreamMetadata &object) { Prototype::operator=(object); return *this; } |
| 904 | inline Unknown &operator=(const ::FLAC__StreamMetadata *object) { Prototype::operator=(object); return *this; } |
| 905 | //@} |
| 906 | |
| 907 | /** Assigns an object with copy control. See |
| 908 | * Prototype::assign_object(::FLAC__StreamMetadata *object, bool copy). |
| 909 | */ |
| 910 | inline Unknown &assign(::FLAC__StreamMetadata *object, bool copy) { Prototype::assign_object(object, copy); return *this; } |
| 911 | |
| 912 | //@{ |
| 913 | /** Check for equality, performing a deep compare by following pointers. */ |
| 914 | inline bool operator==(const Unknown &object) const { return Prototype::operator==(object); } |
| 915 | inline bool operator==(const ::FLAC__StreamMetadata &object) const { return Prototype::operator==(object); } |
| 916 | inline bool operator==(const ::FLAC__StreamMetadata *object) const { return Prototype::operator==(object); } |
| 917 | //@} |
| 918 | |
| 919 | //@{ |
| 920 | /** Check for inequality, performing a deep compare by following pointers. */ |
| 921 | inline bool operator!=(const Unknown &object) const { return Prototype::operator!=(object); } |
| 922 | inline bool operator!=(const ::FLAC__StreamMetadata &object) const { return Prototype::operator!=(object); } |
| 923 | inline bool operator!=(const ::FLAC__StreamMetadata *object) const { return Prototype::operator!=(object); } |
| 924 | //@} |
| 925 | |
| 926 | const FLAC__byte *get_data() const; |
| 927 | |
| 928 | //! This form always copies \a data |
| 929 | bool set_data(const FLAC__byte *data, unsigned length); |
| 930 | bool set_data(FLAC__byte *data, unsigned length, bool copy); |
| 931 | }; |
| 932 | |
| 933 | /* \} */ |
| 934 | |
| 935 | |
| 936 | /** \defgroup flacpp_metadata_level0 FLAC++/metadata.h: metadata level 0 interface |
| 937 | * \ingroup flacpp_metadata |
| 938 | * |
| 939 | * \brief |
| 940 | * Level 0 metadata iterators. |
| 941 | * |
| 942 | * See the \link flac_metadata_level0 C layer equivalent \endlink |
| 943 | * for more. |
| 944 | * |
| 945 | * \{ |
| 946 | */ |
| 947 | |
| 948 | FLACPP_API bool get_streaminfo(const char *filename, StreamInfo &streaminfo); ///< See FLAC__metadata_get_streaminfo(). |
| 949 | |
| 950 | FLACPP_API bool get_tags(const char *filename, VorbisComment *&tags); ///< See FLAC__metadata_get_tags(). |
| 951 | FLACPP_API bool get_tags(const char *filename, VorbisComment &tags); ///< See FLAC__metadata_get_tags(). |
| 952 | |
| 953 | FLACPP_API bool get_cuesheet(const char *filename, CueSheet *&cuesheet); ///< See FLAC__metadata_get_cuesheet(). |
| 954 | FLACPP_API bool get_cuesheet(const char *filename, CueSheet &cuesheet); ///< See FLAC__metadata_get_cuesheet(). |
| 955 | |
| 956 | FLACPP_API bool get_picture(const char *filename, Picture *&picture, ::FLAC__StreamMetadata_Picture_Type type, const char *mime_type, const FLAC__byte *description, unsigned max_width, unsigned max_height, unsigned max_depth, unsigned max_colors); ///< See FLAC__metadata_get_picture(). |
| 957 | FLACPP_API bool get_picture(const char *filename, Picture &picture, ::FLAC__StreamMetadata_Picture_Type type, const char *mime_type, const FLAC__byte *description, unsigned max_width, unsigned max_height, unsigned max_depth, unsigned max_colors); ///< See FLAC__metadata_get_picture(). |
| 958 | |
| 959 | /* \} */ |
| 960 | |
| 961 | |
| 962 | /** \defgroup flacpp_metadata_level1 FLAC++/metadata.h: metadata level 1 interface |
| 963 | * \ingroup flacpp_metadata |
| 964 | * |
| 965 | * \brief |
| 966 | * Level 1 metadata iterator. |
| 967 | * |
| 968 | * The flow through the iterator in the C++ layer is similar |
| 969 | * to the C layer: |
| 970 | * - Create a SimpleIterator instance |
| 971 | * - Check SimpleIterator::is_valid() |
| 972 | * - Call SimpleIterator::init() and check the return |
| 973 | * - Traverse and/or edit. Edits are written to file |
| 974 | * immediately. |
| 975 | * - Destroy the SimpleIterator instance |
| 976 | * |
| 977 | * The ownership of pointers in the C++ layer follows that in |
| 978 | * the C layer, i.e. |
| 979 | * - The objects returned by get_block() are yours to |
| 980 | * modify, but changes are not reflected in the FLAC file |
| 981 | * until you call set_block(). The objects are also |
| 982 | * yours to delete; they are not automatically deleted |
| 983 | * when passed to set_block() or insert_block_after(). |
| 984 | * |
| 985 | * See the \link flac_metadata_level1 C layer equivalent \endlink |
| 986 | * for more. |
| 987 | * |
| 988 | * \{ |
| 989 | */ |
| 990 | |
| 991 | /** This class is a wrapper around the FLAC__metadata_simple_iterator |
| 992 | * structures and methods; see the |
| 993 | * \link flacpp_metadata_level1 usage guide \endlink and |
| 994 | * ::FLAC__Metadata_SimpleIterator. |
| 995 | */ |
| 996 | class FLACPP_API SimpleIterator { |
| 997 | public: |
| 998 | /** This class is a wrapper around FLAC__Metadata_SimpleIteratorStatus. |
| 999 | */ |
| 1000 | class FLACPP_API Status { |
| 1001 | public: |
| 1002 | inline Status(::FLAC__Metadata_SimpleIteratorStatus status): status_(status) { } |
| 1003 | inline operator ::FLAC__Metadata_SimpleIteratorStatus() const { return status_; } |
| 1004 | inline const char *as_cstring() const { return ::FLAC__Metadata_SimpleIteratorStatusString[status_]; } |
| 1005 | protected: |
| 1006 | ::FLAC__Metadata_SimpleIteratorStatus status_; |
| 1007 | }; |
| 1008 | |
| 1009 | SimpleIterator(); |
| 1010 | virtual ~SimpleIterator(); |
| 1011 | |
| 1012 | bool is_valid() const; ///< Returns \c true iff object was properly constructed. |
| 1013 | |
| 1014 | bool init(const char *filename, bool read_only, bool preserve_file_stats); ///< See FLAC__metadata_simple_iterator_init(). |
| 1015 | |
| 1016 | Status status(); ///< See FLAC__metadata_simple_iterator_status(). |
| 1017 | bool is_writable() const; ///< See FLAC__metadata_simple_iterator_is_writable(). |
| 1018 | |
| 1019 | bool next(); ///< See FLAC__metadata_simple_iterator_next(). |
| 1020 | bool prev(); ///< See FLAC__metadata_simple_iterator_prev(). |
| 1021 | bool is_last() const; ///< See FLAC__metadata_simple_iterator_is_last(). |
| 1022 | |
| 1023 | off_t get_block_offset() const; ///< See FLAC__metadata_simple_iterator_get_block_offset(). |
| 1024 | ::FLAC__MetadataType get_block_type() const; ///< See FLAC__metadata_simple_iterator_get_block_type(). |
| 1025 | unsigned get_block_length() const; ///< See FLAC__metadata_simple_iterator_get_block_length(). |
| 1026 | bool get_application_id(FLAC__byte *id); ///< See FLAC__metadata_simple_iterator_get_application_id(). |
| 1027 | Prototype *get_block(); ///< See FLAC__metadata_simple_iterator_get_block(). |
| 1028 | bool set_block(Prototype *block, bool use_padding = true); ///< See FLAC__metadata_simple_iterator_set_block(). |
| 1029 | bool insert_block_after(Prototype *block, bool use_padding = true); ///< See FLAC__metadata_simple_iterator_insert_block_after(). |
| 1030 | bool delete_block(bool use_padding = true); ///< See FLAC__metadata_simple_iterator_delete_block(). |
| 1031 | |
| 1032 | protected: |
| 1033 | ::FLAC__Metadata_SimpleIterator *iterator_; |
| 1034 | void clear(); |
| 1035 | }; |
| 1036 | |
| 1037 | /* \} */ |
| 1038 | |
| 1039 | |
| 1040 | /** \defgroup flacpp_metadata_level2 FLAC++/metadata.h: metadata level 2 interface |
| 1041 | * \ingroup flacpp_metadata |
| 1042 | * |
| 1043 | * \brief |
| 1044 | * Level 2 metadata iterator. |
| 1045 | * |
| 1046 | * The flow through the iterator in the C++ layer is similar |
| 1047 | * to the C layer: |
| 1048 | * - Create a Chain instance |
| 1049 | * - Check Chain::is_valid() |
| 1050 | * - Call Chain::read() and check the return |
| 1051 | * - Traverse and/or edit with an Iterator or with |
| 1052 | * Chain::merge_padding() or Chain::sort_padding() |
| 1053 | * - Write changes back to FLAC file with Chain::write() |
| 1054 | * - Destroy the Chain instance |
| 1055 | * |
| 1056 | * The ownership of pointers in the C++ layer is slightly |
| 1057 | * different than in the C layer, i.e. |
| 1058 | * - The objects returned by Iterator::get_block() are NOT |
| 1059 | * owned by the iterator and should be deleted by the |
| 1060 | * caller when finished, BUT, when you modify the block, |
| 1061 | * it will directly edit what's in the chain and you do |
| 1062 | * not need to call Iterator::set_block(). However the |
| 1063 | * changes will not be reflected in the FLAC file until |
| 1064 | * the chain is written with Chain::write(). |
| 1065 | * - When you pass an object to Iterator::set_block(), |
| 1066 | * Iterator::insert_block_before(), or |
| 1067 | * Iterator::insert_block_after(), the iterator takes |
| 1068 | * ownership of the block and it will be deleted by the |
| 1069 | * chain. |
| 1070 | * |
| 1071 | * See the \link flac_metadata_level2 C layer equivalent \endlink |
| 1072 | * for more. |
| 1073 | * |
| 1074 | * \{ |
| 1075 | */ |
| 1076 | |
| 1077 | /** This class is a wrapper around the FLAC__metadata_chain |
| 1078 | * structures and methods; see the |
| 1079 | * \link flacpp_metadata_level2 usage guide \endlink and |
| 1080 | * ::FLAC__Metadata_Chain. |
| 1081 | */ |
| 1082 | class FLACPP_API Chain { |
| 1083 | public: |
| 1084 | /** This class is a wrapper around FLAC__Metadata_ChainStatus. |
| 1085 | */ |
| 1086 | class FLACPP_API Status { |
| 1087 | public: |
| 1088 | inline Status(::FLAC__Metadata_ChainStatus status): status_(status) { } |
| 1089 | inline operator ::FLAC__Metadata_ChainStatus() const { return status_; } |
| 1090 | inline const char *as_cstring() const { return ::FLAC__Metadata_ChainStatusString[status_]; } |
| 1091 | protected: |
| 1092 | ::FLAC__Metadata_ChainStatus status_; |
| 1093 | }; |
| 1094 | |
| 1095 | Chain(); |
| 1096 | virtual ~Chain(); |
| 1097 | |
| 1098 | friend class Iterator; |
| 1099 | |
| 1100 | bool is_valid() const; ///< Returns \c true iff object was properly constructed. |
| 1101 | |
| 1102 | Status status(); ///< See FLAC__metadata_chain_status(). |
| 1103 | |
| 1104 | bool read(const char *filename, bool is_ogg = false); ///< See FLAC__metadata_chain_read(), FLAC__metadata_chain_read_ogg(). |
| 1105 | bool read(FLAC__IOHandle handle, FLAC__IOCallbacks callbacks, bool is_ogg = false); ///< See FLAC__metadata_chain_read_with_callbacks(), FLAC__metadata_chain_read_ogg_with_callbacks(). |
| 1106 | |
| 1107 | bool check_if_tempfile_needed(bool use_padding); ///< See FLAC__metadata_chain_check_if_tempfile_needed(). |
| 1108 | |
| 1109 | bool write(bool use_padding = true, bool preserve_file_stats = false); ///< See FLAC__metadata_chain_write(). |
| 1110 | bool write(bool use_padding, ::FLAC__IOHandle handle, ::FLAC__IOCallbacks callbacks); ///< See FLAC__metadata_chain_write_with_callbacks(). |
| 1111 | bool write(bool use_padding, ::FLAC__IOHandle handle, ::FLAC__IOCallbacks callbacks, ::FLAC__IOHandle temp_handle, ::FLAC__IOCallbacks temp_callbacks); ///< See FLAC__metadata_chain_write_with_callbacks_and_tempfile(). |
| 1112 | |
| 1113 | void merge_padding(); ///< See FLAC__metadata_chain_merge_padding(). |
| 1114 | void sort_padding(); ///< See FLAC__metadata_chain_sort_padding(). |
| 1115 | |
| 1116 | protected: |
| 1117 | ::FLAC__Metadata_Chain *chain_; |
| 1118 | virtual void clear(); |
| 1119 | }; |
| 1120 | |
| 1121 | /** This class is a wrapper around the FLAC__metadata_iterator |
| 1122 | * structures and methods; see the |
| 1123 | * \link flacpp_metadata_level2 usage guide \endlink and |
| 1124 | * ::FLAC__Metadata_Iterator. |
| 1125 | */ |
| 1126 | class FLACPP_API Iterator { |
| 1127 | public: |
| 1128 | Iterator(); |
| 1129 | virtual ~Iterator(); |
| 1130 | |
| 1131 | bool is_valid() const; ///< Returns \c true iff object was properly constructed. |
| 1132 | |
| 1133 | |
| 1134 | void init(Chain &chain); ///< See FLAC__metadata_iterator_init(). |
| 1135 | |
| 1136 | bool next(); ///< See FLAC__metadata_iterator_next(). |
| 1137 | bool prev(); ///< See FLAC__metadata_iterator_prev(). |
| 1138 | |
| 1139 | ::FLAC__MetadataType get_block_type() const; ///< See FLAC__metadata_iterator_get_block_type(). |
| 1140 | Prototype *get_block(); ///< See FLAC__metadata_iterator_get_block(). |
| 1141 | bool set_block(Prototype *block); ///< See FLAC__metadata_iterator_set_block(). |
| 1142 | bool delete_block(bool replace_with_padding); ///< See FLAC__metadata_iterator_delete_block(). |
| 1143 | bool insert_block_before(Prototype *block); ///< See FLAC__metadata_iterator_insert_block_before(). |
| 1144 | bool insert_block_after(Prototype *block); ///< See FLAC__metadata_iterator_insert_block_after(). |
| 1145 | |
| 1146 | protected: |
| 1147 | ::FLAC__Metadata_Iterator *iterator_; |
| 1148 | virtual void clear(); |
| 1149 | }; |
| 1150 | |
| 1151 | /* \} */ |
| 1152 | |
| 1153 | } |
| 1154 | } |
| 1155 | |
| 1156 | #endif |
src/lib/libflac/libflac/bitreader.c
| r0 | r14135 | |
| 1 | /* libFLAC - Free Lossless Audio Codec library |
| 2 | * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * |
| 8 | * - Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * - Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * - Neither the name of the Xiph.org Foundation nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived from |
| 17 | * this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 26 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 27 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #if HAVE_CONFIG_H |
| 33 | # include <config.h> |
| 34 | #endif |
| 35 | |
| 36 | |
| 37 | #include <stdlib.h> /* for malloc() */ |
| 38 | #include <string.h> /* for memcpy(), memset() */ |
| 39 | |
| 40 | #include "private/bitmath.h" |
| 41 | #include "private/bitreader.h" |
| 42 | #include "private/crc.h" |
| 43 | #include "flac/assert.h" |
| 44 | |
| 45 | /* Things should be fastest when this matches the machine word size */ |
| 46 | /* WATCHOUT: if you change this you must also change the following #defines down to COUNT_ZERO_MSBS below to match */ |
| 47 | /* WATCHOUT: there are a few places where the code will not work unless brword is >= 32 bits wide */ |
| 48 | /* also, some sections currently only have fast versions for 4 or 8 bytes per word */ |
| 49 | typedef FLAC__uint32 brword; |
| 50 | #define FLAC__BYTES_PER_WORD 4 |
| 51 | #define FLAC__BITS_PER_WORD 32 |
| 52 | #define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff) |
| 53 | /* SWAP_BE_WORD_TO_HOST swaps bytes in a brword (which is always big-endian) if necessary to match host byte order */ |
| 54 | #if WORDS_BIGENDIAN |
| 55 | #define SWAP_BE_WORD_TO_HOST(x) (x) |
| 56 | #else |
| 57 | #ifdef _MSC_VER |
| 58 | #define SWAP_BE_WORD_TO_HOST(x) local_swap32_(x) |
| 59 | #else |
| 60 | #define SWAP_BE_WORD_TO_HOST(x) local_swap32_(x) |
| 61 | #endif |
| 62 | #endif |
| 63 | /* counts the # of zero MSBs in a word */ |
| 64 | #define COUNT_ZERO_MSBS(word) ( \ |
| 65 | (word) <= 0xffff ? \ |
| 66 | ( (word) <= 0xff? byte_to_unary_table[word] + 24 : byte_to_unary_table[(word) >> 8] + 16 ) : \ |
| 67 | ( (word) <= 0xffffff? byte_to_unary_table[word >> 16] + 8 : byte_to_unary_table[(word) >> 24] ) \ |
| 68 | ) |
| 69 | /* this alternate might be slightly faster on some systems/compilers: */ |
| 70 | #define COUNT_ZERO_MSBS2(word) ( (word) <= 0xff ? byte_to_unary_table[word] + 24 : ((word) <= 0xffff ? byte_to_unary_table[(word) >> 8] + 16 : ((word) <= 0xffffff ? byte_to_unary_table[(word) >> 16] + 8 : byte_to_unary_table[(word) >> 24])) ) |
| 71 | |
| 72 | |
| 73 | /* |
| 74 | * This should be at least twice as large as the largest number of words |
| 75 | * required to represent any 'number' (in any encoding) you are going to |
| 76 | * read. With FLAC this is on the order of maybe a few hundred bits. |
| 77 | * If the buffer is smaller than that, the decoder won't be able to read |
| 78 | * in a whole number that is in a variable length encoding (e.g. Rice). |
| 79 | * But to be practical it should be at least 1K bytes. |
| 80 | * |
| 81 | * Increase this number to decrease the number of read callbacks, at the |
| 82 | * expense of using more memory. Or decrease for the reverse effect, |
| 83 | * keeping in mind the limit from the first paragraph. The optimal size |
| 84 | * also depends on the CPU cache size and other factors; some twiddling |
| 85 | * may be necessary to squeeze out the best performance. |
| 86 | */ |
| 87 | static const unsigned FLAC__BITREADER_DEFAULT_CAPACITY = 65536u / FLAC__BITS_PER_WORD; /* in words */ |
| 88 | |
| 89 | static const unsigned char byte_to_unary_table[] = { |
| 90 | 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, |
| 91 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 92 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 93 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 94 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 95 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 96 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 97 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 98 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 99 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 100 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 101 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 102 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 103 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 104 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 105 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 106 | }; |
| 107 | |
| 108 | #ifdef min |
| 109 | #undef min |
| 110 | #endif |
| 111 | #define min(x,y) ((x)<(y)?(x):(y)) |
| 112 | #ifdef max |
| 113 | #undef max |
| 114 | #endif |
| 115 | #define max(x,y) ((x)>(y)?(x):(y)) |
| 116 | |
| 117 | /* adjust for compilers that can't understand using LLU suffix for uint64_t literals */ |
| 118 | #ifdef _MSC_VER |
| 119 | #define FLAC__U64L(x) x |
| 120 | #else |
| 121 | #define FLAC__U64L(x) x##LLU |
| 122 | #endif |
| 123 | |
| 124 | #ifndef FLaC__INLINE |
| 125 | #define FLaC__INLINE |
| 126 | #endif |
| 127 | |
| 128 | /* WATCHOUT: assembly routines rely on the order in which these fields are declared */ |
| 129 | struct FLAC__BitReader { |
| 130 | /* any partially-consumed word at the head will stay right-justified as bits are consumed from the left */ |
| 131 | /* any incomplete word at the tail will be left-justified, and bytes from the read callback are added on the right */ |
| 132 | brword *buffer; |
| 133 | unsigned capacity; /* in words */ |
| 134 | unsigned words; /* # of completed words in buffer */ |
| 135 | unsigned bytes; /* # of bytes in incomplete word at buffer[words] */ |
| 136 | unsigned consumed_words; /* #words ... */ |
| 137 | unsigned consumed_bits; /* ... + (#bits of head word) already consumed from the front of buffer */ |
| 138 | unsigned read_crc16; /* the running frame CRC */ |
| 139 | unsigned crc16_align; /* the number of bits in the current consumed word that should not be CRC'd */ |
| 140 | FLAC__BitReaderReadCallback read_callback; |
| 141 | void *client_data; |
| 142 | FLAC__CPUInfo cpu_info; |
| 143 | }; |
| 144 | |
| 145 | |
| 146 | static FLAC__uint32 local_swap32_(FLAC__uint32 x) |
| 147 | { |
| 148 | x = ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF); |
| 149 | return (x>>16) | (x<<16); |
| 150 | } |
| 151 | |
| 152 | #ifdef _MSC_VER |
| 153 | /* OPT: an MSVC built-in would be better */ |
| 154 | static void local_swap32_block_(FLAC__uint32 *start, FLAC__uint32 len) |
| 155 | { |
| 156 | __asm { |
| 157 | mov edx, start |
| 158 | mov ecx, len |
| 159 | test ecx, ecx |
| 160 | loop1: |
| 161 | jz done1 |
| 162 | mov eax, [edx] |
| 163 | bswap eax |
| 164 | mov [edx], eax |
| 165 | add edx, 4 |
| 166 | dec ecx |
| 167 | jmp short loop1 |
| 168 | done1: |
| 169 | } |
| 170 | } |
| 171 | #endif |
| 172 | |
| 173 | static FLaC__INLINE void crc16_update_word_(FLAC__BitReader *br, brword word) |
| 174 | { |
| 175 | register unsigned crc = br->read_crc16; |
| 176 | #if FLAC__BYTES_PER_WORD == 4 |
| 177 | switch(br->crc16_align) { |
| 178 | case 0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 24), crc); |
| 179 | case 8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc); |
| 180 | case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc); |
| 181 | case 24: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc); |
| 182 | } |
| 183 | #elif FLAC__BYTES_PER_WORD == 8 |
| 184 | switch(br->crc16_align) { |
| 185 | case 0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 56), crc); |
| 186 | case 8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 48) & 0xff), crc); |
| 187 | case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 40) & 0xff), crc); |
| 188 | case 24: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 32) & 0xff), crc); |
| 189 | case 32: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 24) & 0xff), crc); |
| 190 | case 40: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc); |
| 191 | case 48: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc); |
| 192 | case 56: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc); |
| 193 | } |
| 194 | #else |
| 195 | for( ; br->crc16_align < FLAC__BITS_PER_WORD; br->crc16_align += 8) |
| 196 | crc = FLAC__CRC16_UPDATE((unsigned)((word >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), crc); |
| 197 | br->read_crc16 = crc; |
| 198 | #endif |
| 199 | br->crc16_align = 0; |
| 200 | } |
| 201 | |
| 202 | /* would be static except it needs to be called by asm routines */ |
| 203 | FLAC__bool bitreader_read_from_client_(FLAC__BitReader *br) |
| 204 | { |
| 205 | unsigned start, end; |
| 206 | size_t bytes; |
| 207 | FLAC__byte *target; |
| 208 | |
| 209 | /* first shift the unconsumed buffer data toward the front as much as possible */ |
| 210 | if(br->consumed_words > 0) { |
| 211 | start = br->consumed_words; |
| 212 | end = br->words + (br->bytes? 1:0); |
| 213 | memmove(br->buffer, br->buffer+start, FLAC__BYTES_PER_WORD * (end - start)); |
| 214 | |
| 215 | br->words -= start; |
| 216 | br->consumed_words = 0; |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * set the target for reading, taking into account word alignment and endianness |
| 221 | */ |
| 222 | bytes = (br->capacity - br->words) * FLAC__BYTES_PER_WORD - br->bytes; |
| 223 | if(bytes == 0) |
| 224 | return false; /* no space left, buffer is too small; see note for FLAC__BITREADER_DEFAULT_CAPACITY */ |
| 225 | target = ((FLAC__byte*)(br->buffer+br->words)) + br->bytes; |
| 226 | |
| 227 | /* before reading, if the existing reader looks like this (say brword is 32 bits wide) |
| 228 | * bitstream : 11 22 33 44 55 br->words=1 br->bytes=1 (partial tail word is left-justified) |
| 229 | * buffer[BE]: 11 22 33 44 55 ?? ?? ?? (shown layed out as bytes sequentially in memory) |
| 230 | * buffer[LE]: 44 33 22 11 ?? ?? ?? 55 (?? being don't-care) |
| 231 | * ^^-------target, bytes=3 |
| 232 | * on LE machines, have to byteswap the odd tail word so nothing is |
| 233 | * overwritten: |
| 234 | */ |
| 235 | #if WORDS_BIGENDIAN |
| 236 | #else |
| 237 | if(br->bytes) |
| 238 | br->buffer[br->words] = SWAP_BE_WORD_TO_HOST(br->buffer[br->words]); |
| 239 | #endif |
| 240 | |
| 241 | /* now it looks like: |
| 242 | * bitstream : 11 22 33 44 55 br->words=1 br->bytes=1 |
| 243 | * buffer[BE]: 11 22 33 44 55 ?? ?? ?? |
| 244 | * buffer[LE]: 44 33 22 11 55 ?? ?? ?? |
| 245 | * ^^-------target, bytes=3 |
| 246 | */ |
| 247 | |
| 248 | /* read in the data; note that the callback may return a smaller number of bytes */ |
| 249 | if(!br->read_callback(target, &bytes, br->client_data)) |
| 250 | return false; |
| 251 | |
| 252 | /* after reading bytes 66 77 88 99 AA BB CC DD EE FF from the client: |
| 253 | * bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF |
| 254 | * buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ?? |
| 255 | * buffer[LE]: 44 33 22 11 55 66 77 88 99 AA BB CC DD EE FF ?? |
| 256 | * now have to byteswap on LE machines: |
| 257 | */ |
| 258 | #if WORDS_BIGENDIAN |
| 259 | #else |
| 260 | end = (br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes + (FLAC__BYTES_PER_WORD-1)) / FLAC__BYTES_PER_WORD; |
| 261 | # if defined(_MSC_VER) && (FLAC__BYTES_PER_WORD == 4) |
| 262 | if(br->cpu_info.type == FLAC__CPUINFO_TYPE_IA32 && br->cpu_info.data.ia32.bswap) { |
| 263 | start = br->words; |
| 264 | local_swap32_block_(br->buffer + start, end - start); |
| 265 | } |
| 266 | else |
| 267 | # endif |
| 268 | for(start = br->words; start < end; start++) |
| 269 | br->buffer[start] = SWAP_BE_WORD_TO_HOST(br->buffer[start]); |
| 270 | #endif |
| 271 | |
| 272 | /* now it looks like: |
| 273 | * bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF |
| 274 | * buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ?? |
| 275 | * buffer[LE]: 44 33 22 11 88 77 66 55 CC BB AA 99 ?? FF EE DD |
| 276 | * finally we'll update the reader values: |
| 277 | */ |
| 278 | end = br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes; |
| 279 | br->words = end / FLAC__BYTES_PER_WORD; |
| 280 | br->bytes = end % FLAC__BYTES_PER_WORD; |
| 281 | |
| 282 | return true; |
| 283 | } |
| 284 | |
| 285 | /*********************************************************************** |
| 286 | * |
| 287 | * Class constructor/destructor |
| 288 | * |
| 289 | ***********************************************************************/ |
| 290 | |
| 291 | FLAC__BitReader *FLAC__bitreader_new(void) |
| 292 | { |
| 293 | FLAC__BitReader *br = (FLAC__BitReader*)calloc(1, sizeof(FLAC__BitReader)); |
| 294 | |
| 295 | /* calloc() implies: |
| 296 | memset(br, 0, sizeof(FLAC__BitReader)); |
| 297 | br->buffer = 0; |
| 298 | br->capacity = 0; |
| 299 | br->words = br->bytes = 0; |
| 300 | br->consumed_words = br->consumed_bits = 0; |
| 301 | br->read_callback = 0; |
| 302 | br->client_data = 0; |
| 303 | */ |
| 304 | return br; |
| 305 | } |
| 306 | |
| 307 | void FLAC__bitreader_delete(FLAC__BitReader *br) |
| 308 | { |
| 309 | FLAC__ASSERT(0 != br); |
| 310 | |
| 311 | FLAC__bitreader_free(br); |
| 312 | free(br); |
| 313 | } |
| 314 | |
| 315 | /*********************************************************************** |
| 316 | * |
| 317 | * Public class methods |
| 318 | * |
| 319 | ***********************************************************************/ |
| 320 | |
| 321 | FLAC__bool FLAC__bitreader_init(FLAC__BitReader *br, FLAC__CPUInfo cpu, FLAC__BitReaderReadCallback rcb, void *cd) |
| 322 | { |
| 323 | FLAC__ASSERT(0 != br); |
| 324 | |
| 325 | br->words = br->bytes = 0; |
| 326 | br->consumed_words = br->consumed_bits = 0; |
| 327 | br->capacity = FLAC__BITREADER_DEFAULT_CAPACITY; |
| 328 | br->buffer = (brword*)malloc(sizeof(brword) * br->capacity); |
| 329 | if(br->buffer == 0) |
| 330 | return false; |
| 331 | br->read_callback = rcb; |
| 332 | br->client_data = cd; |
| 333 | br->cpu_info = cpu; |
| 334 | |
| 335 | return true; |
| 336 | } |
| 337 | |
| 338 | void FLAC__bitreader_free(FLAC__BitReader *br) |
| 339 | { |
| 340 | FLAC__ASSERT(0 != br); |
| 341 | |
| 342 | if(0 != br->buffer) |
| 343 | free(br->buffer); |
| 344 | br->buffer = 0; |
| 345 | br->capacity = 0; |
| 346 | br->words = br->bytes = 0; |
| 347 | br->consumed_words = br->consumed_bits = 0; |
| 348 | br->read_callback = 0; |
| 349 | br->client_data = 0; |
| 350 | } |
| 351 | |
| 352 | FLAC__bool FLAC__bitreader_clear(FLAC__BitReader *br) |
| 353 | { |
| 354 | br->words = br->bytes = 0; |
| 355 | br->consumed_words = br->consumed_bits = 0; |
| 356 | return true; |
| 357 | } |
| 358 | |
| 359 | void FLAC__bitreader_dump(const FLAC__BitReader *br, FILE *out) |
| 360 | { |
| 361 | unsigned i, j; |
| 362 | if(br == 0) { |
| 363 | fprintf(out, "bitreader is NULL\n"); |
| 364 | } |
| 365 | else { |
| 366 | fprintf(out, "bitreader: capacity=%u words=%u bytes=%u consumed: words=%u, bits=%u\n", br->capacity, br->words, br->bytes, br->consumed_words, br->consumed_bits); |
| 367 | |
| 368 | for(i = 0; i < br->words; i++) { |
| 369 | fprintf(out, "%08X: ", i); |
| 370 | for(j = 0; j < FLAC__BITS_PER_WORD; j++) |
| 371 | if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits)) |
| 372 | fprintf(out, "."); |
| 373 | else |
| 374 | fprintf(out, "%01u", br->buffer[i] & (1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0); |
| 375 | fprintf(out, "\n"); |
| 376 | } |
| 377 | if(br->bytes > 0) { |
| 378 | fprintf(out, "%08X: ", i); |
| 379 | for(j = 0; j < br->bytes*8; j++) |
| 380 | if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits)) |
| 381 | fprintf(out, "."); |
| 382 | else |
| 383 | fprintf(out, "%01u", br->buffer[i] & (1 << (br->bytes*8-j-1)) ? 1:0); |
| 384 | fprintf(out, "\n"); |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | void FLAC__bitreader_reset_read_crc16(FLAC__BitReader *br, FLAC__uint16 seed) |
| 390 | { |
| 391 | FLAC__ASSERT(0 != br); |
| 392 | FLAC__ASSERT(0 != br->buffer); |
| 393 | FLAC__ASSERT((br->consumed_bits & 7) == 0); |
| 394 | |
| 395 | br->read_crc16 = (unsigned)seed; |
| 396 | br->crc16_align = br->consumed_bits; |
| 397 | } |
| 398 | |
| 399 | FLAC__uint16 FLAC__bitreader_get_read_crc16(FLAC__BitReader *br) |
| 400 | { |
| 401 | FLAC__ASSERT(0 != br); |
| 402 | FLAC__ASSERT(0 != br->buffer); |
| 403 | FLAC__ASSERT((br->consumed_bits & 7) == 0); |
| 404 | FLAC__ASSERT(br->crc16_align <= br->consumed_bits); |
| 405 | |
| 406 | /* CRC any tail bytes in a partially-consumed word */ |
| 407 | if(br->consumed_bits) { |
| 408 | const brword tail = br->buffer[br->consumed_words]; |
| 409 | for( ; br->crc16_align < br->consumed_bits; br->crc16_align += 8) |
| 410 | br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)((tail >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), br->read_crc16); |
| 411 | } |
| 412 | return br->read_crc16; |
| 413 | } |
| 414 | |
| 415 | FLaC__INLINE FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader *br) |
| 416 | { |
| 417 | return ((br->consumed_bits & 7) == 0); |
| 418 | } |
| 419 | |
| 420 | FLaC__INLINE unsigned FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitReader *br) |
| 421 | { |
| 422 | return 8 - (br->consumed_bits & 7); |
| 423 | } |
| 424 | |
| 425 | FLaC__INLINE unsigned FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitReader *br) |
| 426 | { |
| 427 | return (br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits; |
| 428 | } |
| 429 | |
| 430 | FLaC__INLINE FLAC__bool FLAC__bitreader_read_raw_uint32(FLAC__BitReader *br, FLAC__uint32 *val, unsigned bits) |
| 431 | { |
| 432 | FLAC__ASSERT(0 != br); |
| 433 | FLAC__ASSERT(0 != br->buffer); |
| 434 | |
| 435 | FLAC__ASSERT(bits <= 32); |
| 436 | FLAC__ASSERT((br->capacity*FLAC__BITS_PER_WORD) * 2 >= bits); |
| 437 | FLAC__ASSERT(br->consumed_words <= br->words); |
| 438 | |
| 439 | /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */ |
| 440 | FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32); |
| 441 | |
| 442 | if(bits == 0) { /* OPT: investigate if this can ever happen, maybe change to assertion */ |
| 443 | *val = 0; |
| 444 | return true; |
| 445 | } |
| 446 | |
| 447 | while((br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits < bits) { |
| 448 | if(!bitreader_read_from_client_(br)) |
| 449 | return false; |
| 450 | } |
| 451 | if(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */ |
| 452 | /* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */ |
| 453 | if(br->consumed_bits) { |
| 454 | /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */ |
| 455 | const unsigned n = FLAC__BITS_PER_WORD - br->consumed_bits; |
| 456 | const brword word = br->buffer[br->consumed_words]; |
| 457 | if(bits < n) { |
| 458 | *val = (word & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (n-bits); |
| 459 | br->consumed_bits += bits; |
| 460 | return true; |
| 461 | } |
| 462 | *val = word & (FLAC__WORD_ALL_ONES >> br->consumed_bits); |
| 463 | bits -= n; |
| 464 | crc16_update_word_(br, word); |
| 465 | br->consumed_words++; |
| 466 | br->consumed_bits = 0; |
| 467 | if(bits) { /* if there are still bits left to read, there have to be less than 32 so they will all be in the next word */ |
| 468 | *val <<= bits; |
| 469 | *val |= (br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits)); |
| 470 | br->consumed_bits = bits; |
| 471 | } |
| 472 | return true; |
| 473 | } |
| 474 | else { |
| 475 | const brword word = br->buffer[br->consumed_words]; |
| 476 | if(bits < FLAC__BITS_PER_WORD) { |
| 477 | *val = word >> (FLAC__BITS_PER_WORD-bits); |
| 478 | br->consumed_bits = bits; |
| 479 | return true; |
| 480 | } |
| 481 | /* at this point 'bits' must be == FLAC__BITS_PER_WORD; because of previous assertions, it can't be larger */ |
| 482 | *val = word; |
| 483 | crc16_update_word_(br, word); |
| 484 | br->consumed_words++; |
| 485 | return true; |
| 486 | } |
| 487 | } |
| 488 | else { |
| 489 | /* in this case we're starting our read at a partial tail word; |
| 490 | * the reader has guaranteed that we have at least 'bits' bits |
| 491 | * available to read, which makes this case simpler. |
| 492 | */ |
| 493 | /* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */ |
| 494 | if(br->consumed_bits) { |
| 495 | /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */ |
| 496 | FLAC__ASSERT(br->consumed_bits + bits <= br->bytes*8); |
| 497 | *val = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (FLAC__BITS_PER_WORD-br->consumed_bits-bits); |
| 498 | br->consumed_bits += bits; |
| 499 | return true; |
| 500 | } |
| 501 | else { |
| 502 | *val = br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits); |
| 503 | br->consumed_bits += bits; |
| 504 | return true; |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | FLAC__bool FLAC__bitreader_read_raw_int32(FLAC__BitReader *br, FLAC__int32 *val, unsigned bits) |
| 510 | { |
| 511 | /* OPT: inline raw uint32 code here, or make into a macro if possible in the .h file */ |
| 512 | if(!FLAC__bitreader_read_raw_uint32(br, (FLAC__uint32*)val, bits)) |
| 513 | return false; |
| 514 | /* sign-extend: */ |
| 515 | *val <<= (32-bits); |
| 516 | *val >>= (32-bits); |
| 517 | return true; |
| 518 | } |
| 519 | |
| 520 | FLAC__bool FLAC__bitreader_read_raw_uint64(FLAC__BitReader *br, FLAC__uint64 *val, unsigned bits) |
| 521 | { |
| 522 | FLAC__uint32 hi, lo; |
| 523 | |
| 524 | if(bits > 32) { |
| 525 | if(!FLAC__bitreader_read_raw_uint32(br, &hi, bits-32)) |
| 526 | return false; |
| 527 | if(!FLAC__bitreader_read_raw_uint32(br, &lo, 32)) |
| 528 | return false; |
| 529 | *val = hi; |
| 530 | *val <<= 32; |
| 531 | *val |= lo; |
| 532 | } |
| 533 | else { |
| 534 | if(!FLAC__bitreader_read_raw_uint32(br, &lo, bits)) |
| 535 | return false; |
| 536 | *val = lo; |
| 537 | } |
| 538 | return true; |
| 539 | } |
| 540 | |
| 541 | FLaC__INLINE FLAC__bool FLAC__bitreader_read_uint32_little_endian(FLAC__BitReader *br, FLAC__uint32 *val) |
| 542 | { |
| 543 | FLAC__uint32 x8, x32 = 0; |
| 544 | |
| 545 | /* this doesn't need to be that fast as currently it is only used for vorbis comments */ |
| 546 | |
| 547 | if(!FLAC__bitreader_read_raw_uint32(br, &x32, 8)) |
| 548 | return false; |
| 549 | |
| 550 | if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8)) |
| 551 | return false; |
| 552 | x32 |= (x8 << 8); |
| 553 | |
| 554 | if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8)) |
| 555 | return false; |
| 556 | x32 |= (x8 << 16); |
| 557 | |
| 558 | if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8)) |
| 559 | return false; |
| 560 | x32 |= (x8 << 24); |
| 561 | |
| 562 | *val = x32; |
| 563 | return true; |
| 564 | } |
| 565 | |
| 566 | FLAC__bool FLAC__bitreader_skip_bits_no_crc(FLAC__BitReader *br, unsigned bits) |
| 567 | { |
| 568 | /* |
| 569 | * OPT: a faster implementation is possible but probably not that useful |
| 570 | * since this is only called a couple of times in the metadata readers. |
| 571 | */ |
| 572 | FLAC__ASSERT(0 != br); |
| 573 | FLAC__ASSERT(0 != br->buffer); |
| 574 | |
| 575 | if(bits > 0) { |
| 576 | const unsigned n = br->consumed_bits & 7; |
| 577 | unsigned m; |
| 578 | FLAC__uint32 x; |
| 579 | |
| 580 | if(n != 0) { |
| 581 | m = min(8-n, bits); |
| 582 | if(!FLAC__bitreader_read_raw_uint32(br, &x, m)) |
| 583 | return false; |
| 584 | bits -= m; |
| 585 | } |
| 586 | m = bits / 8; |
| 587 | if(m > 0) { |
| 588 | if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(br, m)) |
| 589 | return false; |
| 590 | bits %= 8; |
| 591 | } |
| 592 | if(bits > 0) { |
| 593 | if(!FLAC__bitreader_read_raw_uint32(br, &x, bits)) |
| 594 | return false; |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | return true; |
| 599 | } |
| 600 | |
| 601 | FLAC__bool FLAC__bitreader_skip_byte_block_aligned_no_crc(FLAC__BitReader *br, unsigned nvals) |
| 602 | { |
| 603 | FLAC__uint32 x; |
| 604 | |
| 605 | FLAC__ASSERT(0 != br); |
| 606 | FLAC__ASSERT(0 != br->buffer); |
| 607 | FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br)); |
| 608 | |
| 609 | /* step 1: skip over partial head word to get word aligned */ |
| 610 | while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */ |
| 611 | if(!FLAC__bitreader_read_raw_uint32(br, &x, 8)) |
| 612 | return false; |
| 613 | nvals--; |
| 614 | } |
| 615 | if(0 == nvals) |
| 616 | return true; |
| 617 | /* step 2: skip whole words in chunks */ |
| 618 | while(nvals >= FLAC__BYTES_PER_WORD) { |
| 619 | if(br->consumed_words < br->words) { |
| 620 | br->consumed_words++; |
| 621 | nvals -= FLAC__BYTES_PER_WORD; |
| 622 | } |
| 623 | else if(!bitreader_read_from_client_(br)) |
| 624 | return false; |
| 625 | } |
| 626 | /* step 3: skip any remainder from partial tail bytes */ |
| 627 | while(nvals) { |
| 628 | if(!FLAC__bitreader_read_raw_uint32(br, &x, 8)) |
| 629 | return false; |
| 630 | nvals--; |
| 631 | } |
| 632 | |
| 633 | return true; |
| 634 | } |
| 635 | |
| 636 | FLAC__bool FLAC__bitreader_read_byte_block_aligned_no_crc(FLAC__BitReader *br, FLAC__byte *val, unsigned nvals) |
| 637 | { |
| 638 | FLAC__uint32 x; |
| 639 | |
| 640 | FLAC__ASSERT(0 != br); |
| 641 | FLAC__ASSERT(0 != br->buffer); |
| 642 | FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br)); |
| 643 | |
| 644 | /* step 1: read from partial head word to get word aligned */ |
| 645 | while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */ |
| 646 | if(!FLAC__bitreader_read_raw_uint32(br, &x, 8)) |
| 647 | return false; |
| 648 | *val++ = (FLAC__byte)x; |
| 649 | nvals--; |
| 650 | } |
| 651 | if(0 == nvals) |
| 652 | return true; |
| 653 | /* step 2: read whole words in chunks */ |
| 654 | while(nvals >= FLAC__BYTES_PER_WORD) { |
| 655 | if(br->consumed_words < br->words) { |
| 656 | const brword word = br->buffer[br->consumed_words++]; |
| 657 | #if FLAC__BYTES_PER_WORD == 4 |
| 658 | val[0] = (FLAC__byte)(word >> 24); |
| 659 | val[1] = (FLAC__byte)(word >> 16); |
| 660 | val[2] = (FLAC__byte)(word >> 8); |
| 661 | val[3] = (FLAC__byte)word; |
| 662 | #elif FLAC__BYTES_PER_WORD == 8 |
| 663 | val[0] = (FLAC__byte)(word >> 56); |
| 664 | val[1] = (FLAC__byte)(word >> 48); |
| 665 | val[2] = (FLAC__byte)(word >> 40); |
| 666 | val[3] = (FLAC__byte)(word >> 32); |
| 667 | val[4] = (FLAC__byte)(word >> 24); |
| 668 | val[5] = (FLAC__byte)(word >> 16); |
| 669 | val[6] = (FLAC__byte)(word >> 8); |
| 670 | val[7] = (FLAC__byte)word; |
| 671 | #else |
| 672 | for(x = 0; x < FLAC__BYTES_PER_WORD; x++) |
| 673 | val[x] = (FLAC__byte)(word >> (8*(FLAC__BYTES_PER_WORD-x-1))); |
| 674 | #endif |
| 675 | val += FLAC__BYTES_PER_WORD; |
| 676 | nvals -= FLAC__BYTES_PER_WORD; |
| 677 | } |
| 678 | else if(!bitreader_read_from_client_(br)) |
| 679 | return false; |
| 680 | } |
| 681 | /* step 3: read any remainder from partial tail bytes */ |
| 682 | while(nvals) { |
| 683 | if(!FLAC__bitreader_read_raw_uint32(br, &x, 8)) |
| 684 | return false; |
| 685 | *val++ = (FLAC__byte)x; |
| 686 | nvals--; |
| 687 | } |
| 688 | |
| 689 | return true; |
| 690 | } |
| 691 | |
| 692 | FLaC__INLINE FLAC__bool FLAC__bitreader_read_unary_unsigned(FLAC__BitReader *br, unsigned *val) |
| 693 | #if 0 /* slow but readable version */ |
| 694 | { |
| 695 | unsigned bit; |
| 696 | |
| 697 | FLAC__ASSERT(0 != br); |
| 698 | FLAC__ASSERT(0 != br->buffer); |
| 699 | |
| 700 | *val = 0; |
| 701 | while(1) { |
| 702 | if(!FLAC__bitreader_read_bit(br, &bit)) |
| 703 | return false; |
| 704 | if(bit) |
| 705 | break; |
| 706 | else |
| 707 | *val++; |
| 708 | } |
| 709 | return true; |
| 710 | } |
| 711 | #else |
| 712 | { |
| 713 | unsigned i; |
| 714 | |
| 715 | FLAC__ASSERT(0 != br); |
| 716 | FLAC__ASSERT(0 != br->buffer); |
| 717 | |
| 718 | *val = 0; |
| 719 | while(1) { |
| 720 | while(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */ |
| 721 | brword b = br->buffer[br->consumed_words] << br->consumed_bits; |
| 722 | if(b) { |
| 723 | i = COUNT_ZERO_MSBS(b); |
| 724 | *val += i; |
| 725 | i++; |
| 726 | br->consumed_bits += i; |
| 727 | if(br->consumed_bits >= FLAC__BITS_PER_WORD) { /* faster way of testing if(br->consumed_bits == FLAC__BITS_PER_WORD) */ |
| 728 | crc16_update_word_(br, br->buffer[br->consumed_words]); |
| 729 | br->consumed_words++; |
| 730 | br->consumed_bits = 0; |
| 731 | } |
| 732 | return true; |
| 733 | } |
| 734 | else { |
| 735 | *val += FLAC__BITS_PER_WORD - br->consumed_bits; |
| 736 | crc16_update_word_(br, br->buffer[br->consumed_words]); |
| 737 | br->consumed_words++; |
| 738 | br->consumed_bits = 0; |
| 739 | /* didn't find stop bit yet, have to keep going... */ |
| 740 | } |
| 741 | } |
| 742 | /* at this point we've eaten up all the whole words; have to try |
| 743 | * reading through any tail bytes before calling the read callback. |
| 744 | * this is a repeat of the above logic adjusted for the fact we |
| 745 | * don't have a whole word. note though if the client is feeding |
| 746 | * us data a byte at a time (unlikely), br->consumed_bits may not |
| 747 | * be zero. |
| 748 | */ |
| 749 | if(br->bytes) { |
| 750 | const unsigned end = br->bytes * 8; |
| 751 | brword b = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << br->consumed_bits; |
| 752 | if(b) { |
| 753 | i = COUNT_ZERO_MSBS(b); |
| 754 | *val += i; |
| 755 | i++; |
| 756 | br->consumed_bits += i; |
| 757 | FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD); |
| 758 | return true; |
| 759 | } |
| 760 | else { |
| 761 | *val += end - br->consumed_bits; |
| 762 | br->consumed_bits += end; |
| 763 | FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD); |
| 764 | /* didn't find stop bit yet, have to keep going... */ |
| 765 | } |
| 766 | } |
| 767 | if(!bitreader_read_from_client_(br)) |
| 768 | return false; |
| 769 | } |
| 770 | } |
| 771 | #endif |
| 772 | |
| 773 | FLAC__bool FLAC__bitreader_read_rice_signed(FLAC__BitReader *br, int *val, unsigned parameter) |
| 774 | { |
| 775 | FLAC__uint32 lsbs = 0, msbs = 0; |
| 776 | unsigned uval; |
| 777 | |
| 778 | FLAC__ASSERT(0 != br); |
| 779 | FLAC__ASSERT(0 != br->buffer); |
| 780 | FLAC__ASSERT(parameter <= 31); |
| 781 | |
| 782 | /* read the unary MSBs and end bit */ |
| 783 | if(!FLAC__bitreader_read_unary_unsigned(br, &msbs)) |
| 784 | return false; |
| 785 | |
| 786 | /* read the binary LSBs */ |
| 787 | if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, parameter)) |
| 788 | return false; |
| 789 | |
| 790 | /* compose the value */ |
| 791 | uval = (msbs << parameter) | lsbs; |
| 792 | if(uval & 1) |
| 793 | *val = -((int)(uval >> 1)) - 1; |
| 794 | else |
| 795 | *val = (int)(uval >> 1); |
| 796 | |
| 797 | return true; |
| 798 | } |
| 799 | |
| 800 | /* this is by far the most heavily used reader call. it ain't pretty but it's fast */ |
| 801 | /* a lot of the logic is copied, then adapted, from FLAC__bitreader_read_unary_unsigned() and FLAC__bitreader_read_raw_uint32() */ |
| 802 | FLAC__bool FLAC__bitreader_read_rice_signed_block(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter) |
| 803 | /* OPT: possibly faster version for use with MSVC */ |
| 804 | #ifdef _MSC_VER |
| 805 | { |
| 806 | unsigned i; |
| 807 | unsigned uval = 0; |
| 808 | unsigned bits; /* the # of binary LSBs left to read to finish a rice codeword */ |
| 809 | |
| 810 | /* try and get br->consumed_words and br->consumed_bits into register; |
| 811 | * must remember to flush them back to *br before calling other |
| 812 | * bitwriter functions that use them, and before returning */ |
| 813 | register unsigned cwords; |
| 814 | register unsigned cbits; |
| 815 | |
| 816 | FLAC__ASSERT(0 != br); |
| 817 | FLAC__ASSERT(0 != br->buffer); |
| 818 | /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */ |
| 819 | FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32); |
| 820 | FLAC__ASSERT(parameter < 32); |
| 821 | /* the above two asserts also guarantee that the binary part never straddles more that 2 words, so we don't have to loop to read it */ |
| 822 | |
| 823 | if(nvals == 0) |
| 824 | return true; |
| 825 | |
| 826 | cbits = br->consumed_bits; |
| 827 | cwords = br->consumed_words; |
| 828 | |
| 829 | while(1) { |
| 830 | |
| 831 | /* read unary part */ |
| 832 | while(1) { |
| 833 | while(cwords < br->words) { /* if we've not consumed up to a partial tail word... */ |
| 834 | brword b = br->buffer[cwords] << cbits; |
| 835 | if(b) { |
| 836 | #if 0 /* slower, probably due to bad register allocation... */ && defined FLAC__CPU_IA32 && !defined FLAC__NO_ASM && FLAC__BITS_PER_WORD == 32 |
| 837 | __asm { |
| 838 | bsr eax, b |
| 839 | not eax |
| 840 | and eax, 31 |
| 841 | mov i, eax |
| 842 | } |
| 843 | #else |
| 844 | i = COUNT_ZERO_MSBS(b); |
| 845 | #endif |
| 846 | uval += i; |
| 847 | bits = parameter; |
| 848 | i++; |
| 849 | cbits += i; |
| 850 | if(cbits == FLAC__BITS_PER_WORD) { |
| 851 | crc16_update_word_(br, br->buffer[cwords]); |
| 852 | cwords++; |
| 853 | cbits = 0; |
| 854 | } |
| 855 | goto break1; |
| 856 | } |
| 857 | else { |
| 858 | uval += FLAC__BITS_PER_WORD - cbits; |
| 859 | crc16_update_word_(br, br->buffer[cwords]); |
| 860 | cwords++; |
| 861 | cbits = 0; |
| 862 | /* didn't find stop bit yet, have to keep going... */ |
| 863 | } |
| 864 | } |
| 865 | /* at this point we've eaten up all the whole words; have to try |
| 866 | * reading through any tail bytes before calling the read callback. |
| 867 | * this is a repeat of the above logic adjusted for the fact we |
| 868 | * don't have a whole word. note though if the client is feeding |
| 869 | * us data a byte at a time (unlikely), br->consumed_bits may not |
| 870 | * be zero. |
| 871 | */ |
| 872 | if(br->bytes) { |
| 873 | const unsigned end = br->bytes * 8; |
| 874 | brword b = (br->buffer[cwords] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << cbits; |
| 875 | if(b) { |
| 876 | i = COUNT_ZERO_MSBS(b); |
| 877 | uval += i; |
| 878 | bits = parameter; |
| 879 | i++; |
| 880 | cbits += i; |
| 881 | FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD); |
| 882 | goto break1; |
| 883 | } |
| 884 | else { |
| 885 | uval += end - cbits; |
| 886 | cbits += end; |
| 887 | FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD); |
| 888 | /* didn't find stop bit yet, have to keep going... */ |
| 889 | } |
| 890 | } |
| 891 | /* flush registers and read; bitreader_read_from_client_() does |
| 892 | * not touch br->consumed_bits at all but we still need to set |
| 893 | * it in case it fails and we have to return false. |
| 894 | */ |
| 895 | br->consumed_bits = cbits; |
| 896 | br->consumed_words = cwords; |
| 897 | if(!bitreader_read_from_client_(br)) |
| 898 | return false; |
| 899 | cwords = br->consumed_words; |
| 900 | } |
| 901 | break1: |
| 902 | /* read binary part */ |
| 903 | FLAC__ASSERT(cwords <= br->words); |
| 904 | |
| 905 | if(bits) { |
| 906 | while((br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits < bits) { |
| 907 | /* flush registers and read; bitreader_read_from_client_() does |
| 908 | * not touch br->consumed_bits at all but we still need to set |
| 909 | * it in case it fails and we have to return false. |
| 910 | */ |
| 911 | br->consumed_bits = cbits; |
| 912 | br->consumed_words = cwords; |
| 913 | if(!bitreader_read_from_client_(br)) |
| 914 | return false; |
| 915 | cwords = br->consumed_words; |
| 916 | } |
| 917 | if(cwords < br->words) { /* if we've not consumed up to a partial tail word... */ |
| 918 | if(cbits) { |
| 919 | /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */ |
| 920 | const unsigned n = FLAC__BITS_PER_WORD - cbits; |
| 921 | const brword word = br->buffer[cwords]; |
| 922 | if(bits < n) { |
| 923 | uval <<= bits; |
| 924 | uval |= (word & (FLAC__WORD_ALL_ONES >> cbits)) >> (n-bits); |
| 925 | cbits += bits; |
| 926 | goto break2; |
| 927 | } |
| 928 | uval <<= n; |
| 929 | uval |= word & (FLAC__WORD_ALL_ONES >> cbits); |
| 930 | bits -= n; |
| 931 | crc16_update_word_(br, word); |
| 932 | cwords++; |
| 933 | cbits = 0; |
| 934 | if(bits) { /* if there are still bits left to read, there have to be less than 32 so they will all be in the next word */ |
| 935 | uval <<= bits; |
| 936 | uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits)); |
| 937 | cbits = bits; |
| 938 | } |
| 939 | goto break2; |
| 940 | } |
| 941 | else { |
| 942 | FLAC__ASSERT(bits < FLAC__BITS_PER_WORD); |
| 943 | uval <<= bits; |
| 944 | uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits); |
| 945 | cbits = bits; |
| 946 | goto break2; |
| 947 | } |
| 948 | } |
| 949 | else { |
| 950 | /* in this case we're starting our read at a partial tail word; |
| 951 | * the reader has guaranteed that we have at least 'bits' bits |
| 952 | * available to read, which makes this case simpler. |
| 953 | */ |
| 954 | uval <<= bits; |
| 955 | if(cbits) { |
| 956 | /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */ |
| 957 | FLAC__ASSERT(cbits + bits <= br->bytes*8); |
| 958 | uval |= (br->buffer[cwords] & (FLAC__WORD_ALL_ONES >> cbits)) >> (FLAC__BITS_PER_WORD-cbits-bits); |
| 959 | cbits += bits; |
| 960 | goto break2; |
| 961 | } |
| 962 | else { |
| 963 | uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits); |
| 964 | cbits += bits; |
| 965 | goto break2; |
| 966 | } |
| 967 | } |
| 968 | } |
| 969 | break2: |
| 970 | /* compose the value */ |
| 971 | *vals = (int)(uval >> 1 ^ -(int)(uval & 1)); |
| 972 | |
| 973 | /* are we done? */ |
| 974 | --nvals; |
| 975 | if(nvals == 0) { |
| 976 | br->consumed_bits = cbits; |
| 977 | br->consumed_words = cwords; |
| 978 | return true; |
| 979 | } |
| 980 | |
| 981 | uval = 0; |
| 982 | ++vals; |
| 983 | |
| 984 | } |
| 985 | } |
| 986 | #else |
| 987 | { |
| 988 | unsigned i; |
| 989 | unsigned uval = 0; |
| 990 | |
| 991 | /* try and get br->consumed_words and br->consumed_bits into register; |
| 992 | * must remember to flush them back to *br before calling other |
| 993 | * bitwriter functions that use them, and before returning */ |
| 994 | register unsigned cwords; |
| 995 | register unsigned cbits; |
| 996 | unsigned ucbits; /* keep track of the number of unconsumed bits in the buffer */ |
| 997 | |
| 998 | FLAC__ASSERT(0 != br); |
| 999 | FLAC__ASSERT(0 != br->buffer); |
| 1000 | /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */ |
| 1001 | FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32); |
| 1002 | FLAC__ASSERT(parameter < 32); |
| 1003 | /* the above two asserts also guarantee that the binary part never straddles more than 2 words, so we don't have to loop to read it */ |
| 1004 | |
| 1005 | if(nvals == 0) |
| 1006 | return true; |
| 1007 | |
| 1008 | cbits = br->consumed_bits; |
| 1009 | cwords = br->consumed_words; |
| 1010 | ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits; |
| 1011 | |
| 1012 | while(1) { |
| 1013 | |
| 1014 | /* read unary part */ |
| 1015 | while(1) { |
| 1016 | while(cwords < br->words) { /* if we've not consumed up to a partial tail word... */ |
| 1017 | brword b = br->buffer[cwords] << cbits; |
| 1018 | if(b) { |
| 1019 | #if 0 /* is not discernably faster... */ && defined FLAC__CPU_IA32 && !defined FLAC__NO_ASM && FLAC__BITS_PER_WORD == 32 && defined __GNUC__ |
| 1020 | asm volatile ( |
| 1021 | "bsrl %1, %0;" |
| 1022 | "notl %0;" |
| 1023 | "andl $31, %0;" |
| 1024 | : "=r"(i) |
| 1025 | : "r"(b) |
| 1026 | ); |
| 1027 | #else |
| 1028 | i = COUNT_ZERO_MSBS(b); |
| 1029 | #endif |
| 1030 | uval += i; |
| 1031 | cbits += i; |
| 1032 | cbits++; /* skip over stop bit */ |
| 1033 | if(cbits >= FLAC__BITS_PER_WORD) { /* faster way of testing if(cbits == FLAC__BITS_PER_WORD) */ |
| 1034 | crc16_update_word_(br, br->buffer[cwords]); |
| 1035 | cwords++; |
| 1036 | cbits = 0; |
| 1037 | } |
| 1038 | goto break1; |
| 1039 | } |
| 1040 | else { |
| 1041 | uval += FLAC__BITS_PER_WORD - cbits; |
| 1042 | crc16_update_word_(br, br->buffer[cwords]); |
| 1043 | cwords++; |
| 1044 | cbits = 0; |
| 1045 | /* didn't find stop bit yet, have to keep going... */ |
| 1046 | } |
| 1047 | } |
| 1048 | /* at this point we've eaten up all the whole words; have to try |
| 1049 | * reading through any tail bytes before calling the read callback. |
| 1050 | * this is a repeat of the above logic adjusted for the fact we |
| 1051 | * don't have a whole word. note though if the client is feeding |
| 1052 | * us data a byte at a time (unlikely), br->consumed_bits may not |
| 1053 | * be zero. |
| 1054 | */ |
| 1055 | if(br->bytes) { |
| 1056 | const unsigned end = br->bytes * 8; |
| 1057 | brword b = (br->buffer[cwords] & ~(FLAC__WORD_ALL_ONES >> end)) << cbits; |
| 1058 | if(b) { |
| 1059 | i = COUNT_ZERO_MSBS(b); |
| 1060 | uval += i; |
| 1061 | cbits += i; |
| 1062 | cbits++; /* skip over stop bit */ |
| 1063 | FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD); |
| 1064 | goto break1; |
| 1065 | } |
| 1066 | else { |
| 1067 | uval += end - cbits; |
| 1068 | cbits += end; |
| 1069 | FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD); |
| 1070 | /* didn't find stop bit yet, have to keep going... */ |
| 1071 | } |
| 1072 | } |
| 1073 | /* flush registers and read; bitreader_read_from_client_() does |
| 1074 | * not touch br->consumed_bits at all but we still need to set |
| 1075 | * it in case it fails and we have to return false. |
| 1076 | */ |
| 1077 | br->consumed_bits = cbits; |
| 1078 | br->consumed_words = cwords; |
| 1079 | if(!bitreader_read_from_client_(br)) |
| 1080 | return false; |
| 1081 | cwords = br->consumed_words; |
| 1082 | ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits + uval; |
| 1083 | /* + uval to offset our count by the # of unary bits already |
| 1084 | * consumed before the read, because we will add these back |
| 1085 | * in all at once at break1 |
| 1086 | */ |
| 1087 | } |
| 1088 | break1: |
| 1089 | ucbits -= uval; |
| 1090 | ucbits--; /* account for stop bit */ |
| 1091 | |
| 1092 | /* read binary part */ |
| 1093 | FLAC__ASSERT(cwords <= br->words); |
| 1094 | |
| 1095 | if(parameter) { |
| 1096 | while(ucbits < parameter) { |
| 1097 | /* flush registers and read; bitreader_read_from_client_() does |
| 1098 | * not touch br->consumed_bits at all but we still need to set |
| 1099 | * it in case it fails and we have to return false. |
| 1100 | */ |
| 1101 | br->consumed_bits = cbits; |
| 1102 | br->consumed_words = cwords; |
| 1103 | if(!bitreader_read_from_client_(br)) |
| 1104 | return false; |
| 1105 | cwords = br->consumed_words; |
| 1106 | ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits; |
| 1107 | } |
| 1108 | if(cwords < br->words) { /* if we've not consumed up to a partial tail word... */ |
| 1109 | if(cbits) { |
| 1110 | /* this also works when consumed_bits==0, it's just slower than necessary for that case */ |
| 1111 | const unsigned n = FLAC__BITS_PER_WORD - cbits; |
| 1112 | const brword word = br->buffer[cwords]; |
| 1113 | if(parameter < n) { |
| 1114 | uval <<= parameter; |
| 1115 | uval |= (word & (FLAC__WORD_ALL_ONES >> cbits)) >> (n-parameter); |
| 1116 | cbits += parameter; |
| 1117 | } |
| 1118 | else { |
| 1119 | uval <<= n; |
| 1120 | uval |= word & (FLAC__WORD_ALL_ONES >> cbits); |
| 1121 | crc16_update_word_(br, word); |
| 1122 | cwords++; |
| 1123 | cbits = parameter - n; |
| 1124 | if(cbits) { /* parameter > n, i.e. if there are still bits left to read, there have to be less than 32 so they will all be in the next word */ |
| 1125 | uval <<= cbits; |
| 1126 | uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits)); |
| 1127 | } |
| 1128 | } |
| 1129 | } |
| 1130 | else { |
| 1131 | cbits = parameter; |
| 1132 | uval <<= parameter; |
| 1133 | uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits); |
| 1134 | } |
| 1135 | } |
| 1136 | else { |
| 1137 | /* in this case we're starting our read at a partial tail word; |
| 1138 | * the reader has guaranteed that we have at least 'parameter' |
| 1139 | * bits available to read, which makes this case simpler. |
| 1140 | */ |
| 1141 | uval <<= parameter; |
| 1142 | if(cbits) { |
| 1143 | /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */ |
| 1144 | FLAC__ASSERT(cbits + parameter <= br->bytes*8); |
| 1145 | uval |= (br->buffer[cwords] & (FLAC__WORD_ALL_ONES >> cbits)) >> (FLAC__BITS_PER_WORD-cbits-parameter); |
| 1146 | cbits += parameter; |
| 1147 | } |
| 1148 | else { |
| 1149 | cbits = parameter; |
| 1150 | uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits); |
| 1151 | } |
| 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | ucbits -= parameter; |
| 1156 | |
| 1157 | /* compose the value */ |
| 1158 | *vals = (int)(uval >> 1 ^ -(int)(uval & 1)); |
| 1159 | |
| 1160 | /* are we done? */ |
| 1161 | --nvals; |
| 1162 | if(nvals == 0) { |
| 1163 | br->consumed_bits = cbits; |
| 1164 | br->consumed_words = cwords; |
| 1165 | return true; |
| 1166 | } |
| 1167 | |
| 1168 | uval = 0; |
| 1169 | ++vals; |
| 1170 | |
| 1171 | } |
| 1172 | } |
| 1173 | #endif |
| 1174 | |
| 1175 | #if 0 /* UNUSED */ |
| 1176 | FLAC__bool FLAC__bitreader_read_golomb_signed(FLAC__BitReader *br, int *val, unsigned parameter) |
| 1177 | { |
| 1178 | FLAC__uint32 lsbs = 0, msbs = 0; |
| 1179 | unsigned bit, uval, k; |
| 1180 | |
| 1181 | FLAC__ASSERT(0 != br); |
| 1182 | FLAC__ASSERT(0 != br->buffer); |
| 1183 | |
| 1184 | k = FLAC__bitmath_ilog2(parameter); |
| 1185 | |
| 1186 | /* read the unary MSBs and end bit */ |
| 1187 | if(!FLAC__bitreader_read_unary_unsigned(br, &msbs)) |
| 1188 | return false; |
| 1189 | |
| 1190 | /* read the binary LSBs */ |
| 1191 | if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k)) |
| 1192 | return false; |
| 1193 | |
| 1194 | if(parameter == 1u<<k) { |
| 1195 | /* compose the value */ |
| 1196 | uval = (msbs << k) | lsbs; |
| 1197 | } |
| 1198 | else { |
| 1199 | unsigned d = (1 << (k+1)) - parameter; |
| 1200 | if(lsbs >= d) { |
| 1201 | if(!FLAC__bitreader_read_bit(br, &bit)) |
| 1202 | return false; |
| 1203 | lsbs <<= 1; |
| 1204 | lsbs |= bit; |
| 1205 | lsbs -= d; |
| 1206 | } |
| 1207 | /* compose the value */ |
| 1208 | uval = msbs * parameter + lsbs; |
| 1209 | } |
| 1210 | |
| 1211 | /* unfold unsigned to signed */ |
| 1212 | if(uval & 1) |
| 1213 | *val = -((int)(uval >> 1)) - 1; |
| 1214 | else |
| 1215 | *val = (int)(uval >> 1); |
| 1216 | |
| 1217 | return true; |
| 1218 | } |
| 1219 | |
| 1220 | FLAC__bool FLAC__bitreader_read_golomb_unsigned(FLAC__BitReader *br, unsigned *val, unsigned parameter) |
| 1221 | { |
| 1222 | FLAC__uint32 lsbs, msbs = 0; |
| 1223 | unsigned bit, k; |
| 1224 | |
| 1225 | FLAC__ASSERT(0 != br); |
| 1226 | FLAC__ASSERT(0 != br->buffer); |
| 1227 | |
| 1228 | k = FLAC__bitmath_ilog2(parameter); |
| 1229 | |
| 1230 | /* read the unary MSBs and end bit */ |
| 1231 | if(!FLAC__bitreader_read_unary_unsigned(br, &msbs)) |
| 1232 | return false; |
| 1233 | |
| 1234 | /* read the binary LSBs */ |
| 1235 | if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k)) |
| 1236 | return false; |
| 1237 | |
| 1238 | if(parameter == 1u<<k) { |
| 1239 | /* compose the value */ |
| 1240 | *val = (msbs << k) | lsbs; |
| 1241 | } |
| 1242 | else { |
| 1243 | unsigned d = (1 << (k+1)) - parameter; |
| 1244 | if(lsbs >= d) { |
| 1245 | if(!FLAC__bitreader_read_bit(br, &bit)) |
| 1246 | return false; |
| 1247 | lsbs <<= 1; |
| 1248 | lsbs |= bit; |
| 1249 | lsbs -= d; |
| 1250 | } |
| 1251 | /* compose the value */ |
| 1252 | *val = msbs * parameter + lsbs; |
| 1253 | } |
| 1254 | |
| 1255 | return true; |
| 1256 | } |
| 1257 | #endif /* UNUSED */ |
| 1258 | |
| 1259 | /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */ |
| 1260 | FLAC__bool FLAC__bitreader_read_utf8_uint32(FLAC__BitReader *br, FLAC__uint32 *val, FLAC__byte *raw, unsigned *rawlen) |
| 1261 | { |
| 1262 | FLAC__uint32 v = 0; |
| 1263 | FLAC__uint32 x; |
| 1264 | unsigned i; |
| 1265 | |
| 1266 | if(!FLAC__bitreader_read_raw_uint32(br, &x, 8)) |
| 1267 | return false; |
| 1268 | if(raw) |
| 1269 | raw[(*rawlen)++] = (FLAC__byte)x; |
| 1270 | if(!(x & 0x80)) { /* 0xxxxxxx */ |
| 1271 | v = x; |
| 1272 | i = 0; |
| 1273 | } |
| 1274 | else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */ |
| 1275 | v = x & 0x1F; |
| 1276 | i = 1; |
| 1277 | } |
| 1278 | else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */ |
| 1279 | v = x & 0x0F; |
| 1280 | i = 2; |
| 1281 | } |
| 1282 | else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */ |
| 1283 | v = x & 0x07; |
| 1284 | i = 3; |
| 1285 | } |
| 1286 | else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */ |
| 1287 | v = x & 0x03; |
| 1288 | i = 4; |
| 1289 | } |
| 1290 | else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */ |
| 1291 | v = x & 0x01; |
| 1292 | i = 5; |
| 1293 | } |
| 1294 | else { |
| 1295 | *val = 0xffffffff; |
| 1296 | return true; |
| 1297 | } |
| 1298 | for( ; i; i--) { |
| 1299 | if(!FLAC__bitreader_read_raw_uint32(br, &x, 8)) |
| 1300 | return false; |
| 1301 | if(raw) |
| 1302 | raw[(*rawlen)++] = (FLAC__byte)x; |
| 1303 | if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */ |
| 1304 | *val = 0xffffffff; |
| 1305 | return true; |
| 1306 | } |
| 1307 | v <<= 6; |
| 1308 | v |= (x & 0x3F); |
| 1309 | } |
| 1310 | *val = v; |
| 1311 | return true; |
| 1312 | } |
| 1313 | |
| 1314 | /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */ |
| 1315 | FLAC__bool FLAC__bitreader_read_utf8_uint64(FLAC__BitReader *br, FLAC__uint64 *val, FLAC__byte *raw, unsigned *rawlen) |
| 1316 | { |
| 1317 | FLAC__uint64 v = 0; |
| 1318 | FLAC__uint32 x; |
| 1319 | unsigned i; |
| 1320 | |
| 1321 | if(!FLAC__bitreader_read_raw_uint32(br, &x, 8)) |
| 1322 | return false; |
| 1323 | if(raw) |
| 1324 | raw[(*rawlen)++] = (FLAC__byte)x; |
| 1325 | if(!(x & 0x80)) { /* 0xxxxxxx */ |
| 1326 | v = x; |
| 1327 | i = 0; |
| 1328 | } |
| 1329 | else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */ |
| 1330 | v = x & 0x1F; |
| 1331 | i = 1; |
| 1332 | } |
| 1333 | else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */ |
| 1334 | v = x & 0x0F; |
| 1335 | i = 2; |
| 1336 | } |
| 1337 | else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */ |
| 1338 | v = x & 0x07; |
| 1339 | i = 3; |
| 1340 | } |
| 1341 | else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */ |
| 1342 | v = x & 0x03; |
| 1343 | i = 4; |
| 1344 | } |
| 1345 | else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */ |
| 1346 | v = x & 0x01; |
| 1347 | i = 5; |
| 1348 | } |
| 1349 | else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */ |
| 1350 | v = 0; |
| 1351 | i = 6; |
| 1352 | } |
| 1353 | else { |
| 1354 | *val = FLAC__U64L(0xffffffffffffffff); |
| 1355 | return true; |
| 1356 | } |
| 1357 | for( ; i; i--) { |
| 1358 | if(!FLAC__bitreader_read_raw_uint32(br, &x, 8)) |
| 1359 | return false; |
| 1360 | if(raw) |
| 1361 | raw[(*rawlen)++] = (FLAC__byte)x; |
| 1362 | if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */ |
| 1363 | *val = FLAC__U64L(0xffffffffffffffff); |
| 1364 | return true; |
| 1365 | } |
| 1366 | v <<= 6; |
| 1367 | v |= (x & 0x3F); |
| 1368 | } |
| 1369 | *val = v; |
| 1370 | return true; |
| 1371 | } |
src/lib/libflac/libflac/metadata_object.c
| r0 | r14135 | |
| 1 | /* libFLAC - Free Lossless Audio Codec library |
| 2 | * Copyright (C) 2001,2002,2003,2004,2005,2006,2007 Josh Coalson |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * |
| 8 | * - Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * - Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * - Neither the name of the Xiph.org Foundation nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived from |
| 17 | * this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 26 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 27 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #if HAVE_CONFIG_H |
| 33 | # include <config.h> |
| 34 | #endif |
| 35 | |
| 36 | #include <stdlib.h> |
| 37 | #include <string.h> |
| 38 | |
| 39 | #include "private/metadata.h" |
| 40 | |
| 41 | #include "flac/assert.h" |
| 42 | #include "share/alloc.h" |
| 43 | |
| 44 | |
| 45 | /**************************************************************************** |
| 46 | * |
| 47 | * Local routines |
| 48 | * |
| 49 | ***************************************************************************/ |
| 50 | |
| 51 | /* copy bytes: |
| 52 | * from = NULL && bytes = 0 |
| 53 | * to <- NULL |
| 54 | * from != NULL && bytes > 0 |
| 55 | * to <- copy of from |
| 56 | * else ASSERT |
| 57 | * malloc error leaves 'to' unchanged |
| 58 | */ |
| 59 | static FLAC__bool copy_bytes_(FLAC__byte **to, const FLAC__byte *from, unsigned bytes) |
| 60 | { |
| 61 | FLAC__ASSERT(0 != to); |
| 62 | if(bytes > 0 && 0 != from) { |
| 63 | FLAC__byte *x; |
| 64 | if(0 == (x = (FLAC__byte*)safe_malloc_(bytes))) |
| 65 | return false; |
| 66 | memcpy(x, from, bytes); |
| 67 | *to = x; |
| 68 | } |
| 69 | else { |
| 70 | FLAC__ASSERT(0 == from); |
| 71 | FLAC__ASSERT(bytes == 0); |
| 72 | *to = 0; |
| 73 | } |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | #if 0 /* UNUSED */ |
| 78 | /* like copy_bytes_(), but free()s the original '*to' if the copy succeeds and the original '*to' is non-NULL */ |
| 79 | static FLAC__bool free_copy_bytes_(FLAC__byte **to, const FLAC__byte *from, unsigned bytes) |
| 80 | { |
| 81 | FLAC__byte *copy; |
| 82 | FLAC__ASSERT(0 != to); |
| 83 | if(copy_bytes_(©, from, bytes)) { |
| 84 | if(*to) |
| 85 | free(*to); |
| 86 | *to = copy; |
| 87 | return true; |
| 88 | } |
| 89 | else |
| 90 | return false; |
| 91 | } |
| 92 | #endif |
| 93 | |
| 94 | /* reallocate entry to 1 byte larger and add a terminating NUL */ |
| 95 | /* realloc() failure leaves entry unchanged */ |
| 96 | static FLAC__bool ensure_null_terminated_(FLAC__byte **entry, unsigned length) |
| 97 | { |
| 98 | FLAC__byte *x = (FLAC__byte*)safe_realloc_add_2op_(*entry, length, /*+*/1); |
| 99 | if(0 != x) { |
| 100 | x[length] = '\0'; |
| 101 | *entry = x; |
| 102 | return true; |
| 103 | } |
| 104 | else |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | /* copies the NUL-terminated C-string 'from' to '*to', leaving '*to' |
| 109 | * unchanged if malloc fails, free()ing the original '*to' if it |
| 110 | * succeeds and the original '*to' was not NULL |
| 111 | */ |
| 112 | static FLAC__bool copy_cstring_(char **to, const char *from) |
| 113 | { |
| 114 | char *copy = strdup(from); |
| 115 | FLAC__ASSERT(to); |
| 116 | if(copy) { |
| 117 | if(*to) |
| 118 | free(*to); |
| 119 | *to = copy; |
| 120 | return true; |
| 121 | } |
| 122 | else |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | static FLAC__bool copy_vcentry_(FLAC__StreamMetadata_VorbisComment_Entry *to, const FLAC__StreamMetadata_VorbisComment_Entry *from) |
| 127 | { |
| 128 | to->length = from->length; |
| 129 | if(0 == from->entry) { |
| 130 | FLAC__ASSERT(from->length == 0); |
| 131 | to->entry = 0; |
| 132 | } |
| 133 | else { |
| 134 | FLAC__byte *x; |
| 135 | FLAC__ASSERT(from->length > 0); |
| 136 | if(0 == (x = (FLAC__byte*)safe_malloc_add_2op_(from->length, /*+*/1))) |
| 137 | return false; |
| 138 | memcpy(x, from->entry, from->length); |
| 139 | x[from->length] = '\0'; |
| 140 | to->entry = x; |
| 141 | } |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | static FLAC__bool copy_track_(FLAC__StreamMetadata_CueSheet_Track *to, const FLAC__StreamMetadata_CueSheet_Track *from) |
| 146 | { |
| 147 | memcpy(to, from, sizeof(FLAC__StreamMetadata_CueSheet_Track)); |
| 148 | if(0 == from->indices) { |
| 149 | FLAC__ASSERT(from->num_indices == 0); |
| 150 | } |
| 151 | else { |
| 152 | FLAC__StreamMetadata_CueSheet_Index *x; |
| 153 | FLAC__ASSERT(from->num_indices > 0); |
| 154 | if(0 == (x = (FLAC__StreamMetadata_CueSheet_Index*)safe_malloc_mul_2op_(from->num_indices, /*times*/sizeof(FLAC__StreamMetadata_CueSheet_Index)))) |
| 155 | return false; |
| 156 | memcpy(x, from->indices, from->num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index)); |
| 157 | to->indices = x; |
| 158 | } |
| 159 | return true; |
| 160 | } |
| 161 | |
| 162 | static void seektable_calculate_length_(FLAC__StreamMetadata *object) |
| 163 | { |
| 164 | FLAC__ASSERT(0 != object); |
| 165 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE); |
| 166 | |
| 167 | object->length = object->data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH; |
| 168 | } |
| 169 | |
| 170 | static FLAC__StreamMetadata_SeekPoint *seekpoint_array_new_(unsigned num_points) |
| 171 | { |
| 172 | FLAC__StreamMetadata_SeekPoint *object_array; |
| 173 | |
| 174 | FLAC__ASSERT(num_points > 0); |
| 175 | |
| 176 | object_array = (FLAC__StreamMetadata_SeekPoint*)safe_malloc_mul_2op_(num_points, /*times*/sizeof(FLAC__StreamMetadata_SeekPoint)); |
| 177 | |
| 178 | if(0 != object_array) { |
| 179 | unsigned i; |
| 180 | for(i = 0; i < num_points; i++) { |
| 181 | object_array[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER; |
| 182 | object_array[i].stream_offset = 0; |
| 183 | object_array[i].frame_samples = 0; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | return object_array; |
| 188 | } |
| 189 | |
| 190 | static void vorbiscomment_calculate_length_(FLAC__StreamMetadata *object) |
| 191 | { |
| 192 | unsigned i; |
| 193 | |
| 194 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
| 195 | |
| 196 | object->length = (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN) / 8; |
| 197 | object->length += object->data.vorbis_comment.vendor_string.length; |
| 198 | object->length += (FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN) / 8; |
| 199 | for(i = 0; i < object->data.vorbis_comment.num_comments; i++) { |
| 200 | object->length += (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8); |
| 201 | object->length += object->data.vorbis_comment.comments[i].length; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | static FLAC__StreamMetadata_VorbisComment_Entry *vorbiscomment_entry_array_new_(unsigned num_comments) |
| 206 | { |
| 207 | FLAC__ASSERT(num_comments > 0); |
| 208 | |
| 209 | return (FLAC__StreamMetadata_VorbisComment_Entry*)safe_calloc_(num_comments, sizeof(FLAC__StreamMetadata_VorbisComment_Entry)); |
| 210 | } |
| 211 | |
| 212 | static void vorbiscomment_entry_array_delete_(FLAC__StreamMetadata_VorbisComment_Entry *object_array, unsigned num_comments) |
| 213 | { |
| 214 | unsigned i; |
| 215 | |
| 216 | FLAC__ASSERT(0 != object_array && num_comments > 0); |
| 217 | |
| 218 | for(i = 0; i < num_comments; i++) |
| 219 | if(0 != object_array[i].entry) |
| 220 | free(object_array[i].entry); |
| 221 | |
| 222 | if(0 != object_array) |
| 223 | free(object_array); |
| 224 | } |
| 225 | |
| 226 | static FLAC__StreamMetadata_VorbisComment_Entry *vorbiscomment_entry_array_copy_(const FLAC__StreamMetadata_VorbisComment_Entry *object_array, unsigned num_comments) |
| 227 | { |
| 228 | FLAC__StreamMetadata_VorbisComment_Entry *return_array; |
| 229 | |
| 230 | FLAC__ASSERT(0 != object_array); |
| 231 | FLAC__ASSERT(num_comments > 0); |
| 232 | |
| 233 | return_array = vorbiscomment_entry_array_new_(num_comments); |
| 234 | |
| 235 | if(0 != return_array) { |
| 236 | unsigned i; |
| 237 | |
| 238 | for(i = 0; i < num_comments; i++) { |
| 239 | if(!copy_vcentry_(return_array+i, object_array+i)) { |
| 240 | vorbiscomment_entry_array_delete_(return_array, num_comments); |
| 241 | return 0; |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | return return_array; |
| 247 | } |
| 248 | |
| 249 | static FLAC__bool vorbiscomment_set_entry_(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry *dest, const FLAC__StreamMetadata_VorbisComment_Entry *src, FLAC__bool copy) |
| 250 | { |
| 251 | FLAC__byte *save; |
| 252 | |
| 253 | FLAC__ASSERT(0 != object); |
| 254 | FLAC__ASSERT(0 != dest); |
| 255 | FLAC__ASSERT(0 != src); |
| 256 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
| 257 | FLAC__ASSERT((0 != src->entry && src->length > 0) || (0 == src->entry && src->length == 0)); |
| 258 | |
| 259 | save = dest->entry; |
| 260 | |
| 261 | if(0 != src->entry && src->length > 0) { |
| 262 | if(copy) { |
| 263 | /* do the copy first so that if we fail we leave the dest object untouched */ |
| 264 | if(!copy_vcentry_(dest, src)) |
| 265 | return false; |
| 266 | } |
| 267 | else { |
| 268 | /* we have to make sure that the string we're taking over is null-terminated */ |
| 269 | |
| 270 | /* |
| 271 | * Stripping the const from src->entry is OK since we're taking |
| 272 | * ownership of the pointer. This is a hack around a deficiency |
| 273 | * in the API where the same function is used for 'copy' and |
| 274 | * 'own', but the source entry is a const pointer. If we were |
| 275 | * precise, the 'own' flavor would be a separate function with a |
| 276 | * non-const source pointer. But it's not, so we hack away. |
| 277 | */ |
| 278 | if(!ensure_null_terminated_((FLAC__byte**)(&src->entry), src->length)) |
| 279 | return false; |
| 280 | *dest = *src; |
| 281 | } |
| 282 | } |
| 283 | else { |
| 284 | /* the src is null */ |
| 285 | *dest = *src; |
| 286 | } |
| 287 | |
| 288 | if(0 != save) |
| 289 | free(save); |
| 290 | |
| 291 | vorbiscomment_calculate_length_(object); |
| 292 | return true; |
| 293 | } |
| 294 | |
| 295 | static int vorbiscomment_find_entry_from_(const FLAC__StreamMetadata *object, unsigned offset, const char *field_name, unsigned field_name_length) |
| 296 | { |
| 297 | unsigned i; |
| 298 | |
| 299 | FLAC__ASSERT(0 != object); |
| 300 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
| 301 | FLAC__ASSERT(0 != field_name); |
| 302 | |
| 303 | for(i = offset; i < object->data.vorbis_comment.num_comments; i++) { |
| 304 | if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments[i], field_name, field_name_length)) |
| 305 | return (int)i; |
| 306 | } |
| 307 | |
| 308 | return -1; |
| 309 | } |
| 310 | |
| 311 | static void cuesheet_calculate_length_(FLAC__StreamMetadata *object) |
| 312 | { |
| 313 | unsigned i; |
| 314 | |
| 315 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET); |
| 316 | |
| 317 | object->length = ( |
| 318 | FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN + |
| 319 | FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN + |
| 320 | FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + |
| 321 | FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN + |
| 322 | FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN |
| 323 | ) / 8; |
| 324 | |
| 325 | object->length += object->data.cue_sheet.num_tracks * ( |
| 326 | FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN + |
| 327 | FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN + |
| 328 | FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN + |
| 329 | FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + |
| 330 | FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + |
| 331 | FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN + |
| 332 | FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN |
| 333 | ) / 8; |
| 334 | |
| 335 | for(i = 0; i < object->data.cue_sheet.num_tracks; i++) { |
| 336 | object->length += object->data.cue_sheet.tracks[i].num_indices * ( |
| 337 | FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN + |
| 338 | FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN + |
| 339 | FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN |
| 340 | ) / 8; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | static FLAC__StreamMetadata_CueSheet_Index *cuesheet_track_index_array_new_(unsigned num_indices) |
| 345 | { |
| 346 | FLAC__ASSERT(num_indices > 0); |
| 347 | |
| 348 | return (FLAC__StreamMetadata_CueSheet_Index*)safe_calloc_(num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index)); |
| 349 | } |
| 350 | |
| 351 | static FLAC__StreamMetadata_CueSheet_Track *cuesheet_track_array_new_(unsigned num_tracks) |
| 352 | { |
| 353 | FLAC__ASSERT(num_tracks > 0); |
| 354 | |
| 355 | return (FLAC__StreamMetadata_CueSheet_Track*)safe_calloc_(num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track)); |
| 356 | } |
| 357 | |
| 358 | static void cuesheet_track_array_delete_(FLAC__StreamMetadata_CueSheet_Track *object_array, unsigned num_tracks) |
| 359 | { |
| 360 | unsigned i; |
| 361 | |
| 362 | FLAC__ASSERT(0 != object_array && num_tracks > 0); |
| 363 | |
| 364 | for(i = 0; i < num_tracks; i++) { |
| 365 | if(0 != object_array[i].indices) { |
| 366 | FLAC__ASSERT(object_array[i].num_indices > 0); |
| 367 | free(object_array[i].indices); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | if(0 != object_array) |
| 372 | free(object_array); |
| 373 | } |
| 374 | |
| 375 | static FLAC__StreamMetadata_CueSheet_Track *cuesheet_track_array_copy_(const FLAC__StreamMetadata_CueSheet_Track *object_array, unsigned num_tracks) |
| 376 | { |
| 377 | FLAC__StreamMetadata_CueSheet_Track *return_array; |
| 378 | |
| 379 | FLAC__ASSERT(0 != object_array); |
| 380 | FLAC__ASSERT(num_tracks > 0); |
| 381 | |
| 382 | return_array = cuesheet_track_array_new_(num_tracks); |
| 383 | |
| 384 | if(0 != return_array) { |
| 385 | unsigned i; |
| 386 | |
| 387 | for(i = 0; i < num_tracks; i++) { |
| 388 | if(!copy_track_(return_array+i, object_array+i)) { |
| 389 | cuesheet_track_array_delete_(return_array, num_tracks); |
| 390 | return 0; |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | return return_array; |
| 396 | } |
| 397 | |
| 398 | static FLAC__bool cuesheet_set_track_(FLAC__StreamMetadata *object, FLAC__StreamMetadata_CueSheet_Track *dest, const FLAC__StreamMetadata_CueSheet_Track *src, FLAC__bool copy) |
| 399 | { |
| 400 | FLAC__StreamMetadata_CueSheet_Index *save; |
| 401 | |
| 402 | FLAC__ASSERT(0 != object); |
| 403 | FLAC__ASSERT(0 != dest); |
| 404 | FLAC__ASSERT(0 != src); |
| 405 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET); |
| 406 | FLAC__ASSERT((0 != src->indices && src->num_indices > 0) || (0 == src->indices && src->num_indices == 0)); |
| 407 | |
| 408 | save = dest->indices; |
| 409 | |
| 410 | /* do the copy first so that if we fail we leave the object untouched */ |
| 411 | if(copy) { |
| 412 | if(!copy_track_(dest, src)) |
| 413 | return false; |
| 414 | } |
| 415 | else { |
| 416 | *dest = *src; |
| 417 | } |
| 418 | |
| 419 | if(0 != save) |
| 420 | free(save); |
| 421 | |
| 422 | cuesheet_calculate_length_(object); |
| 423 | return true; |
| 424 | } |
| 425 | |
| 426 | |
| 427 | /**************************************************************************** |
| 428 | * |
| 429 | * Metadata object routines |
| 430 | * |
| 431 | ***************************************************************************/ |
| 432 | |
| 433 | FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_new(FLAC__MetadataType type) |
| 434 | { |
| 435 | FLAC__StreamMetadata *object; |
| 436 | |
| 437 | if(type > FLAC__MAX_METADATA_TYPE_CODE) |
| 438 | return 0; |
| 439 | |
| 440 | object = (FLAC__StreamMetadata*)calloc(1, sizeof(FLAC__StreamMetadata)); |
| 441 | if(0 != object) { |
| 442 | object->is_last = false; |
| 443 | object->type = type; |
| 444 | switch(type) { |
| 445 | case FLAC__METADATA_TYPE_STREAMINFO: |
| 446 | object->length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH; |
| 447 | break; |
| 448 | case FLAC__METADATA_TYPE_PADDING: |
| 449 | /* calloc() took care of this for us: |
| 450 | object->length = 0; |
| 451 | */ |
| 452 | break; |
| 453 | case FLAC__METADATA_TYPE_APPLICATION: |
| 454 | object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8; |
| 455 | /* calloc() took care of this for us: |
| 456 | object->data.application.data = 0; |
| 457 | */ |
| 458 | break; |
| 459 | case FLAC__METADATA_TYPE_SEEKTABLE: |
| 460 | /* calloc() took care of this for us: |
| 461 | object->length = 0; |
| 462 | object->data.seek_table.num_points = 0; |
| 463 | object->data.seek_table.points = 0; |
| 464 | */ |
| 465 | break; |
| 466 | case FLAC__METADATA_TYPE_VORBIS_COMMENT: |
| 467 | object->data.vorbis_comment.vendor_string.length = (unsigned)strlen(FLAC__VENDOR_STRING); |
| 468 | if(!copy_bytes_(&object->data.vorbis_comment.vendor_string.entry, (const FLAC__byte*)FLAC__VENDOR_STRING, object->data.vorbis_comment.vendor_string.length+1)) { |
| 469 | free(object); |
| 470 | return 0; |
| 471 | } |
| 472 | vorbiscomment_calculate_length_(object); |
| 473 | break; |
| 474 | case FLAC__METADATA_TYPE_CUESHEET: |
| 475 | cuesheet_calculate_length_(object); |
| 476 | break; |
| 477 | case FLAC__METADATA_TYPE_PICTURE: |
| 478 | object->length = ( |
| 479 | FLAC__STREAM_METADATA_PICTURE_TYPE_LEN + |
| 480 | FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN + /* empty mime_type string */ |
| 481 | FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN + /* empty description string */ |
| 482 | FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN + |
| 483 | FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN + |
| 484 | FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN + |
| 485 | FLAC__STREAM_METADATA_PICTURE_COLORS_LEN + |
| 486 | FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN + |
| 487 | 0 /* no data */ |
| 488 | ) / 8; |
| 489 | object->data.picture.type = FLAC__STREAM_METADATA_PICTURE_TYPE_OTHER; |
| 490 | object->data.picture.mime_type = 0; |
| 491 | object->data.picture.description = 0; |
| 492 | /* calloc() took care of this for us: |
| 493 | object->data.picture.width = 0; |
| 494 | object->data.picture.height = 0; |
| 495 | object->data.picture.depth = 0; |
| 496 | object->data.picture.colors = 0; |
| 497 | object->data.picture.data_length = 0; |
| 498 | object->data.picture.data = 0; |
| 499 | */ |
| 500 | /* now initialize mime_type and description with empty strings to make things easier on the client */ |
| 501 | if(!copy_cstring_(&object->data.picture.mime_type, "")) { |
| 502 | free(object); |
| 503 | return 0; |
| 504 | } |
| 505 | if(!copy_cstring_((char**)(&object->data.picture.description), "")) { |
| 506 | if(object->data.picture.mime_type) |
| 507 | free(object->data.picture.mime_type); |
| 508 | free(object); |
| 509 | return 0; |
| 510 | } |
| 511 | break; |
| 512 | default: |
| 513 | /* calloc() took care of this for us: |
| 514 | object->length = 0; |
| 515 | object->data.unknown.data = 0; |
| 516 | */ |
| 517 | break; |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | return object; |
| 522 | } |
| 523 | |
| 524 | FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_clone(const FLAC__StreamMetadata *object) |
| 525 | { |
| 526 | FLAC__StreamMetadata *to; |
| 527 | |
| 528 | FLAC__ASSERT(0 != object); |
| 529 | |
| 530 | if(0 != (to = FLAC__metadata_object_new(object->type))) { |
| 531 | to->is_last = object->is_last; |
| 532 | to->type = object->type; |
| 533 | to->length = object->length; |
| 534 | switch(to->type) { |
| 535 | case FLAC__METADATA_TYPE_STREAMINFO: |
| 536 | memcpy(&to->data.stream_info, &object->data.stream_info, sizeof(FLAC__StreamMetadata_StreamInfo)); |
| 537 | break; |
| 538 | case FLAC__METADATA_TYPE_PADDING: |
| 539 | break; |
| 540 | case FLAC__METADATA_TYPE_APPLICATION: |
| 541 | if(to->length < FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8) { /* underflow check */ |
| 542 | FLAC__metadata_object_delete(to); |
| 543 | return 0; |
| 544 | } |
| 545 | memcpy(&to->data.application.id, &object->data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8); |
| 546 | if(!copy_bytes_(&to->data.application.data, object->data.application.data, object->length - FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8)) { |
| 547 | FLAC__metadata_object_delete(to); |
| 548 | return 0; |
| 549 | } |
| 550 | break; |
| 551 | case FLAC__METADATA_TYPE_SEEKTABLE: |
| 552 | to->data.seek_table.num_points = object->data.seek_table.num_points; |
| 553 | if(to->data.seek_table.num_points > SIZE_MAX / sizeof(FLAC__StreamMetadata_SeekPoint)) { /* overflow check */ |
| 554 | FLAC__metadata_object_delete(to); |
| 555 | return 0; |
| 556 | } |
| 557 | if(!copy_bytes_((FLAC__byte**)&to->data.seek_table.points, (FLAC__byte*)object->data.seek_table.points, object->data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint))) { |
| 558 | FLAC__metadata_object_delete(to); |
| 559 | return 0; |
| 560 | } |
| 561 | break; |
| 562 | case FLAC__METADATA_TYPE_VORBIS_COMMENT: |
| 563 | if(0 != to->data.vorbis_comment.vendor_string.entry) { |
| 564 | free(to->data.vorbis_comment.vendor_string.entry); |
| 565 | to->data.vorbis_comment.vendor_string.entry = 0; |
| 566 | } |
| 567 | if(!copy_vcentry_(&to->data.vorbis_comment.vendor_string, &object->data.vorbis_comment.vendor_string)) { |
| 568 | FLAC__metadata_object_delete(to); |
| 569 | return 0; |
| 570 | } |
| 571 | if(object->data.vorbis_comment.num_comments == 0) { |
| 572 | FLAC__ASSERT(0 == object->data.vorbis_comment.comments); |
| 573 | to->data.vorbis_comment.comments = 0; |
| 574 | } |
| 575 | else { |
| 576 | FLAC__ASSERT(0 != object->data.vorbis_comment.comments); |
| 577 | to->data.vorbis_comment.comments = vorbiscomment_entry_array_copy_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments); |
| 578 | if(0 == to->data.vorbis_comment.comments) { |
| 579 | FLAC__metadata_object_delete(to); |
| 580 | return 0; |
| 581 | } |
| 582 | } |
| 583 | to->data.vorbis_comment.num_comments = object->data.vorbis_comment.num_comments; |
| 584 | break; |
| 585 | case FLAC__METADATA_TYPE_CUESHEET: |
| 586 | memcpy(&to->data.cue_sheet, &object->data.cue_sheet, sizeof(FLAC__StreamMetadata_CueSheet)); |
| 587 | if(object->data.cue_sheet.num_tracks == 0) { |
| 588 | FLAC__ASSERT(0 == object->data.cue_sheet.tracks); |
| 589 | } |
| 590 | else { |
| 591 | FLAC__ASSERT(0 != object->data.cue_sheet.tracks); |
| 592 | to->data.cue_sheet.tracks = cuesheet_track_array_copy_(object->data.cue_sheet.tracks, object->data.cue_sheet.num_tracks); |
| 593 | if(0 == to->data.cue_sheet.tracks) { |
| 594 | FLAC__metadata_object_delete(to); |
| 595 | return 0; |
| 596 | } |
| 597 | } |
| 598 | break; |
| 599 | case FLAC__METADATA_TYPE_PICTURE: |
| 600 | to->data.picture.type = object->data.picture.type; |
| 601 | if(!copy_cstring_(&to->data.picture.mime_type, object->data.picture.mime_type)) { |
| 602 | FLAC__metadata_object_delete(to); |
| 603 | return 0; |
| 604 | } |
| 605 | if(!copy_cstring_((char**)(&to->data.picture.description), (const char*)object->data.picture.description)) { |
| 606 | FLAC__metadata_object_delete(to); |
| 607 | return 0; |
| 608 | } |
| 609 | to->data.picture.width = object->data.picture.width; |
| 610 | to->data.picture.height = object->data.picture.height; |
| 611 | to->data.picture.depth = object->data.picture.depth; |
| 612 | to->data.picture.colors = object->data.picture.colors; |
| 613 | to->data.picture.data_length = object->data.picture.data_length; |
| 614 | if(!copy_bytes_((&to->data.picture.data), object->data.picture.data, object->data.picture.data_length)) { |
| 615 | FLAC__metadata_object_delete(to); |
| 616 | return 0; |
| 617 | } |
| 618 | break; |
| 619 | default: |
| 620 | if(!copy_bytes_(&to->data.unknown.data, object->data.unknown.data, object->length)) { |
| 621 | FLAC__metadata_object_delete(to); |
| 622 | return 0; |
| 623 | } |
| 624 | break; |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | return to; |
| 629 | } |
| 630 | |
| 631 | void FLAC__metadata_object_delete_data(FLAC__StreamMetadata *object) |
| 632 | { |
| 633 | FLAC__ASSERT(0 != object); |
| 634 | |
| 635 | switch(object->type) { |
| 636 | case FLAC__METADATA_TYPE_STREAMINFO: |
| 637 | case FLAC__METADATA_TYPE_PADDING: |
| 638 | break; |
| 639 | case FLAC__METADATA_TYPE_APPLICATION: |
| 640 | if(0 != object->data.application.data) { |
| 641 | free(object->data.application.data); |
| 642 | object->data.application.data = 0; |
| 643 | } |
| 644 | break; |
| 645 | case FLAC__METADATA_TYPE_SEEKTABLE: |
| 646 | if(0 != object->data.seek_table.points) { |
| 647 | free(object->data.seek_table.points); |
| 648 | object->data.seek_table.points = 0; |
| 649 | } |
| 650 | break; |
| 651 | case FLAC__METADATA_TYPE_VORBIS_COMMENT: |
| 652 | if(0 != object->data.vorbis_comment.vendor_string.entry) { |
| 653 | free(object->data.vorbis_comment.vendor_string.entry); |
| 654 | object->data.vorbis_comment.vendor_string.entry = 0; |
| 655 | } |
| 656 | if(0 != object->data.vorbis_comment.comments) { |
| 657 | FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0); |
| 658 | vorbiscomment_entry_array_delete_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments); |
| 659 | } |
| 660 | break; |
| 661 | case FLAC__METADATA_TYPE_CUESHEET: |
| 662 | if(0 != object->data.cue_sheet.tracks) { |
| 663 | FLAC__ASSERT(object->data.cue_sheet.num_tracks > 0); |
| 664 | cuesheet_track_array_delete_(object->data.cue_sheet.tracks, object->data.cue_sheet.num_tracks); |
| 665 | } |
| 666 | break; |
| 667 | case FLAC__METADATA_TYPE_PICTURE: |
| 668 | if(0 != object->data.picture.mime_type) { |
| 669 | free(object->data.picture.mime_type); |
| 670 | object->data.picture.mime_type = 0; |
| 671 | } |
| 672 | if(0 != object->data.picture.description) { |
| 673 | free(object->data.picture.description); |
| 674 | object->data.picture.description = 0; |
| 675 | } |
| 676 | if(0 != object->data.picture.data) { |
| 677 | free(object->data.picture.data); |
| 678 | object->data.picture.data = 0; |
| 679 | } |
| 680 | break; |
| 681 | default: |
| 682 | if(0 != object->data.unknown.data) { |
| 683 | free(object->data.unknown.data); |
| 684 | object->data.unknown.data = 0; |
| 685 | } |
| 686 | break; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | FLAC_API void FLAC__metadata_object_delete(FLAC__StreamMetadata *object) |
| 691 | { |
| 692 | FLAC__metadata_object_delete_data(object); |
| 693 | free(object); |
| 694 | } |
| 695 | |
| 696 | static FLAC__bool compare_block_data_streaminfo_(const FLAC__StreamMetadata_StreamInfo *block1, const FLAC__StreamMetadata_StreamInfo *block2) |
| 697 | { |
| 698 | if(block1->min_blocksize != block2->min_blocksize) |
| 699 | return false; |
| 700 | if(block1->max_blocksize != block2->max_blocksize) |
| 701 | return false; |
| 702 | if(block1->min_framesize != block2->min_framesize) |
| 703 | return false; |
| 704 | if(block1->max_framesize != block2->max_framesize) |
| 705 | return false; |
| 706 | if(block1->sample_rate != block2->sample_rate) |
| 707 | return false; |
| 708 | if(block1->channels != block2->channels) |
| 709 | return false; |
| 710 | if(block1->bits_per_sample != block2->bits_per_sample) |
| 711 | return false; |
| 712 | if(block1->total_samples != block2->total_samples) |
| 713 | return false; |
| 714 | if(0 != memcmp(block1->md5sum, block2->md5sum, 16)) |
| 715 | return false; |
| 716 | return true; |
| 717 | } |
| 718 | |
| 719 | static FLAC__bool compare_block_data_application_(const FLAC__StreamMetadata_Application *block1, const FLAC__StreamMetadata_Application *block2, unsigned block_length) |
| 720 | { |
| 721 | FLAC__ASSERT(0 != block1); |
| 722 | FLAC__ASSERT(0 != block2); |
| 723 | FLAC__ASSERT(block_length >= sizeof(block1->id)); |
| 724 | |
| 725 | if(0 != memcmp(block1->id, block2->id, sizeof(block1->id))) |
| 726 | return false; |
| 727 | if(0 != block1->data && 0 != block2->data) |
| 728 | return 0 == memcmp(block1->data, block2->data, block_length - sizeof(block1->id)); |
| 729 | else |
| 730 | return block1->data == block2->data; |
| 731 | } |
| 732 | |
| 733 | static FLAC__bool compare_block_data_seektable_(const FLAC__StreamMetadata_SeekTable *block1, const FLAC__StreamMetadata_SeekTable *block2) |
| 734 | { |
| 735 | unsigned i; |
| 736 | |
| 737 | FLAC__ASSERT(0 != block1); |
| 738 | FLAC__ASSERT(0 != block2); |
| 739 | |
| 740 | if(block1->num_points != block2->num_points) |
| 741 | return false; |
| 742 | |
| 743 | if(0 != block1->points && 0 != block2->points) { |
| 744 | for(i = 0; i < block1->num_points; i++) { |
| 745 | if(block1->points[i].sample_number != block2->points[i].sample_number) |
| 746 | return false; |
| 747 | if(block1->points[i].stream_offset != block2->points[i].stream_offset) |
| 748 | return false; |
| 749 | if(block1->points[i].frame_samples != block2->points[i].frame_samples) |
| 750 | return false; |
| 751 | } |
| 752 | return true; |
| 753 | } |
| 754 | else |
| 755 | return block1->points == block2->points; |
| 756 | } |
| 757 | |
| 758 | static FLAC__bool compare_block_data_vorbiscomment_(const FLAC__StreamMetadata_VorbisComment *block1, const FLAC__StreamMetadata_VorbisComment *block2) |
| 759 | { |
| 760 | unsigned i; |
| 761 | |
| 762 | if(block1->vendor_string.length != block2->vendor_string.length) |
| 763 | return false; |
| 764 | |
| 765 | if(0 != block1->vendor_string.entry && 0 != block2->vendor_string.entry) { |
| 766 | if(0 != memcmp(block1->vendor_string.entry, block2->vendor_string.entry, block1->vendor_string.length)) |
| 767 | return false; |
| 768 | } |
| 769 | else if(block1->vendor_string.entry != block2->vendor_string.entry) |
| 770 | return false; |
| 771 | |
| 772 | if(block1->num_comments != block2->num_comments) |
| 773 | return false; |
| 774 | |
| 775 | for(i = 0; i < block1->num_comments; i++) { |
| 776 | if(0 != block1->comments[i].entry && 0 != block2->comments[i].entry) { |
| 777 | if(0 != memcmp(block1->comments[i].entry, block2->comments[i].entry, block1->comments[i].length)) |
| 778 | return false; |
| 779 | } |
| 780 | else if(block1->comments[i].entry != block2->comments[i].entry) |
| 781 | return false; |
| 782 | } |
| 783 | return true; |
| 784 | } |
| 785 | |
| 786 | static FLAC__bool compare_block_data_cuesheet_(const FLAC__StreamMetadata_CueSheet *block1, const FLAC__StreamMetadata_CueSheet *block2) |
| 787 | { |
| 788 | unsigned i, j; |
| 789 | |
| 790 | if(0 != strcmp(block1->media_catalog_number, block2->media_catalog_number)) |
| 791 | return false; |
| 792 | |
| 793 | if(block1->lead_in != block2->lead_in) |
| 794 | return false; |
| 795 | |
| 796 | if(block1->is_cd != block2->is_cd) |
| 797 | return false; |
| 798 | |
| 799 | if(block1->num_tracks != block2->num_tracks) |
| 800 | return false; |
| 801 | |
| 802 | if(0 != block1->tracks && 0 != block2->tracks) { |
| 803 | FLAC__ASSERT(block1->num_tracks > 0); |
| 804 | for(i = 0; i < block1->num_tracks; i++) { |
| 805 | if(block1->tracks[i].offset != block2->tracks[i].offset) |
| 806 | return false; |
| 807 | if(block1->tracks[i].number != block2->tracks[i].number) |
| 808 | return false; |
| 809 | if(0 != memcmp(block1->tracks[i].isrc, block2->tracks[i].isrc, sizeof(block1->tracks[i].isrc))) |
| 810 | return false; |
| 811 | if(block1->tracks[i].type != block2->tracks[i].type) |
| 812 | return false; |
| 813 | if(block1->tracks[i].pre_emphasis != block2->tracks[i].pre_emphasis) |
| 814 | return false; |
| 815 | if(block1->tracks[i].num_indices != block2->tracks[i].num_indices) |
| 816 | return false; |
| 817 | if(0 != block1->tracks[i].indices && 0 != block2->tracks[i].indices) { |
| 818 | FLAC__ASSERT(block1->tracks[i].num_indices > 0); |
| 819 | for(j = 0; j < block1->tracks[i].num_indices; j++) { |
| 820 | if(block1->tracks[i].indices[j].offset != block2->tracks[i].indices[j].offset) |
| 821 | return false; |
| 822 | if(block1->tracks[i].indices[j].number != block2->tracks[i].indices[j].number) |
| 823 | return false; |
| 824 | } |
| 825 | } |
| 826 | else if(block1->tracks[i].indices != block2->tracks[i].indices) |
| 827 | return false; |
| 828 | } |
| 829 | } |
| 830 | else if(block1->tracks != block2->tracks) |
| 831 | return false; |
| 832 | return true; |
| 833 | } |
| 834 | |
| 835 | static FLAC__bool compare_block_data_picture_(const FLAC__StreamMetadata_Picture *block1, const FLAC__StreamMetadata_Picture *block2) |
| 836 | { |
| 837 | if(block1->type != block2->type) |
| 838 | return false; |
| 839 | if(block1->mime_type != block2->mime_type && (0 == block1->mime_type || 0 == block2->mime_type || strcmp(block1->mime_type, block2->mime_type))) |
| 840 | return false; |
| 841 | if(block1->description != block2->description && (0 == block1->description || 0 == block2->description || strcmp((const char *)block1->description, (const char *)block2->description))) |
| 842 | return false; |
| 843 | if(block1->width != block2->width) |
| 844 | return false; |
| 845 | if(block1->height != block2->height) |
| 846 | return false; |
| 847 | if(block1->depth != block2->depth) |
| 848 | return false; |
| 849 | if(block1->colors != block2->colors) |
| 850 | return false; |
| 851 | if(block1->data_length != block2->data_length) |
| 852 | return false; |
| 853 | if(block1->data != block2->data && (0 == block1->data || 0 == block2->data || memcmp(block1->data, block2->data, block1->data_length))) |
| 854 | return false; |
| 855 | return true; |
| 856 | } |
| 857 | |
| 858 | static FLAC__bool compare_block_data_unknown_(const FLAC__StreamMetadata_Unknown *block1, const FLAC__StreamMetadata_Unknown *block2, unsigned block_length) |
| 859 | { |
| 860 | FLAC__ASSERT(0 != block1); |
| 861 | FLAC__ASSERT(0 != block2); |
| 862 | |
| 863 | if(0 != block1->data && 0 != block2->data) |
| 864 | return 0 == memcmp(block1->data, block2->data, block_length); |
| 865 | else |
| 866 | return block1->data == block2->data; |
| 867 | } |
| 868 | |
| 869 | FLAC_API FLAC__bool FLAC__metadata_object_is_equal(const FLAC__StreamMetadata *block1, const FLAC__StreamMetadata *block2) |
| 870 | { |
| 871 | FLAC__ASSERT(0 != block1); |
| 872 | FLAC__ASSERT(0 != block2); |
| 873 | |
| 874 | if(block1->type != block2->type) { |
| 875 | return false; |
| 876 | } |
| 877 | if(block1->is_last != block2->is_last) { |
| 878 | return false; |
| 879 | } |
| 880 | if(block1->length != block2->length) { |
| 881 | return false; |
| 882 | } |
| 883 | switch(block1->type) { |
| 884 | case FLAC__METADATA_TYPE_STREAMINFO: |
| 885 | return compare_block_data_streaminfo_(&block1->data.stream_info, &block2->data.stream_info); |
| 886 | case FLAC__METADATA_TYPE_PADDING: |
| 887 | return true; /* we don't compare the padding guts */ |
| 888 | case FLAC__METADATA_TYPE_APPLICATION: |
| 889 | return compare_block_data_application_(&block1->data.application, &block2->data.application, block1->length); |
| 890 | case FLAC__METADATA_TYPE_SEEKTABLE: |
| 891 | return compare_block_data_seektable_(&block1->data.seek_table, &block2->data.seek_table); |
| 892 | case FLAC__METADATA_TYPE_VORBIS_COMMENT: |
| 893 | return compare_block_data_vorbiscomment_(&block1->data.vorbis_comment, &block2->data.vorbis_comment); |
| 894 | case FLAC__METADATA_TYPE_CUESHEET: |
| 895 | return compare_block_data_cuesheet_(&block1->data.cue_sheet, &block2->data.cue_sheet); |
| 896 | case FLAC__METADATA_TYPE_PICTURE: |
| 897 | return compare_block_data_picture_(&block1->data.picture, &block2->data.picture); |
| 898 | default: |
| 899 | return compare_block_data_unknown_(&block1->data.unknown, &block2->data.unknown, block1->length); |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | FLAC_API FLAC__bool FLAC__metadata_object_application_set_data(FLAC__StreamMetadata *object, FLAC__byte *data, unsigned length, FLAC__bool copy) |
| 904 | { |
| 905 | FLAC__byte *save; |
| 906 | |
| 907 | FLAC__ASSERT(0 != object); |
| 908 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_APPLICATION); |
| 909 | FLAC__ASSERT((0 != data && length > 0) || (0 == data && length == 0 && copy == false)); |
| 910 | |
| 911 | save = object->data.application.data; |
| 912 | |
| 913 | /* do the copy first so that if we fail we leave the object untouched */ |
| 914 | if(copy) { |
| 915 | if(!copy_bytes_(&object->data.application.data, data, length)) |
| 916 | return false; |
| 917 | } |
| 918 | else { |
| 919 | object->data.application.data = data; |
| 920 | } |
| 921 | |
| 922 | if(0 != save) |
| 923 | free(save); |
| 924 | |
| 925 | object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8 + length; |
| 926 | return true; |
| 927 | } |
| 928 | |
| 929 | FLAC_API FLAC__bool FLAC__metadata_object_seektable_resize_points(FLAC__StreamMetadata *object, unsigned new_num_points) |
| 930 | { |
| 931 | FLAC__ASSERT(0 != object); |
| 932 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE); |
| 933 | |
| 934 | if(0 == object->data.seek_table.points) { |
| 935 | FLAC__ASSERT(object->data.seek_table.num_points == 0); |
| 936 | if(0 == new_num_points) |
| 937 | return true; |
| 938 | else if(0 == (object->data.seek_table.points = seekpoint_array_new_(new_num_points))) |
| 939 | return false; |
| 940 | } |
| 941 | else { |
| 942 | const size_t old_size = object->data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint); |
| 943 | const size_t new_size = new_num_points * sizeof(FLAC__StreamMetadata_SeekPoint); |
| 944 | |
| 945 | /* overflow check */ |
| 946 | if((size_t)new_num_points > SIZE_MAX / sizeof(FLAC__StreamMetadata_SeekPoint)) |
| 947 | return false; |
| 948 | |
| 949 | FLAC__ASSERT(object->data.seek_table.num_points > 0); |
| 950 | |
| 951 | if(new_size == 0) { |
| 952 | free(object->data.seek_table.points); |
| 953 | object->data.seek_table.points = 0; |
| 954 | } |
| 955 | else if(0 == (object->data.seek_table.points = (FLAC__StreamMetadata_SeekPoint*)realloc(object->data.seek_table.points, new_size))) |
| 956 | return false; |
| 957 | |
| 958 | /* if growing, set new elements to placeholders */ |
| 959 | if(new_size > old_size) { |
| 960 | unsigned i; |
| 961 | for(i = object->data.seek_table.num_points; i < new_num_points; i++) { |
| 962 | object->data.seek_table.points[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER; |
| 963 | object->data.seek_table.points[i].stream_offset = 0; |
| 964 | object->data.seek_table.points[i].frame_samples = 0; |
| 965 | } |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | object->data.seek_table.num_points = new_num_points; |
| 970 | |
| 971 | seektable_calculate_length_(object); |
| 972 | return true; |
| 973 | } |
| 974 | |
| 975 | FLAC_API void FLAC__metadata_object_seektable_set_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point) |
| 976 | { |
| 977 | FLAC__ASSERT(0 != object); |
| 978 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE); |
| 979 | FLAC__ASSERT(point_num < object->data.seek_table.num_points); |
| 980 | |
| 981 | object->data.seek_table.points[point_num] = point; |
| 982 | } |
| 983 | |
| 984 | FLAC_API FLAC__bool FLAC__metadata_object_seektable_insert_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point) |
| 985 | { |
| 986 | int i; |
| 987 | |
| 988 | FLAC__ASSERT(0 != object); |
| 989 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE); |
| 990 | FLAC__ASSERT(point_num <= object->data.seek_table.num_points); |
| 991 | |
| 992 | if(!FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points+1)) |
| 993 | return false; |
| 994 | |
| 995 | /* move all points >= point_num forward one space */ |
| 996 | for(i = (int)object->data.seek_table.num_points-1; i > (int)point_num; i--) |
| 997 | object->data.seek_table.points[i] = object->data.seek_table.points[i-1]; |
| 998 | |
| 999 | FLAC__metadata_object_seektable_set_point(object, point_num, point); |
| 1000 | seektable_calculate_length_(object); |
| 1001 | return true; |
| 1002 | } |
| 1003 | |
| 1004 | FLAC_API FLAC__bool FLAC__metadata_object_seektable_delete_point(FLAC__StreamMetadata *object, unsigned point_num) |
| 1005 | { |
| 1006 | unsigned i; |
| 1007 | |
| 1008 | FLAC__ASSERT(0 != object); |
| 1009 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE); |
| 1010 | FLAC__ASSERT(point_num < object->data.seek_table.num_points); |
| 1011 | |
| 1012 | /* move all points > point_num backward one space */ |
| 1013 | for(i = point_num; i < object->data.seek_table.num_points-1; i++) |
| 1014 | object->data.seek_table.points[i] = object->data.seek_table.points[i+1]; |
| 1015 | |
| 1016 | return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points-1); |
| 1017 | } |
| 1018 | |
| 1019 | FLAC_API FLAC__bool FLAC__metadata_object_seektable_is_legal(const FLAC__StreamMetadata *object) |
| 1020 | { |
| 1021 | FLAC__ASSERT(0 != object); |
| 1022 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE); |
| 1023 | |
| 1024 | return FLAC__format_seektable_is_legal(&object->data.seek_table); |
| 1025 | } |
| 1026 | |
| 1027 | FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_placeholders(FLAC__StreamMetadata *object, unsigned num) |
| 1028 | { |
| 1029 | FLAC__ASSERT(0 != object); |
| 1030 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE); |
| 1031 | |
| 1032 | if(num > 0) |
| 1033 | /* WATCHOUT: we rely on the fact that growing the array adds PLACEHOLDERS at the end */ |
| 1034 | return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points + num); |
| 1035 | else |
| 1036 | return true; |
| 1037 | } |
| 1038 | |
| 1039 | FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_point(FLAC__StreamMetadata *object, FLAC__uint64 sample_number) |
| 1040 | { |
| 1041 | FLAC__StreamMetadata_SeekTable *seek_table; |
| 1042 | |
| 1043 | FLAC__ASSERT(0 != object); |
| 1044 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE); |
| 1045 | |
| 1046 | seek_table = &object->data.seek_table; |
| 1047 | |
| 1048 | if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + 1)) |
| 1049 | return false; |
| 1050 | |
| 1051 | seek_table->points[seek_table->num_points - 1].sample_number = sample_number; |
| 1052 | seek_table->points[seek_table->num_points - 1].stream_offset = 0; |
| 1053 | seek_table->points[seek_table->num_points - 1].frame_samples = 0; |
| 1054 | |
| 1055 | return true; |
| 1056 | } |
| 1057 | |
| 1058 | FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_points(FLAC__StreamMetadata *object, FLAC__uint64 sample_numbers[], unsigned num) |
| 1059 | { |
| 1060 | FLAC__ASSERT(0 != object); |
| 1061 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE); |
| 1062 | FLAC__ASSERT(0 != sample_numbers || num == 0); |
| 1063 | |
| 1064 | if(num > 0) { |
| 1065 | FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table; |
| 1066 | unsigned i, j; |
| 1067 | |
| 1068 | i = seek_table->num_points; |
| 1069 | |
| 1070 | if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num)) |
| 1071 | return false; |
| 1072 | |
| 1073 | for(j = 0; j < num; i++, j++) { |
| 1074 | seek_table->points[i].sample_number = sample_numbers[j]; |
| 1075 | seek_table->points[i].stream_offset = 0; |
| 1076 | seek_table->points[i].frame_samples = 0; |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | return true; |
| 1081 | } |
| 1082 | |
| 1083 | FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_spaced_points(FLAC__StreamMetadata *object, unsigned num, FLAC__uint64 total_samples) |
| 1084 | { |
| 1085 | FLAC__ASSERT(0 != object); |
| 1086 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE); |
| 1087 | FLAC__ASSERT(total_samples > 0); |
| 1088 | |
| 1089 | if(num > 0 && total_samples > 0) { |
| 1090 | FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table; |
| 1091 | unsigned i, j; |
| 1092 | |
| 1093 | i = seek_table->num_points; |
| 1094 | |
| 1095 | if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num)) |
| 1096 | return false; |
| 1097 | |
| 1098 | for(j = 0; j < num; i++, j++) { |
| 1099 | seek_table->points[i].sample_number = total_samples * (FLAC__uint64)j / (FLAC__uint64)num; |
| 1100 | seek_table->points[i].stream_offset = 0; |
| 1101 | seek_table->points[i].frame_samples = 0; |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | return true; |
| 1106 | } |
| 1107 | |
| 1108 | FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_spaced_points_by_samples(FLAC__StreamMetadata *object, unsigned samples, FLAC__uint64 total_samples) |
| 1109 | { |
| 1110 | FLAC__ASSERT(0 != object); |
| 1111 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE); |
| 1112 | FLAC__ASSERT(samples > 0); |
| 1113 | FLAC__ASSERT(total_samples > 0); |
| 1114 | |
| 1115 | if(samples > 0 && total_samples > 0) { |
| 1116 | FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table; |
| 1117 | unsigned i, j; |
| 1118 | FLAC__uint64 num, sample; |
| 1119 | |
| 1120 | num = 1 + total_samples / samples; /* 1+ for the first sample at 0 */ |
| 1121 | /* now account for the fact that we don't place a seekpoint at "total_samples" since samples are number from 0: */ |
| 1122 | if(total_samples % samples == 0) |
| 1123 | num--; |
| 1124 | |
| 1125 | i = seek_table->num_points; |
| 1126 | |
| 1127 | if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + (unsigned)num)) |
| 1128 | return false; |
| 1129 | |
| 1130 | sample = 0; |
| 1131 | for(j = 0; j < num; i++, j++, sample += samples) { |
| 1132 | seek_table->points[i].sample_number = sample; |
| 1133 | seek_table->points[i].stream_offset = 0; |
| 1134 | seek_table->points[i].frame_samples = 0; |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | return true; |
| 1139 | } |
| 1140 | |
| 1141 | FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_sort(FLAC__StreamMetadata *object, FLAC__bool compact) |
| 1142 | { |
| 1143 | unsigned unique; |
| 1144 | |
| 1145 | FLAC__ASSERT(0 != object); |
| 1146 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE); |
| 1147 | |
| 1148 | unique = FLAC__format_seektable_sort(&object->data.seek_table); |
| 1149 | |
| 1150 | return !compact || FLAC__metadata_object_seektable_resize_points(object, unique); |
| 1151 | } |
| 1152 | |
| 1153 | FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_set_vendor_string(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy) |
| 1154 | { |
| 1155 | if(!FLAC__format_vorbiscomment_entry_value_is_legal(entry.entry, entry.length)) |
| 1156 | return false; |
| 1157 | return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.vendor_string, &entry, copy); |
| 1158 | } |
| 1159 | |
| 1160 | FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__StreamMetadata *object, unsigned new_num_comments) |
| 1161 | { |
| 1162 | FLAC__ASSERT(0 != object); |
| 1163 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
| 1164 | |
| 1165 | if(0 == object->data.vorbis_comment.comments) { |
| 1166 | FLAC__ASSERT(object->data.vorbis_comment.num_comments == 0); |
| 1167 | if(0 == new_num_comments) |
| 1168 | return true; |
| 1169 | else if(0 == (object->data.vorbis_comment.comments = vorbiscomment_entry_array_new_(new_num_comments))) |
| 1170 | return false; |
| 1171 | } |
| 1172 | else { |
| 1173 | const size_t old_size = object->data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry); |
| 1174 | const size_t new_size = new_num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry); |
| 1175 | |
| 1176 | /* overflow check */ |
| 1177 | if((size_t)new_num_comments > SIZE_MAX / sizeof(FLAC__StreamMetadata_VorbisComment_Entry)) |
| 1178 | return false; |
| 1179 | |
| 1180 | FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0); |
| 1181 | |
| 1182 | /* if shrinking, free the truncated entries */ |
| 1183 | if(new_num_comments < object->data.vorbis_comment.num_comments) { |
| 1184 | unsigned i; |
| 1185 | for(i = new_num_comments; i < object->data.vorbis_comment.num_comments; i++) |
| 1186 | if(0 != object->data.vorbis_comment.comments[i].entry) |
| 1187 | free(object->data.vorbis_comment.comments[i].entry); |
| 1188 | } |
| 1189 | |
| 1190 | if(new_size == 0) { |
| 1191 | free(object->data.vorbis_comment.comments); |
| 1192 | object->data.vorbis_comment.comments = 0; |
| 1193 | } |
| 1194 | else if(0 == (object->data.vorbis_comment.comments = (FLAC__StreamMetadata_VorbisComment_Entry*)realloc(object->data.vorbis_comment.comments, new_size))) |
| 1195 | return false; |
| 1196 | |
| 1197 | /* if growing, zero all the length/pointers of new elements */ |
| 1198 | if(new_size > old_size) |
| 1199 | memset(object->data.vorbis_comment.comments + object->data.vorbis_comment.num_comments, 0, new_size - old_size); |
| 1200 | } |
| 1201 | |
| 1202 | object->data.vorbis_comment.num_comments = new_num_comments; |
| 1203 | |
| 1204 | vorbiscomment_calculate_length_(object); |
| 1205 | return true; |
| 1206 | } |
| 1207 | |
| 1208 | FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_set_comment(FLAC__StreamMetadata *object, unsigned comment_num, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy) |
| 1209 | { |
| 1210 | FLAC__ASSERT(0 != object); |
| 1211 | FLAC__ASSERT(comment_num < object->data.vorbis_comment.num_comments); |
| 1212 | |
| 1213 | if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length)) |
| 1214 | return false; |
| 1215 | return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.comments[comment_num], &entry, copy); |
| 1216 | } |
| 1217 | |
| 1218 | FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_insert_comment(FLAC__StreamMetadata *object, unsigned comment_num, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy) |
| 1219 | { |
| 1220 | FLAC__StreamMetadata_VorbisComment *vc; |
| 1221 | |
| 1222 | FLAC__ASSERT(0 != object); |
| 1223 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
| 1224 | FLAC__ASSERT(comment_num <= object->data.vorbis_comment.num_comments); |
| 1225 | |
| 1226 | if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length)) |
| 1227 | return false; |
| 1228 | |
| 1229 | vc = &object->data.vorbis_comment; |
| 1230 | |
| 1231 | if(!FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments+1)) |
| 1232 | return false; |
| 1233 | |
| 1234 | /* move all comments >= comment_num forward one space */ |
| 1235 | memmove(&vc->comments[comment_num+1], &vc->comments[comment_num], sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*(vc->num_comments-1-comment_num)); |
| 1236 | vc->comments[comment_num].length = 0; |
| 1237 | vc->comments[comment_num].entry = 0; |
| 1238 | |
| 1239 | return FLAC__metadata_object_vorbiscomment_set_comment(object, comment_num, entry, copy); |
| 1240 | } |
| 1241 | |
| 1242 | FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_append_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy) |
| 1243 | { |
| 1244 | FLAC__ASSERT(0 != object); |
| 1245 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
| 1246 | return FLAC__metadata_object_vorbiscomment_insert_comment(object, object->data.vorbis_comment.num_comments, entry, copy); |
| 1247 | } |
| 1248 | |
| 1249 | FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_replace_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool all, FLAC__bool copy) |
| 1250 | { |
| 1251 | FLAC__ASSERT(0 != entry.entry && entry.length > 0); |
| 1252 | |
| 1253 | if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length)) |
| 1254 | return false; |
| 1255 | |
| 1256 | { |
| 1257 | int i; |
| 1258 | size_t field_name_length; |
| 1259 | const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length); |
| 1260 | |
| 1261 | FLAC__ASSERT(0 != eq); |
| 1262 | |
| 1263 | if(0 == eq) |
| 1264 | return false; /* double protection */ |
| 1265 | |
| 1266 | field_name_length = eq-entry.entry; |
| 1267 | |
| 1268 | if((i = vorbiscomment_find_entry_from_(object, 0, (const char *)entry.entry, field_name_length)) >= 0) { |
| 1269 | unsigned index = (unsigned)i; |
| 1270 | if(!FLAC__metadata_object_vorbiscomment_set_comment(object, index, entry, copy)) |
| 1271 | return false; |
| 1272 | if(all && (index+1 < object->data.vorbis_comment.num_comments)) { |
| 1273 | for(i = vorbiscomment_find_entry_from_(object, index+1, (const char *)entry.entry, field_name_length); i >= 0; ) { |
| 1274 | if(!FLAC__metadata_object_vorbiscomment_delete_comment(object, (unsigned)i)) |
| 1275 | return false; |
| 1276 | if((unsigned)i < object->data.vorbis_comment.num_comments) |
| 1277 | i = vorbiscomment_find_entry_from_(object, (unsigned)i, (const char *)entry.entry, field_name_length); |
| 1278 | else |
| 1279 | i = -1; |
| 1280 | } |
| 1281 | } |
| 1282 | return true; |
| 1283 | } |
| 1284 | else |
| 1285 | return FLAC__metadata_object_vorbiscomment_append_comment(object, entry, copy); |
| 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_delete_comment(FLAC__StreamMetadata *object, unsigned comment_num) |
| 1290 | { |
| 1291 | FLAC__StreamMetadata_VorbisComment *vc; |
| 1292 | |
| 1293 | FLAC__ASSERT(0 != object); |
| 1294 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
| 1295 | FLAC__ASSERT(comment_num < object->data.vorbis_comment.num_comments); |
| 1296 | |
| 1297 | vc = &object->data.vorbis_comment; |
| 1298 | |
| 1299 | /* free the comment at comment_num */ |
| 1300 | if(0 != vc->comments[comment_num].entry) |
| 1301 | free(vc->comments[comment_num].entry); |
| 1302 | |
| 1303 | /* move all comments > comment_num backward one space */ |
| 1304 | memmove(&vc->comments[comment_num], &vc->comments[comment_num+1], sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*(vc->num_comments-comment_num-1)); |
| 1305 | vc->comments[vc->num_comments-1].length = 0; |
| 1306 | vc->comments[vc->num_comments-1].entry = 0; |
| 1307 | |
| 1308 | return FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments-1); |
| 1309 | } |
| 1310 | |
| 1311 | FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(FLAC__StreamMetadata_VorbisComment_Entry *entry, const char *field_name, const char *field_value) |
| 1312 | { |
| 1313 | FLAC__ASSERT(0 != entry); |
| 1314 | FLAC__ASSERT(0 != field_name); |
| 1315 | FLAC__ASSERT(0 != field_value); |
| 1316 | |
| 1317 | if(!FLAC__format_vorbiscomment_entry_name_is_legal(field_name)) |
| 1318 | return false; |
| 1319 | if(!FLAC__format_vorbiscomment_entry_value_is_legal((const FLAC__byte *)field_value, (unsigned)(-1))) |
| 1320 | return false; |
| 1321 | |
| 1322 | { |
| 1323 | const size_t nn = strlen(field_name); |
| 1324 | const size_t nv = strlen(field_value); |
| 1325 | entry->length = nn + 1 /*=*/ + nv; |
| 1326 | if(0 == (entry->entry = (FLAC__byte*)safe_malloc_add_4op_(nn, /*+*/1, /*+*/nv, /*+*/1))) |
| 1327 | return false; |
| 1328 | memcpy(entry->entry, field_name, nn); |
| 1329 | entry->entry[nn] = '='; |
| 1330 | memcpy(entry->entry+nn+1, field_value, nv); |
| 1331 | entry->entry[entry->length] = '\0'; |
| 1332 | } |
| 1333 | |
| 1334 | return true; |
| 1335 | } |
| 1336 | |
| 1337 | FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_to_name_value_pair(const FLAC__StreamMetadata_VorbisComment_Entry entry, char **field_name, char **field_value) |
| 1338 | { |
| 1339 | FLAC__ASSERT(0 != entry.entry && entry.length > 0); |
| 1340 | FLAC__ASSERT(0 != field_name); |
| 1341 | FLAC__ASSERT(0 != field_value); |
| 1342 | |
| 1343 | if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length)) |
| 1344 | return false; |
| 1345 | |
| 1346 | { |
| 1347 | const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length); |
| 1348 | const size_t nn = eq-entry.entry; |
| 1349 | const size_t nv = entry.length-nn-1; /* -1 for the '=' */ |
| 1350 | FLAC__ASSERT(0 != eq); |
| 1351 | if(0 == eq) |
| 1352 | return false; /* double protection */ |
| 1353 | if(0 == (*field_name = (char*)safe_malloc_add_2op_(nn, /*+*/1))) |
| 1354 | return false; |
| 1355 | if(0 == (*field_value = (char*)safe_malloc_add_2op_(nv, /*+*/1))) { |
| 1356 | free(*field_name); |
| 1357 | return false; |
| 1358 | } |
| 1359 | memcpy(*field_name, entry.entry, nn); |
| 1360 | memcpy(*field_value, entry.entry+nn+1, nv); |
| 1361 | (*field_name)[nn] = '\0'; |
| 1362 | (*field_value)[nv] = '\0'; |
| 1363 | } |
| 1364 | |
| 1365 | return true; |
| 1366 | } |
| 1367 | |
| 1368 | FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_matches(const FLAC__StreamMetadata_VorbisComment_Entry entry, const char *field_name, unsigned field_name_length) |
| 1369 | { |
| 1370 | FLAC__ASSERT(0 != entry.entry && entry.length > 0); |
| 1371 | { |
| 1372 | const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length); |
| 1373 | #if defined _MSC_VER || defined __BORLANDC__ || defined __MINGW32__ || defined __EMX__ |
| 1374 | #define FLAC__STRNCASECMP strnicmp |
| 1375 | #else |
| 1376 | #define FLAC__STRNCASECMP strncasecmp |
| 1377 | #endif |
| 1378 | return (0 != eq && (unsigned)(eq-entry.entry) == field_name_length && 0 == FLAC__STRNCASECMP(field_name, (const char *)entry.entry, field_name_length)); |
| 1379 | #undef FLAC__STRNCASECMP |
| 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | FLAC_API int FLAC__metadata_object_vorbiscomment_find_entry_from(const FLAC__StreamMetadata *object, unsigned offset, const char *field_name) |
| 1384 | { |
| 1385 | FLAC__ASSERT(0 != field_name); |
| 1386 | |
| 1387 | return vorbiscomment_find_entry_from_(object, offset, field_name, strlen(field_name)); |
| 1388 | } |
| 1389 | |
| 1390 | FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entry_matching(FLAC__StreamMetadata *object, const char *field_name) |
| 1391 | { |
| 1392 | const unsigned field_name_length = strlen(field_name); |
| 1393 | unsigned i; |
| 1394 | |
| 1395 | FLAC__ASSERT(0 != object); |
| 1396 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
| 1397 | |
| 1398 | for(i = 0; i < object->data.vorbis_comment.num_comments; i++) { |
| 1399 | if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments[i], field_name, field_name_length)) { |
| 1400 | if(!FLAC__metadata_object_vorbiscomment_delete_comment(object, i)) |
| 1401 | return -1; |
| 1402 | else |
| 1403 | return 1; |
| 1404 | } |
| 1405 | } |
| 1406 | |
| 1407 | return 0; |
| 1408 | } |
| 1409 | |
| 1410 | FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entries_matching(FLAC__StreamMetadata *object, const char *field_name) |
| 1411 | { |
| 1412 | FLAC__bool ok = true; |
| 1413 | unsigned matching = 0; |
| 1414 | const unsigned field_name_length = strlen(field_name); |
| 1415 | int i; |
| 1416 | |
| 1417 | FLAC__ASSERT(0 != object); |
| 1418 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
| 1419 | |
| 1420 | /* must delete from end to start otherwise it will interfere with our iteration */ |
| 1421 | for(i = (int)object->data.vorbis_comment.num_comments - 1; ok && i >= 0; i--) { |
| 1422 | if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments[i], field_name, field_name_length)) { |
| 1423 | matching++; |
| 1424 | ok &= FLAC__metadata_object_vorbiscomment_delete_comment(object, (unsigned)i); |
| 1425 | } |
| 1426 | } |
| 1427 | |
| 1428 | return ok? (int)matching : -1; |
| 1429 | } |
| 1430 | |
| 1431 | FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_new(void) |
| 1432 | { |
| 1433 | return (FLAC__StreamMetadata_CueSheet_Track*)calloc(1, sizeof(FLAC__StreamMetadata_CueSheet_Track)); |
| 1434 | } |
| 1435 | |
| 1436 | FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_clone(const FLAC__StreamMetadata_CueSheet_Track *object) |
| 1437 | { |
| 1438 | FLAC__StreamMetadata_CueSheet_Track *to; |
| 1439 | |
| 1440 | FLAC__ASSERT(0 != object); |
| 1441 | |
| 1442 | if(0 != (to = FLAC__metadata_object_cuesheet_track_new())) { |
| 1443 | if(!copy_track_(to, object)) { |
| 1444 | FLAC__metadata_object_cuesheet_track_delete(to); |
| 1445 | return 0; |
| 1446 | } |
| 1447 | } |
| 1448 | |
| 1449 | return to; |
| 1450 | } |
| 1451 | |
| 1452 | void FLAC__metadata_object_cuesheet_track_delete_data(FLAC__StreamMetadata_CueSheet_Track *object) |
| 1453 | { |
| 1454 | FLAC__ASSERT(0 != object); |
| 1455 | |
| 1456 | if(0 != object->indices) { |
| 1457 | FLAC__ASSERT(object->num_indices > 0); |
| 1458 | free(object->indices); |
| 1459 | } |
| 1460 | } |
| 1461 | |
| 1462 | FLAC_API void FLAC__metadata_object_cuesheet_track_delete(FLAC__StreamMetadata_CueSheet_Track *object) |
| 1463 | { |
| 1464 | FLAC__metadata_object_cuesheet_track_delete_data(object); |
| 1465 | free(object); |
| 1466 | } |
| 1467 | |
| 1468 | FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_resize_indices(FLAC__StreamMetadata *object, unsigned track_num, unsigned new_num_indices) |
| 1469 | { |
| 1470 | FLAC__StreamMetadata_CueSheet_Track *track; |
| 1471 | FLAC__ASSERT(0 != object); |
| 1472 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET); |
| 1473 | FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks); |
| 1474 | |
| 1475 | track = &object->data.cue_sheet.tracks[track_num]; |
| 1476 | |
| 1477 | if(0 == track->indices) { |
| 1478 | FLAC__ASSERT(track->num_indices == 0); |
| 1479 | if(0 == new_num_indices) |
| 1480 | return true; |
| 1481 | else if(0 == (track->indices = cuesheet_track_index_array_new_(new_num_indices))) |
| 1482 | return false; |
| 1483 | } |
| 1484 | else { |
| 1485 | const size_t old_size = track->num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index); |
| 1486 | const size_t new_size = new_num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index); |
| 1487 | |
| 1488 | /* overflow check */ |
| 1489 | if((size_t)new_num_indices > SIZE_MAX / sizeof(FLAC__StreamMetadata_CueSheet_Index)) |
| 1490 | return false; |
| 1491 | |
| 1492 | FLAC__ASSERT(track->num_indices > 0); |
| 1493 | |
| 1494 | if(new_size == 0) { |
| 1495 | free(track->indices); |
| 1496 | track->indices = 0; |
| 1497 | } |
| 1498 | else if(0 == (track->indices = (FLAC__StreamMetadata_CueSheet_Index*)realloc(track->indices, new_size))) |
| 1499 | return false; |
| 1500 | |
| 1501 | /* if growing, zero all the lengths/pointers of new elements */ |
| 1502 | if(new_size > old_size) |
| 1503 | memset(track->indices + track->num_indices, 0, new_size - old_size); |
| 1504 | } |
| 1505 | |
| 1506 | track->num_indices = new_num_indices; |
| 1507 | |
| 1508 | cuesheet_calculate_length_(object); |
| 1509 | return true; |
| 1510 | } |
| 1511 | |
| 1512 | FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_insert_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num, FLAC__StreamMetadata_CueSheet_Index index) |
| 1513 | { |
| 1514 | FLAC__StreamMetadata_CueSheet_Track *track; |
| 1515 | |
| 1516 | FLAC__ASSERT(0 != object); |
| 1517 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET); |
| 1518 | FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks); |
| 1519 | FLAC__ASSERT(index_num <= object->data.cue_sheet.tracks[track_num].num_indices); |
| 1520 | |
| 1521 | track = &object->data.cue_sheet.tracks[track_num]; |
| 1522 | |
| 1523 | if(!FLAC__metadata_object_cuesheet_track_resize_indices(object, track_num, track->num_indices+1)) |
| 1524 | return false; |
| 1525 | |
| 1526 | /* move all indices >= index_num forward one space */ |
| 1527 | memmove(&track->indices[index_num+1], &track->indices[index_num], sizeof(FLAC__StreamMetadata_CueSheet_Index)*(track->num_indices-1-index_num)); |
| 1528 | |
| 1529 | track->indices[index_num] = index; |
| 1530 | cuesheet_calculate_length_(object); |
| 1531 | return true; |
| 1532 | } |
| 1533 | |
| 1534 | FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_insert_blank_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num) |
| 1535 | { |
| 1536 | FLAC__StreamMetadata_CueSheet_Index index; |
| 1537 | memset(&index, 0, sizeof(index)); |
| 1538 | return FLAC__metadata_object_cuesheet_track_insert_index(object, track_num, index_num, index); |
| 1539 | } |
| 1540 | |
| 1541 | FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_delete_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num) |
| 1542 | { |
| 1543 | FLAC__StreamMetadata_CueSheet_Track *track; |
| 1544 | |
| 1545 | FLAC__ASSERT(0 != object); |
| 1546 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET); |
| 1547 | FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks); |
| 1548 | FLAC__ASSERT(index_num < object->data.cue_sheet.tracks[track_num].num_indices); |
| 1549 | |
| 1550 | track = &object->data.cue_sheet.tracks[track_num]; |
| 1551 | |
| 1552 | /* move all indices > index_num backward one space */ |
| 1553 | memmove(&track->indices[index_num], &track->indices[index_num+1], sizeof(FLAC__StreamMetadata_CueSheet_Index)*(track->num_indices-index_num-1)); |
| 1554 | |
| 1555 | FLAC__metadata_object_cuesheet_track_resize_indices(object, track_num, track->num_indices-1); |
| 1556 | cuesheet_calculate_length_(object); |
| 1557 | return true; |
| 1558 | } |
| 1559 | |
| 1560 | FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_resize_tracks(FLAC__StreamMetadata *object, unsigned new_num_tracks) |
| 1561 | { |
| 1562 | FLAC__ASSERT(0 != object); |
| 1563 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET); |
| 1564 | |
| 1565 | if(0 == object->data.cue_sheet.tracks) { |
| 1566 | FLAC__ASSERT(object->data.cue_sheet.num_tracks == 0); |
| 1567 | if(0 == new_num_tracks) |
| 1568 | return true; |
| 1569 | else if(0 == (object->data.cue_sheet.tracks = cuesheet_track_array_new_(new_num_tracks))) |
| 1570 | return false; |
| 1571 | } |
| 1572 | else { |
| 1573 | const size_t old_size = object->data.cue_sheet.num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track); |
| 1574 | const size_t new_size = new_num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track); |
| 1575 | |
| 1576 | /* overflow check */ |
| 1577 | if((size_t)new_num_tracks > SIZE_MAX / sizeof(FLAC__StreamMetadata_CueSheet_Track)) |
| 1578 | return false; |
| 1579 | |
| 1580 | FLAC__ASSERT(object->data.cue_sheet.num_tracks > 0); |
| 1581 | |
| 1582 | /* if shrinking, free the truncated entries */ |
| 1583 | if(new_num_tracks < object->data.cue_sheet.num_tracks) { |
| 1584 | unsigned i; |
| 1585 | for(i = new_num_tracks; i < object->data.cue_sheet.num_tracks; i++) |
| 1586 | if(0 != object->data.cue_sheet.tracks[i].indices) |
| 1587 | free(object->data.cue_sheet.tracks[i].indices); |
| 1588 | } |
| 1589 | |
| 1590 | if(new_size == 0) { |
| 1591 | free(object->data.cue_sheet.tracks); |
| 1592 | object->data.cue_sheet.tracks = 0; |
| 1593 | } |
| 1594 | else if(0 == (object->data.cue_sheet.tracks = (FLAC__StreamMetadata_CueSheet_Track*)realloc(object->data.cue_sheet.tracks, new_size))) |
| 1595 | return false; |
| 1596 | |
| 1597 | /* if growing, zero all the lengths/pointers of new elements */ |
| 1598 | if(new_size > old_size) |
| 1599 | memset(object->data.cue_sheet.tracks + object->data.cue_sheet.num_tracks, 0, new_size - old_size); |
| 1600 | } |
| 1601 | |
| 1602 | object->data.cue_sheet.num_tracks = new_num_tracks; |
| 1603 | |
| 1604 | cuesheet_calculate_length_(object); |
| 1605 | return true; |
| 1606 | } |
| 1607 | |
| 1608 | FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_set_track(FLAC__StreamMetadata *object, unsigned track_num, FLAC__StreamMetadata_CueSheet_Track *track, FLAC__bool copy) |
| 1609 | { |
| 1610 | FLAC__ASSERT(0 != object); |
| 1611 | FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks); |
| 1612 | |
| 1613 | return cuesheet_set_track_(object, object->data.cue_sheet.tracks + track_num, track, copy); |
| 1614 | } |
| 1615 | |
| 1616 | FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_insert_track(FLAC__StreamMetadata *object, unsigned track_num, FLAC__StreamMetadata_CueSheet_Track *track, FLAC__bool copy) |
| 1617 | { |
| 1618 | FLAC__StreamMetadata_CueSheet *cs; |
| 1619 | |
| 1620 | FLAC__ASSERT(0 != object); |
| 1621 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET); |
| 1622 | FLAC__ASSERT(track_num <= object->data.cue_sheet.num_tracks); |
| 1623 | |
| 1624 | cs = &object->data.cue_sheet; |
| 1625 | |
| 1626 | if(!FLAC__metadata_object_cuesheet_resize_tracks(object, cs->num_tracks+1)) |
| 1627 | return false; |
| 1628 | |
| 1629 | /* move all tracks >= track_num forward one space */ |
| 1630 | memmove(&cs->tracks[track_num+1], &cs->tracks[track_num], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(cs->num_tracks-1-track_num)); |
| 1631 | cs->tracks[track_num].num_indices = 0; |
| 1632 | cs->tracks[track_num].indices = 0; |
| 1633 | |
| 1634 | return FLAC__metadata_object_cuesheet_set_track(object, track_num, track, copy); |
| 1635 | } |
| 1636 | |
| 1637 | FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_insert_blank_track(FLAC__StreamMetadata *object, unsigned track_num) |
| 1638 | { |
| 1639 | FLAC__StreamMetadata_CueSheet_Track track; |
| 1640 | memset(&track, 0, sizeof(track)); |
| 1641 | return FLAC__metadata_object_cuesheet_insert_track(object, track_num, &track, /*copy=*/false); |
| 1642 | } |
| 1643 | |
| 1644 | FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_delete_track(FLAC__StreamMetadata *object, unsigned track_num) |
| 1645 | { |
| 1646 | FLAC__StreamMetadata_CueSheet *cs; |
| 1647 | |
| 1648 | FLAC__ASSERT(0 != object); |
| 1649 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET); |
| 1650 | FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks); |
| 1651 | |
| 1652 | cs = &object->data.cue_sheet; |
| 1653 | |
| 1654 | /* free the track at track_num */ |
| 1655 | if(0 != cs->tracks[track_num].indices) |
| 1656 | free(cs->tracks[track_num].indices); |
| 1657 | |
| 1658 | /* move all tracks > track_num backward one space */ |
| 1659 | memmove(&cs->tracks[track_num], &cs->tracks[track_num+1], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(cs->num_tracks-track_num-1)); |
| 1660 | cs->tracks[cs->num_tracks-1].num_indices = 0; |
| 1661 | cs->tracks[cs->num_tracks-1].indices = 0; |
| 1662 | |
| 1663 | return FLAC__metadata_object_cuesheet_resize_tracks(object, cs->num_tracks-1); |
| 1664 | } |
| 1665 | |
| 1666 | FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_is_legal(const FLAC__StreamMetadata *object, FLAC__bool check_cd_da_subset, const char **violation) |
| 1667 | { |
| 1668 | FLAC__ASSERT(0 != object); |
| 1669 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET); |
| 1670 | |
| 1671 | return FLAC__format_cuesheet_is_legal(&object->data.cue_sheet, check_cd_da_subset, violation); |
| 1672 | } |
| 1673 | |
| 1674 | static FLAC__uint64 get_index_01_offset_(const FLAC__StreamMetadata_CueSheet *cs, unsigned track) |
| 1675 | { |
| 1676 | if (track >= (cs->num_tracks-1) || cs->tracks[track].num_indices < 1) |
| 1677 | return 0; |
| 1678 | else if (cs->tracks[track].indices[0].number == 1) |
| 1679 | return cs->tracks[track].indices[0].offset + cs->tracks[track].offset + cs->lead_in; |
| 1680 | else if (cs->tracks[track].num_indices < 2) |
| 1681 | return 0; |
| 1682 | else if (cs->tracks[track].indices[1].number == 1) |
| 1683 | return cs->tracks[track].indices[1].offset + cs->tracks[track].offset + cs->lead_in; |
| 1684 | else |
| 1685 | return 0; |
| 1686 | } |
| 1687 | |
| 1688 | static FLAC__uint32 cddb_add_digits_(FLAC__uint32 x) |
| 1689 | { |
| 1690 | FLAC__uint32 n = 0; |
| 1691 | while (x) { |
| 1692 | n += (x%10); |
| 1693 | x /= 10; |
| 1694 | } |
| 1695 | return n; |
| 1696 | } |
| 1697 | |
| 1698 | /*@@@@add to tests*/ |
| 1699 | FLAC_API FLAC__uint32 FLAC__metadata_object_cuesheet_calculate_cddb_id(const FLAC__StreamMetadata *object) |
| 1700 | { |
| 1701 | const FLAC__StreamMetadata_CueSheet *cs; |
| 1702 | |
| 1703 | FLAC__ASSERT(0 != object); |
| 1704 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET); |
| 1705 | |
| 1706 | cs = &object->data.cue_sheet; |
| 1707 | |
| 1708 | if (cs->num_tracks < 2) /* need at least one real track and the lead-out track */ |
| 1709 | return 0; |
| 1710 | |
| 1711 | { |
| 1712 | FLAC__uint32 i, length, sum = 0; |
| 1713 | for (i = 0; i < (cs->num_tracks-1); i++) /* -1 to avoid counting the lead-out */ |
| 1714 | sum += cddb_add_digits_((FLAC__uint32)(get_index_01_offset_(cs, i) / 44100)); |
| 1715 | length = (FLAC__uint32)((cs->tracks[cs->num_tracks-1].offset+cs->lead_in) / 44100) - (FLAC__uint32)(get_index_01_offset_(cs, 0) / 44100); |
| 1716 | |
| 1717 | return (sum % 0xFF) << 24 | length << 8 | (FLAC__uint32)(cs->num_tracks-1); |
| 1718 | } |
| 1719 | } |
| 1720 | |
| 1721 | FLAC_API FLAC__bool FLAC__metadata_object_picture_set_mime_type(FLAC__StreamMetadata *object, char *mime_type, FLAC__bool copy) |
| 1722 | { |
| 1723 | char *old; |
| 1724 | size_t old_length, new_length; |
| 1725 | |
| 1726 | FLAC__ASSERT(0 != object); |
| 1727 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE); |
| 1728 | FLAC__ASSERT(0 != mime_type); |
| 1729 | |
| 1730 | old = object->data.picture.mime_type; |
| 1731 | old_length = old? strlen(old) : 0; |
| 1732 | new_length = strlen(mime_type); |
| 1733 | |
| 1734 | /* do the copy first so that if we fail we leave the object untouched */ |
| 1735 | if(copy) { |
| 1736 | if(new_length >= SIZE_MAX) /* overflow check */ |
| 1737 | return false; |
| 1738 | if(!copy_bytes_((FLAC__byte**)(&object->data.picture.mime_type), (FLAC__byte*)mime_type, new_length+1)) |
| 1739 | return false; |
| 1740 | } |
| 1741 | else { |
| 1742 | object->data.picture.mime_type = mime_type; |
| 1743 | } |
| 1744 | |
| 1745 | if(0 != old) |
| 1746 | free(old); |
| 1747 | |
| 1748 | object->length -= old_length; |
| 1749 | object->length += new_length; |
| 1750 | return true; |
| 1751 | } |
| 1752 | |
| 1753 | FLAC_API FLAC__bool FLAC__metadata_object_picture_set_description(FLAC__StreamMetadata *object, FLAC__byte *description, FLAC__bool copy) |
| 1754 | { |
| 1755 | FLAC__byte *old; |
| 1756 | size_t old_length, new_length; |
| 1757 | |
| 1758 | FLAC__ASSERT(0 != object); |
| 1759 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE); |
| 1760 | FLAC__ASSERT(0 != description); |
| 1761 | |
| 1762 | old = object->data.picture.description; |
| 1763 | old_length = old? strlen((const char *)old) : 0; |
| 1764 | new_length = strlen((const char *)description); |
| 1765 | |
| 1766 | /* do the copy first so that if we fail we leave the object untouched */ |
| 1767 | if(copy) { |
| 1768 | if(new_length >= SIZE_MAX) /* overflow check */ |
| 1769 | return false; |
| 1770 | if(!copy_bytes_(&object->data.picture.description, description, new_length+1)) |
| 1771 | return false; |
| 1772 | } |
| 1773 | else { |
| 1774 | object->data.picture.description = description; |
| 1775 | } |
| 1776 | |
| 1777 | if(0 != old) |
| 1778 | free(old); |
| 1779 | |
| 1780 | object->length -= old_length; |
| 1781 | object->length += new_length; |
| 1782 | return true; |
| 1783 | } |
| 1784 | |
| 1785 | FLAC_API FLAC__bool FLAC__metadata_object_picture_set_data(FLAC__StreamMetadata *object, FLAC__byte *data, FLAC__uint32 length, FLAC__bool copy) |
| 1786 | { |
| 1787 | FLAC__byte *old; |
| 1788 | |
| 1789 | FLAC__ASSERT(0 != object); |
| 1790 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE); |
| 1791 | FLAC__ASSERT((0 != data && length > 0) || (0 == data && length == 0 && copy == false)); |
| 1792 | |
| 1793 | old = object->data.picture.data; |
| 1794 | |
| 1795 | /* do the copy first so that if we fail we leave the object untouched */ |
| 1796 | if(copy) { |
| 1797 | if(!copy_bytes_(&object->data.picture.data, data, length)) |
| 1798 | return false; |
| 1799 | } |
| 1800 | else { |
| 1801 | object->data.picture.data = data; |
| 1802 | } |
| 1803 | |
| 1804 | if(0 != old) |
| 1805 | free(old); |
| 1806 | |
| 1807 | object->length -= object->data.picture.data_length; |
| 1808 | object->data.picture.data_length = length; |
| 1809 | object->length += length; |
| 1810 | return true; |
| 1811 | } |
| 1812 | |
| 1813 | FLAC_API FLAC__bool FLAC__metadata_object_picture_is_legal(const FLAC__StreamMetadata *object, const char **violation) |
| 1814 | { |
| 1815 | FLAC__ASSERT(0 != object); |
| 1816 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE); |
| 1817 | |
| 1818 | return FLAC__format_picture_is_legal(&object->data.picture, violation); |
| 1819 | } |
src/lib/libflac/libflac/metadata_iterators.c
| r0 | r14135 | |
| 1 | /* libFLAC - Free Lossless Audio Codec library |
| 2 | * Copyright (C) 2001,2002,2003,2004,2005,2006,2007 Josh Coalson |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * |
| 8 | * - Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * - Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * - Neither the name of the Xiph.org Foundation nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived from |
| 17 | * this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 26 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 27 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #if HAVE_CONFIG_H |
| 33 | # include <config.h> |
| 34 | #endif |
| 35 | |
| 36 | #include <errno.h> |
| 37 | #include <stdio.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <string.h> |
| 40 | |
| 41 | #if defined _MSC_VER || defined __BORLANDC__ || defined __MINGW32__ |
| 42 | #if defined __BORLANDC__ |
| 43 | #include <utime.h> /* for utime() */ |
| 44 | #else |
| 45 | #include <sys/utime.h> /* for utime() */ |
| 46 | #endif |
| 47 | #include <io.h> /* for chmod() */ |
| 48 | #include <sys/types.h> /* for off_t */ |
| 49 | #if _MSC_VER <= 1600 || defined __BORLANDC__ /* @@@ [2G limit] */ |
| 50 | #define fseeko fseek |
| 51 | #define ftello ftell |
| 52 | #endif |
| 53 | #else |
| 54 | #include <sys/types.h> /* some flavors of BSD (like OS X) require this to get time_t */ |
| 55 | #include <utime.h> /* for utime() */ |
| 56 | #include <unistd.h> /* for chown(), unlink() */ |
| 57 | #endif |
| 58 | #include <sys/stat.h> /* for stat(), maybe chmod() */ |
| 59 | |
| 60 | #include "private/metadata.h" |
| 61 | |
| 62 | #include "flac/assert.h" |
| 63 | #include "flac/stream_decoder.h" |
| 64 | #include "share/alloc.h" |
| 65 | |
| 66 | #ifdef max |
| 67 | #undef max |
| 68 | #endif |
| 69 | #define max(a,b) ((a)>(b)?(a):(b)) |
| 70 | #ifdef min |
| 71 | #undef min |
| 72 | #endif |
| 73 | #define min(a,b) ((a)<(b)?(a):(b)) |
| 74 | |
| 75 | |
| 76 | /**************************************************************************** |
| 77 | * |
| 78 | * Local function declarations |
| 79 | * |
| 80 | ***************************************************************************/ |
| 81 | |
| 82 | static void pack_uint32_(FLAC__uint32 val, FLAC__byte *b, unsigned bytes); |
| 83 | static void pack_uint32_little_endian_(FLAC__uint32 val, FLAC__byte *b, unsigned bytes); |
| 84 | static void pack_uint64_(FLAC__uint64 val, FLAC__byte *b, unsigned bytes); |
| 85 | static FLAC__uint32 unpack_uint32_(FLAC__byte *b, unsigned bytes); |
| 86 | static FLAC__uint32 unpack_uint32_little_endian_(FLAC__byte *b, unsigned bytes); |
| 87 | static FLAC__uint64 unpack_uint64_(FLAC__byte *b, unsigned bytes); |
| 88 | |
| 89 | static FLAC__bool read_metadata_block_header_(FLAC__Metadata_SimpleIterator *iterator); |
| 90 | static FLAC__bool read_metadata_block_data_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block); |
| 91 | static FLAC__bool read_metadata_block_header_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__bool *is_last, FLAC__MetadataType *type, unsigned *length); |
| 92 | static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata *block); |
| 93 | static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_streaminfo_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_StreamInfo *block); |
| 94 | static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_padding_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata_Padding *block, unsigned block_length); |
| 95 | static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_application_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Application *block, unsigned block_length); |
| 96 | static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_seektable_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_SeekTable *block, unsigned block_length); |
| 97 | static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_entry_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_VorbisComment_Entry *entry); |
| 98 | static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_VorbisComment *block); |
| 99 | static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cuesheet_track_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_CueSheet_Track *track); |
| 100 | static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cuesheet_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_CueSheet *block); |
| 101 | static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_picture_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Picture *block); |
| 102 | static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_unknown_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Unknown *block, unsigned block_length); |
| 103 | |
| 104 | static FLAC__bool write_metadata_block_header_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block); |
| 105 | static FLAC__bool write_metadata_block_data_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block); |
| 106 | static FLAC__bool write_metadata_block_header_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata *block); |
| 107 | static FLAC__bool write_metadata_block_data_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata *block); |
| 108 | static FLAC__bool write_metadata_block_data_streaminfo_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_StreamInfo *block); |
| 109 | static FLAC__bool write_metadata_block_data_padding_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Padding *block, unsigned block_length); |
| 110 | static FLAC__bool write_metadata_block_data_application_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Application *block, unsigned block_length); |
| 111 | static FLAC__bool write_metadata_block_data_seektable_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_SeekTable *block); |
| 112 | static FLAC__bool write_metadata_block_data_vorbis_comment_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_VorbisComment *block); |
| 113 | static FLAC__bool write_metadata_block_data_cuesheet_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_CueSheet *block); |
| 114 | static FLAC__bool write_metadata_block_data_picture_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Picture *block); |
| 115 | static FLAC__bool write_metadata_block_data_unknown_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Unknown *block, unsigned block_length); |
| 116 | |
| 117 | static FLAC__bool write_metadata_block_stationary_(FLAC__Metadata_SimpleIterator *iterator, const FLAC__StreamMetadata *block); |
| 118 | static FLAC__bool write_metadata_block_stationary_with_padding_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, unsigned padding_length, FLAC__bool padding_is_last); |
| 119 | static FLAC__bool rewrite_whole_file_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool append); |
| 120 | |
| 121 | static void simple_iterator_push_(FLAC__Metadata_SimpleIterator *iterator); |
| 122 | static FLAC__bool simple_iterator_pop_(FLAC__Metadata_SimpleIterator *iterator); |
| 123 | |
| 124 | static unsigned seek_to_first_metadata_block_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb); |
| 125 | static unsigned seek_to_first_metadata_block_(FILE *f); |
| 126 | |
| 127 | static FLAC__bool simple_iterator_copy_file_prefix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, FLAC__bool append); |
| 128 | static FLAC__bool simple_iterator_copy_file_postfix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, int fixup_is_last_code, off_t fixup_is_last_flag_offset, FLAC__bool backup); |
| 129 | |
| 130 | static FLAC__bool copy_n_bytes_from_file_(FILE *file, FILE *tempfile, off_t bytes, FLAC__Metadata_SimpleIteratorStatus *status); |
| 131 | static FLAC__bool copy_n_bytes_from_file_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOHandle temp_handle, FLAC__IOCallback_Write temp_write_cb, off_t bytes, FLAC__Metadata_SimpleIteratorStatus *status); |
| 132 | static FLAC__bool copy_remaining_bytes_from_file_(FILE *file, FILE *tempfile, FLAC__Metadata_SimpleIteratorStatus *status); |
| 133 | static FLAC__bool copy_remaining_bytes_from_file_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Eof eof_cb, FLAC__IOHandle temp_handle, FLAC__IOCallback_Write temp_write_cb, FLAC__Metadata_SimpleIteratorStatus *status); |
| 134 | |
| 135 | static FLAC__bool open_tempfile_(const char *filename, const char *tempfile_path_prefix, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status); |
| 136 | static FLAC__bool transport_tempfile_(const char *filename, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status); |
| 137 | static void cleanup_tempfile_(FILE **tempfile, char **tempfilename); |
| 138 | |
| 139 | static FLAC__bool get_file_stats_(const char *filename, struct stat *stats); |
| 140 | static void set_file_stats_(const char *filename, struct stat *stats); |
| 141 | |
| 142 | static int fseek_wrapper_(FLAC__IOHandle handle, FLAC__int64 offset, int whence); |
| 143 | static FLAC__int64 ftell_wrapper_(FLAC__IOHandle handle); |
| 144 | |
| 145 | static FLAC__Metadata_ChainStatus get_equivalent_status_(FLAC__Metadata_SimpleIteratorStatus status); |
| 146 | |
| 147 | |
| 148 | #ifdef FLAC__VALGRIND_TESTING |
| 149 | static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) |
| 150 | { |
| 151 | size_t ret = fwrite(ptr, size, nmemb, stream); |
| 152 | if(!ferror(stream)) |
| 153 | fflush(stream); |
| 154 | return ret; |
| 155 | } |
| 156 | #else |
| 157 | #define local__fwrite fwrite |
| 158 | #endif |
| 159 | |
| 160 | /**************************************************************************** |
| 161 | * |
| 162 | * Level 0 implementation |
| 163 | * |
| 164 | ***************************************************************************/ |
| 165 | |
| 166 | static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data); |
| 167 | static void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data); |
| 168 | static void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data); |
| 169 | |
| 170 | typedef struct { |
| 171 | FLAC__bool got_error; |
| 172 | FLAC__StreamMetadata *object; |
| 173 | } level0_client_data; |
| 174 | |
| 175 | static FLAC__StreamMetadata *get_one_metadata_block_(const char *filename, FLAC__MetadataType type) |
| 176 | { |
| 177 | level0_client_data cd; |
| 178 | FLAC__StreamDecoder *decoder; |
| 179 | |
| 180 | FLAC__ASSERT(0 != filename); |
| 181 | |
| 182 | cd.got_error = false; |
| 183 | cd.object = 0; |
| 184 | |
| 185 | decoder = FLAC__stream_decoder_new(); |
| 186 | |
| 187 | if(0 == decoder) |
| 188 | return 0; |
| 189 | |
| 190 | FLAC__stream_decoder_set_md5_checking(decoder, false); |
| 191 | FLAC__stream_decoder_set_metadata_ignore_all(decoder); |
| 192 | FLAC__stream_decoder_set_metadata_respond(decoder, type); |
| 193 | |
| 194 | if(FLAC__stream_decoder_init_file(decoder, filename, write_callback_, metadata_callback_, error_callback_, &cd) != FLAC__STREAM_DECODER_INIT_STATUS_OK || cd.got_error) { |
| 195 | (void)FLAC__stream_decoder_finish(decoder); |
| 196 | FLAC__stream_decoder_delete(decoder); |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder) || cd.got_error) { |
| 201 | (void)FLAC__stream_decoder_finish(decoder); |
| 202 | FLAC__stream_decoder_delete(decoder); |
| 203 | if(0 != cd.object) |
| 204 | FLAC__metadata_object_delete(cd.object); |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | (void)FLAC__stream_decoder_finish(decoder); |
| 209 | FLAC__stream_decoder_delete(decoder); |
| 210 | |
| 211 | return cd.object; |
| 212 | } |
| 213 | |
| 214 | FLAC_API FLAC__bool FLAC__metadata_get_streaminfo(const char *filename, FLAC__StreamMetadata *streaminfo) |
| 215 | { |
| 216 | FLAC__StreamMetadata *object; |
| 217 | |
| 218 | FLAC__ASSERT(0 != filename); |
| 219 | FLAC__ASSERT(0 != streaminfo); |
| 220 | |
| 221 | object = get_one_metadata_block_(filename, FLAC__METADATA_TYPE_STREAMINFO); |
| 222 | |
| 223 | if (object) { |
| 224 | /* can just copy the contents since STREAMINFO has no internal structure */ |
| 225 | *streaminfo = *object; |
| 226 | FLAC__metadata_object_delete(object); |
| 227 | return true; |
| 228 | } |
| 229 | else { |
| 230 | return false; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | FLAC_API FLAC__bool FLAC__metadata_get_tags(const char *filename, FLAC__StreamMetadata **tags) |
| 235 | { |
| 236 | FLAC__ASSERT(0 != filename); |
| 237 | FLAC__ASSERT(0 != tags); |
| 238 | |
| 239 | *tags = get_one_metadata_block_(filename, FLAC__METADATA_TYPE_VORBIS_COMMENT); |
| 240 | |
| 241 | return 0 != *tags; |
| 242 | } |
| 243 | |
| 244 | FLAC_API FLAC__bool FLAC__metadata_get_cuesheet(const char *filename, FLAC__StreamMetadata **cuesheet) |
| 245 | { |
| 246 | FLAC__ASSERT(0 != filename); |
| 247 | FLAC__ASSERT(0 != cuesheet); |
| 248 | |
| 249 | *cuesheet = get_one_metadata_block_(filename, FLAC__METADATA_TYPE_CUESHEET); |
| 250 | |
| 251 | return 0 != *cuesheet; |
| 252 | } |
| 253 | |
| 254 | FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data) |
| 255 | { |
| 256 | (void)decoder, (void)frame, (void)buffer, (void)client_data; |
| 257 | |
| 258 | return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; |
| 259 | } |
| 260 | |
| 261 | void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data) |
| 262 | { |
| 263 | level0_client_data *cd = (level0_client_data *)client_data; |
| 264 | (void)decoder; |
| 265 | |
| 266 | /* |
| 267 | * we assume we only get here when the one metadata block we were |
| 268 | * looking for was passed to us |
| 269 | */ |
| 270 | if(!cd->got_error && 0 == cd->object) { |
| 271 | if(0 == (cd->object = FLAC__metadata_object_clone(metadata))) |
| 272 | cd->got_error = true; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) |
| 277 | { |
| 278 | level0_client_data *cd = (level0_client_data *)client_data; |
| 279 | (void)decoder; |
| 280 | |
| 281 | if(status != FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC) |
| 282 | cd->got_error = true; |
| 283 | } |
| 284 | |
| 285 | FLAC_API FLAC__bool FLAC__metadata_get_picture(const char *filename, FLAC__StreamMetadata **picture, FLAC__StreamMetadata_Picture_Type type, const char *mime_type, const FLAC__byte *description, unsigned max_width, unsigned max_height, unsigned max_depth, unsigned max_colors) |
| 286 | { |
| 287 | FLAC__Metadata_SimpleIterator *it; |
| 288 | FLAC__uint64 max_area_seen = 0; |
| 289 | FLAC__uint64 max_depth_seen = 0; |
| 290 | |
| 291 | FLAC__ASSERT(0 != filename); |
| 292 | FLAC__ASSERT(0 != picture); |
| 293 | |
| 294 | *picture = 0; |
| 295 | |
| 296 | it = FLAC__metadata_simple_iterator_new(); |
| 297 | if(0 == it) |
| 298 | return false; |
| 299 | if(!FLAC__metadata_simple_iterator_init(it, filename, /*read_only=*/true, /*preserve_file_stats=*/true)) { |
| 300 | FLAC__metadata_simple_iterator_delete(it); |
| 301 | return false; |
| 302 | } |
| 303 | do { |
| 304 | if(FLAC__metadata_simple_iterator_get_block_type(it) == FLAC__METADATA_TYPE_PICTURE) { |
| 305 | FLAC__StreamMetadata *obj = FLAC__metadata_simple_iterator_get_block(it); |
| 306 | FLAC__uint64 area = (FLAC__uint64)obj->data.picture.width * (FLAC__uint64)obj->data.picture.height; |
| 307 | /* check constraints */ |
| 308 | if( |
| 309 | (type == (FLAC__StreamMetadata_Picture_Type)(-1) || type == obj->data.picture.type) && |
| 310 | (mime_type == 0 || !strcmp(mime_type, obj->data.picture.mime_type)) && |
| 311 | (description == 0 || !strcmp((const char *)description, (const char *)obj->data.picture.description)) && |
| 312 | obj->data.picture.width <= max_width && |
| 313 | obj->data.picture.height <= max_height && |
| 314 | obj->data.picture.depth <= max_depth && |
| 315 | obj->data.picture.colors <= max_colors && |
| 316 | (area > max_area_seen || (area == max_area_seen && obj->data.picture.depth > max_depth_seen)) |
| 317 | ) { |
| 318 | if(*picture) |
| 319 | FLAC__metadata_object_delete(*picture); |
| 320 | *picture = obj; |
| 321 | max_area_seen = area; |
| 322 | max_depth_seen = obj->data.picture.depth; |
| 323 | } |
| 324 | else { |
| 325 | FLAC__metadata_object_delete(obj); |
| 326 | } |
| 327 | } |
| 328 | } while(FLAC__metadata_simple_iterator_next(it)); |
| 329 | |
| 330 | FLAC__metadata_simple_iterator_delete(it); |
| 331 | |
| 332 | return (0 != *picture); |
| 333 | } |
| 334 | |
| 335 | |
| 336 | /**************************************************************************** |
| 337 | * |
| 338 | * Level 1 implementation |
| 339 | * |
| 340 | ***************************************************************************/ |
| 341 | |
| 342 | #define SIMPLE_ITERATOR_MAX_PUSH_DEPTH (1+4) |
| 343 | /* 1 for initial offset, +4 for our own personal use */ |
| 344 | |
| 345 | struct FLAC__Metadata_SimpleIterator { |
| 346 | FILE *file; |
| 347 | char *filename, *tempfile_path_prefix; |
| 348 | struct stat stats; |
| 349 | FLAC__bool has_stats; |
| 350 | FLAC__bool is_writable; |
| 351 | FLAC__Metadata_SimpleIteratorStatus status; |
| 352 | off_t offset[SIMPLE_ITERATOR_MAX_PUSH_DEPTH]; |
| 353 | off_t first_offset; /* this is the offset to the STREAMINFO block */ |
| 354 | unsigned depth; |
| 355 | /* this is the metadata block header of the current block we are pointing to: */ |
| 356 | FLAC__bool is_last; |
| 357 | FLAC__MetadataType type; |
| 358 | unsigned length; |
| 359 | }; |
| 360 | |
| 361 | FLAC_API const char * const FLAC__Metadata_SimpleIteratorStatusString[] = { |
| 362 | "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK", |
| 363 | "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT", |
| 364 | "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE", |
| 365 | "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE", |
| 366 | "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE", |
| 367 | "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA", |
| 368 | "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR", |
| 369 | "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR", |
| 370 | "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR", |
| 371 | "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR", |
| 372 | "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_UNLINK_ERROR", |
| 373 | "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR", |
| 374 | "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_INTERNAL_ERROR" |
| 375 | }; |
| 376 | |
| 377 | |
| 378 | FLAC_API FLAC__Metadata_SimpleIterator *FLAC__metadata_simple_iterator_new(void) |
| 379 | { |
| 380 | FLAC__Metadata_SimpleIterator *iterator = (FLAC__Metadata_SimpleIterator*)calloc(1, sizeof(FLAC__Metadata_SimpleIterator)); |
| 381 | |
| 382 | if(0 != iterator) { |
| 383 | iterator->file = 0; |
| 384 | iterator->filename = 0; |
| 385 | iterator->tempfile_path_prefix = 0; |
| 386 | iterator->has_stats = false; |
| 387 | iterator->is_writable = false; |
| 388 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 389 | iterator->first_offset = iterator->offset[0] = -1; |
| 390 | iterator->depth = 0; |
| 391 | } |
| 392 | |
| 393 | return iterator; |
| 394 | } |
| 395 | |
| 396 | static void simple_iterator_free_guts_(FLAC__Metadata_SimpleIterator *iterator) |
| 397 | { |
| 398 | FLAC__ASSERT(0 != iterator); |
| 399 | |
| 400 | if(0 != iterator->file) { |
| 401 | fclose(iterator->file); |
| 402 | iterator->file = 0; |
| 403 | if(iterator->has_stats) |
| 404 | set_file_stats_(iterator->filename, &iterator->stats); |
| 405 | } |
| 406 | if(0 != iterator->filename) { |
| 407 | free(iterator->filename); |
| 408 | iterator->filename = 0; |
| 409 | } |
| 410 | if(0 != iterator->tempfile_path_prefix) { |
| 411 | free(iterator->tempfile_path_prefix); |
| 412 | iterator->tempfile_path_prefix = 0; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | FLAC_API void FLAC__metadata_simple_iterator_delete(FLAC__Metadata_SimpleIterator *iterator) |
| 417 | { |
| 418 | FLAC__ASSERT(0 != iterator); |
| 419 | |
| 420 | simple_iterator_free_guts_(iterator); |
| 421 | free(iterator); |
| 422 | } |
| 423 | |
| 424 | FLAC_API FLAC__Metadata_SimpleIteratorStatus FLAC__metadata_simple_iterator_status(FLAC__Metadata_SimpleIterator *iterator) |
| 425 | { |
| 426 | FLAC__Metadata_SimpleIteratorStatus status; |
| 427 | |
| 428 | FLAC__ASSERT(0 != iterator); |
| 429 | |
| 430 | status = iterator->status; |
| 431 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 432 | return status; |
| 433 | } |
| 434 | |
| 435 | static FLAC__bool simple_iterator_prime_input_(FLAC__Metadata_SimpleIterator *iterator, FLAC__bool read_only) |
| 436 | { |
| 437 | unsigned ret; |
| 438 | |
| 439 | FLAC__ASSERT(0 != iterator); |
| 440 | |
| 441 | if(read_only || 0 == (iterator->file = fopen(iterator->filename, "r+b"))) { |
| 442 | iterator->is_writable = false; |
| 443 | if(read_only || errno == EACCES) { |
| 444 | if(0 == (iterator->file = fopen(iterator->filename, "rb"))) { |
| 445 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE; |
| 446 | return false; |
| 447 | } |
| 448 | } |
| 449 | else { |
| 450 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE; |
| 451 | return false; |
| 452 | } |
| 453 | } |
| 454 | else { |
| 455 | iterator->is_writable = true; |
| 456 | } |
| 457 | |
| 458 | ret = seek_to_first_metadata_block_(iterator->file); |
| 459 | switch(ret) { |
| 460 | case 0: |
| 461 | iterator->depth = 0; |
| 462 | iterator->first_offset = iterator->offset[iterator->depth] = ftello(iterator->file); |
| 463 | return read_metadata_block_header_(iterator); |
| 464 | case 1: |
| 465 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 466 | return false; |
| 467 | case 2: |
| 468 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 469 | return false; |
| 470 | case 3: |
| 471 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE; |
| 472 | return false; |
| 473 | default: |
| 474 | FLAC__ASSERT(0); |
| 475 | return false; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | #if 0 |
| 480 | @@@ If we decide to finish implementing this, put this comment back in metadata.h |
| 481 | /* |
| 482 | * The 'tempfile_path_prefix' allows you to specify a directory where |
| 483 | * tempfiles should go. Remember that if your metadata edits cause the |
| 484 | * FLAC file to grow, the entire file will have to be rewritten. If |
| 485 | * 'tempfile_path_prefix' is NULL, the temp file will be written in the |
| 486 | * same directory as the original FLAC file. This makes replacing the |
| 487 | * original with the tempfile fast but requires extra space in the same |
| 488 | * partition for the tempfile. If space is a problem, you can pass a |
| 489 | * directory name belonging to a different partition in |
| 490 | * 'tempfile_path_prefix'. Note that you should use the forward slash |
| 491 | * '/' as the directory separator. A trailing slash is not needed; it |
| 492 | * will be added automatically. |
| 493 | */ |
| 494 | FLAC__bool FLAC__metadata_simple_iterator_init(FLAC__Metadata_SimpleIterator *iterator, const char *filename, FLAC__bool preserve_file_stats, const char *tempfile_path_prefix); |
| 495 | #endif |
| 496 | |
| 497 | FLAC_API FLAC__bool FLAC__metadata_simple_iterator_init(FLAC__Metadata_SimpleIterator *iterator, const char *filename, FLAC__bool read_only, FLAC__bool preserve_file_stats) |
| 498 | { |
| 499 | const char *tempfile_path_prefix = 0; /*@@@ search for comments near 'rename(...)' for what it will take to finish implementing this */ |
| 500 | |
| 501 | FLAC__ASSERT(0 != iterator); |
| 502 | FLAC__ASSERT(0 != filename); |
| 503 | |
| 504 | simple_iterator_free_guts_(iterator); |
| 505 | |
| 506 | if(!read_only && preserve_file_stats) |
| 507 | iterator->has_stats = get_file_stats_(filename, &iterator->stats); |
| 508 | |
| 509 | if(0 == (iterator->filename = strdup(filename))) { |
| 510 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 511 | return false; |
| 512 | } |
| 513 | if(0 != tempfile_path_prefix && 0 == (iterator->tempfile_path_prefix = strdup(tempfile_path_prefix))) { |
| 514 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 515 | return false; |
| 516 | } |
| 517 | |
| 518 | return simple_iterator_prime_input_(iterator, read_only); |
| 519 | } |
| 520 | |
| 521 | FLAC_API FLAC__bool FLAC__metadata_simple_iterator_is_writable(const FLAC__Metadata_SimpleIterator *iterator) |
| 522 | { |
| 523 | FLAC__ASSERT(0 != iterator); |
| 524 | FLAC__ASSERT(0 != iterator->file); |
| 525 | |
| 526 | return iterator->is_writable; |
| 527 | } |
| 528 | |
| 529 | FLAC_API FLAC__bool FLAC__metadata_simple_iterator_next(FLAC__Metadata_SimpleIterator *iterator) |
| 530 | { |
| 531 | FLAC__ASSERT(0 != iterator); |
| 532 | FLAC__ASSERT(0 != iterator->file); |
| 533 | |
| 534 | if(iterator->is_last) |
| 535 | return false; |
| 536 | |
| 537 | if(0 != fseeko(iterator->file, iterator->length, SEEK_CUR)) { |
| 538 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 539 | return false; |
| 540 | } |
| 541 | |
| 542 | iterator->offset[iterator->depth] = ftello(iterator->file); |
| 543 | |
| 544 | return read_metadata_block_header_(iterator); |
| 545 | } |
| 546 | |
| 547 | FLAC_API FLAC__bool FLAC__metadata_simple_iterator_prev(FLAC__Metadata_SimpleIterator *iterator) |
| 548 | { |
| 549 | off_t this_offset; |
| 550 | |
| 551 | FLAC__ASSERT(0 != iterator); |
| 552 | FLAC__ASSERT(0 != iterator->file); |
| 553 | |
| 554 | if(iterator->offset[iterator->depth] == iterator->first_offset) |
| 555 | return false; |
| 556 | |
| 557 | if(0 != fseeko(iterator->file, iterator->first_offset, SEEK_SET)) { |
| 558 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 559 | return false; |
| 560 | } |
| 561 | this_offset = iterator->first_offset; |
| 562 | if(!read_metadata_block_header_(iterator)) |
| 563 | return false; |
| 564 | |
| 565 | /* we ignore any error from ftello() and catch it in fseeko() */ |
| 566 | while(ftello(iterator->file) + (off_t)iterator->length < iterator->offset[iterator->depth]) { |
| 567 | if(0 != fseeko(iterator->file, iterator->length, SEEK_CUR)) { |
| 568 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 569 | return false; |
| 570 | } |
| 571 | this_offset = ftello(iterator->file); |
| 572 | if(!read_metadata_block_header_(iterator)) |
| 573 | return false; |
| 574 | } |
| 575 | |
| 576 | iterator->offset[iterator->depth] = this_offset; |
| 577 | |
| 578 | return true; |
| 579 | } |
| 580 | |
| 581 | /*@@@@add to tests*/ |
| 582 | FLAC_API FLAC__bool FLAC__metadata_simple_iterator_is_last(const FLAC__Metadata_SimpleIterator *iterator) |
| 583 | { |
| 584 | FLAC__ASSERT(0 != iterator); |
| 585 | FLAC__ASSERT(0 != iterator->file); |
| 586 | |
| 587 | return iterator->is_last; |
| 588 | } |
| 589 | |
| 590 | /*@@@@add to tests*/ |
| 591 | FLAC_API off_t FLAC__metadata_simple_iterator_get_block_offset(const FLAC__Metadata_SimpleIterator *iterator) |
| 592 | { |
| 593 | FLAC__ASSERT(0 != iterator); |
| 594 | FLAC__ASSERT(0 != iterator->file); |
| 595 | |
| 596 | return iterator->offset[iterator->depth]; |
| 597 | } |
| 598 | |
| 599 | FLAC_API FLAC__MetadataType FLAC__metadata_simple_iterator_get_block_type(const FLAC__Metadata_SimpleIterator *iterator) |
| 600 | { |
| 601 | FLAC__ASSERT(0 != iterator); |
| 602 | FLAC__ASSERT(0 != iterator->file); |
| 603 | |
| 604 | return iterator->type; |
| 605 | } |
| 606 | |
| 607 | /*@@@@add to tests*/ |
| 608 | FLAC_API unsigned FLAC__metadata_simple_iterator_get_block_length(const FLAC__Metadata_SimpleIterator *iterator) |
| 609 | { |
| 610 | FLAC__ASSERT(0 != iterator); |
| 611 | FLAC__ASSERT(0 != iterator->file); |
| 612 | |
| 613 | return iterator->length; |
| 614 | } |
| 615 | |
| 616 | /*@@@@add to tests*/ |
| 617 | FLAC_API FLAC__bool FLAC__metadata_simple_iterator_get_application_id(FLAC__Metadata_SimpleIterator *iterator, FLAC__byte *id) |
| 618 | { |
| 619 | const unsigned id_bytes = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8; |
| 620 | |
| 621 | FLAC__ASSERT(0 != iterator); |
| 622 | FLAC__ASSERT(0 != iterator->file); |
| 623 | FLAC__ASSERT(0 != id); |
| 624 | |
| 625 | if(iterator->type != FLAC__METADATA_TYPE_APPLICATION) { |
| 626 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT; |
| 627 | return false; |
| 628 | } |
| 629 | |
| 630 | if(fread(id, 1, id_bytes, iterator->file) != id_bytes) { |
| 631 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 632 | return false; |
| 633 | } |
| 634 | |
| 635 | /* back up */ |
| 636 | if(0 != fseeko(iterator->file, -((int)id_bytes), SEEK_CUR)) { |
| 637 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 638 | return false; |
| 639 | } |
| 640 | |
| 641 | return true; |
| 642 | } |
| 643 | |
| 644 | FLAC_API FLAC__StreamMetadata *FLAC__metadata_simple_iterator_get_block(FLAC__Metadata_SimpleIterator *iterator) |
| 645 | { |
| 646 | FLAC__StreamMetadata *block = FLAC__metadata_object_new(iterator->type); |
| 647 | |
| 648 | FLAC__ASSERT(0 != iterator); |
| 649 | FLAC__ASSERT(0 != iterator->file); |
| 650 | |
| 651 | if(0 != block) { |
| 652 | block->is_last = iterator->is_last; |
| 653 | block->length = iterator->length; |
| 654 | |
| 655 | if(!read_metadata_block_data_(iterator, block)) { |
| 656 | FLAC__metadata_object_delete(block); |
| 657 | return 0; |
| 658 | } |
| 659 | |
| 660 | /* back up to the beginning of the block data to stay consistent */ |
| 661 | if(0 != fseeko(iterator->file, iterator->offset[iterator->depth] + FLAC__STREAM_METADATA_HEADER_LENGTH, SEEK_SET)) { |
| 662 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 663 | FLAC__metadata_object_delete(block); |
| 664 | return 0; |
| 665 | } |
| 666 | } |
| 667 | else |
| 668 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 669 | |
| 670 | return block; |
| 671 | } |
| 672 | |
| 673 | FLAC_API FLAC__bool FLAC__metadata_simple_iterator_set_block(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool use_padding) |
| 674 | { |
| 675 | FLAC__ASSERT_DECLARATION(off_t debug_target_offset = iterator->offset[iterator->depth];) |
| 676 | FLAC__bool ret; |
| 677 | |
| 678 | FLAC__ASSERT(0 != iterator); |
| 679 | FLAC__ASSERT(0 != iterator->file); |
| 680 | FLAC__ASSERT(0 != block); |
| 681 | |
| 682 | if(!iterator->is_writable) { |
| 683 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE; |
| 684 | return false; |
| 685 | } |
| 686 | |
| 687 | if(iterator->type == FLAC__METADATA_TYPE_STREAMINFO || block->type == FLAC__METADATA_TYPE_STREAMINFO) { |
| 688 | if(iterator->type != block->type) { |
| 689 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT; |
| 690 | return false; |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | block->is_last = iterator->is_last; |
| 695 | |
| 696 | if(iterator->length == block->length) |
| 697 | return write_metadata_block_stationary_(iterator, block); |
| 698 | else if(iterator->length > block->length) { |
| 699 | if(use_padding && iterator->length >= FLAC__STREAM_METADATA_HEADER_LENGTH + block->length) { |
| 700 | ret = write_metadata_block_stationary_with_padding_(iterator, block, iterator->length - FLAC__STREAM_METADATA_HEADER_LENGTH - block->length, block->is_last); |
| 701 | FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset); |
| 702 | FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH); |
| 703 | return ret; |
| 704 | } |
| 705 | else { |
| 706 | ret = rewrite_whole_file_(iterator, block, /*append=*/false); |
| 707 | FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset); |
| 708 | FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH); |
| 709 | return ret; |
| 710 | } |
| 711 | } |
| 712 | else /* iterator->length < block->length */ { |
| 713 | unsigned padding_leftover = 0; |
| 714 | FLAC__bool padding_is_last = false; |
| 715 | if(use_padding) { |
| 716 | /* first see if we can even use padding */ |
| 717 | if(iterator->is_last) { |
| 718 | use_padding = false; |
| 719 | } |
| 720 | else { |
| 721 | const unsigned extra_padding_bytes_required = block->length - iterator->length; |
| 722 | simple_iterator_push_(iterator); |
| 723 | if(!FLAC__metadata_simple_iterator_next(iterator)) { |
| 724 | (void)simple_iterator_pop_(iterator); |
| 725 | return false; |
| 726 | } |
| 727 | if(iterator->type != FLAC__METADATA_TYPE_PADDING) { |
| 728 | use_padding = false; |
| 729 | } |
| 730 | else { |
| 731 | if(FLAC__STREAM_METADATA_HEADER_LENGTH + iterator->length == extra_padding_bytes_required) { |
| 732 | padding_leftover = 0; |
| 733 | block->is_last = iterator->is_last; |
| 734 | } |
| 735 | else if(iterator->length < extra_padding_bytes_required) |
| 736 | use_padding = false; |
| 737 | else { |
| 738 | padding_leftover = FLAC__STREAM_METADATA_HEADER_LENGTH + iterator->length - extra_padding_bytes_required; |
| 739 | padding_is_last = iterator->is_last; |
| 740 | block->is_last = false; |
| 741 | } |
| 742 | } |
| 743 | if(!simple_iterator_pop_(iterator)) |
| 744 | return false; |
| 745 | } |
| 746 | } |
| 747 | if(use_padding) { |
| 748 | if(padding_leftover == 0) { |
| 749 | ret = write_metadata_block_stationary_(iterator, block); |
| 750 | FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset); |
| 751 | FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH); |
| 752 | return ret; |
| 753 | } |
| 754 | else { |
| 755 | FLAC__ASSERT(padding_leftover >= FLAC__STREAM_METADATA_HEADER_LENGTH); |
| 756 | ret = write_metadata_block_stationary_with_padding_(iterator, block, padding_leftover - FLAC__STREAM_METADATA_HEADER_LENGTH, padding_is_last); |
| 757 | FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset); |
| 758 | FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH); |
| 759 | return ret; |
| 760 | } |
| 761 | } |
| 762 | else { |
| 763 | ret = rewrite_whole_file_(iterator, block, /*append=*/false); |
| 764 | FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset); |
| 765 | FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH); |
| 766 | return ret; |
| 767 | } |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | FLAC_API FLAC__bool FLAC__metadata_simple_iterator_insert_block_after(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool use_padding) |
| 772 | { |
| 773 | unsigned padding_leftover = 0; |
| 774 | FLAC__bool padding_is_last = false; |
| 775 | |
| 776 | FLAC__ASSERT_DECLARATION(off_t debug_target_offset = iterator->offset[iterator->depth] + FLAC__STREAM_METADATA_HEADER_LENGTH + iterator->length;) |
| 777 | FLAC__bool ret; |
| 778 | |
| 779 | FLAC__ASSERT(0 != iterator); |
| 780 | FLAC__ASSERT(0 != iterator->file); |
| 781 | FLAC__ASSERT(0 != block); |
| 782 | |
| 783 | if(!iterator->is_writable) |
| 784 | return false; |
| 785 | |
| 786 | if(block->type == FLAC__METADATA_TYPE_STREAMINFO) { |
| 787 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT; |
| 788 | return false; |
| 789 | } |
| 790 | |
| 791 | block->is_last = iterator->is_last; |
| 792 | |
| 793 | if(use_padding) { |
| 794 | /* first see if we can even use padding */ |
| 795 | if(iterator->is_last) { |
| 796 | use_padding = false; |
| 797 | } |
| 798 | else { |
| 799 | simple_iterator_push_(iterator); |
| 800 | if(!FLAC__metadata_simple_iterator_next(iterator)) { |
| 801 | (void)simple_iterator_pop_(iterator); |
| 802 | return false; |
| 803 | } |
| 804 | if(iterator->type != FLAC__METADATA_TYPE_PADDING) { |
| 805 | use_padding = false; |
| 806 | } |
| 807 | else { |
| 808 | if(iterator->length == block->length) { |
| 809 | padding_leftover = 0; |
| 810 | block->is_last = iterator->is_last; |
| 811 | } |
| 812 | else if(iterator->length < FLAC__STREAM_METADATA_HEADER_LENGTH + block->length) |
| 813 | use_padding = false; |
| 814 | else { |
| 815 | padding_leftover = iterator->length - block->length; |
| 816 | padding_is_last = iterator->is_last; |
| 817 | block->is_last = false; |
| 818 | } |
| 819 | } |
| 820 | if(!simple_iterator_pop_(iterator)) |
| 821 | return false; |
| 822 | } |
| 823 | } |
| 824 | if(use_padding) { |
| 825 | /* move to the next block, which is suitable padding */ |
| 826 | if(!FLAC__metadata_simple_iterator_next(iterator)) |
| 827 | return false; |
| 828 | if(padding_leftover == 0) { |
| 829 | ret = write_metadata_block_stationary_(iterator, block); |
| 830 | FLAC__ASSERT(iterator->offset[iterator->depth] == debug_target_offset); |
| 831 | FLAC__ASSERT(ftello(iterator->file) == debug_target_offset + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH); |
| 832 | return ret; |
| 833 | } |
| 834 | else { |
| 835 | FLAC__ASSERT(padding_leftover >= FLAC__STREAM_METADATA_HEADER_LENGTH); |
| 836 | ret = write_metadata_block_stationary_with_padding_(iterator, block, padding_leftover - FLAC__STREAM_METADATA_HEADER_LENGTH, padding_is_last); |
| 837 | FLAC__ASSERT(iterator->offset[iterator->depth] == debug_target_offset); |
| 838 | FLAC__ASSERT(ftello(iterator->file) == debug_target_offset + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH); |
| 839 | return ret; |
| 840 | } |
| 841 | } |
| 842 | else { |
| 843 | ret = rewrite_whole_file_(iterator, block, /*append=*/true); |
| 844 | FLAC__ASSERT(iterator->offset[iterator->depth] == debug_target_offset); |
| 845 | FLAC__ASSERT(ftello(iterator->file) == debug_target_offset + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH); |
| 846 | return ret; |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | FLAC_API FLAC__bool FLAC__metadata_simple_iterator_delete_block(FLAC__Metadata_SimpleIterator *iterator, FLAC__bool use_padding) |
| 851 | { |
| 852 | FLAC__ASSERT_DECLARATION(off_t debug_target_offset = iterator->offset[iterator->depth];) |
| 853 | FLAC__bool ret; |
| 854 | |
| 855 | if(iterator->type == FLAC__METADATA_TYPE_STREAMINFO) { |
| 856 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT; |
| 857 | return false; |
| 858 | } |
| 859 | |
| 860 | if(use_padding) { |
| 861 | FLAC__StreamMetadata *padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING); |
| 862 | if(0 == padding) { |
| 863 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 864 | return false; |
| 865 | } |
| 866 | padding->length = iterator->length; |
| 867 | if(!FLAC__metadata_simple_iterator_set_block(iterator, padding, false)) { |
| 868 | FLAC__metadata_object_delete(padding); |
| 869 | return false; |
| 870 | } |
| 871 | FLAC__metadata_object_delete(padding); |
| 872 | if(!FLAC__metadata_simple_iterator_prev(iterator)) |
| 873 | return false; |
| 874 | FLAC__ASSERT(iterator->offset[iterator->depth] + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (off_t)iterator->length == debug_target_offset); |
| 875 | FLAC__ASSERT(ftello(iterator->file) + (off_t)iterator->length == debug_target_offset); |
| 876 | return true; |
| 877 | } |
| 878 | else { |
| 879 | ret = rewrite_whole_file_(iterator, 0, /*append=*/false); |
| 880 | FLAC__ASSERT(iterator->offset[iterator->depth] + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (off_t)iterator->length == debug_target_offset); |
| 881 | FLAC__ASSERT(ftello(iterator->file) + (off_t)iterator->length == debug_target_offset); |
| 882 | return ret; |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | |
| 887 | |
| 888 | /**************************************************************************** |
| 889 | * |
| 890 | * Level 2 implementation |
| 891 | * |
| 892 | ***************************************************************************/ |
| 893 | |
| 894 | |
| 895 | typedef struct FLAC__Metadata_Node { |
| 896 | FLAC__StreamMetadata *data; |
| 897 | struct FLAC__Metadata_Node *prev, *next; |
| 898 | } FLAC__Metadata_Node; |
| 899 | |
| 900 | struct FLAC__Metadata_Chain { |
| 901 | char *filename; /* will be NULL if using callbacks */ |
| 902 | FLAC__bool is_ogg; |
| 903 | FLAC__Metadata_Node *head; |
| 904 | FLAC__Metadata_Node *tail; |
| 905 | unsigned nodes; |
| 906 | FLAC__Metadata_ChainStatus status; |
| 907 | off_t first_offset, last_offset; |
| 908 | /* |
| 909 | * This is the length of the chain initially read from the FLAC file. |
| 910 | * it is used to compare against the current length to decide whether |
| 911 | * or not the whole file has to be rewritten. |
| 912 | */ |
| 913 | off_t initial_length; |
| 914 | /* @@@ hacky, these are currently only needed by ogg reader */ |
| 915 | FLAC__IOHandle handle; |
| 916 | FLAC__IOCallback_Read read_cb; |
| 917 | }; |
| 918 | |
| 919 | struct FLAC__Metadata_Iterator { |
| 920 | FLAC__Metadata_Chain *chain; |
| 921 | FLAC__Metadata_Node *current; |
| 922 | }; |
| 923 | |
| 924 | FLAC_API const char * const FLAC__Metadata_ChainStatusString[] = { |
| 925 | "FLAC__METADATA_CHAIN_STATUS_OK", |
| 926 | "FLAC__METADATA_CHAIN_STATUS_ILLEGAL_INPUT", |
| 927 | "FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE", |
| 928 | "FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE", |
| 929 | "FLAC__METADATA_CHAIN_STATUS_NOT_WRITABLE", |
| 930 | "FLAC__METADATA_CHAIN_STATUS_BAD_METADATA", |
| 931 | "FLAC__METADATA_CHAIN_STATUS_READ_ERROR", |
| 932 | "FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR", |
| 933 | "FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR", |
| 934 | "FLAC__METADATA_CHAIN_STATUS_RENAME_ERROR", |
| 935 | "FLAC__METADATA_CHAIN_STATUS_UNLINK_ERROR", |
| 936 | "FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR", |
| 937 | "FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR", |
| 938 | "FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS", |
| 939 | "FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH", |
| 940 | "FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL" |
| 941 | }; |
| 942 | |
| 943 | |
| 944 | static FLAC__Metadata_Node *node_new_(void) |
| 945 | { |
| 946 | return (FLAC__Metadata_Node*)calloc(1, sizeof(FLAC__Metadata_Node)); |
| 947 | } |
| 948 | |
| 949 | static void node_delete_(FLAC__Metadata_Node *node) |
| 950 | { |
| 951 | FLAC__ASSERT(0 != node); |
| 952 | if(0 != node->data) |
| 953 | FLAC__metadata_object_delete(node->data); |
| 954 | free(node); |
| 955 | } |
| 956 | |
| 957 | static void chain_init_(FLAC__Metadata_Chain *chain) |
| 958 | { |
| 959 | FLAC__ASSERT(0 != chain); |
| 960 | |
| 961 | chain->filename = 0; |
| 962 | chain->is_ogg = false; |
| 963 | chain->head = chain->tail = 0; |
| 964 | chain->nodes = 0; |
| 965 | chain->status = FLAC__METADATA_CHAIN_STATUS_OK; |
| 966 | chain->initial_length = 0; |
| 967 | chain->read_cb = 0; |
| 968 | } |
| 969 | |
| 970 | static void chain_clear_(FLAC__Metadata_Chain *chain) |
| 971 | { |
| 972 | FLAC__Metadata_Node *node, *next; |
| 973 | |
| 974 | FLAC__ASSERT(0 != chain); |
| 975 | |
| 976 | for(node = chain->head; node; ) { |
| 977 | next = node->next; |
| 978 | node_delete_(node); |
| 979 | node = next; |
| 980 | } |
| 981 | |
| 982 | if(0 != chain->filename) |
| 983 | free(chain->filename); |
| 984 | |
| 985 | chain_init_(chain); |
| 986 | } |
| 987 | |
| 988 | static void chain_append_node_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node) |
| 989 | { |
| 990 | FLAC__ASSERT(0 != chain); |
| 991 | FLAC__ASSERT(0 != node); |
| 992 | FLAC__ASSERT(0 != node->data); |
| 993 | |
| 994 | node->next = node->prev = 0; |
| 995 | node->data->is_last = true; |
| 996 | if(0 != chain->tail) |
| 997 | chain->tail->data->is_last = false; |
| 998 | |
| 999 | if(0 == chain->head) |
| 1000 | chain->head = node; |
| 1001 | else { |
| 1002 | FLAC__ASSERT(0 != chain->tail); |
| 1003 | chain->tail->next = node; |
| 1004 | node->prev = chain->tail; |
| 1005 | } |
| 1006 | chain->tail = node; |
| 1007 | chain->nodes++; |
| 1008 | } |
| 1009 | |
| 1010 | static void chain_remove_node_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node) |
| 1011 | { |
| 1012 | FLAC__ASSERT(0 != chain); |
| 1013 | FLAC__ASSERT(0 != node); |
| 1014 | |
| 1015 | if(node == chain->head) |
| 1016 | chain->head = node->next; |
| 1017 | else |
| 1018 | node->prev->next = node->next; |
| 1019 | |
| 1020 | if(node == chain->tail) |
| 1021 | chain->tail = node->prev; |
| 1022 | else |
| 1023 | node->next->prev = node->prev; |
| 1024 | |
| 1025 | if(0 != chain->tail) |
| 1026 | chain->tail->data->is_last = true; |
| 1027 | |
| 1028 | chain->nodes--; |
| 1029 | } |
| 1030 | |
| 1031 | static void chain_delete_node_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node) |
| 1032 | { |
| 1033 | chain_remove_node_(chain, node); |
| 1034 | node_delete_(node); |
| 1035 | } |
| 1036 | |
| 1037 | static off_t chain_calculate_length_(FLAC__Metadata_Chain *chain) |
| 1038 | { |
| 1039 | const FLAC__Metadata_Node *node; |
| 1040 | off_t length = 0; |
| 1041 | for(node = chain->head; node; node = node->next) |
| 1042 | length += (FLAC__STREAM_METADATA_HEADER_LENGTH + node->data->length); |
| 1043 | return length; |
| 1044 | } |
| 1045 | |
| 1046 | static void iterator_insert_node_(FLAC__Metadata_Iterator *iterator, FLAC__Metadata_Node *node) |
| 1047 | { |
| 1048 | FLAC__ASSERT(0 != node); |
| 1049 | FLAC__ASSERT(0 != node->data); |
| 1050 | FLAC__ASSERT(0 != iterator); |
| 1051 | FLAC__ASSERT(0 != iterator->current); |
| 1052 | FLAC__ASSERT(0 != iterator->chain); |
| 1053 | FLAC__ASSERT(0 != iterator->chain->head); |
| 1054 | FLAC__ASSERT(0 != iterator->chain->tail); |
| 1055 | |
| 1056 | node->data->is_last = false; |
| 1057 | |
| 1058 | node->prev = iterator->current->prev; |
| 1059 | node->next = iterator->current; |
| 1060 | |
| 1061 | if(0 == node->prev) |
| 1062 | iterator->chain->head = node; |
| 1063 | else |
| 1064 | node->prev->next = node; |
| 1065 | |
| 1066 | iterator->current->prev = node; |
| 1067 | |
| 1068 | iterator->chain->nodes++; |
| 1069 | } |
| 1070 | |
| 1071 | static void iterator_insert_node_after_(FLAC__Metadata_Iterator *iterator, FLAC__Metadata_Node *node) |
| 1072 | { |
| 1073 | FLAC__ASSERT(0 != node); |
| 1074 | FLAC__ASSERT(0 != node->data); |
| 1075 | FLAC__ASSERT(0 != iterator); |
| 1076 | FLAC__ASSERT(0 != iterator->current); |
| 1077 | FLAC__ASSERT(0 != iterator->chain); |
| 1078 | FLAC__ASSERT(0 != iterator->chain->head); |
| 1079 | FLAC__ASSERT(0 != iterator->chain->tail); |
| 1080 | |
| 1081 | iterator->current->data->is_last = false; |
| 1082 | |
| 1083 | node->prev = iterator->current; |
| 1084 | node->next = iterator->current->next; |
| 1085 | |
| 1086 | if(0 == node->next) |
| 1087 | iterator->chain->tail = node; |
| 1088 | else |
| 1089 | node->next->prev = node; |
| 1090 | |
| 1091 | node->prev->next = node; |
| 1092 | |
| 1093 | iterator->chain->tail->data->is_last = true; |
| 1094 | |
| 1095 | iterator->chain->nodes++; |
| 1096 | } |
| 1097 | |
| 1098 | /* return true iff node and node->next are both padding */ |
| 1099 | static FLAC__bool chain_merge_adjacent_padding_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node) |
| 1100 | { |
| 1101 | if(node->data->type == FLAC__METADATA_TYPE_PADDING && 0 != node->next && node->next->data->type == FLAC__METADATA_TYPE_PADDING) { |
| 1102 | const unsigned growth = FLAC__STREAM_METADATA_HEADER_LENGTH + node->next->data->length; |
| 1103 | node->data->length += growth; |
| 1104 | |
| 1105 | chain_delete_node_(chain, node->next); |
| 1106 | return true; |
| 1107 | } |
| 1108 | else |
| 1109 | return false; |
| 1110 | } |
| 1111 | |
| 1112 | /* Returns the new length of the chain, or 0 if there was an error. */ |
| 1113 | /* WATCHOUT: This can get called multiple times before a write, so |
| 1114 | * it should still work when this happens. |
| 1115 | */ |
| 1116 | /* WATCHOUT: Make sure to also update the logic in |
| 1117 | * FLAC__metadata_chain_check_if_tempfile_needed() if the logic here changes. |
| 1118 | */ |
| 1119 | static off_t chain_prepare_for_write_(FLAC__Metadata_Chain *chain, FLAC__bool use_padding) |
| 1120 | { |
| 1121 | off_t current_length = chain_calculate_length_(chain); |
| 1122 | |
| 1123 | if(use_padding) { |
| 1124 | /* if the metadata shrank and the last block is padding, we just extend the last padding block */ |
| 1125 | if(current_length < chain->initial_length && chain->tail->data->type == FLAC__METADATA_TYPE_PADDING) { |
| 1126 | const off_t delta = chain->initial_length - current_length; |
| 1127 | chain->tail->data->length += delta; |
| 1128 | current_length += delta; |
| 1129 | FLAC__ASSERT(current_length == chain->initial_length); |
| 1130 | } |
| 1131 | /* if the metadata shrank more than 4 bytes then there's room to add another padding block */ |
| 1132 | else if(current_length + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH <= chain->initial_length) { |
| 1133 | FLAC__StreamMetadata *padding; |
| 1134 | FLAC__Metadata_Node *node; |
| 1135 | if(0 == (padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING))) { |
| 1136 | chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR; |
| 1137 | return 0; |
| 1138 | } |
| 1139 | padding->length = chain->initial_length - (FLAC__STREAM_METADATA_HEADER_LENGTH + current_length); |
| 1140 | if(0 == (node = node_new_())) { |
| 1141 | FLAC__metadata_object_delete(padding); |
| 1142 | chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR; |
| 1143 | return 0; |
| 1144 | } |
| 1145 | node->data = padding; |
| 1146 | chain_append_node_(chain, node); |
| 1147 | current_length = chain_calculate_length_(chain); |
| 1148 | FLAC__ASSERT(current_length == chain->initial_length); |
| 1149 | } |
| 1150 | /* if the metadata grew but the last block is padding, try cutting the padding to restore the original length so we don't have to rewrite the whole file */ |
| 1151 | else if(current_length > chain->initial_length) { |
| 1152 | const off_t delta = current_length - chain->initial_length; |
| 1153 | if(chain->tail->data->type == FLAC__METADATA_TYPE_PADDING) { |
| 1154 | /* if the delta is exactly the size of the last padding block, remove the padding block */ |
| 1155 | if((off_t)chain->tail->data->length + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH == delta) { |
| 1156 | chain_delete_node_(chain, chain->tail); |
| 1157 | current_length = chain_calculate_length_(chain); |
| 1158 | FLAC__ASSERT(current_length == chain->initial_length); |
| 1159 | } |
| 1160 | /* if there is at least 'delta' bytes of padding, trim the padding down */ |
| 1161 | else if((off_t)chain->tail->data->length >= delta) { |
| 1162 | chain->tail->data->length -= delta; |
| 1163 | current_length -= delta; |
| 1164 | FLAC__ASSERT(current_length == chain->initial_length); |
| 1165 | } |
| 1166 | } |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | return current_length; |
| 1171 | } |
| 1172 | |
| 1173 | static FLAC__bool chain_read_cb_(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb, FLAC__IOCallback_Tell tell_cb) |
| 1174 | { |
| 1175 | FLAC__Metadata_Node *node; |
| 1176 | |
| 1177 | FLAC__ASSERT(0 != chain); |
| 1178 | |
| 1179 | /* we assume we're already at the beginning of the file */ |
| 1180 | |
| 1181 | switch(seek_to_first_metadata_block_cb_(handle, read_cb, seek_cb)) { |
| 1182 | case 0: |
| 1183 | break; |
| 1184 | case 1: |
| 1185 | chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR; |
| 1186 | return false; |
| 1187 | case 2: |
| 1188 | chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR; |
| 1189 | return false; |
| 1190 | case 3: |
| 1191 | chain->status = FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE; |
| 1192 | return false; |
| 1193 | default: |
| 1194 | FLAC__ASSERT(0); |
| 1195 | return false; |
| 1196 | } |
| 1197 | |
| 1198 | { |
| 1199 | FLAC__int64 pos = tell_cb(handle); |
| 1200 | if(pos < 0) { |
| 1201 | chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR; |
| 1202 | return false; |
| 1203 | } |
| 1204 | chain->first_offset = (off_t)pos; |
| 1205 | } |
| 1206 | |
| 1207 | { |
| 1208 | FLAC__bool is_last; |
| 1209 | FLAC__MetadataType type; |
| 1210 | unsigned length; |
| 1211 | |
| 1212 | do { |
| 1213 | node = node_new_(); |
| 1214 | if(0 == node) { |
| 1215 | chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR; |
| 1216 | return false; |
| 1217 | } |
| 1218 | |
| 1219 | if(!read_metadata_block_header_cb_(handle, read_cb, &is_last, &type, &length)) { |
| 1220 | chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR; |
| 1221 | return false; |
| 1222 | } |
| 1223 | |
| 1224 | node->data = FLAC__metadata_object_new(type); |
| 1225 | if(0 == node->data) { |
| 1226 | node_delete_(node); |
| 1227 | chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR; |
| 1228 | return false; |
| 1229 | } |
| 1230 | |
| 1231 | node->data->is_last = is_last; |
| 1232 | node->data->length = length; |
| 1233 | |
| 1234 | chain->status = get_equivalent_status_(read_metadata_block_data_cb_(handle, read_cb, seek_cb, node->data)); |
| 1235 | if(chain->status != FLAC__METADATA_CHAIN_STATUS_OK) { |
| 1236 | node_delete_(node); |
| 1237 | return false; |
| 1238 | } |
| 1239 | chain_append_node_(chain, node); |
| 1240 | } while(!is_last); |
| 1241 | } |
| 1242 | |
| 1243 | { |
| 1244 | FLAC__int64 pos = tell_cb(handle); |
| 1245 | if(pos < 0) { |
| 1246 | chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR; |
| 1247 | return false; |
| 1248 | } |
| 1249 | chain->last_offset = (off_t)pos; |
| 1250 | } |
| 1251 | |
| 1252 | chain->initial_length = chain_calculate_length_(chain); |
| 1253 | |
| 1254 | return true; |
| 1255 | } |
| 1256 | |
| 1257 | static FLAC__StreamDecoderReadStatus chain_read_ogg_read_cb_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data) |
| 1258 | { |
| 1259 | FLAC__Metadata_Chain *chain = (FLAC__Metadata_Chain*)client_data; |
| 1260 | (void)decoder; |
| 1261 | if(*bytes > 0 && chain->status == FLAC__METADATA_CHAIN_STATUS_OK) { |
| 1262 | *bytes = chain->read_cb(buffer, sizeof(FLAC__byte), *bytes, chain->handle); |
| 1263 | if(*bytes == 0) |
| 1264 | return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; |
| 1265 | else |
| 1266 | return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; |
| 1267 | } |
| 1268 | else |
| 1269 | return FLAC__STREAM_DECODER_READ_STATUS_ABORT; |
| 1270 | } |
| 1271 | |
| 1272 | static FLAC__StreamDecoderWriteStatus chain_read_ogg_write_cb_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data) |
| 1273 | { |
| 1274 | (void)decoder, (void)frame, (void)buffer, (void)client_data; |
| 1275 | return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; |
| 1276 | } |
| 1277 | |
| 1278 | static void chain_read_ogg_metadata_cb_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data) |
| 1279 | { |
| 1280 | FLAC__Metadata_Chain *chain = (FLAC__Metadata_Chain*)client_data; |
| 1281 | FLAC__Metadata_Node *node; |
| 1282 | |
| 1283 | (void)decoder; |
| 1284 | |
| 1285 | node = node_new_(); |
| 1286 | if(0 == node) { |
| 1287 | chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR; |
| 1288 | return; |
| 1289 | } |
| 1290 | |
| 1291 | node->data = FLAC__metadata_object_clone(metadata); |
| 1292 | if(0 == node->data) { |
| 1293 | node_delete_(node); |
| 1294 | chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR; |
| 1295 | return; |
| 1296 | } |
| 1297 | |
| 1298 | chain_append_node_(chain, node); |
| 1299 | } |
| 1300 | |
| 1301 | static void chain_read_ogg_error_cb_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) |
| 1302 | { |
| 1303 | FLAC__Metadata_Chain *chain = (FLAC__Metadata_Chain*)client_data; |
| 1304 | (void)decoder, (void)status; |
| 1305 | chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; /*@@@ maybe needs better error code */ |
| 1306 | } |
| 1307 | |
| 1308 | static FLAC__bool chain_read_ogg_cb_(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb) |
| 1309 | { |
| 1310 | FLAC__StreamDecoder *decoder; |
| 1311 | |
| 1312 | FLAC__ASSERT(0 != chain); |
| 1313 | |
| 1314 | /* we assume we're already at the beginning of the file */ |
| 1315 | |
| 1316 | chain->handle = handle; |
| 1317 | chain->read_cb = read_cb; |
| 1318 | if(0 == (decoder = FLAC__stream_decoder_new())) { |
| 1319 | chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR; |
| 1320 | return false; |
| 1321 | } |
| 1322 | FLAC__stream_decoder_set_metadata_respond_all(decoder); |
| 1323 | if(FLAC__stream_decoder_init_ogg_stream(decoder, chain_read_ogg_read_cb_, /*seek_callback=*/0, /*tell_callback=*/0, /*length_callback=*/0, /*eof_callback=*/0, chain_read_ogg_write_cb_, chain_read_ogg_metadata_cb_, chain_read_ogg_error_cb_, chain) != FLAC__STREAM_DECODER_INIT_STATUS_OK) { |
| 1324 | FLAC__stream_decoder_delete(decoder); |
| 1325 | chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; /*@@@ maybe needs better error code */ |
| 1326 | return false; |
| 1327 | } |
| 1328 | |
| 1329 | chain->first_offset = 0; /*@@@ wrong; will need to be set correctly to implement metadata writing for Ogg FLAC */ |
| 1330 | |
| 1331 | if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder)) |
| 1332 | chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; /*@@@ maybe needs better error code */ |
| 1333 | if(chain->status != FLAC__METADATA_CHAIN_STATUS_OK) { |
| 1334 | FLAC__stream_decoder_delete(decoder); |
| 1335 | return false; |
| 1336 | } |
| 1337 | |
| 1338 | FLAC__stream_decoder_delete(decoder); |
| 1339 | |
| 1340 | chain->last_offset = 0; /*@@@ wrong; will need to be set correctly to implement metadata writing for Ogg FLAC */ |
| 1341 | |
| 1342 | chain->initial_length = chain_calculate_length_(chain); |
| 1343 | |
| 1344 | return true; |
| 1345 | } |
| 1346 | |
| 1347 | static FLAC__bool chain_rewrite_metadata_in_place_cb_(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, FLAC__IOCallback_Seek seek_cb) |
| 1348 | { |
| 1349 | FLAC__Metadata_Node *node; |
| 1350 | |
| 1351 | FLAC__ASSERT(0 != chain); |
| 1352 | FLAC__ASSERT(0 != chain->head); |
| 1353 | |
| 1354 | if(0 != seek_cb(handle, chain->first_offset, SEEK_SET)) { |
| 1355 | chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR; |
| 1356 | return false; |
| 1357 | } |
| 1358 | |
| 1359 | for(node = chain->head; node; node = node->next) { |
| 1360 | if(!write_metadata_block_header_cb_(handle, write_cb, node->data)) { |
| 1361 | chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR; |
| 1362 | return false; |
| 1363 | } |
| 1364 | if(!write_metadata_block_data_cb_(handle, write_cb, node->data)) { |
| 1365 | chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR; |
| 1366 | return false; |
| 1367 | } |
| 1368 | } |
| 1369 | |
| 1370 | /*FLAC__ASSERT(fflush(), ftello() == chain->last_offset);*/ |
| 1371 | |
| 1372 | chain->status = FLAC__METADATA_CHAIN_STATUS_OK; |
| 1373 | return true; |
| 1374 | } |
| 1375 | |
| 1376 | static FLAC__bool chain_rewrite_metadata_in_place_(FLAC__Metadata_Chain *chain) |
| 1377 | { |
| 1378 | FILE *file; |
| 1379 | FLAC__bool ret; |
| 1380 | |
| 1381 | FLAC__ASSERT(0 != chain->filename); |
| 1382 | |
| 1383 | if(0 == (file = fopen(chain->filename, "r+b"))) { |
| 1384 | chain->status = FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE; |
| 1385 | return false; |
| 1386 | } |
| 1387 | |
| 1388 | /* chain_rewrite_metadata_in_place_cb_() sets chain->status for us */ |
| 1389 | ret = chain_rewrite_metadata_in_place_cb_(chain, (FLAC__IOHandle)file, (FLAC__IOCallback_Write)fwrite, fseek_wrapper_); |
| 1390 | |
| 1391 | fclose(file); |
| 1392 | |
| 1393 | return ret; |
| 1394 | } |
| 1395 | |
| 1396 | static FLAC__bool chain_rewrite_file_(FLAC__Metadata_Chain *chain, const char *tempfile_path_prefix) |
| 1397 | { |
| 1398 | FILE *f, *tempfile; |
| 1399 | char *tempfilename; |
| 1400 | FLAC__Metadata_SimpleIteratorStatus status; |
| 1401 | const FLAC__Metadata_Node *node; |
| 1402 | |
| 1403 | FLAC__ASSERT(0 != chain); |
| 1404 | FLAC__ASSERT(0 != chain->filename); |
| 1405 | FLAC__ASSERT(0 != chain->head); |
| 1406 | |
| 1407 | /* copy the file prefix (data up to first metadata block */ |
| 1408 | if(0 == (f = fopen(chain->filename, "rb"))) { |
| 1409 | chain->status = FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE; |
| 1410 | return false; |
| 1411 | } |
| 1412 | if(!open_tempfile_(chain->filename, tempfile_path_prefix, &tempfile, &tempfilename, &status)) { |
| 1413 | chain->status = get_equivalent_status_(status); |
| 1414 | cleanup_tempfile_(&tempfile, &tempfilename); |
| 1415 | return false; |
| 1416 | } |
| 1417 | if(!copy_n_bytes_from_file_(f, tempfile, chain->first_offset, &status)) { |
| 1418 | chain->status = get_equivalent_status_(status); |
| 1419 | cleanup_tempfile_(&tempfile, &tempfilename); |
| 1420 | return false; |
| 1421 | } |
| 1422 | |
| 1423 | /* write the metadata */ |
| 1424 | for(node = chain->head; node; node = node->next) { |
| 1425 | if(!write_metadata_block_header_(tempfile, &status, node->data)) { |
| 1426 | chain->status = get_equivalent_status_(status); |
| 1427 | return false; |
| 1428 | } |
| 1429 | if(!write_metadata_block_data_(tempfile, &status, node->data)) { |
| 1430 | chain->status = get_equivalent_status_(status); |
| 1431 | return false; |
| 1432 | } |
| 1433 | } |
| 1434 | /*FLAC__ASSERT(fflush(), ftello() == chain->last_offset);*/ |
| 1435 | |
| 1436 | /* copy the file postfix (everything after the metadata) */ |
| 1437 | if(0 != fseeko(f, chain->last_offset, SEEK_SET)) { |
| 1438 | cleanup_tempfile_(&tempfile, &tempfilename); |
| 1439 | chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR; |
| 1440 | return false; |
| 1441 | } |
| 1442 | if(!copy_remaining_bytes_from_file_(f, tempfile, &status)) { |
| 1443 | cleanup_tempfile_(&tempfile, &tempfilename); |
| 1444 | chain->status = get_equivalent_status_(status); |
| 1445 | return false; |
| 1446 | } |
| 1447 | |
| 1448 | /* move the tempfile on top of the original */ |
| 1449 | (void)fclose(f); |
| 1450 | if(!transport_tempfile_(chain->filename, &tempfile, &tempfilename, &status)) |
| 1451 | return false; |
| 1452 | |
| 1453 | return true; |
| 1454 | } |
| 1455 | |
| 1456 | /* assumes 'handle' is already at beginning of file */ |
| 1457 | static FLAC__bool chain_rewrite_file_cb_(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb, FLAC__IOCallback_Eof eof_cb, FLAC__IOHandle temp_handle, FLAC__IOCallback_Write temp_write_cb) |
| 1458 | { |
| 1459 | FLAC__Metadata_SimpleIteratorStatus status; |
| 1460 | const FLAC__Metadata_Node *node; |
| 1461 | |
| 1462 | FLAC__ASSERT(0 != chain); |
| 1463 | FLAC__ASSERT(0 == chain->filename); |
| 1464 | FLAC__ASSERT(0 != chain->head); |
| 1465 | |
| 1466 | /* copy the file prefix (data up to first metadata block */ |
| 1467 | if(!copy_n_bytes_from_file_cb_(handle, read_cb, temp_handle, temp_write_cb, chain->first_offset, &status)) { |
| 1468 | chain->status = get_equivalent_status_(status); |
| 1469 | return false; |
| 1470 | } |
| 1471 | |
| 1472 | /* write the metadata */ |
| 1473 | for(node = chain->head; node; node = node->next) { |
| 1474 | if(!write_metadata_block_header_cb_(temp_handle, temp_write_cb, node->data)) { |
| 1475 | chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR; |
| 1476 | return false; |
| 1477 | } |
| 1478 | if(!write_metadata_block_data_cb_(temp_handle, temp_write_cb, node->data)) { |
| 1479 | chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR; |
| 1480 | return false; |
| 1481 | } |
| 1482 | } |
| 1483 | /*FLAC__ASSERT(fflush(), ftello() == chain->last_offset);*/ |
| 1484 | |
| 1485 | /* copy the file postfix (everything after the metadata) */ |
| 1486 | if(0 != seek_cb(handle, chain->last_offset, SEEK_SET)) { |
| 1487 | chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR; |
| 1488 | return false; |
| 1489 | } |
| 1490 | if(!copy_remaining_bytes_from_file_cb_(handle, read_cb, eof_cb, temp_handle, temp_write_cb, &status)) { |
| 1491 | chain->status = get_equivalent_status_(status); |
| 1492 | return false; |
| 1493 | } |
| 1494 | |
| 1495 | return true; |
| 1496 | } |
| 1497 | |
| 1498 | FLAC_API FLAC__Metadata_Chain *FLAC__metadata_chain_new(void) |
| 1499 | { |
| 1500 | FLAC__Metadata_Chain *chain = (FLAC__Metadata_Chain*)calloc(1, sizeof(FLAC__Metadata_Chain)); |
| 1501 | |
| 1502 | if(0 != chain) |
| 1503 | chain_init_(chain); |
| 1504 | |
| 1505 | return chain; |
| 1506 | } |
| 1507 | |
| 1508 | FLAC_API void FLAC__metadata_chain_delete(FLAC__Metadata_Chain *chain) |
| 1509 | { |
| 1510 | FLAC__ASSERT(0 != chain); |
| 1511 | |
| 1512 | chain_clear_(chain); |
| 1513 | |
| 1514 | free(chain); |
| 1515 | } |
| 1516 | |
| 1517 | FLAC_API FLAC__Metadata_ChainStatus FLAC__metadata_chain_status(FLAC__Metadata_Chain *chain) |
| 1518 | { |
| 1519 | FLAC__Metadata_ChainStatus status; |
| 1520 | |
| 1521 | FLAC__ASSERT(0 != chain); |
| 1522 | |
| 1523 | status = chain->status; |
| 1524 | chain->status = FLAC__METADATA_CHAIN_STATUS_OK; |
| 1525 | return status; |
| 1526 | } |
| 1527 | |
| 1528 | static FLAC__bool chain_read_(FLAC__Metadata_Chain *chain, const char *filename, FLAC__bool is_ogg) |
| 1529 | { |
| 1530 | FILE *file; |
| 1531 | FLAC__bool ret; |
| 1532 | |
| 1533 | FLAC__ASSERT(0 != chain); |
| 1534 | FLAC__ASSERT(0 != filename); |
| 1535 | |
| 1536 | chain_clear_(chain); |
| 1537 | |
| 1538 | if(0 == (chain->filename = strdup(filename))) { |
| 1539 | chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR; |
| 1540 | return false; |
| 1541 | } |
| 1542 | |
| 1543 | chain->is_ogg = is_ogg; |
| 1544 | |
| 1545 | if(0 == (file = fopen(filename, "rb"))) { |
| 1546 | chain->status = FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE; |
| 1547 | return false; |
| 1548 | } |
| 1549 | |
| 1550 | /* the function also sets chain->status for us */ |
| 1551 | ret = is_ogg? |
| 1552 | chain_read_ogg_cb_(chain, file, (FLAC__IOCallback_Read)fread) : |
| 1553 | chain_read_cb_(chain, file, (FLAC__IOCallback_Read)fread, fseek_wrapper_, ftell_wrapper_) |
| 1554 | ; |
| 1555 | |
| 1556 | fclose(file); |
| 1557 | |
| 1558 | return ret; |
| 1559 | } |
| 1560 | |
| 1561 | FLAC_API FLAC__bool FLAC__metadata_chain_read(FLAC__Metadata_Chain *chain, const char *filename) |
| 1562 | { |
| 1563 | return chain_read_(chain, filename, /*is_ogg=*/false); |
| 1564 | } |
| 1565 | |
| 1566 | /*@@@@add to tests*/ |
| 1567 | FLAC_API FLAC__bool FLAC__metadata_chain_read_ogg(FLAC__Metadata_Chain *chain, const char *filename) |
| 1568 | { |
| 1569 | return chain_read_(chain, filename, /*is_ogg=*/true); |
| 1570 | } |
| 1571 | |
| 1572 | static FLAC__bool chain_read_with_callbacks_(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks, FLAC__bool is_ogg) |
| 1573 | { |
| 1574 | FLAC__bool ret; |
| 1575 | |
| 1576 | FLAC__ASSERT(0 != chain); |
| 1577 | |
| 1578 | chain_clear_(chain); |
| 1579 | |
| 1580 | if (0 == callbacks.read || 0 == callbacks.seek || 0 == callbacks.tell) { |
| 1581 | chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS; |
| 1582 | return false; |
| 1583 | } |
| 1584 | |
| 1585 | chain->is_ogg = is_ogg; |
| 1586 | |
| 1587 | /* rewind */ |
| 1588 | if(0 != callbacks.seek(handle, 0, SEEK_SET)) { |
| 1589 | chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR; |
| 1590 | return false; |
| 1591 | } |
| 1592 | |
| 1593 | /* the function also sets chain->status for us */ |
| 1594 | ret = is_ogg? |
| 1595 | chain_read_ogg_cb_(chain, handle, callbacks.read) : |
| 1596 | chain_read_cb_(chain, handle, callbacks.read, callbacks.seek, callbacks.tell) |
| 1597 | ; |
| 1598 | |
| 1599 | return ret; |
| 1600 | } |
| 1601 | |
| 1602 | FLAC_API FLAC__bool FLAC__metadata_chain_read_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks) |
| 1603 | { |
| 1604 | return chain_read_with_callbacks_(chain, handle, callbacks, /*is_ogg=*/false); |
| 1605 | } |
| 1606 | |
| 1607 | /*@@@@add to tests*/ |
| 1608 | FLAC_API FLAC__bool FLAC__metadata_chain_read_ogg_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks) |
| 1609 | { |
| 1610 | return chain_read_with_callbacks_(chain, handle, callbacks, /*is_ogg=*/true); |
| 1611 | } |
| 1612 | |
| 1613 | FLAC_API FLAC__bool FLAC__metadata_chain_check_if_tempfile_needed(FLAC__Metadata_Chain *chain, FLAC__bool use_padding) |
| 1614 | { |
| 1615 | /* This does all the same checks that are in chain_prepare_for_write_() |
| 1616 | * but doesn't actually alter the chain. Make sure to update the logic |
| 1617 | * here if chain_prepare_for_write_() changes. |
| 1618 | */ |
| 1619 | const off_t current_length = chain_calculate_length_(chain); |
| 1620 | |
| 1621 | FLAC__ASSERT(0 != chain); |
| 1622 | |
| 1623 | if(use_padding) { |
| 1624 | /* if the metadata shrank and the last block is padding, we just extend the last padding block */ |
| 1625 | if(current_length < chain->initial_length && chain->tail->data->type == FLAC__METADATA_TYPE_PADDING) |
| 1626 | return false; |
| 1627 | /* if the metadata shrank more than 4 bytes then there's room to add another padding block */ |
| 1628 | else if(current_length + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH <= chain->initial_length) |
| 1629 | return false; |
| 1630 | /* if the metadata grew but the last block is padding, try cutting the padding to restore the original length so we don't have to rewrite the whole file */ |
| 1631 | else if(current_length > chain->initial_length) { |
| 1632 | const off_t delta = current_length - chain->initial_length; |
| 1633 | if(chain->tail->data->type == FLAC__METADATA_TYPE_PADDING) { |
| 1634 | /* if the delta is exactly the size of the last padding block, remove the padding block */ |
| 1635 | if((off_t)chain->tail->data->length + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH == delta) |
| 1636 | return false; |
| 1637 | /* if there is at least 'delta' bytes of padding, trim the padding down */ |
| 1638 | else if((off_t)chain->tail->data->length >= delta) |
| 1639 | return false; |
| 1640 | } |
| 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | return (current_length != chain->initial_length); |
| 1645 | } |
| 1646 | |
| 1647 | FLAC_API FLAC__bool FLAC__metadata_chain_write(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__bool preserve_file_stats) |
| 1648 | { |
| 1649 | struct stat stats; |
| 1650 | const char *tempfile_path_prefix = 0; |
| 1651 | off_t current_length; |
| 1652 | |
| 1653 | FLAC__ASSERT(0 != chain); |
| 1654 | |
| 1655 | if (chain->is_ogg) { /* cannot write back to Ogg FLAC yet */ |
| 1656 | chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; |
| 1657 | return false; |
| 1658 | } |
| 1659 | |
| 1660 | if (0 == chain->filename) { |
| 1661 | chain->status = FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH; |
| 1662 | return false; |
| 1663 | } |
| 1664 | |
| 1665 | current_length = chain_prepare_for_write_(chain, use_padding); |
| 1666 | |
| 1667 | /* a return value of 0 means there was an error; chain->status is already set */ |
| 1668 | if (0 == current_length) |
| 1669 | return false; |
| 1670 | |
| 1671 | if(preserve_file_stats) |
| 1672 | get_file_stats_(chain->filename, &stats); |
| 1673 | |
| 1674 | if(current_length == chain->initial_length) { |
| 1675 | if(!chain_rewrite_metadata_in_place_(chain)) |
| 1676 | return false; |
| 1677 | } |
| 1678 | else { |
| 1679 | if(!chain_rewrite_file_(chain, tempfile_path_prefix)) |
| 1680 | return false; |
| 1681 | |
| 1682 | /* recompute lengths and offsets */ |
| 1683 | { |
| 1684 | const FLAC__Metadata_Node *node; |
| 1685 | chain->initial_length = current_length; |
| 1686 | chain->last_offset = chain->first_offset; |
| 1687 | for(node = chain->head; node; node = node->next) |
| 1688 | chain->last_offset += (FLAC__STREAM_METADATA_HEADER_LENGTH + node->data->length); |
| 1689 | } |
| 1690 | } |
| 1691 | |
| 1692 | if(preserve_file_stats) |
| 1693 | set_file_stats_(chain->filename, &stats); |
| 1694 | |
| 1695 | return true; |
| 1696 | } |
| 1697 | |
| 1698 | FLAC_API FLAC__bool FLAC__metadata_chain_write_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks) |
| 1699 | { |
| 1700 | off_t current_length; |
| 1701 | |
| 1702 | FLAC__ASSERT(0 != chain); |
| 1703 | |
| 1704 | if (chain->is_ogg) { /* cannot write back to Ogg FLAC yet */ |
| 1705 | chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; |
| 1706 | return false; |
| 1707 | } |
| 1708 | |
| 1709 | if (0 != chain->filename) { |
| 1710 | chain->status = FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH; |
| 1711 | return false; |
| 1712 | } |
| 1713 | |
| 1714 | if (0 == callbacks.write || 0 == callbacks.seek) { |
| 1715 | chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS; |
| 1716 | return false; |
| 1717 | } |
| 1718 | |
| 1719 | if (FLAC__metadata_chain_check_if_tempfile_needed(chain, use_padding)) { |
| 1720 | chain->status = FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL; |
| 1721 | return false; |
| 1722 | } |
| 1723 | |
| 1724 | current_length = chain_prepare_for_write_(chain, use_padding); |
| 1725 | |
| 1726 | /* a return value of 0 means there was an error; chain->status is already set */ |
| 1727 | if (0 == current_length) |
| 1728 | return false; |
| 1729 | |
| 1730 | FLAC__ASSERT(current_length == chain->initial_length); |
| 1731 | |
| 1732 | return chain_rewrite_metadata_in_place_cb_(chain, handle, callbacks.write, callbacks.seek); |
| 1733 | } |
| 1734 | |
| 1735 | FLAC_API FLAC__bool FLAC__metadata_chain_write_with_callbacks_and_tempfile(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks, FLAC__IOHandle temp_handle, FLAC__IOCallbacks temp_callbacks) |
| 1736 | { |
| 1737 | off_t current_length; |
| 1738 | |
| 1739 | FLAC__ASSERT(0 != chain); |
| 1740 | |
| 1741 | if (chain->is_ogg) { /* cannot write back to Ogg FLAC yet */ |
| 1742 | chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; |
| 1743 | return false; |
| 1744 | } |
| 1745 | |
| 1746 | if (0 != chain->filename) { |
| 1747 | chain->status = FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH; |
| 1748 | return false; |
| 1749 | } |
| 1750 | |
| 1751 | if (0 == callbacks.read || 0 == callbacks.seek || 0 == callbacks.eof) { |
| 1752 | chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS; |
| 1753 | return false; |
| 1754 | } |
| 1755 | if (0 == temp_callbacks.write) { |
| 1756 | chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS; |
| 1757 | return false; |
| 1758 | } |
| 1759 | |
| 1760 | if (!FLAC__metadata_chain_check_if_tempfile_needed(chain, use_padding)) { |
| 1761 | chain->status = FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL; |
| 1762 | return false; |
| 1763 | } |
| 1764 | |
| 1765 | current_length = chain_prepare_for_write_(chain, use_padding); |
| 1766 | |
| 1767 | /* a return value of 0 means there was an error; chain->status is already set */ |
| 1768 | if (0 == current_length) |
| 1769 | return false; |
| 1770 | |
| 1771 | FLAC__ASSERT(current_length != chain->initial_length); |
| 1772 | |
| 1773 | /* rewind */ |
| 1774 | if(0 != callbacks.seek(handle, 0, SEEK_SET)) { |
| 1775 | chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR; |
| 1776 | return false; |
| 1777 | } |
| 1778 | |
| 1779 | if(!chain_rewrite_file_cb_(chain, handle, callbacks.read, callbacks.seek, callbacks.eof, temp_handle, temp_callbacks.write)) |
| 1780 | return false; |
| 1781 | |
| 1782 | /* recompute lengths and offsets */ |
| 1783 | { |
| 1784 | const FLAC__Metadata_Node *node; |
| 1785 | chain->initial_length = current_length; |
| 1786 | chain->last_offset = chain->first_offset; |
| 1787 | for(node = chain->head; node; node = node->next) |
| 1788 | chain->last_offset += (FLAC__STREAM_METADATA_HEADER_LENGTH + node->data->length); |
| 1789 | } |
| 1790 | |
| 1791 | return true; |
| 1792 | } |
| 1793 | |
| 1794 | FLAC_API void FLAC__metadata_chain_merge_padding(FLAC__Metadata_Chain *chain) |
| 1795 | { |
| 1796 | FLAC__Metadata_Node *node; |
| 1797 | |
| 1798 | FLAC__ASSERT(0 != chain); |
| 1799 | |
| 1800 | for(node = chain->head; node; ) { |
| 1801 | if(!chain_merge_adjacent_padding_(chain, node)) |
| 1802 | node = node->next; |
| 1803 | } |
| 1804 | } |
| 1805 | |
| 1806 | FLAC_API void FLAC__metadata_chain_sort_padding(FLAC__Metadata_Chain *chain) |
| 1807 | { |
| 1808 | FLAC__Metadata_Node *node, *save; |
| 1809 | unsigned i; |
| 1810 | |
| 1811 | FLAC__ASSERT(0 != chain); |
| 1812 | |
| 1813 | /* |
| 1814 | * Don't try and be too smart... this simple algo is good enough for |
| 1815 | * the small number of nodes that we deal with. |
| 1816 | */ |
| 1817 | for(i = 0, node = chain->head; i < chain->nodes; i++) { |
| 1818 | if(node->data->type == FLAC__METADATA_TYPE_PADDING) { |
| 1819 | save = node->next; |
| 1820 | chain_remove_node_(chain, node); |
| 1821 | chain_append_node_(chain, node); |
| 1822 | node = save; |
| 1823 | } |
| 1824 | else { |
| 1825 | node = node->next; |
| 1826 | } |
| 1827 | } |
| 1828 | |
| 1829 | FLAC__metadata_chain_merge_padding(chain); |
| 1830 | } |
| 1831 | |
| 1832 | |
| 1833 | FLAC_API FLAC__Metadata_Iterator *FLAC__metadata_iterator_new(void) |
| 1834 | { |
| 1835 | FLAC__Metadata_Iterator *iterator = (FLAC__Metadata_Iterator*)calloc(1, sizeof(FLAC__Metadata_Iterator)); |
| 1836 | |
| 1837 | /* calloc() implies: |
| 1838 | iterator->current = 0; |
| 1839 | iterator->chain = 0; |
| 1840 | */ |
| 1841 | |
| 1842 | return iterator; |
| 1843 | } |
| 1844 | |
| 1845 | FLAC_API void FLAC__metadata_iterator_delete(FLAC__Metadata_Iterator *iterator) |
| 1846 | { |
| 1847 | FLAC__ASSERT(0 != iterator); |
| 1848 | |
| 1849 | free(iterator); |
| 1850 | } |
| 1851 | |
| 1852 | FLAC_API void FLAC__metadata_iterator_init(FLAC__Metadata_Iterator *iterator, FLAC__Metadata_Chain *chain) |
| 1853 | { |
| 1854 | FLAC__ASSERT(0 != iterator); |
| 1855 | FLAC__ASSERT(0 != chain); |
| 1856 | FLAC__ASSERT(0 != chain->head); |
| 1857 | |
| 1858 | iterator->chain = chain; |
| 1859 | iterator->current = chain->head; |
| 1860 | } |
| 1861 | |
| 1862 | FLAC_API FLAC__bool FLAC__metadata_iterator_next(FLAC__Metadata_Iterator *iterator) |
| 1863 | { |
| 1864 | FLAC__ASSERT(0 != iterator); |
| 1865 | |
| 1866 | if(0 == iterator->current || 0 == iterator->current->next) |
| 1867 | return false; |
| 1868 | |
| 1869 | iterator->current = iterator->current->next; |
| 1870 | return true; |
| 1871 | } |
| 1872 | |
| 1873 | FLAC_API FLAC__bool FLAC__metadata_iterator_prev(FLAC__Metadata_Iterator *iterator) |
| 1874 | { |
| 1875 | FLAC__ASSERT(0 != iterator); |
| 1876 | |
| 1877 | if(0 == iterator->current || 0 == iterator->current->prev) |
| 1878 | return false; |
| 1879 | |
| 1880 | iterator->current = iterator->current->prev; |
| 1881 | return true; |
| 1882 | } |
| 1883 | |
| 1884 | FLAC_API FLAC__MetadataType FLAC__metadata_iterator_get_block_type(const FLAC__Metadata_Iterator *iterator) |
| 1885 | { |
| 1886 | FLAC__ASSERT(0 != iterator); |
| 1887 | FLAC__ASSERT(0 != iterator->current); |
| 1888 | FLAC__ASSERT(0 != iterator->current->data); |
| 1889 | |
| 1890 | return iterator->current->data->type; |
| 1891 | } |
| 1892 | |
| 1893 | FLAC_API FLAC__StreamMetadata *FLAC__metadata_iterator_get_block(FLAC__Metadata_Iterator *iterator) |
| 1894 | { |
| 1895 | FLAC__ASSERT(0 != iterator); |
| 1896 | FLAC__ASSERT(0 != iterator->current); |
| 1897 | |
| 1898 | return iterator->current->data; |
| 1899 | } |
| 1900 | |
| 1901 | FLAC_API FLAC__bool FLAC__metadata_iterator_set_block(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block) |
| 1902 | { |
| 1903 | FLAC__ASSERT(0 != iterator); |
| 1904 | FLAC__ASSERT(0 != block); |
| 1905 | return FLAC__metadata_iterator_delete_block(iterator, false) && FLAC__metadata_iterator_insert_block_after(iterator, block); |
| 1906 | } |
| 1907 | |
| 1908 | FLAC_API FLAC__bool FLAC__metadata_iterator_delete_block(FLAC__Metadata_Iterator *iterator, FLAC__bool replace_with_padding) |
| 1909 | { |
| 1910 | FLAC__Metadata_Node *save; |
| 1911 | |
| 1912 | FLAC__ASSERT(0 != iterator); |
| 1913 | FLAC__ASSERT(0 != iterator->current); |
| 1914 | |
| 1915 | if(0 == iterator->current->prev) { |
| 1916 | FLAC__ASSERT(iterator->current->data->type == FLAC__METADATA_TYPE_STREAMINFO); |
| 1917 | return false; |
| 1918 | } |
| 1919 | |
| 1920 | save = iterator->current->prev; |
| 1921 | |
| 1922 | if(replace_with_padding) { |
| 1923 | FLAC__metadata_object_delete_data(iterator->current->data); |
| 1924 | iterator->current->data->type = FLAC__METADATA_TYPE_PADDING; |
| 1925 | } |
| 1926 | else { |
| 1927 | chain_delete_node_(iterator->chain, iterator->current); |
| 1928 | } |
| 1929 | |
| 1930 | iterator->current = save; |
| 1931 | return true; |
| 1932 | } |
| 1933 | |
| 1934 | FLAC_API FLAC__bool FLAC__metadata_iterator_insert_block_before(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block) |
| 1935 | { |
| 1936 | FLAC__Metadata_Node *node; |
| 1937 | |
| 1938 | FLAC__ASSERT(0 != iterator); |
| 1939 | FLAC__ASSERT(0 != iterator->current); |
| 1940 | FLAC__ASSERT(0 != block); |
| 1941 | |
| 1942 | if(block->type == FLAC__METADATA_TYPE_STREAMINFO) |
| 1943 | return false; |
| 1944 | |
| 1945 | if(0 == iterator->current->prev) { |
| 1946 | FLAC__ASSERT(iterator->current->data->type == FLAC__METADATA_TYPE_STREAMINFO); |
| 1947 | return false; |
| 1948 | } |
| 1949 | |
| 1950 | if(0 == (node = node_new_())) |
| 1951 | return false; |
| 1952 | |
| 1953 | node->data = block; |
| 1954 | iterator_insert_node_(iterator, node); |
| 1955 | iterator->current = node; |
| 1956 | return true; |
| 1957 | } |
| 1958 | |
| 1959 | FLAC_API FLAC__bool FLAC__metadata_iterator_insert_block_after(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block) |
| 1960 | { |
| 1961 | FLAC__Metadata_Node *node; |
| 1962 | |
| 1963 | FLAC__ASSERT(0 != iterator); |
| 1964 | FLAC__ASSERT(0 != iterator->current); |
| 1965 | FLAC__ASSERT(0 != block); |
| 1966 | |
| 1967 | if(block->type == FLAC__METADATA_TYPE_STREAMINFO) |
| 1968 | return false; |
| 1969 | |
| 1970 | if(0 == (node = node_new_())) |
| 1971 | return false; |
| 1972 | |
| 1973 | node->data = block; |
| 1974 | iterator_insert_node_after_(iterator, node); |
| 1975 | iterator->current = node; |
| 1976 | return true; |
| 1977 | } |
| 1978 | |
| 1979 | |
| 1980 | /**************************************************************************** |
| 1981 | * |
| 1982 | * Local function definitions |
| 1983 | * |
| 1984 | ***************************************************************************/ |
| 1985 | |
| 1986 | void pack_uint32_(FLAC__uint32 val, FLAC__byte *b, unsigned bytes) |
| 1987 | { |
| 1988 | unsigned i; |
| 1989 | |
| 1990 | b += bytes; |
| 1991 | |
| 1992 | for(i = 0; i < bytes; i++) { |
| 1993 | *(--b) = (FLAC__byte)(val & 0xff); |
| 1994 | val >>= 8; |
| 1995 | } |
| 1996 | } |
| 1997 | |
| 1998 | void pack_uint32_little_endian_(FLAC__uint32 val, FLAC__byte *b, unsigned bytes) |
| 1999 | { |
| 2000 | unsigned i; |
| 2001 | |
| 2002 | for(i = 0; i < bytes; i++) { |
| 2003 | *(b++) = (FLAC__byte)(val & 0xff); |
| 2004 | val >>= 8; |
| 2005 | } |
| 2006 | } |
| 2007 | |
| 2008 | void pack_uint64_(FLAC__uint64 val, FLAC__byte *b, unsigned bytes) |
| 2009 | { |
| 2010 | unsigned i; |
| 2011 | |
| 2012 | b += bytes; |
| 2013 | |
| 2014 | for(i = 0; i < bytes; i++) { |
| 2015 | *(--b) = (FLAC__byte)(val & 0xff); |
| 2016 | val >>= 8; |
| 2017 | } |
| 2018 | } |
| 2019 | |
| 2020 | FLAC__uint32 unpack_uint32_(FLAC__byte *b, unsigned bytes) |
| 2021 | { |
| 2022 | FLAC__uint32 ret = 0; |
| 2023 | unsigned i; |
| 2024 | |
| 2025 | for(i = 0; i < bytes; i++) |
| 2026 | ret = (ret << 8) | (FLAC__uint32)(*b++); |
| 2027 | |
| 2028 | return ret; |
| 2029 | } |
| 2030 | |
| 2031 | FLAC__uint32 unpack_uint32_little_endian_(FLAC__byte *b, unsigned bytes) |
| 2032 | { |
| 2033 | FLAC__uint32 ret = 0; |
| 2034 | unsigned i; |
| 2035 | |
| 2036 | b += bytes; |
| 2037 | |
| 2038 | for(i = 0; i < bytes; i++) |
| 2039 | ret = (ret << 8) | (FLAC__uint32)(*--b); |
| 2040 | |
| 2041 | return ret; |
| 2042 | } |
| 2043 | |
| 2044 | FLAC__uint64 unpack_uint64_(FLAC__byte *b, unsigned bytes) |
| 2045 | { |
| 2046 | FLAC__uint64 ret = 0; |
| 2047 | unsigned i; |
| 2048 | |
| 2049 | for(i = 0; i < bytes; i++) |
| 2050 | ret = (ret << 8) | (FLAC__uint64)(*b++); |
| 2051 | |
| 2052 | return ret; |
| 2053 | } |
| 2054 | |
| 2055 | FLAC__bool read_metadata_block_header_(FLAC__Metadata_SimpleIterator *iterator) |
| 2056 | { |
| 2057 | FLAC__ASSERT(0 != iterator); |
| 2058 | FLAC__ASSERT(0 != iterator->file); |
| 2059 | |
| 2060 | if(!read_metadata_block_header_cb_((FLAC__IOHandle)iterator->file, (FLAC__IOCallback_Read)fread, &iterator->is_last, &iterator->type, &iterator->length)) { |
| 2061 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2062 | return false; |
| 2063 | } |
| 2064 | |
| 2065 | return true; |
| 2066 | } |
| 2067 | |
| 2068 | FLAC__bool read_metadata_block_data_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block) |
| 2069 | { |
| 2070 | FLAC__ASSERT(0 != iterator); |
| 2071 | FLAC__ASSERT(0 != iterator->file); |
| 2072 | |
| 2073 | iterator->status = read_metadata_block_data_cb_((FLAC__IOHandle)iterator->file, (FLAC__IOCallback_Read)fread, fseek_wrapper_, block); |
| 2074 | |
| 2075 | return (iterator->status == FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK); |
| 2076 | } |
| 2077 | |
| 2078 | FLAC__bool read_metadata_block_header_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__bool *is_last, FLAC__MetadataType *type, unsigned *length) |
| 2079 | { |
| 2080 | FLAC__byte raw_header[FLAC__STREAM_METADATA_HEADER_LENGTH]; |
| 2081 | |
| 2082 | if(read_cb(raw_header, 1, FLAC__STREAM_METADATA_HEADER_LENGTH, handle) != FLAC__STREAM_METADATA_HEADER_LENGTH) |
| 2083 | return false; |
| 2084 | |
| 2085 | *is_last = raw_header[0] & 0x80? true : false; |
| 2086 | *type = (FLAC__MetadataType)(raw_header[0] & 0x7f); |
| 2087 | *length = unpack_uint32_(raw_header + 1, 3); |
| 2088 | |
| 2089 | /* Note that we don't check: |
| 2090 | * if(iterator->type >= FLAC__METADATA_TYPE_UNDEFINED) |
| 2091 | * we just will read in an opaque block |
| 2092 | */ |
| 2093 | |
| 2094 | return true; |
| 2095 | } |
| 2096 | |
| 2097 | FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata *block) |
| 2098 | { |
| 2099 | switch(block->type) { |
| 2100 | case FLAC__METADATA_TYPE_STREAMINFO: |
| 2101 | return read_metadata_block_data_streaminfo_cb_(handle, read_cb, &block->data.stream_info); |
| 2102 | case FLAC__METADATA_TYPE_PADDING: |
| 2103 | return read_metadata_block_data_padding_cb_(handle, seek_cb, &block->data.padding, block->length); |
| 2104 | case FLAC__METADATA_TYPE_APPLICATION: |
| 2105 | return read_metadata_block_data_application_cb_(handle, read_cb, &block->data.application, block->length); |
| 2106 | case FLAC__METADATA_TYPE_SEEKTABLE: |
| 2107 | return read_metadata_block_data_seektable_cb_(handle, read_cb, &block->data.seek_table, block->length); |
| 2108 | case FLAC__METADATA_TYPE_VORBIS_COMMENT: |
| 2109 | return read_metadata_block_data_vorbis_comment_cb_(handle, read_cb, &block->data.vorbis_comment); |
| 2110 | case FLAC__METADATA_TYPE_CUESHEET: |
| 2111 | return read_metadata_block_data_cuesheet_cb_(handle, read_cb, &block->data.cue_sheet); |
| 2112 | case FLAC__METADATA_TYPE_PICTURE: |
| 2113 | return read_metadata_block_data_picture_cb_(handle, read_cb, &block->data.picture); |
| 2114 | default: |
| 2115 | return read_metadata_block_data_unknown_cb_(handle, read_cb, &block->data.unknown, block->length); |
| 2116 | } |
| 2117 | } |
| 2118 | |
| 2119 | FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_streaminfo_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_StreamInfo *block) |
| 2120 | { |
| 2121 | FLAC__byte buffer[FLAC__STREAM_METADATA_STREAMINFO_LENGTH], *b; |
| 2122 | |
| 2123 | if(read_cb(buffer, 1, FLAC__STREAM_METADATA_STREAMINFO_LENGTH, handle) != FLAC__STREAM_METADATA_STREAMINFO_LENGTH) |
| 2124 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2125 | |
| 2126 | b = buffer; |
| 2127 | |
| 2128 | /* we are using hardcoded numbers for simplicity but we should |
| 2129 | * probably eventually write a bit-level unpacker and use the |
| 2130 | * _STREAMINFO_ constants. |
| 2131 | */ |
| 2132 | block->min_blocksize = unpack_uint32_(b, 2); b += 2; |
| 2133 | block->max_blocksize = unpack_uint32_(b, 2); b += 2; |
| 2134 | block->min_framesize = unpack_uint32_(b, 3); b += 3; |
| 2135 | block->max_framesize = unpack_uint32_(b, 3); b += 3; |
| 2136 | block->sample_rate = (unpack_uint32_(b, 2) << 4) | ((unsigned)(b[2] & 0xf0) >> 4); |
| 2137 | block->channels = (unsigned)((b[2] & 0x0e) >> 1) + 1; |
| 2138 | block->bits_per_sample = ((((unsigned)(b[2] & 0x01)) << 4) | (((unsigned)(b[3] & 0xf0)) >> 4)) + 1; |
| 2139 | block->total_samples = (((FLAC__uint64)(b[3] & 0x0f)) << 32) | unpack_uint64_(b+4, 4); |
| 2140 | memcpy(block->md5sum, b+8, 16); |
| 2141 | |
| 2142 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 2143 | } |
| 2144 | |
| 2145 | FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_padding_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata_Padding *block, unsigned block_length) |
| 2146 | { |
| 2147 | (void)block; /* nothing to do; we don't care about reading the padding bytes */ |
| 2148 | |
| 2149 | if(0 != seek_cb(handle, block_length, SEEK_CUR)) |
| 2150 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 2151 | |
| 2152 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 2153 | } |
| 2154 | |
| 2155 | FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_application_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Application *block, unsigned block_length) |
| 2156 | { |
| 2157 | const unsigned id_bytes = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8; |
| 2158 | |
| 2159 | if(read_cb(block->id, 1, id_bytes, handle) != id_bytes) |
| 2160 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2161 | |
| 2162 | if(block_length < id_bytes) |
| 2163 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2164 | |
| 2165 | block_length -= id_bytes; |
| 2166 | |
| 2167 | if(block_length == 0) { |
| 2168 | block->data = 0; |
| 2169 | } |
| 2170 | else { |
| 2171 | if(0 == (block->data = (FLAC__byte*)malloc(block_length))) |
| 2172 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 2173 | |
| 2174 | if(read_cb(block->data, 1, block_length, handle) != block_length) |
| 2175 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2176 | } |
| 2177 | |
| 2178 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 2179 | } |
| 2180 | |
| 2181 | FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_seektable_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_SeekTable *block, unsigned block_length) |
| 2182 | { |
| 2183 | unsigned i; |
| 2184 | FLAC__byte buffer[FLAC__STREAM_METADATA_SEEKPOINT_LENGTH]; |
| 2185 | |
| 2186 | FLAC__ASSERT(block_length % FLAC__STREAM_METADATA_SEEKPOINT_LENGTH == 0); |
| 2187 | |
| 2188 | block->num_points = block_length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH; |
| 2189 | |
| 2190 | if(block->num_points == 0) |
| 2191 | block->points = 0; |
| 2192 | else if(0 == (block->points = (FLAC__StreamMetadata_SeekPoint*)safe_malloc_mul_2op_(block->num_points, /*times*/sizeof(FLAC__StreamMetadata_SeekPoint)))) |
| 2193 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 2194 | |
| 2195 | for(i = 0; i < block->num_points; i++) { |
| 2196 | if(read_cb(buffer, 1, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH, handle) != FLAC__STREAM_METADATA_SEEKPOINT_LENGTH) |
| 2197 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2198 | /* some MAGIC NUMBERs here */ |
| 2199 | block->points[i].sample_number = unpack_uint64_(buffer, 8); |
| 2200 | block->points[i].stream_offset = unpack_uint64_(buffer+8, 8); |
| 2201 | block->points[i].frame_samples = unpack_uint32_(buffer+16, 2); |
| 2202 | } |
| 2203 | |
| 2204 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 2205 | } |
| 2206 | |
| 2207 | FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_entry_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_VorbisComment_Entry *entry) |
| 2208 | { |
| 2209 | const unsigned entry_length_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8; |
| 2210 | FLAC__byte buffer[4]; /* magic number is asserted below */ |
| 2211 | |
| 2212 | FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8 == sizeof(buffer)); |
| 2213 | |
| 2214 | if(read_cb(buffer, 1, entry_length_len, handle) != entry_length_len) |
| 2215 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2216 | entry->length = unpack_uint32_little_endian_(buffer, entry_length_len); |
| 2217 | |
| 2218 | if(0 != entry->entry) |
| 2219 | free(entry->entry); |
| 2220 | |
| 2221 | if(entry->length == 0) { |
| 2222 | entry->entry = 0; |
| 2223 | } |
| 2224 | else { |
| 2225 | if(0 == (entry->entry = (FLAC__byte*)safe_malloc_add_2op_(entry->length, /*+*/1))) |
| 2226 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 2227 | |
| 2228 | if(read_cb(entry->entry, 1, entry->length, handle) != entry->length) |
| 2229 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2230 | |
| 2231 | entry->entry[entry->length] = '\0'; |
| 2232 | } |
| 2233 | |
| 2234 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 2235 | } |
| 2236 | |
| 2237 | FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_VorbisComment *block) |
| 2238 | { |
| 2239 | unsigned i; |
| 2240 | FLAC__Metadata_SimpleIteratorStatus status; |
| 2241 | const unsigned num_comments_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN / 8; |
| 2242 | FLAC__byte buffer[4]; /* magic number is asserted below */ |
| 2243 | |
| 2244 | FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN / 8 == sizeof(buffer)); |
| 2245 | |
| 2246 | if(FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK != (status = read_metadata_block_data_vorbis_comment_entry_cb_(handle, read_cb, &(block->vendor_string)))) |
| 2247 | return status; |
| 2248 | |
| 2249 | if(read_cb(buffer, 1, num_comments_len, handle) != num_comments_len) |
| 2250 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2251 | block->num_comments = unpack_uint32_little_endian_(buffer, num_comments_len); |
| 2252 | |
| 2253 | if(block->num_comments == 0) { |
| 2254 | block->comments = 0; |
| 2255 | } |
| 2256 | else if(0 == (block->comments = (FLAC__StreamMetadata_VorbisComment_Entry*)calloc(block->num_comments, sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) |
| 2257 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 2258 | |
| 2259 | for(i = 0; i < block->num_comments; i++) { |
| 2260 | if(FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK != (status = read_metadata_block_data_vorbis_comment_entry_cb_(handle, read_cb, block->comments + i))) |
| 2261 | return status; |
| 2262 | } |
| 2263 | |
| 2264 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 2265 | } |
| 2266 | |
| 2267 | FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cuesheet_track_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_CueSheet_Track *track) |
| 2268 | { |
| 2269 | unsigned i, len; |
| 2270 | FLAC__byte buffer[32]; /* asserted below that this is big enough */ |
| 2271 | |
| 2272 | FLAC__ASSERT(sizeof(buffer) >= sizeof(FLAC__uint64)); |
| 2273 | FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN/8); |
| 2274 | FLAC__ASSERT(sizeof(buffer) >= (FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN) / 8); |
| 2275 | |
| 2276 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN % 8 == 0); |
| 2277 | len = FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN / 8; |
| 2278 | if(read_cb(buffer, 1, len, handle) != len) |
| 2279 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2280 | track->offset = unpack_uint64_(buffer, len); |
| 2281 | |
| 2282 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN % 8 == 0); |
| 2283 | len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN / 8; |
| 2284 | if(read_cb(buffer, 1, len, handle) != len) |
| 2285 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2286 | track->number = (FLAC__byte)unpack_uint32_(buffer, len); |
| 2287 | |
| 2288 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0); |
| 2289 | len = FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN / 8; |
| 2290 | if(read_cb(track->isrc, 1, len, handle) != len) |
| 2291 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2292 | |
| 2293 | FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN) % 8 == 0); |
| 2294 | len = (FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN) / 8; |
| 2295 | if(read_cb(buffer, 1, len, handle) != len) |
| 2296 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2297 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN == 1); |
| 2298 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN == 1); |
| 2299 | track->type = buffer[0] >> 7; |
| 2300 | track->pre_emphasis = (buffer[0] >> 6) & 1; |
| 2301 | |
| 2302 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN % 8 == 0); |
| 2303 | len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN / 8; |
| 2304 | if(read_cb(buffer, 1, len, handle) != len) |
| 2305 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2306 | track->num_indices = (FLAC__byte)unpack_uint32_(buffer, len); |
| 2307 | |
| 2308 | if(track->num_indices == 0) { |
| 2309 | track->indices = 0; |
| 2310 | } |
| 2311 | else if(0 == (track->indices = (FLAC__StreamMetadata_CueSheet_Index*)calloc(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index)))) |
| 2312 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 2313 | |
| 2314 | for(i = 0; i < track->num_indices; i++) { |
| 2315 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN % 8 == 0); |
| 2316 | len = FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN / 8; |
| 2317 | if(read_cb(buffer, 1, len, handle) != len) |
| 2318 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2319 | track->indices[i].offset = unpack_uint64_(buffer, len); |
| 2320 | |
| 2321 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN % 8 == 0); |
| 2322 | len = FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN / 8; |
| 2323 | if(read_cb(buffer, 1, len, handle) != len) |
| 2324 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2325 | track->indices[i].number = (FLAC__byte)unpack_uint32_(buffer, len); |
| 2326 | |
| 2327 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN % 8 == 0); |
| 2328 | len = FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN / 8; |
| 2329 | if(read_cb(buffer, 1, len, handle) != len) |
| 2330 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2331 | } |
| 2332 | |
| 2333 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 2334 | } |
| 2335 | |
| 2336 | FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cuesheet_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_CueSheet *block) |
| 2337 | { |
| 2338 | unsigned i, len; |
| 2339 | FLAC__Metadata_SimpleIteratorStatus status; |
| 2340 | FLAC__byte buffer[1024]; /* MSVC needs a constant expression so we put a magic number and assert */ |
| 2341 | |
| 2342 | FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN)/8 <= sizeof(buffer)); |
| 2343 | FLAC__ASSERT(sizeof(FLAC__uint64) <= sizeof(buffer)); |
| 2344 | |
| 2345 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0); |
| 2346 | len = FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN / 8; |
| 2347 | if(read_cb(block->media_catalog_number, 1, len, handle) != len) |
| 2348 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2349 | |
| 2350 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN % 8 == 0); |
| 2351 | len = FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN / 8; |
| 2352 | if(read_cb(buffer, 1, len, handle) != len) |
| 2353 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2354 | block->lead_in = unpack_uint64_(buffer, len); |
| 2355 | |
| 2356 | FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) % 8 == 0); |
| 2357 | len = (FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) / 8; |
| 2358 | if(read_cb(buffer, 1, len, handle) != len) |
| 2359 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2360 | block->is_cd = buffer[0]&0x80? true : false; |
| 2361 | |
| 2362 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN % 8 == 0); |
| 2363 | len = FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN / 8; |
| 2364 | if(read_cb(buffer, 1, len, handle) != len) |
| 2365 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2366 | block->num_tracks = unpack_uint32_(buffer, len); |
| 2367 | |
| 2368 | if(block->num_tracks == 0) { |
| 2369 | block->tracks = 0; |
| 2370 | } |
| 2371 | else if(0 == (block->tracks = (FLAC__StreamMetadata_CueSheet_Track*)calloc(block->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track)))) |
| 2372 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 2373 | |
| 2374 | for(i = 0; i < block->num_tracks; i++) { |
| 2375 | if(FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK != (status = read_metadata_block_data_cuesheet_track_cb_(handle, read_cb, block->tracks + i))) |
| 2376 | return status; |
| 2377 | } |
| 2378 | |
| 2379 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 2380 | } |
| 2381 | |
| 2382 | static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_picture_cstring_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__byte **data, FLAC__uint32 *length, FLAC__uint32 length_len) |
| 2383 | { |
| 2384 | FLAC__byte buffer[sizeof(FLAC__uint32)]; |
| 2385 | |
| 2386 | FLAC__ASSERT(0 != data); |
| 2387 | FLAC__ASSERT(length_len%8 == 0); |
| 2388 | |
| 2389 | length_len /= 8; /* convert to bytes */ |
| 2390 | |
| 2391 | FLAC__ASSERT(sizeof(buffer) >= length_len); |
| 2392 | |
| 2393 | if(read_cb(buffer, 1, length_len, handle) != length_len) |
| 2394 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2395 | *length = unpack_uint32_(buffer, length_len); |
| 2396 | |
| 2397 | if(0 != *data) |
| 2398 | free(*data); |
| 2399 | |
| 2400 | if(0 == (*data = (FLAC__byte*)safe_malloc_add_2op_(*length, /*+*/1))) |
| 2401 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 2402 | |
| 2403 | if(*length > 0) { |
| 2404 | if(read_cb(*data, 1, *length, handle) != *length) |
| 2405 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2406 | } |
| 2407 | |
| 2408 | (*data)[*length] = '\0'; |
| 2409 | |
| 2410 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 2411 | } |
| 2412 | |
| 2413 | FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_picture_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Picture *block) |
| 2414 | { |
| 2415 | FLAC__Metadata_SimpleIteratorStatus status; |
| 2416 | FLAC__byte buffer[4]; /* asserted below that this is big enough */ |
| 2417 | FLAC__uint32 len; |
| 2418 | |
| 2419 | FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_TYPE_LEN/8); |
| 2420 | FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN/8); |
| 2421 | FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN/8); |
| 2422 | FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN/8); |
| 2423 | FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_COLORS_LEN/8); |
| 2424 | |
| 2425 | FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_TYPE_LEN % 8 == 0); |
| 2426 | len = FLAC__STREAM_METADATA_PICTURE_TYPE_LEN / 8; |
| 2427 | if(read_cb(buffer, 1, len, handle) != len) |
| 2428 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2429 | block->type = /*(FLAC__StreamMetadata_Picture_Type)*/unpack_uint32_(buffer, len); // changed for MAME, check |
| 2430 | |
| 2431 | if((status = read_metadata_block_data_picture_cstring_cb_(handle, read_cb, (FLAC__byte**)(&(block->mime_type)), &len, FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN)) != FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK) |
| 2432 | return status; |
| 2433 | |
| 2434 | if((status = read_metadata_block_data_picture_cstring_cb_(handle, read_cb, &(block->description), &len, FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN)) != FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK) |
| 2435 | return status; |
| 2436 | |
| 2437 | FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN % 8 == 0); |
| 2438 | len = FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN / 8; |
| 2439 | if(read_cb(buffer, 1, len, handle) != len) |
| 2440 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2441 | block->width = unpack_uint32_(buffer, len); |
| 2442 | |
| 2443 | FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN % 8 == 0); |
| 2444 | len = FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN / 8; |
| 2445 | if(read_cb(buffer, 1, len, handle) != len) |
| 2446 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2447 | block->height = unpack_uint32_(buffer, len); |
| 2448 | |
| 2449 | FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN % 8 == 0); |
| 2450 | len = FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN / 8; |
| 2451 | if(read_cb(buffer, 1, len, handle) != len) |
| 2452 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2453 | block->depth = unpack_uint32_(buffer, len); |
| 2454 | |
| 2455 | FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_COLORS_LEN % 8 == 0); |
| 2456 | len = FLAC__STREAM_METADATA_PICTURE_COLORS_LEN / 8; |
| 2457 | if(read_cb(buffer, 1, len, handle) != len) |
| 2458 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2459 | block->colors = unpack_uint32_(buffer, len); |
| 2460 | |
| 2461 | /* for convenience we use read_metadata_block_data_picture_cstring_cb_() even though it adds an extra terminating NUL we don't use */ |
| 2462 | if((status = read_metadata_block_data_picture_cstring_cb_(handle, read_cb, &(block->data), &(block->data_length), FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN)) != FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK) |
| 2463 | return status; |
| 2464 | |
| 2465 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 2466 | } |
| 2467 | |
| 2468 | FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_unknown_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Unknown *block, unsigned block_length) |
| 2469 | { |
| 2470 | if(block_length == 0) { |
| 2471 | block->data = 0; |
| 2472 | } |
| 2473 | else { |
| 2474 | if(0 == (block->data = (FLAC__byte*)malloc(block_length))) |
| 2475 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 2476 | |
| 2477 | if(read_cb(block->data, 1, block_length, handle) != block_length) |
| 2478 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2479 | } |
| 2480 | |
| 2481 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 2482 | } |
| 2483 | |
| 2484 | FLAC__bool write_metadata_block_header_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block) |
| 2485 | { |
| 2486 | FLAC__ASSERT(0 != file); |
| 2487 | FLAC__ASSERT(0 != status); |
| 2488 | |
| 2489 | if(!write_metadata_block_header_cb_((FLAC__IOHandle)file, (FLAC__IOCallback_Write)fwrite, block)) { |
| 2490 | *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR; |
| 2491 | return false; |
| 2492 | } |
| 2493 | |
| 2494 | return true; |
| 2495 | } |
| 2496 | |
| 2497 | FLAC__bool write_metadata_block_data_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block) |
| 2498 | { |
| 2499 | FLAC__ASSERT(0 != file); |
| 2500 | FLAC__ASSERT(0 != status); |
| 2501 | |
| 2502 | if (write_metadata_block_data_cb_((FLAC__IOHandle)file, (FLAC__IOCallback_Write)fwrite, block)) { |
| 2503 | *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 2504 | return true; |
| 2505 | } |
| 2506 | else { |
| 2507 | *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR; |
| 2508 | return false; |
| 2509 | } |
| 2510 | } |
| 2511 | |
| 2512 | FLAC__bool write_metadata_block_header_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata *block) |
| 2513 | { |
| 2514 | FLAC__byte buffer[FLAC__STREAM_METADATA_HEADER_LENGTH]; |
| 2515 | |
| 2516 | FLAC__ASSERT(block->length < (1u << FLAC__STREAM_METADATA_LENGTH_LEN)); |
| 2517 | |
| 2518 | buffer[0] = (block->is_last? 0x80 : 0) | (FLAC__byte)block->type; |
| 2519 | pack_uint32_(block->length, buffer + 1, 3); |
| 2520 | |
| 2521 | if(write_cb(buffer, 1, FLAC__STREAM_METADATA_HEADER_LENGTH, handle) != FLAC__STREAM_METADATA_HEADER_LENGTH) |
| 2522 | return false; |
| 2523 | |
| 2524 | return true; |
| 2525 | } |
| 2526 | |
| 2527 | FLAC__bool write_metadata_block_data_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata *block) |
| 2528 | { |
| 2529 | FLAC__ASSERT(0 != block); |
| 2530 | |
| 2531 | switch(block->type) { |
| 2532 | case FLAC__METADATA_TYPE_STREAMINFO: |
| 2533 | return write_metadata_block_data_streaminfo_cb_(handle, write_cb, &block->data.stream_info); |
| 2534 | case FLAC__METADATA_TYPE_PADDING: |
| 2535 | return write_metadata_block_data_padding_cb_(handle, write_cb, &block->data.padding, block->length); |
| 2536 | case FLAC__METADATA_TYPE_APPLICATION: |
| 2537 | return write_metadata_block_data_application_cb_(handle, write_cb, &block->data.application, block->length); |
| 2538 | case FLAC__METADATA_TYPE_SEEKTABLE: |
| 2539 | return write_metadata_block_data_seektable_cb_(handle, write_cb, &block->data.seek_table); |
| 2540 | case FLAC__METADATA_TYPE_VORBIS_COMMENT: |
| 2541 | return write_metadata_block_data_vorbis_comment_cb_(handle, write_cb, &block->data.vorbis_comment); |
| 2542 | case FLAC__METADATA_TYPE_CUESHEET: |
| 2543 | return write_metadata_block_data_cuesheet_cb_(handle, write_cb, &block->data.cue_sheet); |
| 2544 | case FLAC__METADATA_TYPE_PICTURE: |
| 2545 | return write_metadata_block_data_picture_cb_(handle, write_cb, &block->data.picture); |
| 2546 | default: |
| 2547 | return write_metadata_block_data_unknown_cb_(handle, write_cb, &block->data.unknown, block->length); |
| 2548 | } |
| 2549 | } |
| 2550 | |
| 2551 | FLAC__bool write_metadata_block_data_streaminfo_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_StreamInfo *block) |
| 2552 | { |
| 2553 | FLAC__byte buffer[FLAC__STREAM_METADATA_STREAMINFO_LENGTH]; |
| 2554 | const unsigned channels1 = block->channels - 1; |
| 2555 | const unsigned bps1 = block->bits_per_sample - 1; |
| 2556 | |
| 2557 | /* we are using hardcoded numbers for simplicity but we should |
| 2558 | * probably eventually write a bit-level packer and use the |
| 2559 | * _STREAMINFO_ constants. |
| 2560 | */ |
| 2561 | pack_uint32_(block->min_blocksize, buffer, 2); |
| 2562 | pack_uint32_(block->max_blocksize, buffer+2, 2); |
| 2563 | pack_uint32_(block->min_framesize, buffer+4, 3); |
| 2564 | pack_uint32_(block->max_framesize, buffer+7, 3); |
| 2565 | buffer[10] = (block->sample_rate >> 12) & 0xff; |
| 2566 | buffer[11] = (block->sample_rate >> 4) & 0xff; |
| 2567 | buffer[12] = ((block->sample_rate & 0x0f) << 4) | (channels1 << 1) | (bps1 >> 4); |
| 2568 | buffer[13] = (FLAC__byte)(((bps1 & 0x0f) << 4) | ((block->total_samples >> 32) & 0x0f)); |
| 2569 | pack_uint32_((FLAC__uint32)block->total_samples, buffer+14, 4); |
| 2570 | memcpy(buffer+18, block->md5sum, 16); |
| 2571 | |
| 2572 | if(write_cb(buffer, 1, FLAC__STREAM_METADATA_STREAMINFO_LENGTH, handle) != FLAC__STREAM_METADATA_STREAMINFO_LENGTH) |
| 2573 | return false; |
| 2574 | |
| 2575 | return true; |
| 2576 | } |
| 2577 | |
| 2578 | FLAC__bool write_metadata_block_data_padding_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Padding *block, unsigned block_length) |
| 2579 | { |
| 2580 | unsigned i, n = block_length; |
| 2581 | FLAC__byte buffer[1024]; |
| 2582 | |
| 2583 | (void)block; |
| 2584 | |
| 2585 | memset(buffer, 0, 1024); |
| 2586 | |
| 2587 | for(i = 0; i < n/1024; i++) |
| 2588 | if(write_cb(buffer, 1, 1024, handle) != 1024) |
| 2589 | return false; |
| 2590 | |
| 2591 | n %= 1024; |
| 2592 | |
| 2593 | if(write_cb(buffer, 1, n, handle) != n) |
| 2594 | return false; |
| 2595 | |
| 2596 | return true; |
| 2597 | } |
| 2598 | |
| 2599 | FLAC__bool write_metadata_block_data_application_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Application *block, unsigned block_length) |
| 2600 | { |
| 2601 | const unsigned id_bytes = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8; |
| 2602 | |
| 2603 | if(write_cb(block->id, 1, id_bytes, handle) != id_bytes) |
| 2604 | return false; |
| 2605 | |
| 2606 | block_length -= id_bytes; |
| 2607 | |
| 2608 | if(write_cb(block->data, 1, block_length, handle) != block_length) |
| 2609 | return false; |
| 2610 | |
| 2611 | return true; |
| 2612 | } |
| 2613 | |
| 2614 | FLAC__bool write_metadata_block_data_seektable_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_SeekTable *block) |
| 2615 | { |
| 2616 | unsigned i; |
| 2617 | FLAC__byte buffer[FLAC__STREAM_METADATA_SEEKPOINT_LENGTH]; |
| 2618 | |
| 2619 | for(i = 0; i < block->num_points; i++) { |
| 2620 | /* some MAGIC NUMBERs here */ |
| 2621 | pack_uint64_(block->points[i].sample_number, buffer, 8); |
| 2622 | pack_uint64_(block->points[i].stream_offset, buffer+8, 8); |
| 2623 | pack_uint32_(block->points[i].frame_samples, buffer+16, 2); |
| 2624 | if(write_cb(buffer, 1, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH, handle) != FLAC__STREAM_METADATA_SEEKPOINT_LENGTH) |
| 2625 | return false; |
| 2626 | } |
| 2627 | |
| 2628 | return true; |
| 2629 | } |
| 2630 | |
| 2631 | FLAC__bool write_metadata_block_data_vorbis_comment_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_VorbisComment *block) |
| 2632 | { |
| 2633 | unsigned i; |
| 2634 | const unsigned entry_length_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8; |
| 2635 | const unsigned num_comments_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN / 8; |
| 2636 | FLAC__byte buffer[4]; /* magic number is asserted below */ |
| 2637 | |
| 2638 | FLAC__ASSERT(max(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN, FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN) / 8 == sizeof(buffer)); |
| 2639 | |
| 2640 | pack_uint32_little_endian_(block->vendor_string.length, buffer, entry_length_len); |
| 2641 | if(write_cb(buffer, 1, entry_length_len, handle) != entry_length_len) |
| 2642 | return false; |
| 2643 | if(write_cb(block->vendor_string.entry, 1, block->vendor_string.length, handle) != block->vendor_string.length) |
| 2644 | return false; |
| 2645 | |
| 2646 | pack_uint32_little_endian_(block->num_comments, buffer, num_comments_len); |
| 2647 | if(write_cb(buffer, 1, num_comments_len, handle) != num_comments_len) |
| 2648 | return false; |
| 2649 | |
| 2650 | for(i = 0; i < block->num_comments; i++) { |
| 2651 | pack_uint32_little_endian_(block->comments[i].length, buffer, entry_length_len); |
| 2652 | if(write_cb(buffer, 1, entry_length_len, handle) != entry_length_len) |
| 2653 | return false; |
| 2654 | if(write_cb(block->comments[i].entry, 1, block->comments[i].length, handle) != block->comments[i].length) |
| 2655 | return false; |
| 2656 | } |
| 2657 | |
| 2658 | return true; |
| 2659 | } |
| 2660 | |
| 2661 | FLAC__bool write_metadata_block_data_cuesheet_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_CueSheet *block) |
| 2662 | { |
| 2663 | unsigned i, j, len; |
| 2664 | FLAC__byte buffer[1024]; /* asserted below that this is big enough */ |
| 2665 | |
| 2666 | FLAC__ASSERT(sizeof(buffer) >= sizeof(FLAC__uint64)); |
| 2667 | FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN/8); |
| 2668 | FLAC__ASSERT(sizeof(buffer) >= (FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN)/8); |
| 2669 | FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN/8); |
| 2670 | |
| 2671 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0); |
| 2672 | len = FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN / 8; |
| 2673 | if(write_cb(block->media_catalog_number, 1, len, handle) != len) |
| 2674 | return false; |
| 2675 | |
| 2676 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN % 8 == 0); |
| 2677 | len = FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN / 8; |
| 2678 | pack_uint64_(block->lead_in, buffer, len); |
| 2679 | if(write_cb(buffer, 1, len, handle) != len) |
| 2680 | return false; |
| 2681 | |
| 2682 | FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) % 8 == 0); |
| 2683 | len = (FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) / 8; |
| 2684 | memset(buffer, 0, len); |
| 2685 | if(block->is_cd) |
| 2686 | buffer[0] |= 0x80; |
| 2687 | if(write_cb(buffer, 1, len, handle) != len) |
| 2688 | return false; |
| 2689 | |
| 2690 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN % 8 == 0); |
| 2691 | len = FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN / 8; |
| 2692 | pack_uint32_(block->num_tracks, buffer, len); |
| 2693 | if(write_cb(buffer, 1, len, handle) != len) |
| 2694 | return false; |
| 2695 | |
| 2696 | for(i = 0; i < block->num_tracks; i++) { |
| 2697 | FLAC__StreamMetadata_CueSheet_Track *track = block->tracks + i; |
| 2698 | |
| 2699 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN % 8 == 0); |
| 2700 | len = FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN / 8; |
| 2701 | pack_uint64_(track->offset, buffer, len); |
| 2702 | if(write_cb(buffer, 1, len, handle) != len) |
| 2703 | return false; |
| 2704 | |
| 2705 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN % 8 == 0); |
| 2706 | len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN / 8; |
| 2707 | pack_uint32_(track->number, buffer, len); |
| 2708 | if(write_cb(buffer, 1, len, handle) != len) |
| 2709 | return false; |
| 2710 | |
| 2711 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0); |
| 2712 | len = FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN / 8; |
| 2713 | if(write_cb(track->isrc, 1, len, handle) != len) |
| 2714 | return false; |
| 2715 | |
| 2716 | FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN) % 8 == 0); |
| 2717 | len = (FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN) / 8; |
| 2718 | memset(buffer, 0, len); |
| 2719 | buffer[0] = (track->type << 7) | (track->pre_emphasis << 6); |
| 2720 | if(write_cb(buffer, 1, len, handle) != len) |
| 2721 | return false; |
| 2722 | |
| 2723 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN % 8 == 0); |
| 2724 | len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN / 8; |
| 2725 | pack_uint32_(track->num_indices, buffer, len); |
| 2726 | if(write_cb(buffer, 1, len, handle) != len) |
| 2727 | return false; |
| 2728 | |
| 2729 | for(j = 0; j < track->num_indices; j++) { |
| 2730 | FLAC__StreamMetadata_CueSheet_Index *index = track->indices + j; |
| 2731 | |
| 2732 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN % 8 == 0); |
| 2733 | len = FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN / 8; |
| 2734 | pack_uint64_(index->offset, buffer, len); |
| 2735 | if(write_cb(buffer, 1, len, handle) != len) |
| 2736 | return false; |
| 2737 | |
| 2738 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN % 8 == 0); |
| 2739 | len = FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN / 8; |
| 2740 | pack_uint32_(index->number, buffer, len); |
| 2741 | if(write_cb(buffer, 1, len, handle) != len) |
| 2742 | return false; |
| 2743 | |
| 2744 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN % 8 == 0); |
| 2745 | len = FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN / 8; |
| 2746 | memset(buffer, 0, len); |
| 2747 | if(write_cb(buffer, 1, len, handle) != len) |
| 2748 | return false; |
| 2749 | } |
| 2750 | } |
| 2751 | |
| 2752 | return true; |
| 2753 | } |
| 2754 | |
| 2755 | FLAC__bool write_metadata_block_data_picture_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Picture *block) |
| 2756 | { |
| 2757 | unsigned len; |
| 2758 | size_t slen; |
| 2759 | FLAC__byte buffer[4]; /* magic number is asserted below */ |
| 2760 | |
| 2761 | FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_TYPE_LEN%8); |
| 2762 | FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN%8); |
| 2763 | FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN%8); |
| 2764 | FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN%8); |
| 2765 | FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN%8); |
| 2766 | FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN%8); |
| 2767 | FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_COLORS_LEN%8); |
| 2768 | FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN%8); |
| 2769 | FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_TYPE_LEN/8); |
| 2770 | FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN/8); |
| 2771 | FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN/8); |
| 2772 | FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN/8); |
| 2773 | FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN/8); |
| 2774 | FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN/8); |
| 2775 | FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_COLORS_LEN/8); |
| 2776 | FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN/8); |
| 2777 | |
| 2778 | len = FLAC__STREAM_METADATA_PICTURE_TYPE_LEN/8; |
| 2779 | pack_uint32_(block->type, buffer, len); |
| 2780 | if(write_cb(buffer, 1, len, handle) != len) |
| 2781 | return false; |
| 2782 | |
| 2783 | len = FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN/8; |
| 2784 | slen = strlen(block->mime_type); |
| 2785 | pack_uint32_(slen, buffer, len); |
| 2786 | if(write_cb(buffer, 1, len, handle) != len) |
| 2787 | return false; |
| 2788 | if(write_cb(block->mime_type, 1, slen, handle) != slen) |
| 2789 | return false; |
| 2790 | |
| 2791 | len = FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN/8; |
| 2792 | slen = strlen((const char *)block->description); |
| 2793 | pack_uint32_(slen, buffer, len); |
| 2794 | if(write_cb(buffer, 1, len, handle) != len) |
| 2795 | return false; |
| 2796 | if(write_cb(block->description, 1, slen, handle) != slen) |
| 2797 | return false; |
| 2798 | |
| 2799 | len = FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN/8; |
| 2800 | pack_uint32_(block->width, buffer, len); |
| 2801 | if(write_cb(buffer, 1, len, handle) != len) |
| 2802 | return false; |
| 2803 | |
| 2804 | len = FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN/8; |
| 2805 | pack_uint32_(block->height, buffer, len); |
| 2806 | if(write_cb(buffer, 1, len, handle) != len) |
| 2807 | return false; |
| 2808 | |
| 2809 | len = FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN/8; |
| 2810 | pack_uint32_(block->depth, buffer, len); |
| 2811 | if(write_cb(buffer, 1, len, handle) != len) |
| 2812 | return false; |
| 2813 | |
| 2814 | len = FLAC__STREAM_METADATA_PICTURE_COLORS_LEN/8; |
| 2815 | pack_uint32_(block->colors, buffer, len); |
| 2816 | if(write_cb(buffer, 1, len, handle) != len) |
| 2817 | return false; |
| 2818 | |
| 2819 | len = FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN/8; |
| 2820 | pack_uint32_(block->data_length, buffer, len); |
| 2821 | if(write_cb(buffer, 1, len, handle) != len) |
| 2822 | return false; |
| 2823 | if(write_cb(block->data, 1, block->data_length, handle) != block->data_length) |
| 2824 | return false; |
| 2825 | |
| 2826 | return true; |
| 2827 | } |
| 2828 | |
| 2829 | FLAC__bool write_metadata_block_data_unknown_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Unknown *block, unsigned block_length) |
| 2830 | { |
| 2831 | if(write_cb(block->data, 1, block_length, handle) != block_length) |
| 2832 | return false; |
| 2833 | |
| 2834 | return true; |
| 2835 | } |
| 2836 | |
| 2837 | FLAC__bool write_metadata_block_stationary_(FLAC__Metadata_SimpleIterator *iterator, const FLAC__StreamMetadata *block) |
| 2838 | { |
| 2839 | if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) { |
| 2840 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 2841 | return false; |
| 2842 | } |
| 2843 | |
| 2844 | if(!write_metadata_block_header_(iterator->file, &iterator->status, block)) |
| 2845 | return false; |
| 2846 | |
| 2847 | if(!write_metadata_block_data_(iterator->file, &iterator->status, block)) |
| 2848 | return false; |
| 2849 | |
| 2850 | if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) { |
| 2851 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 2852 | return false; |
| 2853 | } |
| 2854 | |
| 2855 | return read_metadata_block_header_(iterator); |
| 2856 | } |
| 2857 | |
| 2858 | FLAC__bool write_metadata_block_stationary_with_padding_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, unsigned padding_length, FLAC__bool padding_is_last) |
| 2859 | { |
| 2860 | FLAC__StreamMetadata *padding; |
| 2861 | |
| 2862 | if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) { |
| 2863 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 2864 | return false; |
| 2865 | } |
| 2866 | |
| 2867 | block->is_last = false; |
| 2868 | |
| 2869 | if(!write_metadata_block_header_(iterator->file, &iterator->status, block)) |
| 2870 | return false; |
| 2871 | |
| 2872 | if(!write_metadata_block_data_(iterator->file, &iterator->status, block)) |
| 2873 | return false; |
| 2874 | |
| 2875 | if(0 == (padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING))) |
| 2876 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 2877 | |
| 2878 | padding->is_last = padding_is_last; |
| 2879 | padding->length = padding_length; |
| 2880 | |
| 2881 | if(!write_metadata_block_header_(iterator->file, &iterator->status, padding)) { |
| 2882 | FLAC__metadata_object_delete(padding); |
| 2883 | return false; |
| 2884 | } |
| 2885 | |
| 2886 | if(!write_metadata_block_data_(iterator->file, &iterator->status, padding)) { |
| 2887 | FLAC__metadata_object_delete(padding); |
| 2888 | return false; |
| 2889 | } |
| 2890 | |
| 2891 | FLAC__metadata_object_delete(padding); |
| 2892 | |
| 2893 | if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) { |
| 2894 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 2895 | return false; |
| 2896 | } |
| 2897 | |
| 2898 | return read_metadata_block_header_(iterator); |
| 2899 | } |
| 2900 | |
| 2901 | FLAC__bool rewrite_whole_file_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool append) |
| 2902 | { |
| 2903 | FILE *tempfile; |
| 2904 | char *tempfilename; |
| 2905 | int fixup_is_last_code = 0; /* 0 => no need to change any is_last flags */ |
| 2906 | off_t fixup_is_last_flag_offset = -1; |
| 2907 | |
| 2908 | FLAC__ASSERT(0 != block || append == false); |
| 2909 | |
| 2910 | if(iterator->is_last) { |
| 2911 | if(append) { |
| 2912 | fixup_is_last_code = 1; /* 1 => clear the is_last flag at the following offset */ |
| 2913 | fixup_is_last_flag_offset = iterator->offset[iterator->depth]; |
| 2914 | } |
| 2915 | else if(0 == block) { |
| 2916 | simple_iterator_push_(iterator); |
| 2917 | if(!FLAC__metadata_simple_iterator_prev(iterator)) { |
| 2918 | (void)simple_iterator_pop_(iterator); |
| 2919 | return false; |
| 2920 | } |
| 2921 | fixup_is_last_code = -1; /* -1 => set the is_last the flag at the following offset */ |
| 2922 | fixup_is_last_flag_offset = iterator->offset[iterator->depth]; |
| 2923 | if(!simple_iterator_pop_(iterator)) |
| 2924 | return false; |
| 2925 | } |
| 2926 | } |
| 2927 | |
| 2928 | if(!simple_iterator_copy_file_prefix_(iterator, &tempfile, &tempfilename, append)) |
| 2929 | return false; |
| 2930 | |
| 2931 | if(0 != block) { |
| 2932 | if(!write_metadata_block_header_(tempfile, &iterator->status, block)) { |
| 2933 | cleanup_tempfile_(&tempfile, &tempfilename); |
| 2934 | return false; |
| 2935 | } |
| 2936 | |
| 2937 | if(!write_metadata_block_data_(tempfile, &iterator->status, block)) { |
| 2938 | cleanup_tempfile_(&tempfile, &tempfilename); |
| 2939 | return false; |
| 2940 | } |
| 2941 | } |
| 2942 | |
| 2943 | if(!simple_iterator_copy_file_postfix_(iterator, &tempfile, &tempfilename, fixup_is_last_code, fixup_is_last_flag_offset, block==0)) |
| 2944 | return false; |
| 2945 | |
| 2946 | if(append) |
| 2947 | return FLAC__metadata_simple_iterator_next(iterator); |
| 2948 | |
| 2949 | return true; |
| 2950 | } |
| 2951 | |
| 2952 | void simple_iterator_push_(FLAC__Metadata_SimpleIterator *iterator) |
| 2953 | { |
| 2954 | FLAC__ASSERT(iterator->depth+1 < SIMPLE_ITERATOR_MAX_PUSH_DEPTH); |
| 2955 | iterator->offset[iterator->depth+1] = iterator->offset[iterator->depth]; |
| 2956 | iterator->depth++; |
| 2957 | } |
| 2958 | |
| 2959 | FLAC__bool simple_iterator_pop_(FLAC__Metadata_SimpleIterator *iterator) |
| 2960 | { |
| 2961 | FLAC__ASSERT(iterator->depth > 0); |
| 2962 | iterator->depth--; |
| 2963 | if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) { |
| 2964 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 2965 | return false; |
| 2966 | } |
| 2967 | |
| 2968 | return read_metadata_block_header_(iterator); |
| 2969 | } |
| 2970 | |
| 2971 | /* return meanings: |
| 2972 | * 0: ok |
| 2973 | * 1: read error |
| 2974 | * 2: seek error |
| 2975 | * 3: not a FLAC file |
| 2976 | */ |
| 2977 | unsigned seek_to_first_metadata_block_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb) |
| 2978 | { |
| 2979 | FLAC__byte buffer[4]; |
| 2980 | size_t n; |
| 2981 | unsigned i; |
| 2982 | |
| 2983 | FLAC__ASSERT(FLAC__STREAM_SYNC_LENGTH == sizeof(buffer)); |
| 2984 | |
| 2985 | /* skip any id3v2 tag */ |
| 2986 | errno = 0; |
| 2987 | n = read_cb(buffer, 1, 4, handle); |
| 2988 | if(errno) |
| 2989 | return 1; |
| 2990 | else if(n != 4) |
| 2991 | return 3; |
| 2992 | else if(0 == memcmp(buffer, "ID3", 3)) { |
| 2993 | unsigned tag_length = 0; |
| 2994 | |
| 2995 | /* skip to the tag length */ |
| 2996 | if(seek_cb(handle, 2, SEEK_CUR) < 0) |
| 2997 | return 2; |
| 2998 | |
| 2999 | /* read the length */ |
| 3000 | for(i = 0; i < 4; i++) { |
| 3001 | if(read_cb(buffer, 1, 1, handle) < 1 || buffer[0] & 0x80) |
| 3002 | return 1; |
| 3003 | tag_length <<= 7; |
| 3004 | tag_length |= (buffer[0] & 0x7f); |
| 3005 | } |
| 3006 | |
| 3007 | /* skip the rest of the tag */ |
| 3008 | if(seek_cb(handle, tag_length, SEEK_CUR) < 0) |
| 3009 | return 2; |
| 3010 | |
| 3011 | /* read the stream sync code */ |
| 3012 | errno = 0; |
| 3013 | n = read_cb(buffer, 1, 4, handle); |
| 3014 | if(errno) |
| 3015 | return 1; |
| 3016 | else if(n != 4) |
| 3017 | return 3; |
| 3018 | } |
| 3019 | |
| 3020 | /* check for the fLaC signature */ |
| 3021 | if(0 == memcmp(FLAC__STREAM_SYNC_STRING, buffer, FLAC__STREAM_SYNC_LENGTH)) |
| 3022 | return 0; |
| 3023 | else |
| 3024 | return 3; |
| 3025 | } |
| 3026 | |
| 3027 | unsigned seek_to_first_metadata_block_(FILE *f) |
| 3028 | { |
| 3029 | return seek_to_first_metadata_block_cb_((FLAC__IOHandle)f, (FLAC__IOCallback_Read)fread, fseek_wrapper_); |
| 3030 | } |
| 3031 | |
| 3032 | FLAC__bool simple_iterator_copy_file_prefix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, FLAC__bool append) |
| 3033 | { |
| 3034 | const off_t offset_end = append? iterator->offset[iterator->depth] + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (off_t)iterator->length : iterator->offset[iterator->depth]; |
| 3035 | |
| 3036 | if(0 != fseeko(iterator->file, 0, SEEK_SET)) { |
| 3037 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 3038 | return false; |
| 3039 | } |
| 3040 | if(!open_tempfile_(iterator->filename, iterator->tempfile_path_prefix, tempfile, tempfilename, &iterator->status)) { |
| 3041 | cleanup_tempfile_(tempfile, tempfilename); |
| 3042 | return false; |
| 3043 | } |
| 3044 | if(!copy_n_bytes_from_file_(iterator->file, *tempfile, offset_end, &iterator->status)) { |
| 3045 | cleanup_tempfile_(tempfile, tempfilename); |
| 3046 | return false; |
| 3047 | } |
| 3048 | |
| 3049 | return true; |
| 3050 | } |
| 3051 | |
| 3052 | FLAC__bool simple_iterator_copy_file_postfix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, int fixup_is_last_code, off_t fixup_is_last_flag_offset, FLAC__bool backup) |
| 3053 | { |
| 3054 | off_t save_offset = iterator->offset[iterator->depth]; |
| 3055 | FLAC__ASSERT(0 != *tempfile); |
| 3056 | |
| 3057 | if(0 != fseeko(iterator->file, save_offset + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (off_t)iterator->length, SEEK_SET)) { |
| 3058 | cleanup_tempfile_(tempfile, tempfilename); |
| 3059 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 3060 | return false; |
| 3061 | } |
| 3062 | if(!copy_remaining_bytes_from_file_(iterator->file, *tempfile, &iterator->status)) { |
| 3063 | cleanup_tempfile_(tempfile, tempfilename); |
| 3064 | return false; |
| 3065 | } |
| 3066 | |
| 3067 | if(fixup_is_last_code != 0) { |
| 3068 | /* |
| 3069 | * if code == 1, it means a block was appended to the end so |
| 3070 | * we have to clear the is_last flag of the previous block |
| 3071 | * if code == -1, it means the last block was deleted so |
| 3072 | * we have to set the is_last flag of the previous block |
| 3073 | */ |
| 3074 | /* MAGIC NUMBERs here; we know the is_last flag is the high bit of the byte at this location */ |
| 3075 | FLAC__byte x; |
| 3076 | if(0 != fseeko(*tempfile, fixup_is_last_flag_offset, SEEK_SET)) { |
| 3077 | cleanup_tempfile_(tempfile, tempfilename); |
| 3078 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 3079 | return false; |
| 3080 | } |
| 3081 | if(fread(&x, 1, 1, *tempfile) != 1) { |
| 3082 | cleanup_tempfile_(tempfile, tempfilename); |
| 3083 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 3084 | return false; |
| 3085 | } |
| 3086 | if(fixup_is_last_code > 0) { |
| 3087 | FLAC__ASSERT(x & 0x80); |
| 3088 | x &= 0x7f; |
| 3089 | } |
| 3090 | else { |
| 3091 | FLAC__ASSERT(!(x & 0x80)); |
| 3092 | x |= 0x80; |
| 3093 | } |
| 3094 | if(0 != fseeko(*tempfile, fixup_is_last_flag_offset, SEEK_SET)) { |
| 3095 | cleanup_tempfile_(tempfile, tempfilename); |
| 3096 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 3097 | return false; |
| 3098 | } |
| 3099 | if(local__fwrite(&x, 1, 1, *tempfile) != 1) { |
| 3100 | cleanup_tempfile_(tempfile, tempfilename); |
| 3101 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR; |
| 3102 | return false; |
| 3103 | } |
| 3104 | } |
| 3105 | |
| 3106 | (void)fclose(iterator->file); |
| 3107 | |
| 3108 | if(!transport_tempfile_(iterator->filename, tempfile, tempfilename, &iterator->status)) |
| 3109 | return false; |
| 3110 | |
| 3111 | if(iterator->has_stats) |
| 3112 | set_file_stats_(iterator->filename, &iterator->stats); |
| 3113 | |
| 3114 | if(!simple_iterator_prime_input_(iterator, !iterator->is_writable)) |
| 3115 | return false; |
| 3116 | if(backup) { |
| 3117 | while(iterator->offset[iterator->depth] + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (off_t)iterator->length < save_offset) |
| 3118 | if(!FLAC__metadata_simple_iterator_next(iterator)) |
| 3119 | return false; |
| 3120 | return true; |
| 3121 | } |
| 3122 | else { |
| 3123 | /* move the iterator to it's original block faster by faking a push, then doing a pop_ */ |
| 3124 | FLAC__ASSERT(iterator->depth == 0); |
| 3125 | iterator->offset[0] = save_offset; |
| 3126 | iterator->depth++; |
| 3127 | return simple_iterator_pop_(iterator); |
| 3128 | } |
| 3129 | } |
| 3130 | |
| 3131 | FLAC__bool copy_n_bytes_from_file_(FILE *file, FILE *tempfile, off_t bytes, FLAC__Metadata_SimpleIteratorStatus *status) |
| 3132 | { |
| 3133 | FLAC__byte buffer[8192]; |
| 3134 | size_t n; |
| 3135 | |
| 3136 | FLAC__ASSERT(bytes >= 0); |
| 3137 | while(bytes > 0) { |
| 3138 | n = min(sizeof(buffer), (size_t)bytes); |
| 3139 | if(fread(buffer, 1, n, file) != n) { |
| 3140 | *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 3141 | return false; |
| 3142 | } |
| 3143 | if(local__fwrite(buffer, 1, n, tempfile) != n) { |
| 3144 | *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR; |
| 3145 | return false; |
| 3146 | } |
| 3147 | bytes -= n; |
| 3148 | } |
| 3149 | |
| 3150 | return true; |
| 3151 | } |
| 3152 | |
| 3153 | FLAC__bool copy_n_bytes_from_file_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOHandle temp_handle, FLAC__IOCallback_Write temp_write_cb, off_t bytes, FLAC__Metadata_SimpleIteratorStatus *status) |
| 3154 | { |
| 3155 | FLAC__byte buffer[8192]; |
| 3156 | size_t n; |
| 3157 | |
| 3158 | FLAC__ASSERT(bytes >= 0); |
| 3159 | while(bytes > 0) { |
| 3160 | n = min(sizeof(buffer), (size_t)bytes); |
| 3161 | if(read_cb(buffer, 1, n, handle) != n) { |
| 3162 | *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 3163 | return false; |
| 3164 | } |
| 3165 | if(temp_write_cb(buffer, 1, n, temp_handle) != n) { |
| 3166 | *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR; |
| 3167 | return false; |
| 3168 | } |
| 3169 | bytes -= n; |
| 3170 | } |
| 3171 | |
| 3172 | return true; |
| 3173 | } |
| 3174 | |
| 3175 | FLAC__bool copy_remaining_bytes_from_file_(FILE *file, FILE *tempfile, FLAC__Metadata_SimpleIteratorStatus *status) |
| 3176 | { |
| 3177 | FLAC__byte buffer[8192]; |
| 3178 | size_t n; |
| 3179 | |
| 3180 | while(!feof(file)) { |
| 3181 | n = fread(buffer, 1, sizeof(buffer), file); |
| 3182 | if(n == 0 && !feof(file)) { |
| 3183 | *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 3184 | return false; |
| 3185 | } |
| 3186 | if(n > 0 && local__fwrite(buffer, 1, n, tempfile) != n) { |
| 3187 | *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR; |
| 3188 | return false; |
| 3189 | } |
| 3190 | } |
| 3191 | |
| 3192 | return true; |
| 3193 | } |
| 3194 | |
| 3195 | FLAC__bool copy_remaining_bytes_from_file_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Eof eof_cb, FLAC__IOHandle temp_handle, FLAC__IOCallback_Write temp_write_cb, FLAC__Metadata_SimpleIteratorStatus *status) |
| 3196 | { |
| 3197 | FLAC__byte buffer[8192]; |
| 3198 | size_t n; |
| 3199 | |
| 3200 | while(!eof_cb(handle)) { |
| 3201 | n = read_cb(buffer, 1, sizeof(buffer), handle); |
| 3202 | if(n == 0 && !eof_cb(handle)) { |
| 3203 | *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 3204 | return false; |
| 3205 | } |
| 3206 | if(n > 0 && temp_write_cb(buffer, 1, n, temp_handle) != n) { |
| 3207 | *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR; |
| 3208 | return false; |
| 3209 | } |
| 3210 | } |
| 3211 | |
| 3212 | return true; |
| 3213 | } |
| 3214 | |
| 3215 | FLAC__bool open_tempfile_(const char *filename, const char *tempfile_path_prefix, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status) |
| 3216 | { |
| 3217 | static const char *tempfile_suffix = ".metadata_edit"; |
| 3218 | if(0 == tempfile_path_prefix) { |
| 3219 | if(0 == (*tempfilename = (char*)safe_malloc_add_3op_(strlen(filename), /*+*/strlen(tempfile_suffix), /*+*/1))) { |
| 3220 | *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 3221 | return false; |
| 3222 | } |
| 3223 | strcpy(*tempfilename, filename); |
| 3224 | strcat(*tempfilename, tempfile_suffix); |
| 3225 | } |
| 3226 | else { |
| 3227 | const char *p = strrchr(filename, '/'); |
| 3228 | if(0 == p) |
| 3229 | p = filename; |
| 3230 | else |
| 3231 | p++; |
| 3232 | |
| 3233 | if(0 == (*tempfilename = (char*)safe_malloc_add_4op_(strlen(tempfile_path_prefix), /*+*/strlen(p), /*+*/strlen(tempfile_suffix), /*+*/2))) { |
| 3234 | *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 3235 | return false; |
| 3236 | } |
| 3237 | strcpy(*tempfilename, tempfile_path_prefix); |
| 3238 | strcat(*tempfilename, "/"); |
| 3239 | strcat(*tempfilename, p); |
| 3240 | strcat(*tempfilename, tempfile_suffix); |
| 3241 | } |
| 3242 | |
| 3243 | if(0 == (*tempfile = fopen(*tempfilename, "w+b"))) { |
| 3244 | *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE; |
| 3245 | return false; |
| 3246 | } |
| 3247 | |
| 3248 | return true; |
| 3249 | } |
| 3250 | |
| 3251 | FLAC__bool transport_tempfile_(const char *filename, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status) |
| 3252 | { |
| 3253 | FLAC__ASSERT(0 != filename); |
| 3254 | FLAC__ASSERT(0 != tempfile); |
| 3255 | FLAC__ASSERT(0 != *tempfile); |
| 3256 | FLAC__ASSERT(0 != tempfilename); |
| 3257 | FLAC__ASSERT(0 != *tempfilename); |
| 3258 | FLAC__ASSERT(0 != status); |
| 3259 | |
| 3260 | (void)fclose(*tempfile); |
| 3261 | *tempfile = 0; |
| 3262 | |
| 3263 | #if defined _MSC_VER || defined __BORLANDC__ || defined __MINGW32__ || defined __EMX__ |
| 3264 | /* on some flavors of windows, rename() will fail if the destination already exists */ |
| 3265 | if(unlink(filename) < 0) { |
| 3266 | cleanup_tempfile_(tempfile, tempfilename); |
| 3267 | *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_UNLINK_ERROR; |
| 3268 | return false; |
| 3269 | } |
| 3270 | #endif |
| 3271 | |
| 3272 | /*@@@ to fully support the tempfile_path_prefix we need to update this piece to actually copy across filesystems instead of just rename(): */ |
| 3273 | if(0 != rename(*tempfilename, filename)) { |
| 3274 | cleanup_tempfile_(tempfile, tempfilename); |
| 3275 | *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR; |
| 3276 | return false; |
| 3277 | } |
| 3278 | |
| 3279 | cleanup_tempfile_(tempfile, tempfilename); |
| 3280 | |
| 3281 | return true; |
| 3282 | } |
| 3283 | |
| 3284 | void cleanup_tempfile_(FILE **tempfile, char **tempfilename) |
| 3285 | { |
| 3286 | if(0 != *tempfile) { |
| 3287 | (void)fclose(*tempfile); |
| 3288 | *tempfile = 0; |
| 3289 | } |
| 3290 | |
| 3291 | if(0 != *tempfilename) { |
| 3292 | (void)unlink(*tempfilename); |
| 3293 | free(*tempfilename); |
| 3294 | *tempfilename = 0; |
| 3295 | } |
| 3296 | } |
| 3297 | |
| 3298 | FLAC__bool get_file_stats_(const char *filename, struct stat *stats) |
| 3299 | { |
| 3300 | FLAC__ASSERT(0 != filename); |
| 3301 | FLAC__ASSERT(0 != stats); |
| 3302 | return (0 == stat(filename, stats)); |
| 3303 | } |
| 3304 | |
| 3305 | void set_file_stats_(const char *filename, struct stat *stats) |
| 3306 | { |
| 3307 | struct utimbuf srctime; |
| 3308 | |
| 3309 | FLAC__ASSERT(0 != filename); |
| 3310 | FLAC__ASSERT(0 != stats); |
| 3311 | |
| 3312 | srctime.actime = stats->st_atime; |
| 3313 | srctime.modtime = stats->st_mtime; |
| 3314 | (void)chmod(filename, stats->st_mode); |
| 3315 | (void)utime(filename, &srctime); |
| 3316 | #if !defined _MSC_VER && !defined __BORLANDC__ && !defined __MINGW32__ && !defined __EMX__ |
| 3317 | (void)chown(filename, stats->st_uid, -1); |
| 3318 | (void)chown(filename, -1, stats->st_gid); |
| 3319 | #endif |
| 3320 | } |
| 3321 | |
| 3322 | int fseek_wrapper_(FLAC__IOHandle handle, FLAC__int64 offset, int whence) |
| 3323 | { |
| 3324 | return fseeko((FILE*)handle, (off_t)offset, whence); |
| 3325 | } |
| 3326 | |
| 3327 | FLAC__int64 ftell_wrapper_(FLAC__IOHandle handle) |
| 3328 | { |
| 3329 | return ftello((FILE*)handle); |
| 3330 | } |
| 3331 | |
| 3332 | FLAC__Metadata_ChainStatus get_equivalent_status_(FLAC__Metadata_SimpleIteratorStatus status) |
| 3333 | { |
| 3334 | switch(status) { |
| 3335 | case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK: |
| 3336 | return FLAC__METADATA_CHAIN_STATUS_OK; |
| 3337 | case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT: |
| 3338 | return FLAC__METADATA_CHAIN_STATUS_ILLEGAL_INPUT; |
| 3339 | case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE: |
| 3340 | return FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE; |
| 3341 | case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE: |
| 3342 | return FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE; |
| 3343 | case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE: |
| 3344 | return FLAC__METADATA_CHAIN_STATUS_NOT_WRITABLE; |
| 3345 | case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA: |
| 3346 | return FLAC__METADATA_CHAIN_STATUS_BAD_METADATA; |
| 3347 | case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR: |
| 3348 | return FLAC__METADATA_CHAIN_STATUS_READ_ERROR; |
| 3349 | case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR: |
| 3350 | return FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR; |
| 3351 | case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR: |
| 3352 | return FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR; |
| 3353 | case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR: |
| 3354 | return FLAC__METADATA_CHAIN_STATUS_RENAME_ERROR; |
| 3355 | case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_UNLINK_ERROR: |
| 3356 | return FLAC__METADATA_CHAIN_STATUS_UNLINK_ERROR; |
| 3357 | case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR: |
| 3358 | return FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR; |
| 3359 | case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_INTERNAL_ERROR: |
| 3360 | default: |
| 3361 | return FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; |
| 3362 | } |
| 3363 | } |
src/lib/libflac/libflac/lpc.c
| r0 | r14135 | |
| 1 | /* libFLAC - Free Lossless Audio Codec library |
| 2 | * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * |
| 8 | * - Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * - Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * - Neither the name of the Xiph.org Foundation nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived from |
| 17 | * this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 26 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 27 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #if HAVE_CONFIG_H |
| 33 | # include <config.h> |
| 34 | #endif |
| 35 | |
| 36 | #include <math.h> |
| 37 | #include "flac/assert.h" |
| 38 | #include "flac/format.h" |
| 39 | #include "private/bitmath.h" |
| 40 | #include "private/lpc.h" |
| 41 | #if defined DEBUG || defined FLAC__OVERFLOW_DETECT || defined FLAC__OVERFLOW_DETECT_VERBOSE |
| 42 | #include <stdio.h> |
| 43 | #endif |
| 44 | |
| 45 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 46 | |
| 47 | #ifndef M_LN2 |
| 48 | /* math.h in VC++ doesn't seem to have this (how Microsoft is that?) */ |
| 49 | #define M_LN2 0.69314718055994530942 |
| 50 | #endif |
| 51 | |
| 52 | /* OPT: #undef'ing this may improve the speed on some architectures */ |
| 53 | #define FLAC__LPC_UNROLLED_FILTER_LOOPS |
| 54 | |
| 55 | |
| 56 | void FLAC__lpc_window_data(const FLAC__int32 in[], const FLAC__real window[], FLAC__real out[], unsigned data_len) |
| 57 | { |
| 58 | unsigned i; |
| 59 | for(i = 0; i < data_len; i++) |
| 60 | out[i] = in[i] * window[i]; |
| 61 | } |
| 62 | |
| 63 | void FLAC__lpc_compute_autocorrelation(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]) |
| 64 | { |
| 65 | /* a readable, but slower, version */ |
| 66 | #if 0 |
| 67 | FLAC__real d; |
| 68 | unsigned i; |
| 69 | |
| 70 | FLAC__ASSERT(lag > 0); |
| 71 | FLAC__ASSERT(lag <= data_len); |
| 72 | |
| 73 | /* |
| 74 | * Technically we should subtract the mean first like so: |
| 75 | * for(i = 0; i < data_len; i++) |
| 76 | * data[i] -= mean; |
| 77 | * but it appears not to make enough of a difference to matter, and |
| 78 | * most signals are already closely centered around zero |
| 79 | */ |
| 80 | while(lag--) { |
| 81 | for(i = lag, d = 0.0; i < data_len; i++) |
| 82 | d += data[i] * data[i - lag]; |
| 83 | autoc[lag] = d; |
| 84 | } |
| 85 | #endif |
| 86 | |
| 87 | /* |
| 88 | * this version tends to run faster because of better data locality |
| 89 | * ('data_len' is usually much larger than 'lag') |
| 90 | */ |
| 91 | FLAC__real d; |
| 92 | unsigned sample, coeff; |
| 93 | const unsigned limit = data_len - lag; |
| 94 | |
| 95 | FLAC__ASSERT(lag > 0); |
| 96 | FLAC__ASSERT(lag <= data_len); |
| 97 | |
| 98 | for(coeff = 0; coeff < lag; coeff++) |
| 99 | autoc[coeff] = 0.0; |
| 100 | for(sample = 0; sample <= limit; sample++) { |
| 101 | d = data[sample]; |
| 102 | for(coeff = 0; coeff < lag; coeff++) |
| 103 | autoc[coeff] += d * data[sample+coeff]; |
| 104 | } |
| 105 | for(; sample < data_len; sample++) { |
| 106 | d = data[sample]; |
| 107 | for(coeff = 0; coeff < data_len - sample; coeff++) |
| 108 | autoc[coeff] += d * data[sample+coeff]; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | void FLAC__lpc_compute_lp_coefficients(const FLAC__real autoc[], unsigned *max_order, FLAC__real lp_coeff[][FLAC__MAX_LPC_ORDER], FLAC__double error[]) |
| 113 | { |
| 114 | unsigned i, j; |
| 115 | FLAC__double r, err, ref[FLAC__MAX_LPC_ORDER], lpc[FLAC__MAX_LPC_ORDER]; |
| 116 | |
| 117 | FLAC__ASSERT(0 != max_order); |
| 118 | FLAC__ASSERT(0 < *max_order); |
| 119 | FLAC__ASSERT(*max_order <= FLAC__MAX_LPC_ORDER); |
| 120 | FLAC__ASSERT(autoc[0] != 0.0); |
| 121 | |
| 122 | err = autoc[0]; |
| 123 | |
| 124 | for(i = 0; i < *max_order; i++) { |
| 125 | /* Sum up this iteration's reflection coefficient. */ |
| 126 | r = -autoc[i+1]; |
| 127 | for(j = 0; j < i; j++) |
| 128 | r -= lpc[j] * autoc[i-j]; |
| 129 | ref[i] = (r/=err); |
| 130 | |
| 131 | /* Update LPC coefficients and total error. */ |
| 132 | lpc[i]=r; |
| 133 | for(j = 0; j < (i>>1); j++) { |
| 134 | FLAC__double tmp = lpc[j]; |
| 135 | lpc[j] += r * lpc[i-1-j]; |
| 136 | lpc[i-1-j] += r * tmp; |
| 137 | } |
| 138 | if(i & 1) |
| 139 | lpc[j] += lpc[j] * r; |
| 140 | |
| 141 | err *= (1.0 - r * r); |
| 142 | |
| 143 | /* save this order */ |
| 144 | for(j = 0; j <= i; j++) |
| 145 | lp_coeff[i][j] = (FLAC__real)(-lpc[j]); /* negate FIR filter coeff to get predictor coeff */ |
| 146 | error[i] = err; |
| 147 | |
| 148 | /* see SF bug #1601812 http://sourceforge.net/tracker/index.php?func=detail&aid=1601812&group_id=13478&atid=113478 */ |
| 149 | if(err == 0.0) { |
| 150 | *max_order = i+1; |
| 151 | return; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | int FLAC__lpc_quantize_coefficients(const FLAC__real lp_coeff[], unsigned order, unsigned precision, FLAC__int32 qlp_coeff[], int *shift) |
| 157 | { |
| 158 | unsigned i; |
| 159 | FLAC__double cmax; |
| 160 | FLAC__int32 qmax, qmin; |
| 161 | |
| 162 | FLAC__ASSERT(precision > 0); |
| 163 | FLAC__ASSERT(precision >= FLAC__MIN_QLP_COEFF_PRECISION); |
| 164 | |
| 165 | /* drop one bit for the sign; from here on out we consider only |lp_coeff[i]| */ |
| 166 | precision--; |
| 167 | qmax = 1 << precision; |
| 168 | qmin = -qmax; |
| 169 | qmax--; |
| 170 | |
| 171 | /* calc cmax = max( |lp_coeff[i]| ) */ |
| 172 | cmax = 0.0; |
| 173 | for(i = 0; i < order; i++) { |
| 174 | const FLAC__double d = fabs(lp_coeff[i]); |
| 175 | if(d > cmax) |
| 176 | cmax = d; |
| 177 | } |
| 178 | |
| 179 | if(cmax <= 0.0) { |
| 180 | /* => coefficients are all 0, which means our constant-detect didn't work */ |
| 181 | return 2; |
| 182 | } |
| 183 | else { |
| 184 | const int max_shiftlimit = (1 << (FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN-1)) - 1; |
| 185 | const int min_shiftlimit = -max_shiftlimit - 1; |
| 186 | int log2cmax; |
| 187 | |
| 188 | (void)frexp(cmax, &log2cmax); |
| 189 | log2cmax--; |
| 190 | *shift = (int)precision - log2cmax - 1; |
| 191 | |
| 192 | if(*shift > max_shiftlimit) |
| 193 | *shift = max_shiftlimit; |
| 194 | else if(*shift < min_shiftlimit) |
| 195 | return 1; |
| 196 | } |
| 197 | |
| 198 | if(*shift >= 0) { |
| 199 | FLAC__double error = 0.0; |
| 200 | FLAC__int32 q; |
| 201 | for(i = 0; i < order; i++) { |
| 202 | error += lp_coeff[i] * (1 << *shift); |
| 203 | #if 1 /* unfortunately lround() is C99 */ |
| 204 | if(error >= 0.0) |
| 205 | q = (FLAC__int32)(error + 0.5); |
| 206 | else |
| 207 | q = (FLAC__int32)(error - 0.5); |
| 208 | #else |
| 209 | q = lround(error); |
| 210 | #endif |
| 211 | #ifdef FLAC__OVERFLOW_DETECT |
| 212 | if(q > qmax+1) /* we expect q==qmax+1 occasionally due to rounding */ |
| 213 | fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q>qmax %d>%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmax,*shift,cmax,precision+1,i,lp_coeff[i]); |
| 214 | else if(q < qmin) |
| 215 | fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q<qmin %d<%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmin,*shift,cmax,precision+1,i,lp_coeff[i]); |
| 216 | #endif |
| 217 | if(q > qmax) |
| 218 | q = qmax; |
| 219 | else if(q < qmin) |
| 220 | q = qmin; |
| 221 | error -= q; |
| 222 | qlp_coeff[i] = q; |
| 223 | } |
| 224 | } |
| 225 | /* negative shift is very rare but due to design flaw, negative shift is |
| 226 | * a NOP in the decoder, so it must be handled specially by scaling down |
| 227 | * coeffs |
| 228 | */ |
| 229 | else { |
| 230 | const int nshift = -(*shift); |
| 231 | FLAC__double error = 0.0; |
| 232 | FLAC__int32 q; |
| 233 | #ifdef DEBUG |
| 234 | fprintf(stderr,"FLAC__lpc_quantize_coefficients: negative shift=%d order=%u cmax=%f\n", *shift, order, cmax); |
| 235 | #endif |
| 236 | for(i = 0; i < order; i++) { |
| 237 | error += lp_coeff[i] / (1 << nshift); |
| 238 | #if 1 /* unfortunately lround() is C99 */ |
| 239 | if(error >= 0.0) |
| 240 | q = (FLAC__int32)(error + 0.5); |
| 241 | else |
| 242 | q = (FLAC__int32)(error - 0.5); |
| 243 | #else |
| 244 | q = lround(error); |
| 245 | #endif |
| 246 | #ifdef FLAC__OVERFLOW_DETECT |
| 247 | if(q > qmax+1) /* we expect q==qmax+1 occasionally due to rounding */ |
| 248 | fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q>qmax %d>%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmax,*shift,cmax,precision+1,i,lp_coeff[i]); |
| 249 | else if(q < qmin) |
| 250 | fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q<qmin %d<%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmin,*shift,cmax,precision+1,i,lp_coeff[i]); |
| 251 | #endif |
| 252 | if(q > qmax) |
| 253 | q = qmax; |
| 254 | else if(q < qmin) |
| 255 | q = qmin; |
| 256 | error -= q; |
| 257 | qlp_coeff[i] = q; |
| 258 | } |
| 259 | *shift = 0; |
| 260 | } |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | void FLAC__lpc_compute_residual_from_qlp_coefficients(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]) |
| 266 | #if defined(FLAC__OVERFLOW_DETECT) || !defined(FLAC__LPC_UNROLLED_FILTER_LOOPS) |
| 267 | { |
| 268 | FLAC__int64 sumo; |
| 269 | unsigned i, j; |
| 270 | FLAC__int32 sum; |
| 271 | const FLAC__int32 *history; |
| 272 | |
| 273 | #ifdef FLAC__OVERFLOW_DETECT_VERBOSE |
| 274 | fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization); |
| 275 | for(i=0;i<order;i++) |
| 276 | fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]); |
| 277 | fprintf(stderr,"\n"); |
| 278 | #endif |
| 279 | FLAC__ASSERT(order > 0); |
| 280 | |
| 281 | for(i = 0; i < data_len; i++) { |
| 282 | sumo = 0; |
| 283 | sum = 0; |
| 284 | history = data; |
| 285 | for(j = 0; j < order; j++) { |
| 286 | sum += qlp_coeff[j] * (*(--history)); |
| 287 | sumo += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*history); |
| 288 | #if defined _MSC_VER |
| 289 | if(sumo > 2147483647I64 || sumo < -2147483648I64) |
| 290 | fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%I64d\n",i,j,qlp_coeff[j],*history,sumo); |
| 291 | #else |
| 292 | if(sumo > 2147483647ll || sumo < -2147483648ll) |
| 293 | fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%lld\n",i,j,qlp_coeff[j],*history,(long long)sumo); |
| 294 | #endif |
| 295 | } |
| 296 | *(residual++) = *(data++) - (sum >> lp_quantization); |
| 297 | } |
| 298 | |
| 299 | /* Here's a slower but clearer version: |
| 300 | for(i = 0; i < data_len; i++) { |
| 301 | sum = 0; |
| 302 | for(j = 0; j < order; j++) |
| 303 | sum += qlp_coeff[j] * data[i-j-1]; |
| 304 | residual[i] = data[i] - (sum >> lp_quantization); |
| 305 | } |
| 306 | */ |
| 307 | } |
| 308 | #else /* fully unrolled version for normal use */ |
| 309 | { |
| 310 | int i; |
| 311 | FLAC__int32 sum; |
| 312 | |
| 313 | FLAC__ASSERT(order > 0); |
| 314 | FLAC__ASSERT(order <= 32); |
| 315 | |
| 316 | /* |
| 317 | * We do unique versions up to 12th order since that's the subset limit. |
| 318 | * Also they are roughly ordered to match frequency of occurrence to |
| 319 | * minimize branching. |
| 320 | */ |
| 321 | if(order <= 12) { |
| 322 | if(order > 8) { |
| 323 | if(order > 10) { |
| 324 | if(order == 12) { |
| 325 | for(i = 0; i < (int)data_len; i++) { |
| 326 | sum = 0; |
| 327 | sum += qlp_coeff[11] * data[i-12]; |
| 328 | sum += qlp_coeff[10] * data[i-11]; |
| 329 | sum += qlp_coeff[9] * data[i-10]; |
| 330 | sum += qlp_coeff[8] * data[i-9]; |
| 331 | sum += qlp_coeff[7] * data[i-8]; |
| 332 | sum += qlp_coeff[6] * data[i-7]; |
| 333 | sum += qlp_coeff[5] * data[i-6]; |
| 334 | sum += qlp_coeff[4] * data[i-5]; |
| 335 | sum += qlp_coeff[3] * data[i-4]; |
| 336 | sum += qlp_coeff[2] * data[i-3]; |
| 337 | sum += qlp_coeff[1] * data[i-2]; |
| 338 | sum += qlp_coeff[0] * data[i-1]; |
| 339 | residual[i] = data[i] - (sum >> lp_quantization); |
| 340 | } |
| 341 | } |
| 342 | else { /* order == 11 */ |
| 343 | for(i = 0; i < (int)data_len; i++) { |
| 344 | sum = 0; |
| 345 | sum += qlp_coeff[10] * data[i-11]; |
| 346 | sum += qlp_coeff[9] * data[i-10]; |
| 347 | sum += qlp_coeff[8] * data[i-9]; |
| 348 | sum += qlp_coeff[7] * data[i-8]; |
| 349 | sum += qlp_coeff[6] * data[i-7]; |
| 350 | sum += qlp_coeff[5] * data[i-6]; |
| 351 | sum += qlp_coeff[4] * data[i-5]; |
| 352 | sum += qlp_coeff[3] * data[i-4]; |
| 353 | sum += qlp_coeff[2] * data[i-3]; |
| 354 | sum += qlp_coeff[1] * data[i-2]; |
| 355 | sum += qlp_coeff[0] * data[i-1]; |
| 356 | residual[i] = data[i] - (sum >> lp_quantization); |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | else { |
| 361 | if(order == 10) { |
| 362 | for(i = 0; i < (int)data_len; i++) { |
| 363 | sum = 0; |
| 364 | sum += qlp_coeff[9] * data[i-10]; |
| 365 | sum += qlp_coeff[8] * data[i-9]; |
| 366 | sum += qlp_coeff[7] * data[i-8]; |
| 367 | sum += qlp_coeff[6] * data[i-7]; |
| 368 | sum += qlp_coeff[5] * data[i-6]; |
| 369 | sum += qlp_coeff[4] * data[i-5]; |
| 370 | sum += qlp_coeff[3] * data[i-4]; |
| 371 | sum += qlp_coeff[2] * data[i-3]; |
| 372 | sum += qlp_coeff[1] * data[i-2]; |
| 373 | sum += qlp_coeff[0] * data[i-1]; |
| 374 | residual[i] = data[i] - (sum >> lp_quantization); |
| 375 | } |
| 376 | } |
| 377 | else { /* order == 9 */ |
| 378 | for(i = 0; i < (int)data_len; i++) { |
| 379 | sum = 0; |
| 380 | sum += qlp_coeff[8] * data[i-9]; |
| 381 | sum += qlp_coeff[7] * data[i-8]; |
| 382 | sum += qlp_coeff[6] * data[i-7]; |
| 383 | sum += qlp_coeff[5] * data[i-6]; |
| 384 | sum += qlp_coeff[4] * data[i-5]; |
| 385 | sum += qlp_coeff[3] * data[i-4]; |
| 386 | sum += qlp_coeff[2] * data[i-3]; |
| 387 | sum += qlp_coeff[1] * data[i-2]; |
| 388 | sum += qlp_coeff[0] * data[i-1]; |
| 389 | residual[i] = data[i] - (sum >> lp_quantization); |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | else if(order > 4) { |
| 395 | if(order > 6) { |
| 396 | if(order == 8) { |
| 397 | for(i = 0; i < (int)data_len; i++) { |
| 398 | sum = 0; |
| 399 | sum += qlp_coeff[7] * data[i-8]; |
| 400 | sum += qlp_coeff[6] * data[i-7]; |
| 401 | sum += qlp_coeff[5] * data[i-6]; |
| 402 | sum += qlp_coeff[4] * data[i-5]; |
| 403 | sum += qlp_coeff[3] * data[i-4]; |
| 404 | sum += qlp_coeff[2] * data[i-3]; |
| 405 | sum += qlp_coeff[1] * data[i-2]; |
| 406 | sum += qlp_coeff[0] * data[i-1]; |
| 407 | residual[i] = data[i] - (sum >> lp_quantization); |
| 408 | } |
| 409 | } |
| 410 | else { /* order == 7 */ |
| 411 | for(i = 0; i < (int)data_len; i++) { |
| 412 | sum = 0; |
| 413 | sum += qlp_coeff[6] * data[i-7]; |
| 414 | sum += qlp_coeff[5] * data[i-6]; |
| 415 | sum += qlp_coeff[4] * data[i-5]; |
| 416 | sum += qlp_coeff[3] * data[i-4]; |
| 417 | sum += qlp_coeff[2] * data[i-3]; |
| 418 | sum += qlp_coeff[1] * data[i-2]; |
| 419 | sum += qlp_coeff[0] * data[i-1]; |
| 420 | residual[i] = data[i] - (sum >> lp_quantization); |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | else { |
| 425 | if(order == 6) { |
| 426 | for(i = 0; i < (int)data_len; i++) { |
| 427 | sum = 0; |
| 428 | sum += qlp_coeff[5] * data[i-6]; |
| 429 | sum += qlp_coeff[4] * data[i-5]; |
| 430 | sum += qlp_coeff[3] * data[i-4]; |
| 431 | sum += qlp_coeff[2] * data[i-3]; |
| 432 | sum += qlp_coeff[1] * data[i-2]; |
| 433 | sum += qlp_coeff[0] * data[i-1]; |
| 434 | residual[i] = data[i] - (sum >> lp_quantization); |
| 435 | } |
| 436 | } |
| 437 | else { /* order == 5 */ |
| 438 | for(i = 0; i < (int)data_len; i++) { |
| 439 | sum = 0; |
| 440 | sum += qlp_coeff[4] * data[i-5]; |
| 441 | sum += qlp_coeff[3] * data[i-4]; |
| 442 | sum += qlp_coeff[2] * data[i-3]; |
| 443 | sum += qlp_coeff[1] * data[i-2]; |
| 444 | sum += qlp_coeff[0] * data[i-1]; |
| 445 | residual[i] = data[i] - (sum >> lp_quantization); |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | else { |
| 451 | if(order > 2) { |
| 452 | if(order == 4) { |
| 453 | for(i = 0; i < (int)data_len; i++) { |
| 454 | sum = 0; |
| 455 | sum += qlp_coeff[3] * data[i-4]; |
| 456 | sum += qlp_coeff[2] * data[i-3]; |
| 457 | sum += qlp_coeff[1] * data[i-2]; |
| 458 | sum += qlp_coeff[0] * data[i-1]; |
| 459 | residual[i] = data[i] - (sum >> lp_quantization); |
| 460 | } |
| 461 | } |
| 462 | else { /* order == 3 */ |
| 463 | for(i = 0; i < (int)data_len; i++) { |
| 464 | sum = 0; |
| 465 | sum += qlp_coeff[2] * data[i-3]; |
| 466 | sum += qlp_coeff[1] * data[i-2]; |
| 467 | sum += qlp_coeff[0] * data[i-1]; |
| 468 | residual[i] = data[i] - (sum >> lp_quantization); |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | else { |
| 473 | if(order == 2) { |
| 474 | for(i = 0; i < (int)data_len; i++) { |
| 475 | sum = 0; |
| 476 | sum += qlp_coeff[1] * data[i-2]; |
| 477 | sum += qlp_coeff[0] * data[i-1]; |
| 478 | residual[i] = data[i] - (sum >> lp_quantization); |
| 479 | } |
| 480 | } |
| 481 | else { /* order == 1 */ |
| 482 | for(i = 0; i < (int)data_len; i++) |
| 483 | residual[i] = data[i] - ((qlp_coeff[0] * data[i-1]) >> lp_quantization); |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | else { /* order > 12 */ |
| 489 | for(i = 0; i < (int)data_len; i++) { |
| 490 | sum = 0; |
| 491 | switch(order) { |
| 492 | case 32: sum += qlp_coeff[31] * data[i-32]; |
| 493 | case 31: sum += qlp_coeff[30] * data[i-31]; |
| 494 | case 30: sum += qlp_coeff[29] * data[i-30]; |
| 495 | case 29: sum += qlp_coeff[28] * data[i-29]; |
| 496 | case 28: sum += qlp_coeff[27] * data[i-28]; |
| 497 | case 27: sum += qlp_coeff[26] * data[i-27]; |
| 498 | case 26: sum += qlp_coeff[25] * data[i-26]; |
| 499 | case 25: sum += qlp_coeff[24] * data[i-25]; |
| 500 | case 24: sum += qlp_coeff[23] * data[i-24]; |
| 501 | case 23: sum += qlp_coeff[22] * data[i-23]; |
| 502 | case 22: sum += qlp_coeff[21] * data[i-22]; |
| 503 | case 21: sum += qlp_coeff[20] * data[i-21]; |
| 504 | case 20: sum += qlp_coeff[19] * data[i-20]; |
| 505 | case 19: sum += qlp_coeff[18] * data[i-19]; |
| 506 | case 18: sum += qlp_coeff[17] * data[i-18]; |
| 507 | case 17: sum += qlp_coeff[16] * data[i-17]; |
| 508 | case 16: sum += qlp_coeff[15] * data[i-16]; |
| 509 | case 15: sum += qlp_coeff[14] * data[i-15]; |
| 510 | case 14: sum += qlp_coeff[13] * data[i-14]; |
| 511 | case 13: sum += qlp_coeff[12] * data[i-13]; |
| 512 | sum += qlp_coeff[11] * data[i-12]; |
| 513 | sum += qlp_coeff[10] * data[i-11]; |
| 514 | sum += qlp_coeff[ 9] * data[i-10]; |
| 515 | sum += qlp_coeff[ 8] * data[i- 9]; |
| 516 | sum += qlp_coeff[ 7] * data[i- 8]; |
| 517 | sum += qlp_coeff[ 6] * data[i- 7]; |
| 518 | sum += qlp_coeff[ 5] * data[i- 6]; |
| 519 | sum += qlp_coeff[ 4] * data[i- 5]; |
| 520 | sum += qlp_coeff[ 3] * data[i- 4]; |
| 521 | sum += qlp_coeff[ 2] * data[i- 3]; |
| 522 | sum += qlp_coeff[ 1] * data[i- 2]; |
| 523 | sum += qlp_coeff[ 0] * data[i- 1]; |
| 524 | } |
| 525 | residual[i] = data[i] - (sum >> lp_quantization); |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | #endif |
| 530 | |
| 531 | void FLAC__lpc_compute_residual_from_qlp_coefficients_wide(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]) |
| 532 | #if defined(FLAC__OVERFLOW_DETECT) || !defined(FLAC__LPC_UNROLLED_FILTER_LOOPS) |
| 533 | { |
| 534 | unsigned i, j; |
| 535 | FLAC__int64 sum; |
| 536 | const FLAC__int32 *history; |
| 537 | |
| 538 | #ifdef FLAC__OVERFLOW_DETECT_VERBOSE |
| 539 | fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization); |
| 540 | for(i=0;i<order;i++) |
| 541 | fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]); |
| 542 | fprintf(stderr,"\n"); |
| 543 | #endif |
| 544 | FLAC__ASSERT(order > 0); |
| 545 | |
| 546 | for(i = 0; i < data_len; i++) { |
| 547 | sum = 0; |
| 548 | history = data; |
| 549 | for(j = 0; j < order; j++) |
| 550 | sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history)); |
| 551 | if(FLAC__bitmath_silog2_wide(sum >> lp_quantization) > 32) { |
| 552 | #if defined _MSC_VER |
| 553 | fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, sum=%I64d\n", i, sum >> lp_quantization); |
| 554 | #else |
| 555 | fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, sum=%lld\n", i, (long long)(sum >> lp_quantization)); |
| 556 | #endif |
| 557 | break; |
| 558 | } |
| 559 | if(FLAC__bitmath_silog2_wide((FLAC__int64)(*data) - (sum >> lp_quantization)) > 32) { |
| 560 | #if defined _MSC_VER |
| 561 | fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, data=%d, sum=%I64d, residual=%I64d\n", i, *data, sum >> lp_quantization, (FLAC__int64)(*data) - (sum >> lp_quantization)); |
| 562 | #else |
| 563 | fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, data=%d, sum=%lld, residual=%lld\n", i, *data, (long long)(sum >> lp_quantization), (long long)((FLAC__int64)(*data) - (sum >> lp_quantization))); |
| 564 | #endif |
| 565 | break; |
| 566 | } |
| 567 | *(residual++) = *(data++) - (FLAC__int32)(sum >> lp_quantization); |
| 568 | } |
| 569 | } |
| 570 | #else /* fully unrolled version for normal use */ |
| 571 | { |
| 572 | int i; |
| 573 | FLAC__int64 sum; |
| 574 | |
| 575 | FLAC__ASSERT(order > 0); |
| 576 | FLAC__ASSERT(order <= 32); |
| 577 | |
| 578 | /* |
| 579 | * We do unique versions up to 12th order since that's the subset limit. |
| 580 | * Also they are roughly ordered to match frequency of occurrence to |
| 581 | * minimize branching. |
| 582 | */ |
| 583 | if(order <= 12) { |
| 584 | if(order > 8) { |
| 585 | if(order > 10) { |
| 586 | if(order == 12) { |
| 587 | for(i = 0; i < (int)data_len; i++) { |
| 588 | sum = 0; |
| 589 | sum += qlp_coeff[11] * (FLAC__int64)data[i-12]; |
| 590 | sum += qlp_coeff[10] * (FLAC__int64)data[i-11]; |
| 591 | sum += qlp_coeff[9] * (FLAC__int64)data[i-10]; |
| 592 | sum += qlp_coeff[8] * (FLAC__int64)data[i-9]; |
| 593 | sum += qlp_coeff[7] * (FLAC__int64)data[i-8]; |
| 594 | sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; |
| 595 | sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; |
| 596 | sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; |
| 597 | sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; |
| 598 | sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; |
| 599 | sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; |
| 600 | sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; |
| 601 | residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); |
| 602 | } |
| 603 | } |
| 604 | else { /* order == 11 */ |
| 605 | for(i = 0; i < (int)data_len; i++) { |
| 606 | sum = 0; |
| 607 | sum += qlp_coeff[10] * (FLAC__int64)data[i-11]; |
| 608 | sum += qlp_coeff[9] * (FLAC__int64)data[i-10]; |
| 609 | sum += qlp_coeff[8] * (FLAC__int64)data[i-9]; |
| 610 | sum += qlp_coeff[7] * (FLAC__int64)data[i-8]; |
| 611 | sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; |
| 612 | sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; |
| 613 | sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; |
| 614 | sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; |
| 615 | sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; |
| 616 | sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; |
| 617 | sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; |
| 618 | residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); |
| 619 | } |
| 620 | } |
| 621 | } |
| 622 | else { |
| 623 | if(order == 10) { |
| 624 | for(i = 0; i < (int)data_len; i++) { |
| 625 | sum = 0; |
| 626 | sum += qlp_coeff[9] * (FLAC__int64)data[i-10]; |
| 627 | sum += qlp_coeff[8] * (FLAC__int64)data[i-9]; |
| 628 | sum += qlp_coeff[7] * (FLAC__int64)data[i-8]; |
| 629 | sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; |
| 630 | sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; |
| 631 | sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; |
| 632 | sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; |
| 633 | sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; |
| 634 | sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; |
| 635 | sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; |
| 636 | residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); |
| 637 | } |
| 638 | } |
| 639 | else { /* order == 9 */ |
| 640 | for(i = 0; i < (int)data_len; i++) { |
| 641 | sum = 0; |
| 642 | sum += qlp_coeff[8] * (FLAC__int64)data[i-9]; |
| 643 | sum += qlp_coeff[7] * (FLAC__int64)data[i-8]; |
| 644 | sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; |
| 645 | sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; |
| 646 | sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; |
| 647 | sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; |
| 648 | sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; |
| 649 | sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; |
| 650 | sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; |
| 651 | residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | else if(order > 4) { |
| 657 | if(order > 6) { |
| 658 | if(order == 8) { |
| 659 | for(i = 0; i < (int)data_len; i++) { |
| 660 | sum = 0; |
| 661 | sum += qlp_coeff[7] * (FLAC__int64)data[i-8]; |
| 662 | sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; |
| 663 | sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; |
| 664 | sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; |
| 665 | sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; |
| 666 | sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; |
| 667 | sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; |
| 668 | sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; |
| 669 | residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); |
| 670 | } |
| 671 | } |
| 672 | else { /* order == 7 */ |
| 673 | for(i = 0; i < (int)data_len; i++) { |
| 674 | sum = 0; |
| 675 | sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; |
| 676 | sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; |
| 677 | sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; |
| 678 | sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; |
| 679 | sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; |
| 680 | sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; |
| 681 | sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; |
| 682 | residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); |
| 683 | } |
| 684 | } |
| 685 | } |
| 686 | else { |
| 687 | if(order == 6) { |
| 688 | for(i = 0; i < (int)data_len; i++) { |
| 689 | sum = 0; |
| 690 | sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; |
| 691 | sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; |
| 692 | sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; |
| 693 | sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; |
| 694 | sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; |
| 695 | sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; |
| 696 | residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); |
| 697 | } |
| 698 | } |
| 699 | else { /* order == 5 */ |
| 700 | for(i = 0; i < (int)data_len; i++) { |
| 701 | sum = 0; |
| 702 | sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; |
| 703 | sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; |
| 704 | sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; |
| 705 | sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; |
| 706 | sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; |
| 707 | residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); |
| 708 | } |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | else { |
| 713 | if(order > 2) { |
| 714 | if(order == 4) { |
| 715 | for(i = 0; i < (int)data_len; i++) { |
| 716 | sum = 0; |
| 717 | sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; |
| 718 | sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; |
| 719 | sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; |
| 720 | sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; |
| 721 | residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); |
| 722 | } |
| 723 | } |
| 724 | else { /* order == 3 */ |
| 725 | for(i = 0; i < (int)data_len; i++) { |
| 726 | sum = 0; |
| 727 | sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; |
| 728 | sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; |
| 729 | sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; |
| 730 | residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); |
| 731 | } |
| 732 | } |
| 733 | } |
| 734 | else { |
| 735 | if(order == 2) { |
| 736 | for(i = 0; i < (int)data_len; i++) { |
| 737 | sum = 0; |
| 738 | sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; |
| 739 | sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; |
| 740 | residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); |
| 741 | } |
| 742 | } |
| 743 | else { /* order == 1 */ |
| 744 | for(i = 0; i < (int)data_len; i++) |
| 745 | residual[i] = data[i] - (FLAC__int32)((qlp_coeff[0] * (FLAC__int64)data[i-1]) >> lp_quantization); |
| 746 | } |
| 747 | } |
| 748 | } |
| 749 | } |
| 750 | else { /* order > 12 */ |
| 751 | for(i = 0; i < (int)data_len; i++) { |
| 752 | sum = 0; |
| 753 | switch(order) { |
| 754 | case 32: sum += qlp_coeff[31] * (FLAC__int64)data[i-32]; |
| 755 | case 31: sum += qlp_coeff[30] * (FLAC__int64)data[i-31]; |
| 756 | case 30: sum += qlp_coeff[29] * (FLAC__int64)data[i-30]; |
| 757 | case 29: sum += qlp_coeff[28] * (FLAC__int64)data[i-29]; |
| 758 | case 28: sum += qlp_coeff[27] * (FLAC__int64)data[i-28]; |
| 759 | case 27: sum += qlp_coeff[26] * (FLAC__int64)data[i-27]; |
| 760 | case 26: sum += qlp_coeff[25] * (FLAC__int64)data[i-26]; |
| 761 | case 25: sum += qlp_coeff[24] * (FLAC__int64)data[i-25]; |
| 762 | case 24: sum += qlp_coeff[23] * (FLAC__int64)data[i-24]; |
| 763 | case 23: sum += qlp_coeff[22] * (FLAC__int64)data[i-23]; |
| 764 | case 22: sum += qlp_coeff[21] * (FLAC__int64)data[i-22]; |
| 765 | case 21: sum += qlp_coeff[20] * (FLAC__int64)data[i-21]; |
| 766 | case 20: sum += qlp_coeff[19] * (FLAC__int64)data[i-20]; |
| 767 | case 19: sum += qlp_coeff[18] * (FLAC__int64)data[i-19]; |
| 768 | case 18: sum += qlp_coeff[17] * (FLAC__int64)data[i-18]; |
| 769 | case 17: sum += qlp_coeff[16] * (FLAC__int64)data[i-17]; |
| 770 | case 16: sum += qlp_coeff[15] * (FLAC__int64)data[i-16]; |
| 771 | case 15: sum += qlp_coeff[14] * (FLAC__int64)data[i-15]; |
| 772 | case 14: sum += qlp_coeff[13] * (FLAC__int64)data[i-14]; |
| 773 | case 13: sum += qlp_coeff[12] * (FLAC__int64)data[i-13]; |
| 774 | sum += qlp_coeff[11] * (FLAC__int64)data[i-12]; |
| 775 | sum += qlp_coeff[10] * (FLAC__int64)data[i-11]; |
| 776 | sum += qlp_coeff[ 9] * (FLAC__int64)data[i-10]; |
| 777 | sum += qlp_coeff[ 8] * (FLAC__int64)data[i- 9]; |
| 778 | sum += qlp_coeff[ 7] * (FLAC__int64)data[i- 8]; |
| 779 | sum += qlp_coeff[ 6] * (FLAC__int64)data[i- 7]; |
| 780 | sum += qlp_coeff[ 5] * (FLAC__int64)data[i- 6]; |
| 781 | sum += qlp_coeff[ 4] * (FLAC__int64)data[i- 5]; |
| 782 | sum += qlp_coeff[ 3] * (FLAC__int64)data[i- 4]; |
| 783 | sum += qlp_coeff[ 2] * (FLAC__int64)data[i- 3]; |
| 784 | sum += qlp_coeff[ 1] * (FLAC__int64)data[i- 2]; |
| 785 | sum += qlp_coeff[ 0] * (FLAC__int64)data[i- 1]; |
| 786 | } |
| 787 | residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); |
| 788 | } |
| 789 | } |
| 790 | } |
| 791 | #endif |
| 792 | |
| 793 | #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */ |
| 794 | |
| 795 | void FLAC__lpc_restore_signal(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]) |
| 796 | #if defined(FLAC__OVERFLOW_DETECT) || !defined(FLAC__LPC_UNROLLED_FILTER_LOOPS) |
| 797 | { |
| 798 | FLAC__int64 sumo; |
| 799 | unsigned i, j; |
| 800 | FLAC__int32 sum; |
| 801 | const FLAC__int32 *r = residual, *history; |
| 802 | |
| 803 | #ifdef FLAC__OVERFLOW_DETECT_VERBOSE |
| 804 | fprintf(stderr,"FLAC__lpc_restore_signal: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization); |
| 805 | for(i=0;i<order;i++) |
| 806 | fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]); |
| 807 | fprintf(stderr,"\n"); |
| 808 | #endif |
| 809 | FLAC__ASSERT(order > 0); |
| 810 | |
| 811 | for(i = 0; i < data_len; i++) { |
| 812 | sumo = 0; |
| 813 | sum = 0; |
| 814 | history = data; |
| 815 | for(j = 0; j < order; j++) { |
| 816 | sum += qlp_coeff[j] * (*(--history)); |
| 817 | sumo += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*history); |
| 818 | #if defined _MSC_VER |
| 819 | if(sumo > 2147483647I64 || sumo < -2147483648I64) |
| 820 | fprintf(stderr,"FLAC__lpc_restore_signal: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%I64d\n",i,j,qlp_coeff[j],*history,sumo); |
| 821 | #else |
| 822 | if(sumo > 2147483647ll || sumo < -2147483648ll) |
| 823 | fprintf(stderr,"FLAC__lpc_restore_signal: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%lld\n",i,j,qlp_coeff[j],*history,(long long)sumo); |
| 824 | #endif |
| 825 | } |
| 826 | *(data++) = *(r++) + (sum >> lp_quantization); |
| 827 | } |
| 828 | |
| 829 | /* Here's a slower but clearer version: |
| 830 | for(i = 0; i < data_len; i++) { |
| 831 | sum = 0; |
| 832 | for(j = 0; j < order; j++) |
| 833 | sum += qlp_coeff[j] * data[i-j-1]; |
| 834 | data[i] = residual[i] + (sum >> lp_quantization); |
| 835 | } |
| 836 | */ |
| 837 | } |
| 838 | #else /* fully unrolled version for normal use */ |
| 839 | { |
| 840 | int i; |
| 841 | FLAC__int32 sum; |
| 842 | |
| 843 | FLAC__ASSERT(order > 0); |
| 844 | FLAC__ASSERT(order <= 32); |
| 845 | |
| 846 | /* |
| 847 | * We do unique versions up to 12th order since that's the subset limit. |
| 848 | * Also they are roughly ordered to match frequency of occurrence to |
| 849 | * minimize branching. |
| 850 | */ |
| 851 | if(order <= 12) { |
| 852 | if(order > 8) { |
| 853 | if(order > 10) { |
| 854 | if(order == 12) { |
| 855 | for(i = 0; i < (int)data_len; i++) { |
| 856 | sum = 0; |
| 857 | sum += qlp_coeff[11] * data[i-12]; |
| 858 | sum += qlp_coeff[10] * data[i-11]; |
| 859 | sum += qlp_coeff[9] * data[i-10]; |
| 860 | sum += qlp_coeff[8] * data[i-9]; |
| 861 | sum += qlp_coeff[7] * data[i-8]; |
| 862 | sum += qlp_coeff[6] * data[i-7]; |
| 863 | sum += qlp_coeff[5] * data[i-6]; |
| 864 | sum += qlp_coeff[4] * data[i-5]; |
| 865 | sum += qlp_coeff[3] * data[i-4]; |
| 866 | sum += qlp_coeff[2] * data[i-3]; |
| 867 | sum += qlp_coeff[1] * data[i-2]; |
| 868 | sum += qlp_coeff[0] * data[i-1]; |
| 869 | data[i] = residual[i] + (sum >> lp_quantization); |
| 870 | } |
| 871 | } |
| 872 | else { /* order == 11 */ |
| 873 | for(i = 0; i < (int)data_len; i++) { |
| 874 | sum = 0; |
| 875 | sum += qlp_coeff[10] * data[i-11]; |
| 876 | sum += qlp_coeff[9] * data[i-10]; |
| 877 | sum += qlp_coeff[8] * data[i-9]; |
| 878 | sum += qlp_coeff[7] * data[i-8]; |
| 879 | sum += qlp_coeff[6] * data[i-7]; |
| 880 | sum += qlp_coeff[5] * data[i-6]; |
| 881 | sum += qlp_coeff[4] * data[i-5]; |
| 882 | sum += qlp_coeff[3] * data[i-4]; |
| 883 | sum += qlp_coeff[2] * data[i-3]; |
| 884 | sum += qlp_coeff[1] * data[i-2]; |
| 885 | sum += qlp_coeff[0] * data[i-1]; |
| 886 | data[i] = residual[i] + (sum >> lp_quantization); |
| 887 | } |
| 888 | } |
| 889 | } |
| 890 | else { |
| 891 | if(order == 10) { |
| 892 | for(i = 0; i < (int)data_len; i++) { |
| 893 | sum = 0; |
| 894 | sum += qlp_coeff[9] * data[i-10]; |
| 895 | sum += qlp_coeff[8] * data[i-9]; |
| 896 | sum += qlp_coeff[7] * data[i-8]; |
| 897 | sum += qlp_coeff[6] * data[i-7]; |
| 898 | sum += qlp_coeff[5] * data[i-6]; |
| 899 | sum += qlp_coeff[4] * data[i-5]; |
| 900 | sum += qlp_coeff[3] * data[i-4]; |
| 901 | sum += qlp_coeff[2] * data[i-3]; |
| 902 | sum += qlp_coeff[1] * data[i-2]; |
| 903 | sum += qlp_coeff[0] * data[i-1]; |
| 904 | data[i] = residual[i] + (sum >> lp_quantization); |
| 905 | } |
| 906 | } |
| 907 | else { /* order == 9 */ |
| 908 | for(i = 0; i < (int)data_len; i++) { |
| 909 | sum = 0; |
| 910 | sum += qlp_coeff[8] * data[i-9]; |
| 911 | sum += qlp_coeff[7] * data[i-8]; |
| 912 | sum += qlp_coeff[6] * data[i-7]; |
| 913 | sum += qlp_coeff[5] * data[i-6]; |
| 914 | sum += qlp_coeff[4] * data[i-5]; |
| 915 | sum += qlp_coeff[3] * data[i-4]; |
| 916 | sum += qlp_coeff[2] * data[i-3]; |
| 917 | sum += qlp_coeff[1] * data[i-2]; |
| 918 | sum += qlp_coeff[0] * data[i-1]; |
| 919 | data[i] = residual[i] + (sum >> lp_quantization); |
| 920 | } |
| 921 | } |
| 922 | } |
| 923 | } |
| 924 | else if(order > 4) { |
| 925 | if(order > 6) { |
| 926 | if(order == 8) { |
| 927 | for(i = 0; i < (int)data_len; i++) { |
| 928 | sum = 0; |
| 929 | sum += qlp_coeff[7] * data[i-8]; |
| 930 | sum += qlp_coeff[6] * data[i-7]; |
| 931 | sum += qlp_coeff[5] * data[i-6]; |
| 932 | sum += qlp_coeff[4] * data[i-5]; |
| 933 | sum += qlp_coeff[3] * data[i-4]; |
| 934 | sum += qlp_coeff[2] * data[i-3]; |
| 935 | sum += qlp_coeff[1] * data[i-2]; |
| 936 | sum += qlp_coeff[0] * data[i-1]; |
| 937 | data[i] = residual[i] + (sum >> lp_quantization); |
| 938 | } |
| 939 | } |
| 940 | else { /* order == 7 */ |
| 941 | for(i = 0; i < (int)data_len; i++) { |
| 942 | sum = 0; |
| 943 | sum += qlp_coeff[6] * data[i-7]; |
| 944 | sum += qlp_coeff[5] * data[i-6]; |
| 945 | sum += qlp_coeff[4] * data[i-5]; |
| 946 | sum += qlp_coeff[3] * data[i-4]; |
| 947 | sum += qlp_coeff[2] * data[i-3]; |
| 948 | sum += qlp_coeff[1] * data[i-2]; |
| 949 | sum += qlp_coeff[0] * data[i-1]; |
| 950 | data[i] = residual[i] + (sum >> lp_quantization); |
| 951 | } |
| 952 | } |
| 953 | } |
| 954 | else { |
| 955 | if(order == 6) { |
| 956 | for(i = 0; i < (int)data_len; i++) { |
| 957 | sum = 0; |
| 958 | sum += qlp_coeff[5] * data[i-6]; |
| 959 | sum += qlp_coeff[4] * data[i-5]; |
| 960 | sum += qlp_coeff[3] * data[i-4]; |
| 961 | sum += qlp_coeff[2] * data[i-3]; |
| 962 | sum += qlp_coeff[1] * data[i-2]; |
| 963 | sum += qlp_coeff[0] * data[i-1]; |
| 964 | data[i] = residual[i] + (sum >> lp_quantization); |
| 965 | } |
| 966 | } |
| 967 | else { /* order == 5 */ |
| 968 | for(i = 0; i < (int)data_len; i++) { |
| 969 | sum = 0; |
| 970 | sum += qlp_coeff[4] * data[i-5]; |
| 971 | sum += qlp_coeff[3] * data[i-4]; |
| 972 | sum += qlp_coeff[2] * data[i-3]; |
| 973 | sum += qlp_coeff[1] * data[i-2]; |
| 974 | sum += qlp_coeff[0] * data[i-1]; |
| 975 | data[i] = residual[i] + (sum >> lp_quantization); |
| 976 | } |
| 977 | } |
| 978 | } |
| 979 | } |
| 980 | else { |
| 981 | if(order > 2) { |
| 982 | if(order == 4) { |
| 983 | for(i = 0; i < (int)data_len; i++) { |
| 984 | sum = 0; |
| 985 | sum += qlp_coeff[3] * data[i-4]; |
| 986 | sum += qlp_coeff[2] * data[i-3]; |
| 987 | sum += qlp_coeff[1] * data[i-2]; |
| 988 | sum += qlp_coeff[0] * data[i-1]; |
| 989 | data[i] = residual[i] + (sum >> lp_quantization); |
| 990 | } |
| 991 | } |
| 992 | else { /* order == 3 */ |
| 993 | for(i = 0; i < (int)data_len; i++) { |
| 994 | sum = 0; |
| 995 | sum += qlp_coeff[2] * data[i-3]; |
| 996 | sum += qlp_coeff[1] * data[i-2]; |
| 997 | sum += qlp_coeff[0] * data[i-1]; |
| 998 | data[i] = residual[i] + (sum >> lp_quantization); |
| 999 | } |
| 1000 | } |
| 1001 | } |
| 1002 | else { |
| 1003 | if(order == 2) { |
| 1004 | for(i = 0; i < (int)data_len; i++) { |
| 1005 | sum = 0; |
| 1006 | sum += qlp_coeff[1] * data[i-2]; |
| 1007 | sum += qlp_coeff[0] * data[i-1]; |
| 1008 | data[i] = residual[i] + (sum >> lp_quantization); |
| 1009 | } |
| 1010 | } |
| 1011 | else { /* order == 1 */ |
| 1012 | for(i = 0; i < (int)data_len; i++) |
| 1013 | data[i] = residual[i] + ((qlp_coeff[0] * data[i-1]) >> lp_quantization); |
| 1014 | } |
| 1015 | } |
| 1016 | } |
| 1017 | } |
| 1018 | else { /* order > 12 */ |
| 1019 | for(i = 0; i < (int)data_len; i++) { |
| 1020 | sum = 0; |
| 1021 | switch(order) { |
| 1022 | case 32: sum += qlp_coeff[31] * data[i-32]; |
| 1023 | case 31: sum += qlp_coeff[30] * data[i-31]; |
| 1024 | case 30: sum += qlp_coeff[29] * data[i-30]; |
| 1025 | case 29: sum += qlp_coeff[28] * data[i-29]; |
| 1026 | case 28: sum += qlp_coeff[27] * data[i-28]; |
| 1027 | case 27: sum += qlp_coeff[26] * data[i-27]; |
| 1028 | case 26: sum += qlp_coeff[25] * data[i-26]; |
| 1029 | case 25: sum += qlp_coeff[24] * data[i-25]; |
| 1030 | case 24: sum += qlp_coeff[23] * data[i-24]; |
| 1031 | case 23: sum += qlp_coeff[22] * data[i-23]; |
| 1032 | case 22: sum += qlp_coeff[21] * data[i-22]; |
| 1033 | case 21: sum += qlp_coeff[20] * data[i-21]; |
| 1034 | case 20: sum += qlp_coeff[19] * data[i-20]; |
| 1035 | case 19: sum += qlp_coeff[18] * data[i-19]; |
| 1036 | case 18: sum += qlp_coeff[17] * data[i-18]; |
| 1037 | case 17: sum += qlp_coeff[16] * data[i-17]; |
| 1038 | case 16: sum += qlp_coeff[15] * data[i-16]; |
| 1039 | case 15: sum += qlp_coeff[14] * data[i-15]; |
| 1040 | case 14: sum += qlp_coeff[13] * data[i-14]; |
| 1041 | case 13: sum += qlp_coeff[12] * data[i-13]; |
| 1042 | sum += qlp_coeff[11] * data[i-12]; |
| 1043 | sum += qlp_coeff[10] * data[i-11]; |
| 1044 | sum += qlp_coeff[ 9] * data[i-10]; |
| 1045 | sum += qlp_coeff[ 8] * data[i- 9]; |
| 1046 | sum += qlp_coeff[ 7] * data[i- 8]; |
| 1047 | sum += qlp_coeff[ 6] * data[i- 7]; |
| 1048 | sum += qlp_coeff[ 5] * data[i- 6]; |
| 1049 | sum += qlp_coeff[ 4] * data[i- 5]; |
| 1050 | sum += qlp_coeff[ 3] * data[i- 4]; |
| 1051 | sum += qlp_coeff[ 2] * data[i- 3]; |
| 1052 | sum += qlp_coeff[ 1] * data[i- 2]; |
| 1053 | sum += qlp_coeff[ 0] * data[i- 1]; |
| 1054 | } |
| 1055 | data[i] = residual[i] + (sum >> lp_quantization); |
| 1056 | } |
| 1057 | } |
| 1058 | } |
| 1059 | #endif |
| 1060 | |
| 1061 | void FLAC__lpc_restore_signal_wide(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]) |
| 1062 | #if defined(FLAC__OVERFLOW_DETECT) || !defined(FLAC__LPC_UNROLLED_FILTER_LOOPS) |
| 1063 | { |
| 1064 | unsigned i, j; |
| 1065 | FLAC__int64 sum; |
| 1066 | const FLAC__int32 *r = residual, *history; |
| 1067 | |
| 1068 | #ifdef FLAC__OVERFLOW_DETECT_VERBOSE |
| 1069 | fprintf(stderr,"FLAC__lpc_restore_signal_wide: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization); |
| 1070 | for(i=0;i<order;i++) |
| 1071 | fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]); |
| 1072 | fprintf(stderr,"\n"); |
| 1073 | #endif |
| 1074 | FLAC__ASSERT(order > 0); |
| 1075 | |
| 1076 | for(i = 0; i < data_len; i++) { |
| 1077 | sum = 0; |
| 1078 | history = data; |
| 1079 | for(j = 0; j < order; j++) |
| 1080 | sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history)); |
| 1081 | if(FLAC__bitmath_silog2_wide(sum >> lp_quantization) > 32) { |
| 1082 | #ifdef _MSC_VER |
| 1083 | fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, sum=%I64d\n", i, sum >> lp_quantization); |
| 1084 | #else |
| 1085 | fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, sum=%lld\n", i, (long long)(sum >> lp_quantization)); |
| 1086 | #endif |
| 1087 | break; |
| 1088 | } |
| 1089 | if(FLAC__bitmath_silog2_wide((FLAC__int64)(*r) + (sum >> lp_quantization)) > 32) { |
| 1090 | #ifdef _MSC_VER |
| 1091 | fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, residual=%d, sum=%I64d, data=%I64d\n", i, *r, sum >> lp_quantization, (FLAC__int64)(*r) + (sum >> lp_quantization)); |
| 1092 | #else |
| 1093 | fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, residual=%d, sum=%lld, data=%lld\n", i, *r, (long long)(sum >> lp_quantization), (long long)((FLAC__int64)(*r) + (sum >> lp_quantization))); |
| 1094 | #endif |
| 1095 | break; |
| 1096 | } |
| 1097 | *(data++) = *(r++) + (FLAC__int32)(sum >> lp_quantization); |
| 1098 | } |
| 1099 | } |
| 1100 | #else /* fully unrolled version for normal use */ |
| 1101 | { |
| 1102 | int i; |
| 1103 | FLAC__int64 sum; |
| 1104 | |
| 1105 | FLAC__ASSERT(order > 0); |
| 1106 | FLAC__ASSERT(order <= 32); |
| 1107 | |
| 1108 | /* |
| 1109 | * We do unique versions up to 12th order since that's the subset limit. |
| 1110 | * Also they are roughly ordered to match frequency of occurrence to |
| 1111 | * minimize branching. |
| 1112 | */ |
| 1113 | if(order <= 12) { |
| 1114 | if(order > 8) { |
| 1115 | if(order > 10) { |
| 1116 | if(order == 12) { |
| 1117 | for(i = 0; i < (int)data_len; i++) { |
| 1118 | sum = 0; |
| 1119 | sum += qlp_coeff[11] * (FLAC__int64)data[i-12]; |
| 1120 | sum += qlp_coeff[10] * (FLAC__int64)data[i-11]; |
| 1121 | sum += qlp_coeff[9] * (FLAC__int64)data[i-10]; |
| 1122 | sum += qlp_coeff[8] * (FLAC__int64)data[i-9]; |
| 1123 | sum += qlp_coeff[7] * (FLAC__int64)data[i-8]; |
| 1124 | sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; |
| 1125 | sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; |
| 1126 | sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; |
| 1127 | sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; |
| 1128 | sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; |
| 1129 | sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; |
| 1130 | sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; |
| 1131 | data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); |
| 1132 | } |
| 1133 | } |
| 1134 | else { /* order == 11 */ |
| 1135 | for(i = 0; i < (int)data_len; i++) { |
| 1136 | sum = 0; |
| 1137 | sum += qlp_coeff[10] * (FLAC__int64)data[i-11]; |
| 1138 | sum += qlp_coeff[9] * (FLAC__int64)data[i-10]; |
| 1139 | sum += qlp_coeff[8] * (FLAC__int64)data[i-9]; |
| 1140 | sum += qlp_coeff[7] * (FLAC__int64)data[i-8]; |
| 1141 | sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; |
| 1142 | sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; |
| 1143 | sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; |
| 1144 | sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; |
| 1145 | sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; |
| 1146 | sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; |
| 1147 | sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; |
| 1148 | data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); |
| 1149 | } |
| 1150 | } |
| 1151 | } |
| 1152 | else { |
| 1153 | if(order == 10) { |
| 1154 | for(i = 0; i < (int)data_len; i++) { |
| 1155 | sum = 0; |
| 1156 | sum += qlp_coeff[9] * (FLAC__int64)data[i-10]; |
| 1157 | sum += qlp_coeff[8] * (FLAC__int64)data[i-9]; |
| 1158 | sum += qlp_coeff[7] * (FLAC__int64)data[i-8]; |
| 1159 | sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; |
| 1160 | sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; |
| 1161 | sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; |
| 1162 | sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; |
| 1163 | sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; |
| 1164 | sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; |
| 1165 | sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; |
| 1166 | data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); |
| 1167 | } |
| 1168 | } |
| 1169 | else { /* order == 9 */ |
| 1170 | for(i = 0; i < (int)data_len; i++) { |
| 1171 | sum = 0; |
| 1172 | sum += qlp_coeff[8] * (FLAC__int64)data[i-9]; |
| 1173 | sum += qlp_coeff[7] * (FLAC__int64)data[i-8]; |
| 1174 | sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; |
| 1175 | sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; |
| 1176 | sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; |
| 1177 | sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; |
| 1178 | sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; |
| 1179 | sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; |
| 1180 | sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; |
| 1181 | data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); |
| 1182 | } |
| 1183 | } |
| 1184 | } |
| 1185 | } |
| 1186 | else if(order > 4) { |
| 1187 | if(order > 6) { |
| 1188 | if(order == 8) { |
| 1189 | for(i = 0; i < (int)data_len; i++) { |
| 1190 | sum = 0; |
| 1191 | sum += qlp_coeff[7] * (FLAC__int64)data[i-8]; |
| 1192 | sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; |
| 1193 | sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; |
| 1194 | sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; |
| 1195 | sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; |
| 1196 | sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; |
| 1197 | sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; |
| 1198 | sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; |
| 1199 | data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); |
| 1200 | } |
| 1201 | } |
| 1202 | else { /* order == 7 */ |
| 1203 | for(i = 0; i < (int)data_len; i++) { |
| 1204 | sum = 0; |
| 1205 | sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; |
| 1206 | sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; |
| 1207 | sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; |
| 1208 | sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; |
| 1209 | sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; |
| 1210 | sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; |
| 1211 | sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; |
| 1212 | data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); |
| 1213 | } |
| 1214 | } |
| 1215 | } |
| 1216 | else { |
| 1217 | if(order == 6) { |
| 1218 | for(i = 0; i < (int)data_len; i++) { |
| 1219 | sum = 0; |
| 1220 | sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; |
| 1221 | sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; |
| 1222 | sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; |
| 1223 | sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; |
| 1224 | sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; |
| 1225 | sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; |
| 1226 | data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); |
| 1227 | } |
| 1228 | } |
| 1229 | else { /* order == 5 */ |
| 1230 | for(i = 0; i < (int)data_len; i++) { |
| 1231 | sum = 0; |
| 1232 | sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; |
| 1233 | sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; |
| 1234 | sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; |
| 1235 | sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; |
| 1236 | sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; |
| 1237 | data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); |
| 1238 | } |
| 1239 | } |
| 1240 | } |
| 1241 | } |
| 1242 | else { |
| 1243 | if(order > 2) { |
| 1244 | if(order == 4) { |
| 1245 | for(i = 0; i < (int)data_len; i++) { |
| 1246 | sum = 0; |
| 1247 | sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; |
| 1248 | sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; |
| 1249 | sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; |
| 1250 | sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; |
| 1251 | data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); |
| 1252 | } |
| 1253 | } |
| 1254 | else { /* order == 3 */ |
| 1255 | for(i = 0; i < (int)data_len; i++) { |
| 1256 | sum = 0; |
| 1257 | sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; |
| 1258 | sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; |
| 1259 | sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; |
| 1260 | data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); |
| 1261 | } |
| 1262 | } |
| 1263 | } |
| 1264 | else { |
| 1265 | if(order == 2) { |
| 1266 | for(i = 0; i < (int)data_len; i++) { |
| 1267 | sum = 0; |
| 1268 | sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; |
| 1269 | sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; |
| 1270 | data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); |
| 1271 | } |
| 1272 | } |
| 1273 | else { /* order == 1 */ |
| 1274 | for(i = 0; i < (int)data_len; i++) |
| 1275 | data[i] = residual[i] + (FLAC__int32)((qlp_coeff[0] * (FLAC__int64)data[i-1]) >> lp_quantization); |
| 1276 | } |
| 1277 | } |
| 1278 | } |
| 1279 | } |
| 1280 | else { /* order > 12 */ |
| 1281 | for(i = 0; i < (int)data_len; i++) { |
| 1282 | sum = 0; |
| 1283 | switch(order) { |
| 1284 | case 32: sum += qlp_coeff[31] * (FLAC__int64)data[i-32]; |
| 1285 | case 31: sum += qlp_coeff[30] * (FLAC__int64)data[i-31]; |
| 1286 | case 30: sum += qlp_coeff[29] * (FLAC__int64)data[i-30]; |
| 1287 | case 29: sum += qlp_coeff[28] * (FLAC__int64)data[i-29]; |
| 1288 | case 28: sum += qlp_coeff[27] * (FLAC__int64)data[i-28]; |
| 1289 | case 27: sum += qlp_coeff[26] * (FLAC__int64)data[i-27]; |
| 1290 | case 26: sum += qlp_coeff[25] * (FLAC__int64)data[i-26]; |
| 1291 | case 25: sum += qlp_coeff[24] * (FLAC__int64)data[i-25]; |
| 1292 | case 24: sum += qlp_coeff[23] * (FLAC__int64)data[i-24]; |
| 1293 | case 23: sum += qlp_coeff[22] * (FLAC__int64)data[i-23]; |
| 1294 | case 22: sum += qlp_coeff[21] * (FLAC__int64)data[i-22]; |
| 1295 | case 21: sum += qlp_coeff[20] * (FLAC__int64)data[i-21]; |
| 1296 | case 20: sum += qlp_coeff[19] * (FLAC__int64)data[i-20]; |
| 1297 | case 19: sum += qlp_coeff[18] * (FLAC__int64)data[i-19]; |
| 1298 | case 18: sum += qlp_coeff[17] * (FLAC__int64)data[i-18]; |
| 1299 | case 17: sum += qlp_coeff[16] * (FLAC__int64)data[i-17]; |
| 1300 | case 16: sum += qlp_coeff[15] * (FLAC__int64)data[i-16]; |
| 1301 | case 15: sum += qlp_coeff[14] * (FLAC__int64)data[i-15]; |
| 1302 | case 14: sum += qlp_coeff[13] * (FLAC__int64)data[i-14]; |
| 1303 | case 13: sum += qlp_coeff[12] * (FLAC__int64)data[i-13]; |
| 1304 | sum += qlp_coeff[11] * (FLAC__int64)data[i-12]; |
| 1305 | sum += qlp_coeff[10] * (FLAC__int64)data[i-11]; |
| 1306 | sum += qlp_coeff[ 9] * (FLAC__int64)data[i-10]; |
| 1307 | sum += qlp_coeff[ 8] * (FLAC__int64)data[i- 9]; |
| 1308 | sum += qlp_coeff[ 7] * (FLAC__int64)data[i- 8]; |
| 1309 | sum += qlp_coeff[ 6] * (FLAC__int64)data[i- 7]; |
| 1310 | sum += qlp_coeff[ 5] * (FLAC__int64)data[i- 6]; |
| 1311 | sum += qlp_coeff[ 4] * (FLAC__int64)data[i- 5]; |
| 1312 | sum += qlp_coeff[ 3] * (FLAC__int64)data[i- 4]; |
| 1313 | sum += qlp_coeff[ 2] * (FLAC__int64)data[i- 3]; |
| 1314 | sum += qlp_coeff[ 1] * (FLAC__int64)data[i- 2]; |
| 1315 | sum += qlp_coeff[ 0] * (FLAC__int64)data[i- 1]; |
| 1316 | } |
| 1317 | data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); |
| 1318 | } |
| 1319 | } |
| 1320 | } |
| 1321 | #endif |
| 1322 | |
| 1323 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 1324 | |
| 1325 | FLAC__double FLAC__lpc_compute_expected_bits_per_residual_sample(FLAC__double lpc_error, unsigned total_samples) |
| 1326 | { |
| 1327 | FLAC__double error_scale; |
| 1328 | |
| 1329 | FLAC__ASSERT(total_samples > 0); |
| 1330 | |
| 1331 | error_scale = 0.5 * M_LN2 * M_LN2 / (FLAC__double)total_samples; |
| 1332 | |
| 1333 | return FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error, error_scale); |
| 1334 | } |
| 1335 | |
| 1336 | FLAC__double FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(FLAC__double lpc_error, FLAC__double error_scale) |
| 1337 | { |
| 1338 | if(lpc_error > 0.0) { |
| 1339 | FLAC__double bps = (FLAC__double)0.5 * log(error_scale * lpc_error) / M_LN2; |
| 1340 | if(bps >= 0.0) |
| 1341 | return bps; |
| 1342 | else |
| 1343 | return 0.0; |
| 1344 | } |
| 1345 | else if(lpc_error < 0.0) { /* error should not be negative but can happen due to inadequate floating-point resolution */ |
| 1346 | return 1e32; |
| 1347 | } |
| 1348 | else { |
| 1349 | return 0.0; |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | unsigned FLAC__lpc_compute_best_order(const FLAC__double lpc_error[], unsigned max_order, unsigned total_samples, unsigned overhead_bits_per_order) |
| 1354 | { |
| 1355 | unsigned order, index, best_index; /* 'index' the index into lpc_error; index==order-1 since lpc_error[0] is for order==1, lpc_error[1] is for order==2, etc */ |
| 1356 | FLAC__double bits, best_bits, error_scale; |
| 1357 | |
| 1358 | FLAC__ASSERT(max_order > 0); |
| 1359 | FLAC__ASSERT(total_samples > 0); |
| 1360 | |
| 1361 | error_scale = 0.5 * M_LN2 * M_LN2 / (FLAC__double)total_samples; |
| 1362 | |
| 1363 | best_index = 0; |
| 1364 | best_bits = (unsigned)(-1); |
| 1365 | |
| 1366 | for(index = 0, order = 1; index < max_order; index++, order++) { |
| 1367 | bits = FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error[index], error_scale) * (FLAC__double)(total_samples - order) + (FLAC__double)(order * overhead_bits_per_order); |
| 1368 | if(bits < best_bits) { |
| 1369 | best_index = index; |
| 1370 | best_bits = bits; |
| 1371 | } |
| 1372 | } |
| 1373 | |
| 1374 | return best_index+1; /* +1 since index of lpc_error[] is order-1 */ |
| 1375 | } |
| 1376 | |
| 1377 | #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */ |
src/lib/libflac/libflac/stream_decoder.c
| r0 | r14135 | |
| 1 | /* libFLAC - Free Lossless Audio Codec library |
| 2 | * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * |
| 8 | * - Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * - Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * - Neither the name of the Xiph.org Foundation nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived from |
| 17 | * this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 26 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 27 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #if HAVE_CONFIG_H |
| 33 | # include <config.h> |
| 34 | #endif |
| 35 | |
| 36 | #include <stdio.h> |
| 37 | #include <stdlib.h> /* for malloc() */ |
| 38 | #include <string.h> /* for memset/memcpy() */ |
| 39 | #include <sys/stat.h> /* for stat() */ |
| 40 | #include <sys/types.h> /* for off_t */ |
| 41 | #if defined _MSC_VER || defined __BORLANDC__ || defined __MINGW32__ |
| 42 | #if _MSC_VER <= 1600 || defined __BORLANDC__ /* @@@ [2G limit] */ |
| 43 | #define fseeko fseek |
| 44 | #define ftello ftell |
| 45 | #endif |
| 46 | #endif |
| 47 | #include "flac/assert.h" |
| 48 | #include "share/alloc.h" |
| 49 | #include "protected/stream_decoder.h" |
| 50 | #include "private/bitreader.h" |
| 51 | #include "private/bitmath.h" |
| 52 | #include "private/cpu.h" |
| 53 | #include "private/crc.h" |
| 54 | #include "private/fixed.h" |
| 55 | #include "private/format.h" |
| 56 | #include "private/lpc.h" |
| 57 | #include "private/md5.h" |
| 58 | #include "private/memory.h" |
| 59 | |
| 60 | #ifdef max |
| 61 | #undef max |
| 62 | #endif |
| 63 | #define max(a,b) ((a)>(b)?(a):(b)) |
| 64 | |
| 65 | /* adjust for compilers that can't understand using LLU suffix for uint64_t literals */ |
| 66 | #ifdef _MSC_VER |
| 67 | #define FLAC__U64L(x) x |
| 68 | #else |
| 69 | #define FLAC__U64L(x) x##LLU |
| 70 | #endif |
| 71 | |
| 72 | |
| 73 | /* technically this should be in an "export.c" but this is convenient enough */ |
| 74 | FLAC_API int FLAC_API_SUPPORTS_OGG_FLAC = |
| 75 | #if FLAC__HAS_OGG |
| 76 | 1 |
| 77 | #else |
| 78 | 0 |
| 79 | #endif |
| 80 | ; |
| 81 | |
| 82 | |
| 83 | /*********************************************************************** |
| 84 | * |
| 85 | * Private static data |
| 86 | * |
| 87 | ***********************************************************************/ |
| 88 | |
| 89 | static FLAC__byte ID3V2_TAG_[3] = { 'I', 'D', '3' }; |
| 90 | |
| 91 | /*********************************************************************** |
| 92 | * |
| 93 | * Private class method prototypes |
| 94 | * |
| 95 | ***********************************************************************/ |
| 96 | |
| 97 | static void set_defaults_(FLAC__StreamDecoder *decoder); |
| 98 | static FILE *get_binary_stdin_(void); |
| 99 | static FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels); |
| 100 | static FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id); |
| 101 | static FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder); |
| 102 | static FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder); |
| 103 | static FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length); |
| 104 | static FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length); |
| 105 | static FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj); |
| 106 | static FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj); |
| 107 | static FLAC__bool read_metadata_picture_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_Picture *obj); |
| 108 | static FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder); |
| 109 | static FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder); |
| 110 | static FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode); |
| 111 | static FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder); |
| 112 | static FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode); |
| 113 | static FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode); |
| 114 | static FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode); |
| 115 | static FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode); |
| 116 | static FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode); |
| 117 | static FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual, FLAC__bool is_extended); |
| 118 | static FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder); |
| 119 | static FLAC__bool read_callback_(FLAC__byte buffer[], size_t *bytes, void *client_data); |
| 120 | #if FLAC__HAS_OGG |
| 121 | static FLAC__StreamDecoderReadStatus read_callback_ogg_aspect_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes); |
| 122 | static FLAC__OggDecoderAspectReadStatus read_callback_proxy_(const void *void_decoder, FLAC__byte buffer[], size_t *bytes, void *client_data); |
| 123 | #endif |
| 124 | static FLAC__StreamDecoderWriteStatus write_audio_frame_to_client_(FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[]); |
| 125 | static void send_error_to_client_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status); |
| 126 | static FLAC__bool seek_to_absolute_sample_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample); |
| 127 | #if FLAC__HAS_OGG |
| 128 | static FLAC__bool seek_to_absolute_sample_ogg_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample); |
| 129 | #endif |
| 130 | static FLAC__StreamDecoderReadStatus file_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data); |
| 131 | static FLAC__StreamDecoderSeekStatus file_seek_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data); |
| 132 | static FLAC__StreamDecoderTellStatus file_tell_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data); |
| 133 | static FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data); |
| 134 | static FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data); |
| 135 | |
| 136 | /*********************************************************************** |
| 137 | * |
| 138 | * Private class data |
| 139 | * |
| 140 | ***********************************************************************/ |
| 141 | |
| 142 | typedef struct FLAC__StreamDecoderPrivate { |
| 143 | #if FLAC__HAS_OGG |
| 144 | FLAC__bool is_ogg; |
| 145 | #endif |
| 146 | FLAC__StreamDecoderReadCallback read_callback; |
| 147 | FLAC__StreamDecoderSeekCallback seek_callback; |
| 148 | FLAC__StreamDecoderTellCallback tell_callback; |
| 149 | FLAC__StreamDecoderLengthCallback length_callback; |
| 150 | FLAC__StreamDecoderEofCallback eof_callback; |
| 151 | FLAC__StreamDecoderWriteCallback write_callback; |
| 152 | FLAC__StreamDecoderMetadataCallback metadata_callback; |
| 153 | FLAC__StreamDecoderErrorCallback error_callback; |
| 154 | /* generic 32-bit datapath: */ |
| 155 | void (*local_lpc_restore_signal)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]); |
| 156 | /* generic 64-bit datapath: */ |
| 157 | void (*local_lpc_restore_signal_64bit)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]); |
| 158 | /* for use when the signal is <= 16 bits-per-sample, or <= 15 bits-per-sample on a side channel (which requires 1 extra bit): */ |
| 159 | void (*local_lpc_restore_signal_16bit)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]); |
| 160 | /* for use when the signal is <= 16 bits-per-sample, or <= 15 bits-per-sample on a side channel (which requires 1 extra bit), AND order <= 8: */ |
| 161 | void (*local_lpc_restore_signal_16bit_order8)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]); |
| 162 | FLAC__bool (*local_bitreader_read_rice_signed_block)(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter); |
| 163 | void *client_data; |
| 164 | FILE *file; /* only used if FLAC__stream_decoder_init_file()/FLAC__stream_decoder_init_file() called, else NULL */ |
| 165 | FLAC__BitReader *input; |
| 166 | FLAC__int32 *output[FLAC__MAX_CHANNELS]; |
| 167 | FLAC__int32 *residual[FLAC__MAX_CHANNELS]; /* WATCHOUT: these are the aligned pointers; the real pointers that should be free()'d are residual_unaligned[] below */ |
| 168 | FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents[FLAC__MAX_CHANNELS]; |
| 169 | unsigned output_capacity, output_channels; |
| 170 | FLAC__uint32 fixed_block_size, next_fixed_block_size; |
| 171 | FLAC__uint64 samples_decoded; |
| 172 | FLAC__bool has_stream_info, has_seek_table; |
| 173 | FLAC__StreamMetadata stream_info; |
| 174 | FLAC__StreamMetadata seek_table; |
| 175 | FLAC__bool metadata_filter[128]; /* MAGIC number 128 == total number of metadata block types == 1 << 7 */ |
| 176 | FLAC__byte *metadata_filter_ids; |
| 177 | size_t metadata_filter_ids_count, metadata_filter_ids_capacity; /* units for both are IDs, not bytes */ |
| 178 | FLAC__Frame frame; |
| 179 | FLAC__bool cached; /* true if there is a byte in lookahead */ |
| 180 | FLAC__CPUInfo cpuinfo; |
| 181 | FLAC__byte header_warmup[2]; /* contains the sync code and reserved bits */ |
| 182 | FLAC__byte lookahead; /* temp storage when we need to look ahead one byte in the stream */ |
| 183 | /* unaligned (original) pointers to allocated data */ |
| 184 | FLAC__int32 *residual_unaligned[FLAC__MAX_CHANNELS]; |
| 185 | FLAC__bool do_md5_checking; /* initially gets protected_->md5_checking but is turned off after a seek or if the metadata has a zero MD5 */ |
| 186 | FLAC__bool internal_reset_hack; /* used only during init() so we can call reset to set up the decoder without rewinding the input */ |
| 187 | FLAC__bool is_seeking; |
| 188 | FLAC__MD5Context md5context; |
| 189 | FLAC__byte computed_md5sum[16]; /* this is the sum we computed from the decoded data */ |
| 190 | /* (the rest of these are only used for seeking) */ |
| 191 | FLAC__Frame last_frame; /* holds the info of the last frame we seeked to */ |
| 192 | FLAC__uint64 first_frame_offset; /* hint to the seek routine of where in the stream the first audio frame starts */ |
| 193 | FLAC__uint64 target_sample; |
| 194 | unsigned unparseable_frame_count; /* used to tell whether we're decoding a future version of FLAC or just got a bad sync */ |
| 195 | #if FLAC__HAS_OGG |
| 196 | FLAC__bool got_a_frame; /* hack needed in Ogg FLAC seek routine to check when process_single() actually writes a frame */ |
| 197 | #endif |
| 198 | } FLAC__StreamDecoderPrivate; |
| 199 | |
| 200 | /*********************************************************************** |
| 201 | * |
| 202 | * Public static class data |
| 203 | * |
| 204 | ***********************************************************************/ |
| 205 | |
| 206 | FLAC_API const char * const FLAC__StreamDecoderStateString[] = { |
| 207 | "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA", |
| 208 | "FLAC__STREAM_DECODER_READ_METADATA", |
| 209 | "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC", |
| 210 | "FLAC__STREAM_DECODER_READ_FRAME", |
| 211 | "FLAC__STREAM_DECODER_END_OF_STREAM", |
| 212 | "FLAC__STREAM_DECODER_OGG_ERROR", |
| 213 | "FLAC__STREAM_DECODER_SEEK_ERROR", |
| 214 | "FLAC__STREAM_DECODER_ABORTED", |
| 215 | "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR", |
| 216 | "FLAC__STREAM_DECODER_UNINITIALIZED" |
| 217 | }; |
| 218 | |
| 219 | FLAC_API const char * const FLAC__StreamDecoderInitStatusString[] = { |
| 220 | "FLAC__STREAM_DECODER_INIT_STATUS_OK", |
| 221 | "FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER", |
| 222 | "FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS", |
| 223 | "FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR", |
| 224 | "FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE", |
| 225 | "FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED" |
| 226 | }; |
| 227 | |
| 228 | FLAC_API const char * const FLAC__StreamDecoderReadStatusString[] = { |
| 229 | "FLAC__STREAM_DECODER_READ_STATUS_CONTINUE", |
| 230 | "FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM", |
| 231 | "FLAC__STREAM_DECODER_READ_STATUS_ABORT" |
| 232 | }; |
| 233 | |
| 234 | FLAC_API const char * const FLAC__StreamDecoderSeekStatusString[] = { |
| 235 | "FLAC__STREAM_DECODER_SEEK_STATUS_OK", |
| 236 | "FLAC__STREAM_DECODER_SEEK_STATUS_ERROR", |
| 237 | "FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED" |
| 238 | }; |
| 239 | |
| 240 | FLAC_API const char * const FLAC__StreamDecoderTellStatusString[] = { |
| 241 | "FLAC__STREAM_DECODER_TELL_STATUS_OK", |
| 242 | "FLAC__STREAM_DECODER_TELL_STATUS_ERROR", |
| 243 | "FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED" |
| 244 | }; |
| 245 | |
| 246 | FLAC_API const char * const FLAC__StreamDecoderLengthStatusString[] = { |
| 247 | "FLAC__STREAM_DECODER_LENGTH_STATUS_OK", |
| 248 | "FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR", |
| 249 | "FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED" |
| 250 | }; |
| 251 | |
| 252 | FLAC_API const char * const FLAC__StreamDecoderWriteStatusString[] = { |
| 253 | "FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE", |
| 254 | "FLAC__STREAM_DECODER_WRITE_STATUS_ABORT" |
| 255 | }; |
| 256 | |
| 257 | FLAC_API const char * const FLAC__StreamDecoderErrorStatusString[] = { |
| 258 | "FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC", |
| 259 | "FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER", |
| 260 | "FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH", |
| 261 | "FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM" |
| 262 | }; |
| 263 | |
| 264 | /*********************************************************************** |
| 265 | * |
| 266 | * Class constructor/destructor |
| 267 | * |
| 268 | ***********************************************************************/ |
| 269 | FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new(void) |
| 270 | { |
| 271 | FLAC__StreamDecoder *decoder; |
| 272 | unsigned i; |
| 273 | |
| 274 | FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */ |
| 275 | |
| 276 | decoder = (FLAC__StreamDecoder*)calloc(1, sizeof(FLAC__StreamDecoder)); |
| 277 | if(decoder == 0) { |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | decoder->protected_ = (FLAC__StreamDecoderProtected*)calloc(1, sizeof(FLAC__StreamDecoderProtected)); |
| 282 | if(decoder->protected_ == 0) { |
| 283 | free(decoder); |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | decoder->private_ = (FLAC__StreamDecoderPrivate*)calloc(1, sizeof(FLAC__StreamDecoderPrivate)); |
| 288 | if(decoder->private_ == 0) { |
| 289 | free(decoder->protected_); |
| 290 | free(decoder); |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | decoder->private_->input = FLAC__bitreader_new(); |
| 295 | if(decoder->private_->input == 0) { |
| 296 | free(decoder->private_); |
| 297 | free(decoder->protected_); |
| 298 | free(decoder); |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | decoder->private_->metadata_filter_ids_capacity = 16; |
| 303 | if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) { |
| 304 | FLAC__bitreader_delete(decoder->private_->input); |
| 305 | free(decoder->private_); |
| 306 | free(decoder->protected_); |
| 307 | free(decoder); |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | for(i = 0; i < FLAC__MAX_CHANNELS; i++) { |
| 312 | decoder->private_->output[i] = 0; |
| 313 | decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0; |
| 314 | } |
| 315 | |
| 316 | decoder->private_->output_capacity = 0; |
| 317 | decoder->private_->output_channels = 0; |
| 318 | decoder->private_->has_seek_table = false; |
| 319 | |
| 320 | for(i = 0; i < FLAC__MAX_CHANNELS; i++) |
| 321 | FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&decoder->private_->partitioned_rice_contents[i]); |
| 322 | |
| 323 | decoder->private_->file = 0; |
| 324 | |
| 325 | set_defaults_(decoder); |
| 326 | |
| 327 | decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED; |
| 328 | |
| 329 | return decoder; |
| 330 | } |
| 331 | |
| 332 | FLAC_API void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder) |
| 333 | { |
| 334 | unsigned i; |
| 335 | |
| 336 | FLAC__ASSERT(0 != decoder); |
| 337 | FLAC__ASSERT(0 != decoder->protected_); |
| 338 | FLAC__ASSERT(0 != decoder->private_); |
| 339 | FLAC__ASSERT(0 != decoder->private_->input); |
| 340 | |
| 341 | (void)FLAC__stream_decoder_finish(decoder); |
| 342 | |
| 343 | if(0 != decoder->private_->metadata_filter_ids) |
| 344 | free(decoder->private_->metadata_filter_ids); |
| 345 | |
| 346 | FLAC__bitreader_delete(decoder->private_->input); |
| 347 | |
| 348 | for(i = 0; i < FLAC__MAX_CHANNELS; i++) |
| 349 | FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&decoder->private_->partitioned_rice_contents[i]); |
| 350 | |
| 351 | free(decoder->private_); |
| 352 | free(decoder->protected_); |
| 353 | free(decoder); |
| 354 | } |
| 355 | |
| 356 | /*********************************************************************** |
| 357 | * |
| 358 | * Public class methods |
| 359 | * |
| 360 | ***********************************************************************/ |
| 361 | |
| 362 | static FLAC__StreamDecoderInitStatus init_stream_internal_( |
| 363 | FLAC__StreamDecoder *decoder, |
| 364 | FLAC__StreamDecoderReadCallback read_callback, |
| 365 | FLAC__StreamDecoderSeekCallback seek_callback, |
| 366 | FLAC__StreamDecoderTellCallback tell_callback, |
| 367 | FLAC__StreamDecoderLengthCallback length_callback, |
| 368 | FLAC__StreamDecoderEofCallback eof_callback, |
| 369 | FLAC__StreamDecoderWriteCallback write_callback, |
| 370 | FLAC__StreamDecoderMetadataCallback metadata_callback, |
| 371 | FLAC__StreamDecoderErrorCallback error_callback, |
| 372 | void *client_data, |
| 373 | FLAC__bool is_ogg |
| 374 | ) |
| 375 | { |
| 376 | FLAC__ASSERT(0 != decoder); |
| 377 | |
| 378 | if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
| 379 | return FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED; |
| 380 | |
| 381 | #if !FLAC__HAS_OGG |
| 382 | if(is_ogg) |
| 383 | return FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER; |
| 384 | #endif |
| 385 | |
| 386 | if( |
| 387 | 0 == read_callback || |
| 388 | 0 == write_callback || |
| 389 | 0 == error_callback || |
| 390 | (seek_callback && (0 == tell_callback || 0 == length_callback || 0 == eof_callback)) |
| 391 | ) |
| 392 | return FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS; |
| 393 | |
| 394 | #if FLAC__HAS_OGG |
| 395 | decoder->private_->is_ogg = is_ogg; |
| 396 | if(is_ogg && !FLAC__ogg_decoder_aspect_init(&decoder->protected_->ogg_decoder_aspect)) |
| 397 | return decoder->protected_->state = FLAC__STREAM_DECODER_OGG_ERROR; |
| 398 | #endif |
| 399 | |
| 400 | /* |
| 401 | * get the CPU info and set the function pointers |
| 402 | */ |
| 403 | FLAC__cpu_info(&decoder->private_->cpuinfo); |
| 404 | /* first default to the non-asm routines */ |
| 405 | decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal; |
| 406 | decoder->private_->local_lpc_restore_signal_64bit = FLAC__lpc_restore_signal_wide; |
| 407 | decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal; |
| 408 | decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal; |
| 409 | decoder->private_->local_bitreader_read_rice_signed_block = FLAC__bitreader_read_rice_signed_block; |
| 410 | /* now override with asm where appropriate */ |
| 411 | #ifndef FLAC__NO_ASM |
| 412 | if(decoder->private_->cpuinfo.use_asm) { |
| 413 | #ifdef FLAC__CPU_IA32 |
| 414 | FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32); |
| 415 | #ifdef FLAC__HAS_NASM |
| 416 | #if 1 /*@@@@@@ OPT: not clearly faster, needs more testing */ |
| 417 | if(decoder->private_->cpuinfo.data.ia32.bswap) |
| 418 | decoder->private_->local_bitreader_read_rice_signed_block = FLAC__bitreader_read_rice_signed_block_asm_ia32_bswap; |
| 419 | #endif |
| 420 | if(decoder->private_->cpuinfo.data.ia32.mmx) { |
| 421 | decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32; |
| 422 | decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32_mmx; |
| 423 | decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ia32_mmx; |
| 424 | } |
| 425 | else { |
| 426 | decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32; |
| 427 | decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32; |
| 428 | decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ia32; |
| 429 | } |
| 430 | #endif |
| 431 | #elif defined FLAC__CPU_PPC |
| 432 | FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_PPC); |
| 433 | if(decoder->private_->cpuinfo.data.ppc.altivec) { |
| 434 | decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ppc_altivec_16; |
| 435 | decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ppc_altivec_16_order8; |
| 436 | } |
| 437 | #endif |
| 438 | } |
| 439 | #endif |
| 440 | |
| 441 | /* from here on, errors are fatal */ |
| 442 | |
| 443 | if(!FLAC__bitreader_init(decoder->private_->input, decoder->private_->cpuinfo, read_callback_, decoder)) { |
| 444 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 445 | return FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR; |
| 446 | } |
| 447 | |
| 448 | decoder->private_->read_callback = read_callback; |
| 449 | decoder->private_->seek_callback = seek_callback; |
| 450 | decoder->private_->tell_callback = tell_callback; |
| 451 | decoder->private_->length_callback = length_callback; |
| 452 | decoder->private_->eof_callback = eof_callback; |
| 453 | decoder->private_->write_callback = write_callback; |
| 454 | decoder->private_->metadata_callback = metadata_callback; |
| 455 | decoder->private_->error_callback = error_callback; |
| 456 | decoder->private_->client_data = client_data; |
| 457 | decoder->private_->fixed_block_size = decoder->private_->next_fixed_block_size = 0; |
| 458 | decoder->private_->samples_decoded = 0; |
| 459 | decoder->private_->has_stream_info = false; |
| 460 | decoder->private_->cached = false; |
| 461 | |
| 462 | decoder->private_->do_md5_checking = decoder->protected_->md5_checking; |
| 463 | decoder->private_->is_seeking = false; |
| 464 | |
| 465 | decoder->private_->internal_reset_hack = true; /* so the following reset does not try to rewind the input */ |
| 466 | if(!FLAC__stream_decoder_reset(decoder)) { |
| 467 | /* above call sets the state for us */ |
| 468 | return FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR; |
| 469 | } |
| 470 | |
| 471 | return FLAC__STREAM_DECODER_INIT_STATUS_OK; |
| 472 | } |
| 473 | |
| 474 | FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_stream( |
| 475 | FLAC__StreamDecoder *decoder, |
| 476 | FLAC__StreamDecoderReadCallback read_callback, |
| 477 | FLAC__StreamDecoderSeekCallback seek_callback, |
| 478 | FLAC__StreamDecoderTellCallback tell_callback, |
| 479 | FLAC__StreamDecoderLengthCallback length_callback, |
| 480 | FLAC__StreamDecoderEofCallback eof_callback, |
| 481 | FLAC__StreamDecoderWriteCallback write_callback, |
| 482 | FLAC__StreamDecoderMetadataCallback metadata_callback, |
| 483 | FLAC__StreamDecoderErrorCallback error_callback, |
| 484 | void *client_data |
| 485 | ) |
| 486 | { |
| 487 | return init_stream_internal_( |
| 488 | decoder, |
| 489 | read_callback, |
| 490 | seek_callback, |
| 491 | tell_callback, |
| 492 | length_callback, |
| 493 | eof_callback, |
| 494 | write_callback, |
| 495 | metadata_callback, |
| 496 | error_callback, |
| 497 | client_data, |
| 498 | /*is_ogg=*/false |
| 499 | ); |
| 500 | } |
| 501 | |
| 502 | FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_stream( |
| 503 | FLAC__StreamDecoder *decoder, |
| 504 | FLAC__StreamDecoderReadCallback read_callback, |
| 505 | FLAC__StreamDecoderSeekCallback seek_callback, |
| 506 | FLAC__StreamDecoderTellCallback tell_callback, |
| 507 | FLAC__StreamDecoderLengthCallback length_callback, |
| 508 | FLAC__StreamDecoderEofCallback eof_callback, |
| 509 | FLAC__StreamDecoderWriteCallback write_callback, |
| 510 | FLAC__StreamDecoderMetadataCallback metadata_callback, |
| 511 | FLAC__StreamDecoderErrorCallback error_callback, |
| 512 | void *client_data |
| 513 | ) |
| 514 | { |
| 515 | return init_stream_internal_( |
| 516 | decoder, |
| 517 | read_callback, |
| 518 | seek_callback, |
| 519 | tell_callback, |
| 520 | length_callback, |
| 521 | eof_callback, |
| 522 | write_callback, |
| 523 | metadata_callback, |
| 524 | error_callback, |
| 525 | client_data, |
| 526 | /*is_ogg=*/true |
| 527 | ); |
| 528 | } |
| 529 | |
| 530 | static FLAC__StreamDecoderInitStatus init_FILE_internal_( |
| 531 | FLAC__StreamDecoder *decoder, |
| 532 | FILE *file, |
| 533 | FLAC__StreamDecoderWriteCallback write_callback, |
| 534 | FLAC__StreamDecoderMetadataCallback metadata_callback, |
| 535 | FLAC__StreamDecoderErrorCallback error_callback, |
| 536 | void *client_data, |
| 537 | FLAC__bool is_ogg |
| 538 | ) |
| 539 | { |
| 540 | FLAC__ASSERT(0 != decoder); |
| 541 | FLAC__ASSERT(0 != file); |
| 542 | |
| 543 | if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
| 544 | return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED; |
| 545 | |
| 546 | if(0 == write_callback || 0 == error_callback) |
| 547 | return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS; |
| 548 | |
| 549 | /* |
| 550 | * To make sure that our file does not go unclosed after an error, we |
| 551 | * must assign the FILE pointer before any further error can occur in |
| 552 | * this routine. |
| 553 | */ |
| 554 | if(file == stdin) |
| 555 | file = get_binary_stdin_(); /* just to be safe */ |
| 556 | |
| 557 | decoder->private_->file = file; |
| 558 | |
| 559 | return init_stream_internal_( |
| 560 | decoder, |
| 561 | file_read_callback_, |
| 562 | decoder->private_->file == stdin? 0: file_seek_callback_, |
| 563 | decoder->private_->file == stdin? 0: file_tell_callback_, |
| 564 | decoder->private_->file == stdin? 0: file_length_callback_, |
| 565 | file_eof_callback_, |
| 566 | write_callback, |
| 567 | metadata_callback, |
| 568 | error_callback, |
| 569 | client_data, |
| 570 | is_ogg |
| 571 | ); |
| 572 | } |
| 573 | |
| 574 | FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_FILE( |
| 575 | FLAC__StreamDecoder *decoder, |
| 576 | FILE *file, |
| 577 | FLAC__StreamDecoderWriteCallback write_callback, |
| 578 | FLAC__StreamDecoderMetadataCallback metadata_callback, |
| 579 | FLAC__StreamDecoderErrorCallback error_callback, |
| 580 | void *client_data |
| 581 | ) |
| 582 | { |
| 583 | return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/false); |
| 584 | } |
| 585 | |
| 586 | FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_FILE( |
| 587 | FLAC__StreamDecoder *decoder, |
| 588 | FILE *file, |
| 589 | FLAC__StreamDecoderWriteCallback write_callback, |
| 590 | FLAC__StreamDecoderMetadataCallback metadata_callback, |
| 591 | FLAC__StreamDecoderErrorCallback error_callback, |
| 592 | void *client_data |
| 593 | ) |
| 594 | { |
| 595 | return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/true); |
| 596 | } |
| 597 | |
| 598 | static FLAC__StreamDecoderInitStatus init_file_internal_( |
| 599 | FLAC__StreamDecoder *decoder, |
| 600 | const char *filename, |
| 601 | FLAC__StreamDecoderWriteCallback write_callback, |
| 602 | FLAC__StreamDecoderMetadataCallback metadata_callback, |
| 603 | FLAC__StreamDecoderErrorCallback error_callback, |
| 604 | void *client_data, |
| 605 | FLAC__bool is_ogg |
| 606 | ) |
| 607 | { |
| 608 | FILE *file; |
| 609 | |
| 610 | FLAC__ASSERT(0 != decoder); |
| 611 | |
| 612 | /* |
| 613 | * To make sure that our file does not go unclosed after an error, we |
| 614 | * have to do the same entrance checks here that are later performed |
| 615 | * in FLAC__stream_decoder_init_FILE() before the FILE* is assigned. |
| 616 | */ |
| 617 | if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
| 618 | return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED; |
| 619 | |
| 620 | if(0 == write_callback || 0 == error_callback) |
| 621 | return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS; |
| 622 | |
| 623 | file = filename? fopen(filename, "rb") : stdin; |
| 624 | |
| 625 | if(0 == file) |
| 626 | return FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE; |
| 627 | |
| 628 | return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, is_ogg); |
| 629 | } |
| 630 | |
| 631 | FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_file( |
| 632 | FLAC__StreamDecoder *decoder, |
| 633 | const char *filename, |
| 634 | FLAC__StreamDecoderWriteCallback write_callback, |
| 635 | FLAC__StreamDecoderMetadataCallback metadata_callback, |
| 636 | FLAC__StreamDecoderErrorCallback error_callback, |
| 637 | void *client_data |
| 638 | ) |
| 639 | { |
| 640 | return init_file_internal_(decoder, filename, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/false); |
| 641 | } |
| 642 | |
| 643 | FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_file( |
| 644 | FLAC__StreamDecoder *decoder, |
| 645 | const char *filename, |
| 646 | FLAC__StreamDecoderWriteCallback write_callback, |
| 647 | FLAC__StreamDecoderMetadataCallback metadata_callback, |
| 648 | FLAC__StreamDecoderErrorCallback error_callback, |
| 649 | void *client_data |
| 650 | ) |
| 651 | { |
| 652 | return init_file_internal_(decoder, filename, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/true); |
| 653 | } |
| 654 | |
| 655 | FLAC_API FLAC__bool FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder) |
| 656 | { |
| 657 | FLAC__bool md5_failed = false; |
| 658 | unsigned i; |
| 659 | |
| 660 | FLAC__ASSERT(0 != decoder); |
| 661 | FLAC__ASSERT(0 != decoder->private_); |
| 662 | FLAC__ASSERT(0 != decoder->protected_); |
| 663 | |
| 664 | if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED) |
| 665 | return true; |
| 666 | |
| 667 | /* see the comment in FLAC__seekable_stream_decoder_reset() as to why we |
| 668 | * always call FLAC__MD5Final() |
| 669 | */ |
| 670 | FLAC__MD5Final(decoder->private_->computed_md5sum, &decoder->private_->md5context); |
| 671 | |
| 672 | if(decoder->private_->has_seek_table && 0 != decoder->private_->seek_table.data.seek_table.points) { |
| 673 | free(decoder->private_->seek_table.data.seek_table.points); |
| 674 | decoder->private_->seek_table.data.seek_table.points = 0; |
| 675 | decoder->private_->has_seek_table = false; |
| 676 | } |
| 677 | FLAC__bitreader_free(decoder->private_->input); |
| 678 | for(i = 0; i < FLAC__MAX_CHANNELS; i++) { |
| 679 | /* WATCHOUT: |
| 680 | * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the |
| 681 | * output arrays have a buffer of up to 3 zeroes in front |
| 682 | * (at negative indices) for alignment purposes; we use 4 |
| 683 | * to keep the data well-aligned. |
| 684 | */ |
| 685 | if(0 != decoder->private_->output[i]) { |
| 686 | free(decoder->private_->output[i]-4); |
| 687 | decoder->private_->output[i] = 0; |
| 688 | } |
| 689 | if(0 != decoder->private_->residual_unaligned[i]) { |
| 690 | free(decoder->private_->residual_unaligned[i]); |
| 691 | decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0; |
| 692 | } |
| 693 | } |
| 694 | decoder->private_->output_capacity = 0; |
| 695 | decoder->private_->output_channels = 0; |
| 696 | |
| 697 | #if FLAC__HAS_OGG |
| 698 | if(decoder->private_->is_ogg) |
| 699 | FLAC__ogg_decoder_aspect_finish(&decoder->protected_->ogg_decoder_aspect); |
| 700 | #endif |
| 701 | |
| 702 | if(0 != decoder->private_->file) { |
| 703 | if(decoder->private_->file != stdin) |
| 704 | fclose(decoder->private_->file); |
| 705 | decoder->private_->file = 0; |
| 706 | } |
| 707 | |
| 708 | if(decoder->private_->do_md5_checking) { |
| 709 | if(memcmp(decoder->private_->stream_info.data.stream_info.md5sum, decoder->private_->computed_md5sum, 16)) |
| 710 | md5_failed = true; |
| 711 | } |
| 712 | decoder->private_->is_seeking = false; |
| 713 | |
| 714 | set_defaults_(decoder); |
| 715 | |
| 716 | decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED; |
| 717 | |
| 718 | return !md5_failed; |
| 719 | } |
| 720 | |
| 721 | FLAC_API FLAC__bool FLAC__stream_decoder_set_ogg_serial_number(FLAC__StreamDecoder *decoder, long value) |
| 722 | { |
| 723 | FLAC__ASSERT(0 != decoder); |
| 724 | FLAC__ASSERT(0 != decoder->private_); |
| 725 | FLAC__ASSERT(0 != decoder->protected_); |
| 726 | if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
| 727 | return false; |
| 728 | #if FLAC__HAS_OGG |
| 729 | /* can't check decoder->private_->is_ogg since that's not set until init time */ |
| 730 | FLAC__ogg_decoder_aspect_set_serial_number(&decoder->protected_->ogg_decoder_aspect, value); |
| 731 | return true; |
| 732 | #else |
| 733 | (void)value; |
| 734 | return false; |
| 735 | #endif |
| 736 | } |
| 737 | |
| 738 | FLAC_API FLAC__bool FLAC__stream_decoder_set_md5_checking(FLAC__StreamDecoder *decoder, FLAC__bool value) |
| 739 | { |
| 740 | FLAC__ASSERT(0 != decoder); |
| 741 | FLAC__ASSERT(0 != decoder->protected_); |
| 742 | if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
| 743 | return false; |
| 744 | decoder->protected_->md5_checking = value; |
| 745 | return true; |
| 746 | } |
| 747 | |
| 748 | FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type) |
| 749 | { |
| 750 | FLAC__ASSERT(0 != decoder); |
| 751 | FLAC__ASSERT(0 != decoder->private_); |
| 752 | FLAC__ASSERT(0 != decoder->protected_); |
| 753 | FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE); |
| 754 | /* double protection */ |
| 755 | if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE) |
| 756 | return false; |
| 757 | if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
| 758 | return false; |
| 759 | decoder->private_->metadata_filter[type] = true; |
| 760 | if(type == FLAC__METADATA_TYPE_APPLICATION) |
| 761 | decoder->private_->metadata_filter_ids_count = 0; |
| 762 | return true; |
| 763 | } |
| 764 | |
| 765 | FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4]) |
| 766 | { |
| 767 | FLAC__ASSERT(0 != decoder); |
| 768 | FLAC__ASSERT(0 != decoder->private_); |
| 769 | FLAC__ASSERT(0 != decoder->protected_); |
| 770 | FLAC__ASSERT(0 != id); |
| 771 | if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
| 772 | return false; |
| 773 | |
| 774 | if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION]) |
| 775 | return true; |
| 776 | |
| 777 | FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids); |
| 778 | |
| 779 | if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) { |
| 780 | if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)safe_realloc_mul_2op_(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity, /*times*/2))) { |
| 781 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 782 | return false; |
| 783 | } |
| 784 | decoder->private_->metadata_filter_ids_capacity *= 2; |
| 785 | } |
| 786 | |
| 787 | memcpy(decoder->private_->metadata_filter_ids + decoder->private_->metadata_filter_ids_count * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)); |
| 788 | decoder->private_->metadata_filter_ids_count++; |
| 789 | |
| 790 | return true; |
| 791 | } |
| 792 | |
| 793 | FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder) |
| 794 | { |
| 795 | unsigned i; |
| 796 | FLAC__ASSERT(0 != decoder); |
| 797 | FLAC__ASSERT(0 != decoder->private_); |
| 798 | FLAC__ASSERT(0 != decoder->protected_); |
| 799 | if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
| 800 | return false; |
| 801 | for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++) |
| 802 | decoder->private_->metadata_filter[i] = true; |
| 803 | decoder->private_->metadata_filter_ids_count = 0; |
| 804 | return true; |
| 805 | } |
| 806 | |
| 807 | FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type) |
| 808 | { |
| 809 | FLAC__ASSERT(0 != decoder); |
| 810 | FLAC__ASSERT(0 != decoder->private_); |
| 811 | FLAC__ASSERT(0 != decoder->protected_); |
| 812 | FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE); |
| 813 | /* double protection */ |
| 814 | if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE) |
| 815 | return false; |
| 816 | if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
| 817 | return false; |
| 818 | decoder->private_->metadata_filter[type] = false; |
| 819 | if(type == FLAC__METADATA_TYPE_APPLICATION) |
| 820 | decoder->private_->metadata_filter_ids_count = 0; |
| 821 | return true; |
| 822 | } |
| 823 | |
| 824 | FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4]) |
| 825 | { |
| 826 | FLAC__ASSERT(0 != decoder); |
| 827 | FLAC__ASSERT(0 != decoder->private_); |
| 828 | FLAC__ASSERT(0 != decoder->protected_); |
| 829 | FLAC__ASSERT(0 != id); |
| 830 | if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
| 831 | return false; |
| 832 | |
| 833 | if(!decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION]) |
| 834 | return true; |
| 835 | |
| 836 | FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids); |
| 837 | |
| 838 | if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) { |
| 839 | if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)safe_realloc_mul_2op_(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity, /*times*/2))) { |
| 840 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 841 | return false; |
| 842 | } |
| 843 | decoder->private_->metadata_filter_ids_capacity *= 2; |
| 844 | } |
| 845 | |
| 846 | memcpy(decoder->private_->metadata_filter_ids + decoder->private_->metadata_filter_ids_count * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)); |
| 847 | decoder->private_->metadata_filter_ids_count++; |
| 848 | |
| 849 | return true; |
| 850 | } |
| 851 | |
| 852 | FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder) |
| 853 | { |
| 854 | FLAC__ASSERT(0 != decoder); |
| 855 | FLAC__ASSERT(0 != decoder->private_); |
| 856 | FLAC__ASSERT(0 != decoder->protected_); |
| 857 | if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
| 858 | return false; |
| 859 | memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter)); |
| 860 | decoder->private_->metadata_filter_ids_count = 0; |
| 861 | return true; |
| 862 | } |
| 863 | |
| 864 | FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder) |
| 865 | { |
| 866 | FLAC__ASSERT(0 != decoder); |
| 867 | FLAC__ASSERT(0 != decoder->protected_); |
| 868 | return decoder->protected_->state; |
| 869 | } |
| 870 | |
| 871 | FLAC_API const char *FLAC__stream_decoder_get_resolved_state_string(const FLAC__StreamDecoder *decoder) |
| 872 | { |
| 873 | return FLAC__StreamDecoderStateString[decoder->protected_->state]; |
| 874 | } |
| 875 | |
| 876 | FLAC_API FLAC__bool FLAC__stream_decoder_get_md5_checking(const FLAC__StreamDecoder *decoder) |
| 877 | { |
| 878 | FLAC__ASSERT(0 != decoder); |
| 879 | FLAC__ASSERT(0 != decoder->protected_); |
| 880 | return decoder->protected_->md5_checking; |
| 881 | } |
| 882 | |
| 883 | FLAC_API FLAC__uint64 FLAC__stream_decoder_get_total_samples(const FLAC__StreamDecoder *decoder) |
| 884 | { |
| 885 | FLAC__ASSERT(0 != decoder); |
| 886 | FLAC__ASSERT(0 != decoder->protected_); |
| 887 | return decoder->private_->has_stream_info? decoder->private_->stream_info.data.stream_info.total_samples : 0; |
| 888 | } |
| 889 | |
| 890 | FLAC_API unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder) |
| 891 | { |
| 892 | FLAC__ASSERT(0 != decoder); |
| 893 | FLAC__ASSERT(0 != decoder->protected_); |
| 894 | return decoder->protected_->channels; |
| 895 | } |
| 896 | |
| 897 | FLAC_API FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder) |
| 898 | { |
| 899 | FLAC__ASSERT(0 != decoder); |
| 900 | FLAC__ASSERT(0 != decoder->protected_); |
| 901 | return decoder->protected_->channel_assignment; |
| 902 | } |
| 903 | |
| 904 | FLAC_API unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder) |
| 905 | { |
| 906 | FLAC__ASSERT(0 != decoder); |
| 907 | FLAC__ASSERT(0 != decoder->protected_); |
| 908 | return decoder->protected_->bits_per_sample; |
| 909 | } |
| 910 | |
| 911 | FLAC_API unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder) |
| 912 | { |
| 913 | FLAC__ASSERT(0 != decoder); |
| 914 | FLAC__ASSERT(0 != decoder->protected_); |
| 915 | return decoder->protected_->sample_rate; |
| 916 | } |
| 917 | |
| 918 | FLAC_API unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder) |
| 919 | { |
| 920 | FLAC__ASSERT(0 != decoder); |
| 921 | FLAC__ASSERT(0 != decoder->protected_); |
| 922 | return decoder->protected_->blocksize; |
| 923 | } |
| 924 | |
| 925 | FLAC_API FLAC__bool FLAC__stream_decoder_get_decode_position(const FLAC__StreamDecoder *decoder, FLAC__uint64 *position) |
| 926 | { |
| 927 | FLAC__ASSERT(0 != decoder); |
| 928 | FLAC__ASSERT(0 != decoder->private_); |
| 929 | FLAC__ASSERT(0 != position); |
| 930 | |
| 931 | #if FLAC__HAS_OGG |
| 932 | if(decoder->private_->is_ogg) |
| 933 | return false; |
| 934 | #endif |
| 935 | if(0 == decoder->private_->tell_callback) |
| 936 | return false; |
| 937 | if(decoder->private_->tell_callback(decoder, position, decoder->private_->client_data) != FLAC__STREAM_DECODER_TELL_STATUS_OK) |
| 938 | return false; |
| 939 | /* should never happen since all FLAC frames and metadata blocks are byte aligned, but check just in case */ |
| 940 | if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) |
| 941 | return false; |
| 942 | FLAC__ASSERT(*position >= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder)); |
| 943 | *position -= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder); |
| 944 | return true; |
| 945 | } |
| 946 | |
| 947 | FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder) |
| 948 | { |
| 949 | FLAC__ASSERT(0 != decoder); |
| 950 | FLAC__ASSERT(0 != decoder->private_); |
| 951 | FLAC__ASSERT(0 != decoder->protected_); |
| 952 | |
| 953 | decoder->private_->samples_decoded = 0; |
| 954 | decoder->private_->do_md5_checking = false; |
| 955 | |
| 956 | #if FLAC__HAS_OGG |
| 957 | if(decoder->private_->is_ogg) |
| 958 | FLAC__ogg_decoder_aspect_flush(&decoder->protected_->ogg_decoder_aspect); |
| 959 | #endif |
| 960 | |
| 961 | if(!FLAC__bitreader_clear(decoder->private_->input)) { |
| 962 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 963 | return false; |
| 964 | } |
| 965 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 966 | |
| 967 | return true; |
| 968 | } |
| 969 | |
| 970 | FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder) |
| 971 | { |
| 972 | FLAC__ASSERT(0 != decoder); |
| 973 | FLAC__ASSERT(0 != decoder->private_); |
| 974 | FLAC__ASSERT(0 != decoder->protected_); |
| 975 | |
| 976 | if(!FLAC__stream_decoder_flush(decoder)) { |
| 977 | /* above call sets the state for us */ |
| 978 | return false; |
| 979 | } |
| 980 | |
| 981 | #if FLAC__HAS_OGG |
| 982 | /*@@@ could go in !internal_reset_hack block below */ |
| 983 | if(decoder->private_->is_ogg) |
| 984 | FLAC__ogg_decoder_aspect_reset(&decoder->protected_->ogg_decoder_aspect); |
| 985 | #endif |
| 986 | |
| 987 | /* Rewind if necessary. If FLAC__stream_decoder_init() is calling us, |
| 988 | * (internal_reset_hack) don't try to rewind since we are already at |
| 989 | * the beginning of the stream and don't want to fail if the input is |
| 990 | * not seekable. |
| 991 | */ |
| 992 | if(!decoder->private_->internal_reset_hack) { |
| 993 | if(decoder->private_->file == stdin) |
| 994 | return false; /* can't rewind stdin, reset fails */ |
| 995 | if(decoder->private_->seek_callback && decoder->private_->seek_callback(decoder, 0, decoder->private_->client_data) == FLAC__STREAM_DECODER_SEEK_STATUS_ERROR) |
| 996 | return false; /* seekable and seek fails, reset fails */ |
| 997 | } |
| 998 | else |
| 999 | decoder->private_->internal_reset_hack = false; |
| 1000 | |
| 1001 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA; |
| 1002 | |
| 1003 | decoder->private_->has_stream_info = false; |
| 1004 | if(decoder->private_->has_seek_table && 0 != decoder->private_->seek_table.data.seek_table.points) { |
| 1005 | free(decoder->private_->seek_table.data.seek_table.points); |
| 1006 | decoder->private_->seek_table.data.seek_table.points = 0; |
| 1007 | decoder->private_->has_seek_table = false; |
| 1008 | } |
| 1009 | decoder->private_->do_md5_checking = decoder->protected_->md5_checking; |
| 1010 | /* |
| 1011 | * This goes in reset() and not flush() because according to the spec, a |
| 1012 | * fixed-blocksize stream must stay that way through the whole stream. |
| 1013 | */ |
| 1014 | decoder->private_->fixed_block_size = decoder->private_->next_fixed_block_size = 0; |
| 1015 | |
| 1016 | /* We initialize the FLAC__MD5Context even though we may never use it. This |
| 1017 | * is because md5 checking may be turned on to start and then turned off if |
| 1018 | * a seek occurs. So we init the context here and finalize it in |
| 1019 | * FLAC__stream_decoder_finish() to make sure things are always cleaned up |
| 1020 | * properly. |
| 1021 | */ |
| 1022 | FLAC__MD5Init(&decoder->private_->md5context); |
| 1023 | |
| 1024 | decoder->private_->first_frame_offset = 0; |
| 1025 | decoder->private_->unparseable_frame_count = 0; |
| 1026 | |
| 1027 | return true; |
| 1028 | } |
| 1029 | |
| 1030 | FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder) |
| 1031 | { |
| 1032 | FLAC__bool got_a_frame; |
| 1033 | FLAC__ASSERT(0 != decoder); |
| 1034 | FLAC__ASSERT(0 != decoder->protected_); |
| 1035 | |
| 1036 | while(1) { |
| 1037 | switch(decoder->protected_->state) { |
| 1038 | case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA: |
| 1039 | if(!find_metadata_(decoder)) |
| 1040 | return false; /* above function sets the status for us */ |
| 1041 | break; |
| 1042 | case FLAC__STREAM_DECODER_READ_METADATA: |
| 1043 | if(!read_metadata_(decoder)) |
| 1044 | return false; /* above function sets the status for us */ |
| 1045 | else |
| 1046 | return true; |
| 1047 | case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC: |
| 1048 | if(!frame_sync_(decoder)) |
| 1049 | return true; /* above function sets the status for us */ |
| 1050 | break; |
| 1051 | case FLAC__STREAM_DECODER_READ_FRAME: |
| 1052 | if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/true)) |
| 1053 | return false; /* above function sets the status for us */ |
| 1054 | if(got_a_frame) |
| 1055 | return true; /* above function sets the status for us */ |
| 1056 | break; |
| 1057 | case FLAC__STREAM_DECODER_END_OF_STREAM: |
| 1058 | case FLAC__STREAM_DECODER_ABORTED: |
| 1059 | return true; |
| 1060 | default: |
| 1061 | FLAC__ASSERT(0); |
| 1062 | return false; |
| 1063 | } |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder) |
| 1068 | { |
| 1069 | FLAC__ASSERT(0 != decoder); |
| 1070 | FLAC__ASSERT(0 != decoder->protected_); |
| 1071 | |
| 1072 | while(1) { |
| 1073 | switch(decoder->protected_->state) { |
| 1074 | case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA: |
| 1075 | if(!find_metadata_(decoder)) |
| 1076 | return false; /* above function sets the status for us */ |
| 1077 | break; |
| 1078 | case FLAC__STREAM_DECODER_READ_METADATA: |
| 1079 | if(!read_metadata_(decoder)) |
| 1080 | return false; /* above function sets the status for us */ |
| 1081 | break; |
| 1082 | case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC: |
| 1083 | case FLAC__STREAM_DECODER_READ_FRAME: |
| 1084 | case FLAC__STREAM_DECODER_END_OF_STREAM: |
| 1085 | case FLAC__STREAM_DECODER_ABORTED: |
| 1086 | return true; |
| 1087 | default: |
| 1088 | FLAC__ASSERT(0); |
| 1089 | return false; |
| 1090 | } |
| 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder) |
| 1095 | { |
| 1096 | FLAC__bool dummy; |
| 1097 | FLAC__ASSERT(0 != decoder); |
| 1098 | FLAC__ASSERT(0 != decoder->protected_); |
| 1099 | |
| 1100 | while(1) { |
| 1101 | switch(decoder->protected_->state) { |
| 1102 | case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA: |
| 1103 | if(!find_metadata_(decoder)) |
| 1104 | return false; /* above function sets the status for us */ |
| 1105 | break; |
| 1106 | case FLAC__STREAM_DECODER_READ_METADATA: |
| 1107 | if(!read_metadata_(decoder)) |
| 1108 | return false; /* above function sets the status for us */ |
| 1109 | break; |
| 1110 | case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC: |
| 1111 | if(!frame_sync_(decoder)) |
| 1112 | return true; /* above function sets the status for us */ |
| 1113 | break; |
| 1114 | case FLAC__STREAM_DECODER_READ_FRAME: |
| 1115 | if(!read_frame_(decoder, &dummy, /*do_full_decode=*/true)) |
| 1116 | return false; /* above function sets the status for us */ |
| 1117 | break; |
| 1118 | case FLAC__STREAM_DECODER_END_OF_STREAM: |
| 1119 | case FLAC__STREAM_DECODER_ABORTED: |
| 1120 | return true; |
| 1121 | default: |
| 1122 | FLAC__ASSERT(0); |
| 1123 | return false; |
| 1124 | } |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | FLAC_API FLAC__bool FLAC__stream_decoder_skip_single_frame(FLAC__StreamDecoder *decoder) |
| 1129 | { |
| 1130 | FLAC__bool got_a_frame; |
| 1131 | FLAC__ASSERT(0 != decoder); |
| 1132 | FLAC__ASSERT(0 != decoder->protected_); |
| 1133 | |
| 1134 | while(1) { |
| 1135 | switch(decoder->protected_->state) { |
| 1136 | case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA: |
| 1137 | case FLAC__STREAM_DECODER_READ_METADATA: |
| 1138 | return false; /* above function sets the status for us */ |
| 1139 | case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC: |
| 1140 | if(!frame_sync_(decoder)) |
| 1141 | return true; /* above function sets the status for us */ |
| 1142 | break; |
| 1143 | case FLAC__STREAM_DECODER_READ_FRAME: |
| 1144 | if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/false)) |
| 1145 | return false; /* above function sets the status for us */ |
| 1146 | if(got_a_frame) |
| 1147 | return true; /* above function sets the status for us */ |
| 1148 | break; |
| 1149 | case FLAC__STREAM_DECODER_END_OF_STREAM: |
| 1150 | case FLAC__STREAM_DECODER_ABORTED: |
| 1151 | return true; |
| 1152 | default: |
| 1153 | FLAC__ASSERT(0); |
| 1154 | return false; |
| 1155 | } |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | FLAC_API FLAC__bool FLAC__stream_decoder_seek_absolute(FLAC__StreamDecoder *decoder, FLAC__uint64 sample) |
| 1160 | { |
| 1161 | FLAC__uint64 length; |
| 1162 | |
| 1163 | FLAC__ASSERT(0 != decoder); |
| 1164 | |
| 1165 | if( |
| 1166 | decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA && |
| 1167 | decoder->protected_->state != FLAC__STREAM_DECODER_READ_METADATA && |
| 1168 | decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC && |
| 1169 | decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME && |
| 1170 | decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM |
| 1171 | ) |
| 1172 | return false; |
| 1173 | |
| 1174 | if(0 == decoder->private_->seek_callback) |
| 1175 | return false; |
| 1176 | |
| 1177 | FLAC__ASSERT(decoder->private_->seek_callback); |
| 1178 | FLAC__ASSERT(decoder->private_->tell_callback); |
| 1179 | FLAC__ASSERT(decoder->private_->length_callback); |
| 1180 | FLAC__ASSERT(decoder->private_->eof_callback); |
| 1181 | |
| 1182 | if(FLAC__stream_decoder_get_total_samples(decoder) > 0 && sample >= FLAC__stream_decoder_get_total_samples(decoder)) |
| 1183 | return false; |
| 1184 | |
| 1185 | decoder->private_->is_seeking = true; |
| 1186 | |
| 1187 | /* turn off md5 checking if a seek is attempted */ |
| 1188 | decoder->private_->do_md5_checking = false; |
| 1189 | |
| 1190 | /* get the file length (currently our algorithm needs to know the length so it's also an error to get FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED) */ |
| 1191 | if(decoder->private_->length_callback(decoder, &length, decoder->private_->client_data) != FLAC__STREAM_DECODER_LENGTH_STATUS_OK) { |
| 1192 | decoder->private_->is_seeking = false; |
| 1193 | return false; |
| 1194 | } |
| 1195 | |
| 1196 | /* if we haven't finished processing the metadata yet, do that so we have the STREAMINFO, SEEK_TABLE, and first_frame_offset */ |
| 1197 | if( |
| 1198 | decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA || |
| 1199 | decoder->protected_->state == FLAC__STREAM_DECODER_READ_METADATA |
| 1200 | ) { |
| 1201 | if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder)) { |
| 1202 | /* above call sets the state for us */ |
| 1203 | decoder->private_->is_seeking = false; |
| 1204 | return false; |
| 1205 | } |
| 1206 | /* check this again in case we didn't know total_samples the first time */ |
| 1207 | if(FLAC__stream_decoder_get_total_samples(decoder) > 0 && sample >= FLAC__stream_decoder_get_total_samples(decoder)) { |
| 1208 | decoder->private_->is_seeking = false; |
| 1209 | return false; |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | { |
| 1214 | const FLAC__bool ok = |
| 1215 | #if FLAC__HAS_OGG |
| 1216 | decoder->private_->is_ogg? |
| 1217 | seek_to_absolute_sample_ogg_(decoder, length, sample) : |
| 1218 | #endif |
| 1219 | seek_to_absolute_sample_(decoder, length, sample) |
| 1220 | ; |
| 1221 | decoder->private_->is_seeking = false; |
| 1222 | return ok; |
| 1223 | } |
| 1224 | } |
| 1225 | |
| 1226 | /*********************************************************************** |
| 1227 | * |
| 1228 | * Protected class methods |
| 1229 | * |
| 1230 | ***********************************************************************/ |
| 1231 | |
| 1232 | unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder) |
| 1233 | { |
| 1234 | FLAC__ASSERT(0 != decoder); |
| 1235 | FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); |
| 1236 | FLAC__ASSERT(!(FLAC__bitreader_get_input_bits_unconsumed(decoder->private_->input) & 7)); |
| 1237 | return FLAC__bitreader_get_input_bits_unconsumed(decoder->private_->input) / 8; |
| 1238 | } |
| 1239 | |
| 1240 | /*********************************************************************** |
| 1241 | * |
| 1242 | * Private class methods |
| 1243 | * |
| 1244 | ***********************************************************************/ |
| 1245 | |
| 1246 | void set_defaults_(FLAC__StreamDecoder *decoder) |
| 1247 | { |
| 1248 | #if FLAC__HAS_OGG |
| 1249 | decoder->private_->is_ogg = false; |
| 1250 | #endif |
| 1251 | decoder->private_->read_callback = 0; |
| 1252 | decoder->private_->seek_callback = 0; |
| 1253 | decoder->private_->tell_callback = 0; |
| 1254 | decoder->private_->length_callback = 0; |
| 1255 | decoder->private_->eof_callback = 0; |
| 1256 | decoder->private_->write_callback = 0; |
| 1257 | decoder->private_->metadata_callback = 0; |
| 1258 | decoder->private_->error_callback = 0; |
| 1259 | decoder->private_->client_data = 0; |
| 1260 | |
| 1261 | memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter)); |
| 1262 | decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true; |
| 1263 | decoder->private_->metadata_filter_ids_count = 0; |
| 1264 | |
| 1265 | decoder->protected_->md5_checking = false; |
| 1266 | |
| 1267 | #if FLAC__HAS_OGG |
| 1268 | FLAC__ogg_decoder_aspect_set_defaults(&decoder->protected_->ogg_decoder_aspect); |
| 1269 | #endif |
| 1270 | } |
| 1271 | |
| 1272 | /* |
| 1273 | * This will forcibly set stdin to binary mode (for OSes that require it) |
| 1274 | */ |
| 1275 | FILE *get_binary_stdin_(void) |
| 1276 | { |
| 1277 | #if 0 |
| 1278 | /* if something breaks here it is probably due to the presence or |
| 1279 | * absence of an underscore before the identifiers 'setmode', |
| 1280 | * 'fileno', and/or 'O_BINARY'; check your system header files. |
| 1281 | */ |
| 1282 | #if defined _MSC_VER || defined __MINGW32__ |
| 1283 | _setmode(_fileno(stdin), _O_BINARY); |
| 1284 | #elif defined __CYGWIN__ |
| 1285 | /* almost certainly not needed for any modern Cygwin, but let's be safe... */ |
| 1286 | setmode(_fileno(stdin), _O_BINARY); |
| 1287 | #elif defined __EMX__ |
| 1288 | setmode(fileno(stdin), O_BINARY); |
| 1289 | #endif |
| 1290 | #endif |
| 1291 | return stdin; |
| 1292 | } |
| 1293 | |
| 1294 | FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels) |
| 1295 | { |
| 1296 | unsigned i; |
| 1297 | FLAC__int32 *tmp; |
| 1298 | |
| 1299 | if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels) |
| 1300 | return true; |
| 1301 | |
| 1302 | /* simply using realloc() is not practical because the number of channels may change mid-stream */ |
| 1303 | |
| 1304 | for(i = 0; i < FLAC__MAX_CHANNELS; i++) { |
| 1305 | if(0 != decoder->private_->output[i]) { |
| 1306 | free(decoder->private_->output[i]-4); |
| 1307 | decoder->private_->output[i] = 0; |
| 1308 | } |
| 1309 | if(0 != decoder->private_->residual_unaligned[i]) { |
| 1310 | free(decoder->private_->residual_unaligned[i]); |
| 1311 | decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0; |
| 1312 | } |
| 1313 | } |
| 1314 | |
| 1315 | for(i = 0; i < channels; i++) { |
| 1316 | /* WATCHOUT: |
| 1317 | * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the |
| 1318 | * output arrays have a buffer of up to 3 zeroes in front |
| 1319 | * (at negative indices) for alignment purposes; we use 4 |
| 1320 | * to keep the data well-aligned. |
| 1321 | */ |
| 1322 | tmp = (FLAC__int32*)safe_malloc_muladd2_(sizeof(FLAC__int32), /*times (*/size, /*+*/4/*)*/); |
| 1323 | if(tmp == 0) { |
| 1324 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 1325 | return false; |
| 1326 | } |
| 1327 | memset(tmp, 0, sizeof(FLAC__int32)*4); |
| 1328 | decoder->private_->output[i] = tmp + 4; |
| 1329 | |
| 1330 | /* WATCHOUT: |
| 1331 | * minimum of quadword alignment for PPC vector optimizations is REQUIRED: |
| 1332 | */ |
| 1333 | if(!FLAC__memory_alloc_aligned_int32_array(size, &decoder->private_->residual_unaligned[i], &decoder->private_->residual[i])) { |
| 1334 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 1335 | return false; |
| 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | decoder->private_->output_capacity = size; |
| 1340 | decoder->private_->output_channels = channels; |
| 1341 | |
| 1342 | return true; |
| 1343 | } |
| 1344 | |
| 1345 | FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id) |
| 1346 | { |
| 1347 | size_t i; |
| 1348 | |
| 1349 | FLAC__ASSERT(0 != decoder); |
| 1350 | FLAC__ASSERT(0 != decoder->private_); |
| 1351 | |
| 1352 | for(i = 0; i < decoder->private_->metadata_filter_ids_count; i++) |
| 1353 | if(0 == memcmp(decoder->private_->metadata_filter_ids + i * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8))) |
| 1354 | return true; |
| 1355 | |
| 1356 | return false; |
| 1357 | } |
| 1358 | |
| 1359 | FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder) |
| 1360 | { |
| 1361 | FLAC__uint32 x; |
| 1362 | unsigned i, id; |
| 1363 | FLAC__bool first = true; |
| 1364 | |
| 1365 | FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); |
| 1366 | |
| 1367 | for(i = id = 0; i < 4; ) { |
| 1368 | if(decoder->private_->cached) { |
| 1369 | x = (FLAC__uint32)decoder->private_->lookahead; |
| 1370 | decoder->private_->cached = false; |
| 1371 | } |
| 1372 | else { |
| 1373 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) |
| 1374 | return false; /* read_callback_ sets the state for us */ |
| 1375 | } |
| 1376 | if(x == FLAC__STREAM_SYNC_STRING[i]) { |
| 1377 | first = true; |
| 1378 | i++; |
| 1379 | id = 0; |
| 1380 | continue; |
| 1381 | } |
| 1382 | if(x == ID3V2_TAG_[id]) { |
| 1383 | id++; |
| 1384 | i = 0; |
| 1385 | if(id == 3) { |
| 1386 | if(!skip_id3v2_tag_(decoder)) |
| 1387 | return false; /* skip_id3v2_tag_ sets the state for us */ |
| 1388 | } |
| 1389 | continue; |
| 1390 | } |
| 1391 | id = 0; |
| 1392 | if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */ |
| 1393 | decoder->private_->header_warmup[0] = (FLAC__byte)x; |
| 1394 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) |
| 1395 | return false; /* read_callback_ sets the state for us */ |
| 1396 | |
| 1397 | /* we have to check if we just read two 0xff's in a row; the second may actually be the beginning of the sync code */ |
| 1398 | /* else we have to check if the second byte is the end of a sync code */ |
| 1399 | if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */ |
| 1400 | decoder->private_->lookahead = (FLAC__byte)x; |
| 1401 | decoder->private_->cached = true; |
| 1402 | } |
| 1403 | else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */ |
| 1404 | decoder->private_->header_warmup[1] = (FLAC__byte)x; |
| 1405 | decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME; |
| 1406 | return true; |
| 1407 | } |
| 1408 | } |
| 1409 | i = 0; |
| 1410 | if(first) { |
| 1411 | send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC); |
| 1412 | first = false; |
| 1413 | } |
| 1414 | } |
| 1415 | |
| 1416 | decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA; |
| 1417 | return true; |
| 1418 | } |
| 1419 | |
| 1420 | FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder) |
| 1421 | { |
| 1422 | FLAC__bool is_last; |
| 1423 | FLAC__uint32 i, x, type, length; |
| 1424 | |
| 1425 | FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); |
| 1426 | |
| 1427 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_IS_LAST_LEN)) |
| 1428 | return false; /* read_callback_ sets the state for us */ |
| 1429 | is_last = x? true : false; |
| 1430 | |
| 1431 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN)) |
| 1432 | return false; /* read_callback_ sets the state for us */ |
| 1433 | |
| 1434 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN)) |
| 1435 | return false; /* read_callback_ sets the state for us */ |
| 1436 | |
| 1437 | if(type == FLAC__METADATA_TYPE_STREAMINFO) { |
| 1438 | if(!read_metadata_streaminfo_(decoder, is_last, length)) |
| 1439 | return false; |
| 1440 | |
| 1441 | decoder->private_->has_stream_info = true; |
| 1442 | if(0 == memcmp(decoder->private_->stream_info.data.stream_info.md5sum, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16)) |
| 1443 | decoder->private_->do_md5_checking = false; |
| 1444 | if(!decoder->private_->is_seeking && decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] && decoder->private_->metadata_callback) |
| 1445 | decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data); |
| 1446 | } |
| 1447 | else if(type == FLAC__METADATA_TYPE_SEEKTABLE) { |
| 1448 | if(!read_metadata_seektable_(decoder, is_last, length)) |
| 1449 | return false; |
| 1450 | |
| 1451 | decoder->private_->has_seek_table = true; |
| 1452 | if(!decoder->private_->is_seeking && decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE] && decoder->private_->metadata_callback) |
| 1453 | decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data); |
| 1454 | } |
| 1455 | else { |
| 1456 | FLAC__bool skip_it = !decoder->private_->metadata_filter[type]; |
| 1457 | unsigned real_length = length; |
| 1458 | FLAC__StreamMetadata block; |
| 1459 | |
| 1460 | block.is_last = is_last; |
| 1461 | block.type = (FLAC__MetadataType)type; |
| 1462 | block.length = length; |
| 1463 | |
| 1464 | if(type == FLAC__METADATA_TYPE_APPLICATION) { |
| 1465 | if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)) |
| 1466 | return false; /* read_callback_ sets the state for us */ |
| 1467 | |
| 1468 | if(real_length < FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) { /* underflow check */ |
| 1469 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;/*@@@@@@ maybe wrong error? need to resync?*/ |
| 1470 | return false; |
| 1471 | } |
| 1472 | |
| 1473 | real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8; |
| 1474 | |
| 1475 | if(decoder->private_->metadata_filter_ids_count > 0 && has_id_filtered_(decoder, block.data.application.id)) |
| 1476 | skip_it = !skip_it; |
| 1477 | } |
| 1478 | |
| 1479 | if(skip_it) { |
| 1480 | if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, real_length)) |
| 1481 | return false; /* read_callback_ sets the state for us */ |
| 1482 | } |
| 1483 | else { |
| 1484 | switch(type) { |
| 1485 | case FLAC__METADATA_TYPE_PADDING: |
| 1486 | /* skip the padding bytes */ |
| 1487 | if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, real_length)) |
| 1488 | return false; /* read_callback_ sets the state for us */ |
| 1489 | break; |
| 1490 | case FLAC__METADATA_TYPE_APPLICATION: |
| 1491 | /* remember, we read the ID already */ |
| 1492 | if(real_length > 0) { |
| 1493 | if(0 == (block.data.application.data = (FLAC__byte*)malloc(real_length))) { |
| 1494 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 1495 | return false; |
| 1496 | } |
| 1497 | if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.data, real_length)) |
| 1498 | return false; /* read_callback_ sets the state for us */ |
| 1499 | } |
| 1500 | else |
| 1501 | block.data.application.data = 0; |
| 1502 | break; |
| 1503 | case FLAC__METADATA_TYPE_VORBIS_COMMENT: |
| 1504 | if(!read_metadata_vorbiscomment_(decoder, &block.data.vorbis_comment)) |
| 1505 | return false; |
| 1506 | break; |
| 1507 | case FLAC__METADATA_TYPE_CUESHEET: |
| 1508 | if(!read_metadata_cuesheet_(decoder, &block.data.cue_sheet)) |
| 1509 | return false; |
| 1510 | break; |
| 1511 | case FLAC__METADATA_TYPE_PICTURE: |
| 1512 | if(!read_metadata_picture_(decoder, &block.data.picture)) |
| 1513 | return false; |
| 1514 | break; |
| 1515 | case FLAC__METADATA_TYPE_STREAMINFO: |
| 1516 | case FLAC__METADATA_TYPE_SEEKTABLE: |
| 1517 | FLAC__ASSERT(0); |
| 1518 | break; |
| 1519 | default: |
| 1520 | if(real_length > 0) { |
| 1521 | if(0 == (block.data.unknown.data = (FLAC__byte*)malloc(real_length))) { |
| 1522 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 1523 | return false; |
| 1524 | } |
| 1525 | if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.unknown.data, real_length)) |
| 1526 | return false; /* read_callback_ sets the state for us */ |
| 1527 | } |
| 1528 | else |
| 1529 | block.data.unknown.data = 0; |
| 1530 | break; |
| 1531 | } |
| 1532 | if(!decoder->private_->is_seeking && decoder->private_->metadata_callback) |
| 1533 | decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data); |
| 1534 | |
| 1535 | /* now we have to free any malloc()ed data in the block */ |
| 1536 | switch(type) { |
| 1537 | case FLAC__METADATA_TYPE_PADDING: |
| 1538 | break; |
| 1539 | case FLAC__METADATA_TYPE_APPLICATION: |
| 1540 | if(0 != block.data.application.data) |
| 1541 | free(block.data.application.data); |
| 1542 | break; |
| 1543 | case FLAC__METADATA_TYPE_VORBIS_COMMENT: |
| 1544 | if(0 != block.data.vorbis_comment.vendor_string.entry) |
| 1545 | free(block.data.vorbis_comment.vendor_string.entry); |
| 1546 | if(block.data.vorbis_comment.num_comments > 0) |
| 1547 | for(i = 0; i < block.data.vorbis_comment.num_comments; i++) |
| 1548 | if(0 != block.data.vorbis_comment.comments[i].entry) |
| 1549 | free(block.data.vorbis_comment.comments[i].entry); |
| 1550 | if(0 != block.data.vorbis_comment.comments) |
| 1551 | free(block.data.vorbis_comment.comments); |
| 1552 | break; |
| 1553 | case FLAC__METADATA_TYPE_CUESHEET: |
| 1554 | if(block.data.cue_sheet.num_tracks > 0) |
| 1555 | for(i = 0; i < block.data.cue_sheet.num_tracks; i++) |
| 1556 | if(0 != block.data.cue_sheet.tracks[i].indices) |
| 1557 | free(block.data.cue_sheet.tracks[i].indices); |
| 1558 | if(0 != block.data.cue_sheet.tracks) |
| 1559 | free(block.data.cue_sheet.tracks); |
| 1560 | break; |
| 1561 | case FLAC__METADATA_TYPE_PICTURE: |
| 1562 | if(0 != block.data.picture.mime_type) |
| 1563 | free(block.data.picture.mime_type); |
| 1564 | if(0 != block.data.picture.description) |
| 1565 | free(block.data.picture.description); |
| 1566 | if(0 != block.data.picture.data) |
| 1567 | free(block.data.picture.data); |
| 1568 | break; |
| 1569 | case FLAC__METADATA_TYPE_STREAMINFO: |
| 1570 | case FLAC__METADATA_TYPE_SEEKTABLE: |
| 1571 | FLAC__ASSERT(0); |
| 1572 | default: |
| 1573 | if(0 != block.data.unknown.data) |
| 1574 | free(block.data.unknown.data); |
| 1575 | break; |
| 1576 | } |
| 1577 | } |
| 1578 | } |
| 1579 | |
| 1580 | if(is_last) { |
| 1581 | /* if this fails, it's OK, it's just a hint for the seek routine */ |
| 1582 | if(!FLAC__stream_decoder_get_decode_position(decoder, &decoder->private_->first_frame_offset)) |
| 1583 | decoder->private_->first_frame_offset = 0; |
| 1584 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 1585 | } |
| 1586 | |
| 1587 | return true; |
| 1588 | } |
| 1589 | |
| 1590 | FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length) |
| 1591 | { |
| 1592 | FLAC__uint32 x; |
| 1593 | unsigned bits, used_bits = 0; |
| 1594 | |
| 1595 | FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); |
| 1596 | |
| 1597 | decoder->private_->stream_info.type = FLAC__METADATA_TYPE_STREAMINFO; |
| 1598 | decoder->private_->stream_info.is_last = is_last; |
| 1599 | decoder->private_->stream_info.length = length; |
| 1600 | |
| 1601 | bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN; |
| 1602 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, bits)) |
| 1603 | return false; /* read_callback_ sets the state for us */ |
| 1604 | decoder->private_->stream_info.data.stream_info.min_blocksize = x; |
| 1605 | used_bits += bits; |
| 1606 | |
| 1607 | bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN; |
| 1608 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN)) |
| 1609 | return false; /* read_callback_ sets the state for us */ |
| 1610 | decoder->private_->stream_info.data.stream_info.max_blocksize = x; |
| 1611 | used_bits += bits; |
| 1612 | |
| 1613 | bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN; |
| 1614 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN)) |
| 1615 | return false; /* read_callback_ sets the state for us */ |
| 1616 | decoder->private_->stream_info.data.stream_info.min_framesize = x; |
| 1617 | used_bits += bits; |
| 1618 | |
| 1619 | bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN; |
| 1620 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN)) |
| 1621 | return false; /* read_callback_ sets the state for us */ |
| 1622 | decoder->private_->stream_info.data.stream_info.max_framesize = x; |
| 1623 | used_bits += bits; |
| 1624 | |
| 1625 | bits = FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN; |
| 1626 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN)) |
| 1627 | return false; /* read_callback_ sets the state for us */ |
| 1628 | decoder->private_->stream_info.data.stream_info.sample_rate = x; |
| 1629 | used_bits += bits; |
| 1630 | |
| 1631 | bits = FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN; |
| 1632 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN)) |
| 1633 | return false; /* read_callback_ sets the state for us */ |
| 1634 | decoder->private_->stream_info.data.stream_info.channels = x+1; |
| 1635 | used_bits += bits; |
| 1636 | |
| 1637 | bits = FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN; |
| 1638 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN)) |
| 1639 | return false; /* read_callback_ sets the state for us */ |
| 1640 | decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1; |
| 1641 | used_bits += bits; |
| 1642 | |
| 1643 | bits = FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN; |
| 1644 | if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &decoder->private_->stream_info.data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN)) |
| 1645 | return false; /* read_callback_ sets the state for us */ |
| 1646 | used_bits += bits; |
| 1647 | |
| 1648 | if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16)) |
| 1649 | return false; /* read_callback_ sets the state for us */ |
| 1650 | used_bits += 16*8; |
| 1651 | |
| 1652 | /* skip the rest of the block */ |
| 1653 | FLAC__ASSERT(used_bits % 8 == 0); |
| 1654 | length -= (used_bits / 8); |
| 1655 | if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, length)) |
| 1656 | return false; /* read_callback_ sets the state for us */ |
| 1657 | |
| 1658 | return true; |
| 1659 | } |
| 1660 | |
| 1661 | FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length) |
| 1662 | { |
| 1663 | FLAC__uint32 i, x; |
| 1664 | FLAC__uint64 xx; |
| 1665 | |
| 1666 | FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); |
| 1667 | |
| 1668 | decoder->private_->seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE; |
| 1669 | decoder->private_->seek_table.is_last = is_last; |
| 1670 | decoder->private_->seek_table.length = length; |
| 1671 | |
| 1672 | decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH; |
| 1673 | |
| 1674 | /* use realloc since we may pass through here several times (e.g. after seeking) */ |
| 1675 | if(0 == (decoder->private_->seek_table.data.seek_table.points = (FLAC__StreamMetadata_SeekPoint*)safe_realloc_mul_2op_(decoder->private_->seek_table.data.seek_table.points, decoder->private_->seek_table.data.seek_table.num_points, /*times*/sizeof(FLAC__StreamMetadata_SeekPoint)))) { |
| 1676 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 1677 | return false; |
| 1678 | } |
| 1679 | for(i = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) { |
| 1680 | if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN)) |
| 1681 | return false; /* read_callback_ sets the state for us */ |
| 1682 | decoder->private_->seek_table.data.seek_table.points[i].sample_number = xx; |
| 1683 | |
| 1684 | if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN)) |
| 1685 | return false; /* read_callback_ sets the state for us */ |
| 1686 | decoder->private_->seek_table.data.seek_table.points[i].stream_offset = xx; |
| 1687 | |
| 1688 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN)) |
| 1689 | return false; /* read_callback_ sets the state for us */ |
| 1690 | decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x; |
| 1691 | } |
| 1692 | length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH); |
| 1693 | /* if there is a partial point left, skip over it */ |
| 1694 | if(length > 0) { |
| 1695 | /*@@@ do a send_error_to_client_() here? there's an argument for either way */ |
| 1696 | if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, length)) |
| 1697 | return false; /* read_callback_ sets the state for us */ |
| 1698 | } |
| 1699 | |
| 1700 | return true; |
| 1701 | } |
| 1702 | |
| 1703 | FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj) |
| 1704 | { |
| 1705 | FLAC__uint32 i; |
| 1706 | |
| 1707 | FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); |
| 1708 | |
| 1709 | /* read vendor string */ |
| 1710 | FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32); |
| 1711 | if(!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->vendor_string.length)) |
| 1712 | return false; /* read_callback_ sets the state for us */ |
| 1713 | if(obj->vendor_string.length > 0) { |
| 1714 | if(0 == (obj->vendor_string.entry = (FLAC__byte*)safe_malloc_add_2op_(obj->vendor_string.length, /*+*/1))) { |
| 1715 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 1716 | return false; |
| 1717 | } |
| 1718 | if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->vendor_string.entry, obj->vendor_string.length)) |
| 1719 | return false; /* read_callback_ sets the state for us */ |
| 1720 | obj->vendor_string.entry[obj->vendor_string.length] = '\0'; |
| 1721 | } |
| 1722 | else |
| 1723 | obj->vendor_string.entry = 0; |
| 1724 | |
| 1725 | /* read num comments */ |
| 1726 | FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN == 32); |
| 1727 | if(!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->num_comments)) |
| 1728 | return false; /* read_callback_ sets the state for us */ |
| 1729 | |
| 1730 | /* read comments */ |
| 1731 | if(obj->num_comments > 0) { |
| 1732 | if(0 == (obj->comments = (FLAC__StreamMetadata_VorbisComment_Entry*)safe_malloc_mul_2op_(obj->num_comments, /*times*/sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) { |
| 1733 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 1734 | return false; |
| 1735 | } |
| 1736 | for(i = 0; i < obj->num_comments; i++) { |
| 1737 | FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32); |
| 1738 | if(!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->comments[i].length)) |
| 1739 | return false; /* read_callback_ sets the state for us */ |
| 1740 | if(obj->comments[i].length > 0) { |
| 1741 | if(0 == (obj->comments[i].entry = (FLAC__byte*)safe_malloc_add_2op_(obj->comments[i].length, /*+*/1))) { |
| 1742 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 1743 | return false; |
| 1744 | } |
| 1745 | if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->comments[i].entry, obj->comments[i].length)) |
| 1746 | return false; /* read_callback_ sets the state for us */ |
| 1747 | obj->comments[i].entry[obj->comments[i].length] = '\0'; |
| 1748 | } |
| 1749 | else |
| 1750 | obj->comments[i].entry = 0; |
| 1751 | } |
| 1752 | } |
| 1753 | else { |
| 1754 | obj->comments = 0; |
| 1755 | } |
| 1756 | |
| 1757 | return true; |
| 1758 | } |
| 1759 | |
| 1760 | FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj) |
| 1761 | { |
| 1762 | FLAC__uint32 i, j, x; |
| 1763 | |
| 1764 | FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); |
| 1765 | |
| 1766 | memset(obj, 0, sizeof(FLAC__StreamMetadata_CueSheet)); |
| 1767 | |
| 1768 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0); |
| 1769 | if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->media_catalog_number, FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN/8)) |
| 1770 | return false; /* read_callback_ sets the state for us */ |
| 1771 | |
| 1772 | if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &obj->lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN)) |
| 1773 | return false; /* read_callback_ sets the state for us */ |
| 1774 | |
| 1775 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN)) |
| 1776 | return false; /* read_callback_ sets the state for us */ |
| 1777 | obj->is_cd = x? true : false; |
| 1778 | |
| 1779 | if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN)) |
| 1780 | return false; /* read_callback_ sets the state for us */ |
| 1781 | |
| 1782 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN)) |
| 1783 | return false; /* read_callback_ sets the state for us */ |
| 1784 | obj->num_tracks = x; |
| 1785 | |
| 1786 | if(obj->num_tracks > 0) { |
| 1787 | if(0 == (obj->tracks = (FLAC__StreamMetadata_CueSheet_Track*)safe_calloc_(obj->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track)))) { |
| 1788 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 1789 | return false; |
| 1790 | } |
| 1791 | for(i = 0; i < obj->num_tracks; i++) { |
| 1792 | FLAC__StreamMetadata_CueSheet_Track *track = &obj->tracks[i]; |
| 1793 | if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN)) |
| 1794 | return false; /* read_callback_ sets the state for us */ |
| 1795 | |
| 1796 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN)) |
| 1797 | return false; /* read_callback_ sets the state for us */ |
| 1798 | track->number = (FLAC__byte)x; |
| 1799 | |
| 1800 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0); |
| 1801 | if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)track->isrc, FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN/8)) |
| 1802 | return false; /* read_callback_ sets the state for us */ |
| 1803 | |
| 1804 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN)) |
| 1805 | return false; /* read_callback_ sets the state for us */ |
| 1806 | track->type = x; |
| 1807 | |
| 1808 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN)) |
| 1809 | return false; /* read_callback_ sets the state for us */ |
| 1810 | track->pre_emphasis = x; |
| 1811 | |
| 1812 | if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN)) |
| 1813 | return false; /* read_callback_ sets the state for us */ |
| 1814 | |
| 1815 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN)) |
| 1816 | return false; /* read_callback_ sets the state for us */ |
| 1817 | track->num_indices = (FLAC__byte)x; |
| 1818 | |
| 1819 | if(track->num_indices > 0) { |
| 1820 | if(0 == (track->indices = (FLAC__StreamMetadata_CueSheet_Index*)safe_calloc_(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index)))) { |
| 1821 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 1822 | return false; |
| 1823 | } |
| 1824 | for(j = 0; j < track->num_indices; j++) { |
| 1825 | FLAC__StreamMetadata_CueSheet_Index *index = &track->indices[j]; |
| 1826 | if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &index->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN)) |
| 1827 | return false; /* read_callback_ sets the state for us */ |
| 1828 | |
| 1829 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN)) |
| 1830 | return false; /* read_callback_ sets the state for us */ |
| 1831 | index->number = (FLAC__byte)x; |
| 1832 | |
| 1833 | if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN)) |
| 1834 | return false; /* read_callback_ sets the state for us */ |
| 1835 | } |
| 1836 | } |
| 1837 | } |
| 1838 | } |
| 1839 | |
| 1840 | return true; |
| 1841 | } |
| 1842 | |
| 1843 | FLAC__bool read_metadata_picture_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_Picture *obj) |
| 1844 | { |
| 1845 | FLAC__uint32 x; |
| 1846 | |
| 1847 | FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); |
| 1848 | |
| 1849 | /* read type */ |
| 1850 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_TYPE_LEN)) |
| 1851 | return false; /* read_callback_ sets the state for us */ |
| 1852 | obj->type = x; |
| 1853 | |
| 1854 | /* read MIME type */ |
| 1855 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN)) |
| 1856 | return false; /* read_callback_ sets the state for us */ |
| 1857 | if(0 == (obj->mime_type = (char*)safe_malloc_add_2op_(x, /*+*/1))) { |
| 1858 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 1859 | return false; |
| 1860 | } |
| 1861 | if(x > 0) { |
| 1862 | if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->mime_type, x)) |
| 1863 | return false; /* read_callback_ sets the state for us */ |
| 1864 | } |
| 1865 | obj->mime_type[x] = '\0'; |
| 1866 | |
| 1867 | /* read description */ |
| 1868 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN)) |
| 1869 | return false; /* read_callback_ sets the state for us */ |
| 1870 | if(0 == (obj->description = (FLAC__byte*)safe_malloc_add_2op_(x, /*+*/1))) { |
| 1871 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 1872 | return false; |
| 1873 | } |
| 1874 | if(x > 0) { |
| 1875 | if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->description, x)) |
| 1876 | return false; /* read_callback_ sets the state for us */ |
| 1877 | } |
| 1878 | obj->description[x] = '\0'; |
| 1879 | |
| 1880 | /* read width */ |
| 1881 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->width, FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN)) |
| 1882 | return false; /* read_callback_ sets the state for us */ |
| 1883 | |
| 1884 | /* read height */ |
| 1885 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->height, FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN)) |
| 1886 | return false; /* read_callback_ sets the state for us */ |
| 1887 | |
| 1888 | /* read depth */ |
| 1889 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->depth, FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN)) |
| 1890 | return false; /* read_callback_ sets the state for us */ |
| 1891 | |
| 1892 | /* read colors */ |
| 1893 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->colors, FLAC__STREAM_METADATA_PICTURE_COLORS_LEN)) |
| 1894 | return false; /* read_callback_ sets the state for us */ |
| 1895 | |
| 1896 | /* read data */ |
| 1897 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &(obj->data_length), FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN)) |
| 1898 | return false; /* read_callback_ sets the state for us */ |
| 1899 | if(0 == (obj->data = (FLAC__byte*)safe_malloc_(obj->data_length))) { |
| 1900 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 1901 | return false; |
| 1902 | } |
| 1903 | if(obj->data_length > 0) { |
| 1904 | if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->data, obj->data_length)) |
| 1905 | return false; /* read_callback_ sets the state for us */ |
| 1906 | } |
| 1907 | |
| 1908 | return true; |
| 1909 | } |
| 1910 | |
| 1911 | FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder) |
| 1912 | { |
| 1913 | FLAC__uint32 x; |
| 1914 | unsigned i, skip; |
| 1915 | |
| 1916 | /* skip the version and flags bytes */ |
| 1917 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 24)) |
| 1918 | return false; /* read_callback_ sets the state for us */ |
| 1919 | /* get the size (in bytes) to skip */ |
| 1920 | skip = 0; |
| 1921 | for(i = 0; i < 4; i++) { |
| 1922 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) |
| 1923 | return false; /* read_callback_ sets the state for us */ |
| 1924 | skip <<= 7; |
| 1925 | skip |= (x & 0x7f); |
| 1926 | } |
| 1927 | /* skip the rest of the tag */ |
| 1928 | if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, skip)) |
| 1929 | return false; /* read_callback_ sets the state for us */ |
| 1930 | return true; |
| 1931 | } |
| 1932 | |
| 1933 | FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder) |
| 1934 | { |
| 1935 | FLAC__uint32 x; |
| 1936 | FLAC__bool first = true; |
| 1937 | |
| 1938 | /* If we know the total number of samples in the stream, stop if we've read that many. */ |
| 1939 | /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */ |
| 1940 | if(FLAC__stream_decoder_get_total_samples(decoder) > 0) { |
| 1941 | if(decoder->private_->samples_decoded >= FLAC__stream_decoder_get_total_samples(decoder)) { |
| 1942 | decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM; |
| 1943 | return true; |
| 1944 | } |
| 1945 | } |
| 1946 | |
| 1947 | /* make sure we're byte aligned */ |
| 1948 | if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) { |
| 1949 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__bitreader_bits_left_for_byte_alignment(decoder->private_->input))) |
| 1950 | return false; /* read_callback_ sets the state for us */ |
| 1951 | } |
| 1952 | |
| 1953 | while(1) { |
| 1954 | if(decoder->private_->cached) { |
| 1955 | x = (FLAC__uint32)decoder->private_->lookahead; |
| 1956 | decoder->private_->cached = false; |
| 1957 | } |
| 1958 | else { |
| 1959 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) |
| 1960 | return false; /* read_callback_ sets the state for us */ |
| 1961 | } |
| 1962 | if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */ |
| 1963 | decoder->private_->header_warmup[0] = (FLAC__byte)x; |
| 1964 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) |
| 1965 | return false; /* read_callback_ sets the state for us */ |
| 1966 | |
| 1967 | /* we have to check if we just read two 0xff's in a row; the second may actually be the beginning of the sync code */ |
| 1968 | /* else we have to check if the second byte is the end of a sync code */ |
| 1969 | if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */ |
| 1970 | decoder->private_->lookahead = (FLAC__byte)x; |
| 1971 | decoder->private_->cached = true; |
| 1972 | } |
| 1973 | else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */ |
| 1974 | decoder->private_->header_warmup[1] = (FLAC__byte)x; |
| 1975 | decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME; |
| 1976 | return true; |
| 1977 | } |
| 1978 | } |
| 1979 | if(first) { |
| 1980 | send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC); |
| 1981 | first = false; |
| 1982 | } |
| 1983 | } |
| 1984 | |
| 1985 | return true; |
| 1986 | } |
| 1987 | |
| 1988 | FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode) |
| 1989 | { |
| 1990 | unsigned channel; |
| 1991 | unsigned i; |
| 1992 | FLAC__int32 mid, side; |
| 1993 | unsigned frame_crc; /* the one we calculate from the input stream */ |
| 1994 | FLAC__uint32 x; |
| 1995 | |
| 1996 | *got_a_frame = false; |
| 1997 | |
| 1998 | /* init the CRC */ |
| 1999 | frame_crc = 0; |
| 2000 | frame_crc = FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc); |
| 2001 | frame_crc = FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc); |
| 2002 | FLAC__bitreader_reset_read_crc16(decoder->private_->input, (FLAC__uint16)frame_crc); |
| 2003 | |
| 2004 | if(!read_frame_header_(decoder)) |
| 2005 | return false; |
| 2006 | if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means we didn't sync on a valid header */ |
| 2007 | return true; |
| 2008 | if(!allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels)) |
| 2009 | return false; |
| 2010 | for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) { |
| 2011 | /* |
| 2012 | * first figure the correct bits-per-sample of the subframe |
| 2013 | */ |
| 2014 | unsigned bps = decoder->private_->frame.header.bits_per_sample; |
| 2015 | switch(decoder->private_->frame.header.channel_assignment) { |
| 2016 | case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: |
| 2017 | /* no adjustment needed */ |
| 2018 | break; |
| 2019 | case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: |
| 2020 | FLAC__ASSERT(decoder->private_->frame.header.channels == 2); |
| 2021 | if(channel == 1) |
| 2022 | bps++; |
| 2023 | break; |
| 2024 | case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: |
| 2025 | FLAC__ASSERT(decoder->private_->frame.header.channels == 2); |
| 2026 | if(channel == 0) |
| 2027 | bps++; |
| 2028 | break; |
| 2029 | case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: |
| 2030 | FLAC__ASSERT(decoder->private_->frame.header.channels == 2); |
| 2031 | if(channel == 1) |
| 2032 | bps++; |
| 2033 | break; |
| 2034 | default: |
| 2035 | FLAC__ASSERT(0); |
| 2036 | } |
| 2037 | /* |
| 2038 | * now read it |
| 2039 | */ |
| 2040 | if(!read_subframe_(decoder, channel, bps, do_full_decode)) |
| 2041 | return false; |
| 2042 | if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */ |
| 2043 | return true; |
| 2044 | } |
| 2045 | if(!read_zero_padding_(decoder)) |
| 2046 | return false; |
| 2047 | if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption (i.e. "zero bits" were not all zeroes) */ |
| 2048 | return true; |
| 2049 | |
| 2050 | /* |
| 2051 | * Read the frame CRC-16 from the footer and check |
| 2052 | */ |
| 2053 | frame_crc = FLAC__bitreader_get_read_crc16(decoder->private_->input); |
| 2054 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN)) |
| 2055 | return false; /* read_callback_ sets the state for us */ |
| 2056 | if(frame_crc == x) { |
| 2057 | if(do_full_decode) { |
| 2058 | /* Undo any special channel coding */ |
| 2059 | switch(decoder->private_->frame.header.channel_assignment) { |
| 2060 | case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: |
| 2061 | /* do nothing */ |
| 2062 | break; |
| 2063 | case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: |
| 2064 | FLAC__ASSERT(decoder->private_->frame.header.channels == 2); |
| 2065 | for(i = 0; i < decoder->private_->frame.header.blocksize; i++) |
| 2066 | decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i]; |
| 2067 | break; |
| 2068 | case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: |
| 2069 | FLAC__ASSERT(decoder->private_->frame.header.channels == 2); |
| 2070 | for(i = 0; i < decoder->private_->frame.header.blocksize; i++) |
| 2071 | decoder->private_->output[0][i] += decoder->private_->output[1][i]; |
| 2072 | break; |
| 2073 | case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: |
| 2074 | FLAC__ASSERT(decoder->private_->frame.header.channels == 2); |
| 2075 | for(i = 0; i < decoder->private_->frame.header.blocksize; i++) { |
| 2076 | #if 1 |
| 2077 | mid = decoder->private_->output[0][i]; |
| 2078 | side = decoder->private_->output[1][i]; |
| 2079 | mid <<= 1; |
| 2080 | mid |= (side & 1); /* i.e. if 'side' is odd... */ |
| 2081 | decoder->private_->output[0][i] = (mid + side) >> 1; |
| 2082 | decoder->private_->output[1][i] = (mid - side) >> 1; |
| 2083 | #else |
| 2084 | /* OPT: without 'side' temp variable */ |
| 2085 | mid = (decoder->private_->output[0][i] << 1) | (decoder->private_->output[1][i] & 1); /* i.e. if 'side' is odd... */ |
| 2086 | decoder->private_->output[0][i] = (mid + decoder->private_->output[1][i]) >> 1; |
| 2087 | decoder->private_->output[1][i] = (mid - decoder->private_->output[1][i]) >> 1; |
| 2088 | #endif |
| 2089 | } |
| 2090 | break; |
| 2091 | default: |
| 2092 | FLAC__ASSERT(0); |
| 2093 | break; |
| 2094 | } |
| 2095 | } |
| 2096 | } |
| 2097 | else { |
| 2098 | /* Bad frame, emit error and zero the output signal */ |
| 2099 | send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH); |
| 2100 | if(do_full_decode) { |
| 2101 | for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) { |
| 2102 | memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize); |
| 2103 | } |
| 2104 | } |
| 2105 | } |
| 2106 | |
| 2107 | *got_a_frame = true; |
| 2108 | |
| 2109 | /* we wait to update fixed_block_size until here, when we're sure we've got a proper frame and hence a correct blocksize */ |
| 2110 | if(decoder->private_->next_fixed_block_size) |
| 2111 | decoder->private_->fixed_block_size = decoder->private_->next_fixed_block_size; |
| 2112 | |
| 2113 | /* put the latest values into the public section of the decoder instance */ |
| 2114 | decoder->protected_->channels = decoder->private_->frame.header.channels; |
| 2115 | decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment; |
| 2116 | decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample; |
| 2117 | decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate; |
| 2118 | decoder->protected_->blocksize = decoder->private_->frame.header.blocksize; |
| 2119 | |
| 2120 | FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER); |
| 2121 | decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize; |
| 2122 | |
| 2123 | /* write it */ |
| 2124 | if(do_full_decode) { |
| 2125 | if(write_audio_frame_to_client_(decoder, &decoder->private_->frame, (const FLAC__int32 * const *)decoder->private_->output) != FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE) |
| 2126 | return false; |
| 2127 | } |
| 2128 | |
| 2129 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 2130 | return true; |
| 2131 | } |
| 2132 | |
| 2133 | FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder) |
| 2134 | { |
| 2135 | FLAC__uint32 x; |
| 2136 | FLAC__uint64 xx; |
| 2137 | unsigned i, blocksize_hint = 0, sample_rate_hint = 0; |
| 2138 | FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */ |
| 2139 | unsigned raw_header_len; |
| 2140 | FLAC__bool is_unparseable = false; |
| 2141 | |
| 2142 | FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)); |
| 2143 | |
| 2144 | /* init the raw header with the saved bits from synchronization */ |
| 2145 | raw_header[0] = decoder->private_->header_warmup[0]; |
| 2146 | raw_header[1] = decoder->private_->header_warmup[1]; |
| 2147 | raw_header_len = 2; |
| 2148 | |
| 2149 | /* check to make sure that reserved bit is 0 */ |
| 2150 | if(raw_header[1] & 0x02) /* MAGIC NUMBER */ |
| 2151 | is_unparseable = true; |
| 2152 | |
| 2153 | /* |
| 2154 | * Note that along the way as we read the header, we look for a sync |
| 2155 | * code inside. If we find one it would indicate that our original |
| 2156 | * sync was bad since there cannot be a sync code in a valid header. |
| 2157 | * |
| 2158 | * Three kinds of things can go wrong when reading the frame header: |
| 2159 | * 1) We may have sync'ed incorrectly and not landed on a frame header. |
| 2160 | * If we don't find a sync code, it can end up looking like we read |
| 2161 | * a valid but unparseable header, until getting to the frame header |
| 2162 | * CRC. Even then we could get a false positive on the CRC. |
| 2163 | * 2) We may have sync'ed correctly but on an unparseable frame (from a |
| 2164 | * future encoder). |
| 2165 | * 3) We may be on a damaged frame which appears valid but unparseable. |
| 2166 | * |
| 2167 | * For all these reasons, we try and read a complete frame header as |
| 2168 | * long as it seems valid, even if unparseable, up until the frame |
| 2169 | * header CRC. |
| 2170 | */ |
| 2171 | |
| 2172 | /* |
| 2173 | * read in the raw header as bytes so we can CRC it, and parse it on the way |
| 2174 | */ |
| 2175 | for(i = 0; i < 2; i++) { |
| 2176 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) |
| 2177 | return false; /* read_callback_ sets the state for us */ |
| 2178 | if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */ |
| 2179 | /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */ |
| 2180 | decoder->private_->lookahead = (FLAC__byte)x; |
| 2181 | decoder->private_->cached = true; |
| 2182 | send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER); |
| 2183 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 2184 | return true; |
| 2185 | } |
| 2186 | raw_header[raw_header_len++] = (FLAC__byte)x; |
| 2187 | } |
| 2188 | |
| 2189 | switch(x = raw_header[2] >> 4) { |
| 2190 | case 0: |
| 2191 | is_unparseable = true; |
| 2192 | break; |
| 2193 | case 1: |
| 2194 | decoder->private_->frame.header.blocksize = 192; |
| 2195 | break; |
| 2196 | case 2: |
| 2197 | case 3: |
| 2198 | case 4: |
| 2199 | case 5: |
| 2200 | decoder->private_->frame.header.blocksize = 576 << (x-2); |
| 2201 | break; |
| 2202 | case 6: |
| 2203 | case 7: |
| 2204 | blocksize_hint = x; |
| 2205 | break; |
| 2206 | case 8: |
| 2207 | case 9: |
| 2208 | case 10: |
| 2209 | case 11: |
| 2210 | case 12: |
| 2211 | case 13: |
| 2212 | case 14: |
| 2213 | case 15: |
| 2214 | decoder->private_->frame.header.blocksize = 256 << (x-8); |
| 2215 | break; |
| 2216 | default: |
| 2217 | FLAC__ASSERT(0); |
| 2218 | break; |
| 2219 | } |
| 2220 | |
| 2221 | switch(x = raw_header[2] & 0x0f) { |
| 2222 | case 0: |
| 2223 | if(decoder->private_->has_stream_info) |
| 2224 | decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate; |
| 2225 | else |
| 2226 | is_unparseable = true; |
| 2227 | break; |
| 2228 | case 1: |
| 2229 | decoder->private_->frame.header.sample_rate = 88200; |
| 2230 | break; |
| 2231 | case 2: |
| 2232 | decoder->private_->frame.header.sample_rate = 176400; |
| 2233 | break; |
| 2234 | case 3: |
| 2235 | decoder->private_->frame.header.sample_rate = 192000; |
| 2236 | break; |
| 2237 | case 4: |
| 2238 | decoder->private_->frame.header.sample_rate = 8000; |
| 2239 | break; |
| 2240 | case 5: |
| 2241 | decoder->private_->frame.header.sample_rate = 16000; |
| 2242 | break; |
| 2243 | case 6: |
| 2244 | decoder->private_->frame.header.sample_rate = 22050; |
| 2245 | break; |
| 2246 | case 7: |
| 2247 | decoder->private_->frame.header.sample_rate = 24000; |
| 2248 | break; |
| 2249 | case 8: |
| 2250 | decoder->private_->frame.header.sample_rate = 32000; |
| 2251 | break; |
| 2252 | case 9: |
| 2253 | decoder->private_->frame.header.sample_rate = 44100; |
| 2254 | break; |
| 2255 | case 10: |
| 2256 | decoder->private_->frame.header.sample_rate = 48000; |
| 2257 | break; |
| 2258 | case 11: |
| 2259 | decoder->private_->frame.header.sample_rate = 96000; |
| 2260 | break; |
| 2261 | case 12: |
| 2262 | case 13: |
| 2263 | case 14: |
| 2264 | sample_rate_hint = x; |
| 2265 | break; |
| 2266 | case 15: |
| 2267 | send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER); |
| 2268 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 2269 | return true; |
| 2270 | default: |
| 2271 | FLAC__ASSERT(0); |
| 2272 | } |
| 2273 | |
| 2274 | x = (unsigned)(raw_header[3] >> 4); |
| 2275 | if(x & 8) { |
| 2276 | decoder->private_->frame.header.channels = 2; |
| 2277 | switch(x & 7) { |
| 2278 | case 0: |
| 2279 | decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE; |
| 2280 | break; |
| 2281 | case 1: |
| 2282 | decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE; |
| 2283 | break; |
| 2284 | case 2: |
| 2285 | decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE; |
| 2286 | break; |
| 2287 | default: |
| 2288 | is_unparseable = true; |
| 2289 | break; |
| 2290 | } |
| 2291 | } |
| 2292 | else { |
| 2293 | decoder->private_->frame.header.channels = (unsigned)x + 1; |
| 2294 | decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; |
| 2295 | } |
| 2296 | |
| 2297 | switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) { |
| 2298 | case 0: |
| 2299 | if(decoder->private_->has_stream_info) |
| 2300 | decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample; |
| 2301 | else |
| 2302 | is_unparseable = true; |
| 2303 | break; |
| 2304 | case 1: |
| 2305 | decoder->private_->frame.header.bits_per_sample = 8; |
| 2306 | break; |
| 2307 | case 2: |
| 2308 | decoder->private_->frame.header.bits_per_sample = 12; |
| 2309 | break; |
| 2310 | case 4: |
| 2311 | decoder->private_->frame.header.bits_per_sample = 16; |
| 2312 | break; |
| 2313 | case 5: |
| 2314 | decoder->private_->frame.header.bits_per_sample = 20; |
| 2315 | break; |
| 2316 | case 6: |
| 2317 | decoder->private_->frame.header.bits_per_sample = 24; |
| 2318 | break; |
| 2319 | case 3: |
| 2320 | case 7: |
| 2321 | is_unparseable = true; |
| 2322 | break; |
| 2323 | default: |
| 2324 | FLAC__ASSERT(0); |
| 2325 | break; |
| 2326 | } |
| 2327 | |
| 2328 | /* check to make sure that reserved bit is 0 */ |
| 2329 | if(raw_header[3] & 0x01) /* MAGIC NUMBER */ |
| 2330 | is_unparseable = true; |
| 2331 | |
| 2332 | /* read the frame's starting sample number (or frame number as the case may be) */ |
| 2333 | if( |
| 2334 | raw_header[1] & 0x01 || |
| 2335 | /*@@@ this clause is a concession to the old way of doing variable blocksize; the only known implementation is flake and can probably be removed without inconveniencing anyone */ |
| 2336 | (decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.min_blocksize != decoder->private_->stream_info.data.stream_info.max_blocksize) |
| 2337 | ) { /* variable blocksize */ |
| 2338 | if(!FLAC__bitreader_read_utf8_uint64(decoder->private_->input, &xx, raw_header, &raw_header_len)) |
| 2339 | return false; /* read_callback_ sets the state for us */ |
| 2340 | if(xx == FLAC__U64L(0xffffffffffffffff)) { /* i.e. non-UTF8 code... */ |
| 2341 | decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */ |
| 2342 | decoder->private_->cached = true; |
| 2343 | send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER); |
| 2344 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 2345 | return true; |
| 2346 | } |
| 2347 | decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER; |
| 2348 | decoder->private_->frame.header.number.sample_number = xx; |
| 2349 | } |
| 2350 | else { /* fixed blocksize */ |
| 2351 | if(!FLAC__bitreader_read_utf8_uint32(decoder->private_->input, &x, raw_header, &raw_header_len)) |
| 2352 | return false; /* read_callback_ sets the state for us */ |
| 2353 | if(x == 0xffffffff) { /* i.e. non-UTF8 code... */ |
| 2354 | decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */ |
| 2355 | decoder->private_->cached = true; |
| 2356 | send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER); |
| 2357 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 2358 | return true; |
| 2359 | } |
| 2360 | decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER; |
| 2361 | decoder->private_->frame.header.number.frame_number = x; |
| 2362 | } |
| 2363 | |
| 2364 | if(blocksize_hint) { |
| 2365 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) |
| 2366 | return false; /* read_callback_ sets the state for us */ |
| 2367 | raw_header[raw_header_len++] = (FLAC__byte)x; |
| 2368 | if(blocksize_hint == 7) { |
| 2369 | FLAC__uint32 _x; |
| 2370 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &_x, 8)) |
| 2371 | return false; /* read_callback_ sets the state for us */ |
| 2372 | raw_header[raw_header_len++] = (FLAC__byte)_x; |
| 2373 | x = (x << 8) | _x; |
| 2374 | } |
| 2375 | decoder->private_->frame.header.blocksize = x+1; |
| 2376 | } |
| 2377 | |
| 2378 | if(sample_rate_hint) { |
| 2379 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) |
| 2380 | return false; /* read_callback_ sets the state for us */ |
| 2381 | raw_header[raw_header_len++] = (FLAC__byte)x; |
| 2382 | if(sample_rate_hint != 12) { |
| 2383 | FLAC__uint32 _x; |
| 2384 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &_x, 8)) |
| 2385 | return false; /* read_callback_ sets the state for us */ |
| 2386 | raw_header[raw_header_len++] = (FLAC__byte)_x; |
| 2387 | x = (x << 8) | _x; |
| 2388 | } |
| 2389 | if(sample_rate_hint == 12) |
| 2390 | decoder->private_->frame.header.sample_rate = x*1000; |
| 2391 | else if(sample_rate_hint == 13) |
| 2392 | decoder->private_->frame.header.sample_rate = x; |
| 2393 | else |
| 2394 | decoder->private_->frame.header.sample_rate = x*10; |
| 2395 | } |
| 2396 | |
| 2397 | /* read the CRC-8 byte */ |
| 2398 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) |
| 2399 | return false; /* read_callback_ sets the state for us */ |
| 2400 | crc8 = (FLAC__byte)x; |
| 2401 | |
| 2402 | if(FLAC__crc8(raw_header, raw_header_len) != crc8) { |
| 2403 | send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER); |
| 2404 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 2405 | return true; |
| 2406 | } |
| 2407 | |
| 2408 | /* calculate the sample number from the frame number if needed */ |
| 2409 | decoder->private_->next_fixed_block_size = 0; |
| 2410 | if(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER) { |
| 2411 | x = decoder->private_->frame.header.number.frame_number; |
| 2412 | decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER; |
| 2413 | if(decoder->private_->fixed_block_size) |
| 2414 | decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->fixed_block_size * (FLAC__uint64)x; |
| 2415 | else if(decoder->private_->has_stream_info) { |
| 2416 | if(decoder->private_->stream_info.data.stream_info.min_blocksize == decoder->private_->stream_info.data.stream_info.max_blocksize) { |
| 2417 | decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__uint64)x; |
| 2418 | decoder->private_->next_fixed_block_size = decoder->private_->stream_info.data.stream_info.max_blocksize; |
| 2419 | } |
| 2420 | else |
| 2421 | is_unparseable = true; |
| 2422 | } |
| 2423 | else if(x == 0) { |
| 2424 | decoder->private_->frame.header.number.sample_number = 0; |
| 2425 | decoder->private_->next_fixed_block_size = decoder->private_->frame.header.blocksize; |
| 2426 | } |
| 2427 | else { |
| 2428 | /* can only get here if the stream has invalid frame numbering and no STREAMINFO, so assume it's not the last (possibly short) frame */ |
| 2429 | decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->frame.header.blocksize * (FLAC__uint64)x; |
| 2430 | } |
| 2431 | } |
| 2432 | |
| 2433 | if(is_unparseable) { |
| 2434 | send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM); |
| 2435 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 2436 | return true; |
| 2437 | } |
| 2438 | |
| 2439 | return true; |
| 2440 | } |
| 2441 | |
| 2442 | FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode) |
| 2443 | { |
| 2444 | FLAC__uint32 x; |
| 2445 | FLAC__bool wasted_bits; |
| 2446 | unsigned i; |
| 2447 | |
| 2448 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) /* MAGIC NUMBER */ |
| 2449 | return false; /* read_callback_ sets the state for us */ |
| 2450 | |
| 2451 | wasted_bits = (x & 1); |
| 2452 | x &= 0xfe; |
| 2453 | |
| 2454 | if(wasted_bits) { |
| 2455 | unsigned u; |
| 2456 | if(!FLAC__bitreader_read_unary_unsigned(decoder->private_->input, &u)) |
| 2457 | return false; /* read_callback_ sets the state for us */ |
| 2458 | decoder->private_->frame.subframes[channel].wasted_bits = u+1; |
| 2459 | bps -= decoder->private_->frame.subframes[channel].wasted_bits; |
| 2460 | } |
| 2461 | else |
| 2462 | decoder->private_->frame.subframes[channel].wasted_bits = 0; |
| 2463 | |
| 2464 | /* |
| 2465 | * Lots of magic numbers here |
| 2466 | */ |
| 2467 | if(x & 0x80) { |
| 2468 | send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC); |
| 2469 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 2470 | return true; |
| 2471 | } |
| 2472 | else if(x == 0) { |
| 2473 | if(!read_subframe_constant_(decoder, channel, bps, do_full_decode)) |
| 2474 | return false; |
| 2475 | } |
| 2476 | else if(x == 2) { |
| 2477 | if(!read_subframe_verbatim_(decoder, channel, bps, do_full_decode)) |
| 2478 | return false; |
| 2479 | } |
| 2480 | else if(x < 16) { |
| 2481 | send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM); |
| 2482 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 2483 | return true; |
| 2484 | } |
| 2485 | else if(x <= 24) { |
| 2486 | if(!read_subframe_fixed_(decoder, channel, bps, (x>>1)&7, do_full_decode)) |
| 2487 | return false; |
| 2488 | if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */ |
| 2489 | return true; |
| 2490 | } |
| 2491 | else if(x < 64) { |
| 2492 | send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM); |
| 2493 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 2494 | return true; |
| 2495 | } |
| 2496 | else { |
| 2497 | if(!read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1, do_full_decode)) |
| 2498 | return false; |
| 2499 | if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */ |
| 2500 | return true; |
| 2501 | } |
| 2502 | |
| 2503 | if(wasted_bits && do_full_decode) { |
| 2504 | x = decoder->private_->frame.subframes[channel].wasted_bits; |
| 2505 | for(i = 0; i < decoder->private_->frame.header.blocksize; i++) |
| 2506 | decoder->private_->output[channel][i] <<= x; |
| 2507 | } |
| 2508 | |
| 2509 | return true; |
| 2510 | } |
| 2511 | |
| 2512 | FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode) |
| 2513 | { |
| 2514 | FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant; |
| 2515 | FLAC__int32 x; |
| 2516 | unsigned i; |
| 2517 | FLAC__int32 *output = decoder->private_->output[channel]; |
| 2518 | |
| 2519 | decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT; |
| 2520 | |
| 2521 | if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &x, bps)) |
| 2522 | return false; /* read_callback_ sets the state for us */ |
| 2523 | |
| 2524 | subframe->value = x; |
| 2525 | |
| 2526 | /* decode the subframe */ |
| 2527 | if(do_full_decode) { |
| 2528 | for(i = 0; i < decoder->private_->frame.header.blocksize; i++) |
| 2529 | output[i] = x; |
| 2530 | } |
| 2531 | |
| 2532 | return true; |
| 2533 | } |
| 2534 | |
| 2535 | FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode) |
| 2536 | { |
| 2537 | FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed; |
| 2538 | FLAC__int32 i32; |
| 2539 | FLAC__uint32 u32; |
| 2540 | unsigned u; |
| 2541 | |
| 2542 | decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED; |
| 2543 | |
| 2544 | subframe->residual = decoder->private_->residual[channel]; |
| 2545 | subframe->order = order; |
| 2546 | |
| 2547 | /* read warm-up samples */ |
| 2548 | for(u = 0; u < order; u++) { |
| 2549 | if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, bps)) |
| 2550 | return false; /* read_callback_ sets the state for us */ |
| 2551 | subframe->warmup[u] = i32; |
| 2552 | } |
| 2553 | |
| 2554 | /* read entropy coding method info */ |
| 2555 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN)) |
| 2556 | return false; /* read_callback_ sets the state for us */ |
| 2557 | subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32; |
| 2558 | switch(subframe->entropy_coding_method.type) { |
| 2559 | case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE: |
| 2560 | case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2: |
| 2561 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN)) |
| 2562 | return false; /* read_callback_ sets the state for us */ |
| 2563 | subframe->entropy_coding_method.data.partitioned_rice.order = u32; |
| 2564 | subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel]; |
| 2565 | break; |
| 2566 | default: |
| 2567 | send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM); |
| 2568 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 2569 | return true; |
| 2570 | } |
| 2571 | |
| 2572 | /* read residual */ |
| 2573 | switch(subframe->entropy_coding_method.type) { |
| 2574 | case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE: |
| 2575 | case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2: |
| 2576 | if(!read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, &decoder->private_->partitioned_rice_contents[channel], decoder->private_->residual[channel], /*is_extended=*/subframe->entropy_coding_method.type == FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2)) |
| 2577 | return false; |
| 2578 | break; |
| 2579 | default: |
| 2580 | FLAC__ASSERT(0); |
| 2581 | } |
| 2582 | |
| 2583 | /* decode the subframe */ |
| 2584 | if(do_full_decode) { |
| 2585 | memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order); |
| 2586 | FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order); |
| 2587 | } |
| 2588 | |
| 2589 | return true; |
| 2590 | } |
| 2591 | |
| 2592 | FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode) |
| 2593 | { |
| 2594 | FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc; |
| 2595 | FLAC__int32 i32; |
| 2596 | FLAC__uint32 u32; |
| 2597 | unsigned u; |
| 2598 | |
| 2599 | decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC; |
| 2600 | |
| 2601 | subframe->residual = decoder->private_->residual[channel]; |
| 2602 | subframe->order = order; |
| 2603 | |
| 2604 | /* read warm-up samples */ |
| 2605 | for(u = 0; u < order; u++) { |
| 2606 | if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, bps)) |
| 2607 | return false; /* read_callback_ sets the state for us */ |
| 2608 | subframe->warmup[u] = i32; |
| 2609 | } |
| 2610 | |
| 2611 | /* read qlp coeff precision */ |
| 2612 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN)) |
| 2613 | return false; /* read_callback_ sets the state for us */ |
| 2614 | if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) { |
| 2615 | send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC); |
| 2616 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 2617 | return true; |
| 2618 | } |
| 2619 | subframe->qlp_coeff_precision = u32+1; |
| 2620 | |
| 2621 | /* read qlp shift */ |
| 2622 | if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN)) |
| 2623 | return false; /* read_callback_ sets the state for us */ |
| 2624 | subframe->quantization_level = i32; |
| 2625 | |
| 2626 | /* read quantized lp coefficiencts */ |
| 2627 | for(u = 0; u < order; u++) { |
| 2628 | if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision)) |
| 2629 | return false; /* read_callback_ sets the state for us */ |
| 2630 | subframe->qlp_coeff[u] = i32; |
| 2631 | } |
| 2632 | |
| 2633 | /* read entropy coding method info */ |
| 2634 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN)) |
| 2635 | return false; /* read_callback_ sets the state for us */ |
| 2636 | subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32; |
| 2637 | switch(subframe->entropy_coding_method.type) { |
| 2638 | case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE: |
| 2639 | case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2: |
| 2640 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN)) |
| 2641 | return false; /* read_callback_ sets the state for us */ |
| 2642 | subframe->entropy_coding_method.data.partitioned_rice.order = u32; |
| 2643 | subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel]; |
| 2644 | break; |
| 2645 | default: |
| 2646 | send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM); |
| 2647 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 2648 | return true; |
| 2649 | } |
| 2650 | |
| 2651 | /* read residual */ |
| 2652 | switch(subframe->entropy_coding_method.type) { |
| 2653 | case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE: |
| 2654 | case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2: |
| 2655 | if(!read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, &decoder->private_->partitioned_rice_contents[channel], decoder->private_->residual[channel], /*is_extended=*/subframe->entropy_coding_method.type == FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2)) |
| 2656 | return false; |
| 2657 | break; |
| 2658 | default: |
| 2659 | FLAC__ASSERT(0); |
| 2660 | } |
| 2661 | |
| 2662 | /* decode the subframe */ |
| 2663 | if(do_full_decode) { |
| 2664 | memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order); |
| 2665 | /*@@@@@@ technically not pessimistic enough, should be more like |
| 2666 | if( (FLAC__uint64)order * ((((FLAC__uint64)1)<<bps)-1) * ((1<<subframe->qlp_coeff_precision)-1) < (((FLAC__uint64)-1) << 32) ) |
| 2667 | */ |
| 2668 | if(bps + subframe->qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32) |
| 2669 | if(bps <= 16 && subframe->qlp_coeff_precision <= 16) { |
| 2670 | if(order <= 8) |
| 2671 | decoder->private_->local_lpc_restore_signal_16bit_order8(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order); |
| 2672 | else |
| 2673 | decoder->private_->local_lpc_restore_signal_16bit(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order); |
| 2674 | } |
| 2675 | else |
| 2676 | decoder->private_->local_lpc_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order); |
| 2677 | else |
| 2678 | decoder->private_->local_lpc_restore_signal_64bit(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order); |
| 2679 | } |
| 2680 | |
| 2681 | return true; |
| 2682 | } |
| 2683 | |
| 2684 | FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode) |
| 2685 | { |
| 2686 | FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim; |
| 2687 | FLAC__int32 x, *residual = decoder->private_->residual[channel]; |
| 2688 | unsigned i; |
| 2689 | |
| 2690 | decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM; |
| 2691 | |
| 2692 | subframe->data = residual; |
| 2693 | |
| 2694 | for(i = 0; i < decoder->private_->frame.header.blocksize; i++) { |
| 2695 | if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &x, bps)) |
| 2696 | return false; /* read_callback_ sets the state for us */ |
| 2697 | residual[i] = x; |
| 2698 | } |
| 2699 | |
| 2700 | /* decode the subframe */ |
| 2701 | if(do_full_decode) |
| 2702 | memcpy(decoder->private_->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize); |
| 2703 | |
| 2704 | return true; |
| 2705 | } |
| 2706 | |
| 2707 | FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual, FLAC__bool is_extended) |
| 2708 | { |
| 2709 | FLAC__uint32 rice_parameter; |
| 2710 | int i; |
| 2711 | unsigned partition, sample, u; |
| 2712 | const unsigned partitions = 1u << partition_order; |
| 2713 | const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order; |
| 2714 | const unsigned plen = is_extended? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; |
| 2715 | const unsigned pesc = is_extended? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER; |
| 2716 | |
| 2717 | /* sanity checks */ |
| 2718 | if(partition_order == 0) { |
| 2719 | if(decoder->private_->frame.header.blocksize < predictor_order) { |
| 2720 | send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC); |
| 2721 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 2722 | return true; |
| 2723 | } |
| 2724 | } |
| 2725 | else { |
| 2726 | if(partition_samples < predictor_order) { |
| 2727 | send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC); |
| 2728 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 2729 | return true; |
| 2730 | } |
| 2731 | } |
| 2732 | |
| 2733 | if(!FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order))) { |
| 2734 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 2735 | return false; |
| 2736 | } |
| 2737 | |
| 2738 | sample = 0; |
| 2739 | for(partition = 0; partition < partitions; partition++) { |
| 2740 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &rice_parameter, plen)) |
| 2741 | return false; /* read_callback_ sets the state for us */ |
| 2742 | partitioned_rice_contents->parameters[partition] = rice_parameter; |
| 2743 | if(rice_parameter < pesc) { |
| 2744 | partitioned_rice_contents->raw_bits[partition] = 0; |
| 2745 | u = (partition_order == 0 || partition > 0)? partition_samples : partition_samples - predictor_order; |
| 2746 | if(!decoder->private_->local_bitreader_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter)) |
| 2747 | return false; /* read_callback_ sets the state for us */ |
| 2748 | sample += u; |
| 2749 | } |
| 2750 | else { |
| 2751 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN)) |
| 2752 | return false; /* read_callback_ sets the state for us */ |
| 2753 | partitioned_rice_contents->raw_bits[partition] = rice_parameter; |
| 2754 | for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) { |
| 2755 | if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i, rice_parameter)) |
| 2756 | return false; /* read_callback_ sets the state for us */ |
| 2757 | residual[sample] = i; |
| 2758 | } |
| 2759 | } |
| 2760 | } |
| 2761 | |
| 2762 | return true; |
| 2763 | } |
| 2764 | |
| 2765 | FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder) |
| 2766 | { |
| 2767 | if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) { |
| 2768 | FLAC__uint32 zero = 0; |
| 2769 | if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitreader_bits_left_for_byte_alignment(decoder->private_->input))) |
| 2770 | return false; /* read_callback_ sets the state for us */ |
| 2771 | if(zero != 0) { |
| 2772 | send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC); |
| 2773 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 2774 | } |
| 2775 | } |
| 2776 | return true; |
| 2777 | } |
| 2778 | |
| 2779 | FLAC__bool read_callback_(FLAC__byte buffer[], size_t *bytes, void *client_data) |
| 2780 | { |
| 2781 | FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data; |
| 2782 | |
| 2783 | if( |
| 2784 | #if FLAC__HAS_OGG |
| 2785 | /* see [1] HACK NOTE below for why we don't call the eof_callback when decoding Ogg FLAC */ |
| 2786 | !decoder->private_->is_ogg && |
| 2787 | #endif |
| 2788 | decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data) |
| 2789 | ) { |
| 2790 | *bytes = 0; |
| 2791 | decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM; |
| 2792 | return false; |
| 2793 | } |
| 2794 | else if(*bytes > 0) { |
| 2795 | /* While seeking, it is possible for our seek to land in the |
| 2796 | * middle of audio data that looks exactly like a frame header |
| 2797 | * from a future version of an encoder. When that happens, our |
| 2798 | * error callback will get an |
| 2799 | * FLAC__STREAM_DECODER_UNPARSEABLE_STREAM and increment its |
| 2800 | * unparseable_frame_count. But there is a remote possibility |
| 2801 | * that it is properly synced at such a "future-codec frame", |
| 2802 | * so to make sure, we wait to see many "unparseable" errors in |
| 2803 | * a row before bailing out. |
| 2804 | */ |
| 2805 | if(decoder->private_->is_seeking && decoder->private_->unparseable_frame_count > 20) { |
| 2806 | decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED; |
| 2807 | return false; |
| 2808 | } |
| 2809 | else { |
| 2810 | const FLAC__StreamDecoderReadStatus status = |
| 2811 | #if FLAC__HAS_OGG |
| 2812 | decoder->private_->is_ogg? |
| 2813 | read_callback_ogg_aspect_(decoder, buffer, bytes) : |
| 2814 | #endif |
| 2815 | decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data) |
| 2816 | ; |
| 2817 | if(status == FLAC__STREAM_DECODER_READ_STATUS_ABORT) { |
| 2818 | decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED; |
| 2819 | return false; |
| 2820 | } |
| 2821 | else if(*bytes == 0) { |
| 2822 | if( |
| 2823 | status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM || |
| 2824 | ( |
| 2825 | #if FLAC__HAS_OGG |
| 2826 | /* see [1] HACK NOTE below for why we don't call the eof_callback when decoding Ogg FLAC */ |
| 2827 | !decoder->private_->is_ogg && |
| 2828 | #endif |
| 2829 | decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data) |
| 2830 | ) |
| 2831 | ) { |
| 2832 | decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM; |
| 2833 | return false; |
| 2834 | } |
| 2835 | else |
| 2836 | return true; |
| 2837 | } |
| 2838 | else |
| 2839 | return true; |
| 2840 | } |
| 2841 | } |
| 2842 | else { |
| 2843 | /* abort to avoid a deadlock */ |
| 2844 | decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED; |
| 2845 | return false; |
| 2846 | } |
| 2847 | /* [1] @@@ HACK NOTE: The end-of-stream checking has to be hacked around |
| 2848 | * for Ogg FLAC. This is because the ogg decoder aspect can lose sync |
| 2849 | * and at the same time hit the end of the stream (for example, seeking |
| 2850 | * to a point that is after the beginning of the last Ogg page). There |
| 2851 | * is no way to report an Ogg sync loss through the callbacks (see note |
| 2852 | * in read_callback_ogg_aspect_()) so it returns CONTINUE with *bytes==0. |
| 2853 | * So to keep the decoder from stopping at this point we gate the call |
| 2854 | * to the eof_callback and let the Ogg decoder aspect set the |
| 2855 | * end-of-stream state when it is needed. |
| 2856 | */ |
| 2857 | } |
| 2858 | |
| 2859 | #if FLAC__HAS_OGG |
| 2860 | FLAC__StreamDecoderReadStatus read_callback_ogg_aspect_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes) |
| 2861 | { |
| 2862 | switch(FLAC__ogg_decoder_aspect_read_callback_wrapper(&decoder->protected_->ogg_decoder_aspect, buffer, bytes, read_callback_proxy_, decoder, decoder->private_->client_data)) { |
| 2863 | case FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK: |
| 2864 | return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; |
| 2865 | /* we don't really have a way to handle lost sync via read |
| 2866 | * callback so we'll let it pass and let the underlying |
| 2867 | * FLAC decoder catch the error |
| 2868 | */ |
| 2869 | case FLAC__OGG_DECODER_ASPECT_READ_STATUS_LOST_SYNC: |
| 2870 | return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; |
| 2871 | case FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM: |
| 2872 | return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; |
| 2873 | case FLAC__OGG_DECODER_ASPECT_READ_STATUS_NOT_FLAC: |
| 2874 | case FLAC__OGG_DECODER_ASPECT_READ_STATUS_UNSUPPORTED_MAPPING_VERSION: |
| 2875 | case FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT: |
| 2876 | case FLAC__OGG_DECODER_ASPECT_READ_STATUS_ERROR: |
| 2877 | case FLAC__OGG_DECODER_ASPECT_READ_STATUS_MEMORY_ALLOCATION_ERROR: |
| 2878 | return FLAC__STREAM_DECODER_READ_STATUS_ABORT; |
| 2879 | default: |
| 2880 | FLAC__ASSERT(0); |
| 2881 | /* double protection */ |
| 2882 | return FLAC__STREAM_DECODER_READ_STATUS_ABORT; |
| 2883 | } |
| 2884 | } |
| 2885 | |
| 2886 | FLAC__OggDecoderAspectReadStatus read_callback_proxy_(const void *void_decoder, FLAC__byte buffer[], size_t *bytes, void *client_data) |
| 2887 | { |
| 2888 | FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder*)void_decoder; |
| 2889 | |
| 2890 | switch(decoder->private_->read_callback(decoder, buffer, bytes, client_data)) { |
| 2891 | case FLAC__STREAM_DECODER_READ_STATUS_CONTINUE: |
| 2892 | return FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK; |
| 2893 | case FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM: |
| 2894 | return FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM; |
| 2895 | case FLAC__STREAM_DECODER_READ_STATUS_ABORT: |
| 2896 | return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT; |
| 2897 | default: |
| 2898 | /* double protection: */ |
| 2899 | FLAC__ASSERT(0); |
| 2900 | return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT; |
| 2901 | } |
| 2902 | } |
| 2903 | #endif |
| 2904 | |
| 2905 | FLAC__StreamDecoderWriteStatus write_audio_frame_to_client_(FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[]) |
| 2906 | { |
| 2907 | if(decoder->private_->is_seeking) { |
| 2908 | FLAC__uint64 this_frame_sample = frame->header.number.sample_number; |
| 2909 | FLAC__uint64 next_frame_sample = this_frame_sample + (FLAC__uint64)frame->header.blocksize; |
| 2910 | FLAC__uint64 target_sample = decoder->private_->target_sample; |
| 2911 | |
| 2912 | FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER); |
| 2913 | |
| 2914 | #if FLAC__HAS_OGG |
| 2915 | decoder->private_->got_a_frame = true; |
| 2916 | #endif |
| 2917 | decoder->private_->last_frame = *frame; /* save the frame */ |
| 2918 | if(this_frame_sample <= target_sample && target_sample < next_frame_sample) { /* we hit our target frame */ |
| 2919 | unsigned delta = (unsigned)(target_sample - this_frame_sample); |
| 2920 | /* kick out of seek mode */ |
| 2921 | decoder->private_->is_seeking = false; |
| 2922 | /* shift out the samples before target_sample */ |
| 2923 | if(delta > 0) { |
| 2924 | unsigned channel; |
| 2925 | const FLAC__int32 *newbuffer[FLAC__MAX_CHANNELS]; |
| 2926 | for(channel = 0; channel < frame->header.channels; channel++) |
| 2927 | newbuffer[channel] = buffer[channel] + delta; |
| 2928 | decoder->private_->last_frame.header.blocksize -= delta; |
| 2929 | decoder->private_->last_frame.header.number.sample_number += (FLAC__uint64)delta; |
| 2930 | /* write the relevant samples */ |
| 2931 | return decoder->private_->write_callback(decoder, &decoder->private_->last_frame, newbuffer, decoder->private_->client_data); |
| 2932 | } |
| 2933 | else { |
| 2934 | /* write the relevant samples */ |
| 2935 | return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data); |
| 2936 | } |
| 2937 | } |
| 2938 | else { |
| 2939 | return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; |
| 2940 | } |
| 2941 | } |
| 2942 | else { |
| 2943 | /* |
| 2944 | * If we never got STREAMINFO, turn off MD5 checking to save |
| 2945 | * cycles since we don't have a sum to compare to anyway |
| 2946 | */ |
| 2947 | if(!decoder->private_->has_stream_info) |
| 2948 | decoder->private_->do_md5_checking = false; |
| 2949 | if(decoder->private_->do_md5_checking) { |
| 2950 | if(!FLAC__MD5Accumulate(&decoder->private_->md5context, buffer, frame->header.channels, frame->header.blocksize, (frame->header.bits_per_sample+7) / 8)) |
| 2951 | return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; |
| 2952 | } |
| 2953 | return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data); |
| 2954 | } |
| 2955 | } |
| 2956 | |
| 2957 | void send_error_to_client_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status) |
| 2958 | { |
| 2959 | if(!decoder->private_->is_seeking) |
| 2960 | decoder->private_->error_callback(decoder, status, decoder->private_->client_data); |
| 2961 | else if(status == FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM) |
| 2962 | decoder->private_->unparseable_frame_count++; |
| 2963 | } |
| 2964 | |
| 2965 | FLAC__bool seek_to_absolute_sample_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample) |
| 2966 | { |
| 2967 | FLAC__uint64 first_frame_offset = decoder->private_->first_frame_offset, lower_bound, upper_bound, lower_bound_sample, upper_bound_sample, this_frame_sample; |
| 2968 | FLAC__int64 pos = -1; |
| 2969 | int i; |
| 2970 | unsigned approx_bytes_per_frame; |
| 2971 | FLAC__bool first_seek = true; |
| 2972 | const FLAC__uint64 total_samples = FLAC__stream_decoder_get_total_samples(decoder); |
| 2973 | const unsigned min_blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize; |
| 2974 | const unsigned max_blocksize = decoder->private_->stream_info.data.stream_info.max_blocksize; |
| 2975 | const unsigned max_framesize = decoder->private_->stream_info.data.stream_info.max_framesize; |
| 2976 | const unsigned min_framesize = decoder->private_->stream_info.data.stream_info.min_framesize; |
| 2977 | /* take these from the current frame in case they've changed mid-stream */ |
| 2978 | unsigned channels = FLAC__stream_decoder_get_channels(decoder); |
| 2979 | unsigned bps = FLAC__stream_decoder_get_bits_per_sample(decoder); |
| 2980 | const FLAC__StreamMetadata_SeekTable *seek_table = decoder->private_->has_seek_table? &decoder->private_->seek_table.data.seek_table : 0; |
| 2981 | |
| 2982 | /* use values from stream info if we didn't decode a frame */ |
| 2983 | if(channels == 0) |
| 2984 | channels = decoder->private_->stream_info.data.stream_info.channels; |
| 2985 | if(bps == 0) |
| 2986 | bps = decoder->private_->stream_info.data.stream_info.bits_per_sample; |
| 2987 | |
| 2988 | /* we are just guessing here */ |
| 2989 | if(max_framesize > 0) |
| 2990 | approx_bytes_per_frame = (max_framesize + min_framesize) / 2 + 1; |
| 2991 | /* |
| 2992 | * Check if it's a known fixed-blocksize stream. Note that though |
| 2993 | * the spec doesn't allow zeroes in the STREAMINFO block, we may |
| 2994 | * never get a STREAMINFO block when decoding so the value of |
| 2995 | * min_blocksize might be zero. |
| 2996 | */ |
| 2997 | else if(min_blocksize == max_blocksize && min_blocksize > 0) { |
| 2998 | /* note there are no () around 'bps/8' to keep precision up since it's an integer calulation */ |
| 2999 | approx_bytes_per_frame = min_blocksize * channels * bps/8 + 64; |
| 3000 | } |
| 3001 | else |
| 3002 | approx_bytes_per_frame = 4096 * channels * bps/8 + 64; |
| 3003 | |
| 3004 | /* |
| 3005 | * First, we set an upper and lower bound on where in the |
| 3006 | * stream we will search. For now we assume the worst case |
| 3007 | * scenario, which is our best guess at the beginning of |
| 3008 | * the first frame and end of the stream. |
| 3009 | */ |
| 3010 | lower_bound = first_frame_offset; |
| 3011 | lower_bound_sample = 0; |
| 3012 | upper_bound = stream_length; |
| 3013 | upper_bound_sample = total_samples > 0 ? total_samples : target_sample /*estimate it*/; |
| 3014 | |
| 3015 | /* |
| 3016 | * Now we refine the bounds if we have a seektable with |
| 3017 | * suitable points. Note that according to the spec they |
| 3018 | * must be ordered by ascending sample number. |
| 3019 | * |
| 3020 | * Note: to protect against invalid seek tables we will ignore points |
| 3021 | * that have frame_samples==0 or sample_number>=total_samples |
| 3022 | */ |
| 3023 | if(seek_table) { |
| 3024 | FLAC__uint64 new_lower_bound = lower_bound; |
| 3025 | FLAC__uint64 new_upper_bound = upper_bound; |
| 3026 | FLAC__uint64 new_lower_bound_sample = lower_bound_sample; |
| 3027 | FLAC__uint64 new_upper_bound_sample = upper_bound_sample; |
| 3028 | |
| 3029 | /* find the closest seek point <= target_sample, if it exists */ |
| 3030 | for(i = (int)seek_table->num_points - 1; i >= 0; i--) { |
| 3031 | if( |
| 3032 | seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && |
| 3033 | seek_table->points[i].frame_samples > 0 && /* defense against bad seekpoints */ |
| 3034 | (total_samples <= 0 || seek_table->points[i].sample_number < total_samples) && /* defense against bad seekpoints */ |
| 3035 | seek_table->points[i].sample_number <= target_sample |
| 3036 | ) |
| 3037 | break; |
| 3038 | } |
| 3039 | if(i >= 0) { /* i.e. we found a suitable seek point... */ |
| 3040 | new_lower_bound = first_frame_offset + seek_table->points[i].stream_offset; |
| 3041 | new_lower_bound_sample = seek_table->points[i].sample_number; |
| 3042 | } |
| 3043 | |
| 3044 | /* find the closest seek point > target_sample, if it exists */ |
| 3045 | for(i = 0; i < (int)seek_table->num_points; i++) { |
| 3046 | if( |
| 3047 | seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && |
| 3048 | seek_table->points[i].frame_samples > 0 && /* defense against bad seekpoints */ |
| 3049 | (total_samples <= 0 || seek_table->points[i].sample_number < total_samples) && /* defense against bad seekpoints */ |
| 3050 | seek_table->points[i].sample_number > target_sample |
| 3051 | ) |
| 3052 | break; |
| 3053 | } |
| 3054 | if(i < (int)seek_table->num_points) { /* i.e. we found a suitable seek point... */ |
| 3055 | new_upper_bound = first_frame_offset + seek_table->points[i].stream_offset; |
| 3056 | new_upper_bound_sample = seek_table->points[i].sample_number; |
| 3057 | } |
| 3058 | /* final protection against unsorted seek tables; keep original values if bogus */ |
| 3059 | if(new_upper_bound >= new_lower_bound) { |
| 3060 | lower_bound = new_lower_bound; |
| 3061 | upper_bound = new_upper_bound; |
| 3062 | lower_bound_sample = new_lower_bound_sample; |
| 3063 | upper_bound_sample = new_upper_bound_sample; |
| 3064 | } |
| 3065 | } |
| 3066 | |
| 3067 | FLAC__ASSERT(upper_bound_sample >= lower_bound_sample); |
| 3068 | /* there are 2 insidious ways that the following equality occurs, which |
| 3069 | * we need to fix: |
| 3070 | * 1) total_samples is 0 (unknown) and target_sample is 0 |
| 3071 | * 2) total_samples is 0 (unknown) and target_sample happens to be |
| 3072 | * exactly equal to the last seek point in the seek table; this |
| 3073 | * means there is no seek point above it, and upper_bound_samples |
| 3074 | * remains equal to the estimate (of target_samples) we made above |
| 3075 | * in either case it does not hurt to move upper_bound_sample up by 1 |
| 3076 | */ |
| 3077 | if(upper_bound_sample == lower_bound_sample) |
| 3078 | upper_bound_sample++; |
| 3079 | |
| 3080 | decoder->private_->target_sample = target_sample; |
| 3081 | while(1) { |
| 3082 | /* check if the bounds are still ok */ |
| 3083 | if (lower_bound_sample >= upper_bound_sample || lower_bound > upper_bound) { |
| 3084 | decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 3085 | return false; |
| 3086 | } |
| 3087 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 3088 | #if defined _MSC_VER || defined __MINGW32__ |
| 3089 | /* with VC++ you have to spoon feed it the casting */ |
| 3090 | pos = (FLAC__int64)lower_bound + (FLAC__int64)((FLAC__double)(FLAC__int64)(target_sample - lower_bound_sample) / (FLAC__double)(FLAC__int64)(upper_bound_sample - lower_bound_sample) * (FLAC__double)(FLAC__int64)(upper_bound - lower_bound)) - approx_bytes_per_frame; |
| 3091 | #else |
| 3092 | pos = (FLAC__int64)lower_bound + (FLAC__int64)((FLAC__double)(target_sample - lower_bound_sample) / (FLAC__double)(upper_bound_sample - lower_bound_sample) * (FLAC__double)(upper_bound - lower_bound)) - approx_bytes_per_frame; |
| 3093 | #endif |
| 3094 | #else |
| 3095 | /* a little less accurate: */ |
| 3096 | if(upper_bound - lower_bound < 0xffffffff) |
| 3097 | pos = (FLAC__int64)lower_bound + (FLAC__int64)(((target_sample - lower_bound_sample) * (upper_bound - lower_bound)) / (upper_bound_sample - lower_bound_sample)) - approx_bytes_per_frame; |
| 3098 | else /* @@@ WATCHOUT, ~2TB limit */ |
| 3099 | pos = (FLAC__int64)lower_bound + (FLAC__int64)((((target_sample - lower_bound_sample)>>8) * ((upper_bound - lower_bound)>>8)) / ((upper_bound_sample - lower_bound_sample)>>16)) - approx_bytes_per_frame; |
| 3100 | #endif |
| 3101 | if(pos >= (FLAC__int64)upper_bound) |
| 3102 | pos = (FLAC__int64)upper_bound - 1; |
| 3103 | if(pos < (FLAC__int64)lower_bound) |
| 3104 | pos = (FLAC__int64)lower_bound; |
| 3105 | if(decoder->private_->seek_callback(decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) { |
| 3106 | decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 3107 | return false; |
| 3108 | } |
| 3109 | if(!FLAC__stream_decoder_flush(decoder)) { |
| 3110 | /* above call sets the state for us */ |
| 3111 | return false; |
| 3112 | } |
| 3113 | /* Now we need to get a frame. First we need to reset our |
| 3114 | * unparseable_frame_count; if we get too many unparseable |
| 3115 | * frames in a row, the read callback will return |
| 3116 | * FLAC__STREAM_DECODER_READ_STATUS_ABORT, causing |
| 3117 | * FLAC__stream_decoder_process_single() to return false. |
| 3118 | */ |
| 3119 | decoder->private_->unparseable_frame_count = 0; |
| 3120 | if(!FLAC__stream_decoder_process_single(decoder)) { |
| 3121 | decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 3122 | return false; |
| 3123 | } |
| 3124 | /* our write callback will change the state when it gets to the target frame */ |
| 3125 | /* actually, we could have got_a_frame if our decoder is at FLAC__STREAM_DECODER_END_OF_STREAM so we need to check for that also */ |
| 3126 | #if 0 |
| 3127 | /*@@@@@@ used to be the following; not clear if the check for end of stream is needed anymore */ |
| 3128 | if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING && decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM) |
| 3129 | break; |
| 3130 | #endif |
| 3131 | if(!decoder->private_->is_seeking) |
| 3132 | break; |
| 3133 | |
| 3134 | FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER); |
| 3135 | this_frame_sample = decoder->private_->last_frame.header.number.sample_number; |
| 3136 | |
| 3137 | if (0 == decoder->private_->samples_decoded || (this_frame_sample + decoder->private_->last_frame.header.blocksize >= upper_bound_sample && !first_seek)) { |
| 3138 | if (pos == (FLAC__int64)lower_bound) { |
| 3139 | /* can't move back any more than the first frame, something is fatally wrong */ |
| 3140 | decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 3141 | return false; |
| 3142 | } |
| 3143 | /* our last move backwards wasn't big enough, try again */ |
| 3144 | approx_bytes_per_frame = approx_bytes_per_frame? approx_bytes_per_frame * 2 : 16; |
| 3145 | continue; |
| 3146 | } |
| 3147 | /* allow one seek over upper bound, so we can get a correct upper_bound_sample for streams with unknown total_samples */ |
| 3148 | first_seek = false; |
| 3149 | |
| 3150 | /* make sure we are not seeking in corrupted stream */ |
| 3151 | if (this_frame_sample < lower_bound_sample) { |
| 3152 | decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 3153 | return false; |
| 3154 | } |
| 3155 | |
| 3156 | /* we need to narrow the search */ |
| 3157 | if(target_sample < this_frame_sample) { |
| 3158 | upper_bound_sample = this_frame_sample + decoder->private_->last_frame.header.blocksize; |
| 3159 | /*@@@@@@ what will decode position be if at end of stream? */ |
| 3160 | if(!FLAC__stream_decoder_get_decode_position(decoder, &upper_bound)) { |
| 3161 | decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 3162 | return false; |
| 3163 | } |
| 3164 | approx_bytes_per_frame = (unsigned)(2 * (upper_bound - pos) / 3 + 16); |
| 3165 | } |
| 3166 | else { /* target_sample >= this_frame_sample + this frame's blocksize */ |
| 3167 | lower_bound_sample = this_frame_sample + decoder->private_->last_frame.header.blocksize; |
| 3168 | if(!FLAC__stream_decoder_get_decode_position(decoder, &lower_bound)) { |
| 3169 | decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 3170 | return false; |
| 3171 | } |
| 3172 | approx_bytes_per_frame = (unsigned)(2 * (lower_bound - pos) / 3 + 16); |
| 3173 | } |
| 3174 | } |
| 3175 | |
| 3176 | return true; |
| 3177 | } |
| 3178 | |
| 3179 | #if FLAC__HAS_OGG |
| 3180 | FLAC__bool seek_to_absolute_sample_ogg_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample) |
| 3181 | { |
| 3182 | FLAC__uint64 left_pos = 0, right_pos = stream_length; |
| 3183 | FLAC__uint64 left_sample = 0, right_sample = FLAC__stream_decoder_get_total_samples(decoder); |
| 3184 | FLAC__uint64 this_frame_sample = (FLAC__uint64)0 - 1; |
| 3185 | FLAC__uint64 pos = 0; /* only initialized to avoid compiler warning */ |
| 3186 | FLAC__bool did_a_seek; |
| 3187 | unsigned iteration = 0; |
| 3188 | |
| 3189 | /* In the first iterations, we will calculate the target byte position |
| 3190 | * by the distance from the target sample to left_sample and |
| 3191 | * right_sample (let's call it "proportional search"). After that, we |
| 3192 | * will switch to binary search. |
| 3193 | */ |
| 3194 | unsigned BINARY_SEARCH_AFTER_ITERATION = 2; |
| 3195 | |
| 3196 | /* We will switch to a linear search once our current sample is less |
| 3197 | * than this number of samples ahead of the target sample |
| 3198 | */ |
| 3199 | static const FLAC__uint64 LINEAR_SEARCH_WITHIN_SAMPLES = FLAC__MAX_BLOCK_SIZE * 2; |
| 3200 | |
| 3201 | /* If the total number of samples is unknown, use a large value, and |
| 3202 | * force binary search immediately. |
| 3203 | */ |
| 3204 | if(right_sample == 0) { |
| 3205 | right_sample = (FLAC__uint64)(-1); |
| 3206 | BINARY_SEARCH_AFTER_ITERATION = 0; |
| 3207 | } |
| 3208 | |
| 3209 | decoder->private_->target_sample = target_sample; |
| 3210 | for( ; ; iteration++) { |
| 3211 | if (iteration == 0 || this_frame_sample > target_sample || target_sample - this_frame_sample > LINEAR_SEARCH_WITHIN_SAMPLES) { |
| 3212 | if (iteration >= BINARY_SEARCH_AFTER_ITERATION) { |
| 3213 | pos = (right_pos + left_pos) / 2; |
| 3214 | } |
| 3215 | else { |
| 3216 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 3217 | #if defined _MSC_VER || defined __MINGW32__ |
| 3218 | /* with MSVC you have to spoon feed it the casting */ |
| 3219 | pos = (FLAC__uint64)((FLAC__double)(FLAC__int64)(target_sample - left_sample) / (FLAC__double)(FLAC__int64)(right_sample - left_sample) * (FLAC__double)(FLAC__int64)(right_pos - left_pos)); |
| 3220 | #else |
| 3221 | pos = (FLAC__uint64)((FLAC__double)(target_sample - left_sample) / (FLAC__double)(right_sample - left_sample) * (FLAC__double)(right_pos - left_pos)); |
| 3222 | #endif |
| 3223 | #else |
| 3224 | /* a little less accurate: */ |
| 3225 | if ((target_sample-left_sample <= 0xffffffff) && (right_pos-left_pos <= 0xffffffff)) |
| 3226 | pos = (FLAC__int64)(((target_sample-left_sample) * (right_pos-left_pos)) / (right_sample-left_sample)); |
| 3227 | else /* @@@ WATCHOUT, ~2TB limit */ |
| 3228 | pos = (FLAC__int64)((((target_sample-left_sample)>>8) * ((right_pos-left_pos)>>8)) / ((right_sample-left_sample)>>16)); |
| 3229 | #endif |
| 3230 | /* @@@ TODO: might want to limit pos to some distance |
| 3231 | * before EOF, to make sure we land before the last frame, |
| 3232 | * thereby getting a this_frame_sample and so having a better |
| 3233 | * estimate. |
| 3234 | */ |
| 3235 | } |
| 3236 | |
| 3237 | /* physical seek */ |
| 3238 | if(decoder->private_->seek_callback((FLAC__StreamDecoder*)decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) { |
| 3239 | decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 3240 | return false; |
| 3241 | } |
| 3242 | if(!FLAC__stream_decoder_flush(decoder)) { |
| 3243 | /* above call sets the state for us */ |
| 3244 | return false; |
| 3245 | } |
| 3246 | did_a_seek = true; |
| 3247 | } |
| 3248 | else |
| 3249 | did_a_seek = false; |
| 3250 | |
| 3251 | decoder->private_->got_a_frame = false; |
| 3252 | if(!FLAC__stream_decoder_process_single(decoder)) { |
| 3253 | decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 3254 | return false; |
| 3255 | } |
| 3256 | if(!decoder->private_->got_a_frame) { |
| 3257 | if(did_a_seek) { |
| 3258 | /* this can happen if we seek to a point after the last frame; we drop |
| 3259 | * to binary search right away in this case to avoid any wasted |
| 3260 | * iterations of proportional search. |
| 3261 | */ |
| 3262 | right_pos = pos; |
| 3263 | BINARY_SEARCH_AFTER_ITERATION = 0; |
| 3264 | } |
| 3265 | else { |
| 3266 | /* this can probably only happen if total_samples is unknown and the |
| 3267 | * target_sample is past the end of the stream |
| 3268 | */ |
| 3269 | decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 3270 | return false; |
| 3271 | } |
| 3272 | } |
| 3273 | /* our write callback will change the state when it gets to the target frame */ |
| 3274 | else if(!decoder->private_->is_seeking) { |
| 3275 | break; |
| 3276 | } |
| 3277 | else { |
| 3278 | this_frame_sample = decoder->private_->last_frame.header.number.sample_number; |
| 3279 | FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER); |
| 3280 | |
| 3281 | if (did_a_seek) { |
| 3282 | if (this_frame_sample <= target_sample) { |
| 3283 | /* The 'equal' case should not happen, since |
| 3284 | * FLAC__stream_decoder_process_single() |
| 3285 | * should recognize that it has hit the |
| 3286 | * target sample and we would exit through |
| 3287 | * the 'break' above. |
| 3288 | */ |
| 3289 | FLAC__ASSERT(this_frame_sample != target_sample); |
| 3290 | |
| 3291 | left_sample = this_frame_sample; |
| 3292 | /* sanity check to avoid infinite loop */ |
| 3293 | if (left_pos == pos) { |
| 3294 | decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 3295 | return false; |
| 3296 | } |
| 3297 | left_pos = pos; |
| 3298 | } |
| 3299 | else if(this_frame_sample > target_sample) { |
| 3300 | right_sample = this_frame_sample; |
| 3301 | /* sanity check to avoid infinite loop */ |
| 3302 | if (right_pos == pos) { |
| 3303 | decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR; |
| 3304 | return false; |
| 3305 | } |
| 3306 | right_pos = pos; |
| 3307 | } |
| 3308 | } |
| 3309 | } |
| 3310 | } |
| 3311 | |
| 3312 | return true; |
| 3313 | } |
| 3314 | #endif |
| 3315 | |
| 3316 | FLAC__StreamDecoderReadStatus file_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data) |
| 3317 | { |
| 3318 | (void)client_data; |
| 3319 | |
| 3320 | if(*bytes > 0) { |
| 3321 | *bytes = fread(buffer, sizeof(FLAC__byte), *bytes, decoder->private_->file); |
| 3322 | if(ferror(decoder->private_->file)) |
| 3323 | return FLAC__STREAM_DECODER_READ_STATUS_ABORT; |
| 3324 | else if(*bytes == 0) |
| 3325 | return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; |
| 3326 | else |
| 3327 | return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; |
| 3328 | } |
| 3329 | else |
| 3330 | return FLAC__STREAM_DECODER_READ_STATUS_ABORT; /* abort to avoid a deadlock */ |
| 3331 | } |
| 3332 | |
| 3333 | FLAC__StreamDecoderSeekStatus file_seek_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data) |
| 3334 | { |
| 3335 | (void)client_data; |
| 3336 | |
| 3337 | if(decoder->private_->file == stdin) |
| 3338 | return FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED; |
| 3339 | else if(fseeko(decoder->private_->file, (off_t)absolute_byte_offset, SEEK_SET) < 0) |
| 3340 | return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR; |
| 3341 | else |
| 3342 | return FLAC__STREAM_DECODER_SEEK_STATUS_OK; |
| 3343 | } |
| 3344 | |
| 3345 | FLAC__StreamDecoderTellStatus file_tell_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data) |
| 3346 | { |
| 3347 | off_t pos; |
| 3348 | (void)client_data; |
| 3349 | |
| 3350 | if(decoder->private_->file == stdin) |
| 3351 | return FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED; |
| 3352 | else if((pos = ftello(decoder->private_->file)) < 0) |
| 3353 | return FLAC__STREAM_DECODER_TELL_STATUS_ERROR; |
| 3354 | else { |
| 3355 | *absolute_byte_offset = (FLAC__uint64)pos; |
| 3356 | return FLAC__STREAM_DECODER_TELL_STATUS_OK; |
| 3357 | } |
| 3358 | } |
| 3359 | |
| 3360 | FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data) |
| 3361 | { |
| 3362 | struct stat filestats; |
| 3363 | (void)client_data; |
| 3364 | |
| 3365 | if(decoder->private_->file == stdin) |
| 3366 | return FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED; |
| 3367 | else if(fstat(fileno(decoder->private_->file), &filestats) != 0) |
| 3368 | return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR; |
| 3369 | else { |
| 3370 | *stream_length = (FLAC__uint64)filestats.st_size; |
| 3371 | return FLAC__STREAM_DECODER_LENGTH_STATUS_OK; |
| 3372 | } |
| 3373 | } |
| 3374 | |
| 3375 | FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data) |
| 3376 | { |
| 3377 | (void)client_data; |
| 3378 | |
| 3379 | return feof(decoder->private_->file)? true : false; |
| 3380 | } |
src/lib/libflac/libflac/stream_encoder.c
| r0 | r14135 | |
| 1 | /* libFLAC - Free Lossless Audio Codec library |
| 2 | * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * |
| 8 | * - Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * - Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * - Neither the name of the Xiph.org Foundation nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived from |
| 17 | * this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 26 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 27 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #if HAVE_CONFIG_H |
| 33 | # include <config.h> |
| 34 | #endif |
| 35 | |
| 36 | #include <limits.h> |
| 37 | #include <stdio.h> |
| 38 | #include <stdlib.h> /* for malloc() */ |
| 39 | #include <string.h> /* for memcpy() */ |
| 40 | #include <sys/types.h> /* for off_t */ |
| 41 | #if defined _MSC_VER || defined __BORLANDC__ || defined __MINGW32__ |
| 42 | #if _MSC_VER <= 1600 || defined __BORLANDC__ /* @@@ [2G limit] */ |
| 43 | #define fseeko fseek |
| 44 | #define ftello ftell |
| 45 | #endif |
| 46 | #endif |
| 47 | #include "flac/assert.h" |
| 48 | #include "flac/stream_decoder.h" |
| 49 | #include "share/alloc.h" |
| 50 | #include "protected/stream_encoder.h" |
| 51 | #include "private/bitwriter.h" |
| 52 | #include "private/bitmath.h" |
| 53 | #include "private/crc.h" |
| 54 | #include "private/cpu.h" |
| 55 | #include "private/fixed.h" |
| 56 | #include "private/format.h" |
| 57 | #include "private/lpc.h" |
| 58 | #include "private/md5.h" |
| 59 | #include "private/memory.h" |
| 60 | #if FLAC__HAS_OGG |
| 61 | #include "private/ogg_helper.h" |
| 62 | #include "private/ogg_mapping.h" |
| 63 | #endif |
| 64 | #include "private/stream_encoder_framing.h" |
| 65 | #include "private/window.h" |
| 66 | |
| 67 | #ifndef FLaC__INLINE |
| 68 | #define FLaC__INLINE |
| 69 | #endif |
| 70 | |
| 71 | #ifdef min |
| 72 | #undef min |
| 73 | #endif |
| 74 | #define min(x,y) ((x)<(y)?(x):(y)) |
| 75 | |
| 76 | #ifdef max |
| 77 | #undef max |
| 78 | #endif |
| 79 | #define max(x,y) ((x)>(y)?(x):(y)) |
| 80 | |
| 81 | /* Exact Rice codeword length calculation is off by default. The simple |
| 82 | * (and fast) estimation (of how many bits a residual value will be |
| 83 | * encoded with) in this encoder is very good, almost always yielding |
| 84 | * compression within 0.1% of exact calculation. |
| 85 | */ |
| 86 | #undef EXACT_RICE_BITS_CALCULATION |
| 87 | /* Rice parameter searching is off by default. The simple (and fast) |
| 88 | * parameter estimation in this encoder is very good, almost always |
| 89 | * yielding compression within 0.1% of the optimal parameters. |
| 90 | */ |
| 91 | #undef ENABLE_RICE_PARAMETER_SEARCH |
| 92 | |
| 93 | |
| 94 | typedef struct { |
| 95 | FLAC__int32 *data[FLAC__MAX_CHANNELS]; |
| 96 | unsigned size; /* of each data[] in samples */ |
| 97 | unsigned tail; |
| 98 | } verify_input_fifo; |
| 99 | |
| 100 | typedef struct { |
| 101 | const FLAC__byte *data; |
| 102 | unsigned capacity; |
| 103 | unsigned bytes; |
| 104 | } verify_output; |
| 105 | |
| 106 | typedef enum { |
| 107 | ENCODER_IN_MAGIC = 0, |
| 108 | ENCODER_IN_METADATA = 1, |
| 109 | ENCODER_IN_AUDIO = 2 |
| 110 | } EncoderStateHint; |
| 111 | |
| 112 | static struct CompressionLevels { |
| 113 | FLAC__bool do_mid_side_stereo; |
| 114 | FLAC__bool loose_mid_side_stereo; |
| 115 | unsigned max_lpc_order; |
| 116 | unsigned qlp_coeff_precision; |
| 117 | FLAC__bool do_qlp_coeff_prec_search; |
| 118 | FLAC__bool do_escape_coding; |
| 119 | FLAC__bool do_exhaustive_model_search; |
| 120 | unsigned min_residual_partition_order; |
| 121 | unsigned max_residual_partition_order; |
| 122 | unsigned rice_parameter_search_dist; |
| 123 | } compression_levels_[] = { |
| 124 | { false, false, 0, 0, false, false, false, 0, 3, 0 }, |
| 125 | { true , true , 0, 0, false, false, false, 0, 3, 0 }, |
| 126 | { true , false, 0, 0, false, false, false, 0, 3, 0 }, |
| 127 | { false, false, 6, 0, false, false, false, 0, 4, 0 }, |
| 128 | { true , true , 8, 0, false, false, false, 0, 4, 0 }, |
| 129 | { true , false, 8, 0, false, false, false, 0, 5, 0 }, |
| 130 | { true , false, 8, 0, false, false, false, 0, 6, 0 }, |
| 131 | { true , false, 8, 0, false, false, true , 0, 6, 0 }, |
| 132 | { true , false, 12, 0, false, false, true , 0, 6, 0 } |
| 133 | }; |
| 134 | |
| 135 | |
| 136 | /*********************************************************************** |
| 137 | * |
| 138 | * Private class method prototypes |
| 139 | * |
| 140 | ***********************************************************************/ |
| 141 | |
| 142 | static void set_defaults_(FLAC__StreamEncoder *encoder); |
| 143 | static void free_(FLAC__StreamEncoder *encoder); |
| 144 | static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_blocksize); |
| 145 | static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples, FLAC__bool is_last_block); |
| 146 | static FLAC__StreamEncoderWriteStatus write_frame_(FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, FLAC__bool is_last_block); |
| 147 | static void update_metadata_(const FLAC__StreamEncoder *encoder); |
| 148 | #if FLAC__HAS_OGG |
| 149 | static void update_ogg_metadata_(FLAC__StreamEncoder *encoder); |
| 150 | #endif |
| 151 | static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block, FLAC__bool is_last_block); |
| 152 | static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block); |
| 153 | |
| 154 | static FLAC__bool process_subframe_( |
| 155 | FLAC__StreamEncoder *encoder, |
| 156 | unsigned min_partition_order, |
| 157 | unsigned max_partition_order, |
| 158 | const FLAC__FrameHeader *frame_header, |
| 159 | unsigned subframe_bps, |
| 160 | const FLAC__int32 integer_signal[], |
| 161 | FLAC__Subframe *subframe[2], |
| 162 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2], |
| 163 | FLAC__int32 *residual[2], |
| 164 | unsigned *best_subframe, |
| 165 | unsigned *best_bits |
| 166 | ); |
| 167 | |
| 168 | static FLAC__bool add_subframe_( |
| 169 | FLAC__StreamEncoder *encoder, |
| 170 | unsigned blocksize, |
| 171 | unsigned subframe_bps, |
| 172 | const FLAC__Subframe *subframe, |
| 173 | FLAC__BitWriter *frame |
| 174 | ); |
| 175 | |
| 176 | static unsigned evaluate_constant_subframe_( |
| 177 | FLAC__StreamEncoder *encoder, |
| 178 | const FLAC__int32 signal, |
| 179 | unsigned blocksize, |
| 180 | unsigned subframe_bps, |
| 181 | FLAC__Subframe *subframe |
| 182 | ); |
| 183 | |
| 184 | static unsigned evaluate_fixed_subframe_( |
| 185 | FLAC__StreamEncoder *encoder, |
| 186 | const FLAC__int32 signal[], |
| 187 | FLAC__int32 residual[], |
| 188 | FLAC__uint64 abs_residual_partition_sums[], |
| 189 | unsigned raw_bits_per_partition[], |
| 190 | unsigned blocksize, |
| 191 | unsigned subframe_bps, |
| 192 | unsigned order, |
| 193 | unsigned rice_parameter, |
| 194 | unsigned rice_parameter_limit, |
| 195 | unsigned min_partition_order, |
| 196 | unsigned max_partition_order, |
| 197 | FLAC__bool do_escape_coding, |
| 198 | unsigned rice_parameter_search_dist, |
| 199 | FLAC__Subframe *subframe, |
| 200 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents |
| 201 | ); |
| 202 | |
| 203 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 204 | static unsigned evaluate_lpc_subframe_( |
| 205 | FLAC__StreamEncoder *encoder, |
| 206 | const FLAC__int32 signal[], |
| 207 | FLAC__int32 residual[], |
| 208 | FLAC__uint64 abs_residual_partition_sums[], |
| 209 | unsigned raw_bits_per_partition[], |
| 210 | const FLAC__real lp_coeff[], |
| 211 | unsigned blocksize, |
| 212 | unsigned subframe_bps, |
| 213 | unsigned order, |
| 214 | unsigned qlp_coeff_precision, |
| 215 | unsigned rice_parameter, |
| 216 | unsigned rice_parameter_limit, |
| 217 | unsigned min_partition_order, |
| 218 | unsigned max_partition_order, |
| 219 | FLAC__bool do_escape_coding, |
| 220 | unsigned rice_parameter_search_dist, |
| 221 | FLAC__Subframe *subframe, |
| 222 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents |
| 223 | ); |
| 224 | #endif |
| 225 | |
| 226 | static unsigned evaluate_verbatim_subframe_( |
| 227 | FLAC__StreamEncoder *encoder, |
| 228 | const FLAC__int32 signal[], |
| 229 | unsigned blocksize, |
| 230 | unsigned subframe_bps, |
| 231 | FLAC__Subframe *subframe |
| 232 | ); |
| 233 | |
| 234 | static unsigned find_best_partition_order_( |
| 235 | struct FLAC__StreamEncoderPrivate *private_, |
| 236 | const FLAC__int32 residual[], |
| 237 | FLAC__uint64 abs_residual_partition_sums[], |
| 238 | unsigned raw_bits_per_partition[], |
| 239 | unsigned residual_samples, |
| 240 | unsigned predictor_order, |
| 241 | unsigned rice_parameter, |
| 242 | unsigned rice_parameter_limit, |
| 243 | unsigned min_partition_order, |
| 244 | unsigned max_partition_order, |
| 245 | unsigned bps, |
| 246 | FLAC__bool do_escape_coding, |
| 247 | unsigned rice_parameter_search_dist, |
| 248 | FLAC__EntropyCodingMethod *best_ecm |
| 249 | ); |
| 250 | |
| 251 | static void precompute_partition_info_sums_( |
| 252 | const FLAC__int32 residual[], |
| 253 | FLAC__uint64 abs_residual_partition_sums[], |
| 254 | unsigned residual_samples, |
| 255 | unsigned predictor_order, |
| 256 | unsigned min_partition_order, |
| 257 | unsigned max_partition_order, |
| 258 | unsigned bps |
| 259 | ); |
| 260 | |
| 261 | static void precompute_partition_info_escapes_( |
| 262 | const FLAC__int32 residual[], |
| 263 | unsigned raw_bits_per_partition[], |
| 264 | unsigned residual_samples, |
| 265 | unsigned predictor_order, |
| 266 | unsigned min_partition_order, |
| 267 | unsigned max_partition_order |
| 268 | ); |
| 269 | |
| 270 | static FLAC__bool set_partitioned_rice_( |
| 271 | #ifdef EXACT_RICE_BITS_CALCULATION |
| 272 | const FLAC__int32 residual[], |
| 273 | #endif |
| 274 | const FLAC__uint64 abs_residual_partition_sums[], |
| 275 | const unsigned raw_bits_per_partition[], |
| 276 | const unsigned residual_samples, |
| 277 | const unsigned predictor_order, |
| 278 | const unsigned suggested_rice_parameter, |
| 279 | const unsigned rice_parameter_limit, |
| 280 | const unsigned rice_parameter_search_dist, |
| 281 | const unsigned partition_order, |
| 282 | const FLAC__bool search_for_escapes, |
| 283 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, |
| 284 | unsigned *bits |
| 285 | ); |
| 286 | |
| 287 | static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples); |
| 288 | |
| 289 | /* verify-related routines: */ |
| 290 | static void append_to_verify_fifo_( |
| 291 | verify_input_fifo *fifo, |
| 292 | const FLAC__int32 * const input[], |
| 293 | unsigned input_offset, |
| 294 | unsigned channels, |
| 295 | unsigned wide_samples |
| 296 | ); |
| 297 | |
| 298 | static void append_to_verify_fifo_interleaved_( |
| 299 | verify_input_fifo *fifo, |
| 300 | const FLAC__int32 input[], |
| 301 | unsigned input_offset, |
| 302 | unsigned channels, |
| 303 | unsigned wide_samples |
| 304 | ); |
| 305 | |
| 306 | static FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data); |
| 307 | static FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data); |
| 308 | static void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data); |
| 309 | static void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data); |
| 310 | |
| 311 | static FLAC__StreamEncoderReadStatus file_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data); |
| 312 | static FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data); |
| 313 | static FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data); |
| 314 | static FLAC__StreamEncoderWriteStatus file_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame, void *client_data); |
| 315 | static FILE *get_binary_stdout_(void); |
| 316 | |
| 317 | |
| 318 | /*********************************************************************** |
| 319 | * |
| 320 | * Private class data |
| 321 | * |
| 322 | ***********************************************************************/ |
| 323 | |
| 324 | typedef struct FLAC__StreamEncoderPrivate { |
| 325 | unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */ |
| 326 | FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */ |
| 327 | FLAC__int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */ |
| 328 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 329 | FLAC__real *real_signal[FLAC__MAX_CHANNELS]; /* (@@@ currently unused) the floating-point version of the input signal */ |
| 330 | FLAC__real *real_signal_mid_side[2]; /* (@@@ currently unused) the floating-point version of the mid-side input signal (stereo only) */ |
| 331 | FLAC__real *window[FLAC__MAX_APODIZATION_FUNCTIONS]; /* the pre-computed floating-point window for each apodization function */ |
| 332 | FLAC__real *windowed_signal; /* the integer_signal[] * current window[] */ |
| 333 | #endif |
| 334 | unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */ |
| 335 | unsigned subframe_bps_mid_side[2]; /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits + 0/1) */ |
| 336 | FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */ |
| 337 | FLAC__int32 *residual_workspace_mid_side[2][2]; |
| 338 | FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2]; |
| 339 | FLAC__Subframe subframe_workspace_mid_side[2][2]; |
| 340 | FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2]; |
| 341 | FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2]; |
| 342 | FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace[FLAC__MAX_CHANNELS][2]; |
| 343 | FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace_mid_side[FLAC__MAX_CHANNELS][2]; |
| 344 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr[FLAC__MAX_CHANNELS][2]; |
| 345 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr_mid_side[FLAC__MAX_CHANNELS][2]; |
| 346 | unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index (0 or 1) into 2nd dimension of the above workspaces */ |
| 347 | unsigned best_subframe_mid_side[2]; |
| 348 | unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */ |
| 349 | unsigned best_subframe_bits_mid_side[2]; |
| 350 | FLAC__uint64 *abs_residual_partition_sums; /* workspace where the sum of abs(candidate residual) for each partition is stored */ |
| 351 | unsigned *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */ |
| 352 | FLAC__BitWriter *frame; /* the current frame being worked on */ |
| 353 | unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */ |
| 354 | unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */ |
| 355 | FLAC__ChannelAssignment last_channel_assignment; |
| 356 | FLAC__StreamMetadata streaminfo; /* scratchpad for STREAMINFO as it is built */ |
| 357 | FLAC__StreamMetadata_SeekTable *seek_table; /* pointer into encoder->protected_->metadata_ where the seek table is */ |
| 358 | unsigned current_sample_number; |
| 359 | unsigned current_frame_number; |
| 360 | FLAC__MD5Context md5context; |
| 361 | FLAC__CPUInfo cpuinfo; |
| 362 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 363 | unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]); |
| 364 | #else |
| 365 | unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]); |
| 366 | #endif |
| 367 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 368 | void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]); |
| 369 | void (*local_lpc_compute_residual_from_qlp_coefficients)(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]); |
| 370 | void (*local_lpc_compute_residual_from_qlp_coefficients_64bit)(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]); |
| 371 | void (*local_lpc_compute_residual_from_qlp_coefficients_16bit)(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]); |
| 372 | #endif |
| 373 | FLAC__bool use_wide_by_block; /* use slow 64-bit versions of some functions because of the block size */ |
| 374 | FLAC__bool use_wide_by_partition; /* use slow 64-bit versions of some functions because of the min partition order and blocksize */ |
| 375 | FLAC__bool use_wide_by_order; /* use slow 64-bit versions of some functions because of the lpc order */ |
| 376 | FLAC__bool disable_constant_subframes; |
| 377 | FLAC__bool disable_fixed_subframes; |
| 378 | FLAC__bool disable_verbatim_subframes; |
| 379 | #if FLAC__HAS_OGG |
| 380 | FLAC__bool is_ogg; |
| 381 | #endif |
| 382 | FLAC__StreamEncoderReadCallback read_callback; /* currently only needed for Ogg FLAC */ |
| 383 | FLAC__StreamEncoderSeekCallback seek_callback; |
| 384 | FLAC__StreamEncoderTellCallback tell_callback; |
| 385 | FLAC__StreamEncoderWriteCallback write_callback; |
| 386 | FLAC__StreamEncoderMetadataCallback metadata_callback; |
| 387 | FLAC__StreamEncoderProgressCallback progress_callback; |
| 388 | void *client_data; |
| 389 | unsigned first_seekpoint_to_check; |
| 390 | FILE *file; /* only used when encoding to a file */ |
| 391 | FLAC__uint64 bytes_written; |
| 392 | FLAC__uint64 samples_written; |
| 393 | unsigned frames_written; |
| 394 | unsigned total_frames_estimate; |
| 395 | /* unaligned (original) pointers to allocated data */ |
| 396 | FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS]; |
| 397 | FLAC__int32 *integer_signal_mid_side_unaligned[2]; |
| 398 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 399 | FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS]; /* (@@@ currently unused) */ |
| 400 | FLAC__real *real_signal_mid_side_unaligned[2]; /* (@@@ currently unused) */ |
| 401 | FLAC__real *window_unaligned[FLAC__MAX_APODIZATION_FUNCTIONS]; |
| 402 | FLAC__real *windowed_signal_unaligned; |
| 403 | #endif |
| 404 | FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2]; |
| 405 | FLAC__int32 *residual_workspace_mid_side_unaligned[2][2]; |
| 406 | FLAC__uint64 *abs_residual_partition_sums_unaligned; |
| 407 | unsigned *raw_bits_per_partition_unaligned; |
| 408 | /* |
| 409 | * These fields have been moved here from private function local |
| 410 | * declarations merely to save stack space during encoding. |
| 411 | */ |
| 412 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 413 | FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */ |
| 414 | #endif |
| 415 | FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_extra[2]; /* from find_best_partition_order_() */ |
| 416 | /* |
| 417 | * The data for the verify section |
| 418 | */ |
| 419 | struct { |
| 420 | FLAC__StreamDecoder *decoder; |
| 421 | EncoderStateHint state_hint; |
| 422 | FLAC__bool needs_magic_hack; |
| 423 | verify_input_fifo input_fifo; |
| 424 | verify_output output; |
| 425 | struct { |
| 426 | FLAC__uint64 absolute_sample; |
| 427 | unsigned frame_number; |
| 428 | unsigned channel; |
| 429 | unsigned sample; |
| 430 | FLAC__int32 expected; |
| 431 | FLAC__int32 got; |
| 432 | } error_stats; |
| 433 | } verify; |
| 434 | FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */ |
| 435 | } FLAC__StreamEncoderPrivate; |
| 436 | |
| 437 | /*********************************************************************** |
| 438 | * |
| 439 | * Public static class data |
| 440 | * |
| 441 | ***********************************************************************/ |
| 442 | |
| 443 | FLAC_API const char * const FLAC__StreamEncoderStateString[] = { |
| 444 | "FLAC__STREAM_ENCODER_OK", |
| 445 | "FLAC__STREAM_ENCODER_UNINITIALIZED", |
| 446 | "FLAC__STREAM_ENCODER_OGG_ERROR", |
| 447 | "FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR", |
| 448 | "FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA", |
| 449 | "FLAC__STREAM_ENCODER_CLIENT_ERROR", |
| 450 | "FLAC__STREAM_ENCODER_IO_ERROR", |
| 451 | "FLAC__STREAM_ENCODER_FRAMING_ERROR", |
| 452 | "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR" |
| 453 | }; |
| 454 | |
| 455 | FLAC_API const char * const FLAC__StreamEncoderInitStatusString[] = { |
| 456 | "FLAC__STREAM_ENCODER_INIT_STATUS_OK", |
| 457 | "FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR", |
| 458 | "FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER", |
| 459 | "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS", |
| 460 | "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS", |
| 461 | "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE", |
| 462 | "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE", |
| 463 | "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE", |
| 464 | "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER", |
| 465 | "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION", |
| 466 | "FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER", |
| 467 | "FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE", |
| 468 | "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA", |
| 469 | "FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED" |
| 470 | }; |
| 471 | |
| 472 | FLAC_API const char * const FLAC__treamEncoderReadStatusString[] = { |
| 473 | "FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE", |
| 474 | "FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM", |
| 475 | "FLAC__STREAM_ENCODER_READ_STATUS_ABORT", |
| 476 | "FLAC__STREAM_ENCODER_READ_STATUS_UNSUPPORTED" |
| 477 | }; |
| 478 | |
| 479 | FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = { |
| 480 | "FLAC__STREAM_ENCODER_WRITE_STATUS_OK", |
| 481 | "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR" |
| 482 | }; |
| 483 | |
| 484 | FLAC_API const char * const FLAC__StreamEncoderSeekStatusString[] = { |
| 485 | "FLAC__STREAM_ENCODER_SEEK_STATUS_OK", |
| 486 | "FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR", |
| 487 | "FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED" |
| 488 | }; |
| 489 | |
| 490 | FLAC_API const char * const FLAC__StreamEncoderTellStatusString[] = { |
| 491 | "FLAC__STREAM_ENCODER_TELL_STATUS_OK", |
| 492 | "FLAC__STREAM_ENCODER_TELL_STATUS_ERROR", |
| 493 | "FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED" |
| 494 | }; |
| 495 | |
| 496 | /* Number of samples that will be overread to watch for end of stream. By |
| 497 | * 'overread', we mean that the FLAC__stream_encoder_process*() calls will |
| 498 | * always try to read blocksize+1 samples before encoding a block, so that |
| 499 | * even if the stream has a total sample count that is an integral multiple |
| 500 | * of the blocksize, we will still notice when we are encoding the last |
| 501 | * block. This is needed, for example, to correctly set the end-of-stream |
| 502 | * marker in Ogg FLAC. |
| 503 | * |
| 504 | * WATCHOUT: some parts of the code assert that OVERREAD_ == 1 and there's |
| 505 | * not really any reason to change it. |
| 506 | */ |
| 507 | static const unsigned OVERREAD_ = 1; |
| 508 | |
| 509 | /*********************************************************************** |
| 510 | * |
| 511 | * Class constructor/destructor |
| 512 | * |
| 513 | */ |
| 514 | FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new(void) |
| 515 | { |
| 516 | FLAC__StreamEncoder *encoder; |
| 517 | unsigned i; |
| 518 | |
| 519 | FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */ |
| 520 | |
| 521 | encoder = (FLAC__StreamEncoder*)calloc(1, sizeof(FLAC__StreamEncoder)); |
| 522 | if(encoder == 0) { |
| 523 | return 0; |
| 524 | } |
| 525 | |
| 526 | encoder->protected_ = (FLAC__StreamEncoderProtected*)calloc(1, sizeof(FLAC__StreamEncoderProtected)); |
| 527 | if(encoder->protected_ == 0) { |
| 528 | free(encoder); |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | encoder->private_ = (FLAC__StreamEncoderPrivate*)calloc(1, sizeof(FLAC__StreamEncoderPrivate)); |
| 533 | if(encoder->private_ == 0) { |
| 534 | free(encoder->protected_); |
| 535 | free(encoder); |
| 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | encoder->private_->frame = FLAC__bitwriter_new(); |
| 540 | if(encoder->private_->frame == 0) { |
| 541 | free(encoder->private_); |
| 542 | free(encoder->protected_); |
| 543 | free(encoder); |
| 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | encoder->private_->file = 0; |
| 548 | |
| 549 | set_defaults_(encoder); |
| 550 | |
| 551 | encoder->private_->is_being_deleted = false; |
| 552 | |
| 553 | for(i = 0; i < FLAC__MAX_CHANNELS; i++) { |
| 554 | encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0]; |
| 555 | encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1]; |
| 556 | } |
| 557 | for(i = 0; i < 2; i++) { |
| 558 | encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0]; |
| 559 | encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1]; |
| 560 | } |
| 561 | for(i = 0; i < FLAC__MAX_CHANNELS; i++) { |
| 562 | encoder->private_->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->partitioned_rice_contents_workspace[i][0]; |
| 563 | encoder->private_->partitioned_rice_contents_workspace_ptr[i][1] = &encoder->private_->partitioned_rice_contents_workspace[i][1]; |
| 564 | } |
| 565 | for(i = 0; i < 2; i++) { |
| 566 | encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]; |
| 567 | encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][1] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]; |
| 568 | } |
| 569 | |
| 570 | for(i = 0; i < FLAC__MAX_CHANNELS; i++) { |
| 571 | FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][0]); |
| 572 | FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][1]); |
| 573 | } |
| 574 | for(i = 0; i < 2; i++) { |
| 575 | FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]); |
| 576 | FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]); |
| 577 | } |
| 578 | for(i = 0; i < 2; i++) |
| 579 | FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_extra[i]); |
| 580 | |
| 581 | encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED; |
| 582 | |
| 583 | return encoder; |
| 584 | } |
| 585 | |
| 586 | FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder) |
| 587 | { |
| 588 | unsigned i; |
| 589 | |
| 590 | FLAC__ASSERT(0 != encoder); |
| 591 | FLAC__ASSERT(0 != encoder->protected_); |
| 592 | FLAC__ASSERT(0 != encoder->private_); |
| 593 | FLAC__ASSERT(0 != encoder->private_->frame); |
| 594 | |
| 595 | encoder->private_->is_being_deleted = true; |
| 596 | |
| 597 | (void)FLAC__stream_encoder_finish(encoder); |
| 598 | |
| 599 | if(0 != encoder->private_->verify.decoder) |
| 600 | FLAC__stream_decoder_delete(encoder->private_->verify.decoder); |
| 601 | |
| 602 | for(i = 0; i < FLAC__MAX_CHANNELS; i++) { |
| 603 | FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][0]); |
| 604 | FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][1]); |
| 605 | } |
| 606 | for(i = 0; i < 2; i++) { |
| 607 | FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]); |
| 608 | FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]); |
| 609 | } |
| 610 | for(i = 0; i < 2; i++) |
| 611 | FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_extra[i]); |
| 612 | |
| 613 | FLAC__bitwriter_delete(encoder->private_->frame); |
| 614 | free(encoder->private_); |
| 615 | free(encoder->protected_); |
| 616 | free(encoder); |
| 617 | } |
| 618 | |
| 619 | /*********************************************************************** |
| 620 | * |
| 621 | * Public class methods |
| 622 | * |
| 623 | ***********************************************************************/ |
| 624 | |
| 625 | static FLAC__StreamEncoderInitStatus init_stream_internal_( |
| 626 | FLAC__StreamEncoder *encoder, |
| 627 | FLAC__StreamEncoderReadCallback read_callback, |
| 628 | FLAC__StreamEncoderWriteCallback write_callback, |
| 629 | FLAC__StreamEncoderSeekCallback seek_callback, |
| 630 | FLAC__StreamEncoderTellCallback tell_callback, |
| 631 | FLAC__StreamEncoderMetadataCallback metadata_callback, |
| 632 | void *client_data, |
| 633 | FLAC__bool is_ogg |
| 634 | ) |
| 635 | { |
| 636 | unsigned i; |
| 637 | FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment, metadata_picture_has_type1, metadata_picture_has_type2; |
| 638 | |
| 639 | FLAC__ASSERT(0 != encoder); |
| 640 | |
| 641 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 642 | return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED; |
| 643 | |
| 644 | #if !FLAC__HAS_OGG |
| 645 | if(is_ogg) |
| 646 | return FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER; |
| 647 | #endif |
| 648 | |
| 649 | if(0 == write_callback || (seek_callback && 0 == tell_callback)) |
| 650 | return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS; |
| 651 | |
| 652 | if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS) |
| 653 | return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS; |
| 654 | |
| 655 | if(encoder->protected_->channels != 2) { |
| 656 | encoder->protected_->do_mid_side_stereo = false; |
| 657 | encoder->protected_->loose_mid_side_stereo = false; |
| 658 | } |
| 659 | else if(!encoder->protected_->do_mid_side_stereo) |
| 660 | encoder->protected_->loose_mid_side_stereo = false; |
| 661 | |
| 662 | if(encoder->protected_->bits_per_sample >= 32) |
| 663 | encoder->protected_->do_mid_side_stereo = false; /* since we currenty do 32-bit math, the side channel would have 33 bps and overflow */ |
| 664 | |
| 665 | if(encoder->protected_->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected_->bits_per_sample > FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE) |
| 666 | return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE; |
| 667 | |
| 668 | if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate)) |
| 669 | return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE; |
| 670 | |
| 671 | if(encoder->protected_->blocksize == 0) { |
| 672 | if(encoder->protected_->max_lpc_order == 0) |
| 673 | encoder->protected_->blocksize = 1152; |
| 674 | else |
| 675 | encoder->protected_->blocksize = 4096; |
| 676 | } |
| 677 | |
| 678 | if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE) |
| 679 | return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE; |
| 680 | |
| 681 | if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER) |
| 682 | return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER; |
| 683 | |
| 684 | if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order) |
| 685 | return FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER; |
| 686 | |
| 687 | if(encoder->protected_->qlp_coeff_precision == 0) { |
| 688 | if(encoder->protected_->bits_per_sample < 16) { |
| 689 | /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */ |
| 690 | /* @@@ until then we'll make a guess */ |
| 691 | encoder->protected_->qlp_coeff_precision = max(FLAC__MIN_QLP_COEFF_PRECISION, 2 + encoder->protected_->bits_per_sample / 2); |
| 692 | } |
| 693 | else if(encoder->protected_->bits_per_sample == 16) { |
| 694 | if(encoder->protected_->blocksize <= 192) |
| 695 | encoder->protected_->qlp_coeff_precision = 7; |
| 696 | else if(encoder->protected_->blocksize <= 384) |
| 697 | encoder->protected_->qlp_coeff_precision = 8; |
| 698 | else if(encoder->protected_->blocksize <= 576) |
| 699 | encoder->protected_->qlp_coeff_precision = 9; |
| 700 | else if(encoder->protected_->blocksize <= 1152) |
| 701 | encoder->protected_->qlp_coeff_precision = 10; |
| 702 | else if(encoder->protected_->blocksize <= 2304) |
| 703 | encoder->protected_->qlp_coeff_precision = 11; |
| 704 | else if(encoder->protected_->blocksize <= 4608) |
| 705 | encoder->protected_->qlp_coeff_precision = 12; |
| 706 | else |
| 707 | encoder->protected_->qlp_coeff_precision = 13; |
| 708 | } |
| 709 | else { |
| 710 | if(encoder->protected_->blocksize <= 384) |
| 711 | encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-2; |
| 712 | else if(encoder->protected_->blocksize <= 1152) |
| 713 | encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-1; |
| 714 | else |
| 715 | encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION; |
| 716 | } |
| 717 | FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION); |
| 718 | } |
| 719 | else if(encoder->protected_->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected_->qlp_coeff_precision > FLAC__MAX_QLP_COEFF_PRECISION) |
| 720 | return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION; |
| 721 | |
| 722 | if(encoder->protected_->streamable_subset) { |
| 723 | if( |
| 724 | encoder->protected_->blocksize != 192 && |
| 725 | encoder->protected_->blocksize != 576 && |
| 726 | encoder->protected_->blocksize != 1152 && |
| 727 | encoder->protected_->blocksize != 2304 && |
| 728 | encoder->protected_->blocksize != 4608 && |
| 729 | encoder->protected_->blocksize != 256 && |
| 730 | encoder->protected_->blocksize != 512 && |
| 731 | encoder->protected_->blocksize != 1024 && |
| 732 | encoder->protected_->blocksize != 2048 && |
| 733 | encoder->protected_->blocksize != 4096 && |
| 734 | encoder->protected_->blocksize != 8192 && |
| 735 | encoder->protected_->blocksize != 16384 |
| 736 | ) |
| 737 | return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE; |
| 738 | if(!FLAC__format_sample_rate_is_subset(encoder->protected_->sample_rate)) |
| 739 | return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE; |
| 740 | if( |
| 741 | encoder->protected_->bits_per_sample != 8 && |
| 742 | encoder->protected_->bits_per_sample != 12 && |
| 743 | encoder->protected_->bits_per_sample != 16 && |
| 744 | encoder->protected_->bits_per_sample != 20 && |
| 745 | encoder->protected_->bits_per_sample != 24 |
| 746 | ) |
| 747 | return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE; |
| 748 | if(encoder->protected_->max_residual_partition_order > FLAC__SUBSET_MAX_RICE_PARTITION_ORDER) |
| 749 | return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE; |
| 750 | if( |
| 751 | encoder->protected_->sample_rate <= 48000 && |
| 752 | ( |
| 753 | encoder->protected_->blocksize > FLAC__SUBSET_MAX_BLOCK_SIZE_48000HZ || |
| 754 | encoder->protected_->max_lpc_order > FLAC__SUBSET_MAX_LPC_ORDER_48000HZ |
| 755 | ) |
| 756 | ) { |
| 757 | return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE; |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN)) |
| 762 | encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1; |
| 763 | if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order) |
| 764 | encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order; |
| 765 | |
| 766 | #if FLAC__HAS_OGG |
| 767 | /* reorder metadata if necessary to ensure that any VORBIS_COMMENT is the first, according to the mapping spec */ |
| 768 | if(is_ogg && 0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 1) { |
| 769 | unsigned i; |
| 770 | for(i = 1; i < encoder->protected_->num_metadata_blocks; i++) { |
| 771 | if(0 != encoder->protected_->metadata[i] && encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) { |
| 772 | FLAC__StreamMetadata *vc = encoder->protected_->metadata[i]; |
| 773 | for( ; i > 0; i--) |
| 774 | encoder->protected_->metadata[i] = encoder->protected_->metadata[i-1]; |
| 775 | encoder->protected_->metadata[0] = vc; |
| 776 | break; |
| 777 | } |
| 778 | } |
| 779 | } |
| 780 | #endif |
| 781 | /* keep track of any SEEKTABLE block */ |
| 782 | if(0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0) { |
| 783 | unsigned i; |
| 784 | for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) { |
| 785 | if(0 != encoder->protected_->metadata[i] && encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) { |
| 786 | encoder->private_->seek_table = &encoder->protected_->metadata[i]->data.seek_table; |
| 787 | break; /* take only the first one */ |
| 788 | } |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | /* validate metadata */ |
| 793 | if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0) |
| 794 | return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA; |
| 795 | metadata_has_seektable = false; |
| 796 | metadata_has_vorbis_comment = false; |
| 797 | metadata_picture_has_type1 = false; |
| 798 | metadata_picture_has_type2 = false; |
| 799 | for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) { |
| 800 | const FLAC__StreamMetadata *m = encoder->protected_->metadata[i]; |
| 801 | if(m->type == FLAC__METADATA_TYPE_STREAMINFO) |
| 802 | return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA; |
| 803 | else if(m->type == FLAC__METADATA_TYPE_SEEKTABLE) { |
| 804 | if(metadata_has_seektable) /* only one is allowed */ |
| 805 | return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA; |
| 806 | metadata_has_seektable = true; |
| 807 | if(!FLAC__format_seektable_is_legal(&m->data.seek_table)) |
| 808 | return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA; |
| 809 | } |
| 810 | else if(m->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) { |
| 811 | if(metadata_has_vorbis_comment) /* only one is allowed */ |
| 812 | return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA; |
| 813 | metadata_has_vorbis_comment = true; |
| 814 | } |
| 815 | else if(m->type == FLAC__METADATA_TYPE_CUESHEET) { |
| 816 | if(!FLAC__format_cuesheet_is_legal(&m->data.cue_sheet, m->data.cue_sheet.is_cd, /*violation=*/0)) |
| 817 | return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA; |
| 818 | } |
| 819 | else if(m->type == FLAC__METADATA_TYPE_PICTURE) { |
| 820 | if(!FLAC__format_picture_is_legal(&m->data.picture, /*violation=*/0)) |
| 821 | return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA; |
| 822 | if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD) { |
| 823 | if(metadata_picture_has_type1) /* there should only be 1 per stream */ |
| 824 | return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA; |
| 825 | metadata_picture_has_type1 = true; |
| 826 | /* standard icon must be 32x32 pixel PNG */ |
| 827 | if( |
| 828 | m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD && |
| 829 | ( |
| 830 | (strcmp(m->data.picture.mime_type, "image/png") && strcmp(m->data.picture.mime_type, "-->")) || |
| 831 | m->data.picture.width != 32 || |
| 832 | m->data.picture.height != 32 |
| 833 | ) |
| 834 | ) |
| 835 | return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA; |
| 836 | } |
| 837 | else if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON) { |
| 838 | if(metadata_picture_has_type2) /* there should only be 1 per stream */ |
| 839 | return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA; |
| 840 | metadata_picture_has_type2 = true; |
| 841 | } |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | encoder->private_->input_capacity = 0; |
| 846 | for(i = 0; i < encoder->protected_->channels; i++) { |
| 847 | encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0; |
| 848 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 849 | encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0; |
| 850 | #endif |
| 851 | } |
| 852 | for(i = 0; i < 2; i++) { |
| 853 | encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0; |
| 854 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 855 | encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0; |
| 856 | #endif |
| 857 | } |
| 858 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 859 | for(i = 0; i < encoder->protected_->num_apodizations; i++) |
| 860 | encoder->private_->window_unaligned[i] = encoder->private_->window[i] = 0; |
| 861 | encoder->private_->windowed_signal_unaligned = encoder->private_->windowed_signal = 0; |
| 862 | #endif |
| 863 | for(i = 0; i < encoder->protected_->channels; i++) { |
| 864 | encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0; |
| 865 | encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0; |
| 866 | encoder->private_->best_subframe[i] = 0; |
| 867 | } |
| 868 | for(i = 0; i < 2; i++) { |
| 869 | encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0; |
| 870 | encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0; |
| 871 | encoder->private_->best_subframe_mid_side[i] = 0; |
| 872 | } |
| 873 | encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0; |
| 874 | encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0; |
| 875 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 876 | encoder->private_->loose_mid_side_stereo_frames = (unsigned)((FLAC__double)encoder->protected_->sample_rate * 0.4 / (FLAC__double)encoder->protected_->blocksize + 0.5); |
| 877 | #else |
| 878 | /* 26214 is the approximate fixed-point equivalent to 0.4 (0.4 * 2^16) */ |
| 879 | /* sample rate can be up to 655350 Hz, and thus use 20 bits, so we do the multiply÷ by hand */ |
| 880 | FLAC__ASSERT(FLAC__MAX_SAMPLE_RATE <= 655350); |
| 881 | FLAC__ASSERT(FLAC__MAX_BLOCK_SIZE <= 65535); |
| 882 | FLAC__ASSERT(encoder->protected_->sample_rate <= 655350); |
| 883 | FLAC__ASSERT(encoder->protected_->blocksize <= 65535); |
| 884 | encoder->private_->loose_mid_side_stereo_frames = (unsigned)FLAC__fixedpoint_trunc((((FLAC__uint64)(encoder->protected_->sample_rate) * (FLAC__uint64)(26214)) << 16) / (encoder->protected_->blocksize<<16) + FLAC__FP_ONE_HALF); |
| 885 | #endif |
| 886 | if(encoder->private_->loose_mid_side_stereo_frames == 0) |
| 887 | encoder->private_->loose_mid_side_stereo_frames = 1; |
| 888 | encoder->private_->loose_mid_side_stereo_frame_count = 0; |
| 889 | encoder->private_->current_sample_number = 0; |
| 890 | encoder->private_->current_frame_number = 0; |
| 891 | |
| 892 | encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30); |
| 893 | encoder->private_->use_wide_by_order = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(max(encoder->protected_->max_lpc_order, FLAC__MAX_FIXED_ORDER))+1 > 30); /*@@@ need to use this? */ |
| 894 | encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */ |
| 895 | |
| 896 | /* |
| 897 | * get the CPU info and set the function pointers |
| 898 | */ |
| 899 | FLAC__cpu_info(&encoder->private_->cpuinfo); |
| 900 | /* first default to the non-asm routines */ |
| 901 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 902 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation; |
| 903 | #endif |
| 904 | encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor; |
| 905 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 906 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients; |
| 907 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit = FLAC__lpc_compute_residual_from_qlp_coefficients_wide; |
| 908 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients; |
| 909 | #endif |
| 910 | /* now override with asm where appropriate */ |
| 911 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 912 | # ifndef FLAC__NO_ASM |
| 913 | if(encoder->private_->cpuinfo.use_asm) { |
| 914 | # ifdef FLAC__CPU_IA32 |
| 915 | FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32); |
| 916 | # ifdef FLAC__HAS_NASM |
| 917 | if(encoder->private_->cpuinfo.data.ia32.sse) { |
| 918 | if(encoder->protected_->max_lpc_order < 4) |
| 919 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4; |
| 920 | else if(encoder->protected_->max_lpc_order < 8) |
| 921 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8; |
| 922 | else if(encoder->protected_->max_lpc_order < 12) |
| 923 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12; |
| 924 | else |
| 925 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32; |
| 926 | } |
| 927 | else if(encoder->private_->cpuinfo.data.ia32._3dnow) |
| 928 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow; |
| 929 | else |
| 930 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32; |
| 931 | if(encoder->private_->cpuinfo.data.ia32.mmx) { |
| 932 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32; |
| 933 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx; |
| 934 | } |
| 935 | else { |
| 936 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32; |
| 937 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32; |
| 938 | } |
| 939 | if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov) |
| 940 | encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov; |
| 941 | # endif /* FLAC__HAS_NASM */ |
| 942 | # endif /* FLAC__CPU_IA32 */ |
| 943 | } |
| 944 | # endif /* !FLAC__NO_ASM */ |
| 945 | #endif /* !FLAC__INTEGER_ONLY_LIBRARY */ |
| 946 | /* finally override based on wide-ness if necessary */ |
| 947 | if(encoder->private_->use_wide_by_block) { |
| 948 | encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide; |
| 949 | } |
| 950 | |
| 951 | /* set state to OK; from here on, errors are fatal and we'll override the state then */ |
| 952 | encoder->protected_->state = FLAC__STREAM_ENCODER_OK; |
| 953 | |
| 954 | #if FLAC__HAS_OGG |
| 955 | encoder->private_->is_ogg = is_ogg; |
| 956 | if(is_ogg && !FLAC__ogg_encoder_aspect_init(&encoder->protected_->ogg_encoder_aspect)) { |
| 957 | encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR; |
| 958 | return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 959 | } |
| 960 | #endif |
| 961 | |
| 962 | encoder->private_->read_callback = read_callback; |
| 963 | encoder->private_->write_callback = write_callback; |
| 964 | encoder->private_->seek_callback = seek_callback; |
| 965 | encoder->private_->tell_callback = tell_callback; |
| 966 | encoder->private_->metadata_callback = metadata_callback; |
| 967 | encoder->private_->client_data = client_data; |
| 968 | |
| 969 | if(!resize_buffers_(encoder, encoder->protected_->blocksize)) { |
| 970 | /* the above function sets the state for us in case of an error */ |
| 971 | return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 972 | } |
| 973 | |
| 974 | if(!FLAC__bitwriter_init(encoder->private_->frame)) { |
| 975 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 976 | return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 977 | } |
| 978 | |
| 979 | /* |
| 980 | * Set up the verify stuff if necessary |
| 981 | */ |
| 982 | if(encoder->protected_->verify) { |
| 983 | /* |
| 984 | * First, set up the fifo which will hold the |
| 985 | * original signal to compare against |
| 986 | */ |
| 987 | encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize+OVERREAD_; |
| 988 | for(i = 0; i < encoder->protected_->channels; i++) { |
| 989 | if(0 == (encoder->private_->verify.input_fifo.data[i] = (FLAC__int32*)safe_malloc_mul_2op_(sizeof(FLAC__int32), /*times*/encoder->private_->verify.input_fifo.size))) { |
| 990 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 991 | return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 992 | } |
| 993 | } |
| 994 | encoder->private_->verify.input_fifo.tail = 0; |
| 995 | |
| 996 | /* |
| 997 | * Now set up a stream decoder for verification |
| 998 | */ |
| 999 | encoder->private_->verify.decoder = FLAC__stream_decoder_new(); |
| 1000 | if(0 == encoder->private_->verify.decoder) { |
| 1001 | encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR; |
| 1002 | return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1003 | } |
| 1004 | |
| 1005 | if(FLAC__stream_decoder_init_stream(encoder->private_->verify.decoder, verify_read_callback_, /*seek_callback=*/0, /*tell_callback=*/0, /*length_callback=*/0, /*eof_callback=*/0, verify_write_callback_, verify_metadata_callback_, verify_error_callback_, /*client_data=*/encoder) != FLAC__STREAM_DECODER_INIT_STATUS_OK) { |
| 1006 | encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR; |
| 1007 | return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1008 | } |
| 1009 | } |
| 1010 | encoder->private_->verify.error_stats.absolute_sample = 0; |
| 1011 | encoder->private_->verify.error_stats.frame_number = 0; |
| 1012 | encoder->private_->verify.error_stats.channel = 0; |
| 1013 | encoder->private_->verify.error_stats.sample = 0; |
| 1014 | encoder->private_->verify.error_stats.expected = 0; |
| 1015 | encoder->private_->verify.error_stats.got = 0; |
| 1016 | |
| 1017 | /* |
| 1018 | * These must be done before we write any metadata, because that |
| 1019 | * calls the write_callback, which uses these values. |
| 1020 | */ |
| 1021 | encoder->private_->first_seekpoint_to_check = 0; |
| 1022 | encoder->private_->samples_written = 0; |
| 1023 | encoder->protected_->streaminfo_offset = 0; |
| 1024 | encoder->protected_->seektable_offset = 0; |
| 1025 | encoder->protected_->audio_offset = 0; |
| 1026 | |
| 1027 | /* |
| 1028 | * write the stream header |
| 1029 | */ |
| 1030 | if(encoder->protected_->verify) |
| 1031 | encoder->private_->verify.state_hint = ENCODER_IN_MAGIC; |
| 1032 | if(!FLAC__bitwriter_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN)) { |
| 1033 | encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
| 1034 | return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1035 | } |
| 1036 | if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) { |
| 1037 | /* the above function sets the state for us in case of an error */ |
| 1038 | return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1039 | } |
| 1040 | |
| 1041 | /* |
| 1042 | * write the STREAMINFO metadata block |
| 1043 | */ |
| 1044 | if(encoder->protected_->verify) |
| 1045 | encoder->private_->verify.state_hint = ENCODER_IN_METADATA; |
| 1046 | encoder->private_->streaminfo.type = FLAC__METADATA_TYPE_STREAMINFO; |
| 1047 | encoder->private_->streaminfo.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */ |
| 1048 | encoder->private_->streaminfo.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH; |
| 1049 | encoder->private_->streaminfo.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */ |
| 1050 | encoder->private_->streaminfo.data.stream_info.max_blocksize = encoder->protected_->blocksize; |
| 1051 | encoder->private_->streaminfo.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */ |
| 1052 | encoder->private_->streaminfo.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */ |
| 1053 | encoder->private_->streaminfo.data.stream_info.sample_rate = encoder->protected_->sample_rate; |
| 1054 | encoder->private_->streaminfo.data.stream_info.channels = encoder->protected_->channels; |
| 1055 | encoder->private_->streaminfo.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample; |
| 1056 | encoder->private_->streaminfo.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */ |
| 1057 | memset(encoder->private_->streaminfo.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */ |
| 1058 | if(encoder->protected_->do_md5) |
| 1059 | FLAC__MD5Init(&encoder->private_->md5context); |
| 1060 | if(!FLAC__add_metadata_block(&encoder->private_->streaminfo, encoder->private_->frame)) { |
| 1061 | encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
| 1062 | return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1063 | } |
| 1064 | if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) { |
| 1065 | /* the above function sets the state for us in case of an error */ |
| 1066 | return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1067 | } |
| 1068 | |
| 1069 | /* |
| 1070 | * Now that the STREAMINFO block is written, we can init this to an |
| 1071 | * absurdly-high value... |
| 1072 | */ |
| 1073 | encoder->private_->streaminfo.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1; |
| 1074 | /* ... and clear this to 0 */ |
| 1075 | encoder->private_->streaminfo.data.stream_info.total_samples = 0; |
| 1076 | |
| 1077 | /* |
| 1078 | * Check to see if the supplied metadata contains a VORBIS_COMMENT; |
| 1079 | * if not, we will write an empty one (FLAC__add_metadata_block() |
| 1080 | * automatically supplies the vendor string). |
| 1081 | * |
| 1082 | * WATCHOUT: the Ogg FLAC mapping requires us to write this block after |
| 1083 | * the STREAMINFO. (In the case that metadata_has_vorbis_comment is |
| 1084 | * true it will have already insured that the metadata list is properly |
| 1085 | * ordered.) |
| 1086 | */ |
| 1087 | if(!metadata_has_vorbis_comment) { |
| 1088 | FLAC__StreamMetadata vorbis_comment; |
| 1089 | vorbis_comment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT; |
| 1090 | vorbis_comment.is_last = (encoder->protected_->num_metadata_blocks == 0); |
| 1091 | vorbis_comment.length = 4 + 4; /* MAGIC NUMBER */ |
| 1092 | vorbis_comment.data.vorbis_comment.vendor_string.length = 0; |
| 1093 | vorbis_comment.data.vorbis_comment.vendor_string.entry = 0; |
| 1094 | vorbis_comment.data.vorbis_comment.num_comments = 0; |
| 1095 | vorbis_comment.data.vorbis_comment.comments = 0; |
| 1096 | if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame)) { |
| 1097 | encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
| 1098 | return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1099 | } |
| 1100 | if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) { |
| 1101 | /* the above function sets the state for us in case of an error */ |
| 1102 | return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | /* |
| 1107 | * write the user's metadata blocks |
| 1108 | */ |
| 1109 | for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) { |
| 1110 | encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1); |
| 1111 | if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame)) { |
| 1112 | encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
| 1113 | return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1114 | } |
| 1115 | if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) { |
| 1116 | /* the above function sets the state for us in case of an error */ |
| 1117 | return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | /* now that all the metadata is written, we save the stream offset */ |
| 1122 | if(encoder->private_->tell_callback && encoder->private_->tell_callback(encoder, &encoder->protected_->audio_offset, encoder->private_->client_data) == FLAC__STREAM_ENCODER_TELL_STATUS_ERROR) { /* FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED just means we didn't get the offset; no error */ |
| 1123 | encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR; |
| 1124 | return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1125 | } |
| 1126 | |
| 1127 | if(encoder->protected_->verify) |
| 1128 | encoder->private_->verify.state_hint = ENCODER_IN_AUDIO; |
| 1129 | |
| 1130 | return FLAC__STREAM_ENCODER_INIT_STATUS_OK; |
| 1131 | } |
| 1132 | |
| 1133 | FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_stream( |
| 1134 | FLAC__StreamEncoder *encoder, |
| 1135 | FLAC__StreamEncoderWriteCallback write_callback, |
| 1136 | FLAC__StreamEncoderSeekCallback seek_callback, |
| 1137 | FLAC__StreamEncoderTellCallback tell_callback, |
| 1138 | FLAC__StreamEncoderMetadataCallback metadata_callback, |
| 1139 | void *client_data |
| 1140 | ) |
| 1141 | { |
| 1142 | return init_stream_internal_( |
| 1143 | encoder, |
| 1144 | /*read_callback=*/0, |
| 1145 | write_callback, |
| 1146 | seek_callback, |
| 1147 | tell_callback, |
| 1148 | metadata_callback, |
| 1149 | client_data, |
| 1150 | /*is_ogg=*/false |
| 1151 | ); |
| 1152 | } |
| 1153 | |
| 1154 | FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_stream( |
| 1155 | FLAC__StreamEncoder *encoder, |
| 1156 | FLAC__StreamEncoderReadCallback read_callback, |
| 1157 | FLAC__StreamEncoderWriteCallback write_callback, |
| 1158 | FLAC__StreamEncoderSeekCallback seek_callback, |
| 1159 | FLAC__StreamEncoderTellCallback tell_callback, |
| 1160 | FLAC__StreamEncoderMetadataCallback metadata_callback, |
| 1161 | void *client_data |
| 1162 | ) |
| 1163 | { |
| 1164 | return init_stream_internal_( |
| 1165 | encoder, |
| 1166 | read_callback, |
| 1167 | write_callback, |
| 1168 | seek_callback, |
| 1169 | tell_callback, |
| 1170 | metadata_callback, |
| 1171 | client_data, |
| 1172 | /*is_ogg=*/true |
| 1173 | ); |
| 1174 | } |
| 1175 | |
| 1176 | static FLAC__StreamEncoderInitStatus init_FILE_internal_( |
| 1177 | FLAC__StreamEncoder *encoder, |
| 1178 | FILE *file, |
| 1179 | FLAC__StreamEncoderProgressCallback progress_callback, |
| 1180 | void *client_data, |
| 1181 | FLAC__bool is_ogg |
| 1182 | ) |
| 1183 | { |
| 1184 | FLAC__StreamEncoderInitStatus init_status; |
| 1185 | |
| 1186 | FLAC__ASSERT(0 != encoder); |
| 1187 | FLAC__ASSERT(0 != file); |
| 1188 | |
| 1189 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1190 | return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED; |
| 1191 | |
| 1192 | /* double protection */ |
| 1193 | if(file == 0) { |
| 1194 | encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR; |
| 1195 | return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1196 | } |
| 1197 | |
| 1198 | /* |
| 1199 | * To make sure that our file does not go unclosed after an error, we |
| 1200 | * must assign the FILE pointer before any further error can occur in |
| 1201 | * this routine. |
| 1202 | */ |
| 1203 | if(file == stdout) |
| 1204 | file = get_binary_stdout_(); /* just to be safe */ |
| 1205 | |
| 1206 | encoder->private_->file = file; |
| 1207 | |
| 1208 | encoder->private_->progress_callback = progress_callback; |
| 1209 | encoder->private_->bytes_written = 0; |
| 1210 | encoder->private_->samples_written = 0; |
| 1211 | encoder->private_->frames_written = 0; |
| 1212 | |
| 1213 | init_status = init_stream_internal_( |
| 1214 | encoder, |
| 1215 | encoder->private_->file == stdout? 0 : is_ogg? file_read_callback_ : 0, |
| 1216 | file_write_callback_, |
| 1217 | encoder->private_->file == stdout? 0 : file_seek_callback_, |
| 1218 | encoder->private_->file == stdout? 0 : file_tell_callback_, |
| 1219 | /*metadata_callback=*/0, |
| 1220 | client_data, |
| 1221 | is_ogg |
| 1222 | ); |
| 1223 | if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) { |
| 1224 | /* the above function sets the state for us in case of an error */ |
| 1225 | return init_status; |
| 1226 | } |
| 1227 | |
| 1228 | { |
| 1229 | unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder); |
| 1230 | |
| 1231 | FLAC__ASSERT(blocksize != 0); |
| 1232 | encoder->private_->total_frames_estimate = (unsigned)((FLAC__stream_encoder_get_total_samples_estimate(encoder) + blocksize - 1) / blocksize); |
| 1233 | } |
| 1234 | |
| 1235 | return init_status; |
| 1236 | } |
| 1237 | |
| 1238 | FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_FILE( |
| 1239 | FLAC__StreamEncoder *encoder, |
| 1240 | FILE *file, |
| 1241 | FLAC__StreamEncoderProgressCallback progress_callback, |
| 1242 | void *client_data |
| 1243 | ) |
| 1244 | { |
| 1245 | return init_FILE_internal_(encoder, file, progress_callback, client_data, /*is_ogg=*/false); |
| 1246 | } |
| 1247 | |
| 1248 | FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_FILE( |
| 1249 | FLAC__StreamEncoder *encoder, |
| 1250 | FILE *file, |
| 1251 | FLAC__StreamEncoderProgressCallback progress_callback, |
| 1252 | void *client_data |
| 1253 | ) |
| 1254 | { |
| 1255 | return init_FILE_internal_(encoder, file, progress_callback, client_data, /*is_ogg=*/true); |
| 1256 | } |
| 1257 | |
| 1258 | static FLAC__StreamEncoderInitStatus init_file_internal_( |
| 1259 | FLAC__StreamEncoder *encoder, |
| 1260 | const char *filename, |
| 1261 | FLAC__StreamEncoderProgressCallback progress_callback, |
| 1262 | void *client_data, |
| 1263 | FLAC__bool is_ogg |
| 1264 | ) |
| 1265 | { |
| 1266 | FILE *file; |
| 1267 | |
| 1268 | FLAC__ASSERT(0 != encoder); |
| 1269 | |
| 1270 | /* |
| 1271 | * To make sure that our file does not go unclosed after an error, we |
| 1272 | * have to do the same entrance checks here that are later performed |
| 1273 | * in FLAC__stream_encoder_init_FILE() before the FILE* is assigned. |
| 1274 | */ |
| 1275 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1276 | return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED; |
| 1277 | |
| 1278 | file = filename? fopen(filename, "w+b") : stdout; |
| 1279 | |
| 1280 | if(file == 0) { |
| 1281 | encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR; |
| 1282 | return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1283 | } |
| 1284 | |
| 1285 | return init_FILE_internal_(encoder, file, progress_callback, client_data, is_ogg); |
| 1286 | } |
| 1287 | |
| 1288 | FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_file( |
| 1289 | FLAC__StreamEncoder *encoder, |
| 1290 | const char *filename, |
| 1291 | FLAC__StreamEncoderProgressCallback progress_callback, |
| 1292 | void *client_data |
| 1293 | ) |
| 1294 | { |
| 1295 | return init_file_internal_(encoder, filename, progress_callback, client_data, /*is_ogg=*/false); |
| 1296 | } |
| 1297 | |
| 1298 | FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_file( |
| 1299 | FLAC__StreamEncoder *encoder, |
| 1300 | const char *filename, |
| 1301 | FLAC__StreamEncoderProgressCallback progress_callback, |
| 1302 | void *client_data |
| 1303 | ) |
| 1304 | { |
| 1305 | return init_file_internal_(encoder, filename, progress_callback, client_data, /*is_ogg=*/true); |
| 1306 | } |
| 1307 | |
| 1308 | FLAC_API FLAC__bool FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder) |
| 1309 | { |
| 1310 | FLAC__bool error = false; |
| 1311 | |
| 1312 | FLAC__ASSERT(0 != encoder); |
| 1313 | FLAC__ASSERT(0 != encoder->private_); |
| 1314 | FLAC__ASSERT(0 != encoder->protected_); |
| 1315 | |
| 1316 | if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1317 | return true; |
| 1318 | |
| 1319 | if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) { |
| 1320 | if(encoder->private_->current_sample_number != 0) { |
| 1321 | const FLAC__bool is_fractional_block = encoder->protected_->blocksize != encoder->private_->current_sample_number; |
| 1322 | encoder->protected_->blocksize = encoder->private_->current_sample_number; |
| 1323 | if(!process_frame_(encoder, is_fractional_block, /*is_last_block=*/true)) |
| 1324 | error = true; |
| 1325 | } |
| 1326 | } |
| 1327 | |
| 1328 | if(encoder->protected_->do_md5) |
| 1329 | FLAC__MD5Final(encoder->private_->streaminfo.data.stream_info.md5sum, &encoder->private_->md5context); |
| 1330 | |
| 1331 | if(!encoder->private_->is_being_deleted) { |
| 1332 | if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK) { |
| 1333 | if(encoder->private_->seek_callback) { |
| 1334 | #if FLAC__HAS_OGG |
| 1335 | if(encoder->private_->is_ogg) |
| 1336 | update_ogg_metadata_(encoder); |
| 1337 | else |
| 1338 | #endif |
| 1339 | update_metadata_(encoder); |
| 1340 | |
| 1341 | /* check if an error occurred while updating metadata */ |
| 1342 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_OK) |
| 1343 | error = true; |
| 1344 | } |
| 1345 | if(encoder->private_->metadata_callback) |
| 1346 | encoder->private_->metadata_callback(encoder, &encoder->private_->streaminfo, encoder->private_->client_data); |
| 1347 | } |
| 1348 | |
| 1349 | if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder && !FLAC__stream_decoder_finish(encoder->private_->verify.decoder)) { |
| 1350 | if(!error) |
| 1351 | encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA; |
| 1352 | error = true; |
| 1353 | } |
| 1354 | } |
| 1355 | |
| 1356 | if(0 != encoder->private_->file) { |
| 1357 | if(encoder->private_->file != stdout) |
| 1358 | fclose(encoder->private_->file); |
| 1359 | encoder->private_->file = 0; |
| 1360 | } |
| 1361 | |
| 1362 | #if FLAC__HAS_OGG |
| 1363 | if(encoder->private_->is_ogg) |
| 1364 | FLAC__ogg_encoder_aspect_finish(&encoder->protected_->ogg_encoder_aspect); |
| 1365 | #endif |
| 1366 | |
| 1367 | free_(encoder); |
| 1368 | set_defaults_(encoder); |
| 1369 | |
| 1370 | if(!error) |
| 1371 | encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED; |
| 1372 | |
| 1373 | return !error; |
| 1374 | } |
| 1375 | |
| 1376 | FLAC_API FLAC__bool FLAC__stream_encoder_set_ogg_serial_number(FLAC__StreamEncoder *encoder, long value) |
| 1377 | { |
| 1378 | FLAC__ASSERT(0 != encoder); |
| 1379 | FLAC__ASSERT(0 != encoder->private_); |
| 1380 | FLAC__ASSERT(0 != encoder->protected_); |
| 1381 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1382 | return false; |
| 1383 | #if FLAC__HAS_OGG |
| 1384 | /* can't check encoder->private_->is_ogg since that's not set until init time */ |
| 1385 | FLAC__ogg_encoder_aspect_set_serial_number(&encoder->protected_->ogg_encoder_aspect, value); |
| 1386 | return true; |
| 1387 | #else |
| 1388 | (void)value; |
| 1389 | return false; |
| 1390 | #endif |
| 1391 | } |
| 1392 | |
| 1393 | FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value) |
| 1394 | { |
| 1395 | FLAC__ASSERT(0 != encoder); |
| 1396 | FLAC__ASSERT(0 != encoder->private_); |
| 1397 | FLAC__ASSERT(0 != encoder->protected_); |
| 1398 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1399 | return false; |
| 1400 | #ifndef FLAC__MANDATORY_VERIFY_WHILE_ENCODING |
| 1401 | encoder->protected_->verify = value; |
| 1402 | #endif |
| 1403 | return true; |
| 1404 | } |
| 1405 | |
| 1406 | FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value) |
| 1407 | { |
| 1408 | FLAC__ASSERT(0 != encoder); |
| 1409 | FLAC__ASSERT(0 != encoder->private_); |
| 1410 | FLAC__ASSERT(0 != encoder->protected_); |
| 1411 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1412 | return false; |
| 1413 | encoder->protected_->streamable_subset = value; |
| 1414 | return true; |
| 1415 | } |
| 1416 | |
| 1417 | FLAC_API FLAC__bool FLAC__stream_encoder_set_do_md5(FLAC__StreamEncoder *encoder, FLAC__bool value) |
| 1418 | { |
| 1419 | FLAC__ASSERT(0 != encoder); |
| 1420 | FLAC__ASSERT(0 != encoder->private_); |
| 1421 | FLAC__ASSERT(0 != encoder->protected_); |
| 1422 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1423 | return false; |
| 1424 | encoder->protected_->do_md5 = value; |
| 1425 | return true; |
| 1426 | } |
| 1427 | |
| 1428 | FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value) |
| 1429 | { |
| 1430 | FLAC__ASSERT(0 != encoder); |
| 1431 | FLAC__ASSERT(0 != encoder->private_); |
| 1432 | FLAC__ASSERT(0 != encoder->protected_); |
| 1433 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1434 | return false; |
| 1435 | encoder->protected_->channels = value; |
| 1436 | return true; |
| 1437 | } |
| 1438 | |
| 1439 | FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value) |
| 1440 | { |
| 1441 | FLAC__ASSERT(0 != encoder); |
| 1442 | FLAC__ASSERT(0 != encoder->private_); |
| 1443 | FLAC__ASSERT(0 != encoder->protected_); |
| 1444 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1445 | return false; |
| 1446 | encoder->protected_->bits_per_sample = value; |
| 1447 | return true; |
| 1448 | } |
| 1449 | |
| 1450 | FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value) |
| 1451 | { |
| 1452 | FLAC__ASSERT(0 != encoder); |
| 1453 | FLAC__ASSERT(0 != encoder->private_); |
| 1454 | FLAC__ASSERT(0 != encoder->protected_); |
| 1455 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1456 | return false; |
| 1457 | encoder->protected_->sample_rate = value; |
| 1458 | return true; |
| 1459 | } |
| 1460 | |
| 1461 | FLAC_API FLAC__bool FLAC__stream_encoder_set_compression_level(FLAC__StreamEncoder *encoder, unsigned value) |
| 1462 | { |
| 1463 | FLAC__bool ok = true; |
| 1464 | FLAC__ASSERT(0 != encoder); |
| 1465 | FLAC__ASSERT(0 != encoder->private_); |
| 1466 | FLAC__ASSERT(0 != encoder->protected_); |
| 1467 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1468 | return false; |
| 1469 | if(value >= sizeof(compression_levels_)/sizeof(compression_levels_[0])) |
| 1470 | value = sizeof(compression_levels_)/sizeof(compression_levels_[0]) - 1; |
| 1471 | ok &= FLAC__stream_encoder_set_do_mid_side_stereo (encoder, compression_levels_[value].do_mid_side_stereo); |
| 1472 | ok &= FLAC__stream_encoder_set_loose_mid_side_stereo (encoder, compression_levels_[value].loose_mid_side_stereo); |
| 1473 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 1474 | #if 0 |
| 1475 | /* was: */ |
| 1476 | ok &= FLAC__stream_encoder_set_apodization (encoder, compression_levels_[value].apodization); |
| 1477 | /* but it's too hard to specify the string in a locale-specific way */ |
| 1478 | #else |
| 1479 | encoder->protected_->num_apodizations = 1; |
| 1480 | encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY; |
| 1481 | encoder->protected_->apodizations[0].parameters.tukey.p = 0.5; |
| 1482 | #endif |
| 1483 | #endif |
| 1484 | ok &= FLAC__stream_encoder_set_max_lpc_order (encoder, compression_levels_[value].max_lpc_order); |
| 1485 | ok &= FLAC__stream_encoder_set_qlp_coeff_precision (encoder, compression_levels_[value].qlp_coeff_precision); |
| 1486 | ok &= FLAC__stream_encoder_set_do_qlp_coeff_prec_search (encoder, compression_levels_[value].do_qlp_coeff_prec_search); |
| 1487 | ok &= FLAC__stream_encoder_set_do_escape_coding (encoder, compression_levels_[value].do_escape_coding); |
| 1488 | ok &= FLAC__stream_encoder_set_do_exhaustive_model_search (encoder, compression_levels_[value].do_exhaustive_model_search); |
| 1489 | ok &= FLAC__stream_encoder_set_min_residual_partition_order(encoder, compression_levels_[value].min_residual_partition_order); |
| 1490 | ok &= FLAC__stream_encoder_set_max_residual_partition_order(encoder, compression_levels_[value].max_residual_partition_order); |
| 1491 | ok &= FLAC__stream_encoder_set_rice_parameter_search_dist (encoder, compression_levels_[value].rice_parameter_search_dist); |
| 1492 | return ok; |
| 1493 | } |
| 1494 | |
| 1495 | FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value) |
| 1496 | { |
| 1497 | FLAC__ASSERT(0 != encoder); |
| 1498 | FLAC__ASSERT(0 != encoder->private_); |
| 1499 | FLAC__ASSERT(0 != encoder->protected_); |
| 1500 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1501 | return false; |
| 1502 | encoder->protected_->blocksize = value; |
| 1503 | return true; |
| 1504 | } |
| 1505 | |
| 1506 | FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value) |
| 1507 | { |
| 1508 | FLAC__ASSERT(0 != encoder); |
| 1509 | FLAC__ASSERT(0 != encoder->private_); |
| 1510 | FLAC__ASSERT(0 != encoder->protected_); |
| 1511 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1512 | return false; |
| 1513 | encoder->protected_->do_mid_side_stereo = value; |
| 1514 | return true; |
| 1515 | } |
| 1516 | |
| 1517 | FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value) |
| 1518 | { |
| 1519 | FLAC__ASSERT(0 != encoder); |
| 1520 | FLAC__ASSERT(0 != encoder->private_); |
| 1521 | FLAC__ASSERT(0 != encoder->protected_); |
| 1522 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1523 | return false; |
| 1524 | encoder->protected_->loose_mid_side_stereo = value; |
| 1525 | return true; |
| 1526 | } |
| 1527 | |
| 1528 | /*@@@@add to tests*/ |
| 1529 | FLAC_API FLAC__bool FLAC__stream_encoder_set_apodization(FLAC__StreamEncoder *encoder, const char *specification) |
| 1530 | { |
| 1531 | FLAC__ASSERT(0 != encoder); |
| 1532 | FLAC__ASSERT(0 != encoder->private_); |
| 1533 | FLAC__ASSERT(0 != encoder->protected_); |
| 1534 | FLAC__ASSERT(0 != specification); |
| 1535 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1536 | return false; |
| 1537 | #ifdef FLAC__INTEGER_ONLY_LIBRARY |
| 1538 | (void)specification; /* silently ignore since we haven't integerized; will always use a rectangular window */ |
| 1539 | #else |
| 1540 | encoder->protected_->num_apodizations = 0; |
| 1541 | while(1) { |
| 1542 | const char *s = strchr(specification, ';'); |
| 1543 | const size_t n = s? (size_t)(s - specification) : strlen(specification); |
| 1544 | if (n==8 && 0 == strncmp("bartlett" , specification, n)) |
| 1545 | encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT; |
| 1546 | else if(n==13 && 0 == strncmp("bartlett_hann", specification, n)) |
| 1547 | encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT_HANN; |
| 1548 | else if(n==8 && 0 == strncmp("blackman" , specification, n)) |
| 1549 | encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN; |
| 1550 | else if(n==26 && 0 == strncmp("blackman_harris_4term_92db", specification, n)) |
| 1551 | encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE; |
| 1552 | else if(n==6 && 0 == strncmp("connes" , specification, n)) |
| 1553 | encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_CONNES; |
| 1554 | else if(n==7 && 0 == strncmp("flattop" , specification, n)) |
| 1555 | encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_FLATTOP; |
| 1556 | else if(n>7 && 0 == strncmp("gauss(" , specification, 6)) { |
| 1557 | FLAC__real stddev = (FLAC__real)strtod(specification+6, 0); |
| 1558 | if (stddev > 0.0 && stddev <= 0.5) { |
| 1559 | encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.gauss.stddev = stddev; |
| 1560 | encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_GAUSS; |
| 1561 | } |
| 1562 | } |
| 1563 | else if(n==7 && 0 == strncmp("hamming" , specification, n)) |
| 1564 | encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HAMMING; |
| 1565 | else if(n==4 && 0 == strncmp("hann" , specification, n)) |
| 1566 | encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HANN; |
| 1567 | else if(n==13 && 0 == strncmp("kaiser_bessel", specification, n)) |
| 1568 | encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_KAISER_BESSEL; |
| 1569 | else if(n==7 && 0 == strncmp("nuttall" , specification, n)) |
| 1570 | encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_NUTTALL; |
| 1571 | else if(n==9 && 0 == strncmp("rectangle" , specification, n)) |
| 1572 | encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_RECTANGLE; |
| 1573 | else if(n==8 && 0 == strncmp("triangle" , specification, n)) |
| 1574 | encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TRIANGLE; |
| 1575 | else if(n>7 && 0 == strncmp("tukey(" , specification, 6)) { |
| 1576 | FLAC__real p = (FLAC__real)strtod(specification+6, 0); |
| 1577 | if (p >= 0.0 && p <= 1.0) { |
| 1578 | encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.tukey.p = p; |
| 1579 | encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TUKEY; |
| 1580 | } |
| 1581 | } |
| 1582 | else if(n==5 && 0 == strncmp("welch" , specification, n)) |
| 1583 | encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_WELCH; |
| 1584 | if (encoder->protected_->num_apodizations == 32) |
| 1585 | break; |
| 1586 | if (s) |
| 1587 | specification = s+1; |
| 1588 | else |
| 1589 | break; |
| 1590 | } |
| 1591 | if(encoder->protected_->num_apodizations == 0) { |
| 1592 | encoder->protected_->num_apodizations = 1; |
| 1593 | encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY; |
| 1594 | encoder->protected_->apodizations[0].parameters.tukey.p = 0.5; |
| 1595 | } |
| 1596 | #endif |
| 1597 | return true; |
| 1598 | } |
| 1599 | |
| 1600 | FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value) |
| 1601 | { |
| 1602 | FLAC__ASSERT(0 != encoder); |
| 1603 | FLAC__ASSERT(0 != encoder->private_); |
| 1604 | FLAC__ASSERT(0 != encoder->protected_); |
| 1605 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1606 | return false; |
| 1607 | encoder->protected_->max_lpc_order = value; |
| 1608 | return true; |
| 1609 | } |
| 1610 | |
| 1611 | FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value) |
| 1612 | { |
| 1613 | FLAC__ASSERT(0 != encoder); |
| 1614 | FLAC__ASSERT(0 != encoder->private_); |
| 1615 | FLAC__ASSERT(0 != encoder->protected_); |
| 1616 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1617 | return false; |
| 1618 | encoder->protected_->qlp_coeff_precision = value; |
| 1619 | return true; |
| 1620 | } |
| 1621 | |
| 1622 | FLAC_API FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value) |
| 1623 | { |
| 1624 | FLAC__ASSERT(0 != encoder); |
| 1625 | FLAC__ASSERT(0 != encoder->private_); |
| 1626 | FLAC__ASSERT(0 != encoder->protected_); |
| 1627 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1628 | return false; |
| 1629 | encoder->protected_->do_qlp_coeff_prec_search = value; |
| 1630 | return true; |
| 1631 | } |
| 1632 | |
| 1633 | FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value) |
| 1634 | { |
| 1635 | FLAC__ASSERT(0 != encoder); |
| 1636 | FLAC__ASSERT(0 != encoder->private_); |
| 1637 | FLAC__ASSERT(0 != encoder->protected_); |
| 1638 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1639 | return false; |
| 1640 | #if 0 |
| 1641 | /*@@@ deprecated: */ |
| 1642 | encoder->protected_->do_escape_coding = value; |
| 1643 | #else |
| 1644 | (void)value; |
| 1645 | #endif |
| 1646 | return true; |
| 1647 | } |
| 1648 | |
| 1649 | FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value) |
| 1650 | { |
| 1651 | FLAC__ASSERT(0 != encoder); |
| 1652 | FLAC__ASSERT(0 != encoder->private_); |
| 1653 | FLAC__ASSERT(0 != encoder->protected_); |
| 1654 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1655 | return false; |
| 1656 | encoder->protected_->do_exhaustive_model_search = value; |
| 1657 | return true; |
| 1658 | } |
| 1659 | |
| 1660 | FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value) |
| 1661 | { |
| 1662 | FLAC__ASSERT(0 != encoder); |
| 1663 | FLAC__ASSERT(0 != encoder->private_); |
| 1664 | FLAC__ASSERT(0 != encoder->protected_); |
| 1665 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1666 | return false; |
| 1667 | encoder->protected_->min_residual_partition_order = value; |
| 1668 | return true; |
| 1669 | } |
| 1670 | |
| 1671 | FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value) |
| 1672 | { |
| 1673 | FLAC__ASSERT(0 != encoder); |
| 1674 | FLAC__ASSERT(0 != encoder->private_); |
| 1675 | FLAC__ASSERT(0 != encoder->protected_); |
| 1676 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1677 | return false; |
| 1678 | encoder->protected_->max_residual_partition_order = value; |
| 1679 | return true; |
| 1680 | } |
| 1681 | |
| 1682 | FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value) |
| 1683 | { |
| 1684 | FLAC__ASSERT(0 != encoder); |
| 1685 | FLAC__ASSERT(0 != encoder->private_); |
| 1686 | FLAC__ASSERT(0 != encoder->protected_); |
| 1687 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1688 | return false; |
| 1689 | #if 0 |
| 1690 | /*@@@ deprecated: */ |
| 1691 | encoder->protected_->rice_parameter_search_dist = value; |
| 1692 | #else |
| 1693 | (void)value; |
| 1694 | #endif |
| 1695 | return true; |
| 1696 | } |
| 1697 | |
| 1698 | FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value) |
| 1699 | { |
| 1700 | FLAC__ASSERT(0 != encoder); |
| 1701 | FLAC__ASSERT(0 != encoder->private_); |
| 1702 | FLAC__ASSERT(0 != encoder->protected_); |
| 1703 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1704 | return false; |
| 1705 | encoder->protected_->total_samples_estimate = value; |
| 1706 | return true; |
| 1707 | } |
| 1708 | |
| 1709 | FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks) |
| 1710 | { |
| 1711 | FLAC__ASSERT(0 != encoder); |
| 1712 | FLAC__ASSERT(0 != encoder->private_); |
| 1713 | FLAC__ASSERT(0 != encoder->protected_); |
| 1714 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1715 | return false; |
| 1716 | if(0 == metadata) |
| 1717 | num_blocks = 0; |
| 1718 | if(0 == num_blocks) |
| 1719 | metadata = 0; |
| 1720 | /* realloc() does not do exactly what we want so... */ |
| 1721 | if(encoder->protected_->metadata) { |
| 1722 | free(encoder->protected_->metadata); |
| 1723 | encoder->protected_->metadata = 0; |
| 1724 | encoder->protected_->num_metadata_blocks = 0; |
| 1725 | } |
| 1726 | if(num_blocks) { |
| 1727 | FLAC__StreamMetadata **m; |
| 1728 | if(0 == (m = (FLAC__StreamMetadata**)safe_malloc_mul_2op_(sizeof(m[0]), /*times*/num_blocks))) |
| 1729 | return false; |
| 1730 | memcpy(m, metadata, sizeof(m[0]) * num_blocks); |
| 1731 | encoder->protected_->metadata = m; |
| 1732 | encoder->protected_->num_metadata_blocks = num_blocks; |
| 1733 | } |
| 1734 | #if FLAC__HAS_OGG |
| 1735 | if(!FLAC__ogg_encoder_aspect_set_num_metadata(&encoder->protected_->ogg_encoder_aspect, num_blocks)) |
| 1736 | return false; |
| 1737 | #endif |
| 1738 | return true; |
| 1739 | } |
| 1740 | |
| 1741 | /* |
| 1742 | * These three functions are not static, but not publically exposed in |
| 1743 | * include/FLAC/ either. They are used by the test suite. |
| 1744 | */ |
| 1745 | FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value) |
| 1746 | { |
| 1747 | FLAC__ASSERT(0 != encoder); |
| 1748 | FLAC__ASSERT(0 != encoder->private_); |
| 1749 | FLAC__ASSERT(0 != encoder->protected_); |
| 1750 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1751 | return false; |
| 1752 | encoder->private_->disable_constant_subframes = value; |
| 1753 | return true; |
| 1754 | } |
| 1755 | |
| 1756 | FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value) |
| 1757 | { |
| 1758 | FLAC__ASSERT(0 != encoder); |
| 1759 | FLAC__ASSERT(0 != encoder->private_); |
| 1760 | FLAC__ASSERT(0 != encoder->protected_); |
| 1761 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1762 | return false; |
| 1763 | encoder->private_->disable_fixed_subframes = value; |
| 1764 | return true; |
| 1765 | } |
| 1766 | |
| 1767 | FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value) |
| 1768 | { |
| 1769 | FLAC__ASSERT(0 != encoder); |
| 1770 | FLAC__ASSERT(0 != encoder->private_); |
| 1771 | FLAC__ASSERT(0 != encoder->protected_); |
| 1772 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1773 | return false; |
| 1774 | encoder->private_->disable_verbatim_subframes = value; |
| 1775 | return true; |
| 1776 | } |
| 1777 | |
| 1778 | FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder) |
| 1779 | { |
| 1780 | FLAC__ASSERT(0 != encoder); |
| 1781 | FLAC__ASSERT(0 != encoder->private_); |
| 1782 | FLAC__ASSERT(0 != encoder->protected_); |
| 1783 | return encoder->protected_->state; |
| 1784 | } |
| 1785 | |
| 1786 | FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder) |
| 1787 | { |
| 1788 | FLAC__ASSERT(0 != encoder); |
| 1789 | FLAC__ASSERT(0 != encoder->private_); |
| 1790 | FLAC__ASSERT(0 != encoder->protected_); |
| 1791 | if(encoder->protected_->verify) |
| 1792 | return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder); |
| 1793 | else |
| 1794 | return FLAC__STREAM_DECODER_UNINITIALIZED; |
| 1795 | } |
| 1796 | |
| 1797 | FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder) |
| 1798 | { |
| 1799 | FLAC__ASSERT(0 != encoder); |
| 1800 | FLAC__ASSERT(0 != encoder->private_); |
| 1801 | FLAC__ASSERT(0 != encoder->protected_); |
| 1802 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR) |
| 1803 | return FLAC__StreamEncoderStateString[encoder->protected_->state]; |
| 1804 | else |
| 1805 | return FLAC__stream_decoder_get_resolved_state_string(encoder->private_->verify.decoder); |
| 1806 | } |
| 1807 | |
| 1808 | FLAC_API void FLAC__stream_encoder_get_verify_decoder_error_stats(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_sample, unsigned *frame_number, unsigned *channel, unsigned *sample, FLAC__int32 *expected, FLAC__int32 *got) |
| 1809 | { |
| 1810 | FLAC__ASSERT(0 != encoder); |
| 1811 | FLAC__ASSERT(0 != encoder->private_); |
| 1812 | FLAC__ASSERT(0 != encoder->protected_); |
| 1813 | if(0 != absolute_sample) |
| 1814 | *absolute_sample = encoder->private_->verify.error_stats.absolute_sample; |
| 1815 | if(0 != frame_number) |
| 1816 | *frame_number = encoder->private_->verify.error_stats.frame_number; |
| 1817 | if(0 != channel) |
| 1818 | *channel = encoder->private_->verify.error_stats.channel; |
| 1819 | if(0 != sample) |
| 1820 | *sample = encoder->private_->verify.error_stats.sample; |
| 1821 | if(0 != expected) |
| 1822 | *expected = encoder->private_->verify.error_stats.expected; |
| 1823 | if(0 != got) |
| 1824 | *got = encoder->private_->verify.error_stats.got; |
| 1825 | } |
| 1826 | |
| 1827 | FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder) |
| 1828 | { |
| 1829 | FLAC__ASSERT(0 != encoder); |
| 1830 | FLAC__ASSERT(0 != encoder->private_); |
| 1831 | FLAC__ASSERT(0 != encoder->protected_); |
| 1832 | return encoder->protected_->verify; |
| 1833 | } |
| 1834 | |
| 1835 | FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder) |
| 1836 | { |
| 1837 | FLAC__ASSERT(0 != encoder); |
| 1838 | FLAC__ASSERT(0 != encoder->private_); |
| 1839 | FLAC__ASSERT(0 != encoder->protected_); |
| 1840 | return encoder->protected_->streamable_subset; |
| 1841 | } |
| 1842 | |
| 1843 | FLAC_API FLAC__bool FLAC__stream_encoder_get_do_md5(const FLAC__StreamEncoder *encoder) |
| 1844 | { |
| 1845 | FLAC__ASSERT(0 != encoder); |
| 1846 | FLAC__ASSERT(0 != encoder->private_); |
| 1847 | FLAC__ASSERT(0 != encoder->protected_); |
| 1848 | return encoder->protected_->do_md5; |
| 1849 | } |
| 1850 | |
| 1851 | FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder) |
| 1852 | { |
| 1853 | FLAC__ASSERT(0 != encoder); |
| 1854 | FLAC__ASSERT(0 != encoder->private_); |
| 1855 | FLAC__ASSERT(0 != encoder->protected_); |
| 1856 | return encoder->protected_->channels; |
| 1857 | } |
| 1858 | |
| 1859 | FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder) |
| 1860 | { |
| 1861 | FLAC__ASSERT(0 != encoder); |
| 1862 | FLAC__ASSERT(0 != encoder->private_); |
| 1863 | FLAC__ASSERT(0 != encoder->protected_); |
| 1864 | return encoder->protected_->bits_per_sample; |
| 1865 | } |
| 1866 | |
| 1867 | FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder) |
| 1868 | { |
| 1869 | FLAC__ASSERT(0 != encoder); |
| 1870 | FLAC__ASSERT(0 != encoder->private_); |
| 1871 | FLAC__ASSERT(0 != encoder->protected_); |
| 1872 | return encoder->protected_->sample_rate; |
| 1873 | } |
| 1874 | |
| 1875 | FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder) |
| 1876 | { |
| 1877 | FLAC__ASSERT(0 != encoder); |
| 1878 | FLAC__ASSERT(0 != encoder->private_); |
| 1879 | FLAC__ASSERT(0 != encoder->protected_); |
| 1880 | return encoder->protected_->blocksize; |
| 1881 | } |
| 1882 | |
| 1883 | FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder) |
| 1884 | { |
| 1885 | FLAC__ASSERT(0 != encoder); |
| 1886 | FLAC__ASSERT(0 != encoder->private_); |
| 1887 | FLAC__ASSERT(0 != encoder->protected_); |
| 1888 | return encoder->protected_->do_mid_side_stereo; |
| 1889 | } |
| 1890 | |
| 1891 | FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder) |
| 1892 | { |
| 1893 | FLAC__ASSERT(0 != encoder); |
| 1894 | FLAC__ASSERT(0 != encoder->private_); |
| 1895 | FLAC__ASSERT(0 != encoder->protected_); |
| 1896 | return encoder->protected_->loose_mid_side_stereo; |
| 1897 | } |
| 1898 | |
| 1899 | FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder) |
| 1900 | { |
| 1901 | FLAC__ASSERT(0 != encoder); |
| 1902 | FLAC__ASSERT(0 != encoder->private_); |
| 1903 | FLAC__ASSERT(0 != encoder->protected_); |
| 1904 | return encoder->protected_->max_lpc_order; |
| 1905 | } |
| 1906 | |
| 1907 | FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder) |
| 1908 | { |
| 1909 | FLAC__ASSERT(0 != encoder); |
| 1910 | FLAC__ASSERT(0 != encoder->private_); |
| 1911 | FLAC__ASSERT(0 != encoder->protected_); |
| 1912 | return encoder->protected_->qlp_coeff_precision; |
| 1913 | } |
| 1914 | |
| 1915 | FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder) |
| 1916 | { |
| 1917 | FLAC__ASSERT(0 != encoder); |
| 1918 | FLAC__ASSERT(0 != encoder->private_); |
| 1919 | FLAC__ASSERT(0 != encoder->protected_); |
| 1920 | return encoder->protected_->do_qlp_coeff_prec_search; |
| 1921 | } |
| 1922 | |
| 1923 | FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder) |
| 1924 | { |
| 1925 | FLAC__ASSERT(0 != encoder); |
| 1926 | FLAC__ASSERT(0 != encoder->private_); |
| 1927 | FLAC__ASSERT(0 != encoder->protected_); |
| 1928 | return encoder->protected_->do_escape_coding; |
| 1929 | } |
| 1930 | |
| 1931 | FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder) |
| 1932 | { |
| 1933 | FLAC__ASSERT(0 != encoder); |
| 1934 | FLAC__ASSERT(0 != encoder->private_); |
| 1935 | FLAC__ASSERT(0 != encoder->protected_); |
| 1936 | return encoder->protected_->do_exhaustive_model_search; |
| 1937 | } |
| 1938 | |
| 1939 | FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder) |
| 1940 | { |
| 1941 | FLAC__ASSERT(0 != encoder); |
| 1942 | FLAC__ASSERT(0 != encoder->private_); |
| 1943 | FLAC__ASSERT(0 != encoder->protected_); |
| 1944 | return encoder->protected_->min_residual_partition_order; |
| 1945 | } |
| 1946 | |
| 1947 | FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder) |
| 1948 | { |
| 1949 | FLAC__ASSERT(0 != encoder); |
| 1950 | FLAC__ASSERT(0 != encoder->private_); |
| 1951 | FLAC__ASSERT(0 != encoder->protected_); |
| 1952 | return encoder->protected_->max_residual_partition_order; |
| 1953 | } |
| 1954 | |
| 1955 | FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder) |
| 1956 | { |
| 1957 | FLAC__ASSERT(0 != encoder); |
| 1958 | FLAC__ASSERT(0 != encoder->private_); |
| 1959 | FLAC__ASSERT(0 != encoder->protected_); |
| 1960 | return encoder->protected_->rice_parameter_search_dist; |
| 1961 | } |
| 1962 | |
| 1963 | FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder) |
| 1964 | { |
| 1965 | FLAC__ASSERT(0 != encoder); |
| 1966 | FLAC__ASSERT(0 != encoder->private_); |
| 1967 | FLAC__ASSERT(0 != encoder->protected_); |
| 1968 | return encoder->protected_->total_samples_estimate; |
| 1969 | } |
| 1970 | |
| 1971 | FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples) |
| 1972 | { |
| 1973 | unsigned i, j = 0, channel; |
| 1974 | const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize; |
| 1975 | |
| 1976 | FLAC__ASSERT(0 != encoder); |
| 1977 | FLAC__ASSERT(0 != encoder->private_); |
| 1978 | FLAC__ASSERT(0 != encoder->protected_); |
| 1979 | FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK); |
| 1980 | |
| 1981 | do { |
| 1982 | const unsigned n = min(blocksize+OVERREAD_-encoder->private_->current_sample_number, samples-j); |
| 1983 | |
| 1984 | if(encoder->protected_->verify) |
| 1985 | append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, n); |
| 1986 | |
| 1987 | for(channel = 0; channel < channels; channel++) |
| 1988 | memcpy(&encoder->private_->integer_signal[channel][encoder->private_->current_sample_number], &buffer[channel][j], sizeof(buffer[channel][0]) * n); |
| 1989 | |
| 1990 | if(encoder->protected_->do_mid_side_stereo) { |
| 1991 | FLAC__ASSERT(channels == 2); |
| 1992 | /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */ |
| 1993 | for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) { |
| 1994 | encoder->private_->integer_signal_mid_side[1][i] = buffer[0][j] - buffer[1][j]; |
| 1995 | encoder->private_->integer_signal_mid_side[0][i] = (buffer[0][j] + buffer[1][j]) >> 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */ |
| 1996 | } |
| 1997 | } |
| 1998 | else |
| 1999 | j += n; |
| 2000 | |
| 2001 | encoder->private_->current_sample_number += n; |
| 2002 | |
| 2003 | /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */ |
| 2004 | if(encoder->private_->current_sample_number > blocksize) { |
| 2005 | FLAC__ASSERT(encoder->private_->current_sample_number == blocksize+OVERREAD_); |
| 2006 | FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */ |
| 2007 | if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false)) |
| 2008 | return false; |
| 2009 | /* move unprocessed overread samples to beginnings of arrays */ |
| 2010 | for(channel = 0; channel < channels; channel++) |
| 2011 | encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][blocksize]; |
| 2012 | if(encoder->protected_->do_mid_side_stereo) { |
| 2013 | encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][blocksize]; |
| 2014 | encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][blocksize]; |
| 2015 | } |
| 2016 | encoder->private_->current_sample_number = 1; |
| 2017 | } |
| 2018 | } while(j < samples); |
| 2019 | |
| 2020 | return true; |
| 2021 | } |
| 2022 | |
| 2023 | FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples) |
| 2024 | { |
| 2025 | unsigned i, j, k, channel; |
| 2026 | FLAC__int32 x, mid, side; |
| 2027 | const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize; |
| 2028 | |
| 2029 | FLAC__ASSERT(0 != encoder); |
| 2030 | FLAC__ASSERT(0 != encoder->private_); |
| 2031 | FLAC__ASSERT(0 != encoder->protected_); |
| 2032 | FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK); |
| 2033 | |
| 2034 | j = k = 0; |
| 2035 | /* |
| 2036 | * we have several flavors of the same basic loop, optimized for |
| 2037 | * different conditions: |
| 2038 | */ |
| 2039 | if(encoder->protected_->do_mid_side_stereo && channels == 2) { |
| 2040 | /* |
| 2041 | * stereo coding: unroll channel loop |
| 2042 | */ |
| 2043 | do { |
| 2044 | if(encoder->protected_->verify) |
| 2045 | append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+OVERREAD_-encoder->private_->current_sample_number, samples-j)); |
| 2046 | |
| 2047 | /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */ |
| 2048 | for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) { |
| 2049 | encoder->private_->integer_signal[0][i] = mid = side = buffer[k++]; |
| 2050 | x = buffer[k++]; |
| 2051 | encoder->private_->integer_signal[1][i] = x; |
| 2052 | mid += x; |
| 2053 | side -= x; |
| 2054 | mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */ |
| 2055 | encoder->private_->integer_signal_mid_side[1][i] = side; |
| 2056 | encoder->private_->integer_signal_mid_side[0][i] = mid; |
| 2057 | } |
| 2058 | encoder->private_->current_sample_number = i; |
| 2059 | /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */ |
| 2060 | if(i > blocksize) { |
| 2061 | if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false)) |
| 2062 | return false; |
| 2063 | /* move unprocessed overread samples to beginnings of arrays */ |
| 2064 | FLAC__ASSERT(i == blocksize+OVERREAD_); |
| 2065 | FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */ |
| 2066 | encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][blocksize]; |
| 2067 | encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][blocksize]; |
| 2068 | encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][blocksize]; |
| 2069 | encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][blocksize]; |
| 2070 | encoder->private_->current_sample_number = 1; |
| 2071 | } |
| 2072 | } while(j < samples); |
| 2073 | } |
| 2074 | else { |
| 2075 | /* |
| 2076 | * independent channel coding: buffer each channel in inner loop |
| 2077 | */ |
| 2078 | do { |
| 2079 | if(encoder->protected_->verify) |
| 2080 | append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+OVERREAD_-encoder->private_->current_sample_number, samples-j)); |
| 2081 | |
| 2082 | /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */ |
| 2083 | for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) { |
| 2084 | for(channel = 0; channel < channels; channel++) |
| 2085 | encoder->private_->integer_signal[channel][i] = buffer[k++]; |
| 2086 | } |
| 2087 | encoder->private_->current_sample_number = i; |
| 2088 | /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */ |
| 2089 | if(i > blocksize) { |
| 2090 | if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false)) |
| 2091 | return false; |
| 2092 | /* move unprocessed overread samples to beginnings of arrays */ |
| 2093 | FLAC__ASSERT(i == blocksize+OVERREAD_); |
| 2094 | FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */ |
| 2095 | for(channel = 0; channel < channels; channel++) |
| 2096 | encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][blocksize]; |
| 2097 | encoder->private_->current_sample_number = 1; |
| 2098 | } |
| 2099 | } while(j < samples); |
| 2100 | } |
| 2101 | |
| 2102 | return true; |
| 2103 | } |
| 2104 | |
| 2105 | /*********************************************************************** |
| 2106 | * |
| 2107 | * Private class methods |
| 2108 | * |
| 2109 | ***********************************************************************/ |
| 2110 | |
| 2111 | void set_defaults_(FLAC__StreamEncoder *encoder) |
| 2112 | { |
| 2113 | FLAC__ASSERT(0 != encoder); |
| 2114 | |
| 2115 | #ifdef FLAC__MANDATORY_VERIFY_WHILE_ENCODING |
| 2116 | encoder->protected_->verify = true; |
| 2117 | #else |
| 2118 | encoder->protected_->verify = false; |
| 2119 | #endif |
| 2120 | encoder->protected_->streamable_subset = true; |
| 2121 | encoder->protected_->do_md5 = true; |
| 2122 | encoder->protected_->do_mid_side_stereo = false; |
| 2123 | encoder->protected_->loose_mid_side_stereo = false; |
| 2124 | encoder->protected_->channels = 2; |
| 2125 | encoder->protected_->bits_per_sample = 16; |
| 2126 | encoder->protected_->sample_rate = 44100; |
| 2127 | encoder->protected_->blocksize = 0; |
| 2128 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 2129 | encoder->protected_->num_apodizations = 1; |
| 2130 | encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY; |
| 2131 | encoder->protected_->apodizations[0].parameters.tukey.p = 0.5; |
| 2132 | #endif |
| 2133 | encoder->protected_->max_lpc_order = 0; |
| 2134 | encoder->protected_->qlp_coeff_precision = 0; |
| 2135 | encoder->protected_->do_qlp_coeff_prec_search = false; |
| 2136 | encoder->protected_->do_exhaustive_model_search = false; |
| 2137 | encoder->protected_->do_escape_coding = false; |
| 2138 | encoder->protected_->min_residual_partition_order = 0; |
| 2139 | encoder->protected_->max_residual_partition_order = 0; |
| 2140 | encoder->protected_->rice_parameter_search_dist = 0; |
| 2141 | encoder->protected_->total_samples_estimate = 0; |
| 2142 | encoder->protected_->metadata = 0; |
| 2143 | encoder->protected_->num_metadata_blocks = 0; |
| 2144 | |
| 2145 | encoder->private_->seek_table = 0; |
| 2146 | encoder->private_->disable_constant_subframes = false; |
| 2147 | encoder->private_->disable_fixed_subframes = false; |
| 2148 | encoder->private_->disable_verbatim_subframes = false; |
| 2149 | #if FLAC__HAS_OGG |
| 2150 | encoder->private_->is_ogg = false; |
| 2151 | #endif |
| 2152 | encoder->private_->read_callback = 0; |
| 2153 | encoder->private_->write_callback = 0; |
| 2154 | encoder->private_->seek_callback = 0; |
| 2155 | encoder->private_->tell_callback = 0; |
| 2156 | encoder->private_->metadata_callback = 0; |
| 2157 | encoder->private_->progress_callback = 0; |
| 2158 | encoder->private_->client_data = 0; |
| 2159 | |
| 2160 | #if FLAC__HAS_OGG |
| 2161 | FLAC__ogg_encoder_aspect_set_defaults(&encoder->protected_->ogg_encoder_aspect); |
| 2162 | #endif |
| 2163 | } |
| 2164 | |
| 2165 | void free_(FLAC__StreamEncoder *encoder) |
| 2166 | { |
| 2167 | unsigned i, channel; |
| 2168 | |
| 2169 | FLAC__ASSERT(0 != encoder); |
| 2170 | if(encoder->protected_->metadata) { |
| 2171 | free(encoder->protected_->metadata); |
| 2172 | encoder->protected_->metadata = 0; |
| 2173 | encoder->protected_->num_metadata_blocks = 0; |
| 2174 | } |
| 2175 | for(i = 0; i < encoder->protected_->channels; i++) { |
| 2176 | if(0 != encoder->private_->integer_signal_unaligned[i]) { |
| 2177 | free(encoder->private_->integer_signal_unaligned[i]); |
| 2178 | encoder->private_->integer_signal_unaligned[i] = 0; |
| 2179 | } |
| 2180 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 2181 | if(0 != encoder->private_->real_signal_unaligned[i]) { |
| 2182 | free(encoder->private_->real_signal_unaligned[i]); |
| 2183 | encoder->private_->real_signal_unaligned[i] = 0; |
| 2184 | } |
| 2185 | #endif |
| 2186 | } |
| 2187 | for(i = 0; i < 2; i++) { |
| 2188 | if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) { |
| 2189 | free(encoder->private_->integer_signal_mid_side_unaligned[i]); |
| 2190 | encoder->private_->integer_signal_mid_side_unaligned[i] = 0; |
| 2191 | } |
| 2192 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 2193 | if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) { |
| 2194 | free(encoder->private_->real_signal_mid_side_unaligned[i]); |
| 2195 | encoder->private_->real_signal_mid_side_unaligned[i] = 0; |
| 2196 | } |
| 2197 | #endif |
| 2198 | } |
| 2199 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 2200 | for(i = 0; i < encoder->protected_->num_apodizations; i++) { |
| 2201 | if(0 != encoder->private_->window_unaligned[i]) { |
| 2202 | free(encoder->private_->window_unaligned[i]); |
| 2203 | encoder->private_->window_unaligned[i] = 0; |
| 2204 | } |
| 2205 | } |
| 2206 | if(0 != encoder->private_->windowed_signal_unaligned) { |
| 2207 | free(encoder->private_->windowed_signal_unaligned); |
| 2208 | encoder->private_->windowed_signal_unaligned = 0; |
| 2209 | } |
| 2210 | #endif |
| 2211 | for(channel = 0; channel < encoder->protected_->channels; channel++) { |
| 2212 | for(i = 0; i < 2; i++) { |
| 2213 | if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) { |
| 2214 | free(encoder->private_->residual_workspace_unaligned[channel][i]); |
| 2215 | encoder->private_->residual_workspace_unaligned[channel][i] = 0; |
| 2216 | } |
| 2217 | } |
| 2218 | } |
| 2219 | for(channel = 0; channel < 2; channel++) { |
| 2220 | for(i = 0; i < 2; i++) { |
| 2221 | if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) { |
| 2222 | free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]); |
| 2223 | encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0; |
| 2224 | } |
| 2225 | } |
| 2226 | } |
| 2227 | if(0 != encoder->private_->abs_residual_partition_sums_unaligned) { |
| 2228 | free(encoder->private_->abs_residual_partition_sums_unaligned); |
| 2229 | encoder->private_->abs_residual_partition_sums_unaligned = 0; |
| 2230 | } |
| 2231 | if(0 != encoder->private_->raw_bits_per_partition_unaligned) { |
| 2232 | free(encoder->private_->raw_bits_per_partition_unaligned); |
| 2233 | encoder->private_->raw_bits_per_partition_unaligned = 0; |
| 2234 | } |
| 2235 | if(encoder->protected_->verify) { |
| 2236 | for(i = 0; i < encoder->protected_->channels; i++) { |
| 2237 | if(0 != encoder->private_->verify.input_fifo.data[i]) { |
| 2238 | free(encoder->private_->verify.input_fifo.data[i]); |
| 2239 | encoder->private_->verify.input_fifo.data[i] = 0; |
| 2240 | } |
| 2241 | } |
| 2242 | } |
| 2243 | FLAC__bitwriter_free(encoder->private_->frame); |
| 2244 | } |
| 2245 | |
| 2246 | FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_blocksize) |
| 2247 | { |
| 2248 | FLAC__bool ok; |
| 2249 | unsigned i, channel; |
| 2250 | |
| 2251 | FLAC__ASSERT(new_blocksize > 0); |
| 2252 | FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK); |
| 2253 | FLAC__ASSERT(encoder->private_->current_sample_number == 0); |
| 2254 | |
| 2255 | /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */ |
| 2256 | if(new_blocksize <= encoder->private_->input_capacity) |
| 2257 | return true; |
| 2258 | |
| 2259 | ok = true; |
| 2260 | |
| 2261 | /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx() |
| 2262 | * requires that the input arrays (in our case the integer signals) |
| 2263 | * have a buffer of up to 3 zeroes in front (at negative indices) for |
| 2264 | * alignment purposes; we use 4 in front to keep the data well-aligned. |
| 2265 | */ |
| 2266 | |
| 2267 | for(i = 0; ok && i < encoder->protected_->channels; i++) { |
| 2268 | ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize+4+OVERREAD_, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]); |
| 2269 | memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4); |
| 2270 | encoder->private_->integer_signal[i] += 4; |
| 2271 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 2272 | #if 0 /* @@@ currently unused */ |
| 2273 | if(encoder->protected_->max_lpc_order > 0) |
| 2274 | ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize+OVERREAD_, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]); |
| 2275 | #endif |
| 2276 | #endif |
| 2277 | } |
| 2278 | for(i = 0; ok && i < 2; i++) { |
| 2279 | ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize+4+OVERREAD_, &encoder->private_->integer_signal_mid_side_unaligned[i], &encoder->private_->integer_signal_mid_side[i]); |
| 2280 | memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4); |
| 2281 | encoder->private_->integer_signal_mid_side[i] += 4; |
| 2282 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 2283 | #if 0 /* @@@ currently unused */ |
| 2284 | if(encoder->protected_->max_lpc_order > 0) |
| 2285 | ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize+OVERREAD_, &encoder->private_->real_signal_mid_side_unaligned[i], &encoder->private_->real_signal_mid_side[i]); |
| 2286 | #endif |
| 2287 | #endif |
| 2288 | } |
| 2289 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 2290 | if(ok && encoder->protected_->max_lpc_order > 0) { |
| 2291 | for(i = 0; ok && i < encoder->protected_->num_apodizations; i++) |
| 2292 | ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize, &encoder->private_->window_unaligned[i], &encoder->private_->window[i]); |
| 2293 | ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize, &encoder->private_->windowed_signal_unaligned, &encoder->private_->windowed_signal); |
| 2294 | } |
| 2295 | #endif |
| 2296 | for(channel = 0; ok && channel < encoder->protected_->channels; channel++) { |
| 2297 | for(i = 0; ok && i < 2; i++) { |
| 2298 | ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]); |
| 2299 | } |
| 2300 | } |
| 2301 | for(channel = 0; ok && channel < 2; channel++) { |
| 2302 | for(i = 0; ok && i < 2; i++) { |
| 2303 | ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize, &encoder->private_->residual_workspace_mid_side_unaligned[channel][i], &encoder->private_->residual_workspace_mid_side[channel][i]); |
| 2304 | } |
| 2305 | } |
| 2306 | /* the *2 is an approximation to the series 1 + 1/2 + 1/4 + ... that sums tree occupies in a flat array */ |
| 2307 | /*@@@ new_blocksize*2 is too pessimistic, but to fix, we need smarter logic because a smaller new_blocksize can actually increase the # of partitions; would require moving this out into a separate function, then checking its capacity against the need of the current blocksize&min/max_partition_order (and maybe predictor order) */ |
| 2308 | ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_blocksize * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums); |
| 2309 | if(encoder->protected_->do_escape_coding) |
| 2310 | ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_blocksize * 2, &encoder->private_->raw_bits_per_partition_unaligned, &encoder->private_->raw_bits_per_partition); |
| 2311 | |
| 2312 | /* now adjust the windows if the blocksize has changed */ |
| 2313 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 2314 | if(ok && new_blocksize != encoder->private_->input_capacity && encoder->protected_->max_lpc_order > 0) { |
| 2315 | for(i = 0; ok && i < encoder->protected_->num_apodizations; i++) { |
| 2316 | switch(encoder->protected_->apodizations[i].type) { |
| 2317 | case FLAC__APODIZATION_BARTLETT: |
| 2318 | FLAC__window_bartlett(encoder->private_->window[i], new_blocksize); |
| 2319 | break; |
| 2320 | case FLAC__APODIZATION_BARTLETT_HANN: |
| 2321 | FLAC__window_bartlett_hann(encoder->private_->window[i], new_blocksize); |
| 2322 | break; |
| 2323 | case FLAC__APODIZATION_BLACKMAN: |
| 2324 | FLAC__window_blackman(encoder->private_->window[i], new_blocksize); |
| 2325 | break; |
| 2326 | case FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE: |
| 2327 | FLAC__window_blackman_harris_4term_92db_sidelobe(encoder->private_->window[i], new_blocksize); |
| 2328 | break; |
| 2329 | case FLAC__APODIZATION_CONNES: |
| 2330 | FLAC__window_connes(encoder->private_->window[i], new_blocksize); |
| 2331 | break; |
| 2332 | case FLAC__APODIZATION_FLATTOP: |
| 2333 | FLAC__window_flattop(encoder->private_->window[i], new_blocksize); |
| 2334 | break; |
| 2335 | case FLAC__APODIZATION_GAUSS: |
| 2336 | FLAC__window_gauss(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.gauss.stddev); |
| 2337 | break; |
| 2338 | case FLAC__APODIZATION_HAMMING: |
| 2339 | FLAC__window_hamming(encoder->private_->window[i], new_blocksize); |
| 2340 | break; |
| 2341 | case FLAC__APODIZATION_HANN: |
| 2342 | FLAC__window_hann(encoder->private_->window[i], new_blocksize); |
| 2343 | break; |
| 2344 | case FLAC__APODIZATION_KAISER_BESSEL: |
| 2345 | FLAC__window_kaiser_bessel(encoder->private_->window[i], new_blocksize); |
| 2346 | break; |
| 2347 | case FLAC__APODIZATION_NUTTALL: |
| 2348 | FLAC__window_nuttall(encoder->private_->window[i], new_blocksize); |
| 2349 | break; |
| 2350 | case FLAC__APODIZATION_RECTANGLE: |
| 2351 | FLAC__window_rectangle(encoder->private_->window[i], new_blocksize); |
| 2352 | break; |
| 2353 | case FLAC__APODIZATION_TRIANGLE: |
| 2354 | FLAC__window_triangle(encoder->private_->window[i], new_blocksize); |
| 2355 | break; |
| 2356 | case FLAC__APODIZATION_TUKEY: |
| 2357 | FLAC__window_tukey(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.tukey.p); |
| 2358 | break; |
| 2359 | case FLAC__APODIZATION_WELCH: |
| 2360 | FLAC__window_welch(encoder->private_->window[i], new_blocksize); |
| 2361 | break; |
| 2362 | default: |
| 2363 | FLAC__ASSERT(0); |
| 2364 | /* double protection */ |
| 2365 | FLAC__window_hann(encoder->private_->window[i], new_blocksize); |
| 2366 | break; |
| 2367 | } |
| 2368 | } |
| 2369 | } |
| 2370 | #endif |
| 2371 | |
| 2372 | if(ok) |
| 2373 | encoder->private_->input_capacity = new_blocksize; |
| 2374 | else |
| 2375 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 2376 | |
| 2377 | return ok; |
| 2378 | } |
| 2379 | |
| 2380 | FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples, FLAC__bool is_last_block) |
| 2381 | { |
| 2382 | const FLAC__byte *buffer; |
| 2383 | size_t bytes; |
| 2384 | |
| 2385 | FLAC__ASSERT(FLAC__bitwriter_is_byte_aligned(encoder->private_->frame)); |
| 2386 | |
| 2387 | if(!FLAC__bitwriter_get_buffer(encoder->private_->frame, &buffer, &bytes)) { |
| 2388 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 2389 | return false; |
| 2390 | } |
| 2391 | |
| 2392 | if(encoder->protected_->verify) { |
| 2393 | encoder->private_->verify.output.data = buffer; |
| 2394 | encoder->private_->verify.output.bytes = bytes; |
| 2395 | if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) { |
| 2396 | encoder->private_->verify.needs_magic_hack = true; |
| 2397 | } |
| 2398 | else { |
| 2399 | if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) { |
| 2400 | FLAC__bitwriter_release_buffer(encoder->private_->frame); |
| 2401 | FLAC__bitwriter_clear(encoder->private_->frame); |
| 2402 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA) |
| 2403 | encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR; |
| 2404 | return false; |
| 2405 | } |
| 2406 | } |
| 2407 | } |
| 2408 | |
| 2409 | if(write_frame_(encoder, buffer, bytes, samples, is_last_block) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) { |
| 2410 | FLAC__bitwriter_release_buffer(encoder->private_->frame); |
| 2411 | FLAC__bitwriter_clear(encoder->private_->frame); |
| 2412 | encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR; |
| 2413 | return false; |
| 2414 | } |
| 2415 | |
| 2416 | FLAC__bitwriter_release_buffer(encoder->private_->frame); |
| 2417 | FLAC__bitwriter_clear(encoder->private_->frame); |
| 2418 | |
| 2419 | if(samples > 0) { |
| 2420 | encoder->private_->streaminfo.data.stream_info.min_framesize = min(bytes, encoder->private_->streaminfo.data.stream_info.min_framesize); |
| 2421 | encoder->private_->streaminfo.data.stream_info.max_framesize = max(bytes, encoder->private_->streaminfo.data.stream_info.max_framesize); |
| 2422 | } |
| 2423 | |
| 2424 | return true; |
| 2425 | } |
| 2426 | |
| 2427 | FLAC__StreamEncoderWriteStatus write_frame_(FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, FLAC__bool is_last_block) |
| 2428 | { |
| 2429 | FLAC__StreamEncoderWriteStatus status; |
| 2430 | FLAC__uint64 output_position = 0; |
| 2431 | |
| 2432 | /* FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED just means we didn't get the offset; no error */ |
| 2433 | if(encoder->private_->tell_callback && encoder->private_->tell_callback(encoder, &output_position, encoder->private_->client_data) == FLAC__STREAM_ENCODER_TELL_STATUS_ERROR) { |
| 2434 | encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR; |
| 2435 | return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR; |
| 2436 | } |
| 2437 | |
| 2438 | /* |
| 2439 | * Watch for the STREAMINFO block and first SEEKTABLE block to go by and store their offsets. |
| 2440 | */ |
| 2441 | if(samples == 0) { |
| 2442 | FLAC__MetadataType type = (buffer[0] & 0x7f); |
| 2443 | if(type == FLAC__METADATA_TYPE_STREAMINFO) |
| 2444 | encoder->protected_->streaminfo_offset = output_position; |
| 2445 | else if(type == FLAC__METADATA_TYPE_SEEKTABLE && encoder->protected_->seektable_offset == 0) |
| 2446 | encoder->protected_->seektable_offset = output_position; |
| 2447 | } |
| 2448 | |
| 2449 | /* |
| 2450 | * Mark the current seek point if hit (if audio_offset == 0 that |
| 2451 | * means we're still writing metadata and haven't hit the first |
| 2452 | * frame yet) |
| 2453 | */ |
| 2454 | if(0 != encoder->private_->seek_table && encoder->protected_->audio_offset > 0 && encoder->private_->seek_table->num_points > 0) { |
| 2455 | const unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder); |
| 2456 | const FLAC__uint64 frame_first_sample = encoder->private_->samples_written; |
| 2457 | const FLAC__uint64 frame_last_sample = frame_first_sample + (FLAC__uint64)blocksize - 1; |
| 2458 | FLAC__uint64 test_sample; |
| 2459 | unsigned i; |
| 2460 | for(i = encoder->private_->first_seekpoint_to_check; i < encoder->private_->seek_table->num_points; i++) { |
| 2461 | test_sample = encoder->private_->seek_table->points[i].sample_number; |
| 2462 | if(test_sample > frame_last_sample) { |
| 2463 | break; |
| 2464 | } |
| 2465 | else if(test_sample >= frame_first_sample) { |
| 2466 | encoder->private_->seek_table->points[i].sample_number = frame_first_sample; |
| 2467 | encoder->private_->seek_table->points[i].stream_offset = output_position - encoder->protected_->audio_offset; |
| 2468 | encoder->private_->seek_table->points[i].frame_samples = blocksize; |
| 2469 | encoder->private_->first_seekpoint_to_check++; |
| 2470 | /* DO NOT: "break;" and here's why: |
| 2471 | * The seektable template may contain more than one target |
| 2472 | * sample for any given frame; we will keep looping, generating |
| 2473 | * duplicate seekpoints for them, and we'll clean it up later, |
| 2474 | * just before writing the seektable back to the metadata. |
| 2475 | */ |
| 2476 | } |
| 2477 | else { |
| 2478 | encoder->private_->first_seekpoint_to_check++; |
| 2479 | } |
| 2480 | } |
| 2481 | } |
| 2482 | |
| 2483 | #if FLAC__HAS_OGG |
| 2484 | if(encoder->private_->is_ogg) { |
| 2485 | status = FLAC__ogg_encoder_aspect_write_callback_wrapper( |
| 2486 | &encoder->protected_->ogg_encoder_aspect, |
| 2487 | buffer, |
| 2488 | bytes, |
| 2489 | samples, |
| 2490 | encoder->private_->current_frame_number, |
| 2491 | is_last_block, |
| 2492 | (FLAC__OggEncoderAspectWriteCallbackProxy)encoder->private_->write_callback, |
| 2493 | encoder, |
| 2494 | encoder->private_->client_data |
| 2495 | ); |
| 2496 | } |
| 2497 | else |
| 2498 | #endif |
| 2499 | status = encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data); |
| 2500 | |
| 2501 | if(status == FLAC__STREAM_ENCODER_WRITE_STATUS_OK) { |
| 2502 | encoder->private_->bytes_written += bytes; |
| 2503 | encoder->private_->samples_written += samples; |
| 2504 | /* we keep a high watermark on the number of frames written because |
| 2505 | * when the encoder goes back to write metadata, 'current_frame' |
| 2506 | * will drop back to 0. |
| 2507 | */ |
| 2508 | encoder->private_->frames_written = max(encoder->private_->frames_written, encoder->private_->current_frame_number+1); |
| 2509 | } |
| 2510 | else |
| 2511 | encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR; |
| 2512 | |
| 2513 | return status; |
| 2514 | } |
| 2515 | |
| 2516 | /* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */ |
| 2517 | void update_metadata_(const FLAC__StreamEncoder *encoder) |
| 2518 | { |
| 2519 | FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)]; |
| 2520 | const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo; |
| 2521 | const FLAC__uint64 samples = metadata->data.stream_info.total_samples; |
| 2522 | const unsigned min_framesize = metadata->data.stream_info.min_framesize; |
| 2523 | const unsigned max_framesize = metadata->data.stream_info.max_framesize; |
| 2524 | const unsigned bps = metadata->data.stream_info.bits_per_sample; |
| 2525 | FLAC__StreamEncoderSeekStatus seek_status; |
| 2526 | |
| 2527 | FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO); |
| 2528 | |
| 2529 | /* All this is based on intimate knowledge of the stream header |
| 2530 | * layout, but a change to the header format that would break this |
| 2531 | * would also break all streams encoded in the previous format. |
| 2532 | */ |
| 2533 | |
| 2534 | /* |
| 2535 | * Write MD5 signature |
| 2536 | */ |
| 2537 | { |
| 2538 | const unsigned md5_offset = |
| 2539 | FLAC__STREAM_METADATA_HEADER_LENGTH + |
| 2540 | ( |
| 2541 | FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN + |
| 2542 | FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN + |
| 2543 | FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN + |
| 2544 | FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN + |
| 2545 | FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN + |
| 2546 | FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN + |
| 2547 | FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN + |
| 2548 | FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN |
| 2549 | ) / 8; |
| 2550 | |
| 2551 | if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + md5_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) { |
| 2552 | if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR) |
| 2553 | encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR; |
| 2554 | return; |
| 2555 | } |
| 2556 | if(encoder->private_->write_callback(encoder, metadata->data.stream_info.md5sum, 16, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) { |
| 2557 | encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR; |
| 2558 | return; |
| 2559 | } |
| 2560 | } |
| 2561 | |
| 2562 | /* |
| 2563 | * Write total samples |
| 2564 | */ |
| 2565 | { |
| 2566 | const unsigned total_samples_byte_offset = |
| 2567 | FLAC__STREAM_METADATA_HEADER_LENGTH + |
| 2568 | ( |
| 2569 | FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN + |
| 2570 | FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN + |
| 2571 | FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN + |
| 2572 | FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN + |
| 2573 | FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN + |
| 2574 | FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN + |
| 2575 | FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN |
| 2576 | - 4 |
| 2577 | ) / 8; |
| 2578 | |
| 2579 | b[0] = ((FLAC__byte)(bps-1) << 4) | (FLAC__byte)((samples >> 32) & 0x0F); |
| 2580 | b[1] = (FLAC__byte)((samples >> 24) & 0xFF); |
| 2581 | b[2] = (FLAC__byte)((samples >> 16) & 0xFF); |
| 2582 | b[3] = (FLAC__byte)((samples >> 8) & 0xFF); |
| 2583 | b[4] = (FLAC__byte)(samples & 0xFF); |
| 2584 | if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + total_samples_byte_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) { |
| 2585 | if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR) |
| 2586 | encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR; |
| 2587 | return; |
| 2588 | } |
| 2589 | if(encoder->private_->write_callback(encoder, b, 5, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) { |
| 2590 | encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR; |
| 2591 | return; |
| 2592 | } |
| 2593 | } |
| 2594 | |
| 2595 | /* |
| 2596 | * Write min/max framesize |
| 2597 | */ |
| 2598 | { |
| 2599 | const unsigned min_framesize_offset = |
| 2600 | FLAC__STREAM_METADATA_HEADER_LENGTH + |
| 2601 | ( |
| 2602 | FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN + |
| 2603 | FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN |
| 2604 | ) / 8; |
| 2605 | |
| 2606 | b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF); |
| 2607 | b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF); |
| 2608 | b[2] = (FLAC__byte)(min_framesize & 0xFF); |
| 2609 | b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF); |
| 2610 | b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF); |
| 2611 | b[5] = (FLAC__byte)(max_framesize & 0xFF); |
| 2612 | if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + min_framesize_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) { |
| 2613 | if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR) |
| 2614 | encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR; |
| 2615 | return; |
| 2616 | } |
| 2617 | if(encoder->private_->write_callback(encoder, b, 6, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) { |
| 2618 | encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR; |
| 2619 | return; |
| 2620 | } |
| 2621 | } |
| 2622 | |
| 2623 | /* |
| 2624 | * Write seektable |
| 2625 | */ |
| 2626 | if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) { |
| 2627 | unsigned i; |
| 2628 | |
| 2629 | FLAC__format_seektable_sort(encoder->private_->seek_table); |
| 2630 | |
| 2631 | FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table)); |
| 2632 | |
| 2633 | if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->seektable_offset + FLAC__STREAM_METADATA_HEADER_LENGTH, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) { |
| 2634 | if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR) |
| 2635 | encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR; |
| 2636 | return; |
| 2637 | } |
| 2638 | |
| 2639 | for(i = 0; i < encoder->private_->seek_table->num_points; i++) { |
| 2640 | FLAC__uint64 xx; |
| 2641 | unsigned x; |
| 2642 | xx = encoder->private_->seek_table->points[i].sample_number; |
| 2643 | b[7] = (FLAC__byte)xx; xx >>= 8; |
| 2644 | b[6] = (FLAC__byte)xx; xx >>= 8; |
| 2645 | b[5] = (FLAC__byte)xx; xx >>= 8; |
| 2646 | b[4] = (FLAC__byte)xx; xx >>= 8; |
| 2647 | b[3] = (FLAC__byte)xx; xx >>= 8; |
| 2648 | b[2] = (FLAC__byte)xx; xx >>= 8; |
| 2649 | b[1] = (FLAC__byte)xx; xx >>= 8; |
| 2650 | b[0] = (FLAC__byte)xx; xx >>= 8; |
| 2651 | xx = encoder->private_->seek_table->points[i].stream_offset; |
| 2652 | b[15] = (FLAC__byte)xx; xx >>= 8; |
| 2653 | b[14] = (FLAC__byte)xx; xx >>= 8; |
| 2654 | b[13] = (FLAC__byte)xx; xx >>= 8; |
| 2655 | b[12] = (FLAC__byte)xx; xx >>= 8; |
| 2656 | b[11] = (FLAC__byte)xx; xx >>= 8; |
| 2657 | b[10] = (FLAC__byte)xx; xx >>= 8; |
| 2658 | b[9] = (FLAC__byte)xx; xx >>= 8; |
| 2659 | b[8] = (FLAC__byte)xx; xx >>= 8; |
| 2660 | x = encoder->private_->seek_table->points[i].frame_samples; |
| 2661 | b[17] = (FLAC__byte)x; x >>= 8; |
| 2662 | b[16] = (FLAC__byte)x; x >>= 8; |
| 2663 | if(encoder->private_->write_callback(encoder, b, 18, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) { |
| 2664 | encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR; |
| 2665 | return; |
| 2666 | } |
| 2667 | } |
| 2668 | } |
| 2669 | } |
| 2670 | |
| 2671 | #if FLAC__HAS_OGG |
| 2672 | /* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */ |
| 2673 | void update_ogg_metadata_(FLAC__StreamEncoder *encoder) |
| 2674 | { |
| 2675 | /* the # of bytes in the 1st packet that precede the STREAMINFO */ |
| 2676 | static const unsigned FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH = |
| 2677 | FLAC__OGG_MAPPING_PACKET_TYPE_LENGTH + |
| 2678 | FLAC__OGG_MAPPING_MAGIC_LENGTH + |
| 2679 | FLAC__OGG_MAPPING_VERSION_MAJOR_LENGTH + |
| 2680 | FLAC__OGG_MAPPING_VERSION_MINOR_LENGTH + |
| 2681 | FLAC__OGG_MAPPING_NUM_HEADERS_LENGTH + |
| 2682 | FLAC__STREAM_SYNC_LENGTH |
| 2683 | ; |
| 2684 | FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)]; |
| 2685 | const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo; |
| 2686 | const FLAC__uint64 samples = metadata->data.stream_info.total_samples; |
| 2687 | const unsigned min_framesize = metadata->data.stream_info.min_framesize; |
| 2688 | const unsigned max_framesize = metadata->data.stream_info.max_framesize; |
| 2689 | ogg_page page; |
| 2690 | |
| 2691 | FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO); |
| 2692 | FLAC__ASSERT(0 != encoder->private_->seek_callback); |
| 2693 | |
| 2694 | /* Pre-check that client supports seeking, since we don't want the |
| 2695 | * ogg_helper code to ever have to deal with this condition. |
| 2696 | */ |
| 2697 | if(encoder->private_->seek_callback(encoder, 0, encoder->private_->client_data) == FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED) |
| 2698 | return; |
| 2699 | |
| 2700 | /* All this is based on intimate knowledge of the stream header |
| 2701 | * layout, but a change to the header format that would break this |
| 2702 | * would also break all streams encoded in the previous format. |
| 2703 | */ |
| 2704 | |
| 2705 | /** |
| 2706 | ** Write STREAMINFO stats |
| 2707 | **/ |
| 2708 | simple_ogg_page__init(&page); |
| 2709 | if(!simple_ogg_page__get_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) { |
| 2710 | simple_ogg_page__clear(&page); |
| 2711 | return; /* state already set */ |
| 2712 | } |
| 2713 | |
| 2714 | /* |
| 2715 | * Write MD5 signature |
| 2716 | */ |
| 2717 | { |
| 2718 | const unsigned md5_offset = |
| 2719 | FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH + |
| 2720 | FLAC__STREAM_METADATA_HEADER_LENGTH + |
| 2721 | ( |
| 2722 | FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN + |
| 2723 | FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN + |
| 2724 | FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN + |
| 2725 | FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN + |
| 2726 | FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN + |
| 2727 | FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN + |
| 2728 | FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN + |
| 2729 | FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN |
| 2730 | ) / 8; |
| 2731 | |
| 2732 | if(md5_offset + 16 > (unsigned)page.body_len) { |
| 2733 | encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR; |
| 2734 | simple_ogg_page__clear(&page); |
| 2735 | return; |
| 2736 | } |
| 2737 | memcpy(page.body + md5_offset, metadata->data.stream_info.md5sum, 16); |
| 2738 | } |
| 2739 | |
| 2740 | /* |
| 2741 | * Write total samples |
| 2742 | */ |
| 2743 | { |
| 2744 | const unsigned total_samples_byte_offset = |
| 2745 | FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH + |
| 2746 | FLAC__STREAM_METADATA_HEADER_LENGTH + |
| 2747 | ( |
| 2748 | FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN + |
| 2749 | FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN + |
| 2750 | FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN + |
| 2751 | FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN + |
| 2752 | FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN + |
| 2753 | FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN + |
| 2754 | FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN |
| 2755 | - 4 |
| 2756 | ) / 8; |
| 2757 | |
| 2758 | if(total_samples_byte_offset + 5 > (unsigned)page.body_len) { |
| 2759 | encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR; |
| 2760 | simple_ogg_page__clear(&page); |
| 2761 | return; |
| 2762 | } |
| 2763 | b[0] = (FLAC__byte)page.body[total_samples_byte_offset] & 0xF0; |
| 2764 | b[0] |= (FLAC__byte)((samples >> 32) & 0x0F); |
| 2765 | b[1] = (FLAC__byte)((samples >> 24) & 0xFF); |
| 2766 | b[2] = (FLAC__byte)((samples >> 16) & 0xFF); |
| 2767 | b[3] = (FLAC__byte)((samples >> 8) & 0xFF); |
| 2768 | b[4] = (FLAC__byte)(samples & 0xFF); |
| 2769 | memcpy(page.body + total_samples_byte_offset, b, 5); |
| 2770 | } |
| 2771 | |
| 2772 | /* |
| 2773 | * Write min/max framesize |
| 2774 | */ |
| 2775 | { |
| 2776 | const unsigned min_framesize_offset = |
| 2777 | FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH + |
| 2778 | FLAC__STREAM_METADATA_HEADER_LENGTH + |
| 2779 | ( |
| 2780 | FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN + |
| 2781 | FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN |
| 2782 | ) / 8; |
| 2783 | |
| 2784 | if(min_framesize_offset + 6 > (unsigned)page.body_len) { |
| 2785 | encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR; |
| 2786 | simple_ogg_page__clear(&page); |
| 2787 | return; |
| 2788 | } |
| 2789 | b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF); |
| 2790 | b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF); |
| 2791 | b[2] = (FLAC__byte)(min_framesize & 0xFF); |
| 2792 | b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF); |
| 2793 | b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF); |
| 2794 | b[5] = (FLAC__byte)(max_framesize & 0xFF); |
| 2795 | memcpy(page.body + min_framesize_offset, b, 6); |
| 2796 | } |
| 2797 | if(!simple_ogg_page__set_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) { |
| 2798 | simple_ogg_page__clear(&page); |
| 2799 | return; /* state already set */ |
| 2800 | } |
| 2801 | simple_ogg_page__clear(&page); |
| 2802 | |
| 2803 | /* |
| 2804 | * Write seektable |
| 2805 | */ |
| 2806 | if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) { |
| 2807 | unsigned i; |
| 2808 | FLAC__byte *p; |
| 2809 | |
| 2810 | FLAC__format_seektable_sort(encoder->private_->seek_table); |
| 2811 | |
| 2812 | FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table)); |
| 2813 | |
| 2814 | simple_ogg_page__init(&page); |
| 2815 | if(!simple_ogg_page__get_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) { |
| 2816 | simple_ogg_page__clear(&page); |
| 2817 | return; /* state already set */ |
| 2818 | } |
| 2819 | |
| 2820 | if((FLAC__STREAM_METADATA_HEADER_LENGTH + 18*encoder->private_->seek_table->num_points) != (unsigned)page.body_len) { |
| 2821 | encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR; |
| 2822 | simple_ogg_page__clear(&page); |
| 2823 | return; |
| 2824 | } |
| 2825 | |
| 2826 | for(i = 0, p = page.body + FLAC__STREAM_METADATA_HEADER_LENGTH; i < encoder->private_->seek_table->num_points; i++, p += 18) { |
| 2827 | FLAC__uint64 xx; |
| 2828 | unsigned x; |
| 2829 | xx = encoder->private_->seek_table->points[i].sample_number; |
| 2830 | b[7] = (FLAC__byte)xx; xx >>= 8; |
| 2831 | b[6] = (FLAC__byte)xx; xx >>= 8; |
| 2832 | b[5] = (FLAC__byte)xx; xx >>= 8; |
| 2833 | b[4] = (FLAC__byte)xx; xx >>= 8; |
| 2834 | b[3] = (FLAC__byte)xx; xx >>= 8; |
| 2835 | b[2] = (FLAC__byte)xx; xx >>= 8; |
| 2836 | b[1] = (FLAC__byte)xx; xx >>= 8; |
| 2837 | b[0] = (FLAC__byte)xx; xx >>= 8; |
| 2838 | xx = encoder->private_->seek_table->points[i].stream_offset; |
| 2839 | b[15] = (FLAC__byte)xx; xx >>= 8; |
| 2840 | b[14] = (FLAC__byte)xx; xx >>= 8; |
| 2841 | b[13] = (FLAC__byte)xx; xx >>= 8; |
| 2842 | b[12] = (FLAC__byte)xx; xx >>= 8; |
| 2843 | b[11] = (FLAC__byte)xx; xx >>= 8; |
| 2844 | b[10] = (FLAC__byte)xx; xx >>= 8; |
| 2845 | b[9] = (FLAC__byte)xx; xx >>= 8; |
| 2846 | b[8] = (FLAC__byte)xx; xx >>= 8; |
| 2847 | x = encoder->private_->seek_table->points[i].frame_samples; |
| 2848 | b[17] = (FLAC__byte)x; x >>= 8; |
| 2849 | b[16] = (FLAC__byte)x; x >>= 8; |
| 2850 | memcpy(p, b, 18); |
| 2851 | } |
| 2852 | |
| 2853 | if(!simple_ogg_page__set_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) { |
| 2854 | simple_ogg_page__clear(&page); |
| 2855 | return; /* state already set */ |
| 2856 | } |
| 2857 | simple_ogg_page__clear(&page); |
| 2858 | } |
| 2859 | } |
| 2860 | #endif |
| 2861 | |
| 2862 | FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block, FLAC__bool is_last_block) |
| 2863 | { |
| 2864 | FLAC__uint16 crc; |
| 2865 | FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK); |
| 2866 | |
| 2867 | /* |
| 2868 | * Accumulate raw signal to the MD5 signature |
| 2869 | */ |
| 2870 | if(encoder->protected_->do_md5 && !FLAC__MD5Accumulate(&encoder->private_->md5context, (const FLAC__int32 * const *)encoder->private_->integer_signal, encoder->protected_->channels, encoder->protected_->blocksize, (encoder->protected_->bits_per_sample+7) / 8)) { |
| 2871 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 2872 | return false; |
| 2873 | } |
| 2874 | |
| 2875 | /* |
| 2876 | * Process the frame header and subframes into the frame bitbuffer |
| 2877 | */ |
| 2878 | if(!process_subframes_(encoder, is_fractional_block)) { |
| 2879 | /* the above function sets the state for us in case of an error */ |
| 2880 | return false; |
| 2881 | } |
| 2882 | |
| 2883 | /* |
| 2884 | * Zero-pad the frame to a byte_boundary |
| 2885 | */ |
| 2886 | if(!FLAC__bitwriter_zero_pad_to_byte_boundary(encoder->private_->frame)) { |
| 2887 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 2888 | return false; |
| 2889 | } |
| 2890 | |
| 2891 | /* |
| 2892 | * CRC-16 the whole thing |
| 2893 | */ |
| 2894 | FLAC__ASSERT(FLAC__bitwriter_is_byte_aligned(encoder->private_->frame)); |
| 2895 | if( |
| 2896 | !FLAC__bitwriter_get_write_crc16(encoder->private_->frame, &crc) || |
| 2897 | !FLAC__bitwriter_write_raw_uint32(encoder->private_->frame, crc, FLAC__FRAME_FOOTER_CRC_LEN) |
| 2898 | ) { |
| 2899 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 2900 | return false; |
| 2901 | } |
| 2902 | |
| 2903 | /* |
| 2904 | * Write it |
| 2905 | */ |
| 2906 | if(!write_bitbuffer_(encoder, encoder->protected_->blocksize, is_last_block)) { |
| 2907 | /* the above function sets the state for us in case of an error */ |
| 2908 | return false; |
| 2909 | } |
| 2910 | |
| 2911 | /* |
| 2912 | * Get ready for the next frame |
| 2913 | */ |
| 2914 | encoder->private_->current_sample_number = 0; |
| 2915 | encoder->private_->current_frame_number++; |
| 2916 | encoder->private_->streaminfo.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize; |
| 2917 | |
| 2918 | return true; |
| 2919 | } |
| 2920 | |
| 2921 | FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block) |
| 2922 | { |
| 2923 | FLAC__FrameHeader frame_header; |
| 2924 | unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order; |
| 2925 | FLAC__bool do_independent, do_mid_side; |
| 2926 | |
| 2927 | /* |
| 2928 | * Calculate the min,max Rice partition orders |
| 2929 | */ |
| 2930 | if(is_fractional_block) { |
| 2931 | max_partition_order = 0; |
| 2932 | } |
| 2933 | else { |
| 2934 | max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize); |
| 2935 | max_partition_order = min(max_partition_order, encoder->protected_->max_residual_partition_order); |
| 2936 | } |
| 2937 | min_partition_order = min(min_partition_order, max_partition_order); |
| 2938 | |
| 2939 | /* |
| 2940 | * Setup the frame |
| 2941 | */ |
| 2942 | frame_header.blocksize = encoder->protected_->blocksize; |
| 2943 | frame_header.sample_rate = encoder->protected_->sample_rate; |
| 2944 | frame_header.channels = encoder->protected_->channels; |
| 2945 | frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */ |
| 2946 | frame_header.bits_per_sample = encoder->protected_->bits_per_sample; |
| 2947 | frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER; |
| 2948 | frame_header.number.frame_number = encoder->private_->current_frame_number; |
| 2949 | |
| 2950 | /* |
| 2951 | * Figure out what channel assignments to try |
| 2952 | */ |
| 2953 | if(encoder->protected_->do_mid_side_stereo) { |
| 2954 | if(encoder->protected_->loose_mid_side_stereo) { |
| 2955 | if(encoder->private_->loose_mid_side_stereo_frame_count == 0) { |
| 2956 | do_independent = true; |
| 2957 | do_mid_side = true; |
| 2958 | } |
| 2959 | else { |
| 2960 | do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT); |
| 2961 | do_mid_side = !do_independent; |
| 2962 | } |
| 2963 | } |
| 2964 | else { |
| 2965 | do_independent = true; |
| 2966 | do_mid_side = true; |
| 2967 | } |
| 2968 | } |
| 2969 | else { |
| 2970 | do_independent = true; |
| 2971 | do_mid_side = false; |
| 2972 | } |
| 2973 | |
| 2974 | FLAC__ASSERT(do_independent || do_mid_side); |
| 2975 | |
| 2976 | /* |
| 2977 | * Check for wasted bits; set effective bps for each subframe |
| 2978 | */ |
| 2979 | if(do_independent) { |
| 2980 | for(channel = 0; channel < encoder->protected_->channels; channel++) { |
| 2981 | const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize); |
| 2982 | encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w; |
| 2983 | encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w; |
| 2984 | } |
| 2985 | } |
| 2986 | if(do_mid_side) { |
| 2987 | FLAC__ASSERT(encoder->protected_->channels == 2); |
| 2988 | for(channel = 0; channel < 2; channel++) { |
| 2989 | const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize); |
| 2990 | encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w; |
| 2991 | encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1); |
| 2992 | } |
| 2993 | } |
| 2994 | |
| 2995 | /* |
| 2996 | * First do a normal encoding pass of each independent channel |
| 2997 | */ |
| 2998 | if(do_independent) { |
| 2999 | for(channel = 0; channel < encoder->protected_->channels; channel++) { |
| 3000 | if(! |
| 3001 | process_subframe_( |
| 3002 | encoder, |
| 3003 | min_partition_order, |
| 3004 | max_partition_order, |
| 3005 | &frame_header, |
| 3006 | encoder->private_->subframe_bps[channel], |
| 3007 | encoder->private_->integer_signal[channel], |
| 3008 | encoder->private_->subframe_workspace_ptr[channel], |
| 3009 | encoder->private_->partitioned_rice_contents_workspace_ptr[channel], |
| 3010 | encoder->private_->residual_workspace[channel], |
| 3011 | encoder->private_->best_subframe+channel, |
| 3012 | encoder->private_->best_subframe_bits+channel |
| 3013 | ) |
| 3014 | ) |
| 3015 | return false; |
| 3016 | } |
| 3017 | } |
| 3018 | |
| 3019 | /* |
| 3020 | * Now do mid and side channels if requested |
| 3021 | */ |
| 3022 | if(do_mid_side) { |
| 3023 | FLAC__ASSERT(encoder->protected_->channels == 2); |
| 3024 | |
| 3025 | for(channel = 0; channel < 2; channel++) { |
| 3026 | if(! |
| 3027 | process_subframe_( |
| 3028 | encoder, |
| 3029 | min_partition_order, |
| 3030 | max_partition_order, |
| 3031 | &frame_header, |
| 3032 | encoder->private_->subframe_bps_mid_side[channel], |
| 3033 | encoder->private_->integer_signal_mid_side[channel], |
| 3034 | encoder->private_->subframe_workspace_ptr_mid_side[channel], |
| 3035 | encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel], |
| 3036 | encoder->private_->residual_workspace_mid_side[channel], |
| 3037 | encoder->private_->best_subframe_mid_side+channel, |
| 3038 | encoder->private_->best_subframe_bits_mid_side+channel |
| 3039 | ) |
| 3040 | ) |
| 3041 | return false; |
| 3042 | } |
| 3043 | } |
| 3044 | |
| 3045 | /* |
| 3046 | * Compose the frame bitbuffer |
| 3047 | */ |
| 3048 | if(do_mid_side) { |
| 3049 | unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */ |
| 3050 | FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */ |
| 3051 | FLAC__ChannelAssignment channel_assignment; |
| 3052 | |
| 3053 | FLAC__ASSERT(encoder->protected_->channels == 2); |
| 3054 | |
| 3055 | if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) { |
| 3056 | channel_assignment = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE); |
| 3057 | } |
| 3058 | else { |
| 3059 | unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */ |
| 3060 | unsigned min_bits; |
| 3061 | int ca; |
| 3062 | |
| 3063 | FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT == 0); |
| 3064 | FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE == 1); |
| 3065 | FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE == 2); |
| 3066 | FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_MID_SIDE == 3); |
| 3067 | FLAC__ASSERT(do_independent && do_mid_side); |
| 3068 | |
| 3069 | /* We have to figure out which channel assignent results in the smallest frame */ |
| 3070 | bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1]; |
| 3071 | bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1]; |
| 3072 | bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1]; |
| 3073 | bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->private_->best_subframe_bits_mid_side[0] + encoder->private_->best_subframe_bits_mid_side[1]; |
| 3074 | |
| 3075 | channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; |
| 3076 | min_bits = bits[channel_assignment]; |
| 3077 | for(ca = 1; ca <= 3; ca++) { |
| 3078 | if(bits[ca] < min_bits) { |
| 3079 | min_bits = bits[ca]; |
| 3080 | channel_assignment = (FLAC__ChannelAssignment)ca; |
| 3081 | } |
| 3082 | } |
| 3083 | } |
| 3084 | |
| 3085 | frame_header.channel_assignment = channel_assignment; |
| 3086 | |
| 3087 | if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) { |
| 3088 | encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
| 3089 | return false; |
| 3090 | } |
| 3091 | |
| 3092 | switch(channel_assignment) { |
| 3093 | case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: |
| 3094 | left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]]; |
| 3095 | right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]]; |
| 3096 | break; |
| 3097 | case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: |
| 3098 | left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]]; |
| 3099 | right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]]; |
| 3100 | break; |
| 3101 | case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: |
| 3102 | left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]]; |
| 3103 | right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]]; |
| 3104 | break; |
| 3105 | case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: |
| 3106 | left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]]; |
| 3107 | right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]]; |
| 3108 | break; |
| 3109 | default: |
| 3110 | FLAC__ASSERT(0); |
| 3111 | } |
| 3112 | |
| 3113 | switch(channel_assignment) { |
| 3114 | case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: |
| 3115 | left_bps = encoder->private_->subframe_bps [0]; |
| 3116 | right_bps = encoder->private_->subframe_bps [1]; |
| 3117 | break; |
| 3118 | case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: |
| 3119 | left_bps = encoder->private_->subframe_bps [0]; |
| 3120 | right_bps = encoder->private_->subframe_bps_mid_side[1]; |
| 3121 | break; |
| 3122 | case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: |
| 3123 | left_bps = encoder->private_->subframe_bps_mid_side[1]; |
| 3124 | right_bps = encoder->private_->subframe_bps [1]; |
| 3125 | break; |
| 3126 | case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: |
| 3127 | left_bps = encoder->private_->subframe_bps_mid_side[0]; |
| 3128 | right_bps = encoder->private_->subframe_bps_mid_side[1]; |
| 3129 | break; |
| 3130 | default: |
| 3131 | FLAC__ASSERT(0); |
| 3132 | } |
| 3133 | |
| 3134 | /* note that encoder_add_subframe_ sets the state for us in case of an error */ |
| 3135 | if(!add_subframe_(encoder, frame_header.blocksize, left_bps , left_subframe , encoder->private_->frame)) |
| 3136 | return false; |
| 3137 | if(!add_subframe_(encoder, frame_header.blocksize, right_bps, right_subframe, encoder->private_->frame)) |
| 3138 | return false; |
| 3139 | } |
| 3140 | else { |
| 3141 | if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) { |
| 3142 | encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
| 3143 | return false; |
| 3144 | } |
| 3145 | |
| 3146 | for(channel = 0; channel < encoder->protected_->channels; channel++) { |
| 3147 | if(!add_subframe_(encoder, frame_header.blocksize, encoder->private_->subframe_bps[channel], &encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]], encoder->private_->frame)) { |
| 3148 | /* the above function sets the state for us in case of an error */ |
| 3149 | return false; |
| 3150 | } |
| 3151 | } |
| 3152 | } |
| 3153 | |
| 3154 | if(encoder->protected_->loose_mid_side_stereo) { |
| 3155 | encoder->private_->loose_mid_side_stereo_frame_count++; |
| 3156 | if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames) |
| 3157 | encoder->private_->loose_mid_side_stereo_frame_count = 0; |
| 3158 | } |
| 3159 | |
| 3160 | encoder->private_->last_channel_assignment = frame_header.channel_assignment; |
| 3161 | |
| 3162 | return true; |
| 3163 | } |
| 3164 | |
| 3165 | FLAC__bool process_subframe_( |
| 3166 | FLAC__StreamEncoder *encoder, |
| 3167 | unsigned min_partition_order, |
| 3168 | unsigned max_partition_order, |
| 3169 | const FLAC__FrameHeader *frame_header, |
| 3170 | unsigned subframe_bps, |
| 3171 | const FLAC__int32 integer_signal[], |
| 3172 | FLAC__Subframe *subframe[2], |
| 3173 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2], |
| 3174 | FLAC__int32 *residual[2], |
| 3175 | unsigned *best_subframe, |
| 3176 | unsigned *best_bits |
| 3177 | ) |
| 3178 | { |
| 3179 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 3180 | FLAC__float fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]; |
| 3181 | #else |
| 3182 | FLAC__fixedpoint fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]; |
| 3183 | #endif |
| 3184 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 3185 | FLAC__double lpc_residual_bits_per_sample; |
| 3186 | FLAC__real autoc[FLAC__MAX_LPC_ORDER+1]; /* WATCHOUT: the size is important even though encoder->protected_->max_lpc_order might be less; some asm routines need all the space */ |
| 3187 | FLAC__double lpc_error[FLAC__MAX_LPC_ORDER]; |
| 3188 | unsigned min_lpc_order, max_lpc_order, lpc_order; |
| 3189 | unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision; |
| 3190 | #endif |
| 3191 | unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order; |
| 3192 | unsigned rice_parameter; |
| 3193 | unsigned _candidate_bits, _best_bits; |
| 3194 | unsigned _best_subframe; |
| 3195 | /* only use RICE2 partitions if stream bps > 16 */ |
| 3196 | const unsigned rice_parameter_limit = FLAC__stream_encoder_get_bits_per_sample(encoder) > 16? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER; |
| 3197 | |
| 3198 | FLAC__ASSERT(frame_header->blocksize > 0); |
| 3199 | |
| 3200 | /* verbatim subframe is the baseline against which we measure other compressed subframes */ |
| 3201 | _best_subframe = 0; |
| 3202 | if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) |
| 3203 | _best_bits = UINT_MAX; |
| 3204 | else |
| 3205 | _best_bits = evaluate_verbatim_subframe_(encoder, integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]); |
| 3206 | |
| 3207 | if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) { |
| 3208 | unsigned signal_is_constant = false; |
| 3209 | guess_fixed_order = encoder->private_->local_fixed_compute_best_predictor(integer_signal+FLAC__MAX_FIXED_ORDER, frame_header->blocksize-FLAC__MAX_FIXED_ORDER, fixed_residual_bits_per_sample); |
| 3210 | /* check for constant subframe */ |
| 3211 | if( |
| 3212 | !encoder->private_->disable_constant_subframes && |
| 3213 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 3214 | fixed_residual_bits_per_sample[1] == 0.0 |
| 3215 | #else |
| 3216 | fixed_residual_bits_per_sample[1] == FLAC__FP_ZERO |
| 3217 | #endif |
| 3218 | ) { |
| 3219 | /* the above means it's possible all samples are the same value; now double-check it: */ |
| 3220 | unsigned i; |
| 3221 | signal_is_constant = true; |
| 3222 | for(i = 1; i < frame_header->blocksize; i++) { |
| 3223 | if(integer_signal[0] != integer_signal[i]) { |
| 3224 | signal_is_constant = false; |
| 3225 | break; |
| 3226 | } |
| 3227 | } |
| 3228 | } |
| 3229 | if(signal_is_constant) { |
| 3230 | _candidate_bits = evaluate_constant_subframe_(encoder, integer_signal[0], frame_header->blocksize, subframe_bps, subframe[!_best_subframe]); |
| 3231 | if(_candidate_bits < _best_bits) { |
| 3232 | _best_subframe = !_best_subframe; |
| 3233 | _best_bits = _candidate_bits; |
| 3234 | } |
| 3235 | } |
| 3236 | else { |
| 3237 | if(!encoder->private_->disable_fixed_subframes || (encoder->protected_->max_lpc_order == 0 && _best_bits == UINT_MAX)) { |
| 3238 | /* encode fixed */ |
| 3239 | if(encoder->protected_->do_exhaustive_model_search) { |
| 3240 | min_fixed_order = 0; |
| 3241 | max_fixed_order = FLAC__MAX_FIXED_ORDER; |
| 3242 | } |
| 3243 | else { |
| 3244 | min_fixed_order = max_fixed_order = guess_fixed_order; |
| 3245 | } |
| 3246 | if(max_fixed_order >= frame_header->blocksize) |
| 3247 | max_fixed_order = frame_header->blocksize - 1; |
| 3248 | for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) { |
| 3249 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 3250 | if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__float)subframe_bps) |
| 3251 | continue; /* don't even try */ |
| 3252 | rice_parameter = (fixed_residual_bits_per_sample[fixed_order] > 0.0)? (unsigned)(fixed_residual_bits_per_sample[fixed_order]+0.5) : 0; /* 0.5 is for rounding */ |
| 3253 | #else |
| 3254 | if(FLAC__fixedpoint_trunc(fixed_residual_bits_per_sample[fixed_order]) >= (int)subframe_bps) |
| 3255 | continue; /* don't even try */ |
| 3256 | rice_parameter = (fixed_residual_bits_per_sample[fixed_order] > FLAC__FP_ZERO)? (unsigned)FLAC__fixedpoint_trunc(fixed_residual_bits_per_sample[fixed_order]+FLAC__FP_ONE_HALF) : 0; /* 0.5 is for rounding */ |
| 3257 | #endif |
| 3258 | rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */ |
| 3259 | if(rice_parameter >= rice_parameter_limit) { |
| 3260 | #ifdef DEBUG_VERBOSE |
| 3261 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, rice_parameter_limit - 1); |
| 3262 | #endif |
| 3263 | rice_parameter = rice_parameter_limit - 1; |
| 3264 | } |
| 3265 | _candidate_bits = |
| 3266 | evaluate_fixed_subframe_( |
| 3267 | encoder, |
| 3268 | integer_signal, |
| 3269 | residual[!_best_subframe], |
| 3270 | encoder->private_->abs_residual_partition_sums, |
| 3271 | encoder->private_->raw_bits_per_partition, |
| 3272 | frame_header->blocksize, |
| 3273 | subframe_bps, |
| 3274 | fixed_order, |
| 3275 | rice_parameter, |
| 3276 | rice_parameter_limit, |
| 3277 | min_partition_order, |
| 3278 | max_partition_order, |
| 3279 | encoder->protected_->do_escape_coding, |
| 3280 | encoder->protected_->rice_parameter_search_dist, |
| 3281 | subframe[!_best_subframe], |
| 3282 | partitioned_rice_contents[!_best_subframe] |
| 3283 | ); |
| 3284 | if(_candidate_bits < _best_bits) { |
| 3285 | _best_subframe = !_best_subframe; |
| 3286 | _best_bits = _candidate_bits; |
| 3287 | } |
| 3288 | } |
| 3289 | } |
| 3290 | |
| 3291 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 3292 | /* encode lpc */ |
| 3293 | if(encoder->protected_->max_lpc_order > 0) { |
| 3294 | if(encoder->protected_->max_lpc_order >= frame_header->blocksize) |
| 3295 | max_lpc_order = frame_header->blocksize-1; |
| 3296 | else |
| 3297 | max_lpc_order = encoder->protected_->max_lpc_order; |
| 3298 | if(max_lpc_order > 0) { |
| 3299 | unsigned a; |
| 3300 | for (a = 0; a < encoder->protected_->num_apodizations; a++) { |
| 3301 | FLAC__lpc_window_data(integer_signal, encoder->private_->window[a], encoder->private_->windowed_signal, frame_header->blocksize); |
| 3302 | encoder->private_->local_lpc_compute_autocorrelation(encoder->private_->windowed_signal, frame_header->blocksize, max_lpc_order+1, autoc); |
| 3303 | /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */ |
| 3304 | if(autoc[0] != 0.0) { |
| 3305 | FLAC__lpc_compute_lp_coefficients(autoc, &max_lpc_order, encoder->private_->lp_coeff, lpc_error); |
| 3306 | if(encoder->protected_->do_exhaustive_model_search) { |
| 3307 | min_lpc_order = 1; |
| 3308 | } |
| 3309 | else { |
| 3310 | const unsigned guess_lpc_order = |
| 3311 | FLAC__lpc_compute_best_order( |
| 3312 | lpc_error, |
| 3313 | max_lpc_order, |
| 3314 | frame_header->blocksize, |
| 3315 | subframe_bps + ( |
| 3316 | encoder->protected_->do_qlp_coeff_prec_search? |
| 3317 | FLAC__MIN_QLP_COEFF_PRECISION : /* have to guess; use the min possible size to avoid accidentally favoring lower orders */ |
| 3318 | encoder->protected_->qlp_coeff_precision |
| 3319 | ) |
| 3320 | ); |
| 3321 | min_lpc_order = max_lpc_order = guess_lpc_order; |
| 3322 | } |
| 3323 | if(max_lpc_order >= frame_header->blocksize) |
| 3324 | max_lpc_order = frame_header->blocksize - 1; |
| 3325 | for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) { |
| 3326 | lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order); |
| 3327 | if(lpc_residual_bits_per_sample >= (FLAC__double)subframe_bps) |
| 3328 | continue; /* don't even try */ |
| 3329 | rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */ |
| 3330 | rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */ |
| 3331 | if(rice_parameter >= rice_parameter_limit) { |
| 3332 | #ifdef DEBUG_VERBOSE |
| 3333 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, rice_parameter_limit - 1); |
| 3334 | #endif |
| 3335 | rice_parameter = rice_parameter_limit - 1; |
| 3336 | } |
| 3337 | if(encoder->protected_->do_qlp_coeff_prec_search) { |
| 3338 | min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION; |
| 3339 | /* try to ensure a 32-bit datapath throughout for 16bps(+1bps for side channel) or less */ |
| 3340 | if(subframe_bps <= 17) { |
| 3341 | max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION); |
| 3342 | max_qlp_coeff_precision = max(max_qlp_coeff_precision, min_qlp_coeff_precision); |
| 3343 | } |
| 3344 | else |
| 3345 | max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION; |
| 3346 | } |
| 3347 | else { |
| 3348 | min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision; |
| 3349 | } |
| 3350 | for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) { |
| 3351 | _candidate_bits = |
| 3352 | evaluate_lpc_subframe_( |
| 3353 | encoder, |
| 3354 | integer_signal, |
| 3355 | residual[!_best_subframe], |
| 3356 | encoder->private_->abs_residual_partition_sums, |
| 3357 | encoder->private_->raw_bits_per_partition, |
| 3358 | encoder->private_->lp_coeff[lpc_order-1], |
| 3359 | frame_header->blocksize, |
| 3360 | subframe_bps, |
| 3361 | lpc_order, |
| 3362 | qlp_coeff_precision, |
| 3363 | rice_parameter, |
| 3364 | rice_parameter_limit, |
| 3365 | min_partition_order, |
| 3366 | max_partition_order, |
| 3367 | encoder->protected_->do_escape_coding, |
| 3368 | encoder->protected_->rice_parameter_search_dist, |
| 3369 | subframe[!_best_subframe], |
| 3370 | partitioned_rice_contents[!_best_subframe] |
| 3371 | ); |
| 3372 | if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */ |
| 3373 | if(_candidate_bits < _best_bits) { |
| 3374 | _best_subframe = !_best_subframe; |
| 3375 | _best_bits = _candidate_bits; |
| 3376 | } |
| 3377 | } |
| 3378 | } |
| 3379 | } |
| 3380 | } |
| 3381 | } |
| 3382 | } |
| 3383 | } |
| 3384 | #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */ |
| 3385 | } |
| 3386 | } |
| 3387 | |
| 3388 | /* under rare circumstances this can happen when all but lpc subframe types are disabled: */ |
| 3389 | if(_best_bits == UINT_MAX) { |
| 3390 | FLAC__ASSERT(_best_subframe == 0); |
| 3391 | _best_bits = evaluate_verbatim_subframe_(encoder, integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]); |
| 3392 | } |
| 3393 | |
| 3394 | *best_subframe = _best_subframe; |
| 3395 | *best_bits = _best_bits; |
| 3396 | |
| 3397 | return true; |
| 3398 | } |
| 3399 | |
| 3400 | FLAC__bool add_subframe_( |
| 3401 | FLAC__StreamEncoder *encoder, |
| 3402 | unsigned blocksize, |
| 3403 | unsigned subframe_bps, |
| 3404 | const FLAC__Subframe *subframe, |
| 3405 | FLAC__BitWriter *frame |
| 3406 | ) |
| 3407 | { |
| 3408 | switch(subframe->type) { |
| 3409 | case FLAC__SUBFRAME_TYPE_CONSTANT: |
| 3410 | if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) { |
| 3411 | encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
| 3412 | return false; |
| 3413 | } |
| 3414 | break; |
| 3415 | case FLAC__SUBFRAME_TYPE_FIXED: |
| 3416 | if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) { |
| 3417 | encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
| 3418 | return false; |
| 3419 | } |
| 3420 | break; |
| 3421 | case FLAC__SUBFRAME_TYPE_LPC: |
| 3422 | if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) { |
| 3423 | encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
| 3424 | return false; |
| 3425 | } |
| 3426 | break; |
| 3427 | case FLAC__SUBFRAME_TYPE_VERBATIM: |
| 3428 | if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), blocksize, subframe_bps, subframe->wasted_bits, frame)) { |
| 3429 | encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
| 3430 | return false; |
| 3431 | } |
| 3432 | break; |
| 3433 | default: |
| 3434 | FLAC__ASSERT(0); |
| 3435 | } |
| 3436 | |
| 3437 | return true; |
| 3438 | } |
| 3439 | |
| 3440 | #define SPOTCHECK_ESTIMATE 0 |
| 3441 | #if SPOTCHECK_ESTIMATE |
| 3442 | static void spotcheck_subframe_estimate_( |
| 3443 | FLAC__StreamEncoder *encoder, |
| 3444 | unsigned blocksize, |
| 3445 | unsigned subframe_bps, |
| 3446 | const FLAC__Subframe *subframe, |
| 3447 | unsigned estimate |
| 3448 | ) |
| 3449 | { |
| 3450 | FLAC__bool ret; |
| 3451 | FLAC__BitWriter *frame = FLAC__bitwriter_new(); |
| 3452 | if(frame == 0) { |
| 3453 | fprintf(stderr, "EST: can't allocate frame\n"); |
| 3454 | return; |
| 3455 | } |
| 3456 | if(!FLAC__bitwriter_init(frame)) { |
| 3457 | fprintf(stderr, "EST: can't init frame\n"); |
| 3458 | return; |
| 3459 | } |
| 3460 | ret = add_subframe_(encoder, blocksize, subframe_bps, subframe, frame); |
| 3461 | FLAC__ASSERT(ret); |
| 3462 | { |
| 3463 | const unsigned actual = FLAC__bitwriter_get_input_bits_unconsumed(frame); |
| 3464 | if(estimate != actual) |
| 3465 | fprintf(stderr, "EST: bad, frame#%u sub#%%d type=%8s est=%u, actual=%u, delta=%d\n", encoder->private_->current_frame_number, FLAC__SubframeTypeString[subframe->type], estimate, actual, (int)actual-(int)estimate); |
| 3466 | } |
| 3467 | FLAC__bitwriter_delete(frame); |
| 3468 | } |
| 3469 | #endif |
| 3470 | |
| 3471 | unsigned evaluate_constant_subframe_( |
| 3472 | FLAC__StreamEncoder *encoder, |
| 3473 | const FLAC__int32 signal, |
| 3474 | unsigned blocksize, |
| 3475 | unsigned subframe_bps, |
| 3476 | FLAC__Subframe *subframe |
| 3477 | ) |
| 3478 | { |
| 3479 | unsigned estimate; |
| 3480 | subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT; |
| 3481 | subframe->data.constant.value = signal; |
| 3482 | |
| 3483 | estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + subframe_bps; |
| 3484 | |
| 3485 | #if SPOTCHECK_ESTIMATE |
| 3486 | spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate); |
| 3487 | #else |
| 3488 | (void)encoder, (void)blocksize; |
| 3489 | #endif |
| 3490 | |
| 3491 | return estimate; |
| 3492 | } |
| 3493 | |
| 3494 | unsigned evaluate_fixed_subframe_( |
| 3495 | FLAC__StreamEncoder *encoder, |
| 3496 | const FLAC__int32 signal[], |
| 3497 | FLAC__int32 residual[], |
| 3498 | FLAC__uint64 abs_residual_partition_sums[], |
| 3499 | unsigned raw_bits_per_partition[], |
| 3500 | unsigned blocksize, |
| 3501 | unsigned subframe_bps, |
| 3502 | unsigned order, |
| 3503 | unsigned rice_parameter, |
| 3504 | unsigned rice_parameter_limit, |
| 3505 | unsigned min_partition_order, |
| 3506 | unsigned max_partition_order, |
| 3507 | FLAC__bool do_escape_coding, |
| 3508 | unsigned rice_parameter_search_dist, |
| 3509 | FLAC__Subframe *subframe, |
| 3510 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents |
| 3511 | ) |
| 3512 | { |
| 3513 | unsigned i, residual_bits, estimate; |
| 3514 | const unsigned residual_samples = blocksize - order; |
| 3515 | |
| 3516 | FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual); |
| 3517 | |
| 3518 | subframe->type = FLAC__SUBFRAME_TYPE_FIXED; |
| 3519 | |
| 3520 | subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE; |
| 3521 | subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents; |
| 3522 | subframe->data.fixed.residual = residual; |
| 3523 | |
| 3524 | residual_bits = |
| 3525 | find_best_partition_order_( |
| 3526 | encoder->private_, |
| 3527 | residual, |
| 3528 | abs_residual_partition_sums, |
| 3529 | raw_bits_per_partition, |
| 3530 | residual_samples, |
| 3531 | order, |
| 3532 | rice_parameter, |
| 3533 | rice_parameter_limit, |
| 3534 | min_partition_order, |
| 3535 | max_partition_order, |
| 3536 | subframe_bps, |
| 3537 | do_escape_coding, |
| 3538 | rice_parameter_search_dist, |
| 3539 | &subframe->data.fixed.entropy_coding_method |
| 3540 | ); |
| 3541 | |
| 3542 | subframe->data.fixed.order = order; |
| 3543 | for(i = 0; i < order; i++) |
| 3544 | subframe->data.fixed.warmup[i] = signal[i]; |
| 3545 | |
| 3546 | estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + (order * subframe_bps) + residual_bits; |
| 3547 | |
| 3548 | #if SPOTCHECK_ESTIMATE |
| 3549 | spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate); |
| 3550 | #endif |
| 3551 | |
| 3552 | return estimate; |
| 3553 | } |
| 3554 | |
| 3555 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 3556 | unsigned evaluate_lpc_subframe_( |
| 3557 | FLAC__StreamEncoder *encoder, |
| 3558 | const FLAC__int32 signal[], |
| 3559 | FLAC__int32 residual[], |
| 3560 | FLAC__uint64 abs_residual_partition_sums[], |
| 3561 | unsigned raw_bits_per_partition[], |
| 3562 | const FLAC__real lp_coeff[], |
| 3563 | unsigned blocksize, |
| 3564 | unsigned subframe_bps, |
| 3565 | unsigned order, |
| 3566 | unsigned qlp_coeff_precision, |
| 3567 | unsigned rice_parameter, |
| 3568 | unsigned rice_parameter_limit, |
| 3569 | unsigned min_partition_order, |
| 3570 | unsigned max_partition_order, |
| 3571 | FLAC__bool do_escape_coding, |
| 3572 | unsigned rice_parameter_search_dist, |
| 3573 | FLAC__Subframe *subframe, |
| 3574 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents |
| 3575 | ) |
| 3576 | { |
| 3577 | FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER]; |
| 3578 | unsigned i, residual_bits, estimate; |
| 3579 | int quantization, ret; |
| 3580 | const unsigned residual_samples = blocksize - order; |
| 3581 | |
| 3582 | /* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps streams */ |
| 3583 | if(subframe_bps <= 16) { |
| 3584 | FLAC__ASSERT(order > 0); |
| 3585 | FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER); |
| 3586 | qlp_coeff_precision = min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order)); |
| 3587 | } |
| 3588 | |
| 3589 | ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization); |
| 3590 | if(ret != 0) |
| 3591 | return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */ |
| 3592 | |
| 3593 | if(subframe_bps + qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32) |
| 3594 | if(subframe_bps <= 16 && qlp_coeff_precision <= 16) |
| 3595 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual); |
| 3596 | else |
| 3597 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual); |
| 3598 | else |
| 3599 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual); |
| 3600 | |
| 3601 | subframe->type = FLAC__SUBFRAME_TYPE_LPC; |
| 3602 | |
| 3603 | subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE; |
| 3604 | subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents; |
| 3605 | subframe->data.lpc.residual = residual; |
| 3606 | |
| 3607 | residual_bits = |
| 3608 | find_best_partition_order_( |
| 3609 | encoder->private_, |
| 3610 | residual, |
| 3611 | abs_residual_partition_sums, |
| 3612 | raw_bits_per_partition, |
| 3613 | residual_samples, |
| 3614 | order, |
| 3615 | rice_parameter, |
| 3616 | rice_parameter_limit, |
| 3617 | min_partition_order, |
| 3618 | max_partition_order, |
| 3619 | subframe_bps, |
| 3620 | do_escape_coding, |
| 3621 | rice_parameter_search_dist, |
| 3622 | &subframe->data.lpc.entropy_coding_method |
| 3623 | ); |
| 3624 | |
| 3625 | subframe->data.lpc.order = order; |
| 3626 | subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision; |
| 3627 | subframe->data.lpc.quantization_level = quantization; |
| 3628 | memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER); |
| 3629 | for(i = 0; i < order; i++) |
| 3630 | subframe->data.lpc.warmup[i] = signal[i]; |
| 3631 | |
| 3632 | estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN + FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN + (order * (qlp_coeff_precision + subframe_bps)) + residual_bits; |
| 3633 | |
| 3634 | #if SPOTCHECK_ESTIMATE |
| 3635 | spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate); |
| 3636 | #endif |
| 3637 | |
| 3638 | return estimate; |
| 3639 | } |
| 3640 | #endif |
| 3641 | |
| 3642 | unsigned evaluate_verbatim_subframe_( |
| 3643 | FLAC__StreamEncoder *encoder, |
| 3644 | const FLAC__int32 signal[], |
| 3645 | unsigned blocksize, |
| 3646 | unsigned subframe_bps, |
| 3647 | FLAC__Subframe *subframe |
| 3648 | ) |
| 3649 | { |
| 3650 | unsigned estimate; |
| 3651 | |
| 3652 | subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM; |
| 3653 | |
| 3654 | subframe->data.verbatim.data = signal; |
| 3655 | |
| 3656 | estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + (blocksize * subframe_bps); |
| 3657 | |
| 3658 | #if SPOTCHECK_ESTIMATE |
| 3659 | spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate); |
| 3660 | #else |
| 3661 | (void)encoder; |
| 3662 | #endif |
| 3663 | |
| 3664 | return estimate; |
| 3665 | } |
| 3666 | |
| 3667 | unsigned find_best_partition_order_( |
| 3668 | FLAC__StreamEncoderPrivate *private_, |
| 3669 | const FLAC__int32 residual[], |
| 3670 | FLAC__uint64 abs_residual_partition_sums[], |
| 3671 | unsigned raw_bits_per_partition[], |
| 3672 | unsigned residual_samples, |
| 3673 | unsigned predictor_order, |
| 3674 | unsigned rice_parameter, |
| 3675 | unsigned rice_parameter_limit, |
| 3676 | unsigned min_partition_order, |
| 3677 | unsigned max_partition_order, |
| 3678 | unsigned bps, |
| 3679 | FLAC__bool do_escape_coding, |
| 3680 | unsigned rice_parameter_search_dist, |
| 3681 | FLAC__EntropyCodingMethod *best_ecm |
| 3682 | ) |
| 3683 | { |
| 3684 | unsigned residual_bits, best_residual_bits = 0; |
| 3685 | unsigned best_parameters_index = 0; |
| 3686 | unsigned best_partition_order = 0; |
| 3687 | const unsigned blocksize = residual_samples + predictor_order; |
| 3688 | |
| 3689 | max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(max_partition_order, blocksize, predictor_order); |
| 3690 | min_partition_order = min(min_partition_order, max_partition_order); |
| 3691 | |
| 3692 | precompute_partition_info_sums_(residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order, bps); |
| 3693 | |
| 3694 | if(do_escape_coding) |
| 3695 | precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order); |
| 3696 | |
| 3697 | { |
| 3698 | int partition_order; |
| 3699 | unsigned sum; |
| 3700 | |
| 3701 | for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) { |
| 3702 | if(! |
| 3703 | set_partitioned_rice_( |
| 3704 | #ifdef EXACT_RICE_BITS_CALCULATION |
| 3705 | residual, |
| 3706 | #endif |
| 3707 | abs_residual_partition_sums+sum, |
| 3708 | raw_bits_per_partition+sum, |
| 3709 | residual_samples, |
| 3710 | predictor_order, |
| 3711 | rice_parameter, |
| 3712 | rice_parameter_limit, |
| 3713 | rice_parameter_search_dist, |
| 3714 | (unsigned)partition_order, |
| 3715 | do_escape_coding, |
| 3716 | &private_->partitioned_rice_contents_extra[!best_parameters_index], |
| 3717 | &residual_bits |
| 3718 | ) |
| 3719 | ) |
| 3720 | { |
| 3721 | FLAC__ASSERT(best_residual_bits != 0); |
| 3722 | break; |
| 3723 | } |
| 3724 | sum += 1u << partition_order; |
| 3725 | if(best_residual_bits == 0 || residual_bits < best_residual_bits) { |
| 3726 | best_residual_bits = residual_bits; |
| 3727 | best_parameters_index = !best_parameters_index; |
| 3728 | best_partition_order = partition_order; |
| 3729 | } |
| 3730 | } |
| 3731 | } |
| 3732 | |
| 3733 | best_ecm->data.partitioned_rice.order = best_partition_order; |
| 3734 | |
| 3735 | { |
| 3736 | /* |
| 3737 | * We are allowed to de-const the pointer based on our special |
| 3738 | * knowledge; it is const to the outside world. |
| 3739 | */ |
| 3740 | FLAC__EntropyCodingMethod_PartitionedRiceContents* prc = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_ecm->data.partitioned_rice.contents; |
| 3741 | unsigned partition; |
| 3742 | |
| 3743 | /* save best parameters and raw_bits */ |
| 3744 | FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(prc, max(6, best_partition_order)); |
| 3745 | memcpy(prc->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partition_order))); |
| 3746 | if(do_escape_coding) |
| 3747 | memcpy(prc->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partition_order))); |
| 3748 | /* |
| 3749 | * Now need to check if the type should be changed to |
| 3750 | * FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2 based on the |
| 3751 | * size of the rice parameters. |
| 3752 | */ |
| 3753 | for(partition = 0; partition < (1u<<best_partition_order); partition++) { |
| 3754 | if(prc->parameters[partition] >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
| 3755 | best_ecm->type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2; |
| 3756 | break; |
| 3757 | } |
| 3758 | } |
| 3759 | } |
| 3760 | |
| 3761 | return best_residual_bits; |
| 3762 | } |
| 3763 | |
| 3764 | #if defined(FLAC__CPU_IA32) && !defined FLAC__NO_ASM && defined FLAC__HAS_NASM |
| 3765 | extern void precompute_partition_info_sums_32bit_asm_ia32_( |
| 3766 | const FLAC__int32 residual[], |
| 3767 | FLAC__uint64 abs_residual_partition_sums[], |
| 3768 | unsigned blocksize, |
| 3769 | unsigned predictor_order, |
| 3770 | unsigned min_partition_order, |
| 3771 | unsigned max_partition_order |
| 3772 | ); |
| 3773 | #endif |
| 3774 | |
| 3775 | void precompute_partition_info_sums_( |
| 3776 | const FLAC__int32 residual[], |
| 3777 | FLAC__uint64 abs_residual_partition_sums[], |
| 3778 | unsigned residual_samples, |
| 3779 | unsigned predictor_order, |
| 3780 | unsigned min_partition_order, |
| 3781 | unsigned max_partition_order, |
| 3782 | unsigned bps |
| 3783 | ) |
| 3784 | { |
| 3785 | const unsigned default_partition_samples = (residual_samples + predictor_order) >> max_partition_order; |
| 3786 | unsigned partitions = 1u << max_partition_order; |
| 3787 | |
| 3788 | FLAC__ASSERT(default_partition_samples > predictor_order); |
| 3789 | |
| 3790 | #if defined(FLAC__CPU_IA32) && !defined FLAC__NO_ASM && defined FLAC__HAS_NASM |
| 3791 | /* slightly pessimistic but still catches all common cases */ |
| 3792 | /* WATCHOUT: "+ bps" is an assumption that the average residual magnitude will not be more than "bps" bits */ |
| 3793 | if(FLAC__bitmath_ilog2(default_partition_samples) + bps < 32) { |
| 3794 | precompute_partition_info_sums_32bit_asm_ia32_(residual, abs_residual_partition_sums, residual_samples + predictor_order, predictor_order, min_partition_order, max_partition_order); |
| 3795 | return; |
| 3796 | } |
| 3797 | #endif |
| 3798 | |
| 3799 | /* first do max_partition_order */ |
| 3800 | { |
| 3801 | unsigned partition, residual_sample, end = (unsigned)(-(int)predictor_order); |
| 3802 | /* slightly pessimistic but still catches all common cases */ |
| 3803 | /* WATCHOUT: "+ bps" is an assumption that the average residual magnitude will not be more than "bps" bits */ |
| 3804 | if(FLAC__bitmath_ilog2(default_partition_samples) + bps < 32) { |
| 3805 | FLAC__uint32 abs_residual_partition_sum; |
| 3806 | |
| 3807 | for(partition = residual_sample = 0; partition < partitions; partition++) { |
| 3808 | end += default_partition_samples; |
| 3809 | abs_residual_partition_sum = 0; |
| 3810 | for( ; residual_sample < end; residual_sample++) |
| 3811 | abs_residual_partition_sum += abs(residual[residual_sample]); /* abs(INT_MIN) is undefined, but if the residual is INT_MIN we have bigger problems */ |
| 3812 | abs_residual_partition_sums[partition] = abs_residual_partition_sum; |
| 3813 | } |
| 3814 | } |
| 3815 | else { /* have to pessimistically use 64 bits for accumulator */ |
| 3816 | FLAC__uint64 abs_residual_partition_sum; |
| 3817 | |
| 3818 | for(partition = residual_sample = 0; partition < partitions; partition++) { |
| 3819 | end += default_partition_samples; |
| 3820 | abs_residual_partition_sum = 0; |
| 3821 | for( ; residual_sample < end; residual_sample++) |
| 3822 | abs_residual_partition_sum += abs(residual[residual_sample]); /* abs(INT_MIN) is undefined, but if the residual is INT_MIN we have bigger problems */ |
| 3823 | abs_residual_partition_sums[partition] = abs_residual_partition_sum; |
| 3824 | } |
| 3825 | } |
| 3826 | } |
| 3827 | |
| 3828 | /* now merge partitions for lower orders */ |
| 3829 | { |
| 3830 | unsigned from_partition = 0, to_partition = partitions; |
| 3831 | int partition_order; |
| 3832 | for(partition_order = (int)max_partition_order - 1; partition_order >= (int)min_partition_order; partition_order--) { |
| 3833 | unsigned i; |
| 3834 | partitions >>= 1; |
| 3835 | for(i = 0; i < partitions; i++) { |
| 3836 | abs_residual_partition_sums[to_partition++] = |
| 3837 | abs_residual_partition_sums[from_partition ] + |
| 3838 | abs_residual_partition_sums[from_partition+1]; |
| 3839 | from_partition += 2; |
| 3840 | } |
| 3841 | } |
| 3842 | } |
| 3843 | } |
| 3844 | |
| 3845 | void precompute_partition_info_escapes_( |
| 3846 | const FLAC__int32 residual[], |
| 3847 | unsigned raw_bits_per_partition[], |
| 3848 | unsigned residual_samples, |
| 3849 | unsigned predictor_order, |
| 3850 | unsigned min_partition_order, |
| 3851 | unsigned max_partition_order |
| 3852 | ) |
| 3853 | { |
| 3854 | int partition_order; |
| 3855 | unsigned from_partition, to_partition = 0; |
| 3856 | const unsigned blocksize = residual_samples + predictor_order; |
| 3857 | |
| 3858 | /* first do max_partition_order */ |
| 3859 | for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) { |
| 3860 | FLAC__int32 r; |
| 3861 | FLAC__uint32 rmax; |
| 3862 | unsigned partition, partition_sample, partition_samples, residual_sample; |
| 3863 | const unsigned partitions = 1u << partition_order; |
| 3864 | const unsigned default_partition_samples = blocksize >> partition_order; |
| 3865 | |
| 3866 | FLAC__ASSERT(default_partition_samples > predictor_order); |
| 3867 | |
| 3868 | for(partition = residual_sample = 0; partition < partitions; partition++) { |
| 3869 | partition_samples = default_partition_samples; |
| 3870 | if(partition == 0) |
| 3871 | partition_samples -= predictor_order; |
| 3872 | rmax = 0; |
| 3873 | for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) { |
| 3874 | r = residual[residual_sample++]; |
| 3875 | /* OPT: maybe faster: rmax |= r ^ (r>>31) */ |
| 3876 | if(r < 0) |
| 3877 | rmax |= ~r; |
| 3878 | else |
| 3879 | rmax |= r; |
| 3880 | } |
| 3881 | /* now we know all residual values are in the range [-rmax-1,rmax] */ |
| 3882 | raw_bits_per_partition[partition] = rmax? FLAC__bitmath_ilog2(rmax) + 2 : 1; |
| 3883 | } |
| 3884 | to_partition = partitions; |
| 3885 | break; /*@@@ yuck, should remove the 'for' loop instead */ |
| 3886 | } |
| 3887 | |
| 3888 | /* now merge partitions for lower orders */ |
| 3889 | for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) { |
| 3890 | unsigned m; |
| 3891 | unsigned i; |
| 3892 | const unsigned partitions = 1u << partition_order; |
| 3893 | for(i = 0; i < partitions; i++) { |
| 3894 | m = raw_bits_per_partition[from_partition]; |
| 3895 | from_partition++; |
| 3896 | raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]); |
| 3897 | from_partition++; |
| 3898 | to_partition++; |
| 3899 | } |
| 3900 | } |
| 3901 | } |
| 3902 | |
| 3903 | #ifdef EXACT_RICE_BITS_CALCULATION |
| 3904 | static FLaC__INLINE unsigned count_rice_bits_in_partition_( |
| 3905 | const unsigned rice_parameter, |
| 3906 | const unsigned partition_samples, |
| 3907 | const FLAC__int32 *residual |
| 3908 | ) |
| 3909 | { |
| 3910 | unsigned i, partition_bits = |
| 3911 | FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + /* actually could end up being FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN but err on side of 16bps */ |
| 3912 | (1+rice_parameter) * partition_samples /* 1 for unary stop bit + rice_parameter for the binary portion */ |
| 3913 | ; |
| 3914 | for(i = 0; i < partition_samples; i++) |
| 3915 | partition_bits += ( (FLAC__uint32)((residual[i]<<1)^(residual[i]>>31)) >> rice_parameter ); |
| 3916 | return partition_bits; |
| 3917 | } |
| 3918 | #else |
| 3919 | static FLaC__INLINE unsigned count_rice_bits_in_partition_( |
| 3920 | const unsigned rice_parameter, |
| 3921 | const unsigned partition_samples, |
| 3922 | const FLAC__uint64 abs_residual_partition_sum |
| 3923 | ) |
| 3924 | { |
| 3925 | return |
| 3926 | FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + /* actually could end up being FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN but err on side of 16bps */ |
| 3927 | (1+rice_parameter) * partition_samples + /* 1 for unary stop bit + rice_parameter for the binary portion */ |
| 3928 | ( |
| 3929 | rice_parameter? |
| 3930 | (unsigned)(abs_residual_partition_sum >> (rice_parameter-1)) /* rice_parameter-1 because the real coder sign-folds instead of using a sign bit */ |
| 3931 | : (unsigned)(abs_residual_partition_sum << 1) /* can't shift by negative number, so reverse */ |
| 3932 | ) |
| 3933 | - (partition_samples >> 1) |
| 3934 | /* -(partition_samples>>1) to subtract out extra contributions to the abs_residual_partition_sum. |
| 3935 | * The actual number of bits used is closer to the sum(for all i in the partition) of abs(residual[i])>>(rice_parameter-1) |
| 3936 | * By using the abs_residual_partition sum, we also add in bits in the LSBs that would normally be shifted out. |
| 3937 | * So the subtraction term tries to guess how many extra bits were contributed. |
| 3938 | * If the LSBs are randomly distributed, this should average to 0.5 extra bits per sample. |
| 3939 | */ |
| 3940 | ; |
| 3941 | } |
| 3942 | #endif |
| 3943 | |
| 3944 | FLAC__bool set_partitioned_rice_( |
| 3945 | #ifdef EXACT_RICE_BITS_CALCULATION |
| 3946 | const FLAC__int32 residual[], |
| 3947 | #endif |
| 3948 | const FLAC__uint64 abs_residual_partition_sums[], |
| 3949 | const unsigned raw_bits_per_partition[], |
| 3950 | const unsigned residual_samples, |
| 3951 | const unsigned predictor_order, |
| 3952 | const unsigned suggested_rice_parameter, |
| 3953 | const unsigned rice_parameter_limit, |
| 3954 | const unsigned rice_parameter_search_dist, |
| 3955 | const unsigned partition_order, |
| 3956 | const FLAC__bool search_for_escapes, |
| 3957 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, |
| 3958 | unsigned *bits |
| 3959 | ) |
| 3960 | { |
| 3961 | unsigned rice_parameter, partition_bits; |
| 3962 | unsigned best_partition_bits, best_rice_parameter = 0; |
| 3963 | unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN; |
| 3964 | unsigned *parameters, *raw_bits; |
| 3965 | #ifdef ENABLE_RICE_PARAMETER_SEARCH |
| 3966 | unsigned min_rice_parameter, max_rice_parameter; |
| 3967 | #else |
| 3968 | (void)rice_parameter_search_dist; |
| 3969 | #endif |
| 3970 | |
| 3971 | FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER); |
| 3972 | FLAC__ASSERT(rice_parameter_limit <= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER); |
| 3973 | |
| 3974 | FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order)); |
| 3975 | parameters = partitioned_rice_contents->parameters; |
| 3976 | raw_bits = partitioned_rice_contents->raw_bits; |
| 3977 | |
| 3978 | if(partition_order == 0) { |
| 3979 | best_partition_bits = (unsigned)(-1); |
| 3980 | #ifdef ENABLE_RICE_PARAMETER_SEARCH |
| 3981 | if(rice_parameter_search_dist) { |
| 3982 | if(suggested_rice_parameter < rice_parameter_search_dist) |
| 3983 | min_rice_parameter = 0; |
| 3984 | else |
| 3985 | min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist; |
| 3986 | max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist; |
| 3987 | if(max_rice_parameter >= rice_parameter_limit) { |
| 3988 | #ifdef DEBUG_VERBOSE |
| 3989 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, rice_parameter_limit - 1); |
| 3990 | #endif |
| 3991 | max_rice_parameter = rice_parameter_limit - 1; |
| 3992 | } |
| 3993 | } |
| 3994 | else |
| 3995 | min_rice_parameter = max_rice_parameter = suggested_rice_parameter; |
| 3996 | |
| 3997 | for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) { |
| 3998 | #else |
| 3999 | rice_parameter = suggested_rice_parameter; |
| 4000 | #endif |
| 4001 | #ifdef EXACT_RICE_BITS_CALCULATION |
| 4002 | partition_bits = count_rice_bits_in_partition_(rice_parameter, residual_samples, residual); |
| 4003 | #else |
| 4004 | partition_bits = count_rice_bits_in_partition_(rice_parameter, residual_samples, abs_residual_partition_sums[0]); |
| 4005 | #endif |
| 4006 | if(partition_bits < best_partition_bits) { |
| 4007 | best_rice_parameter = rice_parameter; |
| 4008 | best_partition_bits = partition_bits; |
| 4009 | } |
| 4010 | #ifdef ENABLE_RICE_PARAMETER_SEARCH |
| 4011 | } |
| 4012 | #endif |
| 4013 | if(search_for_escapes) { |
| 4014 | partition_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[0] * residual_samples; |
| 4015 | if(partition_bits <= best_partition_bits) { |
| 4016 | raw_bits[0] = raw_bits_per_partition[0]; |
| 4017 | best_rice_parameter = 0; /* will be converted to appropriate escape parameter later */ |
| 4018 | best_partition_bits = partition_bits; |
| 4019 | } |
| 4020 | else |
| 4021 | raw_bits[0] = 0; |
| 4022 | } |
| 4023 | parameters[0] = best_rice_parameter; |
| 4024 | bits_ += best_partition_bits; |
| 4025 | } |
| 4026 | else { |
| 4027 | unsigned partition, residual_sample; |
| 4028 | unsigned partition_samples; |
| 4029 | FLAC__uint64 mean, k; |
| 4030 | const unsigned partitions = 1u << partition_order; |
| 4031 | for(partition = residual_sample = 0; partition < partitions; partition++) { |
| 4032 | partition_samples = (residual_samples+predictor_order) >> partition_order; |
| 4033 | if(partition == 0) { |
| 4034 | if(partition_samples <= predictor_order) |
| 4035 | return false; |
| 4036 | else |
| 4037 | partition_samples -= predictor_order; |
| 4038 | } |
| 4039 | mean = abs_residual_partition_sums[partition]; |
| 4040 | /* we are basically calculating the size in bits of the |
| 4041 | * average residual magnitude in the partition: |
| 4042 | * rice_parameter = floor(log2(mean/partition_samples)) |
| 4043 | * 'mean' is not a good name for the variable, it is |
| 4044 | * actually the sum of magnitudes of all residual values |
| 4045 | * in the partition, so the actual mean is |
| 4046 | * mean/partition_samples |
| 4047 | */ |
| 4048 | for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1) |
| 4049 | ; |
| 4050 | if(rice_parameter >= rice_parameter_limit) { |
| 4051 | #ifdef DEBUG_VERBOSE |
| 4052 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, rice_parameter_limit - 1); |
| 4053 | #endif |
| 4054 | rice_parameter = rice_parameter_limit - 1; |
| 4055 | } |
| 4056 | |
| 4057 | best_partition_bits = (unsigned)(-1); |
| 4058 | #ifdef ENABLE_RICE_PARAMETER_SEARCH |
| 4059 | if(rice_parameter_search_dist) { |
| 4060 | if(rice_parameter < rice_parameter_search_dist) |
| 4061 | min_rice_parameter = 0; |
| 4062 | else |
| 4063 | min_rice_parameter = rice_parameter - rice_parameter_search_dist; |
| 4064 | max_rice_parameter = rice_parameter + rice_parameter_search_dist; |
| 4065 | if(max_rice_parameter >= rice_parameter_limit) { |
| 4066 | #ifdef DEBUG_VERBOSE |
| 4067 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, rice_parameter_limit - 1); |
| 4068 | #endif |
| 4069 | max_rice_parameter = rice_parameter_limit - 1; |
| 4070 | } |
| 4071 | } |
| 4072 | else |
| 4073 | min_rice_parameter = max_rice_parameter = rice_parameter; |
| 4074 | |
| 4075 | for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) { |
| 4076 | #endif |
| 4077 | #ifdef EXACT_RICE_BITS_CALCULATION |
| 4078 | partition_bits = count_rice_bits_in_partition_(rice_parameter, partition_samples, residual+residual_sample); |
| 4079 | #else |
| 4080 | partition_bits = count_rice_bits_in_partition_(rice_parameter, partition_samples, abs_residual_partition_sums[partition]); |
| 4081 | #endif |
| 4082 | if(partition_bits < best_partition_bits) { |
| 4083 | best_rice_parameter = rice_parameter; |
| 4084 | best_partition_bits = partition_bits; |
| 4085 | } |
| 4086 | #ifdef ENABLE_RICE_PARAMETER_SEARCH |
| 4087 | } |
| 4088 | #endif |
| 4089 | if(search_for_escapes) { |
| 4090 | partition_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[partition] * partition_samples; |
| 4091 | if(partition_bits <= best_partition_bits) { |
| 4092 | raw_bits[partition] = raw_bits_per_partition[partition]; |
| 4093 | best_rice_parameter = 0; /* will be converted to appropriate escape parameter later */ |
| 4094 | best_partition_bits = partition_bits; |
| 4095 | } |
| 4096 | else |
| 4097 | raw_bits[partition] = 0; |
| 4098 | } |
| 4099 | parameters[partition] = best_rice_parameter; |
| 4100 | bits_ += best_partition_bits; |
| 4101 | residual_sample += partition_samples; |
| 4102 | } |
| 4103 | } |
| 4104 | |
| 4105 | *bits = bits_; |
| 4106 | return true; |
| 4107 | } |
| 4108 | |
| 4109 | unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples) |
| 4110 | { |
| 4111 | unsigned i, shift; |
| 4112 | FLAC__int32 x = 0; |
| 4113 | |
| 4114 | for(i = 0; i < samples && !(x&1); i++) |
| 4115 | x |= signal[i]; |
| 4116 | |
| 4117 | if(x == 0) { |
| 4118 | shift = 0; |
| 4119 | } |
| 4120 | else { |
| 4121 | for(shift = 0; !(x&1); shift++) |
| 4122 | x >>= 1; |
| 4123 | } |
| 4124 | |
| 4125 | if(shift > 0) { |
| 4126 | for(i = 0; i < samples; i++) |
| 4127 | signal[i] >>= shift; |
| 4128 | } |
| 4129 | |
| 4130 | return shift; |
| 4131 | } |
| 4132 | |
| 4133 | void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples) |
| 4134 | { |
| 4135 | unsigned channel; |
| 4136 | |
| 4137 | for(channel = 0; channel < channels; channel++) |
| 4138 | memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples); |
| 4139 | |
| 4140 | fifo->tail += wide_samples; |
| 4141 | |
| 4142 | FLAC__ASSERT(fifo->tail <= fifo->size); |
| 4143 | } |
| 4144 | |
| 4145 | void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples) |
| 4146 | { |
| 4147 | unsigned channel; |
| 4148 | unsigned sample, wide_sample; |
| 4149 | unsigned tail = fifo->tail; |
| 4150 | |
| 4151 | sample = input_offset * channels; |
| 4152 | for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) { |
| 4153 | for(channel = 0; channel < channels; channel++) |
| 4154 | fifo->data[channel][tail] = input[sample++]; |
| 4155 | tail++; |
| 4156 | } |
| 4157 | fifo->tail = tail; |
| 4158 | |
| 4159 | FLAC__ASSERT(fifo->tail <= fifo->size); |
| 4160 | } |
| 4161 | |
| 4162 | FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data) |
| 4163 | { |
| 4164 | FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data; |
| 4165 | const size_t encoded_bytes = encoder->private_->verify.output.bytes; |
| 4166 | (void)decoder; |
| 4167 | |
| 4168 | if(encoder->private_->verify.needs_magic_hack) { |
| 4169 | FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH); |
| 4170 | *bytes = FLAC__STREAM_SYNC_LENGTH; |
| 4171 | memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes); |
| 4172 | encoder->private_->verify.needs_magic_hack = false; |
| 4173 | } |
| 4174 | else { |
| 4175 | if(encoded_bytes == 0) { |
| 4176 | /* |
| 4177 | * If we get here, a FIFO underflow has occurred, |
| 4178 | * which means there is a bug somewhere. |
| 4179 | */ |
| 4180 | FLAC__ASSERT(0); |
| 4181 | return FLAC__STREAM_DECODER_READ_STATUS_ABORT; |
| 4182 | } |
| 4183 | else if(encoded_bytes < *bytes) |
| 4184 | *bytes = encoded_bytes; |
| 4185 | memcpy(buffer, encoder->private_->verify.output.data, *bytes); |
| 4186 | encoder->private_->verify.output.data += *bytes; |
| 4187 | encoder->private_->verify.output.bytes -= *bytes; |
| 4188 | } |
| 4189 | |
| 4190 | return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; |
| 4191 | } |
| 4192 | |
| 4193 | FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data) |
| 4194 | { |
| 4195 | FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data; |
| 4196 | unsigned channel; |
| 4197 | const unsigned channels = frame->header.channels; |
| 4198 | const unsigned blocksize = frame->header.blocksize; |
| 4199 | const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize; |
| 4200 | |
| 4201 | (void)decoder; |
| 4202 | |
| 4203 | for(channel = 0; channel < channels; channel++) { |
| 4204 | if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) { |
| 4205 | unsigned i, sample = 0; |
| 4206 | FLAC__int32 expect = 0, got = 0; |
| 4207 | |
| 4208 | for(i = 0; i < blocksize; i++) { |
| 4209 | if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) { |
| 4210 | sample = i; |
| 4211 | expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i]; |
| 4212 | got = (FLAC__int32)buffer[channel][i]; |
| 4213 | break; |
| 4214 | } |
| 4215 | } |
| 4216 | FLAC__ASSERT(i < blocksize); |
| 4217 | FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER); |
| 4218 | encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample; |
| 4219 | encoder->private_->verify.error_stats.frame_number = (unsigned)(frame->header.number.sample_number / blocksize); |
| 4220 | encoder->private_->verify.error_stats.channel = channel; |
| 4221 | encoder->private_->verify.error_stats.sample = sample; |
| 4222 | encoder->private_->verify.error_stats.expected = expect; |
| 4223 | encoder->private_->verify.error_stats.got = got; |
| 4224 | encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA; |
| 4225 | return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; |
| 4226 | } |
| 4227 | } |
| 4228 | /* dequeue the frame from the fifo */ |
| 4229 | encoder->private_->verify.input_fifo.tail -= blocksize; |
| 4230 | FLAC__ASSERT(encoder->private_->verify.input_fifo.tail <= OVERREAD_); |
| 4231 | for(channel = 0; channel < channels; channel++) |
| 4232 | memmove(&encoder->private_->verify.input_fifo.data[channel][0], &encoder->private_->verify.input_fifo.data[channel][blocksize], encoder->private_->verify.input_fifo.tail * sizeof(encoder->private_->verify.input_fifo.data[0][0])); |
| 4233 | return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; |
| 4234 | } |
| 4235 | |
| 4236 | void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data) |
| 4237 | { |
| 4238 | (void)decoder, (void)metadata, (void)client_data; |
| 4239 | } |
| 4240 | |
| 4241 | void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) |
| 4242 | { |
| 4243 | FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data; |
| 4244 | (void)decoder, (void)status; |
| 4245 | encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR; |
| 4246 | } |
| 4247 | |
| 4248 | FLAC__StreamEncoderReadStatus file_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data) |
| 4249 | { |
| 4250 | (void)client_data; |
| 4251 | |
| 4252 | *bytes = fread(buffer, 1, *bytes, encoder->private_->file); |
| 4253 | if (*bytes == 0) { |
| 4254 | if (feof(encoder->private_->file)) |
| 4255 | return FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM; |
| 4256 | else if (ferror(encoder->private_->file)) |
| 4257 | return FLAC__STREAM_ENCODER_READ_STATUS_ABORT; |
| 4258 | } |
| 4259 | return FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE; |
| 4260 | } |
| 4261 | |
| 4262 | FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data) |
| 4263 | { |
| 4264 | (void)client_data; |
| 4265 | |
| 4266 | if(fseeko(encoder->private_->file, (off_t)absolute_byte_offset, SEEK_SET) < 0) |
| 4267 | return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR; |
| 4268 | else |
| 4269 | return FLAC__STREAM_ENCODER_SEEK_STATUS_OK; |
| 4270 | } |
| 4271 | |
| 4272 | FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data) |
| 4273 | { |
| 4274 | off_t offset; |
| 4275 | |
| 4276 | (void)client_data; |
| 4277 | |
| 4278 | offset = ftello(encoder->private_->file); |
| 4279 | |
| 4280 | if(offset < 0) { |
| 4281 | return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR; |
| 4282 | } |
| 4283 | else { |
| 4284 | *absolute_byte_offset = (FLAC__uint64)offset; |
| 4285 | return FLAC__STREAM_ENCODER_TELL_STATUS_OK; |
| 4286 | } |
| 4287 | } |
| 4288 | |
| 4289 | #ifdef FLAC__VALGRIND_TESTING |
| 4290 | static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) |
| 4291 | { |
| 4292 | size_t ret = fwrite(ptr, size, nmemb, stream); |
| 4293 | if(!ferror(stream)) |
| 4294 | fflush(stream); |
| 4295 | return ret; |
| 4296 | } |
| 4297 | #else |
| 4298 | #define local__fwrite fwrite |
| 4299 | #endif |
| 4300 | |
| 4301 | FLAC__StreamEncoderWriteStatus file_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame, void *client_data) |
| 4302 | { |
| 4303 | (void)client_data, (void)current_frame; |
| 4304 | |
| 4305 | if(local__fwrite(buffer, sizeof(FLAC__byte), bytes, encoder->private_->file) == bytes) { |
| 4306 | FLAC__bool call_it = 0 != encoder->private_->progress_callback && ( |
| 4307 | #if FLAC__HAS_OGG |
| 4308 | /* We would like to be able to use 'samples > 0' in the |
| 4309 | * clause here but currently because of the nature of our |
| 4310 | * Ogg writing implementation, 'samples' is always 0 (see |
| 4311 | * ogg_encoder_aspect.c). The downside is extra progress |
| 4312 | * callbacks. |
| 4313 | */ |
| 4314 | encoder->private_->is_ogg? true : |
| 4315 | #endif |
| 4316 | samples > 0 |
| 4317 | ); |
| 4318 | if(call_it) { |
| 4319 | /* NOTE: We have to add +bytes, +samples, and +1 to the stats |
| 4320 | * because at this point in the callback chain, the stats |
| 4321 | * have not been updated. Only after we return and control |
| 4322 | * gets back to write_frame_() are the stats updated |
| 4323 | */ |
| 4324 | encoder->private_->progress_callback(encoder, encoder->private_->bytes_written+bytes, encoder->private_->samples_written+samples, encoder->private_->frames_written+(samples?1:0), encoder->private_->total_frames_estimate, encoder->private_->client_data); |
| 4325 | } |
| 4326 | return FLAC__STREAM_ENCODER_WRITE_STATUS_OK; |
| 4327 | } |
| 4328 | else |
| 4329 | return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR; |
| 4330 | } |
| 4331 | |
| 4332 | /* |
| 4333 | * This will forcibly set stdout to binary mode (for OSes that require it) |
| 4334 | */ |
| 4335 | FILE *get_binary_stdout_(void) |
| 4336 | { |
| 4337 | #if 0 |
| 4338 | /* if something breaks here it is probably due to the presence or |
| 4339 | * absence of an underscore before the identifiers 'setmode', |
| 4340 | * 'fileno', and/or 'O_BINARY'; check your system header files. |
| 4341 | */ |
| 4342 | #if defined _MSC_VER || defined __MINGW32__ |
| 4343 | _setmode(_fileno(stdout), _O_BINARY); |
| 4344 | #elif defined __CYGWIN__ |
| 4345 | /* almost certainly not needed for any modern Cygwin, but let's be safe... */ |
| 4346 | setmode(_fileno(stdout), _O_BINARY); |
| 4347 | #elif defined __EMX__ |
| 4348 | setmode(fileno(stdout), O_BINARY); |
| 4349 | #endif |
| 4350 | #endif |
| 4351 | return stdout; |
| 4352 | } |