1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
|
/*******************************************************************************
* Copyright 2009-2016 Jörg Müller
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
#include "FFMPEGWriter.h"
#include "Exception.h"
#include <algorithm>
#include <cstring>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avio.h>
#if LIBAVCODEC_VERSION_MAJOR >= 59
#include <libavutil/channel_layout.h>
#endif
}
AUD_NAMESPACE_BEGIN
#if LIBAVCODEC_VERSION_MAJOR < 58
#define FFMPEG_OLD_CODE
#endif
void FFMPEGWriter::encode()
{
sample_t* data = m_input_buffer.getBuffer();
if(m_deinterleave)
{
m_deinterleave_buffer.assureSize(m_input_buffer.getSize());
sample_t* dbuf = m_deinterleave_buffer.getBuffer();
// deinterleave
int single_size = sizeof(sample_t);
for(int channel = 0; channel < m_specs.channels; channel++)
{
for(int i = 0; i < m_input_buffer.getSize() / AUD_SAMPLE_SIZE(m_specs); i++)
{
std::memcpy(((data_t*)dbuf) + (m_input_samples * channel + i) * single_size,
((data_t*)data) + ((m_specs.channels * i) + channel) * single_size, single_size);
}
}
// convert first
if(m_input_size)
m_convert(reinterpret_cast<data_t*>(data), reinterpret_cast<data_t*>(dbuf), m_input_samples * m_specs.channels);
else
std::memcpy(data, dbuf, m_input_buffer.getSize());
}
else
// convert first
if(m_input_size)
m_convert(reinterpret_cast<data_t*>(data), reinterpret_cast<data_t*>(data), m_input_samples * m_specs.channels);
#ifdef FFMPEG_OLD_CODE
m_packet->data = nullptr;
m_packet->size = 0;
av_init_packet(m_packet);
av_frame_unref(m_frame);
int got_packet;
#endif
m_frame->nb_samples = m_input_samples;
m_frame->format = m_codecCtx->sample_fmt;
av_channel_layout_copy(&m_frame->ch_layout, &m_codecCtx->ch_layout);
if(avcodec_fill_audio_frame(m_frame, m_specs.channels, m_codecCtx->sample_fmt, reinterpret_cast<data_t*>(data), m_input_buffer.getSize(), 0) < 0)
AUD_THROW(FileException, "File couldn't be written, filling the audio frame failed with ffmpeg.");
AVRational sample_time = { 1, static_cast<int>(m_specs.rate) };
m_frame->pts = av_rescale_q(m_position - m_input_samples, m_codecCtx->time_base, sample_time);
#ifdef FFMPEG_OLD_CODE
if(avcodec_encode_audio2(m_codecCtx, m_packet, m_frame, &got_packet))
{
AUD_THROW(FileException, "File couldn't be written, audio encoding failed with ffmpeg.");
}
if(got_packet)
{
m_packet->flags |= AV_PKT_FLAG_KEY;
m_packet->stream_index = m_stream->index;
if(av_write_frame(m_formatCtx, m_packet) < 0)
{
av_free_packet(m_packet);
AUD_THROW(FileException, "Frame couldn't be writen to the file with ffmpeg.");
}
av_free_packet(m_packet);
}
#else
if(avcodec_send_frame(m_codecCtx, m_frame) < 0)
AUD_THROW(FileException, "File couldn't be written, audio encoding failed with ffmpeg.");
while(avcodec_receive_packet(m_codecCtx, m_packet) == 0)
{
m_packet->stream_index = m_stream->index;
if(av_write_frame(m_formatCtx, m_packet) < 0)
AUD_THROW(FileException, "Frame couldn't be writen to the file with ffmpeg.");
}
#endif
}
void FFMPEGWriter::close()
{
#ifdef FFMPEG_OLD_CODE
int got_packet = true;
while(got_packet)
{
m_packet->data = nullptr;
m_packet->size = 0;
av_init_packet(m_packet);
if(avcodec_encode_audio2(m_codecCtx, m_packet, nullptr, &got_packet))
AUD_THROW(FileException, "File end couldn't be written, audio encoding failed with ffmpeg.");
if(got_packet)
{
m_packet->flags |= AV_PKT_FLAG_KEY;
m_packet->stream_index = m_stream->index;
if(av_write_frame(m_formatCtx, m_packet))
{
av_free_packet(m_packet);
AUD_THROW(FileException, "Final frames couldn't be writen to the file with ffmpeg.");
}
av_free_packet(m_packet);
}
}
#else
if(avcodec_send_frame(m_codecCtx, nullptr) < 0)
AUD_THROW(FileException, "File couldn't be written, audio encoding failed with ffmpeg.");
while(avcodec_receive_packet(m_codecCtx, m_packet) == 0)
{
m_packet->stream_index = m_stream->index;
if(av_write_frame(m_formatCtx, m_packet) < 0)
AUD_THROW(FileException, "Frame couldn't be writen to the file with ffmpeg.");
}
#endif
}
FFMPEGWriter::FFMPEGWriter(const std::string &filename, DeviceSpecs specs, Container format, Codec codec, unsigned int bitrate) :
m_position(0),
m_specs(specs),
m_formatCtx(nullptr),
m_codecCtx(nullptr),
m_stream(nullptr),
m_packet(nullptr),
m_frame(nullptr),
m_input_samples(0),
m_deinterleave(false)
{
static const char* formats[] = { nullptr, "ac3", "flac", "matroska", "mp2", "mp3", "ogg", "wav" };
if(avformat_alloc_output_context2(&m_formatCtx, nullptr, formats[format], filename.c_str()) < 0)
AUD_THROW(FileException, "File couldn't be written, format couldn't be found with ffmpeg.");
const AVOutputFormat* outputFmt = m_formatCtx->oformat;
if(!outputFmt) {
avformat_free_context(m_formatCtx);
AUD_THROW(FileException, "File couldn't be written, output format couldn't be found with ffmpeg.");
}
AVCodecID audio_codec = AV_CODEC_ID_NONE;
switch(codec)
{
case CODEC_AAC:
audio_codec = AV_CODEC_ID_AAC;
break;
case CODEC_AC3:
audio_codec = AV_CODEC_ID_AC3;
break;
case CODEC_FLAC:
audio_codec = AV_CODEC_ID_FLAC;
break;
case CODEC_MP2:
audio_codec = AV_CODEC_ID_MP2;
break;
case CODEC_MP3:
audio_codec = AV_CODEC_ID_MP3;
break;
case CODEC_OPUS:
audio_codec = AV_CODEC_ID_OPUS;
break;
case CODEC_PCM:
switch(specs.format)
{
case FORMAT_U8:
audio_codec = AV_CODEC_ID_PCM_U8;
break;
case FORMAT_S16:
audio_codec = AV_CODEC_ID_PCM_S16LE;
break;
case FORMAT_S24:
audio_codec = AV_CODEC_ID_PCM_S24LE;
break;
case FORMAT_S32:
audio_codec = AV_CODEC_ID_PCM_S32LE;
break;
case FORMAT_FLOAT32:
audio_codec = AV_CODEC_ID_PCM_F32LE;
break;
case FORMAT_FLOAT64:
audio_codec = AV_CODEC_ID_PCM_F64LE;
break;
default:
audio_codec = AV_CODEC_ID_NONE;
break;
}
break;
case CODEC_VORBIS:
audio_codec = AV_CODEC_ID_VORBIS;
break;
default:
audio_codec = AV_CODEC_ID_NONE;
break;
}
AVChannelLayout channel_layout{};
switch(m_specs.channels)
{
case CHANNELS_MONO:
channel_layout = AV_CHANNEL_LAYOUT_MONO;
break;
case CHANNELS_STEREO:
channel_layout = AV_CHANNEL_LAYOUT_STEREO;
break;
case CHANNELS_STEREO_LFE:
channel_layout = AV_CHANNEL_LAYOUT_2POINT1;
break;
case CHANNELS_SURROUND4:
channel_layout = AV_CHANNEL_LAYOUT_QUAD;
break;
case CHANNELS_SURROUND5:
channel_layout = AV_CHANNEL_LAYOUT_5POINT0_BACK;
break;
case CHANNELS_SURROUND51:
channel_layout = AV_CHANNEL_LAYOUT_5POINT1_BACK;
break;
case CHANNELS_SURROUND61:
channel_layout = AV_CHANNEL_LAYOUT_6POINT1_BACK;
break;
case CHANNELS_SURROUND71:
channel_layout = AV_CHANNEL_LAYOUT_7POINT1;
break;
default:
AUD_THROW(FileException, "File couldn't be written, channel layout not supported.");
}
try
{
if(audio_codec == AV_CODEC_ID_NONE)
AUD_THROW(FileException, "File couldn't be written, audio codec not found with ffmpeg.");
const AVCodec* codec = avcodec_find_encoder(audio_codec);
if(!codec)
AUD_THROW(FileException, "File couldn't be written, audio encoder couldn't be found with ffmpeg.");
m_stream = avformat_new_stream(m_formatCtx, codec);
if(!m_stream)
AUD_THROW(FileException, "File couldn't be written, stream creation failed with ffmpeg.");
m_stream->id = m_formatCtx->nb_streams - 1;
#ifdef FFMPEG_OLD_CODE
m_codecCtx = m_stream->codec;
#else
m_codecCtx = avcodec_alloc_context3(codec);
#endif
if(!m_codecCtx)
AUD_THROW(FileException, "File couldn't be written, context creation failed with ffmpeg.");
switch(m_specs.format)
{
case FORMAT_U8:
m_convert = convert_float_u8;
m_codecCtx->sample_fmt = AV_SAMPLE_FMT_U8;
break;
case FORMAT_S16:
m_convert = convert_float_s16;
m_codecCtx->sample_fmt = AV_SAMPLE_FMT_S16;
break;
case FORMAT_S32:
m_convert = convert_float_s32;
m_codecCtx->sample_fmt = AV_SAMPLE_FMT_S32;
break;
case FORMAT_FLOAT64:
m_convert = convert_float_double;
m_codecCtx->sample_fmt = AV_SAMPLE_FMT_DBL;
break;
default:
m_convert = convert_copy<sample_t>;
m_codecCtx->sample_fmt = AV_SAMPLE_FMT_FLT;
break;
}
if(m_formatCtx->oformat->flags & AVFMT_GLOBALHEADER)
m_codecCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
bool format_supported = false;
for(int i = 0; codec->sample_fmts[i] != -1; i++)
{
if(av_get_alt_sample_fmt(codec->sample_fmts[i], false) == m_codecCtx->sample_fmt)
{
m_deinterleave = av_sample_fmt_is_planar(codec->sample_fmts[i]);
m_codecCtx->sample_fmt = codec->sample_fmts[i];
format_supported = true;
}
}
if(!format_supported)
{
int chosen_index = 0;
auto chosen = av_get_alt_sample_fmt(codec->sample_fmts[chosen_index], false);
for(int i = 1; codec->sample_fmts[i] != -1; i++)
{
auto fmt = av_get_alt_sample_fmt(codec->sample_fmts[i], false);
if((fmt > chosen && chosen < m_codecCtx->sample_fmt) || (fmt > m_codecCtx->sample_fmt && fmt < chosen))
{
chosen = fmt;
chosen_index = i;
}
}
m_codecCtx->sample_fmt = codec->sample_fmts[chosen_index];
m_deinterleave = av_sample_fmt_is_planar(m_codecCtx->sample_fmt);
switch(av_get_alt_sample_fmt(m_codecCtx->sample_fmt, false))
{
case AV_SAMPLE_FMT_U8:
specs.format = FORMAT_U8;
m_convert = convert_float_u8;
break;
case AV_SAMPLE_FMT_S16:
specs.format = FORMAT_S16;
m_convert = convert_float_s16;
break;
case AV_SAMPLE_FMT_S32:
specs.format = FORMAT_S32;
m_convert = convert_float_s32;
break;
case AV_SAMPLE_FMT_FLT:
specs.format = FORMAT_FLOAT32;
m_convert = convert_copy<sample_t>;
break;
case AV_SAMPLE_FMT_DBL:
specs.format = FORMAT_FLOAT64;
m_convert = convert_float_double;
break;
default:
AUD_THROW(FileException, "File couldn't be written, sample format not supported with ffmpeg.");
}
}
m_codecCtx->sample_rate = 0;
if(codec->supported_samplerates)
{
for(int i = 0; codec->supported_samplerates[i]; i++)
{
if(codec->supported_samplerates[i] == m_specs.rate)
{
m_codecCtx->sample_rate = codec->supported_samplerates[i];
break;
}
else if((codec->supported_samplerates[i] > m_codecCtx->sample_rate && m_specs.rate > m_codecCtx->sample_rate) ||
(codec->supported_samplerates[i] < m_codecCtx->sample_rate && m_specs.rate < codec->supported_samplerates[i]))
{
m_codecCtx->sample_rate = codec->supported_samplerates[i];
}
}
}
if(m_codecCtx->sample_rate == 0)
m_codecCtx->sample_rate = m_specs.rate;
m_specs.rate = m_codecCtx->sample_rate;
#ifdef FFMPEG_OLD_CODE
m_codecCtx->codec_id = audio_codec;
#endif
m_codecCtx->codec_type = AVMEDIA_TYPE_AUDIO;
m_codecCtx->bit_rate = bitrate;
av_channel_layout_copy(&m_codecCtx->ch_layout, &channel_layout);
m_stream->time_base.num = m_codecCtx->time_base.num = 1;
m_stream->time_base.den = m_codecCtx->time_base.den = m_codecCtx->sample_rate;
if(avcodec_open2(m_codecCtx, codec, nullptr) < 0)
AUD_THROW(FileException, "File couldn't be written, encoder couldn't be opened with ffmpeg.");
#ifndef FFMPEG_OLD_CODE
if(avcodec_parameters_from_context(m_stream->codecpar, m_codecCtx) < 0)
AUD_THROW(FileException, "File couldn't be written, codec parameters couldn't be copied to the context.");
#endif
int samplesize = std::max(int(AUD_SAMPLE_SIZE(m_specs)), AUD_DEVICE_SAMPLE_SIZE(m_specs));
if((m_input_size = m_codecCtx->frame_size))
m_input_buffer.resize(m_input_size * samplesize);
if(avio_open(&m_formatCtx->pb, filename.c_str(), AVIO_FLAG_WRITE))
AUD_THROW(FileException, "File couldn't be written, file opening failed with ffmpeg.");
if(avformat_write_header(m_formatCtx, nullptr) < 0)
AUD_THROW(FileException, "File couldn't be written, writing the header failed.");
}
catch(Exception&)
{
#ifndef FFMPEG_OLD_CODE
if(m_codecCtx)
avcodec_free_context(&m_codecCtx);
#endif
avformat_free_context(m_formatCtx);
throw;
}
#ifdef FFMPEG_OLD_CODE
m_packet = new AVPacket({});
#else
m_packet = av_packet_alloc();
#endif
m_frame = av_frame_alloc();
}
FFMPEGWriter::~FFMPEGWriter()
{
// writte missing data
if(m_input_samples)
encode();
close();
av_write_trailer(m_formatCtx);
if(m_frame)
av_frame_free(&m_frame);
if(m_packet)
{
#ifdef FFMPEG_OLD_CODE
delete m_packet;
#else
av_packet_free(&m_packet);
#endif
}
#ifdef FFMPEG_OLD_CODE
avcodec_close(m_codecCtx);
#else
if(m_codecCtx)
avcodec_free_context(&m_codecCtx);
#endif
avio_closep(&m_formatCtx->pb);
avformat_free_context(m_formatCtx);
}
int FFMPEGWriter::getPosition() const
{
return m_position;
}
DeviceSpecs FFMPEGWriter::getSpecs() const
{
return m_specs;
}
void FFMPEGWriter::write(unsigned int length, sample_t* buffer)
{
unsigned int samplesize = AUD_SAMPLE_SIZE(m_specs);
if(m_input_size)
{
sample_t* inbuf = m_input_buffer.getBuffer();
while(length)
{
unsigned int len = std::min(m_input_size - m_input_samples, length);
std::memcpy(inbuf + m_input_samples * m_specs.channels, buffer, len * samplesize);
buffer += len * m_specs.channels;
m_input_samples += len;
m_position += len;
length -= len;
if(m_input_samples == m_input_size)
{
encode();
m_input_samples = 0;
}
}
}
else // PCM data, can write directly!
{
int samplesize = AUD_SAMPLE_SIZE(m_specs);
m_input_buffer.assureSize(length * std::max(AUD_DEVICE_SAMPLE_SIZE(m_specs), samplesize));
sample_t* buf = m_input_buffer.getBuffer();
m_convert(reinterpret_cast<data_t*>(buf), reinterpret_cast<data_t*>(buffer), length * m_specs.channels);
m_input_samples = length;
m_position += length;
encode();
}
}
AUD_NAMESPACE_END
|