Browse Source

Code style

qr-code
Martijn Braam 7 months ago
parent
commit
72105c9491
  1. 548
      .clang-format
  2. 1930
      camera.c
  3. 72
      camera.h
  4. 456
      camera_config.c
  5. 60
      camera_config.h
  6. 590
      device.c
  7. 39
      device.h
  8. 223
      ini.c
  9. 14
      ini.h
  10. 789
      io_pipeline.c
  11. 16
      io_pipeline.h
  12. 224
      main.c
  13. 21
      main.h
  14. 13
      matrix.c
  15. 142
      pipeline.c
  16. 7
      pipeline.h
  17. 612
      process_pipeline.c
  18. 24
      process_pipeline.h
  19. 288
      quickpreview.c
  20. 33
      quickpreview.h

548
.clang-format

@ -0,0 +1,548 @@
# SPDX-License-Identifier: GPL-2.0
#
# clang-format configuration file. Intended for clang-format >= 4.
#
# For more information, see:
#
# Documentation/process/clang-format.rst
# https://clang.llvm.org/docs/ClangFormat.html
# https://clang.llvm.org/docs/ClangFormatStyleOptions.html
#
---
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
#AlignEscapedNewlines: Left # Unknown to clang-format-4.0
AlignOperands: true
AlignTrailingComments: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: All
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
#AfterExternBlock: false # Unknown to clang-format-5.0
BeforeCatch: false
BeforeElse: false
IndentBraces: false
#SplitEmptyFunction: true # Unknown to clang-format-4.0
#SplitEmptyRecord: true # Unknown to clang-format-4.0
#SplitEmptyNamespace: true # Unknown to clang-format-4.0
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Custom
#BreakBeforeInheritanceComma: false # Unknown to clang-format-4.0
BreakBeforeTernaryOperators: false
BreakConstructorInitializersBeforeComma: false
#BreakConstructorInitializers: BeforeComma # Unknown to clang-format-4.0
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: false
ColumnLimit: 85
CommentPragmas: '^ IWYU pragma:'
#CompactNamespaces: false # Unknown to clang-format-4.0
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 8
ContinuationIndentWidth: 8
Cpp11BracedListStyle: false
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
#FixNamespaceComments: false # Unknown to clang-format-4.0
# Taken from:
# git grep -h '^#define [^[:space:]]*for_each[^[:space:]]*(' include/ \
# | sed "s,^#define \([^[:space:]]*for_each[^[:space:]]*\)(.*$, - '\1'," \
# | sort | uniq
ForEachMacros:
- 'apei_estatus_for_each_section'
- 'ata_for_each_dev'
- 'ata_for_each_link'
- '__ata_qc_for_each'
- 'ata_qc_for_each'
- 'ata_qc_for_each_raw'
- 'ata_qc_for_each_with_internal'
- 'ax25_for_each'
- 'ax25_uid_for_each'
- '__bio_for_each_bvec'
- 'bio_for_each_bvec'
- 'bio_for_each_bvec_all'
- 'bio_for_each_integrity_vec'
- '__bio_for_each_segment'
- 'bio_for_each_segment'
- 'bio_for_each_segment_all'
- 'bio_list_for_each'
- 'bip_for_each_vec'
- 'bitmap_for_each_clear_region'
- 'bitmap_for_each_set_region'
- 'blkg_for_each_descendant_post'
- 'blkg_for_each_descendant_pre'
- 'blk_queue_for_each_rl'
- 'bond_for_each_slave'
- 'bond_for_each_slave_rcu'
- 'bpf_for_each_spilled_reg'
- 'btree_for_each_safe128'
- 'btree_for_each_safe32'
- 'btree_for_each_safe64'
- 'btree_for_each_safel'
- 'card_for_each_dev'
- 'cgroup_taskset_for_each'
- 'cgroup_taskset_for_each_leader'
- 'cpufreq_for_each_entry'
- 'cpufreq_for_each_entry_idx'
- 'cpufreq_for_each_valid_entry'
- 'cpufreq_for_each_valid_entry_idx'
- 'css_for_each_child'
- 'css_for_each_descendant_post'
- 'css_for_each_descendant_pre'
- 'device_for_each_child_node'
- 'dma_fence_chain_for_each'
- 'do_for_each_ftrace_op'
- 'drm_atomic_crtc_for_each_plane'
- 'drm_atomic_crtc_state_for_each_plane'
- 'drm_atomic_crtc_state_for_each_plane_state'
- 'drm_atomic_for_each_plane_damage'
- 'drm_client_for_each_connector_iter'
- 'drm_client_for_each_modeset'
- 'drm_connector_for_each_possible_encoder'
- 'drm_for_each_bridge_in_chain'
- 'drm_for_each_connector_iter'
- 'drm_for_each_crtc'
- 'drm_for_each_encoder'
- 'drm_for_each_encoder_mask'
- 'drm_for_each_fb'
- 'drm_for_each_legacy_plane'
- 'drm_for_each_plane'
- 'drm_for_each_plane_mask'
- 'drm_for_each_privobj'
- 'drm_mm_for_each_hole'
- 'drm_mm_for_each_node'
- 'drm_mm_for_each_node_in_range'
- 'drm_mm_for_each_node_safe'
- 'flow_action_for_each'
- 'for_each_active_dev_scope'
- 'for_each_active_drhd_unit'
- 'for_each_active_iommu'
- 'for_each_aggr_pgid'
- 'for_each_available_child_of_node'
- 'for_each_bio'
- 'for_each_board_func_rsrc'
- 'for_each_bvec'
- 'for_each_card_auxs'
- 'for_each_card_auxs_safe'
- 'for_each_card_components'
- 'for_each_card_dapms'
- 'for_each_card_pre_auxs'
- 'for_each_card_prelinks'
- 'for_each_card_rtds'
- 'for_each_card_rtds_safe'
- 'for_each_card_widgets'
- 'for_each_card_widgets_safe'
- 'for_each_cgroup_storage_type'
- 'for_each_child_of_node'
- 'for_each_clear_bit'
- 'for_each_clear_bit_from'
- 'for_each_cmsghdr'
- 'for_each_compatible_node'
- 'for_each_component_dais'
- 'for_each_component_dais_safe'
- 'for_each_comp_order'
- 'for_each_console'
- 'for_each_cpu'
- 'for_each_cpu_and'
- 'for_each_cpu_not'
- 'for_each_cpu_wrap'
- 'for_each_dapm_widgets'
- 'for_each_dev_addr'
- 'for_each_dev_scope'
- 'for_each_displayid_db'
- 'for_each_dma_cap_mask'
- 'for_each_dpcm_be'
- 'for_each_dpcm_be_rollback'
- 'for_each_dpcm_be_safe'
- 'for_each_dpcm_fe'
- 'for_each_drhd_unit'
- 'for_each_dss_dev'
- 'for_each_efi_memory_desc'
- 'for_each_efi_memory_desc_in_map'
- 'for_each_element'
- 'for_each_element_extid'
- 'for_each_element_id'
- 'for_each_endpoint_of_node'
- 'for_each_evictable_lru'
- 'for_each_fib6_node_rt_rcu'
- 'for_each_fib6_walker_rt'
- 'for_each_free_mem_pfn_range_in_zone'
- 'for_each_free_mem_pfn_range_in_zone_from'
- 'for_each_free_mem_range'
- 'for_each_free_mem_range_reverse'
- 'for_each_func_rsrc'
- 'for_each_hstate'
- 'for_each_if'
- 'for_each_iommu'
- 'for_each_ip_tunnel_rcu'
- 'for_each_irq_nr'
- 'for_each_link_codecs'
- 'for_each_link_cpus'
- 'for_each_link_platforms'
- 'for_each_lru'
- 'for_each_matching_node'
- 'for_each_matching_node_and_match'
- 'for_each_member'
- 'for_each_mem_region'
- 'for_each_memblock_type'
- 'for_each_memcg_cache_index'
- 'for_each_mem_pfn_range'
- '__for_each_mem_range'
- 'for_each_mem_range'
- '__for_each_mem_range_rev'
- 'for_each_mem_range_rev'
- 'for_each_migratetype_order'
- 'for_each_msi_entry'
- 'for_each_msi_entry_safe'
- 'for_each_net'
- 'for_each_net_continue_reverse'
- 'for_each_netdev'
- 'for_each_netdev_continue'
- 'for_each_netdev_continue_rcu'
- 'for_each_netdev_continue_reverse'
- 'for_each_netdev_feature'
- 'for_each_netdev_in_bond_rcu'
- 'for_each_netdev_rcu'
- 'for_each_netdev_reverse'
- 'for_each_netdev_safe'
- 'for_each_net_rcu'
- 'for_each_new_connector_in_state'
- 'for_each_new_crtc_in_state'
- 'for_each_new_mst_mgr_in_state'
- 'for_each_new_plane_in_state'
- 'for_each_new_private_obj_in_state'
- 'for_each_node'
- 'for_each_node_by_name'
- 'for_each_node_by_type'
- 'for_each_node_mask'
- 'for_each_node_state'
- 'for_each_node_with_cpus'
- 'for_each_node_with_property'
- 'for_each_nonreserved_multicast_dest_pgid'
- 'for_each_of_allnodes'
- 'for_each_of_allnodes_from'
- 'for_each_of_cpu_node'
- 'for_each_of_pci_range'
- 'for_each_old_connector_in_state'
- 'for_each_old_crtc_in_state'
- 'for_each_old_mst_mgr_in_state'
- 'for_each_oldnew_connector_in_state'
- 'for_each_oldnew_crtc_in_state'
- 'for_each_oldnew_mst_mgr_in_state'
- 'for_each_oldnew_plane_in_state'
- 'for_each_oldnew_plane_in_state_reverse'
- 'for_each_oldnew_private_obj_in_state'
- 'for_each_old_plane_in_state'
- 'for_each_old_private_obj_in_state'
- 'for_each_online_cpu'
- 'for_each_online_node'
- 'for_each_online_pgdat'
- 'for_each_pci_bridge'
- 'for_each_pci_dev'
- 'for_each_pci_msi_entry'
- 'for_each_pcm_streams'
- 'for_each_physmem_range'
- 'for_each_populated_zone'
- 'for_each_possible_cpu'
- 'for_each_present_cpu'
- 'for_each_prime_number'
- 'for_each_prime_number_from'
- 'for_each_process'
- 'for_each_process_thread'
- 'for_each_property_of_node'
- 'for_each_registered_fb'
- 'for_each_requested_gpio'
- 'for_each_requested_gpio_in_range'
- 'for_each_reserved_mem_range'
- 'for_each_reserved_mem_region'
- 'for_each_rtd_codec_dais'
- 'for_each_rtd_codec_dais_rollback'
- 'for_each_rtd_components'
- 'for_each_rtd_cpu_dais'
- 'for_each_rtd_cpu_dais_rollback'
- 'for_each_rtd_dais'
- 'for_each_set_bit'
- 'for_each_set_bit_from'
- 'for_each_set_clump8'
- 'for_each_sg'
- 'for_each_sg_dma_page'
- 'for_each_sg_page'
- 'for_each_sgtable_dma_page'
- 'for_each_sgtable_dma_sg'
- 'for_each_sgtable_page'
- 'for_each_sgtable_sg'
- 'for_each_sibling_event'
- 'for_each_subelement'
- 'for_each_subelement_extid'
- 'for_each_subelement_id'
- '__for_each_thread'
- 'for_each_thread'
- 'for_each_unicast_dest_pgid'
- 'for_each_wakeup_source'
- 'for_each_zone'
- 'for_each_zone_zonelist'
- 'for_each_zone_zonelist_nodemask'
- 'fwnode_for_each_available_child_node'
- 'fwnode_for_each_child_node'
- 'fwnode_graph_for_each_endpoint'
- 'gadget_for_each_ep'
- 'genradix_for_each'
- 'genradix_for_each_from'
- 'hash_for_each'
- 'hash_for_each_possible'
- 'hash_for_each_possible_rcu'
- 'hash_for_each_possible_rcu_notrace'
- 'hash_for_each_possible_safe'
- 'hash_for_each_rcu'
- 'hash_for_each_safe'
- 'hctx_for_each_ctx'
- 'hlist_bl_for_each_entry'
- 'hlist_bl_for_each_entry_rcu'
- 'hlist_bl_for_each_entry_safe'
- 'hlist_for_each'
- 'hlist_for_each_entry'
- 'hlist_for_each_entry_continue'
- 'hlist_for_each_entry_continue_rcu'
- 'hlist_for_each_entry_continue_rcu_bh'
- 'hlist_for_each_entry_from'
- 'hlist_for_each_entry_from_rcu'
- 'hlist_for_each_entry_rcu'
- 'hlist_for_each_entry_rcu_bh'
- 'hlist_for_each_entry_rcu_notrace'
- 'hlist_for_each_entry_safe'
- '__hlist_for_each_rcu'
- 'hlist_for_each_safe'
- 'hlist_nulls_for_each_entry'
- 'hlist_nulls_for_each_entry_from'
- 'hlist_nulls_for_each_entry_rcu'
- 'hlist_nulls_for_each_entry_safe'
- 'i3c_bus_for_each_i2cdev'
- 'i3c_bus_for_each_i3cdev'
- 'ide_host_for_each_port'
- 'ide_port_for_each_dev'
- 'ide_port_for_each_present_dev'
- 'idr_for_each_entry'
- 'idr_for_each_entry_continue'
- 'idr_for_each_entry_continue_ul'
- 'idr_for_each_entry_ul'
- 'in_dev_for_each_ifa_rcu'
- 'in_dev_for_each_ifa_rtnl'
- 'inet_bind_bucket_for_each'
- 'inet_lhash2_for_each_icsk_rcu'
- 'key_for_each'
- 'key_for_each_safe'
- 'klp_for_each_func'
- 'klp_for_each_func_safe'
- 'klp_for_each_func_static'
- 'klp_for_each_object'
- 'klp_for_each_object_safe'
- 'klp_for_each_object_static'
- 'kunit_suite_for_each_test_case'
- 'kvm_for_each_memslot'
- 'kvm_for_each_vcpu'
- 'list_for_each'
- 'list_for_each_codec'
- 'list_for_each_codec_safe'
- 'list_for_each_continue'
- 'list_for_each_entry'
- 'list_for_each_entry_continue'
- 'list_for_each_entry_continue_rcu'
- 'list_for_each_entry_continue_reverse'
- 'list_for_each_entry_from'
- 'list_for_each_entry_from_rcu'
- 'list_for_each_entry_from_reverse'
- 'list_for_each_entry_lockless'
- 'list_for_each_entry_rcu'
- 'list_for_each_entry_reverse'
- 'list_for_each_entry_safe'
- 'list_for_each_entry_safe_continue'
- 'list_for_each_entry_safe_from'
- 'list_for_each_entry_safe_reverse'
- 'list_for_each_prev'
- 'list_for_each_prev_safe'
- 'list_for_each_safe'
- 'llist_for_each'
- 'llist_for_each_entry'
- 'llist_for_each_entry_safe'
- 'llist_for_each_safe'
- 'mci_for_each_dimm'
- 'media_device_for_each_entity'
- 'media_device_for_each_intf'
- 'media_device_for_each_link'
- 'media_device_for_each_pad'
- 'nanddev_io_for_each_page'
- 'netdev_for_each_lower_dev'
- 'netdev_for_each_lower_private'
- 'netdev_for_each_lower_private_rcu'
- 'netdev_for_each_mc_addr'
- 'netdev_for_each_uc_addr'
- 'netdev_for_each_upper_dev_rcu'
- 'netdev_hw_addr_list_for_each'
- 'nft_rule_for_each_expr'
- 'nla_for_each_attr'
- 'nla_for_each_nested'
- 'nlmsg_for_each_attr'
- 'nlmsg_for_each_msg'
- 'nr_neigh_for_each'
- 'nr_neigh_for_each_safe'
- 'nr_node_for_each'
- 'nr_node_for_each_safe'
- 'of_for_each_phandle'
- 'of_property_for_each_string'
- 'of_property_for_each_u32'
- 'pci_bus_for_each_resource'
- 'pcm_for_each_format'
- 'ping_portaddr_for_each_entry'
- 'plist_for_each'
- 'plist_for_each_continue'
- 'plist_for_each_entry'
- 'plist_for_each_entry_continue'
- 'plist_for_each_entry_safe'
- 'plist_for_each_safe'
- 'pnp_for_each_card'
- 'pnp_for_each_dev'
- 'protocol_for_each_card'
- 'protocol_for_each_dev'
- 'queue_for_each_hw_ctx'
- 'radix_tree_for_each_slot'
- 'radix_tree_for_each_tagged'
- 'rbtree_postorder_for_each_entry_safe'
- 'rdma_for_each_block'
- 'rdma_for_each_port'
- 'rdma_umem_for_each_dma_block'
- 'resource_list_for_each_entry'
- 'resource_list_for_each_entry_safe'
- 'rhl_for_each_entry_rcu'
- 'rhl_for_each_rcu'
- 'rht_for_each'
- 'rht_for_each_entry'
- 'rht_for_each_entry_from'
- 'rht_for_each_entry_rcu'
- 'rht_for_each_entry_rcu_from'
- 'rht_for_each_entry_safe'
- 'rht_for_each_from'
- 'rht_for_each_rcu'
- 'rht_for_each_rcu_from'
- '__rq_for_each_bio'
- 'rq_for_each_bvec'
- 'rq_for_each_segment'
- 'scsi_for_each_prot_sg'
- 'scsi_for_each_sg'
- 'sctp_for_each_hentry'
- 'sctp_skb_for_each'
- 'shdma_for_each_chan'
- '__shost_for_each_device'
- 'shost_for_each_device'
- 'sk_for_each'
- 'sk_for_each_bound'
- 'sk_for_each_entry_offset_rcu'
- 'sk_for_each_from'
- 'sk_for_each_rcu'
- 'sk_for_each_safe'
- 'sk_nulls_for_each'
- 'sk_nulls_for_each_from'
- 'sk_nulls_for_each_rcu'
- 'snd_array_for_each'
- 'snd_pcm_group_for_each_entry'
- 'snd_soc_dapm_widget_for_each_path'
- 'snd_soc_dapm_widget_for_each_path_safe'
- 'snd_soc_dapm_widget_for_each_sink_path'
- 'snd_soc_dapm_widget_for_each_source_path'
- 'tb_property_for_each'
- 'tcf_exts_for_each_action'
- 'udp_portaddr_for_each_entry'
- 'udp_portaddr_for_each_entry_rcu'
- 'usb_hub_for_each_child'
- 'v4l2_device_for_each_subdev'
- 'v4l2_m2m_for_each_dst_buf'
- 'v4l2_m2m_for_each_dst_buf_safe'
- 'v4l2_m2m_for_each_src_buf'
- 'v4l2_m2m_for_each_src_buf_safe'
- 'virtio_device_for_each_vq'
- 'while_for_each_ftrace_op'
- 'xa_for_each'
- 'xa_for_each_marked'
- 'xa_for_each_range'
- 'xa_for_each_start'
- 'xas_for_each'
- 'xas_for_each_conflict'
- 'xas_for_each_marked'
- 'xbc_array_for_each_value'
- 'xbc_for_each_key_value'
- 'xbc_node_for_each_array_value'
- 'xbc_node_for_each_child'
- 'xbc_node_for_each_key_value'
- 'zorro_for_each_dev'
#IncludeBlocks: Preserve # Unknown to clang-format-5.0
IncludeCategories:
- Regex: '.*'
Priority: 1
IncludeIsMainRegex: '(Test)?$'
IndentCaseLabels: false
#IndentPPDirectives: None # Unknown to clang-format-5.0
IndentWidth: 8
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
#ObjCBinPackProtocolList: Auto # Unknown to clang-format-5.0
ObjCBlockIndentWidth: 8
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
# Taken from git's rules
#PenaltyBreakAssignment: 10 # Unknown to clang-format-4.0
PenaltyBreakBeforeFirstCallParameter: 30
PenaltyBreakComment: 10
PenaltyBreakFirstLessLess: 0
PenaltyBreakString: 10
PenaltyExcessCharacter: 100
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
ReflowComments: false
SortIncludes: false
#SortUsingDeclarations: false # Unknown to clang-format-4.0
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
#SpaceBeforeCtorInitializerColon: true # Unknown to clang-format-5.0
#SpaceBeforeInheritanceColon: true # Unknown to clang-format-5.0
SpaceBeforeParens: ControlStatements
#SpaceBeforeRangeBasedForLoopColon: true # Unknown to clang-format-5.0
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: false
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp03
TabWidth: 8
UseTab: Always
...

