Libav 0.7.1
|
00001 /* 00002 * DivX (XSUB) subtitle encoder 00003 * Copyright (c) 2005 DivX, Inc. 00004 * Copyright (c) 2009 Bjorn Axelsson 00005 * 00006 * This file is part of Libav. 00007 * 00008 * Libav is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * Libav is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with Libav; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00023 #include "avcodec.h" 00024 #include "bytestream.h" 00025 #include "put_bits.h" 00026 00034 #define PADDING 0 00035 #define PADDING_COLOR 0 00036 00042 static void put_xsub_rle(PutBitContext *pb, int len, int color) 00043 { 00044 if (len <= 255) 00045 put_bits(pb, 2 + ((ff_log2_tab[len] >> 1) << 2), len); 00046 else 00047 put_bits(pb, 14, 0); 00048 put_bits(pb, 2, color); 00049 } 00050 00056 static int xsub_encode_rle(PutBitContext *pb, const uint8_t *bitmap, 00057 int linesize, int w, int h) 00058 { 00059 int x0, x1, y, len, color = PADDING_COLOR; 00060 00061 for (y = 0; y < h; y++) { 00062 x0 = 0; 00063 while (x0 < w) { 00064 // Make sure we have enough room for at least one run and padding 00065 if (pb->size_in_bits - put_bits_count(pb) < 7*8) 00066 return -1; 00067 00068 x1 = x0; 00069 color = bitmap[x1++] & 3; 00070 while (x1 < w && (bitmap[x1] & 3) == color) 00071 x1++; 00072 len = x1 - x0; 00073 if (PADDING && x0 == 0) { 00074 if (color == PADDING_COLOR) { 00075 len += PADDING; 00076 x0 -= PADDING; 00077 } else 00078 put_xsub_rle(pb, PADDING, PADDING_COLOR); 00079 } 00080 00081 // Run can't be longer than 255, unless it is the rest of a row 00082 if (x1 == w && color == PADDING_COLOR) { 00083 len += PADDING + (w&1); 00084 } else 00085 len = FFMIN(len, 255); 00086 put_xsub_rle(pb, len, color); 00087 00088 x0 += len; 00089 } 00090 if (color != PADDING_COLOR && (PADDING + (w&1))) 00091 put_xsub_rle(pb, PADDING + (w&1), PADDING_COLOR); 00092 00093 align_put_bits(pb); 00094 00095 bitmap += linesize; 00096 } 00097 00098 return 0; 00099 } 00100 00101 static int make_tc(uint64_t ms, int *tc) 00102 { 00103 static const int tc_divs[3] = { 1000, 60, 60 }; 00104 int i; 00105 for (i=0; i<3; i++) { 00106 tc[i] = ms % tc_divs[i]; 00107 ms /= tc_divs[i]; 00108 } 00109 tc[3] = ms; 00110 return ms > 99; 00111 } 00112 00113 static int xsub_encode(AVCodecContext *avctx, unsigned char *buf, 00114 int bufsize, void *data) 00115 { 00116 AVSubtitle *h = data; 00117 uint64_t startTime = h->pts / 1000; // FIXME: need better solution... 00118 uint64_t endTime = startTime + h->end_display_time - h->start_display_time; 00119 int start_tc[4], end_tc[4]; 00120 uint8_t *hdr = buf + 27; // Point behind the timestamp 00121 uint8_t *rlelenptr; 00122 uint16_t width, height; 00123 int i; 00124 PutBitContext pb; 00125 00126 if (bufsize < 27 + 7*2 + 4*3) { 00127 av_log(avctx, AV_LOG_ERROR, "Buffer too small for XSUB header.\n"); 00128 return -1; 00129 } 00130 00131 // TODO: support multiple rects 00132 if (h->num_rects > 1) 00133 av_log(avctx, AV_LOG_WARNING, "Only single rects supported (%d in subtitle.)\n", h->num_rects); 00134 00135 // TODO: render text-based subtitles into bitmaps 00136 if (!h->rects[0]->pict.data[0] || !h->rects[0]->pict.data[1]) { 00137 av_log(avctx, AV_LOG_WARNING, "No subtitle bitmap available.\n"); 00138 return -1; 00139 } 00140 00141 // TODO: color reduction, similar to dvdsub encoder 00142 if (h->rects[0]->nb_colors > 4) 00143 av_log(avctx, AV_LOG_WARNING, "No more than 4 subtitle colors supported (%d found.)\n", h->rects[0]->nb_colors); 00144 00145 // TODO: Palette swapping if color zero is not transparent 00146 if (((uint32_t *)h->rects[0]->pict.data[1])[0] & 0xff) 00147 av_log(avctx, AV_LOG_WARNING, "Color index 0 is not transparent. Transparency will be messed up.\n"); 00148 00149 if (make_tc(startTime, start_tc) || make_tc(endTime, end_tc)) { 00150 av_log(avctx, AV_LOG_WARNING, "Time code >= 100 hours.\n"); 00151 return -1; 00152 } 00153 00154 snprintf(buf, 28, 00155 "[%02d:%02d:%02d.%03d-%02d:%02d:%02d.%03d]", 00156 start_tc[3], start_tc[2], start_tc[1], start_tc[0], 00157 end_tc[3], end_tc[2], end_tc[1], end_tc[0]); 00158 00159 // Width and height must probably be multiples of 2. 00160 // 2 pixels required on either side of subtitle. 00161 // Possibly due to limitations of hardware renderers. 00162 // TODO: check if the bitmap is already padded 00163 width = FFALIGN(h->rects[0]->w, 2) + PADDING * 2; 00164 height = FFALIGN(h->rects[0]->h, 2); 00165 00166 bytestream_put_le16(&hdr, width); 00167 bytestream_put_le16(&hdr, height); 00168 bytestream_put_le16(&hdr, h->rects[0]->x); 00169 bytestream_put_le16(&hdr, h->rects[0]->y); 00170 bytestream_put_le16(&hdr, h->rects[0]->x + width); 00171 bytestream_put_le16(&hdr, h->rects[0]->y + height); 00172 00173 rlelenptr = hdr; // Will store length of first field here later. 00174 hdr+=2; 00175 00176 // Palette 00177 for (i=0; i<4; i++) 00178 bytestream_put_be24(&hdr, ((uint32_t *)h->rects[0]->pict.data[1])[i]); 00179 00180 // Bitmap 00181 // RLE buffer. Reserve 2 bytes for possible padding after the last row. 00182 init_put_bits(&pb, hdr, bufsize - (hdr - buf) - 2); 00183 if (xsub_encode_rle(&pb, h->rects[0]->pict.data[0], 00184 h->rects[0]->pict.linesize[0]*2, 00185 h->rects[0]->w, (h->rects[0]->h + 1) >> 1)) 00186 return -1; 00187 bytestream_put_le16(&rlelenptr, put_bits_count(&pb) >> 3); // Length of first field 00188 00189 if (xsub_encode_rle(&pb, h->rects[0]->pict.data[0] + h->rects[0]->pict.linesize[0], 00190 h->rects[0]->pict.linesize[0]*2, 00191 h->rects[0]->w, h->rects[0]->h >> 1)) 00192 return -1; 00193 00194 // Enforce total height to be be multiple of 2 00195 if (h->rects[0]->h & 1) { 00196 put_xsub_rle(&pb, h->rects[0]->w, PADDING_COLOR); 00197 align_put_bits(&pb); 00198 } 00199 00200 flush_put_bits(&pb); 00201 00202 return hdr - buf + put_bits_count(&pb)/8; 00203 } 00204 00205 static av_cold int xsub_encoder_init(AVCodecContext *avctx) 00206 { 00207 if (!avctx->codec_tag) 00208 avctx->codec_tag = MKTAG('D','X','S','B'); 00209 00210 return 0; 00211 } 00212 00213 AVCodec ff_xsub_encoder = { 00214 "xsub", 00215 AVMEDIA_TYPE_SUBTITLE, 00216 CODEC_ID_XSUB, 00217 0, 00218 xsub_encoder_init, 00219 xsub_encode, 00220 NULL, 00221 .long_name = NULL_IF_CONFIG_SMALL("DivX subtitles (XSUB)"), 00222 };