From a6c36197ba929df159a311d17bedd2c3c575f57f Mon Sep 17 00:00:00 2001 From: Anshul Maheshwari Date: Tue, 29 Apr 2014 16:29:19 +0530 Subject: [PATCH] Implementation of DVB Subtitle --- .gitignore | 3 + linux/Makefile | 5 +- src/ccextractor.cpp | 5 + src/ccextractor.h | 4 + src/constants.h | 30 +- src/dvb_subtitle_decoder.c | 1643 ++++++++++++++++++++++++++++++++++++ src/dvb_subtitle_decoder.h | 74 ++ src/general_loop.cpp | 9 +- src/params.cpp | 60 ++ src/ts_functions.cpp | 14 +- src/ts_tables.cpp | 43 +- 11 files changed, 1874 insertions(+), 16 deletions(-) create mode 100644 src/dvb_subtitle_decoder.c create mode 100644 src/dvb_subtitle_decoder.h diff --git a/.gitignore b/.gitignore index 06867f8e..e372ad87 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ #Ignore CVS .CVS CVS +*.o +linux/ccextractor +linux/depend diff --git a/linux/Makefile b/linux/Makefile index 1e278ac8..951a2eba 100644 --- a/linux/Makefile +++ b/linux/Makefile @@ -19,7 +19,7 @@ src_OBJS = \ encoding.o es_functions.o es_userdata.o file_functions.o \ general_loop.o mp4.o myth.o output.o params_dump.o params.o \ sequencing.o stream_functions.o telxcc.o timing.o ts_functions.o \ - utility.o wtv_functions.o xds.o + utility.o wtv_functions.o xds.o dvb_subtitle_decoder.o ts_tables.o gpacmp4_OBJS = \ avc_ext.o avilib.o av_parsers.o base_encoding.o bitstream.o \ @@ -42,7 +42,8 @@ src_SRC = \ ../src/output.cpp ../src/params.cpp ../src/params_dump.cpp \ ../src/sequencing.cpp ../src/stream_functions.cpp ../src/telxcc.cpp \ ../src/timing.cpp ../src/ts_functions.cpp ../src/utility.cpp \ - ../src/wtv_functions.cpp ../src/xds.cpp + ../src/wtv_functions.cpp ../src/xds.cpp ../src/dvb_subtitle_decoder.c \ + ../src/ts_tables.cpp gpacmp4_SRC = \ ../src/gpacmp4/avc_ext.c ../src/gpacmp4/avilib.c \ diff --git a/src/ccextractor.cpp b/src/ccextractor.cpp index ac7bfda3..a878c6cb 100644 --- a/src/ccextractor.cpp +++ b/src/ccextractor.cpp @@ -104,6 +104,11 @@ void init_options (struct ccx_s_options *options) options->direct_rollup=0; // Write roll-up captions directly instead of line by line? options->nofontcolor=0; // 1 = don't put tags options->notypesetting=0; // 1 = Don't put , , etc typesetting tags + + /* Select subtitle codec */ + options->codec = CCX_CODEC_ANY; + options->nocodec = CCX_CODEC_NONE; + /* Credit stuff */ options->start_credits_text=NULL; options->end_credits_text=NULL; diff --git a/src/ccextractor.h b/src/ccextractor.h index 105b911b..b1ee7c76 100644 --- a/src/ccextractor.h +++ b/src/ccextractor.h @@ -42,6 +42,10 @@ struct ccx_s_options // Options from user parameters int nofontcolor; int notypesetting; struct ccx_boundary_time extraction_start, extraction_end; // Segment we actually process + + /* subtitle codec type */ + enum cxx_code_type codec; + enum cxx_code_type nocodec; /* Credit stuff */ char *start_credits_text; char *end_credits_text; diff --git a/src/constants.h b/src/constants.h index 5e8eab93..aebf36dd 100644 --- a/src/constants.h +++ b/src/constants.h @@ -123,6 +123,7 @@ enum ccx_mpeg_descriptor CCX_MPEG_DSC_VBI_DATA_DESCRIPTOR = 0x45, CCX_MPEG_DSC_VBI_TELETEXT_DESCRIPTOR = 0x46, CCX_MPEG_DSC_TELETEXT_DESCRIPTOR = 0x56, + CCX_MPEG_DSC_DVB_SUBTITLE = 0x59, }; enum @@ -191,7 +192,8 @@ enum ccx_bufferdata_type CCX_H264 = 3, CCX_HAUPPAGE = 4, CCX_TELETEXT = 5, - CCX_PRIVATE_MPEG2_CC = 6 + CCX_PRIVATE_MPEG2_CC = 6, + CCX_DVB_SUBTITLE = 7, }; enum ccx_frame_type @@ -209,6 +211,32 @@ typedef enum { UNDEF = 0xff } bool_t; +enum cxx_code_type +{ + CCX_CODEC_ANY, + CCX_CODEC_TELETEXT, + CCX_CODEC_DVB, + CCX_CODEC_NONE, +}; + +/* + * This macro to be used when you want to find out whether you + * should parse f_sel subtitle codec type or not + * + * @param u_sel pass the codec selected by user to be searched in + * all elementry stream, we ignore the not to be selected stream + * if we find stream this is selected stream. since setting + * selected stream and not selected to same codec does not + * make ay sense. + * + * @param u_nsel pass the codec selected by user not to be parsed + * we give false value if f_sel is equal to n_sel + * and vice versa true if ... + * + * @param f_sel pass the codec name whom you are tesing to be feasible + * to parse. + */ +#define IS_FEASIBLE(u_sel,u_nsel,f_sel) ( ( (u_sel) == CCX_CODEC_ANY && (u_nsel) != (f_sel) ) || (u_sel) == (f_sel) ) #define CCX_TXT_FORBIDDEN 0 // Ignore teletext packets #define CCX_TXT_AUTO_NOT_YET_FOUND 1 #define CCX_TXT_IN_USE 2 // Positive autodetected, or forced, etc diff --git a/src/dvb_subtitle_decoder.c b/src/dvb_subtitle_decoder.c new file mode 100644 index 00000000..5f9564b7 --- /dev/null +++ b/src/dvb_subtitle_decoder.c @@ -0,0 +1,1643 @@ +/* + * DVB subtitle decoding + * Copyright (c) 2005 Ian Caulfield + * Copyright (c) 2004 Michael Niedermayer + * Copyright (c) 2014 Anshul Maheshwari + * License: GPL 2.0 + * + * You should have received a copy of the GNU General Public + * License along with CCExtractor; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ +/** + * @file dvbsub.c + */ + +#include +#include +#include +#include +#include +#include +#include +/* convert values between host and network byte order(big endian) */ +#include + + +#include "dvb_subtitle_decoder.h" +#define DEBUG +#define DVBSUB_PAGE_SEGMENT 0x10 +#define DVBSUB_REGION_SEGMENT 0x11 +#define DVBSUB_CLUT_SEGMENT 0x12 +#define DVBSUB_OBJECT_SEGMENT 0x13 +#define DVBSUB_DISPLAYDEFINITION_SEGMENT 0x14 +#define DVBSUB_DISPLAY_SEGMENT 0x80 + + +#define RL32(x) (*(unsigned int *)(x)) +#define RB32(x) (ntohl(*(unsigned int *)(x))) +#define RL16(x) (*(unsigned short int*)(x)) +#define RB16(x) (ntohs(*(unsigned short int*)(x))) + + +#define SCALEBITS 10 +#define ONE_HALF (1 << (SCALEBITS - 1)) +#define FIX(x) ((int) ((x) * (1<> SCALEBITS];\ + g = cm[(y + g_add) >> SCALEBITS];\ + b = cm[(y + b_add) >> SCALEBITS];\ +} + +#define times4(x) x, x, x, x +#define times256(x) times4(times4(times4(times4(times4(x))))) + +#define MAX_NEG_CROP 1024 +const uint8_t crop_tab[256 + 2 * MAX_NEG_CROP] = { +times256(0x00), +0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F, +0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F, +0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F, +0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F, +0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F, +0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x5B,0x5C,0x5D,0x5E,0x5F, +0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F, +0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A,0x7B,0x7C,0x7D,0x7E,0x7F, +0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, +0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, +0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, +0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, +0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, +0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, +0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, +0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF, +times256(0xFF) +}; + +#define cm (crop_tab + MAX_NEG_CROP) + +const char *dvb_language[] +{ + "und", + "eng", + "fin", + NULL +}; + +static inline unsigned int bytestream_get_byte(const uint8_t **b) +{ + (*b) += 1; + return ((const uint8_t*)(*b -1))[0]; +} + +static inline unsigned int bytestream_get_be16(const uint8_t **b) +{ + (*b) += 2; + return RB16(*b -2); +} +typedef struct GetBitContext { + const uint8_t *buffer, *buffer_end; + int index; + int size_in_bits; + int size_in_bits_plus8; +} GetBitContext; + + +/** + * Initialize GetBitContext. + * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes + * larger than the actual read bits because some optimized bitstream + * readers read 32 or 64 bit at once and could read over the end + * @param bit_size the size of the buffer in bits + * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow. + */ +static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer, + int bit_size) +{ + int buffer_size; + int ret = 0; + + if (bit_size >= INT_MAX - 7 || bit_size < 0 || !buffer) { + buffer_size = bit_size = 0; + buffer = NULL; + ret = -1; + errno = EINVAL; + } + + buffer_size = (bit_size + 7) >> 3; + + s->buffer = buffer; + s->size_in_bits = bit_size; + s->size_in_bits_plus8 = bit_size + 8; + s->buffer_end = buffer + buffer_size; + s->index = 0; + + return ret; +} + +static inline int get_bits_count(const GetBitContext *s) +{ + return s->index; +} + +static inline unsigned int get_bits(GetBitContext *s, int n) +{ + register int tmp; + unsigned int re_index = s->index; + unsigned int re_cache = 0; + unsigned int re_size_plus8 = s->size_in_bits_plus8; + + if(n <=0 && n>25) + return -1; + re_cache = RB32( s->buffer + (re_index >> 3 )) << (re_index & 7); + + tmp = ((uint32_t) re_cache) >> (32 -n); + + re_index = ( (re_size_plus8 < re_index + (n) )? ( re_size_plus8 ) : ( re_index + (n)) ); + + s->index = re_index; + return tmp; +} +static inline unsigned int get_bits1(GetBitContext *s) +{ + unsigned int index = s->index; + uint8_t result = s->buffer[index >> 3]; + + result <<= index & 7; + result >>= 8 - 1; + + if (s->index < s->size_in_bits_plus8) + index++; + s->index = index; + + return result; +} + +static void freep(void *arg) +{ + void **ptr = (void **)arg; + free(*ptr); + *ptr = NULL; + +} +#ifdef DEBUG +static void png_save(const char *filename, uint32_t *bitmap, int w, int h) +{ + int x, y, v; + FILE *f; + char fname[40], fname2[40]; + char command[1024]; + + snprintf(fname, sizeof(fname), "%s.ppm", filename); + + f = fopen(fname, "w"); + if (!f) { + perror(fname); + return; + } + fprintf(f, "P6\n" + "%d %d\n" + "%d\n", + w, h, 255); + for(y = 0; y < h; y++) { + for(x = 0; x < w; x++) { + v = bitmap[y * w + x]; + putc((v >> 16) & 0xff, f); + putc((v >> 8) & 0xff, f); + putc((v >> 0) & 0xff, f); + } + } + fclose(f); + + + snprintf(fname2, sizeof(fname2), "%s-a.pgm", filename); + + f = fopen(fname2, "w"); + if (!f) { + perror(fname2); + return; + } + fprintf(f, "P5\n" + "%d %d\n" + "%d\n", + w, h, 255); + for(y = 0; y < h; y++) { + for(x = 0; x < w; x++) { + v = bitmap[y * w + x]; + putc((v >> 24) & 0xff, f); + } + } + fclose(f); + + snprintf(command, sizeof(command), "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename); + system(command); + +// snprintf(command, sizeof(command), "rm %s %s", fname, fname2); +// system(command); +} +#endif + +#define RGBA(r,g,b,a) (((unsigned)(a) << 24) | ((r) << 16) | ((g) << 8) | (b)) + +typedef struct DVBSubCLUT { + int id; + int version; + + uint32_t clut4[4]; + uint32_t clut16[16]; + uint32_t clut256[256]; + + struct DVBSubCLUT *next; +} DVBSubCLUT; + +static DVBSubCLUT default_clut; + +typedef struct DVBSubObjectDisplay { + int object_id; + int region_id; + + int x_pos; + int y_pos; + + int fgcolor; + int bgcolor; + + struct DVBSubObjectDisplay *region_list_next; + struct DVBSubObjectDisplay *object_list_next; +} DVBSubObjectDisplay; + +typedef struct DVBSubObject { + int id; + int version; + + int type; + + DVBSubObjectDisplay *display_list; + + struct DVBSubObject *next; +} DVBSubObject; + +typedef struct DVBSubRegionDisplay { + int region_id; + + int x_pos; + int y_pos; + + struct DVBSubRegionDisplay *next; +} DVBSubRegionDisplay; + +typedef struct DVBSubRegion { + int id; + int version; + + int width; + int height; + int depth; + + int clut; + int bgcolor; + + uint8_t *pbuf; + int buf_size; + int dirty; + + DVBSubObjectDisplay *display_list; + + struct DVBSubRegion *next; +} DVBSubRegion; + +typedef struct DVBSubDisplayDefinition { + int version; + + int x; + int y; + int width; + int height; +} DVBSubDisplayDefinition; + +typedef struct DVBSubContext { + int composition_id; + int ancillary_id; + + int version; + int time_out; + DVBSubRegion *region_list; + DVBSubCLUT *clut_list; + DVBSubObject *object_list; + + DVBSubRegionDisplay *display_list; + DVBSubDisplayDefinition *display_definition; +} DVBSubContext; + + +static DVBSubObject* get_object(DVBSubContext *ctx, int object_id) +{ + DVBSubObject *ptr = ctx->object_list; + + while (ptr && ptr->id != object_id) { + ptr = ptr->next; + } + + return ptr; +} + +static DVBSubCLUT* get_clut(DVBSubContext *ctx, int clut_id) +{ + DVBSubCLUT *ptr = ctx->clut_list; + + while (ptr && ptr->id != clut_id) { + ptr = ptr->next; + } + + return ptr; +} + +static DVBSubRegion* get_region(DVBSubContext *ctx, int region_id) +{ + DVBSubRegion *ptr = ctx->region_list; + + while (ptr && ptr->id != region_id) { + ptr = ptr->next; + } + + return ptr; +} + +static void delete_region_display_list(DVBSubContext *ctx, DVBSubRegion *region) +{ + DVBSubObject *object, *obj2, **obj2_ptr; + DVBSubObjectDisplay *display, *obj_disp, **obj_disp_ptr; + + while (region->display_list) { + display = region->display_list; + + object = get_object(ctx, display->object_id); + + if (object) { + obj_disp_ptr = &object->display_list; + obj_disp = *obj_disp_ptr; + + while (obj_disp && obj_disp != display) { + obj_disp_ptr = &obj_disp->object_list_next; + obj_disp = *obj_disp_ptr; + } + + if (obj_disp) { + *obj_disp_ptr = obj_disp->object_list_next; + + if (!object->display_list) { + obj2_ptr = &ctx->object_list; + obj2 = *obj2_ptr; + + while (obj2 != object) { + assert(obj2); + obj2_ptr = &obj2->next; + obj2 = *obj2_ptr; + } + + *obj2_ptr = obj2->next; + + free(obj2); + } + } + } + + region->display_list = display->region_list_next; + + free(display); + } + +} + +static void delete_cluts(DVBSubContext *ctx) +{ + DVBSubCLUT *clut; + + while (ctx->clut_list) { + clut = ctx->clut_list; + + ctx->clut_list = clut->next; + + free(clut); + } +} + +static void delete_objects(DVBSubContext *ctx) +{ + DVBSubObject *object; + + while (ctx->object_list) { + object = ctx->object_list; + + ctx->object_list = object->next; + + free(object); + } +} + +static void delete_regions(DVBSubContext *ctx) +{ + DVBSubRegion *region; + + while (ctx->region_list) { + region = ctx->region_list; + + ctx->region_list = region->next; + + delete_region_display_list(ctx, region); + + free(region->pbuf); + free(region); + } +} +/** + * @param composition_id composition-page_id found in Subtitle descriptors + * associated with subtitle stream in the PMT + * it could be -1 if not found in PMT. + * @param ancillary_id ancillary-page_id found in Subtitle descriptors + * associated with subtitle stream in the PMT. + * it could be -1 if not found in PMT. + * + * @return DVB context kept as void* for abstraction + * + */ +void* dvbsub_init_decoder(int composition_id,int ancillary_id) +{ + int i, r, g, b, a = 0; + DVBSubContext *ctx = (DVBSubContext*)malloc(sizeof(DVBSubContext)); + memset(ctx,0,sizeof(DVBSubContext)); + + ctx->composition_id = composition_id; + ctx->ancillary_id = ancillary_id; + + ctx->version = -1; + + default_clut.id = -1; + default_clut.next = NULL; + + default_clut.clut4[0] = RGBA( 0, 0, 0, 0); + default_clut.clut4[1] = RGBA(255, 255, 255, 255); + default_clut.clut4[2] = RGBA( 0, 0, 0, 255); + default_clut.clut4[3] = RGBA(127, 127, 127, 255); + + default_clut.clut16[0] = RGBA( 0, 0, 0, 0); + for (i = 1; i < 16; i++) { + if (i < 8) { + r = (i & 1) ? 255 : 0; + g = (i & 2) ? 255 : 0; + b = (i & 4) ? 255 : 0; + } else { + r = (i & 1) ? 127 : 0; + g = (i & 2) ? 127 : 0; + b = (i & 4) ? 127 : 0; + } + default_clut.clut16[i] = RGBA(r, g, b, 255); + } + + default_clut.clut256[0] = RGBA( 0, 0, 0, 0); + for (i = 1; i < 256; i++) { + if (i < 8) { + r = (i & 1) ? 255 : 0; + g = (i & 2) ? 255 : 0; + b = (i & 4) ? 255 : 0; + a = 63; + } else { + switch (i & 0x88) { + case 0x00: + r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0); + g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0); + b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0); + a = 255; + break; + case 0x08: + r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0); + g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0); + b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0); + a = 127; + break; + case 0x80: + r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0); + g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0); + b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0); + a = 255; + break; + case 0x88: + r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0); + g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0); + b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0); + a = 255; + break; + } + } + default_clut.clut256[i] = RGBA(r, g, b, a); + } + + return (void*)ctx; +} +int dvbsub_close_decoder(void *dvb_ctx) +{ + DVBSubContext *ctx = (DVBSubContext *)dvb_ctx; + DVBSubRegionDisplay *display; + + delete_regions(ctx); + + delete_objects(ctx); + + delete_cluts(ctx); + + freep(&ctx->display_definition); + + while (ctx->display_list) { + display = ctx->display_list; + ctx->display_list = display->next; + free(display); + } + + return 0; +} + +static int dvbsub_read_2bit_string(uint8_t *destbuf, int dbuf_len, + const uint8_t **srcbuf, int buf_size, + int non_mod, uint8_t *map_table, int x_pos) +{ + GetBitContext gb; + + int bits; + int run_length; + int pixels_read = x_pos; + + init_get_bits(&gb, *srcbuf, buf_size << 3); + + destbuf += x_pos; + + while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) { + bits = get_bits(&gb, 2); + + if (bits) { + if (non_mod != 1 || bits != 1) { + if (map_table) + *destbuf++ = map_table[bits]; + else + *destbuf++ = bits; + } + pixels_read++; + } else { + bits = get_bits1(&gb); + if (bits == 1) { + run_length = get_bits(&gb, 3) + 3; + bits = get_bits(&gb, 2); + + if (non_mod == 1 && bits == 1) + pixels_read += run_length; + else { + if (map_table) + bits = map_table[bits]; + while (run_length-- > 0 && pixels_read < dbuf_len) { + *destbuf++ = bits; + pixels_read++; + } + } + } else { + bits = get_bits1(&gb); + if (bits == 0) { + bits = get_bits(&gb, 2); + if (bits == 2) { + run_length = get_bits(&gb, 4) + 12; + bits = get_bits(&gb, 2); + + if (non_mod == 1 && bits == 1) + pixels_read += run_length; + else { + if (map_table) + bits = map_table[bits]; + while (run_length-- > 0 && pixels_read < dbuf_len) { + *destbuf++ = bits; + pixels_read++; + } + } + } else if (bits == 3) { + run_length = get_bits(&gb, 8) + 29; + bits = get_bits(&gb, 2); + + if (non_mod == 1 && bits == 1) + pixels_read += run_length; + else { + if (map_table) + bits = map_table[bits]; + while (run_length-- > 0 && pixels_read < dbuf_len) { + *destbuf++ = bits; + pixels_read++; + } + } + } else if (bits == 1) { + if (map_table) + bits = map_table[0]; + else + bits = 0; + run_length = 2; + while (run_length-- > 0 && pixels_read < dbuf_len) { + *destbuf++ = bits; + pixels_read++; + } + } else { + (*srcbuf) += (get_bits_count(&gb) + 7) >> 3; + return pixels_read; + } + } else { + if (map_table) + bits = map_table[0]; + else + bits = 0; + *destbuf++ = bits; + pixels_read++; + } + } + } + } + + if (get_bits(&gb, 6)) + printf("DVBSub error: line overflow\n"); + + (*srcbuf) += (get_bits_count(&gb) + 7) >> 3; + + return pixels_read; +} + +static int dvbsub_read_4bit_string(uint8_t *destbuf, int dbuf_len, + const uint8_t **srcbuf, int buf_size, + int non_mod, uint8_t *map_table, int x_pos) +{ + GetBitContext gb; + + int bits; + int run_length; + int pixels_read = x_pos; + + init_get_bits(&gb, *srcbuf, buf_size << 3); + + destbuf += x_pos; + + while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) { + bits = get_bits(&gb, 4); + + if (bits) { + if (non_mod != 1 || bits != 1) { + if (map_table) + *destbuf++ = map_table[bits]; + else + *destbuf++ = bits; + } + pixels_read++; + } else { + bits = get_bits1(&gb); + if (bits == 0) { + run_length = get_bits(&gb, 3); + + if (run_length == 0) { + (*srcbuf) += (get_bits_count(&gb) + 7) >> 3; + return pixels_read; + } + + run_length += 2; + + if (map_table) + bits = map_table[0]; + else + bits = 0; + + while (run_length-- > 0 && pixels_read < dbuf_len) { + *destbuf++ = bits; + pixels_read++; + } + } else { + bits = get_bits1(&gb); + if (bits == 0) { + run_length = get_bits(&gb, 2) + 4; + bits = get_bits(&gb, 4); + + if (non_mod == 1 && bits == 1) + pixels_read += run_length; + else { + if (map_table) + bits = map_table[bits]; + while (run_length-- > 0 && pixels_read < dbuf_len) { + *destbuf++ = bits; + pixels_read++; + } + } + } else { + bits = get_bits(&gb, 2); + if (bits == 2) { + run_length = get_bits(&gb, 4) + 9; + bits = get_bits(&gb, 4); + + if (non_mod == 1 && bits == 1) + pixels_read += run_length; + else { + if (map_table) + bits = map_table[bits]; + while (run_length-- > 0 && pixels_read < dbuf_len) { + *destbuf++ = bits; + pixels_read++; + } + } + } else if (bits == 3) { + run_length = get_bits(&gb, 8) + 25; + bits = get_bits(&gb, 4); + + if (non_mod == 1 && bits == 1) + pixels_read += run_length; + else { + if (map_table) + bits = map_table[bits]; + while (run_length-- > 0 && pixels_read < dbuf_len) { + *destbuf++ = bits; + pixels_read++; + } + } + } else if (bits == 1) { + if (map_table) + bits = map_table[0]; + else + bits = 0; + run_length = 2; + while (run_length-- > 0 && pixels_read < dbuf_len) { + *destbuf++ = bits; + pixels_read++; + } + } else { + if (map_table) + bits = map_table[0]; + else + bits = 0; + *destbuf++ = bits; + pixels_read ++; + } + } + } + } + } + + if (get_bits(&gb, 8)) + printf("DVBSub error: line overflow\n"); + + (*srcbuf) += (get_bits_count(&gb) + 7) >> 3; + + return pixels_read; +} + +static int dvbsub_read_8bit_string(uint8_t *destbuf, int dbuf_len, + const uint8_t **srcbuf, int buf_size, + int non_mod, uint8_t *map_table, int x_pos) +{ + const uint8_t *sbuf_end = (*srcbuf) + buf_size; + int bits; + int run_length; + int pixels_read = x_pos; + + destbuf += x_pos; + + while (*srcbuf < sbuf_end && pixels_read < dbuf_len) { + bits = *(*srcbuf)++; + + if (bits) { + if (non_mod != 1 || bits != 1) { + if (map_table) + *destbuf++ = map_table[bits]; + else + *destbuf++ = bits; + } + pixels_read++; + } else { + bits = *(*srcbuf)++; + run_length = bits & 0x7f; + if ((bits & 0x80) == 0) { + if (run_length == 0) { + return pixels_read; + } + + if (map_table) + bits = map_table[0]; + else + bits = 0; + while (run_length-- > 0 && pixels_read < dbuf_len) { + *destbuf++ = bits; + pixels_read++; + } + } else { + bits = *(*srcbuf)++; + + if (non_mod == 1 && bits == 1) + pixels_read += run_length; + if (map_table) + bits = map_table[bits]; + else while (run_length-- > 0 && pixels_read < dbuf_len) { + *destbuf++ = bits; + pixels_read++; + } + } + } + } + + if (*(*srcbuf)++) + printf("DVBSub error: line overflow\n"); + + return pixels_read; +} + + + +static void dvbsub_parse_pixel_data_block(void *dvb_ctx, DVBSubObjectDisplay *display, + const uint8_t *buf, int buf_size, int top_bottom, int non_mod) +{ + DVBSubContext *ctx = (DVBSubContext *)dvb_ctx; + + DVBSubRegion *region = get_region(ctx, display->region_id); + const uint8_t *buf_end = buf + buf_size; + uint8_t *pbuf; + int x_pos, y_pos; + int i; + + uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf}; + uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff}; + uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, + 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}; + uint8_t *map_table; + + if (region == 0) + return; + + pbuf = region->pbuf; + region->dirty = 1; + + x_pos = display->x_pos; + y_pos = display->y_pos; + + y_pos += top_bottom; + + while (buf < buf_end) { + if ((*buf!=0xf0 && x_pos >= region->width) || y_pos >= region->height) { + printf("Invalid object location! %d-%d %d-%d %02x\n", x_pos, region->width, y_pos, region->height, *buf); + return; + } + + switch (*buf++) { + case 0x10: + if (region->depth == 8) + map_table = map2to8; + else if (region->depth == 4) + map_table = map2to4; + else + map_table = NULL; + + x_pos = dvbsub_read_2bit_string(pbuf + (y_pos * region->width), + region->width, &buf, buf_end - buf, + non_mod, map_table, x_pos); + break; + case 0x11: + if (region->depth < 4) { + printf("4-bit pixel string in %d-bit region!\n", region->depth); + return; + } + + if (region->depth == 8) + map_table = map4to8; + else + map_table = NULL; + + x_pos = dvbsub_read_4bit_string(pbuf + (y_pos * region->width), + region->width, &buf, buf_end - buf, + non_mod, map_table, x_pos); + break; + case 0x12: + if (region->depth < 8) { + printf("8-bit pixel string in %d-bit region!\n", region->depth); + return; + } + + x_pos = dvbsub_read_8bit_string(pbuf + (y_pos * region->width), + region->width, &buf, buf_end - buf, + non_mod, NULL, x_pos); + break; + + case 0x20: + map2to4[0] = (*buf) >> 4; + map2to4[1] = (*buf++) & 0xf; + map2to4[2] = (*buf) >> 4; + map2to4[3] = (*buf++) & 0xf; + break; + case 0x21: + for (i = 0; i < 4; i++) + map2to8[i] = *buf++; + break; + case 0x22: + for (i = 0; i < 16; i++) + map4to8[i] = *buf++; + break; + + case 0xf0: + x_pos = display->x_pos; + y_pos += 2; + break; + default: + printf("Unknown/unsupported pixel block 0x%x\n", *(buf-1)); + } + } + +} + +static void dvbsub_parse_object_segment(void *dvb_ctx, + const uint8_t *buf, int buf_size) +{ + DVBSubContext *ctx = (DVBSubContext*)dvb_ctx; + + const uint8_t *buf_end = buf + buf_size; + int object_id; + DVBSubObject *object; + DVBSubObjectDisplay *display; + int top_field_len, bottom_field_len; + + int coding_method, non_modifying_color; + + object_id = RB16(buf); + buf += 2; + + object = get_object(ctx, object_id); + + if (!object) + return; + + coding_method = ((*buf) >> 2) & 3; + non_modifying_color = ((*buf++) >> 1) & 1; + + if (coding_method == 0) { + top_field_len = RB16(buf); + buf += 2; + bottom_field_len = RB16(buf); + buf += 2; + + if (buf + top_field_len + bottom_field_len > buf_end) { + printf( "Field data size too large\n"); + return; + } + + for (display = object->display_list; display; display = display->object_list_next) { + const uint8_t *block = buf; + int bfl = bottom_field_len; + + dvbsub_parse_pixel_data_block(dvb_ctx, display, block, top_field_len, 0, + non_modifying_color); + + if (bottom_field_len > 0) + block = buf + top_field_len; + else + bfl = top_field_len; + + dvbsub_parse_pixel_data_block(dvb_ctx, display, block, bfl, 1, + non_modifying_color); + } + + } else if (coding_method == 1) { + printf( "FIXME support for sring coding standard\n"); + } else { + printf( "Unknown object coding %d\n", coding_method); + } + +} + +static int dvbsub_parse_clut_segment(void *dvb_ctx, + const uint8_t *buf, int buf_size) +{ + DVBSubContext *ctx = (DVBSubContext*)dvb_ctx; + + const uint8_t *buf_end = buf + buf_size; + int i, clut_id; + int version; + DVBSubCLUT *clut; + int entry_id, depth , full_range; + int y, cr, cb, alpha; + int r, g, b, r_add, g_add, b_add; + + printf("DVB clut packet:\n"); + + for (i=0; i < buf_size; i++) { + printf("%02x ", buf[i]); + if (i % 16 == 15) + printf("\n"); + } + + if (i % 16) + printf("\n"); + + clut_id = *buf++; + version = ((*buf)>>4)&15; + buf += 1; + + clut = get_clut(ctx, clut_id); + + if (!clut) { + clut = (DVBSubCLUT*)malloc(sizeof(DVBSubCLUT)); + + memcpy(clut, &default_clut, sizeof(DVBSubCLUT)); + + clut->id = clut_id; + clut->version = -1; + + clut->next = ctx->clut_list; + ctx->clut_list = clut; + } + + if (clut->version != version) { + + clut->version = version; + + while (buf + 4 < buf_end) { + entry_id = *buf++; + + depth = (*buf) & 0xe0; + + if (depth == 0) { + printf( "Invalid clut depth 0x%x!\n", *buf); + return 0; + } + + full_range = (*buf++) & 1; + + if (full_range) { + y = *buf++; + cr = *buf++; + cb = *buf++; + alpha = *buf++; + } else { + y = buf[0] & 0xfc; + cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4; + cb = (buf[1] << 2) & 0xf0; + alpha = (buf[1] << 6) & 0xc0; + + buf += 2; + } + + if (y == 0) + alpha = 0xff; + + YUV_TO_RGB1_CCIR(cb, cr); + YUV_TO_RGB2_CCIR(r, g, b, y); + + printf("clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha); + if (!!(depth & 0x80) + !!(depth & 0x40) + !!(depth & 0x20) > 1) { + printf("More than one bit level marked: %x\n", depth); + } + + if (depth & 0x80) + clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha); + else if (depth & 0x40) + clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha); + else if (depth & 0x20) + clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha); + } + } + return 0; +} + + +static void dvbsub_parse_region_segment(void*dvb_ctx, + const uint8_t *buf, int buf_size) +{ + DVBSubContext *ctx = (DVBSubContext *)dvb_ctx; + + const uint8_t *buf_end = buf + buf_size; + int region_id, object_id; + int version; + DVBSubRegion *region; + DVBSubObject *object; + DVBSubObjectDisplay *display; + int fill; + + if (buf_size < 10) + return; + + region_id = *buf++; + version = ((*buf)>>4) & 15; + + region = get_region(ctx, region_id); + + if (!region) { + region = (struct DVBSubRegion*) malloc(sizeof(struct DVBSubRegion)); + memset(region,0,sizeof(struct DVBSubRegion)); + + region->id = region_id; + region->version = version; + + region->next = ctx->region_list; + ctx->region_list = region; + } else if(version == region->version) + return; + fill = ((*buf++) >> 3) & 1; + + region->width = RB16(buf); + buf += 2; + region->height = RB16(buf); + buf += 2; + + if (region->width * region->height != region->buf_size) { + free(region->pbuf); + + region->buf_size = region->width * region->height; + + region->pbuf = (uint8_t*)malloc(region->buf_size); + + fill = 1; + region->dirty = 0; + } + + region->depth = 1 << (((*buf++) >> 2) & 7); + if(region->depth<2 || region->depth>8){ + printf( "region depth %d is invalid\n", region->depth); + region->depth= 4; + } + region->clut = *buf++; + + if (region->depth == 8) { + region->bgcolor = *buf++; + buf += 1; + } else { + buf += 1; + + if (region->depth == 4) + region->bgcolor = (((*buf++) >> 4) & 15); + else + region->bgcolor = (((*buf++) >> 2) & 3); + } + + printf("Region %d, (%dx%d)\n", region_id, region->width, region->height); + + if (fill) { + memset(region->pbuf, region->bgcolor, region->buf_size); + printf( "Fill region (%d)\n", region->bgcolor); + } + + delete_region_display_list(ctx, region); + + while (buf + 5 < buf_end) { + object_id = RB16(buf); + buf += 2; + + object = get_object(ctx, object_id); + + if (!object) { + object = (struct DVBSubObject*)malloc(sizeof(struct DVBSubObject)); + memset(object,0,sizeof(struct DVBSubObject)); + + object->id = object_id; + object->next = ctx->object_list; + ctx->object_list = object; + } + + object->type = (*buf) >> 6; + + display = (struct DVBSubObjectDisplay*)malloc(sizeof(struct DVBSubObjectDisplay)); + memset(display,0,sizeof(struct DVBSubObjectDisplay)); + + display->object_id = object_id; + display->region_id = region_id; + + display->x_pos = RB16(buf) & 0xfff; + buf += 2; + display->y_pos = RB16(buf) & 0xfff; + buf += 2; + + if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) { + display->fgcolor = *buf++; + display->bgcolor = *buf++; + } + + display->region_list_next = region->display_list; + region->display_list = display; + + display->object_list_next = object->display_list; + object->display_list = display; + } +} + +static void dvbsub_parse_page_segment(void *dvb_ctx, + const uint8_t *buf, int buf_size) +{ + DVBSubContext *ctx = (DVBSubContext*)dvb_ctx; + DVBSubRegionDisplay *display; + DVBSubRegionDisplay *tmp_display_list, **tmp_ptr; + + const uint8_t *buf_end = buf + buf_size; + int region_id; + int page_state; + int timeout; + int version; + + if (buf_size < 1) + return; + + timeout = *buf++; + version = ((*buf)>>4) & 15; + page_state = ((*buf++) >> 2) & 3; + + //if version same mean we are already updated + if (ctx->version == version) + { + return; + } + + ctx->time_out = timeout; + ctx->version = version; + + printf( "Page time out %ds, state %d\n", ctx->time_out, page_state); + + if (page_state == 1 || page_state == 2) { + delete_regions(ctx); + delete_objects(ctx); + delete_cluts(ctx); + } + + tmp_display_list = ctx->display_list; + ctx->display_list = NULL; + + while (buf + 5 < buf_end) { + region_id = *buf++; + buf += 1; + + display = tmp_display_list; + tmp_ptr = &tmp_display_list; + + while (display && display->region_id != region_id) { + tmp_ptr = &display->next; + display = display->next; + } + + if (!display) + { + display = (struct DVBSubRegionDisplay*)malloc(sizeof(struct DVBSubRegionDisplay)); + memset(display,0,sizeof(struct DVBSubRegionDisplay)); + } + + + display->region_id = region_id; + + display->x_pos = RB16(buf); + buf += 2; + display->y_pos = RB16(buf); + buf += 2; + + *tmp_ptr = display->next; + + display->next = ctx->display_list; + ctx->display_list = display; + + printf( "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos); + } + + while (tmp_display_list) { + display = tmp_display_list; + + tmp_display_list = display->next; + + free(display); + } + +} + + +#ifdef DEBUG +static void save_display_set(DVBSubContext *ctx) +{ + DVBSubRegion *region; + DVBSubRegionDisplay *display; + DVBSubCLUT *clut; + uint32_t *clut_table; + int x_pos, y_pos, width, height; + int x, y, y_off, x_off; + uint32_t *pbuf; + char filename[32]; + static int fileno_index = 0; + + x_pos = -1; + y_pos = -1; + width = 0; + height = 0; + + for (display = ctx->display_list; display; display = display->next) { + region = get_region(ctx, display->region_id); + + if (x_pos == -1) { + x_pos = display->x_pos; + y_pos = display->y_pos; + width = region->width; + height = region->height; + } else { + if (display->x_pos < x_pos) { + width += (x_pos - display->x_pos); + x_pos = display->x_pos; + } + + if (display->y_pos < y_pos) { + height += (y_pos - display->y_pos); + y_pos = display->y_pos; + } + + if (display->x_pos + region->width > x_pos + width) { + width = display->x_pos + region->width - x_pos; + } + + if (display->y_pos + region->height > y_pos + height) { + height = display->y_pos + region->height - y_pos; + } + } + } + + if (x_pos >= 0) { + + pbuf = (uint32_t*)malloc(width * height * 4); + memset(pbuf,0x0,width * height * 4); + + + for (display = ctx->display_list; display; display = display->next) { + region = get_region(ctx, display->region_id); + + x_off = display->x_pos - x_pos; + y_off = display->y_pos - y_pos; + + clut = get_clut(ctx, region->clut); + + if (clut == 0) + clut = &default_clut; + + switch (region->depth) { + case 2: + clut_table = clut->clut4; + break; + case 8: + clut_table = clut->clut256; + break; + case 4: + default: + clut_table = clut->clut16; + break; + } + + for (y = 0; y < region->height; y++) { + for (x = 0; x < region->width; x++) { + pbuf[((y + y_off) * width) + x_off + x] = + clut_table[region->pbuf[y * region->width + x]]; + } + } + + } + + snprintf(filename, sizeof(filename), "dvbs.%d", fileno_index); + + png_save(filename, pbuf, width, height); + + free(pbuf); + } + + fileno_index++; +} +#endif + +static void dvbsub_parse_display_definition_segment(void *dvb_ctx, + const uint8_t *buf, + int buf_size) +{ + DVBSubContext *ctx = (DVBSubContext *)dvb_ctx; + DVBSubDisplayDefinition *display_def = ctx->display_definition; + int dds_version, info_byte; + + if (buf_size < 5) + return; + + info_byte = bytestream_get_byte(&buf); + dds_version = info_byte >> 4; + if (display_def && display_def->version == dds_version) + return; // already have this display definition version + + if (!display_def) { + display_def = (struct DVBSubDisplayDefinition*)malloc(sizeof(*display_def)); + memset(display_def,0,sizeof(*display_def)); + ctx->display_definition = display_def; + } + if (!display_def) + return; + + display_def->version = dds_version; + display_def->x = 0; + display_def->y = 0; + display_def->width = bytestream_get_be16(&buf) + 1; + display_def->height = bytestream_get_be16(&buf) + 1; + + if (buf_size < 13) + return; + + if (info_byte & 1<<3) { // display_window_flag + display_def->x = bytestream_get_be16(&buf); + display_def->width = bytestream_get_be16(&buf) - display_def->x + 1; + display_def->y = bytestream_get_be16(&buf); + display_def->height = bytestream_get_be16(&buf) - display_def->y + 1; + } +} + +static int dvbsub_display_end_segment(void *dvb_ctx, const uint8_t *buf, + int buf_size) +{ + DVBSubContext *ctx = (DVBSubContext *)dvb_ctx; + + DVBSubRegion *region; + DVBSubRegionDisplay *display; + DVBSubCLUT *clut; + int i; + + + + for (display = ctx->display_list; display; display = display->next) + { + region = get_region(ctx, display->region_id); + } + + i = 0; + + for (display = ctx->display_list; display; display = display->next) { + region = get_region(ctx, display->region_id); + + if (!region) + continue; + + if (!region->dirty) + continue; + clut = get_clut(ctx, region->clut); + + if (!clut) + clut = &default_clut; + + i++; + } +#ifdef DEBUG + save_display_set(ctx); +#endif + + return 1; +} +/** + * @param dvb_ctx PreInitialized DVB context using DVB + * @param data output subtitle data, to be implemented + * @param data_size Output subtitle data size. pass the pointer to an intiger, NOT to be NULL. + * @param buf buffer containg segment data, first sync byte needto 0x0f. + * does not include data_identifier and subtitle_stream_id. + * @param buf_size size of buf buffer + * + * @return -1 on error + */ +int dvbsub_decode(void *dvb_ctx, + void *data, int *data_size, + const unsigned char *buf,int buf_size) +{ + DVBSubContext *ctx = (DVBSubContext *)dvb_ctx; +// AVSubtitle *sub = data; + const uint8_t *p, *p_end; + int segment_type; + int page_id; + int segment_length; + int ret; + int got_segment = 0; + + + if (buf_size <= 6 || *buf != 0x0f) { + printf( "incomplete or broken packet"); + return -1; + } + + p = buf; + p_end = buf + buf_size; + + while (p_end - p >= 6 && *p == 0x0f) { + p += 1; + segment_type = *p++; + page_id = RB16(p); + p += 2; + segment_length = RB16(p); + p += 2; + + if (p_end - p < segment_length) { + printf( "incomplete or broken packet"); + return -1; + } + + if (page_id == ctx->composition_id || page_id == ctx->ancillary_id || + ctx->composition_id == -1 || ctx->ancillary_id == -1) { + switch (segment_type) { + case DVBSUB_PAGE_SEGMENT: + dvbsub_parse_page_segment(dvb_ctx, p, segment_length); + got_segment |= 1; + break; + case DVBSUB_REGION_SEGMENT: + dvbsub_parse_region_segment(dvb_ctx, p, segment_length); + got_segment |= 2; + break; + case DVBSUB_CLUT_SEGMENT: + ret = dvbsub_parse_clut_segment(dvb_ctx, p, segment_length); + if (ret < 0) return ret; + got_segment |= 4; + break; + case DVBSUB_OBJECT_SEGMENT: + dvbsub_parse_object_segment(dvb_ctx, p, segment_length); + got_segment |= 8; + break; + case DVBSUB_DISPLAYDEFINITION_SEGMENT: + dvbsub_parse_display_definition_segment(dvb_ctx, p, segment_length); + break; + case DVBSUB_DISPLAY_SEGMENT: + *data_size = dvbsub_display_end_segment(dvb_ctx, p, segment_length); + got_segment |= 16; + break; + default: + printf( "Subtitling segment type 0x%x, page id %d, length %d\n", + segment_type, page_id, segment_length); + break; + } + } + + p += segment_length; + } + // Some streams do not send a display segment but if we have all the other + // segments then we need no further data. + if (got_segment == 15) + *data_size = dvbsub_display_end_segment(dvb_ctx, p, 0); + + return p - buf; +} +/** + * @func parse_dvb_description + * + * data pointer to this function should be after description length and description tag is parsed + * + * @param cfg preallocated dvb_config structure where parsed description will be stored,Not to be NULL + * + * @return return -1 if invalid data found other wise 0 if everything goes well + * errno is set is to EINVAL if invalid data is found + */ +int parse_dvb_description (struct dvb_config* cfg,unsigned char*data,unsigned int len) +{ + int i = 0; + int j = 0; + /* 8 bytes per DVB subtitle substream d2: + * ISO_639_language_code (3 bytes), + * subtitling_type (1 byte), + * composition_page_id (2 bytes), + * ancillary_page_id (2 bytes) + */ + + cfg->n_language = len/8; + + if (len > 0 && len % 8 != 0) + { + errno = EINVAL; + return -1; + } + + if (cfg->n_language > 1) + { + printf("DVB subtitles with multiple languages"); + } + + if (cfg->n_language > MAX_LANGUAGE_PER_DESC) + { + printf("not supported more then %d language",MAX_LANGUAGE_PER_DESC); + } + + for (i = 0; i < cfg->n_language; i++,data += i*8) + { + /* setting language to undefined if not found in language lkup table */ + for(j = 0,cfg->lang_index[i] = 0;dvb_language[j] != NULL;j++) + { + if(!strncmp((const char*)(data),dvb_language[j],3)) + cfg->lang_index[i] = j; + } + cfg->sub_type[i] = data[3]; + cfg->composition_id[i] = RB16(data + 4); + cfg->ancillary_id[i] = RB16(data + 6); + + } + + printf("lang %d %s\n",cfg->lang_index[0],dvb_language[cfg->lang_index[0]]); + printf("comp %hd anci %hd stype %hhd\n",cfg->composition_id[0],cfg->ancillary_id[0],cfg->sub_type[0]); + return 0; +} diff --git a/src/dvb_subtitle_decoder.h b/src/dvb_subtitle_decoder.h new file mode 100644 index 00000000..9f0e592a --- /dev/null +++ b/src/dvb_subtitle_decoder.h @@ -0,0 +1,74 @@ +/* + * DVB subtitle decoding + * Copyright (c) 2014 Anshul + * License: LGPL + * + * This file is part of CCEXtractor + * You should have received a copy of the GNU Lesser General Public + * License along with CCExtractor; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ +/** + * @file dvbsub.c + */ + +#ifndef DVBSUBDEC_H +#define DVBSUBDEC_H + +#define MAX_LANGUAGE_PER_DESC 5 + + +struct dvb_config +{ + unsigned char n_language; + unsigned int lang_index[MAX_LANGUAGE_PER_DESC]; + /* subtitle type */ + unsigned char sub_type[MAX_LANGUAGE_PER_DESC]; + /* composition page id */ + unsigned short composition_id[MAX_LANGUAGE_PER_DESC]; + /* ancillary_page_id */ + unsigned short ancillary_id[MAX_LANGUAGE_PER_DESC]; +}; + + +/** + * @param composition_id composition-page_id found in Subtitle descriptors + * associated with subtitle stream in the PMT + * it could be -1 if not found in PMT. + * @param ancillary_id ancillary-page_id found in Subtitle descriptors + * associated with subtitle stream in the PMT. + * it could be -1 if not found in PMT. + * + * @return DVB context kept as void* for abstraction + * + */ +void* dvbsub_init_decoder(int composition_id,int ancillary_id); + +int dvbsub_close_decoder(void *dvb_ctx); + +/** + * @param dvb_ctx PreInitialized DVB context using DVB + * @param data output subtitle data, to be implemented + * @param data_size Output subtitle data size. pass the pointer to an intiger, NOT to be NULL. + * @param buf buffer containg segment data, first sync byte needto 0x0f. + * does not include data_identifier and subtitle_stream_id. + * @param buf_size size of buf buffer + * + * @return -1 on error + */ +int dvbsub_decode(void *dvb_ctx, + void *data, int *data_size, + const unsigned char *buf,int buf_size); +/** + * @func parse_dvb_description + * + * data pointer to this function should be after description length and description tag is parsed + * + * @param cfg preallocated dvb_config structure where parsed description will be stored,Not to be NULL + * + * @return return -1 if invalid data found other wise 0 if everything goes well + * errno is set is to EINVAL if invalid data is found + */ +int parse_dvb_description (struct dvb_config* cfg,unsigned char*data,unsigned int len); +#endif diff --git a/src/general_loop.cpp b/src/general_loop.cpp index bf5811ea..c892f5fd 100644 --- a/src/general_loop.cpp +++ b/src/general_loop.cpp @@ -6,6 +6,7 @@ #endif #include "708.h" +#include "dvb_subtitle_decoder.h" // IMPORTED TRASH INFO, REMOVE extern long num_nal_unit_type_7; @@ -43,10 +44,10 @@ unsigned char *filebuffer; LLONG filebuffer_start; // Position of buffer start relative to file int filebuffer_pos; // Position of pointer relative to buffer start int bytesinbuffer; // Number of bytes we actually have on buffer +extern void *cxx_dvb_context; LLONG process_raw_with_field (void); - // Program stream specific data grabber LLONG ps_getmoredata(void) { @@ -598,6 +599,12 @@ void general_loop(void) if (pts_set) set_fts(); // Try to fix timing from TS data } + else if(ccx_bufferdatatype == CCX_DVB_SUBTITLE) + { + int out_size = 0; + dvbsub_decode(cxx_dvb_context,NULL,&out_size,buffer + 2,inbuf); + got = inbuf; + } else if (ccx_bufferdatatype == CCX_PES) { got = process_m2v (buffer, inbuf); diff --git a/src/params.cpp b/src/params.cpp index 6aa0a148..4e0a6bb6 100644 --- a/src/params.cpp +++ b/src/params.cpp @@ -560,6 +560,7 @@ void usage (void) mprint ("generate a .srt file, with only data from 3:00 to 5:00 in the input file(s)\n"); mprint ("and then add that (huge) delay, which would make the final file start at\n"); mprint ("5:00 and end at 7:00.\n\n"); + mprint ("Options that affect what segment of the input file(s) to process:\n"); mprint (" -startat time: Only write caption information that starts after the\n"); mprint (" given time.\n"); @@ -572,6 +573,24 @@ void usage (void) mprint (" output formats. In all formats with timing information\n"); mprint (" the times are unchanged.\n"); mprint ("-scr --screenfuls num: Write 'num' screenfuls and terminate processing.\n\n"); + + mprint ("Options that affect which codec is to be used have to be searched in input\n"); + mprint (" If codec type is not selected then first elementry stream suitable for \n" + " subtitle is selected, please consider -teletext -noteletext override this\n" + " option.\n" + " -cocdec dvbsub select the dvb subtitle from all elementry stream,\n" + " if stream of dvb subtitle type is not found then \n" + " nothing is selected and no subtitle is generated\n" + " -nocodec dvbsub ignore dvb subtitle and follow default behaviour\n" + " -codec teletext select the teletext subtitle from elementry stream\n" + " -nocodec teletext ignore teletext subtitle\n" + " NOTE: option given in form -foo=bar ,-foo = bar and --foo=bar are invalid\n" + " valid option are only in form -foo bar\n" + " nocodec and codec parameter must not be same if found to be same \n" + " then parameter of nocodec is ignored, this flag should be passed \n" + " once more then one are not supported yet and last parameter would \n" + " taken in consideration\n"); + mprint ("Adding start and end credits:\n"); mprint (" CCExtractor can _try_ to add a custom message (for credits for example) at\n"); mprint (" the start and end of the file, looking for a window where there are no\n"); @@ -818,6 +837,45 @@ void parse_parameters (int argc, char *argv[]) set_input_format (argv[i]+4); continue; } + + /*user specified subtitle to be selected */ + + if(!strcmp (argv[i],"-codec")) + { + i++; + if(!strcmp (argv[i],"teletext")) + { + ccx_options.codec = CCX_CODEC_TELETEXT; + } + else if(!strcmp (argv[i],"dvbsub")) + { + ccx_options.codec = CCX_CODEC_DVB; + } + else + { + mprint("Invalid option for codec %s\n",argv[i]); + } + continue; + } + /*user specified subtitle to be selected */ + + if(!strcmp (argv[i],"-nocodec")) + { + i++; + if(!strcmp (argv[i],"teletext")) + { + ccx_options.nocodec = CCX_CODEC_TELETEXT; + } + else if(!strcmp (argv[i],"dvbsub")) + { + ccx_options.nocodec = CCX_CODEC_DVB; + } + else + { + mprint("Invalid option for codec %s\n",argv[i]); + } + continue; + } /* Output file formats */ if (strcmp (argv[i],"-srt")==0 || strcmp (argv[i],"-dvdraw")==0 || @@ -1367,11 +1425,13 @@ void parse_parameters (int argc, char *argv[]) } if (strcmp (argv[i],"-teletext")==0) { + ccx_options.codec = CCX_CODEC_TELETEXT; ccx_options.teletext_mode=CCX_TXT_IN_USE; continue; } if (strcmp (argv[i],"-noteletext")==0) { + ccx_options.nocodec = CCX_CODEC_TELETEXT; ccx_options.teletext_mode=CCX_TXT_FORBIDDEN; continue; } diff --git a/src/ts_functions.cpp b/src/ts_functions.cpp index 7e844bdb..94006f53 100644 --- a/src/ts_functions.cpp +++ b/src/ts_functions.cpp @@ -17,7 +17,7 @@ static long haup_capbuflen = 0; // Bytes read in haup_capbuf unsigned TS_program_number = 0; // Identifier for current program unsigned pmtpid = 0; // PID for Program Map Table unsigned cap_stream_type=CCX_STREAM_TYPE_UNKNOWNSTREAM; // Stream type for cappid - +extern void *cxx_dvb_context; // Descriptions for ts ccx_stream_type const char *desc[256]; @@ -464,6 +464,11 @@ LLONG ts_getmoredata(void) ccx_bufferdatatype = CCX_H264; tstr = "H.264"; } + else if ( cap_stream_type == CCX_STREAM_TYPE_PRIVATE_MPEG2 && cxx_dvb_context ) + { + ccx_bufferdatatype = CCX_DVB_SUBTITLE; + tstr = "DVB subtitle"; + } else if ( cap_stream_type == CCX_STREAM_TYPE_UNKNOWNSTREAM && ccx_options.hauppauge_mode) { ccx_bufferdatatype = CCX_HAUPPAGE; @@ -566,13 +571,6 @@ LLONG ts_getmoredata(void) haup_capbuflen=0; } - if( !((stream_id&0xf0)==0xe0)) // 0xBD = private stream - { - // ??? Shouldn't happen. Complain and try again. - mprint("Not a video PES header!\n"); - continue; - } - dbg_print(CCX_DMT_VERBOSE, "TS payload start video PES id: %d len: %ld\n", stream_id, capbuflen); diff --git a/src/ts_tables.cpp b/src/ts_tables.cpp index 4862bf96..abc516da 100644 --- a/src/ts_tables.cpp +++ b/src/ts_tables.cpp @@ -6,8 +6,10 @@ */ #include "ccextractor.h" +#include "dvb_subtitle_decoder.h" static unsigned pmt_warning_shown=0; // Only display warning once +void *cxx_dvb_context = NULL; // PMTs table PAT_entry pmt_array[TS_PMT_MAP_SIZE] = { 0 }; @@ -64,6 +66,8 @@ void clear_PMT_array (void) int parse_PMT (int pos) { int must_flush=0; + int ret = 0; + unsigned char desc_len = 0; if ((ccx_options.ts_forced_cappid || (ccx_options.teletext_mode==CCX_TXT_IN_USE && ccx_options.ts_cappid)) && cap_stream_type!=CCX_STREAM_TYPE_UNKNOWNSTREAM) // Already know what we need, skip @@ -190,20 +194,47 @@ int parse_PMT (int pos) for( unsigned i=0; i < stream_data && (i+4)