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_cluts(DVBSubContext *ctx)
328 while (ctx->clut_list) {
329 clut = ctx->clut_list;
331 ctx->clut_list = clut->next;
337 static void delete_objects(DVBSubContext *ctx)
339 DVBSubObject *object;
341 while (ctx->object_list) {
342 object = ctx->object_list;
344 ctx->object_list = object->next;
350 static void delete_regions(DVBSubContext *ctx)
352 DVBSubRegion *region;
354 while (ctx->region_list) {
355 region = ctx->region_list;
357 ctx->region_list = region->next;
359 delete_region_display_list(ctx, region);
361 av_free(region->pbuf);
366 static av_cold int dvbsub_init_decoder(AVCodecContext *avctx)
368 int i, r, g, b, a = 0;
369 DVBSubContext *ctx = avctx->priv_data;
371 if (!avctx->extradata || avctx->extradata_size != 4) {
372 av_log(avctx, AV_LOG_WARNING, "Invalid extradata, subtitle streams may be combined!\n");
373 ctx->composition_id = -1;
374 ctx->ancillary_id = -1;
376 ctx->composition_id = AV_RB16(avctx->extradata);
377 ctx->ancillary_id = AV_RB16(avctx->extradata + 2);
380 default_clut.id = -1;
381 default_clut.next = NULL;
383 default_clut.clut4[0] = RGBA( 0, 0, 0, 0);
384 default_clut.clut4[1] = RGBA(255, 255, 255, 255);
385 default_clut.clut4[2] = RGBA( 0, 0, 0, 255);
386 default_clut.clut4[3] = RGBA(127, 127, 127, 255);
388 default_clut.clut16[0] = RGBA( 0, 0, 0, 0);
389 for (i = 1; i < 16; i++) {
391 r = (i & 1) ? 255 : 0;
392 g = (i & 2) ? 255 : 0;
393 b = (i & 4) ? 255 : 0;
395 r = (i & 1) ? 127 : 0;
396 g = (i & 2) ? 127 : 0;
397 b = (i & 4) ? 127 : 0;
399 default_clut.clut16[i] = RGBA(r, g, b, 255);
402 default_clut.clut256[0] = RGBA( 0, 0, 0, 0);
403 for (i = 1; i < 256; i++) {
405 r = (i & 1) ? 255 : 0;
406 g = (i & 2) ? 255 : 0;
407 b = (i & 4) ? 255 : 0;
412 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
413 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
414 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
418 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
419 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
420 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
424 r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
425 g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
426 b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
430 r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
431 g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
432 b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
437 default_clut.clut256[i] = RGBA(r, g, b, a);
443 static av_cold int dvbsub_close_decoder(AVCodecContext *avctx)
445 DVBSubContext *ctx = avctx->priv_data;
446 DVBSubRegionDisplay *display;
454 av_freep(&ctx->display_definition);
456 while (ctx->display_list) {
457 display = ctx->display_list;
458 ctx->display_list = display->next;
466 static int dvbsub_read_2bit_string(uint8_t *destbuf, int dbuf_len,
467 const uint8_t **srcbuf, int buf_size,
468 int non_mod, uint8_t *map_table)
476 init_get_bits(&gb, *srcbuf, buf_size << 3);
478 while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
479 bits = get_bits(&gb, 2);
482 if (non_mod != 1 || bits != 1) {
484 *destbuf++ = map_table[bits];
490 bits = get_bits1(&gb);
492 run_length = get_bits(&gb, 3) + 3;
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) {
506 bits = get_bits1(&gb);
508 bits = get_bits(&gb, 2);
510 run_length = get_bits(&gb, 4) + 12;
511 bits = get_bits(&gb, 2);
513 if (non_mod == 1 && bits == 1)
514 pixels_read += run_length;
517 bits = map_table[bits];
518 while (run_length-- > 0 && pixels_read < dbuf_len) {
523 } else if (bits == 3) {
524 run_length = get_bits(&gb, 8) + 29;
525 bits = get_bits(&gb, 2);
527 if (non_mod == 1 && bits == 1)
528 pixels_read += run_length;
531 bits = map_table[bits];
532 while (run_length-- > 0 && pixels_read < dbuf_len) {
537 } else if (bits == 1) {
543 if (pixels_read <= dbuf_len) {
548 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
563 if (get_bits(&gb, 6))
564 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
566 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
571 static int dvbsub_read_4bit_string(uint8_t *destbuf, int dbuf_len,
572 const uint8_t **srcbuf, int buf_size,
573 int non_mod, uint8_t *map_table)
581 init_get_bits(&gb, *srcbuf, buf_size << 3);
583 while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
584 bits = get_bits(&gb, 4);
587 if (non_mod != 1 || bits != 1) {
589 *destbuf++ = map_table[bits];
595 bits = get_bits1(&gb);
597 run_length = get_bits(&gb, 3);
599 if (run_length == 0) {
600 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
611 while (run_length-- > 0 && pixels_read < dbuf_len) {
616 bits = get_bits1(&gb);
618 run_length = get_bits(&gb, 2) + 4;
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) {
632 bits = get_bits(&gb, 2);
634 run_length = get_bits(&gb, 4) + 9;
635 bits = get_bits(&gb, 4);
637 if (non_mod == 1 && bits == 1)
638 pixels_read += run_length;
641 bits = map_table[bits];
642 while (run_length-- > 0 && pixels_read < dbuf_len) {
647 } else if (bits == 3) {
648 run_length = get_bits(&gb, 8) + 25;
649 bits = get_bits(&gb, 4);
651 if (non_mod == 1 && bits == 1)
652 pixels_read += run_length;
655 bits = map_table[bits];
656 while (run_length-- > 0 && pixels_read < dbuf_len) {
661 } else if (bits == 1) {
667 if (pixels_read <= dbuf_len) {
684 if (get_bits(&gb, 8))
685 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
687 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
692 static int dvbsub_read_8bit_string(uint8_t *destbuf, int dbuf_len,
693 const uint8_t **srcbuf, int buf_size,
694 int non_mod, uint8_t *map_table)
696 const uint8_t *sbuf_end = (*srcbuf) + buf_size;
701 while (*srcbuf < sbuf_end && pixels_read < dbuf_len) {
705 if (non_mod != 1 || bits != 1) {
707 *destbuf++ = map_table[bits];
714 run_length = bits & 0x7f;
715 if ((bits & 0x80) == 0) {
716 if (run_length == 0) {
724 while (run_length-- > 0 && pixels_read < dbuf_len) {
731 if (non_mod == 1 && bits == 1)
732 pixels_read += run_length;
734 bits = map_table[bits];
735 else while (run_length-- > 0 && pixels_read < dbuf_len) {
744 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
751 static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
752 const uint8_t *buf, int buf_size, int top_bottom, int non_mod)
754 DVBSubContext *ctx = avctx->priv_data;
756 DVBSubRegion *region = get_region(ctx, display->region_id);
757 const uint8_t *buf_end = buf + buf_size;
762 uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
763 uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
764 uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
765 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
768 av_dlog(avctx, "DVB pixel block size %d, %s field:\n", buf_size,
769 top_bottom ? "bottom" : "top");
771 #ifdef DEBUG_PACKET_CONTENTS
772 for (i = 0; i < buf_size; i++) {
774 av_log(avctx, AV_LOG_INFO, "0x%08p: ", buf+i);
776 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
778 av_log(avctx, AV_LOG_INFO, "\n");
782 av_log(avctx, AV_LOG_INFO, "\n");
791 x_pos = display->x_pos;
792 y_pos = display->y_pos;
794 if ((y_pos & 1) != top_bottom)
797 while (buf < buf_end) {
798 if (x_pos > region->width || y_pos > region->height) {
799 av_log(avctx, AV_LOG_ERROR, "Invalid object location!\n");
805 if (region->depth == 8)
807 else if (region->depth == 4)
812 x_pos += dvbsub_read_2bit_string(pbuf + (y_pos * region->width) + x_pos,
813 region->width - x_pos, &buf, buf_end - buf,
817 if (region->depth < 4) {
818 av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
822 if (region->depth == 8)
827 x_pos += dvbsub_read_4bit_string(pbuf + (y_pos * region->width) + x_pos,
828 region->width - x_pos, &buf, buf_end - buf,
832 if (region->depth < 8) {
833 av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
837 x_pos += dvbsub_read_8bit_string(pbuf + (y_pos * region->width) + x_pos,
838 region->width - x_pos, &buf, buf_end - buf,
843 map2to4[0] = (*buf) >> 4;
844 map2to4[1] = (*buf++) & 0xf;
845 map2to4[2] = (*buf) >> 4;
846 map2to4[3] = (*buf++) & 0xf;
849 for (i = 0; i < 4; i++)
853 for (i = 0; i < 16; i++)
858 x_pos = display->x_pos;
862 av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
868 static void dvbsub_parse_object_segment(AVCodecContext *avctx,
869 const uint8_t *buf, int buf_size)
871 DVBSubContext *ctx = avctx->priv_data;
873 const uint8_t *buf_end = buf + buf_size;
874 const uint8_t *block;
876 DVBSubObject *object;
877 DVBSubObjectDisplay *display;
878 int top_field_len, bottom_field_len;
880 int coding_method, non_modifying_color;
882 object_id = AV_RB16(buf);
885 object = get_object(ctx, object_id);
890 coding_method = ((*buf) >> 2) & 3;
891 non_modifying_color = ((*buf++) >> 1) & 1;
893 if (coding_method == 0) {
894 top_field_len = AV_RB16(buf);
896 bottom_field_len = AV_RB16(buf);
899 if (buf + top_field_len + bottom_field_len > buf_end) {
900 av_log(avctx, AV_LOG_ERROR, "Field data size too large\n");
904 for (display = object->display_list; display; display = display->object_list_next) {
907 dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
908 non_modifying_color);
910 if (bottom_field_len > 0)
911 block = buf + top_field_len;
913 bottom_field_len = top_field_len;
915 dvbsub_parse_pixel_data_block(avctx, display, block, bottom_field_len, 1,
916 non_modifying_color);
919 /* } else if (coding_method == 1) {*/
922 av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
927 static void dvbsub_parse_clut_segment(AVCodecContext *avctx,
928 const uint8_t *buf, int buf_size)
930 DVBSubContext *ctx = avctx->priv_data;
932 const uint8_t *buf_end = buf + buf_size;
935 int entry_id, depth , full_range;
936 int y, cr, cb, alpha;
937 int r, g, b, r_add, g_add, b_add;
939 #ifdef DEBUG_PACKET_CONTENTS
942 av_log(avctx, AV_LOG_INFO, "DVB clut packet:\n");
944 for (i=0; i < buf_size; i++) {
945 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
947 av_log(avctx, AV_LOG_INFO, "\n");
951 av_log(avctx, AV_LOG_INFO, "\n");
958 clut = get_clut(ctx, clut_id);
961 clut = av_malloc(sizeof(DVBSubCLUT));
963 memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
967 clut->next = ctx->clut_list;
968 ctx->clut_list = clut;
971 while (buf + 4 < buf_end) {
974 depth = (*buf) & 0xe0;
977 av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
981 full_range = (*buf++) & 1;
990 cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
991 cb = (buf[1] << 2) & 0xf0;
992 alpha = (buf[1] << 6) & 0xc0;
1000 YUV_TO_RGB1_CCIR(cb, cr);
1001 YUV_TO_RGB2_CCIR(r, g, b, y);
1003 av_dlog(avctx, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
1006 clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
1008 clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
1010 clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
1015 static void dvbsub_parse_region_segment(AVCodecContext *avctx,
1016 const uint8_t *buf, int buf_size)
1018 DVBSubContext *ctx = avctx->priv_data;
1020 const uint8_t *buf_end = buf + buf_size;
1021 int region_id, object_id;
1022 DVBSubRegion *region;
1023 DVBSubObject *object;
1024 DVBSubObjectDisplay *display;
1032 region = get_region(ctx, region_id);
1035 region = av_mallocz(sizeof(DVBSubRegion));
1037 region->id = region_id;
1039 region->next = ctx->region_list;
1040 ctx->region_list = region;
1043 fill = ((*buf++) >> 3) & 1;
1045 region->width = AV_RB16(buf);
1047 region->height = AV_RB16(buf);
1050 if (region->width * region->height != region->buf_size) {
1051 av_free(region->pbuf);
1053 region->buf_size = region->width * region->height;
1055 region->pbuf = av_malloc(region->buf_size);
1060 region->depth = 1 << (((*buf++) >> 2) & 7);
1061 if(region->depth<2 || region->depth>8){
1062 av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);
1065 region->clut = *buf++;
1067 if (region->depth == 8)
1068 region->bgcolor = *buf++;
1072 if (region->depth == 4)
1073 region->bgcolor = (((*buf++) >> 4) & 15);
1075 region->bgcolor = (((*buf++) >> 2) & 3);
1078 av_dlog(avctx, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
1081 memset(region->pbuf, region->bgcolor, region->buf_size);
1082 av_dlog(avctx, "Fill region (%d)\n", region->bgcolor);
1085 delete_region_display_list(ctx, region);
1087 while (buf + 5 < buf_end) {
1088 object_id = AV_RB16(buf);
1091 object = get_object(ctx, object_id);
1094 object = av_mallocz(sizeof(DVBSubObject));
1096 object->id = object_id;
1097 object->next = ctx->object_list;
1098 ctx->object_list = object;
1101 object->type = (*buf) >> 6;
1103 display = av_mallocz(sizeof(DVBSubObjectDisplay));
1105 display->object_id = object_id;
1106 display->region_id = region_id;
1108 display->x_pos = AV_RB16(buf) & 0xfff;
1110 display->y_pos = AV_RB16(buf) & 0xfff;
1113 if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
1114 display->fgcolor = *buf++;
1115 display->bgcolor = *buf++;
1118 display->region_list_next = region->display_list;
1119 region->display_list = display;
1121 display->object_list_next = object->display_list;
1122 object->display_list = display;
1126 static void dvbsub_parse_page_segment(AVCodecContext *avctx,
1127 const uint8_t *buf, int buf_size)
1129 DVBSubContext *ctx = avctx->priv_data;
1130 DVBSubRegionDisplay *display;
1131 DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
1133 const uint8_t *buf_end = buf + buf_size;
1140 ctx->time_out = *buf++;
1141 page_state = ((*buf++) >> 2) & 3;
1143 av_dlog(avctx, "Page time out %ds, state %d\n", ctx->time_out, page_state);
1145 if (page_state == 2) {
1146 delete_regions(ctx);
1147 delete_objects(ctx);
1151 tmp_display_list = ctx->display_list;
1152 ctx->display_list = NULL;
1153 ctx->display_list_size = 0;
1155 while (buf + 5 < buf_end) {
1159 display = tmp_display_list;
1160 tmp_ptr = &tmp_display_list;
1162 while (display && display->region_id != region_id) {
1163 tmp_ptr = &display->next;
1164 display = display->next;
1168 display = av_mallocz(sizeof(DVBSubRegionDisplay));
1170 display->region_id = region_id;
1172 display->x_pos = AV_RB16(buf);
1174 display->y_pos = AV_RB16(buf);
1177 *tmp_ptr = display->next;
1179 display->next = ctx->display_list;
1180 ctx->display_list = display;
1181 ctx->display_list_size++;
1183 av_dlog(avctx, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
1186 while (tmp_display_list) {
1187 display = tmp_display_list;
1189 tmp_display_list = display->next;
1197 #ifdef DEBUG_SAVE_IMAGES
1198 static void save_display_set(DVBSubContext *ctx)
1200 DVBSubRegion *region;
1201 DVBSubRegionDisplay *display;
1203 uint32_t *clut_table;
1204 int x_pos, y_pos, width, height;
1205 int x, y, y_off, x_off;
1208 static int fileno_index = 0;
1215 for (display = ctx->display_list; display; display = display->next) {
1216 region = get_region(ctx, display->region_id);
1219 x_pos = display->x_pos;
1220 y_pos = display->y_pos;
1221 width = region->width;
1222 height = region->height;
1224 if (display->x_pos < x_pos) {
1225 width += (x_pos - display->x_pos);
1226 x_pos = display->x_pos;
1229 if (display->y_pos < y_pos) {
1230 height += (y_pos - display->y_pos);
1231 y_pos = display->y_pos;
1234 if (display->x_pos + region->width > x_pos + width) {
1235 width = display->x_pos + region->width - x_pos;
1238 if (display->y_pos + region->height > y_pos + height) {
1239 height = display->y_pos + region->height - y_pos;
1246 pbuf = av_malloc(width * height * 4);
1248 for (display = ctx->display_list; display; display = display->next) {
1249 region = get_region(ctx, display->region_id);
1251 x_off = display->x_pos - x_pos;
1252 y_off = display->y_pos - y_pos;
1254 clut = get_clut(ctx, region->clut);
1257 clut = &default_clut;
1259 switch (region->depth) {
1261 clut_table = clut->clut4;
1264 clut_table = clut->clut256;
1268 clut_table = clut->clut16;
1272 for (y = 0; y < region->height; y++) {
1273 for (x = 0; x < region->width; x++) {
1274 pbuf[((y + y_off) * width) + x_off + x] =
1275 clut_table[region->pbuf[y * region->width + x]];
1281 snprintf(filename, sizeof(filename), "dvbs.%d", fileno_index);
1283 png_save2(filename, pbuf, width, height);
1292 static void dvbsub_parse_display_definition_segment(AVCodecContext *avctx,
1296 DVBSubContext *ctx = avctx->priv_data;
1297 DVBSubDisplayDefinition *display_def = ctx->display_definition;
1298 int dds_version, info_byte;
1303 info_byte = bytestream_get_byte(&buf);
1304 dds_version = info_byte >> 4;
1305 if (display_def && display_def->version == dds_version)
1306 return; // already have this display definition version
1309 display_def = av_mallocz(sizeof(*display_def));
1310 ctx->display_definition = display_def;
1315 display_def->version = dds_version;
1318 display_def->width = bytestream_get_be16(&buf) + 1;
1319 display_def->height = bytestream_get_be16(&buf) + 1;
1324 if (info_byte & 1<<3) { // display_window_flag
1325 display_def->x = bytestream_get_be16(&buf);
1326 display_def->y = bytestream_get_be16(&buf);
1327 display_def->width = bytestream_get_be16(&buf) - display_def->x + 1;
1328 display_def->height = bytestream_get_be16(&buf) - display_def->y + 1;
1332 static int dvbsub_display_end_segment(AVCodecContext *avctx, const uint8_t *buf,
1333 int buf_size, AVSubtitle *sub)
1335 DVBSubContext *ctx = avctx->priv_data;
1336 DVBSubDisplayDefinition *display_def = ctx->display_definition;
1338 DVBSubRegion *region;
1339 DVBSubRegionDisplay *display;
1340 AVSubtitleRect *rect;
1342 uint32_t *clut_table;
1344 int offset_x=0, offset_y=0;
1346 sub->end_display_time = ctx->time_out * 1000;
1349 offset_x = display_def->x;
1350 offset_y = display_def->y;
1353 sub->num_rects = ctx->display_list_size;
1355 if (sub->num_rects > 0){
1356 sub->rects = av_mallocz(sizeof(*sub->rects) * sub->num_rects);
1357 for(i=0; i<sub->num_rects; i++)
1358 sub->rects[i] = av_mallocz(sizeof(*sub->rects[i]));
1363 for (display = ctx->display_list; display; display = display->next) {
1364 region = get_region(ctx, display->region_id);
1365 rect = sub->rects[i];
1370 rect->x = display->x_pos + offset_x;
1371 rect->y = display->y_pos + offset_y;
1372 rect->w = region->width;
1373 rect->h = region->height;
1374 rect->nb_colors = 16;
1375 rect->type = SUBTITLE_BITMAP;
1376 rect->pict.linesize[0] = region->width;
1378 clut = get_clut(ctx, region->clut);
1381 clut = &default_clut;
1383 switch (region->depth) {
1385 clut_table = clut->clut4;
1388 clut_table = clut->clut256;
1392 clut_table = clut->clut16;
1396 rect->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
1397 memcpy(rect->pict.data[1], clut_table, (1 << region->depth) * sizeof(uint32_t));
1399 rect->pict.data[0] = av_malloc(region->buf_size);
1400 memcpy(rect->pict.data[0], region->pbuf, region->buf_size);
1407 #ifdef DEBUG_SAVE_IMAGES
1408 save_display_set(ctx);
1414 static int dvbsub_decode(AVCodecContext *avctx,
1415 void *data, int *data_size,
1418 const uint8_t *buf = avpkt->data;
1419 int buf_size = avpkt->size;
1420 DVBSubContext *ctx = avctx->priv_data;
1421 AVSubtitle *sub = data;
1422 const uint8_t *p, *p_end;
1427 #ifdef DEBUG_PACKET_CONTENTS
1430 av_log(avctx, AV_LOG_INFO, "DVB sub packet:\n");
1432 for (i=0; i < buf_size; i++) {
1433 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
1435 av_log(avctx, AV_LOG_INFO, "\n");
1439 av_log(avctx, AV_LOG_INFO, "\n");
1443 if (buf_size <= 6 || *buf != 0x0f) {
1444 av_dlog(avctx, "incomplete or broken packet");
1449 p_end = buf + buf_size;
1451 while (p_end - p >= 6 && *p == 0x0f) {
1453 segment_type = *p++;
1454 page_id = AV_RB16(p);
1456 segment_length = AV_RB16(p);
1459 if (p_end - p < segment_length) {
1460 av_dlog(avctx, "incomplete or broken packet");
1464 if (page_id == ctx->composition_id || page_id == ctx->ancillary_id ||
1465 ctx->composition_id == -1 || ctx->ancillary_id == -1) {
1466 switch (segment_type) {
1467 case DVBSUB_PAGE_SEGMENT:
1468 dvbsub_parse_page_segment(avctx, p, segment_length);
1470 case DVBSUB_REGION_SEGMENT:
1471 dvbsub_parse_region_segment(avctx, p, segment_length);
1473 case DVBSUB_CLUT_SEGMENT:
1474 dvbsub_parse_clut_segment(avctx, p, segment_length);
1476 case DVBSUB_OBJECT_SEGMENT:
1477 dvbsub_parse_object_segment(avctx, p, segment_length);
1479 case DVBSUB_DISPLAYDEFINITION_SEGMENT:
1480 dvbsub_parse_display_definition_segment(avctx, p, segment_length);
1481 case DVBSUB_DISPLAY_SEGMENT:
1482 *data_size = dvbsub_display_end_segment(avctx, p, segment_length, sub);
1485 av_dlog(avctx, "Subtitling segment type 0x%x, page id %d, length %d\n",
1486 segment_type, page_id, segment_length);
1491 p += segment_length;
1498 AVCodec ff_dvbsub_decoder = {
1500 AVMEDIA_TYPE_SUBTITLE,
1501 CODEC_ID_DVB_SUBTITLE,
1502 sizeof(DVBSubContext),
1503 dvbsub_init_decoder,
1505 dvbsub_close_decoder,
1507 .long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),