nareshmlx/opencv-pr huggingface dataset:
**File:** 3rdparty/libjpeg-turbo/CMakeLists.txt
**Change Type:** modified
**Context:** PR #22372: [GSoC 2022] nasm/simd support for libjpeg-turbo

**Code Changes:**
```diff
@@ -15,8 +15,55 @@ endif()
 
 message(STATUS "libjpeg-turbo: VERSION = ${VERSION}, BUILD = ${BUILD}")
 
+math(EXPR BITS "${CMAKE_SIZEOF_VOID_P} * 8")
+string(TOLOWER ${CMAKE_SYSTEM_PROCESSOR} CMAKE_SYSTEM_PROCESSOR_LC)
+
+if(CMAKE_SYSTEM_PROCESSOR_LC MATCHES "x86_64" OR
+  CMAKE_SYSTEM_PROCESSOR_LC MATCHES "amd64" OR
+  CMAKE_SYSTEM_PROCESSOR_LC MATCHES "i[0-9]86" OR
```

Review comment:
Briefly, NEON, AVX2, AVX512, etc support are not guaranteed on all CPUs. There are Atom chips, even without AVX2 support. The same for NEON. Some NVIDIA Tegra chips has arm-v7a architecture, but does not implement NEON extension. NEON code will raise SIGILL here. `CPU_BASELINE_FINAL` contains all hardware specific optimizations that are enabled by default and does not participate in dynamic dispatch. libjpeg turbo should use the list of options to enable proper optimizations.
**File:** 3rdparty/libjpeg-turbo/CMakeLists.txt
**Change Type:** modified
**Context:** PR #22372: [GSoC 2022] nasm/simd support for libjpeg-turbo

**Code Changes:**
```diff
@@ -15,8 +15,55 @@ endif()
 
 message(STATUS "libjpeg-turbo: VERSION = ${VERSION}, BUILD = ${BUILD}")
 
+math(EXPR BITS "${CMAKE_SIZEOF_VOID_P} * 8")
+string(TOLOWER ${CMAKE_SYSTEM_PROCESSOR} CMAKE_SYSTEM_PROCESSOR_LC)
+
+if(CMAKE_SYSTEM_PROCESSOR_LC MATCHES "x86_64" OR
+  CMAKE_SYSTEM_PROCESSOR_LC MATCHES "amd64" OR
+  CMAKE_SYSTEM_PROCESSOR_LC MATCHES "i[0-9]86" OR
```

Review comment:
For example you can take a look on Carotene HAL management for ARM: https://github.com/opencv/opencv/blob/9fcf0152140c449f74678add820e81de3e69b8b0/CMakeLists.txt#L880
**File:** 3rdparty/libjpeg-turbo/CMakeLists.txt
**Change Type:** modified
**Context:** PR #22372: [GSoC 2022] nasm/simd support for libjpeg-turbo
**Review Line:** 31

**Code Changes:**
```diff
+  if(BITS EQUAL 64 OR CMAKE_C_COMPILER_ABI MATCHES "ELF X32")
+    set(CPU_TYPE x86_64)
+  else()
+    set(CPU_TYPE i386)
+  endif()
+  if(NOT CMAKE_SYSTEM_PROCESSOR STREQUAL ${CPU_TYPE})
+    set(CMAKE_SYSTEM_PROCESSOR ${CPU_TYPE})
+  endif()
+elseif(CMAKE_SYSTEM_PROCESSOR_LC STREQUAL "aarch64" OR
+  CMAKE_SYSTEM_PROCESSOR_LC MATCHES "^arm")
+  if(BITS EQUAL 64)
```

Review comment:
Looks strange. Why do you need this?
**File:** 3rdparty/libjpeg-turbo/CMakeLists.txt
**Change Type:** modified
**Context:** PR #22372: [GSoC 2022] nasm/simd support for libjpeg-turbo

