xsubdec.c 4.5 KB
Newer Older
R
Reimar Döffinger 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * XSUB subtitle decoder
 * Copyright (c) 2007 Reimar Döffinger
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */
21 22 23 24
#include "avcodec.h"
#include "bitstream.h"
#include "bytestream.h"

25
static av_cold int decode_init(AVCodecContext *avctx) {
26 27 28 29
    avctx->pix_fmt = PIX_FMT_PAL8;
    return 0;
}

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

M
consts  
Michael Niedermayer 已提交
33
static uint64_t parse_timecode(const uint8_t *buf) {
34 35 36 37 38 39 40 41 42 43 44 45
    int i;
    int64_t ms = 0;
    if (buf[2] != ':' || buf[5] != ':' || buf[8] != '.')
        return AV_NOPTS_VALUE;
    for (i = 0; i < sizeof(tc_offsets); i++) {
        uint8_t c = buf[tc_offsets[i]] - '0';
        if (c > 9) return AV_NOPTS_VALUE;
        ms = (ms + c) * tc_muls[i];
    }
    return ms;
}

46
static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
M
consts  
Michael Niedermayer 已提交
47
                        const uint8_t *buf, int buf_size) {
48
    AVSubtitle *sub = data;
M
consts  
Michael Niedermayer 已提交
49
    const uint8_t *buf_end = buf + buf_size;
50 51 52 53 54 55 56 57 58 59
    uint8_t *bitmap;
    int w, h, x, y, rlelen, i;
    GetBitContext gb;

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

60 61 62 63 64
    // read start and end time
    if (buf[0] != '[' || buf[13] != '-' || buf[26] != ']') {
        av_log(avctx, AV_LOG_ERROR, "invalid time code\n");
        return -1;
    }
65 66
    sub->start_display_time = parse_timecode(buf +  1);
    sub->end_display_time   = parse_timecode(buf + 14);
67 68
    buf += 27;

69 70 71 72 73 74 75 76 77 78 79 80 81 82
    // read header
    w = bytestream_get_le16(&buf);
    h = bytestream_get_le16(&buf);
    if (avcodec_check_dimensions(avctx, w, h) < 0)
        return -1;
    x = bytestream_get_le16(&buf);
    y = bytestream_get_le16(&buf);
    // skip bottom right position, it gives no new information
    bytestream_get_le16(&buf);
    bytestream_get_le16(&buf);
    rlelen = bytestream_get_le16(&buf);

    // allocate sub and set values
    if (!sub->rects) {
83 84
        sub->rects =  av_mallocz(sizeof(*sub->rects));
        sub->rects[0] = av_mallocz(sizeof(*sub->rects[0]));
85 86
        sub->num_rects = 1;
    }
87
    av_freep(&sub->rects[0]->pict.data[0]);
88 89
    sub->rects[0]->x = x; sub->rects[0]->y = y;
    sub->rects[0]->w = w; sub->rects[0]->h = h;
90 91
    sub->rects[0]->pict.linesize[0] = w;
    sub->rects[0]->pict.data[0] = av_malloc(w * h);
92
    sub->rects[0]->nb_colors = 4;
93
    sub->rects[0]->pict.data[1] = av_malloc(sub->rects[0]->nb_colors * 4);
94 95

    // read palette
96
    for (i = 0; i < sub->rects[0]->nb_colors; i++)
97
        ((uint32_t*)sub->rects[0]->pict.data[1])[i] = bytestream_get_be24(&buf);
98
    // make all except background (first entry) non-transparent
99
    for (i = 1; i < sub->rects[0]->nb_colors; i++)
100
        ((uint32_t*)sub->rects[0]->pict.data[1])[i] |= 0xff000000;
101 102 103 104

    // process RLE-compressed data
    rlelen = FFMIN(rlelen, buf_end - buf);
    init_get_bits(&gb, buf, rlelen * 8);
105
    bitmap = sub->rects[0]->pict.data[0];
106
    for (y = 0; y < h; y++) {
R
Reimar Döffinger 已提交
107
        // interlaced: do odd lines
108
        if (y == (h + 1) / 2) bitmap = sub->rects[0]->pict.data[0] + w;
109 110
        for (x = 0; x < w; ) {
            int log2 = ff_log2_tab[show_bits(&gb, 8)];
111
            int run = get_bits(&gb, 14 - 4 * (log2 >> 1));
D
Diego Biurrun 已提交
112
            int color = get_bits(&gb, 2);
113 114 115
            run = FFMIN(run, w - x);
            // run length 0 means till end of row
            if (!run) run = w - x;
D
Diego Biurrun 已提交
116
            memset(bitmap, color, run);
117 118 119
            bitmap += run;
            x += run;
        }
R
Reimar Döffinger 已提交
120 121
        // interlaced, skip every second line
        bitmap += w;
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
        align_get_bits(&gb);
    }
    *data_size = 1;
    return buf_size;
}

AVCodec xsub_decoder = {
    "xsub",
    CODEC_TYPE_SUBTITLE,
    CODEC_ID_XSUB,
    0,
    decode_init,
    NULL,
    NULL,
    decode_frame,
137
    .long_name = NULL_IF_CONFIG_SMALL("XSUB"),
138
};