Revision 1aadf63f

View differences:

libavcodec/dvdsubdec.c
19 19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 20
 */
21 21
#include "avcodec.h"
22
#include "bitstream.h"
23
#include "colorspace.h"
24
#include "dsputil.h"
22 25

  
23 26
//#define DEBUG
24 27

  
28
static void yuv_a_to_rgba(const uint8_t *ycbcr, const uint8_t *alpha, uint32_t *rgba, int num_values)
29
{
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;
34

  
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;
42
    }
43
}
44

  
25 45
static int dvdsub_init_decoder(AVCodecContext *avctx)
26 46
{
27 47
    return 0;
28 48
}
29 49

  
30
static int get_nibble(const uint8_t *buf, int nibble_offset)
50
static int decode_run_2bit(GetBitContext *gb, int *color)
51
{
52
    unsigned int v, t;
53

  
54
    v = 0;
55
    for (t = 1; v < t && t <= 0x40; t <<= 2)
56
        v = (v << 4) | get_bits(gb, 4);
57
    *color = v & 3;
58
    if (v < 4) { /* Code for fill rest of line */
59
        return INT_MAX;
60
    }
61
    return v >> 2;
62
}
63

  
64
static int decode_run_8bit(GetBitContext *gb, int *color)
31 65
{
32
    return (buf[nibble_offset >> 1] >> ((1 - (nibble_offset & 1)) << 2)) & 0xf;
66
    int len;
67
    int has_run = get_bits1(gb);
68
    if (get_bits1(gb))
69
        *color = get_bits(gb, 8);
70
    else
71
        *color = get_bits(gb, 2);
72
    if (has_run) {
73
        if (get_bits1(gb)) {
74
            len = get_bits(gb, 7);
75
            if (len == 0)
76
                len = INT_MAX;
77
            else
78
                len += 9;
79
        } else
80
            len = get_bits(gb, 3) + 2;
81
    } else
82
        len = 1;
83
    return len;
33 84
}
34 85

  
35 86
static int decode_rle(uint8_t *bitmap, int linesize, int w, int h,
36
                      const uint8_t *buf, int nibble_offset, int buf_size)
87
                      const uint8_t *buf, int start, int buf_size, int is_8bit)
37 88
{
38
    unsigned int v;
39
    int x, y, len, color, nibble_end;
89
    GetBitContext gb;
90
    int bit_len;
91
    int x, y, len, color;
40 92
    uint8_t *d;
41 93

  
42
    nibble_end = buf_size * 2;
94
    bit_len = (buf_size - start) * 8;
95
    init_get_bits(&gb, buf + start, bit_len);
96

  
43 97
    x = 0;
44 98
    y = 0;
45 99
    d = bitmap;
46 100
    for(;;) {
47
        if (nibble_offset >= nibble_end)
101
        if (get_bits_count(&gb) > bit_len)
48 102
            return -1;
49
        v = get_nibble(buf, nibble_offset++);
50
        if (v < 0x4) {
51
            v = (v << 4) | get_nibble(buf, nibble_offset++);
52
            if (v < 0x10) {
53
                v = (v << 4) | get_nibble(buf, nibble_offset++);
54
                if (v < 0x040) {
55
                    v = (v << 4) | get_nibble(buf, nibble_offset++);
56
                    if (v < 4) {
57
                        v |= (w - x) << 2;
58
                    }
59
                }
60
            }
61
        }
62
        len = v >> 2;
63
        if (len > (w - x))
64
            len = (w - x);
65
        color = v & 0x03;
103
        if (is_8bit)
104
            len = decode_run_8bit(&gb, &color);
105
        else
106
            len = decode_run_2bit(&gb, &color);
107
        len = FFMIN(len, w - x);
66 108
        memset(d + x, color, len);
67 109
        x += len;
68 110
        if (x >= w) {
......
72 114
            d += linesize;
73 115
            x = 0;
74 116
            /* byte align */
75
            nibble_offset += (nibble_offset & 1);
117
            align_get_bits(&gb);
76 118
        }
77 119
    }
78 120
    return 0;
......
121 163
    }
122 164
}
123 165

  
166
#define READ_OFFSET(a) (big_offsets ? AV_RB32(a) : AV_RB16(a))
167

  
124 168
static int decode_dvd_subtitles(AVSubtitle *sub_header,
125 169
                                const uint8_t *buf, int buf_size)
