27 #define DVBSUB_PAGE_SEGMENT 0x10
28 #define DVBSUB_REGION_SEGMENT 0x11
29 #define DVBSUB_CLUT_SEGMENT 0x12
30 #define DVBSUB_OBJECT_SEGMENT 0x13
31 #define DVBSUB_DISPLAYDEFINITION_SEGMENT 0x14
32 #define DVBSUB_DISPLAY_SEGMENT 0x80
34 #define cm (ff_crop_tab + MAX_NEG_CROP)
38 static void png_save(
const char *filename,
uint8_t *bitmap,
int w,
int h,
39 uint32_t *rgba_palette)
43 char fname[40], fname2[40];
46 snprintf(fname, 40,
"%s.ppm", filename);
48 f = fopen(fname,
"w");
57 for(y = 0; y < h; y++) {
58 for(x = 0; x < w; x++) {
59 v = rgba_palette[bitmap[y * w + x]];
60 putc((v >> 16) & 0xff, f);
61 putc((v >> 8) & 0xff, f);
62 putc((v >> 0) & 0xff, f);
68 snprintf(fname2, 40,
"%s-a.pgm", filename);
70 f = fopen(fname2,
"w");
79 for(y = 0; y < h; y++) {
80 for(x = 0; x < w; x++) {
81 v = rgba_palette[bitmap[y * w + x]];
82 putc((v >> 24) & 0xff, f);
87 snprintf(command, 1024,
"pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
90 snprintf(command, 1024,
"rm %s %s", fname, fname2);
95 static void png_save2(
const char *filename, uint32_t *bitmap,
int w,
int h)
99 char fname[40], fname2[40];
102 snprintf(fname,
sizeof(fname),
"%s.ppm", filename);
104 f = fopen(fname,
"w");
113 for(y = 0; y < h; y++) {
114 for(x = 0; x < w; x++) {
115 v = bitmap[y * w + x];
116 putc((v >> 16) & 0xff, f);
117 putc((v >> 8) & 0xff, f);
118 putc((v >> 0) & 0xff, f);
124 snprintf(fname2,
sizeof(fname2),
"%s-a.pgm", filename);
126 f = fopen(fname2,
"w");
135 for(y = 0; y < h; y++) {
136 for(x = 0; x < w; x++) {
137 v = bitmap[y * w + x];
138 putc((v >> 24) & 0xff, f);
143 snprintf(command,
sizeof(command),
"pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
146 snprintf(command,
sizeof(command),
"rm %s %s", fname, fname2);
151 #define RGBA(r,g,b,a) (((unsigned)(a) << 24) | ((r) << 16) | ((g) << 8) | (b))
244 while (ptr && ptr->
id != object_id) {
255 while (ptr && ptr->
id != clut_id) {
266 while (ptr && ptr->
id != region_id) {
284 obj_disp_ptr = &
object->display_list;
285 obj_disp = *obj_disp_ptr;
287 while (obj_disp && obj_disp != display) {
289 obj_disp = *obj_disp_ptr;
299 while (obj2 !=
object) {
301 obj2_ptr = &obj2->
next;
305 *obj2_ptr = obj2->
next;
351 int i,
r,
g,
b,
a = 0;
363 default_clut.
id = -1;
366 default_clut.
clut4[0] =
RGBA( 0, 0, 0, 0);
367 default_clut.
clut4[1] =
RGBA(255, 255, 255, 255);
368 default_clut.
clut4[2] =
RGBA( 0, 0, 0, 255);
369 default_clut.
clut4[3] =
RGBA(127, 127, 127, 255);
372 for (i = 1; i < 16; i++) {
374 r = (i & 1) ? 255 : 0;
375 g = (i & 2) ? 255 : 0;
376 b = (i & 4) ? 255 : 0;
378 r = (i & 1) ? 127 : 0;
379 g = (i & 2) ? 127 : 0;
380 b = (i & 4) ? 127 : 0;
386 for (i = 1; i < 256; i++) {
388 r = (i & 1) ? 255 : 0;
389 g = (i & 2) ? 255 : 0;
390 b = (i & 4) ? 255 : 0;
395 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
396 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
397 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
401 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
402 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
403 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
407 r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
408 g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
409 b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
413 r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
414 g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
415 b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
444 const uint8_t **srcbuf,
int buf_size,
445 int non_mod,
uint8_t *map_table)
455 while (
get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
459 if (non_mod != 1 || bits != 1) {
461 *destbuf++ = map_table[
bits];
472 if (non_mod == 1 && bits == 1)
473 pixels_read += run_length;
476 bits = map_table[
bits];
477 while (run_length-- > 0 && pixels_read < dbuf_len) {
490 if (non_mod == 1 && bits == 1)
491 pixels_read += run_length;
494 bits = map_table[
bits];
495 while (run_length-- > 0 && pixels_read < dbuf_len) {
500 }
else if (bits == 3) {
504 if (non_mod == 1 && bits == 1)
505 pixels_read += run_length;
508 bits = map_table[
bits];
509 while (run_length-- > 0 && pixels_read < dbuf_len) {
514 }
else if (bits == 1) {
520 if (pixels_read <= dbuf_len) {
549 const uint8_t **srcbuf,
int buf_size,
550 int non_mod,
uint8_t *map_table)
560 while (
get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
564 if (non_mod != 1 || bits != 1) {
566 *destbuf++ = map_table[
bits];
576 if (run_length == 0) {
588 while (run_length-- > 0 && pixels_read < dbuf_len) {
598 if (non_mod == 1 && bits == 1)
599 pixels_read += run_length;
602 bits = map_table[
bits];
603 while (run_length-- > 0 && pixels_read < dbuf_len) {
614 if (non_mod == 1 && bits == 1)
615 pixels_read += run_length;
618 bits = map_table[
bits];
619 while (run_length-- > 0 && pixels_read < dbuf_len) {
624 }
else if (bits == 3) {
628 if (non_mod == 1 && bits == 1)
629 pixels_read += run_length;
632 bits = map_table[
bits];
633 while (run_length-- > 0 && pixels_read < dbuf_len) {
638 }
else if (bits == 1) {
644 if (pixels_read <= dbuf_len) {
670 const uint8_t **srcbuf,
int buf_size,
671 int non_mod,
uint8_t *map_table)
673 const uint8_t *sbuf_end = (*srcbuf) + buf_size;
678 while (*srcbuf < sbuf_end && pixels_read < dbuf_len) {
682 if (non_mod != 1 || bits != 1) {
684 *destbuf++ = map_table[
bits];
691 run_length = bits & 0x7f;
692 if ((bits & 0x80) == 0) {
693 if (run_length == 0) {
701 while (run_length-- > 0 && pixels_read < dbuf_len) {
708 if (non_mod == 1 && bits == 1)
709 pixels_read += run_length;
711 bits = map_table[
bits];
712 else while (run_length-- > 0 && pixels_read < dbuf_len) {
729 const uint8_t *buf,
int buf_size,
int top_bottom,
int non_mod)
734 const uint8_t *buf_end = buf + buf_size;
739 uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
740 uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
741 uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
742 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
745 av_dlog(avctx,
"DVB pixel block size %d, %s field:\n", buf_size,
746 top_bottom ?
"bottom" :
"top");
748 for (i = 0; i < buf_size; i++) {
750 av_dlog(avctx,
"0x%8p: ", buf+i);
752 av_dlog(avctx,
"%02x ", buf[i]);
765 x_pos = display->
x_pos;
766 y_pos = display->
y_pos;
768 if ((y_pos & 1) != top_bottom)
771 while (buf < buf_end) {
772 if (x_pos > region->
width || y_pos > region->
height) {
779 if (region->
depth == 8)
781 else if (region->
depth == 4)
787 region->
width - x_pos, &buf, buf_end - buf,
791 if (region->
depth < 4) {
796 if (region->
depth == 8)
802 region->
width - x_pos, &buf, buf_end - buf,
806 if (region->
depth < 8) {
812 region->
width - x_pos, &buf, buf_end - buf,
817 map2to4[0] = (*buf) >> 4;
818 map2to4[1] = (*buf++) & 0xf;
819 map2to4[2] = (*buf) >> 4;
820 map2to4[3] = (*buf++) & 0xf;
823 for (i = 0; i < 4; i++)
827 for (i = 0; i < 16; i++)
832 x_pos = display->
x_pos;
843 const uint8_t *buf,
int buf_size)
847 const uint8_t *buf_end = buf + buf_size;
852 int top_field_len, bottom_field_len;
854 int coding_method, non_modifying_color;
864 coding_method = ((*buf) >> 2) & 3;
865 non_modifying_color = ((*buf++) >> 1) & 1;
867 if (coding_method == 0) {
870 bottom_field_len =
AV_RB16(buf);
873 if (buf + top_field_len + bottom_field_len > buf_end) {
882 non_modifying_color);
884 if (bottom_field_len > 0)
885 block = buf + top_field_len;
887 bottom_field_len = top_field_len;
890 non_modifying_color);
902 const uint8_t *buf,
int buf_size)
906 const uint8_t *buf_end = buf + buf_size;
909 int entry_id, depth , full_range;
910 int y, cr, cb, alpha;
911 int r,
g,
b, r_add, g_add, b_add;
913 av_dlog(avctx,
"DVB clut packet:\n");
915 for (i=0; i < buf_size; i++) {
916 av_dlog(avctx,
"%02x ", buf[i]);
932 memcpy(clut, &default_clut,
sizeof(
DVBSubCLUT));
940 while (buf + 4 < buf_end) {
943 depth = (*buf) & 0xe0;
950 full_range = (*buf++) & 1;
959 cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
960 cb = (buf[1] << 2) & 0xf0;
961 alpha = (buf[1] << 6) & 0xc0;
972 av_dlog(avctx,
"clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
975 clut->
clut4[entry_id] =
RGBA(r,g,b,255 - alpha);
977 clut->
clut16[entry_id] =
RGBA(r,g,b,255 - alpha);
985 const uint8_t *buf,
int buf_size)
989 const uint8_t *buf_end = buf + buf_size;
990 int region_id, object_id;
1006 region->
id = region_id;
1012 fill = ((*buf++) >> 3) & 1;
1029 region->
depth = 1 << (((*buf++) >> 2) & 7);
1034 region->
clut = *buf++;
1036 if (region->
depth == 8)
1041 if (region->
depth == 4)
1042 region->
bgcolor = (((*buf++) >> 4) & 15);
1044 region->
bgcolor = (((*buf++) >> 2) & 3);
1056 while (buf + 5 < buf_end) {
1065 object->id = object_id;
1070 object->
type = (*buf) >> 6;
1082 if ((object->
type == 1 || object->
type == 2) && buf+1 < buf_end) {
1091 object->display_list = display;
1096 const uint8_t *buf,
int buf_size)
1102 const uint8_t *buf_end = buf + buf_size;
1110 page_state = ((*buf++) >> 2) & 3;
1112 av_dlog(avctx,
"Page time out %ds, state %d\n", ctx->
time_out, page_state);
1114 if (page_state == 2) {
1122 while (buf + 5 < buf_end) {
1126 display = tmp_display_list;
1127 tmp_ptr = &tmp_display_list;
1129 while (display && display->
region_id != region_id) {
1130 tmp_ptr = &display->
next;
1131 display = display->
next;
1144 *tmp_ptr = display->
next;
1150 av_dlog(avctx,
"Region %d, (%d,%d)\n", region_id, display->
x_pos, display->
y_pos);
1153 while (tmp_display_list) {
1154 display = tmp_display_list;
1156 tmp_display_list = display->
next;
1170 uint32_t *clut_table;
1172 int x, y, y_off, x_off;
1175 static int fileno_index = 0;
1186 x_pos = display->
x_pos;
1187 y_pos = display->
y_pos;
1188 width = region->
width;
1191 if (display->
x_pos < x_pos) {
1192 width += (x_pos - display->
x_pos);
1193 x_pos = display->
x_pos;
1196 if (display->
y_pos < y_pos) {
1197 height += (y_pos - display->
y_pos);
1198 y_pos = display->
y_pos;
1201 if (display->
x_pos + region->
width > x_pos + width) {
1202 width = display->
x_pos + region->
width - x_pos;
1205 if (display->
y_pos + region->
height > y_pos + height) {
1218 x_off = display->
x_pos - x_pos;
1219 y_off = display->
y_pos - y_pos;
1226 switch (region->
depth) {
1228 clut_table = clut->
clut4;
1235 clut_table = clut->
clut16;
1239 for (y = 0; y < region->
height; y++) {
1240 for (x = 0; x < region->
width; x++) {
1241 pbuf[((y + y_off) * width) + x_off + x] =
1242 clut_table[region->
pbuf[y * region->
width + x]];
1248 snprintf(filename,
sizeof(filename),
"dvbs.%d", fileno_index);
1250 png_save2(filename, pbuf, width, height);
1265 int dds_version, info_byte;
1270 info_byte = bytestream_get_byte(&buf);
1271 dds_version = info_byte >> 4;
1272 if (display_def && display_def->
version == dds_version)
1276 display_def =
av_mallocz(
sizeof(*display_def));
1282 display_def->
version = dds_version;
1285 display_def->
width = bytestream_get_be16(&buf) + 1;
1286 display_def->
height = bytestream_get_be16(&buf) + 1;
1291 if (info_byte & 1<<3) {
1292 display_def->
x = bytestream_get_be16(&buf);
1293 display_def->
y = bytestream_get_be16(&buf);
1294 display_def->
width = bytestream_get_be16(&buf) - display_def->
x + 1;
1295 display_def->
height = bytestream_get_be16(&buf) - display_def->
y + 1;
1309 uint32_t *clut_table;
1311 int offset_x=0, offset_y=0;
1319 offset_x = display_def->
x;
1320 offset_y = display_def->
y;
1335 rect = sub->
rects[i];
1340 rect->
x = display->
x_pos + offset_x;
1341 rect->
y = display->
y_pos + offset_y;
1353 switch (region->
depth) {
1355 clut_table = clut->
clut4;
1362 clut_table = clut->
clut16;
1367 memcpy(rect->
pict.
data[1], clut_table, (1 << region->
depth) *
sizeof(uint32_t));
1378 save_display_set(ctx);
1385 void *
data,
int *data_size,
1389 int buf_size = avpkt->
size;
1398 av_dlog(avctx,
"DVB sub packet:\n");
1400 for (i=0; i < buf_size; i++) {
1401 av_dlog(avctx,
"%02x ", buf[i]);
1409 if (buf_size <= 6 || *buf != 0x0f) {
1410 av_dlog(avctx,
"incomplete or broken packet");
1415 p_end = buf + buf_size;
1417 while (p_end - p >= 6 && *p == 0x0f) {
1419 segment_type = *p++;
1425 if (p_end - p < segment_length) {
1426 av_dlog(avctx,
"incomplete or broken packet");
1432 switch (segment_type) {
1451 av_dlog(avctx,
"Subtitling segment type 0x%x, page id %d, length %d\n",
1452 segment_type, page_id, segment_length);
1457 p += segment_length;
static DVBSubCLUT * get_clut(DVBSubContext *ctx, int clut_id)
static void dvbsub_parse_clut_segment(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display, const uint8_t *buf, int buf_size, int top_bottom, int non_mod)
int x
top left corner of pict, undefined when pict is not set
static DVBSubRegion * get_region(DVBSubContext *ctx, int region_id)
static int dvbsub_display_end_segment(AVCodecContext *avctx, const uint8_t *buf, int buf_size, AVSubtitle *sub)
DVBSubRegionDisplay * display_list
int linesize[AV_NUM_DATA_POINTERS]
number of bytes per line
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define YUV_TO_RGB1_CCIR(cb1, cr1)
static void dvbsub_parse_region_segment(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
int nb_colors
number of colors in pict, undefined when pict is not set
Various defines for YUV<->RGB conversion.
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
static void dvbsub_parse_page_segment(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
int w
width of pict, undefined when pict is not set
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
const char * name
Name of the codec implementation.
static int get_bits_count(const GetBitContext *s)
static int dvbsub_read_2bit_string(uint8_t *destbuf, int dbuf_len, const uint8_t **srcbuf, int buf_size, int non_mod, uint8_t *map_table)
bitstream reader API header.
static void delete_state(DVBSubContext *ctx)
int h
height of pict, undefined when pict is not set
DVBSubRegion * region_list
uint8_t * data[AV_NUM_DATA_POINTERS]
#define DVBSUB_DISPLAYDEFINITION_SEGMENT
struct DVBSubRegion * next
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
int y
top left corner of pict, undefined when pict is not set
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static DVBSubCLUT default_clut
Libavcodec external API header.
uint32_t end_display_time
static void dvbsub_parse_display_definition_segment(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
struct DVBSubObject * next
AVCodec ff_dvbsub_decoder
A bitmap, pict will be set.
AVPicture pict
data+linesize for the bitmap of this subtitle.
DVBSubObjectDisplay * display_list
struct DVBSubObjectDisplay * region_list_next
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
DVBSubObjectDisplay * display_list
DVBSubDisplayDefinition * display_definition
#define AV_LOG_INFO
Standard information.
#define DVBSUB_CLUT_SEGMENT
main external API structure.
static void close(AVCodecParserContext *s)
static DVBSubObject * get_object(DVBSubContext *ctx, int object_id)
static unsigned int get_bits1(GetBitContext *s)
static void delete_region_display_list(DVBSubContext *ctx, DVBSubRegion *region)
#define DVBSUB_DISPLAY_SEGMENT
struct DVBSubRegionDisplay * next
#define YUV_TO_RGB2_CCIR(r, g, b, y1)
struct DVBSubObjectDisplay * object_list_next
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
#define DVBSUB_PAGE_SEGMENT
static av_cold int dvbsub_init_decoder(AVCodecContext *avctx)
#define DVBSUB_REGION_SEGMENT
uint32_t start_display_time
static int dvbsub_decode(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
static av_cold int init(AVCodecParserContext *s)
static int dvbsub_read_4bit_string(uint8_t *destbuf, int dbuf_len, const uint8_t **srcbuf, int buf_size, int non_mod, uint8_t *map_table)
static void dvbsub_parse_object_segment(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
DVBSubObject * object_list
static int dvbsub_read_8bit_string(uint8_t *destbuf, int dbuf_len, const uint8_t **srcbuf, int buf_size, int non_mod, uint8_t *map_table)
#define DVBSUB_OBJECT_SEGMENT
static av_cold int dvbsub_close_decoder(AVCodecContext *avctx)
This structure stores compressed data.
void * av_mallocz(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...