Audacity  2.2.2
FFmpeg.h
Go to the documentation of this file.
1 /**********************************************************************
2 
3 Audacity: A Digital Audio Editor
4 
5 FFmpeg.h
6 
7 Audacity(R) is copyright (c) 1999-2009 Audacity Team.
8 License: GPL v2. See License.txt.
9 
10 ******************************************************************//*******************************************************************/
15 
16 #if !defined(__AUDACITY_FFMPEG__)
17 #define __AUDACITY_FFMPEG__
18 
19 #include "MemoryX.h"
20 
21 #include "Internat.h"
22 
23 // TODO: Determine whether the libav* headers come from the FFmpeg or libav
24 // project and set IS_FFMPEG_PROJECT depending on it.
25 #define IS_FFMPEG_PROJECT 1
26 
27 /* FFmpeg is written in C99. It uses many types from stdint.h. Because we are
28  * compiling this using a C++ compiler we have to put it in extern "C".
29  * __STDC_CONSTANT_MACROS is defined to make <stdint.h> behave like it
30  * is actually being compiled with a C99 compiler.
31  *
32  * The symptoms are that INT64_C is not a valid type, which tends to break
33  * somewhere down in the implementations using this file */
34 
35 /* In order to be able to compile this file when ffmpeg is not available we
36  * need access to the value of USE_FFMPEG, which means config*.h needs to come
37  * in before this file. The suggest way to achieve this is by including
38  * Audacity.h */
39 
40 #if defined(USE_FFMPEG)
41 extern "C" {
42  // Include errno.h before the ffmpeg includes since they depend on
43  // knowing the value of EINVAL...see bottom of avcodec.h. Not doing
44  // so will produce positive error returns when they should be < 0.
45  #include <errno.h>
46 
47  #include <libavcodec/avcodec.h>
48  #include <libavformat/avformat.h>
49  #include <libavutil/fifo.h>
50  #include <libavutil/mathematics.h>
51 
52  #if defined(DISABLE_DYNAMIC_LOADING_FFMPEG)
53  #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55, 45, 101)
54  #define av_frame_alloc avcodec_alloc_frame
55  #define av_frame_free avcodec_free_frame
56  #endif
57 
58  #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54, 59, 100)
59  inline void avcodec_free_frame(AVFrame **frame) {
60  av_free(*frame);
61  }
62  #endif
63 
64  #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54, 51, 100)
65  #define AVCodecID CodecID
66  #define AV_CODEC_ID_AAC CODEC_ID_AAC
67  #define AV_CODEC_ID_AC CODEC_ID_AC
68  #define AV_CODEC_ID_AC3 CODEC_ID_AC3
69  #define AV_CODEC_ID_ADPCM CODEC_ID_ADPCM
70  #define AV_CODEC_ID_ADPCM_CT CODEC_ID_ADPCM_CT
71  #define AV_CODEC_ID_ADPCM_G726 CODEC_ID_ADPCM_G726
72  #define AV_CODEC_ID_ADPCM_IMA_QT CODEC_ID_ADPCM_IMA_QT
73  #define AV_CODEC_ID_ADPCM_IMA_WAV CODEC_ID_ADPCM_IMA_WAV
74  #define AV_CODEC_ID_ADPCM_MS CODEC_ID_ADPCM_MS
75  #define AV_CODEC_ID_ADPCM_SWF CODEC_ID_ADPCM_SWF
76  #define AV_CODEC_ID_ADPCM_YAMAHA CODEC_ID_ADPCM_YAMAHA
77  #define AV_CODEC_ID_ALAC CODEC_ID_ALAC
78  #define AV_CODEC_ID_AMR CODEC_ID_AMR
79  #define AV_CODEC_ID_AMR_NB CODEC_ID_AMR_NB
80  #define AV_CODEC_ID_AMR_WB CODEC_ID_AMR_WB
81  #define AV_CODEC_ID_ATRAC CODEC_ID_ATRAC
82  #define AV_CODEC_ID_ATRAC3 CODEC_ID_ATRAC3
83  #define AV_CODEC_ID_DTS CODEC_ID_DTS
84  #define AV_CODEC_ID_DVAUDIO CODEC_ID_DVAUDIO
85  #define AV_CODEC_ID_FLAC CODEC_ID_FLAC
86  #define AV_CODEC_ID_GSM CODEC_ID_GSM
87  #define AV_CODEC_ID_GSM_MS CODEC_ID_GSM_MS
88  #define AV_CODEC_ID_IMC CODEC_ID_IMC
89  #define AV_CODEC_ID_MACE CODEC_ID_MACE
90  #define AV_CODEC_ID_MACE3 CODEC_ID_MACE3
91  #define AV_CODEC_ID_MACE6 CODEC_ID_MACE6
92  #define AV_CODEC_ID_MP CODEC_ID_MP
93  #define AV_CODEC_ID_MP2 CODEC_ID_MP2
94  #define AV_CODEC_ID_MP3 CODEC_ID_MP3
95  #define AV_CODEC_ID_NELLYMOSER CODEC_ID_NELLYMOSER
96  #define AV_CODEC_ID_NONE CODEC_ID_NONE
97  #define AV_CODEC_ID_PCM CODEC_ID_PCM
98  #define AV_CODEC_ID_PCM_ALAW CODEC_ID_PCM_ALAW
99  #define AV_CODEC_ID_PCM_MULAW CODEC_ID_PCM_MULAW
100  #define AV_CODEC_ID_PCM_S16BE CODEC_ID_PCM_S16BE
101  #define AV_CODEC_ID_PCM_S16LE CODEC_ID_PCM_S16LE
102  #define AV_CODEC_ID_PCM_S24BE CODEC_ID_PCM_S24BE
103  #define AV_CODEC_ID_PCM_S24LE CODEC_ID_PCM_S24LE
104  #define AV_CODEC_ID_PCM_S32BE CODEC_ID_PCM_S32BE
105  #define AV_CODEC_ID_PCM_S32LE CODEC_ID_PCM_S32LE
106  #define AV_CODEC_ID_PCM_S8 CODEC_ID_PCM_S8
107  #define AV_CODEC_ID_PCM_U8 CODEC_ID_PCM_U8
108  #define AV_CODEC_ID_QCELP CODEC_ID_QCELP
109  #define AV_CODEC_ID_QDM CODEC_ID_QDM
110  #define AV_CODEC_ID_QDM2 CODEC_ID_QDM2
111  #define AV_CODEC_ID_ROQ CODEC_ID_ROQ
112  #define AV_CODEC_ID_ROQ_DPCM CODEC_ID_ROQ_DPCM
113  #define AV_CODEC_ID_SONIC CODEC_ID_SONIC
114  #define AV_CODEC_ID_SONIC_LS CODEC_ID_SONIC_LS
115  #define AV_CODEC_ID_TRUESPEECH CODEC_ID_TRUESPEECH
116  #define AV_CODEC_ID_VORBIS CODEC_ID_VORBIS
117  #define AV_CODEC_ID_VOXWARE CODEC_ID_VOXWARE
118  #define AV_CODEC_ID_WMAPRO CODEC_ID_WMAPRO
119  #define AV_CODEC_ID_WMAV CODEC_ID_WMAV
120  #define AV_CODEC_ID_WMAV1 CODEC_ID_WMAV1
121  #define AV_CODEC_ID_WMAV2 CODEC_ID_WMAV2
122  #define AV_CODEC_ID_WMAVOICE CODEC_ID_WMAVOICE
123  #endif
124 
125  #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54, 8, 100)
126  inline bool av_codec_is_encoder(AVCodec *codec) {
127  return codec != NULL && (codec->encode != NULL || codec->encode2 != NULL);
128  }
129  #endif
130  #endif
131 
132  #if LIBAVCODEC_VERSION_MAJOR < 58
133  #ifndef AV_CODEC_FLAG_QSCALE
134  #define AV_CODEC_FLAG_QSCALE CODEC_FLAG_QSCALE
135  #endif
136  #ifndef AV_CODEC_FLAG_GLOBAL_HEADER
137  #define AV_CODEC_FLAG_GLOBAL_HEADER CODEC_FLAG_GLOBAL_HEADER
138  #endif
139  #ifndef AV_CODEC_CAP_SMALL_LAST_FRAME
140  #define AV_CODEC_CAP_SMALL_LAST_FRAME CODEC_CAP_SMALL_LAST_FRAME
141  #endif
142  #endif
143 
144 }
145 #endif
146 
147 #include "Audacity.h"
148 #include "Experimental.h"
149 
150 /* rather earlier than normal, but pulls in config*.h and other program stuff
151  * we need for the next bit */
152 #include <wx/string.h>
153 #include <wx/dynlib.h>
154 #include <wx/log.h> // for wxLogNull
155 #include <wx/utils.h>
157 #include "ShuttleGui.h"
158 #include "Prefs.h"
159 #include <wx/checkbox.h>
160 #include <wx/textctrl.h>
161 
162 #include "audacity/Types.h"
163 
164 // if you needed them, any other audacity header files would go here
165 
167 void av_log_wx_callback(void* ptr, int level, const char* fmt, va_list vl);
168 
169 //----------------------------------------------------------------------------
170 // Get FFmpeg library version
171 //----------------------------------------------------------------------------
172 wxString GetFFmpegVersion(wxWindow *parent);
173 
174 /* from here on in, this stuff only applies when ffmpeg is available */
175 #if defined(USE_FFMPEG)
176 
177 //----------------------------------------------------------------------------
178 // Attempt to load and enable/disable FFmpeg at startup
179 //----------------------------------------------------------------------------
180 void FFmpegStartup();
181 
182 bool LoadFFmpeg(bool showerror);
183 
184 
188 class FFmpegNotFoundDialog final : public wxDialogWrapper
189 {
190 public:
191 
192  FFmpegNotFoundDialog(wxWindow *parent)
193  : wxDialogWrapper(parent, wxID_ANY, wxString(_("FFmpeg not found")))
194  {
195  SetName(GetTitle());
196  ShuttleGui S(this, eIsCreating);
197  PopulateOrExchange(S);
198  }
199 
200  void PopulateOrExchange(ShuttleGui & S)
201  {
202  wxString text;
203 
204  S.SetBorder(10);
205  S.StartVerticalLay(true);
206  {
207  S.AddFixedText(_(
208 "Audacity attempted to use FFmpeg to import an audio file,\n\
209 but the libraries were not found.\n\n\
210 To use FFmpeg import, go to Preferences > Libraries\n\
211 to download or locate the FFmpeg libraries."
212  ));
213 
214  int dontShowDlg = 0;
215  gPrefs->Read(wxT("/FFmpeg/NotFoundDontShow"),&dontShowDlg,0);
216  mDontShow = S.AddCheckBox(_("Do not show this warning again"),dontShowDlg ? wxT("true") : wxT("false"));
217 
219  }
220  S.EndVerticalLay();
221 
222  Layout();
223  Fit();
224  SetMinSize(GetSize());
225  Center();
226 
227  return;
228  }
229 
230  void OnOk(wxCommandEvent & WXUNUSED(event))
231  {
232  if (mDontShow->GetValue())
233  {
234  gPrefs->Write(wxT("/FFmpeg/NotFoundDontShow"),1);
235  gPrefs->Flush();
236  }
237  this->EndModal(0);
238  }
239 
240 private:
241 
242  wxCheckBox *mDontShow;
243 
244  DECLARE_EVENT_TABLE()
245 };
246 
252 class FFmpegLibs
253 {
254 public:
255  FFmpegLibs();
256  ~FFmpegLibs();
257 
260  bool FindLibs(wxWindow *parent);
264  bool LoadLibs(wxWindow *parent, bool showerr);
267  bool ValidLibsLoaded();
268 
274  bool InitLibs(const wxString &libpath_codec, bool showerr);
275 
277  void FreeLibs();
278 
281  wxString GetLibraryVersion()
282  {
283  return wxString::Format(wxT("F(%s),C(%s),U(%s)"),mAVFormatVersion,mAVCodecVersion,mAVUtilVersion);
284  }
285 
286 #if defined(__WXMSW__)
287  /* Library names and file filters for Windows only */
288 
289  wxString GetLibraryTypeString()
290  {
291  /* i18n-hint: do not translate avformat. Preserve the computer gibberish.*/
292  return _("Only avformat.dll|*avformat*.dll|Dynamically Linked Libraries (*.dll)|*.dll|All Files|*");
293  }
294 
295  wxString GetLibAVFormatPath()
296  {
297  wxRegKey reg(wxT("HKEY_LOCAL_MACHINE\\Software\\FFmpeg for Audacity"));
298  wxString path;
299 
300  if (reg.Exists()) {
301  reg.QueryValue(wxT("InstallPath"), path);
302  }
303 
304  return path;
305  }
306 
307  wxString GetLibAVFormatName()
308  {
309  return (wxT("avformat-") wxT(AV_STRINGIFY(LIBAVFORMAT_VERSION_MAJOR)) wxT(".dll"));
310  }
311 
312  wxString GetLibAVCodecName()
313  {
314  return (wxT("avcodec-") wxT(AV_STRINGIFY(LIBAVCODEC_VERSION_MAJOR)) wxT(".dll"));
315  }
316 
317  wxString GetLibAVUtilName()
318  {
319  return (wxT("avutil-") wxT(AV_STRINGIFY(LIBAVUTIL_VERSION_MAJOR)) wxT(".dll"));
320  }
321 #elif defined(__WXMAC__)
322  /* Library names and file filters for Mac OS only */
323  wxString GetLibraryTypeString()
324  {
325  return _("Dynamic Libraries (*.dylib)|*.dylib|All Files (*)|*");
326  }
327 
328  wxString GetLibAVFormatPath()
329  {
330  return wxT("/Library/Application Support/audacity/libs");
331  }
332 
333  wxString GetLibAVFormatName()
334  {
335  return (wxT("libavformat.") wxT(AV_STRINGIFY(LIBAVFORMAT_VERSION_MAJOR)) wxT(".dylib"));
336  }
337 
338  wxString GetLibAVCodecName()
339  {
340  return (wxT("libavcodec.") wxT(AV_STRINGIFY(LIBAVCODEC_VERSION_MAJOR)) wxT(".dylib"));
341  }
342 
343  wxString GetLibAVUtilName()
344  {
345  return (wxT("libavutil.") wxT(AV_STRINGIFY(LIBAVUTIL_VERSION_MAJOR)) wxT(".dylib"));
346  }
347 #else
348  /* Library names and file filters for other platforms, basically Linux and
349  * other *nix platforms */
350  wxString GetLibraryTypeString()
351  {
352  return _("Only libavformat.so|libavformat*.so*|Dynamically Linked Libraries (*.so*)|*.so*|All Files (*)|*");
353  }
354 
355  wxString GetLibAVFormatPath()
356  {
357  return wxT("");
358  }
359 
360  wxString GetLibAVFormatName()
361  {
362  return (wxT("libavformat.so.") wxT(AV_STRINGIFY(LIBAVFORMAT_VERSION_MAJOR)));
363  }
364 
365  wxString GetLibAVCodecName()
366  {
367  return (wxT("libavcodec.so.") wxT(AV_STRINGIFY(LIBAVCODEC_VERSION_MAJOR)));
368  }
369 
370  wxString GetLibAVUtilName()
371  {
372  return (wxT("libavutil.so.") wxT(AV_STRINGIFY(LIBAVUTIL_VERSION_MAJOR)));
373  }
374 #endif // (__WXMAC__) || (__WXMSW__)
375 
379  int refcount;
380 
381 private:
382 
384  wxString mLibAVFormatPath;
385 
387  wxString mAVCodecVersion;
388  wxString mAVFormatVersion;
389  wxString mAVUtilVersion;
390 
392  std::unique_ptr<wxDynamicLibrary> avformat, avcodec, avutil;
393 
395  bool mLibsLoaded;
396 };
397 
401 FFmpegLibs *PickFFmpegLibs();
402 
405 void DropFFmpegLibs();
406 
407 // This object allows access to the AVFormatContext,
408 // and its destructor cleans up memory and file handles
409 struct FFmpegContext {
410  FFmpegContext() {}
411  ~FFmpegContext();
412 
413  AVIOContext *pb{};
414  AVFormatContext *ic_ptr{};
415 };
416 
417 int ufile_fopen(AVIOContext **s, const wxString & name, int flags);
418 int ufile_fopen_input(std::unique_ptr<FFmpegContext> &context_ptr, wxString & name);
419 int ufile_close(AVIOContext *pb);
420 
421 struct streamContext;
422 // common utility functions
423 // utility calls that are shared with ImportFFmpeg and ODDecodeFFmpegTask
424 streamContext *import_ffmpeg_read_next_frame(AVFormatContext* formatContext,
425  streamContext** streams,
426  unsigned int numStreams);
427 
428 int import_ffmpeg_decode_frame(streamContext *sc, bool flushing);
429 
430 #if !defined(DISABLE_DYNAMIC_LOADING_FFMPEG)
431 extern "C" {
432  // A little explanation of what's going on here.
433  //
434  // The FFmpeg function pointers used to be defined in the FFmpegLibs class and all calls would
435  // be done via the global class pointer FFmpegLibsInst. This was fine as long as the prototypes
436  // defined in the class matched the actual functions in the FFmpeg libraries. There was no
437  // compile time validation to prevent invalid function calls. So, what follows is one way of
438  // getting the extra validation.
439  //
440  // Only one source file should define DEFINE_FFMPEG_POINTERS before including ffmpeg.h. This
441  // will cause the compiler to dump all of the function pointers to a single object file and
442  // prevent duplicate symbol definitions during link. This is currently done in ffmpeg.cpp.
443  //
444  // The FFMPEG_FUNCTION_WITH_RETURN and FFMPEG_FUNCTION_NO_RETURN macros do two things each:
445  // 1) Define or reference the variable that contains the actual function pointer
446  // 2) Define an inline function to pass control to the real function
447  //
448  // Since the macros redefine the real ffmpeg functions of the same name, the compiler will
449  // make sure that the definitions are the same. If not, it will complain. For this to occur,
450  // the functions MUST be defined in an extern "C" block otherwise the compiler just thinks the
451  // functions are being overloaded.
452  //
453  // The compiler should optimize away the inline function since it just passes control to the real
454  // function and we should wind up with about the same function call we had before, only now it is
455  // safer due to the validation.
456  //
457  // The FFMPEG_FUNCTION_WITH_RETURN takes 4 arguments:
458  // 1) The return type <---|
459  // 2) The function name | Taken from the FFmpeg funciton prototype
460  // 3) The function arguments <---|
461  // 4) The argument list to pass to the real function
462  //
463  // The FFMPEG_FUNCTION_NO_RETURN takes 3 arguments:
464  // 1) The function name <---| Taken from the FFmpeg funciton prototype
465  // 2) The function arguments <---|
466  // 3) The argument list to pass to the real function
467  //
468  // The FFMPEG_INITDYN macro is responsible for retrieving the address of the real function
469  // and storing that address in the function pointer variable. It will emit an error to the
470  // currently define log destination and return to the calling function.
471  //
472 #if defined(DEFINE_FFMPEG_POINTERS)
473  #define FFX
474 #else
475  #define FFX extern
476 #endif
477 
478 #define FFMPEG_FUNCTION_WITH_RETURN(r, n, a, p) \
479  extern "C" \
480  { \
481  FFX r (*n ## _fp) a; \
482  inline r n a \
483  { \
484  return n ## _fp p; \
485  } \
486  }
487 
488 #define FFMPEG_FUNCTION_NO_RETURN(n, a, p) \
489  extern "C" \
490  { \
491  FFX void (*n ## _fp) a; \
492  inline void n a \
493  { \
494  n ## _fp p; \
495  } \
496  }
497 
498 #define FFMPEG_INITDYN(w, f) \
499  { \
500  wxLogNull off; \
501  *(void**)&f ## _fp = (void*)w->GetSymbol(wxT(#f)); \
502  } \
503  if (f ## _fp == NULL) \
504  { \
505  wxLogError(wxT("Failed to load symbol ") wxT(#f)); \
506  return false; \
507  }
508 
509 #define FFMPEG_INITALT(w, f, x, a) \
510  { \
511  wxLogNull off; \
512  *(void**)&f ## _fp = (void*)w->GetSymbol(wxT(#f)); \
513  } \
514  if (f ## _fp == NULL) \
515  { \
516  { \
517  wxLogNull off; \
518  *(void**)&f ## _fp = (void*)x->GetSymbol(wxT(#a)); \
519  } \
520  if (f ## _fp == NULL) \
521  { \
522  wxLogError(wxT("Failed to load symbol ") wxT(#f)); \
523  return false; \
524  } \
525  }
526 
527  //
528  // libavutil
529  //
530  FFMPEG_FUNCTION_WITH_RETURN(
531  unsigned,
532  avutil_version,
533  (void),
534  ()
535  );
536  FFMPEG_FUNCTION_NO_RETURN(
537  av_log_set_callback,
538  (void (*cb)(void*, int, const char*, va_list)),
539  (cb)
540  );
541  FFMPEG_FUNCTION_NO_RETURN(
542  av_log_default_callback,
543  (void* ptr, int level, const char* fmt, va_list vl),
544  (ptr, level, fmt, vl)
545  );
546  FFMPEG_FUNCTION_NO_RETURN(
547  av_free,
548  (void *ptr),
549  (ptr)
550  );
551  FFMPEG_FUNCTION_WITH_RETURN(
552  int64_t,
553  av_get_default_channel_layout,
554  (int nb_channels),
555  (nb_channels)
556  );
557 
558  //
559  // libavcodec
560  //
561  FFMPEG_FUNCTION_WITH_RETURN(
562  unsigned,
563  avcodec_version,
564  (void),
565  ()
566  );
567  FFMPEG_FUNCTION_WITH_RETURN(
568  AVCodec*,
569  avcodec_find_encoder,
570  (enum AVCodecID id),
571  (id)
572  );
573  FFMPEG_FUNCTION_WITH_RETURN(
574  AVCodec*,
575  avcodec_find_encoder_by_name,
576  (const char *name),
577  (name)
578  );
579  FFMPEG_FUNCTION_WITH_RETURN(
580  AVCodec*,
581  avcodec_find_decoder,
582  (enum AVCodecID id),
583  (id)
584  );
585  FFMPEG_FUNCTION_WITH_RETURN(
586  const char*,
587  avcodec_get_name,
588  (enum AVCodecID id),
589  (id)
590  );
591  FFMPEG_FUNCTION_WITH_RETURN(
592  unsigned int,
593  av_codec_get_tag,
594  (const struct AVCodecTag * const *tags, enum AVCodecID id),
595  (tags, id)
596  );
597  FFMPEG_FUNCTION_WITH_RETURN(
598  int,
599  avcodec_open2,
600  (AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options),
601  (avctx, codec, options);
602  );
603 #if defined(IS_FFMPEG_PROJECT)
604  FFMPEG_FUNCTION_WITH_RETURN(
605  int,
606  avcodec_decode_audio4,
607  (AVCodecContext *avctx, AVFrame *frame, int *got_output, const AVPacket *avpkt),
608  (avctx, frame, got_output, avpkt)
609  );
610 #else
611  FFMPEG_FUNCTION_WITH_RETURN(
612  int,
613  avcodec_decode_audio4,
614  (AVCodecContext *avctx, AVFrame *frame, int *got_output, AVPacket *avpkt),
615  (avctx, frame, got_output, avpkt)
616  );
617 #endif
618  FFMPEG_FUNCTION_WITH_RETURN(
619  int,
620  avcodec_encode_audio2,
621  (AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_output),
622  (avctx, pkt, frame, got_output)
623  );
624  FFMPEG_FUNCTION_WITH_RETURN(
625  int,
626  avcodec_close,
627  (AVCodecContext *avctx),
628  (avctx)
629  );
630  FFMPEG_FUNCTION_NO_RETURN(
631  avcodec_register_all,
632  (void),
633  ()
634  );
635  FFMPEG_FUNCTION_WITH_RETURN(
636  int,
637  av_get_bytes_per_sample,
638  (enum AVSampleFormat sample_fmt),
639  (sample_fmt)
640  );
641  FFMPEG_FUNCTION_WITH_RETURN(
642  int,
643  avcodec_fill_audio_frame,
644  (AVFrame *frame, int nb_channels, enum AVSampleFormat sample_fmt, const uint8_t *buf, int buf_size, int align),
645  (frame, nb_channels, sample_fmt, buf, buf_size, align)
646  );
647 
648  //
649  // libavformat
650  //
651  FFMPEG_FUNCTION_WITH_RETURN(
652  unsigned,
653  avformat_version,
654  (void),
655  ()
656  );
657  FFMPEG_FUNCTION_WITH_RETURN(
658  int,
659  avformat_open_input,
660  (AVFormatContext **ic_ptr, const char *filename, AVInputFormat *fmt, AVDictionary **options),
661  (ic_ptr, filename, fmt, options)
662  );
663  FFMPEG_FUNCTION_NO_RETURN(
664  av_register_all,
665  (void),
666  ()
667  );
668  FFMPEG_FUNCTION_WITH_RETURN(
669  int,
670  avformat_find_stream_info,
671  (AVFormatContext *ic, AVDictionary **options),
672  (ic, options)
673  );
674  FFMPEG_FUNCTION_WITH_RETURN(
675  int,
676  av_read_frame,
677  (AVFormatContext *s, AVPacket *pkt),
678  (s, pkt)
679  );
680  FFMPEG_FUNCTION_WITH_RETURN(
681  int,
682  av_seek_frame,
683  (AVFormatContext *s, int stream_index, int64_t timestamp, int flags),
684  (s, stream_index, timestamp, flags)
685  );
686  FFMPEG_FUNCTION_NO_RETURN(
687  avformat_close_input,
688  (AVFormatContext **s),
689  (s)
690  );
691  FFMPEG_FUNCTION_WITH_RETURN(
692  int,
693  avformat_write_header,
694  (AVFormatContext *s, AVDictionary **options),
695  (s, options)
696  );
697 #if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(55, 33, 101)
698  FFMPEG_FUNCTION_WITH_RETURN(
699  AVOutputFormat*,
700  av_oformat_next,
701  (const AVOutputFormat *f),
702  (f)
703  );
704 #else
705  FFMPEG_FUNCTION_WITH_RETURN(
706  AVOutputFormat*,
707  av_oformat_next,
708  (AVOutputFormat *f),
709  (f)
710  );
711 #endif
712  FFMPEG_FUNCTION_WITH_RETURN(
713  AVCodec*,
714  av_codec_next,
715  (const AVCodec *c),
716  (c)
717  );
718 #if defined(IS_FFMPEG_PROJECT)
719  FFMPEG_FUNCTION_WITH_RETURN(
720  AVStream*,
721  avformat_new_stream,
722  (AVFormatContext *s, const AVCodec *c),
723  (s, c)
724  );
725 #else
726  FFMPEG_FUNCTION_WITH_RETURN(
727  AVStream*,
728  avformat_new_stream,
729  (AVFormatContext *s, AVCodec *c),
730  (s, c)
731  );
732 #endif
733  FFMPEG_FUNCTION_WITH_RETURN(
734  AVFormatContext*,
735  avformat_alloc_context,
736  (void),
737  ()
738  );
739  FFMPEG_FUNCTION_WITH_RETURN(
740  AVOutputFormat*,
741  av_guess_format,
742  (const char *short_name, const char *filename, const char *mime_type),
743  (short_name, filename, mime_type)
744  );
745  FFMPEG_FUNCTION_NO_RETURN(
746  avformat_free_context,
747  (AVFormatContext *s),
748  (s)
749  );
750  FFMPEG_FUNCTION_WITH_RETURN(
751  int,
752  av_write_trailer,
753  (AVFormatContext *s),
754  (s)
755  );
756  FFMPEG_FUNCTION_WITH_RETURN(
757  int,
758  av_interleaved_write_frame,
759  (AVFormatContext *s, AVPacket *pkt),
760  (s, pkt)
761  );
762  FFMPEG_FUNCTION_NO_RETURN(
763  av_init_packet,
764  (AVPacket *pkt),
765  (pkt)
766  );
767  FFMPEG_FUNCTION_WITH_RETURN(
768  int,
769  av_fifo_generic_write,
770  (AVFifoBuffer *f, void *src, int size, int (*func)(void*, void*, int)),
771  (f, src, size, func)
772  );
773  FFMPEG_FUNCTION_NO_RETURN(
774  av_fifo_free,
775  (AVFifoBuffer *f),
776  (f)
777  );
778 #if LIBAVUTIL_VERSION_MAJOR >= 53
779  FFMPEG_FUNCTION_WITH_RETURN(
780  int,
781  av_fifo_size,
782  (const AVFifoBuffer *f),
783  (f)
784  );
785 #else
786  FFMPEG_FUNCTION_WITH_RETURN(
787  int,
788  av_fifo_size,
789  (AVFifoBuffer *f),
790  (f)
791  );
792 #endif
793  FFMPEG_FUNCTION_WITH_RETURN(
794  void*,
795  av_malloc,
796  (size_t size),
797  (size)
798  );
799  /*
800  FFMPEG_FUNCTION_NO_RETURN(
801  av_freep,
802  (void *ptr),
803  (ptr)
804  );
805  */
806  FFMPEG_FUNCTION_WITH_RETURN(
807  int64_t,
808  av_rescale_q,
809  (int64_t a, AVRational bq, AVRational cq),
810  (a, bq, cq)
811  );
812  FFMPEG_FUNCTION_NO_RETURN(
813  av_free_packet,
814  (AVPacket *pkt),
815  (pkt)
816  );
817  FFMPEG_FUNCTION_WITH_RETURN(
818  AVFifoBuffer*,
819  av_fifo_alloc,
820  (unsigned int size),
821  (size)
822  );
823  FFMPEG_FUNCTION_WITH_RETURN(
824  int,
825  av_fifo_generic_read,
826  (AVFifoBuffer *f, void *buf, int buf_size, void (*func)(void*, void*, int)),
827  (f, buf, buf_size, func)
828  );
829  FFMPEG_FUNCTION_WITH_RETURN(
830  int,
831  av_fifo_realloc2,
832  (AVFifoBuffer *f, unsigned int size),
833  (f, size)
834  );
835  FFMPEG_FUNCTION_NO_RETURN(
836  av_dict_free,
837  (AVDictionary **m),
838  (m)
839  );
840 #if LIBAVUTIL_VERSION_MAJOR >= 53
841  FFMPEG_FUNCTION_WITH_RETURN(
842  AVDictionaryEntry *,
843  av_dict_get,
844  (const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags),
845  (m, key, prev, flags)
846  );
847 #else
848  FFMPEG_FUNCTION_WITH_RETURN(
849  AVDictionaryEntry *,
850  av_dict_get,
851  (AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags),
852  (m, key, prev, flags)
853  );
854 #endif
855  FFMPEG_FUNCTION_WITH_RETURN(
856  int,
857  av_dict_set,
858  (AVDictionary **pm, const char *key, const char *value, int flags),
859  (pm, key, value, flags)
860  );
861  FFMPEG_FUNCTION_WITH_RETURN(
862  int64_t,
863  avio_size,
864  (AVIOContext *s),
865  (s)
866  );
867  FFMPEG_FUNCTION_WITH_RETURN(
868  AVIOContext *,
869  avio_alloc_context,
870  (unsigned char *buffer,
871  int buffer_size,
872  int write_flag,
873  void *opaque,
874  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
875  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
876  int64_t (*seek)(void *opaque, int64_t offset, int whence)),
877  (buffer, buffer_size, write_flag, opaque, read_packet, write_packet, seek)
878  );
879  FFMPEG_FUNCTION_WITH_RETURN(
880  int,
881  av_codec_is_encoder,
882  (const AVCodec *codec),
883  (codec)
884  );
885  FFMPEG_FUNCTION_WITH_RETURN(
886  AVFrame*,
887  av_frame_alloc,
888  (void),
889  ()
890  );
891  FFMPEG_FUNCTION_NO_RETURN(
892  av_frame_free,
893  (AVFrame **frame),
894  (frame)
895  );
896  FFMPEG_FUNCTION_WITH_RETURN(
897  int,
898  av_samples_get_buffer_size,
899  (int *linesize, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align),
900  (linesize, nb_channels, nb_samples, sample_fmt, align)
901  );
902 };
903 
904 
905 #endif
906 
907 // Attach some C++ lifetime management to the struct, which owns some memory resources.
908 struct AVPacketEx : public AVPacket
909 {
910  AVPacketEx()
911  {
912  av_init_packet(this);
913  data = nullptr;
914  size = 0;
915  }
916  AVPacketEx(const AVPacketEx &) PROHIBITED;
917  AVPacketEx& operator= (const AVPacketEx&) PROHIBITED;
918  AVPacketEx(AVPacketEx &&that)
919  {
920  steal(std::move(that));
921  }
922  AVPacketEx &operator= (AVPacketEx &&that)
923  {
924  if (this != &that) {
925  reset();
926  steal(std::move(that));
927  }
928  return *this;
929  }
930 
931  ~AVPacketEx ()
932  {
933  reset();
934  }
935 
936  void reset()
937  {
938  // This does not deallocate the pointer, but it frees side data.
939  av_free_packet(this);
940  }
941 
942 private:
943  void steal(AVPacketEx &&that)
944  {
945  memcpy(this, &that, sizeof(that));
946  av_init_packet(&that);
947  that.data = nullptr;
948  that.size = 0;
949  }
950 };
951 
952 // utilites for RAII:
953 
954 // Deleter adaptor for functions like av_free that take a pointer
955 template<typename T, typename R, R(*Fn)(T*)> struct AV_Deleter {
956  inline R operator() (T* p) const
957  {
958  R result{};
959  if (p)
960  result = Fn(p);
961  return result;
962  }
963 };
964 
965 // Specialization of previous for void return
966 template<typename T, void(*Fn)(T*)>
967 struct AV_Deleter<T, void, Fn> {
968  inline void operator() (T* p) const
969  {
970  if (p)
971  Fn(p);
972  }
973 };
974 
975 // Deleter adaptor for functions like av_freep that take a pointer to a pointer
976 template<typename T, typename R, R(*Fn)(T**)> struct AV_Deleterp {
977  inline void operator() (T* p) const
978  {
979  if (p)
980  Fn(&p);
981  }
982 };
983 
984 using AVFrameHolder = std::unique_ptr<
985  AVFrame, AV_Deleterp<AVFrame, void, av_frame_free>
986 >;
987 using AVFifoBufferHolder = std::unique_ptr<
988  AVFifoBuffer, AV_Deleter<AVFifoBuffer, void, av_fifo_free>
989 >;
990 using AVFormatContextHolder = std::unique_ptr<
991  AVFormatContext, AV_Deleter<AVFormatContext, void, avformat_free_context>
992 >;
993 using AVCodecContextHolder = std::unique_ptr<
994  AVCodecContext, AV_Deleter<AVCodecContext, int, avcodec_close>
995 >;
996 using AVDictionaryCleanup = std::unique_ptr<
997  AVDictionary*, AV_Deleter<AVDictionary*, void, av_dict_free>
998 >;
999 struct UFileHolder : public std::unique_ptr<
1000  AVIOContext, ::AV_Deleter<AVIOContext, int, ufile_close>
1001 >
1002 {
1003  UFileHolder() = default;
1004  UFileHolder( UFileHolder &&that )
1005  : std::unique_ptr< AVIOContext, ::AV_Deleter<AVIOContext, int, ufile_close> >(
1006  std::move(that) )
1007  {
1008  }
1009 
1010  // Close explicitly, not ignoring return values.
1011  int close()
1012  {
1013  auto result = get_deleter() ( get() );
1014  release();
1015  return result;
1016  }
1017 };
1018 
1019 template<typename T> using AVMallocHolder = std::unique_ptr<
1020  T, AV_Deleter<void, void, av_free>
1021 >;
1022 
1023 struct streamContext
1024 {
1025  bool m_use{}; // TRUE = this stream will be loaded into Audacity
1026  AVStream *m_stream{}; // an AVStream *
1027  AVCodecContext *m_codecCtx{}; // pointer to m_stream->codec
1028 
1029  Maybe<AVPacketEx> m_pkt; // the last AVPacket we read for this stream
1030  uint8_t *m_pktDataPtr{}; // pointer into m_pkt.data
1031  int m_pktRemainingSiz{};
1032 
1033  int64_t m_pts{}; // the current presentation time of the input stream
1034  int64_t m_ptsOffset{}; // packets associated with stream are relative to this
1035 
1036  int m_frameValid{}; // is m_decodedVideoFrame/m_decodedAudioSamples valid?
1037  AVMallocHolder<uint8_t> m_decodedAudioSamples; // decoded audio samples stored here
1038  unsigned int m_decodedAudioSamplesSiz{}; // current size of m_decodedAudioSamples
1039  size_t m_decodedAudioSamplesValidSiz{}; // # valid bytes in m_decodedAudioSamples
1040  int m_initialchannels{}; // number of channels allocated when we begin the importing. Assumes that number of channels doesn't change on the fly.
1041 
1042  size_t m_samplesize{}; // input sample size in bytes
1043  AVSampleFormat m_samplefmt{ AV_SAMPLE_FMT_NONE }; // input sample format
1044 
1045  size_t m_osamplesize{}; // output sample size in bytes
1046  sampleFormat m_osamplefmt{ floatSample }; // output sample format
1047 
1048  streamContext() { memset(this, 0, sizeof(*this)); }
1049  ~streamContext()
1050  {
1051  }
1052 };
1053 
1055 using ScsPtr = std::shared_ptr<Scs>;
1056 
1057 #endif // USE_FFMPEG
1058 #endif // __AUDACITY_FFMPEG__
1059 
void av_log_wx_callback(void *ptr, int level, const char *fmt, va_list vl)
Callback function to catch FFmpeg log messages.
AudacityPrefs * gPrefs
Definition: Prefs.cpp:73
#define PROHIBITED
Definition: Audacity.h:224
Derived from ShuttleGuiBase, an Audacity specific class for shuttling data to and from GUI...
Definition: ShuttleGui.h:409
Definition: MemoryX.h:204
void EndVerticalLay()
wxCheckBox * AddCheckBox(const wxString &Prompt, const wxString &Selected)
Definition: ShuttleGui.cpp:298
sampleFormat
Definition: Types.h:188
wxString GetFFmpegVersion(wxWindow *parent)
Definition: FFmpeg.cpp:35
void AddFixedText(const wxString &Str, bool bCenter=false)
Definition: ShuttleGui.cpp:397
_("Move Track &Down")+wxT("\t")+(GetActiveProject() -> GetCommandManager() ->GetKeyFromName(wxT("TrackMoveDown")).Raw()), OnMoveTrack) POPUP_MENU_ITEM(OnMoveTopID, _("Move Track to &Top")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveTop")).Raw()), OnMoveTrack) POPUP_MENU_ITEM(OnMoveBottomID, _("Move Track to &Bottom")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveBottom")).Raw()), OnMoveTrack)#define SET_TRACK_NAME_PLUGIN_SYMBOLclass SetTrackNameCommand:public AudacityCommand
const wxChar * name
Definition: Distortion.cpp:94
Memory.h template class for making an array of float, bool, etc.
Definition: MemoryX.h:86
Class used to dynamically load FFmpeg libraries.
void AddStandardButtons(long buttons=eOkButton|eCancelButton, wxButton *extra=NULL)
void SetBorder(int Border)
Definition: ShuttleGui.h:286
void StartVerticalLay(int iProp=1)