]> git.sesse.net Git - vlc/blob - modules/codec/shine/enc_base.h
Qt: sprefs, cleaning of input/codecs part
[vlc] / modules / codec / shine / enc_base.h
1 /***************************************************************************
2  *             __________               __   ___.
3  *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
4  *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
5  *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
6  *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
7  *                     \/            \/     \/    \/            \/
8  * $Id$
9  *
10  * Base declarations for working with software encoders
11  *
12  * Copyright (C) 2006 Michael Sevakis
13  *
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.
18  *
19  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20  * KIND, either express or implied.
21  *
22  ****************************************************************************/
23
24 #ifndef ENC_BASE_H
25 #define ENC_BASE_H
26
27 /* firmware/export/system.h */
28 /* return p incremented by specified number of bytes */
29 #define SKIPBYTES(p, count) ((typeof (p))((char *)(p) + (count)))
30
31 #define P2_M1(p2)  ((1 << (p2))-1)
32
33 /* align up or down to nearest 2^p2 */
34 #define ALIGN_DOWN_P2(n, p2) ((n) & ~P2_M1(p2))
35 #define ALIGN_UP_P2(n, p2)   ALIGN_DOWN_P2((n) + P2_M1(p2),p2)
36 /* end of firmware/export/system.h */
37
38 /** encoder config structures **/
39
40 /** aiff_enc.codec **/
41 struct aiff_enc_config
42 {
43 #if 0
44     unsigned long sample_depth;
45 #endif
46 };
47
48 /** mp3_enc.codec **/
49 #define MP3_BITR_CAP_8      (1 << 0)
50 #define MP3_BITR_CAP_16     (1 << 1)
51 #define MP3_BITR_CAP_24     (1 << 2)
52 #define MP3_BITR_CAP_32     (1 << 3)
53 #define MP3_BITR_CAP_40     (1 << 4)
54 #define MP3_BITR_CAP_48     (1 << 5)
55 #define MP3_BITR_CAP_56     (1 << 6)
56 #define MP3_BITR_CAP_64     (1 << 7)
57 #define MP3_BITR_CAP_80     (1 << 8)
58 #define MP3_BITR_CAP_96     (1 << 9)
59 #define MP3_BITR_CAP_112    (1 << 10)
60 #define MP3_BITR_CAP_128    (1 << 11)
61 #define MP3_BITR_CAP_144    (1 << 12)
62 #define MP3_BITR_CAP_160    (1 << 13)
63 #define MP3_BITR_CAP_192    (1 << 14)
64 #define MP3_BITR_CAP_224    (1 << 15)
65 #define MP3_BITR_CAP_256    (1 << 16)
66 #define MP3_BITR_CAP_320    (1 << 17)
67 #define MP3_ENC_NUM_BITR    18
68
69 /* MPEG 1 */
70 #define MPEG1_SAMPR_CAPS    (SAMPR_CAP_32 | SAMPR_CAP_48 | SAMPR_CAP_44)
71 #define MPEG1_BITR_CAPS     (MP3_BITR_CAP_32  | MP3_BITR_CAP_40  | MP3_BITR_CAP_48  | \
72                              MP3_BITR_CAP_56  | MP3_BITR_CAP_64  | MP3_BITR_CAP_80  | \
73                              MP3_BITR_CAP_96  | MP3_BITR_CAP_112 | MP3_BITR_CAP_128 | \
74                              MP3_BITR_CAP_160 | MP3_BITR_CAP_192 | MP3_BITR_CAP_224 | \
75                              MP3_BITR_CAP_256 | MP3_BITR_CAP_320)
76
77 /* MPEG 2 */
78 #define MPEG2_SAMPR_CAPS    (SAMPR_CAP_22 | SAMPR_CAP_24 | SAMPR_CAP_16)
79 #define MPEG2_BITR_CAPS     (MP3_BITR_CAP_8   | MP3_BITR_CAP_16  | MP3_BITR_CAP_24  | \
80                              MP3_BITR_CAP_32  | MP3_BITR_CAP_40  | MP3_BITR_CAP_48  | \
81                              MP3_BITR_CAP_56  | MP3_BITR_CAP_64  | MP3_BITR_CAP_80  | \
82                              MP3_BITR_CAP_96  | MP3_BITR_CAP_112 | MP3_BITR_CAP_128 | \
83                              MP3_BITR_CAP_144 | MP3_BITR_CAP_160)
84
85 #if 0
86 /* MPEG 2.5 */
87 #define MPEG2_5_SAMPR_CAPS  (SAMPR_CAP_8  | SAMPR_CAP_12 | SAMPR_CAP_11)
88 #define MPEG2_5_BITR_CAPS   MPEG2_BITR_CAPS
89 #endif
90
91 #if 0
92 /* HAVE_MPEG* defines mainly apply to the bitrate menu */
93 #if (REC_SAMPR_CAPS & MPEG1_SAMPR_CAPS) || defined (HAVE_SPDIF_REC)
94 #define HAVE_MPEG1_SAMPR
95 #endif
96
97 #if (REC_SAMPR_CAPS & MPEG2_SAMPR_CAPS) || defined (HAVE_SPDIF_REC)
98 #define HAVE_MPEG2_SAMPR
99 #endif
100 #endif
101
102 #if 0
103 #if (REC_SAMPR_CAPS & MPEG2_5_SAMPR_CAPS) || defined (HAVE_SPDIF_REC)
104 #define HAVE_MPEG2_5_SAMPR
105 #endif
106 #endif /* 0 */
107
108 #define MP3_ENC_SAMPR_CAPS      (MPEG1_SAMPR_CAPS | MPEG2_SAMPR_CAPS)
109
110 /* This number is count of full encoder set */
111 #define MP3_ENC_NUM_SAMPR       6
112
113 extern const unsigned long mp3_enc_sampr[MP3_ENC_NUM_SAMPR];
114 extern const unsigned long mp3_enc_bitr[MP3_ENC_NUM_BITR];
115
116 struct mp3_enc_config
117 {
118     unsigned long bitrate;
119 };
120
121 #define MP3_ENC_BITRATE_CFG_DEFAULT     11 /* 128 */
122 #define MP3_ENC_BITRATE_CFG_VALUE_LIST  "8,16,24,32,40,48,56,64,80,96," \
123                                         "112,128,144,160,192,224,256,320"
124
125 /** wav_enc.codec **/
126 #define WAV_ENC_SAMPR_CAPS      SAMPR_CAP_ALL
127
128 struct wav_enc_config
129 {
130 #if 0
131     unsigned long sample_depth;
132 #endif
133 };
134
135 /** wavpack_enc.codec **/
136 #define WAVPACK_ENC_SAMPR_CAPS  SAMPR_CAP_ALL
137
138 struct wavpack_enc_config
139 {
140 #if 0
141     unsigned long sample_depth;
142 #endif
143 };
144
145 struct encoder_config
146 {
147     union
148     {
149         /* states which *_enc_config member is valid */
150         int rec_format; /* REC_FORMAT_* value */
151         int afmt;       /* AFMT_* value       */
152     };
153
154     union
155     {
156         struct mp3_enc_config     mp3_enc;
157         struct wavpack_enc_config wavpack_enc;
158         struct wav_enc_config     wav_enc;
159     };
160 };
161
162 /** Encoder chunk macros and definitions **/
163 #define CHUNKF_START_FILE 0x0001ul /* This chunk starts a new file         */
164 #define CHUNKF_END_FILE   0x0002ul /* This chunk ends the current file     */
165 #define CHUNKF_PRERECORD  0x0010ul /* This chunk is prerecord data,
166                                       a new file could start anytime       */
167 #define CHUNKF_ABORT      0x0020ul /* Encoder should not finish this
168                                       chunk                                */
169 #define CHUNKF_ERROR    (~0ul ^ (~0ul >> 1)) /* An error has occured
170                                       (passed to/from encoder). Use the
171                                       sign bit to check (long)flags < 0.   */
172 #define CHUNKF_ALLFLAGS (0x0033ul | CHUNKF_ERROR)
173
174 /* Header at the beginning of every encoder chunk */
175 #ifdef DEBUG
176 #define ENC_CHUNK_MAGIC H_TO_BE32(('P' << 24) | ('T' << 16) | ('Y' << 8) | 'R')
177 #endif
178 struct enc_chunk_hdr
179 {
180 #ifdef DEBUG
181     unsigned long id;         /* overflow detection - 'PTYR' - acronym for
182                                  "PTYR Tells You Right" ;)                 */
183 #endif
184     unsigned long flags;      /* in/out: flags used by encoder and file
185                                          writing                           */
186     size_t        enc_size;   /* out:    amount of encoder data written to
187                                          chunk                             */
188     unsigned long num_pcm;    /* out:    number of PCM samples eaten during
189                                          processing
190                                          (<= size of allocated buffer)     */
191     unsigned char *enc_data;  /* out:    pointer to enc_size_written bytes
192                                          of encoded audio data in chunk    */
193     /* Encoder defined data follows header. Can be audio data + any other
194        stuff the encoder needs to handle on a per chunk basis */
195 };
196
197 /* Paranoia: be sure header size is 4-byte aligned */
198 #define ENC_CHUNK_HDR_SIZE \
199             ALIGN_UP_P2(sizeof (struct enc_chunk_hdr), 2)
200 /* Skip the chunk header and return data */
201 #define ENC_CHUNK_SKIP_HDR(t, hdr) \
202             ((typeof (t))((char *)hdr + ENC_CHUNK_HDR_SIZE))
203 /* Cast p to struct enc_chunk_hdr * */
204 #define ENC_CHUNK_HDR(p) \
205             ((struct enc_chunk_hdr *)(p))
206
207 enum enc_events
208 {
209     /* File writing events - data points to enc_file_event_data            */
210     ENC_START_FILE = 0,  /* a new file has been opened and no data has yet
211                             been written                                   */
212     ENC_WRITE_CHUNK,     /* write the current chunk to disk                */
213     ENC_END_FILE,        /* current file about to be closed and all valid
214                             data has been written                          */
215     /* Encoder buffer events - data points to enc_buffer_event_data        */
216     ENC_REC_NEW_STREAM,  /* Take steps to finish current stream and start
217                             new                                            */
218 };
219
220 /**
221  * encoder can write extra data to the file such as headers or more encoded
222  * samples and must update sizes and samples accordingly.
223  */
224 struct enc_file_event_data
225 {
226     struct enc_chunk_hdr *chunk;   /* Current chunk                        */
227     size_t        new_enc_size;    /* New size of chunk                    */
228     unsigned long new_num_pcm;     /* New number of pcm in chunk           */
229     const char   *filename;        /* filename to open if ENC_START_FILE   */
230     int           rec_file;        /* Current file or < 0 if none          */
231     unsigned long num_pcm_samples; /* Current pcm sample count written to
232                                       file so far.                         */
233 };
234
235 /**
236  * encoder may add some data to the end of the last and start of the next
237  * but must never yield when called so any encoding done should be absolutely
238  * minimal.
239  */
240 struct enc_buffer_event_data
241 {
242     unsigned long         flags;       /* in: One or more of:
243                                         *     CHUNKF_PRERECORD
244                                         *     CHUNKF_END_FILE
245                                         *     CHUNKF_START_FILE
246                                         */
247     struct enc_chunk_hdr *pre_chunk;   /* in: pointer to first prerecord
248                                         *     chunk
249                                         */
250     struct enc_chunk_hdr *chunk;       /* in,out: chunk were split occurs -
251                                         *         first chunk of start
252                                         */
253 };
254
255 /** Callbacks called by encoder codec **/
256
257 /* parameters passed to encoder by enc_get_inputs */
258 struct enc_inputs
259 {
260     unsigned long sample_rate;     /* out - pcm frequency                  */
261     int           num_channels;    /* out - number of audio channels       */
262     struct encoder_config *config; /* out - encoder settings               */
263 };
264
265 void enc_get_inputs(struct enc_inputs *inputs);
266
267 /* parameters pass from encoder to enc_set_parameters */
268 struct enc_parameters
269 {
270     /* IN parameters */
271     int           afmt;            /* AFMT_* id - sanity checker           */
272     size_t        chunk_size;      /* max chunk size required              */
273     unsigned long enc_sample_rate; /* actual sample rate used by encoder
274                                       (for recorded time calculation)      */
275     size_t        reserve_bytes;   /* number of bytes to reserve immediately
276                                       following chunks                     */
277     void (*events_callback)(enum enc_events event,
278                             void *data); /*  pointer to events callback    */
279     /* OUT parameters */
280     unsigned char *enc_buffer;     /* pointer to enc_buffer                */
281     size_t         buf_chunk_size; /* size of chunks in enc_buffer         */
282     int            num_chunks;     /* number of chunks allotted to encoder */
283     unsigned char *reserve_buffer; /* pointer to reserve_bytes bytes       */
284 };
285
286 /* set the encoder dimensions - called by encoder codec at initialization
287    and termination */
288 void   enc_set_parameters(struct enc_parameters *params);
289 /* returns pointer to next write chunk in circular buffer */
290 struct enc_chunk_hdr * enc_get_chunk(void);
291 /* releases the current chunk into the available chunks */
292 void   enc_finish_chunk(void);
293
294 #define PCM_MAX_FEED_SIZE       20000 /* max pcm size passed to encoder    */
295
296 /* passes a pointer to next chunk of unprocessed wav data */
297 unsigned char * enc_get_pcm_data(size_t size);
298 /* puts some pcm data back in the queue */
299 size_t          enc_unget_pcm_data(size_t size);
300
301 #endif /* ENC_BASE_H */