42315dab |
/*
* Chronomaster DFA Format Demuxer
* Copyright (c) 2011 Konstantin Shishkov
* |
59bf303d |
* This file is part of FFmpeg. |
42315dab |
* |
59bf303d |
* FFmpeg is free software; you can redistribute it and/or |
42315dab |
* 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.
* |
59bf303d |
* FFmpeg is distributed in the hope that it will be useful, |
42315dab |
* 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 |
59bf303d |
* License along with FFmpeg; if not, write to the Free Software |
42315dab |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
|
d92024f1 |
#include <inttypes.h>
|
42315dab |
#include "libavutil/intreadwrite.h"
#include "avformat.h" |
c3f9ebf7 |
#include "internal.h" |
42315dab |
static int dfa_probe(AVProbeData *p)
{
if (p->buf_size < 4 || AV_RL32(p->buf) != MKTAG('D', 'F', 'I', 'A'))
return 0;
|
bdab2421 |
if (AV_RL32(p->buf + 16) != 0x80)
return AVPROBE_SCORE_MAX / 4;
|
42315dab |
return AVPROBE_SCORE_MAX;
}
|
6e9651d1 |
static int dfa_read_header(AVFormatContext *s) |
42315dab |
{
AVIOContext *pb = s->pb;
AVStream *st;
int frames; |
1557f34b |
int version; |
42315dab |
uint32_t mspf;
if (avio_rl32(pb) != MKTAG('D', 'F', 'I', 'A')) {
av_log(s, AV_LOG_ERROR, "Invalid magic for DFA\n");
return AVERROR_INVALIDDATA;
} |
1557f34b |
version = avio_rl16(pb); |
42315dab |
frames = avio_rl16(pb);
|
3b3bbdd3 |
st = avformat_new_stream(s, NULL); |
42315dab |
if (!st)
return AVERROR(ENOMEM);
|
9200514a |
st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
st->codecpar->codec_id = AV_CODEC_ID_DFA;
st->codecpar->width = avio_rl16(pb);
st->codecpar->height = avio_rl16(pb); |
42315dab |
mspf = avio_rl32(pb);
if (!mspf) {
av_log(s, AV_LOG_WARNING, "Zero FPS reported, defaulting to 10\n");
mspf = 100;
} |
c3f9ebf7 |
avpriv_set_pts_info(st, 24, mspf, 1000); |
42315dab |
avio_skip(pb, 128 - 16); // padding
st->duration = frames;
|
6f69f7a8 |
if (ff_alloc_extradata(st->codecpar, 2)) |
a807c682 |
return AVERROR(ENOMEM); |
6f69f7a8 |
AV_WL16(st->codecpar->extradata, version); |
1557f34b |
if (version == 0x100)
st->sample_aspect_ratio = (AVRational){2, 1};
|
42315dab |
return 0;
}
static int dfa_read_packet(AVFormatContext *s, AVPacket *pkt)
{
AVIOContext *pb = s->pb;
uint32_t frame_size;
int ret, first = 1;
|
bd6f1458 |
if (avio_feof(pb)) |
42315dab |
return AVERROR_EOF;
if (av_get_packet(pb, pkt, 12) != 12)
return AVERROR(EIO); |
bd6f1458 |
while (!avio_feof(pb)) { |
42315dab |
if (!first) {
ret = av_append_packet(pb, pkt, 12);
if (ret < 0) { |
ce70f28a |
av_packet_unref(pkt); |
42315dab |
return ret;
}
} else
first = 0;
frame_size = AV_RL32(pkt->data + pkt->size - 8);
if (frame_size > INT_MAX - 4) { |
d92024f1 |
av_log(s, AV_LOG_ERROR, "Too large chunk size: %"PRIu32"\n", frame_size); |
c71999ef |
av_packet_unref(pkt); |
42315dab |
return AVERROR(EIO);
}
if (AV_RL32(pkt->data + pkt->size - 12) == MKTAG('E', 'O', 'F', 'R')) {
if (frame_size) { |
d92024f1 |
av_log(s, AV_LOG_WARNING,
"skipping %"PRIu32" bytes of end-of-frame marker chunk\n", |
42315dab |
frame_size);
avio_skip(pb, frame_size);
}
return 0;
}
ret = av_append_packet(pb, pkt, frame_size);
if (ret < 0) { |
ce70f28a |
av_packet_unref(pkt); |
42315dab |
return ret;
}
}
return 0;
}
AVInputFormat ff_dfa_demuxer = { |
dfc2c4d9 |
.name = "dfa",
.long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
.read_probe = dfa_probe,
.read_header = dfa_read_header,
.read_packet = dfa_read_packet, |
20234a4b |
.flags = AVFMT_GENERIC_INDEX, |
42315dab |
}; |