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.

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