2 * DVD subtitle encoding for ffmpeg
3 * Copyright (c) 2005 Wolfram Gloger.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 typedef struct DVDSubtitleContext
{
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 inline void putbe16(uint8_t **pq
, uint16_t v
)
97 static int encode_dvd_subtitles(uint8_t *outbuf
, int outbuf_size
,
102 int offset1
[20], offset2
[20];
103 int i
, imax
, color
, alpha
, rects
= h
->num_rects
;
105 unsigned long hist
[256];
108 if (rects
== 0 || h
->rects
== NULL
)
113 // analyze bitmaps, compress to 4 colors
114 for (i
=0; i
<256; ++i
) {
118 for (object_id
= 0; object_id
< rects
; object_id
++)
119 for (i
=0; i
<h
->rects
[object_id
].w
*h
->rects
[object_id
].h
; ++i
) {
120 color
= h
->rects
[object_id
].bitmap
[i
];
121 // only count non-transparent pixels
122 alpha
= h
->rects
[object_id
].rgba_palette
[color
] >> 24;
123 hist
[color
] += alpha
;
125 for (color
=3;; --color
) {
128 for (i
=0; i
<256; ++i
)
129 if (hist
[i
] > hmax
) {
137 av_log(NULL
, AV_LOG_DEBUG
, "dvd_subtitle hist[%d]=%ld -> col %d\n",
138 imax
, hist
[imax
], color
);
146 for (object_id
= 0; object_id
< rects
; object_id
++) {
147 offset1
[object_id
] = q
- outbuf
;
148 // worst case memory requirement: 1 nibble per pixel..
149 if ((q
- outbuf
) + h
->rects
[object_id
].w
*h
->rects
[object_id
].h
/2
150 + 17*rects
+ 21 > outbuf_size
) {
151 av_log(NULL
, AV_LOG_ERROR
, "dvd_subtitle too big\n");
154 dvd_encode_rle(&q
, h
->rects
[object_id
].bitmap
,
155 h
->rects
[object_id
].w
*2,
156 h
->rects
[object_id
].w
, h
->rects
[object_id
].h
>> 1,
158 offset2
[object_id
] = q
- outbuf
;
159 dvd_encode_rle(&q
, h
->rects
[object_id
].bitmap
+ h
->rects
[object_id
].w
,
160 h
->rects
[object_id
].w
*2,
161 h
->rects
[object_id
].w
, h
->rects
[object_id
].h
>> 1,
165 // set data packet size
167 putbe16(&qq
, q
- outbuf
);
169 // send start display command
170 putbe16(&q
, (h
->start_display_time
*90) >> 10);
171 putbe16(&q
, (q
- outbuf
) /*- 2 */ + 8 + 12*rects
+ 2);
172 *q
++ = 0x03; // palette - 4 nibbles
173 *q
++ = 0x03; *q
++ = 0x7f;
174 *q
++ = 0x04; // alpha - 4 nibbles
175 *q
++ = 0xf0; *q
++ = 0x00;
176 //*q++ = 0x0f; *q++ = 0xff;
178 // XXX not sure if more than one rect can really be encoded..
180 for (object_id
= 0; object_id
< rects
; object_id
++) {
181 int x2
= h
->rects
[object_id
].x
+ h
->rects
[object_id
].w
- 1;
182 int y2
= h
->rects
[object_id
].y
+ h
->rects
[object_id
].h
- 1;
185 // x1 x2 -> 6 nibbles
186 *q
++ = h
->rects
[object_id
].x
>> 4;
187 *q
++ = (h
->rects
[object_id
].x
<< 4) | ((x2
>> 8) & 0xf);
189 // y1 y2 -> 6 nibbles
190 *q
++ = h
->rects
[object_id
].y
>> 4;
191 *q
++ = (h
->rects
[object_id
].y
<< 4) | ((y2
>> 8) & 0xf);
196 putbe16(&q
, offset1
[object_id
]);
197 putbe16(&q
, offset2
[object_id
]);
199 *q
++ = 0x01; // start command
200 *q
++ = 0xff; // terminating command
202 // send stop display command last
203 putbe16(&q
, (h
->end_display_time
*90) >> 10);
204 putbe16(&q
, (q
- outbuf
) - 2 /*+ 4*/);
205 *q
++ = 0x02; // set end
206 *q
++ = 0xff; // terminating command
209 putbe16(&qq
, q
- outbuf
);
211 av_log(NULL
, AV_LOG_DEBUG
, "subtitle_packet size=%td\n", q
- outbuf
);
215 static int dvdsub_init_encoder(AVCodecContext
*avctx
)
220 static int dvdsub_close_encoder(AVCodecContext
*avctx
)
225 static int dvdsub_encode(AVCodecContext
*avctx
,
226 unsigned char *buf
, int buf_size
, void *data
)
228 //DVDSubtitleContext *s = avctx->priv_data;
229 AVSubtitle
*sub
= data
;
232 ret
= encode_dvd_subtitles(buf
, buf_size
, sub
);
236 AVCodec dvdsub_encoder
= {
239 CODEC_ID_DVD_SUBTITLE
,
240 sizeof(DVDSubtitleContext
),
243 dvdsub_close_encoder
,
246 /* Local Variables: */
247 /* c-basic-offset:4 */