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