mediaffmpeg.h (4302B)
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/mediaffmpeg.h 22 * @brief plumbing between an extraction context and FFmpeg's demuxer 23 * @author Christian Grothoff 24 * 25 * Shared by thumbnailffmpeg and previewopus. Requires FFmpeg 5.1 or 26 * newer; there is deliberately no compatibility code for older versions. 27 */ 28 #ifndef MEDIAFFMPEG_H 29 #define MEDIAFFMPEG_H 30 31 #include "extractor.h" 32 #include "mediautil.h" 33 34 #include <libavcodec/avcodec.h> 35 #include <libavformat/avformat.h> 36 #include <libavutil/avutil.h> 37 38 /** 39 * Size of the buffer we give to the AVIOContext. Small on purpose: each 40 * refill turns into a read on the extraction context, which is what keeps 41 * the IPC channel from looking hung to the parent process (which kills 42 * plugins that stay silent for 500ms). 43 */ 44 #define EXTRACTOR_FFMPEG_AVIO_BUFFER (16 * 1024) 45 46 /** 47 * Do not look at more than this much data to identify the streams. 48 */ 49 #define EXTRACTOR_FFMPEG_PROBE_SIZE (1024 * 1024) 50 51 /** 52 * Do not spend more than this much media time on stream analysis. 53 */ 54 #define EXTRACTOR_FFMPEG_MAX_ANALYZE (2 * AV_TIME_BASE) 55 56 57 /** 58 * State of an FFmpeg demuxer reading from an extraction context. 59 */ 60 struct EXTRACTOR_FFmpegIO 61 { 62 /** 63 * Extraction context we are reading from. 64 */ 65 struct EXTRACTOR_ExtractContext *ec; 66 67 /** 68 * When we have to give up. 69 */ 70 struct EXTRACTOR_MediaDeadline deadline; 71 72 /** 73 * Format context, NULL if not open. 74 */ 75 AVFormatContext *fmt; 76 77 /** 78 * I/O context wrapping @e ec. 79 */ 80 AVIOContext *avio; 81 }; 82 83 84 /** 85 * Open the input of @a io->ec with FFmpeg and identify its streams. 86 * @a io->ec and @a io->deadline must already be set. 87 * 88 * @param[in,out] io I/O state to complete 89 * @return 0 on success 90 */ 91 int 92 EXTRACTOR_ffmpeg_open_ (struct EXTRACTOR_FFmpegIO *io); 93 94 95 /** 96 * Close what EXTRACTOR_ffmpeg_open_() opened. 97 * 98 * @param[in,out] io I/O state to clean up 99 */ 100 void 101 EXTRACTOR_ffmpeg_close_ (struct EXTRACTOR_FFmpegIO *io); 102 103 104 /** 105 * Close @a io, rewind the extraction context and open it again, so that 106 * the next av_read_frame() starts at the beginning of the file with 107 * fresh demuxer state (and no stream discarded). 108 * 109 * @param[in,out] io I/O state to reopen 110 * @return 0 on success; on failure @a io is closed 111 */ 112 int 113 EXTRACTOR_ffmpeg_reopen_ (struct EXTRACTOR_FFmpegIO *io); 114 115 116 /** 117 * Find the best stream of @a type and open a decoder for it. 118 * 119 * @param io open I/O state 120 * @param type media type to look for 121 * @param[out] ctx set to the open decoder 122 * @return index of the stream, or a negative value on error 123 */ 124 int 125 EXTRACTOR_ffmpeg_open_stream_ (struct EXTRACTOR_FFmpegIO *io, 126 enum AVMediaType type, 127 AVCodecContext **ctx); 128 129 130 /** 131 * Determine where in the media we should sample, honouring both the 132 * requested offset and the (possibly unknown) duration, and seek there. 133 * 134 * @param io open I/O state 135 * @param stream stream to seek in 136 * @param ctx decoder to flush after seeking 137 * @param offset desired position in seconds 138 * @return the position we actually aim for, in seconds; 0 if we could 139 * not seek and the caller should just decode from the start 140 */ 141 double 142 EXTRACTOR_ffmpeg_seek_ (struct EXTRACTOR_FFmpegIO *io, 143 int stream, 144 AVCodecContext *ctx, 145 unsigned int offset); 146 147 148 /** 149 * Suppress FFmpeg's logging. 150 */ 151 void 152 EXTRACTOR_ffmpeg_silence_ (void); 153 154 #endif 155 156 /* end of mediaffmpeg.h */