@@ -9,6 +9,7 @@ extern "C" {
9
9
#include < libswscale/swscale.h>
10
10
#include < libavutil/display.h>
11
11
#include < libavutil/imgutils.h>
12
+ #include < inttypes.h>
12
13
13
14
#include " icc_profiles/rec2020_profile.h"
14
15
#include " icc_profiles/rec601_ntsc_profile.h"
@@ -228,34 +229,17 @@ avcodec_decoder avcodec_decoder_create(const opencv_mat buf, const bool hevc_ena
228
229
}
229
230
230
231
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) {
234
234
const AVCodec* dav1d_codec = avcodec_find_decoder_by_name (" libdav1d" );
235
235
if (dav1d_codec) {
236
236
codec = dav1d_codec;
237
237
av_log (NULL , AV_LOG_INFO, " Using libdav1d AV1 decoder\n " );
238
238
} 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 ;
259
243
}
260
244
}
261
245
@@ -264,26 +248,29 @@ avcodec_decoder avcodec_decoder_create(const opencv_mat buf, const bool hevc_ena
264
248
return NULL ;
265
249
}
266
250
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 " );
273
255
avcodec_decoder_release (d);
274
256
return NULL ;
275
257
}
258
+ av_log (NULL , AV_LOG_INFO, " avcodec_decoder_create: Codec context allocated.\n " );
276
259
277
- d->codec = avcodec_alloc_context3 (codec);
278
-
260
+ av_log (NULL , AV_LOG_INFO, " avcodec_decoder_create: Copying codec parameters to context...\n " );
279
261
res = avcodec_parameters_to_context (d->codec , codec_params);
280
262
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);
281
266
avcodec_decoder_release (d);
282
267
return NULL ;
283
268
}
269
+ av_log (NULL , AV_LOG_INFO, " avcodec_decoder_create: Codec parameters copied to context.\n " );
284
270
285
271
// Configure AV1 decoder for software-only decoding
286
272
if (codec->id == AV_CODEC_ID_AV1) {
273
+ av_log (NULL , AV_LOG_INFO, " avcodec_decoder_create: Configuring AV1 software decoder.\n " );
287
274
// Explicitly disable hardware acceleration to prevent format errors
288
275
d->codec ->hw_device_ctx = NULL ;
289
276
d->codec ->hwaccel_context = NULL ;
@@ -292,11 +279,16 @@ avcodec_decoder avcodec_decoder_create(const opencv_mat buf, const bool hevc_ena
292
279
d->codec ->thread_count = 0 ; // Let FFmpeg choose optimal thread count
293
280
}
294
281
282
+ av_log (NULL , AV_LOG_INFO, " avcodec_decoder_create: Opening codec...\n " );
295
283
res = avcodec_open2 (d->codec , codec, NULL );
296
284
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);
297
288
avcodec_decoder_release (d);
298
289
return NULL ;
299
290
}
291
+ av_log (NULL , AV_LOG_INFO, " avcodec_decoder_create: Codec opened successfully.\n " );
300
292
301
293
return d;
302
294
}
@@ -484,6 +476,7 @@ static int avcodec_decoder_copy_frame(const avcodec_decoder d, opencv_mat mat, A
484
476
return -1 ;
485
477
}
486
478
479
+ av_log (NULL , AV_LOG_INFO, " avcodec_decoder_copy_frame: Creating SwsContext...\n " );
487
480
// Create SwsContext for converting the frame format and scaling
488
481
struct SwsContext * sws =
489
482
sws_getContext (frame->width ,
@@ -497,6 +490,7 @@ static int avcodec_decoder_copy_frame(const avcodec_decoder d, opencv_mat mat, A
497
490
NULL ,
498
491
NULL ,
499
492
NULL );
493
+ av_log (NULL , AV_LOG_INFO, " avcodec_decoder_copy_frame: SwsContext created.\n " );
500
494
501
495
// Configure colorspace
502
496
int colorspace;
@@ -519,12 +513,15 @@ static int avcodec_decoder_copy_frame(const avcodec_decoder d, opencv_mat mat, A
519
513
break ;
520
514
}
521
515
const int * inv_table = sws_getCoefficients (colorspace);
516
+ av_log (NULL , AV_LOG_INFO, " avcodec_decoder_copy_frame: Colorspace configured.\n " );
522
517
523
518
// Configure color range
524
519
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 " );
525
521
526
522
// Configure YUV conversion table
527
523
const int * table = sws_getCoefficients (SWS_CS_DEFAULT);
524
+ av_log (NULL , AV_LOG_INFO, " avcodec_decoder_copy_frame: YUV conversion table configured.\n " );
528
525
529
526
sws_setColorspaceDetails (sws, inv_table, srcRange, table, 1 , 0 , 1 << 16 , 1 << 16 );
530
527
@@ -533,11 +530,14 @@ static int avcodec_decoder_copy_frame(const avcodec_decoder d, opencv_mat mat, A
533
530
av_image_fill_linesizes (dstLinesizes, AV_PIX_FMT_BGRA, stepSize / 4 );
534
531
uint8_t * dstData[4 ] = {cvMat->data , NULL , NULL , NULL };
535
532
533
+ av_log (NULL , AV_LOG_INFO, " avcodec_decoder_copy_frame: Scaling and converting frame...\n " );
536
534
// Perform the scaling and format conversion
537
535
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 " );
538
537
539
538
// Free the SwsContext
540
539
sws_freeContext (sws);
540
+ av_log (NULL , AV_LOG_INFO, " avcodec_decoder_copy_frame: SwsContext freed.\n " );
541
541
}
542
542
543
543
return res;
0 commit comments