From patchwork Sat Jan 28 17:09:55 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 2351 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.89.21 with SMTP id n21csp756969vsb; Sat, 28 Jan 2017 09:10:24 -0800 (PST) X-Received: by 10.28.64.132 with SMTP id n126mr8308813wma.107.1485623424794; Sat, 28 Jan 2017 09:10:24 -0800 (PST) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 51si10154579wra.235.2017.01.28.09.10.24; Sat, 28 Jan 2017 09:10:24 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C617068A32C; Sat, 28 Jan 2017 19:10:19 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm0-f66.google.com (mail-wm0-f66.google.com [74.125.82.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 994FC68A1F5 for ; Sat, 28 Jan 2017 19:10:13 +0200 (EET) Received: by mail-wm0-f66.google.com with SMTP id r126so66002510wmr.3 for ; Sat, 28 Jan 2017 09:10:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=ZXHMoqzvhFxnrGsPoSzygjU8r3JGJ9jBK1Smg3W6xoc=; b=dVgkrgQegXyw5raNzrpXqLhM+VWsoxJ9lU2mCUdbn6/Sw1oJj//cc9bUw9kugY0bKm Hg5B95ArcOIlX2IykAOQsVFDY8xutCn1X9SQIaKsvrU3MBBVF3bfAd1M1rugJV7/fkyZ qsJbj0f+kyykCu1jMQXZOPvKQsOq4R5HcxxTbiWcTuMfBO4QBS4SgvDDQJRpeDOJK+Ql ASn4lJVq/rQFAeKTf/PUrjYTywU4TORCAcvRpSyzN2ZaEEnW+ojHMUmvJdSnUeAhT1rH t1QXkS5IbcUlg1/ChL5ui7f+OXJx/rhugY8G933bm84Pmc2V0DoNIop381fmWzLFtEux ffZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=ZXHMoqzvhFxnrGsPoSzygjU8r3JGJ9jBK1Smg3W6xoc=; b=NwrYFY6tW4OkHjarbep+Kg5u6vbWGHhgDih5uHe2Al52dgwHaTCa2UwVk9xwqPE/Ty +WGBvRAIlldVF/MQr7v0+CSofww8vYcJrOs+3F+40epbxCMT6xn4KmPq1pu796oPW5qU b5cls50LbquATXXWtku949KWWb8cLx6gD82PWLtS6VWQ+xEfyn48ypvfzFPe1oOw6vUh dl7UP9hydaR5ygIymqWETYrw5/iqprlQXC3O28YFiYukSCyBl7OiRIsu9ibxttQUCSSl wafI3Z275r1NvzQy7U8DDu7qvOL2Rq1aX8zCp6Ik6kknG89J+vWYO+isJDbGINwlNgWJ bh7g== X-Gm-Message-State: AIkVDXJdOdMTIRDDgSpmApnUt5ZYIUFy6URAyG+vQb9uUhv47+Hmghcf6oJPqqz/alxDnQ== X-Received: by 10.28.145.3 with SMTP id t3mr8110698wmd.47.1485623414523; Sat, 28 Jan 2017 09:10:14 -0800 (PST) Received: from localhost.localdomain ([37.244.246.24]) by smtp.gmail.com with ESMTPSA id a186sm9752419wmh.1.2017.01.28.09.10.13 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 28 Jan 2017 09:10:13 -0800 (PST) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Sat, 28 Jan 2017 18:09:55 +0100 Message-Id: <20170128170955.24494-1-onemda@gmail.com> X-Mailer: git-send-email 2.9.3 Subject: [FFmpeg-devel] [PATCH] avformat: add SCC muxer X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Paul B Mahol --- libavformat/Makefile | 1 + libavformat/allformats.c | 2 +- libavformat/sccenc.c | 122 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 libavformat/sccenc.c diff --git a/libavformat/Makefile b/libavformat/Makefile index e291925..478c06b 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -435,6 +435,7 @@ OBJS-$(CONFIG_SAP_DEMUXER) += sapdec.o OBJS-$(CONFIG_SAP_MUXER) += sapenc.o OBJS-$(CONFIG_SBG_DEMUXER) += sbgdec.o OBJS-$(CONFIG_SCC_DEMUXER) += sccdec.o subtitles.o +OBJS-$(CONFIG_SCC_MUXER) += sccenc.o subtitles.o OBJS-$(CONFIG_SDP_DEMUXER) += rtsp.o OBJS-$(CONFIG_SDR2_DEMUXER) += sdr2.o OBJS-$(CONFIG_SDS_DEMUXER) += sdsdec.o diff --git a/libavformat/allformats.c b/libavformat/allformats.c index 9c1f6dd..35869e3 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -273,7 +273,7 @@ void av_register_all(void) REGISTER_DEMUXER (SAMI, sami); REGISTER_MUXDEMUX(SAP, sap); REGISTER_DEMUXER (SBG, sbg); - REGISTER_DEMUXER (SCC, scc); + REGISTER_MUXDEMUX(SCC, scc); REGISTER_DEMUXER (SDP, sdp); REGISTER_DEMUXER (SDR2, sdr2); REGISTER_DEMUXER (SDS, sds); diff --git a/libavformat/sccenc.c b/libavformat/sccenc.c new file mode 100644 index 0000000..5205f31 --- /dev/null +++ b/libavformat/sccenc.c @@ -0,0 +1,122 @@ +/* + * SCC muxer + * Copyright (c) 2017 Paul B Mahol + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "avformat.h" +#include "internal.h" +#include "libavutil/log.h" +#include "libavutil/intreadwrite.h" + +typedef struct SCCContext { + int prev_h, prev_m, prev_s, prev_f; + int inside; + int n; +} SCCContext; + +static int scc_write_header(AVFormatContext *avf) +{ + SCCContext *scc = avf->priv_data; + + if (avf->nb_streams != 1 || + avf->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) { + av_log(avf, AV_LOG_ERROR, + "SCC supports only a single subtitles stream.\n"); + return AVERROR(EINVAL); + } + if (avf->streams[0]->codecpar->codec_id != AV_CODEC_ID_EIA_608) { + av_log(avf, AV_LOG_ERROR, + "Unsupported subtitles codec: %s\n", + avcodec_get_name(avf->streams[0]->codecpar->codec_id)); + return AVERROR(EINVAL); + } + avpriv_set_pts_info(avf->streams[0], 64, 1, 1000); + avio_printf(avf->pb, "Scenarist_SCC V1.0\n"); + + scc->prev_h = scc->prev_m = scc->prev_s = scc->prev_f = -1; + scc->inside = 0; + + return 0; +} + +static int scc_write_packet(AVFormatContext *avf, AVPacket *pkt) +{ + SCCContext *scc = avf->priv_data; + int64_t pts = pkt->pts; + int i, h, m, s, f; + + if (pts == AV_NOPTS_VALUE) { + av_log(avf, AV_LOG_WARNING, + "Insufficient timestamps.\n"); + return 0; + } + + h = (int)(pts / (3600000)); + m = (int)(pts / (60000)) % 60; + s = (int)(pts / 1000) % 60; + f = (int)(pts % 1000) / 33; + + for (i = 0; i < pkt->size; i+=3) { + if (pkt->data[i] == 0xfc && ((pkt->data[i + 1] != 0x80 || pkt->data[i + 2] != 0x80))) + break; + } + if (i >= pkt->size) + return 0; + + if (!scc->inside && (scc->prev_h != h || scc->prev_m != m || scc->prev_s != s || scc->prev_f != f)) { + avio_printf(avf->pb, "\n%02d:%02d:%02d:%02d\t", h, m, s, f); + scc->inside = 1; + } + for (i = 0; i < pkt->size; i+=3) { + if (i + 3 > pkt->size) + break; + + if (pkt->data[i] != 0xfc || (pkt->data[i + 1] == 0x80 && pkt->data[i + 2] == 0x80)) + continue; + if (!scc->inside) { + avio_printf(avf->pb, "\n%02d:%02d:%02d:%02d\t", h, m, s, f); + scc->inside = 1; + } + if (scc->n > 0) + avio_printf(avf->pb, " "); + avio_printf(avf->pb, "%02x%02x", pkt->data[i + 1], pkt->data[i + 2]); + scc->n++; + } + if (scc->inside && (scc->prev_h != h || scc->prev_m != m || scc->prev_s != s || scc->prev_f != f)) { + avio_printf(avf->pb, "\n"); + scc->n = 0; + scc->inside = 0; + } + + scc->prev_h = h; + scc->prev_m = m; + scc->prev_s = s; + scc->prev_f = f; + return 0; +} + +AVOutputFormat ff_scc_muxer = { + .name = "scc", + .long_name = NULL_IF_CONFIG_SMALL("Scenarist Closed Captions"), + .extensions = "scc", + .priv_data_size = sizeof(SCCContext), + .write_header = scc_write_header, + .write_packet = scc_write_packet, + .subtitle_codec = AV_CODEC_ID_EIA_608, +};