Harald Welte | 8b01f0c | 2017-05-28 11:04:26 +0200 | [diff] [blame^] | 1 | /* AMR (GSM 06.90) codec */ |
| 2 | /* (C) 2017 Harald Welte <laforge@gnumonks.org> */ |
| 3 | |
| 4 | /* |
| 5 | * This file is part of gapk (GSM Audio Pocket Knife). |
| 6 | * |
| 7 | * gapk is free software: you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation, either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * gapk is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with gapk. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include <gapk/codecs.h> |
| 22 | #include <gapk/benchmark.h> |
| 23 | |
| 24 | #include "config.h" |
| 25 | |
| 26 | |
| 27 | #ifdef HAVE_OPENCORE_AMRNB |
| 28 | |
| 29 | #include <stdlib.h> |
| 30 | #include <stdio.h> |
| 31 | |
| 32 | #include <opencore-amrnb/interf_dec.h> |
| 33 | #include <opencore-amrnb/interf_enc.h> |
| 34 | |
| 35 | struct codec_amr_state { |
| 36 | void *encoder; |
| 37 | void *decoder; |
| 38 | }; |
| 39 | |
| 40 | |
| 41 | static void * |
| 42 | codec_amr_init(void) |
| 43 | { |
| 44 | struct codec_amr_state *st; |
| 45 | |
| 46 | st = calloc(1, sizeof(*st)); |
| 47 | if (!st) |
| 48 | return NULL; |
| 49 | |
| 50 | st->encoder = Encoder_Interface_init(0); |
| 51 | st->decoder = Decoder_Interface_init(); |
| 52 | |
| 53 | return (void *)st; |
| 54 | } |
| 55 | |
| 56 | static void |
| 57 | codec_amr_exit(void *state) |
| 58 | { |
| 59 | struct codec_amr_state *st = state; |
| 60 | |
| 61 | Decoder_Interface_exit(st->decoder); |
| 62 | Encoder_Interface_exit(st->encoder); |
| 63 | |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | static int |
| 68 | codec_amr_encode(void *state, uint8_t *cod, const uint8_t *pcm, unsigned int pcm_len) |
| 69 | { |
| 70 | struct codec_amr_state *st = state; |
| 71 | int rv; |
| 72 | |
| 73 | BENCHMARK_START; |
| 74 | rv = Encoder_Interface_Encode( |
| 75 | st->encoder, |
| 76 | MR122, |
| 77 | (const short*) pcm, |
| 78 | (unsigned char*) cod, |
| 79 | 1 |
| 80 | ); |
| 81 | BENCHMARK_STOP(CODEC_EFR, 1); |
| 82 | |
| 83 | return rv; |
| 84 | } |
| 85 | |
| 86 | static int |
| 87 | codec_amr_decode(void *state, uint8_t *pcm, const uint8_t *cod, unsigned int cod_len) |
| 88 | { |
| 89 | struct codec_amr_state *st = state; |
| 90 | |
| 91 | printf("%s(): %u bytes in\n", __func__, cod_len); |
| 92 | |
| 93 | BENCHMARK_START; |
| 94 | Decoder_Interface_Decode( |
| 95 | st->decoder, |
| 96 | (const unsigned char*) cod, |
| 97 | (short *) pcm, |
| 98 | 0 |
| 99 | ); |
| 100 | BENCHMARK_STOP(CODEC_EFR, 0); |
| 101 | |
| 102 | return PCM_CANON_LEN; |
| 103 | } |
| 104 | |
| 105 | #endif /* HAVE_OPENCORE_AMRNB */ |
| 106 | |
| 107 | |
| 108 | const struct codec_desc codec_amr_desc = { |
| 109 | .type = CODEC_AMR, |
| 110 | .name = "amr", |
| 111 | .description = "GSM 26.071 Adaptive Multi Rate codec", |
| 112 | .canon_frame_len = 0, |
| 113 | #ifdef HAVE_OPENCORE_AMRNB |
| 114 | .codec_enc_format_type = FMT_AMR_OPENCORE, |
| 115 | .codec_dec_format_type = FMT_AMR_OPENCORE, |
| 116 | .codec_init = codec_amr_init, |
| 117 | .codec_exit = codec_amr_exit, |
| 118 | .codec_encode = codec_amr_encode, |
| 119 | .codec_decode = codec_amr_decode, |
| 120 | #endif |
| 121 | }; |