You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

436 lines
13KB

  1. /*
  2. * Binary text demuxer
  3. * eXtended BINary text (XBIN) demuxer
  4. * Artworx Data Format demuxer
  5. * iCEDraw File demuxer
  6. * Copyright (c) 2010 Peter Ross <pross@xvid.org>
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. /**
  25. * @file
  26. * Binary text demuxer
  27. * eXtended BINary text (XBIN) demuxer
  28. * Artworx Data Format demuxer
  29. * iCEDraw File demuxer
  30. */
  31. #include "libavutil/intreadwrite.h"
  32. #include "libavutil/opt.h"
  33. #include "libavutil/parseutils.h"
  34. #include "avformat.h"
  35. #include "internal.h"
  36. #include "sauce.h"
  37. #include "libavcodec/bintext.h"
  38. typedef struct {
  39. const AVClass *class;
  40. int chars_per_frame; /**< characters to send decoder per frame;
  41. set by private options as characters per second, and then
  42. converted to characters per frame at runtime */
  43. int width, height; /**< video size (WxH pixels) (private option) */
  44. AVRational framerate; /**< frames per second (private option) */
  45. uint64_t fsize; /**< file size less metadata buffer */
  46. } BinDemuxContext;
  47. static AVStream * init_stream(AVFormatContext *s)
  48. {
  49. BinDemuxContext *bin = s->priv_data;
  50. AVStream *st = avformat_new_stream(s, NULL);
  51. if (!st)
  52. return NULL;
  53. st->codecpar->codec_tag = 0;
  54. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  55. if (!bin->width) {
  56. st->codecpar->width = (80<<3);
  57. st->codecpar->height = (25<<4);
  58. }
  59. avpriv_set_pts_info(st, 60, bin->framerate.den, bin->framerate.num);
  60. /* simulate tty display speed */
  61. bin->chars_per_frame = av_clip(av_q2d(st->time_base) * bin->chars_per_frame, 1, INT_MAX);
  62. return st;
  63. }
  64. #if CONFIG_BINTEXT_DEMUXER | CONFIG_ADF_DEMUXER | CONFIG_IDF_DEMUXER
  65. /**
  66. * Given filesize and width, calculate height (assume font_height of 16)
  67. */
  68. static void calculate_height(AVCodecParameters *par, uint64_t fsize)
  69. {
  70. par->height = (fsize / ((par->width>>3)*2)) << 4;
  71. }
  72. #endif
  73. #if CONFIG_BINTEXT_DEMUXER
  74. static const uint8_t next_magic[]={
  75. 0x1A, 0x1B, '[', '0', ';', '3', '0', ';', '4', '0', 'm', 'N', 'E', 'X', 'T', 0x00
  76. };
  77. static int next_tag_read(AVFormatContext *avctx, uint64_t *fsize)
  78. {
  79. AVIOContext *pb = avctx->pb;
  80. char buf[36];
  81. int len;
  82. uint64_t start_pos = avio_size(pb) - 256;
  83. avio_seek(pb, start_pos, SEEK_SET);
  84. if (avio_read(pb, buf, sizeof(next_magic)) != sizeof(next_magic))
  85. return -1;
  86. if (memcmp(buf, next_magic, sizeof(next_magic)))
  87. return -1;
  88. if (avio_r8(pb) != 0x01)
  89. return -1;
  90. *fsize -= 256;
  91. #define GET_EFI2_META(name,size) \
  92. len = avio_r8(pb); \
  93. if (len < 1 || len > size) \
  94. return -1; \
  95. if (avio_read(pb, buf, size) == size && *buf) { \
  96. buf[len] = 0; \
  97. av_dict_set(&avctx->metadata, name, buf, 0); \
  98. }
  99. GET_EFI2_META("filename", 12)
  100. GET_EFI2_META("author", 20)
  101. GET_EFI2_META("publisher", 20)
  102. GET_EFI2_META("title", 35)
  103. return 0;
  104. }
  105. static void predict_width(AVCodecParameters *par, uint64_t fsize, int got_width)
  106. {
  107. /** attempt to guess width */
  108. if (!got_width)
  109. par->width = fsize > 4000 ? (160<<3) : (80<<3);
  110. }
  111. static int bin_probe(AVProbeData *p)
  112. {
  113. const uint8_t *d = p->buf;
  114. int magic = 0, sauce = 0;
  115. int invisible = 0;
  116. int i;
  117. if (p->buf_size > 256)
  118. magic = !memcmp(d + p->buf_size - 256, next_magic, sizeof(next_magic));
  119. if (p->buf_size > 128)
  120. sauce = !memcmp(d + p->buf_size - 128, "SAUCE00", 7);
  121. if (magic)
  122. return AVPROBE_SCORE_EXTENSION + 1;
  123. if (av_match_ext(p->filename, "bin")) {
  124. AVCodecParameters par;
  125. int got_width = 0;
  126. par.width = par.height = 0;
  127. if (sauce)
  128. return AVPROBE_SCORE_EXTENSION + 1;
  129. predict_width(&par, p->buf_size, got_width);
  130. if (par.width <= 0)
  131. return 0;
  132. calculate_height(&par, p->buf_size);
  133. if (par.height <= 0)
  134. return 0;
  135. for (i = 0; i < p->buf_size - 256; i+=2) {
  136. if ((d[i+1] & 15) == (d[i+1] >> 4) && d[i] && d[i] != 0xFF && d[i] != ' ') {
  137. invisible ++;
  138. }
  139. }
  140. if (par.width * par.height * 2 / (8*16) == p->buf_size)
  141. return AVPROBE_SCORE_MAX / 2;
  142. return 0;
  143. }
  144. if (sauce)
  145. return 1;
  146. return 0;
  147. }
  148. static int bintext_read_header(AVFormatContext *s)
  149. {
  150. BinDemuxContext *bin = s->priv_data;
  151. AVIOContext *pb = s->pb;
  152. AVStream *st = init_stream(s);
  153. if (!st)
  154. return AVERROR(ENOMEM);
  155. st->codecpar->codec_id = AV_CODEC_ID_BINTEXT;
  156. if (ff_alloc_extradata(st->codecpar, 2))
  157. return AVERROR(ENOMEM);
  158. st->codecpar->extradata[0] = 16;
  159. st->codecpar->extradata[1] = 0;
  160. if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
  161. int got_width = 0;
  162. bin->fsize = avio_size(pb);
  163. if (ff_sauce_read(s, &bin->fsize, &got_width, 0) < 0)
  164. next_tag_read(s, &bin->fsize);
  165. if (!bin->width) {
  166. predict_width(st->codecpar, bin->fsize, got_width);
  167. calculate_height(st->codecpar, bin->fsize);
  168. }
  169. avio_seek(pb, 0, SEEK_SET);
  170. }
  171. return 0;
  172. }
  173. #endif /* CONFIG_BINTEXT_DEMUXER */
  174. #if CONFIG_XBIN_DEMUXER
  175. static int xbin_probe(AVProbeData *p)
  176. {
  177. const uint8_t *d = p->buf;
  178. if (AV_RL32(d) == MKTAG('X','B','I','N') && d[4] == 0x1A &&
  179. AV_RL16(d+5) > 0 && AV_RL16(d+5) <= 160 &&
  180. d[9] > 0 && d[9] <= 32)
  181. return AVPROBE_SCORE_MAX;
  182. return 0;
  183. }
  184. static int xbin_read_header(AVFormatContext *s)
  185. {
  186. BinDemuxContext *bin = s->priv_data;
  187. AVIOContext *pb = s->pb;
  188. char fontheight, flags;
  189. AVStream *st = init_stream(s);
  190. if (!st)
  191. return AVERROR(ENOMEM);
  192. avio_skip(pb, 5);
  193. st->codecpar->width = avio_rl16(pb)<<3;
  194. st->codecpar->height = avio_rl16(pb);
  195. fontheight = avio_r8(pb);
  196. st->codecpar->height *= fontheight;
  197. flags = avio_r8(pb);
  198. st->codecpar->extradata_size = 2;
  199. if ((flags & BINTEXT_PALETTE))
  200. st->codecpar->extradata_size += 48;
  201. if ((flags & BINTEXT_FONT))
  202. st->codecpar->extradata_size += fontheight * (flags & 0x10 ? 512 : 256);
  203. st->codecpar->codec_id = flags & 4 ? AV_CODEC_ID_XBIN : AV_CODEC_ID_BINTEXT;
  204. if (ff_alloc_extradata(st->codecpar, st->codecpar->extradata_size))
  205. return AVERROR(ENOMEM);
  206. st->codecpar->extradata[0] = fontheight;
  207. st->codecpar->extradata[1] = flags;
  208. if (avio_read(pb, st->codecpar->extradata + 2, st->codecpar->extradata_size - 2) < 0)
  209. return AVERROR(EIO);
  210. if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
  211. bin->fsize = avio_size(pb) - 9 - st->codecpar->extradata_size;
  212. ff_sauce_read(s, &bin->fsize, NULL, 0);
  213. avio_seek(pb, 9 + st->codecpar->extradata_size, SEEK_SET);
  214. }
  215. return 0;
  216. }
  217. #endif /* CONFIG_XBIN_DEMUXER */
  218. #if CONFIG_ADF_DEMUXER
  219. static int adf_read_header(AVFormatContext *s)
  220. {
  221. BinDemuxContext *bin = s->priv_data;
  222. AVIOContext *pb = s->pb;
  223. AVStream *st;
  224. if (avio_r8(pb) != 1)
  225. return AVERROR_INVALIDDATA;
  226. st = init_stream(s);
  227. if (!st)
  228. return AVERROR(ENOMEM);
  229. st->codecpar->codec_id = AV_CODEC_ID_BINTEXT;
  230. if (ff_alloc_extradata(st->codecpar, 2 + 48 + 4096))
  231. return AVERROR(ENOMEM);
  232. st->codecpar->extradata[0] = 16;
  233. st->codecpar->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT;
  234. if (avio_read(pb, st->codecpar->extradata + 2, 24) < 0)
  235. return AVERROR(EIO);
  236. avio_skip(pb, 144);
  237. if (avio_read(pb, st->codecpar->extradata + 2 + 24, 24) < 0)
  238. return AVERROR(EIO);
  239. if (avio_read(pb, st->codecpar->extradata + 2 + 48, 4096) < 0)
  240. return AVERROR(EIO);
  241. if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
  242. int got_width = 0;
  243. bin->fsize = avio_size(pb) - 1 - 192 - 4096;
  244. st->codecpar->width = 80<<3;
  245. ff_sauce_read(s, &bin->fsize, &got_width, 0);
  246. if (!bin->width)
  247. calculate_height(st->codecpar, bin->fsize);
  248. avio_seek(pb, 1 + 192 + 4096, SEEK_SET);
  249. }
  250. return 0;
  251. }
  252. #endif /* CONFIG_ADF_DEMUXER */
  253. #if CONFIG_IDF_DEMUXER
  254. static const uint8_t idf_magic[] = {
  255. 0x04, 0x31, 0x2e, 0x34, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x00, 0x15, 0x00
  256. };
  257. static int idf_probe(AVProbeData *p)
  258. {
  259. if (p->buf_size < sizeof(idf_magic))
  260. return 0;
  261. if (!memcmp(p->buf, idf_magic, sizeof(idf_magic)))
  262. return AVPROBE_SCORE_MAX;
  263. return 0;
  264. }
  265. static int idf_read_header(AVFormatContext *s)
  266. {
  267. BinDemuxContext *bin = s->priv_data;
  268. AVIOContext *pb = s->pb;
  269. AVStream *st;
  270. int got_width = 0;
  271. if (!(pb->seekable & AVIO_SEEKABLE_NORMAL))
  272. return AVERROR(EIO);
  273. st = init_stream(s);
  274. if (!st)
  275. return AVERROR(ENOMEM);
  276. st->codecpar->codec_id = AV_CODEC_ID_IDF;
  277. if (ff_alloc_extradata(st->codecpar, 2 + 48 + 4096))
  278. return AVERROR(ENOMEM);
  279. st->codecpar->extradata[0] = 16;
  280. st->codecpar->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT;
  281. avio_seek(pb, avio_size(pb) - 4096 - 48, SEEK_SET);
  282. if (avio_read(pb, st->codecpar->extradata + 2 + 48, 4096) < 0)
  283. return AVERROR(EIO);
  284. if (avio_read(pb, st->codecpar->extradata + 2, 48) < 0)
  285. return AVERROR(EIO);
  286. bin->fsize = avio_size(pb) - 12 - 4096 - 48;
  287. ff_sauce_read(s, &bin->fsize, &got_width, 0);
  288. if (!bin->width)
  289. calculate_height(st->codecpar, bin->fsize);
  290. avio_seek(pb, 12, SEEK_SET);
  291. return 0;
  292. }
  293. #endif /* CONFIG_IDF_DEMUXER */
  294. static int read_packet(AVFormatContext *s,
  295. AVPacket *pkt)
  296. {
  297. BinDemuxContext *bin = s->priv_data;
  298. if (bin->fsize > 0) {
  299. if (av_get_packet(s->pb, pkt, bin->fsize) < 0)
  300. return AVERROR(EIO);
  301. bin->fsize = -1; /* done */
  302. } else if (!bin->fsize) {
  303. if (avio_feof(s->pb))
  304. return AVERROR(EIO);
  305. if (av_get_packet(s->pb, pkt, bin->chars_per_frame) < 0)
  306. return AVERROR(EIO);
  307. } else {
  308. return AVERROR(EIO);
  309. }
  310. pkt->flags |= AV_PKT_FLAG_KEY;
  311. return 0;
  312. }
  313. #define OFFSET(x) offsetof(BinDemuxContext, x)
  314. static const AVOption options[] = {
  315. { "linespeed", "set simulated line speed (bytes per second)", OFFSET(chars_per_frame), AV_OPT_TYPE_INT, {.i64 = 6000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM},
  316. { "video_size", "set video size, such as 640x480 or hd720.", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
  317. { "framerate", "set framerate (frames per second)", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  318. { NULL },
  319. };
  320. #define CLASS(name) \
  321. (const AVClass[1]){{ \
  322. .class_name = name, \
  323. .item_name = av_default_item_name, \
  324. .option = options, \
  325. .version = LIBAVUTIL_VERSION_INT, \
  326. }}
  327. #if CONFIG_BINTEXT_DEMUXER
  328. AVInputFormat ff_bintext_demuxer = {
  329. .name = "bin",
  330. .long_name = NULL_IF_CONFIG_SMALL("Binary text"),
  331. .priv_data_size = sizeof(BinDemuxContext),
  332. .read_probe = bin_probe,
  333. .read_header = bintext_read_header,
  334. .read_packet = read_packet,
  335. .priv_class = CLASS("Binary text demuxer"),
  336. };
  337. #endif
  338. #if CONFIG_XBIN_DEMUXER
  339. AVInputFormat ff_xbin_demuxer = {
  340. .name = "xbin",
  341. .long_name = NULL_IF_CONFIG_SMALL("eXtended BINary text (XBIN)"),
  342. .priv_data_size = sizeof(BinDemuxContext),
  343. .read_probe = xbin_probe,
  344. .read_header = xbin_read_header,
  345. .read_packet = read_packet,
  346. .priv_class = CLASS("eXtended BINary text (XBIN) demuxer"),
  347. };
  348. #endif
  349. #if CONFIG_ADF_DEMUXER
  350. AVInputFormat ff_adf_demuxer = {
  351. .name = "adf",
  352. .long_name = NULL_IF_CONFIG_SMALL("Artworx Data Format"),
  353. .priv_data_size = sizeof(BinDemuxContext),
  354. .read_header = adf_read_header,
  355. .read_packet = read_packet,
  356. .extensions = "adf",
  357. .priv_class = CLASS("Artworx Data Format demuxer"),
  358. };
  359. #endif
  360. #if CONFIG_IDF_DEMUXER
  361. AVInputFormat ff_idf_demuxer = {
  362. .name = "idf",
  363. .long_name = NULL_IF_CONFIG_SMALL("iCE Draw File"),
  364. .priv_data_size = sizeof(BinDemuxContext),
  365. .read_probe = idf_probe,
  366. .read_header = idf_read_header,
  367. .read_packet = read_packet,
  368. .extensions = "idf",
  369. .priv_class = CLASS("iCE Draw File demuxer"),
  370. };
  371. #endif