Skip to content

Commit 0e8c89e

Browse files
committed
Display film.
1 parent 9ebc352 commit 0e8c89e

File tree

1 file changed

+68
-1
lines changed

1 file changed

+68
-1
lines changed

ffmpeg.rkt

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@
186186
[id id]))
187187
(match* (type mode)
188188
[('video 'init)
189+
;; Writing output
189190
(set-avcodec-context-codec-id! ctx id)
190191
(set-avcodec-context-bit-rate! ctx 400000)
191192
(set-avcodec-context-width! ctx 1280);1920)
@@ -198,7 +199,40 @@
198199
(when (eq? id 'mpeg2video)
199200
(set-avcodec-context-max-b-frames! ctx 2))
200201
(when (eq? id 'mpeg1video)
201-
(set-avcodec-context-mb-decision! ctx 2))]
202+
(set-avcodec-context-mb-decision! ctx 2))
203+
204+
;; Displaying output
205+
(set! num-bytes
206+
(av-image-get-buffer-size'rgb24
207+
(avcodec-context-width ctx)
208+
(avcodec-context-height ctx)
209+
32))
210+
(set! buff (av-malloc num-bytes _uint8))
211+
(av-image-fill-arrays (av-frame-data frame-rgb)
212+
(av-frame-linesize frame-rgb)
213+
(array-ptr buff)
214+
'rgb24
215+
(avcodec-context-width ctx)
216+
(avcodec-context-height ctx)
217+
1)
218+
(set! sws
219+
(sws-getContext (avcodec-context-width ctx)
220+
(avcodec-context-height ctx)
221+
(avcodec-context-pix-fmt ctx)
222+
(avcodec-context-width ctx)
223+
(avcodec-context-height ctx)
224+
'rgb24
225+
SWS-BILINEAR
226+
#f #f #f))
227+
(set! f (new frame%
228+
[label "Movie"]
229+
[width (avcodec-context-width ctx)]
230+
[height (avcodec-context-height ctx)]))
231+
(set! c (new video-canvas%
232+
[width (avcodec-context-width ctx)]
233+
[height (avcodec-context-height ctx)]
234+
[parent f]))
235+
(send f show #t)]
202236
[('audio 'init)
203237
(set-avcodec-context-sample-fmt!
204238
ctx (if (avcodec-sample-fmts codec)
@@ -234,6 +268,38 @@
234268
ctx (av-get-channel-layout-nb-channels (avcodec-context-channel-layout ctx)))
235269
(set-avstream-time-base! stream (/ 1 (avcodec-context-sample-rate ctx)))]
236270
[('video 'write)
271+
;(define codec-context ctx)
272+
(define old-obj (queue-callback-data-codec-obj (codec-obj-callback-data obj)))
273+
(define codec-context (codec-obj-codec-context old-obj))
274+
(define packet data)
275+
;; Display output
276+
(with-handlers ([exn:ffmpeg:again? void]
277+
[exn:ffmpeg:eof? void])
278+
(avcodec-send-packet codec-context packet)
279+
(avcodec-receive-frame codec-context frame)
280+
(sws-scale sws
281+
(array-ptr (av-frame-data frame))
282+
(array-ptr (av-frame-linesize frame))
283+
0
284+
(avcodec-context-height codec-context)
285+
(array-ptr (av-frame-data frame-rgb))
286+
(array-ptr (av-frame-linesize frame-rgb)))
287+
(define linesize (array-ref (av-frame-linesize frame-rgb) 0))
288+
(send c draw-frame
289+
(λ ()
290+
(for ([i (in-range (avcodec-context-height codec-context))])
291+
(glTexSubImage2D
292+
GL_TEXTURE_2D
293+
0
294+
0
295+
i
296+
(avcodec-context-width codec-context)
297+
1
298+
GL_RGB
299+
GL_UNSIGNED_BYTE
300+
(ptr-add (array-ref (av-frame-data frame-rgb) 0)
301+
(* i linesize)))))))
302+
;; Write output
237303
data]
238304
#;
239305
[('audio 'write)
@@ -243,6 +309,7 @@
243309

244310
(define in-bundle (file->stream-bundle "/Users/leif/demo2.mp4"))
245311
(demux-stream in-bundle
312+
#:close-context? #f
246313
#:by-index-callback (queue-stream))
247314
(define out-bundle (bundle-for-file "/Users/leif/test.mp4"
248315
in-bundle))

0 commit comments

Comments
 (0)