ez-ffmpeg 0.13.1

A safe and ergonomic Rust interface for FFmpeg integration, designed for ease of use.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
use super::fg_bind::{ifilter_bind_ist, init_filter_graph};
use super::*;

/// Process metadata for output file
///
/// Mirrors FFmpeg's `copy_meta()` flow (ffmpeg_mux_init.c:2913-2983):
/// 1. Metadata mappings (`copy_metadata`)
/// 2. Chapter auto-copy (`copy_chapters`)
/// 3. Default auto-copy (`copy_metadata_default`)
/// 4. User-specified metadata (`of_add_metadata`)
unsafe fn process_metadata(mux: &Muxer, demuxs: &Vec<Demuxer>) -> Result<()> {
    use crate::core::metadata::MetadataType;
    use crate::core::metadata::{
        copy_chapters_from_input, copy_metadata, copy_metadata_default, of_add_metadata,
    };

    // Collect input format contexts for metadata copying
    let input_ctxs: Vec<*const AVFormatContext> = demuxs
        .iter()
        .map(|d| d.in_fmt_ctx_ptr() as *const AVFormatContext)
        .collect();

    let mut metadata_global_manual = false;
    let mut metadata_streams_manual = false;
    let mut metadata_chapters_manual = false;

    // Step 1: Process explicit metadata mappings from user (-map_metadata option)
    // FFmpeg: ffmpeg_mux_init.c:2923-2937
    let mut mark_manual = |meta_type: &MetadataType| -> () {
        match meta_type {
            MetadataType::Global => metadata_global_manual = true,
            MetadataType::Stream(_) => metadata_streams_manual = true,
            MetadataType::Chapter(_) => metadata_chapters_manual = true,
            MetadataType::Program(_) => {}
        }
    };

    for mapping in &mux.metadata_map {
        mark_manual(&mapping.src_type);
        mark_manual(&mapping.dst_type);

        if mapping.input_index >= input_ctxs.len() {
            log::warn!(target: LOG_TARGET,
                "Metadata mapping references non-existent input file index {}",
                mapping.input_index
            );
            continue;
        }

        let input_ctx = input_ctxs[mapping.input_index];
        if let Err(e) = copy_metadata(
            input_ctx,
            mux.out_fmt_ctx_ptr(),
            &mapping.src_type,
            &mapping.dst_type,
        ) {
            log::warn!(target: LOG_TARGET, "Failed to copy metadata from mapping: {}", e);
        }
    }

    // Step 2: Copy chapters from first input with chapters (if auto copy enabled)
    if mux.auto_copy_metadata && !metadata_chapters_manual {
        if let Some(source_demux) = demuxs.iter().find(|d| unsafe {
            !d.in_fmt_ctx_ptr().is_null() && (*d.in_fmt_ctx_ptr()).nb_chapters > 0
        }) {
            if let Err(e) = copy_chapters_from_input(
                source_demux.in_fmt_ctx_ptr(),
                source_demux.ts_offset,
                mux.out_fmt_ctx_ptr(),
                mux.start_time_us,
                mux.recording_time_us,
                true,
            ) {
                log::warn!(target: LOG_TARGET, "Failed to copy chapters: {}", e);
            }
        }
    }

    // Step 3: Apply FFmpeg's automatic metadata copying (if not disabled by user)
    // FFmpeg: ffmpeg_mux_init.c:2962-2983
    let stream_input_mapping = mux.stream_input_mapping();
    let encoding_streams = mux.encoding_streams();

    if mux.auto_copy_metadata {
        if let Err(e) = copy_metadata_default(
            &input_ctxs,
            mux.out_fmt_ctx_ptr(),
            mux.nb_streams,
            &stream_input_mapping,
            &encoding_streams,
            mux.recording_time_us.is_some(),
            mux.auto_copy_metadata,
            metadata_global_manual,
            metadata_streams_manual,
        ) {
            log::warn!(target: LOG_TARGET, "Failed to apply default metadata behavior: {}", e);
        }
    }

    // Step 4: Apply user-specified metadata values (-metadata option)
    // FFmpeg: of_add_metadata runs right after copy_meta (ffmpeg_mux_init.c:3344,3356)
    if let Err(e) = of_add_metadata(
        mux.out_fmt_ctx_ptr(),
        &mux.global_metadata,
        &mux.stream_metadata,
        &mux.chapter_metadata,
        &mux.program_metadata,
    ) {
        log::warn!(target: LOG_TARGET, "Failed to add user metadata: {}", e);
    }

    Ok(())
}

/// Check if linklabel refers to a filter graph output
/// FFmpeg reference: ffmpeg_opt.c:493 - if (arg[0] == '[')
fn is_filter_output_linklabel(linklabel: &str) -> bool {
    linklabel.starts_with('[')
}

/// Parse and expand stream map specifications
/// This mimics FFmpeg's opt_map() behavior: parse once, expand immediately
/// FFmpeg reference: ffmpeg_opt.c:478-596
unsafe fn expand_stream_maps(mux: &mut Muxer, demuxs: &[Demuxer]) -> Result<()> {
    let stream_map_specs = std::mem::take(&mut mux.stream_map_specs);

    for spec in stream_map_specs {
        // FFmpeg reference: opt_map line 488-491 - check for negative map
        let (linklabel, is_negative) = if spec.linklabel.starts_with('-') {
            (&spec.linklabel[1..], true)
        } else {
            (spec.linklabel.as_str(), false)
        };

        // FFmpeg reference: opt_map line 493 - check if this mapping refers to lavfi output
        if is_filter_output_linklabel(linklabel) {
            // FFmpeg reference: opt_map line 494-507 - extract linklabel from brackets
            let pure_linklabel = if linklabel.starts_with('[') {
                // Extract content between '[' and ']'
                if let Some(end_pos) = linklabel.find(']') {
                    &linklabel[1..end_pos]
                } else {
                    warn!(target: LOG_TARGET, "Invalid output link label: {}.", linklabel);
                    return Err(Error::OpenOutput(OpenOutputError::InvalidArgument));
                }
            } else {
                linklabel
            };

            // FFmpeg reference: opt_map line 504 - validate non-empty
            if pure_linklabel.is_empty() {
                warn!(target: LOG_TARGET, "Invalid output link label: {}.", linklabel);
                return Err(Error::OpenOutput(OpenOutputError::InvalidArgument));
            }

            // Store pure linklabel (without brackets) for later matching in map_manual()
            mux.stream_maps.push(StreamMap {
                file_index: 0,   // Not used for filter outputs
                stream_index: 0, // Not used for filter outputs
                linklabel: Some(pure_linklabel.to_string()),
                copy: spec.copy,
                disabled: false,
            });
            continue;
        }

        // FFmpeg reference: opt_map line 512 - parse file index using strtol
        // Try to parse as file stream; if it fails, treat as filter output
        let parse_result = strtol(linklabel);
        if parse_result.is_err() {
            // Failed to parse file index - treat as filter output linklabel
            // This allows bare filter output names like "my-out" (without brackets)
            mux.stream_maps.push(StreamMap {
                file_index: 0,
                stream_index: 0,
                linklabel: Some(linklabel.to_string()),
                copy: spec.copy,
                disabled: false,
            });
            continue;
        }

        let (file_idx, remainder) = parse_result.unwrap();

        // FFmpeg reference: opt_map line 513-517 - validate file index
        if file_idx < 0 || file_idx as usize >= demuxs.len() {
            warn!(target: LOG_TARGET, "Invalid input file index: {}.", file_idx);
            return Err(Error::OpenOutput(OpenOutputError::InvalidArgument));
        }
        let file_idx = file_idx as usize;

        // FFmpeg reference: opt_map line 520 - parse stream specifier
        // FFmpeg reference: opt_map line 533 - handle '?' suffix for allow_unused
        let (spec_str, allow_unused) = if remainder.ends_with('?') {
            (&remainder[..remainder.len() - 1], true)
        } else {
            (remainder, false)
        };

        let stream_spec = if spec_str.is_empty() {
            // Empty specifier matches all streams (FFmpeg behavior)
            StreamSpecifier::default()
        } else {
            // Strip leading ':' and parse stream specifier
            let spec_str = if spec_str.starts_with(':') {
                &spec_str[1..]
            } else {
                spec_str
            };

            StreamSpecifier::parse(spec_str).map_err(|e| {
                warn!(target: LOG_TARGET, "Invalid stream specifier in '{}': {}", linklabel, e);
                Error::OpenOutput(OpenOutputError::InvalidArgument)
            })?
        };

        // FFmpeg reference: opt_map line 543-553 - negative map: disable matching streams
        if is_negative {
            for existing_map in &mut mux.stream_maps {
                // Only process file-based maps (not filter outputs)
                if existing_map.linklabel.is_none() && existing_map.file_index == file_idx {
                    // Check if stream specifier matches
                    let demux = &demuxs[file_idx];
                    let fmt_ctx = demux.in_fmt_ctx_ptr();
                    let avstream = *(*fmt_ctx).streams.add(existing_map.stream_index);

                    if stream_spec.matches(fmt_ctx, avstream) {
                        existing_map.disabled = true;
                    }
                }
            }
            // Negative map doesn't add new entries, just disables existing ones
            continue;
        }

        // FFmpeg reference: opt_map line 555-574 - expand to one StreamMap per matched stream
        let demux = &demuxs[file_idx];
        let fmt_ctx = demux.in_fmt_ctx_ptr();
        let mut matched_count = 0;

        for (stream_idx, _) in demux.get_streams().iter().enumerate() {
            let avstream = *(*fmt_ctx).streams.add(stream_idx);

            // FFmpeg reference: opt_map line 556 - stream_specifier_match
            if stream_spec.matches(fmt_ctx, avstream) {
                mux.stream_maps.push(StreamMap {
                    file_index: file_idx,
                    stream_index: stream_idx,
                    linklabel: None,
                    copy: spec.copy,
                    disabled: false,
                });
                matched_count += 1;
            }
        }

        // FFmpeg reference: opt_map lines 577-590 - error handling for no matches
        if matched_count == 0 {
            if allow_unused {
                // FFmpeg line 579: verbose log for optional mappings
                info!(target: LOG_TARGET, "Stream map '{}' matches no streams; ignoring.", linklabel);
            } else {
                // FFmpeg line 586-587: fatal error with hint about '?' suffix
                warn!(target: LOG_TARGET,
                    "Stream map '{}' matches no streams.\n\
                     To ignore this, add a trailing '?' to the map.",
                    linklabel
                );
                return Err(Error::OpenOutput(OpenOutputError::MatchesNoStreams(
                    linklabel.to_string(),
                )));
            }
        }
    }

    Ok(())
}

