From patchwork Sun Dec 5 16:23:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Soft Works X-Patchwork-Id: 31989 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp3439748iog; Sun, 5 Dec 2021 08:24:07 -0800 (PST) X-Google-Smtp-Source: ABdhPJxnfjYuiq+BAeZ5AVqiKrLjOln8rlr8gEiBb1p/HqjimnkRRRvkCNi+sM/iS9vZvjMtsWR1 X-Received: by 2002:a17:906:4789:: with SMTP id cw9mr38663001ejc.518.1638721446875; Sun, 05 Dec 2021 08:24:06 -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 cm20si13790736edb.527.2021.12.05.08.24.06; Sun, 05 Dec 2021 08:24:06 -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=irIZ3MHK; 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 9CB5468AE9B; Sun, 5 Dec 2021 18:23:49 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from NAM12-MW2-obe.outbound.protection.outlook.com (unknown [40.92.23.47]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id B5D1668A255 for ; Sun, 5 Dec 2021 18:23:39 +0200 (EET) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ZAlHVkX5bf62tPKut5uEoIHpf2yyH9Y0Uqk7oj6bPhVDWnIjdSmx0xLb8WwWBkd/VEbmd2XfEW2RBNEZW2gY71T1GbFVUDFEMgiO8ISia34/cWHrWLOQB5dlR765/FLPNGt2fuiq4Bn8BTMkoBW+G90WiKanIWZ1QviKEr6T6xGr2bHXiJS8J5Ng51tp/iJ24fdiIEyf9Dfkoj/+JshLHoatTlxnxkYGcJyfVWVSrS6GwFO3N9ISBUuQhpQeb1mnPlYEvhJcHkhEIbBJPn/OmtJtepc7brZeJvLUCtxSQQbYNZNmOukXWw0LLz7NIVAYt2Us7zd1pWTapLAllBsAGg== 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=mPmutJfhcmf6SFLm/CvIM3h6N2/yej5FEkav/KOwn0Y=; b=MnNd8/0euNUQNnIVcOJUIkgpGUh9wzbZFmK4l0KnM/Tt0IjO+M4IfWqc40VqRdHrqz1t2qTgjzH9VpErIpPUHGe4iLTko+oCFmNBbWwiLl124QVmTGp9Xakdc0cch9HlO6s5cmpinEtAAH5oApOH356HHRzdXTjwuQYB2DpfEF0ui6KlipUcEP7buP9EK7OosG/z1LjpKVNnlF2n5OHfozDXbrYbxBKz9xqPqbdEv26SnW5Cwu+Q9F7CKozY1nAzF5PVeEee5fu9IYBbGH6/Ua5ja3IgyIxaIxa0tpqp2cpwVG1XlpmI/C6TZEhX3beUQepqpr+fsd1cSvghSNgEig== 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=mPmutJfhcmf6SFLm/CvIM3h6N2/yej5FEkav/KOwn0Y=; b=irIZ3MHKUnF1OAG8CbMXfH3V5s6ew2j9ushuxf/+pWWWd1r/Z2b7XCPwutcCbVywRWqMR6eYu9zHkx7AgoGrVY1LelrlaWMvqPDMefC6d6ON1w2i/al1gYWXe47Ahu+8pX2cxnmU/nwg5cCOJTEjETgCU+9czG8gGoqFvp/DCcDTDCYaI13TKD951eIG3WursODfpV9gOIgnCxY8230Wdclv5ymUtUzEZJBvvGv6JvE9hq3PfKLg+uSg0J+EC+JrGlXfdRw0bQF+9KFoeHJVRsyiy6/nCy/UxU7uqDpF4D8ffUDTMTZgxqfC/+FPuboOnu17OQ5V1+m0h3jRx8o8Iw== Received: from DM8P223MB0365.NAMP223.PROD.OUTLOOK.COM (2603:10b6:8:b::20) by DM8P223MB0333.NAMP223.PROD.OUTLOOK.COM (2603:10b6:8:8::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4755.21; Sun, 5 Dec 2021 16:23:36 +0000 Received: from DM8P223MB0365.NAMP223.PROD.OUTLOOK.COM ([fe80::9c8d:fc63:9488:9775]) by DM8P223MB0365.NAMP223.PROD.OUTLOOK.COM ([fe80::9c8d:fc63:9488:9775%8]) with mapi id 15.20.4755.021; Sun, 5 Dec 2021 16:23:36 +0000 From: Soft Works To: "ffmpeg-devel@ffmpeg.org" Thread-Topic: [PATCH v20 03/20] avcodec/subtitles: Introduce new frame-based subtitle decoding API Thread-Index: AQHX6fR0uklkd5onkEWLPW6fY+BsxQ== Date: Sun, 5 Dec 2021 16:23:36 +0000 Message-ID: References: <7795dfe8ba792c0f4f7f8a543d4df8adf8d8189e.1638721292.git.softworkz@hotmail.com> In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-messagesentrepresentingtype: 1 x-tmn: [7dRVZEDAKMXmFq3inhv6MvcmtRjA1hV9ghu53d3PAeVOve6rwlJGD5Rp2JswS+ZN] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: a1fde3ca-0952-4a09-0f32-08d9b80b96f4 x-ms-traffictypediagnostic: DM8P223MB0333: x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: ZSL51bcckQ2l57wk81WWlRdmX4oBK7I5zgs2G3Lx9ea6envFMYiONCF1CPp4w58C/w7JZURyeJdJD3jO/rPQrIg8wyFMvaC9+FiCOGL+KNKoSZCGF0ftoI/aYEloAzaEl45SU5cknaITpkOIT+Fi3Lww5LlzDj0ct1UXthzJF9YaAyfeNZsNKwH+T7gbuidMYRVYvi8cyVVxh13GonPmmFHHwfdEuXiXSWTH8UKG8thjK5X48HU115zJcDI+wtqLRQFN3rO/IMv8g8SeekrQWmhjoUv/otJtUlRIR4eIzVDupZA5t0BSdJlkzXYihGm7K8hXuAK9FmlIo6jUhlQ1n9o8Z6tnT2KlXMleE5OyM4ncwJgSskijeJUF4XICVXhQQqZHA7rjEzgpJsIFOifVbbJgVrh7DkGNGN6Vvtcn5ltHxoMCc/nSMxMkzAaOTkxZ5AgWAL/DKsfGEuUBFqs4gZfs786Wfkyk6ahQsJuL5L9kgmlE/sKtZzI9raApIHc8kYSvu+zGUY9Qjy+fGpV3AgzT+FyaWlmmseLYUnymg80zKcd7165WVFV6OMLC6eLedrg/A48WT6AK4bZNI7wzaA== x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?utf-8?q?c1gHUKZQCJl2N2KDXTUGTyIlvZTe?= =?utf-8?q?3A6MqNucRUund8LxqogajrPNZZKUB+VesFbdAlU/ZAwfxEHP1Ed8VCi62r2B4RNN2?= =?utf-8?q?tyDlcPpNy8gtdTOpcM3YIkprbn625Dc0QF96DB/ic+OAp0VqZd5yhgWaL22iWemW/?= =?utf-8?q?xSMfp8xLi6gIRUOAaxIKSoG49roI8zU98GK85eEvTtmyFbK4Jf838XuXOowc0WCtI?= =?utf-8?q?/x3jECuB8w46E3tzNIowHqS+0THsQEaVgvYD1Ps3GsA8h4ShRIvpaKLTGd2hfJREC?= =?utf-8?q?39Hsk5xr0A3dfjzSVKcRbhU8Gi65bQWXXK00LqTBsuMq8A2S7iwTGLkfXW1Y6xNVc?= =?utf-8?q?ctwXs0JF8z251+qUUI1kD/SHbKGic+B/Z0GSf49frlpPF1v3Ujhwf0Ajh78FKrjG7?= =?utf-8?q?e2WEwpdNeGl4ftOM86xNRo/lNCtM81SIgBp43W7kzrPbYiKZgEM1/eHM/BS9JP2CE?= =?utf-8?q?alGrVsMswIgBaOU+IXCU2kUZhTamkxFtR6EXSCJyFJOv2+mMo/NvGwUUfgMfcedR+?= =?utf-8?q?Dt9+6IS7o3oNFpNY5xDm34S9a+SRKMsYnHaU8JsyzkdqEUMxWyrMVSgTdo0AdFgEY?= =?utf-8?q?bOQaraSRt/8PsTcqCA//t3xfZVVL+bGZ7N/0wHFUMh6f3nSVPdeMOWSX1gbiq0haU?= =?utf-8?q?NTajP/sYcL2B08219obujhDBx3SVjvkZQD2/jgywrD0nPqoNwk/02bPMibhV4kGU/?= =?utf-8?q?O6W9vtc89OiGZ5vTPy/93R1Q4Eed+DAvgd5UI3Jp1OOxog/F+hqUXy6uiXbln1Lwe?= =?utf-8?q?Ro2pFOQjVimNKgFCFjoD4Yo/smt3bBGdMUDLxpGr1gRGWu+P1h46PVSKLbnbvhzyx?= =?utf-8?q?V0Pbj9UVN0CdstJbRo4ZvkNr9vG31Pa9WQv57to1pVfKoOud32B643muOnYOziiSX?= =?utf-8?q?5ijQhEBMF8lRe6zKrTL7KAZQF7eSOHchpsBBYEsQNCe8cmyM7UxxWLB5THZUXLqR9?= =?utf-8?q?9njOiXsKL5z3y9X9QTa4Cg+ZVSCDAhVLtarF95whkNQ=3D=3D?= MIME-Version: 1.0 X-OriginatorOrg: sct-15-20-4755-11-msonline-outlook-1ff67.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: a1fde3ca-0952-4a09-0f32-08d9b80b96f4 X-MS-Exchange-CrossTenant-originalarrivaltime: 05 Dec 2021 16:23:36.5447 (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: DM8P223MB0333 Subject: [FFmpeg-devel] [PATCH v20 03/20] 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: dMbNdXvPn/+M - 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 --- doc/APIchanges | 7 ++ 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 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 277 insertions(+), 9 deletions(-) diff --git a/doc/APIchanges b/doc/APIchanges index 8c63ea0311..49f1a28f71 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -14,6 +14,13 @@ libavutil: 2021-04-27 API changes, most recent first: +2021-12-05 - xxxxxxxxxx - lavc 60.1.100 - codec_desc.h + Add avcodec_descriptor_get_subtitle_format() + +2021-12-05 - xxxxxxxxxx - lavc 60.1.100 - avcodec.h + Deprecate avsubtitle_free() + Deprecate avcodec_decode_subtitle2(), use regular decode api now + 2021-12-05 - xxxxxxxxxx - lavu 58.1.100 - frame.h Add AVMediaType field to AVFrame Add Fields for carrying subtitle data to AVFrame diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index b05c12e47e..3e734d3003 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 52bf5dcd33..ac267f0df6 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,