2 * DVB subtitle decoding
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"
26 #include "libavutil/colorspace.h"
28 #define DVBSUB_PAGE_SEGMENT 0x10
29 #define DVBSUB_REGION_SEGMENT 0x11
30 #define DVBSUB_CLUT_SEGMENT 0x12
31 #define DVBSUB_OBJECT_SEGMENT 0x13
32 #define DVBSUB_DISPLAYDEFINITION_SEGMENT 0x14
33 #define DVBSUB_DISPLAY_SEGMENT 0x80
35 #define cm (ff_crop_tab + MAX_NEG_CROP)
37 #define RGBA(r,g,b,a) (((unsigned)(a) << 24) | ((r) << 16) | ((g) << 8) | (b))
39 typedef struct DVBSubCLUT {
44 uint32_t clut256[256];
46 struct DVBSubCLUT *next;
49 static DVBSubCLUT default_clut;
51 typedef struct DVBSubObjectDisplay {
61 struct DVBSubObjectDisplay *region_list_next;
62 struct DVBSubObjectDisplay *object_list_next;
63 } DVBSubObjectDisplay;
65 typedef struct DVBSubObject {
70 DVBSubObjectDisplay *display_list;
72 struct DVBSubObject *next;
75 typedef struct DVBSubRegionDisplay {
81 struct DVBSubRegionDisplay *next;
82 } DVBSubRegionDisplay;
84 typedef struct DVBSubRegion {
97 DVBSubObjectDisplay *display_list;
99 struct DVBSubRegion *next;
102 typedef struct DVBSubDisplayDefinition {
109 } DVBSubDisplayDefinition;
111 typedef struct DVBSubContext {
116 DVBSubRegion *region_list;
117 DVBSubCLUT *clut_list;
118 DVBSubObject *object_list;
120 int display_list_size;
121 DVBSubRegionDisplay *display_list;
122 DVBSubDisplayDefinition *display_definition;
126 static DVBSubObject* get_object(DVBSubContext *ctx, int object_id)
128 DVBSubObject *ptr = ctx->object_list;
130 while (ptr && ptr->id != object_id) {
137 static DVBSubCLUT* get_clut(DVBSubContext *ctx, int clut_id)
139 DVBSubCLUT *ptr = ctx->clut_list;
141 while (ptr && ptr->id != clut_id) {
148 static DVBSubRegion* get_region(DVBSubContext *ctx, int region_id)
150 DVBSubRegion *ptr = ctx->region_list;
152 while (ptr && ptr->id != region_id) {
159 static void delete_region_display_list(DVBSubContext *ctx, DVBSubRegion *region)
161 DVBSubObject *object, *obj2, **obj2_ptr;
162 DVBSubObjectDisplay *display, *obj_disp, **obj_disp_ptr;
164 while (region->display_list) {
165 display = region->display_list;
167 object = get_object(ctx, display->object_id);
170 obj_disp_ptr = &object->display_list;
171 obj_disp = *obj_disp_ptr;
173 while (obj_disp && obj_disp != display) {
174 obj_disp_ptr = &obj_disp->object_list_next;
175 obj_disp = *obj_disp_ptr;
179 *obj_disp_ptr = obj_disp->object_list_next;
181 if (!object->display_list) {
182 obj2_ptr = &ctx->object_list;
185 while (obj2 != object) {
187 obj2_ptr = &obj2->next;
191 *obj2_ptr = obj2->next;
198 region->display_list = display->region_list_next;
205 static void delete_state(DVBSubContext *ctx)
207 DVBSubRegion *region;
210 while (ctx->region_list) {
211 region = ctx->region_list;
213 ctx->region_list = region->next;
215 delete_region_display_list(ctx, region);
216 av_free(region->pbuf);
220 while (ctx->clut_list) {
221 clut = ctx->clut_list;
223 ctx->clut_list = clut->next;
228 av_freep(&ctx->display_definition);
230 /* Should already be null */
231 if (ctx->object_list)
232 av_log(NULL, AV_LOG_ERROR, "Memory deallocation error!\n");
235 static av_cold int dvbsub_init_decoder(AVCodecContext *avctx)
237 int i, r, g, b, a = 0;
238 DVBSubContext *ctx = avctx->priv_data;
240 if (!avctx->extradata || avctx->extradata_size != 4) {
241 av_log(avctx, AV_LOG_WARNING, "Invalid extradata, subtitle streams may be combined!\n");
242 ctx->composition_id = -1;
243 ctx->ancillary_id = -1;
245 ctx->composition_id = AV_RB16(avctx->extradata);
246 ctx->ancillary_id = AV_RB16(avctx->extradata + 2);
249 default_clut.id = -1;
250 default_clut.next = NULL;
252 default_clut.clut4[0] = RGBA( 0, 0, 0, 0);
253 default_clut.clut4[1] = RGBA(255, 255, 255, 255);
254 default_clut.clut4[2] = RGBA( 0, 0, 0, 255);
255 default_clut.clut4[3] = RGBA(127, 127, 127, 255);
257 default_clut.clut16[0] = RGBA( 0, 0, 0, 0);
258 for (i = 1; i < 16; i++) {
260 r = (i & 1) ? 255 : 0;
261 g = (i & 2) ? 255 : 0;
262 b = (i & 4) ? 255 : 0;
264 r = (i & 1) ? 127 : 0;
265 g = (i & 2) ? 127 : 0;
266 b = (i & 4) ? 127 : 0;
268 default_clut.clut16[i] = RGBA(r, g, b, 255);
271 default_clut.clut256[0] = RGBA( 0, 0, 0, 0);
272 for (i = 1; i < 256; i++) {
274 r = (i & 1) ? 255 : 0;
275 g = (i & 2) ? 255 : 0;
276 b = (i & 4) ? 255 : 0;
281 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
282 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
283 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
287 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
288 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
289 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
293 r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
294 g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
295 b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
299 r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
300 g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
301 b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
306 default_clut.clut256[i] = RGBA(r, g, b, a);
312 static av_cold int dvbsub_close_decoder(AVCodecContext *avctx)
314 DVBSubContext *ctx = avctx->priv_data;
315 DVBSubRegionDisplay *display;
319 while (ctx->display_list) {
320 display = ctx->display_list;
321 ctx->display_list = display->next;
329 static int dvbsub_read_2bit_string(uint8_t *destbuf, int dbuf_len,
330 const uint8_t **srcbuf, int buf_size,
331 int non_mod, uint8_t *map_table)
339 init_get_bits(&gb, *srcbuf, buf_size << 3);
341 while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
342 bits = get_bits(&gb, 2);
345 if (non_mod != 1 || bits != 1) {
347 *destbuf++ = map_table[bits];
353 bits = get_bits1(&gb);
355 run_length = get_bits(&gb, 3) + 3;
356 bits = get_bits(&gb, 2);
358 if (non_mod == 1 && bits == 1)
359 pixels_read += run_length;
362 bits = map_table[bits];
363 while (run_length-- > 0 && pixels_read < dbuf_len) {
369 bits = get_bits1(&gb);
371 bits = get_bits(&gb, 2);
373 run_length = get_bits(&gb, 4) + 12;
374 bits = get_bits(&gb, 2);
376 if (non_mod == 1 && bits == 1)
377 pixels_read += run_length;
380 bits = map_table[bits];
381 while (run_length-- > 0 && pixels_read < dbuf_len) {
386 } else if (bits == 3) {
387 run_length = get_bits(&gb, 8) + 29;
388 bits = get_bits(&gb, 2);
390 if (non_mod == 1 && bits == 1)
391 pixels_read += run_length;
394 bits = map_table[bits];
395 while (run_length-- > 0 && pixels_read < dbuf_len) {
400 } else if (bits == 1) {
406 if (pixels_read <= dbuf_len) {
411 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
426 if (get_bits(&gb, 6))
427 av_log(NULL, AV_LOG_ERROR, "DVBSub error: line overflow\n");
429 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
434 static int dvbsub_read_4bit_string(uint8_t *destbuf, int dbuf_len,
435 const uint8_t **srcbuf, int buf_size,
436 int non_mod, uint8_t *map_table)
444 init_get_bits(&gb, *srcbuf, buf_size << 3);
446 while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
447 bits = get_bits(&gb, 4);
450 if (non_mod != 1 || bits != 1) {
452 *destbuf++ = map_table[bits];
458 bits = get_bits1(&gb);
460 run_length = get_bits(&gb, 3);
462 if (run_length == 0) {
463 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
474 while (run_length-- > 0 && pixels_read < dbuf_len) {
479 bits = get_bits1(&gb);
481 run_length = get_bits(&gb, 2) + 4;
482 bits = get_bits(&gb, 4);
484 if (non_mod == 1 && bits == 1)
485 pixels_read += run_length;
488 bits = map_table[bits];
489 while (run_length-- > 0 && pixels_read < dbuf_len) {
495 bits = get_bits(&gb, 2);
497 run_length = get_bits(&gb, 4) + 9;
498 bits = get_bits(&gb, 4);
500 if (non_mod == 1 && bits == 1)
501 pixels_read += run_length;
504 bits = map_table[bits];
505 while (run_length-- > 0 && pixels_read < dbuf_len) {
510 } else if (bits == 3) {
511 run_length = get_bits(&gb, 8) + 25;
512 bits = get_bits(&gb, 4);
514 if (non_mod == 1 && bits == 1)
515 pixels_read += run_length;
518 bits = map_table[bits];
519 while (run_length-- > 0 && pixels_read < dbuf_len) {
524 } else if (bits == 1) {
530 if (pixels_read <= dbuf_len) {
547 if (get_bits(&gb, 8))
548 av_log(NULL, AV_LOG_ERROR, "DVBSub error: line overflow\n");
550 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
555 static int dvbsub_read_8bit_string(uint8_t *destbuf, int dbuf_len,
556 const uint8_t **srcbuf, int buf_size,
557 int non_mod, uint8_t *map_table)
559 const uint8_t *sbuf_end = (*srcbuf) + buf_size;
564 while (*srcbuf < sbuf_end && pixels_read < dbuf_len) {
568 if (non_mod != 1 || bits != 1) {
570 *destbuf++ = map_table[bits];
577 run_length = bits & 0x7f;
578 if ((bits & 0x80) == 0) {
579 if (run_length == 0) {
585 if (non_mod == 1 && bits == 1)
586 pixels_read += run_length;
592 while (run_length-- > 0 && pixels_read < dbuf_len) {
600 av_log(NULL, AV_LOG_ERROR, "DVBSub error: line overflow\n");
607 static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
608 const uint8_t *buf, int buf_size, int top_bottom, int non_mod)
610 DVBSubContext *ctx = avctx->priv_data;
612 DVBSubRegion *region = get_region(ctx, display->region_id);
613 const uint8_t *buf_end = buf + buf_size;
618 uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
619 uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
620 uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
621 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
624 ff_dlog(avctx, "DVB pixel block size %d, %s field:\n", buf_size,
625 top_bottom ? "bottom" : "top");
627 for (i = 0; i < buf_size; i++) {
629 ff_dlog(avctx, "0x%8p: ", buf+i);
631 ff_dlog(avctx, "%02x ", buf[i]);
633 ff_dlog(avctx, "\n");
637 ff_dlog(avctx, "\n");
644 x_pos = display->x_pos;
645 y_pos = display->y_pos;
647 if ((y_pos & 1) != top_bottom)
650 while (buf < buf_end) {
651 if (x_pos > region->width || y_pos > region->height) {
652 av_log(avctx, AV_LOG_ERROR, "Invalid object location!\n");
658 if (region->depth == 8)
660 else if (region->depth == 4)
665 x_pos += dvbsub_read_2bit_string(pbuf + (y_pos * region->width) + x_pos,
666 region->width - x_pos, &buf, buf_end - buf,
670 if (region->depth < 4) {
671 av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
675 if (region->depth == 8)
680 x_pos += dvbsub_read_4bit_string(pbuf + (y_pos * region->width) + x_pos,
681 region->width - x_pos, &buf, buf_end - buf,
685 if (region->depth < 8) {
686 av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
690 x_pos += dvbsub_read_8bit_string(pbuf + (y_pos * region->width) + x_pos,
691 region->width - x_pos, &buf, buf_end - buf,
696 map2to4[0] = (*buf) >> 4;
697 map2to4[1] = (*buf++) & 0xf;
698 map2to4[2] = (*buf) >> 4;
699 map2to4[3] = (*buf++) & 0xf;
702 for (i = 0; i < 4; i++)
706 for (i = 0; i < 16; i++)
711 x_pos = display->x_pos;
715 av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
721 static int dvbsub_parse_object_segment(AVCodecContext *avctx,
722 const uint8_t *buf, int buf_size)
724 DVBSubContext *ctx = avctx->priv_data;
726 const uint8_t *buf_end = buf + buf_size;
727 const uint8_t *block;
729 DVBSubObject *object;
730 DVBSubObjectDisplay *display;
731 int top_field_len, bottom_field_len;
733 int coding_method, non_modifying_color;
735 object_id = AV_RB16(buf);
738 object = get_object(ctx, object_id);
741 return AVERROR_INVALIDDATA;
743 coding_method = ((*buf) >> 2) & 3;
744 non_modifying_color = ((*buf++) >> 1) & 1;
746 if (coding_method == 0) {
747 top_field_len = AV_RB16(buf);
749 bottom_field_len = AV_RB16(buf);
752 if (buf + top_field_len + bottom_field_len > buf_end) {
753 av_log(avctx, AV_LOG_ERROR, "Field data size too large\n");
754 return AVERROR_INVALIDDATA;
757 for (display = object->display_list; display; display = display->object_list_next) {
760 dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
761 non_modifying_color);
763 if (bottom_field_len > 0)
764 block = buf + top_field_len;
766 bottom_field_len = top_field_len;
768 dvbsub_parse_pixel_data_block(avctx, display, block, bottom_field_len, 1,
769 non_modifying_color);
772 /* } else if (coding_method == 1) {*/
775 av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
781 static int dvbsub_parse_clut_segment(AVCodecContext *avctx,
782 const uint8_t *buf, int buf_size)
784 DVBSubContext *ctx = avctx->priv_data;
786 const uint8_t *buf_end = buf + buf_size;
789 int entry_id, depth , full_range;
790 int y, cr, cb, alpha;
791 int r, g, b, r_add, g_add, b_add;
793 ff_dlog(avctx, "DVB clut packet:\n");
795 for (i=0; i < buf_size; i++) {
796 ff_dlog(avctx, "%02x ", buf[i]);
798 ff_dlog(avctx, "\n");
802 ff_dlog(avctx, "\n");
807 clut = get_clut(ctx, clut_id);
810 clut = av_malloc(sizeof(DVBSubCLUT));
812 return AVERROR(ENOMEM);
814 memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
818 clut->next = ctx->clut_list;
819 ctx->clut_list = clut;
822 while (buf + 4 < buf_end) {
825 depth = (*buf) & 0xe0;
828 av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
829 return AVERROR_INVALIDDATA;
832 full_range = (*buf++) & 1;
841 cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
842 cb = (buf[1] << 2) & 0xf0;
843 alpha = (buf[1] << 6) & 0xc0;
851 YUV_TO_RGB1_CCIR(cb, cr);
852 YUV_TO_RGB2_CCIR(r, g, b, y);
854 ff_dlog(avctx, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
857 clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
859 clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
861 clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
868 static int dvbsub_parse_region_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 int region_id, object_id;
875 DVBSubRegion *region;
876 DVBSubObject *object;
877 DVBSubObjectDisplay *display;
881 return AVERROR_INVALIDDATA;
885 region = get_region(ctx, region_id);
888 region = av_mallocz(sizeof(DVBSubRegion));
890 return AVERROR(ENOMEM);
892 region->id = region_id;
894 region->next = ctx->region_list;
895 ctx->region_list = region;
898 fill = ((*buf++) >> 3) & 1;
900 region->width = AV_RB16(buf);
902 region->height = AV_RB16(buf);
905 if (region->width * region->height != region->buf_size) {
906 av_free(region->pbuf);
908 region->buf_size = region->width * region->height;
910 region->pbuf = av_malloc(region->buf_size);
912 return AVERROR(ENOMEM);
917 region->depth = 1 << (((*buf++) >> 2) & 7);
918 if(region->depth<2 || region->depth>8){
919 av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);
922 region->clut = *buf++;
924 if (region->depth == 8)
925 region->bgcolor = *buf++;
929 if (region->depth == 4)
930 region->bgcolor = (((*buf++) >> 4) & 15);
932 region->bgcolor = (((*buf++) >> 2) & 3);
935 ff_dlog(avctx, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
938 memset(region->pbuf, region->bgcolor, region->buf_size);
939 ff_dlog(avctx, "Fill region (%d)\n", region->bgcolor);
942 delete_region_display_list(ctx, region);
944 while (buf + 5 < buf_end) {
945 object_id = AV_RB16(buf);
948 object = get_object(ctx, object_id);
951 object = av_mallocz(sizeof(DVBSubObject));
953 return AVERROR(ENOMEM);
955 object->id = object_id;
956 object->next = ctx->object_list;
957 ctx->object_list = object;
960 object->type = (*buf) >> 6;
962 display = av_mallocz(sizeof(DVBSubObjectDisplay));
964 return AVERROR(ENOMEM);
966 display->object_id = object_id;
967 display->region_id = region_id;
969 display->x_pos = AV_RB16(buf) & 0xfff;
971 display->y_pos = AV_RB16(buf) & 0xfff;
974 if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
975 display->fgcolor = *buf++;
976 display->bgcolor = *buf++;
979 display->region_list_next = region->display_list;
980 region->display_list = display;
982 display->object_list_next = object->display_list;
983 object->display_list = display;
989 static int dvbsub_parse_page_segment(AVCodecContext *avctx,
990 const uint8_t *buf, int buf_size)
992 DVBSubContext *ctx = avctx->priv_data;
993 DVBSubRegionDisplay *display;
994 DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
996 const uint8_t *buf_end = buf + buf_size;
1001 return AVERROR_INVALIDDATA;
1003 ctx->time_out = *buf++;
1004 page_state = ((*buf++) >> 2) & 3;
1006 ff_dlog(avctx, "Page time out %ds, state %d\n", ctx->time_out, page_state);
1008 if (page_state == 2) {
1012 tmp_display_list = ctx->display_list;
1013 ctx->display_list = NULL;
1014 ctx->display_list_size = 0;
1016 while (buf + 5 < buf_end) {
1020 display = tmp_display_list;
1021 tmp_ptr = &tmp_display_list;
1023 while (display && display->region_id != region_id) {
1024 tmp_ptr = &display->next;
1025 display = display->next;
1029 display = av_mallocz(sizeof(DVBSubRegionDisplay));
1031 return AVERROR(ENOMEM);
1034 display->region_id = region_id;
1036 display->x_pos = AV_RB16(buf);
1038 display->y_pos = AV_RB16(buf);
1041 *tmp_ptr = display->next;
1043 display->next = ctx->display_list;
1044 ctx->display_list = display;
1045 ctx->display_list_size++;
1047 ff_dlog(avctx, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
1050 while (tmp_display_list) {
1051 display = tmp_display_list;
1053 tmp_display_list = display->next;
1063 static void png_save(const char *filename, uint32_t *bitmap, int w, int h)
1067 char fname[40], fname2[40];
1070 snprintf(fname, sizeof(fname), "%s.ppm", filename);
1072 f = fopen(fname, "w");
1081 for(y = 0; y < h; y++) {
1082 for(x = 0; x < w; x++) {
1083 v = bitmap[y * w + x];
1084 putc((v >> 16) & 0xff, f);
1085 putc((v >> 8) & 0xff, f);
1086 putc((v >> 0) & 0xff, f);
1092 snprintf(fname2, sizeof(fname2), "%s-a.pgm", filename);
1094 f = fopen(fname2, "w");
1103 for(y = 0; y < h; y++) {
1104 for(x = 0; x < w; x++) {
1105 v = bitmap[y * w + x];
1106 putc((v >> 24) & 0xff, f);
1111 snprintf(command, sizeof(command), "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
1112 if (system(command) != 0) {
1113 printf("Error running pnmtopng\n");
1117 snprintf(command, sizeof(command), "rm %s %s", fname, fname2);
1118 if (system(command) != 0) {
1119 printf("Error removing %s and %s\n", fname, fname2);
1124 static int save_display_set(DVBSubContext *ctx)
1126 DVBSubRegion *region;
1127 DVBSubRegionDisplay *display;
1129 uint32_t *clut_table;
1130 int x_pos, y_pos, width, height;
1131 int x, y, y_off, x_off;
1134 static int fileno_index = 0;
1141 for (display = ctx->display_list; display; display = display->next) {
1142 region = get_region(ctx, display->region_id);
1145 x_pos = display->x_pos;
1146 y_pos = display->y_pos;
1147 width = region->width;
1148 height = region->height;
1150 if (display->x_pos < x_pos) {
1151 width += (x_pos - display->x_pos);
1152 x_pos = display->x_pos;
1155 if (display->y_pos < y_pos) {
1156 height += (y_pos - display->y_pos);
1157 y_pos = display->y_pos;
1160 if (display->x_pos + region->width > x_pos + width) {
1161 width = display->x_pos + region->width - x_pos;
1164 if (display->y_pos + region->height > y_pos + height) {
1165 height = display->y_pos + region->height - y_pos;
1172 pbuf = av_malloc(width * height * 4);
1174 return AVERROR(ENOMEM);
1176 for (display = ctx->display_list; display; display = display->next) {
1177 region = get_region(ctx, display->region_id);
1179 x_off = display->x_pos - x_pos;
1180 y_off = display->y_pos - y_pos;
1182 clut = get_clut(ctx, region->clut);
1185 clut = &default_clut;
1187 switch (region->depth) {
1189 clut_table = clut->clut4;
1192 clut_table = clut->clut256;
1196 clut_table = clut->clut16;
1200 for (y = 0; y < region->height; y++) {
1201 for (x = 0; x < region->width; x++) {
1202 pbuf[((y + y_off) * width) + x_off + x] =
1203 clut_table[region->pbuf[y * region->width + x]];
1209 snprintf(filename, sizeof(filename), "dvbs.%d", fileno_index);
1211 png_save(filename, pbuf, width, height);
1221 static int dvbsub_parse_display_definition_segment(AVCodecContext *avctx,
1225 DVBSubContext *ctx = avctx->priv_data;
1226 DVBSubDisplayDefinition *display_def = ctx->display_definition;
1227 int dds_version, info_byte;
1230 return AVERROR_INVALIDDATA;
1232 info_byte = bytestream_get_byte(&buf);
1233 dds_version = info_byte >> 4;
1234 if (display_def && display_def->version == dds_version)
1235 return 0; // already have this display definition version
1238 display_def = av_mallocz(sizeof(*display_def));
1240 return AVERROR(ENOMEM);
1241 ctx->display_definition = display_def;
1244 display_def->version = dds_version;
1247 display_def->width = bytestream_get_be16(&buf) + 1;
1248 display_def->height = bytestream_get_be16(&buf) + 1;
1251 return AVERROR_INVALIDDATA;
1253 if (info_byte & 1<<3) { // display_window_flag
1254 display_def->x = bytestream_get_be16(&buf);
1255 display_def->y = bytestream_get_be16(&buf);
1256 display_def->width = bytestream_get_be16(&buf) - display_def->x + 1;
1257 display_def->height = bytestream_get_be16(&buf) - display_def->y + 1;
1263 static int dvbsub_display_end_segment(AVCodecContext *avctx, const uint8_t *buf,
1264 int buf_size, AVSubtitle *sub)
1266 DVBSubContext *ctx = avctx->priv_data;
1267 DVBSubDisplayDefinition *display_def = ctx->display_definition;
1269 DVBSubRegion *region;
1270 DVBSubRegionDisplay *display;
1271 AVSubtitleRect *rect;
1273 uint32_t *clut_table;
1275 int offset_x=0, offset_y=0;
1278 sub->start_display_time = 0;
1279 sub->end_display_time = ctx->time_out * 1000;
1283 offset_x = display_def->x;
1284 offset_y = display_def->y;
1287 sub->num_rects = ctx->display_list_size;
1288 if (sub->num_rects <= 0)
1289 return AVERROR_INVALIDDATA;
1291 sub->rects = av_mallocz_array(sub->num_rects * sub->num_rects,
1292 sizeof(*sub->rects));
1294 return AVERROR(ENOMEM);
1298 for (display = ctx->display_list; display; display = display->next) {
1299 region = get_region(ctx, display->region_id);
1300 rect = sub->rects[i];
1305 rect->x = display->x_pos + offset_x;
1306 rect->y = display->y_pos + offset_y;
1307 rect->w = region->width;
1308 rect->h = region->height;
1309 rect->nb_colors = 16;
1310 rect->type = SUBTITLE_BITMAP;
1311 rect->linesize[0] = region->width;
1313 clut = get_clut(ctx, region->clut);
1316 clut = &default_clut;
1318 switch (region->depth) {
1320 clut_table = clut->clut4;
1323 clut_table = clut->clut256;
1327 clut_table = clut->clut16;
1331 rect->data[1] = av_mallocz(AVPALETTE_SIZE);
1332 if (!rect->data[1]) {
1333 av_free(sub->rects);
1334 return AVERROR(ENOMEM);
1336 memcpy(rect->data[1], clut_table, (1 << region->depth) * sizeof(uint32_t));
1338 rect->data[0] = av_malloc(region->buf_size);
1339 if (!rect->data[0]) {
1340 av_free(rect->data[1]);
1341 av_free(sub->rects);
1342 return AVERROR(ENOMEM);
1344 memcpy(rect->data[0], region->pbuf, region->buf_size);
1346 #if FF_API_AVPICTURE
1347 FF_DISABLE_DEPRECATION_WARNINGS
1350 for (j = 0; j < 4; j++) {
1351 rect->pict.data[j] = rect->data[j];
1352 rect->pict.linesize[j] = rect->linesize[j];
1355 FF_ENABLE_DEPRECATION_WARNINGS
1364 save_display_set(ctx);
1370 static int dvbsub_decode(AVCodecContext *avctx,
1371 void *data, int *data_size,
1374 const uint8_t *buf = avpkt->data;
1375 int buf_size = avpkt->size;
1376 DVBSubContext *ctx = avctx->priv_data;
1377 AVSubtitle *sub = data;
1378 const uint8_t *p, *p_end;
1384 ff_dlog(avctx, "DVB sub packet:\n");
1386 for (i=0; i < buf_size; i++) {
1387 ff_dlog(avctx, "%02x ", buf[i]);
1389 ff_dlog(avctx, "\n");
1393 ff_dlog(avctx, "\n");
1395 if (buf_size <= 6 || *buf != 0x0f) {
1396 ff_dlog(avctx, "incomplete or broken packet");
1397 return AVERROR_INVALIDDATA;
1401 p_end = buf + buf_size;
1403 while (p_end - p >= 6 && *p == 0x0f) {
1405 segment_type = *p++;
1406 page_id = AV_RB16(p);
1408 segment_length = AV_RB16(p);
1411 if (p_end - p < segment_length) {
1412 ff_dlog(avctx, "incomplete or broken packet");
1416 if (page_id == ctx->composition_id || page_id == ctx->ancillary_id ||
1417 ctx->composition_id == -1 || ctx->ancillary_id == -1) {
1419 switch (segment_type) {
1420 case DVBSUB_PAGE_SEGMENT:
1421 ret = dvbsub_parse_page_segment(avctx, p, segment_length);
1423 case DVBSUB_REGION_SEGMENT:
1424 ret = dvbsub_parse_region_segment(avctx, p, segment_length);
1426 case DVBSUB_CLUT_SEGMENT:
1427 ret = dvbsub_parse_clut_segment(avctx, p, segment_length);
1429 case DVBSUB_OBJECT_SEGMENT:
1430 ret = dvbsub_parse_object_segment(avctx, p, segment_length);
1432 case DVBSUB_DISPLAYDEFINITION_SEGMENT:
1433 ret = dvbsub_parse_display_definition_segment(avctx, p,
1436 case DVBSUB_DISPLAY_SEGMENT:
1437 ret = dvbsub_display_end_segment(avctx, p, segment_length, sub);
1441 ff_dlog(avctx, "Subtitling segment type 0x%x, page id %d, length %d\n",
1442 segment_type, page_id, segment_length);
1449 p += segment_length;
1456 AVCodec ff_dvbsub_decoder = {
1458 .long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),
1459 .type = AVMEDIA_TYPE_SUBTITLE,
1460 .id = AV_CODEC_ID_DVB_SUBTITLE,
1461 .priv_data_size = sizeof(DVBSubContext),
1462 .init = dvbsub_init_decoder,
1463 .close = dvbsub_close_decoder,
1464 .decode = dvbsub_decode,