[asterisk-commits] rizzo: branch rizzo/video_v2 r86753 - /team/rizzo/video_v2/utils/testffmpeg.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Oct 22 11:13:13 CDT 2007


Author: rizzo
Date: Mon Oct 22 11:13:13 2007
New Revision: 86753

URL: http://svn.digium.com/view/asterisk?view=rev&rev=86753
Log:
add new file for testing ffmpeg options

Added:
    team/rizzo/video_v2/utils/testffmpeg.c   (with props)

Added: team/rizzo/video_v2/utils/testffmpeg.c
URL: http://svn.digium.com/view/asterisk/team/rizzo/video_v2/utils/testffmpeg.c?view=auto&rev=86753
==============================================================================
--- team/rizzo/video_v2/utils/testffmpeg.c (added)
+++ team/rizzo/video_v2/utils/testffmpeg.c Mon Oct 22 11:13:13 2007
@@ -1,0 +1,159 @@
+/*
+ * test ffmpeg encoding and decoding.
+ *
+ * This program can be useful to check how ffmpeg behaves when called
+ * with different arguments, or when frames are lost and we don't know
+ * how robust the decoder is to losses.
+ * Optional args: -c codec -w width -h height -o output file
+ * BUILD with
+CFLAGS += -I/usr/local/include -L /usr/local/lib -lavcodec -lthr
+ */
+#include <sys/types.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <memory.h>
+#include <ffmpeg/avcodec.h>
+
+static int test(int w, int h, int fmt, int writefd)
+{
+	int loop;
+	int size = w * h;
+	unsigned bufsize = size * 3/2;
+
+	char *b_in = malloc(bufsize);
+	char *b_enc = malloc(bufsize);
+
+	AVCodec *enc = avcodec_find_encoder(fmt);
+	AVCodecContext  *enc_ctx = avcodec_alloc_context();
+	AVFrame *enc_in = avcodec_alloc_frame();
+
+
+	if (enc == NULL) {
+		fprintf(stderr, "cannot find encoder\n");
+		exit(0);
+	}
+
+	enc_in->data[0] = b_in;
+	enc_in->data[1] = b_in + size;
+	enc_in->data[2] = b_in + size + size/4;
+	enc_in->linesize[0] = w;
+	enc_in->linesize[1] = enc_in->linesize[2] = w/2;
+
+	enc_ctx->pix_fmt = PIX_FMT_YUV420P;
+	enc_ctx->width = w;
+	enc_ctx->height = h;
+	enc_ctx->bit_rate = 100000;
+	enc_ctx->bit_rate_tolerance = 100000;
+	enc_ctx->time_base = (AVRational){1, 5};
+	enc_ctx->gop_size = 10;
+
+	enc_ctx->rtp_mode = 0;
+	fprintf(stderr, "rtp_payload_size is %d\n",
+		enc_ctx->rtp_payload_size);
+	enc_ctx->rtp_payload_size = 0;
+
+	if (avcodec_open(enc_ctx, enc) < 0) {
+		fprintf(stderr, "error opening encoder\n");
+		exit(0);
+	}
+
+	int dfmt = fmt;
+	if (dfmt == CODEC_ID_H263P)
+		dfmt = CODEC_ID_H263;
+	AVCodec *dec = avcodec_find_decoder(dfmt);
+	AVCodecContext  *dec_ctx = avcodec_alloc_context();
+	AVCodecParserContext *dec_parser = av_parser_init(dfmt);
+	AVFrame *dec_out = avcodec_alloc_frame();
+	if (dec == NULL || dec_parser == NULL) {
+		fprintf(stderr, "cannot find decoder\n");
+		exit(0);
+	}
+	fprintf(stderr, "now open decoder\n");
+	if (avcodec_open(dec_ctx, dec) < 0) {
+		fprintf(stderr, "error opening decoder\n");
+		exit(0);
+	}
+
+	for (loop = 0; loop < 100; loop++) {
+		int i, j, dummy;
+		unsigned int datalen;
+		uint8_t *x, *data;
+		for (i=0; i < size * 3 / 2; i++)
+			b_in[i] = (i + loop) % 255;
+		i = avcodec_encode_video(enc_ctx, b_enc, (int)bufsize, enc_in);
+		fprintf(stderr, "encoding took %d bytes of %d\n", i, bufsize);
+		if (i <= 0)
+			continue;
+		if (writefd >= 0)
+			write(writefd, b_enc, i);
+		x = b_enc;
+		while (i) {
+			int l;
+			fprintf(stderr, "decoding, %d left\n", i);
+			if (fmt == CODEC_ID_MPEG4) {
+				data = x;
+				datalen = i;
+				l = i;
+			} else {
+			l = av_parser_parse(dec_parser, dec_ctx,
+				&data, &datalen, x, i, 0, 0);
+			}
+			x += l;
+			i -= l;
+			if (data == NULL || datalen == 0)
+				continue;
+			j = avcodec_decode_video(dec_ctx, dec_out,
+				&dummy, data, datalen);
+		}
+	}
+	return 0;
+}
+
+struct _sx {
+	char *name;
+	int fmt;
+};
+
+static struct _sx fmts[] = {
+	{"h261",	CODEC_ID_H261 },
+	{"h263",	CODEC_ID_H263 },
+	{"h263p",	CODEC_ID_H263P },	// use h263 in decoding
+	{"h264",	CODEC_ID_H264 },
+	{"mpeg4",	CODEC_ID_MPEG4 },
+	{NULL,		-1 },
+};
+
+int main(int argc, char *argv[])
+{
+	int fd = -1, w = 352, h = 288, fmt = CODEC_ID_H261;
+	int i, c;
+
+	avcodec_init();
+	avcodec_register_all();
+	while ((c = getopt(argc, argv, "w:h:o:c:")) != -1) {
+		switch (c) {
+		case 'h':
+			h = atoi(optarg);
+			break;
+		case 'w':
+			w = atoi(optarg);
+			break;
+		case 'c':
+			for (i = 0; fmts[i].name != NULL; i++) {
+				if (!strcasecmp(fmts[i].name, optarg)) {
+					fmt = fmts[i].fmt;
+					break;
+				}
+			}
+			break;
+		case 'o':
+			fd = open(optarg, O_CREAT|O_TRUNC | O_WRONLY, 0666);
+			break;
+		}
+	}
+
+	test(w, h, fmt, fd);
+	return 0;
+}

Propchange: team/rizzo/video_v2/utils/testffmpeg.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/rizzo/video_v2/utils/testffmpeg.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/rizzo/video_v2/utils/testffmpeg.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain




More information about the asterisk-commits mailing list