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