2d1ef48e0d78a6969bfc3efb86ec37a749e61003
[libam7xxx.git] / examples / 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
20 /**
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.
24  */
25
26 #include <stdio.h>
27 #include <stdint.h>
28 #include <string.h>
29 #include <signal.h>
30 #include <getopt.h>
31
32 #include <libavdevice/avdevice.h>
33 #include <libavformat/avformat.h>
34 #include <libavutil/imgutils.h>
35 #include <libswscale/swscale.h>
36
37 #include <am7xxx.h>
38
39 static unsigned int run = 1;
40
41 struct video_input_ctx {
42         AVFormatContext *format_ctx;
43         AVCodecContext  *codec_ctx;
44         int video_stream_index;
45 };
46
47 static int video_input_init(struct video_input_ctx *input_ctx,
48                             const char *input_format_string,
49                             const char *input_path,
50                             AVDictionary **input_options)
51 {
52         AVInputFormat *input_format = NULL;
53         AVFormatContext *input_format_ctx;
54         AVCodecContext *input_codec_ctx;
55         AVCodec *input_codec;
56         int video_index;
57         unsigned int i;
58         int ret;
59
60         avdevice_register_all();
61         avcodec_register_all();
62         av_register_all();
63
64         if (input_format_string) {
65                 /* find the desired input format */
66                 input_format = av_find_input_format(input_format_string);
67                 if (input_format == NULL) {
68                         fprintf(stderr, "cannot find input format\n");
69                         ret = -ENODEV;
70                         goto out;
71                 }
72         }
73
74         if (input_path == NULL) {
75                 fprintf(stderr, "input_path must not be NULL!\n");
76                 ret = -EINVAL;
77                 goto out;
78         }
79
80         /* open the input format/device */
81         input_format_ctx = NULL;
82         ret = avformat_open_input(&input_format_ctx,
83                                   input_path,
84                                   input_format,
85                                   input_options);
86         if (ret < 0) {
87                 fprintf(stderr, "cannot open input format/device\n");
88                 goto out;
89         }
90
91         /* get information on the input stream (e.g. format, bitrate, framerate) */
92         ret = avformat_find_stream_info(input_format_ctx, NULL);
93         if (ret < 0) {
94                 fprintf(stderr, "cannot get information on the stream\n");
95                 goto cleanup;
96         }
97
98         /* dump what was found */
99         av_dump_format(input_format_ctx, 0, input_path, 0);
100
101         /* look for the first video_stream */
102         video_index = -1;
103         for (i = 0; i < input_format_ctx->nb_streams; i++)
104                 if (input_format_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
105                         video_index = i;
106                         break;
107                 }
108         if (video_index == -1) {
109                 fprintf(stderr, "cannot find any video streams\n");
110                 ret = -EINVAL;
111                 goto cleanup;
112         }
113
114         /* get a pointer to the codec context for the video stream */
115         input_codec_ctx = input_format_ctx->streams[video_index]->codec;
116
117         /* find the decoder for the video stream */
118         input_codec = avcodec_find_decoder(input_codec_ctx->codec_id);
119         if (input_codec == NULL) {
120                 fprintf(stderr, "input_codec is NULL!\n");
121                 ret = -EINVAL;
122                 goto cleanup;
123         }
124
125         /* open the decoder */
126         ret = avcodec_open2(input_codec_ctx, input_codec, NULL);
127         if (ret < 0) {
128                 fprintf(stderr, "cannot open input codec\n");
129                 ret = -EINVAL;
130                 goto cleanup;
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
140 cleanup:
141         avformat_close_input(&input_format_ctx);
142 out:
143         av_dict_free(input_options);
144         *input_options = NULL;
145         return ret;
146 }
147
148
149 struct video_output_ctx {
150         AVCodecContext  *codec_ctx;
151         int raw_output;
152 };
153
154 static int video_output_init(struct video_output_ctx *output_ctx,
155                              struct video_input_ctx *input_ctx,
156                              unsigned int upscale,
157                              unsigned int quality,
158                              am7xxx_image_format image_format,
159                              am7xxx_device *dev)
160 {
161         AVCodecContext *output_codec_ctx;
162         AVCodec *output_codec;
163         unsigned int new_output_width;
164         unsigned int new_output_height;
165         int ret;
166
167         if (input_ctx == NULL) {
168                 fprintf(stderr, "input_ctx must not be NULL!\n");
169                 ret = -EINVAL;
170                 goto out;
171         }
172
173         /* create the encoder context */
174         output_codec_ctx = avcodec_alloc_context3(NULL);
175         if (output_codec_ctx == NULL) {
176                 fprintf(stderr, "cannot allocate output codec context!\n");
177                 ret = -ENOMEM;
178                 goto out;
179         }
180
181         /* Calculate the new output dimension so the original picture is shown
182          * in its entirety */
183         ret = am7xxx_calc_scaled_image_dimensions(dev,
184                                                   upscale,
185                                                   (input_ctx->codec_ctx)->width,
186                                                   (input_ctx->codec_ctx)->height,
187                                                   &new_output_width,
188                                                   &new_output_height);
189         if (ret < 0) {
190                 fprintf(stderr, "cannot calculate output dimension\n");
191                 goto cleanup;
192         }
193
194         /* put sample parameters */
195         output_codec_ctx->bit_rate   = (input_ctx->codec_ctx)->bit_rate;
196         output_codec_ctx->width      = new_output_width;
197         output_codec_ctx->height     = new_output_height;
198         output_codec_ctx->time_base.num  =
199                 (input_ctx->format_ctx)->streams[input_ctx->video_stream_index]->time_base.num;
200         output_codec_ctx->time_base.den  =
201                 (input_ctx->format_ctx)->streams[input_ctx->video_stream_index]->time_base.den;
202
203         /* When the raw format is requested we don't actually need to setup
204          * and open a decoder
205          */
206         if (image_format == AM7XXX_IMAGE_FORMAT_NV12) {
207                 fprintf(stdout, "using raw output format\n");
208                 output_codec_ctx->pix_fmt    = AV_PIX_FMT_NV12;
209                 output_ctx->codec_ctx = output_codec_ctx;
210                 output_ctx->raw_output = 1;
211                 ret = 0;
212                 goto out;
213         }
214
215         output_codec_ctx->pix_fmt    = AV_PIX_FMT_YUVJ420P;
216         output_codec_ctx->codec_id   = AV_CODEC_ID_MJPEG;
217         output_codec_ctx->codec_type = AVMEDIA_TYPE_VIDEO;
218
219         /* Set quality and other VBR settings */
220
221         /* @note: 'quality' is expected to be between 1 and 100, but a value
222          * between 0 to 99 has to be passed when calculating qmin and qmax.
223          * This way qmin and qmax will cover the range 1-FF_QUALITY_SCALE, and
224          * in particular they won't be 0, this is needed because they are used
225          * as divisor somewhere in the encoding process */
226         output_codec_ctx->qmin       = output_codec_ctx->qmax = ((100 - (quality - 1)) * FF_QUALITY_SCALE) / 100;
227         output_codec_ctx->mb_lmin    = output_codec_ctx->qmin * FF_QP2LAMBDA;
228         output_codec_ctx->mb_lmax    = output_codec_ctx->qmax * FF_QP2LAMBDA;
229         output_codec_ctx->flags      |= AV_CODEC_FLAG_QSCALE;
230         output_codec_ctx->global_quality = output_codec_ctx->qmin * FF_QP2LAMBDA;
231
232         /* find the encoder */
233         output_codec = avcodec_find_encoder(output_codec_ctx->codec_id);
234         if (output_codec == NULL) {
235                 fprintf(stderr, "cannot find output codec!\n");
236                 ret = -EINVAL;
237                 goto cleanup;
238         }
239
240         /* open the codec */
241         ret = avcodec_open2(output_codec_ctx, output_codec, NULL);
242         if (ret < 0) {
243                 fprintf(stderr, "could not open output codec!\n");
244                 goto cleanup;
245         }
246
247         output_ctx->codec_ctx = output_codec_ctx;
248         output_ctx->raw_output = 0;
249
250         ret = 0;
251         goto out;
252
253 cleanup:
254         avcodec_close(output_codec_ctx);
255         av_free(output_codec_ctx);
256 out:
257         return ret;
258 }
259
260
261 static int am7xxx_play(const char *input_format_string,
262                        AVDictionary **input_options,
263                        const char *input_path,
264                        unsigned int rescale_method,
265                        unsigned int upscale,
266                        unsigned int quality,
267                        am7xxx_image_format image_format,
268                        am7xxx_device *dev,
269                        int dump_frame)
270 {
271         struct video_input_ctx input_ctx;
272         struct video_output_ctx output_ctx;
273         AVFrame *picture_raw;
274         AVFrame *picture_scaled;
275         int out_buf_size;
276         uint8_t *out_buf;
277         int out_picture_size;
278         uint8_t *out_picture;
279         struct SwsContext *sw_scale_ctx;
280         AVPacket in_packet;
281         AVPacket out_packet;
282         int got_picture;
283         int got_packet;
284         int ret;
285
286         ret = video_input_init(&input_ctx, input_format_string, input_path, input_options);
287         if (ret < 0) {
288                 fprintf(stderr, "cannot initialize input\n");
289                 goto out;
290         }
291
292         ret = video_output_init(&output_ctx, &input_ctx, upscale, quality, image_format, dev);
293         if (ret < 0) {
294                 fprintf(stderr, "cannot initialize input\n");
295                 goto cleanup_input;
296         }
297
298         /* allocate an input frame */
299         picture_raw = av_frame_alloc();
300         if (picture_raw == NULL) {
301                 fprintf(stderr, "cannot allocate the raw picture frame!\n");
302                 ret = -ENOMEM;
303                 goto cleanup_output;
304         }
305
306         /* allocate output frame */
307         picture_scaled = av_frame_alloc();
308         if (picture_scaled == NULL) {
309                 fprintf(stderr, "cannot allocate the scaled picture!\n");
310                 ret = -ENOMEM;
311                 goto cleanup_picture_raw;
312         }
313         picture_scaled->format = (output_ctx.codec_ctx)->pix_fmt;
314         picture_scaled->width = (output_ctx.codec_ctx)->width;
315         picture_scaled->height = (output_ctx.codec_ctx)->height;
316
317         /* calculate the bytes needed for the output image and create buffer for the output image */
318         out_buf_size = av_image_get_buffer_size((output_ctx.codec_ctx)->pix_fmt,
319                                                 (output_ctx.codec_ctx)->width,
320                                                 (output_ctx.codec_ctx)->height,
321                                                 1);
322         out_buf = av_malloc(out_buf_size * sizeof(uint8_t));
323         if (out_buf == NULL) {
324                 fprintf(stderr, "cannot allocate output data buffer!\n");
325                 ret = -ENOMEM;
326                 goto cleanup_picture_scaled;
327         }
328
329         /* assign appropriate parts of buffer to image planes in picture_scaled */
330         av_image_fill_arrays(picture_scaled->data,
331                              picture_scaled->linesize,
332                              out_buf,
333                              (output_ctx.codec_ctx)->pix_fmt,
334                              (output_ctx.codec_ctx)->width,
335                              (output_ctx.codec_ctx)->height,
336                              1);
337
338         sw_scale_ctx = sws_getCachedContext(NULL,
339                                             (input_ctx.codec_ctx)->width,
340                                             (input_ctx.codec_ctx)->height,
341                                             (input_ctx.codec_ctx)->pix_fmt,
342                                             (output_ctx.codec_ctx)->width,
343                                             (output_ctx.codec_ctx)->height,
344                                             (output_ctx.codec_ctx)->pix_fmt,
345                                             rescale_method,
346                                             NULL, NULL, NULL);
347         if (sw_scale_ctx == NULL) {
348                 fprintf(stderr, "cannot set up the rescaling context!\n");
349                 ret = -EINVAL;
350                 goto cleanup_out_buf;
351         }
352
353         got_packet = 0;
354         while (run) {
355                 /* read packet */
356                 ret = av_read_frame(input_ctx.format_ctx, &in_packet);
357                 if (ret < 0) {
358                         if (ret == (int)AVERROR_EOF || input_ctx.format_ctx->pb->eof_reached)
359                                 ret = 0;
360                         else
361                                 fprintf(stderr, "av_read_frame failed, EOF?\n");
362                         run = 0;
363                         goto end_while;
364                 }
365
366                 if (in_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 */
369                         goto end_while;
370                 }
371
372                 /* decode */
373                 got_picture = 0;
374                 ret = avcodec_decode_video2(input_ctx.codec_ctx, picture_raw, &got_picture, &in_packet);
375                 if (ret < 0) {
376                         fprintf(stderr, "cannot decode video\n");
377                         run = 0;
378                         goto end_while;
379                 }
380
381                 /* if we got the complete frame */
382                 if (got_picture) {
383                         /* 
384                          * Rescaling the picture also changes its pixel format
385                          * to the raw format supported by the projector if
386                          * this was set in video_output_init()
387                          */
388                         sws_scale(sw_scale_ctx,
389                                   (const uint8_t * const *)picture_raw->data,
390                                   picture_raw->linesize,
391                                   0,
392                                   (input_ctx.codec_ctx)->height,
393                                   picture_scaled->data,
394                                   picture_scaled->linesize);
395
396                         if (output_ctx.raw_output) {
397                                 out_picture = out_buf;
398                                 out_picture_size = out_buf_size;
399                         } else {
400                                 picture_scaled->quality = (output_ctx.codec_ctx)->global_quality;
401                                 av_init_packet(&out_packet);
402                                 out_packet.data = NULL;
403                                 out_packet.size = 0;
404                                 got_packet = 0;
405                                 ret = avcodec_encode_video2(output_ctx.codec_ctx,
406                                                             &out_packet,
407                                                             picture_scaled,
408                                                             &got_packet);
409                                 if (ret < 0 || !got_packet) {
410                                         fprintf(stderr, "cannot encode video\n");
411                                         run = 0;
412                                         goto end_while;
413                                 }
414
415                                 out_picture = out_packet.data;
416                                 out_picture_size = out_packet.size;
417                         }
418
419 #ifdef DEBUG
420                         if (dump_frame) {
421                                 char filename[NAME_MAX];
422                                 FILE *file;
423                                 if (!output_ctx.raw_output)
424                                         snprintf(filename, NAME_MAX, "out_q%03d.jpg", quality);
425                                 else
426                                         snprintf(filename, NAME_MAX, "out.raw");
427                                 file = fopen(filename, "wb");
428                                 fwrite(out_picture, 1, out_picture_size, file);
429                                 fclose(file);
430                         }
431 #else
432                         (void) dump_frame;
433 #endif
434
435                         ret = am7xxx_send_image_async(dev,
436                                                       image_format,
437                                                       (output_ctx.codec_ctx)->width,
438                                                       (output_ctx.codec_ctx)->height,
439                                                       out_picture,
440                                                       out_picture_size);
441                         if (ret < 0) {
442                                 perror("am7xxx_send_image_async");
443                                 run = 0;
444                                 goto end_while;
445                         }
446                 }
447 end_while:
448                 if (!output_ctx.raw_output && got_packet)
449                         av_packet_unref(&out_packet);
450                 av_packet_unref(&in_packet);
451         }
452
453         sws_freeContext(sw_scale_ctx);
454 cleanup_out_buf:
455         av_free(out_buf);
456 cleanup_picture_scaled:
457         av_frame_free(&picture_scaled);
458 cleanup_picture_raw:
459         av_frame_free(&picture_raw);
460
461 cleanup_output:
462         /* av_free is needed as well,
463          * see http://libav.org/doxygen/master/avcodec_8h.html#a5d7440cd7ea195bd0b14f21a00ef36dd
464          */
465         avcodec_close(output_ctx.codec_ctx);
466         av_free(output_ctx.codec_ctx);
467
468 cleanup_input:
469         avcodec_close(input_ctx.codec_ctx);
470         avformat_close_input(&(input_ctx.format_ctx));
471
472 out:
473         return ret;
474 }
475
476 #ifdef HAVE_XCB
477 #include <xcb/xcb.h>
478 static int x_get_screen_dimensions(const char *displayname, int *width, int *height)
479 {
480         int i, screen_number;
481         xcb_connection_t *connection;
482         const xcb_setup_t *setup;
483         xcb_screen_iterator_t iter;
484
485         connection = xcb_connect(displayname, &screen_number);
486         if (xcb_connection_has_error(connection)) {
487                 fprintf(stderr, "Cannot open a connection to %s\n", displayname);
488                 return -EINVAL;
489         }
490
491         setup = xcb_get_setup(connection);
492         if (setup == NULL) {
493                 fprintf(stderr, "Cannot get setup for %s\n", displayname);
494                 xcb_disconnect(connection);
495                 return -EINVAL;
496         }
497
498         iter = xcb_setup_roots_iterator(setup);
499         for (i = 0; i < screen_number; ++i) {
500                 xcb_screen_next(&iter);
501         }
502
503         xcb_screen_t *screen = iter.data;
504
505         *width = screen->width_in_pixels;
506         *height = screen->height_in_pixels;
507
508         xcb_disconnect(connection);
509
510         return 0;
511 }
512
513 static char *get_x_screen_size(const char *input_path)
514 {
515         int len;
516         int width;
517         int height;
518         char *screen_size;
519         int ret;
520
521         ret = x_get_screen_dimensions(input_path, &width, &height);
522         if (ret < 0) {
523                 fprintf(stderr, "Cannot get screen dimensions for %s\n", input_path);
524                 return NULL;
525         }
526
527         len = snprintf(NULL, 0, "%dx%d", width, height);
528
529         screen_size = malloc((len + 1) * sizeof(char));
530         if (screen_size == NULL) {
531                 perror("malloc");
532                 return NULL;
533         }
534
535         len = snprintf(screen_size, len + 1, "%dx%d", width, height);
536         if (len < 0) {
537                 free(screen_size);
538                 screen_size = NULL;
539                 return NULL;
540         }
541         return screen_size;
542 }
543 #else
544 static char *get_x_screen_size(const char *input_path)
545 {
546         (void) input_path;
547         fprintf(stderr, "%s: fallback implementation, assuming a vga screen\n", __func__);
548         return strdup("vga");
549 }
550 #endif
551
552 static void unset_run(int signo)
553 {
554         (void) signo;
555         run = 0;
556 }
557
558 #ifdef HAVE_SIGACTION
559 static int set_signal_handler(void (*signal_handler)(int))
560 {
561         struct sigaction new_action;
562         struct sigaction old_action;
563         int ret;
564
565         new_action.sa_handler = signal_handler;
566         sigemptyset(&new_action.sa_mask);
567         new_action.sa_flags = 0;
568
569         ret = sigaction(SIGINT, NULL, &old_action);
570         if (ret < 0) {
571                 perror("sigaction on old_action");
572                 goto out;
573         }
574
575         if (old_action.sa_handler != SIG_IGN) {
576                 ret = sigaction(SIGINT, &new_action, NULL);
577                 if (ret < 0) {
578                         perror("sigaction on new_action");
579                         goto out;
580                 }
581         }
582
583 out:
584         return ret;
585 }
586 #else
587 static int set_signal_handler(void (*signal_handler)(int))
588 {
589         (void)signal_handler;
590         fprintf(stderr, "set_signal_handler() not implemented, sigaction not available\n");
591         return 0;
592 }
593 #endif
594
595
596 static void usage(char *name)
597 {
598         printf("usage: %s [OPTIONS]\n\n", name);
599         printf("OPTIONS:\n");
600         printf("\t-d <index>\t\tthe device index (default is 0)\n");
601 #ifdef DEBUG
602         printf("\t-D \t\t\tdump the last frame to a file (only active in DEBUG mode)\n");
603 #endif
604         printf("\t-f <input format>\tthe input device format\n");
605         printf("\t-i <input path>\t\tthe input path\n");
606         printf("\t-o <options>\t\ta comma separated list of input format options\n");
607         printf("\t\t\t\tEXAMPLE:\n");
608         printf("\t\t\t\t\t-o draw_mouse=1,framerate=100,video_size=800x480\n");
609         printf("\t-s <scaling method>\tthe rescaling method (see swscale.h)\n");
610         printf("\t-u \t\t\tupscale the image if smaller than the display dimensions\n");
611         printf("\t-F <format>\t\tthe image format to use (default is JPEG)\n");
612         printf("\t\t\t\tSUPPORTED FORMATS:\n");
613         printf("\t\t\t\t\t1 - JPEG\n");
614         printf("\t\t\t\t\t2 - NV12\n");
615         printf("\t-q <quality>\t\tquality of jpeg sent to the device, between 1 and 100\n");
616         printf("\t-l <log level>\t\tthe verbosity level of libam7xxx output (0-5)\n");
617         printf("\t-p <power mode>\t\tthe power mode of device, between %d (off) and %d (turbo)\n",
618                AM7XXX_POWER_OFF, AM7XXX_POWER_TURBO);
619         printf("\t\t\t\tWARNING: Level 2 and greater require the master AND\n");
620         printf("\t\t\t\t         the slave connector to be plugged in.\n");
621         printf("\t-z <zoom mode>\t\tthe display zoom mode, between %d (original) and %d (tele)\n",
622                AM7XXX_ZOOM_ORIGINAL, AM7XXX_ZOOM_TELE);
623         printf("\t-h \t\t\tthis help message\n");
624         printf("\n\nEXAMPLES OF USE:\n");
625         printf("\t%s -f x11grab -i :0.0 -o video_size=800x480\n", name);
626         printf("\t%s -f fbdev -i /dev/fb0\n", name);
627         printf("\t%s -f video4linux2 -i /dev/video0 -o video_size=320x240,frame_rate=100 -u -q 90\n", name);
628         printf("\t%s -i http://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_640x360.m4v\n", name);
629 }
630
631 int main(int argc, char *argv[])
632 {
633         int ret;
634         int opt;
635         char *subopts;
636         char *subopts_saved;
637         char *subopt;
638         char *input_format_string = NULL;
639         AVDictionary *options = NULL;
640         char *input_path = NULL;
641         unsigned int rescale_method = SWS_BICUBIC;
642         unsigned int upscale = 0;
643         unsigned int quality = 95;
644         int log_level = AM7XXX_LOG_INFO;
645         int device_index = 0;
646         int power_mode = AM7XXX_POWER_LOW;
647         int zoom = AM7XXX_ZOOM_ORIGINAL;
648         int format = AM7XXX_IMAGE_FORMAT_JPEG;
649         am7xxx_context *ctx;
650         am7xxx_device *dev;
651         int dump_frame = 0;
652
653         while ((opt = getopt(argc, argv, "d:Df:i:o:s:uF:q:l:p:z:h")) != -1) {
654                 switch (opt) {
655                 case 'd':
656                         device_index = atoi(optarg);
657                         if (device_index < 0) {
658                                 fprintf(stderr, "Unsupported device index\n");
659                                 ret = -EINVAL;
660                                 goto out;
661                         }
662                         break;
663                 case 'D':
664                         dump_frame = 1;
665 #ifndef DEBUG
666                         fprintf(stderr, "Warning: the -D option is only active in DEBUG mode.\n");
667 #endif
668                         break;
669                 case 'f':
670                         input_format_string = strdup(optarg);
671                         break;
672                 case 'i':
673                         input_path = strdup(optarg);
674                         break;
675                 case 'o':
676 #ifdef HAVE_STRTOK_R
677                         /*
678                          * parse suboptions, the expected format is something
679                          * like:
680                          *   draw_mouse=1,framerate=100,video_size=800x480
681                          */
682                         subopts = subopts_saved = strdup(optarg);
683                         while ((subopt = strtok_r(subopts, ",", &subopts))) {
684                                 char *subopt_name = strtok_r(subopt, "=", &subopt);
685                                 char *subopt_value = strtok_r(NULL, "", &subopt);
686                                 if (subopt_value == NULL) {
687                                         fprintf(stderr, "invalid suboption: %s\n", subopt_name);
688                                         continue;
689                                 }
690                                 av_dict_set(&options, subopt_name, subopt_value, 0);
691                         }
692                         free(subopts_saved);
693 #else
694                         fprintf(stderr, "Option '-o' not implemented\n");
695 #endif
696                         break;
697                 case 's':
698                         rescale_method = atoi(optarg);
699                         switch(rescale_method) {
700                         case SWS_FAST_BILINEAR:
701                         case SWS_BILINEAR:
702                         case SWS_BICUBIC:
703                         case SWS_X:
704                         case SWS_POINT:
705                         case SWS_AREA:
706                         case SWS_BICUBLIN:
707                         case SWS_GAUSS:
708                         case SWS_SINC:
709                         case SWS_LANCZOS:
710                         case SWS_SPLINE:
711                                 break;
712                         default:
713                                 fprintf(stderr, "Unsupported rescale method\n");
714                                 ret = -EINVAL;
715                                 goto out;
716                         }
717                         break;
718                 case 'u':
719                         upscale = 1;
720                         break;
721                 case 'F':
722                         format = atoi(optarg);
723                         switch(format) {
724                         case AM7XXX_IMAGE_FORMAT_JPEG:
725                                 fprintf(stdout, "JPEG format\n");
726                                 break;
727                         case AM7XXX_IMAGE_FORMAT_NV12:
728                                 fprintf(stdout, "NV12 format\n");
729                                 break;
730                         default:
731                                 fprintf(stderr, "Unsupported format\n");
732                                 ret = -EINVAL;
733                                 goto out;
734                         }
735                         break;
736                 case 'q':
737                         quality = atoi(optarg);
738                         if (quality < 1 || quality > 100) {
739                                 fprintf(stderr, "Invalid quality value, must be between 1 and 100\n");
740                                 ret = -EINVAL;
741                                 goto out;
742                         }
743                         break;
744                 case 'l':
745                         log_level = atoi(optarg);
746                         if (log_level < AM7XXX_LOG_FATAL || log_level > AM7XXX_LOG_TRACE) {
747                                 fprintf(stderr, "Unsupported log level, falling back to AM7XXX_LOG_ERROR\n");
748                                 log_level = AM7XXX_LOG_ERROR;
749                         }
750                         break;
751                 case 'p':
752                         power_mode = atoi(optarg);
753                         switch(power_mode) {
754                         case AM7XXX_POWER_OFF:
755                         case AM7XXX_POWER_LOW:
756                         case AM7XXX_POWER_MIDDLE:
757                         case AM7XXX_POWER_HIGH:
758                         case AM7XXX_POWER_TURBO:
759                                 fprintf(stdout, "Power mode: %d\n", power_mode);
760                                 break;
761                         default:
762                                 fprintf(stderr, "Invalid power mode value, must be between %d and %d\n",
763                                         AM7XXX_POWER_OFF, AM7XXX_POWER_TURBO);
764                                 ret = -EINVAL;
765                                 goto out;
766                         }
767                         break;
768                 case 'z':
769                         zoom = atoi(optarg);
770                         switch(zoom) {
771                         case AM7XXX_ZOOM_ORIGINAL:
772                         case AM7XXX_ZOOM_H:
773                         case AM7XXX_ZOOM_H_V:
774                         case AM7XXX_ZOOM_TEST:
775                         case AM7XXX_ZOOM_TELE:
776                                 fprintf(stdout, "Zoom: %d\n", zoom);
777                                 break;
778                         default:
779                                 fprintf(stderr, "Invalid zoom mode value, must be between %d and %d\n",
780                                         AM7XXX_ZOOM_ORIGINAL, AM7XXX_ZOOM_TELE);
781                                 ret = -EINVAL;
782                                 goto out;
783                         }
784                         break;
785                 case 'h':
786                         usage(argv[0]);
787                         ret = 0;
788                         goto out;
789                 default: /* '?' */
790                         usage(argv[0]);
791                         ret = -EINVAL;
792                         goto out;
793                 }
794         }
795
796         if (input_path == NULL) {
797                 fprintf(stderr, "The -i option must always be passed\n\n");
798                 usage(argv[0]);
799                 ret = -EINVAL;
800                 goto out;
801         }
802
803         /*
804          * When the input format is 'x11grab' set some useful fallback options
805          * if not supplied by the user, in particular grab full screen
806          */
807         if (input_format_string && strcmp(input_format_string, "x11grab") == 0) {
808                 char *video_size;
809
810                 video_size = get_x_screen_size(input_path);
811
812                 if (!av_dict_get(options, "video_size", NULL, 0))
813                         av_dict_set(&options, "video_size", video_size, 0);
814
815                 if (!av_dict_get(options, "framerate", NULL, 0))
816                         av_dict_set(&options, "framerate", "60", 0);
817
818                 if (!av_dict_get(options, "draw_mouse", NULL, 0))
819                         av_dict_set(&options, "draw_mouse",  "1", 0);
820
821                 free(video_size);
822         }
823
824         ret = set_signal_handler(unset_run);
825         if (ret < 0) {
826                 perror("sigaction");
827                 goto out;
828         }
829
830         ret = am7xxx_init(&ctx);
831         if (ret < 0) {
832                 perror("am7xxx_init");
833                 goto out;
834         }
835
836         am7xxx_set_log_level(ctx, log_level);
837
838         ret = am7xxx_open_device(ctx, &dev, device_index);
839         if (ret < 0) {
840                 perror("am7xxx_open_device");
841                 goto cleanup;
842         }
843
844         ret = am7xxx_set_zoom_mode(dev, zoom);
845         if (ret < 0) {
846                 perror("am7xxx_set_zoom_mode");
847                 goto cleanup;
848         }
849
850         ret = am7xxx_set_power_mode(dev, power_mode);
851         if (ret < 0) {
852                 perror("am7xxx_set_power_mode");
853                 goto cleanup;
854         }
855
856         /* When setting AM7XXX_ZOOM_TEST don't display the actual image */
857         if (zoom == AM7XXX_ZOOM_TEST)
858                 goto cleanup;
859
860         ret = am7xxx_play(input_format_string,
861                           &options,
862                           input_path,
863                           rescale_method,
864                           upscale,
865                           quality,
866                           format,
867                           dev,
868                           dump_frame);
869         if (ret < 0) {
870                 fprintf(stderr, "am7xxx_play failed\n");
871                 goto cleanup;
872         }
873
874 cleanup:
875         am7xxx_shutdown(ctx);
876 out:
877         av_dict_free(&options);
878         free(input_path);
879         free(input_format_string);
880         return ret;
881 }