From patchwork Sat Nov 27 19:37:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Soft Works X-Patchwork-Id: 31762 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:d206:0:0:0:0:0 with SMTP id q6csp4018742iob; Sat, 27 Nov 2021 11:38:25 -0800 (PST) X-Google-Smtp-Source: ABdhPJwgS5V9apjvp13UU3H1xkUYXBVgigi50SCAgu8g3b7YCXVzcwlnLXHYNZY1PuZfRfDhpRaV X-Received: by 2002:a17:906:58d3:: with SMTP id e19mr47359346ejs.350.1638041904817; Sat, 27 Nov 2021 11:38:24 -0800 (PST) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id qo11si12924713ejb.347.2021.11.27.11.38.24; Sat, 27 Nov 2021 11:38:24 -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=@hotmail.com header.s=selector1 header.b=Zs2lfeqI; 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 B5F2E68ABAC; Sat, 27 Nov 2021 21:38:00 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11olkn2066.outbound.protection.outlook.com [40.92.20.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 6CFA068AAE6 for ; Sat, 27 Nov 2021 21:37:54 +0200 (EET) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=V00dRsCoeGevHR2cDQZ7aeLfMT6U+K1rfWyFNxeg8fhMupDIi42e/mAZcmtgcxeSERRz4NUV1AiJ5/S1CHqOEHtYw5NJwAkD2WhZlyn49KiGTAM8qvddGlOMf3Ao48HePis+CESFZcrm7F3r5Zx3PANmIJ7w3Xt9z5ukJHmCT2Le7Z+9s8OmJ1R8/bIVZNfwG8qirrHAF9FXS+L38tQ/GZtu5xBSSWy8ZeCYq+s/CMmQt0MImtlGNkWgVBWzm4FTXwdgLkoXlPbiWX5cDm/lmO1aA9dBYWldFdjeTh01xcZimoeEOgnGyPhNTgXjIPZ22MlZWrqhnBCe+lmpL2fAkA== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=xoipPBXDq+B7miEFXvUPZZmuAsKdEaZrS2Jlf/Hy52c=; b=RAL7YsDHfUzzLBXyRW6Bpmq5zxZTHr9BFQ2v0YsOvX45WKTWPJFUSTU8L9RxwQDx9azx9fvNblU+jntu/smlJ/nlQSwgPKAciXo1CmOyRfVVdg/rFUoOGb0z5Tqj2jSSd64RUNxgAzjr9qaqRBPptexUhK2DZBMTnRpxcjuL1brN15X3Tu83Ym8zZTUm1lw3yrnVQcvYpDM3UI1HnnE+8zHTS6or6A2iW7D4iMhOCBi/XU7x4gA49oI9ECrpDYJoNfq2CVxjJUwktFs+RYktEPL1eB+smx4gjWX9AxoN+2gXmsS/bX2z7TXwDc4z19FOakba/RQo9tF+L/Mj6VtwNg== 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=xoipPBXDq+B7miEFXvUPZZmuAsKdEaZrS2Jlf/Hy52c=; b=Zs2lfeqIlCBdJzcDmAJttMgzliIDAGNkqRaADkDuAEn+qsBsX6oa9mR0UZgCtUBNfRNE3VlBiOXT5ASK7Kgulfr4rP3r/Fit7P8+Vqw5ZwSgWEt7xdYOx1DYaurpZbGbz7EbFazLLTk7EA+ptKasWdE7kZwo9OtBhd+d+MVAdkpW4hgQ67pTYRg2qsbqoKpPVaxD8gaXAa+fuw3bh1Ti8ItNfFSir/FB25zqa6c0CV9c3aPaFuHy4XrccLeJb7hyb4SD4IovWYDMicXaLujnh4MjsQYdLE99VwI7Czan0NVwu5kt89ghcT0JBxktjFSjlwBr8tcpUdRjc0jyTzlMEQ== Received: from DM8P223MB0365.NAMP223.PROD.OUTLOOK.COM (2603:10b6:8:b::20) by DM8P223MB0398.NAMP223.PROD.OUTLOOK.COM (2603:10b6:8:5::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4734.23; Sat, 27 Nov 2021 19:37:43 +0000 Received: from DM8P223MB0365.NAMP223.PROD.OUTLOOK.COM ([fe80::9c8d:fc63:9488:9775]) by DM8P223MB0365.NAMP223.PROD.OUTLOOK.COM ([fe80::9c8d:fc63:9488:9775%5]) with mapi id 15.20.4734.022; Sat, 27 Nov 2021 19:37:43 +0000 From: Soft Works To: "ffmpeg-devel@ffmpeg.org" Thread-Topic: [PATCH v17 03/19] avcodec/subtitles: Introduce new frame-based subtitle decoding API Thread-Index: AQHX48Y+uQz+9IB5MkiGJ3qPhrxB1A== Date: Sat, 27 Nov 2021 19:37:43 +0000 Message-ID: References: <7463f4e16ad074b56143d86846344e23a3594ef3.1638041335.git.softworkz@hotmail.com> In-Reply-To: <7463f4e16ad074b56143d86846344e23a3594ef3.1638041335.git.softworkz@hotmail.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-messagesentrepresentingtype: 1 x-tmn: [SRzS8NrritfQISR4HbzaAdW4cv4J8L1P] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 6ca6bf42-478c-4e96-cd11-08d9b1dd617e x-ms-traffictypediagnostic: DM8P223MB0398: x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: 6IBoNt2BdIto8VwaisBX8LbTcMCoCiaq1j49f9ja+OcIoTaT9088sg710e5vDhojGXOgxEv2MhD3xdkbFp882wQ1ryZ6vOP363K04ha5rXjtu7Il7Go5ikuMfLDsKVj13/0dkWOzTIGuf4sbuhaZBmneCzIO8FmQHGNuPPRa0I1UPV5m9FV9AzL4KRtRf3XvJ3TZEz9ebtJQ7sOby54uA53GSXltSTsJwrvsWOd67AnRlrQAmCkTykqX+a4ZBXgb46X3V5I1CoAE47cPE9EbHbC8aWqmKrlsvOjrbXQdW8BDwAcIQBno2PaHTFQl80XvZYyOSsGDvPtRgx8zXaubNS9L3FrE65k2scCNMshEkjCu+yT2qHXLHU8DKu0iAYpiK26mr3gZxr8zzlH5xIlfKduIqY1N63nDClI5/VSdMt0Tm9RD3f1nNiq+ycQXaz7Uf+Q6wVcSyTD2MKCbsjSSVxivBB7Lg6QoupFSIsKpVknIHGYDVhujkNTYKdtkpA2k9EdTYofjh/B+Tzce4c5LBSVlcS5iBNCk1VFguMIJ9PLsMu5tRmE2LxkGbsl0UWME+unyw4aV5oLQDZt5Otod3A== x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: YVaatxSPrbknKVz9QT3sR8cieI0m24ekUBo8dEPZEytYKyU/uOIIvBhu2AWwNGtzFK8HzWUh2yt9rhdF4yySAFZK7ZJUc8DV+S/rPwPGSdtI9acWElonrLwKaVoqQFpLn3cJXgBTZPZPqWWgFN3OIjeHBzHmn1VxjvlHpDNhDVjOouUCyyKDOMnl8loSGdXaIzf21WDZbYVRh5DMKpzl+h5ZXr7Myb3ao26D4KazE5Zf7GLiiiTSrXsZ5VNl032Kiba6ViXINnJtwgkzRyNEMoBSFxpefBtT0Bx7NCcEmfKKcKTSAFwdIFRF5fhBORSxDjI6RNl+ngKmc5yY6NOJO59LHKMlWHOu+edPmAWdHz5hvgULlEdRvzNf8CA3VQnSeM5spPohONsrR0LhVGYknSLgB0PZxFLu7/X1JnjwwChN33+zfM41SY6EhVjytNF4bfIV+jJPbQIYqQksSlEZU7U7ctyn82m8WIm+ci5l9pDsIxt4UtInC8+1lEM26INkt/DYQDdFkkqlyKqkRaDe6tS0+wKwVtZ0JvgPdpX680ohUJa7/IsZa7ZFwIPcNzybb/H0sNXFHpp+qFqNtH8Dcl2RRdYts0x8NvTRlNT8WQGJresMzSjzG7TQa5MTiJR5UNHK/AH+E0gL9cFdFDxazBLQTmiaH3XtYRhjgBeCiTQpuMXPqPs7gL3pvF+8vf02gTUy2Pn+DFHS2ElnZN4PHA== MIME-Version: 1.0 X-OriginatorOrg: sct-15-20-3912-16-msonline-outlook-1fc6d.templateTenant X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: DM8P223MB0365.NAMP223.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: 6ca6bf42-478c-4e96-cd11-08d9b1dd617e X-MS-Exchange-CrossTenant-originalarrivaltime: 27 Nov 2021 19:37:43.0627 (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: DM8P223MB0398 Subject: [FFmpeg-devel] [PATCH v17 03/19] avcodec/subtitles: Introduce new frame-based subtitle decoding API 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: K1b3iVKk5hVl - Add avcodec_decode_subtitle3 which takes subtitle frames, serving as compatibility shim to legacy subtitle decoding - Add additional methods for conversion between old and new API Signed-off-by: softworkz --- libavcodec/avcodec.h | 8 +- libavcodec/codec_desc.c | 11 +++ libavcodec/codec_desc.h | 8 ++ libavcodec/decode.c | 54 ++++++++++-- libavcodec/internal.h | 16 ++++ libavcodec/utils.c | 182 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 270 insertions(+), 9 deletions(-) diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index b05c12e47e..be571841bd 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -1675,7 +1675,7 @@ typedef struct AVCodecContext { /** * Header containing style information for text subtitles. - * For SUBTITLE_ASS subtitle type, it should contain the whole ASS + * For AV_SUBTITLE_FMT_ASS subtitle type, it should contain the whole ASS * [Script Info] and [V4+ Styles] section, plus the [Events] line and * the Format line following. It shouldn't include any Dialogue line. * - encoding: Set/allocated/freed by user (before avcodec_open2()) @@ -2417,7 +2417,10 @@ int avcodec_close(AVCodecContext *avctx); * Free all allocated data in the given subtitle struct. * * @param sub AVSubtitle to free. + * + * @deprecated Use the regular frame based encode and decode APIs instead. */ +attribute_deprecated void avsubtitle_free(AVSubtitle *sub); /** @@ -2510,7 +2513,10 @@ enum AVChromaLocation avcodec_chroma_pos_to_enum(int xpos, int ypos); * must be freed with avsubtitle_free if *got_sub_ptr is set. * @param[in,out] got_sub_ptr Zero if no subtitle could be decompressed, otherwise, it is nonzero. * @param[in] avpkt The input AVPacket containing the input buffer. + * + * @deprecated Use the new decode API (avcodec_send_packet, avcodec_receive_frame) instead. */ +attribute_deprecated int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub, int *got_sub_ptr, AVPacket *avpkt); diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c index 0974ee03de..e48e4532ba 100644 --- a/libavcodec/codec_desc.c +++ b/libavcodec/codec_desc.c @@ -3548,3 +3548,14 @@ enum AVMediaType avcodec_get_type(enum AVCodecID codec_id) const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id); return desc ? desc->type : AVMEDIA_TYPE_UNKNOWN; } + +enum AVSubtitleType avcodec_descriptor_get_subtitle_format(const AVCodecDescriptor *codec_descriptor) +{ + if(codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) + return AV_SUBTITLE_FMT_BITMAP; + + if(codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB) + return AV_SUBTITLE_FMT_ASS; + + return AV_SUBTITLE_FMT_UNKNOWN; +} diff --git a/libavcodec/codec_desc.h b/libavcodec/codec_desc.h index 126b52df47..ba68d24e0e 100644 --- a/libavcodec/codec_desc.h +++ b/libavcodec/codec_desc.h @@ -121,6 +121,14 @@ const AVCodecDescriptor *avcodec_descriptor_next(const AVCodecDescriptor *prev); */ const AVCodecDescriptor *avcodec_descriptor_get_by_name(const char *name); +/** + * Return subtitle format from a codec descriptor + * + * @param codec_descriptor codec descriptor + * @return the subtitle type (e.g. bitmap, text) + */ +enum AVSubtitleType avcodec_descriptor_get_subtitle_format(const AVCodecDescriptor *codec_descriptor); + /** * @} */ diff --git a/libavcodec/decode.c b/libavcodec/decode.c index c44724d150..0a81a5e35d 100644 --- a/libavcodec/decode.c +++ b/libavcodec/decode.c @@ -576,6 +576,37 @@ static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame) return ret; } +static int decode_subtitle2_priv(AVCodecContext *avctx, AVSubtitle *sub, + int *got_sub_ptr, AVPacket *avpkt); + +static int decode_subtitle_shim(AVCodecContext *avctx, AVFrame *frame, AVPacket *avpkt) +{ + int ret, got_sub_ptr = 0; + AVSubtitle subtitle = { 0 }; + + if (frame->buf[0]) + return AVERROR(EAGAIN); + + av_frame_unref(frame); + + ret = decode_subtitle2_priv(avctx, &subtitle, &got_sub_ptr, avpkt); + + if (ret >= 0 && got_sub_ptr) { + frame->type = AVMEDIA_TYPE_SUBTITLE; + frame->format = subtitle.format; + ret = av_frame_get_buffer2(frame, 0); + + if (ret >= 0) + ret = ff_frame_put_subtitle(frame, &subtitle); + + frame->pkt_dts = avpkt->dts; + } + + avsubtitle_free(&subtitle); + + return ret; +} + int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt) { AVCodecInternal *avci = avctx->internal; @@ -590,6 +621,9 @@ int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacke if (avpkt && !avpkt->size && avpkt->data) return AVERROR(EINVAL); + if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) + return decode_subtitle_shim(avctx, avci->buffer_frame, avpkt); + av_packet_unref(avci->buffer_pkt); if (avpkt && (avpkt->data || avpkt->side_data_elems)) { ret = av_packet_ref(avci->buffer_pkt, avpkt); @@ -651,7 +685,9 @@ int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *fr if (avci->buffer_frame->buf[0]) { av_frame_move_ref(frame, avci->buffer_frame); - } else { + } else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) + return AVERROR(EAGAIN); + else { ret = decode_receive_frame_internal(avctx, frame); if (ret < 0) return ret; @@ -802,9 +838,8 @@ static int utf8_check(const uint8_t *str) return 1; } -int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub, - int *got_sub_ptr, - AVPacket *avpkt) +static int decode_subtitle2_priv(AVCodecContext *avctx, AVSubtitle *sub, + int *got_sub_ptr, AVPacket *avpkt) { int ret = 0; @@ -844,10 +879,7 @@ int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub, avctx->pkt_timebase, ms); } - if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) - sub->format = 0; - else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB) - sub->format = 1; + sub->format = avcodec_descriptor_get_subtitle_format(avctx->codec_descriptor); for (unsigned i = 0; i < sub->num_rects; i++) { if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_IGNORE && @@ -871,6 +903,12 @@ int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub, return ret; } +int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub, + int *got_sub_ptr, AVPacket *avpkt) +{ + return decode_subtitle2_priv(avctx, sub, got_sub_ptr, avpkt); +} + enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *avctx, const enum AVPixelFormat *fmt) { diff --git a/libavcodec/internal.h b/libavcodec/internal.h index a62f8dbd4e..10443ed2d1 100644 --- a/libavcodec/internal.h +++ b/libavcodec/internal.h @@ -363,6 +363,22 @@ int ff_int_from_list_or_default(void *ctx, const char * val_name, int val, void ff_dvdsub_parse_palette(uint32_t *palette, const char *p); +/** + * Copies subtitle data from AVSubtitle to AVFrame. + * + * @deprecated This is a compatibility method for interoperability with + * the legacy subtitle API. + */ +int ff_frame_put_subtitle(AVFrame* frame, const AVSubtitle* sub); + +/** + * Copies subtitle data from AVFrame to AVSubtitle. + * + * @deprecated This is a compatibility method for interoperability with + * the legacy subtitle API. + */ +int ff_frame_get_subtitle(AVSubtitle* sub, AVFrame* frame); + #if defined(_WIN32) && CONFIG_SHARED && !defined(BUILDING_avcodec) # define av_export_avcodec __declspec(dllimport) #else diff --git a/libavcodec/utils.c b/libavcodec/utils.c index a91a54b0dc..613c580c19 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -824,6 +824,188 @@ int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes) return FFMAX(0, duration); } +static int subtitle_area2rect(AVSubtitleRect *dst, const AVSubtitleArea *src) +{ + dst->x = src->x; + dst->y = src->y; + dst->w = src->w; + dst->h = src->h; + dst->nb_colors = src->nb_colors; + dst->type = src->type; + dst->flags = src->flags; + + switch (dst->type) { + case AV_SUBTITLE_FMT_BITMAP: + + if (src->h > 0 && src->w > 0 && src->buf[0]) { + uint32_t *pal; + AVBufferRef *buf = src->buf[0]; + dst->data[0] = av_mallocz(buf->size); + memcpy(dst->data[0], buf->data, buf->size); + dst->linesize[0] = src->linesize[0]; + + dst->data[1] = av_mallocz(256 * 4); + pal = (uint32_t *)dst->data[1]; + + for (unsigned i = 0; i < 256; i++) { + pal[i] = src->pal[i]; + } + } + + break; + case AV_SUBTITLE_FMT_TEXT: + + if (src->text) + dst->text = av_strdup(src->text); + else + dst->text = av_strdup(""); + + if (!dst->text) + return AVERROR(ENOMEM); + + break; + case AV_SUBTITLE_FMT_ASS: + + if (src->ass) + dst->ass = av_strdup(src->ass); + else + dst->ass = av_strdup(""); + + if (!dst->ass) + return AVERROR(ENOMEM); + + break; + default: + + av_log(NULL, AV_LOG_ERROR, "Subtitle rect has invalid format: %d", dst->type); + return AVERROR(EINVAL); + } + + return 0; +} + +static int subtitle_rect2area(AVSubtitleArea *dst, const AVSubtitleRect *src) +{ + dst->x = src->x; + dst->y = src->y; + dst->w = src->w; + dst->h = src->h; + dst->nb_colors = src->nb_colors; + dst->type = src->type; + dst->flags = src->flags; + + switch (dst->type) { + case AV_SUBTITLE_FMT_BITMAP: + + if (src->h > 0 && src->w > 0 && src->data[0]) { + AVBufferRef *buf = av_buffer_allocz(src->h * src->linesize[0]); + memcpy(buf->data, src->data[0], buf->size); + + dst->buf[0] = buf; + dst->linesize[0] = src->linesize[0]; + } + + if (src->data[1]) { + uint32_t *pal = (uint32_t *)src->data[1]; + + for (unsigned i = 0; i < 256; i++) { + dst->pal[i] = pal[i]; + } + } + + break; + case AV_SUBTITLE_FMT_TEXT: + + if (src->text) { + dst->text = av_strdup(src->text); + if (!dst->text) + return AVERROR(ENOMEM); + } + + break; + case AV_SUBTITLE_FMT_ASS: + + if (src->ass) { + dst->ass = av_strdup(src->ass); + if (!dst->ass) + return AVERROR(ENOMEM); + } + + break; + default: + + av_log(NULL, AV_LOG_ERROR, "Subtitle area has invalid format: %d", dst->type); + return AVERROR(EINVAL); + } + + return 0; +} + +/** + * Copies subtitle data from AVSubtitle (deprecated) to AVFrame + * + * @note This is a compatibility method for conversion to the legacy API + */ +int ff_frame_put_subtitle(AVFrame *frame, const AVSubtitle *sub) +{ + frame->format = sub->format; + frame->subtitle_start_time = sub->start_display_time; + frame->subtitle_end_time = sub->end_display_time; + frame->subtitle_pts = sub->pts; + + if (sub->num_rects) { + frame->subtitle_areas = av_malloc_array(sub->num_rects, sizeof(AVSubtitleArea*)); + if (!frame->subtitle_areas) + return AVERROR(ENOMEM); + + for (unsigned i = 0; i < sub->num_rects; i++) { + int ret; + frame->subtitle_areas[i] = av_mallocz(sizeof(AVSubtitleArea)); + if (!frame->subtitle_areas[i]) + return AVERROR(ENOMEM); + ret = subtitle_rect2area(frame->subtitle_areas[i], sub->rects[i]); + if (ret < 0) { + frame->num_subtitle_areas = i; + return ret; + } + } + } + + frame->num_subtitle_areas = sub->num_rects; + return 0; +} + +/** + * Copies subtitle data from AVFrame to AVSubtitle (deprecated) + * + * @note This is a compatibility method for conversion to the legacy API + */ +int ff_frame_get_subtitle(AVSubtitle *sub, AVFrame *frame) +{ + sub->start_display_time = frame->subtitle_start_time; + sub->end_display_time = frame->subtitle_end_time; + sub->pts = frame->subtitle_pts; + + if (frame->num_subtitle_areas) { + sub->rects = av_malloc_array(frame->num_subtitle_areas, sizeof(AVSubtitleRect*)); + if (!sub->rects) + return AVERROR(ENOMEM); + + for (unsigned i = 0; i < frame->num_subtitle_areas; i++) { + int ret; + sub->rects[i] = av_mallocz(sizeof(AVSubtitleRect)); + ret = subtitle_area2rect(sub->rects[i], frame->subtitle_areas[i]); + if (ret < 0) { + sub->num_rects = i; + return ret; + } + } + } + + sub->num_rects = frame->num_subtitle_areas; + return 0; +} + int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes) { int duration = get_audio_frame_duration(par->codec_id, par->sample_rate,