**Code Changes:**
```diff
@@ -15,8 +15,55 @@ endif()
 
 message(STATUS "libjpeg-turbo: VERSION = ${VERSION}, BUILD = ${BUILD}")
 
+math(EXPR BITS "${CMAKE_SIZEOF_VOID_P} * 8")
+string(TOLOWER ${CMAKE_SYSTEM_PROCESSOR} CMAKE_SYSTEM_PROCESSOR_LC)
+
+if(CMAKE_SYSTEM_PROCESSOR_LC MATCHES "x86_64" OR
+  CMAKE_SYSTEM_PROCESSOR_LC MATCHES "amd64" OR
+  CMAKE_SYSTEM_PROCESSOR_LC MATCHES "i[0-9]86" OR
```

Review comment:
OpenCV CMake system does not support build for several architectures in a single step. I do not think that the check is required.
**File:** 3rdparty/libjpeg-turbo/CMakeLists.txt
**Change Type:** modified
**Context:** PR #22372: [GSoC 2022] nasm/simd support for libjpeg-turbo
**Review Line:** 26

**Code Changes:**
```diff
+if(CMAKE_SYSTEM_PROCESSOR_LC MATCHES "x86_64" OR
+  CMAKE_SYSTEM_PROCESSOR_LC MATCHES "amd64" OR
+  CMAKE_SYSTEM_PROCESSOR_LC MATCHES "i[0-9]86" OR
+  CMAKE_SYSTEM_PROCESSOR_LC MATCHES "x86" OR
+  CMAKE_SYSTEM_PROCESSOR_LC MATCHES "ia32")
+  if(BITS EQUAL 64 OR CMAKE_C_COMPILER_ABI MATCHES "ELF X32")
+    set(CPU_TYPE x86_64)
+  else()
+    set(CPU_TYPE i386)
+  endif()
+  if(NOT CMAKE_SYSTEM_PROCESSOR STREQUAL ${CPU_TYPE})
```

Review comment:
`CMAKE_SIZEOF_VOID_P` is already taken in account when X86_64 and X86 are set. You do not need to warry about it here. See https://github.com/opencv/opencv/blob/9fcf0152140c449f74678add820e81de3e69b8b0/cmake/OpenCVDetectCXXCompiler.cmake#L121
**File:** 3rdparty/libjpeg-turbo/CMakeLists.txt
**Change Type:** modified
**Context:** PR #22372: [GSoC 2022] nasm/simd support for libjpeg-turbo
**Review Line:** 36

**Code Changes:**
```diff
+  if(NOT CMAKE_SYSTEM_PROCESSOR STREQUAL ${CPU_TYPE})
+    set(CMAKE_SYSTEM_PROCESSOR ${CPU_TYPE})
+  endif()
+elseif(CMAKE_SYSTEM_PROCESSOR_LC STREQUAL "aarch64" OR
+  CMAKE_SYSTEM_PROCESSOR_LC MATCHES "^arm")
+  if(BITS EQUAL 64)
+    set(CPU_TYPE arm64)
+  else()
+    set(CPU_TYPE arm)
+  endif()
+elseif(CMAKE_SYSTEM_PROCESSOR_LC MATCHES "^ppc" OR
```

Review comment:
The same for ARM as for x86. See https://github.com/opencv/opencv/blob/9fcf0152140c449f74678add820e81de3e69b8b0/cmake/OpenCVDetectCXXCompiler.cmake#L121
**File:** 3rdparty/libjpeg-turbo/CMakeLists.txt
**Change Type:** modified
**Context:** PR #22372: [GSoC 2022] nasm/simd support for libjpeg-turbo

**Code Changes:**
```diff
@@ -15,8 +15,55 @@ endif()
 
 message(STATUS "libjpeg-turbo: VERSION = ${VERSION}, BUILD = ${BUILD}")
 
+math(EXPR BITS "${CMAKE_SIZEOF_VOID_P} * 8")
+string(TOLOWER ${CMAKE_SYSTEM_PROCESSOR} CMAKE_SYSTEM_PROCESSOR_LC)
+
+if(CMAKE_SYSTEM_PROCESSOR_LC MATCHES "x86_64" OR
+  CMAKE_SYSTEM_PROCESSOR_LC MATCHES "amd64" OR
+  CMAKE_SYSTEM_PROCESSOR_LC MATCHES "i[0-9]86" OR
```

