00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "avcodec.h"
00022 #include "bitstream.h"
00023 #include "bytestream.h"
00024
00025 static int decode_init(AVCodecContext *avctx) {
00026 avctx->pix_fmt = PIX_FMT_PAL8;
00027 return 0;
00028 }
00029
00030 static const uint8_t tc_offsets[9] = { 0, 1, 3, 4, 6, 7, 9, 10, 11 };
00031 static const uint8_t tc_muls[9] = { 10, 6, 10, 6, 10, 6, 10, 10, 1 };
00032
00033 static uint64_t parse_timecode(const uint8_t *buf) {
00034 int i;
00035 int64_t ms = 0;
00036 if (buf[2] != ':' || buf[5] != ':' || buf[8] != '.')
00037 return AV_NOPTS_VALUE;
00038 for (i = 0; i < sizeof(tc_offsets); i++) {
00039 uint8_t c = buf[tc_offsets[i]] - '0';
00040 if (c > 9) return AV_NOPTS_VALUE;
00041 ms = (ms + c) * tc_muls[i];
00042 }
00043 return ms;
00044 }
00045
00046 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00047 const uint8_t *buf, int buf_size) {
00048 AVSubtitle *sub = data;
00049 const uint8_t *buf_end = buf + buf_size;
00050 uint8_t *bitmap;
00051 int w, h, x, y, rlelen, i;
00052 GetBitContext gb;
00053
00054
00055 if (buf_size < 27 + 7 * 2 + 4 * 3) {
00056 av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
00057 return -1;
00058 }
00059
00060
00061 if (buf[0] != '[' || buf[13] != '-' || buf[26] != ']') {
00062 av_log(avctx, AV_LOG_ERROR, "invalid time code\n");
00063 return -1;
00064 }
00065 sub->start_display_time = parse_timecode(buf + 1);
00066 sub->end_display_time = parse_timecode(buf + 14);
00067 buf += 27;
00068
00069
00070 w = bytestream_get_le16(&buf);
00071 h = bytestream_get_le16(&buf);
00072 if (avcodec_check_dimensions(avctx, w, h) < 0)
00073 return -1;
00074 x = bytestream_get_le16(&buf);
00075 y = bytestream_get_le16(&buf);
00076
00077 bytestream_get_le16(&buf);
00078 bytestream_get_le16(&buf);
00079 rlelen = bytestream_get_le16(&buf);
00080
00081
00082 if (!sub->rects) {
00083 sub->rects = av_mallocz(sizeof(AVSubtitleRect));
00084 sub->num_rects = 1;
00085 }
00086 av_freep(&sub->rects[0].bitmap);
00087 sub->rects[0].x = x; sub->rects[0].y = y;
00088 sub->rects[0].w = w; sub->rects[0].h = h;
00089 sub->rects[0].linesize = w;
00090 sub->rects[0].bitmap = av_malloc(w * h);
00091 sub->rects[0].nb_colors = 4;
00092 sub->rects[0].rgba_palette = av_malloc(sub->rects[0].nb_colors * 4);
00093
00094
00095 for (i = 0; i < sub->rects[0].nb_colors; i++)
00096 sub->rects[0].rgba_palette[i] = bytestream_get_be24(&buf);
00097
00098 for (i = 1; i < sub->rects[0].nb_colors; i++)
00099 sub->rects[0].rgba_palette[i] |= 0xff000000;
00100
00101
00102 rlelen = FFMIN(rlelen, buf_end - buf);
00103 init_get_bits(&gb, buf, rlelen * 8);
00104 bitmap = sub->rects[0].bitmap;
00105 for (y = 0; y < h; y++) {
00106
00107 if (y == (h + 1) / 2) bitmap = sub->rects[0].bitmap + w;
00108 for (x = 0; x < w; ) {
00109 int log2 = ff_log2_tab[show_bits(&gb, 8)];
00110 int run = get_bits(&gb, 14 - 4 * (log2 >> 1));
00111 int color = get_bits(&gb, 2);
00112 run = FFMIN(run, w - x);
00113
00114 if (!run) run = w - x;
00115 memset(bitmap, color, run);
00116 bitmap += run;
00117 x += run;
00118 }
00119
00120 bitmap += w;
00121 align_get_bits(&gb);
00122 }
00123 *data_size = 1;
00124 return buf_size;
00125 }
00126
00127 AVCodec xsub_decoder = {
00128 "xsub",
00129 CODEC_TYPE_SUBTITLE,
00130 CODEC_ID_XSUB,
00131 0,
00132 decode_init,
00133 NULL,
00134 NULL,
00135 decode_frame,
00136 };