1 /*
2  * TechnoTrend PVA (.pva) demuxer
3  * Copyright (c) 2007, 2008 Ivo van Poorten
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 
22 #include "avformat.h"
23 #include "avio_internal.h"
24 #include "internal.h"
25 #include "mpeg.h"
26 
27 #define PVA_MAX_PAYLOAD_LENGTH  0x17f8
28 #define PVA_VIDEO_PAYLOAD       0x01
29 #define PVA_AUDIO_PAYLOAD       0x02
30 #define PVA_MAGIC               (('A' << 8) + 'V')
31 
32 typedef struct PVAContext {
33     int continue_pes;
34 } PVAContext;
35 
pva_check(const uint8_t *p)36 static int pva_check(const uint8_t *p) {
37     int length = AV_RB16(p + 6);
38     if (AV_RB16(p) != PVA_MAGIC || !p[2] || p[2] > 2 || p[4] != 0x55 ||
39         (p[5] & 0xe0) || length > PVA_MAX_PAYLOAD_LENGTH)
40         return -1;
41     return length + 8;
42 }
43 
pva_probe(const AVProbeData * pd)44 static int pva_probe(const AVProbeData * pd) {
45     const unsigned char *buf = pd->buf;
46     int len = pva_check(buf);
47 
48     if (len < 0)
49         return 0;
50 
51     if (pd->buf_size >= len + 8 &&
52         pva_check(buf + len) >= 0)
53         return AVPROBE_SCORE_EXTENSION;
54 
55     return AVPROBE_SCORE_MAX / 4;
56 }
57 
pva_read_header(AVFormatContext *s)58 static int pva_read_header(AVFormatContext *s) {
59     AVStream *st;
60 
61     if (!(st = avformat_new_stream(s, NULL)))
62         return AVERROR(ENOMEM);
63     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
64     st->codecpar->codec_id   = AV_CODEC_ID_MPEG2VIDEO;
65     ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
66     avpriv_set_pts_info(st, 32, 1, 90000);
67     av_add_index_entry(st, 0, 0, 0, 0, AVINDEX_KEYFRAME);
68 
69     if (!(st = avformat_new_stream(s, NULL)))
70         return AVERROR(ENOMEM);
71     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
72     st->codecpar->codec_id   = AV_CODEC_ID_MP2;
73     ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
74     avpriv_set_pts_info(st, 33, 1, 90000);
75     av_add_index_entry(st, 0, 0, 0, 0, AVINDEX_KEYFRAME);
76 
77     /* the parameters will be extracted from the compressed bitstream */
78     return 0;
79 }
80 
81 #define pva_log if (read_packet) av_log
82 
read_part_of_packet(AVFormatContext *s, int64_t *pts, int *len, int *strid, int read_packet)83 static int read_part_of_packet(AVFormatContext *s, int64_t *pts,
84                                int *len, int *strid, int read_packet) {
85     AVIOContext *pb = s->pb;
86     PVAContext *pvactx = s->priv_data;
87     int syncword, streamid, reserved, flags, length, pts_flag;
88     int64_t pva_pts = AV_NOPTS_VALUE, startpos;
89     int ret;
90 
91 recover:
92     startpos = avio_tell(pb);
93 
94     syncword = avio_rb16(pb);
95     streamid = avio_r8(pb);
96     avio_r8(pb);               /* counter not used */
97     reserved = avio_r8(pb);
98     flags    = avio_r8(pb);
99     length   = avio_rb16(pb);
100 
101     pts_flag = flags & 0x10;
102 
103     if (syncword != PVA_MAGIC) {
104         pva_log(s, AV_LOG_ERROR, "invalid syncword\n");
105         return AVERROR(EIO);
106     }
107     if (streamid != PVA_VIDEO_PAYLOAD && streamid != PVA_AUDIO_PAYLOAD) {
108         pva_log(s, AV_LOG_ERROR, "invalid streamid\n");
109         return AVERROR(EIO);
110     }
111     if (reserved != 0x55) {
112         pva_log(s, AV_LOG_WARNING, "expected reserved byte to be 0x55\n");
113     }
114     if (length > PVA_MAX_PAYLOAD_LENGTH) {
115         pva_log(s, AV_LOG_ERROR, "invalid payload length %u\n", length);
116         return AVERROR(EIO);
117     }
118 
119     if (streamid == PVA_VIDEO_PAYLOAD && pts_flag) {
120         pva_pts = avio_rb32(pb);
121         length -= 4;
122     } else if (streamid == PVA_AUDIO_PAYLOAD) {
123         /* PVA Audio Packets either start with a signaled PES packet or
124          * are a continuation of the previous PES packet. New PES packets
125          * always start at the beginning of a PVA Packet, never somewhere in
126          * the middle. */
127         if (!pvactx->continue_pes) {
128             int pes_signal, pes_header_data_length, pes_packet_length,
129                 pes_flags;
130             unsigned char pes_header_data[256];
131 
132             pes_signal             = avio_rb24(pb);
133             avio_r8(pb);
134             pes_packet_length      = avio_rb16(pb);
135             pes_flags              = avio_rb16(pb);
136             pes_header_data_length = avio_r8(pb);
137 
138             if (avio_feof(pb)) {
139                 return AVERROR_EOF;
140             }
141 
142             if (pes_signal != 1 || pes_header_data_length == 0) {
143                 pva_log(s, AV_LOG_WARNING, "expected non empty signaled PES packet, "
144                                           "trying to recover\n");
145                 avio_skip(pb, length - 9);
146                 if (!read_packet)
147                     return AVERROR(EIO);
148                 goto recover;
149             }
150 
151             ret = ffio_read_size(pb, pes_header_data, pes_header_data_length);
152             if (ret < 0)
153                 return ret;
154             length -= 9 + pes_header_data_length;
155 
156             pes_packet_length -= 3 + pes_header_data_length;
157 
158             pvactx->continue_pes = pes_packet_length;
159 
160             if (pes_flags & 0x80 && (pes_header_data[0] & 0xf0) == 0x20) {
161                 if (pes_header_data_length < 5) {
162                     pva_log(s, AV_LOG_ERROR, "header too short\n");
163                     avio_skip(pb, length);
164                     return AVERROR_INVALIDDATA;
165                 }
166                 pva_pts = ff_parse_pes_pts(pes_header_data);
167             }
168         }
169 
170         pvactx->continue_pes -= length;
171 
172         if (pvactx->continue_pes < 0) {
173             pva_log(s, AV_LOG_WARNING, "audio data corruption\n");
174             pvactx->continue_pes = 0;
175         }
176     }
177 
178     if (pva_pts != AV_NOPTS_VALUE)
179         av_add_index_entry(s->streams[streamid-1], startpos, pva_pts, 0, 0, AVINDEX_KEYFRAME);
180 
181     *pts   = pva_pts;
182     *len   = length;
183     *strid = streamid;
184     return 0;
185 }
186 
pva_read_packet(AVFormatContext *s, AVPacket *pkt)187 static int pva_read_packet(AVFormatContext *s, AVPacket *pkt) {
188     AVIOContext *pb = s->pb;
189     int64_t pva_pts;
190     int ret, length, streamid;
191 
192     if (read_part_of_packet(s, &pva_pts, &length, &streamid, 1) < 0 ||
193        (ret = av_get_packet(pb, pkt, length)) <= 0)
194         return AVERROR(EIO);
195 
196     pkt->stream_index = streamid - 1;
197     pkt->pts = pva_pts;
198 
199     return ret;
200 }
201 
pva_read_timestamp(struct AVFormatContext *s, int stream_index, int64_t *pos, int64_t pos_limit)202 static int64_t pva_read_timestamp(struct AVFormatContext *s, int stream_index,
203                                           int64_t *pos, int64_t pos_limit) {
204     AVIOContext *pb = s->pb;
205     PVAContext *pvactx = s->priv_data;
206     int length, streamid;
207     int64_t res = AV_NOPTS_VALUE;
208 
209     pos_limit = FFMIN(*pos+PVA_MAX_PAYLOAD_LENGTH*8, (uint64_t)*pos+pos_limit);
210 
211     while (*pos < pos_limit) {
212         res = AV_NOPTS_VALUE;
213         avio_seek(pb, *pos, SEEK_SET);
214 
215         pvactx->continue_pes = 0;
216         if (read_part_of_packet(s, &res, &length, &streamid, 0)) {
217             (*pos)++;
218             continue;
219         }
220         if (streamid - 1 != stream_index || res == AV_NOPTS_VALUE) {
221             *pos = avio_tell(pb) + length;
222             continue;
223         }
224         break;
225     }
226 
227     pvactx->continue_pes = 0;
228     return res;
229 }
230 
231 const AVInputFormat ff_pva_demuxer = {
232     .name           = "pva",
233     .long_name      = NULL_IF_CONFIG_SMALL("TechnoTrend PVA"),
234     .priv_data_size = sizeof(PVAContext),
235     .read_probe     = pva_probe,
236     .read_header    = pva_read_header,
237     .read_packet    = pva_read_packet,
238     .read_timestamp = pva_read_timestamp,
239 };
240