summaryrefslogtreecommitdiff
path: root/tools/make_chlayout_test
blob: fcdbda3b73b6674c91012462446675dda8d00845 (plain)
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
#!/usr/bin/env perl

# Copyright (c) 2012 Nicolas George
#
# This file is part of FFmpeg.
#
# FFmpeg is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# FFmpeg is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

=head1 NAME

make_chlayout_test - produce a multichannel test file with the channels
clearly identified

=head1 SYNOPSIS

tools/make_chlayout_test I<channels> I<out_options>

=head1 DESCRIPTION

This script uses B<ffmpeg> and B<libflite> to produce a file with audio
channels clearly identified by their name. The resulting file can be used to
check that the layout and order of channels is correctly handled by a piece
of software, either a part of B<FFmpeg> or not.

I<channels> is a list of channels or channel layouts, separated by '+'.

I<out_options> is a list of valid ffmpeg outout options, including the
output file.

Note that some output codecs or formats can not handle arbitrary channel
layout.

This script requires a B<ffmpeg> binary, either in the source tree or in the
search path; it must have the flite audio source enabled.

=head1 EXAMPLES

Check that the speakers are correctly plugged:

  tools/make_chlayout_test FL+FR -f alsa default

Produce a 5.1 FLAC file:

  tools/make_chlayout_test 5.1 surround.flac

=cut

use strict;
use warnings;
use Getopt::Long ":config" => "require_order";
use Pod::Usage;

GetOptions (
  "help|usage|?|h" => sub { pod2usage({ -verbose => 1, -exitval => 0 }) },
  "manpage|m"      => sub { pod2usage({ -verbose => 2, -exitval => 0 }) },
) and @ARGV >= 2 or pod2usage({ -verbose => 1, -exitval => 1 });

my $channels = shift @ARGV;
my @out_options = @ARGV;

my $ffmpeg = exists $ENV{FFMPEG} ? $ENV{FFMPEG} :
             $0 =~ /(.*)\// && -e "$1/../ffmpeg" ? "$1/../ffmpeg" :
             "ffmpeg";

my %channel_label_to_descr;
my %layout_to_channels;

{
  open my $stderr, ">&STDERR";
  open STDERR, ">", "/dev/null";
  open my $f, "-|", $ffmpeg, "-layouts" or die "$ffmpeg: $!\n";
  open STDERR, ">&", $stderr;
  while (<$f>) {
    chomp;
    next if /^NAME/ or /:$/ or /^$/; # skip headings
    my ($name, $descr) = split " ", $_, 2;
    next unless $descr;
    if ($descr =~ /^[[:upper:]]+(?:\+[[:upper:]]+)*$/) {
      $layout_to_channels{$name} = [ split /\+/, $descr ];
    } else {
      $channel_label_to_descr{$name} = $descr;
    }
  }
}

my @channels = map { @{$layout_to_channels{$_} // [$_]} } split /\+/, $channels;

my $layout = join "+", @channels;
my $graph = "";
my $concat_in = "";
for my $i (0 .. $#channels) {
  my $label = $channels[$i];
  my $descr = $channel_label_to_descr{$label}
    or die "Channel $label not found\n";
  $graph .= "flite=text='${descr}', aformat=channel_layouts=mono, " .
            "pan=${layout}:${label}=c0 [ch$i] ;\n";
  $concat_in .= "[ch$i] ";
}
$graph .= "${concat_in}concat=v=0:a=1:n=" . scalar(@channels);

exec $ffmpeg, "-f", "lavfi", "-i", $graph, @out_options
  or die "$ffmpeg: $!\n";