annotate ffmpeg/libavcodec/movtextdec.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 * 3GPP TS 26.245 Timed Text decoder
yading@10 3 * Copyright (c) 2012 Philip Langdale <philipl@overt.org>
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 "avcodec.h"
yading@10 23 #include "ass.h"
yading@10 24 #include "libavutil/avstring.h"
yading@10 25 #include "libavutil/common.h"
yading@10 26 #include "libavutil/bprint.h"
yading@10 27 #include "libavutil/intreadwrite.h"
yading@10 28
yading@10 29 static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end)
yading@10 30 {
yading@10 31 while (text < text_end) {
yading@10 32 switch (*text) {
yading@10 33 case '\r':
yading@10 34 break;
yading@10 35 case '\n':
yading@10 36 av_bprintf(buf, "\\N");
yading@10 37 break;
yading@10 38 default:
yading@10 39 av_bprint_chars(buf, *text, 1);
yading@10 40 break;
yading@10 41 }
yading@10 42 text++;
yading@10 43 }
yading@10 44
yading@10 45 av_bprintf(buf, "\r\n");
yading@10 46 return 0;
yading@10 47 }
yading@10 48
yading@10 49 static int mov_text_init(AVCodecContext *avctx) {
yading@10 50 /*
yading@10 51 * TODO: Handle the default text style.
yading@10 52 * NB: Most players ignore styles completely, with the result that
yading@10 53 * it's very common to find files where the default style is broken
yading@10 54 * and respecting it results in a worse experience than ignoring it.
yading@10 55 */
yading@10 56 return ff_ass_subtitle_header_default(avctx);
yading@10 57 }
yading@10 58
yading@10 59 static int mov_text_decode_frame(AVCodecContext *avctx,
yading@10 60 void *data, int *got_sub_ptr, AVPacket *avpkt)
yading@10 61 {
yading@10 62 AVSubtitle *sub = data;
yading@10 63 int ts_start, ts_end;
yading@10 64 AVBPrint buf;
yading@10 65 const char *ptr = avpkt->data;
yading@10 66 const char *end;
yading@10 67
yading@10 68 if (!ptr || avpkt->size < 2)
yading@10 69 return AVERROR_INVALIDDATA;
yading@10 70
yading@10 71 /*
yading@10 72 * A packet of size two with value zero is an empty subtitle
yading@10 73 * used to mark the end of the previous non-empty subtitle.
yading@10 74 * We can just drop them here as we have duration information
yading@10 75 * already. If the value is non-zero, then it's technically a
yading@10 76 * bad packet.
yading@10 77 */
yading@10 78 if (avpkt->size == 2)
yading@10 79 return AV_RB16(ptr) == 0 ? 0 : AVERROR_INVALIDDATA;
yading@10 80
yading@10 81 /*
yading@10 82 * The first two bytes of the packet are the length of the text string
yading@10 83 * In complex cases, there are style descriptors appended to the string
yading@10 84 * so we can't just assume the packet size is the string size.
yading@10 85 */
yading@10 86 end = ptr + FFMIN(2 + AV_RB16(ptr), avpkt->size);
yading@10 87 ptr += 2;
yading@10 88
yading@10 89 ts_start = av_rescale_q(avpkt->pts,
yading@10 90 avctx->time_base,
yading@10 91 (AVRational){1,100});
yading@10 92 ts_end = av_rescale_q(avpkt->pts + avpkt->duration,
yading@10 93 avctx->time_base,
yading@10 94 (AVRational){1,100});
yading@10 95
yading@10 96 // Note that the spec recommends lines be no longer than 2048 characters.
yading@10 97 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
yading@10 98 text_to_ass(&buf, ptr, end);
yading@10 99
yading@10 100 if (!av_bprint_is_complete(&buf))
yading@10 101 return AVERROR(ENOMEM);
yading@10 102
yading@10 103 ff_ass_add_rect(sub, buf.str, ts_start, ts_end-ts_start, 0);
yading@10 104 *got_sub_ptr = sub->num_rects > 0;
yading@10 105 av_bprint_finalize(&buf, NULL);
yading@10 106 return avpkt->size;
yading@10 107 }
yading@10 108
yading@10 109 AVCodec ff_movtext_decoder = {
yading@10 110 .name = "mov_text",
yading@10 111 .long_name = NULL_IF_CONFIG_SMALL("3GPP Timed Text subtitle"),
yading@10 112 .type = AVMEDIA_TYPE_SUBTITLE,
yading@10 113 .id = AV_CODEC_ID_MOV_TEXT,
yading@10 114 .init = mov_text_init,
yading@10 115 .decode = mov_text_decode_frame,
yading@10 116 };