/* SPDX-License-Identifier: Apache-2.0 */ /* * Copyright (C) 2012 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef SYSTEM_MEDIA_INCLUDE_ANDROID_CAMERA_METADATA_H #define SYSTEM_MEDIA_INCLUDE_ANDROID_CAMERA_METADATA_H #include #include #include #ifdef __cplusplus extern "C" { #endif /** * Tag hierarchy and enum definitions for camera_metadata_entry * ============================================================================= */ /** * Main enum definitions are in a separate file to make it easy to * maintain */ #include "camera_metadata_tags.h" /** * Enum range for each top-level category */ ANDROID_API extern unsigned int camera_metadata_section_bounds[ANDROID_SECTION_COUNT][2]; ANDROID_API extern const char *camera_metadata_section_names[ANDROID_SECTION_COUNT]; /** * Type definitions for camera_metadata_entry * ============================================================================= */ enum { // Unsigned 8-bit integer (uint8_t) TYPE_BYTE = 0, // Signed 32-bit integer (int32_t) TYPE_INT32 = 1, // 32-bit float (float) TYPE_FLOAT = 2, // Signed 64-bit integer (int64_t) TYPE_INT64 = 3, // 64-bit float (double) TYPE_DOUBLE = 4, // A 64-bit fraction (camera_metadata_rational_t) TYPE_RATIONAL = 5, // Number of type fields NUM_TYPES }; typedef struct camera_metadata_rational { int32_t numerator; int32_t denominator; } camera_metadata_rational_t; /** * A reference to a metadata entry in a buffer. * * The data union pointers point to the real data in the buffer, and can be * modified in-place if the count does not need to change. The count is the * number of entries in data of the entry's type, not a count of bytes. */ typedef struct camera_metadata_entry { size_t index; uint32_t tag; uint8_t type; size_t count; union { uint8_t *u8; int32_t *i32; float *f; int64_t *i64; double *d; camera_metadata_rational_t *r; } data; } camera_metadata_entry_t; /** * A read-only reference to a metadata entry in a buffer. Identical to * camera_metadata_entry in layout */ typedef struct camera_metadata_ro_entry { size_t index; uint32_t tag; uint8_t type; size_t count; union { const uint8_t *u8; const int32_t *i32; const float *f; const int64_t *i64; const double *d; const camera_metadata_rational_t *r; } data; } camera_metadata_ro_entry_t; /** * Size in bytes of each entry type */ ANDROID_API extern const size_t camera_metadata_type_size[NUM_TYPES]; /** * Human-readable name of each entry type */ ANDROID_API extern const char* camera_metadata_type_names[NUM_TYPES]; /** * Main definitions for the metadata entry and array structures * ============================================================================= */ /** * A packet of metadata. This is a list of metadata entries, each of which has * an integer tag to identify its meaning, 'type' and 'count' field, and the * data, which contains a 'count' number of entries of type 'type'. The packet * has a fixed capacity for entries and for extra data. A new entry uses up one * entry slot, and possibly some amount of data capacity; the function * calculate_camera_metadata_entry_data_size() provides the amount of data * capacity that would be used up by an entry. * * Entries are not sorted by default, and are not forced to be unique - multiple * entries with the same tag are allowed. The packet will not dynamically resize * when full. * * The packet is contiguous in memory, with size in bytes given by * get_camera_metadata_size(). Therefore, it can be copied safely with memcpy() * to a buffer of sufficient size. The copy_camera_metadata() function is * intended for eliminating unused capacity in the destination packet. */ struct camera_metadata; typedef struct camera_metadata camera_metadata_t; /** * Functions for manipulating camera metadata * ============================================================================= * * NOTE: Unless otherwise specified, functions that return type "int" * return 0 on success, and non-0 value on error. */ /** * Allocate a new camera_metadata structure, with some initial space for entries * and extra data. The entry_capacity is measured in entry counts, and * data_capacity in bytes. The resulting structure is all contiguous in memory, * and can be freed with free_camera_metadata(). */ ANDROID_API camera_metadata_t *allocate_camera_metadata(size_t entry_capacity, size_t data_capacity); /** * Get the required alignment of a packet of camera metadata, which is the * maximal alignment of the embedded camera_metadata, camera_metadata_buffer_entry, * and camera_metadata_data. */ ANDROID_API size_t get_camera_metadata_alignment(); /** * Allocate a new camera_metadata structure of size src_size. Copy the data, * ignoring alignment, and then attempt validation. If validation * fails, free the memory and return NULL. Otherwise return the pointer. * * The resulting pointer can be freed with free_camera_metadata(). */ ANDROID_API camera_metadata_t *allocate_copy_camera_metadata_checked( const camera_metadata_t *src, size_t src_size); /** * Place a camera metadata structure into an existing buffer. Returns NULL if * the buffer is too small for the requested number of reserved entries and * bytes of data. The entry_capacity is measured in entry counts, and * data_capacity in bytes. If the buffer is larger than the required space, * unused space will be left at the end. If successful, returns a pointer to the * metadata header placed at the start of the buffer. It is the caller's * responsibility to free the original buffer; do not call * free_camera_metadata() with the returned pointer. */ ANDROID_API camera_metadata_t *place_camera_metadata(void *dst, size_t dst_size, size_t entry_capacity, size_t data_capacity); /** * Free a camera_metadata structure. Should only be used with structures * allocated with allocate_camera_metadata(). */ ANDROID_API void free_camera_metadata(camera_metadata_t *metadata); /** * Calculate the buffer size needed for a metadata structure of entry_count * metadata entries, needing a total of data_count bytes of extra data storage. */ ANDROID_API size_t calculate_camera_metadata_size(size_t entry_count, size_t data_count); /** * Get current size of entire metadata structure in bytes, including reserved * but unused space. */ ANDROID_API size_t get_camera_metadata_size(const camera_metadata_t *metadata); /** * Get size of entire metadata buffer in bytes, not including reserved but * unused space. This is the amount of space needed by copy_camera_metadata for * its dst buffer. */ ANDROID_API size_t get_camera_metadata_compact_size(const camera_metadata_t *metadata); /** * Get the current number of entries in the metadata packet. * * metadata packet must be valid, which can be checked before the call with * validate_camera_metadata_structure(). */ ANDROID_API size_t get_camera_metadata_entry_count(const camera_metadata_t *metadata); /** * Get the maximum number of entries that could fit in the metadata packet. */ ANDROID_API size_t get_camera_metadata_entry_capacity(const camera_metadata_t *metadata); /** * Get the current count of bytes used for value storage in the metadata packet. */ ANDROID_API size_t get_camera_metadata_data_count(const camera_metadata_t *metadata); /** * Get the maximum count of bytes that could be used for value storage in the * metadata packet. */ ANDROID_API size_t get_camera_metadata_data_capacity(const camera_metadata_t *metadata); /** * Copy a metadata structure to a memory buffer, compacting it along the * way. That is, in the copied structure, entry_count == entry_capacity, and * data_count == data_capacity. * * If dst_size > get_camera_metadata_compact_size(), the unused bytes are at the * end of the buffer. If dst_size < get_camera_metadata_compact_size(), returns * NULL. Otherwise returns a pointer to the metadata structure header placed at * the start of dst. * * Since the buffer was not allocated by allocate_camera_metadata, the caller is * responsible for freeing the underlying buffer when needed; do not call * free_camera_metadata. */ ANDROID_API camera_metadata_t *copy_camera_metadata(void *dst, size_t dst_size, const camera_metadata_t *src); // Non-zero return values for validate_camera_metadata_structure enum { CAMERA_METADATA_VALIDATION_ERROR = 1, CAMERA_METADATA_VALIDATION_SHIFTED = 2, }; /** * Validate that a metadata is structurally sane. That is, its internal * state is such that we won't get buffer overflows or run into other * 'impossible' issues when calling the other API functions. * * This is useful in particular after copying the binary metadata blob * from an untrusted source, since passing this check means the data is at least * consistent. * * The expected_size argument is optional. * * Returns 0: on success * CAMERA_METADATA_VALIDATION_ERROR: on error * CAMERA_METADATA_VALIDATION_SHIFTED: when the data is not properly aligned, but can be * used as input of clone_camera_metadata and the returned metadata will be valid. * */ ANDROID_API int validate_camera_metadata_structure(const camera_metadata_t *metadata, const size_t *expected_size); /** * Append camera metadata in src to an existing metadata structure in dst. This * does not resize the destination structure, so if it is too small, a non-zero * value is returned. On success, 0 is returned. Appending onto a sorted * structure results in a non-sorted combined structure. */ ANDROID_API int append_camera_metadata(camera_metadata_t *dst, const camera_metadata_t *src); /** * Clone an existing metadata buffer, compacting along the way. This is * equivalent to allocating a new buffer of the minimum needed size, then * appending the buffer to be cloned into the new buffer. The resulting buffer * can be freed with free_camera_metadata(). Returns NULL if cloning failed. */ ANDROID_API camera_metadata_t *clone_camera_metadata(const camera_metadata_t *src); /** * Calculate the number of bytes of extra data a given metadata entry will take * up. That is, if entry of 'type' with a payload of 'data_count' values is * added, how much will the value returned by get_camera_metadata_data_count() * be increased? This value may be zero, if no extra data storage is needed. */ ANDROID_API size_t calculate_camera_metadata_entry_data_size(uint8_t type, size_t data_count); /** * Add a metadata entry to a metadata structure. Returns 0 if the addition * succeeded. Returns a non-zero value if there is insufficient reserved space * left to add the entry, or if the tag is unknown. data_count is the number of * entries in the data array of the tag's type, not a count of * bytes. Vendor-defined tags can not be added using this method, unless * set_vendor_tag_query_ops() has been called first. Entries are always added to * the end of the structure (highest index), so after addition, a * previously-sorted array will be marked as unsorted. * * Returns 0 on success. A non-0 value is returned on error. */ ANDROID_API int add_camera_metadata_entry(camera_metadata_t *dst, uint32_t tag, const void *data, size_t data_count); /** * Sort the metadata buffer for fast searching. If already marked as sorted, * does nothing. Adding or appending entries to the buffer will place the buffer * back into an unsorted state. * * Returns 0 on success. A non-0 value is returned on error. */ ANDROID_API int sort_camera_metadata(camera_metadata_t *dst); /** * Get metadata entry at position index in the metadata buffer. * Index must be less than entry count, which is returned by * get_camera_metadata_entry_count(). * * src and index are inputs; the passed-in entry is updated with the details of * the entry. The data pointer points to the real data in the buffer, and can be * updated as long as the data count does not change. * * Returns 0 on success. A non-0 value is returned on error. */ ANDROID_API int get_camera_metadata_entry(camera_metadata_t *src, size_t index, camera_metadata_entry_t *entry); /** * Get metadata entry at position index, but disallow editing the data. */ ANDROID_API int get_camera_metadata_ro_entry(const camera_metadata_t *src, size_t index, camera_metadata_ro_entry_t *entry); /** * Find an entry with given tag value. If not found, returns -ENOENT. Otherwise, * returns entry contents like get_camera_metadata_entry. * * If multiple entries with the same tag exist, does not have any guarantees on * which is returned. To speed up searching for tags, sort the metadata * structure first by calling sort_camera_metadata(). */ ANDROID_API int find_camera_metadata_entry(camera_metadata_t *src, uint32_t tag, camera_metadata_entry_t *entry); /** * Find an entry with given tag value, but disallow editing the data */ ANDROID_API int find_camera_metadata_ro_entry(const camera_metadata_t *src, uint32_t tag, camera_metadata_ro_entry_t *entry); /** * Delete an entry at given index. This is an expensive operation, since it * requires repacking entries and possibly entry data. This also invalidates any * existing camera_metadata_entry.data pointers to this buffer. Sorting is * maintained. */ ANDROID_API int delete_camera_metadata_entry(camera_metadata_t *dst, size_t index); /** * Updates a metadata entry with new data. If the data size is changing, may * need to adjust the data array, making this an O(N) operation. If the data * size is the same or still fits in the entry space, this is O(1). Maintains * sorting, but invalidates camera_metadata_entry instances that point to the * updated entry. If a non-NULL value is passed in to entry, the entry structure * is updated to match the new buffer state. Returns a non-zero value if there * is no room for the new data in the buffer. */ ANDROID_API int update_camera_metadata_entry(camera_metadata_t *dst, size_t index, const void *data, size_t data_count, camera_metadata_entry_t *updated_entry); /** * Retrieve human-readable name of section the tag is in. Returns NULL if * no such tag is defined. Returns NULL for tags in the vendor section, unless * set_vendor_tag_query_ops() has been used. */ ANDROID_API const char *get_camera_metadata_section_name(uint32_t tag); /** * Retrieve human-readable name of tag (not including section). Returns NULL if * no such tag is defined. Returns NULL for tags in the vendor section, unless * set_vendor_tag_query_ops() has been used. */ ANDROID_API const char *get_camera_metadata_tag_name(uint32_t tag); /** * Retrieve the type of a tag. Returns -1 if no such tag is defined. Returns -1 * for tags in the vendor section, unless set_vendor_tag_query_ops() has been * used. */ ANDROID_API int get_camera_metadata_tag_type(uint32_t tag); /** * Retrieve human-readable name of section the tag is in. Returns NULL if * no such tag is defined. */ ANDROID_API const char *get_local_camera_metadata_section_name(uint32_t tag, const camera_metadata_t *meta); /** * Retrieve human-readable name of tag (not including section). Returns NULL if * no such tag is defined. */ ANDROID_API const char *get_local_camera_metadata_tag_name(uint32_t tag, const camera_metadata_t *meta); /** * Retrieve the type of a tag. Returns -1 if no such tag is defined. */ ANDROID_API int get_local_camera_metadata_tag_type(uint32_t tag, const camera_metadata_t *meta); /** * Set up vendor-specific tag query methods. These are needed to properly add * entries with vendor-specified tags and to use the * get_camera_metadata_section_name, _tag_name, and _tag_type methods with * vendor tags. Returns 0 on success. * * **DEPRECATED** - Please use vendor_tag_ops defined in camera_vendor_tags.h * instead. */ typedef struct vendor_tag_query_ops vendor_tag_query_ops_t; struct vendor_tag_query_ops { /** * Get vendor section name for a vendor-specified entry tag. Only called for * tags >= 0x80000000. The section name must start with the name of the * vendor in the Java package style. For example, CameraZoom inc must prefix * their sections with "com.camerazoom." Must return NULL if the tag is * outside the bounds of vendor-defined sections. */ const char *(*get_camera_vendor_section_name)( const vendor_tag_query_ops_t *v, uint32_t tag); /** * Get tag name for a vendor-specified entry tag. Only called for tags >= * 0x80000000. Must return NULL if the tag is outside the bounds of * vendor-defined sections. */ const char *(*get_camera_vendor_tag_name)( const vendor_tag_query_ops_t *v, uint32_t tag); /** * Get tag type for a vendor-specified entry tag. Only called for tags >= * 0x80000000. Must return -1 if the tag is outside the bounds of * vendor-defined sections. */ int (*get_camera_vendor_tag_type)( const vendor_tag_query_ops_t *v, uint32_t tag); /** * Get the number of vendor tags supported on this platform. Used to * calculate the size of buffer needed for holding the array of all tags * returned by get_camera_vendor_tags(). */ int (*get_camera_vendor_tag_count)( const vendor_tag_query_ops_t *v); /** * Fill an array with all the supported vendor tags on this platform. * get_camera_vendor_tag_count() returns the number of tags supported, and * tag_array should be allocated with enough space to hold all of the tags. */ void (*get_camera_vendor_tags)( const vendor_tag_query_ops_t *v, uint32_t *tag_array); }; /** * **DEPRECATED** - This should only be used by the camera framework. Camera * metadata will transition to using vendor_tag_ops defined in * camera_vendor_tags.h instead. */ ANDROID_API int set_camera_metadata_vendor_tag_ops(const vendor_tag_query_ops_t *query_ops); /** * Print fields in the metadata to the log. * verbosity = 0: Only tag entry information * verbosity = 1: Tag entry information plus at most 16 data values * verbosity = 2: All information */ ANDROID_API void dump_camera_metadata(const camera_metadata_t *metadata, int fd, int verbosity); /** * Print fields in the metadata to the log; adds indentation parameter, which * specifies the number of spaces to insert before each line of the dump */ ANDROID_API void dump_indented_camera_metadata(const camera_metadata_t *metadata, int fd, int verbosity, int indentation); /** * Prints the specified tag value as a string. Only works for enum tags. * Returns 0 on success, -1 on failure. */ ANDROID_API int camera_metadata_enum_snprint(uint32_t tag, uint32_t value, char *dst, size_t size); #ifdef __cplusplus } #endif #endif > 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257