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