Libav
libgsmdec.c
Go to the documentation of this file.
1 /*
2  * Interface to libgsm for GSM decoding
3  * Copyright (c) 2005 Alban Bedel <albeu@free.fr>
4  * Copyright (c) 2006, 2007 Michel Bardiaux <mbardiaux@mediaxim.be>
5  *
6  * This file is part of Libav.
7  *
8  * Libav is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * Libav is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
28 // The idiosyncrasies of GSM-in-WAV are explained at http://kbs.cs.tu-berlin.de/~jutta/toast.html
29 
30 #include "config.h"
31 #if HAVE_GSM_H
32 #include <gsm.h>
33 #else
34 #include <gsm/gsm.h>
35 #endif
36 
38 #include "libavutil/common.h"
39 
40 #include "avcodec.h"
41 #include "internal.h"
42 #include "gsm.h"
43 
44 typedef struct LibGSMDecodeContext {
45  struct gsm_state *state;
47 
49  LibGSMDecodeContext *s = avctx->priv_data;
50 
51  avctx->channels = 1;
53  avctx->sample_rate = 8000;
55 
56  s->state = gsm_create();
57 
58  switch(avctx->codec_id) {
59  case AV_CODEC_ID_GSM:
60  avctx->frame_size = GSM_FRAME_SIZE;
61  avctx->block_align = GSM_BLOCK_SIZE;
62  break;
63  case AV_CODEC_ID_GSM_MS: {
64  int one = 1;
65  gsm_option(s->state, GSM_OPT_WAV49, &one);
66  avctx->frame_size = 2 * GSM_FRAME_SIZE;
68  }
69  }
70 
71  return 0;
72 }
73 
75  LibGSMDecodeContext *s = avctx->priv_data;
76 
77  gsm_destroy(s->state);
78  s->state = NULL;
79  return 0;
80 }
81 
82 static int libgsm_decode_frame(AVCodecContext *avctx, void *data,
83  int *got_frame_ptr, AVPacket *avpkt)
84 {
85  int i, ret;
86  LibGSMDecodeContext *s = avctx->priv_data;
87  AVFrame *frame = data;
88  uint8_t *buf = avpkt->data;
89  int buf_size = avpkt->size;
90  int16_t *samples;
91 
92  if (buf_size < avctx->block_align) {
93  av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
94  return AVERROR_INVALIDDATA;
95  }
96 
97  /* get output buffer */
98  frame->nb_samples = avctx->frame_size;
99  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
100  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
101  return ret;
102  }
103  samples = (int16_t *)frame->data[0];
104 
105  for (i = 0; i < avctx->frame_size / GSM_FRAME_SIZE; i++) {
106  if ((ret = gsm_decode(s->state, buf, samples)) < 0)
107  return -1;
108  buf += GSM_BLOCK_SIZE;
109  samples += GSM_FRAME_SIZE;
110  }
111 
112  *got_frame_ptr = 1;
113 
114  return avctx->block_align;
115 }
116 
117 static void libgsm_flush(AVCodecContext *avctx) {
118  LibGSMDecodeContext *s = avctx->priv_data;
119  int one = 1;
120 
121  gsm_destroy(s->state);
122  s->state = gsm_create();
123  if (avctx->codec_id == AV_CODEC_ID_GSM_MS)
124  gsm_option(s->state, GSM_OPT_WAV49, &one);
125 }
126 
128  .name = "libgsm",
129  .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM"),
130  .type = AVMEDIA_TYPE_AUDIO,
131  .id = AV_CODEC_ID_GSM,
132  .priv_data_size = sizeof(LibGSMDecodeContext),
136  .flush = libgsm_flush,
137  .capabilities = CODEC_CAP_DR1,
138 };
139 
141  .name = "libgsm_ms",
142  .long_name = NULL_IF_CONFIG_SMALL("libgsm GSM Microsoft variant"),
143  .type = AVMEDIA_TYPE_AUDIO,
144  .id = AV_CODEC_ID_GSM_MS,
145  .priv_data_size = sizeof(LibGSMDecodeContext),
149  .flush = libgsm_flush,
150  .capabilities = CODEC_CAP_DR1,
151 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
#define GSM_FRAME_SIZE
Definition: gsm.h:30
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
int size
Definition: avcodec.h:968
static int libgsm_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: libgsmdec.c:82
AVCodec.
Definition: avcodec.h:2790
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:1822
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:275
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1793
uint8_t
#define av_cold
Definition: attributes.h:66
#define GSM_MS_BLOCK_SIZE
Definition: gsm.h:26
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:684
#define GSM_BLOCK_SIZE
Definition: gsm.h:25
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:967
static av_cold int libgsm_decode_init(AVCodecContext *avctx)
Definition: libgsmdec.c:48
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:168
const char * name
Name of the codec implementation.
Definition: avcodec.h:2797
static void libgsm_flush(AVCodecContext *avctx)
Definition: libgsmdec.c:117
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1846
AVCodec ff_libgsm_ms_decoder
Definition: libgsmdec.c:140
audio channel layout utility functions
AVCodec ff_libgsm_decoder
Definition: libgsmdec.c:127
struct gsm_state * state
Definition: libgsmdec.c:45
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1805
NULL
Definition: eval.c:55
Libavcodec external API header.
enum AVCodecID codec_id
Definition: avcodec.h:1061
int sample_rate
samples per second
Definition: avcodec.h:1785
main external API structure.
Definition: avcodec.h:1044
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:490
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:603
static av_cold int libgsm_decode_close(AVCodecContext *avctx)
Definition: libgsmdec.c:74
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
common internal api header.
common internal and external API header
static av_cold void flush(AVCodecContext *avctx)
Flush (reset) the frame ID after seeking.
Definition: alsdec.c:1771
signed 16 bits
Definition: samplefmt.h:64
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:499
void * priv_data
Definition: avcodec.h:1086
as in Berlin toast format
Definition: avcodec.h:396
int channels
number of audio channels
Definition: avcodec.h:1786
#define AV_CH_LAYOUT_MONO
This structure stores compressed data.
Definition: avcodec.h:944
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:179
for(j=16;j >0;--j)