~ubuntu-branches/ubuntu/jaunty/xvidcap/jaunty-proposed

« back to all changes in this revision

Viewing changes to ffmpeg/libavformat/ipmovie.c

  • Committer: Bazaar Package Importer
  • Author(s): John Dong
  • Date: 2008-02-25 15:47:12 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080225154712-qvr11ekcea4c9ry8
Tags: 1.1.6-0.1ubuntu1
* Merge from debian-multimedia (LP: #120003), Ubuntu Changes:
 - For ffmpeg-related build-deps, remove cvs from package names.
 - Standards-Version 3.7.3
 - Maintainer Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 * Interplay MVE File Demuxer
3
3
 * Copyright (c) 2003 The ffmpeg Project
4
4
 *
5
 
 * This library is free software; you can redistribute it and/or
 
5
 * This file is part of FFmpeg.
 
6
 *
 
7
 * FFmpeg is free software; you can redistribute it and/or
6
8
 * modify it under the terms of the GNU Lesser General Public
7
9
 * License as published by the Free Software Foundation; either
8
 
 * version 2 of the License, or (at your option) any later version.
 
10
 * version 2.1 of the License, or (at your option) any later version.
9
11
 *
10
 
 * This library is distributed in the hope that it will be useful,
 
12
 * FFmpeg is distributed in the hope that it will be useful,
11
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
15
 * Lesser General Public License for more details.
14
16
 *
15
17
 * You should have received a copy of the GNU Lesser General Public
16
 
 * License along with this library; if not, write to the Free Software
17
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
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
18
20
 */
19
21
 
20
22
/**
42
44
static inline void debug_ipmovie(const char *format, ...) { }
43
45
#endif
44
46
 
45
 
 
46
 
#define LE_16(x)  ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
47
 
#define LE_32(x)  ((((uint8_t*)(x))[3] << 24) | \
48
 
                   (((uint8_t*)(x))[2] << 16) | \
49
 
                   (((uint8_t*)(x))[1] << 8) | \
50
 
                    ((uint8_t*)(x))[0])
51
 
 
52
47
#define IPMOVIE_SIGNATURE "Interplay MVE File\x1A\0"
53
48
#define IPMOVIE_SIGNATURE_SIZE 20
54
49
#define CHUNK_PREAMBLE_SIZE 4
96
91
    unsigned char *buf;
97
92
    int buf_size;
98
93
 
99
 
    int fps;
 
94
    float fps;
100
95
    int frame_pts_inc;
101
96
 
102
97
    unsigned int video_width;
125
120
 
126
121
} IPMVEContext;
127
122
 
128
 
static int load_ipmovie_packet(IPMVEContext *s, ByteIOContext *pb, 
 
123
static int load_ipmovie_packet(IPMVEContext *s, ByteIOContext *pb,
129
124
    AVPacket *pkt) {
130
125
 
131
126
    int chunk_type;
147
142
        audio_pts *= s->audio_frame_count;
148
143
        audio_pts /= s->audio_sample_rate;
149
144
 
150
 
        if (av_new_packet(pkt, s->audio_chunk_size))
151
 
            return CHUNK_NOMEM;
 
145
        if (s->audio_chunk_size != av_get_packet(pb, pkt, s->audio_chunk_size))
 
146
            return CHUNK_EOF;
152
147
 
153
148
        pkt->stream_index = s->audio_stream_index;
154
149
        pkt->pts = audio_pts;
155
 
        if (get_buffer(pb, pkt->data, s->audio_chunk_size) != 
156
 
            s->audio_chunk_size) {
157
 
            av_free_packet(pkt);
158
 
            return CHUNK_EOF;
159
 
        }
160
150
 
161
151
        /* audio frame maintenance */
162
152
        if (s->audio_type != CODEC_ID_INTERPLAY_DPCM)
166
156
            s->audio_frame_count +=
167
157
                (s->audio_chunk_size - 6) / s->audio_channels;
168
158
 
169
 
        debug_ipmovie("sending audio frame with pts %lld (%d audio frames)\n",
 
159
        debug_ipmovie("sending audio frame with pts %"PRId64" (%d audio frames)\n",
170
160
            audio_pts, s->audio_frame_count);
171
161
 
172
162
        chunk_type = CHUNK_VIDEO;
178
168
        if (av_new_packet(pkt, s->decode_map_chunk_size + s->video_chunk_size))
179
169
            return CHUNK_NOMEM;
180
170
 
 
171
        pkt->pos= s->decode_map_chunk_offset;
181
172
        url_fseek(pb, s->decode_map_chunk_offset, SEEK_SET);
182
173
        s->decode_map_chunk_offset = 0;
183
174
 
184
 
        if (get_buffer(pb, pkt->data, s->decode_map_chunk_size) != 
 
175
        if (get_buffer(pb, pkt->data, s->decode_map_chunk_size) !=
185
176
            s->decode_map_chunk_size) {
186
177
            av_free_packet(pkt);
187
178
            return CHUNK_EOF;
199
190
        pkt->stream_index = s->video_stream_index;
200
191
        pkt->pts = s->video_pts;
201
192
 
 
193
        debug_ipmovie("sending video frame with pts %"PRId64"\n",
 
194
            pkt->pts);
 
195
 
202
196
        s->video_pts += s->frame_pts_inc;
203
197
 
204
198
        chunk_type = CHUNK_VIDEO;
215
209
 
216
210
/* This function loads and processes a single chunk in an IP movie file.
217
211
 * It returns the type of chunk that was processed. */
218
 
static int process_ipmovie_chunk(IPMVEContext *s, ByteIOContext *pb, 
 
212
static int process_ipmovie_chunk(IPMVEContext *s, ByteIOContext *pb,
219
213
    AVPacket *pkt)
220
214
{
221
215
    unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
229
223
    int i, j;
230
224
    int first_color, last_color;
231
225
    int audio_flags;
 
226
    unsigned char r, g, b;
232
227
 
233
228
    /* see if there are any pending packets */
234
229
    chunk_type = load_ipmovie_packet(s, pb, pkt);
235
 
    if ((chunk_type == CHUNK_VIDEO) && (chunk_type != CHUNK_DONE))
 
230
    if (chunk_type != CHUNK_DONE)
236
231
        return chunk_type;
237
232
 
238
233
    /* read the next chunk, wherever the file happens to be pointing */
241
236
    if (get_buffer(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
242
237
        CHUNK_PREAMBLE_SIZE)
243
238
        return CHUNK_BAD;
244
 
    chunk_size = LE_16(&chunk_preamble[0]);
245
 
    chunk_type = LE_16(&chunk_preamble[2]);
 
239
    chunk_size = AV_RL16(&chunk_preamble[0]);
 
240
    chunk_type = AV_RL16(&chunk_preamble[2]);
246
241
 
247
242
    debug_ipmovie("chunk type 0x%04X, 0x%04X bytes: ", chunk_type, chunk_size);
248
243
 
292
287
            break;
293
288
        }
294
289
 
295
 
        opcode_size = LE_16(&opcode_preamble[0]);
 
290
        opcode_size = AV_RL16(&opcode_preamble[0]);
296
291
        opcode_type = opcode_preamble[2];
297
292
        opcode_version = opcode_preamble[3];
298
293
 
330
325
                chunk_type = CHUNK_BAD;
331
326
                break;
332
327
            }
333
 
            s->fps = 1000000 / (LE_32(&scratch[0]) * LE_16(&scratch[4]));
334
 
            s->fps++;  /* above calculation usually yields 14.9; we need 15 */
 
328
            s->fps = 1000000.0 / (AV_RL32(&scratch[0]) * AV_RL16(&scratch[4]));
335
329
            s->frame_pts_inc = 90000 / s->fps;
336
 
            debug_ipmovie("%d frames/second (timer div = %d, subdiv = %d)\n",
337
 
                s->fps, LE_32(&scratch[0]), LE_16(&scratch[4]));
 
330
            debug_ipmovie("  %.2f frames/second (timer div = %d, subdiv = %d)\n",
 
331
                s->fps, AV_RL32(&scratch[0]), AV_RL16(&scratch[4]));
338
332
            break;
339
333
 
340
334
        case OPCODE_INIT_AUDIO_BUFFERS:
349
343
                chunk_type = CHUNK_BAD;
350
344
                break;
351
345
            }
352
 
            s->audio_sample_rate = LE_16(&scratch[4]);
353
 
            audio_flags = LE_16(&scratch[2]);
 
346
            s->audio_sample_rate = AV_RL16(&scratch[4]);
 
347
            audio_flags = AV_RL16(&scratch[2]);
354
348
            /* bit 0 of the flags: 0 = mono, 1 = stereo */
355
349
            s->audio_channels = (audio_flags & 1) + 1;
356
350
            /* bit 1 of the flags: 0 = 8 bit, 1 = 16 bit */
366
360
                s->audio_bits,
367
361
                s->audio_sample_rate,
368
362
                (s->audio_channels == 2) ? "stereo" : "mono",
369
 
                (s->audio_type == CODEC_ID_INTERPLAY_DPCM) ? 
 
363
                (s->audio_type == CODEC_ID_INTERPLAY_DPCM) ?
370
364
                "Interplay audio" : "PCM");
371
365
            break;
372
366
 
387
381
                chunk_type = CHUNK_BAD;
388
382
                break;
389
383
            }
390
 
            s->video_width = LE_16(&scratch[0]) * 8;
391
 
            s->video_height = LE_16(&scratch[2]) * 8;
 
384
            s->video_width = AV_RL16(&scratch[0]) * 8;
 
385
            s->video_height = AV_RL16(&scratch[2]) * 8;
392
386
            debug_ipmovie("video resolution: %d x %d\n",
393
387
                s->video_width, s->video_height);
394
388
            break;
448
442
            }
449
443
 
450
444
            /* load the palette into internal data structure */
451
 
            first_color = LE_16(&scratch[0]);
452
 
            last_color = first_color + LE_16(&scratch[2]);
 
445
            first_color = AV_RL16(&scratch[0]);
 
446
            last_color = first_color + AV_RL16(&scratch[2]) - 1;
453
447
            /* sanity check (since they are 16 bit values) */
454
448
            if ((first_color > 0xFF) || (last_color > 0xFF)) {
455
449
                debug_ipmovie("demux_ipmovie: set_palette indices out of range (%d -> %d)\n",
461
455
            for (i = first_color; i <= last_color; i++) {
462
456
                /* the palette is stored as a 6-bit VGA palette, thus each
463
457
                 * component is shifted up to a 8-bit range */
464
 
                s->palette_control.palette[i * 3 + 0] = scratch[j++] * 4;
465
 
                s->palette_control.palette[i * 3 + 1] = scratch[j++] * 4;
466
 
                s->palette_control.palette[i * 3 + 2] = scratch[j++] * 4;
 
458
                r = scratch[j++] * 4;
 
459
                g = scratch[j++] * 4;
 
460
                b = scratch[j++] * 4;
 
461
                s->palette_control.palette[i] = (r << 16) | (g << 8) | (b);
467
462
            }
468
463
            /* indicate a palette change */
469
464
            s->palette_control.palette_changed = 1;
527
522
    ByteIOContext *pb = &s->pb;
528
523
    AVPacket pkt;
529
524
    AVStream *st;
 
525
    unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
 
526
    int chunk_type;
530
527
 
531
528
    /* initialize private context members */
532
529
    ipmovie->video_pts = ipmovie->audio_frame_count = 0;
540
537
    if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_VIDEO)
541
538
        return AVERROR_INVALIDDATA;
542
539
 
543
 
    /* process the next chunk which should be CHUNK_INIT_AUDIO */
544
 
    if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_AUDIO)
 
540
    /* peek ahead to the next chunk-- if it is an init audio chunk, process
 
541
     * it; if it is the first video chunk, this is a silent file */
 
542
    if (get_buffer(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
 
543
        CHUNK_PREAMBLE_SIZE)
 
544
        return AVERROR_IO;
 
545
    chunk_type = AV_RL16(&chunk_preamble[2]);
 
546
    url_fseek(pb, -CHUNK_PREAMBLE_SIZE, SEEK_CUR);
 
547
 
 
548
    if (chunk_type == CHUNK_VIDEO)
 
549
        ipmovie->audio_type = 0;  /* no audio */
 
550
    else if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_AUDIO)
545
551
        return AVERROR_INVALIDDATA;
546
552
 
547
 
    /* set the pts reference (1 pts = 1/90000) */
548
 
    s->pts_num = 1;
549
 
    s->pts_den = 90000;
550
 
 
551
553
    /* initialize the stream decoders */
552
554
    st = av_new_stream(s, 0);
553
555
    if (!st)
554
556
        return AVERROR_NOMEM;
 
557
    av_set_pts_info(st, 33, 1, 90000);
555
558
    ipmovie->video_stream_index = st->index;
556
 
    st->codec.codec_type = CODEC_TYPE_VIDEO;
557
 
    st->codec.codec_id = CODEC_ID_INTERPLAY_VIDEO;
558
 
    st->codec.codec_tag = 0;  /* no fourcc */
559
 
    st->codec.width = ipmovie->video_width;
560
 
    st->codec.height = ipmovie->video_height;
 
559
    st->codec->codec_type = CODEC_TYPE_VIDEO;
 
560
    st->codec->codec_id = CODEC_ID_INTERPLAY_VIDEO;
 
561
    st->codec->codec_tag = 0;  /* no fourcc */
 
562
    st->codec->width = ipmovie->video_width;
 
563
    st->codec->height = ipmovie->video_height;
561
564
 
562
565
    /* palette considerations */
563
 
    st->codec.extradata_size = sizeof(AVPaletteControl);
564
 
    st->codec.extradata = &ipmovie->palette_control;
 
566
    st->codec->palctrl = &ipmovie->palette_control;
565
567
 
566
 
    st = av_new_stream(s, 0);
567
 
    if (!st)
568
 
        return AVERROR_NOMEM;
569
 
    ipmovie->audio_stream_index = st->index;
570
 
    st->codec.codec_type = CODEC_TYPE_AUDIO;
571
 
    st->codec.codec_id = ipmovie->audio_type;
572
 
    st->codec.codec_tag = 0;  /* no tag */
573
 
    st->codec.channels = ipmovie->audio_channels;
574
 
    st->codec.sample_rate = ipmovie->audio_sample_rate;
575
 
    st->codec.bits_per_sample = ipmovie->audio_bits;
576
 
    st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
577
 
        st->codec.bits_per_sample;
578
 
    if (st->codec.codec_id == CODEC_ID_INTERPLAY_DPCM)
579
 
        st->codec.bit_rate /= 2;
580
 
    st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
 
568
    if (ipmovie->audio_type) {
 
569
        st = av_new_stream(s, 0);
 
570
        if (!st)
 
571
            return AVERROR_NOMEM;
 
572
        av_set_pts_info(st, 33, 1, 90000);
 
573
        ipmovie->audio_stream_index = st->index;
 
574
        st->codec->codec_type = CODEC_TYPE_AUDIO;
 
575
        st->codec->codec_id = ipmovie->audio_type;
 
576
        st->codec->codec_tag = 0;  /* no tag */
 
577
        st->codec->channels = ipmovie->audio_channels;
 
578
        st->codec->sample_rate = ipmovie->audio_sample_rate;
 
579
        st->codec->bits_per_sample = ipmovie->audio_bits;
 
580
        st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
 
581
            st->codec->bits_per_sample;
 
582
        if (st->codec->codec_id == CODEC_ID_INTERPLAY_DPCM)
 
583
            st->codec->bit_rate /= 2;
 
584
        st->codec->block_align = st->codec->channels * st->codec->bits_per_sample;
 
585
    }
581
586
 
582
587
    return 0;
583
588
}
593
598
    if (ret == CHUNK_BAD)
594
599
        ret = AVERROR_INVALIDDATA;
595
600
    else if (ret == CHUNK_EOF)
596
 
        ret = -EIO;
 
601
        ret = AVERROR_IO;
597
602
    else if (ret == CHUNK_NOMEM)
598
603
        ret = AVERROR_NOMEM;
 
604
    else if (ret == CHUNK_VIDEO)
 
605
        ret = 0;
599
606
    else
600
 
        ret = 0;
 
607
        ret = -1;
601
608
 
602
609
    return ret;
603
610
}
609
616
    return 0;
610
617
}
611
618
 
612
 
static AVInputFormat ipmovie_iformat = {
 
619
AVInputFormat ipmovie_demuxer = {
613
620
    "ipmovie",
614
621
    "Interplay MVE format",
615
622
    sizeof(IPMVEContext),
618
625
    ipmovie_read_packet,
619
626
    ipmovie_read_close,
620
627
};
621
 
 
622
 
int ipmovie_init(void)
623
 
{
624
 
    av_register_input_format(&ipmovie_iformat);
625
 
    return 0;
626
 
}
627