00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00030 #include "libavutil/channel_layout.h"
00031 #include "libavutil/intreadwrite.h"
00032 #include "libavutil/dict.h"
00033 #include "avformat.h"
00034 #include "internal.h"
00035
00036 #define FORM_TAG MKTAG('F', 'O', 'R', 'M')
00037 #define MOVE_TAG MKTAG('M', 'O', 'V', 'E')
00038 #define PC__TAG MKTAG('_', 'P', 'C', '_')
00039 #define SOND_TAG MKTAG('S', 'O', 'N', 'D')
00040 #define BNAM_TAG MKTAG('B', 'N', 'A', 'M')
00041 #define SIZE_TAG MKTAG('S', 'I', 'Z', 'E')
00042 #define PALT_TAG MKTAG('P', 'A', 'L', 'T')
00043 #define INDX_TAG MKTAG('I', 'N', 'D', 'X')
00044 #define BRCH_TAG MKTAG('B', 'R', 'C', 'H')
00045 #define SHOT_TAG MKTAG('S', 'H', 'O', 'T')
00046 #define VGA__TAG MKTAG('V', 'G', 'A', ' ')
00047 #define TEXT_TAG MKTAG('T', 'E', 'X', 'T')
00048 #define AUDI_TAG MKTAG('A', 'U', 'D', 'I')
00049
00050
00051 #define WC3_DEFAULT_WIDTH 320
00052 #define WC3_DEFAULT_HEIGHT 165
00053
00054
00055 #define WC3_SAMPLE_RATE 22050
00056 #define WC3_AUDIO_CHANNELS 1
00057 #define WC3_AUDIO_BITS 16
00058
00059
00060 #define WC3_FRAME_FPS 15
00061
00062 #define PALETTE_SIZE (256 * 3)
00063
00064 typedef struct Wc3DemuxContext {
00065 int width;
00066 int height;
00067 int64_t pts;
00068 int video_stream_index;
00069 int audio_stream_index;
00070
00071 AVPacket vpkt;
00072
00073 } Wc3DemuxContext;
00074
00075 static int wc3_probe(AVProbeData *p)
00076 {
00077 if (p->buf_size < 12)
00078 return 0;
00079
00080 if ((AV_RL32(&p->buf[0]) != FORM_TAG) ||
00081 (AV_RL32(&p->buf[8]) != MOVE_TAG))
00082 return 0;
00083
00084 return AVPROBE_SCORE_MAX;
00085 }
00086
00087 static int wc3_read_header(AVFormatContext *s)
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 = AV_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 = AV_CODEC_ID_PCM_S16LE;
00185 st->codec->codec_tag = 1;
00186 st->codec->channels = WC3_AUDIO_CHANNELS;
00187 st->codec->channel_layout = AV_CH_LAYOUT_MONO;
00188 st->codec->bits_per_coded_sample = WC3_AUDIO_BITS;
00189 st->codec->sample_rate = WC3_SAMPLE_RATE;
00190 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
00191 st->codec->bits_per_coded_sample;
00192 st->codec->block_align = WC3_AUDIO_BITS * WC3_AUDIO_CHANNELS;
00193
00194 return 0;
00195 }
00196
00197 static int wc3_read_packet(AVFormatContext *s,
00198 AVPacket *pkt)
00199 {
00200 Wc3DemuxContext *wc3 = s->priv_data;
00201 AVIOContext *pb = s->pb;
00202 unsigned int fourcc_tag;
00203 unsigned int size;
00204 int packet_read = 0;
00205 int ret = 0;
00206 unsigned char text[1024];
00207
00208 while (!packet_read) {
00209
00210 fourcc_tag = avio_rl32(pb);
00211
00212 size = (avio_rb32(pb) + 1) & (~1);
00213 if (url_feof(pb))
00214 return AVERROR(EIO);
00215
00216 switch (fourcc_tag) {
00217
00218 case BRCH_TAG:
00219
00220 break;
00221
00222 case SHOT_TAG:
00223
00224 avio_seek(pb, -8, SEEK_CUR);
00225 av_append_packet(pb, &wc3->vpkt, 8 + 4);
00226 break;
00227
00228 case VGA__TAG:
00229
00230 avio_seek(pb, -8, SEEK_CUR);
00231 ret= av_append_packet(pb, &wc3->vpkt, 8 + size);
00232
00233 if (wc3->vpkt.size > 0)
00234 ret = 0;
00235 *pkt = wc3->vpkt;
00236 wc3->vpkt.data = NULL; wc3->vpkt.size = 0;
00237 pkt->stream_index = wc3->video_stream_index;
00238 pkt->pts = wc3->pts;
00239 packet_read = 1;
00240 break;
00241
00242 case TEXT_TAG:
00243
00244 #if 0
00245 avio_skip(pb, size);
00246 #else
00247 if ((unsigned)size > sizeof(text) || (ret = avio_read(pb, text, size)) != size)
00248 ret = AVERROR(EIO);
00249 else {
00250 int i = 0;
00251 av_log (s, AV_LOG_DEBUG, "Subtitle time!\n");
00252 av_log (s, AV_LOG_DEBUG, " inglish: %s\n", &text[i + 1]);
00253 i += text[i] + 1;
00254 av_log (s, AV_LOG_DEBUG, " doytsch: %s\n", &text[i + 1]);
00255 i += text[i] + 1;
00256 av_log (s, AV_LOG_DEBUG, " fronsay: %s\n", &text[i + 1]);
00257 }
00258 #endif
00259 break;
00260
00261 case AUDI_TAG:
00262
00263 ret= av_get_packet(pb, pkt, size);
00264 pkt->stream_index = wc3->audio_stream_index;
00265 pkt->pts = wc3->pts;
00266
00267
00268 wc3->pts++;
00269
00270 packet_read = 1;
00271 break;
00272
00273 default:
00274 av_log (s, AV_LOG_ERROR, " unrecognized WC3 chunk: %c%c%c%c (0x%02X%02X%02X%02X)\n",
00275 (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24),
00276 (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24));
00277 ret = AVERROR_INVALIDDATA;
00278 packet_read = 1;
00279 break;
00280 }
00281 }
00282
00283 return ret;
00284 }
00285
00286 static int wc3_read_close(AVFormatContext *s)
00287 {
00288 Wc3DemuxContext *wc3 = s->priv_data;
00289
00290 if (wc3->vpkt.size > 0)
00291 av_free_packet(&wc3->vpkt);
00292
00293 return 0;
00294 }
00295
00296 AVInputFormat ff_wc3_demuxer = {
00297 .name = "wc3movie",
00298 .long_name = NULL_IF_CONFIG_SMALL("Wing Commander III movie"),
00299 .priv_data_size = sizeof(Wc3DemuxContext),
00300 .read_probe = wc3_probe,
00301 .read_header = wc3_read_header,
00302 .read_packet = wc3_read_packet,
00303 .read_close = wc3_read_close,
00304 };