41 static AVBufferRef *hw_device_ctx = NULL;
 
   44 static int set_hwframe_ctx(AVCodecContext *ctx, AVBufferRef *hw_device_ctx, int64_t width, int64_t height)
 
   46     AVBufferRef *hw_frames_ref;
 
   47     AVHWFramesContext *frames_ctx = NULL;
 
   50     if (!(hw_frames_ref = av_hwframe_ctx_alloc(hw_device_ctx))) {
 
   51         std::clog << 
"Failed to create HW frame context.\n";
 
   54     frames_ctx = (AVHWFramesContext *)(hw_frames_ref->data);
 
   56     frames_ctx->sw_format = AV_PIX_FMT_NV12;
 
   57     frames_ctx->width = width;
 
   58     frames_ctx->height = height;
 
   59     frames_ctx->initial_pool_size = 20;
 
   60     if ((err = av_hwframe_ctx_init(hw_frames_ref)) < 0) {
 
   61         std::clog << 
"Failed to initialize HW frame context. " <<
 
   62             "Error code: " << av_err2string(err) << 
"\n";
 
   63         av_buffer_unref(&hw_frames_ref);
 
   66     ctx->hw_frames_ctx = av_buffer_ref(hw_frames_ref);
 
   67     if (!ctx->hw_frames_ctx)
 
   68         err = AVERROR(ENOMEM);
 
   70     av_buffer_unref(&hw_frames_ref);
 
   73 #endif // USE_HW_ACCEL 
   76         path(
path), oc(NULL), audio_st(NULL), video_st(NULL), samples(NULL),
 
   77         audio_outbuf(NULL), audio_outbuf_size(0), audio_input_frame_size(0), audio_input_position(0),
 
   78         initial_audio_input_frame_size(0), img_convert_ctx(NULL),
 
   79         video_codec_ctx(NULL), audio_codec_ctx(NULL), is_writing(false), video_timestamp(0), audio_timestamp(0),
 
   80         original_sample_rate(0), original_channels(0), avr(NULL), avr_planar(NULL), is_open(false), prepare_streams(false),
 
   81         write_header(false), write_trailer(false), audio_encoder_buffer_size(0), audio_encoder_buffer(NULL) {
 
  101         if (!prepare_streams)
 
  106             open_video(oc, video_st);
 
  108             open_audio(oc, audio_st);
 
  117 void FFmpegWriter::auto_detect_format() {
 
  123             "Could not allocate memory for AVFormatContext.", path);
 
  127     oc->oformat = av_guess_format(NULL, path.c_str(), NULL);
 
  128     if (oc->oformat == 
nullptr) {
 
  130             "Could not deduce output format from file extension.", path);
 
  134     if (oc->oformat->video_codec != AV_CODEC_ID_NONE && 
info.
has_video)
 
  135         info.
vcodec = avcodec_find_encoder(oc->oformat->video_codec)->name;
 
  138     if (oc->oformat->audio_codec != AV_CODEC_ID_NONE && 
info.
has_audio)
 
  139         info.
acodec = avcodec_find_encoder(oc->oformat->audio_codec)->name;
 
  143 void FFmpegWriter::initialize_streams() {
 
  145         "FFmpegWriter::initialize_streams",
 
  146         "oc->oformat->video_codec", oc->oformat->video_codec,
 
  147         "oc->oformat->audio_codec", oc->oformat->audio_codec,
 
  148         "AV_CODEC_ID_NONE", AV_CODEC_ID_NONE);
 
  153     if (oc->oformat->video_codec != AV_CODEC_ID_NONE && 
info.
has_video)
 
  155         video_st = add_video_stream();
 
  157     if (oc->oformat->audio_codec != AV_CODEC_ID_NONE && 
info.
has_audio)
 
  159         audio_st = add_audio_stream();
 
  165     if (
codec.length() > 0) {
 
  166         const AVCodec *new_codec;
 
  169 #if defined(__linux__) 
  170         if (strstr(
codec.c_str(), 
"_vaapi") != NULL) {
 
  171             new_codec = avcodec_find_encoder_by_name(
codec.c_str());
 
  176         } 
else if (strstr(
codec.c_str(), 
"_nvenc") != NULL) {
 
  177             new_codec = avcodec_find_encoder_by_name(
codec.c_str());
 
  183             new_codec = avcodec_find_encoder_by_name(
codec.c_str());
 
  187 #elif defined(_WIN32) 
  188         if (strstr(
codec.c_str(), 
"_dxva2") != NULL) {
 
  189             new_codec = avcodec_find_encoder_by_name(
codec.c_str());
 
  194         } 
else if (strstr(
codec.c_str(), 
"_nvenc") != NULL) {
 
  195             new_codec = avcodec_find_encoder_by_name(
codec.c_str());
 
  201             new_codec = avcodec_find_encoder_by_name(
codec.c_str());
 
  205 #elif defined(__APPLE__) 
  206         if (strstr(
codec.c_str(), 
"_videotoolbox") != NULL) {
 
  207             new_codec = avcodec_find_encoder_by_name(
codec.c_str());
 
  213             new_codec = avcodec_find_encoder_by_name(
codec.c_str());
 
  218         new_codec = avcodec_find_encoder_by_name(
codec.c_str());
 
  219 #endif //__linux__/_WIN32/__APPLE__ 
  220 #else // USE_HW_ACCEL 
  221         new_codec = avcodec_find_encoder_by_name(
codec.c_str());
 
  222 #endif // USE_HW_ACCEL 
  223         if (new_codec == NULL)
 
  224             throw InvalidCodec(
"A valid video codec could not be found for this file.", path);
 
  243     if (pixel_ratio.
num > 0) {
 
  247     if (bit_rate >= 1000)  
 
  249     if ((bit_rate >= 0) && (bit_rate < 256))  
 
  266         "FFmpegWriter::SetVideoOptions (" + 
codec + 
")",
 
  267         "width", width, 
"height", height,
 
  268         "size.num", size.
num, 
"size.den", size.
den,
 
  269         "fps.num", fps.
num, 
"fps.den", fps.
den);
 
  279         true, 
codec, fps, width, height,
 
  288     if (
codec.length() > 0) {
 
  289         const AVCodec *new_codec = avcodec_find_encoder_by_name(
codec.c_str());
 
  290         if (new_codec == NULL)
 
  291             throw InvalidCodec(
"A valid audio codec could not be found for this file.", path);
 
  297     if (sample_rate > 7999)
 
  306     if (original_sample_rate == 0)
 
  308     if (original_channels == 0)
 
  312         "FFmpegWriter::SetAudioOptions (" + 
codec + 
")",
 
  313         "sample_rate", sample_rate,
 
  314         "channels", channels,
 
  315         "bit_rate", bit_rate);
 
  326         true, 
codec, sample_rate, 2,
 
  335     AVCodecContext *c = NULL;
 
  337     std::stringstream convert(value);
 
  356         throw NoStreamsFound(
"The stream was not found. Be sure to call PrepareStreams() first.", path);
 
  359     const AVOption *option = NULL;
 
  367     if (option || (name == 
"g" || name == 
"qmin" || name == 
"qmax" || name == 
"max_b_frames" || name == 
"mb_decision" ||
 
  368                    name == 
"level" || name == 
"profile" || name == 
"slices" || name == 
"rc_min_rate" || name == 
"rc_max_rate" ||
 
  369                    name == 
"rc_buffer_size" || name == 
"crf" || name == 
"cqp" || name == 
"qp")) {
 
  373             convert >> c->gop_size;
 
  375         else if (name == 
"qmin")
 
  379         else if (name == 
"qmax")
 
  383         else if (name == 
"max_b_frames")
 
  385             convert >> c->max_b_frames;
 
  387         else if (name == 
"mb_decision")
 
  389             convert >> c->mb_decision;
 
  391         else if (name == 
"level")
 
  395         else if (name == 
"profile")
 
  397             convert >> c->profile;
 
  399         else if (name == 
"slices")
 
  401             convert >> c->slices;
 
  403         else if (name == 
"rc_min_rate")
 
  405             convert >> c->rc_min_rate;
 
  407         else if (name == 
"rc_max_rate")
 
  409             convert >> c->rc_max_rate;
 
  411         else if (name == 
"rc_buffer_size")
 
  413             convert >> c->rc_buffer_size;
 
  415         else if (name == 
"cqp") {
 
  419                 av_opt_set_int(c->priv_data, 
"qp", std::min(std::stoi(value),63), 0); 
 
  421 #endif // USE_HW_ACCEL 
  423                 switch (c->codec_id) {
 
  424 #if (LIBAVCODEC_VERSION_MAJOR >= 58) 
  426                 case AV_CODEC_ID_AV1 :
 
  428                     av_opt_set_int(c->priv_data, 
"qp", std::min(std::stoi(value),63), 0); 
 
  431                 case AV_CODEC_ID_VP8 :
 
  432                     c->bit_rate = 10000000;
 
  433                     av_opt_set_int(c->priv_data, 
"qp", std::max(std::min(std::stoi(value), 63), 4), 0); 
 
  435                 case AV_CODEC_ID_VP9 :
 
  437                     av_opt_set_int(c->priv_data, 
"qp", std::min(std::stoi(value), 63), 0); 
 
  438                     if (std::stoi(value) == 0) {
 
  439                         av_opt_set(c->priv_data, 
"preset", 
"veryslow", 0);
 
  440                         av_opt_set_int(c->priv_data, 
"lossless", 1, 0);
 
  443                 case AV_CODEC_ID_H264 :
 
  444                     av_opt_set_int(c->priv_data, 
"qp", std::min(std::stoi(value), 51), 0); 
 
  445                     if (std::stoi(value) == 0) {
 
  446                         av_opt_set(c->priv_data, 
"preset", 
"veryslow", 0);
 
  450                 case AV_CODEC_ID_HEVC :
 
  451                     av_opt_set_int(c->priv_data, 
"qp", std::min(std::stoi(value), 51), 0); 
 
  452                     if (std::stoi(value) == 0) {
 
  453                         av_opt_set(c->priv_data, 
"preset", 
"veryslow", 0);
 
  454                         av_opt_set_int(c->priv_data, 
"lossless", 1, 0);
 
  459                     av_opt_set_int(c->priv_data, 
"qp", std::min(std::stoi(value), 63), 0); 
 
  463         } 
else if (name == 
"crf") {
 
  467                 double mbs = 15000000.0;
 
  476                 c->bit_rate = (int)(mbs);
 
  478 #endif // USE_HW_ACCEL 
  480                 switch (c->codec_id) {
 
  481 #if (LIBAVCODEC_VERSION_MAJOR >= 58) 
  483                     case AV_CODEC_ID_AV1 :
 
  486                         av_opt_set_int(c->priv_data, 
"crf", std::min(std::stoi(value),63), 0);
 
  489                     case AV_CODEC_ID_VP8 :
 
  490                         c->bit_rate = 10000000;
 
  491                         av_opt_set_int(c->priv_data, 
"crf", std::max(std::min(std::stoi(value), 63), 4), 0); 
 
  493                     case AV_CODEC_ID_VP9 :
 
  495                         av_opt_set_int(c->priv_data, 
"crf", std::min(std::stoi(value), 63), 0); 
 
  496                         if (std::stoi(value) == 0) {
 
  497                             av_opt_set(c->priv_data, 
"preset", 
"veryslow", 0);
 
  498                             av_opt_set_int(c->priv_data, 
"lossless", 1, 0);
 
  501                     case AV_CODEC_ID_H264 :
 
  502                         av_opt_set_int(c->priv_data, 
"crf", std::min(std::stoi(value), 51), 0); 
 
  503                         if (std::stoi(value) == 0) {
 
  504                             av_opt_set(c->priv_data, 
"preset", 
"veryslow", 0);
 
  508                     case AV_CODEC_ID_HEVC :
 
  509                         if (strstr(
info.
vcodec.c_str(), 
"svt_hevc") != NULL) {
 
  510                             av_opt_set_int(c->priv_data, 
"preset", 7, 0);
 
  511                             av_opt_set_int(c->priv_data, 
"forced-idr",1,0);
 
  512                             av_opt_set_int(c->priv_data, 
"qp",std::min(std::stoi(value), 51),0);
 
  515                             av_opt_set_int(c->priv_data, 
"crf", std::min(std::stoi(value), 51), 0); 
 
  517                         if (std::stoi(value) == 0) {
 
  518                             av_opt_set(c->priv_data, 
"preset", 
"veryslow", 0);
 
  519                             av_opt_set_int(c->priv_data, 
"lossless", 1, 0);
 
  525                         double mbs = 15000000.0;
 
  533                         c->bit_rate = (int) (mbs);
 
  536         } 
else if (name == 
"qp") {
 
  538 #if (LIBAVCODEC_VERSION_MAJOR >= 58) 
  540                 switch (c->codec_id) {
 
  541                     case AV_CODEC_ID_AV1 :
 
  543                         if (strstr(
info.
vcodec.c_str(), 
"svtav1") != NULL) {
 
  544                             av_opt_set_int(c->priv_data, 
"qp", std::min(std::stoi(value),63), 0);
 
  546                         else if (strstr(
info.
vcodec.c_str(), 
"rav1e") != NULL) {
 
  549                             av_opt_set_int(c->priv_data, 
"qp", std::min(std::stoi(value),255), 0);
 
  551                         else if (strstr(
info.
vcodec.c_str(), 
"aom") != NULL) {
 
  555                             av_opt_set_int(c->priv_data, 
"crf", std::min(std::stoi(value),63), 0);
 
  558                             av_opt_set_int(c->priv_data, 
"crf", std::min(std::stoi(value),63), 0);
 
  560                     case AV_CODEC_ID_HEVC :
 
  562                         if (strstr(
info.
vcodec.c_str(), 
"svt_hevc") != NULL) {
 
  563                             av_opt_set_int(c->priv_data, 
"qp", std::min(std::stoi(value),51), 0);
 
  564                             av_opt_set_int(c->priv_data, 
"preset", 7, 0);
 
  565                             av_opt_set_int(c->priv_data, 
"forced-idr",1,0);
 
  569 #endif  // FFmpeg 4.0+ 
  572             AV_OPTION_SET(st, c->priv_data, name.c_str(), value.c_str(), c);
 
  576             "FFmpegWriter::SetOption (" + (std::string)name + 
")",
 
  581     } 
else if (name == 
"muxing_preset") {
 
  582         if (value == 
"mp4_faststart") {
 
  584             av_dict_set(&
mux_dict, 
"movflags", 
"faststart", 0);
 
  585         } 
else if (value == 
"mp4_fragmented") {
 
  587             av_dict_set(&
mux_dict, 
"movflags", 
"frag_keyframe", 0);
 
  588             av_dict_set(&
mux_dict, 
"min_frag_duration", 
"8000000", 0);
 
  591         throw InvalidOptions(
"The option is not valid for this codec.", path);
 
  602     return avcodec_find_encoder_by_name(codec_name.c_str()) != NULL;
 
  608         throw InvalidOptions(
"No video or audio options have been set.  You must set has_video or has_audio (or both).", path);
 
  611         "FFmpegWriter::PrepareStreams [" + path + 
"]",
 
  616     initialize_streams();
 
  619     prepare_streams = 
true;
 
  625         throw InvalidOptions(
"No video or audio options have been set.  You must set has_video or has_audio (or both).", path);
 
  628     if (!(oc->oformat->flags & AVFMT_NOFILE)) {
 
  629         if (avio_open(&oc->pb, path.c_str(), AVIO_FLAG_WRITE) < 0)
 
  630             throw InvalidFile(
"Could not open or write file.", path);
 
  638         av_dict_set(&oc->metadata, iter->first.c_str(), iter->second.c_str(), 0);
 
  642     AVDictionary *dict = NULL;
 
  648     if (avformat_write_header(oc, &dict) != 0) {
 
  650             "FFmpegWriter::WriteHeader (avformat_write_header)");
 
  651         throw InvalidFile(
"Could not write header to file.", path);
 
  655     if (dict) av_dict_free(&dict);
 
  668         throw WriterClosed(
"The FFmpegWriter is closed.  Call Open() before calling this method.", path);
 
  671         "FFmpegWriter::WriteFrame",
 
  672         "frame->number", frame->number,
 
  673         "is_writing", is_writing);
 
  683 void FFmpegWriter::write_frame(std::shared_ptr<Frame> frame) {
 
  688     bool has_error_encoding_video = 
false;
 
  692         write_audio_packets(
false, frame);
 
  696         process_video_packet(frame);
 
  700         if (av_frames.count(frame)) {
 
  702             AVFrame *frame_final = av_frames[frame];
 
  705             if (!write_video_packet(frame, frame_final)) {
 
  706                 has_error_encoding_video = 
true;
 
  710             av_freep(&(frame_final->data[0]));
 
  712             av_frames.erase(frame);
 
  720     if (has_error_encoding_video)
 
  727         "FFmpegWriter::WriteFrame (from Reader)",
 
  732     for (int64_t number = start; number <= length; number++) {
 
  734         std::shared_ptr<Frame> f = reader->
GetFrame(number);
 
  745         write_audio_packets(
true, NULL);
 
  754     av_write_trailer(oc);
 
  757     write_trailer = 
true;
 
  763 void FFmpegWriter::flush_encoders() {
 
  766 #if (LIBAVFORMAT_VERSION_MAJOR < 58) 
  780             video_timestamp += av_rescale_q(1, av_make_q(
info.
fps.
den, 
info.
fps.
num), video_codec_ctx->time_base);
 
  783             AVPacket* pkt = av_packet_alloc();
 
  797             error_code = avcodec_send_frame(video_codec_ctx, NULL);
 
  799             while (error_code >= 0) {
 
  800                 error_code = avcodec_receive_packet(video_codec_ctx, pkt);
 
  801                 if (error_code == AVERROR(EAGAIN)|| error_code == AVERROR_EOF) {
 
  804                     avcodec_flush_buffers(video_codec_ctx);
 
  807                 av_packet_rescale_ts(pkt, video_codec_ctx->time_base, video_st->time_base);
 
  808                 pkt->stream_index = video_st->index;
 
  809                 error_code = av_interleaved_write_frame(oc, pkt);
 
  811 #else // IS_FFMPEG_3_2 
  814             error_code = avcodec_encode_video2(video_codec_ctx, pkt, NULL, &got_packet);
 
  816 #endif // IS_FFMPEG_3_2 
  818             if (error_code < 0) {
 
  820                     "FFmpegWriter::flush_encoders ERROR [" 
  821                         + av_err2string(error_code) + 
"]",
 
  822                     "error_code", error_code);
 
  829             av_packet_rescale_ts(pkt, video_codec_ctx->time_base, video_st->time_base);
 
  830             pkt->stream_index = video_st->index;
 
  833             error_code = av_interleaved_write_frame(oc, pkt);
 
  834             if (error_code < 0) {
 
  836                     "FFmpegWriter::flush_encoders ERROR [" 
  837                         + av_err2string(error_code) + 
"]",
 
  838                     "error_code", error_code);
 
  847             AVPacket* pkt = av_packet_alloc();
 
  854             pkt->pts = pkt->dts = audio_timestamp;
 
  860             error_code = avcodec_send_frame(audio_codec_ctx, NULL);
 
  862             error_code = avcodec_encode_audio2(audio_codec_ctx, pkt, NULL, &got_packet);
 
  864             if (error_code < 0) {
 
  866                     "FFmpegWriter::flush_encoders ERROR [" 
  867                         + av_err2string(error_code) + 
"]",
 
  868                     "error_code", error_code);
 
  876             pkt->pts = pkt->dts = audio_timestamp;
 
  879             av_packet_rescale_ts(pkt, audio_codec_ctx->time_base, audio_st->time_base);
 
  882             pkt->stream_index = audio_st->index;
 
  883             pkt->flags |= AV_PKT_FLAG_KEY;
 
  886             error_code = av_interleaved_write_frame(oc, pkt);
 
  887             if (error_code < 0) {
 
  889                     "FFmpegWriter::flush_encoders ERROR [" 
  890                         + av_err2string(error_code) + 
"]",
 
  891                     "error_code", error_code);
 
  895             audio_timestamp += pkt->duration;
 
  905 void FFmpegWriter::close_video(AVFormatContext *oc, AVStream *st)
 
  910             av_buffer_unref(&hw_device_ctx);
 
  911             hw_device_ctx = NULL;
 
  914 #endif // USE_HW_ACCEL 
  917     if (video_codec_ctx != 
nullptr) {
 
  919         av_free(video_codec_ctx);
 
  924 void FFmpegWriter::close_audio(AVFormatContext *oc, AVStream *st)
 
  928     delete[] audio_outbuf;
 
  929     delete[] audio_encoder_buffer;
 
  932     audio_encoder_buffer = NULL;
 
  948     if (audio_codec_ctx != 
nullptr) {
 
  950         av_free(audio_codec_ctx);
 
  962         close_video(oc, video_st);
 
  964         close_audio(oc, audio_st);
 
  968         sws_freeContext(img_convert_ctx);
 
  970     if (!(oc->oformat->flags & AVFMT_NOFILE)) {
 
  980     avformat_free_context(oc);
 
  985     prepare_streams = 
false;
 
  986     write_header = 
false;
 
  987     write_trailer = 
false;
 
  993 void FFmpegWriter::add_avframe(std::shared_ptr<Frame> frame, AVFrame *av_frame) {
 
  995     if (!av_frames.count(frame)) {
 
  997         av_frames[frame] = av_frame;
 
 1005 AVStream *FFmpegWriter::add_audio_stream() {
 
 1007     const AVCodec *
codec = avcodec_find_encoder_by_name(
info.
acodec.c_str());
 
 1009         throw InvalidCodec(
"A valid audio codec could not be found for this file.", path);
 
 1012     if (audio_codec_ctx != 
nullptr) {
 
 1017     AVStream* st = avformat_new_stream(oc, 
codec);
 
 1019         throw OutOfMemory(
"Could not allocate memory for the audio stream.", path);
 
 1023 #if (LIBAVFORMAT_VERSION_MAJOR >= 58) 
 1024     st->codecpar->codec_id = 
codec->id;
 
 1026     AVCodecContext* c = audio_codec_ctx;
 
 1028     c->codec_id = 
codec->id;
 
 1029     c->codec_type = AVMEDIA_TYPE_AUDIO;
 
 1038     if (
codec->supported_samplerates) {
 
 1040         for (i = 0; 
codec->supported_samplerates[i] != 0; i++)
 
 1046         if (
codec->supported_samplerates[i] == 0)
 
 1047             throw InvalidSampleRate(
"An invalid sample rate was detected for this codec.", path);
 
 1055     AVChannelLayout ch_layout;
 
 1057     if (
codec->ch_layouts) {
 
 1059         for (i = 0; av_channel_layout_check(&
codec->ch_layouts[i]); i++)
 
 1060             if (av_channel_layout_compare(&ch_layout, &
codec->ch_layouts[i])) {
 
 1062                 av_channel_layout_copy(&c->ch_layout, &ch_layout);
 
 1065         if (!av_channel_layout_check(&
codec->ch_layouts[i]))
 
 1066             throw InvalidChannels(
"An invalid channel layout was detected (i.e. MONO / STEREO).", path);
 
 1069         av_channel_layout_copy(&c->ch_layout, &ch_layout);
 
 1072     if (
codec->channel_layouts) {
 
 1074         for (i = 0; 
codec->channel_layouts[i] != 0; i++)
 
 1075             if (channel_layout == 
codec->channel_layouts[i]) {
 
 1077                 c->channel_layout = channel_layout;
 
 1080         if (
codec->channel_layouts[i] == 0)
 
 1081             throw InvalidChannels(
"An invalid channel layout was detected (i.e. MONO / STEREO).", path);
 
 1084             c->channel_layout = channel_layout;
 
 1088     if (
codec->sample_fmts) {
 
 1089         for (
int i = 0; 
codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
 
 1091             c->sample_fmt = 
codec->sample_fmts[i];
 
 1095     if (c->sample_fmt == AV_SAMPLE_FMT_NONE) {
 
 1097         c->sample_fmt = AV_SAMPLE_FMT_S16;
 
 1101     if (oc->oformat->flags & AVFMT_GLOBALHEADER)
 
 1102 #if (LIBAVCODEC_VERSION_MAJOR >= 57) 
 1104         c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
 
 1106         c->flags |= CODEC_FLAG_GLOBAL_HEADER;
 
 1112     const char* nb_channels_label;
 
 1113     const char* channel_layout_label;
 
 1116     nb_channels = c->ch_layout.nb_channels;
 
 1117     channel_layout = c->ch_layout.u.mask;
 
 1118     nb_channels_label = 
"c->ch_layout.nb_channels";
 
 1119     channel_layout_label = 
"c->ch_layout.u.mask";
 
 1121     nb_channels = c->channels;
 
 1122     nb_channels_label = 
"c->channels";
 
 1123     channel_layout_label = 
"c->channel_layout";
 
 1127         "FFmpegWriter::add_audio_stream",
 
 1128         "c->codec_id", c->codec_id,
 
 1129         "c->bit_rate", c->bit_rate,
 
 1130         nb_channels_label, nb_channels,
 
 1131         "c->sample_fmt", c->sample_fmt,
 
 1132         channel_layout_label, channel_layout,
 
 1133         "c->sample_rate", c->sample_rate);
 
 1139 AVStream *FFmpegWriter::add_video_stream() {
 
 1141     const AVCodec *
codec = avcodec_find_encoder_by_name(
info.
vcodec.c_str());
 
 1143         throw InvalidCodec(
"A valid video codec could not be found for this file.", path);
 
 1146     if (video_codec_ctx != 
nullptr) {
 
 1151     AVStream* st = avformat_new_stream(oc, 
codec);
 
 1153         throw OutOfMemory(
"Could not allocate memory for the video stream.", path);
 
 1157 #if (LIBAVFORMAT_VERSION_MAJOR >= 58) 
 1158     st->codecpar->codec_id = 
codec->id;
 
 1161     AVCodecContext* c = video_codec_ctx;
 
 1163     c->codec_id = 
codec->id;
 
 1164     c->codec_type = AVMEDIA_TYPE_VIDEO;
 
 1172 #
if (LIBAVCODEC_VERSION_MAJOR >= 58)
 
 1173         && c->codec_id != AV_CODEC_ID_AV1
 
 1178             if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO)  {
 
 1187         switch (c->codec_id) {
 
 1188 #if (LIBAVCODEC_VERSION_MAJOR >= 58) 
 1190             case AV_CODEC_ID_AV1 :
 
 1194                 if (strstr(
info.
vcodec.c_str(), 
"aom") != NULL) {
 
 1195                     int calculated_quality = 35;
 
 1198                     av_opt_set_int(c->priv_data, 
"crf", calculated_quality, 0);
 
 1201                     int calculated_quality = 50;
 
 1204                     av_opt_set_int(c->priv_data, 
"qp", calculated_quality, 0);
 
 1208             if (strstr(
info.
vcodec.c_str(), 
"svtav1") != NULL) {
 
 1209                 av_opt_set_int(c->priv_data, 
"preset", 6, 0);
 
 1210                 av_opt_set_int(c->priv_data, 
"forced-idr",1,0);
 
 1212             else if (strstr(
info.
vcodec.c_str(), 
"rav1e") != NULL) {
 
 1213                 av_opt_set_int(c->priv_data, 
"speed", 7, 0);
 
 1214                 av_opt_set_int(c->priv_data, 
"tile-rows", 2, 0);
 
 1215                 av_opt_set_int(c->priv_data, 
"tile-columns", 4, 0);
 
 1217             else if (strstr(
info.
vcodec.c_str(), 
"aom") != NULL) {
 
 1220                 av_opt_set_int(c->priv_data, 
"tile-rows", 1, 0);        
 
 1221                 av_opt_set_int(c->priv_data, 
"tile-columns", 2, 0);     
 
 1222                 av_opt_set_int(c->priv_data, 
"row-mt", 1, 0);           
 
 1223                 av_opt_set_int(c->priv_data, 
"cpu-used", 3, 0);         
 
 1227             case AV_CODEC_ID_VP9 :
 
 1228             case AV_CODEC_ID_HEVC :
 
 1229             case AV_CODEC_ID_VP8 :
 
 1230             case AV_CODEC_ID_H264 :
 
 1266 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(56, 26, 0) 
 1267     c->framerate = av_inv_q(c->time_base);
 
 1269     st->avg_frame_rate = av_inv_q(c->time_base);
 
 1274     c->max_b_frames = 10;
 
 1275     if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO)
 
 1277         c->max_b_frames = 2;
 
 1278     if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO)
 
 1284     if (oc->oformat->flags & AVFMT_GLOBALHEADER)
 
 1285 #if (LIBAVCODEC_VERSION_MAJOR >= 57) 
 1287         c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
 
 1289         c->flags |= CODEC_FLAG_GLOBAL_HEADER;
 
 1294     while (supported_pixel_formats != NULL && *supported_pixel_formats != 
PIX_FMT_NONE) {
 
 1297             c->pix_fmt = *supported_pixel_formats;
 
 1298         ++supported_pixel_formats;
 
 1303         if (oc->oformat->video_codec == AV_CODEC_ID_RAWVIDEO) {
 
 1307 #if (LIBAVFORMAT_VERSION_MAJOR < 58) 
 1309             if (strcmp(oc->oformat->name, 
"gif") != 0)
 
 1312                 oc->oformat->flags |= AVFMT_RAWPICTURE;
 
 1322         "FFmpegWriter::add_video_stream (" 
 1323             + (std::string)oc->oformat->name + 
" : " 
 1324             + (std::string)av_get_pix_fmt_name(c->pix_fmt) + 
")",
 
 1325         "c->codec_id", c->codec_id,
 
 1326         "c->bit_rate", c->bit_rate,
 
 1327         "c->pix_fmt", c->pix_fmt,
 
 1328         "oc->oformat->flags", oc->oformat->flags);
 
 1333 void FFmpegWriter::open_audio(AVFormatContext *oc, AVStream *st) {
 
 1334     const AVCodec *
codec;
 
 1343         codec = avcodec_find_encoder(audio_codec_ctx->codec_id);
 
 1348     AVDictionary *
opts = NULL;
 
 1349     av_dict_set(&
opts, 
"strict", 
"experimental", 0);
 
 1352     if (avcodec_open2(audio_codec_ctx, 
codec, &
opts) < 0)
 
 1353         throw InvalidCodec(
"Could not open audio codec", path);
 
 1357     av_dict_free(&
opts);
 
 1361     if (audio_codec_ctx->frame_size <= 1) {
 
 1367             case AV_CODEC_ID_PCM_S16LE:
 
 1368             case AV_CODEC_ID_PCM_S16BE:
 
 1369             case AV_CODEC_ID_PCM_U16LE:
 
 1370             case AV_CODEC_ID_PCM_U16BE:
 
 1371                 audio_input_frame_size >>= 1;
 
 1378         audio_input_frame_size = audio_codec_ctx->frame_size;
 
 1382     initial_audio_input_frame_size = audio_input_frame_size;
 
 1389     audio_outbuf = 
new uint8_t[audio_outbuf_size];
 
 1393     audio_encoder_buffer = 
new uint8_t[audio_encoder_buffer_size];
 
 1396     for (std::map<std::string, std::string>::iterator iter = 
info.
metadata.begin(); iter != 
info.
metadata.end(); ++iter) {
 
 1397         av_dict_set(&st->metadata, iter->first.c_str(), iter->second.c_str(), 0);
 
 1401         "FFmpegWriter::open_audio",
 
 1402         "audio_codec_ctx->thread_count", audio_codec_ctx->thread_count,
 
 1403         "audio_input_frame_size", audio_input_frame_size,
 
 1408 void FFmpegWriter::open_video(AVFormatContext *oc, AVStream *st) {
 
 1409     const AVCodec *
codec;
 
 1419         char *adapter_ptr = NULL;
 
 1423         std::clog << 
"Encoding Device Nr: " << adapter_num << 
"\n";
 
 1424         if (adapter_num < 3 && adapter_num >=0) {
 
 1425 #if defined(__linux__) 
 1426                 snprintf(adapter,
sizeof(adapter),
"/dev/dri/renderD%d", adapter_num+128);
 
 1428                 adapter_ptr = adapter;
 
 1429 #elif defined(_WIN32) || defined(__APPLE__) 
 1437 #if defined(__linux__) 
 1438         if( adapter_ptr != NULL && access( adapter_ptr, W_OK ) == 0 ) {
 
 1439 #elif defined(_WIN32) || defined(__APPLE__) 
 1440         if( adapter_ptr != NULL ) {
 
 1443                 "Encode Device present using device",
 
 1444                 "adapter", adapter_num);
 
 1449                 "Encode Device not present, using default");
 
 1451         if (av_hwdevice_ctx_create(&hw_device_ctx,
 
 1455                 "FFmpegWriter::open_video ERROR creating hwdevice, Codec name:",
 
 1460 #endif // USE_HW_ACCEL 
 1470     if (video_codec_ctx->max_b_frames && video_codec_ctx->codec_id != AV_CODEC_ID_MPEG4 && video_codec_ctx->codec_id != AV_CODEC_ID_MPEG1VIDEO && video_codec_ctx->codec_id != AV_CODEC_ID_MPEG2VIDEO)
 
 1471         video_codec_ctx->max_b_frames = 0;
 
 1475     av_dict_set(&
opts, 
"strict", 
"experimental", 0);
 
 1489             if (av_opt_get_int(video_codec_ctx->priv_data, 
"qp", 0, &qp) != 0 || qp == 0) {
 
 1491                 av_opt_set(video_codec_ctx->priv_data, 
"rc_mode", 
"VBR", 0);
 
 1495                 video_codec_ctx->rc_max_rate = video_codec_ctx->bit_rate;
 
 1499         switch (video_codec_ctx->codec_id) {
 
 1500             case AV_CODEC_ID_H264:
 
 1501                 video_codec_ctx->max_b_frames = 0;  
 
 1502                 video_codec_ctx->profile = FF_PROFILE_H264_BASELINE | FF_PROFILE_H264_CONSTRAINED;
 
 1503                 av_opt_set(video_codec_ctx->priv_data, 
"preset", 
"slow", 0);
 
 1504                 av_opt_set(video_codec_ctx->priv_data, 
"tune", 
"zerolatency", 0);
 
 1505                 av_opt_set(video_codec_ctx->priv_data, 
"vprofile", 
"baseline", AV_OPT_SEARCH_CHILDREN);
 
 1507             case AV_CODEC_ID_HEVC:
 
 1510             case AV_CODEC_ID_VP9:
 
 1515                     "No codec-specific options defined for this codec. HW encoding may fail",
 
 1516                     "codec_id", video_codec_ctx->codec_id);
 
 1525                 "FFmpegWriter::open_video (set_hwframe_ctx) ERROR faled to set hwframe context",
 
 1528                 av_err2string(err), -1);
 
 1531 #endif // USE_HW_ACCEL 
 1536             video_codec_ctx->codec_tag = MKTAG(
'h', 
'v', 
'c', 
'1');
 
 1539         if (video_codec_ctx->codec_id == AV_CODEC_ID_HEVC) {
 
 1540             video_codec_ctx->codec_tag = MKTAG(
'h', 
'v', 
'c', 
'1');
 
 1545     if (avcodec_open2(video_codec_ctx, 
codec, &
opts) < 0)
 
 1546         throw InvalidCodec(
"Could not open video codec", path);
 
 1550     av_dict_free(&
opts);
 
 1554         av_dict_set(&st->metadata, iter->first.c_str(), iter->second.c_str(), 0);
 
 1558         "FFmpegWriter::open_video",
 
 1559         "video_codec_ctx->thread_count", video_codec_ctx->thread_count);
 
 1564 void FFmpegWriter::write_audio_packets(
bool is_final, std::shared_ptr<openshot::Frame> frame) {
 
 1565     if (!frame && !is_final)
 
 1569     int total_frame_samples = 0;
 
 1570     int frame_position = 0;
 
 1571     int channels_in_frame = 0;
 
 1572     int sample_rate_in_frame = 0;
 
 1573     int samples_in_frame = 0;
 
 1578     int16_t *all_queued_samples = (int16_t *) av_malloc(all_queued_samples_size);
 
 1579     int16_t *all_resampled_samples = NULL;
 
 1580     int16_t *final_samples_planar = NULL;
 
 1581     int16_t *final_samples = NULL;
 
 1584     float *frame_samples_float = NULL;
 
 1588         sample_rate_in_frame = frame->SampleRate();
 
 1589         samples_in_frame = frame->GetAudioSamplesCount();
 
 1590         channels_in_frame = frame->GetAudioChannelsCount();
 
 1591         channel_layout_in_frame = frame->ChannelsLayout();
 
 1594         frame_samples_float = frame->GetInterleavedAudioSamples(&samples_in_frame);
 
 1598     total_frame_samples = samples_in_frame * channels_in_frame;
 
 1601     const int16_t max16 = 32767;
 
 1602     const int16_t min16 = -32768;
 
 1603     for (
int s = 0; s < total_frame_samples; s++, frame_position++) {
 
 1604         float valF = frame_samples_float[s] * (1 << 15);
 
 1608         } 
else if (valF < min16) {
 
 1611             conv = int(valF + 32768.5) - 32768; 
 
 1615         all_queued_samples[frame_position] = conv;
 
 1619     delete[] frame_samples_float;
 
 1623     total_frame_samples = frame_position;
 
 1624     int remaining_frame_samples = total_frame_samples;
 
 1625     int samples_position = 0;
 
 1629         "FFmpegWriter::write_audio_packets",
 
 1630         "is_final", is_final,
 
 1631         "total_frame_samples", total_frame_samples,
 
 1632         "channel_layout_in_frame", channel_layout_in_frame,
 
 1633         "channels_in_frame", channels_in_frame,
 
 1634         "samples_in_frame", samples_in_frame,
 
 1638     AVSampleFormat output_sample_fmt = audio_codec_ctx->sample_fmt;
 
 1640     AVFrame *audio_frame = NULL;
 
 1645         audio_frame->nb_samples = total_frame_samples / channels_in_frame;
 
 1648         int error_code = avcodec_fill_audio_frame(audio_frame, channels_in_frame, AV_SAMPLE_FMT_S16, (uint8_t *) all_queued_samples, all_queued_samples_size, 0);
 
 1649         if (error_code < 0) {
 
 1651                 "FFmpegWriter::write_audio_packets ERROR [" 
 1652                     + av_err2string(error_code) + 
"]",
 
 1653                 "error_code", error_code);
 
 1657         switch (audio_codec_ctx->sample_fmt) {
 
 1658             case AV_SAMPLE_FMT_FLTP: {
 
 1659                 output_sample_fmt = AV_SAMPLE_FMT_FLT;
 
 1662             case AV_SAMPLE_FMT_S32P: {
 
 1663                 output_sample_fmt = AV_SAMPLE_FMT_S32;
 
 1666             case AV_SAMPLE_FMT_S16P: {
 
 1667                 output_sample_fmt = AV_SAMPLE_FMT_S16;
 
 1670             case AV_SAMPLE_FMT_U8P: {
 
 1671                 output_sample_fmt = AV_SAMPLE_FMT_U8;
 
 1681         total_frame_samples *= (float(
info.
sample_rate) / sample_rate_in_frame); 
 
 1682         total_frame_samples *= (float(
info.
channels) / channels_in_frame); 
 
 1687         audio_converted->nb_samples = total_frame_samples / channels_in_frame;
 
 1688         av_samples_alloc(audio_converted->data, audio_converted->linesize, 
info.
channels, audio_converted->nb_samples, output_sample_fmt, 0);
 
 1691             "FFmpegWriter::write_audio_packets (1st resampling)",
 
 1692             "in_sample_fmt", AV_SAMPLE_FMT_S16,
 
 1693             "out_sample_fmt", output_sample_fmt,
 
 1694             "in_sample_rate", sample_rate_in_frame,
 
 1696             "in_channels", channels_in_frame,
 
 1703             AVChannelLayout in_chlayout;
 
 1704             AVChannelLayout out_chlayout;
 
 1705             av_channel_layout_from_mask(&in_chlayout, channel_layout_in_frame);
 
 1707             av_opt_set_chlayout(avr, 
"in_chlayout", &in_chlayout, 0);
 
 1708             av_opt_set_chlayout(avr, 
"out_chlayout", &out_chlayout, 0);
 
 1710             av_opt_set_int(avr, 
"in_channel_layout", channel_layout_in_frame, 0);
 
 1712             av_opt_set_int(avr, 
"in_channels", channels_in_frame, 0);
 
 1715             av_opt_set_int(avr, 
"in_sample_fmt", AV_SAMPLE_FMT_S16, 0);
 
 1716             av_opt_set_int(avr, 
"out_sample_fmt", output_sample_fmt, 0); 
 
 1717             av_opt_set_int(avr, 
"in_sample_rate", sample_rate_in_frame, 0);
 
 1724             audio_converted->data,         
 
 1725             audio_converted->linesize[0],   
 
 1726             audio_converted->nb_samples,     
 
 1728             audio_frame->linesize[0],       
 
 1729             audio_frame->nb_samples     
 
 1733         remaining_frame_samples = total_frame_samples;
 
 1736         all_resampled_samples = (int16_t *) av_malloc(
 
 1738             * (av_get_bytes_per_sample(output_sample_fmt) /
 
 1739                av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) )
 
 1743         memcpy(all_resampled_samples, audio_converted->data[0],
 
 1744                static_cast<size_t>(nb_samples)
 
 1746                    * av_get_bytes_per_sample(output_sample_fmt));
 
 1749         av_freep(&(audio_frame->data[0]));
 
 1751         av_freep(&audio_converted->data[0]);
 
 1753         all_queued_samples = NULL; 
 
 1756             "FFmpegWriter::write_audio_packets (Successfully completed 1st resampling)",
 
 1757             "nb_samples", nb_samples,
 
 1758             "remaining_frame_samples", remaining_frame_samples);
 
 1762     while (remaining_frame_samples > 0 || is_final) {
 
 1764         int remaining_packet_samples = (audio_input_frame_size * 
info.
channels) - audio_input_position;
 
 1768         if (remaining_frame_samples >= remaining_packet_samples) {
 
 1769             diff = remaining_packet_samples;
 
 1771             diff = remaining_frame_samples;
 
 1778                 samples + (audio_input_position
 
 1779                     * (av_get_bytes_per_sample(output_sample_fmt) /
 
 1780                        av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) )
 
 1782                 all_resampled_samples + samples_position,
 
 1783                 static_cast<size_t>(diff)
 
 1784                     * av_get_bytes_per_sample(output_sample_fmt)
 
 1788         audio_input_position += diff;
 
 1789         samples_position += diff * (av_get_bytes_per_sample(output_sample_fmt) / av_get_bytes_per_sample(AV_SAMPLE_FMT_S16));
 
 1790         remaining_frame_samples -= diff;
 
 1793         if (audio_input_position < (audio_input_frame_size * 
info.
channels) && !is_final)
 
 1800         if (av_sample_fmt_is_planar(audio_codec_ctx->sample_fmt)) {
 
 1802                 "FFmpegWriter::write_audio_packets (2nd resampling for Planar formats)",
 
 1803                 "in_sample_fmt", output_sample_fmt,
 
 1804                 "out_sample_fmt", audio_codec_ctx->sample_fmt,
 
 1815                 AVChannelLayout layout;
 
 1817                 av_opt_set_chlayout(avr_planar, 
"in_chlayout", &layout, 0);
 
 1818                 av_opt_set_chlayout(avr_planar, 
"out_chlayout", &layout, 0);
 
 1822                 av_opt_set_int(avr_planar, 
"in_channels", 
info.
channels, 0);
 
 1823                 av_opt_set_int(avr_planar, 
"out_channels", 
info.
channels, 0);
 
 1825                 av_opt_set_int(avr_planar, 
"in_sample_fmt", output_sample_fmt, 0);
 
 1826                 av_opt_set_int(avr_planar, 
"out_sample_fmt", audio_codec_ctx->sample_fmt, 0); 
 
 1835             audio_frame->nb_samples = audio_input_position / 
info.
channels;
 
 1838             final_samples_planar = (int16_t *) av_malloc(
 
 1839                 sizeof(int16_t) * audio_frame->nb_samples * 
info.
channels 
 1840                 * (av_get_bytes_per_sample(output_sample_fmt) /
 
 1841                    av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) )
 
 1845             memcpy(final_samples_planar, samples,
 
 1846                    static_cast<size_t>(audio_frame->nb_samples)
 
 1848                        * av_get_bytes_per_sample(output_sample_fmt));
 
 1851             avcodec_fill_audio_frame(audio_frame, 
info.
channels, output_sample_fmt,
 
 1852                 (uint8_t *) final_samples_planar, audio_encoder_buffer_size, 0);
 
 1855             frame_final->nb_samples = audio_input_frame_size;
 
 1862             frame_final->format = audio_codec_ctx->sample_fmt;
 
 1863             av_samples_alloc(frame_final->data, frame_final->linesize, 
info.
channels,
 
 1864                 frame_final->nb_samples, audio_codec_ctx->sample_fmt, 0);
 
 1870                 frame_final->linesize[0],   
 
 1871                 frame_final->nb_samples,     
 
 1873                 audio_frame->linesize[0],   
 
 1874                 audio_frame->nb_samples   
 
 1878             const auto copy_length = 
static_cast<size_t>(nb_samples)
 
 1879                 * av_get_bytes_per_sample(audio_codec_ctx->sample_fmt)
 
 1883                 memcpy(samples, frame_final->data[0], copy_length);
 
 1886             av_freep(&(audio_frame->data[0]));
 
 1888             all_queued_samples = NULL; 
 
 1891                 "FFmpegWriter::write_audio_packets (Successfully completed 2nd resampling for Planar formats)",
 
 1892                 "nb_samples", nb_samples);
 
 1896             const auto buf_size = 
static_cast<size_t>(audio_input_position)
 
 1897                 * (av_get_bytes_per_sample(audio_codec_ctx->sample_fmt) /
 
 1898                    av_get_bytes_per_sample(AV_SAMPLE_FMT_S16)
 
 1900             final_samples = 
reinterpret_cast<int16_t*
>(
 
 1901                     av_malloc(
sizeof(int16_t) * buf_size));
 
 1904             memcpy(final_samples, samples,
 
 1905                 audio_input_position * av_get_bytes_per_sample(audio_codec_ctx->sample_fmt));
 
 1908             frame_final->nb_samples = audio_input_frame_size;
 
 1912             int nb_channels = audio_codec_ctx->ch_layout.nb_channels;
 
 1914             int nb_channels = audio_codec_ctx->channels;
 
 1916             avcodec_fill_audio_frame(frame_final, nb_channels,
 
 1917                 audio_codec_ctx->sample_fmt, (uint8_t *) final_samples,
 
 1918                 audio_encoder_buffer_size, 0);
 
 1922         frame_final->pts = audio_timestamp;
 
 1926         AVPacket* pkt = av_packet_alloc();
 
 1929         av_init_packet(pkt);
 
 1931         pkt->data = audio_encoder_buffer;
 
 1932         pkt->size = audio_encoder_buffer_size;
 
 1935         pkt->pts = pkt->dts = audio_timestamp;
 
 1938         int got_packet_ptr = 0;
 
 1944         int frame_finished = 0;
 
 1945         error_code = ret =  avcodec_send_frame(audio_codec_ctx, frame_final);
 
 1946         if (ret < 0 && ret !=  AVERROR(EINVAL) && ret != AVERROR_EOF) {
 
 1947             avcodec_send_frame(audio_codec_ctx, NULL);
 
 1952             ret =  avcodec_receive_packet(audio_codec_ctx, pkt);
 
 1955             if(ret == AVERROR(EINVAL) || ret == AVERROR_EOF) {
 
 1956                 avcodec_flush_buffers(audio_codec_ctx);
 
 1960                 ret = frame_finished;
 
 1963         if (!pkt->data && !frame_finished)
 
 1967         got_packet_ptr = ret;
 
 1970         int error_code = avcodec_encode_audio2(audio_codec_ctx, pkt, frame_final, &got_packet_ptr);
 
 1973         if (error_code == 0 && got_packet_ptr) {
 
 1977             pkt->pts = pkt->dts = audio_timestamp;
 
 1980             av_packet_rescale_ts(pkt, audio_codec_ctx->time_base, audio_st->time_base);
 
 1983             pkt->stream_index = audio_st->index;
 
 1984             pkt->flags |= AV_PKT_FLAG_KEY;
 
 1987             error_code = av_interleaved_write_frame(oc, pkt);
 
 1990         if (error_code < 0) {
 
 1992                 "FFmpegWriter::write_audio_packets ERROR [" 
 1993                     + av_err2string(error_code) + 
"]",
 
 1994                 "error_code", error_code);
 
 1998         audio_timestamp += FFMIN(audio_input_frame_size, audio_input_position);
 
 2001         av_freep(&(frame_final->data[0]));
 
 2008         audio_input_position = 0;
 
 2013     if (all_resampled_samples) {
 
 2014         av_freep(&all_resampled_samples);
 
 2015         all_resampled_samples = NULL;
 
 2017     if (all_queued_samples) {
 
 2018         av_freep(&all_queued_samples);
 
 2019         all_queued_samples = NULL;
 
 2024 AVFrame *FFmpegWriter::allocate_avframe(
PixelFormat pix_fmt, 
int width, 
int height, 
int *buffer_size, uint8_t *new_buffer) {
 
 2026     AVFrame *new_av_frame = NULL;
 
 2030     if (new_av_frame == NULL)
 
 2031         throw OutOfMemory(
"Could not allocate AVFrame", path);
 
 2039         new_buffer = (uint8_t *) av_malloc(*buffer_size * 
sizeof(uint8_t));
 
 2042         new_av_frame->width = width;
 
 2043         new_av_frame->height = height;
 
 2044         new_av_frame->format = pix_fmt;
 
 2048     return new_av_frame;
 
 2052 void FFmpegWriter::process_video_packet(std::shared_ptr<Frame> frame) {
 
 2054     int src_w = frame->GetWidth();
 
 2055     int src_h = frame->GetHeight();
 
 2058     if (src_w == 1 && src_h == 1)
 
 2062     const uchar* pixels = frame->GetPixels();
 
 2063     if (!persistent_src_frame) {
 
 2064         persistent_src_frame = av_frame_alloc();
 
 2065         if (!persistent_src_frame)
 
 2066             throw OutOfMemory(
"Could not allocate persistent_src_frame", path);
 
 2067         persistent_src_frame->format      = AV_PIX_FMT_RGBA;
 
 2068         persistent_src_frame->width       = src_w;
 
 2069         persistent_src_frame->height      = src_h;
 
 2070         persistent_src_frame->linesize[0] = src_w * 4;
 
 2072     persistent_src_frame->data[0] = 
const_cast<uint8_t*
>(
 
 2073         reinterpret_cast<const uint8_t*
>(pixels)
 
 2077     if (!persistent_dst_frame) {
 
 2078         persistent_dst_frame = av_frame_alloc();
 
 2079         if (!persistent_dst_frame)
 
 2080             throw OutOfMemory(
"Could not allocate persistent_dst_frame", path);
 
 2083         AVPixelFormat dst_fmt = video_codec_ctx->pix_fmt;
 
 2086             dst_fmt = AV_PIX_FMT_NV12;
 
 2089         persistent_dst_frame->format = dst_fmt;
 
 2090         persistent_dst_frame->width  = 
info.
width;
 
 2093         persistent_dst_size = av_image_get_buffer_size(
 
 2096         if (persistent_dst_size < 0)
 
 2099         persistent_dst_buffer = 
static_cast<uint8_t*
>(
 
 2100             av_malloc(persistent_dst_size)
 
 2102         if (!persistent_dst_buffer)
 
 2103             throw OutOfMemory(
"Could not allocate persistent_dst_buffer", path);
 
 2105         av_image_fill_arrays(
 
 2106             persistent_dst_frame->data,
 
 2107             persistent_dst_frame->linesize,
 
 2108             persistent_dst_buffer,
 
 2117     if (!img_convert_ctx) {
 
 2118         int flags = SWS_FAST_BILINEAR;
 
 2120             flags = SWS_BICUBIC;
 
 2122         AVPixelFormat dst_fmt = video_codec_ctx->pix_fmt;
 
 2125             dst_fmt = AV_PIX_FMT_NV12;
 
 2128         img_convert_ctx = sws_getContext(
 
 2129             src_w, src_h, AV_PIX_FMT_RGBA,
 
 2131             flags, NULL, NULL, NULL
 
 2133         if (!img_convert_ctx)
 
 2140         persistent_src_frame->data,
 
 2141         persistent_src_frame->linesize,
 
 2143         persistent_dst_frame->data,
 
 2144         persistent_dst_frame->linesize
 
 2148     int bytes_final = 0;
 
 2149     AVPixelFormat dst_fmt = video_codec_ctx->pix_fmt;
 
 2152         dst_fmt = AV_PIX_FMT_NV12;
 
 2156     AVFrame* new_frame = allocate_avframe(
 
 2164         throw OutOfMemory(
"Could not allocate new_frame via allocate_avframe", path);
 
 2169         persistent_dst_buffer,
 
 2170         static_cast<size_t>(bytes_final)
 
 2174     add_avframe(frame, new_frame);
 
 2178 bool FFmpegWriter::write_video_packet(std::shared_ptr<Frame> frame, AVFrame *frame_final) {
 
 2179 #if (LIBAVFORMAT_VERSION_MAJOR >= 58) 
 2182         "FFmpegWriter::write_video_packet",
 
 2183         "frame->number", frame->number,
 
 2184         "oc->oformat->flags", oc->oformat->flags);
 
 2192         "FFmpegWriter::write_video_packet",
 
 2193         "frame->number", frame->number,
 
 2194         "oc->oformat->flags & AVFMT_RAWPICTURE", oc->oformat->flags & AVFMT_RAWPICTURE);
 
 2196     if (oc->oformat->flags & AVFMT_RAWPICTURE) {
 
 2200         AVPacket* pkt = av_packet_alloc();
 
 2203         av_init_packet(pkt);
 
 2206     av_packet_from_data(
 
 2207             pkt, frame_final->data[0],
 
 2208             frame_final->linesize[0] * frame_final->height);
 
 2210         pkt->flags |= AV_PKT_FLAG_KEY;
 
 2211         pkt->stream_index = video_st->index;
 
 2214         pkt->pts = video_timestamp;
 
 2217         int error_code = av_interleaved_write_frame(oc, pkt);
 
 2218         if (error_code < 0) {
 
 2220                 "FFmpegWriter::write_video_packet ERROR [" 
 2221                     + av_err2string(error_code) + 
"]",
 
 2222                 "error_code", error_code);
 
 2233         AVPacket* pkt = av_packet_alloc();
 
 2236         av_init_packet(pkt);
 
 2240         pkt->pts = pkt->dts = AV_NOPTS_VALUE;
 
 2243         frame_final->pts = video_timestamp;
 
 2246             if (!(
hw_frame = av_frame_alloc())) {
 
 2247                 std::clog << 
"Error code: av_hwframe_alloc\n";
 
 2249             if (av_hwframe_get_buffer(video_codec_ctx->hw_frames_ctx, 
hw_frame, 0) < 0) {
 
 2250                 std::clog << 
"Error code: av_hwframe_get_buffer\n";
 
 2253                 std::clog << 
"Error hw_frames_ctx.\n";
 
 2255             hw_frame->format = AV_PIX_FMT_NV12;
 
 2256             if ( av_hwframe_transfer_data(
hw_frame, frame_final, 0) < 0) {
 
 2257                 std::clog << 
"Error while transferring frame data to surface.\n";
 
 2259             av_frame_copy_props(
hw_frame, frame_final);
 
 2261 #endif // USE_HW_ACCEL 
 2263         int got_packet_ptr = 0;
 
 2271             ret = avcodec_send_frame(video_codec_ctx, 
hw_frame);  
 
 2273     #endif // USE_HW_ACCEL 
 2275             ret = avcodec_send_frame(video_codec_ctx, frame_final);
 
 2280                 "FFmpegWriter::write_video_packet (Frame not sent)");
 
 2281             if (ret == AVERROR(EAGAIN) ) {
 
 2282                 std::clog << 
"Frame EAGAIN\n";
 
 2284             if (ret == AVERROR_EOF ) {
 
 2285                 std::clog << 
"Frame AVERROR_EOF\n";
 
 2287             avcodec_send_frame(video_codec_ctx, NULL);
 
 2291                 ret = avcodec_receive_packet(video_codec_ctx, pkt);
 
 2293                 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
 
 2305         error_code = avcodec_encode_video2(video_codec_ctx, pkt, frame_final, &got_packet_ptr);
 
 2306         if (error_code != 0) {
 
 2308                 "FFmpegWriter::write_video_packet ERROR [" 
 2309                     + av_err2string(error_code) + 
"]",
 
 2310                 "error_code", error_code);
 
 2312         if (got_packet_ptr == 0) {
 
 2314                 "FFmpegWriter::write_video_packet (Frame gotpacket error)");
 
 2316 #endif // IS_FFMPEG_3_2 
 2319         if (error_code == 0 && got_packet_ptr) {
 
 2321             av_packet_rescale_ts(pkt, video_codec_ctx->time_base, video_st->time_base);
 
 2322             pkt->stream_index = video_st->index;
 
 2325             int result = av_interleaved_write_frame(oc, pkt);
 
 2328                     "FFmpegWriter::write_video_packet ERROR [" 
 2329                         + av_err2string(result) + 
"]",
 
 2344 #endif // USE_HW_ACCEL 
 2348     video_timestamp += av_rescale_q(1, av_make_q(
info.
fps.
den, 
info.
fps.
num), video_codec_ctx->time_base);
 
 2357     av_dump_format(oc, 0, path.c_str(), 1);
 
 2362     original_sample_rate = sample_rate;
 
 2363     original_channels = channels;
 
 2372     oc->strict_std_compliance = FF_COMPLIANCE_UNOFFICIAL;
 
 2374 #if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 0, 0) 
 2376     int proj = av_spherical_from_name(projection.c_str());
 
 2378         proj = AV_SPHERICAL_EQUIRECTANGULAR;
 
 2382     AVSphericalMapping* map = av_spherical_alloc(&sd_size);
 
 2386     map->projection = 
static_cast<AVSphericalProjection
>(proj);
 
 2388     map->yaw   = 
static_cast<int32_t
>(yaw_deg   * (1 << 16));
 
 2389     map->pitch = 
static_cast<int32_t
>(pitch_deg * (1 << 16));
 
 2390     map->roll  = 
static_cast<int32_t
>(roll_deg  * (1 << 16));
 
 2392     av_stream_add_side_data(video_st, AV_PKT_DATA_SPHERICAL, 
reinterpret_cast<uint8_t*
>(map), sd_size);