hedgewars/avwrapper.c
author sheepluva
Tue, 04 Sep 2012 13:18:26 +0200
changeset 7669 a85e1c167b69
parent 7542 37ef1891efe2
child 8065 609d07b337b5
permissions -rw-r--r--
I didn't want to do this since it seems less clean, but... moving the stats-fix into CheckForWin, since that function is the one sending the damage stats (whyyyy?) therefore it's not sufficient to update stats after calling it, some of the stats won't be transfered to frontend then
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7542
37ef1891efe2 Insert license in avwrapper.c
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
     1
/*
37ef1891efe2 Insert license in avwrapper.c
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
     2
 * Hedgewars, a free turn based strategy game
37ef1891efe2 Insert license in avwrapper.c
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
     3
 * Copyright (c) 2004-2012 Andrey Korotaev <unC0Rr@gmail.com>
37ef1891efe2 Insert license in avwrapper.c
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
     4
 *
37ef1891efe2 Insert license in avwrapper.c
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
37ef1891efe2 Insert license in avwrapper.c
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
37ef1891efe2 Insert license in avwrapper.c
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
37ef1891efe2 Insert license in avwrapper.c
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
     8
 *
37ef1891efe2 Insert license in avwrapper.c
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
37ef1891efe2 Insert license in avwrapper.c
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37ef1891efe2 Insert license in avwrapper.c
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37ef1891efe2 Insert license in avwrapper.c
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    12
 * GNU General Public License for more details.
37ef1891efe2 Insert license in avwrapper.c
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    13
 *
37ef1891efe2 Insert license in avwrapper.c
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    14
 * You should have received a copy of the GNU General Public License
37ef1891efe2 Insert license in avwrapper.c
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    15
 * along with this program; if not, write to the Free Software
37ef1891efe2 Insert license in avwrapper.c
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
37ef1891efe2 Insert license in avwrapper.c
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    17
 */
7180
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
#include <stdlib.h>
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    20
#include <stdio.h>
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    21
#include <stdint.h>
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    22
#include <string.h>
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    23
#include <stdarg.h>
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    24
#include "libavformat/avformat.h"
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    25
7356
1ae5cf294216 fix for older ffmpeg (0.7.13)
Stepan777 <stepik-777@mail.ru>
parents: 7306
diff changeset
    26
#ifndef AVIO_FLAG_WRITE
1ae5cf294216 fix for older ffmpeg (0.7.13)
Stepan777 <stepik-777@mail.ru>
parents: 7306
diff changeset
    27
#define AVIO_FLAG_WRITE AVIO_WRONLY
1ae5cf294216 fix for older ffmpeg (0.7.13)
Stepan777 <stepik-777@mail.ru>
parents: 7306
diff changeset
    28
#endif
1ae5cf294216 fix for older ffmpeg (0.7.13)
Stepan777 <stepik-777@mail.ru>
parents: 7306
diff changeset
    29
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    30
static AVFormatContext* g_pContainer;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    31
static AVOutputFormat* g_pFormat;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    32
static AVStream* g_pAStream;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    33
static AVStream* g_pVStream;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    34
static AVFrame* g_pAFrame;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    35
static AVFrame* g_pVFrame;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    36
static AVCodec* g_pACodec;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    37
static AVCodec* g_pVCodec;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    38
static AVCodecContext* g_pAudio;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    39
static AVCodecContext* g_pVideo;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    40
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
    41
static int g_Width, g_Height;
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    42
static uint32_t g_Frequency, g_Channels;
7376
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
    43
static int g_VQuality;
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
    44
