2012-10-05 14:32:20 +02:00
|
|
|
/*
|
|
|
|
* Raw DTS-HD demuxer
|
|
|
|
* Copyright (c) 2012 Paul B Mahol
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libavutil/intreadwrite.h"
|
|
|
|
#include "libavutil/dict.h"
|
2016-05-16 20:45:22 +02:00
|
|
|
#include "libavcodec/dca.h"
|
2012-10-05 14:32:20 +02:00
|
|
|
#include "avformat.h"
|
2016-05-16 20:45:22 +02:00
|
|
|
#include "internal.h"
|
2012-10-05 14:32:20 +02:00
|
|
|
|
|
|
|
#define AUPR_HDR 0x415550522D484452
|
|
|
|
#define AUPRINFO 0x41555052494E464F
|
|
|
|
#define BITSHVTB 0x4249545348565442
|
|
|
|
#define BLACKOUT 0x424C41434B4F5554
|
|
|
|
#define BRANCHPT 0x4252414E43485054
|
|
|
|
#define BUILDVER 0x4255494C44564552
|
|
|
|
#define CORESSMD 0x434F524553534D44
|
|
|
|
#define DTSHDHDR 0x4454534844484452
|
|
|
|
#define EXTSS_MD 0x45585453535f4d44
|
|
|
|
#define FILEINFO 0x46494C45494E464F
|
|
|
|
#define NAVI_TBL 0x4E4156492D54424C
|
|
|
|
#define STRMDATA 0x5354524D44415441
|
|
|
|
#define TIMECODE 0x54494D45434F4445
|
|
|
|
|
|
|
|
typedef struct DTSHDDemuxContext {
|
2012-10-10 16:41:49 +02:00
|
|
|
uint64_t data_end;
|
2012-10-05 14:32:20 +02:00
|
|
|
} DTSHDDemuxContext;
|
|
|
|
|
|
|
|
static int dtshd_probe(AVProbeData *p)
|
|
|
|
{
|
|
|
|
if (AV_RB64(p->buf) == DTSHDHDR)
|
|
|
|
return AVPROBE_SCORE_MAX;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dtshd_read_header(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
DTSHDDemuxContext *dtshd = s->priv_data;
|
|
|
|
AVIOContext *pb = s->pb;
|
|
|
|
uint64_t chunk_type, chunk_size;
|
2016-05-19 05:02:50 +02:00
|
|
|
int64_t duration, data_start;
|
2012-10-05 14:32:20 +02:00
|
|
|
AVStream *st;
|
|
|
|
int ret;
|
|
|
|
char *value;
|
|
|
|
|
|
|
|
st = avformat_new_stream(s, NULL);
|
|
|
|
if (!st)
|
|
|
|
return AVERROR(ENOMEM);
|
2016-04-10 21:58:15 +02:00
|
|
|
st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
|
|
|
|
st->codecpar->codec_id = AV_CODEC_ID_DTS;
|
|
|
|
st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
|
2012-10-05 14:32:20 +02:00
|
|
|
|
2016-05-16 07:13:46 +02:00
|
|
|
for (;;) {
|
2012-10-05 14:32:20 +02:00
|
|
|
chunk_type = avio_rb64(pb);
|
|
|
|
chunk_size = avio_rb64(pb);
|
|
|
|
|
2016-05-16 07:13:46 +02:00
|
|
|
if (avio_feof(pb))
|
|
|
|
break;
|
|
|
|
|
2012-10-05 14:32:20 +02:00
|
|
|
if (chunk_size < 4) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "chunk size too small\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
if (chunk_size > ((uint64_t)1 << 61)) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "chunk size too big\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (chunk_type) {
|
|
|
|
case STRMDATA:
|
2016-05-19 05:02:50 +02:00
|
|
|
data_start = avio_tell(pb);
|
|
|
|
dtshd->data_end = data_start + chunk_size;
|
2012-10-10 16:41:49 +02:00
|
|
|
if (dtshd->data_end <= chunk_size)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2016-05-16 07:13:46 +02:00
|
|
|
if (!pb->seekable)
|
2016-05-16 20:45:22 +02:00
|
|
|
goto break_loop;
|
2016-05-16 07:13:46 +02:00
|
|
|
goto skip;
|
2012-10-05 14:32:20 +02:00
|
|
|
break;
|
2016-05-16 20:45:22 +02:00
|
|
|
case AUPR_HDR:
|
|
|
|
if (chunk_size < 21)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
avio_skip(pb, 3);
|
|
|
|
st->codecpar->sample_rate = avio_rb24(pb);
|
|
|
|
if (!st->codecpar->sample_rate)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
duration = avio_rb32(pb); // num_frames
|
|
|
|
duration *= avio_rb16(pb); // samples_per_frames
|
|
|
|
st->duration = duration;
|
|
|
|
avio_skip(pb, 5);
|
|
|
|
st->codecpar->channels = ff_dca_count_chs_for_mask(avio_rb16(pb));
|
|
|
|
st->codecpar->initial_padding = avio_rb16(pb);
|
|
|
|
avio_skip(pb, chunk_size - 21);
|
|
|
|
break;
|
2012-10-05 14:32:20 +02:00
|
|
|
case FILEINFO:
|
|
|
|
if (chunk_size > INT_MAX)
|
|
|
|
goto skip;
|
|
|
|
value = av_malloc(chunk_size);
|
|
|
|
if (!value)
|
|
|
|
goto skip;
|
|
|
|
avio_read(pb, value, chunk_size);
|
|
|
|
value[chunk_size - 1] = 0;
|
|
|
|
av_dict_set(&s->metadata, "fileinfo", value,
|
|
|
|
AV_DICT_DONT_STRDUP_VAL);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
skip:
|
|
|
|
ret = avio_skip(pb, chunk_size);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-05-16 07:13:46 +02:00
|
|
|
if (!dtshd->data_end)
|
|
|
|
return AVERROR_EOF;
|
|
|
|
|
2016-05-19 05:02:50 +02:00
|
|
|
avio_seek(pb, data_start, SEEK_SET);
|
2016-05-16 07:13:46 +02:00
|
|
|
|
2016-05-16 20:45:22 +02:00
|
|
|
break_loop:
|
|
|
|
if (st->codecpar->sample_rate)
|
|
|
|
avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
|
|
|
|
|
2016-05-16 07:13:46 +02:00
|
|
|
return 0;
|
2012-10-05 14:32:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
DTSHDDemuxContext *dtshd = s->priv_data;
|
2012-10-10 16:41:49 +02:00
|
|
|
int64_t size, left;
|
2012-10-05 14:32:20 +02:00
|
|
|
int ret;
|
|
|
|
|
2012-10-10 16:41:49 +02:00
|
|
|
left = dtshd->data_end - avio_tell(s->pb);
|
|
|
|
size = FFMIN(left, 1024);
|
|
|
|
if (size <= 0)
|
2012-10-05 14:32:20 +02:00
|
|
|
return AVERROR_EOF;
|
|
|
|
|
|
|
|
ret = av_get_packet(s->pb, pkt, size);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
pkt->stream_index = 0;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVInputFormat ff_dtshd_demuxer = {
|
|
|
|
.name = "dtshd",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("raw DTS-HD"),
|
|
|
|
.priv_data_size = sizeof(DTSHDDemuxContext),
|
|
|
|
.read_probe = dtshd_probe,
|
|
|
|
.read_header = dtshd_read_header,
|
|
|
|
.read_packet = raw_read_packet,
|
|
|
|
.flags = AVFMT_GENERIC_INDEX,
|
|
|
|
.extensions = "dtshd",
|
|
|
|
.raw_codec_id = AV_CODEC_ID_DTS,
|
|
|
|
};
|