00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
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
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 avpriv_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, const AVSubtitle *h)
00115 {
00116 uint64_t startTime = h->pts / 1000;
00117 uint64_t endTime = startTime + h->end_display_time - h->start_display_time;
00118 int start_tc[4], end_tc[4];
00119 uint8_t *hdr = buf + 27;
00120 uint8_t *rlelenptr;
00121 uint16_t width, height;
00122 int i;
00123 PutBitContext pb;
00124
00125 if (bufsize < 27 + 7*2 + 4*3) {
00126 av_log(avctx, AV_LOG_ERROR, "Buffer too small for XSUB header.\n");
00127 return -1;
00128 }
00129
00130
00131 if (h->num_rects != 1)
00132 av_log(avctx, AV_LOG_WARNING, "Only single rects supported (%d in subtitle.)\n", h->num_rects);
00133
00134
00135 if (!h->rects[0]->pict.data[0] || !h->rects[0]->pict.data[1]) {
00136 av_log(avctx, AV_LOG_WARNING, "No subtitle bitmap available.\n");
00137 return -1;
00138 }
00139
00140
00141 if (h->rects[0]->nb_colors > 4)
00142 av_log(avctx, AV_LOG_WARNING, "No more than 4 subtitle colors supported (%d found.)\n", h->rects[0]->nb_colors);
00143
00144
00145 if (((uint32_t *)h->rects[0]->pict.data[1])[0] & 0xff)
00146 av_log(avctx, AV_LOG_WARNING, "Color index 0 is not transparent. Transparency will be messed up.\n");
00147
00148 if (make_tc(startTime, start_tc) || make_tc(endTime, end_tc)) {
00149 av_log(avctx, AV_LOG_WARNING, "Time code >= 100 hours.\n");
00150 return -1;
00151 }
00152
00153 snprintf(buf, 28,
00154 "[%02d:%02d:%02d.%03d-%02d:%02d:%02d.%03d]",
00155 start_tc[3], start_tc[2], start_tc[1], start_tc[0],
00156 end_tc[3], end_tc[2], end_tc[1], end_tc[0]);
00157
00158
00159
00160
00161
00162 width = FFALIGN(h->rects[0]->w, 2) + PADDING * 2;
00163 height = FFALIGN(h->rects[0]->h, 2);
00164
00165 bytestream_put_le16(&hdr, width);
00166 bytestream_put_le16(&hdr, height);
00167 bytestream_put_le16(&hdr, h->rects[0]->x);
00168 bytestream_put_le16(&hdr, h->rects[0]->y);
00169 bytestream_put_le16(&hdr, h->rects[0]->x + width);
00170 bytestream_put_le16(&hdr, h->rects[0]->y + height);
00171
00172 rlelenptr = hdr;
00173 hdr+=2;
00174
00175
00176 for (i=0; i<4; i++)
00177 bytestream_put_be24(&hdr, ((uint32_t *)h->rects[0]->pict.data[1])[i]);
00178
00179
00180
00181 init_put_bits(&pb, hdr, bufsize - (hdr - buf) - 2);
00182 if (xsub_encode_rle(&pb, h->rects[0]->pict.data[0],
00183 h->rects[0]->pict.linesize[0]*2,
00184 h->rects[0]->w, (h->rects[0]->h + 1) >> 1))
00185 return -1;
00186 bytestream_put_le16(&rlelenptr, put_bits_count(&pb) >> 3);
00187
00188 if (xsub_encode_rle(&pb, h->rects[0]->pict.data[0] + h->rects[0]->pict.linesize[0],
00189 h->rects[0]->pict.linesize[0]*2,
00190 h->rects[0]->w, h->rects[0]->h >> 1))
00191 return -1;
00192
00193
00194 if (h->rects[0]->h & 1) {
00195 put_xsub_rle(&pb, h->rects[0]->w, PADDING_COLOR);
00196 avpriv_align_put_bits(&pb);
00197 }
00198
00199 flush_put_bits(&pb);
00200
00201 return hdr - buf + put_bits_count(&pb)/8;
00202 }
00203
00204 static av_cold int xsub_encoder_init(AVCodecContext *avctx)
00205 {
00206 if (!avctx->codec_tag)
00207 avctx->codec_tag = MKTAG('D','X','S','B');
00208
00209 return 0;
00210 }
00211
00212 AVCodec ff_xsub_encoder = {
00213 .name = "xsub",
00214 .type = AVMEDIA_TYPE_SUBTITLE,
00215 .id = AV_CODEC_ID_XSUB,
00216 .init = xsub_encoder_init,
00217 .encode_sub = xsub_encode,
00218 .long_name = NULL_IF_CONFIG_SMALL("DivX subtitles (XSUB)"),
00219 };