Review comment:
X86, X86_64, AARCH64, etc flags are set for Apple systems too. You do not need to handle it separately.
**File:** 3rdparty/libjpeg-turbo/CMakeLists.txt
**Change Type:** modified
**Context:** PR #22372: [GSoC 2022] nasm/simd support for libjpeg-turbo
**Review Line:** 57

**Code Changes:**
```diff
+if(CMAKE_OSX_ARCHITECTURES MATCHES "ppc")
+  set(CPU_TYPE powerpc)
+endif()
+if(MSVC_IDE AND CMAKE_GENERATOR_PLATFORM MATCHES "arm64")
+  set(CPU_TYPE arm64)
+endif()
+
+message(STATUS "${BITS}-bit build (${CPU_TYPE})")
+
+OCV_OPTION(ENABLE_LIBJPEG_TURBO_SIMD "Include SIMD extensions for libjpeg-turbo, if available for this platform" (NOT CV_DISABLE_OPTIMIZATION)
+  VISIBLE_IF BUILD_JPEG)
```

Review comment:
On Windows as well.
**File:** 3rdparty/libjpeg-turbo/CMakeLists.txt
**Change Type:** modified
**Context:** PR #22372: [GSoC 2022] nasm/simd support for libjpeg-turbo

**Code Changes:**
```diff
@@ -15,8 +15,55 @@ endif()
 
 message(STATUS "libjpeg-turbo: VERSION = ${VERSION}, BUILD = ${BUILD}")
 
+math(EXPR BITS "${CMAKE_SIZEOF_VOID_P} * 8")
+string(TOLOWER ${CMAKE_SYSTEM_PROCESSOR} CMAKE_SYSTEM_PROCESSOR_LC)
+
+if(CMAKE_SYSTEM_PROCESSOR_LC MATCHES "x86_64" OR
+  CMAKE_SYSTEM_PROCESSOR_LC MATCHES "amd64" OR
+  CMAKE_SYSTEM_PROCESSOR_LC MATCHES "i[0-9]86" OR
```

Review comment:
OpenCV has `CV_DISABLE_OPTIMIZATION` flag in CMake to force disable platform specific optimizations. It's used for debugging purpose mostly, to catch SIMD path misbehavior and compare performance. I propose to replace default value by `(NOT CV_DISABLE_OPTIMIZATION)` how it's done for Carotete: https://github.com/opencv/opencv/blob/9fcf0152140c449f74678add820e81de3e69b8b0/CMakeLists.txt#L246
**File:** 3rdparty/libjpeg-turbo/CMakeLists.txt
**Change Type:** modified
**Context:** PR #22372: [GSoC 2022] nasm/simd support for libjpeg-turbo

**Code Changes:**
```diff
@@ -15,8 +15,55 @@ endif()
 
 message(STATUS "libjpeg-turbo: VERSION = ${VERSION}, BUILD = ${BUILD}")
 
+math(EXPR BITS "${CMAKE_SIZEOF_VOID_P} * 8")
+string(TOLOWER ${CMAKE_SYSTEM_PROCESSOR} CMAKE_SYSTEM_PROCESSOR_LC)
+
+if(CMAKE_SYSTEM_PROCESSOR_LC MATCHES "x86_64" OR
+  CMAKE_SYSTEM_PROCESSOR_LC MATCHES "amd64" OR
+  CMAKE_SYSTEM_PROCESSOR_LC MATCHES "i[0-9]86" OR
```

Review comment:
Also optimization baseline should be checked.
**File:** 3rdparty/libjpeg-turbo/CMakeLists.txt
**Change Type:** modified
**Context:** PR #22372: [GSoC 2022] nasm/simd support for libjpeg-turbo

**Code Changes:**
```diff
@@ -15,8 +15,55 @@ endif()
 
 message(STATUS "libjpeg-turbo: VERSION = ${VERSION}, BUILD = ${BUILD}")
 
+math(EXPR BITS "${CMAKE_SIZEOF_VOID_P} * 8")
+string(TOLOWER ${CMAKE_SYSTEM_PROCESSOR} CMAKE_SYSTEM_PROCESSOR_LC)
+
+if(CMAKE_SYSTEM_PROCESSOR_LC MATCHES "x86_64" OR
+  CMAKE_SYSTEM_PROCESSOR_LC MATCHES "amd64" OR
+  CMAKE_SYSTEM_PROCESSOR_LC MATCHES "i[0-9]86" OR
```

