Statistics
| Branch: | Revision:

ffmpeg / libavformat / r3d.c @ e356fc57

History | View | Annotate | Download (10.5 KB)

1 7eb68edb Baptiste Coudurier
/*
2
 * R3D REDCODE demuxer
3
 * Copyright (c) 2008 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
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
//#define DEBUG
23
24
#include "libavutil/intreadwrite.h"
25
#include "avformat.h"
26
27
typedef struct {
28
    unsigned video_offsets_count;
29
    unsigned *video_offsets;
30
    unsigned rdvo_offset;
31
} R3DContext;
32
33
typedef struct {
34
    unsigned size;
35
    uint32_t tag;
36
    uint64_t offset;
37
} Atom;
38
39
static int read_atom(AVFormatContext *s, Atom *atom)
40
{
41
    atom->offset = url_ftell(s->pb);
42 b7effd4e Anton Khirnov
    atom->size = avio_rb32(s->pb);
43 7eb68edb Baptiste Coudurier
    if (atom->size < 8)
44
        return -1;
45 b7effd4e Anton Khirnov
    atom->tag = avio_rl32(s->pb);
46 dfd2a005 Luca Barbato
    av_dlog(s, "atom %d %.4s offset %#llx\n",
47 7eb68edb Baptiste Coudurier
            atom->size, (char*)&atom->tag, atom->offset);
48
    return atom->size;
49
}
50
51
static int r3d_read_red1(AVFormatContext *s)
52
{
53
    AVStream *st = av_new_stream(s, 0);
54 feacba6c Aurelien Jacobs
    char filename[258];
55 7eb68edb Baptiste Coudurier
    int tmp, tmp2;
56
57
    if (!st)
58 6aa33347 Baptiste Coudurier
        return AVERROR(ENOMEM);
59 72415b2a Stefano Sabatini
    st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
60 7eb68edb Baptiste Coudurier
    st->codec->codec_id = CODEC_ID_JPEG2000;
61
62 b7effd4e Anton Khirnov
    tmp  = avio_r8(s->pb); // major version
63
    tmp2 = avio_r8(s->pb); // minor version
64 dfd2a005 Luca Barbato
    av_dlog(s, "version %d.%d\n", tmp, tmp2);
65 7eb68edb Baptiste Coudurier
66 b7effd4e Anton Khirnov
    tmp = avio_rb16(s->pb); // unknown
67 dfd2a005 Luca Barbato
    av_dlog(s, "unknown1 %d\n", tmp);
68 7eb68edb Baptiste Coudurier
69 b7effd4e Anton Khirnov
    tmp = avio_rb32(s->pb);
70 7eb68edb Baptiste Coudurier
    av_set_pts_info(st, 32, 1, tmp);
71
72 b7effd4e Anton Khirnov
    tmp = avio_rb32(s->pb); // filenum
73 dfd2a005 Luca Barbato
    av_dlog(s, "filenum %d\n", tmp);
74 7eb68edb Baptiste Coudurier
75 e356fc57 Anton Khirnov
    avio_seek(s->pb, 32, SEEK_CUR); // unknown
76 7eb68edb Baptiste Coudurier
77 b7effd4e Anton Khirnov
    st->codec->width  = avio_rb32(s->pb);
78
    st->codec->height = avio_rb32(s->pb);
79 7eb68edb Baptiste Coudurier
80 b7effd4e Anton Khirnov
    tmp = avio_rb16(s->pb); // unknown
81 dfd2a005 Luca Barbato
    av_dlog(s, "unknown2 %d\n", tmp);
82 7eb68edb Baptiste Coudurier
83 b7effd4e Anton Khirnov
    st->codec->time_base.den = avio_rb16(s->pb);
84
    st->codec->time_base.num = avio_rb16(s->pb);
85 7eb68edb Baptiste Coudurier
86 b7effd4e Anton Khirnov
    tmp = avio_r8(s->pb); // audio channels
87 dfd2a005 Luca Barbato
    av_dlog(s, "audio channels %d\n", tmp);
88 7eb68edb Baptiste Coudurier
    if (tmp > 0) {
89
        AVStream *ast = av_new_stream(s, 1);
90 b01c7b75 Kent Mein
        if (!ast)
91
            return AVERROR(ENOMEM);
92 72415b2a Stefano Sabatini
        ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
93 7eb68edb Baptiste Coudurier
        ast->codec->codec_id = CODEC_ID_PCM_S32BE;
94
        ast->codec->channels = tmp;
95
        av_set_pts_info(ast, 32, 1, st->time_base.den);
96
    }
97
98 b7effd4e Anton Khirnov
    avio_read(s->pb, filename, 257);
99 feacba6c Aurelien Jacobs
    filename[sizeof(filename)-1] = 0;
100 2ef6c124 Stefano Sabatini
    av_metadata_set2(&st->metadata, "filename", filename, 0);
101 7eb68edb Baptiste Coudurier
102 dfd2a005 Luca Barbato
    av_dlog(s, "filename %s\n", filename);
103
    av_dlog(s, "resolution %dx%d\n", st->codec->width, st->codec->height);
104
    av_dlog(s, "timescale %d\n", st->time_base.den);
105
    av_dlog(s, "frame rate %d/%d\n",
106 7eb68edb Baptiste Coudurier
            st->codec->time_base.num, st->codec->time_base.den);
107
108
    return 0;
109
}
110
111
static int r3d_read_rdvo(AVFormatContext *s, Atom *atom)
112
{
113
    R3DContext *r3d = s->priv_data;
114
    AVStream *st = s->streams[0];
115
    int i;
116
117
    r3d->video_offsets_count = (atom->size - 8) / 4;
118
    r3d->video_offsets = av_malloc(atom->size);
119
    if (!r3d->video_offsets)
120
        return AVERROR(ENOMEM);
121
122
    for (i = 0; i < r3d->video_offsets_count; i++) {
123 b7effd4e Anton Khirnov
        r3d->video_offsets[i] = avio_rb32(s->pb);
124 7eb68edb Baptiste Coudurier
        if (!r3d->video_offsets[i]) {
125
            r3d->video_offsets_count = i;
126
            break;
127
        }
128 dfd2a005 Luca Barbato
        av_dlog(s, "video offset %d: %#x\n", i, r3d->video_offsets[i]);
129 7eb68edb Baptiste Coudurier
    }
130
131
    if (st->codec->time_base.den)
132
        st->duration = (uint64_t)r3d->video_offsets_count*
133
            st->time_base.den*st->codec->time_base.num/st->codec->time_base.den;
134 dfd2a005 Luca Barbato
    av_dlog(s, "duration %lld\n", st->duration);
135 7eb68edb Baptiste Coudurier
136
    return 0;
137
}
138
139
static void r3d_read_reos(AVFormatContext *s)
140
{
141
    R3DContext *r3d = s->priv_data;
142
    int tmp;
143
144 b7effd4e Anton Khirnov
    r3d->rdvo_offset = avio_rb32(s->pb);
145
    avio_rb32(s->pb); // rdvs offset
146
    avio_rb32(s->pb); // rdao offset
147
    avio_rb32(s->pb); // rdas offset
148 7eb68edb Baptiste Coudurier
149 b7effd4e Anton Khirnov
    tmp = avio_rb32(s->pb);
150 dfd2a005 Luca Barbato
    av_dlog(s, "num video chunks %d\n", tmp);
151 7eb68edb Baptiste Coudurier
152 b7effd4e Anton Khirnov
    tmp = avio_rb32(s->pb);
153 dfd2a005 Luca Barbato
    av_dlog(s, "num audio chunks %d\n", tmp);
154 7eb68edb Baptiste Coudurier
155 e356fc57 Anton Khirnov
    avio_seek(s->pb, 6*4, SEEK_CUR);
156 7eb68edb Baptiste Coudurier
}
157
158
static int r3d_read_header(AVFormatContext *s, AVFormatParameters *ap)
159
{
160
    R3DContext *r3d = s->priv_data;
161
    Atom atom;
162
    int ret;
163
164
    if (read_atom(s, &atom) < 0) {
165
        av_log(s, AV_LOG_ERROR, "error reading atom\n");
166
        return -1;
167
    }
168
    if (atom.tag == MKTAG('R','E','D','1')) {
169
        if ((ret = r3d_read_red1(s)) < 0) {
170
            av_log(s, AV_LOG_ERROR, "error parsing 'red1' atom\n");
171
            return ret;
172
        }
173
    } else {
174
        av_log(s, AV_LOG_ERROR, "could not find 'red1' atom\n");
175
        return -1;
176
    }
177
178
    s->data_offset = url_ftell(s->pb);
179 dfd2a005 Luca Barbato
    av_dlog(s, "data offset %#llx\n", s->data_offset);
180 7eb68edb Baptiste Coudurier
    if (url_is_streamed(s->pb))
181
        return 0;
182
    // find REOB/REOF/REOS to load index
183 6b4aa5da Anton Khirnov
    avio_seek(s->pb, url_fsize(s->pb)-48-8, SEEK_SET);
184 7eb68edb Baptiste Coudurier
    if (read_atom(s, &atom) < 0)
185
        av_log(s, AV_LOG_ERROR, "error reading end atom\n");
186
187
    if (atom.tag != MKTAG('R','E','O','B') &&
188
        atom.tag != MKTAG('R','E','O','F') &&
189
        atom.tag != MKTAG('R','E','O','S'))
190
        goto out;
191
192
    r3d_read_reos(s);
193
194
    if (r3d->rdvo_offset) {
195 6b4aa5da Anton Khirnov
        avio_seek(s->pb, r3d->rdvo_offset, SEEK_SET);
196 7eb68edb Baptiste Coudurier
        if (read_atom(s, &atom) < 0)
197
            av_log(s, AV_LOG_ERROR, "error reading 'rdvo' atom\n");
198
        if (atom.tag == MKTAG('R','D','V','O')) {
199
            if (r3d_read_rdvo(s, &atom) < 0)
200
                av_log(s, AV_LOG_ERROR, "error parsing 'rdvo' atom\n");
201
        }
202
    }
203
204
 out:
205 6b4aa5da Anton Khirnov
    avio_seek(s->pb, s->data_offset, SEEK_SET);
206 7eb68edb Baptiste Coudurier
    return 0;
207
}
208
209
static int r3d_read_redv(AVFormatContext *s, AVPacket *pkt, Atom *atom)
210
{
211
    AVStream *st = s->streams[0];
212
    int tmp, tmp2;
213
    uint64_t pos = url_ftell(s->pb);
214
    unsigned dts;
215 38beab19 Vitor Sessak
    int ret;
216 7eb68edb Baptiste Coudurier
217 b7effd4e Anton Khirnov
    dts = avio_rb32(s->pb);
218 7eb68edb Baptiste Coudurier
219 b7effd4e Anton Khirnov
    tmp = avio_rb32(s->pb);
220 dfd2a005 Luca Barbato
    av_dlog(s, "frame num %d\n", tmp);
221 7eb68edb Baptiste Coudurier
222 b7effd4e Anton Khirnov
    tmp  = avio_r8(s->pb); // major version
223
    tmp2 = avio_r8(s->pb); // minor version
224 dfd2a005 Luca Barbato
    av_dlog(s, "version %d.%d\n", tmp, tmp2);
225 7eb68edb Baptiste Coudurier
226 b7effd4e Anton Khirnov
    tmp = avio_rb16(s->pb); // unknown
227 dfd2a005 Luca Barbato
    av_dlog(s, "unknown %d\n", tmp);
228 7eb68edb Baptiste Coudurier
229
    if (tmp > 4) {
230 b7effd4e Anton Khirnov
        tmp = avio_rb16(s->pb); // unknown
231 dfd2a005 Luca Barbato
        av_dlog(s, "unknown %d\n", tmp);
232 7eb68edb Baptiste Coudurier
233 b7effd4e Anton Khirnov
        tmp = avio_rb16(s->pb); // unknown
234 dfd2a005 Luca Barbato
        av_dlog(s, "unknown %d\n", tmp);
235 7eb68edb Baptiste Coudurier
236 b7effd4e Anton Khirnov
        tmp = avio_rb32(s->pb);
237 dfd2a005 Luca Barbato
        av_dlog(s, "width %d\n", tmp);
238 b7effd4e Anton Khirnov
        tmp = avio_rb32(s->pb);
239 dfd2a005 Luca Barbato
        av_dlog(s, "height %d\n", tmp);
240 7eb68edb Baptiste Coudurier
241 b7effd4e Anton Khirnov
        tmp = avio_rb32(s->pb);
242 dfd2a005 Luca Barbato
        av_dlog(s, "metadata len %d\n", tmp);
243 7eb68edb Baptiste Coudurier
    }
244
    tmp = atom->size - 8 - (url_ftell(s->pb) - pos);
245
    if (tmp < 0)
246
        return -1;
247 38beab19 Vitor Sessak
    ret = av_get_packet(s->pb, pkt, tmp);
248
    if (ret < 0) {
249 7eb68edb Baptiste Coudurier
        av_log(s, AV_LOG_ERROR, "error reading video packet\n");
250
        return -1;
251
    }
252
253
    pkt->stream_index = 0;
254
    pkt->dts = dts;
255
    if (st->codec->time_base.den)
256
        pkt->duration = (uint64_t)st->time_base.den*
257
            st->codec->time_base.num/st->codec->time_base.den;
258 dfd2a005 Luca Barbato
    av_dlog(s, "pkt dts %lld duration %d\n", pkt->dts, pkt->duration);
259 7eb68edb Baptiste Coudurier
260
    return 0;
261
}
262
263
static int r3d_read_reda(AVFormatContext *s, AVPacket *pkt, Atom *atom)
264
{
265
    AVStream *st = s->streams[1];
266
    int tmp, tmp2, samples, size;
267
    uint64_t pos = url_ftell(s->pb);
268
    unsigned dts;
269 38beab19 Vitor Sessak
    int ret;
270 7eb68edb Baptiste Coudurier
271 b7effd4e Anton Khirnov
    dts = avio_rb32(s->pb);
272 7eb68edb Baptiste Coudurier
273 b7effd4e Anton Khirnov
    st->codec->sample_rate = avio_rb32(s->pb);
274 7eb68edb Baptiste Coudurier
275 b7effd4e Anton Khirnov
    samples = avio_rb32(s->pb);
276 7eb68edb Baptiste Coudurier
277 b7effd4e Anton Khirnov
    tmp = avio_rb32(s->pb);
278 dfd2a005 Luca Barbato
    av_dlog(s, "packet num %d\n", tmp);
279 7eb68edb Baptiste Coudurier
280 b7effd4e Anton Khirnov
    tmp = avio_rb16(s->pb); // unkown
281 dfd2a005 Luca Barbato
    av_dlog(s, "unknown %d\n", tmp);
282 7eb68edb Baptiste Coudurier
283 b7effd4e Anton Khirnov
    tmp  = avio_r8(s->pb); // major version
284
    tmp2 = avio_r8(s->pb); // minor version
285 dfd2a005 Luca Barbato
    av_dlog(s, "version %d.%d\n", tmp, tmp2);
286 7eb68edb Baptiste Coudurier
287 b7effd4e Anton Khirnov
    tmp = avio_rb32(s->pb); // unknown
288 dfd2a005 Luca Barbato
    av_dlog(s, "unknown %d\n", tmp);
289 7eb68edb Baptiste Coudurier
290
    size = atom->size - 8 - (url_ftell(s->pb) - pos);
291
    if (size < 0)
292
        return -1;
293 38beab19 Vitor Sessak
    ret = av_get_packet(s->pb, pkt, size);
294
    if (ret < 0) {
295
        av_log(s, AV_LOG_ERROR, "error reading audio packet\n");
296
        return ret;
297 7eb68edb Baptiste Coudurier
    }
298
299
    pkt->stream_index = 1;
300
    pkt->dts = dts;
301
    pkt->duration = av_rescale(samples, st->time_base.den, st->codec->sample_rate);
302 dfd2a005 Luca Barbato
    av_dlog(s, "pkt dts %lld duration %d samples %d sample rate %d\n",
303 7eb68edb Baptiste Coudurier
            pkt->dts, pkt->duration, samples, st->codec->sample_rate);
304
305
    return 0;
306
}
307
308
static int r3d_read_packet(AVFormatContext *s, AVPacket *pkt)
309
{
310
    Atom atom;
311
    int err = 0;
312
313
    while (!err) {
314
        if (read_atom(s, &atom) < 0) {
315
            err = -1;
316
            break;
317
        }
318
        switch (atom.tag) {
319
        case MKTAG('R','E','D','V'):
320
            if (s->streams[0]->discard == AVDISCARD_ALL)
321
                goto skip;
322
            if (!(err = r3d_read_redv(s, pkt, &atom)))
323
                return 0;
324
            break;
325
        case MKTAG('R','E','D','A'):
326
            if (s->nb_streams < 2)
327
                return -1;
328
            if (s->streams[1]->discard == AVDISCARD_ALL)
329
                goto skip;
330
            if (!(err = r3d_read_reda(s, pkt, &atom)))
331
                return 0;
332
            break;
333
        default:
334
        skip:
335 e356fc57 Anton Khirnov
            avio_seek(s->pb, atom.size-8, SEEK_CUR);
336 7eb68edb Baptiste Coudurier
        }
337
    }
338
    return err;
339
}
340
341
static int r3d_probe(AVProbeData *p)
342
{
343
    if (AV_RL32(p->buf + 4) == MKTAG('R','E','D','1'))
344
        return AVPROBE_SCORE_MAX;
345
    return 0;
346
}
347
348
static int r3d_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
349
{
350
    AVStream *st = s->streams[0]; // video stream
351
    R3DContext *r3d = s->priv_data;
352
    int frame_num;
353
354
    if (!st->codec->time_base.num || !st->time_base.den)
355
        return -1;
356
357
    frame_num = sample_time*st->codec->time_base.den/
358
        ((int64_t)st->codec->time_base.num*st->time_base.den);
359 dfd2a005 Luca Barbato
    av_dlog(s, "seek frame num %d timestamp %lld\n", frame_num, sample_time);
360 7eb68edb Baptiste Coudurier
361
    if (frame_num < r3d->video_offsets_count) {
362 6b4aa5da Anton Khirnov
        avio_seek(s->pb, r3d->video_offsets_count, SEEK_SET);
363 7eb68edb Baptiste Coudurier
    } else {
364
        av_log(s, AV_LOG_ERROR, "could not seek to frame %d\n", frame_num);
365
        return -1;
366
    }
367
368
    return 0;
369
}
370
371
static int r3d_close(AVFormatContext *s)
372
{
373
    R3DContext *r3d = s->priv_data;
374
375
    av_freep(&r3d->video_offsets);
376
377
    return 0;
378
}
379
380 c6610a21 Diego Elio Pettenò
AVInputFormat ff_r3d_demuxer = {
381 7eb68edb Baptiste Coudurier
    "r3d",
382
    NULL_IF_CONFIG_SMALL("REDCODE R3D format"),
383
    sizeof(R3DContext),
384
    r3d_probe,
385
    r3d_read_header,
386
    r3d_read_packet,
387
    r3d_close,
388
    r3d_seek,
389
};