libavcodec/bintext.c
07a70cab
 /*
  * Binary text decoder
  * eXtended BINary text (XBIN) decoder
  * iCEDraw File decoder
  * Copyright (c) 2010 Peter Ross (pross@xvid.org)
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
64960fed
  * @file
07a70cab
  * Binary text decoder
  * eXtended BINary text (XBIN) decoder
  * iCEDraw File decoder
  */
 
 #include "libavutil/intreadwrite.h"
81bbce9c
 #include "libavutil/xga_font_data.h"
07a70cab
 #include "avcodec.h"
 #include "cga_data.h"
 #include "bintext.h"
80e9e63c
 #include "internal.h"
07a70cab
 
 typedef struct XbinContext {
80e9e63c
     AVFrame *frame;
07a70cab
     int palette[16];
     int flags;
     int font_height;
     const uint8_t *font;
     int x, y;
 } XbinContext;
 
 static av_cold int decode_init(AVCodecContext *avctx)
 {
     XbinContext *s = avctx->priv_data;
     uint8_t *p;
     int i;
 
ac627b3d
     avctx->pix_fmt = AV_PIX_FMT_PAL8;
07a70cab
     p = avctx->extradata;
     if (p) {
         s->font_height = p[0];
         s->flags = p[1];
         p += 2;
1b7a4c93
         if(avctx->extradata_size < 2 + (!!(s->flags & BINTEXT_PALETTE))*3*16
                                      + (!!(s->flags & BINTEXT_FONT))*s->font_height*256) {
             av_log(avctx, AV_LOG_ERROR, "not enough extradata\n");
             return AVERROR_INVALIDDATA;
         }
07a70cab
     } else {
         s->font_height = 8;
         s->flags = 0;
     }
 
     if ((s->flags & BINTEXT_PALETTE)) {
         for (i = 0; i < 16; i++) {
2b656844
             s->palette[i] = 0xFF000000 | (AV_RB24(p) << 2) | ((AV_RB24(p) >> 4) & 0x30303);
07a70cab
             p += 3;
         }
     } else {
         for (i = 0; i < 16; i++)
             s->palette[i] = 0xFF000000 | ff_cga_palette[i];
     }
 
     if ((s->flags & BINTEXT_FONT)) {
         s->font = p;
     } else {
         switch(s->font_height) {
         default:
696a780c
             av_log(avctx, AV_LOG_WARNING, "font height %i not supported\n", s->font_height);
07a70cab
             s->font_height = 8;
         case 8:
81bbce9c
             s->font = avpriv_cga_font;
07a70cab
             break;
         case 16:
81bbce9c
             s->font = avpriv_vga16_font;
07a70cab
             break;
         }
     }
 
80e9e63c
     s->frame = av_frame_alloc();
     if (!s->frame)
         return AVERROR(ENOMEM);
 
07a70cab
     return 0;
 }
 
 #define DEFAULT_BG_COLOR 0
ab94d1be
 av_unused static void hscroll(AVCodecContext *avctx)
