libavcodec/gsmdec.c
Go to the documentation of this file.
00001 /*
00002  * gsm 06.10 decoder
00003  * Copyright (c) 2010 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00027 #include "avcodec.h"
00028 #include "internal.h"
00029 #include "get_bits.h"
00030 #include "msgsmdec.h"
00031 
00032 #include "gsmdec_template.c"
00033 
00034 static av_cold int gsm_init(AVCodecContext *avctx)
00035 {
00036     GSMContext *s = avctx->priv_data;
00037 
00038     avctx->channels = 1;
00039     if (!avctx->sample_rate)
00040         avctx->sample_rate = 8000;
00041     avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00042 
00043     switch (avctx->codec_id) {
00044     case CODEC_ID_GSM:
00045         avctx->frame_size  = GSM_FRAME_SIZE;
00046         avctx->block_align = GSM_BLOCK_SIZE;
00047         break;
00048     case CODEC_ID_GSM_MS:
00049         avctx->frame_size  = 2 * GSM_FRAME_SIZE;
00050         avctx->block_align = GSM_MS_BLOCK_SIZE;
00051     }
00052 
00053     avcodec_get_frame_defaults(&s->frame);
00054     avctx->coded_frame = &s->frame;
00055 
00056     return 0;
00057 }
00058 
00059 static int gsm_decode_frame(AVCodecContext *avctx, void *data,
00060                             int *got_frame_ptr, AVPacket *avpkt)
00061 {
00062     GSMContext *s = avctx->priv_data;
00063     int res;
00064     GetBitContext gb;
00065     const uint8_t *buf = avpkt->data;
00066     int buf_size = avpkt->size;
00067     int16_t *samples;
00068 
00069     if (buf_size < avctx->block_align) {
00070         av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
00071         return AVERROR_INVALIDDATA;
00072     }
00073 
00074     /* get output buffer */
00075     s->frame.nb_samples = avctx->frame_size;
00076     if ((res = ff_get_buffer(avctx, &s->frame)) < 0) {
00077         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00078         return res;
00079     }
00080     samples = (int16_t *)s->frame.data[0];
00081 
00082     switch (avctx->codec_id) {
00083     case CODEC_ID_GSM:
00084         init_get_bits(&gb, buf, buf_size * 8);
00085         if (get_bits(&gb, 4) != 0xd)
00086             av_log(avctx, AV_LOG_WARNING, "Missing GSM magic!\n");
00087         res = gsm_decode_block(avctx, samples, &gb);
00088         if (res < 0)
00089             return res;
00090         break;
00091     case CODEC_ID_GSM_MS:
00092         res = ff_msgsm_decode_block(avctx, samples, buf);
00093         if (res < 0)
00094             return res;
00095     }
00096 
00097     *got_frame_ptr   = 1;
00098     *(AVFrame *)data = s->frame;
00099 
00100     return avctx->block_align;
00101 }
00102 
00103 static void gsm_flush(AVCodecContext *avctx)
00104 {
00105     GSMContext *s = avctx->priv_data;
00106     memset(s, 0, sizeof(*s));
00107 }
00108 
00109 AVCodec ff_gsm_decoder = {
00110     .name           = "gsm",
00111     .type           = AVMEDIA_TYPE_AUDIO,
00112     .id             = CODEC_ID_GSM,
00113     .priv_data_size = sizeof(GSMContext),
00114     .init           = gsm_init,
00115     .decode         = gsm_decode_frame,
00116     .flush          = gsm_flush,
00117     .capabilities   = CODEC_CAP_DR1,
00118     .long_name = NULL_IF_CONFIG_SMALL("GSM"),
00119 };
00120 
00121 AVCodec ff_gsm_ms_decoder = {
00122     .name           = "gsm_ms",
00123     .type           = AVMEDIA_TYPE_AUDIO,
00124     .id             = CODEC_ID_GSM_MS,
00125     .priv_data_size = sizeof(GSMContext),
00126     .init           = gsm_init,
00127     .decode         = gsm_decode_frame,
00128     .flush          = gsm_flush,
00129     .capabilities   = CODEC_CAP_DR1,
00130     .long_name = NULL_IF_CONFIG_SMALL("GSM Microsoft variant"),
00131 };