hedgewars/avwrapper.c
author Stepan777 <stepik-777@mail.ru>
Sat, 07 Jul 2012 16:21:42 +0400
changeset 7356 1ae5cf294216
parent 7306 3cff5c769509
child 7359 8bb4da06b065
permissions -rw-r--r--
fix for older ffmpeg (0.7.13)
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>
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
     4
#include <stdint.h>
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
     5
#include <string.h>
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
     6
#include <stdarg.h>
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
     7
#include "libavformat/avformat.h"
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
     8
7356
1ae5cf294216 fix for older ffmpeg (0.7.13)
Stepan777 <stepik-777@mail.ru>
parents: 7306
diff changeset
     9
#ifndef AVIO_FLAG_WRITE
1ae5cf294216 fix for older ffmpeg (0.7.13)
Stepan777 <stepik-777@mail.ru>
parents: 7306
diff changeset
    10
#define AVIO_FLAG_WRITE AVIO_WRONLY
1ae5cf294216 fix for older ffmpeg (0.7.13)
Stepan777 <stepik-777@mail.ru>
parents: 7306
diff changeset
    11
#endif
1ae5cf294216 fix for older ffmpeg (0.7.13)
Stepan777 <stepik-777@mail.ru>
parents: 7306
diff changeset
    12
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    13
static AVFormatContext* g_pContainer;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    14
static AVOutputFormat* g_pFormat;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    15
static AVStream* g_pAStream;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    16
static AVStream* g_pVStream;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    17
static AVFrame* g_pAFrame;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    18
static AVFrame* g_pVFrame;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    19
static AVCodec* g_pACodec;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    20
static AVCodec* g_pVCodec;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    21
static AVCodecContext* g_pAudio;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    22
static AVCodecContext* g_pVideo;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    23
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
    24
static int g_Width, g_Height;
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    25
static uint32_t g_Frequency, g_Channels;
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
    26
static int g_VQuality, g_AQuality;
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
    27
static AVRational g_Framerate;
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
    28
static const char* g_pPreset;
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    29
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    30
static FILE* g_pSoundFile;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    31
static int16_t* g_pSamples;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    32
static int g_NumSamples;
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
/*
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    35
Initially I wrote code for latest ffmpeg, but on Linux (Ubuntu)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    36
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
    37
all of this #if LIBAVCODEC_VERSION_MAJOR < 54.
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    38
Actually, it may be possible to remove code for newer version
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    39
and use only code for older version.
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    40
*/
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
#if LIBAVCODEC_VERSION_MAJOR < 54
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    43
#define OUTBUFFER_SIZE 200000
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    44
static uint8_t g_OutBuffer[OUTBUFFER_SIZE];
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    45
#endif
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
// pointer to function from hwengine (uUtils.pas)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    48
static void (*AddFileLogRaw)(const char* pString);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    49
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    50
static void FatalError(const char* pFmt, ...)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    51
{
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    52
    const char Buffer[1024];
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    53
    va_list VaArgs;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    54
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    55
    va_start(VaArgs, pFmt);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    56
    vsnprintf(Buffer, 1024, pFmt, VaArgs);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    57
    va_end(VaArgs);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    58
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    59
    AddFileLogRaw("Error in av-wrapper: ");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    60
    AddFileLogRaw(Buffer);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    61
    AddFileLogRaw("\n");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    62
    exit(1);
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
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    65
// Function to be called from libav for logging.
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    66
// Note: libav can call LogCallback from different threads
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    67
// (there is mutex in AddFileLogRaw).
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    68
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
    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
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    72
    vsnprintf(Buffer, 1024, pFmt, VaArgs);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    73
    AddFileLogRaw(Buffer);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    74
}
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    75
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    76
static void Log(const char* pFmt, ...)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    77
{
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    78
    const char Buffer[1024];
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    79
    va_list VaArgs;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    80
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    81
    va_start(VaArgs, pFmt);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    82
    vsnprintf(Buffer, 1024, pFmt, VaArgs);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    83
    va_end(VaArgs);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    84
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    85
    AddFileLogRaw(Buffer);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    86
}
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    87
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
    88
