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 = (DVBSubContext*) avctx->priv_data;
361 memset(avctx->priv_data, 0, sizeof(DVBSubContext));
363 ctx->composition_id = avctx->sub_id & 0xffff;
364 ctx->ancillary_id = avctx->sub_id >> 16;
366 default_clut.id = -1;
367 default_clut.next = NULL;
369 default_clut.clut4[0] = RGBA( 0, 0, 0, 0);
370 default_clut.clut4[1] = RGBA(255, 255, 255, 255);
371 default_clut.clut4[2] = RGBA( 0, 0, 0, 255);
372 default_clut.clut4[3] = RGBA(127, 127, 127, 255);
374 default_clut.clut16[0] = RGBA( 0, 0, 0, 0);
375 for (i = 1; i < 16; i++) {
377 r = (i & 1) ? 255 : 0;
378 g = (i & 2) ? 255 : 0;
379 b = (i & 4) ? 255 : 0;
381 r = (i & 1) ? 127 : 0;
382 g = (i & 2) ? 127 : 0;
383 b = (i & 4) ? 127 : 0;
385 default_clut.clut16[i] = RGBA(r, g, b, 255);
388 default_clut.clut256[0] = RGBA( 0, 0, 0, 0);
389 for (i = 1; i < 256; i++) {
391 r = (i & 1) ? 255 : 0;
392 g = (i & 2) ? 255 : 0;
393 b = (i & 4) ? 255 : 0;
398 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
399 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
400 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
404 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
405 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
406 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
410 r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
411 g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
412 b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
416 r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
417 g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
418 b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
423 default_clut.clut256[i] = RGBA(r, g, b, a);
429 static av_cold int dvbsub_close_decoder(AVCodecContext *avctx)
431 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
432 DVBSubRegionDisplay *display;
436 while (ctx->display_list) {
437 display = ctx->display_list;
438 ctx->display_list = display->next;
446 static int dvbsub_read_2bit_string(uint8_t *destbuf, int dbuf_len,
447 const uint8_t **srcbuf, int buf_size,
448 int non_mod, uint8_t *map_table)
456 init_get_bits(&gb, *srcbuf, buf_size << 3);
458 while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
459 bits = get_bits(&gb, 2);
462 if (non_mod != 1 || bits != 1) {
464 *destbuf++ = map_table[bits];
470 bits = get_bits1(&gb);
472 run_length = get_bits(&gb, 3) + 3;
473 bits = get_bits(&gb, 2);
475 if (non_mod == 1 && bits == 1)
476 pixels_read += run_length;
479 bits = map_table[bits];
480 while (run_length-- > 0 && pixels_read < dbuf_len) {
486 bits = get_bits1(&gb);
488 bits = get_bits(&gb, 2);
490 run_length = get_bits(&gb, 4) + 12;
491 bits = get_bits(&gb, 2);
493 if (non_mod == 1 && bits == 1)
494 pixels_read += run_length;
497 bits = map_table[bits];
498 while (run_length-- > 0 && pixels_read < dbuf_len) {
503 } else if (bits == 3) {
504 run_length = get_bits(&gb, 8) + 29;
505 bits = get_bits(&gb, 2);
507 if (non_mod == 1 && bits == 1)
508 pixels_read += run_length;
511 bits = map_table[bits];
512 while (run_length-- > 0 && pixels_read < dbuf_len) {
517 } else if (bits == 1) {
523 if (pixels_read <= dbuf_len) {
528 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
543 if (get_bits(&gb, 6))
544 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
546 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
551 static int dvbsub_read_4bit_string(uint8_t *destbuf, int dbuf_len,
552 const uint8_t **srcbuf, int buf_size,
553 int non_mod, uint8_t *map_table)
561 init_get_bits(&gb, *srcbuf, buf_size << 3);
563 while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
564 bits = get_bits(&gb, 4);
567 if (non_mod != 1 || bits != 1) {
569 *destbuf++ = map_table[bits];
575 bits = get_bits1(&gb);
577 run_length = get_bits(&gb, 3);
579 if (run_length == 0) {
580 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
591 while (run_length-- > 0 && pixels_read < dbuf_len) {
596 bits = get_bits1(&gb);
598 run_length = get_bits(&gb, 2) + 4;
599 bits = get_bits(&gb, 4);
601 if (non_mod == 1 && bits == 1)
602 pixels_read += run_length;
605 bits = map_table[bits];
606 while (run_length-- > 0 && pixels_read < dbuf_len) {
612 bits = get_bits(&gb, 2);
614 run_length = get_bits(&gb, 4) + 9;
615 bits = get_bits(&gb, 4);
617 if (non_mod == 1 && bits == 1)
618 pixels_read += run_length;
621 bits = map_table[bits];
622 while (run_length-- > 0 && pixels_read < dbuf_len) {
627 } else if (bits == 3) {
628 run_length = get_bits(&gb, 8) + 25;
629 bits = get_bits(&gb, 4);
631 if (non_mod == 1 && bits == 1)
632 pixels_read += run_length;
635 bits = map_table[bits];
636 while (run_length-- > 0 && pixels_read < dbuf_len) {
641 } else if (bits == 1) {
647 if (pixels_read <= dbuf_len) {
664 if (get_bits(&gb, 8))
665 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
667 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
672 static int dvbsub_read_8bit_string(uint8_t *destbuf, int dbuf_len,
673 const uint8_t **srcbuf, int buf_size,
674 int non_mod, uint8_t *map_table)
676 const uint8_t *sbuf_end = (*srcbuf) + buf_size;
681 while (*srcbuf < sbuf_end && pixels_read < dbuf_len) {
685 if (non_mod != 1 || bits != 1) {
687 *destbuf++ = map_table[bits];
694 run_length = bits & 0x7f;
695 if ((bits & 0x80) == 0) {
696 if (run_length == 0) {
704 while (run_length-- > 0 && pixels_read < dbuf_len) {
711 if (non_mod == 1 && bits == 1)
712 pixels_read += run_length;
714 bits = map_table[bits];
715 else while (run_length-- > 0 && pixels_read < dbuf_len) {
724 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
731 static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
732 const uint8_t *buf, int buf_size, int top_bottom, int non_mod)
734 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
736 DVBSubRegion *region = get_region(ctx, display->region_id);
737 const uint8_t *buf_end = buf + buf_size;
742 uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
743 uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
744 uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
745 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
748 dprintf(avctx, "DVB pixel block size %d, %s field:\n", buf_size,
749 top_bottom ? "bottom" : "top");
751 #ifdef DEBUG_PACKET_CONTENTS
752 for (i = 0; i < buf_size; i++) {
754 av_log(avctx, AV_LOG_INFO, "0x%08p: ", buf+i);
756 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
758 av_log(avctx, AV_LOG_INFO, "\n");
762 av_log(avctx, AV_LOG_INFO, "\n");
771 x_pos = display->x_pos;
772 y_pos = display->y_pos;
774 if ((y_pos & 1) != top_bottom)
777 while (buf < buf_end) {
778 if (x_pos > region->width || y_pos > region->height) {
779 av_log(avctx, AV_LOG_ERROR, "Invalid object location!\n");
785 if (region->depth == 8)
787 else if (region->depth == 4)
792 x_pos += dvbsub_read_2bit_string(pbuf + (y_pos * region->width) + x_pos,
793 region->width - x_pos, &buf, buf_size,
797 if (region->depth < 4) {
798 av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
802 if (region->depth == 8)
807 x_pos += dvbsub_read_4bit_string(pbuf + (y_pos * region->width) + x_pos,
808 region->width - x_pos, &buf, buf_size,
812 if (region->depth < 8) {
813 av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
817 x_pos += dvbsub_read_8bit_string(pbuf + (y_pos * region->width) + x_pos,
818 region->width - x_pos, &buf, buf_size,
823 map2to4[0] = (*buf) >> 4;
824 map2to4[1] = (*buf++) & 0xf;
825 map2to4[2] = (*buf) >> 4;
826 map2to4[3] = (*buf++) & 0xf;
829 for (i = 0; i < 4; i++)
833 for (i = 0; i < 16; i++)
838 x_pos = display->x_pos;
842 av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
848 static void dvbsub_parse_object_segment(AVCodecContext *avctx,
849 const uint8_t *buf, int buf_size)
851 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
853 const uint8_t *buf_end = buf + buf_size;
854 const uint8_t *block;
856 DVBSubObject *object;
857 DVBSubObjectDisplay *display;
858 int top_field_len, bottom_field_len;
860 int coding_method, non_modifying_color;
862 object_id = AV_RB16(buf);
865 object = get_object(ctx, object_id);
870 coding_method = ((*buf) >> 2) & 3;
871 non_modifying_color = ((*buf++) >> 1) & 1;
873 if (coding_method == 0) {
874 top_field_len = AV_RB16(buf);
876 bottom_field_len = AV_RB16(buf);
879 if (buf + top_field_len + bottom_field_len > buf_end) {
880 av_log(avctx, AV_LOG_ERROR, "Field data size too large\n");
884 for (display = object->display_list; display; display = display->object_list_next) {
887 dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
888 non_modifying_color);
890 if (bottom_field_len > 0)
891 block = buf + top_field_len;
893 bottom_field_len = top_field_len;
895 dvbsub_parse_pixel_data_block(avctx, display, block, bottom_field_len, 1,
896 non_modifying_color);
899 /* } else if (coding_method == 1) {*/
902 av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
907 static void dvbsub_parse_clut_segment(AVCodecContext *avctx,
908 const uint8_t *buf, int buf_size)
910 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
912 const uint8_t *buf_end = buf + buf_size;
915 int entry_id, depth , full_range;
916 int y, cr, cb, alpha;
917 int r, g, b, r_add, g_add, b_add;
919 #ifdef DEBUG_PACKET_CONTENTS
922 av_log(avctx, AV_LOG_INFO, "DVB clut packet:\n");
924 for (i=0; i < buf_size; i++) {
925 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
927 av_log(avctx, AV_LOG_INFO, "\n");
931 av_log(avctx, AV_LOG_INFO, "\n");
938 clut = get_clut(ctx, clut_id);
941 clut = av_malloc(sizeof(DVBSubCLUT));
943 memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
947 clut->next = ctx->clut_list;
948 ctx->clut_list = clut;
951 while (buf + 4 < buf_end) {
954 depth = (*buf) & 0xe0;
957 av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
961 full_range = (*buf++) & 1;
970 cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
971 cb = (buf[1] << 2) & 0xf0;
972 alpha = (buf[1] << 6) & 0xc0;
980 YUV_TO_RGB1_CCIR(cb, cr);
981 YUV_TO_RGB2_CCIR(r, g, b, y);
983 dprintf(avctx, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
986 clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
988 clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
990 clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
995 static void dvbsub_parse_region_segment(AVCodecContext *avctx,
996 const uint8_t *buf, int buf_size)
998 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
1000 const uint8_t *buf_end = buf + buf_size;
1001 int region_id, object_id;
1002 DVBSubRegion *region;
1003 DVBSubObject *object;
1004 DVBSubObjectDisplay *display;
1012 region = get_region(ctx, region_id);
1015 region = av_mallocz(sizeof(DVBSubRegion));
1017 region->id = region_id;
1019 region->next = ctx->region_list;
1020 ctx->region_list = region;
1023 fill = ((*buf++) >> 3) & 1;
1025 region->width = AV_RB16(buf);
1027 region->height = AV_RB16(buf);
1030 if (region->width * region->height != region->buf_size) {
1032 av_free(region->pbuf);
1034 region->buf_size = region->width * region->height;
1036 region->pbuf = av_malloc(region->buf_size);
1041 region->depth = 1 << (((*buf++) >> 2) & 7);
1042 if(region->depth<2 || region->depth>8){
1043 av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);
1046 region->clut = *buf++;
1048 if (region->depth == 8)
1049 region->bgcolor = *buf++;
1053 if (region->depth == 4)
1054 region->bgcolor = (((*buf++) >> 4) & 15);
1056 region->bgcolor = (((*buf++) >> 2) & 3);
1059 dprintf(avctx, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
1062 memset(region->pbuf, region->bgcolor, region->buf_size);
1063 dprintf(avctx, "Fill region (%d)\n", region->bgcolor);
1066 delete_region_display_list(ctx, region);
1068 while (buf + 5 < buf_end) {
1069 object_id = AV_RB16(buf);
1072 object = get_object(ctx, object_id);
1075 object = av_mallocz(sizeof(DVBSubObject));
1077 object->id = object_id;
1078 object->next = ctx->object_list;
1079 ctx->object_list = object;
1082 object->type = (*buf) >> 6;
1084 display = av_mallocz(sizeof(DVBSubObjectDisplay));
1086 display->object_id = object_id;
1087 display->region_id = region_id;
1089 display->x_pos = AV_RB16(buf) & 0xfff;
1091 display->y_pos = AV_RB16(buf) & 0xfff;
1094 if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
1095 display->fgcolor = *buf++;
1096 display->bgcolor = *buf++;
1099 display->region_list_next = region->display_list;
1100 region->display_list = display;
1102 display->object_list_next = object->display_list;
1103 object->display_list = display;
1107 static void dvbsub_parse_page_segment(AVCodecContext *avctx,
1108 const uint8_t *buf, int buf_size)
1110 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
1111 DVBSubRegionDisplay *display;
1112 DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
1114 const uint8_t *buf_end = buf + buf_size;
1121 ctx->time_out = *buf++;
1122 page_state = ((*buf++) >> 2) & 3;
1124 dprintf(avctx, "Page time out %ds, state %d\n", ctx->time_out, page_state);
1126 if (page_state == 2) {
1130 tmp_display_list = ctx->display_list;
1131 ctx->display_list = NULL;
1132 ctx->display_list_size = 0;
1134 while (buf + 5 < buf_end) {
1138 display = tmp_display_list;
1139 tmp_ptr = &tmp_display_list;
1141 while (display && display->region_id != region_id) {
1142 tmp_ptr = &display->next;
1143 display = display->next;
1147 display = av_mallocz(sizeof(DVBSubRegionDisplay));
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 dprintf(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;
1176 #ifdef DEBUG_SAVE_IMAGES
1177 static void save_display_set(DVBSubContext *ctx)
1179 DVBSubRegion *region;
1180 DVBSubRegionDisplay *display;
1182 uint32_t *clut_table;
1183 int x_pos, y_pos, width, height;
1184 int x, y, y_off, x_off;
1187 static int fileno_index = 0;
1194 for (display = ctx->display_list; display; display = display->next) {
1195 region = get_region(ctx, display->region_id);
1198 x_pos = display->x_pos;
1199 y_pos = display->y_pos;
1200 width = region->width;
1201 height = region->height;
1203 if (display->x_pos < x_pos) {
1204 width += (x_pos - display->x_pos);
1205 x_pos = display->x_pos;
1208 if (display->y_pos < y_pos) {
1209 height += (y_pos - display->y_pos);
1210 y_pos = display->y_pos;
1213 if (display->x_pos + region->width > x_pos + width) {
1214 width = display->x_pos + region->width - x_pos;
1217 if (display->y_pos + region->height > y_pos + height) {
1218 height = display->y_pos + region->height - y_pos;
1225 pbuf = av_malloc(width * height * 4);
1227 for (display = ctx->display_list; display; display = display->next) {
1228 region = get_region(ctx, display->region_id);
1230 x_off = display->x_pos - x_pos;
1231 y_off = display->y_pos - y_pos;
1233 clut = get_clut(ctx, region->clut);
1236 clut = &default_clut;
1238 switch (region->depth) {
1240 clut_table = clut->clut4;
1243 clut_table = clut->clut256;
1247 clut_table = clut->clut16;
1251 for (y = 0; y < region->height; y++) {
1252 for (x = 0; x < region->width; x++) {
1253 pbuf[((y + y_off) * width) + x_off + x] =
1254 clut_table[region->pbuf[y * region->width + x]];
1260 snprintf(filename, sizeof(filename), "dvbs.%d", fileno_index);
1262 png_save2(filename, pbuf, width, height);
1271 static void dvbsub_parse_display_definition_segment(AVCodecContext *avctx,
1275 DVBSubContext *ctx = avctx->priv_data;
1276 DVBSubDisplayDefinition *display_def = ctx->display_definition;
1277 int dds_version, info_byte;
1282 info_byte = bytestream_get_byte(&buf);
1283 dds_version = info_byte >> 4;
1284 if (display_def && display_def->version == dds_version)
1285 return; // already have this display definition version
1288 display_def = av_mallocz(sizeof(*display_def));
1289 ctx->display_definition = display_def;
1294 display_def->version = dds_version;
1297 display_def->width = bytestream_get_be16(&buf) + 1;
1298 display_def->height = bytestream_get_be16(&buf) + 1;
1303 if (info_byte & 1<<3) { // display_window_flag
1304 display_def->x = bytestream_get_be16(&buf);
1305 display_def->y = bytestream_get_be16(&buf);
1306 display_def->width = bytestream_get_be16(&buf) - display_def->x + 1;
1307 display_def->height = bytestream_get_be16(&buf) - display_def->y + 1;
1311 static int dvbsub_display_end_segment(AVCodecContext *avctx, const uint8_t *buf,
1312 int buf_size, AVSubtitle *sub)
1314 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
1315 DVBSubDisplayDefinition *display_def = ctx->display_definition;
1317 DVBSubRegion *region;
1318 DVBSubRegionDisplay *display;
1319 AVSubtitleRect *rect;
1321 uint32_t *clut_table;
1323 int offset_x=0, offset_y=0;
1326 sub->start_display_time = 0;
1327 sub->end_display_time = ctx->time_out * 1000;
1331 offset_x = display_def->x;
1332 offset_y = display_def->y;
1335 sub->num_rects = ctx->display_list_size;
1337 if (sub->num_rects > 0){
1338 sub->rects = av_mallocz(sizeof(*sub->rects) * sub->num_rects);
1339 for(i=0; i<sub->num_rects; i++)
1340 sub->rects[i] = av_mallocz(sizeof(*sub->rects[i]));
1345 for (display = ctx->display_list; display; display = display->next) {
1346 region = get_region(ctx, display->region_id);
1347 rect = sub->rects[i];
1352 rect->x = display->x_pos + offset_x;
1353 rect->y = display->y_pos + offset_y;
1354 rect->w = region->width;
1355 rect->h = region->height;
1356 rect->nb_colors = 16;
1357 rect->type = SUBTITLE_BITMAP;
1358 rect->pict.linesize[0] = region->width;
1360 clut = get_clut(ctx, region->clut);
1363 clut = &default_clut;
1365 switch (region->depth) {
1367 clut_table = clut->clut4;
1370 clut_table = clut->clut256;
1374 clut_table = clut->clut16;
1378 rect->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
1379 memcpy(rect->pict.data[1], clut_table, (1 << region->depth) * sizeof(uint32_t));
1381 rect->pict.data[0] = av_malloc(region->buf_size);
1382 memcpy(rect->pict.data[0], region->pbuf, region->buf_size);
1389 #ifdef DEBUG_SAVE_IMAGES
1390 save_display_set(ctx);
1396 static int dvbsub_decode(AVCodecContext *avctx,
1397 void *data, int *data_size,
1400 const uint8_t *buf = avpkt->data;
1401 int buf_size = avpkt->size;
1402 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
1403 AVSubtitle *sub = (AVSubtitle*) data;
1404 const uint8_t *p, *p_end;
1409 #ifdef DEBUG_PACKET_CONTENTS
1412 av_log(avctx, AV_LOG_INFO, "DVB sub packet:\n");
1414 for (i=0; i < buf_size; i++) {
1415 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
1417 av_log(avctx, AV_LOG_INFO, "\n");
1421 av_log(avctx, AV_LOG_INFO, "\n");
1429 p_end = buf + buf_size;
1431 while (p < p_end && *p == 0x0f) {
1433 segment_type = *p++;
1434 page_id = AV_RB16(p);
1436 segment_length = AV_RB16(p);
1439 if (page_id == ctx->composition_id || page_id == ctx->ancillary_id) {
1440 switch (segment_type) {
1441 case DVBSUB_PAGE_SEGMENT:
1442 dvbsub_parse_page_segment(avctx, p, segment_length);
1444 case DVBSUB_REGION_SEGMENT:
1445 dvbsub_parse_region_segment(avctx, p, segment_length);
1447 case DVBSUB_CLUT_SEGMENT:
1448 dvbsub_parse_clut_segment(avctx, p, segment_length);
1450 case DVBSUB_OBJECT_SEGMENT:
1451 dvbsub_parse_object_segment(avctx, p, segment_length);
1453 case DVBSUB_DISPLAYDEFINITION_SEGMENT:
1454 dvbsub_parse_display_definition_segment(avctx, p, segment_length);
1455 case DVBSUB_DISPLAY_SEGMENT:
1456 *data_size = dvbsub_display_end_segment(avctx, p, segment_length, sub);
1459 dprintf(avctx, "Subtitling segment type 0x%x, page id %d, length %d\n",
1460 segment_type, page_id, segment_length);
1465 p += segment_length;
1469 dprintf(avctx, "Junk at end of packet\n");
1477 AVCodec dvbsub_decoder = {
1479 AVMEDIA_TYPE_SUBTITLE,
1480 CODEC_ID_DVB_SUBTITLE,
1481 sizeof(DVBSubContext),
1482 dvbsub_init_decoder,
1484 dvbsub_close_decoder,
1486 .long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),