@@ -24,9 +24,15 @@ size_t AVWrapperFrame::getBufferSize() const {
24
24
return av_image_get_buffer_size (format, frame->width , frame->height , 1 );
25
25
} else if (AVWrapperType::Audio) {
26
26
AVSampleFormat format = static_cast <AVSampleFormat>(frame->format );
27
- // return av_samples_get_buffer_size(nullptr, frame->channels, frame->nb_samples, format, 1);
28
- int planes = av_sample_fmt_is_planar (format) ? frame->ch_layout .nb_channels : 1 ;
29
- return frame->linesize [0 ] * planes;
27
+ int channels = 1 ;
28
+ if (av_sample_fmt_is_planar (format)) {
29
+ #if (LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100))
30
+ channels = frame->ch_layout .nb_channels ;
31
+ #else
32
+ channels = frame->channels ;
33
+ #endif
34
+ };
35
+ return frame->linesize [0 ] * channels;
30
36
} else {
31
37
xassert (0 );
32
38
return 0 ;
@@ -49,17 +55,24 @@ size_t AVWrapperFrame::copyBuffer(uint8_t** buffer) const {
49
55
return buffer_size;
50
56
} else if (AVWrapperType::Audio) {
51
57
AVSampleFormat format = static_cast <AVSampleFormat>(frame->format );
52
- if (av_sample_fmt_is_planar (format) && 1 < frame->ch_layout .nb_channels ) {
58
+ int channels = 1 ;
59
+ if (av_sample_fmt_is_planar (format)) {
60
+ #if (LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100))
61
+ channels = frame->ch_layout .nb_channels ;
62
+ #else
63
+ channels = frame->channels ;
64
+ #endif
65
+ };
66
+ if (1 < channels) {
53
67
// We need to pack planar audio
54
- int planes = frame->ch_layout .nb_channels ;
55
68
size_t bytes_sample = av_get_bytes_per_sample (format);
56
- xassert (buffer_size == frame->linesize [0 ] * planes );
69
+ xassert (buffer_size == frame->linesize [0 ] * channels );
57
70
// Store each plane sample as interleaved
58
71
for (int si = 0 ; si < frame->nb_samples ; ++si) {
59
72
size_t srcoff = si * bytes_sample;
60
73
xassert (srcoff + bytes_sample <= frame->linesize [0 ]);
61
- for (int pi = 0 ; pi < planes ; pi ++) {
62
- size_t dstoff = (si * planes + pi ) * bytes_sample;
74
+ for (int pi = 0 ; pi < channels ; pi ++) {
75
+ size_t dstoff = (si * channels + pi ) * bytes_sample;
63
76
xassert (dstoff + bytes_sample <= buffer_size);
64
77
memcpy (*buffer + dstoff, frame->data [pi ] + srcoff, bytes_sample);
65
78
}
@@ -120,7 +133,12 @@ int AVWrapper::close() {
120
133
if (filterGraph) {
121
134
avfilter_graph_free (&filterGraph);
122
135
}
136
+ #if (LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100))
123
137
swrChannelLayout = {};
138
+ #else
139
+ swrChannelLayout = 0 ;
140
+ swrChannels = 0 ;
141
+ #endif
124
142
swrSampleRate = 0 ;
125
143
swrFormat = AV_SAMPLE_FMT_NONE;
126
144
#endif
@@ -162,8 +180,13 @@ void AVWrapper::pullFilterAudio() {
162
180
break ;
163
181
}
164
182
183
+ #if (LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100))
184
+ int nb_channels = swrChannelLayout.nb_channels ;
185
+ #else
186
+ int nb_channels = av_get_channel_layout_nb_channels (swrChannelLayout);
187
+ #endif
165
188
int bytes_per_sample = av_get_bytes_per_sample (swrFormat);
166
- int data_size = converted->frame ->nb_samples * swrChannelLayout. nb_channels * bytes_per_sample;
189
+ int data_size = converted->frame ->nb_samples * nb_channels * bytes_per_sample;
167
190
converted->frame ->linesize [0 ] = data_size;
168
191
converted->type = AVWrapperType::Audio;
169
192
converted->time_base = av_buffersink_get_time_base (buffersinkCtx);
@@ -337,8 +360,13 @@ bool AVWrapper::setupAudioConverter(AVSampleFormat dst_format, int dst_channels,
337
360
}
338
361
#ifdef PERIMETER_FFMPEG_MOVIE
339
362
swrFormat = dst_format != 0 ? dst_format : audioCodecCtx->sample_fmt ;
363
+ #if (LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100))
340
364
int num_channel = dst_channels != 0 ? dst_channels : audioCodecCtx->ch_layout .nb_channels ;
341
365
av_channel_layout_default (&swrChannelLayout, num_channel);
366
+ #else
367
+ swrChannels = dst_channels != 0 ? dst_channels : audioCodecCtx->channels ;
368
+ swrChannelLayout = av_get_default_channel_layout (swrChannels);
369
+ #endif
342
370
swrSampleRate = dst_frequency != 0 ? dst_frequency : audioCodecCtx->sample_rate ;
343
371
if (swrFormat == AV_SAMPLE_FMT_NONE) {
344
372
fprintf (stderr, " setupAudioConverter unsupported device output format %d at %s\n " , dst_format, file_path.c_str ());
@@ -356,12 +384,24 @@ bool AVWrapper::setupAudioConverter(AVSampleFormat dst_format, int dst_channels,
356
384
AVRational time_base = audioCodecCtx->time_base ;
357
385
358
386
char channel_str[128 ];
387
+ #if (LIBAVUTIL_VERSION_INT < AV_VERSION_INT(57, 28, 100))
388
+ const int64_t out_channel_layouts[] = { swrChannelLayout, -1 };
389
+ if (!audioCodecCtx->channel_layout ) {
390
+ audioCodecCtx->channel_layout = av_get_default_channel_layout (audioCodecCtx->channels );
391
+ }
392
+ snprintf (channel_str, sizeof (channel_str), " 0x%" PRIx64, audioCodecCtx->channel_layout );
393
+ #else
359
394
av_channel_layout_describe (&swrChannelLayout, channel_str, sizeof (channel_str));
395
+ #endif
396
+
360
397
std::string args = " time_base=" + std::to_string (time_base.num ) + " /" + std::to_string (time_base.den ) +
361
398
" :sample_rate=" + std::to_string (audioCodecCtx->sample_rate ) +
362
399
" :sample_fmt=" + av_get_sample_fmt_name (audioCodecCtx->sample_fmt ) +
363
400
" :channel_layout=" + channel_str;
364
401
402
+ #if (LIBAVUTIL_VERSION_INT < AV_VERSION_INT(57, 28, 100))
403
+ av_get_channel_layout_string (channel_str, sizeof (channel_str), swrChannels, swrChannelLayout);
404
+ #endif
365
405
std::string filters_descr;
366
406
367
407
// Add aresample filter if sample rates mismatch
@@ -411,15 +451,19 @@ bool AVWrapper::setupAudioConverter(AVSampleFormat dst_format, int dst_channels,
411
451
goto end;
412
452
}
413
453
454
+ #if (LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100))
414
455
ret = av_opt_set (buffersinkCtx, " ch_layouts" , channel_str, AV_OPT_SEARCH_CHILDREN);
456
+ #else
457
+ ret = av_opt_set_int_list (buffersinkCtx, " channel_layouts" , out_channel_layouts, -1 , AV_OPT_SEARCH_CHILDREN);
458
+ #endif
415
459
if (ret < 0 ) {
416
460
fprintf (stderr, " setupAudioConverter Cannot set output channel layout\n " );
417
461
goto end;
418
462
}
419
463
420
464
ret = av_opt_set_int_list (buffersinkCtx, " sample_rates" , out_sample_rates, -1 , AV_OPT_SEARCH_CHILDREN);
421
465
if (ret < 0 ) {
422
- fprintf (stderr, " setupAudioConverter Cannot set output sample rate\n " );
466
+ fprintf (stderr, " Cannot set output sample rate\n " );
423
467
goto end;
424
468
}
425
469
@@ -462,7 +506,11 @@ bool AVWrapper::setupAudioConverter(AVSampleFormat dst_format, int dst_channels,
462
506
463
507
/* Print summary of the sink buffer */
464
508
outlink = buffersinkCtx->inputs [0 ];
509
+ #if (LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100))
465
510
av_channel_layout_describe (&outlink->ch_layout , channel_str, sizeof (channel_str));
511
+ #else
512
+ av_get_channel_layout_string (channel_str, sizeof (channel_str), -1 , outlink->channel_layout );
513
+ #endif
466
514
fprintf (
467
515
stdout, " AVWrapper audio conv: %s\n -> %s\n -> %dHz %s %s\n " ,
468
516
args.c_str (), filters_descr.c_str (),
0 commit comments