-
Notifications
You must be signed in to change notification settings - Fork 277
/
stream_encoder.c
5319 lines (4858 loc) · 219 KB
/
stream_encoder.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* libFLAC - Free Lossless Audio Codec library
* Copyright (C) 2000-2009 Josh Coalson
* Copyright (C) 2011-2024 Xiph.Org Foundation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of the Xiph.org Foundation nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <limits.h>
#include <stdio.h>
#include <stdlib.h> /* for malloc() */
#include <string.h> /* for memcpy() */
#include <sys/types.h> /* for off_t */
#ifdef _WIN32
#include <windows.h> /* for GetFileType() */
#include <io.h> /* for _get_osfhandle() */
#endif
#ifdef HAVE_PTHREAD
#include <pthread.h>
#endif
#include "share/compat.h"
#include "FLAC/assert.h"
#include "FLAC/stream_decoder.h"
#include "protected/stream_encoder.h"
#include "private/bitwriter.h"
#include "private/bitmath.h"
#include "private/crc.h"
#include "private/cpu.h"
#include "private/fixed.h"
#include "private/format.h"
#include "private/lpc.h"
#include "private/md5.h"
#include "private/memory.h"
#include "private/macros.h"
#if FLAC__HAS_OGG
#include "private/ogg_helper.h"
#include "private/ogg_mapping.h"
#endif
#include "private/stream_encoder.h"
#include "private/stream_encoder_framing.h"
#include "private/window.h"
#include "share/alloc.h"
#include "share/private.h"
/* Exact Rice codeword length calculation is off by default. The simple
* (and fast) estimation (of how many bits a residual value will be
* encoded with) in this encoder is very good, almost always yielding
* compression within 0.1% of exact calculation.
*/
#undef EXACT_RICE_BITS_CALCULATION
/* Rice parameter searching is off by default. The simple (and fast)
* parameter estimation in this encoder is very good, almost always
* yielding compression within 0.1% of the optimal parameters.
*/
#undef ENABLE_RICE_PARAMETER_SEARCH
#ifdef local_abs64
#undef local_abs64
#endif
#define local_abs64(x) ((uint64_t)((x)<0? -(x) : (x)))
typedef struct {
FLAC__int32 *data[FLAC__MAX_CHANNELS];
uint32_t size; /* of each data[] in samples */
uint32_t tail;
} verify_input_fifo;
typedef struct {
const FLAC__byte *data;
uint32_t capacity;
uint32_t bytes;
} verify_output;
#ifndef FLAC__INTEGER_ONLY_LIBRARY
typedef struct {
uint32_t a, b, c;
FLAC__ApodizationSpecification * current_apodization;
double autoc_root[FLAC__MAX_LPC_ORDER+1];
double autoc[FLAC__MAX_LPC_ORDER+1];
} apply_apodization_state_struct;
#endif
typedef enum {
ENCODER_IN_MAGIC = 0,
ENCODER_IN_METADATA = 1,
ENCODER_IN_AUDIO = 2
} EncoderStateHint;
static const struct CompressionLevels {
FLAC__bool do_mid_side_stereo;
FLAC__bool loose_mid_side_stereo;
uint32_t max_lpc_order;
uint32_t qlp_coeff_precision;
FLAC__bool do_qlp_coeff_prec_search;
FLAC__bool do_escape_coding;
FLAC__bool do_exhaustive_model_search;
uint32_t min_residual_partition_order;
uint32_t max_residual_partition_order;
uint32_t rice_parameter_search_dist;
const char *apodization;
} compression_levels_[] = {
{ false, false, 0, 0, false, false, false, 0, 3, 0, "tukey(5e-1)" },
{ true , true , 0, 0, false, false, false, 0, 3, 0, "tukey(5e-1)" },
{ true , false, 0, 0, false, false, false, 0, 3, 0, "tukey(5e-1)" },
{ false, false, 6, 0, false, false, false, 0, 4, 0, "tukey(5e-1)" },
{ true , true , 8, 0, false, false, false, 0, 4, 0, "tukey(5e-1)" },
{ true , false, 8, 0, false, false, false, 0, 5, 0, "tukey(5e-1)" },
{ true , false, 8, 0, false, false, false, 0, 6, 0, "subdivide_tukey(2)" },
{ true , false, 12, 0, false, false, false, 0, 6, 0, "subdivide_tukey(2)" },
{ true , false, 12, 0, false, false, false, 0, 6, 0, "subdivide_tukey(3)" }
/* here we use locale-independent 5e-1 instead of 0.5 or 0,5 */
};
/***********************************************************************
*
* Thread-private data
*
***********************************************************************/
typedef struct FLAC__StreamEncoderThreadTask {
FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
FLAC__int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */
FLAC__int64 *integer_signal_33bit_side; /* 33-bit side for 32-bit stereo decorrelation */
#ifndef FLAC__INTEGER_ONLY_LIBRARY
FLAC__real *windowed_signal; /* the integer_signal[] * current window[] */
#endif
uint32_t subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */
uint32_t subframe_bps_mid_side[2]; /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits + 0/1) */
FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
FLAC__int32 *residual_workspace_mid_side[2][2];
FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
FLAC__Subframe subframe_workspace_mid_side[2][2];
FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace[FLAC__MAX_CHANNELS][2];
FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace_mid_side[FLAC__MAX_CHANNELS][2];
FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr[FLAC__MAX_CHANNELS][2];
FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr_mid_side[FLAC__MAX_CHANNELS][2];
uint32_t best_subframe[FLAC__MAX_CHANNELS]; /* index (0 or 1) into 2nd dimension of the above workspaces */
uint32_t best_subframe_mid_side[2];
uint32_t best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
uint32_t best_subframe_bits_mid_side[2];
FLAC__uint64 *abs_residual_partition_sums; /* workspace where the sum of abs(candidate residual) for each partition is stored */
uint32_t *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */
FLAC__BitWriter *frame; /* the current frame being worked on */
uint32_t current_frame_number;
/* unaligned (original) pointers to allocated data */
FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
FLAC__int32 *integer_signal_mid_side_unaligned[2];
FLAC__int64 *integer_signal_33bit_side_unaligned;
#ifndef FLAC__INTEGER_ONLY_LIBRARY
FLAC__real *windowed_signal_unaligned;
#endif
FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
FLAC__uint64 *abs_residual_partition_sums_unaligned;
uint32_t *raw_bits_per_partition_unaligned;
/*
* These fields have been moved here from private function local
* declarations merely to save stack space during encoding.
*/
#ifndef FLAC__INTEGER_ONLY_LIBRARY
FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */
#endif
FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_extra[2]; /* from find_best_partition_order_() */
FLAC__bool disable_constant_subframes;
#ifdef HAVE_PTHREAD
pthread_mutex_t mutex_this_task; /* To lock whole threadtask */
pthread_cond_t cond_task_done;
FLAC__bool task_done;
FLAC__bool returnvalue;
#endif
} FLAC__StreamEncoderThreadTask;
/***********************************************************************
*
* Private class method prototypes
*
***********************************************************************/
static void set_defaults_(FLAC__StreamEncoder *encoder);
static void free_(FLAC__StreamEncoder *encoder);
static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, uint32_t new_blocksize);
static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderThreadTask *threadtask, uint32_t samples, FLAC__bool is_last_block);
static FLAC__StreamEncoderWriteStatus write_frame_(FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, uint32_t samples, FLAC__bool is_last_block);
static void update_metadata_(const FLAC__StreamEncoder *encoder);
#if FLAC__HAS_OGG
static void update_ogg_metadata_(FLAC__StreamEncoder *encoder);
#endif
static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_block);
void * process_frame_thread_(void * encoder);
FLAC__bool process_frame_thread_inner_(FLAC__StreamEncoder * encoder, FLAC__StreamEncoderThreadTask *threadtask);
static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderThreadTask *threadtask);
static FLAC__bool process_subframe_(
FLAC__StreamEncoder *encoder,
FLAC__StreamEncoderThreadTask *threadtask,
uint32_t min_partition_order,
uint32_t max_partition_order,
const FLAC__FrameHeader *frame_header,
uint32_t subframe_bps,
const void *integer_signal,
FLAC__Subframe *subframe[2],
FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
FLAC__int32 *residual[2],
uint32_t *best_subframe,
uint32_t *best_bits
);
#ifndef FLAC__INTEGER_ONLY_LIBRARY
static FLAC__bool apply_apodization_(
FLAC__StreamEncoder *encoder,
FLAC__StreamEncoderThreadTask *threadtask,
apply_apodization_state_struct *apply_apodization_state,
uint32_t blocksize,
double *lpc_error,
uint32_t *max_lpc_order_this_apodization,
uint32_t subframe_bps,
const void *integer_signal,
uint32_t *guess_lpc_order
);
#endif
static FLAC__bool add_subframe_(
FLAC__StreamEncoder *encoder,
uint32_t blocksize,
uint32_t subframe_bps,
const FLAC__Subframe *subframe,
FLAC__BitWriter *frame
);
static uint32_t evaluate_constant_subframe_(
FLAC__StreamEncoder *encoder,
const FLAC__int64 signal,
uint32_t blocksize,
uint32_t subframe_bps,
FLAC__Subframe *subframe
);
static uint32_t evaluate_fixed_subframe_(
FLAC__StreamEncoder *encoder,
FLAC__StreamEncoderThreadTask *threadtask,
const void *signal,
FLAC__int32 residual[],
FLAC__uint64 abs_residual_partition_sums[],
uint32_t raw_bits_per_partition[],
uint32_t blocksize,
uint32_t subframe_bps,
uint32_t order,
uint32_t rice_parameter_limit,
uint32_t min_partition_order,
uint32_t max_partition_order,
FLAC__bool do_escape_coding,
uint32_t rice_parameter_search_dist,
FLAC__Subframe *subframe,
FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
);
#ifndef FLAC__INTEGER_ONLY_LIBRARY
static uint32_t evaluate_lpc_subframe_(
FLAC__StreamEncoder *encoder,
FLAC__StreamEncoderThreadTask *threadtask,
const void *signal,
FLAC__int32 residual[],
FLAC__uint64 abs_residual_partition_sums[],
uint32_t raw_bits_per_partition[],
const FLAC__real lp_coeff[],
uint32_t blocksize,
uint32_t subframe_bps,
uint32_t order,
uint32_t qlp_coeff_precision,
uint32_t rice_parameter_limit,
uint32_t min_partition_order,
uint32_t max_partition_order,
FLAC__bool do_escape_coding,
uint32_t rice_parameter_search_dist,
FLAC__Subframe *subframe,
FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
);
#endif
static uint32_t evaluate_verbatim_subframe_(
FLAC__StreamEncoder *encoder,
const void *signal,
uint32_t blocksize,
uint32_t subframe_bps,
FLAC__Subframe *subframe
);
static uint32_t find_best_partition_order_(
struct FLAC__StreamEncoderPrivate *private_,
FLAC__StreamEncoderThreadTask *threadtask,
const FLAC__int32 residual[],
FLAC__uint64 abs_residual_partition_sums[],
uint32_t raw_bits_per_partition[],
uint32_t residual_samples,
uint32_t predictor_order,
uint32_t rice_parameter_limit,
uint32_t min_partition_order,
uint32_t max_partition_order,
uint32_t bps,
FLAC__bool do_escape_coding,
uint32_t rice_parameter_search_dist,
FLAC__EntropyCodingMethod *best_ecm
);
static void precompute_partition_info_sums_(
const FLAC__int32 residual[],
FLAC__uint64 abs_residual_partition_sums[],
uint32_t residual_samples,
uint32_t predictor_order,
uint32_t min_partition_order,
uint32_t max_partition_order,
uint32_t bps
);
static void precompute_partition_info_escapes_(
const FLAC__int32 residual[],
uint32_t raw_bits_per_partition[],
uint32_t residual_samples,
uint32_t predictor_order,
uint32_t min_partition_order,
uint32_t max_partition_order
);
static FLAC__bool set_partitioned_rice_(
#ifdef EXACT_RICE_BITS_CALCULATION
const FLAC__int32 residual[],
#endif
const FLAC__uint64 abs_residual_partition_sums[],
const uint32_t raw_bits_per_partition[],
const uint32_t residual_samples,
const uint32_t predictor_order,
const uint32_t rice_parameter_limit,
const uint32_t rice_parameter_search_dist,
const uint32_t partition_order,
const FLAC__bool search_for_escapes,
FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
uint32_t *bits
);
static uint32_t get_wasted_bits_(FLAC__int32 signal[], uint32_t samples);
static uint32_t get_wasted_bits_wide_(FLAC__int64 signal_wide[], FLAC__int32 signal[], uint32_t samples);
/* verify-related routines: */
static void append_to_verify_fifo_(
verify_input_fifo *fifo,
const FLAC__int32 * const input[],
uint32_t input_offset,
uint32_t channels,
uint32_t wide_samples
);
static void append_to_verify_fifo_interleaved_(
verify_input_fifo *fifo,
const FLAC__int32 input[],
uint32_t input_offset,
uint32_t channels,
uint32_t wide_samples
);
static FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
static FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
static void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
static void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
static FLAC__StreamEncoderReadStatus file_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
static FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data);
static FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
static FLAC__StreamEncoderWriteStatus file_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, uint32_t samples, uint32_t current_frame, void *client_data);
static FILE *get_binary_stdout_(void);
/***********************************************************************
*
* Private class data
*
***********************************************************************/
typedef struct FLAC__StreamEncoderPrivate {
FLAC__StreamEncoderThreadTask * threadtask[FLAC__STREAM_ENCODER_MAX_THREADTASKS];
#ifdef HAVE_PTHREAD
pthread_t thread[FLAC__STREAM_ENCODER_MAX_THREADS];
#endif
uint32_t input_capacity; /* current size (in samples) of the signal and residual buffers */
#ifndef FLAC__INTEGER_ONLY_LIBRARY
FLAC__real *window[FLAC__MAX_APODIZATION_FUNCTIONS]; /* the pre-computed floating-point window for each apodization function */
FLAC__real *window_unaligned[FLAC__MAX_APODIZATION_FUNCTIONS];
#endif
FLAC__StreamMetadata streaminfo; /* scratchpad for STREAMINFO as it is built */
FLAC__StreamMetadata_SeekTable *seek_table; /* pointer into encoder->protected_->metadata_ where the seek table is */
uint32_t current_sample_number;
uint32_t current_frame_number;
FLAC__MD5Context md5context;
FLAC__CPUInfo cpuinfo;
void (*local_precompute_partition_info_sums)(const FLAC__int32 residual[], FLAC__uint64 abs_residual_partition_sums[], uint32_t residual_samples, uint32_t predictor_order, uint32_t min_partition_order, uint32_t max_partition_order, uint32_t bps);
#ifndef FLAC__INTEGER_ONLY_LIBRARY
uint32_t (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], uint32_t data_len, float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
uint32_t (*local_fixed_compute_best_predictor_wide)(const FLAC__int32 data[], uint32_t data_len, float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
uint32_t (*local_fixed_compute_best_predictor_limit_residual)(const FLAC__int32 data[], uint32_t data_len, float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
#else
uint32_t (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], uint32_t data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
uint32_t (*local_fixed_compute_best_predictor_wide)(const FLAC__int32 data[], uint32_t data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
uint32_t (*local_fixed_compute_best_predictor_limit_residual)(const FLAC__int32 data[], uint32_t data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
#endif
#ifndef FLAC__INTEGER_ONLY_LIBRARY
void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], uint32_t data_len, uint32_t lag, double autoc[]);
void (*local_lpc_compute_residual_from_qlp_coefficients)(const FLAC__int32 *data, uint32_t data_len, const FLAC__int32 qlp_coeff[], uint32_t order, int lp_quantization, FLAC__int32 residual[]);
void (*local_lpc_compute_residual_from_qlp_coefficients_64bit)(const FLAC__int32 *data, uint32_t data_len, const FLAC__int32 qlp_coeff[], uint32_t order, int lp_quantization, FLAC__int32 residual[]);
void (*local_lpc_compute_residual_from_qlp_coefficients_16bit)(const FLAC__int32 *data, uint32_t data_len, const FLAC__int32 qlp_coeff[], uint32_t order, int lp_quantization, FLAC__int32 residual[]);
#endif
FLAC__bool disable_mmx;
FLAC__bool disable_sse2;
FLAC__bool disable_ssse3;
FLAC__bool disable_sse41;
FLAC__bool disable_sse42;
FLAC__bool disable_avx2;
FLAC__bool disable_fma;
FLAC__bool disable_constant_subframes;
FLAC__bool disable_fixed_subframes;
FLAC__bool disable_verbatim_subframes;
FLAC__bool is_ogg;
FLAC__StreamEncoderReadCallback read_callback; /* currently only needed for Ogg FLAC */
FLAC__StreamEncoderSeekCallback seek_callback;
FLAC__StreamEncoderTellCallback tell_callback;
FLAC__StreamEncoderWriteCallback write_callback;
FLAC__StreamEncoderMetadataCallback metadata_callback;
FLAC__StreamEncoderProgressCallback progress_callback;
void *client_data;
uint32_t first_seekpoint_to_check;
FILE *file; /* only used when encoding to a file */
FLAC__uint64 bytes_written;
FLAC__uint64 samples_written;
uint32_t frames_written;
uint32_t total_frames_estimate;
/*
* The data for the verify section
*/
struct {
FLAC__StreamDecoder *decoder;
EncoderStateHint state_hint;
FLAC__bool needs_magic_hack;
verify_input_fifo input_fifo;
verify_output output;
struct {
FLAC__uint64 absolute_sample;
uint32_t frame_number;
uint32_t channel;
uint32_t sample;
FLAC__int32 expected;
FLAC__int32 got;
} error_stats;
} verify;
FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */
uint32_t num_threadtasks;
#ifdef HAVE_PTHREAD
uint32_t num_created_threads;
uint32_t next_thread; /* This is the next thread that needs start, or needs to finish and be restarted */
uint32_t num_started_threadtasks;
uint32_t num_available_threadtasks; /* Number of threadtasks that are available to work on */
uint32_t num_running_threads;
uint32_t next_threadtask; /* Next threadtask that is available to work on */
pthread_mutex_t mutex_md5_fifo;
pthread_mutex_t mutex_work_queue; /* To lock work related variables in this struct */
pthread_cond_t cond_md5_emptied; /* To signal to main thread that MD5 queue has been emptied */
pthread_cond_t cond_work_available; /* To signal to threads that work is available */
pthread_cond_t cond_wake_up_thread; /* To signal that one sleeping thread can wake up */
FLAC__bool md5_active;
FLAC__bool finish_work_threads;
int32_t overcommitted_indicator;
verify_input_fifo md5_fifo;
#endif
} FLAC__StreamEncoderPrivate;
/***********************************************************************
*
* Public static class data
*
***********************************************************************/
FLAC_API const char * const FLAC__StreamEncoderStateString[] = {
"FLAC__STREAM_ENCODER_OK",
"FLAC__STREAM_ENCODER_UNINITIALIZED",
"FLAC__STREAM_ENCODER_OGG_ERROR",
"FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR",
"FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA",
"FLAC__STREAM_ENCODER_CLIENT_ERROR",
"FLAC__STREAM_ENCODER_IO_ERROR",
"FLAC__STREAM_ENCODER_FRAMING_ERROR",
"FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR"
};
FLAC_API const char * const FLAC__StreamEncoderInitStatusString[] = {
"FLAC__STREAM_ENCODER_INIT_STATUS_OK",
"FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR",
"FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER",
"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS",
"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS",
"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE",
"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE",
"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE",
"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER",
"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION",
"FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
"FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE",
"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA",
"FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED"
};
FLAC_API const char * const FLAC__StreamEncoderReadStatusString[] = {
"FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE",
"FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM",
"FLAC__STREAM_ENCODER_READ_STATUS_ABORT",
"FLAC__STREAM_ENCODER_READ_STATUS_UNSUPPORTED"
};
FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = {
"FLAC__STREAM_ENCODER_WRITE_STATUS_OK",
"FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
};
FLAC_API const char * const FLAC__StreamEncoderSeekStatusString[] = {
"FLAC__STREAM_ENCODER_SEEK_STATUS_OK",
"FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR",
"FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED"
};
FLAC_API const char * const FLAC__StreamEncoderTellStatusString[] = {
"FLAC__STREAM_ENCODER_TELL_STATUS_OK",
"FLAC__STREAM_ENCODER_TELL_STATUS_ERROR",
"FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED"
};
/* Number of samples that will be overread to watch for end of stream. By
* 'overread', we mean that the FLAC__stream_encoder_process*() calls will
* always try to read blocksize+1 samples before encoding a block, so that
* even if the stream has a total sample count that is an integral multiple
* of the blocksize, we will still notice when we are encoding the last
* block. This is needed, for example, to correctly set the end-of-stream
* marker in Ogg FLAC.
*
* WATCHOUT: some parts of the code assert that OVERREAD_ == 1 and there's
* not really any reason to change it.
*/
static const uint32_t OVERREAD_ = 1;
/***********************************************************************
*
* Class constructor/destructor
*
*/
FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new(void)
{
FLAC__StreamEncoder *encoder;
uint32_t i;
FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
encoder = safe_calloc_(1, sizeof(FLAC__StreamEncoder));
if(encoder == 0) {
return 0;
}
encoder->protected_ = safe_calloc_(1, sizeof(FLAC__StreamEncoderProtected));
if(encoder->protected_ == 0) {
free(encoder);
return 0;
}
encoder->private_ = safe_calloc_(1, sizeof(FLAC__StreamEncoderPrivate));
if(encoder->private_ == 0) {
free(encoder->protected_);
free(encoder);
return 0;
}
encoder->private_->threadtask[0] = safe_calloc_(1, sizeof(FLAC__StreamEncoderThreadTask));
if(encoder->private_->threadtask[0] == 0) {
free(encoder->private_);
free(encoder->protected_);
free(encoder);
return 0;
}
encoder->private_->threadtask[0]->frame = FLAC__bitwriter_new();
if(encoder->private_->threadtask[0]->frame == 0) {
free(encoder->private_->threadtask[0]);
free(encoder->private_);
free(encoder->protected_);
free(encoder);
return 0;
}
encoder->private_->file = 0;
encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
set_defaults_(encoder);
encoder->private_->is_being_deleted = false;
for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
encoder->private_->threadtask[0]->subframe_workspace_ptr[i][0] = &encoder->private_->threadtask[0]->subframe_workspace[i][0];
encoder->private_->threadtask[0]->subframe_workspace_ptr[i][1] = &encoder->private_->threadtask[0]->subframe_workspace[i][1];
}
for(i = 0; i < 2; i++) {
encoder->private_->threadtask[0]->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->threadtask[0]->subframe_workspace_mid_side[i][0];
encoder->private_->threadtask[0]->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->threadtask[0]->subframe_workspace_mid_side[i][1];
}
for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
encoder->private_->threadtask[0]->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->threadtask[0]->partitioned_rice_contents_workspace[i][0];
encoder->private_->threadtask[0]->partitioned_rice_contents_workspace_ptr[i][1] = &encoder->private_->threadtask[0]->partitioned_rice_contents_workspace[i][1];
}
for(i = 0; i < 2; i++) {
encoder->private_->threadtask[0]->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->threadtask[0]->partitioned_rice_contents_workspace_mid_side[i][0];
encoder->private_->threadtask[0]->partitioned_rice_contents_workspace_ptr_mid_side[i][1] = &encoder->private_->threadtask[0]->partitioned_rice_contents_workspace_mid_side[i][1];
}
for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->threadtask[0]->partitioned_rice_contents_workspace[i][0]);
FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->threadtask[0]->partitioned_rice_contents_workspace[i][1]);
}
for(i = 0; i < 2; i++) {
FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->threadtask[0]->partitioned_rice_contents_workspace_mid_side[i][0]);
FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->threadtask[0]->partitioned_rice_contents_workspace_mid_side[i][1]);
}
for(i = 0; i < 2; i++)
FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->threadtask[0]->partitioned_rice_contents_extra[i]);
return encoder;
}
FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
{
uint32_t i;
if (encoder == NULL)
return ;
FLAC__ASSERT(0 != encoder->protected_);
FLAC__ASSERT(0 != encoder->private_);
FLAC__ASSERT(0 != encoder->private_->threadtask[0]);
FLAC__ASSERT(0 != encoder->private_->threadtask[0]->frame);
encoder->private_->is_being_deleted = true;
(void)FLAC__stream_encoder_finish(encoder);
if(0 != encoder->private_->verify.decoder)
FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->threadtask[0]->partitioned_rice_contents_workspace[i][0]);
FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->threadtask[0]->partitioned_rice_contents_workspace[i][1]);
}
for(i = 0; i < 2; i++) {
FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->threadtask[0]->partitioned_rice_contents_workspace_mid_side[i][0]);
FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->threadtask[0]->partitioned_rice_contents_workspace_mid_side[i][1]);
}
for(i = 0; i < 2; i++)
FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->threadtask[0]->partitioned_rice_contents_extra[i]);
FLAC__bitwriter_delete(encoder->private_->threadtask[0]->frame);
free(encoder->private_->threadtask[0]);
free(encoder->private_);
free(encoder->protected_);
free(encoder);
}
/***********************************************************************
*
* Public class methods
*
***********************************************************************/
static FLAC__StreamEncoderInitStatus init_stream_internal_(
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,
FLAC__bool is_ogg
)
{
uint32_t i, t;
FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment, metadata_picture_has_type1, metadata_picture_has_type2;
FLAC__ASSERT(0 != encoder);
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
if(FLAC__HAS_OGG == 0 && is_ogg)
return FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER;
if(0 == write_callback || (seek_callback && 0 == tell_callback))
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS;
if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS;
if(encoder->protected_->channels != 2) {
encoder->protected_->do_mid_side_stereo = false;
encoder->protected_->loose_mid_side_stereo = false;
}
else if(!encoder->protected_->do_mid_side_stereo)
encoder->protected_->loose_mid_side_stereo = false;
if(encoder->protected_->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected_->bits_per_sample > FLAC__MAX_BITS_PER_SAMPLE)
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE;
if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE;
if(encoder->protected_->blocksize == 0) {
if(encoder->protected_->max_lpc_order == 0)
encoder->protected_->blocksize = 1152;
else
encoder->protected_->blocksize = 4096;
}
if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE;
if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER)
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER;
if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
return FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
if(encoder->protected_->qlp_coeff_precision == 0) {
if(encoder->protected_->bits_per_sample < 16) {
/* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
/* @@@ until then we'll make a guess */
encoder->protected_->qlp_coeff_precision = flac_max(FLAC__MIN_QLP_COEFF_PRECISION, 2 + encoder->protected_->bits_per_sample / 2);
}
else if(encoder->protected_->bits_per_sample == 16) {
if(encoder->protected_->blocksize <= 192)
encoder->protected_->qlp_coeff_precision = 7;
else if(encoder->protected_->blocksize <= 384)
encoder->protected_->qlp_coeff_precision = 8;
else if(encoder->protected_->blocksize <= 576)
encoder->protected_->qlp_coeff_precision = 9;
else if(encoder->protected_->blocksize <= 1152)
encoder->protected_->qlp_coeff_precision = 10;
else if(encoder->protected_->blocksize <= 2304)
encoder->protected_->qlp_coeff_precision = 11;
else if(encoder->protected_->blocksize <= 4608)
encoder->protected_->qlp_coeff_precision = 12;
else
encoder->protected_->qlp_coeff_precision = 13;
}
else {
if(encoder->protected_->blocksize <= 384)
encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-2;
else if(encoder->protected_->blocksize <= 1152)
encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-1;
else
encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
}
FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION);
}
else if(encoder->protected_->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected_->qlp_coeff_precision > FLAC__MAX_QLP_COEFF_PRECISION)
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION;
if(encoder->protected_->streamable_subset) {
if(!FLAC__format_blocksize_is_subset(encoder->protected_->blocksize, encoder->protected_->sample_rate))
return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
if(!FLAC__format_sample_rate_is_subset(encoder->protected_->sample_rate))
return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
if(
encoder->protected_->bits_per_sample != 8 &&
encoder->protected_->bits_per_sample != 12 &&
encoder->protected_->bits_per_sample != 16 &&
encoder->protected_->bits_per_sample != 20 &&
encoder->protected_->bits_per_sample != 24 &&
encoder->protected_->bits_per_sample != 32
)
return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
if(encoder->protected_->max_residual_partition_order > FLAC__SUBSET_MAX_RICE_PARTITION_ORDER)
return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
if(
encoder->protected_->sample_rate <= 48000 &&
(
encoder->protected_->blocksize > FLAC__SUBSET_MAX_BLOCK_SIZE_48000HZ ||
encoder->protected_->max_lpc_order > FLAC__SUBSET_MAX_LPC_ORDER_48000HZ
)
) {
return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
}
}
if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
#if FLAC__HAS_OGG
/* drop any seektable for ogg */
if(is_ogg && 0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0) {
uint32_t i1;
for(i1 = 0; i1 < encoder->protected_->num_metadata_blocks; i1++) {
if(0 != encoder->protected_->metadata[i1] && encoder->protected_->metadata[i1]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
encoder->protected_->num_metadata_blocks--;
for( ; i1 < encoder->protected_->num_metadata_blocks; i1++)
encoder->protected_->metadata[i1] = encoder->protected_->metadata[i1+1];
break;
}
}
}
/* reorder metadata if necessary to ensure that any VORBIS_COMMENT is the first, according to the mapping spec */
if(is_ogg && 0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 1) {
uint32_t i1;
for(i1 = 1; i1 < encoder->protected_->num_metadata_blocks; i1++) {
if(0 != encoder->protected_->metadata[i1] && encoder->protected_->metadata[i1]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
FLAC__StreamMetadata *vc = encoder->protected_->metadata[i1];
for( ; i1 > 0; i1--)
encoder->protected_->metadata[i1] = encoder->protected_->metadata[i1-1];
encoder->protected_->metadata[0] = vc;
break;
}
}
}
#endif
/* keep track of any SEEKTABLE block */
if(0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0) {
uint32_t i2;
for(i2 = 0; i2 < encoder->protected_->num_metadata_blocks; i2++) {
if(0 != encoder->protected_->metadata[i2] && encoder->protected_->metadata[i2]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
encoder->private_->seek_table = &encoder->protected_->metadata[i2]->data.seek_table;
break; /* take only the first one */
}
}
}
/* validate metadata */
if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
metadata_has_seektable = false;
metadata_has_vorbis_comment = false;
metadata_picture_has_type1 = false;
metadata_picture_has_type2 = false;
for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
const FLAC__StreamMetadata *m = encoder->protected_->metadata[i];
if(m->type == FLAC__METADATA_TYPE_STREAMINFO)
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
else if(m->type == FLAC__METADATA_TYPE_SEEKTABLE) {
if(metadata_has_seektable) /* only one is allowed */
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
metadata_has_seektable = true;
if(!FLAC__format_seektable_is_legal(&m->data.seek_table))
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
}
else if(m->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
if(metadata_has_vorbis_comment) /* only one is allowed */
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
metadata_has_vorbis_comment = true;
}
else if(m->type == FLAC__METADATA_TYPE_CUESHEET) {
if(!FLAC__format_cuesheet_is_legal(&m->data.cue_sheet, m->data.cue_sheet.is_cd, /*violation=*/0))
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
}
else if(m->type == FLAC__METADATA_TYPE_PICTURE) {
if(!FLAC__format_picture_is_legal(&m->data.picture, /*violation=*/0))
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD) {
if(metadata_picture_has_type1) /* there should only be 1 per stream */
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
metadata_picture_has_type1 = true;
/* standard icon must be 32x32 pixel PNG */
if(
m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD &&
(
(strcmp(m->data.picture.mime_type, "image/png") && strcmp(m->data.picture.mime_type, "-->")) ||
m->data.picture.width != 32 ||
m->data.picture.height != 32
)
)
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
}
else if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON) {
if(metadata_picture_has_type2) /* there should only be 1 per stream */
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
metadata_picture_has_type2 = true;
}
}
}
encoder->private_->input_capacity = 0;
encoder->private_->current_sample_number = 0;
encoder->private_->current_frame_number = 0;
/*
* get the CPU info and set the function pointers
*/
FLAC__cpu_info(&encoder->private_->cpuinfo);
/* remove cpu info as requested by
* FLAC__stream_encoder_disable_instruction_set */
if(encoder->private_->disable_mmx)
encoder->private_->cpuinfo.x86.mmx = false;
if(encoder->private_->disable_sse2)
encoder->private_->cpuinfo.x86.sse2 = false;
if(encoder->private_->disable_ssse3)
encoder->private_->cpuinfo.x86.ssse3 = false;
if(encoder->private_->disable_sse41)
encoder->private_->cpuinfo.x86.sse41 = false;
if(encoder->private_->disable_sse42)
encoder->private_->cpuinfo.x86.sse42 = false;
if(encoder->private_->disable_avx2)
encoder->private_->cpuinfo.x86.avx2 = false;
if(encoder->private_->disable_fma)
encoder->private_->cpuinfo.x86.fma = false;
/* first default to the non-asm routines */
#ifndef FLAC__INTEGER_ONLY_LIBRARY
encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
#endif
encoder->private_->local_precompute_partition_info_sums = precompute_partition_info_sums_;
encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
encoder->private_->local_fixed_compute_best_predictor_wide = FLAC__fixed_compute_best_predictor_wide;
encoder->private_->local_fixed_compute_best_predictor_limit_residual = FLAC__fixed_compute_best_predictor_limit_residual;
#ifndef FLAC__INTEGER_ONLY_LIBRARY
encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit = FLAC__lpc_compute_residual_from_qlp_coefficients_wide;
encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
#endif
/* now override with asm where appropriate */
#ifndef FLAC__INTEGER_ONLY_LIBRARY
# ifndef FLAC__NO_ASM
#if defined FLAC__CPU_ARM64 && FLAC__HAS_NEONINTRIN
#if FLAC__HAS_A64NEONINTRIN
if(encoder->protected_->max_lpc_order < 8)
encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_neon_lag_8;
else if(encoder->protected_->max_lpc_order < 10)
encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_neon_lag_10;
else if(encoder->protected_->max_lpc_order < 14)
encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_neon_lag_14;
else
encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
#endif
encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_intrin_neon;
encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_intrin_neon;
encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit = FLAC__lpc_compute_residual_from_qlp_coefficients_wide_intrin_neon;
#endif /* defined FLAC__CPU_ARM64 && FLAC__HAS_NEONINTRIN */
if(encoder->private_->cpuinfo.use_asm) {
# ifdef FLAC__CPU_IA32
FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
# if FLAC__HAS_X86INTRIN
# ifdef FLAC__SSE2_SUPPORTED
if (encoder->private_->cpuinfo.x86.sse2) {
if(encoder->protected_->max_lpc_order < 8)
encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_sse2_lag_8;
else if(encoder->protected_->max_lpc_order < 10)
encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_sse2_lag_10;
else if(encoder->protected_->max_lpc_order < 14)
encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_sse2_lag_14;
encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_intrin_sse2;
encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_16_intrin_sse2;
}
# endif
# ifdef FLAC__SSE4_1_SUPPORTED
if (encoder->private_->cpuinfo.x86.sse41) {
encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_intrin_sse41;
encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit = FLAC__lpc_compute_residual_from_qlp_coefficients_wide_intrin_sse41;
}
# endif