Statistics
| Branch: | Revision:

ffmpeg / libavformat / nuv.c @ d7fb5a18

History | View | Annotate | Download (8.34 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 dd6ca721 Reimar Döffinger
    int rtjpg_video;
28 dfca23e3 Reimar Döffinger
} NUVContext;
29
30
typedef enum {
31
    NUV_VIDEO = 'V',
32
    NUV_EXTRADATA = 'D',
33
    NUV_AUDIO = 'A',
34
    NUV_SEEKP = 'R',
35
    NUV_MYTHEXT = 'X'
36 b4be9321 Reimar Döffinger
} nuv_frametype;
37 dfca23e3 Reimar Döffinger
38
static int nuv_probe(AVProbeData *p) {
39
    if (!memcmp(p->buf, "NuppelVideo", 12))
40
        return AVPROBE_SCORE_MAX;
41
    if (!memcmp(p->buf, "MythTVVideo", 12))
42
        return AVPROBE_SCORE_MAX;
43
    return 0;
44
}
45
46
//! little macro to sanitize packet size
47
#define PKTSIZE(s) (s &  0xffffff)
48
49
/**
50
 * \brief read until we found all data needed for decoding
51
 * \param vst video stream of which to change parameters
52
 * \param ast video stream of which to change parameters
53
 * \param myth set if this is a MythTVVideo format file
54
 * \return 1 if all required codec data was found
55
 */
