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