Commit | Line | Data |
---|---|---|
2ba45a60 DM |
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 "internal.h" | |
24 | #include "libavutil/log.h" | |
25 | #include "libavutil/opt.h" | |
26 | ||
27 | typedef struct G729DemuxerContext { | |
28 | AVClass *class; | |
29 | int bit_rate; | |
30 | } G729DemuxerContext; | |
31 | ||
32 | static int g729_read_header(AVFormatContext *s) | |
33 | { | |
34 | AVStream* st; | |
35 | G729DemuxerContext *s1 = s->priv_data; | |
36 | ||
37 | st = avformat_new_stream(s, NULL); | |
38 | if (!st) | |
39 | return AVERROR(ENOMEM); | |
40 | ||
41 | st->codec->codec_type = AVMEDIA_TYPE_AUDIO; | |
42 | st->codec->codec_id = AV_CODEC_ID_G729; | |
43 | st->codec->sample_rate = 8000; | |
44 | st->codec->channels = 1; | |
45 | ||
46 | if (s1 && s1->bit_rate) { | |
47 | s->bit_rate = s1->bit_rate; | |
48 | } | |
49 | ||
50 | if (s->bit_rate == 0) { | |
51 | av_log(s, AV_LOG_DEBUG, "No bitrate specified. Assuming 8000 b/s\n"); | |
52 | s->bit_rate = 8000; | |
53 | } | |
54 | ||
55 | if (s->bit_rate == 6400) { | |
56 | st->codec->block_align = 8; | |
57 | } else if (s->bit_rate == 8000) { | |
58 | st->codec->block_align = 10; | |
59 | } else { | |
60 | 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); | |
61 | return AVERROR_INVALIDDATA; | |
62 | } | |
63 | ||
64 | avpriv_set_pts_info(st, st->codec->block_align << 3, 1, st->codec->sample_rate); | |
65 | return 0; | |
66 | } | |
67 | static int g729_read_packet(AVFormatContext *s, AVPacket *pkt) | |
68 | { | |
69 | int ret; | |
70 | ||
71 | ret = av_get_packet(s->pb, pkt, s->streams[0]->codec->block_align); | |
72 | ||
73 | pkt->stream_index = 0; | |
74 | if (ret < 0) | |
75 | return ret; | |
76 | ||
77 | pkt->dts = pkt->pts = pkt->pos / s->streams[0]->codec->block_align; | |
78 | ||
79 | return ret; | |
80 | } | |
81 | ||
82 | static const AVOption g729_options[] = { | |
83 | { "bit_rate", "", offsetof(G729DemuxerContext, bit_rate), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM }, | |
84 | { NULL }, | |
85 | }; | |
86 | ||
87 | static const AVClass g729_demuxer_class = { | |
88 | .class_name = "g729 demuxer", | |
89 | .item_name = av_default_item_name, | |
90 | .option = g729_options, | |
91 | .version = LIBAVUTIL_VERSION_INT, | |
92 | }; | |
93 | ||
94 | AVInputFormat ff_g729_demuxer = { | |
95 | .name = "g729", | |
96 | .long_name = NULL_IF_CONFIG_SMALL("G.729 raw format demuxer"), | |
97 | .priv_data_size = sizeof(G729DemuxerContext), | |
98 | .read_header = g729_read_header, | |
99 | .read_packet = g729_read_packet, | |
100 | .flags = AVFMT_GENERIC_INDEX, | |
101 | .extensions = "g729", | |
102 | .priv_class = &g729_demuxer_class, | |
103 | }; |