|
6 | 6 | namespace ffmpeg { |
7 | 7 |
|
8 | 8 | namespace { |
| 9 | +static int get_nb_channels(const AVFrame* frame, const AVCodecContext* codec) { |
| 10 | +#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100) |
| 11 | + return frame ? frame->ch_layout.nb_channels : codec->ch_layout.nb_channels; |
| 12 | +#else |
| 13 | + return frame ? frame->channels : codec->channels; |
| 14 | +#endif |
| 15 | +} |
| 16 | + |
9 | 17 | bool operator==(const AudioFormat& x, const AVFrame& y) { |
10 | 18 | return x.samples == static_cast<size_t>(y.sample_rate) && |
11 | | - x.channels == static_cast<size_t>(y.channels) && x.format == y.format; |
| 19 | + x.channels == static_cast<size_t>(get_nb_channels(&y, nullptr)) && |
| 20 | + x.format == y.format; |
12 | 21 | } |
13 | 22 |
|
14 | 23 | bool operator==(const AudioFormat& x, const AVCodecContext& y) { |
15 | 24 | return x.samples == static_cast<size_t>(y.sample_rate) && |
16 | | - x.channels == static_cast<size_t>(y.channels) && x.format == y.sample_fmt; |
| 25 | + x.channels == static_cast<size_t>(get_nb_channels(nullptr, &y)) && |
| 26 | + x.format == y.sample_fmt; |
17 | 27 | } |
18 | 28 |
|
19 | 29 | AudioFormat& toAudioFormat(AudioFormat& x, const AVFrame& y) { |
20 | 30 | x.samples = y.sample_rate; |
21 | | - x.channels = y.channels; |
| 31 | + x.channels = get_nb_channels(&y, nullptr); |
22 | 32 | x.format = y.format; |
23 | 33 | return x; |
24 | 34 | } |
25 | 35 |
|
26 | 36 | AudioFormat& toAudioFormat(AudioFormat& x, const AVCodecContext& y) { |
27 | 37 | x.samples = y.sample_rate; |
28 | | - x.channels = y.channels; |
| 38 | + x.channels = get_nb_channels(nullptr, &y); |
29 | 39 | x.format = y.sample_fmt; |
30 | 40 | return x; |
31 | 41 | } |
@@ -54,9 +64,15 @@ int AudioStream::initFormat() { |
54 | 64 | if (format_.format.audio.samples == 0) { |
55 | 65 | format_.format.audio.samples = codecCtx_->sample_rate; |
56 | 66 | } |
| 67 | +#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100) |
| 68 | + if (format_.format.audio.channels == 0) { |
| 69 | + format_.format.audio.channels = codecCtx_->ch_layout.nb_channels; |
| 70 | + } |
| 71 | +#else |
57 | 72 | if (format_.format.audio.channels == 0) { |
58 | 73 | format_.format.audio.channels = codecCtx_->channels; |
59 | 74 | } |
| 75 | +#endif |
60 | 76 | if (format_.format.audio.format == AV_SAMPLE_FMT_NONE) { |
61 | 77 | format_.format.audio.format = codecCtx_->sample_fmt; |
62 | 78 | } |
|
0 commit comments