3 * Copyright (c) 2023 Paul B Mahol
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/channel_layout.h"
23 #include "libavutil/intreadwrite.h"
28 static int sdns_probe(const AVProbeData
*p
)
30 if (AV_RL32(p
->buf
) != MKTAG('S','D','N','S'))
32 if (AV_RB32(p
->buf
+ 8) <= 0)
34 if (AV_RB32(p
->buf
+ 12) <= 0 ||
35 AV_RB32(p
->buf
+ 12) > 128)
37 return AVPROBE_SCORE_MAX
/ 3;
40 static int sdns_read_header(AVFormatContext
*s
)
42 AVIOContext
*pb
= s
->pb
;
43 AVCodecParameters
*par
;
49 st
= avformat_new_stream(s
, NULL
);
51 return AVERROR(ENOMEM
);
54 par
->codec_type
= AVMEDIA_TYPE_AUDIO
;
55 par
->codec_id
= AV_CODEC_ID_XMA1
;
56 par
->sample_rate
= avio_rb32(pb
);
57 channels
= avio_rb32(pb
);
58 if (channels
<= 0 || channels
> 128)
59 return AVERROR_INVALIDDATA
;
60 av_channel_layout_default(&par
->ch_layout
, channels
);
61 if (par
->sample_rate
<= 0)
62 return AVERROR_INVALIDDATA
;
63 par
->block_align
= 2048;
64 if ((ret
= ff_alloc_extradata(par
, 8 + 20 * ((channels
+ 1) / 2))) < 0)
66 memset(par
->extradata
, 0, 28);
67 par
->extradata
[4] = (channels
+ 1) / 2;
68 for (int i
= 0; i
< par
->extradata
[4]; i
++)
69 par
->extradata
[8 + 20 * i
+ 17] = FFMIN(2, channels
- i
* 2);
70 avpriv_set_pts_info(st
, 64, 1, par
->sample_rate
);
71 avio_seek(pb
, 0x1000, SEEK_SET
);
76 static int sdns_read_packet(AVFormatContext
*s
, AVPacket
*pkt
)
82 ret
= av_get_packet(s
->pb
, pkt
, 2048);
83 pkt
->stream_index
= 0;
88 const FFInputFormat ff_sdns_demuxer
= {
90 .p
.long_name
= NULL_IF_CONFIG_SMALL("Xbox SDNS"),
91 .p
.flags
= AVFMT_GENERIC_INDEX
,
92 .p
.extensions
= "sdns",
93 .read_probe
= sdns_probe
,
94 .read_header
= sdns_read_header
,
95 .read_packet
= sdns_read_packet
,