Skip to content

Commit e58c0bf

Browse files
committed
wip
1 parent fcd6dd8 commit e58c0bf

File tree

3 files changed

+41
-35
lines changed

3 files changed

+41
-35
lines changed

avcodec.cpp

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ extern "C" {
99
#include <libswscale/swscale.h>
1010
#include <libavutil/display.h>
1111
#include <libavutil/imgutils.h>
12+
#include <inttypes.h>
1213

1314
#include "icc_profiles/rec2020_profile.h"
1415
#include "icc_profiles/rec601_ntsc_profile.h"
@@ -228,34 +229,17 @@ avcodec_decoder avcodec_decoder_create(const opencv_mat buf, const bool hevc_ena
228229
}
229230

230231
const AVCodec* codec = avcodec_find_decoder(codec_params->codec_id);
231-
232-
// For AV1, prefer libdav1d decoder for better performance and stability
233-
if (codec_params->codec_id == AV_CODEC_ID_AV1) {
232+
233+
if (codec && codec->id == AV_CODEC_ID_AV1) {
234234
const AVCodec* dav1d_codec = avcodec_find_decoder_by_name("libdav1d");
235235
if (dav1d_codec) {
236236
codec = dav1d_codec;
237237
av_log(NULL, AV_LOG_INFO, "Using libdav1d AV1 decoder\n");
238238
} else {
239-
// Fallback to built-in AV1 decoder if libdav1d not available
240-
const AVCodec* builtin_codec = avcodec_find_decoder_by_name("av1");
241-
if (builtin_codec) {
242-
codec = builtin_codec;
243-
av_log(NULL, AV_LOG_INFO, "Using built-in av1 decoder\n");
244-
} else {
245-
const AVCodec* libaom_codec = avcodec_find_decoder_by_name("libaom-av1");
246-
if (libaom_codec) {
247-
codec = libaom_codec;
248-
av_log(NULL, AV_LOG_INFO, "Using libaom-av1 decoder\n");
249-
} else {
250-
// If no named decoder found, try the direct codec lookup
251-
codec = avcodec_find_decoder(AV_CODEC_ID_AV1);
252-
if (codec) {
253-
av_log(NULL, AV_LOG_INFO, "Using default AV1 decoder: %s\n", codec->name);
254-
} else {
255-
av_log(NULL, AV_LOG_ERROR, "No AV1 decoder found\n");
256-
}
257-
}
258-
}
239+
// If libdav1d not available, disable AV1 to prevent segfaults with the native decoder
240+
av_log(NULL, AV_LOG_ERROR, "libdav1d AV1 decoder not found - AV1 decoding disabled to prevent crashes\n");
241+
avcodec_decoder_release(d);
242+
return NULL;
259243
}
260244
}
261245

@@ -264,26 +248,29 @@ avcodec_decoder avcodec_decoder_create(const opencv_mat buf, const bool hevc_ena
264248
return NULL;
265249
}
266250

267-
if (codec->id == AV_CODEC_ID_HEVC && !hevc_enabled) {
268-
avcodec_decoder_release(d);
269-
return NULL;
270-
}
271-
272-
if (codec->id == AV_CODEC_ID_AV1 && !av1_enabled) {
251+
av_log(NULL, AV_LOG_INFO, "avcodec_decoder_create: Allocating codec context...\n");
252+
d->codec = avcodec_alloc_context3(codec);
253+
if (!d->codec) {
254+
av_log(NULL, AV_LOG_ERROR, "avcodec_decoder_create: Failed to allocate codec context.\n");
273255
avcodec_decoder_release(d);
274256
return NULL;
275257
}
258+
av_log(NULL, AV_LOG_INFO, "avcodec_decoder_create: Codec context allocated.\n");
276259

277-
d->codec = avcodec_alloc_context3(codec);
278-
260+
av_log(NULL, AV_LOG_INFO, "avcodec_decoder_create: Copying codec parameters to context...\n");
279261
res = avcodec_parameters_to_context(d->codec, codec_params);
280262
if (res < 0) {
263+
char err_buf[AV_ERROR_MAX_STRING_SIZE];
264+
av_strerror(res, err_buf, sizeof(err_buf));
265+
av_log(NULL, AV_LOG_ERROR, "avcodec_decoder_create: avcodec_parameters_to_context failed: %s\n", err_buf);
281266
avcodec_decoder_release(d);
282267
return NULL;
283268
}
269+
av_log(NULL, AV_LOG_INFO, "avcodec_decoder_create: Codec parameters copied to context.\n");
284270

285271
// Configure AV1 decoder for software-only decoding
286272
if (codec->id == AV_CODEC_ID_AV1) {
273+
av_log(NULL, AV_LOG_INFO, "avcodec_decoder_create: Configuring AV1 software decoder.\n");
287274
// Explicitly disable hardware acceleration to prevent format errors
288275
d->codec->hw_device_ctx = NULL;
289276
d->codec->hwaccel_context = NULL;
@@ -292,11 +279,16 @@ avcodec_decoder avcodec_decoder_create(const opencv_mat buf, const bool hevc_ena
292279
d->codec->thread_count = 0; // Let FFmpeg choose optimal thread count
293280
}
294281

282+
av_log(NULL, AV_LOG_INFO, "avcodec_decoder_create: Opening codec...\n");
295283
res = avcodec_open2(d->codec, codec, NULL);
296284
if (res < 0) {
285+
char err_buf[AV_ERROR_MAX_STRING_SIZE];
286+
av_strerror(res, err_buf, sizeof(err_buf));
287+
av_log(NULL, AV_LOG_ERROR, "avcodec_decoder_create: avcodec_open2 failed: %s\n", err_buf);
297288
avcodec_decoder_release(d);
298289
return NULL;
299290
}
291+
av_log(NULL, AV_LOG_INFO, "avcodec_decoder_create: Codec opened successfully.\n");
300292

301293
return d;
302294
}
@@ -484,6 +476,7 @@ static int avcodec_decoder_copy_frame(const avcodec_decoder d, opencv_mat mat, A
484476
return -1;
485477
}
486478

479+
av_log(NULL, AV_LOG_INFO, "avcodec_decoder_copy_frame: Creating SwsContext...\n");
487480
// Create SwsContext for converting the frame format and scaling
488481
struct SwsContext* sws =
489482
sws_getContext(frame->width,
@@ -497,6 +490,7 @@ static int avcodec_decoder_copy_frame(const avcodec_decoder d, opencv_mat mat, A
497490
NULL,
498491
NULL,
499492
NULL);
493+
av_log(NULL, AV_LOG_INFO, "avcodec_decoder_copy_frame: SwsContext created.\n");
500494

501495
// Configure colorspace
502496
int colorspace;
@@ -519,12 +513,15 @@ static int avcodec_decoder_copy_frame(const avcodec_decoder d, opencv_mat mat, A
519513
break;
520514
}
521515
const int* inv_table = sws_getCoefficients(colorspace);
516+
av_log(NULL, AV_LOG_INFO, "avcodec_decoder_copy_frame: Colorspace configured.\n");
522517

523518
// Configure color range
524519
int srcRange = frame->color_range == AVCOL_RANGE_JPEG ? 1 : 0;
520+
av_log(NULL, AV_LOG_INFO, "avcodec_decoder_copy_frame: Color range configured.\n");
525521

526522
// Configure YUV conversion table
527523
const int* table = sws_getCoefficients(SWS_CS_DEFAULT);
524+
av_log(NULL, AV_LOG_INFO, "avcodec_decoder_copy_frame: YUV conversion table configured.\n");
528525

529526
sws_setColorspaceDetails(sws, inv_table, srcRange, table, 1, 0, 1 << 16, 1 << 16);
530527

@@ -533,11 +530,14 @@ static int avcodec_decoder_copy_frame(const avcodec_decoder d, opencv_mat mat, A
533530
av_image_fill_linesizes(dstLinesizes, AV_PIX_FMT_BGRA, stepSize / 4);
534531
uint8_t* dstData[4] = {cvMat->data, NULL, NULL, NULL};
535532

533+
av_log(NULL, AV_LOG_INFO, "avcodec_decoder_copy_frame: Scaling and converting frame...\n");
536534
// Perform the scaling and format conversion
537535
sws_scale(sws, frame->data, frame->linesize, 0, frame->height, dstData, dstLinesizes);
536+
av_log(NULL, AV_LOG_INFO, "avcodec_decoder_copy_frame: Frame scaled and converted.\n");
538537

539538
// Free the SwsContext
540539
sws_freeContext(sws);
540+
av_log(NULL, AV_LOG_INFO, "avcodec_decoder_copy_frame: SwsContext freed.\n");
541541
}
542542

543543
return res;

deps/build-deps-linux.sh

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,10 @@ echo '\n--------------------'
387387
echo 'Building ffmpeg'
388388
echo '--------------------\n'
389389
mkdir -p $BASEDIR/ffmpeg
390-
tar -xjf $SRCDIR/ffmpeg-5.1.1.tar.bz2 -C $BASEDIR/ffmpeg --strip-components 1
390+
tar -xJf $SRCDIR/ffmpeg-7.0.2.orig.tar.xz -C $BASEDIR/ffmpeg --strip-components 1
391391
mkdir -p $BUILDDIR/ffmpeg
392392
cd $BUILDDIR/ffmpeg
393-
export PKG_CONFIG_PATH="$PREFIX/lib/pkgconfig:$PKG_CONFIG_PATH"
393+
PKG_CONFIG_PATH="$PREFIX/lib/pkgconfig:$PKG_CONFIG_PATH"
394394
$BASEDIR/ffmpeg/configure $FFMPEG_CROSS_COMPILE_FLAGS \
395395
--prefix=$PREFIX \
396396
--disable-doc \
@@ -419,7 +419,10 @@ $BASEDIR/ffmpeg/configure $FFMPEG_CROSS_COMPILE_FLAGS \
419419
--disable-cuda \
420420
--disable-cuvid \
421421
--disable-nvenc \
422-
--disable-xlib
422+
--disable-xlib \
423+
--extra-cflags="-I$PREFIX/include" \
424+
--extra-ldflags="-L$PREFIX/lib" \
425+
--pkg-config-flags="--static"
423426
make
424427
make install
425428
verify_arch "$PREFIX/lib/libavcodec.a"

deps/build-deps-osx.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ mkdir -p $BASEDIR/ffmpeg
310310
tar -xJf $SRCDIR/ffmpeg-7.0.2.orig.tar.xz -C $BASEDIR/ffmpeg --strip-components 1
311311
mkdir -p $BUILDDIR/ffmpeg
312312
cd $BUILDDIR/ffmpeg
313-
export PKG_CONFIG_PATH="$PREFIX/lib/pkgconfig:$PKG_CONFIG_PATH"
313+
PKG_CONFIG_PATH="$PREFIX/lib/pkgconfig:$PKG_CONFIG_PATH"
314314
$BASEDIR/ffmpeg/configure \
315315
--prefix=$PREFIX \
316316
--disable-doc \
@@ -336,6 +336,9 @@ $BASEDIR/ffmpeg/configure \
336336
--enable-libaom \
337337
--enable-libdav1d \
338338
--disable-iconv \
339+
--extra-cflags="-I$PREFIX/include" \
340+
--extra-ldflags="-L$PREFIX/lib" \
341+
--pkg-config-flags="--static" \
339342
$FFMPEG_ARCH_FLAGS
340343
make
341344
make install

0 commit comments

Comments
 (0)