new more flexible way of passing metadata to stream encoder
[platform/upstream/flac.git] / include / FLAC / stream_encoder.h
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2000,2001,2002  Josh Coalson
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA  02111-1307, USA.
18  */
19
20 #ifndef FLAC__STREAM_ENCODER_H
21 #define FLAC__STREAM_ENCODER_H
22
23 #include "format.h"
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28
29
30 typedef enum {
31         FLAC__STREAM_ENCODER_OK = 0,
32         FLAC__STREAM_ENCODER_INVALID_CALLBACK,
33         FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS,
34         FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE,
35         FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE,
36         FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE,
37         FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION,
38         FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH,
39         FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH,
40         FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE,
41         FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER,
42         FLAC__STREAM_ENCODER_NOT_STREAMABLE,
43         FLAC__STREAM_ENCODER_FRAMING_ERROR,
44         FLAC__STREAM_ENCODER_INVALID_METADATA,
45         FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING,
46         FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING, /* that is, the write_callback returned an error */
47         FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR,
48         FLAC__STREAM_ENCODER_ALREADY_INITIALIZED,
49         FLAC__STREAM_ENCODER_UNINITIALIZED
50 } FLAC__StreamEncoderState;
51 extern const char *FLAC__StreamEncoderStateString[];
52
53 typedef enum {
54         FLAC__STREAM_ENCODER_WRITE_OK = 0,
55         FLAC__STREAM_ENCODER_WRITE_FATAL_ERROR
56 } FLAC__StreamEncoderWriteStatus;
57 extern const char *FLAC__StreamEncoderWriteStatusString[];
58
59 /***********************************************************************
60  *
61  * class FLAC__StreamEncoder
62  *
63  ***********************************************************************/
64
65 struct FLAC__StreamEncoderProtected;
66 struct FLAC__StreamEncoderPrivate;
67 typedef struct {
68         struct FLAC__StreamEncoderProtected *protected_; /* avoid the C++ keyword 'protected' */
69         struct FLAC__StreamEncoderPrivate *private_; /* avoid the C++ keyword 'private' */
70 } FLAC__StreamEncoder;
71
72 /***********************************************************************
73  *
74  * Class constructor/destructor
75  *
76  ***********************************************************************/
77
78 /*
79  * Any parameters that are not set before FLAC__stream_encoder_init()
80  * will take on the defaults from the constructor, shown below.
81  * For more on what the parameters mean, see the documentation.
82  *
83  * FLAC__bool   streamable_subset             (DEFAULT: true  ) true to limit encoder to generating a Subset stream, else false
84  * FLAC__bool   do_mid_side_stereo            (DEFAULT: false ) if true then channels must be 2
85  * FLAC__bool   loose_mid_side_stereo         (DEFAULT: false ) if true then do_mid_side_stereo must be true
86  * unsigned     channels                      (DEFAULT: 2     ) must be <= FLAC__MAX_CHANNELS
87  * unsigned     bits_per_sample               (DEFAULT: 16    ) do not give the encoder wider data than what you specify here or bad things will happen!
88  * unsigned     sample_rate                   (DEFAULT: 44100 )
89  * unsigned     blocksize                     (DEFAULT: 1152  )
90  * unsigned     max_lpc_order                 (DEFAULT: 0     ) 0 => encoder will not try general LPC, only fixed predictors; must be <= FLAC__MAX_LPC_ORDER
91  * unsigned     qlp_coeff_precision           (DEFAULT: 0     ) >= FLAC__MIN_QLP_COEFF_PRECISION, or 0 to let encoder select based on blocksize;
92  *                                                              qlp_coeff_precision+bits_per_sample must be < 32
93  * FLAC__bool   do_qlp_coeff_prec_search      (DEFAULT: false ) false => use qlp_coeff_precision, true => search around qlp_coeff_precision, take best
94  * FLAC__bool   do_escape_coding              (DEFAULT: false ) true => search for escape codes in the entropy coding stage for slightly better compression
95  * FLAC__bool   do_exhaustive_model_search    (DEFAULT: false ) false => use estimated bits per residual for scoring, true => generate all, take shortest
96  * unsigned     min_residual_partition_order  (DEFAULT: 0     ) 0 => estimate Rice parameter based on residual variance; >0 => partition residual, use parameter
97  * unsigned     max_residual_partition_order  (DEFAULT: 0     )      for each based on mean; min_ and max_ specify the min and max Rice partition order
98  * unsigned     rice_parameter_search_dist    (DEFAULT: 0     ) 0 => try only calc'd parameter k; else try all [k-dist..k+dist] parameters, use best
99  * FLAC__uint64 total_samples_estimate        (DEFAULT: 0     ) may be 0 if unknown.  acts as a placeholder in the STREAMINFO until the actual total is calculated
100  * FLAC__StreamMetaData **metadata            (DEFAULT: NULL,0) optional metadata blocks to prepend.  STREAMINFO is not allowed since it is done internally.
101  * + unsigned num_blocks
102  *            (*write_callback)()             (DEFAULT: NULL  ) The callbacks are the only values that MUST be set before FLAC__stream_encoder_init()
103  *            (*metadata_callback)()          (DEFAULT: NULL  )
104  * void*        client_data                   (DEFAULT: NULL  ) passed back through the callbacks
105  */
106 FLAC__StreamEncoder *FLAC__stream_encoder_new();
107 void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder);
108
109 /***********************************************************************
110  *
111  * Public class method prototypes
112  *
113  ***********************************************************************/
114
115 /*
116  * Various "set" methods.  These may only be called when the encoder
117  * is in the state FLAC__STREAM_ENCODER_UNINITIALIZED, i.e. after
118  * FLAC__stream_encoder_new() or FLAC__stream_encoder_finish(), but
119  * before FLAC__stream_encoder_init().  If this is the case they will
120  * return true, otherwise false.
121  *
122  * NOTE that these functions do not validate the values as many are
123  * interdependent.  The FLAC__stream_encoder_init() function will do
124  * this, so make sure to pay attention to the state returned by
125  * FLAC__stream_encoder_init().
126  *
127  * Any parameters that are not set before FLAC__stream_encoder_init()
128  * will take on the defaults from the constructor.  NOTE that
129  * FLAC__stream_encoder_finish() does NOT reset the values to the
130  * constructor defaults.
131 @@@@ update so that only _set_ methods that need to return FLAC__bool, else void
132  */
133 FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value);
134 FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value);
135 FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value);
136 FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value);
137 FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value);
138 FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value);
139 FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value);
140 FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value);
141 FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value);
142 FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value);
143 FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value);
144 FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value);
145 FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value);
146 FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value);
147 FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value);
148 FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value);
149 FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetaData **metadata, unsigned num_blocks);
150 FLAC__bool FLAC__stream_encoder_set_write_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteStatus (*value)(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data));
151 FLAC__bool FLAC__stream_encoder_set_metadata_callback(FLAC__StreamEncoder *encoder, void (*value)(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data));
152 FLAC__bool FLAC__stream_encoder_set_client_data(FLAC__StreamEncoder *encoder, void *value);
153
154 /*
155  * Various "get" methods
156  */
157 FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder);
158 FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder);
159 FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder);
160 FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder);
161 unsigned   FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder);
162 unsigned   FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder);
163 unsigned   FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder);
164 unsigned   FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder);
165 unsigned   FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder);
166 unsigned   FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder);
167 FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder);
168 FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder);
169 FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder);
170 unsigned   FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder);
171 unsigned   FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder);
172 unsigned   FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder);
173
174 /*
175  * Initialize the instance; should be called after construction and
176  * 'set' calls but before any of the 'process' calls.  Will set and
177  * return the encoder state, which will be FLAC__STREAM_ENCODER_OK
178  * if initialization succeeded.
179  */
180 FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder);
181
182 /*
183  * Flush the encoding buffer, release resources, and return the encoder
184  * state to FLAC__STREAM_ENCODER_UNINITIALIZED.  Note that this can
185  * generate one or more write_callback()s before returning.
186  */
187 void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder);
188
189 /*
190  * Methods for encoding the data
191  */
192 FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 *buf[], unsigned samples);
193 FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buf[], unsigned samples);
194
195 #ifdef __cplusplus
196 }
197 #endif
198
199 #endif