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
24 #define PVA_MAX_PAYLOAD_LENGTH 0x17f8
25 #define PVA_VIDEO_PAYLOAD 0x01
26 #define PVA_AUDIO_PAYLOAD 0x02
27 #define PVA_MAGIC (('A' << 8) + 'V')
33 static int pva_probe(AVProbeData * pd) {
34 unsigned char *buf = pd->buf;
36 if (AV_RB16(buf) == PVA_MAGIC && buf[2] && buf[2] < 3 && buf[4] == 0x55)
37 return AVPROBE_SCORE_MAX / 2;
42 static int pva_read_header(AVFormatContext *s, AVFormatParameters *ap) {
45 if (!(st = av_new_stream(s, 0)))
46 return AVERROR(ENOMEM);
47 st->codec->codec_type = CODEC_TYPE_VIDEO;
48 st->codec->codec_id = CODEC_ID_MPEG2VIDEO;
49 st->need_parsing = AVSTREAM_PARSE_FULL;
50 av_set_pts_info(st, 32, 1, 90000);
52 if (!(st = av_new_stream(s, 1)))
53 return AVERROR(ENOMEM);
54 st->codec->codec_type = CODEC_TYPE_AUDIO;
55 st->codec->codec_id = CODEC_ID_MP2;
56 st->need_parsing = AVSTREAM_PARSE_HEADERS;
57 av_set_pts_info(st, 33, 1, 90000);
59 /* the parameters will be extracted from the compressed bitstream */
63 static int pva_read_packet(AVFormatContext *s, AVPacket *pkt) {
64 ByteIOContext *pb = s->pb;
65 PVAContext *pvactx = s->priv_data;
66 int ret, syncword, streamid, reserved, flags, length, pts_flag;
67 int64_t pva_pts = AV_NOPTS_VALUE;
69 syncword = get_be16(pb);
70 streamid = get_byte(pb);
71 get_byte(pb); /* counter not used */
72 reserved = get_byte(pb);
74 length = get_be16(pb);
76 pts_flag = flags & 0x10;
78 if (syncword != PVA_MAGIC) {
79 av_log(s, AV_LOG_ERROR, "invalid syncword\n");
82 if (reserved != 0x55) {
83 av_log(s, AV_LOG_WARNING, "expected reserved byte to be 0x55\n");
85 if (length > PVA_MAX_PAYLOAD_LENGTH) {
86 av_log(s, AV_LOG_ERROR, "invalid payload length %u\n", length);
90 if (streamid == PVA_VIDEO_PAYLOAD && pts_flag) {
91 pva_pts = get_be32(pb);
93 } else if (streamid == PVA_AUDIO_PAYLOAD) {
94 /* PVA Audio Packets either start with a signaled PES packet or
95 * are a continuation of the previous PES packet. New PES packets
96 * always start at the beginning of a PVA Packet, never somewhere in
98 if (!pvactx->continue_pes) {
99 int pes_signal, pes_header_data_length, pes_packet_length,
101 unsigned char pes_header_data[256];
103 pes_signal = get_be24(pb);
105 pes_packet_length = get_be16(pb);
106 pes_flags = get_be16(pb);
107 pes_header_data_length = get_byte(pb);
109 if (pes_signal != 1) {
110 av_log(s, AV_LOG_ERROR, "expected signaled PES packet\n");
114 get_buffer(pb, pes_header_data, pes_header_data_length);
115 length -= 9 + pes_header_data_length;
117 pes_packet_length -= 3 + pes_header_data_length;
119 pvactx->continue_pes = pes_packet_length;
121 if (pes_flags & 0x80 && (pes_header_data[0] & 0xf0) == 0x20) {
122 pva_pts = ((long long) *pes_header_data & 0x0e) << 29;
123 pva_pts += (AV_RB16(pes_header_data+1) >> 1) << 15;
124 pva_pts += AV_RB16(pes_header_data+3) >> 1;
128 pvactx->continue_pes -= length;
130 if (pvactx->continue_pes < 0) {
131 av_log(s, AV_LOG_WARNING, "audio data corruption\n");
132 pvactx->continue_pes = 0;
136 if ((ret = av_get_packet(pb, pkt, length)) <= 0)
139 pkt->stream_index = streamid - 1;
146 AVInputFormat pva_demuxer = {
148 "pva file and stream format",