2 * TechnoTrend PVA (.pva) demuxer
3 * Copyright (c) 2007, 2008 Ivo van Poorten
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #define PVA_MAX_PAYLOAD_LENGTH 0x17f8
27 #define PVA_VIDEO_PAYLOAD 0x01
28 #define PVA_AUDIO_PAYLOAD 0x02
29 #define PVA_MAGIC (('A' << 8) + 'V')
35 static int pva_check(const uint8_t *p) {
36 int length = AV_RB16(p + 6);
37 if (AV_RB16(p) != PVA_MAGIC || !p[2] || p[2] > 2 || p[4] != 0x55 ||
38 (p[5] & 0xe0) || length > PVA_MAX_PAYLOAD_LENGTH)
43 static int pva_probe(AVProbeData * pd) {
44 const unsigned char *buf = pd->buf;
45 int len = pva_check(buf);
50 if (pd->buf_size >= len + 8 &&
51 pva_check(buf + len) >= 0)
52 return AVPROBE_SCORE_EXTENSION;
54 return AVPROBE_SCORE_MAX / 4;
57 static int pva_read_header(AVFormatContext *s) {
60 if (!(st = avformat_new_stream(s, NULL)))
61 return AVERROR(ENOMEM);
62 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
63 st->codec->codec_id = AV_CODEC_ID_MPEG2VIDEO;
64 st->need_parsing = AVSTREAM_PARSE_FULL;
65 avpriv_set_pts_info(st, 32, 1, 90000);
66 av_add_index_entry(st, 0, 0, 0, 0, AVINDEX_KEYFRAME);
68 if (!(st = avformat_new_stream(s, NULL)))
69 return AVERROR(ENOMEM);
70 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
71 st->codec->codec_id = AV_CODEC_ID_MP2;
72 st->need_parsing = AVSTREAM_PARSE_FULL;
73 avpriv_set_pts_info(st, 33, 1, 90000);
74 av_add_index_entry(st, 0, 0, 0, 0, AVINDEX_KEYFRAME);
76 /* the parameters will be extracted from the compressed bitstream */
80 #define pva_log if (read_packet) av_log
82 static int read_part_of_packet(AVFormatContext *s, int64_t *pts,
83 int *len, int *strid, int read_packet) {
84 AVIOContext *pb = s->pb;
85 PVAContext *pvactx = s->priv_data;
86 int syncword, streamid, reserved, flags, length, pts_flag;
87 int64_t pva_pts = AV_NOPTS_VALUE, startpos;
90 startpos = avio_tell(pb);
92 syncword = avio_rb16(pb);
93 streamid = avio_r8(pb);
94 avio_r8(pb); /* counter not used */
95 reserved = avio_r8(pb);
97 length = avio_rb16(pb);
99 pts_flag = flags & 0x10;
101 if (syncword != PVA_MAGIC) {
102 pva_log(s, AV_LOG_ERROR, "invalid syncword\n");
105 if (streamid != PVA_VIDEO_PAYLOAD && streamid != PVA_AUDIO_PAYLOAD) {
106 pva_log(s, AV_LOG_ERROR, "invalid streamid\n");
109 if (reserved != 0x55) {
110 pva_log(s, AV_LOG_WARNING, "expected reserved byte to be 0x55\n");
112 if (length > PVA_MAX_PAYLOAD_LENGTH) {
113 pva_log(s, AV_LOG_ERROR, "invalid payload length %u\n", length);
117 if (streamid == PVA_VIDEO_PAYLOAD && pts_flag) {
118 pva_pts = avio_rb32(pb);
120 } else if (streamid == PVA_AUDIO_PAYLOAD) {
121 /* PVA Audio Packets either start with a signaled PES packet or
122 * are a continuation of the previous PES packet. New PES packets
123 * always start at the beginning of a PVA Packet, never somewhere in
125 if (!pvactx->continue_pes) {
126 int pes_signal, pes_header_data_length, pes_packet_length,
128 unsigned char pes_header_data[256];
130 pes_signal = avio_rb24(pb);
132 pes_packet_length = avio_rb16(pb);
133 pes_flags = avio_rb16(pb);
134 pes_header_data_length = avio_r8(pb);
136 if (pes_signal != 1) {
137 pva_log(s, AV_LOG_WARNING, "expected signaled PES packet, "
138 "trying to recover\n");
139 avio_skip(pb, length - 9);
145 avio_read(pb, pes_header_data, pes_header_data_length);
146 length -= 9 + pes_header_data_length;
148 pes_packet_length -= 3 + pes_header_data_length;
150 pvactx->continue_pes = pes_packet_length;
152 if (pes_flags & 0x80 && (pes_header_data[0] & 0xf0) == 0x20)
153 pva_pts = ff_parse_pes_pts(pes_header_data);
156 pvactx->continue_pes -= length;
158 if (pvactx->continue_pes < 0) {
159 pva_log(s, AV_LOG_WARNING, "audio data corruption\n");
160 pvactx->continue_pes = 0;
164 if (pva_pts != AV_NOPTS_VALUE)
165 av_add_index_entry(s->streams[streamid-1], startpos, pva_pts, 0, 0, AVINDEX_KEYFRAME);
173 static int pva_read_packet(AVFormatContext *s, AVPacket *pkt) {
174 AVIOContext *pb = s->pb;
176 int ret, length, streamid;
178 if (read_part_of_packet(s, &pva_pts, &length, &streamid, 1) < 0 ||
179 (ret = av_get_packet(pb, pkt, length)) <= 0)
182 pkt->stream_index = streamid - 1;
188 static int64_t pva_read_timestamp(struct AVFormatContext *s, int stream_index,
189 int64_t *pos, int64_t pos_limit) {
190 AVIOContext *pb = s->pb;
191 PVAContext *pvactx = s->priv_data;
192 int length, streamid;
193 int64_t res = AV_NOPTS_VALUE;
195 pos_limit = FFMIN(*pos+PVA_MAX_PAYLOAD_LENGTH*8, (uint64_t)*pos+pos_limit);
197 while (*pos < pos_limit) {
198 res = AV_NOPTS_VALUE;
199 avio_seek(pb, *pos, SEEK_SET);
201 pvactx->continue_pes = 0;
202 if (read_part_of_packet(s, &res, &length, &streamid, 0)) {
206 if (streamid - 1 != stream_index || res == AV_NOPTS_VALUE) {
207 *pos = avio_tell(pb) + length;
213 pvactx->continue_pes = 0;
217 AVInputFormat ff_pva_demuxer = {
219 .long_name = NULL_IF_CONFIG_SMALL("TechnoTrend PVA"),
220 .priv_data_size = sizeof(PVAContext),
221 .read_probe = pva_probe,
222 .read_header = pva_read_header,
223 .read_packet = pva_read_packet,
224 .read_timestamp = pva_read_timestamp,