aboutsummaryrefslogtreecommitdiff
path: root/example/decode_filter_encode.c
blob: 48f31a73dd1f712f22ff47dd2ebfbf35d1fa5c8e (plain) (blame)
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include <bits/pthreadtypes.h>
#include <inttypes.h>
#include <libavcodec/avcodec.h>
#include <libavfilter/avfilter.h>
#include <libavformat/avformat.h>
#include <libavutil/pixdesc.h>
#include <libswscale/swscale.h>
#include <stdio.h>
#include <stdlib.h>
#include <tuberia.h>
#include <unistd.h>

const char *optstring = "hi:o:s:T";

static void printhelp(const char *argv0)
{
	printf(
			"%s -i <input> -o <output> -s <size> [-T]\n"
			"%s -h\n"
			"\n"
			" -i input           the input file\n"
			" -o output          the output file\n"
			" -s WIDTHxHEIGHT    the size to scale the video\n"
			" -T                 don't use libtuberia\n"
			" -h                 print this help\n", argv0, argv0);
}

static void close_codec(AVCodecContext **ctx)
{
	avcodec_free_context(ctx);
}

static void close_input(AVFormatContext **ctx)
{
	avformat_close_input(ctx);
}

static int open_input(AVFormatContext **avformatin, AVCodecContext **avcodecin,
		const char *file)
{
	AVFormatContext *ctx = NULL;
	AVCodec *codec = NULL;
	int i = 0;

	if (avformatin == NULL || avcodecin == NULL || file == NULL || !file[0]) {
		return -1;
	}

	printf("Opening input file %s\n", file);
	if (avformat_open_input(&ctx, file, NULL, NULL)) {
		return -1;
	}
	printf("Parsing input file %s\n", file);
	if (avformat_find_stream_info(ctx, NULL) < 0) {
		close_input(&ctx);
		return -1;
	}

	for (i = 0; i < (int)ctx->nb_streams; i++) {
		if (ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
			codec = avcodec_find_decoder(ctx->streams[i]->codecpar->codec_id);
			if (codec == NULL) {
				fprintf(stderr, "Couldn't find decoder for codec id %d (%s)\n",
						ctx->streams[i]->codecpar->codec_id,
						avcodec_get_name(ctx->streams[i]->codecpar->codec_id));
				close_input(&ctx);
				return -1;
			}
			printf("Opening input codec %s (%d)\n", avcodec_get_name(codec->id),
					codec->id);
			*avcodecin = avcodec_alloc_context3(codec);
			avcodec_parameters_to_context(*avcodecin, ctx->streams[i]->codecpar);
			(*avcodecin)->time_base = ctx->streams[i]->time_base;
			if (avcodec_open2(*avcodecin, codec, NULL) < 0) {
				fprintf(stderr, "Couldn't open codec %d (%s)\n",
						ctx->streams[i]->codecpar->codec_id,
						avcodec_get_name(ctx->streams[i]->codecpar->codec_id));
				close_input(&ctx);
				close_codec(avcodecin);
				return -1;
			}
			break;
		}
	}

	*avformatin = ctx;

	return 0;
}

static void close_sws(struct SwsContext **ctx)
{
	sws_freeContext(*ctx);
	*ctx = NULL;
}

static void close_output(AVFormatContext **ctx)
{
	av_write_trailer(*ctx);
	avformat_flush(*ctx);
	avio_closep(&(*ctx)->pb);
	avformat_free_context(*ctx);
	*ctx = NULL;
}

