58 #include "system/compat/SystemCompat.h"
59 using namespace ::br::pucrio::telemidia::ginga::core::system::compat;
61 #include "system/thread/Thread.h"
62 using namespace ::br::pucrio::telemidia::ginga::core::system::thread;
64 #include "mb/interface/IContinuousMediaProvider.h"
65 #include "mb/interface/sdl/SDLDeviceScreen.h"
76 #include "libavutil/avstring.h"
77 #include "libavutil/time.h"
78 #include "libavformat/avformat.h"
79 #include "libavdevice/avdevice.h"
80 #include "libswscale/swscale.h"
81 #include "libavutil/opt.h"
82 #include "libavcodec/avfft.h"
83 #include "libswresample/swresample.h"
86 # include "libavfilter/avcodec.h"
87 # include "libavfilter/avfilter.h"
88 # include "libavfilter/buffersink.h"
89 # include "libavfilter/buffersrc.h"
99 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
103 #define INT64_MAX (__INT64_C(9223372036854775807))
110 #include <SDL_thread.h>
113 #define ONE_HALF (1 << (SCALEBITS - 1))
114 #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
116 #define RGB_TO_Y_CCIR(r, g, b) \
117 ((FIX(0.29900*219.0/255.0) * (r) + FIX(0.58700*219.0/255.0) * (g) + \
118 FIX(0.11400*219.0/255.0) * (b) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
120 #define RGB_TO_U_CCIR(r1, g1, b1, shift)\
121 (((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 + \
122 FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS \
125 #define RGB_TO_V_CCIR(r1, g1, b1, shift)\
126 (((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 - \
127 FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + \
130 #define MAX_QUEUE_SIZE (15 * 1024 * 1024)
135 #define SDL_AUDIO_BUFFER_SIZE 1024
138 #define AV_SYNC_THRESHOLD_MIN 0.01
140 #define AV_SYNC_THRESHOLD_MAX 0.1
142 #define AV_SYNC_FRAMEDUP_THRESHOLD 0.1
144 #define AV_NOSYNC_THRESHOLD 10.0
147 #define SAMPLE_CORRECTION_PERCENT_MAX 10
150 #define EXTERNAL_CLOCK_SPEED_MIN 0.900
151 #define EXTERNAL_CLOCK_SPEED_MAX 1.010
152 #define EXTERNAL_CLOCK_SPEED_STEP 0.001
155 #define AUDIO_DIFF_AVG_NB 20
158 #define REFRESH_RATE 0.01
162 #define SAMPLE_ARRAY_SIZE (8 * 65536)
164 #define VIDEO_PICTURE_QUEUE_SIZE 3
166 #define ALPHA_BLEND(a, oldp, newp, s)\
167 ((((oldp << s) * (255 - (a))) + (newp * (a))) / (255 << s))
169 #define RGBA_IN(r, g, b, a, s) {\
170 unsigned int v = ((const uint32_t *)(s))[0];\
171 a = (v >> 24) & 0xff;\
172 r = (v >> 16) & 0xff;\
173 g = (v >> 8) & 0xff;\
177 #define YUVA_IN(y, u, v, a, s, pal) {\
178 unsigned int val = ((const uint32_t *)(pal))[*(const uint8_t*)(s)];\
179 a = (val >> 24) & 0xff;\
180 y = (val >> 16) & 0xff;\
181 u = (val >> 8) & 0xff;\
185 #define YUVA_OUT(d, y, u, v, a) {\
186 ((uint32_t *)(d))[0] = (a << 24) | (y << 16) | (u << 8) | v;\
199 namespace telemidia {
238 int64_t channel_layout;
239 enum AVSampleFormat fmt;
255 SDL_Thread *read_tid;
256 SDL_Thread *video_tid;
257 AVInputFormat *iformat;
263 int queue_attachments_req;
268 int read_pause_return;
283 int audio_clock_serial;
284 double audio_diff_cum;
285 double audio_diff_avg_coef;
286 double audio_diff_threshold;
287 int audio_diff_avg_count;
290 int audio_hw_buf_size;
291 uint8_t silence_buf[SDL_AUDIO_BUFFER_SIZE];
294 unsigned int audio_buf_size;
295 unsigned int audio_buf1_size;
297 int audio_write_buf_size;
298 int audio_buf_frames_pending;
299 AVPacket audio_pkt_temp;
301 int audio_pkt_temp_serial;
302 int audio_last_serial;
306 struct SwrContext *swr_ctx;
307 int frame_drops_early;
308 int frame_drops_late;
310 int64_t audio_frame_next_pts;
311 int16_t sample_array[SAMPLE_ARRAY_SIZE];
312 int sample_array_index;
316 FFTSample *rdft_data;
318 double last_vis_time;
321 double frame_last_returned_time;
322 double frame_last_filter_delay;
326 int64_t video_current_pos;
327 double max_frame_duration;
329 int pictq_size, pictq_rindex, pictq_windex;
330 SDL_mutex *pictq_mutex;
331 SDL_cond *pictq_cond;
332 SDL_Rect last_display_rect;
338 AVFilterContext *in_video_filter;
339 AVFilterContext *out_video_filter;
340 AVFilterContext *in_audio_filter;
341 AVFilterContext *out_audio_filter;
342 AVFilterGraph *agraph;
345 int last_video_stream, last_audio_stream;
347 SDL_cond *continue_read_thread;
352 static const short AV_SYNC_AUDIO_MASTER = 0;
353 static const short AV_SYNC_VIDEO_MASTER = 1;
354 static const short AV_SYNC_EXTERNAL_CLOCK = 2;
358 static const short ST_PLAYING = 0;
359 static const short ST_PAUSED = 1;
360 static const short ST_STOPPED = 2;
363 static const int ASD_SAMPLES = 4096;
364 static const int ASD_FREQ = 44100;
365 static const int ASD_CHANNELS = 2;
367 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
368 static const int ASD_FORMAT = AUDIO_S16LSB;
370 static const int ASD_FORMAT = AUDIO_S16MSB;
373 int wanted_stream[AVMEDIA_TYPE_NB];
378 int workaround_ffmpeg_bugs;
382 int error_concealment;
383 int decoder_reorder_pts;
396 static short refCount;
398 static pthread_mutex_t aiMutex;
399 static set<SDL2ffmpeg*> aInstances;
408 SDL_AudioSpec wantedSpec;
409 static SDL_AudioSpec spec;
412 SDL_Texture* texture;
420 int64_t mono_cb_time;
431 void close(
bool quit);
433 string ffmpegErr(
int err);
439 bool hasVideoStream();
445 void getOriginalResolution(
int* width,
int* height);
447 double getDuration();
448 double getPosition();
449 void seek(int64_t pos);
451 void setTexture(SDL_Texture* texture);
453 SDL_Texture* getTexture();
457 void setSoundLevel(
float level);
460 SDL_AudioSpec* spec,
int sample_rate, uint8_t channels);
462 static char* interleave(uint8_t* src,
int srcLen,
double ratio);
464 static char* createCVT(
465 uint8_t* src,
int srcLen,
double ratio,
int sampleSize);
467 static void clamp(
short* buf,
int len);
470 static int cmp_audio_fmts(
471 enum AVSampleFormat fmt1,
472 int64_t channel_count1,
473 enum AVSampleFormat fmt2,
474 int64_t channel_count2);
476 static int64_t get_valid_channel_layout(
477 int64_t channel_layout,
int channels);
479 int nts_packet_queue_put(
PacketQueue *q, AVPacket *pkt);
480 int packet_queue_put(
PacketQueue *q, AVPacket *pkt);
481 int packet_queue_put_nullpacket(
PacketQueue *q,
int stream_index);
487 int packet_queue_get(
488 PacketQueue *q, AVPacket *pkt,
int block,
int* serial);
494 double get_clock(
Clock* c);
495 void set_clock_at(
Clock* c,
double pts,
int serial,
double time);
496 void set_clock(
Clock* c,
double pts,
int serial);
497 void set_clock_speed(
Clock* c,
double speed);
498 void init_clock(
Clock* c,
int* queue_serial);
499 void sync_clock_to_slave(
Clock* c,
Clock* slave);
500 int get_master_sync_type();
503 double get_master_clock();
506 void check_external_clock_speed();
508 void stream_seek(int64_t pos, int64_t rel,
int seek_by_bytes);
509 void stream_toggle_pause();
511 void step_to_next_frame();
512 double compute_target_delay(
double delay);
514 void pictq_next_picture();
515 int pictq_prev_picture();
516 void update_video_pts(
double pts, int64_t pos,
int serial);
519 static void video_refresh(
void* opaque,
double* remaining_time);
520 void alloc_picture();
530 int get_video_frame(AVFrame *
frame, AVPacket* pkt,
int* serial);
533 int configure_filtergraph(
534 AVFilterGraph* graph,
535 const char* filtergraph,
536 AVFilterContext* source_ctx,
537 AVFilterContext* sink_ctx);
539 int configure_video_filters(
540 AVFilterGraph* graph,
541 const char* vfilters,
544 int configure_audio_filters(
545 const char *afilters,
546 int force_output_format);
549 static int video_thread(
void *arg);
551 int synchronize_audio(
int nb_samples);
553 int audio_decode_frame();
554 static void sdl_audio_callback(
void *opaque, Uint8 *stream,
int len);
557 int64_t wanted_channel_layout,
558 int wanted_nb_channels,
559 int wanted_sample_rate,
562 int stream_component_open(
int stream_index);
563 void stream_component_close(
int stream_index);
565 static int decode_interrupt_cb(
void *ctx);
566 static int is_realtime(AVFormatContext *s);
568 static int read_thread(
void *arg);
570 void stream_cycle_channel(
int codec_type);
572 static int lockmgr(
void **mtx,
enum AVLockOp op);
574 static AVDictionary* filter_codec_opts(
576 enum AVCodecID codec_id,
Definition: SDL2ffmpeg.h:235
Definition: SDL2ffmpeg.h:254
Definition: NetworkUtil.h:53
Definition: IContinuousMediaProvider.h:71
Definition: SDL2ffmpeg.h:219
Definition: SDL2ffmpeg.h:244
Definition: SDL2ffmpeg.h:209
Definition: SDL2ffmpeg.h:203
Definition: SDL2ffmpeg.h:350