1

/*
 * DVB subtitle encoding
 * Copyright (c) 2005 Fabrice Bellard
 *
 * 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
 */
#include "libavutil/opt.h"
#include "avcodec.h"
#include "bytestream.h"
#include "libavutil/colorspace.h"
#include <Imlib2.h>

uint8_t *text[] = {
    (uint8_t *)"Test line 01_01_01",
    (uint8_t *)"Test line 02_02_02",
    (uint8_t *)"Test line 03_03_03",
    (uint8_t *)"Test line 04_04_04",
    (uint8_t *)"Test line 05_05_05",
    (uint8_t *)"Test line 06_06_06",
    (uint8_t *)"Test line 07_07_07",
    (uint8_t *)"Test line 08_08_08",
    (uint8_t *)"Test line 09_09_09",
    (uint8_t *)"Test line 10_10_10",
    (uint8_t *)"Test line 11_11_11",
    (uint8_t *)"Test line 12_12_12",
    (uint8_t *)"Test line 13_13_13",
    (uint8_t *)"Test line 14_14_14",
    (uint8_t *)"Test line 15_15_15",
    (uint8_t *)"Test line 16_16_16",
    (uint8_t *)"Test line 17_17_17",
    (uint8_t *)"Test line 18_18_18",
    (uint8_t *)"Test line 19_19_19",
    (uint8_t *)"Test line 20_20_20",
    (uint8_t *)"Test line 21_21_21",
    (uint8_t *)"Test line 22_22_22",
    (uint8_t *)"Test line 23_23_23",
};

static uint8_t paritytab[] = {
    0x80, 0x01, 0x02, 0x83, 0x04, 0x85, 0x86, 0x07,
    0x08, 0x89, 0x8a, 0x0b, 0x8c, 0x0d, 0x0e, 0x8f,
    0x10, 0x91, 0x92, 0x13, 0x94, 0x15, 0x16, 0x97,
    0x98, 0x19, 0x1a, 0x9b, 0x1c, 0x9d, 0x9e, 0x1f,
    0x20, 0xa1, 0xa2, 0x23, 0xa4, 0x25, 0x26, 0xa7,
    0xa8, 0x29, 0x2a, 0xab, 0x2c, 0xad, 0xae, 0x2f,
    0xb0, 0x31, 0x32, 0xb3, 0x34, 0xb5, 0xb6, 0x37,
    0x38, 0xb9, 0xba, 0x3b, 0xbc, 0x3d, 0x3e, 0xbf,
    0x40, 0xc1, 0xc2, 0x43, 0xc4, 0x45, 0x46, 0xc7,
    0xc8, 0x49, 0x4a, 0xcb, 0x4c, 0xcd, 0xce, 0x4f,
    0xd0, 0x51, 0x52, 0xd3, 0x54, 0xd5, 0xd6, 0x57,
    0x58, 0xd9, 0xda, 0x5b, 0xdc, 0x5d, 0x5e, 0xdf,
    0xe0, 0x61, 0x62, 0xe3, 0x64, 0xe5, 0xe6, 0x67,
    0x68, 0xe9, 0xea, 0x6b, 0xec, 0x6d, 0x6e, 0xef,
    0x70, 0xf1, 0xf2, 0x73, 0xf4, 0x75, 0x76, 0xf7,
    0xf8, 0x79, 0x7a, 0xfb, 0x7c, 0xfd, 0xfe, 0x7f,
};

static uint8_t ham84tab[] = {
    0x15, 0x02, 0x49, 0x5e, 0x64, 0x73, 0x38, 0x2f,
    0xd0, 0xc7, 0x8c, 0x9b, 0xa1, 0xb6, 0xfd, 0xea,
};

static uint8_t invtab[] = {
    0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
    0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
    0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
    0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
    0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
    0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
    0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
    0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
    0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
    0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
    0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
    0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
    0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
    0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
    0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
    0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
    0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
    0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
    0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
    0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
    0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
    0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
    0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
    0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
    0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
    0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
    0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
    0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
    0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
    0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
    0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
    0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff,
};

typedef struct TexAttr {
    uint8_t    xpos;
    uint8_t    ypos;
    uint8_t    bk_color;
    uint8_t    ft_color;
} TexAttr;

