mirror of https://github.com/FFmpeg/FFmpeg.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
154 lines
4.4 KiB
154 lines
4.4 KiB
20 years ago
|
/*
|
||
11 years ago
|
* Interface to libgsm for GSM decoding
|
||
20 years ago
|
* Copyright (c) 2005 Alban Bedel <albeu@free.fr>
|
||
18 years ago
|
* Copyright (c) 2006, 2007 Michel Bardiaux <mbardiaux@mediaxim.be>
|
||
20 years ago
|
*
|
||
18 years ago
|
* This file is part of FFmpeg.
|
||
|
*
|
||
|
* FFmpeg is free software; you can redistribute it and/or
|
||
20 years ago
|
* modify it under the terms of the GNU Lesser General Public
|
||
|
* License as published by the Free Software Foundation; either
|
||
18 years ago
|
* version 2.1 of the License, or (at your option) any later version.
|
||
20 years ago
|
*
|
||
18 years ago
|
* FFmpeg is distributed in the hope that it will be useful,
|
||
20 years ago
|
* 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 years ago
|
* License along with FFmpeg; if not, write to the Free Software
|
||
19 years ago
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||
20 years ago
|
*/
|
||
19 years ago
|
|
||
20 years ago
|
/**
|
||
15 years ago
|
* @file
|
||
11 years ago
|
* Interface to libgsm for GSM decoding
|
||
20 years ago
|
*/
|
||
|
|
||
18 years ago
|
// The idiosyncrasies of GSM-in-WAV are explained at http://kbs.cs.tu-berlin.de/~jutta/toast.html
|
||
|
|
||
12 years ago
|
#include "config.h"
|
||
|
#if HAVE_GSM_H
|
||
|
#include <gsm.h>
|
||
|
#else
|
||
15 years ago
|
#include <gsm/gsm.h>
|
||
12 years ago
|
#endif
|
||
20 years ago
|
|
||
12 years ago
|
#include "libavutil/channel_layout.h"
|
||
|
#include "libavutil/common.h"
|
||
11 years ago
|
|
||
13 years ago
|
#include "avcodec.h"
|
||
13 years ago
|
#include "internal.h"
|
||
13 years ago
|
#include "gsm.h"
|
||
20 years ago
|
|
||
13 years ago
|
typedef struct LibGSMDecodeContext {
|
||
|
struct gsm_state *state;
|
||
|
} LibGSMDecodeContext;
|
||
|
|
||
13 years ago
|
static av_cold int libgsm_decode_init(AVCodecContext *avctx) {
|
||
13 years ago
|
LibGSMDecodeContext *s = avctx->priv_data;
|
||
|
|
||
12 years ago
|
avctx->channels = 1;
|
||
|
avctx->channel_layout = AV_CH_LAYOUT_MONO;
|
||
12 years ago
|
if (!avctx->sample_rate)
|
||
|
avctx->sample_rate = 8000;
|
||
12 years ago
|
avctx->sample_fmt = AV_SAMPLE_FMT_S16;
|
||
13 years ago
|
|
||
13 years ago
|
s->state = gsm_create();
|
||
13 years ago
|
|
||
|
switch(avctx->codec_id) {
|
||
12 years ago
|
case AV_CODEC_ID_GSM:
|
||
13 years ago
|
avctx->frame_size = GSM_FRAME_SIZE;
|
||
|
avctx->block_align = GSM_BLOCK_SIZE;
|
||
|
break;
|
||
12 years ago
|
case AV_CODEC_ID_GSM_MS: {
|
||
13 years ago
|
int one = 1;
|
||
13 years ago
|
gsm_option(s->state, GSM_OPT_WAV49, &one);
|
||
13 years ago
|
avctx->frame_size = 2 * GSM_FRAME_SIZE;
|
||
|
avctx->block_align = GSM_MS_BLOCK_SIZE;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
static av_cold int libgsm_decode_close(AVCodecContext *avctx) {
|
||
13 years ago
|
LibGSMDecodeContext *s = avctx->priv_data;
|
||
|
|
||
|
gsm_destroy(s->state);
|
||
|
s->state = NULL;
|
||
13 years ago
|
return 0;
|
||
|
}
|
||
|
|
||
13 years ago
|
static int libgsm_decode_frame(AVCodecContext *avctx, void *data,
|
||
|
int *got_frame_ptr, AVPacket *avpkt)
|
||
|
{
|
||
13 years ago
|
int i, ret;
|
||
13 years ago
|
LibGSMDecodeContext *s = avctx->priv_data;
|
||
12 years ago
|
AVFrame *frame = data;
|
||
14 years ago
|
uint8_t *buf = avpkt->data;
|
||
16 years ago
|
int buf_size = avpkt->size;
|
||
13 years ago
|
int16_t *samples;
|
||
13 years ago
|
|
||
13 years ago
|
if (buf_size < avctx->block_align) {
|
||
|
av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
|
||
|
return AVERROR_INVALIDDATA;
|
||
18 years ago
|
}
|
||
13 years ago
|
|
||
13 years ago
|
/* get output buffer */
|
||
12 years ago
|
frame->nb_samples = avctx->frame_size;
|
||
12 years ago
|
if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
|
||
13 years ago
|
return ret;
|
||
12 years ago
|
samples = (int16_t *)frame->data[0];
|
||
13 years ago
|
|
||
13 years ago
|
for (i = 0; i < avctx->frame_size / GSM_FRAME_SIZE; i++) {
|
||
13 years ago
|
if ((ret = gsm_decode(s->state, buf, samples)) < 0)
|
||
13 years ago
|
return -1;
|
||
|
buf += GSM_BLOCK_SIZE;
|
||
|
samples += GSM_FRAME_SIZE;
|
||
18 years ago
|
}
|
||
13 years ago
|
|
||
12 years ago
|
*got_frame_ptr = 1;
|
||
13 years ago
|
|
||
18 years ago
|
return avctx->block_align;
|
||
20 years ago
|
}
|
||
|
|
||
13 years ago
|
static void libgsm_flush(AVCodecContext *avctx) {
|
||
13 years ago
|
LibGSMDecodeContext *s = avctx->priv_data;
|
||
13 years ago
|
int one = 1;
|
||
13 years ago
|
|
||
|
gsm_destroy(s->state);
|
||
|
s->state = gsm_create();
|
||
12 years ago
|
if (avctx->codec_id == AV_CODEC_ID_GSM_MS)
|
||
13 years ago
|
gsm_option(s->state, GSM_OPT_WAV49, &one);
|
||
13 years ago
|
}
|
||
|
|
||
13 years ago
|
#if CONFIG_LIBGSM_DECODER
|
||
14 years ago
|
AVCodec ff_libgsm_decoder = {
|
||
14 years ago
|
.name = "libgsm",
|
||
11 years ago
|
.long_name = NULL_IF_CONFIG_SMALL("libgsm GSM"),
|
||
14 years ago
|
.type = AVMEDIA_TYPE_AUDIO,
|
||
12 years ago
|
.id = AV_CODEC_ID_GSM,
|
||
13 years ago
|
.priv_data_size = sizeof(LibGSMDecodeContext),
|
||
13 years ago
|
.init = libgsm_decode_init,
|
||
|
.close = libgsm_decode_close,
|
||
14 years ago
|
.decode = libgsm_decode_frame,
|
||
13 years ago
|
.flush = libgsm_flush,
|
||
13 years ago
|
.capabilities = CODEC_CAP_DR1,
|
||
20 years ago
|
};
|
||
13 years ago
|
#endif
|
||
|
#if CONFIG_LIBGSM_MS_DECODER
|
||
14 years ago
|
AVCodec ff_libgsm_ms_decoder = {
|
||
14 years ago
|
.name = "libgsm_ms",
|
||
11 years ago
|
.long_name = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),
|
||
14 years ago
|
.type = AVMEDIA_TYPE_AUDIO,
|
||
12 years ago
|
.id = AV_CODEC_ID_GSM_MS,
|
||
13 years ago
|
.priv_data_size = sizeof(LibGSMDecodeContext),
|
||
13 years ago
|
.init = libgsm_decode_init,
|
||
|
.close = libgsm_decode_close,
|
||
14 years ago
|
.decode = libgsm_decode_frame,
|
||
13 years ago
|
.flush = libgsm_flush,
|
||
13 years ago
|
.capabilities = CODEC_CAP_DR1,
|
||
18 years ago
|
};
|
||
13 years ago
|
#endif
|