35static CLG_LogRef
LOG = {
"video.write"};
38static bool request_float_audio_buffer(
int codec_id)
40 return ELEM(codec_id, AV_CODEC_ID_AAC, AV_CODEC_ID_AC3, AV_CODEC_ID_VORBIS);
45static int write_audio_frame(MovieWriter *context)
47 AVFrame *frame =
nullptr;
48 AVCodecContext *c =
context->audio_codec;
50 bke::sound_device_read(
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;
62 av_channel_layout_copy(&frame->ch_layout, &c->ch_layout);
63 const int num_channels = c->ch_layout.nb_channels;
66 if (
context->audio_deinterleave) {
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,
75 (num_channels * i + channel) *
context->audio_sample_size,
80 temp =
context->audio_deinterleave_buffer;
82 context->audio_input_buffer = temp;
85 avcodec_fill_audio_frame(frame,
89 context->audio_input_samples * num_channels *
95 char error_str[AV_ERROR_MAX_STRING_SIZE];
96 int ret = avcodec_send_frame(c, frame);
99 av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE,
ret);
104 AVPacket *pkt = av_packet_alloc();
108 ret = avcodec_receive_packet(c, pkt);
109 if (
ret == AVERROR(EAGAIN) ||
ret == AVERROR_EOF) {
113 av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE,
ret);
114 CLOG_ERROR(&
LOG,
"Error encoding audio frame: %s", error_str);
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
124 pkt->flags |= AV_PKT_FLAG_KEY;
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);
135 av_packet_free(&pkt);
136 av_frame_free(&frame);
142bool movie_audio_open(MovieWriter *context,
150# ifdef WITH_AUDASPACE
152 AVCodecContext *c =
context->audio_codec;
154 aud::DeviceSpecs specs;
155# ifdef FFMPEG_USE_OLD_CHANNEL_VARS
156 specs.channels = aud::Channels(c->channels);
158 specs.channels = aud::Channels(c->ch_layout.nb_channels);
161 switch (av_get_packed_sample_fmt(c->sample_fmt)) {
162 case AV_SAMPLE_FMT_U8:
163 specs.format = aud::FORMAT_U8;
165 case AV_SAMPLE_FMT_S16:
166 specs.format = aud::FORMAT_S16;
168 case AV_SAMPLE_FMT_S32:
169 specs.format = aud::FORMAT_S32;
171 case AV_SAMPLE_FMT_FLT:
172 specs.format = aud::FORMAT_FLOAT32;
174 case AV_SAMPLE_FMT_DBL:
175 specs.format = aud::FORMAT_FLOAT64;
183 specs.rate = mixrate;
185 context->audio_mixdown_device = BKE_sound_mixdown(scene, specs, start_frame, volume);
189 UNUSED_VARS(context, scene, start_frame, mixrate, volume, reports);
194void movie_audio_close(MovieWriter *context,
bool is_autosplit)
196# ifdef WITH_AUDASPACE
198 context->audio_mixdown_device.reset();
205AVStream *alloc_audio_stream(MovieWriter *context,
215 const AVCodec *codec;
219 st = avformat_new_stream(of,
nullptr);
225 codec = avcodec_find_encoder(codec_id);
228 context->audio_codec =
nullptr;
232 int channel_layout_mask = 0;
233 int channel_count = 0;
234 switch (audio_channels) {
236 channel_layout_mask = AV_CH_LAYOUT_MONO;
240 channel_layout_mask = AV_CH_LAYOUT_STEREO;
244 channel_layout_mask = AV_CH_LAYOUT_QUAD;
248 channel_layout_mask = AV_CH_LAYOUT_5POINT1_BACK;
252 channel_layout_mask = AV_CH_LAYOUT_7POINT1;
263 case AV_CODEC_ID_MP2:
264 if (
context->ffmpeg_audio_bitrate > 384) {
265 context->ffmpeg_audio_bitrate = 384;
268 "The audio is rendered with a bitrate of 384kbit/s, the maximum bitrate MP2 "
272 case AV_CODEC_ID_MP3:
273 if (
context->ffmpeg_audio_bitrate > 320) {
274 context->ffmpeg_audio_bitrate = 320;
277 "The audio is rendered with a bitrate of 320kbit/s, the maximum bitrate MP3 "
281 case AV_CODEC_ID_AAC:
282 if (
context->ffmpeg_audio_bitrate > 250 * channel_count) {
288 "The audio is rendered with a bitrate of roughly 250kbit/s per channel, the maximum "
289 "bitrate AAC supports.");
292 case AV_CODEC_ID_AC3:
293 if (
context->ffmpeg_audio_bitrate > 640) {
294 context->ffmpeg_audio_bitrate = 640;
297 "The audio is rendered with a bitrate of 640kbit/s, the maximum bitrate AC3 "
301 case AV_CODEC_ID_OPUS:
302 if (
context->ffmpeg_audio_bitrate > 256 * channel_count) {
303 context->ffmpeg_audio_bitrate = 256 * channel_count;
306 "The audio is rendered with a bitrate of 256kbit/s per channel, the maximum "
307 "bitrate Opus supports.");
310 case AV_CODEC_ID_VORBIS:
311 if (
context->ffmpeg_audio_bitrate > 240 * channel_count) {
312 context->ffmpeg_audio_bitrate = 240 * channel_count;
315 "The audio is rendered with a bitrate of 240kbit/s per channel, the maximum "
316 "bitrate Vorbis supports.");
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;
329 c->sample_rate = audio_mixrate;
330 c->bit_rate =
context->ffmpeg_audio_bitrate * 1000;
331 c->sample_fmt = AV_SAMPLE_FMT_S16;
333# ifdef FFMPEG_USE_OLD_CHANNEL_VARS
334 c->channels = audio_channels;
335 c->channel_layout = channel_layout_mask;
337 av_channel_layout_from_mask(&c->ch_layout, channel_layout_mask);
340 if (request_float_audio_buffer(codec_id)) {
342 c->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
343 c->sample_fmt = AV_SAMPLE_FMT_FLT;
353 const enum AVSampleFormat *p = sample_fmts;
354 for (; *p != -1; p++) {
355 if (*p == c->sample_fmt) {
361 c->sample_fmt = sample_fmts[0];
366 if (supported_samplerates) {
367 const int *p = supported_samplerates;
371 int dist =
abs(c->sample_rate - *p);
372 if (dist < best_dist) {
378 c->sample_rate = best;
381 if (of->oformat->flags & AVFMT_GLOBALHEADER) {
382 c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
385 int ret = avcodec_open2(c, codec,
nullptr);
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;
399 c->time_base.num = 1;
400 c->time_base.den = c->sample_rate;
402 if (c->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE) {
407 context->audio_input_samples = 1024;
410 context->audio_input_samples = c->frame_size;
413 context->audio_deinterleave = av_sample_fmt_is_planar(c->sample_fmt);
415 context->audio_sample_size = av_get_bytes_per_sample(c->sample_fmt);
418 audio_channels *
context->audio_sample_size);
419 if (
context->audio_deinterleave) {
421 context->audio_input_samples * audio_channels *
context->audio_sample_size);
426 avcodec_parameters_from_context(st->codecpar, c);
431void write_audio_frames(MovieWriter *context,
double to_pts)
433# ifdef WITH_AUDASPACE
434 AVCodecContext *c =
context->audio_codec;
436 while (
context->audio_stream) {
437 if ((
context->audio_time_total >= to_pts) || !write_audio_frame(context)) {
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);
#define CLOG_ERROR(clg_ref,...)
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)
int context(const bContext *C, const char *member, bContextDataResult *result)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ 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)