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"
26 #include "libavutil/colorspace.h"
28 #define DVBSUB_PAGE_SEGMENT 0x10
29 #define DVBSUB_REGION_SEGMENT 0x11
30 #define DVBSUB_CLUT_SEGMENT 0x12
31 #define DVBSUB_OBJECT_SEGMENT 0x13
32 #define DVBSUB_DISPLAYDEFINITION_SEGMENT 0x14
33 #define DVBSUB_DISPLAY_SEGMENT 0x80
35 #define cm (ff_crop_tab + 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) (((unsigned)(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) {
700 if (non_mod == 1 && bits == 1)
701 pixels_read += run_length;
707 while (run_length-- > 0 && pixels_read < dbuf_len) {
715 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
722 static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
723 const uint8_t *buf, int buf_size, int top_bottom, int non_mod)
725 DVBSubContext *ctx = avctx->priv_data;
727 DVBSubRegion *region = get_region(ctx, display->region_id);
728 const uint8_t *buf_end = buf + buf_size;
733 uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
734 uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
735 uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
736 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
739 ff_dlog(avctx, "DVB pixel block size %d, %s field:\n", buf_size,
740 top_bottom ? "bottom" : "top");
742 for (i = 0; i < buf_size; i++) {
744 ff_dlog(avctx, "0x%8p: ", buf+i);
746 ff_dlog(avctx, "%02x ", buf[i]);
748 ff_dlog(avctx, "\n");
752 ff_dlog(avctx, "\n");
759 x_pos = display->x_pos;
760 y_pos = display->y_pos;
762 if ((y_pos & 1) != top_bottom)
765 while (buf < buf_end) {
766 if (x_pos > region->width || y_pos > region->height) {
767 av_log(avctx, AV_LOG_ERROR, "Invalid object location!\n");
773 if (region->depth == 8)
775 else if (region->depth == 4)
780 x_pos += dvbsub_read_2bit_string(pbuf + (y_pos * region->width) + x_pos,
781 region->width - x_pos, &buf, buf_end - buf,
785 if (region->depth < 4) {
786 av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
790 if (region->depth == 8)
795 x_pos += dvbsub_read_4bit_string(pbuf + (y_pos * region->width) + x_pos,
796 region->width - x_pos, &buf, buf_end - buf,
800 if (region->depth < 8) {
801 av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
805 x_pos += dvbsub_read_8bit_string(pbuf + (y_pos * region->width) + x_pos,
806 region->width - x_pos, &buf, buf_end - buf,
811 map2to4[0] = (*buf) >> 4;
812 map2to4[1] = (*buf++) & 0xf;
813 map2to4[2] = (*buf) >> 4;
814 map2to4[3] = (*buf++) & 0xf;
817 for (i = 0; i < 4; i++)
821 for (i = 0; i < 16; i++)
826 x_pos = display->x_pos;
830 av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
836 static int dvbsub_parse_object_segment(AVCodecContext *avctx,
837 const uint8_t *buf, int buf_size)
839 DVBSubContext *ctx = avctx->priv_data;
841 const uint8_t *buf_end = buf + buf_size;
842 const uint8_t *block;
844 DVBSubObject *object;
845 DVBSubObjectDisplay *display;
846 int top_field_len, bottom_field_len;
848 int coding_method, non_modifying_color;
850 object_id = AV_RB16(buf);
853 object = get_object(ctx, object_id);
856 return AVERROR_INVALIDDATA;
858 coding_method = ((*buf) >> 2) & 3;
859 non_modifying_color = ((*buf++) >> 1) & 1;
861 if (coding_method == 0) {
862 top_field_len = AV_RB16(buf);
864 bottom_field_len = AV_RB16(buf);
867 if (buf + top_field_len + bottom_field_len > buf_end) {
868 av_log(avctx, AV_LOG_ERROR, "Field data size too large\n");
869 return AVERROR_INVALIDDATA;
872 for (display = object->display_list; display; display = display->object_list_next) {
875 dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
876 non_modifying_color);
878 if (bottom_field_len > 0)
879 block = buf + top_field_len;
881 bottom_field_len = top_field_len;
883 dvbsub_parse_pixel_data_block(avctx, display, block, bottom_field_len, 1,
884 non_modifying_color);
887 /* } else if (coding_method == 1) {*/
890 av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
896 static int dvbsub_parse_clut_segment(AVCodecContext *avctx,
897 const uint8_t *buf, int buf_size)
899 DVBSubContext *ctx = avctx->priv_data;
901 const uint8_t *buf_end = buf + buf_size;
904 int entry_id, depth , full_range;
905 int y, cr, cb, alpha;
906 int r, g, b, r_add, g_add, b_add;
908 ff_dlog(avctx, "DVB clut packet:\n");
910 for (i=0; i < buf_size; i++) {
911 ff_dlog(avctx, "%02x ", buf[i]);
913 ff_dlog(avctx, "\n");
917 ff_dlog(avctx, "\n");
922 clut = get_clut(ctx, clut_id);
925 clut = av_malloc(sizeof(DVBSubCLUT));
927 return AVERROR(ENOMEM);
929 memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
933 clut->next = ctx->clut_list;
934 ctx->clut_list = clut;
937 while (buf + 4 < buf_end) {
940 depth = (*buf) & 0xe0;
943 av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
944 return AVERROR_INVALIDDATA;
947 full_range = (*buf++) & 1;
956 cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
957 cb = (buf[1] << 2) & 0xf0;
958 alpha = (buf[1] << 6) & 0xc0;
966 YUV_TO_RGB1_CCIR(cb, cr);
967 YUV_TO_RGB2_CCIR(r, g, b, y);
969 ff_dlog(avctx, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
972 clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
974 clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
976 clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
983 static int dvbsub_parse_region_segment(AVCodecContext *avctx,
984 const uint8_t *buf, int buf_size)
986 DVBSubContext *ctx = avctx->priv_data;
988 const uint8_t *buf_end = buf + buf_size;
989 int region_id, object_id;
990 DVBSubRegion *region;
991 DVBSubObject *object;
992 DVBSubObjectDisplay *display;
996 return AVERROR_INVALIDDATA;
1000 region = get_region(ctx, region_id);
1003 region = av_mallocz(sizeof(DVBSubRegion));
1005 return AVERROR(ENOMEM);
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);
1027 return AVERROR(ENOMEM);
1032 region->depth = 1 << (((*buf++) >> 2) & 7);
1033 if(region->depth<2 || region->depth>8){
1034 av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);
1037 region->clut = *buf++;
1039 if (region->depth == 8)
1040 region->bgcolor = *buf++;
1044 if (region->depth == 4)
1045 region->bgcolor = (((*buf++) >> 4) & 15);
1047 region->bgcolor = (((*buf++) >> 2) & 3);
1050 ff_dlog(avctx, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
1053 memset(region->pbuf, region->bgcolor, region->buf_size);
1054 ff_dlog(avctx, "Fill region (%d)\n", region->bgcolor);
1057 delete_region_display_list(ctx, region);
1059 while (buf + 5 < buf_end) {
1060 object_id = AV_RB16(buf);
1063 object = get_object(ctx, object_id);
1066 object = av_mallocz(sizeof(DVBSubObject));
1068 return AVERROR(ENOMEM);
1070 object->id = object_id;
1071 object->next = ctx->object_list;
1072 ctx->object_list = object;
1075 object->type = (*buf) >> 6;
1077 display = av_mallocz(sizeof(DVBSubObjectDisplay));
1079 return AVERROR(ENOMEM);
1081 display->object_id = object_id;
1082 display->region_id = region_id;
1084 display->x_pos = AV_RB16(buf) & 0xfff;
1086 display->y_pos = AV_RB16(buf) & 0xfff;
1089 if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
1090 display->fgcolor = *buf++;
1091 display->bgcolor = *buf++;
1094 display->region_list_next = region->display_list;
1095 region->display_list = display;
1097 display->object_list_next = object->display_list;
1098 object->display_list = display;
1104 static int dvbsub_parse_page_segment(AVCodecContext *avctx,
1105 const uint8_t *buf, int buf_size)
1107 DVBSubContext *ctx = avctx->priv_data;
1108 DVBSubRegionDisplay *display;
1109 DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
1111 const uint8_t *buf_end = buf + buf_size;
1116 return AVERROR_INVALIDDATA;
1118 ctx->time_out = *buf++;
1119 page_state = ((*buf++) >> 2) & 3;
1121 ff_dlog(avctx, "Page time out %ds, state %d\n", ctx->time_out, page_state);
1123 if (page_state == 2) {
1127 tmp_display_list = ctx->display_list;
1128 ctx->display_list = NULL;
1129 ctx->display_list_size = 0;
1131 while (buf + 5 < buf_end) {
1135 display = tmp_display_list;
1136 tmp_ptr = &tmp_display_list;
1138 while (display && display->region_id != region_id) {
1139 tmp_ptr = &display->next;
1140 display = display->next;
1144 display = av_mallocz(sizeof(DVBSubRegionDisplay));
1146 return AVERROR(ENOMEM);
1149 display->region_id = region_id;
1151 display->x_pos = AV_RB16(buf);
1153 display->y_pos = AV_RB16(buf);
1156 *tmp_ptr = display->next;
1158 display->next = ctx->display_list;
1159 ctx->display_list = display;
1160 ctx->display_list_size++;
1162 ff_dlog(avctx, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
1165 while (tmp_display_list) {
1166 display = tmp_display_list;
1168 tmp_display_list = display->next;
1178 static void save_display_set(DVBSubContext *ctx)
1180 DVBSubRegion *region;
1181 DVBSubRegionDisplay *display;
1183 uint32_t *clut_table;
1184 int x_pos, y_pos, width, height;
1185 int x, y, y_off, x_off;
1188 static int fileno_index = 0;
1195 for (display = ctx->display_list; display; display = display->next) {
1196 region = get_region(ctx, display->region_id);
1199 x_pos = display->x_pos;
1200 y_pos = display->y_pos;
1201 width = region->width;
1202 height = region->height;
1204 if (display->x_pos < x_pos) {
1205 width += (x_pos - display->x_pos);
1206 x_pos = display->x_pos;
1209 if (display->y_pos < y_pos) {
1210 height += (y_pos - display->y_pos);
1211 y_pos = display->y_pos;
1214 if (display->x_pos + region->width > x_pos + width) {
1215 width = display->x_pos + region->width - x_pos;
1218 if (display->y_pos + region->height > y_pos + height) {
1219 height = display->y_pos + region->height - y_pos;
1226 pbuf = av_malloc(width * height * 4);
1228 return AVERROR(ENOMEM);
1230 for (display = ctx->display_list; display; display = display->next) {
1231 region = get_region(ctx, display->region_id);
1233 x_off = display->x_pos - x_pos;
1234 y_off = display->y_pos - y_pos;
1236 clut = get_clut(ctx, region->clut);
1239 clut = &default_clut;
1241 switch (region->depth) {
1243 clut_table = clut->clut4;
1246 clut_table = clut->clut256;
1250 clut_table = clut->clut16;
1254 for (y = 0; y < region->height; y++) {
1255 for (x = 0; x < region->width; x++) {
1256 pbuf[((y + y_off) * width) + x_off + x] =
1257 clut_table[region->pbuf[y * region->width + x]];
1263 snprintf(filename, sizeof(filename), "dvbs.%d", fileno_index);
1265 png_save2(filename, pbuf, width, height);
1274 static int dvbsub_parse_display_definition_segment(AVCodecContext *avctx,
1278 DVBSubContext *ctx = avctx->priv_data;
1279 DVBSubDisplayDefinition *display_def = ctx->display_definition;
1280 int dds_version, info_byte;
1283 return AVERROR_INVALIDDATA;
1285 info_byte = bytestream_get_byte(&buf);
1286 dds_version = info_byte >> 4;
1287 if (display_def && display_def->version == dds_version)
1288 return 0; // already have this display definition version
1291 display_def = av_mallocz(sizeof(*display_def));
1293 return AVERROR(ENOMEM);
1294 ctx->display_definition = display_def;
1297 display_def->version = dds_version;
1300 display_def->width = bytestream_get_be16(&buf) + 1;
1301 display_def->height = bytestream_get_be16(&buf) + 1;
1304 return AVERROR_INVALIDDATA;
1306 if (info_byte & 1<<3) { // display_window_flag
1307 display_def->x = bytestream_get_be16(&buf);
1308 display_def->y = bytestream_get_be16(&buf);
1309 display_def->width = bytestream_get_be16(&buf) - display_def->x + 1;
1310 display_def->height = bytestream_get_be16(&buf) - display_def->y + 1;
1316 static int dvbsub_display_end_segment(AVCodecContext *avctx, const uint8_t *buf,
1317 int buf_size, AVSubtitle *sub)
1319 DVBSubContext *ctx = avctx->priv_data;
1320 DVBSubDisplayDefinition *display_def = ctx->display_definition;
1322 DVBSubRegion *region;
1323 DVBSubRegionDisplay *display;
1324 AVSubtitleRect *rect;
1326 uint32_t *clut_table;
1328 int offset_x=0, offset_y=0;
1331 sub->start_display_time = 0;
1332 sub->end_display_time = ctx->time_out * 1000;
1336 offset_x = display_def->x;
1337 offset_y = display_def->y;
1340 sub->num_rects = ctx->display_list_size;
1341 if (sub->num_rects <= 0)
1342 return AVERROR_INVALIDDATA;
1344 sub->rects = av_mallocz_array(sub->num_rects * sub->num_rects,
1345 sizeof(*sub->rects));
1347 return AVERROR(ENOMEM);
1351 for (display = ctx->display_list; display; display = display->next) {
1352 region = get_region(ctx, display->region_id);
1353 rect = sub->rects[i];
1358 rect->x = display->x_pos + offset_x;
1359 rect->y = display->y_pos + offset_y;
1360 rect->w = region->width;
1361 rect->h = region->height;
1362 rect->nb_colors = 16;
1363 rect->type = SUBTITLE_BITMAP;
1364 rect->pict.linesize[0] = region->width;
1366 clut = get_clut(ctx, region->clut);
1369 clut = &default_clut;
1371 switch (region->depth) {
1373 clut_table = clut->clut4;
1376 clut_table = clut->clut256;
1380 clut_table = clut->clut16;
1384 rect->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
1385 if (!rect->pict.data[1]) {
1386 av_free(sub->rects);
1387 return AVERROR(ENOMEM);
1389 memcpy(rect->pict.data[1], clut_table, (1 << region->depth) * sizeof(uint32_t));
1391 rect->pict.data[0] = av_malloc(region->buf_size);
1392 if (!rect->pict.data[0]) {
1393 av_free(rect->pict.data[1]);
1394 av_free(sub->rects);
1395 return AVERROR(ENOMEM);
1397 memcpy(rect->pict.data[0], region->pbuf, region->buf_size);
1405 save_display_set(ctx);
1411 static int dvbsub_decode(AVCodecContext *avctx,
1412 void *data, int *data_size,
1415 const uint8_t *buf = avpkt->data;
1416 int buf_size = avpkt->size;
1417 DVBSubContext *ctx = avctx->priv_data;
1418 AVSubtitle *sub = data;
1419 const uint8_t *p, *p_end;
1425 ff_dlog(avctx, "DVB sub packet:\n");
1427 for (i=0; i < buf_size; i++) {
1428 ff_dlog(avctx, "%02x ", buf[i]);
1430 ff_dlog(avctx, "\n");
1434 ff_dlog(avctx, "\n");
1436 if (buf_size <= 6 || *buf != 0x0f) {
1437 ff_dlog(avctx, "incomplete or broken packet");
1438 return AVERROR_INVALIDDATA;
1442 p_end = buf + buf_size;
1444 while (p_end - p >= 6 && *p == 0x0f) {
1446 segment_type = *p++;
1447 page_id = AV_RB16(p);
1449 segment_length = AV_RB16(p);
1452 if (p_end - p < segment_length) {
1453 ff_dlog(avctx, "incomplete or broken packet");
1457 if (page_id == ctx->composition_id || page_id == ctx->ancillary_id ||
1458 ctx->composition_id == -1 || ctx->ancillary_id == -1) {
1460 switch (segment_type) {
1461 case DVBSUB_PAGE_SEGMENT:
1462 ret = dvbsub_parse_page_segment(avctx, p, segment_length);
1464 case DVBSUB_REGION_SEGMENT:
1465 ret = dvbsub_parse_region_segment(avctx, p, segment_length);
1467 case DVBSUB_CLUT_SEGMENT:
1468 ret = dvbsub_parse_clut_segment(avctx, p, segment_length);
1470 case DVBSUB_OBJECT_SEGMENT:
1471 ret = dvbsub_parse_object_segment(avctx, p, segment_length);
1473 case DVBSUB_DISPLAYDEFINITION_SEGMENT:
1474 ret = dvbsub_parse_display_definition_segment(avctx, p,
1477 case DVBSUB_DISPLAY_SEGMENT:
1478 ret = dvbsub_display_end_segment(avctx, p, segment_length, sub);
1482 ff_dlog(avctx, "Subtitling segment type 0x%x, page id %d, length %d\n",
1483 segment_type, page_id, segment_length);
1490 p += segment_length;
1497 AVCodec ff_dvbsub_decoder = {
1499 .long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),
1500 .type = AVMEDIA_TYPE_SUBTITLE,
1501 .id = AV_CODEC_ID_DVB_SUBTITLE,
1502 .priv_data_size = sizeof(DVBSubContext),
1503 .init = dvbsub_init_decoder,
1504 .close = dvbsub_close_decoder,
1505 .decode = dvbsub_decode,