pub(super) fn outputs_bind(
    muxs: &mut Vec<Muxer>,
    filter_graphs: &mut Vec<FilterGraph>,
    demuxs: &mut Vec<Demuxer>,
) -> Result<()> {
    // FFmpeg reference: ffmpeg.c calls opt_map during command-line parsing
    // We parse and expand stream maps early, before processing individual streams
    // This must happen AFTER demuxers are opened (need stream info for matching)
    // but BEFORE map_manual() is called (which uses the expanded StreamMap entries)
    unsafe {
        for mux in muxs.iter_mut() {
            if !mux.stream_map_specs.is_empty() {
                expand_stream_maps(mux, demuxs)?;
            }
        }
    }

    for (i, mux) in muxs.iter_mut().enumerate() {
        if mux.stream_maps.is_empty() {
            // Initialize auto_disable with muxer's stream disable flags
            // FFmpeg reference: fftools/ffmpeg_mux_init.c:1891-1895
            // auto_disable bitmask: 1 << AVMEDIA_TYPE_* disables that stream type
            let mut auto_disable = 0i32;
            if mux.video_disable {
                auto_disable |= 1 << (AVMEDIA_TYPE_VIDEO as i32);
            }
            if mux.audio_disable {
                auto_disable |= 1 << (AVMEDIA_TYPE_AUDIO as i32);
            }
            if mux.subtitle_disable {
                auto_disable |= 1 << (AVMEDIA_TYPE_SUBTITLE as i32);
            }
            if mux.data_disable {
                auto_disable |= 1 << (AVMEDIA_TYPE_DATA as i32);
            }
            output_bind_by_unlabeled_filter(i, mux, filter_graphs, &mut auto_disable)?;
            /* pick the first stream of each type */
            map_auto_streams(i, mux, demuxs, filter_graphs, auto_disable)?;
        } else {
            for stream_map in mux.stream_maps.clone() {
                map_manual(i, mux, &stream_map, filter_graphs, demuxs)?;
            }
        }

        // Create attachment streams (`-attach`) AFTER stream mapping and BEFORE
        // metadata, mirroring FFmpeg's `of_add_attachments`. They take the
        // highest stream indices and never enter the packet path.
        // SAFETY: the output AVFormatContext is live and still owned by `mux`
        // here (the mux worker has not been spawned yet).
        unsafe {
            crate::core::context::attachment::create_attachment_streams(mux)?;
        }

        // Process metadata
        unsafe {
            process_metadata(mux, demuxs)?;
        }
    }

    Ok(())
}

