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
101 extern const unsigned long mp3_enc_sampr
[MP3_ENC_NUM_SAMPR
];
103 extern const unsigned long mp3_enc_bitr
[MP3_ENC_NUM_BITR
];
105 struct mp3_enc_config
107 unsigned long bitrate
;
110 #define MP3_ENC_BITRATE_CFG_DEFAULT 11 /* 128 */
111 #define MP3_ENC_BITRATE_CFG_VALUE_LIST "8,16,24,32,40,48,56,64,80,96," \
112 "112,128,144,160,192,224,256,320"
114 /** wav_enc.codec **/
115 #define WAV_ENC_SAMPR_CAPS SAMPR_CAP_ALL
117 struct wav_enc_config
120 unsigned long sample_depth
;
124 /** wavpack_enc.codec **/
125 #define WAVPACK_ENC_SAMPR_CAPS SAMPR_CAP_ALL
127 struct wavpack_enc_config
130 unsigned long sample_depth
;
134 struct encoder_config
138 /* states which *_enc_config member is valid */
139 int rec_format
; /* REC_FORMAT_* value */
140 int afmt
; /* AFMT_* value */
145 struct mp3_enc_config mp3_enc
;
146 struct wavpack_enc_config wavpack_enc
;
147 struct wav_enc_config wav_enc
;
151 /** Encoder chunk macros and definitions **/
152 #define CHUNKF_START_FILE 0x0001ul /* This chunk starts a new file */
153 #define CHUNKF_END_FILE 0x0002ul /* This chunk ends the current file */
154 #define CHUNKF_PRERECORD 0x0010ul /* This chunk is prerecord data,
155 a new file could start anytime */
156 #define CHUNKF_ABORT 0x0020ul /* Encoder should not finish this
158 #define CHUNKF_ERROR (~0ul ^ (~0ul >> 1)) /* An error has occured
159 (passed to/from encoder). Use the
160 sign bit to check (long)flags < 0. */
161 #define CHUNKF_ALLFLAGS (0x0033ul | CHUNKF_ERROR)
163 /* Header at the beginning of every encoder chunk */
165 #define H_TO_BE32 htobe32
166 #define ENC_CHUNK_MAGIC H_TO_BE32(('P' << 24) | ('T' << 16) | ('Y' << 8) | 'R')
171 unsigned long id
; /* overflow detection - 'PTYR' - acronym for
172 "PTYR Tells You Right" ;) */
174 unsigned long flags
; /* in/out: flags used by encoder and file
176 size_t enc_size
; /* out: amount of encoder data written to
178 unsigned long num_pcm
; /* out: number of PCM samples eaten during
180 (<= size of allocated buffer) */
181 unsigned char *enc_data
; /* out: pointer to enc_size_written bytes
182 of encoded audio data in chunk */
183 /* Encoder defined data follows header. Can be audio data + any other
184 stuff the encoder needs to handle on a per chunk basis */
187 /* Paranoia: be sure header size is 4-byte aligned */
188 #define ENC_CHUNK_HDR_SIZE \
189 ALIGN_UP_P2(sizeof (struct enc_chunk_hdr), 2)
190 /* Skip the chunk header and return data */
191 #define ENC_CHUNK_SKIP_HDR(t, hdr) \
192 ((typeof (t))((char *)hdr + ENC_CHUNK_HDR_SIZE))
193 /* Cast p to struct enc_chunk_hdr * */
194 #define ENC_CHUNK_HDR(p) \
195 ((struct enc_chunk_hdr *)(p))
199 /* File writing events - data points to enc_file_event_data */
200 ENC_START_FILE
= 0, /* a new file has been opened and no data has yet
202 ENC_WRITE_CHUNK
, /* write the current chunk to disk */
203 ENC_END_FILE
, /* current file about to be closed and all valid
204 data has been written */
205 /* Encoder buffer events - data points to enc_buffer_event_data */
206 ENC_REC_NEW_STREAM
, /* Take steps to finish current stream and start
211 * encoder can write extra data to the file such as headers or more encoded
212 * samples and must update sizes and samples accordingly.
214 struct enc_file_event_data
216 struct enc_chunk_hdr
*chunk
; /* Current chunk */
217 size_t new_enc_size
; /* New size of chunk */
218 unsigned long new_num_pcm
; /* New number of pcm in chunk */
219 const char *filename
; /* filename to open if ENC_START_FILE */
220 int rec_file
; /* Current file or < 0 if none */
221 unsigned long num_pcm_samples
; /* Current pcm sample count written to
226 * encoder may add some data to the end of the last and start of the next
227 * but must never yield when called so any encoding done should be absolutely
230 struct enc_buffer_event_data
232 unsigned long flags
; /* in: One or more of:
237 struct enc_chunk_hdr
*pre_chunk
; /* in: pointer to first prerecord
240 struct enc_chunk_hdr
*chunk
; /* in,out: chunk were split occurs -
241 * first chunk of start
245 /** Callbacks called by encoder codec **/
247 /* parameters passed to encoder by enc_get_inputs */
250 unsigned long sample_rate
; /* out - pcm frequency */
251 int num_channels
; /* out - number of audio channels */
252 int rec_mono_mode
; /* out - how to create mono */
253 struct encoder_config
*config
; /* out - encoder settings */
256 void enc_get_inputs(struct enc_inputs
*inputs
);
258 /* parameters pass from encoder to enc_set_parameters */
259 struct enc_parameters
262 int afmt
; /* AFMT_* id - sanity checker */
263 size_t chunk_size
; /* max chunk size required */
264 unsigned long enc_sample_rate
; /* actual sample rate used by encoder
265 (for recorded time calculation) */
266 size_t reserve_bytes
; /* number of bytes to reserve immediately
268 void (*events_callback
)(enum enc_events event
,
269 void *data
); /* pointer to events callback */
271 unsigned char *enc_buffer
; /* pointer to enc_buffer */
272 size_t buf_chunk_size
; /* size of chunks in enc_buffer */
273 int num_chunks
; /* number of chunks allotted to encoder */
274 unsigned char *reserve_buffer
; /* pointer to reserve_bytes bytes */
277 /* set the encoder dimensions - called by encoder codec at initialization
279 void enc_set_parameters(struct enc_parameters
*params
);
280 /* returns pointer to next write chunk in circular buffer */
281 struct enc_chunk_hdr
* enc_get_chunk(void);
282 /* releases the current chunk into the available chunks */
283 void enc_finish_chunk(void);
285 #define PCM_MAX_FEED_SIZE 20000 /* max pcm size passed to encoder */
287 /* passes a pointer to next chunk of unprocessed wav data */
288 unsigned char * enc_get_pcm_data(size_t size
);
289 /* puts some pcm data back in the queue */
290 size_t enc_unget_pcm_data(size_t size
);
292 #endif /* ENC_BASE_H */