00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avformat.h"
00023 #include "mpeg.h"
00024
00025 #define PVA_MAX_PAYLOAD_LENGTH 0x17f8
00026 #define PVA_VIDEO_PAYLOAD 0x01
00027 #define PVA_AUDIO_PAYLOAD 0x02
00028 #define PVA_MAGIC (('A' << 8) + 'V')
00029
00030 typedef struct {
00031 int continue_pes;
00032 } PVAContext;
00033
00034 static int pva_probe(AVProbeData * pd) {
00035 unsigned char *buf = pd->buf;
00036
00037 if (AV_RB16(buf) == PVA_MAGIC && buf[2] && buf[2] < 3 && buf[4] == 0x55)
00038 return AVPROBE_SCORE_MAX / 2;
00039
00040 return 0;
00041 }
00042
00043 static int pva_read_header(AVFormatContext *s, AVFormatParameters *ap) {
00044 AVStream *st;
00045
00046 if (!(st = av_new_stream(s, 0)))
00047 return AVERROR(ENOMEM);
00048 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00049 st->codec->codec_id = CODEC_ID_MPEG2VIDEO;
00050 st->need_parsing = AVSTREAM_PARSE_FULL;
00051 av_set_pts_info(st, 32, 1, 90000);
00052 av_add_index_entry(st, 0, 0, 0, 0, AVINDEX_KEYFRAME);
00053
00054 if (!(st = av_new_stream(s, 1)))
00055 return AVERROR(ENOMEM);
00056 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00057 st->codec->codec_id = CODEC_ID_MP2;
00058 st->need_parsing = AVSTREAM_PARSE_FULL;
00059 av_set_pts_info(st, 33, 1, 90000);
00060 av_add_index_entry(st, 0, 0, 0, 0, AVINDEX_KEYFRAME);
00061
00062
00063 return 0;
00064 }
00065
00066 #define pva_log if (read_packet) av_log
00067
00068 static int read_part_of_packet(AVFormatContext *s, int64_t *pts,
00069 int *len, int *strid, int read_packet) {
00070 AVIOContext *pb = s->pb;
00071 PVAContext *pvactx = s->priv_data;
00072 int syncword, streamid, reserved, flags, length, pts_flag;
00073 int64_t pva_pts = AV_NOPTS_VALUE, startpos;
00074
00075 recover:
00076 startpos = avio_tell(pb);
00077
00078 syncword = avio_rb16(pb);
00079 streamid = avio_r8(pb);
00080 avio_r8(pb);
00081 reserved = avio_r8(pb);
00082 flags = avio_r8(pb);
00083 length = avio_rb16(pb);
00084
00085 pts_flag = flags & 0x10;
00086
00087 if (syncword != PVA_MAGIC) {
00088 pva_log(s, AV_LOG_ERROR, "invalid syncword\n");
00089 return AVERROR(EIO);
00090 }
00091 if (streamid != PVA_VIDEO_PAYLOAD && streamid != PVA_AUDIO_PAYLOAD) {
00092 pva_log(s, AV_LOG_ERROR, "invalid streamid\n");
00093 return AVERROR(EIO);
00094 }
00095 if (reserved != 0x55) {
00096 pva_log(s, AV_LOG_WARNING, "expected reserved byte to be 0x55\n");
00097 }
00098 if (length > PVA_MAX_PAYLOAD_LENGTH) {
00099 pva_log(s, AV_LOG_ERROR, "invalid payload length %u\n", length);
00100 return AVERROR(EIO);
00101 }
00102
00103 if (streamid == PVA_VIDEO_PAYLOAD && pts_flag) {
00104 pva_pts = avio_rb32(pb);
00105 length -= 4;
00106 } else if (streamid == PVA_AUDIO_PAYLOAD) {
00107
00108
00109
00110
00111 if (!pvactx->continue_pes) {
00112 int pes_signal, pes_header_data_length, pes_packet_length,
00113 pes_flags;
00114 unsigned char pes_header_data[256];
00115
00116 pes_signal = avio_rb24(pb);
00117 avio_r8(pb);
00118 pes_packet_length = avio_rb16(pb);
00119 pes_flags = avio_rb16(pb);
00120 pes_header_data_length = avio_r8(pb);
00121
00122 if (pes_signal != 1) {
00123 pva_log(s, AV_LOG_WARNING, "expected signaled PES packet, "
00124 "trying to recover\n");
00125 avio_skip(pb, length - 9);
00126 if (!read_packet)
00127 return AVERROR(EIO);
00128 goto recover;
00129 }
00130
00131 avio_read(pb, pes_header_data, pes_header_data_length);
00132 length -= 9 + pes_header_data_length;
00133
00134 pes_packet_length -= 3 + pes_header_data_length;
00135
00136 pvactx->continue_pes = pes_packet_length;
00137
00138 if (pes_flags & 0x80 && (pes_header_data[0] & 0xf0) == 0x20)
00139 pva_pts = ff_parse_pes_pts(pes_header_data);
00140 }
00141
00142 pvactx->continue_pes -= length;
00143
00144 if (pvactx->continue_pes < 0) {
00145 pva_log(s, AV_LOG_WARNING, "audio data corruption\n");
00146 pvactx->continue_pes = 0;
00147 }
00148 }
00149
00150 if (pva_pts != AV_NOPTS_VALUE)
00151 av_add_index_entry(s->streams[streamid-1], startpos, pva_pts, 0, 0, AVINDEX_KEYFRAME);
00152
00153 *pts = pva_pts;
00154 *len = length;
00155 *strid = streamid;
00156 return 0;
00157 }
00158
00159 static int pva_read_packet(AVFormatContext *s, AVPacket *pkt) {
00160 AVIOContext *pb = s->pb;
00161 int64_t pva_pts;
00162 int ret, length, streamid;
00163
00164 if (read_part_of_packet(s, &pva_pts, &length, &streamid, 1) < 0 ||
00165 (ret = av_get_packet(pb, pkt, length)) <= 0)
00166 return AVERROR(EIO);
00167
00168 pkt->stream_index = streamid - 1;
00169 pkt->pts = pva_pts;
00170
00171 return ret;
00172 }
00173
00174 static int64_t pva_read_timestamp(struct AVFormatContext *s, int stream_index,
00175 int64_t *pos, int64_t pos_limit) {
00176 AVIOContext *pb = s->pb;
00177 PVAContext *pvactx = s->priv_data;
00178 int length, streamid;
00179 int64_t res = AV_NOPTS_VALUE;
00180
00181 pos_limit = FFMIN(*pos+PVA_MAX_PAYLOAD_LENGTH*8, (uint64_t)*pos+pos_limit);
00182
00183 while (*pos < pos_limit) {
00184 res = AV_NOPTS_VALUE;
00185 avio_seek(pb, *pos, SEEK_SET);
00186
00187 pvactx->continue_pes = 0;
00188 if (read_part_of_packet(s, &res, &length, &streamid, 0)) {
00189 (*pos)++;
00190 continue;
00191 }
00192 if (streamid - 1 != stream_index || res == AV_NOPTS_VALUE) {
00193 *pos = avio_tell(pb) + length;
00194 continue;
00195 }
00196 break;
00197 }
00198
00199 pvactx->continue_pes = 0;
00200 return res;
00201 }
00202
00203 AVInputFormat ff_pva_demuxer = {
00204 "pva",
00205 NULL_IF_CONFIG_SMALL("TechnoTrend PVA file and stream format"),
00206 sizeof(PVAContext),
00207 pva_probe,
00208 pva_read_header,
00209 pva_read_packet,
00210 .read_timestamp = pva_read_timestamp
00211 };