00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00033 #include "libavutil/intreadwrite.h"
00034 #include "avformat.h"
00035 #include "internal.h"
00036 #include "sauce.h"
00037 #include "libavcodec/bintext.h"
00038
00039 #define LINE_RATE 6000
00041 typedef struct {
00042 int chars_per_frame;
00043 uint64_t fsize;
00044 } BinDemuxContext;
00045
00046 #if CONFIG_BINTEXT_DEMUXER | CONFIG_ADF_DEMUXER | CONFIG_IDF_DEMUXER
00047
00050 static void calculate_height(AVCodecContext *avctx, uint64_t fsize)
00051 {
00052 avctx->height = (fsize / ((avctx->width>>3)*2)) << 4;
00053 }
00054 #endif
00055
00056 #if CONFIG_BINTEXT_DEMUXER
00057 static const uint8_t next_magic[]={
00058 0x1A, 0x1B, '[', '0', ';', '3', '0', ';', '4', '0', 'm', 'N', 'E', 'X', 'T', 0x00
00059 };
00060
00061 static int next_tag_read(AVFormatContext *avctx, uint64_t *fsize)
00062 {
00063 AVIOContext *pb = avctx->pb;
00064 char buf[36];
00065 int len;
00066 uint64_t start_pos = avio_size(pb) - 256;
00067
00068 avio_seek(pb, start_pos, SEEK_SET);
00069 if (avio_read(pb, buf, sizeof(next_magic)) != sizeof(next_magic))
00070 return -1;
00071 if (memcmp(buf, next_magic, sizeof(next_magic)))
00072 return -1;
00073 if (avio_r8(pb) != 0x01)
00074 return -1;
00075
00076 *fsize -= 256;
00077
00078 #define GET_EFI2_META(name,size) \
00079 len = avio_r8(pb); \
00080 if (len < 1 || len > size) \
00081 return -1; \
00082 if (avio_read(pb, buf, size) == size && *buf) { \
00083 buf[len] = 0; \
00084 av_dict_set(&avctx->metadata, name, buf, 0); \
00085 }
00086
00087 GET_EFI2_META("filename", 12)
00088 GET_EFI2_META("author", 20)
00089 GET_EFI2_META("publisher", 20)
00090 GET_EFI2_META("title", 35)
00091
00092 return 0;
00093 }
00094
00095 static void predict_width(AVCodecContext *avctx, uint64_t fsize, int got_width)
00096 {
00098 if (!got_width)
00099 avctx->width = fsize > 4000 ? (160<<3) : (80<<3);
00100 }
00101
00102 static AVStream * init_stream(AVFormatContext *s,
00103 AVFormatParameters *ap)
00104 {
00105 BinDemuxContext *bin = s->priv_data;
00106 AVStream *st = avformat_new_stream(s, NULL);
00107 if (!st)
00108 return NULL;
00109 st->codec->codec_tag = 0;
00110 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00111
00112 if (!ap->time_base.num) {
00113 avpriv_set_pts_info(st, 60, 1, 25);
00114 } else {
00115 avpriv_set_pts_info(st, 60, ap->time_base.num, ap->time_base.den);
00116 }
00117
00118
00119 bin->chars_per_frame = FFMAX(av_q2d(st->time_base) * (ap->sample_rate ? ap->sample_rate : LINE_RATE), 1);
00120
00121 st->codec->width = ap->width ? ap->width : (80<<3);
00122 st->codec->height = ap->height ? ap->height : (25<<4);
00123 return st;
00124 }
00125
00126 static int bintext_read_header(AVFormatContext *s,
00127 AVFormatParameters *ap)
00128 {
00129 BinDemuxContext *bin = s->priv_data;
00130 AVIOContext *pb = s->pb;
00131
00132 AVStream *st = init_stream(s, ap);
00133 if (!st)
00134 return AVERROR(ENOMEM);
00135 st->codec->codec_id = CODEC_ID_BINTEXT;
00136
00137 st->codec->extradata_size = 2;
00138 st->codec->extradata = av_malloc(st->codec->extradata_size);
00139 if (!st->codec->extradata)
00140 return AVERROR(ENOMEM);
00141 st->codec->extradata[0] = 16;
00142 st->codec->extradata[1] = 0;
00143
00144 if (pb->seekable) {
00145 int got_width = 0;
00146 bin->fsize = avio_size(pb);
00147 if (ff_sauce_read(s, &bin->fsize, &got_width, 0) < 0)
00148 next_tag_read(s, &bin->fsize);
00149 if (!ap->width)
00150 predict_width(st->codec, bin->fsize, got_width);
00151 if (!ap->height)
00152 calculate_height(st->codec, bin->fsize);
00153 avio_seek(pb, 0, SEEK_SET);
00154 }
00155 return 0;
00156 };
00157 #endif
00158
00159 #if CONFIG_XBIN_DEMUXER
00160 static int xbin_probe(AVProbeData *p)
00161 {
00162 const uint8_t *d = p->buf;
00163
00164 if (AV_RL32(d) == MKTAG('X','B','I','N') && d[4] == 0x1A &&
00165 AV_RL16(d+5) > 0 && AV_RL16(d+5) <= 160 &&
00166 d[9] > 0 && d[9] <= 32)
00167 return AVPROBE_SCORE_MAX;
00168 return 0;
00169 }
00170
00171 static int xbin_read_header(AVFormatContext *s,
00172 AVFormatParameters *ap)
00173 {
00174 BinDemuxContext *bin = s->priv_data;
00175 AVIOContext *pb = s->pb;
00176 char fontheight, flags;
00177
00178 AVStream *st = init_stream(s, ap);
00179 if (!st)
00180 return AVERROR(ENOMEM);
00181
00182 avio_skip(pb, 5);
00183 st->codec->width = avio_rl16(pb)<<3;
00184 st->codec->height = avio_rl16(pb);
00185 fontheight = avio_r8(pb);
00186 st->codec->height *= fontheight;
00187 flags = avio_r8(pb);
00188
00189 st->codec->extradata_size = 2;
00190 if ((flags & BINTEXT_PALETTE))
00191 st->codec->extradata_size += 48;
00192 if ((flags & BINTEXT_FONT))
00193 st->codec->extradata_size += fontheight * (flags & 0x10 ? 512 : 256);
00194 st->codec->codec_id = flags & 4 ? CODEC_ID_XBIN : CODEC_ID_BINTEXT;
00195
00196 st->codec->extradata = av_malloc(st->codec->extradata_size);
00197 if (!st->codec->extradata)
00198 return AVERROR(ENOMEM);
00199 st->codec->extradata[0] = fontheight;
00200 st->codec->extradata[1] = flags;
00201 if (avio_read(pb, st->codec->extradata + 2, st->codec->extradata_size - 2) < 0)
00202 return AVERROR(EIO);
00203
00204 if (pb->seekable) {
00205 bin->fsize = avio_size(pb) - 9 - st->codec->extradata_size;
00206 ff_sauce_read(s, &bin->fsize, NULL, 0);
00207 avio_seek(pb, 9 + st->codec->extradata_size, SEEK_SET);
00208 }
00209
00210 return 0;
00211 }
00212 #endif
00213
00214 #if CONFIG_ADF_DEMUXER
00215 static int adf_read_header(AVFormatContext *s,
00216 AVFormatParameters *ap)
00217 {
00218 BinDemuxContext *bin = s->priv_data;
00219 AVIOContext *pb = s->pb;
00220 AVStream *st;
00221
00222 if (avio_r8(pb) != 1)
00223 return AVERROR_INVALIDDATA;
00224
00225 st = init_stream(s, ap);
00226 if (!st)
00227 return AVERROR(ENOMEM);
00228 st->codec->codec_id = CODEC_ID_BINTEXT;
00229
00230 st->codec->extradata_size = 2 + 48 + 4096;
00231 st->codec->extradata = av_malloc(st->codec->extradata_size);
00232 if (!st->codec->extradata)
00233 return AVERROR(ENOMEM);
00234 st->codec->extradata[0] = 16;
00235 st->codec->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT;
00236
00237 if (avio_read(pb, st->codec->extradata + 2, 24) < 0)
00238 return AVERROR(EIO);
00239 avio_skip(pb, 144);
00240 if (avio_read(pb, st->codec->extradata + 2 + 24, 24) < 0)
00241 return AVERROR(EIO);
00242 if (avio_read(pb, st->codec->extradata + 2 + 48, 4096) < 0)
00243 return AVERROR(EIO);
00244
00245 if (pb->seekable) {
00246 int got_width = 0;
00247 bin->fsize = avio_size(pb) - 1 - 192 - 4096;
00248 st->codec->width = 80<<3;
00249 ff_sauce_read(s, &bin->fsize, &got_width, 0);
00250 if (!ap->height)
00251 calculate_height(st->codec, bin->fsize);
00252 avio_seek(pb, 1 + 192 + 4096, SEEK_SET);
00253 }
00254 return 0;
00255 }
00256 #endif
00257
00258 #if CONFIG_IDF_DEMUXER
00259 static const uint8_t idf_magic[] = {
00260 0x04, 0x31, 0x2e, 0x34, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x00, 0x15, 0x00
00261 };
00262
00263 static int idf_probe(AVProbeData *p)
00264 {
00265 if (p->buf_size < sizeof(idf_magic))
00266 return 0;
00267 if (!memcmp(p->buf, idf_magic, sizeof(idf_magic)))
00268 return AVPROBE_SCORE_MAX;
00269 return 0;
00270 }
00271
00272 static int idf_read_header(AVFormatContext *s,
00273 AVFormatParameters *ap)
00274 {
00275 BinDemuxContext *bin = s->priv_data;
00276 AVIOContext *pb = s->pb;
00277 AVStream *st;
00278 int got_width = 0;
00279
00280 if (!pb->seekable)
00281 return AVERROR(EIO);
00282
00283 st = init_stream(s, ap);
00284 if (!st)
00285 return AVERROR(ENOMEM);
00286 st->codec->codec_id = CODEC_ID_IDF;
00287
00288 st->codec->extradata_size = 2 + 48 + 4096;
00289 st->codec->extradata = av_malloc(st->codec->extradata_size);
00290 if (!st->codec->extradata)
00291 return AVERROR(ENOMEM);
00292 st->codec->extradata[0] = 16;
00293 st->codec->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT;
00294
00295 avio_seek(pb, avio_size(pb) - 4096 - 48, SEEK_SET);
00296
00297 if (avio_read(pb, st->codec->extradata + 2 + 48, 4096) < 0)
00298 return AVERROR(EIO);
00299 if (avio_read(pb, st->codec->extradata + 2, 48) < 0)
00300 return AVERROR(EIO);
00301
00302 bin->fsize = avio_size(pb) - 12 - 4096 - 48;
00303 ff_sauce_read(s, &bin->fsize, &got_width, 0);
00304 if (!ap->height)
00305 calculate_height(st->codec, bin->fsize);
00306 avio_seek(pb, 12, SEEK_SET);
00307 return 0;
00308 }
00309 #endif
00310
00311 static int read_packet(AVFormatContext *s,
00312 AVPacket *pkt)
00313 {
00314 BinDemuxContext *bin = s->priv_data;
00315
00316 if (bin->fsize > 0) {
00317 if (av_get_packet(s->pb, pkt, bin->fsize) < 0)
00318 return AVERROR(EIO);
00319 bin->fsize = -1;
00320 } else if (!bin->fsize) {
00321 if (url_feof(s->pb))
00322 return AVERROR(EIO);
00323 if (av_get_packet(s->pb, pkt, bin->chars_per_frame) < 0)
00324 return AVERROR(EIO);
00325 } else {
00326 return AVERROR(EIO);
00327 }
00328
00329 pkt->flags |= AV_PKT_FLAG_KEY;
00330 return 0;
00331 }
00332
00333 #if CONFIG_BINTEXT_DEMUXER
00334 AVInputFormat ff_bintext_demuxer = {
00335 .name = "bin",
00336 .long_name = NULL_IF_CONFIG_SMALL("Binary text"),
00337 .priv_data_size = sizeof(BinDemuxContext),
00338 .read_header = bintext_read_header,
00339 .read_packet = read_packet,
00340 .extensions = "bin",
00341 };
00342 #endif
00343
00344 #if CONFIG_XBIN_DEMUXER
00345 AVInputFormat ff_xbin_demuxer = {
00346 .name = "xbin",
00347 .long_name = NULL_IF_CONFIG_SMALL("eXtended BINary text (XBIN)"),
00348 .priv_data_size = sizeof(BinDemuxContext),
00349 .read_probe = xbin_probe,
00350 .read_header = xbin_read_header,
00351 .read_packet = read_packet,
00352 };
00353 #endif
00354
00355 #if CONFIG_ADF_DEMUXER
00356 AVInputFormat ff_adf_demuxer = {
00357 .name = "adf",
00358 .long_name = NULL_IF_CONFIG_SMALL("Artworx Data Format"),
00359 .priv_data_size = sizeof(BinDemuxContext),
00360 .read_header = adf_read_header,
00361 .read_packet = read_packet,
00362 .extensions = "adf",
00363 };
00364 #endif
00365
00366 #if CONFIG_IDF_DEMUXER
00367 AVInputFormat ff_idf_demuxer = {
00368 .name = "idf",
00369 .long_name = NULL_IF_CONFIG_SMALL("iCE Draw File"),
00370 .priv_data_size = sizeof(BinDemuxContext),
00371 .read_probe = idf_probe,
00372 .read_header = idf_read_header,
00373 .read_packet = read_packet,
00374 .extensions = "idf",
00375 };
00376 #endif