From patchwork Tue Nov 12 02:16:11 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 16213 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 47D244482F3 for ; Tue, 12 Nov 2019 04:17:41 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1FBC768AC9B; Tue, 12 Nov 2019 04:17:41 +0200 (EET) 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 350B6689A29 for ; Tue, 12 Nov 2019 04:17:34 +0200 (EET) Received: by mail-qt1-f195.google.com with SMTP id p20so18064556qtq.5 for ; Mon, 11 Nov 2019 18:17:34 -0800 (PST) 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=31gcOr6ZWIIqQO3vf0e88rApjAVHuPf13NQoFlE5KRo=; b=kmNCZQFV4BglLyvJrWquFCBSMCCHv/iN/vxl2ZCRbcBJKMn8924XrcQaOU3ML5nOwI YQs7DYvC3hlhuCrlg+d+zt0TUQxW3CBos7rykbaQBlwedhGU1nEbENrGYZMWDli4FRET WCMoKh/irS0LXaFwJIbUW9P+u507EPD0HNOqU1sjaWEAnT2lthuVlauBJmQUivi4QpLE o/6xyy8uSoPsVps+LGZ9hXsolJ4ESK53ScMCBX7B29a2yQalM045ffNOeZhpWY5RNzPc iOCsEZiRhYo9L3vOaizy5Ft7KqI8/8IWu30L4R+R0dpuwuErqozrbei1HXCRA9vIrU8j I4iQ== 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=31gcOr6ZWIIqQO3vf0e88rApjAVHuPf13NQoFlE5KRo=; b=J501elO3lJif6ONfFJqIzGNHBdzWF5KmamyLqRQgXSzVSLBaymVd56GaptiIotwwlA sEFZrU0o0VIS/YMP1ersheeGB4rdd7gH+3oKnXJDtn452V7XRvBK1TGHFSKjjeLLbEmj sCJY3wHAhs7LLAQzLbKTAQFofbreUM80u1C/nvMKQNqh1wOQ+Xxa5mSn/O+RQ30GCkFn 9slUUx4UyCN6Dkxq+OxQBDbp+DoxDbvCJGf1lrYmOrSH9SIV8f9Lu4h6z90RwX6OTsH4 mJcroIxPyeB1EXoNwzKwtr/WGDnpivhKA66+W08NKtgPtd+vE+UFMM8Lva+2d4ZNDMI+ s54A== X-Gm-Message-State: APjAAAXg4EztuSRjC0BiPbGdkMow1mDD+UIif3SK6Vy8Ua9Hom7JwHAG 1XCQnwzZXLNt31TphqfhNZ2crjL3 X-Google-Smtp-Source: APXvYqyf+YoZDuUUkcd8RVAGAgXQNcO2FpFJ344mZ3NBBTgus2Ga5g2YYcDNmUhxCoe55HNSqo+04Q== X-Received: by 2002:aed:3bf8:: with SMTP id s53mr28851836qte.373.1573525052199; Mon, 11 Nov 2019 18:17:32 -0800 (PST) Received: from localhost.localdomain ([181.23.83.211]) by smtp.gmail.com with ESMTPSA id p3sm8231011qkf.107.2019.11.11.18.17.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2019 18:17:31 -0800 (PST) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Mon, 11 Nov 2019 23:16:11 -0300 Message-Id: <20191112021611.2645-1-jamrial@gmail.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <9ba62239-1691-b30f-3cb6-bd01005214ce@gmail.com> References: <9ba62239-1691-b30f-3cb6-bd01005214ce@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 5/5 v2] avformat: add an AV1 Annex B 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: James Almer --- configure | 1 + libavformat/Makefile | 1 + libavformat/allformats.c | 1 + libavformat/obu.c | 277 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 280 insertions(+) create mode 100644 libavformat/obu.c diff --git a/configure b/configure index 70f60997c1..a8dbba879d 100755 --- a/configure +++ b/configure @@ -3293,6 +3293,7 @@ mxf_d10_muxer_select="mxf_muxer" mxf_opatom_muxer_select="mxf_muxer" nut_muxer_select="riffenc" nuv_demuxer_select="riffdec" +obu_demuxer_select="av1_frame_merge_bsf" oga_muxer_select="ogg_muxer" ogg_demuxer_select="dirac_parse" ogv_muxer_select="ogg_muxer" diff --git a/libavformat/Makefile b/libavformat/Makefile index 8251f8f657..9057d0358a 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -350,6 +350,7 @@ OBJS-$(CONFIG_NULL_MUXER) += nullenc.o OBJS-$(CONFIG_NUT_DEMUXER) += nutdec.o nut.o isom.o OBJS-$(CONFIG_NUT_MUXER) += nutenc.o nut.o OBJS-$(CONFIG_NUV_DEMUXER) += nuv.o +OBJS-$(CONFIG_OBU_DEMUXER) += obu.o OBJS-$(CONFIG_OGG_DEMUXER) += oggdec.o \ oggparsecelt.o \ oggparsedaala.o \ diff --git a/libavformat/allformats.c b/libavformat/allformats.c index f7fea32b45..152644e9f9 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -282,6 +282,7 @@ extern AVInputFormat ff_nut_demuxer; extern AVOutputFormat ff_nut_muxer; extern AVInputFormat ff_nuv_demuxer; extern AVOutputFormat ff_oga_muxer; +extern AVInputFormat ff_obu_demuxer; extern AVInputFormat ff_ogg_demuxer; extern AVOutputFormat ff_ogg_muxer; extern AVOutputFormat ff_ogv_muxer; diff --git a/libavformat/obu.c b/libavformat/obu.c new file mode 100644 index 0000000000..a8ba8a26f4 --- /dev/null +++ b/libavformat/obu.c @@ -0,0 +1,277 @@ +/* + * AV1 Annex B demuxer + * Copyright (c) 2019 James Almer + * + * 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 "config.h" + +#include "libavutil/common.h" +#include "libavutil/opt.h" +#include "libavcodec/av1_parse.h" +#include "avformat.h" +#include "avio_internal.h" +#include "internal.h" + +typedef struct AnnexBContext { + const AVClass *class; + AVBSFContext *bsf; + uint32_t temporal_unit_size; + uint32_t frame_unit_size; + AVRational framerate; +} AnnexBContext; + +static int leb(AVIOContext *pb, uint32_t *len) { + int more, i = 0; + uint8_t byte; + *len = 0; + do { + unsigned bits; + byte = avio_r8(pb); + more = byte & 0x80; + bits = byte & 0x7f; + if (i <= 3 || (i == 4 && bits < (1 << 4))) + *len |= bits << (i * 7); + else if (bits) + return AVERROR_INVALIDDATA; + if (++i == 8 && more) + return AVERROR_INVALIDDATA; + if (pb->eof_reached || pb->error) + return pb->error ? pb->error : AVERROR(EIO); + } while (more); + return i; +} + +static int read_obu(const uint8_t *buf, int size, int64_t *obu_size, int *type) +{ + int start_pos, temporal_id, spatial_id; + int len; + + len = parse_obu_header(buf, size, obu_size, &start_pos, + type, &temporal_id, &spatial_id); + if (len < 0) + return len; + + return 0; +} + +static int annexb_probe(const AVProbeData *p) +{ + AVIOContext pb; + int64_t obu_size; + uint32_t temporal_unit_size, frame_unit_size, obu_unit_size; + int seq = 0, frame_header = 0; + int ret, type, cnt = 0; + + ffio_init_context(&pb, p->buf, p->buf_size, 0, + NULL, NULL, NULL, NULL); + + ret = leb(&pb, &temporal_unit_size); + if (ret < 0) + return 0; + cnt += ret; + ret = leb(&pb, &frame_unit_size); + if (ret < 0 || ((int64_t)frame_unit_size + ret) > temporal_unit_size) + return 0; + cnt += ret; + temporal_unit_size -= ret; + ret = leb(&pb, &obu_unit_size); + if (ret < 0 || ((int64_t)obu_unit_size + ret) >= frame_unit_size) + return 0; + cnt += ret; + + temporal_unit_size -= obu_unit_size + ret; + frame_unit_size -= obu_unit_size + ret; + + avio_skip(&pb, obu_unit_size); + if (pb.eof_reached || pb.error) + return 0; + + // Check that the first OBU is a Temporal Delimiter. + ret = read_obu(p->buf + cnt, FFMIN(p->buf_size - cnt, obu_unit_size), &obu_size, &type); + if (ret < 0 || type != AV1_OBU_TEMPORAL_DELIMITER || obu_size > 0) + return 0; + cnt += obu_unit_size; + + do { + ret = leb(&pb, &obu_unit_size); + if (ret < 0 || ((int64_t)obu_unit_size + ret) > frame_unit_size) + return 0; + cnt += ret; + + avio_skip(&pb, obu_unit_size); + if (pb.eof_reached || pb.error) + return 0; + + ret = read_obu(p->buf + cnt, FFMIN(p->buf_size - cnt, obu_unit_size), &obu_size, &type); + if (ret < 0) + return 0; + cnt += obu_unit_size; + + if (type == AV1_OBU_SEQUENCE_HEADER) + seq = 1; + if (type == AV1_OBU_FRAME || type == AV1_OBU_FRAME_HEADER) { + if (frame_header || !seq) + return 0; + frame_header = 1; + break; + } + if (type == AV1_OBU_TILE_GROUP && !frame_header) + return 0; + + temporal_unit_size -= obu_unit_size + ret; + frame_unit_size -= obu_unit_size + ret; + } while (!frame_header && frame_unit_size); + + return frame_header ? AVPROBE_SCORE_EXTENSION + 1 : 0; +} + +static int annexb_read_header(AVFormatContext *s) +{ + AnnexBContext *c = s->priv_data; + const AVBitStreamFilter *filter = av_bsf_get_by_name("av1_frame_merge"); + AVStream *st; + int ret; + + if (!filter) { + av_log(c, AV_LOG_ERROR, "av1_frame_merge bitstream filter " + "not found. This is a bug, please report it.\n"); + return AVERROR_BUG; + } + + st = avformat_new_stream(s, NULL); + if (!st) + return AVERROR(ENOMEM); + + st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; + st->codecpar->codec_id = AV_CODEC_ID_AV1; + st->need_parsing = AVSTREAM_PARSE_HEADERS; + + st->internal->avctx->framerate = c->framerate; + // taken from rawvideo demuxers + avpriv_set_pts_info(st, 64, 1, 1200000); + + ret = av_bsf_alloc(filter, &c->bsf); + if (ret < 0) + return ret; + + ret = avcodec_parameters_copy(c->bsf->par_in, st->codecpar); + if (ret < 0) { + av_bsf_free(&c->bsf); + return ret; + } + + ret = av_bsf_init(c->bsf); + if (ret < 0) + av_bsf_free(&c->bsf); + + return ret; +} + +static int annexb_read_packet(AVFormatContext *s, AVPacket *pkt) +{ + AnnexBContext *c = s->priv_data; + uint32_t obu_unit_size; + int ret, len; + +retry: + if (avio_feof(s->pb)) { + if (c->temporal_unit_size || c->frame_unit_size) + return AVERROR(EIO); + av_bsf_send_packet(c->bsf, NULL); + goto end; + } + + if (!c->temporal_unit_size) { + len = leb(s->pb, &c->temporal_unit_size); + if (len < 0) return AVERROR_INVALIDDATA; + } + + if (!c->frame_unit_size) { + len = leb(s->pb, &c->frame_unit_size); + if (len < 0 || ((int64_t)c->frame_unit_size + len) > c->temporal_unit_size) + return AVERROR_INVALIDDATA; + c->temporal_unit_size -= len; + } + + len = leb(s->pb, &obu_unit_size); + if (len < 0 || ((int64_t)obu_unit_size + len) > c->frame_unit_size) + return AVERROR_INVALIDDATA; + + ret = av_get_packet(s->pb, pkt, obu_unit_size); + if (ret < 0) + return ret; + if (ret != obu_unit_size) + return AVERROR(EIO); + + c->temporal_unit_size -= obu_unit_size + len; + c->frame_unit_size -= obu_unit_size + len; + + ret = av_bsf_send_packet(c->bsf, pkt); + if (ret < 0) { + av_log(s, AV_LOG_ERROR, "Failed to send packet to " + "av1_frame_merge filter\n"); + return ret; + } + +end: + ret = av_bsf_receive_packet(c->bsf, pkt); + if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) + av_log(s, AV_LOG_ERROR, "av1_frame_merge filter failed to " + "send output packet\n"); + + if (ret == AVERROR(EAGAIN)) + goto retry; + + return ret; +} + +static int annexb_read_close(AVFormatContext *s) +{ + AnnexBContext *c = s->priv_data; + + av_bsf_free(&c->bsf); + return 0; +} + +#define OFFSET(x) offsetof(AnnexBContext, x) +#define DEC AV_OPT_FLAG_DECODING_PARAM +static const AVOption annexb_options[] = { + { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC}, + { NULL }, +}; + +static const AVClass annexb_demuxer_class = { + .class_name = "AV1 Annex-B demuxer", + .item_name = av_default_item_name, + .option = annexb_options, + .version = LIBAVUTIL_VERSION_INT, +}; + +AVInputFormat ff_obu_demuxer = { + .name = "obu", + .long_name = NULL_IF_CONFIG_SMALL("OBU"), + .priv_data_size = sizeof(AnnexBContext), + .read_probe = annexb_probe, + .read_header = annexb_read_header, + .read_packet = annexb_read_packet, + .read_close = annexb_read_close, + .extensions = "obu", + .flags = AVFMT_GENERIC_INDEX, + .priv_class = &annexb_demuxer_class, +};