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