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