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 * 32;
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 = ret / GSM_BLOCK_SIZE;
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->block_align = GSM_BLOCK_SIZE;
00069 st->codec->bit_rate = GSM_BLOCK_SIZE * 8 * c->sample_rate / GSM_BLOCK_SAMPLES;
00070
00071 avpriv_set_pts_info(st, 64, GSM_BLOCK_SAMPLES, GSM_SAMPLE_RATE);
00072
00073 return 0;
00074 }
00075
00076 static int gsm_read_seek2(AVFormatContext *s, int stream_index, int64_t min_ts,
00077 int64_t ts, int64_t max_ts, int flags)
00078 {
00079 GSMDemuxerContext *c = s->priv_data;
00080
00081
00082 if (!(flags & AVSEEK_FLAG_BYTE)) {
00083 if (stream_index < 0) {
00084 AVRational bitrate_q = { GSM_BLOCK_SAMPLES, c->sample_rate * GSM_BLOCK_SIZE };
00085 ts = av_rescale_q(ts, AV_TIME_BASE_Q, bitrate_q);
00086 min_ts = av_rescale_q(min_ts, AV_TIME_BASE_Q, bitrate_q);
00087 max_ts = av_rescale_q(max_ts, AV_TIME_BASE_Q, bitrate_q);
00088 } else {
00089 ts *= GSM_BLOCK_SIZE;
00090 min_ts *= GSM_BLOCK_SIZE;
00091 max_ts *= GSM_BLOCK_SIZE;
00092 }
00093 }
00094
00095 ts = (ts + GSM_BLOCK_SIZE / 2) / GSM_BLOCK_SIZE * GSM_BLOCK_SIZE;
00096 ts = FFMAX(0, ts);
00097
00098
00099 while (ts < min_ts)
00100 ts += GSM_BLOCK_SIZE;
00101 while (ts > max_ts)
00102 ts -= GSM_BLOCK_SIZE;
00103 if (ts < min_ts || ts > max_ts)
00104 return -1;
00105
00106 return avio_seek(s->pb, ts, SEEK_SET);
00107 }
00108
00109 static const AVOption options[] = {
00110 { "sample_rate", "", offsetof(GSMDemuxerContext, sample_rate),
00111 AV_OPT_TYPE_INT, {.dbl = GSM_SAMPLE_RATE}, 1, INT_MAX / GSM_BLOCK_SIZE,
00112 AV_OPT_FLAG_DECODING_PARAM },
00113 { NULL },
00114 };
00115
00116 static const AVClass class = {
00117 .class_name = "gsm demuxer",
00118 .item_name = av_default_item_name,
00119 .option = options,
00120 .version = LIBAVUTIL_VERSION_INT,
00121 };
00122
00123 AVInputFormat ff_gsm_demuxer = {
00124 .name = "gsm",
00125 .long_name = NULL_IF_CONFIG_SMALL("raw GSM"),
00126 .priv_data_size = sizeof(GSMDemuxerContext),
00127 .read_header = gsm_read_header,
00128 .read_packet = gsm_read_packet,
00129 .read_seek2 = gsm_read_seek2,
00130 .extensions = "gsm",
00131 .value = CODEC_ID_GSM,
00132 .priv_class = &class,
00133 };