typedef struct SubClut {
    uint32_t clut4[4];
    uint32_t clut16[16];
    uint32_t clut256[256];
} SubClut;

typedef union Pixel32 {
    uint32_t integer;
    struct {
        uint8_t b, g, r, a;/*for LITTLE_ENDIAN*/
    }color;
} Pixel32;


typedef struct DVBSubtitleContext {
    AVClass *class;
    /*
     *  type = image, image file's path and image file's suffix
     * */
    char * imagepath;
    char * imagesuffix;
    /*
     * type = ass
     * */
    char * fontpath;
    char * font;
    uint64_t fontinteger;
    int fontalpha;
    uint64_t borderinteger;
    int borderalpha;
    char * sub_align;
    int sub_pos;
    int nb_colors;
    Pixel32 fontcolor;
    Pixel32 bordercolor;
    Imlib_Font fn;
    /*
     * the common variable
     * */
    int hide_state;
    int object_version;
    SubClut clut;
} DVBSubtitleContext;

static int get_head_len(const char * ass)
{
    int head_len = 0;
    int count = 0;

    while(*ass != '\0') {
        head_len++;
        if(*ass == ',')
            count++;
        if(count == 3)
            break;
        ass++;
    }
    if(count == 3)
        return head_len;
    else
        return -1;
}

static void invert_data(uint8_t *q, uint8_t len)
{
    uint8_t i;

    for(i = 0; i < len; i++) *q++ = invtab[*q];

}

static int encode_magazine_packet_number(uint8_t *q, uint8_t mag, uint8_t nr)
{
    uint8_t m, n;

    m = (mag | ((nr & 0x01) << 3)) & 0x0f;
    n = (nr >> 1) & 0x0f;

    *q++ = ham84tab[m];
    *q++ = ham84tab[n];

    return 2;
}
 
static int encode_page_header(uint8_t *q, uint8_t* pageno, uint8_t *subcode, uint8_t* ctrlbits)
{
    *q++ = ham84tab[pageno[0] & 0x0f];
    *q++ = ham84tab[pageno[1] & 0x0f];

    *q++ = ham84tab[subcode[0] & 0x0f];
    *q++ = ham84tab[(subcode[1] | (ctrlbits[0] << 3)) & 0x0f];      // C4
    *q++ = ham84tab[subcode[2] & 0x0f];
    *q++ = ham84tab[(subcode[3] | (ctrlbits[1] << 2)) & 0x0f];      // C5 c6

    *q++ = ham84tab[ctrlbits[2] & 0x0f];                            // C7 C8 C9 C10
    *q++ = ham84tab[ctrlbits[3] & 0x0f];                            // C11 C12 C13 C14

    return 8;
}

static int encode_page_header_data(uint8_t *q, TexAttr *attr, uint8_t *buff, uint8_t len)
{
    uint8_t i, j, charlen = 0;

    *q++ = paritytab[buff[0]];
    *q++ = paritytab[buff[1]];
    *q++ = paritytab[buff[2]];
    *q++ = paritytab[0x20];
    *q++ = paritytab[0x07];

    charlen = len - 5;

    charlen = FFMIN(strlen(buff) - 3, charlen);

    for(i = 5, j = 3; i < len; i++) {
        if(i >= charlen) *q++ = paritytab[0x20];
        else             *q++ = paritytab[buff[j++]];
    }
 
    return len;
}

static int encode_data_block(uint8_t *q, TexAttr *attr, uint8_t *buff, uint8_t len)
{
    uint8_t i, charlen = 0, clrlen = 0;

    charlen = len;

    if(len < 40) {
        *q++ = paritytab[attr->bk_color];
        *q++ = paritytab[0x1d];
        charlen -= 2;
        clrlen +=2;
    }

    *q++ = paritytab[attr->ft_color];

    charlen -= 1;
    clrlen += 1;

    charlen = FFMIN(strlen(buff), charlen);

    for(i = 0; i < len - clrlen; i++) {
        if(i >= charlen) *q++ = paritytab[0x20];
        else             *q++ = paritytab[buff[i]];
    }
 
    return len;
}

