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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 {
33 } PVAContext;
34 
35 static int pva_probe(AVProbeData * pd) {
36  unsigned char *buf = pd->buf;
37 
38  if (AV_RB16(buf) == PVA_MAGIC && buf[2] && buf[2] < 3 && buf[4] == 0x55)
39  return AVPROBE_SCORE_MAX / 2;
40 
41  return 0;
42 }
43 
45  AVStream *st;
46 
47  if (!(st = avformat_new_stream(s, NULL)))
48  return AVERROR(ENOMEM);
52  avpriv_set_pts_info(st, 32, 1, 90000);
53  av_add_index_entry(st, 0, 0, 0, 0, AVINDEX_KEYFRAME);
54 
55  if (!(st = avformat_new_stream(s, NULL)))
56  return AVERROR(ENOMEM);
60  avpriv_set_pts_info(st, 33, 1, 90000);
61  av_add_index_entry(st, 0, 0, 0, 0, AVINDEX_KEYFRAME);
62 
63  /* the parameters will be extracted from the compressed bitstream */
64  return 0;
65 }
66 
67 #define pva_log if (read_packet) av_log
68 
69 static int read_part_of_packet(AVFormatContext *s, int64_t *pts,
70  int *len, int *strid, int read_packet) {
71  AVIOContext *pb = s->pb;
72  PVAContext *pvactx = s->priv_data;
73  int syncword, streamid, reserved, flags, length, pts_flag;
74  int64_t pva_pts = AV_NOPTS_VALUE, startpos;
75 
76 recover:
77  startpos = avio_tell(pb);
78 
79  syncword = avio_rb16(pb);
80  streamid = avio_r8(pb);
81  avio_r8(pb); /* counter not used */
82  reserved = avio_r8(pb);
83  flags = avio_r8(pb);
84  length = avio_rb16(pb);
85 
86  pts_flag = flags & 0x10;
87 
88  if (syncword != PVA_MAGIC) {
89  pva_log(s, AV_LOG_ERROR, "invalid syncword\n");
90  return AVERROR(EIO);
91  }
92  if (streamid != PVA_VIDEO_PAYLOAD && streamid != PVA_AUDIO_PAYLOAD) {
93  pva_log(s, AV_LOG_ERROR, "invalid streamid\n");
94  return AVERROR(EIO);
95  }
96  if (reserved != 0x55) {
97  pva_log(s, AV_LOG_WARNING, "expected reserved byte to be 0x55\n");
98  }
99  if (length > PVA_MAX_PAYLOAD_LENGTH) {
100  pva_log(s, AV_LOG_ERROR, "invalid payload length %u\n", length);
101  return AVERROR(EIO);
102  }
103 
104  if (streamid == PVA_VIDEO_PAYLOAD && pts_flag) {
105  pva_pts = avio_rb32(pb);
106  length -= 4;
107  } else if (streamid == PVA_AUDIO_PAYLOAD) {
108  /* PVA Audio Packets either start with a signaled PES packet or
109  * are a continuation of the previous PES packet. New PES packets
110  * always start at the beginning of a PVA Packet, never somewhere in
111  * the middle. */
112  if (!pvactx->continue_pes) {
113  int pes_signal, pes_header_data_length, pes_packet_length,
114  pes_flags;
115  unsigned char pes_header_data[256];
116 
117  pes_signal = avio_rb24(pb);
118  avio_r8(pb);
119  pes_packet_length = avio_rb16(pb);
120  pes_flags = avio_rb16(pb);
121  pes_header_data_length = avio_r8(pb);
122 
123  if (pes_signal != 1) {
124  pva_log(s, AV_LOG_WARNING, "expected signaled PES packet, "
125  "trying to recover\n");
126  avio_skip(pb, length - 9);
127  if (!read_packet)
128  return AVERROR(EIO);
129  goto recover;
130  }
131 
132  avio_read(pb, pes_header_data, pes_header_data_length);
133  length -= 9 + pes_header_data_length;
134 
135  pes_packet_length -= 3 + pes_header_data_length;
136 
137  pvactx->continue_pes = pes_packet_length;
138 
139  if (pes_flags & 0x80 && (pes_header_data[0] & 0xf0) == 0x20)
140  pva_pts = ff_parse_pes_pts(pes_header_data);
141  }
142 
143  pvactx->continue_pes -= length;
144 
145  if (pvactx->continue_pes < 0) {
146  pva_log(s, AV_LOG_WARNING, "audio data corruption\n");
147  pvactx->continue_pes = 0;
148  }
149  }
150 
151  if (pva_pts != AV_NOPTS_VALUE)
152  av_add_index_entry(s->streams[streamid-1], startpos, pva_pts, 0, 0, AVINDEX_KEYFRAME);
153 
154  *pts = pva_pts;
155  *len = length;
156  *strid = streamid;
157  return 0;
158 }
159 
161  AVIOContext *pb = s->pb;
162  int64_t pva_pts;
163  int ret, length, streamid;
164 
165  if (read_part_of_packet(s, &pva_pts, &length, &streamid, 1) < 0 ||
166  (ret = av_get_packet(pb, pkt, length)) <= 0)
167  return AVERROR(EIO);
168 
169  pkt->stream_index = streamid - 1;
170  pkt->pts = pva_pts;
171 
172  return ret;
173 }
174 
175 static int64_t pva_read_timestamp(struct AVFormatContext *s, int stream_index,
176  int64_t *pos, int64_t pos_limit) {
177  AVIOContext *pb = s->pb;
178  PVAContext *pvactx = s->priv_data;
179  int length, streamid;
180  int64_t res = AV_NOPTS_VALUE;
181 
182  pos_limit = FFMIN(*pos+PVA_MAX_PAYLOAD_LENGTH*8, (uint64_t)*pos+pos_limit);
183 
184  while (*pos < pos_limit) {
185  res = AV_NOPTS_VALUE;
186  avio_seek(pb, *pos, SEEK_SET);
187 
188  pvactx->continue_pes = 0;
189  if (read_part_of_packet(s, &res, &length, &streamid, 0)) {
190  (*pos)++;
191  continue;
192  }
193  if (streamid - 1 != stream_index || res == AV_NOPTS_VALUE) {
194  *pos = avio_tell(pb) + length;
195  continue;
196  }
197  break;
198  }
199 
200  pvactx->continue_pes = 0;
201  return res;
202 }
203 
205  .name = "pva",
206  .long_name = NULL_IF_CONFIG_SMALL("TechnoTrend PVA"),
207  .priv_data_size = sizeof(PVAContext),
211  .read_timestamp = pva_read_timestamp,
212 };
static int pva_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: pva.c:160
Bytestream IO Context.
Definition: avio.h:68
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:1393
#define PVA_MAGIC
Definition: pva.c:29
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:3283
static int pva_read_header(AVFormatContext *s)
Definition: pva.c:44
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:186
int continue_pes
Definition: pva.c:32
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:562
Format I/O context.
Definition: avformat.h:828
#define PVA_VIDEO_PAYLOAD
Definition: pva.c:27
static int read_part_of_packet(AVFormatContext *s, int64_t *pts, int *len, int *strid, int read_packet)
Definition: pva.c:69
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:577
enum AVStreamParseType need_parsing
Definition: avformat.h:775
AVStream ** streams
Definition: avformat.h:876
static int flags
Definition: log.c:42
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:218
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:219
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:446
#define AV_RB16
Definition: intreadwrite.h:53
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
AVStream * avformat_new_stream(AVFormatContext *s, AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2736
Definition: pva.c:31
static int64_t ff_parse_pes_pts(const uint8_t *buf)
Parse MPEG-PES five-byte timestamp.
Definition: mpeg.h:66
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:437
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:641
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:340
#define PVA_AUDIO_PAYLOAD
Definition: pva.c:28
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:570
static int64_t pva_read_timestamp(struct AVFormatContext *s, int stream_index, int64_t *pos, int64_t pos_limit)
Definition: pva.c:175
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:54
AVInputFormat ff_pva_demuxer
Definition: pva.c:204
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:210
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:100
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:536
#define PVA_MAX_PAYLOAD_LENGTH
Definition: pva.c:26
Stream structure.
Definition: avformat.h:622
NULL
Definition: eval.c:52
enum AVMediaType codec_type
Definition: avcodec.h:1347
enum AVCodecID codec_id
Definition: avcodec.h:1350
AVIOContext * pb
I/O context.
Definition: avformat.h:861
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
This structure contains the data a format has to probe a file.
Definition: avformat.h:338
full parsing and repack
Definition: avformat.h:576
Main libavformat public API header.
#define pva_log
Definition: pva.c:67
static int pva_probe(AVProbeData *pd)
Definition: pva.c:35
int len
void * priv_data
Format private data.
Definition: avformat.h:848
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:455
int stream_index
Definition: avcodec.h:917
This structure stores compressed data.
Definition: avcodec.h:898
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:908