2 * DVD subtitle encoding
3 * Copyright (c) 2005 Wolfram Gloger
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "bytestream.h"
23 #include "libavutil/avassert.h"
24 #include "libavutil/bprint.h"
25 #include "libavutil/imgutils.h"
28 uint32_t global_palette[16];
31 // ncnt is the nibble counter
32 #define PUTNIBBLE(val)\
35 *q++ = bitbuf | ((val) & 0x0f);\
40 static void dvd_encode_rle(uint8_t **pq,
41 const uint8_t *bitmap, int linesize,
46 unsigned int bitbuf = 0;
52 for (y = 0; y < h; ++y) {
54 for(x = 0; x < w; x += len) {
56 for (len=1; x+len < w; ++len)
57 if (bitmap[x+len] != color)
60 av_assert0(color < 4);
62 PUTNIBBLE((len << 2)|color);
63 } else if (len < 0x10) {
65 PUTNIBBLE((len << 2)|color);
66 } else if (len < 0x40) {
69 PUTNIBBLE((len << 2)|color);
70 } else if (x+len == w) {
81 PUTNIBBLE((len << 2)|color);
93 static int color_distance(uint32_t a, uint32_t b)
97 for (i = 0; i < 32; i += 8) {
98 d = ((a >> i) & 0xFF) - ((b >> i) & 0xFF);
105 * Count colors used in a rectangle, quantizing alpha and grouping by
106 * nearest global palette entry.
108 static void count_colors(AVCodecContext *avctx, unsigned hits[33],
109 const AVSubtitleRect *r)
111 DVDSubtitleContext *dvdc = avctx->priv_data;
112 unsigned count[256] = { 0 };
113 uint32_t *palette = (uint32_t *)r->pict.data[1];
115 int x, y, i, j, match, d, best_d, av_uninit(best_j);
116 uint8_t *p = r->pict.data[0];
118 for (y = 0; y < r->h; y++) {
119 for (x = 0; x < r->w; x++)
121 p += r->pict.linesize[0] - r->w;
123 for (i = 0; i < 256; i++) {
124 if (!count[i]) /* avoid useless search */
127 /* 0: transparent, 1-16: semi-transparent, 17-33 opaque */
128 match = color < 0x33000000 ? 0 : color < 0xCC000000 ? 1 : 17;
131 for (j = 0; j < 16; j++) {
132 d = color_distance(color & 0xFFFFFF, dvdc->global_palette[j]);
140 hits[match] += count[i];
144 static void select_palette(AVCodecContext *avctx, int out_palette[4],
145 int out_alpha[4], unsigned hits[33])
147 DVDSubtitleContext *dvdc = avctx->priv_data;
148 int i, j, bright, mult;
150 int selected[4] = { 0 };
151 uint32_t pseudopal[33] = { 0 };
152 uint32_t refcolor[3] = { 0x00000000, 0xFFFFFFFF, 0xFF000000 };
154 /* Bonus for transparent: if the rectangle fits tightly the text, the
155 background color can be quite rare, but it would be ugly without it */
157 /* Bonus for bright colors */
158 for (i = 0; i < 16; i++) {
159 if (!(hits[1 + i] + hits[17 + i]))
160 continue; /* skip unused colors to gain time */
161 color = dvdc->global_palette[i];
163 for (j = 0; j < 3; j++, color >>= 8)
164 bright += (color & 0xFF) < 0x40 || (color & 0xFF) >= 0xC0;
165 mult = 2 + FFMIN(bright, 2);
166 hits[ 1 + i] *= mult;
167 hits[17 + i] *= mult;
170 /* Select four most frequent colors */
171 for (i = 0; i < 4; i++) {
172 for (j = 0; j < 33; j++)
173 if (hits[j] > hits[selected[i]])
175 hits[selected[i]] = 0;
178 /* Order the colors like in most DVDs:
179 0: background, 1: foreground, 2: outline */
180 for (i = 0; i < 16; i++) {
181 pseudopal[ 1 + i] = 0x80000000 | dvdc->global_palette[i];
182 pseudopal[17 + i] = 0xFF000000 | dvdc->global_palette[i];
184 for (i = 0; i < 3; i++) {
185 int best_d = color_distance(refcolor[i], pseudopal[selected[i]]);
186 for (j = i + 1; j < 4; j++) {
187 int d = color_distance(refcolor[i], pseudopal[selected[j]]);
189 FFSWAP(int, selected[i], selected[j]);
196 for (i = 0; i < 4; i++) {
197 out_palette[i] = selected[i] ? (selected[i] - 1) & 0xF : 0;
198 out_alpha [i] = !selected[i] ? 0 : selected[i] < 17 ? 0x80 : 0xFF;
202 static void build_color_map(AVCodecContext *avctx, int cmap[],
203 const uint32_t palette[],
204 const int out_palette[], int const out_alpha[])
206 DVDSubtitleContext *dvdc = avctx->priv_data;
208 uint32_t pseudopal[4];
210 for (i = 0; i < 4; i++)
211 pseudopal[i] = (out_alpha[i] << 24) |
212 dvdc->global_palette[out_palette[i]];
213 for (i = 0; i < 256; i++) {
215 for (j = 0; j < 4; j++) {
216 d = color_distance(pseudopal[j], palette[i]);
225 static void copy_rectangle(AVSubtitleRect *dst, AVSubtitleRect *src, int cmap[])
230 p = src->pict.data[0];
231 q = dst->pict.data[0] + (src->x - dst->x) +
232 (src->y - dst->y) * dst->pict.linesize[0];
233 for (y = 0; y < src->h; y++) {
234 for (x = 0; x < src->w; x++)
235 *(q++) = cmap[*(p++)];
236 p += src->pict.linesize[0] - src->w;
237 q += dst->pict.linesize[0] - src->w;
241 static int encode_dvd_subtitles(AVCodecContext *avctx,
242 uint8_t *outbuf, int outbuf_size,
245 DVDSubtitleContext *dvdc = avctx->priv_data;
247 int offset1, offset2;
248 int i, rects = h->num_rects, ret;
249 unsigned global_palette_hits[33] = { 0 };
253 AVSubtitleRect vrect;
254 uint8_t *vrect_data = NULL;
257 if (rects == 0 || h->rects == NULL)
258 return AVERROR(EINVAL);
259 for (i = 0; i < rects; i++)
260 if (h->rects[i]->type != SUBTITLE_BITMAP) {
261 av_log(avctx, AV_LOG_ERROR, "Bitmap subtitle required\n");
262 return AVERROR(EINVAL);
264 vrect = *h->rects[0];
267 /* DVD subtitles can have only one rectangle: build a virtual
268 rectangle containing all actual rectangles.
269 The data of the rectangles will be copied later, when the palette
270 is decided, because the rectangles may have different palettes. */
271 int xmin = h->rects[0]->x, xmax = xmin + h->rects[0]->w;
272 int ymin = h->rects[0]->y, ymax = ymin + h->rects[0]->h;
273 for (i = 1; i < rects; i++) {
274 xmin = FFMIN(xmin, h->rects[i]->x);
275 ymin = FFMIN(ymin, h->rects[i]->y);
276 xmax = FFMAX(xmax, h->rects[i]->x + h->rects[i]->w);
277 ymax = FFMAX(ymax, h->rects[i]->y + h->rects[i]->h);
281 vrect.w = xmax - xmin;
282 vrect.h = ymax - ymin;
283 if ((ret = av_image_check_size(vrect.w, vrect.h, 0, avctx)) < 0)
286 /* Count pixels outside the virtual rectangle as transparent */
287 global_palette_hits[0] = vrect.w * vrect.h;
288 for (i = 0; i < rects; i++)
289 global_palette_hits[0] -= h->rects[i]->w * h->rects[i]->h;
292 for (i = 0; i < rects; i++)
293 count_colors(avctx, global_palette_hits, h->rects[i]);
294 select_palette(avctx, out_palette, out_alpha, global_palette_hits);
297 if (!(vrect_data = av_calloc(vrect.w, vrect.h)))
298 return AVERROR(ENOMEM);
299 vrect.pict.data [0] = vrect_data;
300 vrect.pict.linesize[0] = vrect.w;
301 for (i = 0; i < rects; i++) {
302 build_color_map(avctx, cmap, (uint32_t *)h->rects[i]->pict.data[1],
303 out_palette, out_alpha);
304 copy_rectangle(&vrect, h->rects[i], cmap);
306 for (i = 0; i < 4; i++)
309 build_color_map(avctx, cmap, (uint32_t *)h->rects[0]->pict.data[1],
310 out_palette, out_alpha);
313 av_log(avctx, AV_LOG_DEBUG, "Selected palette:");
314 for (i = 0; i < 4; i++)
315 av_log(avctx, AV_LOG_DEBUG, " 0x%06x@@%02x (0x%x,0x%x)",
316 dvdc->global_palette[out_palette[i]], out_alpha[i],
317 out_palette[i], out_alpha[i] >> 4);
318 av_log(avctx, AV_LOG_DEBUG, "\n");
322 offset1 = q - outbuf;
323 // worst case memory requirement: 1 nibble per pixel..
324 if ((q - outbuf) + vrect.w * vrect.h / 2 + 17 + 21 > outbuf_size) {
325 av_log(NULL, AV_LOG_ERROR, "dvd_subtitle too big\n");
326 ret = AVERROR_BUFFER_TOO_SMALL;
329 dvd_encode_rle(&q, vrect.pict.data[0], vrect.w * 2,
330 vrect.w, (vrect.h + 1) >> 1, cmap);
331 offset2 = q - outbuf;
332 dvd_encode_rle(&q, vrect.pict.data[0] + vrect.w, vrect.w * 2,
333 vrect.w, vrect.h >> 1, cmap);
335 // set data packet size
337 bytestream_put_be16(&qq, q - outbuf);
339 // send start display command
340 bytestream_put_be16(&q, (h->start_display_time*90) >> 10);
341 bytestream_put_be16(&q, (q - outbuf) /*- 2 */ + 8 + 12 + 2);
342 *q++ = 0x03; // palette - 4 nibbles
343 *q++ = (out_palette[3] << 4) | out_palette[2];
344 *q++ = (out_palette[1] << 4) | out_palette[0];
345 *q++ = 0x04; // alpha - 4 nibbles
346 *q++ = (out_alpha[3] & 0xF0) | (out_alpha[2] >> 4);
347 *q++ = (out_alpha[1] & 0xF0) | (out_alpha[0] >> 4);
350 x2 = vrect.x + vrect.w - 1;
351 y2 = vrect.y + vrect.h - 1;
354 // x1 x2 -> 6 nibbles
356 *q++ = (vrect.x << 4) | ((x2 >> 8) & 0xf);
358 // y1 y2 -> 6 nibbles
360 *q++ = (vrect.y << 4) | ((y2 >> 8) & 0xf);
365 bytestream_put_be16(&q, offset1);
366 bytestream_put_be16(&q, offset2);
368 *q++ = 0x01; // start command
369 *q++ = 0xff; // terminating command
371 // send stop display command last
372 bytestream_put_be16(&q, (h->end_display_time*90) >> 10);
373 bytestream_put_be16(&q, (q - outbuf) - 2 /*+ 4*/);
374 *q++ = 0x02; // set end
375 *q++ = 0xff; // terminating command
378 bytestream_put_be16(&qq, q - outbuf);
380 av_log(NULL, AV_LOG_DEBUG, "subtitle_packet size=%td\n", q - outbuf);
388 static int dvdsub_init(AVCodecContext *avctx)
390 DVDSubtitleContext *dvdc = avctx->priv_data;
391 static const uint32_t default_palette[16] = {
392 0x000000, 0x0000FF, 0x00FF00, 0xFF0000,
393 0xFFFF00, 0xFF00FF, 0x00FFFF, 0xFFFFFF,
394 0x808000, 0x8080FF, 0x800080, 0x80FF80,
395 0x008080, 0xFF8080, 0x555555, 0xAAAAAA,
400 av_assert0(sizeof(dvdc->global_palette) == sizeof(default_palette));
401 memcpy(dvdc->global_palette, default_palette, sizeof(dvdc->global_palette));
403 av_bprint_init(&extradata, 0, 1);
404 if (avctx->width && avctx->height)
405 av_bprintf(&extradata, "size: %dx%d\n", avctx->width, avctx->height);
406 av_bprintf(&extradata, "palette:");
407 for (i = 0; i < 16; i++)
408 av_bprintf(&extradata, " %06"PRIx32"%c",
409 dvdc->global_palette[i] & 0xFFFFFF, i < 15 ? ',' : '\n');
411 if ((ret = av_bprint_finalize(&extradata, (char **)&avctx->extradata)) < 0)
413 avctx->extradata_size = extradata.len;
418 static int dvdsub_encode(AVCodecContext *avctx,
419 unsigned char *buf, int buf_size, void *data)
421 //DVDSubtitleContext *s = avctx->priv_data;
422 AVSubtitle *sub = data;
425 ret = encode_dvd_subtitles(avctx, buf, buf_size, sub);
429 AVCodec ff_dvdsub_encoder = {
431 .type = AVMEDIA_TYPE_SUBTITLE,
432 .id = AV_CODEC_ID_DVD_SUBTITLE,
434 .encode = dvdsub_encode,
435 .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),
436 .priv_data_size = sizeof(DVDSubtitleContext),