/libavformat/bintext.c

http://github.com/FFmpeg/FFmpeg · C · 439 lines · 340 code · 62 blank · 37 comment · 73 complexity · 265f29aefbc5e190fe4ee872e271dedd MD5 · raw file

  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(const 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 < 8)
  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. int ret;
  153. AVStream *st = init_stream(s);
  154. if (!st)
  155. return AVERROR(ENOMEM);
  156. st->codecpar->codec_id = AV_CODEC_ID_BINTEXT;
  157. if ((ret = ff_alloc_extradata(st->codecpar, 2)) < 0)
  158. return ret;
  159. st->codecpar->extradata[0] = 16;
  160. st->codecpar->extradata[1] = 0;
  161. if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
  162. int got_width = 0;
  163. bin->fsize = avio_size(pb);
  164. if (ff_sauce_read(s, &bin->fsize, &got_width, 0) < 0)
  165. next_tag_read(s, &bin->fsize);
  166. if (!bin->width) {
  167. predict_width(st->codecpar, bin->fsize, got_width);
  168. if (st->codecpar->width < 8)
  169. return AVERROR_INVALIDDATA;
  170. calculate_height(st->codecpar, bin->fsize);
  171. }
  172. avio_seek(pb, 0, SEEK_SET);
  173. }
  174. return 0;
  175. }
  176. #endif /* CONFIG_BINTEXT_DEMUXER */
  177. #if CONFIG_XBIN_DEMUXER
  178. static int xbin_probe(const AVProbeData *p)
  179. {
  180. const uint8_t *d = p->buf;
  181. if (AV_RL32(d) == MKTAG('X','B','I','N') && d[4] == 0x1A &&
  182. AV_RL16(d+5) > 0 && AV_RL16(d+5) <= 160 &&
  183. d[9] > 0 && d[9] <= 32)
  184. return AVPROBE_SCORE_MAX;
  185. return 0;
  186. }
  187. static int xbin_read_header(AVFormatContext *s)
  188. {
  189. BinDemuxContext *bin = s->priv_data;
  190. AVIOContext *pb = s->pb;
  191. char fontheight, flags;
  192. int ret;
  193. AVStream *st = init_stream(s);
  194. if (!st)
  195. return AVERROR(ENOMEM);
  196. avio_skip(pb, 5);
  197. st->codecpar->width = avio_rl16(pb)<<3;
  198. st->codecpar->height = avio_rl16(pb);
  199. fontheight = avio_r8(pb);
  200. st->codecpar->height *= fontheight;
  201. flags = avio_r8(pb);
  202. st->codecpar->extradata_size = 2;
  203. if ((flags & BINTEXT_PALETTE))
  204. st->codecpar->extradata_size += 48;
  205. if ((flags & BINTEXT_FONT))
  206. st->codecpar->extradata_size += fontheight * (flags & 0x10 ? 512 : 256);
  207. st->codecpar->codec_id = flags & 4 ? AV_CODEC_ID_XBIN : AV_CODEC_ID_BINTEXT;
  208. ret = ff_alloc_extradata(st->codecpar, st->codecpar->extradata_size);
  209. if (ret < 0)
  210. return ret;
  211. st->codecpar->extradata[0] = fontheight;
  212. st->codecpar->extradata[1] = flags;
  213. if (avio_read(pb, st->codecpar->extradata + 2, st->codecpar->extradata_size - 2) < 0)
  214. return AVERROR(EIO);
  215. if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
  216. bin->fsize = avio_size(pb) - 9 - st->codecpar->extradata_size;
  217. ff_sauce_read(s, &bin->fsize, NULL, 0);
  218. avio_seek(pb, 9 + st->codecpar->extradata_size, SEEK_SET);
  219. }
  220. return 0;
  221. }
  222. #endif /* CONFIG_XBIN_DEMUXER */
  223. #if CONFIG_ADF_DEMUXER
  224. static int adf_read_header(AVFormatContext *s)
  225. {
  226. BinDemuxContext *bin = s->priv_data;
  227. AVIOContext *pb = s->pb;
  228. AVStream *st;
  229. int ret;
  230. if (avio_r8(pb) != 1)
  231. return AVERROR_INVALIDDATA;
  232. st = init_stream(s);
  233. if (!st)
  234. return AVERROR(ENOMEM);
  235. st->codecpar->codec_id = AV_CODEC_ID_BINTEXT;
  236. if ((ret = ff_alloc_extradata(st->codecpar, 2 + 48 + 4096)) < 0)
  237. return ret;
  238. st->codecpar->extradata[0] = 16;
  239. st->codecpar->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT;
  240. if (avio_read(pb, st->codecpar->extradata + 2, 24) < 0)
  241. return AVERROR(EIO);
  242. avio_skip(pb, 144);
  243. if (avio_read(pb, st->codecpar->extradata + 2 + 24, 24) < 0)
  244. return AVERROR(EIO);
  245. if (avio_read(pb, st->codecpar->extradata + 2 + 48, 4096) < 0)
  246. return AVERROR(EIO);
  247. if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
  248. int got_width = 0;
  249. bin->fsize = avio_size(pb) - 1 - 192 - 4096;
  250. st->codecpar->width = 80<<3;
  251. ff_sauce_read(s, &bin->fsize, &got_width, 0);
  252. if (!bin->width)
  253. calculate_height(st->codecpar, bin->fsize);
  254. avio_seek(pb, 1 + 192 + 4096, SEEK_SET);
  255. }
  256. return 0;
  257. }
  258. #endif /* CONFIG_ADF_DEMUXER */
  259. #if CONFIG_IDF_DEMUXER
  260. static const uint8_t idf_magic[] = {
  261. 0x04, 0x31, 0x2e, 0x34, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x00, 0x15, 0x00
  262. };
  263. static int idf_probe(const AVProbeData *p)
  264. {
  265. if (p->buf_size < sizeof(idf_magic))
  266. return 0;
  267. if (!memcmp(p->buf, idf_magic, sizeof(idf_magic)))
  268. return AVPROBE_SCORE_MAX;
  269. return 0;
  270. }
  271. static int idf_read_header(AVFormatContext *s)
  272. {
  273. BinDemuxContext *bin = s->priv_data;
  274. AVIOContext *pb = s->pb;
  275. AVStream *st;
  276. int got_width = 0, ret;
  277. if (!(pb->seekable & AVIO_SEEKABLE_NORMAL))
  278. return AVERROR(EIO);
  279. st = init_stream(s);
  280. if (!st)
  281. return AVERROR(ENOMEM);
  282. st->codecpar->codec_id = AV_CODEC_ID_IDF;
  283. if ((ret = ff_alloc_extradata(st->codecpar, 2 + 48 + 4096)) < 0)
  284. return ret;
  285. st->codecpar->extradata[0] = 16;
  286. st->codecpar->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT;
  287. avio_seek(pb, avio_size(pb) - 4096 - 48, SEEK_SET);
  288. if (avio_read(pb, st->codecpar->extradata + 2 + 48, 4096) < 0)
  289. return AVERROR(EIO);
  290. if (avio_read(pb, st->codecpar->extradata + 2, 48) < 0)
  291. return AVERROR(EIO);
  292. bin->fsize = avio_size(pb) - 12 - 4096 - 48;
  293. ff_sauce_read(s, &bin->fsize, &got_width, 0);
  294. if (!bin->width)
  295. calculate_height(st->codecpar, bin->fsize);
  296. avio_seek(pb, 12, SEEK_SET);
  297. return 0;
  298. }
  299. #endif /* CONFIG_IDF_DEMUXER */
  300. static int read_packet(AVFormatContext *s,
  301. AVPacket *pkt)
  302. {
  303. BinDemuxContext *bin = s->priv_data;
  304. if (bin->fsize > 0) {
  305. if (av_get_packet(s->pb, pkt, bin->fsize) < 0)
  306. return AVERROR(EIO);
  307. bin->fsize = -1; /* done */
  308. } else if (!bin->fsize) {
  309. if (avio_feof(s->pb))
  310. return AVERROR(EIO);
  311. if (av_get_packet(s->pb, pkt, bin->chars_per_frame) < 0)
  312. return AVERROR(EIO);
  313. } else {
  314. return AVERROR(EIO);
  315. }
  316. pkt->flags |= AV_PKT_FLAG_KEY;
  317. return 0;
  318. }
  319. #define OFFSET(x) offsetof(BinDemuxContext, x)
  320. static const AVOption options[] = {
  321. { "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},
  322. { "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 },
  323. { "framerate", "set framerate (frames per second)", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  324. { NULL },
  325. };
  326. #define CLASS(name) \
  327. (const AVClass[1]){{ \
  328. .class_name = name, \
  329. .item_name = av_default_item_name, \
  330. .option = options, \
  331. .version = LIBAVUTIL_VERSION_INT, \
  332. }}
  333. #if CONFIG_BINTEXT_DEMUXER
  334. AVInputFormat ff_bintext_demuxer = {
  335. .name = "bin",
  336. .long_name = NULL_IF_CONFIG_SMALL("Binary text"),
  337. .priv_data_size = sizeof(BinDemuxContext),
  338. .read_probe = bin_probe,
  339. .read_header = bintext_read_header,
  340. .read_packet = read_packet,
  341. .priv_class = CLASS("Binary text demuxer"),
  342. };
  343. #endif
  344. #if CONFIG_XBIN_DEMUXER
  345. AVInputFormat ff_xbin_demuxer = {
  346. .name = "xbin",
  347. .long_name = NULL_IF_CONFIG_SMALL("eXtended BINary text (XBIN)"),
  348. .priv_data_size = sizeof(BinDemuxContext),
  349. .read_probe = xbin_probe,
  350. .read_header = xbin_read_header,
  351. .read_packet = read_packet,
  352. .priv_class = CLASS("eXtended BINary text (XBIN) demuxer"),
  353. };
  354. #endif
  355. #if CONFIG_ADF_DEMUXER
  356. AVInputFormat ff_adf_demuxer = {
  357. .name = "adf",
  358. .long_name = NULL_IF_CONFIG_SMALL("Artworx Data Format"),
  359. .priv_data_size = sizeof(BinDemuxContext),
  360. .read_header = adf_read_header,
  361. .read_packet = read_packet,
  362. .extensions = "adf",
  363. .priv_class = CLASS("Artworx Data Format demuxer"),
  364. };
  365. #endif
  366. #if CONFIG_IDF_DEMUXER
  367. AVInputFormat ff_idf_demuxer = {
  368. .name = "idf",
  369. .long_name = NULL_IF_CONFIG_SMALL("iCE Draw File"),
  370. .priv_data_size = sizeof(BinDemuxContext),
  371. .read_probe = idf_probe,
  372. .read_header = idf_read_header,
  373. .read_packet = read_packet,
  374. .extensions = "idf",
  375. .priv_class = CLASS("iCE Draw File demuxer"),
  376. };
  377. #endif