libavcodec/xsubdec.c
Go to the documentation of this file.
00001 /*
00002  * XSUB subtitle decoder
00003  * Copyright (c) 2007 Reimar Döffinger
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include "libavutil/mathematics.h"
00023 #include "libavutil/imgutils.h"
00024 #include "avcodec.h"
00025 #include "get_bits.h"
00026 #include "bytestream.h"
00027 
00028 static av_cold int decode_init(AVCodecContext *avctx) {
00029     avctx->pix_fmt = PIX_FMT_PAL8;
00030     return 0;
00031 }
00032 
00033 static const uint8_t tc_offsets[9] = { 0, 1, 3, 4, 6, 7, 9, 10, 11 };
00034 static const uint8_t tc_muls[9] = { 10, 6, 10, 6, 10, 10, 10, 10, 1 };
00035 
00036 static int64_t parse_timecode(const uint8_t *buf, int64_t packet_time) {
00037     int i;
00038     int64_t ms = 0;
00039     if (buf[2] != ':' || buf[5] != ':' || buf[8] != '.')
00040         return AV_NOPTS_VALUE;
00041     for (i = 0; i < sizeof(tc_offsets); i++) {
00042         uint8_t c = buf[tc_offsets[i]] - '0';
00043         if (c > 9) return AV_NOPTS_VALUE;
00044         ms = (ms + c) * tc_muls[i];
00045     }
00046     return ms - packet_time;
00047 }
00048 
00049 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00050                         AVPacket *avpkt) {
00051     const uint8_t *buf = avpkt->data;
00052     int buf_size = avpkt->size;
00053     AVSubtitle *sub = data;
00054     const uint8_t *buf_end = buf + buf_size;
00055     uint8_t *bitmap;
00056     int w, h, x, y, i;
00057     int64_t packet_time = 0;
00058     GetBitContext gb;
00059     int has_alpha = avctx->codec_tag == MKTAG('D','X','S','A');
00060 
00061     // check that at least header fits
00062     if (buf_size < 27 + 7 * 2 + 4 * 3) {
00063         av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
00064         return -1;
00065     }
00066 
00067     // read start and end time
00068     if (buf[0] != '[' || buf[13] != '-' || buf[26] != ']') {
00069         av_log(avctx, AV_LOG_ERROR, "invalid time code\n");
00070         return -1;
00071     }
00072     if (avpkt->pts != AV_NOPTS_VALUE)
00073         packet_time = av_rescale_q(avpkt->pts, AV_TIME_BASE_Q, (AVRational){1, 1000});
00074     sub->start_display_time = parse_timecode(buf +  1, packet_time);
00075     sub->end_display_time   = parse_timecode(buf + 14, packet_time);
00076     buf += 27;
00077 
00078     // read header
00079     w = bytestream_get_le16(&buf);
00080     h = bytestream_get_le16(&buf);
00081     if (av_image_check_size(w, h, 0, avctx) < 0)
00082         return -1;
00083     x = bytestream_get_le16(&buf);
00084     y = bytestream_get_le16(&buf);
00085     // skip bottom right position, it gives no new information
00086     bytestream_get_le16(&buf);
00087     bytestream_get_le16(&buf);
00088     // The following value is supposed to indicate the start offset
00089     // (relative to the palette) of the data for the second field,
00090     // however there are files  where it has a bogus value and thus
00091     // we just ignore it
00092     bytestream_get_le16(&buf);
00093 
00094     // allocate sub and set values
00095     sub->rects =  av_mallocz(sizeof(*sub->rects));
00096     sub->rects[0] = av_mallocz(sizeof(*sub->rects[0]));
00097     sub->num_rects = 1;
00098     sub->rects[0]->x = x; sub->rects[0]->y = y;
00099     sub->rects[0]->w = w; sub->rects[0]->h = h;
00100     sub->rects[0]->type = SUBTITLE_BITMAP;
00101     sub->rects[0]->pict.linesize[0] = w;
00102     sub->rects[0]->pict.data[0] = av_malloc(w * h);
00103     sub->rects[0]->nb_colors = 4;
00104     sub->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
00105 
00106     // read palette
00107     for (i = 0; i < sub->rects[0]->nb_colors; i++)
00108         ((uint32_t*)sub->rects[0]->pict.data[1])[i] = bytestream_get_be24(&buf);
00109     // make all except background (first entry) non-transparent
00110     for (i = 0; i < sub->rects[0]->nb_colors; i++)
00111         ((uint32_t*)sub->rects[0]->pict.data[1])[i] |= (has_alpha ? *buf++ : (i ? 0xff : 0)) << 24;
00112 
00113     // process RLE-compressed data
00114     init_get_bits(&gb, buf, (buf_end - buf) * 8);
00115     bitmap = sub->rects[0]->pict.data[0];
00116     for (y = 0; y < h; y++) {
00117         // interlaced: do odd lines
00118         if (y == (h + 1) / 2) bitmap = sub->rects[0]->pict.data[0] + w;
00119         for (x = 0; x < w; ) {
00120             int log2 = ff_log2_tab[show_bits(&gb, 8)];
00121             int run = get_bits(&gb, 14 - 4 * (log2 >> 1));
00122             int color = get_bits(&gb, 2);
00123             run = FFMIN(run, w - x);
00124             // run length 0 means till end of row
00125             if (!run) run = w - x;
00126             memset(bitmap, color, run);
00127             bitmap += run;
00128             x += run;
00129         }
00130         // interlaced, skip every second line
00131         bitmap += w;
00132         align_get_bits(&gb);
00133     }
00134     *data_size = 1;
00135     return buf_size;
00136 }
00137 
00138 AVCodec ff_xsub_decoder = {
00139     .name      = "xsub",
00140     .type      = AVMEDIA_TYPE_SUBTITLE,
00141     .id        = CODEC_ID_XSUB,
00142     .init      = decode_init,
00143     .decode    = decode_frame,
00144     .long_name = NULL_IF_CONFIG_SMALL("XSUB"),
00145 };