1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Base declarations for working with software encoders
12 * Copyright (C) 2006 Michael Sevakis
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ****************************************************************************/
27 /** encoder config structures **/
29 /** aiff_enc.codec **/
30 struct aiff_enc_config
33 unsigned long sample_depth
;
38 #define MP3_BITR_CAP_8 (1 << 0)
39 #define MP3_BITR_CAP_16 (1 << 1)
40 #define MP3_BITR_CAP_24 (1 << 2)
41 #define MP3_BITR_CAP_32 (1 << 3)
42 #define MP3_BITR_CAP_40 (1 << 4)
43 #define MP3_BITR_CAP_48 (1 << 5)
44 #define MP3_BITR_CAP_56 (1 << 6)
45 #define MP3_BITR_CAP_64 (1 << 7)
46 #define MP3_BITR_CAP_80 (1 << 8)
47 #define MP3_BITR_CAP_96 (1 << 9)
48 #define MP3_BITR_CAP_112 (1 << 10)
49 #define MP3_BITR_CAP_128 (1 << 11)
50 #define MP3_BITR_CAP_144 (1 << 12)
51 #define MP3_BITR_CAP_160 (1 << 13)
52 #define MP3_BITR_CAP_192 (1 << 14)
53 #define MP3_BITR_CAP_224 (1 << 15)
54 #define MP3_BITR_CAP_256 (1 << 16)
55 #define MP3_BITR_CAP_320 (1 << 17)
56 #define MP3_ENC_NUM_BITR 18
59 #define MPEG1_SAMPR_CAPS (SAMPR_CAP_32 | SAMPR_CAP_48 | SAMPR_CAP_44)
60 #define MPEG1_BITR_CAPS (MP3_BITR_CAP_32 | MP3_BITR_CAP_40 | MP3_BITR_CAP_48 | \
61 MP3_BITR_CAP_56 | MP3_BITR_CAP_64 | MP3_BITR_CAP_80 | \
62 MP3_BITR_CAP_96 | MP3_BITR_CAP_112 | MP3_BITR_CAP_128 | \
63 MP3_BITR_CAP_160 | MP3_BITR_CAP_192 | MP3_BITR_CAP_224 | \
64 MP3_BITR_CAP_256 | MP3_BITR_CAP_320)
67 #define MPEG2_SAMPR_CAPS (SAMPR_CAP_22 | SAMPR_CAP_24 | SAMPR_CAP_16)
68 #define MPEG2_BITR_CAPS (MP3_BITR_CAP_8 | MP3_BITR_CAP_16 | MP3_BITR_CAP_24 | \
69 MP3_BITR_CAP_32 | MP3_BITR_CAP_40 | MP3_BITR_CAP_48 | \
70 MP3_BITR_CAP_56 | MP3_BITR_CAP_64 | MP3_BITR_CAP_80 | \
71 MP3_BITR_CAP_96 | MP3_BITR_CAP_112 | MP3_BITR_CAP_128 | \
72 MP3_BITR_CAP_144 | MP3_BITR_CAP_160)
76 #define MPEG2_5_SAMPR_CAPS (SAMPR_CAP_8 | SAMPR_CAP_12 | SAMPR_CAP_11)
77 #define MPEG2_5_BITR_CAPS MPEG2_BITR_CAPS
80 /* HAVE_MPEG* defines mainly apply to the bitrate menu */
81 #if (REC_SAMPR_CAPS & MPEG1_SAMPR_CAPS) || defined (HAVE_SPDIF_REC)
82 #define HAVE_MPEG1_SAMPR
85 #if (REC_SAMPR_CAPS & MPEG2_SAMPR_CAPS) || defined (HAVE_SPDIF_REC)
86 #define HAVE_MPEG2_SAMPR
90 #if (REC_SAMPR_CAPS & MPEG2_5_SAMPR_CAPS) || defined (HAVE_SPDIF_REC)
91 #define HAVE_MPEG2_5_SAMPR
95 #define MP3_ENC_SAMPR_CAPS (MPEG1_SAMPR_CAPS | MPEG2_SAMPR_CAPS)
97 /* This number is count of full encoder set */
98 #define MP3_ENC_NUM_SAMPR 6
100 extern const unsigned long mp3_enc_sampr
[MP3_ENC_NUM_SAMPR
];
101 extern const unsigned long mp3_enc_bitr
[MP3_ENC_NUM_BITR
];
103 struct mp3_enc_config
105 unsigned long bitrate
;
108 #define MP3_ENC_BITRATE_CFG_DEFAULT 11 /* 128 */
109 #define MP3_ENC_BITRATE_CFG_VALUE_LIST "8,16,24,32,40,48,56,64,80,96," \
110 "112,128,144,160,192,224,256,320"
112 /** wav_enc.codec **/
113 #define WAV_ENC_SAMPR_CAPS SAMPR_CAP_ALL
115 struct wav_enc_config
118 unsigned long sample_depth
;
122 /** wavpack_enc.codec **/
123 #define WAVPACK_ENC_SAMPR_CAPS SAMPR_CAP_ALL
125 struct wavpack_enc_config
128 unsigned long sample_depth
;
132 struct encoder_config
136 /* states which *_enc_config member is valid */
137 int rec_format
; /* REC_FORMAT_* value */
138 int afmt
; /* AFMT_* value */
143 struct mp3_enc_config mp3_enc
;
144 struct wavpack_enc_config wavpack_enc
;
145 struct wav_enc_config wav_enc
;
149 /** Encoder chunk macros and definitions **/
150 #define CHUNKF_START_FILE 0x0001ul /* This chunk starts a new file */
151 #define CHUNKF_END_FILE 0x0002ul /* This chunk ends the current file */
152 #define CHUNKF_PRERECORD 0x0010ul /* This chunk is prerecord data,
153 a new file could start anytime */
154 #define CHUNKF_ABORT 0x0020ul /* Encoder should not finish this
156 #define CHUNKF_ERROR (~0ul ^ (~0ul >> 1)) /* An error has occured
157 (passed to/from encoder). Use the
158 sign bit to check (long)flags < 0. */
159 #define CHUNKF_ALLFLAGS (0x0033ul | CHUNKF_ERROR)
161 /* Header at the beginning of every encoder chunk */
163 #define ENC_CHUNK_MAGIC H_TO_BE32(('P' << 24) | ('T' << 16) | ('Y' << 8) | 'R')
168 unsigned long id
; /* overflow detection - 'PTYR' - acronym for
169 "PTYR Tells You Right" ;) */
171 unsigned long flags
; /* in/out: flags used by encoder and file
173 size_t enc_size
; /* out: amount of encoder data written to
175 unsigned long num_pcm
; /* out: number of PCM samples eaten during
177 (<= size of allocated buffer) */
178 unsigned char *enc_data
; /* out: pointer to enc_size_written bytes
179 of encoded audio data in chunk */
180 /* Encoder defined data follows header. Can be audio data + any other
181 stuff the encoder needs to handle on a per chunk basis */
184 /* Paranoia: be sure header size is 4-byte aligned */
185 #define ENC_CHUNK_HDR_SIZE \
186 ALIGN_UP_P2(sizeof (struct enc_chunk_hdr), 2)
187 /* Skip the chunk header and return data */
188 #define ENC_CHUNK_SKIP_HDR(t, hdr) \
189 ((typeof (t))((char *)hdr + ENC_CHUNK_HDR_SIZE))
190 /* Cast p to struct enc_chunk_hdr * */
191 #define ENC_CHUNK_HDR(p) \
192 ((struct enc_chunk_hdr *)(p))
196 /* File writing events - data points to enc_file_event_data */
197 ENC_START_FILE
= 0, /* a new file has been opened and no data has yet
199 ENC_WRITE_CHUNK
, /* write the current chunk to disk */
200 ENC_END_FILE
, /* current file about to be closed and all valid
201 data has been written */
202 /* Encoder buffer events - data points to enc_buffer_event_data */
203 ENC_REC_NEW_STREAM
, /* Take steps to finish current stream and start
208 * encoder can write extra data to the file such as headers or more encoded
209 * samples and must update sizes and samples accordingly.
211 struct enc_file_event_data
213 struct enc_chunk_hdr
*chunk
; /* Current chunk */
214 size_t new_enc_size
; /* New size of chunk */
215 unsigned long new_num_pcm
; /* New number of pcm in chunk */
216 const char *filename
; /* filename to open if ENC_START_FILE */
217 int rec_file
; /* Current file or < 0 if none */
218 unsigned long num_pcm_samples
; /* Current pcm sample count written to
223 * encoder may add some data to the end of the last and start of the next
224 * but must never yield when called so any encoding done should be absolutely
227 struct enc_buffer_event_data
229 unsigned long flags
; /* in: One or more of:
234 struct enc_chunk_hdr
*pre_chunk
; /* in: pointer to first prerecord
237 struct enc_chunk_hdr
*chunk
; /* in,out: chunk were split occurs -
238 * first chunk of start
242 /** Callbacks called by encoder codec **/
244 /* parameters passed to encoder by enc_get_inputs */
247 unsigned long sample_rate
; /* out - pcm frequency */
248 int num_channels
; /* out - number of audio channels */
249 int rec_mono_mode
; /* out - how to create mono */
250 struct encoder_config
*config
; /* out - encoder settings */
253 void enc_get_inputs(struct enc_inputs
*inputs
);
255 /* parameters pass from encoder to enc_set_parameters */
256 struct enc_parameters
259 int afmt
; /* AFMT_* id - sanity checker */
260 size_t chunk_size
; /* max chunk size required */
261 unsigned long enc_sample_rate
; /* actual sample rate used by encoder
262 (for recorded time calculation) */
263 size_t reserve_bytes
; /* number of bytes to reserve immediately
265 void (*events_callback
)(enum enc_events event
,
266 void *data
); /* pointer to events callback */
268 unsigned char *enc_buffer
; /* pointer to enc_buffer */
269 size_t buf_chunk_size
; /* size of chunks in enc_buffer */
270 int num_chunks
; /* number of chunks allotted to encoder */
271 unsigned char *reserve_buffer
; /* pointer to reserve_bytes bytes */
274 /* set the encoder dimensions - called by encoder codec at initialization
276 void enc_set_parameters(struct enc_parameters
*params
);
277 /* returns pointer to next write chunk in circular buffer */
278 struct enc_chunk_hdr
* enc_get_chunk(void);
279 /* releases the current chunk into the available chunks */
280 void enc_finish_chunk(void);
282 #define PCM_MAX_FEED_SIZE 20000 /* max pcm size passed to encoder */
284 /* passes a pointer to next chunk of unprocessed wav data */
285 unsigned char * enc_get_pcm_data(size_t size
);
286 /* puts some pcm data back in the queue */
287 size_t enc_unget_pcm_data(size_t size
);
289 #endif /* ENC_BASE_H */