Statistics
| Branch: | Revision:

ffmpeg / libavformat / nuv.c @ 756fb7fe

History | View | Annotate | Download (7.67 KB)

1
/*
2
 * NuppelVideo demuxer.
3
 * Copyright (c) 2006 Reimar Doeffinger.
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
#include "avformat.h"
22
#include "riff.h"
23

    
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
                if (vst && subtype == 'R') {
69
                    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
                } else
99
                    url_fskip(pb, 4 * 4);
100

    
101
                size -= 6 * 4;
102
                url_fskip(pb, size);
103
                return 1;
104
            case NUV_SEEKP:
105
                size = 11;
106
                break;
107
            default:
108
                url_fskip(pb, 7);
109
                size = PKTSIZE(get_le32(pb));
110
                break;
111
        }
112
        url_fskip(pb, size);
113
    }
114
    return 0;
115
}
116

    
117
static int nuv_header(AVFormatContext *s, AVFormatParameters *ap) {
118
    NUVContext *ctx = s->priv_data;
119
    ByteIOContext *pb = &s->pb;
120
    char id_string[12], version_string[5];
121
    double aspect, fps;
122
    int is_mythtv, width, height, v_packs, a_packs;
123
    int stream_nr = 0;
124
    AVStream *vst = NULL, *ast = NULL;
125
    get_buffer(pb, id_string, 12);
126
    is_mythtv = !memcmp(id_string, "MythTVVideo", 12);
127
    get_buffer(pb, version_string, 5);
128
    url_fskip(pb, 3); // padding
129
    width = get_le32(pb);
130
    height = get_le32(pb);
131
    get_le32(pb); // unused, "desiredwidth"
132
    get_le32(pb); // unused, "desiredheight"
133
    get_byte(pb); // 'P' == progressive, 'I' == interlaced
134
    url_fskip(pb, 3); // padding
135
    aspect = av_int2dbl(get_le64(pb));
136
    fps = av_int2dbl(get_le64(pb));
137

    
138
    // number of packets per stream type, -1 means unknown, e.g. streaming
139
    v_packs = get_le32(pb);
140
    a_packs = get_le32(pb);
141
    get_le32(pb); // text
142

    
143
    get_le32(pb); // keyframe distance (?)
144

    
145
    if (v_packs) {
146
        ctx->v_id = stream_nr++;
147
        vst = av_new_stream(s, ctx->v_id);
148
        vst->codec->codec_type = CODEC_TYPE_VIDEO;
149
        vst->codec->codec_id = CODEC_ID_NUV;
150
        vst->codec->codec_tag = MKTAG('R', 'J', 'P', 'G');
151
        vst->codec->width = width;
152
        vst->codec->height = height;
153
        vst->codec->bits_per_sample = 10;
154
        vst->codec->sample_aspect_ratio = av_d2q(aspect, 10000);
155
        vst->r_frame_rate = av_d2q(fps, 60000);
156
        av_set_pts_info(vst, 32, 1, 1000);
157
    } else
158
        ctx->v_id = -1;
159

    
160
    if (a_packs) {
161
        ctx->a_id = stream_nr++;
162
        ast = av_new_stream(s, ctx->a_id);
163
        ast->codec->codec_type = CODEC_TYPE_AUDIO;
164
        ast->codec->codec_id = CODEC_ID_PCM_S16LE;
165
        ast->codec->channels = 2;
166
        ast->codec->sample_rate = 44100;
167
        ast->codec->bit_rate = 2 * 2 * 44100 * 8;
168
        ast->codec->block_align = 2 * 2;
169
        ast->codec->bits_per_sample = 16;
170
        av_set_pts_info(ast, 32, 1, 1000);
171
    } else
172
        ctx->a_id = -1;
173

    
174
    get_codec_data(pb, vst, ast, is_mythtv);
175
    return 0;
176
}
177

    
178
#define HDRSIZE 12
179

    
180
static int nuv_packet(AVFormatContext *s, AVPacket *pkt) {
181
    NUVContext *ctx = s->priv_data;
182
    ByteIOContext *pb = &s->pb;
183
    uint8_t hdr[HDRSIZE];
184
    frametype_t frametype;
185
    int ret, size;
186
    while (!url_feof(pb)) {
187
        ret = get_buffer(pb, hdr, HDRSIZE);
188
        if (ret <= 0)
189
            return ret ? ret : -1;
190
        frametype = hdr[0];
191
        size = PKTSIZE(AV_RL32(&hdr[8]));
192
        switch (frametype) {
193
            case NUV_VIDEO:
194
            case NUV_EXTRADATA:
195
                if (ctx->v_id < 0) {
196
                    av_log(s, AV_LOG_ERROR, "Video packet in file without video stream!\n");
197
                    url_fskip(pb, size);
198
                    break;
199
                }
200
                ret = av_new_packet(pkt, HDRSIZE + size);
201
                if (ret < 0)
202
                    return ret;
203
                pkt->pos = url_ftell(pb);
204
                pkt->pts = AV_RL32(&hdr[4]);
205
                pkt->stream_index = ctx->v_id;
206
                memcpy(pkt->data, hdr, HDRSIZE);
207
                ret = get_buffer(pb, pkt->data + HDRSIZE, size);
208
                return ret;
209
            case NUV_AUDIO:
210
                if (ctx->a_id < 0) {
211
                    av_log(s, AV_LOG_ERROR, "Audio packet in file without audio stream!\n");
212
                    url_fskip(pb, size);
213
                    break;
214
                }
215
                ret = av_get_packet(pb, pkt, size);
216
                pkt->pts = AV_RL32(&hdr[4]);
217
                pkt->stream_index = ctx->a_id;
218
                return ret;
219
            case NUV_SEEKP:
220
                // contains no data, size value is invalid
221
                break;
222
            default:
223
                url_fskip(pb, size);
224
                break;
225
        }
226
    }
227
    return AVERROR_IO;
228
}
229

    
230
AVInputFormat nuv_demuxer = {
231
    "nuv",
232
    "NuppelVideo format",
233
    sizeof(NUVContext),
234
    nuv_probe,
235
    nuv_header,
236
    nuv_packet,
237
    NULL,
238
    NULL,
239
};