blob: c6652b9b43cd8fac17c2691e0b350072e5f5b139
1 | /* |
2 | * PVF demuxer |
3 | * Copyright (c) 2012 Paul B Mahol |
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 "libavcodec/internal.h" |
23 | #include "avformat.h" |
24 | #include "internal.h" |
25 | #include "pcm.h" |
26 | |
27 | static int pvf_probe(AVProbeData *p) |
28 | { |
29 | if (!memcmp(p->buf, "PVF1\n", 5)) |
30 | return AVPROBE_SCORE_MAX; |
31 | return 0; |
32 | } |
33 | |
34 | static int pvf_read_header(AVFormatContext *s) |
35 | { |
36 | char buffer[32]; |
37 | AVStream *st; |
38 | int bps, channels, sample_rate; |
39 | |
40 | avio_skip(s->pb, 5); |
41 | ff_get_line(s->pb, buffer, sizeof(buffer)); |
42 | if (sscanf(buffer, "%d %d %d", |
43 | &channels, |
44 | &sample_rate, |
45 | &bps) != 3) |
46 | return AVERROR_INVALIDDATA; |
47 | |
48 | if (channels <= 0 || channels > FF_SANE_NB_CHANNELS || |
49 | bps <= 0 || bps > INT_MAX / FF_SANE_NB_CHANNELS || sample_rate <= 0) |
50 | return AVERROR_INVALIDDATA; |
51 | |
52 | st = avformat_new_stream(s, NULL); |
53 | if (!st) |
54 | return AVERROR(ENOMEM); |
55 | |
56 | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; |
57 | st->codecpar->channels = channels; |
58 | st->codecpar->sample_rate = sample_rate; |
59 | st->codecpar->codec_id = ff_get_pcm_codec_id(bps, 0, 1, 0xFFFF); |
60 | st->codecpar->bits_per_coded_sample = bps; |
61 | st->codecpar->block_align = bps * st->codecpar->channels / 8; |
62 | |
63 | avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate); |
64 | |
65 | return 0; |
66 | } |
67 | |
68 | AVInputFormat ff_pvf_demuxer = { |
69 | .name = "pvf", |
70 | .long_name = NULL_IF_CONFIG_SMALL("PVF (Portable Voice Format)"), |
71 | .read_probe = pvf_probe, |
72 | .read_header = pvf_read_header, |
73 | .read_packet = ff_pcm_read_packet, |
74 | .read_seek = ff_pcm_read_seek, |
75 | .extensions = "pvf", |
76 | .flags = AVFMT_GENERIC_INDEX, |
77 | }; |
78 |