am7xxx-play: get rd of the deprecated function avpicture_fill()
[libam7xxx.git] / examples / picoproj.c
1 /* picoproj - test program for libam7xxx
2  *
3  * Copyright (C) 2012-2014  Antonio Ospite <ao2@ao2.it>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 /**
20  * @example examples/picoproj.c
21  * A minimal example to show how to use libam7xxx to display a static image.
22  */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <errno.h>
32
33 #include "am7xxx.h"
34
35 static void usage(char *name)
36 {
37         printf("usage: %s [OPTIONS]\n\n", name);
38         printf("OPTIONS:\n");
39         printf("\t-d <index>\t\tthe device index (default is 0)\n");
40         printf("\t-f <filename>\t\tthe image file to upload\n");
41         printf("\t-F <format>\t\tthe image format to use (default is JPEG)\n");
42         printf("\t\t\t\tSUPPORTED FORMATS:\n");
43         printf("\t\t\t\t\t1 - JPEG\n");
44         printf("\t\t\t\t\t2 - NV12\n");
45         printf("\t-l <log level>\t\tthe verbosity level of libam7xxx output (0-5)\n");
46         printf("\t-p <power mode>\t\tthe power mode of device, between %d (off) and %d (turbo)\n",
47                AM7XXX_POWER_OFF, AM7XXX_POWER_TURBO);
48         printf("\t\t\t\tWARNING: Level 2 and greater require the master AND\n");
49         printf("\t\t\t\t         the slave connector to be plugged in.\n");
50         printf("\t-z <zoom mode>\t\tthe display zoom mode, between %d (original) and %d (tele)\n",
51                AM7XXX_ZOOM_ORIGINAL, AM7XXX_ZOOM_TELE);
52         printf("\t-W <image width>\tthe width of the image to upload\n");
53         printf("\t-H <image height>\tthe height of the image to upload\n");
54         printf("\t-h \t\t\tthis help message\n");
55         printf("\n\nEXAMPLE OF USE:\n");
56         printf("\t%s -f file.jpg -F 1 -l 5 -W 800 -H 480\n", name);
57 }
58
59 int main(int argc, char *argv[])
60 {
61         int ret;
62         int opt;
63
64         char filename[FILENAME_MAX] = {0};
65         FILE *image_fp;
66         struct stat st;
67         am7xxx_context *ctx;
68         am7xxx_device *dev;
69         int log_level = AM7XXX_LOG_INFO;
70         int device_index = 0;
71         int power_mode = AM7XXX_POWER_LOW;
72         int zoom = AM7XXX_ZOOM_ORIGINAL;
73         int format = AM7XXX_IMAGE_FORMAT_JPEG;
74         int width = 800;
75         int height = 480;
76         unsigned char *image;
77         off_t size;
78         am7xxx_device_info device_info;
79
80         while ((opt = getopt(argc, argv, "d:f:F:l:p:z:W:H:h")) != -1) {
81                 switch (opt) {
82                 case 'd':
83                         device_index = atoi(optarg);
84                         if (device_index < 0) {
85                                 fprintf(stderr, "Unsupported device index\n");
86                                 ret = -EINVAL;
87                                 goto out;
88                         }
89                         break;
90                 case 'f':
91                         if (filename[0] != '\0')
92                                 fprintf(stderr, "Warning: image file already specified\n");
93                         strncpy(filename, optarg, FILENAME_MAX);
94                         break;
95                 case 'F':
96                         format = atoi(optarg);
97                         switch(format) {
98                         case AM7XXX_IMAGE_FORMAT_JPEG:
99                                 fprintf(stdout, "JPEG format\n");
100                                 break;
101                         case AM7XXX_IMAGE_FORMAT_NV12:
102                                 fprintf(stdout, "NV12 format\n");
103                                 break;
104                         default:
105                                 fprintf(stderr, "Unsupported format\n");
106                                 ret = -EINVAL;
107                                 goto out;
108                         }
109                         break;
110                 case 'l':
111                         log_level = atoi(optarg);
112                         if (log_level < AM7XXX_LOG_FATAL || log_level > AM7XXX_LOG_TRACE) {
113                                 fprintf(stderr, "Unsupported log level, falling back to AM7XXX_LOG_ERROR\n");
114                                 log_level = AM7XXX_LOG_ERROR;
115                         }
116                         break;
117                 case 'p':
118                         power_mode = atoi(optarg);
119                         switch(power_mode) {
120                         case AM7XXX_POWER_OFF:
121                         case AM7XXX_POWER_LOW:
122                         case AM7XXX_POWER_MIDDLE:
123                         case AM7XXX_POWER_HIGH:
124                         case AM7XXX_POWER_TURBO:
125                                 fprintf(stdout, "Power mode: %d\n", power_mode);
126                                 break;
127                         default:
128                                 fprintf(stderr, "Invalid power mode value, must be between %d and %d\n",
129                                         AM7XXX_POWER_OFF, AM7XXX_POWER_TURBO);
130                                 ret = -EINVAL;
131                                 goto out;
132                         }
133                         break;
134                 case 'z':
135                         zoom = atoi(optarg);
136                         switch(zoom) {
137                         case AM7XXX_ZOOM_ORIGINAL:
138                         case AM7XXX_ZOOM_H:
139                         case AM7XXX_ZOOM_H_V:
140                         case AM7XXX_ZOOM_TEST:
141                         case AM7XXX_ZOOM_TELE:
142                                 fprintf(stdout, "Zoom: %d\n", zoom);
143                                 break;
144                         default:
145                                 fprintf(stderr, "Invalid zoom mode value, must be between %d and %d\n",
146                                         AM7XXX_ZOOM_ORIGINAL, AM7XXX_ZOOM_TELE);
147                                 ret = -EINVAL;
148                                 goto out;
149                         }
150                         break;
151                 case 'W':
152                         width = atoi(optarg);
153                         if (width < 0) {
154                                 fprintf(stderr, "Unsupported width\n");
155                                 ret = -EINVAL;
156                                 goto out;
157                         }
158                         break;
159                 case 'H':
160                         height = atoi(optarg);
161                         if (height < 0) {
162                                 fprintf(stderr, "Unsupported height\n");
163                                 ret = -EINVAL;
164                                 goto out;
165                         }
166                         break;
167                 case 'h':
168                         usage(argv[0]);
169                         ret = 0;
170                         goto out;
171                 default: /* '?' */
172                         usage(argv[0]);
173                         ret = -EINVAL;
174                         goto out;
175                 }
176         }
177
178         if (filename[0] == '\0') {
179                 fprintf(stderr, "An image file MUST be specified with the -f option.\n\n");
180                 usage(argv[0]);
181                 ret = -EINVAL;
182                 goto out;
183         }
184
185         image_fp = fopen(filename, "rb");
186         if (image_fp == NULL) {
187                 perror("fopen");
188                 ret = -EINVAL;
189                 goto out;
190         }
191         ret = fstat(fileno(image_fp), &st);
192         if (ret < 0) {
193                 perror("fstat");
194                 goto out_close_image_fp;
195         }
196         size = st.st_size;
197
198         image = malloc(size * sizeof(unsigned char));
199         if (image == NULL) {
200                 perror("malloc");
201                 ret = -ENOMEM;
202                 goto out_close_image_fp;
203         }
204
205         ret = (int)fread(image, size, 1, image_fp);
206         if (ret != 1) {
207                 if (feof(image_fp))
208                         fprintf(stderr, "Unexpected end of file.\n");
209                 else if (ferror(image_fp))
210                         perror("fread");
211                 else
212                         fprintf(stderr, "Unexpected error condition.\n");
213
214                 if (ret >= 0)
215                         ret = -EINVAL;
216                 goto out_free_image;
217         }
218
219         ret = am7xxx_init(&ctx);
220         if (ret < 0) {
221                 perror("am7xxx_init");
222                 goto out_free_image;
223         }
224
225         am7xxx_set_log_level(ctx, log_level);
226
227         ret = am7xxx_open_device(ctx, &dev, 0);
228         if (ret < 0) {
229                 perror("am7xxx_open_device");
230                 goto cleanup;
231         }
232
233         ret = am7xxx_close_device(dev);
234         if (ret < 0) {
235                 perror("am7xxx_close_device");
236                 goto cleanup;
237         }
238
239         ret = am7xxx_open_device(ctx, &dev, device_index);
240         if (ret < 0) {
241                 perror("am7xxx_open_device");
242                 goto cleanup;
243         }
244
245         ret = am7xxx_get_device_info(dev, &device_info);
246         if (ret < 0) {
247                 perror("am7xxx_get_device_info");
248                 goto cleanup;
249         }
250         printf("Native resolution: %dx%d\n",
251                device_info.native_width, device_info.native_height);
252
253         ret = am7xxx_set_zoom_mode(dev, zoom);
254         if (ret < 0) {
255                 perror("am7xxx_set_zoom_mode");
256                 goto cleanup;
257         }
258
259         ret = am7xxx_set_power_mode(dev, power_mode);
260         if (ret < 0) {
261                 perror("am7xxx_set_power_mode");
262                 goto cleanup;
263         }
264
265         /* When setting AM7XXX_ZOOM_TEST don't display the actual image */
266         if (zoom == AM7XXX_ZOOM_TEST) {
267                 printf("AM7XXX_ZOOM_TEST requested, not sending actual image.\n");
268                 goto cleanup;
269         }
270
271         if ((unsigned int)width > device_info.native_width ||
272             (unsigned int)height > device_info.native_height)
273                 fprintf(stderr,
274                         "WARNING: image is %dx%d, not fitting the native resolution, it may be displayed wrongly!\n",
275                         width, height);
276
277         ret = am7xxx_send_image(dev, format, width, height, image, (unsigned int)size);
278         if (ret < 0) {
279                 perror("am7xxx_send_image");
280                 goto cleanup;
281         }
282
283         ret = 0;
284
285 cleanup:
286         am7xxx_shutdown(ctx);
287
288 out_free_image:
289         free(image);
290
291 out_close_image_fp:
292         if (fclose(image_fp) == EOF)
293                 perror("fclose");
294
295 out:
296         return ret;
297 }