static AVRational g_Framerate;
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    45
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    46
static FILE* g_pSoundFile;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    47
static int16_t* g_pSamples;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    48
static int g_NumSamples;
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
/*
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    51
Initially I wrote code for latest ffmpeg, but on Linux (Ubuntu)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    52
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
    53
all of this #if LIBAVCODEC_VERSION_MAJOR < 54.
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    54
Actually, it may be possible to remove code for newer version
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    55
and use only code for older version.
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
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    58
#if LIBAVCODEC_VERSION_MAJOR < 54
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    59
#define OUTBUFFER_SIZE 200000
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    60
static uint8_t g_OutBuffer[OUTBUFFER_SIZE];
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    61
#endif
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    62
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    63
// pointer to function from hwengine (uUtils.pas)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    64
static void (*AddFileLogRaw)(const char* pString);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    65
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    66
static void FatalError(const char* pFmt, ...)
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
    const char Buffer[1024];
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    69
    va_list VaArgs;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    70
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    71
    va_start(VaArgs, pFmt);
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
    va_end(VaArgs);
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
    AddFileLogRaw("Error in av-wrapper: ");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    76
    AddFileLogRaw(Buffer);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    77
    AddFileLogRaw("\n");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    78
    exit(1);
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
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    81
// Function to be called from libav for logging.
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    82
// Note: libav can call LogCallback from different threads
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    83
// (there is mutex in AddFileLogRaw).
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    84
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
    85
{
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    86
    const char Buffer[1024];
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    87
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    88
    vsnprintf(Buffer, 1024, pFmt, VaArgs);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    89
    AddFileLogRaw(Buffer);
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
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    92
static void Log(const char* pFmt, ...)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    93
{
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    94
    const char Buffer[1024];
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    95
    va_list VaArgs;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    96
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    97
    va_start(VaArgs, pFmt);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    98
    vsnprintf(Buffer, 1024, pFmt, VaArgs);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    99
    va_end(VaArgs);
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
    AddFileLogRaw(Buffer);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   102
}
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   103
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   104
static void AddAudioStream()
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   105
{
7235
baa69bd025d9 1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents: 7198
diff changeset
   106
#if LIBAVFORMAT_VERSION_MAJOR >= 54
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   107
    g_pAStream = avformat_new_stream(g_pContainer, g_pACodec);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   108
#else
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   109
    g_pAStream = av_new_stream(g_pContainer, 1);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   110
#endif
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   111
    if(!g_pAStream)
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   112
    {
7306
3cff5c769509 Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents: 7282
diff changeset
   113
        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
   114
        return;
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   115
    }
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   116
    g_pAStream->id = 1;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   117
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   118
    g_pAudio = g_pAStream->codec;
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   119
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   120
    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
   121
    g_pAudio->codec_id = g_pACodec->id;
7180
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
    // put parameters
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   124
    g_pAudio->sample_fmt = AV_SAMPLE_FMT_S16;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   125
    g_pAudio->sample_rate = g_Frequency;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   126
    g_pAudio->channels = g_Channels;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   127
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   128
    // set quality
7376
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   129
    g_pAudio->bit_rate = 160000;
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   130
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   131
    // for codecs that support variable bitrate use it, it should be better
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   132
    g_pAudio->flags |= CODEC_FLAG_QSCALE;
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   133
    g_pAudio->global_quality = 1*FF_QP2LAMBDA;
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   134
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   135
    // some formats want stream headers to be separate
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   136
    if (g_pFormat->flags & AVFMT_GLOBALHEADER)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   137
        g_pAudio->flags |= CODEC_FLAG_GLOBAL_HEADER;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   138
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   139
    // open it
7359
8bb4da06b065 Fix compilation of hwengine on ffmpeg 0.7.13.
Stepan777 <stepik-777@mail.ru>
parents: 7356
diff changeset
   140
#if LIBAVCODEC_VERSION_MAJOR >= 53
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   141
    if (avcodec_open2(g_pAudio, g_pACodec, NULL) < 0)
7359
8bb4da06b065 Fix compilation of hwengine on ffmpeg 0.7.13.
Stepan777 <stepik-777@mail.ru>
parents: 7356
diff changeset
   142
#else
8bb4da06b065 Fix compilation of hwengine on ffmpeg 0.7.13.
Stepan777 <stepik-777@mail.ru>
parents: 7356
diff changeset
   143
    if (avcodec_open(g_pAudio, g_pACodec) < 0)
8bb4da06b065 Fix compilation of hwengine on ffmpeg 0.7.13.
Stepan777 <stepik-777@mail.ru>
parents: 7356
diff changeset
   144
#endif
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   145
    {
7306
3cff5c769509 Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents: 7282
diff changeset
   146
        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
   147
        return;
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   148
    }
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   149
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   150
#if LIBAVCODEC_VERSION_MAJOR >= 54
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   151
    if (g_pACodec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   152
#else
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   153
    if (g_pAudio->frame_size == 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   154
#endif
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   155
        g_NumSamples = 4096;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   156
    else
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   157
        g_NumSamples = g_pAudio->frame_size;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   158
    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
   159
    g_pAFrame = avcodec_alloc_frame();
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   160
    if (!g_pAFrame)
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   161
    {
7306
3cff5c769509 Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents: 7282
diff changeset
   162
        Log("Could not allocate frame\n");
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   163
        return;
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   164
    }
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   165
}
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
// returns non-zero if there is more sound
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   168
static int WriteAudioFrame()
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   169
{
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   170
    if (!g_pAStream)
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   171
        return 0;
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   172
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   173
    AVPacket Packet = { 0 };
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   174
    av_init_packet(&Packet);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   175
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   176
    int NumSamples = fread(g_pSamples, 2*g_Channels, g_NumSamples, g_pSoundFile);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   177
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   178
#if LIBAVCODEC_VERSION_MAJOR >= 54
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   179
    AVFrame* pFrame = NULL;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   180
    if (NumSamples > 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   181
    {
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   182
        g_pAFrame->nb_samples = NumSamples;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   183
        avcodec_fill_audio_frame(g_pAFrame, g_Channels, AV_SAMPLE_FMT_S16,
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   184
                                 (uint8_t*)g_pSamples, NumSamples*2*g_Channels, 1);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   185
        pFrame = g_pAFrame;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   186
    }
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   187
    // when NumSamples == 0 we still need to call encode_audio2 to flush
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   188
    int got_packet;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   189
    if (avcodec_encode_audio2(g_pAudio, &Packet, pFrame, &got_packet) != 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   190
        FatalError("avcodec_encode_audio2 failed");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   191
    if (!got_packet)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   192
        return 0;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   193
#else
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   194
    if (NumSamples == 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   195
        return 0;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   196
    int BufferSize = OUTBUFFER_SIZE;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   197
    if (g_pAudio->frame_size == 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   198
        BufferSize = NumSamples*g_Channels*2;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   199
    Packet.size = avcodec_encode_audio(g_pAudio, g_OutBuffer, BufferSize, g_pSamples);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   200
    if (Packet.size == 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   201
        return 1;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   202
    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
   203
        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
   204
    Packet.flags |= AV_PKT_FLAG_KEY;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   205
    Packet.data = g_OutBuffer;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   206
#endif
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
    // Write the compressed frame to the media file.
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   209
    Packet.stream_index = g_pAStream->index;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   210
    if (av_interleaved_write_frame(g_pContainer, &Packet) != 0) 
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   211
        FatalError("Error while writing audio frame");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   212
    return 1;
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
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   215
// add a video output stream
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   216
static void AddVideoStream()
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   217
{
7235
baa69bd025d9 1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents: 7198
diff changeset
   218
#if LIBAVFORMAT_VERSION_MAJOR >= 54
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   219
    g_pVStream = avformat_new_stream(g_pContainer, g_pVCodec);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   220
#else
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   221
    g_pVStream = av_new_stream(g_pContainer, 0);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   222
#endif
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   223
    if (!g_pVStream)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   224
        FatalError("Could not allocate video stream");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   225
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   226
    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
   227
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   228
    avcodec_get_context_defaults3(g_pVideo, g_pVCodec);
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   229
    g_pVideo->codec_id = g_pVCodec->id;
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   230
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   231
    // put parameters
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   232
    // 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
   233
    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
   234
    g_pVideo->height = g_Height & ~1; // make even
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   235
    /* 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
   236
       of which frame timestamps are represented. for fixed-fps content,
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   237
       timebase should be 1/framerate and timestamp increments should be
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   238
       identically 1. */
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   239
    g_pVideo->time_base.den = g_Framerate.num;
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   240
    g_pVideo->time_base.num = g_Framerate.den;
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   241
    //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
   242
    g_pVideo->pix_fmt = PIX_FMT_YUV420P;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   243
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   244
    // set quality
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   245
    if (g_VQuality > 100)
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   246
        g_pVideo->bit_rate = g_VQuality;
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   247
    else
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   248
    {
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   249
        g_pVideo->flags |= CODEC_FLAG_QSCALE;
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   250
        g_pVideo->global_quality = g_VQuality*FF_QP2LAMBDA;
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   251
    }
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   252
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   253
    // some formats want stream headers to be separate
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   254
    if (g_pFormat->flags & AVFMT_GLOBALHEADER)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   255
        g_pVideo->flags |= CODEC_FLAG_GLOBAL_HEADER;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   256
