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