libgsmenc.c 4.38 KB
Newer Older
1
/*
2
 * Interface to libgsm for GSM encoding
3
 * Copyright (c) 2005 Alban Bedel <albeu@free.fr>
4
 * Copyright (c) 2006, 2007 Michel Bardiaux <mbardiaux@mediaxim.be>
5
 *
6
 * This file is part of Libav.
7
 *
8
 * Libav is free software; you can redistribute it and/or
9 10
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * Libav is distributed in the hope that it will be useful,
14 15 16 17 18
 * 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
19
 * License along with Libav; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22

23
/**
24
 * @file
25
 * Interface to libgsm for GSM encoding
26 27
 */

28 29
// The idiosyncrasies of GSM-in-WAV are explained at http://kbs.cs.tu-berlin.de/~jutta/toast.html

30
#include <gsm.h>
31

32
#include "libavutil/common.h"
33

Justin Ruggles's avatar
Justin Ruggles committed
34
#include "avcodec.h"
35
#include "internal.h"
Justin Ruggles's avatar
Justin Ruggles committed
36
#include "gsm.h"
37

38
static av_cold int libgsm_encode_init(AVCodecContext *avctx) {
39 40 41
    if (avctx->channels > 1) {
        av_log(avctx, AV_LOG_ERROR, "Mono required for GSM, got %d channels\n",
               avctx->channels);
42
        return -1;
43
    }
Michael Niedermayer's avatar
Michael Niedermayer committed
44

45 46 47 48 49 50 51 52 53 54 55 56 57 58
    if (avctx->sample_rate != 8000) {
        av_log(avctx, AV_LOG_ERROR, "Sample rate 8000Hz required for GSM, got %dHz\n",
               avctx->sample_rate);
        if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)
            return -1;
    }
    if (avctx->bit_rate != 13000 /* Official */ &&
        avctx->bit_rate != 13200 /* Very common */ &&
        avctx->bit_rate != 0 /* Unknown; a.o. mov does not set bitrate when decoding */ ) {
        av_log(avctx, AV_LOG_ERROR, "Bitrate 13000bps required for GSM, got %dbps\n",
               avctx->bit_rate);
        if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)
            return -1;
    }
59 60

    avctx->priv_data = gsm_create();
61

62
    switch(avctx->codec_id) {
63
    case AV_CODEC_ID_GSM:
64 65 66
        avctx->frame_size = GSM_FRAME_SIZE;
        avctx->block_align = GSM_BLOCK_SIZE;
        break;
67
    case AV_CODEC_ID_GSM_MS: {
68 69 70 71 72 73 74
        int one = 1;
        gsm_option(avctx->priv_data, GSM_OPT_WAV49, &one);
        avctx->frame_size = 2*GSM_FRAME_SIZE;
        avctx->block_align = GSM_MS_BLOCK_SIZE;
        }
    }

75 76 77
    return 0;
}

78
static av_cold int libgsm_encode_close(AVCodecContext *avctx) {
79 80 81 82 83
    gsm_destroy(avctx->priv_data);
    avctx->priv_data = NULL;
    return 0;
}

84 85 86 87 88 89 90 91 92 93 94
static int libgsm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
                               const AVFrame *frame, int *got_packet_ptr)
{
    int ret;
    gsm_signal *samples = (gsm_signal *)frame->data[0];
    struct gsm_state *state = avctx->priv_data;

    if ((ret = ff_alloc_packet(avpkt, avctx->block_align))) {
        av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
        return ret;
    }
95 96

    switch(avctx->codec_id) {
97
    case AV_CODEC_ID_GSM:
98
        gsm_encode(state, samples, avpkt->data);
99
        break;
100
    case AV_CODEC_ID_GSM_MS:
101 102
        gsm_encode(state, samples,                  avpkt->data);
        gsm_encode(state, samples + GSM_FRAME_SIZE, avpkt->data + 32);
103
    }
104 105 106

    *got_packet_ptr = 1;
    return 0;
107 108 109
}


110
AVCodec ff_libgsm_encoder = {
111
    .name           = "libgsm",
112
    .long_name      = NULL_IF_CONFIG_SMALL("libgsm GSM"),
113
    .type           = AVMEDIA_TYPE_AUDIO,
114
    .id             = AV_CODEC_ID_GSM,
115
    .init           = libgsm_encode_init,
116
    .encode2        = libgsm_encode_frame,
117
    .close          = libgsm_encode_close,
118 119
    .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
                                                     AV_SAMPLE_FMT_NONE },
120
    .wrapper_name   = "libgsm",
121 122
};

123
AVCodec ff_libgsm_ms_encoder = {
124
    .name           = "libgsm_ms",
125
    .long_name      = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),
126
    .type           = AVMEDIA_TYPE_AUDIO,
127
    .id             = AV_CODEC_ID_GSM_MS,
128
    .init           = libgsm_encode_init,
129
    .encode2        = libgsm_encode_frame,
130
    .close          = libgsm_encode_close,
131 132
    .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
                                                     AV_SAMPLE_FMT_NONE },
133
    .wrapper_name   = "libgsm",
134
};