7376
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   257
#if LIBAVCODEC_VERSION_MAJOR < 54
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   258
    // for some versions of ffmpeg x264 options must be set explicitly
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   259
    if (strcmp(g_pVCodec->name, "libx264") == 0)
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   260
    {
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   261
        g_pVideo->coder_type = FF_CODER_TYPE_AC;
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   262
        g_pVideo->flags |= CODEC_FLAG_LOOP_FILTER;
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   263
        g_pVideo->crf = 23;
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   264
        g_pVideo->thread_count = 3;
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   265
        g_pVideo->me_cmp = FF_CMP_CHROMA;
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   266
        g_pVideo->partitions = X264_PART_I8X8 | X264_PART_I4X4 | X264_PART_P8X8 | X264_PART_B8X8;
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   267
        g_pVideo->me_method = ME_HEX;
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   268
        g_pVideo->me_subpel_quality = 7;
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   269
        g_pVideo->me_range = 16;
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   270
        g_pVideo->gop_size = 250;
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   271
        g_pVideo->keyint_min = 25;
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   272
        g_pVideo->scenechange_threshold = 40;
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   273
        g_pVideo->i_quant_factor = 0.71;
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   274
        g_pVideo->b_frame_strategy = 1;
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   275
        g_pVideo->qcompress = 0.6;
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   276
        g_pVideo->qmin = 10;
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   277
        g_pVideo->qmax = 51;
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   278
        g_pVideo->max_qdiff = 4;
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   279
        g_pVideo->max_b_frames = 3;
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   280
        g_pVideo->refs = 3;
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   281
        g_pVideo->directpred = 1;
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   282
        g_pVideo->trellis = 1;
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   283
        g_pVideo->flags2 = CODEC_FLAG2_BPYRAMID | CODEC_FLAG2_MIXED_REFS | CODEC_FLAG2_WPRED | CODEC_FLAG2_8X8DCT | CODEC_FLAG2_FASTPSKIP;
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   284
        g_pVideo->weighted_p_pred = 2;
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   285
    }
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   286
#endif
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   287
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   288
    // open the codec