static int open_output(AVFormatContext **avformatout,
		AVCodecContext **avcodecout, struct SwsContext **sws, const char *file,
		const AVFormatContext *input, int width, int height)
{
	AVFormatContext *ctx = NULL;
	int i = 0;
	const AVCodec *codec = NULL;
	const AVCodecParameters *codecparin = NULL;
	AVStream *stream = NULL;

	if (avformatout == NULL || avcodecout == NULL || input == NULL
			|| file == NULL || !file[0]) {
		return -1;
	}
	printf("Opening output file %s\n", file);
	if (avformat_alloc_output_context2(&ctx, NULL, NULL, file) < 0
			|| ctx == NULL) {
		return -1;
	}

	for (i = 0; i < (int)input->nb_streams; i++) {
		codecparin = input->streams[i]->codecpar;
		codec = avcodec_find_encoder(codecparin->codec_id);
		if (codec == NULL) {
			fprintf(stderr, "Couldn't find encoder for codec id %d (%s)\n",
				codecparin->codec_id, avcodec_get_name(codecparin->codec_id));
			close_output(&ctx);
			return -1;
		}
		printf("Opening output codec %s (%d)\n", avcodec_get_name(codec->id),
				codec->id);
		stream = avformat_new_stream(ctx, codec);
		avcodec_parameters_copy(stream->codecpar, codecparin);
		stream->time_base = input->streams[i]->time_base;
		if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
			stream->codecpar->width = width;
			stream->codecpar->height = height;
		}
		if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
			*avcodecout = avcodec_alloc_context3(codec);
			avcodec_parameters_to_context(*avcodecout, stream->codecpar);
			(*avcodecout)->time_base = stream->time_base;
			if (avcodec_open2(*avcodecout, codec, NULL) < 0) {
				fprintf(stderr, "Couldn't open codec %s (%d)\n",
						avcodec_get_name(codecparin->codec_id),
						codecparin->codec_id);
				close_output(&ctx);
				close_codec(avcodecout);
				return -1;
			}
			printf("Opening resize context %dx%d %s -> %dx%d %s\n",
						codecparin->width, codecparin->height,
						av_get_pix_fmt_name((enum AVPixelFormat)codecparin->format),
						stream->codecpar->width, stream->codecpar->height,
						av_get_pix_fmt_name((enum AVPixelFormat)stream->codecpar->format));
			*sws = sws_getContext(codecparin->width, codecparin->height,
					(enum AVPixelFormat)codecparin->format,
					stream->codecpar->width, stream->codecpar->height,
					(enum AVPixelFormat)stream->codecpar->format,
					SWS_GAUSS, NULL, NULL, NULL);
			if (*sws == NULL) {
				fprintf(stderr, "Couldn't open resize context %dx%d %s -> %dx%d %s\n",
						codecparin->width, codecparin->height,
						av_get_pix_fmt_name((enum AVPixelFormat)codecparin->format),
						stream->codecpar->width, stream->codecpar->height,
						av_get_pix_fmt_name((enum AVPixelFormat)stream->codecpar->format));
				close_output(&ctx);
				close_codec(avcodecout);
				close_sws(sws);
			}
		}
	}

	if (!(ctx->flags & AVFMT_NOFILE)) {
		int ret = avio_open(&ctx->pb, file, AVIO_FLAG_WRITE);
		if (ret < 0) {
			fprintf(stderr, "Couldn't create output file %s %s (%d)\n", file,
					av_err2str(ret), ret);
			close_output(&ctx);
			close_codec(avcodecout);
			close_sws(sws);
			return -1;
		}
	}
	if (avformat_write_header(ctx, NULL) < 0) {
		fprintf(stderr, "Couldn't write header to output file %s\n", file);
	}
	*avformatout = ctx;
	return 0;
}

static void do_tuberia(AVFormatContext *avformatin, AVCodecContext *avcodecin,
		AVFormatContext *avformatout, struct SwsContext *sws,
		AVCodecContext *avcodecout)
{
	//TODO
	(void)avformatin;
	(void)avcodecin;
	(void)avformatout;
	(void)avcodecout;
	(void)sws;
	printf("Starting to transcode with libtuberia\n");
	printf("Transcode with libtuberia finished\n");
}

static int decode_packet(AVCodecContext *ctx, AVPacket *packet, AVFrame *frame)
{
	int ret = 0;
	ret = avcodec_send_packet(ctx, packet);
	av_packet_unref(packet);
	if (!ret) {
		ret = avcodec_receive_frame(ctx, frame);
	} else {
		fprintf(stderr, "Couldn't decode video packet\n");
	}

	return ret;
}

