forked from bakape/thumbnailer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffmpeg.c
126 lines (110 loc) · 2.91 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "ffmpeg.h"
const int bufSize = 1 << 12;
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);
}
// Initialize am AVFormatContext with the buffered file
int create_context(AVFormatContext** ctx)
{
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;
int err = avformat_open_input(ctx, NULL, NULL, NULL);
if (err < 0) {
return err;
}
// Calls avcodec_open2 internally, so needs locking
err = pthread_mutex_lock(&codecMu);
if (err < 0) {
return err;
}
err = avformat_find_stream_info(*ctx, NULL);
if (err < 0) {
int muErr = pthread_mutex_unlock(&codecMu);
if (muErr < 0) {
return muErr;
}
return err;
}
err = pthread_mutex_unlock(&codecMu);
if (err < 0) {
return err;
}
return 0;
}
void destroy(AVFormatContext* ctx)
{
av_free(ctx->pb->buffer);
ctx->pb->buffer = NULL;
av_free(ctx->pb);
av_free(ctx);
}
// Create a AVCodecContext of the desired media type
int codec_context(AVCodecContext** avcc, int* stream, AVFormatContext* avfc,
const enum AVMediaType type)
{
int err;
AVStream* st = NULL;
const 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.
if (st->codecpar->codec_id == AV_CODEC_ID_VP8) {
codec = avcodec_find_decoder_by_name("libvpx");
} else if (st->codecpar->codec_id == AV_CODEC_ID_VP9) {
codec = avcodec_find_decoder_by_name("libvpx-vp9");
}
if (!codec) {
codec = avcodec_find_decoder(st->codecpar->codec_id);
if (!codec) {
return -1;
}
}
*avcc = avcodec_alloc_context3(codec);
if (!*avcc) {
return -1;
}
err = avcodec_parameters_to_context(*avcc, st->codecpar);
if (err < 0) {
return err;
}
// Not thread safe. Needs lock.
err = pthread_mutex_lock(&codecMu);
if (err < 0) {
return err;
}
err = avcodec_open2(*avcc, codec, NULL);
if (err < 0) {
avcodec_free_context(avcc);
int muErr = pthread_mutex_unlock(&codecMu);
if (muErr < 0) {
return muErr;
}
return err;
}
err = pthread_mutex_unlock(&codecMu);
if (err < 0) {
return err;
}
return 0;
}
// Format ffmpeg error code to string message
char* format_error(const int code)
{
char* buf = malloc(1024);
av_strerror(code, buf, 1024);
return buf;
}