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