-
Notifications
You must be signed in to change notification settings - Fork 27
/
ffjpeg.cpp
executable file
·304 lines (247 loc) · 7.63 KB
/
ffjpeg.cpp
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// 包含头文件
#include <pthread.h>
#include "ffjpeg.h"
extern "C" {
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
}
// 内部类型定义
typedef struct
{
struct SwsContext *sws_ctx;
AVPixelFormat ifmt;
int iw;
int ih;
int ow;
int oh;
AVFrame picture;
const char *file;
pthread_t thread_id;
#ifdef ENABLE_MEDIARECORDER_JNI
jclass jcls_mr;
jobject jobj_mr;
jmethodID jmid_cb;
#endif
} JPEGENC;
// 内部函数实现
static void* ffjpeg_encode_thread_proc(void *param)
{
JPEGENC *encoder = (JPEGENC*)param;
AVCodecContext *codec_ctxt = NULL;
AVCodec *codec = NULL;
AVPacket packet ;
int got = 0;
int ret = 0;
#ifdef ENABLE_MEDIARECORDER_JNI
JNIEnv *env = get_jni_env();
#endif
// init packet
memset(&packet, 0, sizeof(AVPacket));
// do encoding
codec = avcodec_find_encoder(AV_CODEC_ID_MJPEG);
if (!codec) {
av_log(NULL, AV_LOG_ERROR, "failed to find encoder !\n");
goto done;
}
codec_ctxt = avcodec_alloc_context3(codec);
if (!codec) {
av_log(NULL, AV_LOG_ERROR, "failed to allocate codec context !\n");
goto done;
}
codec_ctxt->width = encoder->picture.width;
codec_ctxt->height = encoder->picture.height;
codec_ctxt->time_base.num = 1;
codec_ctxt->time_base.den = 25;
codec_ctxt->pix_fmt = AV_PIX_FMT_YUVJ420P;
if (avcodec_open2(codec_ctxt, codec, NULL) < 0) {
av_log(NULL, AV_LOG_ERROR, "failed to open encoder !\n");
goto done;
}
avcodec_encode_video2(codec_ctxt, &packet, &encoder->picture, &got);
if (got) {
FILE *fp = fopen(encoder->file, "wb");
if (fp) {
fwrite(packet.data, packet.size, 1, fp);
fclose(fp);
}
}
done:
avcodec_close(codec_ctxt);
av_free(codec_ctxt);
av_packet_unref(&packet);
#ifdef ENABLE_MEDIARECORDER_JNI
if (env && encoder->jcls_mr && encoder->jobj_mr && encoder->jmid_cb) {
env->CallVoidMethod(encoder->jobj_mr, encoder->jmid_cb, env->NewStringUTF(encoder->file), encoder->ow, encoder->oh);
}
// need call DetachCurrentThread
g_jvm->DetachCurrentThread();
#endif
encoder->file = NULL;
return NULL;
}
static void alloc_picture(AVFrame *picture, enum AVPixelFormat pix_fmt, int width, int height)
{
int ret;
picture->format = pix_fmt;
picture->width = width;
picture->height = height;
/* allocate the buffers for the frame data */
ret = av_frame_get_buffer(picture, 32);
if (ret < 0) {
printf("could not allocate frame data.\n");
exit(1);
}
}
// 函数实现
void* ffjpeg_encoder_init(void)
{
// allocate context for ffencoder
JPEGENC *encoder = (JPEGENC*)calloc(1, sizeof(JPEGENC));
if (!encoder) {
return NULL;
}
// init ffmpeg library
av_register_all();
return encoder;
}
int ffjpeg_encoder_encode(void *ctxt, const char *file, int w, int h, AVFrame *frame)
{
JPEGENC *encoder = (JPEGENC*)ctxt;
// check valid
if (!ctxt) return -1;
// check encoder busy or not
if (encoder->file) {
return -1; // encoder busy
}
else {
encoder->file = file;
}
if ( frame->format != encoder->ifmt
|| frame->width != encoder->iw || frame->height != encoder->ih
|| w != encoder->ow || h != encoder->oh ) {
if (encoder->sws_ctx) {
sws_freeContext(encoder->sws_ctx);
}
encoder->sws_ctx = sws_getContext(frame->width, frame->height, (AVPixelFormat)frame->format,
w, h, AV_PIX_FMT_YUVJ420P, SWS_FAST_BILINEAR, NULL, NULL, NULL);
if (!encoder->sws_ctx) {
printf("could not initialize the conversion context jpg\n");
exit(1);
}
if (w != encoder->ow || h != encoder->oh) {
// free jpeg picture frame
av_frame_unref(&encoder->picture);
// alloc picture
alloc_picture(&encoder->picture, AV_PIX_FMT_YUVJ420P, w, h);
}
encoder->ow = w;
encoder->oh = h;
encoder->ifmt = (AVPixelFormat)frame->format;
encoder->iw = frame->width;
encoder->ih = frame->height;
}
// scale picture
sws_scale(
encoder->sws_ctx,
frame->data,
frame->linesize,
0,
encoder->ih,
encoder->picture.data,
encoder->picture.linesize);
// create jpeg encoding thread
pthread_create(&encoder->thread_id, NULL, ffjpeg_encode_thread_proc, encoder);
return 0;
}
void ffjpeg_encoder_free(void *ctxt)
{
JPEGENC *encoder = (JPEGENC*)ctxt;
if (!ctxt) return;
// wait jpeg encode thread exit
pthread_join(encoder->thread_id, NULL);
// free jpeg picture frame
av_frame_unref(&encoder->picture);
// free sws context
sws_freeContext(encoder->sws_ctx);
#ifdef ENABLE_MEDIARECORDER_JNI
get_jni_env()->DeleteGlobalRef(encoder->jobj_mr);
#endif
free(ctxt);
}
#ifdef ENABLE_MEDIARECORDER_JNI
void ffjpeg_encoder_init_jni_callback(void *ctxt, JNIEnv *env, jobject obj)
{
JPEGENC *encoder = (JPEGENC*)ctxt;
if (!encoder) return;
encoder->jcls_mr = env->GetObjectClass(obj);
encoder->jobj_mr = env->NewGlobalRef(obj);
encoder->jmid_cb = env->GetMethodID(encoder->jcls_mr, "internalTakePhotoCallback", "(Ljava/lang/String;II)V");
}
#endif
// 内部类型定义
typedef struct {
AVCodec *codec;
AVCodecContext *ctxt;
AVFrame frame;
} JPEGDEC;
// 函数实现
void* ffjpeg_decoder_init(void)
{
JPEGDEC *dec = NULL;
int failed = 0;
/* initialize libavcodec, and register all codecs and formats. */
av_register_all();
dec = (JPEGDEC*)calloc(1, sizeof(JPEGDEC));
if (!dec) return NULL;
dec->codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG);
if (!dec->codec) {
fprintf(stderr, "codec not found\n");
failed = 1;
goto done;
}
dec->ctxt = avcodec_alloc_context3(dec->codec);
if (!dec->ctxt) {
fprintf(stderr, "could not allocate video codec context\n");
failed = 1;
goto done;
}
if (dec->codec->capabilities & AV_CODEC_CAP_TRUNCATED) {
dec->ctxt->flags |= AV_CODEC_FLAG_TRUNCATED; // we do not send complete frames
}
/* For some codecs, such as msmpeg4 and mpeg4, width and height
MUST be initialized there because this information is not
available in the bitstream. */
/* open it */
if (avcodec_open2(dec->ctxt, dec->codec, NULL) < 0) {
fprintf(stderr, "could not open codec\n");
failed = 1;
goto done;
}
done:
if (failed) {
ffjpeg_decoder_free(dec);
dec = NULL;
}
return dec;
}
void ffjpeg_decoder_free(void *ctxt)
{
JPEGDEC *dec = (JPEGDEC*)ctxt;
if (!dec) return;
avcodec_close(dec->ctxt);
av_free(dec->ctxt);
av_frame_unref(&dec->frame);
}
AVFrame *ffjpeg_decoder_decode(void *ctxt, void *buf, int len)
{
JPEGDEC *dec = (JPEGDEC*)ctxt;
if (!dec) return NULL;
AVPacket packet;
int got = 0;
av_init_packet(&packet);
packet.data = (uint8_t*)buf;
packet.size = len;
avcodec_decode_video2(dec->ctxt, &dec->frame, &got, &packet);
return &dec->frame;
}