libilbc.c 5.84 KB
Newer Older
1 2 3 4
/*
 * iLBC decoder/encoder stub
 * Copyright (c) 2012 Martin Storsjo
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 9 10 11
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19 20 21 22 23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <ilbc.h>

24
#include "libavutil/channel_layout.h"
25
#include "libavutil/common.h"
26
#include "libavutil/opt.h"
27
#include "avcodec.h"
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include "internal.h"

static int get_mode(AVCodecContext *avctx)
{
    if (avctx->block_align == 38)
        return 20;
    else if (avctx->block_align == 50)
        return 30;
    else if (avctx->bit_rate > 0)
        return avctx->bit_rate <= 14000 ? 30 : 20;
    else
        return -1;
}

typedef struct ILBCDecContext {
    const AVClass *class;
    iLBC_Dec_Inst_t decoder;
    int enhance;
} ILBCDecContext;

static const AVOption ilbc_dec_options[] = {
49
    { "enhance", "Enhance the decoded audio (adds delay)", offsetof(ILBCDecContext, enhance), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM },
50 51 52 53
    { NULL }
};

static const AVClass ilbc_dec_class = {
54 55 56
    .class_name = "libilbc",
    .item_name  = av_default_item_name,
    .option     = ilbc_dec_options,
chinshou's avatar
chinshou committed
57
    .version    = LIBAVUTIL_VERSION_INT,
58 59 60 61 62 63 64 65 66 67 68 69 70 71
};

static av_cold int ilbc_decode_init(AVCodecContext *avctx)
{
    ILBCDecContext *s  = avctx->priv_data;
    int mode;

    if ((mode = get_mode(avctx)) < 0) {
        av_log(avctx, AV_LOG_ERROR, "iLBC frame mode not indicated\n");
        return AVERROR(EINVAL);
    }

    WebRtcIlbcfix_InitDecode(&s->decoder, mode, s->enhance);

72 73 74 75
    avctx->channels       = 1;
    avctx->channel_layout = AV_CH_LAYOUT_MONO;
    avctx->sample_rate    = 8000;
    avctx->sample_fmt     = AV_SAMPLE_FMT_S16;
76 77 78 79 80 81 82 83 84 85

    return 0;
}

static int ilbc_decode_frame(AVCodecContext *avctx, void *data,
                             int *got_frame_ptr, AVPacket *avpkt)
{
    const uint8_t *buf = avpkt->data;
    int buf_size       = avpkt->size;
    ILBCDecContext *s  = avctx->priv_data;
86
    AVFrame *frame     = data;
87 88 89 90 91 92 93 94
    int ret;

    if (s->decoder.no_of_bytes > buf_size) {
        av_log(avctx, AV_LOG_ERROR, "iLBC frame too short (%u, should be %u)\n",
               buf_size, s->decoder.no_of_bytes);
        return AVERROR_INVALIDDATA;
    }

95
    frame->nb_samples = s->decoder.blockl;
96
    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
97 98
        return ret;

99
    WebRtcIlbcfix_DecodeImpl((WebRtc_Word16*) frame->data[0],
100 101
                             (const WebRtc_UWord16*) buf, &s->decoder, 1);

102
    *got_frame_ptr = 1;
103 104 105 106 107 108

    return s->decoder.no_of_bytes;
}

AVCodec ff_libilbc_decoder = {
    .name           = "libilbc",
109
    .long_name      = NULL_IF_CONFIG_SMALL("iLBC (Internet Low Bitrate Codec)"),
110
    .type           = AVMEDIA_TYPE_AUDIO,
111
    .id             = AV_CODEC_ID_ILBC,
112 113 114 115 116 117 118 119 120 121 122 123 124 125
    .priv_data_size = sizeof(ILBCDecContext),
    .init           = ilbc_decode_init,
    .decode         = ilbc_decode_frame,
    .capabilities   = CODEC_CAP_DR1,
    .priv_class     = &ilbc_dec_class,
};

typedef struct ILBCEncContext {
    const AVClass *class;
    iLBC_Enc_Inst_t encoder;
    int mode;
} ILBCEncContext;

static const AVOption ilbc_enc_options[] = {
126
    { "mode", "iLBC mode (20 or 30 ms frames)", offsetof(ILBCEncContext, mode), AV_OPT_TYPE_INT, { .i64 = 20 }, 20, 30, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
127 128 129 130
    { NULL }
};

static const AVClass ilbc_enc_class = {
131 132 133 134
    .class_name = "libilbc",
    .item_name  = av_default_item_name,
    .option     = ilbc_enc_options,
    .version    = LIBAVUTIL_VERSION_INT,
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
};

static av_cold int ilbc_encode_init(AVCodecContext *avctx)
{
    ILBCEncContext *s = avctx->priv_data;
    int mode;

    if (avctx->sample_rate != 8000) {
        av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
        return AVERROR(EINVAL);
    }

    if (avctx->channels != 1) {
        av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
        return AVERROR(EINVAL);
    }

    if ((mode = get_mode(avctx)) > 0)
        s->mode = mode;
    else
        s->mode = s->mode != 30 ? 20 : 30;
    WebRtcIlbcfix_InitEncode(&s->encoder, s->mode);

    avctx->block_align = s->encoder.no_of_bytes;
    avctx->frame_size  = s->encoder.blockl;

    return 0;
}

static int ilbc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
                             const AVFrame *frame, int *got_packet_ptr)
{
    ILBCEncContext *s = avctx->priv_data;
    int ret;

170
    if ((ret = ff_alloc_packet2(avctx, avpkt, 50)) < 0)
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
        return ret;

    WebRtcIlbcfix_EncodeImpl((WebRtc_UWord16*) avpkt->data, (const WebRtc_Word16*) frame->data[0], &s->encoder);

    avpkt->size     = s->encoder.no_of_bytes;
    *got_packet_ptr = 1;
    return 0;
}

static const AVCodecDefault ilbc_encode_defaults[] = {
    { "b", "0" },
    { NULL }
};

AVCodec ff_libilbc_encoder = {
    .name           = "libilbc",
187
    .long_name      = NULL_IF_CONFIG_SMALL("iLBC (Internet Low Bitrate Codec)"),
188
    .type           = AVMEDIA_TYPE_AUDIO,
189
    .id             = AV_CODEC_ID_ILBC,
190 191 192 193 194 195 196 197
    .priv_data_size = sizeof(ILBCEncContext),
    .init           = ilbc_encode_init,
    .encode2        = ilbc_encode_frame,
    .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
                                                     AV_SAMPLE_FMT_NONE },
    .defaults       = ilbc_encode_defaults,
    .priv_class     = &ilbc_enc_class,
};