Replace 5 with AOT_SBR when referring to the MPEG-4 audio object type.
[FFMpeg-mirror/lagarith.git] / libavcodec / dvdsubdec.c
bloba99b7af85864a8015eca6629fe5681a98ccec6b3
1 /*
2 * DVD subtitle decoding for ffmpeg
3 * Copyright (c) 2005 Fabrice Bellard
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
21 #include "avcodec.h"
22 #include "get_bits.h"
23 #include "colorspace.h"
24 #include "dsputil.h"
26 //#define DEBUG
28 static void yuv_a_to_rgba(const uint8_t *ycbcr, const uint8_t *alpha, uint32_t *rgba, int num_values)
30 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
31 uint8_t r, g, b;
32 int i, y, cb, cr;
33 int r_add, g_add, b_add;
35 for (i = num_values; i > 0; i--) {
36 y = *ycbcr++;
37 cb = *ycbcr++;
38 cr = *ycbcr++;
39 YUV_TO_RGB1_CCIR(cb, cr);
40 YUV_TO_RGB2_CCIR(r, g, b, y);
41 *rgba++ = (*alpha++ << 24) | (r << 16) | (g << 8) | b;
45 static int decode_run_2bit(GetBitContext *gb, int *color)
47 unsigned int v, t;
49 v = 0;
50 for (t = 1; v < t && t <= 0x40; t <<= 2)
51 v = (v << 4) | get_bits(gb, 4);
52 *color = v & 3;
53 if (v < 4) { /* Code for fill rest of line */
54 return INT_MAX;
56 return v >> 2;
59 static int decode_run_8bit(GetBitContext *gb, int *color)
61 int len;
62 int has_run = get_bits1(gb);
63 if (get_bits1(gb))
64 *color = get_bits(gb, 8);
65 else
66 *color = get_bits(gb, 2);
67 if (has_run) {
68 if (get_bits1(gb)) {
69 len = get_bits(gb, 7);
70 if (len == 0)
71 len = INT_MAX;
72 else
73 len += 9;
74 } else
75 len = get_bits(gb, 3) + 2;
76 } else
77 len = 1;
78 return len;
81 static int decode_rle(uint8_t *bitmap, int linesize, int w, int h,
82 const uint8_t *buf, int start, int buf_size, int is_8bit)
84 GetBitContext gb;
85 int bit_len;
86 int x, y, len, color;
87 uint8_t *d;
89 bit_len = (buf_size - start) * 8;
90 init_get_bits(&gb, buf + start, bit_len);
92 x = 0;
93 y = 0;
94 d = bitmap;
95 for(;;) {
96 if (get_bits_count(&gb) > bit_len)
97 return -1;
98 if (is_8bit)
99 len = decode_run_8bit(&gb, &color);
100 else
101 len = decode_run_2bit(&gb, &color);
102 len = FFMIN(len, w - x);
103 memset(d + x, color, len);
104 x += len;
105 if (x >= w) {
106 y++;
107 if (y >= h)
108 break;
109 d += linesize;
110 x = 0;
111 /* byte align */
112 align_get_bits(&gb);
115 return 0;
118 static void guess_palette(uint32_t *rgba_palette,
119 uint8_t *colormap,
120 uint8_t *alpha,
121 uint32_t subtitle_color)
123 uint8_t color_used[16];
124 int nb_opaque_colors, i, level, j, r, g, b;
126 for(i = 0; i < 4; i++)
127 rgba_palette[i] = 0;
129 memset(color_used, 0, 16);
130 nb_opaque_colors = 0;
131 for(i = 0; i < 4; i++) {
132 if (alpha[i] != 0 && !color_used[colormap[i]]) {
133 color_used[colormap[i]] = 1;
134 nb_opaque_colors++;
138 if (nb_opaque_colors == 0)
139 return;
141 j = nb_opaque_colors;
142 memset(color_used, 0, 16);
143 for(i = 0; i < 4; i++) {
144 if (alpha[i] != 0) {
145 if (!color_used[colormap[i]]) {
146 level = (0xff * j) / nb_opaque_colors;
147 r = (((subtitle_color >> 16) & 0xff) * level) >> 8;
148 g = (((subtitle_color >> 8) & 0xff) * level) >> 8;
149 b = (((subtitle_color >> 0) & 0xff) * level) >> 8;
150 rgba_palette[i] = b | (g << 8) | (r << 16) | ((alpha[i] * 17) << 24);
151 color_used[colormap[i]] = (i + 1);
152 j--;
153 } else {
154 rgba_palette[i] = (rgba_palette[color_used[colormap[i]] - 1] & 0x00ffffff) |
155 ((alpha[i] * 17) << 24);
161 #define READ_OFFSET(a) (big_offsets ? AV_RB32(a) : AV_RB16(a))
163 static int decode_dvd_subtitles(AVSubtitle *sub_header,
164 const uint8_t *buf, int buf_size)
166 int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos;
167 int big_offsets, offset_size, is_8bit = 0;
168 const uint8_t *yuv_palette = 0;
169 uint8_t colormap[4], alpha[256];
170 int date;
171 int i;
172 int is_menu = 0;
174 if (buf_size < 10)
175 return -1;
176 sub_header->rects = NULL;
177 sub_header->num_rects = 0;
178 sub_header->format = 0;
179 sub_header->start_display_time = 0;
180 sub_header->end_display_time = 0;
182 if (AV_RB16(buf) == 0) { /* HD subpicture with 4-byte offsets */
183 big_offsets = 1;
184 offset_size = 4;
185 cmd_pos = 6;
186 } else {
187 big_offsets = 0;
188 offset_size = 2;
189 cmd_pos = 2;
192 cmd_pos = READ_OFFSET(buf + cmd_pos);
194 while ((cmd_pos + 2 + offset_size) < buf_size) {
195 date = AV_RB16(buf + cmd_pos);
196 next_cmd_pos = READ_OFFSET(buf + cmd_pos + 2);
197 dprintf(NULL, "cmd_pos=0x%04x next=0x%04x date=%d\n",
198 cmd_pos, next_cmd_pos, date);
199 pos = cmd_pos + 2 + offset_size;
200 offset1 = -1;
201 offset2 = -1;
202 x1 = y1 = x2 = y2 = 0;
203 while (pos < buf_size) {
204 cmd = buf[pos++];
205 dprintf(NULL, "cmd=%02x\n", cmd);
206 switch(cmd) {
207 case 0x00:
208 /* menu subpicture */
209 is_menu = 1;
210 break;
211 case 0x01:
212 /* set start date */
213 sub_header->start_display_time = (date << 10) / 90;
214 break;
215 case 0x02:
216 /* set end date */
217 sub_header->end_display_time = (date << 10) / 90;
218 break;
219 case 0x03:
220 /* set colormap */
221 if ((buf_size - pos) < 2)
222 goto fail;
223 colormap[3] = buf[pos] >> 4;
224 colormap[2] = buf[pos] & 0x0f;
225 colormap[1] = buf[pos + 1] >> 4;
226 colormap[0] = buf[pos + 1] & 0x0f;
227 pos += 2;
228 break;
229 case 0x04:
230 /* set alpha */
231 if ((buf_size - pos) < 2)
232 goto fail;
233 alpha[3] = buf[pos] >> 4;
234 alpha[2] = buf[pos] & 0x0f;
235 alpha[1] = buf[pos + 1] >> 4;
236 alpha[0] = buf[pos + 1] & 0x0f;
237 pos += 2;
238 dprintf(NULL, "alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]);
239 break;
240 case 0x05:
241 case 0x85:
242 if ((buf_size - pos) < 6)
243 goto fail;
244 x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
245 x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
246 y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
247 y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
248 if (cmd & 0x80)
249 is_8bit = 1;
250 dprintf(NULL, "x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2);
251 pos += 6;
252 break;
253 case 0x06:
254 if ((buf_size - pos) < 4)
255 goto fail;
256 offset1 = AV_RB16(buf + pos);
257 offset2 = AV_RB16(buf + pos + 2);
258 dprintf(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
259 pos += 4;
260 break;
261 case 0x86:
262 if ((buf_size - pos) < 8)
263 goto fail;
264 offset1 = AV_RB32(buf + pos);
265 offset2 = AV_RB32(buf + pos + 4);
266 dprintf(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
267 pos += 8;
268 break;
270 case 0x83:
271 /* HD set palette */
272 if ((buf_size - pos) < 768)
273 goto fail;
274 yuv_palette = buf + pos;
275 pos += 768;
276 break;
277 case 0x84:
278 /* HD set contrast (alpha) */
279 if ((buf_size - pos) < 256)
280 goto fail;
281 for (i = 0; i < 256; i++)
282 alpha[i] = 0xFF - buf[pos+i];
283 pos += 256;
284 break;
286 case 0xff:
287 goto the_end;
288 default:
289 dprintf(NULL, "unrecognised subpicture command 0x%x\n", cmd);
290 goto the_end;
293 the_end:
294 if (offset1 >= 0) {
295 int w, h;
296 uint8_t *bitmap;
298 /* decode the bitmap */
299 w = x2 - x1 + 1;
300 if (w < 0)
301 w = 0;
302 h = y2 - y1;
303 if (h < 0)
304 h = 0;
305 if (w > 0 && h > 0) {
306 if (sub_header->rects != NULL) {
307 for (i = 0; i < sub_header->num_rects; i++) {
308 av_freep(&sub_header->rects[i]->pict.data[0]);
309 av_freep(&sub_header->rects[i]->pict.data[1]);
310 av_freep(&sub_header->rects[i]);
312 av_freep(&sub_header->rects);
313 sub_header->num_rects = 0;
316 bitmap = av_malloc(w * h);
317 sub_header->rects = av_mallocz(sizeof(*sub_header->rects));
318 sub_header->rects[0] = av_mallocz(sizeof(AVSubtitleRect));
319 sub_header->num_rects = 1;
320 sub_header->rects[0]->pict.data[0] = bitmap;
321 decode_rle(bitmap, w * 2, w, (h + 1) / 2,
322 buf, offset1, buf_size, is_8bit);
323 decode_rle(bitmap + w, w * 2, w, h / 2,
324 buf, offset2, buf_size, is_8bit);
325 if (is_8bit) {
326 if (yuv_palette == 0)
327 goto fail;
328 sub_header->rects[0]->pict.data[1] = av_malloc(256 * 4);
329 sub_header->rects[0]->nb_colors = 256;
330 yuv_a_to_rgba(yuv_palette, alpha, (uint32_t*)sub_header->rects[0]->pict.data[1], 256);
331 } else {
332 sub_header->rects[0]->pict.data[1] = av_malloc(4 * 4);
333 sub_header->rects[0]->nb_colors = 4;
334 guess_palette((uint32_t*)sub_header->rects[0]->pict.data[1],
335 colormap, alpha, 0xffff00);
337 sub_header->rects[0]->x = x1;
338 sub_header->rects[0]->y = y1;
339 sub_header->rects[0]->w = w;
340 sub_header->rects[0]->h = h;
341 sub_header->rects[0]->type = SUBTITLE_BITMAP;
342 sub_header->rects[0]->pict.linesize[0] = w;
345 if (next_cmd_pos == cmd_pos)
346 break;
347 cmd_pos = next_cmd_pos;
349 if (sub_header->num_rects > 0)
350 return is_menu;
351 fail:
352 if (sub_header->rects != NULL) {
353 for (i = 0; i < sub_header->num_rects; i++) {
354 av_freep(&sub_header->rects[i]->pict.data[0]);
355 av_freep(&sub_header->rects[i]->pict.data[1]);
356 av_freep(&sub_header->rects[i]);
358 av_freep(&sub_header->rects);
359 sub_header->num_rects = 0;
361 return -1;
364 static int is_transp(const uint8_t *buf, int pitch, int n,
365 const uint8_t *transp_color)
367 int i;
368 for(i = 0; i < n; i++) {
369 if (!transp_color[*buf])
370 return 0;
371 buf += pitch;
373 return 1;
376 /* return 0 if empty rectangle, 1 if non empty */
377 static int find_smallest_bounding_rectangle(AVSubtitle *s)
379 uint8_t transp_color[256];
380 int y1, y2, x1, x2, y, w, h, i;
381 uint8_t *bitmap;
383 if (s->num_rects == 0 || s->rects == NULL || s->rects[0]->w <= 0 || s->rects[0]->h <= 0)
384 return 0;
386 memset(transp_color, 0, 256);
387 for(i = 0; i < s->rects[0]->nb_colors; i++) {
388 if ((((uint32_t*)s->rects[0]->pict.data[1])[i] >> 24) == 0)
389 transp_color[i] = 1;
391 y1 = 0;
392 while (y1 < s->rects[0]->h && is_transp(s->rects[0]->pict.data[0] + y1 * s->rects[0]->pict.linesize[0],
393 1, s->rects[0]->w, transp_color))
394 y1++;
395 if (y1 == s->rects[0]->h) {
396 av_freep(&s->rects[0]->pict.data[0]);
397 s->rects[0]->w = s->rects[0]->h = 0;
398 return 0;
401 y2 = s->rects[0]->h - 1;
402 while (y2 > 0 && is_transp(s->rects[0]->pict.data[0] + y2 * s->rects[0]->pict.linesize[0], 1,
403 s->rects[0]->w, transp_color))
404 y2--;
405 x1 = 0;
406 while (x1 < (s->rects[0]->w - 1) && is_transp(s->rects[0]->pict.data[0] + x1, s->rects[0]->pict.linesize[0],
407 s->rects[0]->h, transp_color))
408 x1++;
409 x2 = s->rects[0]->w - 1;
410 while (x2 > 0 && is_transp(s->rects[0]->pict.data[0] + x2, s->rects[0]->pict.linesize[0], s->rects[0]->h,
411 transp_color))
412 x2--;
413 w = x2 - x1 + 1;
414 h = y2 - y1 + 1;
415 bitmap = av_malloc(w * h);
416 if (!bitmap)
417 return 1;
418 for(y = 0; y < h; y++) {
419 memcpy(bitmap + w * y, s->rects[0]->pict.data[0] + x1 + (y1 + y) * s->rects[0]->pict.linesize[0], w);
421 av_freep(&s->rects[0]->pict.data[0]);
422 s->rects[0]->pict.data[0] = bitmap;
423 s->rects[0]->pict.linesize[0] = w;
424 s->rects[0]->w = w;
425 s->rects[0]->h = h;
426 s->rects[0]->x += x1;
427 s->rects[0]->y += y1;
428 return 1;
431 #ifdef DEBUG
432 #undef fprintf
433 #undef perror
434 #undef exit
435 static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
436 uint32_t *rgba_palette)
438 int x, y, v;
439 FILE *f;
441 f = fopen(filename, "w");
442 if (!f) {
443 perror(filename);
444 exit(1);
446 fprintf(f, "P6\n"
447 "%d %d\n"
448 "%d\n",
449 w, h, 255);
450 for(y = 0; y < h; y++) {
451 for(x = 0; x < w; x++) {
452 v = rgba_palette[bitmap[y * w + x]];
453 putc((v >> 16) & 0xff, f);
454 putc((v >> 8) & 0xff, f);
455 putc((v >> 0) & 0xff, f);
458 fclose(f);
460 #endif
462 static int dvdsub_decode(AVCodecContext *avctx,
463 void *data, int *data_size,
464 AVPacket *avpkt)
466 const uint8_t *buf = avpkt->data;
467 int buf_size = avpkt->size;
468 AVSubtitle *sub = (void *)data;
469 int is_menu;
471 is_menu = decode_dvd_subtitles(sub, buf, buf_size);
473 if (is_menu < 0) {
474 no_subtitle:
475 *data_size = 0;
477 return buf_size;
479 if (!is_menu && find_smallest_bounding_rectangle(sub) == 0)
480 goto no_subtitle;
482 #if defined(DEBUG)
483 dprintf(NULL, "start=%d ms end =%d ms\n",
484 sub->start_display_time,
485 sub->end_display_time);
486 ppm_save("/tmp/a.ppm", sub->rects[0]->pict.data[0],
487 sub->rects[0]->w, sub->rects[0]->h, sub->rects[0]->pict.data[1]);
488 #endif
490 *data_size = 1;
491 return buf_size;
494 AVCodec dvdsub_decoder = {
495 "dvdsub",
496 CODEC_TYPE_SUBTITLE,
497 CODEC_ID_DVD_SUBTITLE,
499 NULL,
500 NULL,
501 NULL,
502 dvdsub_decode,
503 .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),