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 typedef struct DVDSubtitleContext {
29 // ncnt is the nibble counter
30 #define PUTNIBBLE(val)\
33 *q++ = bitbuf | ((val) & 0x0f);\
38 static void dvd_encode_rle(uint8_t **pq,
39 const uint8_t *bitmap, int linesize,
44 unsigned int bitbuf = 0;
50 for (y = 0; y < h; ++y) {
52 for(x = 0; x < w; x += len) {
54 for (len=1; x+len < w; ++len)
55 if (bitmap[x+len] != color)
60 PUTNIBBLE((len << 2)|color);
61 } else if (len < 0x10) {
63 PUTNIBBLE((len << 2)|color);
64 } else if (len < 0x40) {
67 PUTNIBBLE((len << 2)|color);
68 } else if (x+len == w) {
79 PUTNIBBLE((len << 2)|color);
91 static inline void putbe16(uint8_t **pq, uint16_t v)
99 static int encode_dvd_subtitles(uint8_t *outbuf, int outbuf_size,
104 int offset1[20], offset2[20];
105 int i, imax, color, alpha, rects = h->num_rects;
107 unsigned long hist[256];
110 if (rects == 0 || h->rects == NULL)
115 // analyze bitmaps, compress to 4 colors
116 for (i=0; i<256; ++i) {
120 for (object_id = 0; object_id < rects; object_id++)
121 for (i=0; i<h->rects[object_id].w*h->rects[object_id].h; ++i) {
122 color = h->rects[object_id].bitmap[i];
123 // only count non-transparent pixels
124 alpha = h->rects[object_id].rgba_palette[color] >> 24;
125 hist[color] += alpha;
127 for (color=3;; --color) {
130 for (i=0; i<256; ++i)
131 if (hist[i] > hmax) {
139 av_log(NULL, AV_LOG_DEBUG, "dvd_subtitle hist[%d]=%ld -> col %d\n",
140 imax, hist[imax], color);
148 for (object_id = 0; object_id < rects; object_id++) {
149 offset1[object_id] = q - outbuf;
150 // worst case memory requirement: 1 nibble per pixel..
151 if ((q - outbuf) + h->rects[object_id].w*h->rects[object_id].h/2
152 + 17*rects + 21 > outbuf_size) {
153 av_log(NULL, AV_LOG_ERROR, "dvd_subtitle too big\n");
156 dvd_encode_rle(&q, h->rects[object_id].bitmap,
157 h->rects[object_id].w*2,
158 h->rects[object_id].w, h->rects[object_id].h >> 1,
160 offset2[object_id] = q - outbuf;
161 dvd_encode_rle(&q, h->rects[object_id].bitmap + h->rects[object_id].w,
162 h->rects[object_id].w*2,
163 h->rects[object_id].w, h->rects[object_id].h >> 1,
167 // set data packet size
169 putbe16(&qq, q - outbuf);
171 // send start display command
172 putbe16(&q, (h->start_display_time*90) >> 10);
173 putbe16(&q, (q - outbuf) /*- 2 */ + 8 + 12*rects + 2);
174 *q++ = 0x03; // palette - 4 nibbles
175 *q++ = 0x03; *q++ = 0x7f;
176 *q++ = 0x04; // alpha - 4 nibbles
177 *q++ = 0xf0; *q++ = 0x00;
178 //*q++ = 0x0f; *q++ = 0xff;
180 // XXX not sure if more than one rect can really be encoded..
182 for (object_id = 0; object_id < rects; object_id++) {
183 int x2 = h->rects[object_id].x + h->rects[object_id].w - 1;
184 int y2 = h->rects[object_id].y + h->rects[object_id].h - 1;
187 // x1 x2 -> 6 nibbles
188 *q++ = h->rects[object_id].x >> 4;
189 *q++ = (h->rects[object_id].x << 4) | ((x2 >> 8) & 0xf);
191 // y1 y2 -> 6 nibbles
192 *q++ = h->rects[object_id].y >> 4;
193 *q++ = (h->rects[object_id].y << 4) | ((y2 >> 8) & 0xf);
198 putbe16(&q, offset1[object_id]);
199 putbe16(&q, offset2[object_id]);
201 *q++ = 0x01; // start command
202 *q++ = 0xff; // terminating command
204 // send stop display command last
205 putbe16(&q, (h->end_display_time*90) >> 10);
206 putbe16(&q, (q - outbuf) - 2 /*+ 4*/);
207 *q++ = 0x02; // set end
208 *q++ = 0xff; // terminating command
211 putbe16(&qq, q - outbuf);
213 av_log(NULL, AV_LOG_DEBUG, "subtitle_packet size=%td\n", q - outbuf);
217 static int dvdsub_init_encoder(AVCodecContext *avctx)
222 static int dvdsub_close_encoder(AVCodecContext *avctx)
227 static int dvdsub_encode(AVCodecContext *avctx,
228 unsigned char *buf, int buf_size, void *data)
230 //DVDSubtitleContext *s = avctx->priv_data;
231 AVSubtitle *sub = data;
234 ret = encode_dvd_subtitles(buf, buf_size, sub);
238 AVCodec dvdsub_encoder = {
241 CODEC_ID_DVD_SUBTITLE,
242 sizeof(DVDSubtitleContext),
245 dvdsub_close_encoder,
248 /* Local Variables: */
249 /* c-basic-offset:4 */