annotate ffmpeg/libavcodec/xsubdec.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * XSUB subtitle decoder
yading@10 3 * Copyright (c) 2007 Reimar Döffinger
yading@10 4 *
yading@10 5 * This file is part of FFmpeg.
yading@10 6 *
yading@10 7 * FFmpeg is free software; you can redistribute it and/or
yading@10 8 * modify it under the terms of the GNU Lesser General Public
yading@10 9 * License as published by the Free Software Foundation; either
yading@10 10 * version 2.1 of the License, or (at your option) any later version.
yading@10 11 *
yading@10 12 * FFmpeg is distributed in the hope that it will be useful,
yading@10 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 15 * Lesser General Public License for more details.
yading@10 16 *
yading@10 17 * You should have received a copy of the GNU Lesser General Public
yading@10 18 * License along with FFmpeg; if not, write to the Free Software
yading@10 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 20 */
yading@10 21
yading@10 22 #include "libavutil/mathematics.h"
yading@10 23 #include "libavutil/imgutils.h"
yading@10 24 #include "avcodec.h"
yading@10 25 #include "get_bits.h"
yading@10 26 #include "bytestream.h"
yading@10 27
yading@10 28 static av_cold int decode_init(AVCodecContext *avctx) {
yading@10 29 avctx->pix_fmt = AV_PIX_FMT_PAL8;
yading@10 30 return 0;
yading@10 31 }
yading@10 32
yading@10 33 static const uint8_t tc_offsets[9] = { 0, 1, 3, 4, 6, 7, 9, 10, 11 };
yading@10 34 static const uint8_t tc_muls[9] = { 10, 6, 10, 6, 10, 10, 10, 10, 1 };
yading@10 35
yading@10 36 static int64_t parse_timecode(const uint8_t *buf, int64_t packet_time) {
yading@10 37 int i;
yading@10 38 int64_t ms = 0;
yading@10 39 if (buf[2] != ':' || buf[5] != ':' || buf[8] != '.')
yading@10 40 return AV_NOPTS_VALUE;
yading@10 41 for (i = 0; i < sizeof(tc_offsets); i++) {
yading@10 42 uint8_t c = buf[tc_offsets[i]] - '0';
yading@10 43 if (c > 9) return AV_NOPTS_VALUE;
yading@10 44 ms = (ms + c) * tc_muls[i];
yading@10 45 }
yading@10 46 return ms - packet_time;
yading@10 47 }
yading@10 48
yading@10 49 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
yading@10 50 AVPacket *avpkt) {
yading@10 51 const uint8_t *buf = avpkt->data;
yading@10 52 int buf_size = avpkt->size;
yading@10 53 AVSubtitle *sub = data;
yading@10 54 const uint8_t *buf_end = buf + buf_size;
yading@10 55 uint8_t *bitmap;
yading@10 56 int w, h, x, y, i;
yading@10 57 int64_t packet_time = 0;
yading@10 58 GetBitContext gb;
yading@10 59 int has_alpha = avctx->codec_tag == MKTAG('D','X','S','A');
yading@10 60
yading@10 61 // check that at least header fits
yading@10 62 if (buf_size < 27 + 7 * 2 + 4 * 3) {
yading@10 63 av_log(avctx, AV_LOG_ERROR, "coded frame size %d too small\n", buf_size);
yading@10 64 return -1;
yading@10 65 }
yading@10 66
yading@10 67 // read start and end time
yading@10 68 if (buf[0] != '[' || buf[13] != '-' || buf[26] != ']') {
yading@10 69 av_log(avctx, AV_LOG_ERROR, "invalid time code\n");
yading@10 70 return -1;
yading@10 71 }
yading@10 72 if (avpkt->pts != AV_NOPTS_VALUE)
yading@10 73 packet_time = av_rescale_q(avpkt->pts, AV_TIME_BASE_Q, (AVRational){1, 1000});
yading@10 74 sub->start_display_time = parse_timecode(buf + 1, packet_time);
yading@10 75 sub->end_display_time = parse_timecode(buf + 14, packet_time);
yading@10 76 buf += 27;
yading@10 77
yading@10 78 // read header
yading@10 79 w = bytestream_get_le16(&buf);
yading@10 80 h = bytestream_get_le16(&buf);
yading@10 81 if (av_image_check_size(w, h, 0, avctx) < 0)
yading@10 82 return -1;
yading@10 83 x = bytestream_get_le16(&buf);
yading@10 84 y = bytestream_get_le16(&buf);
yading@10 85 // skip bottom right position, it gives no new information
yading@10 86 bytestream_get_le16(&buf);
yading@10 87 bytestream_get_le16(&buf);
yading@10 88 // The following value is supposed to indicate the start offset
yading@10 89 // (relative to the palette) of the data for the second field,
yading@10 90 // however there are files in which it has a bogus value and thus
yading@10 91 // we just ignore it
yading@10 92 bytestream_get_le16(&buf);
yading@10 93
yading@10 94 // allocate sub and set values
yading@10 95 sub->rects = av_mallocz(sizeof(*sub->rects));
yading@10 96 sub->rects[0] = av_mallocz(sizeof(*sub->rects[0]));
yading@10 97 sub->num_rects = 1;
yading@10 98 sub->rects[0]->x = x; sub->rects[0]->y = y;
yading@10 99 sub->rects[0]->w = w; sub->rects[0]->h = h;
yading@10 100 sub->rects[0]->type = SUBTITLE_BITMAP;
yading@10 101 sub->rects[0]->pict.linesize[0] = w;
yading@10 102 sub->rects[0]->pict.data[0] = av_malloc(w * h);
yading@10 103 sub->rects[0]->nb_colors = 4;
yading@10 104 sub->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
yading@10 105
yading@10 106 // read palette
yading@10 107 for (i = 0; i < sub->rects[0]->nb_colors; i++)
yading@10 108 ((uint32_t*)sub->rects[0]->pict.data[1])[i] = bytestream_get_be24(&buf);
yading@10 109 // make all except background (first entry) non-transparent
yading@10 110 for (i = 0; i < sub->rects[0]->nb_colors; i++)
yading@10 111 ((uint32_t*)sub->rects[0]->pict.data[1])[i] |= (has_alpha ? *buf++ : (i ? 0xff : 0)) << 24;
yading@10 112
yading@10 113 // process RLE-compressed data
yading@10 114 init_get_bits(&gb, buf, (buf_end - buf) * 8);
yading@10 115 bitmap = sub->rects[0]->pict.data[0];
yading@10 116 for (y = 0; y < h; y++) {
yading@10 117 // interlaced: do odd lines
yading@10 118 if (y == (h + 1) / 2) bitmap = sub->rects[0]->pict.data[0] + w;
yading@10 119 for (x = 0; x < w; ) {
yading@10 120 int log2 = ff_log2_tab[show_bits(&gb, 8)];
yading@10 121 int run = get_bits(&gb, 14 - 4 * (log2 >> 1));
yading@10 122 int color = get_bits(&gb, 2);
yading@10 123 run = FFMIN(run, w - x);
yading@10 124 // run length 0 means till end of row
yading@10 125 if (!run) run = w - x;
yading@10 126 memset(bitmap, color, run);
yading@10 127 bitmap += run;
yading@10 128 x += run;
yading@10 129 }
yading@10 130 // interlaced, skip every second line
yading@10 131 bitmap += w;
yading@10 132 align_get_bits(&gb);
yading@10 133 }
yading@10 134 *data_size = 1;
yading@10 135 return buf_size;
yading@10 136 }
yading@10 137
yading@10 138 AVCodec ff_xsub_decoder = {
yading@10 139 .name = "xsub",
yading@10 140 .type = AVMEDIA_TYPE_SUBTITLE,
yading@10 141 .id = AV_CODEC_ID_XSUB,
yading@10 142 .init = decode_init,
yading@10 143 .decode = decode_frame,
yading@10 144 .long_name = NULL_IF_CONFIG_SMALL("XSUB"),
yading@10 145 };