summaryrefslogtreecommitdiff
path: root/lib/jpg-source.c
blob: 0bbe26fae2f2a9ce3c9cf5fe5e436b1fb0e61700 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * JPEG still image video source
 *
 * Copyright (C) 2018 Paul Elder
 *
 * Contact: Paul Elder <paul.elder@ideasonboard.com>
 */

#include <sys/types.h>
#include <sys/stat.h>

#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <linux/videodev2.h>

#include "events.h"
#include "tools.h"
#include "v4l2.h"
#include "jpg-source.h"
#include "video-buffers.h"

struct jpg_source {
	struct video_source src;

	unsigned int imgsize;
	void *imgdata;
};

#define to_jpg_source(s) container_of(s, struct jpg_source, src)

static void jpg_source_destroy(struct video_source *s)
{
	struct jpg_source *src = to_jpg_source(s);

	if (src->imgdata)
		free(src->imgdata);

	free(src);
}

static int jpg_source_set_format(struct video_source *s __attribute__((unused)),
				  struct v4l2_pix_format *fmt)
{
	if (fmt->pixelformat != v4l2_fourcc('M', 'J', 'P', 'G')) {
		printf("jpg-source: unsupported fourcc\n");
		return -EINVAL;
	}

	return 0;
}

static int jpg_source_set_frame_rate(struct video_source *s __attribute__((unused)),
				     unsigned int fps __attribute__((unused)))
{
	return 0;
}

static int jpg_source_free_buffers(struct video_source *s __attribute__((unused)))
{
	return 0;
}

static int jpg_source_stream_on(struct video_source *s __attribute__((unused)))
{
	return 0;
}

static int jpg_source_stream_off(struct video_source *s __attribute__((unused)))
{
	return 0;
}

static void jpg_source_fill_buffer(struct video_source *s,
				   struct video_buffer *buf)
{
	struct jpg_source *src = to_jpg_source(s);

	memcpy(buf->mem, src->imgdata, src->imgsize);
	buf->bytesused = src->imgsize;
}

static const struct video_source_ops jpg_source_ops = {
	.destroy = jpg_source_destroy,
	.set_format = jpg_source_set_format,
	.set_frame_rate = jpg_source_set_frame_rate,
	.alloc_buffers = NULL,
	.export_buffers = NULL,
	.free_buffers = jpg_source_free_buffers,
	.stream_on = jpg_source_stream_on,
	.stream_off = jpg_source_stream_off,
	.queue_buffer = NULL,
	.fill_buffer = jpg_source_fill_buffer,
};

struct video_source *jpg_video_source_create(const char *img_path)
{
	struct jpg_source *src;
	int fd = -1;
	int ret;

	printf("using jpg video source\n");

	if (img_path == NULL)
		return NULL;

	src = malloc(sizeof *src);
	if (!src)
		return NULL;

	memset(src, 0, sizeof *src);
	src->src.ops = &jpg_source_ops;

	fd = open(img_path, O_RDONLY);
	if (fd == -1) {
		printf("Unable to open MJPEG image '%s'\n", img_path);
		goto err_free_src;
	}

	src->imgsize = lseek(fd, 0, SEEK_END);
	lseek(fd, 0, SEEK_SET);
	src->imgdata = malloc(src->imgsize);
	if (src->imgdata == NULL) {
		printf("Unable to allocate memory for MJPEG image\n");
		goto err_close_fd;
	}

	ret = read(fd, src->imgdata, src->imgsize);
	if (ret < 0) {
		fprintf(stderr, "error reading data from %s: %d\n", img_path, errno);
		goto err_free_imgdata;
	}

	close(fd);

	return &src->src;

err_free_imgdata:
	free(src->imgdata);
err_close_fd:
	close(fd);
err_free_src:
	free(src);

	return NULL;
}

void jpg_video_source_init(struct video_source *s, struct events *events)
{
	struct jpg_source *src = to_jpg_source(s);

	src->src.events = events;
}