From patchwork Mon Mar 6 12:02:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhao Zhili X-Patchwork-Id: 40598 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:d046:b0:cd:afd7:272c with SMTP id hv6csp2587534pzb; Sun, 5 Mar 2023 20:03:19 -0800 (PST) X-Google-Smtp-Source: AK7set8FPy2fO5lARE3bUL74h7qiA1fxHGYp+V4QYm2774n8o9pU/TW6woomlytPDlagdzVS5jvp X-Received: by 2002:a17:906:58c3:b0:878:625c:c8ff with SMTP id e3-20020a17090658c300b00878625cc8ffmr11669171ejs.54.1678075399474; Sun, 05 Mar 2023 20:03:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678075399; cv=none; d=google.com; s=arc-20160816; b=d4kwAyOrv+OBmS1QXMOxlm9B/xcvwvEsYILGHILYD6/fLjgJa/pp2Vh7wXSbe8NBCt i1rLf+3nZnDsFLJEWrmTrN00HgPvpwGcC6pwDyORnu/9Oyrev1daXo3qeFXA1N0Cozf0 nxOZ0r/FQnTf6SBAWmRvw2cyo/T/+l4rJ47ijD5Yo4lGRJaeG3V3Td9pkm5vixyZGDrr h+ZUDSETjAnz2OLBJfDLQLGFhCAxNEqwOf3nTstTEIM/GsojZAcejuOr4NCzbSmpljD1 lPWS6JV9zUNsuaK/63yGIFytal+V+CZggYDE51zci7N3KZl+Sq8w71C5b3rm9lmEe4ni lDCQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to:date :to:from:message-id:dkim-signature:delivered-to; bh=9Fc2Nf7SNe27WtiA5uIXqhm3PlBJ4mpfvAVPnlBZtWk=; b=EkhfFc3etIK/eVH9WTzoKGdMoyvLqDzPPQOZwd37SoLHp9vZuVFiVwmUzA+EgjM3x+ cpcoLC56DfaOngtmE6iL1k3Kd58/5gf7gmncKvrpfG+RieKAhGgoXQ5Dc6DIlO4AjnRr w3uouvg9zxziWHRT5S3DTQSShnSlyJhqQbd0dATrYfHKXFRsHOgjAQYIqX3FC6X9EXme 1qv66JVjvO3fzajkVjoHA1IYmjZ9FMXN7NPuhNzNUy8E0Ylb6W8gX+2iCy0MkvqiujYI MGucIoVdkuXPXRfyqKZj96BauDyeGSdUHniGQmST6KIDBSIDVuOUIz3XPZj5KwlGuJp9 aFwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@foxmail.com header.s=s201512 header.b=QaHjuEX7; 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=foxmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id x21-20020a170906b09500b008c1468c314esi8503405ejy.384.2023.03.05.20.03.18; Sun, 05 Mar 2023 20:03:19 -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=@foxmail.com header.s=s201512 header.b=QaHjuEX7; 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=foxmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A420168BB94; Mon, 6 Mar 2023 06:02:26 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from out203-205-221-173.mail.qq.com (out203-205-221-173.mail.qq.com [203.205.221.173]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id ED2F6688373 for ; Mon, 6 Mar 2023 06:02:14 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1678075331; bh=c+neZnYx+Xte1WkPQPWehO5ruJPyRVyG9D34oW2P4rM=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=QaHjuEX7XRDGNq4KZ7O1u4H3J94m7moyUVx7Ty1wOq1t83RuNk+p9b1Sri11mX8fO XmrdM86UnyCkFEe2hTR0kmPE8+m8JcyUwBihxg/htbWYe5kNQfPOgq45+ql3Xo8g+o hZ7lc9vDsr9JZiHvDemC7qfwlhlZUerj/Kw8KtIQ= Received: from localhost.localdomain ([119.147.10.201]) by newxmesmtplogicsvrsza12-0.qq.com (NewEsmtp) with SMTP id 88210DA; Mon, 06 Mar 2023 12:02:08 +0800 X-QQ-mid: xmsmtpt1678075330t6bqhu9uw Message-ID: X-QQ-XMAILINFO: NafziRg7Bx69dhUgfJJ75B3eA7Crybn4pJbTUXoUmneRgPBs2khNb0TcqSSdrb HvUBXG32Z6ZGDwo+HQ0Z5Kg5AzAVnWjH6qVIl/qidUS3wUE3eAtylXHQ1rQ2R3MJngaHSzEXN7bI niW5ibzpG1rjMERFNqJgTF2eMyP4B8ZeJPclfPsgPkRB279KZvwK6osmSMO6Po008p9tPuGIdNGb Vfjps0c29EM8HpHyjZeL28HUeFJChs0QqwTBPsF3Nf3jp+bGzSRq0mHnJeTnmhh1YKGj60D7N/Ku CPzrZ2KL5xBO2AUOo8BjU65sdHI/D+7f5EmSwpBr43ZA6SmO13i6oESHAwesK2b7QAIpmUe1RRlN DaWBCMf4m3Rj0O1pdQZq1HDHnzJ1B3KeEEvxczqCAyK8N/4CvadBf1Iheb614nZgf8ttQP1o1Row dn0BDMrIxu28CCkcowL3ZtnwekikP1LnNW96ygQjPHf63gZYbej9tEWqVxY7JdWtf8dexNayRVGj afEizpa5nw32OAOlZYphqv35fGwW2K1TgLFnzUgTNBokrM4hH+K9rYEkS6jGSKkL2sw5obEWArWb qQinTAafkMW5SKEaiLKmQx7Jaq8jfKHmjqOg8Lj5n3w46pCuKsGnliFkVFgTBVMV5kU7lhRceLdD m2LPvkk1xOn3mBX/SnhoHYkopQNX88MOWsCNBYumL4X0JiIfJ0e9wLNf5Zm/u/eCb9liDbXa1oV0 8pX2lqfOY+kY6Pz91MZJcwTWjKu4hHvsBqzc3AfuW7mIpzQqUJ1FXh2XO8nmL5NTniHce10cGUb0 JWExnQVAGNLxoLSLe1o+ZtZ+WbYCSaEnwhju/PtvKYezz0gLUNdo7Uq1uahLCNojR2OWxshErtnv bxRYoXkPN58BT/ZiKUG7DKIqv5NUjI2FEDiNdh3+BOutHG0NHIc/LVSXxySTdupjUBltD2t9v9U4 FoN8WmOpWiMTJozCTfG87eeHcLbQsrszRS6RsKLunBlYx6Mi/kDzTVEIaG5XP9/VEt5SC6AGo= From: Zhao Zhili To: ffmpeg-devel@ffmpeg.org Date: Mon, 6 Mar 2023 20:02:09 +0800 X-OQ-MSGID: <20230306120211.1249250-4-quinkblack@foxmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230306120211.1249250-1-quinkblack@foxmail.com> References: <20230306120211.1249250-1-quinkblack@foxmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v3 3/5] avformat/mov: parse ISO-14496-12 ChannelLayout X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Cc: Zhao Zhili Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: kPWabxeLDaWd From: Zhao Zhili Only support chnl version 0 now. Signed-off-by: Zhao Zhili --- libavformat/mov.c | 85 +++++++++++- libavformat/mov_chan.c | 296 +++++++++++++++++++++++++++++++++++++++++ libavformat/mov_chan.h | 26 ++++ 3 files changed, 406 insertions(+), 1 deletion(-) diff --git a/libavformat/mov.c b/libavformat/mov.c index 14528bdcaa..057fd872b1 100644 --- a/libavformat/mov.c +++ b/libavformat/mov.c @@ -940,6 +940,88 @@ static int mov_read_chan(MOVContext *c, AVIOContext *pb, MOVAtom atom) return 0; } +static int mov_read_chnl(MOVContext *c, AVIOContext *pb, MOVAtom atom) +{ + int64_t end = av_sat_add64(avio_tell(pb), atom.size); + int stream_structure; + int version, flags; + int ret = 0; + AVStream *st; + + if (c->fc->nb_streams < 1) + return 0; + st = c->fc->streams[c->fc->nb_streams-1]; + + version = avio_r8(pb); + flags = avio_rb24(pb); + if (version != 0 || flags != 0) { + av_log(c->fc, AV_LOG_ERROR, + "Unsupported 'chnl' box with version %d, flags: %#x", + version, flags); + return AVERROR_INVALIDDATA; + } + + stream_structure = avio_r8(pb); + + // stream carries channels + if (stream_structure & 1) { + int layout = avio_r8(pb); + + av_log(c->fc, AV_LOG_TRACE, "'chnl' layout %d\n", layout); + if (!layout) { + uint8_t *positions = av_malloc(st->codecpar->ch_layout.nb_channels); + + if (!positions) + return AVERROR(ENOMEM); + for (int i = 0; i < st->codecpar->ch_layout.nb_channels; i++) { + int speaker_pos = avio_r8(pb); + + av_log(c->fc, AV_LOG_TRACE, "speaker_position %d\n", speaker_pos); + if (speaker_pos == 126) { // explicit position + avpriv_request_sample(c->fc, "explicit position"); + av_freep(&positions); + return AVERROR_PATCHWELCOME; + } else { + positions[i] = speaker_pos; + } + } + + ret = ff_mov_get_layout_from_channel_positions(positions, + st->codecpar->ch_layout.nb_channels, + &st->codecpar->ch_layout); + av_freep(&positions); + if (ret) { + av_log(c->fc, AV_LOG_ERROR, + "get channel layout from speaker positions failed, %s\n", + av_err2str(ret)); + return ret; + } + } else { + uint64_t omitted_channel_map = avio_rb64(pb); + + if (omitted_channel_map) { + avpriv_request_sample(c->fc, "omitted_channel_map 0x%" PRIx64 " != 0", + omitted_channel_map); + return AVERROR_PATCHWELCOME; + } + ff_mov_get_channel_layout_from_config(layout, &st->codecpar->ch_layout); + } + } + + // stream carries objects + if (stream_structure & 2) { + int obj_count = avio_r8(pb); + av_log(c->fc, AV_LOG_TRACE, "'chnl' with object_count %d\n", obj_count); + } + + if (avio_tell(pb) != end) { + av_log(c->fc, AV_LOG_WARNING, "skip %" PRId64 " bytes of unknown data inside chnl\n", + end - avio_tell(pb)); + avio_seek(pb, end, SEEK_SET); + } + return ret; +} + static int mov_read_wfex(MOVContext *c, AVIOContext *pb, MOVAtom atom) { AVStream *st; @@ -7817,7 +7899,8 @@ static const MOVParseTableEntry mov_default_parse_table[] = { { MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */ { MKTAG('w','f','e','x'), mov_read_wfex }, { MKTAG('c','m','o','v'), mov_read_cmov }, -{ MKTAG('c','h','a','n'), mov_read_chan }, /* channel layout */ +{ MKTAG('c','h','a','n'), mov_read_chan }, /* channel layout from quicktime */ +{ MKTAG('c','h','n','l'), mov_read_chnl }, /* channel layout from ISO-14496-12 */ { MKTAG('d','v','c','1'), mov_read_dvc1 }, { MKTAG('s','g','p','d'), mov_read_sgpd }, { MKTAG('s','b','g','p'), mov_read_sbgp }, diff --git a/libavformat/mov_chan.c b/libavformat/mov_chan.c index f66bf0df7f..df17976e59 100644 --- a/libavformat/mov_chan.c +++ b/libavformat/mov_chan.c @@ -551,3 +551,299 @@ int ff_mov_read_chan(AVFormatContext *s, AVIOContext *pb, AVStream *st, return 0; } + +/* ISO/IEC 23001-8, 8.2 */ +static const AVChannelLayout iso_channel_configuration[] = { + // 0: any setup + {}, + + // 1: centre front + AV_CHANNEL_LAYOUT_MONO, + + // 2: left front, right front + AV_CHANNEL_LAYOUT_STEREO, + + // 3: centre front, left front, right front + AV_CHANNEL_LAYOUT_SURROUND, + + // 4: centre front, left front, right front, rear centre + AV_CHANNEL_LAYOUT_4POINT0, + + // 5: centre front, left front, right front, left surround, right surround + AV_CHANNEL_LAYOUT_5POINT0, + + // 6: 5 + LFE + AV_CHANNEL_LAYOUT_5POINT1, + + // 7: centre front, left front centre, right front centre, + // left front, right front, left surround, right surround, LFE + AV_CHANNEL_LAYOUT_7POINT1_WIDE, + + // 8: channel1, channel2 + AV_CHANNEL_LAYOUT_STEREO_DOWNMIX, + + // 9: left front, right front, rear centre + AV_CHANNEL_LAYOUT_2_1, + + // 10: left front, right front, left surround, right surround + AV_CHANNEL_LAYOUT_2_2, + + // 11: centre front, left front, right front, left surround, right surround, rear centre, LFE + AV_CHANNEL_LAYOUT_6POINT1, + + // 12: centre front, left front, right front + // left surround, right surround + // rear surround left, rear surround right + // LFE + AV_CHANNEL_LAYOUT_7POINT1, + + // 13: + AV_CHANNEL_LAYOUT_22POINT2, + + // 14: + AV_CHANNEL_LAYOUT_7POINT1_TOP_BACK, + + // TODO: 15 - 20 +}; + +/* ISO/IEC 23001-8, table 8 */ +static const enum AVChannel iso_channel_position[] = { + // 0: left front + AV_CHAN_FRONT_LEFT, + + // 1: right front + AV_CHAN_FRONT_RIGHT, + + // 2: centre front + AV_CHAN_FRONT_CENTER, + + // 3: low frequence enhancement + AV_CHAN_LOW_FREQUENCY, + + // 4: left surround + // TODO + AV_CHAN_NONE, + + // 5: right surround + // TODO + AV_CHAN_NONE, + + // 6: left front centre + AV_CHAN_FRONT_LEFT_OF_CENTER, + + // 7: right front centre + AV_CHAN_FRONT_RIGHT_OF_CENTER, + + // 8: rear surround left + AV_CHAN_BACK_LEFT, + + // 9: rear surround right + AV_CHAN_BACK_RIGHT, + + // 10: rear centre + AV_CHAN_BACK_CENTER, + + // 11: left surround direct + AV_CHAN_SURROUND_DIRECT_LEFT, + + // 12: right surround direct + AV_CHAN_SURROUND_DIRECT_RIGHT, + + // 13: left side surround + AV_CHAN_SIDE_LEFT, + + // 14: right side surround + AV_CHAN_SIDE_RIGHT, + + // 15: left wide front + AV_CHAN_WIDE_LEFT, + + // 16: right wide front + AV_CHAN_WIDE_RIGHT, + + // 17: left front vertical height + AV_CHAN_TOP_FRONT_LEFT, + + // 18: right front vertical height + AV_CHAN_TOP_FRONT_RIGHT, + + // 19: centre front vertical height + AV_CHAN_TOP_FRONT_CENTER, + + // 20: left surround vertical height rear + AV_CHAN_TOP_BACK_LEFT, + + // 21: right surround vertical height rear + AV_CHAN_TOP_BACK_RIGHT, + + // 22: centre vertical height rear + AV_CHAN_TOP_BACK_CENTER, + + // 23: left vertical height side surround + AV_CHAN_TOP_SIDE_LEFT, + + // 24: right vertical height side surround + AV_CHAN_TOP_SIDE_RIGHT, + + // 25: top centre surround + AV_CHAN_TOP_CENTER, + + // 26: low frequency enhancement 2 + AV_CHAN_LOW_FREQUENCY_2, + + // 27: left front vertical bottom + AV_CHAN_BOTTOM_FRONT_LEFT, + + // 28: right front vertical bottom + AV_CHAN_BOTTOM_FRONT_RIGHT, + + // 29: centre front vertical bottom + AV_CHAN_BOTTOM_FRONT_CENTER, + + // 30: left vertical height surround + // TODO + AV_CHAN_NONE, + + // 31: right vertical height surround + // TODO + AV_CHAN_NONE, + + // 32, 33, 34, 35, reserved + AV_CHAN_NONE, + AV_CHAN_NONE, + AV_CHAN_NONE, + AV_CHAN_NONE, + + // 36: low frequency enhancement 3 + AV_CHAN_NONE, + + // 37: left edge of screen + AV_CHAN_NONE, + // 38: right edge of screen + AV_CHAN_NONE, + // 39: half-way between centre of screen and left edge of screen + AV_CHAN_NONE, + // 40: half-way between centre of screen and right edge of screen + AV_CHAN_NONE, + + // 41: left back surround + AV_CHAN_NONE, + + // 42: right back surround + AV_CHAN_NONE, + + // 43 - 125: reserved + // 126: explicit position + // 127: unknown /undefined +}; + +int ff_mov_get_channel_config_from_layout(const AVChannelLayout *layout, int *config) +{ + // Set default value which means any setup in 23001-8 + *config = 0; + for (int i = 0; i < FF_ARRAY_ELEMS(iso_channel_configuration); i++) { + if (!av_channel_layout_compare(layout, iso_channel_configuration + i)) { + *config = i; + break; + } + } + + return 0; +} + +int ff_mov_get_channel_layout_from_config(int config, AVChannelLayout *layout) +{ + if (config > 0 && config < FF_ARRAY_ELEMS(iso_channel_configuration)) { + av_channel_layout_copy(layout, &iso_channel_configuration[config]); + return 0; + } + + return -1; +} + +int ff_mov_get_channel_positions_from_layout(const AVChannelLayout *layout, + uint8_t *position, int position_num) +{ + enum AVChannel channel; + + if (position_num < layout->nb_channels) + return AVERROR(EINVAL); + + for (int i = 0; i < layout->nb_channels; i++) { + position[i] = 127; + channel = av_channel_layout_channel_from_index(layout, i); + if (channel == AV_CHAN_NONE) + return AVERROR(EINVAL); + + for (int j = 0; j < FF_ARRAY_ELEMS(iso_channel_position); j++) { + if (iso_channel_position[j] == channel) { + position[i] = j; + break; + } + } + if (position[i] == 127) + return AVERROR(EINVAL); + } + + return 0; +} + +int ff_mov_get_layout_from_channel_positions(const uint8_t *position, int position_num, + AVChannelLayout *layout) +{ + int ret; + enum AVChannel channel; + + av_channel_layout_uninit(layout); + + if (position_num <= 63) { + layout->order = AV_CHANNEL_ORDER_NATIVE; + layout->nb_channels = position_num; + for (int i = 0; i < position_num; i++) { + if (position[i] >= FF_ARRAY_ELEMS(iso_channel_position)) { + ret = AVERROR_PATCHWELCOME; + goto error; + } + + channel = iso_channel_position[position[i]]; + // unsupported layout + if (channel == AV_CHAN_NONE) { + ret = AVERROR_PATCHWELCOME; + goto error; + } + + layout->u.mask |= 1ULL << channel; + } + } else { + layout->order = AV_CHANNEL_ORDER_CUSTOM; + layout->nb_channels = position_num; + layout->u.map = av_calloc(position_num, sizeof(*layout->u.map)); + if (!layout->u.map) { + ret = AVERROR(ENOMEM); + goto error; + } + + for (int i = 0; i < position_num; i++) { + if (position[i] >= FF_ARRAY_ELEMS(iso_channel_position)) { + ret = AVERROR_PATCHWELCOME; + goto error; + } + + channel = iso_channel_position[position[i]]; + // unsupported layout + if (channel == AV_CHAN_NONE) { + ret = AVERROR_PATCHWELCOME; + goto error; + } + + layout->u.map[i].id = channel; + } + } + + + return 0; + +error: + av_channel_layout_uninit(layout); + return ret; +} diff --git a/libavformat/mov_chan.h b/libavformat/mov_chan.h index 93d9878798..8c807798ab 100644 --- a/libavformat/mov_chan.h +++ b/libavformat/mov_chan.h @@ -163,4 +163,30 @@ int ff_mov_get_channel_layout_tag(const AVCodecParameters *par, int ff_mov_read_chan(AVFormatContext *s, AVIOContext *pb, AVStream *st, int64_t size); +/** + * Get ISO/IEC 23001-8 ChannelConfiguration from AVChannelLayout. + * + */ +int ff_mov_get_channel_config_from_layout(const AVChannelLayout *layout, int *config); + +/** + * Get AVChannelLayout from ISO/IEC 23001-8 ChannelConfiguration. + * + * @return 0 for success, -1 for doesn't match, layout is untouched on failure + */ + +int ff_mov_get_channel_layout_from_config(int config, AVChannelLayout *layout); + +/** + * Get ISO/IEC 23001-8 OutputChannelPosition from AVChannelLayout. + */ +int ff_mov_get_channel_positions_from_layout(const AVChannelLayout *layout, + uint8_t *position, int position_num); + +/** + * Get AVChannelLayout from ISO/IEC 23001-8 OutputChannelPosition. + */ +int ff_mov_get_layout_from_channel_positions(const uint8_t *position, int position_num, + AVChannelLayout *layout); + #endif /* AVFORMAT_MOV_CHAN_H */