00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00035 #include "avformat.h"
00036
00037
00038
00039 #define DEBUG_IPMOVIE 0
00040
00041 #if DEBUG_IPMOVIE
00042 #define debug_ipmovie printf
00043 #else
00044 static inline void debug_ipmovie(const char *format, ...) { }
00045 #endif
00046
00047 #define IPMOVIE_SIGNATURE "Interplay MVE File\x1A\0"
00048 #define IPMOVIE_SIGNATURE_SIZE 20
00049 #define CHUNK_PREAMBLE_SIZE 4
00050 #define OPCODE_PREAMBLE_SIZE 4
00051
00052 #define CHUNK_INIT_AUDIO 0x0000
00053 #define CHUNK_AUDIO_ONLY 0x0001
00054 #define CHUNK_INIT_VIDEO 0x0002
00055 #define CHUNK_VIDEO 0x0003
00056 #define CHUNK_SHUTDOWN 0x0004
00057 #define CHUNK_END 0x0005
00058
00059 #define CHUNK_DONE 0xFFFC
00060 #define CHUNK_NOMEM 0xFFFD
00061 #define CHUNK_EOF 0xFFFE
00062 #define CHUNK_BAD 0xFFFF
00063
00064 #define OPCODE_END_OF_STREAM 0x00
00065 #define OPCODE_END_OF_CHUNK 0x01
00066 #define OPCODE_CREATE_TIMER 0x02
00067 #define OPCODE_INIT_AUDIO_BUFFERS 0x03
00068 #define OPCODE_START_STOP_AUDIO 0x04
00069 #define OPCODE_INIT_VIDEO_BUFFERS 0x05
00070 #define OPCODE_UNKNOWN_06 0x06
00071 #define OPCODE_SEND_BUFFER 0x07
00072 #define OPCODE_AUDIO_FRAME 0x08
00073 #define OPCODE_SILENCE_FRAME 0x09
00074 #define OPCODE_INIT_VIDEO_MODE 0x0A
00075 #define OPCODE_CREATE_GRADIENT 0x0B
00076 #define OPCODE_SET_PALETTE 0x0C
00077 #define OPCODE_SET_PALETTE_COMPRESSED 0x0D
00078 #define OPCODE_UNKNOWN_0E 0x0E
00079 #define OPCODE_SET_DECODING_MAP 0x0F
00080 #define OPCODE_UNKNOWN_10 0x10
00081 #define OPCODE_VIDEO_DATA 0x11
00082 #define OPCODE_UNKNOWN_12 0x12
00083 #define OPCODE_UNKNOWN_13 0x13
00084 #define OPCODE_UNKNOWN_14 0x14
00085 #define OPCODE_UNKNOWN_15 0x15
00086
00087 #define PALETTE_COUNT 256
00088
00089 typedef struct IPMVEContext {
00090
00091 unsigned char *buf;
00092 int buf_size;
00093
00094 float fps;
00095 int frame_pts_inc;
00096
00097 unsigned int video_width;
00098 unsigned int video_height;
00099 int64_t video_pts;
00100
00101 unsigned int audio_bits;
00102 unsigned int audio_channels;
00103 unsigned int audio_sample_rate;
00104 unsigned int audio_type;
00105 unsigned int audio_frame_count;
00106
00107 int video_stream_index;
00108 int audio_stream_index;
00109
00110 offset_t audio_chunk_offset;
00111 int audio_chunk_size;
00112 offset_t video_chunk_offset;
00113 int video_chunk_size;
00114 offset_t decode_map_chunk_offset;
00115 int decode_map_chunk_size;
00116
00117 offset_t next_chunk_offset;
00118
00119 AVPaletteControl palette_control;
00120
00121 } IPMVEContext;
00122
00123 static int load_ipmovie_packet(IPMVEContext *s, ByteIOContext *pb,
00124 AVPacket *pkt) {
00125
00126 int chunk_type;
00127 int64_t audio_pts = 0;
00128
00129 if (s->audio_chunk_offset) {
00130
00131
00132 if (s->audio_type != CODEC_ID_INTERPLAY_DPCM) {
00133 s->audio_chunk_offset += 6;
00134 s->audio_chunk_size -= 6;
00135 }
00136
00137 url_fseek(pb, s->audio_chunk_offset, SEEK_SET);
00138 s->audio_chunk_offset = 0;
00139
00140
00141 audio_pts = 90000;
00142 audio_pts *= s->audio_frame_count;
00143 audio_pts /= s->audio_sample_rate;
00144
00145 if (s->audio_chunk_size != av_get_packet(pb, pkt, s->audio_chunk_size))
00146 return CHUNK_EOF;
00147
00148 pkt->stream_index = s->audio_stream_index;
00149 pkt->pts = audio_pts;
00150
00151
00152 if (s->audio_type != CODEC_ID_INTERPLAY_DPCM)
00153 s->audio_frame_count +=
00154 (s->audio_chunk_size / s->audio_channels / (s->audio_bits / 8));
00155 else
00156 s->audio_frame_count +=
00157 (s->audio_chunk_size - 6) / s->audio_channels;
00158
00159 debug_ipmovie("sending audio frame with pts %"PRId64" (%d audio frames)\n",
00160 audio_pts, s->audio_frame_count);
00161
00162 chunk_type = CHUNK_VIDEO;
00163
00164 } else if (s->decode_map_chunk_offset) {
00165
00166
00167
00168 if (av_new_packet(pkt, s->decode_map_chunk_size + s->video_chunk_size))
00169 return CHUNK_NOMEM;
00170
00171 pkt->pos= s->decode_map_chunk_offset;
00172 url_fseek(pb, s->decode_map_chunk_offset, SEEK_SET);
00173 s->decode_map_chunk_offset = 0;
00174
00175 if (get_buffer(pb, pkt->data, s->decode_map_chunk_size) !=
00176 s->decode_map_chunk_size) {
00177 av_free_packet(pkt);
00178 return CHUNK_EOF;
00179 }
00180
00181 url_fseek(pb, s->video_chunk_offset, SEEK_SET);
00182 s->video_chunk_offset = 0;
00183
00184 if (get_buffer(pb, pkt->data + s->decode_map_chunk_size,
00185 s->video_chunk_size) != s->video_chunk_size) {
00186 av_free_packet(pkt);
00187 return CHUNK_EOF;
00188 }
00189
00190 pkt->stream_index = s->video_stream_index;
00191 pkt->pts = s->video_pts;
00192
00193 debug_ipmovie("sending video frame with pts %"PRId64"\n",
00194 pkt->pts);
00195
00196 s->video_pts += s->frame_pts_inc;
00197
00198 chunk_type = CHUNK_VIDEO;
00199
00200 } else {
00201
00202 url_fseek(pb, s->next_chunk_offset, SEEK_SET);
00203 chunk_type = CHUNK_DONE;
00204
00205 }
00206
00207 return chunk_type;
00208 }
00209
00210
00211
00212 static int process_ipmovie_chunk(IPMVEContext *s, ByteIOContext *pb,
00213 AVPacket *pkt)
00214 {
00215 unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
00216 int chunk_type;
00217 int chunk_size;
00218 unsigned char opcode_preamble[OPCODE_PREAMBLE_SIZE];
00219 unsigned char opcode_type;
00220 unsigned char opcode_version;
00221 int opcode_size;
00222 unsigned char scratch[1024];
00223 int i, j;
00224 int first_color, last_color;
00225 int audio_flags;
00226 unsigned char r, g, b;
00227
00228
00229 chunk_type = load_ipmovie_packet(s, pb, pkt);
00230 if (chunk_type != CHUNK_DONE)
00231 return chunk_type;
00232
00233
00234 if (url_feof(pb))
00235 return CHUNK_EOF;
00236 if (get_buffer(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
00237 CHUNK_PREAMBLE_SIZE)
00238 return CHUNK_BAD;
00239 chunk_size = AV_RL16(&chunk_preamble[0]);
00240 chunk_type = AV_RL16(&chunk_preamble[2]);
00241
00242 debug_ipmovie("chunk type 0x%04X, 0x%04X bytes: ", chunk_type, chunk_size);
00243
00244 switch (chunk_type) {
00245
00246 case CHUNK_INIT_AUDIO:
00247 debug_ipmovie("initialize audio\n");
00248 break;
00249
00250 case CHUNK_AUDIO_ONLY:
00251 debug_ipmovie("audio only\n");
00252 break;
00253
00254 case CHUNK_INIT_VIDEO:
00255 debug_ipmovie("initialize video\n");
00256 break;
00257
00258 case CHUNK_VIDEO:
00259 debug_ipmovie("video (and audio)\n");
00260 break;
00261
00262 case CHUNK_SHUTDOWN:
00263 debug_ipmovie("shutdown\n");
00264 break;
00265
00266 case CHUNK_END:
00267 debug_ipmovie("end\n");
00268 break;
00269
00270 default:
00271 debug_ipmovie("invalid chunk\n");
00272 chunk_type = CHUNK_BAD;
00273 break;
00274
00275 }
00276
00277 while ((chunk_size > 0) && (chunk_type != CHUNK_BAD)) {
00278
00279
00280 if (url_feof(pb)) {
00281 chunk_type = CHUNK_EOF;
00282 break;
00283 }
00284 if (get_buffer(pb, opcode_preamble, CHUNK_PREAMBLE_SIZE) !=
00285 CHUNK_PREAMBLE_SIZE) {
00286 chunk_type = CHUNK_BAD;
00287 break;
00288 }
00289
00290 opcode_size = AV_RL16(&opcode_preamble[0]);
00291 opcode_type = opcode_preamble[2];
00292 opcode_version = opcode_preamble[3];
00293
00294 chunk_size -= OPCODE_PREAMBLE_SIZE;
00295 chunk_size -= opcode_size;
00296 if (chunk_size < 0) {
00297 debug_ipmovie("chunk_size countdown just went negative\n");
00298 chunk_type = CHUNK_BAD;
00299 break;
00300 }
00301
00302 debug_ipmovie(" opcode type %02X, version %d, 0x%04X bytes: ",
00303 opcode_type, opcode_version, opcode_size);
00304 switch (opcode_type) {
00305
00306 case OPCODE_END_OF_STREAM:
00307 debug_ipmovie("end of stream\n");
00308 url_fseek(pb, opcode_size, SEEK_CUR);
00309 break;
00310
00311 case OPCODE_END_OF_CHUNK:
00312 debug_ipmovie("end of chunk\n");
00313 url_fseek(pb, opcode_size, SEEK_CUR);
00314 break;
00315
00316 case OPCODE_CREATE_TIMER:
00317 debug_ipmovie("create timer\n");
00318 if ((opcode_version > 0) || (opcode_size > 6)) {
00319 debug_ipmovie("bad create_timer opcode\n");
00320 chunk_type = CHUNK_BAD;
00321 break;
00322 }
00323 if (get_buffer(pb, scratch, opcode_size) !=
00324 opcode_size) {
00325 chunk_type = CHUNK_BAD;
00326 break;
00327 }
00328 s->fps = 1000000.0 / (AV_RL32(&scratch[0]) * AV_RL16(&scratch[4]));
00329 s->frame_pts_inc = 90000 / s->fps;
00330 debug_ipmovie(" %.2f frames/second (timer div = %d, subdiv = %d)\n",
00331 s->fps, AV_RL32(&scratch[0]), AV_RL16(&scratch[4]));
00332 break;
00333
00334 case OPCODE_INIT_AUDIO_BUFFERS:
00335 debug_ipmovie("initialize audio buffers\n");
00336 if ((opcode_version > 1) || (opcode_size > 10)) {
00337 debug_ipmovie("bad init_audio_buffers opcode\n");
00338 chunk_type = CHUNK_BAD;
00339 break;
00340 }
00341 if (get_buffer(pb, scratch, opcode_size) !=
00342 opcode_size) {
00343 chunk_type = CHUNK_BAD;
00344 break;
00345 }
00346 s->audio_sample_rate = AV_RL16(&scratch[4]);
00347 audio_flags = AV_RL16(&scratch[2]);
00348
00349 s->audio_channels = (audio_flags & 1) + 1;
00350
00351 s->audio_bits = (((audio_flags >> 1) & 1) + 1) * 8;
00352
00353 if ((opcode_version == 1) && (audio_flags & 0x4))
00354 s->audio_type = CODEC_ID_INTERPLAY_DPCM;
00355 else if (s->audio_bits == 16)
00356 s->audio_type = CODEC_ID_PCM_S16LE;
00357 else
00358 s->audio_type = CODEC_ID_PCM_U8;
00359 debug_ipmovie("audio: %d bits, %d Hz, %s, %s format\n",
00360 s->audio_bits,
00361 s->audio_sample_rate,
00362 (s->audio_channels == 2) ? "stereo" : "mono",
00363 (s->audio_type == CODEC_ID_INTERPLAY_DPCM) ?
00364 "Interplay audio" : "PCM");
00365 break;
00366
00367 case OPCODE_START_STOP_AUDIO:
00368 debug_ipmovie("start/stop audio\n");
00369 url_fseek(pb, opcode_size, SEEK_CUR);
00370 break;
00371
00372 case OPCODE_INIT_VIDEO_BUFFERS:
00373 debug_ipmovie("initialize video buffers\n");
00374 if ((opcode_version > 2) || (opcode_size > 8)) {
00375 debug_ipmovie("bad init_video_buffers opcode\n");
00376 chunk_type = CHUNK_BAD;
00377 break;
00378 }
00379 if (get_buffer(pb, scratch, opcode_size) !=
00380 opcode_size) {
00381 chunk_type = CHUNK_BAD;
00382 break;
00383 }
00384 s->video_width = AV_RL16(&scratch[0]) * 8;
00385 s->video_height = AV_RL16(&scratch[2]) * 8;
00386 debug_ipmovie("video resolution: %d x %d\n",
00387 s->video_width, s->video_height);
00388 break;
00389
00390 case OPCODE_UNKNOWN_06:
00391 case OPCODE_UNKNOWN_0E:
00392 case OPCODE_UNKNOWN_10:
00393 case OPCODE_UNKNOWN_12:
00394 case OPCODE_UNKNOWN_13:
00395 case OPCODE_UNKNOWN_14:
00396 case OPCODE_UNKNOWN_15:
00397 debug_ipmovie("unknown (but documented) opcode %02X\n", opcode_type);
00398 url_fseek(pb, opcode_size, SEEK_CUR);
00399 break;
00400
00401 case OPCODE_SEND_BUFFER:
00402 debug_ipmovie("send buffer\n");
00403 url_fseek(pb, opcode_size, SEEK_CUR);
00404 break;
00405
00406 case OPCODE_AUDIO_FRAME:
00407 debug_ipmovie("audio frame\n");
00408
00409
00410 s->audio_chunk_offset = url_ftell(pb);
00411 s->audio_chunk_size = opcode_size;
00412 url_fseek(pb, opcode_size, SEEK_CUR);
00413 break;
00414
00415 case OPCODE_SILENCE_FRAME:
00416 debug_ipmovie("silence frame\n");
00417 url_fseek(pb, opcode_size, SEEK_CUR);
00418 break;
00419
00420 case OPCODE_INIT_VIDEO_MODE:
00421 debug_ipmovie("initialize video mode\n");
00422 url_fseek(pb, opcode_size, SEEK_CUR);
00423 break;
00424
00425 case OPCODE_CREATE_GRADIENT:
00426 debug_ipmovie("create gradient\n");
00427 url_fseek(pb, opcode_size, SEEK_CUR);
00428 break;
00429
00430 case OPCODE_SET_PALETTE:
00431 debug_ipmovie("set palette\n");
00432
00433
00434 if (opcode_size > 0x304) {
00435 debug_ipmovie("demux_ipmovie: set_palette opcode too large\n");
00436 chunk_type = CHUNK_BAD;
00437 break;
00438 }
00439 if (get_buffer(pb, scratch, opcode_size) != opcode_size) {
00440 chunk_type = CHUNK_BAD;
00441 break;
00442 }
00443
00444
00445 first_color = AV_RL16(&scratch[0]);
00446 last_color = first_color + AV_RL16(&scratch[2]) - 1;
00447
00448 if ((first_color > 0xFF) || (last_color > 0xFF)) {
00449 debug_ipmovie("demux_ipmovie: set_palette indices out of range (%d -> %d)\n",
00450 first_color, last_color);
00451 chunk_type = CHUNK_BAD;
00452 break;
00453 }
00454 j = 4;
00455 for (i = first_color; i <= last_color; i++) {
00456
00457
00458 r = scratch[j++] * 4;
00459 g = scratch[j++] * 4;
00460 b = scratch[j++] * 4;
00461 s->palette_control.palette[i] = (r << 16) | (g << 8) | (b);
00462 }
00463
00464 s->palette_control.palette_changed = 1;
00465 break;
00466
00467 case OPCODE_SET_PALETTE_COMPRESSED:
00468 debug_ipmovie("set palette compressed\n");
00469 url_fseek(pb, opcode_size, SEEK_CUR);
00470 break;
00471
00472 case OPCODE_SET_DECODING_MAP:
00473 debug_ipmovie("set decoding map\n");
00474
00475
00476 s->decode_map_chunk_offset = url_ftell(pb);
00477 s->decode_map_chunk_size = opcode_size;
00478 url_fseek(pb, opcode_size, SEEK_CUR);
00479 break;
00480
00481 case OPCODE_VIDEO_DATA:
00482 debug_ipmovie("set video data\n");
00483
00484
00485 s->video_chunk_offset = url_ftell(pb);
00486 s->video_chunk_size = opcode_size;
00487 url_fseek(pb, opcode_size, SEEK_CUR);
00488 break;
00489
00490 default:
00491 debug_ipmovie("*** unknown opcode type\n");
00492 chunk_type = CHUNK_BAD;
00493 break;
00494
00495 }
00496 }
00497
00498
00499 s->next_chunk_offset = url_ftell(pb);
00500
00501
00502 if ((chunk_type == CHUNK_VIDEO) || (chunk_type == CHUNK_AUDIO_ONLY))
00503 chunk_type = load_ipmovie_packet(s, pb, pkt);
00504
00505 return chunk_type;
00506 }
00507
00508 static int ipmovie_probe(AVProbeData *p)
00509 {
00510 if (strncmp(p->buf, IPMOVIE_SIGNATURE, IPMOVIE_SIGNATURE_SIZE) != 0)
00511 return 0;
00512
00513 return AVPROBE_SCORE_MAX;
00514 }
00515
00516 static int ipmovie_read_header(AVFormatContext *s,
00517 AVFormatParameters *ap)
00518 {
00519 IPMVEContext *ipmovie = s->priv_data;
00520 ByteIOContext *pb = s->pb;
00521 AVPacket pkt;
00522 AVStream *st;
00523 unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
00524 int chunk_type;
00525
00526
00527 ipmovie->video_pts = ipmovie->audio_frame_count = 0;
00528 ipmovie->audio_chunk_offset = ipmovie->video_chunk_offset =
00529 ipmovie->decode_map_chunk_offset = 0;
00530
00531
00532 ipmovie->next_chunk_offset = IPMOVIE_SIGNATURE_SIZE + 6;
00533
00534
00535 if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_VIDEO)
00536 return AVERROR_INVALIDDATA;
00537
00538
00539
00540 if (get_buffer(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
00541 CHUNK_PREAMBLE_SIZE)
00542 return AVERROR(EIO);
00543 chunk_type = AV_RL16(&chunk_preamble[2]);
00544 url_fseek(pb, -CHUNK_PREAMBLE_SIZE, SEEK_CUR);
00545
00546 if (chunk_type == CHUNK_VIDEO)
00547 ipmovie->audio_type = 0;
00548 else if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_AUDIO)
00549 return AVERROR_INVALIDDATA;
00550
00551
00552 st = av_new_stream(s, 0);
00553 if (!st)
00554 return AVERROR(ENOMEM);
00555 av_set_pts_info(st, 33, 1, 90000);
00556 ipmovie->video_stream_index = st->index;
00557 st->codec->codec_type = CODEC_TYPE_VIDEO;
00558 st->codec->codec_id = CODEC_ID_INTERPLAY_VIDEO;
00559 st->codec->codec_tag = 0;
00560 st->codec->width = ipmovie->video_width;
00561 st->codec->height = ipmovie->video_height;
00562
00563
00564 st->codec->palctrl = &ipmovie->palette_control;
00565
00566 if (ipmovie->audio_type) {
00567 st = av_new_stream(s, 0);
00568 if (!st)
00569 return AVERROR(ENOMEM);
00570 av_set_pts_info(st, 33, 1, 90000);
00571 ipmovie->audio_stream_index = st->index;
00572 st->codec->codec_type = CODEC_TYPE_AUDIO;
00573 st->codec->codec_id = ipmovie->audio_type;
00574 st->codec->codec_tag = 0;
00575 st->codec->channels = ipmovie->audio_channels;
00576 st->codec->sample_rate = ipmovie->audio_sample_rate;
00577 st->codec->bits_per_sample = ipmovie->audio_bits;
00578 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
00579 st->codec->bits_per_sample;
00580 if (st->codec->codec_id == CODEC_ID_INTERPLAY_DPCM)
00581 st->codec->bit_rate /= 2;
00582 st->codec->block_align = st->codec->channels * st->codec->bits_per_sample;
00583 }
00584
00585 return 0;
00586 }
00587
00588 static int ipmovie_read_packet(AVFormatContext *s,
00589 AVPacket *pkt)
00590 {
00591 IPMVEContext *ipmovie = s->priv_data;
00592 ByteIOContext *pb = s->pb;
00593 int ret;
00594
00595 ret = process_ipmovie_chunk(ipmovie, pb, pkt);
00596 if (ret == CHUNK_BAD)
00597 ret = AVERROR_INVALIDDATA;
00598 else if (ret == CHUNK_EOF)
00599 ret = AVERROR(EIO);
00600 else if (ret == CHUNK_NOMEM)
00601 ret = AVERROR(ENOMEM);
00602 else if (ret == CHUNK_VIDEO)
00603 ret = 0;
00604 else
00605 ret = -1;
00606
00607 return ret;
00608 }
00609
00610 static int ipmovie_read_close(AVFormatContext *s)
00611 {
00612
00613
00614 return 0;
00615 }
00616
00617 AVInputFormat ipmovie_demuxer = {
00618 "ipmovie",
00619 "Interplay MVE format",
00620 sizeof(IPMVEContext),
00621 ipmovie_probe,
00622 ipmovie_read_header,
00623 ipmovie_read_packet,
00624 ipmovie_read_close,
00625 };