mediautil.h (5987B)
1 /* 2 This file is part of libextractor. 3 Copyright (C) 2026 Vidyut Samanta and Christian Grothoff 4 5 libextractor is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published 7 by the Free Software Foundation; either version 3, or (at your 8 option) any later version. 9 10 libextractor is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with libextractor; see the file COPYING. If not, write to the 17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 18 Boston, MA 02110-1301, USA. 19 */ 20 /** 21 * @file plugins/mediautil.h 22 * @brief helpers shared by the video thumbnail and audio preview plugins 23 * @author Christian Grothoff 24 * 25 * The four media plugins (thumbnailffmpeg, thumbnailgst, previewopus and 26 * previewgst) all have to do the same three boring things before they can 27 * get to work: parse the plugin option string, make sure the input even 28 * looks like media, and keep an eye on the clock. This file is compiled 29 * into each of them (plugins are modules and must not depend on each 30 * other, so this is deliberately a source-level, not a library-level, 31 * dependency). 32 */ 33 #ifndef MEDIAUTIL_H 34 #define MEDIAUTIL_H 35 36 #include "extractor.h" 37 38 /** 39 * Maximum number of bytes we are willing to emit for a thumbnail. 40 */ 41 #define EXTRACTOR_MEDIA_MAX_THUMBNAIL (100 * 1024) 42 43 /** 44 * Maximum number of bytes we are willing to emit for an audio preview. 45 */ 46 #define EXTRACTOR_MEDIA_MAX_PREVIEW (64 * 1024) 47 48 /** 49 * Largest image we are willing to scale down, in pixels. Guards against 50 * absurd dimensions in a hostile file header. 51 */ 52 #define EXTRACTOR_MEDIA_MAX_PIXELS (64 * 1024 * 1024) 53 54 /** 55 * Largest width or height we accept for the source video. 56 */ 57 #define EXTRACTOR_MEDIA_MAX_DIMENSION 16384 58 59 60 /** 61 * Options a media plugin understands, as given in the plugin 62 * configuration string, i.e. "thumbnailffmpeg(size=256,format=png)". 63 */ 64 struct EXTRACTOR_MediaOptions 65 { 66 /** 67 * Edge length of the box the thumbnail must fit into, in pixels. 68 */ 69 unsigned int size; 70 71 /** 72 * Emit PNG instead of JPEG (option "format=png"). 73 */ 74 int png; 75 76 /** 77 * How many seconds into the media we would like to sample. 78 */ 79 unsigned int offset; 80 81 /** 82 * Length of the audio preview in seconds. 83 */ 84 unsigned int length; 85 86 /** 87 * Target bitrate of the Opus encoder, in bits per second. 88 */ 89 unsigned int bitrate; 90 91 /** 92 * Wall clock budget for the entire extraction, in milliseconds. 93 */ 94 unsigned int deadline; 95 }; 96 97 98 /** 99 * A wall clock deadline. 100 */ 101 struct EXTRACTOR_MediaDeadline 102 { 103 /** 104 * When we must be done, in milliseconds since some arbitrary epoch. 105 */ 106 uint64_t expiration; 107 }; 108 109 110 /** 111 * Initialize @a opt with the defaults, then apply the comma-separated 112 * "key=value" assignments from @a config. Unknown keys and malformed 113 * values are ignored; the resulting values are always in range. 114 * 115 * @param config configuration string of the plugin, may be NULL 116 * @param[out] opt set to the resulting options 117 */ 118 void 119 EXTRACTOR_media_options_parse_ (const char *config, 120 struct EXTRACTOR_MediaOptions *opt); 121 122 123 /** 124 * Check whether the input of @a ec looks like media we should touch, 125 * using libmagic on the first few kilobytes. Always leaves the 126 * extraction context rewound to the beginning of the file. 127 * 128 * Handing arbitrary bytes to a media framework is expensive and grows 129 * the attack surface for no gain, so both plugin families gate on this. 130 * 131 * @param ec extraction context to inspect 132 * @param want_audio also accept "audio/*" (in addition to "video/*") 133 * @return 1 if the input should be processed, 0 if not 134 */ 135 int 136 EXTRACTOR_media_is_media_ (struct EXTRACTOR_ExtractContext *ec, 137 int want_audio); 138 139 140 /** 141 * Start the clock. 142 * 143 * @param[out] dl deadline to initialize 144 * @param ms budget in milliseconds from now 145 */ 146 void 147 EXTRACTOR_media_deadline_start_ (struct EXTRACTOR_MediaDeadline *dl, 148 unsigned int ms); 149 150 151 /** 152 * Check whether the budget is used up. 153 * 154 * @param dl deadline to check 155 * @return 1 if we are out of time 156 */ 157 int 158 EXTRACTOR_media_deadline_expired_ (const struct EXTRACTOR_MediaDeadline *dl); 159 160 161 /** 162 * Time left before @a dl expires. 163 * 164 * @param dl deadline to check 165 * @return remaining milliseconds, 0 if expired 166 */ 167 uint64_t 168 EXTRACTOR_media_deadline_remaining_ (const struct EXTRACTOR_MediaDeadline *dl); 169 170 171 /** 172 * Compute the size of a thumbnail with the aspect ratio of a 173 * @a width x @a height image that fits into a @a box x @a box square. 174 * Never scales up. 175 * 176 * @param width width of the source image 177 * @param height height of the source image 178 * @param box edge length of the target square 179 * @param[out] rwidth width of the thumbnail 180 * @param[out] rheight height of the thumbnail 181 * @return 1 on success, 0 if the source dimensions are unusable 182 */ 183 int 184 EXTRACTOR_media_thumbnail_size_ (unsigned int width, 185 unsigned int height, 186 unsigned int box, 187 unsigned int *rwidth, 188 unsigned int *rheight); 189 190 191 /** 192 * Apply a linear fade-in and fade-out to 16-bit signed mono PCM. 193 * Without this, a preview cut out of the middle of a track starts and 194 * ends with an audible click. 195 * 196 * @param[in,out] samples PCM data to modify 197 * @param num_samples number of samples in @a samples 198 * @param fade_samples length of each ramp in samples 199 */ 200 void 201 EXTRACTOR_media_fade_s16_ (int16_t *samples, 202 size_t num_samples, 203 size_t fade_samples); 204 205 #endif 206 207 /* end of mediautil.h */