00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/mathematics.h"
00023 #include "libavutil/opt.h"
00024 #include "avformat.h"
00025 #include "internal.h"
00026
00027 #define GSM_BLOCK_SIZE 33
00028 #define GSM_BLOCK_SAMPLES 160
00029 #define GSM_SAMPLE_RATE 8000
00030
00031 typedef struct {
00032 AVClass *class;
00033 int sample_rate;
00034 } GSMDemuxerContext;
00035
00036 static int gsm_read_packet(AVFormatContext *s, AVPacket *pkt)
00037 {
00038 int ret, size;
00039
00040 size = GSM_BLOCK_SIZE;
00041
00042 pkt->pos = avio_tell(s->pb);
00043 pkt->stream_index = 0;
00044
00045 ret = av_get_packet(s->pb, pkt, size);
00046 if (ret < GSM_BLOCK_SIZE) {
00047 av_free_packet(pkt);
00048 return ret < 0 ? ret : AVERROR(EIO);
00049 }
00050 pkt->size = ret;
00051 pkt->duration = 1;
00052 pkt->pts = pkt->pos / GSM_BLOCK_SIZE;
00053
00054 return 0;
00055 }
00056
00057 static int gsm_read_header(AVFormatContext *s, AVFormatParameters *ap)
00058 {
00059 GSMDemuxerContext *c = s->priv_data;
00060 AVStream *st = avformat_new_stream(s, NULL);
00061 if (!st)
00062 return AVERROR(ENOMEM);
00063
00064 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00065 st->codec->codec_id = s->iformat->value;
00066 st->codec->channels = 1;
00067 st->codec->sample_rate = c->sample_rate;
00068 st->codec->bit_rate = GSM_BLOCK_SIZE * 8 * c->sample_rate / GSM_BLOCK_SAMPLES;
00069
00070 avpriv_set_pts_info(st, 64, GSM_BLOCK_SAMPLES, GSM_SAMPLE_RATE);
00071
00072 return 0;
00073 }
00074
00075 static const AVOption options[] = {
00076 { "sample_rate", "", offsetof(GSMDemuxerContext, sample_rate),
00077 AV_OPT_TYPE_INT, {.dbl = GSM_SAMPLE_RATE}, 1, INT_MAX / GSM_BLOCK_SIZE,
00078 AV_OPT_FLAG_DECODING_PARAM },
00079 { NULL },
00080 };
00081
00082 static const AVClass class = {
00083 .class_name = "gsm demuxer",
00084 .item_name = av_default_item_name,
00085 .option = options,
00086 .version = LIBAVUTIL_VERSION_INT,
00087 };
00088
00089 AVInputFormat ff_gsm_demuxer = {
00090 .name = "gsm",
00091 .long_name = NULL_IF_CONFIG_SMALL("raw GSM"),
00092 .priv_data_size = sizeof(GSMDemuxerContext),
00093 .read_header = gsm_read_header,
00094 .read_packet = gsm_read_packet,
00095 .flags = AVFMT_GENERIC_INDEX,
00096 .extensions = "gsm",
00097 .value = CODEC_ID_GSM,
00098 .priv_class = &class,
00099 };