126 170
{
127 171
    int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos;
128
    uint8_t colormap[4], alpha[4];
172
    int big_offsets, offset_size, is_8bit = 0;
173
    const uint8_t *yuv_palette = 0;
174
    uint8_t colormap[4], alpha[256];
129 175
    int date;
130 176
    int i;
131 177
    int is_menu = 0;
132 178

  
133
    if (buf_size < 4)
179
    if (buf_size < 10)
134 180
        return -1;
135 181
    sub_header->rects = NULL;
136 182
    sub_header->num_rects = 0;
137 183
    sub_header->start_display_time = 0;
138 184
    sub_header->end_display_time = 0;
139 185

  
140
    cmd_pos = AV_RB16(buf + 2);
141
    while ((cmd_pos + 4) < buf_size) {
186
    if (AV_RB16(buf) == 0) {   /* HD subpicture with 4-byte offsets */
187
        big_offsets = 1;
188
        offset_size = 4;
189
        cmd_pos = 6;
190
    } else {
191
        big_offsets = 0;
192
        offset_size = 2;
193
        cmd_pos = 2;
194
    }
195

  
196
    cmd_pos = READ_OFFSET(buf + cmd_pos);
197

  
198
    while ((cmd_pos + 2 + offset_size) < buf_size) {
142 199
        date = AV_RB16(buf + cmd_pos);
143
        next_cmd_pos = AV_RB16(buf + cmd_pos + 2);
200
        next_cmd_pos = READ_OFFSET(buf + cmd_pos + 2);
144 201
#ifdef DEBUG
145 202
        av_log(NULL, AV_LOG_INFO, "cmd_pos=0x%04x next=0x%04x date=%d\n",
146 203
               cmd_pos, next_cmd_pos, date);
147 204
#endif
148
        pos = cmd_pos + 4;
205
        pos = cmd_pos + 2 + offset_size;
149 206
        offset1 = -1;
150 207
        offset2 = -1;
151 208
        x1 = y1 = x2 = y2 = 0;
......
191 248
#endif
192 249
                break;
193 250
            case 0x05:
251
            case 0x85:
194 252
                if ((buf_size - pos) < 6)
195 253
                    goto fail;
196 254
                x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
197 255
                x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
198 256
                y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
199 257
                y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
258
                if (cmd & 0x80)
259
                    is_8bit = 1;
200 260
#ifdef DEBUG
201 261
                av_log(NULL, AV_LOG_INFO, "x1=%d x2=%d y1=%d y2=%d\n",
202 262
                       x1, x2, y1, y2);
......
213 273
#endif
214 274
                pos += 4;
215 275
                break;
276
            case 0x86:
277
                if ((buf_size - pos) < 8)
278
                    goto fail;
279
                offset1 = AV_RB32(buf + pos);
280
                offset2 = AV_RB32(buf + pos + 4);
281
#ifdef DEBUG
282
                av_log(NULL, AV_LOG_INFO, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
283
#endif
284
                pos += 8;
285
                break;
286

  
287
            case 0x83:
288
                /* HD set palette */
289
                if ((buf_size - pos) < 768)
290
                    goto fail;
291
                yuv_palette = buf + pos;
292
                pos += 768;
293
                break;
294
            case 0x84:
295
                /* HD set contrast (alpha) */
296
                if ((buf_size - pos) < 256)
297
                    goto fail;
298
                for (i = 0; i < 256; i++)
299
                    alpha[i] = 0xFF - buf[pos+i];
300
                pos += 256;
301
                break;
302

  
216 303
            case 0xff:
304
                goto the_end;
217 305
            default:
306
#ifdef DEBUG
307
                av_log(NULL, AV_LOG_INFO, "unrecognised subpicture command 0x%x\n", cmd);
308
#endif
218 309
                goto the_end;
219 310
            }
220 311
        }
......
243 334
                bitmap = av_malloc(w * h);
244 335
                sub_header->rects = av_mallocz(sizeof(AVSubtitleRect));
245 336
                sub_header->num_rects = 1;
246
                sub_header->rects[0].rgba_palette = av_malloc(4 * 4);
337
                sub_header->rects[0].bitmap = bitmap;
247 338
                decode_rle(bitmap, w * 2, w, (h + 1) / 2,
248
                           buf, offset1 * 2, buf_size);
339
                           buf, offset1, buf_size, is_8bit);
249 340
                decode_rle(bitmap + w, w * 2, w, h / 2,
250
                           buf, offset2 * 2, buf_size);
341
                           buf, offset2, buf_size, is_8bit);
342
                if (is_8bit) {
343
                    if (yuv_palette == 0)
344
                        goto fail;
345
                    sub_header->rects[0].rgba_palette = av_malloc(256 * 4);
346
                    sub_header->rects[0].nb_colors = 256;
347
                    yuv_a_to_rgba(yuv_palette, alpha, sub_header->rects[0].rgba_palette, 256);
348
                } else {
349
                    sub_header->rects[0].rgba_palette = av_malloc(4 * 4);
350
                    sub_header->rects[0].nb_colors = 4;
251 351
                guess_palette(sub_header->rects[0].rgba_palette,
252 352
                              colormap, alpha, 0xffff00);
353
                }
253 354
                sub_header->rects[0].x = x1;
254 355
                sub_header->rects[0].y = y1;
255 356
                sub_header->rects[0].w = w;
256 357
                sub_header->rects[0].h = h;
257
                sub_header->rects[0].nb_colors = 4;
258 358
                sub_header->rects[0].linesize = w;
259
                sub_header->rects[0].bitmap = bitmap;
260 359
            }
261 360
        }
262 361
        if (next_cmd_pos == cmd_pos)
......
266 365
    if (sub_header->num_rects > 0)
267 366
        return is_menu;
268 367
 fail:
368
    if (sub_header->rects != NULL) {
369
        for (i = 0; i < sub_header->num_rects; i++) {
370
            av_free(sub_header->rects[i].bitmap);
371
            av_free(sub_header->rects[i].rgba_palette);
372
        }
373
        av_freep(&sub_header->rects);
374
        sub_header->num_rects = 0;
375
    }
269 376
    return -1;
270 377
}
271 378

  

Also available in: Unified diff