@@ -478,16 +478,8 @@ void FFmpegWriter::SetOption(StreamType stream, std::string name, std::string va
478478#if (LIBAVCODEC_VERSION_MAJOR >= 58)
479479 case AV_CODEC_ID_AV1 :
480480 c->bit_rate = 0 ;
481+ // AV1 only supports "crf" quality values
481482 av_opt_set_int (c->priv_data , " crf" , std::min (std::stoi (value),63 ), 0 );
482- if (strstr (info.vcodec .c_str (), " svt_av1" ) != NULL ) {
483- av_opt_set_int (c->priv_data , " preset" , 6 , 0 );
484- av_opt_set_int (c->priv_data , " forced-idr" ,1 ,0 );
485- }
486- if (strstr (info.vcodec .c_str (), " rav1e" ) != NULL ) {
487- av_opt_set_int (c->priv_data , " speed" , 7 , 0 );
488- av_opt_set_int (c->priv_data , " tile-rows" , 2 , 0 );
489- av_opt_set_int (c->priv_data , " tile-columns" , 4 , 0 );
490- }
491483 break ;
492484#endif
493485 case AV_CODEC_ID_VP8 :
@@ -547,24 +539,17 @@ void FFmpegWriter::SetOption(StreamType stream, std::string name, std::string va
547539 c->bit_rate = 0 ;
548540 if (strstr (info.vcodec .c_str (), " svt_av1" ) != NULL ) {
549541 av_opt_set_int (c->priv_data , " qp" , std::min (std::stoi (value),63 ), 0 );
550- av_opt_set_int (c->priv_data , " preset" , 6 , 0 );
551- av_opt_set_int (c->priv_data , " forced-idr" ,1 ,0 );
552542 }
553543 else if (strstr (info.vcodec .c_str (), " rav1e" ) != NULL ) {
554544 // Set number of tiles to a fixed value
555- // TODO Let user choose number of tiles
545+ // TODO Let user choose number of tiles
556546 av_opt_set_int (c->priv_data , " qp" , std::min (std::stoi (value),255 ), 0 );
557- av_opt_set_int (c->priv_data , " speed" , 7 , 0 );
558- av_opt_set_int (c->priv_data , " tile-rows" , 2 , 0 ); // number of rows
559- av_opt_set_int (c->priv_data , " tile-columns" , 4 , 0 ); // number of columns
560547 }
561548 else if (strstr (info.vcodec .c_str (), " aom" ) != NULL ) {
562549 // Set number of tiles to a fixed value
563- // TODO Let user choose number of tiles
550+ // TODO Let user choose number of tiles
564551 // libaom doesn't have qp only crf
565552 av_opt_set_int (c->priv_data , " crf" , std::min (std::stoi (value),63 ), 0 );
566- av_opt_set_int (c->priv_data , " tile-rows" , 1 , 0 ); // log2 of number of rows
567- av_opt_set_int (c->priv_data , " tile-columns" , 2 , 0 ); // log2 of number of columns
568553 }
569554 else {
570555 av_opt_set_int (c->priv_data , " crf" , std::min (std::stoi (value),63 ), 0 );
@@ -1210,7 +1195,11 @@ AVStream *FFmpegWriter::add_video_stream() {
12101195#endif
12111196
12121197 /* Init video encoder options */
1213- if (info.video_bit_rate >= 1000 ) {
1198+ if (info.video_bit_rate >= 1000
1199+ #if (LIBAVCODEC_VERSION_MAJOR >= 58)
1200+ && c->codec_id != AV_CODEC_ID_AV1
1201+ #endif
1202+ ) {
12141203 c->bit_rate = info.video_bit_rate ;
12151204 if (info.video_bit_rate >= 1500000 ) {
12161205 c->qmin = 2 ;
@@ -1219,11 +1208,46 @@ AVStream *FFmpegWriter::add_video_stream() {
12191208 // Here should be the setting for low fixed bitrate
12201209 // Defaults are used because mpeg2 otherwise had problems
12211210 } else {
1222- // Check if codec supports crf
1211+ // Check if codec supports crf or qp
12231212 switch (c->codec_id ) {
12241213#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 39, 101)
12251214#if (LIBAVCODEC_VERSION_MAJOR >= 58)
12261215 case AV_CODEC_ID_AV1 :
1216+ // TODO: Set `crf` or `qp` according to bitrate, as bitrate is not supported by these encoders yet.
1217+ if (info.video_bit_rate >= 1000 ) {
1218+ c->bit_rate = 0 ;
1219+ if (strstr (info.vcodec .c_str (), " aom" ) != NULL ) {
1220+ int calculated_quality = 35 ;
1221+ if (info.video_bit_rate < 500000 ) calculated_quality = 50 ;
1222+ if (info.video_bit_rate > 5000000 ) calculated_quality = 10 ;
1223+ av_opt_set_int (c->priv_data , " crf" , calculated_quality, 0 );
1224+ info.video_bit_rate = calculated_quality;
1225+ } else {
1226+ int calculated_quality = 50 ;
1227+ if (info.video_bit_rate < 500000 ) calculated_quality = 60 ;
1228+ if (info.video_bit_rate > 5000000 ) calculated_quality = 15 ;
1229+ av_opt_set_int (c->priv_data , " qp" , calculated_quality, 0 );
1230+ info.video_bit_rate = calculated_quality;
1231+ } // medium
1232+ }
1233+ if (strstr (info.vcodec .c_str (), " svt_av1" ) != NULL ) {
1234+ av_opt_set_int (c->priv_data , " preset" , 6 , 0 );
1235+ av_opt_set_int (c->priv_data , " forced-idr" ,1 ,0 );
1236+ }
1237+ else if (strstr (info.vcodec .c_str (), " rav1e" ) != NULL ) {
1238+ av_opt_set_int (c->priv_data , " speed" , 7 , 0 );
1239+ av_opt_set_int (c->priv_data , " tile-rows" , 2 , 0 );
1240+ av_opt_set_int (c->priv_data , " tile-columns" , 4 , 0 );
1241+ }
1242+ else if (strstr (info.vcodec .c_str (), " aom" ) != NULL ) {
1243+ // Set number of tiles to a fixed value
1244+ // TODO: Allow user to chose their own number of tiles
1245+ av_opt_set_int (c->priv_data , " tile-rows" , 1 , 0 ); // log2 of number of rows
1246+ av_opt_set_int (c->priv_data , " tile-columns" , 2 , 0 ); // log2 of number of columns
1247+ av_opt_set_int (c->priv_data , " row-mt" , 1 , 0 ); // use multiple cores
1248+ av_opt_set_int (c->priv_data , " cpu-used" , 3 , 0 ); // default is 1, usable is 4
1249+ }
1250+ // break;
12271251#endif
12281252 case AV_CODEC_ID_VP9 :
12291253 case AV_CODEC_ID_HEVC :
0 commit comments