3feb44a9 |
/*
* 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" |
c3f9ebf7 |
#include "internal.h" |
9ae09203 |
#include "mpeg.h" |
3feb44a9 |
#define PVA_MAX_PAYLOAD_LENGTH 0x17f8
#define PVA_VIDEO_PAYLOAD 0x01
#define PVA_AUDIO_PAYLOAD 0x02
#define PVA_MAGIC (('A' << 8) + 'V')
|
daf8cf35 |
typedef struct PVAContext { |
3feb44a9 |
int continue_pes;
} PVAContext;
|
28b9099a |
static int pva_check(const uint8_t *p) { |
e91230a5 |
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;
}
|
3feb44a9 |
static int pva_probe(AVProbeData * pd) { |
28b9099a |
const unsigned char *buf = pd->buf; |
e91230a5 |
int len = pva_check(buf);
if (len < 0)
return 0; |
3feb44a9 |
|
e91230a5 |
if (pd->buf_size >= len + 8 &&
pva_check(buf + len) >= 0) |
e0f8be64 |
return AVPROBE_SCORE_EXTENSION; |
3feb44a9 |
|
e91230a5 |
return AVPROBE_SCORE_MAX / 4; |
3feb44a9 |
}
|
6e9651d1 |
static int pva_read_header(AVFormatContext *s) { |
3feb44a9 |
AVStream *st;
|
3b3bbdd3 |
if (!(st = avformat_new_stream(s, NULL))) |
3feb44a9 |
return AVERROR(ENOMEM); |
9200514a |
st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
st->codecpar->codec_id = AV_CODEC_ID_MPEG2VIDEO; |
3feb44a9 |
st->need_parsing = AVSTREAM_PARSE_FULL; |
c3f9ebf7 |
avpriv_set_pts_info(st, 32, 1, 90000); |
e03caf2e |
av_add_index_entry(st, 0, 0, 0, 0, AVINDEX_KEYFRAME); |
3feb44a9 |
|
84ad31ff |
if (!(st = avformat_new_stream(s, NULL))) |
3feb44a9 |
return AVERROR(ENOMEM); |
9200514a |
st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
st->codecpar->codec_id = AV_CODEC_ID_MP2; |
6125d865 |
st->need_parsing = AVSTREAM_PARSE_FULL; |
c3f9ebf7 |
avpriv_set_pts_info(st, 33, 1, 90000); |
e03caf2e |
av_add_index_entry(st, 0, 0, 0, 0, AVINDEX_KEYFRAME); |
3feb44a9 |
/* the parameters will be extracted from the compressed bitstream */
return 0;
}
|
e03caf2e |
#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) { |
ae628ec1 |
AVIOContext *pb = s->pb; |
3feb44a9 |
PVAContext *pvactx = s->priv_data; |
e03caf2e |
int syncword, streamid, reserved, flags, length, pts_flag;
int64_t pva_pts = AV_NOPTS_VALUE, startpos; |
5ec3c7b7 |
int ret; |
3feb44a9 |
|
dba13944 |
recover: |
a2704c97 |
startpos = avio_tell(pb); |
e03caf2e |
|
b7effd4e |
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); |
3feb44a9 |
|
5e40d0e8 |
pts_flag = flags & 0x10; |
3feb44a9 |
if (syncword != PVA_MAGIC) { |
e03caf2e |
pva_log(s, AV_LOG_ERROR, "invalid syncword\n"); |
3feb44a9 |
return AVERROR(EIO);
} |
7705cf7e |
if (streamid != PVA_VIDEO_PAYLOAD && streamid != PVA_AUDIO_PAYLOAD) { |
e03caf2e |
pva_log(s, AV_LOG_ERROR, "invalid streamid\n"); |
7705cf7e |
return AVERROR(EIO);
} |
3feb44a9 |
if (reserved != 0x55) { |
e03caf2e |
pva_log(s, AV_LOG_WARNING, "expected reserved byte to be 0x55\n"); |
3feb44a9 |
}
if (length > PVA_MAX_PAYLOAD_LENGTH) { |
e03caf2e |
pva_log(s, AV_LOG_ERROR, "invalid payload length %u\n", length); |
3feb44a9 |
return AVERROR(EIO);
}
if (streamid == PVA_VIDEO_PAYLOAD && pts_flag) { |
b7effd4e |
pva_pts = avio_rb32(pb); |
3feb44a9 |
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];
|
b7effd4e |
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); |
3feb44a9 |
|
6f4b82cc |
if (avio_feof(pb)) {
return AVERROR_EOF;
}
|
5ec3c7b7 |
if (pes_signal != 1 || pes_header_data_length == 0) {
pva_log(s, AV_LOG_WARNING, "expected non empty signaled PES packet, " |
dba13944 |
"trying to recover\n"); |
45a8a02a |
avio_skip(pb, length - 9); |
e03caf2e |
if (!read_packet)
return AVERROR(EIO); |
dba13944 |
goto recover; |
3feb44a9 |
}
|
5ec3c7b7 |
ret = avio_read(pb, pes_header_data, pes_header_data_length);
if (ret != pes_header_data_length)
return ret < 0 ? ret : AVERROR_INVALIDDATA; |
3feb44a9 |
length -= 9 + pes_header_data_length;
pes_packet_length -= 3 + pes_header_data_length;
pvactx->continue_pes = pes_packet_length;
|
eedd9148 |
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;
} |
9ae09203 |
pva_pts = ff_parse_pes_pts(pes_header_data); |
eedd9148 |
} |
3feb44a9 |
}
pvactx->continue_pes -= length;
if (pvactx->continue_pes < 0) { |
e03caf2e |
pva_log(s, AV_LOG_WARNING, "audio data corruption\n"); |
3feb44a9 |
pvactx->continue_pes = 0;
}
}
|
e03caf2e |
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) { |
ae628ec1 |
AVIOContext *pb = s->pb; |
e03caf2e |
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) |
3feb44a9 |
return AVERROR(EIO);
pkt->stream_index = streamid - 1; |
ffc29341 |
pkt->pts = pva_pts; |
3feb44a9 |
return ret;
}
|
e03caf2e |
static int64_t pva_read_timestamp(struct AVFormatContext *s, int stream_index,
int64_t *pos, int64_t pos_limit) { |
ae628ec1 |
AVIOContext *pb = s->pb; |
e03caf2e |
PVAContext *pvactx = s->priv_data;
int length, streamid; |
896873b5 |
int64_t res = AV_NOPTS_VALUE; |
e03caf2e |
pos_limit = FFMIN(*pos+PVA_MAX_PAYLOAD_LENGTH*8, (uint64_t)*pos+pos_limit);
while (*pos < pos_limit) {
res = AV_NOPTS_VALUE; |
6b4aa5da |
avio_seek(pb, *pos, SEEK_SET); |
e03caf2e |
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) { |
a2704c97 |
*pos = avio_tell(pb) + length; |
e03caf2e |
continue;
}
break;
}
pvactx->continue_pes = 0;
return res;
}
|
c6610a21 |
AVInputFormat ff_pva_demuxer = { |
dfc2c4d9 |
.name = "pva", |
6774247a |
.long_name = NULL_IF_CONFIG_SMALL("TechnoTrend PVA"), |
dfc2c4d9 |
.priv_data_size = sizeof(PVAContext),
.read_probe = pva_probe,
.read_header = pva_read_header,
.read_packet = pva_read_packet, |
20234a4b |
.read_timestamp = pva_read_timestamp, |
3feb44a9 |
}; |