2 * DVB subtitle decoding for ffmpeg
3 * Copyright (c) 2005 Ian Caulfield.
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "bitstream.h"
26 //#define DEBUG_PACKET_CONTENTS
27 //#define DEBUG_SAVE_IMAGES
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_DISPLAY_SEGMENT 0x80
35 static unsigned char *cm;
37 #ifdef DEBUG_SAVE_IMAGES
40 static void png_save(const char *filename, uint8_t *bitmap, int w, int h,
41 uint32_t *rgba_palette)
45 char fname[40], fname2[40];
48 snprintf(fname, 40, "%s.ppm", filename);
50 f = fopen(fname, "w");
59 for(y = 0; y < h; y++) {
60 for(x = 0; x < w; x++) {
61 v = rgba_palette[bitmap[y * w + x]];
62 putc((v >> 16) & 0xff, f);
63 putc((v >> 8) & 0xff, f);
64 putc((v >> 0) & 0xff, f);
70 snprintf(fname2, 40, "%s-a.pgm", filename);
72 f = fopen(fname2, "w");
81 for(y = 0; y < h; y++) {
82 for(x = 0; x < w; x++) {
83 v = rgba_palette[bitmap[y * w + x]];
84 putc((v >> 24) & 0xff, f);
89 snprintf(command, 1024, "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
92 snprintf(command, 1024, "rm %s %s", fname, fname2);
97 static void png_save2(const char *filename, uint32_t *bitmap, int w, int h)
101 char fname[40], fname2[40];
104 snprintf(fname, 40, "%s.ppm", filename);
106 f = fopen(fname, "w");
115 for(y = 0; y < h; y++) {
116 for(x = 0; x < w; x++) {
117 v = bitmap[y * w + x];
118 putc((v >> 16) & 0xff, f);
119 putc((v >> 8) & 0xff, f);
120 putc((v >> 0) & 0xff, f);
126 snprintf(fname2, 40, "%s-a.pgm", filename);
128 f = fopen(fname2, "w");
137 for(y = 0; y < h; y++) {
138 for(x = 0; x < w; x++) {
139 v = bitmap[y * w + x];
140 putc((v >> 24) & 0xff, f);
145 snprintf(command, 1024, "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
148 snprintf(command, 1024, "rm %s %s", fname, fname2);
153 #define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
155 typedef struct DVBSubCLUT {
160 uint32_t clut256[256];
162 struct DVBSubCLUT *next;
165 static DVBSubCLUT default_clut;
167 typedef struct DVBSubObjectDisplay {
177 struct DVBSubObjectDisplay *region_list_next;
178 struct DVBSubObjectDisplay *object_list_next;
179 } DVBSubObjectDisplay;
181 typedef struct DVBSubObject {
186 DVBSubObjectDisplay *display_list;
188 struct DVBSubObject *next;
191 typedef struct DVBSubRegionDisplay {
197 struct DVBSubRegionDisplay *next;
198 } DVBSubRegionDisplay;
200 typedef struct DVBSubRegion {
213 DVBSubObjectDisplay *display_list;
215 struct DVBSubRegion *next;
218 typedef struct DVBSubContext {
223 DVBSubRegion *region_list;
224 DVBSubCLUT *clut_list;
225 DVBSubObject *object_list;
227 int display_list_size;
228 DVBSubRegionDisplay *display_list;
232 static DVBSubObject* get_object(DVBSubContext *ctx, int object_id)
234 DVBSubObject *ptr = ctx->object_list;
236 while (ptr != NULL && ptr->id != object_id) {
243 static DVBSubCLUT* get_clut(DVBSubContext *ctx, int clut_id)
245 DVBSubCLUT *ptr = ctx->clut_list;
247 while (ptr != NULL && ptr->id != clut_id) {
254 static DVBSubRegion* get_region(DVBSubContext *ctx, int region_id)
256 DVBSubRegion *ptr = ctx->region_list;
258 while (ptr != NULL && ptr->id != region_id) {
265 static void delete_region_display_list(DVBSubContext *ctx, DVBSubRegion *region)
267 DVBSubObject *object, *obj2, **obj2_ptr;
268 DVBSubObjectDisplay *display, *obj_disp, **obj_disp_ptr;
270 while (region->display_list != NULL) {
271 display = region->display_list;
273 object = get_object(ctx, display->object_id);
275 if (object != NULL) {
276 obj_disp = object->display_list;
277 obj_disp_ptr = &object->display_list;
279 while (obj_disp != NULL && obj_disp != display) {
280 obj_disp_ptr = &obj_disp->object_list_next;
281 obj_disp = obj_disp->object_list_next;
285 *obj_disp_ptr = obj_disp->object_list_next;
287 if (object->display_list == NULL) {
288 obj2 = ctx->object_list;
289 obj2_ptr = &ctx->object_list;
291 while (obj2 != NULL && obj2 != object) {
292 obj2_ptr = &obj2->next;
296 *obj2_ptr = obj2->next;
303 region->display_list = display->region_list_next;
310 static void delete_state(DVBSubContext *ctx)
312 DVBSubRegion *region;
315 while (ctx->region_list != NULL)
317 region = ctx->region_list;
319 ctx->region_list = region->next;
321 delete_region_display_list(ctx, region);
322 if (region->pbuf != NULL)
323 av_free(region->pbuf);
328 while (ctx->clut_list != NULL)
330 clut = ctx->clut_list;
332 ctx->clut_list = clut->next;
337 /* Should already be null */
338 if (ctx->object_list != NULL)
339 av_log(0, AV_LOG_ERROR, "Memory deallocation error!\n");
342 static int dvbsub_init_decoder(AVCodecContext *avctx)
344 int i, r, g, b, a = 0;
345 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
347 cm = ff_cropTbl + MAX_NEG_CROP;
349 memset(avctx->priv_data, 0, sizeof(DVBSubContext));
351 ctx->composition_id = avctx->sub_id & 0xffff;
352 ctx->ancillary_id = avctx->sub_id >> 16;
354 default_clut.id = -1;
355 default_clut.next = NULL;
357 default_clut.clut4[0] = RGBA( 0, 0, 0, 0);
358 default_clut.clut4[1] = RGBA(255, 255, 255, 255);
359 default_clut.clut4[2] = RGBA( 0, 0, 0, 255);
360 default_clut.clut4[3] = RGBA(127, 127, 127, 255);
362 default_clut.clut16[0] = RGBA( 0, 0, 0, 0);
363 for (i = 1; i < 16; i++) {
365 r = (i & 1) ? 255 : 0;
366 g = (i & 2) ? 255 : 0;
367 b = (i & 4) ? 255 : 0;
369 r = (i & 1) ? 127 : 0;
370 g = (i & 2) ? 127 : 0;
371 b = (i & 4) ? 127 : 0;
373 default_clut.clut16[i] = RGBA(r, g, b, 255);
376 default_clut.clut256[0] = RGBA( 0, 0, 0, 0);
377 for (i = 1; i < 256; i++) {
379 r = (i & 1) ? 255 : 0;
380 g = (i & 2) ? 255 : 0;
381 b = (i & 4) ? 255 : 0;
386 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
387 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
388 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
392 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
393 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
394 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
398 r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
399 g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
400 b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
404 r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
405 g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
406 b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
411 default_clut.clut256[i] = RGBA(r, g, b, a);
417 static int dvbsub_close_decoder(AVCodecContext *avctx)
419 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
420 DVBSubRegionDisplay *display;
424 while (ctx->display_list != NULL)
426 display = ctx->display_list;
427 ctx->display_list = display->next;
435 static int dvbsub_read_2bit_string(uint8_t *destbuf, int dbuf_len,
436 uint8_t **srcbuf, int buf_size,
437 int non_mod, uint8_t *map_table)
445 init_get_bits(&gb, *srcbuf, buf_size << 8);
447 while (get_bits_count(&gb) < (buf_size << 8) && pixels_read < dbuf_len) {
448 bits = get_bits(&gb, 2);
451 if (non_mod != 1 || bits != 1) {
452 if (map_table != NULL)
453 *destbuf++ = map_table[bits];
459 bits = get_bits(&gb, 1);
461 run_length = get_bits(&gb, 3) + 3;
462 bits = get_bits(&gb, 2);
464 if (non_mod == 1 && bits == 1)
465 pixels_read += run_length;
467 if (map_table != NULL)
468 bits = map_table[bits];
469 while (run_length-- > 0 && pixels_read < dbuf_len) {
475 bits = get_bits(&gb, 1);
477 bits = get_bits(&gb, 2);
479 run_length = get_bits(&gb, 4) + 12;
480 bits = get_bits(&gb, 2);
482 if (non_mod == 1 && bits == 1)
483 pixels_read += run_length;
485 if (map_table != NULL)
486 bits = map_table[bits];
487 while (run_length-- > 0 && pixels_read < dbuf_len) {
492 } else if (bits == 3) {
493 run_length = get_bits(&gb, 8) + 29;
494 bits = get_bits(&gb, 2);
496 if (non_mod == 1 && bits == 1)
497 pixels_read += run_length;
499 if (map_table != NULL)
500 bits = map_table[bits];
501 while (run_length-- > 0 && pixels_read < dbuf_len) {
506 } else if (bits == 1) {
508 if (map_table != NULL)
512 if (pixels_read <= dbuf_len) {
517 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
521 if (map_table != NULL)
532 if (get_bits(&gb, 6) != 0)
533 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
535 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
540 static int dvbsub_read_4bit_string(uint8_t *destbuf, int dbuf_len,
541 uint8_t **srcbuf, int buf_size,
542 int non_mod, uint8_t *map_table)
550 init_get_bits(&gb, *srcbuf, buf_size << 8);
552 while (get_bits_count(&gb) < (buf_size << 8) && pixels_read < dbuf_len) {
553 bits = get_bits(&gb, 4);
556 if (non_mod != 1 || bits != 1) {
557 if (map_table != NULL)
558 *destbuf++ = map_table[bits];
564 bits = get_bits(&gb, 1);
566 run_length = get_bits(&gb, 3);
568 if (run_length == 0) {
569 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
575 if (map_table != NULL)
580 while (run_length-- > 0 && pixels_read < dbuf_len) {
585 bits = get_bits(&gb, 1);
587 run_length = get_bits(&gb, 2) + 4;
588 bits = get_bits(&gb, 4);
590 if (non_mod == 1 && bits == 1)
591 pixels_read += run_length;
593 if (map_table != NULL)
594 bits = map_table[bits];
595 while (run_length-- > 0 && pixels_read < dbuf_len) {
601 bits = get_bits(&gb, 2);
603 run_length = get_bits(&gb, 4) + 9;
604 bits = get_bits(&gb, 4);
606 if (non_mod == 1 && bits == 1)
607 pixels_read += run_length;
609 if (map_table != NULL)
610 bits = map_table[bits];
611 while (run_length-- > 0 && pixels_read < dbuf_len) {
616 } else if (bits == 3) {
617 run_length = get_bits(&gb, 8) + 25;
618 bits = get_bits(&gb, 4);
620 if (non_mod == 1 && bits == 1)
621 pixels_read += run_length;
623 if (map_table != NULL)
624 bits = map_table[bits];
625 while (run_length-- > 0 && pixels_read < dbuf_len) {
630 } else if (bits == 1) {
632 if (map_table != NULL)
636 if (pixels_read <= dbuf_len) {
641 if (map_table != NULL)
653 if (get_bits(&gb, 8) != 0)
654 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
656 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
661 static int dvbsub_read_8bit_string(uint8_t *destbuf, int dbuf_len,
662 uint8_t **srcbuf, int buf_size,
663 int non_mod, uint8_t *map_table)
665 uint8_t *sbuf_end = (*srcbuf) + buf_size;
670 while (*srcbuf < sbuf_end && pixels_read < dbuf_len) {
674 if (non_mod != 1 || bits != 1) {
675 if (map_table != NULL)
676 *destbuf++ = map_table[bits];
683 run_length = bits & 0x7f;
684 if ((bits & 0x80) == 0) {
685 if (run_length == 0) {
689 if (map_table != NULL)
693 while (run_length-- > 0 && pixels_read < dbuf_len) {
700 if (non_mod == 1 && bits == 1)
701 pixels_read += run_length;
702 if (map_table != NULL)
703 bits = map_table[bits];
704 else while (run_length-- > 0 && pixels_read < dbuf_len) {
712 if (*(*srcbuf)++ != 0)
713 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
720 static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
721 uint8_t *buf, int buf_size, int top_bottom, int non_mod)
723 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
725 DVBSubRegion *region = get_region(ctx, display->region_id);
726 uint8_t *buf_end = buf + buf_size;
731 uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
732 uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
733 uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
734 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
738 av_log(avctx, AV_LOG_INFO, "DVB pixel block size %d, %s field:\n", buf_size,
739 top_bottom ? "bottom" : "top");
742 #ifdef DEBUG_PACKET_CONTENTS
743 for (i = 0; i < buf_size; i++)
746 av_log(avctx, AV_LOG_INFO, "0x%08p: ", buf+i);
748 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
750 av_log(avctx, AV_LOG_INFO, "\n");
754 av_log(avctx, AV_LOG_INFO, "\n");
763 x_pos = display->x_pos;
764 y_pos = display->y_pos;
766 if ((y_pos & 1) != top_bottom)
769 while (buf < buf_end) {
770 if (x_pos > region->width || y_pos > region->height) {
771 av_log(avctx, AV_LOG_ERROR, "Invalid object location!\n");
777 if (region->depth == 8)
779 else if (region->depth == 4)
784 x_pos += dvbsub_read_2bit_string(pbuf + (y_pos * region->width) + x_pos,
785 region->width - x_pos, &buf, buf_size,
789 if (region->depth < 4) {
790 av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
794 if (region->depth == 8)
799 x_pos += dvbsub_read_4bit_string(pbuf + (y_pos * region->width) + x_pos,
800 region->width - x_pos, &buf, buf_size,
804 if (region->depth < 8) {
805 av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
809 x_pos += dvbsub_read_8bit_string(pbuf + (y_pos * region->width) + x_pos,
810 region->width - x_pos, &buf, buf_size,
815 map2to4[0] = (*buf) >> 4;
816 map2to4[1] = (*buf++) & 0xf;
817 map2to4[2] = (*buf) >> 4;
818 map2to4[3] = (*buf++) & 0xf;
821 for (i = 0; i < 4; i++)
825 for (i = 0; i < 16; i++)
830 x_pos = display->x_pos;
834 av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
840 static void dvbsub_parse_object_segment(AVCodecContext *avctx,
841 uint8_t *buf, int buf_size)
843 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
845 uint8_t *buf_end = buf + buf_size;
848 DVBSubObject *object;
849 DVBSubObjectDisplay *display;
850 int top_field_len, bottom_field_len;
852 int coding_method, non_modifying_colour;
854 object_id = BE_16(buf);
857 object = get_object(ctx, object_id);
862 coding_method = ((*buf) >> 2) & 3;
863 non_modifying_colour = ((*buf++) >> 1) & 1;
865 if (coding_method == 0) {
866 top_field_len = BE_16(buf);
868 bottom_field_len = BE_16(buf);
871 if (buf + top_field_len + bottom_field_len > buf_end) {
872 av_log(avctx, AV_LOG_ERROR, "Field data size too large\n");
876 for (display = object->display_list; display != 0; display = display->object_list_next) {
879 dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
880 non_modifying_colour);
882 if (bottom_field_len > 0)
883 block = buf + top_field_len;
885 bottom_field_len = top_field_len;
887 dvbsub_parse_pixel_data_block(avctx, display, block, bottom_field_len, 1,
888 non_modifying_colour);
891 /* } else if (coding_method == 1) {*/
894 av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
900 #define ONE_HALF (1 << (SCALEBITS - 1))
901 #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
903 #define YUV_TO_RGB1_CCIR(cb1, cr1)\
907 r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
908 g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
910 b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
913 #define YUV_TO_RGB2_CCIR(r, g, b, y1)\
915 y = ((y1) - 16) * FIX(255.0/219.0);\
916 r = cm[(y + r_add) >> SCALEBITS];\
917 g = cm[(y + g_add) >> SCALEBITS];\
918 b = cm[(y + b_add) >> SCALEBITS];\
922 static void dvbsub_parse_clut_segment(AVCodecContext *avctx,
923 uint8_t *buf, int buf_size)
925 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
927 uint8_t *buf_end = buf + buf_size;
930 int entry_id, depth , full_range;
931 int y, cr, cb, alpha;
932 int r, g, b, r_add, g_add, b_add;
934 #ifdef DEBUG_PACKET_CONTENTS
937 av_log(avctx, AV_LOG_INFO, "DVB clut packet:\n");
939 for (i=0; i < buf_size; i++)
941 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
943 av_log(avctx, AV_LOG_INFO, "\n");
947 av_log(avctx, AV_LOG_INFO, "\n");
954 clut = get_clut(ctx, clut_id);
957 clut = av_malloc(sizeof(DVBSubCLUT));
959 memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
963 clut->next = ctx->clut_list;
964 ctx->clut_list = clut;
967 while (buf + 4 < buf_end)
971 depth = (*buf) & 0xe0;
974 av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
978 full_range = (*buf++) & 1;
987 cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
988 cb = (buf[1] << 2) & 0xf0;
989 alpha = (buf[1] << 6) & 0xc0;
997 YUV_TO_RGB1_CCIR(cb, cr);
998 YUV_TO_RGB2_CCIR(r, g, b, y);
1001 av_log(avctx, AV_LOG_INFO, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
1005 clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
1007 clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
1009 clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
1014 static void dvbsub_parse_region_segment(AVCodecContext *avctx,
1015 uint8_t *buf, int buf_size)
1017 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
1019 uint8_t *buf_end = buf + buf_size;
1020 int region_id, object_id;
1021 DVBSubRegion *region;
1022 DVBSubObject *object;
1023 DVBSubObjectDisplay *display;
1031 region = get_region(ctx, region_id);
1035 region = av_mallocz(sizeof(DVBSubRegion));
1037 region->id = region_id;
1039 region->next = ctx->region_list;
1040 ctx->region_list = region;
1043 fill = ((*buf++) >> 3) & 1;
1045 region->width = BE_16(buf);
1047 region->height = BE_16(buf);
1050 if (region->width * region->height != region->buf_size) {
1051 if (region->pbuf != 0)
1052 av_free(region->pbuf);
1054 region->buf_size = region->width * region->height;
1056 region->pbuf = av_malloc(region->buf_size);
1061 region->depth = 1 << (((*buf++) >> 2) & 7);
1062 region->clut = *buf++;
1064 if (region->depth == 8)
1065 region->bgcolour = *buf++;
1069 if (region->depth == 4)
1070 region->bgcolour = (((*buf++) >> 4) & 15);
1072 region->bgcolour = (((*buf++) >> 2) & 3);
1076 av_log(avctx, AV_LOG_INFO, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
1080 memset(region->pbuf, region->bgcolour, region->buf_size);
1082 av_log(avctx, AV_LOG_INFO, "Fill region (%d)\n", region->bgcolour);
1086 delete_region_display_list(ctx, region);
1088 while (buf + 5 < buf_end) {
1089 object_id = BE_16(buf);
1092 object = get_object(ctx, object_id);
1094 if (object == NULL) {
1095 object = av_mallocz(sizeof(DVBSubObject));
1097 object->id = object_id;
1098 object->next = ctx->object_list;
1099 ctx->object_list = object;
1102 object->type = (*buf) >> 6;
1104 display = av_mallocz(sizeof(DVBSubObjectDisplay));
1106 display->object_id = object_id;
1107 display->region_id = region_id;
1109 display->x_pos = BE_16(buf) & 0xfff;
1111 display->y_pos = BE_16(buf) & 0xfff;
1114 if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
1115 display->fgcolour = *buf++;
1116 display->bgcolour = *buf++;
1119 display->region_list_next = region->display_list;
1120 region->display_list = display;
1122 display->object_list_next = object->display_list;
1123 object->display_list = display;
1127 static void dvbsub_parse_page_segment(AVCodecContext *avctx,
1128 uint8_t *buf, int buf_size)
1130 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
1131 DVBSubRegionDisplay *display;
1132 DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
1134 uint8_t *buf_end = buf + buf_size;
1141 ctx->time_out = *buf++;
1142 page_state = ((*buf++) >> 2) & 3;
1145 av_log(avctx, AV_LOG_INFO, "Page time out %ds, state %d\n", ctx->time_out, page_state);
1148 if (page_state == 2)
1153 tmp_display_list = ctx->display_list;
1154 ctx->display_list = NULL;
1155 ctx->display_list_size = 0;
1157 while (buf + 5 < buf_end) {
1161 display = tmp_display_list;
1162 tmp_ptr = &tmp_display_list;
1164 while (display != NULL && display->region_id != region_id) {
1165 tmp_ptr = &display->next;
1166 display = display->next;
1169 if (display == NULL)
1170 display = av_mallocz(sizeof(DVBSubRegionDisplay));
1172 display->region_id = region_id;
1174 display->x_pos = BE_16(buf);
1176 display->y_pos = BE_16(buf);
1179 *tmp_ptr = display->next;
1181 display->next = ctx->display_list;
1182 ctx->display_list = display;
1183 ctx->display_list_size++;
1186 av_log(avctx, AV_LOG_INFO, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
1190 while (tmp_display_list != 0) {
1191 display = tmp_display_list;
1193 tmp_display_list = display->next;
1201 #ifdef DEBUG_SAVE_IMAGES
1202 static void save_display_set(DVBSubContext *ctx)
1204 DVBSubRegion *region;
1205 DVBSubRegionDisplay *display;
1207 uint32_t *clut_table;
1208 int x_pos, y_pos, width, height;
1209 int x, y, y_off, x_off;
1212 static int fileno_index = 0;
1219 for (display = ctx->display_list; display != NULL; display = display->next) {
1220 region = get_region(ctx, display->region_id);
1223 x_pos = display->x_pos;
1224 y_pos = display->y_pos;
1225 width = region->width;
1226 height = region->height;
1228 if (display->x_pos < x_pos) {
1229 width += (x_pos - display->x_pos);
1230 x_pos = display->x_pos;
1233 if (display->y_pos < y_pos) {
1234 height += (y_pos - display->y_pos);
1235 y_pos = display->y_pos;
1238 if (display->x_pos + region->width > x_pos + width) {
1239 width = display->x_pos + region->width - x_pos;
1242 if (display->y_pos + region->height > y_pos + height) {
1243 height = display->y_pos + region->height - y_pos;
1250 pbuf = av_malloc(width * height * 4);
1252 for (display = ctx->display_list; display != NULL; display = display->next) {
1253 region = get_region(ctx, display->region_id);
1255 x_off = display->x_pos - x_pos;
1256 y_off = display->y_pos - y_pos;
1258 clut = get_clut(ctx, region->clut);
1261 clut = &default_clut;
1263 switch (region->depth) {
1265 clut_table = clut->clut4;
1268 clut_table = clut->clut256;
1272 clut_table = clut->clut16;
1276 for (y = 0; y < region->height; y++) {
1277 for (x = 0; x < region->width; x++) {
1278 pbuf[((y + y_off) * width) + x_off + x] =
1279 clut_table[region->pbuf[y * region->width + x]];
1285 snprintf(filename, 32, "dvbs.%d", fileno_index);
1287 png_save2(filename, pbuf, width, height);
1296 static int dvbsub_display_end_segment(AVCodecContext *avctx, uint8_t *buf,
1297 int buf_size, AVSubtitle *sub)
1299 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
1301 DVBSubRegion *region;
1302 DVBSubRegionDisplay *display;
1303 AVSubtitleRect *rect;
1305 uint32_t *clut_table;
1309 sub->start_display_time = 0;
1310 sub->end_display_time = ctx->time_out * 1000;
1313 sub->num_rects = ctx->display_list_size;
1315 if (sub->num_rects > 0)
1316 sub->rects = av_mallocz(sizeof(AVSubtitleRect) * sub->num_rects);
1320 for (display = ctx->display_list; display != NULL; display = display->next) {
1321 region = get_region(ctx, display->region_id);
1322 rect = &sub->rects[i];
1327 rect->x = display->x_pos;
1328 rect->y = display->y_pos;
1329 rect->w = region->width;
1330 rect->h = region->height;
1331 rect->nb_colors = 16;
1332 rect->linesize = region->width;
1334 clut = get_clut(ctx, region->clut);
1337 clut = &default_clut;
1339 switch (region->depth) {
1341 clut_table = clut->clut4;
1344 clut_table = clut->clut256;
1348 clut_table = clut->clut16;
1352 rect->rgba_palette = av_malloc((1 << region->depth) * sizeof(uint32_t));
1353 memcpy(rect->rgba_palette, clut_table, (1 << region->depth) * sizeof(uint32_t));
1355 rect->bitmap = av_malloc(region->buf_size);
1356 memcpy(rect->bitmap, region->pbuf, region->buf_size);
1363 #ifdef DEBUG_SAVE_IMAGES
1364 save_display_set(ctx);
1370 static int dvbsub_decode(AVCodecContext *avctx,
1371 void *data, int *data_size,
1372 uint8_t *buf, int buf_size)
1374 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
1375 AVSubtitle *sub = (AVSubtitle*) data;
1381 #ifdef DEBUG_PACKET_CONTENTS
1384 av_log(avctx, AV_LOG_INFO, "DVB sub packet:\n");
1386 for (i=0; i < buf_size; i++)
1388 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
1390 av_log(avctx, AV_LOG_INFO, "\n");
1394 av_log(avctx, AV_LOG_INFO, "\n");
1402 p_end = buf + buf_size;
1404 while (p < p_end && *p == 0x0f)
1407 segment_type = *p++;
1410 segment_length = BE_16(p);
1413 if (page_id == ctx->composition_id || page_id == ctx->ancillary_id) {
1414 switch (segment_type) {
1415 case DVBSUB_PAGE_SEGMENT:
1416 dvbsub_parse_page_segment(avctx, p, segment_length);
1418 case DVBSUB_REGION_SEGMENT:
1419 dvbsub_parse_region_segment(avctx, p, segment_length);
1421 case DVBSUB_CLUT_SEGMENT:
1422 dvbsub_parse_clut_segment(avctx, p, segment_length);
1424 case DVBSUB_OBJECT_SEGMENT:
1425 dvbsub_parse_object_segment(avctx, p, segment_length);
1427 case DVBSUB_DISPLAY_SEGMENT:
1428 *data_size = dvbsub_display_end_segment(avctx, p, segment_length, sub);
1432 av_log(avctx, AV_LOG_INFO, "Subtitling segment type 0x%x, page id %d, length %d\n",
1433 segment_type, page_id, segment_length);
1439 p += segment_length;
1445 av_log(avctx, AV_LOG_INFO, "Junk at end of packet\n");
1454 AVCodec dvbsub_decoder = {
1456 CODEC_TYPE_SUBTITLE,
1457 CODEC_ID_DVB_SUBTITLE,
1458 sizeof(DVBSubContext),
1459 dvbsub_init_decoder,
1461 dvbsub_close_decoder,
1465 /* Parser (mostly) copied from dvdsub.c */
1467 #define PARSE_BUF_SIZE (65536)
1470 /* parser definition */
1471 typedef struct DVBSubParseContext {
1472 uint8_t *packet_buf;
1476 } DVBSubParseContext;
1478 static int dvbsub_parse_init(AVCodecParserContext *s)
1480 DVBSubParseContext *pc = s->priv_data;
1481 pc->packet_buf = av_malloc(PARSE_BUF_SIZE);
1486 static int dvbsub_parse(AVCodecParserContext *s,
1487 AVCodecContext *avctx,
1488 uint8_t **poutbuf, int *poutbuf_size,
1489 const uint8_t *buf, int buf_size)
1491 DVBSubParseContext *pc = s->priv_data;
1493 int len, buf_pos = 0;
1496 av_log(avctx, AV_LOG_INFO, "DVB parse packet pts=%"PRIx64", lpts=%"PRIx64", cpts=%"PRIx64":\n",
1497 s->pts, s->last_pts, s->cur_frame_pts[s->cur_frame_start_index]);
1500 #ifdef DEBUG_PACKET_CONTENTS
1503 for (i=0; i < buf_size; i++)
1505 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
1507 av_log(avctx, AV_LOG_INFO, "\n");
1511 av_log(avctx, AV_LOG_INFO, "\n");
1518 s->fetch_timestamp = 1;
1520 if (s->last_pts != s->pts && s->last_pts != AV_NOPTS_VALUE) /* Start of a new packet */
1522 if (pc->packet_index != pc->packet_start)
1525 av_log(avctx, AV_LOG_INFO, "Discarding %d bytes\n",
1526 pc->packet_index - pc->packet_start);
1530 pc->packet_start = 0;
1531 pc->packet_index = 0;
1533 if (buf_size < 2 || buf[0] != 0x20 || buf[1] != 0x00) {
1535 av_log(avctx, AV_LOG_INFO, "Bad packet header\n");
1544 if (pc->packet_start != 0)
1546 if (pc->packet_index != pc->packet_start)
1548 memmove(pc->packet_buf, pc->packet_buf + pc->packet_start,
1549 pc->packet_index - pc->packet_start);
1551 pc->packet_index -= pc->packet_start;
1552 pc->packet_start = 0;
1554 pc->packet_start = 0;
1555 pc->packet_index = 0;
1560 if (buf_size - buf_pos + pc->packet_index > PARSE_BUF_SIZE)
1563 /* if not currently in a packet, discard data */
1564 if (pc->in_packet == 0)
1567 memcpy(pc->packet_buf + pc->packet_index, buf + buf_pos, buf_size - buf_pos);
1568 pc->packet_index += buf_size - buf_pos;
1571 p_end = pc->packet_buf + pc->packet_index;
1581 if (p + len + 6 <= p_end)
1583 *poutbuf_size += len + 6;
1590 } else if (*p == 0xff) {
1594 av_log(avctx, AV_LOG_INFO, "Junk at end of packet\n");
1597 pc->packet_index = p - pc->packet_buf;
1601 av_log(avctx, AV_LOG_ERROR, "Junk in packet\n");
1603 pc->packet_index = p - pc->packet_buf;
1609 if (*poutbuf_size > 0)
1611 *poutbuf = pc->packet_buf;
1612 pc->packet_start = *poutbuf_size;
1615 if (s->last_pts == AV_NOPTS_VALUE)
1616 s->last_pts = s->pts;
1621 static void dvbsub_parse_close(AVCodecParserContext *s)
1623 DVBSubParseContext *pc = s->priv_data;
1624 av_freep(&pc->packet_buf);
1627 AVCodecParser dvbsub_parser = {
1628 { CODEC_ID_DVB_SUBTITLE },
1629 sizeof(DVBSubParseContext),