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_cropTbl + MAX_NEG_CROP)
40 static void png_save(const char *filename, uint8_t *bitmap, int w, int h,
41 uint32_t *rgba_palette)
45 char fname[40], fname2[40];
48 snprintf(fname, 40, "%s.ppm", filename);
50 f = fopen(fname, "w");
59 for(y = 0; y < h; y++) {
60 for(x = 0; x < w; x++) {
61 v = rgba_palette[bitmap[y * w + x]];
62 putc((v >> 16) & 0xff, f);
63 putc((v >> 8) & 0xff, f);
64 putc((v >> 0) & 0xff, f);
70 snprintf(fname2, 40, "%s-a.pgm", filename);
72 f = fopen(fname2, "w");
81 for(y = 0; y < h; y++) {
82 for(x = 0; x < w; x++) {
83 v = rgba_palette[bitmap[y * w + x]];
84 putc((v >> 24) & 0xff, f);
89 snprintf(command, 1024, "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
92 snprintf(command, 1024, "rm %s %s", fname, fname2);
97 static void png_save2(const char *filename, uint32_t *bitmap, int w, int h)
101 char fname[40], fname2[40];
104 snprintf(fname, sizeof(fname), "%s.ppm", filename);
106 f = fopen(fname, "w");
115 for(y = 0; y < h; y++) {
116 for(x = 0; x < w; x++) {
117 v = bitmap[y * w + x];
118 putc((v >> 16) & 0xff, f);
119 putc((v >> 8) & 0xff, f);
120 putc((v >> 0) & 0xff, f);
126 snprintf(fname2, sizeof(fname2), "%s-a.pgm", filename);
128 f = fopen(fname2, "w");
137 for(y = 0; y < h; y++) {
138 for(x = 0; x < w; x++) {
139 v = bitmap[y * w + x];
140 putc((v >> 24) & 0xff, f);
145 snprintf(command, sizeof(command), "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
148 snprintf(command, sizeof(command), "rm %s %s", fname, fname2);
153 #define RGBA(r,g,b,a) (((unsigned)(a) << 24) | ((r) << 16) | ((g) << 8) | (b))
155 typedef struct DVBSubCLUT {
160 uint32_t clut256[256];
162 struct DVBSubCLUT *next;
165 static DVBSubCLUT default_clut;
167 typedef struct DVBSubObjectDisplay {
177 struct DVBSubObjectDisplay *region_list_next;
178 struct DVBSubObjectDisplay *object_list_next;
179 } DVBSubObjectDisplay;
181 typedef struct DVBSubObject {
186 DVBSubObjectDisplay *display_list;
188 struct DVBSubObject *next;
191 typedef struct DVBSubRegionDisplay {
197 struct DVBSubRegionDisplay *next;
198 } DVBSubRegionDisplay;
200 typedef struct DVBSubRegion {
213 DVBSubObjectDisplay *display_list;
215 struct DVBSubRegion *next;
218 typedef struct DVBSubDisplayDefinition {
225 } DVBSubDisplayDefinition;
227 typedef struct DVBSubContext {
232 DVBSubRegion *region_list;
233 DVBSubCLUT *clut_list;
234 DVBSubObject *object_list;
236 int display_list_size;
237 DVBSubRegionDisplay *display_list;
238 DVBSubDisplayDefinition *display_definition;
242 static DVBSubObject* get_object(DVBSubContext *ctx, int object_id)
244 DVBSubObject *ptr = ctx->object_list;
246 while (ptr && ptr->id != object_id) {
253 static DVBSubCLUT* get_clut(DVBSubContext *ctx, int clut_id)
255 DVBSubCLUT *ptr = ctx->clut_list;
257 while (ptr && ptr->id != clut_id) {
264 static DVBSubRegion* get_region(DVBSubContext *ctx, int region_id)
266 DVBSubRegion *ptr = ctx->region_list;
268 while (ptr && ptr->id != region_id) {
275 static void delete_region_display_list(DVBSubContext *ctx, DVBSubRegion *region)
277 DVBSubObject *object, *obj2, **obj2_ptr;
278 DVBSubObjectDisplay *display, *obj_disp, **obj_disp_ptr;
280 while (region->display_list) {
281 display = region->display_list;
283 object = get_object(ctx, display->object_id);
286 obj_disp_ptr = &object->display_list;
287 obj_disp = *obj_disp_ptr;
289 while (obj_disp && obj_disp != display) {
290 obj_disp_ptr = &obj_disp->object_list_next;
291 obj_disp = *obj_disp_ptr;
295 *obj_disp_ptr = obj_disp->object_list_next;
297 if (!object->display_list) {
298 obj2_ptr = &ctx->object_list;
301 while (obj2 != object) {
303 obj2_ptr = &obj2->next;
307 *obj2_ptr = obj2->next;
314 region->display_list = display->region_list_next;
321 static void delete_state(DVBSubContext *ctx)
323 DVBSubRegion *region;
326 while (ctx->region_list) {
327 region = ctx->region_list;
329 ctx->region_list = region->next;
331 delete_region_display_list(ctx, region);
332 av_free(region->pbuf);
336 while (ctx->clut_list) {
337 clut = ctx->clut_list;
339 ctx->clut_list = clut->next;
344 av_freep(&ctx->display_definition);
346 /* Should already be null */
347 if (ctx->object_list)
348 av_log(0, AV_LOG_ERROR, "Memory deallocation error!\n");
351 static av_cold int dvbsub_init_decoder(AVCodecContext *avctx)
353 int i, r, g, b, a = 0;
354 DVBSubContext *ctx = avctx->priv_data;
356 if (!avctx->extradata || avctx->extradata_size != 4) {
357 av_log(avctx, AV_LOG_WARNING, "Invalid extradata, subtitle streams may be combined!\n");
358 ctx->composition_id = -1;
359 ctx->ancillary_id = -1;
361 ctx->composition_id = AV_RB16(avctx->extradata);
362 ctx->ancillary_id = AV_RB16(avctx->extradata + 2);
365 default_clut.id = -1;
366 default_clut.next = NULL;
368 default_clut.clut4[0] = RGBA( 0, 0, 0, 0);
369 default_clut.clut4[1] = RGBA(255, 255, 255, 255);
370 default_clut.clut4[2] = RGBA( 0, 0, 0, 255);
371 default_clut.clut4[3] = RGBA(127, 127, 127, 255);
373 default_clut.clut16[0] = RGBA( 0, 0, 0, 0);
374 for (i = 1; i < 16; i++) {
376 r = (i & 1) ? 255 : 0;
377 g = (i & 2) ? 255 : 0;
378 b = (i & 4) ? 255 : 0;
380 r = (i & 1) ? 127 : 0;
381 g = (i & 2) ? 127 : 0;
382 b = (i & 4) ? 127 : 0;
384 default_clut.clut16[i] = RGBA(r, g, b, 255);
387 default_clut.clut256[0] = RGBA( 0, 0, 0, 0);
388 for (i = 1; i < 256; i++) {
390 r = (i & 1) ? 255 : 0;
391 g = (i & 2) ? 255 : 0;
392 b = (i & 4) ? 255 : 0;
397 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
398 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
399 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
403 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
404 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
405 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
409 r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
410 g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
411 b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
415 r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
416 g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
417 b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
422 default_clut.clut256[i] = RGBA(r, g, b, a);
428 static av_cold int dvbsub_close_decoder(AVCodecContext *avctx)
430 DVBSubContext *ctx = avctx->priv_data;
431 DVBSubRegionDisplay *display;
435 while (ctx->display_list) {
436 display = ctx->display_list;
437 ctx->display_list = display->next;
445 static int dvbsub_read_2bit_string(uint8_t *destbuf, int dbuf_len,
446 const uint8_t **srcbuf, int buf_size,
447 int non_mod, uint8_t *map_table)
455 init_get_bits(&gb, *srcbuf, buf_size << 3);
457 while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
458 bits = get_bits(&gb, 2);
461 if (non_mod != 1 || bits != 1) {
463 *destbuf++ = map_table[bits];
469 bits = get_bits1(&gb);
471 run_length = get_bits(&gb, 3) + 3;
472 bits = get_bits(&gb, 2);
474 if (non_mod == 1 && bits == 1)
475 pixels_read += run_length;
478 bits = map_table[bits];
479 while (run_length-- > 0 && pixels_read < dbuf_len) {
485 bits = get_bits1(&gb);
487 bits = get_bits(&gb, 2);
489 run_length = get_bits(&gb, 4) + 12;
490 bits = get_bits(&gb, 2);
492 if (non_mod == 1 && bits == 1)
493 pixels_read += run_length;
496 bits = map_table[bits];
497 while (run_length-- > 0 && pixels_read < dbuf_len) {
502 } else if (bits == 3) {
503 run_length = get_bits(&gb, 8) + 29;
504 bits = get_bits(&gb, 2);
506 if (non_mod == 1 && bits == 1)
507 pixels_read += run_length;
510 bits = map_table[bits];
511 while (run_length-- > 0 && pixels_read < dbuf_len) {
516 } else if (bits == 1) {
522 if (pixels_read <= dbuf_len) {
527 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
542 if (get_bits(&gb, 6))
543 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
545 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
550 static int dvbsub_read_4bit_string(uint8_t *destbuf, int dbuf_len,
551 const uint8_t **srcbuf, int buf_size,
552 int non_mod, uint8_t *map_table)
560 init_get_bits(&gb, *srcbuf, buf_size << 3);
562 while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
563 bits = get_bits(&gb, 4);
566 if (non_mod != 1 || bits != 1) {
568 *destbuf++ = map_table[bits];
574 bits = get_bits1(&gb);
576 run_length = get_bits(&gb, 3);
578 if (run_length == 0) {
579 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
590 while (run_length-- > 0 && pixels_read < dbuf_len) {
595 bits = get_bits1(&gb);
597 run_length = get_bits(&gb, 2) + 4;
598 bits = get_bits(&gb, 4);
600 if (non_mod == 1 && bits == 1)
601 pixels_read += run_length;
604 bits = map_table[bits];
605 while (run_length-- > 0 && pixels_read < dbuf_len) {
611 bits = get_bits(&gb, 2);
613 run_length = get_bits(&gb, 4) + 9;
614 bits = get_bits(&gb, 4);
616 if (non_mod == 1 && bits == 1)
617 pixels_read += run_length;
620 bits = map_table[bits];
621 while (run_length-- > 0 && pixels_read < dbuf_len) {
626 } else if (bits == 3) {
627 run_length = get_bits(&gb, 8) + 25;
628 bits = get_bits(&gb, 4);
630 if (non_mod == 1 && bits == 1)
631 pixels_read += run_length;
634 bits = map_table[bits];
635 while (run_length-- > 0 && pixels_read < dbuf_len) {
640 } else if (bits == 1) {
646 if (pixels_read <= dbuf_len) {
663 if (get_bits(&gb, 8))
664 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
666 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
671 static int dvbsub_read_8bit_string(uint8_t *destbuf, int dbuf_len,
672 const uint8_t **srcbuf, int buf_size,
673 int non_mod, uint8_t *map_table)
675 const uint8_t *sbuf_end = (*srcbuf) + buf_size;
680 while (*srcbuf < sbuf_end && pixels_read < dbuf_len) {
684 if (non_mod != 1 || bits != 1) {
686 *destbuf++ = map_table[bits];
693 run_length = bits & 0x7f;
694 if ((bits & 0x80) == 0) {
695 if (run_length == 0) {
703 while (run_length-- > 0 && pixels_read < dbuf_len) {
710 if (non_mod == 1 && bits == 1)
711 pixels_read += run_length;
713 bits = map_table[bits];
714 else while (run_length-- > 0 && pixels_read < dbuf_len) {
723 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
730 static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
731 const uint8_t *buf, int buf_size, int top_bottom, int non_mod)
733 DVBSubContext *ctx = avctx->priv_data;
735 DVBSubRegion *region = get_region(ctx, display->region_id);
736 const uint8_t *buf_end = buf + buf_size;
741 uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
742 uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
743 uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
744 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
747 av_dlog(avctx, "DVB pixel block size %d, %s field:\n", buf_size,
748 top_bottom ? "bottom" : "top");
750 for (i = 0; i < buf_size; i++) {
752 av_dlog(avctx, "0x%8p: ", buf+i);
754 av_dlog(avctx, "%02x ", buf[i]);
756 av_dlog(avctx, "\n");
760 av_dlog(avctx, "\n");
767 x_pos = display->x_pos;
768 y_pos = display->y_pos;
770 if ((y_pos & 1) != top_bottom)
773 while (buf < buf_end) {
774 if (x_pos > region->width || y_pos > region->height) {
775 av_log(avctx, AV_LOG_ERROR, "Invalid object location!\n");
781 if (region->depth == 8)
783 else if (region->depth == 4)
788 x_pos += dvbsub_read_2bit_string(pbuf + (y_pos * region->width) + x_pos,
789 region->width - x_pos, &buf, buf_end - buf,
793 if (region->depth < 4) {
794 av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
798 if (region->depth == 8)
803 x_pos += dvbsub_read_4bit_string(pbuf + (y_pos * region->width) + x_pos,
804 region->width - x_pos, &buf, buf_end - buf,
808 if (region->depth < 8) {
809 av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
813 x_pos += dvbsub_read_8bit_string(pbuf + (y_pos * region->width) + x_pos,
814 region->width - x_pos, &buf, buf_end - buf,
819 map2to4[0] = (*buf) >> 4;
820 map2to4[1] = (*buf++) & 0xf;
821 map2to4[2] = (*buf) >> 4;
822 map2to4[3] = (*buf++) & 0xf;
825 for (i = 0; i < 4; i++)
829 for (i = 0; i < 16; i++)
834 x_pos = display->x_pos;
838 av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
844 static void dvbsub_parse_object_segment(AVCodecContext *avctx,
845 const uint8_t *buf, int buf_size)
847 DVBSubContext *ctx = avctx->priv_data;
849 const uint8_t *buf_end = buf + buf_size;
850 const uint8_t *block;
852 DVBSubObject *object;
853 DVBSubObjectDisplay *display;
854 int top_field_len, bottom_field_len;
856 int coding_method, non_modifying_color;
858 object_id = AV_RB16(buf);
861 object = get_object(ctx, object_id);
866 coding_method = ((*buf) >> 2) & 3;
867 non_modifying_color = ((*buf++) >> 1) & 1;
869 if (coding_method == 0) {
870 top_field_len = AV_RB16(buf);
872 bottom_field_len = AV_RB16(buf);
875 if (buf + top_field_len + bottom_field_len > buf_end) {
876 av_log(avctx, AV_LOG_ERROR, "Field data size too large\n");
880 for (display = object->display_list; display; display = display->object_list_next) {
883 dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
884 non_modifying_color);
886 if (bottom_field_len > 0)
887 block = buf + top_field_len;
889 bottom_field_len = top_field_len;
891 dvbsub_parse_pixel_data_block(avctx, display, block, bottom_field_len, 1,
892 non_modifying_color);
895 /* } else if (coding_method == 1) {*/
898 av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
903 static void dvbsub_parse_clut_segment(AVCodecContext *avctx,
904 const uint8_t *buf, int buf_size)
906 DVBSubContext *ctx = avctx->priv_data;
908 const uint8_t *buf_end = buf + buf_size;
911 int entry_id, depth , full_range;
912 int y, cr, cb, alpha;
913 int r, g, b, r_add, g_add, b_add;
915 av_dlog(avctx, "DVB clut packet:\n");
917 for (i=0; i < buf_size; i++) {
918 av_dlog(avctx, "%02x ", buf[i]);
920 av_dlog(avctx, "\n");
924 av_dlog(avctx, "\n");
929 clut = get_clut(ctx, clut_id);
932 clut = av_malloc(sizeof(DVBSubCLUT));
934 memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
938 clut->next = ctx->clut_list;
939 ctx->clut_list = clut;
942 while (buf + 4 < buf_end) {
945 depth = (*buf) & 0xe0;
948 av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
952 full_range = (*buf++) & 1;
961 cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
962 cb = (buf[1] << 2) & 0xf0;
963 alpha = (buf[1] << 6) & 0xc0;
971 YUV_TO_RGB1_CCIR(cb, cr);
972 YUV_TO_RGB2_CCIR(r, g, b, y);
974 av_dlog(avctx, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
977 clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
979 clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
981 clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
986 static void dvbsub_parse_region_segment(AVCodecContext *avctx,
987 const uint8_t *buf, int buf_size)
989 DVBSubContext *ctx = avctx->priv_data;
991 const uint8_t *buf_end = buf + buf_size;
992 int region_id, object_id;
993 DVBSubRegion *region;
994 DVBSubObject *object;
995 DVBSubObjectDisplay *display;
1003 region = get_region(ctx, region_id);
1006 region = av_mallocz(sizeof(DVBSubRegion));
1008 region->id = region_id;
1010 region->next = ctx->region_list;
1011 ctx->region_list = region;
1014 fill = ((*buf++) >> 3) & 1;
1016 region->width = AV_RB16(buf);
1018 region->height = AV_RB16(buf);
1021 if (region->width * region->height != region->buf_size) {
1022 av_free(region->pbuf);
1024 region->buf_size = region->width * region->height;
1026 region->pbuf = av_malloc(region->buf_size);
1031 region->depth = 1 << (((*buf++) >> 2) & 7);
1032 if(region->depth<2 || region->depth>8){
1033 av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);
1036 region->clut = *buf++;
1038 if (region->depth == 8)
1039 region->bgcolor = *buf++;
1043 if (region->depth == 4)
1044 region->bgcolor = (((*buf++) >> 4) & 15);
1046 region->bgcolor = (((*buf++) >> 2) & 3);
1049 av_dlog(avctx, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
1052 memset(region->pbuf, region->bgcolor, region->buf_size);
1053 av_dlog(avctx, "Fill region (%d)\n", region->bgcolor);
1056 delete_region_display_list(ctx, region);
1058 while (buf + 5 < buf_end) {
1059 object_id = AV_RB16(buf);
1062 object = get_object(ctx, object_id);
1065 object = av_mallocz(sizeof(DVBSubObject));
1067 object->id = object_id;
1068 object->next = ctx->object_list;
1069 ctx->object_list = object;
1072 object->type = (*buf) >> 6;
1074 display = av_mallocz(sizeof(DVBSubObjectDisplay));
1076 display->object_id = object_id;
1077 display->region_id = region_id;
1079 display->x_pos = AV_RB16(buf) & 0xfff;
1081 display->y_pos = AV_RB16(buf) & 0xfff;
1084 if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
1085 display->fgcolor = *buf++;
1086 display->bgcolor = *buf++;
1089 display->region_list_next = region->display_list;
1090 region->display_list = display;
1092 display->object_list_next = object->display_list;
1093 object->display_list = display;
1097 static void dvbsub_parse_page_segment(AVCodecContext *avctx,
1098 const uint8_t *buf, int buf_size)
1100 DVBSubContext *ctx = avctx->priv_data;
1101 DVBSubRegionDisplay *display;
1102 DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
1104 const uint8_t *buf_end = buf + buf_size;
1111 ctx->time_out = *buf++;
1112 page_state = ((*buf++) >> 2) & 3;
1114 av_dlog(avctx, "Page time out %ds, state %d\n", ctx->time_out, page_state);
1116 if (page_state == 2) {
1120 tmp_display_list = ctx->display_list;
1121 ctx->display_list = NULL;
1122 ctx->display_list_size = 0;
1124 while (buf + 5 < buf_end) {
1128 display = tmp_display_list;
1129 tmp_ptr = &tmp_display_list;
1131 while (display && display->region_id != region_id) {
1132 tmp_ptr = &display->next;
1133 display = display->next;
1137 display = av_mallocz(sizeof(DVBSubRegionDisplay));
1139 display->region_id = region_id;
1141 display->x_pos = AV_RB16(buf);
1143 display->y_pos = AV_RB16(buf);
1146 *tmp_ptr = display->next;
1148 display->next = ctx->display_list;
1149 ctx->display_list = display;
1150 ctx->display_list_size++;
1152 av_dlog(avctx, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
1155 while (tmp_display_list) {
1156 display = tmp_display_list;
1158 tmp_display_list = display->next;
1167 static void save_display_set(DVBSubContext *ctx)
1169 DVBSubRegion *region;
1170 DVBSubRegionDisplay *display;
1172 uint32_t *clut_table;
1173 int x_pos, y_pos, width, height;
1174 int x, y, y_off, x_off;
1177 static int fileno_index = 0;
1184 for (display = ctx->display_list; display; display = display->next) {
1185 region = get_region(ctx, display->region_id);
1188 x_pos = display->x_pos;
1189 y_pos = display->y_pos;
1190 width = region->width;
1191 height = region->height;
1193 if (display->x_pos < x_pos) {
1194 width += (x_pos - display->x_pos);
1195 x_pos = display->x_pos;
1198 if (display->y_pos < y_pos) {
1199 height += (y_pos - display->y_pos);
1200 y_pos = display->y_pos;
1203 if (display->x_pos + region->width > x_pos + width) {
1204 width = display->x_pos + region->width - x_pos;
1207 if (display->y_pos + region->height > y_pos + height) {
1208 height = display->y_pos + region->height - y_pos;
1215 pbuf = av_malloc(width * height * 4);
1217 for (display = ctx->display_list; display; display = display->next) {
1218 region = get_region(ctx, display->region_id);
1220 x_off = display->x_pos - x_pos;
1221 y_off = display->y_pos - y_pos;
1223 clut = get_clut(ctx, region->clut);
1226 clut = &default_clut;
1228 switch (region->depth) {
1230 clut_table = clut->clut4;
1233 clut_table = clut->clut256;
1237 clut_table = clut->clut16;
1241 for (y = 0; y < region->height; y++) {
1242 for (x = 0; x < region->width; x++) {
1243 pbuf[((y + y_off) * width) + x_off + x] =
1244 clut_table[region->pbuf[y * region->width + x]];
1250 snprintf(filename, sizeof(filename), "dvbs.%d", fileno_index);
1252 png_save2(filename, pbuf, width, height);
1261 static void dvbsub_parse_display_definition_segment(AVCodecContext *avctx,
1265 DVBSubContext *ctx = avctx->priv_data;
1266 DVBSubDisplayDefinition *display_def = ctx->display_definition;
1267 int dds_version, info_byte;
1272 info_byte = bytestream_get_byte(&buf);
1273 dds_version = info_byte >> 4;
1274 if (display_def && display_def->version == dds_version)
1275 return; // already have this display definition version
1278 display_def = av_mallocz(sizeof(*display_def));
1279 ctx->display_definition = display_def;
1284 display_def->version = dds_version;
1287 display_def->width = bytestream_get_be16(&buf) + 1;
1288 display_def->height = bytestream_get_be16(&buf) + 1;
1293 if (info_byte & 1<<3) { // display_window_flag
1294 display_def->x = bytestream_get_be16(&buf);
1295 display_def->y = bytestream_get_be16(&buf);
1296 display_def->width = bytestream_get_be16(&buf) - display_def->x + 1;
1297 display_def->height = bytestream_get_be16(&buf) - display_def->y + 1;
1301 static int dvbsub_display_end_segment(AVCodecContext *avctx, const uint8_t *buf,
1302 int buf_size, AVSubtitle *sub)
1304 DVBSubContext *ctx = avctx->priv_data;
1305 DVBSubDisplayDefinition *display_def = ctx->display_definition;
1307 DVBSubRegion *region;
1308 DVBSubRegionDisplay *display;
1309 AVSubtitleRect *rect;
1311 uint32_t *clut_table;
1313 int offset_x=0, offset_y=0;
1316 sub->start_display_time = 0;
1317 sub->end_display_time = ctx->time_out * 1000;
1321 offset_x = display_def->x;
1322 offset_y = display_def->y;
1325 sub->num_rects = ctx->display_list_size;
1327 if (sub->num_rects > 0){
1328 sub->rects = av_mallocz(sizeof(*sub->rects) * sub->num_rects);
1329 for(i=0; i<sub->num_rects; i++)
1330 sub->rects[i] = av_mallocz(sizeof(*sub->rects[i]));
1335 for (display = ctx->display_list; display; display = display->next) {
1336 region = get_region(ctx, display->region_id);
1337 rect = sub->rects[i];
1342 rect->x = display->x_pos + offset_x;
1343 rect->y = display->y_pos + offset_y;
1344 rect->w = region->width;
1345 rect->h = region->height;
1346 rect->nb_colors = 16;
1347 rect->type = SUBTITLE_BITMAP;
1348 rect->pict.linesize[0] = region->width;
1350 clut = get_clut(ctx, region->clut);
1353 clut = &default_clut;
1355 switch (region->depth) {
1357 clut_table = clut->clut4;
1360 clut_table = clut->clut256;
1364 clut_table = clut->clut16;
1368 rect->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
1369 memcpy(rect->pict.data[1], clut_table, (1 << region->depth) * sizeof(uint32_t));
1371 rect->pict.data[0] = av_malloc(region->buf_size);
1372 memcpy(rect->pict.data[0], region->pbuf, region->buf_size);
1380 save_display_set(ctx);
1386 static int dvbsub_decode(AVCodecContext *avctx,
1387 void *data, int *data_size,
1390 const uint8_t *buf = avpkt->data;
1391 int buf_size = avpkt->size;
1392 DVBSubContext *ctx = avctx->priv_data;
1393 AVSubtitle *sub = data;
1394 const uint8_t *p, *p_end;
1400 av_dlog(avctx, "DVB sub packet:\n");
1402 for (i=0; i < buf_size; i++) {
1403 av_dlog(avctx, "%02x ", buf[i]);
1405 av_dlog(avctx, "\n");
1409 av_dlog(avctx, "\n");
1411 if (buf_size <= 6 || *buf != 0x0f) {
1412 av_dlog(avctx, "incomplete or broken packet");
1417 p_end = buf + buf_size;
1419 while (p_end - p >= 6 && *p == 0x0f) {
1421 segment_type = *p++;
1422 page_id = AV_RB16(p);
1424 segment_length = AV_RB16(p);
1427 if (p_end - p < segment_length) {
1428 av_dlog(avctx, "incomplete or broken packet");
1432 if (page_id == ctx->composition_id || page_id == ctx->ancillary_id ||
1433 ctx->composition_id == -1 || ctx->ancillary_id == -1) {
1434 switch (segment_type) {
1435 case DVBSUB_PAGE_SEGMENT:
1436 dvbsub_parse_page_segment(avctx, p, segment_length);
1438 case DVBSUB_REGION_SEGMENT:
1439 dvbsub_parse_region_segment(avctx, p, segment_length);
1441 case DVBSUB_CLUT_SEGMENT:
1442 dvbsub_parse_clut_segment(avctx, p, segment_length);
1444 case DVBSUB_OBJECT_SEGMENT:
1445 dvbsub_parse_object_segment(avctx, p, segment_length);
1447 case DVBSUB_DISPLAYDEFINITION_SEGMENT:
1448 dvbsub_parse_display_definition_segment(avctx, p, segment_length);
1449 case DVBSUB_DISPLAY_SEGMENT:
1450 *data_size = dvbsub_display_end_segment(avctx, p, segment_length, sub);
1453 av_dlog(avctx, "Subtitling segment type 0x%x, page id %d, length %d\n",
1454 segment_type, page_id, segment_length);
1459 p += segment_length;
1466 AVCodec ff_dvbsub_decoder = {
1468 .type = AVMEDIA_TYPE_SUBTITLE,
1469 .id = CODEC_ID_DVB_SUBTITLE,
1470 .priv_data_size = sizeof(DVBSubContext),
1471 .init = dvbsub_init_decoder,
1472 .close = dvbsub_close_decoder,
1473 .decode = dvbsub_decode,
1474 .long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),