libopusenc: opusenc.h Source File

| libopusenc 0.3 Stand-alone encoder library for .opus files.

Loading...
Searching...
No Matches
opusenc.h
1/* Copyright (c) 2017 Jean-Marc Valin */
2/*
3 Redistribution and use in source and binary forms, with or without
4 modification, are permitted provided that the following conditions
5 are met:
6
7 - Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9
10 - Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
18 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*/
26
27#ifndef OPUSENC_H
28# define OPUSENC_H
29
52
53# if defined(__cplusplus)
54extern "C" {
55# endif
56
57#include <stddef.h>
58#include <opus.h>
59
60#ifndef OPE_EXPORT
61# if defined(WIN32)
62# if defined(OPE_BUILD) && defined(DLL_EXPORT)
63# define OPE_EXPORT __declspec(dllexport)
64# else
65# define OPE_EXPORT
66# endif
67# elif defined(__GNUC__) && defined(OPE_BUILD)
68# define OPE_EXPORT __attribute__ ((visibility ("default")))
69# else
70# define OPE_EXPORT
71# endif
72#endif
73
83
84
85/* Bump this when we change the API. */
87#define OPE_API_VERSION 0
88
89#define OPE_OK 0
90/* Based on the relevant libopus code minus 10. */
91#define OPE_BAD_ARG -11
92#define OPE_INTERNAL_ERROR -13
93#define OPE_UNIMPLEMENTED -15
94#define OPE_ALLOC_FAIL -17
95
96/* Specific to libopusenc. */
97#define OPE_CANNOT_OPEN -30
98#define OPE_TOO_LATE -31
99#define OPE_INVALID_PICTURE -32
100#define OPE_INVALID_ICON -33
101#define OPE_WRITE_FAIL -34
102#define OPE_CLOSE_FAIL -35
103
106
107/* These are the "raw" request values -- they should usually not be used. */
108#define OPE_SET_DECISION_DELAY_REQUEST 14000
109#define OPE_GET_DECISION_DELAY_REQUEST 14001
110#define OPE_SET_MUXING_DELAY_REQUEST 14002
111#define OPE_GET_MUXING_DELAY_REQUEST 14003
112#define OPE_SET_COMMENT_PADDING_REQUEST 14004
113#define OPE_GET_COMMENT_PADDING_REQUEST 14005
114#define OPE_SET_SERIALNO_REQUEST 14006
115#define OPE_GET_SERIALNO_REQUEST 14007
116#define OPE_SET_PACKET_CALLBACK_REQUEST 14008
117/*#define OPE_GET_PACKET_CALLBACK_REQUEST 14009*/
118#define OPE_SET_HEADER_GAIN_REQUEST 14010
119#define OPE_GET_HEADER_GAIN_REQUEST 14011
120#define OPE_GET_NB_STREAMS_REQUEST 14013
121#define OPE_GET_NB_COUPLED_STREAMS_REQUEST 14015
122
123/* Macros to trigger compilation errors when the wrong types are provided to a CTL. */
124/* These macros are not part of the API and are only for use within the macros below. */
125#define ope_check_int(x) (((void)((x) == (opus_int32)0)), (opus_int32)(x))
126#define ope_check_int_ptr(ptr) ((ptr) + ((ptr) - (opus_int32*)(ptr)))
127#define ope_check_packet_func(x) ((void)((void (*)(void *, const unsigned char *, opus_int32, opus_uint32))0 == (x)), (x))
128#define ope_check_void_ptr(x) ((void)((void *)0 == (x)), (x))
129
132
137
138#define OPE_SET_DECISION_DELAY(x) OPE_SET_DECISION_DELAY_REQUEST, ope_check_int(x)
139#define OPE_GET_DECISION_DELAY(x) OPE_GET_DECISION_DELAY_REQUEST, ope_check_int_ptr(x)
140#define OPE_SET_MUXING_DELAY(x) OPE_SET_MUXING_DELAY_REQUEST, ope_check_int(x)
141#define OPE_GET_MUXING_DELAY(x) OPE_GET_MUXING_DELAY_REQUEST, ope_check_int_ptr(x)
142#define OPE_SET_COMMENT_PADDING(x) OPE_SET_COMMENT_PADDING_REQUEST, ope_check_int(x)
143#define OPE_GET_COMMENT_PADDING(x) OPE_GET_COMMENT_PADDING_REQUEST, ope_check_int_ptr(x)
144#define OPE_SET_SERIALNO(x) OPE_SET_SERIALNO_REQUEST, ope_check_int(x)
145#define OPE_GET_SERIALNO(x) OPE_GET_SERIALNO_REQUEST, ope_check_int_ptr(x)
146#define OPE_SET_PACKET_CALLBACK(x,u) OPE_SET_PACKET_CALLBACK_REQUEST, ope_check_packet_func(x), ope_check_void_ptr(u)
147/*#define OPE_GET_PACKET_CALLBACK(x,u) OPE_GET_PACKET_CALLBACK_REQUEST, (x), (u)*/
148#define OPE_SET_HEADER_GAIN(x) OPE_SET_HEADER_GAIN_REQUEST, ope_check_int(x)
149#define OPE_GET_HEADER_GAIN(x) OPE_GET_HEADER_GAIN_REQUEST, ope_check_int_ptr(x)
150#define OPE_GET_NB_STREAMS(x) OPE_GET_NB_STREAMS_REQUEST, ope_check_int_ptr(x)
151#define OPE_GET_NB_COUPLED_STREAMS(x) OPE_GET_NB_COUPLED_STREAMS_REQUEST, ope_check_int_ptr(x)
154
157
162
171 typedef int (*ope_write_func)(void *user_data, const unsigned char *ptr, opus_int32 len);
172
179 typedef int (*ope_close_func)(void *user_data);
180
187 typedef void (*ope_packet_func)(void *user_data, const unsigned char *packet_ptr, opus_int32 packet_len, opus_uint32 flags);
188
190 typedef struct {
192 ope_write_func write;
194 ope_close_func close;
195} OpusEncCallbacks;
196
198
200typedef struct OggOpusComments OggOpusComments;
201
203typedef struct OggOpusEnc OggOpusEnc;
204
207
212
215OPE_EXPORT OggOpusComments *ope_comments_create(void);
216
220OPE_EXPORT OggOpusComments *ope_comments_copy(OggOpusComments *comments);
221
224OPE_EXPORT void ope_comments_destroy(OggOpusComments *comments);
225
232OPE_EXPORT int ope_comments_add(OggOpusComments *comments, const char *tag, const char *val);
233
239OPE_EXPORT int ope_comments_add_string(OggOpusComments *comments, const char *tag_and_val);
240
248OPE_EXPORT int ope_comments_add_picture(OggOpusComments *comments, const char *filename, int picture_type, const char *description);
249
258OPE_EXPORT int ope_comments_add_picture_from_memory(OggOpusComments *comments, const char *ptr, size_t size, int picture_type, const char *description);
259
262
265
270
280OPE_EXPORT OggOpusEnc *ope_encoder_create_file(const char *path, OggOpusComments *comments, opus_int32 rate, int channels, int family, int *error);
281
292OPE_EXPORT OggOpusEnc *ope_encoder_create_callbacks(const OpusEncCallbacks *callbacks, void *user_data,
293 OggOpusComments *comments, opus_int32 rate, int channels, int family, int *error);
294
304OPE_EXPORT OggOpusEnc *ope_encoder_create_pull(OggOpusComments *comments, opus_int32 rate, int channels, int family, int *error);
305
315OPE_EXPORT int ope_encoder_deferred_init_with_mapping(OggOpusEnc *enc, int family, int streams,
316 int coupled_streams, const unsigned char *mapping);
317
323OPE_EXPORT int ope_encoder_write_float(OggOpusEnc *enc, const float *pcm, int samples_per_channel);
324
330OPE_EXPORT int ope_encoder_write(OggOpusEnc *enc, const opus_int16 *pcm, int samples_per_channel);
331
338OPE_EXPORT int ope_encoder_get_page(OggOpusEnc *enc, unsigned char **page, opus_int32 *len, int flush);
339
344OPE_EXPORT int ope_encoder_drain(OggOpusEnc *enc);
345
349OPE_EXPORT void ope_encoder_destroy(OggOpusEnc *enc);
350
356OPE_EXPORT int ope_encoder_chain_current(OggOpusEnc *enc, OggOpusComments *comments);
357
364OPE_EXPORT int ope_encoder_continue_new_file(OggOpusEnc *enc, const char *path, OggOpusComments *comments);
365
372OPE_EXPORT int ope_encoder_continue_new_callbacks(OggOpusEnc *enc, void *user_data, OggOpusComments *comments);
373
378OPE_EXPORT int ope_encoder_flush_header(OggOpusEnc *enc);
379
385OPE_EXPORT int ope_encoder_ctl(OggOpusEnc *enc, int request, ...);
386
392OPE_EXPORT const char *ope_strerror(int error);
393
396OPE_EXPORT const char *ope_get_version_string(void);
397
400OPE_EXPORT int ope_get_abi_version(void);
401
404
405# if defined(__cplusplus)
406}
407# endif
408
409#endif
ope_write_func
int(* ope_write_func)(void *user_data, const unsigned char *ptr, opus_int32 len)
Called for writing a page.
Definition opusenc.h:171
ope_close_func
int(* ope_close_func)(void *user_data)
Called for closing a stream.
Definition opusenc.h:179
ope_packet_func
void(* ope_packet_func)(void *user_data, const unsigned char *packet_ptr, opus_int32 packet_len, opus_uint32 flags)
Called on every packet encoded (including header).
Definition opusenc.h:187
ope_comments_copy
OPE_EXPORT OggOpusComments * ope_comments_copy(OggOpusComments *comments)
Create a deep copy of a comments object.
ope_comments_add
OPE_EXPORT int ope_comments_add(OggOpusComments *comments, const char *tag, const char *val)
Add a comment.
ope_comments_create
OPE_EXPORT OggOpusComments * ope_comments_create(void)
Create a new comments object.
ope_comments_destroy
OPE_EXPORT void ope_comments_destroy(OggOpusComments *comments)
Destroys a comments object.
ope_comments_add_picture
OPE_EXPORT int ope_comments_add_picture(OggOpusComments *comments, const char *filename, int picture_type, const char *description)
Add a picture from a file.
ope_comments_add_picture_from_memory
OPE_EXPORT int ope_comments_add_picture_from_memory(OggOpusComments *comments, const char *ptr, size_t size, int picture_type, const char *description)
Add a picture already in memory.
ope_comments_add_string
OPE_EXPORT int ope_comments_add_string(OggOpusComments *comments, const char *tag_and_val)
Add a comment as a single tag=value string.
ope_encoder_create_callbacks
OPE_EXPORT OggOpusEnc * ope_encoder_create_callbacks(const OpusEncCallbacks *callbacks, void *user_data, OggOpusComments *comments, opus_int32 rate, int channels, int family, int *error)
Create a new OggOpus stream to be handled using callbacks.
ope_encoder_drain
OPE_EXPORT int ope_encoder_drain(OggOpusEnc *enc)
Finalizes the stream, but does not deallocate the object.
ope_encoder_create_pull
OPE_EXPORT OggOpusEnc * ope_encoder_create_pull(OggOpusComments *comments, opus_int32 rate, int channels, int family, int *error)
Create a new OggOpus stream to be used along with.ope_encoder_get_page().
ope_get_version_string
OPE_EXPORT const char * ope_get_version_string(void)
Returns a string representing the version of libopusenc being used at run time.
ope_encoder_get_page
OPE_EXPORT int ope_encoder_get_page(OggOpusEnc *enc, unsigned char **page, opus_int32 *len, int flush)
Get the next page from the stream (only if using ope_encoder_create_pull()).
ope_encoder_write_float
OPE_EXPORT int ope_encoder_write_float(OggOpusEnc *enc, const float *pcm, int samples_per_channel)
Add/encode any number of float samples to the stream.
ope_encoder_continue_new_callbacks
OPE_EXPORT int ope_encoder_continue_new_callbacks(OggOpusEnc *enc, void *user_data, OggOpusComments *comments)
Ends the stream and create a new file (callback-based).
ope_encoder_continue_new_file
OPE_EXPORT int ope_encoder_continue_new_file(OggOpusEnc *enc, const char *path, OggOpusComments *comments)
Ends the stream and create a new file.
ope_encoder_create_file
OPE_EXPORT OggOpusEnc * ope_encoder_create_file(const char *path, OggOpusComments *comments, opus_int32 rate, int channels, int family, int *error)
Create a new OggOpus file.
ope_encoder_flush_header
OPE_EXPORT int ope_encoder_flush_header(OggOpusEnc *enc)
Write out the header now rather than wait for audio to begin.
ope_strerror
OPE_EXPORT const char * ope_strerror(int error)
Converts a libopusenc error code into a human readable string.
ope_encoder_destroy
OPE_EXPORT void ope_encoder_destroy(OggOpusEnc *enc)
Deallocates the obect.
ope_get_abi_version
OPE_EXPORT int ope_get_abi_version(void)
ABI version for this header.
ope_encoder_write
OPE_EXPORT int ope_encoder_write(OggOpusEnc *enc, const opus_int16 *pcm, int samples_per_channel)
Add/encode any number of 16-bit linear samples to the stream.
ope_encoder_deferred_init_with_mapping
OPE_EXPORT int ope_encoder_deferred_init_with_mapping(OggOpusEnc *enc, int family, int streams, int coupled_streams, const unsigned char *mapping)
Deferred initialization of the encoder to force an explicit channel mapping.
ope_encoder_ctl
OPE_EXPORT int ope_encoder_ctl(OggOpusEnc *enc, int request,...)
Sets encoder options.
ope_encoder_chain_current
OPE_EXPORT int ope_encoder_chain_current(OggOpusEnc *enc, OggOpusComments *comments)
Ends the stream and create a new stream within the same file.
OpusEncCallbacks
Callback functions for accessing the stream.
Definition opusenc.h:190
OpusEncCallbacks::write
ope_write_func write
Callback for writing to the stream.
Definition opusenc.h:192
OpusEncCallbacks::close
ope_close_func close
Callback for closing the stream.
Definition opusenc.h:194