1/*
2 * XSUB subtitle decoder
3 * Copyright (c) 2007 Reimar D��ffinger
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 "libavutil/mathematics.h"
23#include "libavutil/imgutils.h"
24#include "avcodec.h"
25#include "get_bits.h"
26#include "bytestream.h"
27
28static av_cold int decode_init(AVCodecContext *avctx) {
29    avctx->pix_fmt = AV_PIX_FMT_PAL8;
30    return 0;
31}
32
33static const uint8_t tc_offsets[9] = { 0, 1, 3, 4, 6, 7, 9, 10, 11 };
34static const uint8_t tc_muls[9] = { 10, 6, 10, 6, 10, 10, 10, 10, 1 };
35
36static int64_t parse_timecode(const uint8_t *buf, int64_t packet_time) {
37    int i;
38    int64_t ms = 0;
39    if (buf[2] != ':' || buf[5] != ':' || buf[8] != '.')
40        return AV_NOPTS_VALUE;
41    for (i = 0; i < sizeof(tc_offsets); i++) {
42        uint8_t c = buf[tc_offsets[i]] - '0';
43        if (c > 9) return AV_NOPTS_VALUE;
44        ms = (ms + c) * tc_muls[i];
45    }
46    return ms - packet_time;
47}
48
49static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
50                        AVPacket *avpkt) {
51    const uint8_t *buf = avpkt->data;
52    int buf_size = avpkt->size;
53    AVSubtitle *sub = data;
54    const uint8_t *buf_end = buf + buf_size;
55    uint8_t *bitmap;
56    int w, h, x, y, i;
57    int64_t packet_time = 0;
58    GetBitContext gb;
59    int has_alpha = avctx->codec_tag == MKTAG('D','X','S','A');
60
61    // check that at least header fits
62    if (buf_size < 27 + 7 * 2 + 4 * 3) {
63        av_log(avctx, AV_LOG_ERROR, "coded frame size %d too small\n", buf_size);
64        return -1;
65    }
66
67    // read start and end time
68    if (buf[0] != '[' || buf[13] != '-' || buf[26] != ']') {
69        av_log(avctx, AV_LOG_ERROR, "invalid time code\n");
70        return -1;
71    }
72    if (avpkt->pts != AV_NOPTS_VALUE)
73        packet_time = av_rescale_q(avpkt->pts, AV_TIME_BASE_Q, (AVRational){1, 1000});
74    sub->start_display_time = parse_timecode(buf +  1, packet_time);
75    sub->end_display_time   = parse_timecode(buf + 14, packet_time);
76    buf += 27;
77
78    // read header
79    w = bytestream_get_le16(&buf);
80    h = bytestream_get_le16(&buf);
81    if (av_image_check_size(w, h, 0, avctx) < 0)
82        return -1;
83    x = bytestream_get_le16(&buf);
84    y = bytestream_get_le16(&buf);
85    // skip bottom right position, it gives no new information
86    bytestream_get_le16(&buf);
87    bytestream_get_le16(&buf);
88    // The following value is supposed to indicate the start offset
89    // (relative to the palette) of the data for the second field,
90    // however there are files in which it has a bogus value and thus
91    // we just ignore it
92    bytestream_get_le16(&buf);
93
94    // allocate sub and set values
95    sub->rects =  av_mallocz(sizeof(*sub->rects));
96    sub->rects[0] = av_mallocz(sizeof(*sub->rects[0]));
97    sub->num_rects = 1;
98    sub->rects[0]->x = x; sub->rects[0]->y = y;
99    sub->rects[0]->w = w; sub->rects[0]->h = h;
100    sub->rects[0]->type = SUBTITLE_BITMAP;
101    sub->rects[0]->pict.linesize[0] = w;
102    sub->rects[0]->pict.data[0] = av_malloc(w * h);
103    sub->rects[0]->nb_colors = 4;
104    sub->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
105
106    // read palette
107    for (i = 0; i < sub->rects[0]->nb_colors; i++)
108        ((uint32_t*)sub->rects[0]->pict.data[1])[i] = bytestream_get_be24(&buf);
109    // make all except background (first entry) non-transparent
110    for (i = 0; i < sub->rects[0]->nb_colors; i++)
111        ((uint32_t*)sub->rects[0]->pict.data[1])[i] |= (has_alpha ? *buf++ : (i ? 0xff : 0)) << 24;
112
113    // process RLE-compressed data
114    init_get_bits(&gb, buf, (buf_end - buf) * 8);
115    bitmap = sub->rects[0]->pict.data[0];
116    for (y = 0; y < h; y++) {
117        // interlaced: do odd lines
118        if (y == (h + 1) / 2) bitmap = sub->rects[0]->pict.data[0] + w;
119        for (x = 0; x < w; ) {
120            int log2 = ff_log2_tab[show_bits(&gb, 8)];
121            int run = get_bits(&gb, 14 - 4 * (log2 >> 1));
122            int color = get_bits(&gb, 2);
123            run = FFMIN(run, w - x);
124            // run length 0 means till end of row
125            if (!run) run = w - x;
126            memset(bitmap, color, run);
127            bitmap += run;
128            x += run;
129        }
130        // interlaced, skip every second line
131        bitmap += w;
132        align_get_bits(&gb);
133    }
134    *data_size = 1;
135    return buf_size;
136}
137
138AVCodec ff_xsub_decoder = {
139    .name      = "xsub",
140    .long_name = NULL_IF_CONFIG_SMALL("XSUB"),
141    .type      = AVMEDIA_TYPE_SUBTITLE,
142    .id        = AV_CODEC_ID_XSUB,
143    .init      = decode_init,
144    .decode    = decode_frame,
145};
146