[Libav-user] new channels api in 5.1 and up?

Andrew Randrianasulu randrianasulu at gmail.com
Mon Apr 8 14:33:05 EEST 2024


Hello!

I am trying to adapt our program to API removed in ffmpeg 7.0

https://git.cinelerra-gg.org/git/?p=goodguy/cinelerra.git;a=blob;f=cinelerra-5.1/cinelerra/fileac3.C;h=dd3b4536d0d0997ba90001474ead3f520e828123;hb=HEAD

currently it reads

 codec = avcodec_find_encoder(AV_CODEC_ID_AC3);
 131                 if(!codec)
 132                 {
 133                         eprintf(_("FileAC3::open_file codec not
found.\n"));
 134                         result = 1;
 135                 }
 136                 if( !result && !(fd = fopen(asset->path, "w")))
 137                 {
 138                         eprintf(_("Error while opening \"%s\" for
writing. \n%m\n"), asset->path);
 139                         result = 1;
 140                 }
 141                 if( !result ) {
 142                         int channels = asset->channels;
 143                         int sample_rate = asset->sample_rate;
 144                         int64_t layout = get_channel_layout(channels);
 145                         int bitrate = asset->ac3_bitrate * 1000;
 146                         av_init_packet(&avpkt);
 147                         codec_context = avcodec_alloc_context3(codec);
 148                         codec_context->bit_rate = bitrate;
 149                         codec_context->sample_rate = sample_rate;
 150                         codec_context->channels = channels;
 151                         codec_context->channel_layout = layout;
 152                         codec_context->sample_fmt =
codec->sample_fmts[0];
 153                         resample_context = swr_alloc_set_opts(NULL,
 154                                         layout,
codec_context->sample_fmt, sample_rate,
 155                                         layout, AV_SAMPLE_FMT_S16,
sample_rate,
 156                                         0, NULL);
 157                         swr_init(resample_context);
 158                         if(avcodec_open2(codec_context, codec, 0))
 159                         {
 160                                 eprintf(_("FileAC3::open_file failed
to open codec.\n"));
 161                                 result = 1;
 162                         }
 163                 }

I tried to mod it like this

if( !result ) {
int channels = asset->channels;
int sample_rate = asset->sample_rate;
int64_t layout = get_channel_layout(channels);
AVChannelLayout ch_layout;
av_channel_layout_default(&ch_layout, channels);
int bitrate = asset->ac3_bitrate * 1000;
av_init_packet(&avpkt);
codec_context = avcodec_alloc_context3(codec);
codec_context->bit_rate = bitrate;
codec_context->sample_rate = sample_rate;
//codec_context->ch_layout.nb_channels = channels;
//codec_context->ch_layout.u.mask = layout;
av_channel_layout_copy(&codec_context->ch_layout, &ch_layout);
codec_context->sample_fmt = codec->sample_fmts[0];
SwrContext *resample_context = NULL;
swr_alloc_set_opts2(&resample_context,
&ch_layout, codec_context->sample_fmt, sample_rate,
&ch_layout, AV_SAMPLE_FMT_S16, sample_rate,
0, NULL);
swr_init(resample_context);
if(avcodec_open2(codec_context, codec, 0))
{
eprintf(_("FileAC3::open_file failed to open codec.\n"));
result = 1;
}

but unfortunately resulting file is empty ???

I tried to augment another function down in file, FileAC3::write_samples,
by av_channel_layout_copy() too but then  whole program crash :) :(

strangely enough similar changes in ffmpeg.C works, even if I initially
tried to  adapt fileac3.C

Other cinelerra forks currently use older ffmpeg so I am on my own here.

Attached two patches against cinelerra-gg git where I tried to make it
compile and work. It compiles and partially works, but ac3 encoding is a
bust ...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20240408/46f8a278/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0005-Fix-audio-encoding-with-ffmpeg-7.0.patch
Type: text/x-patch
Size: 1288 bytes
Desc: not available
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20240408/46f8a278/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-Adapt-to-ffmpeg-7.0-drop-ffmpeg-pre-5.1-support.patch
Type: text/x-patch
Size: 12701 bytes
Desc: not available
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20240408/46f8a278/attachment-0001.bin>


More information about the Libav-user mailing list