libextractor

GNU libextractor
Log | Files | Refs | Submodules | README | LICENSE

mediaffmpeg.h (3942B)


      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  * Find the best stream of @a type and open a decoder for it.
    106  *
    107  * @param io open I/O state
    108  * @param type media type to look for
    109  * @param[out] ctx set to the open decoder
    110  * @return index of the stream, or a negative value on error
    111  */
    112 int
    113 EXTRACTOR_ffmpeg_open_stream_ (struct EXTRACTOR_FFmpegIO *io,
    114                                enum AVMediaType type,
    115                                AVCodecContext **ctx);
    116 
    117 
    118 /**
    119  * Determine where in the media we should sample, honouring both the
    120  * requested offset and the (possibly unknown) duration, and seek there.
    121  *
    122  * @param io open I/O state
    123  * @param stream stream to seek in
    124  * @param ctx decoder to flush after seeking
    125  * @param offset desired position in seconds
    126  * @return the position we actually aim for, in seconds; 0 if we could
    127  *         not seek and the caller should just decode from the start
    128  */
    129 double
    130 EXTRACTOR_ffmpeg_seek_ (struct EXTRACTOR_FFmpegIO *io,
    131                         int stream,
    132                         AVCodecContext *ctx,
    133                         unsigned int offset);
    134 
    135 
    136 /**
    137  * Suppress FFmpeg's logging.
    138  */
    139 void
    140 EXTRACTOR_ffmpeg_silence_ (void);
    141 
    142 #endif
    143 
    144 /* end of mediaffmpeg.h */