yading@10: /* yading@10: * DivX (XSUB) subtitle encoder yading@10: * Copyright (c) 2005 DivX, Inc. yading@10: * Copyright (c) 2009 Bjorn Axelsson 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 "avcodec.h" yading@10: #include "bytestream.h" yading@10: #include "put_bits.h" yading@10: yading@10: /** yading@10: * Number of pixels to pad left and right. yading@10: * yading@10: * The official encoder pads the subtitles with two pixels on either side, yading@10: * but until we find out why, we won't do it (we will pad to have width yading@10: * divisible by 2 though). yading@10: */ yading@10: #define PADDING 0 yading@10: #define PADDING_COLOR 0 yading@10: yading@10: /** yading@10: * Encode a single color run. At most 16 bits will be used. yading@10: * @param len length of the run, values > 255 mean "until end of line", may not be < 0. yading@10: * @param color color to encode, only the lowest two bits are used and all others must be 0. yading@10: */ yading@10: static void put_xsub_rle(PutBitContext *pb, int len, int color) yading@10: { yading@10: if (len <= 255) yading@10: put_bits(pb, 2 + ((ff_log2_tab[len] >> 1) << 2), len); yading@10: else yading@10: put_bits(pb, 14, 0); yading@10: put_bits(pb, 2, color); yading@10: } yading@10: yading@10: /** yading@10: * Encode a 4-color bitmap with XSUB rle. yading@10: * yading@10: * The encoded bitmap may be wider than the source bitmap due to padding. yading@10: */ yading@10: static int xsub_encode_rle(PutBitContext *pb, const uint8_t *bitmap, yading@10: int linesize, int w, int h) yading@10: { yading@10: int x0, x1, y, len, color = PADDING_COLOR; yading@10: yading@10: for (y = 0; y < h; y++) { yading@10: x0 = 0; yading@10: while (x0 < w) { yading@10: // Make sure we have enough room for at least one run and padding yading@10: if (pb->size_in_bits - put_bits_count(pb) < 7*8) yading@10: return -1; yading@10: yading@10: x1 = x0; yading@10: color = bitmap[x1++] & 3; yading@10: while (x1 < w && (bitmap[x1] & 3) == color) yading@10: x1++; yading@10: len = x1 - x0; yading@10: if (PADDING && x0 == 0) { yading@10: if (color == PADDING_COLOR) { yading@10: len += PADDING; yading@10: x0 -= PADDING; yading@10: } else yading@10: put_xsub_rle(pb, PADDING, PADDING_COLOR); yading@10: } yading@10: yading@10: // Run can't be longer than 255, unless it is the rest of a row yading@10: if (x1 == w && color == PADDING_COLOR) { yading@10: len += PADDING + (w&1); yading@10: } else yading@10: len = FFMIN(len, 255); yading@10: put_xsub_rle(pb, len, color); yading@10: yading@10: x0 += len; yading@10: } yading@10: if (color != PADDING_COLOR && (PADDING + (w&1))) yading@10: put_xsub_rle(pb, PADDING + (w&1), PADDING_COLOR); yading@10: yading@10: avpriv_align_put_bits(pb); yading@10: yading@10: bitmap += linesize; yading@10: } yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: static int make_tc(uint64_t ms, int *tc) yading@10: { yading@10: static const int tc_divs[3] = { 1000, 60, 60 }; yading@10: int i; yading@10: for (i=0; i<3; i++) { yading@10: tc[i] = ms % tc_divs[i]; yading@10: ms /= tc_divs[i]; yading@10: } yading@10: tc[3] = ms; yading@10: return ms > 99; yading@10: } yading@10: yading@10: static int xsub_encode(AVCodecContext *avctx, unsigned char *buf, yading@10: int bufsize, const AVSubtitle *h) yading@10: { yading@10: uint64_t startTime = h->pts / 1000; // FIXME: need better solution... yading@10: uint64_t endTime = startTime + h->end_display_time - h->start_display_time; yading@10: int start_tc[4], end_tc[4]; yading@10: uint8_t *hdr = buf + 27; // Point behind the timestamp yading@10: uint8_t *rlelenptr; yading@10: uint16_t width, height; yading@10: int i; yading@10: PutBitContext pb; yading@10: yading@10: if (bufsize < 27 + 7*2 + 4*3) { yading@10: av_log(avctx, AV_LOG_ERROR, "Buffer too small for XSUB header.\n"); yading@10: return -1; yading@10: } yading@10: yading@10: // TODO: support multiple rects yading@10: if (h->num_rects != 1) yading@10: av_log(avctx, AV_LOG_WARNING, "Only single rects supported (%d in subtitle.)\n", h->num_rects); yading@10: yading@10: // TODO: render text-based subtitles into bitmaps yading@10: if (!h->rects[0]->pict.data[0] || !h->rects[0]->pict.data[1]) { yading@10: av_log(avctx, AV_LOG_WARNING, "No subtitle bitmap available.\n"); yading@10: return -1; yading@10: } yading@10: yading@10: // TODO: color reduction, similar to dvdsub encoder yading@10: if (h->rects[0]->nb_colors > 4) yading@10: av_log(avctx, AV_LOG_WARNING, "No more than 4 subtitle colors supported (%d found.)\n", h->rects[0]->nb_colors); yading@10: yading@10: // TODO: Palette swapping if color zero is not transparent yading@10: if (((uint32_t *)h->rects[0]->pict.data[1])[0] & 0xff) yading@10: av_log(avctx, AV_LOG_WARNING, "Color index 0 is not transparent. Transparency will be messed up.\n"); yading@10: yading@10: if (make_tc(startTime, start_tc) || make_tc(endTime, end_tc)) { yading@10: av_log(avctx, AV_LOG_WARNING, "Time code >= 100 hours.\n"); yading@10: return -1; yading@10: } yading@10: yading@10: snprintf(buf, 28, yading@10: "[%02d:%02d:%02d.%03d-%02d:%02d:%02d.%03d]", yading@10: start_tc[3], start_tc[2], start_tc[1], start_tc[0], yading@10: end_tc[3], end_tc[2], end_tc[1], end_tc[0]); yading@10: yading@10: // Width and height must probably be multiples of 2. yading@10: // 2 pixels required on either side of subtitle. yading@10: // Possibly due to limitations of hardware renderers. yading@10: // TODO: check if the bitmap is already padded yading@10: width = FFALIGN(h->rects[0]->w, 2) + PADDING * 2; yading@10: height = FFALIGN(h->rects[0]->h, 2); yading@10: yading@10: bytestream_put_le16(&hdr, width); yading@10: bytestream_put_le16(&hdr, height); yading@10: bytestream_put_le16(&hdr, h->rects[0]->x); yading@10: bytestream_put_le16(&hdr, h->rects[0]->y); yading@10: bytestream_put_le16(&hdr, h->rects[0]->x + width); yading@10: bytestream_put_le16(&hdr, h->rects[0]->y + height); yading@10: yading@10: rlelenptr = hdr; // Will store length of first field here later. yading@10: hdr+=2; yading@10: yading@10: // Palette yading@10: for (i=0; i<4; i++) yading@10: bytestream_put_be24(&hdr, ((uint32_t *)h->rects[0]->pict.data[1])[i]); yading@10: yading@10: // Bitmap yading@10: // RLE buffer. Reserve 2 bytes for possible padding after the last row. yading@10: init_put_bits(&pb, hdr, bufsize - (hdr - buf) - 2); yading@10: if (xsub_encode_rle(&pb, h->rects[0]->pict.data[0], yading@10: h->rects[0]->pict.linesize[0]*2, yading@10: h->rects[0]->w, (h->rects[0]->h + 1) >> 1)) yading@10: return -1; yading@10: bytestream_put_le16(&rlelenptr, put_bits_count(&pb) >> 3); // Length of first field yading@10: yading@10: if (xsub_encode_rle(&pb, h->rects[0]->pict.data[0] + h->rects[0]->pict.linesize[0], yading@10: h->rects[0]->pict.linesize[0]*2, yading@10: h->rects[0]->w, h->rects[0]->h >> 1)) yading@10: return -1; yading@10: yading@10: // Enforce total height to be be multiple of 2 yading@10: if (h->rects[0]->h & 1) { yading@10: put_xsub_rle(&pb, h->rects[0]->w, PADDING_COLOR); yading@10: avpriv_align_put_bits(&pb); yading@10: } yading@10: yading@10: flush_put_bits(&pb); yading@10: yading@10: return hdr - buf + put_bits_count(&pb)/8; yading@10: } yading@10: yading@10: static av_cold int xsub_encoder_init(AVCodecContext *avctx) yading@10: { yading@10: if (!avctx->codec_tag) yading@10: avctx->codec_tag = MKTAG('D','X','S','B'); yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: AVCodec ff_xsub_encoder = { yading@10: .name = "xsub", yading@10: .type = AVMEDIA_TYPE_SUBTITLE, yading@10: .id = AV_CODEC_ID_XSUB, yading@10: .init = xsub_encoder_init, yading@10: .encode_sub = xsub_encode, yading@10: .long_name = NULL_IF_CONFIG_SMALL("DivX subtitles (XSUB)"), yading@10: };