49
49
#include "internal.h"
50
50
#include "thread.h"
51
51
52
- #if CONFIG_LCMS2
53
- # include "fflcms2.h"
54
- #endif
55
-
56
52
static int apply_param_change (AVCodecContext * avctx , const AVPacket * avpkt )
57
53
{
58
54
int ret ;
@@ -508,6 +504,54 @@ FF_ENABLE_DEPRECATION_WARNINGS
508
504
return ret < 0 ? ret : 0 ;
509
505
}
510
506
507
+ #if CONFIG_LCMS2
508
+ static int detect_colorspace (AVCodecContext * avctx , AVFrame * frame )
509
+ {
510
+ AVCodecInternal * avci = avctx -> internal ;
511
+ enum AVColorTransferCharacteristic trc ;
512
+ AVColorPrimariesDesc coeffs ;
513
+ enum AVColorPrimaries prim ;
514
+ cmsHPROFILE profile ;
515
+ AVFrameSideData * sd ;
516
+ int ret ;
517
+ if (!(avctx -> flags2 & AV_CODEC_FLAG2_ICC_PROFILES ))
518
+ return 0 ;
519
+
520
+ sd = av_frame_get_side_data (frame , AV_FRAME_DATA_ICC_PROFILE );
521
+ if (!sd || !sd -> size )
522
+ return 0 ;
523
+
524
+ if (!avci -> icc .avctx ) {
525
+ ret = ff_icc_context_init (& avci -> icc , avctx );
526
+ if (ret < 0 )
527
+ return ret ;
528
+ }
529
+
530
+ profile = cmsOpenProfileFromMemTHR (avci -> icc .ctx , sd -> data , sd -> size );
531
+ if (!profile )
532
+ return AVERROR_INVALIDDATA ;
533
+
534
+ ret = ff_icc_profile_read_primaries (& avci -> icc , profile , & coeffs );
535
+ if (!ret )
536
+ ret = ff_icc_profile_detect_transfer (& avci -> icc , profile , & trc );
537
+ cmsCloseProfile (profile );
538
+ if (ret < 0 )
539
+ return ret ;
540
+
541
+ prim = av_csp_primaries_id_from_desc (& coeffs );
542
+ if (prim != AVCOL_PRI_UNSPECIFIED )
543
+ frame -> color_primaries = prim ;
544
+ if (trc != AVCOL_TRC_UNSPECIFIED )
545
+ frame -> color_trc = trc ;
546
+ return 0 ;
547
+ }
548
+ #else /* !CONFIG_LCMS2 */
549
+ static int detect_colorspace (av_unused AVCodecContext * c , av_unused AVFrame * f )
550
+ {
551
+ return 0 ;
552
+ }
553
+ #endif
554
+
511
555
static int decode_simple_receive_frame (AVCodecContext * avctx , AVFrame * frame )
512
556
{
513
557
int ret ;
@@ -528,7 +572,7 @@ static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
528
572
{
529
573
AVCodecInternal * avci = avctx -> internal ;
530
574
const FFCodec * const codec = ffcodec (avctx -> codec );
531
- int ret ;
575
+ int ret , ok ;
532
576
533
577
av_assert0 (!frame -> buf [0 ]);
534
578
@@ -542,6 +586,13 @@ static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
542
586
if (ret == AVERROR_EOF )
543
587
avci -> draining_done = 1 ;
544
588
589
+ /* preserve ret */
590
+ ok = detect_colorspace (avctx , frame );
591
+ if (ok < 0 ) {
592
+ av_frame_unref (frame );
593
+ return ok ;
594
+ }
595
+
545
596
if (!(codec -> caps_internal & FF_CODEC_CAP_SETS_FRAME_PROPS ) &&
546
597
IS_EMPTY (avci -> last_pkt_props )) {
547
598
// May fail if the FIFO is empty.
0 commit comments