32#include <libavcodec/avcodec.h>
33#include <libavdevice/avdevice.h>
34#include <libavformat/avformat.h>
35#include <libavutil/imgutils.h>
36#include <libswscale/swscale.h>
40static unsigned int run = 1;
42struct video_input_ctx {
43 AVFormatContext *format_ctx;
44 AVCodecContext *codec_ctx;
45 int video_stream_index;
48static int video_input_init(
struct video_input_ctx *input_ctx,
49 const char *input_format_string,
50 const char *input_path,
51 AVDictionary **input_options)
53 AVInputFormat
const *input_format = NULL;
54 AVFormatContext *input_format_ctx;
55 AVCodecParameters *input_codec_params;
56 AVCodecContext *input_codec_ctx;
57 AVCodec
const *input_codec;
61 avdevice_register_all();
62#if LIBAVCODEC_VERSION_MAJOR < 59
63 avcodec_register_all();
67 if (input_format_string) {
69 input_format = av_find_input_format(input_format_string);
70 if (input_format == NULL) {
71 fprintf(stderr,
"cannot find input format\n");
77 if (input_path == NULL) {
78 fprintf(stderr,
"input_path must not be NULL!\n");
84 input_format_ctx = NULL;
85 ret = avformat_open_input(&input_format_ctx,
90 fprintf(stderr,
"cannot open input format/device\n");
95 ret = avformat_find_stream_info(input_format_ctx, NULL);
97 fprintf(stderr,
"cannot get information on the stream\n");
102 av_dump_format(input_format_ctx, 0, input_path, 0);
105 video_index = av_find_best_stream(input_format_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &input_codec, 0);
106 if (video_index < 0) {
107 fprintf(stderr,
"cannot find any video streams\n");
112 input_codec_ctx = avcodec_alloc_context3(input_codec);
113 if (input_codec_ctx == NULL) {
114 fprintf(stderr,
"failed to allocate the input codec context\n");
119 input_codec_params = input_format_ctx->streams[video_index]->codecpar;
120 ret = avcodec_parameters_to_context(input_codec_ctx, input_codec_params);
122 fprintf(stderr,
"cannot copy parameters to input codec context\n");
127 ret = avcodec_open2(input_codec_ctx, input_codec, NULL);
129 fprintf(stderr,
"cannot open input codec\n");
133 input_ctx->format_ctx = input_format_ctx;
134 input_ctx->codec_ctx = input_codec_ctx;
135 input_ctx->video_stream_index = video_index;
141 avcodec_free_context(&input_codec_ctx);
143 avformat_close_input(&input_format_ctx);
145 av_dict_free(input_options);
146 *input_options = NULL;
151struct video_output_ctx {
152 AVCodecContext *codec_ctx;
156static int video_output_init(
struct video_output_ctx *output_ctx,
157 struct video_input_ctx *input_ctx,
158 unsigned int upscale,
159 unsigned int quality,
163 AVCodecContext *output_codec_ctx;
164 AVCodec
const *output_codec;
165 unsigned int new_output_width;
166 unsigned int new_output_height;
169 if (input_ctx == NULL) {
170 fprintf(stderr,
"input_ctx must not be NULL!\n");
176 output_codec_ctx = avcodec_alloc_context3(NULL);
177 if (output_codec_ctx == NULL) {
178 fprintf(stderr,
"cannot allocate output codec context!\n");
185 ret = am7xxx_calc_scaled_image_dimensions(dev,
187 (input_ctx->codec_ctx)->width,
188 (input_ctx->codec_ctx)->height,
192 fprintf(stderr,
"cannot calculate output dimension\n");
197 output_codec_ctx->bit_rate = (input_ctx->codec_ctx)->bit_rate;
198 output_codec_ctx->width = new_output_width;
199 output_codec_ctx->height = new_output_height;
200 output_codec_ctx->time_base.num =
201 (input_ctx->format_ctx)->streams[input_ctx->video_stream_index]->time_base.num;
202 output_codec_ctx->time_base.den =
203 (input_ctx->format_ctx)->streams[input_ctx->video_stream_index]->time_base.den;
209 fprintf(stdout,
"using raw output format\n");
210 output_codec_ctx->pix_fmt = AV_PIX_FMT_NV12;
211 output_ctx->codec_ctx = output_codec_ctx;
212 output_ctx->raw_output = 1;
218 output_codec_ctx->pix_fmt = AV_PIX_FMT_YUVJ420P;
219 output_codec_ctx->codec_id = AV_CODEC_ID_MJPEG;
220 output_codec_ctx->codec_type = AVMEDIA_TYPE_VIDEO;
229 output_codec_ctx->qmin = output_codec_ctx->qmax = ((100 - (quality - 1)) * FF_QUALITY_SCALE) / 100;
230 output_codec_ctx->mb_lmin = output_codec_ctx->qmin * FF_QP2LAMBDA;
231 output_codec_ctx->mb_lmax = output_codec_ctx->qmax * FF_QP2LAMBDA;
232 output_codec_ctx->flags |= AV_CODEC_FLAG_QSCALE;
233 output_codec_ctx->global_quality = output_codec_ctx->qmin * FF_QP2LAMBDA;
236 output_codec = avcodec_find_encoder(output_codec_ctx->codec_id);
237 if (output_codec == NULL) {
238 fprintf(stderr,
"cannot find output codec!\n");
244 ret = avcodec_open2(output_codec_ctx, output_codec, NULL);
246 fprintf(stderr,
"could not open output codec!\n");
250 output_ctx->codec_ctx = output_codec_ctx;
251 output_ctx->raw_output = 0;
257 avcodec_free_context(&output_codec_ctx);
278static int decode(AVCodecContext *avctx, AVFrame *frame,
int *got_frame, AVPacket *pkt)
285 ret = avcodec_send_packet(avctx, pkt);
292 return ret == AVERROR_EOF ? 0 : ret;
295 ret = avcodec_receive_frame(avctx, frame);
296 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
304static int encode(AVCodecContext *avctx, AVPacket *pkt,
int *got_packet, AVFrame *frame)
310 ret = avcodec_send_frame(avctx, frame);
314 ret = avcodec_receive_packet(avctx, pkt);
317 if (ret == AVERROR(EAGAIN))
324static int am7xxx_play(
const char *input_format_string,
325 AVDictionary **input_options,
326 const char *input_path,
327 unsigned int rescale_method,
328 unsigned int upscale,
329 unsigned int quality,
334 struct video_input_ctx input_ctx;
335 struct video_output_ctx output_ctx;
337 AVFrame *frame_scaled;
342 struct SwsContext *sw_scale_ctx;
349 ret = video_input_init(&input_ctx, input_format_string, input_path, input_options);
351 fprintf(stderr,
"cannot initialize input\n");
355 ret = video_output_init(&output_ctx, &input_ctx, upscale, quality, image_format, dev);
357 fprintf(stderr,
"cannot initialize input\n");
362 frame_raw = av_frame_alloc();
363 if (frame_raw == NULL) {
364 fprintf(stderr,
"cannot allocate the raw frame!\n");
370 frame_scaled = av_frame_alloc();
371 if (frame_scaled == NULL) {
372 fprintf(stderr,
"cannot allocate the scaled frame!\n");
374 goto cleanup_frame_raw;
376 frame_scaled->format = (output_ctx.codec_ctx)->pix_fmt;
377 frame_scaled->width = (output_ctx.codec_ctx)->width;
378 frame_scaled->height = (output_ctx.codec_ctx)->height;
381 out_buf_size = av_image_get_buffer_size((output_ctx.codec_ctx)->pix_fmt,
382 (output_ctx.codec_ctx)->width,
383 (output_ctx.codec_ctx)->height,
385 out_buf = av_malloc(out_buf_size *
sizeof(uint8_t));
386 if (out_buf == NULL) {
387 fprintf(stderr,
"cannot allocate output data buffer!\n");
389 goto cleanup_frame_scaled;
393 av_image_fill_arrays(frame_scaled->data,
394 frame_scaled->linesize,
396 (output_ctx.codec_ctx)->pix_fmt,
397 (output_ctx.codec_ctx)->width,
398 (output_ctx.codec_ctx)->height,
401 sw_scale_ctx = sws_getCachedContext(NULL,
402 (input_ctx.codec_ctx)->width,
403 (input_ctx.codec_ctx)->height,
404 (input_ctx.codec_ctx)->pix_fmt,
405 (output_ctx.codec_ctx)->width,
406 (output_ctx.codec_ctx)->height,
407 (output_ctx.codec_ctx)->pix_fmt,
410 if (sw_scale_ctx == NULL) {
411 fprintf(stderr,
"cannot set up the rescaling context!\n");
413 goto cleanup_out_buf;
419 ret = av_read_frame(input_ctx.format_ctx, &in_packet);
421 if (ret == (
int)AVERROR_EOF || input_ctx.format_ctx->pb->eof_reached)
424 fprintf(stderr,
"av_read_frame failed, EOF?\n");
429 if (in_packet.stream_index != input_ctx.video_stream_index) {
437 ret = decode(input_ctx.codec_ctx, frame_raw, &got_frame, &in_packet);
439 fprintf(stderr,
"cannot decode video\n");
451 sws_scale(sw_scale_ctx,
452 (
const uint8_t *
const *)frame_raw->data,
455 (input_ctx.codec_ctx)->height,
457 frame_scaled->linesize);
459 if (output_ctx.raw_output) {
461 out_frame_size = out_buf_size;
463 frame_scaled->quality = (output_ctx.codec_ctx)->global_quality;
464 av_init_packet(&out_packet);
465 out_packet.data = NULL;
468 ret = encode(output_ctx.codec_ctx,
472 if (ret < 0 || !got_packet) {
473 fprintf(stderr,
"cannot encode video\n");
478 out_frame = out_packet.data;
479 out_frame_size = out_packet.size;
484 char filename[NAME_MAX];
486 if (!output_ctx.raw_output)
487 snprintf(filename, NAME_MAX,
"out_q%03d.jpg", quality);
489 snprintf(filename, NAME_MAX,
"out.raw");
490 file = fopen(filename,
"wb");
491 fwrite(out_frame, 1, out_frame_size, file);
498 ret = am7xxx_send_image_async(dev,
500 (output_ctx.codec_ctx)->width,
501 (output_ctx.codec_ctx)->height,
505 perror(
"am7xxx_send_image_async");
511 if (!output_ctx.raw_output && got_packet)
512 av_packet_unref(&out_packet);
513 av_packet_unref(&in_packet);
516 sws_freeContext(sw_scale_ctx);
520 av_frame_free(&frame_scaled);
522 av_frame_free(&frame_raw);
528 avcodec_free_context(&(output_ctx.codec_ctx));
531 avcodec_free_context(&(input_ctx.codec_ctx));
532 avformat_close_input(&(input_ctx.format_ctx));
540static int x_get_screen_dimensions(
const char *displayname,
int *width,
int *height)
542 int i, screen_number;
543 xcb_connection_t *connection;
544 const xcb_setup_t *setup;
545 xcb_screen_iterator_t iter;
547 connection = xcb_connect(displayname, &screen_number);
548 if (xcb_connection_has_error(connection)) {
549 fprintf(stderr,
"Cannot open a connection to %s\n", displayname);
553 setup = xcb_get_setup(connection);
555 fprintf(stderr,
"Cannot get setup for %s\n", displayname);
556 xcb_disconnect(connection);
560 iter = xcb_setup_roots_iterator(setup);
561 for (i = 0; i < screen_number; ++i) {
562 xcb_screen_next(&iter);
565 xcb_screen_t *screen = iter.data;
567 *width = screen->width_in_pixels;
568 *height = screen->height_in_pixels;
570 xcb_disconnect(connection);
575static char *get_x_screen_size(
const char *input_path)
583 ret = x_get_screen_dimensions(input_path, &width, &height);
585 fprintf(stderr,
"Cannot get screen dimensions for %s\n", input_path);
589 len = snprintf(NULL, 0,
"%dx%d", width, height);
591 screen_size = malloc((len + 1) *
sizeof(
char));
592 if (screen_size == NULL) {
597 len = snprintf(screen_size, len + 1,
"%dx%d", width, height);
606static char *get_x_screen_size(
const char *input_path)
609 fprintf(stderr,
"%s: fallback implementation, assuming a vga screen\n", __func__);
610 return strdup(
"vga");
614static void unset_run(
int signo)
621static int set_signal_handler(
void (*signal_handler)(
int))
623 struct sigaction new_action;
624 struct sigaction old_action;
627 new_action.sa_handler = signal_handler;
628 sigemptyset(&new_action.sa_mask);
629 new_action.sa_flags = 0;
631 ret = sigaction(SIGINT, NULL, &old_action);
633 perror(
"sigaction on old_action");
637 if (old_action.sa_handler != SIG_IGN) {
638 ret = sigaction(SIGINT, &new_action, NULL);
640 perror(
"sigaction on new_action");
649static int set_signal_handler(
void (*signal_handler)(
int))
651 (void)signal_handler;
652 fprintf(stderr,
"set_signal_handler() not implemented, sigaction not available\n");
658static void usage(
char *name)
660 printf(
"usage: %s [OPTIONS]\n\n", name);
661 printf(
"OPTIONS:\n");
662 printf(
"\t-d <index>\t\tthe device index (default is 0)\n");
664 printf(
"\t-D \t\t\tdump the last frame to a file (only active in DEBUG mode)\n");
666 printf(
"\t-f <input format>\tthe input device format\n");
667 printf(
"\t-i <input path>\t\tthe input path\n");
668 printf(
"\t-o <options>\t\ta comma separated list of input format options\n");
669 printf(
"\t\t\t\tEXAMPLE:\n");
670 printf(
"\t\t\t\t\t-o draw_mouse=1,framerate=100,video_size=800x480\n");
671 printf(
"\t-s <scaling method>\tthe rescaling method (see swscale.h)\n");
672 printf(
"\t-u \t\t\tupscale the image if smaller than the display dimensions\n");
673 printf(
"\t-F <format>\t\tthe image format to use (default is JPEG)\n");
674 printf(
"\t\t\t\tSUPPORTED FORMATS:\n");
675 printf(
"\t\t\t\t\t1 - JPEG\n");
676 printf(
"\t\t\t\t\t2 - NV12\n");
677 printf(
"\t-q <quality>\t\tquality of jpeg sent to the device, between 1 and 100\n");
678 printf(
"\t-l <log level>\t\tthe verbosity level of libam7xxx output (0-5)\n");
679 printf(
"\t-p <power mode>\t\tthe power mode of device, between %d (off) and %d (turbo)\n",
681 printf(
"\t\t\t\tWARNING: Level 2 and greater require the master AND\n");
682 printf(
"\t\t\t\t the slave connector to be plugged in.\n");
683 printf(
"\t-z <zoom mode>\t\tthe display zoom mode, between %d (original) and %d (tele)\n",
685 printf(
"\t-h \t\t\tthis help message\n");
686 printf(
"\n\nEXAMPLES OF USE:\n");
687 printf(
"\t%s -f x11grab -i :0.0 -o video_size=800x480\n", name);
688 printf(
"\t%s -f fbdev -i /dev/fb0\n", name);
689 printf(
"\t%s -f video4linux2 -i /dev/video0 -o video_size=320x240,frame_rate=100 -u -q 90\n", name);
690 printf(
"\t%s -i http://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_640x360.m4v\n", name);
693int main(
int argc,
char *argv[])
700 char *input_format_string = NULL;
701 AVDictionary *options = NULL;
702 char *input_path = NULL;
703 unsigned int rescale_method = SWS_BICUBIC;
704 unsigned int upscale = 0;
705 unsigned int quality = 95;
707 int device_index = 0;
715 while ((opt = getopt(argc, argv,
"d:Df:i:o:s:uF:q:l:p:z:h")) != -1) {
718 device_index = atoi(optarg);
719 if (device_index < 0) {
720 fprintf(stderr,
"Unsupported device index\n");
728 fprintf(stderr,
"Warning: the -D option is only active in DEBUG mode.\n");
732 input_format_string = strdup(optarg);
735 input_path = strdup(optarg);
744 subopts = subopts_saved = strdup(optarg);
745 while ((subopt = strtok_r(subopts,
",", &subopts))) {
746 char *subopt_name = strtok_r(subopt,
"=", &subopt);
747 char *subopt_value = strtok_r(NULL,
"", &subopt);
748 if (subopt_value == NULL) {
749 fprintf(stderr,
"invalid suboption: %s\n", subopt_name);
752 av_dict_set(&options, subopt_name, subopt_value, 0);
756 fprintf(stderr,
"Option '-o' not implemented\n");
760 rescale_method = atoi(optarg);
761 switch(rescale_method) {
762 case SWS_FAST_BILINEAR:
775 fprintf(stderr,
"Unsupported rescale method\n");
784 format = atoi(optarg);
787 fprintf(stdout,
"JPEG format\n");
790 fprintf(stdout,
"NV12 format\n");
793 fprintf(stderr,
"Unsupported format\n");
799 quality = atoi(optarg);
800 if (quality < 1 || quality > 100) {
801 fprintf(stderr,
"Invalid quality value, must be between 1 and 100\n");
807 log_level = atoi(optarg);
809 fprintf(stderr,
"Unsupported log level, falling back to AM7XXX_LOG_ERROR\n");
814 power_mode = atoi(optarg);
821 fprintf(stdout,
"Power mode: %d\n", power_mode);
824 fprintf(stderr,
"Invalid power mode value, must be between %d and %d\n",
838 fprintf(stdout,
"Zoom: %d\n", zoom);
841 fprintf(stderr,
"Invalid zoom mode value, must be between %d and %d\n",
858 if (input_path == NULL) {
859 fprintf(stderr,
"The -i option must always be passed\n\n");
869 if (input_format_string && strcmp(input_format_string,
"x11grab") == 0) {
872 video_size = get_x_screen_size(input_path);
874 if (!av_dict_get(options,
"video_size", NULL, 0))
875 av_dict_set(&options,
"video_size", video_size, 0);
877 if (!av_dict_get(options,
"framerate", NULL, 0))
878 av_dict_set(&options,
"framerate",
"60", 0);
880 if (!av_dict_get(options,
"draw_mouse", NULL, 0))
881 av_dict_set(&options,
"draw_mouse",
"1", 0);
886 ret = set_signal_handler(unset_run);
892 ret = am7xxx_init(&ctx);
894 perror(
"am7xxx_init");
898 am7xxx_set_log_level(ctx, log_level);
900 ret = am7xxx_open_device(ctx, &dev, device_index);
902 perror(
"am7xxx_open_device");
906 ret = am7xxx_set_zoom_mode(dev, zoom);
908 perror(
"am7xxx_set_zoom_mode");
912 ret = am7xxx_set_power_mode(dev, power_mode);
914 perror(
"am7xxx_set_power_mode");
922 ret = am7xxx_play(input_format_string,
932 fprintf(stderr,
"am7xxx_play failed\n");
937 am7xxx_shutdown(ctx);
939 av_dict_free(&options);
941 free(input_format_string);
@ AM7XXX_ZOOM_H
Zoom 1: H Scale (changes aspect ratio).
@ AM7XXX_ZOOM_ORIGINAL
Original Size, as retrieved via am7xxx_device_info.
@ AM7XXX_ZOOM_TELE
Zoom Tele: available on some PicoPix models.
@ AM7XXX_ZOOM_TEST
Zoom test screen, the firmware version is shown as well.
@ AM7XXX_ZOOM_H_V
Zoom 2: H/V Scale (changes aspect ratio).
@ AM7XXX_LOG_ERROR
Error messages, typically they describe API functions failures.
@ AM7XXX_LOG_INFO
Informations about the device operations.
@ AM7XXX_LOG_TRACE
Verbose informations about the communication with the hardware.
@ AM7XXX_POWER_HIGH
More brightness, but more power consumption.
@ AM7XXX_POWER_OFF
Display is powered off, no image shown.
@ AM7XXX_POWER_TURBO
Max brightness and power consumption.
@ AM7XXX_POWER_LOW
Low power consumption but also low brightness.
@ AM7XXX_POWER_MIDDLE
Middle level of brightness.
am7xxx_image_format
The image formats accepted by the device.
@ AM7XXX_IMAGE_FORMAT_JPEG
JPEG format.
@ AM7XXX_IMAGE_FORMAT_NV12
Raw YUV in the NV12 variant.
struct _am7xxx_context am7xxx_context
An opaque data type representing a context.
struct _am7xxx_device am7xxx_device
An opaque data type representing an am7xxx device.