@@ -406,6 +406,29 @@ inline int _opencv_ffmpeg_av_image_get_buffer_size(enum AVPixelFormat pix_fmt, i
406
406
#endif
407
407
};
408
408
409
+ static AVRational _opencv_ffmpeg_get_sample_aspect_ratio (AVStream *stream)
410
+ {
411
+ #if LIBAVUTIL_VERSION_MICRO >= 100 && LIBAVUTIL_BUILD >= CALC_FFMPEG_VERSION(54, 5, 100)
412
+ return av_guess_sample_aspect_ratio (NULL , stream, NULL );
413
+ #else
414
+ AVRational undef = {0 , 1 };
415
+
416
+ // stream
417
+ AVRational ratio = stream ? stream->sample_aspect_ratio : undef;
418
+ av_reduce (&ratio.num , &ratio.den , ratio.num , ratio.den , INT_MAX);
419
+ if (ratio.num > 0 && ratio.den > 0 )
420
+ return ratio;
421
+
422
+ // codec
423
+ ratio = stream && stream->codec ? stream->codec ->sample_aspect_ratio : undef;
424
+ av_reduce (&ratio.num , &ratio.den , ratio.num , ratio.den , INT_MAX);
425
+ if (ratio.num > 0 && ratio.den > 0 )
426
+ return ratio;
427
+
428
+ return undef;
429
+ #endif
430
+ }
431
+
409
432
410
433
struct CvCapture_FFMPEG
411
434
{
@@ -427,7 +450,6 @@ struct CvCapture_FFMPEG
427
450
double get_duration_sec () const ;
428
451
double get_fps () const ;
429
452
int get_bitrate () const ;
430
- AVRational get_sample_aspect_ratio (AVStream *stream) const ;
431
453
432
454
double r2d (AVRational r) const ;
433
455
int64_t dts_to_frame_number (int64_t dts);
@@ -1089,9 +1111,9 @@ double CvCapture_FFMPEG::getProperty( int property_id ) const
1089
1111
return (double )video_st->codec .codec_tag ;
1090
1112
#endif
1091
1113
case CV_FFMPEG_CAP_PROP_SAR_NUM:
1092
- return get_sample_aspect_ratio (ic->streams [video_stream]).num ;
1114
+ return _opencv_ffmpeg_get_sample_aspect_ratio (ic->streams [video_stream]).num ;
1093
1115
case CV_FFMPEG_CAP_PROP_SAR_DEN:
1094
- return get_sample_aspect_ratio (ic->streams [video_stream]).den ;
1116
+ return _opencv_ffmpeg_get_sample_aspect_ratio (ic->streams [video_stream]).den ;
1095
1117
default :
1096
1118
break ;
1097
1119
}
@@ -1164,28 +1186,6 @@ int64_t CvCapture_FFMPEG::dts_to_frame_number(int64_t dts)
1164
1186
return (int64_t )(get_fps () * sec + 0.5 );
1165
1187
}
1166
1188
1167
- AVRational CvCapture_FFMPEG::get_sample_aspect_ratio (AVStream *stream) const
1168
- {
1169
- AVRational undef = {0 , 1 };
1170
- AVRational stream_sample_aspect_ratio = stream ? stream->sample_aspect_ratio : undef;
1171
- AVRational frame_sample_aspect_ratio = stream && stream->codec ? stream->codec ->sample_aspect_ratio : undef;
1172
-
1173
- av_reduce (&stream_sample_aspect_ratio.num , &stream_sample_aspect_ratio.den ,
1174
- stream_sample_aspect_ratio.num , stream_sample_aspect_ratio.den , INT_MAX);
1175
- if (stream_sample_aspect_ratio.num <= 0 || stream_sample_aspect_ratio.den <= 0 )
1176
- stream_sample_aspect_ratio = undef;
1177
-
1178
- av_reduce (&frame_sample_aspect_ratio.num , &frame_sample_aspect_ratio.den ,
1179
- frame_sample_aspect_ratio.num , frame_sample_aspect_ratio.den , INT_MAX);
1180
- if (frame_sample_aspect_ratio.num <= 0 || frame_sample_aspect_ratio.den <= 0 )
1181
- frame_sample_aspect_ratio = undef;
1182
-
1183
- if (stream_sample_aspect_ratio.num )
1184
- return stream_sample_aspect_ratio;
1185
- else
1186
- return frame_sample_aspect_ratio;
1187
- }
1188
-
1189
1189
double CvCapture_FFMPEG::dts_to_sec (int64_t dts)
1190
1190
{
1191
1191
return (double )(dts - ic->streams [video_stream]->start_time ) *
0 commit comments