bintext.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. char *video_size; /**< video size (WxH pixels) (private option) */
  44. char *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->codec->codec_tag = 0;
  54. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  55. if (bin->video_size) {
  56. if (av_parse_video_size(&st->codec->width, &st->codec->height, bin->video_size) < 0) {
  57. av_log(s, AV_LOG_ERROR, "Could not parse video size: '%s'\n", bin->video_size);
  58. return NULL;
  59. }
  60. } else {
  61. st->codec->width = (80<<3);
  62. st->codec->height = (25<<4);
  63. }
  64. if (bin->framerate) {
  65. AVRational framerate;
  66. if (av_parse_video_rate(&framerate, bin->framerate) < 0) {
  67. av_log(s, AV_LOG_ERROR, "Could not parse framerate: '%s'\n", bin->framerate);
  68. return NULL;
  69. }
  70. avpriv_set_pts_info(st, 60, framerate.den, framerate.num);
  71. } else {
  72. avpriv_set_pts_info(st, 60, 1, 25);
  73. }
  74. /* simulate tty display speed */
  75. bin->chars_per_frame = FFMAX(av_q2d(st->time_base) * bin->chars_per_frame, 1);
  76. return st;
  77. }
  78. #if CONFIG_BINTEXT_DEMUXER | CONFIG_ADF_DEMUXER | CONFIG_IDF_DEMUXER
  79. /**
  80. * Given filesize and width, calculate height (assume font_height of 16)
  81. */
  82. static void calculate_height(AVCodecContext *avctx, uint64_t fsize)
  83. {
  84. avctx->height = (fsize / ((avctx->width>>3)*2)) << 4;
  85. }
  86. #endif
  87. #if CONFIG_BINTEXT_DEMUXER
  88. static const uint8_t next_magic[]={
  89. 0x1A, 0x1B, '[', '0', ';', '3', '0', ';', '4', '0', 'm', 'N', 'E', 'X', 'T', 0x00
  90. };
  91. static int next_tag_read(AVFormatContext *avctx, uint64_t *fsize)
  92. {
  93. AVIOContext *pb = avctx->pb;
  94. char buf[36];
  95. int len;
  96. uint64_t start_pos = avio_size(pb) - 256;
  97. avio_seek(pb, start_pos, SEEK_SET);
  98. if (avio_read(pb, buf, sizeof(next_magic)) != sizeof(next_magic))
  99. return -1;
  100. if (memcmp(buf, next_magic, sizeof(next_magic)))
  101. return -1;
  102. if (avio_r8(pb) != 0x01)
  103. return -1;
  104. *fsize -= 256;
  105. #define GET_EFI2_META(name,size) \
  106. len = avio_r8(pb); \
  107. if (len < 1 || len > size) \
  108. return -1; \
  109. if (avio_read(pb, buf, size) == size && *buf) { \
  110. buf[len] = 0; \
  111. av_dict_set(&avctx->metadata, name, buf, 0); \
  112. }
  113. GET_EFI2_META("filename", 12)
  114. GET_EFI2_META("author", 20)
  115. GET_EFI2_META("publisher", 20)
  116. GET_EFI2_META("title", 35)
  117. return 0;
  118. }
  119. static void predict_width(AVCodecContext *avctx, uint64_t fsize, int got_width)
  120. {
  121. /** attempt to guess width */
  122. if (!got_width)
  123. avctx->width = fsize > 4000 ? (160<<3) : (80<<3);
  124. }
  125. static int bintext_read_header(AVFormatContext *s)
  126. {
  127. BinDemuxContext *bin = s->priv_data;
  128. AVIOContext *pb = s->pb;
  129. AVStream *st = init_stream(s);
  130. if (!st)
  131. return AVERROR(ENOMEM);
  132. st->codec->codec_id = AV_CODEC_ID_BINTEXT;
  133. st->codec->extradata_size = 2;
  134. st->codec->extradata = av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  135. if (!st->codec->extradata)
  136. return AVERROR(ENOMEM);
  137. st->codec->extradata[0] = 16;
  138. st->codec->extradata[1] = 0;
  139. if (pb->seekable) {
  140. int got_width = 0;
  141. bin->fsize = avio_size(pb);
  142. if (ff_sauce_read(s, &bin->fsize, &got_width, 0) < 0)
  143. next_tag_read(s, &bin->fsize);
  144. if (!bin->video_size) {
  145. predict_width(st->codec, bin->fsize, got_width);
  146. calculate_height(st->codec, bin->fsize);
  147. }
  148. avio_seek(pb, 0, SEEK_SET);
  149. }
  150. return 0;
  151. }
  152. #endif /* CONFIG_BINTEXT_DEMUXER */
  153. #if CONFIG_XBIN_DEMUXER
  154. static int xbin_probe(AVProbeData *p)
  155. {
  156. const uint8_t *d = p->buf;
  157. if (AV_RL32(d) == MKTAG('X','B','I','N') && d[4] == 0x1A &&
  158. AV_RL16(d+5) > 0 && AV_RL16(d+5) <= 160 &&
  159. d[9] > 0 && d[9] <= 32)
  160. return AVPROBE_SCORE_MAX;
  161. return 0;
  162. }
  163. static int xbin_read_header(AVFormatContext *s)
  164. {
  165. BinDemuxContext *bin = s->priv_data;
  166. AVIOContext *pb = s->pb;
  167. char fontheight, flags;
  168. AVStream *st = init_stream(s);
  169. if (!st)
  170. return AVERROR(ENOMEM);
  171. avio_skip(pb, 5);
  172. st->codec->width = avio_rl16(pb)<<3;
  173. st->codec->height = avio_rl16(pb);
  174. fontheight = avio_r8(pb);
  175. st->codec->height *= fontheight;
  176. flags = avio_r8(pb);
  177. st->codec->extradata_size = 2;
  178. if ((flags & BINTEXT_PALETTE))
  179. st->codec->extradata_size += 48;
  180. if ((flags & BINTEXT_FONT))
  181. st->codec->extradata_size += fontheight * (flags & 0x10 ? 512 : 256);
  182. st->codec->codec_id = flags & 4 ? AV_CODEC_ID_XBIN : AV_CODEC_ID_BINTEXT;
  183. st->codec->extradata = av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  184. if (!st->codec->extradata)
  185. return AVERROR(ENOMEM);
  186. st->codec->extradata[0] = fontheight;
  187. st->codec->extradata[1] = flags;
  188. if (avio_read(pb, st->codec->extradata + 2, st->codec->extradata_size - 2) < 0)
  189. return AVERROR(EIO);
  190. if (pb->seekable) {
  191. bin->fsize = avio_size(pb) - 9 - st->codec->extradata_size;
  192. ff_sauce_read(s, &bin->fsize, NULL, 0);
  193. avio_seek(pb, 9 + st->codec->extradata_size, SEEK_SET);
  194. }
  195. return 0;
  196. }
  197. #endif /* CONFIG_XBIN_DEMUXER */
  198. #if CONFIG_ADF_DEMUXER
  199. static int adf_read_header(AVFormatContext *s)
  200. {
  201. BinDemuxContext *bin = s->priv_data;
  202. AVIOContext *pb = s->pb;
  203. AVStream *st;
  204. if (avio_r8(pb) != 1)
  205. return AVERROR_INVALIDDATA;
  206. st = init_stream(s);
  207. if (!st)
  208. return AVERROR(ENOMEM);
  209. st->codec->codec_id = AV_CODEC_ID_BINTEXT;
  210. st->codec->extradata_size = 2 + 48 + 4096;
  211. st->codec->extradata = av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  212. if (!st->codec->extradata)
  213. return AVERROR(ENOMEM);
  214. st->codec->extradata[0] = 16;
  215. st->codec->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT;
  216. if (avio_read(pb, st->codec->extradata + 2, 24) < 0)
  217. return AVERROR(EIO);
  218. avio_skip(pb, 144);
  219. if (avio_read(pb, st->codec->extradata + 2 + 24, 24) < 0)
  220. return AVERROR(EIO);
  221. if (avio_read(pb, st->codec->extradata + 2 + 48, 4096) < 0)
  222. return AVERROR(EIO);
  223. if (pb->seekable) {
  224. int got_width = 0;
  225. bin->fsize = avio_size(pb) - 1 - 192 - 4096;
  226. st->codec->width = 80<<3;
  227. ff_sauce_read(s, &bin->fsize, &got_width, 0);
  228. if (!bin->video_size)
  229. calculate_height(st->codec, bin->fsize);
  230. avio_seek(pb, 1 + 192 + 4096, SEEK_SET);
  231. }
  232. return 0;
  233. }
  234. #endif /* CONFIG_ADF_DEMUXER */
  235. #if CONFIG_IDF_DEMUXER
  236. static const uint8_t idf_magic[] = {
  237. 0x04, 0x31, 0x2e, 0x34, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x00, 0x15, 0x00
  238. };
  239. static int idf_probe(AVProbeData *p)
  240. {
  241. if (p->buf_size < sizeof(idf_magic))
  242. return 0;
  243. if (!memcmp(p->buf, idf_magic, sizeof(idf_magic)))
  244. return AVPROBE_SCORE_MAX;
  245. return 0;
  246. }
  247. static int idf_read_header(AVFormatContext *s)
  248. {
  249. BinDemuxContext *bin = s->priv_data;
  250. AVIOContext *pb = s->pb;
  251. AVStream *st;
  252. int got_width = 0;
  253. if (!pb->seekable)
  254. return AVERROR(EIO);
  255. st = init_stream(s);
  256. if (!st)
  257. return AVERROR(ENOMEM);
  258. st->codec->codec_id = AV_CODEC_ID_IDF;
  259. st->codec->extradata_size = 2 + 48 + 4096;
  260. st->codec->extradata = av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  261. if (!st->codec->extradata)
  262. return AVERROR(ENOMEM);
  263. st->codec->extradata[0] = 16;
  264. st->codec->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT;
  265. avio_seek(pb, avio_size(pb) - 4096 - 48, SEEK_SET);
  266. if (avio_read(pb, st->codec->extradata + 2 + 48, 4096) < 0)
  267. return AVERROR(EIO);
  268. if (avio_read(pb, st->codec->extradata + 2, 48) < 0)
  269. return AVERROR(EIO);
  270. bin->fsize = avio_size(pb) - 12 - 4096 - 48;
  271. ff_sauce_read(s, &bin->fsize, &got_width, 0);
  272. if (!bin->video_size)
  273. calculate_height(st->codec, bin->fsize);
  274. avio_seek(pb, 12, SEEK_SET);
  275. return 0;
  276. }
  277. #endif /* CONFIG_IDF_DEMUXER */
  278. static int read_packet(AVFormatContext *s,
  279. AVPacket *pkt)
  280. {
  281. BinDemuxContext *bin = s->priv_data;
  282. if (bin->fsize > 0) {
  283. if (av_get_packet(s->pb, pkt, bin->fsize) < 0)
  284. return AVERROR(EIO);
  285. bin->fsize = -1; /* done */
  286. } else if (!bin->fsize) {
  287. if (url_feof(s->pb))
  288. return AVERROR(EIO);
  289. if (av_get_packet(s->pb, pkt, bin->chars_per_frame) < 0)
  290. return AVERROR(EIO);
  291. } else {
  292. return AVERROR(EIO);
  293. }
  294. pkt->flags |= AV_PKT_FLAG_KEY;
  295. return 0;
  296. }
  297. #define OFFSET(x) offsetof(BinDemuxContext, x)
  298. static const AVOption options[] = {
  299. { "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},
  300. { "video_size", "set video size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
  301. { "framerate", "set framerate (frames per second)", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
  302. { NULL },
  303. };
  304. #define CLASS(name) \
  305. (const AVClass[1]){{ \
  306. .class_name = name, \
  307. .item_name = av_default_item_name, \
  308. .option = options, \
  309. .version = LIBAVUTIL_VERSION_INT, \
  310. }}
  311. #if CONFIG_BINTEXT_DEMUXER
  312. AVInputFormat ff_bintext_demuxer = {
  313. .name = "bin",
  314. .long_name = NULL_IF_CONFIG_SMALL("Binary text"),
  315. .priv_data_size = sizeof(BinDemuxContext),
  316. .read_header = bintext_read_header,
  317. .read_packet = read_packet,
  318. .extensions = "bin",
  319. .priv_class = CLASS("Binary text demuxer"),
  320. };
  321. #endif
  322. #if CONFIG_XBIN_DEMUXER
  323. AVInputFormat ff_xbin_demuxer = {
  324. .name = "xbin",
  325. .long_name = NULL_IF_CONFIG_SMALL("eXtended BINary text (XBIN)"),
  326. .priv_data_size = sizeof(BinDemuxContext),
  327. .read_probe = xbin_probe,
  328. .read_header = xbin_read_header,
  329. .read_packet = read_packet,
  330. .priv_class = CLASS("eXtended BINary text (XBIN) demuxer"),
  331. };
  332. #endif
  333. #if CONFIG_ADF_DEMUXER
  334. AVInputFormat ff_adf_demuxer = {
  335. .name = "adf",
  336. .long_name = NULL_IF_CONFIG_SMALL("Artworx Data Format"),
  337. .priv_data_size = sizeof(BinDemuxContext),
  338. .read_header = adf_read_header,
  339. .read_packet = read_packet,
  340. .extensions = "adf",
  341. .priv_class = CLASS("Artworx Data Format demuxer"),
  342. };
  343. #endif
  344. #if CONFIG_IDF_DEMUXER
  345. AVInputFormat ff_idf_demuxer = {
  346. .name = "idf",
  347. .long_name = NULL_IF_CONFIG_SMALL("iCE Draw File"),
  348. .priv_data_size = sizeof(BinDemuxContext),
  349. .read_probe = idf_probe,
  350. .read_header = idf_read_header,
  351. .read_packet = read_packet,
  352. .extensions = "idf",
  353. .priv_class = CLASS("iCE Draw File demuxer"),
  354. };
  355. #endif