7359
8bb4da06b065 Fix compilation of hwengine on ffmpeg 0.7.13.
Stepan777 <stepik-777@mail.ru>
parents: 7356
diff changeset
   289
#if LIBAVCODEC_VERSION_MAJOR >= 53
8bb4da06b065 Fix compilation of hwengine on ffmpeg 0.7.13.
Stepan777 <stepik-777@mail.ru>
parents: 7356
diff changeset
   290
    AVDictionary* pDict = NULL;
8bb4da06b065 Fix compilation of hwengine on ffmpeg 0.7.13.
Stepan777 <stepik-777@mail.ru>
parents: 7356
diff changeset
   291
    if (strcmp(g_pVCodec->name, "libx264") == 0)
7376
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   292
        av_dict_set(&pDict, "preset", "medium", 0);
7359
8bb4da06b065 Fix compilation of hwengine on ffmpeg 0.7.13.
Stepan777 <stepik-777@mail.ru>
parents: 7356
diff changeset
   293
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   294
    if (avcodec_open2(g_pVideo, g_pVCodec, &pDict) < 0)
7359
8bb4da06b065 Fix compilation of hwengine on ffmpeg 0.7.13.
Stepan777 <stepik-777@mail.ru>
parents: 7356
diff changeset
   295
#else
8bb4da06b065 Fix compilation of hwengine on ffmpeg 0.7.13.
Stepan777 <stepik-777@mail.ru>
parents: 7356
diff changeset
   296
    if (avcodec_open(g_pVideo, g_pVCodec) < 0)
8bb4da06b065 Fix compilation of hwengine on ffmpeg 0.7.13.
Stepan777 <stepik-777@mail.ru>
parents: 7356
diff changeset
   297
