# HG changeset patch # User bellard # Date 1117807178 0 # Node ID d8874c8749ece9f327b48b75c2c9b894b015d787 # Parent 975074f04b951ee1366f807f78166624443e7c18 subtitle codec type support diff -r 975074f04b95 -r d8874c8749ec Makefile --- a/Makefile Thu Jun 02 21:15:20 2005 +0000 +++ b/Makefile Fri Jun 03 13:59:38 2005 +0000 @@ -17,8 +17,8 @@ ratecontrol.o adpcm.o eval.o error_resilience.o \ fft.o mdct.o raw.o golomb.o cabac.o\ dpcm.o adx.o rational.o faandct.o parser.o g726.o \ - vp3dsp.o integer.o h264idct.o rangecoder.o pnm.o h263.o msmpeg4.o h263dec.o - + vp3dsp.o integer.o h264idct.o rangecoder.o pnm.o h263.o msmpeg4.o h263dec.o dvdsub.o dvbsub.o + ifeq ($(CONFIG_AASC_DECODER),yes) OBJS+= aasc.o endif diff -r 975074f04b95 -r d8874c8749ec allcodecs.c --- a/allcodecs.c Thu Jun 02 21:15:20 2005 +0000 +++ b/allcodecs.c Fri Jun 03 13:59:38 2005 +0000 @@ -551,6 +551,10 @@ #undef PCM_CODEC + /* subtitles */ + register_avcodec(&dvdsub_decoder); + register_avcodec(&dvbsub_encoder); + /* parsers */ av_register_codec_parser(&mpegvideo_parser); av_register_codec_parser(&mpeg4video_parser); @@ -568,5 +572,6 @@ #ifdef CONFIG_AC3 av_register_codec_parser(&ac3_parser); #endif + av_register_codec_parser(&dvdsub_parser); } diff -r 975074f04b95 -r d8874c8749ec avcodec.h --- a/avcodec.h Thu Jun 02 21:15:20 2005 +0000 +++ b/avcodec.h Fri Jun 03 13:59:38 2005 +0000 @@ -17,7 +17,7 @@ #define FFMPEG_VERSION_INT 0x000409 #define FFMPEG_VERSION "0.4.9-pre1" -#define LIBAVCODEC_BUILD 4755 +#define LIBAVCODEC_BUILD 4756 #define LIBAVCODEC_VERSION_INT FFMPEG_VERSION_INT #define LIBAVCODEC_VERSION FFMPEG_VERSION @@ -175,6 +175,10 @@ CODEC_ID_GSM, CODEC_ID_OGGTHEORA= 0x16000, + + /* subtitle codecs */ + CODEC_ID_DVD_SUBTITLE= 0x17000, + CODEC_ID_DVB_SUBTITLE, CODEC_ID_MPEG2TS= 0x20000, /* _FAKE_ codec to indicate a raw MPEG2 transport stream (only used by libavformat) */ @@ -188,6 +192,7 @@ CODEC_TYPE_VIDEO, CODEC_TYPE_AUDIO, CODEC_TYPE_DATA, + CODEC_TYPE_SUBTITLE, }; /** @@ -1880,6 +1885,20 @@ } AVPaletteControl; +typedef struct AVSubtitle { + uint16_t format; /* 0 = graphics */ + uint16_t x; + uint16_t y; + uint16_t w; + uint16_t h; + uint16_t nb_colors; + uint32_t start_display_time; /* relative to packet pts, in ms */ + uint32_t end_display_time; /* relative to packet pts, in ms */ + int linesize; + uint32_t *rgba_palette; + uint8_t *bitmap; +} AVSubtitle; + extern AVCodec ac3_encoder; extern AVCodec mp2_encoder; extern AVCodec mp3lame_encoder; @@ -2067,6 +2086,10 @@ extern AVCodec ac3_decoder; extern AVCodec dts_decoder; +/* subtitles */ +extern AVCodec dvdsub_decoder; +extern AVCodec dvbsub_encoder; + /* resample.c */ struct ReSampleContext; @@ -2204,6 +2227,9 @@ int avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture, int *got_picture_ptr, uint8_t *buf, int buf_size); +int avcodec_decode_subtitle(AVCodecContext *avctx, AVSubtitle *sub, + int *got_sub_ptr, + const uint8_t *buf, int buf_size); int avcodec_parse_frame(AVCodecContext *avctx, uint8_t **pdata, int *data_size_ptr, uint8_t *buf, int buf_size); @@ -2211,6 +2237,8 @@ const short *samples); int avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size, const AVFrame *pict); +int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size, + const AVSubtitle *sub); int avcodec_close(AVCodecContext *avctx); @@ -2310,6 +2338,7 @@ extern AVCodecParser pnm_parser; extern AVCodecParser mpegaudio_parser; extern AVCodecParser ac3_parser; +extern AVCodecParser dvdsub_parser; /* memory */ void *av_malloc(unsigned int size); diff -r 975074f04b95 -r d8874c8749ec dvbsub.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dvbsub.c Fri Jun 03 13:59:38 2005 +0000 @@ -0,0 +1,407 @@ +/* + * DVB subtitle encoding for ffmpeg + * Copyright (c) 2005 Fabrice Bellard. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#include "avcodec.h" + +typedef struct DVBSubtitleContext { + int hide_state; + int object_version; +} DVBSubtitleContext; + +#define PUTBITS2(val)\ +{\ + bitbuf |= (val) << bitcnt;\ + bitcnt -= 2;\ + if (bitcnt < 0) {\ + bitcnt = 6;\ + *q++ = bitbuf;\ + bitbuf = 0;\ + }\ +} + +static void dvb_encode_rle2(uint8_t **pq, + const uint8_t *bitmap, int linesize, + int w, int h) +{ + uint8_t *q; + unsigned int bitbuf; + int bitcnt; + int x, y, len, x1, v, color; + + q = *pq; + + for(y = 0; y < h; y++) { + *q++ = 0x10; + bitbuf = 0; + bitcnt = 6; + + x = 0; + while (x < w) { + x1 = x; + color = bitmap[x1++]; + while (x1 < w && bitmap[x1] == color) + x1++; + len = x1 - x; + if (color == 0 && len == 2) { + PUTBITS2(0); + PUTBITS2(0); + PUTBITS2(1); + } else if (len >= 3 && len <= 10) { + v = len - 3; + PUTBITS2(0); + PUTBITS2((v >> 2) | 2); + PUTBITS2(v & 3); + PUTBITS2(color); + } else if (len >= 12 && len <= 27) { + v = len - 12; + PUTBITS2(0); + PUTBITS2(0); + PUTBITS2(2); + PUTBITS2(v >> 2); + PUTBITS2(v & 3); + PUTBITS2(color); + } else if (len >= 29) { + /* length = 29 ... 284 */ + if (len > 284) + len = 284; + v = len - 29; + PUTBITS2(0); + PUTBITS2(0); + PUTBITS2(3); + PUTBITS2((v >> 6)); + PUTBITS2((v >> 4) & 3); + PUTBITS2((v >> 2) & 3); + PUTBITS2(v & 3); + PUTBITS2(color); + } else { + PUTBITS2(color); + if (color == 0) { + PUTBITS2(1); + } + len = 1; + } + x += len; + } + /* end of line */ + PUTBITS2(0); + PUTBITS2(0); + PUTBITS2(0); + if (bitcnt != 6) { + *q++ = bitbuf; + } + *q++ = 0xf0; + bitmap += linesize; + } + *pq = q; +} + +#define PUTBITS4(val)\ +{\ + bitbuf |= (val) << bitcnt;\ + bitcnt -= 4;\ + if (bitcnt < 0) {\ + bitcnt = 4;\ + *q++ = bitbuf;\ + bitbuf = 0;\ + }\ +} + +/* some DVB decoders only implement 4 bits/pixel */ +static void dvb_encode_rle4(uint8_t **pq, + const uint8_t *bitmap, int linesize, + int w, int h) +{ + uint8_t *q; + unsigned int bitbuf; + int bitcnt; + int x, y, len, x1, v, color; + + q = *pq; + + for(y = 0; y < h; y++) { + *q++ = 0x11; + bitbuf = 0; + bitcnt = 4; + + x = 0; + while (x < w) { + x1 = x; + color = bitmap[x1++]; + while (x1 < w && bitmap[x1] == color) + x1++; + len = x1 - x; + if (color == 0 && len == 2) { + PUTBITS4(0); + PUTBITS4(0xd); + } else if (color == 0 && (len >= 3 && len <= 9)) { + PUTBITS4(0); + PUTBITS4(len - 2); + } else if (len >= 4 && len <= 7) { + PUTBITS4(0); + PUTBITS4(8 + len - 4); + PUTBITS4(color); + } else if (len >= 9 && len <= 24) { + PUTBITS4(0); + PUTBITS4(0xe); + PUTBITS4(len - 9); + PUTBITS4(color); + } else if (len >= 25) { + if (len > 280) + len = 280; + v = len - 25; + PUTBITS4(0); + PUTBITS4(0xf); + PUTBITS4(v >> 4); + PUTBITS4(v & 0xf); + PUTBITS4(color); + } else { + PUTBITS4(color); + if (color == 0) { + PUTBITS4(0xc); + } + len = 1; + } + x += len; + } + /* end of line */ + PUTBITS4(0); + PUTBITS4(0); + if (bitcnt != 4) { + *q++ = bitbuf; + } + *q++ = 0xf0; + bitmap += linesize; + } + *pq = q; +} + +#define SCALEBITS 10 +#define ONE_HALF (1 << (SCALEBITS - 1)) +#define FIX(x) ((int) ((x) * (1<> SCALEBITS) + +#define RGB_TO_U_CCIR(r1, g1, b1, shift)\ +(((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 + \ + FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128) + +#define RGB_TO_V_CCIR(r1, g1, b1, shift)\ +(((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 - \ + FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128) + +static inline void putbe16(uint8_t **pq, uint16_t v) +{ + uint8_t *q; + q = *pq; + *q++ = v >> 8; + *q++ = v; + *pq = q; +} + +static int encode_dvb_subtitles(DVBSubtitleContext *s, + uint8_t *outbuf, AVSubtitle *h) +{ + uint8_t *q, *pseg_len; + int page_id, region_id, clut_id, object_id, i, bpp_index, page_state; + + + q = outbuf; + + page_id = 1; + region_id = 0; + clut_id = 0; + object_id = 0; + if (h->nb_colors <= 4) { + /* 2 bpp, some decoders do not support it correctly */ + bpp_index = 0; + } else if (h->nb_colors <= 16) { + /* 4 bpp, standard encoding */ + bpp_index = 1; + } else { + return -1; + } + + *q++ = 0x00; /* subtitle_stream_id */ + + /* page composition segment */ + + *q++ = 0x0f; /* sync_byte */ + *q++ = 0x10; /* segment_type */ + putbe16(&q, page_id); + pseg_len = q; + q += 2; /* segment length */ + *q++ = 30; /* page_timeout (seconds) */ + if (s->hide_state) + page_state = 0; /* normal case */ + else + page_state = 2; /* mode change */ + /* page_version = 0 + page_state */ + *q++ = s->object_version | (page_state << 2) | 3; + *q++ = region_id; + *q++ = 0xff; /* reserved */ + putbe16(&q, 0); /* left pos */ + putbe16(&q, 0); /* top pos */ + + putbe16(&pseg_len, q - pseg_len - 2); + + if (!s->hide_state) { + /* CLUT segment */ + + *q++ = 0x0f; /* sync byte */ + *q++ = 0x12; /* CLUT definition segment */ + putbe16(&q, page_id); + pseg_len = q; + q += 2; /* segment length */ + *q++ = clut_id; + *q++ = (0 << 4) | 0xf; /* version = 0 */ + + for(i = 0; i < h->nb_colors; i++) { + *q++ = i; /* clut_entry_id */ + *q++ = (1 << (7 - bpp_index)) | (0xf << 1) | 1; /* 2 bits/pixel full range */ + { + int a, r, g, b; + a = (h->rgba_palette[i] >> 24) & 0xff; + r = (h->rgba_palette[i] >> 16) & 0xff; + g = (h->rgba_palette[i] >> 8) & 0xff; + b = (h->rgba_palette[i] >> 0) & 0xff; + + *q++ = RGB_TO_Y_CCIR(r, g, b); + *q++ = RGB_TO_V_CCIR(r, g, b, 0); + *q++ = RGB_TO_U_CCIR(r, g, b, 0); + *q++ = 255 - a; + } + } + + putbe16(&pseg_len, q - pseg_len - 2); + } + /* region composition segment */ + + *q++ = 0x0f; /* sync_byte */ + *q++ = 0x11; /* segment_type */ + putbe16(&q, page_id); + pseg_len = q; + q += 2; /* segment length */ + *q++ = region_id; + *q++ = (s->object_version << 4) | (0 << 3) | 0x07; /* version , no fill */ + putbe16(&q, 720); /* region width */ + putbe16(&q, 576); /* region height */ + *q++ = ((1 + bpp_index) << 5) | ((1 + bpp_index) << 2) | 0x03; + *q++ = clut_id; + *q++ = 0; /* 8 bit fill colors */ + *q++ = 0x03; /* 4 bit and 2 bit fill colors */ + + if (!s->hide_state) { + putbe16(&q, object_id); + *q++ = (0 << 6) | (0 << 4) | ((h->x >> 8) & 0xf); + *q++ = h->x; + *q++ = 0xf0 | ((h->y >> 8) & 0xf); + *q++ = h->y; + } + + putbe16(&pseg_len, q - pseg_len - 2); + + if (!s->hide_state) { + + /* Object Data segment */ + + *q++ = 0x0f; /* sync byte */ + *q++ = 0x13; + putbe16(&q, page_id); + pseg_len = q; + q += 2; /* segment length */ + + putbe16(&q, object_id); + *q++ = (s->object_version << 4) | (0 << 2) | (0 << 1) | 1; /* version = 0, + onject_coding_method, + non_modifying_color_flag */ + { + uint8_t *ptop_field_len, *pbottom_field_len, *top_ptr, *bottom_ptr; + void (*dvb_encode_rle)(uint8_t **pq, + const uint8_t *bitmap, int linesize, + int w, int h); + ptop_field_len = q; + q += 2; + pbottom_field_len = q; + q += 2; + + if (bpp_index == 0) + dvb_encode_rle = dvb_encode_rle2; + else + dvb_encode_rle = dvb_encode_rle4; + + top_ptr = q; + dvb_encode_rle(&q, h->bitmap, h->w * 2, h->w, h->h >> 1); + bottom_ptr = q; + dvb_encode_rle(&q, h->bitmap + h->w, h->w * 2, h->w, h->h >> 1); + + putbe16(&ptop_field_len, bottom_ptr - top_ptr); + putbe16(&pbottom_field_len, q - bottom_ptr); + } + + putbe16(&pseg_len, q - pseg_len - 2); + } + + /* end of display set segment */ + + *q++ = 0x0f; /* sync_byte */ + *q++ = 0x80; /* segment_type */ + putbe16(&q, page_id); + pseg_len = q; + q += 2; /* segment length */ + + putbe16(&pseg_len, q - pseg_len - 2); + + *q++ = 0xff; /* end of PES data */ + + s->object_version = (s->object_version + 1) & 0xf; + s->hide_state = !s->hide_state; + return q - outbuf; +} + +static int dvbsub_init_decoder(AVCodecContext *avctx) +{ + return 0; +} + +static int dvbsub_close_decoder(AVCodecContext *avctx) +{ + return 0; +} + +static int dvbsub_encode(AVCodecContext *avctx, + unsigned char *buf, int buf_size, void *data) +{ + DVBSubtitleContext *s = avctx->priv_data; + AVSubtitle *sub = data; + int ret; + + ret = encode_dvb_subtitles(s, buf, sub); + return ret; +} + +AVCodec dvbsub_encoder = { + "dvbsub", + CODEC_TYPE_SUBTITLE, + CODEC_ID_DVB_SUBTITLE, + sizeof(DVBSubtitleContext), + dvbsub_init_decoder, + dvbsub_encode, + dvbsub_close_decoder, +}; diff -r 975074f04b95 -r d8874c8749ec dvdsub.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dvdsub.c Fri Jun 03 13:59:38 2005 +0000 @@ -0,0 +1,459 @@ +/* + * DVD subtitle decoding for ffmpeg + * Copyright (c) 2005 Fabrice Bellard. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#include "avcodec.h" + +//#define DEBUG + +typedef struct DVDSubContext { +} DVDSubContext; + +static int dvdsub_init_decoder(AVCodecContext *avctx) +{ + return 0; +} + +uint16_t getbe16(const uint8_t *p) +{ + return (p[0] << 8) | p[1]; +} + +int get_nibble(const uint8_t *buf, int nibble_offset) +{ + return (buf[nibble_offset >> 1] >> ((1 - (nibble_offset & 1)) << 2)) & 0xf; +} + +static int decode_rle(uint8_t *bitmap, int linesize, int w, int h, + const uint8_t *buf, int nibble_offset, int buf_size) +{ + unsigned int v; + int x, y, len, color, nibble_end; + uint8_t *d; + + nibble_end = buf_size * 2; + x = 0; + y = 0; + d = bitmap; + for(;;) { + if (nibble_offset >= nibble_end) + return -1; + v = get_nibble(buf, nibble_offset++); + if (v < 0x4) { + v = (v << 4) | get_nibble(buf, nibble_offset++); + if (v < 0x10) { + v = (v << 4) | get_nibble(buf, nibble_offset++); + if (v < 0x040) { + v = (v << 4) | get_nibble(buf, nibble_offset++); + if (v < 4) { + v |= (w - x) << 2; + } + } + } + } + len = v >> 2; + if (len > (w - x)) + len = (w - x); + color = v & 0x03; + memset(d + x, color, len); + x += len; + if (x >= w) { + y++; + if (y >= h) + break; + d += linesize; + x = 0; + /* byte align */ + nibble_offset += (nibble_offset & 1); + } + } + return 0; +} + +static void guess_palette(uint32_t *rgba_palette, + uint8_t *palette, + uint8_t *alpha, + uint32_t subtitle_color) +{ + uint8_t color_used[16]; + int nb_opaque_colors, i, level, j, r, g, b; + + for(i = 0; i < 4; i++) + rgba_palette[i] = 0; + + memset(color_used, 0, 16); + nb_opaque_colors = 0; + for(i = 0; i < 4; i++) { + if (alpha[i] != 0 && !color_used[palette[i]]) { + color_used[palette[i]] = 1; + nb_opaque_colors++; + } + } + + if (nb_opaque_colors == 0) + return; + + j = 0; + memset(color_used, 0, 16); + for(i = 0; i < 4; i++) { + if (alpha[i] != 0) { + if (!color_used[palette[i]]) { + level = (0xff * (j + 1)) / nb_opaque_colors; + r = (((subtitle_color >> 16) & 0xff) * level) >> 8; + g = (((subtitle_color >> 8) & 0xff) * level) >> 8; + b = (((subtitle_color >> 0) & 0xff) * level) >> 8; + rgba_palette[i] = b | (g << 8) | (r << 16) | (0xff << 24); + color_used[palette[i]] = (i + 1); + j++; + } else { + rgba_palette[i] = rgba_palette[color_used[palette[i]] - 1]; + } + } + } +} + +static int decode_dvd_subtitles(AVSubtitle *sub_header, + const uint8_t *buf, int buf_size) +{ + int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos; + uint8_t palette[4], alpha[4]; + int date; + + if (buf_size < 4) + return -1; + sub_header->bitmap = NULL; + sub_header->rgba_palette = NULL; + sub_header->start_display_time = 0; + sub_header->end_display_time = 0; + + cmd_pos = getbe16(buf + 2); + while ((cmd_pos + 4) < buf_size) { + date = getbe16(buf + cmd_pos); + next_cmd_pos = getbe16(buf + cmd_pos + 2); +#ifdef DEBUG + av_log(NULL, AV_LOG_INFO, "cmd_pos=0x%04x next=0x%04x date=%d\n", + cmd_pos, next_cmd_pos, date); +#endif + pos = cmd_pos + 4; + offset1 = -1; + offset2 = -1; + x1 = y1 = x2 = y2 = 0; + while (pos < buf_size) { + cmd = buf[pos++]; +#ifdef DEBUG + av_log(NULL, AV_LOG_INFO, "cmd=%02x\n", cmd); +#endif + switch(cmd) { + case 0x00: + /* force display */ + break; + case 0x01: + /* set start date */ + sub_header->start_display_time = date * 10; + break; + case 0x02: + /* set end date */ + sub_header->end_display_time = date * 10; + break; + case 0x03: + /* set palette */ + if ((buf_size - pos) < 2) + goto fail; + palette[0] = buf[pos] >> 4; + palette[1] = buf[pos] & 0x0f; + palette[2] = buf[pos + 1] >> 4; + palette[3] = buf[pos + 1] & 0x0f; + pos += 2; + break; + case 0x04: + /* set alpha */ + if ((buf_size - pos) < 2) + goto fail; + alpha[0] = buf[pos] >> 4; + alpha[1] = buf[pos] & 0x0f; + alpha[2] = buf[pos + 1] >> 4; + alpha[3] = buf[pos + 1] & 0x0f; + pos += 2; + break; + case 0x05: + if ((buf_size - pos) < 6) + goto fail; + x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4); + x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2]; + y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4); + y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5]; +#ifdef DEBUG + av_log(NULL, AV_LOG_INFO, "x1=%d x2=%d y1=%d y2=%d\n", + x1, x2, y1, y2); +#endif + pos += 6; + break; + case 0x06: + if ((buf_size - pos) < 4) + goto fail; + offset1 = getbe16(buf + pos); + offset2 = getbe16(buf + pos + 2); +#ifdef DEBUG + av_log(NULL, AV_LOG_INFO, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2); +#endif + pos += 4; + break; + case 0xff: + default: + goto the_end; + } + } + the_end: + if (offset1 >= 0) { + int w, h; + uint8_t *bitmap; + + /* decode the bitmap */ + w = x2 - x1 + 1; + if (w < 0) + w = 0; + h = y2 - y1; + if (h < 0) + h = 0; + if (w > 0 && h > 0) { + av_freep(&sub_header->bitmap); + av_freep(&sub_header->rgba_palette); + bitmap = av_malloc(w * h); + sub_header->rgba_palette = av_malloc(4 * 4); + decode_rle(bitmap, w * 2, w, h / 2, + buf, offset1 * 2, buf_size); + decode_rle(bitmap + w, w * 2, w, h / 2, + buf, offset2 * 2, buf_size); + guess_palette(sub_header->rgba_palette, + palette, alpha, 0xffff00); + sub_header->x = x1; + sub_header->y = y1; + sub_header->w = w; + sub_header->h = h; + sub_header->nb_colors = 4; + sub_header->linesize = w; + sub_header->bitmap = bitmap; + } + } + if (next_cmd_pos == cmd_pos) + break; + cmd_pos = next_cmd_pos; + } + if (sub_header->bitmap) + return 0; + fail: + return -1; +} + +static int is_transp(const uint8_t *buf, int pitch, int n, + const uint8_t *transp_color) +{ + int i; + for(i = 0; i < n; i++) { + if (!transp_color[*buf]) + return 0; + buf += pitch; + } + return 1; +} + +/* return 0 if empty rectangle, 1 if non empty */ +static int find_smallest_bouding_rectangle(AVSubtitle *s) +{ + uint8_t transp_color[256]; + int y1, y2, x1, x2, y, w, h, i; + uint8_t *bitmap; + + if (s->w <= 0 || s->h <= 0) + return 0; + + memset(transp_color, 0, 256); + for(i = 0; i < s->nb_colors; i++) { + if ((s->rgba_palette[i] >> 24) == 0) + transp_color[i] = 1; + } + y1 = 0; + while (y1 < s->h && is_transp(s->bitmap + y1 * s->linesize, 1, s->w, + transp_color)) + y1++; + if (y1 == s->h) { + av_freep(&s->bitmap); + s->w = s->h = 0; + return 0; + } + + y2 = s->h - 1; + while (y2 > 0 && is_transp(s->bitmap + y2 * s->linesize, 1, s->w, + transp_color)) + y2--; + x1 = 0; + while (x1 < (s->w - 1) && is_transp(s->bitmap + x1, s->linesize, s->h, + transp_color)) + x1++; + x2 = s->w - 1; + while (x2 > 0 && is_transp(s->bitmap + x2, s->linesize, s->h, + transp_color)) + x2--; + w = x2 - x1 + 1; + h = y2 - y1 + 1; + bitmap = av_malloc(w * h); + if (!bitmap) + return 1; + for(y = 0; y < h; y++) { + memcpy(bitmap + w * y, s->bitmap + x1 + (y1 + y) * s->linesize, w); + } + av_freep(&s->bitmap); + s->bitmap = bitmap; + s->linesize = w; + s->w = w; + s->h = h; + s->x += x1; + s->y += y1; + return 1; +} + +static int dvdsub_close_decoder(AVCodecContext *avctx) +{ + return 0; +} + +#ifdef DEBUG +#undef fprintf +static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h, + uint32_t *rgba_palette) +{ + int x, y, v; + FILE *f; + + f = fopen(filename, "w"); + if (!f) { + perror(filename); + exit(1); + } + 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 = rgba_palette[bitmap[y * w + x]]; + putc((v >> 16) & 0xff, f); + putc((v >> 8) & 0xff, f); + putc((v >> 0) & 0xff, f); + } + } + fclose(f); +} +#endif + +static int dvdsub_decode(AVCodecContext *avctx, + void *data, int *data_size, + uint8_t *buf, int buf_size) +{ + AVSubtitle *sub = (void *)data; + + if (decode_dvd_subtitles(sub, buf, buf_size) < 0) { + no_subtitle: + *data_size = 0; + + return buf_size; + } + if (find_smallest_bouding_rectangle(sub) == 0) + goto no_subtitle; + +#if defined(DEBUG) + av_log(NULL, AV_LOG_INFO, "start=%d ms end =%d ms\n", + sub->start_display_time, + sub->end_display_time); + ppm_save("/tmp/a.ppm", sub->bitmap, + sub->w, sub->h, sub->rgba_palette); +#endif + + *data_size = 1; + return buf_size; +} + +AVCodec dvdsub_decoder = { + "dvdsub", + CODEC_TYPE_SUBTITLE, + CODEC_ID_DVD_SUBTITLE, + sizeof(DVDSubContext), + dvdsub_init_decoder, + NULL, + dvdsub_close_decoder, + dvdsub_decode, +}; + +/* parser definition */ +typedef struct DVDSubParseContext { + uint8_t *packet; + int packet_len; + int packet_index; +} DVDSubParseContext; + +static int dvdsub_parse_init(AVCodecParserContext *s) +{ + return 0; +} + +static int dvdsub_parse(AVCodecParserContext *s, + AVCodecContext *avctx, + uint8_t **poutbuf, int *poutbuf_size, + const uint8_t *buf, int buf_size) +{ + DVDSubParseContext *pc = s->priv_data; + + if (pc->packet_index == 0) { + if (buf_size < 2) + return 0; + pc->packet_len = (buf[0] << 8) | buf[1]; + av_freep(&pc->packet); + pc->packet = av_malloc(pc->packet_len); + } + if (pc->packet) { + if (pc->packet_index + buf_size <= pc->packet_len) { + memcpy(pc->packet + pc->packet_index, buf, buf_size); + pc->packet_index += buf_size; + if (pc->packet_index >= pc->packet_len) { + *poutbuf = pc->packet; + *poutbuf_size = pc->packet_len; + pc->packet_index = 0; + return buf_size; + } + } else { + /* erroneous size */ + pc->packet_index = 0; + } + } + *poutbuf = NULL; + *poutbuf_size = 0; + return buf_size; +} + +static void dvdsub_parse_close(AVCodecParserContext *s) +{ + DVDSubParseContext *pc = s->priv_data; + av_freep(&pc->packet); +} + +AVCodecParser dvdsub_parser = { + { CODEC_ID_DVD_SUBTITLE }, + sizeof(DVDSubParseContext), + dvdsub_parse_init, + dvdsub_parse, + dvdsub_parse_close, +}; diff -r 975074f04b95 -r d8874c8749ec utils.c --- a/utils.c Thu Jun 02 21:15:20 2005 +0000 +++ b/utils.c Fri Jun 03 13:59:38 2005 +0000 @@ -590,6 +590,15 @@ return 0; } +int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size, + const AVSubtitle *sub) +{ + int ret; + ret = avctx->codec->encode(avctx, buf, buf_size, (void *)sub); + avctx->frame_number++; + return ret; +} + /** * decode a frame. * @param buf bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE larger then the actual read bytes @@ -639,6 +648,23 @@ return ret; } +/* decode a subtitle message. return -1 if error, otherwise return the + *number of bytes used. If no subtitle could be decompressed, + *got_sub_ptr is zero. Otherwise, the subtitle is stored in *sub. */ +int avcodec_decode_subtitle(AVCodecContext *avctx, AVSubtitle *sub, + int *got_sub_ptr, + const uint8_t *buf, int buf_size) +{ + int ret; + + *got_sub_ptr = 0; + ret = avctx->codec->decode(avctx, sub, got_sub_ptr, + (uint8_t *)buf, buf_size); + if (*got_sub_ptr) + avctx->frame_number++; + return ret; +} + int avcodec_close(AVCodecContext *avctx) { if (avctx->codec->close) @@ -808,6 +834,10 @@ snprintf(buf, buf_size, "Data: %s", codec_name); bitrate = enc->bit_rate; break; + case CODEC_TYPE_SUBTITLE: + snprintf(buf, buf_size, "Subtitle: %s", codec_name); + bitrate = enc->bit_rate; + break; default: snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type); return;