From patchwork Sun Nov 19 19:08:45 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: 44723 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:6a89:b0:181:818d:5e7f with SMTP id bi9csp1183963pzb; Sun, 19 Nov 2023 11:09:05 -0800 (PST) X-Google-Smtp-Source: AGHT+IGdZWQD5kifAuAfcoKxnJlt36yp6hSsJSOwKF44FSmbrM+NkfAf4jrWnqzcSnyO/WAqwh+R X-Received: by 2002:ac2:5619:0:b0:509:8dee:71e with SMTP id v25-20020ac25619000000b005098dee071emr3663915lfd.0.1700420945169; Sun, 19 Nov 2023 11:09:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700420945; cv=none; d=google.com; s=arc-20160816; b=XJTn3YAnyMEmNYaZF7XWNMKAZp2wshX/OeHKS6tb7c4cZ85hY7bvwrgESkjL6Roj1s quFpbLVXSOWqlGlJbtUH50j0wScQqPedr2c64HfeQcQOp+RdTJiRumCpwoVGgAni9vnb 8hsRf/JRYltI4Cl7XyLzr7yH68yDhlE9tMEyFdrEkceDG4gLHHKZLHkJqpv9LdjgZKUI RWIiI585mOg6iLyf7Ld0pLJLef3jYGzMVzqI4jqL38XNZKQ8hMk8nH90ZtNXCuS9j/Qr CDjDgwScJGtVWGHT8rID9Xm5Oq+IjGUff7uQbem1kKqFIIZTIipYop73xoGj0BAeoKTH yuMA== 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=UWGHQMFNl4IHqETWnVAy5TLtdw2lFa6X+qcHbQa15KA=; fh=lvOXre93Kc+LM49rLl0yJcoSym9484cIB4O1jbJgoXk=; b=mPM6dNAwaBDPDVtqf5VMnB6vYI4E3WwsFooLX9lSWErv9m8C2jSzSWhb3ad8vQeI1m /bIOn4VkLNfv42/CipkagKvfxz+NbrT9hWBBZ2aPhlhtS5/Ag0NaplrurRMr5bKweuOf ERvxt9lk3LDNiZXvE9KLJhLOcWDsM0vjwcrJcnZdCJmOM1WJlnWJQEjEmv1pCjp9Z5Yd UwANnJCJPQ8h+UbjQll0utrRCp5WziCEzGyxBFJSv/Ny4lSV6EZVPyOtcdrfgaBJA/59 DLu8lMVuxF49o8v8iBmZi3NCIzfH72J6ONMLq5riKo4iVFzVNTYRUjSGYkEOuwYZnCbS oZjg== 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 n7-20020a509347000000b005412c0ba2fasi3490239eda.585.2023.11.19.11.09.04; Sun, 19 Nov 2023 11:09:05 -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 2E0EA68C9C5; Sun, 19 Nov 2023 21:09:01 +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 EDD4568AA4B for ; Sun, 19 Nov 2023 21:08:53 +0200 (EET) Date: Sun, 19 Nov 2023 19:08:45 +0000 To: FFmpeg development discussions and patches Message-ID: Feedback-ID: 28710920:user:proton MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v6 1/2] avformat/flac_picture: Add ff_flac_write_picture 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: +1L75MwuCqLM Attached. From 1b7a510ff5720d21868f0284c7a50489034bee7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zsolt=20Vad=C3=A1sz?= Date: Fri, 10 Mar 2023 11:23:13 +0000 Subject: [PATCH v6 1/2] avformat/flac_picture: Add ff_flac_write_picture This function is able to write cover art into both FLAC and Ogg files Signed-off-by: Zsolt Vadasz --- libavformat/flac_picture.c | 132 +++++++++++++++++++++++++++++++++++++ libavformat/flac_picture.h | 5 ++ libavformat/flacenc.c | 90 +------------------------ 3 files changed, 140 insertions(+), 87 deletions(-) diff --git a/libavformat/flac_picture.c b/libavformat/flac_picture.c index b33fee75b4..30152a2ba9 100644 --- a/libavformat/flac_picture.c +++ b/libavformat/flac_picture.c @@ -20,6 +20,9 @@ */ #include "libavutil/intreadwrite.h" +#include "libavutil/avstring.h" +#include "libavutil/base64.h" +#include "libavutil/pixdesc.h" #include "libavcodec/bytestream.h" #include "libavcodec/png.h" #include "avformat.h" @@ -188,3 +191,132 @@ fail: return ret; } + +int ff_flac_write_picture(struct AVFormatContext *s, + int isogg, + unsigned *attached_types, + int audio_stream_idx, // unused if !isogg + AVPacket *pkt) +{ + AVIOContext *pb = s->pb; + const AVPixFmtDescriptor *pixdesc; + const CodecMime *mime = ff_id3v2_mime_tags; + AVDictionaryEntry *e; + const char *mimetype = NULL, *desc = ""; + const AVStream *st = s->streams[pkt->stream_index]; + int i, mimelen, desclen, type = 0, blocklen; + + if (!pkt->data) + return 0; + + while (mime->id != AV_CODEC_ID_NONE) { + if (mime->id == st->codecpar->codec_id) { + mimetype = mime->str; + break; + } + mime++; + } + if (!mimetype) { + av_log(s, AV_LOG_ERROR, "No mimetype is known for stream %d, cannot " + "write an attached picture.\n", st->index); + return AVERROR(EINVAL); + } + mimelen = strlen(mimetype); + + /* get the picture type */ + e = av_dict_get(st->metadata, "comment", NULL, 0); + for (i = 0; e && i < FF_ARRAY_ELEMS(ff_id3v2_picture_types); i++) { + if (!av_strcasecmp(e->value, ff_id3v2_picture_types[i])) { + type = i; + break; + } + } + + if (((*attached_types) & (1 << type)) & 0x6) { + av_log(s, AV_LOG_ERROR, "Duplicate attachment for type '%s'\n", ff_id3v2_picture_types[type]); + return AVERROR(EINVAL); + } + + if (type == 1 && (st->codecpar->codec_id != AV_CODEC_ID_PNG || + st->codecpar->width != 32 || + st->codecpar->height != 32)) { + av_log(s, AV_LOG_ERROR, "File icon attachment must be a 32x32 PNG"); + return AVERROR(EINVAL); + } + + *attached_types |= (1 << type); + + /* get the description */ + if ((e = av_dict_get(st->metadata, "title", NULL, 0))) + desc = e->value; + desclen = strlen(desc); + + blocklen = 4 + 4 + mimelen + 4 + desclen + 4 + 4 + 4 + 4 + 4 + pkt->size; + if (blocklen >= 1<<24) { + av_log(s, AV_LOG_ERROR, "Picture block too big %d >= %d\n", blocklen, 1<<24); + return AVERROR(EINVAL); + } + + if(!isogg) { + avio_w8(pb, 0x06); + avio_wb24(pb, blocklen); + + avio_wb32(pb, type); + + avio_wb32(pb, mimelen); + avio_write(pb, mimetype, mimelen); + + avio_wb32(pb, desclen); + avio_write(pb, desc, desclen); + + avio_wb32(pb, st->codecpar->width); + avio_wb32(pb, st->codecpar->height); + if ((pixdesc = av_pix_fmt_desc_get(st->codecpar->format))) + avio_wb32(pb, av_get_bits_per_pixel(pixdesc)); + else + avio_wb32(pb, 0); + avio_wb32(pb, 0); + + avio_wb32(pb, pkt->size); + avio_write(pb, pkt->data, pkt->size); + } else { + uint8_t *metadata_block_picture, *ptr; + int encoded_len, ret; + char *encoded; + AVStream *audio_stream = s->streams[audio_stream_idx]; + + metadata_block_picture = av_mallocz(blocklen); + ptr = metadata_block_picture; + bytestream_put_be32(&ptr, type); + + bytestream_put_be32(&ptr, mimelen); + bytestream_put_buffer(&ptr, mimetype, mimelen); + + bytestream_put_be32(&ptr, desclen); + bytestream_put_buffer(&ptr, desc, desclen); + + bytestream_put_be32(&ptr, st->codecpar->width); + bytestream_put_be32(&ptr, st->codecpar->height); + if ((pixdesc = av_pix_fmt_desc_get(st->codecpar->format))) + bytestream_put_be32(&ptr, av_get_bits_per_pixel(pixdesc)); + else + bytestream_put_be32(&ptr, 0); + bytestream_put_be32(&ptr, 0); + + bytestream_put_be32(&ptr, pkt->size); + bytestream_put_buffer(&ptr, pkt->data, pkt->size); + + encoded_len = AV_BASE64_SIZE(blocklen); + encoded = av_mallocz(encoded_len); + av_base64_encode(encoded, encoded_len, metadata_block_picture, blocklen); + av_free(metadata_block_picture); + + ret = av_dict_set(&audio_stream->metadata, "METADATA_BLOCK_PICTURE", encoded, 0); + av_free(encoded); + av_packet_unref(pkt); + + if (ret < 0) + return ret; + } + return 0; +} diff --git a/libavformat/flac_picture.h b/libavformat/flac_picture.h index db074e531d..efa11aee32 100644 --- a/libavformat/flac_picture.h +++ b/libavformat/flac_picture.h @@ -39,5 +39,10 @@ */ int ff_flac_parse_picture(AVFormatContext *s, uint8_t **buf, int buf_size, int truncate_workaround); +int ff_flac_write_picture(struct AVFormatContext *s, + int isogg, + unsigned *attached_types, + int audio_stream_idx, + AVPacket *pkt); #endif /* AVFORMAT_FLAC_PICTURE_H */ diff --git a/libavformat/flacenc.c b/libavformat/flacenc.c index a8beec7750..7970c2531d 100644 --- a/libavformat/flacenc.c +++ b/libavformat/flacenc.c @@ -33,6 +33,7 @@ #include "mux.h" #include "version.h" #include "vorbiscomment.h" +#include "flac_picture.h" typedef struct FlacMuxerContext { @@ -79,94 +80,9 @@ static int flac_write_block_comment(AVIOContext *pb, AVDictionary **m, return 0; } -static int flac_write_picture(struct AVFormatContext *s, AVPacket *pkt) -{ - FlacMuxerContext *c = s->priv_data; - AVIOContext *pb = s->pb; - const AVPixFmtDescriptor *pixdesc; - const CodecMime *mime = ff_id3v2_mime_tags; - AVDictionaryEntry *e; - const char *mimetype = NULL, *desc = ""; - const AVStream *st = s->streams[pkt->stream_index]; - int i, mimelen, desclen, type = 0, blocklen; - - if (!pkt->data) - return 0; - - while (mime->id != AV_CODEC_ID_NONE) { - if (mime->id == st->codecpar->codec_id) { - mimetype = mime->str; - break; - } - mime++; - } - if (!mimetype) { - av_log(s, AV_LOG_ERROR, "No mimetype is known for stream %d, cannot " - "write an attached picture.\n", st->index); - return AVERROR(EINVAL); - } - mimelen = strlen(mimetype); - - /* get the picture type */ - e = av_dict_get(st->metadata, "comment", NULL, 0); - for (i = 0; e && i < FF_ARRAY_ELEMS(ff_id3v2_picture_types); i++) { - if (!av_strcasecmp(e->value, ff_id3v2_picture_types[i])) { - type = i; - break; - } - } - - if ((c->attached_types & (1 << type)) & 0x6) { - av_log(s, AV_LOG_ERROR, "Duplicate attachment for type '%s'\n", ff_id3v2_picture_types[type]); - return AVERROR(EINVAL); - } - - if (type == 1 && (st->codecpar->codec_id != AV_CODEC_ID_PNG || - st->codecpar->width != 32 || - st->codecpar->height != 32)) { - av_log(s, AV_LOG_ERROR, "File icon attachment must be a 32x32 PNG"); - return AVERROR(EINVAL); - } - - c->attached_types |= (1 << type); - - /* get the description */ - if ((e = av_dict_get(st->metadata, "title", NULL, 0))) - desc = e->value; - desclen = strlen(desc); - - blocklen = 4 + 4 + mimelen + 4 + desclen + 4 + 4 + 4 + 4 + 4 + pkt->size; - if (blocklen >= 1<<24) { - av_log(s, AV_LOG_ERROR, "Picture block too big %d >= %d\n", blocklen, 1<<24); - return AVERROR(EINVAL); - } - - avio_w8(pb, 0x06); - avio_wb24(pb, blocklen); - - avio_wb32(pb, type); - - avio_wb32(pb, mimelen); - avio_write(pb, mimetype, mimelen); - - avio_wb32(pb, desclen); - avio_write(pb, desc, desclen); - - avio_wb32(pb, st->codecpar->width); - avio_wb32(pb, st->codecpar->height); - if ((pixdesc = av_pix_fmt_desc_get(st->codecpar->format))) - avio_wb32(pb, av_get_bits_per_pixel(pixdesc)); - else - avio_wb32(pb, 0); - avio_wb32(pb, 0); - - avio_wb32(pb, pkt->size); - avio_write(pb, pkt->data, pkt->size); - return 0; -} - static int flac_finish_header(struct AVFormatContext *s) { + FlacMuxerContext *c = s->priv_data; int i, ret, padding = s->metadata_header_padding; if (padding < 0) padding = 8192; @@ -179,7 +95,7 @@ static int flac_finish_header(struct AVFormatContext *s) AVPacket *pkt = st->priv_data; if (!pkt) continue; - ret = flac_write_picture(s, pkt); + ret = ff_flac_write_picture(s, 0, &c->attached_types, -1, pkt); av_packet_unref(pkt); if (ret < 0 && (s->error_recognition & AV_EF_EXPLODE)) return ret; -- 2.34.1