fn map_manual(
    index: usize,
    mux: &mut Muxer,
    stream_map: &StreamMap,
    filter_graphs: &mut Vec<FilterGraph>,
    demuxs: &mut Vec<Demuxer>,
) -> Result<()> {
    // FFmpeg reference: ffmpeg_mux_init.c:1720-1721 - check disabled flag
    if stream_map.disabled {
        return Ok(());
    }

    // FFmpeg reference: ffmpeg_mux_init.c:1723 - check for filter output
    if let Some(linklabel) = &stream_map.linklabel {
        // This is a filter graph output - match by linklabel
        for filter_graph in filter_graphs.iter_mut() {
            for i in 0..filter_graph.outputs.len() {
                let option = {
                    let output_filter = &filter_graph.outputs[i];
                    if output_filter.has_dst()
                        || output_filter.linklabel.is_empty()
                        || &output_filter.linklabel != linklabel
                    {
                        continue;
                    }

                    choose_encoder(mux, output_filter.media_type)?
                };

                match option {
                    None => {
                        // FFmpeg reference: ffmpeg_mux_init.c:1237-1242
                        // Filtering and streamcopy cannot be used together
                        error!(target: LOG_TARGET,
                            "Filtering and streamcopy cannot be used together. \
                             No encoder available for filter output type {:?}.",
                            filter_graph.outputs[i].media_type
                        );
                        return Err(OpenOutputError::InvalidArgument.into());
                    }
                    Some((codec_id, enc)) => {
                        return ofilter_bind_ost(
                            index,
                            mux,
                            filter_graph,
                            i,
                            codec_id,
                            enc,
                            None,
                            false,
                        )
                        .map(|_| ());
                    }
                }
            }
        }

        // FFmpeg reference: ffmpeg_mux_init.c:1740-1742 - filter output not found error
        warn!(target: LOG_TARGET,
            "Output with label '{}' does not exist in any defined filter graph, \
             or was already used elsewhere.",
            linklabel
        );
        return Err(OpenOutputError::InvalidArgument.into());
    }

    // This is an input file stream - use pre-parsed file_index and stream_index
    // These were already validated and expanded in expand_stream_maps()
    let demux_idx = stream_map.file_index;
    let stream_index = stream_map.stream_index;

    let demux = &mut demuxs[demux_idx];

    // Get immutable data first to avoid borrow checker issues
    let demux_node = demux.node.clone();
    let (media_type, input_stream_duration, input_stream_time_base) = {
        let input_stream = demux.get_stream_mut(stream_index);
        (
            input_stream.codec_type,
            input_stream.duration,
            input_stream.time_base,
        )
    };

    // FFmpeg reference: fftools/ffmpeg_mux_init.c:1761-1768
    // Check stream disable flags for manual mapping
    // If a stream type is disabled, skip mapping even if explicitly requested
    match media_type {
        AVMEDIA_TYPE_VIDEO if mux.video_disable => {
            info!(target: LOG_TARGET, "Skipping video stream mapping (video_disable=true)");
            return Ok(());
        }
        AVMEDIA_TYPE_AUDIO if mux.audio_disable => {
            info!(target: LOG_TARGET, "Skipping audio stream mapping (audio_disable=true)");
            return Ok(());
        }
        AVMEDIA_TYPE_SUBTITLE if mux.subtitle_disable => {
            info!(target: LOG_TARGET, "Skipping subtitle stream mapping (subtitle_disable=true)");
            return Ok(());
        }
        AVMEDIA_TYPE_DATA if mux.data_disable => {
            info!(target: LOG_TARGET, "Skipping data stream mapping (data_disable=true)");
            return Ok(());
        }
        _ => {}
    }

    info!(target: LOG_TARGET,
        "Binding output stream to input {}:{} ({})",
        demux_idx,
        stream_index,
        match media_type {
            AVMEDIA_TYPE_VIDEO => "video",
            AVMEDIA_TYPE_AUDIO => "audio",
            AVMEDIA_TYPE_SUBTITLE => "subtitle",
            AVMEDIA_TYPE_DATA => "data",
            AVMEDIA_TYPE_ATTACHMENT => "attachment",
            _ => "unknown",
        }
    );

    let option = choose_encoder(mux, media_type)?;

    match option {
        None => {
            // copy
            let (packet_sender, pre_sender, gate, _st, output_stream_index) =
                mux.new_copy_stream(demux_node)?;
            demux.add_packet_dst(
                packet_sender,
                stream_index,
                output_stream_index,
                // Architecture Y': a `-shortest` copy follower carries its mux
                // stream's `source_finished` so the demux can stop producing it
                // once `sq_mux` cascade-finishes it. `None` otherwise.
                if mux.shortest {
                    mux.stream_source_finished(output_stream_index)
                } else {
                    None
                },
                CopyMuxHandle { pre_sender, gate },
            );
            mux.register_stream_source(output_stream_index, demux_idx, stream_index, false);

            unsafe {
                streamcopy_init(
                    mux,
                    *(*demux.in_fmt_ctx_ptr()).streams.add(stream_index),
                    *(*mux.out_fmt_ctx_ptr()).streams.add(output_stream_index),
                    demux.framerate,
                )?;
                rescale_duration(
                    input_stream_duration,
                    input_stream_time_base,
                    *(*mux.out_fmt_ctx_ptr()).streams.add(output_stream_index),
                );
                mux.stream_ready()
            }
        }
        Some((codec_id, enc)) => {
            // connect input_stream to output
            if stream_map.copy {
                // copy
                let (packet_sender, pre_sender, gate, _st, output_stream_index) =
                    mux.new_copy_stream(demux_node)?;
                demux.add_packet_dst(
                    packet_sender,
                    stream_index,
                    output_stream_index,
                    // Architecture Y': a `-shortest` copy follower carries its mux
                    // stream's `source_finished` so the demux can stop producing it
                    // once `sq_mux` cascade-finishes it. `None` otherwise.
                    if mux.shortest {
                        mux.stream_source_finished(output_stream_index)
                    } else {
                        None
                    },
                    CopyMuxHandle { pre_sender, gate },
                );
                mux.register_stream_source(output_stream_index, demux_idx, stream_index, false);

                unsafe {
                    streamcopy_init(
                        mux,
                        *(*demux.in_fmt_ctx_ptr()).streams.add(stream_index),
                        *(*mux.out_fmt_ctx_ptr()).streams.add(output_stream_index),
                        demux.framerate,
                    )?;
                    rescale_duration(
                        input_stream_duration,
                        input_stream_time_base,
                        *(*mux.out_fmt_ctx_ptr()).streams.add(output_stream_index),
                    );
                    mux.stream_ready()
                }
            } else if media_type == AVMEDIA_TYPE_VIDEO || media_type == AVMEDIA_TYPE_AUDIO {
                init_simple_filtergraph(
                    demux,
                    stream_index,
                    codec_id,
                    enc,
                    index,
                    mux,
                    filter_graphs,
                    demux_idx,
                )?;
            } else {
                let (frame_sender, output_stream_index) =
                    mux.add_enc_stream(media_type, enc, demux_node, false)?;
                let input_stream = demux.get_stream_mut(stream_index);
                input_stream.add_dst(frame_sender);
                demux.connect_stream(stream_index);
                mux.register_stream_source(output_stream_index, demux_idx, stream_index, true);

                unsafe {
                    rescale_duration(
                        input_stream_duration,
                        input_stream_time_base,
                        *(*mux.out_fmt_ctx_ptr()).streams.add(output_stream_index),
                    );
                }
            }
        }
    }

    Ok(())
}

#[cfg(not(docsrs))]
fn set_channel_layout(
    ch_layout: &mut AVChannelLayout,
    ch_layouts: &Option<Vec<AVChannelLayout>>,
    layout_requested: &AVChannelLayout,
) -> Result<()> {
    unsafe {
        // Scenario 1: If the requested layout has a specified order (not UNSPEC), copy it directly
        if layout_requested.order != AV_CHANNEL_ORDER_UNSPEC {
            let ret = av_channel_layout_copy(ch_layout, layout_requested);
            if ret < 0 {
                return Err(OpenOutputError::from(ret).into());
            }
            return Ok(());
        }

        // Scenario 2: Requested layout is UNSPEC and no encoder-supported layouts available
        // Use default layout based on channel count
        if ch_layouts.is_none() {
            av_channel_layout_default(ch_layout, layout_requested.nb_channels);
            return Ok(());
        }

        // Scenario 3: Try to match channel count from encoder's supported layouts
        if let Some(layouts) = ch_layouts {
            for layout in layouts {
                if layout.nb_channels == layout_requested.nb_channels {
                    let ret = av_channel_layout_copy(ch_layout, layout);
                    if ret < 0 {
                        return Err(OpenOutputError::from(ret).into());
                    }
                    return Ok(());
                }
            }
        }

        // Scenario 4: No matching channel count found, use default layout
        av_channel_layout_default(ch_layout, layout_requested.nb_channels);
        Ok(())
    }
}

#[cfg(docsrs)]
fn configure_output_filter_opts(
    index: usize,
    mux: &mut Muxer,
    output_filter: &mut OutputFilter,
    codec_id: AVCodecID,
    enc: *const AVCodec,
    output_stream_index: usize,
) -> Result<()> {
    Ok(())
}

