FFmpeg
Loading...
Searching...
No Matches
pva.c
Go to the documentation of this file.
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 "demux.h"
25#include "internal.h"
26#include "mpeg.h"
27
28#define PVA_MAX_PAYLOAD_LENGTH 0x17f8
29#define PVA_VIDEO_PAYLOAD 0x01
30#define PVA_AUDIO_PAYLOAD 0x02
31#define PVA_MAGIC (('A' << 8) + 'V')
32
33typedef struct PVAContext {
36
37static int pva_check(const uint8_t *p) {
38 int length = AV_RB16(p + 6);
39 if (AV_RB16(p) != PVA_MAGIC || !p[2] || p[2] > 2 || p[4] != 0x55 ||
40 (p[5] & 0xe0) || length > PVA_MAX_PAYLOAD_LENGTH)
41 return -1;
42 return length + 8;
43}
44
45static int pva_probe(const AVProbeData * pd) {
46 const unsigned char *buf = pd->buf;
47 int len = pva_check(buf);
48
49 if (len < 0)
50 return 0;
51
52 if (pd->buf_size >= len + 8 &&
53 pva_check(buf + len) >= 0)
55
56 return AVPROBE_SCORE_MAX / 4;
57}
58
60 AVStream *st;
61
62 if (!(st = avformat_new_stream(s, NULL)))
63 return AVERROR(ENOMEM);
67 avpriv_set_pts_info(st, 32, 1, 90000);
68 av_add_index_entry(st, 0, 0, 0, 0, AVINDEX_KEYFRAME);
69
70 if (!(st = avformat_new_stream(s, NULL)))
71 return AVERROR(ENOMEM);
75 avpriv_set_pts_info(st, 33, 1, 90000);
76 av_add_index_entry(st, 0, 0, 0, 0, AVINDEX_KEYFRAME);
77
78 /* the parameters will be extracted from the compressed bitstream */
79 return 0;
80}
81
82#define pva_log if (read_packet) av_log
83
85 int *len, int *strid, int read_packet) {
86 AVIOContext *pb = s->pb;
87 PVAContext *pvactx = s->priv_data;
88 int syncword, streamid, reserved, flags, length, pts_flag;
89 int64_t pva_pts = AV_NOPTS_VALUE, startpos;
90 int ret;
91
93 startpos = avio_tell(pb);
94
95 syncword = avio_rb16(pb);
96 streamid = avio_r8(pb);
97 avio_r8(pb); /* counter not used */
98 reserved = avio_r8(pb);
99 flags = avio_r8(pb);
100 length = avio_rb16(pb);
101
102 pts_flag = flags & 0x10;
103
104 if (syncword != PVA_MAGIC) {
105 pva_log(s, AV_LOG_ERROR, "invalid syncword\n");
106 return AVERROR_INVALIDDATA;
107 }
108 if (streamid != PVA_VIDEO_PAYLOAD && streamid != PVA_AUDIO_PAYLOAD) {
109 pva_log(s, AV_LOG_ERROR, "invalid streamid\n");
110 return AVERROR_INVALIDDATA;
111 }
112 if (reserved != 0x55) {
113 pva_log(s, AV_LOG_WARNING, "expected reserved byte to be 0x55\n");
114 }
115 if (length > PVA_MAX_PAYLOAD_LENGTH) {
116 pva_log(s, AV_LOG_ERROR, "invalid payload length %u\n", length);
117 return AVERROR_INVALIDDATA;
118 }
119
120 if (streamid == PVA_VIDEO_PAYLOAD && pts_flag) {
121 pva_pts = avio_rb32(pb);
122 length -= 4;
123 } else if (streamid == PVA_AUDIO_PAYLOAD) {
124 /* PVA Audio Packets either start with a signaled PES packet or
125 * are a continuation of the previous PES packet. New PES packets
126 * always start at the beginning of a PVA Packet, never somewhere in
127 * the middle. */
128 if (!pvactx->continue_pes) {
129 int pes_signal, pes_header_data_length, pes_packet_length,
130 pes_flags;
131 unsigned char pes_header_data[256];
132
133 pes_signal = avio_rb24(pb);
134 avio_r8(pb);
135 pes_packet_length = avio_rb16(pb);
136 pes_flags = avio_rb16(pb);
137 pes_header_data_length = avio_r8(pb);
138
139 if (avio_feof(pb)) {
140 return AVERROR_EOF;
141 }
142
143 if (pes_signal != 1 || pes_header_data_length == 0) {
144 pva_log(s, AV_LOG_WARNING, "expected non empty signaled PES packet, "
145 "trying to recover\n");
146 avio_skip(pb, length - 9);
147 if (!read_packet)
148 return AVERROR_INVALIDDATA;
149 goto recover;
150 }
151
152 ret = ffio_read_size(pb, pes_header_data, pes_header_data_length);
153 if (ret < 0)
154 return ret;
155 length -= 9 + pes_header_data_length;
156
157 pes_packet_length -= 3 + pes_header_data_length;
158
159 pvactx->continue_pes = pes_packet_length;
160
161 if (pes_flags & 0x80 && (pes_header_data[0] & 0xf0) == 0x20) {
162 if (pes_header_data_length < 5) {
163 pva_log(s, AV_LOG_ERROR, "header too short\n");
164 avio_skip(pb, length);
165 return AVERROR_INVALIDDATA;
166 }
167 pva_pts = ff_parse_pes_pts(pes_header_data);
168 }
169 }
170
171 pvactx->continue_pes -= length;
172
173 if (pvactx->continue_pes < 0) {
174 pva_log(s, AV_LOG_WARNING, "audio data corruption\n");
175 pvactx->continue_pes = 0;
176 }
177 }
178
179 if (pva_pts != AV_NOPTS_VALUE)
180 av_add_index_entry(s->streams[streamid-1], startpos, pva_pts, 0, 0, AVINDEX_KEYFRAME);
181
182 *pts = pva_pts;
183 *len = length;
184 *strid = streamid;
185 return 0;
186}
187
189 AVIOContext *pb = s->pb;
190 int64_t pva_pts;
191 int ret, length, streamid;
192
193 if (read_part_of_packet(s, &pva_pts, &length, &streamid, 1) < 0 ||
194 (ret = av_get_packet(pb, pkt, length)) <= 0)
195 return AVERROR_INVALIDDATA;
196
197 pkt->stream_index = streamid - 1;
198 pkt->pts = pva_pts;
199
200 return ret;
201}
202
203static int64_t pva_read_timestamp(struct AVFormatContext *s, int stream_index,
204 int64_t *pos, int64_t pos_limit) {
205 AVIOContext *pb = s->pb;
206 PVAContext *pvactx = s->priv_data;
207 int length, streamid;
209
210 pos_limit = FFMIN(*pos+PVA_MAX_PAYLOAD_LENGTH*8, (uint64_t)*pos+pos_limit);
211
212 while (*pos < pos_limit) {
213 res = AV_NOPTS_VALUE;
214 avio_seek(pb, *pos, SEEK_SET);
215
216 pvactx->continue_pes = 0;
217 if (read_part_of_packet(s, &res, &length, &streamid, 0)) {
218 (*pos)++;
219 continue;
220 }
221 if (streamid - 1 != stream_index || res == AV_NOPTS_VALUE) {
222 *pos = avio_tell(pb) + length;
223 continue;
224 }
225 break;
226 }
227
228 pvactx->continue_pes = 0;
229 return res;
230}
231
233 .p.name = "pva",
234 .p.long_name = NULL_IF_CONFIG_SMALL("TechnoTrend PVA"),
235 .priv_data_size = sizeof(PVAContext),
240};
const FFInputFormat ff_pva_demuxer
Definition pva.c:232
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVINDEX_KEYFRAME
Definition avformat.h:628
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition avformat.h:481
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition utils.c:98
@ AVSTREAM_PARSE_FULL
full parsing and repack
Definition avformat.h:611
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
unsigned int avio_rb16(AVIOContext *s)
Definition aviobuf.c:749
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
unsigned int avio_rb24(AVIOContext *s)
Definition aviobuf.c:757
unsigned int avio_rb32(AVIOContext *s)
Definition aviobuf.c:764
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:665
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_MP2
Definition codec_id.h:453
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition codec_id.h:52
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp, int size, int distance, int flags)
Add an index entry into a sorted list.
Definition seek.c:122
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define AV_RB16(p)
static av_always_inline FFStream * ffstream(AVStream *st)
Definition internal.h:365
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define FFMIN(a, b)
Definition macros.h:49
static int64_t ff_parse_pes_pts(const uint8_t *buf)
Parse MPEG-PES five-byte timestamp.
Definition mpeg.h:69
static int64_t pva_read_timestamp(struct AVFormatContext *s, int stream_index, int64_t *pos, int64_t pos_limit)
Definition pva.c:203
#define PVA_AUDIO_PAYLOAD
Definition pva.c:30
#define PVA_MAX_PAYLOAD_LENGTH
Definition pva.c:28
static int pva_read_header(AVFormatContext *s)
Definition pva.c:59
#define pva_log
Definition pva.c:82
static int read_part_of_packet(AVFormatContext *s, int64_t *pts, int *len, int *strid, int read_packet)
Definition pva.c:84
static int pva_check(const uint8_t *p)
Definition pva.c:37
#define PVA_MAGIC
Definition pva.c:31
static int pva_probe(const AVProbeData *pd)
Definition pva.c:45
#define PVA_VIDEO_PAYLOAD
Definition pva.c:29
static int pva_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition pva.c:188
static int64_t read_timestamp(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit, int64_t(*read_timestamp)(struct AVFormatContext *, int, int64_t *, int64_t))
Definition seek.c:281
unsigned int pos
Definition spdifenc.c:431
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
int buf_size
Size of buf except extra allocated bytes.
Definition avformat.h:474
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition avformat.h:473
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
enum AVStreamParseType need_parsing
Definition internal.h:321
int continue_pes
Definition pva.c:34
static int64_t pts
int len
static int recover(WtvContext *wtv, uint64_t broken_pos)
Try to seek over a broken chunk.
Definition wtvdec.c:717