]> git.sesse.net Git - ffmpeg/blob - libavcodec/dvdsubdec.c
Merge commit '64f7575fbd64e5b65d5c644347408588c776f1fe'
[ffmpeg] / libavcodec / dvdsubdec.c
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
22 #include "avcodec.h"
23 #include "get_bits.h"
24 #include "internal.h"
25
26 #include "libavutil/attributes.h"
27 #include "libavutil/colorspace.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/avstring.h"
31 #include "libavutil/bswap.h"
32
33 typedef struct DVDSubContext
34 {
35   AVClass *class;
36   uint32_t palette[16];
37   char    *palette_str;
38   char    *ifo_str;
39   int      has_palette;
40   uint8_t  colormap[4];
41   uint8_t  alpha[256];
42   uint8_t *buf;
43   int      buf_size;
44   int      forced_subs_only;
45 #ifdef DEBUG
46   int sub_id;
47 #endif
48 } DVDSubContext;
49
50 static void yuv_a_to_rgba(const uint8_t *ycbcr, const uint8_t *alpha, uint32_t *rgba, int num_values)
51 {
52     const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
53     uint8_t r, g, b;
54     int i, y, cb, cr;
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++;
61         YUV_TO_RGB1_CCIR(cb, cr);
62         YUV_TO_RGB2_CCIR(r, g, b, y);
63         *rgba++ = (*alpha++ << 24) | (r << 16) | (g << 8) | b;
64     }
65 }
66
67 static int decode_run_2bit(GetBitContext *gb, int *color)
68 {
69     unsigned int v, t;
70
71     v = 0;
72     for (t = 1; v < t && t <= 0x40; t <<= 2)
73         v = (v << 4) | get_bits(gb, 4);
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
81 static int decode_run_8bit(GetBitContext *gb, int *color)
82 {
83     int len;
84     int has_run = get_bits1(gb);
85     if (get_bits1(gb))
86         *color = get_bits(gb, 8);
87     else
88         *color = get_bits(gb, 2);
89     if (has_run) {
90         if (get_bits1(gb)) {
91             len = get_bits(gb, 7);
92             if (len == 0)
93                 len = INT_MAX;
94             else
95                 len += 9;
96         } else
97             len = get_bits(gb, 3) + 2;
98     } else
99         len = 1;
100     return len;
101 }
102
103 static int decode_rle(uint8_t *bitmap, int linesize, int w, int h,
104                       const uint8_t *buf, int start, int buf_size, int is_8bit)
105 {
106     GetBitContext gb;
107     int bit_len;
108     int x, y, len, color;
109     uint8_t *d;
110
111     bit_len = (buf_size - start) * 8;
112     init_get_bits(&gb, buf + start, bit_len);
113
114     x = 0;
115     y = 0;
116     d = bitmap;
117     for(;;) {
118         if (get_bits_count(&gb) > bit_len)
119             return -1;
120         if (is_8bit)
121             len = decode_run_8bit(&gb, &color);
122         else
123             len = decode_run_2bit(&gb, &color);
124         len = FFMIN(len, w - x);
125         memset(d + x, color, len);
126         x += len;
127         if (x >= w) {
128             y++;
129             if (y >= h)
130                 break;
131             d += linesize;
132             x = 0;
133             /* byte align */
134             align_get_bits(&gb);
135         }
136     }
137     return 0;
138 }
139
140 static void guess_palette(DVDSubContext* ctx,
141                           uint32_t *rgba_palette,
142                           uint32_t subtitle_color)
143 {
144     static const uint8_t level_map[4][4] = {
145         // this configuration (full range, lowest to highest) in tests
146         // seemed most common, so assume this
147         {0xff},
148         {0x00, 0xff},
149         {0x00, 0x80, 0xff},
150         {0x00, 0x55, 0xaa, 0xff},
151     };
152     uint8_t color_used[16] = { 0 };
153     int nb_opaque_colors, i, level, j, r, g, b;
154     uint8_t *colormap = ctx->colormap, *alpha = ctx->alpha;
155
156     if(ctx->has_palette) {
157         for(i = 0; i < 4; i++)
158             rgba_palette[i] = (ctx->palette[colormap[i]] & 0x00ffffff)
159                               | ((alpha[i] * 17U) << 24);
160         return;
161     }
162
163     for(i = 0; i < 4; i++)
164         rgba_palette[i] = 0;
165
166     nb_opaque_colors = 0;
167     for(i = 0; i < 4; i++) {
168         if (alpha[i] != 0 && !color_used[colormap[i]]) {
169             color_used[colormap[i]] = 1;
170             nb_opaque_colors++;
171         }
172     }
173
174     if (nb_opaque_colors == 0)
175         return;
176
177     j = 0;
178     memset(color_used, 0, 16);
179     for(i = 0; i < 4; i++) {
180         if (alpha[i] != 0) {
181             if (!color_used[colormap[i]])  {
182                 level = level_map[nb_opaque_colors][j];
183                 r = (((subtitle_color >> 16) & 0xff) * level) >> 8;
184                 g = (((subtitle_color >> 8) & 0xff) * level) >> 8;
185                 b = (((subtitle_color >> 0) & 0xff) * level) >> 8;
186                 rgba_palette[i] = b | (g << 8) | (r << 16) | ((alpha[i] * 17) << 24);
187                 color_used[colormap[i]] = (i + 1);
188                 j++;
189             } else {
190                 rgba_palette[i] = (rgba_palette[color_used[colormap[i]] - 1] & 0x00ffffff) |
191                                     ((alpha[i] * 17) << 24);
192             }
193         }
194     }
195 }
196
197 static void reset_rects(AVSubtitle *sub_header)
198 {
199     int i;
200
201     if (sub_header->rects) {
202         for (i = 0; i < sub_header->num_rects; i++) {
203             av_freep(&sub_header->rects[i]->pict.data[0]);
204             av_freep(&sub_header->rects[i]->pict.data[1]);
205             av_freep(&sub_header->rects[i]);
206         }
207         av_freep(&sub_header->rects);
208         sub_header->num_rects = 0;
209     }
210 }
211
212 #define READ_OFFSET(a) (big_offsets ? AV_RB32(a) : AV_RB16(a))
213
214 static int decode_dvd_subtitles(DVDSubContext *ctx, AVSubtitle *sub_header,
215                                 const uint8_t *buf, int buf_size)
216 {
217     int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos;
218     int big_offsets, offset_size, is_8bit = 0;
219     const uint8_t *yuv_palette = NULL;
220     uint8_t *colormap = ctx->colormap, *alpha = ctx->alpha;
221     int date;
222     int i;
223     int is_menu = 0;
224
225     if (buf_size < 10)
226         return -1;
227
228     if (AV_RB16(buf) == 0) {   /* HD subpicture with 4-byte offsets */
229         big_offsets = 1;
230         offset_size = 4;
231         cmd_pos = 6;
232     } else {
233         big_offsets = 0;
234         offset_size = 2;
235         cmd_pos = 2;
236     }
237
238     cmd_pos = READ_OFFSET(buf + cmd_pos);
239
240     if (cmd_pos < 0 || cmd_pos > buf_size - 2 - offset_size)
241         return AVERROR(EAGAIN);
242
243     while (cmd_pos > 0 && cmd_pos < buf_size - 2 - offset_size) {
244         date = AV_RB16(buf + cmd_pos);
245         next_cmd_pos = READ_OFFSET(buf + cmd_pos + 2);
246         av_dlog(NULL, "cmd_pos=0x%04x next=0x%04x date=%d\n",
247                 cmd_pos, next_cmd_pos, date);
248         pos = cmd_pos + 2 + offset_size;
249         offset1 = -1;
250         offset2 = -1;
251         x1 = y1 = x2 = y2 = 0;
252         while (pos < buf_size) {
253             cmd = buf[pos++];
254             av_dlog(NULL, "cmd=%02x\n", cmd);
255             switch(cmd) {
256             case 0x00:
257                 /* menu subpicture */
258                 is_menu = 1;
259                 break;
260             case 0x01:
261                 /* set start date */
262                 sub_header->start_display_time = (date << 10) / 90;
263                 break;
264             case 0x02:
265                 /* set end date */
266                 sub_header->end_display_time = (date << 10) / 90;
267                 break;
268             case 0x03:
269                 /* set colormap */
270                 if ((buf_size - pos) < 2)
271                     goto fail;
272                 colormap[3] = buf[pos] >> 4;
273                 colormap[2] = buf[pos] & 0x0f;
274                 colormap[1] = buf[pos + 1] >> 4;
275                 colormap[0] = buf[pos + 1] & 0x0f;
276                 pos += 2;
277                 break;
278             case 0x04:
279                 /* set alpha */
280                 if ((buf_size - pos) < 2)
281                     goto fail;
282                 alpha[3] = buf[pos] >> 4;
283                 alpha[2] = buf[pos] & 0x0f;
284                 alpha[1] = buf[pos + 1] >> 4;
285                 alpha[0] = buf[pos + 1] & 0x0f;
286                 pos += 2;
287             av_dlog(NULL, "alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]);
288                 break;
289             case 0x05:
290             case 0x85:
291                 if ((buf_size - pos) < 6)
292                     goto fail;
293                 x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
294                 x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
295                 y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
296                 y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
297                 if (cmd & 0x80)
298                     is_8bit = 1;
299                 av_dlog(NULL, "x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2);
300                 pos += 6;
301                 break;
302             case 0x06:
303                 if ((buf_size - pos) < 4)
304                     goto fail;
305                 offset1 = AV_RB16(buf + pos);
306                 offset2 = AV_RB16(buf + pos + 2);
307                 av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
308                 pos += 4;
309                 break;
310             case 0x86:
311                 if ((buf_size - pos) < 8)
312                     goto fail;
313                 offset1 = AV_RB32(buf + pos);
314                 offset2 = AV_RB32(buf + pos + 4);
315                 av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
316                 pos += 8;
317                 break;
318
319             case 0x83:
320                 /* HD set palette */
321                 if ((buf_size - pos) < 768)
322                     goto fail;
323                 yuv_palette = buf + pos;
324                 pos += 768;
325                 break;
326             case 0x84:
327                 /* HD set contrast (alpha) */
328                 if ((buf_size - pos) < 256)
329                     goto fail;
330                 for (i = 0; i < 256; i++)
331                     alpha[i] = 0xFF - buf[pos+i];
332                 pos += 256;
333                 break;
334
335             case 0xff:
336                 goto the_end;
337             default:
338                 av_dlog(NULL, "unrecognised subpicture command 0x%x\n", cmd);
339                 goto the_end;
340             }
341         }
342     the_end:
343         if (offset1 >= 0) {
344             int w, h;
345             uint8_t *bitmap;
346
347             /* decode the bitmap */
348             w = x2 - x1 + 1;
349             if (w < 0)
350                 w = 0;
351             h = y2 - y1 + 1;
352             if (h < 0)
353                 h = 0;
354             if (w > 0 && h > 0) {
355                 reset_rects(sub_header);
356
357                 bitmap = av_malloc(w * h);
358                 sub_header->rects = av_mallocz(sizeof(*sub_header->rects));
359                 sub_header->rects[0] = av_mallocz(sizeof(AVSubtitleRect));
360                 sub_header->num_rects = 1;
361                 sub_header->rects[0]->pict.data[0] = bitmap;
362                 decode_rle(bitmap, w * 2, w, (h + 1) / 2,
363                            buf, offset1, buf_size, is_8bit);
364                 decode_rle(bitmap + w, w * 2, w, h / 2,
365                            buf, offset2, buf_size, is_8bit);
366                 sub_header->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
367                 if (is_8bit) {
368                     if (!yuv_palette)
369                         goto fail;
370                     sub_header->rects[0]->nb_colors = 256;
371                     yuv_a_to_rgba(yuv_palette, alpha, (uint32_t*)sub_header->rects[0]->pict.data[1], 256);
372                 } else {
373                     sub_header->rects[0]->nb_colors = 4;
374                     guess_palette(ctx, (uint32_t*)sub_header->rects[0]->pict.data[1],
375                                   0xffff00);
376                 }
377                 sub_header->rects[0]->x = x1;
378                 sub_header->rects[0]->y = y1;
379                 sub_header->rects[0]->w = w;
380                 sub_header->rects[0]->h = h;
381                 sub_header->rects[0]->type = SUBTITLE_BITMAP;
382                 sub_header->rects[0]->pict.linesize[0] = w;
383                 sub_header->rects[0]->flags = is_menu ? AV_SUBTITLE_FLAG_FORCED : 0;
384             }
385         }
386         if (next_cmd_pos < cmd_pos) {
387             av_log(NULL, AV_LOG_ERROR, "Invalid command offset\n");
388             break;
389         }
390         if (next_cmd_pos == cmd_pos)
391             break;
392         cmd_pos = next_cmd_pos;
393     }
394     if (sub_header->num_rects > 0)
395         return is_menu;
396  fail:
397     reset_rects(sub_header);
398     return -1;
399 }
400
401 static int is_transp(const uint8_t *buf, int pitch, int n,
402                      const uint8_t *transp_color)
403 {
404     int i;
405     for(i = 0; i < n; i++) {
406         if (!transp_color[*buf])
407             return 0;
408         buf += pitch;
409     }
410     return 1;
411 }
412
413 /* return 0 if empty rectangle, 1 if non empty */
414 static int find_smallest_bounding_rectangle(AVSubtitle *s)
415 {
416     uint8_t transp_color[256] = { 0 };
417     int y1, y2, x1, x2, y, w, h, i;
418     uint8_t *bitmap;
419
420     if (s->num_rects == 0 || !s->rects || s->rects[0]->w <= 0 || s->rects[0]->h <= 0)
421         return 0;
422
423     for(i = 0; i < s->rects[0]->nb_colors; i++) {
424         if ((((uint32_t*)s->rects[0]->pict.data[1])[i] >> 24) == 0)
425             transp_color[i] = 1;
426     }
427     y1 = 0;
428     while (y1 < s->rects[0]->h && is_transp(s->rects[0]->pict.data[0] + y1 * s->rects[0]->pict.linesize[0],
429                                   1, s->rects[0]->w, transp_color))
430         y1++;
431     if (y1 == s->rects[0]->h) {
432         av_freep(&s->rects[0]->pict.data[0]);
433         s->rects[0]->w = s->rects[0]->h = 0;
434         return 0;
435     }
436
437     y2 = s->rects[0]->h - 1;
438     while (y2 > 0 && is_transp(s->rects[0]->pict.data[0] + y2 * s->rects[0]->pict.linesize[0], 1,
439                                s->rects[0]->w, transp_color))
440         y2--;
441     x1 = 0;
442     while (x1 < (s->rects[0]->w - 1) && is_transp(s->rects[0]->pict.data[0] + x1, s->rects[0]->pict.linesize[0],
443                                         s->rects[0]->h, transp_color))
444         x1++;
445     x2 = s->rects[0]->w - 1;
446     while (x2 > 0 && is_transp(s->rects[0]->pict.data[0] + x2, s->rects[0]->pict.linesize[0], s->rects[0]->h,
447                                   transp_color))
448         x2--;
449     w = x2 - x1 + 1;
450     h = y2 - y1 + 1;
451     bitmap = av_malloc(w * h);
452     if (!bitmap)
453         return 1;
454     for(y = 0; y < h; y++) {
455         memcpy(bitmap + w * y, s->rects[0]->pict.data[0] + x1 + (y1 + y) * s->rects[0]->pict.linesize[0], w);
456     }
457     av_freep(&s->rects[0]->pict.data[0]);
458     s->rects[0]->pict.data[0] = bitmap;
459     s->rects[0]->pict.linesize[0] = w;
460     s->rects[0]->w = w;
461     s->rects[0]->h = h;
462     s->rects[0]->x += x1;
463     s->rects[0]->y += y1;
464     return 1;
465 }
466
467 #ifdef DEBUG
468 #define ALPHA_MIX(A,BACK,FORE) (((255-(A)) * (BACK) + (A) * (FORE)) / 255)
469 static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
470                      uint32_t *rgba_palette)
471 {
472     int x, y, alpha;
473     uint32_t v;
474     int back[3] = {0, 255, 0};  /* green background */
475     FILE *f;
476
477     f = fopen(filename, "w");
478     if (!f) {
479         perror(filename);
480         return;
481     }
482     fprintf(f, "P6\n"
483             "%d %d\n"
484             "%d\n",
485             w, h, 255);
486     for(y = 0; y < h; y++) {
487         for(x = 0; x < w; x++) {
488             v = rgba_palette[bitmap[y * w + x]];
489             alpha = v >> 24;
490             putc(ALPHA_MIX(alpha, back[0], (v >> 16) & 0xff), f);
491             putc(ALPHA_MIX(alpha, back[1], (v >> 8) & 0xff), f);
492             putc(ALPHA_MIX(alpha, back[2], (v >> 0) & 0xff), f);
493         }
494     }
495     fclose(f);
496 }
497 #endif
498
499 static int append_to_cached_buf(AVCodecContext *avctx,
500                                 const uint8_t *buf, int buf_size)
501 {
502     DVDSubContext *ctx = avctx->priv_data;
503
504     if (ctx->buf_size > 0xffff - buf_size) {
505         av_log(avctx, AV_LOG_WARNING, "Attempt to reconstruct "
506                "too large SPU packets aborted.\n");
507         av_freep(&ctx->buf);
508         return AVERROR_INVALIDDATA;
509     }
510     ctx->buf = av_realloc(ctx->buf, ctx->buf_size + buf_size);
511     if (!ctx->buf)
512         return AVERROR(ENOMEM);
513     memcpy(ctx->buf + ctx->buf_size, buf, buf_size);
514     ctx->buf_size += buf_size;
515     return 0;
516 }
517
518 static int dvdsub_decode(AVCodecContext *avctx,
519                          void *data, int *data_size,
520                          AVPacket *avpkt)
521 {
522     DVDSubContext *ctx = avctx->priv_data;
523     const uint8_t *buf = avpkt->data;
524     int buf_size = avpkt->size;
525     AVSubtitle *sub = data;
526     int is_menu;
527
528     if (ctx->buf) {
529         int ret = append_to_cached_buf(avctx, buf, buf_size);
530         if (ret < 0) {
531             *data_size = 0;
532             return ret;
533         }
534         buf = ctx->buf;
535         buf_size = ctx->buf_size;
536     }
537
538     is_menu = decode_dvd_subtitles(ctx, sub, buf, buf_size);
539     if (is_menu == AVERROR(EAGAIN)) {
540         *data_size = 0;
541         return append_to_cached_buf(avctx, buf, buf_size);
542     }
543
544     if (is_menu < 0) {
545     no_subtitle:
546         reset_rects(sub);
547         *data_size = 0;
548
549         return buf_size;
550     }
551     if (!is_menu && find_smallest_bounding_rectangle(sub) == 0)
552         goto no_subtitle;
553
554     if (ctx->forced_subs_only && !(sub->rects[0]->flags & AV_SUBTITLE_FLAG_FORCED))
555         goto no_subtitle;
556
557 #if defined(DEBUG)
558     {
559     char ppm_name[32];
560
561     snprintf(ppm_name, sizeof(ppm_name), "/tmp/%05d.ppm", ctx->sub_id++);
562     av_dlog(NULL, "start=%d ms end =%d ms\n",
563             sub->start_display_time,
564             sub->end_display_time);
565     ppm_save(ppm_name, sub->rects[0]->pict.data[0],
566              sub->rects[0]->w, sub->rects[0]->h, (uint32_t*) sub->rects[0]->pict.data[1]);
567     }
568 #endif
569
570     av_freep(&ctx->buf);
571     ctx->buf_size = 0;
572     *data_size = 1;
573     return buf_size;
574 }
575
576 static void parse_palette(DVDSubContext *ctx, char *p)
577 {
578     int i;
579
580     ctx->has_palette = 1;
581     for(i=0;i<16;i++) {
582         ctx->palette[i] = strtoul(p, &p, 16);
583         while(*p == ',' || av_isspace(*p))
584             p++;
585     }
586 }
587
588 static int parse_ifo_palette(DVDSubContext *ctx, char *p)
589 {
590     FILE *ifo;
591     char ifostr[12];
592     uint32_t sp_pgci, pgci, off_pgc, pgc;
593     uint8_t r, g, b, yuv[65], *buf;
594     int i, y, cb, cr, r_add, g_add, b_add;
595     int ret = 0;
596     const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
597
598     ctx->has_palette = 0;
599     if ((ifo = fopen(p, "r")) == NULL) {
600         av_log(ctx, AV_LOG_WARNING, "Unable to open IFO file \"%s\": %s\n", p, av_err2str(AVERROR(errno)));
601         return AVERROR_EOF;
602     }
603     if (fread(ifostr, 12, 1, ifo) != 1 || memcmp(ifostr, "DVDVIDEO-VTS", 12)) {
604         av_log(ctx, AV_LOG_WARNING, "\"%s\" is not a proper IFO file\n", p);
605         ret = AVERROR_INVALIDDATA;
606         goto end;
607     }
608     if (fseek(ifo, 0xCC, SEEK_SET) == -1) {
609         ret = AVERROR(errno);
610         goto end;
611     }
612     if (fread(&sp_pgci, 4, 1, ifo) == 1) {
613         pgci = av_be2ne32(sp_pgci) * 2048;
614         if (fseek(ifo, pgci + 0x0C, SEEK_SET) == -1) {
615             ret = AVERROR(errno);
616             goto end;
617         }
618         if (fread(&off_pgc, 4, 1, ifo) == 1) {
619             pgc = pgci + av_be2ne32(off_pgc);
620             if (fseek(ifo, pgc + 0xA4, SEEK_SET) == -1) {
621                 ret = AVERROR(errno);
622                 goto end;
623             }
624             if (fread(yuv, 64, 1, ifo) == 1) {
625                 buf = yuv;
626                 for(i=0; i<16; i++) {
627                     y  = *++buf;
628                     cr = *++buf;
629                     cb = *++buf;
630                     YUV_TO_RGB1_CCIR(cb, cr);
631                     YUV_TO_RGB2_CCIR(r, g, b, y);
632                     ctx->palette[i] = (r << 16) + (g << 8) + b;
633                     buf++;
634                 }
635                 ctx->has_palette = 1;
636             }
637         }
638     }
639     if (ctx->has_palette == 0) {
640         av_log(ctx, AV_LOG_WARNING, "Failed to read palette from IFO file \"%s\"\n", p);
641         ret = AVERROR_INVALIDDATA;
642     }
643 end:
644     fclose(ifo);
645     return ret;
646 }
647
648 static int dvdsub_parse_extradata(AVCodecContext *avctx)
649 {
650     DVDSubContext *ctx = (DVDSubContext*) avctx->priv_data;
651     char *dataorig, *data;
652     int ret = 1;
653
654     if (!avctx->extradata || !avctx->extradata_size)
655         return 1;
656
657     dataorig = data = av_malloc(avctx->extradata_size+1);
658     if (!data)
659         return AVERROR(ENOMEM);
660     memcpy(data, avctx->extradata, avctx->extradata_size);
661     data[avctx->extradata_size] = '\0';
662
663     for(;;) {
664         int pos = strcspn(data, "\n\r");
665         if (pos==0 && *data==0)
666             break;
667
668         if (strncmp("palette:", data, 8) == 0) {
669             parse_palette(ctx, data + 8);
670         } else if (strncmp("size:", data, 5) == 0) {
671             int w, h;
672             if (sscanf(data + 5, "%dx%d", &w, &h) == 2) {
673                ret = ff_set_dimensions(avctx, w, h);
674                if (ret < 0)
675                    goto fail;
676             }
677         }
678
679         data += pos;
680         data += strspn(data, "\n\r");
681     }
682
683 fail:
684     av_free(dataorig);
685     return ret;
686 }
687
688 static av_cold int dvdsub_init(AVCodecContext *avctx)
689 {
690     DVDSubContext *ctx = avctx->priv_data;
691     int ret;
692
693     if ((ret = dvdsub_parse_extradata(avctx)) < 0)
694         return ret;
695
696     if (ctx->ifo_str)
697         parse_ifo_palette(ctx, ctx->ifo_str);
698     if (ctx->palette_str)
699         parse_palette(ctx, ctx->palette_str);
700     if (ctx->has_palette) {
701         int i;
702         av_log(avctx, AV_LOG_DEBUG, "palette:");
703         for(i=0;i<16;i++)
704             av_log(avctx, AV_LOG_DEBUG, " 0x%06x", ctx->palette[i]);
705         av_log(avctx, AV_LOG_DEBUG, "\n");
706     }
707
708     return 1;
709 }
710
711 static av_cold int dvdsub_close(AVCodecContext *avctx)
712 {
713     DVDSubContext *ctx = avctx->priv_data;
714     av_freep(&ctx->buf);
715     ctx->buf_size = 0;
716     return 0;
717 }
718
719 #define OFFSET(field) offsetof(DVDSubContext, field)
720 #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
721 static const AVOption options[] = {
722     { "palette", "set the global palette", OFFSET(palette_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, SD },
723     { "ifo_palette", "obtain the global palette from .IFO file", OFFSET(ifo_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, SD },
724     { "forced_subs_only", "Only show forced subtitles", OFFSET(forced_subs_only), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, SD},
725     { NULL }
726 };
727 static const AVClass dvdsub_class = {
728     .class_name = "dvdsubdec",
729     .item_name  = av_default_item_name,
730     .option     = options,
731     .version    = LIBAVUTIL_VERSION_INT,
732 };
733
734 AVCodec ff_dvdsub_decoder = {
735     .name           = "dvdsub",
736     .long_name      = NULL_IF_CONFIG_SMALL("DVD subtitles"),
737     .type           = AVMEDIA_TYPE_SUBTITLE,
738     .id             = AV_CODEC_ID_DVD_SUBTITLE,
739     .priv_data_size = sizeof(DVDSubContext),
740     .init           = dvdsub_init,
741     .decode         = dvdsub_decode,
742     .close          = dvdsub_close,
743     .priv_class     = &dvdsub_class,
744 };