1 /*
2  * RAW PCM demuxers
3  * Copyright (c) 2002 Fabrice Bellard
4  * Copyright (c) 2022 Jack Bruienne
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include "libavutil/avstring.h"
24 #include "avformat.h"
25 #include "internal.h"
26 #include "pcm.h"
27 #include "libavutil/log.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/avassert.h"
30 
31 typedef struct DFPWMAudioDemuxerContext {
32     AVClass *class;
33     int sample_rate;
34 #if FF_API_OLD_CHANNEL_LAYOUT
35     int channels;
36 #endif
37     AVChannelLayout ch_layout;
38 } DFPWMAudioDemuxerContext;
39 
dfpwm_read_header(AVFormatContext *s)40 static int dfpwm_read_header(AVFormatContext *s)
41 {
42     DFPWMAudioDemuxerContext *s1 = s->priv_data;
43     AVCodecParameters *par;
44     AVStream *st;
45     int ret;
46 
47     st = avformat_new_stream(s, NULL);
48     if (!st)
49         return AVERROR(ENOMEM);
50     par = st->codecpar;
51 
52     par->codec_type  = AVMEDIA_TYPE_AUDIO;
53     par->codec_id    = s->iformat->raw_codec_id;
54     par->sample_rate = s1->sample_rate;
55 #if FF_API_OLD_CHANNEL_LAYOUT
56     if (s1->ch_layout.nb_channels) {
57 #endif
58     ret = av_channel_layout_copy(&par->ch_layout, &s1->ch_layout);
59     if (ret < 0)
60         return ret;
61 #if FF_API_OLD_CHANNEL_LAYOUT
62     } else
63         par->ch_layout.nb_channels = s1->channels;
64 #endif
65     par->bits_per_coded_sample = 1;
66     par->block_align = 1;
67 
68     avpriv_set_pts_info(st, 64, 1, par->sample_rate);
69     return 0;
70 }
71 
72 static const AVOption dfpwm_options[] = {
73     { "sample_rate", "", offsetof(DFPWMAudioDemuxerContext, sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
74 #if FF_API_OLD_CHANNEL_LAYOUT
75     { "channels",    "", offsetof(DFPWMAudioDemuxerContext, channels),    AV_OPT_TYPE_INT, {.i64 = 1}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_DEPRECATED },
76     { "ch_layout",   "", offsetof(DFPWMAudioDemuxerContext, ch_layout),   AV_OPT_TYPE_CHLAYOUT, {.str = NULL}, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
77 #else
78     { "ch_layout",   "", offsetof(DFPWMAudioDemuxerContext, ch_layout),   AV_OPT_TYPE_CHLAYOUT, {.str = "mono"}, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
79 #endif
80     { NULL },
81 };
82 static const AVClass dfpwm_demuxer_class = {
83     .class_name = "dfpwm demuxer",
84     .item_name  = av_default_item_name,
85     .option     = dfpwm_options,
86     .version    = LIBAVUTIL_VERSION_INT,
87 };
88 
89 const AVInputFormat ff_dfpwm_demuxer = {
90     .name           = "dfpwm",
91     .long_name      = NULL_IF_CONFIG_SMALL("raw DFPWM1a"),
92     .priv_data_size = sizeof(DFPWMAudioDemuxerContext),
93     .read_header    = dfpwm_read_header,
94     .read_packet    = ff_pcm_read_packet,
95     .read_seek      = ff_pcm_read_seek,
96     .flags          = AVFMT_GENERIC_INDEX,
97     .extensions     = "dfpwm",
98     .raw_codec_id   = AV_CODEC_ID_DFPWM,
99     .priv_class     = &dfpwm_demuxer_class,
100 };
101