2 * DVB subtitle decoding
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"
26 #include "libavutil/colorspace.h"
27 #include "libavutil/opt.h"
29 #define DVBSUB_PAGE_SEGMENT 0x10
30 #define DVBSUB_REGION_SEGMENT 0x11
31 #define DVBSUB_CLUT_SEGMENT 0x12
32 #define DVBSUB_OBJECT_SEGMENT 0x13
33 #define DVBSUB_DISPLAYDEFINITION_SEGMENT 0x14
34 #define DVBSUB_DISPLAY_SEGMENT 0x80
36 #define cm (ff_crop_tab + MAX_NEG_CROP)
39 static void png_save(const char *filename, uint32_t *bitmap, int w, int h)
43 char fname[40], fname2[40];
46 snprintf(fname, sizeof(fname), "%s.ppm", filename);
48 f = fopen(fname, "w");
57 for(y = 0; y < h; y++) {
58 for(x = 0; x < w; x++) {
59 v = bitmap[y * w + x];
60 putc((v >> 16) & 0xff, f);
61 putc((v >> 8) & 0xff, f);
62 putc((v >> 0) & 0xff, f);
68 snprintf(fname2, sizeof(fname2), "%s-a.pgm", filename);
70 f = fopen(fname2, "w");
79 for(y = 0; y < h; y++) {
80 for(x = 0; x < w; x++) {
81 v = bitmap[y * w + x];
82 putc((v >> 24) & 0xff, f);
87 snprintf(command, sizeof(command), "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
90 snprintf(command, sizeof(command), "rm %s %s", fname, fname2);
95 #define RGBA(r,g,b,a) (((unsigned)(a) << 24) | ((r) << 16) | ((g) << 8) | (b))
97 typedef struct DVBSubCLUT {
103 uint32_t clut256[256];
105 struct DVBSubCLUT *next;
108 static DVBSubCLUT default_clut;
110 typedef struct DVBSubObjectDisplay {
120 struct DVBSubObjectDisplay *region_list_next;
121 struct DVBSubObjectDisplay *object_list_next;
122 } DVBSubObjectDisplay;
124 typedef struct DVBSubObject {
130 DVBSubObjectDisplay *display_list;
132 struct DVBSubObject *next;
135 typedef struct DVBSubRegionDisplay {
141 struct DVBSubRegionDisplay *next;
142 } DVBSubRegionDisplay;
144 typedef struct DVBSubRegion {
159 DVBSubObjectDisplay *display_list;
161 struct DVBSubRegion *next;
164 typedef struct DVBSubDisplayDefinition {
171 } DVBSubDisplayDefinition;
173 typedef struct DVBSubContext {
180 int compute_edt; /**< if 1 end display time calculated using pts
181 if 0 (Default) calculated using time out */
185 DVBSubRegion *region_list;
186 DVBSubCLUT *clut_list;
187 DVBSubObject *object_list;
189 DVBSubRegionDisplay *display_list;
190 DVBSubDisplayDefinition *display_definition;
194 static DVBSubObject* get_object(DVBSubContext *ctx, int object_id)
196 DVBSubObject *ptr = ctx->object_list;
198 while (ptr && ptr->id != object_id) {
205 static DVBSubCLUT* get_clut(DVBSubContext *ctx, int clut_id)
207 DVBSubCLUT *ptr = ctx->clut_list;
209 while (ptr && ptr->id != clut_id) {
216 static DVBSubRegion* get_region(DVBSubContext *ctx, int region_id)
218 DVBSubRegion *ptr = ctx->region_list;
220 while (ptr && ptr->id != region_id) {
227 static void delete_region_display_list(DVBSubContext *ctx, DVBSubRegion *region)
229 DVBSubObject *object, *obj2, **obj2_ptr;
230 DVBSubObjectDisplay *display, *obj_disp, **obj_disp_ptr;
232 while (region->display_list) {
233 display = region->display_list;
235 object = get_object(ctx, display->object_id);
238 obj_disp_ptr = &object->display_list;
239 obj_disp = *obj_disp_ptr;
241 while (obj_disp && obj_disp != display) {
242 obj_disp_ptr = &obj_disp->object_list_next;
243 obj_disp = *obj_disp_ptr;
247 *obj_disp_ptr = obj_disp->object_list_next;
249 if (!object->display_list) {
250 obj2_ptr = &ctx->object_list;
253 while (obj2 != object) {
255 obj2_ptr = &obj2->next;
259 *obj2_ptr = obj2->next;
266 region->display_list = display->region_list_next;
273 static void delete_cluts(DVBSubContext *ctx)
275 while (ctx->clut_list) {
276 DVBSubCLUT *clut = ctx->clut_list;
278 ctx->clut_list = clut->next;
284 static void delete_objects(DVBSubContext *ctx)
286 while (ctx->object_list) {
287 DVBSubObject *object = ctx->object_list;
289 ctx->object_list = object->next;
295 static void delete_regions(DVBSubContext *ctx)
297 while (ctx->region_list) {
298 DVBSubRegion *region = ctx->region_list;
300 ctx->region_list = region->next;
302 delete_region_display_list(ctx, region);
304 av_freep(®ion->pbuf);
309 static av_cold int dvbsub_init_decoder(AVCodecContext *avctx)
311 int i, r, g, b, a = 0;
312 DVBSubContext *ctx = avctx->priv_data;
314 if (ctx->substream < 0) {
315 ctx->composition_id = -1;
316 ctx->ancillary_id = -1;
317 } else if (!avctx->extradata || (avctx->extradata_size < 4) || ((avctx->extradata_size % 5 != 0) && (avctx->extradata_size != 4))) {
318 av_log(avctx, AV_LOG_WARNING, "Invalid DVB subtitles stream extradata!\n");
319 ctx->composition_id = -1;
320 ctx->ancillary_id = -1;
322 if (avctx->extradata_size > 5*ctx->substream + 2) {
323 ctx->composition_id = AV_RB16(avctx->extradata + 5*ctx->substream);
324 ctx->ancillary_id = AV_RB16(avctx->extradata + 5*ctx->substream + 2);
326 av_log(avctx, AV_LOG_WARNING, "Selected DVB subtitles sub-stream %d is not available\n", ctx->substream);
327 ctx->composition_id = AV_RB16(avctx->extradata);
328 ctx->ancillary_id = AV_RB16(avctx->extradata + 2);
333 ctx->prev_start = AV_NOPTS_VALUE;
335 default_clut.id = -1;
336 default_clut.next = NULL;
338 default_clut.clut4[0] = RGBA( 0, 0, 0, 0);
339 default_clut.clut4[1] = RGBA(255, 255, 255, 255);
340 default_clut.clut4[2] = RGBA( 0, 0, 0, 255);
341 default_clut.clut4[3] = RGBA(127, 127, 127, 255);
343 default_clut.clut16[0] = RGBA( 0, 0, 0, 0);
344 for (i = 1; i < 16; i++) {
346 r = (i & 1) ? 255 : 0;
347 g = (i & 2) ? 255 : 0;
348 b = (i & 4) ? 255 : 0;
350 r = (i & 1) ? 127 : 0;
351 g = (i & 2) ? 127 : 0;
352 b = (i & 4) ? 127 : 0;
354 default_clut.clut16[i] = RGBA(r, g, b, 255);
357 default_clut.clut256[0] = RGBA( 0, 0, 0, 0);
358 for (i = 1; i < 256; i++) {
360 r = (i & 1) ? 255 : 0;
361 g = (i & 2) ? 255 : 0;
362 b = (i & 4) ? 255 : 0;
367 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
368 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
369 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
373 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
374 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
375 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
379 r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
380 g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
381 b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
385 r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
386 g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
387 b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
392 default_clut.clut256[i] = RGBA(r, g, b, a);
398 static av_cold int dvbsub_close_decoder(AVCodecContext *avctx)
400 DVBSubContext *ctx = avctx->priv_data;
401 DVBSubRegionDisplay *display;
409 av_freep(&ctx->display_definition);
411 while (ctx->display_list) {
412 display = ctx->display_list;
413 ctx->display_list = display->next;
421 static int dvbsub_read_2bit_string(AVCodecContext *avctx,
422 uint8_t *destbuf, int dbuf_len,
423 const uint8_t **srcbuf, int buf_size,
424 int non_mod, uint8_t *map_table, int x_pos)
430 int pixels_read = x_pos;
432 init_get_bits(&gb, *srcbuf, buf_size << 3);
436 while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
437 bits = get_bits(&gb, 2);
440 if (non_mod != 1 || bits != 1) {
442 *destbuf++ = map_table[bits];
448 bits = get_bits1(&gb);
450 run_length = get_bits(&gb, 3) + 3;
451 bits = get_bits(&gb, 2);
453 if (non_mod == 1 && bits == 1)
454 pixels_read += run_length;
457 bits = map_table[bits];
458 while (run_length-- > 0 && pixels_read < dbuf_len) {
464 bits = get_bits1(&gb);
466 bits = get_bits(&gb, 2);
468 run_length = get_bits(&gb, 4) + 12;
469 bits = get_bits(&gb, 2);
471 if (non_mod == 1 && bits == 1)
472 pixels_read += run_length;
475 bits = map_table[bits];
476 while (run_length-- > 0 && pixels_read < dbuf_len) {
481 } else if (bits == 3) {
482 run_length = get_bits(&gb, 8) + 29;
483 bits = get_bits(&gb, 2);
485 if (non_mod == 1 && bits == 1)
486 pixels_read += run_length;
489 bits = map_table[bits];
490 while (run_length-- > 0 && pixels_read < dbuf_len) {
495 } else if (bits == 1) {
501 while (run_length-- > 0 && pixels_read < dbuf_len) {
506 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
521 if (get_bits(&gb, 6))
522 av_log(avctx, AV_LOG_ERROR, "line overflow\n");
524 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
529 static int dvbsub_read_4bit_string(AVCodecContext *avctx, uint8_t *destbuf, int dbuf_len,
530 const uint8_t **srcbuf, int buf_size,
531 int non_mod, uint8_t *map_table, int x_pos)
537 int pixels_read = x_pos;
539 init_get_bits(&gb, *srcbuf, buf_size << 3);
543 while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
544 bits = get_bits(&gb, 4);
547 if (non_mod != 1 || bits != 1) {
549 *destbuf++ = map_table[bits];
555 bits = get_bits1(&gb);
557 run_length = get_bits(&gb, 3);
559 if (run_length == 0) {
560 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
571 while (run_length-- > 0 && pixels_read < dbuf_len) {
576 bits = get_bits1(&gb);
578 run_length = get_bits(&gb, 2) + 4;
579 bits = get_bits(&gb, 4);
581 if (non_mod == 1 && bits == 1)
582 pixels_read += run_length;
585 bits = map_table[bits];
586 while (run_length-- > 0 && pixels_read < dbuf_len) {
592 bits = get_bits(&gb, 2);
594 run_length = get_bits(&gb, 4) + 9;
595 bits = get_bits(&gb, 4);
597 if (non_mod == 1 && bits == 1)
598 pixels_read += run_length;
601 bits = map_table[bits];
602 while (run_length-- > 0 && pixels_read < dbuf_len) {
607 } else if (bits == 3) {
608 run_length = get_bits(&gb, 8) + 25;
609 bits = get_bits(&gb, 4);
611 if (non_mod == 1 && bits == 1)
612 pixels_read += run_length;
615 bits = map_table[bits];
616 while (run_length-- > 0 && pixels_read < dbuf_len) {
621 } else if (bits == 1) {
627 while (run_length-- > 0 && pixels_read < dbuf_len) {
644 if (get_bits(&gb, 8))
645 av_log(avctx, AV_LOG_ERROR, "line overflow\n");
647 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
652 static int dvbsub_read_8bit_string(AVCodecContext *avctx,
653 uint8_t *destbuf, int dbuf_len,
654 const uint8_t **srcbuf, int buf_size,
655 int non_mod, uint8_t *map_table, int x_pos)
657 const uint8_t *sbuf_end = (*srcbuf) + buf_size;
660 int pixels_read = x_pos;
664 while (*srcbuf < sbuf_end && pixels_read < dbuf_len) {
668 if (non_mod != 1 || bits != 1) {
670 *destbuf++ = map_table[bits];
677 run_length = bits & 0x7f;
678 if ((bits & 0x80) == 0) {
679 if (run_length == 0) {
687 if (non_mod == 1 && bits == 1)
688 pixels_read += run_length;
691 bits = map_table[bits];
692 while (run_length-- > 0 && pixels_read < dbuf_len) {
701 av_log(avctx, AV_LOG_ERROR, "line overflow\n");
706 static void compute_default_clut(AVSubtitleRect *rect, int w, int h)
708 uint8_t list[256] = {0};
709 uint8_t list_inv[256];
710 int counttab[256] = {0};
713 #define V(x,y) rect->data[0][(x) + (y)*rect->linesize[0]]
714 for (y = 0; y<h; y++) {
715 for (x = 0; x<w; x++) {
717 int vl = x ? V(x-1,y) + 1 : 0;
718 int vr = x+1<w ? V(x+1,y) + 1 : 0;
719 int vt = y ? V(x,y-1) + 1 : 0;
720 int vb = y+1<h ? V(x,y+1) + 1 : 0;
721 counttab[v-1] += !!((v!=vl) + (v!=vr) + (v!=vt) + (v!=vb));
724 #define L(x,y) list[ rect->data[0][(x) + (y)*rect->linesize[0]] ]
726 for (i = 0; i<256; i++) {
727 int scoretab[256] = {0};
730 for (y = 0; y<h; y++) {
731 for (x = 0; x<w; x++) {
732 int v = rect->data[0][x + y*rect->linesize[0]];
734 int l_l = x ? L(x-1, y) : 1;
735 int l_r = x+1<w ? L(x+1, y) : 1;
736 int l_t = y ? L(x, y-1) : 1;
737 int l_b = y+1<h ? L(x, y+1) : 1;
741 scoretab[v] += l_l + l_r + l_t + l_b;
742 score = 1024LL*scoretab[v] / counttab[v];
743 if (score > bestscore) {
752 list_inv[ i ] = bestv;
755 count = FFMAX(i - 1, 1);
756 for (i--; i>=0; i--) {
758 AV_WN32(rect->data[1] + 4*list_inv[i], RGBA(v/2,v,v/2,v));
763 static int save_subtitle_set(AVCodecContext *avctx, AVSubtitle *sub, int *got_output)
765 DVBSubContext *ctx = avctx->priv_data;
766 DVBSubRegionDisplay *display;
767 DVBSubDisplayDefinition *display_def = ctx->display_definition;
768 DVBSubRegion *region;
769 AVSubtitleRect *rect;
771 uint32_t *clut_table;
773 int offset_x=0, offset_y=0;
778 offset_x = display_def->x;
779 offset_y = display_def->y;
782 /* Not touching AVSubtitles again*/
784 avpriv_request_sample(ctx, "Different Version of Segment asked Twice");
785 return AVERROR_PATCHWELCOME;
787 for (display = ctx->display_list; display; display = display->next) {
788 region = get_region(ctx, display->region_id);
789 if (region && region->dirty)
793 if(ctx->compute_edt == 0) {
794 sub->end_display_time = ctx->time_out * 1000;
796 } else if (ctx->prev_start != AV_NOPTS_VALUE) {
797 sub->end_display_time = av_rescale_q((sub->pts - ctx->prev_start ), AV_TIME_BASE_Q, (AVRational){ 1, 1000 }) - 1;
800 if (sub->num_rects > 0) {
802 sub->rects = av_mallocz_array(sizeof(*sub->rects), sub->num_rects);
804 ret = AVERROR(ENOMEM);
808 for(i=0; i<sub->num_rects; i++)
809 sub->rects[i] = av_mallocz(sizeof(*sub->rects[i]));
813 for (display = ctx->display_list; display; display = display->next) {
814 region = get_region(ctx, display->region_id);
822 rect = sub->rects[i];
823 rect->x = display->x_pos + offset_x;
824 rect->y = display->y_pos + offset_y;
825 rect->w = region->width;
826 rect->h = region->height;
827 rect->nb_colors = (1 << region->depth);
828 rect->type = SUBTITLE_BITMAP;
829 rect->linesize[0] = region->width;
831 clut = get_clut(ctx, region->clut);
834 clut = &default_clut;
836 switch (region->depth) {
838 clut_table = clut->clut4;
841 clut_table = clut->clut256;
845 clut_table = clut->clut16;
849 rect->data[1] = av_mallocz(AVPALETTE_SIZE);
850 if (!rect->data[1]) {
851 ret = AVERROR(ENOMEM);
854 memcpy(rect->data[1], clut_table, (1 << region->depth) * sizeof(uint32_t));
856 rect->data[0] = av_malloc(region->buf_size);
857 if (!rect->data[0]) {
858 ret = AVERROR(ENOMEM);
862 memcpy(rect->data[0], region->pbuf, region->buf_size);
864 if ((clut == &default_clut && ctx->compute_clut == -1) || ctx->compute_clut == 1)
865 compute_default_clut(rect, rect->w, rect->h);
868 FF_DISABLE_DEPRECATION_WARNINGS
871 for (j = 0; j < 4; j++) {
872 rect->pict.data[j] = rect->data[j];
873 rect->pict.linesize[j] = rect->linesize[j];
876 FF_ENABLE_DEPRECATION_WARNINGS
886 for(i=0; i<sub->num_rects; i++) {
887 rect = sub->rects[i];
889 av_freep(&rect->data[0]);
890 av_freep(&rect->data[1]);
892 av_freep(&sub->rects[i]);
894 av_freep(&sub->rects);
900 static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
901 const uint8_t *buf, int buf_size, int top_bottom, int non_mod)
903 DVBSubContext *ctx = avctx->priv_data;
905 DVBSubRegion *region = get_region(ctx, display->region_id);
906 const uint8_t *buf_end = buf + buf_size;
911 uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
912 uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
913 uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
914 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
918 ff_dlog(avctx, "DVB pixel block size %d, %s field:\n", buf_size,
919 top_bottom ? "bottom" : "top");
921 for (i = 0; i < buf_size; i++) {
923 ff_dlog(avctx, "0x%8p: ", buf+i);
925 ff_dlog(avctx, "%02x ", buf[i]);
927 ff_dlog(avctx, "\n");
931 ff_dlog(avctx, "\n");
940 x_pos = display->x_pos;
941 y_pos = display->y_pos;
945 while (buf < buf_end) {
946 if ((*buf!=0xf0 && x_pos >= region->width) || y_pos >= region->height) {
947 av_log(avctx, AV_LOG_ERROR, "Invalid object location! %d-%d %d-%d %02x\n", x_pos, region->width, y_pos, region->height, *buf);
953 if (region->depth == 8)
955 else if (region->depth == 4)
960 x_pos = dvbsub_read_2bit_string(avctx, pbuf + (y_pos * region->width),
961 region->width, &buf, buf_end - buf,
962 non_mod, map_table, x_pos);
965 if (region->depth < 4) {
966 av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
970 if (region->depth == 8)
975 x_pos = dvbsub_read_4bit_string(avctx, pbuf + (y_pos * region->width),
976 region->width, &buf, buf_end - buf,
977 non_mod, map_table, x_pos);
980 if (region->depth < 8) {
981 av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
985 x_pos = dvbsub_read_8bit_string(avctx, pbuf + (y_pos * region->width),
986 region->width, &buf, buf_end - buf,
987 non_mod, NULL, x_pos);
991 map2to4[0] = (*buf) >> 4;
992 map2to4[1] = (*buf++) & 0xf;
993 map2to4[2] = (*buf) >> 4;
994 map2to4[3] = (*buf++) & 0xf;
997 for (i = 0; i < 4; i++)
1001 for (i = 0; i < 16; i++)
1002 map4to8[i] = *buf++;
1006 x_pos = display->x_pos;
1010 av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
1016 static int dvbsub_parse_object_segment(AVCodecContext *avctx,
1017 const uint8_t *buf, int buf_size)
1019 DVBSubContext *ctx = avctx->priv_data;
1021 const uint8_t *buf_end = buf + buf_size;
1023 DVBSubObject *object;
1024 DVBSubObjectDisplay *display;
1025 int top_field_len, bottom_field_len;
1027 int coding_method, non_modifying_color;
1029 object_id = AV_RB16(buf);
1032 object = get_object(ctx, object_id);
1035 return AVERROR_INVALIDDATA;
1037 coding_method = ((*buf) >> 2) & 3;
1038 non_modifying_color = ((*buf++) >> 1) & 1;
1040 if (coding_method == 0) {
1041 top_field_len = AV_RB16(buf);
1043 bottom_field_len = AV_RB16(buf);
1046 if (buf + top_field_len + bottom_field_len > buf_end) {
1047 av_log(avctx, AV_LOG_ERROR, "Field data size %d+%d too large\n", top_field_len, bottom_field_len);
1048 return AVERROR_INVALIDDATA;
1051 for (display = object->display_list; display; display = display->object_list_next) {
1052 const uint8_t *block = buf;
1053 int bfl = bottom_field_len;
1055 dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
1056 non_modifying_color);
1058 if (bottom_field_len > 0)
1059 block = buf + top_field_len;
1061 bfl = top_field_len;
1063 dvbsub_parse_pixel_data_block(avctx, display, block, bfl, 1,
1064 non_modifying_color);
1067 /* } else if (coding_method == 1) {*/
1070 av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
1076 static int dvbsub_parse_clut_segment(AVCodecContext *avctx,
1077 const uint8_t *buf, int buf_size)
1079 DVBSubContext *ctx = avctx->priv_data;
1081 const uint8_t *buf_end = buf + buf_size;
1085 int entry_id, depth , full_range;
1086 int y, cr, cb, alpha;
1087 int r, g, b, r_add, g_add, b_add;
1089 ff_dlog(avctx, "DVB clut packet:\n");
1091 for (i=0; i < buf_size; i++) {
1092 ff_dlog(avctx, "%02x ", buf[i]);
1094 ff_dlog(avctx, "\n");
1098 ff_dlog(avctx, "\n");
1101 version = ((*buf)>>4)&15;
1104 clut = get_clut(ctx, clut_id);
1107 clut = av_malloc(sizeof(DVBSubCLUT));
1109 return AVERROR(ENOMEM);
1111 memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
1116 clut->next = ctx->clut_list;
1117 ctx->clut_list = clut;
1120 if (clut->version != version) {
1122 clut->version = version;
1124 while (buf + 4 < buf_end) {
1127 depth = (*buf) & 0xe0;
1130 av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
1133 full_range = (*buf++) & 1;
1142 cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
1143 cb = (buf[1] << 2) & 0xf0;
1144 alpha = (buf[1] << 6) & 0xc0;
1152 YUV_TO_RGB1_CCIR(cb, cr);
1153 YUV_TO_RGB2_CCIR(r, g, b, y);
1155 ff_dlog(avctx, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
1156 if (!!(depth & 0x80) + !!(depth & 0x40) + !!(depth & 0x20) > 1) {
1157 ff_dlog(avctx, "More than one bit level marked: %x\n", depth);
1158 if (avctx->strict_std_compliance > FF_COMPLIANCE_NORMAL)
1159 return AVERROR_INVALIDDATA;
1163 clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
1164 else if (depth & 0x40)
1165 clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
1166 else if (depth & 0x20)
1167 clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
1175 static int dvbsub_parse_region_segment(AVCodecContext *avctx,
1176 const uint8_t *buf, int buf_size)
1178 DVBSubContext *ctx = avctx->priv_data;
1180 const uint8_t *buf_end = buf + buf_size;
1181 int region_id, object_id;
1182 int av_unused version;
1183 DVBSubRegion *region;
1184 DVBSubObject *object;
1185 DVBSubObjectDisplay *display;
1189 return AVERROR_INVALIDDATA;
1193 region = get_region(ctx, region_id);
1196 region = av_mallocz(sizeof(DVBSubRegion));
1198 return AVERROR(ENOMEM);
1200 region->id = region_id;
1201 region->version = -1;
1203 region->next = ctx->region_list;
1204 ctx->region_list = region;
1207 version = ((*buf)>>4) & 15;
1208 fill = ((*buf++) >> 3) & 1;
1210 region->width = AV_RB16(buf);
1212 region->height = AV_RB16(buf);
1215 if (region->width * region->height != region->buf_size) {
1216 av_free(region->pbuf);
1218 region->buf_size = region->width * region->height;
1220 region->pbuf = av_malloc(region->buf_size);
1221 if (!region->pbuf) {
1225 return AVERROR(ENOMEM);
1232 region->depth = 1 << (((*buf++) >> 2) & 7);
1233 if(region->depth<2 || region->depth>8){
1234 av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);
1237 region->clut = *buf++;
1239 if (region->depth == 8) {
1240 region->bgcolor = *buf++;
1245 if (region->depth == 4)
1246 region->bgcolor = (((*buf++) >> 4) & 15);
1248 region->bgcolor = (((*buf++) >> 2) & 3);
1251 ff_dlog(avctx, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
1254 memset(region->pbuf, region->bgcolor, region->buf_size);
1255 ff_dlog(avctx, "Fill region (%d)\n", region->bgcolor);
1258 delete_region_display_list(ctx, region);
1260 while (buf + 5 < buf_end) {
1261 object_id = AV_RB16(buf);
1264 object = get_object(ctx, object_id);
1267 object = av_mallocz(sizeof(DVBSubObject));
1269 return AVERROR(ENOMEM);
1271 object->id = object_id;
1272 object->next = ctx->object_list;
1273 ctx->object_list = object;
1276 object->type = (*buf) >> 6;
1278 display = av_mallocz(sizeof(DVBSubObjectDisplay));
1280 return AVERROR(ENOMEM);
1282 display->object_id = object_id;
1283 display->region_id = region_id;
1285 display->x_pos = AV_RB16(buf) & 0xfff;
1287 display->y_pos = AV_RB16(buf) & 0xfff;
1290 if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
1291 display->fgcolor = *buf++;
1292 display->bgcolor = *buf++;
1295 display->region_list_next = region->display_list;
1296 region->display_list = display;
1298 display->object_list_next = object->display_list;
1299 object->display_list = display;
1305 static int dvbsub_parse_page_segment(AVCodecContext *avctx,
1306 const uint8_t *buf, int buf_size, AVSubtitle *sub, int *got_output)
1308 DVBSubContext *ctx = avctx->priv_data;
1309 DVBSubRegionDisplay *display;
1310 DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
1312 const uint8_t *buf_end = buf + buf_size;
1319 return AVERROR_INVALIDDATA;
1322 version = ((*buf)>>4) & 15;
1323 page_state = ((*buf++) >> 2) & 3;
1325 if (ctx->version == version) {
1329 ctx->time_out = timeout;
1330 ctx->version = version;
1332 ff_dlog(avctx, "Page time out %ds, state %d\n", ctx->time_out, page_state);
1334 if(ctx->compute_edt == 1)
1335 save_subtitle_set(avctx, sub, got_output);
1337 if (page_state == 1 || page_state == 2) {
1338 delete_regions(ctx);
1339 delete_objects(ctx);
1343 tmp_display_list = ctx->display_list;
1344 ctx->display_list = NULL;
1346 while (buf + 5 < buf_end) {
1350 display = tmp_display_list;
1351 tmp_ptr = &tmp_display_list;
1353 while (display && display->region_id != region_id) {
1354 tmp_ptr = &display->next;
1355 display = display->next;
1359 display = av_mallocz(sizeof(DVBSubRegionDisplay));
1361 return AVERROR(ENOMEM);
1364 display->region_id = region_id;
1366 display->x_pos = AV_RB16(buf);
1368 display->y_pos = AV_RB16(buf);
1371 *tmp_ptr = display->next;
1373 display->next = ctx->display_list;
1374 ctx->display_list = display;
1376 ff_dlog(avctx, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
1379 while (tmp_display_list) {
1380 display = tmp_display_list;
1382 tmp_display_list = display->next;
1392 static int save_display_set(DVBSubContext *ctx)
1394 DVBSubRegion *region;
1395 DVBSubRegionDisplay *display;
1397 uint32_t *clut_table;
1398 int x_pos, y_pos, width, height;
1399 int x, y, y_off, x_off;
1402 static int fileno_index = 0;
1409 for (display = ctx->display_list; display; display = display->next) {
1410 region = get_region(ctx, display->region_id);
1416 x_pos = display->x_pos;
1417 y_pos = display->y_pos;
1418 width = region->width;
1419 height = region->height;
1421 if (display->x_pos < x_pos) {
1422 width += (x_pos - display->x_pos);
1423 x_pos = display->x_pos;
1426 if (display->y_pos < y_pos) {
1427 height += (y_pos - display->y_pos);
1428 y_pos = display->y_pos;
1431 if (display->x_pos + region->width > x_pos + width) {
1432 width = display->x_pos + region->width - x_pos;
1435 if (display->y_pos + region->height > y_pos + height) {
1436 height = display->y_pos + region->height - y_pos;
1443 pbuf = av_malloc(width * height * 4);
1447 for (display = ctx->display_list; display; display = display->next) {
1448 region = get_region(ctx, display->region_id);
1453 x_off = display->x_pos - x_pos;
1454 y_off = display->y_pos - y_pos;
1456 clut = get_clut(ctx, region->clut);
1459 clut = &default_clut;
1461 switch (region->depth) {
1463 clut_table = clut->clut4;
1466 clut_table = clut->clut256;
1470 clut_table = clut->clut16;
1474 for (y = 0; y < region->height; y++) {
1475 for (x = 0; x < region->width; x++) {
1476 pbuf[((y + y_off) * width) + x_off + x] =
1477 clut_table[region->pbuf[y * region->width + x]];
1483 snprintf(filename, sizeof(filename), "dvbs.%d", fileno_index);
1485 png_save(filename, pbuf, width, height);
1495 static int dvbsub_parse_display_definition_segment(AVCodecContext *avctx,
1499 DVBSubContext *ctx = avctx->priv_data;
1500 DVBSubDisplayDefinition *display_def = ctx->display_definition;
1501 int dds_version, info_byte;
1504 return AVERROR_INVALIDDATA;
1506 info_byte = bytestream_get_byte(&buf);
1507 dds_version = info_byte >> 4;
1508 if (display_def && display_def->version == dds_version)
1509 return 0; // already have this display definition version
1512 display_def = av_mallocz(sizeof(*display_def));
1514 return AVERROR(ENOMEM);
1515 ctx->display_definition = display_def;
1518 display_def->version = dds_version;
1521 display_def->width = bytestream_get_be16(&buf) + 1;
1522 display_def->height = bytestream_get_be16(&buf) + 1;
1523 if (!avctx->width || !avctx->height) {
1524 avctx->width = display_def->width;
1525 avctx->height = display_def->height;
1528 if (info_byte & 1<<3) { // display_window_flag
1530 return AVERROR_INVALIDDATA;
1532 display_def->x = bytestream_get_be16(&buf);
1533 display_def->width = bytestream_get_be16(&buf) - display_def->x + 1;
1534 display_def->y = bytestream_get_be16(&buf);
1535 display_def->height = bytestream_get_be16(&buf) - display_def->y + 1;
1541 static int dvbsub_display_end_segment(AVCodecContext *avctx, const uint8_t *buf,
1542 int buf_size, AVSubtitle *sub,int *got_output)
1544 DVBSubContext *ctx = avctx->priv_data;
1546 if(ctx->compute_edt == 0)
1547 save_subtitle_set(avctx, sub, got_output);
1549 save_display_set(ctx);
1554 static int dvbsub_decode(AVCodecContext *avctx,
1555 void *data, int *data_size,
1558 const uint8_t *buf = avpkt->data;
1559 int buf_size = avpkt->size;
1560 DVBSubContext *ctx = avctx->priv_data;
1561 AVSubtitle *sub = data;
1562 const uint8_t *p, *p_end;
1568 int got_segment = 0;
1571 ff_dlog(avctx, "DVB sub packet:\n");
1573 for (i=0; i < buf_size; i++) {
1574 ff_dlog(avctx, "%02x ", buf[i]);
1576 ff_dlog(avctx, "\n");
1580 ff_dlog(avctx, "\n");
1582 if (buf_size <= 6 || *buf != 0x0f) {
1583 ff_dlog(avctx, "incomplete or broken packet");
1584 return AVERROR_INVALIDDATA;
1588 p_end = buf + buf_size;
1590 while (p_end - p >= 6 && *p == 0x0f) {
1592 segment_type = *p++;
1593 page_id = AV_RB16(p);
1595 segment_length = AV_RB16(p);
1598 if (avctx->debug & FF_DEBUG_STARTCODE) {
1599 av_log(avctx, AV_LOG_DEBUG, "segment_type:%d page_id:%d segment_length:%d\n", segment_type, page_id, segment_length);
1602 if (p_end - p < segment_length) {
1603 ff_dlog(avctx, "incomplete or broken packet");
1608 if (page_id == ctx->composition_id || page_id == ctx->ancillary_id ||
1609 ctx->composition_id == -1 || ctx->ancillary_id == -1) {
1611 switch (segment_type) {
1612 case DVBSUB_PAGE_SEGMENT:
1613 ret = dvbsub_parse_page_segment(avctx, p, segment_length, sub, data_size);
1616 case DVBSUB_REGION_SEGMENT:
1617 ret = dvbsub_parse_region_segment(avctx, p, segment_length);
1620 case DVBSUB_CLUT_SEGMENT:
1621 ret = dvbsub_parse_clut_segment(avctx, p, segment_length);
1622 if (ret < 0) goto end;
1625 case DVBSUB_OBJECT_SEGMENT:
1626 ret = dvbsub_parse_object_segment(avctx, p, segment_length);
1629 case DVBSUB_DISPLAYDEFINITION_SEGMENT:
1630 ret = dvbsub_parse_display_definition_segment(avctx, p,
1634 case DVBSUB_DISPLAY_SEGMENT:
1635 ret = dvbsub_display_end_segment(avctx, p, segment_length, sub, data_size);
1636 if (got_segment == 15 && !got_dds && !avctx->width && !avctx->height) {
1637 // Default from ETSI EN 300 743 V1.3.1 (7.2.1)
1639 avctx->height = 576;
1644 ff_dlog(avctx, "Subtitling segment type 0x%x, page id %d, length %d\n",
1645 segment_type, page_id, segment_length);
1652 p += segment_length;
1654 // Some streams do not send a display segment but if we have all the other
1655 // segments then we need no further data.
1656 if (got_segment == 15) {
1657 av_log(avctx, AV_LOG_DEBUG, "Missing display_end_segment, emulating\n");
1658 dvbsub_display_end_segment(avctx, p, 0, sub, data_size);
1664 avsubtitle_free(sub);
1667 if(ctx->compute_edt == 1 )
1668 FFSWAP(int64_t, ctx->prev_start, sub->pts);
1674 #define DS AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_SUBTITLE_PARAM
1675 static const AVOption options[] = {
1676 {"compute_edt", "compute end of time using pts or timeout", offsetof(DVBSubContext, compute_edt), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DS},
1677 {"compute_clut", "compute clut when not available(-1) or always(1) or never(0)", offsetof(DVBSubContext, compute_clut), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, DS},
1678 {"dvb_substream", "", offsetof(DVBSubContext, substream), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 63, DS},
1681 static const AVClass dvbsubdec_class = {
1682 .class_name = "DVB Sub Decoder",
1683 .item_name = av_default_item_name,
1685 .version = LIBAVUTIL_VERSION_INT,
1688 AVCodec ff_dvbsub_decoder = {
1690 .long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),
1691 .type = AVMEDIA_TYPE_SUBTITLE,
1692 .id = AV_CODEC_ID_DVB_SUBTITLE,
1693 .priv_data_size = sizeof(DVBSubContext),
1694 .init = dvbsub_init_decoder,
1695 .close = dvbsub_close_decoder,
1696 .decode = dvbsub_decode,
1697 .priv_class = &dvbsubdec_class,