00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "avcodec.h"
00022 #include "bytestream.h"
00023 #include "libavutil/avassert.h"
00024 #include "libavutil/bprint.h"
00025 #include "libavutil/imgutils.h"
00026
00027 typedef struct {
00028 uint32_t global_palette[16];
00029 } DVDSubtitleContext;
00030
00031
00032 #define PUTNIBBLE(val)\
00033 do {\
00034 if (ncnt++ & 1)\
00035 *q++ = bitbuf | ((val) & 0x0f);\
00036 else\
00037 bitbuf = (val) << 4;\
00038 } while(0)
00039
00040 static void dvd_encode_rle(uint8_t **pq,
00041 const uint8_t *bitmap, int linesize,
00042 int w, int h,
00043 const int cmap[256])
00044 {
00045 uint8_t *q;
00046 unsigned int bitbuf = 0;
00047 int ncnt;
00048 int x, y, len, color;
00049
00050 q = *pq;
00051
00052 for (y = 0; y < h; ++y) {
00053 ncnt = 0;
00054 for(x = 0; x < w; x += len) {
00055 color = bitmap[x];
00056 for (len=1; x+len < w; ++len)
00057 if (bitmap[x+len] != color)
00058 break;
00059 color = cmap[color];
00060 av_assert0(color < 4);
00061 if (len < 0x04) {
00062 PUTNIBBLE((len << 2)|color);
00063 } else if (len < 0x10) {
00064 PUTNIBBLE(len >> 2);
00065 PUTNIBBLE((len << 2)|color);
00066 } else if (len < 0x40) {
00067 PUTNIBBLE(0);
00068 PUTNIBBLE(len >> 2);
00069 PUTNIBBLE((len << 2)|color);
00070 } else if (x+len == w) {
00071 PUTNIBBLE(0);
00072 PUTNIBBLE(0);
00073 PUTNIBBLE(0);
00074 PUTNIBBLE(color);
00075 } else {
00076 if (len > 0xff)
00077 len = 0xff;
00078 PUTNIBBLE(0);
00079 PUTNIBBLE(len >> 6);
00080 PUTNIBBLE(len >> 2);
00081 PUTNIBBLE((len << 2)|color);
00082 }
00083 }
00084
00085 if (ncnt & 1)
00086 PUTNIBBLE(0);
00087 bitmap += linesize;
00088 }
00089
00090 *pq = q;
00091 }
00092
00093 static int color_distance(uint32_t a, uint32_t b)
00094 {
00095 int r = 0, d, i;
00096
00097 for (i = 0; i < 32; i += 8) {
00098 d = ((a >> i) & 0xFF) - ((b >> i) & 0xFF);
00099 r += d * d;
00100 }
00101 return r;
00102 }
00103
00108 static void count_colors(AVCodecContext *avctx, unsigned hits[33],
00109 const AVSubtitleRect *r)
00110 {
00111 DVDSubtitleContext *dvdc = avctx->priv_data;
00112 unsigned count[256] = { 0 };
00113 uint32_t *palette = (uint32_t *)r->pict.data[1];
00114 uint32_t color;
00115 int x, y, i, j, match, d, best_d, av_uninit(best_j);
00116 uint8_t *p = r->pict.data[0];
00117
00118 for (y = 0; y < r->h; y++) {
00119 for (x = 0; x < r->w; x++)
00120 count[*(p++)]++;
00121 p += r->pict.linesize[0] - r->w;
00122 }
00123 for (i = 0; i < 256; i++) {
00124 if (!count[i])
00125 continue;
00126 color = palette[i];
00127
00128 match = color < 0x33000000 ? 0 : color < 0xCC000000 ? 1 : 17;
00129 if (match) {
00130 best_d = INT_MAX;
00131 for (j = 0; j < 16; j++) {
00132 d = color_distance(color & 0xFFFFFF, dvdc->global_palette[j]);
00133 if (d < best_d) {
00134 best_d = d;
00135 best_j = j;
00136 }
00137 }
00138 match += best_j;
00139 }
00140 hits[match] += count[i];
00141 }
00142 }
00143
00144 static void select_palette(AVCodecContext *avctx, int out_palette[4],
00145 int out_alpha[4], unsigned hits[33])
00146 {
00147 DVDSubtitleContext *dvdc = avctx->priv_data;
00148 int i, j, bright, mult;
00149 uint32_t color;
00150 int selected[4] = { 0 };
00151 uint32_t pseudopal[33] = { 0 };
00152 uint32_t refcolor[3] = { 0x00000000, 0xFFFFFFFF, 0xFF000000 };
00153
00154
00155
00156 hits[0] *= 16;
00157
00158 for (i = 0; i < 16; i++) {
00159 if (!(hits[1 + i] + hits[17 + i]))
00160 continue;
00161 color = dvdc->global_palette[i];
00162 bright = 0;
00163 for (j = 0; j < 3; j++, color >>= 8)
00164 bright += (color & 0xFF) < 0x40 || (color & 0xFF) >= 0xC0;
00165 mult = 2 + FFMIN(bright, 2);
00166 hits[ 1 + i] *= mult;
00167 hits[17 + i] *= mult;
00168 }
00169
00170
00171 for (i = 0; i < 4; i++) {
00172 for (j = 0; j < 33; j++)
00173 if (hits[j] > hits[selected[i]])
00174 selected[i] = j;
00175 hits[selected[i]] = 0;
00176 }
00177
00178
00179
00180 for (i = 0; i < 16; i++) {
00181 pseudopal[ 1 + i] = 0x80000000 | dvdc->global_palette[i];
00182 pseudopal[17 + i] = 0xFF000000 | dvdc->global_palette[i];
00183 }
00184 for (i = 0; i < 3; i++) {
00185 int best_d = color_distance(refcolor[i], pseudopal[selected[i]]);
00186 for (j = i + 1; j < 4; j++) {
00187 int d = color_distance(refcolor[i], pseudopal[selected[j]]);
00188 if (d < best_d) {
00189 FFSWAP(int, selected[i], selected[j]);
00190 best_d = d;
00191 }
00192 }
00193 }
00194
00195
00196 for (i = 0; i < 4; i++) {
00197 out_palette[i] = selected[i] ? (selected[i] - 1) & 0xF : 0;
00198 out_alpha [i] = !selected[i] ? 0 : selected[i] < 17 ? 0x80 : 0xFF;
00199 }
00200 }
00201
00202 static void build_color_map(AVCodecContext *avctx, int cmap[],
00203 const uint32_t palette[],
00204 const int out_palette[], int const out_alpha[])
00205 {
00206 DVDSubtitleContext *dvdc = avctx->priv_data;
00207 int i, j, d, best_d;
00208 uint32_t pseudopal[4];
00209
00210 for (i = 0; i < 4; i++)
00211 pseudopal[i] = (out_alpha[i] << 24) |
00212 dvdc->global_palette[out_palette[i]];
00213 for (i = 0; i < 256; i++) {
00214 best_d = INT_MAX;
00215 for (j = 0; j < 4; j++) {
00216 d = color_distance(pseudopal[j], palette[i]);
00217 if (d < best_d) {
00218 cmap[i] = j;
00219 best_d = d;
00220 }
00221 }
00222 }
00223 }
00224
00225 static void copy_rectangle(AVSubtitleRect *dst, AVSubtitleRect *src, int cmap[])
00226 {
00227 int x, y;
00228 uint8_t *p, *q;
00229
00230 p = src->pict.data[0];
00231 q = dst->pict.data[0] + (src->x - dst->x) +
00232 (src->y - dst->y) * dst->pict.linesize[0];
00233 for (y = 0; y < src->h; y++) {
00234 for (x = 0; x < src->w; x++)
00235 *(q++) = cmap[*(p++)];
00236 p += src->pict.linesize[0] - src->w;
00237 q += dst->pict.linesize[0] - src->w;
00238 }
00239 }
00240
00241 static int encode_dvd_subtitles(AVCodecContext *avctx,
00242 uint8_t *outbuf, int outbuf_size,
00243 const AVSubtitle *h)
00244 {
00245 DVDSubtitleContext *dvdc = avctx->priv_data;
00246 uint8_t *q, *qq;
00247 int offset1, offset2;
00248 int i, rects = h->num_rects, ret;
00249 unsigned global_palette_hits[33] = { 0 };
00250 int cmap[256];
00251 int out_palette[4];
00252 int out_alpha[4];
00253 AVSubtitleRect vrect;
00254 uint8_t *vrect_data = NULL;
00255 int x2, y2;
00256
00257 if (rects == 0 || h->rects == NULL)
00258 return AVERROR(EINVAL);
00259 for (i = 0; i < rects; i++)
00260 if (h->rects[i]->type != SUBTITLE_BITMAP) {
00261 av_log(avctx, AV_LOG_ERROR, "Bitmap subtitle required\n");
00262 return AVERROR(EINVAL);
00263 }
00264 vrect = *h->rects[0];
00265
00266 if (rects > 1) {
00267
00268
00269
00270
00271 int xmin = h->rects[0]->x, xmax = xmin + h->rects[0]->w;
00272 int ymin = h->rects[0]->y, ymax = ymin + h->rects[0]->h;
00273 for (i = 1; i < rects; i++) {
00274 xmin = FFMIN(xmin, h->rects[i]->x);
00275 ymin = FFMIN(ymin, h->rects[i]->y);
00276 xmax = FFMAX(xmax, h->rects[i]->x + h->rects[i]->w);
00277 ymax = FFMAX(ymax, h->rects[i]->y + h->rects[i]->h);
00278 }
00279 vrect.x = xmin;
00280 vrect.y = ymin;
00281 vrect.w = xmax - xmin;
00282 vrect.h = ymax - ymin;
00283 if ((ret = av_image_check_size(vrect.w, vrect.h, 0, avctx)) < 0)
00284 return ret;
00285
00286
00287 global_palette_hits[0] = vrect.w * vrect.h;
00288 for (i = 0; i < rects; i++)
00289 global_palette_hits[0] -= h->rects[i]->w * h->rects[i]->h;
00290 }
00291
00292 for (i = 0; i < rects; i++)
00293 count_colors(avctx, global_palette_hits, h->rects[i]);
00294 select_palette(avctx, out_palette, out_alpha, global_palette_hits);
00295
00296 if (rects > 1) {
00297 if (!(vrect_data = av_calloc(vrect.w, vrect.h)))
00298 return AVERROR(ENOMEM);
00299 vrect.pict.data [0] = vrect_data;
00300 vrect.pict.linesize[0] = vrect.w;
00301 for (i = 0; i < rects; i++) {
00302 build_color_map(avctx, cmap, (uint32_t *)h->rects[i]->pict.data[1],
00303 out_palette, out_alpha);
00304 copy_rectangle(&vrect, h->rects[i], cmap);
00305 }
00306 for (i = 0; i < 4; i++)
00307 cmap[i] = i;
00308 } else {
00309 build_color_map(avctx, cmap, (uint32_t *)h->rects[0]->pict.data[1],
00310 out_palette, out_alpha);
00311 }
00312
00313 av_log(avctx, AV_LOG_DEBUG, "Selected palette:");
00314 for (i = 0; i < 4; i++)
00315 av_log(avctx, AV_LOG_DEBUG, " 0x%06x@@%02x (0x%x,0x%x)",
00316 dvdc->global_palette[out_palette[i]], out_alpha[i],
00317 out_palette[i], out_alpha[i] >> 4);
00318 av_log(avctx, AV_LOG_DEBUG, "\n");
00319
00320
00321 q = outbuf + 4;
00322 offset1 = q - outbuf;
00323
00324 if ((q - outbuf) + vrect.w * vrect.h / 2 + 17 + 21 > outbuf_size) {
00325 av_log(NULL, AV_LOG_ERROR, "dvd_subtitle too big\n");
00326 ret = AVERROR_BUFFER_TOO_SMALL;
00327 goto fail;
00328 }
00329 dvd_encode_rle(&q, vrect.pict.data[0], vrect.w * 2,
00330 vrect.w, (vrect.h + 1) >> 1, cmap);
00331 offset2 = q - outbuf;
00332 dvd_encode_rle(&q, vrect.pict.data[0] + vrect.w, vrect.w * 2,
00333 vrect.w, vrect.h >> 1, cmap);
00334
00335
00336 qq = outbuf + 2;
00337 bytestream_put_be16(&qq, q - outbuf);
00338
00339
00340 bytestream_put_be16(&q, (h->start_display_time*90) >> 10);
00341 bytestream_put_be16(&q, (q - outbuf) + 8 + 12 + 2);
00342 *q++ = 0x03;
00343 *q++ = (out_palette[3] << 4) | out_palette[2];
00344 *q++ = (out_palette[1] << 4) | out_palette[0];
00345 *q++ = 0x04;
00346 *q++ = (out_alpha[3] & 0xF0) | (out_alpha[2] >> 4);
00347 *q++ = (out_alpha[1] & 0xF0) | (out_alpha[0] >> 4);
00348
00349
00350 x2 = vrect.x + vrect.w - 1;
00351 y2 = vrect.y + vrect.h - 1;
00352
00353 *q++ = 0x05;
00354
00355 *q++ = vrect.x >> 4;
00356 *q++ = (vrect.x << 4) | ((x2 >> 8) & 0xf);
00357 *q++ = x2;
00358
00359 *q++ = vrect.y >> 4;
00360 *q++ = (vrect.y << 4) | ((y2 >> 8) & 0xf);
00361 *q++ = y2;
00362
00363 *q++ = 0x06;
00364
00365 bytestream_put_be16(&q, offset1);
00366 bytestream_put_be16(&q, offset2);
00367
00368 *q++ = 0x01;
00369 *q++ = 0xff;
00370
00371
00372 bytestream_put_be16(&q, (h->end_display_time*90) >> 10);
00373 bytestream_put_be16(&q, (q - outbuf) - 2 );
00374 *q++ = 0x02;
00375 *q++ = 0xff;
00376
00377 qq = outbuf;
00378 bytestream_put_be16(&qq, q - outbuf);
00379
00380 av_log(NULL, AV_LOG_DEBUG, "subtitle_packet size=%td\n", q - outbuf);
00381 ret = q - outbuf;
00382
00383 fail:
00384 av_free(vrect_data);
00385 return ret;
00386 }
00387
00388 static int dvdsub_init(AVCodecContext *avctx)
00389 {
00390 DVDSubtitleContext *dvdc = avctx->priv_data;
00391 static const uint32_t default_palette[16] = {
00392 0x000000, 0x0000FF, 0x00FF00, 0xFF0000,
00393 0xFFFF00, 0xFF00FF, 0x00FFFF, 0xFFFFFF,
00394 0x808000, 0x8080FF, 0x800080, 0x80FF80,
00395 0x008080, 0xFF8080, 0x555555, 0xAAAAAA,
00396 };
00397 AVBPrint extradata;
00398 int i, ret;
00399
00400 av_assert0(sizeof(dvdc->global_palette) == sizeof(default_palette));
00401 memcpy(dvdc->global_palette, default_palette, sizeof(dvdc->global_palette));
00402
00403 av_bprint_init(&extradata, 0, 1);
00404 if (avctx->width && avctx->height)
00405 av_bprintf(&extradata, "size: %dx%d\n", avctx->width, avctx->height);
00406 av_bprintf(&extradata, "palette:");
00407 for (i = 0; i < 16; i++)
00408 av_bprintf(&extradata, " %06"PRIx32"%c",
00409 dvdc->global_palette[i] & 0xFFFFFF, i < 15 ? ',' : '\n');
00410
00411 if ((ret = av_bprint_finalize(&extradata, (char **)&avctx->extradata)) < 0)
00412 return ret;
00413 avctx->extradata_size = extradata.len;
00414
00415 return 0;
00416 }
00417
00418 static int dvdsub_encode(AVCodecContext *avctx,
00419 unsigned char *buf, int buf_size,
00420 const AVSubtitle *sub)
00421 {
00422
00423 int ret;
00424
00425 ret = encode_dvd_subtitles(avctx, buf, buf_size, sub);
00426 return ret;
00427 }
00428
00429 AVCodec ff_dvdsub_encoder = {
00430 .name = "dvdsub",
00431 .type = AVMEDIA_TYPE_SUBTITLE,
00432 .id = AV_CODEC_ID_DVD_SUBTITLE,
00433 .init = dvdsub_init,
00434 .encode_sub = dvdsub_encode,
00435 .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),
00436 .priv_data_size = sizeof(DVDSubtitleContext),
00437 };