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
23 #include "bitstream.h"
24 #include "colorspace.h"
27 //#define DEBUG_PACKET_CONTENTS
28 //#define DEBUG_SAVE_IMAGES
30 #define DVBSUB_PAGE_SEGMENT 0x10
31 #define DVBSUB_REGION_SEGMENT 0x11
32 #define DVBSUB_CLUT_SEGMENT 0x12
33 #define DVBSUB_OBJECT_SEGMENT 0x13
34 #define DVBSUB_DISPLAY_SEGMENT 0x80
36 #define cm (ff_cropTbl + MAX_NEG_CROP)
38 #ifdef DEBUG_SAVE_IMAGES
41 static void png_save(const char *filename, uint8_t *bitmap, int w, int h,
42 uint32_t *rgba_palette)
46 char fname[40], fname2[40];
49 snprintf(fname, 40, "%s.ppm", filename);
51 f = fopen(fname, "w");
60 for(y = 0; y < h; y++) {
61 for(x = 0; x < w; x++) {
62 v = rgba_palette[bitmap[y * w + x]];
63 putc((v >> 16) & 0xff, f);
64 putc((v >> 8) & 0xff, f);
65 putc((v >> 0) & 0xff, f);
71 snprintf(fname2, 40, "%s-a.pgm", filename);
73 f = fopen(fname2, "w");
82 for(y = 0; y < h; y++) {
83 for(x = 0; x < w; x++) {
84 v = rgba_palette[bitmap[y * w + x]];
85 putc((v >> 24) & 0xff, f);
90 snprintf(command, 1024, "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
93 snprintf(command, 1024, "rm %s %s", fname, fname2);
98 static void png_save2(const char *filename, uint32_t *bitmap, int w, int h)
102 char fname[40], fname2[40];
105 snprintf(fname, sizeof(fname), "%s.ppm", filename);
107 f = fopen(fname, "w");
116 for(y = 0; y < h; y++) {
117 for(x = 0; x < w; x++) {
118 v = bitmap[y * w + x];
119 putc((v >> 16) & 0xff, f);
120 putc((v >> 8) & 0xff, f);
121 putc((v >> 0) & 0xff, f);
127 snprintf(fname2, sizeof(fname2), "%s-a.pgm", filename);
129 f = fopen(fname2, "w");
138 for(y = 0; y < h; y++) {
139 for(x = 0; x < w; x++) {
140 v = bitmap[y * w + x];
141 putc((v >> 24) & 0xff, f);
146 snprintf(command, sizeof(command), "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
149 snprintf(command, sizeof(command), "rm %s %s", fname, fname2);
154 #define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
156 typedef struct DVBSubCLUT {
161 uint32_t clut256[256];
163 struct DVBSubCLUT *next;
166 static DVBSubCLUT default_clut;
168 typedef struct DVBSubObjectDisplay {
178 struct DVBSubObjectDisplay *region_list_next;
179 struct DVBSubObjectDisplay *object_list_next;
180 } DVBSubObjectDisplay;
182 typedef struct DVBSubObject {
187 DVBSubObjectDisplay *display_list;
189 struct DVBSubObject *next;
192 typedef struct DVBSubRegionDisplay {
198 struct DVBSubRegionDisplay *next;
199 } DVBSubRegionDisplay;
201 typedef struct DVBSubRegion {
214 DVBSubObjectDisplay *display_list;
216 struct DVBSubRegion *next;
219 typedef struct DVBSubContext {
224 DVBSubRegion *region_list;
225 DVBSubCLUT *clut_list;
226 DVBSubObject *object_list;
228 int display_list_size;
229 DVBSubRegionDisplay *display_list;
233 static DVBSubObject* get_object(DVBSubContext *ctx, int object_id)
235 DVBSubObject *ptr = ctx->object_list;
237 while (ptr && ptr->id != object_id) {
244 static DVBSubCLUT* get_clut(DVBSubContext *ctx, int clut_id)
246 DVBSubCLUT *ptr = ctx->clut_list;
248 while (ptr && ptr->id != clut_id) {
255 static DVBSubRegion* get_region(DVBSubContext *ctx, int region_id)
257 DVBSubRegion *ptr = ctx->region_list;
259 while (ptr && ptr->id != region_id) {
266 static void delete_region_display_list(DVBSubContext *ctx, DVBSubRegion *region)
268 DVBSubObject *object, *obj2, **obj2_ptr;
269 DVBSubObjectDisplay *display, *obj_disp, **obj_disp_ptr;
271 while (region->display_list) {
272 display = region->display_list;
274 object = get_object(ctx, display->object_id);
277 obj_disp_ptr = &object->display_list;
278 obj_disp = *obj_disp_ptr;
280 while (obj_disp && obj_disp != display) {
281 obj_disp_ptr = &obj_disp->object_list_next;
282 obj_disp = *obj_disp_ptr;
286 *obj_disp_ptr = obj_disp->object_list_next;
288 if (!object->display_list) {
289 obj2_ptr = &ctx->object_list;
292 while (obj2 != object) {
294 obj2_ptr = &obj2->next;
298 *obj2_ptr = obj2->next;
305 region->display_list = display->region_list_next;
312 static void delete_state(DVBSubContext *ctx)
314 DVBSubRegion *region;
317 while (ctx->region_list) {
318 region = ctx->region_list;
320 ctx->region_list = region->next;
322 delete_region_display_list(ctx, region);
324 av_free(region->pbuf);
329 while (ctx->clut_list) {
330 clut = ctx->clut_list;
332 ctx->clut_list = clut->next;
337 /* Should already be null */
338 if (ctx->object_list)
339 av_log(0, AV_LOG_ERROR, "Memory deallocation error!\n");
342 static av_cold int dvbsub_init_decoder(AVCodecContext *avctx)
344 int i, r, g, b, a = 0;
345 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
347 memset(avctx->priv_data, 0, sizeof(DVBSubContext));
349 ctx->composition_id = avctx->sub_id & 0xffff;
350 ctx->ancillary_id = avctx->sub_id >> 16;
352 default_clut.id = -1;
353 default_clut.next = NULL;
355 default_clut.clut4[0] = RGBA( 0, 0, 0, 0);
356 default_clut.clut4[1] = RGBA(255, 255, 255, 255);
357 default_clut.clut4[2] = RGBA( 0, 0, 0, 255);
358 default_clut.clut4[3] = RGBA(127, 127, 127, 255);
360 default_clut.clut16[0] = RGBA( 0, 0, 0, 0);
361 for (i = 1; i < 16; i++) {
363 r = (i & 1) ? 255 : 0;
364 g = (i & 2) ? 255 : 0;
365 b = (i & 4) ? 255 : 0;
367 r = (i & 1) ? 127 : 0;
368 g = (i & 2) ? 127 : 0;
369 b = (i & 4) ? 127 : 0;
371 default_clut.clut16[i] = RGBA(r, g, b, 255);
374 default_clut.clut256[0] = RGBA( 0, 0, 0, 0);
375 for (i = 1; i < 256; i++) {
377 r = (i & 1) ? 255 : 0;
378 g = (i & 2) ? 255 : 0;
379 b = (i & 4) ? 255 : 0;
384 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
385 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
386 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
390 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
391 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
392 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
396 r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
397 g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
398 b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
402 r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
403 g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
404 b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
409 default_clut.clut256[i] = RGBA(r, g, b, a);
415 static av_cold int dvbsub_close_decoder(AVCodecContext *avctx)
417 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
418 DVBSubRegionDisplay *display;
422 while (ctx->display_list) {
423 display = ctx->display_list;
424 ctx->display_list = display->next;
432 static int dvbsub_read_2bit_string(uint8_t *destbuf, int dbuf_len,
433 const uint8_t **srcbuf, int buf_size,
434 int non_mod, uint8_t *map_table)
442 init_get_bits(&gb, *srcbuf, buf_size << 8);
444 while (get_bits_count(&gb) < (buf_size << 8) && pixels_read < dbuf_len) {
445 bits = get_bits(&gb, 2);
448 if (non_mod != 1 || bits != 1) {
450 *destbuf++ = map_table[bits];
456 bits = get_bits1(&gb);
458 run_length = get_bits(&gb, 3) + 3;
459 bits = get_bits(&gb, 2);
461 if (non_mod == 1 && bits == 1)
462 pixels_read += run_length;
465 bits = map_table[bits];
466 while (run_length-- > 0 && pixels_read < dbuf_len) {
472 bits = get_bits1(&gb);
474 bits = get_bits(&gb, 2);
476 run_length = get_bits(&gb, 4) + 12;
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) {
489 } else if (bits == 3) {
490 run_length = get_bits(&gb, 8) + 29;
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 == 1) {
509 if (pixels_read <= dbuf_len) {
514 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
529 if (get_bits(&gb, 6))
530 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
532 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
537 static int dvbsub_read_4bit_string(uint8_t *destbuf, int dbuf_len,
538 const uint8_t **srcbuf, int buf_size,
539 int non_mod, uint8_t *map_table)
547 init_get_bits(&gb, *srcbuf, buf_size << 8);
549 while (get_bits_count(&gb) < (buf_size << 8) && pixels_read < dbuf_len) {
550 bits = get_bits(&gb, 4);
553 if (non_mod != 1 || bits != 1) {
555 *destbuf++ = map_table[bits];
561 bits = get_bits1(&gb);
563 run_length = get_bits(&gb, 3);
565 if (run_length == 0) {
566 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
577 while (run_length-- > 0 && pixels_read < dbuf_len) {
582 bits = get_bits1(&gb);
584 run_length = get_bits(&gb, 2) + 4;
585 bits = get_bits(&gb, 4);
587 if (non_mod == 1 && bits == 1)
588 pixels_read += run_length;
591 bits = map_table[bits];
592 while (run_length-- > 0 && pixels_read < dbuf_len) {
598 bits = get_bits(&gb, 2);
600 run_length = get_bits(&gb, 4) + 9;
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) {
613 } else if (bits == 3) {
614 run_length = get_bits(&gb, 8) + 25;
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 == 1) {
633 if (pixels_read <= dbuf_len) {
650 if (get_bits(&gb, 8))
651 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
653 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
658 static int dvbsub_read_8bit_string(uint8_t *destbuf, int dbuf_len,
659 const uint8_t **srcbuf, int buf_size,
660 int non_mod, uint8_t *map_table)
662 const uint8_t *sbuf_end = (*srcbuf) + buf_size;
667 while (*srcbuf < sbuf_end && pixels_read < dbuf_len) {
671 if (non_mod != 1 || bits != 1) {
673 *destbuf++ = map_table[bits];
680 run_length = bits & 0x7f;
681 if ((bits & 0x80) == 0) {
682 if (run_length == 0) {
690 while (run_length-- > 0 && pixels_read < dbuf_len) {
697 if (non_mod == 1 && bits == 1)
698 pixels_read += run_length;
700 bits = map_table[bits];
701 else while (run_length-- > 0 && pixels_read < dbuf_len) {
710 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
717 static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
718 const uint8_t *buf, int buf_size, int top_bottom, int non_mod)
720 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
722 DVBSubRegion *region = get_region(ctx, display->region_id);
723 const uint8_t *buf_end = buf + buf_size;
728 uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
729 uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
730 uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
731 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
735 av_log(avctx, AV_LOG_INFO, "DVB pixel block size %d, %s field:\n", buf_size,
736 top_bottom ? "bottom" : "top");
739 #ifdef DEBUG_PACKET_CONTENTS
740 for (i = 0; i < buf_size; i++) {
742 av_log(avctx, AV_LOG_INFO, "0x%08p: ", buf+i);
744 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
746 av_log(avctx, AV_LOG_INFO, "\n");
750 av_log(avctx, AV_LOG_INFO, "\n");
759 x_pos = display->x_pos;
760 y_pos = display->y_pos;
762 if ((y_pos & 1) != top_bottom)
765 while (buf < buf_end) {
766 if (x_pos > region->width || y_pos > region->height) {
767 av_log(avctx, AV_LOG_ERROR, "Invalid object location!\n");
773 if (region->depth == 8)
775 else if (region->depth == 4)
780 x_pos += dvbsub_read_2bit_string(pbuf + (y_pos * region->width) + x_pos,
781 region->width - x_pos, &buf, buf_size,
785 if (region->depth < 4) {
786 av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
790 if (region->depth == 8)
795 x_pos += dvbsub_read_4bit_string(pbuf + (y_pos * region->width) + x_pos,
796 region->width - x_pos, &buf, buf_size,
800 if (region->depth < 8) {
801 av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
805 x_pos += dvbsub_read_8bit_string(pbuf + (y_pos * region->width) + x_pos,
806 region->width - x_pos, &buf, buf_size,
811 map2to4[0] = (*buf) >> 4;
812 map2to4[1] = (*buf++) & 0xf;
813 map2to4[2] = (*buf) >> 4;
814 map2to4[3] = (*buf++) & 0xf;
817 for (i = 0; i < 4; i++)
821 for (i = 0; i < 16; i++)
826 x_pos = display->x_pos;
830 av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
836 static void dvbsub_parse_object_segment(AVCodecContext *avctx,
837 const uint8_t *buf, int buf_size)
839 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
841 const uint8_t *buf_end = buf + buf_size;
842 const uint8_t *block;
844 DVBSubObject *object;
845 DVBSubObjectDisplay *display;
846 int top_field_len, bottom_field_len;
848 int coding_method, non_modifying_color;
850 object_id = AV_RB16(buf);
853 object = get_object(ctx, object_id);
858 coding_method = ((*buf) >> 2) & 3;
859 non_modifying_color = ((*buf++) >> 1) & 1;
861 if (coding_method == 0) {
862 top_field_len = AV_RB16(buf);
864 bottom_field_len = AV_RB16(buf);
867 if (buf + top_field_len + bottom_field_len > buf_end) {
868 av_log(avctx, AV_LOG_ERROR, "Field data size too large\n");
872 for (display = object->display_list; display; display = display->object_list_next) {
875 dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
876 non_modifying_color);
878 if (bottom_field_len > 0)
879 block = buf + top_field_len;
881 bottom_field_len = top_field_len;
883 dvbsub_parse_pixel_data_block(avctx, display, block, bottom_field_len, 1,
884 non_modifying_color);
887 /* } else if (coding_method == 1) {*/
890 av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
895 static void dvbsub_parse_clut_segment(AVCodecContext *avctx,
896 const uint8_t *buf, int buf_size)
898 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
900 const uint8_t *buf_end = buf + buf_size;
903 int entry_id, depth , full_range;
904 int y, cr, cb, alpha;
905 int r, g, b, r_add, g_add, b_add;
907 #ifdef DEBUG_PACKET_CONTENTS
910 av_log(avctx, AV_LOG_INFO, "DVB clut packet:\n");
912 for (i=0; i < buf_size; i++) {
913 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
915 av_log(avctx, AV_LOG_INFO, "\n");
919 av_log(avctx, AV_LOG_INFO, "\n");
926 clut = get_clut(ctx, clut_id);
929 clut = av_malloc(sizeof(DVBSubCLUT));
931 memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
935 clut->next = ctx->clut_list;
936 ctx->clut_list = clut;
939 while (buf + 4 < buf_end) {
942 depth = (*buf) & 0xe0;
945 av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
949 full_range = (*buf++) & 1;
958 cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
959 cb = (buf[1] << 2) & 0xf0;
960 alpha = (buf[1] << 6) & 0xc0;
968 YUV_TO_RGB1_CCIR(cb, cr);
969 YUV_TO_RGB2_CCIR(r, g, b, y);
972 av_log(avctx, AV_LOG_INFO, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
976 clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
978 clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
980 clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
985 static void dvbsub_parse_region_segment(AVCodecContext *avctx,
986 const uint8_t *buf, int buf_size)
988 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
990 const uint8_t *buf_end = buf + buf_size;
991 int region_id, object_id;
992 DVBSubRegion *region;
993 DVBSubObject *object;
994 DVBSubObjectDisplay *display;
1002 region = get_region(ctx, region_id);
1005 region = av_mallocz(sizeof(DVBSubRegion));
1007 region->id = region_id;
1009 region->next = ctx->region_list;
1010 ctx->region_list = region;
1013 fill = ((*buf++) >> 3) & 1;
1015 region->width = AV_RB16(buf);
1017 region->height = AV_RB16(buf);
1020 if (region->width * region->height != region->buf_size) {
1022 av_free(region->pbuf);
1024 region->buf_size = region->width * region->height;
1026 region->pbuf = av_malloc(region->buf_size);
1031 region->depth = 1 << (((*buf++) >> 2) & 7);
1032 if(region->depth<2 || region->depth>8){
1033 av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);
1036 region->clut = *buf++;
1038 if (region->depth == 8)
1039 region->bgcolor = *buf++;
1043 if (region->depth == 4)
1044 region->bgcolor = (((*buf++) >> 4) & 15);
1046 region->bgcolor = (((*buf++) >> 2) & 3);
1050 av_log(avctx, AV_LOG_INFO, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
1054 memset(region->pbuf, region->bgcolor, region->buf_size);
1056 av_log(avctx, AV_LOG_INFO, "Fill region (%d)\n", region->bgcolor);
1060 delete_region_display_list(ctx, region);
1062 while (buf + 5 < buf_end) {
1063 object_id = AV_RB16(buf);
1066 object = get_object(ctx, object_id);
1069 object = av_mallocz(sizeof(DVBSubObject));
1071 object->id = object_id;
1072 object->next = ctx->object_list;
1073 ctx->object_list = object;
1076 object->type = (*buf) >> 6;
1078 display = av_mallocz(sizeof(DVBSubObjectDisplay));
1080 display->object_id = object_id;
1081 display->region_id = region_id;
1083 display->x_pos = AV_RB16(buf) & 0xfff;
1085 display->y_pos = AV_RB16(buf) & 0xfff;
1088 if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
1089 display->fgcolor = *buf++;
1090 display->bgcolor = *buf++;
1093 display->region_list_next = region->display_list;
1094 region->display_list = display;
1096 display->object_list_next = object->display_list;
1097 object->display_list = display;
1101 static void dvbsub_parse_page_segment(AVCodecContext *avctx,
1102 const uint8_t *buf, int buf_size)
1104 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
1105 DVBSubRegionDisplay *display;
1106 DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
1108 const uint8_t *buf_end = buf + buf_size;
1115 ctx->time_out = *buf++;
1116 page_state = ((*buf++) >> 2) & 3;
1119 av_log(avctx, AV_LOG_INFO, "Page time out %ds, state %d\n", ctx->time_out, page_state);
1122 if (page_state == 2) {
1126 tmp_display_list = ctx->display_list;
1127 ctx->display_list = NULL;
1128 ctx->display_list_size = 0;
1130 while (buf + 5 < buf_end) {
1134 display = tmp_display_list;
1135 tmp_ptr = &tmp_display_list;
1137 while (display && display->region_id != region_id) {
1138 tmp_ptr = &display->next;
1139 display = display->next;
1143 display = av_mallocz(sizeof(DVBSubRegionDisplay));
1145 display->region_id = region_id;
1147 display->x_pos = AV_RB16(buf);
1149 display->y_pos = AV_RB16(buf);
1152 *tmp_ptr = display->next;
1154 display->next = ctx->display_list;
1155 ctx->display_list = display;
1156 ctx->display_list_size++;
1159 av_log(avctx, AV_LOG_INFO, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
1163 while (tmp_display_list) {
1164 display = tmp_display_list;
1166 tmp_display_list = display->next;
1174 #ifdef DEBUG_SAVE_IMAGES
1175 static void save_display_set(DVBSubContext *ctx)
1177 DVBSubRegion *region;
1178 DVBSubRegionDisplay *display;
1180 uint32_t *clut_table;
1181 int x_pos, y_pos, width, height;
1182 int x, y, y_off, x_off;
1185 static int fileno_index = 0;
1192 for (display = ctx->display_list; display; display = display->next) {
1193 region = get_region(ctx, display->region_id);
1196 x_pos = display->x_pos;
1197 y_pos = display->y_pos;
1198 width = region->width;
1199 height = region->height;
1201 if (display->x_pos < x_pos) {
1202 width += (x_pos - display->x_pos);
1203 x_pos = display->x_pos;
1206 if (display->y_pos < y_pos) {
1207 height += (y_pos - display->y_pos);
1208 y_pos = display->y_pos;
1211 if (display->x_pos + region->width > x_pos + width) {
1212 width = display->x_pos + region->width - x_pos;
1215 if (display->y_pos + region->height > y_pos + height) {
1216 height = display->y_pos + region->height - y_pos;
1223 pbuf = av_malloc(width * height * 4);
1225 for (display = ctx->display_list; display; display = display->next) {
1226 region = get_region(ctx, display->region_id);
1228 x_off = display->x_pos - x_pos;
1229 y_off = display->y_pos - y_pos;
1231 clut = get_clut(ctx, region->clut);
1234 clut = &default_clut;
1236 switch (region->depth) {
1238 clut_table = clut->clut4;
1241 clut_table = clut->clut256;
1245 clut_table = clut->clut16;
1249 for (y = 0; y < region->height; y++) {
1250 for (x = 0; x < region->width; x++) {
1251 pbuf[((y + y_off) * width) + x_off + x] =
1252 clut_table[region->pbuf[y * region->width + x]];
1258 snprintf(filename, sizeof(filename), "dvbs.%d", fileno_index);
1260 png_save2(filename, pbuf, width, height);
1269 static int dvbsub_display_end_segment(AVCodecContext *avctx, const uint8_t *buf,
1270 int buf_size, AVSubtitle *sub)
1272 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
1274 DVBSubRegion *region;
1275 DVBSubRegionDisplay *display;
1276 AVSubtitleRect *rect;
1278 uint32_t *clut_table;
1282 sub->start_display_time = 0;
1283 sub->end_display_time = ctx->time_out * 1000;
1286 sub->num_rects = ctx->display_list_size;
1288 if (sub->num_rects > 0){
1289 sub->rects = av_mallocz(sizeof(*sub->rects) * sub->num_rects);
1290 for(i=0; i<sub->num_rects; i++)
1291 sub->rects[i] = av_mallocz(sizeof(*sub->rects[i]));
1296 for (display = ctx->display_list; display; display = display->next) {
1297 region = get_region(ctx, display->region_id);
1298 rect = sub->rects[i];
1303 rect->x = display->x_pos;
1304 rect->y = display->y_pos;
1305 rect->w = region->width;
1306 rect->h = region->height;
1307 rect->nb_colors = 16;
1308 rect->pict.linesize[0] = region->width;
1310 clut = get_clut(ctx, region->clut);
1313 clut = &default_clut;
1315 switch (region->depth) {
1317 clut_table = clut->clut4;
1320 clut_table = clut->clut256;
1324 clut_table = clut->clut16;
1328 rect->pict.data[1] = av_malloc((1 << region->depth) * sizeof(uint32_t));
1329 memcpy(rect->pict.data[1], clut_table, (1 << region->depth) * sizeof(uint32_t));
1331 rect->pict.data[0] = av_malloc(region->buf_size);
1332 memcpy(rect->pict.data[0], region->pbuf, region->buf_size);
1339 #ifdef DEBUG_SAVE_IMAGES
1340 save_display_set(ctx);
1346 static int dvbsub_decode(AVCodecContext *avctx,
1347 void *data, int *data_size,
1348 const uint8_t *buf, int buf_size)
1350 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
1351 AVSubtitle *sub = (AVSubtitle*) data;
1352 const uint8_t *p, *p_end;
1357 #ifdef DEBUG_PACKET_CONTENTS
1360 av_log(avctx, AV_LOG_INFO, "DVB sub packet:\n");
1362 for (i=0; i < buf_size; i++) {
1363 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
1365 av_log(avctx, AV_LOG_INFO, "\n");
1369 av_log(avctx, AV_LOG_INFO, "\n");
1377 p_end = buf + buf_size;
1379 while (p < p_end && *p == 0x0f) {
1381 segment_type = *p++;
1382 page_id = AV_RB16(p);
1384 segment_length = AV_RB16(p);
1387 if (page_id == ctx->composition_id || page_id == ctx->ancillary_id) {
1388 switch (segment_type) {
1389 case DVBSUB_PAGE_SEGMENT:
1390 dvbsub_parse_page_segment(avctx, p, segment_length);
1392 case DVBSUB_REGION_SEGMENT:
1393 dvbsub_parse_region_segment(avctx, p, segment_length);
1395 case DVBSUB_CLUT_SEGMENT:
1396 dvbsub_parse_clut_segment(avctx, p, segment_length);
1398 case DVBSUB_OBJECT_SEGMENT:
1399 dvbsub_parse_object_segment(avctx, p, segment_length);
1401 case DVBSUB_DISPLAY_SEGMENT:
1402 *data_size = dvbsub_display_end_segment(avctx, p, segment_length, sub);
1406 av_log(avctx, AV_LOG_INFO, "Subtitling segment type 0x%x, page id %d, length %d\n",
1407 segment_type, page_id, segment_length);
1413 p += segment_length;
1418 av_log(avctx, AV_LOG_INFO, "Junk at end of packet\n");
1427 AVCodec dvbsub_decoder = {
1429 CODEC_TYPE_SUBTITLE,
1430 CODEC_ID_DVB_SUBTITLE,
1431 sizeof(DVBSubContext),
1432 dvbsub_init_decoder,
1434 dvbsub_close_decoder,
1436 .long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),