PR #26023 opened by Baptiste Coudurier (bcoudurier) URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26023 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26023.patch
Make the DV encoder, decoder, muxer and demuxer follow SMPTE 314M, SMPTE 370M and IEC 61834. - dvenc: second subcode block carried SSYB numbers 0-5 instead of 6-11, with wrong AP3/APT bits; VS/VSC packs were repeated in reserved VAUX positions; DV100 used QNO 15 class 3, which 370M does not define. - dvdec/dv: take BCSYS into account for 16:9. - dv demuxer: read the time code from any SSYB (SMPTE streams only have it in 3/5/9/11); 720p time code and audio timestamps span frame pairs. - dv muxer: SMPTE pack layout when APT != 0, IEC 61834 otherwise; fix locked mode, emphasis, source situation, week day, the 0x8000 audio error code, and 720p muxing (one audio frame over two frames, up to 4 stereo pairs). The time code is also written to SSYB 0 and 6, where Apple and older readers look for it. - frame_thread_encoder: worker threads always saw frame_num 0, so 720p frames were all written as first halves. Tested against Apple's DV/DVCPRO/DVCPRO HD encoders and decoders, both ways, for all profiles (25, 29.97, 50, 59.94), in MOV, MXF and raw DV, and against real camcorder streams for the IEC layout. 🤖 Generated with [Claude Code](https://claude.com/claude-code) From 5a247931605491c56ed029082fd7051f2f49f452 Mon Sep 17 00:00:00 2001 From: Baptiste Coudurier <[email protected]> Date: Thu, 12 Nov 2026 19:25:46 -0800 Subject: [PATCH 1/7] avcodec/frame_thread_encoder: set frame_num in the worker threads Worker contexts always saw frame_num 0, so the dv encoder wrote all 720p frames as the first half of a frame pair. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Baptiste Coudurier <[email protected]> --- libavcodec/frame_thread_encoder.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libavcodec/frame_thread_encoder.c b/libavcodec/frame_thread_encoder.c index 6af3db722e0..f0306c134c8 100644 --- a/libavcodec/frame_thread_encoder.c +++ b/libavcodec/frame_thread_encoder.c @@ -44,6 +44,7 @@ typedef struct{ AVFrame *indata; AVPacket *outdata; + int64_t frame_num; int return_code; int finished; int got_packet; @@ -64,6 +65,7 @@ typedef struct{ unsigned next_task_index; unsigned task_index; unsigned finished_task_index; + int64_t frame_num; ///< number of frames submitted pthread_t worker[MAX_THREADS]; atomic_int exit; @@ -104,6 +106,7 @@ static void * attribute_align_arg worker(void *v){ task = &c->tasks[task_index]; frame = task->indata; pkt = task->outdata; + avctx->frame_num = task->frame_num; ret = ff_encode_encode_cb(avctx, pkt, frame, &task->got_packet); pthread_mutex_lock(&c->finished_task_mutex); @@ -308,6 +311,7 @@ int ff_thread_video_encode_frame(AVCodecContext *avctx, AVPacket *pkt, if(frame){ av_frame_move_ref(c->tasks[c->task_index].indata, frame); + c->tasks[c->task_index].frame_num = c->frame_num++; pthread_mutex_lock(&c->task_fifo_mutex); c->task_index = (c->task_index + 1) % c->max_tasks; -- 2.54.0 From 0567d25f8df9314549237a75ffa60644a3e6517c Mon Sep 17 00:00:00 2001 From: Baptiste Coudurier <[email protected]> Date: Fri, 13 Nov 2026 00:02:45 -0800 Subject: [PATCH 2/7] avcodec/dvenc: write SSYB IDs and VAUX packs per SMPTE 314M and 370M Also stop using DV100 QNO 15 with class 3, undefined in SMPTE 370M. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Baptiste Coudurier <[email protected]> --- libavcodec/dvenc.c | 64 +++++++++++++++++------------ tests/ref/lavf/dv | 2 +- tests/ref/lavf/dv_ntsc | 2 +- tests/ref/lavf/dv_pal | 2 +- tests/ref/lavf/mxf_dv25 | 2 +- tests/ref/lavf/mxf_dvcpro100 | 2 +- tests/ref/lavf/mxf_dvcpro50 | 2 +- tests/ref/vsynth/vsynth1-dv | 2 +- tests/ref/vsynth/vsynth1-dv-411 | 2 +- tests/ref/vsynth/vsynth1-dv-50 | 2 +- tests/ref/vsynth/vsynth1-dv-fhd | 2 +- tests/ref/vsynth/vsynth1-dv-hd | 2 +- tests/ref/vsynth/vsynth2-dv | 2 +- tests/ref/vsynth/vsynth2-dv-411 | 2 +- tests/ref/vsynth/vsynth2-dv-50 | 2 +- tests/ref/vsynth/vsynth2-dv-fhd | 2 +- tests/ref/vsynth/vsynth2-dv-hd | 2 +- tests/ref/vsynth/vsynth3-dv-fhd | 2 +- tests/ref/vsynth/vsynth3-dv-hd | 2 +- tests/ref/vsynth/vsynth_lena-dv | 2 +- tests/ref/vsynth/vsynth_lena-dv-411 | 2 +- tests/ref/vsynth/vsynth_lena-dv-50 | 2 +- tests/ref/vsynth/vsynth_lena-dv-fhd | 2 +- tests/ref/vsynth/vsynth_lena-dv-hd | 2 +- 24 files changed, 60 insertions(+), 50 deletions(-) diff --git a/libavcodec/dvenc.c b/libavcodec/dvenc.c index b58e3000482..75e97e0efe2 100644 --- a/libavcodec/dvenc.c +++ b/libavcodec/dvenc.c @@ -334,8 +334,7 @@ static const uint8_t dv100_qlevels[DV100_NUM_QLEVELS] = { DV100_MAKE_QLEVEL(11,2), // 20*4=80 DV100_MAKE_QLEVEL(12,2), // 22*4=88 DV100_MAKE_QLEVEL(13,2), // 24*4=96 - // ... - DV100_MAKE_QLEVEL(15,3), // 52*8=416 + DV100_MAKE_QLEVEL(14,2), // 28*4=112, coarsest step allowed by SMPTE 370M Table 26 }; static const int dv100_min_bias = 0; @@ -1021,6 +1020,12 @@ static int dv_encode_video_segment(AVCodecContext *avctx, void *arg) return 0; } +/* Track application ID: 000 for IEC 61834, 001 for SMPTE 314M/370M */ +static int dv_apt(const AVDVProfile *sys) +{ + return sys->pix_fmt != AV_PIX_FMT_YUV420P; +} + static inline int dv_write_pack(enum DVPackType pack_id, DVEncContext *c, uint8_t *buf) { @@ -1043,7 +1048,7 @@ static inline int dv_write_pack(enum DVPackType pack_id, DVEncContext *c, * compression scheme (if any). */ uint8_t aspect = 0; - int apt = (c->sys->pix_fmt == AV_PIX_FMT_YUV420P ? 0 : 1); + int apt = dv_apt(c->sys); int fs; if (c->avctx->height >= 720) @@ -1082,7 +1087,8 @@ static inline int dv_write_pack(enum DVPackType pack_id, DVEncContext *c, buf[3] = (3 << 6) | /* reserved -- always 1 */ (c->sys->dsf << 5) | /* system: 60fields/50fields */ c->sys->video_stype; /* signal type video compression */ - buf[4] = 0xff; /* VISC: 0xff -- no information */ + buf[4] = apt ? 0x7f : /* VISC: no information (SMPTE 314M/370M) */ + 0xff; /* tuner category: no information (IEC 61834) */ break; case DV_VIDEO_CONTROL: buf[1] = (0 << 6) | /* Copy generation management (CGMS) 0 -- free */ @@ -1121,21 +1127,20 @@ static inline int dv_write_dif_id(enum DVSectionType t, uint8_t chan_num, return 3; } -static inline int dv_write_ssyb_id(uint8_t syb_num, uint8_t fr, uint8_t *buf) +static inline int dv_write_ssyb_id(uint8_t syb_num, uint8_t fr, int apt, uint8_t *buf) { - if (syb_num == 0 || syb_num == 6) { - buf[0] = (fr << 7) | /* FR ID 1 - first half of each channel; 0 - second */ - (0 << 4) | /* AP3 (Subcode application ID) */ - 0x0f; /* reserved -- always 1 */ - } else if (syb_num == 11) { - buf[0] = (fr << 7) | /* FR ID 1 - first half of each channel; 0 - second */ - 0x7f; /* reserved -- always 1 */ - } else { - buf[0] = (fr << 7) | /* FR ID 1 - first half of each channel; 0 - second */ - (0 << 4) | /* APT (Track application ID) */ - 0x0f; /* reserved -- always 1 */ - } - buf[1] = 0xf0 | /* reserved -- always 1 */ + int id; + + if (syb_num == 0 || syb_num == 6) + id = apt; /* AP3 (Subcode application ID) */ + else if (syb_num == 11) + id = apt; /* APT (Track application ID) */ + else + id = 7; /* reserved -- always 1 */ + buf[0] = (fr << 7) | /* FR ID 1 - first half of each channel; 0 - second */ + (id << 4) | + 0x0f; /* arbitrary */ + buf[1] = 0xf0 | /* arbitrary */ (syb_num & 0x0f); /* SSYB number 0 - 11 */ buf[2] = 0xff; /* reserved -- always 1 */ return 3; @@ -1146,6 +1151,7 @@ static void dv_format_frame(DVEncContext *c, uint8_t *buf) int chan, i, j, k; /* We work with 720p frames split in half. The odd half-frame is chan 2,3 */ int chan_offset = 2*(c->sys->height == 720 && c->avctx->frame_num & 1); + int apt = dv_apt(c->sys); for (chan = 0; chan < c->sys->n_difchan; chan++) { for (i = 0; i < c->sys->difseg_size; i++) { @@ -1157,23 +1163,27 @@ static void dv_format_frame(DVEncContext *c, uint8_t *buf) c, buf); buf += 72; /* unused bytes */ - /* DV subcode: 2DIFs */ + /* DV subcode: 2DIFs, SSYB 0-5 and 6-11 */ for (j = 0; j < 2; j++) { buf += dv_write_dif_id(DV_SECT_SUBCODE, chan+chan_offset, i, j, buf); for (k = 0; k < 6; k++) - buf += dv_write_ssyb_id(k, (i < c->sys->difseg_size / 2), buf) + 5; + buf += dv_write_ssyb_id(j * 6 + k, (i < c->sys->difseg_size / 2), + apt, buf) + 5; buf += 29; /* unused bytes */ } - /* DV VAUX: 3DIFS */ + /* DV VAUX: 3DIFS, the VS and VSC packs are pack numbers 39 and 40 + * in even DIF sequences, 0 and 1 in odd ones */ for (j = 0; j < 3; j++) { buf += dv_write_dif_id(DV_SECT_VAUX, chan+chan_offset, i, j, buf); - buf += dv_write_pack(DV_VIDEO_SOURCE, c, buf); - buf += dv_write_pack(DV_VIDEO_CONTROL, c, buf); - buf += 7 * 5; - buf += dv_write_pack(DV_VIDEO_SOURCE, c, buf); - buf += dv_write_pack(DV_VIDEO_CONTROL, c, buf); - buf += 4 * 5 + 2; /* unused bytes */ + if ((i & 1) && j == 0) { + dv_write_pack(DV_VIDEO_SOURCE, c, buf); + dv_write_pack(DV_VIDEO_CONTROL, c, buf + 5); + } else if (!(i & 1) && j == 2) { + dv_write_pack(DV_VIDEO_SOURCE, c, buf + 9 * 5); + dv_write_pack(DV_VIDEO_CONTROL, c, buf + 10 * 5); + } + buf += 15 * 5 + 2; } /* DV Audio/Video: 135 Video DIFs + 9 Audio DIFs */ diff --git a/tests/ref/lavf/dv b/tests/ref/lavf/dv index d3015714111..e99cc439fdb 100644 --- a/tests/ref/lavf/dv +++ b/tests/ref/lavf/dv @@ -1,3 +1,3 @@ -343b03a4ae5a95adb7340fc453c7e61e *tests/data/lavf/lavf.dv +7a5a5ac11e81ecf4ed1e0b7a3a4dfe45 *tests/data/lavf/lavf.dv 3600000 tests/data/lavf/lavf.dv tests/data/lavf/lavf.dv CRC=0x3efa60d5 diff --git a/tests/ref/lavf/dv_ntsc b/tests/ref/lavf/dv_ntsc index 7b5953461a8..e677ab53fe7 100644 --- a/tests/ref/lavf/dv_ntsc +++ b/tests/ref/lavf/dv_ntsc @@ -1,3 +1,3 @@ -947f17de36e17a8f91c844ae84ea1409 *tests/data/lavf/lavf.dv_ntsc +e68e9d6a3ae391f020930541dec65b98 *tests/data/lavf/lavf.dv_ntsc 3480000 tests/data/lavf/lavf.dv_ntsc tests/data/lavf/lavf.dv_ntsc CRC=0xadfc9598 diff --git a/tests/ref/lavf/dv_pal b/tests/ref/lavf/dv_pal index b16911f8d11..3c2e8cc29f9 100644 --- a/tests/ref/lavf/dv_pal +++ b/tests/ref/lavf/dv_pal @@ -1,3 +1,3 @@ -7a0264fd1c59908e6c6c3d7557a1ca21 *tests/data/lavf/lavf.dv_pal +64e61fe4bf46434ff42d2e527d60bfdf *tests/data/lavf/lavf.dv_pal 3600000 tests/data/lavf/lavf.dv_pal tests/data/lavf/lavf.dv_pal CRC=0x5573b571 diff --git a/tests/ref/lavf/mxf_dv25 b/tests/ref/lavf/mxf_dv25 index 33a05113984..39e0b6f6357 100644 --- a/tests/ref/lavf/mxf_dv25 +++ b/tests/ref/lavf/mxf_dv25 @@ -1,3 +1,3 @@ -e9a10efc1a6d127448ac6b60b0b3af51 *tests/data/lavf/lavf.mxf_dv25 +bbd0a3da5a10977aae8c238fb5074fa2 *tests/data/lavf/lavf.mxf_dv25 3834413 tests/data/lavf/lavf.mxf_dv25 tests/data/lavf/lavf.mxf_dv25 CRC=0x3efa60d5 diff --git a/tests/ref/lavf/mxf_dvcpro100 b/tests/ref/lavf/mxf_dvcpro100 index 21e1d21e536..a1606433094 100644 --- a/tests/ref/lavf/mxf_dvcpro100 +++ b/tests/ref/lavf/mxf_dvcpro100 @@ -1,3 +1,3 @@ -a2d6031d60aee54d24919d7628a1f7b5 *tests/data/lavf/lavf.mxf_dvcpro100 +fcc49adb758be1be03078e0bbcab5f5b *tests/data/lavf/lavf.mxf_dvcpro100 14637613 tests/data/lavf/lavf.mxf_dvcpro100 tests/data/lavf/lavf.mxf_dvcpro100 CRC=0x25feb3fd diff --git a/tests/ref/lavf/mxf_dvcpro50 b/tests/ref/lavf/mxf_dvcpro50 index 7cf0cc8f0c1..500839f4fc7 100644 --- a/tests/ref/lavf/mxf_dvcpro50 +++ b/tests/ref/lavf/mxf_dvcpro50 @@ -1,3 +1,3 @@ -25160bc35e28e0fe89b5ce1ea89e0eff *tests/data/lavf/lavf.mxf_dvcpro50 +e29d64f2f2d9852c7617d23b880e1ec4 *tests/data/lavf/lavf.mxf_dvcpro50 7431213 tests/data/lavf/lavf.mxf_dvcpro50 tests/data/lavf/lavf.mxf_dvcpro50 CRC=0xfd34a6d9 diff --git a/tests/ref/vsynth/vsynth1-dv b/tests/ref/vsynth/vsynth1-dv index b381cea89cd..ffbb6c9dea3 100644 --- a/tests/ref/vsynth/vsynth1-dv +++ b/tests/ref/vsynth/vsynth1-dv @@ -1,4 +1,4 @@ -2d6d0aa8d50ad1678080fa9c33e24bc1 *tests/data/fate/vsynth1-dv.dv +b1667f501827fc842c34b5524b503c5d *tests/data/fate/vsynth1-dv.dv 7200000 tests/data/fate/vsynth1-dv.dv 7dcf06a0956c25c407c72140cdcfa078 *tests/data/fate/vsynth1-dv.out.rawvideo stddev: 6.90 PSNR: 31.34 MAXDIFF: 76 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth/vsynth1-dv-411 b/tests/ref/vsynth/vsynth1-dv-411 index 86e64cf39d7..2cfbc3cddf8 100644 --- a/tests/ref/vsynth/vsynth1-dv-411 +++ b/tests/ref/vsynth/vsynth1-dv-411 @@ -1,4 +1,4 @@ -a2a0d0c750707a1b8facbe6ebf3cc446 *tests/data/fate/vsynth1-dv-411.dv +46193233ea9200c9ba3c9ad93a77d7bd *tests/data/fate/vsynth1-dv-411.dv 7200000 tests/data/fate/vsynth1-dv-411.dv 4e294ecbb5ad48ce776fea99ef35d89e *tests/data/fate/vsynth1-dv-411.out.rawvideo stddev: 9.43 PSNR: 28.63 MAXDIFF: 84 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth/vsynth1-dv-50 b/tests/ref/vsynth/vsynth1-dv-50 index 920b2607091..0e8adc31f6f 100644 --- a/tests/ref/vsynth/vsynth1-dv-50 +++ b/tests/ref/vsynth/vsynth1-dv-50 @@ -1,4 +1,4 @@ -f8a602f4b0003576f284a4a69b096707 *tests/data/fate/vsynth1-dv-50.dv +8951d46f08e193168bca07fa1102ce56 *tests/data/fate/vsynth1-dv-50.dv 14400000 tests/data/fate/vsynth1-dv-50.dv 1e9aeeb0ff36ccd83017cad460384bc3 *tests/data/fate/vsynth1-dv-50.out.rawvideo stddev: 1.72 PSNR: 43.37 MAXDIFF: 29 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth/vsynth1-dv-fhd b/tests/ref/vsynth/vsynth1-dv-fhd index 0027208ab9d..d0ae2823b21 100644 --- a/tests/ref/vsynth/vsynth1-dv-fhd +++ b/tests/ref/vsynth/vsynth1-dv-fhd @@ -1,4 +1,4 @@ -70b61b2668c657ea8e861cbffc8f417f *tests/data/fate/vsynth1-dv-fhd.dv +cdbab155c27a667e8589b7648a2aae63 *tests/data/fate/vsynth1-dv-fhd.dv 28800000 tests/data/fate/vsynth1-dv-fhd.dv af6efc55053a9b5407dfd0e7722deb90 *tests/data/fate/vsynth1-dv-fhd.out.rawvideo stddev: 2.54 PSNR: 40.02 MAXDIFF: 35 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth/vsynth1-dv-hd b/tests/ref/vsynth/vsynth1-dv-hd index 86644e8b366..237cc29197b 100644 --- a/tests/ref/vsynth/vsynth1-dv-hd +++ b/tests/ref/vsynth/vsynth1-dv-hd @@ -1,4 +1,4 @@ -4ee275d498d80ea25f07700e1b045768 *tests/data/fate/vsynth1-dv-hd.dv +d32d7bc353465c976ce26c194bdb5481 *tests/data/fate/vsynth1-dv-hd.dv 14400000 tests/data/fate/vsynth1-dv-hd.dv 20e3ea2d0341a0b323b8fdc223554619 *tests/data/fate/vsynth1-dv-hd.out.rawvideo stddev: 4.28 PSNR: 35.50 MAXDIFF: 74 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth/vsynth2-dv b/tests/ref/vsynth/vsynth2-dv index ee7c6fa4f7b..99abeae2d48 100644 --- a/tests/ref/vsynth/vsynth2-dv +++ b/tests/ref/vsynth/vsynth2-dv @@ -1,4 +1,4 @@ -e77f0b747ed8d1492c74a12acb47752e *tests/data/fate/vsynth2-dv.dv +ba042068d0a47d3cc7f83180d4890a0a *tests/data/fate/vsynth2-dv.dv 7200000 tests/data/fate/vsynth2-dv.dv f16c6dc6fcf7e2de82460e10985e5d10 *tests/data/fate/vsynth2-dv.out.rawvideo stddev: 1.99 PSNR: 42.13 MAXDIFF: 38 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth/vsynth2-dv-411 b/tests/ref/vsynth/vsynth2-dv-411 index c9057ba29a2..fb88fb794c2 100644 --- a/tests/ref/vsynth/vsynth2-dv-411 +++ b/tests/ref/vsynth/vsynth2-dv-411 @@ -1,4 +1,4 @@ -89b47e267c6a89f508863b5413df8477 *tests/data/fate/vsynth2-dv-411.dv +1e1bee4489cfba04b565dbf0ec6c9d90 *tests/data/fate/vsynth2-dv-411.dv 7200000 tests/data/fate/vsynth2-dv-411.dv 4c73684b84cdab58e99752c9d1e60b6d *tests/data/fate/vsynth2-dv-411.out.rawvideo stddev: 3.48 PSNR: 37.28 MAXDIFF: 56 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth/vsynth2-dv-50 b/tests/ref/vsynth/vsynth2-dv-50 index dcd4259eb24..cdcc06217b5 100644 --- a/tests/ref/vsynth/vsynth2-dv-50 +++ b/tests/ref/vsynth/vsynth2-dv-50 @@ -1,4 +1,4 @@ -322b2e94e1408a683244b1b347448ad4 *tests/data/fate/vsynth2-dv-50.dv +0e25653fe86a228a893e474fb04d4610 *tests/data/fate/vsynth2-dv-50.dv 14400000 tests/data/fate/vsynth2-dv-50.dv ac8fa753f86a1ad0422e7e1bc1334433 *tests/data/fate/vsynth2-dv-50.out.rawvideo stddev: 0.86 PSNR: 49.43 MAXDIFF: 17 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth/vsynth2-dv-fhd b/tests/ref/vsynth/vsynth2-dv-fhd index ec025e6af14..39a04b74dd3 100644 --- a/tests/ref/vsynth/vsynth2-dv-fhd +++ b/tests/ref/vsynth/vsynth2-dv-fhd @@ -1,4 +1,4 @@ -25cc6c3dc0c29374eb57446c31023313 *tests/data/fate/vsynth2-dv-fhd.dv +86bdeda24e755548d6965a96b801d430 *tests/data/fate/vsynth2-dv-fhd.dv 28800000 tests/data/fate/vsynth2-dv-fhd.dv 247449892bb8dbc5be8147c670981c66 *tests/data/fate/vsynth2-dv-fhd.out.rawvideo stddev: 1.16 PSNR: 46.83 MAXDIFF: 21 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth/vsynth2-dv-hd b/tests/ref/vsynth/vsynth2-dv-hd index 141d5b1cb31..37c5ea438de 100644 --- a/tests/ref/vsynth/vsynth2-dv-hd +++ b/tests/ref/vsynth/vsynth2-dv-hd @@ -1,4 +1,4 @@ -60d48e611674a055808d0d892a537468 *tests/data/fate/vsynth2-dv-hd.dv +96deba58a874daa8bc49442bdc3ad823 *tests/data/fate/vsynth2-dv-hd.dv 14400000 tests/data/fate/vsynth2-dv-hd.dv fdf957d1e9d70a5e832dbcdfd2d5a27b *tests/data/fate/vsynth2-dv-hd.out.rawvideo stddev: 1.74 PSNR: 43.28 MAXDIFF: 34 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth/vsynth3-dv-fhd b/tests/ref/vsynth/vsynth3-dv-fhd index 3c48d90de77..bcd8dcab105 100644 --- a/tests/ref/vsynth/vsynth3-dv-fhd +++ b/tests/ref/vsynth/vsynth3-dv-fhd @@ -1,4 +1,4 @@ -f9d934b86233425aefe65f838e513e54 *tests/data/fate/vsynth3-dv-fhd.dv +c6bdeb2e94215fcdca6c067ef6551a91 *tests/data/fate/vsynth3-dv-fhd.dv 28800000 tests/data/fate/vsynth3-dv-fhd.dv a038ad7c3c09f776304ef7accdea9c74 *tests/data/fate/vsynth3-dv-fhd.out.rawvideo stddev: 0.00 PSNR:999.99 MAXDIFF: 0 bytes: 86700/ 86700 diff --git a/tests/ref/vsynth/vsynth3-dv-hd b/tests/ref/vsynth/vsynth3-dv-hd index 9ec5341e9d7..0085d9894d1 100644 --- a/tests/ref/vsynth/vsynth3-dv-hd +++ b/tests/ref/vsynth/vsynth3-dv-hd @@ -1,4 +1,4 @@ -76680acd86ee4dd16404f49e32cf3f38 *tests/data/fate/vsynth3-dv-hd.dv +cc8e9a707d546024c92f784afff2d7f7 *tests/data/fate/vsynth3-dv-hd.dv 14400000 tests/data/fate/vsynth3-dv-hd.dv a038ad7c3c09f776304ef7accdea9c74 *tests/data/fate/vsynth3-dv-hd.out.rawvideo stddev: 0.00 PSNR:999.99 MAXDIFF: 0 bytes: 86700/ 86700 diff --git a/tests/ref/vsynth/vsynth_lena-dv b/tests/ref/vsynth/vsynth_lena-dv index f0732cca101..1906fa915a2 100644 --- a/tests/ref/vsynth/vsynth_lena-dv +++ b/tests/ref/vsynth/vsynth_lena-dv @@ -1,4 +1,4 @@ -04d844b7ec7b4af546ad082b58912d05 *tests/data/fate/vsynth_lena-dv.dv +7df3306c387bcef22843838889975127 *tests/data/fate/vsynth_lena-dv.dv 7200000 tests/data/fate/vsynth_lena-dv.dv 0c7c574084780daf0e9732cf7a8a8fa5 *tests/data/fate/vsynth_lena-dv.out.rawvideo stddev: 1.69 PSNR: 43.54 MAXDIFF: 33 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth/vsynth_lena-dv-411 b/tests/ref/vsynth/vsynth_lena-dv-411 index 09ba97c5605..8dea366cb23 100644 --- a/tests/ref/vsynth/vsynth_lena-dv-411 +++ b/tests/ref/vsynth/vsynth_lena-dv-411 @@ -1,4 +1,4 @@ -d7c8447eb7b5ff54109b6c789f099f55 *tests/data/fate/vsynth_lena-dv-411.dv +25ebf952fd588426583a456637462be8 *tests/data/fate/vsynth_lena-dv-411.dv 7200000 tests/data/fate/vsynth_lena-dv-411.dv a2e4189e19c8b1d1f68c8a75db14041a *tests/data/fate/vsynth_lena-dv-411.out.rawvideo stddev: 2.87 PSNR: 38.96 MAXDIFF: 45 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth/vsynth_lena-dv-50 b/tests/ref/vsynth/vsynth_lena-dv-50 index cdda0e401a1..95c700f167b 100644 --- a/tests/ref/vsynth/vsynth_lena-dv-50 +++ b/tests/ref/vsynth/vsynth_lena-dv-50 @@ -1,4 +1,4 @@ -68f4c1bb613b3f5d0afd106072b9734c *tests/data/fate/vsynth_lena-dv-50.dv +1b8b837c2441437f562c55341b5266c3 *tests/data/fate/vsynth_lena-dv-50.dv 14400000 tests/data/fate/vsynth_lena-dv-50.dv 9313f04ef89da88fbb7157930f3f418e *tests/data/fate/vsynth_lena-dv-50.out.rawvideo stddev: 0.78 PSNR: 50.28 MAXDIFF: 12 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth/vsynth_lena-dv-fhd b/tests/ref/vsynth/vsynth_lena-dv-fhd index b5701423db5..23e9451f548 100644 --- a/tests/ref/vsynth/vsynth_lena-dv-fhd +++ b/tests/ref/vsynth/vsynth_lena-dv-fhd @@ -1,4 +1,4 @@ -a8a1070929f5016c7cdc5074a6d5739d *tests/data/fate/vsynth_lena-dv-fhd.dv +c2d7b838a7b62070e0572d006bd3d53a *tests/data/fate/vsynth_lena-dv-fhd.dv 28800000 tests/data/fate/vsynth_lena-dv-fhd.dv fba140abe1d417f829c3bf20657d220d *tests/data/fate/vsynth_lena-dv-fhd.out.rawvideo stddev: 1.03 PSNR: 47.80 MAXDIFF: 14 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth/vsynth_lena-dv-hd b/tests/ref/vsynth/vsynth_lena-dv-hd index f92ac2ce1bf..4920484c71a 100644 --- a/tests/ref/vsynth/vsynth_lena-dv-hd +++ b/tests/ref/vsynth/vsynth_lena-dv-hd @@ -1,4 +1,4 @@ -a4583803c9ec843d5ace1228a8d42d4f *tests/data/fate/vsynth_lena-dv-hd.dv +953683604045a1dcb6ffb2fdd477f4c5 *tests/data/fate/vsynth_lena-dv-hd.dv 14400000 tests/data/fate/vsynth_lena-dv-hd.dv 85896fd67284fab17ff60ea04eb2fa9e *tests/data/fate/vsynth_lena-dv-hd.out.rawvideo stddev: 1.48 PSNR: 44.68 MAXDIFF: 26 bytes: 7603200/ 7603200 -- 2.54.0 From 3eb3ac953296e2176b425bb4df325a61af0c803f Mon Sep 17 00:00:00 2001 From: Baptiste Coudurier <[email protected]> Date: Thu, 12 Nov 2026 19:27:04 -0800 Subject: [PATCH 3/7] avcodec/dvdec, avformat/dv: check BCSYS for the 16:9 display mode With BCSYS 1, IEC 61834-4 DISP 010 is a 14:9 letterbox in a 4:3 frame. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Baptiste Coudurier <[email protected]> --- libavcodec/dv.h | 21 +++++++++++++++++++++ libavcodec/dvdec.c | 3 +-- libavformat/dv.c | 3 +-- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/libavcodec/dv.h b/libavcodec/dv.h index abff9f1ea9a..df1cd0f6e26 100644 --- a/libavcodec/dv.h +++ b/libavcodec/dv.h @@ -27,6 +27,8 @@ #ifndef AVCODEC_DV_H #define AVCODEC_DV_H +#include <stdint.h> + enum DVSectionType { DV_SECT_HEADER = 0x1f, DV_SECT_SUBCODE = 0x3f, @@ -50,6 +52,25 @@ enum DVPackType { DV_UNKNOWN_PACK = 0xff, }; +/** + * Check the display select mode of a VAUX source control pack for 16:9. + * + * @param apt track application ID from the DIF header, 0 for IEC 61834 + * @param vsc VAUX source control pack + */ +static inline int ff_dv_is_16_9(int apt, const uint8_t *vsc) +{ + int disp = vsc[2] & 0x07; + int bcsys = vsc[3] & 0x03; + + /* IEC 61834-4 9.2: 16:9 squeeze with BCSYS 0, 14:9 letterbox with + * BCSYS 1, 16:9 anamorphic with BCSYS 1; SMPTE 314M and 370M only + * define 16:9 squeeze. */ + if (disp == 2) + return apt || bcsys != 1; + return !apt && disp == 7; +} + #define DV_PROFILE_IS_HD(p) ((p)->video_stype & 0x10) #define DV_PROFILE_IS_1080i50(p) (((p)->video_stype == 0x14) && ((p)->dsf == 1)) #define DV_PROFILE_IS_1080i60(p) (((p)->video_stype == 0x14) && ((p)->dsf == 0)) diff --git a/libavcodec/dvdec.c b/libavcodec/dvdec.c index 4799ec96dc3..02dfdf40c49 100644 --- a/libavcodec/dvdec.c +++ b/libavcodec/dvdec.c @@ -656,8 +656,7 @@ static int dvvideo_decode_frame(AVCodecContext *avctx, AVFrame *frame, vsc_pack = buf + 80 * 5 + 48 + 5; if (*vsc_pack == DV_VIDEO_CONTROL) { apt = buf[4] & 0x07; - is16_9 = (vsc_pack[2] & 0x07) == 0x02 || - (!apt && (vsc_pack[2] & 0x07) == 0x07); + is16_9 = ff_dv_is_16_9(apt, vsc_pack); ff_set_sar(avctx, s->sys->sar[is16_9]); } diff --git a/libavformat/dv.c b/libavformat/dv.c index 84677284bdf..35bbdc3b32a 100644 --- a/libavformat/dv.c +++ b/libavformat/dv.c @@ -324,8 +324,7 @@ static int dv_extract_video_info(DVDemuxContext *c, const uint8_t *frame) /* finding out SAR is a little bit messy */ vsc_pack = dv_extract_pack(frame, DV_VIDEO_CONTROL); apt = frame[4] & 0x07; - is16_9 = (vsc_pack && ((vsc_pack[2] & 0x07) == 0x02 || - (!apt && (vsc_pack[2] & 0x07) == 0x07))); + is16_9 = vsc_pack && ff_dv_is_16_9(apt, vsc_pack); c->vst->sample_aspect_ratio = c->sys->sar[is16_9]; par->bit_rate = av_rescale_q(c->sys->frame_size, (AVRational) { 8, 1 }, -- 2.54.0 From 7582c1f394deed6aadef75cc618c07f3d694334e Mon Sep 17 00:00:00 2001 From: Baptiste Coudurier <[email protected]> Date: Thu, 12 Nov 2026 19:27:15 -0800 Subject: [PATCH 4/7] avformat/dv: find the time code pack in any SSYB SMPTE 314M and 370M streams only carry it in SSYB 3, 5, 9 and 11. 720p time code counts frame pairs. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Baptiste Coudurier <[email protected]> --- libavformat/dv.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/libavformat/dv.c b/libavformat/dv.c index 35bbdc3b32a..435054c31e0 100644 --- a/libavformat/dv.c +++ b/libavformat/dv.c @@ -115,9 +115,6 @@ static const uint8_t *dv_extract_pack(const uint8_t *frame, enum DVPackType t) if (c&1) offs = (80 * 3 + 8 + c*12000); else offs = (80 * 5 + 48 + 5 + c*12000); break; - case DV_TIMECODE: - offs = (80*1 + 3 + 3); - break; default: return NULL; } @@ -334,17 +331,29 @@ static int dv_extract_video_info(DVDemuxContext *c, const uint8_t *frame) static int dv_extract_timecode(DVDemuxContext* c, const uint8_t* frame, char *tc) { - const uint8_t *tc_pack; + const uint8_t *tc_pack = NULL; + AVRational rate = av_inv_q(c->sys->time_base); // For PAL systems, drop frame bit is replaced by an arbitrary // bit so its value should not be considered. Drop frame timecode // is only relevant for NTSC systems. - int prevent_df = c->sys->ltc_divisor == 25 || c->sys->ltc_divisor == 50; + int prevent_df = c->sys->dsf; - tc_pack = dv_extract_pack(frame, DV_TIMECODE); + /* look for a time code pack in the SSYBs of both subcode DIF blocks, + * SMPTE 314M and 370M only use SSYB 3, 5, 9 and 11 */ + for (int i = 0; i < 12 && !tc_pack; i++) { + const uint8_t *pack = frame + 80 * (1 + i / 6) + 3 + 8 * (i % 6) + 3; + if (*pack == DV_TIMECODE) + tc_pack = pack; + } if (!tc_pack) return 0; - av_timecode_make_smpte_tc_string2(tc, av_inv_q(c->sys->time_base), AV_RB32(tc_pack + 1), prevent_df, 1); + + /* 720p time code counts frame pairs (SMPTE 370M 3.1.4) */ + if (c->sys->height == 720) + rate.den *= 2; + + av_timecode_make_smpte_tc_string2(tc, rate, AV_RB32(tc_pack + 1), prevent_df, 1); return 1; } -- 2.54.0 From dc4081bc87381d5defcd6b8fa4d0b988eb7f70ab Mon Sep 17 00:00:00 2001 From: Baptiste Coudurier <[email protected]> Date: Thu, 12 Nov 2026 19:27:21 -0800 Subject: [PATCH 5/7] avformat/dv: fix audio timestamps of the second 720p frame of a pair Both frames of a pair carry the same audio frame on different DIF channels. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Baptiste Coudurier <[email protected]> --- libavformat/dv.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/libavformat/dv.c b/libavformat/dv.c index 435054c31e0..fc174d59a0e 100644 --- a/libavformat/dv.c +++ b/libavformat/dv.c @@ -455,13 +455,23 @@ int avpriv_dv_produce_packet(DVDemuxContext *c, AVPacket *pkt, (AVRational){ 1, c->audio_pkt[0].sample_rate }, c->ast[0]->time_base); - // if audio timestamps are more than one frame away from video, - // assume desync happened (e.g. due to dropped audio frames) and - // resynchronize - pts = (FFABS(next_pts_video - c->next_pts_audio) >= duration) ? - next_pts_video : c->next_pts_audio; + if (c->sys->height == 720 && !(buf[1] & 0x0C)) { + /* the second frame of a 720p pair carries the other DIF channels + * of the same audio frame (SMPTE 370M 3.1.4) */ + next_pts_video -= av_rescale_q(1, c->sys->time_base, c->ast[0]->time_base); + pts = next_pts_video; + if (c->next_pts_audio != AV_NOPTS_VALUE && + FFABS(next_pts_video - (c->next_pts_audio - duration)) < duration) + pts = c->next_pts_audio - duration; + } else { + // if audio timestamps are more than one frame away from video, + // assume desync happened (e.g. due to dropped audio frames) and + // resynchronize + pts = (FFABS(next_pts_video - c->next_pts_audio) >= duration) ? + next_pts_video : c->next_pts_audio; - c->next_pts_audio = pts + duration; + c->next_pts_audio = pts + duration; + } } for (i = 0; i < c->ach; i++) { -- 2.54.0 From 675a70a780723c6161992ea496161e1d9c4da905 Mon Sep 17 00:00:00 2001 From: Baptiste Coudurier <[email protected]> Date: Thu, 12 Nov 2026 19:27:43 -0800 Subject: [PATCH 6/7] avformat/dvenc: write the AAUX and subcode packs per the DV standards Follow SMPTE 314M/370M when the track application ID is set, and IEC 61834 otherwise; fix the locked mode, emphasis, source situation and week day fields, and replace the 0x8000 audio error code. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Baptiste Coudurier <[email protected]> --- libavcodec/dv.h | 1 + libavformat/dvenc.c | 121 +++++++++++++++++----------- tests/ref/lavf/dv | 2 +- tests/ref/lavf/dv_ntsc | 2 +- tests/ref/lavf/dv_pal | 2 +- tests/ref/vsynth/vsynth1-dv | 2 +- tests/ref/vsynth/vsynth1-dv-411 | 2 +- tests/ref/vsynth/vsynth1-dv-50 | 2 +- tests/ref/vsynth/vsynth1-dv-fhd | 2 +- tests/ref/vsynth/vsynth1-dv-hd | 2 +- tests/ref/vsynth/vsynth2-dv | 2 +- tests/ref/vsynth/vsynth2-dv-411 | 2 +- tests/ref/vsynth/vsynth2-dv-50 | 2 +- tests/ref/vsynth/vsynth2-dv-fhd | 2 +- tests/ref/vsynth/vsynth2-dv-hd | 2 +- tests/ref/vsynth/vsynth3-dv-fhd | 2 +- tests/ref/vsynth/vsynth3-dv-hd | 2 +- tests/ref/vsynth/vsynth_lena-dv | 2 +- tests/ref/vsynth/vsynth_lena-dv-411 | 2 +- tests/ref/vsynth/vsynth_lena-dv-50 | 2 +- tests/ref/vsynth/vsynth_lena-dv-fhd | 2 +- tests/ref/vsynth/vsynth_lena-dv-hd | 2 +- 22 files changed, 94 insertions(+), 68 deletions(-) diff --git a/libavcodec/dv.h b/libavcodec/dv.h index df1cd0f6e26..d57f166d331 100644 --- a/libavcodec/dv.h +++ b/libavcodec/dv.h @@ -41,6 +41,7 @@ enum DVPackType { DV_HEADER525 = 0x3f, /* see dv_write_pack for important details on */ DV_HEADER625 = 0xbf, /* these two packs */ DV_TIMECODE = 0x13, + DV_BINARY_GROUP = 0x14, DV_AUDIO_SOURCE = 0x50, DV_AUDIO_CONTROL = 0x51, DV_AUDIO_RECDATE = 0x52, diff --git a/libavformat/dvenc.c b/libavformat/dvenc.c index 773c05d3949..890644bbae1 100644 --- a/libavformat/dvenc.c +++ b/libavformat/dvenc.c @@ -51,6 +51,7 @@ typedef struct DVMuxContext { AVStream *ast[4]; /* stereo audio streams */ AVFifo *audio_data[4]; /* FIFO for storing excessive amounts of PCM */ int frames; /* current frame number */ + int smpte; /* SMPTE 314M/370M (APT != 0) or IEC 61834 data layout */ int64_t start_time; /* recording start time */ int has_audio; /* frame under construction has audio */ int has_video; /* frame under construction has video */ @@ -58,19 +59,29 @@ typedef struct DVMuxContext { AVTimecode tc; /* timecode context */ } DVMuxContext; -static const int dv_aaux_packs_dist[12][9] = { - { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff }, - { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff }, - { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff }, - { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff }, - { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff }, - { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff }, - { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff }, - { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff }, - { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff }, - { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff }, - { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff }, - { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff }, +/* AAUX packs, IEC 61834 main area: source, source control, rec date, rec time */ +static const uint8_t dv_aaux_packs_iec[2][9] = { + { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff }, /* even DIF sequence */ + { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff }, /* odd DIF sequence */ +}; + +/* AAUX packs, SMPTE 314M Table 15 and 370M Table 18, the others are reserved */ +static const uint8_t dv_aaux_packs_smpte[2][9] = { + { 0xff, 0xff, 0xff, 0x50, 0x51, 0xff, 0xff, 0xff, 0xff }, + { 0x50, 0x51, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, +}; + +/* Subcode packs per SSYB number */ +static const uint8_t dv_ssyb_packs_iec[2][12] = { + { 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13 }, /* first half */ + { 0x13, 0x62, 0x63, 0x13, 0x62, 0x63, 0x13, 0x62, 0x63, 0x13, 0x62, 0x63 }, /* second half */ +}; + +/* SMPTE 314M Table 9 and 370M Table 10, plus a time code pack in the + * reserved SSYB 0 and 6, the only ones Apple and older readers look at */ +static const uint8_t dv_ssyb_packs_smpte[2][12] = { + { 0x13, 0xff, 0xff, 0x13, 0x14, 0x13, 0x13, 0xff, 0xff, 0x13, 0x14, 0x13 }, + { 0x13, 0xff, 0xff, 0x13, 0xff, 0xff, 0x13, 0xff, 0xff, 0x13, 0xff, 0xff }, }; static void brktimegm(time_t secs, struct tm *tm) @@ -83,7 +94,7 @@ static void brktimegm(time_t secs, struct tm *tm) static int dv_audio_frame_size(const AVDVProfile* sys, int frame, int sample_rate) { - if ((sys->time_base.den == 25 || sys->time_base.den == 50) && sys->time_base.num == 1) { + if (sys->dsf) { if (sample_rate == 32000) return 1280; else if (sample_rate == 44100) return 1764; else return 1920; @@ -106,45 +117,55 @@ static int dv_write_pack(enum DVPackType pack_id, DVMuxContext *c, uint8_t* buf, switch (pack_id) { case DV_TIMECODE: timecode = av_timecode_get_smpte_from_framenum(&c->tc, c->frames); - timecode |= 1<<23 | 1<<15 | 1<<7 | 1<<6; // biphase and binary group flags + if (!c->smpte) /* IEC 61834-4 title time code: BF and reserved bits */ + timecode |= 1U<<31 | 1<<30 | 1<<23 | 1<<15 | 1<<7 | 1<<6; AV_WB32(buf + 1, timecode); break; + case DV_BINARY_GROUP: + AV_WB32(buf + 1, 0); + break; case DV_AUDIO_SOURCE: /* AAUX source pack */ if (c->ast[channel]->codecpar->sample_rate == 44100) { audio_type = 1; } else if (c->ast[channel]->codecpar->sample_rate == 32000) audio_type = 2; - buf[1] = (1 << 7) | /* locked mode -- SMPTE only supports locked mode */ + buf[1] = (0 << 7) | /* LF: 0 -- locked mode */ (1 << 6) | /* reserved -- always 1 */ (dv_audio_frame_size(c->sys, c->frames, c->ast[channel]->codecpar->sample_rate) - c->sys->audio_min_samples[audio_type]); /* # of samples */ buf[2] = (0 << 7) | /* multi-stereo */ (0 << 5) | /* #of audio channels per block: 0 -- 1 channel */ - (0 << 4) | /* pair bit: 0 -- one pair of channels */ + (c->smpte << 4) | /* pair bit: 0 -- one pair of channels, reserved in SMPTE */ (seq >= c->sys->difseg_size/2); /* audio mode (1st or 2nd channel) */ buf[3] = (1 << 7) | /* res */ (1 << 6) | /* multi-language flag */ (c->sys->dsf << 5) | /* system: 60fields/50fields */ (DV_PROFILE_IS_HD(c->sys) ? 0x3 : c->sys->video_stype ? 2 : 0); /* stype */ buf[4] = (1 << 7) | /* emphasis: 1 -- off */ - (0 << 6) | /* emphasis time constant: 0 -- reserved */ + (1 << 6) | /* emphasis time constant: 1 -- 50/15us */ (audio_type << 3) | /* frequency: 0 -- 48kHz, 1 -- 44,1kHz, 2 -- 32kHz */ 0; /* quantization: 0 -- 16-bit linear, 1 -- 12-bit nonlinear */ break; case DV_AUDIO_CONTROL: - buf[1] = (0 << 6) | /* copy protection: 0 -- unrestricted */ - (1 << 4) | /* input source: 1 -- digital input */ - (3 << 2) | /* compression: 3 -- no information */ - 0; /* misc. info/SMPTE emphasis off */ + if (c->smpte) { + buf[1] = (0 << 6) | /* copy protection: 0 -- unrestricted */ + 0x3c | /* reserved -- always 1 */ + 0; /* EFC: 0 -- emphasis off */ + } else { + buf[1] = (0 << 6) | /* copy protection: 0 -- unrestricted */ + (1 << 4) | /* input source: 1 -- digital input */ + (3 << 2) | /* compression: 3 -- no information */ + 3; /* source situation: 3 -- no information */ + } buf[2] = (1 << 7) | /* recording start point: 1 -- no */ (1 << 6) | /* recording end point: 1 -- no */ (1 << 3) | /* recording mode: 1 -- original */ 7; buf[3] = (1 << 7) | /* direction: 1 -- forward */ - (c->sys->pix_fmt == AV_PIX_FMT_YUV420P ? 0x20 : /* speed */ - c->sys->ltc_divisor * 4); + (!c->smpte ? 0x20 : /* speed: normal */ + c->sys->dsf ? 100 : 120); /* speed: 100/100 or 120/120 */ buf[4] = (1 << 7) | /* reserved -- always 1 */ 0x7f; /* genre category */ break; @@ -158,7 +179,7 @@ static int dv_write_pack(enum DVPackType pack_id, DVMuxContext *c, uint8_t* buf, buf[2] = (3 << 6) | /* reserved -- always 1 */ ((tc.tm_mday / 10) << 4) | /* Tens of day */ (tc.tm_mday % 10); /* Units of day */ - buf[3] = /* we set high 4 bits to 0, shouldn't we set them to week? */ + buf[3] = (tc.tm_wday << 5) | /* Day of week */ ((tc.tm_mon / 10) << 4) | /* Tens of month */ (tc.tm_mon % 10); /* Units of month */ buf[4] = (((tc.tm_year % 100) / 10) << 4) | /* Tens of year */ @@ -189,13 +210,14 @@ static int dv_write_pack(enum DVPackType pack_id, DVMuxContext *c, uint8_t* buf, static void dv_inject_audio(DVMuxContext *c, int channel, uint8_t* frame_ptr) { + const uint8_t (*packs)[9] = c->smpte ? dv_aaux_packs_smpte : dv_aaux_packs_iec; int i, j, d, of, size; size = 4 * dv_audio_frame_size(c->sys, c->frames, c->ast[channel]->codecpar->sample_rate); frame_ptr += channel * c->sys->difseg_size * 150 * 80; for (i = 0; i < c->sys->difseg_size; i++) { frame_ptr += 6 * 80; /* skip DIF segment header */ for (j = 0; j < 9; j++) { - dv_write_pack(dv_aaux_packs_dist[i][j], c, &frame_ptr[3], channel, i); + dv_write_pack(packs[i & 1][j], c, &frame_ptr[3], channel, i); for (d = 8; d < 80; d+=2) { of = c->sys->audio_shuffle[i][j] + (d - 8)/2 * c->sys->audio_stride; if (of*2 >= size) @@ -204,6 +226,9 @@ static void dv_inject_audio(DVMuxContext *c, int channel, uint8_t* frame_ptr) // FIXME: maybe we have to admit that DV is a big-endian PCM av_fifo_peek(c->audio_data[channel], frame_ptr + d, 2, of * 2); FFSWAP(uint8_t, frame_ptr[d], frame_ptr[d + 1]); + /* 0x8000 is the audio error code */ + if (frame_ptr[d] == 0x80 && !frame_ptr[d + 1]) + frame_ptr[d + 1] = 1; } frame_ptr += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */ } @@ -212,30 +237,28 @@ static void dv_inject_audio(DVMuxContext *c, int channel, uint8_t* frame_ptr) static void dv_inject_metadata(DVMuxContext *c, uint8_t* frame) { + const uint8_t (*ssyb_packs)[12] = c->smpte ? dv_ssyb_packs_smpte : dv_ssyb_packs_iec; int j, k; uint8_t* buf; - int seq = 0; + int seq; - for (buf = frame; buf < frame + c->sys->frame_size; buf += 150 * 80, seq++) { - /* DV subcode: 2nd and 3d DIFs */ - for (j = 80; j < 80 * 3; j += 80) { - for (k = 6; k < 6 * 8; k += 8) - dv_write_pack(DV_TIMECODE, c, &buf[j+k], 0, seq); + for (buf = frame; buf < frame + c->sys->frame_size; buf += 150 * 80) { + int second_half; - if (((long)(buf-frame)/(c->sys->frame_size/(c->sys->difseg_size*c->sys->n_difchan))%c->sys->difseg_size) > 5) { /* FIXME: is this really needed ? */ - dv_write_pack(DV_VIDEO_RECDATE, c, &buf[j+14], 0, seq); - dv_write_pack(DV_VIDEO_RECTIME, c, &buf[j+22], 0, seq); - dv_write_pack(DV_VIDEO_RECDATE, c, &buf[j+38], 0, seq); - dv_write_pack(DV_VIDEO_RECTIME, c, &buf[j+46], 0, seq); - } - } + seq = (buf[1] >> 4) % c->sys->difseg_size; + second_half = seq >= c->sys->difseg_size / 2; - /* DV VAUX: 4th, 5th and 6th 3DIFs */ - for (j = 80*3 + 3; j < 80*6; j += 80) { - dv_write_pack(DV_VIDEO_RECDATE, c, &buf[j+5* 2], 0, seq); - dv_write_pack(DV_VIDEO_RECTIME, c, &buf[j+5* 3], 0, seq); - dv_write_pack(DV_VIDEO_RECDATE, c, &buf[j+5*11], 0, seq); - dv_write_pack(DV_VIDEO_RECTIME, c, &buf[j+5*12], 0, seq); + /* DV subcode: 2nd and 3d DIFs, SSYB 0-5 and 6-11 */ + for (j = 0; j < 2; j++) + for (k = 0; k < 6; k++) + dv_write_pack(ssyb_packs[second_half][j * 6 + k], c, + &buf[80 * (j + 1) + 3 + 8 * k + 3], 0, seq); + + /* DV VAUX: IEC 61834 main area, after the VS and VSC packs */ + if (!c->smpte) { + uint8_t *p = seq & 1 ? &buf[80 * 3 + 3 + 5 * 2] : &buf[80 * 5 + 3 + 5 * 11]; + dv_write_pack(DV_VIDEO_RECDATE, c, p, 0, seq); + dv_write_pack(DV_VIDEO_RECTIME, c, p + 5, 0, seq); } } } @@ -290,6 +313,7 @@ static int dv_assemble_frame(AVFormatContext *s, /* Let us see if we have enough data to construct one DV frame. */ if (c->has_video == 1 && c->has_audio + 1 == 1 << c->n_ast) { + c->smpte = (*frame)[4] & 0x07; /* APT */ dv_inject_metadata(c, *frame); c->has_audio = 0; for (i=0; i < c->n_ast; i++) { @@ -391,9 +415,10 @@ static int dv_init_mux(AVFormatContext* s) return AVERROR_INVALIDDATA; } - if ((c->sys->time_base.den != 25 && c->sys->time_base.den != 50) || c->sys->time_base.num != 1) { - for (i = 0; i < 2; i++) { - if (c->ast[i] && c->ast[i]->codecpar->sample_rate != 48000) { + /* SMPTE 314M and 370M, and IEC 61834 for 525/60, only allow 48kHz */ + if (!c->sys->dsf || c->sys->pix_fmt != AV_PIX_FMT_YUV420P) { + for (i = 0; i < c->n_ast; i++) { + if (c->ast[i]->codecpar->sample_rate != 48000) { av_log(s, AV_LOG_ERROR, "Invalid sample rate %d for audio stream #%d for this video profile, must be 48000.\n", c->ast[i]->codecpar->sample_rate, i); diff --git a/tests/ref/lavf/dv b/tests/ref/lavf/dv index e99cc439fdb..2a169b8b2c7 100644 --- a/tests/ref/lavf/dv +++ b/tests/ref/lavf/dv @@ -1,3 +1,3 @@ -7a5a5ac11e81ecf4ed1e0b7a3a4dfe45 *tests/data/lavf/lavf.dv +8446a4c970bfc00c9e7f0a05d21f671c *tests/data/lavf/lavf.dv 3600000 tests/data/lavf/lavf.dv tests/data/lavf/lavf.dv CRC=0x3efa60d5 diff --git a/tests/ref/lavf/dv_ntsc b/tests/ref/lavf/dv_ntsc index e677ab53fe7..95c1724aeeb 100644 --- a/tests/ref/lavf/dv_ntsc +++ b/tests/ref/lavf/dv_ntsc @@ -1,3 +1,3 @@ -e68e9d6a3ae391f020930541dec65b98 *tests/data/lavf/lavf.dv_ntsc +3dec2c1df0acfa4b69d25d1153c53210 *tests/data/lavf/lavf.dv_ntsc 3480000 tests/data/lavf/lavf.dv_ntsc tests/data/lavf/lavf.dv_ntsc CRC=0xadfc9598 diff --git a/tests/ref/lavf/dv_pal b/tests/ref/lavf/dv_pal index 3c2e8cc29f9..1a0a2e342a7 100644 --- a/tests/ref/lavf/dv_pal +++ b/tests/ref/lavf/dv_pal @@ -1,3 +1,3 @@ -64e61fe4bf46434ff42d2e527d60bfdf *tests/data/lavf/lavf.dv_pal +aae522a40597ad549a54bec855779ff8 *tests/data/lavf/lavf.dv_pal 3600000 tests/data/lavf/lavf.dv_pal tests/data/lavf/lavf.dv_pal CRC=0x5573b571 diff --git a/tests/ref/vsynth/vsynth1-dv b/tests/ref/vsynth/vsynth1-dv index ffbb6c9dea3..e026dadc528 100644 --- a/tests/ref/vsynth/vsynth1-dv +++ b/tests/ref/vsynth/vsynth1-dv @@ -1,4 +1,4 @@ -b1667f501827fc842c34b5524b503c5d *tests/data/fate/vsynth1-dv.dv +5288f6819c4d9fc23aa243ff02848821 *tests/data/fate/vsynth1-dv.dv 7200000 tests/data/fate/vsynth1-dv.dv 7dcf06a0956c25c407c72140cdcfa078 *tests/data/fate/vsynth1-dv.out.rawvideo stddev: 6.90 PSNR: 31.34 MAXDIFF: 76 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth/vsynth1-dv-411 b/tests/ref/vsynth/vsynth1-dv-411 index 2cfbc3cddf8..05c03d39e6a 100644 --- a/tests/ref/vsynth/vsynth1-dv-411 +++ b/tests/ref/vsynth/vsynth1-dv-411 @@ -1,4 +1,4 @@ -46193233ea9200c9ba3c9ad93a77d7bd *tests/data/fate/vsynth1-dv-411.dv +c7bc89368b09f4f916b36a55a6589d8a *tests/data/fate/vsynth1-dv-411.dv 7200000 tests/data/fate/vsynth1-dv-411.dv 4e294ecbb5ad48ce776fea99ef35d89e *tests/data/fate/vsynth1-dv-411.out.rawvideo stddev: 9.43 PSNR: 28.63 MAXDIFF: 84 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth/vsynth1-dv-50 b/tests/ref/vsynth/vsynth1-dv-50 index 0e8adc31f6f..0c92fe73e2b 100644 --- a/tests/ref/vsynth/vsynth1-dv-50 +++ b/tests/ref/vsynth/vsynth1-dv-50 @@ -1,4 +1,4 @@ -8951d46f08e193168bca07fa1102ce56 *tests/data/fate/vsynth1-dv-50.dv +83a1f319b392c9a16d374dc431c8cca5 *tests/data/fate/vsynth1-dv-50.dv 14400000 tests/data/fate/vsynth1-dv-50.dv 1e9aeeb0ff36ccd83017cad460384bc3 *tests/data/fate/vsynth1-dv-50.out.rawvideo stddev: 1.72 PSNR: 43.37 MAXDIFF: 29 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth/vsynth1-dv-fhd b/tests/ref/vsynth/vsynth1-dv-fhd index d0ae2823b21..ac077c3a521 100644 --- a/tests/ref/vsynth/vsynth1-dv-fhd +++ b/tests/ref/vsynth/vsynth1-dv-fhd @@ -1,4 +1,4 @@ -cdbab155c27a667e8589b7648a2aae63 *tests/data/fate/vsynth1-dv-fhd.dv +ea39111791bca417b95b40441ec2ca6c *tests/data/fate/vsynth1-dv-fhd.dv 28800000 tests/data/fate/vsynth1-dv-fhd.dv af6efc55053a9b5407dfd0e7722deb90 *tests/data/fate/vsynth1-dv-fhd.out.rawvideo stddev: 2.54 PSNR: 40.02 MAXDIFF: 35 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth/vsynth1-dv-hd b/tests/ref/vsynth/vsynth1-dv-hd index 237cc29197b..df9711e421a 100644 --- a/tests/ref/vsynth/vsynth1-dv-hd +++ b/tests/ref/vsynth/vsynth1-dv-hd @@ -1,4 +1,4 @@ -d32d7bc353465c976ce26c194bdb5481 *tests/data/fate/vsynth1-dv-hd.dv +6403497b3a293e4d764d12fe60a8d3e5 *tests/data/fate/vsynth1-dv-hd.dv 14400000 tests/data/fate/vsynth1-dv-hd.dv 20e3ea2d0341a0b323b8fdc223554619 *tests/data/fate/vsynth1-dv-hd.out.rawvideo stddev: 4.28 PSNR: 35.50 MAXDIFF: 74 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth/vsynth2-dv b/tests/ref/vsynth/vsynth2-dv index 99abeae2d48..fa83ef16b16 100644 --- a/tests/ref/vsynth/vsynth2-dv +++ b/tests/ref/vsynth/vsynth2-dv @@ -1,4 +1,4 @@ -ba042068d0a47d3cc7f83180d4890a0a *tests/data/fate/vsynth2-dv.dv +68227ab2f4191a82c80815989e60af68 *tests/data/fate/vsynth2-dv.dv 7200000 tests/data/fate/vsynth2-dv.dv f16c6dc6fcf7e2de82460e10985e5d10 *tests/data/fate/vsynth2-dv.out.rawvideo stddev: 1.99 PSNR: 42.13 MAXDIFF: 38 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth/vsynth2-dv-411 b/tests/ref/vsynth/vsynth2-dv-411 index fb88fb794c2..a740d88f762 100644 --- a/tests/ref/vsynth/vsynth2-dv-411 +++ b/tests/ref/vsynth/vsynth2-dv-411 @@ -1,4 +1,4 @@ -1e1bee4489cfba04b565dbf0ec6c9d90 *tests/data/fate/vsynth2-dv-411.dv +4c6d11673c5173025a979356d40ed62a *tests/data/fate/vsynth2-dv-411.dv 7200000 tests/data/fate/vsynth2-dv-411.dv 4c73684b84cdab58e99752c9d1e60b6d *tests/data/fate/vsynth2-dv-411.out.rawvideo stddev: 3.48 PSNR: 37.28 MAXDIFF: 56 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth/vsynth2-dv-50 b/tests/ref/vsynth/vsynth2-dv-50 index cdcc06217b5..c724616c469 100644 --- a/tests/ref/vsynth/vsynth2-dv-50 +++ b/tests/ref/vsynth/vsynth2-dv-50 @@ -1,4 +1,4 @@ -0e25653fe86a228a893e474fb04d4610 *tests/data/fate/vsynth2-dv-50.dv +d8fbad2393be66f87b74e78c24dc2a96 *tests/data/fate/vsynth2-dv-50.dv 14400000 tests/data/fate/vsynth2-dv-50.dv ac8fa753f86a1ad0422e7e1bc1334433 *tests/data/fate/vsynth2-dv-50.out.rawvideo stddev: 0.86 PSNR: 49.43 MAXDIFF: 17 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth/vsynth2-dv-fhd b/tests/ref/vsynth/vsynth2-dv-fhd index 39a04b74dd3..09038ac0f57 100644 --- a/tests/ref/vsynth/vsynth2-dv-fhd +++ b/tests/ref/vsynth/vsynth2-dv-fhd @@ -1,4 +1,4 @@ -86bdeda24e755548d6965a96b801d430 *tests/data/fate/vsynth2-dv-fhd.dv +4e38ef1e4b869d8fe4d4d4ebaef05a46 *tests/data/fate/vsynth2-dv-fhd.dv 28800000 tests/data/fate/vsynth2-dv-fhd.dv 247449892bb8dbc5be8147c670981c66 *tests/data/fate/vsynth2-dv-fhd.out.rawvideo stddev: 1.16 PSNR: 46.83 MAXDIFF: 21 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth/vsynth2-dv-hd b/tests/ref/vsynth/vsynth2-dv-hd index 37c5ea438de..70242ef60b3 100644 --- a/tests/ref/vsynth/vsynth2-dv-hd +++ b/tests/ref/vsynth/vsynth2-dv-hd @@ -1,4 +1,4 @@ -96deba58a874daa8bc49442bdc3ad823 *tests/data/fate/vsynth2-dv-hd.dv +0484c6ed14d5b6e72b08a01410c260d8 *tests/data/fate/vsynth2-dv-hd.dv 14400000 tests/data/fate/vsynth2-dv-hd.dv fdf957d1e9d70a5e832dbcdfd2d5a27b *tests/data/fate/vsynth2-dv-hd.out.rawvideo stddev: 1.74 PSNR: 43.28 MAXDIFF: 34 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth/vsynth3-dv-fhd b/tests/ref/vsynth/vsynth3-dv-fhd index bcd8dcab105..7a165e161ff 100644 --- a/tests/ref/vsynth/vsynth3-dv-fhd +++ b/tests/ref/vsynth/vsynth3-dv-fhd @@ -1,4 +1,4 @@ -c6bdeb2e94215fcdca6c067ef6551a91 *tests/data/fate/vsynth3-dv-fhd.dv +bbcbb3047f134cbc94602520ad20b568 *tests/data/fate/vsynth3-dv-fhd.dv 28800000 tests/data/fate/vsynth3-dv-fhd.dv a038ad7c3c09f776304ef7accdea9c74 *tests/data/fate/vsynth3-dv-fhd.out.rawvideo stddev: 0.00 PSNR:999.99 MAXDIFF: 0 bytes: 86700/ 86700 diff --git a/tests/ref/vsynth/vsynth3-dv-hd b/tests/ref/vsynth/vsynth3-dv-hd index 0085d9894d1..1782c77cdc8 100644 --- a/tests/ref/vsynth/vsynth3-dv-hd +++ b/tests/ref/vsynth/vsynth3-dv-hd @@ -1,4 +1,4 @@ -cc8e9a707d546024c92f784afff2d7f7 *tests/data/fate/vsynth3-dv-hd.dv +6a4d041bc451a0a01298309dc6960ebb *tests/data/fate/vsynth3-dv-hd.dv 14400000 tests/data/fate/vsynth3-dv-hd.dv a038ad7c3c09f776304ef7accdea9c74 *tests/data/fate/vsynth3-dv-hd.out.rawvideo stddev: 0.00 PSNR:999.99 MAXDIFF: 0 bytes: 86700/ 86700 diff --git a/tests/ref/vsynth/vsynth_lena-dv b/tests/ref/vsynth/vsynth_lena-dv index 1906fa915a2..bce6e905c01 100644 --- a/tests/ref/vsynth/vsynth_lena-dv +++ b/tests/ref/vsynth/vsynth_lena-dv @@ -1,4 +1,4 @@ -7df3306c387bcef22843838889975127 *tests/data/fate/vsynth_lena-dv.dv +8f14038887a8b3c4deec4b66aeee517b *tests/data/fate/vsynth_lena-dv.dv 7200000 tests/data/fate/vsynth_lena-dv.dv 0c7c574084780daf0e9732cf7a8a8fa5 *tests/data/fate/vsynth_lena-dv.out.rawvideo stddev: 1.69 PSNR: 43.54 MAXDIFF: 33 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth/vsynth_lena-dv-411 b/tests/ref/vsynth/vsynth_lena-dv-411 index 8dea366cb23..f40faf78536 100644 --- a/tests/ref/vsynth/vsynth_lena-dv-411 +++ b/tests/ref/vsynth/vsynth_lena-dv-411 @@ -1,4 +1,4 @@ -25ebf952fd588426583a456637462be8 *tests/data/fate/vsynth_lena-dv-411.dv +833e57dd0b135992010633694b690b82 *tests/data/fate/vsynth_lena-dv-411.dv 7200000 tests/data/fate/vsynth_lena-dv-411.dv a2e4189e19c8b1d1f68c8a75db14041a *tests/data/fate/vsynth_lena-dv-411.out.rawvideo stddev: 2.87 PSNR: 38.96 MAXDIFF: 45 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth/vsynth_lena-dv-50 b/tests/ref/vsynth/vsynth_lena-dv-50 index 95c700f167b..56c8f09b421 100644 --- a/tests/ref/vsynth/vsynth_lena-dv-50 +++ b/tests/ref/vsynth/vsynth_lena-dv-50 @@ -1,4 +1,4 @@ -1b8b837c2441437f562c55341b5266c3 *tests/data/fate/vsynth_lena-dv-50.dv +80b28aecb005442c6d5c50bd69bb5b80 *tests/data/fate/vsynth_lena-dv-50.dv 14400000 tests/data/fate/vsynth_lena-dv-50.dv 9313f04ef89da88fbb7157930f3f418e *tests/data/fate/vsynth_lena-dv-50.out.rawvideo stddev: 0.78 PSNR: 50.28 MAXDIFF: 12 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth/vsynth_lena-dv-fhd b/tests/ref/vsynth/vsynth_lena-dv-fhd index 23e9451f548..b51e95de596 100644 --- a/tests/ref/vsynth/vsynth_lena-dv-fhd +++ b/tests/ref/vsynth/vsynth_lena-dv-fhd @@ -1,4 +1,4 @@ -c2d7b838a7b62070e0572d006bd3d53a *tests/data/fate/vsynth_lena-dv-fhd.dv +abe6e6976547a0ea0f7d985286efebe2 *tests/data/fate/vsynth_lena-dv-fhd.dv 28800000 tests/data/fate/vsynth_lena-dv-fhd.dv fba140abe1d417f829c3bf20657d220d *tests/data/fate/vsynth_lena-dv-fhd.out.rawvideo stddev: 1.03 PSNR: 47.80 MAXDIFF: 14 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth/vsynth_lena-dv-hd b/tests/ref/vsynth/vsynth_lena-dv-hd index 4920484c71a..1a001708925 100644 --- a/tests/ref/vsynth/vsynth_lena-dv-hd +++ b/tests/ref/vsynth/vsynth_lena-dv-hd @@ -1,4 +1,4 @@ -953683604045a1dcb6ffb2fdd477f4c5 *tests/data/fate/vsynth_lena-dv-hd.dv +ae4470887ab21c71fadf9023f3361337 *tests/data/fate/vsynth_lena-dv-hd.dv 14400000 tests/data/fate/vsynth_lena-dv-hd.dv 85896fd67284fab17ff60ea04eb2fa9e *tests/data/fate/vsynth_lena-dv-hd.out.rawvideo stddev: 1.48 PSNR: 44.68 MAXDIFF: 26 bytes: 7603200/ 7603200 -- 2.54.0 From cff867536f64e985b35bb21a86d7858866c64c9b Mon Sep 17 00:00:00 2001 From: Baptiste Coudurier <[email protected]> Date: Thu, 12 Nov 2026 19:28:07 -0800 Subject: [PATCH 7/7] avformat/dvenc: mux 720p audio and time code over frame pairs SMPTE 370M carries one audio frame and time code value over two 720p frames, on DIF channels 0-1 then 2-3. Allow one stereo pair per DIF channel. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Baptiste Coudurier <[email protected]> --- libavformat/dvenc.c | 146 +++++++++++++++-------------- tests/ref/vsynth/vsynth1-dv-hd | 2 +- tests/ref/vsynth/vsynth2-dv-hd | 2 +- tests/ref/vsynth/vsynth3-dv-hd | 2 +- tests/ref/vsynth/vsynth_lena-dv-hd | 2 +- 5 files changed, 82 insertions(+), 72 deletions(-) diff --git a/libavformat/dvenc.c b/libavformat/dvenc.c index 890644bbae1..86ae0795273 100644 --- a/libavformat/dvenc.c +++ b/libavformat/dvenc.c @@ -47,15 +47,16 @@ typedef struct DVMuxContext { AVClass *av_class; const AVDVProfile* sys; /* current DV profile, e.g.: 525/60, 625/50 */ - int n_ast; /* number of stereo audio streams (up to 2) */ - AVStream *ast[4]; /* stereo audio streams */ + int n_ast; /* number of stereo audio streams (up to 4) */ + AVStream *ast[4]; /* stereo audio streams, one per DIF channel */ AVFifo *audio_data[4]; /* FIFO for storing excessive amounts of PCM */ - int frames; /* current frame number */ + int frames; /* number of DV frames written */ + int afn; /* audio and time code frame number, 720p frames + are processed in pairs (SMPTE 370M 3.1.4) */ int smpte; /* SMPTE 314M/370M (APT != 0) or IEC 61834 data layout */ int64_t start_time; /* recording start time */ - int has_audio; /* frame under construction has audio */ - int has_video; /* frame under construction has video */ - uint8_t frame_buf[DV_MAX_FRAME_SIZE]; /* frame under construction */ + int has_video; /* number of queued video frames */ + uint8_t frame_buf[DV_MAX_FRAME_SIZE]; /* queued video frames */ AVTimecode tc; /* timecode context */ } DVMuxContext; @@ -106,6 +107,12 @@ static int dv_audio_frame_size(const AVDVProfile* sys, int frame, int sample_rat sizeof(sys->audio_samples_dist[0]))]; } +/* first DIF channel of a frame, 720p frames carry DIF channels 0,1 or 2,3 */ +static int dv_first_chan(const AVDVProfile *sys, const uint8_t *frame) +{ + return sys->height == 720 && !(frame[1] & 0x04) ? 2 : 0; +} + static int dv_write_pack(enum DVPackType pack_id, DVMuxContext *c, uint8_t* buf, int channel, int seq) { struct tm tc; @@ -116,7 +123,7 @@ static int dv_write_pack(enum DVPackType pack_id, DVMuxContext *c, uint8_t* buf, buf[0] = (uint8_t)pack_id; switch (pack_id) { case DV_TIMECODE: - timecode = av_timecode_get_smpte_from_framenum(&c->tc, c->frames); + timecode = av_timecode_get_smpte_from_framenum(&c->tc, c->afn); if (!c->smpte) /* IEC 61834-4 title time code: BF and reserved bits */ timecode |= 1U<<31 | 1<<30 | 1<<23 | 1<<15 | 1<<7 | 1<<6; AV_WB32(buf + 1, timecode); @@ -131,7 +138,7 @@ static int dv_write_pack(enum DVPackType pack_id, DVMuxContext *c, uint8_t* buf, audio_type = 2; buf[1] = (0 << 7) | /* LF: 0 -- locked mode */ (1 << 6) | /* reserved -- always 1 */ - (dv_audio_frame_size(c->sys, c->frames, c->ast[channel]->codecpar->sample_rate) - + (dv_audio_frame_size(c->sys, c->afn, c->ast[channel]->codecpar->sample_rate) - c->sys->audio_min_samples[audio_type]); /* # of samples */ buf[2] = (0 << 7) | /* multi-stereo */ @@ -208,12 +215,12 @@ static int dv_write_pack(enum DVPackType pack_id, DVMuxContext *c, uint8_t* buf, return 5; } -static void dv_inject_audio(DVMuxContext *c, int channel, uint8_t* frame_ptr) +static void dv_inject_audio(DVMuxContext *c, int channel, int difchan, uint8_t* frame_ptr) { const uint8_t (*packs)[9] = c->smpte ? dv_aaux_packs_smpte : dv_aaux_packs_iec; int i, j, d, of, size; - size = 4 * dv_audio_frame_size(c->sys, c->frames, c->ast[channel]->codecpar->sample_rate); - frame_ptr += channel * c->sys->difseg_size * 150 * 80; + size = 4 * dv_audio_frame_size(c->sys, c->afn, c->ast[channel]->codecpar->sample_rate); + frame_ptr += difchan * c->sys->difseg_size * 150 * 80; for (i = 0; i < c->sys->difseg_size; i++) { frame_ptr += 6 * 80; /* skip DIF segment header */ for (j = 0; j < 9; j++) { @@ -267,18 +274,52 @@ static void dv_inject_metadata(DVMuxContext *c, uint8_t* frame) * The following 3 functions constitute our interface to the world */ -static int dv_assemble_frame(AVFormatContext *s, - DVMuxContext *c, AVStream* st, - const uint8_t *data, int data_size, uint8_t **frame) +/* Check that the audio for the DIF channels of a frame is available. */ +static int dv_frame_ready(DVMuxContext *c, const uint8_t *frame) { - int i, reqasize; + int first_chan = dv_first_chan(c->sys, frame); - *frame = &c->frame_buf[0]; + for (int chan = 0; chan < c->sys->n_difchan && first_chan + chan < c->n_ast; chan++) { + int i = first_chan + chan; + int reqasize = 4 * dv_audio_frame_size(c->sys, c->afn, c->ast[i]->codecpar->sample_rate); + if (av_fifo_can_read(c->audio_data[i]) < reqasize) + return 0; + } + return 1; +} + +static void dv_write_frame(AVFormatContext *s, DVMuxContext *c, uint8_t *frame) +{ + int first_chan = dv_first_chan(c->sys, frame); + + c->smpte = frame[4] & 0x07; /* APT */ + dv_inject_metadata(c, frame); + for (int chan = 0; chan < c->sys->n_difchan && first_chan + chan < c->n_ast; chan++) { + int i = first_chan + chan; + int reqasize = 4 * dv_audio_frame_size(c->sys, c->afn, c->ast[i]->codecpar->sample_rate); + dv_inject_audio(c, i, chan, frame); + av_fifo_drain2(c->audio_data[i], reqasize); + } + + avio_write(s->pb, frame, c->sys->frame_size); + + c->frames++; + if (c->sys->height != 720 || first_chan) + c->afn++; +} + +static int dv_assemble_frame(AVFormatContext *s, DVMuxContext *c, AVStream* st, + const uint8_t *data, int data_size) +{ + /* 720p audio frames span two video frames, so allow one more to be + * queued while waiting for the audio */ + int max_video = c->sys->height == 720 ? 2 : 1; + int i; switch (st->codecpar->codec_type) { case AVMEDIA_TYPE_VIDEO: /* FIXME: we have to have more sensible approach than this one */ - if (c->has_video) { + if (c->has_video == max_video) { av_log(s, AV_LOG_ERROR, "Can't process DV frame #%d. Insufficient audio data or severe sync problem.\n", c->frames); return AVERROR(EINVAL); } @@ -288,8 +329,8 @@ static int dv_assemble_frame(AVFormatContext *s, return AVERROR(ENOSYS); } - memcpy(*frame, data, c->sys->frame_size); - c->has_video = 1; + memcpy(c->frame_buf + c->has_video * c->sys->frame_size, data, c->sys->frame_size); + c->has_video++; break; case AVMEDIA_TYPE_AUDIO: for (i = 0; i < c->n_ast && st != c->ast[i]; i++); @@ -300,34 +341,16 @@ static int dv_assemble_frame(AVFormatContext *s, return AVERROR(EINVAL); } av_fifo_write(c->audio_data[i], data, data_size); - - reqasize = 4 * dv_audio_frame_size(c->sys, c->frames, st->codecpar->sample_rate); - - /* Let us see if we've got enough audio for one DV frame. */ - c->has_audio |= ((reqasize <= av_fifo_can_read(c->audio_data[i])) << i); - break; default: break; } - /* Let us see if we have enough data to construct one DV frame. */ - if (c->has_video == 1 && c->has_audio + 1 == 1 << c->n_ast) { - c->smpte = (*frame)[4] & 0x07; /* APT */ - dv_inject_metadata(c, *frame); - c->has_audio = 0; - for (i=0; i < c->n_ast; i++) { - dv_inject_audio(c, i, *frame); - reqasize = 4 * dv_audio_frame_size(c->sys, c->frames, c->ast[i]->codecpar->sample_rate); - av_fifo_drain2(c->audio_data[i], reqasize); - c->has_audio |= ((reqasize <= av_fifo_can_read(c->audio_data[i])) << i); - } - - c->has_video = 0; - - c->frames++; - - return c->sys->frame_size; + /* Let us see if we have enough data to construct DV frames. */ + while (c->has_video && dv_frame_ready(c, c->frame_buf)) { + dv_write_frame(s, c, c->frame_buf); + if (--c->has_video) + memmove(c->frame_buf, c->frame_buf + c->sys->frame_size, c->sys->frame_size); } return 0; @@ -337,7 +360,7 @@ static int dv_init_mux(AVFormatContext* s) { DVMuxContext *c = s->priv_data; AVStream *vst = NULL; - int i; + int i, max_ast; if (s->nb_streams > 5) { av_log(s, AV_LOG_ERROR, @@ -364,11 +387,6 @@ static int dv_init_mux(AVFormatContext* s) vst = st; break; case AVMEDIA_TYPE_AUDIO: - if (c->n_ast > 1) { - av_log(s, AV_LOG_ERROR, - "More than two audio streams found, at most 2 are accepted.\n"); - return AVERROR_INVALIDDATA; - } /* Some checks -- DV format is very picky about its incoming streams */ if (st->codecpar->codec_id != AV_CODEC_ID_PCM_S16LE) { av_log(s, AV_LOG_ERROR, @@ -427,19 +445,18 @@ static int dv_init_mux(AVFormatContext* s) } } - for (i = 1; i < 2; i++) { - if (((c->n_ast > i) && (c->sys->n_difchan < (2 * i)))) { - const char *mode = c->n_ast > 1 ? "50Mps" : "25Mps"; - av_log(s, AV_LOG_ERROR, - "Invalid number of channels %d, only %d stereo pairs is allowed in %s mode.\n", - c->n_ast, i, mode); - return AVERROR_INVALIDDATA; - } + /* one stereo pair per DIF channel, 720p has 4 DIF channels split in 2 frames */ + max_ast = c->sys->height == 720 ? 4 : c->sys->n_difchan; + if (c->n_ast > max_ast) { + av_log(s, AV_LOG_ERROR, + "Invalid number of audio streams %d, only %d stereo pairs are allowed in this video profile.\n", + c->n_ast, max_ast); + return AVERROR_INVALIDDATA; } /* Ok, everything seems to be in working order */ c->frames = 0; - c->has_audio = 0; + c->afn = 0; c->has_video = 0; ff_parse_creation_time_metadata(s, &c->start_time, 1); @@ -468,7 +485,8 @@ static int dv_write_header(AVFormatContext *s) " (50Mbps allows an optional second audio stream)\n"); return AVERROR_INVALIDDATA; } - rate.num = dvc->sys->ltc_divisor; + /* 720p time code counts frame pairs (SMPTE 370M 3.1.4) */ + rate.num = dvc->sys->ltc_divisor / (dvc->sys->height == 720 ? 2 : 1); rate.den = 1; if (!tcr) { // no global timecode, look into the streams int i; @@ -485,16 +503,8 @@ static int dv_write_header(AVFormatContext *s) static int dv_write_packet(struct AVFormatContext *s, AVPacket *pkt) { - uint8_t* frame; - int fsize; - - fsize = dv_assemble_frame(s, s->priv_data, s->streams[pkt->stream_index], - pkt->data, pkt->size, &frame); - if (fsize < 0) { - return fsize; - } - avio_write(s->pb, frame, fsize); - return 0; + return dv_assemble_frame(s, s->priv_data, s->streams[pkt->stream_index], + pkt->data, pkt->size); } /* diff --git a/tests/ref/vsynth/vsynth1-dv-hd b/tests/ref/vsynth/vsynth1-dv-hd index df9711e421a..40a4dda9f37 100644 --- a/tests/ref/vsynth/vsynth1-dv-hd +++ b/tests/ref/vsynth/vsynth1-dv-hd @@ -1,4 +1,4 @@ -6403497b3a293e4d764d12fe60a8d3e5 *tests/data/fate/vsynth1-dv-hd.dv +f606d7ff21da947fb11001a5f8b94ed1 *tests/data/fate/vsynth1-dv-hd.dv 14400000 tests/data/fate/vsynth1-dv-hd.dv 20e3ea2d0341a0b323b8fdc223554619 *tests/data/fate/vsynth1-dv-hd.out.rawvideo stddev: 4.28 PSNR: 35.50 MAXDIFF: 74 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth/vsynth2-dv-hd b/tests/ref/vsynth/vsynth2-dv-hd index 70242ef60b3..ced1f1ae9af 100644 --- a/tests/ref/vsynth/vsynth2-dv-hd +++ b/tests/ref/vsynth/vsynth2-dv-hd @@ -1,4 +1,4 @@ -0484c6ed14d5b6e72b08a01410c260d8 *tests/data/fate/vsynth2-dv-hd.dv +c72cb9e991a0160392d6e995b087f848 *tests/data/fate/vsynth2-dv-hd.dv 14400000 tests/data/fate/vsynth2-dv-hd.dv fdf957d1e9d70a5e832dbcdfd2d5a27b *tests/data/fate/vsynth2-dv-hd.out.rawvideo stddev: 1.74 PSNR: 43.28 MAXDIFF: 34 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth/vsynth3-dv-hd b/tests/ref/vsynth/vsynth3-dv-hd index 1782c77cdc8..9947682c49b 100644 --- a/tests/ref/vsynth/vsynth3-dv-hd +++ b/tests/ref/vsynth/vsynth3-dv-hd @@ -1,4 +1,4 @@ -6a4d041bc451a0a01298309dc6960ebb *tests/data/fate/vsynth3-dv-hd.dv +5406d0ef906ed168930a9a8cfe7c7584 *tests/data/fate/vsynth3-dv-hd.dv 14400000 tests/data/fate/vsynth3-dv-hd.dv a038ad7c3c09f776304ef7accdea9c74 *tests/data/fate/vsynth3-dv-hd.out.rawvideo stddev: 0.00 PSNR:999.99 MAXDIFF: 0 bytes: 86700/ 86700 diff --git a/tests/ref/vsynth/vsynth_lena-dv-hd b/tests/ref/vsynth/vsynth_lena-dv-hd index 1a001708925..60daad55fe9 100644 --- a/tests/ref/vsynth/vsynth_lena-dv-hd +++ b/tests/ref/vsynth/vsynth_lena-dv-hd @@ -1,4 +1,4 @@ -ae4470887ab21c71fadf9023f3361337 *tests/data/fate/vsynth_lena-dv-hd.dv +cd0125ae466731ac62cc2b13d76f9409 *tests/data/fate/vsynth_lena-dv-hd.dv 14400000 tests/data/fate/vsynth_lena-dv-hd.dv 85896fd67284fab17ff60ea04eb2fa9e *tests/data/fate/vsynth_lena-dv-hd.out.rawvideo stddev: 1.48 PSNR: 44.68 MAXDIFF: 26 bytes: 7603200/ 7603200 -- 2.54.0 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
