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