-
Notifications
You must be signed in to change notification settings - Fork 6
/
ffmpeg.c
173 lines (134 loc) · 4.56 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
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
#include "ffmpeg.h"
void ffmpeginit() {
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
av_register_all();
avformat_network_init();
#endif
}
struct AVStream * stream_at(struct AVFormatContext *c, int idx) {
if (idx >= 0 && idx < c->nb_streams)
return c->streams[idx];
return NULL;
}
int rtsp_encode(AVCodecContext *avctx, AVPacket *pkt, int *got_packet, AVFrame *frame)
{
int ret;
*got_packet = 0;
if (!frame) {
return -1;
}
ret = avcodec_send_frame(avctx, frame);
if (ret < 0)
return ret;
ret = avcodec_receive_packet(avctx, pkt);
if (!ret)
*got_packet = 1;
if (ret == AVERROR(EAGAIN))
return 0;
return ret;
}
int rtsp_avcodec_encode_wav(AVCodecContext *pCodecCtx, AVFrame *pFrame,AVPacket *packet) {
const AVCodec *wavCodec = avcodec_find_encoder(AV_CODEC_ID_PCM_S16LE );
int ret = -1;
if (!wavCodec) {
return ret;
}
AVCodecContext *wavContext = avcodec_alloc_context3(wavCodec);
if (!wavContext) {
wavCodec = NULL;
return ret;
}
wavContext->bit_rate = pCodecCtx->bit_rate;
wavContext->sample_fmt = AV_SAMPLE_FMT_S16;
wavContext->sample_rate = pFrame->sample_rate;
wavContext->channel_layout = pFrame->channel_layout;
wavContext->channels = pFrame->channels;
ret = avcodec_open2(wavContext, wavCodec, NULL);
if (ret < 0) {
goto error;
}
int gotFrame;
ret = rtsp_encode(wavContext, packet, &gotFrame, pFrame);
if (ret < 0) {
goto error;
}
error:
avcodec_close(wavContext);
avcodec_free_context(&wavContext);
wavCodec = NULL;
return ret;
}
int rtsp_avcodec_encode_jpeg(AVCodecContext *pCodecCtx, AVFrame *pFrame,AVPacket *packet) {
const AVCodec *jpegCodec = avcodec_find_encoder(AV_CODEC_ID_MJPEG);
int ret = -1;
if (!jpegCodec) {
return ret;
}
AVCodecContext *jpegContext = avcodec_alloc_context3(jpegCodec);
if (!jpegContext) {
jpegCodec = NULL;
return ret;
}
jpegContext->pix_fmt = AV_PIX_FMT_YUVJ420P;
jpegContext->height = pFrame->height;
jpegContext->width = pFrame->width;
jpegContext->time_base= (AVRational){1,25};
ret = avcodec_open2(jpegContext, jpegCodec, NULL);
if (ret < 0) {
goto error;
}
int gotFrame;
ret = rtsp_encode(jpegContext, packet, &gotFrame, pFrame);
if (ret < 0) {
goto error;
}
error:
avcodec_close(jpegContext);
avcodec_free_context(&jpegContext);
jpegCodec = NULL;
return ret;
}
uint8_t *rtsp_convert(AVCodecContext *pCodecCtx,AVFrame *pFrame,AVFrame *nFrame,int *size, int format) {
struct SwsContext *img_convert_ctx = sws_getCachedContext( NULL, pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pFrame->width, pFrame->height, format, SWS_BICUBIC, NULL, NULL, NULL );
nFrame->format = format;
nFrame->width = pFrame->width;
nFrame->height = pFrame->height;
*size = av_image_get_buffer_size( format, pFrame->width, pFrame->height, 1);
uint8_t *tmp_picture_buf = (uint8_t *)malloc(*size);
av_image_fill_arrays(nFrame->data, nFrame->linesize, tmp_picture_buf, format, pFrame->width, pFrame->height, 1);
sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, nFrame->height, nFrame->data, nFrame->linesize);
sws_freeContext(img_convert_ctx);
return tmp_picture_buf;
}
int rtsp_avcodec_encode_jpeg_nv12(AVCodecContext *pCodecCtx, AVFrame *pFrame,AVPacket *packet) {
int size = 0;
AVFrame *nFrame = av_frame_alloc();
uint8_t * data = rtsp_convert(pCodecCtx, pFrame, nFrame, &size, AV_PIX_FMT_YUV420P);
int ret = rtsp_avcodec_encode_jpeg(pCodecCtx,nFrame,packet);
free(data);
av_frame_free(&nFrame);
return ret;
}
int rtsp_avcodec_encode_resample_wav(AVCodecContext *pCodecCtx,SwrContext *swr_ctx, AVFrame *pFrame,AVPacket *packet) {
int ret = 0;
AVFrame *nFrame = av_frame_alloc();
nFrame->channel_layout = pFrame->channel_layout;
nFrame->sample_rate = pFrame->sample_rate;
nFrame->format = AV_SAMPLE_FMT_S16;
// Without this, there is no sound at all at the output (PTS stuff I guess)
ret = av_frame_copy_props(nFrame, pFrame);
if (ret < 0) {
goto error;
}
ret = swr_convert_frame(swr_ctx, nFrame, pFrame);
if (ret < 0) {
goto error;
}
ret = rtsp_avcodec_encode_wav(pCodecCtx,nFrame,packet);
if (ret < 0) {
goto error;
}
error:
av_frame_free(&nFrame);
return ret;
}