Commit 420f158f64 for ffmpeg

commit 420f158f648571c859a31582bedb462f2412aa35
Author: Baptiste Coudurier <baptiste.coudurier@gmail.com>
Date:   Sun Nov 22 12:25:09 2026 -0800

    avformat/movenc: select DVCPRO HD tags from coded family and scan type

    Use the width for 1080 lines and the first frame for 720 lines, else the
    existing tag or the frame rate, and use dvh2/dvh3 when progressive.

    Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
    Signed-off-by: Baptiste Coudurier <baptiste.coudurier@gmail.com>
    Assisted-by: Fairy

diff --git a/configure b/configure
index eed7a76aa9..3790ca7e3d 100755
--- a/configure
+++ b/configure
@@ -3963,7 +3963,7 @@ mlp_demuxer_select="mlp_parser"
 mmf_muxer_select="riffenc"
 mov_demuxer_select="iso_media riffdec"
 mov_demuxer_suggest="iamfdec zlib"
-mov_muxer_select="cbs_apv_lavf cbs_av1_lavf iso_media iso_writer riffenc rtpenc_chain vp9_superframe_bsf aac_adtstoasc_bsf ac3_parser"
+mov_muxer_select="cbs_apv_lavf cbs_av1_lavf dvprofile iso_media iso_writer riffenc rtpenc_chain vp9_superframe_bsf aac_adtstoasc_bsf ac3_parser"
 mov_muxer_suggest="iamfenc"
 mp3_demuxer_select="mpegaudio_parser"
 mp3_muxer_select="mpegaudioheader"
diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index a23ca51bbf..9af53ce43e 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -42,6 +42,7 @@
 #include "lcevc.h"
 #include "libavcodec/ac3_parser_internal.h"
 #include "libavcodec/dnxhddata.h"
+#include "libavcodec/dv_profile.h"
 #include "libavcodec/flac.h"
 #include "libavcodec/mpegaudiodata.h"
 #include "libavcodec/get_bits.h"
@@ -1975,9 +1976,34 @@ static int mov_write_dpxe_tag(AVIOContext *pb, MOVTrack *track)
     return 0;
 }

