00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/intreadwrite.h"
00023 #include "avformat.h"
00024 #include "internal.h"
00025 #include "avio.h"
00026
00027 typedef struct {
00028 int version;
00029 int audio_stream_index;
00030 int video_stream_index;
00031 } SMUSHContext;
00032
00033 static int smush_read_probe(AVProbeData *p)
00034 {
00035 if ((AV_RL32(p->buf) == MKTAG('S', 'A', 'N', 'M') ||
00036 AV_RL32(p->buf) == MKTAG('A', 'N', 'I', 'M'))) {
00037 return AVPROBE_SCORE_MAX;
00038 }
00039
00040 return 0;
00041 }
00042
00043 static int smush_read_header(AVFormatContext *ctx)
00044 {
00045 SMUSHContext *smush = ctx->priv_data;
00046 AVIOContext *pb = ctx->pb;
00047 AVStream *vst, *ast;
00048 uint32_t magic, nframes, size, subversion, i;
00049 uint32_t width = 0, height = 0, got_audio = 0, read = 0;
00050 uint32_t sample_rate, channels, palette[256];
00051
00052 magic = avio_rb32(pb);
00053 avio_skip(pb, 4);
00054
00055 if (magic == MKBETAG('A', 'N', 'I', 'M')) {
00056 if (avio_rb32(pb) != MKBETAG('A', 'H', 'D', 'R'))
00057 return AVERROR_INVALIDDATA;
00058
00059 size = avio_rb32(pb);
00060 if (size < 3 * 256 + 6)
00061 return AVERROR_INVALIDDATA;
00062
00063 smush->version = 0;
00064 subversion = avio_rl16(pb);
00065 nframes = avio_rl16(pb);
00066
00067 avio_skip(pb, 2);
00068
00069 for (i = 0; i < 256; i++)
00070 palette[i] = avio_rb24(pb);
00071
00072 avio_skip(pb, size - (3 * 256 + 6));
00073 } else if (magic == MKBETAG('S', 'A', 'N', 'M') ) {
00074 if (avio_rb32(pb) != MKBETAG('S', 'H', 'D', 'R'))
00075 return AVERROR_INVALIDDATA;
00076
00077 size = avio_rb32(pb);
00078 if (size < 14)
00079 return AVERROR_INVALIDDATA;
00080
00081 smush->version = 1;
00082 subversion = avio_rl16(pb);
00083 nframes = avio_rl32(pb);
00084 avio_skip(pb, 2);
00085 width = avio_rl16(pb);
00086 height = avio_rl16(pb);
00087 avio_skip(pb, 2);
00088 avio_skip(pb, size - 14);
00089
00090 if (avio_rb32(pb) != MKBETAG('F', 'L', 'H', 'D'))
00091 return AVERROR_INVALIDDATA;
00092
00093 size = avio_rb32(pb);
00094 while (!got_audio && ((read + 8) < size)) {
00095 uint32_t sig, chunk_size;
00096
00097 if (url_feof(pb))
00098 return AVERROR_EOF;
00099
00100 sig = avio_rb32(pb);
00101 chunk_size = avio_rb32(pb);
00102 read += 8;
00103 switch (sig) {
00104 case MKBETAG('W', 'a', 'v', 'e'):
00105 got_audio = 1;
00106 sample_rate = avio_rl32(pb);
00107 channels = avio_rl32(pb);
00108 avio_skip(pb, chunk_size - 8);
00109 read += chunk_size;
00110 break;
00111 case MKBETAG('B', 'l', '1', '6'):
00112 case MKBETAG('A', 'N', 'N', 'O'):
00113 avio_skip(pb, chunk_size);
00114 read += chunk_size;
00115 break;
00116 default:
00117 return AVERROR_INVALIDDATA;
00118 break;
00119 }
00120 }
00121
00122 avio_skip(pb, size - read);
00123 } else {
00124 av_log(ctx, AV_LOG_ERROR, "Wrong magic\n");
00125 return AVERROR_INVALIDDATA;
00126 }
00127
00128 vst = avformat_new_stream(ctx, 0);
00129 if (!vst)
00130 return AVERROR(ENOMEM);
00131
00132 smush->video_stream_index = vst->index;
00133
00134 vst->start_time = 0;
00135 vst->duration =
00136 vst->nb_frames = nframes;
00137 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00138 vst->codec->codec_id = AV_CODEC_ID_SANM;
00139 vst->codec->codec_tag = 0;
00140 vst->codec->width = width;
00141 vst->codec->height = height;
00142
00143 avpriv_set_pts_info(vst, 64, 66667, 1000000);
00144
00145 if (!smush->version) {
00146 vst->codec->extradata = av_malloc(1024 + 2 + FF_INPUT_BUFFER_PADDING_SIZE);
00147 if (!vst->codec->extradata)
00148 return AVERROR(ENOMEM);
00149
00150 vst->codec->extradata_size = 1024 + 2;
00151 AV_WL16(vst->codec->extradata, subversion);
00152 for (i = 0; i < 256; i++)
00153 AV_WL32(vst->codec->extradata + 2 + i * 4, palette[i]);
00154 }
00155
00156 if (got_audio) {
00157 ast = avformat_new_stream(ctx, 0);
00158 if (!ast)
00159 return AVERROR(ENOMEM);
00160
00161 smush->audio_stream_index = ast->index;
00162
00163 ast->start_time = 0;
00164 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00165 ast->codec->codec_id = AV_CODEC_ID_VIMA;
00166 ast->codec->codec_tag = 0;
00167 ast->codec->sample_rate = sample_rate;
00168 ast->codec->channels = channels;
00169
00170 avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
00171 }
00172
00173 return 0;
00174 }
00175
00176 static int smush_read_packet(AVFormatContext *ctx, AVPacket *pkt)
00177 {
00178 SMUSHContext *smush = ctx->priv_data;
00179 AVIOContext *pb = ctx->pb;
00180 int done = 0;
00181
00182 while (!done) {
00183 uint32_t sig, size;
00184
00185 if (url_feof(pb))
00186 return AVERROR_EOF;
00187
00188 sig = avio_rb32(pb);
00189 size = avio_rb32(pb);
00190
00191 switch (sig) {
00192 case MKBETAG('F', 'R', 'M', 'E'):
00193 if (smush->version)
00194 break;
00195 if (av_get_packet(pb, pkt, size) < 0)
00196 return AVERROR(EIO);
00197
00198 pkt->stream_index = smush->video_stream_index;
00199 done = 1;
00200 break;
00201 case MKBETAG('B', 'l', '1', '6'):
00202 if (av_get_packet(pb, pkt, size) < 0)
00203 return AVERROR(EIO);
00204
00205 pkt->stream_index = smush->video_stream_index;
00206 pkt->duration = 1;
00207 done = 1;
00208 break;
00209 case MKBETAG('W', 'a', 'v', 'e'):
00210 if (size < 13)
00211 return AVERROR_INVALIDDATA;
00212 if (av_get_packet(pb, pkt, size) < 0)
00213 return AVERROR(EIO);
00214
00215 pkt->stream_index = smush->audio_stream_index;
00216 pkt->flags |= AV_PKT_FLAG_KEY;
00217 pkt->duration = AV_RB32(pkt->data);
00218 if (pkt->duration == 0xFFFFFFFFu)
00219 pkt->duration = AV_RB32(pkt->data + 8);
00220 done = 1;
00221 break;
00222 default:
00223 avio_skip(pb, size);
00224 break;
00225 }
00226 }
00227
00228 return 0;
00229 }
00230
00231 AVInputFormat ff_smush_demuxer = {
00232 .name = "smush",
00233 .long_name = NULL_IF_CONFIG_SMALL("LucasArts Smush"),
00234 .priv_data_size = sizeof(SMUSHContext),
00235 .read_probe = smush_read_probe,
00236 .read_header = smush_read_header,
00237 .read_packet = smush_read_packet,
00238 };