#[cfg(not(docsrs))]
fn configure_output_filter_opts(
    index: usize,
    mux: &mut Muxer,
    output_filter: &mut OutputFilter,
    codec_id: AVCodecID,
    enc: *const AVCodec,
    output_stream_index: usize,
) -> Result<()> {
    unsafe {
        output_filter.opts.name = format!("#{index}:{output_stream_index}");
        output_filter.opts.enc = enc;
        output_filter.opts.trim_start_us = mux.start_time_us;
        output_filter.opts.trim_duration_us = mux.recording_time_us;
        output_filter.opts.ts_offset = mux.start_time_us;
        // NEW-SC-03: carry this output's auto-conversion tuning to the output
        // filter opts. filter_task::configure_filtergraph resolves the graph
        // level value (scale_sws_opts / aresample_swr_opts) from these when the
        // graph itself has none.
        output_filter.opts.sws_opts = mux.sws_opts.clone();
        output_filter.opts.swr_opts = mux.swr_opts.clone();

        output_filter.opts.flags = OFILTER_FLAG_DISABLE_CONVERT
            | OFILTER_FLAG_AUTOSCALE
            | if av_get_exact_bits_per_sample(codec_id) == 24 {
                OFILTER_FLAG_AUDIO_24BIT
            } else {
                0
            };

        let enc_ctx = avcodec_alloc_context3(enc);
        if enc_ctx.is_null() {
            return Err(OpenOutputError::OutOfMemory.into());
        }
        let _codec_ctx = CodecContext::new(enc_ctx);

        (*enc_ctx).thread_count = 0;

        if output_filter.media_type == AVMEDIA_TYPE_VIDEO {
            // formats
            let mut formats: *const AVPixelFormat = null();
            let mut ret = avcodec_get_supported_config(
                enc_ctx,
                null(),
                AV_CODEC_CONFIG_PIX_FORMAT,
                0,
                &mut formats as *mut _ as *mut *const libc::c_void,
                null_mut(),
            );
            if ret < 0 {
                return Err(OpenOutputError::from(ret).into());
            }

            let mut current = formats;
            let mut format_list = Vec::new();
            let mut count = 0;
            const MAX_FORMATS: usize = 512;
            while !current.is_null() && *current != AV_PIX_FMT_NONE && count < MAX_FORMATS {
                format_list.push(*current);
                current = current.add(1);
                count += 1;
            }
            if count >= MAX_FORMATS {
                warn!(target: LOG_TARGET, "Reached maximum format limit");
            }
            output_filter.opts.formats = Some(format_list);

            // framerates
            let mut framerates: *const AVRational = null();
            ret = avcodec_get_supported_config(
                enc_ctx,
                null(),
                AV_CODEC_CONFIG_FRAME_RATE,
                0,
                &mut framerates as *mut _ as *mut *const libc::c_void,
                null_mut(),
            );
            if ret < 0 {
                return Err(OpenOutputError::from(ret).into());
            }
            let mut framerate_list = Vec::new();
            let mut current = framerates;
            let mut count = 0;
            const MAX_FRAMERATES: usize = 64;
            while !current.is_null()
                && (*current).num != 0
                && (*current).den != 0
                && count < MAX_FRAMERATES
            {
                framerate_list.push(*current);
                current = current.add(1);
                count += 1;
            }
            if count >= MAX_FRAMERATES {
                warn!(target: LOG_TARGET, "Reached maximum framerate limit");
            }
            output_filter.opts.framerates = Some(framerate_list);

            if let Some(framerate) = mux.framerate {
                output_filter.opts.framerate = framerate;
            }

            // -fpsmax: only one of -r/-fpsmax may be set per stream
            // (ffmpeg_mux_init.c:618-621).
            if let Some(framerate_max) = mux.framerate_max {
                if mux.framerate.is_some() {
                    error!(target: LOG_TARGET, "Only one of framerate and framerate_max can be set for an output");
                    return Err(Error::OpenOutput(OpenOutputError::InvalidArgument));
                }
                output_filter.opts.framerate_max = framerate_max;
            }

            // Set user-requested pixel format (equivalent to -pix_fmt in FFmpeg)
            // FFmpeg reference: fftools/ffmpeg_filter.c - ofilter_bind_ost sets format from OptionsContext
            if let Some(pix_fmt) = mux.pix_fmt {
                output_filter.opts.format = pix_fmt;
            }

            // color_spaces
            let mut color_spaces: *const AVColorSpace = null();
            ret = avcodec_get_supported_config(
                enc_ctx,
                null(),
                AV_CODEC_CONFIG_COLOR_SPACE,
                0,
                &mut color_spaces as *mut _ as *mut *const libc::c_void,
                null_mut(),
            );
            if ret < 0 {
                return Err(OpenOutputError::from(ret).into());
            }
            let mut color_space_list = Vec::new();
            let mut current = color_spaces;
            let mut count = 0;
            const MAX_COLOR_SPACES: usize = 128;
            while !current.is_null()
                && *current != AVCOL_SPC_UNSPECIFIED
                && count < MAX_COLOR_SPACES
            {
                color_space_list.push(*current);
                current = current.add(1);
                count += 1;
            }
            if count >= MAX_COLOR_SPACES {
                warn!(target: LOG_TARGET, "Reached maximum color space limit");
            }
            output_filter.opts.color_spaces = Some(color_space_list);

            //color_ranges
            let mut color_ranges: *const AVColorRange = null();
            ret = avcodec_get_supported_config(
                enc_ctx,
                null(),
                AV_CODEC_CONFIG_COLOR_RANGE,
                0,
                &mut color_ranges as *mut _ as *mut *const libc::c_void,
                null_mut(),
            );
            if ret < 0 {
                return Err(OpenOutputError::from(ret).into());
            }
            let mut color_range_list = Vec::new();
            let mut current = color_ranges;
            let mut count = 0;
            const MAX_COLOR_RANGES: usize = 64;
            while !current.is_null()
                && *current != AVCOL_RANGE_UNSPECIFIED
                && count < MAX_COLOR_RANGES
            {
                color_range_list.push(*current);
                current = current.add(1);
                count += 1;
            }
            if count >= MAX_COLOR_RANGES {
                warn!(target: LOG_TARGET, "Reached maximum color range limit");
            }
            output_filter.opts.color_ranges = Some(color_range_list);

            let stream = &mux.get_streams()[output_stream_index];
            output_filter.opts.vsync_method = stream.vsync_method;
        } else {
            if let Some(sample_fmt) = &mux.audio_sample_fmt {
                output_filter.opts.audio_format = *sample_fmt;
            }
            // audio formats
            let mut audio_formats: *const AVSampleFormat = null();
            let mut ret = avcodec_get_supported_config(
                enc_ctx,
                null(),
                AV_CODEC_CONFIG_SAMPLE_FORMAT,
                0,
                &mut audio_formats as *mut _ as *mut _,
                null_mut(),
            );
            if ret < 0 {
                return Err(OpenOutputError::from(ret).into());
            }

            let mut current = audio_formats;
            let mut audio_format_list = Vec::new();
            let mut count = 0;
            const MAX_AUDIO_FORMATS: usize = 32;
            while !current.is_null() && *current != AV_SAMPLE_FMT_NONE && count < MAX_AUDIO_FORMATS
            {
                audio_format_list.push(*current);
                current = current.add(1);
                count += 1;
            }
            if count >= MAX_AUDIO_FORMATS {
                warn!(target: LOG_TARGET, "Reached maximum audio format limit");
            }
            output_filter.opts.audio_formats = Some(audio_format_list);

            if let Some(audio_sample_rate) = &mux.audio_sample_rate {
                output_filter.opts.sample_rate = *audio_sample_rate;
            }
            // sample_rates
            let mut rates: *const i32 = null();
            ret = avcodec_get_supported_config(
                enc_ctx,
                null(),
                AV_CODEC_CONFIG_SAMPLE_RATE,
                0,
                &mut rates as *mut _ as *mut _,
                null_mut(),
            );
            if ret < 0 {
                return Err(OpenOutputError::from(ret).into());
            }
            let mut rate_list = Vec::new();
            let mut current = rates;
            let mut count = 0;
            const MAX_SAMPLE_RATES: usize = 64;
            while !current.is_null() && *current != 0 && count < MAX_SAMPLE_RATES {
                rate_list.push(*current);
                current = current.add(1);
                count += 1;
            }
            if count >= MAX_SAMPLE_RATES {
                warn!(target: LOG_TARGET, "Reached maximum sample rate limit");
            }
            output_filter.opts.sample_rates = Some(rate_list);

            if let Some(channels) = &mux.audio_channels {
                output_filter.opts.ch_layout.nb_channels = *channels;
            }
            // channel_layouts
            let mut layouts: *const AVChannelLayout = null();
            ret = avcodec_get_supported_config(
                enc_ctx,
                null(),
                AV_CODEC_CONFIG_CHANNEL_LAYOUT,
                0,
                &mut layouts as *mut _ as *mut _,
                null_mut(),
            );
            if ret < 0 {
                return Err(OpenOutputError::from(ret).into());
            }
            let mut layout_list = Vec::new();
            let mut current = layouts;
            let mut count = 0;
            const MAX_CHANNEL_LAYOUTS: usize = 128;
            while !current.is_null()
                && (*current).order != AV_CHANNEL_ORDER_UNSPEC
                && count < MAX_CHANNEL_LAYOUTS
            {
                layout_list.push(*current);
                current = current.add(1);
                count += 1;
            }
            if count >= MAX_CHANNEL_LAYOUTS {
                warn!(target: LOG_TARGET, "Reached maximum channel layout limit");
            }
            output_filter.opts.ch_layouts = Some(layout_list);

            // Call set_channel_layout to resolve UNSPEC layouts to proper defaults
            // Corresponds to FFmpeg ffmpeg_filter.c:879-882
            if output_filter.opts.ch_layout.nb_channels > 0 {
                let layout_requested = output_filter.opts.ch_layout;
                set_channel_layout(
                    &mut output_filter.opts.ch_layout,
                    &output_filter.opts.ch_layouts,
                    &layout_requested,
                )?;
            }
        }
    };
    Ok(())
}

