Repository: opencv
Issue: Opencv from source build fails
Labels: duplicate

##### System information (version)

- OpenCV => :grey_question: 4.5.5
- Operating System / Platform => :grey_question: masOS Monterey 
- Compiler => :grey_question: terminal

##### Detailed description

I'm trying to build opencv from source on my Mac. I keep running into an error which causes build to fail.

##### Steps to reproduce

Here is the full error log:
```
(base) aniekan@Aniekans-MacBook-Pro opencv_build % sudo make -j8
[  0%] Built target opencv_videoio_plugins
[  0%] Built target opencv_highgui_plugins
[  0%] Built target ittnotify
[  3%] Built target ippiw
[  5%] Built target libopenjp2
[ 12%] Built target libjpeg-turbo
[ 13%] Built target libtiff
[ 14%] Built target quirc
[ 15%] Built target libpng
[ 15%] Built target gen_opencv_python_source
[ 25%] Built target libwebp
[ 26%] Built target ade
[ 30%] Built target libprotobuf
Consolidate compiler generated dependencies of target opencv_core
[ 39%] Built target opencv_core
[ 40%] Built target opencv_flann
[ 40%] Built target opencv_version
[ 41%] Built target opencv_ml
[ 48%] Built target opencv_imgproc
[ 50%] Built target opencv_photo
[ 52%] Built target opencv_features2d
[ 54%] Built target opencv_imgcodecs
Consolidate compiler generated dependencies of target opencv_videoio
[ 64%] Built target opencv_dnn
[ 69%] Built target opencv_calib3d
[ 69%] Built target opencv_model_diagnostics
[ 69%] Building CXX object modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_ffmpeg.cpp.o
[ 70%] Built target opencv_objdetect
[ 72%] Built target opencv_stitching
[ 74%] Built target opencv_video
[ 85%] Built target opencv_gapi
In file included from /Users/aniekan/opencv/modules/videoio/src/cap_ffmpeg.cpp:50:
/Users/aniekan/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:542:5: error: unknown type name 'AVBSFContext'; did you mean 'AVIOContext'?
    AVBSFContext* bsfc;
    ^~~~~~~~~~~~
    AVIOContext
/usr/local/Cellar/ffmpeg/5.0/include/libavformat/avio.h:318:3: note: 'AVIOContext' declared here
} AVIOContext;
  ^
In file included from /Users/aniekan/opencv/modules/videoio/src/cap_ffmpeg.cpp:50:
/Users/aniekan/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:620:34: error: no member named 'codec' in 'AVStream'
        avcodec_close( video_st->codec );
                       ~~~~~~~~  ^
/Users/aniekan/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:658:9: error: use of undeclared identifier 'av_bsf_free'; did you mean 'av_opt_free'?
        av_bsf_free(&bsfc);
        ^~~~~~~~~~~
        av_opt_free
/usr/local/Cellar/ffmpeg/5.0/include/libavutil/opt.h:456:6: note: 'av_opt_free' declared here
void av_opt_free(void *obj);
     ^
In file included from /Users/aniekan/opencv/modules/videoio/src/cap_ffmpeg.cpp:50:
/Users/aniekan/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:803:39: error: unknown type name 'AVLockOp'
static int LockCallBack(void **mutex, AVLockOp op)
                                      ^
/Users/aniekan/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:885:9: error: use of undeclared identifier 'av_register_all'
        av_register_all();
        ^
/Users/aniekan/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:892:9: error: use of undeclared identifier 'av_lockmgr_register'
        av_lockmgr_register(NULL);
        ^
/Users/aniekan/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:1004:22: error: assigning to 'AVInputFormat *' from 'const AVInputFormat *' discards qualifiers
      input_format = av_find_input_format(entry->value);
                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/aniekan/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:1023:47: error: no member named 'codec' in 'AVStream'
        AVCodecContext* enc = ic->streams[i]->codec;
                              ~~~~~~~~~~~~~~  ^
/Users/aniekan/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:1109:33: error: assigning to 'AVCodec *' from 'const AVCodec *' discards qualifiers
                        codec = avcodec_find_decoder(enc->codec_id);
                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/aniekan/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:1118:33: error: assigning to 'AVCodec *' from 'const AVCodec *' discards qualifiers
                        codec = avcodec_find_decoder_by_name(video_codec_param->value);
                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/aniekan/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:1238:19: error: unknown type name 'AVBitStreamFilter'
            const AVBitStreamFilter * bsf = av_bsf_get_by_name(filterName);
                  ^
/Users/aniekan/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:1238:45: error: use of undeclared identifier 'av_bsf_get_by_name'
            const AVBitStreamFilter * bsf = av_bsf_get_by_name(filterName);
                                            ^
/Users/aniekan/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:1250:43: error: no member named 'par_in' in 'AVIOContext'
            avcodec_parameters_copy(bsfc->par_in, ic->streams[video_stream]->codecpar);
                                    ~~~~  ^
/Users/aniekan/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:1251:19: error: use of undeclared identifier 'av_bsf_init'
            err = av_bsf_init(bsfc);
                  ^
/Users/aniekan/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:1275:19: error: use of undeclared identifier 'av_bsf_send_packet'; did you mean 'avcodec_send_packet'?
        int err = av_bsf_send_packet(bsfc, &packet);
                  ^~~~~~~~~~~~~~~~~~
                  avcodec_send_packet
/usr/local/Cellar/ffmpeg/5.0/include/libavcodec/avcodec.h:2581:5: note: 'avcodec_send_packet' declared here
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt);
    ^
In file included from /Users/aniekan/opencv/modules/videoio/src/cap_ffmpeg.cpp:50:
/Users/aniekan/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:1275:38: error: cannot initialize a parameter of type 'AVCodecContext *' with an lvalue of type 'AVIOContext *'
        int err = av_bsf_send_packet(bsfc, &packet);
                                     ^~~~
/usr/local/Cellar/ffmpeg/5.0/include/libavcodec/avcodec.h:2581:41: note: passing argument to parameter 'avctx' here
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt);
                                        ^
In file included from /Users/aniekan/opencv/modules/videoio/src/cap_ffmpeg.cpp:50:
/Users/aniekan/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:1281:15: error: use of undeclared identifier 'av_bsf_receive_packet'; did you mean 'avcodec_receive_packet'?
        err = av_bsf_receive_packet(bsfc, &packet_filtered);
              ^~~~~~~~~~~~~~~~~~~~~
              avcodec_receive_packet
/usr/local/Cellar/ffmpeg/5.0/include/libavcodec/avcodec.h:2657:5: note: 'avcodec_receive_packet' declared here
int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt);
    ^
In file included from /Users/aniekan/opencv/modules/videoio/src/cap_ffmpeg.cpp:50:
/Users/aniekan/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:1281:37: error: cannot initialize a parameter of type 'AVCodecContext *' with an lvalue of type 'AVIOContext *'
        err = av_bsf_receive_packet(bsfc, &packet_filtered);
                                    ^~~~
/usr/local/Cellar/ffmpeg/5.0/include/libavcodec/avcodec.h:2657:44: note: passing argument to parameter 'avctx' here
int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt);
                                           ^
In file included from /Users/aniekan/opencv/modules/videoio/src/cap_ffmpeg.cpp:50:
/Users/aniekan/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:1325:45: error: no member named 'codec' in 'AVStream'
    valid = avcodec_receive_frame(video_st->codec, picture) >= 0;
                                  ~~~~~~~~  ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
make[2]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_ffmpeg.cpp.o] Error 1
make[1]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/all] Error 2
make: *** [all] Error 2
```

