This source file includes following definitions.
- dvb_encode_rle2
- dvb_encode_rle4
- dvb_encode_rle8
- encode_dvb_subtitles
- dvbsub_encode
#include "avcodec.h"
#include "bytestream.h"
#include "libavutil/colorspace.h"
typedef struct DVBSubtitleContext {
int object_version;
} DVBSubtitleContext;
#define PUTBITS2(val)\
{\
bitbuf |= (val) << bitcnt;\
bitcnt -= 2;\
if (bitcnt < 0) {\
bitcnt = 6;\
*q++ = bitbuf;\
bitbuf = 0;\
}\
}
static void dvb_encode_rle2(uint8_t **pq,
const uint8_t *bitmap, int linesize,
int w, int h)
{
uint8_t *q;
unsigned int bitbuf;
int bitcnt;
int x, y, len, x1, v, color;
q = *pq;
for(y = 0; y < h; y++) {
*q++ = 0x10;
bitbuf = 0;
bitcnt = 6;
x = 0;
while (x < w) {
x1 = x;
color = bitmap[x1++];
while (x1 < w && bitmap[x1] == color)
x1++;
len = x1 - x;
if (color == 0 && len == 2) {
PUTBITS2(0);
PUTBITS2(0);
PUTBITS2(1);
} else if (len >= 3 && len <= 10) {
v = len - 3;
PUTBITS2(0);
PUTBITS2((v >> 2) | 2);
PUTBITS2(v & 3);
PUTBITS2(color);
} else if (len >= 12 && len <= 27) {
v = len - 12;
PUTBITS2(0);
PUTBITS2(0);
PUTBITS2(2);
PUTBITS2(v >> 2);
PUTBITS2(v & 3);
PUTBITS2(color);
} else if (len >= 29) {
if (len > 284)
len = 284;
v = len - 29;
PUTBITS2(0);
PUTBITS2(0);
PUTBITS2(3);
PUTBITS2((v >> 6));
PUTBITS2((v >> 4) & 3);
PUTBITS2((v >> 2) & 3);
PUTBITS2(v & 3);
PUTBITS2(color);
} else {
PUTBITS2(color);
if (color == 0) {
PUTBITS2(1);
}
len = 1;
}
x += len;
}
PUTBITS2(0);
PUTBITS2(0);
PUTBITS2(0);
if (bitcnt != 6) {
*q++ = bitbuf;
}
*q++ = 0xf0;
bitmap += linesize;
}
*pq = q;
}
#define PUTBITS4(val)\
{\
bitbuf |= (val) << bitcnt;\
bitcnt -= 4;\
if (bitcnt < 0) {\
bitcnt = 4;\
*q++ = bitbuf;\
bitbuf = 0;\
}\
}
static void dvb_encode_rle4(uint8_t **pq,
const uint8_t *bitmap, int linesize,
int w, int h)
{
uint8_t *q;
unsigned int bitbuf;
int bitcnt;
int x, y, len, x1, v, color;
q = *pq;
for(y = 0; y < h; y++) {
*q++ = 0x11;
bitbuf = 0;
bitcnt = 4;
x = 0;
while (x < w) {
x1 = x;
color = bitmap[x1++];
while (x1 < w && bitmap[x1] == color)
x1++;
len = x1 - x;
if (color == 0 && len == 2) {
PUTBITS4(0);
PUTBITS4(0xd);
} else if (color == 0 && (len >= 3 && len <= 9)) {
PUTBITS4(0);
PUTBITS4(len - 2);
} else if (len >= 4 && len <= 7) {
PUTBITS4(0);
PUTBITS4(8 + len - 4);
PUTBITS4(color);
} else if (len >= 9 && len <= 24) {
PUTBITS4(0);
PUTBITS4(0xe);
PUTBITS4(len - 9);
PUTBITS4(color);
} else if (len >= 25) {
if (len > 280)
len = 280;
v = len - 25;
PUTBITS4(0);
PUTBITS4(0xf);
PUTBITS4(v >> 4);
PUTBITS4(v & 0xf);
PUTBITS4(color);
} else {
PUTBITS4(color);
if (color == 0) {
PUTBITS4(0xc);
}
len = 1;
}
x += len;
}
PUTBITS4(0);
PUTBITS4(0);
if (bitcnt != 4) {
*q++ = bitbuf;
}
*q++ = 0xf0;
bitmap += linesize;
}
*pq = q;
}
static void dvb_encode_rle8(uint8_t **pq,
const uint8_t *bitmap, int linesize,
int w, int h)
{
uint8_t *q;
int x, y, len, x1, color;
q = *pq;
for (y = 0; y < h; y++) {
*q++ = 0x12;
x = 0;
while (x < w) {
x1 = x;
color = bitmap[x1++];
while (x1 < w && bitmap[x1] == color)
x1++;
len = x1 - x;
if (len == 1 && color) {
*q++ = color;
} else {
if (color == 0x00) {
len = FFMIN(len, 127);
*q++ = 0x00;
*q++ = len;
} else if (len > 2) {
len = FFMIN(len, 127);
*q++ = 0x00;
*q++ = 0x80+len;
*q++ = color;
}
else if (len == 2) {
*q++ = color;
*q++ = color;
} else {
*q++ = color;
len = 1;
}
}
x += len;
}
*q++ = 0x00;
*q++ = 0x00;
bitmap += linesize;
}
*pq = q;
}
static int encode_dvb_subtitles(DVBSubtitleContext *s,
uint8_t *outbuf, const AVSubtitle *h)
{
uint8_t *q, *pseg_len;
int page_id, region_id, clut_id, object_id, i, bpp_index, page_state;
q = outbuf;
page_id = 1;
if (h->num_rects && !h->rects)
return -1;
*q++ = 0x0f;
*q++ = 0x10;
bytestream_put_be16(&q, page_id);
pseg_len = q;
q += 2;
*q++ = 30;
page_state = 2;
*q++ = (s->object_version << 4) | (page_state << 2) | 3;
for (region_id = 0; region_id < h->num_rects; region_id++) {
*q++ = region_id;
*q++ = 0xff;
bytestream_put_be16(&q, h->rects[region_id]->x);
bytestream_put_be16(&q, h->rects[region_id]->y);
}
bytestream_put_be16(&pseg_len, q - pseg_len - 2);
if (h->num_rects) {
for (clut_id = 0; clut_id < h->num_rects; clut_id++) {
if (h->rects[clut_id]->nb_colors <= 4) {
bpp_index = 0;
} else if (h->rects[clut_id]->nb_colors <= 16) {
bpp_index = 1;
} else if (h->rects[clut_id]->nb_colors <= 256) {
bpp_index = 2;
} else {
return -1;
}
*q++ = 0x0f;
*q++ = 0x12;
bytestream_put_be16(&q, page_id);
pseg_len = q;
q += 2;
*q++ = clut_id;
*q++ = (0 << 4) | 0xf;
for(i = 0; i < h->rects[clut_id]->nb_colors; i++) {
*q++ = i;
*q++ = (1 << (7 - bpp_index)) | (0xf << 1) | 1;
{
int a, r, g, b;
uint32_t x= ((uint32_t*)h->rects[clut_id]->pict.data[1])[i];
a = (x >> 24) & 0xff;
r = (x >> 16) & 0xff;
g = (x >> 8) & 0xff;
b = (x >> 0) & 0xff;
*q++ = RGB_TO_Y_CCIR(r, g, b);
*q++ = RGB_TO_V_CCIR(r, g, b, 0);
*q++ = RGB_TO_U_CCIR(r, g, b, 0);
*q++ = 255 - a;
}
}
bytestream_put_be16(&pseg_len, q - pseg_len - 2);
}
}
for (region_id = 0; region_id < h->num_rects; region_id++) {
if (h->rects[region_id]->nb_colors <= 4) {
bpp_index = 0;
} else if (h->rects[region_id]->nb_colors <= 16) {
bpp_index = 1;
} else {
return -1;
}
*q++ = 0x0f;
*q++ = 0x11;
bytestream_put_be16(&q, page_id);
pseg_len = q;
q += 2;
*q++ = region_id;
*q++ = (s->object_version << 4) | (0 << 3) | 0x07;
bytestream_put_be16(&q, h->rects[region_id]->w);
bytestream_put_be16(&q, h->rects[region_id]->h);
*q++ = ((1 + bpp_index) << 5) | ((1 + bpp_index) << 2) | 0x03;
*q++ = region_id;
*q++ = 0;
*q++ = 0x03;
bytestream_put_be16(&q, region_id);
*q++ = (0 << 6) | (0 << 4);
*q++ = 0;
*q++ = 0xf0;
*q++ = 0;
bytestream_put_be16(&pseg_len, q - pseg_len - 2);
}
if (h->num_rects) {
for (object_id = 0; object_id < h->num_rects; object_id++) {
void (*dvb_encode_rle)(uint8_t **pq,
const uint8_t *bitmap, int linesize,
int w, int h);
if (h->rects[object_id]->nb_colors <= 4) {
dvb_encode_rle = dvb_encode_rle2;
} else if (h->rects[object_id]->nb_colors <= 16) {
dvb_encode_rle = dvb_encode_rle4;
} else if (h->rects[object_id]->nb_colors <= 256) {
dvb_encode_rle = dvb_encode_rle8;
} else {
return -1;
}
*q++ = 0x0f;
*q++ = 0x13;
bytestream_put_be16(&q, page_id);
pseg_len = q;
q += 2;
bytestream_put_be16(&q, object_id);
*q++ = (s->object_version << 4) | (0 << 2) | (0 << 1) | 1;
{
uint8_t *ptop_field_len, *pbottom_field_len, *top_ptr, *bottom_ptr;
ptop_field_len = q;
q += 2;
pbottom_field_len = q;
q += 2;
top_ptr = q;
dvb_encode_rle(&q, h->rects[object_id]->pict.data[0], h->rects[object_id]->w * 2,
h->rects[object_id]->w, h->rects[object_id]->h >> 1);
bottom_ptr = q;
dvb_encode_rle(&q, h->rects[object_id]->pict.data[0] + h->rects[object_id]->w,
h->rects[object_id]->w * 2, h->rects[object_id]->w,
h->rects[object_id]->h >> 1);
bytestream_put_be16(&ptop_field_len, bottom_ptr - top_ptr);
bytestream_put_be16(&pbottom_field_len, q - bottom_ptr);
}
bytestream_put_be16(&pseg_len, q - pseg_len - 2);
}
}
*q++ = 0x0f;
*q++ = 0x80;
bytestream_put_be16(&q, page_id);
pseg_len = q;
q += 2;
bytestream_put_be16(&pseg_len, q - pseg_len - 2);
s->object_version = (s->object_version + 1) & 0xf;
return q - outbuf;
}
static int dvbsub_encode(AVCodecContext *avctx,
unsigned char *buf, int buf_size,
const AVSubtitle *sub)
{
DVBSubtitleContext *s = avctx->priv_data;
int ret;
ret = encode_dvb_subtitles(s, buf, sub);
return ret;
}
AVCodec ff_dvbsub_encoder = {
.name = "dvbsub",
.long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),
.type = AVMEDIA_TYPE_SUBTITLE,
.id = AV_CODEC_ID_DVB_SUBTITLE,
.priv_data_size = sizeof(DVBSubtitleContext),
.encode_sub = dvbsub_encode,
};