-
Notifications
You must be signed in to change notification settings - Fork 2
/
image-io.cc
220 lines (166 loc) · 4.04 KB
/
image-io.cc
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* H.265 video codec.
* Copyright (c) 2013-2014 struktur AG, Dirk Farin <[email protected]>
*
* Authors: struktur AG, Dirk Farin <[email protected]>
*
* This file is part of libde265.
*
* libde265 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* libde265 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libde265. If not, see <http://www.gnu.org/licenses/>.
*/
#include "libde265/image-io.h"
#include <assert.h>
ImageSource::ImageSource()
{
}
ImageSource_YUV::ImageSource_YUV()
: mFH(NULL)
{
}
ImageSource_YUV::~ImageSource_YUV()
{
if (mFH) {
fclose(mFH);
}
}
bool ImageSource_YUV::set_input_file(const char* filename, int w,int h)
{
assert(mFH==NULL);
mFH = fopen(filename,"rb");
if (mFH==NULL) {
return false;
}
width =w;
height=h;
mReachedEndOfFile = false;
return true;
}
de265_image* ImageSource_YUV::read_next_image()
{
if (mReachedEndOfFile) return NULL;
de265_image* img = new de265_image;
img->alloc_image(width,height,de265_chroma_420, NULL, false,
NULL, /*NULL,*/ 0, NULL, false);
assert(img); // TODO: error handling
// --- load image ---
uint8_t* p;
int stride;
p = img->get_image_plane(0); stride = img->get_image_stride(0);
for (int y=0;y<height;y++) {
if (fread(p+y*stride,1,width,mFH) != width) {
goto check_eof;
}
}
p = img->get_image_plane(1); stride = img->get_image_stride(1);
for (int y=0;y<height/2;y++) {
if (fread(p+y*stride,1,width/2,mFH) != width/2) {
goto check_eof;
}
}
p = img->get_image_plane(2); stride = img->get_image_stride(2);
for (int y=0;y<height/2;y++) {
if (fread(p+y*stride,1,width/2,mFH) != width/2) {
goto check_eof;
}
}
// --- check for EOF ---
check_eof:
if (feof(mFH)) {
mReachedEndOfFile = true;
delete img;
return NULL;
}
else {
return img;
}
}
/*
ImageSource::ImageStatus ImageSource_YUV::get_status()
{
return Available;
}
*/
de265_image* ImageSource_YUV::get_image(bool block)
{
de265_image* img = read_next_image();
return img;
}
void ImageSource_YUV::skip_frames(int n)
{
int imageSize = width*height*3/2;
fseek(mFH,n * imageSize, SEEK_CUR);
}
ImageSink_YUV::~ImageSink_YUV()
{
if (mFH) {
fclose(mFH);
}
}
bool ImageSink_YUV::set_filename(const char* filename)
{
assert(mFH==NULL);
mFH = fopen(filename,"wb");
return true;
}
void ImageSink_YUV::send_image(const de265_image* img)
{
// --- write image ---
const uint8_t* p;
int stride;
int width = img->get_width();
int height= img->get_height();
p = img->get_image_plane(0); stride = img->get_image_stride(0);
for (int y=0;y<height;y++) {
size_t n = fwrite(p+y*stride,1,width,mFH);
(void)n;
}
p = img->get_image_plane(1); stride = img->get_image_stride(1);
for (int y=0;y<height/2;y++) {
size_t n = fwrite(p+y*stride,1,width/2,mFH);
(void)n;
}
p = img->get_image_plane(2); stride = img->get_image_stride(2);
for (int y=0;y<height/2;y++) {
size_t n = fwrite(p+y*stride,1,width/2,mFH);
(void)n;
}
}
PacketSink_File::PacketSink_File()
: mFH(NULL)
{
}
LIBDE265_API PacketSink_File::~PacketSink_File()
{
if (mFH) {
fclose(mFH);
}
}
LIBDE265_API void PacketSink_File::set_filename(const char* filename)
{
assert(mFH==NULL);
mFH = fopen(filename,"wb");
}
LIBDE265_API void PacketSink_File::send_packet(const uint8_t* data, int n)
{
uint8_t startCode[3];
startCode[0] = 0;
startCode[1] = 0;
startCode[2] = 1;
size_t dummy;
dummy = fwrite(startCode,1,3,mFH);
(void)dummy;
dummy = fwrite(data,1,n,mFH);
(void)dummy;
fflush(mFH);
}