4 * A pedagogical video player that really works! Now with seeking features.
6 * Code based on FFplay, Copyright (c) 2003 Fabrice Bellard, and a tutorial by
7 * Martin Bohme <boehme@inb.uni-luebeckREMOVETHIS.de>.
15 #include <libavcodec/avcodec.h>
16 #include <libavformat/avformat.h>
17 #include <libavformat/avio.h>
18 #include <libavutil/time.h>
19 #include <libavutil/avstring.h>
20 #include <libavutil/channel_layout.h>
21 #include <libswscale/swscale.h>
22 #include <libswresample/swresample.h>
25 #include <SDL_thread.h>
26 #include <SDL_video.h>
36 static bool has_direct_out
= false;
37 static bool has_latency_check
= false;
38 static LPALGETSOURCEDVSOFT alGetSourcedvSOFT
;
40 #define AUDIO_BUFFER_TIME 100 /* In milliseconds, per-buffer */
41 #define AUDIO_BUFFER_QUEUE_SIZE 8 /* Number of buffers to queue */
42 #define MAX_AUDIOQ_SIZE (5 * 16 * 1024) /* Bytes of compressed audio data to keep queued */
43 #define MAX_VIDEOQ_SIZE (5 * 256 * 1024) /* Bytes of compressed video data to keep queued */
44 #define AV_SYNC_THRESHOLD 0.01
45 #define AV_NOSYNC_THRESHOLD 10.0
46 #define SAMPLE_CORRECTION_MAX_DIFF 0.1
47 #define AUDIO_DIFF_AVG_NB 20
48 #define VIDEO_PICTURE_QUEUE_SIZE 16
51 FF_UPDATE_EVENT
= SDL_USEREVENT
,
57 typedef struct PacketQueue
{
58 AVPacketList
*first_pkt
, *last_pkt
;
59 volatile int nb_packets
;
61 volatile bool flushing
;
66 typedef struct VideoPicture
{
68 int width
, height
; /* Logical image size (actual size may be larger) */
69 volatile bool updated
;
73 typedef struct AudioState
{
79 /* Used for clock difference average computation */
82 double diff_threshold
;
84 /* Time (in seconds) of the next sample to be buffered */
87 /* Decompressed sample frame, and swresample context for conversion */
88 AVFrame
*decoded_aframe
;
89 struct SwrContext
*swres_ctx
;
91 /* Conversion format, for what gets fed to OpenAL */
93 enum AVSampleFormat dst_sample_fmt
;
95 /* Storage of converted samples */
97 ssize_t samples_len
; /* In samples */
106 ALuint buffer
[AUDIO_BUFFER_QUEUE_SIZE
];
113 typedef struct VideoState
{
120 double frame_last_pts
;
121 double frame_last_delay
;
123 /* time (av_gettime) at which we updated current_pts - used to have running video pts */
124 int64_t current_pts_time
;
126 /* Decompressed video frame, and swscale context for conversion */
127 AVFrame
*decoded_vframe
;
128 struct SwsContext
*swscale_ctx
;
130 VideoPicture pictq
[VIDEO_PICTURE_QUEUE_SIZE
];
131 int pictq_size
, pictq_rindex
, pictq_windex
;
138 typedef struct MovieState
{
139 AVFormatContext
*pFormatCtx
;
140 int videoStream
, audioStream
;
142 volatile bool seek_req
;
144 volatile bool direct_req
;
148 int64_t external_clock_base
;
153 althrd_t parse_thread
;
161 AV_SYNC_AUDIO_MASTER
,
162 AV_SYNC_VIDEO_MASTER
,
163 AV_SYNC_EXTERNAL_MASTER
,
165 DEFAULT_AV_SYNC_TYPE
= AV_SYNC_EXTERNAL_MASTER
168 static AVPacket flush_pkt
= { .data
= (uint8_t*)"FLUSH" };
170 static void packet_queue_init(PacketQueue
*q
)
172 memset(q
, 0, sizeof(PacketQueue
));
173 almtx_init(&q
->mutex
, almtx_plain
);
174 alcnd_init(&q
->cond
);
176 static int packet_queue_put(PacketQueue
*q
, AVPacket
*pkt
)
179 if(pkt
!= &flush_pkt
&& !pkt
->buf
&& av_dup_packet(pkt
) < 0)
182 pkt1
= av_malloc(sizeof(AVPacketList
));
187 almtx_lock(&q
->mutex
);
191 q
->last_pkt
->next
= pkt1
;
194 q
->size
+= pkt1
->pkt
.size
;
195 almtx_unlock(&q
->mutex
);
197 alcnd_signal(&q
->cond
);
200 static int packet_queue_get(PacketQueue
*q
, AVPacket
*pkt
, MovieState
*state
)
205 almtx_lock(&q
->mutex
);
211 q
->first_pkt
= pkt1
->next
;
215 q
->size
-= pkt1
->pkt
.size
;
227 alcnd_wait(&q
->cond
, &q
->mutex
);
229 almtx_unlock(&q
->mutex
);
232 static void packet_queue_clear(PacketQueue
*q
)
234 AVPacketList
*pkt
, *pkt1
;
236 almtx_lock(&q
->mutex
);
237 for(pkt
= q
->first_pkt
;pkt
!= NULL
;pkt
= pkt1
)
240 if(pkt
->pkt
.data
!= flush_pkt
.data
)
241 av_free_packet(&pkt
->pkt
);
248 almtx_unlock(&q
->mutex
);
250 static void packet_queue_flush(PacketQueue
*q
)
252 almtx_lock(&q
->mutex
);
254 almtx_unlock(&q
->mutex
);
255 alcnd_signal(&q
->cond
);
257 static void packet_queue_deinit(PacketQueue
*q
)
259 packet_queue_clear(q
);
260 alcnd_destroy(&q
->cond
);
261 almtx_destroy(&q
->mutex
);
265 static double get_audio_clock(AudioState
*state
)
269 almtx_lock(&state
->src_mutex
);
270 /* The audio clock is the timestamp of the sample currently being heard.
271 * It's based on 4 components:
272 * 1 - The timestamp of the next sample to buffer (state->current_pts)
273 * 2 - The length of the source's buffer queue (AL_SEC_LENGTH_SOFT)
274 * 3 - The offset OpenAL is currently at in the source (the first value
275 * from AL_SEC_OFFSET_LATENCY_SOFT)
276 * 4 - The latency between OpenAL and the DAC (the second value from
277 * AL_SEC_OFFSET_LATENCY_SOFT)
279 * Subtracting the length of the source queue from the next sample's
280 * timestamp gives the timestamp of the sample at start of the source
281 * queue. Adding the source offset to that results in the timestamp for
282 * OpenAL's current position, and subtracting the source latency from that
283 * gives the timestamp of the sample currently at the DAC.
285 pts
= state
->current_pts
;
288 ALdouble offset
[2] = { 0.0, 0.0 };
289 ALdouble queue_len
= 0.0;
292 /* NOTE: The source state must be checked last, in case an underrun
293 * occurs and the source stops between retrieving the offset+latency
294 * and getting the state. */
295 if(has_latency_check
)
297 alGetSourcedvSOFT(state
->source
, AL_SEC_OFFSET_LATENCY_SOFT
, offset
);
298 alGetSourcedvSOFT(state
->source
, AL_SEC_LENGTH_SOFT
, &queue_len
);
303 alGetSourcei(state
->source
, AL_SAMPLE_OFFSET
, &ioffset
);
304 alGetSourcei(state
->source
, AL_SAMPLE_LENGTH_SOFT
, &ilen
);
305 offset
[0] = (double)ioffset
/ state
->st
->codec
->sample_rate
;
306 queue_len
= (double)ilen
/ state
->st
->codec
->sample_rate
;
308 alGetSourcei(state
->source
, AL_SOURCE_STATE
, &status
);
310 /* If the source is AL_STOPPED, then there was an underrun and all
311 * buffers are processed, so ignore the source queue. The audio thread
312 * will put the source into an AL_INITIAL state and clear the queue
313 * when it starts recovery. */
314 if(status
!= AL_STOPPED
)
315 pts
= pts
- queue_len
+ offset
[0];
316 if(status
== AL_PLAYING
)
317 pts
= pts
- offset
[1];
319 almtx_unlock(&state
->src_mutex
);
321 return (pts
>= 0.0) ? pts
: 0.0;
323 static double get_video_clock(VideoState
*state
)
325 double delta
= (av_gettime() - state
->current_pts_time
) / 1000000.0;
326 return state
->current_pts
+ delta
;
328 static double get_external_clock(MovieState
*movState
)
330 return (av_gettime()-movState
->external_clock_base
) / 1000000.0;
333 double get_master_clock(MovieState
*movState
)
335 if(movState
->av_sync_type
== AV_SYNC_VIDEO_MASTER
)
336 return get_video_clock(&movState
->video
);
337 if(movState
->av_sync_type
== AV_SYNC_AUDIO_MASTER
)
338 return get_audio_clock(&movState
->audio
);
339 return get_external_clock(movState
);
342 /* Return how many samples to skip to maintain sync (negative means to
343 * duplicate samples). */
344 static int synchronize_audio(MovieState
*movState
)
346 double diff
, avg_diff
;
349 if(movState
->av_sync_type
== AV_SYNC_AUDIO_MASTER
)
352 ref_clock
= get_master_clock(movState
);
353 diff
= ref_clock
- get_audio_clock(&movState
->audio
);
355 if(!(fabs(diff
) < AV_NOSYNC_THRESHOLD
))
357 /* Difference is TOO big; reset diff stuff */
358 movState
->audio
.diff_accum
= 0.0;
362 /* Accumulate the diffs */
363 movState
->audio
.diff_accum
= movState
->audio
.diff_accum
*movState
->audio
.diff_avg_coef
+ diff
;
364 avg_diff
= movState
->audio
.diff_accum
*(1.0 - movState
->audio
.diff_avg_coef
);
365 if(fabs(avg_diff
) < movState
->audio
.diff_threshold
)
368 /* Constrain the per-update difference to avoid exceedingly large skips */
369 if(!(diff
<= SAMPLE_CORRECTION_MAX_DIFF
))
370 diff
= SAMPLE_CORRECTION_MAX_DIFF
;
371 else if(!(diff
>= -SAMPLE_CORRECTION_MAX_DIFF
))
372 diff
= -SAMPLE_CORRECTION_MAX_DIFF
;
373 return (int)(diff
*movState
->audio
.st
->codec
->sample_rate
);
376 static int audio_decode_frame(MovieState
*movState
)
378 AVPacket
*pkt
= &movState
->audio
.pkt
;
380 while(!movState
->quit
)
382 while(!movState
->quit
&& pkt
->size
== 0)
386 /* Get the next packet */
388 if((err
=packet_queue_get(&movState
->audio
.q
, pkt
, movState
)) <= 0)
394 if(pkt
->data
== flush_pkt
.data
)
396 avcodec_flush_buffers(movState
->audio
.st
->codec
);
397 movState
->audio
.diff_accum
= 0.0;
398 movState
->audio
.current_pts
= av_q2d(movState
->audio
.st
->time_base
)*pkt
->pts
;
400 alSourceRewind(movState
->audio
.source
);
401 alSourcei(movState
->audio
.source
, AL_BUFFER
, 0);
403 av_new_packet(pkt
, 0);
408 /* If provided, update w/ pts */
409 if(pkt
->pts
!= AV_NOPTS_VALUE
)
410 movState
->audio
.current_pts
= av_q2d(movState
->audio
.st
->time_base
)*pkt
->pts
;
413 AVFrame
*frame
= movState
->audio
.decoded_aframe
;
415 int len1
= avcodec_decode_audio4(movState
->audio
.st
->codec
, frame
,
419 if(len1
<= pkt
->size
)
421 /* Move the unread data to the front and clear the end bits */
422 int remaining
= pkt
->size
- len1
;
423 memmove(pkt
->data
, &pkt
->data
[len1
], remaining
);
424 av_shrink_packet(pkt
, remaining
);
427 if(!got_frame
|| frame
->nb_samples
<= 0)
429 av_frame_unref(frame
);
433 if(frame
->nb_samples
> movState
->audio
.samples_max
)
435 av_freep(&movState
->audio
.samples
);
437 &movState
->audio
.samples
, NULL
, movState
->audio
.st
->codec
->channels
,
438 frame
->nb_samples
, movState
->audio
.dst_sample_fmt
, 0
440 movState
->audio
.samples_max
= frame
->nb_samples
;
442 /* Return the amount of sample frames converted */
443 int data_size
= swr_convert(movState
->audio
.swres_ctx
,
444 &movState
->audio
.samples
, frame
->nb_samples
,
445 (const uint8_t**)frame
->data
, frame
->nb_samples
448 av_frame_unref(frame
);
455 static int read_audio(MovieState
*movState
, uint8_t *samples
, int length
)
457 int sample_skip
= synchronize_audio(movState
);
460 /* Read the next chunk of data, refill the buffer, and queue it
462 length
/= movState
->audio
.frame_size
;
463 while(audio_size
< length
)
465 if(movState
->audio
.samples_len
<= 0 || movState
->audio
.samples_pos
>= movState
->audio
.samples_len
)
467 int frame_len
= audio_decode_frame(movState
);
468 if(frame_len
< 0) return -1;
470 movState
->audio
.samples_len
= frame_len
;
471 if(movState
->audio
.samples_len
== 0)
474 movState
->audio
.samples_pos
= (movState
->audio
.samples_len
< sample_skip
) ?
475 movState
->audio
.samples_len
: sample_skip
;
476 sample_skip
-= movState
->audio
.samples_pos
;
478 movState
->audio
.current_pts
+= (double)movState
->audio
.samples_pos
/
479 (double)movState
->audio
.st
->codec
->sample_rate
;
483 int rem
= length
- audio_size
;
484 if(movState
->audio
.samples_pos
>= 0)
486 int n
= movState
->audio
.frame_size
;
487 int len
= movState
->audio
.samples_len
- movState
->audio
.samples_pos
;
488 if(rem
> len
) rem
= len
;
489 memcpy(samples
+ audio_size
*n
,
490 movState
->audio
.samples
+ movState
->audio
.samples_pos
*n
,
495 int n
= movState
->audio
.frame_size
;
496 int len
= -movState
->audio
.samples_pos
;
497 if(rem
> len
) rem
= len
;
499 /* Add samples by copying the first sample */
502 uint8_t sample
= ((uint8_t*)movState
->audio
.samples
)[0];
503 uint8_t *q
= (uint8_t*)samples
+ audio_size
;
504 for(int i
= 0;i
< rem
;i
++)
509 uint16_t sample
= ((uint16_t*)movState
->audio
.samples
)[0];
510 uint16_t *q
= (uint16_t*)samples
+ audio_size
;
511 for(int i
= 0;i
< rem
;i
++)
516 uint32_t sample
= ((uint32_t*)movState
->audio
.samples
)[0];
517 uint32_t *q
= (uint32_t*)samples
+ audio_size
;
518 for(int i
= 0;i
< rem
;i
++)
523 uint64_t sample
= ((uint64_t*)movState
->audio
.samples
)[0];
524 uint64_t *q
= (uint64_t*)samples
+ audio_size
;
525 for(int i
= 0;i
< rem
;i
++)
530 uint8_t *sample
= movState
->audio
.samples
;
531 uint8_t *q
= samples
+ audio_size
*n
;
532 for(int i
= 0;i
< rem
;i
++)
534 memcpy(q
, sample
, n
);
540 movState
->audio
.samples_pos
+= rem
;
541 movState
->audio
.current_pts
+= (double)rem
/ movState
->audio
.st
->codec
->sample_rate
;
545 return audio_size
* movState
->audio
.frame_size
;
548 static int audio_thread(void *userdata
)
550 MovieState
*movState
= (MovieState
*)userdata
;
551 uint8_t *samples
= NULL
;
555 alGenBuffers(AUDIO_BUFFER_QUEUE_SIZE
, movState
->audio
.buffer
);
556 alGenSources(1, &movState
->audio
.source
);
558 alSourcei(movState
->audio
.source
, AL_SOURCE_RELATIVE
, AL_TRUE
);
559 alSourcei(movState
->audio
.source
, AL_ROLLOFF_FACTOR
, 0);
561 av_new_packet(&movState
->audio
.pkt
, 0);
563 /* Find a suitable format for OpenAL. */
564 movState
->audio
.format
= AL_NONE
;
565 if(movState
->audio
.st
->codec
->sample_fmt
== AV_SAMPLE_FMT_U8
||
566 movState
->audio
.st
->codec
->sample_fmt
== AV_SAMPLE_FMT_U8P
)
568 movState
->audio
.dst_sample_fmt
= AV_SAMPLE_FMT_U8
;
569 movState
->audio
.frame_size
= 1;
570 if(movState
->audio
.st
->codec
->channel_layout
== AV_CH_LAYOUT_7POINT1
&&
571 alIsExtensionPresent("AL_EXT_MCFORMATS") &&
572 (fmt
=alGetEnumValue("AL_FORMAT_71CHN8")) != AL_NONE
&& fmt
!= -1)
574 movState
->audio
.dst_ch_layout
= movState
->audio
.st
->codec
->channel_layout
;
575 movState
->audio
.frame_size
*= 8;
576 movState
->audio
.format
= fmt
;
578 if((movState
->audio
.st
->codec
->channel_layout
== AV_CH_LAYOUT_5POINT1
||
579 movState
->audio
.st
->codec
->channel_layout
== AV_CH_LAYOUT_5POINT1_BACK
) &&
580 alIsExtensionPresent("AL_EXT_MCFORMATS") &&
581 (fmt
=alGetEnumValue("AL_FORMAT_51CHN8")) != AL_NONE
&& fmt
!= -1)
583 movState
->audio
.dst_ch_layout
= movState
->audio
.st
->codec
->channel_layout
;
584 movState
->audio
.frame_size
*= 6;
585 movState
->audio
.format
= fmt
;
587 if(movState
->audio
.st
->codec
->channel_layout
== AV_CH_LAYOUT_MONO
)
589 movState
->audio
.dst_ch_layout
= AV_CH_LAYOUT_MONO
;
590 movState
->audio
.frame_size
*= 1;
591 movState
->audio
.format
= AL_FORMAT_MONO8
;
593 if(movState
->audio
.format
== AL_NONE
)
595 movState
->audio
.dst_ch_layout
= AV_CH_LAYOUT_STEREO
;
596 movState
->audio
.frame_size
*= 2;
597 movState
->audio
.format
= AL_FORMAT_STEREO8
;
600 if((movState
->audio
.st
->codec
->sample_fmt
== AV_SAMPLE_FMT_FLT
||
601 movState
->audio
.st
->codec
->sample_fmt
== AV_SAMPLE_FMT_FLTP
) &&
602 alIsExtensionPresent("AL_EXT_FLOAT32"))
604 movState
->audio
.dst_sample_fmt
= AV_SAMPLE_FMT_FLT
;
605 movState
->audio
.frame_size
= 4;
606 if(movState
->audio
.st
->codec
->channel_layout
== AV_CH_LAYOUT_7POINT1
&&
607 alIsExtensionPresent("AL_EXT_MCFORMATS") &&
608 (fmt
=alGetEnumValue("AL_FORMAT_71CHN32")) != AL_NONE
&& fmt
!= -1)
610 movState
->audio
.dst_ch_layout
= movState
->audio
.st
->codec
->channel_layout
;
611 movState
->audio
.frame_size
*= 8;
612 movState
->audio
.format
= fmt
;
614 if((movState
->audio
.st
->codec
->channel_layout
== AV_CH_LAYOUT_5POINT1
||
615 movState
->audio
.st
->codec
->channel_layout
== AV_CH_LAYOUT_5POINT1_BACK
) &&
616 alIsExtensionPresent("AL_EXT_MCFORMATS") &&
617 (fmt
=alGetEnumValue("AL_FORMAT_51CHN32")) != AL_NONE
&& fmt
!= -1)
619 movState
->audio
.dst_ch_layout
= movState
->audio
.st
->codec
->channel_layout
;
620 movState
->audio
.frame_size
*= 6;
621 movState
->audio
.format
= fmt
;
623 if(movState
->audio
.st
->codec
->channel_layout
== AV_CH_LAYOUT_MONO
)
625 movState
->audio
.dst_ch_layout
= AV_CH_LAYOUT_MONO
;
626 movState
->audio
.frame_size
*= 1;
627 movState
->audio
.format
= AL_FORMAT_MONO_FLOAT32
;
629 if(movState
->audio
.format
== AL_NONE
)
631 movState
->audio
.dst_ch_layout
= AV_CH_LAYOUT_STEREO
;
632 movState
->audio
.frame_size
*= 2;
633 movState
->audio
.format
= AL_FORMAT_STEREO_FLOAT32
;
636 if(movState
->audio
.format
== AL_NONE
)
638 movState
->audio
.dst_sample_fmt
= AV_SAMPLE_FMT_S16
;
639 movState
->audio
.frame_size
= 2;
640 if(movState
->audio
.st
->codec
->channel_layout
== AV_CH_LAYOUT_7POINT1
&&
641 alIsExtensionPresent("AL_EXT_MCFORMATS") &&
642 (fmt
=alGetEnumValue("AL_FORMAT_71CHN16")) != AL_NONE
&& fmt
!= -1)
644 movState
->audio
.dst_ch_layout
= movState
->audio
.st
->codec
->channel_layout
;
645 movState
->audio
.frame_size
*= 8;
646 movState
->audio
.format
= fmt
;
648 if((movState
->audio
.st
->codec
->channel_layout
== AV_CH_LAYOUT_5POINT1
||
649 movState
->audio
.st
->codec
->channel_layout
== AV_CH_LAYOUT_5POINT1_BACK
) &&
650 alIsExtensionPresent("AL_EXT_MCFORMATS") &&
651 (fmt
=alGetEnumValue("AL_FORMAT_51CHN16")) != AL_NONE
&& fmt
!= -1)
653 movState
->audio
.dst_ch_layout
= movState
->audio
.st
->codec
->channel_layout
;
654 movState
->audio
.frame_size
*= 6;
655 movState
->audio
.format
= fmt
;
657 if(movState
->audio
.st
->codec
->channel_layout
== AV_CH_LAYOUT_MONO
)
659 movState
->audio
.dst_ch_layout
= AV_CH_LAYOUT_MONO
;
660 movState
->audio
.frame_size
*= 1;
661 movState
->audio
.format
= AL_FORMAT_MONO16
;
663 if(movState
->audio
.format
== AL_NONE
)
665 movState
->audio
.dst_ch_layout
= AV_CH_LAYOUT_STEREO
;
666 movState
->audio
.frame_size
*= 2;
667 movState
->audio
.format
= AL_FORMAT_STEREO16
;
670 buffer_len
= AUDIO_BUFFER_TIME
* movState
->audio
.st
->codec
->sample_rate
/ 1000 *
671 movState
->audio
.frame_size
;
672 samples
= av_malloc(buffer_len
);
674 movState
->audio
.samples
= NULL
;
675 movState
->audio
.samples_max
= 0;
676 movState
->audio
.samples_pos
= 0;
677 movState
->audio
.samples_len
= 0;
679 if(!(movState
->audio
.decoded_aframe
=av_frame_alloc()))
681 fprintf(stderr
, "Failed to allocate audio frame\n");
685 movState
->audio
.swres_ctx
= swr_alloc_set_opts(NULL
,
686 movState
->audio
.dst_ch_layout
,
687 movState
->audio
.dst_sample_fmt
,
688 movState
->audio
.st
->codec
->sample_rate
,
689 movState
->audio
.st
->codec
->channel_layout
?
690 movState
->audio
.st
->codec
->channel_layout
:
691 (uint64_t)av_get_default_channel_layout(movState
->audio
.st
->codec
->channels
),
692 movState
->audio
.st
->codec
->sample_fmt
,
693 movState
->audio
.st
->codec
->sample_rate
,
696 if(!movState
->audio
.swres_ctx
|| swr_init(movState
->audio
.swres_ctx
) != 0)
698 fprintf(stderr
, "Failed to initialize audio converter\n");
702 almtx_lock(&movState
->audio
.src_mutex
);
703 while(alGetError() == AL_NO_ERROR
&& !movState
->quit
)
705 /* First remove any processed buffers. */
707 alGetSourcei(movState
->audio
.source
, AL_BUFFERS_PROCESSED
, &processed
);
708 alSourceUnqueueBuffers(movState
->audio
.source
, processed
, (ALuint
[AUDIO_BUFFER_QUEUE_SIZE
]){});
710 /* Refill the buffer queue. */
712 alGetSourcei(movState
->audio
.source
, AL_BUFFERS_QUEUED
, &queued
);
713 while(queued
< AUDIO_BUFFER_QUEUE_SIZE
)
717 /* Read the next chunk of data, fill the buffer, and queue it on
719 audio_size
= read_audio(movState
, samples
, buffer_len
);
720 if(audio_size
< 0) break;
722 ALuint bufid
= movState
->audio
.buffer
[movState
->audio
.buffer_idx
++];
723 movState
->audio
.buffer_idx
%= AUDIO_BUFFER_QUEUE_SIZE
;
725 alBufferData(bufid
, movState
->audio
.format
, samples
, audio_size
,
726 movState
->audio
.st
->codec
->sample_rate
);
727 alSourceQueueBuffers(movState
->audio
.source
, 1, &bufid
);
731 /* Check that the source is playing. */
733 alGetSourcei(movState
->audio
.source
, AL_SOURCE_STATE
, &state
);
734 if(state
== AL_STOPPED
)
736 /* AL_STOPPED means there was an underrun. Double-check that all
737 * processed buffers are removed, then rewind the source to get it
738 * back into an AL_INITIAL state. */
739 alGetSourcei(movState
->audio
.source
, AL_BUFFERS_PROCESSED
, &processed
);
740 alSourceUnqueueBuffers(movState
->audio
.source
, processed
, (ALuint
[AUDIO_BUFFER_QUEUE_SIZE
]){});
741 alSourceRewind(movState
->audio
.source
);
745 almtx_unlock(&movState
->audio
.src_mutex
);
747 /* (re)start the source if needed, and wait for a buffer to finish */
748 if(state
!= AL_PLAYING
&& state
!= AL_PAUSED
)
750 alGetSourcei(movState
->audio
.source
, AL_BUFFERS_QUEUED
, &queued
);
751 if(queued
> 0) alSourcePlay(movState
->audio
.source
);
753 SDL_Delay(AUDIO_BUFFER_TIME
);
755 if(movState
->direct_req
)
759 alGetSourcei(movState
->audio
.source
, AL_DIRECT_CHANNELS_SOFT
, &state
);
761 alSourcei(movState
->audio
.source
, AL_DIRECT_CHANNELS_SOFT
,
762 state
? AL_TRUE
: AL_FALSE
);
763 printf("Direct channels %s\n", state
? "on" : "off");
766 movState
->direct_req
= false;
769 almtx_lock(&movState
->audio
.src_mutex
);
771 almtx_unlock(&movState
->audio
.src_mutex
);
774 av_frame_free(&movState
->audio
.decoded_aframe
);
775 swr_free(&movState
->audio
.swres_ctx
);
778 av_freep(&movState
->audio
.samples
);
780 alDeleteSources(1, &movState
->audio
.source
);
781 alDeleteBuffers(AUDIO_BUFFER_QUEUE_SIZE
, movState
->audio
.buffer
);
787 static Uint32
sdl_refresh_timer_cb(Uint32 interval
, void *opaque
)
791 SDL_PushEvent(&(SDL_Event
){ .user
={.type
=FF_REFRESH_EVENT
, .data1
=opaque
} });
792 return 0; /* 0 means stop timer */
795 /* Schedule a video refresh in 'delay' ms */
796 static void schedule_refresh(MovieState
*movState
, int delay
)
798 SDL_AddTimer(delay
, sdl_refresh_timer_cb
, movState
);
801 static void video_display(MovieState
*movState
, SDL_Window
*screen
, SDL_Renderer
*renderer
)
803 VideoPicture
*vp
= &movState
->video
.pictq
[movState
->video
.pictq_rindex
];
812 if(movState
->video
.st
->codec
->sample_aspect_ratio
.num
== 0)
816 aspect_ratio
= av_q2d(movState
->video
.st
->codec
->sample_aspect_ratio
) *
817 movState
->video
.st
->codec
->width
/
818 movState
->video
.st
->codec
->height
;
820 if(aspect_ratio
<= 0.0f
)
822 aspect_ratio
= (float)movState
->video
.st
->codec
->width
/
823 (float)movState
->video
.st
->codec
->height
;
826 SDL_GetWindowSize(screen
, &win_w
, &win_h
);
828 w
= ((int)rint(h
* aspect_ratio
) + 3) & ~3;
832 h
= ((int)rint(w
/ aspect_ratio
) + 3) & ~3;
837 SDL_RenderCopy(renderer
, vp
->bmp
,
838 &(SDL_Rect
){ .x
=0, .y
=0, .w
=vp
->width
, .h
=vp
->height
},
839 &(SDL_Rect
){ .x
=x
, .y
=y
, .w
=w
, .h
=h
}
841 SDL_RenderPresent(renderer
);
844 static void video_refresh_timer(MovieState
*movState
, SDL_Window
*screen
, SDL_Renderer
*renderer
)
846 if(!movState
->video
.st
)
848 schedule_refresh(movState
, 100);
852 almtx_lock(&movState
->video
.pictq_mutex
);
854 if(movState
->video
.pictq_size
== 0)
855 schedule_refresh(movState
, 1);
858 VideoPicture
*vp
= &movState
->video
.pictq
[movState
->video
.pictq_rindex
];
859 double actual_delay
, delay
, sync_threshold
, ref_clock
, diff
;
861 movState
->video
.current_pts
= vp
->pts
;
862 movState
->video
.current_pts_time
= av_gettime();
864 delay
= vp
->pts
- movState
->video
.frame_last_pts
; /* the pts from last time */
865 if(delay
<= 0 || delay
>= 1.0)
867 /* if incorrect delay, use previous one */
868 delay
= movState
->video
.frame_last_delay
;
870 /* save for next time */
871 movState
->video
.frame_last_delay
= delay
;
872 movState
->video
.frame_last_pts
= vp
->pts
;
874 /* Update delay to sync to clock if not master source. */
875 if(movState
->av_sync_type
!= AV_SYNC_VIDEO_MASTER
)
877 ref_clock
= get_master_clock(movState
);
878 diff
= vp
->pts
- ref_clock
;
880 /* Skip or repeat the frame. Take delay into account. */
881 sync_threshold
= (delay
> AV_SYNC_THRESHOLD
) ? delay
: AV_SYNC_THRESHOLD
;
882 if(fabs(diff
) < AV_NOSYNC_THRESHOLD
)
884 if(diff
<= -sync_threshold
)
886 else if(diff
>= sync_threshold
)
891 movState
->video
.frame_timer
+= delay
;
892 /* Compute the REAL delay. */
893 actual_delay
= movState
->video
.frame_timer
- (av_gettime() / 1000000.0);
894 if(!(actual_delay
>= 0.010))
896 /* We don't have time to handle this picture, just skip to the next one. */
897 movState
->video
.pictq_rindex
= (movState
->video
.pictq_rindex
+1)%VIDEO_PICTURE_QUEUE_SIZE
;
898 movState
->video
.pictq_size
--;
899 alcnd_signal(&movState
->video
.pictq_cond
);
902 schedule_refresh(movState
, (int)(actual_delay
*1000.0 + 0.5));
904 /* Show the picture! */
905 video_display(movState
, screen
, renderer
);
907 /* Update queue for next picture. */
908 movState
->video
.pictq_rindex
= (movState
->video
.pictq_rindex
+1)%VIDEO_PICTURE_QUEUE_SIZE
;
909 movState
->video
.pictq_size
--;
910 alcnd_signal(&movState
->video
.pictq_cond
);
912 almtx_unlock(&movState
->video
.pictq_mutex
);
916 static void update_picture(MovieState
*movState
, bool *first_update
, SDL_Window
*screen
, SDL_Renderer
*renderer
)
918 VideoPicture
*vp
= &movState
->video
.pictq
[movState
->video
.pictq_windex
];
920 /* allocate or resize the buffer! */
921 if(!vp
->bmp
|| vp
->width
!= movState
->video
.st
->codec
->width
||
922 vp
->height
!= movState
->video
.st
->codec
->height
)
925 SDL_DestroyTexture(vp
->bmp
);
926 vp
->bmp
= SDL_CreateTexture(
927 renderer
, SDL_PIXELFORMAT_YV12
, SDL_TEXTUREACCESS_STREAMING
,
928 movState
->video
.st
->codec
->coded_width
, movState
->video
.st
->codec
->coded_height
931 fprintf(stderr
, "Failed to create YV12 texture!\n");
932 vp
->width
= movState
->video
.st
->codec
->width
;
933 vp
->height
= movState
->video
.st
->codec
->height
;
935 if(*first_update
&& vp
->width
> 0 && vp
->height
> 0)
937 /* For the first update, set the window size to the video size. */
938 *first_update
= false;
942 if(movState
->video
.st
->codec
->sample_aspect_ratio
.num
!= 0 &&
943 movState
->video
.st
->codec
->sample_aspect_ratio
.den
!= 0)
945 double aspect_ratio
= av_q2d(movState
->video
.st
->codec
->sample_aspect_ratio
);
946 if(aspect_ratio
>= 1.0)
947 w
= (int)(w
*aspect_ratio
+ 0.5);
948 else if(aspect_ratio
> 0.0)
949 h
= (int)(h
/aspect_ratio
+ 0.5);
951 SDL_SetWindowSize(screen
, w
, h
);
957 AVFrame
*frame
= movState
->video
.decoded_vframe
;
961 if(movState
->video
.st
->codec
->pix_fmt
== AV_PIX_FMT_YUV420P
)
962 SDL_UpdateYUVTexture(vp
->bmp
, NULL
,
963 frame
->data
[0], frame
->linesize
[0],
964 frame
->data
[1], frame
->linesize
[1],
965 frame
->data
[2], frame
->linesize
[2]
967 else if(SDL_LockTexture(vp
->bmp
, NULL
, &pixels
, &pitch
) != 0)
968 fprintf(stderr
, "Failed to lock texture\n");
971 // Convert the image into YUV format that SDL uses
972 int coded_w
= movState
->video
.st
->codec
->coded_width
;
973 int coded_h
= movState
->video
.st
->codec
->coded_height
;
974 int w
= movState
->video
.st
->codec
->width
;
975 int h
= movState
->video
.st
->codec
->height
;
976 if(!movState
->video
.swscale_ctx
)
977 movState
->video
.swscale_ctx
= sws_getContext(
978 w
, h
, movState
->video
.st
->codec
->pix_fmt
,
979 w
, h
, AV_PIX_FMT_YUV420P
, SWS_X
, NULL
, NULL
, NULL
982 /* point pict at the queue */
984 pict
.data
[0] = pixels
;
985 pict
.data
[2] = pict
.data
[0] + coded_w
*coded_h
;
986 pict
.data
[1] = pict
.data
[2] + coded_w
*coded_h
/4;
988 pict
.linesize
[0] = pitch
;
989 pict
.linesize
[2] = pitch
/ 2;
990 pict
.linesize
[1] = pitch
/ 2;
992 sws_scale(movState
->video
.swscale_ctx
, (const uint8_t**)frame
->data
,
993 frame
->linesize
, 0, h
, pict
.data
, pict
.linesize
);
994 SDL_UnlockTexture(vp
->bmp
);
998 almtx_lock(&movState
->video
.pictq_mutex
);
1000 almtx_unlock(&movState
->video
.pictq_mutex
);
1001 alcnd_signal(&movState
->video
.pictq_cond
);
1004 static int queue_picture(MovieState
*movState
, double pts
)
1006 /* Wait until we have space for a new pic */
1007 almtx_lock(&movState
->video
.pictq_mutex
);
1008 while(movState
->video
.pictq_size
>= VIDEO_PICTURE_QUEUE_SIZE
&& !movState
->quit
)
1009 alcnd_wait(&movState
->video
.pictq_cond
, &movState
->video
.pictq_mutex
);
1010 almtx_unlock(&movState
->video
.pictq_mutex
);
1015 VideoPicture
*vp
= &movState
->video
.pictq
[movState
->video
.pictq_windex
];
1017 /* We have to create/update the picture in the main thread */
1018 vp
->updated
= false;
1019 SDL_PushEvent(&(SDL_Event
){ .user
={.type
=FF_UPDATE_EVENT
, .data1
=movState
} });
1021 /* Wait until the picture is updated. */
1022 almtx_lock(&movState
->video
.pictq_mutex
);
1023 while(!vp
->updated
&& !movState
->quit
)
1024 alcnd_wait(&movState
->video
.pictq_cond
, &movState
->video
.pictq_mutex
);
1025 almtx_unlock(&movState
->video
.pictq_mutex
);
1030 movState
->video
.pictq_windex
= (movState
->video
.pictq_windex
+1)%VIDEO_PICTURE_QUEUE_SIZE
;
1031 almtx_lock(&movState
->video
.pictq_mutex
);
1032 movState
->video
.pictq_size
++;
1033 almtx_unlock(&movState
->video
.pictq_mutex
);
1038 static double synchronize_video(MovieState
*movState
, double pts
)
1042 if(pts
== 0.0) /* if we aren't given a pts, set it to the clock */
1043 pts
= movState
->video
.clock
;
1044 else /* if we have pts, set video clock to it */
1045 movState
->video
.clock
= pts
;
1047 /* update the video clock */
1048 frame_delay
= av_q2d(movState
->video
.st
->codec
->time_base
);
1049 /* if we are repeating a frame, adjust clock accordingly */
1050 frame_delay
+= movState
->video
.decoded_vframe
->repeat_pict
* (frame_delay
* 0.5);
1051 movState
->video
.clock
+= frame_delay
;
1055 int video_thread(void *arg
)
1057 MovieState
*movState
= (MovieState
*)arg
;
1058 AVPacket
*packet
= (AVPacket
[1]){};
1059 int64_t saved_pts
, pkt_pts
;
1062 movState
->video
.decoded_vframe
= av_frame_alloc();
1063 while(packet_queue_get(&movState
->video
.q
, packet
, movState
) >= 0)
1065 if(packet
->data
== flush_pkt
.data
)
1067 avcodec_flush_buffers(movState
->video
.st
->codec
);
1069 almtx_lock(&movState
->video
.pictq_mutex
);
1070 movState
->video
.pictq_size
= 0;
1071 movState
->video
.pictq_rindex
= 0;
1072 movState
->video
.pictq_windex
= 0;
1073 almtx_unlock(&movState
->video
.pictq_mutex
);
1075 movState
->video
.clock
= av_q2d(movState
->video
.st
->time_base
)*packet
->pts
;
1076 movState
->video
.current_pts
= movState
->video
.clock
;
1077 movState
->video
.current_pts_time
= av_gettime();
1081 pkt_pts
= packet
->pts
;
1083 /* Decode video frame */
1084 avcodec_decode_video2(movState
->video
.st
->codec
, movState
->video
.decoded_vframe
,
1085 &frameFinished
, packet
);
1086 if(pkt_pts
!= AV_NOPTS_VALUE
&& !movState
->video
.decoded_vframe
->opaque
)
1088 /* Store the packet's original pts in the frame, in case the frame
1089 * is not finished decoding yet. */
1090 saved_pts
= pkt_pts
;
1091 movState
->video
.decoded_vframe
->opaque
= &saved_pts
;
1094 av_free_packet(packet
);
1098 double pts
= av_q2d(movState
->video
.st
->time_base
);
1099 if(packet
->dts
!= AV_NOPTS_VALUE
)
1101 else if(movState
->video
.decoded_vframe
->opaque
)
1102 pts
*= *(int64_t*)movState
->video
.decoded_vframe
->opaque
;
1105 movState
->video
.decoded_vframe
->opaque
= NULL
;
1107 pts
= synchronize_video(movState
, pts
);
1108 if(queue_picture(movState
, pts
) < 0)
1113 sws_freeContext(movState
->video
.swscale_ctx
);
1114 movState
->video
.swscale_ctx
= NULL
;
1115 av_frame_free(&movState
->video
.decoded_vframe
);
1120 static int stream_component_open(MovieState
*movState
, int stream_index
)
1122 AVFormatContext
*pFormatCtx
= movState
->pFormatCtx
;
1123 AVCodecContext
*codecCtx
;
1126 if(stream_index
< 0 || (unsigned int)stream_index
>= pFormatCtx
->nb_streams
)
1129 /* Get a pointer to the codec context for the video stream, and open the
1130 * associated codec */
1131 codecCtx
= pFormatCtx
->streams
[stream_index
]->codec
;
1133 codec
= avcodec_find_decoder(codecCtx
->codec_id
);
1134 if(!codec
|| avcodec_open2(codecCtx
, codec
, NULL
) < 0)
1136 fprintf(stderr
, "Unsupported codec!\n");
1140 /* Initialize and start the media type handler */
1141 switch(codecCtx
->codec_type
)
1143 case AVMEDIA_TYPE_AUDIO
:
1144 movState
->audioStream
= stream_index
;
1145 movState
->audio
.st
= pFormatCtx
->streams
[stream_index
];
1147 /* Averaging filter for audio sync */
1148 movState
->audio
.diff_avg_coef
= exp(log(0.01) / AUDIO_DIFF_AVG_NB
);
1149 /* Correct audio only if larger error than this */
1150 movState
->audio
.diff_threshold
= 2.0 * 0.050/* 50 ms */;
1152 memset(&movState
->audio
.pkt
, 0, sizeof(movState
->audio
.pkt
));
1153 if(althrd_create(&movState
->audio
.thread
, audio_thread
, movState
) != althrd_success
)
1155 movState
->audioStream
= -1;
1156 movState
->audio
.st
= NULL
;
1160 case AVMEDIA_TYPE_VIDEO
:
1161 movState
->videoStream
= stream_index
;
1162 movState
->video
.st
= pFormatCtx
->streams
[stream_index
];
1164 movState
->video
.current_pts_time
= av_gettime();
1165 movState
->video
.frame_timer
= (double)movState
->video
.current_pts_time
/
1167 movState
->video
.frame_last_delay
= 40e-3;
1169 if(althrd_create(&movState
->video
.thread
, video_thread
, movState
) != althrd_success
)
1171 movState
->videoStream
= -1;
1172 movState
->video
.st
= NULL
;
1183 static int decode_interrupt_cb(void *ctx
)
1185 return ((MovieState
*)ctx
)->quit
;
1188 int decode_thread(void *arg
)
1190 MovieState
*movState
= (MovieState
*)arg
;
1191 AVFormatContext
*fmtCtx
= movState
->pFormatCtx
;
1192 AVPacket
*packet
= (AVPacket
[1]){};
1193 int video_index
= -1;
1194 int audio_index
= -1;
1196 movState
->videoStream
= -1;
1197 movState
->audioStream
= -1;
1199 /* Dump information about file onto standard error */
1200 av_dump_format(fmtCtx
, 0, movState
->filename
, 0);
1202 /* Find the first video and audio streams */
1203 for(unsigned int i
= 0;i
< fmtCtx
->nb_streams
;i
++)
1205 if(fmtCtx
->streams
[i
]->codec
->codec_type
== AVMEDIA_TYPE_VIDEO
&& video_index
< 0)
1207 else if(fmtCtx
->streams
[i
]->codec
->codec_type
== AVMEDIA_TYPE_AUDIO
&& audio_index
< 0)
1210 movState
->external_clock_base
= av_gettime();
1211 if(audio_index
>= 0)
1212 stream_component_open(movState
, audio_index
);
1213 if(video_index
>= 0)
1214 stream_component_open(movState
, video_index
);
1216 if(movState
->videoStream
< 0 && movState
->audioStream
< 0)
1218 fprintf(stderr
, "%s: could not open codecs\n", movState
->filename
);
1222 /* Main packet handling loop */
1223 while(!movState
->quit
)
1225 if(movState
->seek_req
)
1227 int64_t seek_target
= movState
->seek_pos
;
1228 int stream_index
= -1;
1230 /* Prefer seeking on the video stream. */
1231 if(movState
->videoStream
>= 0)
1232 stream_index
= movState
->videoStream
;
1233 else if(movState
->audioStream
>= 0)
1234 stream_index
= movState
->audioStream
;
1236 /* Get a seek timestamp for the appropriate stream. */
1237 int64_t timestamp
= seek_target
;
1238 if(stream_index
>= 0)
1239 timestamp
= av_rescale_q(seek_target
, AV_TIME_BASE_Q
, fmtCtx
->streams
[stream_index
]->time_base
);
1241 if(av_seek_frame(movState
->pFormatCtx
, stream_index
, timestamp
, 0) < 0)
1242 fprintf(stderr
, "%s: error while seeking\n", movState
->pFormatCtx
->filename
);
1245 /* Seek successful, clear the packet queues and send a special
1246 * 'flush' packet with the new stream clock time. */
1247 if(movState
->audioStream
>= 0)
1249 packet_queue_clear(&movState
->audio
.q
);
1250 flush_pkt
.pts
= av_rescale_q(seek_target
, AV_TIME_BASE_Q
,
1251 fmtCtx
->streams
[movState
->audioStream
]->time_base
1253 packet_queue_put(&movState
->audio
.q
, &flush_pkt
);
1255 if(movState
->videoStream
>= 0)
1257 packet_queue_clear(&movState
->video
.q
);
1258 flush_pkt
.pts
= av_rescale_q(seek_target
, AV_TIME_BASE_Q
,
1259 fmtCtx
->streams
[movState
->videoStream
]->time_base
1261 packet_queue_put(&movState
->video
.q
, &flush_pkt
);
1263 movState
->external_clock_base
= av_gettime() - seek_target
;
1265 movState
->seek_req
= false;
1268 if(movState
->audio
.q
.size
>= MAX_AUDIOQ_SIZE
||
1269 movState
->video
.q
.size
>= MAX_VIDEOQ_SIZE
)
1275 if(av_read_frame(movState
->pFormatCtx
, packet
) < 0)
1277 packet_queue_flush(&movState
->video
.q
);
1278 packet_queue_flush(&movState
->audio
.q
);
1282 /* Place the packet in the queue it's meant for, or discard it. */
1283 if(packet
->stream_index
== movState
->videoStream
)
1284 packet_queue_put(&movState
->video
.q
, packet
);
1285 else if(packet
->stream_index
== movState
->audioStream
)
1286 packet_queue_put(&movState
->audio
.q
, packet
);
1288 av_free_packet(packet
);
1291 /* all done - wait for it */
1292 while(!movState
->quit
)
1294 if(movState
->audio
.q
.nb_packets
== 0 && movState
->video
.q
.nb_packets
== 0)
1300 movState
->quit
= true;
1301 packet_queue_flush(&movState
->video
.q
);
1302 packet_queue_flush(&movState
->audio
.q
);
1304 if(movState
->videoStream
>= 0)
1305 althrd_join(movState
->video
.thread
, NULL
);
1306 if(movState
->audioStream
>= 0)
1307 althrd_join(movState
->audio
.thread
, NULL
);
1309 SDL_PushEvent(&(SDL_Event
){ .user
={.type
=FF_QUIT_EVENT
, .data1
=movState
} });
1315 static void stream_seek(MovieState
*movState
, double incr
)
1317 if(!movState
->seek_req
)
1319 double newtime
= get_master_clock(movState
)+incr
;
1320 if(newtime
<= 0.0) movState
->seek_pos
= 0;
1321 else movState
->seek_pos
= (int64_t)(newtime
* AV_TIME_BASE
);
1322 movState
->seek_req
= true;
1326 int main(int argc
, char *argv
[])
1329 MovieState
*movState
;
1330 bool first_update
= true;
1332 SDL_Renderer
*renderer
;
1334 ALCcontext
*context
;
1339 fprintf(stderr
, "Usage: %s <file>\n", argv
[0]);
1342 /* Register all formats and codecs */
1344 /* Initialize networking protocols */
1345 avformat_network_init();
1347 if(SDL_Init(SDL_INIT_VIDEO
| SDL_INIT_TIMER
))
1349 fprintf(stderr
, "Could not initialize SDL - %s\n", SDL_GetError());
1353 /* Make a window to put our video */
1354 screen
= SDL_CreateWindow("alffplay", 0, 0, 640, 480, SDL_WINDOW_RESIZABLE
);
1357 fprintf(stderr
, "SDL: could not set video mode - exiting\n");
1360 /* Make a renderer to handle the texture image surface and rendering. */
1361 renderer
= SDL_CreateRenderer(screen
, -1, SDL_RENDERER_ACCELERATED
);
1364 SDL_RendererInfo rinf
;
1367 /* Make sure the renderer supports YV12 textures. If not, fallback to a
1368 * software renderer. */
1369 if(SDL_GetRendererInfo(renderer
, &rinf
) == 0)
1371 for(Uint32 i
= 0;!ok
&& i
< rinf
.num_texture_formats
;i
++)
1372 ok
= (rinf
.texture_formats
[i
] == SDL_PIXELFORMAT_YV12
);
1376 fprintf(stderr
, "YV12 pixelformat textures not supported on renderer %s\n", rinf
.name
);
1377 SDL_DestroyRenderer(renderer
);
1382 renderer
= SDL_CreateRenderer(screen
, -1, SDL_RENDERER_SOFTWARE
);
1385 fprintf(stderr
, "SDL: could not create renderer - exiting\n");
1388 SDL_SetRenderDrawColor(renderer
, 0, 0, 0, 255);
1389 SDL_RenderFillRect(renderer
, NULL
);
1390 SDL_RenderPresent(renderer
);
1392 /* Open an audio device */
1395 if(argc
> 3 && strcmp(argv
[1], "-device") == 0)
1398 device
= alcOpenDevice(argv
[2]);
1400 fprintf(stderr
, "OpenAL: could not open \"%s\" - trying default\n", argv
[2]);
1403 device
= alcOpenDevice(NULL
);
1406 fprintf(stderr
, "OpenAL: could not open device - exiting\n");
1409 context
= alcCreateContext(device
, NULL
);
1412 fprintf(stderr
, "OpenAL: could not create context - exiting\n");
1415 if(alcMakeContextCurrent(context
) == ALC_FALSE
)
1417 fprintf(stderr
, "OpenAL: could not make context current - exiting\n");
1421 if(!alIsExtensionPresent("AL_SOFT_source_length"))
1423 fprintf(stderr
, "Required AL_SOFT_source_length not supported - exiting\n");
1427 if(!alIsExtensionPresent("AL_SOFT_direct_channels"))
1428 fprintf(stderr
, "AL_SOFT_direct_channels not supported.\n");
1430 has_direct_out
= true;
1432 if(!alIsExtensionPresent("AL_SOFT_source_latency"))
1433 fprintf(stderr
, "AL_SOFT_source_latency not supported, audio may be a bit laggy.\n");
1436 alGetSourcedvSOFT
= alGetProcAddress("alGetSourcedvSOFT");
1437 has_latency_check
= true;
1441 movState
= av_mallocz(sizeof(MovieState
));
1443 av_strlcpy(movState
->filename
, argv
[fileidx
], sizeof(movState
->filename
));
1445 packet_queue_init(&movState
->audio
.q
);
1446 packet_queue_init(&movState
->video
.q
);
1448 almtx_init(&movState
->video
.pictq_mutex
, almtx_plain
);
1449 alcnd_init(&movState
->video
.pictq_cond
);
1450 almtx_init(&movState
->audio
.src_mutex
, almtx_recursive
);
1452 movState
->av_sync_type
= DEFAULT_AV_SYNC_TYPE
;
1454 movState
->pFormatCtx
= avformat_alloc_context();
1455 movState
->pFormatCtx
->interrupt_callback
= (AVIOInterruptCB
){.callback
=decode_interrupt_cb
, .opaque
=movState
};
1457 if(avio_open2(&movState
->pFormatCtx
->pb
, movState
->filename
, AVIO_FLAG_READ
,
1458 &movState
->pFormatCtx
->interrupt_callback
, NULL
))
1460 fprintf(stderr
, "Failed to open %s\n", movState
->filename
);
1464 /* Open movie file */
1465 if(avformat_open_input(&movState
->pFormatCtx
, movState
->filename
, NULL
, NULL
) != 0)
1467 fprintf(stderr
, "Failed to open %s\n", movState
->filename
);
1471 /* Retrieve stream information */
1472 if(avformat_find_stream_info(movState
->pFormatCtx
, NULL
) < 0)
1474 fprintf(stderr
, "%s: failed to find stream info\n", movState
->filename
);
1478 schedule_refresh(movState
, 40);
1481 if(althrd_create(&movState
->parse_thread
, decode_thread
, movState
) != althrd_success
)
1483 fprintf(stderr
, "Failed to create parse thread!\n");
1486 while(SDL_WaitEvent(&event
) == 1)
1491 switch(event
.key
.keysym
.sym
)
1494 movState
->quit
= true;
1498 stream_seek(movState
, -10.0);
1501 stream_seek(movState
, 10.0);
1504 stream_seek(movState
, 30.0);
1507 stream_seek(movState
, -30.0);
1511 movState
->direct_req
= true;
1519 case SDL_WINDOWEVENT
:
1520 switch(event
.window
.event
)
1522 case SDL_WINDOWEVENT_RESIZED
:
1523 SDL_SetRenderDrawColor(renderer
, 0, 0, 0, 255);
1524 SDL_RenderFillRect(renderer
, NULL
);
1533 movState
->quit
= true;
1536 case FF_UPDATE_EVENT
:
1537 update_picture(event
.user
.data1
, &first_update
, screen
, renderer
);
1540 case FF_REFRESH_EVENT
:
1541 video_refresh_timer(event
.user
.data1
, screen
, renderer
);
1545 althrd_join(movState
->parse_thread
, NULL
);
1547 avformat_close_input(&movState
->pFormatCtx
);
1549 almtx_destroy(&movState
->audio
.src_mutex
);
1550 almtx_destroy(&movState
->video
.pictq_mutex
);
1551 alcnd_destroy(&movState
->video
.pictq_cond
);
1552 packet_queue_deinit(&movState
->video
.q
);
1553 packet_queue_deinit(&movState
->audio
.q
);
1555 alcMakeContextCurrent(NULL
);
1556 alcDestroyContext(context
);
1557 alcCloseDevice(device
);
1567 fprintf(stderr
, "SDL_WaitEvent error - %s\n", SDL_GetError());