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