Skip to content

Commit be24c58

Browse files
authored
Merge pull request #312 from ferdnyc/detect-interlace
FFmpegReader: Detect interlaced video on file open
2 parents f3fc0d8 + 9e0d194 commit be24c58

File tree

1 file changed

+35
-20
lines changed

1 file changed

+35
-20
lines changed

src/FFmpegReader.cpp

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,33 @@ void FFmpegReader::UpdateVideoInfo() {
729729
info.display_ratio.num = size.num;
730730
info.display_ratio.den = size.den;
731731

732+
// Get scan type and order from codec context/params
733+
if (!check_interlace) {
734+
check_interlace = true;
735+
AVFieldOrder field_order = AV_GET_CODEC_ATTRIBUTES(pStream, pCodecCtx)->field_order;
736+
switch(field_order) {
737+
case AV_FIELD_PROGRESSIVE:
738+
info.interlaced_frame = false;
739+
break;
740+
case AV_FIELD_TT:
741+
case AV_FIELD_TB:
742+
info.interlaced_frame = true;
743+
info.top_field_first = true;
744+
break;
745+
case AV_FIELD_BT:
746+
case AV_FIELD_BB:
747+
info.interlaced_frame = true;
748+
info.top_field_first = false;
749+
break;
750+
case AV_FIELD_UNKNOWN:
751+
// Check again later?
752+
check_interlace = false;
753+
break;
754+
}
755+
// check_interlace will prevent these checks being repeated,
756+
// unless it was cleared because we got an AV_FIELD_UNKNOWN response.
757+
}
758+
732759
// Set the video timebase
733760
info.video_timebase.num = pStream->time_base.num;
734761
info.video_timebase.den = pStream->time_base.den;
@@ -1078,14 +1105,14 @@ bool FFmpegReader::GetAVFrame() {
10781105
ZmqLogger::Instance()->AppendDebugMethod("FFmpegReader::GetAVFrame (Packet not sent)");
10791106
}
10801107
else {
1081-
AVFrame *next_frame2;
1082-
if (hw_de_on && hw_de_supported) {
1083-
next_frame2 = AV_ALLOCATE_FRAME();
1084-
}
1085-
else
1086-
{
1087-
next_frame2 = next_frame;
1088-
}
1108+
AVFrame *next_frame2;
1109+
if (hw_de_on && hw_de_supported) {
1110+
next_frame2 = AV_ALLOCATE_FRAME();
1111+
}
1112+
else
1113+
{
1114+
next_frame2 = next_frame;
1115+
}
10891116
pFrame = AV_ALLOCATE_FRAME();
10901117
while (ret >= 0) {
10911118
ret = avcodec_receive_frame(pCodecCtx, next_frame2);
@@ -1119,11 +1146,6 @@ bool FFmpegReader::GetAVFrame() {
11191146
av_image_alloc(pFrame->data, pFrame->linesize, info.width, info.height, (AVPixelFormat)(pStream->codecpar->format), 1);
11201147
av_image_copy(pFrame->data, pFrame->linesize, (const uint8_t**)next_frame->data, next_frame->linesize,
11211148
(AVPixelFormat)(pStream->codecpar->format), info.width, info.height);
1122-
if (!check_interlace) {
1123-
check_interlace = true;
1124-
info.interlaced_frame = next_frame->interlaced_frame;
1125-
info.top_field_first = next_frame->top_field_first;
1126-
}
11271149
}
11281150
}
11291151
if (hw_de_on && hw_de_supported) {
@@ -1143,13 +1165,6 @@ bool FFmpegReader::GetAVFrame() {
11431165
avpicture_alloc((AVPicture *) pFrame, pCodecCtx->pix_fmt, info.width, info.height);
11441166
av_picture_copy((AVPicture *) pFrame, (AVPicture *) next_frame, pCodecCtx->pix_fmt, info.width,
11451167
info.height);
1146-
1147-
// Detect interlaced frame (only once)
1148-
if (!check_interlace) {
1149-
check_interlace = true;
1150-
info.interlaced_frame = next_frame->interlaced_frame;
1151-
info.top_field_first = next_frame->top_field_first;
1152-
}
11531168
}
11541169
#endif
11551170
}

0 commit comments

Comments
 (0)