[FFmpeg-devel] [PATCH v3 0/1] avcodec/libjxlenc: Add libjxl_anim encoder

2023-12-21 Thread Zsolt Vadász via ffmpeg-devel
This patchset adds support for encoding animated JPEG XL images via a
new encoder (libjxl_anim). When using the encoder, the output format
needs to be set to raw video, as shown below:
`ffmpeg -i sample.gif -c:v libjxl_anim -f rawvideo out.jxl`

V3 changes:
- Renamed encoder to libjxl_anim for consistency
- PTS is now calculated
- JxlFrameHeader.duration is calculated from PTS instead of relying on
  AVFrame->duration being set
- This version also does not rely on AVCodecContext->frame_num either
- Disabled the container container format (JxlEncoderUseContainer)

Zsolt Vadász (1):
  avcodec/libjxlenc: Add libjxl_animated encoder

 configure  |   1 +
 libavcodec/allcodecs.c |   1 +
 libavcodec/libjxlenc.c | 226 ++---
 3 files changed, 189 insertions(+), 39 deletions(-)

-- 
2.34.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v3 1/1] avcodec/libjxlenc: Add libjxl_animated encoder

2023-12-21 Thread Zsolt Vadász via ffmpeg-devel
---
 configure  |   1 +
 libavcodec/allcodecs.c |   1 +
 libavcodec/libjxlenc.c | 226 ++---
 3 files changed, 189 insertions(+), 39 deletions(-)

diff --git a/configure b/configure
index 7d2ee66000..2d27d8015a 100755
--- a/configure
+++ b/configure
@@ -3418,6 +3418,7 @@ libilbc_decoder_deps="libilbc"
 libilbc_encoder_deps="libilbc"
 libjxl_decoder_deps="libjxl libjxl_threads"
 libjxl_encoder_deps="libjxl libjxl_threads"
+libjxl_anim_encoder_deps="libjxl libjxl_threads"
 libkvazaar_encoder_deps="libkvazaar"
 libmodplug_demuxer_deps="libmodplug"
 libmp3lame_encoder_deps="libmp3lame"
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index b0f004e15c..22c7227946 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -784,6 +784,7 @@ extern const FFCodec ff_libilbc_encoder;
 extern const FFCodec ff_libilbc_decoder;
 extern const FFCodec ff_libjxl_decoder;
 extern const FFCodec ff_libjxl_encoder;
+extern const FFCodec ff_libjxl_anim_encoder;
 extern const FFCodec ff_libmp3lame_encoder;
 extern const FFCodec ff_libopencore_amrnb_encoder;
 extern const FFCodec ff_libopencore_amrnb_decoder;
diff --git a/libavcodec/libjxlenc.c b/libavcodec/libjxlenc.c
index d707f3a61b..f23e86939e 100644
--- a/libavcodec/libjxlenc.c
+++ b/libavcodec/libjxlenc.c
@@ -34,6 +34,7 @@
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/pixfmt.h"
+#include "libavutil/rational.h"
 #include "libavutil/version.h"
 
 #include "avcodec.h"
