hedgewars/avwrapper.c
author Stepan777 <stepik-777@mail.ru>
Mon, 04 Jun 2012 21:32:30 +0400
changeset 7180 53ffc8853008
child 7198 5debd5fe526e
permissions -rw-r--r--
here it is
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
     1
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
     2
#include <stdlib.h>
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
     3
#include <stdio.h>
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
     4
#include <string.h>
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
     5
#include <stdarg.h>
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
     6
#include "libavformat/avformat.h"
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
     7
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
     8
static AVFormatContext* g_pContainer;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
     9
static AVOutputFormat* g_pFormat;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    10
static AVStream* g_pAStream;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    11
static AVStream* g_pVStream;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    12
static AVFrame* g_pAFrame;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    13
static AVFrame* g_pVFrame;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    14
static AVCodec* g_pACodec;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    15
static AVCodec* g_pVCodec;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    16
static AVCodecContext* g_pAudio;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    17
static AVCodecContext* g_pVideo;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    18
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    19
static int g_Width, g_Height, g_Framerate;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    20
static int g_Frequency, g_Channels;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    21
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    22
static FILE* g_pSoundFile;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    23
static int16_t* g_pSamples;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    24
static int g_NumSamples;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    25
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    26
/*
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    27
Initially I wrote code for latest ffmpeg, but on Linux (Ubuntu)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    28
only older version is available from repository. That's why you see here
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    29
all of this #if LIBAVCODEC_VERSION_MAJOR < 54.
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    30
Actually, it may be possible to remove code for newer version
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    31
and use only code for older version.
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    32
*/
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    33
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    34
#if LIBAVCODEC_VERSION_MAJOR < 54
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    35
#define OUTBUFFER_SIZE 200000
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    36
static uint8_t g_OutBuffer[OUTBUFFER_SIZE];
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    37
#endif
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    38
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    39
// pointer to function from hwengine (uUtils.pas)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    40
static void (*AddFileLogRaw)(const char* pString);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    41
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    42
static void FatalError(const char* pFmt, ...)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    43
{
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    44
    const char Buffer[1024];
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    45
    va_list VaArgs;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    46
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    47
    va_start(VaArgs, pFmt);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    48
    vsnprintf(Buffer, 1024, pFmt, VaArgs);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    49
    va_end(VaArgs);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    50
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    51
    AddFileLogRaw("Error in av-wrapper: ");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    52
    AddFileLogRaw(Buffer);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    53
    AddFileLogRaw("\n");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    54
    exit(1);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    55
}
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    56
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    57
// Function to be called from libav for logging.
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    58
// Note: libav can call LogCallback from different threads
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    59
// (there is mutex in AddFileLogRaw).
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    60
static void LogCallback(void* p, int Level, const char* pFmt, va_list VaArgs)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    61
{
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    62
    const char Buffer[1024];
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    63
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    64
    vsnprintf(Buffer, 1024, pFmt, VaArgs);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    65
    AddFileLogRaw(Buffer);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    66
}
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    67
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    68
static void Log(const char* pFmt, ...)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    69
{
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    70
    const char Buffer[1024];
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    71
    va_list VaArgs;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    72
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    73
    va_start(VaArgs, pFmt);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    74
    vsnprintf(Buffer, 1024, pFmt, VaArgs);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    75
    va_end(VaArgs);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    76
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    77
    AddFileLogRaw(Buffer);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    78
}
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    79
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    80
static void AddAudioStream(enum CodecID codec_id)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    81
{
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    82
#if LIBAVCODEC_VERSION_MAJOR >= 54
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    83
    g_pAStream = avformat_new_stream(g_pContainer, g_pACodec);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    84
#else
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    85
    g_pAStream = av_new_stream(g_pContainer, 1);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    86
#endif
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    87
    if(!g_pAStream)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    88
        FatalError("Could not allocate audio stream");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    89
    g_pAStream->id = 1;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    90
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    91
    g_pAudio = g_pAStream->codec;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    92
    avcodec_get_context_defaults3(g_pAudio, g_pACodec);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    93
    g_pAudio->codec_id = codec_id;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    94
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    95
    // put parameters
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    96
    g_pAudio->sample_fmt = AV_SAMPLE_FMT_S16;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    97
 //   pContext->bit_rate = 128000;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    98
    g_pAudio->sample_rate = g_Frequency;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    99
    g_pAudio->channels = g_Channels;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   100
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   101
    // some formats want stream headers to be separate
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   102
    if (g_pFormat->flags & AVFMT_GLOBALHEADER)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   103
        g_pAudio->flags |= CODEC_FLAG_GLOBAL_HEADER;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   104
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   105
    // open it
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   106
    if (avcodec_open2(g_pAudio, g_pACodec, NULL) < 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   107
        FatalError("Could not open audio codec %s", g_pACodec->long_name);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   108
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   109
#if LIBAVCODEC_VERSION_MAJOR >= 54
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   110
    if (g_pACodec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   111
#else
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   112
    if (g_pAudio->frame_size == 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   113
#endif
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   114
        g_NumSamples = 4096;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   115
    else
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   116
        g_NumSamples = g_pAudio->frame_size;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   117
    g_pSamples = (int16_t*)av_malloc(g_NumSamples*g_Channels*sizeof(int16_t));
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   118
    g_pAFrame = avcodec_alloc_frame();
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   119
    if (!g_pAFrame)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   120
        FatalError("Could not allocate frame");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   121
}
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   122
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   123
// returns non-zero if there is more sound
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   124
static int WriteAudioFrame()
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   125
{
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   126
    AVPacket Packet = { 0 };
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   127
    av_init_packet(&Packet);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   128
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   129
    int NumSamples = fread(g_pSamples, 2*g_Channels, g_NumSamples, g_pSoundFile);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   130
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   131
#if LIBAVCODEC_VERSION_MAJOR >= 54
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   132
    AVFrame* pFrame = NULL;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   133
    if (NumSamples > 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   134
    {
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   135
        g_pAFrame->nb_samples = NumSamples;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   136
        avcodec_fill_audio_frame(g_pAFrame, g_Channels, AV_SAMPLE_FMT_S16,
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   137
                                 (uint8_t*)g_pSamples, NumSamples*2*g_Channels, 1);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   138
        pFrame = g_pAFrame;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   139
    }
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   140
    // when NumSamples == 0 we still need to call encode_audio2 to flush
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   141
    int got_packet;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   142
    if (avcodec_encode_audio2(g_pAudio, &Packet, pFrame, &got_packet) != 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   143
        FatalError("avcodec_encode_audio2 failed");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   144
    if (!got_packet)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   145
        return 0;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   146
#else
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   147
    if (NumSamples == 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   148
        return 0;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   149
    int BufferSize = OUTBUFFER_SIZE;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   150
    if (g_pAudio->frame_size == 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   151
        BufferSize = NumSamples*g_Channels*2;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   152
    Packet.size = avcodec_encode_audio(g_pAudio, g_OutBuffer, BufferSize, g_pSamples);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   153
    if (Packet.size == 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   154
        return 1;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   155
    if (g_pAudio->coded_frame && g_pAudio->coded_frame->pts != AV_NOPTS_VALUE)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   156
        Packet.pts = av_rescale_q(g_pAudio->coded_frame->pts, g_pAudio->time_base, g_pAStream->time_base);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   157
    Packet.flags |= AV_PKT_FLAG_KEY;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   158
    Packet.data = g_OutBuffer;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   159
#endif
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   160
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   161
    // Write the compressed frame to the media file.
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   162
    Packet.stream_index = g_pAStream->index;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   163
    if (av_interleaved_write_frame(g_pContainer, &Packet) != 0) 
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   164
        FatalError("Error while writing audio frame");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   165
    return 1;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   166
}
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   167
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   168
// add a video output stream
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   169
static void AddVideoStream(enum CodecID codec_id)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   170
{
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   171
#if LIBAVCODEC_VERSION_MAJOR >= 54
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   172
    g_pVStream = avformat_new_stream(g_pContainer, g_pVCodec);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   173
#else
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   174
    g_pVStream = av_new_stream(g_pContainer, 0);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   175
#endif
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   176
    if (!g_pVStream)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   177
        FatalError("Could not allocate video stream");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   178
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   179
    g_pVideo = g_pVStream->codec;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   180
    avcodec_get_context_defaults3( g_pVideo, g_pVCodec );
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   181
    g_pVideo->codec_id = codec_id;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   182
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   183
    // put parameters
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   184
    // resolution must be a multiple of two
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   185
    g_pVideo->width = g_Width;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   186
    g_pVideo->height = g_Height;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   187
    /* time base: this is the fundamental unit of time (in seconds) in terms
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   188
       of which frame timestamps are represented. for fixed-fps content,
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   189
       timebase should be 1/framerate and timestamp increments should be
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   190
       identically 1. */
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   191
    g_pVideo->time_base.den = g_Framerate;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   192
    g_pVideo->time_base.num = 1;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   193
    //g_pVideo->gop_size = 12; /* emit one intra frame every twelve frames at most */
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   194
    g_pVideo->pix_fmt = PIX_FMT_YUV420P;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   195
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   196
    // some formats want stream headers to be separate
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   197
    if (g_pFormat->flags & AVFMT_GLOBALHEADER)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   198
        g_pVideo->flags |= CODEC_FLAG_GLOBAL_HEADER;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   199
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   200
    AVDictionary* pDict = NULL;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   201
    if (codec_id == CODEC_ID_H264)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   202
    {
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   203
       // av_dict_set(&pDict, "tune", "animation", 0);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   204
       // av_dict_set(&pDict, "preset", "veryslow", 0);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   205
       av_dict_set(&pDict, "crf", "20", 0);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   206
    }
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   207
    else
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   208
    {
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   209
        g_pVideo->flags |= CODEC_FLAG_QSCALE;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   210
       // g_pVideo->bit_rate = g_Width*g_Height*g_Framerate/4;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   211
        g_pVideo->global_quality = 15*FF_QP2LAMBDA;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   212
    }
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   213
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   214
    // open the codec
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   215
    if (avcodec_open2(g_pVideo, g_pVCodec, &pDict) < 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   216
        FatalError("Could not open video codec %s", g_pVCodec->long_name);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   217
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   218
    g_pVFrame = avcodec_alloc_frame();
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   219
    if (!g_pVFrame)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   220
        FatalError("Could not allocate frame");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   221
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   222
    g_pVFrame->linesize[0] = g_Width;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   223
    g_pVFrame->linesize[1] = g_Width/2;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   224
    g_pVFrame->linesize[2] = g_Width/2;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   225
    g_pVFrame->linesize[3] = 0;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   226
}
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   227
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   228
static int WriteFrame( AVFrame* pFrame )
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   229
{
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   230
    double AudioTime, VideoTime;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   231
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   232
    // write interleaved audio frame
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   233
    if (g_pAStream)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   234
    {
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   235
        VideoTime = (double)g_pVStream->pts.val*g_pVStream->time_base.num/g_pVStream->time_base.den;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   236
        do
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   237
            AudioTime = (double)g_pAStream->pts.val*g_pAStream->time_base.num/g_pAStream->time_base.den;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   238
        while (AudioTime < VideoTime && WriteAudioFrame());
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   239
    }
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   240
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   241
    AVPacket Packet;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   242
    av_init_packet(&Packet);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   243
    Packet.data = NULL;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   244
    Packet.size = 0;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   245
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   246
    g_pVFrame->pts++;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   247
    if (g_pFormat->flags & AVFMT_RAWPICTURE)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   248
    {
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   249
        /* raw video case. The API will change slightly in the near
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   250
           future for that. */
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   251
        Packet.flags |= AV_PKT_FLAG_KEY;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   252
        Packet.stream_index = g_pVStream->index;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   253
        Packet.data = (uint8_t*)pFrame;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   254
        Packet.size = sizeof(AVPicture);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   255
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   256
        if (av_interleaved_write_frame(g_pContainer, &Packet) != 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   257
            FatalError("Error while writing video frame");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   258
        return 0;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   259
    }
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   260
    else
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   261
    {
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   262
#if LIBAVCODEC_VERSION_MAJOR >= 54
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   263
        int got_packet;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   264
        if (avcodec_encode_video2(g_pVideo, &Packet, pFrame, &got_packet) < 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   265
            FatalError("avcodec_encode_video2 failed");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   266
        if (!got_packet)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   267
            return 0;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   268
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   269
        if (Packet.pts != AV_NOPTS_VALUE)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   270
            Packet.pts = av_rescale_q(Packet.pts, g_pVideo->time_base, g_pVStream->time_base);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   271
        if (Packet.dts != AV_NOPTS_VALUE)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   272
            Packet.dts = av_rescale_q(Packet.dts, g_pVideo->time_base, g_pVStream->time_base);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   273
#else 
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   274
        Packet.size = avcodec_encode_video(g_pVideo, g_OutBuffer, OUTBUFFER_SIZE, pFrame);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   275
        if (Packet.size < 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   276
            FatalError("avcodec_encode_video failed");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   277
        if (Packet.size == 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   278
            return 0;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   279
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   280
        if( g_pVideo->coded_frame->pts != AV_NOPTS_VALUE)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   281
            Packet.pts = av_rescale_q(g_pVideo->coded_frame->pts, g_pVideo->time_base, g_pVStream->time_base);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   282
        if( g_pVideo->coded_frame->key_frame )
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   283
            Packet.flags |= AV_PKT_FLAG_KEY;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   284
        Packet.data = g_OutBuffer;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   285
#endif
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   286
        // write the compressed frame in the media file
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   287
        Packet.stream_index = g_pVStream->index;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   288
        if (av_interleaved_write_frame(g_pContainer, &Packet) != 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   289
            FatalError("Error while writing video frame");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   290
            
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   291
        return 1;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   292
    }
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   293
}
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   294
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   295
void AVWrapper_WriteFrame(uint8_t* pY, uint8_t* pCb, uint8_t* pCr)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   296
{
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   297
    g_pVFrame->data[0] = pY;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   298
    g_pVFrame->data[1] = pCb;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   299
    g_pVFrame->data[2] = pCr;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   300
    WriteFrame(g_pVFrame);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   301
}
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   302
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   303
void AVWrapper_GetList()
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   304
{
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   305
    // initialize libav and register all codecs and formats
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   306
    av_register_all();
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   307
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   308
#if 0
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   309
    AVOutputFormat* pFormat = NULL;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   310
    while (pFormat = av_oformat_next(pFormat))
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   311
    {
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   312
        Log("%s; %s; %s;\n", pFormat->name, pFormat->long_name, pFormat->mime_type);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   313
        
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   314
        AVCodec* pCodec = NULL;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   315
        while (pCodec = av_codec_next(pCodec))
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   316
        {
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   317
            if (!av_codec_is_encoder(pCodec))
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   318
                continue;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   319
            if (avformat_query_codec(pFormat, pCodec->id, FF_COMPLIANCE_NORMAL) != 1)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   320
                continue;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   321
            if (pCodec->type = AVMEDIA_TYPE_VIDEO)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   322
            {
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   323
                if (pCodec->supported_framerate != NULL)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   324
                    continue;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   325
                Log("    Video: %s; %s;\n", pCodec->name, pCodec->long_name);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   326
            }
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   327
            if (pCodec->type = AVMEDIA_TYPE_AUDIO)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   328
            {
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   329
               /* if (pCodec->supported_samplerates == NULL)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   330
                    continue;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   331
                int i;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   332
                for(i = 0; i <)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   333
                    supported_samplerates*/
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   334
                Log("    Audio: %s; %s;\n", pCodec->name, pCodec->long_name);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   335
            }
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   336
        }
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   337
   /*     struct AVCodecTag** pTags = pCur->codec_tag;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   338
        int i;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   339
        for (i = 0; ; i++)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   340
        {
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   341
            enum CodecID id = av_codec_get_id(pTags, i);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   342
            if (id == CODEC_ID_NONE)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   343
                break;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   344
            AVCodec* pCodec = avcodec_find_encoder(id);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   345
            Log("    %i: %s; %s;\n", id, pCodec->name, pCodec->long_name);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   346
        }*/
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   347
    }
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   348
#endif
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   349
}
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   350
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   351
void AVWrapper_Init(void (*pAddFileLogRaw)(const char*), const char* pFilename, const char* pSoundFile, int Width, int Height, int Framerate, int Frequency, int Channels)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   352
{    
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   353
    AddFileLogRaw = pAddFileLogRaw;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   354
    av_log_set_callback( &LogCallback );
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   355
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   356
    g_Width = Width;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   357
    g_Height = Height;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   358
    g_Framerate = Framerate;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   359
    g_Frequency = Frequency;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   360
    g_Channels = Channels;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   361
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   362
    // initialize libav and register all codecs and formats
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   363
    av_register_all();
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   364
    
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   365
    AVWrapper_GetList();
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   366
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   367
    // allocate the output media context
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   368
#if LIBAVCODEC_VERSION_MAJOR >= 54
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   369
    avformat_alloc_output_context2(&g_pContainer, NULL, "mp4", pFilename);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   370
#else
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   371
    g_pFormat = av_guess_format(NULL, pFilename, NULL);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   372
    if (!g_pFormat)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   373
        FatalError("guess_format");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   374
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   375
    // allocate the output media context
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   376
    g_pContainer = avformat_alloc_context();
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   377
    if (g_pContainer)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   378
    {
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   379
        g_pContainer->oformat = g_pFormat;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   380
        snprintf(g_pContainer->filename, sizeof(g_pContainer->filename), "%s", pFilename);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   381
    }
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   382
#endif
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   383
    if (!g_pContainer)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   384
        FatalError("Could not allocate output context");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   385
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   386
    g_pFormat = g_pContainer->oformat;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   387
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   388
    enum CodecID VideoCodecID = g_pFormat->video_codec;//CODEC_ID_H264;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   389
    enum CodecID AudioCodecID = g_pFormat->audio_codec;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   390
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   391
    g_pVStream = NULL;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   392
    g_pAStream = NULL;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   393
    if (VideoCodecID != CODEC_ID_NONE)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   394
    {
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   395
        g_pVCodec = avcodec_find_encoder(VideoCodecID);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   396
        if (!g_pVCodec)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   397
            FatalError("Video codec not found");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   398
        AddVideoStream(VideoCodecID);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   399
    }
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   400
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   401
    if (AudioCodecID != CODEC_ID_NONE)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   402
    {
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   403
        g_pACodec = avcodec_find_encoder(AudioCodecID);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   404
        if (!g_pACodec)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   405
            FatalError("Audio codec not found");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   406
        AddAudioStream(AudioCodecID);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   407
    }
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   408
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   409
    if (g_pAStream)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   410
    {
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   411
        g_pSoundFile = fopen(pSoundFile, "rb");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   412
        if (!g_pSoundFile)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   413
            FatalError("Could not open %s", pSoundFile);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   414
    }
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   415
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   416
    // write format info to log
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   417
    av_dump_format(g_pContainer, 0, pFilename, 1);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   418
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   419
    // open the output file, if needed
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   420
    if (!(g_pFormat->flags & AVFMT_NOFILE))
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   421
    {
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   422
        if (avio_open(&g_pContainer->pb, pFilename, AVIO_FLAG_WRITE) < 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   423
            FatalError("Could not open output file (%s)", pFilename);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   424
    }
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   425
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   426
    // write the stream header, if any
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   427
    avformat_write_header(g_pContainer, NULL);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   428
    g_pVFrame->pts = -1;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   429
}
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   430
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   431
void AVWrapper_Close()
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   432
{
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   433
    // output buffered frames
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   434
    if (g_pVCodec->capabilities & CODEC_CAP_DELAY)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   435
        while( WriteFrame(NULL) );
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   436
    // output any remaining audio
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   437
    while( WriteAudioFrame() );
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   438
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   439
    // write the trailer, if any.
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   440
    av_write_trailer(g_pContainer);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   441
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   442
    // close each codec
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   443
    if( g_pVStream )
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   444
    {
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   445
        avcodec_close(g_pVStream->codec);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   446
        av_free(g_pVFrame);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   447
    }
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   448
    if( g_pAStream )
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   449
    {
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   450
        avcodec_close(g_pAStream->codec);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   451
        av_free(g_pAFrame);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   452
        av_free(g_pSamples);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   453
        fclose(g_pSoundFile);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   454
    }
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   455
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   456
    // free the streams
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   457
    int i;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   458
    for (i = 0; i < g_pContainer->nb_streams; i++)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   459
    {
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   460
        av_freep(&g_pContainer->streams[i]->codec);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   461
        av_freep(&g_pContainer->streams[i]);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   462
    }
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   463
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   464
    // close the output file
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   465
    if (!(g_pFormat->flags & AVFMT_NOFILE))
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   466
        avio_close(g_pContainer->pb);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   467
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   468
    // free the stream 
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   469
    av_free(g_pContainer);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   470
}