2 * DVD subtitle encoding
3 * Copyright (c) 2005 Wolfram Gloger
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "bytestream.h"
27 // ncnt is the nibble counter
28 #define PUTNIBBLE(val)\
31 *q++ = bitbuf | ((val) & 0x0f);\
36 static void dvd_encode_rle(uint8_t **pq,
37 const uint8_t *bitmap, int linesize,
42 unsigned int bitbuf = 0;
48 for (y = 0; y < h; ++y) {
50 for(x = 0; x < w; x += len) {
52 for (len=1; x+len < w; ++len)
53 if (bitmap[x+len] != color)
58 PUTNIBBLE((len << 2)|color);
59 } else if (len < 0x10) {
61 PUTNIBBLE((len << 2)|color);
62 } else if (len < 0x40) {
65 PUTNIBBLE((len << 2)|color);
66 } else if (x+len == w) {
77 PUTNIBBLE((len << 2)|color);
89 static int encode_dvd_subtitles(uint8_t *outbuf, int outbuf_size,
94 int offset1[20], offset2[20];
95 int i, imax, color, alpha, rects = h->num_rects;
97 unsigned long hist[256];
100 if (rects == 0 || !h->rects)
105 // analyze bitmaps, compress to 4 colors
106 for (i=0; i<256; ++i) {
110 for (object_id = 0; object_id < rects; object_id++) {
112 FF_DISABLE_DEPRECATION_WARNINGS
113 if (!h->rects[object_id]->data[0]) {
114 AVSubtitleRect *rect = h->rects[object_id];
116 for (j = 0; j < 4; j++) {
117 rect->data[j] = rect->pict.data[j];
118 rect->linesize[j] = rect->pict.linesize[j];
121 FF_ENABLE_DEPRECATION_WARNINGS
124 for (i=0; i<h->rects[object_id]->w*h->rects[object_id]->h; ++i) {
125 color = h->rects[object_id]->data[0][i];
126 // only count non-transparent pixels
127 alpha = ((uint32_t *)h->rects[object_id]->data[1])[color] >> 24;
128 hist[color] += alpha;
131 for (color=3;; --color) {
134 for (i=0; i<256; ++i)
135 if (hist[i] > hmax) {
143 av_log(NULL, AV_LOG_DEBUG, "dvd_subtitle hist[%d]=%ld -> col %d\n",
144 imax, hist[imax], color);
152 for (object_id = 0; object_id < rects; object_id++) {
153 offset1[object_id] = q - outbuf;
154 // worst case memory requirement: 1 nibble per pixel..
155 if ((q - outbuf) + h->rects[object_id]->w*h->rects[object_id]->h/2
156 + 17*rects + 21 > outbuf_size) {
157 av_log(NULL, AV_LOG_ERROR, "dvd_subtitle too big\n");
160 dvd_encode_rle(&q, h->rects[object_id]->data[0],
161 h->rects[object_id]->w*2,
162 h->rects[object_id]->w, h->rects[object_id]->h >> 1,
164 offset2[object_id] = q - outbuf;
165 dvd_encode_rle(&q, h->rects[object_id]->data[0] + h->rects[object_id]->w,
166 h->rects[object_id]->w*2,
167 h->rects[object_id]->w, h->rects[object_id]->h >> 1,
171 // set data packet size
173 bytestream_put_be16(&qq, q - outbuf);
175 // send start display command
176 bytestream_put_be16(&q, (h->start_display_time*90) >> 10);
177 bytestream_put_be16(&q, (q - outbuf) /*- 2 */ + 8 + 12*rects + 2);
178 *q++ = 0x03; // palette - 4 nibbles
179 *q++ = 0x03; *q++ = 0x7f;
180 *q++ = 0x04; // alpha - 4 nibbles
181 *q++ = 0xf0; *q++ = 0x00;
182 //*q++ = 0x0f; *q++ = 0xff;
184 // XXX not sure if more than one rect can really be encoded..
186 for (object_id = 0; object_id < rects; object_id++) {
187 int x2 = h->rects[object_id]->x + h->rects[object_id]->w - 1;
188 int y2 = h->rects[object_id]->y + h->rects[object_id]->h - 1;
191 // x1 x2 -> 6 nibbles
192 *q++ = h->rects[object_id]->x >> 4;
193 *q++ = (h->rects[object_id]->x << 4) | ((x2 >> 8) & 0xf);
195 // y1 y2 -> 6 nibbles
196 *q++ = h->rects[object_id]->y >> 4;
197 *q++ = (h->rects[object_id]->y << 4) | ((y2 >> 8) & 0xf);
202 bytestream_put_be16(&q, offset1[object_id]);
203 bytestream_put_be16(&q, offset2[object_id]);
205 *q++ = 0x01; // start command
206 *q++ = 0xff; // terminating command
208 // send stop display command last
209 bytestream_put_be16(&q, (h->end_display_time*90) >> 10);
210 bytestream_put_be16(&q, (q - outbuf) - 2 /*+ 4*/);
211 *q++ = 0x02; // set end
212 *q++ = 0xff; // terminating command
215 bytestream_put_be16(&qq, q - outbuf);
217 av_log(NULL, AV_LOG_DEBUG, "subtitle_packet size=%td\n", q - outbuf);
221 static int dvdsub_encode(AVCodecContext *avctx,
222 unsigned char *buf, int buf_size,
223 const AVSubtitle *sub)
225 //DVDSubtitleContext *s = avctx->priv_data;
228 ret = encode_dvd_subtitles(buf, buf_size, sub);
232 AVCodec ff_dvdsub_encoder = {
234 .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),
235 .type = AVMEDIA_TYPE_SUBTITLE,
236 .id = AV_CODEC_ID_DVD_SUBTITLE,
237 .encode_sub = dvdsub_encode,