From patchwork Sat Jul 7 17:41:28 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Karsten Otto X-Patchwork-Id: 9645 Delivered-To: ffmpegpatchwork@gmail.com Received: by 2002:a02:104:0:0:0:0:0 with SMTP id c4-v6csp850452jad; Sat, 7 Jul 2018 10:42:07 -0700 (PDT) X-Google-Smtp-Source: AAOMgpe5HFPx0Q4Frqfz+tyxIyS4WKmNunv/M07WIAl//bKbXMX7TptOERhNzh7xWn6B1vno//s2 X-Received: by 2002:a5d:67c6:: with SMTP id n6-v6mr10145685wrw.39.1530985327260; Sat, 07 Jul 2018 10:42:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530985327; cv=none; d=google.com; s=arc-20160816; b=bWs/S/A0/SrF7QtPQvuSuGK/Aw1egZHDVbjvSF53eMGXllZ0uFZ6is8V54BHa/sgfY zxMowMMaKqQHXFld3pxrnFqp3l/3kcSmGyVTunXP2PzEd7Xeb+t0mGVLbHLKsltjcTL6 th+ylVZPEKddfsd3n5HNNpPlm08nONBhDy5PLb6ykDO7vq7+FHNnzNN0pMWmdkYj0MrB slTa4BtR1nBrSLT6u217m3+V6/Y9BixY26iwafUqESVnwdvAHo3011Jemvz/oU2aXwIC xAZxDB04ujUAS08fS1HHULba6nHTufmndfufsdnn+bykZS/INbLD179f1Do+mAtZteOd NTGg== 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:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to:arc-authentication-results; bh=4/lFldxXz4ERLuE93FjJzAWNux0Zs+LW0JjAmKAD/80=; b=vuYAP5YWydBOd3DKCfUayedK7bNNR0dYmqOjD2QT90Sb1yi7cbx9EBdwt1YJaJrlcO qBZDiG17s753Tcb8/KuJhbQeN/GPfL7U1TTsR4siIVxRe4ceokQeyKCjqUEA2NOnKaIv MWZiB7AytILO/kWrJ3IbV/Qsb6Eejo/e/QuBLz3Y1s834y9kayUSksGmS/rbZ5QyyZsB inEzaoHst7qdX6cv/vSVPF0j2fYlHugL/5032UPXsuWIRwvlvX9PnlQC7HPCvH2P0h5z key/KqZd4yBk7aZx9hkXhIHoPWsgCU+CPtGnEpcu3XZuM4lhvbLDG3qgfLZX6m9voOyN Y5tQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@posteo.de header.s=2017 header.b=h9YTeJB5; 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=posteo.de Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id c128-v6si9388323wmc.198.2018.07.07.10.42.06; Sat, 07 Jul 2018 10:42:07 -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=@posteo.de header.s=2017 header.b=h9YTeJB5; 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=posteo.de Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B7B5D689B4A; Sat, 7 Jul 2018 20:41:33 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mout01.posteo.de (mout01.posteo.de [185.67.36.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 0FB726882C8 for ; Sat, 7 Jul 2018 20:41:25 +0300 (EEST) Received: from submission (posteo.de [89.146.220.130]) by mout01.posteo.de (Postfix) with ESMTPS id 32D10210E5 for ; Sat, 7 Jul 2018 19:41:30 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.de; s=2017; t=1530985290; bh=RhOobSgOkkwOh3AdM71+FWmMF0KiaPkx6pzNtRDs79I=; h=From:To:Subject:Date:From; b=h9YTeJB5qZDKPDZwI5JORV7StaaCiizHz/fyA2J/C548Y3ACYeww15PqZk2yHHBKY q2FJCtzMrDc0Jd8bKFtZEAYt9l+6NK3KT+pybwcHBObpD1We/JYtJPKmc492OcdAww PusHf9zvxOqsrtUknZ9R6zeT8V/ubKmIume/dZlQl2MShLokYRn7kI/GqGt3AFKp/F 8/q+x//D8MZjwoDdcLmyzgZFVY7Kk9W7a1BnrOabwZF5JjJsE+ejoZo3qZUoD+L1h6 Bbakv3nGlDfNTIdmPRnHqCp8Qk/cxGu9pn9g7gSFdFqb2WuzyC0D5ubHNmhuJuhdhB e1Wfm01kuSNCg== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 41NJmn6W2Fz9rxM for ; Sat, 7 Jul 2018 19:41:29 +0200 (CEST) From: Karsten Otto To: ffmpeg-devel@ffmpeg.org Date: Sat, 7 Jul 2018 19:41:28 +0200 Message-Id: <20180707174129.835-3-ottoka@posteo.de> X-Mailer: git-send-email 2.14.3 (Apple Git-98) In-Reply-To: <20180707174129.835-1-ottoka@posteo.de> References: <20180707174129.835-1-ottoka@posteo.de> Subject: [FFmpeg-devel] [PATCH v4 2/3] aadec: add chapters and seeking 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" read_packet reads content in chunks. Thus seek must be clamped to valid chunk positions in the file, which in turn are relative to chapter start positions. So in read_header, scan for chapter headers once by skipping through the content. Set stream time_base based on bitrate in bytes/s, for easy timestamp to position conversion. Then in read_seek, find the chapter containing the seek position, calculate the nearest chunk position, and reinit the read_seek state accordingly. --- libavformat/aadec.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 83 insertions(+), 4 deletions(-) diff --git a/libavformat/aadec.c b/libavformat/aadec.c index 4db71b1939..e3c03bc522 100644 --- a/libavformat/aadec.c +++ b/libavformat/aadec.c @@ -35,6 +35,8 @@ #define MAX_TOC_ENTRIES 16 #define MAX_DICTIONARY_ENTRIES 128 #define TEA_BLOCK_SIZE 8 +#define CHAPTER_HEADER_SIZE 8 +#define TIMEPREC 1000 typedef struct AADemuxContext { AVClass *class; @@ -46,6 +48,7 @@ typedef struct AADemuxContext { struct AVTEA *tea_ctx; uint8_t file_key[16]; int64_t current_chapter_size; + int64_t content_start; int64_t content_end; } AADemuxContext; @@ -70,7 +73,7 @@ static int aa_read_header(AVFormatContext *s) uint32_t nkey, nval, toc_size, npairs, header_seed = 0, start; char key[128], val[128], codec_name[64] = {0}; uint8_t output[24], dst[8], src[8]; - int64_t largest_size = -1, current_size = -1; + int64_t largest_size = -1, current_size = -1, chapter_pos; struct toc_entry { uint32_t offset; uint32_t size; @@ -173,19 +176,23 @@ static int aa_read_header(AVFormatContext *s) st->codecpar->codec_id = AV_CODEC_ID_MP3; st->codecpar->sample_rate = 22050; st->need_parsing = AVSTREAM_PARSE_FULL_RAW; - st->start_time = 0; + avpriv_set_pts_info(st, 64, 8, 32000 * TIMEPREC); } else if (!strcmp(codec_name, "acelp85")) { st->codecpar->codec_id = AV_CODEC_ID_SIPR; st->codecpar->block_align = 19; st->codecpar->channels = 1; st->codecpar->sample_rate = 8500; + st->codecpar->bit_rate = 8500; st->need_parsing = AVSTREAM_PARSE_FULL_RAW; + avpriv_set_pts_info(st, 64, 8, 8500 * TIMEPREC); } else if (!strcmp(codec_name, "acelp16")) { st->codecpar->codec_id = AV_CODEC_ID_SIPR; st->codecpar->block_align = 20; st->codecpar->channels = 1; st->codecpar->sample_rate = 16000; + st->codecpar->bit_rate = 16000; st->need_parsing = AVSTREAM_PARSE_FULL_RAW; + avpriv_set_pts_info(st, 64, 8, 16000 * TIMEPREC); } /* determine, and jump to audio start offset */ @@ -198,7 +205,30 @@ static int aa_read_header(AVFormatContext *s) } start = TOC[largest_idx].offset; avio_seek(pb, start, SEEK_SET); + + // extract chapter positions. since all formats have constant bit rate, use it + // as time base in bytes/s, for easy stream position <-> timestamp conversion + st->start_time = 0; + c->content_start = start; c->content_end = start + largest_size; + + while ((chapter_pos = avio_tell(pb)) >= 0 && chapter_pos < c->content_end) { + int chapter_idx, chapter_size; + chapter_idx = s->nb_chapters; + chapter_pos -= start + CHAPTER_HEADER_SIZE * chapter_idx; + chapter_size = avio_rb32(pb); + if (chapter_size == 0) break; + avio_skip(pb, 4); + avio_skip(pb, chapter_size); + if (!avpriv_new_chapter(s, chapter_idx, st->time_base, + chapter_pos * TIMEPREC, (chapter_pos + chapter_size) * TIMEPREC, NULL)) + return AVERROR(ENOMEM); + } + + st->duration = (largest_size - CHAPTER_HEADER_SIZE * s->nb_chapters) * TIMEPREC; + + ff_update_cur_dts(s, st, 0); + avio_seek(pb, start, SEEK_SET); c->current_chapter_size = 0; return 0; @@ -215,9 +245,10 @@ static int aa_read_packet(AVFormatContext *s, AVPacket *pkt) int written = 0; int ret; AADemuxContext *c = s->priv_data; + uint64_t pos = avio_tell(s->pb); // are we at the end of the audio content? - if (avio_tell(s->pb) >= c->content_end) { + if (pos >= c->content_end) { return AVERROR_EOF; } @@ -230,6 +261,7 @@ static int aa_read_packet(AVFormatContext *s, AVPacket *pkt) av_log(s, AV_LOG_DEBUG, "Chapter %d (%" PRId64 " bytes)\n", c->chapter_idx, c->current_chapter_size); c->chapter_idx = c->chapter_idx + 1; avio_skip(s->pb, 4); // data start offset + pos += 8; c->current_codec_second_size = c->codec_second_size; } @@ -267,10 +299,56 @@ static int aa_read_packet(AVFormatContext *s, AVPacket *pkt) if (ret < 0) return ret; memcpy(pkt->data, buf, written); + pkt->pos = pos; return 0; } +static int aa_read_seek(AVFormatContext *s, + int stream_index, int64_t timestamp, int flags) +{ + AADemuxContext *c = s->priv_data; + AVChapter *ch; + int64_t chapter_pos, chapter_start, chapter_size; + int chapter_idx = 0; + + // find chapter containing seek timestamp + if (timestamp < 0) + timestamp = 0; + + while (chapter_idx < s->nb_chapters && timestamp >= s->chapters[chapter_idx]->end) { + ++chapter_idx; + } + + if (chapter_idx >= s->nb_chapters) { + chapter_idx = s->nb_chapters - 1; + if (chapter_idx < 0) return -1; // there is no chapter. + timestamp = s->chapters[chapter_idx]->end; + } + + ch = s->chapters[chapter_idx]; + + // sync by clamping timestamp to nearest valid block position in its chapter + chapter_size = ch->end / TIMEPREC - ch->start / TIMEPREC; + chapter_pos = av_rescale_rnd((timestamp - ch->start) / TIMEPREC, + 1, c->codec_second_size, + (flags & AVSEEK_FLAG_BACKWARD) ? AV_ROUND_DOWN : AV_ROUND_UP) + * c->codec_second_size; + if (chapter_pos >= chapter_size) + chapter_pos = chapter_size; + chapter_start = c->content_start + (ch->start / TIMEPREC) + CHAPTER_HEADER_SIZE * (1 + chapter_idx); + + // reinit read state + avio_seek(s->pb, chapter_start + chapter_pos, SEEK_SET); + c->current_codec_second_size = c->codec_second_size; + c->current_chapter_size = chapter_size - chapter_pos; + c->chapter_idx = 1 + chapter_idx; + + ff_update_cur_dts(s, s->streams[0], ch->start + chapter_pos * TIMEPREC); + + return 1; +} + static int aa_probe(AVProbeData *p) { uint8_t *buf = p->buf; @@ -316,6 +394,7 @@ AVInputFormat ff_aa_demuxer = { .read_probe = aa_probe, .read_header = aa_read_header, .read_packet = aa_read_packet, + .read_seek = aa_read_seek, .read_close = aa_read_close, - .flags = AVFMT_GENERIC_INDEX, + .flags = AVFMT_NO_BYTE_SEEK | AVFMT_NOGENSEARCH, };