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