summaryrefslogtreecommitdiff
path: root/src/codec/ffmpeg/common.c
blob: e697a0006f0fe43ecb1ea9dd88fbb7dab3a91326 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#define AL_LOG_SECTION "ff"
//#define AL_LOG_ENABLE_TRACE
#include <al/log.h>
#include <al/lib.h>
#include <libavutil/log.h>
#include <libavformat/avformat.h>

#include "common.h"

s64 camu_ff_frame_duration(AVFrame *frame)
{
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 30, 100)
    return frame->duration;
#else
    return frame->pkt_duration;
#endif
}

static __thread struct {
    char buf[AL_LOG_MESSAGE_SIZE];
    size_t pos;
} log_context;

// If a line takes more than 1 step to print, make sure we stay within AL_LOG_MESSAGE_SIZE.
#define AV_LOG_MAX_CHUNK (AL_LOG_MESSAGE_SIZE / 2)

static void av_log_callback(void *userdata, s32 level, const char *fmt, va_list args)
{
    (void)userdata;

    char *offset = log_context.buf + log_context.pos;
    s32 ret = al_vsnprintf(offset, AV_LOG_MAX_CHUNK, fmt, args);
    al_assert(ret > 0);

    offset += ret;
    log_context.pos += ret;

    if ((log_context.pos > 0 && *(offset - 1) == '\n') || log_context.pos >= AV_LOG_MAX_CHUNK) {
        if (level < AV_LOG_WARNING) {
            log_info(log_context.buf);
        } else if (level < AL_LOG_DEBUG) {
            log_debug(log_context.buf);
        } else {
            log_trace(log_context.buf);
        }
        log_context.pos = 0;
    }
}

void camu_ff_set_log_callback(void (*callback)(void *, s32, const char *, va_list))
{
    av_log_set_callback(callback);
}

void camu_ff_common_init(void)
{

#ifdef CAMU_OLD_FFMPEG
    log_info("Using FFmpeg version "FFMPEG_VERSION" (old).");
    AL_IGNORE_WARNING("-Wdeprecated-declarations")
    av_register_all();
    AL_IGNORE_WARNING_END
#else
    log_info("Using FFmpeg version "FFMPEG_VERSION".");
#endif
    camu_ff_set_log_callback(av_log_callback);
}