FFmpeg
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 "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 {
34 } PVAContext;
35 
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 
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)
54 
55  return AVPROBE_SCORE_MAX / 4;
56 }
57 
59  AVStream *st;
60 
61  if (!(st = avformat_new_stream(s, NULL)))
62  return AVERROR(ENOMEM);
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);
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 
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 
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 
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 
232  .name = "pva",
233  .long_name = NULL_IF_CONFIG_SMALL("TechnoTrend PVA"),
234  .priv_data_size = sizeof(PVAContext),
239 };
pva_probe
static int pva_probe(const AVProbeData *pd)
Definition: pva.c:44
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
pva_read_header
static int pva_read_header(AVFormatContext *s)
Definition: pva.c:58
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:768
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
ff_parse_pes_pts
static int64_t ff_parse_pes_pts(const uint8_t *buf)
Parse MPEG-PES five-byte timestamp.
Definition: mpeg.h:68
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
PVAContext::continue_pes
int continue_pes
Definition: pva.c:33
PVA_AUDIO_PAYLOAD
#define PVA_AUDIO_PAYLOAD
Definition: pva.c:29
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:450
AVINDEX_KEYFRAME
#define AVINDEX_KEYFRAME
Definition: avformat.h:809
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:459
ffstream
static av_always_inline FFStream * ffstream(AVStream *st)
Definition: internal.h:432
av_add_index_entry
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:117
pva_read_packet
static int pva_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: pva.c:187
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:504
pts
static int64_t pts
Definition: transcode_aac.c:653
PVA_VIDEO_PAYLOAD
#define PVA_VIDEO_PAYLOAD
Definition: pva.c:28
avio_rb32
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:790
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
AVInputFormat
Definition: avformat.h:650
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:655
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:449
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AV_CODEC_ID_MP2
@ AV_CODEC_ID_MP2
Definition: codec_id.h:423
FFStream::need_parsing
enum AVStreamParseType need_parsing
Definition: internal.h:405
AVFormatContext
Format I/O context.
Definition: avformat.h:1200
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1095
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:527
PVAContext
Definition: pva.c:32
NULL
#define NULL
Definition: coverity.c:32
read_probe
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
pva_check
static int pva_check(const uint8_t *p)
Definition: pva.c:36
read_part_of_packet
static int read_part_of_packet(AVFormatContext *s, int64_t *pts, int *len, int *strid, int read_packet)
Definition: pva.c:83
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:447
AVPROBE_SCORE_EXTENSION
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:457
PVA_MAX_PAYLOAD_LENGTH
#define PVA_MAX_PAYLOAD_LENGTH
Definition: pva.c:27
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
avio_rb24
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:783
recover
static int recover(WtvContext *wtv, uint64_t broken_pos)
Try to seek over a broken chunk.
Definition: wtvdec.c:758
pva_log
#define pva_log
Definition: pva.c:81
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:632
mpeg.h
pva_read_timestamp
static int64_t pva_read_timestamp(struct AVFormatContext *s, int stream_index, int64_t *pos, int64_t pos_limit)
Definition: pva.c:202
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:366
avio_internal.h
ff_pva_demuxer
const AVInputFormat ff_pva_demuxer
Definition: pva.c:231
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
PVA_MAGIC
#define PVA_MAGIC
Definition: pva.c:30
len
int len
Definition: vorbis_enc_data.h:426
av_get_packet
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:197
ret
ret
Definition: filter_design.txt:187
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
AVStream
Stream structure.
Definition: avformat.h:935
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:260
avio_rb16
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:775
pos
unsigned int pos
Definition: spdifenc.c:412
avformat.h
avpriv_set_pts_info
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: utils.c:1196
AVPacket::stream_index
int stream_index
Definition: packet.h:375
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:347
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
AVPacket
This structure stores compressed data.
Definition: packet.h:350
AVSTREAM_PARSE_FULL
@ AVSTREAM_PARSE_FULL
full parsing and repack
Definition: avformat.h:792
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
ffio_read_size
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:691
read_timestamp
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:273
AV_CODEC_ID_MPEG2VIDEO
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition: codec_id.h:52
AV_RB16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:98
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:375