Statistics
| Branch: | Revision:

ffmpeg / libavformat / nuv.c @ 663a5d9d

History | View | Annotate | Download (7.73 KB)

1 dfca23e3 Reimar Döffinger
/*
2
 * NuppelVideo demuxer.
3
 * Copyright (c) 2006 Reimar Doeffinger.
4
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 dfca23e3 Reimar Döffinger
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 dfca23e3 Reimar Döffinger
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 dfca23e3 Reimar Döffinger
 * 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 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 dfca23e3 Reimar Döffinger
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
#include "avformat.h"
22 9d9f4119 Måns Rullgård
#include "riff.h"
23 dfca23e3 Reimar Döffinger
24
typedef struct {
25
    int v_id;
26
    int a_id;
27
} NUVContext;
28
29
typedef enum {
30
    NUV_VIDEO = 'V',
31
    NUV_EXTRADATA = 'D',
32
    NUV_AUDIO = 'A',
33
    NUV_SEEKP = 'R',
34
    NUV_MYTHEXT = 'X'
35
} frametype_t;
36
37
static int nuv_probe(AVProbeData *p) {
38
    if (!memcmp(p->buf, "NuppelVideo", 12))
39
        return AVPROBE_SCORE_MAX;
40
    if (!memcmp(p->buf, "MythTVVideo", 12))
41
        return AVPROBE_SCORE_MAX;
42
    return 0;
43
}
44
45
//! little macro to sanitize packet size
46
#define PKTSIZE(s) (s &  0xffffff)
47
48
/**
49
 * \brief read until we found all data needed for decoding
50
 * \param vst video stream of which to change parameters
51
 * \param ast video stream of which to change parameters
52
 * \param myth set if this is a MythTVVideo format file
53
 * \return 1 if all required codec data was found
54
 */
