2 * MD STUDIO audio demuxer
4 * Copyright (c) 2009 Benjamin Larsson
6 * This file is part of FFmpeg.
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.
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.
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
23 #include "libavutil/channel_layout.h"
24 #include "libavutil/intreadwrite.h"
26 #include "avio_internal.h"
31 #define AT1_SU_SIZE 212
33 static int aea_read_probe(const AVProbeData
*p
)
35 if (p
->buf_size
<= 2048+AT1_SU_SIZE
)
38 /* Magic is '00 08 00 00' in little-endian*/
39 if (AV_RL32(p
->buf
)==0x800) {
40 int ch
, block_size
, score
= 0;
43 if (ch
!= 1 && ch
!= 2)
46 block_size
= ch
* AT1_SU_SIZE
;
47 /* Check so that the redundant bsm bytes and info bytes are valid
48 * the block size mode bytes have to be the same
49 * the info bytes have to be the same
51 for (int i
= 2048 + block_size
; i
+ block_size
<= p
->buf_size
; i
+= block_size
) {
52 if (AV_RN16(p
->buf
+i
) != AV_RN16(p
->buf
+i
+AT1_SU_SIZE
))
56 return FFMIN(AVPROBE_SCORE_MAX
/ 4 + score
, AVPROBE_SCORE_MAX
);
61 static int aea_read_header(AVFormatContext
*s
)
63 AVStream
*st
= avformat_new_stream(s
, NULL
);
67 return AVERROR(ENOMEM
);
69 /* Read the title, parse the number of channels and skip to pos 2048(0x800) */
70 avio_rl32(s
->pb
); // magic
71 ret
= ffio_read_size(s
->pb
, title
, sizeof(title
) - 1);
74 title
[sizeof(title
) - 1] = '\0';
76 av_dict_set(&st
->metadata
, "title", title
, 0);
77 avio_rl32(s
->pb
); // Block count
78 channels
= avio_r8(s
->pb
);
79 avio_skip(s
->pb
, 1783);
82 st
->codecpar
->codec_type
= AVMEDIA_TYPE_AUDIO
;
83 st
->codecpar
->codec_id
= AV_CODEC_ID_ATRAC1
;
84 st
->codecpar
->sample_rate
= 44100;
85 st
->codecpar
->bit_rate
= 146000 * channels
;
87 if (channels
< 1 || channels
> 8) {
88 av_log(s
, AV_LOG_ERROR
, "Channels %d not supported!\n", channels
);
89 return AVERROR_INVALIDDATA
;
92 av_channel_layout_default(&st
->codecpar
->ch_layout
, channels
);
94 st
->codecpar
->block_align
= AT1_SU_SIZE
* st
->codecpar
->ch_layout
.nb_channels
;
95 avpriv_set_pts_info(st
, 64, 1, 44100);
99 static int aea_read_packet(AVFormatContext
*s
, AVPacket
*pkt
)
101 return av_get_packet(s
->pb
, pkt
, s
->streams
[0]->codecpar
->block_align
);
104 const FFInputFormat ff_aea_demuxer
= {
106 .p
.long_name
= NULL_IF_CONFIG_SMALL("MD STUDIO audio"),
107 .p
.flags
= AVFMT_GENERIC_INDEX
,
108 .p
.extensions
= "aea",
109 .read_probe
= aea_read_probe
,
110 .read_header
= aea_read_header
,
111 .read_packet
= aea_read_packet
,
112 .read_seek
= ff_pcm_read_seek
,