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 static unsigned int run = 1;
40 struct video_input_ctx {
41 AVFormatContext *format_ctx;
42 AVCodecContext *codec_ctx;
43 int video_stream_index;
46 static int video_input_init(struct video_input_ctx *input_ctx,
47 const char *input_format_string,
48 const char *input_path,
49 AVDictionary **input_options)
51 AVInputFormat *input_format = NULL;
52 AVFormatContext *input_format_ctx;
53 AVCodecContext *input_codec_ctx;
59 avdevice_register_all();
60 avcodec_register_all();
63 if (input_format_string) {
64 /* find the desired input format */
65 input_format = av_find_input_format(input_format_string);
66 if (input_format == NULL) {
67 fprintf(stderr, "cannot find input format\n");
73 if (input_path == NULL) {
74 fprintf(stderr, "input_path must not be NULL!\n");
79 /* open the input format/device */
80 input_format_ctx = NULL;
81 ret = avformat_open_input(&input_format_ctx,
86 fprintf(stderr, "cannot open input format/device\n");
90 /* get information on the input stream (e.g. format, bitrate, framerate) */
91 ret = avformat_find_stream_info(input_format_ctx, NULL);
93 fprintf(stderr, "cannot get information on the stream\n");
97 /* dump what was found */
98 av_dump_format(input_format_ctx, 0, input_path, 0);
100 /* look for the first video_stream */
102 for (i = 0; i < input_format_ctx->nb_streams; i++)
103 if (input_format_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
107 if (video_index == -1) {
108 fprintf(stderr, "cannot find any video streams\n");
113 /* get a pointer to the codec context for the video stream */
114 input_codec_ctx = input_format_ctx->streams[video_index]->codec;
115 if (input_codec_ctx == NULL) {
116 fprintf(stderr, "input codec context is not valid\n");
121 /* find the decoder for the video stream */
122 input_codec = avcodec_find_decoder(input_codec_ctx->codec_id);
123 if (input_codec == NULL) {
124 fprintf(stderr, "input_codec is NULL!\n");
129 /* open the decoder */
130 ret = avcodec_open2(input_codec_ctx, input_codec, NULL);
132 fprintf(stderr, "cannot open input codec\n");
137 input_ctx->format_ctx = input_format_ctx;
138 input_ctx->codec_ctx = input_codec_ctx;
139 input_ctx->video_stream_index = video_index;
145 avformat_close_input(&input_format_ctx);
147 av_dict_free(input_options);
148 *input_options = NULL;
153 struct video_output_ctx {
154 AVCodecContext *codec_ctx;
158 static int video_output_init(struct video_output_ctx *output_ctx,
159 struct video_input_ctx *input_ctx,
160 unsigned int upscale,
161 unsigned int quality,
162 am7xxx_image_format image_format,
165 AVCodecContext *output_codec_ctx;
166 AVCodec *output_codec;
167 unsigned int new_output_width;
168 unsigned int new_output_height;
171 if (input_ctx == NULL) {
172 fprintf(stderr, "input_ctx must not be NULL!\n");
177 /* create the encoder context */
178 output_codec_ctx = avcodec_alloc_context3(NULL);
179 if (output_codec_ctx == NULL) {
180 fprintf(stderr, "cannot allocate output codec context!\n");
185 /* Calculate the new output dimension so the original picture is shown
187 ret = am7xxx_calc_scaled_image_dimensions(dev,
189 (input_ctx->codec_ctx)->width,
190 (input_ctx->codec_ctx)->height,
194 fprintf(stderr, "cannot calculate output dimension\n");
198 /* put sample parameters */
199 output_codec_ctx->bit_rate = (input_ctx->codec_ctx)->bit_rate;
200 output_codec_ctx->width = new_output_width;
201 output_codec_ctx->height = new_output_height;
202 output_codec_ctx->time_base.num = (input_ctx->codec_ctx)->time_base.num;
203 output_codec_ctx->time_base.den = (input_ctx->codec_ctx)->time_base.den;
205 /* When the raw format is requested we don't actually need to setup
208 if (image_format == AM7XXX_IMAGE_FORMAT_NV12) {
209 fprintf(stdout, "using raw output format\n");
210 output_codec_ctx->pix_fmt = PIX_FMT_NV12;
211 output_ctx->codec_ctx = output_codec_ctx;
212 output_ctx->raw_output = 1;
217 output_codec_ctx->pix_fmt = PIX_FMT_YUVJ420P;
218 output_codec_ctx->codec_id = CODEC_ID_MJPEG;
219 output_codec_ctx->codec_type = AVMEDIA_TYPE_VIDEO;
221 /* Set quality and other VBR settings */
223 /* @note: 'quality' is expected to be between 1 and 100, but a value
224 * between 0 to 99 has to be passed when calculating qmin and qmax.
225 * This way qmin and qmax will cover the range 1-FF_QUALITY_SCALE, and
226 * in particular they won't be 0, this is needed because they are used
227 * as divisor somewhere in the encoding process */
228 output_codec_ctx->qmin = output_codec_ctx->qmax = ((100 - (quality - 1)) * FF_QUALITY_SCALE) / 100;
229 output_codec_ctx->mb_lmin = output_codec_ctx->lmin = output_codec_ctx->qmin * FF_QP2LAMBDA;
230 output_codec_ctx->mb_lmax = output_codec_ctx->lmax = output_codec_ctx->qmax * FF_QP2LAMBDA;
231 output_codec_ctx->flags |= CODEC_FLAG_QSCALE;
232 output_codec_ctx->global_quality = output_codec_ctx->qmin * FF_QP2LAMBDA;
234 /* find the encoder */
235 output_codec = avcodec_find_encoder(output_codec_ctx->codec_id);
236 if (output_codec == NULL) {
237 fprintf(stderr, "cannot find output codec!\n");
243 ret = avcodec_open2(output_codec_ctx, output_codec, NULL);
245 fprintf(stderr, "could not open output codec!\n");
249 output_ctx->codec_ctx = output_codec_ctx;
250 output_ctx->raw_output = 0;
256 avcodec_close(output_codec_ctx);
257 av_free(output_codec_ctx);
263 static int am7xxx_play(const char *input_format_string,
264 AVDictionary **input_options,
265 const char *input_path,
266 unsigned int rescale_method,
267 unsigned int upscale,
268 unsigned int quality,
269 am7xxx_image_format image_format,
272 struct video_input_ctx input_ctx;
273 struct video_output_ctx output_ctx;
274 AVFrame *picture_raw;
275 AVFrame *picture_scaled;
278 int out_picture_size;
279 struct SwsContext *sw_scale_ctx;
284 ret = video_input_init(&input_ctx, input_format_string, input_path, input_options);
286 fprintf(stderr, "cannot initialize input\n");
290 ret = video_output_init(&output_ctx, &input_ctx, upscale, quality, image_format, dev);
292 fprintf(stderr, "cannot initialize input\n");
296 /* allocate an input frame */
297 picture_raw = avcodec_alloc_frame();
298 if (picture_raw == NULL) {
299 fprintf(stderr, "cannot allocate the raw picture frame!\n");
304 /* allocate output frame */
305 picture_scaled = avcodec_alloc_frame();
306 if (picture_scaled == NULL) {
307 fprintf(stderr, "cannot allocate the scaled picture!\n");
309 goto cleanup_picture_raw;
312 /* calculate the bytes needed for the output image and create buffer for the output image */
313 out_buf_size = avpicture_get_size((output_ctx.codec_ctx)->pix_fmt,
314 (output_ctx.codec_ctx)->width,
315 (output_ctx.codec_ctx)->height);
316 out_buf = av_malloc(out_buf_size * sizeof(uint8_t));
317 if (out_buf == NULL) {
318 fprintf(stderr, "cannot allocate output data buffer!\n");
320 goto cleanup_picture_scaled;
323 /* assign appropriate parts of buffer to image planes in picture_scaled */
324 avpicture_fill((AVPicture *)picture_scaled,
326 (output_ctx.codec_ctx)->pix_fmt,
327 (output_ctx.codec_ctx)->width,
328 (output_ctx.codec_ctx)->height);
330 sw_scale_ctx = sws_getCachedContext(NULL,
331 (input_ctx.codec_ctx)->width,
332 (input_ctx.codec_ctx)->height,
333 (input_ctx.codec_ctx)->pix_fmt,
334 (output_ctx.codec_ctx)->width,
335 (output_ctx.codec_ctx)->height,
336 (output_ctx.codec_ctx)->pix_fmt,
339 if (sw_scale_ctx == NULL) {
340 fprintf(stderr, "cannot set up the rescaling context!\n");
342 goto cleanup_out_buf;
347 ret = av_read_frame(input_ctx.format_ctx, &packet);
349 if (ret == (int)AVERROR_EOF || input_ctx.format_ctx->pb->eof_reached)
352 fprintf(stderr, "av_read_frame failed, EOF?\n");
357 if (packet.stream_index != input_ctx.video_stream_index) {
358 /* that is more or less a "continue", but there is
359 * still the packet to free */
365 ret = avcodec_decode_video2(input_ctx.codec_ctx, picture_raw, &got_picture, &packet);
367 fprintf(stderr, "cannot decode video\n");
372 /* if we get the complete frame */
374 /* convert it to YUV */
375 sws_scale(sw_scale_ctx,
376 (const uint8_t * const*)picture_raw->data,
377 picture_raw->linesize,
379 (input_ctx.codec_ctx)->height,
380 picture_scaled->data,
381 picture_scaled->linesize);
383 if (output_ctx.raw_output) {
384 out_picture_size = out_buf_size;
386 picture_scaled->quality = (output_ctx.codec_ctx)->global_quality;
387 /* TODO: switch to avcodec_encode_video2() eventually */
388 out_picture_size = avcodec_encode_video(output_ctx.codec_ctx,
392 if (out_picture_size < 0) {
393 fprintf(stderr, "cannot encode video\n");
394 ret = out_picture_size;
401 char filename[NAME_MAX];
403 if (!output_ctx.raw_output)
404 snprintf(filename, NAME_MAX, "out_q%03d.jpg", quality);
406 snprintf(filename, NAME_MAX, "out.raw");
407 file = fopen(filename, "wb");
408 fwrite(out_buf, 1, out_picture_size, file);
412 ret = am7xxx_send_image(dev,
414 (output_ctx.codec_ctx)->width,
415 (output_ctx.codec_ctx)->height,
419 perror("am7xxx_send_image");
425 av_free_packet(&packet);
428 sws_freeContext(sw_scale_ctx);
431 cleanup_picture_scaled:
432 av_free(picture_scaled);
434 av_free(picture_raw);
437 /* av_free is needed as well,
438 * see http://libav.org/doxygen/master/avcodec_8h.html#a5d7440cd7ea195bd0b14f21a00ef36dd
440 avcodec_close(output_ctx.codec_ctx);
441 av_free(output_ctx.codec_ctx);
444 avcodec_close(input_ctx.codec_ctx);
445 avformat_close_input(&(input_ctx.format_ctx));
453 static int x_get_screen_dimensions(const char *displayname, int *width, int *height)
455 int i, screen_number;
456 xcb_connection_t *connection;
457 const xcb_setup_t *setup;
458 xcb_screen_iterator_t iter;
460 connection = xcb_connect(displayname, &screen_number);
461 if (xcb_connection_has_error(connection)) {
462 fprintf(stderr, "Cannot open a connection to %s\n", displayname);
466 setup = xcb_get_setup(connection);
468 fprintf(stderr, "Cannot get setup for %s\n", displayname);
469 xcb_disconnect(connection);
473 iter = xcb_setup_roots_iterator(setup);
474 for (i = 0; i < screen_number; ++i) {
475 xcb_screen_next(&iter);
478 xcb_screen_t *screen = iter.data;
480 *width = screen->width_in_pixels;
481 *height = screen->height_in_pixels;
483 xcb_disconnect(connection);
488 static char *get_x_screen_size(const char *input_path)
496 ret = x_get_screen_dimensions(input_path, &width, &height);
498 fprintf(stderr, "Cannot get screen dimensions for %s\n", input_path);
502 len = snprintf(NULL, 0, "%dx%d", width, height);
504 screen_size = malloc((len + 1) * sizeof(char));
505 if (screen_size == NULL) {
510 len = snprintf(screen_size, len + 1, "%dx%d", width, height);
519 static char *get_x_screen_size(const char *input_path)
522 fprintf(stderr, "%s: fallback implementation\n", __func__);
523 return strdup("vga");
527 static void unset_run(int signo)
533 static int set_signal_handler(void (*signal_handler)(int))
535 struct sigaction new_action;
536 struct sigaction old_action;
539 new_action.sa_handler = signal_handler;
540 sigemptyset(&new_action.sa_mask);
541 new_action.sa_flags = 0;
543 ret = sigaction(SIGINT, NULL, &old_action);
545 perror("sigaction on old_action");
549 if (old_action.sa_handler != SIG_IGN) {
550 ret = sigaction(SIGINT, &new_action, NULL);
552 perror("sigaction on new_action");
561 static void usage(char *name)
563 printf("usage: %s [OPTIONS]\n\n", name);
564 printf("OPTIONS:\n");
565 printf("\t-f <input format>\tthe input device format\n");
566 printf("\t-i <input path>\t\tthe input path\n");
567 printf("\t-o <options>\t\ta comma separated list of input format options\n");
568 printf("\t\t\t\tEXAMPLE:\n");
569 printf("\t\t\t\t\t-o draw_mouse=1,framerate=100,video_size=800x480\n");
570 printf("\t-s <scaling method>\tthe rescaling method (see swscale.h)\n");
571 printf("\t-u \t\t\tupscale the image if smaller than the display dimensions\n");
572 printf("\t-F <format>\t\tthe image format to use (default is JPEG)\n");
573 printf("\t\t\t\tSUPPORTED FORMATS:\n");
574 printf("\t\t\t\t\t1 - JPEG\n");
575 printf("\t\t\t\t\t2 - NV12\n");
576 printf("\t-q <quality>\t\tquality of jpeg sent to the device, between 1 and 100\n");
577 printf("\t-l <log level>\t\tthe verbosity level of libam7xxx output (0-5)\n");
578 printf("\t-p <power level>\tpower level of device, between %x (off) and %x (maximum)\n", AM7XXX_POWER_OFF, AM7XXX_POWER_TURBO);
579 printf("\t\t\t\tWARNING: Level 2 and greater require the master AND\n\t\t\t\t\t the slave connector to be plugged in.\n");
580 printf("\t-h \t\t\tthis help message\n");
581 printf("\n\nEXAMPLES OF USE:\n");
582 printf("\t%s -f x11grab -i :0.0 -o video_size=800x480\n", name);
583 printf("\t%s -f fbdev -i /dev/fb0\n", name);
584 printf("\t%s -f video4linux2 -i /dev/video0 -o video_size=320x240,frame_rate=100 -u -q 90\n", name);
585 printf("\t%s -i http://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_640x360.m4v\n", name);
588 int main(int argc, char *argv[])
595 char *input_format_string = NULL;
596 AVDictionary *options = NULL;
597 char *input_path = NULL;
598 unsigned int rescale_method = SWS_BICUBIC;
599 unsigned int upscale = 0;
600 unsigned int quality = 95;
601 int log_level = AM7XXX_LOG_INFO;
602 am7xxx_power_mode power_mode = AM7XXX_POWER_LOW;
603 int format = AM7XXX_IMAGE_FORMAT_JPEG;
607 while ((opt = getopt(argc, argv, "f:i:o:s:uF:q:l:hp:")) != -1) {
610 input_format_string = strdup(optarg);
613 input_path = strdup(optarg);
617 * parse suboptions, the expected format is something
619 * draw_mouse=1,framerate=100,video_size=800x480
621 subopts = subopts_saved = strdup(optarg);
622 while((subopt = strtok_r(subopts, ",", &subopts))) {
623 char *subopt_name = strtok_r(subopt, "=", &subopt);
624 char *subopt_value = strtok_r(NULL, "", &subopt);
625 if (subopt_value == NULL) {
626 fprintf(stderr, "invalid suboption: %s\n", subopt_name);
629 av_dict_set(&options, subopt_name, subopt_value, 0);
634 rescale_method = atoi(optarg);
635 switch(rescale_method) {
636 case SWS_FAST_BILINEAR:
649 fprintf(stderr, "Unsupported rescale method\n");
658 format = atoi(optarg);
660 case AM7XXX_IMAGE_FORMAT_JPEG:
661 fprintf(stdout, "JPEG format\n");
663 case AM7XXX_IMAGE_FORMAT_NV12:
664 fprintf(stdout, "NV12 format\n");
667 fprintf(stderr, "Unsupported format\n");
673 quality = atoi(optarg);
674 if (quality < 1 || quality > 100) {
675 fprintf(stderr, "Invalid quality value, must be between 1 and 100\n");
681 log_level = atoi(optarg);
682 if (log_level < AM7XXX_LOG_FATAL || log_level > AM7XXX_LOG_TRACE) {
683 fprintf(stderr, "Unsupported log level, falling back to AM7XXX_LOG_ERROR\n");
684 log_level = AM7XXX_LOG_ERROR;
693 power_mode = atoi(optarg);
695 case AM7XXX_POWER_OFF:
696 case AM7XXX_POWER_LOW:
697 case AM7XXX_POWER_MIDDLE:
698 case AM7XXX_POWER_HIGH:
699 case AM7XXX_POWER_TURBO:
700 fprintf(stdout, "Power mode: %x\n", power_mode);
703 fprintf(stderr, "Invalid power mode value, must be between %x and %x\n", AM7XXX_POWER_OFF, AM7XXX_POWER_TURBO);
715 if (input_path == NULL) {
716 fprintf(stderr, "The -i option must always be passed\n");
722 * When the input format is 'x11grab' set some useful fallback options
723 * if not supplied by the user, in particular grab full screen
725 if (input_format_string && strcmp(input_format_string, "x11grab") == 0) {
728 video_size = get_x_screen_size(input_path);
730 if (!av_dict_get(options, "video_size", NULL, 0))
731 av_dict_set(&options, "video_size", video_size, 0);
733 if (!av_dict_get(options, "framerate", NULL, 0))
734 av_dict_set(&options, "framerate", "60", 0);
736 if (!av_dict_get(options, "draw_mouse", NULL, 0))
737 av_dict_set(&options, "draw_mouse", "1", 0);
742 ret = set_signal_handler(unset_run);
748 ret = am7xxx_init(&ctx);
750 perror("am7xxx_init");
754 am7xxx_set_log_level(ctx, log_level);
756 ret = am7xxx_open_device(ctx, &dev, 0);
758 perror("am7xxx_open_device");
762 ret = am7xxx_set_power_mode(dev, power_mode);
764 perror("am7xxx_set_power_mode");
768 ret = am7xxx_play(input_format_string,
777 fprintf(stderr, "am7xxx_play failed\n");
782 am7xxx_shutdown(ctx);
784 av_dict_free(&options);
786 free(input_format_string);