Statistics
| Branch: | Revision:

ffmpeg / libavformat / pva.c @ b973f9ad

History | View | Annotate | Download (6.5 KB)

1 3feb44a9 Ivo van Poorten
/*
2
 * TechnoTrend PVA (.pva) demuxer
3
 * Copyright (c) 2007, 2008 Ivo van Poorten
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg 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
 * FFmpeg 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 FFmpeg; 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 9ae09203 Ivo van Poorten
#include "mpeg.h"
24 3feb44a9 Ivo van Poorten
25
#define PVA_MAX_PAYLOAD_LENGTH  0x17f8
26
#define PVA_VIDEO_PAYLOAD       0x01
27
#define PVA_AUDIO_PAYLOAD       0x02
28
#define PVA_MAGIC               (('A' << 8) + 'V')
29
30
typedef struct {
31
    int continue_pes;
32
} PVAContext;
33
34
static int pva_probe(AVProbeData * pd) {
35
    unsigned char *buf = pd->buf;
36
37
    if (AV_RB16(buf) == PVA_MAGIC && buf[2] && buf[2] < 3 && buf[4] == 0x55)
38
        return AVPROBE_SCORE_MAX / 2;
39
40
    return 0;
41
}
42
43
static int pva_read_header(AVFormatContext *s, AVFormatParameters *ap) {
44
    AVStream *st;
45
46
    if (!(st = av_new_stream(s, 0)))
47
        return AVERROR(ENOMEM);
48
    st->codec->codec_type = CODEC_TYPE_VIDEO;
49
    st->codec->codec_id   = CODEC_ID_MPEG2VIDEO;
50
    st->need_parsing      = AVSTREAM_PARSE_FULL;
51
    av_set_pts_info(st, 32, 1, 90000);
52 e03caf2e Ivo van Poorten
    av_add_index_entry(st, 0, 0, 0, 0, AVINDEX_KEYFRAME);
53 3feb44a9 Ivo van Poorten
54
    if (!(st = av_new_stream(s, 1)))
55
        return AVERROR(ENOMEM);
56
    st->codec->codec_type = CODEC_TYPE_AUDIO;
57 fe332ad6 Ivo van Poorten
    st->codec->codec_id   = CODEC_ID_MP2;
58 3feb44a9 Ivo van Poorten
    st->need_parsing      = AVSTREAM_PARSE_HEADERS;
59
    av_set_pts_info(st, 33, 1, 90000);
60 e03caf2e Ivo van Poorten
    av_add_index_entry(st, 0, 0, 0, 0, AVINDEX_KEYFRAME);
61 3feb44a9 Ivo van Poorten
62
    /* the parameters will be extracted from the compressed bitstream */
63
    return 0;
