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.

389 lines
11KB

  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->codec->codec_tag = 0;
  54. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  55. if (!bin->width) {
  56. st->codec->width = (80<<3);
  57. st->codec->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(AVCodecContext *avctx, uint64_t fsize)
  69. {
  70. avctx->height = (fsize / ((avctx->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(AVCodecContext *avctx, uint64_t fsize, int got_width)
  106. {
  107. /** attempt to guess width */
  108. if (!got_width)
  109. avctx->width = fsize > 4000 ? (160<<3) : (80<<3);
  110. }
  111. static int bintext_read_header(AVFormatContext *s)
  112. {
  113. BinDemuxContext *bin = s->priv_data;
  114. AVIOContext *pb = s->pb;
  115. AVStream *st = init_stream(s);
  116. if (!st)
  117. return AVERROR(ENOMEM);
  118. st->codec->codec_id = AV_CODEC_ID_BINTEXT;
  119. if (ff_alloc_extradata(st->codec, 2))
  120. return AVERROR(ENOMEM);
  121. st->codec->extradata[0] = 16;
  122. st->codec->extradata[1] = 0;
  123. if (pb->seekable) {
  124. int got_width = 0;
  125. bin->fsize = avio_size(pb);
  126. if (ff_sauce_read(s, &bin->fsize, &got_width, 0) < 0)
  127. next_tag_read(s, &bin->fsize);
  128. if (!bin->width) {
  129. predict_width(st->codec, bin->fsize, got_width);
  130. calculate_height(st->codec, bin->fsize);
  131. }
  132. avio_seek(pb, 0, SEEK_SET);
  133. }
  134. return 0;
  135. }
  136. #endif /* CONFIG_BINTEXT_DEMUXER */
  137. #if CONFIG_XBIN_DEMUXER
  138. static int xbin_probe(AVProbeData *p)
  139. {
  140. const uint8_t *d = p->buf;
  141. if (AV_RL32(d) == MKTAG('X','B','I','N') && d[4] == 0x1A &&
  142. AV_RL16(d+5) > 0 && AV_RL16(d+5) <= 160 &&
  143. d[9] > 0 && d[9] <= 32)
  144. return AVPROBE_SCORE_MAX;
  145. return 0;
  146. }
  147. static int xbin_read_header(AVFormatContext *s)
  148. {
  149. BinDemuxContext *bin = s->priv_data;
  150. AVIOContext *pb = s->pb;
  151. char fontheight, flags;
  152. AVStream *st = init_stream(s);
  153. if (!st)
  154. return AVERROR(ENOMEM);
  155. avio_skip(pb, 5);
  156. st->codec->width = avio_rl16(pb)<<3;
  157. st->codec->height = avio_rl16(pb);
  158. fontheight = avio_r8(pb);
  159. st->codec->height *= fontheight;
  160. flags = avio_r8(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 ? AV_CODEC_ID_XBIN : AV_CODEC_ID_BINTEXT;
  167. if (ff_alloc_extradata(st->codec, st->codec->extradata_size))
  168. return AVERROR(ENOMEM);
  169. st->codec->extradata[0] = fontheight;
  170. st->codec->extradata[1] = flags;
  171. if (avio_read(pb, st->codec->extradata + 2, st->codec->extradata_size - 2) < 0)
  172. return AVERROR(EIO);
  173. if (pb->seekable) {
  174. bin->fsize = avio_size(pb) - 9 - st->codec->extradata_size;
  175. ff_sauce_read(s, &bin->fsize, NULL, 0);
  176. avio_seek(pb, 9 + st->codec->extradata_size, SEEK_SET);
  177. }
  178. return 0;
  179. }
  180. #endif /* CONFIG_XBIN_DEMUXER */
  181. #if CONFIG_ADF_DEMUXER
  182. static int adf_read_header(AVFormatContext *s)
  183. {
  184. BinDemuxContext *bin = s->priv_data;
  185. AVIOContext *pb = s->pb;
  186. AVStream *st;
  187. if (avio_r8(pb) != 1)
  188. return AVERROR_INVALIDDATA;
  189. st = init_stream(s);
  190. if (!st)
  191. return AVERROR(ENOMEM);
  192. st->codec->codec_id = AV_CODEC_ID_BINTEXT;
  193. if (ff_alloc_extradata(st->codec, 2 + 48 + 4096))
  194. return AVERROR(ENOMEM);
  195. st->codec->extradata[0] = 16;
  196. st->codec->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT;
  197. if (avio_read(pb, st->codec->extradata + 2, 24) < 0)
  198. return AVERROR(EIO);
  199. avio_skip(pb, 144);
  200. if (avio_read(pb, st->codec->extradata + 2 + 24, 24) < 0)
  201. return AVERROR(EIO);
  202. if (avio_read(pb, st->codec->extradata + 2 + 48, 4096) < 0)
  203. return AVERROR(EIO);
  204. if (pb->seekable) {
  205. int got_width = 0;
  206. bin->fsize = avio_size(pb) - 1 - 192 - 4096;
  207. st->codec->width = 80<<3;
  208. ff_sauce_read(s, &bin->fsize, &got_width, 0);
  209. if (!bin->width)
  210. calculate_height(st->codec, bin->fsize);
  211. avio_seek(pb, 1 + 192 + 4096, SEEK_SET);
  212. }
  213. return 0;
  214. }
  215. #endif /* CONFIG_ADF_DEMUXER */
  216. #if CONFIG_IDF_DEMUXER
  217. static const uint8_t idf_magic[] = {
  218. 0x04, 0x31, 0x2e, 0x34, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x00, 0x15, 0x00
  219. };
  220. static int idf_probe(AVProbeData *p)
  221. {
  222. if (p->buf_size < sizeof(idf_magic))
  223. return 0;
  224. if (!memcmp(p->buf, idf_magic, sizeof(idf_magic)))
  225. return AVPROBE_SCORE_MAX;
  226. return 0;
  227. }
  228. static int idf_read_header(AVFormatContext *s)
  229. {
  230. BinDemuxContext *bin = s->priv_data;
  231. AVIOContext *pb = s->pb;
  232. AVStream *st;
  233. int got_width = 0;
  234. if (!pb->seekable)
  235. return AVERROR(EIO);
  236. st = init_stream(s);
  237. if (!st)
  238. return AVERROR(ENOMEM);
  239. st->codec->codec_id = AV_CODEC_ID_IDF;
  240. if (ff_alloc_extradata(st->codec, 2 + 48 + 4096))
  241. return AVERROR(ENOMEM);
  242. st->codec->extradata[0] = 16;
  243. st->codec->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT;
  244. avio_seek(pb, avio_size(pb) - 4096 - 48, SEEK_SET);
  245. if (avio_read(pb, st->codec->extradata + 2 + 48, 4096) < 0)
  246. return AVERROR(EIO);
  247. if (avio_read(pb, st->codec->extradata + 2, 48) < 0)
  248. return AVERROR(EIO);
  249. bin->fsize = avio_size(pb) - 12 - 4096 - 48;
  250. ff_sauce_read(s, &bin->fsize, &got_width, 0);
  251. if (!bin->width)
  252. calculate_height(st->codec, bin->fsize);
  253. avio_seek(pb, 12, SEEK_SET);
  254. return 0;
  255. }
  256. #endif /* CONFIG_IDF_DEMUXER */
  257. static int read_packet(AVFormatContext *s,
  258. AVPacket *pkt)
  259. {
  260. BinDemuxContext *bin = s->priv_data;
  261. if (bin->fsize > 0) {
  262. if (av_get_packet(s->pb, pkt, bin->fsize) < 0)
  263. return AVERROR(EIO);
  264. bin->fsize = -1; /* done */
  265. } else if (!bin->fsize) {
  266. if (url_feof(s->pb))
  267. return AVERROR(EIO);
  268. if (av_get_packet(s->pb, pkt, bin->chars_per_frame) < 0)
  269. return AVERROR(EIO);
  270. } else {
  271. return AVERROR(EIO);
  272. }
  273. pkt->flags |= AV_PKT_FLAG_KEY;
  274. return 0;
  275. }
  276. #define OFFSET(x) offsetof(BinDemuxContext, x)
  277. static const AVOption options[] = {
  278. { "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},
  279. { "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 },
  280. { "framerate", "set framerate (frames per second)", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
  281. { NULL },
  282. };
  283. #define CLASS(name) \
  284. (const AVClass[1]){{ \
  285. .class_name = name, \
  286. .item_name = av_default_item_name, \
  287. .option = options, \
  288. .version = LIBAVUTIL_VERSION_INT, \
  289. }}
  290. #if CONFIG_BINTEXT_DEMUXER
  291. AVInputFormat ff_bintext_demuxer = {
  292. .name = "bin",
  293. .long_name = NULL_IF_CONFIG_SMALL("Binary text"),
  294. .priv_data_size = sizeof(BinDemuxContext),
  295. .read_header = bintext_read_header,
  296. .read_packet = read_packet,
  297. .extensions = "bin",
  298. .priv_class = CLASS("Binary text demuxer"),
  299. };
  300. #endif
  301. #if CONFIG_XBIN_DEMUXER
  302. AVInputFormat ff_xbin_demuxer = {
  303. .name = "xbin",
  304. .long_name = NULL_IF_CONFIG_SMALL("eXtended BINary text (XBIN)"),
  305. .priv_data_size = sizeof(BinDemuxContext),
  306. .read_probe = xbin_probe,
  307. .read_header = xbin_read_header,
  308. .read_packet = read_packet,
  309. .priv_class = CLASS("eXtended BINary text (XBIN) demuxer"),
  310. };
  311. #endif
  312. #if CONFIG_ADF_DEMUXER
  313. AVInputFormat ff_adf_demuxer = {
  314. .name = "adf",
  315. .long_name = NULL_IF_CONFIG_SMALL("Artworx Data Format"),
  316. .priv_data_size = sizeof(BinDemuxContext),
  317. .read_header = adf_read_header,
  318. .read_packet = read_packet,
  319. .extensions = "adf",
  320. .priv_class = CLASS("Artworx Data Format demuxer"),
  321. };
  322. #endif
  323. #if CONFIG_IDF_DEMUXER
  324. AVInputFormat ff_idf_demuxer = {
  325. .name = "idf",
  326. .long_name = NULL_IF_CONFIG_SMALL("iCE Draw File"),
  327. .priv_data_size = sizeof(BinDemuxContext),
  328. .read_probe = idf_probe,
  329. .read_header = idf_read_header,
  330. .read_packet = read_packet,
  331. .extensions = "idf",
  332. .priv_class = CLASS("iCE Draw File demuxer"),
  333. };
  334. #endif