-
Notifications
You must be signed in to change notification settings - Fork 11
/
aveasy.c
297 lines (249 loc) · 7.24 KB
/
aveasy.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
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
/* aveasy - a simple wrapper around libavformat, libavdevice and libswscale
Copyright (C) 2010, 2011 Wolfgang Draxinger <code+aveasy@datenwolf.net>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include "aveasy.h"
void describe_AVInputFormat(
AVInputFormat const *const iformat )
{
if( !iformat )
return;
fprintf( stderr,
"name: %s\n"
"long_name: %s\n"
"priv_data_size: %d\n"
"read_probe: %p\n"
"read_header: %p\n" "read_packet: %p\n" "read_close: %p\n"
#if LIBAVFORMAT_VERSION_MAJOR < 53
"read_seek: %p\n"
#endif
"read_timestamp: %p\n"
"flags %x\n"
"read_play: %p\n"
"read_pause: %p\n"
"read_seek2: %p\n",
iformat->name,
iformat->long_name,
iformat->priv_data_size,
iformat->read_probe,
iformat->read_header,
iformat->read_packet, iformat->read_close,
#if LIBAVFORMAT_VERSION_MAJOR < 53
iformat->read_seek,
#endif
iformat->read_timestamp,
iformat->flags,
iformat->read_play, iformat->read_pause, iformat->read_seek2 );
}
struct AVEasyInputContext {
AVDictionary *format_parameters;
AVInputFormat *input_format;
AVFormatContext *format_context;
AVCodecContext *codec_context;
AVCodec *codec;
AVFrame *encoded_frame;
AVFrame *raw_frame;
struct SwsContext *sws_context;
int video_stream;
enum PixelFormat pixel_format;
size_t buffer_size;
void *buffer;
};
AVEasyInputContext *aveasy_input_open_v4l2(
char const * const path,
unsigned short width,
unsigned short height,
enum CodecID connection_codec,
enum PixelFormat pixel_format )
{
AVEasyInputContext *ctx;
int i;
ctx = (AVEasyInputContext*)malloc(sizeof(*ctx));
if(!ctx)
goto fail_alloc_context;
ctx->format_context = avformat_alloc_context();
if( !ctx->format_context )
goto fail_alloc_context;
ctx->format_context->video_codec_id = connection_codec;
ctx->format_parameters = NULL;
av_dict_set(&ctx->format_parameters, "preallocated_context", "1", 0);
char video_size[BUFSIZ];
snprintf(video_size, BUFSIZ, "%dx%d", width, height);
av_dict_set(&ctx->format_parameters, "video_size", video_size, 0);
ctx->input_format = av_find_input_format("video4linux2");
if(!ctx->input_format)
goto fail_find_input_format;
if( avformat_open_input( &ctx->format_context,
path,
ctx->input_format,
&ctx->format_parameters ) )
goto fail_open_input;
if( avformat_find_stream_info( ctx->format_context, NULL ) < 0 )
goto fail_find_stream_info;
av_dump_format( ctx->format_context, 0, path, false );
ctx->video_stream = -1;
for(i = 0; i < ctx->format_context->nb_streams; ++i) {
if( ctx->format_context->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO ) {
ctx->video_stream = i;
break;
}
}
if( ctx->video_stream == -1 )
goto fail_find_videostream;
ctx->codec_context = ctx->format_context->streams[ctx->video_stream]->codec;
if( !ctx->codec_context )
goto fail_get_codec_context;
ctx->sws_context = sws_getContext(
ctx->codec_context->width,
ctx->codec_context->height,
ctx->codec_context->pix_fmt,
ctx->codec_context->width,
ctx->codec_context->height,
ctx->pixel_format = pixel_format,
SWS_FAST_BILINEAR,
0, 0, 0 );
if( !ctx->sws_context )
goto fail_get_swscontext;
ctx->codec = avcodec_find_decoder( ctx->codec_context->codec_id );
if(! ctx->codec )
goto fail_find_decoder;
if( avcodec_open2( ctx->codec_context, ctx->codec, NULL ) < 0 )
goto fail_decoder_open;
// Fix for some some codecs which report wrong frame rate
if( ctx->codec_context->time_base.num > 999 &&
ctx->codec_context->time_base.den == 1 )
ctx->codec_context->time_base.den = 1000;
ctx->encoded_frame = avcodec_alloc_frame();
if( !ctx->encoded_frame )
goto fail_alloc_encoded_frame;
ctx->raw_frame = avcodec_alloc_frame();
if( !ctx->raw_frame )
goto fail_alloc_raw_frame;
ctx->buffer_size = avpicture_get_size(
ctx->pixel_format,
ctx->codec_context->width,
ctx->codec_context->height );
ctx->buffer = av_malloc( ctx->buffer_size );
if( !ctx->buffer )
goto fail_alloc_buffer;
avpicture_fill( (AVPicture*) ctx->raw_frame,
(unsigned char*)ctx->buffer,
ctx->pixel_format,
ctx->codec_context->width,
ctx->codec_context->height );
return ctx;
/* ----------------------------------------------- */
fail_alloc_buffer:
av_free(ctx->raw_frame);
fail_alloc_raw_frame:
av_free(ctx->encoded_frame);
fail_alloc_encoded_frame:
fail_decoder_open:
fail_find_decoder:
sws_freeContext(ctx->sws_context);
fail_get_swscontext:
fail_get_codec_context:
fail_find_videostream:
for(i = 0; i < ctx->format_context->nb_streams; ++i) {
if( ctx->format_context->streams[i]->codec ) {
avcodec_close(ctx->format_context->streams[i]->codec);
}
}
fail_find_stream_info:
avformat_close_input(&ctx->format_context);
fail_open_input:
fail_find_input_format:
av_free(ctx->format_context);
fail_alloc_context:
return 0;
}
void aveasy_input_close(AVEasyInputContext * const ctx)
{
int i;
if(!ctx)
return;
av_free(ctx->raw_frame);
av_free(ctx->encoded_frame);
sws_freeContext(ctx->sws_context);
for(i = 0; i < ctx->format_context->nb_streams; ++i) {
if( ctx->format_context->streams[i]->codec ) {
avcodec_close(ctx->format_context->streams[i]->codec);
}
}
avformat_close_input(&ctx->format_context);
av_free(ctx->format_context);
}
void *aveasy_input_read_frame(AVEasyInputContext * const ctx)
{
AVPacket packet;
if(!ctx)
return 0;
av_init_packet(&packet);
for(;;) {
if( av_read_frame(ctx->format_context, &packet) ) {
av_free_packet( &packet );
return 0;
}
if( packet.stream_index == ctx->video_stream ) {
int frame_finished = 0;
avcodec_decode_video2(
ctx->codec_context,
ctx->encoded_frame,
&frame_finished,
&packet);
if( frame_finished ) {
int ret =
sws_scale( ctx->sws_context,
ctx->encoded_frame->data,
ctx->encoded_frame->linesize,
0,
ctx->codec_context->height,
ctx->raw_frame->data,
ctx->raw_frame->linesize );
av_free_packet( &packet );
return ctx->buffer;
}
}
}
av_free_packet( &packet );
return 0;
}
int aveasy_input_width(AVEasyInputContext const * const ctx)
{
if(!ctx)
return 0;
return ctx->codec_context->width;
}
int aveasy_input_height(AVEasyInputContext const * const ctx)
{
if(!ctx)
return 0;
return ctx->codec_context->height;
}
size_t aveasy_input_buffer_size(AVEasyInputContext const * const ctx)
{
if(!ctx)
return 0;
return ctx->buffer_size;
}
void* aveasy_input_buffer(AVEasyInputContext const * const ctx)
{
if(!ctx)
return 0;
return ctx->buffer;
}