yading@10: /* yading@10: * 3GPP TS 26.245 Timed Text decoder yading@10: * Copyright (c) 2012 Philip Langdale yading@10: * yading@10: * This file is part of FFmpeg. yading@10: * yading@10: * FFmpeg is free software; you can redistribute it and/or yading@10: * modify it under the terms of the GNU Lesser General Public yading@10: * License as published by the Free Software Foundation; either yading@10: * version 2.1 of the License, or (at your option) any later version. yading@10: * yading@10: * FFmpeg is distributed in the hope that it will be useful, yading@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@10: * Lesser General Public License for more details. yading@10: * yading@10: * You should have received a copy of the GNU Lesser General Public yading@10: * License along with FFmpeg; if not, write to the Free Software yading@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@10: */ yading@10: yading@10: #include "avcodec.h" yading@10: #include "ass.h" yading@10: #include "libavutil/avstring.h" yading@10: #include "libavutil/common.h" yading@10: #include "libavutil/bprint.h" yading@10: #include "libavutil/intreadwrite.h" yading@10: yading@10: static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end) yading@10: { yading@10: while (text < text_end) { yading@10: switch (*text) { yading@10: case '\r': yading@10: break; yading@10: case '\n': yading@10: av_bprintf(buf, "\\N"); yading@10: break; yading@10: default: yading@10: av_bprint_chars(buf, *text, 1); yading@10: break; yading@10: } yading@10: text++; yading@10: } yading@10: yading@10: av_bprintf(buf, "\r\n"); yading@10: return 0; yading@10: } yading@10: yading@10: static int mov_text_init(AVCodecContext *avctx) { yading@10: /* yading@10: * TODO: Handle the default text style. yading@10: * NB: Most players ignore styles completely, with the result that yading@10: * it's very common to find files where the default style is broken yading@10: * and respecting it results in a worse experience than ignoring it. yading@10: */ yading@10: return ff_ass_subtitle_header_default(avctx); yading@10: } yading@10: yading@10: static int mov_text_decode_frame(AVCodecContext *avctx, yading@10: void *data, int *got_sub_ptr, AVPacket *avpkt) yading@10: { yading@10: AVSubtitle *sub = data; yading@10: int ts_start, ts_end; yading@10: AVBPrint buf; yading@10: const char *ptr = avpkt->data; yading@10: const char *end; yading@10: yading@10: if (!ptr || avpkt->size < 2) yading@10: return AVERROR_INVALIDDATA; yading@10: yading@10: /* yading@10: * A packet of size two with value zero is an empty subtitle yading@10: * used to mark the end of the previous non-empty subtitle. yading@10: * We can just drop them here as we have duration information yading@10: * already. If the value is non-zero, then it's technically a yading@10: * bad packet. yading@10: */ yading@10: if (avpkt->size == 2) yading@10: return AV_RB16(ptr) == 0 ? 0 : AVERROR_INVALIDDATA; yading@10: yading@10: /* yading@10: * The first two bytes of the packet are the length of the text string yading@10: * In complex cases, there are style descriptors appended to the string yading@10: * so we can't just assume the packet size is the string size. yading@10: */ yading@10: end = ptr + FFMIN(2 + AV_RB16(ptr), avpkt->size); yading@10: ptr += 2; yading@10: yading@10: ts_start = av_rescale_q(avpkt->pts, yading@10: avctx->time_base, yading@10: (AVRational){1,100}); yading@10: ts_end = av_rescale_q(avpkt->pts + avpkt->duration, yading@10: avctx->time_base, yading@10: (AVRational){1,100}); yading@10: yading@10: // Note that the spec recommends lines be no longer than 2048 characters. yading@10: av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED); yading@10: text_to_ass(&buf, ptr, end); yading@10: yading@10: if (!av_bprint_is_complete(&buf)) yading@10: return AVERROR(ENOMEM); yading@10: yading@10: ff_ass_add_rect(sub, buf.str, ts_start, ts_end-ts_start, 0); yading@10: *got_sub_ptr = sub->num_rects > 0; yading@10: av_bprint_finalize(&buf, NULL); yading@10: return avpkt->size; yading@10: } yading@10: yading@10: AVCodec ff_movtext_decoder = { yading@10: .name = "mov_text", yading@10: .long_name = NULL_IF_CONFIG_SMALL("3GPP Timed Text subtitle"), yading@10: .type = AVMEDIA_TYPE_SUBTITLE, yading@10: .id = AV_CODEC_ID_MOV_TEXT, yading@10: .init = mov_text_init, yading@10: .decode = mov_text_decode_frame, yading@10: };