2 * DVB subtitle decoding
3 * Copyright (c) 2005 Ian Caulfield
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "bytestream.h"
25 #include "libavutil/colorspace.h"
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))
153 typedef struct DVBSubCLUT {
158 uint32_t clut256[256];
160 struct DVBSubCLUT *next;
163 static DVBSubCLUT default_clut;
165 typedef struct DVBSubObjectDisplay {
175 struct DVBSubObjectDisplay *region_list_next;
176 struct DVBSubObjectDisplay *object_list_next;
177 } DVBSubObjectDisplay;
179 typedef struct DVBSubObject {
184 DVBSubObjectDisplay *display_list;
186 struct DVBSubObject *next;
189 typedef struct DVBSubRegionDisplay {
195 struct DVBSubRegionDisplay *next;
196 } DVBSubRegionDisplay;
198 typedef struct DVBSubRegion {
211 DVBSubObjectDisplay *display_list;
213 struct DVBSubRegion *next;
216 typedef struct DVBSubDisplayDefinition {
223 } DVBSubDisplayDefinition;
225 typedef struct DVBSubContext {
230 DVBSubRegion *region_list;
231 DVBSubCLUT *clut_list;
232 DVBSubObject *object_list;
234 int display_list_size;
235 DVBSubRegionDisplay *display_list;
236 DVBSubDisplayDefinition *display_definition;
240 static DVBSubObject* get_object(DVBSubContext *ctx, int object_id)
242 DVBSubObject *ptr = ctx->object_list;
244 while (ptr && ptr->id != object_id) {
251 static DVBSubCLUT* get_clut(DVBSubContext *ctx, int clut_id)
253 DVBSubCLUT *ptr = ctx->clut_list;
255 while (ptr && ptr->id != clut_id) {
262 static DVBSubRegion* get_region(DVBSubContext *ctx, int region_id)
264 DVBSubRegion *ptr = ctx->region_list;
266 while (ptr && ptr->id != region_id) {
273 static void delete_region_display_list(DVBSubContext *ctx, DVBSubRegion *region)
275 DVBSubObject *object, *obj2, **obj2_ptr;
276 DVBSubObjectDisplay *display, *obj_disp, **obj_disp_ptr;
278 while (region->display_list) {
279 display = region->display_list;
281 object = get_object(ctx, display->object_id);
284 obj_disp_ptr = &object->display_list;
285 obj_disp = *obj_disp_ptr;
287 while (obj_disp && obj_disp != display) {
288 obj_disp_ptr = &obj_disp->object_list_next;
289 obj_disp = *obj_disp_ptr;
293 *obj_disp_ptr = obj_disp->object_list_next;
295 if (!object->display_list) {
296 obj2_ptr = &ctx->object_list;
299 while (obj2 != object) {
301 obj2_ptr = &obj2->next;
305 *obj2_ptr = obj2->next;
312 region->display_list = display->region_list_next;
319 static void delete_state(DVBSubContext *ctx)
321 DVBSubRegion *region;
324 while (ctx->region_list) {
325 region = ctx->region_list;
327 ctx->region_list = region->next;
329 delete_region_display_list(ctx, region);
330 av_free(region->pbuf);
334 while (ctx->clut_list) {
335 clut = ctx->clut_list;
337 ctx->clut_list = clut->next;
342 av_freep(&ctx->display_definition);
344 /* Should already be null */
345 if (ctx->object_list)
346 av_log(0, AV_LOG_ERROR, "Memory deallocation error!\n");
349 static av_cold int dvbsub_init_decoder(AVCodecContext *avctx)
351 int i, r, g, b, a = 0;
352 DVBSubContext *ctx = avctx->priv_data;
354 if (!avctx->extradata || avctx->extradata_size != 4) {
355 av_log(avctx, AV_LOG_WARNING, "Invalid extradata, subtitle streams may be combined!\n");
356 ctx->composition_id = -1;
357 ctx->ancillary_id = -1;
359 ctx->composition_id = AV_RB16(avctx->extradata);
360 ctx->ancillary_id = AV_RB16(avctx->extradata + 2);
363 default_clut.id = -1;
364 default_clut.next = NULL;
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);
371 default_clut.clut16[0] = RGBA( 0, 0, 0, 0);
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;
382 default_clut.clut16[i] = RGBA(r, g, b, 255);
385 default_clut.clut256[0] = RGBA( 0, 0, 0, 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);
420 default_clut.clut256[i] = RGBA(r, g, b, a);
426 static av_cold int dvbsub_close_decoder(AVCodecContext *avctx)
428 DVBSubContext *ctx = avctx->priv_data;
429 DVBSubRegionDisplay *display;
433 while (ctx->display_list) {
434 display = ctx->display_list;
435 ctx->display_list = display->next;
443 static int dvbsub_read_2bit_string(uint8_t *destbuf, int dbuf_len,
444 const uint8_t **srcbuf, int buf_size,
445 int non_mod, uint8_t *map_table)
453 init_get_bits(&gb, *srcbuf, buf_size << 3);
455 while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
456 bits = get_bits(&gb, 2);
459 if (non_mod != 1 || bits != 1) {
461 *destbuf++ = map_table[bits];
467 bits = get_bits1(&gb);
469 run_length = get_bits(&gb, 3) + 3;
470 bits = get_bits(&gb, 2);
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) {
483 bits = get_bits1(&gb);
485 bits = get_bits(&gb, 2);
487 run_length = get_bits(&gb, 4) + 12;
488 bits = get_bits(&gb, 2);
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) {
501 run_length = get_bits(&gb, 8) + 29;
502 bits = get_bits(&gb, 2);
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) {
525 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
540 if (get_bits(&gb, 6))
541 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
543 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
548 static int dvbsub_read_4bit_string(uint8_t *destbuf, int dbuf_len,
549 const uint8_t **srcbuf, int buf_size,
550 int non_mod, uint8_t *map_table)
558 init_get_bits(&gb, *srcbuf, buf_size << 3);
560 while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
561 bits = get_bits(&gb, 4);
564 if (non_mod != 1 || bits != 1) {
566 *destbuf++ = map_table[bits];
572 bits = get_bits1(&gb);
574 run_length = get_bits(&gb, 3);
576 if (run_length == 0) {
577 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
588 while (run_length-- > 0 && pixels_read < dbuf_len) {
593 bits = get_bits1(&gb);
595 run_length = get_bits(&gb, 2) + 4;
596 bits = get_bits(&gb, 4);
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) {
609 bits = get_bits(&gb, 2);
611 run_length = get_bits(&gb, 4) + 9;
612 bits = get_bits(&gb, 4);
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) {
625 run_length = get_bits(&gb, 8) + 25;
626 bits = get_bits(&gb, 4);
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) {
661 if (get_bits(&gb, 8))
662 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
664 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
669 static int dvbsub_read_8bit_string(uint8_t *destbuf, int 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) {
699 if (non_mod == 1 && bits == 1)
700 pixels_read += run_length;
706 while (run_length-- > 0 && pixels_read < dbuf_len) {
714 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
721 static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
722 const uint8_t *buf, int buf_size, int top_bottom, int non_mod)
724 DVBSubContext *ctx = avctx->priv_data;
726 DVBSubRegion *region = get_region(ctx, display->region_id);
727 const uint8_t *buf_end = buf + buf_size;
732 uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
733 uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
734 uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
735 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
738 av_dlog(avctx, "DVB pixel block size %d, %s field:\n", buf_size,
739 top_bottom ? "bottom" : "top");
741 for (i = 0; i < buf_size; i++) {
743 av_dlog(avctx, "0x%8p: ", buf+i);
745 av_dlog(avctx, "%02x ", buf[i]);
747 av_dlog(avctx, "\n");
751 av_dlog(avctx, "\n");
758 x_pos = display->x_pos;
759 y_pos = display->y_pos;
761 if ((y_pos & 1) != top_bottom)
764 while (buf < buf_end) {
765 if (x_pos > region->width || y_pos > region->height) {
766 av_log(avctx, AV_LOG_ERROR, "Invalid object location!\n");
772 if (region->depth == 8)
774 else if (region->depth == 4)
779 x_pos += dvbsub_read_2bit_string(pbuf + (y_pos * region->width) + x_pos,
780 region->width - x_pos, &buf, buf_end - buf,
784 if (region->depth < 4) {
785 av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
789 if (region->depth == 8)
794 x_pos += dvbsub_read_4bit_string(pbuf + (y_pos * region->width) + x_pos,
795 region->width - x_pos, &buf, buf_end - buf,
799 if (region->depth < 8) {
800 av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
804 x_pos += dvbsub_read_8bit_string(pbuf + (y_pos * region->width) + x_pos,
805 region->width - x_pos, &buf, buf_end - buf,
810 map2to4[0] = (*buf) >> 4;
811 map2to4[1] = (*buf++) & 0xf;
812 map2to4[2] = (*buf) >> 4;
813 map2to4[3] = (*buf++) & 0xf;
816 for (i = 0; i < 4; i++)
820 for (i = 0; i < 16; i++)
825 x_pos = display->x_pos;
829 av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
835 static void dvbsub_parse_object_segment(AVCodecContext *avctx,
836 const uint8_t *buf, int buf_size)
838 DVBSubContext *ctx = avctx->priv_data;
840 const uint8_t *buf_end = buf + buf_size;
841 const uint8_t *block;
843 DVBSubObject *object;
844 DVBSubObjectDisplay *display;
845 int top_field_len, bottom_field_len;
847 int coding_method, non_modifying_color;
849 object_id = AV_RB16(buf);
852 object = get_object(ctx, object_id);
857 coding_method = ((*buf) >> 2) & 3;
858 non_modifying_color = ((*buf++) >> 1) & 1;
860 if (coding_method == 0) {
861 top_field_len = AV_RB16(buf);
863 bottom_field_len = AV_RB16(buf);
866 if (buf + top_field_len + bottom_field_len > buf_end) {
867 av_log(avctx, AV_LOG_ERROR, "Field data size too large\n");
871 for (display = object->display_list; display; display = display->object_list_next) {
874 dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
875 non_modifying_color);
877 if (bottom_field_len > 0)
878 block = buf + top_field_len;
880 bottom_field_len = top_field_len;
882 dvbsub_parse_pixel_data_block(avctx, display, block, bottom_field_len, 1,
883 non_modifying_color);
886 /* } else if (coding_method == 1) {*/
889 av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
894 static void dvbsub_parse_clut_segment(AVCodecContext *avctx,
895 const uint8_t *buf, int buf_size)
897 DVBSubContext *ctx = avctx->priv_data;
899 const uint8_t *buf_end = buf + buf_size;
902 int entry_id, depth , full_range;
903 int y, cr, cb, alpha;
904 int r, g, b, r_add, g_add, b_add;
906 av_dlog(avctx, "DVB clut packet:\n");
908 for (i=0; i < buf_size; i++) {
909 av_dlog(avctx, "%02x ", buf[i]);
911 av_dlog(avctx, "\n");
915 av_dlog(avctx, "\n");
920 clut = get_clut(ctx, clut_id);
923 clut = av_malloc(sizeof(DVBSubCLUT));
925 memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
929 clut->next = ctx->clut_list;
930 ctx->clut_list = clut;
933 while (buf + 4 < buf_end) {
936 depth = (*buf) & 0xe0;
939 av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
943 full_range = (*buf++) & 1;
952 cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
953 cb = (buf[1] << 2) & 0xf0;
954 alpha = (buf[1] << 6) & 0xc0;
962 YUV_TO_RGB1_CCIR(cb, cr);
963 YUV_TO_RGB2_CCIR(r, g, b, y);
965 av_dlog(avctx, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
968 clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
970 clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
972 clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
977 static void dvbsub_parse_region_segment(AVCodecContext *avctx,
978 const uint8_t *buf, int buf_size)
980 DVBSubContext *ctx = avctx->priv_data;
982 const uint8_t *buf_end = buf + buf_size;
983 int region_id, object_id;
984 DVBSubRegion *region;
985 DVBSubObject *object;
986 DVBSubObjectDisplay *display;
994 region = get_region(ctx, region_id);
997 region = av_mallocz(sizeof(DVBSubRegion));
999 region->id = region_id;
1001 region->next = ctx->region_list;
1002 ctx->region_list = region;
1005 fill = ((*buf++) >> 3) & 1;
1007 region->width = AV_RB16(buf);
1009 region->height = AV_RB16(buf);
1012 if (region->width * region->height != region->buf_size) {
1013 av_free(region->pbuf);
1015 region->buf_size = region->width * region->height;
1017 region->pbuf = av_malloc(region->buf_size);
1022 region->depth = 1 << (((*buf++) >> 2) & 7);
1023 if(region->depth<2 || region->depth>8){
1024 av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);
1027 region->clut = *buf++;
1029 if (region->depth == 8)
1030 region->bgcolor = *buf++;
1034 if (region->depth == 4)
1035 region->bgcolor = (((*buf++) >> 4) & 15);
1037 region->bgcolor = (((*buf++) >> 2) & 3);
1040 av_dlog(avctx, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
1043 memset(region->pbuf, region->bgcolor, region->buf_size);
1044 av_dlog(avctx, "Fill region (%d)\n", region->bgcolor);
1047 delete_region_display_list(ctx, region);
1049 while (buf + 5 < buf_end) {
1050 object_id = AV_RB16(buf);
1053 object = get_object(ctx, object_id);
1056 object = av_mallocz(sizeof(DVBSubObject));
1058 object->id = object_id;
1059 object->next = ctx->object_list;
1060 ctx->object_list = object;
1063 object->type = (*buf) >> 6;
1065 display = av_mallocz(sizeof(DVBSubObjectDisplay));
1067 display->object_id = object_id;
1068 display->region_id = region_id;
1070 display->x_pos = AV_RB16(buf) & 0xfff;
1072 display->y_pos = AV_RB16(buf) & 0xfff;
1075 if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
1076 display->fgcolor = *buf++;
1077 display->bgcolor = *buf++;
1080 display->region_list_next = region->display_list;
1081 region->display_list = display;
1083 display->object_list_next = object->display_list;
1084 object->display_list = display;
1088 static void dvbsub_parse_page_segment(AVCodecContext *avctx,
1089 const uint8_t *buf, int buf_size)
1091 DVBSubContext *ctx = avctx->priv_data;
1092 DVBSubRegionDisplay *display;
1093 DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
1095 const uint8_t *buf_end = buf + buf_size;
1102 ctx->time_out = *buf++;
1103 page_state = ((*buf++) >> 2) & 3;
1105 av_dlog(avctx, "Page time out %ds, state %d\n", ctx->time_out, page_state);
1107 if (page_state == 2) {
1111 tmp_display_list = ctx->display_list;
1112 ctx->display_list = NULL;
1113 ctx->display_list_size = 0;
1115 while (buf + 5 < buf_end) {
1119 display = tmp_display_list;
1120 tmp_ptr = &tmp_display_list;
1122 while (display && display->region_id != region_id) {
1123 tmp_ptr = &display->next;
1124 display = display->next;
1128 display = av_mallocz(sizeof(DVBSubRegionDisplay));
1130 display->region_id = region_id;
1132 display->x_pos = AV_RB16(buf);
1134 display->y_pos = AV_RB16(buf);
1137 *tmp_ptr = display->next;
1139 display->next = ctx->display_list;
1140 ctx->display_list = display;
1141 ctx->display_list_size++;
1143 av_dlog(avctx, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
1146 while (tmp_display_list) {
1147 display = tmp_display_list;
1149 tmp_display_list = display->next;
1158 static void save_display_set(DVBSubContext *ctx)
1160 DVBSubRegion *region;
1161 DVBSubRegionDisplay *display;
1163 uint32_t *clut_table;
1164 int x_pos, y_pos, width, height;
1165 int x, y, y_off, x_off;
1168 static int fileno_index = 0;
1175 for (display = ctx->display_list; display; display = display->next) {
1176 region = get_region(ctx, display->region_id);
1179 x_pos = display->x_pos;
1180 y_pos = display->y_pos;
1181 width = region->width;
1182 height = region->height;
1184 if (display->x_pos < x_pos) {
1185 width += (x_pos - display->x_pos);
1186 x_pos = display->x_pos;
1189 if (display->y_pos < y_pos) {
1190 height += (y_pos - display->y_pos);
1191 y_pos = display->y_pos;
1194 if (display->x_pos + region->width > x_pos + width) {
1195 width = display->x_pos + region->width - x_pos;
1198 if (display->y_pos + region->height > y_pos + height) {
1199 height = display->y_pos + region->height - y_pos;
1206 pbuf = av_malloc(width * height * 4);
1208 for (display = ctx->display_list; display; display = display->next) {
1209 region = get_region(ctx, display->region_id);
1211 x_off = display->x_pos - x_pos;
1212 y_off = display->y_pos - y_pos;
1214 clut = get_clut(ctx, region->clut);
1217 clut = &default_clut;
1219 switch (region->depth) {
1221 clut_table = clut->clut4;
1224 clut_table = clut->clut256;
1228 clut_table = clut->clut16;
1232 for (y = 0; y < region->height; y++) {
1233 for (x = 0; x < region->width; x++) {
1234 pbuf[((y + y_off) * width) + x_off + x] =
1235 clut_table[region->pbuf[y * region->width + x]];
1241 snprintf(filename, sizeof(filename), "dvbs.%d", fileno_index);
1243 png_save2(filename, pbuf, width, height);
1252 static void dvbsub_parse_display_definition_segment(AVCodecContext *avctx,
1256 DVBSubContext *ctx = avctx->priv_data;
1257 DVBSubDisplayDefinition *display_def = ctx->display_definition;
1258 int dds_version, info_byte;
1263 info_byte = bytestream_get_byte(&buf);
1264 dds_version = info_byte >> 4;
1265 if (display_def && display_def->version == dds_version)
1266 return; // already have this display definition version
1269 display_def = av_mallocz(sizeof(*display_def));
1270 ctx->display_definition = display_def;
1275 display_def->version = dds_version;
1278 display_def->width = bytestream_get_be16(&buf) + 1;
1279 display_def->height = bytestream_get_be16(&buf) + 1;
1284 if (info_byte & 1<<3) { // display_window_flag
1285 display_def->x = bytestream_get_be16(&buf);
1286 display_def->y = bytestream_get_be16(&buf);
1287 display_def->width = bytestream_get_be16(&buf) - display_def->x + 1;
1288 display_def->height = bytestream_get_be16(&buf) - display_def->y + 1;
1292 static int dvbsub_display_end_segment(AVCodecContext *avctx, const uint8_t *buf,
1293 int buf_size, AVSubtitle *sub)
1295 DVBSubContext *ctx = avctx->priv_data;
1296 DVBSubDisplayDefinition *display_def = ctx->display_definition;
1298 DVBSubRegion *region;
1299 DVBSubRegionDisplay *display;
1300 AVSubtitleRect *rect;
1302 uint32_t *clut_table;
1304 int offset_x=0, offset_y=0;
1307 sub->start_display_time = 0;
1308 sub->end_display_time = ctx->time_out * 1000;
1312 offset_x = display_def->x;
1313 offset_y = display_def->y;
1316 sub->num_rects = ctx->display_list_size;
1317 if (sub->num_rects <= 0)
1318 return AVERROR_INVALIDDATA;
1320 sub->rects = av_mallocz_array(sub->num_rects * sub->num_rects,
1321 sizeof(*sub->rects));
1323 return AVERROR(ENOMEM);
1327 for (display = ctx->display_list; display; display = display->next) {
1328 region = get_region(ctx, display->region_id);
1329 rect = sub->rects[i];
1334 rect->x = display->x_pos + offset_x;
1335 rect->y = display->y_pos + offset_y;
1336 rect->w = region->width;
1337 rect->h = region->height;
1338 rect->nb_colors = 16;
1339 rect->type = SUBTITLE_BITMAP;
1340 rect->pict.linesize[0] = region->width;
1342 clut = get_clut(ctx, region->clut);
1345 clut = &default_clut;
1347 switch (region->depth) {
1349 clut_table = clut->clut4;
1352 clut_table = clut->clut256;
1356 clut_table = clut->clut16;
1360 rect->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
1361 if (!rect->pict.data[1]) {
1362 av_free(sub->rects);
1363 return AVERROR(ENOMEM);
1365 memcpy(rect->pict.data[1], clut_table, (1 << region->depth) * sizeof(uint32_t));
1367 rect->pict.data[0] = av_malloc(region->buf_size);
1368 if (!rect->pict.data[0]) {
1369 av_free(rect->pict.data[1]);
1370 av_free(sub->rects);
1371 return AVERROR(ENOMEM);
1373 memcpy(rect->pict.data[0], region->pbuf, region->buf_size);
1381 save_display_set(ctx);
1387 static int dvbsub_decode(AVCodecContext *avctx,
1388 void *data, int *data_size,
1391 const uint8_t *buf = avpkt->data;
1392 int buf_size = avpkt->size;
1393 DVBSubContext *ctx = avctx->priv_data;
1394 AVSubtitle *sub = data;
1395 const uint8_t *p, *p_end;
1401 av_dlog(avctx, "DVB sub packet:\n");
1403 for (i=0; i < buf_size; i++) {
1404 av_dlog(avctx, "%02x ", buf[i]);
1406 av_dlog(avctx, "\n");
1410 av_dlog(avctx, "\n");
1412 if (buf_size <= 6 || *buf != 0x0f) {
1413 av_dlog(avctx, "incomplete or broken packet");
1418 p_end = buf + buf_size;
1420 while (p_end - p >= 6 && *p == 0x0f) {
1422 segment_type = *p++;
1423 page_id = AV_RB16(p);
1425 segment_length = AV_RB16(p);
1428 if (p_end - p < segment_length) {
1429 av_dlog(avctx, "incomplete or broken packet");
1433 if (page_id == ctx->composition_id || page_id == ctx->ancillary_id ||
1434 ctx->composition_id == -1 || ctx->ancillary_id == -1) {
1435 switch (segment_type) {
1436 case DVBSUB_PAGE_SEGMENT:
1437 dvbsub_parse_page_segment(avctx, p, segment_length);
1439 case DVBSUB_REGION_SEGMENT:
1440 dvbsub_parse_region_segment(avctx, p, segment_length);
1442 case DVBSUB_CLUT_SEGMENT:
1443 dvbsub_parse_clut_segment(avctx, p, segment_length);
1445 case DVBSUB_OBJECT_SEGMENT:
1446 dvbsub_parse_object_segment(avctx, p, segment_length);
1448 case DVBSUB_DISPLAYDEFINITION_SEGMENT:
1449 dvbsub_parse_display_definition_segment(avctx, p, segment_length);
1451 case DVBSUB_DISPLAY_SEGMENT:
1452 *data_size = dvbsub_display_end_segment(avctx, p, segment_length, sub);
1455 av_dlog(avctx, "Subtitling segment type 0x%x, page id %d, length %d\n",
1456 segment_type, page_id, segment_length);
1461 p += segment_length;
1468 AVCodec ff_dvbsub_decoder = {
1470 .long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),
1471 .type = AVMEDIA_TYPE_SUBTITLE,
1472 .id = AV_CODEC_ID_DVB_SUBTITLE,
1473 .priv_data_size = sizeof(DVBSubContext),
1474 .init = dvbsub_init_decoder,
1475 .close = dvbsub_close_decoder,
1476 .decode = dvbsub_decode,