55
static int get_codec_data(ByteIOContext *pb, AVStream *vst,
56
                          AVStream *ast, int myth) {
57
    frametype_t frametype;
58
    if (!vst && !myth)
59
        return 1; // no codec data needed
60
    while (!url_feof(pb)) {
61
        int size, subtype;
62
        frametype = get_byte(pb);
63
        switch (frametype) {
64
            case NUV_EXTRADATA:
65
                subtype = get_byte(pb);
66
                url_fskip(pb, 6);
67
                size = PKTSIZE(get_le32(pb));
68 50a6c318 Reimar Döffinger
                if (vst && subtype == 'R') {
69 dfca23e3 Reimar Döffinger
                    vst->codec->extradata_size = size;
70
                    vst->codec->extradata = av_malloc(size);
71
                    get_buffer(pb, vst->codec->extradata, size);
72
                    size = 0;
73
                    if (!myth)
74
                        return 1;
75
                }
76
                break;
77
            case NUV_MYTHEXT:
78
                url_fskip(pb, 7);
79
                size = PKTSIZE(get_le32(pb));
80
                if (size != 128 * 4)
81
                    break;
82
                get_le32(pb); // version
83
                if (vst) {
84
                    vst->codec->codec_tag = get_le32(pb);
85
                    vst->codec->codec_id =
86
                        codec_get_id(codec_bmp_tags, vst->codec->codec_tag);
87
                } else
88
                    url_fskip(pb, 4);
89
90
                if (ast) {
91
                    ast->codec->codec_tag = get_le32(pb);
92
                    ast->codec->sample_rate = get_le32(pb);
93
                    ast->codec->bits_per_sample = get_le32(pb);
94
                    ast->codec->channels = get_le32(pb);
95
                    ast->codec->codec_id =
96
                        wav_codec_get_id(ast->codec->codec_tag,
97
                                         ast->codec->bits_per_sample);
98 8a59d9a3 Michael Niedermayer
                    ast->need_parsing = AVSTREAM_PARSE_FULL;
99 dfca23e3 Reimar Döffinger
                } else
100
                    url_fskip(pb, 4 * 4);
101
102
                size -= 6 * 4;
103
                url_fskip(pb, size);
104
                return 1;
105
            case NUV_SEEKP:
106
                size = 11;
107
                break;
108
            default:
109
                url_fskip(pb, 7);
110
                size = PKTSIZE(get_le32(pb));
111
                break;
112
        }
113
        url_fskip(pb, size);
114
    }
115
    return 0;
116
}
117
118
static int nuv_header(AVFormatContext *s, AVFormatParameters *ap) {
119 e4141433 Nicholas Tung
    NUVContext *ctx = s->priv_data;
120 dfca23e3 Reimar Döffinger
    ByteIOContext *pb = &s->pb;
121
    char id_string[12], version_string[5];
122
    double aspect, fps;
123
    int is_mythtv, width, height, v_packs, a_packs;
124
    int stream_nr = 0;
125
    AVStream *vst = NULL, *ast = NULL;
126
    get_buffer(pb, id_string, 12);
127
    is_mythtv = !memcmp(id_string, "MythTVVideo", 12);
128
    get_buffer(pb, version_string, 5);
129
    url_fskip(pb, 3); // padding
130
    width = get_le32(pb);
131
    height = get_le32(pb);
132
    get_le32(pb); // unused, "desiredwidth"
133
    get_le32(pb); // unused, "desiredheight"
134
    get_byte(pb); // 'P' == progressive, 'I' == interlaced
135
    url_fskip(pb, 3); // padding
136
    aspect = av_int2dbl(get_le64(pb));
137
    fps = av_int2dbl(get_le64(pb));
138
139
    // number of packets per stream type, -1 means unknown, e.g. streaming
140
    v_packs = get_le32(pb);
141
    a_packs = get_le32(pb);
142
    get_le32(pb); // text
143
144
    get_le32(pb); // keyframe distance (?)
145
146
    if (v_packs) {
147
        ctx->v_id = stream_nr++;
148
        vst = av_new_stream(s, ctx->v_id);
149
        vst->codec->codec_type = CODEC_TYPE_VIDEO;
150
        vst->codec->codec_id = CODEC_ID_NUV;
151
        vst->codec->codec_tag = MKTAG('R', 'J', 'P', 'G');
152
        vst->codec->width = width;
153
        vst->codec->height = height;
154
        vst->codec->bits_per_sample = 10;
155
        vst->codec->sample_aspect_ratio = av_d2q(aspect, 10000);
156 8643594c Michael Niedermayer
        vst->r_frame_rate = av_d2q(fps, 60000);
157 dfca23e3 Reimar Döffinger
        av_set_pts_info(vst, 32, 1, 1000);
158
    } else
159
        ctx->v_id = -1;
160
161
    if (a_packs) {
162
        ctx->a_id = stream_nr++;
163
        ast = av_new_stream(s, ctx->a_id);
164
        ast->codec->codec_type = CODEC_TYPE_AUDIO;
165
        ast->codec->codec_id = CODEC_ID_PCM_S16LE;
166
        ast->codec->channels = 2;
167
        ast->codec->sample_rate = 44100;
168
        ast->codec->bit_rate = 2 * 2 * 44100 * 8;
169
        ast->codec->block_align = 2 * 2;
170
        ast->codec->bits_per_sample = 16;
171
        av_set_pts_info(ast, 32, 1, 1000);
172
    } else
173
        ctx->a_id = -1;
174
175
    get_codec_data(pb, vst, ast, is_mythtv);
176
    return 0;
177
}
178
179
#define HDRSIZE 12
180
181
static int nuv_packet(AVFormatContext *s, AVPacket *pkt) {
182 e4141433 Nicholas Tung
    NUVContext *ctx = s->priv_data;
183 dfca23e3 Reimar Döffinger
    ByteIOContext *pb = &s->pb;
184
    uint8_t hdr[HDRSIZE];
185
    frametype_t frametype;
186
    int ret, size;
187
    while (!url_feof(pb)) {
188
        ret = get_buffer(pb, hdr, HDRSIZE);
189
        if (ret <= 0)
190 a7b286e8 Reimar Döffinger
            return ret ? ret : -1;
191 dfca23e3 Reimar Döffinger
        frametype = hdr[0];
192 fead30d4 Alex Beregszaszi
        size = PKTSIZE(AV_RL32(&hdr[8]));
193 dfca23e3 Reimar Döffinger
        switch (frametype) {
194
            case NUV_VIDEO:
195
            case NUV_EXTRADATA:
196
                if (ctx->v_id < 0) {
197
                    av_log(s, AV_LOG_ERROR, "Video packet in file without video stream!\n");
198
                    url_fskip(pb, size);
199
                    break;
200
                }
201
                ret = av_new_packet(pkt, HDRSIZE + size);
202
                if (ret < 0)
203
                    return ret;
204
                pkt->pos = url_ftell(pb);
205 fead30d4 Alex Beregszaszi
                pkt->pts = AV_RL32(&hdr[4]);
206 dfca23e3 Reimar Döffinger
                pkt->stream_index = ctx->v_id;
207
                memcpy(pkt->data, hdr, HDRSIZE);
208
                ret = get_buffer(pb, pkt->data + HDRSIZE, size);
209
                return ret;
210
            case NUV_AUDIO:
211
                if (ctx->a_id < 0) {
212
                    av_log(s, AV_LOG_ERROR, "Audio packet in file without audio stream!\n");
213
                    url_fskip(pb, size);
214
                    break;
215
                }
216
                ret = av_get_packet(pb, pkt, size);
217 fead30d4 Alex Beregszaszi
                pkt->pts = AV_RL32(&hdr[4]);
218 dfca23e3 Reimar Döffinger
                pkt->stream_index = ctx->a_id;
219
                return ret;
220
            case NUV_SEEKP:
221
                // contains no data, size value is invalid
222
                break;
223
            default:
224
                url_fskip(pb, size);
225
                break;
226
        }
227
    }
228 6f3e0b21 Panagiotis Issaris
    return AVERROR(EIO);
229 dfca23e3 Reimar Döffinger
}
230
231 ff70e601 Måns Rullgård
AVInputFormat nuv_demuxer = {
232 dfca23e3 Reimar Döffinger
    "nuv",
233
    "NuppelVideo format",
234
    sizeof(NUVContext),
235
    nuv_probe,
236
    nuv_header,
237
    nuv_packet,
238
    NULL,
239
    NULL,
240
};