1 /*
2 * DVD subtitle decoding
3 * Copyright (c) 2005 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
25
32
34 {
45 #ifdef DEBUG
46 int sub_id;
47 #endif
49
51 {
55 int r_add, g_add, b_add;
56
57 for (i = num_values; i > 0; i--) {
58 y = *ycbcr++;
59 cr = *ycbcr++;
60 cb = *ycbcr++;
63 *rgba++ = (*alpha++ << 24) | (r << 16) | (g << 8) | b;
64 }
65 }
66
68 {
69 unsigned int v, t;
70
71 v = 0;
72 for (t = 1; v < t && t <= 0x40; t <<= 2)
74 *color = v & 3;
75 if (v < 4) { /* Code for fill rest of line */
76 return INT_MAX;
77 }
78 return v >> 2;
79 }
80
82 {
87 else
89 if (has_run) {
92 if (len == 0)
93 len = INT_MAX;
94 else
95 len += 9;
96 } else
98 } else
99 len = 1;
101 }
102
105 {
107 int bit_len;
110
111 if (start >= buf_size)
112 return -1;
113
114 if (w <= 0 || h <= 0)
115 return -1;
116
117 bit_len = (buf_size -
start) * 8;
119
120 x = 0;
121 y = 0;
122 d = bitmap;
123 for(;;) {
125 return -1;
126 if (is_8bit)
128 else
130 len =
FFMIN(len, w - x);
131 memset(d + x, color, len);
133 if (x >= w) {
134 y++;
135 if (y >= h)
136 break;
137 d += linesize;
138 x = 0;
139 /* byte align */
141 }
142 }
143 return 0;
144 }
145
147 uint32_t *rgba_palette,
148 uint32_t subtitle_color)
149 {
150 static const uint8_t level_map[4][4] = {
151 // this configuration (full range, lowest to highest) in tests
152 // seemed most common, so assume this
153 {0xff},
154 {0x00, 0xff},
155 {0x00, 0x80, 0xff},
156 {0x00, 0x55, 0xaa, 0xff},
157 };
158 uint8_t color_used[16] = { 0 };
159 int nb_opaque_colors, i,
level, j,
r,
g,
b;
161
163 for(i = 0; i < 4; i++)
164 rgba_palette[i] = (ctx->
palette[colormap[i]] & 0x00ffffff)
165 | ((
alpha[i] * 17
U) << 24);
166 return;
167 }
168
169 for(i = 0; i < 4; i++)
170 rgba_palette[i] = 0;
171
172 nb_opaque_colors = 0;
173 for(i = 0; i < 4; i++) {
174 if (
alpha[i] != 0 && !color_used[colormap[i]]) {
175 color_used[colormap[i]] = 1;
176 nb_opaque_colors++;
177 }
178 }
179
180 if (nb_opaque_colors == 0)
181 return;
182
183 j = 0;
184 memset(color_used, 0, 16);
185 for(i = 0; i < 4; i++) {
187 if (!color_used[colormap[i]]) {
188 level = level_map[nb_opaque_colors][j];
189 r = (((subtitle_color >> 16) & 0xff) *
level) >> 8;
190 g = (((subtitle_color >> 8) & 0xff) *
level) >> 8;
191 b = (((subtitle_color >> 0) & 0xff) *
level) >> 8;
192 rgba_palette[i] = b | (g << 8) | (r << 16) | ((
alpha[i] * 17) << 24);
193 color_used[colormap[i]] = (i + 1);
194 j++;
195 } else {
196 rgba_palette[i] = (rgba_palette[color_used[colormap[i]] - 1] & 0x00ffffff) |
197 ((
alpha[i] * 17) << 24);
198 }
199 }
200 }
201 }
202
204 {
205 int i;
206
207 if (sub_header->
rects) {
208 for (i = 0; i < sub_header->
num_rects; i++) {
212 }
215 }
216 }
217
218 #define READ_OFFSET(a) (big_offsets ? AV_RB32(a) : AV_RB16(a))
219
222 {
223 int cmd_pos, pos, cmd, x1, y1, x2, y2, next_cmd_pos;
224 int big_offsets, offset_size, is_8bit = 0;
227 int date;
228 int i;
229 int is_menu = 0;
231 int64_t offset1, offset2;
232
233 if (buf_size < 10)
234 return -1;
235
236 if (
AV_RB16(buf) == 0) {
/* HD subpicture with 4-byte offsets */
237 big_offsets = 1;
238 offset_size = 4;
239 cmd_pos = 6;
240 } else {
241 big_offsets = 0;
242 offset_size = 2;
243 cmd_pos = 2;
244 }
245
248
249 if (cmd_pos < 0 || cmd_pos > buf_size - 2 - offset_size) {
250 if (cmd_pos > size) {
252 return 0;
253 }
255 }
256
257 while (cmd_pos > 0 && cmd_pos < buf_size - 2 - offset_size) {
260 ff_dlog(
NULL,
"cmd_pos=0x%04x next=0x%04x date=%d\n",
261 cmd_pos, next_cmd_pos, date);
262 pos = cmd_pos + 2 + offset_size;
263 offset1 = -1;
264 offset2 = -1;
265 x1 = y1 = x2 = y2 = 0;
266 while (pos < buf_size) {
267 cmd = buf[pos++];
269 switch(cmd) {
270 case 0x00:
271 /* menu subpicture */
272 is_menu = 1;
273 break;
274 case 0x01:
275 /* set start date */
277 break;
278 case 0x02:
279 /* set end date */
281 break;
282 case 0x03:
283 /* set colormap */
284 if ((buf_size - pos) < 2)
286 colormap[3] = buf[pos] >> 4;
287 colormap[2] = buf[pos] & 0x0f;
288 colormap[1] = buf[pos + 1] >> 4;
289 colormap[0] = buf[pos + 1] & 0x0f;
290 pos += 2;
291 break;
292 case 0x04:
293 /* set alpha */
294 if ((buf_size - pos) < 2)
296 alpha[3] = buf[pos] >> 4;
297 alpha[2] = buf[pos] & 0x0f;
298 alpha[1] = buf[pos + 1] >> 4;
299 alpha[0] = buf[pos + 1] & 0x0f;
300 pos += 2;
302 break;
303 case 0x05:
304 case 0x85:
305 if ((buf_size - pos) < 6)
307 x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
308 x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
309 y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
310 y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
311 if (cmd & 0x80)
312 is_8bit = 1;
313 ff_dlog(
NULL,
"x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2);
314 pos += 6;
315 break;
316 case 0x06:
317 if ((buf_size - pos) < 4)
320 offset2 =
AV_RB16(buf + pos + 2);
321 ff_dlog(
NULL,
"offset1=0x%04"PRIx64
" offset2=0x%04"PRIx64
"\n", offset1, offset2);
322 pos += 4;
323 break;
324 case 0x86:
325 if ((buf_size - pos) < 8)
328 offset2 =
AV_RB32(buf + pos + 4);
329 ff_dlog(
NULL,
"offset1=0x%04"PRIx64
" offset2=0x%04"PRIx64
"\n", offset1, offset2);
330 pos += 8;
331 break;
332
333 case 0x83:
334 /* HD set palette */
335 if ((buf_size - pos) < 768)
337 yuv_palette = buf + pos;
338 pos += 768;
339 break;
340 case 0x84:
341 /* HD set contrast (alpha) */
342 if ((buf_size - pos) < 256)
344 for (i = 0; i < 256; i++)
345 alpha[i] = 0xFF - buf[pos+i];
346 pos += 256;
347 break;
348
349 case 0xff:
350 goto the_end;
351 default:
352 ff_dlog(
NULL,
"unrecognised subpicture command 0x%x\n", cmd);
353 goto the_end;
354 }
355 }
356 the_end:
357 if (offset1 >= buf_size || offset2 >= buf_size)
359
360 if (offset1 >= 0 && offset2 >= 0) {
363
364 /* decode the bitmap */
365 w = x2 - x1 + 1;
366 if (w < 0)
367 w = 0;
368 h = y2 - y1 + 1;
369 if (h < 0)
370 h = 0;
371 if (w > 0 && h > 0) {
373
375 if (!sub_header->
rects)
378 if (!sub_header->
rects[0])
382 if (!bitmap)
385 buf, offset1, buf_size, is_8bit) < 0)
388 buf, offset2, buf_size, is_8bit) < 0)
393 if (is_8bit) {
394 if (!yuv_palette)
398 (uint32_t *)sub_header->
rects[0]->
data[1],
399 256);
400 } else {
403 0xffff00);
404 }
405 sub_header->
rects[0]->
x = x1;
406 sub_header->
rects[0]->
y = y1;
407 sub_header->
rects[0]->
w = w;
412
413 #if FF_API_AVPICTURE
415 for (i = 0; i < 4; i++) {
418 }
420 #endif
421 }
422 }
423 if (next_cmd_pos < cmd_pos) {
425 break;
426 }
427 if (next_cmd_pos == cmd_pos)
428 break;
429 cmd_pos = next_cmd_pos;
430 }
432 return is_menu;
435 return -1;
436 }
437
440 {
441 int i;
442 for(i = 0; i <
n; i++) {
443 if (!transp_color[*buf])
444 return 0;
445 buf += pitch;
446 }
447 return 1;
448 }
449
450 /* return 0 if empty rectangle, 1 if non empty */
452 {
453 uint8_t transp_color[256] = { 0 };
454 int y1, y2, x1, x2, y, w,
h, i;
456
458 return 0;
459
461 if ((((uint32_t *)s->
rects[0]->
data[1])[i] >> 24) == 0)
462 transp_color[i] = 1;
463 }
464 y1 = 0;
466 1, s->
rects[0]->
w, transp_color))
467 y1++;
468 if (y1 == s->
rects[0]->
h) {
471 return 0;
472 }
473
476 s->
rects[0]->
w, transp_color))
477 y2--;
478 x1 = 0;
480 s->
rects[0]->
h, transp_color))
481 x1++;
484 transp_color))
485 x2--;
486 w = x2 - x1 + 1;
487 h = y2 - y1 + 1;
489 if (!bitmap)
490 return 1;
491 for(y = 0; y <
h; y++) {
493 }
501
502 #if FF_API_AVPICTURE
504 for (i = 0; i < 4; i++) {
507 }
509 #endif
510
511 return 1;
512 }
513
514 #ifdef DEBUG
515 #define ALPHA_MIX(A,BACK,FORE) (((255-(A)) * (BACK) + (A) * (FORE)) / 255)
516 static void ppm_save(
const char *filename,
uint8_t *bitmap,
int w,
int h,
517 uint32_t *rgba_palette)
518 {
520 uint32_t v;
521 int back[3] = {0, 255, 0}; /* green background */
522 FILE *f;
523
524 f = fopen(filename, "w");
525 if (!f) {
526 perror(filename);
527 return;
528 }
529 fprintf(f, "P6\n"
530 "%d %d\n"
531 "%d\n",
532 w, h, 255);
533 for(y = 0; y <
h; y++) {
534 for(x = 0; x < w; x++) {
535 v = rgba_palette[bitmap[y * w + x]];
536 alpha = v >> 24;
537 putc(ALPHA_MIX(alpha, back[0], (v >> 16) & 0xff), f);
538 putc(ALPHA_MIX(alpha, back[1], (v >> 8) & 0xff), f);
539 putc(ALPHA_MIX(alpha, back[2], (v >> 0) & 0xff), f);
540 }
541 }
542 fclose(f);
543 }
544 #endif
545
548 {
550
551 if (ctx->
buf_size >=
sizeof(ctx->
buf) - buf_size) {
553 "too large SPU packets aborted.\n");
556 }
559 return 0;
560 }
561
563 void *
data,
int *data_size,
565 {
568 int buf_size = avpkt->
size;
570 int appended = 0;
571 int is_menu;
572
575 if (ret < 0) {
576 *data_size = 0;
577 return ret;
578 }
581 appended = 1;
582 }
583
585 if (is_menu ==
AVERROR(EAGAIN)) {
586 *data_size = 0;
588 }
589
590 if (is_menu < 0) {
591 no_subtitle:
593 *data_size = 0;
594
595 return buf_size;
596 }
598 goto no_subtitle;
599
601 goto no_subtitle;
602
603 #if defined(DEBUG)
604 {
605 char ppm_name[32];
606
607 snprintf(ppm_name,
sizeof(ppm_name),
"/tmp/%05d.ppm", ctx->sub_id++);
611 ppm_save(ppm_name, sub->
rects[0]->
data[0],
613 }
614 #endif
615
617 *data_size = 1;
618 return buf_size;
619 }
620
622 {
623 int i;
624
626 for(i=0;i<16;i++) {
627 ctx->
palette[i] = strtoul(p, &p, 16);
629 p++;
630 }
631 }
632
634 {
635 FILE *ifo;
636 char ifostr[12];
637 uint32_t sp_pgci, pgci, off_pgc, pgc;
639 int i, y,
cb,
cr, r_add, g_add, b_add;
640 int ret = 0;
642
644 if ((ifo = fopen(p,
"r")) ==
NULL) {
647 }
648 if (fread(ifostr, 12, 1, ifo) != 1 || memcmp(ifostr, "DVDVIDEO-VTS", 12)) {
652 }
653 if (fseek(ifo, 0xCC, SEEK_SET) == -1) {
656 }
657 if (fread(&sp_pgci, 4, 1, ifo) == 1) {
659 if (fseek(ifo, pgci + 0x0C, SEEK_SET) == -1) {
662 }
663 if (fread(&off_pgc, 4, 1, ifo) == 1) {
665 if (fseek(ifo, pgc + 0xA4, SEEK_SET) == -1) {
668 }
669 if (fread(yuv, 64, 1, ifo) == 1) {
670 buf = yuv;
671 for(i=0; i<16; i++) {
677 ctx->
palette[i] = (r << 16) + (g << 8) +
b;
678 buf++;
679 }
681 }
682 }
683 }
687 }
689 fclose(ifo);
690 return ret;
691 }
692
694 {
696 char *dataorig, *
data;
697 int ret = 1;
698
700 return 1;
701
707
708 for(;;) {
709 int pos = strcspn(
data,
"\n\r");
710 if (pos==0 && *
data==0)
711 break;
712
713 if (strncmp(
"palette:",
data, 8) == 0) {
715 }
else if (strncmp(
"size:",
data, 5) == 0) {
717 if (sscanf(
data + 5,
"%dx%d", &w, &h) == 2) {
719 if (ret < 0)
721 }
722 }
723
726 }
727
730 return ret;
731 }
732
734 {
736 int ret;
737
739 return ret;
740
746 int i;
748 for(i=0;i<16;i++)
751 }
752
753 return 1;
754 }
755
757 {
760 }
761
763 {
765 return 0;
766 }
767
768 #define OFFSET(field) offsetof(DVDSubContext, field)
769 #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
773 {
"forced_subs_only",
"Only show forced subtitles",
OFFSET(forced_subs_only),
AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1,
SD},
775 };
781 };
782
793 .priv_class = &dvdsub_class,
794 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
static void dvdsub_flush(AVCodecContext *avctx)
int x
top left corner of pict, undefined when pict is not set
ptrdiff_t const GLvoid * data
static void flush(AVCodecContext *avctx)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
#define YUV_TO_RGB1_CCIR(cb1, cr1)
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
static av_cold int init(AVCodecContext *avctx)
int nb_colors
number of colors in pict, undefined when pict is not set
static av_const int av_isspace(int c)
Locale-independent conversion of ASCII isspace.
Various defines for YUV<->RGB conversion.
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
static av_cold int dvdsub_close(AVCodecContext *avctx)
attribute_deprecated AVPicture pict
Macro definitions for various function/variable attributes.
int w
width of pict, undefined when pict is not set
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
static double cb(void *priv, double x, double y)
static const uint32_t color[16+AV_CLASS_CATEGORY_NB]
static av_cold int end(AVCodecContext *avctx)
attribute_deprecated int linesize[AV_NUM_DATA_POINTERS]
number of bytes per line
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
static void reset_rects(AVSubtitle *sub_header)
static int get_bits_count(const GetBitContext *s)
#define AVERROR_EOF
End of file.
bitstream reader API header.
int h
height of pict, undefined when pict is not set
static av_cold int dvdsub_init(AVCodecContext *avctx)
static double alpha(void *priv, double x, double y)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int y
top left corner of pict, undefined when pict is not set
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
const char * name
Name of the codec implementation.
static int append_to_cached_buf(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
uint32_t end_display_time
static int decode_run_8bit(GetBitContext *gb, int *color)
A bitmap, pict will be set.
#define AV_SUBTITLE_FLAG_FORCED
static const AVClass dvdsub_class
static void yuv_a_to_rgba(const uint8_t *ycbcr, const uint8_t *alpha, uint32_t *rgba, int num_values)
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
static int parse_ifo_palette(DVDSubContext *ctx, char *p)
static int is_transp(const uint8_t *buf, int pitch, int n, const uint8_t *transp_color)
attribute_deprecated uint8_t * data[AV_NUM_DATA_POINTERS]
pointers to the image data planes
uint8_t * data[4]
data+linesize for the bitmap of this subtitle.
AVCodec ff_dvdsub_decoder
static void parse_palette(DVDSubContext *ctx, char *p)
Libavcodec external API header.
main external API structure.
static const AVOption options[]
static int decode_dvd_subtitles(DVDSubContext *ctx, AVSubtitle *sub_header, const uint8_t *buf, int buf_size)
static unsigned int get_bits1(GetBitContext *s)
Describe the class of an AVClass context structure.
#define YUV_TO_RGB2_CCIR(r, g, b, y1)
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
static int decode_run_2bit(GetBitContext *gb, int *color)
static int find_smallest_bounding_rectangle(AVSubtitle *s)
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
#define FF_DISABLE_DEPRECATION_WARNINGS
common internal api header.
static int dvdsub_parse_extradata(AVCodecContext *avctx)
uint32_t start_display_time
static void guess_palette(DVDSubContext *ctx, uint32_t *rgba_palette, uint32_t subtitle_color)
#define FF_ENABLE_DEPRECATION_WARNINGS
static int dvdsub_decode(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
static const uint8_t * align_get_bits(GetBitContext *s)
static double cr(void *priv, double x, double y)
This structure stores compressed data.
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
static int decode_rle(uint8_t *bitmap, int linesize, int w, int h, const uint8_t *buf, int start, int buf_size, int is_8bit)