From patchwork Sat Jan 27 23:01:14 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Karsten Otto X-Patchwork-Id: 7428 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.156.27 with SMTP id q27csp2288800jak; Sat, 27 Jan 2018 15:01:58 -0800 (PST) X-Google-Smtp-Source: AH8x226hGPK0HuY1TftLPBnDJkF6RxRFN/K8UN9ILVltOGQWGhWQ8dsAoqDPwcHmLwFfPlxMAQ2X X-Received: by 10.223.171.24 with SMTP id q24mr604900wrc.206.1517094118044; Sat, 27 Jan 2018 15:01:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517094118; cv=none; d=google.com; s=arc-20160816; b=VoM2wfP02wH0cynzEgQt+ad7f957o1PA0+zhH00DOkxjhLEMbDt0GfxpCORIt6udoa 5ymBLulXv1F2PEKmiADpMiUOkJyUaTBRYwVkJPmu7zxUbrFVr/HxkkOK+N9QgFHn06s8 0VhY8hR/CIQkQxOVFshJSmdqer4qhbSyZcjloJYQnr6DuEqgfmozprIoXVpZNfywmVWf SYj1khTh1C0p4rkkjCaveVHm4fgk6Ilpawx899w8tIZ38Ma9f0mUa1RfBHPFMqkr2AQ0 ys8txdU/MQX+LKz6Q+VYeGUIUK/H2nGVezsK7SyamBKc0+Fo1hCpUD14b2Ztjzd7bPLR pILg== 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=r/iQIlEAH72C/yufxKNgN1L9dW8POx/a0DeN3y+WO9I=; b=B+g0FjyXKyRg1B1TZzBEFexdVRHuYqjrI73gzDm6vVce+c1fdRp3opD6zeJp775R1c SAFE6x5UdQrKkdA5xsYtLuOgfi7j1DdvU/P7QOb4xil3Ke0hZ0LIUZe019IrMwEiKnUi 0tzEzsIunnTyFRG3vDtb5/y2o3MGkJwNDzlqG3Y4XFvgDcxoMlpgKs85U/gwlcjzuA/H HyYUQdqqGgZ2ScO2TUgRGkwZketz9ygOR64k8pAhutGa+9wgOKaN7j52vLbDvfrHre3u P4vctFGk7C8jfbVgNPaPFhGD8J5PAMlvZt17ph8SYlX2yQ1VhuwnGbx0wuQLWumxkUPs nSQA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@posteo.de header.s=2017 header.b=c2KKXAPI; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id t67si4754086wmg.85.2018.01.27.15.01.57; Sat, 27 Jan 2018 15:01:58 -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=@posteo.de header.s=2017 header.b=c2KKXAPI; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 26EE16804C8; Sun, 28 Jan 2018 01:01:38 +0200 (EET) 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 19526680348 for ; Sun, 28 Jan 2018 01:01:29 +0200 (EET) Received: from submission (posteo.de [89.146.220.130]) by mout01.posteo.de (Postfix) with ESMTPS id A6B0820E88 for ; Sun, 28 Jan 2018 00:01:32 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.de; s=2017; t=1517094092; bh=N0vOWbnrey0Tvn5rM9/HuhrJfOG06KhB/1QsQ33MPjc=; h=From:To:Subject:Date:From; b=c2KKXAPIA2gFeiNA1c4hy11qOIj94eX7hmnRwS9aC4EK81DnYVSNXfD416cxpZLAk PasebJ21jCjSMEhE3DlCGOSip5qt0Bx2LMKi80ZYmW5jVMRLBYolAccvcixrShCEo3 mtdyolb50P/CYcganZj658TuW6OmLQHICKGxWvvLsuFSc2v5lHxKanZYu/JArGh9m4 bCPllWnm+gxDjnJS7kphnLm3JpdIcx94Ioyaxwe/IpdIsAwP+4Vw5mzkcU+i9PaRJ2 HSmQQnpG3gyQbuAXKrh3BL7DIfxMMe/udDG9xEq2oPtB9tJQU3KYRuP1uGavRnaidC AuBchLBBwTs1Q== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 3zTWVM46mdz9rxN for ; Sun, 28 Jan 2018 00:01:31 +0100 (CET) From: Karsten Otto To: ffmpeg-devel@ffmpeg.org Date: Sun, 28 Jan 2018 00:01:14 +0100 Message-Id: <20180127230114.82506-3-ottoka@posteo.de> X-Mailer: git-send-email 2.14.3 (Apple Git-98) In-Reply-To: <20180127230114.82506-1-ottoka@posteo.de> References: <20180127230114.82506-1-ottoka@posteo.de> Subject: [FFmpeg-devel] [PATCH 2/2] 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 to 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 | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 3 deletions(-) diff --git a/libavformat/aadec.c b/libavformat/aadec.c index d6b6c125da..2480781df0 100644 --- a/libavformat/aadec.c +++ b/libavformat/aadec.c @@ -35,6 +35,7 @@ #define MAX_TOC_ENTRIES 16 #define MAX_DICTIONARY_ENTRIES 128 #define TEA_BLOCK_SIZE 8 +#define CHAPTER_HEADER_SIZE 8 typedef struct AADemuxContext { AVClass *class; @@ -46,6 +47,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 +72,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; @@ -172,19 +174,23 @@ static int aa_read_header(AVFormatContext *s) if (!strcmp(codec_name, "mp332")) { st->codecpar->codec_id = AV_CODEC_ID_MP3; st->codecpar->sample_rate = 22050; + st->time_base = av_make_q(8, 32000); st->need_parsing = AVSTREAM_PARSE_FULL_RAW; - st->start_time = 0; } 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->time_base = av_make_q(8, 8500); st->need_parsing = AVSTREAM_PARSE_FULL_RAW; } 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->time_base = av_make_q(8, 16000); st->need_parsing = AVSTREAM_PARSE_FULL_RAW; } @@ -198,7 +204,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, chapter_pos + chapter_size, NULL)) + return AVERROR(ENOMEM); + } + + st->duration = largest_size - CHAPTER_HEADER_SIZE * s->nb_chapters; + + ff_update_cur_dts(s, st, 0); + avio_seek(pb, start, SEEK_SET); c->current_chapter_size = 0; return 0; @@ -267,6 +296,45 @@ static int aa_read_packet(AVFormatContext *s, AVPacket *pkt) 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; + 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_pos = (timestamp - ch->start) / c->codec_second_size * c->codec_second_size; + chapter_start = c->content_start + ch->start + CHAPTER_HEADER_SIZE * (1 + chapter_idx); + + avio_seek(s->pb, chapter_start + chapter_pos, SEEK_SET); + c->current_codec_second_size = c->codec_second_size; + c->current_chapter_size = ch->end - ch->start; + c->current_chapter_size -= chapter_pos; + c->chapter_idx = 1 + chapter_idx; + + ff_update_cur_dts(s, s->streams[0], ch->start + chapter_pos); + + return 1; +} + static int aa_probe(AVProbeData *p) { uint8_t *buf = p->buf; @@ -312,6 +380,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, };