static int encode_dvb_teletext(DVBSubtitleContext *s, uint8_t *outbuf, AVSubtitle *h)
{
    int i = 0, head_len = 0;
    static int m = 0;
    uint8_t *q;
    uint8_t rfl = 0x27;
    int mag, nr;
    int rows;
    int padding = 0, len, size;
    static uint8_t pageno[2], subcode[4], ctrlbits[4];
    static uint32_t ncount = 0;

    q = outbuf;
    
    *q++ = 0x10; // data identifier
    
    rows = FFMIN(h->num_rects, 24);

    subcode[0] = 0;
    subcode[0] %= 16;
    subcode[1] = 0;
    subcode[1] %= 8;
    subcode[2] = 0;
    subcode[2] %= 16;
    subcode[3] = 0;
    subcode[3] %= 4;

    ctrlbits[0] = 0x00;          // C4, Erase Page
    ctrlbits[1] = 0x00;          // C5, C6
    ctrlbits[2] = 0x04;          // C7, C8, C9, C10
    ctrlbits[3] = 0x0d;          // C11, C12, C13, C14, character subset, italian

    for(i = 0; i < rows; i++) {
        char *text;
        TexAttr attr;

        head_len = get_head_len(h->rects[i]->ass);
        if(head_len < 0) return -1;

        text = &h->rects[23]->ass[head_len];

        *q++ = 0x03;                      // data unit id
        *q++ = 0x2c;                      // data unit length
        *q++ = rfl + (i % 9);             // 2 reserved, 1 field_parity, 5 line_offset
        *q++ = 0xe4;                      // frame coding

        mag       = text[0] - 0x30;
        pageno[1] = text[1] - 0x30;
        pageno[0] = text[2] - 0x30;

        if((ncount % 3) == 1)  mag = 4;
        else                   mag = 6;

        nr = (i + 1) % 25;                // max 31, for simple, now take 23;

        if(i == 23) {
             nr = 0;
        }

        attr.bk_color = 0x04;
        attr.ft_color = 0x07;

        if((ncount % 3) == 0 && i == 23)  {
            text[0] = '4';
            mag = 4;
        }

        if((ncount % 3) == 1 && i == 23)  {
            text[0] = '6';
            mag = 6;
        }

        if((ncount % 3) == 1 && i == 23)  {
            text[0] = '5';
            mag = 5;
        }

        q += encode_magazine_packet_number(q, mag, nr);  // i-th, i+1-th byte, magazine 2,4,6,8

        if(i == 23) {
            q += encode_page_header(q, pageno, subcode, ctrlbits);
        }

        q += encode_data_block(q, &attr, (uint8_t *)&h->rects[i]->ass[head_len], i == 23 ? 32 : 40);

        invert_data(q - 42, 42);
    }

    ncount++;

    len = 188 - 50;
    size = q - &outbuf[1];

    if(size < len) padding = len - size;
    else if(size > len) {
        len = size - len;
        padding = 184 - len % 184;
        if(padding == 184) padding = 0;
    }

    for(size = padding; size > 0; size -= 46) {
        *q++ = 0xff;
        *q++ = 0x2c;
        memset(q, 0xff, 44);
        q += 44;
    }

    return q - outbuf;
}

static int dvbtex_encode(AVCodecContext *avctx,
        unsigned char *buf, int buf_size, void *data)
{
    DVBSubtitleContext *s = avctx->priv_data;
    AVSubtitle *sub = data;
    int encoderet = 0;

    encoderet = encode_dvb_teletext(s, buf, sub);
   
    return encoderet;
}

static av_cold int dvbtex_encoder_init(AVCodecContext *avctx)
{
    return 0;
}

#define OFFSET(x) offsetof(DVBSubtitleContext, x)

static const AVOption options[] = {
    { NULL },
};

static const AVClass libdvbtex_class = {
    .class_name = "dvbtex encoder",
    .item_name  = av_default_item_name,
    .option     = options,
    .version    = LIBAVUTIL_VERSION_INT,
};

AVCodec ff_dvbtex_encoder = {
    .name           = "dvbteletext",
    .type           = AVMEDIA_TYPE_SUBTITLE,
    .id             = CODEC_ID_DVB_TELETEXT,
    .priv_data_size = sizeof(DVBSubtitleContext),
    .init           = dvbtex_encoder_init,
    .encode         = dvbtex_encode,
    .long_name      = NULL_IF_CONFIG_SMALL("DVB teletext"),
    .priv_class     = &libdvbtex_class,
};


你可能感兴趣的:(c,struct,image,header,Class,colors)