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/imgutils.h"
28 #include "libavutil/opt.h"
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_DISPLAYDEFINITION_SEGMENT 0x14
35 #define DVBSUB_DISPLAY_SEGMENT 0x80
37 #define cm (ff_crop_tab + MAX_NEG_CROP)
39 #define RGBA(r,g,b,a) (((unsigned)(a) << 24) | ((r) << 16) | ((g) << 8) | (b))
41 typedef struct DVBSubCLUT {
47 uint32_t clut256[256];
49 struct DVBSubCLUT *next;
52 static DVBSubCLUT default_clut;
54 typedef struct DVBSubObjectDisplay {
64 struct DVBSubObjectDisplay *region_list_next;
65 struct DVBSubObjectDisplay *object_list_next;
66 } DVBSubObjectDisplay;
68 typedef struct DVBSubObject {
74 DVBSubObjectDisplay *display_list;
76 struct DVBSubObject *next;
79 typedef struct DVBSubRegionDisplay {
85 struct DVBSubRegionDisplay *next;
86 } DVBSubRegionDisplay;
88 typedef struct DVBSubRegion {
99 uint8_t computed_clut[4*256];
100 int has_computed_clut;
106 DVBSubObjectDisplay *display_list;
108 struct DVBSubRegion *next;
111 typedef struct DVBSubDisplayDefinition {
118 } DVBSubDisplayDefinition;
120 typedef struct DVBSubContext {
127 int compute_edt; /**< if 1 end display time calculated using pts
128 if 0 (Default) calculated using time out */
132 DVBSubRegion *region_list;
133 DVBSubCLUT *clut_list;
134 DVBSubObject *object_list;
136 DVBSubRegionDisplay *display_list;
137 DVBSubDisplayDefinition *display_definition;
141 static DVBSubObject* get_object(DVBSubContext *ctx, int object_id)
143 DVBSubObject *ptr = ctx->object_list;
145 while (ptr && ptr->id != object_id) {
152 static DVBSubCLUT* get_clut(DVBSubContext *ctx, int clut_id)
154 DVBSubCLUT *ptr = ctx->clut_list;
156 while (ptr && ptr->id != clut_id) {
163 static DVBSubRegion* get_region(DVBSubContext *ctx, int region_id)
165 DVBSubRegion *ptr = ctx->region_list;
167 while (ptr && ptr->id != region_id) {
174 static void delete_region_display_list(DVBSubContext *ctx, DVBSubRegion *region)
176 DVBSubObject *object, *obj2, **obj2_ptr;
177 DVBSubObjectDisplay *display, *obj_disp, **obj_disp_ptr;
179 while (region->display_list) {
180 display = region->display_list;
182 object = get_object(ctx, display->object_id);
185 obj_disp_ptr = &object->display_list;
186 obj_disp = *obj_disp_ptr;
188 while (obj_disp && obj_disp != display) {
189 obj_disp_ptr = &obj_disp->object_list_next;
190 obj_disp = *obj_disp_ptr;
194 *obj_disp_ptr = obj_disp->object_list_next;
196 if (!object->display_list) {
197 obj2_ptr = &ctx->object_list;
200 while (obj2 != object) {
202 obj2_ptr = &obj2->next;
206 *obj2_ptr = obj2->next;
213 region->display_list = display->region_list_next;
220 static void delete_cluts(DVBSubContext *ctx)
222 while (ctx->clut_list) {
223 DVBSubCLUT *clut = ctx->clut_list;
225 ctx->clut_list = clut->next;
231 static void delete_objects(DVBSubContext *ctx)
233 while (ctx->object_list) {
234 DVBSubObject *object = ctx->object_list;
236 ctx->object_list = object->next;
242 static void delete_regions(DVBSubContext *ctx)
244 while (ctx->region_list) {
245 DVBSubRegion *region = ctx->region_list;
247 ctx->region_list = region->next;
249 delete_region_display_list(ctx, region);
251 av_freep(®ion->pbuf);
256 static av_cold int dvbsub_init_decoder(AVCodecContext *avctx)
258 int i, r, g, b, a = 0;
259 DVBSubContext *ctx = avctx->priv_data;
261 if (ctx->substream < 0) {
262 ctx->composition_id = -1;
263 ctx->ancillary_id = -1;
264 } else if (!avctx->extradata || (avctx->extradata_size < 4) || ((avctx->extradata_size % 5 != 0) && (avctx->extradata_size != 4))) {
265 av_log(avctx, AV_LOG_WARNING, "Invalid DVB subtitles stream extradata!\n");
266 ctx->composition_id = -1;
267 ctx->ancillary_id = -1;
269 if (avctx->extradata_size > 5*ctx->substream + 2) {
270 ctx->composition_id = AV_RB16(avctx->extradata + 5*ctx->substream);
271 ctx->ancillary_id = AV_RB16(avctx->extradata + 5*ctx->substream + 2);
273 av_log(avctx, AV_LOG_WARNING, "Selected DVB subtitles sub-stream %d is not available\n", ctx->substream);
274 ctx->composition_id = AV_RB16(avctx->extradata);
275 ctx->ancillary_id = AV_RB16(avctx->extradata + 2);
280 ctx->prev_start = AV_NOPTS_VALUE;
282 default_clut.id = -1;
283 default_clut.next = NULL;
285 default_clut.clut4[0] = RGBA( 0, 0, 0, 0);
286 default_clut.clut4[1] = RGBA(255, 255, 255, 255);
287 default_clut.clut4[2] = RGBA( 0, 0, 0, 255);
288 default_clut.clut4[3] = RGBA(127, 127, 127, 255);
290 default_clut.clut16[0] = RGBA( 0, 0, 0, 0);
291 for (i = 1; i < 16; i++) {
293 r = (i & 1) ? 255 : 0;
294 g = (i & 2) ? 255 : 0;
295 b = (i & 4) ? 255 : 0;
297 r = (i & 1) ? 127 : 0;
298 g = (i & 2) ? 127 : 0;
299 b = (i & 4) ? 127 : 0;
301 default_clut.clut16[i] = RGBA(r, g, b, 255);
304 default_clut.clut256[0] = RGBA( 0, 0, 0, 0);
305 for (i = 1; i < 256; i++) {
307 r = (i & 1) ? 255 : 0;
308 g = (i & 2) ? 255 : 0;
309 b = (i & 4) ? 255 : 0;
314 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
315 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
316 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
320 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
321 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
322 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
326 r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
327 g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
328 b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
332 r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
333 g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
334 b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
339 default_clut.clut256[i] = RGBA(r, g, b, a);
345 static av_cold int dvbsub_close_decoder(AVCodecContext *avctx)
347 DVBSubContext *ctx = avctx->priv_data;
348 DVBSubRegionDisplay *display;
356 av_freep(&ctx->display_definition);
358 while (ctx->display_list) {
359 display = ctx->display_list;
360 ctx->display_list = display->next;
368 static int dvbsub_read_2bit_string(AVCodecContext *avctx,
369 uint8_t *destbuf, int dbuf_len,
370 const uint8_t **srcbuf, int buf_size,
371 int non_mod, uint8_t *map_table, int x_pos)
377 int pixels_read = x_pos;
379 init_get_bits(&gb, *srcbuf, buf_size << 3);
383 while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
384 bits = get_bits(&gb, 2);
387 if (non_mod != 1 || bits != 1) {
389 *destbuf++ = map_table[bits];
395 bits = get_bits1(&gb);
397 run_length = get_bits(&gb, 3) + 3;
398 bits = get_bits(&gb, 2);
400 if (non_mod == 1 && bits == 1)
401 pixels_read += run_length;
404 bits = map_table[bits];
405 while (run_length-- > 0 && pixels_read < dbuf_len) {
411 bits = get_bits1(&gb);
413 bits = get_bits(&gb, 2);
415 run_length = get_bits(&gb, 4) + 12;
416 bits = get_bits(&gb, 2);
418 if (non_mod == 1 && bits == 1)
419 pixels_read += run_length;
422 bits = map_table[bits];
423 while (run_length-- > 0 && pixels_read < dbuf_len) {
428 } else if (bits == 3) {
429 run_length = get_bits(&gb, 8) + 29;
430 bits = get_bits(&gb, 2);
432 if (non_mod == 1 && bits == 1)
433 pixels_read += run_length;
436 bits = map_table[bits];
437 while (run_length-- > 0 && pixels_read < dbuf_len) {
442 } else if (bits == 1) {
448 while (run_length-- > 0 && pixels_read < dbuf_len) {
453 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
468 if (get_bits(&gb, 6))
469 av_log(avctx, AV_LOG_ERROR, "line overflow\n");
471 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
476 static int dvbsub_read_4bit_string(AVCodecContext *avctx, uint8_t *destbuf, int dbuf_len,
477 const uint8_t **srcbuf, int buf_size,
478 int non_mod, uint8_t *map_table, int x_pos)
484 int pixels_read = x_pos;
486 init_get_bits(&gb, *srcbuf, buf_size << 3);
490 while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
491 bits = get_bits(&gb, 4);
494 if (non_mod != 1 || bits != 1) {
496 *destbuf++ = map_table[bits];
502 bits = get_bits1(&gb);
504 run_length = get_bits(&gb, 3);
506 if (run_length == 0) {
507 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
518 while (run_length-- > 0 && pixels_read < dbuf_len) {
523 bits = get_bits1(&gb);
525 run_length = get_bits(&gb, 2) + 4;
526 bits = get_bits(&gb, 4);
528 if (non_mod == 1 && bits == 1)
529 pixels_read += run_length;
532 bits = map_table[bits];
533 while (run_length-- > 0 && pixels_read < dbuf_len) {
539 bits = get_bits(&gb, 2);
541 run_length = get_bits(&gb, 4) + 9;
542 bits = get_bits(&gb, 4);
544 if (non_mod == 1 && bits == 1)
545 pixels_read += run_length;
548 bits = map_table[bits];
549 while (run_length-- > 0 && pixels_read < dbuf_len) {
554 } else if (bits == 3) {
555 run_length = get_bits(&gb, 8) + 25;
556 bits = get_bits(&gb, 4);
558 if (non_mod == 1 && bits == 1)
559 pixels_read += run_length;
562 bits = map_table[bits];
563 while (run_length-- > 0 && pixels_read < dbuf_len) {
568 } else if (bits == 1) {
574 while (run_length-- > 0 && pixels_read < dbuf_len) {
591 if (get_bits(&gb, 8))
592 av_log(avctx, AV_LOG_ERROR, "line overflow\n");
594 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
599 static int dvbsub_read_8bit_string(AVCodecContext *avctx,
600 uint8_t *destbuf, int dbuf_len,
601 const uint8_t **srcbuf, int buf_size,
602 int non_mod, uint8_t *map_table, int x_pos)
604 const uint8_t *sbuf_end = (*srcbuf) + buf_size;
607 int pixels_read = x_pos;
611 while (*srcbuf < sbuf_end && pixels_read < dbuf_len) {
615 if (non_mod != 1 || bits != 1) {
617 *destbuf++ = map_table[bits];
624 run_length = bits & 0x7f;
625 if ((bits & 0x80) == 0) {
626 if (run_length == 0) {
634 if (non_mod == 1 && bits == 1)
635 pixels_read += run_length;
638 bits = map_table[bits];
639 while (run_length-- > 0 && pixels_read < dbuf_len) {
648 av_log(avctx, AV_LOG_ERROR, "line overflow\n");
653 static void compute_default_clut(uint8_t *clut, AVSubtitleRect *rect, int w, int h)
655 uint8_t list[256] = {0};
656 uint8_t list_inv[256];
657 int counttab[256] = {0};
659 ptrdiff_t stride = rect->linesize[0];
660 #define V(x,y) rect->data[0][(x) + (y)*stride]
661 for (y = 0; y<h; y++) {
662 for (x = 0; x<w; x++) {
664 int vl = x ? V(x-1,y) + 1 : 0;
665 int vr = x+1<w ? V(x+1,y) + 1 : 0;
666 int vt = y ? V(x,y-1) + 1 : 0;
667 int vb = y+1<h ? V(x,y+1) + 1 : 0;
668 counttab[v-1] += !!((v!=vl) + (v!=vr) + (v!=vt) + (v!=vb));
671 #define L(x,y) list[d[(x) + (y)*stride]]
673 for (i = 0; i<256; i++) {
674 int scoretab[256] = {0};
677 for (y = 0; y<h; y++) {
678 for (x = 0; x<w; x++) {
679 uint8_t *d = &rect->data[0][x + y*stride];
682 int l_l = x ? L(-1, 0) : 1;
683 int l_r = x+1<w ? L( 1, 0) : 1;
684 int l_t = y ? L( 0,-1) : 1;
685 int l_b = y+1<h ? L( 0, 1) : 1;
688 scoretab[v] += l_l + l_r + l_t + l_b;
691 for (x = 0; x < 256; x++) {
693 int score = 1024LL*scoretab[x] / counttab[x];
694 if (score > bestscore) {
703 list_inv[ i ] = bestv;
706 count = FFMAX(i - 1, 1);
707 for (i--; i>=0; i--) {
709 AV_WN32(clut + 4*list_inv[i], RGBA(v/2,v,v/2,v));
714 static int save_subtitle_set(AVCodecContext *avctx, AVSubtitle *sub, int *got_output)
716 DVBSubContext *ctx = avctx->priv_data;
717 DVBSubRegionDisplay *display;
718 DVBSubDisplayDefinition *display_def = ctx->display_definition;
719 DVBSubRegion *region;
720 AVSubtitleRect *rect;
722 uint32_t *clut_table;
724 int offset_x=0, offset_y=0;
729 offset_x = display_def->x;
730 offset_y = display_def->y;
733 /* Not touching AVSubtitles again*/
735 avpriv_request_sample(ctx, "Different Version of Segment asked Twice");
736 return AVERROR_PATCHWELCOME;
738 for (display = ctx->display_list; display; display = display->next) {
739 region = get_region(ctx, display->region_id);
740 if (region && region->dirty)
744 if(ctx->compute_edt == 0) {
745 sub->end_display_time = ctx->time_out * 1000;
747 } else if (ctx->prev_start != AV_NOPTS_VALUE) {
748 sub->end_display_time = av_rescale_q((sub->pts - ctx->prev_start ), AV_TIME_BASE_Q, (AVRational){ 1, 1000 }) - 1;
751 if (sub->num_rects > 0) {
753 sub->rects = av_mallocz_array(sizeof(*sub->rects), sub->num_rects);
755 ret = AVERROR(ENOMEM);
759 for (i = 0; i < sub->num_rects; i++) {
760 sub->rects[i] = av_mallocz(sizeof(*sub->rects[i]));
761 if (!sub->rects[i]) {
762 ret = AVERROR(ENOMEM);
769 for (display = ctx->display_list; display; display = display->next) {
770 region = get_region(ctx, display->region_id);
778 rect = sub->rects[i];
779 rect->x = display->x_pos + offset_x;
780 rect->y = display->y_pos + offset_y;
781 rect->w = region->width;
782 rect->h = region->height;
783 rect->nb_colors = (1 << region->depth);
784 rect->type = SUBTITLE_BITMAP;
785 rect->linesize[0] = region->width;
787 clut = get_clut(ctx, region->clut);
790 clut = &default_clut;
792 switch (region->depth) {
794 clut_table = clut->clut4;
797 clut_table = clut->clut256;
801 clut_table = clut->clut16;
805 rect->data[1] = av_mallocz(AVPALETTE_SIZE);
806 if (!rect->data[1]) {
807 ret = AVERROR(ENOMEM);
810 memcpy(rect->data[1], clut_table, (1 << region->depth) * sizeof(uint32_t));
812 rect->data[0] = av_malloc(region->buf_size);
813 if (!rect->data[0]) {
814 ret = AVERROR(ENOMEM);
818 memcpy(rect->data[0], region->pbuf, region->buf_size);
820 if ((clut == &default_clut && ctx->compute_clut == -1) || ctx->compute_clut == 1) {
821 if (!region->has_computed_clut) {
822 compute_default_clut(region->computed_clut, rect, rect->w, rect->h);
823 region->has_computed_clut = 1;
826 memcpy(rect->data[1], region->computed_clut, sizeof(region->computed_clut));
830 FF_DISABLE_DEPRECATION_WARNINGS
833 for (j = 0; j < 4; j++) {
834 rect->pict.data[j] = rect->data[j];
835 rect->pict.linesize[j] = rect->linesize[j];
838 FF_ENABLE_DEPRECATION_WARNINGS
848 for(i=0; i<sub->num_rects; i++) {
849 rect = sub->rects[i];
851 av_freep(&rect->data[0]);
852 av_freep(&rect->data[1]);
854 av_freep(&sub->rects[i]);
856 av_freep(&sub->rects);
862 static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
863 const uint8_t *buf, int buf_size, int top_bottom, int non_mod)
865 DVBSubContext *ctx = avctx->priv_data;
867 DVBSubRegion *region = get_region(ctx, display->region_id);
868 const uint8_t *buf_end = buf + buf_size;
873 uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
874 uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
875 uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
876 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
880 ff_dlog(avctx, "DVB pixel block size %d, %s field:\n", buf_size,
881 top_bottom ? "bottom" : "top");
883 for (i = 0; i < buf_size; i++) {
885 ff_dlog(avctx, "0x%8p: ", buf+i);
887 ff_dlog(avctx, "%02x ", buf[i]);
889 ff_dlog(avctx, "\n");
893 ff_dlog(avctx, "\n");
902 x_pos = display->x_pos;
903 y_pos = display->y_pos;
907 while (buf < buf_end) {
908 if ((*buf!=0xf0 && x_pos >= region->width) || y_pos >= region->height) {
909 av_log(avctx, AV_LOG_ERROR, "Invalid object location! %d-%d %d-%d %02x\n", x_pos, region->width, y_pos, region->height, *buf);
915 if (region->depth == 8)
917 else if (region->depth == 4)
922 x_pos = dvbsub_read_2bit_string(avctx, pbuf + (y_pos * region->width),
923 region->width, &buf, buf_end - buf,
924 non_mod, map_table, x_pos);
927 if (region->depth < 4) {
928 av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
932 if (region->depth == 8)
937 x_pos = dvbsub_read_4bit_string(avctx, pbuf + (y_pos * region->width),
938 region->width, &buf, buf_end - buf,
939 non_mod, map_table, x_pos);
942 if (region->depth < 8) {
943 av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
947 x_pos = dvbsub_read_8bit_string(avctx, pbuf + (y_pos * region->width),
948 region->width, &buf, buf_end - buf,
949 non_mod, NULL, x_pos);
953 map2to4[0] = (*buf) >> 4;
954 map2to4[1] = (*buf++) & 0xf;
955 map2to4[2] = (*buf) >> 4;
956 map2to4[3] = (*buf++) & 0xf;
959 for (i = 0; i < 4; i++)
963 for (i = 0; i < 16; i++)
968 x_pos = display->x_pos;
972 av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
976 region->has_computed_clut = 0;
979 static int dvbsub_parse_object_segment(AVCodecContext *avctx,
980 const uint8_t *buf, int buf_size)
982 DVBSubContext *ctx = avctx->priv_data;
984 const uint8_t *buf_end = buf + buf_size;
986 DVBSubObject *object;
987 DVBSubObjectDisplay *display;
988 int top_field_len, bottom_field_len;
990 int coding_method, non_modifying_color;
992 object_id = AV_RB16(buf);
995 object = get_object(ctx, object_id);
998 return AVERROR_INVALIDDATA;
1000 coding_method = ((*buf) >> 2) & 3;
1001 non_modifying_color = ((*buf++) >> 1) & 1;
1003 if (coding_method == 0) {
1004 top_field_len = AV_RB16(buf);
1006 bottom_field_len = AV_RB16(buf);
1009 if (buf + top_field_len + bottom_field_len > buf_end) {
1010 av_log(avctx, AV_LOG_ERROR, "Field data size %d+%d too large\n", top_field_len, bottom_field_len);
1011 return AVERROR_INVALIDDATA;
1014 for (display = object->display_list; display; display = display->object_list_next) {
1015 const uint8_t *block = buf;
1016 int bfl = bottom_field_len;
1018 dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
1019 non_modifying_color);
1021 if (bottom_field_len > 0)
1022 block = buf + top_field_len;
1024 bfl = top_field_len;
1026 dvbsub_parse_pixel_data_block(avctx, display, block, bfl, 1,
1027 non_modifying_color);
1030 /* } else if (coding_method == 1) {*/
1033 av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
1039 static int dvbsub_parse_clut_segment(AVCodecContext *avctx,
1040 const uint8_t *buf, int buf_size)
1042 DVBSubContext *ctx = avctx->priv_data;
1044 const uint8_t *buf_end = buf + buf_size;
1048 int entry_id, depth , full_range;
1049 int y, cr, cb, alpha;
1050 int r, g, b, r_add, g_add, b_add;
1052 ff_dlog(avctx, "DVB clut packet:\n");
1054 for (i=0; i < buf_size; i++) {
1055 ff_dlog(avctx, "%02x ", buf[i]);
1057 ff_dlog(avctx, "\n");
1061 ff_dlog(avctx, "\n");
1064 version = ((*buf)>>4)&15;
1067 clut = get_clut(ctx, clut_id);
1070 clut = av_malloc(sizeof(DVBSubCLUT));
1072 return AVERROR(ENOMEM);
1074 memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
1079 clut->next = ctx->clut_list;
1080 ctx->clut_list = clut;
1083 if (clut->version != version) {
1085 clut->version = version;
1087 while (buf + 4 < buf_end) {
1090 depth = (*buf) & 0xe0;
1093 av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
1096 full_range = (*buf++) & 1;
1105 cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
1106 cb = (buf[1] << 2) & 0xf0;
1107 alpha = (buf[1] << 6) & 0xc0;
1115 YUV_TO_RGB1_CCIR(cb, cr);
1116 YUV_TO_RGB2_CCIR(r, g, b, y);
1118 ff_dlog(avctx, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
1119 if (!!(depth & 0x80) + !!(depth & 0x40) + !!(depth & 0x20) > 1) {
1120 ff_dlog(avctx, "More than one bit level marked: %x\n", depth);
1121 if (avctx->strict_std_compliance > FF_COMPLIANCE_NORMAL)
1122 return AVERROR_INVALIDDATA;
1125 if (depth & 0x80 && entry_id < 4)
1126 clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
1127 else if (depth & 0x40 && entry_id < 16)
1128 clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
1129 else if (depth & 0x20)
1130 clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
1138 static int dvbsub_parse_region_segment(AVCodecContext *avctx,
1139 const uint8_t *buf, int buf_size)
1141 DVBSubContext *ctx = avctx->priv_data;
1143 const uint8_t *buf_end = buf + buf_size;
1144 int region_id, object_id;
1145 int av_unused version;
1146 DVBSubRegion *region;
1147 DVBSubObject *object;
1148 DVBSubObjectDisplay *display;
1153 return AVERROR_INVALIDDATA;
1157 region = get_region(ctx, region_id);
1160 region = av_mallocz(sizeof(DVBSubRegion));
1162 return AVERROR(ENOMEM);
1164 region->id = region_id;
1165 region->version = -1;
1167 region->next = ctx->region_list;
1168 ctx->region_list = region;
1171 version = ((*buf)>>4) & 15;
1172 fill = ((*buf++) >> 3) & 1;
1174 region->width = AV_RB16(buf);
1176 region->height = AV_RB16(buf);
1179 ret = av_image_check_size2(region->width, region->height, avctx->max_pixels, AV_PIX_FMT_PAL8, 0, avctx);
1180 if (ret >= 0 && region->width * region->height * 2 > 320 * 1024 * 8) {
1181 ret = AVERROR_INVALIDDATA;
1182 av_log(avctx, AV_LOG_ERROR, "Pixel buffer memory constraint violated\n");
1185 region->width= region->height= 0;
1189 if (region->width * region->height != region->buf_size) {
1190 av_free(region->pbuf);
1192 region->buf_size = region->width * region->height;
1194 region->pbuf = av_malloc(region->buf_size);
1195 if (!region->pbuf) {
1199 return AVERROR(ENOMEM);
1206 region->depth = 1 << (((*buf++) >> 2) & 7);
1207 if(region->depth<2 || region->depth>8){
1208 av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);
1211 region->clut = *buf++;
1213 if (region->depth == 8) {
1214 region->bgcolor = *buf++;
1219 if (region->depth == 4)
1220 region->bgcolor = (((*buf++) >> 4) & 15);
1222 region->bgcolor = (((*buf++) >> 2) & 3);
1225 ff_dlog(avctx, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
1228 memset(region->pbuf, region->bgcolor, region->buf_size);
1229 ff_dlog(avctx, "Fill region (%d)\n", region->bgcolor);
1232 delete_region_display_list(ctx, region);
1234 while (buf + 5 < buf_end) {
1235 object_id = AV_RB16(buf);
1238 object = get_object(ctx, object_id);
1241 object = av_mallocz(sizeof(DVBSubObject));
1243 return AVERROR(ENOMEM);
1245 object->id = object_id;
1246 object->next = ctx->object_list;
1247 ctx->object_list = object;
1250 object->type = (*buf) >> 6;
1252 display = av_mallocz(sizeof(DVBSubObjectDisplay));
1254 return AVERROR(ENOMEM);
1256 display->object_id = object_id;
1257 display->region_id = region_id;
1259 display->x_pos = AV_RB16(buf) & 0xfff;
1261 display->y_pos = AV_RB16(buf) & 0xfff;
1264 if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
1265 display->fgcolor = *buf++;
1266 display->bgcolor = *buf++;
1269 display->region_list_next = region->display_list;
1270 region->display_list = display;
1272 display->object_list_next = object->display_list;
1273 object->display_list = display;
1279 static int dvbsub_parse_page_segment(AVCodecContext *avctx,
1280 const uint8_t *buf, int buf_size, AVSubtitle *sub, int *got_output)
1282 DVBSubContext *ctx = avctx->priv_data;
1283 DVBSubRegionDisplay *display;
1284 DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
1286 const uint8_t *buf_end = buf + buf_size;
1293 return AVERROR_INVALIDDATA;
1296 version = ((*buf)>>4) & 15;
1297 page_state = ((*buf++) >> 2) & 3;
1299 if (ctx->version == version) {
1303 ctx->time_out = timeout;
1304 ctx->version = version;
1306 ff_dlog(avctx, "Page time out %ds, state %d\n", ctx->time_out, page_state);
1308 if(ctx->compute_edt == 1)
1309 save_subtitle_set(avctx, sub, got_output);
1311 if (page_state == 1 || page_state == 2) {
1312 delete_regions(ctx);
1313 delete_objects(ctx);
1317 tmp_display_list = ctx->display_list;
1318 ctx->display_list = NULL;
1320 while (buf + 5 < buf_end) {
1324 display = ctx->display_list;
1325 while (display && display->region_id != region_id) {
1326 display = display->next;
1329 av_log(avctx, AV_LOG_ERROR, "duplicate region\n");
1333 display = tmp_display_list;
1334 tmp_ptr = &tmp_display_list;
1336 while (display && display->region_id != region_id) {
1337 tmp_ptr = &display->next;
1338 display = display->next;
1342 display = av_mallocz(sizeof(DVBSubRegionDisplay));
1344 return AVERROR(ENOMEM);
1347 display->region_id = region_id;
1349 display->x_pos = AV_RB16(buf);
1351 display->y_pos = AV_RB16(buf);
1354 *tmp_ptr = display->next;
1356 display->next = ctx->display_list;
1357 ctx->display_list = display;
1359 ff_dlog(avctx, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
1362 while (tmp_display_list) {
1363 display = tmp_display_list;
1365 tmp_display_list = display->next;
1375 static void png_save(DVBSubContext *ctx, const char *filename, uint32_t *bitmap, int w, int h)
1379 char fname[40], fname2[40];
1382 snprintf(fname, sizeof(fname), "%s.ppm", filename);
1384 f = fopen(fname, "w");
1393 for(y = 0; y < h; y++) {
1394 for(x = 0; x < w; x++) {
1395 v = bitmap[y * w + x];
1396 putc((v >> 16) & 0xff, f);
1397 putc((v >> 8) & 0xff, f);
1398 putc((v >> 0) & 0xff, f);
1404 snprintf(fname2, sizeof(fname2), "%s-a.pgm", filename);
1406 f = fopen(fname2, "w");
1415 for(y = 0; y < h; y++) {
1416 for(x = 0; x < w; x++) {
1417 v = bitmap[y * w + x];
1418 putc((v >> 24) & 0xff, f);
1423 snprintf(command, sizeof(command), "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
1424 if (system(command) != 0) {
1425 av_log(ctx, AV_LOG_ERROR, "Error running pnmtopng\n");
1429 snprintf(command, sizeof(command), "rm %s %s", fname, fname2);
1430 if (system(command) != 0) {
1431 av_log(ctx, AV_LOG_ERROR, "Error removing %s and %s\n", fname, fname2);
1436 static int save_display_set(DVBSubContext *ctx)
1438 DVBSubRegion *region;
1439 DVBSubRegionDisplay *display;
1441 uint32_t *clut_table;
1442 int x_pos, y_pos, width, height;
1443 int x, y, y_off, x_off;
1446 static int fileno_index = 0;
1453 for (display = ctx->display_list; display; display = display->next) {
1454 region = get_region(ctx, display->region_id);
1460 x_pos = display->x_pos;
1461 y_pos = display->y_pos;
1462 width = region->width;
1463 height = region->height;
1465 if (display->x_pos < x_pos) {
1466 width += (x_pos - display->x_pos);
1467 x_pos = display->x_pos;
1470 if (display->y_pos < y_pos) {
1471 height += (y_pos - display->y_pos);
1472 y_pos = display->y_pos;
1475 if (display->x_pos + region->width > x_pos + width) {
1476 width = display->x_pos + region->width - x_pos;
1479 if (display->y_pos + region->height > y_pos + height) {
1480 height = display->y_pos + region->height - y_pos;
1487 pbuf = av_malloc(width * height * 4);
1491 for (display = ctx->display_list; display; display = display->next) {
1492 region = get_region(ctx, display->region_id);
1497 x_off = display->x_pos - x_pos;
1498 y_off = display->y_pos - y_pos;
1500 clut = get_clut(ctx, region->clut);
1503 clut = &default_clut;
1505 switch (region->depth) {
1507 clut_table = clut->clut4;
1510 clut_table = clut->clut256;
1514 clut_table = clut->clut16;
1518 for (y = 0; y < region->height; y++) {
1519 for (x = 0; x < region->width; x++) {
1520 pbuf[((y + y_off) * width) + x_off + x] =
1521 clut_table[region->pbuf[y * region->width + x]];
1527 snprintf(filename, sizeof(filename), "dvbs.%d", fileno_index);
1529 png_save(ctx, filename, pbuf, width, height);
1539 static int dvbsub_parse_display_definition_segment(AVCodecContext *avctx,
1543 DVBSubContext *ctx = avctx->priv_data;
1544 DVBSubDisplayDefinition *display_def = ctx->display_definition;
1545 int dds_version, info_byte;
1548 return AVERROR_INVALIDDATA;
1550 info_byte = bytestream_get_byte(&buf);
1551 dds_version = info_byte >> 4;
1552 if (display_def && display_def->version == dds_version)
1553 return 0; // already have this display definition version
1556 display_def = av_mallocz(sizeof(*display_def));
1558 return AVERROR(ENOMEM);
1559 ctx->display_definition = display_def;
1562 display_def->version = dds_version;
1565 display_def->width = bytestream_get_be16(&buf) + 1;
1566 display_def->height = bytestream_get_be16(&buf) + 1;
1567 if (!avctx->width || !avctx->height) {
1568 avctx->width = display_def->width;
1569 avctx->height = display_def->height;
1572 if (info_byte & 1<<3) { // display_window_flag
1574 return AVERROR_INVALIDDATA;
1576 display_def->x = bytestream_get_be16(&buf);
1577 display_def->width = bytestream_get_be16(&buf) - display_def->x + 1;
1578 display_def->y = bytestream_get_be16(&buf);
1579 display_def->height = bytestream_get_be16(&buf) - display_def->y + 1;
1585 static int dvbsub_display_end_segment(AVCodecContext *avctx, const uint8_t *buf,
1586 int buf_size, AVSubtitle *sub,int *got_output)
1588 DVBSubContext *ctx = avctx->priv_data;
1590 if(ctx->compute_edt == 0)
1591 save_subtitle_set(avctx, sub, got_output);
1593 save_display_set(ctx);
1598 static int dvbsub_decode(AVCodecContext *avctx,
1599 void *data, int *data_size,
1602 const uint8_t *buf = avpkt->data;
1603 int buf_size = avpkt->size;
1604 DVBSubContext *ctx = avctx->priv_data;
1605 AVSubtitle *sub = data;
1606 const uint8_t *p, *p_end;
1612 int got_segment = 0;
1615 ff_dlog(avctx, "DVB sub packet:\n");
1617 for (i=0; i < buf_size; i++) {
1618 ff_dlog(avctx, "%02x ", buf[i]);
1620 ff_dlog(avctx, "\n");
1624 ff_dlog(avctx, "\n");
1626 if (buf_size <= 6 || *buf != 0x0f) {
1627 ff_dlog(avctx, "incomplete or broken packet");
1628 return AVERROR_INVALIDDATA;
1632 p_end = buf + buf_size;
1634 while (p_end - p >= 6 && *p == 0x0f) {
1636 segment_type = *p++;
1637 page_id = AV_RB16(p);
1639 segment_length = AV_RB16(p);
1642 if (avctx->debug & FF_DEBUG_STARTCODE) {
1643 av_log(avctx, AV_LOG_DEBUG, "segment_type:%d page_id:%d segment_length:%d\n", segment_type, page_id, segment_length);
1646 if (p_end - p < segment_length) {
1647 ff_dlog(avctx, "incomplete or broken packet");
1652 if (page_id == ctx->composition_id || page_id == ctx->ancillary_id ||
1653 ctx->composition_id == -1 || ctx->ancillary_id == -1) {
1655 switch (segment_type) {
1656 case DVBSUB_PAGE_SEGMENT:
1657 ret = dvbsub_parse_page_segment(avctx, p, segment_length, sub, data_size);
1660 case DVBSUB_REGION_SEGMENT:
1661 ret = dvbsub_parse_region_segment(avctx, p, segment_length);
1664 case DVBSUB_CLUT_SEGMENT:
1665 ret = dvbsub_parse_clut_segment(avctx, p, segment_length);
1666 if (ret < 0) goto end;
1669 case DVBSUB_OBJECT_SEGMENT:
1670 ret = dvbsub_parse_object_segment(avctx, p, segment_length);
1673 case DVBSUB_DISPLAYDEFINITION_SEGMENT:
1674 ret = dvbsub_parse_display_definition_segment(avctx, p,
1678 case DVBSUB_DISPLAY_SEGMENT:
1679 ret = dvbsub_display_end_segment(avctx, p, segment_length, sub, data_size);
1680 if (got_segment == 15 && !got_dds && !avctx->width && !avctx->height) {
1681 // Default from ETSI EN 300 743 V1.3.1 (7.2.1)
1683 avctx->height = 576;
1688 ff_dlog(avctx, "Subtitling segment type 0x%x, page id %d, length %d\n",
1689 segment_type, page_id, segment_length);
1696 p += segment_length;
1698 // Some streams do not send a display segment but if we have all the other
1699 // segments then we need no further data.
1700 if (got_segment == 15) {
1701 av_log(avctx, AV_LOG_DEBUG, "Missing display_end_segment, emulating\n");
1702 dvbsub_display_end_segment(avctx, p, 0, sub, data_size);
1708 avsubtitle_free(sub);
1711 if(ctx->compute_edt == 1 )
1712 FFSWAP(int64_t, ctx->prev_start, sub->pts);
1718 #define DS AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_SUBTITLE_PARAM
1719 static const AVOption options[] = {
1720 {"compute_edt", "compute end of time using pts or timeout", offsetof(DVBSubContext, compute_edt), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DS},
1721 {"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},
1722 {"dvb_substream", "", offsetof(DVBSubContext, substream), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 63, DS},
1725 static const AVClass dvbsubdec_class = {
1726 .class_name = "DVB Sub Decoder",
1727 .item_name = av_default_item_name,
1729 .version = LIBAVUTIL_VERSION_INT,
1732 AVCodec ff_dvbsub_decoder = {
1734 .long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),
1735 .type = AVMEDIA_TYPE_SUBTITLE,
1736 .id = AV_CODEC_ID_DVB_SUBTITLE,
1737 .priv_data_size = sizeof(DVBSubContext),
1738 .init = dvbsub_init_decoder,
1739 .close = dvbsub_close_decoder,
1740 .decode = dvbsub_decode,
1741 .priv_class = &dvbsubdec_class,