1930
camera.c
File diff suppressed because it is too large
View File

72
camera.h

@ -5,19 +5,19 @@
#include <stdint.h>
typedef enum {
MP_PIXEL_FMT_UNSUPPORTED,
MP_PIXEL_FMT_BGGR8,
MP_PIXEL_FMT_GBRG8,
MP_PIXEL_FMT_GRBG8,
MP_PIXEL_FMT_RGGB8,
MP_PIXEL_FMT_BGGR10P,
MP_PIXEL_FMT_GBRG10P,
MP_PIXEL_FMT_GRBG10P,
MP_PIXEL_FMT_RGGB10P,
MP_PIXEL_FMT_UYVY,
MP_PIXEL_FMT_YUYV,
MP_PIXEL_FMT_MAX,
MP_PIXEL_FMT_UNSUPPORTED,
MP_PIXEL_FMT_BGGR8,
MP_PIXEL_FMT_GBRG8,
MP_PIXEL_FMT_GRBG8,
MP_PIXEL_FMT_RGGB8,
MP_PIXEL_FMT_BGGR10P,
MP_PIXEL_FMT_GBRG10P,
MP_PIXEL_FMT_GRBG10P,
MP_PIXEL_FMT_RGGB10P,
MP_PIXEL_FMT_UYVY,
MP_PIXEL_FMT_YUYV,
MP_PIXEL_FMT_MAX,
} MPPixelFormat;
const char *mp_pixel_format_to_str(MPPixelFormat pixel_format);
@ -31,23 +31,24 @@ uint32_t mp_pixel_format_to_v4l_bus_code(MPPixelFormat pixel_format);
uint32_t mp_pixel_format_bits_per_pixel(MPPixelFormat pixel_format);
uint32_t mp_pixel_format_width_to_bytes(MPPixelFormat pixel_format, uint32_t width);
uint32_t mp_pixel_format_width_to_colors(MPPixelFormat pixel_format, uint32_t width);
uint32_t mp_pixel_format_height_to_colors(MPPixelFormat pixel_format, uint32_t height);
uint32_t mp_pixel_format_height_to_colors(MPPixelFormat pixel_format,
uint32_t height);
typedef struct {
MPPixelFormat pixel_format;
MPPixelFormat pixel_format;
struct v4l2_fract frame_interval;
uint32_t width;
uint32_t height;
struct v4l2_fract frame_interval;
uint32_t width;
uint32_t height;
} MPCameraMode;
bool mp_camera_mode_is_equivalent(const MPCameraMode *m1, const MPCameraMode *m2);
typedef struct {
uint32_t pixel_format;
uint32_t width;
uint32_t height;
uint8_t *data;
uint32_t pixel_format;
uint32_t width;
uint32_t height;
uint8_t *data;
} MPImage;
typedef struct _MPCamera MPCamera;
@ -66,7 +67,8 @@ bool mp_camera_set_mode(MPCamera *camera, MPCameraMode *mode);
bool mp_camera_start_capture(MPCamera *camera);
bool mp_camera_stop_capture(MPCamera *camera);
bool mp_camera_is_capturing(MPCamera *camera);
bool mp_camera_capture_image(MPCamera *camera, void (*callback)(MPImage, void *), void *user_data);
bool mp_camera_capture_image(MPCamera *camera, void (*callback)(MPImage, void *),
void *user_data);
typedef struct _MPCameraModeList MPCameraModeList;
@ -77,21 +79,21 @@ MPCameraModeList *mp_camera_mode_list_next(MPCameraModeList *list);
void mp_camera_mode_list_free(MPCameraModeList *list);
typedef struct {
uint32_t id;
uint32_t type;
char name[32];
uint32_t id;
uint32_t type;
char name[32];
int32_t min;
int32_t max;
int32_t step;
int32_t default_value;
int32_t min;
int32_t max;
int32_t step;
int32_t default_value;
uint32_t flags;
uint32_t flags;
uint32_t element_size;
uint32_t element_count;
uint32_t dimensions_count;
uint32_t dimensions[V4L2_CTRL_MAX_DIMS];
uint32_t element_size;
uint32_t element_count;
uint32_t dimensions_count;
uint32_t dimensions[V4L2_CTRL_MAX_DIMS];
} MPControl;
const char *mp_control_id_to_str(uint32_t id);

