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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package media
import (
"context"
"encoding/binary"
"image/jpeg"
"io"
"os"
"strings"
"codeberg.org/gruf/go-byteutil"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/log"
)
const (
// image magic header bytes.
magicJPEG = "\xff\xd8\xff"
)
// probe will first attempt to probe the file at path using native Go code
// (for performance), but falls back to using ffprobe to retrieve media details.
func probe(ctx context.Context, filepath string) (*result, error) {
// Open input file at given path.
file, err := os.Open(filepath)
if err != nil {
return nil, gtserror.Newf("error opening file %s: %w", filepath, err)
}
// Close on return.
defer file.Close()
// Byte buf to check for
// file header magic bytes.
buf := make([]byte, 3)
// Read file header into buffer.
_, err = io.ReadFull(file, buf)
if err != nil {
return nil, gtserror.Newf("error reading file %s: %w", filepath, err)
}
switch {
// Attempt to probe JPEG types
// separately, to save calls into
// WebAssembly for a common image.
case string(buf[:len(magicJPEG)]) == magicJPEG:
log.Debug(ctx, "probing jpeg")
return probeJPEG(file)
default:
// Close BEFORE
// pass to ffprobe.
_ = file.Close()
// For everything else, fall back
// to calling ffprobe on input file.
log.Debug(ctx, "ffprobing file")
return ffprobe(ctx, filepath)
}
}
// probeJPEG decodes the given file as JPEG and determines
// image details from the decoded JPEG using native Go code.
func probeJPEG(file *os.File) (*result, error) {
// Attempt to decode JPEG, adding back hdr magic.
cfg, err := jpeg.DecodeConfig(io.MultiReader(
strings.NewReader(magicJPEG),
file,
))
if err != nil {
return nil, gtserror.Newf("error decoding file %s: %w", file.Name(), err)
}
// Jump back to file start.
_, err = file.Seek(0, 0)
if err != nil {
return nil, gtserror.Newf("error seeking in file %s: %w", file.Name(), err)
}
// Read orientation data from EXIF.
orientation := readOrientation(file)
// Setup result as if
// ffprobe'd resulting in
// JPEG file container.
var res result
res.format = "image2"
// Set image orientation data.
res.orientation = orientation
// Extract image details.
res.video = []videoStream{{
stream: stream{codec: "mjpeg"},
width: cfg.Width,
height: cfg.Height,
// setting a pixel color format
// doesn't matter for JPEG, as we
// don't bother even using it.
pixfmt: "",
}}
return &res, nil
}
// readOrientation reads orientation EXIF
// data (if it even exists) from image file.
//
// copied from github.com/disintegration/imaging
// but modified to optimize discard operations.
func readOrientation(r *os.File) int {
const (
markerAPP1 = 0xffe1
exifHeader = 0x45786966
byteOrderBE = 0x4d4d
byteOrderLE = 0x4949
orientationTag = 0x0112
)
// Setup a discard read buffer.
buf := new(byteutil.Buffer)
buf.Guarantee(32)
// discard simply reads into buf.
discard := func(n int) error {
buf.Guarantee(n) // ensure big enough
_, err := io.ReadFull(r, buf.B[:n])
return err
}
// Skip past JPEG SOI marker.
if err := discard(2); err != nil {
return orientationUnspecified
}
// Find JPEG
// APP1 marker.
for {
var marker, size uint16
if err := binary.Read(r, binary.BigEndian, &marker); err != nil {
return orientationUnspecified
}
if err := binary.Read(r, binary.BigEndian, &size); err != nil {
return orientationUnspecified
}
if marker>>8 != 0xff {
return orientationUnspecified // Invalid JPEG marker.
}
if marker == markerAPP1 {
break
}
if size < 2 {
return orientationUnspecified // Invalid block size.
}
if err := discard(int(size - 2)); err != nil {
return orientationUnspecified
}
}
// Check if EXIF
// header is present.
var header uint32
if err := binary.Read(r, binary.BigEndian, &header); err != nil {
return orientationUnspecified
}
if header != exifHeader {
return orientationUnspecified
}
if err := discard(2); err != nil {
return orientationUnspecified
}
// Read byte
// order info.
var (
byteOrderTag uint16
byteOrder binary.ByteOrder
)
if err := binary.Read(r, binary.BigEndian, &byteOrderTag); err != nil {
return orientationUnspecified
}
switch byteOrderTag {
case byteOrderBE:
byteOrder = binary.BigEndian
case byteOrderLE:
byteOrder = binary.LittleEndian
default:
return orientationUnspecified // Invalid byte order flag.
}
if err := discard(2); err != nil {
return orientationUnspecified
}
// Skip the
// EXIF offset.
var offset uint32
if err := binary.Read(r, byteOrder, &offset); err != nil {
return orientationUnspecified
}
if offset < 8 {
return orientationUnspecified // Invalid offset value.
}
if err := discard(int(offset - 8)); err != nil {
return orientationUnspecified
}
// Read the
// number of tags.
var numTags uint16
if err := binary.Read(r, byteOrder, &numTags); err != nil {
return orientationUnspecified
}
// Find the orientation tag.
for i := 0; i < int(numTags); i++ {
var tag uint16
if err := binary.Read(r, byteOrder, &tag); err != nil {
return orientationUnspecified
}
if tag != orientationTag {
if err := discard(10); err != nil {
return orientationUnspecified
}
continue
}
if err := discard(6); err != nil {
return orientationUnspecified
}
var val uint16
if err := binary.Read(r, byteOrder, &val); err != nil {
return orientationUnspecified
}
if val < 1 || val > 8 {
return orientationUnspecified // Invalid tag value.
}
return int(val)
}
// Missing orientation tag.
return orientationUnspecified
}
|