Statistics
| Branch: | Revision:

ffmpeg / libavcodec / xsubdec.c @ 59f6f64e

History | View | Annotate | Download (3.32 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
    ms = av_rescale_q(ms, (AVRational){1, 1000}, avctx->time_base);
24
    return ms;
25
}
26

    
27
static const uint8_t runbits[8] = { 2, 2, 6, 6, 10, 10, 14, 14 };
28

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

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

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

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

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

    
77
    // read palette
78
    for (i = 0; i < sub->rects[0].nb_colors; i++)
79
        sub->rects[0].rgba_palette[i] = bytestream_get_be24(&buf);
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
        for (x = 0; x < w; ) {
87
            int log2 = ff_log2_tab[show_bits(&gb, 8)];
88
            int run = get_bits(&gb, runbits[log2]);
89
            int colour = get_bits(&gb, 2);
90
            run = FFMIN(run, w - x);
91
            // run length 0 means till end of row
92
            if (!run) run = w - x;
93
            memset(bitmap, colour, run);
94
            bitmap += run;
95
            x += run;
96
        }
97
        align_get_bits(&gb);
98
    }
99
    *data_size = 1;
100
    return buf_size;
101
}
102

    
103
AVCodec xsub_decoder = {
104
    "xsub",
105
    CODEC_TYPE_SUBTITLE,
106
    CODEC_ID_XSUB,
107
    0,
108
    decode_init,
109
    NULL,
110
    NULL,
111
    decode_frame,
112
};