• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

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 #include "avcodec.h"
00022 #include "get_bits.h"
00023 #include "bytestream.h"
00024 
00025 static av_cold int decode_init(AVCodecContext *avctx) {
00026     avctx->pix_fmt = PIX_FMT_PAL8;
00027     return 0;
00028 }
00029 
00030 static const uint8_t tc_offsets[9] = { 0, 1, 3, 4, 6, 7, 9, 10, 11 };
00031 static const uint8_t tc_muls[9] = { 10, 6, 10, 6, 10, 10, 10, 10, 1 };
00032 
00033 static int64_t parse_timecode(const uint8_t *buf, int64_t packet_time) {
00034     int i;
00035     int64_t ms = 0;
00036     if (buf[2] != ':' || buf[5] != ':' || buf[8] != '.')
00037         return AV_NOPTS_VALUE;
00038     for (i = 0; i < sizeof(tc_offsets); i++) {
00039         uint8_t c = buf[tc_offsets[i]] - '0';
00040         if (c > 9) return AV_NOPTS_VALUE;
00041         ms = (ms + c) * tc_muls[i];
00042     }
00043     return ms - packet_time;
00044 }
00045 
00046 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00047                         AVPacket *avpkt) {
00048     const uint8_t *buf = avpkt->data;
00049     int buf_size = avpkt->size;
00050     AVSubtitle *sub = data;
00051     const uint8_t *buf_end = buf + buf_size;
00052     uint8_t *bitmap;
00053     int w, h, x, y, rlelen, i;
00054     int64_t packet_time = 0;
00055     GetBitContext gb;
00056 
00057     memset(sub, 0, sizeof(*sub));
00058 
00059     // check that at least header fits
00060     if (buf_size < 27 + 7 * 2 + 4 * 3) {
00061         av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
00062         return -1;
00063     }
00064 
00065     // read start and end time
00066     if (buf[0] != '[' || buf[13] != '-' || buf[26] != ']') {
00067         av_log(avctx, AV_LOG_ERROR, "invalid time code\n");
00068         return -1;
00069     }
00070     if (avpkt->pts != AV_NOPTS_VALUE)
00071         packet_time = av_rescale_q(avpkt->pts, AV_TIME_BASE_Q, (AVRational){1, 1000});
00072     sub->start_display_time = parse_timecode(buf +  1, packet_time);
00073     sub->end_display_time   = parse_timecode(buf + 14, packet_time);
00074     buf += 27;
00075 
00076     // read header
00077     w = bytestream_get_le16(&buf);
00078     h = bytestream_get_le16(&buf);
00079     if (avcodec_check_dimensions(avctx, w, h) < 0)
00080         return -1;
00081     x = bytestream_get_le16(&buf);
00082     y = bytestream_get_le16(&buf);
00083     // skip bottom right position, it gives no new information
00084     bytestream_get_le16(&buf);
00085     bytestream_get_le16(&buf);
00086     rlelen = bytestream_get_le16(&buf);
00087 
00088     // allocate sub and set values
00089     sub->rects =  av_mallocz(sizeof(*sub->rects));
00090     sub->rects[0] = av_mallocz(sizeof(*sub->rects[0]));
00091     sub->num_rects = 1;
00092     sub->rects[0]->x = x; sub->rects[0]->y = y;
00093     sub->rects[0]->w = w; sub->rects[0]->h = h;
00094     sub->rects[0]->type = SUBTITLE_BITMAP;
00095     sub->rects[0]->pict.linesize[0] = w;
00096     sub->rects[0]->pict.data[0] = av_malloc(w * h);
00097     sub->rects[0]->nb_colors = 4;
00098     sub->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
00099 
00100     // read palette
00101     for (i = 0; i < sub->rects[0]->nb_colors; i++)
00102         ((uint32_t*)sub->rects[0]->pict.data[1])[i] = bytestream_get_be24(&buf);
00103     // make all except background (first entry) non-transparent
00104     for (i = 1; i < sub->rects[0]->nb_colors; i++)
00105         ((uint32_t*)sub->rects[0]->pict.data[1])[i] |= 0xff000000;
00106 
00107     // process RLE-compressed data
00108     rlelen = FFMIN(rlelen, buf_end - buf);
00109     init_get_bits(&gb, buf, rlelen * 8);
00110     bitmap = sub->rects[0]->pict.data[0];
00111     for (y = 0; y < h; y++) {
00112         // interlaced: do odd lines
00113         if (y == (h + 1) / 2) bitmap = sub->rects[0]->pict.data[0] + w;
00114         for (x = 0; x < w; ) {
00115             int log2 = ff_log2_tab[show_bits(&gb, 8)];
00116             int run = get_bits(&gb, 14 - 4 * (log2 >> 1));
00117             int color = get_bits(&gb, 2);
00118             run = FFMIN(run, w - x);
00119             // run length 0 means till end of row
00120             if (!run) run = w - x;
00121             memset(bitmap, color, run);
00122             bitmap += run;
00123             x += run;
00124         }
00125         // interlaced, skip every second line
00126         bitmap += w;
00127         align_get_bits(&gb);
00128     }
00129     *data_size = 1;
00130     return buf_size;
00131 }
00132 
00133 AVCodec xsub_decoder = {
00134     "xsub",
00135     AVMEDIA_TYPE_SUBTITLE,
00136     CODEC_ID_XSUB,
00137     0,
00138     decode_init,
00139     NULL,
00140     NULL,
00141     decode_frame,
00142     .long_name = NULL_IF_CONFIG_SMALL("XSUB"),
00143 };

Generated on Fri Sep 16 2011 17:17:47 for FFmpeg by  doxygen 1.7.1