456
camera_config.c

@ -20,263 +20,269 @@ static char *exif_model;
static bool
find_config(char *conffile)
{
char buf[512];
char *xdg_config_home;
wordexp_t exp_result;
FILE *fp;
// Resolve XDG stuff
if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL) {
xdg_config_home = "~/.config";
}
wordexp(xdg_config_home, &exp_result, 0);
xdg_config_home = strdup(exp_result.we_wordv[0]);
wordfree(&exp_result);
if (access("/proc/device-tree/compatible", F_OK) != -1) {
// Reads to compatible string of the current device tree, looks like:
// pine64,pinephone-1.2\0allwinner,sun50i-a64\0
fp = fopen("/proc/device-tree/compatible", "r");
fgets(buf, 512, fp);
fclose(fp);
// Check config/%dt.ini in the current working directory
sprintf(conffile, "config/%s.ini", buf);
if(access(conffile, F_OK) != -1) {
printf("Found config file at %s\n", conffile);
return true;
}
// Check for a config file in XDG_CONFIG_HOME
sprintf(conffile, "%s/megapixels/config/%s.ini", xdg_config_home, buf);
if(access(conffile, F_OK) != -1) {
printf("Found config file at %s\n", conffile);
return true;
}
// Check user overridden /etc/megapixels/config/$dt.ini
sprintf(conffile, "%s/megapixels/config/%s.ini", SYSCONFDIR, buf);
if(access(conffile, F_OK) != -1) {
printf("Found config file at %s\n", conffile);
return true;
}
// Check packaged /usr/share/megapixels/config/$dt.ini
sprintf(conffile, "%s/megapixels/config/%s.ini", DATADIR, buf);
if(access(conffile, F_OK) != -1) {
printf("Found config file at %s\n", conffile);
return true;
}
printf("%s not found\n", conffile);
} else {
printf("Could not read device name from device tree\n");
}
// If all else fails, fall back to /etc/megapixels.ini
sprintf(conffile, "/etc/megapixels.ini");
if (access(conffile, F_OK) != -1) {
printf("Found config file at %s\n", conffile);
return true;
}
return false;
char buf[512];
char *xdg_config_home;
wordexp_t exp_result;
FILE *fp;
// Resolve XDG stuff
if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL) {
xdg_config_home = "~/.config";
}
wordexp(xdg_config_home, &exp_result, 0);
xdg_config_home = strdup(exp_result.we_wordv[0]);
wordfree(&exp_result);
if (access("/proc/device-tree/compatible", F_OK) != -1) {
// Reads to compatible string of the current device tree, looks like:
// pine64,pinephone-1.2\0allwinner,sun50i-a64\0
fp = fopen("/proc/device-tree/compatible", "r");
fgets(buf, 512, fp);
fclose(fp);
// Check config/%dt.ini in the current working directory
sprintf(conffile, "config/%s.ini", buf);
if (access(conffile, F_OK) != -1) {
printf("Found config file at %s\n", conffile);
return true;
}
// Check for a config file in XDG_CONFIG_HOME
sprintf(conffile, "%s/megapixels/config/%s.ini", xdg_config_home,
buf);
if (access(conffile, F_OK) != -1) {
printf("Found config file at %s\n", conffile);
return true;
}
// Check user overridden /etc/megapixels/config/$dt.ini
sprintf(conffile, "%s/megapixels/config/%s.ini", SYSCONFDIR, buf);
if (access(conffile, F_OK) != -1) {
printf("Found config file at %s\n", conffile);
return true;
}
// Check packaged /usr/share/megapixels/config/$dt.ini
sprintf(conffile, "%s/megapixels/config/%s.ini", DATADIR, buf);
if (access(conffile, F_OK) != -1) {
printf("Found config file at %s\n", conffile);
return true;
}
printf("%s not found\n", conffile);
} else {
printf("Could not read device name from device tree\n");
}
// If all else fails, fall back to /etc/megapixels.ini
sprintf(conffile, "/etc/megapixels.ini");
if (access(conffile, F_OK) != -1) {
printf("Found config file at %s\n", conffile);
return true;
}
return false;
}
static int
strtoint(const char *nptr, char **endptr, int base)
{
long x = strtol(nptr, endptr, base);
assert(x <= INT_MAX);
return (int) x;
long x = strtol(nptr, endptr, base);
assert(x <= INT_MAX);
return (int)x;
}
static bool
config_handle_camera_mode(const char *prefix, MPCameraMode * mode, const char *name, const char *value)
config_handle_camera_mode(const char *prefix, MPCameraMode *mode, const char *name,
const char *value)
{
int prefix_length = strlen(prefix);
if (strncmp(prefix, name, prefix_length) != 0)
return false;
name += prefix_length;
if (strcmp(name, "width") == 0) {
mode->width = strtoint(value, NULL, 10);
} else if (strcmp(name, "height") == 0) {
mode->height = strtoint(value, NULL, 10);
} else if (strcmp(name, "rate") == 0) {
mode->frame_interval.numerator = 1;
mode->frame_interval.denominator = strtoint(value, NULL, 10);
} else if (strcmp(name, "fmt") == 0) {
mode->pixel_format = mp_pixel_format_from_str(value);
if (mode->pixel_format == MP_PIXEL_FMT_UNSUPPORTED) {
g_printerr("Unsupported pixelformat %s\n", value);
exit(1);
}
} else {
return false;
}
return true;
int prefix_length = strlen(prefix);
if (strncmp(prefix, name, prefix_length) != 0)
return false;
name += prefix_length;
if (strcmp(name, "width") == 0) {
mode->width = strtoint(value, NULL, 10);
} else if (strcmp(name, "height") == 0) {
mode->height = strtoint(value, NULL, 10);
} else if (strcmp(name, "rate") == 0) {
mode->frame_interval.numerator = 1;
mode->frame_interval.denominator = strtoint(value, NULL, 10);
} else if (strcmp(name, "fmt") == 0) {
mode->pixel_format = mp_pixel_format_from_str(value);
if (mode->pixel_format == MP_PIXEL_FMT_UNSUPPORTED) {
g_printerr("Unsupported pixelformat %s\n", value);
exit(1);
}
} else {
return false;
}
return true;
}
static int
config_ini_handler(void *user, const char *section, const char *name,
const char *value)
const char *value)
{
if (strcmp(section, "device") == 0) {
if (strcmp(name, "make") == 0) {
exif_make = strdup(value);
} else if (strcmp(name, "model") == 0) {
exif_model = strdup(value);
} else {
g_printerr("Unknown key '%s' in [device]\n", name);
exit(1);
}
} else {
if (num_cameras == MP_MAX_CAMERAS) {
g_printerr("More cameras defined than NUM_CAMERAS\n");
exit(1);
}
size_t index = 0;
for (; index < num_cameras; ++index) {
if (strcmp(cameras[index].cfg_name, section) == 0) {
break;
}
}
if (index == num_cameras) {
printf("Adding camera %s from config\n", section);
++num_cameras;
cameras[index].index = index;
strcpy(cameras[index].cfg_name, section);
}
struct mp_camera_config *cc = &cameras[index];
if (config_handle_camera_mode("capture-", &cc->capture_mode, name, value)) {
} else if (config_handle_camera_mode("preview-", &cc->preview_mode, name, value)) {
} else if (strcmp(name, "rotate") == 0) {
cc->rotate = strtoint(value, NULL, 10);
} else if (strcmp(name, "mirrored") == 0) {
cc->mirrored = strcmp(value, "true") == 0;
} else if (strcmp(name, "driver") == 0) {
strcpy(cc->dev_name, value);
} else if (strcmp(name, "media-driver") == 0) {
strcpy(cc->media_dev_name, value);
} else if (strcmp(name, "media-links") == 0) {
char **linkdefs = g_strsplit(value, ",", 0);
for (int i = 0; i < MP_MAX_LINKS && linkdefs[i] != NULL; ++i) {
char **linkdef = g_strsplit(linkdefs[i], "->", 2);
char **porta = g_strsplit(linkdef[0], ":", 2);
char **portb = g_strsplit(linkdef[1], ":", 2);
strcpy(cc->media_links[i].source_name, porta[0]);
strcpy(cc->media_links[i].target_name, portb[0]);
cc->media_links[i].source_port = strtoint(porta[1], NULL, 10);
cc->media_links[i].target_port = strtoint(portb[1], NULL, 10);
g_strfreev(portb);
g_strfreev(porta);
g_strfreev(linkdef);
++cc->num_media_links;
}
g_strfreev(linkdefs);
} else if (strcmp(name, "colormatrix") == 0) {
sscanf(value, "%f,%f,%f,%f,%f,%f,%f,%f,%f",
cc->colormatrix+0,
cc->colormatrix+1,
cc->colormatrix+2,
cc->colormatrix+3,
cc->colormatrix+4,
cc->colormatrix+5,
cc->colormatrix+6,
cc->colormatrix+7,
cc->colormatrix+8
);
} else if (strcmp(name, "forwardmatrix") == 0) {
sscanf(value, "%f,%f,%f,%f,%f,%f,%f,%f,%f",
cc->forwardmatrix+0,
cc->forwardmatrix+1,
cc->forwardmatrix+2,
cc->forwardmatrix+3,
cc->forwardmatrix+4,
cc->forwardmatrix+5,
cc->forwardmatrix+6,
cc->forwardmatrix+7,
cc->forwardmatrix+8
);
} else if (strcmp(name, "whitelevel") == 0) {
cc->whitelevel = strtoint(value, NULL, 10);
} else if (strcmp(name, "blacklevel") == 0) {
cc->blacklevel = strtoint(value, NULL, 10);
} else if (strcmp(name, "focallength") == 0) {
cc->focallength = strtof(value, NULL);
} else if (strcmp(name, "cropfactor") == 0) {
cc->cropfactor = strtof(value, NULL);
} else if (strcmp(name, "fnumber") == 0) {
cc->fnumber = strtod(value, NULL);
} else if (strcmp(name, "iso-min") == 0) {
cc->iso_min = strtod(value, NULL);
} else if (strcmp(name, "iso-max") == 0) {
cc->iso_max = strtod(value, NULL);
} else {
g_printerr("Unknown key '%s' in [%s]\n", name, section);
exit(1);
}
}
return 1;
if (strcmp(section, "device") == 0) {
if (strcmp(name, "make") == 0) {
exif_make = strdup(value);
} else if (strcmp(name, "model") == 0) {
exif_model = strdup(value);
} else {
g_printerr("Unknown key '%s' in [device]\n", name);
exit(1);
}
} else {
if (num_cameras == MP_MAX_CAMERAS) {
g_printerr("More cameras defined than NUM_CAMERAS\n");
exit(1);
}
size_t index = 0;
for (; index < num_cameras; ++index) {
if (strcmp(cameras[index].cfg_name, section) == 0) {
break;
}
}
if (index == num_cameras) {
printf("Adding camera %s from config\n", section);
++num_cameras;
cameras[index].index = index;
strcpy(cameras[index].cfg_name, section);
}
struct mp_camera_config *cc = &cameras[index];
if (config_handle_camera_mode("capture-", &cc->capture_mode, name,
value)) {
} else if (config_handle_camera_mode("preview-", &cc->preview_mode,
name, value)) {
} else if (strcmp(name, "rotate") == 0) {
cc->rotate = strtoint(value, NULL, 10);
} else if (strcmp(name, "mirrored") == 0) {
cc->mirrored = strcmp(value, "true") == 0;
} else if (strcmp(name, "driver") == 0) {
strcpy(cc->dev_name, value);
} else if (strcmp(name, "media-driver") == 0) {
strcpy(cc->media_dev_name, value);
} else if (strcmp(name, "media-links") == 0) {
char **linkdefs = g_strsplit(value, ",", 0);
for (int i = 0; i < MP_MAX_LINKS && linkdefs[i] != NULL;
++i) {
char **linkdef = g_strsplit(linkdefs[i], "->", 2);
char **porta = g_strsplit(linkdef[0], ":", 2);
char **portb = g_strsplit(linkdef[1], ":", 2);
strcpy(cc->media_links[i].source_name, porta[0]);
strcpy(cc->media_links[i].target_name, portb[0]);
cc->media_links[i].source_port =
strtoint(porta[1], NULL, 10);
cc->media_links[i].target_port =
strtoint(portb[1], NULL, 10);
g_strfreev(portb);
g_strfreev(porta);
g_strfreev(linkdef);
++cc->num_media_links;
}
g_strfreev(linkdefs);
} else if (strcmp(name, "colormatrix") == 0) {
sscanf(value, "%f,%f,%f,%f,%f,%f,%f,%f,%f",
cc->colormatrix + 0, cc->colormatrix + 1,
cc->colormatrix + 2, cc->colormatrix + 3,
cc->colormatrix + 4, cc->colormatrix + 5,
cc->colormatrix + 6, cc->colormatrix + 7,
cc->colormatrix + 8);
} else if (strcmp(name, "forwardmatrix") == 0) {
sscanf(value, "%f,%f,%f,%f,%f,%f,%f,%f,%f",
cc->forwardmatrix + 0, cc->forwardmatrix + 1,
cc->forwardmatrix + 2, cc->forwardmatrix + 3,
cc->forwardmatrix + 4, cc->forwardmatrix + 5,
cc->forwardmatrix + 6, cc->forwardmatrix + 7,
cc->forwardmatrix + 8);
} else if (strcmp(name, "whitelevel") == 0) {
cc->whitelevel = strtoint(value, NULL, 10);
} else if (strcmp(name, "blacklevel") == 0) {
cc->blacklevel = strtoint(value, NULL, 10);
} else if (strcmp(name, "focallength") == 0) {
cc->focallength = strtof(value, NULL);
} else if (strcmp(name, "cropfactor") == 0) {
cc->cropfactor = strtof(value, NULL);
} else if (strcmp(name, "fnumber") == 0) {
cc->fnumber = strtod(value, NULL);
} else if (strcmp(name, "iso-min") == 0) {
cc->iso_min = strtod(value, NULL);
} else if (strcmp(name, "iso-max") == 0) {
cc->iso_max = strtod(value, NULL);
} else {
g_printerr("Unknown key '%s' in [%s]\n", name, section);
exit(1);
}
}
return 1;
}
void
calculate_matrices() {
calculate_matrices()
{
for (size_t i = 0; i < MP_MAX_CAMERAS; ++i) {
if (cameras[i].colormatrix != NULL && cameras[i].forwardmatrix != NULL) {
multiply_matrices(cameras[i].colormatrix, cameras[i].forwardmatrix, cameras[i].previewmatrix);
if (cameras[i].colormatrix != NULL &&
cameras[i].forwardmatrix != NULL) {
multiply_matrices(cameras[i].colormatrix,
cameras[i].forwardmatrix,
cameras[i].previewmatrix);
}
}
}
bool mp_load_config() {
char file[512];
if (!find_config(file)) {
g_printerr("Could not find any config file\n");
return false;
}
bool
mp_load_config()
{
char file[512];
if (!find_config(file)) {
g_printerr("Could not find any config file\n");
return false;
}
int result = ini_parse(file, config_ini_handler, NULL);
if (result == -1) {
g_printerr("Config file not found\n");
return false;
}
if (result == -2) {
g_printerr("Could not allocate memory to parse config file\n");
return false;
}
if (result != 0) {
g_printerr("Could not parse config file\n");
return false;
}
int result = ini_parse(file, config_ini_handler, NULL);
if (result == -1) {
g_printerr("Config file not found\n");
return false;
}
if (result == -2) {
g_printerr("Could not allocate memory to parse config file\n");
return false;
}
if (result != 0) {
g_printerr("Could not parse config file\n");
return false;
}
calculate_matrices();
calculate_matrices();
return true;
return true;
}
const char * mp_get_device_make()
const char *
mp_get_device_make()
{
return exif_make;
return exif_make;
}
const char * mp_get_device_model()
const char *
mp_get_device_model()
{
return exif_model;
return exif_model;
}
const struct mp_camera_config * mp_get_camera_config(size_t index)
const struct mp_camera_config *
mp_get_camera_config(size_t index)
{
if (index >= num_cameras)
return NULL;
if (index >= num_cameras)
return NULL;
return &cameras[index];
return &cameras[index];
}

60
camera_config.h

@ -9,42 +9,42 @@
#define MP_MAX_LINKS 10
struct mp_media_link_config {
char source_name[100];
char target_name[100];
int source_port;
int target_port;
char source_name[100];
char target_name[100];
int source_port;
int target_port;
};
struct mp_camera_config {
size_t index;
char cfg_name[100];
char dev_name[260];
char media_dev_name[260];
MPCameraMode capture_mode;
MPCameraMode preview_mode;
int rotate;
bool mirrored;
struct mp_media_link_config media_links[MP_MAX_LINKS];
int num_media_links;
float colormatrix[9];
float forwardmatrix[9];
float previewmatrix[9];
int blacklevel;
int whitelevel;
float focallength;
float cropfactor;
double fnumber;
int iso_min;
int iso_max;
size_t index;
char cfg_name[100];
char dev_name[260];
char media_dev_name[260];
MPCameraMode capture_mode;
MPCameraMode preview_mode;
int rotate;
bool mirrored;
struct mp_media_link_config media_links[MP_MAX_LINKS];
int num_media_links;
float colormatrix[9];
float forwardmatrix[9];
float previewmatrix[9];
int blacklevel;
int whitelevel;
float focallength;
float cropfactor;
double fnumber;
int iso_min;
int iso_max;
};
bool mp_load_config();
const char *mp_get_device_make();
const char *mp_get_device_model();
const struct mp_camera_config * mp_get_camera_config(size_t index);
const struct mp_camera_config *mp_get_camera_config(size_t index);

590
device.c

@ -8,416 +8,456 @@
#include <sys/ioctl.h>
#include <unistd.h>
bool mp_find_device_path(struct media_v2_intf_devnode devnode, char *path, int length)
bool
mp_find_device_path(struct media_v2_intf_devnode devnode, char *path, int length)
{
char uevent_path[256];
snprintf(uevent_path, 256, "/sys/dev/char/%d:%d/uevent", devnode.major, devnode.minor);
char uevent_path[256];
snprintf(uevent_path, 256, "/sys/dev/char/%d:%d/uevent", devnode.major,
devnode.minor);
FILE *f = fopen(uevent_path, "r");
if (!f) {
return false;
}
FILE *f = fopen(uevent_path, "r");
if (!f) {
return false;
}
char line[512];
while (fgets(line, 512, f)) {
if (strncmp(line, "DEVNAME=", 8) == 0) {
// Drop newline
int length = strlen(line);
if (line[length - 1] == '\n')
line[length - 1] = '\0';
char line[512];
while (fgets(line, 512, f)) {
if (strncmp(line, "DEVNAME=", 8) == 0) {
// Drop newline
int length = strlen(line);
if (line[length - 1] == '\n')
line[length - 1] = '\0';
snprintf(path, length, "/dev/%s", line + 8);
return true;
}
}
snprintf(path, length, "/dev/%s", line + 8);
return true;
}
}
fclose(f);
fclose(f);
return false;
return false;
}
struct _MPDevice {
int fd;
struct media_device_info info;
struct media_v2_entity *entities;
size_t num_entities;
struct media_v2_interface *interfaces;
size_t num_interfaces;
struct media_v2_pad *pads;
size_t num_pads;
struct media_v2_link *links;
size_t num_links;
int fd;
struct media_device_info info;
struct media_v2_entity *entities;
size_t num_entities;
struct media_v2_interface *interfaces;
size_t num_interfaces;
struct media_v2_pad *pads;
size_t num_pads;
struct media_v2_link *links;
size_t num_links;
};
static void errno_printerr(const char *s)
static void
errno_printerr(const char *s)
{
g_printerr("MPDevice: %s error %d, %s\n", s, errno, strerror(errno));
g_printerr("MPDevice: %s error %d, %s\n", s, errno, strerror(errno));
}
static int xioctl(int fd, int request, void *arg)
static int
xioctl(int fd, int request, void *arg)
{
int r;
do {
r = ioctl(fd, request, arg);
} while (r == -1 && errno == EINTR);
return r;
int r;
do {
r = ioctl(fd, request, arg);
} while (r == -1 && errno == EINTR);
return r;
}
MPDevice *mp_device_find(const char *driver_name)
MPDevice *
mp_device_find(const char *driver_name)
{
MPDeviceList *list = mp_device_list_new();
MPDeviceList *list = mp_device_list_new();
MPDevice *found_device = mp_device_list_find_remove(&list, driver_name);
MPDevice *found_device = mp_device_list_find_remove(&list, driver_name);
mp_device_list_free(list);
mp_device_list_free(list);
return found_device;
return found_device;
}
MPDevice *mp_device_open(const char *path)
MPDevice *
mp_device_open(const char *path)
{
int fd = open(path, O_RDWR);
if (fd == -1) {
errno_printerr("open");
return NULL;
}
int fd = open(path, O_RDWR);
if (fd == -1) {
errno_printerr("open");
return NULL;
}
return mp_device_new(fd);
return mp_device_new(fd);
}
MPDevice *mp_device_new(int fd)
MPDevice *
mp_device_new(int fd)
{
// Get the topology of the media device
struct media_v2_topology topology = {};
if (xioctl(fd, MEDIA_IOC_G_TOPOLOGY, &topology) == -1
|| topology.num_entities == 0) {
close(fd);
return NULL;
}
// Get the topology of the media device
struct media_v2_topology topology = {};
if (xioctl(fd, MEDIA_IOC_G_TOPOLOGY, &topology) == -1 ||
topology.num_entities == 0) {
close(fd);
return NULL;
}
// Create the device
MPDevice *device = calloc(1, sizeof(MPDevice));
device->fd = fd;
device->entities = calloc(topology.num_entities, sizeof(struct media_v2_entity));
device->num_entities = topology.num_entities;
device->interfaces = calloc(topology.num_interfaces, sizeof(struct media_v2_interface));
device->num_interfaces = topology.num_interfaces;
device->pads = calloc(topology.num_pads, sizeof(struct media_v2_pad));
device->num_pads = topology.num_pads;
device->links = calloc(topology.num_links, sizeof(struct media_v2_link));
device->num_links = topology.num_links;
// Create the device
MPDevice *device = calloc(1, sizeof(MPDevice));
device->fd = fd;
device->entities =
calloc(topology.num_entities, sizeof(struct media_v2_entity));
device->num_entities = topology.num_entities;
device->interfaces =
calloc(topology.num_interfaces, sizeof(struct media_v2_interface));
device->num_interfaces = topology.num_interfaces;
device->pads = calloc(topology.num_pads, sizeof(struct media_v2_pad));
device->num_pads = topology.num_pads;
device->links = calloc(topology.num_links, sizeof(struct media_v2_link));
device->num_links = topology.num_links;
// Get the actual devices and interfaces
topology.ptr_entities = (uint64_t)device->entities;
topology.ptr_interfaces = (uint64_t)device->interfaces;
topology.ptr_pads = (uint64_t)device->pads;
topology.ptr_links = (uint64_t)device->links;
if (xioctl(fd, MEDIA_IOC_G_TOPOLOGY, &topology) == -1) {
errno_printerr("MEDIA_IOC_G_TOPOLOGY");
mp_device_close(device);
return NULL;
}
// Get the actual devices and interfaces
topology.ptr_entities = (uint64_t)device->entities;
topology.ptr_interfaces = (uint64_t)device->interfaces;
topology.ptr_pads = (uint64_t)device->pads;
topology.ptr_links = (uint64_t)device->links;
if (xioctl(fd, MEDIA_IOC_G_TOPOLOGY, &topology) == -1) {
errno_printerr("MEDIA_IOC_G_TOPOLOGY");
mp_device_close(device);
return NULL;
}
// Get device info
if (xioctl(fd, MEDIA_IOC_DEVICE_INFO, &device->info) == -1) {
errno_printerr("MEDIA_IOC_DEVICE_INFO");
mp_device_close(device);
return NULL;
}
// Get device info
if (xioctl(fd, MEDIA_IOC_DEVICE_INFO, &device->info) == -1) {
errno_printerr("MEDIA_IOC_DEVICE_INFO");
mp_device_close(device);
return NULL;
}
return device;
return device;
}
void mp_device_close(MPDevice *device)
void
mp_device_close(MPDevice *device)
{
close(device->fd);
free(device->entities);
free(device->interfaces);
free(device->pads);
free(device->links);
free(device);
close(device->fd);
free(device->entities);
free(device->interfaces);
free(device->pads);
free(device->links);
free(device);
}
bool mp_device_setup_link(MPDevice *device, uint32_t source_pad_id, uint32_t sink_pad_id, bool enabled)
bool
mp_device_setup_link(MPDevice *device, uint32_t source_pad_id, uint32_t sink_pad_id,
bool enabled)
{
const struct media_v2_pad *source_pad = mp_device_get_pad(device, source_pad_id);
g_return_val_if_fail(source_pad, false);
const struct media_v2_pad *source_pad =
mp_device_get_pad(device, source_pad_id);
g_return_val_if_fail(source_pad, false);
const struct media_v2_pad *sink_pad = mp_device_get_pad(device, sink_pad_id);
g_return_val_if_fail(sink_pad, false);
const struct media_v2_pad *sink_pad = mp_device_get_pad(device, sink_pad_id);
g_return_val_if_fail(sink_pad, false);
struct media_link_desc link = {};
link.flags = enabled ? MEDIA_LNK_FL_ENABLED : 0;
link.source.entity = source_pad->entity_id;
link.source.index = 0;
link.sink.entity = sink_pad->entity_id;
link.sink.index = 0;
if (xioctl(device->fd, MEDIA_IOC_SETUP_LINK, &link) == -1) {
errno_printerr("MEDIA_IOC_SETUP_LINK");
return false;
}
struct media_link_desc link = {};
link.flags = enabled ? MEDIA_LNK_FL_ENABLED : 0;
link.source.entity = source_pad->entity_id;
link.source.index = 0;
link.sink.entity = sink_pad->entity_id;
link.sink.index = 0;
if (xioctl(device->fd, MEDIA_IOC_SETUP_LINK, &link) == -1) {
errno_printerr("MEDIA_IOC_SETUP_LINK");
return false;
}
return true;
return true;
}
const struct media_v2_entity *mp_device_find_entity(const MPDevice *device, const char *driver_name)
const struct media_v2_entity *
mp_device_find_entity(const MPDevice *device, const char *driver_name)
{
int length = strlen(driver_name);
int length = strlen(driver_name);
// Find the entity from the name
for (uint32_t i = 0; i < device->num_entities; ++i) {
if (strncmp(device->entities[i].name, driver_name, length) == 0) {
return &device->entities[i];
}
}
return NULL;
// Find the entity from the name
for (uint32_t i = 0; i < device->num_entities; ++i) {
if (strncmp(device->entities[i].name, driver_name, length) == 0) {
return &device->entities[i];
}
}
return NULL;
}
const struct media_v2_entity *mp_device_find_entity_type(const MPDevice *device, const uint32_t type)
const struct media_v2_entity *
mp_device_find_entity_type(const MPDevice *device, const uint32_t type)
{
// Find the entity from the entity type
for (uint32_t i = 0; i < device->num_entities; ++i) {
if (device->entities[i].function == type) {
return &device->entities[i];
}
}
return NULL;
// Find the entity from the entity type
for (uint32_t i = 0; i < device->num_entities; ++i) {
if (device->entities[i].function == type) {
return &device->entities[i];
}
}
return NULL;
}
const struct media_device_info *mp_device_get_info(const MPDevice *device)
const struct media_device_info *
mp_device_get_info(const MPDevice *device)
{
return &device->info;
return &device->info;
}
const struct media_v2_entity *mp_device_get_entity(const MPDevice *device, uint32_t id)
const struct media_v2_entity *
mp_device_get_entity(const MPDevice *device, uint32_t id)
{
for (int i = 0; i < device->num_entities; ++i) {
if (device->entities[i].id == id) {
return &device->entities[i];
}
}
return NULL;
for (int i = 0; i < device->num_entities; ++i) {
if (device->entities[i].id == id) {
return &device->entities[i];
}
}
return NULL;
}
const struct media_v2_entity *mp_device_get_entities(const MPDevice *device)
const struct media_v2_entity *
mp_device_get_entities(const MPDevice *device)
{
return device->entities;
return device->entities;
}
size_t mp_device_get_num_entities(const MPDevice *device)
size_t
mp_device_get_num_entities(const