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, void *data)
00115 {
00116 AVSubtitle *h = data;
00117 uint64_t startTime = h->pts / 1000;
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;
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
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
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
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
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
00160
00161
00162
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;
00174 hdr+=2;
00175
00176
00177 for (i=0; i<4; i++)
00178 bytestream_put_be24(&hdr, ((uint32_t *)h->rects[0]->pict.data[1])[i]);
00179
00180
00181
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);
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
00195 if (h->rects[0]->h & 1) {
00196 put_xsub_rle(&pb, h->rects[0]->w, PADDING_COLOR);
00197 avpriv_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 .name = "xsub",
00215 .type = AVMEDIA_TYPE_SUBTITLE,
00216 .id = CODEC_ID_XSUB,
00217 .init = xsub_encoder_init,
00218 .encode = xsub_encode,
00219 .long_name = NULL_IF_CONFIG_SMALL("DivX subtitles (XSUB)"),
00220 };