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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
#define AL_LOG_SECTION "stb_image"
#include <al/log.h>
#include "common.h"
#define STBI_MALLOC camu_page_alloc
#define STBI_FREE al_free
#define STBI_REALLOC camu_page_realloc
#define STBI_ASSERT al_assert
#define STBI_NO_STDIO
#define STBI_NO_FAILURE_STRINGS
#define STB_IMAGE_IMPLEMENTATION
#ifdef CAMU_LOCAL_STB
#include <stb_image.h>
#else
#include <stb/stb_image.h>
#endif
#include "stb_image.h"
#define FORCE_RGBA
#ifdef LIANA_SERVER
#include "../cache/entry.h"
static bool stbi_demuxer_init(struct camu_demuxer *demux, struct cch_handle *handle)
{
struct camu_stbi_demuxer *stb = (struct camu_stbi_demuxer *)demux;
stb->handle = handle;
nn_buffer_init(&stb->buffer);
stb->eof = false;
cch_handle_seek(stb->handle, 0, SEEK_SET);
struct cch_backing *backing = stb->handle->entry->backing;
off_t size = backing->get_size_if_known(backing);
nn_buffer_ensure_space(&stb->buffer, size);
cch_handle_read(stb->handle, nn_buffer_get_ptr(&stb->buffer, 0), size);
stb->buffer.size = size;
s32 w, h, channels;
u8 *data = nn_buffer_get_ptr(&stb->buffer, 0);
if (!stbi_info_from_memory(data, stb->buffer.size, &w, &h, &channels)) {
log_error("stbi_info_from_memory() failed.");
return false;
}
struct camu_codec_stream stream = { 0 };
stream.mode = CAMU_NORMAL;
stream.type = CAMU_STREAM_VIDEO;
struct camu_video_format *fmt = &stream.video.fmt;
fmt->width = (u32)w;
fmt->height = (u32)h;
#ifdef FORCE_RGBA
fmt->format = camu_pixel_format_from_channels(4);
#else
fmt->format = camu_pixel_format_from_channels(channels);
#endif
al_array_push(stb->demux.streams, stream);
return true;
}
static s32 stbi_demuxer_get_packet(struct camu_demuxer *demux, struct camu_codec_packet *packet)
{
struct camu_stbi_demuxer *stb = (struct camu_stbi_demuxer *)demux;
if (!stb->eof) {
packet->mode = CAMU_NORMAL;
packet->buffer = &stb->buffer;
stb->eof = true;
return CAMU_OK;
}
return CAMU_ERR_EOF;
}
static u64 stbi_demuxer_get_duration(struct camu_demuxer *demux)
{
(void)demux;
return 0;
}
static bool stbi_demuxer_seek(struct camu_demuxer *demux, u64 pos, bool bytes)
{
(void)demux;
(void)pos;
(void)bytes;
return true;
}
static void stbi_demuxer_free(struct camu_demuxer **demux)
{
struct camu_stbi_demuxer *stb = (struct camu_stbi_demuxer *)*demux;
nn_buffer_free(&stb->buffer);
al_array_free(stb->demux.streams);
al_free(stb);
*demux = NULL;
}
struct camu_demuxer *camu_stbi_demuxer_create(void)
{
struct camu_stbi_demuxer *stb = al_alloc_object(struct camu_stbi_demuxer);
stb->demux.mode = CAMU_NORMAL;
al_array_init(stb->demux.streams);
stb->demux.init = stbi_demuxer_init;
stb->demux.get_packet = stbi_demuxer_get_packet;
stb->demux.get_duration = stbi_demuxer_get_duration;
stb->demux.seek = stbi_demuxer_seek;
stb->demux.free = stbi_demuxer_free;
return (struct camu_demuxer *)stb;
}
#endif
#ifdef LIANA_CLIENT
static bool stbi_decoder_init(struct camu_decoder *dec, struct camu_renderer *renderer, struct camu_codec_stream *stream,
void (*callback)(void *, struct camu_codec_frame *), void *userdata)
{
struct camu_stbi_decoder *stb = (struct camu_stbi_decoder *)dec;
(void)renderer;
stb->stream = stream;
stb->callback = callback;
stb->userdata = userdata;
return true;
}
static s32 stbi_decoder_push(struct camu_decoder *dec, struct camu_codec_packet *packet)
{
struct camu_stbi_decoder *stb = (struct camu_stbi_decoder *)dec;
if (!packet) return CAMU_OK;
s32 w, h, channels;
u8 *data = nn_buffer_get_ptr(packet->buffer, 0);
#ifdef FORCE_RGBA
u8 *pixels = stbi_load_from_memory(data, packet->buffer->size, &w, &h, &channels, 4);
#else
u8 *pixels = stbi_load_from_memory(data, packet->buffer->size, &w, &h, &channels, 0);
#endif
if (!pixels) {
log_error("stbi_load_from_memory() failed.");
return CAMU_ERR_ERROR;
}
struct camu_video_format *fmt = &stb->stream->video.fmt;
al_assert(w > 0 && h > 0 && (u32)w == fmt->width && (u32)h == fmt->height);
struct camu_codec_frame *frame = al_alloc_object(struct camu_codec_frame);
frame->mode = CAMU_NORMAL;
frame->flags = 0;
frame->data = pixels;
frame->video.width = (u32)w;
frame->video.height = (u32)h;
#ifdef FORCE_RGBA
frame->format = camu_pixel_format_from_channels(4);
#else
frame->format = camu_pixel_format_from_channels(channels);
al_assert(frame->format == fmt->format);
#endif
frame->pts = 0.0;
stb->callback(stb->userdata, frame);
return CAMU_OK;
}
static void stbi_decoder_flush(struct camu_decoder *dec)
{
(void)dec;
}
static s32 stbi_decoder_process(struct camu_decoder *dec)
{
(void)dec;
return CAMU_ERR_EOF;
}
static void stbi_decoder_free(struct camu_decoder **dec)
{
struct camu_stbi_decoder *stb = (struct camu_stbi_decoder *)*dec;
al_free(stb);
*dec = NULL;
}
struct camu_decoder *camu_stbi_decoder_create(void)
{
struct camu_stbi_decoder *stb = al_alloc_object(struct camu_stbi_decoder);
stb->dec.mode = CAMU_NORMAL;
stb->dec.init = stbi_decoder_init;
stb->dec.push = stbi_decoder_push;
stb->dec.process = stbi_decoder_process;
stb->dec.flush = stbi_decoder_flush;
stb->dec.free = stbi_decoder_free;
return (struct camu_decoder *)stb;
}
#endif
|