From patchwork Thu Oct 17 18:59:08 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 15819 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 3AD4344A238 for ; Thu, 17 Oct 2019 22:00:13 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1F30168AB08; Thu, 17 Oct 2019 22:00:13 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qt1-f195.google.com (mail-qt1-f195.google.com [209.85.160.195]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 29D9B68AAE8 for ; Thu, 17 Oct 2019 22:00:06 +0300 (EEST) Received: by mail-qt1-f195.google.com with SMTP id m61so5161347qte.7 for ; Thu, 17 Oct 2019 12:00:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=vjFVu5dDJvveR8Bso3FtITGiSIOGnrfE7Sazl8Udils=; b=W3j/K5w9zoa7wKX5OHaI1bnMUEzw0Sf6gxWWfNcPKdHT5NP84VsE47QU90H+Jy+ehP uNJRXt1V0nrRrj5E4H6T9fkwVSCLlZ8xlKiYROw32aMWMcWERCjtBNHRiBHrhPKjykYc J+9fHG3u6t4bbQ8Tfeh84X4QKFo9I44bDo1sTJLqOXSKdAxsGrJ7Z69cfx7h88gGnn91 1iRqRksL1ubL8tAu4FnyasKNe9x3N+o70ykzb6bcVuG6qZp77fZUb88grrM18LGJm9F9 VPMiZGAoYHchZ7YgrLelcofSu+8w/+xP+JZSfDXc7UXQpVw4rOwHlC6hsD8hr62k4jTT 4/7g== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=vjFVu5dDJvveR8Bso3FtITGiSIOGnrfE7Sazl8Udils=; b=eD1OSZyN9wTVr5awzwGc5Fl75DGSNXXdznaVQv0Af0dv3Afas5eCFERCSDrswCVpye tlAfoB4JWsKq+6qSF7pOnJZihncKNQkB/QfhnpUuryhsUkvq8J5H1iEmdL4xsBrwX/zx l7fh9MePe8iJ7ROMlHyRP+MRaI3iqearnXABabc6sFtaR+0zxYwjAeZ+0QSpEU2jWa45 HS2VoNeB/tNnnlZ2gYmHYjSU13aCe1uCtzQJzul2/KtNDGP2mk250WZEWb7BYUJ1IhV0 ts+Qc/OCuRBjzfY7I3tM00dV5yAr6CGmcWtBn2KmYLYOS8LCoXs13RR5Hc6NGkriYMnp sSkg== X-Gm-Message-State: APjAAAXUsBKlFFQqY8Q/pFdsjbcoQ0fKhTB7KVUiqNt6AfjhfOK0pTWu CoCQCH3HakqITGJYJkN4TOmsX3On X-Google-Smtp-Source: APXvYqwLPabu1MWEEMT0g9Ct/rkZUY2m80Cgzdakzd435mYCkEf2SPNulI6HXoc6Df+OCfc8N+uq+w== X-Received: by 2002:ac8:73cf:: with SMTP id v15mr5529150qtp.131.1571338804814; Thu, 17 Oct 2019 12:00:04 -0700 (PDT) Received: from localhost.localdomain ([191.83.221.234]) by smtp.gmail.com with ESMTPSA id v141sm798933qka.59.2019.10.17.12.00.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Oct 2019 12:00:04 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Thu, 17 Oct 2019 15:59:08 -0300 Message-Id: <20191017185916.2957-4-jamrial@gmail.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191017185916.2957-1-jamrial@gmail.com> References: <20191017185916.2957-1-jamrial@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 03/11] avformat/dashenc: allow splitting fragments following P-Frame reordering 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Currently implemented by using the pic_type value from AV_PKT_DATA_QUALITY_STATS packet side data. Signed-off-by: James Almer --- A (probably much slower) alternative would be inserting an AVCodecParserContext, which would also enable this kind of fragment splitting for non encoding scenarios. libavformat/dashenc.c | 46 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/libavformat/dashenc.c b/libavformat/dashenc.c index 91487c48d9..7be22b268f 100644 --- a/libavformat/dashenc.c +++ b/libavformat/dashenc.c @@ -62,6 +62,7 @@ enum { FRAG_TYPE_NONE = 0, FRAG_TYPE_EVERY_FRAME, FRAG_TYPE_DURATION, + FRAG_TYPE_PFRAMES, FRAG_TYPE_NB }; @@ -100,6 +101,7 @@ typedef struct OutputStream { Segment **segments; int64_t first_pts, start_pts, max_pts; int64_t last_dts, last_pts; + int last_flags; int bit_rate; SegmentType segment_type; /* segment type selected for this particular stream */ const char *format_name; @@ -115,6 +117,7 @@ typedef struct OutputStream { char temp_path[1024]; double availability_time_offset; int total_pkt_size; + int total_pkt_duration; int muxer_overhead; } OutputStream; @@ -1427,6 +1430,10 @@ static int dash_init(AVFormatContext *s) av_log(s, AV_LOG_WARNING, "no video stream and no seg duration set\n"); return AVERROR(EINVAL); } + if (!c->has_video && c->frag_type == FRAG_TYPE_PFRAMES) { + av_log(s, AV_LOG_WARNING, "no video stream and P-frame fragmentation set\n"); + return AVERROR(EINVAL); + } c->nr_of_streams_flushed = 0; @@ -1695,6 +1702,7 @@ static int dash_flush(AVFormatContext *s, int final, int stream) av_rescale_q(os->max_pts - os->start_pts, st->time_base, AV_TIME_BASE_Q); os->total_pkt_size = 0; + os->total_pkt_duration = 0; if (!os->bit_rate) { // calculate average bitrate of first segment @@ -1796,9 +1804,13 @@ static int dash_write_packet(AVFormatContext *s, AVPacket *pkt) sizeof(c->availability_start_time)); } + if (!os->packets_written) + os->availability_time_offset = 0; + if (!os->availability_time_offset && (c->frag_type == FRAG_TYPE_NONE || c->frag_type == FRAG_TYPE_DURATION || + (c->frag_type == FRAG_TYPE_PFRAMES && st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO) || (c->frag_type == FRAG_TYPE_EVERY_FRAME && pkt->duration))) { int64_t frame_duration = 0; @@ -1863,11 +1875,40 @@ static int dash_write_packet(AVFormatContext *s, AVPacket *pkt) os->max_pts = pkt->pts + pkt->duration; else os->max_pts = FFMAX(os->max_pts, pkt->pts + pkt->duration); - os->packets_written++; - os->total_pkt_size += pkt->size; + + if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && + c->frag_type == FRAG_TYPE_PFRAMES && + os->packets_written) { + int side_size, pic_type = 0; + uint8_t *side = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, &side_size); + if (side && side_size > 4) + pic_type = side[4]; + + if ((pic_type == AV_PICTURE_TYPE_P && + st->codecpar->video_delay && + !(os->last_flags & AV_PKT_FLAG_KEY)) || + pkt->flags & AV_PKT_FLAG_KEY) { + ret = av_write_frame(os->ctx, NULL); + if (ret < 0) + return ret; + + if (!os->availability_time_offset) { + int64_t frag_duration = av_rescale_q(os->total_pkt_duration, st->time_base, + AV_TIME_BASE_Q); + os->availability_time_offset = ((double) os->seg_duration - + frag_duration) / AV_TIME_BASE; + } + } + } + if ((ret = ff_write_chained(os->ctx, 0, pkt, s, 0)) < 0) return ret; + os->packets_written++; + os->total_pkt_size += pkt->size; + os->total_pkt_duration += pkt->duration; + os->last_flags = pkt->flags; + if (!os->init_range_length) flush_init_segment(s, os); @@ -1994,6 +2035,7 @@ static const AVOption options[] = { { "none", "one fragment per segment", 0, AV_OPT_TYPE_CONST, {.i64 = FRAG_TYPE_NONE }, 0, UINT_MAX, E, "frag_type"}, { "every_frame", "fragment at every frame", 0, AV_OPT_TYPE_CONST, {.i64 = FRAG_TYPE_EVERY_FRAME }, 0, UINT_MAX, E, "frag_type"}, { "duration", "fragment at specific time intervals", 0, AV_OPT_TYPE_CONST, {.i64 = FRAG_TYPE_DURATION }, 0, UINT_MAX, E, "frag_type"}, + { "pframes", "fragment at keyframes and following P-Frame reordering (Video only, experimental)", 0, AV_OPT_TYPE_CONST, {.i64 = FRAG_TYPE_PFRAMES }, 0, UINT_MAX, E, "frag_type"}, { "remove_at_exit", "remove all segments when finished", OFFSET(remove_at_exit), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, E }, { "use_template", "Use SegmentTemplate instead of SegmentList", OFFSET(use_template), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, E }, { "use_timeline", "Use SegmentTimeline in SegmentTemplate", OFFSET(use_timeline), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, E },