fn map_auto_streams(
    mux_index: usize,
    mux: &mut Muxer,
    demuxs: &mut Vec<Demuxer>,
    filter_graphs: &mut Vec<FilterGraph>,
    auto_disable: i32,
) -> Result<()> {
    unsafe {
        let oformat = (*mux.out_fmt_ctx_ptr()).oformat;
        map_auto_stream(
            mux_index,
            mux,
            demuxs,
            oformat,
            AVMEDIA_TYPE_VIDEO,
            filter_graphs,
            auto_disable,
        )?;
        map_auto_stream(
            mux_index,
            mux,
            demuxs,
            oformat,
            AVMEDIA_TYPE_AUDIO,
            filter_graphs,
            auto_disable,
        )?;
        map_auto_subtitle(mux, demuxs, oformat, auto_disable)?;
        map_auto_data(mux, demuxs, oformat, auto_disable)?;
    }
    Ok(())
}

#[cfg(docsrs)]
unsafe fn map_auto_subtitle(
    mux: &mut Muxer,
    demuxs: &mut Vec<Demuxer>,
    oformat: *const AVOutputFormat,
    auto_disable: i32,
) -> Result<()> {
    Ok(())
}

#[cfg(not(docsrs))]
unsafe fn map_auto_subtitle(
    mux: &mut Muxer,
    demuxs: &mut Vec<Demuxer>,
    oformat: *const AVOutputFormat,
    auto_disable: i32,
) -> Result<()> {
    if auto_disable & (1 << AVMEDIA_TYPE_SUBTITLE as i32) != 0 {
        return Ok(());
    }

    // An explicit user codec (including "copy") must not be gated on the
    // container's default subtitle encoder being available
    // (matches ffmpeg_mux_init.c:1660 `!avcodec_find_encoder(...) && !subtitle_codec_name`).
    let output_codec = avcodec_find_encoder((*oformat).subtitle_codec);
    if output_codec.is_null() && mux.subtitle_codec.is_none() {
        return Ok(());
    }
    let output_descriptor = if output_codec.is_null() {
        null()
    } else {
        avcodec_descriptor_get((*output_codec).id)
    };

    // Scan every subtitle stream of every input and map the first compatible
    // one (matches ffmpeg_mux_init.c:1701-1725, which iterates all input
    // streams and only stops after a successful mapping — an incompatible
    // first track, e.g. PGS before srt, must not end the search).
    for (demux_idx, demux) in demuxs.iter_mut().enumerate() {
        for stream_index in 0..demux.get_streams().len() {
            if demux.get_stream(stream_index).codec_type != AVMEDIA_TYPE_SUBTITLE {
                continue;
            }

            let input_descriptor =
                avcodec_descriptor_get((*demux.get_stream(stream_index).codec_parameters).codec_id);
            let mut input_props = 0;
            if !input_descriptor.is_null() {
                input_props =
                    (*input_descriptor).props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
            }
            let mut output_props = 0;
            if !output_descriptor.is_null() {
                output_props = (*output_descriptor).props
                    & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
            }

            // A user-chosen codec short-circuits the text/bitmap check
            // (matches ffmpeg_mux_init.c:1679 `subtitle_codec_name || ...`).
            let compatible = mux.subtitle_codec.is_some()
                || input_props & output_props != 0
                // Map dvb teletext which has neither property to any output subtitle encoder
                || !input_descriptor.is_null() && !output_descriptor.is_null()
                    && ((*input_descriptor).props == 0 || (*output_descriptor).props == 0);
            if !compatible {
                continue;
            }

            // choose_encoder returns None for "copy": take the streamcopy
            // path like map_auto_stream instead of failing.
            let option = choose_encoder(mux, AVMEDIA_TYPE_SUBTITLE)?;
            if let Some((_codec_id, enc)) = option {
                let (frame_sender, output_stream_index) =
                    mux.add_enc_stream(AVMEDIA_TYPE_SUBTITLE, enc, demux.node.clone(), false)?;
                demux.get_stream_mut(stream_index).add_dst(frame_sender);
                demux.connect_stream(stream_index);
                mux.register_stream_source(output_stream_index, demux_idx, stream_index, true);
                let input_stream = demux.get_stream(stream_index);
                unsafe {
                    rescale_duration(
                        input_stream.duration,
                        input_stream.time_base,
                        *(*mux.out_fmt_ctx_ptr()).streams.add(output_stream_index),
                    );
                }
            } else {
                let input_stream = demux.get_stream(stream_index);
                let input_stream_duration = input_stream.duration;
                let input_stream_time_base = input_stream.time_base;

                let (packet_sender, pre_sender, gate, _st, output_stream_index) =
                    mux.new_copy_stream(demux.node.clone())?;
                demux.add_packet_dst(
                    packet_sender,
                    stream_index,
                    output_stream_index,
                    // Architecture Y': a `-shortest` copy follower carries its mux
                    // stream's `source_finished` so the demux can stop producing it
                    // once `sq_mux` cascade-finishes it. `None` otherwise.
                    if mux.shortest {
                        mux.stream_source_finished(output_stream_index)
                    } else {
                        None
                    },
                    CopyMuxHandle { pre_sender, gate },
                );
                mux.register_stream_source(output_stream_index, demux_idx, stream_index, false);

                unsafe {
                    streamcopy_init(
                        mux,
                        *(*demux.in_fmt_ctx_ptr()).streams.add(stream_index),
                        *(*mux.out_fmt_ctx_ptr()).streams.add(output_stream_index),
                        demux.framerate,
                    )?;
                    rescale_duration(
                        input_stream_duration,
                        input_stream_time_base,
                        *(*mux.out_fmt_ctx_ptr()).streams.add(output_stream_index),
                    );
                    mux.stream_ready()
                }
            }
            return Ok(());
        }
    }

    Ok(())
}

#[cfg(docsrs)]
unsafe fn map_auto_data(
    mux: &mut Muxer,
    demuxs: &mut Vec<Demuxer>,
    oformat: *const AVOutputFormat,
    auto_disable: i32,
) -> Result<()> {
    Ok(())
}

