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