[FFmpeg-devel] [PATCH v10] avcodec: add farbfeld encoder, decoder and demuxer

Lynne dev at lynne.ee
Fri Jun 7 21:12:09 EEST 2024


On 07/06/2024 19:47, Marcus B Spencer wrote:
> farbfeld is an uncompressed image format that is a part of suckless
> tools (https://tools.suckless.org).
> 
> Its documentation is available at https://tools.suckless.org/farbfeld.
> 
> Add support for this image format in avcodec and avformat, and update the image2
> format accordingly.
> 
> Signed-off-by: Marcus B Spencer <marcus at marcusspencer.xyz>
> ---
>   Changelog                 |  1 +
>   doc/general_contents.texi |  2 +
>   libavcodec/Makefile       |  2 +
>   libavcodec/allcodecs.c    |  2 +
>   libavcodec/codec_desc.c   |  7 +++
>   libavcodec/codec_id.h     |  1 +
>   libavcodec/farbfelddec.c  | 82 +++++++++++++++++++++++++++++++++
>   libavcodec/farbfeldenc.c  | 96 +++++++++++++++++++++++++++++++++++++++
>   libavcodec/version.h      |  4 +-
>   libavformat/Makefile      |  1 +
>   libavformat/allformats.c  |  1 +
>   libavformat/img2.c        |  1 +
>   libavformat/img2dec.c     | 16 +++++++
>   libavformat/img2enc.c     |  2 +-
>   libavformat/version.h     |  4 +-
>   15 files changed, 217 insertions(+), 5 deletions(-)
>   create mode 100644 libavcodec/farbfelddec.c
>   create mode 100644 libavcodec/farbfeldenc.c
> 
> diff --git a/Changelog b/Changelog
> index 03d6b29ad8..102c718ffc 100644
> --- a/Changelog
> +++ b/Changelog
> @@ -12,6 +12,7 @@ version <next>:
>   - qsv_params option added for QSV encoders
>   - VVC decoder compatible with DVB test content
>   - xHE-AAC decoder
> +- farbfeld encoder, decoder and demuxer
>   
>   
>   version 7.0:
> diff --git a/doc/general_contents.texi b/doc/general_contents.texi
> index e7cf4f8239..fab30610a4 100644
> --- a/doc/general_contents.texi
> +++ b/doc/general_contents.texi
> @@ -853,6 +853,8 @@ following image formats are supported:
>       @tab X PixMap image format
>   @item XWD  @tab X @tab X
>       @tab X Window Dump image format
> + at item FF           @tab X @tab X
> +    @tab farbfeld uncompressed image format
>   @end multitable
>   
>   @code{X} means that the feature in that column (encoding / decoding) is supported.
> diff --git a/libavcodec/Makefile b/libavcodec/Makefile
> index 8ab4398b6c..08a3d39af0 100644
> --- a/libavcodec/Makefile
> +++ b/libavcodec/Makefile
> @@ -356,6 +356,8 @@ OBJS-$(CONFIG_ESCAPE130_DECODER)       += escape130.o
>   OBJS-$(CONFIG_EVRC_DECODER)            += evrcdec.o acelp_vectors.o lsp.o
>   OBJS-$(CONFIG_EXR_DECODER)             += exr.o exrdsp.o half2float.o
>   OBJS-$(CONFIG_EXR_ENCODER)             += exrenc.o float2half.o
> +OBJS-$(CONFIG_FARBFELD_DECODER)        += farbfelddec.o
> +OBJS-$(CONFIG_FARBFELD_ENCODER)        += farbfeldenc.o
>   OBJS-$(CONFIG_FASTAUDIO_DECODER)       += fastaudio.o
>   OBJS-$(CONFIG_FFV1_DECODER)            += ffv1dec.o ffv1.o
>   OBJS-$(CONFIG_FFV1_ENCODER)            += ffv1enc.o ffv1.o
> diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
> index b102a8069e..9f9eda8ec6 100644
> --- a/libavcodec/allcodecs.c
> +++ b/libavcodec/allcodecs.c
> @@ -115,6 +115,8 @@ extern const FFCodec ff_escape124_decoder;
>   extern const FFCodec ff_escape130_decoder;
>   extern const FFCodec ff_exr_encoder;
>   extern const FFCodec ff_exr_decoder;
> +extern const FFCodec ff_farbfeld_decoder;
> +extern const FFCodec ff_farbfeld_encoder;
>   extern const FFCodec ff_ffv1_encoder;
>   extern const FFCodec ff_ffv1_decoder;
>   extern const FFCodec ff_ffvhuff_encoder;
> diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
> index a28ef68061..33dbd2ce94 100644
> --- a/libavcodec/codec_desc.c
> +++ b/libavcodec/codec_desc.c
> @@ -1959,6 +1959,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
>           .long_name = NULL_IF_CONFIG_SMALL("LEAD MCMP"),
>           .props     = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY,
>       },
> +    {
> +        .id        = AV_CODEC_ID_FARBFELD,
> +        .type      = AVMEDIA_TYPE_VIDEO,
> +        .name      = "farbfeld",
> +        .long_name = NULL_IF_CONFIG_SMALL("farbfeld uncompressed image"),
> +        .props     = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS,
> +    },
>   
>       /* various PCM "codecs" */
>       {
> diff --git a/libavcodec/codec_id.h b/libavcodec/codec_id.h
> index 0ab1e34a61..d4b0d23f7e 100644
> --- a/libavcodec/codec_id.h
> +++ b/libavcodec/codec_id.h
> @@ -322,6 +322,7 @@ enum AVCodecID {
>       AV_CODEC_ID_RTV1,
>       AV_CODEC_ID_VMIX,
>       AV_CODEC_ID_LEAD,
> +    AV_CODEC_ID_FARBFELD,
>   
>       /* various PCM "codecs" */
>       AV_CODEC_ID_FIRST_AUDIO = 0x10000,     ///< A dummy id pointing at the start of audio codecs
> diff --git a/libavcodec/farbfelddec.c b/libavcodec/farbfelddec.c
> new file mode 100644
> index 0000000000..b56727225c
> --- /dev/null
> +++ b/libavcodec/farbfelddec.c
> @@ -0,0 +1,82 @@
> +/*
> + * Copyright (c) 2024 Marcus B Spencer <marcus at marcusspencer.xyz>
> + *
> + * Permission is hereby granted, free of charge, to any person obtaining a copy
> + * of this software and associated documentation files (the “Software”), to
> + * deal in the Software without restriction, including without limitation the
> + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
> + * sell copies of the Software, and to permit persons to whom the Software is
> + * furnished to do so, subject to the following conditions:
> + *
> + * The above copyright notice and this permission notice shall be included in
> + * all copies or substantial portions of the Software.
> + *
> + * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
> + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
> + * IN THE SOFTWARE.
> + */
> +
> +#include "avcodec.h"
> +#include "bytestream.h"
> +#include "codec_internal.h"
> +#include "decode.h"
> +#include "libavutil/imgutils.h"
> +#include "thread.h"
> +
> +static int farbfeld_decode_frame(AVCodecContext *ctx, AVFrame *p,
> +                                 int *got_frame, AVPacket *pkt)
> +{
> +    int width, height;
> +    GetByteContext g;
> +    int ret;
> +
> +    bytestream2_init(&g, pkt->data, pkt->size);
> +    bytestream2_skip(&g, 8);
> +
> +    width  = bytestream2_get_be32(&g);
> +    height = bytestream2_get_be32(&g);
> +
> +    if ((ret = ff_set_dimensions(ctx, width, height)) < 0)
> +        return ret;
> +
> +    ctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
> +
> +    if (ctx->skip_frame >= AVDISCARD_ALL)
> +        return pkt->size;
> +
> +    if ((ret = ff_thread_get_buffer(ctx, p, 0)) < 0)
> +        return ret;
> +
> +    if ((ret = av_image_get_buffer_size(ctx->pix_fmt, width, height, 1)) < 0)
> +        return ret;
> +
> +    if (bytestream2_get_bytes_left(&g) < ret)
> +        return AVERROR_INVALIDDATA;
> +
> +    av_image_fill_arrays(
> +        p->data,
> +        p->linesize,
> +        g.buffer,
> +        ctx->pix_fmt,
> +        width,
> +        height,
> +        1
> +    );
> +
> +    *got_frame = 1;
> +
> +    return pkt->size;
> +}
> +
> +const FFCodec ff_farbfeld_decoder = {
> +    .p.name         = "farbfeld",
> +    CODEC_LONG_NAME("farbfeld uncompressed image"),
> +    .p.type         = AVMEDIA_TYPE_VIDEO,
> +    .p.id           = AV_CODEC_ID_FARBFELD,
> +    .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
> +    FF_CODEC_DECODE_CB(farbfeld_decode_frame),
> +};
> diff --git a/libavcodec/farbfeldenc.c b/libavcodec/farbfeldenc.c
> new file mode 100644
> index 0000000000..2801324852
> --- /dev/null
> +++ b/libavcodec/farbfeldenc.c
> @@ -0,0 +1,96 @@
> +/*
> + * Copyright (c) 2024 Marcus B Spencer <marcus at marcusspencer.xyz>
> + *
> + * Permission is hereby granted, free of charge, to any person obtaining a copy
> + * of this software and associated documentation files (the “Software”), to
> + * deal in the Software without restriction, including without limitation the
> + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
> + * sell copies of the Software, and to permit persons to whom the Software is
> + * furnished to do so, subject to the following conditions:
> + *
> + * The above copyright notice and this permission notice shall be included in
> + * all copies or substantial portions of the Software.
> + *
> + * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
> + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
> + * IN THE SOFTWARE.
> + */
> +
> +#include "avcodec.h"
> +#include "bytestream.h"
> +#include "codec_internal.h"
> +#include "encode.h"
> +#include "libavutil/imgutils.h"
> +
> +#define HEADER_SIZE 16
> +#define PLANE_COUNT 4 // required by av_image_copy_to_buffer
> +
> +static int farbfeld_encode_frame(AVCodecContext *ctx, AVPacket *pkt,
> +                                 const AVFrame *p, int *got_packet)
> +{
> +    int raw_img_size = av_image_get_buffer_size(
> +        p->format,
> +        p->width,
> +        p->height,
> +        1
> +    );
> +    const uint8_t *planes[4];
> +    int64_t buf_size;
> +    uint8_t *buf;
> +    int ret;
> +
> +    if (raw_img_size < 0)
> +        return raw_img_size;
> +
> +#if INT_MAX > INT64_MAX - HEADER_SIZE
> +    if (raw_img_size > INT64_MAX - HEADER_SIZE)
> +        return AVERROR(ERANGE);
> +#endif
> +
> +    buf_size = (int64_t)raw_img_size + HEADER_SIZE;
> +
> +    if ((ret = ff_get_encode_buffer(ctx, pkt, buf_size, 0)) < 0)
> +        return ret;
> +
> +    buf = pkt->data;
> +
> +    bytestream_put_buffer(&buf, "farbfeld", 8);
> +
> +    bytestream_put_be32(&buf, ctx->width);
> +    bytestream_put_be32(&buf, ctx->height);
> +
> +    for (int i = 0; i < PLANE_COUNT; ++i)
> +        planes[i] = p->data[i];
> +
> +    av_image_copy_to_buffer(
> +        buf,
> +        raw_img_size,
> +        planes,
> +        p->linesize,
> +        p->format,
> +        p->width,
> +        p->height,
> +        1
> +    );
> +
> +    *got_packet = 1;
> +
> +    return 0;
> +}
> +
> +const FFCodec ff_farbfeld_encoder = {
> +    .p.name         = "farbfeld",
> +    CODEC_LONG_NAME("farbfeld uncompressed image"),
> +    .p.type         = AVMEDIA_TYPE_VIDEO,
> +    .p.id           = AV_CODEC_ID_FARBFELD,
> +    .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
> +    FF_CODEC_ENCODE_CB(farbfeld_encode_frame),
> +    .p.pix_fmts     = (const enum AVPixelFormat[]){
> +        AV_PIX_FMT_RGBA64BE,
> +        AV_PIX_FMT_NONE
> +    },
> +};
> diff --git a/libavcodec/version.h b/libavcodec/version.h
> index 39dbec0208..7acb261bb3 100644
> --- a/libavcodec/version.h
> +++ b/libavcodec/version.h
> @@ -29,8 +29,8 @@
>   
>   #include "version_major.h"
>   
> -#define LIBAVCODEC_VERSION_MINOR   6
> -#define LIBAVCODEC_VERSION_MICRO 101
> +#define LIBAVCODEC_VERSION_MINOR   7
> +#define LIBAVCODEC_VERSION_MICRO 100
>   
>   #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
>                                                  LIBAVCODEC_VERSION_MINOR, \
> diff --git a/libavformat/Makefile b/libavformat/Makefile
> index 1c4d9deccd..588a097149 100644
> --- a/libavformat/Makefile
> +++ b/libavformat/Makefile
> @@ -284,6 +284,7 @@ OBJS-$(CONFIG_IMAGE_CRI_PIPE_DEMUXER)     += img2dec.o img2.o
>   OBJS-$(CONFIG_IMAGE_DDS_PIPE_DEMUXER)     += img2dec.o img2.o
>   OBJS-$(CONFIG_IMAGE_DPX_PIPE_DEMUXER)     += img2dec.o img2.o
>   OBJS-$(CONFIG_IMAGE_EXR_PIPE_DEMUXER)     += img2dec.o img2.o
> +OBJS-$(CONFIG_IMAGE_FARBFELD_PIPE_DEMUXER)+= img2dec.o img2.o
>   OBJS-$(CONFIG_IMAGE_GEM_PIPE_DEMUXER)     += img2dec.o img2.o
>   OBJS-$(CONFIG_IMAGE_GIF_PIPE_DEMUXER)     += img2dec.o img2.o
>   OBJS-$(CONFIG_IMAGE_HDR_PIPE_DEMUXER)     += img2dec.o img2.o
> diff --git a/libavformat/allformats.c b/libavformat/allformats.c
> index 305fa46532..6900ee8199 100644
> --- a/libavformat/allformats.c
> +++ b/libavformat/allformats.c
> @@ -537,6 +537,7 @@ extern const FFInputFormat  ff_image_cri_pipe_demuxer;
>   extern const FFInputFormat  ff_image_dds_pipe_demuxer;
>   extern const FFInputFormat  ff_image_dpx_pipe_demuxer;
>   extern const FFInputFormat  ff_image_exr_pipe_demuxer;
> +extern const FFInputFormat  ff_image_farbfeld_pipe_demuxer;
>   extern const FFInputFormat  ff_image_gem_pipe_demuxer;
>   extern const FFInputFormat  ff_image_gif_pipe_demuxer;
>   extern const FFInputFormat  ff_image_hdr_pipe_demuxer;
> diff --git a/libavformat/img2.c b/libavformat/img2.c
> index 9981867f82..77edc7ff9b 100644
> --- a/libavformat/img2.c
> +++ b/libavformat/img2.c
> @@ -95,6 +95,7 @@
>       TAG(QOI,             qoi      ) \
>       TAG(RADIANCE_HDR,    hdr      ) \
>       TAG(WBMP,            wbmp     ) \
> +    TAG(FARBFELD,        ff       ) \
>       TAG(NONE,                     )
>   
>   #define LENGTH_CHECK(CODECID, STR) \
> diff --git a/libavformat/img2dec.c b/libavformat/img2dec.c
> index a40675d434..70091c70f2 100644
> --- a/libavformat/img2dec.c
> +++ b/libavformat/img2dec.c
> @@ -1205,6 +1205,21 @@ static int vbn_probe(const AVProbeData *p)
>       return 0;
>   }
>   
> +static int farbfeld_probe(const AVProbeData *p)
> +{
> +    if (p->buf_size < 16)
> +        return 0;
> +
> +    if (memcmp(p->buf, "farbfeld", 8))
> +        return 0;
> +
> +    // check if width or height is zero
> +    if (AV_RB32(p->buf + 8) == 0 || AV_RB32(p->buf + 12) == 0)
> +        return 0;
> +
> +    return AVPROBE_SCORE_MAX - 1;
> +}
> +
>   #define IMAGEAUTO_DEMUXER_0(imgname, codecid)
>   #define IMAGEAUTO_DEMUXER_1(imgname, codecid)\
>   const FFInputFormat ff_image_ ## imgname ## _pipe_demuxer = {\
> @@ -1234,6 +1249,7 @@ IMAGEAUTO_DEMUXER(cri,       CRI)
>   IMAGEAUTO_DEMUXER(dds,       DDS)
>   IMAGEAUTO_DEMUXER(dpx,       DPX)
>   IMAGEAUTO_DEMUXER(exr,       EXR)
> +IMAGEAUTO_DEMUXER(farbfeld,  FARBFELD)
>   IMAGEAUTO_DEMUXER(gem,       GEM)
>   IMAGEAUTO_DEMUXER(gif,       GIF)
>   IMAGEAUTO_DEMUXER_EXT(hdr,   RADIANCE_HDR, HDR)
> diff --git a/libavformat/img2enc.c b/libavformat/img2enc.c
> index 526a11e5ee..13355a6fad 100644
> --- a/libavformat/img2enc.c
> +++ b/libavformat/img2enc.c
> @@ -276,7 +276,7 @@ const FFOutputFormat ff_image2_muxer = {
>       .p.long_name    = NULL_IF_CONFIG_SMALL("image2 sequence"),
>       .p.extensions   = "bmp,dpx,exr,jls,jpeg,jpg,jxl,ljpg,pam,pbm,pcx,pfm,pgm,pgmyuv,phm,"
>                         "png,ppm,sgi,tga,tif,tiff,jp2,j2c,j2k,xwd,sun,ras,rs,im1,im8,"
> -                      "im24,sunras,vbn,xbm,xface,pix,y,avif,qoi,hdr,wbmp",
> +                      "im24,sunras,vbn,xbm,xface,pix,y,avif,qoi,hdr,wbmp,ff",
>       .priv_data_size = sizeof(VideoMuxData),
>       .p.video_codec  = AV_CODEC_ID_MJPEG,
>       .write_header   = write_header,
> diff --git a/libavformat/version.h b/libavformat/version.h
> index 4687cd857c..af7d0a1024 100644
> --- a/libavformat/version.h
> +++ b/libavformat/version.h
> @@ -31,8 +31,8 @@
>   
>   #include "version_major.h"
>   
> -#define LIBAVFORMAT_VERSION_MINOR   3
> -#define LIBAVFORMAT_VERSION_MICRO 104
> +#define LIBAVFORMAT_VERSION_MINOR   4
> +#define LIBAVFORMAT_VERSION_MICRO 100
>   
>   #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
>                                                  LIBAVFORMAT_VERSION_MINOR, \

Since the "codec" is essentially just raw data, you should just make the 
demuxer output native RGBA64, and the muxer accept RGBA64. There's no 
reason to have this as a codec, since the header field is completely 
redundant with the fields an AVFrame contains.

Plus, you avoid all copies entirely in this way.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_0xA2FEA5F03F034464.asc
Type: application/pgp-keys
Size: 624 bytes
Desc: OpenPGP public key
URL: <https://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20240607/ebb9959c/attachment.key>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_signature.asc
Type: application/pgp-signature
Size: 236 bytes
Desc: OpenPGP digital signature
URL: <https://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20240607/ebb9959c/attachment.sig>


More information about the ffmpeg-devel mailing list