bintext.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 "avformat.h"
  33. #include "sauce.h"
  34. #include "libavcodec/bintext.h"
  35. #define LINE_RATE 6000 /** characters per second */
  36. typedef struct {
  37. int chars_per_frame;
  38. uint64_t fsize; /**< file size less metadata buffer */
  39. } BinDemuxContext;
  40. #if CONFIG_BINTEXT_DEMUXER | CONFIG_ADF_DEMUXER | CONFIG_IDF_DEMUXER
  41. /**
  42. * Given filesize and width, calculate height (assume font_height of 16)
  43. */
  44. static void calculate_height(AVCodecContext *avctx, uint64_t fsize)
  45. {
  46. avctx->height = (fsize / ((avctx->width>>3)*2)) << 4;
  47. }
  48. #endif
  49. #if CONFIG_BINTEXT_DEMUXER
  50. static const uint8_t next_magic[]={
  51. 0x1A, 0x1B, '[', '0', ';', '3', '0', ';', '4', '0', 'm', 'N', 'E', 'X', 'T', 0x00
  52. };
  53. static int next_tag_read(AVFormatContext *avctx, uint64_t *fsize)
  54. {
  55. ByteIOContext *pb = avctx->pb;
  56. char buf[36];
  57. int len;
  58. uint64_t start_pos = url_fsize(pb) - 256;
  59. url_fseek(pb, start_pos, SEEK_SET);
  60. if (get_buffer(pb, buf, sizeof(next_magic)) != sizeof(next_magic))
  61. return -1;
  62. if (memcmp(buf, next_magic, sizeof(next_magic)))
  63. return -1;
  64. if (get_byte(pb) != 0x01)
  65. return -1;
  66. *fsize -= 256;
  67. #define GET_EFI2_META(name,size) \
  68. len = get_byte(pb); \
  69. if (len < 1 || len > size) \
  70. return -1; \
  71. if (get_buffer(pb, buf, size) == size && *buf) { \
  72. buf[len] = 0; \
  73. av_metadata_set2(&avctx->metadata, name, buf, 0); \
  74. }
  75. GET_EFI2_META("filename", 12)
  76. GET_EFI2_META("author", 20)
  77. GET_EFI2_META("publisher", 20)
  78. GET_EFI2_META("title", 35)
  79. return 0;
  80. }
  81. static void predict_width(AVCodecContext *avctx, uint64_t fsize, int got_width)
  82. {
  83. /** attempt to guess width */
  84. if (!got_width)
  85. avctx->width = fsize > 4000 ? (160<<3) : (80<<3);
  86. }
  87. static AVStream * init_stream(AVFormatContext *s,
  88. AVFormatParameters *ap)
  89. {
  90. BinDemuxContext *bin = s->priv_data;
  91. AVStream *st = av_new_stream(s, 0);
  92. if (!st)
  93. return NULL;
  94. st->codec->codec_tag = 0;
  95. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  96. if (!ap->time_base.num) {
  97. av_set_pts_info(st, 60, 1, 25);
  98. } else {
  99. av_set_pts_info(st, 60, ap->time_base.num, ap->time_base.den);
  100. }
  101. /* simulate tty display speed */
  102. bin->chars_per_frame = FFMAX(av_q2d(st->time_base) * (ap->sample_rate ? ap->sample_rate : LINE_RATE), 1);
  103. st->codec->width = ap->width ? ap->width : (80<<3);
  104. st->codec->height = ap->height ? ap->height : (25<<4);
  105. return st;
  106. }
  107. static int bintext_read_header(AVFormatContext *s,
  108. AVFormatParameters *ap)
  109. {
  110. BinDemuxContext *bin = s->priv_data;
  111. ByteIOContext *pb = s->pb;
  112. AVStream *st = init_stream(s, ap);
  113. if (!st)
  114. return AVERROR(ENOMEM);
  115. st->codec->codec_id = CODEC_ID_BINTEXT;
  116. st->codec->extradata_size = 2;
  117. st->codec->extradata = av_malloc(st->codec->extradata_size);
  118. if (!st->codec->extradata)
  119. return AVERROR(ENOMEM);
  120. st->codec->extradata[0] = 16;
  121. st->codec->extradata[1] = 0;
  122. if (!url_is_streamed(pb)) {
  123. int got_width = 0;
  124. bin->fsize = url_fsize(pb);
  125. if (ff_sauce_read(s, &bin->fsize, &got_width, 0) < 0)
  126. next_tag_read(s, &bin->fsize);
  127. if (!ap->width)
  128. predict_width(st->codec, bin->fsize, got_width);
  129. if (!ap->height)
  130. calculate_height(st->codec, bin->fsize);
  131. url_fseek(pb, 0, SEEK_SET);
  132. }
  133. return 0;
  134. };
  135. #endif /* CONFIG_BINTEXT_DEMUXER */
  136. #if CONFIG_XBIN_DEMUXER
  137. static int xbin_probe(AVProbeData *p)
  138. {
  139. const uint8_t *d = p->buf;
  140. if (AV_RL32(d) == MKTAG('X','B','I','N') && d[4] == 0x1A &&
  141. AV_RL16(d+5) > 0 && AV_RL16(d+5) <= 160 &&
  142. d[9] > 0 && d[9] <= 32)
  143. return AVPROBE_SCORE_MAX;
  144. return 0;
  145. }
  146. static int xbin_read_header(AVFormatContext *s,
  147. AVFormatParameters *ap)
  148. {
  149. BinDemuxContext *bin = s->priv_data;
  150. ByteIOContext *pb = s->pb;
  151. char fontheight, flags;
  152. AVStream *st = init_stream(s, ap);
  153. if (!st)
  154. return AVERROR(ENOMEM);
  155. url_fskip(pb, 5);
  156. st->codec->width = get_le16(pb)<<3;
  157. st->codec->height = get_le16(pb);
  158. fontheight = get_byte(pb);
  159. st->codec->height *= fontheight;
  160. flags = get_byte(pb);
  161. st->codec->extradata_size = 2;
  162. if ((flags & BINTEXT_PALETTE))
  163. st->codec->extradata_size += 48;
  164. if ((flags & BINTEXT_FONT))
  165. st->codec->extradata_size += fontheight * (flags & 0x10 ? 512 : 256);
  166. st->codec->codec_id = flags & 4 ? CODEC_ID_XBIN : CODEC_ID_BINTEXT;
  167. st->codec->extradata = av_malloc(st->codec->extradata_size);
  168. if (!st->codec->extradata)
  169. return AVERROR(ENOMEM);
  170. st->codec->extradata[0] = fontheight;
  171. st->codec->extradata[1] = flags;
  172. if (get_buffer(pb, st->codec->extradata + 2, st->codec->extradata_size - 2) < 0)
  173. return AVERROR(EIO);
  174. if (!url_is_streamed(pb)) {
  175. bin->fsize = url_fsize(pb) - 9 - st->codec->extradata_size;
  176. ff_sauce_read(s, &bin->fsize, NULL, 0);
  177. url_fseek(pb, 9 + st->codec->extradata_size, SEEK_SET);
  178. }
  179. return 0;
  180. }
  181. #endif /* CONFIG_XBIN_DEMUXER */
  182. #if CONFIG_ADF_DEMUXER
  183. static int adf_read_header(AVFormatContext *s,
  184. AVFormatParameters *ap)
  185. {
  186. BinDemuxContext *bin = s->priv_data;
  187. ByteIOContext *pb = s->pb;
  188. AVStream *st;
  189. if (get_byte(pb) != 1)
  190. return AVERROR_INVALIDDATA;
  191. st = init_stream(s, ap);
  192. if (!st)
  193. return AVERROR(ENOMEM);
  194. st->codec->codec_id = CODEC_ID_BINTEXT;
  195. st->codec->extradata_size = 2 + 48 + 4096;
  196. st->codec->extradata = av_malloc(st->codec->extradata_size);
  197. if (!st->codec->extradata)
  198. return AVERROR(ENOMEM);
  199. st->codec->extradata[0] = 16;
  200. st->codec->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT;
  201. if (get_buffer(pb, st->codec->extradata + 2, 24) < 0)
  202. return AVERROR(EIO);
  203. url_fskip(pb, 144);
  204. if (get_buffer(pb, st->codec->extradata + 2 + 24, 24) < 0)
  205. return AVERROR(EIO);
  206. if (get_buffer(pb, st->codec->extradata + 2 + 48, 4096) < 0)
  207. return AVERROR(EIO);
  208. if (!url_is_streamed(pb)) {
  209. int got_width = 0;
  210. bin->fsize = url_fsize(pb) - 1 - 192 - 4096;
  211. st->codec->width = 80<<3;
  212. ff_sauce_read(s, &bin->fsize, &got_width, 0);
  213. if (!ap->height)
  214. calculate_height(st->codec, bin->fsize);
  215. url_fseek(pb, 1 + 192 + 4096, SEEK_SET);
  216. }
  217. return 0;
  218. }
  219. #endif /* CONFIG_ADF_DEMUXER */
  220. #if CONFIG_IDF_DEMUXER
  221. static const uint8_t idf_magic[] = {
  222. 0x04, 0x31, 0x2e, 0x34, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x00, 0x15, 0x00
  223. };
  224. static int idf_probe(AVProbeData *p)
  225. {
  226. if (p->buf_size < sizeof(idf_magic))
  227. return 0;
  228. if (!memcmp(p->buf, idf_magic, sizeof(idf_magic)))
  229. return AVPROBE_SCORE_MAX;
  230. return 0;
  231. }
  232. static int idf_read_header(AVFormatContext *s,
  233. AVFormatParameters *ap)
  234. {
  235. BinDemuxContext *bin = s->priv_data;
  236. ByteIOContext *pb = s->pb;
  237. AVStream *st;
  238. int got_width = 0;
  239. if (url_is_streamed(pb))
  240. return AVERROR(EIO);
  241. st = init_stream(s, ap);
  242. if (!st)
  243. return AVERROR(ENOMEM);
  244. st->codec->codec_id = CODEC_ID_IDF;
  245. st->codec->extradata_size = 2 + 48 + 4096;
  246. st->codec->extradata = av_malloc(st->codec->extradata_size);
  247. if (!st->codec->extradata)
  248. return AVERROR(ENOMEM);
  249. st->codec->extradata[0] = 16;
  250. st->codec->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT;
  251. url_fseek(pb, url_fsize(pb) - 4096 - 48, SEEK_SET);
  252. if (get_buffer(pb, st->codec->extradata + 2 + 48, 4096) < 0)
  253. return AVERROR(EIO);
  254. if (get_buffer(pb, st->codec->extradata + 2, 48) < 0)
  255. return AVERROR(EIO);
  256. bin->fsize = url_fsize(pb) - 12 - 4096 - 48;
  257. ff_sauce_read(s, &bin->fsize, &got_width, 0);
  258. if (!ap->height)
  259. calculate_height(st->codec, bin->fsize);
  260. url_fseek(pb, 12, SEEK_SET);
  261. return 0;
  262. }
  263. #endif /* CONFIG_IDF_DEMUXER */
  264. static int read_packet(AVFormatContext *s,
  265. AVPacket *pkt)
  266. {
  267. BinDemuxContext *bin = s->priv_data;
  268. if (bin->fsize > 0) {
  269. if (av_get_packet(s->pb, pkt, bin->fsize) < 0)
  270. return AVERROR(EIO);
  271. bin->fsize = -1; /* done */
  272. } else if (!bin->fsize) {
  273. if (url_feof(s->pb))
  274. return AVERROR(EIO);
  275. if (av_get_packet(s->pb, pkt, bin->chars_per_frame) < 0)
  276. return AVERROR(EIO);
  277. } else {
  278. return AVERROR(EIO);
  279. }
  280. pkt->flags |= AV_PKT_FLAG_KEY;
  281. return 0;
  282. }
  283. #if CONFIG_BINTEXT_DEMUXER
  284. AVInputFormat ff_bintext_demuxer = {
  285. .name = "bin",
  286. .long_name = NULL_IF_CONFIG_SMALL("Binary text"),
  287. .priv_data_size = sizeof(BinDemuxContext),
  288. .read_header = bintext_read_header,
  289. .read_packet = read_packet,
  290. .extensions = "bin",
  291. };
  292. #endif
  293. #if CONFIG_XBIN_DEMUXER
  294. AVInputFormat ff_xbin_demuxer = {
  295. .name = "xbin",
  296. .long_name = NULL_IF_CONFIG_SMALL("eXtended BINary text (XBIN)"),
  297. .priv_data_size = sizeof(BinDemuxContext),
  298. .read_probe = xbin_probe,
  299. .read_header = xbin_read_header,
  300. .read_packet = read_packet,
  301. };
  302. #endif
  303. #if CONFIG_ADF_DEMUXER
  304. AVInputFormat ff_adf_demuxer = {
  305. .name = "adf",
  306. .long_name = NULL_IF_CONFIG_SMALL("Artworx Data Format"),
  307. .priv_data_size = sizeof(BinDemuxContext),
  308. .read_header = adf_read_header,
  309. .read_packet = read_packet,
  310. .extensions = "adf",
  311. };
  312. #endif
  313. #if CONFIG_IDF_DEMUXER
  314. AVInputFormat ff_idf_demuxer = {
  315. .name = "idf",
  316. .long_name = NULL_IF_CONFIG_SMALL("iCE Draw File"),
  317. .priv_data_size = sizeof(BinDemuxContext),
  318. .read_probe = idf_probe,
  319. .read_header = idf_read_header,
  320. .read_packet = read_packet,
  321. .extensions = "idf",
  322. };
  323. #endif