From patchwork Wed Aug 2 06:30:39 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rodger Combs X-Patchwork-Id: 4587 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.46.211 with SMTP id u202csp880902vsu; Wed, 2 Aug 2017 00:17:33 -0700 (PDT) X-Received: by 10.28.154.211 with SMTP id c202mr2975762wme.91.1501658252984; Wed, 02 Aug 2017 00:17:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1501658252; cv=none; d=google.com; s=arc-20160816; b=vzRl8Bpu3AS2GvHGU3DSwzbRmMwePMtFe/cU1IVZjcq01AsN9N5o8DWfE9xteHru1u Rv5CWAo4F5jlDVCtR8r9xj+wUofAqieiNgs/WfhHekS5tt+1UBSE9/sbrAVrHEVyHE63 V5ljqE7gqo679ILVjb5rRYD8uO4l6heO3SklMenzDt83xlzSneB9GiVdD/xgE3b4UxLP Oo1htTwl9axRFxMuN4lQ/3rTqUWoxnm7rokwbmOwRDJh3xhyaOtscIztG2/SRe4yoK5L UiN0NDc0sFtCz1a4d8hijhoWAaJexmjZrNrlJc7wgZkbIUtCInLWBWp4lNY6/Yi9ForP si+A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:message-id:date:to:from:dkim-signature :delivered-to:arc-authentication-results; bh=VMZyAAzUEihaC6R3iCfB4JCb7pHOZD5ezuW/l2u16jM=; b=QjSWBBVcpOPb66TnqII+ImucKIqRv2TbNaLf0+D3CiHp/EC+15M6VyD0Z9f8NwxkXF yE2yO1vLH12R+RS3rWgdxNidbrq5ywGj2JV5Y3aSFC8I6hjRmcr/tTfVS7DhfwYb0smH TMDYEYVu8I0ZBwpDEROQ/eWwm82N8dK/moLZV697Esxss7kPnBJTgQrw4YQ7P66aiLLq 9p9IgQiJoOiHgIVzE6cgaTMz9aIJhimXU3N/jeAahPTZLHls7qEiqvaL/swpE7ahTuJg TaaumdaLH1RtWZLkH7ccqNQUw5yudMI83e6EywHGdWlDpCpTlP9UL8cof8NLBSwq6IMs 3KpA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.b=u3xtnjWT; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id w63si16629212wrb.70.2017.08.02.00.17.32; Wed, 02 Aug 2017 00:17:32 -0700 (PDT) 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 header.b=u3xtnjWT; 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 D6BF968A45B; Wed, 2 Aug 2017 10:17:26 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-io0-f176.google.com (mail-io0-f176.google.com [209.85.223.176]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id B64A768A444 for ; Wed, 2 Aug 2017 10:17:19 +0300 (EEST) Received: by mail-io0-f176.google.com with SMTP id g35so17038000ioi.3 for ; Wed, 02 Aug 2017 00:17:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=u57hKfQY8t22mFDgOf4BwbvxPZJ7M2pZw1dT+kdIRhY=; b=u3xtnjWTeNG6OMomR9kPhpEdoRHkRF/5dxo+MpreCvU4pD5gNLlBX4H/2smDgCqaqZ qPyBT/rPx2RveovsYKXrUOukSYbWp80WtBnmUZ6s5cMk2sRWPm8ZZ5VzYm2GpBBOc9aI gqSLh0cIE8JdPWjXR89V+zbfVo+nRNNXpKl0jUJ/ng8WeMTXnFsp9Ibu8Ef9kV6NRuhV EcrJM+oPEgrz25HY0jF7rRNv64o875cMBCrl5Xxb+5EfyYi9idvm1uLuMVWhcouLxVhb AasjfRa08hcP2gVSbrxoLvYgrkP8SWw8md/CijtmKcpjTz3FF2QpmUr2VhxYFSTsT9Dg pzng== 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=u57hKfQY8t22mFDgOf4BwbvxPZJ7M2pZw1dT+kdIRhY=; b=tvQSWn09BJfkRfXxGp5+G+xMeYW1Zp0vgaWkpsyRDEuPPfMykjhutXCzBzo0Nalx2R 2cXLKok8ENlQOJzCZHz64i9rhmM18eD+ll7H3ZqyZvltlUkJU8j7D3p3TimMxVpc/4XX KHmZAInsHZ3ojoVqGVgt+uX+3DcJhvfUAdvYvwTx5QVc5hZRSIcO7as0UuFhAGNPeXFD ZWS5hpEjH0N8lXofGodWF5gOMK7duBUUExUKHGNIWSHvormruSGRapNrcIwkMtRypFLo cEFq4ktjksAC20DUs1Tm2baez4TsNnfq69oOSHx0bOlSkMn/r00IQJVmCyQRgh+sVRMO Ocpw== X-Gm-Message-State: AIVw113WwyYrs3xXPuIQOSaxzGn5JqxYVj2YIz040lESuB1flglW8RIs h/u9EZtII6jfjMhEVrU= X-Received: by 10.107.138.89 with SMTP id m86mr24725524iod.26.1501658241213; Wed, 02 Aug 2017 00:17:21 -0700 (PDT) Received: from rcombs-NAS.hsd1.il.comcast.net. (c-73-110-121-59.hsd1.il.comcast.net. [73.110.121.59]) by smtp.gmail.com with ESMTPSA id 196sm4382274iou.50.2017.08.02.00.17.20 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 02 Aug 2017 00:17:20 -0700 (PDT) From: Rodger Combs To: ffmpeg-devel@ffmpeg.org Date: Wed, 2 Aug 2017 01:30:39 -0500 Message-Id: <1501655445-18976-1-git-send-email-rodger.combs@gmail.com> X-Mailer: git-send-email 2.6.4 Subject: [FFmpeg-devel] [PATCH 1/7] lavf: add cue sheet demuxer 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" --- Changelog | 2 + doc/demuxers.texi | 17 ++++ libavformat/Makefile | 1 + libavformat/allformats.c | 1 + libavformat/cuedec.c | 246 +++++++++++++++++++++++++++++++++++++++++++++++ libavformat/version.h | 2 +- 6 files changed, 268 insertions(+), 1 deletion(-) create mode 100644 libavformat/cuedec.c diff --git a/Changelog b/Changelog index 187ae79..6701d30 100644 --- a/Changelog +++ b/Changelog @@ -29,6 +29,8 @@ version : - limiter video filter - libvmaf video filter - Dolby E decoder and SMPTE 337M demuxer +- Cue sheet demuxer + version 3.3: - CrystalHD decoder moved to new decode API diff --git a/doc/demuxers.texi b/doc/demuxers.texi index 29a23d4..04eaf27 100644 --- a/doc/demuxers.texi +++ b/doc/demuxers.texi @@ -244,6 +244,23 @@ file subdir/file-2.wav @end example @end itemize +@section cue + +Cue sheet demuxer. + +This demuxer reads a cue sheet (text file) and exports its track listing in +the form of AVChapters. Packet data is read from the file listed in the sheet, +which must be in the same directory. To override the path the packet data is +read from, use the @code{url} option. + +This demuxer currently only supports cue sheets with a single audio file. + +This demuxer can be used along with the segment muxer to split a cue+audio pair +into individual files for the different tracks: +@example +ffmpeg -i input.cue -f segment -segment_chapters 1 '%02d $artist$ - $title$.flac' +@end example + @section flv, live_flv Adobe Flash Video Format demuxer. diff --git a/libavformat/Makefile b/libavformat/Makefile index b0ef82c..4381c42 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -130,6 +130,7 @@ OBJS-$(CONFIG_CDXL_DEMUXER) += cdxl.o OBJS-$(CONFIG_CINE_DEMUXER) += cinedec.o OBJS-$(CONFIG_CONCAT_DEMUXER) += concatdec.o OBJS-$(CONFIG_CRC_MUXER) += crcenc.o +OBJS-$(CONFIG_CUE_DEMUXER) += cuedec.o OBJS-$(CONFIG_DATA_DEMUXER) += rawdec.o OBJS-$(CONFIG_DATA_MUXER) += rawenc.o OBJS-$(CONFIG_DASH_MUXER) += dashenc.o diff --git a/libavformat/allformats.c b/libavformat/allformats.c index 1ebc142..25afa8b 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -96,6 +96,7 @@ static void register_all(void) REGISTER_DEMUXER (CINE, cine); REGISTER_DEMUXER (CONCAT, concat); REGISTER_MUXER (CRC, crc); + REGISTER_DEMUXER (CUE, cue); REGISTER_MUXER (DASH, dash); REGISTER_MUXDEMUX(DATA, data); REGISTER_MUXDEMUX(DAUD, daud); diff --git a/libavformat/cuedec.c b/libavformat/cuedec.c new file mode 100644 index 0000000..11b256d --- /dev/null +++ b/libavformat/cuedec.c @@ -0,0 +1,246 @@ +/* + * Cue sheet demuxer + * Copyright (c) 2016 The FFmpeg Project + * + * 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 + */ + +/** + * @file + * Cue sheet demuxer + * @author Rodger Combs + */ + +#include "avformat.h" +#include "internal.h" +#include "subtitles.h" +#include "url.h" +#include "libavutil/intreadwrite.h" +#include "libavutil/avstring.h" +#include "libavutil/opt.h" + +typedef struct CueDemuxContext { + AVClass *class; + char *url; + AVFormatContext *avf; +} CueDemuxContext; + +static int cue_probe(AVProbeData *p) +{ + const unsigned char *ptr = p->buf; + int has_file = 0, has_track = 0; + + if (AV_RB24(ptr) == 0xEFBBBF) + ptr += 3; /* skip UTF-8 BOM */ + while (*ptr && (!has_file || !has_track)) { + while (*ptr == ' ' || *ptr == '\t') + ptr++; + if (!strncmp(ptr, "FILE ", 5)) { + ptr += 5; + while (*ptr == ' ' || *ptr == '\t') + ptr++; + if (*ptr == '"') + has_file = 1; + } else if (!strncmp(ptr, "TRACK ", 6)) { + ptr += 6; + while (*ptr == ' ' || *ptr == '\t') + ptr++; + if (!av_isdigit(*ptr)) + return 0; + while (av_isdigit(*ptr)) + ptr++; + if (*ptr != ' ' && *ptr != '\t') + return 0; + while (*ptr == ' ' || *ptr == '\t') + ptr++; + if (!strncmp(ptr, "AUDIO", 5)) + has_track = 1; + } + ptr += ff_subtitles_next_line(ptr); + } + if (has_file && has_track) + return AVPROBE_SCORE_MAX - 5; + return 0; +} + +static char *get_token(char *in) +{ + char *end; + while (av_isspace(*in)) + in++; + if (*in == '"') { + in++; + end = in + strcspn(in, "\"\n\t\r"); + } else { + end = in + strcspn(in, " \n\t\r"); + } + *end = '\0'; + return in; +} + +static int cue_read_header(AVFormatContext *s) +{ + int got_file = 0; + int ret, i; + CueDemuxContext *cue = s->priv_data; + char line[4096], *ptr; + AVDictionary **meta = &s->metadata; + AVChapter *chap = NULL; + while (ff_get_line(s->pb, line, sizeof(line))) { + ptr = line; + if (AV_RB24(ptr) == 0xEFBBBF) + ptr += 3; /* skip UTF-8 BOM */ + while (*ptr == ' ' || *ptr == '\t') + ptr++; + if (!strncmp(ptr, "REM ", 4)) { + char *end = ptr + strcspn(ptr, "\r\n"); + *end = '\0'; + av_log(s, AV_LOG_INFO, "Comment: \"%s\"\n", ptr + 4); + } else if (!strncmp(ptr, "TITLE ", 6)) { + ptr = get_token(ptr + 6); + av_dict_set(meta, chap ? "title" : "album", ptr, 0); + } else if (!strncmp(ptr, "PERFORMER ", 10)) { + ptr = get_token(ptr + 10); + av_dict_set(meta, chap ? "artist" : "album_artist", ptr, 0); + } else if (!strncmp(ptr, "FILE ", 5)) { + if (got_file) { + avpriv_request_sample(s, "Cue sheet with multiple FILE directives"); + return AVERROR_PATCHWELCOME; + } + if (!cue->url || !*cue->url) { + const char *filename = get_token(ptr + 5); + char url[4096] = {0}; + + if (filename[strcspn(filename, "/\\:")] != 0) { + av_log(s, AV_LOG_ERROR, "Only bare filenames are allowed in cue FILE directives.\n" + "To read from '%s', use the 'url' option explicitly.", filename); + return AVERROR(EPERM); + } + + av_freep(&cue->url); + ff_make_absolute_url(url, sizeof(url), s->filename, filename); + if (!(cue->url = av_strdup(url))) + return AVERROR(ENOMEM); + } + got_file = 1; + } else if (!strncmp(ptr, "TRACK ", 6)) { + int index = strtol(ptr + 6, &ptr, 10); + chap = avpriv_new_chapter(s, index, (AVRational){1, 75}, AV_NOPTS_VALUE, AV_NOPTS_VALUE, NULL); + if (!chap) + return AVERROR(ENOMEM); + meta = &chap->metadata; + if ((ret = av_dict_copy(meta, s->metadata, 0)) < 0) + return ret; + av_dict_set_int(meta, "track", index, 0); + } else if (!strncmp(ptr, "INDEX ", 6)) { + int min, sec, frame; + int index; + if (!chap) + return AVERROR_INVALIDDATA; + if (sscanf(ptr + 6, "%u %u:%u:%u", &index, &min, &sec, &frame) != 4) + return AVERROR_INVALIDDATA; + if (index == 1 || chap->start == 0) + chap->start = min * 75 * 60 + sec * 75 + frame; + } else { + av_log(s, AV_LOG_WARNING, "Unknown command: \"%s\"\n", ptr); + } + } + + if (!cue->url || !*cue->url) + return AVERROR_INVALIDDATA; + + if (!(cue->avf = avformat_alloc_context())) + return AVERROR(ENOMEM); + + cue->avf->interrupt_callback = s->interrupt_callback; + if ((ret = ff_copy_whiteblacklists(cue->avf, s)) < 0) + return ret; + + if ((ret = avformat_open_input(&cue->avf, cue->url, NULL, NULL)) < 0 || + (ret = avformat_find_stream_info(cue->avf, NULL)) < 0) { + av_log(s, AV_LOG_ERROR, "Failed to open '%s'\n", cue->url); + avformat_close_input(&cue->avf); + return ret; + } + + ff_read_frame_flush(cue->avf); + + for (i = 0; i < cue->avf->nb_streams; i++) { + AVStream *st = avformat_new_stream(s, NULL); + AVStream *ist = cue->avf->streams[i]; + if (!st) + return AVERROR(ENOMEM); + st->id = i; + + avcodec_parameters_copy(st->codecpar, ist->codecpar); + + st->disposition = ist->disposition; + avpriv_set_pts_info(st, ist->pts_wrap_bits, ist->time_base.num, ist->time_base.den); + av_copy_packet(&st->attached_pic, &ist->attached_pic); + } + + s->duration = cue->avf->duration; + + return 0; +} + +static int cue_read_packet(AVFormatContext *s, AVPacket *pkt) +{ + CueDemuxContext *cue = s->priv_data; + return av_read_frame(cue->avf, pkt); +} + +static int cue_read_seek(AVFormatContext *s, int stream_index, + int64_t min_ts, int64_t ts, int64_t max_ts, int flags) +{ + CueDemuxContext *cue = s->priv_data; + return avformat_seek_file(cue->avf, stream_index, min_ts, ts, max_ts, flags); +} + +static int cue_read_close(AVFormatContext *s) +{ + CueDemuxContext *cue = s->priv_data; + avformat_close_input(&cue->avf); + return 0; +} + +#define OFFSET(x) offsetof(CueDemuxContext, x) +#define E AV_OPT_FLAG_DECODING_PARAM +static const AVOption options[] = { + { "url", "override underlying audio location", OFFSET(url), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, E }, + { NULL } +}; + +static const AVClass cue_class = { + .class_name = "Cue sheet demuxer", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + +AVInputFormat ff_cue_demuxer = { + .name = "cue", + .long_name = NULL_IF_CONFIG_SMALL("Cue sheet"), + .extensions = "cue", + .priv_data_size = sizeof(CueDemuxContext), + .read_probe = cue_probe, + .read_header = cue_read_header, + .read_packet = cue_read_packet, + .read_seek2 = cue_read_seek, + .read_close = cue_read_close, + .priv_class = &cue_class, +}; diff --git a/libavformat/version.h b/libavformat/version.h index 48b81f2..a8cf4c1 100644 --- a/libavformat/version.h +++ b/libavformat/version.h @@ -32,7 +32,7 @@ // Major bumping may affect Ticket5467, 5421, 5451(compatibility with Chromium) // Also please add any ticket numbers that you believe might be affected here #define LIBAVFORMAT_VERSION_MAJOR 57 -#define LIBAVFORMAT_VERSION_MINOR 76 +#define LIBAVFORMAT_VERSION_MINOR 77 #define LIBAVFORMAT_VERSION_MICRO 100 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \