libam7xxx 0.1
Communication library for Actions Micro AM7XXX based USB projectors and DPFs
Loading...
Searching...
No Matches
am7xxx-play.c
1/*
2 * am7xxx-play - play stuff on an am7xxx device (e.g. Acer C110, PicoPix 1020)
3 *
4 * Copyright (C) 2012-2014 Antonio Ospite <ao2@ao2.it>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
26#include <stdio.h>
27#include <stdint.h>
28#include <string.h>
29#include <signal.h>
30#include <getopt.h>
31
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>
37
38#include <am7xxx.h>
39
40static unsigned int run = 1;
41
42struct video_input_ctx {
43 AVFormatContext *format_ctx;
44 AVCodecContext *codec_ctx;
45 int video_stream_index;
46};
47
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)
52{
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;
58 int video_index;
59 int ret;
60
61 avdevice_register_all();
62#if LIBAVCODEC_VERSION_MAJOR < 59
63 avcodec_register_all();
64 av_register_all();
65#endif
66
67 if (input_format_string) {
68 /* find the desired input format */
69 input_format = av_find_input_format(input_format_string);
70 if (input_format == NULL) {
71 fprintf(stderr, "cannot find input format\n");
72 ret = -ENODEV;
73 goto out;
74 }
75 }
76
77 if (input_path == NULL) {
78 fprintf(stderr, "input_path must not be NULL!\n");
79 ret = -EINVAL;
80 goto out;
81 }
82
83 /* open the input format/device */
84 input_format_ctx = NULL;
85 ret = avformat_open_input(&input_format_ctx,
86 input_path,
87 input_format,
88 input_options);
89 if (ret < 0) {
90 fprintf(stderr, "cannot open input format/device\n");
91 goto out;
92 }
93
94 /* get information on the input stream (e.g. format, bitrate, framerate) */
95 ret = avformat_find_stream_info(input_format_ctx, NULL);
96 if (ret < 0) {
97 fprintf(stderr, "cannot get information on the stream\n");
98 goto cleanup;
99 }
100
101 /* dump what was found */
102 av_dump_format(input_format_ctx, 0, input_path, 0);
103
104 /* look for the first video_stream */
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");
108 ret = -EINVAL;
109 goto cleanup;
110 }
111
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");
115 ret = -ENOMEM;
116 goto cleanup;
117 }
118
119 input_codec_params = input_format_ctx->streams[video_index]->codecpar;
120 ret = avcodec_parameters_to_context(input_codec_ctx, input_codec_params);
121 if (ret < 0) {
122 fprintf(stderr, "cannot copy parameters to input codec context\n");
123 goto cleanup_ctx;
124 }
125
126 /* open the decoder */
127 ret = avcodec_open2(input_codec_ctx, input_codec, NULL);
128 if (ret < 0) {
129 fprintf(stderr, "cannot open input codec\n");
130 goto cleanup_ctx;
131 }
132
133 input_ctx->format_ctx = input_format_ctx;
134 input_ctx->codec_ctx = input_codec_ctx;
135 input_ctx->video_stream_index = video_index;
136
137 ret = 0;
138 goto out;
139
140cleanup_ctx:
141 avcodec_free_context(&input_codec_ctx);
142cleanup:
143 avformat_close_input(&input_format_ctx);
144out:
145 av_dict_free(input_options);
146 *input_options = NULL;
147 return ret;
148}
149
150
151struct video_output_ctx {
152 AVCodecContext *codec_ctx;
153 int raw_output;
154};
155
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,
160 am7xxx_image_format image_format,
161 am7xxx_device *dev)
162{
163 AVCodecContext *output_codec_ctx;
164 AVCodec const *output_codec;
165 unsigned int new_output_width;
166 unsigned int new_output_height;
167 int ret;
168
169 if (input_ctx == NULL) {
170 fprintf(stderr, "input_ctx must not be NULL!\n");
171 ret = -EINVAL;
172 goto out;
173 }
174
175 /* create the encoder context */
176 output_codec_ctx = avcodec_alloc_context3(NULL);
177 if (output_codec_ctx == NULL) {
178 fprintf(stderr, "cannot allocate output codec context!\n");
179 ret = -ENOMEM;
180 goto out;
181 }
182
183 /* Calculate the new output dimension so the original frame is shown
184 * in its entirety */
185 ret = am7xxx_calc_scaled_image_dimensions(dev,
186 upscale,
187 (input_ctx->codec_ctx)->width,
188 (input_ctx->codec_ctx)->height,
189 &new_output_width,
190 &new_output_height);
191 if (ret < 0) {
192 fprintf(stderr, "cannot calculate output dimension\n");
193 goto cleanup;
194 }
195
196 /* put sample parameters */
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;
204
205 /* When the raw format is requested we don't actually need to setup
206 * and open a decoder
207 */
208 if (image_format == AM7XXX_IMAGE_FORMAT_NV12) {
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;
213 ret = 0;
214 goto out;
215 }
216
217 /* YUVJ420P is deprecated in swscaler, but mjpeg still relies on it. */
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;
221
222 /* Set quality and other VBR settings */
223
224 /* @note: 'quality' is expected to be between 1 and 100, but a value
225 * between 0 to 99 has to be passed when calculating qmin and qmax.
226 * This way qmin and qmax will cover the range 1-FF_QUALITY_SCALE, and
227 * in particular they won't be 0, this is needed because they are used
228 * as divisor somewhere in the encoding process */
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;
234
235 /* find the encoder */
236 output_codec = avcodec_find_encoder(output_codec_ctx->codec_id);
237 if (output_codec == NULL) {
238 fprintf(stderr, "cannot find output codec!\n");
239 ret = -EINVAL;
240 goto cleanup;
241 }
242
243 /* open the codec */
244 ret = avcodec_open2(output_codec_ctx, output_codec, NULL);
245 if (ret < 0) {
246 fprintf(stderr, "could not open output codec!\n");
247 goto cleanup;
248 }
249
250 output_ctx->codec_ctx = output_codec_ctx;
251 output_ctx->raw_output = 0;
252
253 ret = 0;
254 goto out;
255
256cleanup:
257 avcodec_free_context(&output_codec_ctx);
258out:
259 return ret;
260}
261
262
263/*
264 * Wrap the new avcodec API from FFMpeg 3.1 to minimize the changes in the
265 * user code.
266 *
267 * If the use of the wrappers were to be made conditional, a check like the
268 * following could be used:
269 *
270 * #if (LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101))
271 *
272 * As derived from the APIchanges document:
273 * https://github.com/FFmpeg/FFmpeg/blob/master/doc/APIchanges
274 *
275 * The wrapper implementation has been taken from:
276 * https://blogs.gentoo.org/lu_zero/2016/03/29/new-avcodec-api/
277 */
278static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
279{
280 int ret;
281
282 *got_frame = 0;
283
284 if (pkt) {
285 ret = avcodec_send_packet(avctx, pkt);
286 /*
287 * In particular, we don't expect AVERROR(EAGAIN), because we
288 * read all decoded frames with avcodec_receive_frame() until
289 * done.
290 */
291 if (ret < 0)
292 return ret == AVERROR_EOF ? 0 : ret;
293 }
294
295 ret = avcodec_receive_frame(avctx, frame);
296 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
297 return ret;
298 if (ret >= 0)
299 *got_frame = 1;
300
301 return 0;
302}
303
304static int encode(AVCodecContext *avctx, AVPacket *pkt, int *got_packet, AVFrame *frame)
305{
306 int ret;
307
308 *got_packet = 0;
309
310 ret = avcodec_send_frame(avctx, frame);
311 if (ret < 0)
312 return ret;
313
314 ret = avcodec_receive_packet(avctx, pkt);
315 if (!ret)
316 *got_packet = 1;
317 if (ret == AVERROR(EAGAIN))
318 return 0;
319
320 return ret;
321}
322
323
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,
330 am7xxx_image_format image_format,
331 am7xxx_device *dev,
332 int dump_frame)
333{
334 struct video_input_ctx input_ctx;
335 struct video_output_ctx output_ctx;
336 AVFrame *frame_raw;
337 AVFrame *frame_scaled;
338 int out_buf_size;
339 uint8_t *out_buf;
340 int out_frame_size;
341 uint8_t *out_frame;
342 struct SwsContext *sw_scale_ctx;
343 AVPacket in_packet;
344 AVPacket out_packet;
345 int got_frame;
346 int got_packet;
347 int ret;
348
349 ret = video_input_init(&input_ctx, input_format_string, input_path, input_options);
350 if (ret < 0) {
351 fprintf(stderr, "cannot initialize input\n");
352 goto out;
353 }
354
355 ret = video_output_init(&output_ctx, &input_ctx, upscale, quality, image_format, dev);
356 if (ret < 0) {
357 fprintf(stderr, "cannot initialize input\n");
358 goto cleanup_input;
359 }
360
361 /* allocate an input frame */
362 frame_raw = av_frame_alloc();
363 if (frame_raw == NULL) {
364 fprintf(stderr, "cannot allocate the raw frame!\n");
365 ret = -ENOMEM;
366 goto cleanup_output;
367 }
368
369 /* allocate output frame */
370 frame_scaled = av_frame_alloc();
371 if (frame_scaled == NULL) {
372 fprintf(stderr, "cannot allocate the scaled frame!\n");
373 ret = -ENOMEM;
374 goto cleanup_frame_raw;
375 }
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;
379
380 /* calculate the bytes needed for the output image and create buffer for the output image */
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,
384 1);
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");
388 ret = -ENOMEM;
389 goto cleanup_frame_scaled;
390 }
391
392 /* assign appropriate parts of buffer to image planes in frame_scaled */
393 av_image_fill_arrays(frame_scaled->data,
394 frame_scaled->linesize,
395 out_buf,
396 (output_ctx.codec_ctx)->pix_fmt,
397 (output_ctx.codec_ctx)->width,
398 (output_ctx.codec_ctx)->height,
399 1);
400
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,
408 rescale_method,
409 NULL, NULL, NULL);
410 if (sw_scale_ctx == NULL) {
411 fprintf(stderr, "cannot set up the rescaling context!\n");
412 ret = -EINVAL;
413 goto cleanup_out_buf;
414 }
415
416 got_packet = 0;
417 while (run) {
418 /* read packet */
419 ret = av_read_frame(input_ctx.format_ctx, &in_packet);
420 if (ret < 0) {
421 if (ret == (int)AVERROR_EOF || input_ctx.format_ctx->pb->eof_reached)
422 ret = 0;
423 else
424 fprintf(stderr, "av_read_frame failed, EOF?\n");
425 run = 0;
426 goto end_while;
427 }
428
429 if (in_packet.stream_index != input_ctx.video_stream_index) {
430 /* that is more or less a "continue", but there is
431 * still the packet to free */
432 goto end_while;
433 }
434
435 /* decode */
436 got_frame = 0;
437 ret = decode(input_ctx.codec_ctx, frame_raw, &got_frame, &in_packet);
438 if (ret < 0) {
439 fprintf(stderr, "cannot decode video\n");
440 run = 0;
441 goto end_while;
442 }
443
444 /* if we got the complete frame */
445 if (got_frame) {
446 /*
447 * Rescaling the frame also changes its pixel format
448 * to the raw format supported by the projector if
449 * this was set in video_output_init()
450 */
451 sws_scale(sw_scale_ctx,
452 (const uint8_t * const *)frame_raw->data,
453 frame_raw->linesize,
454 0,
455 (input_ctx.codec_ctx)->height,
456 frame_scaled->data,
457 frame_scaled->linesize);
458
459 if (output_ctx.raw_output) {
460 out_frame = out_buf;
461 out_frame_size = out_buf_size;
462 } else {
463 frame_scaled->quality = (output_ctx.codec_ctx)->global_quality;
464 av_init_packet(&out_packet);
465 out_packet.data = NULL;
466 out_packet.size = 0;
467 got_packet = 0;
468 ret = encode(output_ctx.codec_ctx,
469 &out_packet,
470 &got_packet,
471 frame_scaled);
472 if (ret < 0 || !got_packet) {
473 fprintf(stderr, "cannot encode video\n");
474 run = 0;
475 goto end_while;
476 }
477
478 out_frame = out_packet.data;
479 out_frame_size = out_packet.size;
480 }
481
482#ifdef DEBUG
483 if (dump_frame) {
484 char filename[NAME_MAX];
485 FILE *file;
486 if (!output_ctx.raw_output)
487 snprintf(filename, NAME_MAX, "out_q%03d.jpg", quality);
488 else
489 snprintf(filename, NAME_MAX, "out.raw");
490 file = fopen(filename, "wb");
491 fwrite(out_frame, 1, out_frame_size, file);
492 fclose(file);
493 }
494#else
495 (void) dump_frame;
496#endif
497
498 ret = am7xxx_send_image_async(dev,
499 image_format,
500 (output_ctx.codec_ctx)->width,
501 (output_ctx.codec_ctx)->height,
502 out_frame,
503 out_frame_size);
504 if (ret < 0) {
505 perror("am7xxx_send_image_async");
506 run = 0;
507 goto end_while;
508 }
509 }
510end_while:
511 if (!output_ctx.raw_output && got_packet)
512 av_packet_unref(&out_packet);
513 av_packet_unref(&in_packet);
514 }
515
516 sws_freeContext(sw_scale_ctx);
517cleanup_out_buf:
518 av_free(out_buf);
519cleanup_frame_scaled:
520 av_frame_free(&frame_scaled);
521cleanup_frame_raw:
522 av_frame_free(&frame_raw);
523
524cleanup_output:
525 /* Freeing the codec context is needed as well,
526 * see https://libav.org/documentation/doxygen/master/group__lavc__core.html#gaf4daa92361efb3523ef5afeb0b54077f
527 */
528 avcodec_close(output_ctx.codec_ctx);
529 avcodec_free_context(&(output_ctx.codec_ctx));
530
531cleanup_input:
532 avcodec_close(input_ctx.codec_ctx);
533 avcodec_free_context(&(input_ctx.codec_ctx));
534 avformat_close_input(&(input_ctx.format_ctx));
535
536out:
537 return ret;
538}
539
540#ifdef HAVE_XCB
541#include <xcb/xcb.h>
542static int x_get_screen_dimensions(const char *displayname, int *width, int *height)
543{
544 int i, screen_number;
545 xcb_connection_t *connection;
546 const xcb_setup_t *setup;
547 xcb_screen_iterator_t iter;
548
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);
552 return -EINVAL;
553 }
554
555 setup = xcb_get_setup(connection);
556 if (setup == NULL) {
557 fprintf(stderr, "Cannot get setup for %s\n", displayname);
558 xcb_disconnect(connection);
559 return -EINVAL;
560 }
561
562 iter = xcb_setup_roots_iterator(setup);
563 for (i = 0; i < screen_number; ++i) {
564 xcb_screen_next(&iter);
565 }
566
567 xcb_screen_t *screen = iter.data;
568
569 *width = screen->width_in_pixels;
570 *height = screen->height_in_pixels;
571
572 xcb_disconnect(connection);
573
574 return 0;
575}
576
577static char *get_x_screen_size(const char *input_path)
578{
579 int len;
580 int width;
581 int height;
582 char *screen_size;
583 int ret;
584
585 ret = x_get_screen_dimensions(input_path, &width, &height);
586 if (ret < 0) {
587 fprintf(stderr, "Cannot get screen dimensions for %s\n", input_path);
588 return NULL;
589 }
590
591 len = snprintf(NULL, 0, "%dx%d", width, height);
592
593 screen_size = malloc((len + 1) * sizeof(char));
594 if (screen_size == NULL) {
595 perror("malloc");
596 return NULL;
597 }
598
599 len = snprintf(screen_size, len + 1, "%dx%d", width, height);
600 if (len < 0) {
601 free(screen_size);
602 screen_size = NULL;
603 return NULL;
604 }
605 return screen_size;
606}
607#else
608static char *get_x_screen_size(const char *input_path)
609{
610 (void) input_path;
611 fprintf(stderr, "%s: fallback implementation, assuming a vga screen\n", __func__);
612 return strdup("vga");
613}
614#endif
615
616static void unset_run(int signo)
617{
618 (void) signo;
619 run = 0;
620}
621
622#ifdef HAVE_SIGACTION
623static int set_signal_handler(void (*signal_handler)(int))
624{
625 struct sigaction new_action;
626 struct sigaction old_action;
627 int ret;
628
629 new_action.sa_handler = signal_handler;
630 sigemptyset(&new_action.sa_mask);
631 new_action.sa_flags = 0;
632
633 ret = sigaction(SIGINT, NULL, &old_action);
634 if (ret < 0) {
635 perror("sigaction on old_action");
636 goto out;
637 }
638
639 if (old_action.sa_handler != SIG_IGN) {
640 ret = sigaction(SIGINT, &new_action, NULL);
641 if (ret < 0) {
642 perror("sigaction on new_action");
643 goto out;
644 }
645 }
646
647out:
648 return ret;
649}
650#else
651static int set_signal_handler(void (*signal_handler)(int))
652{
653 (void)signal_handler;
654 fprintf(stderr, "set_signal_handler() not implemented, sigaction not available\n");
655 return 0;
656}
657#endif
658
659
660static void usage(char *name)
661{
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");
665#ifdef DEBUG
666 printf("\t-D \t\t\tdump the last frame to a file (only active in DEBUG mode)\n");
667#endif
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);
693}
694
695int main(int argc, char *argv[])
696{
697 int ret;
698 int opt;
699 char *subopts;
700 char *subopts_saved;
701 char *subopt;
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;
708 int log_level = AM7XXX_LOG_INFO;
709 int device_index = 0;
710 int power_mode = AM7XXX_POWER_LOW;
711 int zoom = AM7XXX_ZOOM_ORIGINAL;
712 int format = AM7XXX_IMAGE_FORMAT_JPEG;
713 am7xxx_context *ctx;
714 am7xxx_device *dev;
715 int dump_frame = 0;
716
717 while ((opt = getopt(argc, argv, "d:Df:i:o:s:uF:q:l:p:z:h")) != -1) {
718 switch (opt) {
719 case 'd':
720 device_index = atoi(optarg);
721 if (device_index < 0) {
722 fprintf(stderr, "Unsupported device index\n");
723 ret = -EINVAL;
724 goto out;
725 }
726 break;
727 case 'D':
728 dump_frame = 1;
729#ifndef DEBUG
730 fprintf(stderr, "Warning: the -D option is only active in DEBUG mode.\n");
731#endif
732 break;
733 case 'f':
734 input_format_string = strdup(optarg);
735 break;
736 case 'i':
737 input_path = strdup(optarg);
738 break;
739 case 'o':
740#ifdef HAVE_STRTOK_R
741 /*
742 * parse suboptions, the expected format is something
743 * like:
744 * draw_mouse=1,framerate=100,video_size=800x480
745 */
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);
752 continue;
753 }
754 av_dict_set(&options, subopt_name, subopt_value, 0);
755 }
756 free(subopts_saved);
757#else
758 fprintf(stderr, "Option '-o' not implemented\n");
759#endif
760 break;
761 case 's':
762 rescale_method = atoi(optarg);
763 switch(rescale_method) {
764 case SWS_FAST_BILINEAR:
765 case SWS_BILINEAR:
766 case SWS_BICUBIC:
767 case SWS_X:
768 case SWS_POINT:
769 case SWS_AREA:
770 case SWS_BICUBLIN:
771 case SWS_GAUSS:
772 case SWS_SINC:
773 case SWS_LANCZOS:
774 case SWS_SPLINE:
775 break;
776 default:
777 fprintf(stderr, "Unsupported rescale method\n");
778 ret = -EINVAL;
779 goto out;
780 }
781 break;
782 case 'u':
783 upscale = 1;
784 break;
785 case 'F':
786 format = atoi(optarg);
787 switch(format) {
789 fprintf(stdout, "JPEG format\n");
790 break;
792 fprintf(stdout, "NV12 format\n");
793 break;
794 default:
795 fprintf(stderr, "Unsupported format\n");
796 ret = -EINVAL;
797 goto out;
798 }
799 break;
800 case 'q':
801 quality = atoi(optarg);
802 if (quality < 1 || quality > 100) {
803 fprintf(stderr, "Invalid quality value, must be between 1 and 100\n");
804 ret = -EINVAL;
805 goto out;
806 }
807 break;
808 case 'l':
809 log_level = atoi(optarg);
810 if (log_level < AM7XXX_LOG_FATAL || log_level > AM7XXX_LOG_TRACE) {
811 fprintf(stderr, "Unsupported log level, falling back to AM7XXX_LOG_ERROR\n");
812 log_level = AM7XXX_LOG_ERROR;
813 }
814 break;
815 case 'p':
816 power_mode = atoi(optarg);
817 switch(power_mode) {
818 case AM7XXX_POWER_OFF:
819 case AM7XXX_POWER_LOW:
823 fprintf(stdout, "Power mode: %d\n", power_mode);
824 break;
825 default:
826 fprintf(stderr, "Invalid power mode value, must be between %d and %d\n",
828 ret = -EINVAL;
829 goto out;
830 }
831 break;
832 case 'z':
833 zoom = atoi(optarg);
834 switch(zoom) {
836 case AM7XXX_ZOOM_H:
837 case AM7XXX_ZOOM_H_V:
838 case AM7XXX_ZOOM_TEST:
839 case AM7XXX_ZOOM_TELE:
840 fprintf(stdout, "Zoom: %d\n", zoom);
841 break;
842 default:
843 fprintf(stderr, "Invalid zoom mode value, must be between %d and %d\n",
845 ret = -EINVAL;
846 goto out;
847 }
848 break;
849 case 'h':
850 usage(argv[0]);
851 ret = 0;
852 goto out;
853 default: /* '?' */
854 usage(argv[0]);
855 ret = -EINVAL;
856 goto out;
857 }
858 }
859
860 if (input_path == NULL) {
861 fprintf(stderr, "The -i option must always be passed\n\n");
862 usage(argv[0]);
863 ret = -EINVAL;
864 goto out;
865 }
866
867 /*
868 * When the input format is 'x11grab' set some useful fallback options
869 * if not supplied by the user, in particular grab full screen
870 */
871 if (input_format_string && strcmp(input_format_string, "x11grab") == 0) {
872 char *video_size;
873
874 video_size = get_x_screen_size(input_path);
875
876 if (!av_dict_get(options, "video_size", NULL, 0))
877 av_dict_set(&options, "video_size", video_size, 0);
878
879 if (!av_dict_get(options, "framerate", NULL, 0))
880 av_dict_set(&options, "framerate", "60", 0);
881
882 if (!av_dict_get(options, "draw_mouse", NULL, 0))
883 av_dict_set(&options, "draw_mouse", "1", 0);
884
885 free(video_size);
886 }
887
888 ret = set_signal_handler(unset_run);
889 if (ret < 0) {
890 perror("sigaction");
891 goto out;
892 }
893
894 ret = am7xxx_init(&ctx);
895 if (ret < 0) {
896 perror("am7xxx_init");
897 goto out;
898 }
899
900 am7xxx_set_log_level(ctx, log_level);
901
902 ret = am7xxx_open_device(ctx, &dev, device_index);
903 if (ret < 0) {
904 perror("am7xxx_open_device");
905 goto cleanup;
906 }
907
908 ret = am7xxx_set_zoom_mode(dev, zoom);
909 if (ret < 0) {
910 perror("am7xxx_set_zoom_mode");
911 goto cleanup;
912 }
913
914 ret = am7xxx_set_power_mode(dev, power_mode);
915 if (ret < 0) {
916 perror("am7xxx_set_power_mode");
917 goto cleanup;
918 }
919
920 /* When setting AM7XXX_ZOOM_TEST don't display the actual image */
921 if (zoom == AM7XXX_ZOOM_TEST)
922 goto cleanup;
923
924 ret = am7xxx_play(input_format_string,
925 &options,
926 input_path,
927 rescale_method,
928 upscale,
929 quality,
930 format,
931 dev,
932 dump_frame);
933 if (ret < 0) {
934 fprintf(stderr, "am7xxx_play failed\n");
935 goto cleanup;
936 }
937
938cleanup:
939 am7xxx_shutdown(ctx);
940out:
941 av_dict_free(&options);
942 free(input_path);
943 free(input_format_string);
944 return ret;
945}
Public libam7xxx API.
@ AM7XXX_ZOOM_H
Zoom 1: H Scale (changes aspect ratio).
Definition am7xxx.h:124
@ AM7XXX_ZOOM_ORIGINAL
Original Size, as retrieved via am7xxx_device_info.
Definition am7xxx.h:123
@ AM7XXX_ZOOM_TELE
Zoom Tele: available on some PicoPix models.
Definition am7xxx.h:127
@ AM7XXX_ZOOM_TEST
Zoom test screen, the firmware version is shown as well.
Definition am7xxx.h:126
@ AM7XXX_ZOOM_H_V
Zoom 2: H/V Scale (changes aspect ratio).
Definition am7xxx.h:125
@ AM7XXX_LOG_ERROR
Error messages, typically they describe API functions failures.
Definition am7xxx.h:70
@ AM7XXX_LOG_INFO
Informations about the device operations.
Definition am7xxx.h:72
@ AM7XXX_LOG_TRACE
Verbose informations about the communication with the hardware.
Definition am7xxx.h:74
@ AM7XXX_POWER_HIGH
More brightness, but more power consumption.
Definition am7xxx.h:103
@ AM7XXX_POWER_OFF
Display is powered off, no image shown.
Definition am7xxx.h:100
@ AM7XXX_POWER_TURBO
Max brightness and power consumption.
Definition am7xxx.h:104
@ AM7XXX_POWER_LOW
Low power consumption but also low brightness.
Definition am7xxx.h:101
@ AM7XXX_POWER_MIDDLE
Middle level of brightness.
Definition am7xxx.h:102
am7xxx_image_format
The image formats accepted by the device.
Definition am7xxx.h:80
@ AM7XXX_IMAGE_FORMAT_JPEG
JPEG format.
Definition am7xxx.h:81
@ AM7XXX_IMAGE_FORMAT_NV12
Raw YUV in the NV12 variant.
Definition am7xxx.h:82
struct _am7xxx_context am7xxx_context
An opaque data type representing a context.
Definition am7xxx.h:38
struct _am7xxx_device am7xxx_device
An opaque data type representing an am7xxx device.
Definition am7xxx.h:46