Statistics
| Branch: | Revision:

ffmpeg / libavcodec / xsubdec.c @ e3c90129

History | View | Annotate | Download (3.52 KB)

1
#include "avcodec.h"
2
#include "bitstream.h"
3
#include "bytestream.h"
4

    
5
static int decode_init(AVCodecContext *avctx) {
6
    avctx->pix_fmt = PIX_FMT_PAL8;
7
    return 0;
8
}
9

    
10
static const uint8_t tc_offsets[9] = { 0, 1, 3, 4, 6, 7, 9, 10, 11 };
11
static const uint8_t tc_muls[9] = { 10, 6, 10, 6, 10, 6, 10, 10, 1 };
12

    
13
static uint64_t parse_timecode(AVCodecContext *avctx, uint8_t *buf) {
14
    int i;
15
    int64_t ms = 0;
16
    if (buf[2] != ':' || buf[5] != ':' || buf[8] != '.')
17
        return AV_NOPTS_VALUE;
18
    for (i = 0; i < sizeof(tc_offsets); i++) {
19
        uint8_t c = buf[tc_offsets[i]] - '0';
20
        if (c > 9) return AV_NOPTS_VALUE;
21
        ms = (ms + c) * tc_muls[i];
22
    }
23
    return ms;
24
}
25

    
26
static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
27
                        uint8_t *buf, int buf_size) {
28
    AVSubtitle *sub = data;
29
    uint8_t *buf_end = buf + buf_size;
30
    uint8_t *bitmap;
31
    int w, h, x, y, rlelen, i;
32
    GetBitContext gb;
33

    
34
    // check that at least header fits
35
    if (buf_size < 27 + 7 * 2 + 4 * 3) {
36
        av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
37
        return -1;
38
    }
39

    
40
    // read start and end time
41
    if (buf[0] != '[' || buf[13] != '-' || buf[26] != ']') {
42
        av_log(avctx, AV_LOG_ERROR, "invalid time code\n");
43
        return -1;
44
    }
45
    sub->start_display_time = parse_timecode(avctx, buf +  1);
46
    sub->end_display_time   = parse_timecode(avctx, buf + 14);
47
    buf += 27;
48

    
49
    // read header
50
    w = bytestream_get_le16(&buf);
51
    h = bytestream_get_le16(&buf);
52
    if (avcodec_check_dimensions(avctx, w, h) < 0)
53
        return -1;
54
    x = bytestream_get_le16(&buf);
55
    y = bytestream_get_le16(&buf);
56
    // skip bottom right position, it gives no new information
57
    bytestream_get_le16(&buf);
58
    bytestream_get_le16(&buf);
59
    rlelen = bytestream_get_le16(&buf);
60

    
61
    // allocate sub and set values
62
    if (!sub->rects) {
63
        sub->rects = av_mallocz(sizeof(AVSubtitleRect));
64
        sub->num_rects = 1;
65
    }
66
    av_freep(&sub->rects[0].bitmap);
67
    sub->rects[0].x = x; sub->rects[0].y = y;
68
    sub->rects[0].w = w; sub->rects[0].h = h;
69
    sub->rects[0].linesize = w;
70
    sub->rects[0].bitmap = av_malloc(w * h);
71
    sub->rects[0].nb_colors = 4;
72
    sub->rects[0].rgba_palette = av_malloc(sub->rects[0].nb_colors * 4);
73

    
74
    // read palette
75
    for (i = 0; i < sub->rects[0].nb_colors; i++)
76
        sub->rects[0].rgba_palette[i] = bytestream_get_be24(&buf);
77
    // make all except background (first entry) non-transparent
78
    for (i = 1; i < sub->rects[0].nb_colors; i++)
79
        sub->rects[0].rgba_palette[i] |= 0xff000000;
80

    
81
    // process RLE-compressed data
82
    rlelen = FFMIN(rlelen, buf_end - buf);
83
    init_get_bits(&gb, buf, rlelen * 8);
84
    bitmap = sub->rects[0].bitmap;
85
    for (y = 0; y < h; y++) {
86
        // interlaced: do odd lines
87
        if (y == (h + 1) / 2) bitmap = sub->rects[0].bitmap + w;
88
        for (x = 0; x < w; ) {
89
            int log2 = ff_log2_tab[show_bits(&gb, 8)];
90
            int run = get_bits(&gb, 14 - 4 * (log2 >> 1));
91
            int colour = get_bits(&gb, 2);
92
            run = FFMIN(run, w - x);
93
            // run length 0 means till end of row
94
            if (!run) run = w - x;
95
            memset(bitmap, colour, run);
96
            bitmap += run;
97
            x += run;
98
        }
99
        // interlaced, skip every second line
100
        bitmap += w;
101
        align_get_bits(&gb);
102
    }
103
    *data_size = 1;
104
    return buf_size;
105
}
106

    
107
AVCodec xsub_decoder = {
108
    "xsub",
109
    CODEC_TYPE_SUBTITLE,
110
    CODEC_ID_XSUB,
111
    0,
112
    decode_init,
113
    NULL,
114
    NULL,
115
    decode_frame,
116
};