LCOV - code coverage report
Current view: top level - libavformat - bintext.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 39 197 19.8 %
Date: 2018-02-23 19:59:10 Functions: 5 12 41.7 %

          Line data    Source code
       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             : /**
      26             :  * @file
      27             :  * Binary text demuxer
      28             :  * eXtended BINary text (XBIN) demuxer
      29             :  * Artworx Data Format demuxer
      30             :  * iCEDraw File demuxer
      31             :  */
      32             : 
      33             : #include "libavutil/intreadwrite.h"
      34             : #include "libavutil/opt.h"
      35             : #include "libavutil/parseutils.h"
      36             : #include "avformat.h"
      37             : #include "internal.h"
      38             : #include "sauce.h"
      39             : #include "libavcodec/bintext.h"
      40             : 
      41             : typedef struct {
      42             :     const AVClass *class;
      43             :     int chars_per_frame; /**< characters to send decoder per frame;
      44             :                               set by private options as characters per second, and then
      45             :                               converted to characters per frame at runtime */
      46             :     int width, height;    /**< video size (WxH pixels) (private option) */
      47             :     AVRational framerate; /**< frames per second (private option) */
      48             :     uint64_t fsize;  /**< file size less metadata buffer */
      49             : } BinDemuxContext;
      50             : 
      51           0 : static AVStream * init_stream(AVFormatContext *s)
      52             : {
      53           0 :     BinDemuxContext *bin = s->priv_data;
      54           0 :     AVStream *st = avformat_new_stream(s, NULL);
      55           0 :     if (!st)
      56           0 :         return NULL;
      57           0 :     st->codecpar->codec_tag   = 0;
      58           0 :     st->codecpar->codec_type  = AVMEDIA_TYPE_VIDEO;
      59             : 
      60           0 :     if (!bin->width) {
      61           0 :         st->codecpar->width  = (80<<3);
      62           0 :         st->codecpar->height = (25<<4);
      63             :     }
      64             : 
      65           0 :     avpriv_set_pts_info(st, 60, bin->framerate.den, bin->framerate.num);
      66             : 
      67             :     /* simulate tty display speed */
      68           0 :     bin->chars_per_frame = av_clip(av_q2d(st->time_base) * bin->chars_per_frame, 1, INT_MAX);
      69             : 
      70           0 :     return st;
      71             : }
      72             : 
      73             : #if CONFIG_BINTEXT_DEMUXER | CONFIG_ADF_DEMUXER | CONFIG_IDF_DEMUXER
      74             : /**
      75             :  * Given filesize and width, calculate height (assume font_height of 16)
      76             :  */
      77           2 : static void calculate_height(AVCodecParameters *par, uint64_t fsize)
      78             : {
      79           2 :     par->height = (fsize / ((par->width>>3)*2)) << 4;
      80           2 : }
      81             : #endif
      82             : 
      83             : #if CONFIG_BINTEXT_DEMUXER
      84             : static const uint8_t next_magic[]={
      85             :     0x1A, 0x1B, '[', '0', ';', '3', '0', ';', '4', '0', 'm', 'N', 'E', 'X', 'T', 0x00
      86             : };
      87             : 
      88           0 : static int next_tag_read(AVFormatContext *avctx, uint64_t *fsize)
      89             : {
      90           0 :     AVIOContext *pb = avctx->pb;
      91             :     char buf[36];
      92             :     int len;
      93           0 :     uint64_t start_pos = avio_size(pb) - 256;
      94             : 
      95           0 :     avio_seek(pb, start_pos, SEEK_SET);
      96           0 :     if (avio_read(pb, buf, sizeof(next_magic)) != sizeof(next_magic))
      97           0 :         return -1;
      98           0 :     if (memcmp(buf, next_magic, sizeof(next_magic)))
      99           0 :         return -1;
     100           0 :     if (avio_r8(pb) != 0x01)
     101           0 :         return -1;
     102             : 
     103           0 :     *fsize -= 256;
     104             : 
     105             : #define GET_EFI2_META(name,size) \
     106             :     len = avio_r8(pb); \
     107             :     if (len < 1 || len > size) \
     108             :         return -1; \
     109             :     if (avio_read(pb, buf, size) == size && *buf) { \
     110             :         buf[len] = 0; \
     111             :         av_dict_set(&avctx->metadata, name, buf, 0); \
     112             :     }
     113             : 
     114           0 :     GET_EFI2_META("filename",  12)
     115           0 :     GET_EFI2_META("author",    20)
     116           0 :     GET_EFI2_META("publisher", 20)
     117           0 :     GET_EFI2_META("title",     35)
     118             : 
     119           0 :     return 0;
     120             : }
     121             : 
     122           2 : static void predict_width(AVCodecParameters *par, uint64_t fsize, int got_width)
     123             : {
     124             :     /** attempt to guess width */
     125           2 :     if (!got_width)
     126           2 :         par->width = fsize > 4000 ? (160<<3) : (80<<3);
     127           2 : }
     128             : 
     129        6170 : static int bin_probe(AVProbeData *p)
     130             : {
     131        6170 :     const uint8_t *d = p->buf;
     132        6170 :     int magic = 0, sauce = 0;
     133        6170 :     int invisible = 0;
     134             :     int i;
     135             : 
     136        6170 :     if (p->buf_size > 256)
     137        6154 :         magic = !memcmp(d + p->buf_size - 256, next_magic, sizeof(next_magic));
     138        6170 :     if (p->buf_size > 128)
     139        6165 :         sauce = !memcmp(d + p->buf_size - 128, "SAUCE00", 7);
     140             : 
     141        6170 :     if (magic)
     142           0 :         return AVPROBE_SCORE_EXTENSION + 1;
     143             : 
     144        6170 :     if (av_match_ext(p->filename, "bin")) {
     145             :         AVCodecParameters par;
     146           2 :         int got_width = 0;
     147           2 :         par.width = par.height = 0;
     148           2 :         if (sauce)
     149           0 :             return AVPROBE_SCORE_EXTENSION + 1;
     150             : 
     151           2 :         predict_width(&par, p->buf_size, got_width);
     152           2 :         if (par.width <= 0)
     153           0 :             return 0;
     154           2 :         calculate_height(&par, p->buf_size);
     155           2 :         if (par.height <= 0)
     156           0 :             return 0;
     157             : 
     158        1794 :         for (i = 0; i < p->buf_size - 256;  i+=2) {
     159        1792 :             if ((d[i+1] & 15) == (d[i+1] >> 4) && d[i] && d[i] != 0xFF && d[i] != ' ') {
     160         120 :                 invisible ++;
     161             :             }
     162             :         }
     163             : 
     164           2 :         if (par.width * par.height * 2 / (8*16) == p->buf_size)
     165           0 :             return AVPROBE_SCORE_MAX / 2;
     166           2 :         return 1;
     167             :     }
     168             : 
     169        6168 :     if (sauce)
     170           0 :         return 1;
     171             : 
     172        6168 :     return 0;
     173             : }
     174             : 
     175             : 
     176           0 : static int bintext_read_header(AVFormatContext *s)
     177             : {
     178           0 :     BinDemuxContext *bin = s->priv_data;
     179           0 :     AVIOContext *pb = s->pb;
     180             : 
     181           0 :     AVStream *st = init_stream(s);
     182           0 :     if (!st)
     183           0 :         return AVERROR(ENOMEM);
     184           0 :     st->codecpar->codec_id    = AV_CODEC_ID_BINTEXT;
     185             : 
     186           0 :     if (ff_alloc_extradata(st->codecpar, 2))
     187           0 :         return AVERROR(ENOMEM);
     188           0 :     st->codecpar->extradata[0] = 16;
     189           0 :     st->codecpar->extradata[1] = 0;
     190             : 
     191           0 :     if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
     192           0 :         int got_width = 0;
     193           0 :         bin->fsize = avio_size(pb);
     194           0 :         if (ff_sauce_read(s, &bin->fsize, &got_width, 0) < 0)
     195           0 :             next_tag_read(s, &bin->fsize);
     196           0 :         if (!bin->width) {
     197           0 :             predict_width(st->codecpar, bin->fsize, got_width);
     198           0 :             calculate_height(st->codecpar, bin->fsize);
     199             :         }
     200           0 :         avio_seek(pb, 0, SEEK_SET);
     201             :     }
     202           0 :     return 0;
     203             : }
     204             : #endif /* CONFIG_BINTEXT_DEMUXER */
     205             : 
     206             : #if CONFIG_XBIN_DEMUXER
     207        6170 : static int xbin_probe(AVProbeData *p)
     208             : {
     209        6170 :     const uint8_t *d = p->buf;
     210             : 
     211        6170 :     if (AV_RL32(d) == MKTAG('X','B','I','N') && d[4] == 0x1A &&
     212           0 :         AV_RL16(d+5) > 0 && AV_RL16(d+5) <= 160 &&
     213           0 :         d[9] > 0 && d[9] <= 32)
     214           0 :         return AVPROBE_SCORE_MAX;
     215        6170 :     return 0;
     216             : }
     217             : 
     218           0 : static int xbin_read_header(AVFormatContext *s)
     219             : {
     220           0 :     BinDemuxContext *bin = s->priv_data;
     221           0 :     AVIOContext *pb = s->pb;
     222             :     char fontheight, flags;
     223             : 
     224           0 :     AVStream *st = init_stream(s);
     225           0 :     if (!st)
     226           0 :         return AVERROR(ENOMEM);
     227             : 
     228           0 :     avio_skip(pb, 5);
     229           0 :     st->codecpar->width   = avio_rl16(pb)<<3;
     230           0 :     st->codecpar->height  = avio_rl16(pb);
     231           0 :     fontheight         = avio_r8(pb);
     232           0 :     st->codecpar->height *= fontheight;
     233           0 :     flags              = avio_r8(pb);
     234             : 
     235           0 :     st->codecpar->extradata_size = 2;
     236           0 :     if ((flags & BINTEXT_PALETTE))
     237           0 :         st->codecpar->extradata_size += 48;
     238           0 :     if ((flags & BINTEXT_FONT))
     239           0 :         st->codecpar->extradata_size += fontheight * (flags & 0x10 ? 512 : 256);
     240           0 :     st->codecpar->codec_id    = flags & 4 ? AV_CODEC_ID_XBIN : AV_CODEC_ID_BINTEXT;
     241             : 
     242           0 :     if (ff_alloc_extradata(st->codecpar, st->codecpar->extradata_size))
     243           0 :         return AVERROR(ENOMEM);
     244           0 :     st->codecpar->extradata[0] = fontheight;
     245           0 :     st->codecpar->extradata[1] = flags;
     246           0 :     if (avio_read(pb, st->codecpar->extradata + 2, st->codecpar->extradata_size - 2) < 0)
     247           0 :         return AVERROR(EIO);
     248             : 
     249           0 :     if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
     250           0 :         bin->fsize = avio_size(pb) - 9 - st->codecpar->extradata_size;
     251           0 :         ff_sauce_read(s, &bin->fsize, NULL, 0);
     252           0 :         avio_seek(pb, 9 + st->codecpar->extradata_size, SEEK_SET);
     253             :     }
     254             : 
     255           0 :     return 0;
     256             : }
     257             : #endif /* CONFIG_XBIN_DEMUXER */
     258             : 
     259             : #if CONFIG_ADF_DEMUXER
     260           0 : static int adf_read_header(AVFormatContext *s)
     261             : {
     262           0 :     BinDemuxContext *bin = s->priv_data;
     263           0 :     AVIOContext *pb = s->pb;
     264             :     AVStream *st;
     265             : 
     266           0 :     if (avio_r8(pb) != 1)
     267           0 :         return AVERROR_INVALIDDATA;
     268             : 
     269           0 :     st = init_stream(s);
     270           0 :     if (!st)
     271           0 :         return AVERROR(ENOMEM);
     272           0 :     st->codecpar->codec_id    = AV_CODEC_ID_BINTEXT;
     273             : 
     274           0 :     if (ff_alloc_extradata(st->codecpar, 2 + 48 + 4096))
     275           0 :         return AVERROR(ENOMEM);
     276           0 :     st->codecpar->extradata[0] = 16;
     277           0 :     st->codecpar->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT;
     278             : 
     279           0 :     if (avio_read(pb, st->codecpar->extradata + 2, 24) < 0)
     280           0 :         return AVERROR(EIO);
     281           0 :     avio_skip(pb, 144);
     282           0 :     if (avio_read(pb, st->codecpar->extradata + 2 + 24, 24) < 0)
     283           0 :         return AVERROR(EIO);
     284           0 :     if (avio_read(pb, st->codecpar->extradata + 2 + 48, 4096) < 0)
     285           0 :         return AVERROR(EIO);
     286             : 
     287           0 :     if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
     288           0 :         int got_width = 0;
     289           0 :         bin->fsize = avio_size(pb) - 1 - 192 - 4096;
     290           0 :         st->codecpar->width = 80<<3;
     291           0 :         ff_sauce_read(s, &bin->fsize, &got_width, 0);
     292           0 :         if (!bin->width)
     293           0 :             calculate_height(st->codecpar, bin->fsize);
     294           0 :         avio_seek(pb, 1 + 192 + 4096, SEEK_SET);
     295             :     }
     296           0 :     return 0;
     297             : }
     298             : #endif /* CONFIG_ADF_DEMUXER */
     299             : 
     300             : #if CONFIG_IDF_DEMUXER
     301             : static const uint8_t idf_magic[] = {
     302             :     0x04, 0x31, 0x2e, 0x34, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x00, 0x15, 0x00
     303             : };
     304             : 
     305        6170 : static int idf_probe(AVProbeData *p)
     306             : {
     307        6170 :     if (p->buf_size < sizeof(idf_magic))
     308           0 :         return 0;
     309        6170 :     if (!memcmp(p->buf, idf_magic, sizeof(idf_magic)))
     310           0 :         return AVPROBE_SCORE_MAX;
     311        6170 :     return 0;
     312             : }
     313             : 
     314           0 : static int idf_read_header(AVFormatContext *s)
     315             : {
     316           0 :     BinDemuxContext *bin = s->priv_data;
     317           0 :     AVIOContext *pb = s->pb;
     318             :     AVStream *st;
     319           0 :     int got_width = 0;
     320             : 
     321           0 :     if (!(pb->seekable & AVIO_SEEKABLE_NORMAL))
     322           0 :         return AVERROR(EIO);
     323             : 
     324           0 :     st = init_stream(s);
     325           0 :     if (!st)
     326           0 :         return AVERROR(ENOMEM);
     327           0 :     st->codecpar->codec_id    = AV_CODEC_ID_IDF;
     328             : 
     329           0 :     if (ff_alloc_extradata(st->codecpar, 2 + 48 + 4096))
     330           0 :         return AVERROR(ENOMEM);
     331           0 :     st->codecpar->extradata[0] = 16;
     332           0 :     st->codecpar->extradata[1] = BINTEXT_PALETTE|BINTEXT_FONT;
     333             : 
     334           0 :     avio_seek(pb, avio_size(pb) - 4096 - 48, SEEK_SET);
     335             : 
     336           0 :     if (avio_read(pb, st->codecpar->extradata + 2 + 48, 4096) < 0)
     337           0 :         return AVERROR(EIO);
     338           0 :     if (avio_read(pb, st->codecpar->extradata + 2, 48) < 0)
     339           0 :         return AVERROR(EIO);
     340             : 
     341           0 :     bin->fsize = avio_size(pb) - 12 - 4096 - 48;
     342           0 :     ff_sauce_read(s, &bin->fsize, &got_width, 0);
     343           0 :     if (!bin->width)
     344           0 :         calculate_height(st->codecpar, bin->fsize);
     345           0 :     avio_seek(pb, 12, SEEK_SET);
     346           0 :     return 0;
     347             : }
     348             : #endif /* CONFIG_IDF_DEMUXER */
     349             : 
     350           0 : static int read_packet(AVFormatContext *s,
     351             :                            AVPacket *pkt)
     352             : {
     353           0 :     BinDemuxContext *bin = s->priv_data;
     354             : 
     355           0 :     if (bin->fsize > 0) {
     356           0 :         if (av_get_packet(s->pb, pkt, bin->fsize) < 0)
     357           0 :             return AVERROR(EIO);
     358           0 :         bin->fsize = -1; /* done */
     359           0 :     } else if (!bin->fsize) {
     360           0 :         if (avio_feof(s->pb))
     361           0 :             return AVERROR(EIO);
     362           0 :         if (av_get_packet(s->pb, pkt, bin->chars_per_frame) < 0)
     363           0 :             return AVERROR(EIO);
     364             :     } else {
     365           0 :         return AVERROR(EIO);
     366             :     }
     367             : 
     368           0 :     pkt->flags |= AV_PKT_FLAG_KEY;
     369           0 :     return 0;
     370             : }
     371             : 
     372             : #define OFFSET(x) offsetof(BinDemuxContext, x)
     373             : static const AVOption options[] = {
     374             :     { "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},
     375             :     { "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 },
     376             :     { "framerate", "set framerate (frames per second)", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
     377             :     { NULL },
     378             : };
     379             : 
     380             : #define CLASS(name) \
     381             : (const AVClass[1]){{ \
     382             :     .class_name     = name, \
     383             :     .item_name      = av_default_item_name, \
     384             :     .option         = options, \
     385             :     .version        = LIBAVUTIL_VERSION_INT, \
     386             : }}
     387             : 
     388             : #if CONFIG_BINTEXT_DEMUXER
     389             : AVInputFormat ff_bintext_demuxer = {
     390             :     .name           = "bin",
     391             :     .long_name      = NULL_IF_CONFIG_SMALL("Binary text"),
     392             :     .priv_data_size = sizeof(BinDemuxContext),
     393             :     .read_probe     = bin_probe,
     394             :     .read_header    = bintext_read_header,
     395             :     .read_packet    = read_packet,
     396             :     .priv_class     = CLASS("Binary text demuxer"),
     397             : };
     398             : #endif
     399             : 
     400             : #if CONFIG_XBIN_DEMUXER
     401             : AVInputFormat ff_xbin_demuxer = {
     402             :     .name           = "xbin",
     403             :     .long_name      = NULL_IF_CONFIG_SMALL("eXtended BINary text (XBIN)"),
     404             :     .priv_data_size = sizeof(BinDemuxContext),
     405             :     .read_probe     = xbin_probe,
     406             :     .read_header    = xbin_read_header,
     407             :     .read_packet    = read_packet,
     408             :     .priv_class     = CLASS("eXtended BINary text (XBIN) demuxer"),
     409             : };
     410             : #endif
     411             : 
     412             : #if CONFIG_ADF_DEMUXER
     413             : AVInputFormat ff_adf_demuxer = {
     414             :     .name           = "adf",
     415             :     .long_name      = NULL_IF_CONFIG_SMALL("Artworx Data Format"),
     416             :     .priv_data_size = sizeof(BinDemuxContext),
     417             :     .read_header    = adf_read_header,
     418             :     .read_packet    = read_packet,
     419             :     .extensions     = "adf",
     420             :     .priv_class     = CLASS("Artworx Data Format demuxer"),
     421             : };
     422             : #endif
     423             : 
     424             : #if CONFIG_IDF_DEMUXER
     425             : AVInputFormat ff_idf_demuxer = {
     426             :     .name           = "idf",
     427             :     .long_name      = NULL_IF_CONFIG_SMALL("iCE Draw File"),
     428             :     .priv_data_size = sizeof(BinDemuxContext),
     429             :     .read_probe     = idf_probe,
     430             :     .read_header    = idf_read_header,
     431             :     .read_packet    = read_packet,
     432             :     .extensions     = "idf",
     433             :     .priv_class     = CLASS("iCE Draw File demuxer"),
     434             : };
     435             : #endif

Generated by: LCOV version 1.13