annotate ffmpeg/libavcodec/gsmdec.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * gsm 06.10 decoder
yading@10 3 * Copyright (c) 2010 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
yading@10 4 *
yading@10 5 * This file is part of FFmpeg.
yading@10 6 *
yading@10 7 * FFmpeg is free software; you can redistribute it and/or
yading@10 8 * modify it under the terms of the GNU Lesser General Public
yading@10 9 * License as published by the Free Software Foundation; either
yading@10 10 * version 2.1 of the License, or (at your option) any later version.
yading@10 11 *
yading@10 12 * FFmpeg is distributed in the hope that it will be useful,
yading@10 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 15 * Lesser General Public License for more details.
yading@10 16 *
yading@10 17 * You should have received a copy of the GNU Lesser General Public
yading@10 18 * License along with FFmpeg; if not, write to the Free Software
yading@10 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 20 */
yading@10 21
yading@10 22 /**
yading@10 23 * @file
yading@10 24 * GSM decoder
yading@10 25 */
yading@10 26
yading@10 27 #include "libavutil/channel_layout.h"
yading@10 28 #include "avcodec.h"
yading@10 29 #include "get_bits.h"
yading@10 30 #include "internal.h"
yading@10 31 #include "msgsmdec.h"
yading@10 32
yading@10 33 #include "gsmdec_template.c"
yading@10 34
yading@10 35 static av_cold int gsm_init(AVCodecContext *avctx)
yading@10 36 {
yading@10 37 avctx->channels = 1;
yading@10 38 avctx->channel_layout = AV_CH_LAYOUT_MONO;
yading@10 39 if (!avctx->sample_rate)
yading@10 40 avctx->sample_rate = 8000;
yading@10 41 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
yading@10 42
yading@10 43 switch (avctx->codec_id) {
yading@10 44 case AV_CODEC_ID_GSM:
yading@10 45 avctx->frame_size = GSM_FRAME_SIZE;
yading@10 46 avctx->block_align = GSM_BLOCK_SIZE;
yading@10 47 break;
yading@10 48 case AV_CODEC_ID_GSM_MS:
yading@10 49 avctx->frame_size = 2 * GSM_FRAME_SIZE;
yading@10 50 avctx->block_align = GSM_MS_BLOCK_SIZE;
yading@10 51 }
yading@10 52
yading@10 53 return 0;
yading@10 54 }
yading@10 55
yading@10 56 static int gsm_decode_frame(AVCodecContext *avctx, void *data,
yading@10 57 int *got_frame_ptr, AVPacket *avpkt)
yading@10 58 {
yading@10 59 AVFrame *frame = data;
yading@10 60 int res;
yading@10 61 GetBitContext gb;
yading@10 62 const uint8_t *buf = avpkt->data;
yading@10 63 int buf_size = avpkt->size;
yading@10 64 int16_t *samples;
yading@10 65
yading@10 66 if (buf_size < avctx->block_align) {
yading@10 67 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
yading@10 68 return AVERROR_INVALIDDATA;
yading@10 69 }
yading@10 70
yading@10 71 /* get output buffer */
yading@10 72 frame->nb_samples = avctx->frame_size;
yading@10 73 if ((res = ff_get_buffer(avctx, frame, 0)) < 0)
yading@10 74 return res;
yading@10 75 samples = (int16_t *)frame->data[0];
yading@10 76
yading@10 77 switch (avctx->codec_id) {
yading@10 78 case AV_CODEC_ID_GSM:
yading@10 79 init_get_bits(&gb, buf, buf_size * 8);
yading@10 80 if (get_bits(&gb, 4) != 0xd)
yading@10 81 av_log(avctx, AV_LOG_WARNING, "Missing GSM magic!\n");
yading@10 82 res = gsm_decode_block(avctx, samples, &gb);
yading@10 83 if (res < 0)
yading@10 84 return res;
yading@10 85 break;
yading@10 86 case AV_CODEC_ID_GSM_MS:
yading@10 87 res = ff_msgsm_decode_block(avctx, samples, buf);
yading@10 88 if (res < 0)
yading@10 89 return res;
yading@10 90 }
yading@10 91
yading@10 92 *got_frame_ptr = 1;
yading@10 93
yading@10 94 return avctx->block_align;
yading@10 95 }
yading@10 96
yading@10 97 static void gsm_flush(AVCodecContext *avctx)
yading@10 98 {
yading@10 99 GSMContext *s = avctx->priv_data;
yading@10 100 memset(s, 0, sizeof(*s));
yading@10 101 }
yading@10 102
yading@10 103 #if CONFIG_GSM_DECODER
yading@10 104 AVCodec ff_gsm_decoder = {
yading@10 105 .name = "gsm",
yading@10 106 .type = AVMEDIA_TYPE_AUDIO,
yading@10 107 .id = AV_CODEC_ID_GSM,
yading@10 108 .priv_data_size = sizeof(GSMContext),
yading@10 109 .init = gsm_init,
yading@10 110 .decode = gsm_decode_frame,
yading@10 111 .flush = gsm_flush,
yading@10 112 .capabilities = CODEC_CAP_DR1,
yading@10 113 .long_name = NULL_IF_CONFIG_SMALL("GSM"),
yading@10 114 };
yading@10 115 #endif
yading@10 116 #if CONFIG_GSM_MS_DECODER
yading@10 117 AVCodec ff_gsm_ms_decoder = {
yading@10 118 .name = "gsm_ms",
yading@10 119 .type = AVMEDIA_TYPE_AUDIO,
yading@10 120 .id = AV_CODEC_ID_GSM_MS,
yading@10 121 .priv_data_size = sizeof(GSMContext),
yading@10 122 .init = gsm_init,
yading@10 123 .decode = gsm_decode_frame,
yading@10 124 .flush = gsm_flush,
yading@10 125 .capabilities = CODEC_CAP_DR1,
yading@10 126 .long_name = NULL_IF_CONFIG_SMALL("GSM Microsoft variant"),
yading@10 127 };
yading@10 128 #endif