Review comment:
If this code is borrowed from upstream, then we should keep it with minimal changes (to keep updates from upstream reliable)
**File:** 3rdparty/libjpeg-turbo/CMakeLists.txt
**Change Type:** modified
**Context:** PR #22372: [GSoC 2022] nasm/simd support for libjpeg-turbo

**Code Changes:**
```diff
@@ -15,8 +15,55 @@ endif()
 
 message(STATUS "libjpeg-turbo: VERSION = ${VERSION}, BUILD = ${BUILD}")
 
+math(EXPR BITS "${CMAKE_SIZEOF_VOID_P} * 8")
+string(TOLOWER ${CMAKE_SYSTEM_PROCESSOR} CMAKE_SYSTEM_PROCESSOR_LC)
+
+if(CMAKE_SYSTEM_PROCESSOR_LC MATCHES "x86_64" OR
+  CMAKE_SYSTEM_PROCESSOR_LC MATCHES "amd64" OR
+  CMAKE_SYSTEM_PROCESSOR_LC MATCHES "i[0-9]86" OR
```

Review comment:
I borrowed from upstream. It makes sense to keep it as original. What do you mean by minimal changes?
**File:** CMakeLists.txt
**Change Type:** modified
**Context:** PR #22372: [GSoC 2022] nasm/simd support for libjpeg-turbo

**Code Changes:**
```diff
@@ -1341,6 +1341,16 @@ if(WITH_JPEG OR HAVE_JPEG)
     status("    JPEG:" NO)
   elseif(BUILD_JPEG)
     status("    JPEG:" "build-${JPEG_LIBRARY} (ver ${JPEG_LIB_VERSION})")
+    if(ENABLE_LIBJPEG_TURBO_SIMD)
+      status("        SIMD Support Request:" "YES")
+      if(HAVE_LIBJPEG_TURBO_SIMD)
+        status("        SIMD Support:" "YES")
+      else()
+        status("        SIMD Support:" "NO")
```

Review comment:
I realized i do not need that part. I removed but did not pushed yet. It does not affect anything anyway.
**File:** CMakeLists.txt
**Change Type:** modified
**Context:** PR #22372: [GSoC 2022] nasm/simd support for libjpeg-turbo

**Code Changes:**
```diff
@@ -1341,6 +1341,16 @@ if(WITH_JPEG OR HAVE_JPEG)
     status("    JPEG:" NO)
   elseif(BUILD_JPEG)
     status("    JPEG:" "build-${JPEG_LIBRARY} (ver ${JPEG_LIB_VERSION})")
+    if(ENABLE_LIBJPEG_TURBO_SIMD)
+      status("        SIMD Support Request:" "YES")
+      if(HAVE_LIBJPEG_TURBO_SIMD)
+        status("        SIMD Support:" "YES")
+      else()
+        status("        SIMD Support:" "NO")
```

Review comment:
Changed this line temporary to build libjpeg-turbo from source in linux too.
**File:** 3rdparty/libjpeg-turbo/CMakeLists.txt
**Change Type:** modified
**Context:** PR #22372: [GSoC 2022] nasm/simd support for libjpeg-turbo

**Code Changes:**
```diff
@@ -15,8 +15,55 @@ endif()
 
 message(STATUS "libjpeg-turbo: VERSION = ${VERSION}, BUILD = ${BUILD}")
 
+math(EXPR BITS "${CMAKE_SIZEOF_VOID_P} * 8")
+string(TOLOWER ${CMAKE_SYSTEM_PROCESSOR} CMAKE_SYSTEM_PROCESSOR_LC)
+
+if(CMAKE_SYSTEM_PROCESSOR_LC MATCHES "x86_64" OR
+  CMAKE_SYSTEM_PROCESSOR_LC MATCHES "amd64" OR
+  CMAKE_SYSTEM_PROCESSOR_LC MATCHES "i[0-9]86" OR
```

