* Interplay MVE File Demuxer
* Copyright (c) 2003 The ffmpeg Project
*
- * This library is free software; you can redistribute it and/or
+ * This file is part of Libav.
+ *
+ * Libav is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * version 2.1 of the License, or (at your option) any later version.
*
- * This library is distributed in the hope that it will be useful,
+ * Libav is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * License along with Libav; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
- * @file ipmovie.c
+ * @file
* Interplay MVE file demuxer
* by Mike Melanson (melanson@pcisys.net)
* For more information regarding the Interplay MVE file format, visit:
* up and sending out the chunks.
*/
+#include "libavutil/channel_layout.h"
+#include "libavutil/intreadwrite.h"
#include "avformat.h"
+#include "internal.h"
-/* debugging support: #define DEBUG_IPMOVIE as non-zero to see extremely
- * verbose information about the demux process */
-#define DEBUG_IPMOVIE 0
-
-#if DEBUG_IPMOVIE
-#define debug_ipmovie printf
-#else
-static inline void debug_ipmovie(const char *format, ...) { }
-#endif
-
-
-#define LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
-#define LE_32(x) ((((uint8_t*)(x))[3] << 24) | \
- (((uint8_t*)(x))[2] << 16) | \
- (((uint8_t*)(x))[1] << 8) | \
- ((uint8_t*)(x))[0])
-
-#define IPMOVIE_SIGNATURE "Interplay MVE File\x1A\0"
-#define IPMOVIE_SIGNATURE_SIZE 20
#define CHUNK_PREAMBLE_SIZE 4
#define OPCODE_PREAMBLE_SIZE 4
unsigned char *buf;
int buf_size;
- int fps;
- int frame_pts_inc;
+ uint64_t frame_pts_inc;
+ unsigned int video_bpp;
unsigned int video_width;
unsigned int video_height;
int64_t video_pts;
+ uint32_t palette[256];
+ int has_palette;
+ int changed;
unsigned int audio_bits;
unsigned int audio_channels;
unsigned int audio_sample_rate;
- unsigned int audio_type;
+ enum AVCodecID audio_type;
unsigned int audio_frame_count;
int video_stream_index;
int audio_stream_index;
- offset_t audio_chunk_offset;
+ int64_t audio_chunk_offset;
int audio_chunk_size;
- offset_t video_chunk_offset;
+ int64_t video_chunk_offset;
int video_chunk_size;
- offset_t decode_map_chunk_offset;
+ int64_t decode_map_chunk_offset;
int decode_map_chunk_size;
- offset_t next_chunk_offset;
-
- AVPaletteControl palette_control;
+ int64_t next_chunk_offset;
} IPMVEContext;
-static int load_ipmovie_packet(IPMVEContext *s, ByteIOContext *pb,
+static int load_ipmovie_packet(IPMVEContext *s, AVIOContext *pb,
AVPacket *pkt) {
int chunk_type;
- int64_t audio_pts = 0;
if (s->audio_chunk_offset) {
+ if (s->audio_type == AV_CODEC_ID_NONE) {
+ av_log(NULL, AV_LOG_ERROR, "Can not read audio packet before"
+ "audio codec is known\n");
+ return CHUNK_BAD;
+ }
/* adjust for PCM audio by skipping chunk header */
- if (s->audio_type != CODEC_ID_INTERPLAY_DPCM) {
+ if (s->audio_type != AV_CODEC_ID_INTERPLAY_DPCM) {
s->audio_chunk_offset += 6;
s->audio_chunk_size -= 6;
}
- url_fseek(pb, s->audio_chunk_offset, SEEK_SET);
+ avio_seek(pb, s->audio_chunk_offset, SEEK_SET);
s->audio_chunk_offset = 0;
- /* figure out the audio pts */
- audio_pts = 90000;
- audio_pts *= s->audio_frame_count;
- audio_pts /= s->audio_sample_rate;
-
- if (av_new_packet(pkt, s->audio_chunk_size))
- return CHUNK_NOMEM;
+ if (s->audio_chunk_size != av_get_packet(pb, pkt, s->audio_chunk_size))
+ return CHUNK_EOF;
pkt->stream_index = s->audio_stream_index;
- pkt->pts = audio_pts;
- if (get_buffer(pb, pkt->data, s->audio_chunk_size) !=
- s->audio_chunk_size) {
- av_free_packet(pkt);
- return CHUNK_EOF;
- }
+ pkt->pts = s->audio_frame_count;
/* audio frame maintenance */
- if (s->audio_type != CODEC_ID_INTERPLAY_DPCM)
+ if (s->audio_type != AV_CODEC_ID_INTERPLAY_DPCM)
s->audio_frame_count +=
(s->audio_chunk_size / s->audio_channels / (s->audio_bits / 8));
else
s->audio_frame_count +=
- (s->audio_chunk_size - 6) / s->audio_channels;
+ (s->audio_chunk_size - 6 - s->audio_channels) / s->audio_channels;
- debug_ipmovie("sending audio frame with pts %lld (%d audio frames)\n",
- audio_pts, s->audio_frame_count);
+ av_dlog(NULL, "sending audio frame with pts %"PRId64" (%d audio frames)\n",
+ pkt->pts, s->audio_frame_count);
chunk_type = CHUNK_VIDEO;
if (av_new_packet(pkt, s->decode_map_chunk_size + s->video_chunk_size))
return CHUNK_NOMEM;
- url_fseek(pb, s->decode_map_chunk_offset, SEEK_SET);
+ if (s->has_palette) {
+ uint8_t *pal;
+
+ pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
+ AVPALETTE_SIZE);
+ if (pal) {
+ memcpy(pal, s->palette, AVPALETTE_SIZE);
+ s->has_palette = 0;
+ }
+ }
+
+ if (s->changed) {
+ ff_add_param_change(pkt, 0, 0, 0, s->video_width, s->video_height);
+ s->changed = 0;
+ }
+ pkt->pos= s->decode_map_chunk_offset;
+ avio_seek(pb, s->decode_map_chunk_offset, SEEK_SET);
s->decode_map_chunk_offset = 0;
- if (get_buffer(pb, pkt->data, s->decode_map_chunk_size) !=
+ if (avio_read(pb, pkt->data, s->decode_map_chunk_size) !=
s->decode_map_chunk_size) {
av_free_packet(pkt);
return CHUNK_EOF;
}
- url_fseek(pb, s->video_chunk_offset, SEEK_SET);
+ avio_seek(pb, s->video_chunk_offset, SEEK_SET);
s->video_chunk_offset = 0;
- if (get_buffer(pb, pkt->data + s->decode_map_chunk_size,
+ if (avio_read(pb, pkt->data + s->decode_map_chunk_size,
s->video_chunk_size) != s->video_chunk_size) {
av_free_packet(pkt);
return CHUNK_EOF;
pkt->stream_index = s->video_stream_index;
pkt->pts = s->video_pts;
+ av_dlog(NULL, "sending video frame with pts %"PRId64"\n", pkt->pts);
+
s->video_pts += s->frame_pts_inc;
chunk_type = CHUNK_VIDEO;
} else {
- url_fseek(pb, s->next_chunk_offset, SEEK_SET);
+ avio_seek(pb, s->next_chunk_offset, SEEK_SET);
chunk_type = CHUNK_DONE;
}
/* This function loads and processes a single chunk in an IP movie file.
* It returns the type of chunk that was processed. */
-static int process_ipmovie_chunk(IPMVEContext *s, ByteIOContext *pb,
+static int process_ipmovie_chunk(IPMVEContext *s, AVIOContext *pb,
AVPacket *pkt)
{
unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
int i, j;
int first_color, last_color;
int audio_flags;
+ unsigned char r, g, b;
+ unsigned int width, height;
/* see if there are any pending packets */
chunk_type = load_ipmovie_packet(s, pb, pkt);
- if ((chunk_type == CHUNK_VIDEO) && (chunk_type != CHUNK_DONE))
+ if (chunk_type != CHUNK_DONE)
return chunk_type;
/* read the next chunk, wherever the file happens to be pointing */
- if (url_feof(pb))
+ if (pb->eof_reached)
return CHUNK_EOF;
- if (get_buffer(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
+ if (avio_read(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
CHUNK_PREAMBLE_SIZE)
return CHUNK_BAD;
- chunk_size = LE_16(&chunk_preamble[0]);
- chunk_type = LE_16(&chunk_preamble[2]);
+ chunk_size = AV_RL16(&chunk_preamble[0]);
+ chunk_type = AV_RL16(&chunk_preamble[2]);
- debug_ipmovie("chunk type 0x%04X, 0x%04X bytes: ", chunk_type, chunk_size);
+ av_dlog(NULL, "chunk type 0x%04X, 0x%04X bytes: ", chunk_type, chunk_size);
switch (chunk_type) {
case CHUNK_INIT_AUDIO:
- debug_ipmovie("initialize audio\n");
+ av_dlog(NULL, "initialize audio\n");
break;
case CHUNK_AUDIO_ONLY:
- debug_ipmovie("audio only\n");
+ av_dlog(NULL, "audio only\n");
break;
case CHUNK_INIT_VIDEO:
- debug_ipmovie("initialize video\n");
+ av_dlog(NULL, "initialize video\n");
break;
case CHUNK_VIDEO:
- debug_ipmovie("video (and audio)\n");
+ av_dlog(NULL, "video (and audio)\n");
break;
case CHUNK_SHUTDOWN:
- debug_ipmovie("shutdown\n");
+ av_dlog(NULL, "shutdown\n");
break;
case CHUNK_END:
- debug_ipmovie("end\n");
+ av_dlog(NULL, "end\n");
break;
default:
- debug_ipmovie("invalid chunk\n");
+ av_dlog(NULL, "invalid chunk\n");
chunk_type = CHUNK_BAD;
break;
while ((chunk_size > 0) && (chunk_type != CHUNK_BAD)) {
/* read the next chunk, wherever the file happens to be pointing */
- if (url_feof(pb)) {
+ if (pb->eof_reached) {
chunk_type = CHUNK_EOF;
break;
}
- if (get_buffer(pb, opcode_preamble, CHUNK_PREAMBLE_SIZE) !=
+ if (avio_read(pb, opcode_preamble, CHUNK_PREAMBLE_SIZE) !=
CHUNK_PREAMBLE_SIZE) {
chunk_type = CHUNK_BAD;
break;
}
- opcode_size = LE_16(&opcode_preamble[0]);
+ opcode_size = AV_RL16(&opcode_preamble[0]);
opcode_type = opcode_preamble[2];
opcode_version = opcode_preamble[3];
chunk_size -= OPCODE_PREAMBLE_SIZE;
chunk_size -= opcode_size;
if (chunk_size < 0) {
- debug_ipmovie("chunk_size countdown just went negative\n");
+ av_dlog(NULL, "chunk_size countdown just went negative\n");
chunk_type = CHUNK_BAD;
break;
}
- debug_ipmovie(" opcode type %02X, version %d, 0x%04X bytes: ",
- opcode_type, opcode_version, opcode_size);
+ av_dlog(NULL, " opcode type %02X, version %d, 0x%04X bytes: ",
+ opcode_type, opcode_version, opcode_size);
switch (opcode_type) {
case OPCODE_END_OF_STREAM:
- debug_ipmovie("end of stream\n");
- url_fseek(pb, opcode_size, SEEK_CUR);
+ av_dlog(NULL, "end of stream\n");
+ avio_skip(pb, opcode_size);
break;
case OPCODE_END_OF_CHUNK:
- debug_ipmovie("end of chunk\n");
- url_fseek(pb, opcode_size, SEEK_CUR);
+ av_dlog(NULL, "end of chunk\n");
+ avio_skip(pb, opcode_size);
break;
case OPCODE_CREATE_TIMER:
- debug_ipmovie("create timer\n");
+ av_dlog(NULL, "create timer\n");
if ((opcode_version > 0) || (opcode_size > 6)) {
- debug_ipmovie("bad create_timer opcode\n");
+ av_dlog(NULL, "bad create_timer opcode\n");
chunk_type = CHUNK_BAD;
break;
}
- if (get_buffer(pb, scratch, opcode_size) !=
+ if (avio_read(pb, scratch, opcode_size) !=
opcode_size) {
chunk_type = CHUNK_BAD;
break;
}
- s->fps = 1000000 / (LE_32(&scratch[0]) * LE_16(&scratch[4]));
- s->fps++; /* above calculation usually yields 14.9; we need 15 */
- s->frame_pts_inc = 90000 / s->fps;
- debug_ipmovie("%d frames/second (timer div = %d, subdiv = %d)\n",
- s->fps, LE_32(&scratch[0]), LE_16(&scratch[4]));
+ s->frame_pts_inc = ((uint64_t)AV_RL32(&scratch[0])) * AV_RL16(&scratch[4]);
+ av_dlog(NULL, " %.2f frames/second (timer div = %d, subdiv = %d)\n",
+ 1000000.0 / s->frame_pts_inc, AV_RL32(&scratch[0]),
+ AV_RL16(&scratch[4]));
break;
case OPCODE_INIT_AUDIO_BUFFERS:
- debug_ipmovie("initialize audio buffers\n");
+ av_dlog(NULL, "initialize audio buffers\n");
if ((opcode_version > 1) || (opcode_size > 10)) {
- debug_ipmovie("bad init_audio_buffers opcode\n");
+ av_dlog(NULL, "bad init_audio_buffers opcode\n");
chunk_type = CHUNK_BAD;
break;
}
- if (get_buffer(pb, scratch, opcode_size) !=
+ if (avio_read(pb, scratch, opcode_size) !=
opcode_size) {
chunk_type = CHUNK_BAD;
break;
}
- s->audio_sample_rate = LE_16(&scratch[4]);
- audio_flags = LE_16(&scratch[2]);
+ s->audio_sample_rate = AV_RL16(&scratch[4]);
+ audio_flags = AV_RL16(&scratch[2]);
/* bit 0 of the flags: 0 = mono, 1 = stereo */
s->audio_channels = (audio_flags & 1) + 1;
/* bit 1 of the flags: 0 = 8 bit, 1 = 16 bit */
s->audio_bits = (((audio_flags >> 1) & 1) + 1) * 8;
/* bit 2 indicates compressed audio in version 1 opcode */
if ((opcode_version == 1) && (audio_flags & 0x4))
- s->audio_type = CODEC_ID_INTERPLAY_DPCM;
+ s->audio_type = AV_CODEC_ID_INTERPLAY_DPCM;
else if (s->audio_bits == 16)
- s->audio_type = CODEC_ID_PCM_S16LE;
+ s->audio_type = AV_CODEC_ID_PCM_S16LE;
else
- s->audio_type = CODEC_ID_PCM_U8;
- debug_ipmovie("audio: %d bits, %d Hz, %s, %s format\n",
- s->audio_bits,
- s->audio_sample_rate,
- (s->audio_channels == 2) ? "stereo" : "mono",
- (s->audio_type == CODEC_ID_INTERPLAY_DPCM) ?
- "Interplay audio" : "PCM");
+ s->audio_type = AV_CODEC_ID_PCM_U8;
+ av_dlog(NULL, "audio: %d bits, %d Hz, %s, %s format\n",
+ s->audio_bits, s->audio_sample_rate,
+ (s->audio_channels == 2) ? "stereo" : "mono",
+ (s->audio_type == AV_CODEC_ID_INTERPLAY_DPCM) ?
+ "Interplay audio" : "PCM");
break;
case OPCODE_START_STOP_AUDIO:
- debug_ipmovie("start/stop audio\n");
- url_fseek(pb, opcode_size, SEEK_CUR);
+ av_dlog(NULL, "start/stop audio\n");
+ avio_skip(pb, opcode_size);
break;
case OPCODE_INIT_VIDEO_BUFFERS:
- debug_ipmovie("initialize video buffers\n");
+ av_dlog(NULL, "initialize video buffers\n");
if ((opcode_version > 2) || (opcode_size > 8)) {
- debug_ipmovie("bad init_video_buffers opcode\n");
+ av_dlog(NULL, "bad init_video_buffers opcode\n");
chunk_type = CHUNK_BAD;
break;
}
- if (get_buffer(pb, scratch, opcode_size) !=
+ if (avio_read(pb, scratch, opcode_size) !=
opcode_size) {
chunk_type = CHUNK_BAD;
break;
}
- s->video_width = LE_16(&scratch[0]) * 8;
- s->video_height = LE_16(&scratch[2]) * 8;
- debug_ipmovie("video resolution: %d x %d\n",
- s->video_width, s->video_height);
+ width = AV_RL16(&scratch[0]) * 8;
+ height = AV_RL16(&scratch[2]) * 8;
+ if (width != s->video_width) {
+ s->video_width = width;
+ s->changed++;
+ }
+ if (height != s->video_height) {
+ s->video_height = height;
+ s->changed++;
+ }
+ if (opcode_version < 2 || !AV_RL16(&scratch[6])) {
+ s->video_bpp = 8;
+ } else {
+ s->video_bpp = 16;
+ }
+ av_dlog(NULL, "video resolution: %d x %d\n",
+ s->video_width, s->video_height);
break;
case OPCODE_UNKNOWN_06:
case OPCODE_UNKNOWN_13:
case OPCODE_UNKNOWN_14:
case OPCODE_UNKNOWN_15:
- debug_ipmovie("unknown (but documented) opcode %02X\n", opcode_type);
- url_fseek(pb, opcode_size, SEEK_CUR);
+ av_dlog(NULL, "unknown (but documented) opcode %02X\n", opcode_type);
+ avio_skip(pb, opcode_size);
break;
case OPCODE_SEND_BUFFER:
- debug_ipmovie("send buffer\n");
- url_fseek(pb, opcode_size, SEEK_CUR);
+ av_dlog(NULL, "send buffer\n");
+ avio_skip(pb, opcode_size);
break;
case OPCODE_AUDIO_FRAME:
- debug_ipmovie("audio frame\n");
+ av_dlog(NULL, "audio frame\n");
/* log position and move on for now */
- s->audio_chunk_offset = url_ftell(pb);
+ s->audio_chunk_offset = avio_tell(pb);
s->audio_chunk_size = opcode_size;
- url_fseek(pb, opcode_size, SEEK_CUR);
+ avio_skip(pb, opcode_size);
break;
case OPCODE_SILENCE_FRAME:
- debug_ipmovie("silence frame\n");
- url_fseek(pb, opcode_size, SEEK_CUR);
+ av_dlog(NULL, "silence frame\n");
+ avio_skip(pb, opcode_size);
break;
case OPCODE_INIT_VIDEO_MODE:
- debug_ipmovie("initialize video mode\n");
- url_fseek(pb, opcode_size, SEEK_CUR);
+ av_dlog(NULL, "initialize video mode\n");
+ avio_skip(pb, opcode_size);
break;
case OPCODE_CREATE_GRADIENT:
- debug_ipmovie("create gradient\n");
- url_fseek(pb, opcode_size, SEEK_CUR);
+ av_dlog(NULL, "create gradient\n");
+ avio_skip(pb, opcode_size);
break;
case OPCODE_SET_PALETTE:
- debug_ipmovie("set palette\n");
+ av_dlog(NULL, "set palette\n");
/* check for the logical maximum palette size
* (3 * 256 + 4 bytes) */
if (opcode_size > 0x304) {
- debug_ipmovie("demux_ipmovie: set_palette opcode too large\n");
+ av_dlog(NULL, "demux_ipmovie: set_palette opcode too large\n");
chunk_type = CHUNK_BAD;
break;
}
- if (get_buffer(pb, scratch, opcode_size) != opcode_size) {
+ if (avio_read(pb, scratch, opcode_size) != opcode_size) {
chunk_type = CHUNK_BAD;
break;
}
/* load the palette into internal data structure */
- first_color = LE_16(&scratch[0]);
- last_color = LE_16(&scratch[2]);
+ first_color = AV_RL16(&scratch[0]);
+ last_color = first_color + AV_RL16(&scratch[2]) - 1;
/* sanity check (since they are 16 bit values) */
if ((first_color > 0xFF) || (last_color > 0xFF)) {
- debug_ipmovie("demux_ipmovie: set_palette indices out of range (%d -> %d)\n",
+ av_dlog(NULL, "demux_ipmovie: set_palette indexes out of range (%d -> %d)\n",
first_color, last_color);
chunk_type = CHUNK_BAD;
break;
for (i = first_color; i <= last_color; i++) {
/* the palette is stored as a 6-bit VGA palette, thus each
* component is shifted up to a 8-bit range */
- s->palette_control.palette[i * 3 + 0] = scratch[j++] * 4;
- s->palette_control.palette[i * 3 + 1] = scratch[j++] * 4;
- s->palette_control.palette[i * 3 + 2] = scratch[j++] * 4;
+ r = scratch[j++] * 4;
+ g = scratch[j++] * 4;
+ b = scratch[j++] * 4;
+ s->palette[i] = (r << 16) | (g << 8) | (b);
}
- /* indicate a palette change */
- s->palette_control.palette_changed = 1;
+ s->has_palette = 1;
break;
case OPCODE_SET_PALETTE_COMPRESSED:
- debug_ipmovie("set palette compressed\n");
- url_fseek(pb, opcode_size, SEEK_CUR);
+ av_dlog(NULL, "set palette compressed\n");
+ avio_skip(pb, opcode_size);
break;
case OPCODE_SET_DECODING_MAP:
- debug_ipmovie("set decoding map\n");
+ av_dlog(NULL, "set decoding map\n");
/* log position and move on for now */
- s->decode_map_chunk_offset = url_ftell(pb);
+ s->decode_map_chunk_offset = avio_tell(pb);
s->decode_map_chunk_size = opcode_size;
- url_fseek(pb, opcode_size, SEEK_CUR);
+ avio_skip(pb, opcode_size);
break;
case OPCODE_VIDEO_DATA:
- debug_ipmovie("set video data\n");
+ av_dlog(NULL, "set video data\n");
/* log position and move on for now */
- s->video_chunk_offset = url_ftell(pb);
+ s->video_chunk_offset = avio_tell(pb);
s->video_chunk_size = opcode_size;
- url_fseek(pb, opcode_size, SEEK_CUR);
+ avio_skip(pb, opcode_size);
break;
default:
- debug_ipmovie("*** unknown opcode type\n");
+ av_dlog(NULL, "*** unknown opcode type\n");
chunk_type = CHUNK_BAD;
break;
}
/* make a note of where the stream is sitting */
- s->next_chunk_offset = url_ftell(pb);
+ s->next_chunk_offset = avio_tell(pb);
/* dispatch the first of any pending packets */
if ((chunk_type == CHUNK_VIDEO) || (chunk_type == CHUNK_AUDIO_ONLY))
return chunk_type;
}
+static const char signature[] = "Interplay MVE File\x1A\0\x1A";
+
static int ipmovie_probe(AVProbeData *p)
{
- if (p->buf_size < IPMOVIE_SIGNATURE_SIZE)
- return 0;
- if (strncmp(p->buf, IPMOVIE_SIGNATURE, IPMOVIE_SIGNATURE_SIZE) != 0)
- return 0;
+ uint8_t *b = p->buf;
+ uint8_t *b_end = p->buf + p->buf_size - sizeof(signature);
+ do {
+ if (memcmp(b++, signature, sizeof(signature)) == 0)
+ return AVPROBE_SCORE_MAX;
+ } while (b < b_end);
- return AVPROBE_SCORE_MAX;
+ return 0;
}
-static int ipmovie_read_header(AVFormatContext *s,
- AVFormatParameters *ap)
+static int ipmovie_read_header(AVFormatContext *s)
{
- IPMVEContext *ipmovie = (IPMVEContext *)s->priv_data;
- ByteIOContext *pb = &s->pb;
+ IPMVEContext *ipmovie = s->priv_data;
+ AVIOContext *pb = s->pb;
AVPacket pkt;
AVStream *st;
-
+ unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
+ int chunk_type;
+ uint8_t signature_buffer[sizeof(signature)];
+
+ avio_read(pb, signature_buffer, sizeof(signature_buffer));
+ while (memcmp(signature_buffer, signature, sizeof(signature))) {
+ memmove(signature_buffer, signature_buffer + 1, sizeof(signature_buffer) - 1);
+ signature_buffer[sizeof(signature_buffer) - 1] = avio_r8(pb);
+ if (pb->eof_reached)
+ return AVERROR_EOF;
+ }
/* initialize private context members */
ipmovie->video_pts = ipmovie->audio_frame_count = 0;
ipmovie->audio_chunk_offset = ipmovie->video_chunk_offset =
ipmovie->decode_map_chunk_offset = 0;
/* on the first read, this will position the stream at the first chunk */
- ipmovie->next_chunk_offset = IPMOVIE_SIGNATURE_SIZE + 6;
+ ipmovie->next_chunk_offset = avio_tell(pb) + 4;
/* process the first chunk which should be CHUNK_INIT_VIDEO */
if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_VIDEO)
return AVERROR_INVALIDDATA;
- /* process the next chunk which should be CHUNK_INIT_AUDIO */
- if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_AUDIO)
- return AVERROR_INVALIDDATA;
+ /* peek ahead to the next chunk-- if it is an init audio chunk, process
+ * it; if it is the first video chunk, this is a silent file */
+ if (avio_read(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
+ CHUNK_PREAMBLE_SIZE)
+ return AVERROR(EIO);
+ chunk_type = AV_RL16(&chunk_preamble[2]);
+ avio_seek(pb, -CHUNK_PREAMBLE_SIZE, SEEK_CUR);
- /* set the pts reference (1 pts = 1/90000) */
- s->pts_num = 1;
- s->pts_den = 90000;
+ if (chunk_type == CHUNK_VIDEO)
+ ipmovie->audio_type = AV_CODEC_ID_NONE; /* no audio */
+ else if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_AUDIO)
+ return AVERROR_INVALIDDATA;
/* initialize the stream decoders */
- st = av_new_stream(s, 0);
+ st = avformat_new_stream(s, NULL);
if (!st)
- return AVERROR_NOMEM;
+ return AVERROR(ENOMEM);
+ avpriv_set_pts_info(st, 63, 1, 1000000);
ipmovie->video_stream_index = st->index;
- st->codec.codec_type = CODEC_TYPE_VIDEO;
- st->codec.codec_id = CODEC_ID_INTERPLAY_VIDEO;
- st->codec.codec_tag = 0; /* no fourcc */
- st->codec.width = ipmovie->video_width;
- st->codec.height = ipmovie->video_height;
-
- /* palette considerations */
- st->codec.extradata_size = sizeof(AVPaletteControl);
- st->codec.extradata = &ipmovie->palette_control;
-
- st = av_new_stream(s, 0);
- if (!st)
- return AVERROR_NOMEM;
- ipmovie->audio_stream_index = st->index;
- st->codec.codec_type = CODEC_TYPE_AUDIO;
- st->codec.codec_id = ipmovie->audio_type;
- st->codec.codec_tag = 0; /* no tag */
- st->codec.channels = ipmovie->audio_channels;
- st->codec.sample_rate = ipmovie->audio_sample_rate;
- st->codec.bits_per_sample = ipmovie->audio_bits;
- st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
- st->codec.bits_per_sample;
- if (st->codec.codec_id == CODEC_ID_INTERPLAY_DPCM)
- st->codec.bit_rate /= 2;
- st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
+ st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
+ st->codec->codec_id = AV_CODEC_ID_INTERPLAY_VIDEO;
+ st->codec->codec_tag = 0; /* no fourcc */
+ st->codec->width = ipmovie->video_width;
+ st->codec->height = ipmovie->video_height;
+ st->codec->bits_per_coded_sample = ipmovie->video_bpp;
+
+ if (ipmovie->audio_type) {
+ st = avformat_new_stream(s, NULL);
+ if (!st)
+ return AVERROR(ENOMEM);
+ avpriv_set_pts_info(st, 32, 1, ipmovie->audio_sample_rate);
+ ipmovie->audio_stream_index = st->index;
+ st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
+ st->codec->codec_id = ipmovie->audio_type;
+ st->codec->codec_tag = 0; /* no tag */
+ st->codec->channels = ipmovie->audio_channels;
+ st->codec->channel_layout = st->codec->channels == 1 ? AV_CH_LAYOUT_MONO :
+ AV_CH_LAYOUT_STEREO;
+ st->codec->sample_rate = ipmovie->audio_sample_rate;
+ st->codec->bits_per_coded_sample = ipmovie->audio_bits;
+ st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
+ st->codec->bits_per_coded_sample;
+ if (st->codec->codec_id == AV_CODEC_ID_INTERPLAY_DPCM)
+ st->codec->bit_rate /= 2;
+ st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
+ }
return 0;
}
static int ipmovie_read_packet(AVFormatContext *s,
AVPacket *pkt)
{
- IPMVEContext *ipmovie = (IPMVEContext *)s->priv_data;
- ByteIOContext *pb = &s->pb;
+ IPMVEContext *ipmovie = s->priv_data;
+ AVIOContext *pb = s->pb;
int ret;
ret = process_ipmovie_chunk(ipmovie, pb, pkt);
if (ret == CHUNK_BAD)
ret = AVERROR_INVALIDDATA;
else if (ret == CHUNK_EOF)
- ret = -EIO;
+ ret = AVERROR(EIO);
else if (ret == CHUNK_NOMEM)
- ret = AVERROR_NOMEM;
- else
+ ret = AVERROR(ENOMEM);
+ else if (ret == CHUNK_VIDEO)
ret = 0;
+ else
+ ret = -1;
return ret;
}
-static int ipmovie_read_close(AVFormatContext *s)
-{
-// IPMVEContext *ipmovie = (IPMVEContext *)s->priv_data;
-
- return 0;
-}
-
-static AVInputFormat ipmovie_iformat = {
- "ipmovie",
- "Interplay MVE format",
- sizeof(IPMVEContext),
- ipmovie_probe,
- ipmovie_read_header,
- ipmovie_read_packet,
- ipmovie_read_close,
+AVInputFormat ff_ipmovie_demuxer = {
+ .name = "ipmovie",
+ .long_name = NULL_IF_CONFIG_SMALL("Interplay MVE"),
+ .priv_data_size = sizeof(IPMVEContext),
+ .read_probe = ipmovie_probe,
+ .read_header = ipmovie_read_header,
+ .read_packet = ipmovie_read_packet,
};
-
-int ipmovie_init(void)
-{
- av_register_input_format(&ipmovie_iformat);
- return 0;
-}
-