2 * DVD subtitle encoding for ffmpeg
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
26 // ncnt is the nibble counter
27 #define PUTNIBBLE(val)\
30 *q++ = bitbuf | ((val) & 0x0f);\
35 static void dvd_encode_rle(uint8_t **pq,
36 const uint8_t *bitmap, int linesize,
41 unsigned int bitbuf = 0;
47 for (y = 0; y < h; ++y) {
49 for(x = 0; x < w; x += len) {
51 for (len=1; x+len < w; ++len)
52 if (bitmap[x+len] != color)
57 PUTNIBBLE((len << 2)|color);
58 } else if (len < 0x10) {
60 PUTNIBBLE((len << 2)|color);
61 } else if (len < 0x40) {
64 PUTNIBBLE((len << 2)|color);
65 } else if (x+len == w) {
76 PUTNIBBLE((len << 2)|color);
88 static inline void putbe16(uint8_t **pq, uint16_t v)
96 static int encode_dvd_subtitles(uint8_t *outbuf, int outbuf_size,
101 int offset1[20], offset2[20];
102 int i, imax, color, alpha, rects = h->num_rects;
104 unsigned long hist[256];
107 if (rects == 0 || h->rects == NULL)
112 // analyze bitmaps, compress to 4 colors
113 for (i=0; i<256; ++i) {
117 for (object_id = 0; object_id < rects; object_id++)
118 for (i=0; i<h->rects[object_id].w*h->rects[object_id].h; ++i) {
119 color = h->rects[object_id].bitmap[i];
120 // only count non-transparent pixels
121 alpha = h->rects[object_id].rgba_palette[color] >> 24;
122 hist[color] += alpha;
124 for (color=3;; --color) {
127 for (i=0; i<256; ++i)
128 if (hist[i] > hmax) {
136 av_log(NULL, AV_LOG_DEBUG, "dvd_subtitle hist[%d]=%ld -> col %d\n",
137 imax, hist[imax], color);
145 for (object_id = 0; object_id < rects; object_id++) {
146 offset1[object_id] = q - outbuf;
147 // worst case memory requirement: 1 nibble per pixel..
148 if ((q - outbuf) + h->rects[object_id].w*h->rects[object_id].h/2
149 + 17*rects + 21 > outbuf_size) {
150 av_log(NULL, AV_LOG_ERROR, "dvd_subtitle too big\n");
153 dvd_encode_rle(&q, h->rects[object_id].bitmap,
154 h->rects[object_id].w*2,
155 h->rects[object_id].w, h->rects[object_id].h >> 1,
157 offset2[object_id] = q - outbuf;
158 dvd_encode_rle(&q, h->rects[object_id].bitmap + h->rects[object_id].w,
159 h->rects[object_id].w*2,
160 h->rects[object_id].w, h->rects[object_id].h >> 1,
164 // set data packet size
166 putbe16(&qq, q - outbuf);
168 // send start display command
169 putbe16(&q, (h->start_display_time*90) >> 10);
170 putbe16(&q, (q - outbuf) /*- 2 */ + 8 + 12*rects + 2);
171 *q++ = 0x03; // palette - 4 nibbles
172 *q++ = 0x03; *q++ = 0x7f;
173 *q++ = 0x04; // alpha - 4 nibbles
174 *q++ = 0xf0; *q++ = 0x00;
175 //*q++ = 0x0f; *q++ = 0xff;
177 // XXX not sure if more than one rect can really be encoded..
179 for (object_id = 0; object_id < rects; object_id++) {
180 int x2 = h->rects[object_id].x + h->rects[object_id].w - 1;
181 int y2 = h->rects[object_id].y + h->rects[object_id].h - 1;
184 // x1 x2 -> 6 nibbles
185 *q++ = h->rects[object_id].x >> 4;
186 *q++ = (h->rects[object_id].x << 4) | ((x2 >> 8) & 0xf);
188 // y1 y2 -> 6 nibbles
189 *q++ = h->rects[object_id].y >> 4;
190 *q++ = (h->rects[object_id].y << 4) | ((y2 >> 8) & 0xf);
195 putbe16(&q, offset1[object_id]);
196 putbe16(&q, offset2[object_id]);
198 *q++ = 0x01; // start command
199 *q++ = 0xff; // terminating command
201 // send stop display command last
202 putbe16(&q, (h->end_display_time*90) >> 10);
203 putbe16(&q, (q - outbuf) - 2 /*+ 4*/);
204 *q++ = 0x02; // set end
205 *q++ = 0xff; // terminating command
208 putbe16(&qq, q - outbuf);
210 av_log(NULL, AV_LOG_DEBUG, "subtitle_packet size=%td\n", q - outbuf);
214 static int dvdsub_init_encoder(AVCodecContext *avctx)
219 static int dvdsub_close_encoder(AVCodecContext *avctx)
224 static int dvdsub_encode(AVCodecContext *avctx,
225 unsigned char *buf, int buf_size, void *data)
227 //DVDSubtitleContext *s = avctx->priv_data;
228 AVSubtitle *sub = data;
231 ret = encode_dvd_subtitles(buf, buf_size, sub);
235 AVCodec dvdsub_encoder = {
238 CODEC_ID_DVD_SUBTITLE,
242 dvdsub_close_encoder,
245 /* Local Variables: */
246 /* c-basic-offset:4 */