hedgewars/videorec/avwrapper.c
branchwebgl
changeset 8096 453917e94e55
parent 8065 609d07b337b5
parent 7981 aac257b77842
child 9127 e350500c4edb
equal deleted inserted replaced
8053:2e836bebb518 8096:453917e94e55
    20 #include <stdio.h>
    20 #include <stdio.h>
    21 #include <stdint.h>
    21 #include <stdint.h>
    22 #include <string.h>
    22 #include <string.h>
    23 #include <stdarg.h>
    23 #include <stdarg.h>
    24 #include "libavformat/avformat.h"
    24 #include "libavformat/avformat.h"
       
    25 #include "libavutil/mathematics.h"
    25 
    26 
    26 #ifndef AVIO_FLAG_WRITE
    27 #ifndef AVIO_FLAG_WRITE
    27 #define AVIO_FLAG_WRITE AVIO_WRONLY
    28 #define AVIO_FLAG_WRITE AVIO_WRONLY
    28 #endif
    29 #endif
    29 
    30 
    45 
    46 
    46 static FILE* g_pSoundFile;
    47 static FILE* g_pSoundFile;
    47 static int16_t* g_pSamples;
    48 static int16_t* g_pSamples;
    48 static int g_NumSamples;
    49 static int g_NumSamples;
    49 
    50 
    50 /*
       
    51 Initially I wrote code for latest ffmpeg, but on Linux (Ubuntu)
       
    52 only older version is available from repository. That's why you see here
       
    53 all of this #if LIBAVCODEC_VERSION_MAJOR < 54.
       
    54 Actually, it may be possible to remove code for newer version
       
    55 and use only code for older version.
       
    56 */
       
    57 
    51 
    58 #if LIBAVCODEC_VERSION_MAJOR < 54
    52 #if LIBAVCODEC_VERSION_MAJOR < 54
    59 #define OUTBUFFER_SIZE 200000
    53 #define OUTBUFFER_SIZE 200000
    60 static uint8_t g_OutBuffer[OUTBUFFER_SIZE];
    54 static uint8_t g_OutBuffer[OUTBUFFER_SIZE];
    61 #endif
    55 #endif
    63 // pointer to function from hwengine (uUtils.pas)
    57 // pointer to function from hwengine (uUtils.pas)
    64 static void (*AddFileLogRaw)(const char* pString);
    58 static void (*AddFileLogRaw)(const char* pString);
    65 
    59 
    66 static void FatalError(const char* pFmt, ...)
    60 static void FatalError(const char* pFmt, ...)
    67 {
    61 {
    68     const char Buffer[1024];
    62     char Buffer[1024];
    69     va_list VaArgs;
    63     va_list VaArgs;
    70 
    64 
    71     va_start(VaArgs, pFmt);
    65     va_start(VaArgs, pFmt);
    72     vsnprintf(Buffer, 1024, pFmt, VaArgs);
    66     vsnprintf(Buffer, 1024, pFmt, VaArgs);
    73     va_end(VaArgs);
    67     va_end(VaArgs);
    81 // Function to be called from libav for logging.
    75 // Function to be called from libav for logging.
    82 // Note: libav can call LogCallback from different threads
    76 // Note: libav can call LogCallback from different threads
    83 // (there is mutex in AddFileLogRaw).
    77 // (there is mutex in AddFileLogRaw).
    84 static void LogCallback(void* p, int Level, const char* pFmt, va_list VaArgs)
    78 static void LogCallback(void* p, int Level, const char* pFmt, va_list VaArgs)
    85 {
    79 {
    86     const char Buffer[1024];
    80     char Buffer[1024];
    87 
    81 
    88     vsnprintf(Buffer, 1024, pFmt, VaArgs);
    82     vsnprintf(Buffer, 1024, pFmt, VaArgs);
    89     AddFileLogRaw(Buffer);
    83     AddFileLogRaw(Buffer);
    90 }
    84 }
    91 
    85 
    92 static void Log(const char* pFmt, ...)
    86 static void Log(const char* pFmt, ...)
    93 {
    87 {
    94     const char Buffer[1024];
    88     char Buffer[1024];
    95     va_list VaArgs;
    89     va_list VaArgs;
    96 
    90 
    97     va_start(VaArgs, pFmt);
    91     va_start(VaArgs, pFmt);
    98     vsnprintf(Buffer, 1024, pFmt, VaArgs);
    92     vsnprintf(Buffer, 1024, pFmt, VaArgs);
    99     va_end(VaArgs);
    93     va_end(VaArgs);
   101     AddFileLogRaw(Buffer);
    95     AddFileLogRaw(Buffer);
   102 }
    96 }
   103 
    97 
   104 static void AddAudioStream()
    98 static void AddAudioStream()
   105 {
    99 {
   106 #if LIBAVFORMAT_VERSION_MAJOR >= 54
   100 #if LIBAVFORMAT_VERSION_MAJOR >= 53
   107     g_pAStream = avformat_new_stream(g_pContainer, g_pACodec);
   101     g_pAStream = avformat_new_stream(g_pContainer, g_pACodec);
   108 #else
   102 #else
   109     g_pAStream = av_new_stream(g_pContainer, 1);
   103     g_pAStream = av_new_stream(g_pContainer, 1);
   110 #endif
   104 #endif
   111     if(!g_pAStream)
   105     if(!g_pAStream)
   173     AVPacket Packet = { 0 };
   167     AVPacket Packet = { 0 };
   174     av_init_packet(&Packet);
   168     av_init_packet(&Packet);
   175 
   169 
   176     int NumSamples = fread(g_pSamples, 2*g_Channels, g_NumSamples, g_pSoundFile);
   170     int NumSamples = fread(g_pSamples, 2*g_Channels, g_NumSamples, g_pSoundFile);
   177 
   171 
   178 #if LIBAVCODEC_VERSION_MAJOR >= 54
   172 #if LIBAVCODEC_VERSION_MAJOR >= 53
   179     AVFrame* pFrame = NULL;
   173     AVFrame* pFrame = NULL;
   180     if (NumSamples > 0)
   174     if (NumSamples > 0)
   181     {
   175     {
   182         g_pAFrame->nb_samples = NumSamples;
   176         g_pAFrame->nb_samples = NumSamples;
   183         avcodec_fill_audio_frame(g_pAFrame, g_Channels, AV_SAMPLE_FMT_S16,
   177         avcodec_fill_audio_frame(g_pAFrame, g_Channels, AV_SAMPLE_FMT_S16,
   213 }
   207 }
   214 
   208 
   215 // add a video output stream
   209 // add a video output stream
   216 static void AddVideoStream()
   210 static void AddVideoStream()
   217 {
   211 {
   218 #if LIBAVFORMAT_VERSION_MAJOR >= 54
   212 #if LIBAVFORMAT_VERSION_MAJOR >= 53
   219     g_pVStream = avformat_new_stream(g_pContainer, g_pVCodec);
   213     g_pVStream = avformat_new_stream(g_pContainer, g_pVCodec);
   220 #else
   214 #else
   221     g_pVStream = av_new_stream(g_pContainer, 0);
   215     g_pVStream = av_new_stream(g_pContainer, 0);
   222 #endif
   216 #endif
   223     if (!g_pVStream)
   217     if (!g_pVStream)
   252 
   246 
   253     // some formats want stream headers to be separate
   247     // some formats want stream headers to be separate
   254     if (g_pFormat->flags & AVFMT_GLOBALHEADER)
   248     if (g_pFormat->flags & AVFMT_GLOBALHEADER)
   255         g_pVideo->flags |= CODEC_FLAG_GLOBAL_HEADER;
   249         g_pVideo->flags |= CODEC_FLAG_GLOBAL_HEADER;
   256 
   250 
   257 #if LIBAVCODEC_VERSION_MAJOR < 54
   251 #if LIBAVCODEC_VERSION_MAJOR < 53
   258     // for some versions of ffmpeg x264 options must be set explicitly
   252     // for some versions of ffmpeg x264 options must be set explicitly
   259     if (strcmp(g_pVCodec->name, "libx264") == 0)
   253     if (strcmp(g_pVCodec->name, "libx264") == 0)
   260     {
   254     {
   261         g_pVideo->coder_type = FF_CODER_TYPE_AC;
   255         g_pVideo->coder_type = FF_CODER_TYPE_AC;
   262         g_pVideo->flags |= CODEC_FLAG_LOOP_FILTER;
   256         g_pVideo->flags |= CODEC_FLAG_LOOP_FILTER;