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