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.

377 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 "avformat.h"
  33. #include "internal.h"
  34. #include "sauce.h"
  35. #include "libavcodec/bintext.h"
  36. #define LINE_RATE 6000 /** characters per second */
  37. typedef struct {
  38. int chars_per_frame;
  39. uint64_t fsize; /**< file size less metadata buffer */
  40. } BinDemuxContext;
  41. #if CONFIG_BINTEXT_DEMUXER | CONFIG_ADF_DEMUXER | CONFIG_IDF_DEMUXER
  42. /**
  43. * Given filesize and width, calculate height (assume font_height of 16)
  44. */
  45. static void calculate_height(AVCodecContext *avctx, uint64_t fsize)
  46. {
  47. avctx->height = (fsize / ((avctx->width>>3)*2)) << 4;
  48. }
  49. #endif
  50. #if CONFIG_BINTEXT_DEMUXER
  51. static const uint8_t next_magic[]={
  52. 0x1A, 0x1B, '[', '0', ';', '3', '0', ';', '4', '0', 'm', 'N', 'E', 'X', 'T', 0x00
  53. };
  54. static int next_tag_read(AVFormatContext *avctx, uint64_t *fsize)
  55. {
  56. AVIOContext *pb = avctx->pb;
  57. char buf[36];
  58. int len;
  59. uint64_t start_pos = avio_size(pb) - 256;
  60. avio_seek(pb, start_pos, SEEK_SET);
  61. if (avio_read(pb, buf, sizeof(next_magic)) != sizeof(next_magic))
  62. return -1;
  63. if (memcmp(buf, next_magic, sizeof(next_magic)))
  64. return -1;
  65. if (avio_r8(pb) != 0x01)
  66. return -1;
  67. *fsize -= 256;
  68. #define GET_EFI2_META(name,size) \
  69. len = avio_r8(pb); \
  70. if (len < 1 || len > size) \
  71. return -1; \
  72. if (avio_read(pb, buf, size) == size && *buf) { \
  73. buf[len] = 0; \
  74. av_dict_set(&avctx->metadata, name, buf, 0); \
  75. }
  76. GET_EFI2_META("filename", 12)
  77. GET_EFI2_META("author", 20)
  78. GET_EFI2_META("publisher", 20)
  79. GET_EFI2_META("title", 35)
  80. return 0;
  81. }
  82. static void predict_width(AVCodecContext *avctx, uint64_t fsize, int got_width)
  83. {
  84. /** attempt to guess width */
  85. if (!got_width)
  86. avctx->width = fsize > 4000 ? (160<<3) : (80<<3);
  87. }
  88. static AVStream * init_stream(AVFormatContext *s,
  89. AVFormatParameters *ap)
  90. {
  91. BinDemuxContext *bin = s->priv_data;
  92. AVStream *st = avformat_new_stream(s, NULL);
  93. if (!st)
  94. return NULL;
  95. st->codec->codec_tag = 0;
  96. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  97. if (!ap->time_base.num) {
  98. avpriv_set_pts_info(st, 60, 1, 25);
  99. } else {
  100. avpriv_set_pts_info(st, 60, ap->time_base.num, ap->time_base.den);
  101. }
  102. /* simulate tty display speed */
  103. bin->chars_per_frame = FFMAX(av_q2d(st->time_base) * (ap->sample_rate ? ap->sample_rate : LINE_RATE), 1);
  104. st->codec->width = ap->width ? ap->width : (80<<3);
  105. st->codec->height = ap->height ? ap->height : (25<<4);
  106. return st;
  107. }
  108. static int bintext_read_header(AVFormatContext *s,
  109. AVFormatParameters *ap)
  110. {
  111. BinDemuxContext *bin = s->priv_data;
  112. AVIOContext *pb = s->pb;
  113. AVStream *st = init_stream(s, ap);
  114. if (!st)
  115. return AVERROR(ENOMEM);
  116. st->codec->codec_id = CODEC_ID_BINTEXT;
  117. st->codec->extradata_size = 2;
  118. st->codec->extradata = av_malloc(st->codec->extradata_size);
  119. if (!st->codec->extradata)
  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 (!ap->width)
  129. predict_width(st->codec, bin->fsize, got_width);
  130. if (!ap->height)
  131. calculate_height(st->codec, bin->fsize);
  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. AVFormatParameters *ap)
  149. {
  150. BinDemuxContext *bin = s->priv_data;
  151. AVIOContext *pb = s->pb;
  152. char fontheight, flags;
  153. AVStream *st = init_stream(s, ap);
  154. if (!st)
  155. return AVERROR(ENOMEM);
  156. avio_skip(pb, 5);
  157. st->codec->width = avio_rl16(pb)<<3;
  158. st->codec->height = avio_rl16(pb);
  159. fontheight = avio_r8(pb);
  160. st->codec->height *= fontheight;
  161. flags = avio_r8(pb);
  162. st->codec->extradata_size = 2;
  163. if ((flags & BINTEXT_PALETTE))
  164. st->codec->extradata_size += 48;
  165. if ((flags & BINTEXT_FONT))
  166. st->codec->extradata_size += fontheight * (flags & 0x10 ? 512 : 256);
  167. st->codec->codec_id = flags & 4 ? CODEC_ID_XBIN : CODEC_ID_BINTEXT;
  168. st->codec->extradata = av_malloc(st->codec->extradata_size);
  169. if (!st->codec->extradata)
  170. return AVERROR(ENOMEM);
  171. st->codec->extradata[0] = fontheight;
  172. st->codec->extradata[1] = flags;
  173. if (avio_read(pb, st->codec->extradata + 2, st->codec->extradata_size - 2) < 0)
  174. return AVERROR(EIO);
  175. if (pb->seekable) {
  176. bin->fsize = avio_size(pb) - 9 - st->codec->extradata_size;
  177. ff_sauce_read(s, &bin->fsize, NULL, 0);
  178. avio_seek(pb, 9 + st->codec->extradata_size, SEEK_SET);
  179. }
  180. return 0;
  181. }
  182. #endif /* CONFIG_XBIN_DEMUXER */
  183. #if CONFIG_ADF_DEMUXER
  184. static int adf_read_header(AVFormatContext *s,
  185. AVFormatParameters *ap)
  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, ap);
  193. if (!st)
  194. return AVERROR(ENOMEM);
  195. st->codec->codec_id = CODEC_ID_BINTEXT;
  196. st->codec->extradata_size = 2 + 48 + 4096;
  197. st->codec->extradata = av_malloc(st->codec->extradata_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 (!ap->height)
  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. AVFormatParameters *ap)
  235. {
  236. BinDemuxContext *bin = s->priv_data;
  237. AVIOContext *pb = s->pb;
  238. AVStream *st;
  239. int got_width = 0;
  240. if (!pb->seekable)
  241. return AVERROR(EIO);
  242. st = init_stream(s, ap);
  243. if (!st)
  244. return AVERROR(ENOMEM);
  245. st->codec->codec_id = CODEC_ID_IDF;
  246. st->codec->extradata_size = 2 + 48 + 4096;
  247. st->codec->extradata = av_malloc(st->codec->extradata_size);
  248. if (!st->codec->extradata)
  249. return AVERROR(ENOMEM);
  250. st->codec->extradata[0] = 16;
  251. st->codec->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT;
  252. avio_seek(pb, avio_size(pb) - 4096 - 48, SEEK_SET);
  253. if (avio_read(pb, st->codec->extradata + 2 + 48, 4096) < 0)
  254. return AVERROR(EIO);
  255. if (avio_read(pb, st->codec->extradata + 2, 48) < 0)
  256. return AVERROR(EIO);
  257. bin->fsize = avio_size(pb) - 12 - 4096 - 48;
  258. ff_sauce_read(s, &bin->fsize, &got_width, 0);
  259. if (!ap->height)
  260. calculate_height(st->codec, bin->fsize);
  261. avio_seek(pb, 12, SEEK_SET);
  262. return 0;
  263. }
  264. #endif /* CONFIG_IDF_DEMUXER */
  265. static int read_packet(AVFormatContext *s,
  266. AVPacket *pkt)
  267. {
  268. BinDemuxContext *bin = s->priv_data;
  269. if (bin->fsize > 0) {
  270. if (av_get_packet(s->pb, pkt, bin->fsize) < 0)
  271. return AVERROR(EIO);
  272. bin->fsize = -1; /* done */
  273. } else if (!bin->fsize) {
  274. if (url_feof(s->pb))
  275. return AVERROR(EIO);
  276. if (av_get_packet(s->pb, pkt, bin->chars_per_frame) < 0)
  277. return AVERROR(EIO);
  278. } else {
  279. return AVERROR(EIO);
  280. }
  281. pkt->flags |= AV_PKT_FLAG_KEY;
  282. return 0;
  283. }
  284. #if CONFIG_BINTEXT_DEMUXER
  285. AVInputFormat ff_bintext_demuxer = {
  286. .name = "bin",
  287. .long_name = NULL_IF_CONFIG_SMALL("Binary text"),
  288. .priv_data_size = sizeof(BinDemuxContext),
  289. .read_header = bintext_read_header,
  290. .read_packet = read_packet,
  291. .extensions = "bin",
  292. };
  293. #endif
  294. #if CONFIG_XBIN_DEMUXER
  295. AVInputFormat ff_xbin_demuxer = {
  296. .name = "xbin",
  297. .long_name = NULL_IF_CONFIG_SMALL("eXtended BINary text (XBIN)"),
  298. .priv_data_size = sizeof(BinDemuxContext),
  299. .read_probe = xbin_probe,
  300. .read_header = xbin_read_header,
  301. .read_packet = read_packet,
  302. };
  303. #endif
  304. #if CONFIG_ADF_DEMUXER
  305. AVInputFormat ff_adf_demuxer = {
  306. .name = "adf",
  307. .long_name = NULL_IF_CONFIG_SMALL("Artworx Data Format"),
  308. .priv_data_size = sizeof(BinDemuxContext),
  309. .read_header = adf_read_header,
  310. .read_packet = read_packet,
  311. .extensions = "adf",
  312. };
  313. #endif
  314. #if CONFIG_IDF_DEMUXER
  315. AVInputFormat ff_idf_demuxer = {
  316. .name = "idf",
  317. .long_name = NULL_IF_CONFIG_SMALL("iCE Draw File"),
  318. .priv_data_size = sizeof(BinDemuxContext),
  319. .read_probe = idf_probe,
  320. .read_header = idf_read_header,
  321. .read_packet = read_packet,
  322. .extensions = "idf",
  323. };
  324. #endif