hedgewars/avwrapper/avwrapper.c
author sheepluva
Sun, 02 Oct 2016 01:26:59 +0200
changeset 11860 0abbe6f1e4f2
parent 11807 93621207563b
child 13537 ecdf6ce2301e
permissions -rw-r--r--
increase heart count in seduction effect. FPS are overrated anyway
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
11046
47a8c19ecb60 more copyright fixes
sheepluva
parents: 10108
diff changeset
     3
 * Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com>
7542
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
10108
c68cf030eded update FSF address. note: two sdl include files (by Sam Lantinga) still have the old FSF address in their copyright - but I ain't gonna touch their copyright headers
sheepluva
parents: 9998
diff changeset
    16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
7542
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>
11298
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
    24
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
    25
#include "libavcodec/avcodec.h"
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    26
#include "libavformat/avformat.h"
11298
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
    27
#include "libavutil/avutil.h"
8065
609d07b337b5 fix deprecation warnings for libav sources
koda
parents: 7542
diff changeset
    28
#include "libavutil/mathematics.h"
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    29
9203
bcc25cb9b122 and now also avwrapper can be shared/static, add visibility to its exported functions, drop (needed only when static)
koda
parents: 9080
diff changeset
    30
#if (defined _MSC_VER)
bcc25cb9b122 and now also avwrapper can be shared/static, add visibility to its exported functions, drop (needed only when static)
koda
parents: 9080
diff changeset
    31
#define AVWRAP_DECL __declspec(dllexport)
bcc25cb9b122 and now also avwrapper can be shared/static, add visibility to its exported functions, drop (needed only when static)
koda
parents: 9080
diff changeset
    32
#elif ((__GNUC__ >= 3) && (!__EMX__) && (!sun))
bcc25cb9b122 and now also avwrapper can be shared/static, add visibility to its exported functions, drop (needed only when static)
koda
parents: 9080
diff changeset
    33
#define AVWRAP_DECL __attribute__((visibility("default")))
bcc25cb9b122 and now also avwrapper can be shared/static, add visibility to its exported functions, drop (needed only when static)
koda
parents: 9080
diff changeset
    34
#else
bcc25cb9b122 and now also avwrapper can be shared/static, add visibility to its exported functions, drop (needed only when static)
koda
parents: 9080
diff changeset
    35
#define AVWRAP_DECL
bcc25cb9b122 and now also avwrapper can be shared/static, add visibility to its exported functions, drop (needed only when static)
koda
parents: 9080
diff changeset
    36
#endif
bcc25cb9b122 and now also avwrapper can be shared/static, add visibility to its exported functions, drop (needed only when static)
koda
parents: 9080
diff changeset
    37
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    38
static AVFormatContext* g_pContainer;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    39
static AVOutputFormat* g_pFormat;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    40
static AVStream* g_pAStream;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    41
static AVStream* g_pVStream;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    42
static AVFrame* g_pAFrame;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    43
static AVFrame* g_pVFrame;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    44
static AVCodec* g_pACodec;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    45
static AVCodec* g_pVCodec;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    46
static AVCodecContext* g_pAudio;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    47
static AVCodecContext* g_pVideo;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    48
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
    49
static int g_Width, g_Height;
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    50
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
    51
static int g_VQuality;
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
    52
static AVRational g_Framerate;
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    53
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    54
static FILE* g_pSoundFile;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    55
static int16_t* g_pSamples;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    56
static int g_NumSamples;
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
11298
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
    59
// compatibility section
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    60
#if LIBAVCODEC_VERSION_MAJOR < 54
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    61
#define OUTBUFFER_SIZE 200000
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    62
static uint8_t g_OutBuffer[OUTBUFFER_SIZE];
11298
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
    63
#define avcodec_open2(x, y, z)              avcodec_open(x, y)
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    64
#endif
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    65
11614
3c959df6638e Add an additional compatibility layer for video
koda
parents: 11612
diff changeset
    66
#if LIBAVCODEC_VERSION_MAJOR < 55
3c959df6638e Add an additional compatibility layer for video
koda
parents: 11612
diff changeset
    67
#define avcodec_default_get_buffer2(x, y ,z) avcodec_default_get_buffer(x, y)
3c959df6638e Add an additional compatibility layer for video
koda
parents: 11612
diff changeset
    68
