2008-01-02 10:45:28 +00:00
|
|
|
/*
|
|
|
|
* TechnoTrend PVA (.pva) demuxer
|
|
|
|
* Copyright (c) 2007, 2008 Ivo van Poorten
|
|
|
|
*
|
|
|
|
* 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 "avformat.h"
|
2011-11-29 18:28:15 +00:00
|
|
|
#include "internal.h"
|
2008-01-07 23:38:12 +00:00
|
|
|
#include "mpeg.h"
|
2008-01-02 10:45:28 +00:00
|
|
|
|
|
|
|
#define PVA_MAX_PAYLOAD_LENGTH 0x17f8
|
|
|
|
#define PVA_VIDEO_PAYLOAD 0x01
|
|
|
|
#define PVA_AUDIO_PAYLOAD 0x02
|
|
|
|
#define PVA_MAGIC (('A' << 8) + 'V')
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int continue_pes;
|
|
|
|
} PVAContext;
|
|
|
|
|
2012-12-25 00:54:41 +00:00
|
|
|
static int pva_check(const uint8_t *p) {
|
2011-10-13 17:45:49 +00:00
|
|
|
int length = AV_RB16(p + 6);
|
|
|
|
if (AV_RB16(p) != PVA_MAGIC || !p[2] || p[2] > 2 || p[4] != 0x55 ||
|
|
|
|
(p[5] & 0xe0) || length > PVA_MAX_PAYLOAD_LENGTH)
|
|
|
|
return -1;
|
|
|
|
return length + 8;
|
|
|
|
}
|
|
|
|
|
2008-01-02 10:45:28 +00:00
|
|
|
static int pva_probe(AVProbeData * pd) {
|
2012-12-25 00:54:41 +00:00
|
|
|
const unsigned char *buf = pd->buf;
|
2011-10-13 17:45:49 +00:00
|
|
|
int len = pva_check(buf);
|
|
|
|
|
|
|
|
if (len < 0)
|
|
|
|
return 0;
|
2008-01-02 10:45:28 +00:00
|
|
|
|
2011-10-13 17:45:49 +00:00
|
|
|
if (pd->buf_size >= len + 8 &&
|
|
|
|
pva_check(buf + len) >= 0)
|
2013-03-25 15:12:51 +00:00
|
|
|
return AVPROBE_SCORE_EXTENSION;
|
2008-01-02 10:45:28 +00:00
|
|
|
|
2011-10-13 17:45:49 +00:00
|
|
|
return AVPROBE_SCORE_MAX / 4;
|
2008-01-02 10:45:28 +00:00
|
|
|
}
|
|
|
|
|
2012-01-12 12:20:36 +00:00
|
|
|
static int pva_read_header(AVFormatContext *s) {
|
2008-01-02 10:45:28 +00:00
|
|
|
AVStream *st;
|
|
|
|
|
2011-06-18 09:43:24 +00:00
|
|
|
if (!(st = avformat_new_stream(s, NULL)))
|
2008-01-02 10:45:28 +00:00
|
|
|
return AVERROR(ENOMEM);
|
2010-03-30 23:30:55 +00:00
|
|
|
st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
|
2012-08-05 09:11:04 +00:00
|
|
|
st->codec->codec_id = AV_CODEC_ID_MPEG2VIDEO;
|
2008-01-02 10:45:28 +00:00
|
|
|
st->need_parsing = AVSTREAM_PARSE_FULL;
|
2011-11-29 18:28:15 +00:00
|
|
|
avpriv_set_pts_info(st, 32, 1, 90000);
|
2008-01-07 23:42:52 +00:00
|
|
|
av_add_index_entry(st, 0, 0, 0, 0, AVINDEX_KEYFRAME);
|
2008-01-02 10:45:28 +00:00
|
|
|
|
2011-06-18 09:43:24 +00:00
|
|
|
if (!(st = avformat_new_stream(s, NULL)))
|
2008-01-02 10:45:28 +00:00
|
|
|
return AVERROR(ENOMEM);
|
2010-03-30 23:30:55 +00:00
|
|
|
st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
|
2012-08-05 09:11:04 +00:00
|
|
|
st->codec->codec_id = AV_CODEC_ID_MP2;
|
2009-05-27 06:58:22 +00:00
|
|
|
st->need_parsing = AVSTREAM_PARSE_FULL;
|
2011-11-29 18:28:15 +00:00
|
|
|
avpriv_set_pts_info(st, 33, 1, 90000);
|
2008-01-07 23:42:52 +00:00
|
|
|
av_add_index_entry(st, 0, 0, 0, 0, AVINDEX_KEYFRAME);
|
2008-01-02 10:45:28 +00:00
|
|
|
|
|
|
|
/* the parameters will be extracted from the compressed bitstream */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-01-07 23:42:52 +00:00
|
|
|
#define pva_log if (read_packet) av_log
|
|
|
|
|
|
|
|
static int read_part_of_packet(AVFormatContext *s, int64_t *pts,
|
|
|
|
int *len, int *strid, int read_packet) {
|
2011-02-20 10:04:12 +00:00
|
|
|
AVIOContext *pb = s->pb;
|
2008-01-02 10:45:28 +00:00
|
|
|
PVAContext *pvactx = s->priv_data;
|
2008-01-07 23:42:52 +00:00
|
|
|
int syncword, streamid, reserved, flags, length, pts_flag;
|
|
|
|
int64_t pva_pts = AV_NOPTS_VALUE, startpos;
|
2013-12-20 17:07:30 +00:00
|
|
|
int ret;
|
2008-01-02 10:45:28 +00:00
|
|
|
|
2008-01-05 22:27:49 +00:00
|
|
|
recover:
|
2011-03-03 19:11:45 +00:00
|
|
|
startpos = avio_tell(pb);
|
2008-01-07 23:42:52 +00:00
|
|
|
|
2011-02-21 15:43:01 +00:00
|
|
|
syncword = avio_rb16(pb);
|
|
|
|
streamid = avio_r8(pb);
|
|
|
|
avio_r8(pb); /* counter not used */
|
|
|
|
reserved = avio_r8(pb);
|
|
|
|
flags = avio_r8(pb);
|
|
|
|
length = avio_rb16(pb);
|
2008-01-02 10:45:28 +00:00
|
|
|
|
2008-01-05 22:33:27 +00:00
|
|
|
pts_flag = flags & 0x10;
|
2008-01-02 10:45:28 +00:00
|
|
|
|
|
|
|
if (syncword != PVA_MAGIC) {
|
2008-01-07 23:42:52 +00:00
|
|
|
pva_log(s, AV_LOG_ERROR, "invalid syncword\n");
|
2008-01-02 10:45:28 +00:00
|
|
|
return AVERROR(EIO);
|
|
|
|
}
|
2008-01-05 22:18:44 +00:00
|
|
|
if (streamid != PVA_VIDEO_PAYLOAD && streamid != PVA_AUDIO_PAYLOAD) {
|
2008-01-07 23:42:52 +00:00
|
|
|
pva_log(s, AV_LOG_ERROR, "invalid streamid\n");
|
2008-01-05 22:18:44 +00:00
|
|
|
return AVERROR(EIO);
|
|
|
|
}
|
2008-01-02 10:45:28 +00:00
|
|
|
if (reserved != 0x55) {
|
2008-01-07 23:42:52 +00:00
|
|
|
pva_log(s, AV_LOG_WARNING, "expected reserved byte to be 0x55\n");
|
2008-01-02 10:45:28 +00:00
|
|
|
}
|
|
|
|
if (length > PVA_MAX_PAYLOAD_LENGTH) {
|
2008-01-07 23:42:52 +00:00
|
|
|
pva_log(s, AV_LOG_ERROR, "invalid payload length %u\n", length);
|
2008-01-02 10:45:28 +00:00
|
|
|
return AVERROR(EIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (streamid == PVA_VIDEO_PAYLOAD && pts_flag) {
|
2011-02-21 15:43:01 +00:00
|
|
|
pva_pts = avio_rb32(pb);
|
2008-01-02 10:45:28 +00:00
|
|
|
length -= 4;
|
|
|
|
} else if (streamid == PVA_AUDIO_PAYLOAD) {
|
|
|
|
/* PVA Audio Packets either start with a signaled PES packet or
|
|
|
|
* are a continuation of the previous PES packet. New PES packets
|
|
|
|
* always start at the beginning of a PVA Packet, never somewhere in
|
|
|
|
* the middle. */
|
|
|
|
if (!pvactx->continue_pes) {
|
|
|
|
int pes_signal, pes_header_data_length, pes_packet_length,
|
|
|
|
pes_flags;
|
|
|
|
unsigned char pes_header_data[256];
|
|
|
|
|
2011-02-21 15:43:01 +00:00
|
|
|
pes_signal = avio_rb24(pb);
|
|
|
|
avio_r8(pb);
|
|
|
|
pes_packet_length = avio_rb16(pb);
|
|
|
|
pes_flags = avio_rb16(pb);
|
|
|
|
pes_header_data_length = avio_r8(pb);
|
2008-01-02 10:45:28 +00:00
|
|
|
|
2013-12-20 17:07:30 +00:00
|
|
|
if (pes_signal != 1 || pes_header_data_length == 0) {
|
|
|
|
pva_log(s, AV_LOG_WARNING, "expected non empty signaled PES packet, "
|
2008-01-05 22:27:49 +00:00
|
|
|
"trying to recover\n");
|
2011-03-15 08:14:38 +00:00
|
|
|
avio_skip(pb, length - 9);
|
2008-01-07 23:42:52 +00:00
|
|
|
if (!read_packet)
|
|
|
|
return AVERROR(EIO);
|
2008-01-05 22:27:49 +00:00
|
|
|
goto recover;
|
2008-01-02 10:45:28 +00:00
|
|
|
}
|
|
|
|
|
2013-12-20 17:07:30 +00:00
|
|
|
ret = avio_read(pb, pes_header_data, pes_header_data_length);
|
|
|
|
if (ret != pes_header_data_length)
|
|
|
|
return ret < 0 ? ret : AVERROR_INVALIDDATA;
|
2008-01-02 10:45:28 +00:00
|
|
|
length -= 9 + pes_header_data_length;
|
|
|
|
|
|
|
|
pes_packet_length -= 3 + pes_header_data_length;
|
|
|
|
|
|
|
|
pvactx->continue_pes = pes_packet_length;
|
|
|
|
|
2013-12-20 17:07:30 +00:00
|
|
|
if (pes_flags & 0x80 && (pes_header_data[0] & 0xf0) == 0x20) {
|
|
|
|
if (pes_header_data_length < 5) {
|
|
|
|
pva_log(s, AV_LOG_ERROR, "header too short\n");
|
|
|
|
avio_skip(pb, length);
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2008-01-07 23:38:12 +00:00
|
|
|
pva_pts = ff_parse_pes_pts(pes_header_data);
|
2013-12-20 17:07:30 +00:00
|
|
|
}
|
2008-01-02 10:45:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pvactx->continue_pes -= length;
|
|
|
|
|
|
|
|
if (pvactx->continue_pes < 0) {
|
2008-01-07 23:42:52 +00:00
|
|
|
pva_log(s, AV_LOG_WARNING, "audio data corruption\n");
|
2008-01-02 10:45:28 +00:00
|
|
|
pvactx->continue_pes = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-07 23:42:52 +00:00
|
|
|
if (pva_pts != AV_NOPTS_VALUE)
|
|
|
|
av_add_index_entry(s->streams[streamid-1], startpos, pva_pts, 0, 0, AVINDEX_KEYFRAME);
|
|
|
|
|
|
|
|
*pts = pva_pts;
|
|
|
|
*len = length;
|
|
|
|
*strid = streamid;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pva_read_packet(AVFormatContext *s, AVPacket *pkt) {
|
2011-02-20 10:04:12 +00:00
|
|
|
AVIOContext *pb = s->pb;
|
2008-01-07 23:42:52 +00:00
|
|
|
int64_t pva_pts;
|
|
|
|
int ret, length, streamid;
|
|
|
|
|
|
|
|
if (read_part_of_packet(s, &pva_pts, &length, &streamid, 1) < 0 ||
|
|
|
|
(ret = av_get_packet(pb, pkt, length)) <= 0)
|
2008-01-02 10:45:28 +00:00
|
|
|
return AVERROR(EIO);
|
|
|
|
|
|
|
|
pkt->stream_index = streamid - 1;
|
2008-01-07 23:40:04 +00:00
|
|
|
pkt->pts = pva_pts;
|
2008-01-02 10:45:28 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-01-07 23:42:52 +00:00
|
|
|
static int64_t pva_read_timestamp(struct AVFormatContext *s, int stream_index,
|
|
|
|
int64_t *pos, int64_t pos_limit) {
|
2011-02-20 10:04:12 +00:00
|
|
|
AVIOContext *pb = s->pb;
|
2008-01-07 23:42:52 +00:00
|
|
|
PVAContext *pvactx = s->priv_data;
|
|
|
|
int length, streamid;
|
2009-04-23 18:44:03 +00:00
|
|
|
int64_t res = AV_NOPTS_VALUE;
|
2008-01-07 23:42:52 +00:00
|
|
|
|
|
|
|
pos_limit = FFMIN(*pos+PVA_MAX_PAYLOAD_LENGTH*8, (uint64_t)*pos+pos_limit);
|
|
|
|
|
|
|
|
while (*pos < pos_limit) {
|
|
|
|
res = AV_NOPTS_VALUE;
|
2011-02-28 13:57:54 +00:00
|
|
|
avio_seek(pb, *pos, SEEK_SET);
|
2008-01-07 23:42:52 +00:00
|
|
|
|
|
|
|
pvactx->continue_pes = 0;
|
|
|
|
if (read_part_of_packet(s, &res, &length, &streamid, 0)) {
|
|
|
|
(*pos)++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (streamid - 1 != stream_index || res == AV_NOPTS_VALUE) {
|
2011-03-03 19:11:45 +00:00
|
|
|
*pos = avio_tell(pb) + length;
|
2008-01-07 23:42:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
pvactx->continue_pes = 0;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2011-01-25 22:03:28 +00:00
|
|
|
AVInputFormat ff_pva_demuxer = {
|
2011-07-16 20:18:12 +00:00
|
|
|
.name = "pva",
|
2012-07-24 21:51:41 +00:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("TechnoTrend PVA"),
|
2011-07-16 20:18:12 +00:00
|
|
|
.priv_data_size = sizeof(PVAContext),
|
|
|
|
.read_probe = pva_probe,
|
|
|
|
.read_header = pva_read_header,
|
|
|
|
.read_packet = pva_read_packet,
|
2012-04-06 14:50:48 +00:00
|
|
|
.read_timestamp = pva_read_timestamp,
|
2008-01-02 10:45:28 +00:00
|
|
|
};
|