#[cfg(not(docsrs))]
unsafe fn map_auto_data(
    mux: &mut Muxer,
    demuxs: &mut Vec<Demuxer>,
    oformat: *const AVOutputFormat,
    auto_disable: i32,
) -> Result<()> {
    if auto_disable & (1 << AVMEDIA_TYPE_DATA as i32) != 0 {
        return Ok(());
    }

    /* Data only if codec id match */
    let codec_id = av_guess_codec(
        oformat,
        null(),
        (*mux.out_fmt_ctx_ptr()).url,
        null(),
        AVMEDIA_TYPE_DATA,
    );

    if codec_id == AV_CODEC_ID_NONE {
        return Ok(());
    }

    for (demux_idx, demux) in demuxs.iter_mut().enumerate() {
        let option = demux
            .get_streams()
            .iter()
            .enumerate()
            .find_map(|(index, input_stream)| {
                if input_stream.codec_type == AVMEDIA_TYPE_DATA
                    && (*input_stream.codec_parameters).codec_id == codec_id
                {
                    Some(index)
                } else {
                    None
                }
            });

        if option.is_none() {
            continue;
        }

        let stream_index = option.unwrap();
        let option = choose_encoder(mux, AVMEDIA_TYPE_DATA)?;

        if option.is_some() {
            // FFmpeg reference: ffmpeg_mux_init.c:79-89
            // choose_encoder always returns enc=NULL for AVMEDIA_TYPE_DATA
            unreachable!("DATA streams do not have encoders in FFmpeg");
        } else {
            // FFmpeg behavior: DATA streams use stream copy when no encoder is available
            // Reference: fftools/ffmpeg_mux_init.c:79-89 - choose_encoder returns enc=NULL for DATA
            // Reference: fftools/ffmpeg_mux_init.c:1236-1246 - ost_add uses stream copy when enc=NULL
            let input_stream = demux.get_stream(stream_index);
            let input_stream_duration = input_stream.duration;
            let input_stream_time_base = input_stream.time_base;

            let (packet_sender, pre_sender, gate, _st, output_stream_index) =
                mux.new_copy_stream(demux.node.clone())?;
            demux.add_packet_dst(
                packet_sender,
                stream_index,
                output_stream_index,
                // Architecture Y': a `-shortest` copy follower carries its mux
                // stream's `source_finished` so the demux can stop producing it
                // once `sq_mux` cascade-finishes it. `None` otherwise.
                if mux.shortest {
                    mux.stream_source_finished(output_stream_index)
                } else {
                    None
                },
                CopyMuxHandle { pre_sender, gate },
            );
            mux.register_stream_source(output_stream_index, demux_idx, stream_index, false);

            streamcopy_init(
                mux,
                *(*demux.in_fmt_ctx_ptr()).streams.add(stream_index),
                *(*mux.out_fmt_ctx_ptr()).streams.add(output_stream_index),
                demux.framerate,
            )?;
            rescale_duration(
                input_stream_duration,
                input_stream_time_base,
                *(*mux.out_fmt_ctx_ptr()).streams.add(output_stream_index),
            );
            mux.stream_ready()
        }

        break;
    }

    Ok(())
}

#[cfg(docsrs)]
unsafe fn map_auto_stream(
    mux_index: usize,
    mux: &mut Muxer,
    demuxs: &mut Vec<Demuxer>,
    oformat: *const AVOutputFormat,
    media_type: AVMediaType,
    filter_graphs: &mut Vec<FilterGraph>,
    auto_disable: i32,
) -> Result<()> {
    Ok(())
}

#[cfg(not(docsrs))]
unsafe fn map_auto_stream(
    mux_index: usize,
    mux: &mut Muxer,
    demuxs: &mut Vec<Demuxer>,
    oformat: *const AVOutputFormat,
    media_type: AVMediaType,
    filter_graphs: &mut Vec<FilterGraph>,
    auto_disable: i32,
) -> Result<()> {
    if auto_disable & (1 << media_type as i32) != 0 {
        return Ok(());
    }
    if (media_type == AVMEDIA_TYPE_VIDEO
        || media_type == AVMEDIA_TYPE_AUDIO
        || media_type == AVMEDIA_TYPE_DATA)
        && av_guess_codec(
            oformat,
            null(),
            (*mux.out_fmt_ctx_ptr()).url,
            null(),
            media_type,
        ) == AV_CODEC_ID_NONE
    {
        return Ok(());
    }

    // Mirror ffmpeg_mux_init.c map_auto_video/map_auto_audio: score every
    // stream of every input and map the single global best (video: highest
    // resolution; audio: most channels). Other types keep first-found.
    let selected = if media_type == AVMEDIA_TYPE_VIDEO || media_type == AVMEDIA_TYPE_AUDIO {
        unsafe { select_best_stream(demuxs, oformat, media_type) }
    } else {
        demuxs.iter().enumerate().find_map(|(demux_idx, demux)| {
            demux
                .get_streams()
                .iter()
                .position(|input_stream| input_stream.codec_type == media_type)
                .map(|stream_index| (demux_idx, stream_index))
        })
    };
    let Some((demux_idx, stream_index)) = selected else {
        return Ok(());
    };

    let demux = &mut demuxs[demux_idx];
    let input_file_idx = demux_idx;
    let option = choose_encoder(mux, media_type)?;

    if let Some((codec_id, enc)) = option {
        if media_type == AVMEDIA_TYPE_VIDEO || media_type == AVMEDIA_TYPE_AUDIO {
            init_simple_filtergraph(
                demux,
                stream_index,
                codec_id,
                enc,
                mux_index,
                mux,
                filter_graphs,
                input_file_idx,
            )?;
        } else {
            let (frame_sender, output_stream_index) =
                mux.add_enc_stream(media_type, enc, demux.node.clone(), false)?;
            demux.get_stream_mut(stream_index).add_dst(frame_sender);
            demux.connect_stream(stream_index);
            mux.register_stream_source(output_stream_index, input_file_idx, stream_index, true);
            let input_stream = demux.get_stream(stream_index);
            unsafe {
                rescale_duration(
                    input_stream.duration,
                    input_stream.time_base,
                    *(*mux.out_fmt_ctx_ptr()).streams.add(output_stream_index),
                );
            }
        }

        return Ok(());
    }

    // copy: like the encoder branch, the CLI maps exactly one stream per
    // media type.
    let input_stream = demux.get_stream(stream_index);
    let input_stream_duration = input_stream.duration;
    let input_stream_time_base = input_stream.time_base;

    let (packet_sender, pre_sender, gate, _st, output_stream_index) =
        mux.new_copy_stream(demux.node.clone())?;
    demux.add_packet_dst(
        packet_sender,
        stream_index,
        output_stream_index,
        if mux.shortest {
            mux.stream_source_finished(output_stream_index)
        } else {
            None
        },
        CopyMuxHandle { pre_sender, gate },
    );
    mux.register_stream_source(output_stream_index, input_file_idx, stream_index, false);

    unsafe {
        streamcopy_init(
            mux,
            *(*demux.in_fmt_ctx_ptr()).streams.add(stream_index),
            *(*mux.out_fmt_ctx_ptr()).streams.add(output_stream_index),
            demux.framerate,
        )?;
        rescale_duration(
            input_stream_duration,
            input_stream_time_base,
            *(*mux.out_fmt_ctx_ptr()).streams.add(output_stream_index),
        );
        mux.stream_ready()
    }

    Ok(())
}

