AVStream* AVFormatWriter::AddAudioStream(void) { AVCodecContext *c; AVStream *st; st = avformat_new_stream(m_ctx, NULL); if (!st) { LOG(VB_RECORD, LOG_ERR, LOC + "AddAudioStream(): avformat_new_stream() failed"); return NULL; } st->id = 1; c = st->codec; c->codec_id = m_ctx->oformat->audio_codec; c->codec_type = AVMEDIA_TYPE_AUDIO; if (!gCoreContext->GetSetting("HLSAUDIO").isEmpty()) { if (gCoreContext->GetSetting("HLSAUDIO") == "aac") c->sample_fmt = AV_SAMPLE_FMT_FLT; else c->sample_fmt = AV_SAMPLE_FMT_S16; } else #if CONFIG_LIBMP3LAME_ENCODER || CONFIG_LIBFAAC_ENCODER c->sample_fmt = AV_SAMPLE_FMT_S16; #else c->sample_fmt = AV_SAMPLE_FMT_FLT; #endif m_audioBytesPerSample = m_audioChannels * av_get_bytes_per_sample(c->sample_fmt); c->bit_rate = m_audioBitrate; c->sample_rate = m_audioSampleRate; c->channels = m_audioChannels; // c->flags |= CODEC_FLAG_QSCALE; // VBR // c->global_quality = blah; if (!m_avVideoCodec) { c->time_base = GetCodecTimeBase(); st->time_base.den = 90000; st->time_base.num = 1; } if(m_ctx->oformat->flags & AVFMT_GLOBALHEADER) c->flags |= CODEC_FLAG_GLOBAL_HEADER; return st; }
AVStream* AVFormatWriter::AddAudioStream(void) { AVCodecContext *c; AVStream *st; st = av_new_stream(m_ctx, 1); if (!st) { LOG(VB_RECORD, LOG_ERR, LOC + "AddAudioStream(): av_new_stream() failed"); return NULL; } c = st->codec; c->codec_id = m_ctx->oformat->audio_codec; c->codec_type = AVMEDIA_TYPE_AUDIO; c->sample_fmt = AV_SAMPLE_FMT_S16; m_audioBytesPerSample = m_audioChannels * 2; c->bit_rate = m_audioBitrate; c->sample_rate = m_audioSampleRate; c->channels = m_audioChannels; // c->flags |= CODEC_FLAG_QSCALE; // VBR // c->global_quality = blah; if (!m_avVideoCodec) { // c->time_base = (AVRational){1, m_audioSampleRate}; // st->time_base = c->time_base; c->time_base = GetCodecTimeBase(); st->time_base.den = 90000; st->time_base.num = 1; } // LOG(VB_RECORD, LOG_ERR, LOC + QString("AddAudioStream(): br: %1, sr, %2, c: %3, tb: %4/%5").arg(c->bit_rate).arg(c->sample_rate).arg(c->channels).arg(c->time_base.den).arg(c->time_base.num)); if (c->codec_id == CODEC_ID_AAC) c->profile = FF_PROFILE_AAC_MAIN; if(m_ctx->oformat->flags & AVFMT_GLOBALHEADER) c->flags |= CODEC_FLAG_GLOBAL_HEADER; return st; }
AVStream* AVFormatWriter::AddAudioStream(void) { AVCodecContext *c; AVStream *st; st = avformat_new_stream(m_ctx, NULL); if (!st) { LOG(VB_RECORD, LOG_ERR, LOC + "AddAudioStream(): avformat_new_stream() failed"); return NULL; } st->id = 1; c = st->codec; c->codec_id = m_ctx->oformat->audio_codec; c->codec_type = AVMEDIA_TYPE_AUDIO; c->bit_rate = m_audioBitrate; c->sample_rate = m_audioFrameRate; c->channels = m_audioChannels; // c->flags |= CODEC_FLAG_QSCALE; // VBR // c->global_quality = blah; if (!m_avVideoCodec) { c->time_base = GetCodecTimeBase(); st->time_base.den = 90000; st->time_base.num = 1; } if(m_ctx->oformat->flags & AVFMT_GLOBALHEADER) c->flags |= CODEC_FLAG_GLOBAL_HEADER; return st; }
AVStream* AVFormatWriter::AddVideoStream(void) { AVCodecContext *c; AVStream *st; st = av_new_stream(m_ctx, 0); if (!st) { LOG(VB_RECORD, LOG_ERR, LOC + "AddVideoStream(): av_new_stream() failed"); return NULL; } c = st->codec; c->codec_id = m_ctx->oformat->video_codec; c->codec_type = AVMEDIA_TYPE_VIDEO; c->bit_rate = m_videoBitrate; c->width = m_width; c->height = m_height; // c->sample_aspect_ratio.num = (int)floor(m_aspect * 10000); // c->sample_aspect_ratio.den = 10000; c->time_base = GetCodecTimeBase(); st->time_base.den = 90000; st->time_base.num = 1; st->r_frame_rate.num = 0; st->r_frame_rate.den = 0; c->gop_size = m_keyFrameDist; c->pix_fmt = PIX_FMT_YUV420P; c->thread_count = m_encodingThreadCount; if (c->codec_id == CODEC_ID_MPEG2VIDEO) { c->max_b_frames = 2; } else if (c->codec_id == CODEC_ID_MPEG1VIDEO) { c->mb_decision = 2; } else if (c->codec_id == CODEC_ID_H264) { c->coder_type = 0; c->max_b_frames = 0; c->slices = 8; c->level = 13; c->flags |= CODEC_FLAG_LOOP_FILTER; c->me_cmp |= 1; c->partitions |= X264_PART_I8X8 + X264_PART_I4X4 + X264_PART_P8X8 + X264_PART_B8X8; c->me_method = ME_HEX; c->me_subpel_quality = 6; c->me_range = 16; c->keyint_min = 25; c->scenechange_threshold = 40; c->i_quant_factor = 0.71; c->b_frame_strategy = 1; c->qcompress = 0.6; c->qmin = 10; c->qmax = 51; c->max_qdiff = 4; c->refs = 3; c->directpred = 1; c->rc_lookahead = 0; c->flags2 |= CODEC_FLAG2_FASTPSKIP; c->flags2 |= CODEC_FLAG2_8X8DCT; c->flags2 ^= CODEC_FLAG2_8X8DCT; c->flags2 |= CODEC_FLAG2_WPRED; c->flags2 ^= CODEC_FLAG2_WPRED; } if(m_ctx->oformat->flags & AVFMT_GLOBALHEADER) c->flags |= CODEC_FLAG_GLOBAL_HEADER; // LOG(VB_RECORD, LOG_ERR, LOC + QString("AddVideoStream(): br: %1, gs: %2, tb: %3/%4, w: %5, h: %6").arg(c->bit_rate).arg(c->gop_size).arg(c->time_base.den).arg(c->time_base.num).arg(c->width).arg(c->height)); return st; }
AVStream* AVFormatWriter::AddVideoStream(void) { AVCodecContext *c; AVStream *st; AVCodec *codec; st = avformat_new_stream(m_ctx, NULL); if (!st) { LOG(VB_RECORD, LOG_ERR, LOC + "AddVideoStream(): avformat_new_stream() failed"); return NULL; } st->id = 0; c = st->codec; codec = avcodec_find_encoder(m_ctx->oformat->video_codec); if (!codec) { LOG(VB_RECORD, LOG_ERR, LOC + "AddVideoStream(): avcodec_find_encoder() failed"); return NULL; } avcodec_get_context_defaults3(c, codec); c->codec = codec; c->codec_id = m_ctx->oformat->video_codec; c->codec_type = AVMEDIA_TYPE_VIDEO; c->bit_rate = m_videoBitrate; c->width = m_width; c->height = m_height; // c->sample_aspect_ratio.num = (int)floor(m_aspect * 10000); // c->sample_aspect_ratio.den = 10000; c->time_base = GetCodecTimeBase(); st->time_base.den = 90000; st->time_base.num = 1; st->r_frame_rate.num = 0; st->r_frame_rate.den = 0; c->gop_size = m_keyFrameDist; c->pix_fmt = AV_PIX_FMT_YUV420P; c->thread_count = m_encodingThreadCount; c->thread_type = FF_THREAD_SLICE; if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO) { c->max_b_frames = 2; } else if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO) { c->mb_decision = 2; } else if (c->codec_id == AV_CODEC_ID_H264) { // Try to provide the widest software/device support by automatically using // the Baseline profile where the given bitrate and resolution permits if ((c->height > 720) || // Approximate highest resolution supported by Baseline 3.1 (c->bit_rate > 1000000)) // 14,000 Kbps aka 14Mbps maximum permissable rate for Baseline 3.1 { c->level = 40; av_opt_set(c->priv_data, "profile", "main", 0); } else if ((c->height > 576) || // Approximate highest resolution supported by Baseline 3.0 (c->bit_rate > 1000000)) // 10,000 Kbps aka 10Mbps maximum permissable rate for Baseline 3.0 { c->level = 31; av_opt_set(c->priv_data, "profile", "baseline", 0); } else { c->level = 30; // Baseline 3.0 is the most widely supported, but it's limited to SD av_opt_set(c->priv_data, "profile", "baseline", 0); } c->coder_type = 0; c->max_b_frames = 0; c->slices = 8; c->flags |= CODEC_FLAG_LOOP_FILTER; c->me_cmp |= 1; c->me_method = ME_HEX; c->me_subpel_quality = 6; c->me_range = 16; c->keyint_min = 25; c->scenechange_threshold = 40; c->i_quant_factor = 0.71; c->b_frame_strategy = 1; c->qcompress = 0.6; c->qmin = 10; c->qmax = 51; c->max_qdiff = 4; c->refs = 3; c->trellis = 0; av_opt_set(c, "partitions", "i8x8,i4x4,p8x8,b8x8", 0); av_opt_set_int(c, "direct-pred", 1, 0); av_opt_set_int(c, "rc-lookahead", 0, 0); av_opt_set_int(c, "fast-pskip", 1, 0); av_opt_set_int(c, "mixed-refs", 1, 0); av_opt_set_int(c, "8x8dct", 0, 0); av_opt_set_int(c, "weightb", 0, 0); av_opt_set(c->priv_data, "preset", m_encodingPreset.toLatin1().constData(), 0); av_opt_set(c->priv_data, "tune", m_encodingTune.toLatin1().constData(), 0); } if(m_ctx->oformat->flags & AVFMT_GLOBALHEADER) c->flags |= CODEC_FLAG_GLOBAL_HEADER; return st; }
AVStream* AVFormatWriter::AddVideoStream(void) { AVCodecContext *c; AVStream *st; AVCodec *codec; st = avformat_new_stream(m_ctx, NULL); if (!st) { LOG(VB_RECORD, LOG_ERR, LOC + "AddVideoStream(): avformat_new_stream() failed"); return NULL; } st->id = 0; c = st->codec; codec = avcodec_find_encoder(m_ctx->oformat->video_codec); if (!codec) { LOG(VB_RECORD, LOG_ERR, LOC + "AddVideoStream(): avcodec_find_encoder() failed"); return false; } avcodec_get_context_defaults3(c, codec); c->codec = codec; c->codec_id = m_ctx->oformat->video_codec; c->codec_type = AVMEDIA_TYPE_VIDEO; c->bit_rate = m_videoBitrate; c->width = m_width; c->height = m_height; // c->sample_aspect_ratio.num = (int)floor(m_aspect * 10000); // c->sample_aspect_ratio.den = 10000; c->time_base = GetCodecTimeBase(); st->time_base.den = 90000; st->time_base.num = 1; st->r_frame_rate.num = 0; st->r_frame_rate.den = 0; c->gop_size = m_keyFrameDist; c->pix_fmt = PIX_FMT_YUV420P; c->thread_count = m_encodingThreadCount; c->thread_type = FF_THREAD_SLICE; if (c->codec_id == CODEC_ID_MPEG2VIDEO) { c->max_b_frames = 2; } else if (c->codec_id == CODEC_ID_MPEG1VIDEO) { c->mb_decision = 2; } else if (c->codec_id == CODEC_ID_H264) { if ((c->width > 480) || (c->bit_rate > 600000)) { c->level = 31; av_opt_set(c->priv_data, "profile", "main", 0); } else { c->level = 30; av_opt_set(c->priv_data, "profile", "baseline", 0); } c->coder_type = 0; c->max_b_frames = 0; c->slices = 8; c->flags |= CODEC_FLAG_LOOP_FILTER; c->me_cmp |= 1; c->me_method = ME_HEX; c->me_subpel_quality = 6; c->me_range = 16; c->keyint_min = 25; c->scenechange_threshold = 40; c->i_quant_factor = 0.71; c->b_frame_strategy = 1; c->qcompress = 0.6; c->qmin = 10; c->qmax = 51; c->max_qdiff = 4; c->refs = 3; c->trellis = 0; av_opt_set(c, "partitions", "i8x8,i4x4,p8x8,b8x8", 0); av_opt_set_int(c, "direct-pred", 1, 0); av_opt_set_int(c, "rc-lookahead", 0, 0); av_opt_set_int(c, "fast-pskip", 1, 0); av_opt_set_int(c, "mixed-refs", 1, 0); av_opt_set_int(c, "8x8dct", 0, 0); av_opt_set_int(c, "weightb", 0, 0); } if(m_ctx->oformat->flags & AVFMT_GLOBALHEADER) c->flags |= CODEC_FLAG_GLOBAL_HEADER; return st; }