yading@10: /* yading@10: * XSUB subtitle decoder yading@10: * Copyright (c) 2007 Reimar Döffinger yading@10: * yading@10: * This file is part of FFmpeg. yading@10: * yading@10: * FFmpeg is free software; you can redistribute it and/or yading@10: * modify it under the terms of the GNU Lesser General Public yading@10: * License as published by the Free Software Foundation; either yading@10: * version 2.1 of the License, or (at your option) any later version. yading@10: * yading@10: * FFmpeg is distributed in the hope that it will be useful, yading@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@10: * Lesser General Public License for more details. yading@10: * yading@10: * You should have received a copy of the GNU Lesser General Public yading@10: * License along with FFmpeg; if not, write to the Free Software yading@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@10: */ yading@10: yading@10: #include "libavutil/mathematics.h" yading@10: #include "libavutil/imgutils.h" yading@10: #include "avcodec.h" yading@10: #include "get_bits.h" yading@10: #include "bytestream.h" yading@10: yading@10: static av_cold int decode_init(AVCodecContext *avctx) { yading@10: avctx->pix_fmt = AV_PIX_FMT_PAL8; yading@10: return 0; yading@10: } yading@10: yading@10: static const uint8_t tc_offsets[9] = { 0, 1, 3, 4, 6, 7, 9, 10, 11 }; yading@10: static const uint8_t tc_muls[9] = { 10, 6, 10, 6, 10, 10, 10, 10, 1 }; yading@10: yading@10: static int64_t parse_timecode(const uint8_t *buf, int64_t packet_time) { yading@10: int i; yading@10: int64_t ms = 0; yading@10: if (buf[2] != ':' || buf[5] != ':' || buf[8] != '.') yading@10: return AV_NOPTS_VALUE; yading@10: for (i = 0; i < sizeof(tc_offsets); i++) { yading@10: uint8_t c = buf[tc_offsets[i]] - '0'; yading@10: if (c > 9) return AV_NOPTS_VALUE; yading@10: ms = (ms + c) * tc_muls[i]; yading@10: } yading@10: return ms - packet_time; yading@10: } yading@10: yading@10: static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, yading@10: AVPacket *avpkt) { yading@10: const uint8_t *buf = avpkt->data; yading@10: int buf_size = avpkt->size; yading@10: AVSubtitle *sub = data; yading@10: const uint8_t *buf_end = buf + buf_size; yading@10: uint8_t *bitmap; yading@10: int w, h, x, y, i; yading@10: int64_t packet_time = 0; yading@10: GetBitContext gb; yading@10: int has_alpha = avctx->codec_tag == MKTAG('D','X','S','A'); yading@10: yading@10: // check that at least header fits yading@10: if (buf_size < 27 + 7 * 2 + 4 * 3) { yading@10: av_log(avctx, AV_LOG_ERROR, "coded frame size %d too small\n", buf_size); yading@10: return -1; yading@10: } yading@10: yading@10: // read start and end time yading@10: if (buf[0] != '[' || buf[13] != '-' || buf[26] != ']') { yading@10: av_log(avctx, AV_LOG_ERROR, "invalid time code\n"); yading@10: return -1; yading@10: } yading@10: if (avpkt->pts != AV_NOPTS_VALUE) yading@10: packet_time = av_rescale_q(avpkt->pts, AV_TIME_BASE_Q, (AVRational){1, 1000}); yading@10: sub->start_display_time = parse_timecode(buf + 1, packet_time); yading@10: sub->end_display_time = parse_timecode(buf + 14, packet_time); yading@10: buf += 27; yading@10: yading@10: // read header yading@10: w = bytestream_get_le16(&buf); yading@10: h = bytestream_get_le16(&buf); yading@10: if (av_image_check_size(w, h, 0, avctx) < 0) yading@10: return -1; yading@10: x = bytestream_get_le16(&buf); yading@10: y = bytestream_get_le16(&buf); yading@10: // skip bottom right position, it gives no new information yading@10: bytestream_get_le16(&buf); yading@10: bytestream_get_le16(&buf); yading@10: // The following value is supposed to indicate the start offset yading@10: // (relative to the palette) of the data for the second field, yading@10: // however there are files in which it has a bogus value and thus yading@10: // we just ignore it yading@10: bytestream_get_le16(&buf); yading@10: yading@10: // allocate sub and set values yading@10: sub->rects = av_mallocz(sizeof(*sub->rects)); yading@10: sub->rects[0] = av_mallocz(sizeof(*sub->rects[0])); yading@10: sub->num_rects = 1; yading@10: sub->rects[0]->x = x; sub->rects[0]->y = y; yading@10: sub->rects[0]->w = w; sub->rects[0]->h = h; yading@10: sub->rects[0]->type = SUBTITLE_BITMAP; yading@10: sub->rects[0]->pict.linesize[0] = w; yading@10: sub->rects[0]->pict.data[0] = av_malloc(w * h); yading@10: sub->rects[0]->nb_colors = 4; yading@10: sub->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE); yading@10: yading@10: // read palette yading@10: for (i = 0; i < sub->rects[0]->nb_colors; i++) yading@10: ((uint32_t*)sub->rects[0]->pict.data[1])[i] = bytestream_get_be24(&buf); yading@10: // make all except background (first entry) non-transparent yading@10: for (i = 0; i < sub->rects[0]->nb_colors; i++) yading@10: ((uint32_t*)sub->rects[0]->pict.data[1])[i] |= (has_alpha ? *buf++ : (i ? 0xff : 0)) << 24; yading@10: yading@10: // process RLE-compressed data yading@10: init_get_bits(&gb, buf, (buf_end - buf) * 8); yading@10: bitmap = sub->rects[0]->pict.data[0]; yading@10: for (y = 0; y < h; y++) { yading@10: // interlaced: do odd lines yading@10: if (y == (h + 1) / 2) bitmap = sub->rects[0]->pict.data[0] + w; yading@10: for (x = 0; x < w; ) { yading@10: int log2 = ff_log2_tab[show_bits(&gb, 8)]; yading@10: int run = get_bits(&gb, 14 - 4 * (log2 >> 1)); yading@10: int color = get_bits(&gb, 2); yading@10: run = FFMIN(run, w - x); yading@10: // run length 0 means till end of row yading@10: if (!run) run = w - x; yading@10: memset(bitmap, color, run); yading@10: bitmap += run; yading@10: x += run; yading@10: } yading@10: // interlaced, skip every second line yading@10: bitmap += w; yading@10: align_get_bits(&gb); yading@10: } yading@10: *data_size = 1; yading@10: return buf_size; yading@10: } yading@10: yading@10: AVCodec ff_xsub_decoder = { yading@10: .name = "xsub", yading@10: .type = AVMEDIA_TYPE_SUBTITLE, yading@10: .id = AV_CODEC_ID_XSUB, yading@10: .init = decode_init, yading@10: .decode = decode_frame, yading@10: .long_name = NULL_IF_CONFIG_SMALL("XSUB"), yading@10: };