This repository has been archived by the owner on Oct 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
ffmpeg.c
92 lines (79 loc) · 2.24 KB
/
ffmpeg.c
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "ffmpeg.h"
static const int bufSize = 1 << 12;
static pthread_mutex_t codecMu = PTHREAD_MUTEX_INITIALIZER;
void init(void)
{
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
av_register_all();
#endif
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 10, 100)
avcodec_register_all();
#endif
av_log_set_level(16);
}
int create_context(AVFormatContext** ctx, const char* input_format)
{
unsigned char* buf = malloc(bufSize);
AVFormatContext* c = *ctx;
c->pb = avio_alloc_context(
buf, bufSize, 0, c, readCallBack, NULL, seekCallBack);
c->flags |= AVFMT_FLAG_CUSTOM_IO | AVFMT_FLAG_DISCARD_CORRUPT;
AVInputFormat* avif = NULL;
if (input_format) {
avif = av_find_input_format(input_format);
}
int err = avformat_open_input(ctx, NULL, avif, NULL);
if (err < 0) {
return err;
}
// Calls avcodec_open2 internally, so needs locking
pthread_mutex_lock(&codecMu);
err = avformat_find_stream_info(*ctx, NULL);
pthread_mutex_unlock(&codecMu);
return err;
}
int codec_context(AVCodecContext** avcc, int* stream, AVFormatContext* avfc,
const enum AVMediaType type)
{
int err;
AVStream* st = NULL;
AVCodec* codec = NULL;
*stream = av_find_best_stream(avfc, type, -1, -1, NULL, 0);
if (*stream < 0) {
return *stream;
}
st = avfc->streams[*stream];
// ffvp8/9 doesn't support alpha channel so force libvpx.
switch (st->codecpar->codec_id) {
case AV_CODEC_ID_VP8:
codec = avcodec_find_decoder_by_name("libvpx");
break;
case AV_CODEC_ID_VP9:
codec = avcodec_find_decoder_by_name("libvpx-vp9");
break;
}
if (!codec) {
codec = avcodec_find_decoder(st->codecpar->codec_id);
if (!codec) {
return -1;
}
}
*avcc = avcodec_alloc_context3(codec);
if (!*avcc) {
goto end;
}
err = avcodec_parameters_to_context(*avcc, st->codecpar);
if (err < 0) {
goto end;
}
// Not thread safe. Needs lock.
pthread_mutex_lock(&codecMu);
err = avcodec_open2(*avcc, codec, NULL);
pthread_mutex_unlock(&codecMu);
end:
if (err < 0 && *avcc) {
avcodec_free_context(avcc);
*avcc = NULL;
}
return err;
}