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"
24 #include "libavutil/avassert.h"
25 #include "libavutil/bprint.h"
26 #include "libavutil/imgutils.h"
29 uint32_t global_palette[16];
32 // ncnt is the nibble counter
33 #define PUTNIBBLE(val)\
36 *q++ = bitbuf | ((val) & 0x0f);\
41 static void dvd_encode_rle(uint8_t **pq,
42 const uint8_t *bitmap, int linesize,
47 unsigned int bitbuf = 0;
53 for (y = 0; y < h; ++y) {
55 for(x = 0; x < w; x += len) {
57 for (len=1; x+len < w; ++len)
58 if (bitmap[x+len] != color)
61 av_assert0(color < 4);
63 PUTNIBBLE((len << 2)|color);
64 } else if (len < 0x10) {
66 PUTNIBBLE((len << 2)|color);
67 } else if (len < 0x40) {
70 PUTNIBBLE((len << 2)|color);
71 } else if (x+len == w) {
82 PUTNIBBLE((len << 2)|color);
94 static int color_distance(uint32_t a, uint32_t b)
97 int alpha_a = 8, alpha_b = 8;
99 for (i = 24; i >= 0; i -= 8) {
100 d = alpha_a * (int)((a >> i) & 0xFF) -
101 alpha_b * (int)((b >> i) & 0xFF);
110 * Count colors used in a rectangle, quantizing alpha and grouping by
111 * nearest global palette entry.
113 static void count_colors(AVCodecContext *avctx, unsigned hits[33],
114 const AVSubtitleRect *r)
116 DVDSubtitleContext *dvdc = avctx->priv_data;
117 unsigned count[256] = { 0 };
118 uint32_t *palette = (uint32_t *)r->pict.data[1];
120 int x, y, i, j, match, d, best_d, av_uninit(best_j);
121 uint8_t *p = r->pict.data[0];
123 for (y = 0; y < r->h; y++) {
124 for (x = 0; x < r->w; x++)
126 p += r->pict.linesize[0] - r->w;
128 for (i = 0; i < 256; i++) {
129 if (!count[i]) /* avoid useless search */
132 /* 0: transparent, 1-16: semi-transparent, 17-33 opaque */
133 match = color < 0x33000000 ? 0 : color < 0xCC000000 ? 1 : 17;
136 for (j = 0; j < 16; j++) {
137 d = color_distance(0xFF000000 | color,
138 0xFF000000 | dvdc->global_palette[j]);
146 hits[match] += count[i];
150 static void select_palette(AVCodecContext *avctx, int out_palette[4],
151 int out_alpha[4], unsigned hits[33])
153 DVDSubtitleContext *dvdc = avctx->priv_data;
154 int i, j, bright, mult;
156 int selected[4] = { 0 };
157 uint32_t pseudopal[33] = { 0 };
158 uint32_t refcolor[3] = { 0x00000000, 0xFFFFFFFF, 0xFF000000 };
160 /* Bonus for transparent: if the rectangle fits tightly the text, the
161 background color can be quite rare, but it would be ugly without it */
163 /* Bonus for bright colors */
164 for (i = 0; i < 16; i++) {
165 if (!(hits[1 + i] + hits[17 + i]))
166 continue; /* skip unused colors to gain time */
167 color = dvdc->global_palette[i];
169 for (j = 0; j < 3; j++, color >>= 8)
170 bright += (color & 0xFF) < 0x40 || (color & 0xFF) >= 0xC0;
171 mult = 2 + FFMIN(bright, 2);
172 hits[ 1 + i] *= mult;
173 hits[17 + i] *= mult;
176 /* Select four most frequent colors */
177 for (i = 0; i < 4; i++) {
178 for (j = 0; j < 33; j++)
179 if (hits[j] > hits[selected[i]])
181 hits[selected[i]] = 0;
184 /* Order the colors like in most DVDs:
185 0: background, 1: foreground, 2: outline */
186 for (i = 0; i < 16; i++) {
187 pseudopal[ 1 + i] = 0x80000000 | dvdc->global_palette[i];
188 pseudopal[17 + i] = 0xFF000000 | dvdc->global_palette[i];
190 for (i = 0; i < 3; i++) {
191 int best_d = color_distance(refcolor[i], pseudopal[selected[i]]);
192 for (j = i + 1; j < 4; j++) {
193 int d = color_distance(refcolor[i], pseudopal[selected[j]]);
195 FFSWAP(int, selected[i], selected[j]);
202 for (i = 0; i < 4; i++) {
203 out_palette[i] = selected[i] ? (selected[i] - 1) & 0xF : 0;
204 out_alpha [i] = !selected[i] ? 0 : selected[i] < 17 ? 0x80 : 0xFF;
208 static void build_color_map(AVCodecContext *avctx, int cmap[],
209 const uint32_t palette[],
210 const int out_palette[], unsigned int const out_alpha[])
212 DVDSubtitleContext *dvdc = avctx->priv_data;
214 uint32_t pseudopal[4];
216 for (i = 0; i < 4; i++)
217 pseudopal[i] = (out_alpha[i] << 24) |
218 dvdc->global_palette[out_palette[i]];
219 for (i = 0; i < 256; i++) {
221 for (j = 0; j < 4; j++) {
222 d = color_distance(pseudopal[j], palette[i]);
231 static void copy_rectangle(AVSubtitleRect *dst, AVSubtitleRect *src, int cmap[])
236 p = src->pict.data[0];
237 q = dst->pict.data[0] + (src->x - dst->x) +
238 (src->y - dst->y) * dst->pict.linesize[0];
239 for (y = 0; y < src->h; y++) {
240 for (x = 0; x < src->w; x++)
241 *(q++) = cmap[*(p++)];
242 p += src->pict.linesize[0] - src->w;
243 q += dst->pict.linesize[0] - src->w;
247 static int encode_dvd_subtitles(AVCodecContext *avctx,
248 uint8_t *outbuf, int outbuf_size,
251 DVDSubtitleContext *dvdc = avctx->priv_data;
253 int offset1, offset2;
254 int i, rects = h->num_rects, ret;
255 unsigned global_palette_hits[33] = { 0 };
259 AVSubtitleRect vrect;
260 uint8_t *vrect_data = NULL;
264 if (rects == 0 || h->rects == NULL)
265 return AVERROR(EINVAL);
266 for (i = 0; i < rects; i++)
267 if (h->rects[i]->type != SUBTITLE_BITMAP) {
268 av_log(avctx, AV_LOG_ERROR, "Bitmap subtitle required\n");
269 return AVERROR(EINVAL);
271 /* Mark this subtitle forced if any of the rectangles is forced. */
272 for (i = 0; i < rects; i++)
273 if ((h->rects[i]->flags & AV_SUBTITLE_FLAG_FORCED) != 0) {
277 vrect = *h->rects[0];
280 /* DVD subtitles can have only one rectangle: build a virtual
281 rectangle containing all actual rectangles.
282 The data of the rectangles will be copied later, when the palette
283 is decided, because the rectangles may have different palettes. */
284 int xmin = h->rects[0]->x, xmax = xmin + h->rects[0]->w;
285 int ymin = h->rects[0]->y, ymax = ymin + h->rects[0]->h;
286 for (i = 1; i < rects; i++) {
287 xmin = FFMIN(xmin, h->rects[i]->x);
288 ymin = FFMIN(ymin, h->rects[i]->y);
289 xmax = FFMAX(xmax, h->rects[i]->x + h->rects[i]->w);
290 ymax = FFMAX(ymax, h->rects[i]->y + h->rects[i]->h);
294 vrect.w = xmax - xmin;
295 vrect.h = ymax - ymin;
296 if ((ret = av_image_check_size(vrect.w, vrect.h, 0, avctx)) < 0)
299 /* Count pixels outside the virtual rectangle as transparent */
300 global_palette_hits[0] = vrect.w * vrect.h;
301 for (i = 0; i < rects; i++)
302 global_palette_hits[0] -= h->rects[i]->w * h->rects[i]->h;
305 for (i = 0; i < rects; i++)
306 count_colors(avctx, global_palette_hits, h->rects[i]);
307 select_palette(avctx, out_palette, out_alpha, global_palette_hits);
310 if (!(vrect_data = av_calloc(vrect.w, vrect.h)))
311 return AVERROR(ENOMEM);
312 vrect.pict.data [0] = vrect_data;
313 vrect.pict.linesize[0] = vrect.w;
314 for (i = 0; i < rects; i++) {
315 build_color_map(avctx, cmap, (uint32_t *)h->rects[i]->pict.data[1],
316 out_palette, out_alpha);
317 copy_rectangle(&vrect, h->rects[i], cmap);
319 for (i = 0; i < 4; i++)
322 build_color_map(avctx, cmap, (uint32_t *)h->rects[0]->pict.data[1],
323 out_palette, out_alpha);
326 av_log(avctx, AV_LOG_DEBUG, "Selected palette:");
327 for (i = 0; i < 4; i++)
328 av_log(avctx, AV_LOG_DEBUG, " 0x%06x@@%02x (0x%x,0x%x)",
329 dvdc->global_palette[out_palette[i]], out_alpha[i],
330 out_palette[i], out_alpha[i] >> 4);
331 av_log(avctx, AV_LOG_DEBUG, "\n");
335 offset1 = q - outbuf;
336 // worst case memory requirement: 1 nibble per pixel..
337 if ((q - outbuf) + vrect.w * vrect.h / 2 + 17 + 21 > outbuf_size) {
338 av_log(NULL, AV_LOG_ERROR, "dvd_subtitle too big\n");
339 ret = AVERROR_BUFFER_TOO_SMALL;
342 dvd_encode_rle(&q, vrect.pict.data[0], vrect.w * 2,
343 vrect.w, (vrect.h + 1) >> 1, cmap);
344 offset2 = q - outbuf;
345 dvd_encode_rle(&q, vrect.pict.data[0] + vrect.w, vrect.w * 2,
346 vrect.w, vrect.h >> 1, cmap);
348 // set data packet size
350 bytestream_put_be16(&qq, q - outbuf);
352 // send start display command
353 bytestream_put_be16(&q, (h->start_display_time*90) >> 10);
354 bytestream_put_be16(&q, (q - outbuf) /*- 2 */ + 8 + 12 + 2);
355 *q++ = 0x03; // palette - 4 nibbles
356 *q++ = (out_palette[3] << 4) | out_palette[2];
357 *q++ = (out_palette[1] << 4) | out_palette[0];
358 *q++ = 0x04; // alpha - 4 nibbles
359 *q++ = (out_alpha[3] & 0xF0) | (out_alpha[2] >> 4);
360 *q++ = (out_alpha[1] & 0xF0) | (out_alpha[0] >> 4);
363 x2 = vrect.x + vrect.w - 1;
364 y2 = vrect.y + vrect.h - 1;
367 // x1 x2 -> 6 nibbles
369 *q++ = (vrect.x << 4) | ((x2 >> 8) & 0xf);
371 // y1 y2 -> 6 nibbles
373 *q++ = (vrect.y << 4) | ((y2 >> 8) & 0xf);
378 bytestream_put_be16(&q, offset1);
379 bytestream_put_be16(&q, offset2);
381 *q++ = forced ? 0x00 : 0x01; // start command
382 *q++ = 0xff; // terminating command
384 // send stop display command last
385 bytestream_put_be16(&q, (h->end_display_time*90) >> 10);
386 bytestream_put_be16(&q, (q - outbuf) - 2 /*+ 4*/);
387 *q++ = 0x02; // set end
388 *q++ = 0xff; // terminating command
391 bytestream_put_be16(&qq, q - outbuf);
393 av_log(NULL, AV_LOG_DEBUG, "subtitle_packet size=%"PTRDIFF_SPECIFIER"\n", q - outbuf);
401 static int dvdsub_init(AVCodecContext *avctx)
403 DVDSubtitleContext *dvdc = avctx->priv_data;
404 static const uint32_t default_palette[16] = {
405 0x000000, 0x0000FF, 0x00FF00, 0xFF0000,
406 0xFFFF00, 0xFF00FF, 0x00FFFF, 0xFFFFFF,
407 0x808000, 0x8080FF, 0x800080, 0x80FF80,
408 0x008080, 0xFF8080, 0x555555, 0xAAAAAA,
413 av_assert0(sizeof(dvdc->global_palette) == sizeof(default_palette));
414 memcpy(dvdc->global_palette, default_palette, sizeof(dvdc->global_palette));
416 av_bprint_init(&extradata, 0, 1);
417 if (avctx->width && avctx->height)
418 av_bprintf(&extradata, "size: %dx%d\n", avctx->width, avctx->height);
419 av_bprintf(&extradata, "palette:");
420 for (i = 0; i < 16; i++)
421 av_bprintf(&extradata, " %06"PRIx32"%c",
422 dvdc->global_palette[i] & 0xFFFFFF, i < 15 ? ',' : '\n');
424 ret = avpriv_bprint_to_extradata(avctx, &extradata);
431 static int dvdsub_encode(AVCodecContext *avctx,
432 unsigned char *buf, int buf_size,
433 const AVSubtitle *sub)
435 //DVDSubtitleContext *s = avctx->priv_data;
438 ret = encode_dvd_subtitles(avctx, buf, buf_size, sub);
442 AVCodec ff_dvdsub_encoder = {
444 .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),
445 .type = AVMEDIA_TYPE_SUBTITLE,
446 .id = AV_CODEC_ID_DVD_SUBTITLE,
448 .encode_sub = dvdsub_encode,
449 .priv_data_size = sizeof(DVDSubtitleContext),