#endif
3c959df6638e Add an additional compatibility layer for video
koda
parents: 11612
diff changeset
    69
11298
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
    70
#if LIBAVCODEC_VERSION_MAJOR < 56
11621
968bff2534ca Keep very old compatibility
koda
parents: 11619
diff changeset
    71
#if LIBAVCODEC_VERSION_MAJOR < 55
11629
c2e6df2cfac7 Fix function name in avwrapper
koda
parents: 11621
diff changeset
    72
#define av_frame_free                       av_freep
11621
968bff2534ca Keep very old compatibility
koda
parents: 11619
diff changeset
    73
#else
968bff2534ca Keep very old compatibility
koda
parents: 11619
diff changeset
    74
#define av_frame_free                       avcodec_free_frame
968bff2534ca Keep very old compatibility
koda
parents: 11619
diff changeset
    75
#endif
968bff2534ca Keep very old compatibility
koda
parents: 11619
diff changeset
    76
11298
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
    77
#define av_frame_alloc                      avcodec_alloc_frame
11618
bc83e11e5ee7 More compatibility wrappers for video
koda
parents: 11614
diff changeset
    78
#define av_frame_unref                      avcodec_get_frame_defaults
11351
82784d932577 videorec: Appropriately rescale timestamps and duration
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11350
diff changeset
    79
#define av_packet_rescale_ts                rescale_ts
82784d932577 videorec: Appropriately rescale timestamps and duration
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11350
diff changeset
    80
82784d932577 videorec: Appropriately rescale timestamps and duration
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11350
diff changeset
    81
static void rescale_ts(AVPacket *pkt, AVRational ctb, AVRational stb)
82784d932577 videorec: Appropriately rescale timestamps and duration
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11350
diff changeset
    82
{
82784d932577 videorec: Appropriately rescale timestamps and duration
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11350
diff changeset
    83
    if (pkt->pts != AV_NOPTS_VALUE)
82784d932577 videorec: Appropriately rescale timestamps and duration
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11350
diff changeset
    84
        pkt->pts = av_rescale_q(pkt->pts, ctb, stb);
82784d932577 videorec: Appropriately rescale timestamps and duration
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11350
diff changeset
    85
    if (pkt->dts != AV_NOPTS_VALUE)
82784d932577 videorec: Appropriately rescale timestamps and duration
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11350
diff changeset
    86
        pkt->dts = av_rescale_q(pkt->dts, ctb, stb);
82784d932577 videorec: Appropriately rescale timestamps and duration
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11350
diff changeset
    87
    if (pkt->duration > 0)
82784d932577 videorec: Appropriately rescale timestamps and duration
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11350
diff changeset
    88
        pkt->duration = av_rescale_q(pkt->duration, ctb, stb);
82784d932577 videorec: Appropriately rescale timestamps and duration
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11350
diff changeset
    89
}
11298
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
    90
#endif
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
    91
11401
7012234df6c4 avwrapper: Explicitly check for macro existence
koda
parents: 11396
diff changeset
    92
#ifndef AV_CODEC_CAP_DELAY
11298
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
    93
#define AV_CODEC_CAP_DELAY                  CODEC_CAP_DELAY
11401
7012234df6c4 avwrapper: Explicitly check for macro existence
koda
parents: 11396
diff changeset
    94
#endif
7012234df6c4 avwrapper: Explicitly check for macro existence
koda
parents: 11396
diff changeset
    95
#ifndef AV_CODEC_CAP_VARIABLE_FRAME_SIZE
11298
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
    96
#define AV_CODEC_CAP_VARIABLE_FRAME_SIZE    CODEC_CAP_VARIABLE_FRAME_SIZE
11401
7012234df6c4 avwrapper: Explicitly check for macro existence
koda
parents: 11396
diff changeset
    97
#endif
7012234df6c4 avwrapper: Explicitly check for macro existence
koda
parents: 11396
diff changeset
    98
#ifndef AV_CODEC_FLAG_GLOBAL_HEADER
11298
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
    99
#define AV_CODEC_FLAG_GLOBAL_HEADER         CODEC_FLAG_GLOBAL_HEADER
11401
7012234df6c4 avwrapper: Explicitly check for macro existence
koda
parents: 11396
diff changeset
   100
#endif
7012234df6c4 avwrapper: Explicitly check for macro existence
koda
parents: 11396
diff changeset
   101
