-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.h
142 lines (113 loc) · 4.62 KB
/
common.h
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
/*Functions and structs common to frontend*/
#ifndef COMMON
#define COMMON
#include "FLAC/stream_encoder.h"
#include "dr_wav.h"
#include <inttypes.h>
#include <omp.h>
#include <time.h>
#ifdef USE_OPENSSL
#include <openssl/md5.h>
#else
#include "mbedtls/md5.h"
typedef mbedtls_md5_context MD5_CTX;
#endif
enum{LAST_UNDEFINED, LAST_HEADER, LAST_SEEKTABLE, LAST_PRESERVED};
enum{MODE_CHUNK, MODE_GSET, MODE_PEAKSET, MODE_GASC, MODE_FIXED};
enum{UI_UNDEFINED, UI_PRESET, UI_MANUAL};
typedef struct{
int *blocks, diff_comp_settings, tweak, merge, mode, wildcard, outperc, queue_size, md5, lpc_order_limit, rice_order_limit, work_count, peakset_window, seek;
size_t blocks_count;
char *input_format;
char *comp_anal, *comp_output, *comp_outputalt, *apod_anal, *apod_output, *apod_outputalt;
int lax, channels, bps, sample_rate;/*flac*/
uint32_t minf, maxf;
uint8_t hash[16], input_md5[16], zero[16];
uint64_t input_tot_samples;//total samples if available, probably from input flac header
int blocksize_min, blocksize_max, blocksize_limit_lower, blocksize_limit_upper;
FLAC__bool (*encode_func) (FLAC__StaticEncoder*, const void*, uint32_t, uint64_t, void*, size_t*);
int ui_type, seektable, preserve_flac_metadata;
} flac_settings;
typedef struct{
uint64_t *effort_anal, *effort_output, *effort_tweak, *effort_merge;
double cpu_time;
size_t work_count;
} stats;
/*wrap a static encoder with its output*/
typedef struct{
FLAC__StaticEncoder *enc;
uint8_t *outbuf;
size_t outbuf_size, sample_cnt;
uint64_t curr_sample;
} simple_enc;
/*output queue*/
typedef struct{
simple_enc **sq;
size_t depth;
int *outstate;
size_t *saved, *cnt;
} queue;
typedef struct{
uint64_t sample_num, offset;
uint16_t frame_sample_cnt;
} seekpoint_t;
typedef struct{
size_t seektable_loc, firstframe_loc;
int write_cnt;
seekpoint_t *set;
size_t cnt, alloc;
} seektable_t;
typedef struct{
int usecache;
FILE *fout;
uint8_t *cache;
size_t cache_size, cache_alloc;
size_t outloc;//current size of output
size_t sampleloc;//current samples written
seektable_t seektable;
int blocktype_containing_islast_flag;
} output;
int out_open(output *out, const char *pathname, int seek);
size_t out_write(output *out, const void *ptr, size_t size);
void out_close(output *out);
typedef struct input input;
typedef struct input{
void *buf;
uint64_t loc_analysis;//global loc of where analysis is processing
uint64_t loc_output;//global loc of what has been fully processed
uint64_t loc_buffer;//global loc in stream that the start of input array points to
uint64_t sample_cnt;//local number of samples in input array available to analysis
FLAC__StreamDecoder *dec;//flac
drwav wav;//wav
FILE *cdda;
flac_settings *set;//flac/wav fills vitals in
output *out;//when preserving flac input metadata it's done in the metadata callback
MD5_CTX ctx;//hash as input read
size_t (*input_read) (input*, size_t);//function to read more input
void (*input_close) (input*);//function to close input
} input;
void _(char *s);
void _if(int goodbye, char *s);
FLAC__StaticEncoder *init_static_encoder(flac_settings *set, int blocksize, char *comp, char *apod);
void print_settings(flac_settings *set);
void print_stats(stats *stat, input *in, size_t outsize);
/*allocate the queue*/
void queue_alloc(queue *q, flac_settings *set);
/*flush the queue then deallocate*/
void queue_dealloc(queue *q, flac_settings *set, input *in, stats *stat, output *out);
/*encode an analysis frame with a simple encoder instance
Also MD5 input if context present, it is up to the analysis algorithm if and when to hash*/
void simple_enc_analyse(simple_enc *senc, flac_settings *set, input *in, uint32_t samples, uint64_t curr_sample, stats *stat);
void simple_enc_dealloc(simple_enc *senc);
/*Encode and output the rest of the file as a single frame with output settings if there's not enough of the file left for analysis to chew on
Advance curr_sample if necessary*/
int simple_enc_eof(queue *q, simple_enc **senc, flac_settings *set, input *in, uint64_t threshold, stats *stat, output *out);
/* Assumes the context has already done an analysis encode with the same input
If analysis settings == output settings, add precomputed frame to output queue
Otherwise, redo frame encode using output settings and add to queue
Return a fresh context as the queue has taken the old one
Advance curr_sample value*/
simple_enc *simple_enc_out(queue *q, simple_enc *senc, flac_settings *set, input *in, stats *stat, output *out);
void mode_boilerplate_init(flac_settings *set, clock_t *cstart, queue *q, stats *stat);
void mode_boilerplate_finish(flac_settings *set, clock_t *cstart, queue *q, stats *stat, input *in, output *out);
#endif