##### Issue submission checklist

 - [x] I report the issue, it's not a question
   <!--
   OpenCV team works with forum.opencv.org, Stack Overflow and other communities
   to discuss problems. Tickets with questions without a real issue statement will be
   closed.
   -->
 - [x] I checked the problem with documentation, FAQ, open issues,
       forum.opencv.org, Stack Overflow, etc and have not found any solution
   <!--
   Places to check:
   * OpenCV documentation: https://docs.opencv.org
   * FAQ page: https://github.com/opencv/opencv/wiki/FAQ
   * OpenCV forum: https://forum.opencv.org
   * OpenCV issue tracker: https://github.com/opencv/opencv/issues?q=is%3Aissue
   * Stack Overflow branch: https://stackoverflow.com/questions/tagged/opencv
   -->
 - [x] I updated to the latest OpenCV version and the issue is still there
   <!--
   master branch for OpenCV 4.x and 3.4 branch for OpenCV 3.x releases.
   OpenCV team supports only the latest release for each branch.
   The ticket is closed if the problem is not reproduced with the modern version.
   -->
 - [x] There is reproducer code and related data files: videos, images, onnx, etc
   <!--
   The best reproducer -- test case for OpenCV that we can add to the library.
   Recommendations for media files and binary files:
   * Try to reproduce the issue with images and videos in opencv_extra repository
     to reduce attachment size
   * Use PNG for images, if you report some CV related bug, but not image reader
     issue
   * Attach the image as an archive to the ticket, if you report some reader issue.
     Image hosting services compress images and it breaks the repro code.
   * Provide ONNX file for some public model or ONNX file with random weights,
     if you report ONNX parsing or handling issue. Architecture details diagram
     from netron tool can be very useful too. See https://lutzroeder.github.io/netron/
   -->