]> git.sesse.net Git - ffmpeg/blob - libavcodec/dvdsubdec.c
ee06d55117fd955637eaa2263f9226dd13031af7
[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[0x10000];
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     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;
118     init_get_bits(&gb, buf + start, bit_len);
119
120     x = 0;
121     y = 0;
122     d = bitmap;
123     for(;;) {
124         if (get_bits_count(&gb) > bit_len)
125             return -1;
126         if (is_8bit)
127             len = decode_run_8bit(&gb, &color);
128         else
129             len = decode_run_2bit(&gb, &color);
130         len = FFMIN(len, w - x);
131         memset(d + x, color, len);
132         x += len;
133         if (x >= w) {
134             y++;
135             if (y >= h)
136                 break;
137             d += linesize;
138             x = 0;
139             /* byte align */
140             align_get_bits(&gb);
141         }
142     }
143     return 0;
144 }
145
146 static void guess_palette(DVDSubContext* ctx,
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;
160     uint8_t *colormap = ctx->colormap, *alpha = ctx->alpha;
161
162     if(ctx->has_palette) {
163         for(i = 0; i < 4; i++)
164             rgba_palette[i] = (ctx->palette[colormap[i]] & 0x00ffffff)
165                               | ((alpha[i] * 17U) << 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++) {
186         if (alpha[i] != 0) {
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
203 static void reset_rects(AVSubtitle *sub_header)
204 {
205     int i;
206
207     if (sub_header->rects) {
208         for (i = 0; i < sub_header->num_rects; i++) {
209             av_freep(&sub_header->rects[i]->data[0]);
210             av_freep(&sub_header->rects[i]->data[1]);
211             av_freep(&sub_header->rects[i]);
212         }
213         av_freep(&sub_header->rects);
214         sub_header->num_rects = 0;
215     }
216 }
217
218 #define READ_OFFSET(a) (big_offsets ? AV_RB32(a) : AV_RB16(a))
219
220 static int decode_dvd_subtitles(DVDSubContext *ctx, AVSubtitle *sub_header,
221                                 const uint8_t *buf, int buf_size)
222 {
223     int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos;
224     int big_offsets, offset_size, is_8bit = 0;
225     const uint8_t *yuv_palette = NULL;
226     uint8_t *colormap = ctx->colormap, *alpha = ctx->alpha;
227     int date;
228     int i;
229     int is_menu = 0;
230     uint32_t size;
231
232     if (buf_size < 10)
233         return -1;
234
235     if (AV_RB16(buf) == 0) {   /* HD subpicture with 4-byte offsets */
236         big_offsets = 1;
237         offset_size = 4;
238         cmd_pos = 6;
239     } else {
240         big_offsets = 0;
241         offset_size = 2;
242         cmd_pos = 2;
243     }
244
245     size = READ_OFFSET(buf + (big_offsets ? 2 : 0));
246     cmd_pos = READ_OFFSET(buf + cmd_pos);
247
248     if (cmd_pos < 0 || cmd_pos > buf_size - 2 - offset_size) {
249         if (cmd_pos > size) {
250             av_log(ctx, AV_LOG_ERROR, "Discarding invalid packet\n");
251             return 0;
252         }
253         return AVERROR(EAGAIN);
254     }
255
256     while (cmd_pos > 0 && cmd_pos < buf_size - 2 - offset_size) {
257         date = AV_RB16(buf + cmd_pos);
258         next_cmd_pos = READ_OFFSET(buf + cmd_pos + 2);
259         ff_dlog(NULL, "cmd_pos=0x%04x next=0x%04x date=%d\n",
260                 cmd_pos, next_cmd_pos, date);
261         pos = cmd_pos + 2 + offset_size;
262         offset1 = -1;
263         offset2 = -1;
264         x1 = y1 = x2 = y2 = 0;
265         while (pos < buf_size) {
266             cmd = buf[pos++];
267             ff_dlog(NULL, "cmd=%02x\n", cmd);
268             switch(cmd) {
269             case 0x00:
270                 /* menu subpicture */
271                 is_menu = 1;
272                 break;
273             case 0x01:
274                 /* set start date */
275                 sub_header->start_display_time = (date << 10) / 90;
276                 break;
277             case 0x02:
278                 /* set end date */
279                 sub_header->end_display_time = (date << 10) / 90;
280                 break;
281             case 0x03:
282                 /* set colormap */
283                 if ((buf_size - pos) < 2)
284                     goto fail;
285                 colormap[3] = buf[pos] >> 4;
286                 colormap[2] = buf[pos] & 0x0f;
287                 colormap[1] = buf[pos + 1] >> 4;
288                 colormap[0] = buf[pos + 1] & 0x0f;
289                 pos += 2;
290                 break;
291             case 0x04:
292                 /* set alpha */
293                 if ((buf_size - pos) < 2)
294                     goto fail;
295                 alpha[3] = buf[pos] >> 4;
296                 alpha[2] = buf[pos] & 0x0f;
297                 alpha[1] = buf[pos + 1] >> 4;
298                 alpha[0] = buf[pos + 1] & 0x0f;
299                 pos += 2;
300                 ff_dlog(NULL, "alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]);
301                 break;
302             case 0x05:
303             case 0x85:
304                 if ((buf_size - pos) < 6)
305                     goto fail;
306                 x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
307                 x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
308                 y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
309                 y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
310                 if (cmd & 0x80)
311                     is_8bit = 1;
312                 ff_dlog(NULL, "x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2);
313                 pos += 6;
314                 break;
315             case 0x06:
316                 if ((buf_size - pos) < 4)
317                     goto fail;
318                 offset1 = AV_RB16(buf + pos);
319                 offset2 = AV_RB16(buf + pos + 2);
320                 ff_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
321                 pos += 4;
322                 break;
323             case 0x86:
324                 if ((buf_size - pos) < 8)
325                     goto fail;
326                 offset1 = AV_RB32(buf + pos);
327                 offset2 = AV_RB32(buf + pos + 4);
328                 ff_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
329                 pos += 8;
330                 break;
331
332             case 0x83:
333                 /* HD set palette */
334                 if ((buf_size - pos) < 768)
335                     goto fail;
336                 yuv_palette = buf + pos;
337                 pos += 768;
338                 break;
339             case 0x84:
340                 /* HD set contrast (alpha) */
341                 if ((buf_size - pos) < 256)
342                     goto fail;
343                 for (i = 0; i < 256; i++)
344                     alpha[i] = 0xFF - buf[pos+i];
345                 pos += 256;
346                 break;
347
348             case 0xff:
349                 goto the_end;
350             default:
351                 ff_dlog(NULL, "unrecognised subpicture command 0x%x\n", cmd);
352                 goto the_end;
353             }
354         }
355     the_end:
356         if (offset1 >= 0) {
357             int w, h;
358             uint8_t *bitmap;
359
360             /* decode the bitmap */
361             w = x2 - x1 + 1;
362             if (w < 0)
363                 w = 0;
364             h = y2 - y1 + 1;
365             if (h < 0)
366                 h = 0;
367             if (w > 0 && h > 0) {
368                 reset_rects(sub_header);
369
370                 sub_header->rects = av_mallocz(sizeof(*sub_header->rects));
371                 if (!sub_header->rects)
372                     goto fail;
373                 sub_header->rects[0] = av_mallocz(sizeof(AVSubtitleRect));
374                 if (!sub_header->rects[0])
375                     goto fail;
376                 sub_header->num_rects = 1;
377                 bitmap = sub_header->rects[0]->data[0] = av_malloc(w * h);
378                 if (!bitmap)
379                     goto fail;
380                 if (decode_rle(bitmap, w * 2, w, (h + 1) / 2,
381                                buf, offset1, buf_size, is_8bit) < 0)
382                     goto fail;
383                 if (decode_rle(bitmap + w, w * 2, w, h / 2,
384                                buf, offset2, buf_size, is_8bit) < 0)
385                     goto fail;
386                 sub_header->rects[0]->data[1] = av_mallocz(AVPALETTE_SIZE);
387                 if (!sub_header->rects[0]->data[1])
388                     goto fail;
389                 if (is_8bit) {
390                     if (!yuv_palette)
391                         goto fail;
392                     sub_header->rects[0]->nb_colors = 256;
393                     yuv_a_to_rgba(yuv_palette, alpha,
394                                   (uint32_t *)sub_header->rects[0]->data[1],
395                                   256);
396                 } else {
397                     sub_header->rects[0]->nb_colors = 4;
398                     guess_palette(ctx, (uint32_t*)sub_header->rects[0]->data[1],
399                                   0xffff00);
400                 }
401                 sub_header->rects[0]->x = x1;
402                 sub_header->rects[0]->y = y1;
403                 sub_header->rects[0]->w = w;
404                 sub_header->rects[0]->h = h;
405                 sub_header->rects[0]->type = SUBTITLE_BITMAP;
406                 sub_header->rects[0]->linesize[0] = w;
407                 sub_header->rects[0]->flags = is_menu ? AV_SUBTITLE_FLAG_FORCED : 0;
408
409 #if FF_API_AVPICTURE
410 FF_DISABLE_DEPRECATION_WARNINGS
411                 for (i = 0; i < 4; i++) {
412                     sub_header->rects[0]->pict.data[i] = sub_header->rects[0]->data[i];
413                     sub_header->rects[0]->pict.linesize[i] = sub_header->rects[0]->linesize[i];
414                 }
415 FF_ENABLE_DEPRECATION_WARNINGS
416 #endif
417             }
418         }
419         if (next_cmd_pos < cmd_pos) {
420             av_log(ctx, AV_LOG_ERROR, "Invalid command offset\n");
421             break;
422         }
423         if (next_cmd_pos == cmd_pos)
424             break;
425         cmd_pos = next_cmd_pos;
426     }
427     if (sub_header->num_rects > 0)
428         return is_menu;
429  fail:
430     reset_rects(sub_header);
431     return -1;
432 }
433
434 static int is_transp(const uint8_t *buf, int pitch, int n,
435                      const uint8_t *transp_color)
436 {
437     int i;
438     for(i = 0; i < n; i++) {
439         if (!transp_color[*buf])
440             return 0;
441         buf += pitch;
442     }
443     return 1;
444 }
445
446 /* return 0 if empty rectangle, 1 if non empty */
447 static int find_smallest_bounding_rectangle(AVSubtitle *s)
448 {
449     uint8_t transp_color[256] = { 0 };
450     int y1, y2, x1, x2, y, w, h, i;
451     uint8_t *bitmap;
452
453     if (s->num_rects == 0 || !s->rects || s->rects[0]->w <= 0 || s->rects[0]->h <= 0)
454         return 0;
455
456     for(i = 0; i < s->rects[0]->nb_colors; i++) {
457         if ((((uint32_t *)s->rects[0]->data[1])[i] >> 24) == 0)
458             transp_color[i] = 1;
459     }
460     y1 = 0;
461     while (y1 < s->rects[0]->h && is_transp(s->rects[0]->data[0] + y1 * s->rects[0]->linesize[0],
462                                   1, s->rects[0]->w, transp_color))
463         y1++;
464     if (y1 == s->rects[0]->h) {
465         av_freep(&s->rects[0]->data[0]);
466         s->rects[0]->w = s->rects[0]->h = 0;
467         return 0;
468     }
469
470     y2 = s->rects[0]->h - 1;
471     while (y2 > 0 && is_transp(s->rects[0]->data[0] + y2 * s->rects[0]->linesize[0], 1,
472                                s->rects[0]->w, transp_color))
473         y2--;
474     x1 = 0;
475     while (x1 < (s->rects[0]->w - 1) && is_transp(s->rects[0]->data[0] + x1, s->rects[0]->linesize[0],
476                                         s->rects[0]->h, transp_color))
477         x1++;
478     x2 = s->rects[0]->w - 1;
479     while (x2 > 0 && is_transp(s->rects[0]->data[0] + x2, s->rects[0]->linesize[0], s->rects[0]->h,
480                                   transp_color))
481         x2--;
482     w = x2 - x1 + 1;
483     h = y2 - y1 + 1;
484     bitmap = av_malloc(w * h);
485     if (!bitmap)
486         return 1;
487     for(y = 0; y < h; y++) {
488         memcpy(bitmap + w * y, s->rects[0]->data[0] + x1 + (y1 + y) * s->rects[0]->linesize[0], w);
489     }
490     av_freep(&s->rects[0]->data[0]);
491     s->rects[0]->data[0] = bitmap;
492     s->rects[0]->linesize[0] = w;
493     s->rects[0]->w = w;
494     s->rects[0]->h = h;
495     s->rects[0]->x += x1;
496     s->rects[0]->y += y1;
497
498 #if FF_API_AVPICTURE
499 FF_DISABLE_DEPRECATION_WARNINGS
500     for (i = 0; i < 4; i++) {
501         s->rects[0]->pict.data[i] = s->rects[0]->data[i];
502         s->rects[0]->pict.linesize[i] = s->rects[0]->linesize[i];
503     }
504 FF_ENABLE_DEPRECATION_WARNINGS
505 #endif
506
507     return 1;
508 }
509
510 #ifdef DEBUG
511 #define ALPHA_MIX(A,BACK,FORE) (((255-(A)) * (BACK) + (A) * (FORE)) / 255)
512 static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
513                      uint32_t *rgba_palette)
514 {
515     int x, y, alpha;
516     uint32_t v;
517     int back[3] = {0, 255, 0};  /* green background */
518     FILE *f;
519
520     f = fopen(filename, "w");
521     if (!f) {
522         perror(filename);
523         return;
524     }
525     fprintf(f, "P6\n"
526             "%d %d\n"
527             "%d\n",
528             w, h, 255);
529     for(y = 0; y < h; y++) {
530         for(x = 0; x < w; x++) {
531             v = rgba_palette[bitmap[y * w + x]];
532             alpha = v >> 24;
533             putc(ALPHA_MIX(alpha, back[0], (v >> 16) & 0xff), f);
534             putc(ALPHA_MIX(alpha, back[1], (v >> 8) & 0xff), f);
535             putc(ALPHA_MIX(alpha, back[2], (v >> 0) & 0xff), f);
536         }
537     }
538     fclose(f);
539 }
540 #endif
541
542 static int append_to_cached_buf(AVCodecContext *avctx,
543                                 const uint8_t *buf, int buf_size)
544 {
545     DVDSubContext *ctx = avctx->priv_data;
546
547     if (ctx->buf_size >= sizeof(ctx->buf) - buf_size) {
548         av_log(avctx, AV_LOG_WARNING, "Attempt to reconstruct "
549                "too large SPU packets aborted.\n");
550         ctx->buf_size = 0;
551         return AVERROR_INVALIDDATA;
552     }
553     memcpy(ctx->buf + ctx->buf_size, buf, buf_size);
554     ctx->buf_size += buf_size;
555     return 0;
556 }
557
558 static int dvdsub_decode(AVCodecContext *avctx,
559                          void *data, int *data_size,
560                          AVPacket *avpkt)
561 {
562     DVDSubContext *ctx = avctx->priv_data;
563     const uint8_t *buf = avpkt->data;
564     int buf_size = avpkt->size;
565     AVSubtitle *sub = data;
566     int appended = 0;
567     int is_menu;
568
569     if (ctx->buf_size) {
570         int ret = append_to_cached_buf(avctx, buf, buf_size);
571         if (ret < 0) {
572             *data_size = 0;
573             return ret;
574         }
575         buf = ctx->buf;
576         buf_size = ctx->buf_size;
577         appended = 1;
578     }
579
580     is_menu = decode_dvd_subtitles(ctx, sub, buf, buf_size);
581     if (is_menu == AVERROR(EAGAIN)) {
582         *data_size = 0;
583         return appended ? 0 : append_to_cached_buf(avctx, buf, buf_size);
584     }
585
586     if (is_menu < 0) {
587     no_subtitle:
588         reset_rects(sub);
589         *data_size = 0;
590
591         return buf_size;
592     }
593     if (!is_menu && find_smallest_bounding_rectangle(sub) == 0)
594         goto no_subtitle;
595
596     if (ctx->forced_subs_only && !(sub->rects[0]->flags & AV_SUBTITLE_FLAG_FORCED))
597         goto no_subtitle;
598
599 #if defined(DEBUG)
600     {
601     char ppm_name[32];
602
603     snprintf(ppm_name, sizeof(ppm_name), "/tmp/%05d.ppm", ctx->sub_id++);
604     ff_dlog(NULL, "start=%d ms end =%d ms\n",
605             sub->start_display_time,
606             sub->end_display_time);
607     ppm_save(ppm_name, sub->rects[0]->data[0],
608              sub->rects[0]->w, sub->rects[0]->h, (uint32_t*) sub->rects[0]->data[1]);
609     }
610 #endif
611
612     ctx->buf_size = 0;
613     *data_size = 1;
614     return buf_size;
615 }
616
617 static void parse_palette(DVDSubContext *ctx, char *p)
618 {
619     int i;
620
621     ctx->has_palette = 1;
622     for(i=0;i<16;i++) {
623         ctx->palette[i] = strtoul(p, &p, 16);
624         while(*p == ',' || av_isspace(*p))
625             p++;
626     }
627 }
628
629 static int parse_ifo_palette(DVDSubContext *ctx, char *p)
630 {
631     FILE *ifo;
632     char ifostr[12];
633     uint32_t sp_pgci, pgci, off_pgc, pgc;
634     uint8_t r, g, b, yuv[65], *buf;
635     int i, y, cb, cr, r_add, g_add, b_add;
636     int ret = 0;
637     const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
638
639     ctx->has_palette = 0;
640     if ((ifo = fopen(p, "r")) == NULL) {
641         av_log(ctx, AV_LOG_WARNING, "Unable to open IFO file \"%s\": %s\n", p, av_err2str(AVERROR(errno)));
642         return AVERROR_EOF;
643     }
644     if (fread(ifostr, 12, 1, ifo) != 1 || memcmp(ifostr, "DVDVIDEO-VTS", 12)) {
645         av_log(ctx, AV_LOG_WARNING, "\"%s\" is not a proper IFO file\n", p);
646         ret = AVERROR_INVALIDDATA;
647         goto end;
648     }
649     if (fseek(ifo, 0xCC, SEEK_SET) == -1) {
650         ret = AVERROR(errno);
651         goto end;
652     }
653     if (fread(&sp_pgci, 4, 1, ifo) == 1) {
654         pgci = av_be2ne32(sp_pgci) * 2048;
655         if (fseek(ifo, pgci + 0x0C, SEEK_SET) == -1) {
656             ret = AVERROR(errno);
657             goto end;
658         }
659         if (fread(&off_pgc, 4, 1, ifo) == 1) {
660             pgc = pgci + av_be2ne32(off_pgc);
661             if (fseek(ifo, pgc + 0xA4, SEEK_SET) == -1) {
662                 ret = AVERROR(errno);
663                 goto end;
664             }
665             if (fread(yuv, 64, 1, ifo) == 1) {
666                 buf = yuv;
667                 for(i=0; i<16; i++) {
668                     y  = *++buf;
669                     cr = *++buf;
670                     cb = *++buf;
671                     YUV_TO_RGB1_CCIR(cb, cr);
672                     YUV_TO_RGB2_CCIR(r, g, b, y);
673                     ctx->palette[i] = (r << 16) + (g << 8) + b;
674                     buf++;
675                 }
676                 ctx->has_palette = 1;
677             }
678         }
679     }
680     if (ctx->has_palette == 0) {
681         av_log(ctx, AV_LOG_WARNING, "Failed to read palette from IFO file \"%s\"\n", p);
682         ret = AVERROR_INVALIDDATA;
683     }
684 end:
685     fclose(ifo);
686     return ret;
687 }
688
689 static int dvdsub_parse_extradata(AVCodecContext *avctx)
690 {
691     DVDSubContext *ctx = (DVDSubContext*) avctx->priv_data;
692     char *dataorig, *data;
693     int ret = 1;
694
695     if (!avctx->extradata || !avctx->extradata_size)
696         return 1;
697
698     dataorig = data = av_malloc(avctx->extradata_size+1);
699     if (!data)
700         return AVERROR(ENOMEM);
701     memcpy(data, avctx->extradata, avctx->extradata_size);
702     data[avctx->extradata_size] = '\0';
703
704     for(;;) {
705         int pos = strcspn(data, "\n\r");
706         if (pos==0 && *data==0)
707             break;
708
709         if (strncmp("palette:", data, 8) == 0) {
710             parse_palette(ctx, data + 8);
711         } else if (strncmp("size:", data, 5) == 0) {
712             int w, h;
713             if (sscanf(data + 5, "%dx%d", &w, &h) == 2) {
714                ret = ff_set_dimensions(avctx, w, h);
715                if (ret < 0)
716                    goto fail;
717             }
718         }
719
720         data += pos;
721         data += strspn(data, "\n\r");
722     }
723
724 fail:
725     av_free(dataorig);
726     return ret;
727 }
728
729 static av_cold int dvdsub_init(AVCodecContext *avctx)
730 {
731     DVDSubContext *ctx = avctx->priv_data;
732     int ret;
733
734     if ((ret = dvdsub_parse_extradata(avctx)) < 0)
735         return ret;
736
737     if (ctx->ifo_str)
738         parse_ifo_palette(ctx, ctx->ifo_str);
739     if (ctx->palette_str)
740         parse_palette(ctx, ctx->palette_str);
741     if (ctx->has_palette) {
742         int i;
743         av_log(avctx, AV_LOG_DEBUG, "palette:");
744         for(i=0;i<16;i++)
745             av_log(avctx, AV_LOG_DEBUG, " 0x%06x", ctx->palette[i]);
746         av_log(avctx, AV_LOG_DEBUG, "\n");
747     }
748
749     return 1;
750 }
751
752 static void dvdsub_flush(AVCodecContext *avctx)
753 {
754     DVDSubContext *ctx = avctx->priv_data;
755     ctx->buf_size = 0;
756 }
757
758 static av_cold int dvdsub_close(AVCodecContext *avctx)
759 {
760     dvdsub_flush(avctx);
761     return 0;
762 }
763
764 #define OFFSET(field) offsetof(DVDSubContext, field)
765 #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
766 static const AVOption options[] = {
767     { "palette", "set the global palette", OFFSET(palette_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, SD },
768     { "ifo_palette", "obtain the global palette from .IFO file", OFFSET(ifo_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, SD },
769     { "forced_subs_only", "Only show forced subtitles", OFFSET(forced_subs_only), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, SD},
770     { NULL }
771 };
772 static const AVClass dvdsub_class = {
773     .class_name = "dvdsubdec",
774     .item_name  = av_default_item_name,
775     .option     = options,
776     .version    = LIBAVUTIL_VERSION_INT,
777 };
778
779 AVCodec ff_dvdsub_decoder = {
780     .name           = "dvdsub",
781     .long_name      = NULL_IF_CONFIG_SMALL("DVD subtitles"),
782     .type           = AVMEDIA_TYPE_SUBTITLE,
783     .id             = AV_CODEC_ID_DVD_SUBTITLE,
784     .priv_data_size = sizeof(DVDSubContext),
785     .init           = dvdsub_init,
786     .decode         = dvdsub_decode,
787     .flush          = dvdsub_flush,
788     .close          = dvdsub_close,
789     .priv_class     = &dvdsub_class,
790 };