From patchwork Sun Nov 19 19:08:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Zsolt_Vad=C3=A1sz?= X-Patchwork-Id: 44724 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:6a89:b0:181:818d:5e7f with SMTP id bi9csp1184113pzb; Sun, 19 Nov 2023 11:09:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IHQnHnClIMPFUkTdRr89iCvqr/WdGGIwtx129zWkIGGySOuKFsoZaH2+S3iPJcqJzpsIGjz X-Received: by 2002:a17:906:b2ca:b0:9fd:8cd9:7842 with SMTP id cf10-20020a170906b2ca00b009fd8cd97842mr1299704ejb.44.1700420965261; Sun, 19 Nov 2023 11:09:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700420965; cv=none; d=google.com; s=arc-20160816; b=0UGI8XvLZTeZvBB8s50JsetSD8XAwXlUiDZEm4uD6c9qXLJOdExWWj9MBpxSrhDIo3 zEiWXabdP30IVR7fdOqJEsi32GmIXSwYqHR6O2HFAZvNcikl6yUEvty1aB7C5amm3Olk 0TFaK9Zgw9xy5Ch17Qp9iyZJF1q6AbFPCH8FCvu0Tdm4m+JLcq8e8ztTMVz3NVC9hLjx eqpoFCemCej3ZRC16b1xbl/WO3YVGHiP0OTVzRQ8qSE2wt2aWRppisRiddkBq1dD8xtj e+yc1PmvIWaQGduIbycvmHLLRgb95ykG7JAW1UU/bsfVSCyNUHiwp4+Osce9mB2ZFGk1 t8bQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence:subject :mime-version:feedback-id:message-id:to:date:delivered-to; bh=f5yMgEECVj7vLr8/rwO0Qr8IFVyS072vYf+J/F9Uy9g=; fh=lvOXre93Kc+LM49rLl0yJcoSym9484cIB4O1jbJgoXk=; b=SVy0ZKp7nqzjiEQcO2x9hjB2MgG7BDfIO17gcMSWIoRq+Eh2ORXOq70bg/EF5OTiqt 2egrO/fZohx2leDQrFsi1pGkqQpa5+S0jN31XN5GUcqq4yjdxsKKv3JZWJMEOYayeGR7 Q6rSVQ6h7xSnenbkXYggu8FirdP0p1ZnIvfuyngThjFXKM8pYfO47lQL11Hy/cxvCVth bJ1UkKR4G9fu9yYpIiek9U1DFi9klIALvUcXE6zKRVWNAzoycO31EAEAHl+imYR3J8l+ 7RIuK4o16nCwONhuHUJ6Kv+ILBalStguChTBYNSCMXBM5qJoT23OxYFJiTtglS3OTM22 CK2w== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id p27-20020a17090635db00b009fbc617a7d3si1513081ejb.292.2023.11.19.11.09.23; Sun, 19 Nov 2023 11:09:25 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 67C0668BE8F; Sun, 19 Nov 2023 21:09:21 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-4324.protonmail.ch (mail-4324.protonmail.ch [185.70.43.24]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 1BE4068BE8F for ; Sun, 19 Nov 2023 21:09:14 +0200 (EET) Date: Sun, 19 Nov 2023 19:08:59 +0000 To: FFmpeg development discussions and patches Message-ID: Feedback-ID: 28710920:user:proton MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v6 2/2] avformat/oggenc: Add support for embedding cover art 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: , X-Patchwork-Original-From: =?utf-8?q?Zsolt_Vad=C3=A1sz_via_ffmpeg-devel?= From: =?utf-8?q?Zsolt_Vad=C3=A1sz?= Reply-To: FFmpeg development discussions and patches Cc: =?utf-8?q?Zsolt_Vad=C3=A1sz?= Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 95xID1ja7vI1 Attached. From 38716e20b2e8b3a711cc22f7e7abc45731aa79b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zsolt=20Vad=C3=A1sz?= Date: Fri, 10 Mar 2023 11:26:27 +0000 Subject: [PATCH v6 2/2] avformat/oggenc: Add support for embedding cover art Fixes #4448. Signed-off-by: Zsolt Vadasz --- libavformat/oggenc.c | 214 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 176 insertions(+), 38 deletions(-) diff --git a/libavformat/oggenc.c b/libavformat/oggenc.c index 69a66f586d..893f4cac14 100644 --- a/libavformat/oggenc.c +++ b/libavformat/oggenc.c @@ -23,19 +23,28 @@ #include +#include "libavcodec/codec_id.h" +#include "libavutil/avutil.h" #include "libavutil/crc.h" +#include "libavutil/log.h" #include "libavutil/mathematics.h" #include "libavutil/opt.h" #include "libavutil/random_seed.h" +#include "libavutil/pixdesc.h" +#include "libavutil/avstring.h" +#include "libavutil/base64.h" +#include "libavutil/bswap.h" #include "libavcodec/xiph.h" #include "libavcodec/bytestream.h" #include "libavcodec/flac.h" #include "avformat.h" +#include "id3v2.h" #include "avio_internal.h" #include "internal.h" #include "mux.h" #include "version.h" #include "vorbiscomment.h" +#include "flac_picture.h" #define MAX_PAGE_SIZE 65025 @@ -78,6 +87,11 @@ typedef struct OGGContext { int pref_size; ///< preferred page size (0 => fill all segments) int64_t pref_duration; ///< preferred page duration (0 => fill all segments) int serial_offset; + + PacketList queue; + int audio_stream_idx; + int waiting_pics; + unsigned attached_types; } OGGContext; #define OFFSET(x) offsetof(OGGContext, x) @@ -469,12 +483,14 @@ static void ogg_write_pages(AVFormatContext *s, int flush) ogg->page_list = p; } -static int ogg_init(AVFormatContext *s) +static int ogg_finish_init(AVFormatContext *s) { OGGContext *ogg = s->priv_data; OGGStreamContext *oggstream = NULL; int i, j; + ogg->waiting_pics = 0; + if (ogg->pref_size) av_log(s, AV_LOG_WARNING, "The pagesize option is deprecated\n"); @@ -482,29 +498,10 @@ static int ogg_init(AVFormatContext *s) AVStream *st = s->streams[i]; unsigned serial_num = i + ogg->serial_offset; - if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { - if (st->codecpar->codec_id == AV_CODEC_ID_OPUS) - /* Opus requires a fixed 48kHz clock */ - avpriv_set_pts_info(st, 64, 1, 48000); - else - avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate); - } - - if (st->codecpar->codec_id != AV_CODEC_ID_VORBIS && - st->codecpar->codec_id != AV_CODEC_ID_THEORA && - st->codecpar->codec_id != AV_CODEC_ID_SPEEX && - st->codecpar->codec_id != AV_CODEC_ID_FLAC && - st->codecpar->codec_id != AV_CODEC_ID_OPUS && - st->codecpar->codec_id != AV_CODEC_ID_VP8) { - av_log(s, AV_LOG_ERROR, "Unsupported codec id in stream %d\n", i); - return AVERROR(EINVAL); - } + if(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && + (st->disposition & AV_DISPOSITION_ATTACHED_PIC)) + continue; - if ((!st->codecpar->extradata || !st->codecpar->extradata_size) && - st->codecpar->codec_id != AV_CODEC_ID_VP8) { - av_log(s, AV_LOG_ERROR, "No extradata present\n"); - return AVERROR_INVALIDDATA; - } oggstream = av_mallocz(sizeof(*oggstream)); if (!oggstream) return AVERROR(ENOMEM); @@ -515,8 +512,11 @@ static int ogg_init(AVFormatContext *s) do { serial_num = av_get_random_seed(); for (j = 0; j < i; j++) { + // NULL for attached_pic OGGStreamContext *sc = s->streams[j]->priv_data; - if (serial_num == sc->serial_num) + if(!sc) + continue; + else if (serial_num == sc->serial_num) break; } } while (j < i); @@ -563,9 +563,9 @@ static int ogg_init(AVFormatContext *s) int framing_bit = st->codecpar->codec_id == AV_CODEC_ID_VORBIS ? 1 : 0; if (avpriv_split_xiph_headers(st->codecpar->extradata, st->codecpar->extradata_size, - st->codecpar->codec_id == AV_CODEC_ID_VORBIS ? 30 : 42, - (const uint8_t**)oggstream->header, oggstream->header_len) < 0) { - av_log(s, AV_LOG_ERROR, "Extradata corrupted\n"); + st->codecpar->codec_id == AV_CODEC_ID_VORBIS ? 30 : 42, + (const uint8_t**)oggstream->header, oggstream->header_len) < 0) { + av_log(s, AV_LOG_ERROR, "Extradata corrupted for stream #%d\n", i); oggstream->header[1] = NULL; return AVERROR_INVALIDDATA; } @@ -602,13 +602,67 @@ static int ogg_init(AVFormatContext *s) return 0; } -static int ogg_write_header(AVFormatContext *s) +static int ogg_init(AVFormatContext *s) +{ + OGGContext *ogg = s->priv_data; + int i; + + ogg->waiting_pics = 0; + ogg->attached_types = 0; + + if (ogg->pref_size) + av_log(s, AV_LOG_WARNING, "The pagesize option is deprecated\n"); + + for (i = 0; i < s->nb_streams; i++) { + AVStream *st = s->streams[i]; + + if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { + ogg->audio_stream_idx = i; + if (st->codecpar->codec_id == AV_CODEC_ID_OPUS) + /* Opus requires a fixed 48kHz clock */ + avpriv_set_pts_info(st, 64, 1, 48000); + else + avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate); + } + + if (st->codecpar->codec_id != AV_CODEC_ID_VORBIS && + st->codecpar->codec_id != AV_CODEC_ID_THEORA && + st->codecpar->codec_id != AV_CODEC_ID_SPEEX && + st->codecpar->codec_id != AV_CODEC_ID_FLAC && + st->codecpar->codec_id != AV_CODEC_ID_OPUS && + st->codecpar->codec_id != AV_CODEC_ID_VP8 && + st->codecpar->codec_id != AV_CODEC_ID_PNG && + st->codecpar->codec_id != AV_CODEC_ID_MJPEG) { + av_log(s, AV_LOG_ERROR, "Unsupported codec id in stream %d\n", i); + return AVERROR(EINVAL); + } + + if ((!st->codecpar->extradata || !st->codecpar->extradata_size) && + st->codecpar->codec_id != AV_CODEC_ID_VP8 && + st->codecpar->codec_id != AV_CODEC_ID_PNG && + st->codecpar->codec_id != AV_CODEC_ID_MJPEG) { + av_log(s, AV_LOG_ERROR, "No extradata present\n"); + return AVERROR_INVALIDDATA; + } + if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && + (st->disposition & AV_DISPOSITION_ATTACHED_PIC)) + ogg->waiting_pics++; + } + + if (!ogg->waiting_pics) + return ogg_finish_init(s); + return 0; +} + +static int ogg_finish_header(AVFormatContext *s) { OGGStreamContext *oggstream = NULL; int i, j; for (j = 0; j < s->nb_streams; j++) { oggstream = s->streams[j]->priv_data; + if(!oggstream) + continue; ogg_buffer_data(s, s->streams[j], oggstream->header[0], oggstream->header_len[0], 0, 1); oggstream->page.flags |= 2; // bos @@ -617,6 +671,8 @@ static int ogg_write_header(AVFormatContext *s) for (j = 0; j < s->nb_streams; j++) { AVStream *st = s->streams[j]; oggstream = st->priv_data; + if(!oggstream) + continue; for (i = 1; i < 3; i++) { if (oggstream->header_len[i]) ogg_buffer_data(s, st, oggstream->header[i], @@ -632,6 +688,14 @@ static int ogg_write_header(AVFormatContext *s) return 0; } +static int ogg_write_header(AVFormatContext *s) +{ + OGGContext *ogg = s->priv_data; + if (!ogg->waiting_pics) + return ogg_finish_header(s); + return 0; +} + static int ogg_write_packet_internal(AVFormatContext *s, AVPacket *pkt) { AVStream *st = s->streams[pkt->stream_index]; @@ -684,20 +748,89 @@ static int ogg_write_packet_internal(AVFormatContext *s, AVPacket *pkt) return 0; } +static int ogg_queue_flush(AVFormatContext *s) +{ + OGGContext *c = s->priv_data; + AVPacket *const pkt = ffformatcontext(s)->pkt; + int ret, write = 1; + ret = ogg_finish_init(s); + if (ret < 0) + write = 0; + ret = ogg_finish_header(s); + if (ret < 0) + write = 0; + + while (c->queue.head) { + avpriv_packet_list_get(&c->queue, pkt); + if (write && (ret = ogg_write_packet_internal(s, pkt)) < 0) + write = 0; + av_packet_unref(pkt); + } + return ret; +} + static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt) { - int i; + OGGContext *c = s->priv_data; + int i, ret; + AVStream *st = s->streams[pkt->stream_index]; - if (pkt) - return pkt->size ? ogg_write_packet_internal(s, pkt) : 0; + if (pkt) { + if (pkt->stream_index == c->audio_stream_idx) { + if (c->waiting_pics) { + /* buffer audio packets until we get all the pictures */ + ret = avpriv_packet_list_put(&c->queue, pkt, NULL, 0); + if (ret < 0) { + av_log(s, AV_LOG_ERROR, "Out of memory in packet queue; skipping attached pictures\n"); + c->waiting_pics = 0; + ret = ogg_queue_flush(s); + if (ret < 0) + return ret; + return pkt->size ? ogg_write_packet_internal(s, pkt) : 0; + } + } else + return pkt->size ? ogg_write_packet_internal(s, pkt) : 0; + } else if(c->waiting_pics && + (st->disposition & AV_DISPOSITION_ATTACHED_PIC)) { + /* warn only once for each stream */ + if (st->nb_frames == 1) { + av_log(s, AV_LOG_WARNING, "Got more than one picture in stream %d," + " ignoring.\n", pkt->stream_index); + } + if (st->nb_frames >= 1) { + av_log(s, AV_LOG_WARNING, "Attached picture must not have more than one frame.\n"); + return 0; + } - for (i = 0; i < s->nb_streams; i++) { - OGGStreamContext *oggstream = s->streams[i]->priv_data; - if (oggstream->page.segments_count) - ogg_buffer_page(s, oggstream); - } + //st->priv_data = av_packet_clone(pkt); + //if (!st->priv_data) + // av_log(s, AV_LOG_ERROR, "Out of memory queueing an attached picture; skipping\n"); + ret = ff_flac_write_picture(s, + 1, + &c->attached_types, + c->audio_stream_idx, + pkt); + if (ret < 0) { + av_log(s, AV_LOG_ERROR, "Failed to process attached picture.\n"); + return ret; + } + c->waiting_pics--; + + /* flush the buffered audio packets */ + if (!c->waiting_pics && + (ret = ogg_queue_flush(s)) < 0) + return ret; + } else + return pkt->size ? ogg_write_packet_internal(s, pkt) : 0; + } else { + for (i = 0; i < s->nb_streams; i++) { + OGGStreamContext *oggstream = s->streams[i]->priv_data; + if (oggstream->page.segments_count) + ogg_buffer_page(s, oggstream); + } - ogg_write_pages(s, 2); + ogg_write_pages(s, 2); + } return 1; } @@ -708,6 +841,8 @@ static int ogg_write_trailer(AVFormatContext *s) /* flush current page if needed */ for (i = 0; i < s->nb_streams; i++) { OGGStreamContext *oggstream = s->streams[i]->priv_data; + if(!oggstream) + continue; if (oggstream->page.size > 0) ogg_buffer_page(s, oggstream); @@ -735,7 +870,9 @@ static void ogg_free(AVFormatContext *s) st->codecpar->codec_id == AV_CODEC_ID_VP8) { av_freep(&oggstream->header[0]); } - av_freep(&oggstream->header[1]); + if (st->codecpar->codec_id != AV_CODEC_ID_PNG && + st->codecpar->codec_id != AV_CODEC_ID_MJPEG) + av_freep(&oggstream->header[1]); } while (p) { @@ -861,6 +998,7 @@ const FFOutputFormat ff_opus_muxer = { .p.extensions = "opus", .priv_data_size = sizeof(OGGContext), .p.audio_codec = AV_CODEC_ID_OPUS, + .p.video_codec = AV_CODEC_ID_PNG, .init = ogg_init, .write_header = ogg_write_header, .write_packet = ogg_write_packet, -- 2.34.1