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
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 == NULL)
105 // analyze bitmaps, compress to 4 colors
106 for (i=0; i<256; ++i) {
110 for (object_id = 0; object_id < rects; object_id++)
111 for (i=0; i<h->rects[object_id].w*h->rects[object_id].h; ++i) {
112 color = h->rects[object_id].bitmap[i];
113 // only count non-transparent pixels
114 alpha = h->rects[object_id].rgba_palette[color] >> 24;
115 hist[color] += alpha;
117 for (color=3;; --color) {
120 for (i=0; i<256; ++i)
121 if (hist[i] > hmax) {
129 av_log(NULL, AV_LOG_DEBUG, "dvd_subtitle hist[%d]=%ld -> col %d\n",
130 imax, hist[imax], color);
138 for (object_id = 0; object_id < rects; object_id++) {
139 offset1[object_id] = q - outbuf;
140 // worst case memory requirement: 1 nibble per pixel..
141 if ((q - outbuf) + h->rects[object_id].w*h->rects[object_id].h/2
142 + 17*rects + 21 > outbuf_size) {
143 av_log(NULL, AV_LOG_ERROR, "dvd_subtitle too big\n");
146 dvd_encode_rle(&q, h->rects[object_id].bitmap,
147 h->rects[object_id].w*2,
148 h->rects[object_id].w, h->rects[object_id].h >> 1,
150 offset2[object_id] = q - outbuf;
151 dvd_encode_rle(&q, h->rects[object_id].bitmap + h->rects[object_id].w,
152 h->rects[object_id].w*2,
153 h->rects[object_id].w, h->rects[object_id].h >> 1,
157 // set data packet size
159 bytestream_put_be16(&qq, q - outbuf);
161 // send start display command
162 bytestream_put_be16(&q, (h->start_display_time*90) >> 10);
163 bytestream_put_be16(&q, (q - outbuf) /*- 2 */ + 8 + 12*rects + 2);
164 *q++ = 0x03; // palette - 4 nibbles
165 *q++ = 0x03; *q++ = 0x7f;
166 *q++ = 0x04; // alpha - 4 nibbles
167 *q++ = 0xf0; *q++ = 0x00;
168 //*q++ = 0x0f; *q++ = 0xff;
170 // XXX not sure if more than one rect can really be encoded..
172 for (object_id = 0; object_id < rects; object_id++) {
173 int x2 = h->rects[object_id].x + h->rects[object_id].w - 1;
174 int y2 = h->rects[object_id].y + h->rects[object_id].h - 1;
177 // x1 x2 -> 6 nibbles
178 *q++ = h->rects[object_id].x >> 4;
179 *q++ = (h->rects[object_id].x << 4) | ((x2 >> 8) & 0xf);
181 // y1 y2 -> 6 nibbles
182 *q++ = h->rects[object_id].y >> 4;
183 *q++ = (h->rects[object_id].y << 4) | ((y2 >> 8) & 0xf);
188 bytestream_put_be16(&q, offset1[object_id]);
189 bytestream_put_be16(&q, offset2[object_id]);
191 *q++ = 0x01; // start command
192 *q++ = 0xff; // terminating command
194 // send stop display command last
195 bytestream_put_be16(&q, (h->end_display_time*90) >> 10);
196 bytestream_put_be16(&q, (q - outbuf) - 2 /*+ 4*/);
197 *q++ = 0x02; // set end
198 *q++ = 0xff; // terminating command
201 bytestream_put_be16(&qq, q - outbuf);
203 av_log(NULL, AV_LOG_DEBUG, "subtitle_packet size=%td\n", q - outbuf);
207 static int dvdsub_init_encoder(AVCodecContext *avctx)
212 static int dvdsub_close_encoder(AVCodecContext *avctx)
217 static int dvdsub_encode(AVCodecContext *avctx,
218 unsigned char *buf, int buf_size, void *data)
220 //DVDSubtitleContext *s = avctx->priv_data;
221 AVSubtitle *sub = data;
224 ret = encode_dvd_subtitles(buf, buf_size, sub);
228 AVCodec dvdsub_encoder = {
231 CODEC_ID_DVD_SUBTITLE,
235 dvdsub_close_encoder,
238 /* Local Variables: */
239 /* c-basic-offset:4 */