Release version 0.1.6
[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->qmin * FF_QP2LAMBDA;
239         output_codec_ctx->mb_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                        int dump_frame)
281 {
282         struct video_input_ctx input_ctx;
283         struct video_output_ctx output_ctx;
284         AVFrame *picture_raw;
285         AVFrame *picture_scaled;
286         int out_buf_size;
287         uint8_t *out_buf;
288         int out_picture_size;
289         uint8_t *out_picture;
290         struct SwsContext *sw_scale_ctx;
291         AVPacket in_packet;
292         AVPacket out_packet;
293         int got_picture;
294         int got_packet;
295         int ret;
296
297         ret = video_input_init(&input_ctx, input_format_string, input_path, input_options);
298         if (ret < 0) {
299                 fprintf(stderr, "cannot initialize input\n");
300                 goto out;
301         }
302
303         ret = video_output_init(&output_ctx, &input_ctx, upscale, quality, image_format, dev);
304         if (ret < 0) {
305                 fprintf(stderr, "cannot initialize input\n");
306                 goto cleanup_input;
307         }
308
309         /* allocate an input frame */
310         picture_raw = av_frame_alloc();
311         if (picture_raw == NULL) {
312                 fprintf(stderr, "cannot allocate the raw picture frame!\n");
313                 ret = -ENOMEM;
314                 goto cleanup_output;
315         }
316
317         /* allocate output frame */
318         picture_scaled = av_frame_alloc();
319         if (picture_scaled == NULL) {
320                 fprintf(stderr, "cannot allocate the scaled picture!\n");
321                 ret = -ENOMEM;
322                 goto cleanup_picture_raw;
323         }
324         picture_scaled->format = (output_ctx.codec_ctx)->pix_fmt;
325         picture_scaled->width = (output_ctx.codec_ctx)->width;
326         picture_scaled->height = (output_ctx.codec_ctx)->height;
327
328         /* calculate the bytes needed for the output image and create buffer for the output image */
329         out_buf_size = avpicture_get_size((output_ctx.codec_ctx)->pix_fmt,
330                                           (output_ctx.codec_ctx)->width,
331                                           (output_ctx.codec_ctx)->height);
332         out_buf = av_malloc(out_buf_size * sizeof(uint8_t));
333         if (out_buf == NULL) {
334                 fprintf(stderr, "cannot allocate output data buffer!\n");
335                 ret = -ENOMEM;
336                 goto cleanup_picture_scaled;
337         }
338
339         /* assign appropriate parts of buffer to image planes in picture_scaled */
340         avpicture_fill((AVPicture *)picture_scaled,
341                        out_buf,
342                        (output_ctx.codec_ctx)->pix_fmt,
343                        (output_ctx.codec_ctx)->width,
344                        (output_ctx.codec_ctx)->height);
345
346         sw_scale_ctx = sws_getCachedContext(NULL,
347                                             (input_ctx.codec_ctx)->width,
348                                             (input_ctx.codec_ctx)->height,
349                                             (input_ctx.codec_ctx)->pix_fmt,
350                                             (output_ctx.codec_ctx)->width,
351                                             (output_ctx.codec_ctx)->height,
352                                             (output_ctx.codec_ctx)->pix_fmt,
353                                             rescale_method,
354                                             NULL, NULL, NULL);
355         if (sw_scale_ctx == NULL) {
356                 fprintf(stderr, "cannot set up the rescaling context!\n");
357                 ret = -EINVAL;
358                 goto cleanup_out_buf;
359         }
360
361         got_packet = 0;
362         while (run) {
363                 /* read packet */
364                 ret = av_read_frame(input_ctx.format_ctx, &in_packet);
365                 if (ret < 0) {
366                         if (ret == (int)AVERROR_EOF || input_ctx.format_ctx->pb->eof_reached)
367                                 ret = 0;
368                         else
369                                 fprintf(stderr, "av_read_frame failed, EOF?\n");
370                         run = 0;
371                         goto end_while;
372                 }
373
374                 if (in_packet.stream_index != input_ctx.video_stream_index) {
375                         /* that is more or less a "continue", but there is
376                          * still the packet to free */
377                         goto end_while;
378                 }
379
380                 /* decode */
381                 got_picture = 0;
382                 ret = avcodec_decode_video2(input_ctx.codec_ctx, picture_raw, &got_picture, &in_packet);
383                 if (ret < 0) {
384                         fprintf(stderr, "cannot decode video\n");
385                         run = 0;
386                         goto end_while;
387                 }
388
389                 /* if we got the complete frame */
390                 if (got_picture) {
391                         /* convert it to YUV */
392                         sws_scale(sw_scale_ctx,
393                                   (const uint8_t * const *)picture_raw->data,
394                                   picture_raw->linesize,
395                                   0,
396                                   (input_ctx.codec_ctx)->height,
397                                   picture_scaled->data,
398                                   picture_scaled->linesize);
399
400                         if (output_ctx.raw_output) {
401                                 out_picture = out_buf;
402                                 out_picture_size = out_buf_size;
403                         } else {
404                                 picture_scaled->quality = (output_ctx.codec_ctx)->global_quality;
405                                 av_init_packet(&out_packet);
406                                 out_packet.data = NULL;
407                                 out_packet.size = 0;
408                                 got_packet = 0;
409                                 ret = avcodec_encode_video2(output_ctx.codec_ctx,
410                                                             &out_packet,
411                                                             picture_scaled,
412                                                             &got_packet);
413                                 if (ret < 0 || !got_packet) {
414                                         fprintf(stderr, "cannot encode video\n");
415                                         run = 0;
416                                         goto end_while;
417                                 }
418
419                                 out_picture = out_packet.data;
420                                 out_picture_size = out_packet.size;
421                         }
422
423 #ifdef DEBUG
424                         if (dump_frame) {
425                                 char filename[NAME_MAX];
426                                 FILE *file;
427                                 if (!output_ctx.raw_output)
428                                         snprintf(filename, NAME_MAX, "out_q%03d.jpg", quality);
429                                 else
430                                         snprintf(filename, NAME_MAX, "out.raw");
431                                 file = fopen(filename, "wb");
432                                 fwrite(out_picture, 1, out_picture_size, file);
433                                 fclose(file);
434                         }
435 #else
436                         (void) dump_frame;
437 #endif
438
439                         ret = am7xxx_send_image_async(dev,
440                                                       image_format,
441                                                       (output_ctx.codec_ctx)->width,
442                                                       (output_ctx.codec_ctx)->height,
443                                                       out_picture,
444                                                       out_picture_size);
445                         if (ret < 0) {
446                                 perror("am7xxx_send_image_async");
447                                 run = 0;
448                                 goto end_while;
449                         }
450                 }
451 end_while:
452                 if (!output_ctx.raw_output && got_packet)
453                         av_free_packet(&out_packet);
454                 av_free_packet(&in_packet);
455         }
456
457         sws_freeContext(sw_scale_ctx);
458 cleanup_out_buf:
459         av_free(out_buf);
460 cleanup_picture_scaled:
461         av_frame_free(&picture_scaled);
462 cleanup_picture_raw:
463         av_frame_free(&picture_raw);
464
465 cleanup_output:
466         /* av_free is needed as well,
467          * see http://libav.org/doxygen/master/avcodec_8h.html#a5d7440cd7ea195bd0b14f21a00ef36dd
468          */
469         avcodec_close(output_ctx.codec_ctx);
470         av_free(output_ctx.codec_ctx);
471
472 cleanup_input:
473         avcodec_close(input_ctx.codec_ctx);
474         avformat_close_input(&(input_ctx.format_ctx));
475
476 out:
477         return ret;
478 }
479
480 #ifdef HAVE_XCB
481 #include <xcb/xcb.h>
482 static int x_get_screen_dimensions(const char *displayname, int *width, int *height)
483 {
484         int i, screen_number;
485         xcb_connection_t *connection;
486         const xcb_setup_t *setup;
487         xcb_screen_iterator_t iter;
488
489         connection = xcb_connect(displayname, &screen_number);
490         if (xcb_connection_has_error(connection)) {
491                 fprintf(stderr, "Cannot open a connection to %s\n", displayname);
492                 return -EINVAL;
493         }
494
495         setup = xcb_get_setup(connection);
496         if (setup == NULL) {
497                 fprintf(stderr, "Cannot get setup for %s\n", displayname);
498                 xcb_disconnect(connection);
499                 return -EINVAL;
500         }
501
502         iter = xcb_setup_roots_iterator(setup);
503         for (i = 0; i < screen_number; ++i) {
504                 xcb_screen_next(&iter);
505         }
506
507         xcb_screen_t *screen = iter.data;
508
509         *width = screen->width_in_pixels;
510         *height = screen->height_in_pixels;
511
512         xcb_disconnect(connection);
513
514         return 0;
515 }
516
517 static char *get_x_screen_size(const char *input_path)
518 {
519         int len;
520         int width;
521         int height;
522         char *screen_size;
523         int ret;
524
525         ret = x_get_screen_dimensions(input_path, &width, &height);
526         if (ret < 0) {
527                 fprintf(stderr, "Cannot get screen dimensions for %s\n", input_path);
528                 return NULL;
529         }
530
531         len = snprintf(NULL, 0, "%dx%d", width, height);
532
533         screen_size = malloc((len + 1) * sizeof(char));
534         if (screen_size == NULL) {
535                 perror("malloc");
536                 return NULL;
537         }
538
539         len = snprintf(screen_size, len + 1, "%dx%d", width, height);
540         if (len < 0) {
541                 free(screen_size);
542                 screen_size = NULL;
543                 return NULL;
544         }
545         return screen_size;
546 }
547 #else
548 static char *get_x_screen_size(const char *input_path)
549 {
550         (void) input_path;
551         fprintf(stderr, "%s: fallback implementation\n", __func__);
552         return strdup("vga");
553 }
554 #endif
555
556 static void unset_run(int signo)
557 {
558         (void) signo;
559         run = 0;
560 }
561
562 #ifdef HAVE_SIGACTION
563 static int set_signal_handler(void (*signal_handler)(int))
564 {
565         struct sigaction new_action;
566         struct sigaction old_action;
567         int ret;
568
569         new_action.sa_handler = signal_handler;
570         sigemptyset(&new_action.sa_mask);
571         new_action.sa_flags = 0;
572
573         ret = sigaction(SIGINT, NULL, &old_action);
574         if (ret < 0) {
575                 perror("sigaction on old_action");
576                 goto out;
577         }
578
579         if (old_action.sa_handler != SIG_IGN) {
580                 ret = sigaction(SIGINT, &new_action, NULL);
581                 if (ret < 0) {
582                         perror("sigaction on new_action");
583                         goto out;
584                 }
585         }
586
587 out:
588         return ret;
589 }
590 #else
591 static int set_signal_handler(void (*signal_handler)(int))
592 {
593         (void)signal_handler;
594         fprintf(stderr, "set_signal_handler() not implemented, sigaction not available\n");
595         return 0;
596 }
597 #endif
598
599
600 static void usage(char *name)
601 {
602         printf("usage: %s [OPTIONS]\n\n", name);
603         printf("OPTIONS:\n");
604         printf("\t-d <index>\t\tthe device index (default is 0)\n");
605 #ifdef DEBUG
606         printf("\t-D \t\t\tdump the last frame to a file (only active in DEBUG mode)\n");
607 #endif
608         printf("\t-f <input format>\tthe input device format\n");
609         printf("\t-i <input path>\t\tthe input path\n");
610         printf("\t-o <options>\t\ta comma separated list of input format options\n");
611         printf("\t\t\t\tEXAMPLE:\n");
612         printf("\t\t\t\t\t-o draw_mouse=1,framerate=100,video_size=800x480\n");
613         printf("\t-s <scaling method>\tthe rescaling method (see swscale.h)\n");
614         printf("\t-u \t\t\tupscale the image if smaller than the display dimensions\n");
615         printf("\t-F <format>\t\tthe image format to use (default is JPEG)\n");
616         printf("\t\t\t\tSUPPORTED FORMATS:\n");
617         printf("\t\t\t\t\t1 - JPEG\n");
618         printf("\t\t\t\t\t2 - NV12\n");
619         printf("\t-q <quality>\t\tquality of jpeg sent to the device, between 1 and 100\n");
620         printf("\t-l <log level>\t\tthe verbosity level of libam7xxx output (0-5)\n");
621         printf("\t-p <power mode>\t\tthe power mode of device, between %d (off) and %d (turbo)\n",
622                AM7XXX_POWER_OFF, AM7XXX_POWER_TURBO);
623         printf("\t\t\t\tWARNING: Level 2 and greater require the master AND\n");
624         printf("\t\t\t\t         the slave connector to be plugged in.\n");
625         printf("\t-z <zoom mode>\t\tthe display zoom mode, between %d (original) and %d (tele)\n",
626                AM7XXX_ZOOM_ORIGINAL, AM7XXX_ZOOM_TELE);
627         printf("\t-h \t\t\tthis help message\n");
628         printf("\n\nEXAMPLES OF USE:\n");
629         printf("\t%s -f x11grab -i :0.0 -o video_size=800x480\n", name);
630         printf("\t%s -f fbdev -i /dev/fb0\n", name);
631         printf("\t%s -f video4linux2 -i /dev/video0 -o video_size=320x240,frame_rate=100 -u -q 90\n", name);
632         printf("\t%s -i http://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_640x360.m4v\n", name);
633 }
634
635 int main(int argc, char *argv[])
636 {
637         int ret;
638         int opt;
639         char *subopts;
640         char *subopts_saved;
641         char *subopt;
642         char *input_format_string = NULL;
643         AVDictionary *options = NULL;
644         char *input_path = NULL;
645         unsigned int rescale_method = SWS_BICUBIC;
646         unsigned int upscale = 0;
647         unsigned int quality = 95;
648         int log_level = AM7XXX_LOG_INFO;
649         int device_index = 0;
650         int power_mode = AM7XXX_POWER_LOW;
651         int zoom = AM7XXX_ZOOM_ORIGINAL;
652         int format = AM7XXX_IMAGE_FORMAT_JPEG;
653         am7xxx_context *ctx;
654         am7xxx_device *dev;
655         int dump_frame = 0;
656
657         while ((opt = getopt(argc, argv, "d:Df:i:o:s:uF:q:l:p:z:h")) != -1) {
658                 switch (opt) {
659                 case 'd':
660                         device_index = atoi(optarg);
661                         if (device_index < 0) {
662                                 fprintf(stderr, "Unsupported device index\n");
663                                 ret = -EINVAL;
664                                 goto out;
665                         }
666                         break;
667                 case 'D':
668                         dump_frame = 1;
669 #ifndef DEBUG
670                         fprintf(stderr, "Warning: the -D option is only active in DEBUG mode.\n");
671 #endif
672                         break;
673                 case 'f':
674                         input_format_string = strdup(optarg);
675                         break;
676                 case 'i':
677                         input_path = strdup(optarg);
678                         break;
679                 case 'o':
680 #ifdef HAVE_STRTOK_R
681                         /*
682                          * parse suboptions, the expected format is something
683                          * like:
684                          *   draw_mouse=1,framerate=100,video_size=800x480
685                          */
686                         subopts = subopts_saved = strdup(optarg);
687                         while ((subopt = strtok_r(subopts, ",", &subopts))) {
688                                 char *subopt_name = strtok_r(subopt, "=", &subopt);
689                                 char *subopt_value = strtok_r(NULL, "", &subopt);
690                                 if (subopt_value == NULL) {
691                                         fprintf(stderr, "invalid suboption: %s\n", subopt_name);
692                                         continue;
693                                 }
694                                 av_dict_set(&options, subopt_name, subopt_value, 0);
695                         }
696                         free(subopts_saved);
697 #else
698                         fprintf(stderr, "Option '-o' not implemented\n");
699 #endif
700                         break;
701                 case 's':
702                         rescale_method = atoi(optarg);
703                         switch(rescale_method) {
704                         case SWS_FAST_BILINEAR:
705                         case SWS_BILINEAR:
706                         case SWS_BICUBIC:
707                         case SWS_X:
708                         case SWS_POINT:
709                         case SWS_AREA:
710                         case SWS_BICUBLIN:
711                         case SWS_GAUSS:
712                         case SWS_SINC:
713                         case SWS_LANCZOS:
714                         case SWS_SPLINE:
715                                 break;
716                         default:
717                                 fprintf(stderr, "Unsupported rescale method\n");
718                                 ret = -EINVAL;
719                                 goto out;
720                         }
721                         break;
722                 case 'u':
723                         upscale = 1;
724                         break;
725                 case 'F':
726                         format = atoi(optarg);
727                         switch(format) {
728                         case AM7XXX_IMAGE_FORMAT_JPEG:
729                                 fprintf(stdout, "JPEG format\n");
730                                 break;
731                         case AM7XXX_IMAGE_FORMAT_NV12:
732                                 fprintf(stdout, "NV12 format\n");
733                                 break;
734                         default:
735                                 fprintf(stderr, "Unsupported format\n");
736                                 ret = -EINVAL;
737                                 goto out;
738                         }
739                         break;
740                 case 'q':
741                         quality = atoi(optarg);
742                         if (quality < 1 || quality > 100) {
743                                 fprintf(stderr, "Invalid quality value, must be between 1 and 100\n");
744                                 ret = -EINVAL;
745                                 goto out;
746                         }
747                         break;
748                 case 'l':
749                         log_level = atoi(optarg);
750                         if (log_level < AM7XXX_LOG_FATAL || log_level > AM7XXX_LOG_TRACE) {
751                                 fprintf(stderr, "Unsupported log level, falling back to AM7XXX_LOG_ERROR\n");
752                                 log_level = AM7XXX_LOG_ERROR;
753                         }
754                         break;
755                 case 'p':
756                         power_mode = atoi(optarg);
757                         switch(power_mode) {
758                         case AM7XXX_POWER_OFF:
759                         case AM7XXX_POWER_LOW:
760                         case AM7XXX_POWER_MIDDLE:
761                         case AM7XXX_POWER_HIGH:
762                         case AM7XXX_POWER_TURBO:
763                                 fprintf(stdout, "Power mode: %d\n", power_mode);
764                                 break;
765                         default:
766                                 fprintf(stderr, "Invalid power mode value, must be between %d and %d\n",
767                                         AM7XXX_POWER_OFF, AM7XXX_POWER_TURBO);
768                                 ret = -EINVAL;
769                                 goto out;
770                         }
771                         break;
772                 case 'z':
773                         zoom = atoi(optarg);
774                         switch(zoom) {
775                         case AM7XXX_ZOOM_ORIGINAL:
776                         case AM7XXX_ZOOM_H:
777                         case AM7XXX_ZOOM_H_V:
778                         case AM7XXX_ZOOM_TEST:
779                         case AM7XXX_ZOOM_TELE:
780                                 fprintf(stdout, "Zoom: %d\n", zoom);
781                                 break;
782                         default:
783                                 fprintf(stderr, "Invalid zoom mode value, must be between %d and %d\n",
784                                         AM7XXX_ZOOM_ORIGINAL, AM7XXX_ZOOM_TELE);
785                                 ret = -EINVAL;
786                                 goto out;
787                         }
788                         break;
789                 case 'h':
790                         usage(argv[0]);
791                         ret = 0;
792                         goto out;
793                 default: /* '?' */
794                         usage(argv[0]);
795                         ret = -EINVAL;
796                         goto out;
797                 }
798         }
799
800         if (input_path == NULL) {
801                 fprintf(stderr, "The -i option must always be passed\n\n");
802                 usage(argv[0]);
803                 ret = -EINVAL;
804                 goto out;
805         }
806
807         /*
808          * When the input format is 'x11grab' set some useful fallback options
809          * if not supplied by the user, in particular grab full screen
810          */
811         if (input_format_string && strcmp(input_format_string, "x11grab") == 0) {
812                 char *video_size;
813
814                 video_size = get_x_screen_size(input_path);
815
816                 if (!av_dict_get(options, "video_size", NULL, 0))
817                         av_dict_set(&options, "video_size", video_size, 0);
818
819                 if (!av_dict_get(options, "framerate", NULL, 0))
820                         av_dict_set(&options, "framerate", "60", 0);
821
822                 if (!av_dict_get(options, "draw_mouse", NULL, 0))
823                         av_dict_set(&options, "draw_mouse",  "1", 0);
824
825                 free(video_size);
826         }
827
828         ret = set_signal_handler(unset_run);
829         if (ret < 0) {
830                 perror("sigaction");
831                 goto out;
832         }
833
834         ret = am7xxx_init(&ctx);
835         if (ret < 0) {
836                 perror("am7xxx_init");
837                 goto out;
838         }
839
840         am7xxx_set_log_level(ctx, log_level);
841
842         ret = am7xxx_open_device(ctx, &dev, device_index);
843         if (ret < 0) {
844                 perror("am7xxx_open_device");
845                 goto cleanup;
846         }
847
848         ret = am7xxx_set_zoom_mode(dev, zoom);
849         if (ret < 0) {
850                 perror("am7xxx_set_zoom_mode");
851                 goto cleanup;
852         }
853
854         ret = am7xxx_set_power_mode(dev, power_mode);
855         if (ret < 0) {
856                 perror("am7xxx_set_power_mode");
857                 goto cleanup;
858         }
859
860         /* When setting AM7XXX_ZOOM_TEST don't display the actual image */
861         if (zoom == AM7XXX_ZOOM_TEST)
862                 goto cleanup;
863
864         ret = am7xxx_play(input_format_string,
865                           &options,
866                           input_path,
867                           rescale_method,
868                           upscale,
869                           quality,
870                           format,
871                           dev,
872                           dump_frame);
873         if (ret < 0) {
874                 fprintf(stderr, "am7xxx_play failed\n");
875                 goto cleanup;
876         }
877
878 cleanup:
879         am7xxx_shutdown(ctx);
880 out:
881         av_dict_free(&options);
882         free(input_path);
883         free(input_format_string);
884         return ret;
885 }