1#!/usr/bin/env perl
2
3# Copyright (c) 2012 Nicolas George
4#
5# This file is part of FFmpeg.
6#
7# FFmpeg is free software; you can redistribute it and/or
8# modify it under the terms of the GNU Lesser General Public
9# License as published by the Free Software Foundation; either
10# version 2.1 of the License, or (at your option) any later version.
11#
12# FFmpeg is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15# See the GNU Lesser General Public License for more details.
16#
17# You should have received a copy of the GNU Lesser General Public License
18# along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
19# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
21=head1 NAME
22
23make_chlayout_test - produce a multichannel test file with the channels
24clearly identified
25
26=head1 SYNOPSIS
27
28tools/make_chlayout_test I<channels> I<out_options>
29
30=head1 DESCRIPTION
31
32This script uses B<ffmpeg> and B<libflite> to produce a file with audio
33channels clearly identified by their name. The resulting file can be used to
34check that the layout and order of channels is correctly handled by a piece
35of software, either a part of B<FFmpeg> or not.
36
37I<channels> is a list of channels or channel layouts, separated by '+'.
38
39I<out_options> is a list of valid ffmpeg outout options, including the
40output file.
41
42Note that some output codecs or formats can not handle arbitrary channel
43layout.
44
45This script requires a B<ffmpeg> binary, either in the source tree or in the
46search path; it must have the flite audio source enabled.
47
48=head1 EXAMPLES
49
50Check that the speakers are correctly plugged:
51
52  tools/make_chlayout_test FL+FR -f alsa default
53
54Produce a 5.1 FLAC file:
55
56  tools/make_chlayout_test 5.1 surround.flac
57
58=cut
59
60use strict;
61use warnings;
62use Getopt::Long ":config" => "require_order";
63use Pod::Usage;
64
65GetOptions (
66  "help|usage|?|h" => sub { pod2usage({ -verbose => 1, -exitval => 0 }) },
67  "manpage|m"      => sub { pod2usage({ -verbose => 2, -exitval => 0 }) },
68) and @ARGV >= 2 or pod2usage({ -verbose => 1, -exitval => 1 });
69
70my $channels = shift @ARGV;
71my @out_options = @ARGV;
72
73my $ffmpeg = exists $ENV{FFMPEG} ? $ENV{FFMPEG} :
74             $0 =~ /(.*)\// && -e "$1/../ffmpeg" ? "$1/../ffmpeg" :
75             "ffmpeg";
76
77my %channel_label_to_descr;
78my %layout_to_channels;
79
80{
81  open my $stderr, ">&STDERR";
82  open STDERR, ">", "/dev/null";
83  open my $f, "-|", $ffmpeg, "-layouts" or die "$ffmpeg: $!\n";
84  open STDERR, ">&", $stderr;
85  while (<$f>) {
86    chomp;
87    next if /^NAME/ or /:$/ or /^$/; # skip headings
88    my ($name, $descr) = split " ", $_, 2;
89    next unless $descr;
90    if ($descr =~ /^[[:upper:]]+(?:\+[[:upper:]]+)*$/) {
91      $layout_to_channels{$name} = [ split /\+/, $descr ];
92    } else {
93      $channel_label_to_descr{$name} = $descr;
94    }
95  }
96}
97
98my @channels = map { @{$layout_to_channels{$_} // [$_]} } split /\+/, $channels;
99
100my $layout = join "+", @channels;
101my $graph = "";
102my $concat_in = "";
103for my $i (0 .. $#channels) {
104  my $label = $channels[$i];
105  my $descr = $channel_label_to_descr{$label}
106    or die "Channel $label not found\n";
107  $graph .= "flite=text='${descr}', aformat=channel_layouts=mono, " .
108            "pan=${layout}:${label}=c0 [ch$i] ;\n";
109  $concat_in .= "[ch$i] ";
110}
111$graph .= "${concat_in}concat=v=0:a=1:n=" . scalar(@channels);
112
113exec $ffmpeg, "-f", "lavfi", "-i", $graph, @out_options
114  or die "$ffmpeg: $!\n";
115