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 "internal.h"
24 #include "mpeg.h"
25 
26 #define PVA_MAX_PAYLOAD_LENGTH 0x17f8
27 #define PVA_VIDEO_PAYLOAD 0x01
28 #define PVA_AUDIO_PAYLOAD 0x02
29 #define PVA_MAGIC (('A' << 8) + 'V')
30 
31 typedef struct PVAContext {
33 } PVAContext;
34 
35 static int pva_check(const uint8_t *p) {
36  int length = AV_RB16(p + 6);
37  if (AV_RB16(p) != PVA_MAGIC || !p[2] || p[2] > 2 || p[4] != 0x55 ||
38  (p[5] & 0xe0) || length > PVA_MAX_PAYLOAD_LENGTH)
39  return -1;
40  return length + 8;
41 }
42 
43 static int pva_probe(const AVProbeData * pd) {
44  const unsigned char *buf = pd->buf;
45  int len = pva_check(buf);
46 
47  if (len < 0)
48  return 0;
49 
50  if (pd->buf_size >= len + 8 &&
51  pva_check(buf + len) >= 0)
53 
54  return AVPROBE_SCORE_MAX / 4;
55 }
56 
58  AVStream *st;
59 
60  if (!(st = avformat_new_stream(s, NULL)))
61  return AVERROR(ENOMEM);
65  avpriv_set_pts_info(st, 32, 1, 90000);
66  av_add_index_entry(st, 0, 0, 0, 0, AVINDEX_KEYFRAME);
67 
68  if (!(st = avformat_new_stream(s, NULL)))
69  return AVERROR(ENOMEM);
73  avpriv_set_pts_info(st, 33, 1, 90000);
74  av_add_index_entry(st, 0, 0, 0, 0, AVINDEX_KEYFRAME);
75 
76  /* the parameters will be extracted from the compressed bitstream */
77  return 0;
78 }
79 
80 #define pva_log if (read_packet) av_log
81 
82 static int read_part_of_packet(AVFormatContext *s, int64_t *pts,
83  int *len, int *strid, int read_packet) {
84  AVIOContext *pb = s->pb;
85  PVAContext *pvactx = s->priv_data;
86  int syncword, streamid, reserved, flags, length, pts_flag;
87  int64_t pva_pts = AV_NOPTS_VALUE, startpos;
88  int ret;
89 
90 recover:
91  startpos = avio_tell(pb);
92 
93  syncword = avio_rb16(pb);
94  streamid = avio_r8(pb);
95  avio_r8(pb); /* counter not used */
96  reserved = avio_r8(pb);
97  flags = avio_r8(pb);
98  length = avio_rb16(pb);
99 
100  pts_flag = flags & 0x10;
101 
102  if (syncword != PVA_MAGIC) {
103  pva_log(s, AV_LOG_ERROR, "invalid syncword\n");
104  return AVERROR(EIO);
105  }
106  if (streamid != PVA_VIDEO_PAYLOAD && streamid != PVA_AUDIO_PAYLOAD) {
107  pva_log(s, AV_LOG_ERROR, "invalid streamid\n");
108  return AVERROR(EIO);
109  }
110  if (reserved != 0x55) {
111  pva_log(s, AV_LOG_WARNING, "expected reserved byte to be 0x55\n");
112  }
114  pva_log(s, AV_LOG_ERROR, "invalid payload length %u\n", length);
115  return AVERROR(EIO);
116  }
117 
118  if (streamid == PVA_VIDEO_PAYLOAD && pts_flag) {
119  pva_pts = avio_rb32(pb);
120  length -= 4;
121  } else if (streamid == PVA_AUDIO_PAYLOAD) {
122  /* PVA Audio Packets either start with a signaled PES packet or
123  * are a continuation of the previous PES packet. New PES packets
124  * always start at the beginning of a PVA Packet, never somewhere in
125  * the middle. */
126  if (!pvactx->continue_pes) {
127  int pes_signal, pes_header_data_length, pes_packet_length,
128  pes_flags;
129  unsigned char pes_header_data[256];
130 
131  pes_signal = avio_rb24(pb);
132  avio_r8(pb);
133  pes_packet_length = avio_rb16(pb);
134  pes_flags = avio_rb16(pb);
135  pes_header_data_length = avio_r8(pb);
136 
137  if (avio_feof(pb)) {
138  return AVERROR_EOF;
139  }
140 
141  if (pes_signal != 1 || pes_header_data_length == 0) {
142  pva_log(s, AV_LOG_WARNING, "expected non empty signaled PES packet, "
143  "trying to recover\n");
144  avio_skip(pb, length - 9);
145  if (!read_packet)
146  return AVERROR(EIO);
147  goto recover;
148  }
149 
150  ret = avio_read(pb, pes_header_data, pes_header_data_length);
151  if (ret != pes_header_data_length)
152  return ret < 0 ? ret : AVERROR_INVALIDDATA;
153  length -= 9 + pes_header_data_length;
154 
155  pes_packet_length -= 3 + pes_header_data_length;
156 
157  pvactx->continue_pes = pes_packet_length;
158 
159  if (pes_flags & 0x80 && (pes_header_data[0] & 0xf0) == 0x20) {
160  if (pes_header_data_length < 5) {
161  pva_log(s, AV_LOG_ERROR, "header too short\n");
162  avio_skip(pb, length);
163  return AVERROR_INVALIDDATA;
164  }
165  pva_pts = ff_parse_pes_pts(pes_header_data);
166  }
167  }
168 
169  pvactx->continue_pes -= length;
170 
171  if (pvactx->continue_pes < 0) {
172  pva_log(s, AV_LOG_WARNING, "audio data corruption\n");
173  pvactx->continue_pes = 0;
174  }
175  }
176 
177  if (pva_pts != AV_NOPTS_VALUE)
178  av_add_index_entry(s->streams[streamid-1], startpos, pva_pts, 0, 0, AVINDEX_KEYFRAME);
179 
180  *pts = pva_pts;
181  *len = length;
182  *strid = streamid;
183  return 0;
184 }
185 
187  AVIOContext *pb = s->pb;
188  int64_t pva_pts;
189  int ret, length, streamid;
190 
191  if (read_part_of_packet(s, &pva_pts, &length, &streamid, 1) < 0 ||
192  (ret = av_get_packet(pb, pkt, length)) <= 0)
193  return AVERROR(EIO);
194 
195  pkt->stream_index = streamid - 1;
196  pkt->pts = pva_pts;
197 
198  return ret;
199 }
200 
201 static int64_t pva_read_timestamp(struct AVFormatContext *s, int stream_index,
202  int64_t *pos, int64_t pos_limit) {
203  AVIOContext *pb = s->pb;
204  PVAContext *pvactx = s->priv_data;
205  int length, streamid;
206  int64_t res = AV_NOPTS_VALUE;
207 
208  pos_limit = FFMIN(*pos+PVA_MAX_PAYLOAD_LENGTH*8, (uint64_t)*pos+pos_limit);
209 
210  while (*pos < pos_limit) {
211  res = AV_NOPTS_VALUE;
212  avio_seek(pb, *pos, SEEK_SET);
213 
214  pvactx->continue_pes = 0;
215  if (read_part_of_packet(s, &res, &length, &streamid, 0)) {
216  (*pos)++;
217  continue;
218  }
219  if (streamid - 1 != stream_index || res == AV_NOPTS_VALUE) {
220  *pos = avio_tell(pb) + length;
221  continue;
222  }
223  break;
224  }
225 
226  pvactx->continue_pes = 0;
227  return res;
228 }
229 
231  .name = "pva",
232  .long_name = NULL_IF_CONFIG_SMALL("TechnoTrend PVA"),
233  .priv_data_size = sizeof(PVAContext),
237  .read_timestamp = pva_read_timestamp,
238 };
pva_probe
static int pva_probe(const AVProbeData *pd)
Definition: pva.c:43
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
pva_read_header
static int pva_read_header(AVFormatContext *s)
Definition: pva.c:57
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:4480
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3953
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:55
PVAContext::continue_pes
int continue_pes
Definition: pva.c:32
PVA_AUDIO_PAYLOAD
#define PVA_AUDIO_PAYLOAD
Definition: pva.c:28
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:449
ff_pva_demuxer
AVInputFormat ff_pva_demuxer
Definition: pva.c:230
AVINDEX_KEYFRAME
#define AVINDEX_KEYFRAME
Definition: avformat.h:808
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:458
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: utils.c:2056
pva_read_packet
static int pva_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: pva.c:186
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
pts
static int64_t pts
Definition: transcode_aac.c:647
PVA_VIDEO_PAYLOAD
#define PVA_VIDEO_PAYLOAD
Definition: pva.c:27
avio_rb32
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:800
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
buf
void * buf
Definition: avisynth_c.h:766
AVInputFormat
Definition: avformat.h:640
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:645
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:448
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AV_CODEC_ID_MP2
@ AV_CODEC_ID_MP2
Definition: avcodec.h:564
AVStream::need_parsing
enum AVStreamParseType need_parsing
Definition: avformat.h:1088
AVFormatContext
Format I/O context.
Definition: avformat.h:1342
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1017
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:530
PVAContext
Definition: pva.c:31
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:35
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:82
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:446
AVPROBE_SCORE_EXTENSION
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:456
PVA_MAX_PAYLOAD_LENGTH
#define PVA_MAX_PAYLOAD_LENGTH
Definition: pva.c:26
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
avio_rb24
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:793
recover
static int recover(WtvContext *wtv, uint64_t broken_pos)
Try to seek over a broken chunk.
Definition: wtvdec.c:767
pva_log
#define pva_log
Definition: pva.c:80
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:188
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *s, 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:4910
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:638
FFMIN
#define FFMIN(a, b)
Definition: common.h:96
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:201
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1470
uint8_t
uint8_t
Definition: audio_convert.c:194
PVA_MAGIC
#define PVA_MAGIC
Definition: pva.c:29
len
int len
Definition: vorbis_enc_data.h:452
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:313
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:870
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:246
avio_rb16
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:785
avformat.h
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:647
AVPacket::stream_index
int stream_index
Definition: avcodec.h:1479
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:331
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: avcodec.h:3957
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1454
AVSTREAM_PARSE_FULL
@ AVSTREAM_PARSE_FULL
full parsing and repack
Definition: avformat.h:791
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:565
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
length
const char int length
Definition: avisynth_c.h:860
AV_CODEC_ID_MPEG2VIDEO
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:220
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:94
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:358