Blender V5.3
movie_write_audio.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#ifdef _MSC_VER
10/* This needs to be included first to prevent FFmpeg's headers adding defines for various math
11 * constants leading to duplicate definitions. */
12# include <cmath>
13#endif
14
15#include "movie_util.hh"
16#include "movie_write.hh"
17
18#ifdef WITH_FFMPEG
19
20# include <cstdio>
21# include <cstring>
22
23# include "DNA_scene_types.h"
24
25# include "BLI_string.hh"
26# include "BLI_utildefines.hh"
27
28# include "BKE_report.hh"
29# include "BKE_sound.hh"
30
31# include "CLG_log.h"
32
33namespace blender {
34
35static CLG_LogRef LOG = {"video.write"};
36
37/* If any of these codecs, we prefer the float sample format (if supported) */
38static bool request_float_audio_buffer(int codec_id)
39{
40 return ELEM(codec_id, AV_CODEC_ID_AAC, AV_CODEC_ID_AC3, AV_CODEC_ID_VORBIS);
41}
42
43# ifdef WITH_AUDASPACE
44
45static int write_audio_frame(MovieWriter *context)
46{
47 AVFrame *frame = nullptr;
48 AVCodecContext *c = context->audio_codec;
49
50 bke::sound_device_read(
51 context->audio_mixdown_device, context->audio_input_buffer, context->audio_input_samples);
52
53 frame = av_frame_alloc();
54 frame->pts = context->audio_time / av_q2d(c->time_base);
55 frame->nb_samples = context->audio_input_samples;
56 frame->format = c->sample_fmt;
57# ifdef FFMPEG_USE_OLD_CHANNEL_VARS
58 frame->channels = c->channels;
59 frame->channel_layout = c->channel_layout;
60 const int num_channels = c->channels;
61# else
62 av_channel_layout_copy(&frame->ch_layout, &c->ch_layout);
63 const int num_channels = c->ch_layout.nb_channels;
64# endif
65
66 if (context->audio_deinterleave) {
67 int channel, i;
68 uint8_t *temp;
69
70 for (channel = 0; channel < num_channels; channel++) {
71 for (i = 0; i < frame->nb_samples; i++) {
72 memcpy(context->audio_deinterleave_buffer +
73 (i + channel * frame->nb_samples) * context->audio_sample_size,
74 context->audio_input_buffer +
75 (num_channels * i + channel) * context->audio_sample_size,
76 context->audio_sample_size);
77 }
78 }
79
80 temp = context->audio_deinterleave_buffer;
81 context->audio_deinterleave_buffer = context->audio_input_buffer;
82 context->audio_input_buffer = temp;
83 }
84
85 avcodec_fill_audio_frame(frame,
86 num_channels,
87 c->sample_fmt,
88 context->audio_input_buffer,
89 context->audio_input_samples * num_channels *
90 context->audio_sample_size,
91 1);
92
93 int success = 1;
94
95 char error_str[AV_ERROR_MAX_STRING_SIZE];
96 int ret = avcodec_send_frame(c, frame);
97 if (ret < 0) {
98 /* Can't send frame to encoder. This shouldn't happen. */
99 av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE, ret);
100 CLOG_ERROR(&LOG, "Can't send audio frame: %s", error_str);
101 success = -1;
102 }
103
104 AVPacket *pkt = av_packet_alloc();
105
106 while (ret >= 0) {
107
108 ret = avcodec_receive_packet(c, pkt);
109 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
110 break;
111 }
112 if (ret < 0) {
113 av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE, ret);
114 CLOG_ERROR(&LOG, "Error encoding audio frame: %s", error_str);
115 success = -1;
116 }
117
118 pkt->stream_index = context->audio_stream->index;
119 av_packet_rescale_ts(pkt, c->time_base, context->audio_stream->time_base);
120# ifdef FFMPEG_USE_DURATION_WORKAROUND
121 my_guess_pkt_duration(context->outfile, context->audio_stream, pkt);
122# endif
123
124 pkt->flags |= AV_PKT_FLAG_KEY;
125
126 int write_ret = av_interleaved_write_frame(context->outfile, pkt);
127 if (write_ret != 0) {
128 av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE, ret);
129 CLOG_ERROR(&LOG, "Error writing audio packet: %s", error_str);
130 success = -1;
131 break;
132 }
133 }
134
135 av_packet_free(&pkt);
136 av_frame_free(&frame);
137
138 return success;
139}
140# endif /* #ifdef WITH_AUDASPACE */
141
142bool movie_audio_open(MovieWriter *context,
143 const Scene *scene,
144 int start_frame,
145 int mixrate,
146 float volume,
147 ReportList *reports)
148{
149 bool success = true;
150# ifdef WITH_AUDASPACE
151 if (context->audio_stream) {
152 AVCodecContext *c = context->audio_codec;
153
154 aud::DeviceSpecs specs;
155# ifdef FFMPEG_USE_OLD_CHANNEL_VARS
156 specs.channels = aud::Channels(c->channels);
157# else
158 specs.channels = aud::Channels(c->ch_layout.nb_channels);
159# endif
160
161 switch (av_get_packed_sample_fmt(c->sample_fmt)) {
162 case AV_SAMPLE_FMT_U8:
163 specs.format = aud::FORMAT_U8;
164 break;
165 case AV_SAMPLE_FMT_S16:
166 specs.format = aud::FORMAT_S16;
167 break;
168 case AV_SAMPLE_FMT_S32:
169 specs.format = aud::FORMAT_S32;
170 break;
171 case AV_SAMPLE_FMT_FLT:
172 specs.format = aud::FORMAT_FLOAT32;
173 break;
174 case AV_SAMPLE_FMT_DBL:
175 specs.format = aud::FORMAT_FLOAT64;
176 break;
177 default:
178 BKE_report(reports, RPT_ERROR, "Audio sample format unsupported");
179 success = false;
180 break;
181 }
182
183 specs.rate = mixrate;
184 if (success) {
185 context->audio_mixdown_device = BKE_sound_mixdown(scene, specs, start_frame, volume);
186 }
187 }
188# else
189 UNUSED_VARS(context, scene, start_frame, mixrate, volume, reports);
190# endif
191 return success;
192}
193
194void movie_audio_close(MovieWriter *context, bool is_autosplit)
195{
196# ifdef WITH_AUDASPACE
197 if (!is_autosplit) {
198 context->audio_mixdown_device.reset();
199 }
200# else
201 UNUSED_VARS(context, is_autosplit);
202# endif
203}
204
205AVStream *alloc_audio_stream(MovieWriter *context,
206 int audio_mixrate,
207 int audio_channels,
208 AVCodecID codec_id,
209 AVFormatContext *of,
210 char *error,
211 int error_maxncpy,
212 ReportList *reports)
213{
214 AVStream *st;
215 const AVCodec *codec;
216
217 error[0] = '\0';
218
219 st = avformat_new_stream(of, nullptr);
220 if (!st) {
221 return nullptr;
222 }
223 st->id = 1;
224
225 codec = avcodec_find_encoder(codec_id);
226 if (!codec) {
227 CLOG_ERROR(&LOG, "Couldn't find valid audio codec");
228 context->audio_codec = nullptr;
229 return nullptr;
230 }
231
232 int channel_layout_mask = 0;
233 int channel_count = 0;
234 switch (audio_channels) {
236 channel_layout_mask = AV_CH_LAYOUT_MONO;
237 channel_count = 1;
238 break;
240 channel_layout_mask = AV_CH_LAYOUT_STEREO;
241 channel_count = 2;
242 break;
244 channel_layout_mask = AV_CH_LAYOUT_QUAD;
245 channel_count = 4;
246 break;
248 channel_layout_mask = AV_CH_LAYOUT_5POINT1_BACK;
249 channel_count = 6;
250 break;
252 channel_layout_mask = AV_CH_LAYOUT_7POINT1;
253 channel_count = 8;
254 break;
255 default:
256 BLI_assert(false);
257 break;
258 }
259
260 /* Clamp audio bitrate and report info if bitrate is set higher than the maximum bitrate of the
261 * codec. */
262 switch (codec_id) {
263 case AV_CODEC_ID_MP2:
264 if (context->ffmpeg_audio_bitrate > 384) {
265 context->ffmpeg_audio_bitrate = 384;
266 BKE_report(reports,
267 RPT_INFO,
268 "The audio is rendered with a bitrate of 384kbit/s, the maximum bitrate MP2 "
269 "supports.");
270 }
271 break;
272 case AV_CODEC_ID_MP3:
273 if (context->ffmpeg_audio_bitrate > 320) {
274 context->ffmpeg_audio_bitrate = 320;
275 BKE_report(reports,
276 RPT_INFO,
277 "The audio is rendered with a bitrate of 320kbit/s, the maximum bitrate MP3 "
278 "supports.");
279 }
280 break;
281 case AV_CODEC_ID_AAC:
282 if (context->ffmpeg_audio_bitrate > 250 * channel_count) {
283 /* AAC doesn't specify a maximum bitrate. Instead, the maximum bitrate is dependent on the
284 * encoder used. Clamping of the bitrate is therefore left to the encoder. */
286 reports,
287 RPT_INFO,
288 "The audio is rendered with a bitrate of roughly 250kbit/s per channel, the maximum "
289 "bitrate AAC supports.");
290 }
291 break;
292 case AV_CODEC_ID_AC3:
293 if (context->ffmpeg_audio_bitrate > 640) {
294 context->ffmpeg_audio_bitrate = 640;
295 BKE_report(reports,
296 RPT_INFO,
297 "The audio is rendered with a bitrate of 640kbit/s, the maximum bitrate AC3 "
298 "supports.");
299 }
300 break;
301 case AV_CODEC_ID_OPUS:
302 if (context->ffmpeg_audio_bitrate > 256 * channel_count) {
303 context->ffmpeg_audio_bitrate = 256 * channel_count;
304 BKE_report(reports,
305 RPT_INFO,
306 "The audio is rendered with a bitrate of 256kbit/s per channel, the maximum "
307 "bitrate Opus supports.");
308 }
309 break;
310 case AV_CODEC_ID_VORBIS:
311 if (context->ffmpeg_audio_bitrate > 240 * channel_count) {
312 context->ffmpeg_audio_bitrate = 240 * channel_count;
313 BKE_report(reports,
314 RPT_INFO,
315 "The audio is rendered with a bitrate of 240kbit/s per channel, the maximum "
316 "bitrate Vorbis supports.");
317 }
318 break;
319 default:
320 /* Default case for suppressing compiler warnings. */
321 break;
322 }
323
324 context->audio_codec = avcodec_alloc_context3(codec);
325 AVCodecContext *c = context->audio_codec;
326 c->thread_count = MOV_thread_count();
327 c->thread_type = FF_THREAD_SLICE;
328
329 c->sample_rate = audio_mixrate;
330 c->bit_rate = context->ffmpeg_audio_bitrate * 1000;
331 c->sample_fmt = AV_SAMPLE_FMT_S16;
332
333# ifdef FFMPEG_USE_OLD_CHANNEL_VARS
334 c->channels = audio_channels;
335 c->channel_layout = channel_layout_mask;
336# else
337 av_channel_layout_from_mask(&c->ch_layout, channel_layout_mask);
338# endif
339
340 if (request_float_audio_buffer(codec_id)) {
341 /* mainly for AAC codec which is experimental */
342 c->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
343 c->sample_fmt = AV_SAMPLE_FMT_FLT;
344 }
345
346 const enum AVSampleFormat *sample_fmts = ffmpeg_get_sample_fmts(c, codec);
347 if (sample_fmts) {
348 /* Check if the preferred sample format for this codec is supported.
349 * this is because, depending on the version of LIBAV,
350 * and with the whole FFMPEG/LIBAV fork situation,
351 * you have various implementations around.
352 * Float samples in particular are not always supported. */
353 const enum AVSampleFormat *p = sample_fmts;
354 for (; *p != -1; p++) {
355 if (*p == c->sample_fmt) {
356 break;
357 }
358 }
359 if (*p == -1) {
360 /* sample format incompatible with codec. Defaulting to a format known to work */
361 c->sample_fmt = sample_fmts[0];
362 }
363 }
364
365 const int *supported_samplerates = ffmpeg_get_sample_rates(c, codec);
366 if (supported_samplerates) {
367 const int *p = supported_samplerates;
368 int best = 0;
369 int best_dist = INT_MAX;
370 for (; *p; p++) {
371 int dist = abs(c->sample_rate - *p);
372 if (dist < best_dist) {
373 best_dist = dist;
374 best = *p;
375 }
376 }
377 /* best is the closest supported sample rate (same as selected if best_dist == 0) */
378 c->sample_rate = best;
379 }
380
381 if (of->oformat->flags & AVFMT_GLOBALHEADER) {
382 c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
383 }
384
385 int ret = avcodec_open2(c, codec, nullptr);
386
387 if (ret < 0) {
388 char error_str[AV_ERROR_MAX_STRING_SIZE];
389 av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE, ret);
390 CLOG_ERROR(&LOG, "Couldn't initialize audio codec: %s", error_str);
391 BLI_strncpy(error, ffmpeg_last_error(), error_maxncpy);
392 avcodec_free_context(&c);
393 context->audio_codec = nullptr;
394 return nullptr;
395 }
396
397 /* Need to prevent floating point exception when using VORBIS audio codec,
398 * initialize this value in the same way as it's done in FFMPEG itself (sergey) */
399 c->time_base.num = 1;
400 c->time_base.den = c->sample_rate;
401
402 if (c->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE) {
403 /* If the audio format has a variable frame size, default to 1024.
404 * This is because we won't try to encode any variable frame size.
405 * 1024 seems to be a good compromise between size and speed.
406 */
407 context->audio_input_samples = 1024;
408 }
409 else {
410 context->audio_input_samples = c->frame_size;
411 }
412
413 context->audio_deinterleave = av_sample_fmt_is_planar(c->sample_fmt);
414
415 context->audio_sample_size = av_get_bytes_per_sample(c->sample_fmt);
416
417 context->audio_input_buffer = (uint8_t *)av_malloc(context->audio_input_samples *
418 audio_channels * context->audio_sample_size);
419 if (context->audio_deinterleave) {
420 context->audio_deinterleave_buffer = (uint8_t *)av_malloc(
421 context->audio_input_samples * audio_channels * context->audio_sample_size);
422 }
423
424 context->audio_time = 0.0f;
425
426 avcodec_parameters_from_context(st->codecpar, c);
427
428 return st;
429}
430
431void write_audio_frames(MovieWriter *context, double to_pts)
432{
433# ifdef WITH_AUDASPACE
434 AVCodecContext *c = context->audio_codec;
435
436 while (context->audio_stream) {
437 if ((context->audio_time_total >= to_pts) || !write_audio_frame(context)) {
438 break;
439 }
440 context->audio_time_total += double(context->audio_input_samples) / double(c->sample_rate);
441 context->audio_time += double(context->audio_input_samples) / double(c->sample_rate);
442 }
443# else
444 UNUSED_VARS(context, to_pts);
445# endif
446}
447
448} // namespace blender
449
450#endif /* WITH_FFMPEG */
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define UNUSED_VARS(...)
#define ELEM(...)
FFMPEG_INLINE enum AVSampleFormat * ffmpeg_get_sample_fmts(struct AVCodecContext *context, const AVCodec *codec)
FFMPEG_INLINE const int * ffmpeg_get_sample_rates(struct AVCodecContext *context, const AVCodec *codec)
FFMPEG_INLINE void my_guess_pkt_duration(AVFormatContext *s, AVStream *st, AVPacket *pkt)
#define INT_MAX
#define abs
#define LOG(level)
Definition log.h:97
int context(const bContext *C, const char *member, bContextDataResult *result)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ FFM_CHANNELS_SURROUND4
@ FFM_CHANNELS_SURROUND51
@ FFM_CHANNELS_SURROUND71
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
unsigned char uint8_t
Definition BKE_text.h:79