Review comment:
unnecessary whitespace change
**File:** CMakeLists.txt
**Change Type:** modified
**Context:** PR #22372: [GSoC 2022] nasm/simd support for libjpeg-turbo

**Code Changes:**
```diff
@@ -1341,6 +1341,16 @@ if(WITH_JPEG OR HAVE_JPEG)
     status("    JPEG:" NO)
   elseif(BUILD_JPEG)
     status("    JPEG:" "build-${JPEG_LIBRARY} (ver ${JPEG_LIB_VERSION})")
+    if(ENABLE_LIBJPEG_TURBO_SIMD)
+      status("        SIMD Support Request:" "YES")
+      if(HAVE_LIBJPEG_TURBO_SIMD)
+        status("        SIMD Support:" "YES")
+      else()
+        status("        SIMD Support:" "NO")
```

Review comment:
Please move this flag into `3rdparty/libjpeg-turbo` `WITH_` prefix should be replaced to `ENABLE_` (`WITH_` is used in OpenCV for "auto-detection" of external components/dependencies/libraries)
**File:** CMakeLists.txt
**Change Type:** modified
**Context:** PR #22372: [GSoC 2022] nasm/simd support for libjpeg-turbo

**Code Changes:**
```diff
@@ -1341,6 +1341,16 @@ if(WITH_JPEG OR HAVE_JPEG)
     status("    JPEG:" NO)
   elseif(BUILD_JPEG)
     status("    JPEG:" "build-${JPEG_LIBRARY} (ver ${JPEG_LIB_VERSION})")
+    if(ENABLE_LIBJPEG_TURBO_SIMD)
+      status("        SIMD Support Request:" "YES")
+      if(HAVE_LIBJPEG_TURBO_SIMD)
+        status("        SIMD Support:" "YES")
+      else()
+        status("        SIMD Support:" "NO")
```

Review comment:
We should distinguish a request from the user and the actual "final" value (which depends on the platform (e.g. MSA is not handled yet), compiler, etc) Also there is still "libjpeg" (without "-turbo")
**File:** modules/imgcodecs/perf/perf_jpeg.cpp
**Change Type:** added
**Context:** PR #22372: [GSoC 2022] nasm/simd support for libjpeg-turbo

**Code Changes:**
```diff
@@ -0,0 +1,38 @@
+// This file is part of OpenCV project.
+// It is subject to the license terms in the LICENSE file found in the top-level directory
+// of this distribution and at http://opencv.org/license.html
+#include "perf_precomp.hpp"
+
+namespace opencv_test
+{
+using namespace perf;
+
```

Review comment:
Please avoid indentation in namespaces.
**File:** modules/imgcodecs/perf/perf_jpeg.cpp
**Change Type:** added
**Context:** PR #22372: [GSoC 2022] nasm/simd support for libjpeg-turbo

**Code Changes:**
```diff
@@ -0,0 +1,38 @@
+// This file is part of OpenCV project.
+// It is subject to the license terms in the LICENSE file found in the top-level directory
+// of this distribution and at http://opencv.org/license.html
+#include "perf_precomp.hpp"
+
+namespace opencv_test
+{
+using namespace perf;
+
```

Review comment:
> std::string What is that?
**File:** 3rdparty/libjpeg-turbo/CMakeLists.txt
**Change Type:** modified
**Context:** PR #22372: [GSoC 2022] nasm/simd support for libjpeg-turbo
**Review Line:** 149

**Code Changes:**
```diff
   set(JPEG_SOURCES ${JPEG_SOURCES} jdarith.c)
 endif()
 
-# No SIMD
-set(JPEG_SOURCES ${JPEG_SOURCES} jsimd_none.c)
+if(MSVC)
+  option(WITH_CRT_DLL
+    "Link all ${CMAKE_PROJECT_NAME} libraries and executables with the C run-time DLL (msvcr*.dll) instead of the static C run-time library (libcmt*.lib.)  The default is to use the C run-time DLL only with the libraries and executables that need it."
+    FALSE)
+  if(NOT WITH_CRT_DLL)
+    # Use the static C library for all build types
```

Review comment:
I borrowed these lines from the upstream. Same as `MSVC_IDE` parts.
**File:** CMakeLists.txt
**Change Type:** modified
**Context:** PR #22372: [GSoC 2022] nasm/simd support for libjpeg-turbo