07a70cab
 {
     XbinContext *s = avctx->priv_data;
     if (s->y < avctx->height - s->font_height) {
         s->y += s->font_height;
     } else {
80e9e63c
         memmove(s->frame->data[0], s->frame->data[0] + s->font_height*s->frame->linesize[0],
             (avctx->height - s->font_height)*s->frame->linesize[0]);
         memset(s->frame->data[0] + (avctx->height - s->font_height)*s->frame->linesize[0],
             DEFAULT_BG_COLOR, s->font_height * s->frame->linesize[0]);
07a70cab
     }
 }
 
 #define FONT_WIDTH 8
 
 /**
  * Draw character to screen
  */
 static void draw_char(AVCodecContext *avctx, int c, int a)
 {
     XbinContext *s = avctx->priv_data;
     if (s->y > avctx->height - s->font_height)
         return;
80e9e63c
     ff_draw_pc_font(s->frame->data[0] + s->y * s->frame->linesize[0] + s->x,
                     s->frame->linesize[0], s->font, s->font_height, c,
07a70cab
                     a & 0x0F, a >> 4);
     s->x += FONT_WIDTH;
b9dbaa40
     if (s->x > avctx->width - FONT_WIDTH) {
07a70cab
         s->x = 0;
         s->y += s->font_height;
     }
 }
 
 static int decode_frame(AVCodecContext *avctx,
4012cd6c
                             void *data, int *got_frame,
07a70cab
                             AVPacket *avpkt)
 {
     XbinContext *s = avctx->priv_data;
     const uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
     const uint8_t *buf_end = buf+buf_size;
80e9e63c
     int ret;
07a70cab
 
3775af0f
     s->x = s->y = 0;
1ec94b0f
     if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
         return ret;
80e9e63c
     s->frame->pict_type           = AV_PICTURE_TYPE_I;
     s->frame->palette_has_changed = 1;
     memcpy(s->frame->data[1], s->palette, 16 * 4);
07a70cab
 
7a72695c
     if (avctx->codec_id == AV_CODEC_ID_XBIN) {
07a70cab
         while (buf + 2 < buf_end) {
             int i,c,a;
             int type  = *buf >> 6;
             int count = (*buf & 0x3F) + 1;
             buf++;
             switch (type) {
             case 0: //no compression
                 for (i = 0; i < count && buf + 1 < buf_end; i++) {
                     draw_char(avctx, buf[0], buf[1]);
                     buf += 2;
                 }
                 break;
             case 1: //character compression
                 c = *buf++;
                 for (i = 0; i < count && buf < buf_end; i++)
                     draw_char(avctx, c, *buf++);
                 break;
             case 2: //attribute compression
                 a = *buf++;
                 for (i = 0; i < count && buf < buf_end; i++)
                     draw_char(avctx, *buf++, a);
                 break;
             case 3: //character/attribute compression
                 c = *buf++;
                 a = *buf++;
                 for (i = 0; i < count && buf < buf_end; i++)
                     draw_char(avctx, c, a);
                 break;
             }
         }
7a72695c
     } else if (avctx->codec_id == AV_CODEC_ID_IDF) {
07a70cab
         while (buf + 2 < buf_end) {
             if (AV_RL16(buf) == 1) {
                int i;
                if (buf + 6 > buf_end)
                    break;
                for (i = 0; i < buf[2]; i++)
                    draw_char(avctx, buf[4], buf[5]);
                buf += 6;
             } else {
                draw_char(avctx, buf[0], buf[1]);
                buf += 2;
             }
         }
     } else {
         while (buf + 1 < buf_end) {
             draw_char(avctx, buf[0], buf[1]);
             buf += 2;
         }
     }
 
80e9e63c
     if ((ret = av_frame_ref(data, s->frame)) < 0)
         return ret;
4012cd6c
     *got_frame      = 1;
07a70cab
     return buf_size;
 }
 
 static av_cold int decode_end(AVCodecContext *avctx)
 {
     XbinContext *s = avctx->priv_data;
 
80e9e63c
     av_frame_free(&s->frame);
07a70cab
 
     return 0;
 }
 
68a257e6
 #if CONFIG_BINTEXT_DECODER
07a70cab
 AVCodec ff_bintext_decoder = {
ba10207b
     .name           = "bintext",
b46f1910
     .long_name      = NULL_IF_CONFIG_SMALL("Binary text"),
ba10207b
     .type           = AVMEDIA_TYPE_VIDEO,
7a72695c
     .id             = AV_CODEC_ID_BINTEXT,
ba10207b
     .priv_data_size = sizeof(XbinContext),
     .init           = decode_init,
     .close          = decode_end,
     .decode         = decode_frame,
444e9874
     .capabilities   = AV_CODEC_CAP_DR1,
07a70cab
 };
68a257e6
 #endif
 #if CONFIG_XBIN_DECODER
07a70cab
 AVCodec ff_xbin_decoder = {
ba10207b
     .name           = "xbin",
b46f1910
     .long_name      = NULL_IF_CONFIG_SMALL("eXtended BINary text"),
ba10207b
     .type           = AVMEDIA_TYPE_VIDEO,
7a72695c
     .id             = AV_CODEC_ID_XBIN,
ba10207b
     .priv_data_size = sizeof(XbinContext),
     .init           = decode_init,
     .close          = decode_end,
     .decode         = decode_frame,
444e9874
     .capabilities   = AV_CODEC_CAP_DR1,
07a70cab
 };
68a257e6
 #endif
 #if CONFIG_IDF_DECODER
07a70cab
 AVCodec ff_idf_decoder = {
ba10207b
     .name           = "idf",
b46f1910
     .long_name      = NULL_IF_CONFIG_SMALL("iCEDraw text"),
ba10207b
     .type           = AVMEDIA_TYPE_VIDEO,
7a72695c
     .id             = AV_CODEC_ID_IDF,
ba10207b
     .priv_data_size = sizeof(XbinContext),
     .init           = decode_init,
     .close          = decode_end,
     .decode         = decode_frame,
444e9874
     .capabilities   = AV_CODEC_CAP_DR1,
07a70cab
 };
68a257e6
 #endif