#ifndef AV_CODEC_FLAG_QSCALE
11298
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
   102
#define AV_CODEC_FLAG_QSCALE                CODEC_FLAG_QSCALE
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
   103
#endif
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
   104
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
   105
#if LIBAVFORMAT_VERSION_MAJOR < 53
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
   106
#define AVIO_FLAG_WRITE                     AVIO_WRONLY
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
   107
#endif
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
   108
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
   109
#if LIBAVFORMAT_VERSION_MAJOR < 54
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
   110
#define avformat_new_stream(x, y)           av_new_stream(x, y->type == AVMEDIA_TYPE_AUDIO)
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
   111
#endif
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
   112
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
   113
#if LIBAVUTIL_VERSION_MAJOR < 54
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
   114
#define AV_PIX_FMT_YUV420P                  PIX_FMT_YUV420P
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
   115
#endif
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
   116
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
   117
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   118
// pointer to function from hwengine (uUtils.pas)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   119
static void (*AddFileLogRaw)(const char* pString);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   120
9925
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   121
static int FatalError(const char* pFmt, ...)
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   122
{
8065
609d07b337b5 fix deprecation warnings for libav sources
koda
parents: 7542
diff changeset
   123
    char Buffer[1024];
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   124
    va_list VaArgs;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   125
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   126
    va_start(VaArgs, pFmt);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   127
    vsnprintf(Buffer, 1024, pFmt, VaArgs);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   128
    va_end(VaArgs);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   129
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   130
    AddFileLogRaw("Error in av-wrapper: ");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   131
    AddFileLogRaw(Buffer);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   132
    AddFileLogRaw("\n");
9925
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   133
    return(-1);
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   134
}
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   135
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   136
// Function to be called from libav for logging.
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   137
// Note: libav can call LogCallback from different threads
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   138
// (there is mutex in AddFileLogRaw).
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   139
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
   140
{
8065
609d07b337b5 fix deprecation warnings for libav sources
koda
parents: 7542
diff changeset
   141
    char Buffer[1024];
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   142
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   143
    vsnprintf(Buffer, 1024, pFmt, VaArgs);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   144
    AddFileLogRaw(Buffer);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   145
}
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   146
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   147
static void Log(const char* pFmt, ...)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   148
{
8065
609d07b337b5 fix deprecation warnings for libav sources
koda
parents: 7542
diff changeset
   149
    char Buffer[1024];
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   150
    va_list VaArgs;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   151
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   152
    va_start(VaArgs, pFmt);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   153
    vsnprintf(Buffer, 1024, pFmt, VaArgs);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   154
    va_end(VaArgs);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   155
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   156
    AddFileLogRaw(Buffer);
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
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   159
static void AddAudioStream()
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   160
{
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   161
    g_pAStream = avformat_new_stream(g_pContainer, g_pACodec);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   162
    if(!g_pAStream)
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   163
    {
7306
3cff5c769509 Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents: 7282
diff changeset
   164
        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
   165
        return;
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   166
    }
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   167
    g_pAStream->id = 1;
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
    g_pAudio = g_pAStream->codec;
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   170
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   171
    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
   172
    g_pAudio->codec_id = g_pACodec->id;
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   173
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   174
    // put parameters
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   175
    g_pAudio->sample_fmt = AV_SAMPLE_FMT_S16;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   176
    g_pAudio->sample_rate = g_Frequency;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   177
    g_pAudio->channels = g_Channels;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   178
11350
846aa36f7cdc videorec: Always initialize audio and video time bases
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11349
diff changeset
   179
    // set time base as invers of sample rate
846aa36f7cdc videorec: Always initialize audio and video time bases
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11349
diff changeset
   180
    g_pAudio->time_base.den = g_pAStream->time_base.den = g_Frequency;
846aa36f7cdc videorec: Always initialize audio and video time bases
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11349
diff changeset
   181
    g_pAudio->time_base.num = g_pAStream->time_base.num = 1;
846aa36f7cdc videorec: Always initialize audio and video time bases
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11349
diff changeset
   182
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   183
    // 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
   184
    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
   185
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
   186
    // for codecs that support variable bitrate use it, it should be better
11298
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
   187
    g_pAudio->flags |= AV_CODEC_FLAG_QSCALE;
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
   188
    g_pAudio->global_quality = 1*FF_QP2LAMBDA;
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   189
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   190
    // some formats want stream headers to be separate
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   191
    if (g_pFormat->flags & AVFMT_GLOBALHEADER)
11298
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
   192
        g_pAudio->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   193
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   194
    // open it
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   195
    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
   196
    {
7306
3cff5c769509 Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents: 7282
diff changeset
   197
        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
   198
        return;
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   199
    }
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   200
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   201
#if LIBAVCODEC_VERSION_MAJOR >= 54
11298
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
   202
    if (g_pACodec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   203
#else
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   204
    if (g_pAudio->frame_size == 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   205
#endif
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   206
        g_NumSamples = 4096;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   207
    else
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   208
        g_NumSamples = g_pAudio->frame_size;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   209
    g_pSamples = (int16_t*)av_malloc(g_NumSamples*g_Channels*sizeof(int16_t));
11298
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
   210
    g_pAFrame = av_frame_alloc();
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   211
    if (!g_pAFrame)
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   212
    {
7306
3cff5c769509 Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents: 7282
diff changeset
   213
        Log("Could not allocate frame\n");
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   214
        return;
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   215
    }
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   216
}
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   217
9925
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   218
// returns non-zero if there is more sound, -1 in case of error
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   219
static int WriteAudioFrame()
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   220
{
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   221
    if (!g_pAStream)
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   222
        return 0;
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   223
11300
f9fa0d6dc69f Let initialization happen in the appropriate function
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11298
diff changeset
   224
    AVPacket Packet;
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   225
    av_init_packet(&Packet);
11347
f830c080aa9d videorec: Make sure the audio packet is considered empty
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11316
diff changeset
   226
    Packet.data = NULL;
f830c080aa9d videorec: Make sure the audio packet is considered empty
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11316
diff changeset
   227
    Packet.size = 0;
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   228
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   229
    int NumSamples = fread(g_pSamples, 2*g_Channels, g_NumSamples, g_pSoundFile);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   230
8065
609d07b337b5 fix deprecation warnings for libav sources
koda
parents: 7542
diff changeset
   231
#if LIBAVCODEC_VERSION_MAJOR >= 53
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   232
    AVFrame* pFrame = NULL;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   233
    if (NumSamples > 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   234
    {
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   235
        g_pAFrame->nb_samples = NumSamples;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   236
        avcodec_fill_audio_frame(g_pAFrame, g_Channels, AV_SAMPLE_FMT_S16,
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   237
                                 (uint8_t*)g_pSamples, NumSamples*2*g_Channels, 1);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   238
        pFrame = g_pAFrame;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   239
    }
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   240
    // when NumSamples == 0 we still need to call encode_audio2 to flush
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   241
    int got_packet;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   242
    if (avcodec_encode_audio2(g_pAudio, &Packet, pFrame, &got_packet) != 0)
9925
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   243
        return FatalError("avcodec_encode_audio2 failed");
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   244
    if (!got_packet)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   245
        return 0;
11351
82784d932577 videorec: Appropriately rescale timestamps and duration
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11350
diff changeset
   246
82784d932577 videorec: Appropriately rescale timestamps and duration
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11350
diff changeset
   247
    av_packet_rescale_ts(&Packet, g_pAudio->time_base, g_pAStream->time_base);
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   248
#else
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   249
    if (NumSamples == 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   250
        return 0;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   251
    int BufferSize = OUTBUFFER_SIZE;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   252
    if (g_pAudio->frame_size == 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   253
        BufferSize = NumSamples*g_Channels*2;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   254
    Packet.size = avcodec_encode_audio(g_pAudio, g_OutBuffer, BufferSize, g_pSamples);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   255
    if (Packet.size == 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   256
        return 1;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   257
    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
   258
        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
   259
    Packet.flags |= AV_PKT_FLAG_KEY;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   260
    Packet.data = g_OutBuffer;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   261
#endif
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   262
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   263
    // Write the compressed frame to the media file.
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   264
    Packet.stream_index = g_pAStream->index;
8811
dcdfcddf37ee move avwraper compilation in its own folder
koda
parents: 8065
diff changeset
   265
    if (av_interleaved_write_frame(g_pContainer, &Packet) != 0)
9925
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   266
        return FatalError("Error while writing audio frame");
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   267
    return 1;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   268
}
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   269
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   270
// add a video output stream
9925
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   271
static int AddVideoStream()
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
    g_pVStream = avformat_new_stream(g_pContainer, g_pVCodec);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   274
    if (!g_pVStream)
9925
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   275
        return FatalError("Could not allocate video stream");
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   276
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   277
    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
   278
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   279
    avcodec_get_context_defaults3(g_pVideo, g_pVCodec);
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   280
    g_pVideo->codec_id = g_pVCodec->id;
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   281
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   282
    // put parameters
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   283
    // 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
   284
    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
   285
    g_pVideo->height = g_Height & ~1; // make even
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   286
    /* 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
   287
       of which frame timestamps are represented. for fixed-fps content,
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   288
       timebase should be 1/framerate and timestamp increments should be
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   289
       identically 1. */
11350
846aa36f7cdc videorec: Always initialize audio and video time bases
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11349
diff changeset
   290
    g_pVideo->time_base.den = g_pVStream->time_base.den = g_Framerate.num;
846aa36f7cdc videorec: Always initialize audio and video time bases
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11349
diff changeset
   291
    g_pVideo->time_base.num = g_pVStream->time_base.num = g_Framerate.den;
11353
7ab863821864 videorec: Drop a commented out option
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11352
diff changeset
   292
11298
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
   293
    g_pVideo->pix_fmt = AV_PIX_FMT_YUV420P;
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   294
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   295
    // set quality
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   296
    if (g_VQuality > 100)
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   297
        g_pVideo->bit_rate = g_VQuality;
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   298
    else
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   299
    {
11298
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
   300
        g_pVideo->flags |= AV_CODEC_FLAG_QSCALE;
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   301
        g_pVideo->global_quality = g_VQuality*FF_QP2LAMBDA;
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   302
    }
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   303
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   304
    // some formats want stream headers to be separate
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   305
    if (g_pFormat->flags & AVFMT_GLOBALHEADER)
11298
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
   306
        g_pVideo->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   307
8065
609d07b337b5 fix deprecation warnings for libav sources
koda
parents: 7542
diff changeset
   308
#if LIBAVCODEC_VERSION_MAJOR < 53
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
   309
    // 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
   310
    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
   311
    {
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
   312
        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
   313
        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
   314
        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
   315
        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
   316
        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
   317
        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
   318
        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
   319
        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
   320
        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
   321
        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
   322
        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
   323
        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
   324
        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
   325
        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
   326
        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
   327
        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
   328
        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
   329
        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
   330
        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
   331
        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
   332
        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
   333
        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
   334
        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
   335
        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
   336
    }
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
   337
#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
   338
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   339
    // open the codec
11316
516e9b1ac889 videorec: Drop setting x264 preset
koda
parents: 11300
diff changeset
   340
    if (avcodec_open2(g_pVideo, g_pVCodec, NULL) < 0)
9925
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   341
        return FatalError("Could not open video codec %s", g_pVCodec->long_name);
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   342
11298
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
   343
    g_pVFrame = av_frame_alloc();
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   344
    if (!g_pVFrame)
9925
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   345
        return FatalError("Could not allocate frame");
11612
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   346
    av_frame_unref(g_pVFrame);
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   347
11349
dc17a495ee7f videorec: Fully set frame properties
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11348
diff changeset
   348
    g_pVFrame->width = g_Width;
dc17a495ee7f videorec: Fully set frame properties
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11348
diff changeset
   349
    g_pVFrame->height = g_Height;
dc17a495ee7f videorec: Fully set frame properties
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11348
diff changeset
   350
    g_pVFrame->format = AV_PIX_FMT_YUV420P;
11612
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   351
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   352
    return avcodec_default_get_buffer2(g_pVideo, g_pVFrame, 0);
7180
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
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   355
static int WriteFrame(AVFrame* pFrame)
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   356
{
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   357
    double AudioTime, VideoTime;
9925
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   358
    int ret;
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   359
    // write interleaved audio frame
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   360
    if (g_pAStream)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   361
    {
11298
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
   362
        VideoTime = (double)g_pVFrame->pts * g_pVStream->time_base.num/g_pVStream->time_base.den;
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   363
        do
9925
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   364
        {
11298
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
   365
            AudioTime = (double)g_pAFrame->pts * g_pAStream->time_base.num/g_pAStream->time_base.den;
9925
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   366
            ret = WriteAudioFrame();
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   367
        }
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   368
        while (AudioTime < VideoTime && ret);
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   369
        if (ret < 0)
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   370
            return ret;
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   371
    }
8811
dcdfcddf37ee move avwraper compilation in its own folder
koda
parents: 8065
diff changeset
   372
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   373
    if (!g_pVStream)
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   374
        return 0;
7180
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
    AVPacket Packet;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   377
    av_init_packet(&Packet);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   378
    Packet.data = NULL;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   379
    Packet.size = 0;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   380
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   381
    g_pVFrame->pts++;
11352
ce154ffb9a8b videorec: Use future API for raw video encoding
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11351
diff changeset
   382
#if LIBAVCODEC_VERSION_MAJOR < 58
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   383
    if (g_pFormat->flags & AVFMT_RAWPICTURE)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   384
    {
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   385
        /* raw video case. The API will change slightly in the near
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   386
           future for that. */
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   387
        Packet.flags |= AV_PKT_FLAG_KEY;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   388
        Packet.stream_index = g_pVStream->index;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   389
        Packet.data = (uint8_t*)pFrame;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   390
        Packet.size = sizeof(AVPicture);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   391
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   392
        if (av_interleaved_write_frame(g_pContainer, &Packet) != 0)
9925
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   393
            return FatalError("Error while writing video frame");
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   394
        return 0;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   395
    }
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   396
    else
11352
ce154ffb9a8b videorec: Use future API for raw video encoding
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11351
diff changeset
   397
#endif
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   398
    {
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   399
#if LIBAVCODEC_VERSION_MAJOR >= 54
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   400
        int got_packet;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   401
        if (avcodec_encode_video2(g_pVideo, &Packet, pFrame, &got_packet) < 0)
9925
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   402
            return FatalError("avcodec_encode_video2 failed");
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   403
        if (!got_packet)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   404
            return 0;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   405
11351
82784d932577 videorec: Appropriately rescale timestamps and duration
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11350
diff changeset
   406
        av_packet_rescale_ts(&Packet, g_pVideo->time_base, g_pVStream->time_base);
8811
dcdfcddf37ee move avwraper compilation in its own folder
koda
parents: 8065
diff changeset
   407
#else
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   408
        Packet.size = avcodec_encode_video(g_pVideo, g_OutBuffer, OUTBUFFER_SIZE, pFrame);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   409
        if (Packet.size < 0)
9925
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   410
            return FatalError("avcodec_encode_video failed");
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   411
        if (Packet.size == 0)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   412
            return 0;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   413
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   414
        if( g_pVideo->coded_frame->pts != AV_NOPTS_VALUE)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   415
            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
   416
        if( g_pVideo->coded_frame->key_frame )
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   417
            Packet.flags |= AV_PKT_FLAG_KEY;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   418
        Packet.data = g_OutBuffer;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   419
#endif
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   420
        // write the compressed frame in the media file
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   421
        Packet.stream_index = g_pVStream->index;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   422
        if (av_interleaved_write_frame(g_pContainer, &Packet) != 0)
9925
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   423
            return FatalError("Error while writing video frame");
8811
dcdfcddf37ee move avwraper compilation in its own folder
koda
parents: 8065
diff changeset
   424
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   425
        return 1;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   426
    }
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   427
}
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   428
11612
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   429
AVWRAP_DECL int AVWrapper_WriteFrame(uint8_t *buf)
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   430
{
11612
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   431
    int x, y, stride = g_Width * 4;
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   432
    uint8_t *data[3];
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   433
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   434
    // copy pointers, prepare source
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   435
    memcpy(data, g_pVFrame->data, sizeof(data));
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   436
    buf += (g_Height - 1) * stride;
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   437
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   438
    // convert to YUV 4:2:0
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   439
    for (y = 0; y < g_Height; y++) {
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   440
        for (x = 0; x < g_Width; x++) {
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   441
            int r = buf[x * 4 + 0];
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   442
            int g = buf[x * 4 + 1];
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   443
            int b = buf[x * 4 + 2];
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   444
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   445
            int luma = (int)(0.299f * r +  0.587f * g + 0.114f * b);
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   446
            data[0][x] = av_clip_uint8(luma);
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   447
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   448
            if (!(x & 1) && !(y & 1)) {
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   449
                int r = (buf[x * 4 + 0]          + buf[(x + 1) * 4 + 0] +
11807
93621207563b avrec crash/math fix (sheepy here, committing koda's fix because I'm tired of having it lying around in my repo :P
koda
parents: 11629
diff changeset
   450
                         buf[x * 4 + 0 - stride] + buf[(x + 1) * 4 + 0 - stride]) / 4;
11612
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   451
                int g = (buf[x * 4 + 1]          + buf[(x + 1) * 4 + 1] +
11807
93621207563b avrec crash/math fix (sheepy here, committing koda's fix because I'm tired of having it lying around in my repo :P
koda
parents: 11629
diff changeset
   452
                         buf[x * 4 + 1 - stride] + buf[(x + 1) * 4 + 1 - stride]) / 4;
11612
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   453
                int b = (buf[x * 4 + 2]          + buf[(x + 1) * 4 + 2] +
11807
93621207563b avrec crash/math fix (sheepy here, committing koda's fix because I'm tired of having it lying around in my repo :P
koda
parents: 11629
diff changeset
   454
                         buf[x * 4 + 2 - stride] + buf[(x + 1) * 4 + 2 - stride]) / 4;
11612
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   455
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   456
                int cr = (int)(-0.14713f * r - 0.28886f * g + 0.436f   * b);
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   457
                int cb = (int)( 0.615f   * r - 0.51499f * g - 0.10001f * b);
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   458
                data[1][x / 2] = av_clip_uint8(128 + cr);
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   459
                data[2][x / 2] = av_clip_uint8(128 + cb);
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   460
            }
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   461
        }
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   462
        buf += -stride;
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   463
        data[0] += g_pVFrame->linesize[0];
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   464
        if (y & 1) {
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   465
            data[1] += g_pVFrame->linesize[1];
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   466
            data[2] += g_pVFrame->linesize[2];
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   467
        }
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   468
    }
b7d5d75469ee Move pixel format conversion from uVideoRec to AVWrapper
koda
parents: 11543
diff changeset
   469
9925
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   470
    return WriteFrame(g_pVFrame);
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   471
}
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   472
9925
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   473
AVWRAP_DECL int AVWrapper_Init(
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   474
         void (*pAddFileLogRaw)(const char*),
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   475
         const char* pFilename,
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   476
         const char* pDesc,
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   477
         const char* pSoundFile,
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   478
         const char* pFormatName,
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   479
         const char* pVCodecName,
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   480
         const char* pACodecName,
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   481
         int Width, int Height,
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   482
         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
   483
         int VQuality)
8811
dcdfcddf37ee move avwraper compilation in its own folder
koda
parents: 8065
diff changeset
   484
{
9925
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   485
    int ret;
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   486
    AddFileLogRaw = pAddFileLogRaw;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   487
    av_log_set_callback( &LogCallback );
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   488
7282
c65992e2d794 correctly fix odd dimensions (it was incorrectly fixed in previous commit)
Stepan777 <stepik-777@mail.ru>
parents: 7280
diff changeset
   489
    g_Width  = Width;
c65992e2d794 correctly fix odd dimensions (it was incorrectly fixed in previous commit)
Stepan777 <stepik-777@mail.ru>
parents: 7280
diff changeset
   490
    g_Height = Height;
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   491
    g_Framerate.num = FramerateNum;
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   492
    g_Framerate.den = FramerateDen;
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   493
    g_VQuality = VQuality;
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   494
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   495
    // initialize libav and register all codecs and formats
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   496
    av_register_all();
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   497
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   498
    // find format
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   499
    g_pFormat = av_guess_format(pFormatName, NULL, NULL);
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   500
    if (!g_pFormat)
9925
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   501
        return FatalError("Format \"%s\" was not found", pFormatName);
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   502
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   503
    // allocate the output media context
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   504
    g_pContainer = avformat_alloc_context();
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   505
    if (!g_pContainer)
9925
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   506
        return FatalError("Could not allocate output context");
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   507
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   508
    g_pContainer->oformat = g_pFormat;
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   509
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   510
    // store description of file
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   511
    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
   512
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   513
    // 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
   514
    char ext[16];
baa69bd025d9 1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents: 7198
diff changeset
   515
    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
   516
    ext[15] = 0;
baa69bd025d9 1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents: 7198
diff changeset
   517
    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
   518
    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
   519
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   520
    // find codecs
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   521
    g_pVCodec = avcodec_find_encoder_by_name(pVCodecName);
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   522
    g_pACodec = avcodec_find_encoder_by_name(pACodecName);
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   523
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   524
    // add audio and video stream to container
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   525
    g_pVStream = NULL;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   526
    g_pAStream = NULL;
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   527
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   528
    if (g_pVCodec)
9925
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   529
    {
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   530
        ret = AddVideoStream();
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   531
        if (ret < 0)
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   532
            return ret;
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   533
    }
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   534
    else
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   535
        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
   536
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   537
    if (g_pACodec)
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   538
    {
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   539
        g_pSoundFile = fopen(pSoundFile, "rb");
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   540
        if (g_pSoundFile)
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   541
        {
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   542
            fread(&g_Frequency, 4, 1, g_pSoundFile);
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   543
            fread(&g_Channels, 4, 1, g_pSoundFile);
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   544
            AddAudioStream();
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   545
        }
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   546
        else
7306
3cff5c769509 Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents: 7282
diff changeset
   547
            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
   548
    }
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   549
    else
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   550
        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
   551
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   552
    if (!g_pAStream && !g_pVStream)
9925
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   553
        return FatalError("No video, no audio, aborting...");
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   554
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   555
    // write format info to log
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   556
    av_dump_format(g_pContainer, 0, g_pContainer->filename, 1);
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   557
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   558
    // open the output file, if needed
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   559
    if (!(g_pFormat->flags & AVFMT_NOFILE))
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   560
    {
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   561
        if (avio_open(&g_pContainer->pb, g_pContainer->filename, AVIO_FLAG_WRITE) < 0)
9925
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   562
            return FatalError("Could not open output file (%s)", g_pContainer->filename);
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   563
    }
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   564
11543
3617c611406b avwrapper: Do not ignore avformat_write_header return value
koda
parents: 11401
diff changeset
   565
    g_pVFrame->pts = -1;
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   566
11543
3617c611406b avwrapper: Do not ignore avformat_write_header return value
koda
parents: 11401
diff changeset
   567
    // write the stream header, if any
3617c611406b avwrapper: Do not ignore avformat_write_header return value
koda
parents: 11401
diff changeset
   568
    return avformat_write_header(g_pContainer, NULL);
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   569
}
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   570
9925
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   571
AVWRAP_DECL int AVWrapper_Close()
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   572
{
9925
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   573
    int ret;
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   574
    // output buffered frames
11298
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
   575
    if (g_pVCodec->capabilities & AV_CODEC_CAP_DELAY)
9925
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   576
    {
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   577
        do
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   578
            ret = WriteFrame(NULL);
11348
44248a9cdcac videorec: Fix flushing encoders
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11347
diff changeset
   579
        while (ret > 0);
9925
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   580
        if (ret < 0)
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   581
            return ret;
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   582
    }
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   583
    // output any remaining audio
9925
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   584
    do
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   585
    {
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   586
        ret = WriteAudioFrame();
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   587
    }
11348
44248a9cdcac videorec: Fix flushing encoders
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11347
diff changeset
   588
    while(ret > 0);
9925
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   589
    if (ret < 0)
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   590
        return ret;
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   591
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   592
    // write the trailer, if any.
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   593
    av_write_trailer(g_pContainer);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   594
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   595
    // close the output file
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   596
    if (!(g_pFormat->flags & AVFMT_NOFILE))
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   597
        avio_close(g_pContainer->pb);
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   598
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   599
    // free everything
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   600
    if (g_pVStream)
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   601
    {
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   602
        avcodec_close(g_pVideo);
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   603
        av_free(g_pVideo);
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   604
        av_free(g_pVStream);
11298
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
   605
        av_frame_free(&g_pVFrame);
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   606
    }
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   607
    if (g_pAStream)
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   608
    {
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   609
        avcodec_close(g_pAudio);
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   610
        av_free(g_pAudio);
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   611
        av_free(g_pAStream);
11298
9fcdf0969d53 Update libav API, keeping forward and backward compatibility
Vittorio Giovara <vittorio.giovara@gmail.com>
parents: 11046
diff changeset
   612
        av_frame_free(&g_pAFrame);
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   613
        av_free(g_pSamples);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   614
        fclose(g_pSoundFile);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   615
    }
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   616
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   617
    av_free(g_pContainer);
9925
113e61a8cadc Fixed avwrapper exit calls, fixing issue 752
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 9224
diff changeset
   618
    return 0;
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   619
}