+static int mov_is_dv_720_tag(unsigned tag)
+{
+    return tag == MKTAG('d','v','h','q') ||
+           tag == MKTAG('d','v','h','p') ||
+           tag == MKTAG('d','v','h','4');
+}
+
 static int mov_get_dv_codec_tag(AVFormatContext *s, MOVTrack *track)
 {
-    int tag;
+    int tag, rate_50hz;
+    /* The coded DV system can differ from the presentation frame rate. */
+    AVRational rate = track->par->framerate;
+    int coded_rate = rate.num && rate.den;
+    int64_t fps;
+    int progressive = track->par->field_order == AV_FIELD_PROGRESSIVE;
+
+    if (!coded_rate)
+        rate = track->st->avg_frame_rate;
+    if (!rate.num || !rate.den)
+        rate = av_inv_q(track->st->time_base);
+    /* average frame rates may be slightly off */
+    fps = rate.den > 0 ? av_rescale(rate.num, 1, rate.den) : 0;
+    rate_50hz = fps == 25 || fps == 50;
+
+    /* the 1080-line bitstream does not signal progressive frames */
+    if (track->par->field_order == AV_FIELD_UNKNOWN)
+        progressive = track->par->codec_tag == MKTAG('d','v','h','2') ||
+                      track->par->codec_tag == MKTAG('d','v','h','3');

     if (track->par->width == 720) { /* SD */
         if (track->par->height == 480) { /* NTSC */
@@ -1987,11 +2013,16 @@ static int mov_get_dv_codec_tag(AVFormatContext *s, MOVTrack *track)
         else if (track->par->format == AV_PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
         else                                                tag = MKTAG('d','v','p','p');
     } else if (track->par->height == 720) { /* HD 720 line */
-        if  (track->st->time_base.den == 50)                tag = MKTAG('d','v','h','q');
+        if (!coded_rate && mov_is_dv_720_tag(track->par->codec_tag))
+            tag = track->par->codec_tag;
+        else if (rate_50hz)                                 tag = MKTAG('d','v','h','q');
         else                                                tag = MKTAG('d','v','h','p');
     } else if (track->par->height == 1080) { /* HD 1080 line */
-        if  (track->st->time_base.den == 25)                tag = MKTAG('d','v','h','5');
-        else                                                tag = MKTAG('d','v','h','6');
+        if  (track->par->width == 1440 ||
+             (track->par->width != 1280 && rate_50hz))
+            tag = progressive ? MKTAG('d','v','h','2') : MKTAG('d','v','h','5');
+        else
+            tag = progressive ? MKTAG('d','v','h','3') : MKTAG('d','v','h','6');
     } else {
         av_log(s, AV_LOG_ERROR, "unsupported height for dv codec\n");
         return 0;
@@ -7406,6 +7437,20 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
         return AVERROR_PATCHWELCOME;
     }

+    /* The first frame gives the 720-line DV family. */
+    if (par->codec_id == AV_CODEC_ID_DVVIDEO && par->height == 720 &&
+        !trk->entry && !mov->moov_written && trk->mode == MODE_MOV &&
+        s->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
+        mov_is_dv_720_tag(trk->tag)) {
+        const AVDVProfile *sys = av_dv_frame_profile(NULL, pkt->data, pkt->size);
+        if (sys && sys->height == 720) {
+            if (sys->dsf)
+                trk->tag = MKTAG('d','v','h','q');
+            else if (trk->tag != MKTAG('d','v','h','4'))
+                trk->tag = MKTAG('d','v','h','p');
+        }
+    }
+
     if (mov->flags & FF_MOV_FLAG_FRAGMENT || mov->mode == MODE_AVIF) {
         int ret;
         if (mov->moov_written || mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
diff --git a/tests/fate/mov.mak b/tests/fate/mov.mak
index 2d3b3dd777..3660d321c2 100644
--- a/tests/fate/mov.mak
+++ b/tests/fate/mov.mak
@@ -455,6 +455,21 @@ fate-mov-mp4-mpegh-5_1_4: CMD = stream_demux mp4 $(TARGET_SAMPLES)/mov/mpegh_lay
 FATE_MOV_FFMPEG_FFPROBE_SAMPLES-$(call REMUX, MP4 MOV) += fate-mov-mp4-mpegh-5_1_4-copy
 fate-mov-mp4-mpegh-5_1_4-copy: CMD = stream_remux mp4 $(TARGET_SAMPLES)/mov/mpegh_layout16.mp4 "" mp4 "" "" "-c:a copy" "-show_entries stream=profile,level,codec_tag_string"

+# The 1080-line DV bitstream does not signal progressive frames: keep dvh2.
+FATE_MOV_FFMPEG_FFPROBE_SAMPLES-$(call ALLYES, FFMPEG FFPROBE MOV_MUXER MOV_DEMUXER FILE_PROTOCOL) += fate-mov-dv-tag-copy-1080p25
+fate-mov-dv-tag-copy-1080p25: CMD = run_with_temp \
+    "$(FFMPEG) -nostdin -v error -i $(TARGET_SAMPLES)/dv/dvcprohd_1080p25.mov -map 0:v -write_tmcd 0 -c copy -f mov -y" \
+    "ffprobe$(PROGSSUF)$(EXESUF) -v error -bitexact -show_data_hash CRC32 \
+    -show_entries packet=size,data_hash:stream=codec_tag_string -of compact=p=0" mov
+
+# The first packet must correct the provisional 60 Hz tag before the moov.
+FATE_MOV_FFMPEG_FFPROBE_SAMPLES-$(call ALLYES, FFMPEG FFPROBE MOV_MUXER MOV_DEMUXER FILE_PROTOCOL) += fate-mov-dv-tag-delay-720p50
+fate-mov-dv-tag-delay-720p50: CMD = run_with_temp \
+    "$(FFMPEG) -nostdin -v error -nofind_stream_info -i $(TARGET_SAMPLES)/dv/dvcprohd_720p50.mov \
+    -map 0:v -write_tmcd 0 -c copy -tag:v dvhp -movflags +delay_moov -f mov -y" \
+    "ffprobe$(PROGSSUF)$(EXESUF) -v error -bitexact -show_data_hash CRC32 \
+    -show_entries packet=size,data_hash:stream=codec_tag_string -of compact=p=0" mov
+
 FATE_SAMPLES_FFMPEG_FFPROBE += $(FATE_MOV_FFMPEG_FFPROBE_SAMPLES-yes)
 FATE_SAMPLES_FFMPEG += $(FATE_MOV_FFMPEG_SAMPLES-yes)

diff --git a/tests/ref/fate/mov-dv-tag-copy-1080p25 b/tests/ref/fate/mov-dv-tag-copy-1080p25
new file mode 100644
index 0000000000..4f57c9290b
--- /dev/null
+++ b/tests/ref/fate/mov-dv-tag-copy-1080p25
@@ -0,0 +1,2 @@
+size=576000|data_hash=CRC32:0b08f48b
+codec_tag_string=dvh2
diff --git a/tests/ref/fate/mov-dv-tag-delay-720p50 b/tests/ref/fate/mov-dv-tag-delay-720p50
new file mode 100644
index 0000000000..0dc6cfd2e5
--- /dev/null
+++ b/tests/ref/fate/mov-dv-tag-delay-720p50
@@ -0,0 +1,2 @@
+size=288000|data_hash=CRC32:2baffb02
+codec_tag_string=dvhq