41 #if defined(ASTERISK_REGISTER_FILE)
42 ASTERISK_REGISTER_FILE()
43 #elif defined(ASTERISK_FILE_VERSION)
44 ASTERISK_FILE_VERSION(__FILE__,
"$Revision: $")
59 #include <opus/opus.h>
61 #include "asterisk/opus.h"
63 #define BUFFER_SAMPLES 5760
64 #define MAX_CHANNELS 2
65 #define OPUS_SAMPLES 960
68 #include "asterisk/slin.h"
84 static int (*opus_samples_previous)(
struct ast_frame *frame);
92 int16_t buf[BUFFER_SAMPLES];
96 int decode_fec_incoming;
101 unsigned int maxbitrate;
102 unsigned int maxplayrate;
108 unsigned int spropmaxcapturerate;
109 unsigned int spropstereo;
113 static int opus_encoder_construct(
struct ast_trans_pvt *pvt,
int sampling_rate)
117 const opus_int32 bitrate = attr ? attr->maxbitrate : CODEC_OPUS_DEFAULT_BITRATE;
118 const int maxplayrate = attr ? attr->maxplayrate : CODEC_OPUS_DEFAULT_MAX_PLAYBACK_RATE;
119 const int channels = attr ? attr->stereo + 1 : CODEC_OPUS_DEFAULT_STEREO + 1;
120 const opus_int32 vbr = attr ? !(attr->cbr) : !CODEC_OPUS_DEFAULT_CBR;
121 const opus_int32 fec = attr ? attr->fec : CODEC_OPUS_DEFAULT_FEC;
122 const opus_int32 dtx = attr ? attr->dtx : CODEC_OPUS_DEFAULT_DTX;
123 const int application = OPUS_APPLICATION_VOIP;
126 opvt->opus = opus_encoder_create(sampling_rate, channels, application, &status);
128 if (status != OPUS_OK) {
129 ast_log(LOG_ERROR,
"Error creating the Opus encoder: %s\n", opus_strerror(status));
133 if (sampling_rate <= 8000 || maxplayrate <= 8000) {
134 status = opus_encoder_ctl(opvt->opus, OPUS_SET_MAX_BANDWIDTH(OPUS_BANDWIDTH_NARROWBAND));
135 }
else if (sampling_rate <= 12000 || maxplayrate <= 12000) {
136 status = opus_encoder_ctl(opvt->opus, OPUS_SET_MAX_BANDWIDTH(OPUS_BANDWIDTH_MEDIUMBAND));
137 }
else if (sampling_rate <= 16000 || maxplayrate <= 16000) {
138 status = opus_encoder_ctl(opvt->opus, OPUS_SET_MAX_BANDWIDTH(OPUS_BANDWIDTH_WIDEBAND));
139 }
else if (sampling_rate <= 24000 || maxplayrate <= 24000) {
140 status = opus_encoder_ctl(opvt->opus, OPUS_SET_MAX_BANDWIDTH(OPUS_BANDWIDTH_SUPERWIDEBAND));
143 if (0 < bitrate && bitrate != 510000) {
144 status = opus_encoder_ctl(opvt->opus, OPUS_SET_BITRATE(bitrate));
146 status = opus_encoder_ctl(opvt->opus, OPUS_SET_VBR(vbr));
147 status = opus_encoder_ctl(opvt->opus, OPUS_SET_INBAND_FEC(fec));
148 status = opus_encoder_ctl(opvt->opus, OPUS_SET_DTX(dtx));
150 opvt->sampling_rate = sampling_rate;
151 opvt->multiplier = 48000 / sampling_rate;
152 opvt->framesize = sampling_rate / 50;
157 ast_debug(3,
"Created encoder #%d (%d -> opus)\n", opvt->id, sampling_rate);
169 opvt->multiplier = 48000 / opvt->sampling_rate;
172 opvt->opus = opus_decoder_create(opvt->sampling_rate, opvt->channels, &error);
174 if (error != OPUS_OK) {
175 ast_log(LOG_ERROR,
"Error creating the Opus decoder: %s\n", opus_strerror(error));
183 ast_debug(3,
"Created decoder #%d (opus -> %d)\n", opvt->id, opvt->sampling_rate);
198 opvt->previous_lost = 0;
224 while (pvt->
samples >= opvt->framesize) {
226 const int status = opus_encode(opvt->opus,
232 samples += opvt->framesize;
233 pvt->
samples -= opvt->framesize;
236 ast_log(LOG_ERROR,
"Error encoding the Opus frame: %s\n", opus_strerror(status));
255 memmove(opvt->buf, opvt->buf + samples, pvt->
samples * 2);
271 if (!opvt->inited && f->
datalen == 0) {
273 }
else if (!opvt->inited) {
274 status = opus_decoder_construct(pvt, f);
289 opvt->decode_fec_incoming = attr->fec;
292 decode_fec = opvt->decode_fec_incoming;
327 if (f->
datalen == 0 && opvt->previous_lost) {
335 opus_decoder_ctl(opvt->opus, OPUS_GET_LAST_PACKET_DURATION(&frame_size));
336 dst = pvt->outbuf.i16 + (pvt->
samples * opvt->channels);
339 status = opus_decode(opvt->opus, src, len, dst, frame_size, decode_fec);
341 ast_log(LOG_ERROR,
"%s\n", opus_strerror(status));
344 pvt->
datalen += status * opvt->channels *
sizeof(int16_t);
350 opvt->previous_lost = (f->
datalen == 0 || status < 0);
355 if (f->
datalen == 0 && !decode_fec) {
361 opus_decoder_ctl(opvt->opus, OPUS_GET_LAST_PACKET_DURATION(&frame_size));
362 dst = pvt->outbuf.i16 + (pvt->
samples * opvt->channels);
365 status = opus_decode(opvt->opus, src, len, dst, frame_size, decode_fec);
367 ast_log(LOG_ERROR,
"%s\n", opus_strerror(status));
370 pvt->
datalen += status * opvt->channels *
sizeof(int16_t);
372 opvt->previous_lost = (f->
datalen == 0 || status < 0);
388 ast_log(LOG_ERROR,
"%s\n", opus_strerror(status));
391 pvt->
datalen += status * opvt->channels *
sizeof(int16_t);
393 opvt->previous_lost = (f->
datalen == 0 || status < 0);
398 if (!opvt->previous_lost) {
404 frame_size = BUFFER_SAMPLES / opvt->multiplier;
405 dst = pvt->outbuf.i16 + (pvt->
samples * opvt->channels);
408 status = opus_decode(opvt->opus, src, len, dst, frame_size, decode_fec);
410 ast_log(LOG_ERROR,
"%s\n", opus_strerror(status));
413 pvt->
datalen += status * opvt->channels *
sizeof(int16_t);
415 opvt->previous_lost = (f->
datalen == 0 || status < 0);
431 opus_decoder_ctl(opvt->opus, OPUS_GET_LAST_PACKET_DURATION(&frame_size));
432 dst = pvt->outbuf.i16 + (pvt->
samples * opvt->channels);
435 status = opus_decode(opvt->opus, src, len, dst, frame_size, decode_fec);
437 ast_log(LOG_ERROR,
"%s\n", opus_strerror(status));
440 pvt->
datalen += status * opvt->channels *
sizeof(int16_t);
443 frame_size = BUFFER_SAMPLES / opvt->multiplier;
444 dst = pvt->outbuf.i16 + (pvt->
samples * opvt->channels);
447 status = opus_decode(opvt->opus, src, len, dst, frame_size, decode_fec);
449 ast_log(LOG_ERROR,
"%s\n", opus_strerror(status));
452 pvt->
datalen += status * opvt->channels *
sizeof(int16_t);
454 opvt->previous_lost = (f->
datalen == 0 || status < 0);
461 opus_decoder_ctl(opvt->opus, OPUS_GET_LAST_PACKET_DURATION(&frame_size));
462 dst = pvt->outbuf.i16 + (pvt->
samples * opvt->channels);
465 status = opus_decode(opvt->opus, src, len, dst, frame_size, decode_fec);
467 ast_log(LOG_ERROR,
"%s\n", opus_strerror(status));
470 pvt->
datalen += status * opvt->channels *
sizeof(int16_t);
473 frame_size = BUFFER_SAMPLES / opvt->multiplier;
474 dst = pvt->outbuf.i16 + (pvt->
samples * opvt->channels);
477 status = opus_decode(opvt->opus, src, len, dst, frame_size, decode_fec);
479 ast_log(LOG_ERROR,
"%s\n", opus_strerror(status));
482 pvt->
datalen += status * opvt->channels *
sizeof(int16_t);
484 opvt->previous_lost = (f->
datalen == 0 || status < 0);
493 if (!opvt || !opvt->opus) {
497 opus_encoder_destroy(opvt->opus);
502 ast_debug(3,
"Destroyed encoder #%d (%d->opus)\n", opvt->id, opvt->sampling_rate);
509 if (!opvt || !opvt->opus) {
513 opus_decoder_destroy(opvt->opus);
518 ast_debug(3,
"Destroyed decoder #%d (opus->%d)\n", opvt->id, opvt->sampling_rate);
530 " Displays Opus encoder/decoder utilization.\n";
537 return CLI_SHOWUSAGE;
542 ast_cli(a->fd,
"%d/%d encoders/decoders are in use.\n",
copy.encoders,
copy.decoders);
553 .type = AST_MEDIA_TYPE_AUDIO,
554 .sample_rate = 48000,
558 .type = AST_MEDIA_TYPE_AUDIO,
562 .newpvt = opustolin_new,
563 .framein = opustolin_framein,
564 .destroy = opustolin_destroy,
565 .sample = opus_sample,
567 .buffer_samples = (BUFFER_SAMPLES / (48000 / 8000)) * 2,
568 .buf_size = (BUFFER_SAMPLES / (48000 / 8000)) * MAX_CHANNELS *
sizeof(opus_int16) * 2,
577 .type = AST_MEDIA_TYPE_AUDIO,
582 .type = AST_MEDIA_TYPE_AUDIO,
583 .sample_rate = 48000,
586 .newpvt = lintoopus_new,
587 .framein = lintoopus_framein,
588 .frameout = lintoopus_frameout,
589 .destroy = lintoopus_destroy,
590 .sample = slin8_sample,
592 .buffer_samples = BUFFER_SAMPLES,
593 .buf_size = BUFFER_SAMPLES * 2,
598 .name =
"opustolin12",
601 .type = AST_MEDIA_TYPE_AUDIO,
602 .sample_rate = 48000,
606 .type = AST_MEDIA_TYPE_AUDIO,
607 .sample_rate = 12000,
610 .newpvt = opustolin_new,
611 .framein = opustolin_framein,
612 .destroy = opustolin_destroy,
613 .sample = opus_sample,
615 .buffer_samples = (BUFFER_SAMPLES / (48000 / 12000)) * 2,
616 .buf_size = (BUFFER_SAMPLES / (48000 / 12000)) * MAX_CHANNELS *
sizeof(opus_int16) * 2,
622 .name =
"lin12toopus",
625 .type = AST_MEDIA_TYPE_AUDIO,
626 .sample_rate = 12000,
630 .type = AST_MEDIA_TYPE_AUDIO,
631 .sample_rate = 48000,
634 .newpvt = lintoopus_new,
635 .framein = lintoopus_framein,
636 .frameout = lintoopus_frameout,
637 .destroy = lintoopus_destroy,
639 .buffer_samples = BUFFER_SAMPLES,
640 .buf_size = BUFFER_SAMPLES * 2,
645 .name =
"opustolin16",
648 .type = AST_MEDIA_TYPE_AUDIO,
649 .sample_rate = 48000,
653 .type = AST_MEDIA_TYPE_AUDIO,
654 .sample_rate = 16000,
657 .newpvt = opustolin_new,
658 .framein = opustolin_framein,
659 .destroy = opustolin_destroy,
660 .sample = opus_sample,
662 .buffer_samples = (BUFFER_SAMPLES / (48000 / 16000)) * 2,
663 .buf_size = (BUFFER_SAMPLES / (48000 / 16000)) * MAX_CHANNELS *
sizeof(opus_int16) * 2,
669 .name =
"lin16toopus",
672 .type = AST_MEDIA_TYPE_AUDIO,
673 .sample_rate = 16000,
677 .type = AST_MEDIA_TYPE_AUDIO,
678 .sample_rate = 48000,
681 .newpvt = lintoopus_new,
682 .framein = lintoopus_framein,
683 .frameout = lintoopus_frameout,
684 .destroy = lintoopus_destroy,
685 .sample = slin16_sample,
687 .buffer_samples = BUFFER_SAMPLES,
688 .buf_size = BUFFER_SAMPLES * 2,
693 .name =
"opustolin24",
696 .type = AST_MEDIA_TYPE_AUDIO,
697 .sample_rate = 48000,
701 .type = AST_MEDIA_TYPE_AUDIO,
702 .sample_rate = 24000,
705 .newpvt = opustolin_new,
706 .framein = opustolin_framein,
707 .destroy = opustolin_destroy,
708 .sample = opus_sample,
710 .buffer_samples = (BUFFER_SAMPLES / (48000 / 24000)) * 2,
711 .buf_size = (BUFFER_SAMPLES / (48000 / 24000)) * MAX_CHANNELS *
sizeof(opus_int16) * 2,
717 .name =
"lin24toopus",
720 .type = AST_MEDIA_TYPE_AUDIO,
721 .sample_rate = 24000,
725 .type = AST_MEDIA_TYPE_AUDIO,
726 .sample_rate = 48000,
729 .newpvt = lintoopus_new,
730 .framein = lintoopus_framein,
731 .frameout = lintoopus_frameout,
732 .destroy = lintoopus_destroy,
734 .buffer_samples = BUFFER_SAMPLES,
735 .buf_size = BUFFER_SAMPLES * 2,
740 .name =
"opustolin48",
743 .type = AST_MEDIA_TYPE_AUDIO,
744 .sample_rate = 48000,
748 .type = AST_MEDIA_TYPE_AUDIO,
749 .sample_rate = 48000,
752 .newpvt = opustolin_new,
753 .framein = opustolin_framein,
754 .destroy = opustolin_destroy,
755 .sample = opus_sample,
757 .buffer_samples = BUFFER_SAMPLES * 2,
758 .buf_size = BUFFER_SAMPLES * MAX_CHANNELS *
sizeof(opus_int16) * 2,
764 .name =
"lin48toopus",
767 .type = AST_MEDIA_TYPE_AUDIO,
768 .sample_rate = 48000,
772 .type = AST_MEDIA_TYPE_AUDIO,
773 .sample_rate = 48000,
776 .newpvt = lintoopus_new,
777 .framein = lintoopus_framein,
778 .frameout = lintoopus_frameout,
779 .destroy = lintoopus_destroy,
781 .buffer_samples = BUFFER_SAMPLES,
782 .buf_size = BUFFER_SAMPLES * 2,
786 AST_CLI_DEFINE(handle_cli_opus_show,
"Display Opus codec utilization.")
789 static int opus_samples(
struct ast_frame *frame)
791 opus_int32 sampling_rate = 48000;
793 return opus_packet_get_nb_samples(frame->
data.ptr, frame->
datalen, sampling_rate);
796 static int reload(
void)
802 static int unload_module(
void)
825 static int load_module(
void)
829 opus_codec =
ast_codec_get(
"opus", AST_MEDIA_TYPE_AUDIO, 48000);
849 AST_MODULE_INFO(
ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT,
"Opus Coder/Decoder",
851 .unload = unload_module,
struct ast_frame * ast_trans_frameout(struct ast_trans_pvt *pvt, int datalen, int samples)
generic frameout function
int datalen
actual space used in outbuf
Asterisk locking-related definitions:
Asterisk main include file. File version handling, generic pbx functions.
Descriptor of a translator.
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Support for translation of data formats. translate.c.
descriptor for a cli entry.
#define AST_LIST_NEXT(elm, field)
Returns the next entry in the list after the given entry.
#define ast_cli_register_multiple(e, len)
Register multiple commands.
static int copy(char *infile, char *outfile)
Utility function to copy a file.
struct ast_codec * ast_codec_get(const char *name, enum ast_media_type type, unsigned int sample_rate)
Retrieve a codec given a name, type, and sample rate.
Opus attribute structure.
int ast_atomic_fetchadd_int(volatile int *p, int v)
Atomically add v to *p and return the previous value of *p.
struct ast_frame_subclass subclass
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
#define ast_register_translator(t)
See __ast_register_translator()
Asterisk internal frame definitions.
int ast_unregister_translator(struct ast_translator *t)
Unregister a translator Unregisters the given translator.
A set of macros to manage forward-linked lists.
#define ast_debug(level,...)
Log a DEBUG message.
struct ast_codec dst_codec
struct ast_format * explicit_dst
Default structure for translators, with the basic fields and buffers, all allocated as part of the sa...
struct ast_codec src_codec
union ast_frame::@224 data
Support for logging to various files, console and syslog Configuration in file logger.conf.
unsigned int sample_rate
Sample rate (number of samples carried in a second)
Standard Command Line Interface.
int(* samples_count)(struct ast_frame *frame)
Retrieve the number of samples in a frame.
Data structure associated with a single frame of data.
struct ast_format * format
#define ASTERISK_GPL_KEY
The text the key() function should return.
Asterisk module definitions.
Represents a media codec within Asterisk.