64
}
65
66 e03caf2e Ivo van Poorten
#define pva_log if (read_packet) av_log
67
68
static int read_part_of_packet(AVFormatContext *s, int64_t *pts,
69
                               int *len, int *strid, int read_packet) {
70 3feb44a9 Ivo van Poorten
    ByteIOContext *pb = s->pb;
71
    PVAContext *pvactx = s->priv_data;
72 e03caf2e Ivo van Poorten
    int syncword, streamid, reserved, flags, length, pts_flag;
73
    int64_t pva_pts = AV_NOPTS_VALUE, startpos;
74 3feb44a9 Ivo van Poorten
75 dba13944 Ivo van Poorten
recover:
76 e03caf2e Ivo van Poorten
    startpos = url_ftell(pb);
77
78 3feb44a9 Ivo van Poorten
    syncword = get_be16(pb);
79
    streamid = get_byte(pb);
80
    get_byte(pb);               /* counter not used */
81
    reserved = get_byte(pb);
82
    flags    = get_byte(pb);
83
    length   = get_be16(pb);
84
85 5e40d0e8 Ivo van Poorten
    pts_flag = flags & 0x10;
86 3feb44a9 Ivo van Poorten
87
    if (syncword != PVA_MAGIC) {
88 e03caf2e Ivo van Poorten
        pva_log(s, AV_LOG_ERROR, "invalid syncword\n");
89 3feb44a9 Ivo van Poorten
        return AVERROR(EIO);
90
    }
91 7705cf7e Ivo van Poorten
    if (streamid != PVA_VIDEO_PAYLOAD && streamid != PVA_AUDIO_PAYLOAD) {
92 e03caf2e Ivo van Poorten
        pva_log(s, AV_LOG_ERROR, "invalid streamid\n");
93 7705cf7e Ivo van Poorten
        return AVERROR(EIO);
94
    }
95 3feb44a9 Ivo van Poorten
    if (reserved != 0x55) {
96 e03caf2e Ivo van Poorten
        pva_log(s, AV_LOG_WARNING, "expected reserved byte to be 0x55\n");
97 3feb44a9 Ivo van Poorten
    }
98
    if (length > PVA_MAX_PAYLOAD_LENGTH) {
99 e03caf2e Ivo van Poorten
        pva_log(s, AV_LOG_ERROR, "invalid payload length %u\n", length);
100 3feb44a9 Ivo van Poorten
        return AVERROR(EIO);
101
    }
102
103
    if (streamid == PVA_VIDEO_PAYLOAD && pts_flag) {
104
        pva_pts = get_be32(pb);
105
        length -= 4;
106
    } else if (streamid == PVA_AUDIO_PAYLOAD) {
107
        /* PVA Audio Packets either start with a signaled PES packet or
108
         * are a continuation of the previous PES packet. New PES packets
109
         * always start at the beginning of a PVA Packet, never somewhere in
110
         * the middle. */
111
        if (!pvactx->continue_pes) {
112
            int pes_signal, pes_header_data_length, pes_packet_length,
113
                pes_flags;
114
            unsigned char pes_header_data[256];
115
116
            pes_signal             = get_be24(pb);
117
            get_byte(pb);
118
            pes_packet_length      = get_be16(pb);
119
            pes_flags              = get_be16(pb);
120
            pes_header_data_length = get_byte(pb);
121
122
            if (pes_signal != 1) {
123 e03caf2e Ivo van Poorten
                pva_log(s, AV_LOG_WARNING, "expected signaled PES packet, "
124 dba13944 Ivo van Poorten
                                          "trying to recover\n");
125
                url_fskip(pb, length - 9);
126 e03caf2e Ivo van Poorten
                if (!read_packet)
127
                    return AVERROR(EIO);
128 dba13944 Ivo van Poorten
                goto recover;
129 3feb44a9 Ivo van Poorten
            }
130
131
            get_buffer(pb, pes_header_data, pes_header_data_length);
132
            length -= 9 + pes_header_data_length;
133
134
            pes_packet_length -= 3 + pes_header_data_length;
135
136
            pvactx->continue_pes = pes_packet_length;
137
138 9ae09203 Ivo van Poorten
            if (pes_flags & 0x80 && (pes_header_data[0] & 0xf0) == 0x20)
139
                pva_pts = ff_parse_pes_pts(pes_header_data);
140 3feb44a9 Ivo van Poorten
        }
141
142
        pvactx->continue_pes -= length;
143
144
        if (pvactx->continue_pes < 0) {
145 e03caf2e Ivo van Poorten
            pva_log(s, AV_LOG_WARNING, "audio data corruption\n");
146 3feb44a9 Ivo van Poorten
            pvactx->continue_pes = 0;
147
        }
148
    }
149
150 e03caf2e Ivo van Poorten
    if (pva_pts != AV_NOPTS_VALUE)
151
        av_add_index_entry(s->streams[streamid-1], startpos, pva_pts, 0, 0, AVINDEX_KEYFRAME);
152
153
    *pts   = pva_pts;
154
    *len   = length;
155
    *strid = streamid;
156
    return 0;
157
}
158
159
static int pva_read_packet(AVFormatContext *s, AVPacket *pkt) {
160
    ByteIOContext *pb = s->pb;
161
    int64_t pva_pts;
162
    int ret, length, streamid;
163
164
    if (read_part_of_packet(s, &pva_pts, &length, &streamid, 1) < 0 ||
165
       (ret = av_get_packet(pb, pkt, length)) <= 0)
166 3feb44a9 Ivo van Poorten
        return AVERROR(EIO);
167
168
    pkt->stream_index = streamid - 1;
169 ffc29341 Ivo van Poorten
    pkt->pts = pva_pts;
170 3feb44a9 Ivo van Poorten
171
    return ret;
172
}
173
174 e03caf2e Ivo van Poorten
static int64_t pva_read_timestamp(struct AVFormatContext *s, int stream_index,
175
                                          int64_t *pos, int64_t pos_limit) {
176
    ByteIOContext *pb = s->pb;
177
    PVAContext *pvactx = s->priv_data;
178
    int length, streamid;
179
    int64_t res;
180
181
    pos_limit = FFMIN(*pos+PVA_MAX_PAYLOAD_LENGTH*8, (uint64_t)*pos+pos_limit);
182
183
    while (*pos < pos_limit) {
184
        res = AV_NOPTS_VALUE;
185
        url_fseek(pb, *pos, SEEK_SET);
186
187
        pvactx->continue_pes = 0;
188
        if (read_part_of_packet(s, &res, &length, &streamid, 0)) {
189
            (*pos)++;
190
            continue;
191
        }
192
        if (streamid - 1 != stream_index || res == AV_NOPTS_VALUE) {
193
            *pos = url_ftell(pb) + length;
194
            continue;
195
        }
196
        break;
197
    }
198
199
    pvactx->continue_pes = 0;
200
    return res;
201
}
202
203 3feb44a9 Ivo van Poorten
AVInputFormat pva_demuxer = {
204
    "pva",
205
    "pva file and stream format",
206
    sizeof(PVAContext),
207
    pva_probe,
208
    pva_read_header,
209
    pva_read_packet,
210 e03caf2e Ivo van Poorten
    .read_timestamp = pva_read_timestamp
211 3feb44a9 Ivo van Poorten
};