2756
|
1 /*
|
|
2 * DVD subtitle decoding for ffmpeg
|
|
3 * Copyright (c) 2005 Fabrice Bellard.
|
|
4 *
|
|
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.
|
|
9 *
|
|
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.
|
|
14 *
|
|
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
18 */
|
|
19 #include "avcodec.h"
|
|
20
|
|
21 //#define DEBUG
|
|
22
|
|
23 typedef struct DVDSubContext {
|
|
24 } DVDSubContext;
|
|
25
|
|
26 static int dvdsub_init_decoder(AVCodecContext *avctx)
|
|
27 {
|
|
28 return 0;
|
|
29 }
|
|
30
|
|
31 uint16_t getbe16(const uint8_t *p)
|
|
32 {
|
|
33 return (p[0] << 8) | p[1];
|
|
34 }
|
|
35
|
|
36 int get_nibble(const uint8_t *buf, int nibble_offset)
|
|
37 {
|
|
38 return (buf[nibble_offset >> 1] >> ((1 - (nibble_offset & 1)) << 2)) & 0xf;
|
|
39 }
|
|
40
|
|
41 static int decode_rle(uint8_t *bitmap, int linesize, int w, int h,
|
|
42 const uint8_t *buf, int nibble_offset, int buf_size)
|
|
43 {
|
|
44 unsigned int v;
|
|
45 int x, y, len, color, nibble_end;
|
|
46 uint8_t *d;
|
|
47
|
|
48 nibble_end = buf_size * 2;
|
|
49 x = 0;
|
|
50 y = 0;
|
|
51 d = bitmap;
|
|
52 for(;;) {
|
|
53 if (nibble_offset >= nibble_end)
|
|
54 return -1;
|
|
55 v = get_nibble(buf, nibble_offset++);
|
|
56 if (v < 0x4) {
|
|
57 v = (v << 4) | get_nibble(buf, nibble_offset++);
|
|
58 if (v < 0x10) {
|
|
59 v = (v << 4) | get_nibble(buf, nibble_offset++);
|
|
60 if (v < 0x040) {
|
|
61 v = (v << 4) | get_nibble(buf, nibble_offset++);
|
|
62 if (v < 4) {
|
|
63 v |= (w - x) << 2;
|
|
64 }
|
|
65 }
|
|
66 }
|
|
67 }
|
|
68 len = v >> 2;
|
|
69 if (len > (w - x))
|
|
70 len = (w - x);
|
|
71 color = v & 0x03;
|
|
72 memset(d + x, color, len);
|
|
73 x += len;
|
|
74 if (x >= w) {
|
|
75 y++;
|
|
76 if (y >= h)
|
|
77 break;
|
|
78 d += linesize;
|
|
79 x = 0;
|
|
80 /* byte align */
|
|
81 nibble_offset += (nibble_offset & 1);
|
|
82 }
|
|
83 }
|
|
84 return 0;
|
|
85 }
|
|
86
|
|
87 static void guess_palette(uint32_t *rgba_palette,
|
|
88 uint8_t *palette,
|
|
89 uint8_t *alpha,
|
|
90 uint32_t subtitle_color)
|
|
91 {
|
|
92 uint8_t color_used[16];
|
|
93 int nb_opaque_colors, i, level, j, r, g, b;
|
|
94
|
|
95 for(i = 0; i < 4; i++)
|
|
96 rgba_palette[i] = 0;
|
|
97
|
|
98 memset(color_used, 0, 16);
|
|
99 nb_opaque_colors = 0;
|
|
100 for(i = 0; i < 4; i++) {
|
|
101 if (alpha[i] != 0 && !color_used[palette[i]]) {
|
|
102 color_used[palette[i]] = 1;
|
|
103 nb_opaque_colors++;
|
|
104 }
|
|
105 }
|
|
106
|
|
107 if (nb_opaque_colors == 0)
|
|
108 return;
|
|
109
|
|
110 j = 0;
|
|
111 memset(color_used, 0, 16);
|
|
112 for(i = 0; i < 4; i++) {
|
|
113 if (alpha[i] != 0) {
|
|
114 if (!color_used[palette[i]]) {
|
|
115 level = (0xff * (j + 1)) / nb_opaque_colors;
|
|
116 r = (((subtitle_color >> 16) & 0xff) * level) >> 8;
|
|
117 g = (((subtitle_color >> 8) & 0xff) * level) >> 8;
|
|
118 b = (((subtitle_color >> 0) & 0xff) * level) >> 8;
|
|
119 rgba_palette[i] = b | (g << 8) | (r << 16) | (0xff << 24);
|
|
120 color_used[palette[i]] = (i + 1);
|
|
121 j++;
|
|
122 } else {
|
|
123 rgba_palette[i] = rgba_palette[color_used[palette[i]] - 1];
|
|
124 }
|
|
125 }
|
|
126 }
|
|
127 }
|
|
128
|
|
129 static int decode_dvd_subtitles(AVSubtitle *sub_header,
|
|
130 const uint8_t *buf, int buf_size)
|
|
131 {
|
|
132 int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos;
|
|
133 uint8_t palette[4], alpha[4];
|
|
134 int date;
|
2797
|
135 int i;
|
2756
|
136
|
|
137 if (buf_size < 4)
|
|
138 return -1;
|
2797
|
139 sub_header->rects = NULL;
|
|
140 sub_header->num_rects = 0;
|
2756
|
141 sub_header->start_display_time = 0;
|
|
142 sub_header->end_display_time = 0;
|
|
143
|
|
144 cmd_pos = getbe16(buf + 2);
|
|
145 while ((cmd_pos + 4) < buf_size) {
|
|
146 date = getbe16(buf + cmd_pos);
|
|
147 next_cmd_pos = getbe16(buf + cmd_pos + 2);
|
|
148 #ifdef DEBUG
|
|
149 av_log(NULL, AV_LOG_INFO, "cmd_pos=0x%04x next=0x%04x date=%d\n",
|
|
150 cmd_pos, next_cmd_pos, date);
|
|
151 #endif
|
|
152 pos = cmd_pos + 4;
|
|
153 offset1 = -1;
|
|
154 offset2 = -1;
|
|
155 x1 = y1 = x2 = y2 = 0;
|
|
156 while (pos < buf_size) {
|
|
157 cmd = buf[pos++];
|
|
158 #ifdef DEBUG
|
|
159 av_log(NULL, AV_LOG_INFO, "cmd=%02x\n", cmd);
|
|
160 #endif
|
|
161 switch(cmd) {
|
|
162 case 0x00:
|
|
163 /* force display */
|
|
164 break;
|
|
165 case 0x01:
|
|
166 /* set start date */
|
|
167 sub_header->start_display_time = date * 10;
|
|
168 break;
|
|
169 case 0x02:
|
|
170 /* set end date */
|
|
171 sub_header->end_display_time = date * 10;
|
|
172 break;
|
|
173 case 0x03:
|
|
174 /* set palette */
|
|
175 if ((buf_size - pos) < 2)
|
|
176 goto fail;
|
|
177 palette[0] = buf[pos] >> 4;
|
|
178 palette[1] = buf[pos] & 0x0f;
|
|
179 palette[2] = buf[pos + 1] >> 4;
|
|
180 palette[3] = buf[pos + 1] & 0x0f;
|
|
181 pos += 2;
|
|
182 break;
|
|
183 case 0x04:
|
|
184 /* set alpha */
|
|
185 if ((buf_size - pos) < 2)
|
|
186 goto fail;
|
|
187 alpha[0] = buf[pos] >> 4;
|
|
188 alpha[1] = buf[pos] & 0x0f;
|
|
189 alpha[2] = buf[pos + 1] >> 4;
|
|
190 alpha[3] = buf[pos + 1] & 0x0f;
|
|
191 pos += 2;
|
|
192 break;
|
|
193 case 0x05:
|
|
194 if ((buf_size - pos) < 6)
|
|
195 goto fail;
|
|
196 x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
|
|
197 x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
|
|
198 y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
|
|
199 y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
|
|
200 #ifdef DEBUG
|
|
201 av_log(NULL, AV_LOG_INFO, "x1=%d x2=%d y1=%d y2=%d\n",
|
|
202 x1, x2, y1, y2);
|
|
203 #endif
|
|
204 pos += 6;
|
|
205 break;
|
|
206 case 0x06:
|
|
207 if ((buf_size - pos) < 4)
|
|
208 goto fail;
|
|
209 offset1 = getbe16(buf + pos);
|
|
210 offset2 = getbe16(buf + pos + 2);
|
|
211 #ifdef DEBUG
|
|
212 av_log(NULL, AV_LOG_INFO, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
|
|
213 #endif
|
|
214 pos += 4;
|
|
215 break;
|
|
216 case 0xff:
|
|
217 default:
|
|
218 goto the_end;
|
|
219 }
|
|
220 }
|
|
221 the_end:
|
|
222 if (offset1 >= 0) {
|
|
223 int w, h;
|
|
224 uint8_t *bitmap;
|
|
225
|
|
226 /* decode the bitmap */
|
|
227 w = x2 - x1 + 1;
|
|
228 if (w < 0)
|
|
229 w = 0;
|
|
230 h = y2 - y1;
|
|
231 if (h < 0)
|
|
232 h = 0;
|
|
233 if (w > 0 && h > 0) {
|
2797
|
234 if (sub_header->rects != NULL) {
|
|
235 for (i = 0; i < sub_header->num_rects; i++) {
|
|
236 av_free(sub_header->rects[i].bitmap);
|
|
237 av_free(sub_header->rects[i].rgba_palette);
|
|
238 }
|
|
239 av_freep(&sub_header->rects);
|
|
240 sub_header->num_rects = 0;
|
|
241 }
|
|
242
|
2756
|
243 bitmap = av_malloc(w * h);
|
2797
|
244 sub_header->rects = av_mallocz(sizeof(AVSubtitleRect));
|
|
245 sub_header->num_rects = 1;
|
|
246 sub_header->rects[0].rgba_palette = av_malloc(4 * 4);
|
2756
|
247 decode_rle(bitmap, w * 2, w, h / 2,
|
|
248 buf, offset1 * 2, buf_size);
|
|
249 decode_rle(bitmap + w, w * 2, w, h / 2,
|
|
250 buf, offset2 * 2, buf_size);
|
2797
|
251 guess_palette(sub_header->rects[0].rgba_palette,
|
2756
|
252 palette, alpha, 0xffff00);
|
2797
|
253 sub_header->rects[0].x = x1;
|
|
254 sub_header->rects[0].y = y1;
|
|
255 sub_header->rects[0].w = w;
|
|
256 sub_header->rects[0].h = h;
|
|
257 sub_header->rects[0].nb_colors = 4;
|
|
258 sub_header->rects[0].linesize = w;
|
|
259 sub_header->rects[0].bitmap = bitmap;
|
2756
|
260 }
|
|
261 }
|
|
262 if (next_cmd_pos == cmd_pos)
|
|
263 break;
|
|
264 cmd_pos = next_cmd_pos;
|
|
265 }
|
2797
|
266 if (sub_header->num_rects > 0)
|
2756
|
267 return 0;
|
|
268 fail:
|
|
269 return -1;
|
|
270 }
|
|
271
|
|
272 static int is_transp(const uint8_t *buf, int pitch, int n,
|
|
273 const uint8_t *transp_color)
|
|
274 {
|
|
275 int i;
|
|
276 for(i = 0; i < n; i++) {
|
|
277 if (!transp_color[*buf])
|
|
278 return 0;
|
|
279 buf += pitch;
|
|
280 }
|
|
281 return 1;
|
|
282 }
|
|
283
|
|
284 /* return 0 if empty rectangle, 1 if non empty */
|
|
285 static int find_smallest_bouding_rectangle(AVSubtitle *s)
|
|
286 {
|
|
287 uint8_t transp_color[256];
|
|
288 int y1, y2, x1, x2, y, w, h, i;
|
|
289 uint8_t *bitmap;
|
|
290
|
2797
|
291 if (s->num_rects == 0 || s->rects == NULL || s->rects[0].w <= 0 || s->rects[0].h <= 0)
|
2756
|
292 return 0;
|
|
293
|
|
294 memset(transp_color, 0, 256);
|
2797
|
295 for(i = 0; i < s->rects[0].nb_colors; i++) {
|
|
296 if ((s->rects[0].rgba_palette[i] >> 24) == 0)
|
2756
|
297 transp_color[i] = 1;
|
|
298 }
|
|
299 y1 = 0;
|
2797
|
300 while (y1 < s->rects[0].h && is_transp(s->rects[0].bitmap + y1 * s->rects[0].linesize,
|
|
301 1, s->rects[0].w, transp_color))
|
2756
|
302 y1++;
|
2797
|
303 if (y1 == s->rects[0].h) {
|
|
304 av_freep(&s->rects[0].bitmap);
|
|
305 s->rects[0].w = s->rects[0].h = 0;
|
2756
|
306 return 0;
|
|
307 }
|
|
308
|
2797
|
309 y2 = s->rects[0].h - 1;
|
|
310 while (y2 > 0 && is_transp(s->rects[0].bitmap + y2 * s->rects[0].linesize, 1,
|
|
311 s->rects[0].w, transp_color))
|
2756
|
312 y2--;
|
|
313 x1 = 0;
|
2797
|
314 while (x1 < (s->rects[0].w - 1) && is_transp(s->rects[0].bitmap + x1, s->rects[0].linesize,
|
|
315 s->rects[0].h, transp_color))
|
2756
|
316 x1++;
|
2797
|
317 x2 = s->rects[0].w - 1;
|
|
318 while (x2 > 0 && is_transp(s->rects[0].bitmap + x2, s->rects[0].linesize, s->rects[0].h,
|
2756
|
319 transp_color))
|
|
320 x2--;
|
|
321 w = x2 - x1 + 1;
|
|
322 h = y2 - y1 + 1;
|
|
323 bitmap = av_malloc(w * h);
|
|
324 if (!bitmap)
|
|
325 return 1;
|
|
326 for(y = 0; y < h; y++) {
|
2797
|
327 memcpy(bitmap + w * y, s->rects[0].bitmap + x1 + (y1 + y) * s->rects[0].linesize, w);
|
2756
|
328 }
|
2797
|
329 av_freep(&s->rects[0].bitmap);
|
|
330 s->rects[0].bitmap = bitmap;
|
|
331 s->rects[0].linesize = w;
|
|
332 s->rects[0].w = w;
|
|
333 s->rects[0].h = h;
|
|
334 s->rects[0].x += x1;
|
|
335 s->rects[0].y += y1;
|
2756
|
336 return 1;
|
|
337 }
|
|
338
|
|
339 static int dvdsub_close_decoder(AVCodecContext *avctx)
|
|
340 {
|
|
341 return 0;
|
|
342 }
|
|
343
|
|
344 #ifdef DEBUG
|
|
345 #undef fprintf
|
|
346 static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
|
|
347 uint32_t *rgba_palette)
|
|
348 {
|
|
349 int x, y, v;
|
|
350 FILE *f;
|
|
351
|
|
352 f = fopen(filename, "w");
|
|
353 if (!f) {
|
|
354 perror(filename);
|
|
355 exit(1);
|
|
356 }
|
|
357 fprintf(f, "P6\n"
|
|
358 "%d %d\n"
|
|
359 "%d\n",
|
|
360 w, h, 255);
|
|
361 for(y = 0; y < h; y++) {
|
|
362 for(x = 0; x < w; x++) {
|
|
363 v = rgba_palette[bitmap[y * w + x]];
|
|
364 putc((v >> 16) & 0xff, f);
|
|
365 putc((v >> 8) & 0xff, f);
|
|
366 putc((v >> 0) & 0xff, f);
|
|
367 }
|
|
368 }
|
|
369 fclose(f);
|
|
370 }
|
|
371 #endif
|
|
372
|
|
373 static int dvdsub_decode(AVCodecContext *avctx,
|
|
374 void *data, int *data_size,
|
|
375 uint8_t *buf, int buf_size)
|
|
376 {
|
|
377 AVSubtitle *sub = (void *)data;
|
|
378
|
|
379 if (decode_dvd_subtitles(sub, buf, buf_size) < 0) {
|
|
380 no_subtitle:
|
|
381 *data_size = 0;
|
|
382
|
|
383 return buf_size;
|
|
384 }
|
|
385 if (find_smallest_bouding_rectangle(sub) == 0)
|
|
386 goto no_subtitle;
|
|
387
|
|
388 #if defined(DEBUG)
|
|
389 av_log(NULL, AV_LOG_INFO, "start=%d ms end =%d ms\n",
|
|
390 sub->start_display_time,
|
|
391 sub->end_display_time);
|
2797
|
392 ppm_save("/tmp/a.ppm", sub->rects[0].bitmap,
|
|
393 sub->rects[0].w, sub->rects[0].h, sub->rects[0].rgba_palette);
|
2756
|
394 #endif
|
|
395
|
|
396 *data_size = 1;
|
|
397 return buf_size;
|
|
398 }
|
|
399
|
|
400 AVCodec dvdsub_decoder = {
|
|
401 "dvdsub",
|
|
402 CODEC_TYPE_SUBTITLE,
|
|
403 CODEC_ID_DVD_SUBTITLE,
|
|
404 sizeof(DVDSubContext),
|
|
405 dvdsub_init_decoder,
|
|
406 NULL,
|
|
407 dvdsub_close_decoder,
|
|
408 dvdsub_decode,
|
|
409 };
|
|
410
|
|
411 /* parser definition */
|
|
412 typedef struct DVDSubParseContext {
|
|
413 uint8_t *packet;
|
|
414 int packet_len;
|
|
415 int packet_index;
|
|
416 } DVDSubParseContext;
|
|
417
|
|
418 static int dvdsub_parse_init(AVCodecParserContext *s)
|
|
419 {
|
|
420 return 0;
|
|
421 }
|
|
422
|
|
423 static int dvdsub_parse(AVCodecParserContext *s,
|
|
424 AVCodecContext *avctx,
|
|
425 uint8_t **poutbuf, int *poutbuf_size,
|
|
426 const uint8_t *buf, int buf_size)
|
|
427 {
|
|
428 DVDSubParseContext *pc = s->priv_data;
|
|
429
|
|
430 if (pc->packet_index == 0) {
|
|
431 if (buf_size < 2)
|
|
432 return 0;
|
|
433 pc->packet_len = (buf[0] << 8) | buf[1];
|
|
434 av_freep(&pc->packet);
|
|
435 pc->packet = av_malloc(pc->packet_len);
|
|
436 }
|
|
437 if (pc->packet) {
|
|
438 if (pc->packet_index + buf_size <= pc->packet_len) {
|
|
439 memcpy(pc->packet + pc->packet_index, buf, buf_size);
|
|
440 pc->packet_index += buf_size;
|
|
441 if (pc->packet_index >= pc->packet_len) {
|
|
442 *poutbuf = pc->packet;
|
|
443 *poutbuf_size = pc->packet_len;
|
|
444 pc->packet_index = 0;
|
|
445 return buf_size;
|
|
446 }
|
|
447 } else {
|
|
448 /* erroneous size */
|
|
449 pc->packet_index = 0;
|
|
450 }
|
|
451 }
|
|
452 *poutbuf = NULL;
|
|
453 *poutbuf_size = 0;
|
|
454 return buf_size;
|
|
455 }
|
|
456
|
|
457 static void dvdsub_parse_close(AVCodecParserContext *s)
|
|
458 {
|
|
459 DVDSubParseContext *pc = s->priv_data;
|
|
460 av_freep(&pc->packet);
|
|
461 }
|
|
462
|
|
463 AVCodecParser dvdsub_parser = {
|
|
464 { CODEC_ID_DVD_SUBTITLE },
|
|
465 sizeof(DVDSubParseContext),
|
|
466 dvdsub_parse_init,
|
|
467 dvdsub_parse,
|
|
468 dvdsub_parse_close,
|
|
469 };
|