2 * DVB subtitle decoding for ffmpeg
3 * Copyright (c) 2005 Ian Caulfield
5 * This file is part of FFmpeg.
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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"
28 //#define DEBUG_PACKET_CONTENTS
29 //#define DEBUG_SAVE_IMAGES
31 #define DVBSUB_PAGE_SEGMENT 0x10
32 #define DVBSUB_REGION_SEGMENT 0x11
33 #define DVBSUB_CLUT_SEGMENT 0x12
34 #define DVBSUB_OBJECT_SEGMENT 0x13
35 #define DVBSUB_DISPLAYDEFINITION_SEGMENT 0x14
36 #define DVBSUB_DISPLAY_SEGMENT 0x80
38 #define cm (ff_cropTbl + MAX_NEG_CROP)
40 #ifdef DEBUG_SAVE_IMAGES
43 static void png_save(const char *filename, uint8_t *bitmap, int w, int h,
44 uint32_t *rgba_palette)
48 char fname[40], fname2[40];
51 snprintf(fname, 40, "%s.ppm", filename);
53 f = fopen(fname, "w");
62 for(y = 0; y < h; y++) {
63 for(x = 0; x < w; x++) {
64 v = rgba_palette[bitmap[y * w + x]];
65 putc((v >> 16) & 0xff, f);
66 putc((v >> 8) & 0xff, f);
67 putc((v >> 0) & 0xff, f);
73 snprintf(fname2, 40, "%s-a.pgm", filename);
75 f = fopen(fname2, "w");
84 for(y = 0; y < h; y++) {
85 for(x = 0; x < w; x++) {
86 v = rgba_palette[bitmap[y * w + x]];
87 putc((v >> 24) & 0xff, f);
92 snprintf(command, 1024, "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
95 snprintf(command, 1024, "rm %s %s", fname, fname2);
100 static void png_save2(const char *filename, uint32_t *bitmap, int w, int h)
104 char fname[40], fname2[40];
107 snprintf(fname, sizeof(fname), "%s.ppm", filename);
109 f = fopen(fname, "w");
118 for(y = 0; y < h; y++) {
119 for(x = 0; x < w; x++) {
120 v = bitmap[y * w + x];
121 putc((v >> 16) & 0xff, f);
122 putc((v >> 8) & 0xff, f);
123 putc((v >> 0) & 0xff, f);
129 snprintf(fname2, sizeof(fname2), "%s-a.pgm", filename);
131 f = fopen(fname2, "w");
140 for(y = 0; y < h; y++) {
141 for(x = 0; x < w; x++) {
142 v = bitmap[y * w + x];
143 putc((v >> 24) & 0xff, f);
148 snprintf(command, sizeof(command), "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
151 snprintf(command, sizeof(command), "rm %s %s", fname, fname2);
156 #define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
158 typedef struct DVBSubCLUT {
163 uint32_t clut256[256];
165 struct DVBSubCLUT *next;
168 static DVBSubCLUT default_clut;
170 typedef struct DVBSubObjectDisplay {
180 struct DVBSubObjectDisplay *region_list_next;
181 struct DVBSubObjectDisplay *object_list_next;
182 } DVBSubObjectDisplay;
184 typedef struct DVBSubObject {
189 DVBSubObjectDisplay *display_list;
191 struct DVBSubObject *next;
194 typedef struct DVBSubRegionDisplay {
200 struct DVBSubRegionDisplay *next;
201 } DVBSubRegionDisplay;
203 typedef struct DVBSubRegion {
216 DVBSubObjectDisplay *display_list;
218 struct DVBSubRegion *next;
221 typedef struct DVBSubDisplayDefinition {
228 } DVBSubDisplayDefinition;
230 typedef struct DVBSubContext {
235 DVBSubRegion *region_list;
236 DVBSubCLUT *clut_list;
237 DVBSubObject *object_list;
239 int display_list_size;
240 DVBSubRegionDisplay *display_list;
241 DVBSubDisplayDefinition *display_definition;
245 static DVBSubObject* get_object(DVBSubContext *ctx, int object_id)
247 DVBSubObject *ptr = ctx->object_list;
249 while (ptr && ptr->id != object_id) {
256 static DVBSubCLUT* get_clut(DVBSubContext *ctx, int clut_id)
258 DVBSubCLUT *ptr = ctx->clut_list;
260 while (ptr && ptr->id != clut_id) {
267 static DVBSubRegion* get_region(DVBSubContext *ctx, int region_id)
269 DVBSubRegion *ptr = ctx->region_list;
271 while (ptr && ptr->id != region_id) {
278 static void delete_region_display_list(DVBSubContext *ctx, DVBSubRegion *region)
280 DVBSubObject *object, *obj2, **obj2_ptr;
281 DVBSubObjectDisplay *display, *obj_disp, **obj_disp_ptr;
283 while (region->display_list) {
284 display = region->display_list;
286 object = get_object(ctx, display->object_id);
289 obj_disp_ptr = &object->display_list;
290 obj_disp = *obj_disp_ptr;
292 while (obj_disp && obj_disp != display) {
293 obj_disp_ptr = &obj_disp->object_list_next;
294 obj_disp = *obj_disp_ptr;
298 *obj_disp_ptr = obj_disp->object_list_next;
300 if (!object->display_list) {
301 obj2_ptr = &ctx->object_list;
304 while (obj2 != object) {
306 obj2_ptr = &obj2->next;
310 *obj2_ptr = obj2->next;
317 region->display_list = display->region_list_next;
324 static void delete_state(DVBSubContext *ctx)
326 DVBSubRegion *region;
329 while (ctx->region_list) {
330 region = ctx->region_list;
332 ctx->region_list = region->next;
334 delete_region_display_list(ctx, region);
336 av_free(region->pbuf);
341 while (ctx->clut_list) {
342 clut = ctx->clut_list;
344 ctx->clut_list = clut->next;
349 av_freep(&ctx->display_definition);
351 /* Should already be null */
352 if (ctx->object_list)
353 av_log(0, AV_LOG_ERROR, "Memory deallocation error!\n");
356 static av_cold int dvbsub_init_decoder(AVCodecContext *avctx)
358 int i, r, g, b, a = 0;
359 DVBSubContext *ctx = avctx->priv_data;
361 if (!avctx->extradata || avctx->extradata_size != 4) {
362 av_log(avctx, AV_LOG_WARNING, "Invalid extradata, subtitle streams may be combined!\n");
363 ctx->composition_id = -1;
364 ctx->ancillary_id = -1;
366 ctx->composition_id = AV_RB16(avctx->extradata);
367 ctx->ancillary_id = AV_RB16(avctx->extradata + 2);
370 default_clut.id = -1;
371 default_clut.next = NULL;
373 default_clut.clut4[0] = RGBA( 0, 0, 0, 0);
374 default_clut.clut4[1] = RGBA(255, 255, 255, 255);
375 default_clut.clut4[2] = RGBA( 0, 0, 0, 255);
376 default_clut.clut4[3] = RGBA(127, 127, 127, 255);
378 default_clut.clut16[0] = RGBA( 0, 0, 0, 0);
379 for (i = 1; i < 16; i++) {
381 r = (i & 1) ? 255 : 0;
382 g = (i & 2) ? 255 : 0;
383 b = (i & 4) ? 255 : 0;
385 r = (i & 1) ? 127 : 0;
386 g = (i & 2) ? 127 : 0;
387 b = (i & 4) ? 127 : 0;
389 default_clut.clut16[i] = RGBA(r, g, b, 255);
392 default_clut.clut256[0] = RGBA( 0, 0, 0, 0);
393 for (i = 1; i < 256; i++) {
395 r = (i & 1) ? 255 : 0;
396 g = (i & 2) ? 255 : 0;
397 b = (i & 4) ? 255 : 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 = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
409 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
410 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
414 r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
415 g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
416 b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
420 r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
421 g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
422 b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
427 default_clut.clut256[i] = RGBA(r, g, b, a);
433 static av_cold int dvbsub_close_decoder(AVCodecContext *avctx)
435 DVBSubContext *ctx = avctx->priv_data;
436 DVBSubRegionDisplay *display;
440 while (ctx->display_list) {
441 display = ctx->display_list;
442 ctx->display_list = display->next;
450 static int dvbsub_read_2bit_string(uint8_t *destbuf, int dbuf_len,
451 const uint8_t **srcbuf, int buf_size,
452 int non_mod, uint8_t *map_table)
460 init_get_bits(&gb, *srcbuf, buf_size << 3);
462 while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
463 bits = get_bits(&gb, 2);
466 if (non_mod != 1 || bits != 1) {
468 *destbuf++ = map_table[bits];
474 bits = get_bits1(&gb);
476 run_length = get_bits(&gb, 3) + 3;
477 bits = get_bits(&gb, 2);
479 if (non_mod == 1 && bits == 1)
480 pixels_read += run_length;
483 bits = map_table[bits];
484 while (run_length-- > 0 && pixels_read < dbuf_len) {
490 bits = get_bits1(&gb);
492 bits = get_bits(&gb, 2);
494 run_length = get_bits(&gb, 4) + 12;
495 bits = get_bits(&gb, 2);
497 if (non_mod == 1 && bits == 1)
498 pixels_read += run_length;
501 bits = map_table[bits];
502 while (run_length-- > 0 && pixels_read < dbuf_len) {
507 } else if (bits == 3) {
508 run_length = get_bits(&gb, 8) + 29;
509 bits = get_bits(&gb, 2);
511 if (non_mod == 1 && bits == 1)
512 pixels_read += run_length;
515 bits = map_table[bits];
516 while (run_length-- > 0 && pixels_read < dbuf_len) {
521 } else if (bits == 1) {
527 if (pixels_read <= dbuf_len) {
532 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
547 if (get_bits(&gb, 6))
548 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
550 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
555 static int dvbsub_read_4bit_string(uint8_t *destbuf, int dbuf_len,
556 const uint8_t **srcbuf, int buf_size,
557 int non_mod, uint8_t *map_table)
565 init_get_bits(&gb, *srcbuf, buf_size << 3);
567 while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
568 bits = get_bits(&gb, 4);
571 if (non_mod != 1 || bits != 1) {
573 *destbuf++ = map_table[bits];
579 bits = get_bits1(&gb);
581 run_length = get_bits(&gb, 3);
583 if (run_length == 0) {
584 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
595 while (run_length-- > 0 && pixels_read < dbuf_len) {
600 bits = get_bits1(&gb);
602 run_length = get_bits(&gb, 2) + 4;
603 bits = get_bits(&gb, 4);
605 if (non_mod == 1 && bits == 1)
606 pixels_read += run_length;
609 bits = map_table[bits];
610 while (run_length-- > 0 && pixels_read < dbuf_len) {
616 bits = get_bits(&gb, 2);
618 run_length = get_bits(&gb, 4) + 9;
619 bits = get_bits(&gb, 4);
621 if (non_mod == 1 && bits == 1)
622 pixels_read += run_length;
625 bits = map_table[bits];
626 while (run_length-- > 0 && pixels_read < dbuf_len) {
631 } else if (bits == 3) {
632 run_length = get_bits(&gb, 8) + 25;
633 bits = get_bits(&gb, 4);
635 if (non_mod == 1 && bits == 1)
636 pixels_read += run_length;
639 bits = map_table[bits];
640 while (run_length-- > 0 && pixels_read < dbuf_len) {
645 } else if (bits == 1) {
651 if (pixels_read <= dbuf_len) {
668 if (get_bits(&gb, 8))
669 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
671 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
676 static int dvbsub_read_8bit_string(uint8_t *destbuf, int dbuf_len,
677 const uint8_t **srcbuf, int buf_size,
678 int non_mod, uint8_t *map_table)
680 const uint8_t *sbuf_end = (*srcbuf) + buf_size;
685 while (*srcbuf < sbuf_end && pixels_read < dbuf_len) {
689 if (non_mod != 1 || bits != 1) {
691 *destbuf++ = map_table[bits];
698 run_length = bits & 0x7f;
699 if ((bits & 0x80) == 0) {
700 if (run_length == 0) {
708 while (run_length-- > 0 && pixels_read < dbuf_len) {
715 if (non_mod == 1 && bits == 1)
716 pixels_read += run_length;
718 bits = map_table[bits];
719 else while (run_length-- > 0 && pixels_read < dbuf_len) {
728 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
735 static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
736 const uint8_t *buf, int buf_size, int top_bottom, int non_mod)
738 DVBSubContext *ctx = avctx->priv_data;
740 DVBSubRegion *region = get_region(ctx, display->region_id);
741 const uint8_t *buf_end = buf + buf_size;
746 uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
747 uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
748 uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
749 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
752 dprintf(avctx, "DVB pixel block size %d, %s field:\n", buf_size,
753 top_bottom ? "bottom" : "top");
755 #ifdef DEBUG_PACKET_CONTENTS
756 for (i = 0; i < buf_size; i++) {
758 av_log(avctx, AV_LOG_INFO, "0x%08p: ", buf+i);
760 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
762 av_log(avctx, AV_LOG_INFO, "\n");
766 av_log(avctx, AV_LOG_INFO, "\n");
775 x_pos = display->x_pos;
776 y_pos = display->y_pos;
778 if ((y_pos & 1) != top_bottom)
781 while (buf < buf_end) {
782 if (x_pos > region->width || y_pos > region->height) {
783 av_log(avctx, AV_LOG_ERROR, "Invalid object location!\n");
789 if (region->depth == 8)
791 else if (region->depth == 4)
796 x_pos += dvbsub_read_2bit_string(pbuf + (y_pos * region->width) + x_pos,
797 region->width - x_pos, &buf, buf_size,
801 if (region->depth < 4) {
802 av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
806 if (region->depth == 8)
811 x_pos += dvbsub_read_4bit_string(pbuf + (y_pos * region->width) + x_pos,
812 region->width - x_pos, &buf, buf_size,
816 if (region->depth < 8) {
817 av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
821 x_pos += dvbsub_read_8bit_string(pbuf + (y_pos * region->width) + x_pos,
822 region->width - x_pos, &buf, buf_size,
827 map2to4[0] = (*buf) >> 4;
828 map2to4[1] = (*buf++) & 0xf;
829 map2to4[2] = (*buf) >> 4;
830 map2to4[3] = (*buf++) & 0xf;
833 for (i = 0; i < 4; i++)
837 for (i = 0; i < 16; i++)
842 x_pos = display->x_pos;
846 av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
852 static void dvbsub_parse_object_segment(AVCodecContext *avctx,
853 const uint8_t *buf, int buf_size)
855 DVBSubContext *ctx = avctx->priv_data;
857 const uint8_t *buf_end = buf + buf_size;
858 const uint8_t *block;
860 DVBSubObject *object;
861 DVBSubObjectDisplay *display;
862 int top_field_len, bottom_field_len;
864 int coding_method, non_modifying_color;
866 object_id = AV_RB16(buf);
869 object = get_object(ctx, object_id);
874 coding_method = ((*buf) >> 2) & 3;
875 non_modifying_color = ((*buf++) >> 1) & 1;
877 if (coding_method == 0) {
878 top_field_len = AV_RB16(buf);
880 bottom_field_len = AV_RB16(buf);
883 if (buf + top_field_len + bottom_field_len > buf_end) {
884 av_log(avctx, AV_LOG_ERROR, "Field data size too large\n");
888 for (display = object->display_list; display; display = display->object_list_next) {
891 dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
892 non_modifying_color);
894 if (bottom_field_len > 0)
895 block = buf + top_field_len;
897 bottom_field_len = top_field_len;
899 dvbsub_parse_pixel_data_block(avctx, display, block, bottom_field_len, 1,
900 non_modifying_color);
903 /* } else if (coding_method == 1) {*/
906 av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
911 static void dvbsub_parse_clut_segment(AVCodecContext *avctx,
912 const uint8_t *buf, int buf_size)
914 DVBSubContext *ctx = avctx->priv_data;
916 const uint8_t *buf_end = buf + buf_size;
919 int entry_id, depth , full_range;
920 int y, cr, cb, alpha;
921 int r, g, b, r_add, g_add, b_add;
923 #ifdef DEBUG_PACKET_CONTENTS
926 av_log(avctx, AV_LOG_INFO, "DVB clut packet:\n");
928 for (i=0; i < buf_size; i++) {
929 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
931 av_log(avctx, AV_LOG_INFO, "\n");
935 av_log(avctx, AV_LOG_INFO, "\n");
942 clut = get_clut(ctx, clut_id);
945 clut = av_malloc(sizeof(DVBSubCLUT));
947 memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
951 clut->next = ctx->clut_list;
952 ctx->clut_list = clut;
955 while (buf + 4 < buf_end) {
958 depth = (*buf) & 0xe0;
961 av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
965 full_range = (*buf++) & 1;
974 cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
975 cb = (buf[1] << 2) & 0xf0;
976 alpha = (buf[1] << 6) & 0xc0;
984 YUV_TO_RGB1_CCIR(cb, cr);
985 YUV_TO_RGB2_CCIR(r, g, b, y);
987 dprintf(avctx, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
990 clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
992 clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
994 clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
999 static void dvbsub_parse_region_segment(AVCodecContext *avctx,
1000 const uint8_t *buf, int buf_size)
1002 DVBSubContext *ctx = avctx->priv_data;
1004 const uint8_t *buf_end = buf + buf_size;
1005 int region_id, object_id;
1006 DVBSubRegion *region;
1007 DVBSubObject *object;
1008 DVBSubObjectDisplay *display;
1016 region = get_region(ctx, region_id);
1019 region = av_mallocz(sizeof(DVBSubRegion));
1021 region->id = region_id;
1023 region->next = ctx->region_list;
1024 ctx->region_list = region;
1027 fill = ((*buf++) >> 3) & 1;
1029 region->width = AV_RB16(buf);
1031 region->height = AV_RB16(buf);
1034 if (region->width * region->height != region->buf_size) {
1036 av_free(region->pbuf);
1038 region->buf_size = region->width * region->height;
1040 region->pbuf = av_malloc(region->buf_size);
1045 region->depth = 1 << (((*buf++) >> 2) & 7);
1046 if(region->depth<2 || region->depth>8){
1047 av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);
1050 region->clut = *buf++;
1052 if (region->depth == 8)
1053 region->bgcolor = *buf++;
1057 if (region->depth == 4)
1058 region->bgcolor = (((*buf++) >> 4) & 15);
1060 region->bgcolor = (((*buf++) >> 2) & 3);
1063 dprintf(avctx, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
1066 memset(region->pbuf, region->bgcolor, region->buf_size);
1067 dprintf(avctx, "Fill region (%d)\n", region->bgcolor);
1070 delete_region_display_list(ctx, region);
1072 while (buf + 5 < buf_end) {
1073 object_id = AV_RB16(buf);
1076 object = get_object(ctx, object_id);
1079 object = av_mallocz(sizeof(DVBSubObject));
1081 object->id = object_id;
1082 object->next = ctx->object_list;
1083 ctx->object_list = object;
1086 object->type = (*buf) >> 6;
1088 display = av_mallocz(sizeof(DVBSubObjectDisplay));
1090 display->object_id = object_id;
1091 display->region_id = region_id;
1093 display->x_pos = AV_RB16(buf) & 0xfff;
1095 display->y_pos = AV_RB16(buf) & 0xfff;
1098 if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
1099 display->fgcolor = *buf++;
1100 display->bgcolor = *buf++;
1103 display->region_list_next = region->display_list;
1104 region->display_list = display;
1106 display->object_list_next = object->display_list;
1107 object->display_list = display;
1111 static void dvbsub_parse_page_segment(AVCodecContext *avctx,
1112 const uint8_t *buf, int buf_size)
1114 DVBSubContext *ctx = avctx->priv_data;
1115 DVBSubRegionDisplay *display;
1116 DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
1118 const uint8_t *buf_end = buf + buf_size;
1125 ctx->time_out = *buf++;
1126 page_state = ((*buf++) >> 2) & 3;
1128 dprintf(avctx, "Page time out %ds, state %d\n", ctx->time_out, page_state);
1130 if (page_state == 2) {
1134 tmp_display_list = ctx->display_list;
1135 ctx->display_list = NULL;
1136 ctx->display_list_size = 0;
1138 while (buf + 5 < buf_end) {
1142 display = tmp_display_list;
1143 tmp_ptr = &tmp_display_list;
1145 while (display && display->region_id != region_id) {
1146 tmp_ptr = &display->next;
1147 display = display->next;
1151 display = av_mallocz(sizeof(DVBSubRegionDisplay));
1153 display->region_id = region_id;
1155 display->x_pos = AV_RB16(buf);
1157 display->y_pos = AV_RB16(buf);
1160 *tmp_ptr = display->next;
1162 display->next = ctx->display_list;
1163 ctx->display_list = display;
1164 ctx->display_list_size++;
1166 dprintf(avctx, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
1169 while (tmp_display_list) {
1170 display = tmp_display_list;
1172 tmp_display_list = display->next;
1180 #ifdef DEBUG_SAVE_IMAGES
1181 static void save_display_set(DVBSubContext *ctx)
1183 DVBSubRegion *region;
1184 DVBSubRegionDisplay *display;
1186 uint32_t *clut_table;
1187 int x_pos, y_pos, width, height;
1188 int x, y, y_off, x_off;
1191 static int fileno_index = 0;
1198 for (display = ctx->display_list; display; display = display->next) {
1199 region = get_region(ctx, display->region_id);
1202 x_pos = display->x_pos;
1203 y_pos = display->y_pos;
1204 width = region->width;
1205 height = region->height;
1207 if (display->x_pos < x_pos) {
1208 width += (x_pos - display->x_pos);
1209 x_pos = display->x_pos;
1212 if (display->y_pos < y_pos) {
1213 height += (y_pos - display->y_pos);
1214 y_pos = display->y_pos;
1217 if (display->x_pos + region->width > x_pos + width) {
1218 width = display->x_pos + region->width - x_pos;
1221 if (display->y_pos + region->height > y_pos + height) {
1222 height = display->y_pos + region->height - y_pos;
1229 pbuf = av_malloc(width * height * 4);
1231 for (display = ctx->display_list; display; display = display->next) {
1232 region = get_region(ctx, display->region_id);
1234 x_off = display->x_pos - x_pos;
1235 y_off = display->y_pos - y_pos;
1237 clut = get_clut(ctx, region->clut);
1240 clut = &default_clut;
1242 switch (region->depth) {
1244 clut_table = clut->clut4;
1247 clut_table = clut->clut256;
1251 clut_table = clut->clut16;
1255 for (y = 0; y < region->height; y++) {
1256 for (x = 0; x < region->width; x++) {
1257 pbuf[((y + y_off) * width) + x_off + x] =
1258 clut_table[region->pbuf[y * region->width + x]];
1264 snprintf(filename, sizeof(filename), "dvbs.%d", fileno_index);
1266 png_save2(filename, pbuf, width, height);
1275 static void dvbsub_parse_display_definition_segment(AVCodecContext *avctx,
1279 DVBSubContext *ctx = avctx->priv_data;
1280 DVBSubDisplayDefinition *display_def = ctx->display_definition;
1281 int dds_version, info_byte;
1286 info_byte = bytestream_get_byte(&buf);
1287 dds_version = info_byte >> 4;
1288 if (display_def && display_def->version == dds_version)
1289 return; // already have this display definition version
1292 display_def = av_mallocz(sizeof(*display_def));
1293 ctx->display_definition = display_def;
1298 display_def->version = dds_version;
1301 display_def->width = bytestream_get_be16(&buf) + 1;
1302 display_def->height = bytestream_get_be16(&buf) + 1;
1307 if (info_byte & 1<<3) { // display_window_flag
1308 display_def->x = bytestream_get_be16(&buf);
1309 display_def->y = bytestream_get_be16(&buf);
1310 display_def->width = bytestream_get_be16(&buf) - display_def->x + 1;
1311 display_def->height = bytestream_get_be16(&buf) - display_def->y + 1;
1315 static int dvbsub_display_end_segment(AVCodecContext *avctx, const uint8_t *buf,
1316 int buf_size, AVSubtitle *sub)
1318 DVBSubContext *ctx = avctx->priv_data;
1319 DVBSubDisplayDefinition *display_def = ctx->display_definition;
1321 DVBSubRegion *region;
1322 DVBSubRegionDisplay *display;
1323 AVSubtitleRect *rect;
1325 uint32_t *clut_table;
1327 int offset_x=0, offset_y=0;
1330 sub->start_display_time = 0;
1331 sub->end_display_time = ctx->time_out * 1000;
1335 offset_x = display_def->x;
1336 offset_y = display_def->y;
1339 sub->num_rects = ctx->display_list_size;
1341 if (sub->num_rects > 0){
1342 sub->rects = av_mallocz(sizeof(*sub->rects) * sub->num_rects);
1343 for(i=0; i<sub->num_rects; i++)
1344 sub->rects[i] = av_mallocz(sizeof(*sub->rects[i]));
1349 for (display = ctx->display_list; display; display = display->next) {
1350 region = get_region(ctx, display->region_id);
1351 rect = sub->rects[i];
1356 rect->x = display->x_pos + offset_x;
1357 rect->y = display->y_pos + offset_y;
1358 rect->w = region->width;
1359 rect->h = region->height;
1360 rect->nb_colors = 16;
1361 rect->type = SUBTITLE_BITMAP;
1362 rect->pict.linesize[0] = region->width;
1364 clut = get_clut(ctx, region->clut);
1367 clut = &default_clut;
1369 switch (region->depth) {
1371 clut_table = clut->clut4;
1374 clut_table = clut->clut256;
1378 clut_table = clut->clut16;
1382 rect->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
1383 memcpy(rect->pict.data[1], clut_table, (1 << region->depth) * sizeof(uint32_t));
1385 rect->pict.data[0] = av_malloc(region->buf_size);
1386 memcpy(rect->pict.data[0], region->pbuf, region->buf_size);
1393 #ifdef DEBUG_SAVE_IMAGES
1394 save_display_set(ctx);
1400 static int dvbsub_decode(AVCodecContext *avctx,
1401 void *data, int *data_size,
1404 const uint8_t *buf = avpkt->data;
1405 int buf_size = avpkt->size;
1406 DVBSubContext *ctx = avctx->priv_data;
1407 AVSubtitle *sub = data;
1408 const uint8_t *p, *p_end;
1413 #ifdef DEBUG_PACKET_CONTENTS
1416 av_log(avctx, AV_LOG_INFO, "DVB sub packet:\n");
1418 for (i=0; i < buf_size; i++) {
1419 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
1421 av_log(avctx, AV_LOG_INFO, "\n");
1425 av_log(avctx, AV_LOG_INFO, "\n");
1433 p_end = buf + buf_size;
1435 while (p < p_end && *p == 0x0f) {
1437 segment_type = *p++;
1438 page_id = AV_RB16(p);
1440 segment_length = AV_RB16(p);
1443 if (page_id == ctx->composition_id || page_id == ctx->ancillary_id ||
1444 ctx->composition_id == -1 || ctx->ancillary_id == -1) {
1445 switch (segment_type) {
1446 case DVBSUB_PAGE_SEGMENT:
1447 dvbsub_parse_page_segment(avctx, p, segment_length);
1449 case DVBSUB_REGION_SEGMENT:
1450 dvbsub_parse_region_segment(avctx, p, segment_length);
1452 case DVBSUB_CLUT_SEGMENT:
1453 dvbsub_parse_clut_segment(avctx, p, segment_length);
1455 case DVBSUB_OBJECT_SEGMENT:
1456 dvbsub_parse_object_segment(avctx, p, segment_length);
1458 case DVBSUB_DISPLAYDEFINITION_SEGMENT:
1459 dvbsub_parse_display_definition_segment(avctx, p, segment_length);
1460 case DVBSUB_DISPLAY_SEGMENT:
1461 *data_size = dvbsub_display_end_segment(avctx, p, segment_length, sub);
1464 dprintf(avctx, "Subtitling segment type 0x%x, page id %d, length %d\n",
1465 segment_type, page_id, segment_length);
1470 p += segment_length;
1474 dprintf(avctx, "Junk at end of packet\n");
1482 AVCodec dvbsub_decoder = {
1484 AVMEDIA_TYPE_SUBTITLE,
1485 CODEC_ID_DVB_SUBTITLE,
1486 sizeof(DVBSubContext),
1487 dvbsub_init_decoder,
1489 dvbsub_close_decoder,
1491 .long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),