@@ -448,12 +448,71 @@ void CodecContext2::open(Dictionary &options, const Codec &codec, OptionalErrorC
448
448
options.assign (prt);
449
449
}
450
450
451
+ namespace {
452
+ // Close code in new way: recreate context with parameters coping
453
+ // Use legacy avcodec_close() for old ffmpeg versions.
454
+ int codec_close (AVCodecContext*& ctx)
455
+ {
456
+ #if API_AVCODEC_CLOSE
457
+ return avcodec_close (ctx);
458
+ #else
459
+ AVCodecContext *ctxNew = nullptr ;
460
+ AVCodecParameters *parTmp = nullptr ;
461
+
462
+ int ret;
463
+
464
+ ctxNew = avcodec_alloc_context3 (ctx->codec );
465
+ if (!ctxNew) {
466
+ ret = AVERROR (ENOMEM);
467
+ goto fail;
468
+ }
469
+
470
+ parTmp = avcodec_parameters_alloc ();
471
+ if (!parTmp) {
472
+ ret = AVERROR (ENOMEM);
473
+ goto fail;
474
+ }
475
+
476
+ ret = avcodec_parameters_from_context (parTmp, ctx);
477
+ if (ret < 0 )
478
+ goto fail;
479
+
480
+ ret = avcodec_parameters_to_context (ctxNew, parTmp);
481
+ if (ret < 0 )
482
+ goto fail;
483
+
484
+ ctxNew->pkt_timebase = ctx->pkt_timebase ;
485
+
486
+
487
+ #if (LIBAVCODEC_VERSION_MAJOR < 61) || (defined(FF_API_TICKS_PER_FRAME) && FF_API_TICKS_PER_FRAME)
488
+ FF_DISABLE_DEPRECATION_WARNINGS
489
+ ctxNew->ticks_per_frame = ctx->ticks_per_frame ;
490
+ FF_ENABLE_DEPRECATION_WARNINGS
491
+ #endif
492
+
493
+ avcodec_free_context (&ctx);
494
+ ctx = ctxNew;
495
+
496
+ ctxNew = nullptr ;
497
+ ret = 0 ;
498
+
499
+ fail:
500
+ avcodec_free_context (&ctxNew);
501
+ avcodec_parameters_free (&parTmp);
502
+
503
+ return ret;
504
+ #endif
505
+ }
506
+ }
507
+
451
508
void CodecContext2::close (OptionalErrorCode ec)
452
509
{
453
510
clear_if (ec);
454
511
if (isOpened ())
455
512
{
456
- avcodec_close (m_raw);
513
+ if (auto sts = codec_close (m_raw); sts) {
514
+ throws_if (ec, sts, ffmpeg_category ());
515
+ }
457
516
return ;
458
517
}
459
518
throws_if (ec, Errors::CodecNotOpened);
0 commit comments