/// Pick the globally best video/audio stream across all inputs
/// (ffmpeg_mux_init.c map_auto_video:1539 / map_auto_audio:1648): video is
/// scored by resolution, audio by channel count, both raised by
/// NEW_PACKETS/DEFAULT-disposition bonuses; attached pictures only win where
/// the container itself is picture-based (avformat_query_codec == 'APIC').
#[cfg(not(docsrs))]
unsafe fn select_best_stream(
    demuxs: &[Demuxer],
    oformat: *const AVOutputFormat,
    media_type: AVMediaType,
) -> Option<(usize, usize)> {
    const APIC_TAG: i32 =
        (b'A' as i32) | ((b'P' as i32) << 8) | ((b'I' as i32) << 16) | ((b'C' as i32) << 24);
    let qcr = if media_type == AVMEDIA_TYPE_VIDEO {
        avformat_query_codec(oformat, (*oformat).video_codec, 0)
    } else {
        0
    };

    let mut best: Option<(usize, usize)> = None;
    let mut best_score: i64 = 0;

    for (demux_idx, demux) in demuxs.iter().enumerate() {
        let mut file_best: Option<usize> = None;
        let mut file_best_score: i64 = 0;

        for (stream_index, input_stream) in demux.get_streams().iter().enumerate() {
            if input_stream.codec_type != media_type {
                continue;
            }
            // The CLI also skips AV_CODEC_PROP_ENHANCEMENT streams (LCEVC
            // enhancement layers); ffmpeg-sys-next 7.1 has no binding for
            // that flag yet, so the check is deferred to the bindings bump.

            let par = input_stream.codec_parameters;
            let st = input_stream.stream.inner;
            let attached_pic = (*st).disposition & AV_DISPOSITION_ATTACHED_PIC != 0;

            let mut score: i64 = if media_type == AVMEDIA_TYPE_VIDEO {
                (*par).width as i64 * (*par).height as i64
            } else {
                (*par).ch_layout.nb_channels as i64
            };
            if (*st).event_flags & AVSTREAM_EVENT_FLAG_NEW_PACKETS != 0 {
                score += 100_000_000;
            }
            if (*st).disposition & AV_DISPOSITION_DEFAULT != 0 {
                score += 5_000_000;
            }
            if media_type == AVMEDIA_TYPE_VIDEO && qcr != APIC_TAG && attached_pic {
                // Cover art only wins when nothing else is available.
                score = 1;
            }

            if score > file_best_score {
                if media_type == AVMEDIA_TYPE_VIDEO && qcr == APIC_TAG && !attached_pic {
                    // This container carries video only as an attached picture.
                    continue;
                }
                file_best_score = score;
                file_best = Some(stream_index);
            }
        }

        if let Some(stream_index) = file_best {
            let st = demux.get_stream(stream_index).stream.inner;
            let attached_pic = (*st).disposition & AV_DISPOSITION_ATTACHED_PIC != 0;
            // The DEFAULT-disposition bonus ranks streams within one file
            // only; drop it before comparing across files.
            if (media_type != AVMEDIA_TYPE_VIDEO || qcr == APIC_TAG || !attached_pic)
                && (*st).disposition & AV_DISPOSITION_DEFAULT != 0
            {
                file_best_score -= 5_000_000;
            }
            if file_best_score > best_score {
                best_score = file_best_score;
                best = Some((demux_idx, stream_index));
            }
        }
    }

    best
}

fn init_simple_filtergraph(
    demux: &mut Demuxer,
    stream_index: usize,
    codec_id: AVCodecID,
    enc: *const AVCodec,
    mux_index: usize,
    mux: &mut Muxer,
    filter_graphs: &mut Vec<FilterGraph>,
    input_file_idx: usize,
) -> Result<()> {
    let codec_type = demux.get_stream(stream_index).codec_type;

    let filter_desc = if codec_type == AVMEDIA_TYPE_VIDEO {
        "null"
    } else {
        "anull"
    };
    // Implicit per-output graph: no explicit FilterComplex, so there is no
    // graph-level sws/swr value here. The per-output request (Output::set_sws_opts
    // / set_swr_opts) flows in through the bound OutputFilterOptions instead and
    // is resolved in filter_task::configure_filtergraph.
    let mut filter_graph = init_filter_graph(filter_graphs.len(), filter_desc, None, None, None)?;

    // filter_graph.inputs[0].media_type = codec_type;
    // filter_graph.outputs[0].media_type = codec_type;

    ifilter_bind_ist(&mut filter_graph, 0, stream_index, demux)?;
    // fftools ost->ist rule: fed directly by a single-stream input
    // (ffmpeg_mux_init.c:817-822).
    let single_stream_direct_input = demux.get_streams().len() == 1;
    ofilter_bind_ost(
        mux_index,
        mux,
        &mut filter_graph,
        0,
        codec_id,
        enc,
        Some((input_file_idx, stream_index)),
        single_stream_direct_input,
    )?;

    filter_graphs.push(filter_graph);

    Ok(())
}

unsafe fn rescale_duration(src_duration: i64, src_time_base: AVRational, stream: *mut AVStream) {
    (*stream).duration = av_rescale_q(src_duration, src_time_base, (*stream).time_base);
}

#[cfg(docsrs)]
fn streamcopy_init(
    mux: &mut Muxer,
    input_stream: *mut AVStream,
    output_stream: *mut AVStream,
    input_framerate: AVRational,
) -> Result<()> {
    Ok(())
}

#[cfg(not(docsrs))]
fn streamcopy_init(
    mux: &mut Muxer,
    input_stream: *mut AVStream,
    output_stream: *mut AVStream,
    input_framerate: AVRational,
) -> Result<()> {
    unsafe {
        let codec_ctx = avcodec_alloc_context3(null_mut());
        if codec_ctx.is_null() {
            return Err(OpenOutputError::OutOfMemory.into());
        }
        let _codec_context = CodecContext::new(codec_ctx);

        let mut ret = avcodec_parameters_to_context(codec_ctx, (*input_stream).codecpar);
        if ret < 0 {
            error!(target: LOG_TARGET, "Error setting up codec context options.");
            return Err(OpenOutputError::from(ret).into());
        }

        ret = avcodec_parameters_from_context((*output_stream).codecpar, codec_ctx);
        if ret < 0 {
            error!(target: LOG_TARGET, "Error getting reference codec parameters.");
            return Err(OpenOutputError::from(ret).into());
        }

        // In the CLI this is the user's -tag value, 0 when absent; ez has no
        // per-stream tag option yet. Seeding it from the copied parameters
        // made the check below unreachable, so a source tag the target
        // container cannot represent (e.g. AVI's FMP4 into mp4) was kept and
        // avformat_write_header rejected it.
        let mut codec_tag = 0;
        if codec_tag == 0 {
            let ct = (*(*mux.out_fmt_ctx_ptr()).oformat).codec_tag;
            let mut codec_tag_tmp = 0;
            if ct.is_null()
                || av_codec_get_id(ct, (*(*output_stream).codecpar).codec_tag)
                    == (*(*output_stream).codecpar).codec_id
                || av_codec_get_tag2(
                    ct,
                    (*(*output_stream).codecpar).codec_id,
                    &mut codec_tag_tmp,
                ) == 0
            {
                codec_tag = (*(*output_stream).codecpar).codec_tag;
            }
        }
        (*(*output_stream).codecpar).codec_tag = codec_tag;

        // Match FFmpeg CLI: framerate only applies to video streams.
        // In CLI, ist->framerate is only set for video (ffmpeg_demux.c:1429-1432, case AVMEDIA_TYPE_VIDEO)
        // and ost->frame_rate is only set in new_stream_video() (ffmpeg_mux_init.c:607).
        // Since ez-ffmpeg stores framerate per-file (not per-stream), we need an explicit guard
        // to prevent applying framerate to audio/subtitle streams in streamcopy mode.
        let codec_type = (*(*output_stream).codecpar).codec_type;
        let mut fr = AVRational { num: 0, den: 0 };
        if codec_type == AVMEDIA_TYPE_VIDEO {
            fr = mux.framerate.unwrap_or(AVRational { num: 0, den: 0 });
            if fr.num == 0 {
                fr = input_framerate;
            }
        }

        if fr.num != 0 {
            (*output_stream).avg_frame_rate = fr;
        } else {
            (*output_stream).avg_frame_rate = (*input_stream).avg_frame_rate;
        }

        // copy timebase while removing common factors
        if (*output_stream).time_base.num <= 0 || (*output_stream).time_base.den <= 0 {
            if fr.num != 0 {
                (*output_stream).time_base = av_inv_q(fr);
            } else {
                (*output_stream).time_base =
                    av_add_q((*input_stream).time_base, AVRational { num: 0, den: 1 });
            }
        }

        for i in 0..(*(*input_stream).codecpar).nb_coded_side_data {
            let sd_src = (*(*input_stream).codecpar)
                .coded_side_data
                .offset(i as isize);

            let sd_dst = av_packet_side_data_new(
                &mut (*(*output_stream).codecpar).coded_side_data,
                &mut (*(*output_stream).codecpar).nb_coded_side_data,
                (*sd_src).type_,
                (*sd_src).size,
                0,
            );
            if sd_dst.is_null() {
                return Err(OpenOutputError::OutOfMemory.into());
            }
            std::ptr::copy_nonoverlapping(
                (*sd_src).data as *const u8,
                (*sd_dst).data,
                (*sd_src).size,
            );
        }

        match (*(*output_stream).codecpar).codec_type {
            AVMEDIA_TYPE_AUDIO => {
                if ((*(*output_stream).codecpar).block_align == 1
                    || (*(*output_stream).codecpar).block_align == 1152
                    || (*(*output_stream).codecpar).block_align == 576)
                    && (*(*output_stream).codecpar).codec_id == AV_CODEC_ID_MP3
                {
                    (*(*output_stream).codecpar).block_align = 0;
                }
                if (*(*output_stream).codecpar).codec_id == AV_CODEC_ID_AC3 {
                    (*(*output_stream).codecpar).block_align = 0;
                }
            }
            AVMEDIA_TYPE_VIDEO => {
                let sar = if (*input_stream).sample_aspect_ratio.num != 0 {
                    (*input_stream).sample_aspect_ratio
                } else {
                    (*(*output_stream).codecpar).sample_aspect_ratio
                };
                (*output_stream).sample_aspect_ratio = sar;
                (*(*output_stream).codecpar).sample_aspect_ratio = sar;
                (*output_stream).r_frame_rate = (*input_stream).r_frame_rate;
            }
            _ => {}
        }
    };
    Ok(())
}

