]> git.xonotic.org Git - xonotic/darkplaces.git/blob - av_backend_libav.c
sv_main: Add campaign cvar. Set 1 if not coop or deathmatch (Quake rerelease compatib...
[xonotic/darkplaces.git] / av_backend_libav.c
1 /*
2 Copyright (C) 2020 David Knapp (Cloudwalk)
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20
21 #include "darkplaces.h"
22
23 #include "libavcodec/avcodec.h"
24 #include "libavformat/avformat.h"
25
26 // Decoding functions
27 AVCodec                    *(*qavcodec_find_decoder)                            (enum AVCodecID id);
28 AVCodec                    *(*qavcodec_find_decoder_by_name)                    (const char *name);
29 int                         (*qavcodec_default_get_buffer2)                     (AVCodecContext *s, AVFrame *frame, int flags);
30 void                        (*qavcodec_align_dimensions)                        (AVCodecContext *s, int *width, int *height);
31 void                        (*qavcodec_align_dimensions2)                       (AVCodecContext *s, int *width, int *height, int linesize_align[AV_NUM_DATA_POINTERS]);
32 int                         (*qavcodec_enum_to_chroma_pos)                      (int *xpos, int *ypos, enum AVChromaLocation pos);
33 enum AVChromaLocation       (*qavcodec_chroma_pos_to_enum)                      (int xpos, int ypos);
34 int                         (*qavcodec_send_packet)                             (AVCodecContext *avctx, const AVPacket *avpkt);
35 int                         (*qavcodec_receive_frame)                           (AVCodecContext *avctx, AVFrame *frame);
36 int                         (*qavcodec_send_frame)                              (AVCodecContext *avctx, const AVFrame *frame);
37 int                         (*qavcodec_receive_packet)                          (AVCodecContext *avctx, AVPacket *avpkt);
38 int                         (*qavcodec_get_hw_frames_parameters)                (AVCodecContext *avctx, AVBufferRef *device_ref, enum AVPixelFormat hw_pix_fmt, AVBufferRef **out_frames_ref);
39
40 // Encoding functions
41 AVCodec                    *(*qavcodec_find_encoder)                            (enum AVCodecID id);
42 AVCodec                    *(*qavcodec_find_encoder_by_name)                    (const char *name);
43 int                         (*qavcodec_encode_subtitle)                         (AVCodecContext *avctx, uint8_t *buf, int buf_size, const AVSubtitle *sub);
44
45 // Core functions
46 const AVCodecHWConfig      *(*qavcodec_get_hw_config)                           (const AVCodec *codec, int index);
47 const AVCodec              *(*qav_codec_iterate)                                (void **opaque);
48 unsigned                    (*qavcodec_version)                                 (void);
49 const char                 *(*qavcodec_configuration)                           (void);
50 const char                 *(*qavcodec_license)                                 (void);
51 AVCodecContext             *(*qavcodec_alloc_context3)                          (const AVCodec *codec);
52 void                        (*qavcodec_free_context)                            (AVCodecContext **avctx);
53 int                         (*qavcodec_get_context_defaults3)                   (AVCodecContext *s, const AVCodec *codec);
54 const AVClass              *(*qavcodec_get_class)                               (void);
55 const AVClass              *(*qavcodec_get_frame_class)                         (void);
56 const AVClass              *(*qavcodec_get_subtitle_rect_class)                 (void);
57 AVCodecParameters          *(*qavcodec_parameters_alloc)                        (void);
58 void                        (*qavcodec_parameters_free)                         (AVCodecParameters **par);
59 int                         (*qavcodec_parameters_copy)                         (AVCodecParameters *dst, const AVCodecParameters *src);
60 int                         (*qavcodec_parameters_from_context)                 (AVCodecParameters *par, const AVCodecContext *codec);
61 int                         (*qavcodec_parameters_to_context)                   (AVCodecContext *codec, const AVCodecParameters *par);
62 int                         (*qavcodec_open2)                                   (AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options);
63 int                         (*qavcodec_close)                                   (AVCodecContext *avctx);
64 void                        (*qavsubtitle_free)                                 (AVSubtitle *sub);
65
66 // Demuxing functions
67 AVInputFormat              *(*qav_find_input_format)                            (const char *short_name);
68 AVInputFormat              *(*qav_probe_input_format)                           (AVProbeData *pd, int is_opened);
69 AVInputFormat              *(*qav_probe_input_format2)                          (AVProbeData *pd, int is_opened, int *score_max);
70 AVInputFormat              *(*qav_probe_input_format3)                          (AVProbeData *pd, int is_opened, int *score_ret);
71 int                         (*qav_probe_input_buffer2)                          (AVIOContext *pb, AVInputFormat **fmt, const char *url, void *logctx, unsigned int offset, unsigned int max_probe_size);
72 int                         (*qav_probe_input_buffer)                           (AVIOContext *pb, AVInputFormat **fmt, const char *url, void *logctx, unsigned int offset, unsigned int max_probe_size);
73 int                         (*qavformat_open_input)                             (AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options);
74 int                         (*qavformat_find_stream_info)                       (AVFormatContext *ic, AVDictionary **options);
75 AVProgram                  *(*qav_find_program_from_stream)                     (AVFormatContext *ic, AVProgram *last, int s);
76 void                        (*qav_program_add_stream_index)                     (AVFormatContext *ac, int progid, unsigned int idx);
77 int                         (*qav_find_best_stream)                             (AVFormatContext *ic, enum AVMediaType type, int wanted_stream_nb, int related_stream, AVCodec **decoder_ret, int flags);
78 int                         (*qav_read_frame)                                   (AVFormatContext *s, AVPacket *pkt);
79 int                         (*qav_seek_frame)                                   (AVFormatContext *s, int stream_index, int64_t timestamp, int flags);
80 int                         (*qavformat_seek_file)                              (AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags);
81 int                         (*qavformat_flush)                                  (AVFormatContext *s);
82 int                         (*qav_read_play)                                    (AVFormatContext *s);
83 int                         (*qav_read_pause)                                   (AVFormatContext *s);
84 void                        (*qavformat_close_input)                            (AVFormatContext **s);
85
86 // Muxing functions
87 av_warn_unused_result int   (*qavformat_write_header)                           (AVFormatContext *s, AVDictionary **options);
88 av_warn_unused_result int   (*qavformat_init_output)                            (AVFormatContext *s, AVDictionary **options);
89 int                         (*qav_write_frame)                                  (AVFormatContext *s, AVPacket *pkt);
90 int                         (*qav_interleaved_write_frame)                      (AVFormatContext *s, AVPacket *pkt);
91 int                         (*qav_write_uncoded_frame)                          (AVFormatContext *s, int stream_index, AVFrame *frame);
92 int                         (*qav_interleaved_write_uncoded_frame)              (AVFormatContext *s, int stream_index, AVFrame *frame);
93 int                         (*qav_write_uncoded_frame_query)                    (AVFormatContext *s, int stream_index);
94 int                         (*qav_write_trailer)                                (AVFormatContext *s);
95 AVOutputFormat             *(*qav_guess_format)                                 (const char *short_name, const char *filename, const char *mime_type);
96 enum AVCodecID              (*qav_guess_codec)                                  (AVOutputFormat *fmt, const char *short_name, const char *filename, const char *mime_type, enum AVMediaType type);
97 int                         (*qav_get_output_timestamp)                         (struct AVFormatContext *s, int stream, int64_t *dts, int64_t *wall);
98
99 // Core functions
100 unsigned                    (*qavformat_version)                                (void);
101 const char                 *(*qavformat_configuration)                          (void);
102 const char                 *(*qavformat_license)                                (void);
103 void                        (*qav_register_all)                                 (void);
104 void                        (*qav_register_input_format)                        (AVInputFormat *format);
105 void                        (*qav_register_output_format)                       (AVOutputFormat *format);
106 int                         (*qavformat_network_init)                           (void);
107 int                         (*qavformat_network_deinit)                         (void);
108 AVInputFormat              *(*qav_iformat_next)                                 (const AVInputFormat *f);
109 AVOutputFormat             *(*qav_oformat_next)                                 (const AVOutputFormat *f);
110 AVFormatContext            *(*qavformat_alloc_context)                          (void);
111 void                        (*qavformat_free_context)                           (AVFormatContext *s);
112 const AVClass              *(*qavformat_get_class)                              (void);
113 AVStream                   *(*qavformat_new_stream)                             (AVFormatContext *s, const AVCodec *c);
114 uint8_t                    *(*qav_stream_new_side_data)                         (AVStream *stream, enum AVPacketSideDataType type, int size);
115 uint8_t                    *(*qav_stream_get_side_data)                         (const AVStream *stream, enum AVPacketSideDataType type, int *size);
116 AVProgram                  *(*qav_new_program)                                  (AVFormatContext *s, int id);
117
118 // Utility functions
119 void                        (*qav_hex_dump)                                     (FILE *f, const uint8_t *buf, int size);
120 void                        (*qav_hex_dump_log)                                 (void *avcl, int level, const uint8_t *buf, int size);
121 void                        (*qav_pkt_dump2)                                    (FILE *f, const AVPacket *pkt, int dump_payload, const AVStream *st);
122 void                        (*qav_pkt_dump_log2)                                (void *avcl, int level, const AVPacket *pkt, int dump_payload, const AVStream *st);
123 enum AVCodecID              (*qav_codec_get_id)                                 (const struct AVCodecTag *const *tags, unsigned int tag);
124 unsigned int                (*qav_codec_get_tag)                                (const struct AVCodecTag *const *tags, enum AVCodecID id);
125 int                         (*qav_codec_get_tag2)                               (const struct AVCodecTag *const *tags, enum AVCodecID id, unsigned int *tag);
126 int                         (*qav_find_default_stream_index)                    (AVFormatContext *s);
127 int                         (*qav_index_search_timestamp)                       (AVStream *st, int64_t timestamp, int flags);
128 int                         (*qav_add_index_entry)                              (AVStream *st, int64_t pos, int64_t timestamp, int size, int distance, int flags);
129 void                        (*qav_url_split)                                    (char *proto, int proto_size, char *authorization, int authorization_size, char *hostname, int hostname_size, int *port_ptr, char *path, int path_size, const char *url);
130 void                        (*qav_dump_format)                                  (AVFormatContext *ic, int index, const char *url, int is_output);
131 int                         (*qav_get_frame_filename2)                          (char *buf, int buf_size, const char *path, int number, int flags);
132 int                         (*qav_get_frame_filename)                           (char *buf, int buf_size, const char *path, int number);
133 int                         (*qav_filename_number_test)                         (const char *filename);
134 int                         (*qav_sdp_create)                                   (AVFormatContext *ac[], int n_files, char *buf, int size);
135 int                         (*qav_match_ext)                                    (const char *filename, const char *extensions);
136 int                         (*qavformat_query_codec)                            (const AVOutputFormat *ofmt, enum AVCodecID codec_id, int std_compliance);
137 AVRational                  (*qav_guess_sample_aspect_ratio)                    (AVFormatContext *format, AVStream *stream, AVFrame *frame);
138 AVRational                  (*qav_guess_frame_rate)                             (AVFormatContext *ctx, AVStream *stream, AVFrame *frame);
139 int                         (*qavformat_match_stream_specifier)                 (AVFormatContext *s, AVStream *st, const char *spec);
140 int                         (*qavformat_queue_attached_pictures)                (AVFormatContext *s);
141 int                         (*qavformat_transfer_internal_stream_timing_info)   (const AVOutputFormat *ofmt, AVStream *ost, const AVStream *ist, enum AVTimebaseSource copy_tb);
142 AVRational                  (*qav_stream_get_codec_timebase)                    (const AVStream *st);
143
144 static dllfunction_t libavcodecfuncs[] =
145 {
146         {"avcodec_find_decoder",                             (void **) &qavcodec_find_decoder},
147         {"avcodec_find_decoder_by_name",                     (void **) &qavcodec_find_decoder_by_name},
148         {"avcodec_default_get_buffer2",                      (void **) &qavcodec_default_get_buffer2},
149         {"avcodec_align_dimensions",                         (void **) &qavcodec_align_dimensions},
150         {"avcodec_align_dimensions2",                        (void **) &qavcodec_align_dimensions},
151         {"avcodec_enum_to_chroma_pos",                       (void **) &qavcodec_enum_to_chroma_pos},
152         {"avcodec_chroma_pos_to_enum",                       (void **) &qavcodec_chroma_pos_to_enum},
153         {"avcodec_send_packet",                              (void **) &qavcodec_send_packet},
154         {"avcodec_receive_frame",                            (void **) &qavcodec_receive_frame},
155         {"avcodec_send_frame",                               (void **) &qavcodec_send_frame},
156         {"avcodec_receive_packet",                           (void **) &qavcodec_receive_packet},
157         {"avcodec_get_hw_frames_parameters",                 (void **) &qavcodec_get_hw_frames_parameters},
158         {"avcodec_get_hw_config",                            (void **) &qavcodec_get_hw_config},
159         {"av_codec_iterate",                                 (void **) &qav_codec_iterate},
160         {"avcodec_version",                                  (void **) &qavcodec_version},
161         {"avcodec_configuration",                            (void **) &qavcodec_configuration},
162         {"avcodec_license",                                  (void **) &qavcodec_license},
163         {"avcodec_alloc_context3",                           (void **) &qavcodec_alloc_context3},
164         {"avcodec_free_context",                             (void **) &qavcodec_free_context},
165         {"avcodec_get_context_defaults3",                    (void **) &qavcodec_get_context_defaults3},
166         {"avcodec_get_class",                                (void **) &qavcodec_get_class},
167         {"avcodec_get_frame_class",                          (void **) &qavcodec_get_frame_class},
168         {"avcodec_get_subtitle_rect_class",                  (void **) &qavcodec_get_subtitle_rect_class},
169         {"avcodec_parameters_alloc",                         (void **) &qavcodec_parameters_alloc},
170         {"avcodec_parameters_free",                          (void **) &qavcodec_parameters_free},
171         {"avcodec_parameters_copy",                          (void **) &qavcodec_parameters_copy},
172         {"avcodec_parameters_from_context",                  (void **) &qavcodec_parameters_from_context},
173         {"avcodec_parameters_to_context",                    (void **) &qavcodec_parameters_to_context},
174         {"avcodec_open2",                                    (void **) &qavcodec_open2},
175         {"avcodec_close",                                    (void **) &qavcodec_close},
176         {"avsubtitle_free",                                  (void **) &qavsubtitle_free},
177         {NULL, NULL}
178 };
179
180 static dllfunction_t libavformatfuncs[] =
181 {
182         {"av_find_input_format",                             (void **) &qav_find_input_format},
183         {"av_probe_input_format",                            (void **) &qav_probe_input_format},
184         {"av_probe_input_format2",                           (void **) &qav_probe_input_format2},
185         {"av_probe_input_format3",                           (void **) &qav_probe_input_format3},
186         {"av_probe_input_buffer2",                           (void **) &qav_probe_input_buffer2},
187         {"av_probe_input_buffer",                            (void **) &qav_probe_input_buffer},
188         {"avformat_open_input",                              (void **) &qavformat_open_input},
189         {"avformat_find_stream_info",                        (void **) &qavformat_find_stream_info},
190         {"av_find_program_from_stream",                      (void **) &qav_find_program_from_stream},
191         {"av_program_add_stream_index",                      (void **) &qav_program_add_stream_index},
192         {"av_find_best_stream",                              (void **) &qav_find_best_stream},
193         {"av_read_frame",                                    (void **) &qav_read_frame},
194         {"av_seek_frame",                                    (void **) &qav_seek_frame},
195         {"avformat_seek_file",                               (void **) &qavformat_seek_file},
196         {"avformat_flush",                                   (void **) &qavformat_flush},
197         {"av_read_play",                                     (void **) &qav_read_play},
198         {"av_read_pause",                                    (void **) &qav_read_pause},
199         {"avformat_close_input",                             (void **) &qavformat_close_input},
200         {"avformat_write_header",                            (void **) &qavformat_write_header},
201         {"avformat_init_output",                             (void **) &qavformat_init_output},
202         {"av_write_frame",                                   (void **) &qav_write_frame},
203         {"av_interleaved_write_frame",                       (void **) &qav_interleaved_write_frame},
204         {"av_write_uncoded_frame",                           (void **) &qav_write_uncoded_frame},
205         {"av_interleaved_write_uncoded_frame",               (void **) &qav_interleaved_write_uncoded_frame},
206         {"av_write_uncoded_frame_query",                     (void **) &qav_write_uncoded_frame_query},
207         {"av_write_trailer",                                 (void **) &qav_write_trailer},
208         {"av_guess_format",                                  (void **) &qav_guess_format},
209         {"av_guess_codec",                                   (void **) &qav_guess_codec},
210         {"av_get_output_timestamp",                          (void **) &qav_get_output_timestamp},
211         {"avformat_version",                                 (void **) &qavformat_version},
212         {"avformat_configuration",                           (void **) &qavformat_configuration},
213         {"avformat_license",                                 (void **) &qavformat_license},
214         {"av_register_all",                                  (void **) &qav_register_all},
215         {"av_register_input_format",                         (void **) &qav_register_input_format},
216         {"av_register_output_format",                        (void **) &qav_register_output_format},
217         {"avformat_network_init",                            (void **) &qavformat_network_init},
218         {"avformat_network_deinit",                          (void **) &qavformat_network_deinit},
219         {"av_iformat_next",                                  (void **) &qav_iformat_next},
220         {"av_oformat_next",                                  (void **) &qav_oformat_next},
221         {"avformat_alloc_context",                           (void **) &qavformat_alloc_context},
222         {"avformat_free_context",                            (void **) &qavformat_free_context},
223         {"avformat_get_class",                               (void **) &qavformat_get_class},
224         {"avformat_new_stream",                              (void **) &qavformat_new_stream},
225         {"av_stream_new_side_data",                          (void **) &qav_stream_new_side_data},
226         {"av_stream_get_side_data",                          (void **) &qav_stream_get_side_data},
227         {"av_new_program",                                   (void **) &qav_new_program},
228         {"av_hex_dump",                                      (void **) &qav_hex_dump},
229         {"av_hex_dump_log",                                  (void **) &qav_hex_dump_log},
230         {"av_pkt_dump2",                                     (void **) &qav_pkt_dump2},
231         {"av_pkt_dump_log2",                                 (void **) &qav_pkt_dump_log2},
232         {"av_codec_get_id",                                  (void **) &qav_codec_get_id},
233         {"av_codec_get_tag",                                 (void **) &qav_codec_get_tag},
234         {"av_codec_get_tag2",                                (void **) &qav_codec_get_tag2},
235         {"av_find_default_stream_index",                     (void **) &qav_find_default_stream_index},
236         {"av_index_search_timestamp",                        (void **) &qav_index_search_timestamp},
237         {"av_add_index_entry",                               (void **) &qav_add_index_entry},
238         {"av_url_split",                                     (void **) &qav_url_split},
239         {"av_dump_format",                                   (void **) &qav_dump_format},
240         {"av_get_frame_filename2",                           (void **) &qav_get_frame_filename2},
241         {"av_get_frame_filename",                            (void **) &qav_get_frame_filename},
242         {"av_filename_number_test",                          (void **) &qav_filename_number_test},
243         {"av_sdp_create",                                    (void **) &qav_sdp_create},
244         {"av_match_ext",                                     (void **) &qav_match_ext},
245         {"avformat_query_codec",                             (void **) &qavformat_query_codec},
246         {"av_guess_sample_aspect_ratio",                     (void **) &qav_guess_sample_aspect_ratio},
247         {"av_guess_frame_rate",                              (void **) &qav_guess_frame_rate},
248         {"avformat_match_stream_specifier",                  (void **) &qavformat_match_stream_specifier},
249         {"avformat_queue_attached_pictures",                 (void **) &qavformat_queue_attached_pictures},
250         {"avformat_transfer_internal_stream_timing_info",    (void **) &qavformat_transfer_internal_stream_timing_info},
251         {"av_stream_get_codec_timebase",                     (void **) &qav_stream_get_codec_timebase},
252         {NULL, NULL}
253 };
254
255 static dllhandle_t libavcodec_dll = NULL;
256 static dllhandle_t libavformat_dll = NULL;
257
258 qbool LibAV_LoadLibrary(void)
259 {
260         const char* dllnames_libavcodec [] =
261         {
262 #if defined(WIN32)
263                 "libavcodec.dll",
264 #elif defined(MACOSX)
265                 "libavcodec.dylib",
266 #else
267                 "libavcodec.so",
268 #endif
269                 NULL
270         };
271
272         const char* dllnames_libavformat [] =
273         {
274 #if defined(WIN32)
275                 "libavformat.dll",
276 #elif defined(MACOSX)
277                 "libavformat.dylib",
278 #else
279                 "libavformat.so",
280 #endif
281                 NULL
282         };
283
284         if (libavcodec_dll && libavformat_dll) // Already loaded?
285                 return true;
286
287 // COMMANDLINEOPTION: -nolibav disables libav support
288         if (Sys_CheckParm("-nolibav"))
289                 return false;
290
291         // Load the DLL
292         if (Sys_LoadDependency (dllnames_libavcodec, &libavcodec_dll, libavcodecfuncs))
293         {
294                 if(Sys_LoadDependency(dllnames_libavformat, &libavformat_dll, libavformatfuncs))
295                         return true;
296                 Con_Printf(CON_ERROR "Failed to load the libavformat library. Cannot use libavcodec without it\n");
297                 Sys_FreeLibrary(&libavcodec_dll);
298         }
299
300         return false;
301 }