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)
38 static void png_save(const char *filename, uint32_t *bitmap, int w, int h)
42 char fname[40], fname2[40];
45 snprintf(fname, sizeof(fname), "%s.ppm", filename);
47 f = fopen(fname, "w");
56 for(y = 0; y < h; y++) {
57 for(x = 0; x < w; x++) {
58 v = bitmap[y * w + x];
59 putc((v >> 16) & 0xff, f);
60 putc((v >> 8) & 0xff, f);
61 putc((v >> 0) & 0xff, f);
67 snprintf(fname2, sizeof(fname2), "%s-a.pgm", filename);
69 f = fopen(fname2, "w");
78 for(y = 0; y < h; y++) {
79 for(x = 0; x < w; x++) {
80 v = bitmap[y * w + x];
81 putc((v >> 24) & 0xff, f);
86 snprintf(command, sizeof(command), "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
89 snprintf(command, sizeof(command), "rm %s %s", fname, fname2);
94 #define RGBA(r,g,b,a) (((unsigned)(a) << 24) | ((r) << 16) | ((g) << 8) | (b))
96 typedef struct DVBSubCLUT {
101 uint32_t clut256[256];
103 struct DVBSubCLUT *next;
106 static DVBSubCLUT default_clut;
108 typedef struct DVBSubObjectDisplay {
118 struct DVBSubObjectDisplay *region_list_next;
119 struct DVBSubObjectDisplay *object_list_next;
120 } DVBSubObjectDisplay;
122 typedef struct DVBSubObject {
127 DVBSubObjectDisplay *display_list;
129 struct DVBSubObject *next;
132 typedef struct DVBSubRegionDisplay {
138 struct DVBSubRegionDisplay *next;
139 } DVBSubRegionDisplay;
141 typedef struct DVBSubRegion {
154 DVBSubObjectDisplay *display_list;
156 struct DVBSubRegion *next;
159 typedef struct DVBSubDisplayDefinition {
166 } DVBSubDisplayDefinition;
168 typedef struct DVBSubContext {
173 DVBSubRegion *region_list;
174 DVBSubCLUT *clut_list;
175 DVBSubObject *object_list;
177 int display_list_size;
178 DVBSubRegionDisplay *display_list;
179 DVBSubDisplayDefinition *display_definition;
183 static DVBSubObject* get_object(DVBSubContext *ctx, int object_id)
185 DVBSubObject *ptr = ctx->object_list;
187 while (ptr && ptr->id != object_id) {
194 static DVBSubCLUT* get_clut(DVBSubContext *ctx, int clut_id)
196 DVBSubCLUT *ptr = ctx->clut_list;
198 while (ptr && ptr->id != clut_id) {
205 static DVBSubRegion* get_region(DVBSubContext *ctx, int region_id)
207 DVBSubRegion *ptr = ctx->region_list;
209 while (ptr && ptr->id != region_id) {
216 static void delete_region_display_list(DVBSubContext *ctx, DVBSubRegion *region)
218 DVBSubObject *object, *obj2, **obj2_ptr;
219 DVBSubObjectDisplay *display, *obj_disp, **obj_disp_ptr;
221 while (region->display_list) {
222 display = region->display_list;
224 object = get_object(ctx, display->object_id);
227 obj_disp_ptr = &object->display_list;
228 obj_disp = *obj_disp_ptr;
230 while (obj_disp && obj_disp != display) {
231 obj_disp_ptr = &obj_disp->object_list_next;
232 obj_disp = *obj_disp_ptr;
236 *obj_disp_ptr = obj_disp->object_list_next;
238 if (!object->display_list) {
239 obj2_ptr = &ctx->object_list;
242 while (obj2 != object) {
244 obj2_ptr = &obj2->next;
248 *obj2_ptr = obj2->next;
255 region->display_list = display->region_list_next;
262 static void delete_state(DVBSubContext *ctx)
264 DVBSubRegion *region;
267 while (ctx->region_list) {
268 region = ctx->region_list;
270 ctx->region_list = region->next;
272 delete_region_display_list(ctx, region);
273 av_free(region->pbuf);
277 while (ctx->clut_list) {
278 clut = ctx->clut_list;
280 ctx->clut_list = clut->next;
285 av_freep(&ctx->display_definition);
287 /* Should already be null */
288 if (ctx->object_list)
289 av_log(NULL, AV_LOG_ERROR, "Memory deallocation error!\n");
292 static av_cold int dvbsub_init_decoder(AVCodecContext *avctx)
294 int i, r, g, b, a = 0;
295 DVBSubContext *ctx = avctx->priv_data;
297 if (!avctx->extradata || avctx->extradata_size != 4) {
298 av_log(avctx, AV_LOG_WARNING, "Invalid extradata, subtitle streams may be combined!\n");
299 ctx->composition_id = -1;
300 ctx->ancillary_id = -1;
302 ctx->composition_id = AV_RB16(avctx->extradata);
303 ctx->ancillary_id = AV_RB16(avctx->extradata + 2);
306 default_clut.id = -1;
307 default_clut.next = NULL;
309 default_clut.clut4[0] = RGBA( 0, 0, 0, 0);
310 default_clut.clut4[1] = RGBA(255, 255, 255, 255);
311 default_clut.clut4[2] = RGBA( 0, 0, 0, 255);
312 default_clut.clut4[3] = RGBA(127, 127, 127, 255);
314 default_clut.clut16[0] = RGBA( 0, 0, 0, 0);
315 for (i = 1; i < 16; i++) {
317 r = (i & 1) ? 255 : 0;
318 g = (i & 2) ? 255 : 0;
319 b = (i & 4) ? 255 : 0;
321 r = (i & 1) ? 127 : 0;
322 g = (i & 2) ? 127 : 0;
323 b = (i & 4) ? 127 : 0;
325 default_clut.clut16[i] = RGBA(r, g, b, 255);
328 default_clut.clut256[0] = RGBA( 0, 0, 0, 0);
329 for (i = 1; i < 256; i++) {
331 r = (i & 1) ? 255 : 0;
332 g = (i & 2) ? 255 : 0;
333 b = (i & 4) ? 255 : 0;
338 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
339 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
340 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
344 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
345 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
346 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
350 r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
351 g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
352 b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
356 r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
357 g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
358 b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
363 default_clut.clut256[i] = RGBA(r, g, b, a);
369 static av_cold int dvbsub_close_decoder(AVCodecContext *avctx)
371 DVBSubContext *ctx = avctx->priv_data;
372 DVBSubRegionDisplay *display;
376 while (ctx->display_list) {
377 display = ctx->display_list;
378 ctx->display_list = display->next;
386 static int dvbsub_read_2bit_string(uint8_t *destbuf, int dbuf_len,
387 const uint8_t **srcbuf, int buf_size,
388 int non_mod, uint8_t *map_table)
396 init_get_bits(&gb, *srcbuf, buf_size << 3);
398 while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
399 bits = get_bits(&gb, 2);
402 if (non_mod != 1 || bits != 1) {
404 *destbuf++ = map_table[bits];
410 bits = get_bits1(&gb);
412 run_length = get_bits(&gb, 3) + 3;
413 bits = get_bits(&gb, 2);
415 if (non_mod == 1 && bits == 1)
416 pixels_read += run_length;
419 bits = map_table[bits];
420 while (run_length-- > 0 && pixels_read < dbuf_len) {
426 bits = get_bits1(&gb);
428 bits = get_bits(&gb, 2);
430 run_length = get_bits(&gb, 4) + 12;
431 bits = get_bits(&gb, 2);
433 if (non_mod == 1 && bits == 1)
434 pixels_read += run_length;
437 bits = map_table[bits];
438 while (run_length-- > 0 && pixels_read < dbuf_len) {
443 } else if (bits == 3) {
444 run_length = get_bits(&gb, 8) + 29;
445 bits = get_bits(&gb, 2);
447 if (non_mod == 1 && bits == 1)
448 pixels_read += run_length;
451 bits = map_table[bits];
452 while (run_length-- > 0 && pixels_read < dbuf_len) {
457 } else if (bits == 1) {
463 if (pixels_read <= dbuf_len) {
468 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
483 if (get_bits(&gb, 6))
484 av_log(NULL, AV_LOG_ERROR, "DVBSub error: line overflow\n");
486 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
491 static int dvbsub_read_4bit_string(uint8_t *destbuf, int dbuf_len,
492 const uint8_t **srcbuf, int buf_size,
493 int non_mod, uint8_t *map_table)
501 init_get_bits(&gb, *srcbuf, buf_size << 3);
503 while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
504 bits = get_bits(&gb, 4);
507 if (non_mod != 1 || bits != 1) {
509 *destbuf++ = map_table[bits];
515 bits = get_bits1(&gb);
517 run_length = get_bits(&gb, 3);
519 if (run_length == 0) {
520 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
531 while (run_length-- > 0 && pixels_read < dbuf_len) {
536 bits = get_bits1(&gb);
538 run_length = get_bits(&gb, 2) + 4;
539 bits = get_bits(&gb, 4);
541 if (non_mod == 1 && bits == 1)
542 pixels_read += run_length;
545 bits = map_table[bits];
546 while (run_length-- > 0 && pixels_read < dbuf_len) {
552 bits = get_bits(&gb, 2);
554 run_length = get_bits(&gb, 4) + 9;
555 bits = get_bits(&gb, 4);
557 if (non_mod == 1 && bits == 1)
558 pixels_read += run_length;
561 bits = map_table[bits];
562 while (run_length-- > 0 && pixels_read < dbuf_len) {
567 } else if (bits == 3) {
568 run_length = get_bits(&gb, 8) + 25;
569 bits = get_bits(&gb, 4);
571 if (non_mod == 1 && bits == 1)
572 pixels_read += run_length;
575 bits = map_table[bits];
576 while (run_length-- > 0 && pixels_read < dbuf_len) {
581 } else if (bits == 1) {
587 if (pixels_read <= dbuf_len) {
604 if (get_bits(&gb, 8))
605 av_log(NULL, AV_LOG_ERROR, "DVBSub error: line overflow\n");
607 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
612 static int dvbsub_read_8bit_string(uint8_t *destbuf, int dbuf_len,
613 const uint8_t **srcbuf, int buf_size,
614 int non_mod, uint8_t *map_table)
616 const uint8_t *sbuf_end = (*srcbuf) + buf_size;
621 while (*srcbuf < sbuf_end && pixels_read < dbuf_len) {
625 if (non_mod != 1 || bits != 1) {
627 *destbuf++ = map_table[bits];
634 run_length = bits & 0x7f;
635 if ((bits & 0x80) == 0) {
636 if (run_length == 0) {
642 if (non_mod == 1 && bits == 1)
643 pixels_read += run_length;
649 while (run_length-- > 0 && pixels_read < dbuf_len) {
657 av_log(NULL, AV_LOG_ERROR, "DVBSub error: line overflow\n");
664 static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
665 const uint8_t *buf, int buf_size, int top_bottom, int non_mod)
667 DVBSubContext *ctx = avctx->priv_data;
669 DVBSubRegion *region = get_region(ctx, display->region_id);
670 const uint8_t *buf_end = buf + buf_size;
675 uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
676 uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
677 uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
678 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
681 ff_dlog(avctx, "DVB pixel block size %d, %s field:\n", buf_size,
682 top_bottom ? "bottom" : "top");
684 for (i = 0; i < buf_size; i++) {
686 ff_dlog(avctx, "0x%8p: ", buf+i);
688 ff_dlog(avctx, "%02x ", buf[i]);
690 ff_dlog(avctx, "\n");
694 ff_dlog(avctx, "\n");
701 x_pos = display->x_pos;
702 y_pos = display->y_pos;
704 if ((y_pos & 1) != top_bottom)
707 while (buf < buf_end) {
708 if (x_pos > region->width || y_pos > region->height) {
709 av_log(avctx, AV_LOG_ERROR, "Invalid object location!\n");
715 if (region->depth == 8)
717 else if (region->depth == 4)
722 x_pos += dvbsub_read_2bit_string(pbuf + (y_pos * region->width) + x_pos,
723 region->width - x_pos, &buf, buf_end - buf,
727 if (region->depth < 4) {
728 av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
732 if (region->depth == 8)
737 x_pos += dvbsub_read_4bit_string(pbuf + (y_pos * region->width) + x_pos,
738 region->width - x_pos, &buf, buf_end - buf,
742 if (region->depth < 8) {
743 av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
747 x_pos += dvbsub_read_8bit_string(pbuf + (y_pos * region->width) + x_pos,
748 region->width - x_pos, &buf, buf_end - buf,
753 map2to4[0] = (*buf) >> 4;
754 map2to4[1] = (*buf++) & 0xf;
755 map2to4[2] = (*buf) >> 4;
756 map2to4[3] = (*buf++) & 0xf;
759 for (i = 0; i < 4; i++)
763 for (i = 0; i < 16; i++)
768 x_pos = display->x_pos;
772 av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
778 static int dvbsub_parse_object_segment(AVCodecContext *avctx,
779 const uint8_t *buf, int buf_size)
781 DVBSubContext *ctx = avctx->priv_data;
783 const uint8_t *buf_end = buf + buf_size;
784 const uint8_t *block;
786 DVBSubObject *object;
787 DVBSubObjectDisplay *display;
788 int top_field_len, bottom_field_len;
790 int coding_method, non_modifying_color;
792 object_id = AV_RB16(buf);
795 object = get_object(ctx, object_id);
798 return AVERROR_INVALIDDATA;
800 coding_method = ((*buf) >> 2) & 3;
801 non_modifying_color = ((*buf++) >> 1) & 1;
803 if (coding_method == 0) {
804 top_field_len = AV_RB16(buf);
806 bottom_field_len = AV_RB16(buf);
809 if (buf + top_field_len + bottom_field_len > buf_end) {
810 av_log(avctx, AV_LOG_ERROR, "Field data size too large\n");
811 return AVERROR_INVALIDDATA;
814 for (display = object->display_list; display; display = display->object_list_next) {
817 dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
818 non_modifying_color);
820 if (bottom_field_len > 0)
821 block = buf + top_field_len;
823 bottom_field_len = top_field_len;
825 dvbsub_parse_pixel_data_block(avctx, display, block, bottom_field_len, 1,
826 non_modifying_color);
829 /* } else if (coding_method == 1) {*/
832 av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
838 static int dvbsub_parse_clut_segment(AVCodecContext *avctx,
839 const uint8_t *buf, int buf_size)
841 DVBSubContext *ctx = avctx->priv_data;
843 const uint8_t *buf_end = buf + buf_size;
846 int entry_id, depth , full_range;
847 int y, cr, cb, alpha;
848 int r, g, b, r_add, g_add, b_add;
850 ff_dlog(avctx, "DVB clut packet:\n");
852 for (i=0; i < buf_size; i++) {
853 ff_dlog(avctx, "%02x ", buf[i]);
855 ff_dlog(avctx, "\n");
859 ff_dlog(avctx, "\n");
864 clut = get_clut(ctx, clut_id);
867 clut = av_malloc(sizeof(DVBSubCLUT));
869 return AVERROR(ENOMEM);
871 memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
875 clut->next = ctx->clut_list;
876 ctx->clut_list = clut;
879 while (buf + 4 < buf_end) {
882 depth = (*buf) & 0xe0;
885 av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
886 return AVERROR_INVALIDDATA;
889 full_range = (*buf++) & 1;
898 cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
899 cb = (buf[1] << 2) & 0xf0;
900 alpha = (buf[1] << 6) & 0xc0;
908 YUV_TO_RGB1_CCIR(cb, cr);
909 YUV_TO_RGB2_CCIR(r, g, b, y);
911 ff_dlog(avctx, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
914 clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
916 clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
918 clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
925 static int dvbsub_parse_region_segment(AVCodecContext *avctx,
926 const uint8_t *buf, int buf_size)
928 DVBSubContext *ctx = avctx->priv_data;
930 const uint8_t *buf_end = buf + buf_size;
931 int region_id, object_id;
932 DVBSubRegion *region;
933 DVBSubObject *object;
934 DVBSubObjectDisplay *display;
938 return AVERROR_INVALIDDATA;
942 region = get_region(ctx, region_id);
945 region = av_mallocz(sizeof(DVBSubRegion));
947 return AVERROR(ENOMEM);
949 region->id = region_id;
951 region->next = ctx->region_list;
952 ctx->region_list = region;
955 fill = ((*buf++) >> 3) & 1;
957 region->width = AV_RB16(buf);
959 region->height = AV_RB16(buf);
962 if (region->width * region->height != region->buf_size) {
963 av_free(region->pbuf);
965 region->buf_size = region->width * region->height;
967 region->pbuf = av_malloc(region->buf_size);
969 return AVERROR(ENOMEM);
974 region->depth = 1 << (((*buf++) >> 2) & 7);
975 if(region->depth<2 || region->depth>8){
976 av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);
979 region->clut = *buf++;
981 if (region->depth == 8)
982 region->bgcolor = *buf++;
986 if (region->depth == 4)
987 region->bgcolor = (((*buf++) >> 4) & 15);
989 region->bgcolor = (((*buf++) >> 2) & 3);
992 ff_dlog(avctx, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
995 memset(region->pbuf, region->bgcolor, region->buf_size);
996 ff_dlog(avctx, "Fill region (%d)\n", region->bgcolor);
999 delete_region_display_list(ctx, region);
1001 while (buf + 5 < buf_end) {
1002 object_id = AV_RB16(buf);
1005 object = get_object(ctx, object_id);
1008 object = av_mallocz(sizeof(DVBSubObject));
1010 return AVERROR(ENOMEM);
1012 object->id = object_id;
1013 object->next = ctx->object_list;
1014 ctx->object_list = object;
1017 object->type = (*buf) >> 6;
1019 display = av_mallocz(sizeof(DVBSubObjectDisplay));
1021 return AVERROR(ENOMEM);
1023 display->object_id = object_id;
1024 display->region_id = region_id;
1026 display->x_pos = AV_RB16(buf) & 0xfff;
1028 display->y_pos = AV_RB16(buf) & 0xfff;
1031 if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
1032 display->fgcolor = *buf++;
1033 display->bgcolor = *buf++;
1036 display->region_list_next = region->display_list;
1037 region->display_list = display;
1039 display->object_list_next = object->display_list;
1040 object->display_list = display;
1046 static int dvbsub_parse_page_segment(AVCodecContext *avctx,
1047 const uint8_t *buf, int buf_size)
1049 DVBSubContext *ctx = avctx->priv_data;
1050 DVBSubRegionDisplay *display;
1051 DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
1053 const uint8_t *buf_end = buf + buf_size;
1058 return AVERROR_INVALIDDATA;
1060 ctx->time_out = *buf++;
1061 page_state = ((*buf++) >> 2) & 3;
1063 ff_dlog(avctx, "Page time out %ds, state %d\n", ctx->time_out, page_state);
1065 if (page_state == 2) {
1069 tmp_display_list = ctx->display_list;
1070 ctx->display_list = NULL;
1071 ctx->display_list_size = 0;
1073 while (buf + 5 < buf_end) {
1077 display = tmp_display_list;
1078 tmp_ptr = &tmp_display_list;
1080 while (display && display->region_id != region_id) {
1081 tmp_ptr = &display->next;
1082 display = display->next;
1086 display = av_mallocz(sizeof(DVBSubRegionDisplay));
1088 return AVERROR(ENOMEM);
1091 display->region_id = region_id;
1093 display->x_pos = AV_RB16(buf);
1095 display->y_pos = AV_RB16(buf);
1098 *tmp_ptr = display->next;
1100 display->next = ctx->display_list;
1101 ctx->display_list = display;
1102 ctx->display_list_size++;
1104 ff_dlog(avctx, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
1107 while (tmp_display_list) {
1108 display = tmp_display_list;
1110 tmp_display_list = display->next;
1120 static int save_display_set(DVBSubContext *ctx)
1122 DVBSubRegion *region;
1123 DVBSubRegionDisplay *display;
1125 uint32_t *clut_table;
1126 int x_pos, y_pos, width, height;
1127 int x, y, y_off, x_off;
1130 static int fileno_index = 0;
1137 for (display = ctx->display_list; display; display = display->next) {
1138 region = get_region(ctx, display->region_id);
1141 x_pos = display->x_pos;
1142 y_pos = display->y_pos;
1143 width = region->width;
1144 height = region->height;
1146 if (display->x_pos < x_pos) {
1147 width += (x_pos - display->x_pos);
1148 x_pos = display->x_pos;
1151 if (display->y_pos < y_pos) {
1152 height += (y_pos - display->y_pos);
1153 y_pos = display->y_pos;
1156 if (display->x_pos + region->width > x_pos + width) {
1157 width = display->x_pos + region->width - x_pos;
1160 if (display->y_pos + region->height > y_pos + height) {
1161 height = display->y_pos + region->height - y_pos;
1168 pbuf = av_malloc(width * height * 4);
1170 return AVERROR(ENOMEM);
1172 for (display = ctx->display_list; display; display = display->next) {
1173 region = get_region(ctx, display->region_id);
1175 x_off = display->x_pos - x_pos;
1176 y_off = display->y_pos - y_pos;
1178 clut = get_clut(ctx, region->clut);
1181 clut = &default_clut;
1183 switch (region->depth) {
1185 clut_table = clut->clut4;
1188 clut_table = clut->clut256;
1192 clut_table = clut->clut16;
1196 for (y = 0; y < region->height; y++) {
1197 for (x = 0; x < region->width; x++) {
1198 pbuf[((y + y_off) * width) + x_off + x] =
1199 clut_table[region->pbuf[y * region->width + x]];
1205 snprintf(filename, sizeof(filename), "dvbs.%d", fileno_index);
1207 png_save(filename, pbuf, width, height);
1217 static int dvbsub_parse_display_definition_segment(AVCodecContext *avctx,
1221 DVBSubContext *ctx = avctx->priv_data;
1222 DVBSubDisplayDefinition *display_def = ctx->display_definition;
1223 int dds_version, info_byte;
1226 return AVERROR_INVALIDDATA;
1228 info_byte = bytestream_get_byte(&buf);
1229 dds_version = info_byte >> 4;
1230 if (display_def && display_def->version == dds_version)
1231 return 0; // already have this display definition version
1234 display_def = av_mallocz(sizeof(*display_def));
1236 return AVERROR(ENOMEM);
1237 ctx->display_definition = display_def;
1240 display_def->version = dds_version;
1243 display_def->width = bytestream_get_be16(&buf) + 1;
1244 display_def->height = bytestream_get_be16(&buf) + 1;
1247 return AVERROR_INVALIDDATA;
1249 if (info_byte & 1<<3) { // display_window_flag
1250 display_def->x = bytestream_get_be16(&buf);
1251 display_def->y = bytestream_get_be16(&buf);
1252 display_def->width = bytestream_get_be16(&buf) - display_def->x + 1;
1253 display_def->height = bytestream_get_be16(&buf) - display_def->y + 1;
1259 static int dvbsub_display_end_segment(AVCodecContext *avctx, const uint8_t *buf,
1260 int buf_size, AVSubtitle *sub)
1262 DVBSubContext *ctx = avctx->priv_data;
1263 DVBSubDisplayDefinition *display_def = ctx->display_definition;
1265 DVBSubRegion *region;
1266 DVBSubRegionDisplay *display;
1267 AVSubtitleRect *rect;
1269 uint32_t *clut_table;
1271 int offset_x=0, offset_y=0;
1274 sub->start_display_time = 0;
1275 sub->end_display_time = ctx->time_out * 1000;
1279 offset_x = display_def->x;
1280 offset_y = display_def->y;
1283 sub->num_rects = ctx->display_list_size;
1284 if (sub->num_rects <= 0)
1285 return AVERROR_INVALIDDATA;
1287 sub->rects = av_mallocz_array(sub->num_rects * sub->num_rects,
1288 sizeof(*sub->rects));
1290 return AVERROR(ENOMEM);
1294 for (display = ctx->display_list; display; display = display->next) {
1295 region = get_region(ctx, display->region_id);
1296 rect = sub->rects[i];
1301 rect->x = display->x_pos + offset_x;
1302 rect->y = display->y_pos + offset_y;
1303 rect->w = region->width;
1304 rect->h = region->height;
1305 rect->nb_colors = 16;
1306 rect->type = SUBTITLE_BITMAP;
1307 rect->linesize[0] = region->width;
1309 clut = get_clut(ctx, region->clut);
1312 clut = &default_clut;
1314 switch (region->depth) {
1316 clut_table = clut->clut4;
1319 clut_table = clut->clut256;
1323 clut_table = clut->clut16;
1327 rect->data[1] = av_mallocz(AVPALETTE_SIZE);
1328 if (!rect->data[1]) {
1329 av_free(sub->rects);
1330 return AVERROR(ENOMEM);
1332 memcpy(rect->data[1], clut_table, (1 << region->depth) * sizeof(uint32_t));
1334 rect->data[0] = av_malloc(region->buf_size);
1335 if (!rect->data[0]) {
1336 av_free(rect->data[1]);
1337 av_free(sub->rects);
1338 return AVERROR(ENOMEM);
1340 memcpy(rect->data[0], region->pbuf, region->buf_size);
1342 #if FF_API_AVPICTURE
1343 FF_DISABLE_DEPRECATION_WARNINGS
1346 for (j = 0; j < 4; j++) {
1347 rect->pict.data[j] = rect->data[j];
1348 rect->pict.linesize[j] = rect->linesize[j];
1351 FF_ENABLE_DEPRECATION_WARNINGS
1360 save_display_set(ctx);
1366 static int dvbsub_decode(AVCodecContext *avctx,
1367 void *data, int *data_size,
1370 const uint8_t *buf = avpkt->data;
1371 int buf_size = avpkt->size;
1372 DVBSubContext *ctx = avctx->priv_data;
1373 AVSubtitle *sub = data;
1374 const uint8_t *p, *p_end;
1380 ff_dlog(avctx, "DVB sub packet:\n");
1382 for (i=0; i < buf_size; i++) {
1383 ff_dlog(avctx, "%02x ", buf[i]);
1385 ff_dlog(avctx, "\n");
1389 ff_dlog(avctx, "\n");
1391 if (buf_size <= 6 || *buf != 0x0f) {
1392 ff_dlog(avctx, "incomplete or broken packet");
1393 return AVERROR_INVALIDDATA;
1397 p_end = buf + buf_size;
1399 while (p_end - p >= 6 && *p == 0x0f) {
1401 segment_type = *p++;
1402 page_id = AV_RB16(p);
1404 segment_length = AV_RB16(p);
1407 if (p_end - p < segment_length) {
1408 ff_dlog(avctx, "incomplete or broken packet");
1412 if (page_id == ctx->composition_id || page_id == ctx->ancillary_id ||
1413 ctx->composition_id == -1 || ctx->ancillary_id == -1) {
1415 switch (segment_type) {
1416 case DVBSUB_PAGE_SEGMENT:
1417 ret = dvbsub_parse_page_segment(avctx, p, segment_length);
1419 case DVBSUB_REGION_SEGMENT:
1420 ret = dvbsub_parse_region_segment(avctx, p, segment_length);
1422 case DVBSUB_CLUT_SEGMENT:
1423 ret = dvbsub_parse_clut_segment(avctx, p, segment_length);
1425 case DVBSUB_OBJECT_SEGMENT:
1426 ret = dvbsub_parse_object_segment(avctx, p, segment_length);
1428 case DVBSUB_DISPLAYDEFINITION_SEGMENT:
1429 ret = dvbsub_parse_display_definition_segment(avctx, p,
1432 case DVBSUB_DISPLAY_SEGMENT:
1433 ret = dvbsub_display_end_segment(avctx, p, segment_length, sub);
1437 ff_dlog(avctx, "Subtitling segment type 0x%x, page id %d, length %d\n",
1438 segment_type, page_id, segment_length);
1445 p += segment_length;
1452 AVCodec ff_dvbsub_decoder = {
1454 .long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),
1455 .type = AVMEDIA_TYPE_SUBTITLE,
1456 .id = AV_CODEC_ID_DVB_SUBTITLE,
1457 .priv_data_size = sizeof(DVBSubContext),
1458 .init = dvbsub_init_decoder,
1459 .close = dvbsub_close_decoder,
1460 .decode = dvbsub_decode,