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