@@ -49,11 +50,18 @@ typedef struct LibJxlEncodeContext {
 void *runner;
 JxlEncoder *encoder;
 JxlEncoderFrameSettings *options;
+JxlPixelFormat jxl_fmt;
 int effort;
 float distance;
 int modular;
 uint8_t *buffer;
 size_t buffer_size;
+int animated;
+/* Only used by libjxl_animated */
+AVRational libjxl_time_base;
+int is_first_frame;
+int64_t duration;
+AVFrame *prev;
 } LibJxlEncodeContext;
 
 /**
@@ -183,6 +191,8 @@ static av_cold int libjxl_encode_init(AVCodecContext *avctx)
 return AVERROR(ENOMEM);
 }
 
+ctx->animated = 0;
+
 return 0;
 }
 
@@ -237,28 +247,19 @@ static int libjxl_populate_primaries(void *avctx, 
JxlColorEncoding *jxl_color, e
 return 0;
 }
 
-/**
- * Encode an entire frame. Currently animation, is not supported by
- * this encoder, so this will always reinitialize a new still image
- * and encode a one-frame image (for image2 and image2pipe).
- */
-static int libjxl_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const 
AVFrame *frame, int *got_packet)
+static int libjxl_encode_init_image(AVCodecContext *avctx, const AVFrame 
*frame)
 {
 LibJxlEncodeContext *ctx = avctx->priv_data;
 AVFrameSideData *sd;
 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(frame->format);
 JxlBasicInfo info;
 JxlColorEncoding jxl_color;
-JxlPixelFormat jxl_fmt;
+JxlPixelFormat *jxl_fmt = >jxl_fmt;
 int bits_per_sample;
 #if JPEGXL_NUMERIC_VERSION >= JPEGXL_COMPUTE_NUMERIC_VERSION(0, 8, 0)
 JxlBitDepth jxl_bit_depth;
 #endif
-JxlEncoderStatus jret;
 int ret;
-size_t available = ctx->buffer_size;
-size_t bytes_written = 0;
-uint8_t *next_out = ctx->buffer;
 
 ret = libjxl_init_jxl_encoder(avctx);
 if (ret) {
@@ -268,23 +269,30 @@ static int libjxl_encode_frame(AVCodecContext *avctx, 
AVPacket *pkt, const AVFra
 
 /* populate the basic info settings */
 JxlEncoderInitBasicInfo();
-jxl_fmt.num_channels = pix_desc->nb_components;
+jxl_fmt->num_channels = pix_desc->nb_components;
 info.xsize = frame->width;
 info.ysize = frame->height;
-info.num_extra_channels = (jxl_fmt.num_channels + 1) % 2;
-info.num_color_channels = jxl_fmt.num_channels - info.num_extra_channels;
-bits_per_sample = av_get_bits_per_pixel(pix_desc) / jxl_fmt.num_channels;
+info.num_extra_channels = (jxl_fmt->num_channels + 1) % 2;
+info.num_color_channels = jxl_fmt->num_channels - info.num_extra_channels;
+bits_per_sample = av_get_bits_per_pixel(pix_desc) / jxl_fmt->num_channels;
 info.bits_per_sample = avctx->bits_per_raw_sample > 0 && !(pix_desc->flags 
& AV_PIX_FMT_FLAG_FLOAT)
? avctx->bits_per_raw_sample : bits_per_sample;
 info.alpha_bits = (info.num_extra_channels > 0) * info.bits_per_sample;
 if (pix_desc->flags & AV_PIX_FMT_FLAG_FLOAT) {
 info.exponent_bits_per_sample = info.bits_per_sample > 16 ? 8 : 5;
 info.alpha_exponent_bits = info.alpha_bits ? 
info.exponent_bits_per_sample : 0;
-jxl_fmt.data_type = info.bits_per_sample > 16 ? JXL_TYPE_FLOAT : 
JXL_TYPE_FLOAT16;
+jxl_fmt->data_type = info.bits_per_sample > 16 ? JXL_TYPE_FLOAT : 
JXL_TYPE_FLOAT16;
 } else {
 info.exponent_bits_per_sample = 0;
 info.alpha_exponent_bits = 0;
-jxl_fmt.data_type = info.bits_per_sample <= 8 ? JXL_TYPE_UINT8 : 
JXL_TYPE_UINT16;
+

Re: [FFmpeg-devel] [PATCH v2 1/1] avcodec/libjxlenc: Add libjxl_animated encoder

2023-12-19 Thread Zsolt Vadász via ffmpeg-devel
On Friday, December 15th, 2023 at 11:18 PM, Leo Izen  wrote:


> On 12/15/23 16:31, Zsolt Vadász via ffmpeg-devel wrote:
> 
> > On Friday, December 15th, 2023 at 10:12 PM, Leo Izen leo.i...@gmail.com 
> > wrote:
> > 
> > > On 12/15/23 11:40, Zsolt Vadász via ffmpeg-devel wrote:
> > > 
> > > > On Friday, December 15th, 2023 at 12:20 AM, Leo Izen leo.i...@gmail.com 
> > > > wrote:
> > > > 
> > > > > > + AVFrame *last;
> > > > > 
> > > > > I don't see the purpose of keeping this here.
> > > > 
> > > > The name is misleading, I should have named it `previous`, since it 
> > > > always contains the previous frame.
> > > > I did it this way so I could call JxlEncoderCloseInput when the 
> > > > callback received NULL.
> > > 
> > > This isn't needed to call JxlEncoderCloseInput, as it takes one
> > > argument, which is JxlEncoder *.
> > 
> > Indeed, but according to the docs[0]:
> > "If the last frame or last box has been added, JxlEncoderCloseInput, 
> > JxlEncoderCloseFrames
> > and/or JxlEncoderCloseBoxes must be called before the next 
> > JxlEncoderProcessOutput call,
> > or the codestream won’t be encoded correctly."
> > When the encoder eventually receives NULL, there isn't anything left to 
> > add, unless I store the previous frame,
> > is there?
> 
> 
> You can always call JxlEncoderProcessOutput at the start of the loop,
> rather than calling it after JxlEncoderAddImageFrame. (Except perhaps
> the first loop.)
> 
> If you do this, you'll process the frame that was added last loop, and
> then you can add another frame. If you receive NULL, you call
> JxlEncoderCloseInput at the start of the loop, before you call
> JxlEncoderProcessOutput. This saves you from having to store the AVFrame
> in memory. libjxl will have a copy of it in memory anyway for reference
> purposes so this allows us to reduce memory usage.
> 

Thanks, this actually completely crossed my mind.
However, this complicates the PTS related parts, specifically calculating
JxlFrameHeader.duration from PTS for the first frame (since you said that
AVFrame->duration is unreliable). There'd still have to be an AVFrame
stored in the context because of it, since we don't have two PTSes yet
that we can subtract from one another. IMO it would look a tad bit cleaner
to keep the extra AVFrame in the encode context.
> > > > > > +
> > > > > > + if(!ctx->last && !avctx->internal->draining) { > + ctx->last = 
> > > > > > av_frame_clone(frame);
> > > > > > + *got_packet = 0;
> > > > > > + return AVERROR(EAGAIN);
> > > > > 
> > > > > It looks like you're trying to emit one packet per image, rather than
> > > > > one packet per encoded frame. This is fine, but you should not be
> > > > > calling av_frame_clone, and there's no reason to use
> > > > > avctx->internal->draining here. If you are doing this, you also have 
> > > > > no
> > > > > reason to cache ctx->last at all.
> > > > 
> > > > It's the opposite, I'm trying to emit a packet for each frame of the 
> > > > animation.
> > > 
> > > Libjxl provides no promise of doing this meaningfully, by the way. You
> > > may end up with arbitrary subdivisions, not subdivisions on frame
> > > boundaries.
> > 
> > Well that's one thing I didn't account for, a real pity. Do you recommend 
> > emitting a
> > single packet instead?
> 
> 
> You can still accomplish what you're trying to do with libjxl but you
> need to finesse it a little bit. You need to (1) disable the container
> format, as its interaction with Frames is undefined, and you need to (2)
> loop calls to JxlEncoderProcessOutput until it returns JXL_ENC_SUCCESS.
> 
> The docs specify "This encodes the frames and/or boxes added so far" so
> it will end up flushing out the remaining frames if the container format
> is disabled.
> 
> > > > > > +const FFCodec ff_libjxl_animated_encoder = {
> > > > > > + .p.name = "libjxl_animated",
> > > > > > + CODEC_LONG_NAME("libjxl Animated JPEG XL"),
> > > > > > + .p.type = AVMEDIA_TYPE_VIDEO,
> > > > > > + .p.id = AV_CODEC_ID_JPEGXL,
> > > > > > + .priv_data_size = sizeof(LibJxlEncodeContext),
> > > > > > + .init = libjxl_animated_encode_init,
> > > > > > + FF_CODEC_ENCODE_CB(li

Re: [FFmpeg-devel] [PATCH v2 1/1] avcodec/libjxlenc: Add libjxl_animated encoder

2023-12-15 Thread Zsolt Vadász via ffmpeg-devel
On Friday, December 15th, 2023 at 10:12 PM, Leo Izen  wrote:


> On 12/15/23 11:40, Zsolt Vadász via ffmpeg-devel wrote:
> 
> > On Friday, December 15th, 2023 at 12:20 AM, Leo Izen leo.i...@gmail.com 
> > wrote:
> > 
> > > > + AVFrame *last;
> > > 
> > > I don't see the purpose of keeping this here.
> > 
> > The name is misleading, I should have named it `previous`, since it always 
> > contains the previous frame.
> > I did it this way so I could call JxlEncoderCloseInput when the callback 
> > received NULL.
> 
> 
> This isn't needed to call JxlEncoderCloseInput, as it takes one
> argument, which is JxlEncoder *.

Indeed, but according to the docs[0]:
"If the last frame or last box has been added, JxlEncoderCloseInput, 
JxlEncoderCloseFrames
and/or JxlEncoderCloseBoxes must be called before the next 
JxlEncoderProcessOutput call,
or the codestream won’t be encoded correctly."
When the encoder eventually receives NULL, there isn't anything left to add, 
unless I store the previous frame,
is there?
> > > > +
> > > > + if(!ctx->last && !avctx->internal->draining) { > + ctx->last = 
> > > > av_frame_clone(frame);
> > > > + *got_packet = 0;
> > > > + return AVERROR(EAGAIN);
> > > 
> > > It looks like you're trying to emit one packet per image, rather than
> > > one packet per encoded frame. This is fine, but you should not be
> > > calling av_frame_clone, and there's no reason to use
> > > avctx->internal->draining here. If you are doing this, you also have no
> > > reason to cache ctx->last at all.
> > 
> > It's the opposite, I'm trying to emit a packet for each frame of the 
> > animation.
> 
> 
> Libjxl provides no promise of doing this meaningfully, by the way. You
> may end up with arbitrary subdivisions, not subdivisions on frame
> boundaries.
> 
Well that's one thing I didn't account for, a real pity. Do you recommend 
emitting a
single packet instead?
> > > > +const FFCodec ff_libjxl_animated_encoder = {
> > > > + .p.name = "libjxl_animated",
> > > > + CODEC_LONG_NAME("libjxl Animated JPEG XL"),
> > > > + .p.type = AVMEDIA_TYPE_VIDEO,
> > > > + .p.id = AV_CODEC_ID_JPEGXL,
> > > > + .priv_data_size = sizeof(LibJxlEncodeContext),
> > > > + .init = libjxl_animated_encode_init,
> > > > + FF_CODEC_ENCODE_CB(libjxl_animated_encode_frame),
> > > > + .close = libjxl_encode_close,
> > > > + .p.capabilities = AV_CODEC_CAP_OTHER_THREADS |
> > > > + AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE |
> > > > + AV_CODEC_CAP_DELAY,
> > > > + .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE |
> > > > + FF_CODEC_CAP_AUTO_THREADS | FF_CODEC_CAP_INIT_CLEANUP |
> > > > + FF_CODEC_CAP_ICC_PROFILES | FF_CODEC_CAP_EOF_FLUSH,
> > > 
> > > Why FF_CODEC_CAP_EOF_FLUSH?
> > 
> > So the encoder receives a NULL after the last frame has been submitted,
> > so JxlEncoderCloseInput can be called and the final frame properly emitted.
> 
> 
> Ah, that makes sense, thanks.
> ___
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> To unsubscribe, visit link above, or email
> ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[0] 
https://libjxl.readthedocs.io/en/latest/api_encoder.html#_CPPv423JxlEncoderProcessOutputP10JxlEncoderPP7uint8_tP6size_t
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH v2 1/1] avcodec/libjxlenc: Add libjxl_animated encoder

2023-12-15 Thread Zsolt Vadász via ffmpeg-devel
On Friday, December 15th, 2023 at 12:20 AM, Leo Izen  wrote:


> > + AVFrame *last;
> 
> 
> I don't see the purpose of keeping this here.
> 

The name is misleading, I should have named it `previous`, since it always 
contains the previous frame.
I did it this way so I could call JxlEncoderCloseInput when the callback 
received NULL.
> > +
> > + if(!ctx->last && !avctx->internal->draining) { > + ctx->last = 
> > av_frame_clone(frame);
> > + *got_packet = 0;
> > + return AVERROR(EAGAIN);
> 
> It looks like you're trying to emit one packet per image, rather than
> one packet per encoded frame. This is fine, but you should not be
> calling av_frame_clone, and there's no reason to use
> avctx->internal->draining here. If you are doing this, you also have no
> 
> reason to cache ctx->last at all.

It's the opposite, I'm trying to emit a packet for each frame of the animation.
> > +const FFCodec ff_libjxl_animated_encoder = {
> > + .p.name = "libjxl_animated",
> > + CODEC_LONG_NAME("libjxl Animated JPEG XL"),
> > + .p.type = AVMEDIA_TYPE_VIDEO,
> > + .p.id = AV_CODEC_ID_JPEGXL,
> > + .priv_data_size = sizeof(LibJxlEncodeContext),
> > + .init = libjxl_animated_encode_init,
> > + FF_CODEC_ENCODE_CB(libjxl_animated_encode_frame),
> > + .close = libjxl_encode_close,
> > + .p.capabilities = AV_CODEC_CAP_OTHER_THREADS |
> > + AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE |
> > + AV_CODEC_CAP_DELAY,
> > + .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE |
> > + FF_CODEC_CAP_AUTO_THREADS | FF_CODEC_CAP_INIT_CLEANUP |
> > + FF_CODEC_CAP_ICC_PROFILES | FF_CODEC_CAP_EOF_FLUSH,
> 
> 
> Why FF_CODEC_CAP_EOF_FLUSH?

So the encoder receives a NULL after the last frame has been submitted,
so JxlEncoderCloseInput can be called and the final frame properly emitted.
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v2 1/1] avcodec/libjxlenc: Add libjxl_animated encoder

2023-12-13 Thread Zsolt Vadász via ffmpeg-devel
---
 configure  |   1 +
 libavcodec/allcodecs.c |   1 +
 libavcodec/libjxlenc.c | 214 +
 3 files changed, 177 insertions(+), 39 deletions(-)

diff --git a/configure b/configure
index 7d2ee66000..a334a9b1e0 100755
--- a/configure
+++ b/configure
@@ -3418,6 +3418,7 @@ libilbc_decoder_deps="libilbc"
 libilbc_encoder_deps="libilbc"
 libjxl_decoder_deps="libjxl libjxl_threads"
 libjxl_encoder_deps="libjxl libjxl_threads"
+libjxl_animated_encoder_deps="libjxl libjxl_threads"
 libkvazaar_encoder_deps="libkvazaar"
 libmodplug_demuxer_deps="libmodplug"
 libmp3lame_encoder_deps="libmp3lame"
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index b0f004e15c..e6733b0d4f 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -784,6 +784,7 @@ extern const FFCodec ff_libilbc_encoder;
 extern const FFCodec ff_libilbc_decoder;
 extern const FFCodec ff_libjxl_decoder;
 extern const FFCodec ff_libjxl_encoder;
+extern const FFCodec ff_libjxl_animated_encoder;
 extern const FFCodec ff_libmp3lame_encoder;
 extern const FFCodec ff_libopencore_amrnb_encoder;
 extern const FFCodec ff_libopencore_amrnb_decoder;
diff --git a/libavcodec/libjxlenc.c b/libavcodec/libjxlenc.c
index d707f3a61b..bf44307a34 100644
--- a/libavcodec/libjxlenc.c
+++ b/libavcodec/libjxlenc.c
@@ -39,6 +39,7 @@
 #include "avcodec.h"
 #include "encode.h"
 #include "codec_internal.h"
+#include "internal.h"
 
 #include 
 #include 
@@ -49,11 +50,15 @@ typedef struct LibJxlEncodeContext {
 void *runner;
 JxlEncoder *encoder;
 JxlEncoderFrameSettings *options;
+JxlPixelFormat jxl_fmt;
 int effort;
 float distance;
 int modular;
 uint8_t *buffer;
 size_t buffer_size;
+/* Only used by libjxl_animated */
+int animated;
+AVFrame *last;
 } LibJxlEncodeContext;
 
 /**
@@ -183,6 +188,8 @@ static av_cold int libjxl_encode_init(AVCodecContext *avctx)
 return AVERROR(ENOMEM);
 }
 
+ctx->animated = 0;
+
 return 0;
 }
 
@@ -237,28 +244,19 @@ static int libjxl_populate_primaries(void *avctx, 
JxlColorEncoding *jxl_color, e
 return 0;
 }
 
-/**
- * Encode an entire frame. Currently animation, is not supported by
- * this encoder, so this will always reinitialize a new still image
- * and encode a one-frame image (for image2 and image2pipe).
- */
-static int libjxl_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const 
AVFrame *frame, int *got_packet)
+static int libjxl_encode_init_image(AVCodecContext *avctx, const AVFrame 
*frame)
 {
 LibJxlEncodeContext *ctx = avctx->priv_data;
 AVFrameSideData *sd;
 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(frame->format);
 JxlBasicInfo info;
 JxlColorEncoding jxl_color;
-JxlPixelFormat jxl_fmt;
+JxlPixelFormat *jxl_fmt = >jxl_fmt;
 int bits_per_sample;
 #if JPEGXL_NUMERIC_VERSION >= JPEGXL_COMPUTE_NUMERIC_VERSION(0, 8, 0)
 JxlBitDepth jxl_bit_depth;
 #endif
-JxlEncoderStatus jret;
 int ret;
-size_t available = ctx->buffer_size;
-size_t bytes_written = 0;
-uint8_t *next_out = ctx->buffer;
 
 ret = libjxl_init_jxl_encoder(avctx);
 if (ret) {
@@ -268,23 +266,30 @@ static int libjxl_encode_frame(AVCodecContext *avctx, 
AVPacket *pkt, const AVFra
 
 /* populate the basic info settings */
 JxlEncoderInitBasicInfo();
-jxl_fmt.num_channels = pix_desc->nb_components;
+jxl_fmt->num_channels = pix_desc->nb_components;
 info.xsize = frame->width;
 info.ysize = frame->height;
-info.num_extra_channels = (jxl_fmt.num_channels + 1) % 2;
-info.num_color_channels = jxl_fmt.num_channels - info.num_extra_channels;
-bits_per_sample = av_get_bits_per_pixel(pix_desc) / jxl_fmt.num_channels;
+info.num_extra_channels = (jxl_fmt->num_channels + 1) % 2;
+info.num_color_channels = jxl_fmt->num_channels - info.num_extra_channels;
+bits_per_sample = av_get_bits_per_pixel(pix_desc) / jxl_fmt->num_channels;
 info.bits_per_sample = avctx->bits_per_raw_sample > 0 && !(pix_desc->flags 
& AV_PIX_FMT_FLAG_FLOAT)
? avctx->bits_per_raw_sample : bits_per_sample;
 info.alpha_bits = (info.num_extra_channels > 0) * info.bits_per_sample;
 if (pix_desc->flags & AV_PIX_FMT_FLAG_FLOAT) {
 info.exponent_bits_per_sample = info.bits_per_sample > 16 ? 8 : 5;
 info.alpha_exponent_bits = info.alpha_bits ? 
info.exponent_bits_per_sample : 0;
-jxl_fmt.data_type = info.bits_per_sample > 16 ? JXL_TYPE_FLOAT : 
JXL_TYPE_FLOAT16;
+jxl_fmt->data_type = info.bits_per_sample > 16 ? JXL_TYPE_FLOAT : 
JXL_TYPE_FLOAT16;
 } else {
 info.exponent_bits_per_sample = 0;
 info.alpha_exponent_bits = 0;
-jxl_fmt.data_type = info.bits_per_sample <= 8 ? JXL_TYPE_UINT8 : 
JXL_TYPE_UINT16;
+jxl_fmt->data_type = info.bits_per_sample <= 8 ? JXL_TYPE_UINT8 : 
JXL_TYPE_UINT16;
+}
+if(ctx->animated) {
+info.have_animation = 1;
+  

[FFmpeg-devel] [PATCH v2 0/1] avcodec/libjxlenc: Add libjxl_animated encoder

2023-12-13 Thread Zsolt Vadász via ffmpeg-devel
This patchset adds support for encoding animated JPEG XL images via a
new encoder (libjxl_animated). When using the encoder, the output format
needs to be set to raw video, as shown below:
`ffmpeg -i sample.gif -c:v libjxl_animated -f rawvideo out.jxl`

V2 changes:
- This version doesn't move the JxlBasicInfo struct into
LibJxlEncodeContext (the JxlPixelFormat struct is still in the encoder context 
because
it's used in multiple functions, while the encode callbacks only use
the `ysize` from the JxlBasicInfo, which is just `frame-height`)
- It also doesn't overwrite `avctx->time_base`
- Builds that disable libjxl now succeed

Zsolt Vadasz (1):
  avcodec/libjxlenc: Add libjxl_animated encoder

 configure  |   1 +
 libavcodec/allcodecs.c |   1 +
 libavcodec/libjxlenc.c | 214 +
 3 files changed, 177 insertions(+), 39 deletions(-)

-- 
2.43.0

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH 1/3] avcodec/libjxlenc: Move JxlBasicInfo to LibJxlEncodeContext

2023-12-13 Thread Zsolt Vadász via ffmpeg-devel

On Tuesday, December 12th, 2023 at 9:17 PM, Leo Izen  wrote:
> On 12/11/23 12:05, Zsolt Vadász via ffmpeg-devel wrote:
> 
> > ---
> > libavcodec/libjxlenc.c | 45 +-
> > 1 file changed, 23 insertions(+), 22 deletions(-)
> > 
> > diff --git a/libavcodec/libjxlenc.c b/libavcodec/libjxlenc.c
> > index d707f3a61b..92a458d51a 100644
> 
> 
> Why?
My apologies, I have to admit this patch set has a lot of things left over from 
me figuring out how to go about patching the code. Will clean it up for V2.
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 3/3] avcodec/libjxlenc: Add libjxl_animated encoder

2023-12-11 Thread Zsolt Vadász via ffmpeg-devel
---
 libavcodec/allcodecs.c |   1 +
 libavcodec/libjxlenc.c | 197 -
 2 files changed, 157 insertions(+), 41 deletions(-)

diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index b0f004e15c..e6733b0d4f 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -784,6 +784,7 @@ extern const FFCodec ff_libilbc_encoder;
 extern const FFCodec ff_libilbc_decoder;
 extern const FFCodec ff_libjxl_decoder;
 extern const FFCodec ff_libjxl_encoder;
+extern const FFCodec ff_libjxl_animated_encoder;
 extern const FFCodec ff_libmp3lame_encoder;
 extern const FFCodec ff_libopencore_amrnb_encoder;
 extern const FFCodec ff_libopencore_amrnb_decoder;
diff --git a/libavcodec/libjxlenc.c b/libavcodec/libjxlenc.c
index 6110c42a7c..5d437b2c05 100644
--- a/libavcodec/libjxlenc.c
+++ b/libavcodec/libjxlenc.c
@@ -31,14 +31,17 @@
 #include "libavutil/error.h"
 #include "libavutil/frame.h"
 #include "libavutil/libm.h"
+#include "libavutil/log.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/pixfmt.h"
 #include "libavutil/version.h"

+#include "packet.h"
 #include "avcodec.h"
 #include "encode.h"
 #include "codec_internal.h"
+#include "internal.h"

 #include 
 #include 
@@ -51,13 +54,14 @@ typedef struct LibJxlEncodeContext {
 JxlEncoderFrameSettings *options;
 JxlBasicInfo info;
 JxlPixelFormat jxl_fmt;
-int animated;
-int first_frame;
 int effort;
 float distance;
 int modular;
 uint8_t *buffer;
 size_t buffer_size;
+/* Only used by libjxl-animated */
+AVFrame *last;
+int animated;
 } LibJxlEncodeContext;

 /**
@@ -185,7 +189,6 @@ static av_cold int libjxl_encode_init(AVCodecContext *avctx)
 }

 ctx->animated = 0;
-ctx->first_frame = 1;

 return 0;
 }
@@ -284,6 +287,14 @@ static int libjxl_encode_init_image(AVCodecContext *avctx, 
const AVFrame *frame)
 info->alpha_exponent_bits = 0;
 jxl_fmt->data_type = info->bits_per_sample <= 8 ? JXL_TYPE_UINT8 : 
JXL_TYPE_UINT16;
 }
+if(ctx->animated) {
+info->have_animation = 1;
+info->animation.have_timecodes = 0;
+info->animation.num_loops = 0;
+info->animation.tps_numerator = frame->time_base.den;
+info->animation.tps_denominator = frame->time_base.num;
+avctx->time_base = frame->time_base;
+}

 #if JPEGXL_NUMERIC_VERSION >= JPEGXL_COMPUTE_NUMERIC_VERSION(0, 8, 0)
 jxl_bit_depth.bits_per_sample = bits_per_sample;
@@ -386,37 +397,12 @@ static int libjxl_encode_init_image(AVCodecContext 
*avctx, const AVFrame *frame)
 return 0;
 }

-/**
- * Encode an entire frame. Currently animation, is not supported by
- * this encoder, so this will always reinitialize a new still image
- * and encode a one-frame image (for image2 and image2pipe).
- */
-static int libjxl_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const 
AVFrame *frame, int *got_packet)
+static int libjxl_encode_process_output(AVCodecContext *avctx, size_t 
*bytes_written)
 {
 LibJxlEncodeContext *ctx = avctx->priv_data;
 JxlEncoderStatus jret;
-JxlBasicInfo *info = >info;
-JxlPixelFormat *jxl_fmt = >jxl_fmt;
-int ret;
-size_t available = ctx->buffer_size;
-size_t bytes_written = 0;
 uint8_t *next_out = ctx->buffer;
-
-if(!ctx->animated || ctx->first_frame) {
-if((ret = libjxl_encode_init_image(avctx, frame)) < 0)
-return ret;
-ctx->first_frame = 0;
-}
-
-if (JxlEncoderAddImageFrame(ctx->options, jxl_fmt, frame->data[0], 
jxl_fmt->align * info->ysize) != JXL_ENC_SUCCESS) {
-av_log(avctx, AV_LOG_ERROR, "Failed to add Image Frame\n");
-return AVERROR_EXTERNAL;
-}
-
-/*
- * Run this after the last frame in the image has been passed.
- */
-JxlEncoderCloseInput(ctx->encoder);
+size_t available = ctx->buffer_size;

 while (1) {
 jret = JxlEncoderProcessOutput(ctx->encoder, _out, );
@@ -424,7 +410,7 @@ static int libjxl_encode_frame(AVCodecContext *avctx, 
AVPacket *pkt, const AVFra
 av_log(avctx, AV_LOG_ERROR, "Unspecified libjxl error occurred\n");
 return AVERROR_EXTERNAL;
 }
-bytes_written = ctx->buffer_size - available;
+*bytes_written = ctx->buffer_size - available;
 /* all data passed has been encoded */
 if (jret == JXL_ENC_SUCCESS)
 break;
@@ -441,14 +427,46 @@ static int libjxl_encode_frame(AVCodecContext *avctx, 
AVPacket *pkt, const AVFra
 return AVERROR(ENOMEM);
 ctx->buffer = temp;
 ctx->buffer_size = new_size;
-next_out = ctx->buffer + bytes_written;
-available = new_size - bytes_written;
+next_out = ctx->buffer + *bytes_written;
+available = new_size - *bytes_written;
 continue;
 }
 av_log(avctx, AV_LOG_ERROR, "Bad libjxl event: %d\n", jret);
 return 

[FFmpeg-devel] [PATCH 1/3] avcodec/libjxlenc: Move JxlBasicInfo to LibJxlEncodeContext

2023-12-11 Thread Zsolt Vadász via ffmpeg-devel
---
 libavcodec/libjxlenc.c | 45 +-
 1 file changed, 23 insertions(+), 22 deletions(-)

diff --git a/libavcodec/libjxlenc.c b/libavcodec/libjxlenc.c
index d707f3a61b..92a458d51a 100644
--- a/libavcodec/libjxlenc.c
+++ b/libavcodec/libjxlenc.c
@@ -49,6 +49,7 @@ typedef struct LibJxlEncodeContext {
 void *runner;
 JxlEncoder *encoder;
 JxlEncoderFrameSettings *options;
+JxlBasicInfo info;
 int effort;
 float distance;
 int modular;
@@ -95,9 +96,6 @@ static int libjxl_init_jxl_encoder(AVCodecContext *avctx)
 {
 LibJxlEncodeContext *ctx = avctx->priv_data;
 
-/* reset the encoder every frame for image2 muxer */
-JxlEncoderReset(ctx->encoder);
-
 ctx->options = JxlEncoderFrameSettingsCreate(ctx->encoder, NULL);
 if (!ctx->options) {
 av_log(avctx, AV_LOG_ERROR, "Failed to create JxlEncoderOptions\n");
@@ -247,7 +245,7 @@ static int libjxl_encode_frame(AVCodecContext *avctx, 
AVPacket *pkt, const AVFra
 LibJxlEncodeContext *ctx = avctx->priv_data;
 AVFrameSideData *sd;
 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(frame->format);
-JxlBasicInfo info;
+JxlBasicInfo *info = >info;
 JxlColorEncoding jxl_color;
 JxlPixelFormat jxl_fmt;
 int bits_per_sample;
@@ -260,6 +258,9 @@ static int libjxl_encode_frame(AVCodecContext *avctx, 
AVPacket *pkt, const AVFra
 size_t bytes_written = 0;
 uint8_t *next_out = ctx->buffer;
 
+/* reset the encoder every frame for image2 muxer */
+JxlEncoderReset(ctx->encoder);
+
 ret = libjxl_init_jxl_encoder(avctx);
 if (ret) {
 av_log(avctx, AV_LOG_ERROR, "Error frame-initializing JxlEncoder\n");
@@ -267,31 +268,31 @@ static int libjxl_encode_frame(AVCodecContext *avctx, 
AVPacket *pkt, const AVFra
 }
 
 /* populate the basic info settings */
-JxlEncoderInitBasicInfo();
+JxlEncoderInitBasicInfo(info);
 jxl_fmt.num_channels = pix_desc->nb_components;
-info.xsize = frame->width;
-info.ysize = frame->height;
-info.num_extra_channels = (jxl_fmt.num_channels + 1) % 2;
-info.num_color_channels = jxl_fmt.num_channels - info.num_extra_channels;
+info->xsize = frame->width;
+info->ysize = frame->height;
+info->num_extra_channels = (jxl_fmt.num_channels + 1) % 2;
+info->num_color_channels = jxl_fmt.num_channels - info->num_extra_channels;
 bits_per_sample = av_get_bits_per_pixel(pix_desc) / jxl_fmt.num_channels;
-info.bits_per_sample = avctx->bits_per_raw_sample > 0 && !(pix_desc->flags 
& AV_PIX_FMT_FLAG_FLOAT)
+info->bits_per_sample = avctx->bits_per_raw_sample > 0 && 
!(pix_desc->flags & AV_PIX_FMT_FLAG_FLOAT)
? avctx->bits_per_raw_sample : bits_per_sample;
-info.alpha_bits = (info.num_extra_channels > 0) * info.bits_per_sample;
+info->alpha_bits = (info->num_extra_channels > 0) * info->bits_per_sample;
 if (pix_desc->flags & AV_PIX_FMT_FLAG_FLOAT) {
-info.exponent_bits_per_sample = info.bits_per_sample > 16 ? 8 : 5;
-info.alpha_exponent_bits = info.alpha_bits ? 
info.exponent_bits_per_sample : 0;
-jxl_fmt.data_type = info.bits_per_sample > 16 ? JXL_TYPE_FLOAT : 
JXL_TYPE_FLOAT16;
+info->exponent_bits_per_sample = info->bits_per_sample > 16 ? 8 : 5;
+info->alpha_exponent_bits = info->alpha_bits ? 
info->exponent_bits_per_sample : 0;
+jxl_fmt.data_type = info->bits_per_sample > 16 ? JXL_TYPE_FLOAT : 
JXL_TYPE_FLOAT16;
 } else {
-info.exponent_bits_per_sample = 0;
-info.alpha_exponent_bits = 0;
-jxl_fmt.data_type = info.bits_per_sample <= 8 ? JXL_TYPE_UINT8 : 
JXL_TYPE_UINT16;
+info->exponent_bits_per_sample = 0;
+info->alpha_exponent_bits = 0;
+jxl_fmt.data_type = info->bits_per_sample <= 8 ? JXL_TYPE_UINT8 : 
JXL_TYPE_UINT16;
 }
 
 #if JPEGXL_NUMERIC_VERSION >= JPEGXL_COMPUTE_NUMERIC_VERSION(0, 8, 0)
 jxl_bit_depth.bits_per_sample = bits_per_sample;
 jxl_bit_depth.type = JXL_BIT_DEPTH_FROM_PIXEL_FORMAT;
 jxl_bit_depth.exponent_bits_per_sample = pix_desc->flags & 
AV_PIX_FMT_FLAG_FLOAT ?
- info.exponent_bits_per_sample : 0;
+ info->exponent_bits_per_sample : 
0;
 #endif
 
 /* JPEG XL format itself does not support limited range */
@@ -302,9 +303,9 @@ static int libjxl_encode_frame(AVCodecContext *avctx, 
AVPacket *pkt, const AVFra
 av_log(avctx, AV_LOG_WARNING, "Unknown color range, assuming full 
(pc)\n");
 
 /* bitexact lossless requires there to be no XYB transform */
-info.uses_original_profile = ctx->distance == 0.0;
+info->uses_original_profile = ctx->distance == 0.0;
 
-if (JxlEncoderSetBasicInfo(ctx->encoder, ) != JXL_ENC_SUCCESS) {
+if (JxlEncoderSetBasicInfo(ctx->encoder, info) != JXL_ENC_SUCCESS) {
 av_log(avctx, AV_LOG_ERROR, "Failed to set JxlBasicInfo\n");
 return 

[FFmpeg-devel] [PATCH 2/3] avcodec/libjxlenc: Move image initialization code into libjxl_encode_init_image

2023-12-11 Thread Zsolt Vadász via ffmpeg-devel
---
 libavcodec/libjxlenc.c | 69 +++---
 1 file changed, 45 insertions(+), 24 deletions(-)

diff --git a/libavcodec/libjxlenc.c b/libavcodec/libjxlenc.c
index 92a458d51a..6110c42a7c 100644
--- a/libavcodec/libjxlenc.c
+++ b/libavcodec/libjxlenc.c
@@ -50,6 +50,9 @@ typedef struct LibJxlEncodeContext {
 JxlEncoder *encoder;
 JxlEncoderFrameSettings *options;
 JxlBasicInfo info;
+JxlPixelFormat jxl_fmt;
+int animated;
+int first_frame;
 int effort;
 float distance;
 int modular;
@@ -181,6 +184,9 @@ static av_cold int libjxl_encode_init(AVCodecContext *avctx)
 return AVERROR(ENOMEM);
 }
 
+ctx->animated = 0;
+ctx->first_frame = 1;
+
 return 0;
 }
 
@@ -235,28 +241,19 @@ static int libjxl_populate_primaries(void *avctx, 
JxlColorEncoding *jxl_color, e
 return 0;
 }
 
-/**
- * Encode an entire frame. Currently animation, is not supported by
- * this encoder, so this will always reinitialize a new still image
- * and encode a one-frame image (for image2 and image2pipe).
- */
-static int libjxl_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const 
AVFrame *frame, int *got_packet)
+static int libjxl_encode_init_image(AVCodecContext *avctx, const AVFrame 
*frame)
 {
 LibJxlEncodeContext *ctx = avctx->priv_data;
-AVFrameSideData *sd;
-const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(frame->format);
+int ret;
 JxlBasicInfo *info = >info;
+JxlPixelFormat *jxl_fmt = >jxl_fmt;
 JxlColorEncoding jxl_color;
-JxlPixelFormat jxl_fmt;
 int bits_per_sample;
 #if JPEGXL_NUMERIC_VERSION >= JPEGXL_COMPUTE_NUMERIC_VERSION(0, 8, 0)
 JxlBitDepth jxl_bit_depth;
 #endif
-JxlEncoderStatus jret;
-int ret;
-size_t available = ctx->buffer_size;
-size_t bytes_written = 0;
-uint8_t *next_out = ctx->buffer;
+const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(frame->format);
+AVFrameSideData *sd;
 
 /* reset the encoder every frame for image2 muxer */
 JxlEncoderReset(ctx->encoder);
@@ -269,23 +266,23 @@ static int libjxl_encode_frame(AVCodecContext *avctx, 
AVPacket *pkt, const AVFra
 
 /* populate the basic info settings */
 JxlEncoderInitBasicInfo(info);
-jxl_fmt.num_channels = pix_desc->nb_components;
+jxl_fmt->num_channels = pix_desc->nb_components;
 info->xsize = frame->width;
 info->ysize = frame->height;
-info->num_extra_channels = (jxl_fmt.num_channels + 1) % 2;
-info->num_color_channels = jxl_fmt.num_channels - info->num_extra_channels;
-bits_per_sample = av_get_bits_per_pixel(pix_desc) / jxl_fmt.num_channels;
+info->num_extra_channels = (jxl_fmt->num_channels + 1) % 2;
+info->num_color_channels = jxl_fmt->num_channels - 
info->num_extra_channels;
+bits_per_sample = av_get_bits_per_pixel(pix_desc) / jxl_fmt->num_channels;
 info->bits_per_sample = avctx->bits_per_raw_sample > 0 && 
!(pix_desc->flags & AV_PIX_FMT_FLAG_FLOAT)
? avctx->bits_per_raw_sample : bits_per_sample;
 info->alpha_bits = (info->num_extra_channels > 0) * info->bits_per_sample;
 if (pix_desc->flags & AV_PIX_FMT_FLAG_FLOAT) {
 info->exponent_bits_per_sample = info->bits_per_sample > 16 ? 8 : 5;
 info->alpha_exponent_bits = info->alpha_bits ? 
info->exponent_bits_per_sample : 0;
-jxl_fmt.data_type = info->bits_per_sample > 16 ? JXL_TYPE_FLOAT : 
JXL_TYPE_FLOAT16;
+jxl_fmt->data_type = info->bits_per_sample > 16 ? JXL_TYPE_FLOAT : 
JXL_TYPE_FLOAT16;
 } else {
 info->exponent_bits_per_sample = 0;
 info->alpha_exponent_bits = 0;
-jxl_fmt.data_type = info->bits_per_sample <= 8 ? JXL_TYPE_UINT8 : 
JXL_TYPE_UINT16;
+jxl_fmt->data_type = info->bits_per_sample <= 8 ? JXL_TYPE_UINT8 : 
JXL_TYPE_UINT16;
 }
 
 #if JPEGXL_NUMERIC_VERSION >= JPEGXL_COMPUTE_NUMERIC_VERSION(0, 8, 0)
@@ -383,17 +380,41 @@ static int libjxl_encode_frame(AVCodecContext *avctx, 
AVPacket *pkt, const AVFra
 av_log(avctx, AV_LOG_WARNING, "Could not increase codestream 
level\n");
 }
 
-jxl_fmt.endianness = JXL_NATIVE_ENDIAN;
-jxl_fmt.align = frame->linesize[0];
+jxl_fmt->endianness = JXL_NATIVE_ENDIAN;
+jxl_fmt->align = frame->linesize[0];
+
+return 0;
+}
+
+/**
+ * Encode an entire frame. Currently animation, is not supported by
+ * this encoder, so this will always reinitialize a new still image
+ * and encode a one-frame image (for image2 and image2pipe).
+ */
+static int libjxl_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const 
AVFrame *frame, int *got_packet)
+{
+LibJxlEncodeContext *ctx = avctx->priv_data;
+JxlEncoderStatus jret;
+JxlBasicInfo *info = >info;
+JxlPixelFormat *jxl_fmt = >jxl_fmt;
+int ret;
+size_t available = ctx->buffer_size;
+size_t bytes_written = 0;
+uint8_t *next_out = ctx->buffer;
+
+if(!ctx->animated || ctx->first_frame) {
+if((ret = 

[FFmpeg-devel] [PATCH v6 2/2] avformat/oggenc: Add support for embedding cover art

2023-11-19 Thread Zsolt Vadász via ffmpeg-devel
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 +
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;
   

[FFmpeg-devel] [PATCH v6 1/2] avformat/flac_picture: Add ff_flac_write_picture

2023-11-19 Thread Zsolt Vadász via ffmpeg-devel
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 +
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(, type);
+
+bytestream_put_be32(, mimelen);
+bytestream_put_buffer(, mimetype, mimelen);
+
+bytestream_put_be32(, desclen);
+bytestream_put_buffer(, desc, desclen);
+
+bytestream_put_be32(, st->codecpar->width);
+bytestream_put_be32(, st->codecpar->height);
+if ((pixdesc = av_pix_fmt_desc_get(st->codecpar->format)))
+bytestream_put_be32(, av_get_bits_per_pixel(pixdesc));
+else
+bytestream_put_be32(, 0);
+bytestream_put_be32(, 0);
+
+bytestream_put_be32(, pkt->size);
+bytestream_put_buffer(, pkt->data, pkt->size);
+
+encoded_len = AV_BASE64_SIZE(blocklen);
+encoded = av_mallocz(encoded_len);
+av_base64_encode(encoded, encoded_len, 

Re: [FFmpeg-devel] [PATCH v5 1/2] avformat/flac_picture: Add ff_flac_write_picture

2023-08-06 Thread Zsolt Vadász
Hi, it's been a while, can I get a review on this patch set? It would close 
https://trac.ffmpeg.org/ticket/4448.

Thanks in advance!
Zsolt
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v5 2/2] avformat/oggenc: Add support for embedding cover art

2023-03-10 Thread Zsolt Vadász
Fixes #4448. The cover art must have DISPOSITION_ATTACHED_PIC.

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 2e582d0754..ace2ef840b 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)

[FFmpeg-devel] [PATCH v5 1/2] avformat/flac_picture: Add ff_flac_write_picture

2023-03-10 Thread Zsolt Vadász
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(, type);
+
+bytestream_put_be32(, mimelen);
+bytestream_put_buffer(, mimetype, mimelen);
+
+bytestream_put_be32(, desclen);
+bytestream_put_buffer(, desc, desclen);
+
+bytestream_put_be32(, st->codecpar->width);
+bytestream_put_be32(, st->codecpar->height);
+if ((pixdesc = av_pix_fmt_desc_get(st->codecpar->format)))
+bytestream_put_be32(, av_get_bits_per_pixel(pixdesc));
+else
+bytestream_put_be32(, 0);
+bytestream_put_be32(, 0);
+
+bytestream_put_be32(, pkt->size);
+bytestream_put_buffer(, 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(_stream->metadata, "METADATA_BLOCK_PICTURE", 
encoded, 0);
+av_free(encoded);
+av_packet_unref(pkt);
+
+if (ret < 

[FFmpeg-devel] [PATCH v4] avformat: Add support for embedding cover art in Ogg files

2023-03-05 Thread Zsolt Vadász
This version can actually be applied to master. Also added checks to
ensure there won't be an OGGStreamContext for the cover art to suppress
warnings in mpv.

Signed-off-by: Zsolt Vadasz 
---
 libavformat/flac_picture.c | 132 ++
 libavformat/flac_picture.h |   5 +
 libavformat/flacenc.c  |  90 +--
 libavformat/oggenc.c   | 217 ++---
 4 files changed, 319 insertions(+), 125 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(, type);
+
+bytestream_put_be32(, mimelen);
+bytestream_put_buffer(, mimetype, mimelen);
+
+bytestream_put_be32(, desclen);
+bytestream_put_buffer(, desc, desclen);
+
+bytestream_put_be32(, st->codecpar->width);
+bytestream_put_be32(, st->codecpar->height);
+if ((pixdesc = av_pix_fmt_desc_get(st->codecpar->format)))
+bytestream_put_be32(, av_get_bits_per_pixel(pixdesc));
+else
+bytestream_put_be32(, 0);
+bytestream_put_be32(, 0);
+
+bytestream_put_be32(, pkt->size);
+bytestream_put_buffer(, 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 = 

[FFmpeg-devel] Subject: [PATCH v4] avformat: Add support for embedding cover art in Ogg files

2023-03-04 Thread Zsolt Vadász
Signed-off-by: Zsolt Vadasz 
---
 libavformat/flac_picture.c | 132 ++
 libavformat/flac_picture.h |   5 +
 libavformat/flacenc.c  |  90 +--
 libavformat/oggenc.c   | 217 ++---
 4 files changed, 319 insertions(+), 125 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(, type);
+
+bytestream_put_be32(, mimelen);
+bytestream_put_buffer(, mimetype, mimelen);
+
+bytestream_put_be32(, desclen);
+bytestream_put_buffer(, desc, desclen);
+
+bytestream_put_be32(, st->codecpar->width);
+bytestream_put_be32(, st->codecpar->height);
+if ((pixdesc = av_pix_fmt_desc_get(st->codecpar->format)))
+bytestream_put_be32(, av_get_bits_per_pixel(pixdesc));
+else
+bytestream_put_be32(, 0);
+bytestream_put_be32(, 0);
+
+bytestream_put_be32(, pkt->size);
+bytestream_put_buffer(, 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(_stream->metadata, "METADATA_BLOCK_PICTURE", 
encoded, 0);
+av_free(encoded);
+av_packet_unref(pkt);
+
+if (ret < 0)
+return 

[FFmpeg-devel] [PATCH v3] avformat: Add support for embedding cover art in Ogg files

2023-01-17 Thread Zsolt Vadász
Signed-off-by: Zsolt Vadasz 
---
 libavformat/flac_picture.c | 132 +++
 libavformat/flac_picture.h |   5 +
 libavformat/flacenc.c  |  90 +---
 libavformat/oggenc.c   | 207 ++---
 4 files changed, 308 insertions(+), 126 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(, type);
+
+bytestream_put_be32(, mimelen);
+bytestream_put_buffer(, mimetype, mimelen);
+
+bytestream_put_be32(, desclen);
+bytestream_put_buffer(, desc, desclen);
+
+bytestream_put_be32(, st->codecpar->width);
+bytestream_put_be32(, st->codecpar->height);
+if ((pixdesc = av_pix_fmt_desc_get(st->codecpar->format)))
+bytestream_put_be32(, av_get_bits_per_pixel(pixdesc));
+else
+bytestream_put_be32(, 0);
+bytestream_put_be32(, 0);
+
+bytestream_put_be32(, pkt->size);
+bytestream_put_buffer(, 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(_stream->metadata, "METADATA_BLOCK_PICTURE", 
encoded, 0);
+av_free(encoded);
+av_packet_unref(pkt);
+
+if (ret < 0)
+

[FFmpeg-devel] [PATCH v2] avformat: Add support for embedding cover art in Ogg files

2023-01-15 Thread Zsolt Vadász
Signed-off-by: Zsolt Vadasz 
---
 libavformat/flac_picture.c | 132 +++
 libavformat/flac_picture.h |   5 +
 libavformat/flacenc.c  |  90 +---
 libavformat/oggenc.c   | 207 ++---
 4 files changed, 308 insertions(+), 126 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(, type);
+
+bytestream_put_be32(, mimelen);
+bytestream_put_buffer(, mimetype, mimelen);
+
+bytestream_put_be32(, desclen);
+bytestream_put_buffer(, desc, desclen);
+
+bytestream_put_be32(, st->codecpar->width);
+bytestream_put_be32(, st->codecpar->height);
+if ((pixdesc = av_pix_fmt_desc_get(st->codecpar->format)))
+bytestream_put_be32(, av_get_bits_per_pixel(pixdesc));
+else
+bytestream_put_be32(, 0);
+bytestream_put_be32(, 0);
+
+bytestream_put_be32(, pkt->size);
+bytestream_put_buffer(, 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(_stream->metadata, "METADATA_BLOCK_PICTURE", 
encoded, 0);
+av_free(encoded);
+av_packet_unref(pkt);
+
+if (ret < 0)
+

[FFmpeg-devel] [PATCH] avformat/oggparsevorbis: Don't append to title metadata

2023-01-04 Thread Zsolt Vadász
This patch fixes #9642.

Signed-off-by: Zsolt Vadasz 
---
 libavformat/oggparsevorbis.c | 10 +++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/libavformat/oggparsevorbis.c b/libavformat/oggparsevorbis.c
index 061840c2ed..02be4e07b8 100644
--- a/libavformat/oggparsevorbis.c
+++ b/libavformat/oggparsevorbis.c
@@ -134,9 +134,13 @@ static int vorbis_parse_single_comment(AVFormatContext 
*as, AVDictionary **m,
 }
 } else if (!ogm_chapter(as, t, v)) {
 (*updates)++;
-if (av_dict_get(*m, t, NULL, 0))
-av_dict_set(m, t, ";", AV_DICT_APPEND);
-av_dict_set(m, t, v, AV_DICT_APPEND);
+if(!av_strncasecmp("title", t, strlen("title")))
+av_dict_set(m, t, v, 0);
+else {
+if (av_dict_get(*m, t, NULL, 0))
+av_dict_set(m, t, ";", AV_DICT_APPEND);
+av_dict_set(m, t, v, AV_DICT_APPEND);
+}
 }
 end:
 t[tl] = '=';
-- 
2.39.0

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH] avformat: Add support for embedding cover art in Ogg files

2023-01-03 Thread Zsolt Vadász
On Sunday, January 1st, 2023 at 10:41 AM, Jean-Baptiste Kempf 
 wrote:


> On Thu, 29 Dec 2022, at 22:05, Zsolt Vadász wrote:
> 
> > It's done similarly to how the flac muxer does it, so I reused most of
> > the code and adapted it.
> 
> 
> Would a common function make sense here?

Yes, I named it ff_flac_write_picture and put it in libavformat/flac_picture.c, 
here is the revised patch:

Signed-off-by: Zsolt Vadasz 
---
 libavformat/flac_picture.c | 132 +++
 libavformat/flac_picture.h |   5 +
 libavformat/flacenc.c  |  90 +---
 libavformat/oggenc.c   | 207 ++---
 4 files changed, 308 insertions(+), 126 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(, type);
+
+bytestream_put_be32(, mimelen);
+bytestream_put_buffer(, mimetype, mimelen);
+
+bytestream_put_be32(, desclen);
+bytestream_put_buffer(, desc, desclen);
+
+bytestream_put_be32(, st->codecpar->width);
+bytestream_put_be32(, st->codec

[FFmpeg-devel] [PATCH] avformat: Add support for embedding cover art in Ogg files

2022-12-29 Thread Zsolt Vadász
It's done similarly to how the flac muxer does it, so I reused most of the code 
and adapted it.

Signed-off-by: Zsolt Vadasz 
---
 libavformat/oggenc.c | 293 +--
 1 file changed, 254 insertions(+), 39 deletions(-)

diff --git a/libavformat/oggenc.c b/libavformat/oggenc.c
index 5003314adb..bfc51628f2 100644
--- a/libavformat/oggenc.c
+++ b/libavformat/oggenc.c
@@ -23,14 +23,22 @@
 
 #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 "version.h"
@@ -77,6 +85,10 @@ 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 attached_pics;
 } OGGContext;
 
 #define OFFSET(x) offsetof(OGGContext, x)
@@ -468,12 +480,107 @@ static void ogg_write_pages(AVFormatContext *s, int 
flush)
 ogg->page_list = p;
 }
 
-static int ogg_init(AVFormatContext *s)
+static int ogg_attach_pic_to_metadata(AVFormatContext *s, AVPacket *pkt)
+{
+OGGContext *c = s->priv_data;
+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];
+AVStream *audio_stream = s->streams[c->audio_stream_idx];
+unsigned int i, mimelen, desclen, type = 0, blocklen;
+uint8_t *ptr, *metadata_block_picture = NULL;
+int encoded_len, ret;
+char *encoded;
+
+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 (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);
+}
+
+/* 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);
+}
+
+metadata_block_picture = av_mallocz(blocklen);
+ptr = metadata_block_picture;
+bytestream_put_be32(, type);
+
+bytestream_put_be32(, mimelen);
+bytestream_put_buffer(, mimetype, mimelen);
+
+bytestream_put_be32(, desclen);
+bytestream_put_buffer(, desc, desclen);
+
+bytestream_put_be32(, st->codecpar->width);
+bytestream_put_be32(, st->codecpar->height);
+if ((pixdesc = av_pix_fmt_desc_get(st->codecpar->format)))
+bytestream_put_be32(, av_get_bits_per_pixel(pixdesc));
+else
+bytestream_put_be32(, 0);
+bytestream_put_be32(, 0);
+
+bytestream_put_be32(, pkt->size);
+bytestream_put_buffer(, 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(_stream->metadata, "METADATA_BLOCK_PICTURE", 
encoded, 0);
+av_free(encoded);
+av_packet_unref(pkt);
+
+if (ret < 0)
+return ret;
+return 0;
+}
+
+static int ogg_finish_init(AVFormatContext *s)
 {
 OGGContext *ogg = s->priv_data;
 OGGStreamContext *oggstream = NULL;
 int i, j;
 
+ogg->attached_pics = 0;
+
 if (ogg->pref_size)
 av_log(s, AV_LOG_WARNING, "The pagesize option is deprecated\n");
 
@@ -481,29 +588,6 @@ static int ogg_init(AVFormatContext *s)
 AVStream *st = s->streams[i];
 unsigned