Skip to content

Commit 75425c2

Browse files
author
zRedShift
committedNov 22, 2018
Comment out unused code. Optimize the structure of ffmpeg.go.
1 parent 447c2d5 commit 75425c2

File tree

3 files changed

+170
-215
lines changed

3 files changed

+170
-215
lines changed
 

‎ffmpeg.c

+43-43
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ int create_format_context(AVFormatContext *fmt_ctx, int callbacks) {
3636
if (callbacks & READ_CALLBACK) {
3737
reader = readCallback;
3838
}
39-
if (callbacks & WRITE_CALLBACK) {
40-
writer = writeCallback;
41-
write_flag = 1;
42-
}
39+
// if (callbacks & WRITE_CALLBACK) {
40+
// writer = writeCallback;
41+
// write_flag = 1;
42+
// }
4343
if (callbacks & SEEK_CALLBACK) {
4444
seeker = seekCallback;
4545
}
@@ -216,45 +216,45 @@ AVFrame *convert_frame_to_rgb(AVFrame *frame, int alpha) {
216216
return output_frame;
217217
}
218218

219-
int encode_frame_to_png(AVFormatContext *fmt_ctx, AVFrame *frame) {
220-
AVCodec *enc = avcodec_find_encoder(AV_CODEC_ID_PNG);
221-
if (!enc) {
222-
return AVERROR_ENCODER_NOT_FOUND;
223-
}
224-
AVCodecContext *enc_ctx = avcodec_alloc_context3(enc);
225-
if (!enc_ctx) {
226-
return AVERROR(ENOMEM);
227-
}
228-
enc_ctx->width = frame->width;
229-
enc_ctx->height = frame->height;
230-
enc_ctx->pix_fmt = frame->format;
231-
enc_ctx->codec_type = AVMEDIA_TYPE_VIDEO;
232-
enc_ctx->time_base = (AVRational) {1, 1};
233-
enc_ctx->compression_level = INT_MAX;
234-
int err = open_codec(enc_ctx, enc);
235-
if (err < 0) {
236-
avcodec_free_context(&enc_ctx);
237-
return err;
238-
}
239-
AVPacket pkt;
240-
av_init_packet(&pkt);
241-
pkt.data = NULL;
242-
pkt.size = 0;
243-
err = avcodec_send_frame(enc_ctx, frame);
244-
if (err < 0) {
245-
avcodec_free_context(&enc_ctx);
246-
return err;
247-
}
248-
err = avcodec_receive_packet(enc_ctx, &pkt);
249-
if (err < 0) {
250-
avcodec_free_context(&enc_ctx);
251-
return err;
252-
}
253-
err = writeCallback(fmt_ctx, pkt.data, pkt.size);
254-
av_packet_unref(&pkt);
255-
avcodec_free_context(&enc_ctx);
256-
return err;
257-
}
219+
//int encode_frame_to_png(AVFormatContext *fmt_ctx, AVFrame *frame) {
220+
// AVCodec *enc = avcodec_find_encoder(AV_CODEC_ID_PNG);
221+
// if (!enc) {
222+
// return AVERROR_ENCODER_NOT_FOUND;
223+
// }
224+
// AVCodecContext *enc_ctx = avcodec_alloc_context3(enc);
225+
// if (!enc_ctx) {
226+
// return AVERROR(ENOMEM);
227+
// }
228+
// enc_ctx->width = frame->width;
229+
// enc_ctx->height = frame->height;
230+
// enc_ctx->pix_fmt = frame->format;
231+
// enc_ctx->codec_type = AVMEDIA_TYPE_VIDEO;
232+
// enc_ctx->time_base = (AVRational) {1, 1};
233+
// enc_ctx->compression_level = INT_MAX;
234+
// int err = open_codec(enc_ctx, enc);
235+
// if (err < 0) {
236+
// avcodec_free_context(&enc_ctx);
237+
// return err;
238+
// }
239+
// AVPacket pkt;
240+
// av_init_packet(&pkt);
241+
// pkt.data = NULL;
242+
// pkt.size = 0;
243+
// err = avcodec_send_frame(enc_ctx, frame);
244+
// if (err < 0) {
245+
// avcodec_free_context(&enc_ctx);
246+
// return err;
247+
// }
248+
// err = avcodec_receive_packet(enc_ctx, &pkt);
249+
// if (err < 0) {
250+
// avcodec_free_context(&enc_ctx);
251+
// return err;
252+
// }
253+
// err = writeCallback(fmt_ctx, pkt.data, pkt.size);
254+
// av_packet_unref(&pkt);
255+
// avcodec_free_context(&enc_ctx);
256+
// return err;
257+
//}
258258

259259
AVPacket create_packet() {
260260
AVPacket pkt;

‎ffmpeg.go

+122-167
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ import (
1818

1919
//export readCallback
2020
func readCallback(opaque unsafe.Pointer, buf *C.uint8_t, bufSize C.int) C.int {
21-
ctx, ok := ctxMap.file(opaque)
21+
ctx, ok := ctxMap.context(opaque)
2222
if !ok {
2323
return C.int(avErrUnknown)
2424
}
2525
p := (*[1 << 30]byte)(unsafe.Pointer(buf))[:bufSize:bufSize]
26-
n, err := ctx.Read(p)
26+
n, err := ctx.file.Read(p)
2727
if n > 0 || err == nil {
2828
return C.int(n)
2929
}
@@ -35,39 +35,39 @@ func readCallback(opaque unsafe.Pointer, buf *C.uint8_t, bufSize C.int) C.int {
3535
}
3636
}
3737

38-
//export writeCallback
39-
func writeCallback(opaque unsafe.Pointer, buf *C.uint8_t, bufSize C.int) C.int {
40-
ctx, ok := ctxMap.file(opaque)
41-
if !ok {
42-
return C.int(avErrUnknown)
43-
}
44-
p := (*[1 << 30]byte)(unsafe.Pointer(buf))[:bufSize:bufSize]
45-
n, err := ctx.Write(p)
46-
if err != nil {
47-
return C.int(avErrUnknown)
48-
}
49-
return C.int(n)
50-
}
38+
////export writeCallback
39+
//func writeCallback(opaque unsafe.Pointer, buf *C.uint8_t, bufSize C.int) C.int {
40+
// ctx, ok := ctxMap.file(opaque)
41+
// if !ok {
42+
// return C.int(avErrUnknown)
43+
// }
44+
// p := (*[1 << 30]byte)(unsafe.Pointer(buf))[:bufSize:bufSize]
45+
// n, err := ctx.Write(p)
46+
// if err != nil {
47+
// return C.int(avErrUnknown)
48+
// }
49+
// return C.int(n)
50+
//}
5151

5252
//export seekCallback
5353
func seekCallback(opaque unsafe.Pointer, offset C.int64_t, whence C.int) C.int64_t {
54-
ctx, ok := ctxMap.file(opaque)
54+
ctx, ok := ctxMap.context(opaque)
5555
if !ok {
5656
return C.int64_t(avErrUnknown)
5757
}
58-
if !ctx.SeekEnd && whence >= C.SEEK_END {
59-
f, ok := ctx.Reader.(*seekstream.File)
58+
if !ctx.file.SeekEnd && whence >= C.SEEK_END {
59+
f, ok := ctx.file.Reader.(*seekstream.File)
6060
if !ok || !f.IsDone() {
6161
return C.int64_t(avErrUnknown)
6262

6363
}
64-
ctx.SeekEnd = true
65-
ctx.Size = f.Size()
64+
ctx.file.SeekEnd = true
65+
ctx.file.Size = f.Size()
6666
}
67-
if whence == C.AVSEEK_SIZE && ctx.Size > 0 {
68-
return C.int64_t(ctx.Size)
67+
if whence == C.AVSEEK_SIZE && ctx.file.Size > 0 {
68+
return C.int64_t(ctx.file.Size)
6969
}
70-
n, err := ctx.Seek(int64(offset), int(whence))
70+
n, err := ctx.file.Seek(int64(offset), int(whence))
7171
if err != nil {
7272
return C.int64_t(avErrUnknown)
7373
}
@@ -78,7 +78,7 @@ func seekCallback(opaque unsafe.Pointer, offset C.int64_t, whence C.int) C.int64
7878
func interruptCallback(opaque unsafe.Pointer) C.int {
7979
if ctx, ok := ctxMap.context(opaque); ok {
8080
select {
81-
case <-ctx.Done():
81+
case <-ctx.context.Done():
8282
return 1
8383
default:
8484
return 0
@@ -114,7 +114,7 @@ func SetFFmpegLogLevel(logLevel AVLogLevel) {
114114

115115
const (
116116
readCallbackFlag = 1 << iota
117-
writeCallbackflag
117+
//writeCallbackflag
118118
seekCallbackFlag
119119
interruptCallbackFlag
120120
)
@@ -124,27 +124,26 @@ const (
124124
hasAudio
125125
)
126126

127-
type contextKey int
128-
129-
const (
130-
fileKey contextKey = iota
131-
fmtCtxKey
132-
streamKey
133-
decCtxKey
134-
thumbCtxKey
135-
frameKey
136-
durationKey
137-
)
127+
type avContext struct {
128+
context context.Context
129+
file *File
130+
formatContext *C.AVFormatContext
131+
stream *C.AVStream
132+
codecContext *C.AVCodecContext
133+
thumbContext *C.ThumbContext
134+
frame *C.AVFrame
135+
durationInFormat bool
136+
}
138137

139138
type avError int
140139

141140
const (
142-
avErrNoMem avError = -C.ENOMEM
143-
avErrEOF avError = C.AVERROR_EOF
144-
avErrUnknown avError = C.AVERROR_UNKNOWN
145-
avErrDecoderNotFound avError = C.AVERROR_DECODER_NOT_FOUND
146-
avErrInvalidData avError = C.AVERROR_INVALIDDATA
147-
errTooBig avError = C.ERR_TOO_BIG
141+
avErrNoMem = avError(-C.ENOMEM)
142+
avErrEOF = avError(C.AVERROR_EOF)
143+
avErrUnknown = avError(C.AVERROR_UNKNOWN)
144+
avErrDecoderNotFound = avError(C.AVERROR_DECODER_NOT_FOUND)
145+
avErrInvalidData = avError(C.AVERROR_INVALIDDATA)
146+
errTooBig = avError(C.ERR_TOO_BIG)
148147
)
149148

150149
func (e avError) errorString() string {
@@ -167,181 +166,144 @@ func (e avError) Error() string { return "ffmpeg: " + e.errorString() }
167166

168167
type contextMap struct {
169168
sync.RWMutex
170-
m map[*C.AVFormatContext]context.Context
169+
m map[*C.AVFormatContext]*avContext
171170
}
172171

173-
func (m *contextMap) file(opaque unsafe.Pointer) (file *File, ok bool) {
172+
func (m *contextMap) context(opaque unsafe.Pointer) (*avContext, bool) {
174173
m.RLock()
175174
ctx, ok := m.m[(*C.AVFormatContext)(opaque)]
176175
m.RUnlock()
177-
if ok {
178-
file, ok = ctx.Value(fileKey).(*File)
179-
}
180-
return
181-
}
182-
183-
func (m *contextMap) context(opaque unsafe.Pointer) (ctx context.Context, ok bool) {
184-
m.RLock()
185-
ctx, ok = m.m[(*C.AVFormatContext)(opaque)]
186-
m.RUnlock()
187-
return
176+
return ctx, ok
188177
}
189178

190-
func (m *contextMap) set(ctx context.Context) {
191-
fmtCtx := ctx.Value(fmtCtxKey).(*C.AVFormatContext)
179+
func (m *contextMap) set(ctx *avContext) {
192180
m.Lock()
193-
m.m[fmtCtx] = ctx
181+
m.m[ctx.formatContext] = ctx
194182
m.Unlock()
195-
return
196183
}
197184

198-
func (m *contextMap) delete(ctx context.Context) (*C.AVFormatContext, bool) {
199-
if fmtCtx, ok := ctx.Value(fmtCtxKey).(*C.AVFormatContext); ok {
200-
m.Lock()
201-
delete(m.m, fmtCtx)
202-
m.Unlock()
203-
return fmtCtx, true
204-
}
205-
return nil, false
185+
func (m *contextMap) delete(ctx *avContext) {
186+
m.Lock()
187+
delete(m.m, ctx.formatContext)
188+
m.Unlock()
206189
}
207190

208-
var ctxMap = contextMap{m: make(map[*C.AVFormatContext]context.Context)}
191+
var ctxMap = contextMap{m: make(map[*C.AVFormatContext]*avContext)}
209192

210-
func freeFormatContext(ctx context.Context) {
211-
if fmtCtx, ok := ctxMap.delete(ctx); ok && fmtCtx != nil {
212-
C.free_format_context(fmtCtx)
213-
}
193+
func freeFormatContext(ctx *avContext) {
194+
C.free_format_context(ctx.formatContext)
195+
ctxMap.delete(ctx)
214196
}
215197

216-
func createFormatContext(ctx context.Context, callbackFlags C.int) (context.Context, error) {
217-
var fmtCtx *C.AVFormatContext
218-
intErr := C.allocate_format_context(&fmtCtx)
198+
func createFormatContext(ctx *avContext, callbackFlags C.int) error {
199+
intErr := C.allocate_format_context(&ctx.formatContext)
219200
if intErr < 0 {
220-
return nil, avError(intErr)
201+
return avError(intErr)
221202
}
222-
ctx = context.WithValue(ctx, fmtCtxKey, fmtCtx)
223203
ctxMap.set(ctx)
224-
intErr = C.create_format_context(fmtCtx, callbackFlags)
204+
intErr = C.create_format_context(ctx.formatContext, callbackFlags)
225205
if intErr < 0 {
226206
ctxMap.delete(ctx)
227-
return nil, avError(intErr)
207+
return avError(intErr)
228208
}
229209
metaData(ctx)
230-
ctx = duration(ctx)
231-
ctx, err := findStreams(ctx)
210+
duration(ctx)
211+
err := findStreams(ctx)
232212
if err != nil {
233213
freeFormatContext(ctx)
234214
}
235-
return ctx, err
215+
return err
236216
}
237217

238-
func metaData(ctx context.Context) {
218+
func metaData(ctx *avContext) {
239219
var artist, title *C.char
240-
fmtCtx := ctx.Value(fmtCtxKey).(*C.AVFormatContext)
241-
C.get_metadata(fmtCtx, &artist, &title)
242-
file := ctx.Value(fileKey).(*File)
243-
file.Artist = C.GoString(artist)
244-
file.Title = C.GoString(title)
220+
C.get_metadata(ctx.formatContext, &artist, &title)
221+
ctx.file.Artist = C.GoString(artist)
222+
ctx.file.Title = C.GoString(title)
245223
}
246224

247-
func duration(ctx context.Context) context.Context {
248-
durationInFormat := false
249-
if fmtCtx := ctx.Value(fmtCtxKey).(*C.AVFormatContext); fmtCtx.duration > 0 {
250-
durationInFormat = true
251-
ctx.Value(fileKey).(*File).Duration = time.Duration(1000 * fmtCtx.duration)
225+
func duration(ctx *avContext) {
226+
if ctx.formatContext.duration > 0 {
227+
ctx.durationInFormat = true
228+
ctx.file.Duration = time.Duration(1000 * ctx.formatContext.duration)
252229
}
253-
return context.WithValue(ctx, durationKey, durationInFormat)
254-
255230
}
256231

257-
func fullDuration(ctx context.Context) error {
232+
func fullDuration(ctx *avContext) error {
258233
defer freeFormatContext(ctx)
259-
if ctx.Value(durationKey).(bool) {
234+
if ctx.durationInFormat {
260235
return nil
261236
}
262-
newDuration := time.Duration(C.find_duration(ctx.Value(fmtCtxKey).(*C.AVFormatContext)))
237+
newDuration := time.Duration(C.find_duration(ctx.formatContext))
263238
if newDuration < 0 {
264239
return avError(newDuration)
265240
}
266-
file := ctx.Value(fileKey).(*File)
267-
if newDuration > file.Duration {
268-
file.Duration = newDuration
241+
if newDuration > ctx.file.Duration {
242+
ctx.file.Duration = newDuration
269243
}
270244
return nil
271245
}
272246

273-
func findStreams(ctx context.Context) (context.Context, error) {
274-
var vStream *C.AVStream
247+
func findStreams(ctx *avContext) error {
275248
var orientation C.int
276-
err := C.find_streams(ctx.Value(fmtCtxKey).(*C.AVFormatContext), &vStream, &orientation)
249+
err := C.find_streams(ctx.formatContext, &ctx.stream, &orientation)
277250
if err < 0 {
278-
return ctx, avError(err)
251+
return avError(err)
279252
}
280-
ctx = context.WithValue(ctx, streamKey, vStream)
281-
file := ctx.Value(fileKey).(*File)
282-
file.HasVideo = err&hasVideo != 0
283-
file.HasAudio = err&hasAudio != 0
284-
if !file.HasVideo {
285-
file.Media = "audio"
253+
ctx.file.HasVideo = err&hasVideo != 0
254+
ctx.file.HasAudio = err&hasAudio != 0
255+
if !ctx.file.HasVideo {
256+
ctx.file.Media = "audio"
286257
} else {
287-
file.Width = int(vStream.codecpar.width)
288-
file.Height = int(vStream.codecpar.height)
289-
file.Orientation = int(orientation)
258+
ctx.file.Width = int(ctx.stream.codecpar.width)
259+
ctx.file.Height = int(ctx.stream.codecpar.height)
260+
ctx.file.Orientation = int(orientation)
290261
}
291-
return ctx, nil
262+
return nil
292263
}
293264

294-
func createDecoder(ctx context.Context) (context.Context, error) {
295-
var decCtx *C.AVCodecContext
296-
err := C.create_codec_context(ctx.Value(streamKey).(*C.AVStream), &decCtx)
265+
func createDecoder(ctx *avContext) error {
266+
err := C.create_codec_context(ctx.stream, &ctx.codecContext)
297267
if err < 0 {
298-
return ctx, avError(err)
268+
return avError(err)
299269
}
300-
ctx = context.WithValue(ctx, decCtxKey, decCtx)
301-
defer C.avcodec_free_context(&decCtx)
270+
defer C.avcodec_free_context(&ctx.codecContext)
302271
return createThumbContext(ctx)
303272
}
304273

305-
func incrementDuration(ctx context.Context, frame *C.AVFrame) {
306-
if !ctx.Value(durationKey).(bool) && frame.pts != C.AV_NOPTS_VALUE {
307-
vStream := ctx.Value(streamKey).(*C.AVStream)
308-
ptsToNano := C.int64_t(1000000000 * vStream.time_base.num / vStream.time_base.den)
274+
func incrementDuration(ctx *avContext, frame *C.AVFrame) {
275+
if !ctx.durationInFormat && frame.pts != C.AV_NOPTS_VALUE {
276+
ptsToNano := C.int64_t(1000000000 * ctx.stream.time_base.num / ctx.stream.time_base.den)
309277
newDuration := time.Duration(frame.pts * ptsToNano)
310-
file := ctx.Value(fileKey).(*File)
311-
if newDuration > file.Duration {
312-
file.Duration = newDuration
278+
if newDuration > ctx.file.Duration {
279+
ctx.file.Duration = newDuration
313280
}
314281
}
315282
}
316283

317-
func populateHistogram(ctx context.Context, frames <-chan *C.AVFrame) <-chan struct{} {
284+
func populateHistogram(ctx *avContext, frames <-chan *C.AVFrame) <-chan struct{} {
318285
done := make(chan struct{})
319-
thumbCtx := ctx.Value(thumbCtxKey).(*C.ThumbContext)
320286
go func() {
321287
var n C.int
322288
for frame := range frames {
323-
C.populate_histogram(thumbCtx, n, frame)
289+
C.populate_histogram(ctx.thumbContext, n, frame)
324290
n++
325291
}
326-
thumbCtx.n = n
292+
ctx.thumbContext.n = n
327293
done <- struct{}{}
328294
close(done)
329295
}()
330296
return done
331297
}
332298

333-
func createThumbContext(ctx context.Context) (context.Context, error) {
299+
func createThumbContext(ctx *avContext) error {
334300
pkt := C.create_packet()
335-
fmtCtx := ctx.Value(fmtCtxKey).(*C.AVFormatContext)
336-
vStream := ctx.Value(streamKey).(*C.AVStream)
337-
decCtx := ctx.Value(decCtxKey).(*C.AVCodecContext)
338301
var frame *C.AVFrame
339-
var thumbCtx *C.ThumbContext
340-
err := C.obtain_next_frame(fmtCtx, decCtx, vStream.index, &pkt, &frame)
302+
err := C.obtain_next_frame(ctx.formatContext, ctx.codecContext, ctx.stream.index, &pkt, &frame)
341303
if err >= 0 {
342304
incrementDuration(ctx, frame)
343-
thumbCtx = C.create_thumb_context(vStream, frame)
344-
if thumbCtx == nil {
305+
ctx.thumbContext = C.create_thumb_context(ctx.stream, frame)
306+
if ctx.thumbContext == nil {
345307
err = C.int(avErrNoMem)
346308
}
347309
}
@@ -352,11 +314,10 @@ func createThumbContext(ctx context.Context) (context.Context, error) {
352314
if frame != nil {
353315
C.av_frame_free(&frame)
354316
}
355-
return ctx, avError(err)
317+
return avError(err)
356318
}
357-
ctx = context.WithValue(ctx, thumbCtxKey, thumbCtx)
358-
defer C.free_thumb_context(thumbCtx)
359-
frames := make(chan *C.AVFrame, thumbCtx.max_frames)
319+
defer C.free_thumb_context(ctx.thumbContext)
320+
frames := make(chan *C.AVFrame, ctx.thumbContext.max_frames)
360321
done := populateHistogram(ctx, frames)
361322
frames <- frame
362323
if pkt.buf != nil {
@@ -365,16 +326,12 @@ func createThumbContext(ctx context.Context) (context.Context, error) {
365326
return populateThumbContext(ctx, frames, done)
366327
}
367328

368-
func populateThumbContext(ctx context.Context, frames chan *C.AVFrame, done <-chan struct{}) (context.Context, error) {
329+
func populateThumbContext(ctx *avContext, frames chan *C.AVFrame, done <-chan struct{}) error {
369330
pkt := C.create_packet()
370331
var frame *C.AVFrame
371-
fmtCtx := ctx.Value(fmtCtxKey).(*C.AVFormatContext)
372-
vStream := ctx.Value(streamKey).(*C.AVStream)
373-
decCtx := ctx.Value(decCtxKey).(*C.AVCodecContext)
374-
thumbCtx := ctx.Value(thumbCtxKey).(*C.ThumbContext)
375332
var err C.int
376-
for i := C.int(1); i < thumbCtx.max_frames; i++ {
377-
err = C.obtain_next_frame(fmtCtx, decCtx, vStream.index, &pkt, &frame)
333+
for i := C.int(1); i < ctx.thumbContext.max_frames; i++ {
334+
err = C.obtain_next_frame(ctx.formatContext, ctx.codecContext, ctx.stream.index, &pkt, &frame)
378335
if err < 0 {
379336
break
380337
}
@@ -391,48 +348,46 @@ func populateThumbContext(ctx context.Context, frames chan *C.AVFrame, done <-ch
391348
}
392349
<-done
393350
if err != 0 && err != C.int(avErrEOF) {
394-
return ctx, avError(err)
351+
return avError(err)
395352
}
396353
return convertFrameToRGB(ctx)
397354
}
398355

399-
func convertFrameToRGB(ctx context.Context) (context.Context, error) {
400-
thumbCtx := ctx.Value(thumbCtxKey).(*C.ThumbContext)
401-
outputFrame := C.convert_frame_to_rgb(C.process_frames(thumbCtx), thumbCtx.alpha)
356+
func convertFrameToRGB(ctx *avContext) error {
357+
outputFrame := C.convert_frame_to_rgb(C.process_frames(ctx.thumbContext), ctx.thumbContext.alpha)
402358
if outputFrame == nil {
403-
return ctx, avErrNoMem
359+
return avErrNoMem
404360
}
405-
ctx = context.WithValue(ctx, frameKey, outputFrame)
406-
ctx.Value(fileKey).(*File).HasAlpha = thumbCtx.alpha != 0
407-
return ctx, nil
361+
ctx.frame = outputFrame
362+
ctx.file.HasAlpha = ctx.thumbContext.alpha != 0
363+
return nil
408364
}
409365

410-
func thumbnail(ctx context.Context) <-chan error {
366+
func thumbnail(ctx *avContext) <-chan error {
411367
errCh := make(chan error)
412-
outputFrame := ctx.Value(frameKey).(*C.AVFrame)
413368
go func() {
414-
err := thumbnailFromFFmpeg(ctx.Value(fileKey).(*File), outputFrame.data[0])
415-
C.av_frame_free(&outputFrame)
369+
err := thumbnailFromFFmpeg(ctx.file, ctx.frame.data[0])
370+
C.av_frame_free(&ctx.frame)
416371
errCh <- err
417372
close(errCh)
418373
}()
419374
return errCh
420375
}
421376

422-
func ffmpegThumbnail(ctx context.Context, file *File) error {
423-
ctx = context.WithValue(ctx, fileKey, file)
377+
func ffmpegThumbnail(context context.Context, file *File) error {
378+
ctx := &avContext{context: context, file: file}
424379
callbackFlags := C.int(readCallbackFlag | interruptCallbackFlag)
425380
if file.Seeker != nil {
426381
callbackFlags |= seekCallbackFlag
427382
}
428-
ctx, err := createFormatContext(ctx, callbackFlags)
383+
err := createFormatContext(ctx, callbackFlags)
429384
if err != nil {
430385
return err
431386
}
432387
if !file.HasVideo {
433388
return fullDuration(ctx)
434389
}
435-
if ctx, err = createDecoder(ctx); err == errTooBig || err == avErrDecoderNotFound {
390+
if err = createDecoder(ctx); err == errTooBig || err == avErrDecoderNotFound {
436391
return fullDuration(ctx)
437392
}
438393
if err != nil {

‎ffmpeg.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
#define BUFFER_SIZE 1 << 12
1616
#define READ_CALLBACK 1
17-
#define WRITE_CALLBACK 2
18-
#define SEEK_CALLBACK 4
19-
#define INTERRUPT_CALLBACK 8
17+
//#define WRITE_CALLBACK 2
18+
#define SEEK_CALLBACK 2
19+
#define INTERRUPT_CALLBACK 4
2020
#define HAS_VIDEO_STREAM 1
2121
#define HAS_AUDIO_STREAM 2
2222
#define ERR_TOO_BIG FFERRTAG('H','M','M','M')
@@ -48,7 +48,7 @@ int create_codec_context(AVStream *video_stream, AVCodecContext **dec_ctx);
4848

4949
AVFrame *convert_frame_to_rgb(AVFrame *frame, int alpha);
5050

51-
int encode_frame_to_png(AVFormatContext *fmt_ctx, AVFrame *frame);
51+
//int encode_frame_to_png(AVFormatContext *fmt_ctx, AVFrame *frame);
5252

5353
AVPacket create_packet();
5454

@@ -67,7 +67,7 @@ void populate_histogram(ThumbContext *thumb_ctx, int n, AVFrame *frame);
6767

6868
extern int readCallback(void *opaque, uint8_t *buf, int buf_size);
6969

70-
extern int writeCallback(void *opaque, uint8_t *buf, int buf_size);
70+
//extern int writeCallback(void *opaque, uint8_t *buf, int buf_size);
7171

7272
extern int64_t seekCallback(void *opaque, int64_t seek, int whence);
7373

0 commit comments

Comments
 (0)
Please sign in to comment.