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"
23 #include "libavutil/avassert.h"
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)
55 av_assert0(color < 4);
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 int encode_dvd_subtitles(uint8_t *outbuf, int outbuf_size,
93 int offset1[20], offset2[20];
94 int i, imax, color, alpha, rects = h->num_rects;
96 unsigned long hist[256];
99 if (rects == 0 || h->rects == NULL)
104 // analyze bitmaps, compress to 4 colors
105 for (i=0; i<256; ++i) {
109 for (object_id = 0; object_id < rects; object_id++)
110 for (i=0; i<h->rects[object_id]->w*h->rects[object_id]->h; ++i) {
111 color = h->rects[object_id]->pict.data[0][i];
112 // only count non-transparent pixels
113 alpha = ((uint32_t*)h->rects[object_id]->pict.data[1])[color] >> 24;
114 hist[color] += alpha;
116 for (color=3;; --color) {
119 for (i=0; i<256; ++i)
120 if (hist[i] > hmax) {
128 av_log(NULL, AV_LOG_DEBUG, "dvd_subtitle hist[%d]=%ld -> col %d\n",
129 imax, hist[imax], color);
137 for (object_id = 0; object_id < rects; object_id++) {
138 offset1[object_id] = q - outbuf;
139 // worst case memory requirement: 1 nibble per pixel..
140 if ((q - outbuf) + h->rects[object_id]->w*h->rects[object_id]->h/2
141 + 17*rects + 21 > outbuf_size) {
142 av_log(NULL, AV_LOG_ERROR, "dvd_subtitle too big\n");
145 dvd_encode_rle(&q, h->rects[object_id]->pict.data[0],
146 h->rects[object_id]->w*2,
147 h->rects[object_id]->w, h->rects[object_id]->h >> 1,
149 offset2[object_id] = q - outbuf;
150 dvd_encode_rle(&q, h->rects[object_id]->pict.data[0] + h->rects[object_id]->w,
151 h->rects[object_id]->w*2,
152 h->rects[object_id]->w, h->rects[object_id]->h >> 1,
156 // set data packet size
158 bytestream_put_be16(&qq, q - outbuf);
160 // send start display command
161 bytestream_put_be16(&q, (h->start_display_time*90) >> 10);
162 bytestream_put_be16(&q, (q - outbuf) /*- 2 */ + 8 + 12*rects + 2);
163 *q++ = 0x03; // palette - 4 nibbles
164 *q++ = 0x03; *q++ = 0x7f;
165 *q++ = 0x04; // alpha - 4 nibbles
166 *q++ = 0xf0; *q++ = 0x00;
167 //*q++ = 0x0f; *q++ = 0xff;
169 // XXX not sure if more than one rect can really be encoded..
171 for (object_id = 0; object_id < rects; object_id++) {
172 int x2 = h->rects[object_id]->x + h->rects[object_id]->w - 1;
173 int y2 = h->rects[object_id]->y + h->rects[object_id]->h - 1;
176 // x1 x2 -> 6 nibbles
177 *q++ = h->rects[object_id]->x >> 4;
178 *q++ = (h->rects[object_id]->x << 4) | ((x2 >> 8) & 0xf);
180 // y1 y2 -> 6 nibbles
181 *q++ = h->rects[object_id]->y >> 4;
182 *q++ = (h->rects[object_id]->y << 4) | ((y2 >> 8) & 0xf);
187 bytestream_put_be16(&q, offset1[object_id]);
188 bytestream_put_be16(&q, offset2[object_id]);
190 *q++ = 0x01; // start command
191 *q++ = 0xff; // terminating command
193 // send stop display command last
194 bytestream_put_be16(&q, (h->end_display_time*90) >> 10);
195 bytestream_put_be16(&q, (q - outbuf) - 2 /*+ 4*/);
196 *q++ = 0x02; // set end
197 *q++ = 0xff; // terminating command
200 bytestream_put_be16(&qq, q - outbuf);
202 av_log(NULL, AV_LOG_DEBUG, "subtitle_packet size=%td\n", q - outbuf);
206 static int dvdsub_encode(AVCodecContext *avctx,
207 unsigned char *buf, int buf_size, void *data)
209 //DVDSubtitleContext *s = avctx->priv_data;
210 AVSubtitle *sub = data;
213 ret = encode_dvd_subtitles(buf, buf_size, sub);
217 AVCodec ff_dvdsub_encoder = {
219 .type = AVMEDIA_TYPE_SUBTITLE,
220 .id = AV_CODEC_ID_DVD_SUBTITLE,
221 .encode = dvdsub_encode,
222 .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),