| FFmpegKit Linux API 6.0

Loading...
Searching...
No Matches
fftools_ffmpeg_mux.h
Go to the documentation of this file.
1/*
2 * Muxer internal APIs - should not be included outside of ffmpeg_mux*
3 * Copyright (c) 2023 ARTHENICA LTD
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/*
23 * This file is the modified version of ffmpeg_mux.h file living in ffmpeg source code under the fftools folder. We
24 * manually update it each time we depend on a new ffmpeg version. Below you can see the list of changes applied
25 * by us to develop ffmpeg-kit library.
26 *
27 * ffmpeg-kit changes by ARTHENICA LTD
28 *
29 * 07.2023
30 * --------------------------------------------------------
31 * - FFmpeg 6.0 changes migrated
32 * - fftools header names updated
33 * - want_sdp made thread-local
34 * - EncStatsFile declaration migrated from ffmpeg_mux_init.c
35 * - WARN_MULTIPLE_OPT_USAGE, MATCH_PER_STREAM_OPT, MATCH_PER_TYPE_OPT, SPECIFIER_OPT_FMT declarations migrated from
36 * ffmpeg.h
37 * - ms_from_ost migrated to ffmpeg_mux.c
38 */
39
40#ifndef FFTOOLS_FFMPEG_MUX_H
41#define FFTOOLS_FFMPEG_MUX_H
42
43#include <stdatomic.h>
44#include <stdint.h>
45
46#include "fftools_thread_queue.h"
47
48#include "libavformat/avformat.h"
49
50#include "libavcodec/packet.h"
51
52#include "libavutil/dict.h"
53#include "libavutil/fifo.h"
54#include "libavutil/thread.h"
55
56#define SPECIFIER_OPT_FMT_str "%s"
57#define SPECIFIER_OPT_FMT_i "%i"
58#define SPECIFIER_OPT_FMT_i64 "%"PRId64
59#define SPECIFIER_OPT_FMT_ui64 "%"PRIu64
60#define SPECIFIER_OPT_FMT_f "%f"
61#define SPECIFIER_OPT_FMT_dbl "%lf"
62
63#define WARN_MULTIPLE_OPT_USAGE(name, type, so, st)\
64{\
65 char namestr[128] = "";\
66 const char *spec = so->specifier && so->specifier[0] ? so->specifier : "";\
67 for (int _i = 0; opt_name_##name[_i]; _i++)\
68 av_strlcatf(namestr, sizeof(namestr), "-%s%s", opt_name_##name[_i], opt_name_##name[_i+1] ? (opt_name_##name[_i+2] ? ", " : " or ") : "");\
69 av_log(NULL, AV_LOG_WARNING, "Multiple %s options specified for stream %d, only the last option '-%s%s%s "SPECIFIER_OPT_FMT_##type"' will be used.\n",\
70 namestr, st->index, opt_name_##name[0], spec[0] ? ":" : "", spec, so->u.type);\
71}
72
73#define MATCH_PER_STREAM_OPT(name, type, outvar, fmtctx, st)\
74{\
75 int _ret, _matches = 0;\
76 SpecifierOpt *so;\
77 for (int _i = 0; _i < o->nb_ ## name; _i++) {\
78 char *spec = o->name[_i].specifier;\
79 if ((_ret = check_stream_specifier(fmtctx, st, spec)) > 0) {\
80 outvar = o->name[_i].u.type;\
81 so = &o->name[_i];\
82 _matches++;\
83 } else if (_ret < 0)\
84 exit_program(1);\
85 }\
86 if (_matches > 1)\
87 WARN_MULTIPLE_OPT_USAGE(name, type, so, st);\
88}
89
90#define MATCH_PER_TYPE_OPT(name, type, outvar, fmtctx, mediatype)\
91{\
92 int i;\
93 for (i = 0; i < o->nb_ ## name; i++) {\
94 char *spec = o->name[i].specifier;\
95 if (!strcmp(spec, mediatype))\
96 outvar = o->name[i].u.type;\
97 }\
98}
99
100 typedef struct MuxStream {
101 OutputStream ost;
102
103 // name used for logging
104 char log_name[32];
105
106 /* the packets are buffered here until the muxer is ready to be initialized */
107 AVFifo *muxing_queue;
108
109 AVBSFContext *bsf_ctx;
110
111 EncStats stats;
112
113 int64_t max_frames;
114
115 /*
116 * The size of the AVPackets' buffers in queue.
117 * Updated when a packet is either pushed or pulled from the queue.
118 */
119 size_t muxing_queue_data_size;
120
121 int max_muxing_queue_size;
122
123 /* Threshold after which max_muxing_queue_size will be in effect */
124 size_t muxing_queue_data_threshold;
125
126 /* dts of the last packet sent to the muxer, in the stream timebase
127 * used for making up missing dts values */
128 int64_t last_mux_dts;
129} MuxStream;
130
131 typedef struct Muxer {
132 OutputFile of;
133
134 // name used for logging
135 char log_name[32];
136
137 AVFormatContext *fc;
138
139 pthread_t thread;
140 ThreadQueue *tq;
141
142 AVDictionary *opts;
143
144 int thread_queue_size;
145
146 /* filesize limit expressed in bytes */
147 int64_t limit_filesize;
148 atomic_int_least64_t last_filesize;
149 int header_written;
150
151 SyncQueue *sq_mux;
152 AVPacket *sq_pkt;
153} Muxer;
154
155 typedef struct EncStatsFile {
156 char *path;
157 AVIOContext *io;
158} EncStatsFile;
159
160/* whether we want to print an SDP, set in of_open() */
161 extern __thread int want_sdp;
162
163 int mux_check_init(Muxer *mux);
164
165#endif /* FFTOOLS_FFMPEG_MUX_H */
mux_check_init
int mux_check_init(Muxer *mux)
Definition fftools_ffmpeg_mux.c:536
want_sdp
__thread int want_sdp
Definition fftools_ffmpeg_mux.c:66
fftools_thread_queue.h
EncStatsFile
Definition fftools_ffmpeg_mux.h:155
EncStatsFile::io
AVIOContext * io
Definition fftools_ffmpeg_mux.h:157
EncStatsFile::path
char * path
Definition fftools_ffmpeg_mux.h:156
EncStats
Definition fftools_ffmpeg.h:576
MuxStream
Definition fftools_ffmpeg_mux.h:100
MuxStream::ost
OutputStream ost
Definition fftools_ffmpeg_mux.h:101
MuxStream::log_name
char log_name[32]
Definition fftools_ffmpeg_mux.h:104
MuxStream::bsf_ctx
AVBSFContext * bsf_ctx
Definition fftools_ffmpeg_mux.h:109
MuxStream::max_frames
int64_t max_frames
Definition fftools_ffmpeg_mux.h:113
MuxStream::muxing_queue
AVFifo * muxing_queue
Definition fftools_ffmpeg_mux.h:107
MuxStream::max_muxing_queue_size
int max_muxing_queue_size
Definition fftools_ffmpeg_mux.h:121
MuxStream::muxing_queue_data_size
size_t muxing_queue_data_size
Definition fftools_ffmpeg_mux.h:119
MuxStream::muxing_queue_data_threshold
size_t muxing_queue_data_threshold
Definition fftools_ffmpeg_mux.h:124
MuxStream::last_mux_dts
int64_t last_mux_dts
Definition fftools_ffmpeg_mux.h:128
MuxStream::stats
EncStats stats
Definition fftools_ffmpeg_mux.h:111
Muxer
Definition fftools_ffmpeg_mux.h:131
Muxer::opts
AVDictionary * opts
Definition fftools_ffmpeg_mux.h:142
Muxer::of
OutputFile of
Definition fftools_ffmpeg_mux.h:132
Muxer::thread_queue_size
int thread_queue_size
Definition fftools_ffmpeg_mux.h:144
Muxer::last_filesize
atomic_int_least64_t last_filesize
Definition fftools_ffmpeg_mux.h:148
Muxer::sq_pkt
AVPacket * sq_pkt
Definition fftools_ffmpeg_mux.h:152
Muxer::header_written
int header_written
Definition fftools_ffmpeg_mux.h:149
Muxer::tq
ThreadQueue * tq
Definition fftools_ffmpeg_mux.h:140
Muxer::log_name
char log_name[32]
Definition fftools_ffmpeg_mux.h:135
Muxer::sq_mux
SyncQueue * sq_mux
Definition fftools_ffmpeg_mux.h:151
Muxer::fc
AVFormatContext * fc
Definition fftools_ffmpeg_mux.h:137
Muxer::thread
pthread_t thread
Definition fftools_ffmpeg_mux.h:139
Muxer::limit_filesize
int64_t limit_filesize
Definition fftools_ffmpeg_mux.h:147
OutputFile
Definition fftools_ffmpeg.h:741
OutputStream
Definition fftools_ffmpeg.h:611
SyncQueue
Definition fftools_sync_queue.c:59
ThreadQueue
Definition fftools_thread_queue.c:56