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.

396 lines
12KB

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