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>
40 static unsigned int run = 1;
42 struct video_input_ctx {
43 AVFormatContext *format_ctx;
44 AVCodecContext *codec_ctx;
45 int video_stream_index;
48 static 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;
151 struct video_output_ctx {
152 AVCodecContext *codec_ctx;
156 static 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");
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);
278 static 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)
304 static 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))
324 static 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);
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);
519 cleanup_frame_scaled:
520 av_frame_free(&frame_scaled);
522 av_frame_free(&frame_raw);
528 avcodec_close(output_ctx.codec_ctx);
529 avcodec_free_context(&(output_ctx.codec_ctx));
532 avcodec_close(input_ctx.codec_ctx);
533 avcodec_free_context(&(input_ctx.codec_ctx));
534 avformat_close_input(&(input_ctx.format_ctx));
542 static int x_get_screen_dimensions(
const char *displayname,
int *width,
int *height)
544 int i, screen_number;
545 xcb_connection_t *connection;
546 const xcb_setup_t *setup;
547 xcb_screen_iterator_t iter;
549 connection = xcb_connect(displayname, &screen_number);
550 if (xcb_connection_has_error(connection)) {
551 fprintf(stderr,
"Cannot open a connection to %s\n", displayname);
555 setup = xcb_get_setup(connection);
557 fprintf(stderr,
"Cannot get setup for %s\n", displayname);
558 xcb_disconnect(connection);
562 iter = xcb_setup_roots_iterator(setup);
563 for (i = 0; i < screen_number; ++i) {
564 xcb_screen_next(&iter);
567 xcb_screen_t *screen = iter.data;
569 *width = screen->width_in_pixels;
570 *height = screen->height_in_pixels;
572 xcb_disconnect(connection);
577 static char *get_x_screen_size(
const char *input_path)
585 ret = x_get_screen_dimensions(input_path, &width, &height);
587 fprintf(stderr,
"Cannot get screen dimensions for %s\n", input_path);
591 len = snprintf(NULL, 0,
"%dx%d", width, height);
593 screen_size = malloc((len + 1) *
sizeof(
char));
594 if (screen_size == NULL) {
599 len = snprintf(screen_size, len + 1,
"%dx%d", width, height);
608 static char *get_x_screen_size(
const char *input_path)
611 fprintf(stderr,
"%s: fallback implementation, assuming a vga screen\n", __func__);
612 return strdup(
"vga");
616 static void unset_run(
int signo)
622 #ifdef HAVE_SIGACTION
623 static int set_signal_handler(
void (*signal_handler)(
int))
625 struct sigaction new_action;
626 struct sigaction old_action;
629 new_action.sa_handler = signal_handler;
630 sigemptyset(&new_action.sa_mask);
631 new_action.sa_flags = 0;
633 ret = sigaction(SIGINT, NULL, &old_action);
635 perror(
"sigaction on old_action");
639 if (old_action.sa_handler != SIG_IGN) {
640 ret = sigaction(SIGINT, &new_action, NULL);
642 perror(
"sigaction on new_action");
651 static int set_signal_handler(
void (*signal_handler)(
int))
653 (void)signal_handler;
654 fprintf(stderr,
"set_signal_handler() not implemented, sigaction not available\n");
660 static void usage(
char *name)
662 printf(
"usage: %s [OPTIONS]\n\n", name);
663 printf(
"OPTIONS:\n");
664 printf(
"\t-d <index>\t\tthe device index (default is 0)\n");
666 printf(
"\t-D \t\t\tdump the last frame to a file (only active in DEBUG mode)\n");
668 printf(
"\t-f <input format>\tthe input device format\n");
669 printf(
"\t-i <input path>\t\tthe input path\n");
670 printf(
"\t-o <options>\t\ta comma separated list of input format options\n");
671 printf(
"\t\t\t\tEXAMPLE:\n");
672 printf(
"\t\t\t\t\t-o draw_mouse=1,framerate=100,video_size=800x480\n");
673 printf(
"\t-s <scaling method>\tthe rescaling method (see swscale.h)\n");
674 printf(
"\t-u \t\t\tupscale the image if smaller than the display dimensions\n");
675 printf(
"\t-F <format>\t\tthe image format to use (default is JPEG)\n");
676 printf(
"\t\t\t\tSUPPORTED FORMATS:\n");
677 printf(
"\t\t\t\t\t1 - JPEG\n");
678 printf(
"\t\t\t\t\t2 - NV12\n");
679 printf(
"\t-q <quality>\t\tquality of jpeg sent to the device, between 1 and 100\n");
680 printf(
"\t-l <log level>\t\tthe verbosity level of libam7xxx output (0-5)\n");
681 printf(
"\t-p <power mode>\t\tthe power mode of device, between %d (off) and %d (turbo)\n",
683 printf(
"\t\t\t\tWARNING: Level 2 and greater require the master AND\n");
684 printf(
"\t\t\t\t the slave connector to be plugged in.\n");
685 printf(
"\t-z <zoom mode>\t\tthe display zoom mode, between %d (original) and %d (tele)\n",
687 printf(
"\t-h \t\t\tthis help message\n");
688 printf(
"\n\nEXAMPLES OF USE:\n");
689 printf(
"\t%s -f x11grab -i :0.0 -o video_size=800x480\n", name);
690 printf(
"\t%s -f fbdev -i /dev/fb0\n", name);
691 printf(
"\t%s -f video4linux2 -i /dev/video0 -o video_size=320x240,frame_rate=100 -u -q 90\n", name);
692 printf(
"\t%s -i http://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_640x360.m4v\n", name);
695 int main(
int argc,
char *argv[])
702 char *input_format_string = NULL;
703 AVDictionary *options = NULL;
704 char *input_path = NULL;
705 unsigned int rescale_method = SWS_BICUBIC;
706 unsigned int upscale = 0;
707 unsigned int quality = 95;
709 int device_index = 0;
717 while ((opt = getopt(argc, argv,
"d:Df:i:o:s:uF:q:l:p:z:h")) != -1) {
720 device_index = atoi(optarg);
721 if (device_index < 0) {
722 fprintf(stderr,
"Unsupported device index\n");
730 fprintf(stderr,
"Warning: the -D option is only active in DEBUG mode.\n");
734 input_format_string = strdup(optarg);
737 input_path = strdup(optarg);
746 subopts = subopts_saved = strdup(optarg);
747 while ((subopt = strtok_r(subopts,
",", &subopts))) {
748 char *subopt_name = strtok_r(subopt,
"=", &subopt);
749 char *subopt_value = strtok_r(NULL,
"", &subopt);
750 if (subopt_value == NULL) {
751 fprintf(stderr,
"invalid suboption: %s\n", subopt_name);
754 av_dict_set(&options, subopt_name, subopt_value, 0);
758 fprintf(stderr,
"Option '-o' not implemented\n");
762 rescale_method = atoi(optarg);
763 switch(rescale_method) {
764 case SWS_FAST_BILINEAR:
777 fprintf(stderr,
"Unsupported rescale method\n");
786 format = atoi(optarg);
789 fprintf(stdout,
"JPEG format\n");
792 fprintf(stdout,
"NV12 format\n");
795 fprintf(stderr,
"Unsupported format\n");
801 quality = atoi(optarg);
802 if (quality < 1 || quality > 100) {
803 fprintf(stderr,
"Invalid quality value, must be between 1 and 100\n");
809 log_level = atoi(optarg);
811 fprintf(stderr,
"Unsupported log level, falling back to AM7XXX_LOG_ERROR\n");
816 power_mode = atoi(optarg);
823 fprintf(stdout,
"Power mode: %d\n", power_mode);
826 fprintf(stderr,
"Invalid power mode value, must be between %d and %d\n",
840 fprintf(stdout,
"Zoom: %d\n", zoom);
843 fprintf(stderr,
"Invalid zoom mode value, must be between %d and %d\n",
860 if (input_path == NULL) {
861 fprintf(stderr,
"The -i option must always be passed\n\n");
871 if (input_format_string && strcmp(input_format_string,
"x11grab") == 0) {
874 video_size = get_x_screen_size(input_path);
876 if (!av_dict_get(options,
"video_size", NULL, 0))
877 av_dict_set(&options,
"video_size", video_size, 0);
879 if (!av_dict_get(options,
"framerate", NULL, 0))
880 av_dict_set(&options,
"framerate",
"60", 0);
882 if (!av_dict_get(options,
"draw_mouse", NULL, 0))
883 av_dict_set(&options,
"draw_mouse",
"1", 0);
888 ret = set_signal_handler(unset_run);
896 perror(
"am7xxx_init");
904 perror(
"am7xxx_open_device");
910 perror(
"am7xxx_set_zoom_mode");
916 perror(
"am7xxx_set_power_mode");
924 ret = am7xxx_play(input_format_string,
934 fprintf(stderr,
"am7xxx_play failed\n");
941 av_dict_free(&options);
943 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.
int am7xxx_calc_scaled_image_dimensions(am7xxx_device *dev, unsigned int upscale, unsigned int original_width, unsigned int original_height, unsigned int *scaled_width, unsigned int *scaled_height)
Calculate the dimensions of an image to be shown on an am7xxx device.
@ 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.
int am7xxx_send_image_async(am7xxx_device *dev, am7xxx_image_format format, unsigned int width, unsigned int height, unsigned char *image, unsigned int image_size)
Queue transfer of an image for display on an am7xxx device and return immediately.
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.
void am7xxx_shutdown(am7xxx_context *ctx)
Cleanup the library data structures and free the context.
void am7xxx_set_log_level(am7xxx_context *ctx, am7xxx_log_level log_level)
Set verbosity level of log messages.
int am7xxx_set_power_mode(am7xxx_device *dev, am7xxx_power_mode power)
Set the power mode of an am7xxx device.
struct _am7xxx_device am7xxx_device
An opaque data type representing an am7xxx device.
int am7xxx_set_zoom_mode(am7xxx_device *dev, am7xxx_zoom_mode zoom)
Set the zoom mode of an am7xxx device.
struct _am7xxx_context am7xxx_context
An opaque data type representing a context.
int am7xxx_init(am7xxx_context **ctx)
Initialize the library context and data structures, and scan for devices.
int am7xxx_open_device(am7xxx_context *ctx, am7xxx_device **dev, unsigned int device_index)
Open an am7xxx_device according to a index.