static void AddAudioStream()
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    89
{
7235
baa69bd025d9 1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents: 7198
diff changeset
    90
#if LIBAVFORMAT_VERSION_MAJOR >= 54
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    91
    g_pAStream = avformat_new_stream(g_pContainer, g_pACodec);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    92
#else
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    93
    g_pAStream = av_new_stream(g_pContainer, 1);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    94
#endif
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    95
    if(!g_pAStream)
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    96
    {
7306
3cff5c769509 Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents: 7282
diff changeset
    97
        Log("Could not allocate audio stream\n");
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    98
        return;
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    99
    }
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   100
    g_pAStream->id = 1;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   101
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   102
    g_pAudio = g_pAStream->codec;
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   103
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   104
    avcodec_get_context_defaults3(g_pAudio, g_pACodec);
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   105
    g_pAudio->codec_id = g_pACodec->id;
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   106
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   107
    // put parameters
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   108
    g_pAudio->sample_fmt = AV_SAMPLE_FMT_S16;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   109
    g_pAudio->sample_rate = g_Frequency;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   110
    g_pAudio->channels = g_Channels;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   111
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   112
    // set quality
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   113
    if (g_AQuality > 100)
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   114
        g_pAudio->bit_rate = g_AQuality;
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   115
    else
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   116
    {
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   117
        g_pAudio->flags |= CODEC_FLAG_QSCALE;
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   118
        g_pAudio->global_quality = g_AQuality*FF_QP2LAMBDA;
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   119
    }
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   120
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   121
    // some formats want stream headers to be separate
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   122
    if (g_pFormat->flags & AVFMT_GLOBALHEADER)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   123
        g_pAudio->flags |= CODEC_FLAG_GLOBAL_HEADER;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   124
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   125
    // open it
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   126
    if (avcodec_open2(g_pAudio, g_pACodec, NULL) < 0)
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   127
    {
7306
3cff5c769509 Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents: 7282
diff changeset
   128
        Log("Could not open audio codec %s\n", g_pACodec->long_name);
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   129
        return;
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   130
    }
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   131
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   132
#if LIBAVCODEC_VERSION_MAJOR >= 54
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   133
    if (g_pACodec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   134
#else
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   135
    if (g_pAudio->frame_size == 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   136
#endif
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   137
        g_NumSamples = 4096;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   138
    else
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   139
        g_NumSamples = g_pAudio->frame_size;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   140
    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
   141
    g_pAFrame = avcodec_alloc_frame();
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   142
    if (!g_pAFrame)
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   143
    {
7306
3cff5c769509 Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents: 7282
diff changeset
   144
        Log("Could not allocate frame\n");
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   145
        return;
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   146
    }
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   147
}
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   148
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   149
// returns non-zero if there is more sound
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   150
static int WriteAudioFrame()
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   151
{
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   152
    if (!g_pAStream)
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   153
        return 0;
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   154
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   155
    AVPacket Packet = { 0 };
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   156
    av_init_packet(&Packet);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   157
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   158
    int NumSamples = fread(g_pSamples, 2*g_Channels, g_NumSamples, g_pSoundFile);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   159
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   160
#if LIBAVCODEC_VERSION_MAJOR >= 54
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   161
    AVFrame* pFrame = NULL;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   162
    if (NumSamples > 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   163
    {
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   164
        g_pAFrame->nb_samples = NumSamples;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   165
        avcodec_fill_audio_frame(g_pAFrame, g_Channels, AV_SAMPLE_FMT_S16,
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   166
                                 (uint8_t*)g_pSamples, NumSamples*2*g_Channels, 1);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   167
        pFrame = g_pAFrame;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   168
    }
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   169
    // when NumSamples == 0 we still need to call encode_audio2 to flush
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   170
    int got_packet;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   171
    if (avcodec_encode_audio2(g_pAudio, &Packet, pFrame, &got_packet) != 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   172
        FatalError("avcodec_encode_audio2 failed");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   173
    if (!got_packet)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   174
        return 0;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   175
#else
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   176
    if (NumSamples == 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   177
        return 0;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   178
    int BufferSize = OUTBUFFER_SIZE;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   179
    if (g_pAudio->frame_size == 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   180
        BufferSize = NumSamples*g_Channels*2;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   181
    Packet.size = avcodec_encode_audio(g_pAudio, g_OutBuffer, BufferSize, g_pSamples);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   182
    if (Packet.size == 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   183
        return 1;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   184
    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
   185
        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
   186
    Packet.flags |= AV_PKT_FLAG_KEY;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   187
    Packet.data = g_OutBuffer;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   188
#endif
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   189
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   190
    // Write the compressed frame to the media file.
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   191
    Packet.stream_index = g_pAStream->index;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   192
    if (av_interleaved_write_frame(g_pContainer, &Packet) != 0) 
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   193
        FatalError("Error while writing audio frame");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   194
    return 1;
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
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   197
// add a video output stream
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   198
static void AddVideoStream()
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   199
{
7235
baa69bd025d9 1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents: 7198
diff changeset
   200
#if LIBAVFORMAT_VERSION_MAJOR >= 54
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   201
    g_pVStream = avformat_new_stream(g_pContainer, g_pVCodec);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   202
#else
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   203
    g_pVStream = av_new_stream(g_pContainer, 0);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   204
#endif
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   205
    if (!g_pVStream)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   206
        FatalError("Could not allocate video stream");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   207
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   208
    g_pVideo = g_pVStream->codec;
7235
baa69bd025d9 1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents: 7198
diff changeset
   209
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   210
    avcodec_get_context_defaults3(g_pVideo, g_pVCodec);
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   211
    g_pVideo->codec_id = g_pVCodec->id;
7180
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
    // put parameters
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   214
    // resolution must be a multiple of two
7282
c65992e2d794 correctly fix odd dimensions (it was incorrectly fixed in previous commit)
Stepan777 <stepik-777@mail.ru>
parents: 7280
diff changeset
   215
    g_pVideo->width  = g_Width  & ~1; // make even (dimensions should be even)
c65992e2d794 correctly fix odd dimensions (it was incorrectly fixed in previous commit)
Stepan777 <stepik-777@mail.ru>
parents: 7280
diff changeset
   216
    g_pVideo->height = g_Height & ~1; // make even
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   217
    /* 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
   218
       of which frame timestamps are represented. for fixed-fps content,
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   219
       timebase should be 1/framerate and timestamp increments should be
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   220
       identically 1. */
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   221
    g_pVideo->time_base.den = g_Framerate.num;
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   222
    g_pVideo->time_base.num = g_Framerate.den;
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   223
    //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
   224
    g_pVideo->pix_fmt = PIX_FMT_YUV420P;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   225
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   226
    // set quality
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   227
    if (g_VQuality > 100)
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   228
        g_pVideo->bit_rate = g_VQuality;
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   229
    else
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   230
    {
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   231
        g_pVideo->flags |= CODEC_FLAG_QSCALE;
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   232
        g_pVideo->global_quality = g_VQuality*FF_QP2LAMBDA;
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   233
    }
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   234
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   235
    AVDictionary* pDict = NULL;
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   236
    if (strcmp(g_pVCodec->name, "libx264") == 0)
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   237
        av_dict_set(&pDict, "preset", g_pPreset, 0);
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   238
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   239
    // some formats want stream headers to be separate
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   240
    if (g_pFormat->flags & AVFMT_GLOBALHEADER)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   241
        g_pVideo->flags |= CODEC_FLAG_GLOBAL_HEADER;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   242
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   243
    // open the codec
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   244
    if (avcodec_open2(g_pVideo, g_pVCodec, &pDict) < 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   245
        FatalError("Could not open video codec %s", g_pVCodec->long_name);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   246
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   247
    g_pVFrame = avcodec_alloc_frame();
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   248
    if (!g_pVFrame)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   249
        FatalError("Could not allocate frame");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   250
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   251
    g_pVFrame->linesize[0] = g_Width;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   252
    g_pVFrame->linesize[1] = g_Width/2;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   253
    g_pVFrame->linesize[2] = g_Width/2;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   254
    g_pVFrame->linesize[3] = 0;
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
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   257
static int WriteFrame(AVFrame* pFrame)
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   258
{
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   259
    double AudioTime, VideoTime;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   260
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   261
    // write interleaved audio frame
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   262
    if (g_pAStream)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   263
    {
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   264
        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
   265
        do
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   266
            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
   267
        while (AudioTime < VideoTime && WriteAudioFrame());
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   268
    }
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   269
    
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   270
    if (!g_pVStream)
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   271
        return 0;
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   272
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   273
    AVPacket Packet;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   274
    av_init_packet(&Packet);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   275
    Packet.data = NULL;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   276
    Packet.size = 0;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   277
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   278
    g_pVFrame->pts++;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   279
    if (g_pFormat->flags & AVFMT_RAWPICTURE)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   280
    {
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   281
        /* raw video case. The API will change slightly in the near
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   282
           future for that. */
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.stream_index = g_pVStream->index;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   285
        Packet.data = (uint8_t*)pFrame;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   286
        Packet.size = sizeof(AVPicture);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   287
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
        return 0;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   291
    }
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   292
    else
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
#if LIBAVCODEC_VERSION_MAJOR >= 54
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   295
        int got_packet;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   296
        if (avcodec_encode_video2(g_pVideo, &Packet, pFrame, &got_packet) < 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   297
            FatalError("avcodec_encode_video2 failed");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   298
        if (!got_packet)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   299
            return 0;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   300
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   301
        if (Packet.pts != AV_NOPTS_VALUE)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   302
            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
   303
        if (Packet.dts != AV_NOPTS_VALUE)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   304
            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
   305
#else 
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   306
        Packet.size = avcodec_encode_video(g_pVideo, g_OutBuffer, OUTBUFFER_SIZE, pFrame);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   307
        if (Packet.size < 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   308
            FatalError("avcodec_encode_video failed");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   309
        if (Packet.size == 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   310
            return 0;
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
        if( g_pVideo->coded_frame->pts != AV_NOPTS_VALUE)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   313
            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
   314
        if( g_pVideo->coded_frame->key_frame )
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   315
            Packet.flags |= AV_PKT_FLAG_KEY;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   316
        Packet.data = g_OutBuffer;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   317
#endif
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   318
        // write the compressed frame in the media file
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   319
        Packet.stream_index = g_pVStream->index;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   320
        if (av_interleaved_write_frame(g_pContainer, &Packet) != 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   321
            FatalError("Error while writing video frame");
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
        return 1;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   324
    }
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   325
}
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
void AVWrapper_WriteFrame(uint8_t* pY, uint8_t* pCb, uint8_t* pCr)
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
    g_pVFrame->data[0] = pY;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   330
    g_pVFrame->data[1] = pCb;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   331
    g_pVFrame->data[2] = pCr;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   332
    WriteFrame(g_pVFrame);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   333
}
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   334
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   335
void AVWrapper_Init(
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   336
         void (*pAddFileLogRaw)(const char*),
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   337
         const char* pFilename,
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   338
         const char* pDesc,
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   339
         const char* pSoundFile,
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   340
         const char* pFormatName,
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   341
         const char* pVCodecName,
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   342
         const char* pACodecName,
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   343
         const char* pVPreset,
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   344
         int Width, int Height,
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   345
         int FramerateNum, int FramerateDen,
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   346
         int VQuality, int AQuality)
7180
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
    AddFileLogRaw = pAddFileLogRaw;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   349
    av_log_set_callback( &LogCallback );
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   350
7282
c65992e2d794 correctly fix odd dimensions (it was incorrectly fixed in previous commit)
Stepan777 <stepik-777@mail.ru>
parents: 7280
diff changeset
   351
    g_Width  = Width;
c65992e2d794 correctly fix odd dimensions (it was incorrectly fixed in previous commit)
Stepan777 <stepik-777@mail.ru>
parents: 7280
diff changeset
   352
    g_Height = Height;
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   353
    g_Framerate.num = FramerateNum;
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   354
    g_Framerate.den = FramerateDen;
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   355
    g_VQuality = VQuality;
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   356
    g_AQuality = AQuality;
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   357
    g_pPreset = pVPreset;
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   358
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   359
    // initialize libav and register all codecs and formats
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   360
    av_register_all();
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   361
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   362
    // find format
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   363
    g_pFormat = av_guess_format(pFormatName, NULL, NULL);
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   364
    if (!g_pFormat)
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   365
        FatalError("Format \"%s\" was not found", pFormatName);
7180
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
    g_pContainer = avformat_alloc_context();
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   369
    if (!g_pContainer)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   370
        FatalError("Could not allocate output context");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   371
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   372
    g_pContainer->oformat = g_pFormat;
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   373
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   374
    // store description of file
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   375
    av_dict_set(&g_pContainer->metadata, "comment", pDesc, 0);
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   376
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   377
    // append extesnion to filename
7235
baa69bd025d9 1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents: 7198
diff changeset
   378
    char ext[16];
baa69bd025d9 1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents: 7198
diff changeset
   379
    strncpy(ext, g_pFormat->extensions, 16);
baa69bd025d9 1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents: 7198
diff changeset
   380
    ext[15] = 0;
baa69bd025d9 1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents: 7198
diff changeset
   381
    ext[strcspn(ext,",")] = 0;
baa69bd025d9 1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents: 7198
diff changeset
   382
    snprintf(g_pContainer->filename, sizeof(g_pContainer->filename), "%s.%s", pFilename, ext);
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   383
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   384
    // find codecs
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   385
    g_pVCodec = avcodec_find_encoder_by_name(pVCodecName);
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   386
    g_pACodec = avcodec_find_encoder_by_name(pACodecName);
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   387
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   388
    // add audio and video stream to container
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   389
    g_pVStream = NULL;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   390
    g_pAStream = NULL;
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   391
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   392
    if (g_pVCodec)
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   393
        AddVideoStream();
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   394
    else
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   395
        Log("Video codec \"%s\" was not found; video will be ignored.\n", pVCodecName);
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   396
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   397
    if (g_pACodec)
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   398
    {
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   399
        g_pSoundFile = fopen(pSoundFile, "rb");
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   400
        if (g_pSoundFile)
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   401
        {
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   402
            fread(&g_Frequency, 4, 1, g_pSoundFile);
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   403
            fread(&g_Channels, 4, 1, g_pSoundFile);
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   404
            AddAudioStream();
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   405
        }
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   406
        else
7306
3cff5c769509 Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents: 7282
diff changeset
   407
            Log("Could not open %s\n", pSoundFile);
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   408
    }
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   409
    else
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   410
        Log("Audio codec \"%s\" was not found; audio will be ignored.\n", pACodecName);
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   411
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   412
    if (!g_pAStream && !g_pVStream)
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   413
        FatalError("No video, no audio, aborting...");
7180
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
    // write format info to log
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   416
    av_dump_format(g_pContainer, 0, g_pContainer->filename, 1);
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   417
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   418
    // open the output file, if needed
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   419
    if (!(g_pFormat->flags & AVFMT_NOFILE))
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   420
    {
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   421
        if (avio_open(&g_pContainer->pb, g_pContainer->filename, AVIO_FLAG_WRITE) < 0)
7306
3cff5c769509 Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents: 7282
diff changeset
   422
            FatalError("Could not open output file (%s)", g_pContainer->filename);
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   423
    }
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
    // write the stream header, if any
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   426
    avformat_write_header(g_pContainer, NULL);
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   427
7180
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
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   442
    // close the output file
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   443
    if (!(g_pFormat->flags & AVFMT_NOFILE))
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   444
        avio_close(g_pContainer->pb);
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   445
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   446
    // free everything
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   447
    if (g_pVStream)
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   448
    {
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   449
        avcodec_close(g_pVideo);
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   450
        av_free(g_pVideo);
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   451
        av_free(g_pVStream);
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   452
        av_free(g_pVFrame);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   453
    }
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   454
    if (g_pAStream)
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   455
    {
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   456
        avcodec_close(g_pAudio);
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   457
        av_free(g_pAudio);
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   458
        av_free(g_pAStream);
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   459
        av_free(g_pAFrame);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   460
        av_free(g_pSamples);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   461
        fclose(g_pSoundFile);
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
    av_free(g_pContainer);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   465
}