2009-09-10 21:48:12 +03:00
|
|
|
/*
|
|
|
|
* MD STUDIO audio demuxer
|
|
|
|
*
|
|
|
|
* Copyright (c) 2009 Benjamin Larsson
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2012-11-10 18:00:00 +03:00
|
|
|
#include "libavutil/channel_layout.h"
|
|
|
|
#include "libavutil/intreadwrite.h"
|
2009-09-10 21:48:12 +03:00
|
|
|
#include "avformat.h"
|
2024-03-15 17:38:30 +02:00
|
|
|
#include "avio_internal.h"
|
2024-02-10 16:50:43 +02:00
|
|
|
#include "demux.h"
|
2024-03-15 18:20:44 +02:00
|
|
|
#include "internal.h"
|
2010-08-31 00:17:34 +03:00
|
|
|
#include "pcm.h"
|
2009-09-10 21:48:12 +03:00
|
|
|
|
2023-10-02 00:53:16 +02:00
|
|
|
#define AT1_SU_SIZE 212
|
2009-09-10 21:48:12 +03:00
|
|
|
|
2019-03-21 02:18:37 +02:00
|
|
|
static int aea_read_probe(const AVProbeData *p)
|
2009-09-10 21:48:12 +03:00
|
|
|
{
|
2023-10-02 00:53:16 +02:00
|
|
|
if (p->buf_size <= 2048+AT1_SU_SIZE)
|
2009-09-10 21:48:12 +03:00
|
|
|
return 0;
|
|
|
|
|
2016-04-27 19:45:23 +02:00
|
|
|
/* Magic is '00 08 00 00' in little-endian*/
|
2009-09-10 22:12:13 +03:00
|
|
|
if (AV_RL32(p->buf)==0x800) {
|
2023-10-02 00:53:16 +02:00
|
|
|
int ch, block_size, score = 0;
|
2014-12-23 19:41:53 +02:00
|
|
|
ch = p->buf[264];
|
2009-09-10 21:48:12 +03:00
|
|
|
|
2009-09-13 21:27:53 +03:00
|
|
|
if (ch != 1 && ch != 2)
|
|
|
|
return 0;
|
2009-09-10 21:48:12 +03:00
|
|
|
|
2023-10-02 00:53:16 +02:00
|
|
|
block_size = ch * AT1_SU_SIZE;
|
2009-09-10 21:48:12 +03:00
|
|
|
/* Check so that the redundant bsm bytes and info bytes are valid
|
|
|
|
* the block size mode bytes have to be the same
|
|
|
|
* the info bytes have to be the same
|
|
|
|
*/
|
2023-10-02 00:53:16 +02:00
|
|
|
for (int i = 2048 + block_size; i + block_size <= p->buf_size; i += block_size) {
|
|
|
|
if (AV_RN16(p->buf+i) != AV_RN16(p->buf+i+AT1_SU_SIZE))
|
2014-12-23 19:41:53 +02:00
|
|
|
return 0;
|
2023-10-02 00:53:16 +02:00
|
|
|
score++;
|
2014-12-23 19:41:53 +02:00
|
|
|
}
|
2023-10-02 00:53:16 +02:00
|
|
|
return FFMIN(AVPROBE_SCORE_MAX / 4 + score, AVPROBE_SCORE_MAX);
|
2009-09-10 21:48:12 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-12 15:20:36 +03:00
|
|
|
static int aea_read_header(AVFormatContext *s)
|
2009-09-10 21:48:12 +03:00
|
|
|
{
|
2011-06-18 12:43:24 +03:00
|
|
|
AVStream *st = avformat_new_stream(s, NULL);
|
2024-03-15 17:38:30 +02:00
|
|
|
char title[256 + 1];
|
|
|
|
int channels, ret;
|
2009-09-10 21:48:12 +03:00
|
|
|
if (!st)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2024-03-15 17:38:30 +02:00
|
|
|
/* Read the title, parse the number of channels and skip to pos 2048(0x800) */
|
|
|
|
avio_rl32(s->pb); // magic
|
|
|
|
ret = ffio_read_size(s->pb, title, sizeof(title) - 1);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
title[sizeof(title) - 1] = '\0';
|
|
|
|
if (title[0] != '\0')
|
|
|
|
av_dict_set(&st->metadata, "title", title, 0);
|
|
|
|
avio_rl32(s->pb); // Block count
|
2017-03-31 13:25:52 +02:00
|
|
|
channels = avio_r8(s->pb);
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(s->pb, 1783);
|
2009-09-10 21:48:12 +03:00
|
|
|
|
|
|
|
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
|
|
|
|
st->codecpar->codec_id = AV_CODEC_ID_ATRAC1;
|
|
|
|
st->codecpar->sample_rate = 44100;
|
2023-10-02 01:07:52 +02:00
|
|
|
st->codecpar->bit_rate = 146000 * channels;
|
2009-09-10 21:48:12 +03:00
|
|
|
|
2017-03-31 13:25:52 +02:00
|
|
|
if (channels != 1 && channels != 2) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Channels %d not supported!\n", channels);
|
2015-01-15 14:50:03 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2009-09-10 21:48:12 +03:00
|
|
|
}
|
|
|
|
|
2017-03-31 13:25:52 +02:00
|
|
|
av_channel_layout_default(&st->codecpar->ch_layout, channels);
|
2009-09-10 21:48:12 +03:00
|
|
|
|
2017-03-31 13:25:52 +02:00
|
|
|
st->codecpar->block_align = AT1_SU_SIZE * st->codecpar->ch_layout.nb_channels;
|
2024-03-15 18:20:44 +02:00
|
|
|
avpriv_set_pts_info(st, 64, 1, 44100);
|
2009-09-10 21:48:12 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int aea_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
2022-09-27 00:13:10 +02:00
|
|
|
return av_get_packet(s->pb, pkt, s->streams[0]->codecpar->block_align);
|
2009-09-10 21:48:12 +03:00
|
|
|
}
|
|
|
|
|
2024-02-10 16:50:43 +02:00
|
|
|
const FFInputFormat ff_aea_demuxer = {
|
|
|
|
.p.name = "aea",
|
|
|
|
.p.long_name = NULL_IF_CONFIG_SMALL("MD STUDIO audio"),
|
|
|
|
.p.flags = AVFMT_GENERIC_INDEX,
|
|
|
|
.p.extensions = "aea",
|
2011-07-16 23:18:12 +03:00
|
|
|
.read_probe = aea_read_probe,
|
|
|
|
.read_header = aea_read_header,
|
|
|
|
.read_packet = aea_read_packet,
|
2012-02-15 12:28:39 +03:00
|
|
|
.read_seek = ff_pcm_read_seek,
|
2009-09-10 21:48:12 +03:00
|
|
|
};
|