]> git.sesse.net Git - ffmpeg/blob - libavformat/g729dec.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavformat / g729dec.c
1 /*
2  * G.729 raw format demuxer
3  * Copyright (c) 2011 Vladimir Voroshilov
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "avformat.h"
23 #include "libavutil/log.h"
24 #include "libavutil/opt.h"
25
26 typedef struct G729DemuxerContext {
27     AVClass *class;
28     int bit_rate;
29 } G729DemuxerContext;
30
31 static int g729_read_header(AVFormatContext *s, AVFormatParameters *ap)
32 {
33     AVStream* st;
34     G729DemuxerContext *s1 = s->priv_data;
35
36     st = avformat_new_stream(s, NULL);
37     if (!st)
38         return AVERROR(ENOMEM);
39
40     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
41     st->codec->codec_id = CODEC_ID_G729;
42     st->codec->sample_rate = 8000;
43     st->codec->channels = 1;
44
45     if (s1 && s1->bit_rate) {
46         s->bit_rate = s1->bit_rate;
47     }
48
49     if (s->bit_rate == 0) {
50         av_log(s, AV_LOG_DEBUG, "No bitrate specified. Assuming 8000 b/s\n");
51         s->bit_rate = 8000;
52     }
53
54     if (s->bit_rate == 6400) {
55         st->codec->block_align = 8;
56     } else if (s->bit_rate == 8000) {
57         st->codec->block_align = 10;
58     } else {
59         av_log(s, AV_LOG_ERROR, "Only 8000 b/s and 6400 b/s bitrates are supported. Provided: %d b/s\n", s->bit_rate);
60         return AVERROR_INVALIDDATA;
61     }
62
63     av_set_pts_info(st, st->codec->block_align << 3, 1, st->codec->sample_rate);
64     return 0;
65 }
66 static int g729_read_packet(AVFormatContext *s, AVPacket *pkt)
67 {
68     int ret;
69
70     ret = av_get_packet(s->pb, pkt, s->streams[0]->codec->block_align);
71
72     pkt->stream_index = 0;
73     if (ret < 0)
74         return ret;
75
76     pkt->dts = pkt->pts = pkt->pos / s->streams[0]->codec->block_align;
77
78     return ret;
79 }
80
81 static const AVOption g729_options[] = {
82     { "bit_rate", "", offsetof(G729DemuxerContext, bit_rate), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
83     { NULL },
84 };
85
86 static const AVClass g729_demuxer_class = {
87     .class_name = "g729 demuxer",
88     .item_name  = av_default_item_name,
89     .option     = g729_options,
90     .version    = LIBAVUTIL_VERSION_INT,
91 };
92
93 AVInputFormat ff_g729_demuxer = {
94     .name           = "g729",
95     .long_name      = NULL_IF_CONFIG_SMALL("G.729 raw format demuxer"),
96     .priv_data_size = sizeof(G729DemuxerContext),
97     .read_header    = g729_read_header,
98     .read_packet    = g729_read_packet,
99     .flags          = AVFMT_GENERIC_INDEX,
100     .extensions     = "g729",
101     .priv_class     = &g729_demuxer_class,
102 };