00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "libavutil/channel_layout.h"
00028 #include "avcodec.h"
00029 #include "get_bits.h"
00030 #include "internal.h"
00031 #include "msgsmdec.h"
00032
00033 #include "gsmdec_template.c"
00034
00035 static av_cold int gsm_init(AVCodecContext *avctx)
00036 {
00037 GSMContext *s = avctx->priv_data;
00038
00039 avctx->channels = 1;
00040 avctx->channel_layout = AV_CH_LAYOUT_MONO;
00041 if (!avctx->sample_rate)
00042 avctx->sample_rate = 8000;
00043 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00044
00045 switch (avctx->codec_id) {
00046 case AV_CODEC_ID_GSM:
00047 avctx->frame_size = GSM_FRAME_SIZE;
00048 avctx->block_align = GSM_BLOCK_SIZE;
00049 break;
00050 case AV_CODEC_ID_GSM_MS:
00051 avctx->frame_size = 2 * GSM_FRAME_SIZE;
00052 avctx->block_align = GSM_MS_BLOCK_SIZE;
00053 }
00054
00055 avcodec_get_frame_defaults(&s->frame);
00056 avctx->coded_frame = &s->frame;
00057
00058 return 0;
00059 }
00060
00061 static int gsm_decode_frame(AVCodecContext *avctx, void *data,
00062 int *got_frame_ptr, AVPacket *avpkt)
00063 {
00064 GSMContext *s = avctx->priv_data;
00065 int res;
00066 GetBitContext gb;
00067 const uint8_t *buf = avpkt->data;
00068 int buf_size = avpkt->size;
00069 int16_t *samples;
00070
00071 if (buf_size < avctx->block_align) {
00072 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
00073 return AVERROR_INVALIDDATA;
00074 }
00075
00076
00077 s->frame.nb_samples = avctx->frame_size;
00078 if ((res = ff_get_buffer(avctx, &s->frame)) < 0) {
00079 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00080 return res;
00081 }
00082 samples = (int16_t *)s->frame.data[0];
00083
00084 switch (avctx->codec_id) {
00085 case AV_CODEC_ID_GSM:
00086 init_get_bits(&gb, buf, buf_size * 8);
00087 if (get_bits(&gb, 4) != 0xd)
00088 av_log(avctx, AV_LOG_WARNING, "Missing GSM magic!\n");
00089 res = gsm_decode_block(avctx, samples, &gb);
00090 if (res < 0)
00091 return res;
00092 break;
00093 case AV_CODEC_ID_GSM_MS:
00094 res = ff_msgsm_decode_block(avctx, samples, buf);
00095 if (res < 0)
00096 return res;
00097 }
00098
00099 *got_frame_ptr = 1;
00100 *(AVFrame *)data = s->frame;
00101
00102 return avctx->block_align;
00103 }
00104
00105 static void gsm_flush(AVCodecContext *avctx)
00106 {
00107 GSMContext *s = avctx->priv_data;
00108 memset(s, 0, sizeof(*s));
00109 }
00110
00111 #if CONFIG_GSM_DECODER
00112 AVCodec ff_gsm_decoder = {
00113 .name = "gsm",
00114 .type = AVMEDIA_TYPE_AUDIO,
00115 .id = AV_CODEC_ID_GSM,
00116 .priv_data_size = sizeof(GSMContext),
00117 .init = gsm_init,
00118 .decode = gsm_decode_frame,
00119 .flush = gsm_flush,
00120 .capabilities = CODEC_CAP_DR1,
00121 .long_name = NULL_IF_CONFIG_SMALL("GSM"),
00122 };
00123 #endif
00124 #if CONFIG_GSM_MS_DECODER
00125 AVCodec ff_gsm_ms_decoder = {
00126 .name = "gsm_ms",
00127 .type = AVMEDIA_TYPE_AUDIO,
00128 .id = AV_CODEC_ID_GSM_MS,
00129 .priv_data_size = sizeof(GSMContext),
00130 .init = gsm_init,
00131 .decode = gsm_decode_frame,
00132 .flush = gsm_flush,
00133 .capabilities = CODEC_CAP_DR1,
00134 .long_name = NULL_IF_CONFIG_SMALL("GSM Microsoft variant"),
00135 };
00136 #endif