66281658 |
/*
* 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
*/ |
0ebcdf5c |
#include "libavutil/mathematics.h" |
7ffe76e5 |
#include "libavutil/imgutils.h" |
7e2643ae |
#include "avcodec.h" |
9106a698 |
#include "get_bits.h" |
7e2643ae |
#include "bytestream.h"
|
98a6fff9 |
static av_cold int decode_init(AVCodecContext *avctx) { |
716d413c |
avctx->pix_fmt = AV_PIX_FMT_PAL8; |
7e2643ae |
return 0;
}
|
59f6f64e |
static const uint8_t tc_offsets[9] = { 0, 1, 3, 4, 6, 7, 9, 10, 11 }; |
ffe2c09d |
static const uint8_t tc_muls[9] = { 10, 6, 10, 6, 10, 10, 10, 10, 1 }; |
59f6f64e |
|
cea0b527 |
static int64_t parse_timecode(const uint8_t *buf, int64_t packet_time) { |
59f6f64e |
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];
} |
cea0b527 |
return ms - packet_time; |
59f6f64e |
}
|
7e2643ae |
static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, |
7a00bbad |
AVPacket *avpkt) {
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size; |
7e2643ae |
AVSubtitle *sub = data; |
7993df65 |
const uint8_t *buf_end = buf + buf_size; |
7e2643ae |
uint8_t *bitmap; |
7ecdc03e |
int w, h, x, y, i, ret; |
cea0b527 |
int64_t packet_time = 0; |
7e2643ae |
GetBitContext gb; |
d6f910ea |
int has_alpha = avctx->codec_tag == MKTAG('D','X','S','A'); |
7e2643ae |
// check that at least header fits |
5a1addd7 |
if (buf_size < 27 + 7 * 2 + 4 * (3 + has_alpha)) { |
3cb83b22 |
av_log(avctx, AV_LOG_ERROR, "coded frame size %d too small\n", buf_size); |
7e2643ae |
return -1;
}
|
59f6f64e |
// read start and end time
if (buf[0] != '[' || buf[13] != '-' || buf[26] != ']') {
av_log(avctx, AV_LOG_ERROR, "invalid time code\n");
return -1;
} |
cea0b527 |
if (avpkt->pts != AV_NOPTS_VALUE)
packet_time = av_rescale_q(avpkt->pts, AV_TIME_BASE_Q, (AVRational){1, 1000});
sub->start_display_time = parse_timecode(buf + 1, packet_time);
sub->end_display_time = parse_timecode(buf + 14, packet_time); |
59f6f64e |
buf += 27;
|
7e2643ae |
// read header
w = bytestream_get_le16(&buf);
h = bytestream_get_le16(&buf); |
e16f217c |
if (av_image_check_size(w, h, 0, avctx) < 0) |
7e2643ae |
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); |
3b4621ac |
// The following value is supposed to indicate the start offset
// (relative to the palette) of the data for the second field, |
92d2b909 |
// however there are files in which it has a bogus value and thus |
3b4621ac |
// we just ignore it
bytestream_get_le16(&buf); |
7e2643ae |
|
2002436b |
if (buf_end - buf < h + 3*4)
return AVERROR_INVALIDDATA;
|
7e2643ae |
// allocate sub and set values |
4a3190ed |
sub->rects = av_mallocz(sizeof(*sub->rects)); |
f9a5a89b |
if (!sub->rects)
return AVERROR(ENOMEM);
|
4a3190ed |
sub->rects[0] = av_mallocz(sizeof(*sub->rects[0])); |
f9a5a89b |
if (!sub->rects[0]) {
av_freep(&sub->rects);
return AVERROR(ENOMEM);
} |
db4fac64 |
sub->rects[0]->x = x; sub->rects[0]->y = y;
sub->rects[0]->w = w; sub->rects[0]->h = h; |
6dc13ccb |
sub->rects[0]->type = SUBTITLE_BITMAP; |
a17a7661 |
sub->rects[0]->linesize[0] = w;
sub->rects[0]->data[0] = av_malloc(w * h); |
db4fac64 |
sub->rects[0]->nb_colors = 4; |
a17a7661 |
sub->rects[0]->data[1] = av_mallocz(AVPALETTE_SIZE);
if (!sub->rects[0]->data[0] || !sub->rects[0]->data[1]) {
av_freep(&sub->rects[0]->data[1]);
av_freep(&sub->rects[0]->data[0]); |
f9a5a89b |
av_freep(&sub->rects[0]);
av_freep(&sub->rects);
return AVERROR(ENOMEM);
}
sub->num_rects = 1; |
7e2643ae |
// read palette |
db4fac64 |
for (i = 0; i < sub->rects[0]->nb_colors; i++) |
a17a7661 |
((uint32_t*)sub->rects[0]->data[1])[i] = bytestream_get_be24(&buf); |
5a1addd7 |
if (!has_alpha) {
// make all except background (first entry) non-transparent
for (i = 1; i < sub->rects[0]->nb_colors; i++) |
a17a7661 |
((uint32_t *)sub->rects[0]->data[1])[i] |= 0xff000000; |
5a1addd7 |
} else {
for (i = 0; i < sub->rects[0]->nb_colors; i++) |
a17a7661 |
((uint32_t *)sub->rects[0]->data[1])[i] |= *buf++ << 24; |
5a1addd7 |
} |
7e2643ae |
|
a17a7661 |
#if FF_API_AVPICTURE
FF_DISABLE_DEPRECATION_WARNINGS |
b7e64fba |
{
AVSubtitleRect *rect;
int j; |
a17a7661 |
rect = sub->rects[0];
for (j = 0; j < 4; j++) {
rect->pict.data[j] = rect->data[j];
rect->pict.linesize[j] = rect->linesize[j];
} |
b7e64fba |
} |
a17a7661 |
FF_ENABLE_DEPRECATION_WARNINGS
#endif
|
7e2643ae |
// process RLE-compressed data |
7ecdc03e |
if ((ret = init_get_bits8(&gb, buf, buf_end - buf)) < 0)
return ret; |
a17a7661 |
bitmap = sub->rects[0]->data[0]; |
7e2643ae |
for (y = 0; y < h; y++) { |
3c247b4f |
// interlaced: do odd lines |
a17a7661 |
if (y == (h + 1) / 2) bitmap = sub->rects[0]->data[0] + w; |
7e2643ae |
for (x = 0; x < w; ) {
int log2 = ff_log2_tab[show_bits(&gb, 8)]; |
d6136c76 |
int run = get_bits(&gb, 14 - 4 * (log2 >> 1)); |
cedb83a6 |
int color = get_bits(&gb, 2); |
7e2643ae |
run = FFMIN(run, w - x);
// run length 0 means till end of row
if (!run) run = w - x; |
cedb83a6 |
memset(bitmap, color, run); |
7e2643ae |
bitmap += run;
x += run;
} |
3c247b4f |
// interlaced, skip every second line
bitmap += w; |
7e2643ae |
align_get_bits(&gb);
}
*data_size = 1;
return buf_size;
}
|
e7e2df27 |
AVCodec ff_xsub_decoder = { |
933a6fd5 |
.name = "xsub", |
b2bed932 |
.long_name = NULL_IF_CONFIG_SMALL("XSUB"), |
933a6fd5 |
.type = AVMEDIA_TYPE_SUBTITLE, |
36ef5369 |
.id = AV_CODEC_ID_XSUB, |
933a6fd5 |
.init = decode_init,
.decode = decode_frame, |
7e2643ae |
}; |