00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00030 #include "libavutil/intreadwrite.h"
00031 #include "libavutil/dict.h"
00032 #include "avformat.h"
00033 #include "internal.h"
00034
00035 #define FORM_TAG MKTAG('F', 'O', 'R', 'M')
00036 #define MOVE_TAG MKTAG('M', 'O', 'V', 'E')
00037 #define PC__TAG MKTAG('_', 'P', 'C', '_')
00038 #define SOND_TAG MKTAG('S', 'O', 'N', 'D')
00039 #define BNAM_TAG MKTAG('B', 'N', 'A', 'M')
00040 #define SIZE_TAG MKTAG('S', 'I', 'Z', 'E')
00041 #define PALT_TAG MKTAG('P', 'A', 'L', 'T')
00042 #define INDX_TAG MKTAG('I', 'N', 'D', 'X')
00043 #define BRCH_TAG MKTAG('B', 'R', 'C', 'H')
00044 #define SHOT_TAG MKTAG('S', 'H', 'O', 'T')
00045 #define VGA__TAG MKTAG('V', 'G', 'A', ' ')
00046 #define TEXT_TAG MKTAG('T', 'E', 'X', 'T')
00047 #define AUDI_TAG MKTAG('A', 'U', 'D', 'I')
00048
00049
00050 #define WC3_DEFAULT_WIDTH 320
00051 #define WC3_DEFAULT_HEIGHT 165
00052
00053
00054 #define WC3_SAMPLE_RATE 22050
00055 #define WC3_AUDIO_CHANNELS 1
00056 #define WC3_AUDIO_BITS 16
00057
00058
00059 #define WC3_FRAME_FPS 15
00060
00061 #define PALETTE_SIZE (256 * 3)
00062
00063 typedef struct Wc3DemuxContext {
00064 int width;
00065 int height;
00066 int64_t pts;
00067 int video_stream_index;
00068 int audio_stream_index;
00069
00070 AVPacket vpkt;
00071
00072 } Wc3DemuxContext;
00073
00074 static int wc3_probe(AVProbeData *p)
00075 {
00076 if (p->buf_size < 12)
00077 return 0;
00078
00079 if ((AV_RL32(&p->buf[0]) != FORM_TAG) ||
00080 (AV_RL32(&p->buf[8]) != MOVE_TAG))
00081 return 0;
00082
00083 return AVPROBE_SCORE_MAX;
00084 }
00085
00086 static int wc3_read_header(AVFormatContext *s,
00087 AVFormatParameters *ap)
00088 {
00089 Wc3DemuxContext *wc3 = s->priv_data;
00090 AVIOContext *pb = s->pb;
00091 unsigned int fourcc_tag;
00092 unsigned int size;
00093 AVStream *st;
00094 int ret = 0;
00095 char *buffer;
00096
00097
00098 wc3->width = WC3_DEFAULT_WIDTH;
00099 wc3->height = WC3_DEFAULT_HEIGHT;
00100 wc3->pts = 0;
00101 wc3->video_stream_index = wc3->audio_stream_index = 0;
00102 av_init_packet(&wc3->vpkt);
00103 wc3->vpkt.data = NULL; wc3->vpkt.size = 0;
00104
00105
00106 avio_skip(pb, 12);
00107
00108
00109
00110 fourcc_tag = avio_rl32(pb);
00111 size = (avio_rb32(pb) + 1) & (~1);
00112
00113 do {
00114 switch (fourcc_tag) {
00115
00116 case SOND_TAG:
00117 case INDX_TAG:
00118
00119 avio_skip(pb, size);
00120 break;
00121
00122 case PC__TAG:
00123
00124 avio_skip(pb, 12);
00125 break;
00126
00127 case BNAM_TAG:
00128
00129 buffer = av_malloc(size+1);
00130 if (!buffer)
00131 return AVERROR(ENOMEM);
00132 if ((ret = avio_read(pb, buffer, size)) != size)
00133 return AVERROR(EIO);
00134 buffer[size] = 0;
00135 av_dict_set(&s->metadata, "title", buffer,
00136 AV_DICT_DONT_STRDUP_VAL);
00137 break;
00138
00139 case SIZE_TAG:
00140
00141 wc3->width = avio_rl32(pb);
00142 wc3->height = avio_rl32(pb);
00143 break;
00144
00145 case PALT_TAG:
00146
00147 avio_seek(pb, -8, SEEK_CUR);
00148 av_append_packet(pb, &wc3->vpkt, 8 + PALETTE_SIZE);
00149 break;
00150
00151 default:
00152 av_log(s, AV_LOG_ERROR, " unrecognized WC3 chunk: %c%c%c%c (0x%02X%02X%02X%02X)\n",
00153 (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24),
00154 (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24));
00155 return AVERROR_INVALIDDATA;
00156 }
00157
00158 fourcc_tag = avio_rl32(pb);
00159
00160 size = (avio_rb32(pb) + 1) & (~1);
00161 if (url_feof(pb))
00162 return AVERROR(EIO);
00163
00164 } while (fourcc_tag != BRCH_TAG);
00165
00166
00167 st = avformat_new_stream(s, NULL);
00168 if (!st)
00169 return AVERROR(ENOMEM);
00170 avpriv_set_pts_info(st, 33, 1, WC3_FRAME_FPS);
00171 wc3->video_stream_index = st->index;
00172 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00173 st->codec->codec_id = CODEC_ID_XAN_WC3;
00174 st->codec->codec_tag = 0;
00175 st->codec->width = wc3->width;
00176 st->codec->height = wc3->height;
00177
00178 st = avformat_new_stream(s, NULL);
00179 if (!st)
00180 return AVERROR(ENOMEM);
00181 avpriv_set_pts_info(st, 33, 1, WC3_FRAME_FPS);
00182 wc3->audio_stream_index = st->index;
00183 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00184 st->codec->codec_id = CODEC_ID_PCM_S16LE;
00185 st->codec->codec_tag = 1;
00186 st->codec->channels = WC3_AUDIO_CHANNELS;
00187 st->codec->bits_per_coded_sample = WC3_AUDIO_BITS;
00188 st->codec->sample_rate = WC3_SAMPLE_RATE;
00189 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
00190 st->codec->bits_per_coded_sample;
00191 st->codec->block_align = WC3_AUDIO_BITS * WC3_AUDIO_CHANNELS;
00192
00193 return 0;
00194 }
00195
00196 static int wc3_read_packet(AVFormatContext *s,
00197 AVPacket *pkt)
00198 {
00199 Wc3DemuxContext *wc3 = s->priv_data;
00200 AVIOContext *pb = s->pb;
00201 unsigned int fourcc_tag;
00202 unsigned int size;
00203 int packet_read = 0;
00204 int ret = 0;
00205 unsigned char text[1024];
00206
00207 while (!packet_read) {
00208
00209 fourcc_tag = avio_rl32(pb);
00210
00211 size = (avio_rb32(pb) + 1) & (~1);
00212 if (url_feof(pb))
00213 return AVERROR(EIO);
00214
00215 switch (fourcc_tag) {
00216
00217 case BRCH_TAG:
00218
00219 break;
00220
00221 case SHOT_TAG:
00222
00223 avio_seek(pb, -8, SEEK_CUR);
00224 av_append_packet(pb, &wc3->vpkt, 8 + 4);
00225 break;
00226
00227 case VGA__TAG:
00228
00229 avio_seek(pb, -8, SEEK_CUR);
00230 ret= av_append_packet(pb, &wc3->vpkt, 8 + size);
00231
00232 if (wc3->vpkt.size > 0)
00233 ret = 0;
00234 *pkt = wc3->vpkt;
00235 wc3->vpkt.data = NULL; wc3->vpkt.size = 0;
00236 pkt->stream_index = wc3->video_stream_index;
00237 pkt->pts = wc3->pts;
00238 packet_read = 1;
00239 break;
00240
00241 case TEXT_TAG:
00242
00243 #if 0
00244 avio_skip(pb, size);
00245 #else
00246 if ((unsigned)size > sizeof(text) || (ret = avio_read(pb, text, size)) != size)
00247 ret = AVERROR(EIO);
00248 else {
00249 int i = 0;
00250 av_log (s, AV_LOG_DEBUG, "Subtitle time!\n");
00251 av_log (s, AV_LOG_DEBUG, " inglish: %s\n", &text[i + 1]);
00252 i += text[i] + 1;
00253 av_log (s, AV_LOG_DEBUG, " doytsch: %s\n", &text[i + 1]);
00254 i += text[i] + 1;
00255 av_log (s, AV_LOG_DEBUG, " fronsay: %s\n", &text[i + 1]);
00256 }
00257 #endif
00258 break;
00259
00260 case AUDI_TAG:
00261
00262 ret= av_get_packet(pb, pkt, size);
00263 pkt->stream_index = wc3->audio_stream_index;
00264 pkt->pts = wc3->pts;
00265
00266
00267 wc3->pts++;
00268
00269 packet_read = 1;
00270 break;
00271
00272 default:
00273 av_log (s, AV_LOG_ERROR, " unrecognized WC3 chunk: %c%c%c%c (0x%02X%02X%02X%02X)\n",
00274 (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24),
00275 (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24));
00276 ret = AVERROR_INVALIDDATA;
00277 packet_read = 1;
00278 break;
00279 }
00280 }
00281
00282 return ret;
00283 }
00284
00285 static int wc3_read_close(AVFormatContext *s)
00286 {
00287 Wc3DemuxContext *wc3 = s->priv_data;
00288
00289 if (wc3->vpkt.size > 0)
00290 av_free_packet(&wc3->vpkt);
00291
00292 return 0;
00293 }
00294
00295 AVInputFormat ff_wc3_demuxer = {
00296 .name = "wc3movie",
00297 .long_name = NULL_IF_CONFIG_SMALL("Wing Commander III movie format"),
00298 .priv_data_size = sizeof(Wc3DemuxContext),
00299 .read_probe = wc3_probe,
00300 .read_header = wc3_read_header,
00301 .read_packet = wc3_read_packet,
00302 .read_close = wc3_read_close,
00303 };