]> git.sesse.net Git - ffmpeg/blob - libavformat/pva.c
validate streamid before use
[ffmpeg] / libavformat / pva.c
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
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')
28
29 typedef struct {
30     int continue_pes;
31 } PVAContext;
32
33 static int pva_probe(AVProbeData * pd) {
34     unsigned char *buf = pd->buf;
35
36     if (AV_RB16(buf) == PVA_MAGIC && buf[2] && buf[2] < 3 && buf[4] == 0x55)
37         return AVPROBE_SCORE_MAX / 2;
38
39     return 0;
40 }
41
42 static int pva_read_header(AVFormatContext *s, AVFormatParameters *ap) {
43     AVStream *st;
44
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);
51
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);
58
59     /* the parameters will be extracted from the compressed bitstream */
60     return 0;
61 }
62
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;
68
69     syncword = get_be16(pb);
70     streamid = get_byte(pb);
71     get_byte(pb);               /* counter not used */
72     reserved = get_byte(pb);
73     flags    = get_byte(pb);
74     length   = get_be16(pb);
75
76     pts_flag  = flags & 0x10;
77
78     if (syncword != PVA_MAGIC) {
79         av_log(s, AV_LOG_ERROR, "invalid syncword\n");
80         return AVERROR(EIO);
81     }
82     if (streamid != PVA_VIDEO_PAYLOAD && streamid != PVA_AUDIO_PAYLOAD) {
83         av_log(s, AV_LOG_ERROR, "invalid streamid\n");
84         return AVERROR(EIO);
85     }
86     if (reserved != 0x55) {
87         av_log(s, AV_LOG_WARNING, "expected reserved byte to be 0x55\n");
88     }
89     if (length > PVA_MAX_PAYLOAD_LENGTH) {
90         av_log(s, AV_LOG_ERROR, "invalid payload length %u\n", length);
91         return AVERROR(EIO);
92     }
93
94     if (streamid == PVA_VIDEO_PAYLOAD && pts_flag) {
95         pva_pts = get_be32(pb);
96         length -= 4;
97     } else if (streamid == PVA_AUDIO_PAYLOAD) {
98         /* PVA Audio Packets either start with a signaled PES packet or
99          * are a continuation of the previous PES packet. New PES packets
100          * always start at the beginning of a PVA Packet, never somewhere in
101          * the middle. */
102         if (!pvactx->continue_pes) {
103             int pes_signal, pes_header_data_length, pes_packet_length,
104                 pes_flags;
105             unsigned char pes_header_data[256];
106
107             pes_signal             = get_be24(pb);
108             get_byte(pb);
109             pes_packet_length      = get_be16(pb);
110             pes_flags              = get_be16(pb);
111             pes_header_data_length = get_byte(pb);
112
113             if (pes_signal != 1) {
114                 av_log(s, AV_LOG_ERROR, "expected signaled PES packet\n");
115                 return AVERROR(EIO);
116             }
117
118             get_buffer(pb, pes_header_data, pes_header_data_length);
119             length -= 9 + pes_header_data_length;
120
121             pes_packet_length -= 3 + pes_header_data_length;
122
123             pvactx->continue_pes = pes_packet_length;
124
125             if (pes_flags & 0x80 && (pes_header_data[0] & 0xf0) == 0x20) {
126                 pva_pts  = ((long long) *pes_header_data & 0x0e) << 29;
127                 pva_pts += (AV_RB16(pes_header_data+1) >> 1) << 15;
128                 pva_pts +=  AV_RB16(pes_header_data+3) >> 1;
129             }
130         }
131
132         pvactx->continue_pes -= length;
133
134         if (pvactx->continue_pes < 0) {
135             av_log(s, AV_LOG_WARNING, "audio data corruption\n");
136             pvactx->continue_pes = 0;
137         }
138     }
139
140     if ((ret = av_get_packet(pb, pkt, length)) <= 0)
141         return AVERROR(EIO);
142
143     pkt->stream_index = streamid - 1;
144     if (pva_pts)
145         pkt->pts = pva_pts;
146
147     return ret;
148 }
149
150 AVInputFormat pva_demuxer = {
151     "pva",
152     "pva file and stream format",
153     sizeof(PVAContext),
154     pva_probe,
155     pva_read_header,
156     pva_read_packet,
157 };