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"
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);
335 av_free(region->pbuf);
339 while (ctx->clut_list) {
340 clut = ctx->clut_list;
342 ctx->clut_list = clut->next;
347 av_freep(&ctx->display_definition);
349 /* Should already be null */
350 if (ctx->object_list)
351 av_log(0, AV_LOG_ERROR, "Memory deallocation error!\n");
354 static av_cold int dvbsub_init_decoder(AVCodecContext *avctx)
356 int i, r, g, b, a = 0;
357 DVBSubContext *ctx = avctx->priv_data;
359 if (!avctx->extradata || avctx->extradata_size != 4) {
360 av_log(avctx, AV_LOG_WARNING, "Invalid extradata, subtitle streams may be combined!\n");
361 ctx->composition_id = -1;
362 ctx->ancillary_id = -1;
364 ctx->composition_id = AV_RB16(avctx->extradata);
365 ctx->ancillary_id = AV_RB16(avctx->extradata + 2);
368 default_clut.id = -1;
369 default_clut.next = NULL;
371 default_clut.clut4[0] = RGBA( 0, 0, 0, 0);
372 default_clut.clut4[1] = RGBA(255, 255, 255, 255);
373 default_clut.clut4[2] = RGBA( 0, 0, 0, 255);
374 default_clut.clut4[3] = RGBA(127, 127, 127, 255);
376 default_clut.clut16[0] = RGBA( 0, 0, 0, 0);
377 for (i = 1; i < 16; i++) {
379 r = (i & 1) ? 255 : 0;
380 g = (i & 2) ? 255 : 0;
381 b = (i & 4) ? 255 : 0;
383 r = (i & 1) ? 127 : 0;
384 g = (i & 2) ? 127 : 0;
385 b = (i & 4) ? 127 : 0;
387 default_clut.clut16[i] = RGBA(r, g, b, 255);
390 default_clut.clut256[0] = RGBA( 0, 0, 0, 0);
391 for (i = 1; i < 256; i++) {
393 r = (i & 1) ? 255 : 0;
394 g = (i & 2) ? 255 : 0;
395 b = (i & 4) ? 255 : 0;
400 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
401 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
402 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
406 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
407 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
408 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
412 r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
413 g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
414 b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
418 r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
419 g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
420 b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
425 default_clut.clut256[i] = RGBA(r, g, b, a);
431 static av_cold int dvbsub_close_decoder(AVCodecContext *avctx)
433 DVBSubContext *ctx = avctx->priv_data;
434 DVBSubRegionDisplay *display;
438 while (ctx->display_list) {
439 display = ctx->display_list;
440 ctx->display_list = display->next;
448 static int dvbsub_read_2bit_string(uint8_t *destbuf, int dbuf_len,
449 const uint8_t **srcbuf, int buf_size,
450 int non_mod, uint8_t *map_table)
458 init_get_bits(&gb, *srcbuf, buf_size << 3);
460 while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
461 bits = get_bits(&gb, 2);
464 if (non_mod != 1 || bits != 1) {
466 *destbuf++ = map_table[bits];
472 bits = get_bits1(&gb);
474 run_length = get_bits(&gb, 3) + 3;
475 bits = get_bits(&gb, 2);
477 if (non_mod == 1 && bits == 1)
478 pixels_read += run_length;
481 bits = map_table[bits];
482 while (run_length-- > 0 && pixels_read < dbuf_len) {
488 bits = get_bits1(&gb);
490 bits = get_bits(&gb, 2);
492 run_length = get_bits(&gb, 4) + 12;
493 bits = get_bits(&gb, 2);
495 if (non_mod == 1 && bits == 1)
496 pixels_read += run_length;
499 bits = map_table[bits];
500 while (run_length-- > 0 && pixels_read < dbuf_len) {
505 } else if (bits == 3) {
506 run_length = get_bits(&gb, 8) + 29;
507 bits = get_bits(&gb, 2);
509 if (non_mod == 1 && bits == 1)
510 pixels_read += run_length;
513 bits = map_table[bits];
514 while (run_length-- > 0 && pixels_read < dbuf_len) {
519 } else if (bits == 1) {
525 if (pixels_read <= dbuf_len) {
530 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
545 if (get_bits(&gb, 6))
546 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
548 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
553 static int dvbsub_read_4bit_string(uint8_t *destbuf, int dbuf_len,
554 const uint8_t **srcbuf, int buf_size,
555 int non_mod, uint8_t *map_table)
563 init_get_bits(&gb, *srcbuf, buf_size << 3);
565 while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
566 bits = get_bits(&gb, 4);
569 if (non_mod != 1 || bits != 1) {
571 *destbuf++ = map_table[bits];
577 bits = get_bits1(&gb);
579 run_length = get_bits(&gb, 3);
581 if (run_length == 0) {
582 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
593 while (run_length-- > 0 && pixels_read < dbuf_len) {
598 bits = get_bits1(&gb);
600 run_length = get_bits(&gb, 2) + 4;
601 bits = get_bits(&gb, 4);
603 if (non_mod == 1 && bits == 1)
604 pixels_read += run_length;
607 bits = map_table[bits];
608 while (run_length-- > 0 && pixels_read < dbuf_len) {
614 bits = get_bits(&gb, 2);
616 run_length = get_bits(&gb, 4) + 9;
617 bits = get_bits(&gb, 4);
619 if (non_mod == 1 && bits == 1)
620 pixels_read += run_length;
623 bits = map_table[bits];
624 while (run_length-- > 0 && pixels_read < dbuf_len) {
629 } else if (bits == 3) {
630 run_length = get_bits(&gb, 8) + 25;
631 bits = get_bits(&gb, 4);
633 if (non_mod == 1 && bits == 1)
634 pixels_read += run_length;
637 bits = map_table[bits];
638 while (run_length-- > 0 && pixels_read < dbuf_len) {
643 } else if (bits == 1) {
649 if (pixels_read <= dbuf_len) {
666 if (get_bits(&gb, 8))
667 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
669 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
674 static int dvbsub_read_8bit_string(uint8_t *destbuf, int dbuf_len,
675 const uint8_t **srcbuf, int buf_size,
676 int non_mod, uint8_t *map_table)
678 const uint8_t *sbuf_end = (*srcbuf) + buf_size;
683 while (*srcbuf < sbuf_end && pixels_read < dbuf_len) {
687 if (non_mod != 1 || bits != 1) {
689 *destbuf++ = map_table[bits];
696 run_length = bits & 0x7f;
697 if ((bits & 0x80) == 0) {
698 if (run_length == 0) {
706 while (run_length-- > 0 && pixels_read < dbuf_len) {
713 if (non_mod == 1 && bits == 1)
714 pixels_read += run_length;
716 bits = map_table[bits];
717 else while (run_length-- > 0 && pixels_read < dbuf_len) {
726 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
733 static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
734 const uint8_t *buf, int buf_size, int top_bottom, int non_mod)
736 DVBSubContext *ctx = avctx->priv_data;
738 DVBSubRegion *region = get_region(ctx, display->region_id);
739 const uint8_t *buf_end = buf + buf_size;
744 uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
745 uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
746 uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
747 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
750 av_dlog(avctx, "DVB pixel block size %d, %s field:\n", buf_size,
751 top_bottom ? "bottom" : "top");
753 #ifdef DEBUG_PACKET_CONTENTS
754 for (i = 0; i < buf_size; i++) {
756 av_log(avctx, AV_LOG_INFO, "0x%08p: ", buf+i);
758 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
760 av_log(avctx, AV_LOG_INFO, "\n");
764 av_log(avctx, AV_LOG_INFO, "\n");
773 x_pos = display->x_pos;
774 y_pos = display->y_pos;
776 if ((y_pos & 1) != top_bottom)
779 while (buf < buf_end) {
780 if (x_pos > region->width || y_pos > region->height) {
781 av_log(avctx, AV_LOG_ERROR, "Invalid object location!\n");
787 if (region->depth == 8)
789 else if (region->depth == 4)
794 x_pos += dvbsub_read_2bit_string(pbuf + (y_pos * region->width) + x_pos,
795 region->width - x_pos, &buf, buf_end - buf,
799 if (region->depth < 4) {
800 av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
804 if (region->depth == 8)
809 x_pos += dvbsub_read_4bit_string(pbuf + (y_pos * region->width) + x_pos,
810 region->width - x_pos, &buf, buf_end - buf,
814 if (region->depth < 8) {
815 av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
819 x_pos += dvbsub_read_8bit_string(pbuf + (y_pos * region->width) + x_pos,
820 region->width - x_pos, &buf, buf_end - buf,
825 map2to4[0] = (*buf) >> 4;
826 map2to4[1] = (*buf++) & 0xf;
827 map2to4[2] = (*buf) >> 4;
828 map2to4[3] = (*buf++) & 0xf;
831 for (i = 0; i < 4; i++)
835 for (i = 0; i < 16; i++)
840 x_pos = display->x_pos;
844 av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
850 static void dvbsub_parse_object_segment(AVCodecContext *avctx,
851 const uint8_t *buf, int buf_size)
853 DVBSubContext *ctx = avctx->priv_data;
855 const uint8_t *buf_end = buf + buf_size;
856 const uint8_t *block;
858 DVBSubObject *object;
859 DVBSubObjectDisplay *display;
860 int top_field_len, bottom_field_len;
862 int coding_method, non_modifying_color;
864 object_id = AV_RB16(buf);
867 object = get_object(ctx, object_id);
872 coding_method = ((*buf) >> 2) & 3;
873 non_modifying_color = ((*buf++) >> 1) & 1;
875 if (coding_method == 0) {
876 top_field_len = AV_RB16(buf);
878 bottom_field_len = AV_RB16(buf);
881 if (buf + top_field_len + bottom_field_len > buf_end) {
882 av_log(avctx, AV_LOG_ERROR, "Field data size too large\n");
886 for (display = object->display_list; display; display = display->object_list_next) {
889 dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
890 non_modifying_color);
892 if (bottom_field_len > 0)
893 block = buf + top_field_len;
895 bottom_field_len = top_field_len;
897 dvbsub_parse_pixel_data_block(avctx, display, block, bottom_field_len, 1,
898 non_modifying_color);
901 /* } else if (coding_method == 1) {*/
904 av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
909 static void dvbsub_parse_clut_segment(AVCodecContext *avctx,
910 const uint8_t *buf, int buf_size)
912 DVBSubContext *ctx = avctx->priv_data;
914 const uint8_t *buf_end = buf + buf_size;
917 int entry_id, depth , full_range;
918 int y, cr, cb, alpha;
919 int r, g, b, r_add, g_add, b_add;
921 #ifdef DEBUG_PACKET_CONTENTS
924 av_log(avctx, AV_LOG_INFO, "DVB clut packet:\n");
926 for (i=0; i < buf_size; i++) {
927 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
929 av_log(avctx, AV_LOG_INFO, "\n");
933 av_log(avctx, AV_LOG_INFO, "\n");
940 clut = get_clut(ctx, clut_id);
943 clut = av_malloc(sizeof(DVBSubCLUT));
945 memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
949 clut->next = ctx->clut_list;
950 ctx->clut_list = clut;
953 while (buf + 4 < buf_end) {
956 depth = (*buf) & 0xe0;
959 av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
963 full_range = (*buf++) & 1;
972 cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
973 cb = (buf[1] << 2) & 0xf0;
974 alpha = (buf[1] << 6) & 0xc0;
982 YUV_TO_RGB1_CCIR(cb, cr);
983 YUV_TO_RGB2_CCIR(r, g, b, y);
985 av_dlog(avctx, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
988 clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
990 clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
992 clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
997 static void dvbsub_parse_region_segment(AVCodecContext *avctx,
998 const uint8_t *buf, int buf_size)
1000 DVBSubContext *ctx = avctx->priv_data;
1002 const uint8_t *buf_end = buf + buf_size;
1003 int region_id, object_id;
1004 DVBSubRegion *region;
1005 DVBSubObject *object;
1006 DVBSubObjectDisplay *display;
1014 region = get_region(ctx, region_id);
1017 region = av_mallocz(sizeof(DVBSubRegion));
1019 region->id = region_id;
1021 region->next = ctx->region_list;
1022 ctx->region_list = region;
1025 fill = ((*buf++) >> 3) & 1;
1027 region->width = AV_RB16(buf);
1029 region->height = AV_RB16(buf);
1032 if (region->width * region->height != region->buf_size) {
1033 av_free(region->pbuf);
1035 region->buf_size = region->width * region->height;
1037 region->pbuf = av_malloc(region->buf_size);
1042 region->depth = 1 << (((*buf++) >> 2) & 7);
1043 if(region->depth<2 || region->depth>8){
1044 av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);
1047 region->clut = *buf++;
1049 if (region->depth == 8)
1050 region->bgcolor = *buf++;
1054 if (region->depth == 4)
1055 region->bgcolor = (((*buf++) >> 4) & 15);
1057 region->bgcolor = (((*buf++) >> 2) & 3);
1060 av_dlog(avctx, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
1063 memset(region->pbuf, region->bgcolor, region->buf_size);
1064 av_dlog(avctx, "Fill region (%d)\n", region->bgcolor);
1067 delete_region_display_list(ctx, region);
1069 while (buf + 5 < buf_end) {
1070 object_id = AV_RB16(buf);
1073 object = get_object(ctx, object_id);
1076 object = av_mallocz(sizeof(DVBSubObject));
1078 object->id = object_id;
1079 object->next = ctx->object_list;
1080 ctx->object_list = object;
1083 object->type = (*buf) >> 6;
1085 display = av_mallocz(sizeof(DVBSubObjectDisplay));
1087 display->object_id = object_id;
1088 display->region_id = region_id;
1090 display->x_pos = AV_RB16(buf) & 0xfff;
1092 display->y_pos = AV_RB16(buf) & 0xfff;
1095 if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
1096 display->fgcolor = *buf++;
1097 display->bgcolor = *buf++;
1100 display->region_list_next = region->display_list;
1101 region->display_list = display;
1103 display->object_list_next = object->display_list;
1104 object->display_list = display;
1108 static void dvbsub_parse_page_segment(AVCodecContext *avctx,
1109 const uint8_t *buf, int buf_size)
1111 DVBSubContext *ctx = avctx->priv_data;
1112 DVBSubRegionDisplay *display;
1113 DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
1115 const uint8_t *buf_end = buf + buf_size;
1122 ctx->time_out = *buf++;
1123 page_state = ((*buf++) >> 2) & 3;
1125 av_dlog(avctx, "Page time out %ds, state %d\n", ctx->time_out, page_state);
1127 if (page_state == 2) {
1131 tmp_display_list = ctx->display_list;
1132 ctx->display_list = NULL;
1133 ctx->display_list_size = 0;
1135 while (buf + 5 < buf_end) {
1139 display = tmp_display_list;
1140 tmp_ptr = &tmp_display_list;
1142 while (display && display->region_id != region_id) {
1143 tmp_ptr = &display->next;
1144 display = display->next;
1148 display = av_mallocz(sizeof(DVBSubRegionDisplay));
1150 display->region_id = region_id;
1152 display->x_pos = AV_RB16(buf);
1154 display->y_pos = AV_RB16(buf);
1157 *tmp_ptr = display->next;
1159 display->next = ctx->display_list;
1160 ctx->display_list = display;
1161 ctx->display_list_size++;
1163 av_dlog(avctx, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
1166 while (tmp_display_list) {
1167 display = tmp_display_list;
1169 tmp_display_list = display->next;
1177 #ifdef DEBUG_SAVE_IMAGES
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 for (display = ctx->display_list; display; display = display->next) {
1229 region = get_region(ctx, display->region_id);
1231 x_off = display->x_pos - x_pos;
1232 y_off = display->y_pos - y_pos;
1234 clut = get_clut(ctx, region->clut);
1237 clut = &default_clut;
1239 switch (region->depth) {
1241 clut_table = clut->clut4;
1244 clut_table = clut->clut256;
1248 clut_table = clut->clut16;
1252 for (y = 0; y < region->height; y++) {
1253 for (x = 0; x < region->width; x++) {
1254 pbuf[((y + y_off) * width) + x_off + x] =
1255 clut_table[region->pbuf[y * region->width + x]];
1261 snprintf(filename, sizeof(filename), "dvbs.%d", fileno_index);
1263 png_save2(filename, pbuf, width, height);
1272 static void dvbsub_parse_display_definition_segment(AVCodecContext *avctx,
1276 DVBSubContext *ctx = avctx->priv_data;
1277 DVBSubDisplayDefinition *display_def = ctx->display_definition;
1278 int dds_version, info_byte;
1283 info_byte = bytestream_get_byte(&buf);
1284 dds_version = info_byte >> 4;
1285 if (display_def && display_def->version == dds_version)
1286 return; // already have this display definition version
1289 display_def = av_mallocz(sizeof(*display_def));
1290 ctx->display_definition = display_def;
1295 display_def->version = dds_version;
1298 display_def->width = bytestream_get_be16(&buf) + 1;
1299 display_def->height = bytestream_get_be16(&buf) + 1;
1304 if (info_byte & 1<<3) { // display_window_flag
1305 display_def->x = bytestream_get_be16(&buf);
1306 display_def->y = bytestream_get_be16(&buf);
1307 display_def->width = bytestream_get_be16(&buf) - display_def->x + 1;
1308 display_def->height = bytestream_get_be16(&buf) - display_def->y + 1;
1312 static int dvbsub_display_end_segment(AVCodecContext *avctx, const uint8_t *buf,
1313 int buf_size, AVSubtitle *sub)
1315 DVBSubContext *ctx = avctx->priv_data;
1316 DVBSubDisplayDefinition *display_def = ctx->display_definition;
1318 DVBSubRegion *region;
1319 DVBSubRegionDisplay *display;
1320 AVSubtitleRect *rect;
1322 uint32_t *clut_table;
1324 int offset_x=0, offset_y=0;
1327 sub->start_display_time = 0;
1328 sub->end_display_time = ctx->time_out * 1000;
1332 offset_x = display_def->x;
1333 offset_y = display_def->y;
1336 sub->num_rects = ctx->display_list_size;
1338 if (sub->num_rects > 0){
1339 sub->rects = av_mallocz(sizeof(*sub->rects) * sub->num_rects);
1340 for(i=0; i<sub->num_rects; i++)
1341 sub->rects[i] = av_mallocz(sizeof(*sub->rects[i]));
1346 for (display = ctx->display_list; display; display = display->next) {
1347 region = get_region(ctx, display->region_id);
1348 rect = sub->rects[i];
1353 rect->x = display->x_pos + offset_x;
1354 rect->y = display->y_pos + offset_y;
1355 rect->w = region->width;
1356 rect->h = region->height;
1357 rect->nb_colors = 16;
1358 rect->type = SUBTITLE_BITMAP;
1359 rect->pict.linesize[0] = region->width;
1361 clut = get_clut(ctx, region->clut);
1364 clut = &default_clut;
1366 switch (region->depth) {
1368 clut_table = clut->clut4;
1371 clut_table = clut->clut256;
1375 clut_table = clut->clut16;
1379 rect->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
1380 memcpy(rect->pict.data[1], clut_table, (1 << region->depth) * sizeof(uint32_t));
1382 rect->pict.data[0] = av_malloc(region->buf_size);
1383 memcpy(rect->pict.data[0], region->pbuf, region->buf_size);
1390 #ifdef DEBUG_SAVE_IMAGES
1391 save_display_set(ctx);
1397 static int dvbsub_decode(AVCodecContext *avctx,
1398 void *data, int *data_size,
1401 const uint8_t *buf = avpkt->data;
1402 int buf_size = avpkt->size;
1403 DVBSubContext *ctx = avctx->priv_data;
1404 AVSubtitle *sub = data;
1405 const uint8_t *p, *p_end;
1410 #ifdef DEBUG_PACKET_CONTENTS
1413 av_log(avctx, AV_LOG_INFO, "DVB sub packet:\n");
1415 for (i=0; i < buf_size; i++) {
1416 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
1418 av_log(avctx, AV_LOG_INFO, "\n");
1422 av_log(avctx, AV_LOG_INFO, "\n");
1426 if (buf_size <= 6 || *buf != 0x0f) {
1427 av_dlog(avctx, "incomplete or broken packet");
1432 p_end = buf + buf_size;
1434 while (p_end - p >= 6 && *p == 0x0f) {
1436 segment_type = *p++;
1437 page_id = AV_RB16(p);
1439 segment_length = AV_RB16(p);
1442 if (p_end - p < segment_length) {
1443 av_dlog(avctx, "incomplete or broken packet");
1447 if (page_id == ctx->composition_id || page_id == ctx->ancillary_id ||
1448 ctx->composition_id == -1 || ctx->ancillary_id == -1) {
1449 switch (segment_type) {
1450 case DVBSUB_PAGE_SEGMENT:
1451 dvbsub_parse_page_segment(avctx, p, segment_length);
1453 case DVBSUB_REGION_SEGMENT:
1454 dvbsub_parse_region_segment(avctx, p, segment_length);
1456 case DVBSUB_CLUT_SEGMENT:
1457 dvbsub_parse_clut_segment(avctx, p, segment_length);
1459 case DVBSUB_OBJECT_SEGMENT:
1460 dvbsub_parse_object_segment(avctx, p, segment_length);
1462 case DVBSUB_DISPLAYDEFINITION_SEGMENT:
1463 dvbsub_parse_display_definition_segment(avctx, p, segment_length);
1464 case DVBSUB_DISPLAY_SEGMENT:
1465 *data_size = dvbsub_display_end_segment(avctx, p, segment_length, sub);
1468 av_dlog(avctx, "Subtitling segment type 0x%x, page id %d, length %d\n",
1469 segment_type, page_id, segment_length);
1474 p += segment_length;
1481 AVCodec ff_dvbsub_decoder = {
1483 AVMEDIA_TYPE_SUBTITLE,
1484 CODEC_ID_DVB_SUBTITLE,
1485 sizeof(DVBSubContext),
1486 dvbsub_init_decoder,
1488 dvbsub_close_decoder,
1490 .long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),