**Code Changes:**
```diff
@@ -1341,6 +1341,16 @@ if(WITH_JPEG OR HAVE_JPEG)
     status("    JPEG:" NO)
   elseif(BUILD_JPEG)
     status("    JPEG:" "build-${JPEG_LIBRARY} (ver ${JPEG_LIB_VERSION})")
+    if(ENABLE_LIBJPEG_TURBO_SIMD)
+      status("        SIMD Support Request:" "YES")
+      if(HAVE_LIBJPEG_TURBO_SIMD)
+        status("        SIMD Support:" "YES")
+      else()
+        status("        SIMD Support:" "NO")
```

Review comment:
"libjpeg" without -turbo will report `"SIMD Support: "NO"` by default. And i think this is not a false promise. I don`t think that i should worry about that. And libjpeg is deprecated according to [docs](https://github.com/opencv/opencv/tree/4.x/3rdparty) at 4.x.
**File:** CMakeLists.txt
**Change Type:** modified
**Context:** PR #22372: [GSoC 2022] nasm/simd support for libjpeg-turbo

**Code Changes:**
```diff
@@ -1341,6 +1341,16 @@ if(WITH_JPEG OR HAVE_JPEG)
     status("    JPEG:" NO)
   elseif(BUILD_JPEG)
     status("    JPEG:" "build-${JPEG_LIBRARY} (ver ${JPEG_LIB_VERSION})")
+    if(ENABLE_LIBJPEG_TURBO_SIMD)
+      status("        SIMD Support Request:" "YES")
+      if(HAVE_LIBJPEG_TURBO_SIMD)
+        status("        SIMD Support:" "YES")
+      else()
+        status("        SIMD Support:" "NO")
```

Review comment:
`HAVE_LIBJPEG_TURBO_SIMD` sounds more consistent
**File:** 3rdparty/libjpeg-turbo/CMakeLists.txt
**Change Type:** modified
**Context:** PR #22372: [GSoC 2022] nasm/simd support for libjpeg-turbo

**Code Changes:**
```diff
@@ -15,8 +15,55 @@ endif()
 
 message(STATUS "libjpeg-turbo: VERSION = ${VERSION}, BUILD = ${BUILD}")
 
