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 {
00088 Wc3DemuxContext *wc3 = s->priv_data;
00089 AVIOContext *pb = s->pb;
00090 unsigned int fourcc_tag;
00091 unsigned int size;
00092 AVStream *st;
00093 int ret = 0;
00094 char *buffer;
00095
00096
00097 wc3->width = WC3_DEFAULT_WIDTH;
00098 wc3->height = WC3_DEFAULT_HEIGHT;
00099 wc3->pts = 0;
00100 wc3->video_stream_index = wc3->audio_stream_index = 0;
00101 av_init_packet(&wc3->vpkt);
00102 wc3->vpkt.data = NULL; wc3->vpkt.size = 0;
00103
00104
00105 avio_skip(pb, 12);
00106
00107
00108
00109 fourcc_tag = avio_rl32(pb);
00110 size = (avio_rb32(pb) + 1) & (~1);
00111
00112 do {
00113 switch (fourcc_tag) {
00114
00115 case SOND_TAG:
00116 case INDX_TAG:
00117
00118 avio_skip(pb, size);
00119 break;
00120
00121 case PC__TAG:
00122
00123 avio_skip(pb, 12);
00124 break;
00125
00126 case BNAM_TAG:
00127
00128 buffer = av_malloc(size+1);
00129 if (!buffer)
00130 return AVERROR(ENOMEM);
00131 if ((ret = avio_read(pb, buffer, size)) != size)
00132 return AVERROR(EIO);
00133 buffer[size] = 0;
00134 av_dict_set(&s->metadata, "title", buffer,
00135 AV_DICT_DONT_STRDUP_VAL);
00136 break;
00137
00138 case SIZE_TAG:
00139
00140 wc3->width = avio_rl32(pb);
00141 wc3->height = avio_rl32(pb);
00142 break;
00143
00144 case PALT_TAG:
00145
00146 avio_seek(pb, -8, SEEK_CUR);
00147 av_append_packet(pb, &wc3->vpkt, 8 + PALETTE_SIZE);
00148 break;
00149
00150 default:
00151 av_log(s, AV_LOG_ERROR, " unrecognized WC3 chunk: %c%c%c%c (0x%02X%02X%02X%02X)\n",
00152 (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24),
00153 (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24));
00154 return AVERROR_INVALIDDATA;
00155 }
00156
00157 fourcc_tag = avio_rl32(pb);
00158
00159 size = (avio_rb32(pb) + 1) & (~1);
00160 if (url_feof(pb))
00161 return AVERROR(EIO);
00162
00163 } while (fourcc_tag != BRCH_TAG);
00164
00165
00166 st = avformat_new_stream(s, NULL);
00167 if (!st)
00168 return AVERROR(ENOMEM);
00169 avpriv_set_pts_info(st, 33, 1, WC3_FRAME_FPS);
00170 wc3->video_stream_index = st->index;
00171 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00172 st->codec->codec_id = AV_CODEC_ID_XAN_WC3;
00173 st->codec->codec_tag = 0;
00174 st->codec->width = wc3->width;
00175 st->codec->height = wc3->height;
00176
00177 st = avformat_new_stream(s, NULL);
00178 if (!st)
00179 return AVERROR(ENOMEM);
00180 avpriv_set_pts_info(st, 33, 1, WC3_FRAME_FPS);
00181 wc3->audio_stream_index = st->index;
00182 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00183 st->codec->codec_id = AV_CODEC_ID_PCM_S16LE;
00184 st->codec->codec_tag = 1;
00185 st->codec->channels = WC3_AUDIO_CHANNELS;
00186 st->codec->bits_per_coded_sample = WC3_AUDIO_BITS;
00187 st->codec->sample_rate = WC3_SAMPLE_RATE;
00188 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
00189 st->codec->bits_per_coded_sample;
00190 st->codec->block_align = WC3_AUDIO_BITS * WC3_AUDIO_CHANNELS;
00191
00192 return 0;
00193 }
00194
00195 static int wc3_read_packet(AVFormatContext *s,
00196 AVPacket *pkt)
00197 {
00198 Wc3DemuxContext *wc3 = s->priv_data;
00199 AVIOContext *pb = s->pb;
00200 unsigned int fourcc_tag;
00201 unsigned int size;
00202 int packet_read = 0;
00203 int ret = 0;
00204 unsigned char text[1024];
00205
00206 while (!packet_read) {
00207
00208 fourcc_tag = avio_rl32(pb);
00209
00210 size = (avio_rb32(pb) + 1) & (~1);
00211 if (url_feof(pb))
00212 return AVERROR(EIO);
00213
00214 switch (fourcc_tag) {
00215
00216 case BRCH_TAG:
00217
00218 break;
00219
00220 case SHOT_TAG:
00221
00222 avio_seek(pb, -8, SEEK_CUR);
00223 av_append_packet(pb, &wc3->vpkt, 8 + 4);
00224 break;
00225
00226 case VGA__TAG:
00227
00228 avio_seek(pb, -8, SEEK_CUR);
00229 ret= av_append_packet(pb, &wc3->vpkt, 8 + size);
00230
00231 if (wc3->vpkt.size > 0)
00232 ret = 0;
00233 *pkt = wc3->vpkt;
00234 wc3->vpkt.data = NULL; wc3->vpkt.size = 0;
00235 pkt->stream_index = wc3->video_stream_index;
00236 pkt->pts = wc3->pts;
00237 packet_read = 1;
00238 break;
00239
00240 case TEXT_TAG:
00241
00242 #if 0
00243 avio_skip(pb, size);
00244 #else
00245 if ((unsigned)size > sizeof(text) || (ret = avio_read(pb, text, size)) != size)
00246 ret = AVERROR(EIO);
00247 else {
00248 int i = 0;
00249 av_log (s, AV_LOG_DEBUG, "Subtitle time!\n");
00250 av_log (s, AV_LOG_DEBUG, " inglish: %s\n", &text[i + 1]);
00251 i += text[i] + 1;
00252 av_log (s, AV_LOG_DEBUG, " doytsch: %s\n", &text[i + 1]);
00253 i += text[i] + 1;
00254 av_log (s, AV_LOG_DEBUG, " fronsay: %s\n", &text[i + 1]);
00255 }
00256 #endif
00257 break;
00258
00259 case AUDI_TAG:
00260
00261 ret= av_get_packet(pb, pkt, size);
00262 pkt->stream_index = wc3->audio_stream_index;
00263 pkt->pts = wc3->pts;
00264
00265
00266 wc3->pts++;
00267
00268 packet_read = 1;
00269 break;
00270
00271 default:
00272 av_log (s, AV_LOG_ERROR, " unrecognized WC3 chunk: %c%c%c%c (0x%02X%02X%02X%02X)\n",
00273 (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24),
00274 (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24));
00275 ret = AVERROR_INVALIDDATA;
00276 packet_read = 1;
00277 break;
00278 }
00279 }
00280
00281 return ret;
00282 }
00283
00284 static int wc3_read_close(AVFormatContext *s)
00285 {
00286 Wc3DemuxContext *wc3 = s->priv_data;
00287
00288 if (wc3->vpkt.size > 0)
00289 av_free_packet(&wc3->vpkt);
00290
00291 return 0;
00292 }
00293
00294 AVInputFormat ff_wc3_demuxer = {
00295 .name = "wc3movie",
00296 .long_name = NULL_IF_CONFIG_SMALL("Wing Commander III movie"),
00297 .priv_data_size = sizeof(Wc3DemuxContext),
00298 .read_probe = wc3_probe,
00299 .read_header = wc3_read_header,
00300 .read_packet = wc3_read_packet,
00301 .read_close = wc3_read_close,
00302 };