[Libav-user] zscale: code 3074: no path between colorspaces
Gianluca Cannata
gcannata23 at gmail.com
Fri Jun 28 10:08:44 EEST 2024
I have a series of JPEG files I would like to downsample and downresize
with zscale filter but It always gives me the error code in the subject of
this email.
This is the output from ffprobe:
ffprobe -v info -i /home/gianluca/Pictures/DSCF7253.JPG
ffprobe version N-116035-g6aeb084c39 Copyright (c) 2007-2024 the FFmpeg
developers
built with gcc 11 (Ubuntu 11.4.0-1ubuntu1~22.04)
configuration: --prefix=/usr --disable-static --enable-shared
--enable-gpl --enable-version3 --disable-programs --disable-doc
--disable-avdevice --disable-swresample --enable-swscale --disable-postproc
--disable-network --disable-everything --enable-protocol='file, subfile,
pipe' --enable-demuxer='image2, image2pipe, image_jpeg_pipe, mjpeg'
--enable-muxer=mjpeg --enable-decoder='mjpeg, mjpegb'
--enable-encoder=mjpeg --enable-parser=mjpeg --enable-filter='format, null,
copy, buffer, buffersink, scale, libplacebo, zscale' --enable-libplacebo
--enable-libzimg --enable-vulkan --enable-ffprobe --enable-ffmpeg
libavutil 59. 26.100 / 59. 26.100
libavcodec 61. 8.100 / 61. 8.100
libavformat 61. 4.100 / 61. 4.100
libavfilter 10. 2.102 / 10. 2.102
libswscale 8. 2.100 / 8. 2.100
Input #0, image2, from '/home/gianluca/Pictures/DSCF7253.JPG':
Duration: 00:00:00.04, start: 0.000000, bitrate: 1274307 kb/s
Stream #0:0: Video: mjpeg (Baseline), yuvj422p(pc,
bt470bg/unknown/unknown), 4896x3264, 25 fps, 25 tbr, 25 tbn
Then I try with this code to downspample and downresize the image:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <libgen.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavfilter/avfilter.h>
#include <libavfilter/buffersrc.h>
#include <libavfilter/buffersink.h>
#include <libavutil/log.h>
int main(int argc, char **argv)
{
if (argc != 2) {
fprintf(stderr, "usage: %s <filename>\n", argv[0]);
exit(EXIT_FAILURE);
}
const char *filename = argv[1];
const char *filename2 = basename(filename);
av_log_set_level(AV_LOG_TRACE);
AVFormatContext *fmt_ctx = avformat_alloc_context();
avformat_open_input(&fmt_ctx, filename, NULL, NULL);
AVCodecContext *dec_ctx;
AVCodec *dec;
av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0);
dec_ctx = avcodec_alloc_context3(dec);
avcodec_open2(dec_ctx, dec, NULL);
AVFrame *frame = av_frame_alloc();
AVPacket *packet = av_packet_alloc();
av_read_frame(fmt_ctx, packet);
avcodec_send_packet(dec_ctx, packet);
avcodec_receive_frame(dec_ctx, frame);
av_packet_unref(packet);
av_log(NULL, AV_LOG_TRACE, "filename: %s, w: %d, h: %d, fmt: %d\n",
filename, frame->width, frame->height, frame->format);
AVFilterGraph *filter_graph = avfilter_graph_alloc();
char buffersrc_args[512];
snprintf(buffersrc_args, sizeof(buffersrc_args),
"video_size=%dx%d:pix_fmt=%d:time_base=1/25", frame->width, frame->height,
frame->format);
AVFilterContext *buffersrc_ctx;
avfilter_graph_create_filter(&buffersrc_ctx,
avfilter_get_by_name("buffer"), NULL, buffersrc_args, NULL, filter_graph);
char zscale_args[512];
snprintf(zscale_args, sizeof(zscale_args),
"w=%d:h=%d:f=bicubic:r=full:p=709:t=iec61966-2-1:m=470bg", (frame->width /
2), (frame->height / 2));
AVFilterContext *zscale_ctx = NULL;
const AVFilter *zscale_filter = avfilter_get_by_name("zscale");
avfilter_graph_create_filter(&zscale_ctx, zscale_filter, NULL,
zscale_args, NULL, filter_graph);
AVFilterContext *buffersink_ctx;
avfilter_graph_create_filter(&buffersink_ctx,
avfilter_get_by_name("buffersink"), NULL, NULL, NULL, filter_graph);
avfilter_link(buffersrc_ctx, 0, zscale_ctx, 0);
avfilter_link(zscale_ctx, 0, buffersink_ctx, 0);
avfilter_graph_config(filter_graph, NULL);
AVFrame *filtered_frame = av_frame_alloc();
av_buffersrc_add_frame(buffersrc_ctx, frame);
while (1) {
int ret = av_buffersink_get_frame(buffersink_ctx, filtered_frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
break;
if (ret < 0) {
fprintf(stderr, "av_buffersink_get_frame(): %s\n",
av_err2str(ret));
exit(EXIT_FAILURE);
}
}
av_log(NULL, AV_LOG_TRACE, "filename: %s, w: %d, h: %d, fmt: %d\n",
filename2, filtered_frame->width, filtered_frame->height,
filtered_frame->format);
AVCodecContext *enc_ctx;
AVCodec *enc;
AVPacket *enc_packet = av_packet_alloc();
enc = avcodec_find_encoder_by_name("mjpeg");
enc_ctx = avcodec_alloc_context3(enc);
enc_ctx->width = filtered_frame->width;
enc_ctx->height = filtered_frame->height;
enc_ctx->bit_rate = dec_ctx->bit_rate * 1024;
enc_ctx->time_base = (AVRational) {1, 25};
enc_ctx->framerate = (AVRational) {25, 1};
enc_ctx->pix_fmt = filtered_frame->format;
enc_ctx->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
enc_ctx->compression_level = 0;
enc_ctx->color_range = AVCOL_RANGE_JPEG;
avcodec_open2(enc_ctx, enc, NULL);
avcodec_send_frame(enc_ctx, filtered_frame);
avcodec_receive_packet(enc_ctx, enc_packet);
FILE *dst_file = fopen(filename2, "wb");
fwrite(enc_packet->data, 1, enc_packet->size, dst_file);
fclose(dst_file);
av_packet_unref(enc_packet);
av_frame_free(&frame);
av_frame_free(&filtered_frame);
avfilter_graph_free(&filter_graph);
avformat_close_input(&fmt_ctx);
avformat_free_context(fmt_ctx);
exit(EXIT_SUCCESS);
}
What am I doing wrong ?
Sincerely
Gianluca
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20240628/94e6870d/attachment.htm>
More information about the Libav-user
mailing list