2 * am7xxx-play - play stuff on an am7xxx device (e.g. Acer C110, PicoPix 1020)
4 * Copyright (C) 2012 Antonio Ospite <ospite@studenti.unina.it>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * @example examples/am7xxx-play.c
22 * am7xxx-play uses libavdevice, libavformat, libavcodec and libswscale to
23 * decode the input, encode it to jpeg and display it with libam7xxx.
32 #include <libavdevice/avdevice.h>
33 #include <libavformat/avformat.h>
34 #include <libswscale/swscale.h>
38 /* On some systems ENOTSUP is not defined, fallback to its value on
39 * linux which is equal to EOPNOTSUPP which is 95
45 static unsigned int run = 1;
47 struct video_input_ctx {
48 AVFormatContext *format_ctx;
49 AVCodecContext *codec_ctx;
50 int video_stream_index;
53 static int video_input_init(struct video_input_ctx *input_ctx,
54 const char *input_format_string,
55 const char *input_path,
56 AVDictionary **input_options)
58 AVInputFormat *input_format = NULL;
59 AVFormatContext *input_format_ctx;
60 AVCodecContext *input_codec_ctx;
66 avdevice_register_all();
67 avcodec_register_all();
70 if (input_format_string) {
71 /* find the desired input format */
72 input_format = av_find_input_format(input_format_string);
73 if (input_format == NULL) {
74 fprintf(stderr, "cannot find input format\n");
80 if (input_path == NULL) {
81 fprintf(stderr, "input_path must not be NULL!\n");
86 /* open the input format/device */
87 input_format_ctx = NULL;
88 ret = avformat_open_input(&input_format_ctx,
93 fprintf(stderr, "cannot open input format/device\n");
97 /* get information on the input stream (e.g. format, bitrate, framerate) */
98 ret = avformat_find_stream_info(input_format_ctx, NULL);
100 fprintf(stderr, "cannot get information on the stream\n");
104 /* dump what was found */
105 av_dump_format(input_format_ctx, 0, input_path, 0);
107 /* look for the first video_stream */
109 for (i = 0; i < input_format_ctx->nb_streams; i++)
110 if (input_format_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
114 if (video_index == -1) {
115 fprintf(stderr, "cannot find any video streams\n");
120 /* get a pointer to the codec context for the video stream */
121 input_codec_ctx = input_format_ctx->streams[video_index]->codec;
122 if (input_codec_ctx == NULL) {
123 fprintf(stderr, "input codec context is not valid\n");
128 /* find the decoder for the video stream */
129 input_codec = avcodec_find_decoder(input_codec_ctx->codec_id);
130 if (input_codec == NULL) {
131 fprintf(stderr, "input_codec is NULL!\n");
136 /* open the decoder */
137 ret = avcodec_open2(input_codec_ctx, input_codec, NULL);
139 fprintf(stderr, "cannot open input codec\n");
144 input_ctx->format_ctx = input_format_ctx;
145 input_ctx->codec_ctx = input_codec_ctx;
146 input_ctx->video_stream_index = video_index;
152 avformat_close_input(&input_format_ctx);
154 av_dict_free(input_options);
155 *input_options = NULL;
160 struct video_output_ctx {
161 AVCodecContext *codec_ctx;
165 static int video_output_init(struct video_output_ctx *output_ctx,
166 struct video_input_ctx *input_ctx,
167 unsigned int upscale,
168 unsigned int quality,
169 am7xxx_image_format image_format,
172 AVCodecContext *output_codec_ctx;
173 AVCodec *output_codec;
174 unsigned int new_output_width;
175 unsigned int new_output_height;
178 if (input_ctx == NULL) {
179 fprintf(stderr, "input_ctx must not be NULL!\n");
184 /* create the encoder context */
185 output_codec_ctx = avcodec_alloc_context3(NULL);
186 if (output_codec_ctx == NULL) {
187 fprintf(stderr, "cannot allocate output codec context!\n");
192 /* Calculate the new output dimension so the original picture is shown
194 ret = am7xxx_calc_scaled_image_dimensions(dev,
196 (input_ctx->codec_ctx)->width,
197 (input_ctx->codec_ctx)->height,
201 fprintf(stderr, "cannot calculate output dimension\n");
205 /* put sample parameters */
206 output_codec_ctx->bit_rate = (input_ctx->codec_ctx)->bit_rate;
207 output_codec_ctx->width = new_output_width;
208 output_codec_ctx->height = new_output_height;
209 output_codec_ctx->time_base.num =
210 (input_ctx->format_ctx)->streams[input_ctx->video_stream_index]->time_base.num;
211 output_codec_ctx->time_base.den =
212 (input_ctx->format_ctx)->streams[input_ctx->video_stream_index]->time_base.den;
214 /* When the raw format is requested we don't actually need to setup
217 if (image_format == AM7XXX_IMAGE_FORMAT_NV12) {
218 fprintf(stdout, "using raw output format\n");
219 output_codec_ctx->pix_fmt = PIX_FMT_NV12;
220 output_ctx->codec_ctx = output_codec_ctx;
221 output_ctx->raw_output = 1;
226 output_codec_ctx->pix_fmt = PIX_FMT_YUVJ420P;
227 output_codec_ctx->codec_id = CODEC_ID_MJPEG;
228 output_codec_ctx->codec_type = AVMEDIA_TYPE_VIDEO;
230 /* Set quality and other VBR settings */
232 /* @note: 'quality' is expected to be between 1 and 100, but a value
233 * between 0 to 99 has to be passed when calculating qmin and qmax.
234 * This way qmin and qmax will cover the range 1-FF_QUALITY_SCALE, and
235 * in particular they won't be 0, this is needed because they are used
236 * as divisor somewhere in the encoding process */
237 output_codec_ctx->qmin = output_codec_ctx->qmax = ((100 - (quality - 1)) * FF_QUALITY_SCALE) / 100;
238 output_codec_ctx->mb_lmin = output_codec_ctx->lmin = output_codec_ctx->qmin * FF_QP2LAMBDA;
239 output_codec_ctx->mb_lmax = output_codec_ctx->lmax = output_codec_ctx->qmax * FF_QP2LAMBDA;
240 output_codec_ctx->flags |= CODEC_FLAG_QSCALE;
241 output_codec_ctx->global_quality = output_codec_ctx->qmin * FF_QP2LAMBDA;
243 /* find the encoder */
244 output_codec = avcodec_find_encoder(output_codec_ctx->codec_id);
245 if (output_codec == NULL) {
246 fprintf(stderr, "cannot find output codec!\n");
252 ret = avcodec_open2(output_codec_ctx, output_codec, NULL);
254 fprintf(stderr, "could not open output codec!\n");
258 output_ctx->codec_ctx = output_codec_ctx;
259 output_ctx->raw_output = 0;
265 avcodec_close(output_codec_ctx);
266 av_free(output_codec_ctx);
272 static int am7xxx_play(const char *input_format_string,
273 AVDictionary **input_options,
274 const char *input_path,
275 unsigned int rescale_method,
276 unsigned int upscale,
277 unsigned int quality,
278 am7xxx_image_format image_format,
281 struct video_input_ctx input_ctx;
282 struct video_output_ctx output_ctx;
283 AVFrame *picture_raw;
284 AVFrame *picture_scaled;
287 int out_picture_size;
288 struct SwsContext *sw_scale_ctx;
293 ret = video_input_init(&input_ctx, input_format_string, input_path, input_options);
295 fprintf(stderr, "cannot initialize input\n");
299 ret = video_output_init(&output_ctx, &input_ctx, upscale, quality, image_format, dev);
301 fprintf(stderr, "cannot initialize input\n");
305 /* allocate an input frame */
306 picture_raw = avcodec_alloc_frame();
307 if (picture_raw == NULL) {
308 fprintf(stderr, "cannot allocate the raw picture frame!\n");
313 /* allocate output frame */
314 picture_scaled = avcodec_alloc_frame();
315 if (picture_scaled == NULL) {
316 fprintf(stderr, "cannot allocate the scaled picture!\n");
318 goto cleanup_picture_raw;
321 /* calculate the bytes needed for the output image and create buffer for the output image */
322 out_buf_size = avpicture_get_size((output_ctx.codec_ctx)->pix_fmt,
323 (output_ctx.codec_ctx)->width,
324 (output_ctx.codec_ctx)->height);
325 out_buf = av_malloc(out_buf_size * sizeof(uint8_t));
326 if (out_buf == NULL) {
327 fprintf(stderr, "cannot allocate output data buffer!\n");
329 goto cleanup_picture_scaled;
332 /* assign appropriate parts of buffer to image planes in picture_scaled */
333 avpicture_fill((AVPicture *)picture_scaled,
335 (output_ctx.codec_ctx)->pix_fmt,
336 (output_ctx.codec_ctx)->width,
337 (output_ctx.codec_ctx)->height);
339 sw_scale_ctx = sws_getCachedContext(NULL,
340 (input_ctx.codec_ctx)->width,
341 (input_ctx.codec_ctx)->height,
342 (input_ctx.codec_ctx)->pix_fmt,
343 (output_ctx.codec_ctx)->width,
344 (output_ctx.codec_ctx)->height,
345 (output_ctx.codec_ctx)->pix_fmt,
348 if (sw_scale_ctx == NULL) {
349 fprintf(stderr, "cannot set up the rescaling context!\n");
351 goto cleanup_out_buf;
356 ret = av_read_frame(input_ctx.format_ctx, &packet);
358 if (ret == (int)AVERROR_EOF || input_ctx.format_ctx->pb->eof_reached)
361 fprintf(stderr, "av_read_frame failed, EOF?\n");
366 if (packet.stream_index != input_ctx.video_stream_index) {
367 /* that is more or less a "continue", but there is
368 * still the packet to free */
374 ret = avcodec_decode_video2(input_ctx.codec_ctx, picture_raw, &got_picture, &packet);
376 fprintf(stderr, "cannot decode video\n");
381 /* if we get the complete frame */
383 /* convert it to YUV */
384 sws_scale(sw_scale_ctx,
385 (const uint8_t * const*)picture_raw->data,
386 picture_raw->linesize,
388 (input_ctx.codec_ctx)->height,
389 picture_scaled->data,
390 picture_scaled->linesize);
392 if (output_ctx.raw_output) {
393 out_picture_size = out_buf_size;
395 picture_scaled->quality = (output_ctx.codec_ctx)->global_quality;
396 /* TODO: switch to avcodec_encode_video2() eventually */
397 out_picture_size = avcodec_encode_video(output_ctx.codec_ctx,
401 if (out_picture_size < 0) {
402 fprintf(stderr, "cannot encode video\n");
403 ret = out_picture_size;
410 char filename[NAME_MAX];
412 if (!output_ctx.raw_output)
413 snprintf(filename, NAME_MAX, "out_q%03d.jpg", quality);
415 snprintf(filename, NAME_MAX, "out.raw");
416 file = fopen(filename, "wb");
417 fwrite(out_buf, 1, out_picture_size, file);
421 ret = am7xxx_send_image(dev,
423 (output_ctx.codec_ctx)->width,
424 (output_ctx.codec_ctx)->height,
428 perror("am7xxx_send_image");
434 av_free_packet(&packet);
437 sws_freeContext(sw_scale_ctx);
440 cleanup_picture_scaled:
441 av_free(picture_scaled);
443 av_free(picture_raw);
446 /* av_free is needed as well,
447 * see http://libav.org/doxygen/master/avcodec_8h.html#a5d7440cd7ea195bd0b14f21a00ef36dd
449 avcodec_close(output_ctx.codec_ctx);
450 av_free(output_ctx.codec_ctx);
453 avcodec_close(input_ctx.codec_ctx);
454 avformat_close_input(&(input_ctx.format_ctx));
462 static int x_get_screen_dimensions(const char *displayname, int *width, int *height)
464 int i, screen_number;
465 xcb_connection_t *connection;
466 const xcb_setup_t *setup;
467 xcb_screen_iterator_t iter;
469 connection = xcb_connect(displayname, &screen_number);
470 if (xcb_connection_has_error(connection)) {
471 fprintf(stderr, "Cannot open a connection to %s\n", displayname);
475 setup = xcb_get_setup(connection);
477 fprintf(stderr, "Cannot get setup for %s\n", displayname);
478 xcb_disconnect(connection);
482 iter = xcb_setup_roots_iterator(setup);
483 for (i = 0; i < screen_number; ++i) {
484 xcb_screen_next(&iter);
487 xcb_screen_t *screen = iter.data;
489 *width = screen->width_in_pixels;
490 *height = screen->height_in_pixels;
492 xcb_disconnect(connection);
497 static char *get_x_screen_size(const char *input_path)
505 ret = x_get_screen_dimensions(input_path, &width, &height);
507 fprintf(stderr, "Cannot get screen dimensions for %s\n", input_path);
511 len = snprintf(NULL, 0, "%dx%d", width, height);
513 screen_size = malloc((len + 1) * sizeof(char));
514 if (screen_size == NULL) {
519 len = snprintf(screen_size, len + 1, "%dx%d", width, height);
528 static char *get_x_screen_size(const char *input_path)
531 fprintf(stderr, "%s: fallback implementation\n", __func__);
532 return strdup("vga");
536 static void unset_run(int signo)
542 #ifdef HAVE_SIGACTION
543 static int set_signal_handler(void (*signal_handler)(int))
545 struct sigaction new_action;
546 struct sigaction old_action;
549 new_action.sa_handler = signal_handler;
550 sigemptyset(&new_action.sa_mask);
551 new_action.sa_flags = 0;
553 ret = sigaction(SIGINT, NULL, &old_action);
555 perror("sigaction on old_action");
559 if (old_action.sa_handler != SIG_IGN) {
560 ret = sigaction(SIGINT, &new_action, NULL);
562 perror("sigaction on new_action");
571 static int set_signal_handler(void (*signal_handler)(int))
573 (void)signal_handler;
574 fprintf(stderr, "set_signal_handler() not implemented, sigaction not available\n");
580 static void usage(char *name)
582 printf("usage: %s [OPTIONS]\n\n", name);
583 printf("OPTIONS:\n");
584 printf("\t-f <input format>\tthe input device format\n");
585 printf("\t-i <input path>\t\tthe input path\n");
586 printf("\t-o <options>\t\ta comma separated list of input format options\n");
587 printf("\t\t\t\tEXAMPLE:\n");
588 printf("\t\t\t\t\t-o draw_mouse=1,framerate=100,video_size=800x480\n");
589 printf("\t-s <scaling method>\tthe rescaling method (see swscale.h)\n");
590 printf("\t-u \t\t\tupscale the image if smaller than the display dimensions\n");
591 printf("\t-F <format>\t\tthe image format to use (default is JPEG)\n");
592 printf("\t\t\t\tSUPPORTED FORMATS:\n");
593 printf("\t\t\t\t\t1 - JPEG\n");
594 printf("\t\t\t\t\t2 - NV12\n");
595 printf("\t-q <quality>\t\tquality of jpeg sent to the device, between 1 and 100\n");
596 printf("\t-l <log level>\t\tthe verbosity level of libam7xxx output (0-5)\n");
597 printf("\t-p <power mode>\t\tthe power mode of device, between %d (off) and %d (turbo)\n",
598 AM7XXX_POWER_OFF, AM7XXX_POWER_TURBO);
599 printf("\t\t\t\tWARNING: Level 2 and greater require the master AND\n");
600 printf("\t\t\t\t the slave connector to be plugged in.\n");
601 printf("\t-z <zoom mode>\t\tthe display zoom mode, between %d (original) and %d (test)\n",
602 AM7XXX_ZOOM_ORIGINAL, AM7XXX_ZOOM_TEST);
603 printf("\t-h \t\t\tthis help message\n");
604 printf("\n\nEXAMPLES OF USE:\n");
605 printf("\t%s -f x11grab -i :0.0 -o video_size=800x480\n", name);
606 printf("\t%s -f fbdev -i /dev/fb0\n", name);
607 printf("\t%s -f video4linux2 -i /dev/video0 -o video_size=320x240,frame_rate=100 -u -q 90\n", name);
608 printf("\t%s -i http://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_640x360.m4v\n", name);
611 int main(int argc, char *argv[])
618 char *input_format_string = NULL;
619 AVDictionary *options = NULL;
620 char *input_path = NULL;
621 unsigned int rescale_method = SWS_BICUBIC;
622 unsigned int upscale = 0;
623 unsigned int quality = 95;
624 int log_level = AM7XXX_LOG_INFO;
625 am7xxx_power_mode power_mode = AM7XXX_POWER_LOW;
626 am7xxx_zoom_mode zoom = AM7XXX_ZOOM_ORIGINAL;
627 int format = AM7XXX_IMAGE_FORMAT_JPEG;
631 while ((opt = getopt(argc, argv, "f:i:o:s:uF:q:l:p:z:h")) != -1) {
634 input_format_string = strdup(optarg);
637 input_path = strdup(optarg);
642 * parse suboptions, the expected format is something
644 * draw_mouse=1,framerate=100,video_size=800x480
646 subopts = subopts_saved = strdup(optarg);
647 while((subopt = strtok_r(subopts, ",", &subopts))) {
648 char *subopt_name = strtok_r(subopt, "=", &subopt);
649 char *subopt_value = strtok_r(NULL, "", &subopt);
650 if (subopt_value == NULL) {
651 fprintf(stderr, "invalid suboption: %s\n", subopt_name);
654 av_dict_set(&options, subopt_name, subopt_value, 0);
658 fprintf(stderr, "Option '-o' not implemented\n");
662 rescale_method = atoi(optarg);
663 switch(rescale_method) {
664 case SWS_FAST_BILINEAR:
677 fprintf(stderr, "Unsupported rescale method\n");
686 format = atoi(optarg);
688 case AM7XXX_IMAGE_FORMAT_JPEG:
689 fprintf(stdout, "JPEG format\n");
691 case AM7XXX_IMAGE_FORMAT_NV12:
692 fprintf(stdout, "NV12 format\n");
695 fprintf(stderr, "Unsupported format\n");
701 quality = atoi(optarg);
702 if (quality < 1 || quality > 100) {
703 fprintf(stderr, "Invalid quality value, must be between 1 and 100\n");
709 log_level = atoi(optarg);
710 if (log_level < AM7XXX_LOG_FATAL || log_level > AM7XXX_LOG_TRACE) {
711 fprintf(stderr, "Unsupported log level, falling back to AM7XXX_LOG_ERROR\n");
712 log_level = AM7XXX_LOG_ERROR;
716 power_mode = atoi(optarg);
718 case AM7XXX_POWER_OFF:
719 case AM7XXX_POWER_LOW:
720 case AM7XXX_POWER_MIDDLE:
721 case AM7XXX_POWER_HIGH:
722 case AM7XXX_POWER_TURBO:
723 fprintf(stdout, "Power mode: %d\n", power_mode);
726 fprintf(stderr, "Invalid power mode value, must be between %d and %d\n",
727 AM7XXX_POWER_OFF, AM7XXX_POWER_TURBO);
735 case AM7XXX_ZOOM_ORIGINAL:
737 case AM7XXX_ZOOM_H_V:
738 case AM7XXX_ZOOM_TEST:
739 fprintf(stdout, "Zoom: %d\n", zoom);
742 fprintf(stderr, "Invalid zoom mode value, must be between %d and %d\n",
743 AM7XXX_ZOOM_ORIGINAL, AM7XXX_ZOOM_TEST);
759 if (input_path == NULL) {
760 fprintf(stderr, "The -i option must always be passed\n");
766 * When the input format is 'x11grab' set some useful fallback options
767 * if not supplied by the user, in particular grab full screen
769 if (input_format_string && strcmp(input_format_string, "x11grab") == 0) {
772 video_size = get_x_screen_size(input_path);
774 if (!av_dict_get(options, "video_size", NULL, 0))
775 av_dict_set(&options, "video_size", video_size, 0);
777 if (!av_dict_get(options, "framerate", NULL, 0))
778 av_dict_set(&options, "framerate", "60", 0);
780 if (!av_dict_get(options, "draw_mouse", NULL, 0))
781 av_dict_set(&options, "draw_mouse", "1", 0);
786 ret = set_signal_handler(unset_run);
792 ret = am7xxx_init(&ctx);
794 perror("am7xxx_init");
798 am7xxx_set_log_level(ctx, log_level);
800 ret = am7xxx_open_device(ctx, &dev, 0);
802 perror("am7xxx_open_device");
806 ret = am7xxx_set_zoom_mode(dev, zoom);
808 perror("am7xxx_set_zoom_mode");
812 ret = am7xxx_set_power_mode(dev, power_mode);
814 perror("am7xxx_set_power_mode");
818 /* When setting AM7XXX_ZOOM_TEST don't display the actual image */
819 if (zoom == AM7XXX_ZOOM_TEST)
822 ret = am7xxx_play(input_format_string,
831 fprintf(stderr, "am7xxx_play failed\n");
836 am7xxx_shutdown(ctx);
838 av_dict_free(&options);
840 free(input_format_string);