56
static int get_codec_data(ByteIOContext *pb, AVStream *vst,
57
                          AVStream *ast, int myth) {
58 b4be9321 Reimar Döffinger
    nuv_frametype frametype;
59 dfca23e3 Reimar Döffinger
    if (!vst && !myth)
60
        return 1; // no codec data needed
61
    while (!url_feof(pb)) {
62
        int size, subtype;
63
        frametype = get_byte(pb);
64
        switch (frametype) {
65
            case NUV_EXTRADATA:
66
                subtype = get_byte(pb);
67
                url_fskip(pb, 6);
68
                size = PKTSIZE(get_le32(pb));
69 50a6c318 Reimar Döffinger
                if (vst && subtype == 'R') {
70 dfca23e3 Reimar Döffinger
                    vst->codec->extradata_size = size;
71
                    vst->codec->extradata = av_malloc(size);
72
                    get_buffer(pb, vst->codec->extradata, size);
73
                    size = 0;
74
                    if (!myth)
75
                        return 1;
76
                }
77
                break;
78
            case NUV_MYTHEXT:
79
                url_fskip(pb, 7);
80
                size = PKTSIZE(get_le32(pb));
81
                if (size != 128 * 4)
82
                    break;
83
                get_le32(pb); // version
84
                if (vst) {
85
                    vst->codec->codec_tag = get_le32(pb);
86
                    vst->codec->codec_id =
87
                        codec_get_id(codec_bmp_tags, vst->codec->codec_tag);
88 efd74286 Reimar Döffinger
                    if (vst->codec->codec_tag == MKTAG('R', 'J', 'P', 'G'))
89
                        vst->codec->codec_id = CODEC_ID_NUV;
90 dfca23e3 Reimar Döffinger
                } else
91
                    url_fskip(pb, 4);
92
93
                if (ast) {
94
                    ast->codec->codec_tag = get_le32(pb);
95
                    ast->codec->sample_rate = get_le32(pb);
96 dd1c8f3e Luca Abeni
                    ast->codec->bits_per_coded_sample = get_le32(pb);
97 dfca23e3 Reimar Döffinger
                    ast->codec->channels = get_le32(pb);
98
                    ast->codec->codec_id =
99
                        wav_codec_get_id(ast->codec->codec_tag,
100 dd1c8f3e Luca Abeni
                                         ast->codec->bits_per_coded_sample);
101 8a59d9a3 Michael Niedermayer
                    ast->need_parsing = AVSTREAM_PARSE_FULL;
102 dfca23e3 Reimar Döffinger
                } else
103
                    url_fskip(pb, 4 * 4);
104
105
                size -= 6 * 4;
106
                url_fskip(pb, size);
107
                return 1;
108
            case NUV_SEEKP:
109
                size = 11;
110
                break;
111
            default:
112
                url_fskip(pb, 7);
113
                size = PKTSIZE(get_le32(pb));
114
                break;
115
        }
116
        url_fskip(pb, size);
117
    }
118
    return 0;
119
}
120
121
static int nuv_header(AVFormatContext *s, AVFormatParameters *ap) {
122 e4141433 Nicholas Tung
    NUVContext *ctx = s->priv_data;
123 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
124 dfca23e3 Reimar Döffinger
    char id_string[12], version_string[5];
125
    double aspect, fps;
126
    int is_mythtv, width, height, v_packs, a_packs;
127
    int stream_nr = 0;
128
    AVStream *vst = NULL, *ast = NULL;
129
    get_buffer(pb, id_string, 12);
130
    is_mythtv = !memcmp(id_string, "MythTVVideo", 12);
131
    get_buffer(pb, version_string, 5);
132
    url_fskip(pb, 3); // padding
133
    width = get_le32(pb);
134
    height = get_le32(pb);
135
    get_le32(pb); // unused, "desiredwidth"
136
    get_le32(pb); // unused, "desiredheight"
137
    get_byte(pb); // 'P' == progressive, 'I' == interlaced
138
    url_fskip(pb, 3); // padding
139
    aspect = av_int2dbl(get_le64(pb));
140 4c8e5dfc Joakim Plate
    if (aspect > 0.9999 && aspect < 1.0001)
141
        aspect = 4.0 / 3.0;
142 dfca23e3 Reimar Döffinger
    fps = av_int2dbl(get_le64(pb));
143
144
    // number of packets per stream type, -1 means unknown, e.g. streaming
145
    v_packs = get_le32(pb);
146
    a_packs = get_le32(pb);
147
    get_le32(pb); // text
148
149
    get_le32(pb); // keyframe distance (?)
150
151
    if (v_packs) {
152
        ctx->v_id = stream_nr++;
153
        vst = av_new_stream(s, ctx->v_id);
154 60583fb6 Reimar Döffinger
        if (!vst)
155
            return AVERROR(ENOMEM);
156 dfca23e3 Reimar Döffinger
        vst->codec->codec_type = CODEC_TYPE_VIDEO;
157
        vst->codec->codec_id = CODEC_ID_NUV;
158
        vst->codec->width = width;
159
        vst->codec->height = height;
160 dd1c8f3e Luca Abeni
        vst->codec->bits_per_coded_sample = 10;
161 59729451 Aurelien Jacobs
        vst->sample_aspect_ratio = av_d2q(aspect * height / width, 10000);
162 8643594c Michael Niedermayer
        vst->r_frame_rate = av_d2q(fps, 60000);
163 dfca23e3 Reimar Döffinger
        av_set_pts_info(vst, 32, 1, 1000);
164
    } else
165
        ctx->v_id = -1;
166
167
    if (a_packs) {
168
        ctx->a_id = stream_nr++;
169
        ast = av_new_stream(s, ctx->a_id);
170 60583fb6 Reimar Döffinger
        if (!ast)
171
            return AVERROR(ENOMEM);
172 dfca23e3 Reimar Döffinger
        ast->codec->codec_type = CODEC_TYPE_AUDIO;
173
        ast->codec->codec_id = CODEC_ID_PCM_S16LE;
174
        ast->codec->channels = 2;
175
        ast->codec->sample_rate = 44100;
176
        ast->codec->bit_rate = 2 * 2 * 44100 * 8;
177
        ast->codec->block_align = 2 * 2;
178 dd1c8f3e Luca Abeni
        ast->codec->bits_per_coded_sample = 16;
179 dfca23e3 Reimar Döffinger
        av_set_pts_info(ast, 32, 1, 1000);
180
    } else
181
        ctx->a_id = -1;
182
183
    get_codec_data(pb, vst, ast, is_mythtv);
184 00496302 Reimar Döffinger
    ctx->rtjpg_video = vst && vst->codec->codec_id == CODEC_ID_NUV;
185 dfca23e3 Reimar Döffinger
    return 0;
186
}
187
188
#define HDRSIZE 12
189
190
static int nuv_packet(AVFormatContext *s, AVPacket *pkt) {
191 e4141433 Nicholas Tung
    NUVContext *ctx = s->priv_data;
192 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
193 dfca23e3 Reimar Döffinger
    uint8_t hdr[HDRSIZE];
194 b4be9321 Reimar Döffinger
    nuv_frametype frametype;
195 dfca23e3 Reimar Döffinger
    int ret, size;
196
    while (!url_feof(pb)) {
197 dd6ca721 Reimar Döffinger
        int copyhdrsize = ctx->rtjpg_video ? HDRSIZE : 0;
198 dfca23e3 Reimar Döffinger
        ret = get_buffer(pb, hdr, HDRSIZE);
199
        if (ret <= 0)
200 a7b286e8 Reimar Döffinger
            return ret ? ret : -1;
201 dfca23e3 Reimar Döffinger
        frametype = hdr[0];
202 fead30d4 Alex Beregszaszi
        size = PKTSIZE(AV_RL32(&hdr[8]));
203 dfca23e3 Reimar Döffinger
        switch (frametype) {
204
            case NUV_EXTRADATA:
205 dd6ca721 Reimar Döffinger
                if (!ctx->rtjpg_video) {
206
                    url_fskip(pb, size);
207
                    break;
208
                }
209
            case NUV_VIDEO:
210 dfca23e3 Reimar Döffinger
                if (ctx->v_id < 0) {
211
                    av_log(s, AV_LOG_ERROR, "Video packet in file without video stream!\n");
212
                    url_fskip(pb, size);
213
                    break;
214
                }
215 dd6ca721 Reimar Döffinger
                ret = av_new_packet(pkt, copyhdrsize + size);
216 dfca23e3 Reimar Döffinger
                if (ret < 0)
217
                    return ret;
218 dd6ca721 Reimar Döffinger
                pkt->pos = url_ftell(pb) - copyhdrsize;
219 fead30d4 Alex Beregszaszi
                pkt->pts = AV_RL32(&hdr[4]);
220 dfca23e3 Reimar Döffinger
                pkt->stream_index = ctx->v_id;
221 dd6ca721 Reimar Döffinger
                memcpy(pkt->data, hdr, copyhdrsize);
222
                ret = get_buffer(pb, pkt->data + copyhdrsize, size);
223 dfca23e3 Reimar Döffinger
                return ret;
224
            case NUV_AUDIO:
225
                if (ctx->a_id < 0) {
226
                    av_log(s, AV_LOG_ERROR, "Audio packet in file without audio stream!\n");
227
                    url_fskip(pb, size);
228
                    break;
229
                }
230
                ret = av_get_packet(pb, pkt, size);
231 fead30d4 Alex Beregszaszi
                pkt->pts = AV_RL32(&hdr[4]);
232 dfca23e3 Reimar Döffinger
                pkt->stream_index = ctx->a_id;
233
                return ret;
234
            case NUV_SEEKP:
235
                // contains no data, size value is invalid
236
                break;
237
            default:
238
                url_fskip(pb, size);
239
                break;
240
        }
241
    }
242 6f3e0b21 Panagiotis Issaris
    return AVERROR(EIO);
243 dfca23e3 Reimar Döffinger
}
244
245 ff70e601 Måns Rullgård
AVInputFormat nuv_demuxer = {
246 dfca23e3 Reimar Döffinger
    "nuv",
247 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("NuppelVideo format"),
248 dfca23e3 Reimar Döffinger
    sizeof(NUVContext),
249
    nuv_probe,
250
    nuv_header,
251
    nuv_packet,
252
    NULL,
253
    NULL,
254
};