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->duration = 1;
00051 pkt->pts = pkt->pos / GSM_BLOCK_SIZE;
00052
00053 return 0;
00054 }
00055
00056 static int gsm_read_header(AVFormatContext *s)
00057 {
00058 GSMDemuxerContext *c = s->priv_data;
00059 AVStream *st = avformat_new_stream(s, NULL);
00060 if (!st)
00061 return AVERROR(ENOMEM);
00062
00063 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00064 st->codec->codec_id = s->iformat->raw_codec_id;
00065 st->codec->channels = 1;
00066 st->codec->sample_rate = c->sample_rate;
00067 st->codec->bit_rate = GSM_BLOCK_SIZE * 8 * c->sample_rate / GSM_BLOCK_SAMPLES;
00068
00069 avpriv_set_pts_info(st, 64, GSM_BLOCK_SAMPLES, GSM_SAMPLE_RATE);
00070
00071 return 0;
00072 }
00073
00074 static const AVOption options[] = {
00075 { "sample_rate", "", offsetof(GSMDemuxerContext, sample_rate),
00076 AV_OPT_TYPE_INT, {.dbl = GSM_SAMPLE_RATE}, 1, INT_MAX / GSM_BLOCK_SIZE,
00077 AV_OPT_FLAG_DECODING_PARAM },
00078 { NULL },
00079 };
00080
00081 static const AVClass class = {
00082 .class_name = "gsm demuxer",
00083 .item_name = av_default_item_name,
00084 .option = options,
00085 .version = LIBAVUTIL_VERSION_INT,
00086 };
00087
00088 AVInputFormat ff_gsm_demuxer = {
00089 .name = "gsm",
00090 .long_name = NULL_IF_CONFIG_SMALL("raw GSM"),
00091 .priv_data_size = sizeof(GSMDemuxerContext),
00092 .read_header = gsm_read_header,
00093 .read_packet = gsm_read_packet,
00094 .flags = AVFMT_GENERIC_INDEX,
00095 .extensions = "gsm",
00096 .raw_codec_id = CODEC_ID_GSM,
00097 .priv_class = &class,
00098 };