00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00028
00029
00030 #include <gsm/gsm.h>
00031
00032 #include "libavutil/channel_layout.h"
00033 #include "libavutil/common.h"
00034 #include "avcodec.h"
00035 #include "internal.h"
00036 #include "gsm.h"
00037
00038 static av_cold int libgsm_encode_close(AVCodecContext *avctx) {
00039 #if FF_API_OLD_ENCODE_AUDIO
00040 av_freep(&avctx->coded_frame);
00041 #endif
00042 gsm_destroy(avctx->priv_data);
00043 avctx->priv_data = NULL;
00044 return 0;
00045 }
00046
00047 static av_cold int libgsm_encode_init(AVCodecContext *avctx) {
00048 if (avctx->channels > 1) {
00049 av_log(avctx, AV_LOG_ERROR, "Mono required for GSM, got %d channels\n",
00050 avctx->channels);
00051 return -1;
00052 }
00053
00054 if (avctx->sample_rate != 8000) {
00055 av_log(avctx, AV_LOG_ERROR, "Sample rate 8000Hz required for GSM, got %dHz\n",
00056 avctx->sample_rate);
00057 if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)
00058 return -1;
00059 }
00060 if (avctx->bit_rate != 13000 &&
00061 avctx->bit_rate != 13200 &&
00062 avctx->bit_rate != 0 ) {
00063 av_log(avctx, AV_LOG_ERROR, "Bitrate 13000bps required for GSM, got %dbps\n",
00064 avctx->bit_rate);
00065 if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)
00066 return -1;
00067 }
00068
00069 avctx->priv_data = gsm_create();
00070 if (!avctx->priv_data)
00071 goto error;
00072
00073 switch(avctx->codec_id) {
00074 case AV_CODEC_ID_GSM:
00075 avctx->frame_size = GSM_FRAME_SIZE;
00076 avctx->block_align = GSM_BLOCK_SIZE;
00077 break;
00078 case AV_CODEC_ID_GSM_MS: {
00079 int one = 1;
00080 gsm_option(avctx->priv_data, GSM_OPT_WAV49, &one);
00081 avctx->frame_size = 2*GSM_FRAME_SIZE;
00082 avctx->block_align = GSM_MS_BLOCK_SIZE;
00083 }
00084 }
00085
00086 #if FF_API_OLD_ENCODE_AUDIO
00087 avctx->coded_frame= avcodec_alloc_frame();
00088 if (!avctx->coded_frame)
00089 goto error;
00090 #endif
00091
00092 return 0;
00093 error:
00094 libgsm_encode_close(avctx);
00095 return -1;
00096 }
00097
00098 static int libgsm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
00099 const AVFrame *frame, int *got_packet_ptr)
00100 {
00101 int ret;
00102 gsm_signal *samples = (gsm_signal *)frame->data[0];
00103 struct gsm_state *state = avctx->priv_data;
00104
00105 if ((ret = ff_alloc_packet2(avctx, avpkt, avctx->block_align)))
00106 return ret;
00107
00108 switch(avctx->codec_id) {
00109 case AV_CODEC_ID_GSM:
00110 gsm_encode(state, samples, avpkt->data);
00111 break;
00112 case AV_CODEC_ID_GSM_MS:
00113 gsm_encode(state, samples, avpkt->data);
00114 gsm_encode(state, samples + GSM_FRAME_SIZE, avpkt->data + 32);
00115 }
00116
00117 *got_packet_ptr = 1;
00118 return 0;
00119 }
00120
00121
00122 #if CONFIG_LIBGSM_ENCODER
00123 AVCodec ff_libgsm_encoder = {
00124 .name = "libgsm",
00125 .type = AVMEDIA_TYPE_AUDIO,
00126 .id = AV_CODEC_ID_GSM,
00127 .init = libgsm_encode_init,
00128 .encode2 = libgsm_encode_frame,
00129 .close = libgsm_encode_close,
00130 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
00131 AV_SAMPLE_FMT_NONE },
00132 .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM"),
00133 };
00134 #endif
00135 #if CONFIG_LIBGSM_MS_ENCODER
00136 AVCodec ff_libgsm_ms_encoder = {
00137 .name = "libgsm_ms",
00138 .type = AVMEDIA_TYPE_AUDIO,
00139 .id = AV_CODEC_ID_GSM_MS,
00140 .init = libgsm_encode_init,
00141 .encode2 = libgsm_encode_frame,
00142 .close = libgsm_encode_close,
00143 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
00144 AV_SAMPLE_FMT_NONE },
00145 .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),
00146 };
00147 #endif
00148
00149 typedef struct LibGSMDecodeContext {
00150 AVFrame frame;
00151 struct gsm_state *state;
00152 } LibGSMDecodeContext;
00153
00154 static av_cold int libgsm_decode_init(AVCodecContext *avctx) {
00155 LibGSMDecodeContext *s = avctx->priv_data;
00156
00157 avctx->channels = 1;
00158 avctx->channel_layout = AV_CH_LAYOUT_MONO;
00159 if (!avctx->sample_rate)
00160 avctx->sample_rate = 8000;
00161 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00162
00163 s->state = gsm_create();
00164
00165 switch(avctx->codec_id) {
00166 case AV_CODEC_ID_GSM:
00167 avctx->frame_size = GSM_FRAME_SIZE;
00168 avctx->block_align = GSM_BLOCK_SIZE;
00169 break;
00170 case AV_CODEC_ID_GSM_MS: {
00171 int one = 1;
00172 gsm_option(s->state, GSM_OPT_WAV49, &one);
00173 avctx->frame_size = 2 * GSM_FRAME_SIZE;
00174 avctx->block_align = GSM_MS_BLOCK_SIZE;
00175 }
00176 }
00177
00178 avcodec_get_frame_defaults(&s->frame);
00179 avctx->coded_frame = &s->frame;
00180
00181 return 0;
00182 }
00183
00184 static av_cold int libgsm_decode_close(AVCodecContext *avctx) {
00185 LibGSMDecodeContext *s = avctx->priv_data;
00186
00187 gsm_destroy(s->state);
00188 s->state = NULL;
00189 return 0;
00190 }
00191
00192 static int libgsm_decode_frame(AVCodecContext *avctx, void *data,
00193 int *got_frame_ptr, AVPacket *avpkt)
00194 {
00195 int i, ret;
00196 LibGSMDecodeContext *s = avctx->priv_data;
00197 uint8_t *buf = avpkt->data;
00198 int buf_size = avpkt->size;
00199 int16_t *samples;
00200
00201 if (buf_size < avctx->block_align) {
00202 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
00203 return AVERROR_INVALIDDATA;
00204 }
00205
00206
00207 s->frame.nb_samples = avctx->frame_size;
00208 if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
00209 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00210 return ret;
00211 }
00212 samples = (int16_t *)s->frame.data[0];
00213
00214 for (i = 0; i < avctx->frame_size / GSM_FRAME_SIZE; i++) {
00215 if ((ret = gsm_decode(s->state, buf, samples)) < 0)
00216 return -1;
00217 buf += GSM_BLOCK_SIZE;
00218 samples += GSM_FRAME_SIZE;
00219 }
00220
00221 *got_frame_ptr = 1;
00222 *(AVFrame *)data = s->frame;
00223
00224 return avctx->block_align;
00225 }
00226
00227 static void libgsm_flush(AVCodecContext *avctx) {
00228 LibGSMDecodeContext *s = avctx->priv_data;
00229 int one = 1;
00230
00231 gsm_destroy(s->state);
00232 s->state = gsm_create();
00233 if (avctx->codec_id == AV_CODEC_ID_GSM_MS)
00234 gsm_option(s->state, GSM_OPT_WAV49, &one);
00235 }
00236
00237 #if CONFIG_LIBGSM_DECODER
00238 AVCodec ff_libgsm_decoder = {
00239 .name = "libgsm",
00240 .type = AVMEDIA_TYPE_AUDIO,
00241 .id = AV_CODEC_ID_GSM,
00242 .priv_data_size = sizeof(LibGSMDecodeContext),
00243 .init = libgsm_decode_init,
00244 .close = libgsm_decode_close,
00245 .decode = libgsm_decode_frame,
00246 .flush = libgsm_flush,
00247 .capabilities = CODEC_CAP_DR1,
00248 .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM"),
00249 };
00250 #endif
00251 #if CONFIG_LIBGSM_MS_DECODER
00252 AVCodec ff_libgsm_ms_decoder = {
00253 .name = "libgsm_ms",
00254 .type = AVMEDIA_TYPE_AUDIO,
00255 .id = AV_CODEC_ID_GSM_MS,
00256 .priv_data_size = sizeof(LibGSMDecodeContext),
00257 .init = libgsm_decode_init,
00258 .close = libgsm_decode_close,
00259 .decode = libgsm_decode_frame,
00260 .flush = libgsm_flush,
00261 .capabilities = CODEC_CAP_DR1,
00262 .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),
00263 };
00264 #endif