2 * DVB subtitle decoding for ffmpeg
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)
39 static void png_save(const char *filename, uint8_t *bitmap, int w, int h,
40 uint32_t *rgba_palette)
44 char fname[40], fname2[40];
47 snprintf(fname, 40, "%s.ppm", filename);
49 f = fopen(fname, "w");
58 for(y = 0; y < h; y++) {
59 for(x = 0; x < w; x++) {
60 v = rgba_palette[bitmap[y * w + x]];
61 putc((v >> 16) & 0xff, f);
62 putc((v >> 8) & 0xff, f);
63 putc((v >> 0) & 0xff, f);
69 snprintf(fname2, 40, "%s-a.pgm", filename);
71 f = fopen(fname2, "w");
80 for(y = 0; y < h; y++) {
81 for(x = 0; x < w; x++) {
82 v = rgba_palette[bitmap[y * w + x]];
83 putc((v >> 24) & 0xff, f);
88 snprintf(command, 1024, "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
91 snprintf(command, 1024, "rm %s %s", fname, fname2);
96 static void png_save2(const char *filename, uint32_t *bitmap, int w, int h)
100 char fname[40], fname2[40];
103 snprintf(fname, sizeof(fname), "%s.ppm", filename);
105 f = fopen(fname, "w");
114 for(y = 0; y < h; y++) {
115 for(x = 0; x < w; x++) {
116 v = bitmap[y * w + x];
117 putc((v >> 16) & 0xff, f);
118 putc((v >> 8) & 0xff, f);
119 putc((v >> 0) & 0xff, f);
125 snprintf(fname2, sizeof(fname2), "%s-a.pgm", filename);
127 f = fopen(fname2, "w");
136 for(y = 0; y < h; y++) {
137 for(x = 0; x < w; x++) {
138 v = bitmap[y * w + x];
139 putc((v >> 24) & 0xff, f);
144 snprintf(command, sizeof(command), "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
147 snprintf(command, sizeof(command), "rm %s %s", fname, fname2);
152 #define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
154 typedef struct DVBSubCLUT {
159 uint32_t clut256[256];
161 struct DVBSubCLUT *next;
164 static DVBSubCLUT default_clut;
166 typedef struct DVBSubObjectDisplay {
176 struct DVBSubObjectDisplay *region_list_next;
177 struct DVBSubObjectDisplay *object_list_next;
178 } DVBSubObjectDisplay;
180 typedef struct DVBSubObject {
185 DVBSubObjectDisplay *display_list;
187 struct DVBSubObject *next;
190 typedef struct DVBSubRegionDisplay {
196 struct DVBSubRegionDisplay *next;
197 } DVBSubRegionDisplay;
199 typedef struct DVBSubRegion {
212 DVBSubObjectDisplay *display_list;
214 struct DVBSubRegion *next;
217 typedef struct DVBSubDisplayDefinition {
224 } DVBSubDisplayDefinition;
226 typedef struct DVBSubContext {
231 DVBSubRegion *region_list;
232 DVBSubCLUT *clut_list;
233 DVBSubObject *object_list;
235 int display_list_size;
236 DVBSubRegionDisplay *display_list;
237 DVBSubDisplayDefinition *display_definition;
241 static DVBSubObject* get_object(DVBSubContext *ctx, int object_id)
243 DVBSubObject *ptr = ctx->object_list;
245 while (ptr && ptr->id != object_id) {
252 static DVBSubCLUT* get_clut(DVBSubContext *ctx, int clut_id)
254 DVBSubCLUT *ptr = ctx->clut_list;
256 while (ptr && ptr->id != clut_id) {
263 static DVBSubRegion* get_region(DVBSubContext *ctx, int region_id)
265 DVBSubRegion *ptr = ctx->region_list;
267 while (ptr && ptr->id != region_id) {
274 static void delete_region_display_list(DVBSubContext *ctx, DVBSubRegion *region)
276 DVBSubObject *object, *obj2, **obj2_ptr;
277 DVBSubObjectDisplay *display, *obj_disp, **obj_disp_ptr;
279 while (region->display_list) {
280 display = region->display_list;
282 object = get_object(ctx, display->object_id);
285 obj_disp_ptr = &object->display_list;
286 obj_disp = *obj_disp_ptr;
288 while (obj_disp && obj_disp != display) {
289 obj_disp_ptr = &obj_disp->object_list_next;
290 obj_disp = *obj_disp_ptr;
294 *obj_disp_ptr = obj_disp->object_list_next;
296 if (!object->display_list) {
297 obj2_ptr = &ctx->object_list;
300 while (obj2 != object) {
302 obj2_ptr = &obj2->next;
306 *obj2_ptr = obj2->next;
313 region->display_list = display->region_list_next;
320 static void delete_state(DVBSubContext *ctx)
322 DVBSubRegion *region;
325 while (ctx->region_list) {
326 region = ctx->region_list;
328 ctx->region_list = region->next;
330 delete_region_display_list(ctx, region);
331 av_free(region->pbuf);
335 while (ctx->clut_list) {
336 clut = ctx->clut_list;
338 ctx->clut_list = clut->next;
343 av_freep(&ctx->display_definition);
345 /* Should already be null */
346 if (ctx->object_list)
347 av_log(0, AV_LOG_ERROR, "Memory deallocation error!\n");
350 static av_cold int dvbsub_init_decoder(AVCodecContext *avctx)
352 int i, r, g, b, a = 0;
353 DVBSubContext *ctx = avctx->priv_data;
355 if (!avctx->extradata || avctx->extradata_size != 4) {
356 av_log(avctx, AV_LOG_WARNING, "Invalid extradata, subtitle streams may be combined!\n");
357 ctx->composition_id = -1;
358 ctx->ancillary_id = -1;
360 ctx->composition_id = AV_RB16(avctx->extradata);
361 ctx->ancillary_id = AV_RB16(avctx->extradata + 2);
364 default_clut.id = -1;
365 default_clut.next = NULL;
367 default_clut.clut4[0] = RGBA( 0, 0, 0, 0);
368 default_clut.clut4[1] = RGBA(255, 255, 255, 255);
369 default_clut.clut4[2] = RGBA( 0, 0, 0, 255);
370 default_clut.clut4[3] = RGBA(127, 127, 127, 255);
372 default_clut.clut16[0] = RGBA( 0, 0, 0, 0);
373 for (i = 1; i < 16; i++) {
375 r = (i & 1) ? 255 : 0;
376 g = (i & 2) ? 255 : 0;
377 b = (i & 4) ? 255 : 0;
379 r = (i & 1) ? 127 : 0;
380 g = (i & 2) ? 127 : 0;
381 b = (i & 4) ? 127 : 0;
383 default_clut.clut16[i] = RGBA(r, g, b, 255);
386 default_clut.clut256[0] = RGBA( 0, 0, 0, 0);
387 for (i = 1; i < 256; i++) {
389 r = (i & 1) ? 255 : 0;
390 g = (i & 2) ? 255 : 0;
391 b = (i & 4) ? 255 : 0;
396 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
397 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
398 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
402 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
403 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
404 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
408 r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
409 g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
410 b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
414 r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
415 g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
416 b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
421 default_clut.clut256[i] = RGBA(r, g, b, a);
427 static av_cold int dvbsub_close_decoder(AVCodecContext *avctx)
429 DVBSubContext *ctx = avctx->priv_data;
430 DVBSubRegionDisplay *display;
434 while (ctx->display_list) {
435 display = ctx->display_list;
436 ctx->display_list = display->next;
444 static int dvbsub_read_2bit_string(uint8_t *destbuf, int dbuf_len,
445 const uint8_t **srcbuf, int buf_size,
446 int non_mod, uint8_t *map_table)
454 init_get_bits(&gb, *srcbuf, buf_size << 3);
456 while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
457 bits = get_bits(&gb, 2);
460 if (non_mod != 1 || bits != 1) {
462 *destbuf++ = map_table[bits];
468 bits = get_bits1(&gb);
470 run_length = get_bits(&gb, 3) + 3;
471 bits = get_bits(&gb, 2);
473 if (non_mod == 1 && bits == 1)
474 pixels_read += run_length;
477 bits = map_table[bits];
478 while (run_length-- > 0 && pixels_read < dbuf_len) {
484 bits = get_bits1(&gb);
486 bits = get_bits(&gb, 2);
488 run_length = get_bits(&gb, 4) + 12;
489 bits = get_bits(&gb, 2);
491 if (non_mod == 1 && bits == 1)
492 pixels_read += run_length;
495 bits = map_table[bits];
496 while (run_length-- > 0 && pixels_read < dbuf_len) {
501 } else if (bits == 3) {
502 run_length = get_bits(&gb, 8) + 29;
503 bits = get_bits(&gb, 2);
505 if (non_mod == 1 && bits == 1)
506 pixels_read += run_length;
509 bits = map_table[bits];
510 while (run_length-- > 0 && pixels_read < dbuf_len) {
515 } else if (bits == 1) {
521 if (pixels_read <= dbuf_len) {
526 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
541 if (get_bits(&gb, 6))
542 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
544 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
549 static int dvbsub_read_4bit_string(uint8_t *destbuf, int dbuf_len,
550 const uint8_t **srcbuf, int buf_size,
551 int non_mod, uint8_t *map_table)
559 init_get_bits(&gb, *srcbuf, buf_size << 3);
561 while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
562 bits = get_bits(&gb, 4);
565 if (non_mod != 1 || bits != 1) {
567 *destbuf++ = map_table[bits];
573 bits = get_bits1(&gb);
575 run_length = get_bits(&gb, 3);
577 if (run_length == 0) {
578 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
589 while (run_length-- > 0 && pixels_read < dbuf_len) {
594 bits = get_bits1(&gb);
596 run_length = get_bits(&gb, 2) + 4;
597 bits = get_bits(&gb, 4);
599 if (non_mod == 1 && bits == 1)
600 pixels_read += run_length;
603 bits = map_table[bits];
604 while (run_length-- > 0 && pixels_read < dbuf_len) {
610 bits = get_bits(&gb, 2);
612 run_length = get_bits(&gb, 4) + 9;
613 bits = get_bits(&gb, 4);
615 if (non_mod == 1 && bits == 1)
616 pixels_read += run_length;
619 bits = map_table[bits];
620 while (run_length-- > 0 && pixels_read < dbuf_len) {
625 } else if (bits == 3) {
626 run_length = get_bits(&gb, 8) + 25;
627 bits = get_bits(&gb, 4);
629 if (non_mod == 1 && bits == 1)
630 pixels_read += run_length;
633 bits = map_table[bits];
634 while (run_length-- > 0 && pixels_read < dbuf_len) {
639 } else if (bits == 1) {
645 if (pixels_read <= dbuf_len) {
662 if (get_bits(&gb, 8))
663 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
665 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
670 static int dvbsub_read_8bit_string(uint8_t *destbuf, int dbuf_len,
671 const uint8_t **srcbuf, int buf_size,
672 int non_mod, uint8_t *map_table)
674 const uint8_t *sbuf_end = (*srcbuf) + buf_size;
679 while (*srcbuf < sbuf_end && pixels_read < dbuf_len) {
683 if (non_mod != 1 || bits != 1) {
685 *destbuf++ = map_table[bits];
692 run_length = bits & 0x7f;
693 if ((bits & 0x80) == 0) {
694 if (run_length == 0) {
702 while (run_length-- > 0 && pixels_read < dbuf_len) {
709 if (non_mod == 1 && bits == 1)
710 pixels_read += run_length;
712 bits = map_table[bits];
713 else while (run_length-- > 0 && pixels_read < dbuf_len) {
722 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
729 static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
730 const uint8_t *buf, int buf_size, int top_bottom, int non_mod)
732 DVBSubContext *ctx = avctx->priv_data;
734 DVBSubRegion *region = get_region(ctx, display->region_id);
735 const uint8_t *buf_end = buf + buf_size;
740 uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
741 uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
742 uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
743 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
746 av_dlog(avctx, "DVB pixel block size %d, %s field:\n", buf_size,
747 top_bottom ? "bottom" : "top");
749 for (i = 0; i < buf_size; i++) {
751 av_dlog(avctx, "0x%8p: ", buf+i);
753 av_dlog(avctx, "%02x ", buf[i]);
755 av_dlog(avctx, "\n");
759 av_dlog(avctx, "\n");
766 x_pos = display->x_pos;
767 y_pos = display->y_pos;
769 if ((y_pos & 1) != top_bottom)
772 while (buf < buf_end) {
773 if (x_pos > region->width || y_pos > region->height) {
774 av_log(avctx, AV_LOG_ERROR, "Invalid object location!\n");
780 if (region->depth == 8)
782 else if (region->depth == 4)
787 x_pos += dvbsub_read_2bit_string(pbuf + (y_pos * region->width) + x_pos,
788 region->width - x_pos, &buf, buf_end - buf,
792 if (region->depth < 4) {
793 av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
797 if (region->depth == 8)
802 x_pos += dvbsub_read_4bit_string(pbuf + (y_pos * region->width) + x_pos,
803 region->width - x_pos, &buf, buf_end - buf,
807 if (region->depth < 8) {
808 av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
812 x_pos += dvbsub_read_8bit_string(pbuf + (y_pos * region->width) + x_pos,
813 region->width - x_pos, &buf, buf_end - buf,
818 map2to4[0] = (*buf) >> 4;
819 map2to4[1] = (*buf++) & 0xf;
820 map2to4[2] = (*buf) >> 4;
821 map2to4[3] = (*buf++) & 0xf;
824 for (i = 0; i < 4; i++)
828 for (i = 0; i < 16; i++)
833 x_pos = display->x_pos;
837 av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
843 static void dvbsub_parse_object_segment(AVCodecContext *avctx,
844 const uint8_t *buf, int buf_size)
846 DVBSubContext *ctx = avctx->priv_data;
848 const uint8_t *buf_end = buf + buf_size;
849 const uint8_t *block;
851 DVBSubObject *object;
852 DVBSubObjectDisplay *display;
853 int top_field_len, bottom_field_len;
855 int coding_method, non_modifying_color;
857 object_id = AV_RB16(buf);
860 object = get_object(ctx, object_id);
865 coding_method = ((*buf) >> 2) & 3;
866 non_modifying_color = ((*buf++) >> 1) & 1;
868 if (coding_method == 0) {
869 top_field_len = AV_RB16(buf);
871 bottom_field_len = AV_RB16(buf);
874 if (buf + top_field_len + bottom_field_len > buf_end) {
875 av_log(avctx, AV_LOG_ERROR, "Field data size too large\n");
879 for (display = object->display_list; display; display = display->object_list_next) {
882 dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
883 non_modifying_color);
885 if (bottom_field_len > 0)
886 block = buf + top_field_len;
888 bottom_field_len = top_field_len;
890 dvbsub_parse_pixel_data_block(avctx, display, block, bottom_field_len, 1,
891 non_modifying_color);
894 /* } else if (coding_method == 1) {*/
897 av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
902 static void dvbsub_parse_clut_segment(AVCodecContext *avctx,
903 const uint8_t *buf, int buf_size)
905 DVBSubContext *ctx = avctx->priv_data;
907 const uint8_t *buf_end = buf + buf_size;
910 int entry_id, depth , full_range;
911 int y, cr, cb, alpha;
912 int r, g, b, r_add, g_add, b_add;
914 av_dlog(avctx, "DVB clut packet:\n");
916 for (i=0; i < buf_size; i++) {
917 av_dlog(avctx, "%02x ", buf[i]);
919 av_dlog(avctx, "\n");
923 av_dlog(avctx, "\n");
928 clut = get_clut(ctx, clut_id);
931 clut = av_malloc(sizeof(DVBSubCLUT));
933 memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
937 clut->next = ctx->clut_list;
938 ctx->clut_list = clut;
941 while (buf + 4 < buf_end) {
944 depth = (*buf) & 0xe0;
947 av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
951 full_range = (*buf++) & 1;
960 cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
961 cb = (buf[1] << 2) & 0xf0;
962 alpha = (buf[1] << 6) & 0xc0;
970 YUV_TO_RGB1_CCIR(cb, cr);
971 YUV_TO_RGB2_CCIR(r, g, b, y);
973 av_dlog(avctx, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
976 clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
978 clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
980 clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
985 static void dvbsub_parse_region_segment(AVCodecContext *avctx,
986 const uint8_t *buf, int buf_size)
988 DVBSubContext *ctx = avctx->priv_data;
990 const uint8_t *buf_end = buf + buf_size;
991 int region_id, object_id;
992 DVBSubRegion *region;
993 DVBSubObject *object;
994 DVBSubObjectDisplay *display;
1002 region = get_region(ctx, region_id);
1005 region = av_mallocz(sizeof(DVBSubRegion));
1007 region->id = region_id;
1009 region->next = ctx->region_list;
1010 ctx->region_list = region;
1013 fill = ((*buf++) >> 3) & 1;
1015 region->width = AV_RB16(buf);
1017 region->height = AV_RB16(buf);
1020 if (region->width * region->height != region->buf_size) {
1021 av_free(region->pbuf);
1023 region->buf_size = region->width * region->height;
1025 region->pbuf = av_malloc(region->buf_size);
1030 region->depth = 1 << (((*buf++) >> 2) & 7);
1031 if(region->depth<2 || region->depth>8){
1032 av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);
1035 region->clut = *buf++;
1037 if (region->depth == 8)
1038 region->bgcolor = *buf++;
1042 if (region->depth == 4)
1043 region->bgcolor = (((*buf++) >> 4) & 15);
1045 region->bgcolor = (((*buf++) >> 2) & 3);
1048 av_dlog(avctx, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
1051 memset(region->pbuf, region->bgcolor, region->buf_size);
1052 av_dlog(avctx, "Fill region (%d)\n", region->bgcolor);
1055 delete_region_display_list(ctx, region);
1057 while (buf + 5 < buf_end) {
1058 object_id = AV_RB16(buf);
1061 object = get_object(ctx, object_id);
1064 object = av_mallocz(sizeof(DVBSubObject));
1066 object->id = object_id;
1067 object->next = ctx->object_list;
1068 ctx->object_list = object;
1071 object->type = (*buf) >> 6;
1073 display = av_mallocz(sizeof(DVBSubObjectDisplay));
1075 display->object_id = object_id;
1076 display->region_id = region_id;
1078 display->x_pos = AV_RB16(buf) & 0xfff;
1080 display->y_pos = AV_RB16(buf) & 0xfff;
1083 if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
1084 display->fgcolor = *buf++;
1085 display->bgcolor = *buf++;
1088 display->region_list_next = region->display_list;
1089 region->display_list = display;
1091 display->object_list_next = object->display_list;
1092 object->display_list = display;
1096 static void dvbsub_parse_page_segment(AVCodecContext *avctx,
1097 const uint8_t *buf, int buf_size)
1099 DVBSubContext *ctx = avctx->priv_data;
1100 DVBSubRegionDisplay *display;
1101 DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
1103 const uint8_t *buf_end = buf + buf_size;
1110 ctx->time_out = *buf++;
1111 page_state = ((*buf++) >> 2) & 3;
1113 av_dlog(avctx, "Page time out %ds, state %d\n", ctx->time_out, page_state);
1115 if (page_state == 2) {
1119 tmp_display_list = ctx->display_list;
1120 ctx->display_list = NULL;
1121 ctx->display_list_size = 0;
1123 while (buf + 5 < buf_end) {
1127 display = tmp_display_list;
1128 tmp_ptr = &tmp_display_list;
1130 while (display && display->region_id != region_id) {
1131 tmp_ptr = &display->next;
1132 display = display->next;
1136 display = av_mallocz(sizeof(DVBSubRegionDisplay));
1138 display->region_id = region_id;
1140 display->x_pos = AV_RB16(buf);
1142 display->y_pos = AV_RB16(buf);
1145 *tmp_ptr = display->next;
1147 display->next = ctx->display_list;
1148 ctx->display_list = display;
1149 ctx->display_list_size++;
1151 av_dlog(avctx, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
1154 while (tmp_display_list) {
1155 display = tmp_display_list;
1157 tmp_display_list = display->next;
1166 static void save_display_set(DVBSubContext *ctx)
1168 DVBSubRegion *region;
1169 DVBSubRegionDisplay *display;
1171 uint32_t *clut_table;
1172 int x_pos, y_pos, width, height;
1173 int x, y, y_off, x_off;
1176 static int fileno_index = 0;
1183 for (display = ctx->display_list; display; display = display->next) {
1184 region = get_region(ctx, display->region_id);
1187 x_pos = display->x_pos;
1188 y_pos = display->y_pos;
1189 width = region->width;
1190 height = region->height;
1192 if (display->x_pos < x_pos) {
1193 width += (x_pos - display->x_pos);
1194 x_pos = display->x_pos;
1197 if (display->y_pos < y_pos) {
1198 height += (y_pos - display->y_pos);
1199 y_pos = display->y_pos;
1202 if (display->x_pos + region->width > x_pos + width) {
1203 width = display->x_pos + region->width - x_pos;
1206 if (display->y_pos + region->height > y_pos + height) {
1207 height = display->y_pos + region->height - y_pos;
1214 pbuf = av_malloc(width * height * 4);
1216 for (display = ctx->display_list; display; display = display->next) {
1217 region = get_region(ctx, display->region_id);
1219 x_off = display->x_pos - x_pos;
1220 y_off = display->y_pos - y_pos;
1222 clut = get_clut(ctx, region->clut);
1225 clut = &default_clut;
1227 switch (region->depth) {
1229 clut_table = clut->clut4;
1232 clut_table = clut->clut256;
1236 clut_table = clut->clut16;
1240 for (y = 0; y < region->height; y++) {
1241 for (x = 0; x < region->width; x++) {
1242 pbuf[((y + y_off) * width) + x_off + x] =
1243 clut_table[region->pbuf[y * region->width + x]];
1249 snprintf(filename, sizeof(filename), "dvbs.%d", fileno_index);
1251 png_save2(filename, pbuf, width, height);
1260 static void dvbsub_parse_display_definition_segment(AVCodecContext *avctx,
1264 DVBSubContext *ctx = avctx->priv_data;
1265 DVBSubDisplayDefinition *display_def = ctx->display_definition;
1266 int dds_version, info_byte;
1271 info_byte = bytestream_get_byte(&buf);
1272 dds_version = info_byte >> 4;
1273 if (display_def && display_def->version == dds_version)
1274 return; // already have this display definition version
1277 display_def = av_mallocz(sizeof(*display_def));
1278 ctx->display_definition = display_def;
1283 display_def->version = dds_version;
1286 display_def->width = bytestream_get_be16(&buf) + 1;
1287 display_def->height = bytestream_get_be16(&buf) + 1;
1292 if (info_byte & 1<<3) { // display_window_flag
1293 display_def->x = bytestream_get_be16(&buf);
1294 display_def->y = bytestream_get_be16(&buf);
1295 display_def->width = bytestream_get_be16(&buf) - display_def->x + 1;
1296 display_def->height = bytestream_get_be16(&buf) - display_def->y + 1;
1300 static int dvbsub_display_end_segment(AVCodecContext *avctx, const uint8_t *buf,
1301 int buf_size, AVSubtitle *sub)
1303 DVBSubContext *ctx = avctx->priv_data;
1304 DVBSubDisplayDefinition *display_def = ctx->display_definition;
1306 DVBSubRegion *region;
1307 DVBSubRegionDisplay *display;
1308 AVSubtitleRect *rect;
1310 uint32_t *clut_table;
1312 int offset_x=0, offset_y=0;
1315 sub->start_display_time = 0;
1316 sub->end_display_time = ctx->time_out * 1000;
1320 offset_x = display_def->x;
1321 offset_y = display_def->y;
1324 sub->num_rects = ctx->display_list_size;
1326 if (sub->num_rects > 0){
1327 sub->rects = av_mallocz(sizeof(*sub->rects) * sub->num_rects);
1328 for(i=0; i<sub->num_rects; i++)
1329 sub->rects[i] = av_mallocz(sizeof(*sub->rects[i]));
1334 for (display = ctx->display_list; display; display = display->next) {
1335 region = get_region(ctx, display->region_id);
1336 rect = sub->rects[i];
1341 rect->x = display->x_pos + offset_x;
1342 rect->y = display->y_pos + offset_y;
1343 rect->w = region->width;
1344 rect->h = region->height;
1345 rect->nb_colors = 16;
1346 rect->type = SUBTITLE_BITMAP;
1347 rect->pict.linesize[0] = region->width;
1349 clut = get_clut(ctx, region->clut);
1352 clut = &default_clut;
1354 switch (region->depth) {
1356 clut_table = clut->clut4;
1359 clut_table = clut->clut256;
1363 clut_table = clut->clut16;
1367 rect->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
1368 memcpy(rect->pict.data[1], clut_table, (1 << region->depth) * sizeof(uint32_t));
1370 rect->pict.data[0] = av_malloc(region->buf_size);
1371 memcpy(rect->pict.data[0], region->pbuf, region->buf_size);
1379 save_display_set(ctx);
1385 static int dvbsub_decode(AVCodecContext *avctx,
1386 void *data, int *data_size,
1389 const uint8_t *buf = avpkt->data;
1390 int buf_size = avpkt->size;
1391 DVBSubContext *ctx = avctx->priv_data;
1392 AVSubtitle *sub = data;
1393 const uint8_t *p, *p_end;
1399 av_dlog(avctx, "DVB sub packet:\n");
1401 for (i=0; i < buf_size; i++) {
1402 av_dlog(avctx, "%02x ", buf[i]);
1404 av_dlog(avctx, "\n");
1408 av_dlog(avctx, "\n");
1410 if (buf_size <= 6 || *buf != 0x0f) {
1411 av_dlog(avctx, "incomplete or broken packet");
1416 p_end = buf + buf_size;
1418 while (p_end - p >= 6 && *p == 0x0f) {
1420 segment_type = *p++;
1421 page_id = AV_RB16(p);
1423 segment_length = AV_RB16(p);
1426 if (p_end - p < segment_length) {
1427 av_dlog(avctx, "incomplete or broken packet");
1431 if (page_id == ctx->composition_id || page_id == ctx->ancillary_id ||
1432 ctx->composition_id == -1 || ctx->ancillary_id == -1) {
1433 switch (segment_type) {
1434 case DVBSUB_PAGE_SEGMENT:
1435 dvbsub_parse_page_segment(avctx, p, segment_length);
1437 case DVBSUB_REGION_SEGMENT:
1438 dvbsub_parse_region_segment(avctx, p, segment_length);
1440 case DVBSUB_CLUT_SEGMENT:
1441 dvbsub_parse_clut_segment(avctx, p, segment_length);
1443 case DVBSUB_OBJECT_SEGMENT:
1444 dvbsub_parse_object_segment(avctx, p, segment_length);
1446 case DVBSUB_DISPLAYDEFINITION_SEGMENT:
1447 dvbsub_parse_display_definition_segment(avctx, p, segment_length);
1448 case DVBSUB_DISPLAY_SEGMENT:
1449 *data_size = dvbsub_display_end_segment(avctx, p, segment_length, sub);
1452 av_dlog(avctx, "Subtitling segment type 0x%x, page id %d, length %d\n",
1453 segment_type, page_id, segment_length);
1458 p += segment_length;
1465 AVCodec ff_dvbsub_decoder = {
1467 AVMEDIA_TYPE_SUBTITLE,
1468 CODEC_ID_DVB_SUBTITLE,
1469 sizeof(DVBSubContext),
1470 dvbsub_init_decoder,
1472 dvbsub_close_decoder,
1474 .long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),