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_latency_check
= false;
37 static LPALGETSOURCEDVSOFT alGetSourcedvSOFT
;
39 #define AUDIO_BUFFER_TIME 100 /* In milliseconds, per-buffer */
40 #define AUDIO_BUFFER_QUEUE_SIZE 8 /* Number of buffers to queue */
41 #define MAX_AUDIOQ_SIZE (5 * 16 * 1024) /* Bytes of compressed audio data to keep queued */
42 #define MAX_VIDEOQ_SIZE (5 * 256 * 1024) /* Bytes of compressed video data to keep queued */
43 #define AV_SYNC_THRESHOLD 0.01
44 #define AV_NOSYNC_THRESHOLD 10.0
45 #define SAMPLE_CORRECTION_MAX_DIFF 0.1
46 #define AUDIO_DIFF_AVG_NB 20
47 #define VIDEO_PICTURE_QUEUE_SIZE 16
50 FF_UPDATE_EVENT
= SDL_USEREVENT
,
56 typedef struct PacketQueue
{
57 AVPacketList
*first_pkt
, *last_pkt
;
58 volatile int nb_packets
;
60 volatile bool flushing
;
65 typedef struct VideoPicture
{
67 int width
, height
; /* Logical image size (actual size may be larger) */
68 volatile bool updated
;
72 typedef struct AudioState
{
78 /* Used for clock difference average computation */
81 double diff_threshold
;
83 /* Time (in seconds) of the next sample to be buffered */
86 /* Decompressed sample frame, and swresample context for conversion */
87 AVFrame
*decoded_aframe
;
88 struct SwrContext
*swres_ctx
;
90 /* Conversion format, for what gets fed to OpenAL */
92 enum AVSampleFormat dst_sample_fmt
;
94 /* Storage of converted samples */
96 ssize_t samples_len
; /* In samples */
105 ALuint buffer
[AUDIO_BUFFER_QUEUE_SIZE
];
112 typedef struct VideoState
{
119 double frame_last_pts
;
120 double frame_last_delay
;
122 /* time (av_gettime) at which we updated current_pts - used to have running video pts */
123 int64_t current_pts_time
;
125 /* Decompressed video frame, and swscale context for conversion */
126 AVFrame
*decoded_vframe
;
127 struct SwsContext
*swscale_ctx
;
129 VideoPicture pictq
[VIDEO_PICTURE_QUEUE_SIZE
];
130 int pictq_size
, pictq_rindex
, pictq_windex
;
137 typedef struct MovieState
{
138 AVFormatContext
*pFormatCtx
;
139 int videoStream
, audioStream
;
141 volatile bool seek_req
;
146 int64_t external_clock_base
;
151 althrd_t parse_thread
;
159 AV_SYNC_AUDIO_MASTER
,
160 AV_SYNC_VIDEO_MASTER
,
161 AV_SYNC_EXTERNAL_MASTER
,
163 DEFAULT_AV_SYNC_TYPE
= AV_SYNC_EXTERNAL_MASTER
166 static AVPacket flush_pkt
= { .data
= (uint8_t*)"FLUSH" };
168 static void packet_queue_init(PacketQueue
*q
)
170 memset(q
, 0, sizeof(PacketQueue
));
171 almtx_init(&q
->mutex
, almtx_plain
);
172 alcnd_init(&q
->cond
);
174 static int packet_queue_put(PacketQueue
*q
, AVPacket
*pkt
)
177 if(pkt
!= &flush_pkt
&& !pkt
->buf
&& av_dup_packet(pkt
) < 0)
180 pkt1
= av_malloc(sizeof(AVPacketList
));
185 almtx_lock(&q
->mutex
);
189 q
->last_pkt
->next
= pkt1
;
192 q
->size
+= pkt1
->pkt
.size
;
193 almtx_unlock(&q
->mutex
);
195 alcnd_signal(&q
->cond
);
198 static int packet_queue_get(PacketQueue
*q
, AVPacket
*pkt
, MovieState
*state
)
203 almtx_lock(&q
->mutex
);
209 q
->first_pkt
= pkt1
->next
;
213 q
->size
-= pkt1
->pkt
.size
;
225 alcnd_wait(&q
->cond
, &q
->mutex
);
227 almtx_unlock(&q
->mutex
);
230 static void packet_queue_clear(PacketQueue
*q
)
232 AVPacketList
*pkt
, *pkt1
;
234 almtx_lock(&q
->mutex
);
235 for(pkt
= q
->first_pkt
;pkt
!= NULL
;pkt
= pkt1
)
238 if(pkt
->pkt
.data
!= flush_pkt
.data
)
239 av_free_packet(&pkt
->pkt
);
246 almtx_unlock(&q
->mutex
);
248 static void packet_queue_flush(PacketQueue
*q
)
250 almtx_lock(&q
->mutex
);
252 almtx_unlock(&q
->mutex
);
253 alcnd_signal(&q
->cond
);
255 static void packet_queue_deinit(PacketQueue
*q
)
257 packet_queue_clear(q
);
258 alcnd_destroy(&q
->cond
);
259 almtx_destroy(&q
->mutex
);
263 static double get_audio_clock(AudioState
*state
)
267 almtx_lock(&state
->src_mutex
);
268 /* The audio clock is the timestamp of the sample currently being heard.
269 * It's based on 4 components:
270 * 1 - The timestamp of the next sample to buffer (state->current_pts)
271 * 2 - The length of the source's buffer queue (AL_SEC_LENGTH_SOFT)
272 * 3 - The offset OpenAL is currently at in the source (the first value
273 * from AL_SEC_OFFSET_LATENCY_SOFT)
274 * 4 - The latency between OpenAL and the DAC (the second value from
275 * AL_SEC_OFFSET_LATENCY_SOFT)
277 * Subtracting the length of the source queue from the next sample's
278 * timestamp gives the timestamp of the sample at start of the source
279 * queue. Adding the source offset to that results in the timestamp for
280 * OpenAL's current position, and subtracting the source latency from that
281 * gives the timestamp of the sample currently at the DAC.
283 pts
= state
->current_pts
;
286 ALdouble offset
[2] = { 0.0, 0.0 };
287 ALdouble queue_len
= 0.0;
290 /* NOTE: The source state must be checked last, in case an underrun
291 * occurs and the source stops between retrieving the offset+latency
292 * and getting the state. */
293 if(has_latency_check
)
295 alGetSourcedvSOFT(state
->source
, AL_SEC_OFFSET_LATENCY_SOFT
, offset
);
296 alGetSourcedvSOFT(state
->source
, AL_SEC_LENGTH_SOFT
, &queue_len
);
301 alGetSourcei(state
->source
, AL_SAMPLE_OFFSET
, &ioffset
);
302 alGetSourcei(state
->source
, AL_SAMPLE_LENGTH_SOFT
, &ilen
);
303 offset
[0] = (double)ioffset
/ state
->st
->codec
->sample_rate
;
304 queue_len
= (double)ilen
/ state
->st
->codec
->sample_rate
;
306 alGetSourcei(state
->source
, AL_SOURCE_STATE
, &status
);
308 /* If the source is AL_STOPPED, then there was an underrun and all
309 * buffers are processed, so ignore the source queue. The audio thread
310 * will put the source into an AL_INITIAL state and clear the queue
311 * when it starts recovery. */
312 if(status
!= AL_STOPPED
)
313 pts
= pts
- queue_len
+ offset
[0];
314 if(status
== AL_PLAYING
)
315 pts
= pts
- offset
[1];
317 almtx_unlock(&state
->src_mutex
);
319 return (pts
>= 0.0) ? pts
: 0.0;
321 static double get_video_clock(VideoState
*state
)
323 double delta
= (av_gettime() - state
->current_pts_time
) / 1000000.0;
324 return state
->current_pts
+ delta
;
326 static double get_external_clock(MovieState
*movState
)
328 return (av_gettime()-movState
->external_clock_base
) / 1000000.0;
331 double get_master_clock(MovieState
*movState
)
333 if(movState
->av_sync_type
== AV_SYNC_VIDEO_MASTER
)
334 return get_video_clock(&movState
->video
);
335 if(movState
->av_sync_type
== AV_SYNC_AUDIO_MASTER
)
336 return get_audio_clock(&movState
->audio
);
337 return get_external_clock(movState
);
340 /* Return how many samples to skip to maintain sync (negative means to
341 * duplicate samples). */
342 static int synchronize_audio(MovieState
*movState
)
344 double diff
, avg_diff
;
347 if(movState
->av_sync_type
== AV_SYNC_AUDIO_MASTER
)
350 ref_clock
= get_master_clock(movState
);
351 diff
= ref_clock
- get_audio_clock(&movState
->audio
);
353 if(!(diff
< AV_NOSYNC_THRESHOLD
))
355 /* Difference is TOO big; reset diff stuff */
356 movState
->audio
.diff_accum
= 0.0;
360 /* Accumulate the diffs */
361 movState
->audio
.diff_accum
= movState
->audio
.diff_accum
*movState
->audio
.diff_avg_coef
+ diff
;
362 avg_diff
= movState
->audio
.diff_accum
*(1.0 - movState
->audio
.diff_avg_coef
);
363 if(fabs(avg_diff
) < movState
->audio
.diff_threshold
)
366 /* Constrain the per-update difference to avoid exceedingly large skips */
367 if(!(diff
<= SAMPLE_CORRECTION_MAX_DIFF
))
368 diff
= SAMPLE_CORRECTION_MAX_DIFF
;
369 else if(!(diff
>= -SAMPLE_CORRECTION_MAX_DIFF
))
370 diff
= -SAMPLE_CORRECTION_MAX_DIFF
;
371 return (int)(diff
*movState
->audio
.st
->codec
->sample_rate
);
374 static int audio_decode_frame(MovieState
*movState
)
376 AVPacket
*pkt
= &movState
->audio
.pkt
;
378 while(!movState
->quit
)
380 while(!movState
->quit
&& pkt
->size
== 0)
384 /* Get the next packet */
386 if((err
=packet_queue_get(&movState
->audio
.q
, pkt
, movState
)) <= 0)
392 if(pkt
->data
== flush_pkt
.data
)
394 avcodec_flush_buffers(movState
->audio
.st
->codec
);
395 movState
->audio
.diff_accum
= 0.0;
396 movState
->audio
.current_pts
= av_q2d(movState
->audio
.st
->time_base
)*pkt
->pts
;
398 alSourceRewind(movState
->audio
.source
);
399 alSourcei(movState
->audio
.source
, AL_BUFFER
, 0);
401 av_new_packet(pkt
, 0);
406 /* If provided, update w/ pts */
407 if(pkt
->pts
!= AV_NOPTS_VALUE
)
408 movState
->audio
.current_pts
= av_q2d(movState
->audio
.st
->time_base
)*pkt
->pts
;
411 AVFrame
*frame
= movState
->audio
.decoded_aframe
;
413 int len1
= avcodec_decode_audio4(movState
->audio
.st
->codec
, frame
,
417 if(len1
<= pkt
->size
)
419 /* Move the unread data to the front and clear the end bits */
420 int remaining
= pkt
->size
- len1
;
421 memmove(pkt
->data
, &pkt
->data
[len1
], remaining
);
422 av_shrink_packet(pkt
, remaining
);
425 if(!got_frame
|| frame
->nb_samples
<= 0)
427 av_frame_unref(frame
);
431 if(frame
->nb_samples
> movState
->audio
.samples_max
)
433 av_freep(&movState
->audio
.samples
);
435 &movState
->audio
.samples
, NULL
, movState
->audio
.st
->codec
->channels
,
436 frame
->nb_samples
, movState
->audio
.dst_sample_fmt
, 0
438 movState
->audio
.samples_max
= frame
->nb_samples
;
440 /* Return the amount of sample frames converted */
441 int data_size
= swr_convert(movState
->audio
.swres_ctx
,
442 &movState
->audio
.samples
, frame
->nb_samples
,
443 (const uint8_t**)frame
->data
, frame
->nb_samples
446 av_frame_unref(frame
);
453 static int read_audio(MovieState
*movState
, uint8_t *samples
, int length
)
455 int sample_skip
= synchronize_audio(movState
);
458 /* Read the next chunk of data, refill the buffer, and queue it
460 length
/= movState
->audio
.frame_size
;
461 while(audio_size
< length
)
463 if(movState
->audio
.samples_len
<= 0 || movState
->audio
.samples_pos
>= movState
->audio
.samples_len
)
465 int frame_len
= audio_decode_frame(movState
);
466 if(frame_len
< 0) return -1;
468 movState
->audio
.samples_len
= frame_len
;
469 if(movState
->audio
.samples_len
== 0)
472 movState
->audio
.samples_pos
= (movState
->audio
.samples_len
< sample_skip
) ?
473 movState
->audio
.samples_len
: sample_skip
;
474 sample_skip
-= movState
->audio
.samples_pos
;
476 movState
->audio
.current_pts
+= (double)movState
->audio
.samples_pos
/
477 (double)movState
->audio
.st
->codec
->sample_rate
;
481 int rem
= length
- audio_size
;
482 if(movState
->audio
.samples_pos
>= 0)
484 int n
= movState
->audio
.frame_size
;
485 int len
= movState
->audio
.samples_len
- movState
->audio
.samples_pos
;
486 if(rem
> len
) rem
= len
;
487 memcpy(samples
+ audio_size
*n
,
488 movState
->audio
.samples
+ movState
->audio
.samples_pos
*n
,
493 int n
= movState
->audio
.frame_size
;
494 int len
= -movState
->audio
.samples_pos
;
495 if(rem
> len
) rem
= len
;
497 /* Add samples by copying the first sample */
500 uint8_t sample
= ((uint8_t*)movState
->audio
.samples
)[0];
501 uint8_t *q
= (uint8_t*)samples
+ audio_size
;
502 for(int i
= 0;i
< rem
;i
++)
507 uint16_t sample
= ((uint16_t*)movState
->audio
.samples
)[0];
508 uint16_t *q
= (uint16_t*)samples
+ audio_size
;
509 for(int i
= 0;i
< rem
;i
++)
514 uint32_t sample
= ((uint32_t*)movState
->audio
.samples
)[0];
515 uint32_t *q
= (uint32_t*)samples
+ audio_size
;
516 for(int i
= 0;i
< rem
;i
++)
521 uint64_t sample
= ((uint64_t*)movState
->audio
.samples
)[0];
522 uint64_t *q
= (uint64_t*)samples
+ audio_size
;
523 for(int i
= 0;i
< rem
;i
++)
528 uint8_t *sample
= movState
->audio
.samples
;
529 uint8_t *q
= samples
+ audio_size
*n
;
530 for(int i
= 0;i
< rem
;i
++)
532 memcpy(q
, sample
, n
);
538 movState
->audio
.samples_pos
+= rem
;
539 movState
->audio
.current_pts
+= (double)rem
/ movState
->audio
.st
->codec
->sample_rate
;
543 return audio_size
* movState
->audio
.frame_size
;
546 static int audio_thread(void *userdata
)
548 MovieState
*movState
= (MovieState
*)userdata
;
549 uint8_t *samples
= NULL
;
552 alGenBuffers(AUDIO_BUFFER_QUEUE_SIZE
, movState
->audio
.buffer
);
553 alGenSources(1, &movState
->audio
.source
);
555 alSourcei(movState
->audio
.source
, AL_SOURCE_RELATIVE
, AL_TRUE
);
556 alSourcei(movState
->audio
.source
, AL_ROLLOFF_FACTOR
, 0);
558 av_new_packet(&movState
->audio
.pkt
, 0);
560 /* Find a suitable format for OpenAL. Currently does not handle surround
561 * sound (everything non-mono becomes stereo). */
562 if(movState
->audio
.st
->codec
->sample_fmt
== AV_SAMPLE_FMT_U8
||
563 movState
->audio
.st
->codec
->sample_fmt
== AV_SAMPLE_FMT_U8P
)
565 movState
->audio
.dst_sample_fmt
= AV_SAMPLE_FMT_U8
;
566 movState
->audio
.frame_size
= 1;
567 if(movState
->audio
.st
->codec
->channel_layout
== AV_CH_LAYOUT_MONO
)
569 movState
->audio
.dst_ch_layout
= AV_CH_LAYOUT_MONO
;
570 movState
->audio
.frame_size
*= 1;
571 movState
->audio
.format
= AL_FORMAT_MONO8
;
575 movState
->audio
.dst_ch_layout
= AV_CH_LAYOUT_STEREO
;
576 movState
->audio
.frame_size
*= 2;
577 movState
->audio
.format
= AL_FORMAT_STEREO8
;
580 else if((movState
->audio
.st
->codec
->sample_fmt
== AV_SAMPLE_FMT_FLT
||
581 movState
->audio
.st
->codec
->sample_fmt
== AV_SAMPLE_FMT_FLTP
) &&
582 alIsExtensionPresent("AL_EXT_FLOAT32"))
584 movState
->audio
.dst_sample_fmt
= AV_SAMPLE_FMT_FLT
;
585 movState
->audio
.frame_size
= 4;
586 if(movState
->audio
.st
->codec
->channel_layout
== AV_CH_LAYOUT_MONO
)
588 movState
->audio
.dst_ch_layout
= AV_CH_LAYOUT_MONO
;
589 movState
->audio
.frame_size
*= 1;
590 movState
->audio
.format
= AL_FORMAT_MONO_FLOAT32
;
594 movState
->audio
.dst_ch_layout
= AV_CH_LAYOUT_STEREO
;
595 movState
->audio
.frame_size
*= 2;
596 movState
->audio
.format
= AL_FORMAT_STEREO_FLOAT32
;
601 movState
->audio
.dst_sample_fmt
= AV_SAMPLE_FMT_S16
;
602 movState
->audio
.frame_size
= 2;
603 if(movState
->audio
.st
->codec
->channel_layout
== AV_CH_LAYOUT_MONO
)
605 movState
->audio
.dst_ch_layout
= AV_CH_LAYOUT_MONO
;
606 movState
->audio
.frame_size
*= 1;
607 movState
->audio
.format
= AL_FORMAT_MONO16
;
611 movState
->audio
.dst_ch_layout
= AV_CH_LAYOUT_STEREO
;
612 movState
->audio
.frame_size
*= 2;
613 movState
->audio
.format
= AL_FORMAT_STEREO16
;
616 buffer_len
= AUDIO_BUFFER_TIME
* movState
->audio
.st
->codec
->sample_rate
/ 1000 *
617 movState
->audio
.frame_size
;
618 samples
= av_malloc(buffer_len
);
620 movState
->audio
.samples
= NULL
;
621 movState
->audio
.samples_max
= 0;
622 movState
->audio
.samples_pos
= 0;
623 movState
->audio
.samples_len
= 0;
625 if(!(movState
->audio
.decoded_aframe
=av_frame_alloc()))
627 fprintf(stderr
, "Failed to allocate audio frame\n");
631 movState
->audio
.swres_ctx
= swr_alloc_set_opts(NULL
,
632 movState
->audio
.dst_ch_layout
,
633 movState
->audio
.dst_sample_fmt
,
634 movState
->audio
.st
->codec
->sample_rate
,
635 movState
->audio
.st
->codec
->channel_layout
,
636 movState
->audio
.st
->codec
->sample_fmt
,
637 movState
->audio
.st
->codec
->sample_rate
,
640 if(!movState
->audio
.swres_ctx
|| swr_init(movState
->audio
.swres_ctx
) != 0)
642 fprintf(stderr
, "Failed to initialize audio converter\n");
646 almtx_lock(&movState
->audio
.src_mutex
);
647 while(alGetError() == AL_NO_ERROR
&& !movState
->quit
)
649 /* First remove any processed buffers. */
651 alGetSourcei(movState
->audio
.source
, AL_BUFFERS_PROCESSED
, &processed
);
652 alSourceUnqueueBuffers(movState
->audio
.source
, processed
, (ALuint
[AUDIO_BUFFER_QUEUE_SIZE
]){});
654 /* Refill the buffer queue. */
656 alGetSourcei(movState
->audio
.source
, AL_BUFFERS_QUEUED
, &queued
);
657 while(queued
< AUDIO_BUFFER_QUEUE_SIZE
)
661 /* Read the next chunk of data, fill the buffer, and queue it on
663 audio_size
= read_audio(movState
, samples
, buffer_len
);
664 if(audio_size
< 0) break;
666 ALuint bufid
= movState
->audio
.buffer
[movState
->audio
.buffer_idx
++];
667 movState
->audio
.buffer_idx
%= AUDIO_BUFFER_QUEUE_SIZE
;
669 alBufferData(bufid
, movState
->audio
.format
, samples
, audio_size
,
670 movState
->audio
.st
->codec
->sample_rate
);
671 alSourceQueueBuffers(movState
->audio
.source
, 1, &bufid
);
675 /* Check that the source is playing. */
677 alGetSourcei(movState
->audio
.source
, AL_SOURCE_STATE
, &state
);
678 if(state
== AL_STOPPED
)
680 /* AL_STOPPED means there was an underrun. Double-check that all
681 * processed buffers are removed, then rewind the source to get it
682 * back into an AL_INITIAL state. */
683 alGetSourcei(movState
->audio
.source
, AL_BUFFERS_PROCESSED
, &processed
);
684 alSourceUnqueueBuffers(movState
->audio
.source
, processed
, (ALuint
[AUDIO_BUFFER_QUEUE_SIZE
]){});
685 alSourceRewind(movState
->audio
.source
);
689 almtx_unlock(&movState
->audio
.src_mutex
);
691 /* (re)start the source if needed, and wait for a buffer to finish */
692 if(state
!= AL_PLAYING
&& state
!= AL_PAUSED
)
694 alGetSourcei(movState
->audio
.source
, AL_BUFFERS_QUEUED
, &queued
);
695 if(queued
> 0) alSourcePlay(movState
->audio
.source
);
697 SDL_Delay(AUDIO_BUFFER_TIME
);
699 almtx_lock(&movState
->audio
.src_mutex
);
701 almtx_unlock(&movState
->audio
.src_mutex
);
704 av_frame_free(&movState
->audio
.decoded_aframe
);
705 swr_free(&movState
->audio
.swres_ctx
);
708 av_freep(&movState
->audio
.samples
);
710 alDeleteSources(1, &movState
->audio
.source
);
711 alDeleteBuffers(AUDIO_BUFFER_QUEUE_SIZE
, movState
->audio
.buffer
);
717 static Uint32
sdl_refresh_timer_cb(Uint32 interval
, void *opaque
)
721 SDL_PushEvent(&(SDL_Event
){ .user
={.type
=FF_REFRESH_EVENT
, .data1
=opaque
} });
722 return 0; /* 0 means stop timer */
725 /* Schedule a video refresh in 'delay' ms */
726 static void schedule_refresh(MovieState
*movState
, int delay
)
728 SDL_AddTimer(delay
, sdl_refresh_timer_cb
, movState
);
731 static void video_display(MovieState
*movState
, SDL_Window
*screen
, SDL_Renderer
*renderer
)
733 VideoPicture
*vp
= &movState
->video
.pictq
[movState
->video
.pictq_rindex
];
742 if(movState
->video
.st
->codec
->sample_aspect_ratio
.num
== 0)
746 aspect_ratio
= av_q2d(movState
->video
.st
->codec
->sample_aspect_ratio
) *
747 movState
->video
.st
->codec
->width
/
748 movState
->video
.st
->codec
->height
;
750 if(aspect_ratio
<= 0.0f
)
752 aspect_ratio
= (float)movState
->video
.st
->codec
->width
/
753 (float)movState
->video
.st
->codec
->height
;
756 SDL_GetWindowSize(screen
, &win_w
, &win_h
);
758 w
= ((int)rint(h
* aspect_ratio
) + 3) & ~3;
762 h
= ((int)rint(w
/ aspect_ratio
) + 3) & ~3;
767 SDL_RenderCopy(renderer
, vp
->bmp
,
768 &(SDL_Rect
){ .x
=0, .y
=0, .w
=vp
->width
, .h
=vp
->height
},
769 &(SDL_Rect
){ .x
=x
, .y
=y
, .w
=w
, .h
=h
}
771 SDL_RenderPresent(renderer
);
774 static void video_refresh_timer(MovieState
*movState
, SDL_Window
*screen
, SDL_Renderer
*renderer
)
776 if(!movState
->video
.st
)
778 schedule_refresh(movState
, 100);
782 almtx_lock(&movState
->video
.pictq_mutex
);
784 if(movState
->video
.pictq_size
== 0)
785 schedule_refresh(movState
, 1);
788 VideoPicture
*vp
= &movState
->video
.pictq
[movState
->video
.pictq_rindex
];
789 double actual_delay
, delay
, sync_threshold
, ref_clock
, diff
;
791 movState
->video
.current_pts
= vp
->pts
;
792 movState
->video
.current_pts_time
= av_gettime();
794 delay
= vp
->pts
- movState
->video
.frame_last_pts
; /* the pts from last time */
795 if(delay
<= 0 || delay
>= 1.0)
797 /* if incorrect delay, use previous one */
798 delay
= movState
->video
.frame_last_delay
;
800 /* save for next time */
801 movState
->video
.frame_last_delay
= delay
;
802 movState
->video
.frame_last_pts
= vp
->pts
;
804 /* Update delay to sync to clock if not master source. */
805 if(movState
->av_sync_type
!= AV_SYNC_VIDEO_MASTER
)
807 ref_clock
= get_master_clock(movState
);
808 diff
= vp
->pts
- ref_clock
;
810 /* Skip or repeat the frame. Take delay into account. */
811 sync_threshold
= (delay
> AV_SYNC_THRESHOLD
) ? delay
: AV_SYNC_THRESHOLD
;
812 if(fabs(diff
) < AV_NOSYNC_THRESHOLD
)
814 if(diff
<= -sync_threshold
)
816 else if(diff
>= sync_threshold
)
821 movState
->video
.frame_timer
+= delay
;
822 /* Compute the REAL delay. */
823 actual_delay
= movState
->video
.frame_timer
- (av_gettime() / 1000000.0);
824 if(!(actual_delay
>= 0.010))
826 /* We don't have time to handle this picture, just skip to the next one. */
827 movState
->video
.pictq_rindex
= (movState
->video
.pictq_rindex
+1)%VIDEO_PICTURE_QUEUE_SIZE
;
828 movState
->video
.pictq_size
--;
829 alcnd_signal(&movState
->video
.pictq_cond
);
832 schedule_refresh(movState
, (int)(actual_delay
*1000.0 + 0.5));
834 /* Show the picture! */
835 video_display(movState
, screen
, renderer
);
837 /* Update queue for next picture. */
838 movState
->video
.pictq_rindex
= (movState
->video
.pictq_rindex
+1)%VIDEO_PICTURE_QUEUE_SIZE
;
839 movState
->video
.pictq_size
--;
840 alcnd_signal(&movState
->video
.pictq_cond
);
842 almtx_unlock(&movState
->video
.pictq_mutex
);
846 static void update_picture(MovieState
*movState
, bool *first_update
, SDL_Window
*screen
, SDL_Renderer
*renderer
)
848 VideoPicture
*vp
= &movState
->video
.pictq
[movState
->video
.pictq_windex
];
850 /* allocate or resize the buffer! */
851 if(!vp
->bmp
|| vp
->width
!= movState
->video
.st
->codec
->width
||
852 vp
->height
!= movState
->video
.st
->codec
->height
)
855 SDL_DestroyTexture(vp
->bmp
);
856 vp
->bmp
= SDL_CreateTexture(
857 renderer
, SDL_PIXELFORMAT_YV12
, SDL_TEXTUREACCESS_STREAMING
,
858 movState
->video
.st
->codec
->coded_width
, movState
->video
.st
->codec
->coded_height
861 fprintf(stderr
, "Failed to create YV12 texture!\n");
862 vp
->width
= movState
->video
.st
->codec
->width
;
863 vp
->height
= movState
->video
.st
->codec
->height
;
865 if(*first_update
&& vp
->width
> 0 && vp
->height
> 0)
867 /* For the first update, set the window size to the video size. */
868 *first_update
= false;
872 if(movState
->video
.st
->codec
->sample_aspect_ratio
.num
!= 0 &&
873 movState
->video
.st
->codec
->sample_aspect_ratio
.den
!= 0)
875 double aspect_ratio
= av_q2d(movState
->video
.st
->codec
->sample_aspect_ratio
);
876 if(aspect_ratio
>= 1.0)
877 w
= (int)(w
*aspect_ratio
+ 0.5);
878 else if(aspect_ratio
> 0.0)
879 h
= (int)(h
/aspect_ratio
+ 0.5);
881 SDL_SetWindowSize(screen
, w
, h
);
887 AVFrame
*frame
= movState
->video
.decoded_vframe
;
891 if(movState
->video
.st
->codec
->pix_fmt
== PIX_FMT_YUV420P
)
892 SDL_UpdateYUVTexture(vp
->bmp
, NULL
,
893 frame
->data
[0], frame
->linesize
[0],
894 frame
->data
[1], frame
->linesize
[1],
895 frame
->data
[2], frame
->linesize
[2]
897 else if(SDL_LockTexture(vp
->bmp
, NULL
, &pixels
, &pitch
) != 0)
898 fprintf(stderr
, "Failed to lock texture\n");
901 // Convert the image into YUV format that SDL uses
902 int coded_w
= movState
->video
.st
->codec
->coded_width
;
903 int coded_h
= movState
->video
.st
->codec
->coded_height
;
904 int w
= movState
->video
.st
->codec
->width
;
905 int h
= movState
->video
.st
->codec
->height
;
906 if(!movState
->video
.swscale_ctx
)
907 movState
->video
.swscale_ctx
= sws_getContext(
908 w
, h
, movState
->video
.st
->codec
->pix_fmt
,
909 w
, h
, PIX_FMT_YUV420P
, SWS_X
, NULL
, NULL
, NULL
912 /* point pict at the queue */
914 pict
.data
[0] = pixels
;
915 pict
.data
[2] = pict
.data
[0] + coded_w
*coded_h
;
916 pict
.data
[1] = pict
.data
[2] + coded_w
*coded_h
/4;
918 pict
.linesize
[0] = pitch
;
919 pict
.linesize
[2] = pitch
/ 2;
920 pict
.linesize
[1] = pitch
/ 2;
922 sws_scale(movState
->video
.swscale_ctx
, (const uint8_t**)frame
->data
,
923 frame
->linesize
, 0, h
, pict
.data
, pict
.linesize
);
924 SDL_UnlockTexture(vp
->bmp
);
928 almtx_lock(&movState
->video
.pictq_mutex
);
930 almtx_unlock(&movState
->video
.pictq_mutex
);
931 alcnd_signal(&movState
->video
.pictq_cond
);
934 static int queue_picture(MovieState
*movState
, double pts
)
936 /* Wait until we have space for a new pic */
937 almtx_lock(&movState
->video
.pictq_mutex
);
938 while(movState
->video
.pictq_size
>= VIDEO_PICTURE_QUEUE_SIZE
&& !movState
->quit
)
939 alcnd_wait(&movState
->video
.pictq_cond
, &movState
->video
.pictq_mutex
);
940 almtx_unlock(&movState
->video
.pictq_mutex
);
945 VideoPicture
*vp
= &movState
->video
.pictq
[movState
->video
.pictq_windex
];
947 /* We have to create/update the picture in the main thread */
949 SDL_PushEvent(&(SDL_Event
){ .user
={.type
=FF_UPDATE_EVENT
, .data1
=movState
} });
951 /* Wait until the picture is updated. */
952 almtx_lock(&movState
->video
.pictq_mutex
);
953 while(!vp
->updated
&& !movState
->quit
)
954 alcnd_wait(&movState
->video
.pictq_cond
, &movState
->video
.pictq_mutex
);
955 almtx_unlock(&movState
->video
.pictq_mutex
);
960 movState
->video
.pictq_windex
= (movState
->video
.pictq_windex
+1)%VIDEO_PICTURE_QUEUE_SIZE
;
961 almtx_lock(&movState
->video
.pictq_mutex
);
962 movState
->video
.pictq_size
++;
963 almtx_unlock(&movState
->video
.pictq_mutex
);
968 static double synchronize_video(MovieState
*movState
, double pts
)
972 if(pts
== 0.0) /* if we aren't given a pts, set it to the clock */
973 pts
= movState
->video
.clock
;
974 else /* if we have pts, set video clock to it */
975 movState
->video
.clock
= pts
;
977 /* update the video clock */
978 frame_delay
= av_q2d(movState
->video
.st
->codec
->time_base
);
979 /* if we are repeating a frame, adjust clock accordingly */
980 frame_delay
+= movState
->video
.decoded_vframe
->repeat_pict
* (frame_delay
* 0.5);
981 movState
->video
.clock
+= frame_delay
;
985 int video_thread(void *arg
)
987 MovieState
*movState
= (MovieState
*)arg
;
988 AVPacket
*packet
= (AVPacket
[1]){};
989 int64_t saved_pts
, pkt_pts
;
992 movState
->video
.decoded_vframe
= av_frame_alloc();
993 while(packet_queue_get(&movState
->video
.q
, packet
, movState
) >= 0)
995 if(packet
->data
== flush_pkt
.data
)
997 avcodec_flush_buffers(movState
->video
.st
->codec
);
999 almtx_lock(&movState
->video
.pictq_mutex
);
1000 movState
->video
.pictq_size
= 0;
1001 movState
->video
.pictq_rindex
= 0;
1002 movState
->video
.pictq_windex
= 0;
1003 almtx_unlock(&movState
->video
.pictq_mutex
);
1005 movState
->video
.clock
= av_q2d(movState
->video
.st
->time_base
)*packet
->pts
;
1006 movState
->video
.current_pts
= movState
->video
.clock
;
1007 movState
->video
.current_pts_time
= av_gettime();
1011 pkt_pts
= packet
->pts
;
1013 /* Decode video frame */
1014 avcodec_decode_video2(movState
->video
.st
->codec
, movState
->video
.decoded_vframe
,
1015 &frameFinished
, packet
);
1016 if(pkt_pts
!= AV_NOPTS_VALUE
&& !movState
->video
.decoded_vframe
->opaque
)
1018 /* Store the packet's original pts in the frame, in case the frame
1019 * is not finished decoding yet. */
1020 saved_pts
= pkt_pts
;
1021 movState
->video
.decoded_vframe
->opaque
= &saved_pts
;
1024 av_free_packet(packet
);
1028 double pts
= av_q2d(movState
->video
.st
->time_base
);
1029 if(packet
->dts
!= AV_NOPTS_VALUE
)
1031 else if(movState
->video
.decoded_vframe
->opaque
)
1032 pts
*= *(int64_t*)movState
->video
.decoded_vframe
->opaque
;
1035 movState
->video
.decoded_vframe
->opaque
= NULL
;
1037 pts
= synchronize_video(movState
, pts
);
1038 if(queue_picture(movState
, pts
) < 0)
1043 sws_freeContext(movState
->video
.swscale_ctx
);
1044 movState
->video
.swscale_ctx
= NULL
;
1045 av_frame_free(&movState
->video
.decoded_vframe
);
1050 static int stream_component_open(MovieState
*movState
, int stream_index
)
1052 AVFormatContext
*pFormatCtx
= movState
->pFormatCtx
;
1053 AVCodecContext
*codecCtx
;
1056 if(stream_index
< 0 || (unsigned int)stream_index
>= pFormatCtx
->nb_streams
)
1059 /* Get a pointer to the codec context for the video stream, and open the
1060 * associated codec */
1061 codecCtx
= pFormatCtx
->streams
[stream_index
]->codec
;
1063 codec
= avcodec_find_decoder(codecCtx
->codec_id
);
1064 if(!codec
|| avcodec_open2(codecCtx
, codec
, NULL
) < 0)
1066 fprintf(stderr
, "Unsupported codec!\n");
1070 /* Initialize and start the media type handler */
1071 switch(codecCtx
->codec_type
)
1073 case AVMEDIA_TYPE_AUDIO
:
1074 movState
->audioStream
= stream_index
;
1075 movState
->audio
.st
= pFormatCtx
->streams
[stream_index
];
1077 /* Averaging filter for audio sync */
1078 movState
->audio
.diff_avg_coef
= exp(log(0.01) / AUDIO_DIFF_AVG_NB
);
1079 /* Correct audio only if larger error than this */
1080 movState
->audio
.diff_threshold
= 2.0 * 0.050/* 50 ms */;
1082 memset(&movState
->audio
.pkt
, 0, sizeof(movState
->audio
.pkt
));
1083 if(althrd_create(&movState
->audio
.thread
, audio_thread
, movState
) != althrd_success
)
1085 movState
->audioStream
= -1;
1086 movState
->audio
.st
= NULL
;
1090 case AVMEDIA_TYPE_VIDEO
:
1091 movState
->videoStream
= stream_index
;
1092 movState
->video
.st
= pFormatCtx
->streams
[stream_index
];
1094 movState
->video
.current_pts_time
= av_gettime();
1095 movState
->video
.frame_timer
= (double)movState
->video
.current_pts_time
/
1097 movState
->video
.frame_last_delay
= 40e-3;
1099 if(althrd_create(&movState
->video
.thread
, video_thread
, movState
) != althrd_success
)
1101 movState
->videoStream
= -1;
1102 movState
->video
.st
= NULL
;
1113 static int decode_interrupt_cb(void *ctx
)
1115 return ((MovieState
*)ctx
)->quit
;
1118 int decode_thread(void *arg
)
1120 MovieState
*movState
= (MovieState
*)arg
;
1121 AVFormatContext
*fmtCtx
= movState
->pFormatCtx
;
1122 AVPacket
*packet
= (AVPacket
[1]){};
1123 int video_index
= -1;
1124 int audio_index
= -1;
1126 movState
->videoStream
= -1;
1127 movState
->audioStream
= -1;
1129 /* Dump information about file onto standard error */
1130 av_dump_format(fmtCtx
, 0, movState
->filename
, 0);
1132 /* Find the first video and audio streams */
1133 for(unsigned int i
= 0;i
< fmtCtx
->nb_streams
;i
++)
1135 if(fmtCtx
->streams
[i
]->codec
->codec_type
== AVMEDIA_TYPE_VIDEO
&& video_index
< 0)
1137 else if(fmtCtx
->streams
[i
]->codec
->codec_type
== AVMEDIA_TYPE_AUDIO
&& audio_index
< 0)
1140 movState
->external_clock_base
= av_gettime();
1141 if(audio_index
>= 0)
1142 stream_component_open(movState
, audio_index
);
1143 if(video_index
>= 0)
1144 stream_component_open(movState
, video_index
);
1146 if(movState
->videoStream
< 0 && movState
->audioStream
< 0)
1148 fprintf(stderr
, "%s: could not open codecs\n", movState
->filename
);
1152 /* Main packet handling loop */
1153 while(!movState
->quit
)
1155 if(movState
->seek_req
)
1157 int64_t seek_target
= movState
->seek_pos
;
1158 int stream_index
= -1;
1160 /* Prefer seeking on the video stream. */
1161 if(movState
->videoStream
>= 0)
1162 stream_index
= movState
->videoStream
;
1163 else if(movState
->audioStream
>= 0)
1164 stream_index
= movState
->audioStream
;
1166 /* Get a seek timestamp for the appropriate stream. */
1167 int64_t timestamp
= seek_target
;
1168 if(stream_index
>= 0)
1169 timestamp
= av_rescale_q(seek_target
, AV_TIME_BASE_Q
, fmtCtx
->streams
[stream_index
]->time_base
);
1171 if(av_seek_frame(movState
->pFormatCtx
, stream_index
, timestamp
, 0) < 0)
1172 fprintf(stderr
, "%s: error while seeking\n", movState
->pFormatCtx
->filename
);
1175 /* Seek successful, clear the packet queues and send a special
1176 * 'flush' packet with the new stream clock time. */
1177 if(movState
->audioStream
>= 0)
1179 packet_queue_clear(&movState
->audio
.q
);
1180 flush_pkt
.pts
= av_rescale_q(seek_target
, AV_TIME_BASE_Q
,
1181 fmtCtx
->streams
[movState
->audioStream
]->time_base
1183 packet_queue_put(&movState
->audio
.q
, &flush_pkt
);
1185 if(movState
->videoStream
>= 0)
1187 packet_queue_clear(&movState
->video
.q
);
1188 flush_pkt
.pts
= av_rescale_q(seek_target
, AV_TIME_BASE_Q
,
1189 fmtCtx
->streams
[movState
->videoStream
]->time_base
1191 packet_queue_put(&movState
->video
.q
, &flush_pkt
);
1193 movState
->external_clock_base
= av_gettime() - seek_target
;
1195 movState
->seek_req
= false;
1198 if(movState
->audio
.q
.size
>= MAX_AUDIOQ_SIZE
||
1199 movState
->video
.q
.size
>= MAX_VIDEOQ_SIZE
)
1205 if(av_read_frame(movState
->pFormatCtx
, packet
) < 0)
1207 packet_queue_flush(&movState
->video
.q
);
1208 packet_queue_flush(&movState
->audio
.q
);
1212 /* Place the packet in the queue it's meant for, or discard it. */
1213 if(packet
->stream_index
== movState
->videoStream
)
1214 packet_queue_put(&movState
->video
.q
, packet
);
1215 else if(packet
->stream_index
== movState
->audioStream
)
1216 packet_queue_put(&movState
->audio
.q
, packet
);
1218 av_free_packet(packet
);
1221 /* all done - wait for it */
1222 while(!movState
->quit
)
1224 if(movState
->audio
.q
.nb_packets
== 0 && movState
->video
.q
.nb_packets
== 0)
1230 movState
->quit
= true;
1231 packet_queue_flush(&movState
->video
.q
);
1232 packet_queue_flush(&movState
->audio
.q
);
1234 if(movState
->videoStream
>= 0)
1235 althrd_join(movState
->video
.thread
, NULL
);
1236 if(movState
->audioStream
>= 0)
1237 althrd_join(movState
->audio
.thread
, NULL
);
1239 SDL_PushEvent(&(SDL_Event
){ .user
={.type
=FF_QUIT_EVENT
, .data1
=movState
} });
1245 static void stream_seek(MovieState
*movState
, double incr
)
1247 if(!movState
->seek_req
)
1249 double newtime
= get_master_clock(movState
)+incr
;
1250 if(newtime
<= 0.0) movState
->seek_pos
= 0;
1251 else movState
->seek_pos
= (int64_t)(newtime
* AV_TIME_BASE
);
1252 movState
->seek_req
= true;
1256 int main(int argc
, char *argv
[])
1259 MovieState
*movState
;
1260 bool first_update
= true;
1262 SDL_Renderer
*renderer
;
1264 ALCcontext
*context
;
1268 fprintf(stderr
, "Usage: %s <file>\n", argv
[0]);
1271 /* Register all formats and codecs */
1273 /* Initialize networking protocols */
1274 avformat_network_init();
1276 if(SDL_Init(SDL_INIT_VIDEO
| SDL_INIT_TIMER
))
1278 fprintf(stderr
, "Could not initialize SDL - %s\n", SDL_GetError());
1282 /* Make a window to put our video */
1283 screen
= SDL_CreateWindow("alffplay", 0, 0, 640, 480, SDL_WINDOW_RESIZABLE
);
1286 fprintf(stderr
, "SDL: could not set video mode - exiting\n");
1289 /* Make a renderer to handle the texture image surface and rendering. */
1290 renderer
= SDL_CreateRenderer(screen
, -1, SDL_RENDERER_ACCELERATED
);
1293 SDL_RendererInfo rinf
;
1296 /* Make sure the renderer supports YV12 textures. If not, fallback to a
1297 * software renderer. */
1298 if(SDL_GetRendererInfo(renderer
, &rinf
) == 0)
1300 for(Uint32 i
= 0;!ok
&& i
< rinf
.num_texture_formats
;i
++)
1301 ok
= (rinf
.texture_formats
[i
] == SDL_PIXELFORMAT_YV12
);
1305 fprintf(stderr
, "YV12 pixelformat textures not supported on renderer %s\n", rinf
.name
);
1306 SDL_DestroyRenderer(renderer
);
1311 renderer
= SDL_CreateRenderer(screen
, -1, SDL_RENDERER_SOFTWARE
);
1314 fprintf(stderr
, "SDL: could not create renderer - exiting\n");
1317 SDL_SetRenderDrawColor(renderer
, 0, 0, 0, 255);
1318 SDL_RenderFillRect(renderer
, NULL
);
1319 SDL_RenderPresent(renderer
);
1321 /* Open an audio device */
1322 device
= alcOpenDevice(NULL
);
1325 fprintf(stderr
, "OpenAL: could not open device - exiting\n");
1328 context
= alcCreateContext(device
, NULL
);
1331 fprintf(stderr
, "OpenAL: could not create context - exiting\n");
1334 if(alcMakeContextCurrent(context
) == ALC_FALSE
)
1336 fprintf(stderr
, "OpenAL: could not make context current - exiting\n");
1340 if(!alIsExtensionPresent("AL_SOFT_source_length"))
1342 fprintf(stderr
, "Required AL_SOFT_source_length not supported - exiting\n");
1346 if(!alIsExtensionPresent("AL_SOFT_source_latency"))
1347 fprintf(stderr
, "AL_SOFT_source_latency not supported, audio may be a bit laggy.\n");
1350 alGetSourcedvSOFT
= alGetProcAddress("alGetSourcedvSOFT");
1351 has_latency_check
= true;
1355 movState
= av_mallocz(sizeof(MovieState
));
1357 av_strlcpy(movState
->filename
, argv
[1], sizeof(movState
->filename
));
1359 packet_queue_init(&movState
->audio
.q
);
1360 packet_queue_init(&movState
->video
.q
);
1362 almtx_init(&movState
->video
.pictq_mutex
, almtx_plain
);
1363 alcnd_init(&movState
->video
.pictq_cond
);
1364 almtx_init(&movState
->audio
.src_mutex
, almtx_recursive
);
1366 movState
->av_sync_type
= DEFAULT_AV_SYNC_TYPE
;
1368 movState
->pFormatCtx
= avformat_alloc_context();
1369 movState
->pFormatCtx
->interrupt_callback
= (AVIOInterruptCB
){.callback
=decode_interrupt_cb
, .opaque
=movState
};
1371 if(avio_open2(&movState
->pFormatCtx
->pb
, movState
->filename
, AVIO_FLAG_READ
,
1372 &movState
->pFormatCtx
->interrupt_callback
, NULL
))
1374 fprintf(stderr
, "Failed to open %s\n", movState
->filename
);
1378 /* Open movie file */
1379 if(avformat_open_input(&movState
->pFormatCtx
, movState
->filename
, NULL
, NULL
) != 0)
1381 fprintf(stderr
, "Failed to open %s\n", movState
->filename
);
1385 /* Retrieve stream information */
1386 if(avformat_find_stream_info(movState
->pFormatCtx
, NULL
) < 0)
1388 fprintf(stderr
, "%s: failed to find stream info\n", movState
->filename
);
1392 schedule_refresh(movState
, 40);
1395 if(althrd_create(&movState
->parse_thread
, decode_thread
, movState
) != althrd_success
)
1397 fprintf(stderr
, "Failed to create parse thread!\n");
1400 while(SDL_WaitEvent(&event
) == 1)
1405 switch(event
.key
.keysym
.sym
)
1408 movState
->quit
= true;
1412 stream_seek(movState
, -10.0);
1415 stream_seek(movState
, 10.0);
1418 stream_seek(movState
, 30.0);
1421 stream_seek(movState
, -30.0);
1429 case SDL_WINDOWEVENT
:
1430 switch(event
.window
.event
)
1432 case SDL_WINDOWEVENT_RESIZED
:
1433 SDL_SetRenderDrawColor(renderer
, 0, 0, 0, 255);
1434 SDL_RenderFillRect(renderer
, NULL
);
1443 movState
->quit
= true;
1446 case FF_UPDATE_EVENT
:
1447 update_picture(event
.user
.data1
, &first_update
, screen
, renderer
);
1450 case FF_REFRESH_EVENT
:
1451 video_refresh_timer(event
.user
.data1
, screen
, renderer
);
1455 althrd_join(movState
->parse_thread
, NULL
);
1457 avformat_close_input(&movState
->pFormatCtx
);
1459 almtx_destroy(&movState
->audio
.src_mutex
);
1460 almtx_destroy(&movState
->video
.pictq_mutex
);
1461 alcnd_destroy(&movState
->video
.pictq_cond
);
1462 packet_queue_deinit(&movState
->video
.q
);
1463 packet_queue_deinit(&movState
->audio
.q
);
1465 alcMakeContextCurrent(NULL
);
1466 alcDestroyContext(context
);
1467 alcCloseDevice(device
);
1477 fprintf(stderr
, "SDL_WaitEvent error - %s\n", SDL_GetError());