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 "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 
33 typedef struct PVAContext {
35 } PVAContext;
36 
37 static 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 
45 static 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 
92 recover:
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(EIO);
107  }
108  if (streamid != PVA_VIDEO_PAYLOAD && streamid != PVA_AUDIO_PAYLOAD) {
109  pva_log(s, AV_LOG_ERROR, "invalid streamid\n");
110  return AVERROR(EIO);
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(EIO);
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(EIO);
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(EIO);
196 
197  pkt->stream_index = streamid - 1;
198  pkt->pts = pva_pts;
199 
200  return ret;
201 }
202 
203 static 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;
208  int64_t res = AV_NOPTS_VALUE;
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 };
pva_probe
static int pva_probe(const AVProbeData *pd)
Definition: pva.c:45
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:59
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
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
ff_parse_pes_pts
static int64_t ff_parse_pes_pts(const uint8_t *buf)
Parse MPEG-PES five-byte timestamp.
Definition: mpeg.h:69
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
int64_t
long long int64_t
Definition: coverity.c:34
PVAContext::continue_pes
int continue_pes
Definition: pva.c:34
PVA_AUDIO_PAYLOAD
#define PVA_AUDIO_PAYLOAD
Definition: pva.c:30
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:454
AVINDEX_KEYFRAME
#define AVINDEX_KEYFRAME
Definition: avformat.h:610
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
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: avformat.c:853
ffstream
static av_always_inline FFStream * ffstream(AVStream *st)
Definition: internal.h:417
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:120
pva_read_packet
static int pva_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: pva.c:188
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:494
pts
static int64_t pts
Definition: transcode_aac.c:643
PVA_VIDEO_PAYLOAD
#define PVA_VIDEO_PAYLOAD
Definition: pva.c:29
avio_rb32
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:760
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
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:41
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AV_CODEC_ID_MP2
@ AV_CODEC_ID_MP2
Definition: codec_id.h:440
ff_pva_demuxer
const FFInputFormat ff_pva_demuxer
Definition: pva.c:232
FFStream::need_parsing
enum AVStreamParseType need_parsing
Definition: internal.h:386
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:766
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:550
PVAContext
Definition: pva.c:33
NULL
#define NULL
Definition: coverity.c:32
pva_check
static int pva_check(const uint8_t *p)
Definition: pva.c:37
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:84
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
AVPROBE_SCORE_EXTENSION
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:461
PVA_MAX_PAYLOAD_LENGTH
#define PVA_MAX_PAYLOAD_LENGTH
Definition: pva.c:28
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
avio_rb24
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:753
recover
static int recover(WtvContext *wtv, uint64_t broken_pos)
Try to seek over a broken chunk.
Definition: wtvdec.c:749
pva_log
#define pva_log
Definition: pva.c:82
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:106
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:41
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:602
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:203
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:515
avio_internal.h
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
demux.h
PVA_MAGIC
#define PVA_MAGIC
Definition: pva.c:31
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:103
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:743
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:230
avio_rb16
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:745
pos
unsigned int pos
Definition: spdifenc.c:413
avformat.h
AVPacket::stream_index
int stream_index
Definition: packet.h:524
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:317
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:499
FFInputFormat
Definition: demux.h:37
AVSTREAM_PARSE_FULL
@ AVSTREAM_PARSE_FULL
full parsing and repack
Definition: avformat.h:593
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
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:661
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:279
AV_CODEC_ID_MPEG2VIDEO
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition: codec_id.h:54
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:345