annotate pva.c @ 2880:a174a3a0c2c0 libavformat

TechnoTrend PVA Demuxer
author ivo
date Wed, 02 Jan 2008 10:45:28 +0000
parents
children 35cc09bfab21
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2880
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
1 /*
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
2 * TechnoTrend PVA (.pva) demuxer
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
3 * Copyright (c) 2007, 2008 Ivo van Poorten
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
4 *
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
5 * This file is part of FFmpeg.
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
6 *
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
11 *
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
15 * Lesser General Public License for more details.
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
16 *
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
20 */
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
21
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
22 #include "avformat.h"
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
23
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
24 #define PVA_MAX_PAYLOAD_LENGTH 0x17f8
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
25 #define PVA_VIDEO_PAYLOAD 0x01
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
26 #define PVA_AUDIO_PAYLOAD 0x02
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
27 #define PVA_MAGIC (('A' << 8) + 'V')
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
28
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
29 typedef struct {
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
30 int continue_pes;
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
31 } PVAContext;
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
32
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
33 static int pva_probe(AVProbeData * pd) {
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
34 unsigned char *buf = pd->buf;
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
35
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
36 if (AV_RB16(buf) == PVA_MAGIC && buf[2] && buf[2] < 3 && buf[4] == 0x55)
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
37 return AVPROBE_SCORE_MAX / 2;
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
38
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
39 return 0;
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
40 }
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
41
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
42 static int pva_read_header(AVFormatContext *s, AVFormatParameters *ap) {
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
43 AVStream *st;
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
44
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
45 if (!(st = av_new_stream(s, 0)))
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
46 return AVERROR(ENOMEM);
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
47 st->codec->codec_type = CODEC_TYPE_VIDEO;
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
48 st->codec->codec_id = CODEC_ID_MPEG2VIDEO;
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
49 st->need_parsing = AVSTREAM_PARSE_FULL;
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
50 av_set_pts_info(st, 32, 1, 90000);
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
51
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
52 if (!(st = av_new_stream(s, 1)))
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
53 return AVERROR(ENOMEM);
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
54 st->codec->codec_type = CODEC_TYPE_AUDIO;
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
55 st->codec->codec_id = CODEC_ID_MP3;
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
56 st->need_parsing = AVSTREAM_PARSE_HEADERS;
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
57 av_set_pts_info(st, 33, 1, 90000);
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
58
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
59 /* the parameters will be extracted from the compressed bitstream */
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
60 return 0;
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
61 }
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
62
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
63 static int pva_read_packet(AVFormatContext *s, AVPacket *pkt) {
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
64 ByteIOContext *pb = s->pb;
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
65 PVAContext *pvactx = s->priv_data;
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
66 int ret, syncword, streamid, reserved, flags, length, pts_flag;
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
67 long long pva_pts = 0;
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
68
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
69 syncword = get_be16(pb);
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
70 streamid = get_byte(pb);
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
71 get_byte(pb); /* counter not used */
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
72 reserved = get_byte(pb);
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
73 flags = get_byte(pb);
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
74 length = get_be16(pb);
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
75
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
76 pts_flag = (flags & 0x10) >> 4;
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
77
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
78 if (syncword != PVA_MAGIC) {
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
79 av_log(s, AV_LOG_ERROR, "invalid syncword\n");
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
80 return AVERROR(EIO);
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
81 }
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
82 if (reserved != 0x55) {
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
83 av_log(s, AV_LOG_WARNING, "expected reserved byte to be 0x55\n");
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
84 }
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
85 if (length > PVA_MAX_PAYLOAD_LENGTH) {
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
86 av_log(s, AV_LOG_ERROR, "invalid payload length %u\n", length);
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
87 return AVERROR(EIO);
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
88 }
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
89
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
90 if (streamid == PVA_VIDEO_PAYLOAD && pts_flag) {
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
91 pva_pts = get_be32(pb);
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
92 length -= 4;
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
93 } else if (streamid == PVA_AUDIO_PAYLOAD) {
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
94 /* PVA Audio Packets either start with a signaled PES packet or
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
95 * are a continuation of the previous PES packet. New PES packets
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
96 * always start at the beginning of a PVA Packet, never somewhere in
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
97 * the middle. */
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
98 if (!pvactx->continue_pes) {
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
99 int pes_signal, pes_header_data_length, pes_packet_length,
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
100 pes_flags;
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
101 unsigned char pes_header_data[256];
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
102
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
103 pes_signal = get_be24(pb);
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
104 get_byte(pb);
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
105 pes_packet_length = get_be16(pb);
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
106 pes_flags = get_be16(pb);
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
107 pes_header_data_length = get_byte(pb);
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
108
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
109 if (pes_signal != 1) {
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
110 av_log(s, AV_LOG_ERROR, "expected signaled PES packet\n");
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
111 return AVERROR(EIO);
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
112 }
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
113
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
114 get_buffer(pb, pes_header_data, pes_header_data_length);
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
115 length -= 9 + pes_header_data_length;
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
116
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
117 pes_packet_length -= 3 + pes_header_data_length;
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
118
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
119 pvactx->continue_pes = pes_packet_length;
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
120
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
121 if (pes_flags & 0x80 && (pes_header_data[0] & 0xf0) == 0x20) {
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
122 pva_pts = ((long long) *pes_header_data & 0x0e) << 29;
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
123 pva_pts += (AV_RB16(pes_header_data+1) >> 1) << 15;
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
124 pva_pts += AV_RB16(pes_header_data+3) >> 1;
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
125 }
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
126 }
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
127
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
128 pvactx->continue_pes -= length;
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
129
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
130 if (pvactx->continue_pes < 0) {
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
131 av_log(s, AV_LOG_WARNING, "audio data corruption\n");
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
132 pvactx->continue_pes = 0;
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
133 }
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
134 }
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
135
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
136 if ((ret = av_get_packet(pb, pkt, length)) <= 0)
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
137 return AVERROR(EIO);
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
138
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
139 pkt->stream_index = streamid - 1;
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
140 if (pva_pts)
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
141 pkt->pts = pva_pts;
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
142
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
143 return ret;
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
144 }
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
145
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
146 AVInputFormat pva_demuxer = {
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
147 "pva",
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
148 "pva file and stream format",
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
149 sizeof(PVAContext),
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
150 pva_probe,
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
151 pva_read_header,
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
152 pva_read_packet,
a174a3a0c2c0 TechnoTrend PVA Demuxer
ivo
parents:
diff changeset
153 };