static int scale_frame(struct SwsContext *sws, AVFrame *frame, AVFrame *scaled,
		int width, int height, enum AVPixelFormat pix_fmt)
{
	av_frame_copy_props(scaled, frame);
	scaled->width = width;
	scaled->height = height;
	scaled->format = pix_fmt;
	av_frame_get_buffer(scaled, 0);
	sws_scale(sws, (const uint8_t * const *)frame->data, frame->linesize,
			0, frame->height, scaled->data, scaled->linesize);
	av_frame_unref(frame);

	return 0;
}

static int encode_frame(AVCodecContext *ctx, AVFrame *frame, AVPacket *packet)
{
	int ret = 0;

	ret = avcodec_send_frame(ctx, frame);
	av_frame_unref(frame);
	if (!ret) {
		ret = avcodec_receive_packet(ctx, packet);
	} else {
		fprintf(stderr, "Couldn't encode video packet\n");
	}

	return ret;
}

static void do_no_tuberia(AVFormatContext *avformatin, AVCodecContext *avcodecin,
		AVFormatContext *avformatout, struct SwsContext *sws,
		AVCodecContext *avcodecout)
{
	AVPacket *packet = NULL;
	AVFrame *frame = NULL;
	AVFrame *scaled = NULL;
	int idx = 0;

	printf("Starting to transcode without libtuberia\n");

	packet = av_packet_alloc();
	frame = av_frame_alloc();
	scaled = av_frame_alloc();
	while (!av_read_frame(avformatin, packet)) {
		idx = packet->stream_index;
		if (avformatin->streams[idx]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
			if (decode_packet(avcodecin, packet, frame)) {
				continue;
			}
			scale_frame(sws, frame, scaled, avcodecout->width,
					avcodecout->height, avcodecout->pix_fmt);
			if (encode_frame(avcodecout, scaled, packet)) {
				continue;
			}
			packet->stream_index = idx;
		}
		av_packet_rescale_ts(packet, avformatin->streams[idx]->time_base,
				avformatout->streams[idx]->time_base);
		av_interleaved_write_frame(avformatout, packet);
	}
	av_packet_free(&packet);
	av_frame_free(&frame);
	av_frame_free(&scaled);

	printf("Transcode without libtuberia finished\n");
}

int main(int argc, char **argv)
{
	int c = 0;
	char *input = NULL;
	char *output = NULL;
	int width = 720;
	int height = 404;
	int notuberia = 0;
	AVFormatContext *avformatin = NULL;
	AVFormatContext *avformatout = NULL;
	AVCodecContext *avcodecin = NULL;
	AVCodecContext *avcodecout = NULL;
	struct SwsContext *sws = NULL;

	while ((c = getopt(argc, argv, optstring)) >= 0) {
		switch (c) {
			case 'h':
				printhelp(argv[0]);
				return 0;
				break;
			case 'i':
				input = optarg;
				break;
			case 'o':
				output = optarg;
				break;
			case 's':
				if (sscanf(optarg, "%dx%d", &width, &height) != 2) {
					fprintf(stderr, "Size parameter must be WxH, instead it is %s\n",
							optarg);
					return -1;
				}
				break;
			case 'T':
				notuberia = 1;
				break;
			case '?':
			default:
				fprintf(stderr, "Incorrect arguments. Run %s -h\n", argv[0]);
				return -1;
		}
	}
	if (input == NULL || !input[0]) {
		printhelp(argv[0]);
		return -1;
	}

	if (open_input(&avformatin, &avcodecin, input) < 0) {
		fprintf(stderr, "Couldn't open input %s\n", input);
		return -1;
	}
	if (open_output(&avformatout, &avcodecout, &sws, output, avformatin, width,
				height) < 0) {
		fprintf(stderr, "Couldn't initiate output %s\n", output);
		return -1;
	}

	if (notuberia) {
		do_no_tuberia(avformatin, avcodecin, avformatout, sws, avcodecout);
	} else {
		do_tuberia(avformatin, avcodecin, avformatout, sws, avcodecout);
	}

	close_input(&avformatin);
	close_output(&avformatout);
	close_codec(&avcodecin);
	close_codec(&avcodecout);
	close_sws(&sws);

	return 0;
}