fn output_bind_by_unlabeled_filter(
    index: usize,
    mux: &mut Muxer,
    filter_graphs: &mut Vec<FilterGraph>,
    auto_disable: &mut i32,
) -> Result<()> {
    let fg_len = filter_graphs.len();

    for i in 0..fg_len {
        let filter_graph = &mut filter_graphs[i];

        for i in 0..filter_graph.outputs.len() {
            let media_type = filter_graph.outputs[i].media_type;

            // Check if this stream type is disabled
            if *auto_disable & (1 << media_type as i32) != 0 {
                continue;
            }

            let option = {
                let output_filter = &filter_graph.outputs[i];
                if (!output_filter.linklabel.is_empty() && output_filter.linklabel != "out")
                    || output_filter.has_dst()
                {
                    continue;
                }

                choose_encoder(mux, output_filter.media_type)?
            };

            match option {
                None => {
                    warn!(target: LOG_TARGET,
                        "An unexpected media_type {:?} appears in output_filter",
                        media_type
                    );
                }
                Some((codec_id, enc)) => {
                    *auto_disable |= 1 << media_type as i32;
                    ofilter_bind_ost(index, mux, filter_graph, i, codec_id, enc, None, false)?;
                }
            }
        }
    }

    Ok(())
}

fn ofilter_bind_ost(
    index: usize,
    mux: &mut Muxer,
    filter_graph: &mut FilterGraph,
    output_filter_index: usize,
    codec_id: AVCodecID,
    enc: *const AVCodec,
    stream_source: Option<(usize, usize)>,
    single_stream_direct_input: bool,
) -> Result<usize> {
    let output_filter = &mut filter_graph.outputs[output_filter_index];
    let (frame_sender, output_stream_index) = mux.add_enc_stream(
        output_filter.media_type,
        enc,
        filter_graph.node.clone(),
        single_stream_direct_input,
    )?;
    output_filter.set_dst(frame_sender);

    if let Some((file_idx, stream_idx)) = stream_source {
        mux.register_stream_source(output_stream_index, file_idx, stream_idx, true);
    }

    configure_output_filter_opts(
        index,
        mux,
        output_filter,
        codec_id,
        enc,
        output_stream_index,
    )?;
    Ok(output_stream_index)
}

fn choose_encoder(
    mux: &Muxer,
    media_type: AVMediaType,
) -> Result<Option<(AVCodecID, *const AVCodec)>> {
    let media_codec = match media_type {
        AVMEDIA_TYPE_VIDEO => mux.video_codec.clone(),
        AVMEDIA_TYPE_AUDIO => mux.audio_codec.clone(),
        AVMEDIA_TYPE_SUBTITLE => mux.subtitle_codec.clone(),
        _ => return Ok(None),
    };

    match media_codec {
        None => {
            let url = CString::new(&*mux.url)?;
            unsafe {
                let codec_id = av_guess_codec(
                    (*mux.out_fmt_ctx_ptr()).oformat,
                    null(),
                    url.as_ptr(),
                    null(),
                    media_type,
                );
                let enc = avcodec_find_encoder(codec_id);
                if enc.is_null() {
                    let format_name = (*(*mux.out_fmt_ctx_ptr()).oformat).name;
                    let format_name = CStr::from_ptr(format_name).to_str();
                    let codec_name = avcodec_get_name(codec_id);
                    let codec_name = CStr::from_ptr(codec_name).to_str();
                    if let (Ok(format_name), Ok(codec_name)) = (format_name, codec_name) {
                        error!(target: LOG_TARGET, "Automatic encoder selection failed Default encoder for format {format_name} (codec {codec_name}) is probably disabled. Please choose an encoder manually.");
                    }
                    return Err(OpenOutputError::from(AVERROR_ENCODER_NOT_FOUND).into());
                }

                return Ok(Some((codec_id, enc)));
            }
        }
        Some(media_codec) if media_codec != "copy" => unsafe {
            let media_codec_cstr = CString::new(media_codec.clone())?;

            let mut enc = avcodec_find_encoder_by_name(media_codec_cstr.as_ptr());
            let desc = avcodec_descriptor_get_by_name(media_codec_cstr.as_ptr());

            if enc.is_null() && !desc.is_null() {
                enc = avcodec_find_encoder((*desc).id);
                if !enc.is_null() {
                    let codec_name = (*enc).name;
                    let codec_name = CStr::from_ptr(codec_name).to_str();
                    let desc_name = (*desc).name;
                    let desc_name = CStr::from_ptr(desc_name).to_str();
                    if let (Ok(codec_name), Ok(desc_name)) = (codec_name, desc_name) {
                        debug!(target: LOG_TARGET, "Matched encoder '{codec_name}' for codec '{desc_name}'.");
                    }
                }
            }

            if enc.is_null() {
                error!(target: LOG_TARGET, "Unknown encoder '{media_codec}'");
                return Err(OpenOutputError::from(AVERROR_ENCODER_NOT_FOUND).into());
            }

            if (*enc).type_ != media_type {
                error!(target: LOG_TARGET, "Invalid encoder type '{media_codec}'");
                return Err(OpenOutputError::InvalidArgument.into());
            }
            let codec_id = (*enc).id;
            return Ok(Some((codec_id, enc)));
        },
        _ => {}
    };

    Ok(None)
}