Libav
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)
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 (avio_feof(pb)) {
124  return AVERROR_EOF;
125  }
126 
127  if (pes_signal != 1) {
128  pva_log(s, AV_LOG_WARNING, "expected signaled PES packet, "
129  "trying to recover\n");
130  avio_skip(pb, length - 9);
131  if (!read_packet)
132  return AVERROR(EIO);
133  goto recover;
134  }
135 
136  avio_read(pb, pes_header_data, pes_header_data_length);
137  length -= 9 + pes_header_data_length;
138 
139  pes_packet_length -= 3 + pes_header_data_length;
140 
141  pvactx->continue_pes = pes_packet_length;
142 
143  if (pes_flags & 0x80 && (pes_header_data[0] & 0xf0) == 0x20)
144  pva_pts = ff_parse_pes_pts(pes_header_data);
145  }
146 
147  pvactx->continue_pes -= length;
148 
149  if (pvactx->continue_pes < 0) {
150  pva_log(s, AV_LOG_WARNING, "audio data corruption\n");
151  pvactx->continue_pes = 0;
152  }
153  }
154 
155  if (pva_pts != AV_NOPTS_VALUE)
156  av_add_index_entry(s->streams[streamid-1], startpos, pva_pts, 0, 0, AVINDEX_KEYFRAME);
157 
158  *pts = pva_pts;
159  *len = length;
160  *strid = streamid;
161  return 0;
162 }
163 
165  AVIOContext *pb = s->pb;
166  int64_t pva_pts;
167  int ret, length, streamid;
168 
169  if (read_part_of_packet(s, &pva_pts, &length, &streamid, 1) < 0 ||
170  (ret = av_get_packet(pb, pkt, length)) <= 0)
171  return AVERROR(EIO);
172 
173  pkt->stream_index = streamid - 1;
174  pkt->pts = pva_pts;
175 
176  return ret;
177 }
178 
179 static int64_t pva_read_timestamp(struct AVFormatContext *s, int stream_index,
180  int64_t *pos, int64_t pos_limit) {
181  AVIOContext *pb = s->pb;
182  PVAContext *pvactx = s->priv_data;
183  int length, streamid;
184  int64_t res = AV_NOPTS_VALUE;
185 
186  pos_limit = FFMIN(*pos+PVA_MAX_PAYLOAD_LENGTH*8, (uint64_t)*pos+pos_limit);
187 
188  while (*pos < pos_limit) {
189  res = AV_NOPTS_VALUE;
190  avio_seek(pb, *pos, SEEK_SET);
191 
192  pvactx->continue_pes = 0;
193  if (read_part_of_packet(s, &res, &length, &streamid, 0)) {
194  (*pos)++;
195  continue;
196  }
197  if (streamid - 1 != stream_index || res == AV_NOPTS_VALUE) {
198  *pos = avio_tell(pb) + length;
199  continue;
200  }
201  break;
202  }
203 
204  pvactx->continue_pes = 0;
205  return res;
206 }
207 
209  .name = "pva",
210  .long_name = NULL_IF_CONFIG_SMALL("TechnoTrend PVA"),
211  .priv_data_size = sizeof(PVAContext),
215  .read_timestamp = pva_read_timestamp,
216 };
static int pva_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: pva.c:164
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:1181
#define PVA_MAGIC
Definition: pva.c:29
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
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:2829
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:591
Format I/O context.
Definition: avformat.h:922
#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:606
enum AVStreamParseType need_parsing
Definition: avformat.h:867
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2521
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:990
static int flags
Definition: log.c:44
#define AVERROR_EOF
End of file.
Definition: error.h:51
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:117
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:463
#define AVINDEX_KEYFRAME
Definition: avformat.h:662
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#define AV_RB16
Definition: intreadwrite.h:53
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:145
Definition: pva.c:31
static int64_t ff_parse_pes_pts(const uint8_t *buf)
Parse MPEG-PES five-byte timestamp.
Definition: mpeg.h:67
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:454
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:397
#define PVA_AUDIO_PAYLOAD
Definition: pva.c:28
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:599
static int64_t pva_read_timestamp(struct AVFormatContext *s, int stream_index, int64_t *pos, int64_t pos_limit)
Definition: pva.c:179
#define FFMIN(a, b)
Definition: common.h:57
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
AVInputFormat ff_pva_demuxer
Definition: pva.c:208
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:110
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:544
#define PVA_MAX_PAYLOAD_LENGTH
Definition: pva.c:26
Stream structure.
Definition: avformat.h:699
NULL
Definition: eval.c:55
enum AVMediaType codec_type
Definition: avcodec.h:1058
enum AVCodecID codec_id
Definition: avcodec.h:1067
AVIOContext * pb
I/O context.
Definition: avformat.h:964
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:402
This structure contains the data a format has to probe a file.
Definition: avformat.h:395
full parsing and repack
Definition: avformat.h:653
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:950
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:525
int avio_feof(AVIOContext *s)
Definition: aviobuf.c:260
int stream_index
Definition: avcodec.h:975
This structure stores compressed data.
Definition: avcodec.h:950
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:966
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:228