#endif
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   298
        FatalError("Could not open video codec %s", g_pVCodec->long_name);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   299
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   300
    g_pVFrame = avcodec_alloc_frame();
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   301
    if (!g_pVFrame)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   302
        FatalError("Could not allocate frame");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   303
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   304
    g_pVFrame->linesize[0] = g_Width;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   305
    g_pVFrame->linesize[1] = g_Width/2;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   306
    g_pVFrame->linesize[2] = g_Width/2;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   307
    g_pVFrame->linesize[3] = 0;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   308
}
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   309
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   310
static int WriteFrame(AVFrame* pFrame)
7180
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
    double AudioTime, VideoTime;
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
    // write interleaved audio frame
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   315
    if (g_pAStream)
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
        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
   318
        do
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   319
            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
   320
        while (AudioTime < VideoTime && WriteAudioFrame());
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   321
    }
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   322
    
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   323
    if (!g_pVStream)
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   324
        return 0;
7180
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
    AVPacket Packet;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   327
    av_init_packet(&Packet);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   328
    Packet.data = NULL;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   329
    Packet.size = 0;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   330
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   331
    g_pVFrame->pts++;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   332
    if (g_pFormat->flags & AVFMT_RAWPICTURE)
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
        /* raw video case. The API will change slightly in the near
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   335
           future for that. */
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   336
        Packet.flags |= AV_PKT_FLAG_KEY;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   337
        Packet.stream_index = g_pVStream->index;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   338
        Packet.data = (uint8_t*)pFrame;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   339
        Packet.size = sizeof(AVPicture);
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
        if (av_interleaved_write_frame(g_pContainer, &Packet) != 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   342
            FatalError("Error while writing video frame");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   343
        return 0;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   344
    }
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   345
    else
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
#if LIBAVCODEC_VERSION_MAJOR >= 54
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   348
        int got_packet;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   349
        if (avcodec_encode_video2(g_pVideo, &Packet, pFrame, &got_packet) < 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   350
            FatalError("avcodec_encode_video2 failed");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   351
        if (!got_packet)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   352
            return 0;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   353
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   354
        if (Packet.pts != AV_NOPTS_VALUE)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   355
            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
   356
        if (Packet.dts != AV_NOPTS_VALUE)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   357
            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
   358
#else 
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   359
        Packet.size = avcodec_encode_video(g_pVideo, g_OutBuffer, OUTBUFFER_SIZE, pFrame);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   360
        if (Packet.size < 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   361
            FatalError("avcodec_encode_video failed");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   362
        if (Packet.size == 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   363
            return 0;
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
        if( g_pVideo->coded_frame->pts != AV_NOPTS_VALUE)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   366
            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
   367
        if( g_pVideo->coded_frame->key_frame )
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   368
            Packet.flags |= AV_PKT_FLAG_KEY;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   369
        Packet.data = g_OutBuffer;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   370
#endif
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   371
        // write the compressed frame in the media file
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   372
        Packet.stream_index = g_pVStream->index;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   373
        if (av_interleaved_write_frame(g_pContainer, &Packet) != 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   374
            FatalError("Error while writing video frame");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   375
            
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   376
        return 1;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   377
    }
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
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   380
void AVWrapper_WriteFrame(uint8_t* pY, uint8_t* pCb, uint8_t* pCr)
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
    g_pVFrame->data[0] = pY;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   383
    g_pVFrame->data[1] = pCb;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   384
    g_pVFrame->data[2] = pCr;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   385
    WriteFrame(g_pVFrame);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   386
}
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   387
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   388
void AVWrapper_Init(
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   389
         void (*pAddFileLogRaw)(const char*),
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   390
         const char* pFilename,
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   391
         const char* pDesc,
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   392
         const char* pSoundFile,
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   393
         const char* pFormatName,
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   394
         const char* pVCodecName,
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   395
         const char* pACodecName,
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   396
         int Width, int Height,
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   397
         int FramerateNum, int FramerateDen,
7376
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7359
diff changeset
   398
         int VQuality)
7180
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
    AddFileLogRaw = pAddFileLogRaw;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   401
    av_log_set_callback( &LogCallback );
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   402
7282
c65992e2d794 correctly fix odd dimensions (it was incorrectly fixed in previous commit)
Stepan777 <stepik-777@mail.ru>
parents: 7280
diff changeset
   403
    g_Width  = Width;
c65992e2d794 correctly fix odd dimensions (it was incorrectly fixed in previous commit)
Stepan777 <stepik-777@mail.ru>
parents: 7280
diff changeset
   404
    g_Height = Height;
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   405
    g_Framerate.num = FramerateNum;
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   406
    g_Framerate.den = FramerateDen;
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   407
    g_VQuality = VQuality;
7180
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
    // initialize libav and register all codecs and formats
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   410
    av_register_all();
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   411
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   412
    // find format
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   413
    g_pFormat = av_guess_format(pFormatName, NULL, NULL);
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   414
    if (!g_pFormat)
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   415
        FatalError("Format \"%s\" was not found", pFormatName);
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   416
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   417
    // allocate the output media context
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   418
    g_pContainer = avformat_alloc_context();
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   419
    if (!g_pContainer)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   420
        FatalError("Could not allocate output context");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   421
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   422
    g_pContainer->oformat = g_pFormat;
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   423
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   424
    // store description of file
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   425
    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
   426
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   427
    // 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
   428
    char ext[16];
baa69bd025d9 1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents: 7198
diff changeset
   429
    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
   430
    ext[15] = 0;
baa69bd025d9 1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents: 7198
diff changeset
   431
    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
   432
    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
   433
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   434
    // find codecs
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   435
    g_pVCodec = avcodec_find_encoder_by_name(pVCodecName);
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   436
    g_pACodec = avcodec_find_encoder_by_name(pACodecName);
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   437
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   438
    // add audio and video stream to container
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   439
    g_pVStream = NULL;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   440
    g_pAStream = NULL;
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   441
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   442
    if (g_pVCodec)
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   443
        AddVideoStream();
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   444
    else
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   445
        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
   446
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   447
    if (g_pACodec)
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   448
    {
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   449
        g_pSoundFile = fopen(pSoundFile, "rb");
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   450
        if (g_pSoundFile)
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   451
        {
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   452
            fread(&g_Frequency, 4, 1, g_pSoundFile);
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   453
            fread(&g_Channels, 4, 1, g_pSoundFile);
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   454
            AddAudioStream();
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   455
        }
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   456
        else
7306
3cff5c769509 Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents: 7282
diff changeset
   457
            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
   458
    }
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   459
    else
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   460
        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
   461
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   462
    if (!g_pAStream && !g_pVStream)
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   463
        FatalError("No video, no audio, aborting...");
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   464
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   465
    // write format info to log
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   466
    av_dump_format(g_pContainer, 0, g_pContainer->filename, 1);
7180
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
    // open the output file, if needed
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   469
    if (!(g_pFormat->flags & AVFMT_NOFILE))
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   470
    {
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   471
        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
   472
            FatalError("Could not open output file (%s)", g_pContainer->filename);
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   473
    }
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   474
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   475
    // write the stream header, if any
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   476
    avformat_write_header(g_pContainer, NULL);
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   477
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   478
    g_pVFrame->pts = -1;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   479
}
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   480
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   481
void AVWrapper_Close()
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   482
{
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   483
    // output buffered frames
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   484
    if (g_pVCodec->capabilities & CODEC_CAP_DELAY)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   485
        while( WriteFrame(NULL) );
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   486
    // output any remaining audio
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   487
    while( WriteAudioFrame() );
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   488
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   489
    // write the trailer, if any.
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   490
    av_write_trailer(g_pContainer);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   491
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   492
    // close the output file
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   493
    if (!(g_pFormat->flags & AVFMT_NOFILE))
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   494
        avio_close(g_pContainer->pb);
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   495
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   496
    // free everything
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   497
    if (g_pVStream)
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   498
    {
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   499
        avcodec_close(g_pVideo);
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   500
        av_free(g_pVideo);
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   501
        av_free(g_pVStream);
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   502
        av_free(g_pVFrame);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   503
    }
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   504
    if (g_pAStream)
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   505
    {
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   506
        avcodec_close(g_pAudio);
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   507
        av_free(g_pAudio);
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   508
        av_free(g_pAStream);
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   509
        av_free(g_pAFrame);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   510
        av_free(g_pSamples);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   511
        fclose(g_pSoundFile);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   512
    }
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   513
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   514
    av_free(g_pContainer);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   515
}