From patchwork Thu Aug 19 07:43:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Soft Works X-Patchwork-Id: 29605 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2a4a:0:0:0:0 with SMTP id k10csp64236iov; Thu, 19 Aug 2021 00:43:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz1eA5Hx3I/oLFKqJpeBL81auB78zJauikvFaVnuE0ibokhNL5lLzZX1RI8/qL3WzrA5C8g X-Received: by 2002:a17:906:3947:: with SMTP id g7mr9543494eje.87.1629359010482; Thu, 19 Aug 2021 00:43:30 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id z19si2371921edc.428.2021.08.19.00.43.30; Thu, 19 Aug 2021 00:43:30 -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=@hotmail.com header.s=selector1 header.b=cnBa+0No; arc=fail (body hash mismatch); 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=hotmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 18C8868A03B; Thu, 19 Aug 2021 10:43:22 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11olkn2099.outbound.protection.outlook.com [40.92.19.99]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 730CF68020A for ; Thu, 19 Aug 2021 10:43:15 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=abaFbtF3Wi/TqE00EasXJc+YTRHvzMGNF0HMGPU9oZIDn0bZ8EuSKWX0oyehKE9jKnf22tY14FQQDVb8OCBgmALUOHVt2mJgt1UprTiawNm4H0ViFIorUXr+WLzlVaMU0p6sf4btKVFyXnQZ409s8PxACyPjZ1IfhAPyZInazHB0n3NW/b+ccT6hPY6lxwc76okPFvuWKX1It8H1t4JB5p9jK2pFfQkPjAKbRITY7N0o4Wl3lq84BdqrVN3nSUjYNZj053ZWRhmnecxP0qG0HnnruyC6xb1FyHv4ZEGs8LxmLsEtR/8vvwJh96C6tFD/ICYJ/T++iqHOoeqidVngog== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=96bcPrLEZBrHmXfa900MRbR69huK0v4B70YT2cfCm00=; b=LLMKyqNuEnpwnBxqiSF/dldfHOYChr9cVbJnxvFsFo+oAYNgBPp9OtevOQO/67az+SWysLxmTqktNr2oTDWLC43JzpxQTBPcF628g+2upcljYA3RTO6R5EVsMkjgkH2uqGDod41V7hoJr0whlGYwQMd7aEn1OZgMsSwwgZRKXFfyKHuxel4vJ/ZBCH0vtmK+/aztEqG4KJl+Q9SjX/G7Tng74y2ziUQ0W9AyYsWoFF+PjEE+CJLJ1ZeZMiK4BWLE0J3eESvCOKrOMPS2r5g+wcocWq9dTIxiZ005g8HTMotyn40JKVu54RYjJC8BOBhBTfG+LQlssdvPMZsgs1/bWQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hotmail.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=96bcPrLEZBrHmXfa900MRbR69huK0v4B70YT2cfCm00=; b=cnBa+0NolEcLzSR+zw4wUaaabfOKNSoHUEzsfUM5lC/I+nUH9NLR3273r4HlN2PtbJIXXlLMh7m8EM5XWTRKqwYUWc6PjYJF5GgjWtQPyUms324cDPrODQqhXZrTDlt+bUMHwJXHLS2TyuRzUQ7kbNVNs6xZazBwdLheDXNpkBXiGVoDBRWzkz0/EfbEUHoJWkM07EM+aTZwnScUYJin7zGU7NrCO8xCIfYe6OYPyM76xsUeICHE/RJY5PkoDL0Dny/mutfNpWwSByb6DYkB0aMB7gQhGWkC040bzS5agaqnrqFnjnGHEVq0ctgeXyPITg120P2moBxTafhZaQJR9g== Received: from MN2PR04MB5981.namprd04.prod.outlook.com (2603:10b6:208:da::10) by MN2PR04MB6255.namprd04.prod.outlook.com (2603:10b6:208:d5::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4415.22; Thu, 19 Aug 2021 07:43:09 +0000 Received: from MN2PR04MB5981.namprd04.prod.outlook.com ([fe80::5d83:1c26:c2b1:3a30]) by MN2PR04MB5981.namprd04.prod.outlook.com ([fe80::5d83:1c26:c2b1:3a30%6]) with mapi id 15.20.4415.024; Thu, 19 Aug 2021 07:43:09 +0000 From: Soft Works To: "ffmpeg-devel@ffmpeg.org" Thread-Topic: [PATCH 1/9] lavu/frame: avframe add type property Thread-Index: AdeUhtWMG2LoKHM6QwCJ7VzhSu+5wg== Date: Thu, 19 Aug 2021 07:43:09 +0000 Message-ID: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-messagesentrepresentingtype: 1 x-tmn: [+yTljXQAD8PwWo34JrnsP1J51xVqCIGA] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 38c0d72e-df4c-4ad5-7fa3-08d962e4fd6f x-ms-traffictypediagnostic: MN2PR04MB6255: x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: Rm2Q+x7mtRD1Buy+N8NjJcdRqPVpv1jPnoiSG2R/OKytwbOuyDcxkoyNJaK97BH9DTo4P8sUQIAfaxIsaMZmvum7pynkZJekMihqoi+2vMGrAlIo5bym4NqMWuDjVkHBthGLkqO8H2ZhzfzTjFEU/Kq5glS9reFiKkX/91NiDkI5s3x98pWV31OpPsDmtM9g5bpacubJKskMncj5j8o2a2G6CrJTyeNhwMYFua3s0+phvp5eAOp2xDvD/5YxSMwd9s2f3eboj8Qd6aXdeJ/NPljgYRNgj55liknB7heZRA+beDn9Lp5tusbjvgy46JYqH7W8OR1KV26kUSFfQr3xB8A2smvCi0/39MXSCJ1Bb6nMM1GGP7n1iUxoC0oCB/NmZa99bBtg8zIzL4IZMfMQnU/oF255RujErCkLKUJNUb2vUqKIFk4r3EPLg8FgQzwT x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: FUJeK6OF6yMhnTFGaf8CO3Gw+CHGS9E7xHf6JVTYoyjFq/b8a8sIajii9aBkDUpWuDcTrXtXtAbjFrTmexCLvRtMH0u3/5IvMXu02sXWBuJMCi5sqZPYvC8G+tYJgoCrrmbyI97kxKj2RDgGUzCy4g== x-ms-exchange-transport-forked: True MIME-Version: 1.0 X-OriginatorOrg: sct-15-20-3174-20-msonline-outlook-529c7.templateTenant X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: MN2PR04MB5981.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: 38c0d72e-df4c-4ad5-7fa3-08d962e4fd6f X-MS-Exchange-CrossTenant-originalarrivaltime: 19 Aug 2021 07:43:09.2534 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR04MB6255 Subject: [FFmpeg-devel] [PATCH 1/9] lavu/frame: avframe add type property 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: NJHdb/ULST2w Signed-off-by: softworkz --- libavutil/frame.c | 74 ++++++++++++++++++++++++++++++++++++--------- libavutil/frame.h | 39 ++++++++++++++++++++++-- libavutil/version.h | 2 +- 3 files changed, 97 insertions(+), 18 deletions(-) diff --git a/libavutil/frame.c b/libavutil/frame.c index b0ceaf7145..7d95849cef 100644 --- a/libavutil/frame.c +++ b/libavutil/frame.c @@ -244,22 +244,39 @@ static int get_audio_buffer(AVFrame *frame, int align) } int av_frame_get_buffer(AVFrame *frame, int align) +{ + if (frame->width > 0 && frame->height > 0) + return av_frame_get_buffer2(frame, AVMEDIA_TYPE_VIDEO, align); + else if (frame->nb_samples > 0 && (frame->channel_layout || frame->channels > 0)) + return av_frame_get_buffer2(frame, AVMEDIA_TYPE_AUDIO, align); + + return AVERROR(EINVAL); +} + +int av_frame_get_buffer2(AVFrame *frame, enum AVMediaType type, int align) { if (frame->format < 0) return AVERROR(EINVAL); - if (frame->width > 0 && frame->height > 0) + frame->type = type; + + switch(frame->type) { + case AVMEDIA_TYPE_VIDEO: return get_video_buffer(frame, align); - else if (frame->nb_samples > 0 && (frame->channel_layout || frame->channels > 0)) + case AVMEDIA_TYPE_AUDIO: return get_audio_buffer(frame, align); - - return AVERROR(EINVAL); + case AVMEDIA_TYPE_SUBTITLE: + return 0; + default: + return AVERROR(EINVAL); + } } static int frame_copy_props(AVFrame *dst, const AVFrame *src, int force_copy) { int ret, i; + dst->type = src->type; dst->key_frame = src->key_frame; dst->pict_type = src->pict_type; dst->sample_aspect_ratio = src->sample_aspect_ratio; @@ -331,6 +348,7 @@ int av_frame_ref(AVFrame *dst, const AVFrame *src) av_assert1(dst->width == 0 && dst->height == 0); av_assert1(dst->channels == 0); + dst->type = src->type; dst->format = src->format; dst->width = src->width; dst->height = src->height; @@ -499,6 +517,7 @@ int av_frame_make_writable(AVFrame *frame) return 0; memset(&tmp, 0, sizeof(tmp)); + tmp.type = frame->type; tmp.format = frame->format; tmp.width = frame->width; tmp.height = frame->height; @@ -544,14 +563,22 @@ AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane) uint8_t *data; int planes, i; - if (frame->nb_samples) { - int channels = frame->channels; - if (!channels) - return NULL; - CHECK_CHANNELS_CONSISTENCY(frame); - planes = av_sample_fmt_is_planar(frame->format) ? channels : 1; - } else + switch(frame->type) { + case AVMEDIA_TYPE_VIDEO: planes = 4; + break; + case AVMEDIA_TYPE_AUDIO: + { + int channels = frame->channels; + if (!channels) + return NULL; + CHECK_CHANNELS_CONSISTENCY(frame); + planes = av_sample_fmt_is_planar(frame->format) ? channels : 1; + break; + } + default: + return NULL; + } if (plane < 0 || plane >= planes || !frame->extended_data[plane]) return NULL; @@ -675,17 +702,34 @@ static int frame_copy_audio(AVFrame *dst, const AVFrame *src) return 0; } +static int frame_copy_subtitles(AVFrame *dst, const AVFrame *src) +{ + dst->type = AVMEDIA_TYPE_SUBTITLE; + dst->format = src->format; + + if (src->buf[0]) { + dst->buf[0] = av_buffer_ref(src->buf[0]); + dst->data[0] = src->data[0]; + } + + return 0; +} + int av_frame_copy(AVFrame *dst, const AVFrame *src) { if (dst->format != src->format || dst->format < 0) return AVERROR(EINVAL); - if (dst->width > 0 && dst->height > 0) + switch(dst->type) { + case AVMEDIA_TYPE_VIDEO: return frame_copy_video(dst, src); - else if (dst->nb_samples > 0 && dst->channels > 0) + case AVMEDIA_TYPE_AUDIO: return frame_copy_audio(dst, src); - - return AVERROR(EINVAL); + case AVMEDIA_TYPE_SUBTITLE: + return frame_copy_subtitles(dst, src); + default: + return AVERROR(EINVAL); + } } void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type) diff --git a/libavutil/frame.h b/libavutil/frame.h index ff2540a20f..c104815df9 100644 --- a/libavutil/frame.h +++ b/libavutil/frame.h @@ -271,7 +271,7 @@ typedef struct AVRegionOfInterest { } AVRegionOfInterest; /** - * This structure describes decoded (raw) audio or video data. + * This structure describes decoded (raw) audio, video or subtitle data. * * AVFrame must be allocated using av_frame_alloc(). Note that this only * allocates the AVFrame itself, the buffers for the data must be managed @@ -302,6 +302,13 @@ typedef struct AVRegionOfInterest { */ typedef struct AVFrame { #define AV_NUM_DATA_POINTERS 8 + /** + * Media type of the frame (audio, video, subtitles..) + * + * See AVMEDIA_TYPE_xxx + */ + enum AVMediaType type; + /** * pointer to the picture/channel planes. * This might be different from the first allocated byte @@ -371,7 +378,7 @@ typedef struct AVFrame { /** * format of the frame, -1 if unknown or unset * Values correspond to enum AVPixelFormat for video frames, - * enum AVSampleFormat for audio) + * enum AVSampleFormat for audio, AVSubtitleType for subtitles) */ int format; @@ -721,6 +728,8 @@ void av_frame_move_ref(AVFrame *dst, AVFrame *src); /** * Allocate new buffer(s) for audio or video data. * + * Note: For subtitle data, use av_frame_get_buffer2 + * * The following fields must be set on frame before calling this function: * - format (pixel format for video, sample format for audio) * - width and height for video @@ -743,6 +752,32 @@ void av_frame_move_ref(AVFrame *dst, AVFrame *src); */ int av_frame_get_buffer(AVFrame *frame, int align); +/** + * Allocate new buffer(s) for audio, video or subtitle data. + * + * The following fields must be set on frame before calling this function: + * - format (pixel format for video, sample format for audio) + * - width and height for video + * - nb_samples and channel_layout for audio + * + * This function will fill AVFrame.data and AVFrame.buf arrays and, if + * necessary, allocate and fill AVFrame.extended_data and AVFrame.extended_buf. + * For planar formats, one buffer will be allocated for each plane. + * + * @warning: if frame already has been allocated, calling this function will + * leak memory. In addition, undefined behavior can occur in certain + * cases. + * + * @param frame frame in which to store the new buffers. + * @param type media type to set for the frame. + * @param align Required buffer size alignment. If equal to 0, alignment will be + * chosen automatically for the current CPU. It is highly + * recommended to pass 0 here unless you know what you are doing. + * + * @return 0 on success, a negative AVERROR on error. + */ +int av_frame_get_buffer2(AVFrame *frame, enum AVMediaType type, int align); + /** * Check if the frame data is writable. * diff --git a/libavutil/version.h b/libavutil/version.h index 4b77387b08..1e6a80f86e 100644 --- a/libavutil/version.h +++ b/libavutil/version.h @@ -79,7 +79,7 @@ */ #define LIBAVUTIL_VERSION_MAJOR 57 -#define LIBAVUTIL_VERSION_MINOR 4 +#define LIBAVUTIL_VERSION_MINOR 5 #define LIBAVUTIL_VERSION_MICRO 101 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \