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