lavf/dvenc: improve error messaging

Provide useful information about the failure in the error message, do
not let the user guess.
This commit is contained in:
Stefano Sabatini 2024-01-20 16:17:59 +01:00
parent 58a1386eaf
commit bce9234f10
1 changed files with 82 additions and 38 deletions

View File

@ -39,6 +39,7 @@
#include "libavutil/fifo.h" #include "libavutil/fifo.h"
#include "libavutil/mathematics.h" #include "libavutil/mathematics.h"
#include "libavutil/intreadwrite.h" #include "libavutil/intreadwrite.h"
#include "libavutil/pixdesc.h"
#include "libavutil/timecode.h" #include "libavutil/timecode.h"
#define MAX_AUDIO_FRAME_SIZE 192000 // 1 second of 48khz 32-bit audio #define MAX_AUDIO_FRAME_SIZE 192000 // 1 second of 48khz 32-bit audio
@ -308,62 +309,107 @@ static int dv_assemble_frame(AVFormatContext *s,
return 0; return 0;
} }
static DVMuxContext* dv_init_mux(AVFormatContext* s) static int dv_init_mux(AVFormatContext* s)
{ {
DVMuxContext *c = s->priv_data; DVMuxContext *c = s->priv_data;
AVStream *vst = NULL; AVStream *vst = NULL;
int i; int i;
/* we support at most 1 video and 2 audio streams */ if (s->nb_streams > 5) {
if (s->nb_streams > 5) av_log(s, AV_LOG_ERROR,
return NULL; "Invalid number of streams %d, the muxer supports at most 1 video channel and 4 audio channels.\n",
s->nb_streams);
return AVERROR_INVALIDDATA;
}
/* We have to sort out where audio and where video stream is */ /* We have to sort out where audio and where video stream is */
for (i=0; i<s->nb_streams; i++) { for (i=0; i<s->nb_streams; i++) {
AVStream *st = s->streams[i]; AVStream *st = s->streams[i];
switch (st->codecpar->codec_type) { switch (st->codecpar->codec_type) {
case AVMEDIA_TYPE_VIDEO: case AVMEDIA_TYPE_VIDEO:
if (vst) return NULL; if (vst) {
if (st->codecpar->codec_id != AV_CODEC_ID_DVVIDEO) av_log(s, AV_LOG_ERROR,
goto bail_out; "More than one video stream found, only one is accepted.\n");
return AVERROR_INVALIDDATA;
}
if (st->codecpar->codec_id != AV_CODEC_ID_DVVIDEO) {
av_log(s, AV_LOG_ERROR,
"Invalid codec for video stream, only DVVIDEO is supported.\n");
return AVERROR_INVALIDDATA;
}
vst = st; vst = st;
break; break;
case AVMEDIA_TYPE_AUDIO: case AVMEDIA_TYPE_AUDIO:
if (c->n_ast > 1) return NULL; if (c->n_ast > 1) {
av_log(s, AV_LOG_ERROR,
"More than two audio streams found, at most 2 are accepted.\n");
return AVERROR_INVALIDDATA;
}
/* Some checks -- DV format is very picky about its incoming streams */ /* Some checks -- DV format is very picky about its incoming streams */
if(st->codecpar->codec_id != AV_CODEC_ID_PCM_S16LE || if (st->codecpar->codec_id != AV_CODEC_ID_PCM_S16LE) {
st->codecpar->ch_layout.nb_channels != 2) av_log(s, AV_LOG_ERROR,
goto bail_out; "Invalid codec for stream %d, only PCM_S16LE is supported\n.", i);
return AVERROR_INVALIDDATA;
}
if (st->codecpar->ch_layout.nb_channels != 2) {
av_log(s, AV_LOG_ERROR,
"Invalid number of audio channels %d for stream %d, only 2 channels are supported\n.",
st->codecpar->ch_layout.nb_channels, i);
return AVERROR_INVALIDDATA;
}
if (st->codecpar->sample_rate != 48000 && if (st->codecpar->sample_rate != 48000 &&
st->codecpar->sample_rate != 44100 && st->codecpar->sample_rate != 44100 &&
st->codecpar->sample_rate != 32000 ) st->codecpar->sample_rate != 32000) {
goto bail_out; av_log(s, AV_LOG_ERROR,
"Invalid audio sample rate %d for stream %d, only 32000, 44100, and 48000 are supported.\n",
st->codecpar->sample_rate, i);
return AVERROR_INVALIDDATA;
}
c->ast[c->n_ast++] = st; c->ast[c->n_ast++] = st;
break; break;
default: default:
goto bail_out; av_log(s, AV_LOG_ERROR,
"Invalid media type for stream %d, only audio and video are supported.\n", i);
return AVERROR_INVALIDDATA;
} }
} }
if (!vst) if (!vst) {
goto bail_out; av_log(s, AV_LOG_ERROR,
"Missing video stream, must be present\n");
return AVERROR_INVALIDDATA;
}
c->sys = av_dv_codec_profile2(vst->codecpar->width, vst->codecpar->height, c->sys = av_dv_codec_profile2(vst->codecpar->width, vst->codecpar->height,
vst->codecpar->format, vst->time_base); vst->codecpar->format, vst->time_base);
if (!c->sys) if (!c->sys) {
goto bail_out; av_log(s, AV_LOG_ERROR,
"Could not find a valid video profile for size:%dx%d format:%s tb:%d%d\n",
if ((c->sys->time_base.den != 25 && c->sys->time_base.den != 50) || c->sys->time_base.num != 1) { vst->codecpar->width, vst->codecpar->height,
if (c->ast[0] && c->ast[0]->codecpar->sample_rate != 48000) av_get_pix_fmt_name(vst->codecpar->format),
goto bail_out; vst->time_base.num, vst->time_base.den);
if (c->ast[1] && c->ast[1]->codecpar->sample_rate != 48000) return AVERROR_INVALIDDATA;
goto bail_out;
} }
if (((c->n_ast > 1) && (c->sys->n_difchan < 2)) || if ((c->sys->time_base.den != 25 && c->sys->time_base.den != 50) || c->sys->time_base.num != 1) {
((c->n_ast > 2) && (c->sys->n_difchan < 4))) { for (i = 0; i < 2; i++) {
/* only 2 stereo pairs allowed in 50Mbps mode */ if (c->ast[i] && c->ast[i]->codecpar->sample_rate != 48000) {
goto bail_out; av_log(s, AV_LOG_ERROR,
"Invalid sample rate %d for audio stream #%d for this video profile, must be 48000.\n",
c->ast[i]->codecpar->sample_rate, i);
return AVERROR_INVALIDDATA;
}
}
}
for (i = 1; i < 2; i++) {
if (((c->n_ast > i) && (c->sys->n_difchan < (2 * i)))) {
const char *mode = c->n_ast > 1 ? "50Mps" : "25Mps";
av_log(s, AV_LOG_ERROR,
"Invalid number of channels %d, only %d stereo pairs is allowed in %s mode.\n",
c->n_ast, i, mode);
return AVERROR_INVALIDDATA;
}
} }
/* Ok, everything seems to be in working order */ /* Ok, everything seems to be in working order */
@ -376,14 +422,12 @@ static DVMuxContext* dv_init_mux(AVFormatContext* s)
if (!c->ast[i]) if (!c->ast[i])
continue; continue;
c->audio_data[i] = av_fifo_alloc2(100 * MAX_AUDIO_FRAME_SIZE, 1, 0); c->audio_data[i] = av_fifo_alloc2(100 * MAX_AUDIO_FRAME_SIZE, 1, 0);
if (!c->audio_data[i]) if (!c->audio_data[i]) {
goto bail_out; return AVERROR(ENOMEM);
}
} }
return c; return 0;
bail_out:
return NULL;
} }
static int dv_write_header(AVFormatContext *s) static int dv_write_header(AVFormatContext *s)
@ -392,12 +436,12 @@ static int dv_write_header(AVFormatContext *s)
DVMuxContext *dvc = s->priv_data; DVMuxContext *dvc = s->priv_data;
AVDictionaryEntry *tcr = av_dict_get(s->metadata, "timecode", NULL, 0); AVDictionaryEntry *tcr = av_dict_get(s->metadata, "timecode", NULL, 0);
if (!dv_init_mux(s)) { if (dv_init_mux(s) < 0) {
av_log(s, AV_LOG_ERROR, "Can't initialize DV format!\n" av_log(s, AV_LOG_ERROR, "Can't initialize DV format!\n"
"Make sure that you supply exactly two streams:\n" "Make sure that you supply at least two streams:\n"
" video: 25fps or 29.97fps, audio: 2ch/48|44|32kHz/PCM\n" " video: 25fps or 29.97fps, audio: 2ch/48|44.1|32kHz/PCM\n"
" (50Mbps allows an optional second audio stream)\n"); " (50Mbps allows an optional second audio stream)\n");
return -1; return AVERROR_INVALIDDATA;
} }
rate.num = dvc->sys->ltc_divisor; rate.num = dvc->sys->ltc_divisor;
rate.den = 1; rate.den = 1;