+math(EXPR BITS "${CMAKE_SIZEOF_VOID_P} * 8")
+string(TOLOWER ${CMAKE_SYSTEM_PROCESSOR} CMAKE_SYSTEM_PROCESSOR_LC)
+
+if(CMAKE_SYSTEM_PROCESSOR_LC MATCHES "x86_64" OR
+  CMAKE_SYSTEM_PROCESSOR_LC MATCHES "amd64" OR
+  CMAKE_SYSTEM_PROCESSOR_LC MATCHES "i[0-9]86" OR
```

Review comment:
Please remove debug print
**File:** 3rdparty/libjpeg-turbo/CMakeLists.txt
**Change Type:** modified
**Context:** PR #22372: [GSoC 2022] nasm/simd support for libjpeg-turbo
**Review Line:** 198

**Code Changes:**
```diff
+else()
+  set(WITH_SIMD 0)
+endif()
+
+if(WITH_SIMD)
+  message(STATUS "SIMD extensions: ${CPU_TYPE} (WITH_SIMD = ${WITH_SIMD})")
+  set(HAVE_LIBJPEG_TURBO_SIMD 1 PARENT_SCOPE)
+  if(MSVC_IDE OR XCODE)
+    set_source_files_properties(${SIMD_OBJS} PROPERTIES GENERATED 1)
+  endif()
+else()
```

Review comment:
I think this message is redundant. SIMD status is reported form `simd/CmakeLists.txt`. In particular on armv6 I see: ``` SIMD extensions: arm (WITH_SIMD = 1) ```
**File:** 3rdparty/libjpeg-turbo/CMakeLists.txt
**Change Type:** modified
**Context:** PR #22372: [GSoC 2022] nasm/simd support for libjpeg-turbo
**Review Line:** 198

**Code Changes:**
```diff
+else()
+  set(WITH_SIMD 0)
+endif()
+
+if(WITH_SIMD)
+  message(STATUS "SIMD extensions: ${CPU_TYPE} (WITH_SIMD = ${WITH_SIMD})")
+  set(HAVE_LIBJPEG_TURBO_SIMD 1 PARENT_SCOPE)
+  if(MSVC_IDE OR XCODE)
+    set_source_files_properties(${SIMD_OBJS} PROPERTIES GENERATED 1)
+  endif()
+else()
```

Review comment:
Actually this line reports this. There is no message reported on `simd/CMakeLists.txt` except failures and it reports NEON or partial NEON support. I believe it is good have for other type CPU's.
**File:** modules/videoio/src/cap_android_mediandk.cpp
**Change Type:** modified
**Context:** PR #22503: Add H264 / H265 writter support for Android

**Code Changes:**
```diff
@@ -13,14 +13,17 @@
 #include <sys/types.h>
 #include <fcntl.h>
 #include <android/log.h>
-
+#include <android/native_window.h>
 #include "media/NdkMediaCodec.h"
+#include "media/NdkMediaMuxer.h"
 #include "media/NdkMediaExtractor.h"
+#include "media/NdkMediaFormat.h"
```

Review comment:
> `error: use of undeclared identifier 'AMediaCodec_signalEndOfInputStream'`` Do you know which minimal NDK version is required for this feature?
**File:** modules/videoio/src/cap_android_mediandk.cpp
**Change Type:** modified
**Context:** PR #22503: Add H264 / H265 writter support for Android

**Code Changes:**
```diff
@@ -13,14 +13,17 @@
 #include <sys/types.h>
 #include <fcntl.h>
 #include <android/log.h>
-
+#include <android/native_window.h>
 #include "media/NdkMediaCodec.h"
+#include "media/NdkMediaMuxer.h"
 #include "media/NdkMediaExtractor.h"
+#include "media/NdkMediaFormat.h"
```

Review comment:
Good point. I seems to be 26: https://developer.android.com/ndk/reference/group/media#amediacodec_signalendofinputstream I'll try to avoid it to make it compile with 21.
**File:** modules/videoio/src/cap_android_mediandk.cpp
**Change Type:** modified
**Context:** PR #22503: Add H264 / H265 writter support for Android

**Code Changes:**
```diff
@@ -13,14 +13,17 @@
 #include <sys/types.h>
 #include <fcntl.h>
 #include <android/log.h>
-
+#include <android/native_window.h>
 #include "media/NdkMediaCodec.h"
+#include "media/NdkMediaMuxer.h"
 #include "media/NdkMediaExtractor.h"
+#include "media/NdkMediaFormat.h"
```

Review comment:
I added a special implementation for NDK < 26 (actually I think there should be minimum 21). I think it's slightly slower because there is not conversion RGB => YUV NV12 in opencv. Also it is limited to "aligned" resolutions (on my device it must be aligned to 4, but it may depends on vendor / hardware).
**File:** modules/videoio/src/cap_android_mediandk.cpp
**Change Type:** modified
**Context:** PR #22503: Add H264 / H265 writter support for Android

**Code Changes:**
```diff
@@ -13,14 +13,17 @@
 #include <sys/types.h>
 #include <fcntl.h>
 #include <android/log.h>
-
+#include <android/native_window.h>
 #include "media/NdkMediaCodec.h"
+#include "media/NdkMediaMuxer.h"
 #include "media/NdkMediaExtractor.h"
+#include "media/NdkMediaFormat.h"
```

Review comment:
Fixed tailing spaces.
**File:** modules/videoio/src/cap_android_mediandk.cpp
**Change Type:** modified
**Context:** PR #22503: Add H264 / H265 writter support for Android

**Code Changes:**
```diff
@@ -13,14 +13,17 @@
 #include <sys/types.h>
 #include <fcntl.h>
 #include <android/log.h>
-
+#include <android/native_window.h>
 #include "media/NdkMediaCodec.h"
+#include "media/NdkMediaMuxer.h"
 #include "media/NdkMediaExtractor.h"
+#include "media/NdkMediaFormat.h"
```

Review comment:
Where do the constants come from?
**File:** modules/videoio/src/cap_android_mediandk.cpp
**Change Type:** modified
**Context:** PR #22503: Add H264 / H265 writter support for Android
**Review Line:** 507

**Code Changes:**
```diff
+            if (AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM == buffer.format) {
+                Mat bufferMat(image.rows, image.cols, CV_8UC4, buffer.bits, buffer.stride * 4);
+                cvtColor(image, bufferMat, CV_BGR2RGBA);
+            } else {
+                LOGE("Unknow surface buffer format: %u", buffer.format);
+            }
+
+            ANativeWindow_unlockAndPost(surface);
+        }
+        #else
+        LOGV("[write] image: %d  x %d", image.cols, image.rows);
```

Review comment:
I propose to add warning in "else" case.
**File:** modules/videoio/src/cap_android_mediandk.cpp
**Change Type:** modified
**Context:** PR #22503: Add H264 / H265 writter support for Android

**Code Changes:**
```diff
@@ -13,14 +13,17 @@
 #include <sys/types.h>
 #include <fcntl.h>
 #include <android/log.h>
-
+#include <android/native_window.h>
 #include "media/NdkMediaCodec.h"
+#include "media/NdkMediaMuxer.h"
 #include "media/NdkMediaExtractor.h"
+#include "media/NdkMediaFormat.h"
```

Review comment:
Please add logcat warning for the case.
**File:** modules/videoio/src/cap_android_mediandk.cpp
**Change Type:** modified
**Context:** PR #22503: Add H264 / H265 writter support for Android

**Code Changes:**
```diff
@@ -13,14 +13,17 @@
 #include <sys/types.h>
 #include <fcntl.h>
 #include <android/log.h>
-
+#include <android/native_window.h>
 #include "media/NdkMediaCodec.h"
+#include "media/NdkMediaMuxer.h"
 #include "media/NdkMediaExtractor.h"
+#include "media/NdkMediaFormat.h"
```

Review comment:
You can wrap ANdroid buffer to `cv::Mat` with stride. In this case there is no need to do color conversion per-row. cvtColor respects strides. In case if buffer is larger than required frame in height, you can use its ROI.
**File:** modules/videoio/src/cap_android_mediandk.cpp
**Change Type:** modified
**Context:** PR #22503: Add H264 / H265 writter support for Android

**Code Changes:**
```diff
@@ -13,14 +13,17 @@
 #include <sys/types.h>
 #include <fcntl.h>
 #include <android/log.h>
-
+#include <android/native_window.h>
 #include "media/NdkMediaCodec.h"
+#include "media/NdkMediaMuxer.h"
 #include "media/NdkMediaExtractor.h"
+#include "media/NdkMediaFormat.h"
```

Review comment:
`cv::merge` does it for you: https://docs.opencv.org/4.x/d2/de8/group__core__array.html#ga7d7b4d6c6ee504b30a20b1680029c7b4
**File:** modules/videoio/src/cap_android_mediandk.cpp
**Change Type:** modified
**Context:** PR #22503: Add H264 / H265 writter support for Android
**Review Line:** 635

**Code Changes:**
```diff
+
+        #if __ANDROID_API__ >= 26
+        if (NULL != surface) ANativeWindow_release(surface);
+        #endif
+
+        if (fd >= 0) ::close(fd);
+        if (NULL != format) AMediaFormat_delete(format);
+
+        format = NULL;
+        encoder = NULL;
+        muxer = NULL;
```

Review comment:
fd == 0 is not valid descriptor.
**File:** modules/videoio/src/cap_android_mediandk.cpp
**Change Type:** modified
**Context:** PR #22503: Add H264 / H265 writter support for Android

**Code Changes:**
```diff
@@ -13,14 +13,17 @@
 #include <sys/types.h>
 #include <fcntl.h>
 #include <android/log.h>
-
+#include <android/native_window.h>
 #include "media/NdkMediaCodec.h"
+#include "media/NdkMediaMuxer.h"
 #include "media/NdkMediaExtractor.h"
+#include "media/NdkMediaFormat.h"
```

Review comment:
I propose to move "then" section to the next line with indentation everywhere. It makes code more readable.