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