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