01310af2 |
/*
* Copyright (c) 2003 Fabrice Bellard
* |
b78e7197 |
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or |
01310af2 |
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either |
b78e7197 |
* version 2.1 of the License, or (at your option) any later version. |
01310af2 |
* |
b78e7197 |
* FFmpeg is distributed in the hope that it will be useful, |
01310af2 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public |
b78e7197 |
* License along with FFmpeg; if not, write to the Free Software |
5509bffa |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
01310af2 |
*/ |
364a9607 |
|
93613338 |
/**
* @file
* simple media player based on the FFmpeg libraries
*/
|
ba11257e |
#include "config.h" |
8a3ceaf4 |
#include <inttypes.h> |
0f4e8165 |
#include <math.h>
#include <limits.h> |
73f2cf4e |
#include <signal.h> |
245976da |
#include "libavutil/avstring.h" |
2b4abbd6 |
#include "libavutil/colorspace.h" |
0ebcdf5c |
#include "libavutil/mathematics.h" |
718c7b18 |
#include "libavutil/pixdesc.h" |
7ffe76e5 |
#include "libavutil/imgutils.h" |
b6bde8c7 |
#include "libavutil/dict.h" |
7ffe76e5 |
#include "libavutil/parseutils.h"
#include "libavutil/samplefmt.h" |
f6d71b39 |
#include "libavutil/avassert.h" |
896bb0d7 |
#include "libavutil/time.h" |
245976da |
#include "libavformat/avformat.h"
#include "libavdevice/avdevice.h"
#include "libswscale/swscale.h" |
41d0eb1c |
#include "libavutil/opt.h" |
166621ab |
#include "libavcodec/avfft.h" |
1dd3c473 |
#include "libswresample/swresample.h" |
01310af2 |
|
917d2bb3 |
#if CONFIG_AVFILTER |
566666ca |
# include "libavfilter/avcodec.h" |
917d2bb3 |
# include "libavfilter/avfilter.h" |
4f7dfe12 |
# include "libavfilter/buffersink.h" |
67339f6e |
# include "libavfilter/buffersrc.h" |
917d2bb3 |
#endif
|
01310af2 |
#include <SDL.h>
#include <SDL_thread.h>
|
25c32d08 |
#include "cmdutils.h" |
31319a8c |
|
d38c9e7a |
#include <assert.h>
|
89b503b5 |
const char program_name[] = "ffplay"; |
ea9c581f |
const int program_birth_year = 2003; |
4cfac5bc |
|
79ee4683 |
#define MAX_QUEUE_SIZE (15 * 1024 * 1024)
#define MIN_FRAMES 5 |
01310af2 |
|
638c9d91 |
/* SDL audio buffer size, in samples. Should be small to have precise
A/V sync as SDL does not have hardware buffer fullness info. */
#define SDL_AUDIO_BUFFER_SIZE 1024
|
e341cb11 |
/* no AV sync correction is done if below the minimum AV sync threshold */
#define AV_SYNC_THRESHOLD_MIN 0.01
/* AV sync correction is done if above the maximum AV sync threshold */
#define AV_SYNC_THRESHOLD_MAX 0.1
/* If a frame duration is longer than this, it will not be duplicated to compensate AV sync */
#define AV_SYNC_FRAMEDUP_THRESHOLD 0.1 |
638c9d91 |
/* no AV correction is done if too big error */
#define AV_NOSYNC_THRESHOLD 10.0
/* maximum audio speed change to get correct sync */
#define SAMPLE_CORRECTION_PERCENT_MAX 10
|
747c749d |
/* external clock speed adjustment constants for realtime sources based on buffer fullness */
#define EXTERNAL_CLOCK_SPEED_MIN 0.900
#define EXTERNAL_CLOCK_SPEED_MAX 1.010
#define EXTERNAL_CLOCK_SPEED_STEP 0.001
|
638c9d91 |
/* we use about AUDIO_DIFF_AVG_NB A-V differences to make the average */
#define AUDIO_DIFF_AVG_NB 20
|
b853cfe7 |
/* polls for possible required screen refresh at least this often, should be less than 1/fps */
#define REFRESH_RATE 0.01
|
01310af2 |
/* NOTE: the size must be big enough to compensate the hardware audio buffersize size */ |
cdea19e7 |
/* TODO: We assume that a decoded and resampled frame fits into this buffer */
#define SAMPLE_ARRAY_SIZE (8 * 65536) |
01310af2 |
|
ef7f3b08 |
#define CURSOR_HIDE_DELAY 1000000
|
e2718e7a |
static int64_t sws_flags = SWS_BICUBIC; |
03ae87a3 |
|
2a4c7e65 |
typedef struct MyAVPacketList {
AVPacket pkt;
struct MyAVPacketList *next;
int serial;
} MyAVPacketList;
|
01310af2 |
typedef struct PacketQueue { |
2a4c7e65 |
MyAVPacketList *first_pkt, *last_pkt; |
01310af2 |
int nb_packets;
int size;
int abort_request; |
2a4c7e65 |
int serial; |
01310af2 |
SDL_mutex *mutex;
SDL_cond *cond;
} PacketQueue;
|
b764b53a |
#define VIDEO_PICTURE_QUEUE_SIZE 3 |
72ce053b |
#define SUBPICTURE_QUEUE_SIZE 4 |
01310af2 |
typedef struct VideoPicture { |
c1ef30a6 |
double pts; // presentation timestamp for this picture
int64_t pos; // byte position in file |
01310af2 |
SDL_Overlay *bmp;
int width, height; /* source height & width */
int allocated; |
8085a5b7 |
int reallocate; |
2a4c7e65 |
int serial; |
917d2bb3 |
|
354468fc |
AVRational sar; |
01310af2 |
} VideoPicture;
|
72ce053b |
typedef struct SubPicture {
double pts; /* presentation time stamp for this picture */
AVSubtitle sub; |
9fac752a |
int serial; |
72ce053b |
} SubPicture;
|
e1248f5c |
typedef struct AudioParams {
int freq;
int channels; |
a3fa27e3 |
int64_t channel_layout; |
e1248f5c |
enum AVSampleFormat fmt;
} AudioParams;
|
f2175a62 |
typedef struct Clock {
double pts; /* clock base */
double pts_drift; /* clock base minus time at which we updated the clock */
double last_updated;
double speed;
int serial; /* clock is based on a packet with this serial */
int paused;
int *queue_serial; /* pointer to the current packet queue serial, used for obsolete clock detection */
} Clock;
|
01310af2 |
enum {
AV_SYNC_AUDIO_MASTER, /* default choice */
AV_SYNC_VIDEO_MASTER, |
638c9d91 |
AV_SYNC_EXTERNAL_CLOCK, /* synchronize to an external clock */ |
01310af2 |
};
typedef struct VideoState { |
8adf9bb2 |
SDL_Thread *read_tid; |
01310af2 |
SDL_Thread *video_tid; |
638c9d91 |
AVInputFormat *iformat; |
01310af2 |
int no_background;
int abort_request; |
dbe7170e |
int force_refresh; |
01310af2 |
int paused; |
416e3508 |
int last_paused; |
491ca0e8 |
int queue_attachments_req; |
72ea344b |
int seek_req; |
3ba1438d |
int seek_flags; |
72ea344b |
int64_t seek_pos; |
4ed29207 |
int64_t seek_rel; |
f5668147 |
int read_pause_return; |
01310af2 |
AVFormatContext *ic; |
747c749d |
int realtime; |
e84ca8d3 |
int audio_finished;
int video_finished; |
01310af2 |
|
f2175a62 |
Clock audclk;
Clock vidclk;
Clock extclk;
|
01310af2 |
int audio_stream; |
115329f1 |
|
01310af2 |
int av_sync_type; |
115329f1 |
|
638c9d91 |
double audio_clock; |
26c208cf |
int audio_clock_serial; |
638c9d91 |
double audio_diff_cum; /* used for AV difference average computation */
double audio_diff_avg_coef;
double audio_diff_threshold;
int audio_diff_avg_count; |
01310af2 |
AVStream *audio_st;
PacketQueue audioq;
int audio_hw_buf_size; |
e2a2c49f |
uint8_t silence_buf[SDL_AUDIO_BUFFER_SIZE]; |
5a4476e2 |
uint8_t *audio_buf; |
f199f385 |
uint8_t *audio_buf1; |
7fea94ce |
unsigned int audio_buf_size; /* in bytes */ |
571ef42d |
unsigned int audio_buf1_size; |
01310af2 |
int audio_buf_index; /* in bytes */ |
10b7b4a6 |
int audio_write_buf_size; |
0b24e341 |
int audio_buf_frames_pending; |
bea18375 |
AVPacket audio_pkt_temp; |
01310af2 |
AVPacket audio_pkt; |
2a4c7e65 |
int audio_pkt_temp_serial; |
e96175ad |
int audio_last_serial; |
e1248f5c |
struct AudioParams audio_src; |
e96175ad |
#if CONFIG_AVFILTER
struct AudioParams audio_filter_src;
#endif |
e1248f5c |
struct AudioParams audio_tgt; |
1dd3c473 |
struct SwrContext *swr_ctx; |
d54af906 |
int frame_drops_early;
int frame_drops_late; |
f199f385 |
AVFrame *frame; |
b383498e |
int64_t audio_frame_next_pts; |
115329f1 |
|
54ad8e06 |
enum ShowMode { |
1d6c82d4 |
SHOW_MODE_NONE = -1, SHOW_MODE_VIDEO = 0, SHOW_MODE_WAVES, SHOW_MODE_RDFT, SHOW_MODE_NB |
f8b8c694 |
} show_mode; |
01310af2 |
int16_t sample_array[SAMPLE_ARRAY_SIZE];
int sample_array_index; |
5e0257e3 |
int last_i_start; |
166621ab |
RDFTContext *rdft; |
12eeda34 |
int rdft_bits; |
7dbbf6a1 |
FFTSample *rdft_data; |
12eeda34 |
int xpos; |
b853cfe7 |
double last_vis_time; |
115329f1 |
|
72ce053b |
SDL_Thread *subtitle_tid;
int subtitle_stream;
AVStream *subtitle_st;
PacketQueue subtitleq;
SubPicture subpq[SUBPICTURE_QUEUE_SIZE];
int subpq_size, subpq_rindex, subpq_windex;
SDL_mutex *subpq_mutex;
SDL_cond *subpq_cond; |
115329f1 |
|
638c9d91 |
double frame_timer;
double frame_last_pts; |
4e268aae |
double frame_last_duration; |
223cba6e |
double frame_last_dropped_pts; |
8f17a8ef |
double frame_last_returned_time;
double frame_last_filter_delay; |
223cba6e |
int64_t frame_last_dropped_pos; |
de81d8da |
int frame_last_dropped_serial; |
01310af2 |
int video_stream;
AVStream *video_st;
PacketQueue videoq; |
c1ef30a6 |
int64_t video_current_pos; // current displayed file pos |
f7eb50f3 |
double max_frame_duration; // maximum duration of a frame - above this, we consider the jump a timestamp discontinuity |
01310af2 |
VideoPicture pictq[VIDEO_PICTURE_QUEUE_SIZE];
int pictq_size, pictq_rindex, pictq_windex;
SDL_mutex *pictq_mutex;
SDL_cond *pictq_cond; |
917d2bb3 |
#if !CONFIG_AVFILTER |
3ac56e28 |
struct SwsContext *img_convert_ctx; |
917d2bb3 |
#endif |
65f6c42a |
SDL_Rect last_display_rect; |
115329f1 |
|
01310af2 |
char filename[1024];
int width, height, xleft, ytop; |
5db1f94b |
int step; |
41db429d |
|
917d2bb3 |
#if CONFIG_AVFILTER |
c1ef30a6 |
AVFilterContext *in_video_filter; // the first filter in the video chain
AVFilterContext *out_video_filter; // the last filter in the video chain |
e96175ad |
AVFilterContext *in_audio_filter; // the first filter in the audio chain
AVFilterContext *out_audio_filter; // the last filter in the audio chain
AVFilterGraph *agraph; // audio filter graph |
917d2bb3 |
#endif |
d38c9e7a |
|
8c9971c3 |
int last_video_stream, last_audio_stream, last_subtitle_stream; |
0e5042be |
SDL_cond *continue_read_thread; |
01310af2 |
} VideoState;
/* options specified by the user */
static AVInputFormat *file_iformat;
static const char *input_filename; |
076db5ed |
static const char *window_title; |
01310af2 |
static int fs_screen_width;
static int fs_screen_height; |
5de3f724 |
static int default_width = 640;
static int default_height = 480; |
da7c65f0 |
static int screen_width = 0; |
fccb19e3 |
static int screen_height = 0; |
01310af2 |
static int audio_disable;
static int video_disable; |
d0c6ed7d |
static int subtitle_disable; |
da7c65f0 |
static int wanted_stream[AVMEDIA_TYPE_NB] = {
[AVMEDIA_TYPE_AUDIO] = -1,
[AVMEDIA_TYPE_VIDEO] = -1,
[AVMEDIA_TYPE_SUBTITLE] = -1, |
5b369983 |
}; |
da7c65f0 |
static int seek_by_bytes = -1; |
01310af2 |
static int display_disable; |
1e1a0b18 |
static int show_status = 1; |
638c9d91 |
static int av_sync_type = AV_SYNC_AUDIO_MASTER; |
72ea344b |
static int64_t start_time = AV_NOPTS_VALUE; |
d834d63b |
static int64_t duration = AV_NOPTS_VALUE; |
6387c3e6 |
static int workaround_bugs = 1; |
6fc5b059 |
static int fast = 0; |
30bc6613 |
static int genpts = 0; |
70d54392 |
static int lowres = 0; |
1b51e051 |
static int error_concealment = 3; |
da7c65f0 |
static int decoder_reorder_pts = -1; |
2d1653b0 |
static int autoexit; |
066ce8c9 |
static int exit_on_keydown;
static int exit_on_mousedown; |
da7c65f0 |
static int loop = 1; |
41211483 |
static int framedrop = -1; |
cb3fd029 |
static int infinite_buffer = -1; |
1d6c82d4 |
static enum ShowMode show_mode = SHOW_MODE_NONE; |
5eda0967 |
static const char *audio_codec_name;
static const char *subtitle_codec_name;
static const char *video_codec_name; |
b853cfe7 |
double rdftspeed = 0.02; |
ef7f3b08 |
static int64_t cursor_last_shown;
static int cursor_hidden = 0; |
917d2bb3 |
#if CONFIG_AVFILTER
static char *vfilters = NULL; |
e96175ad |
static char *afilters = NULL; |
917d2bb3 |
#endif |
01310af2 |
/* current context */
static int is_full_screen; |
5e0257e3 |
static int64_t audio_callback_time; |
01310af2 |
|
2c676c33 |
static AVPacket flush_pkt; |
39c6a118 |
|
01310af2 |
#define FF_ALLOC_EVENT (SDL_USEREVENT) |
638c9d91 |
#define FF_QUIT_EVENT (SDL_USEREVENT + 2) |
01310af2 |
|
2c676c33 |
static SDL_Surface *screen; |
01310af2 |
|
e96175ad |
static inline
int cmp_audio_fmts(enum AVSampleFormat fmt1, int64_t channel_count1,
enum AVSampleFormat fmt2, int64_t channel_count2)
{
/* If channel count == 1, planar and non-planar formats are the same */
if (channel_count1 == 1 && channel_count2 == 1)
return av_get_packed_sample_fmt(fmt1) != av_get_packed_sample_fmt(fmt2);
else
return channel_count1 != channel_count2 || fmt1 != fmt2;
}
static inline
int64_t get_valid_channel_layout(int64_t channel_layout, int channels)
{
if (channel_layout && av_get_channel_layout_nb_channels(channel_layout) == channels)
return channel_layout;
else
return 0;
}
|
515bd00e |
static int packet_queue_put(PacketQueue *q, AVPacket *pkt); |
dad09ff9 |
|
a687acbb |
static int packet_queue_put_private(PacketQueue *q, AVPacket *pkt) |
eef16966 |
{ |
2a4c7e65 |
MyAVPacketList *pkt1; |
eef16966 |
|
a687acbb |
if (q->abort_request)
return -1; |
eef16966 |
|
2a4c7e65 |
pkt1 = av_malloc(sizeof(MyAVPacketList)); |
eef16966 |
if (!pkt1)
return -1;
pkt1->pkt = *pkt;
pkt1->next = NULL; |
2a4c7e65 |
if (pkt == &flush_pkt)
q->serial++;
pkt1->serial = q->serial; |
eef16966 |
if (!q->last_pkt)
q->first_pkt = pkt1;
else
q->last_pkt->next = pkt1;
q->last_pkt = pkt1;
q->nb_packets++;
q->size += pkt1->pkt.size + sizeof(*pkt1);
/* XXX: should duplicate packet data in DV case */
SDL_CondSignal(q->cond); |
a687acbb |
return 0;
}
static int packet_queue_put(PacketQueue *q, AVPacket *pkt)
{
int ret; |
eef16966 |
|
a687acbb |
/* duplicate the packet */
if (pkt != &flush_pkt && av_dup_packet(pkt) < 0)
return -1;
SDL_LockMutex(q->mutex);
ret = packet_queue_put_private(q, pkt); |
eef16966 |
SDL_UnlockMutex(q->mutex); |
a687acbb |
if (pkt != &flush_pkt && ret < 0)
av_free_packet(pkt);
return ret; |
eef16966 |
} |
515bd00e |
|
b118d3e2 |
static int packet_queue_put_nullpacket(PacketQueue *q, int stream_index)
{
AVPacket pkt1, *pkt = &pkt1;
av_init_packet(pkt);
pkt->data = NULL;
pkt->size = 0;
pkt->stream_index = stream_index;
return packet_queue_put(q, pkt);
}
|
01310af2 |
/* packet queue handling */
static void packet_queue_init(PacketQueue *q)
{
memset(q, 0, sizeof(PacketQueue));
q->mutex = SDL_CreateMutex();
q->cond = SDL_CreateCond(); |
a687acbb |
q->abort_request = 1; |
01310af2 |
}
|
72ea344b |
static void packet_queue_flush(PacketQueue *q) |
01310af2 |
{ |
2a4c7e65 |
MyAVPacketList *pkt, *pkt1; |
01310af2 |
|
687fae2b |
SDL_LockMutex(q->mutex); |
da7c65f0 |
for (pkt = q->first_pkt; pkt != NULL; pkt = pkt1) { |
01310af2 |
pkt1 = pkt->next;
av_free_packet(&pkt->pkt); |
da6c4573 |
av_freep(&pkt); |
01310af2 |
} |
72ea344b |
q->last_pkt = NULL;
q->first_pkt = NULL;
q->nb_packets = 0;
q->size = 0; |
687fae2b |
SDL_UnlockMutex(q->mutex); |
72ea344b |
}
|
a687acbb |
static void packet_queue_destroy(PacketQueue *q) |
72ea344b |
{
packet_queue_flush(q); |
01310af2 |
SDL_DestroyMutex(q->mutex);
SDL_DestroyCond(q->cond);
}
static void packet_queue_abort(PacketQueue *q)
{
SDL_LockMutex(q->mutex);
q->abort_request = 1; |
115329f1 |
|
01310af2 |
SDL_CondSignal(q->cond);
SDL_UnlockMutex(q->mutex);
}
|
a687acbb |
static void packet_queue_start(PacketQueue *q)
{
SDL_LockMutex(q->mutex);
q->abort_request = 0;
packet_queue_put_private(q, &flush_pkt);
SDL_UnlockMutex(q->mutex);
}
|
01310af2 |
/* return < 0 if aborted, 0 if no packet and > 0 if packet. */ |
2a4c7e65 |
static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block, int *serial) |
01310af2 |
{ |
2a4c7e65 |
MyAVPacketList *pkt1; |
01310af2 |
int ret;
SDL_LockMutex(q->mutex);
|
da7c65f0 |
for (;;) { |
01310af2 |
if (q->abort_request) {
ret = -1;
break;
} |
115329f1 |
|
01310af2 |
pkt1 = q->first_pkt;
if (pkt1) {
q->first_pkt = pkt1->next;
if (!q->first_pkt)
q->last_pkt = NULL;
q->nb_packets--; |
7b776589 |
q->size -= pkt1->pkt.size + sizeof(*pkt1); |
01310af2 |
*pkt = pkt1->pkt; |
2a4c7e65 |
if (serial)
*serial = pkt1->serial; |
01310af2 |
av_free(pkt1);
ret = 1;
break;
} else if (!block) {
ret = 0;
break;
} else {
SDL_CondWait(q->cond, q->mutex);
}
}
SDL_UnlockMutex(q->mutex);
return ret;
}
|
115329f1 |
static inline void fill_rectangle(SDL_Surface *screen, |
00b70f8d |
int x, int y, int w, int h, int color, int update) |
01310af2 |
{
SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.w = w;
rect.h = h;
SDL_FillRect(screen, &rect, color); |
00b70f8d |
if (update && w > 0 && h > 0)
SDL_UpdateRect(screen, x, y, w, h); |
01310af2 |
}
|
65f6c42a |
/* draw only the border of a rectangle */
static void fill_border(int xleft, int ytop, int width, int height, int x, int y, int w, int h, int color, int update)
{
int w1, w2, h1, h2;
/* fill the background */
w1 = x;
if (w1 < 0)
w1 = 0;
w2 = width - (x + w);
if (w2 < 0)
w2 = 0;
h1 = y;
if (h1 < 0)
h1 = 0;
h2 = height - (y + h);
if (h2 < 0)
h2 = 0;
fill_rectangle(screen,
xleft, ytop,
w1, height,
color, update);
fill_rectangle(screen,
xleft + width - w2, ytop,
w2, height,
color, update);
fill_rectangle(screen,
xleft + w1, ytop,
width - w1 - w2, h1,
color, update);
fill_rectangle(screen,
xleft + w1, ytop + height - h2,
width - w1 - w2, h2,
color, update);
}
|
72ce053b |
#define ALPHA_BLEND(a, oldp, newp, s)\
((((oldp << s) * (255 - (a))) + (newp * (a))) / (255 << s))
#define RGBA_IN(r, g, b, a, s)\
{\
unsigned int v = ((const uint32_t *)(s))[0];\
a = (v >> 24) & 0xff;\
r = (v >> 16) & 0xff;\
g = (v >> 8) & 0xff;\
b = v & 0xff;\
}
#define YUVA_IN(y, u, v, a, s, pal)\
{\ |
57cf99f2 |
unsigned int val = ((const uint32_t *)(pal))[*(const uint8_t*)(s)];\ |
72ce053b |
a = (val >> 24) & 0xff;\
y = (val >> 16) & 0xff;\
u = (val >> 8) & 0xff;\
v = val & 0xff;\
}
#define YUVA_OUT(d, y, u, v, a)\
{\
((uint32_t *)(d))[0] = (a << 24) | (y << 16) | (u << 8) | v;\
}
#define BPP 1
|
0a8cd696 |
static void blend_subrect(AVPicture *dst, const AVSubtitleRect *rect, int imgw, int imgh) |
72ce053b |
{
int wrap, wrap3, width2, skip2;
int y, u, v, a, u1, v1, a1, w, h;
uint8_t *lum, *cb, *cr;
const uint8_t *p;
const uint32_t *pal; |
9cb5a11e |
int dstx, dsty, dstw, dsth;
|
7cf9c6ae |
dstw = av_clip(rect->w, 0, imgw);
dsth = av_clip(rect->h, 0, imgh);
dstx = av_clip(rect->x, 0, imgw - dstw);
dsty = av_clip(rect->y, 0, imgh - dsth); |
9cb5a11e |
lum = dst->data[0] + dsty * dst->linesize[0]; |
da7c65f0 |
cb = dst->data[1] + (dsty >> 1) * dst->linesize[1];
cr = dst->data[2] + (dsty >> 1) * dst->linesize[2]; |
9cb5a11e |
|
f54b31b9 |
width2 = ((dstw + 1) >> 1) + (dstx & ~dstw & 1); |
9cb5a11e |
skip2 = dstx >> 1; |
72ce053b |
wrap = dst->linesize[0]; |
25b4c651 |
wrap3 = rect->pict.linesize[0];
p = rect->pict.data[0];
pal = (const uint32_t *)rect->pict.data[1]; /* Now in YCrCb! */ |
115329f1 |
|
9cb5a11e |
if (dsty & 1) {
lum += dstx; |
72ce053b |
cb += skip2;
cr += skip2; |
115329f1 |
|
9cb5a11e |
if (dstx & 1) { |
72ce053b |
YUVA_IN(y, u, v, a, p, pal);
lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
cb++;
cr++;
lum++;
p += BPP;
} |
da7c65f0 |
for (w = dstw - (dstx & 1); w >= 2; w -= 2) { |
72ce053b |
YUVA_IN(y, u, v, a, p, pal);
u1 = u;
v1 = v;
a1 = a;
lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
YUVA_IN(y, u, v, a, p + BPP, pal);
u1 += u;
v1 += v;
a1 += a;
lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
cb++;
cr++;
p += 2 * BPP;
lum += 2;
}
if (w) {
YUVA_IN(y, u, v, a, p, pal);
lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0); |
676ef505 |
p++;
lum++; |
72ce053b |
} |
4606a059 |
p += wrap3 - dstw * BPP;
lum += wrap - dstw - dstx; |
72ce053b |
cb += dst->linesize[1] - width2 - skip2;
cr += dst->linesize[2] - width2 - skip2;
} |
da7c65f0 |
for (h = dsth - (dsty & 1); h >= 2; h -= 2) { |
9cb5a11e |
lum += dstx; |
72ce053b |
cb += skip2;
cr += skip2; |
115329f1 |
|
9cb5a11e |
if (dstx & 1) { |
72ce053b |
YUVA_IN(y, u, v, a, p, pal);
u1 = u;
v1 = v;
a1 = a;
lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
p += wrap3;
lum += wrap;
YUVA_IN(y, u, v, a, p, pal);
u1 += u;
v1 += v;
a1 += a;
lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
cb++;
cr++;
p += -wrap3 + BPP;
lum += -wrap + 1;
} |
da7c65f0 |
for (w = dstw - (dstx & 1); w >= 2; w -= 2) { |
72ce053b |
YUVA_IN(y, u, v, a, p, pal);
u1 = u;
v1 = v;
a1 = a;
lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
|
f8ca63e8 |
YUVA_IN(y, u, v, a, p + BPP, pal); |
72ce053b |
u1 += u;
v1 += v;
a1 += a;
lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
p += wrap3;
lum += wrap;
YUVA_IN(y, u, v, a, p, pal);
u1 += u;
v1 += v;
a1 += a;
lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
|
f8ca63e8 |
YUVA_IN(y, u, v, a, p + BPP, pal); |
72ce053b |
u1 += u;
v1 += v;
a1 += a;
lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 2);
cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 2);
cb++;
cr++;
p += -wrap3 + 2 * BPP;
lum += -wrap + 2;
}
if (w) {
YUVA_IN(y, u, v, a, p, pal);
u1 = u;
v1 = v;
a1 = a;
lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
p += wrap3;
lum += wrap;
YUVA_IN(y, u, v, a, p, pal);
u1 += u;
v1 += v;
a1 += a;
lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
cb++;
cr++;
p += -wrap3 + BPP;
lum += -wrap + 1;
} |
9cb5a11e |
p += wrap3 + (wrap3 - dstw * BPP);
lum += wrap + (wrap - dstw - dstx); |
72ce053b |
cb += dst->linesize[1] - width2 - skip2;
cr += dst->linesize[2] - width2 - skip2;
}
/* handle odd height */
if (h) { |
9cb5a11e |
lum += dstx; |
72ce053b |
cb += skip2;
cr += skip2; |
115329f1 |
|
9cb5a11e |
if (dstx & 1) { |
72ce053b |
YUVA_IN(y, u, v, a, p, pal);
lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
cb++;
cr++;
lum++;
p += BPP;
} |
da7c65f0 |
for (w = dstw - (dstx & 1); w >= 2; w -= 2) { |
72ce053b |
YUVA_IN(y, u, v, a, p, pal);
u1 = u;
v1 = v;
a1 = a;
lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
YUVA_IN(y, u, v, a, p + BPP, pal);
u1 += u;
v1 += v;
a1 += a;
lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u, 1);
cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v, 1);
cb++;
cr++;
p += 2 * BPP;
lum += 2;
}
if (w) {
YUVA_IN(y, u, v, a, p, pal);
lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
}
}
}
|
2d059d8d |
static void free_picture(VideoPicture *vp)
{
if (vp->bmp) {
SDL_FreeYUVOverlay(vp->bmp);
vp->bmp = NULL;
}
}
|
72ce053b |
static void free_subpicture(SubPicture *sp)
{ |
e1d7c883 |
avsubtitle_free(&sp->sub); |
72ce053b |
}
|
bd14d845 |
static void calculate_display_rect(SDL_Rect *rect, int scr_xleft, int scr_ytop, int scr_width, int scr_height, VideoPicture *vp)
{
float aspect_ratio;
int width, height, x, y;
|
32fdfdfb |
if (vp->sar.num == 0) |
bd14d845 |
aspect_ratio = 0;
else |
32fdfdfb |
aspect_ratio = av_q2d(vp->sar); |
bd14d845 |
if (aspect_ratio <= 0.0)
aspect_ratio = 1.0;
aspect_ratio *= (float)vp->width / (float)vp->height;
/* XXX: we suppose the screen has a 1.0 pixel ratio */
height = scr_height;
width = ((int)rint(height * aspect_ratio)) & ~1;
if (width > scr_width) {
width = scr_width;
height = ((int)rint(width / aspect_ratio)) & ~1;
}
x = (scr_width - width) / 2;
y = (scr_height - height) / 2;
rect->x = scr_xleft + x;
rect->y = scr_ytop + y;
rect->w = FFMAX(width, 1);
rect->h = FFMAX(height, 1);
}
|
01310af2 |
static void video_image_display(VideoState *is)
{
VideoPicture *vp; |
72ce053b |
SubPicture *sp;
AVPicture pict; |
01310af2 |
SDL_Rect rect; |
72ce053b |
int i; |
01310af2 |
vp = &is->pictq[is->pictq_rindex];
if (vp->bmp) { |
df149f6c |
if (is->subtitle_st) {
if (is->subpq_size > 0) { |
72ce053b |
sp = &is->subpq[is->subpq_rindex];
|
df149f6c |
if (vp->pts >= sp->pts + ((float) sp->sub.start_display_time / 1000)) { |
72ce053b |
SDL_LockYUVOverlay (vp->bmp);
pict.data[0] = vp->bmp->pixels[0];
pict.data[1] = vp->bmp->pixels[2];
pict.data[2] = vp->bmp->pixels[1];
pict.linesize[0] = vp->bmp->pitches[0];
pict.linesize[1] = vp->bmp->pitches[2];
pict.linesize[2] = vp->bmp->pitches[1];
for (i = 0; i < sp->sub.num_rects; i++) |
db4fac64 |
blend_subrect(&pict, sp->sub.rects[i], |
0a8cd696 |
vp->bmp->w, vp->bmp->h); |
72ce053b |
SDL_UnlockYUVOverlay (vp->bmp);
}
}
}
|
bd14d845 |
calculate_display_rect(&rect, is->xleft, is->ytop, is->width, is->height, vp); |
72ce053b |
|
01310af2 |
SDL_DisplayYUVOverlay(vp->bmp, &rect); |
65f6c42a |
if (rect.x != is->last_display_rect.x || rect.y != is->last_display_rect.y || rect.w != is->last_display_rect.w || rect.h != is->last_display_rect.h || is->force_refresh) {
int bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
fill_border(is->xleft, is->ytop, is->width, is->height, rect.x, rect.y, rect.w, rect.h, bgcolor, 1);
is->last_display_rect = rect;
} |
01310af2 |
}
}
static inline int compute_mod(int a, int b)
{ |
91b27e49 |
return a < 0 ? a%b + b : a%b; |
01310af2 |
}
static void video_audio_display(VideoState *s)
{
int i, i_start, x, y1, y, ys, delay, n, nb_display_channels;
int ch, channels, h, h2, bgcolor, fgcolor; |
92b50b71 |
int64_t time_diff; |
4c7c7645 |
int rdft_bits, nb_freq;
|
da7c65f0 |
for (rdft_bits = 1; (1 << rdft_bits) < 2 * s->height; rdft_bits++) |
4c7c7645 |
; |
da7c65f0 |
nb_freq = 1 << (rdft_bits - 1); |
115329f1 |
|
01310af2 |
/* compute display index : center on currently output samples */ |
e1248f5c |
channels = s->audio_tgt.channels; |
01310af2 |
nb_display_channels = channels; |
5e0257e3 |
if (!s->paused) { |
f8b8c694 |
int data_used= s->show_mode == SHOW_MODE_WAVES ? s->width : (2*nb_freq); |
5e0257e3 |
n = 2 * channels; |
10b7b4a6 |
delay = s->audio_write_buf_size; |
5e0257e3 |
delay /= n; |
115329f1 |
|
5e0257e3 |
/* to be more precise, we take into account the time spent since
the last buffer computation */
if (audio_callback_time) {
time_diff = av_gettime() - audio_callback_time; |
e1248f5c |
delay -= (time_diff * s->audio_tgt.freq) / 1000000; |
5e0257e3 |
} |
115329f1 |
|
da7c65f0 |
delay += 2 * data_used; |
4c7c7645 |
if (delay < data_used)
delay = data_used; |
ac50bcc8 |
i_start= x = compute_mod(s->sample_array_index - delay * channels, SAMPLE_ARRAY_SIZE); |
f8b8c694 |
if (s->show_mode == SHOW_MODE_WAVES) { |
da7c65f0 |
h = INT_MIN;
for (i = 0; i < 1000; i += channels) {
int idx = (SAMPLE_ARRAY_SIZE + x - i) % SAMPLE_ARRAY_SIZE;
int a = s->sample_array[idx];
int b = s->sample_array[(idx + 4 * channels) % SAMPLE_ARRAY_SIZE];
int c = s->sample_array[(idx + 5 * channels) % SAMPLE_ARRAY_SIZE];
int d = s->sample_array[(idx + 9 * channels) % SAMPLE_ARRAY_SIZE];
int score = a - d;
if (h < score && (b ^ c) < 0) {
h = score;
i_start = idx; |
6c7165c7 |
} |
ac50bcc8 |
}
}
|
5e0257e3 |
s->last_i_start = i_start;
} else {
i_start = s->last_i_start; |
01310af2 |
}
bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00); |
f8b8c694 |
if (s->show_mode == SHOW_MODE_WAVES) { |
6c7165c7 |
fill_rectangle(screen,
s->xleft, s->ytop, s->width, s->height, |
00b70f8d |
bgcolor, 0); |
6c7165c7 |
fgcolor = SDL_MapRGB(screen->format, 0xff, 0xff, 0xff);
/* total height for one channel */
h = s->height / nb_display_channels;
/* graph height / 2 */
h2 = (h * 9) / 20; |
da7c65f0 |
for (ch = 0; ch < nb_display_channels; ch++) { |
6c7165c7 |
i = i_start + ch;
y1 = s->ytop + ch * h + (h / 2); /* position of center line */ |
da7c65f0 |
for (x = 0; x < s->width; x++) { |
6c7165c7 |
y = (s->sample_array[i] * h2) >> 15;
if (y < 0) {
y = -y;
ys = y1 - y;
} else {
ys = y1;
}
fill_rectangle(screen,
s->xleft + x, ys, 1, y, |
00b70f8d |
fgcolor, 0); |
6c7165c7 |
i += channels;
if (i >= SAMPLE_ARRAY_SIZE)
i -= SAMPLE_ARRAY_SIZE; |
01310af2 |
}
}
|
6c7165c7 |
fgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0xff); |
01310af2 |
|
da7c65f0 |
for (ch = 1; ch < nb_display_channels; ch++) { |
6c7165c7 |
y = s->ytop + ch * h;
fill_rectangle(screen,
s->xleft, y, s->width, 1, |
00b70f8d |
fgcolor, 0); |
6c7165c7 |
}
SDL_UpdateRect(screen, s->xleft, s->ytop, s->width, s->height); |
da7c65f0 |
} else { |
12eeda34 |
nb_display_channels= FFMIN(nb_display_channels, 2); |
da7c65f0 |
if (rdft_bits != s->rdft_bits) { |
166621ab |
av_rdft_end(s->rdft); |
7dbbf6a1 |
av_free(s->rdft_data); |
166621ab |
s->rdft = av_rdft_init(rdft_bits, DFT_R2C); |
da7c65f0 |
s->rdft_bits = rdft_bits;
s->rdft_data = av_malloc(4 * nb_freq * sizeof(*s->rdft_data)); |
12eeda34 |
}
{ |
7dbbf6a1 |
FFTSample *data[2]; |
da7c65f0 |
for (ch = 0; ch < nb_display_channels; ch++) {
data[ch] = s->rdft_data + 2 * nb_freq * ch; |
12eeda34 |
i = i_start + ch; |
da7c65f0 |
for (x = 0; x < 2 * nb_freq; x++) {
double w = (x-nb_freq) * (1.0 / nb_freq);
data[ch][x] = s->sample_array[i] * (1.0 - w * w); |
12eeda34 |
i += channels;
if (i >= SAMPLE_ARRAY_SIZE)
i -= SAMPLE_ARRAY_SIZE;
} |
166621ab |
av_rdft_calc(s->rdft, data[ch]); |
12eeda34 |
} |
03039f4c |
/* Least efficient way to do this, we should of course
* directly access it but it is more than fast enough. */ |
da7c65f0 |
for (y = 0; y < s->height; y++) {
double w = 1 / sqrt(nb_freq);
int a = sqrt(w * sqrt(data[0][2 * y + 0] * data[0][2 * y + 0] + data[0][2 * y + 1] * data[0][2 * y + 1]));
int b = (nb_display_channels == 2 ) ? sqrt(w * sqrt(data[1][2 * y + 0] * data[1][2 * y + 0]
+ data[1][2 * y + 1] * data[1][2 * y + 1])) : a;
a = FFMIN(a, 255);
b = FFMIN(b, 255);
fgcolor = SDL_MapRGB(screen->format, a, b, (a + b) / 2); |
12eeda34 |
fill_rectangle(screen,
s->xpos, s->height-y, 1, 1, |
00b70f8d |
fgcolor, 0); |
12eeda34 |
}
}
SDL_UpdateRect(screen, s->xpos, s->ytop, 1, s->height); |
e6093e36 |
if (!s->paused)
s->xpos++; |
da7c65f0 |
if (s->xpos >= s->width) |
12eeda34 |
s->xpos= s->xleft;
} |
01310af2 |
}
|
d5708923 |
static void stream_close(VideoState *is)
{
int i;
/* XXX: use a special url_shutdown call to abort parse cleanly */
is->abort_request = 1;
SDL_WaitThread(is->read_tid, NULL); |
a687acbb |
packet_queue_destroy(&is->videoq);
packet_queue_destroy(&is->audioq);
packet_queue_destroy(&is->subtitleq); |
d5708923 |
/* free all pictures */ |
2d059d8d |
for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++)
free_picture(&is->pictq[i]); |
9f120e03 |
for (i = 0; i < SUBPICTURE_QUEUE_SIZE; i++)
free_subpicture(&is->subpq[i]); |
d5708923 |
SDL_DestroyMutex(is->pictq_mutex);
SDL_DestroyCond(is->pictq_cond);
SDL_DestroyMutex(is->subpq_mutex);
SDL_DestroyCond(is->subpq_cond); |
0e5042be |
SDL_DestroyCond(is->continue_read_thread); |
d5708923 |
#if !CONFIG_AVFILTER |
c91f1f3f |
sws_freeContext(is->img_convert_ctx); |
d5708923 |
#endif
av_free(is);
}
|
84506ebd |
static void do_exit(VideoState *is) |
d5708923 |
{ |
84506ebd |
if (is) {
stream_close(is); |
d5708923 |
} |
ee0ff051 |
av_lockmgr_register(NULL); |
d5708923 |
uninit_opts();
#if CONFIG_AVFILTER |
55594447 |
av_freep(&vfilters); |
d5708923 |
#endif |
13b7781e |
avformat_network_deinit(); |
d5708923 |
if (show_status)
printf("\n");
SDL_Quit(); |
4a34e54b |
av_log(NULL, AV_LOG_QUIET, "%s", ""); |
d5708923 |
exit(0);
}
|
73f2cf4e |
static void sigterm_handler(int sig)
{
exit(123);
}
|
5de3f724 |
static int video_open(VideoState *is, int force_set_video_mode, VideoPicture *vp) |
da7c65f0 |
{
int flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL; |
990c8438 |
int w,h; |
255c7bb1 |
SDL_Rect rect; |
990c8438 |
|
da7c65f0 |
if (is_full_screen) flags |= SDL_FULLSCREEN;
else flags |= SDL_RESIZABLE; |
fb84155b |
|
5de3f724 |
if (vp && vp->width) {
calculate_display_rect(&rect, 0, 0, INT_MAX, vp->height, vp);
default_width = rect.w;
default_height = rect.h;
}
|
990c8438 |
if (is_full_screen && fs_screen_width) {
w = fs_screen_width;
h = fs_screen_height; |
da7c65f0 |
} else if (!is_full_screen && screen_width) { |
fb84155b |
w = screen_width;
h = screen_height; |
990c8438 |
} else { |
5de3f724 |
w = default_width;
h = default_height; |
990c8438 |
} |
87917a32 |
w = FFMIN(16383, w); |
da7c65f0 |
if (screen && is->width == screen->w && screen->w == w |
9fb2b412 |
&& is->height== screen->h && screen->h == h && !force_set_video_mode) |
d3d7b12e |
return 0; |
990c8438 |
screen = SDL_SetVideoMode(w, h, 0, flags);
if (!screen) { |
3b491c5a |
av_log(NULL, AV_LOG_FATAL, "SDL: could not set video mode - exiting\n"); |
84506ebd |
do_exit(is); |
990c8438 |
} |
076db5ed |
if (!window_title)
window_title = input_filename;
SDL_WM_SetCaption(window_title, window_title); |
990c8438 |
|
da7c65f0 |
is->width = screen->w; |
990c8438 |
is->height = screen->h;
return 0;
} |
8c982c5d |
|
01310af2 |
/* display the current picture, if any */
static void video_display(VideoState *is)
{ |
da7c65f0 |
if (!screen) |
5de3f724 |
video_open(is, 0, NULL); |
f8b8c694 |
if (is->audio_st && is->show_mode != SHOW_MODE_VIDEO) |
01310af2 |
video_audio_display(is);
else if (is->video_st)
video_image_display(is);
}
|
f2175a62 |
static double get_clock(Clock *c) |
638c9d91 |
{ |
f2175a62 |
if (*c->queue_serial != c->serial) |
26c208cf |
return NAN; |
f2175a62 |
if (c->paused) {
return c->pts; |
10b7b4a6 |
} else { |
f2175a62 |
double time = av_gettime() / 1000000.0;
return c->pts_drift + time - (time - c->last_updated) * (1.0 - c->speed); |
638c9d91 |
}
}
|
f2175a62 |
static void set_clock_at(Clock *c, double pts, int serial, double time) |
638c9d91 |
{ |
f2175a62 |
c->pts = pts;
c->last_updated = time;
c->pts_drift = c->pts - time;
c->serial = serial; |
638c9d91 |
}
|
f2175a62 |
static void set_clock(Clock *c, double pts, int serial) |
638c9d91 |
{ |
f2175a62 |
double time = av_gettime() / 1000000.0;
set_clock_at(c, pts, serial, time);
}
static void set_clock_speed(Clock *c, double speed)
{
set_clock(c, get_clock(c), c->serial);
c->speed = speed;
}
static void init_clock(Clock *c, int *queue_serial)
{
c->speed = 1.0;
c->paused = 0;
c->queue_serial = queue_serial;
set_clock(c, NAN, -1);
}
static void sync_clock_to_slave(Clock *c, Clock *slave)
{
double clock = get_clock(c);
double slave_clock = get_clock(slave);
if (!isnan(slave_clock) && (isnan(clock) || fabs(clock - slave_clock) > AV_NOSYNC_THRESHOLD))
set_clock(c, slave_clock, slave->serial); |
638c9d91 |
}
|
fca16a15 |
static int get_master_sync_type(VideoState *is) { |
72ea344b |
if (is->av_sync_type == AV_SYNC_VIDEO_MASTER) {
if (is->video_st) |
fca16a15 |
return AV_SYNC_VIDEO_MASTER; |
72ea344b |
else |
fca16a15 |
return AV_SYNC_AUDIO_MASTER; |
72ea344b |
} else if (is->av_sync_type == AV_SYNC_AUDIO_MASTER) {
if (is->audio_st) |
fca16a15 |
return AV_SYNC_AUDIO_MASTER; |
72ea344b |
else |
3166a6fc |
return AV_SYNC_EXTERNAL_CLOCK; |
72ea344b |
} else { |
fca16a15 |
return AV_SYNC_EXTERNAL_CLOCK;
}
}
/* get the current master clock value */
static double get_master_clock(VideoState *is)
{
double val;
switch (get_master_sync_type(is)) {
case AV_SYNC_VIDEO_MASTER: |
f2175a62 |
val = get_clock(&is->vidclk); |
fca16a15 |
break;
case AV_SYNC_AUDIO_MASTER: |
f2175a62 |
val = get_clock(&is->audclk); |
fca16a15 |
break;
default: |
f2175a62 |
val = get_clock(&is->extclk); |
fca16a15 |
break; |
72ea344b |
} |
638c9d91 |
return val;
}
|
747c749d |
static void check_external_clock_speed(VideoState *is) {
if (is->video_stream >= 0 && is->videoq.nb_packets <= MIN_FRAMES / 2 ||
is->audio_stream >= 0 && is->audioq.nb_packets <= MIN_FRAMES / 2) { |
f2175a62 |
set_clock_speed(&is->extclk, FFMAX(EXTERNAL_CLOCK_SPEED_MIN, is->extclk.speed - EXTERNAL_CLOCK_SPEED_STEP)); |
747c749d |
} else if ((is->video_stream < 0 || is->videoq.nb_packets > MIN_FRAMES * 2) &&
(is->audio_stream < 0 || is->audioq.nb_packets > MIN_FRAMES * 2)) { |
f2175a62 |
set_clock_speed(&is->extclk, FFMIN(EXTERNAL_CLOCK_SPEED_MAX, is->extclk.speed + EXTERNAL_CLOCK_SPEED_STEP)); |
747c749d |
} else { |
f2175a62 |
double speed = is->extclk.speed; |
747c749d |
if (speed != 1.0) |
f2175a62 |
set_clock_speed(&is->extclk, speed + EXTERNAL_CLOCK_SPEED_STEP * (1.0 - speed) / fabs(1.0 - speed)); |
747c749d |
}
}
|
72ea344b |
/* seek in the stream */ |
2ef46053 |
static void stream_seek(VideoState *is, int64_t pos, int64_t rel, int seek_by_bytes) |
72ea344b |
{ |
687fae2b |
if (!is->seek_req) {
is->seek_pos = pos; |
4ed29207 |
is->seek_rel = rel; |
3890dd3a |
is->seek_flags &= ~AVSEEK_FLAG_BYTE; |
94b594c6 |
if (seek_by_bytes)
is->seek_flags |= AVSEEK_FLAG_BYTE; |
687fae2b |
is->seek_req = 1; |
4be7d578 |
SDL_CondSignal(is->continue_read_thread); |
687fae2b |
} |
72ea344b |
}
/* pause or resume the video */ |
ab7fdbab |
static void stream_toggle_pause(VideoState *is) |
72ea344b |
{ |
68aefbe8 |
if (is->paused) { |
f2175a62 |
is->frame_timer += av_gettime() / 1000000.0 + is->vidclk.pts_drift - is->vidclk.pts; |
da7c65f0 |
if (is->read_pause_return != AVERROR(ENOSYS)) { |
f2175a62 |
is->vidclk.paused = 0; |
f5668147 |
} |
f2175a62 |
set_clock(&is->vidclk, get_clock(&is->vidclk), is->vidclk.serial); |
72ea344b |
} |
f2175a62 |
set_clock(&is->extclk, get_clock(&is->extclk), is->extclk.serial);
is->paused = is->audclk.paused = is->vidclk.paused = is->extclk.paused = !is->paused; |
72ea344b |
}
|
c5eab4bb |
static void toggle_pause(VideoState *is)
{
stream_toggle_pause(is);
is->step = 0;
}
static void step_to_next_frame(VideoState *is)
{
/* if the stream is paused unpause it, then step */
if (is->paused)
stream_toggle_pause(is);
is->step = 1;
}
|
4e268aae |
static double compute_target_delay(double delay, VideoState *is) |
49410784 |
{ |
4e268aae |
double sync_threshold, diff; |
49410784 |
/* update delay to follow master synchronisation source */ |
d30c6925 |
if (get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER) { |
49410784 |
/* if video is slave, we try to correct big delays by
duplicating or deleting a frame */ |
f2175a62 |
diff = get_clock(&is->vidclk) - get_master_clock(is); |
49410784 |
/* skip or repeat frame. We take into account the
delay to compute the threshold. I still don't know
if it is the best guess */ |
e341cb11 |
sync_threshold = FFMAX(AV_SYNC_THRESHOLD_MIN, FFMIN(AV_SYNC_THRESHOLD_MAX, delay));
if (!isnan(diff) && fabs(diff) < is->max_frame_duration) { |
49410784 |
if (diff <= -sync_threshold) |
e341cb11 |
delay = FFMAX(0, delay + diff);
else if (diff >= sync_threshold && delay > AV_SYNC_FRAMEDUP_THRESHOLD)
delay = delay + diff; |
49410784 |
else if (diff >= sync_threshold)
delay = 2 * delay;
}
} |
8543f0f9 |
|
4e268aae |
av_dlog(NULL, "video: delay=%0.3f A-V=%f\n",
delay, -diff);
return delay;
}
static void pictq_next_picture(VideoState *is) {
/* update queue size and signal for next picture */
if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE)
is->pictq_rindex = 0; |
eecc17a7 |
|
4e268aae |
SDL_LockMutex(is->pictq_mutex);
is->pictq_size--;
SDL_CondSignal(is->pictq_cond);
SDL_UnlockMutex(is->pictq_mutex); |
49410784 |
}
|
36dc476f |
static int pictq_prev_picture(VideoState *is) { |
4a45e722 |
VideoPicture *prevvp; |
36dc476f |
int ret = 0; |
4a45e722 |
/* update queue size and signal for the previous picture */
prevvp = &is->pictq[(is->pictq_rindex + VIDEO_PICTURE_QUEUE_SIZE - 1) % VIDEO_PICTURE_QUEUE_SIZE]; |
1427c684 |
if (prevvp->allocated && prevvp->serial == is->videoq.serial) { |
4a45e722 |
SDL_LockMutex(is->pictq_mutex); |
b764b53a |
if (is->pictq_size < VIDEO_PICTURE_QUEUE_SIZE) { |
4a45e722 |
if (--is->pictq_rindex == -1)
is->pictq_rindex = VIDEO_PICTURE_QUEUE_SIZE - 1;
is->pictq_size++; |
36dc476f |
ret = 1; |
4a45e722 |
}
SDL_CondSignal(is->pictq_cond);
SDL_UnlockMutex(is->pictq_mutex);
} |
36dc476f |
return ret; |
4a45e722 |
}
|
2a4c7e65 |
static void update_video_pts(VideoState *is, double pts, int64_t pos, int serial) { |
223cba6e |
/* update current video pts */ |
f2175a62 |
set_clock(&is->vidclk, pts, serial);
sync_clock_to_slave(&is->extclk, &is->vidclk); |
223cba6e |
is->video_current_pos = pos;
is->frame_last_pts = pts;
}
|
01310af2 |
/* called to display each frame */ |
b853cfe7 |
static void video_refresh(void *opaque, double *remaining_time) |
01310af2 |
{
VideoState *is = opaque;
VideoPicture *vp; |
223cba6e |
double time; |
638c9d91 |
|
72ce053b |
SubPicture *sp, *sp2; |
01310af2 |
|
747c749d |
if (!is->paused && get_master_sync_type(is) == AV_SYNC_EXTERNAL_CLOCK && is->realtime)
check_external_clock_speed(is);
|
b853cfe7 |
if (!display_disable && is->show_mode != SHOW_MODE_VIDEO && is->audio_st) {
time = av_gettime() / 1000000.0;
if (is->force_refresh || is->last_vis_time + rdftspeed < time) {
video_display(is);
is->last_vis_time = time;
}
*remaining_time = FFMIN(*remaining_time, is->last_vis_time + rdftspeed - time);
} |
ec89ea30 |
|
01310af2 |
if (is->video_st) { |
38f64966 |
int redisplay = 0; |
81f26d69 |
if (is->force_refresh) |
38f64966 |
redisplay = pictq_prev_picture(is); |
d38c9e7a |
retry: |
01310af2 |
if (is->pictq_size == 0) { |
223cba6e |
SDL_LockMutex(is->pictq_mutex);
if (is->frame_last_dropped_pts != AV_NOPTS_VALUE && is->frame_last_dropped_pts > is->frame_last_pts) { |
de81d8da |
update_video_pts(is, is->frame_last_dropped_pts, is->frame_last_dropped_pos, is->frame_last_dropped_serial); |
223cba6e |
is->frame_last_dropped_pts = AV_NOPTS_VALUE;
}
SDL_UnlockMutex(is->pictq_mutex); |
491ca0e8 |
// nothing to do, no picture to display in the queue |
01310af2 |
} else { |
4e268aae |
double last_duration, duration, delay; |
638c9d91 |
/* dequeue the picture */ |
01310af2 |
vp = &is->pictq[is->pictq_rindex]; |
638c9d91 |
|
1427c684 |
if (vp->serial != is->videoq.serial) { |
4e268aae |
pictq_next_picture(is); |
38f64966 |
redisplay = 0; |
4e268aae |
goto retry;
}
|
dbe7170e |
if (is->paused)
goto display;
|
4e268aae |
/* compute nominal last_duration */
last_duration = vp->pts - is->frame_last_pts; |
d148339d |
if (!isnan(last_duration) && last_duration > 0 && last_duration < is->max_frame_duration) { |
4e268aae |
/* if duration of the last frame was sane, update last_duration in video state */
is->frame_last_duration = last_duration;
} |
3b6f1526 |
if (redisplay)
delay = 0.0;
else
delay = compute_target_delay(is->frame_last_duration, is); |
4e268aae |
|
223cba6e |
time= av_gettime()/1000000.0; |
b764b53a |
if (time < is->frame_timer + delay && !redisplay) { |
b853cfe7 |
*remaining_time = FFMIN(is->frame_timer + delay - time, *remaining_time); |
d38c9e7a |
return; |
b853cfe7 |
} |
4e268aae |
|
97e42551 |
is->frame_timer += delay;
if (delay > 0 && time - is->frame_timer > AV_SYNC_THRESHOLD_MAX)
is->frame_timer = time; |
4e268aae |
|
223cba6e |
SDL_LockMutex(is->pictq_mutex); |
35b2f30f |
if (!redisplay && !isnan(vp->pts)) |
d148339d |
update_video_pts(is, vp->pts, vp->pos, vp->serial); |
223cba6e |
SDL_UnlockMutex(is->pictq_mutex); |
4e268aae |
|
da7c65f0 |
if (is->pictq_size > 1) {
VideoPicture *nextvp = &is->pictq[(is->pictq_rindex + 1) % VIDEO_PICTURE_QUEUE_SIZE]; |
d872e113 |
duration = nextvp->pts - vp->pts; |
38f64966 |
if(!is->step && (redisplay || framedrop>0 || (framedrop && get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER)) && time > is->frame_timer + duration){
if (!redisplay)
is->frame_drops_late++; |
4e268aae |
pictq_next_picture(is); |
38f64966 |
redisplay = 0; |
d38c9e7a |
goto retry;
}
} |
638c9d91 |
|
da7c65f0 |
if (is->subtitle_st) { |
9fac752a |
while (is->subpq_size > 0) { |
72ce053b |
sp = &is->subpq[is->subpq_rindex];
if (is->subpq_size > 1)
sp2 = &is->subpq[(is->subpq_rindex + 1) % SUBPICTURE_QUEUE_SIZE];
else
sp2 = NULL;
|
9fac752a |
if (sp->serial != is->subtitleq.serial
|| (is->vidclk.pts > (sp->pts + ((float) sp->sub.end_display_time / 1000))) |
05f0d531 |
|| (sp2 && is->vidclk.pts > (sp2->pts + ((float) sp2->sub.start_display_time / 1000)))) |
72ce053b |
{
free_subpicture(sp);
/* update queue size and signal for next picture */
if (++is->subpq_rindex == SUBPICTURE_QUEUE_SIZE)
is->subpq_rindex = 0;
SDL_LockMutex(is->subpq_mutex);
is->subpq_size--;
SDL_CondSignal(is->subpq_cond);
SDL_UnlockMutex(is->subpq_mutex); |
9fac752a |
} else {
break; |
72ce053b |
}
}
}
|
dbe7170e |
display: |
01310af2 |
/* display picture */ |
ec89ea30 |
if (!display_disable && is->show_mode == SHOW_MODE_VIDEO) |
24d13ebc |
video_display(is); |
115329f1 |
|
4a45e722 |
pictq_next_picture(is); |
97660b8b |
if (is->step && !is->paused)
stream_toggle_pause(is); |
01310af2 |
}
} |
dbe7170e |
is->force_refresh = 0; |
01310af2 |
if (show_status) {
static int64_t last_time;
int64_t cur_time; |
72ce053b |
int aqsize, vqsize, sqsize; |
638c9d91 |
double av_diff; |
115329f1 |
|
01310af2 |
cur_time = av_gettime(); |
1e1a0b18 |
if (!last_time || (cur_time - last_time) >= 30000) { |
01310af2 |
aqsize = 0;
vqsize = 0; |
72ce053b |
sqsize = 0; |
01310af2 |
if (is->audio_st)
aqsize = is->audioq.size;
if (is->video_st)
vqsize = is->videoq.size; |
72ce053b |
if (is->subtitle_st)
sqsize = is->subtitleq.size; |
638c9d91 |
av_diff = 0;
if (is->audio_st && is->video_st) |
f2175a62 |
av_diff = get_clock(&is->audclk) - get_clock(&is->vidclk); |
5b492720 |
else if (is->video_st)
av_diff = get_master_clock(is) - get_clock(&is->vidclk);
else if (is->audio_st)
av_diff = get_master_clock(is) - get_clock(&is->audclk); |
3b491c5a |
av_log(NULL, AV_LOG_INFO,
"%7.2f %s:%7.3f fd=%4d aq=%5dKB vq=%5dKB sq=%5dB f=%"PRId64"/%"PRId64" \r", |
d6705a27 |
get_master_clock(is), |
5b492720 |
(is->audio_st && is->video_st) ? "A-V" : (is->video_st ? "M-V" : (is->audio_st ? "M-A" : " ")), |
d6705a27 |
av_diff, |
d54af906 |
is->frame_drops_early + is->frame_drops_late, |
d6705a27 |
aqsize / 1024,
vqsize / 1024,
sqsize,
is->video_st ? is->video_st->codec->pts_correction_num_faulty_dts : 0,
is->video_st ? is->video_st->codec->pts_correction_num_faulty_pts : 0); |
01310af2 |
fflush(stdout);
last_time = cur_time;
}
}
}
/* allocate a picture (needs to do that in main thread to avoid
potential locking problems */ |
99b01e45 |
static void alloc_picture(VideoState *is) |
01310af2 |
{
VideoPicture *vp; |
30d724bd |
int64_t bufferdiff; |
01310af2 |
vp = &is->pictq[is->pictq_windex];
|
2d059d8d |
free_picture(vp); |
01310af2 |
|
5de3f724 |
video_open(is, 0, vp); |
843509e2 |
|
917d2bb3 |
vp->bmp = SDL_CreateYUVOverlay(vp->width, vp->height, |
115329f1 |
SDL_YV12_OVERLAY, |
61890b02 |
screen); |
30d724bd |
bufferdiff = vp->bmp ? FFMAX(vp->bmp->pixels[0], vp->bmp->pixels[1]) - FFMIN(vp->bmp->pixels[0], vp->bmp->pixels[1]) : 0; |
f73d6d2a |
if (!vp->bmp || vp->bmp->pitches[0] < vp->width || bufferdiff < (int64_t)vp->height * vp->bmp->pitches[0]) { |
cb036f90 |
/* SDL allocates a buffer smaller than requested if the video
* overlay hardware is unable to support the requested size. */ |
3b491c5a |
av_log(NULL, AV_LOG_FATAL,
"Error: the video system does not support an image\n" |
70d54392 |
"size of %dx%d pixels. Try using -lowres or -vf \"scale=w:h\"\n" |
cb036f90 |
"to reduce the image size.\n", vp->width, vp->height ); |
84506ebd |
do_exit(is); |
cb036f90 |
} |
01310af2 |
SDL_LockMutex(is->pictq_mutex);
vp->allocated = 1;
SDL_CondSignal(is->pictq_cond);
SDL_UnlockMutex(is->pictq_mutex);
}
|
cf0c63d9 |
static void duplicate_right_border_pixels(SDL_Overlay *bmp) {
int i, width, height;
Uint8 *p, *maxp;
for (i = 0; i < 3; i++) {
width = bmp->w;
height = bmp->h;
if (i > 0) {
width >>= 1;
height >>= 1;
}
if (bmp->pitches[i] > width) {
maxp = bmp->pixels[i] + bmp->pitches[i] * height - 1;
for (p = bmp->pixels[i] + width - 1; p < maxp; p += bmp->pitches[i])
*(p+1) = *p;
}
}
}
|
5534d8f7 |
static int queue_picture(VideoState *is, AVFrame *src_frame, double pts, int64_t pos, int serial) |
01310af2 |
{
VideoPicture *vp; |
c2606259 |
#if defined(DEBUG_SYNC) && 0 |
5534d8f7 |
printf("frame_type=%c pts=%0.3f\n",
av_get_picture_type_char(src_frame->pict_type), pts); |
c2606259 |
#endif |
a6f395d6 |
|
01310af2 |
/* wait until we have space to put a new picture */
SDL_LockMutex(is->pictq_mutex); |
d38c9e7a |
|
4a45e722 |
/* keep the last already displayed picture in the queue */ |
b764b53a |
while (is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE - 1 && |
01310af2 |
!is->videoq.abort_request) {
SDL_CondWait(is->pictq_cond, is->pictq_mutex);
}
SDL_UnlockMutex(is->pictq_mutex); |
115329f1 |
|
01310af2 |
if (is->videoq.abort_request)
return -1;
vp = &is->pictq[is->pictq_windex];
|
32fdfdfb |
vp->sar = src_frame->sample_aspect_ratio; |
99b01e45 |
|
01310af2 |
/* alloc or resize hardware picture buffer */ |
99b01e45 |
if (!vp->bmp || vp->reallocate || !vp->allocated || |
843509e2 |
vp->width != src_frame->width ||
vp->height != src_frame->height) { |
01310af2 |
SDL_Event event;
|
fec5777e |
vp->allocated = 0; |
8085a5b7 |
vp->reallocate = 0; |
99b01e45 |
vp->width = src_frame->width;
vp->height = src_frame->height; |
01310af2 |
/* the allocation must be done in the main thread to avoid |
99b01e45 |
locking problems. */ |
01310af2 |
event.type = FF_ALLOC_EVENT; |
99b01e45 |
event.user.data1 = is; |
01310af2 |
SDL_PushEvent(&event); |
115329f1 |
|
01310af2 |
/* wait until the picture is allocated */
SDL_LockMutex(is->pictq_mutex);
while (!vp->allocated && !is->videoq.abort_request) {
SDL_CondWait(is->pictq_cond, is->pictq_mutex);
} |
0a1cf662 |
/* if the queue is aborted, we have to pop the pending ALLOC event or wait for the allocation to complete */
if (is->videoq.abort_request && SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_EVENTMASK(FF_ALLOC_EVENT)) != 1) {
while (!vp->allocated) {
SDL_CondWait(is->pictq_cond, is->pictq_mutex);
}
} |
01310af2 |
SDL_UnlockMutex(is->pictq_mutex);
if (is->videoq.abort_request)
return -1;
}
|
638c9d91 |
/* if the frame is not skipped, then display it */ |
01310af2 |
if (vp->bmp) { |
a92be9b8 |
AVPicture pict = { { 0 } }; |
fbf1b885 |
|
01310af2 |
/* get a pointer on the bitmap */
SDL_LockYUVOverlay (vp->bmp);
pict.data[0] = vp->bmp->pixels[0];
pict.data[1] = vp->bmp->pixels[2];
pict.data[2] = vp->bmp->pixels[1];
pict.linesize[0] = vp->bmp->pitches[0];
pict.linesize[1] = vp->bmp->pitches[2];
pict.linesize[2] = vp->bmp->pitches[1]; |
917d2bb3 |
#if CONFIG_AVFILTER |
da7c65f0 |
// FIXME use direct rendering |
a6f395d6 |
av_picture_copy(&pict, (AVPicture *)src_frame, |
f1a75aa0 |
src_frame->format, vp->width, vp->height); |
917d2bb3 |
#else |
e2718e7a |
av_opt_get_int(sws_opts, "sws_flags", 0, &sws_flags); |
3ac56e28 |
is->img_convert_ctx = sws_getCachedContext(is->img_convert_ctx, |
f1a75aa0 |
vp->width, vp->height, src_frame->format, vp->width, vp->height, |
ac627b3d |
AV_PIX_FMT_YUV420P, sws_flags, NULL, NULL, NULL); |
3ac56e28 |
if (is->img_convert_ctx == NULL) { |
3b491c5a |
av_log(NULL, AV_LOG_FATAL, "Cannot initialize the conversion context\n"); |
26ba8235 |
exit(1);
} |
3ac56e28 |
sws_scale(is->img_convert_ctx, src_frame->data, src_frame->linesize, |
917d2bb3 |
0, vp->height, pict.data, pict.linesize);
#endif |
cf0c63d9 |
/* workaround SDL PITCH_WORKAROUND */
duplicate_right_border_pixels(vp->bmp); |
01310af2 |
/* update the bitmap content */
SDL_UnlockYUVOverlay(vp->bmp);
|
638c9d91 |
vp->pts = pts; |
1a620dd7 |
vp->pos = pos; |
2a4c7e65 |
vp->serial = serial; |
01310af2 |
/* now we can update the picture count */
if (++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE)
is->pictq_windex = 0;
SDL_LockMutex(is->pictq_mutex);
is->pictq_size++;
SDL_UnlockMutex(is->pictq_mutex);
} |
638c9d91 |
return 0;
}
|
85b9bf56 |
static int get_video_frame(VideoState *is, AVFrame *frame, AVPacket *pkt, int *serial) |
01310af2 |
{ |
1427c684 |
int got_picture; |
01310af2 |
|
2a4c7e65 |
if (packet_queue_get(&is->videoq, pkt, 1, serial) < 0) |
199c18a7 |
return -1; |
6c7d3ead |
|
199c18a7 |
if (pkt->data == flush_pkt.data) {
avcodec_flush_buffers(is->video_st->codec); |
6c7d3ead |
|
199c18a7 |
SDL_LockMutex(is->pictq_mutex); |
491ca0e8 |
// Make sure there are no long delay timers (ideally we should just flush the queue but that's harder) |
199c18a7 |
while (is->pictq_size && !is->videoq.abort_request) {
SDL_CondWait(is->pictq_cond, is->pictq_mutex); |
39c6a118 |
} |
199c18a7 |
is->video_current_pos = -1;
is->frame_last_pts = AV_NOPTS_VALUE; |
4e268aae |
is->frame_last_duration = 0; |
199c18a7 |
is->frame_timer = (double)av_gettime() / 1000000.0; |
223cba6e |
is->frame_last_dropped_pts = AV_NOPTS_VALUE;
SDL_UnlockMutex(is->pictq_mutex); |
199c18a7 |
return 0;
} |
7a8bfa5d |
|
349b65ee |
if(avcodec_decode_video2(is->video_st->codec, frame, &got_picture, pkt) < 0) |
143a5390 |
return 0; |
199c18a7 |
|
e84ca8d3 |
if (!got_picture && !pkt->data)
is->video_finished = *serial;
|
199c18a7 |
if (got_picture) { |
223cba6e |
int ret = 1; |
d148339d |
double dpts = NAN; |
223cba6e |
|
199c18a7 |
if (decoder_reorder_pts == -1) { |
85b9bf56 |
frame->pts = av_frame_get_best_effort_timestamp(frame); |
199c18a7 |
} else if (decoder_reorder_pts) { |
85b9bf56 |
frame->pts = frame->pkt_pts; |
199c18a7 |
} else { |
85b9bf56 |
frame->pts = frame->pkt_dts; |
199c18a7 |
}
|
d148339d |
if (frame->pts != AV_NOPTS_VALUE)
dpts = av_q2d(is->video_st->time_base) * frame->pts; |
41db429d |
|
40693ba3 |
frame->sample_aspect_ratio = av_guess_sample_aspect_ratio(is->ic, is->video_st, frame);
|
d30c6925 |
if (framedrop>0 || (framedrop && get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER)) { |
223cba6e |
SDL_LockMutex(is->pictq_mutex); |
d148339d |
if (is->frame_last_pts != AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE) { |
f2175a62 |
double clockdiff = get_clock(&is->vidclk) - get_master_clock(is); |
223cba6e |
double ptsdiff = dpts - is->frame_last_pts; |
26c208cf |
if (!isnan(clockdiff) && fabs(clockdiff) < AV_NOSYNC_THRESHOLD && |
d148339d |
!isnan(ptsdiff) && ptsdiff > 0 && ptsdiff < AV_NOSYNC_THRESHOLD && |
b8facbee |
clockdiff + ptsdiff - is->frame_last_filter_delay < 0 &&
is->videoq.nb_packets) { |
04de0e04 |
is->frame_last_dropped_pos = av_frame_get_pkt_pos(frame); |
223cba6e |
is->frame_last_dropped_pts = dpts; |
de81d8da |
is->frame_last_dropped_serial = *serial; |
d54af906 |
is->frame_drops_early++; |
c46a8c61 |
av_frame_unref(frame); |
223cba6e |
ret = 0;
}
}
SDL_UnlockMutex(is->pictq_mutex);
} |
d38c9e7a |
|
223cba6e |
return ret; |
d38c9e7a |
} |
917d2bb3 |
return 0;
}
#if CONFIG_AVFILTER |
79a7451d |
static int configure_filtergraph(AVFilterGraph *graph, const char *filtergraph,
AVFilterContext *source_ctx, AVFilterContext *sink_ctx)
{
int ret;
AVFilterInOut *outputs = NULL, *inputs = NULL;
if (filtergraph) {
outputs = avfilter_inout_alloc();
inputs = avfilter_inout_alloc();
if (!outputs || !inputs) {
ret = AVERROR(ENOMEM);
goto fail;
}
outputs->name = av_strdup("in");
outputs->filter_ctx = source_ctx;
outputs->pad_idx = 0;
outputs->next = NULL;
inputs->name = av_strdup("out");
inputs->filter_ctx = sink_ctx;
inputs->pad_idx = 0;
inputs->next = NULL;
|
838bd731 |
if ((ret = avfilter_graph_parse_ptr(graph, filtergraph, &inputs, &outputs, NULL)) < 0) |
79a7451d |
goto fail;
} else {
if ((ret = avfilter_link(source_ctx, 0, sink_ctx, 0)) < 0)
goto fail;
}
|
abd49a75 |
ret = avfilter_graph_config(graph, NULL); |
79a7451d |
fail:
avfilter_inout_free(&outputs);
avfilter_inout_free(&inputs);
return ret;
}
|
3f72dbe4 |
static int configure_video_filters(AVFilterGraph *graph, VideoState *is, const char *vfilters, AVFrame *frame) |
917d2bb3 |
{ |
ac627b3d |
static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE }; |
3f073fa2 |
char sws_flags_str[128]; |
67339f6e |
char buffersrc_args[256]; |
8904a0f1 |
int ret; |
09214f49 |
AVFilterContext *filt_src = NULL, *filt_out = NULL, *filt_crop; |
67339f6e |
AVCodecContext *codec = is->video_st->codec; |
612d9cdb |
AVRational fr = av_guess_frame_rate(is->ic, is->video_st, NULL); |
67339f6e |
|
2d8d63a9 |
av_opt_get_int(sws_opts, "sws_flags", 0, &sws_flags); |
e2718e7a |
snprintf(sws_flags_str, sizeof(sws_flags_str), "flags=%"PRId64, sws_flags); |
3f073fa2 |
graph->scale_sws_opts = av_strdup(sws_flags_str); |
917d2bb3 |
|
251f3987 |
snprintf(buffersrc_args, sizeof(buffersrc_args),
"video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d", |
3f72dbe4 |
frame->width, frame->height, frame->format, |
67339f6e |
is->video_st->time_base.num, is->video_st->time_base.den, |
a63d7213 |
codec->sample_aspect_ratio.num, FFMAX(codec->sample_aspect_ratio.den, 1)); |
612d9cdb |
if (fr.num && fr.den)
av_strlcatf(buffersrc_args, sizeof(buffersrc_args), ":frame_rate=%d/%d", fr.num, fr.den); |
67339f6e |
if ((ret = avfilter_graph_create_filter(&filt_src,
avfilter_get_by_name("buffer"), |
8f45c314 |
"ffplay_buffer", buffersrc_args, NULL, |
67339f6e |
graph)) < 0) |
8cb74024 |
goto fail; |
3c5fe5b5 |
|
61930bd0 |
ret = avfilter_graph_create_filter(&filt_out, |
ceac5c54 |
avfilter_get_by_name("buffersink"), |
e87c1cdb |
"ffplay_buffersink", NULL, NULL, graph); |
c4415f6e |
if (ret < 0) |
8cb74024 |
goto fail; |
917d2bb3 |
|
e87c1cdb |
if ((ret = av_opt_set_int_list(filt_out, "pix_fmts", pix_fmts, AV_PIX_FMT_NONE, AV_OPT_SEARCH_CHILDREN)) < 0)
goto fail;
|
a366beaf |
/* SDL YUV code is not handling odd width/height for some driver
* combinations, therefore we crop the picture to an even width/height. */
if ((ret = avfilter_graph_create_filter(&filt_crop,
avfilter_get_by_name("crop"),
"ffplay_crop", "floor(in_w/2)*2:floor(in_h/2)*2", NULL, graph)) < 0) |
8cb74024 |
goto fail; |
09214f49 |
if ((ret = avfilter_link(filt_crop, 0, filt_out, 0)) < 0) |
8cb74024 |
goto fail; |
ac712309 |
|
a366beaf |
if ((ret = configure_filtergraph(graph, vfilters, filt_src, filt_crop)) < 0) |
8cb74024 |
goto fail; |
917d2bb3 |
|
67339f6e |
is->in_video_filter = filt_src; |
917d2bb3 |
is->out_video_filter = filt_out; |
71a1d111 |
|
8cb74024 |
fail: |
8904a0f1 |
return ret;
}
|
e96175ad |
static int configure_audio_filters(VideoState *is, const char *afilters, int force_output_format)
{ |
4d16ba2d |
static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE }; |
e96175ad |
int sample_rates[2] = { 0, -1 };
int64_t channel_layouts[2] = { 0, -1 };
int channels[2] = { 0, -1 };
AVFilterContext *filt_asrc = NULL, *filt_asink = NULL; |
44758b4d |
char aresample_swr_opts[512] = "";
AVDictionaryEntry *e = NULL; |
e96175ad |
char asrc_args[256];
int ret;
avfilter_graph_free(&is->agraph);
if (!(is->agraph = avfilter_graph_alloc()))
return AVERROR(ENOMEM);
|
44758b4d |
while ((e = av_dict_get(swr_opts, "", e, AV_DICT_IGNORE_SUFFIX)))
av_strlcatf(aresample_swr_opts, sizeof(aresample_swr_opts), "%s=%s:", e->key, e->value);
if (strlen(aresample_swr_opts))
aresample_swr_opts[strlen(aresample_swr_opts)-1] = '\0';
av_opt_set(is->agraph, "aresample_swr_opts", aresample_swr_opts, 0);
|
e96175ad |
ret = snprintf(asrc_args, sizeof(asrc_args), |
325846aa |
"sample_rate=%d:sample_fmt=%s:channels=%d:time_base=%d/%d", |
e96175ad |
is->audio_filter_src.freq, av_get_sample_fmt_name(is->audio_filter_src.fmt), |
325846aa |
is->audio_filter_src.channels,
1, is->audio_filter_src.freq); |
e96175ad |
if (is->audio_filter_src.channel_layout)
snprintf(asrc_args + ret, sizeof(asrc_args) - ret,
":channel_layout=0x%"PRIx64, is->audio_filter_src.channel_layout);
ret = avfilter_graph_create_filter(&filt_asrc,
avfilter_get_by_name("abuffer"), "ffplay_abuffer",
asrc_args, NULL, is->agraph);
if (ret < 0)
goto end;
|
e87c1cdb |
ret = avfilter_graph_create_filter(&filt_asink,
avfilter_get_by_name("abuffersink"), "ffplay_abuffersink",
NULL, NULL, is->agraph);
if (ret < 0)
goto end;
if ((ret = av_opt_set_int_list(filt_asink, "sample_fmts", sample_fmts, AV_SAMPLE_FMT_NONE, AV_OPT_SEARCH_CHILDREN)) < 0)
goto end;
if ((ret = av_opt_set_int(filt_asink, "all_channel_counts", 1, AV_OPT_SEARCH_CHILDREN)) < 0) |
e96175ad |
goto end;
if (force_output_format) {
channel_layouts[0] = is->audio_tgt.channel_layout; |
e87c1cdb |
channels [0] = is->audio_tgt.channels;
sample_rates [0] = is->audio_tgt.freq;
if ((ret = av_opt_set_int(filt_asink, "all_channel_counts", 0, AV_OPT_SEARCH_CHILDREN)) < 0)
goto end;
if ((ret = av_opt_set_int_list(filt_asink, "channel_layouts", channel_layouts, -1, AV_OPT_SEARCH_CHILDREN)) < 0)
goto end;
if ((ret = av_opt_set_int_list(filt_asink, "channel_counts" , channels , -1, AV_OPT_SEARCH_CHILDREN)) < 0)
goto end;
if ((ret = av_opt_set_int_list(filt_asink, "sample_rates" , sample_rates , -1, AV_OPT_SEARCH_CHILDREN)) < 0)
goto end; |
e96175ad |
}
if ((ret = configure_filtergraph(is->agraph, afilters, filt_asrc, filt_asink)) < 0)
goto end;
is->in_audio_filter = filt_asrc;
is->out_audio_filter = filt_asink;
end:
if (ret < 0)
avfilter_graph_free(&is->agraph);
return ret;
} |
8904a0f1 |
#endif /* CONFIG_AVFILTER */
static int video_thread(void *arg)
{ |
906f9dce |
AVPacket pkt = { 0 }; |
8904a0f1 |
VideoState *is = arg; |
354468fc |
AVFrame *frame = av_frame_alloc(); |
8904a0f1 |
double pts;
int ret; |
2a4c7e65 |
int serial = 0; |
8904a0f1 |
#if CONFIG_AVFILTER
AVFilterGraph *graph = avfilter_graph_alloc(); |
67339f6e |
AVFilterContext *filt_out = NULL, *filt_in = NULL; |
58f22f70 |
int last_w = 0;
int last_h = 0; |
ac627b3d |
enum AVPixelFormat last_format = -2; |
f65e5712 |
int last_serial = -1; |
917d2bb3 |
#endif
|
da7c65f0 |
for (;;) { |
917d2bb3 |
while (is->paused && !is->videoq.abort_request)
SDL_Delay(10); |
67339f6e |
|
cb819338 |
avcodec_get_frame_defaults(frame); |
906f9dce |
av_free_packet(&pkt);
|
85b9bf56 |
ret = get_video_frame(is, frame, &pkt, &serial); |
67339f6e |
if (ret < 0)
goto the_end;
if (!ret)
continue;
|
917d2bb3 |
#if CONFIG_AVFILTER |
230bef0a |
if ( last_w != frame->width
|| last_h != frame->height |
f65e5712 |
|| last_format != frame->format
|| last_serial != serial) { |
212bb649 |
av_log(NULL, AV_LOG_DEBUG, |
58c4b0f9 |
"Video frame changed from size:%dx%d format:%s serial:%d to size:%dx%d format:%s serial:%d\n",
last_w, last_h,
(const char *)av_x_if_null(av_get_pix_fmt_name(last_format), "none"), last_serial,
frame->width, frame->height,
(const char *)av_x_if_null(av_get_pix_fmt_name(frame->format), "none"), serial); |
428c59d9 |
avfilter_graph_free(&graph);
graph = avfilter_graph_alloc(); |
3f72dbe4 |
if ((ret = configure_video_filters(graph, is, vfilters, frame)) < 0) { |
58f22f70 |
SDL_Event event;
event.type = FF_QUIT_EVENT;
event.user.data1 = is;
SDL_PushEvent(&event); |
c9651d4b |
av_free_packet(&pkt); |
428c59d9 |
goto the_end; |
c9651d4b |
} |
cd947e9a |
filt_in = is->in_video_filter; |
428c59d9 |
filt_out = is->out_video_filter; |
230bef0a |
last_w = frame->width;
last_h = frame->height;
last_format = frame->format; |
f65e5712 |
last_serial = serial; |
428c59d9 |
} |
67339f6e |
|
354468fc |
ret = av_buffersrc_add_frame(filt_in, frame);
if (ret < 0)
goto the_end; |
32fdfdfb |
av_frame_unref(frame);
avcodec_get_frame_defaults(frame); |
c9651d4b |
av_free_packet(&pkt);
|
67339f6e |
while (ret >= 0) { |
a78bc6f7 |
is->frame_last_returned_time = av_gettime() / 1000000.0;
|
32fdfdfb |
ret = av_buffersink_get_frame_flags(filt_out, frame, 0); |
67339f6e |
if (ret < 0) { |
18be3fac |
if (ret == AVERROR_EOF)
is->video_finished = serial; |
67339f6e |
ret = 0;
break;
}
|
a78bc6f7 |
is->frame_last_filter_delay = av_gettime() / 1000000.0 - is->frame_last_returned_time;
if (fabs(is->frame_last_filter_delay) > AV_NOSYNC_THRESHOLD / 10.0)
is->frame_last_filter_delay = 0;
|
d148339d |
pts = (frame->pts == AV_NOPTS_VALUE) ? NAN : frame->pts * av_q2d(filt_out->inputs[0]->time_base); |
85b9bf56 |
ret = queue_picture(is, frame, pts, av_frame_get_pkt_pos(frame), serial); |
32fdfdfb |
av_frame_unref(frame); |
387b4ac9 |
} |
917d2bb3 |
#else |
d148339d |
pts = (frame->pts == AV_NOPTS_VALUE) ? NAN : frame->pts * av_q2d(is->video_st->time_base); |
04de0e04 |
ret = queue_picture(is, frame, pts, av_frame_get_pkt_pos(frame), serial); |
32fdfdfb |
av_frame_unref(frame); |
917d2bb3 |
#endif |
b93e12fd |
|
917d2bb3 |
if (ret < 0)
goto the_end; |
01310af2 |
}
the_end: |
c2e8691c |
avcodec_flush_buffers(is->video_st->codec); |
917d2bb3 |
#if CONFIG_AVFILTER |
ab543afe |
avfilter_graph_free(&graph); |
917d2bb3 |
#endif |
906f9dce |
av_free_packet(&pkt); |
354468fc |
av_frame_free(&frame); |
01310af2 |
return 0;
}
|
72ce053b |
static int subtitle_thread(void *arg)
{
VideoState *is = arg;
SubPicture *sp;
AVPacket pkt1, *pkt = &pkt1; |
98704131 |
int got_subtitle; |
9fac752a |
int serial; |
72ce053b |
double pts;
int i, j;
int r, g, b, y, u, v, a;
|
da7c65f0 |
for (;;) { |
72ce053b |
while (is->paused && !is->subtitleq.abort_request) {
SDL_Delay(10);
} |
9fac752a |
if (packet_queue_get(&is->subtitleq, pkt, 1, &serial) < 0) |
72ce053b |
break; |
115329f1 |
|
da7c65f0 |
if (pkt->data == flush_pkt.data) { |
39c6a118 |
avcodec_flush_buffers(is->subtitle_st->codec);
continue;
} |
72ce053b |
SDL_LockMutex(is->subpq_mutex);
while (is->subpq_size >= SUBPICTURE_QUEUE_SIZE &&
!is->subtitleq.abort_request) {
SDL_CondWait(is->subpq_cond, is->subpq_mutex);
}
SDL_UnlockMutex(is->subpq_mutex); |
115329f1 |
|
72ce053b |
if (is->subtitleq.abort_request) |
71a1d111 |
return 0; |
115329f1 |
|
72ce053b |
sp = &is->subpq[is->subpq_windex];
/* NOTE: ipts is the PTS of the _first_ picture beginning in
this packet, if any */
pts = 0;
if (pkt->pts != AV_NOPTS_VALUE) |
da7c65f0 |
pts = av_q2d(is->subtitle_st->time_base) * pkt->pts; |
72ce053b |
|
98704131 |
avcodec_decode_subtitle2(is->subtitle_st->codec, &sp->sub,
&got_subtitle, pkt); |
72ce053b |
if (got_subtitle && sp->sub.format == 0) { |
2939e258 |
if (sp->sub.pts != AV_NOPTS_VALUE)
pts = sp->sub.pts / (double)AV_TIME_BASE; |
72ce053b |
sp->pts = pts; |
9fac752a |
sp->serial = serial; |
115329f1 |
|
72ce053b |
for (i = 0; i < sp->sub.num_rects; i++)
{ |
db4fac64 |
for (j = 0; j < sp->sub.rects[i]->nb_colors; j++) |
72ce053b |
{ |
25b4c651 |
RGBA_IN(r, g, b, a, (uint32_t*)sp->sub.rects[i]->pict.data[1] + j); |
72ce053b |
y = RGB_TO_Y_CCIR(r, g, b);
u = RGB_TO_U_CCIR(r, g, b, 0);
v = RGB_TO_V_CCIR(r, g, b, 0); |
25b4c651 |
YUVA_OUT((uint32_t*)sp->sub.rects[i]->pict.data[1] + j, y, u, v, a); |
72ce053b |
}
}
/* now we can update the picture count */
if (++is->subpq_windex == SUBPICTURE_QUEUE_SIZE)
is->subpq_windex = 0;
SDL_LockMutex(is->subpq_mutex);
is->subpq_size++;
SDL_UnlockMutex(is->subpq_mutex); |
608989f6 |
} else if (got_subtitle) {
avsubtitle_free(&sp->sub); |
72ce053b |
}
av_free_packet(pkt);
}
return 0;
}
|
01310af2 |
/* copy samples for viewing in editor window */
static void update_sample_display(VideoState *is, short *samples, int samples_size)
{ |
705c6520 |
int size, len; |
01310af2 |
size = samples_size / sizeof(short);
while (size > 0) {
len = SAMPLE_ARRAY_SIZE - is->sample_array_index;
if (len > size)
len = size;
memcpy(is->sample_array + is->sample_array_index, samples, len * sizeof(short));
samples += len;
is->sample_array_index += len;
if (is->sample_array_index >= SAMPLE_ARRAY_SIZE)
is->sample_array_index = 0;
size -= len;
}
}
|
6f06545b |
/* return the wanted number of samples to get better sync if sync_type is video
* or external master clock */
static int synchronize_audio(VideoState *is, int nb_samples) |
01310af2 |
{ |
6f06545b |
int wanted_nb_samples = nb_samples; |
01310af2 |
/* if not master, then we try to remove or add samples to correct the clock */ |
d30c6925 |
if (get_master_sync_type(is) != AV_SYNC_AUDIO_MASTER) { |
638c9d91 |
double diff, avg_diff; |
6f06545b |
int min_nb_samples, max_nb_samples; |
115329f1 |
|
f2175a62 |
diff = get_clock(&is->audclk) - get_master_clock(is); |
115329f1 |
|
26c208cf |
if (!isnan(diff) && fabs(diff) < AV_NOSYNC_THRESHOLD) { |
638c9d91 |
is->audio_diff_cum = diff + is->audio_diff_avg_coef * is->audio_diff_cum;
if (is->audio_diff_avg_count < AUDIO_DIFF_AVG_NB) {
/* not enough measures to have a correct estimate */
is->audio_diff_avg_count++;
} else {
/* estimate the A-V difference */
avg_diff = is->audio_diff_cum * (1.0 - is->audio_diff_avg_coef);
if (fabs(avg_diff) >= is->audio_diff_threshold) { |
e1248f5c |
wanted_nb_samples = nb_samples + (int)(diff * is->audio_src.freq); |
6f06545b |
min_nb_samples = ((nb_samples * (100 - SAMPLE_CORRECTION_PERCENT_MAX) / 100));
max_nb_samples = ((nb_samples * (100 + SAMPLE_CORRECTION_PERCENT_MAX) / 100));
wanted_nb_samples = FFMIN(FFMAX(wanted_nb_samples, min_nb_samples), max_nb_samples); |
638c9d91 |
} |
5534d8f7 |
av_dlog(NULL, "diff=%f adiff=%f sample_diff=%d apts=%0.3f %f\n", |
6f06545b |
diff, avg_diff, wanted_nb_samples - nb_samples, |
5534d8f7 |
is->audio_clock, is->audio_diff_threshold); |
01310af2 |
} |
638c9d91 |
} else {
/* too big difference : may be initial PTS errors, so
reset A-V filter */
is->audio_diff_avg_count = 0; |
da7c65f0 |
is->audio_diff_cum = 0; |
01310af2 |
}
}
|
6f06545b |
return wanted_nb_samples; |
01310af2 |
}
|
6032a1c9 |
/**
* Decode one audio frame and return its uncompressed size.
*
* The processed audio frame is decoded, converted if required, and
* stored in is->audio_buf, with size in bytes given by the return
* value.
*/ |
6e32b377 |
static int audio_decode_frame(VideoState *is) |
01310af2 |
{ |
bea18375 |
AVPacket *pkt_temp = &is->audio_pkt_temp; |
01310af2 |
AVPacket *pkt = &is->audio_pkt; |
da7c65f0 |
AVCodecContext *dec = is->audio_st->codec; |
1822519d |
int len1, data_size, resampled_data_size; |
a5a1e3cb |
int64_t dec_channel_layout;
int got_frame; |
02af4e9a |
av_unused double audio_clock0; |
6f06545b |
int wanted_nb_samples; |
e96175ad |
AVRational tb; |
0b24e341 |
int ret;
int reconfigure; |
01310af2 |
|
da7c65f0 |
for (;;) { |
72ea344b |
/* NOTE: the audio packet can contain several frames */ |
782e06e2 |
while (pkt_temp->stream_index != -1 || is->audio_buf_frames_pending) { |
f199f385 |
if (!is->frame) {
if (!(is->frame = avcodec_alloc_frame()))
return AVERROR(ENOMEM); |
738487f8 |
} else {
av_frame_unref(is->frame); |
f199f385 |
avcodec_get_frame_defaults(is->frame); |
738487f8 |
} |
f199f385 |
|
48e43a19 |
if (is->audioq.serial != is->audio_pkt_temp_serial)
break;
|
a6f51de3 |
if (is->paused)
return -1;
|
0b24e341 |
if (!is->audio_buf_frames_pending) { |
5dacf7b1 |
len1 = avcodec_decode_audio4(dec, is->frame, &got_frame, pkt_temp);
if (len1 < 0) {
/* if error, we skip the frame */
pkt_temp->size = 0;
break;
} |
0b24e341 |
|
b383498e |
pkt_temp->dts =
pkt_temp->pts = AV_NOPTS_VALUE; |
5dacf7b1 |
pkt_temp->data += len1;
pkt_temp->size -= len1; |
782e06e2 |
if (pkt_temp->data && pkt_temp->size <= 0 || !pkt_temp->data && !got_frame)
pkt_temp->stream_index = -1; |
e84ca8d3 |
if (!pkt_temp->data && !got_frame)
is->audio_finished = is->audio_pkt_temp_serial; |
59df4b82 |
|
782e06e2 |
if (!got_frame) |
5dacf7b1 |
continue; |
9eafdd51 |
|
5dacf7b1 |
tb = (AVRational){1, is->frame->sample_rate};
if (is->frame->pts != AV_NOPTS_VALUE)
is->frame->pts = av_rescale_q(is->frame->pts, dec->time_base, tb); |
447d2e31 |
else if (is->frame->pkt_pts != AV_NOPTS_VALUE)
is->frame->pts = av_rescale_q(is->frame->pkt_pts, is->audio_st->time_base, tb); |
b383498e |
else if (is->audio_frame_next_pts != AV_NOPTS_VALUE) |
b198e33a |
#if CONFIG_AVFILTER |
b383498e |
is->frame->pts = av_rescale_q(is->audio_frame_next_pts, (AVRational){1, is->audio_filter_src.freq}, tb); |
b198e33a |
#else
is->frame->pts = av_rescale_q(is->audio_frame_next_pts, (AVRational){1, is->audio_src.freq}, tb);
#endif |
b383498e |
if (is->frame->pts != AV_NOPTS_VALUE)
is->audio_frame_next_pts = is->frame->pts + is->frame->nb_samples; |
e96175ad |
#if CONFIG_AVFILTER
dec_channel_layout = get_valid_channel_layout(is->frame->channel_layout, av_frame_get_channels(is->frame));
reconfigure =
cmp_audio_fmts(is->audio_filter_src.fmt, is->audio_filter_src.channels,
is->frame->format, av_frame_get_channels(is->frame)) ||
is->audio_filter_src.channel_layout != dec_channel_layout ||
is->audio_filter_src.freq != is->frame->sample_rate ||
is->audio_pkt_temp_serial != is->audio_last_serial;
if (reconfigure) {
char buf1[1024], buf2[1024];
av_get_channel_layout_string(buf1, sizeof(buf1), -1, is->audio_filter_src.channel_layout);
av_get_channel_layout_string(buf2, sizeof(buf2), -1, dec_channel_layout);
av_log(NULL, AV_LOG_DEBUG,
"Audio frame changed from rate:%d ch:%d fmt:%s layout:%s serial:%d to rate:%d ch:%d fmt:%s layout:%s serial:%d\n",
is->audio_filter_src.freq, is->audio_filter_src.channels, av_get_sample_fmt_name(is->audio_filter_src.fmt), buf1, is->audio_last_serial,
is->frame->sample_rate, av_frame_get_channels(is->frame), av_get_sample_fmt_name(is->frame->format), buf2, is->audio_pkt_temp_serial);
is->audio_filter_src.fmt = is->frame->format;
is->audio_filter_src.channels = av_frame_get_channels(is->frame);
is->audio_filter_src.channel_layout = dec_channel_layout;
is->audio_filter_src.freq = is->frame->sample_rate;
is->audio_last_serial = is->audio_pkt_temp_serial;
if ((ret = configure_audio_filters(is, afilters, 1)) < 0)
return ret;
}
if ((ret = av_buffersrc_add_frame(is->in_audio_filter, is->frame)) < 0)
return ret;
av_frame_unref(is->frame); |
0b24e341 |
#endif
}
#if CONFIG_AVFILTER
if ((ret = av_buffersink_get_frame_flags(is->out_audio_filter, is->frame, 0)) < 0) {
if (ret == AVERROR(EAGAIN)) {
is->audio_buf_frames_pending = 0;
continue;
} |
18be3fac |
if (ret == AVERROR_EOF)
is->audio_finished = is->audio_pkt_temp_serial; |
0b24e341 |
return ret; |
e96175ad |
} |
0b24e341 |
is->audio_buf_frames_pending = 1;
tb = is->out_audio_filter->inputs[0]->time_base; |
e96175ad |
#endif |
9eafdd51 |
|
ec1d2e2f |
data_size = av_samples_get_buffer_size(NULL, av_frame_get_channels(is->frame), |
f199f385 |
is->frame->nb_samples, |
d0707677 |
is->frame->format, 1); |
5a4476e2 |
|
0d3ffde6 |
dec_channel_layout = |
ec1d2e2f |
(is->frame->channel_layout && av_frame_get_channels(is->frame) == av_get_channel_layout_nb_channels(is->frame->channel_layout)) ?
is->frame->channel_layout : av_get_default_channel_layout(av_frame_get_channels(is->frame)); |
6f06545b |
wanted_nb_samples = synchronize_audio(is, is->frame->nb_samples); |
1dd3c473 |
|
d0707677 |
if (is->frame->format != is->audio_src.fmt ||
dec_channel_layout != is->audio_src.channel_layout ||
is->frame->sample_rate != is->audio_src.freq ||
(wanted_nb_samples != is->frame->nb_samples && !is->swr_ctx)) { |
4fd07b93 |
swr_free(&is->swr_ctx); |
645c61fb |
is->swr_ctx = swr_alloc_set_opts(NULL, |
e1248f5c |
is->audio_tgt.channel_layout, is->audio_tgt.fmt, is->audio_tgt.freq, |
d0707677 |
dec_channel_layout, is->frame->format, is->frame->sample_rate, |
eafa2b60 |
0, NULL); |
1dd3c473 |
if (!is->swr_ctx || swr_init(is->swr_ctx) < 0) { |
3b491c5a |
av_log(NULL, AV_LOG_ERROR,
"Cannot create sample rate converter for conversion of %d Hz %s %d channels to %d Hz %s %d channels!\n", |
ec1d2e2f |
is->frame->sample_rate, av_get_sample_fmt_name(is->frame->format), av_frame_get_channels(is->frame), |
394130ef |
is->audio_tgt.freq, av_get_sample_fmt_name(is->audio_tgt.fmt), is->audio_tgt.channels); |
1dd3c473 |
break; |
5a4476e2 |
} |
e1248f5c |
is->audio_src.channel_layout = dec_channel_layout; |
ec1d2e2f |
is->audio_src.channels = av_frame_get_channels(is->frame); |
d0707677 |
is->audio_src.freq = is->frame->sample_rate;
is->audio_src.fmt = is->frame->format; |
5a4476e2 |
}
|
1dd3c473 |
if (is->swr_ctx) { |
e2c96fbf |
const uint8_t **in = (const uint8_t **)is->frame->extended_data; |
571ef42d |
uint8_t **out = &is->audio_buf1;
int out_count = (int64_t)wanted_nb_samples * is->audio_tgt.freq / is->frame->sample_rate + 256;
int out_size = av_samples_get_buffer_size(NULL, is->audio_tgt.channels, out_count, is->audio_tgt.fmt, 0); |
1822519d |
int len2; |
047716bb |
if (out_size < 0) { |
3b491c5a |
av_log(NULL, AV_LOG_ERROR, "av_samples_get_buffer_size() failed\n"); |
047716bb |
break;
} |
6f06545b |
if (wanted_nb_samples != is->frame->nb_samples) { |
d0707677 |
if (swr_set_compensation(is->swr_ctx, (wanted_nb_samples - is->frame->nb_samples) * is->audio_tgt.freq / is->frame->sample_rate,
wanted_nb_samples * is->audio_tgt.freq / is->frame->sample_rate) < 0) { |
3b491c5a |
av_log(NULL, AV_LOG_ERROR, "swr_set_compensation() failed\n"); |
6f06545b |
break;
}
} |
571ef42d |
av_fast_malloc(&is->audio_buf1, &is->audio_buf1_size, out_size);
if (!is->audio_buf1)
return AVERROR(ENOMEM); |
b12e61ac |
len2 = swr_convert(is->swr_ctx, out, out_count, in, is->frame->nb_samples); |
1dd3c473 |
if (len2 < 0) { |
3b491c5a |
av_log(NULL, AV_LOG_ERROR, "swr_convert() failed\n"); |
5a4476e2 |
break;
} |
b12e61ac |
if (len2 == out_count) { |
3b491c5a |
av_log(NULL, AV_LOG_WARNING, "audio buffer is probably too small\n"); |
1dd3c473 |
swr_init(is->swr_ctx);
} |
571ef42d |
is->audio_buf = is->audio_buf1; |
e1248f5c |
resampled_data_size = len2 * is->audio_tgt.channels * av_get_bytes_per_sample(is->audio_tgt.fmt); |
1dd3c473 |
} else { |
f199f385 |
is->audio_buf = is->frame->data[0]; |
347ecfdc |
resampled_data_size = data_size; |
5a4476e2 |
}
|
02af4e9a |
audio_clock0 = is->audio_clock; |
9eafdd51 |
/* update the audio clock with the pts */ |
3886572a |
if (is->frame->pts != AV_NOPTS_VALUE) |
e96175ad |
is->audio_clock = is->frame->pts * av_q2d(tb) + (double) is->frame->nb_samples / is->frame->sample_rate; |
3886572a |
else
is->audio_clock = NAN;
is->audio_clock_serial = is->audio_pkt_temp_serial; |
1f6b9cc3 |
#ifdef DEBUG |
72ea344b |
{
static double last_clock; |
02af4e9a |
printf("audio: delay=%0.3f clock=%0.3f clock0=%0.3f\n", |
72ea344b |
is->audio_clock - last_clock, |
02af4e9a |
is->audio_clock, audio_clock0); |
72ea344b |
last_clock = is->audio_clock; |
01310af2 |
} |
72ea344b |
#endif |
1dd3c473 |
return resampled_data_size; |
01310af2 |
}
|
72ea344b |
/* free the current packet */
if (pkt->data) |
01310af2 |
av_free_packet(pkt); |
efe8a1ba |
memset(pkt_temp, 0, sizeof(*pkt_temp)); |
782e06e2 |
pkt_temp->stream_index = -1; |
115329f1 |
|
3070600d |
if (is->audioq.abort_request) { |
72ea344b |
return -1;
} |
115329f1 |
|
0e5042be |
if (is->audioq.nb_packets == 0)
SDL_CondSignal(is->continue_read_thread);
|
01310af2 |
/* read next packet */ |
782e06e2 |
if ((packet_queue_get(&is->audioq, pkt, 1, &is->audio_pkt_temp_serial)) < 0) |
01310af2 |
return -1; |
59df4b82 |
|
ec1f3cab |
if (pkt->data == flush_pkt.data) { |
abdff646 |
avcodec_flush_buffers(dec); |
0b24e341 |
is->audio_buf_frames_pending = 0; |
b383498e |
is->audio_frame_next_pts = AV_NOPTS_VALUE; |
73b2043d |
if ((is->ic->iformat->flags & (AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK)) && !is->ic->iformat->read_seek)
is->audio_frame_next_pts = is->audio_st->start_time; |
ec1f3cab |
} |
39c6a118 |
|
f199f385 |
*pkt_temp = *pkt; |
01310af2 |
}
}
/* prepare a new audio buffer */ |
358061f6 |
static void sdl_audio_callback(void *opaque, Uint8 *stream, int len) |
01310af2 |
{
VideoState *is = opaque;
int audio_size, len1; |
10b7b4a6 |
int bytes_per_sec; |
e1248f5c |
int frame_size = av_samples_get_buffer_size(NULL, is->audio_tgt.channels, 1, is->audio_tgt.fmt, 1); |
01310af2 |
audio_callback_time = av_gettime(); |
115329f1 |
|
01310af2 |
while (len > 0) {
if (is->audio_buf_index >= is->audio_buf_size) { |
6e32b377 |
audio_size = audio_decode_frame(is); |
01310af2 |
if (audio_size < 0) {
/* if error, just output silence */ |
e2a2c49f |
is->audio_buf = is->silence_buf; |
1348420b |
is->audio_buf_size = sizeof(is->silence_buf) / frame_size * frame_size; |
01310af2 |
} else { |
f8b8c694 |
if (is->show_mode != SHOW_MODE_VIDEO) |
01310af2 |
update_sample_display(is, (int16_t *)is->audio_buf, audio_size);
is->audio_buf_size = audio_size;
}
is->audio_buf_index = 0;
}
len1 = is->audio_buf_size - is->audio_buf_index;
if (len1 > len)
len1 = len;
memcpy(stream, (uint8_t *)is->audio_buf + is->audio_buf_index, len1);
len -= len1;
stream += len1;
is->audio_buf_index += len1;
} |
e1248f5c |
bytes_per_sec = is->audio_tgt.freq * is->audio_tgt.channels * av_get_bytes_per_sample(is->audio_tgt.fmt); |
10b7b4a6 |
is->audio_write_buf_size = is->audio_buf_size - is->audio_buf_index;
/* Let's assume the audio driver that is used by SDL has two periods. */ |
3886572a |
if (!isnan(is->audio_clock)) {
set_clock_at(&is->audclk, is->audio_clock - (double)(2 * is->audio_hw_buf_size + is->audio_write_buf_size) / bytes_per_sec, is->audio_clock_serial, audio_callback_time / 1000000.0);
sync_clock_to_slave(&is->extclk, &is->audclk);
} |
01310af2 |
}
|
7821264c |
static int audio_open(void *opaque, int64_t wanted_channel_layout, int wanted_nb_channels, int wanted_sample_rate, struct AudioParams *audio_hw_params) |
03095d73 |
{
SDL_AudioSpec wanted_spec, spec;
const char *env; |
5c6a5874 |
static const int next_nb_channels[] = {0, 0, 1, 6, 2, 6, 4, 6}; |
03095d73 |
env = SDL_getenv("SDL_AUDIO_CHANNELS"); |
7821264c |
if (env) { |
ab0ea7cb |
wanted_nb_channels = atoi(env); |
7821264c |
wanted_channel_layout = av_get_default_channel_layout(wanted_nb_channels);
}
if (!wanted_channel_layout || wanted_nb_channels != av_get_channel_layout_nb_channels(wanted_channel_layout)) {
wanted_channel_layout = av_get_default_channel_layout(wanted_nb_channels); |
03095d73 |
wanted_channel_layout &= ~AV_CH_LAYOUT_STEREO_DOWNMIX;
}
wanted_spec.channels = av_get_channel_layout_nb_channels(wanted_channel_layout); |
7821264c |
wanted_spec.freq = wanted_sample_rate; |
03095d73 |
if (wanted_spec.freq <= 0 || wanted_spec.channels <= 0) { |
3b491c5a |
av_log(NULL, AV_LOG_ERROR, "Invalid sample rate or channel count!\n"); |
03095d73 |
return -1;
}
wanted_spec.format = AUDIO_S16SYS;
wanted_spec.silence = 0;
wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE;
wanted_spec.callback = sdl_audio_callback; |
73420379 |
wanted_spec.userdata = opaque; |
0dad5292 |
while (SDL_OpenAudio(&wanted_spec, &spec) < 0) { |
3b491c5a |
av_log(NULL, AV_LOG_WARNING, "SDL_OpenAudio (%d channels): %s\n", wanted_spec.channels, SDL_GetError()); |
0dad5292 |
wanted_spec.channels = next_nb_channels[FFMIN(7, wanted_spec.channels)];
if (!wanted_spec.channels) { |
3b491c5a |
av_log(NULL, AV_LOG_ERROR,
"No more channel combinations to try, audio open failed\n"); |
0dad5292 |
return -1;
}
wanted_channel_layout = av_get_default_channel_layout(wanted_spec.channels); |
03095d73 |
}
if (spec.format != AUDIO_S16SYS) { |
3b491c5a |
av_log(NULL, AV_LOG_ERROR,
"SDL advised audio format %d is not supported!\n", spec.format); |
03095d73 |
return -1;
}
if (spec.channels != wanted_spec.channels) {
wanted_channel_layout = av_get_default_channel_layout(spec.channels);
if (!wanted_channel_layout) { |
3b491c5a |
av_log(NULL, AV_LOG_ERROR,
"SDL advised channel count %d is not supported!\n", spec.channels); |
03095d73 |
return -1;
}
}
|
7821264c |
audio_hw_params->fmt = AV_SAMPLE_FMT_S16;
audio_hw_params->freq = spec.freq;
audio_hw_params->channel_layout = wanted_channel_layout;
audio_hw_params->channels = spec.channels; |
73420379 |
return spec.size; |
03095d73 |
}
|
01310af2 |
/* open a given stream. Return 0 if OK */
static int stream_component_open(VideoState *is, int stream_index)
{
AVFormatContext *ic = is->ic; |
fe74099a |
AVCodecContext *avctx; |
01310af2 |
AVCodec *codec; |
013b7007 |
const char *forced_codec_name = NULL; |
3009f521 |
AVDictionary *opts;
AVDictionaryEntry *t = NULL; |
e96175ad |
int sample_rate, nb_channels;
int64_t channel_layout; |
1822519d |
int ret; |
c7fe2a37 |
int stream_lowres = lowres; |
01310af2 |
if (stream_index < 0 || stream_index >= ic->nb_streams)
return -1; |
fe74099a |
avctx = ic->streams[stream_index]->codec; |
115329f1 |
|
fe74099a |
codec = avcodec_find_decoder(avctx->codec_id); |
6d13499b |
|
5eda0967 |
switch(avctx->codec_type){ |
013b7007 |
case AVMEDIA_TYPE_AUDIO : is->last_audio_stream = stream_index; forced_codec_name = audio_codec_name; break;
case AVMEDIA_TYPE_SUBTITLE: is->last_subtitle_stream = stream_index; forced_codec_name = subtitle_codec_name; break;
case AVMEDIA_TYPE_VIDEO : is->last_video_stream = stream_index; forced_codec_name = video_codec_name; break; |
5eda0967 |
} |
013b7007 |
if (forced_codec_name)
codec = avcodec_find_decoder_by_name(forced_codec_name);
if (!codec) { |
3b491c5a |
if (forced_codec_name) av_log(NULL, AV_LOG_WARNING,
"No codec could be found with name '%s'\n", forced_codec_name);
else av_log(NULL, AV_LOG_WARNING,
"No codec could be found with id %d\n", avctx->codec_id); |
99119bdf |
return -1; |
013b7007 |
} |
99119bdf |
|
1cbb11cd |
avctx->codec_id = codec->id; |
da7c65f0 |
avctx->workaround_bugs = workaround_bugs; |
c7fe2a37 |
if(stream_lowres > av_codec_get_max_lowres(codec)){ |
d8407ee2 |
av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n", |
b72040da |
av_codec_get_max_lowres(codec)); |
c7fe2a37 |
stream_lowres = av_codec_get_max_lowres(codec); |
d8407ee2 |
} |
c7fe2a37 |
av_codec_set_lowres(avctx, stream_lowres); |
da7c65f0 |
avctx->error_concealment = error_concealment; |
fe74099a |
|
c7fe2a37 |
if(stream_lowres) avctx->flags |= CODEC_FLAG_EMU_EDGE; |
da7c65f0 |
if (fast) avctx->flags2 |= CODEC_FLAG2_FAST; |
175714c0 |
if(codec->capabilities & CODEC_CAP_DR1)
avctx->flags |= CODEC_FLAG_EMU_EDGE;
|
fdb93344 |
opts = filter_codec_opts(codec_opts, avctx->codec_id, ic, ic->streams[stream_index], codec); |
2473a45c |
if (!av_dict_get(opts, "threads", NULL, 0))
av_dict_set(&opts, "threads", "auto", 0); |
c7fe2a37 |
if (stream_lowres)
av_dict_set(&opts, "lowres", av_asprintf("%d", stream_lowres), AV_DICT_DONT_STRDUP_VAL); |
738487f8 |
if (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO) |
354468fc |
av_dict_set(&opts, "refcounted_frames", "1", 0); |
fec39d99 |
if (avcodec_open2(avctx, codec, &opts) < 0) |
01310af2 |
return -1; |
3009f521 |
if ((t = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
return AVERROR_OPTION_NOT_FOUND;
} |
51b73087 |
|
3f3fe38d |
ic->streams[stream_index]->discard = AVDISCARD_DEFAULT; |
da7c65f0 |
switch (avctx->codec_type) { |
72415b2a |
case AVMEDIA_TYPE_AUDIO: |
e96175ad |
#if CONFIG_AVFILTER
{
AVFilterLink *link;
is->audio_filter_src.freq = avctx->sample_rate;
is->audio_filter_src.channels = avctx->channels;
is->audio_filter_src.channel_layout = get_valid_channel_layout(avctx->channel_layout, avctx->channels);
is->audio_filter_src.fmt = avctx->sample_fmt;
if ((ret = configure_audio_filters(is, afilters, 0)) < 0)
return ret;
link = is->out_audio_filter->inputs[0];
sample_rate = link->sample_rate;
nb_channels = link->channels;
channel_layout = link->channel_layout;
}
#else
sample_rate = avctx->sample_rate;
nb_channels = avctx->channels;
channel_layout = avctx->channel_layout;
#endif
|
1822519d |
/* prepare audio output */ |
e96175ad |
if ((ret = audio_open(is, channel_layout, nb_channels, sample_rate, &is->audio_tgt)) < 0) |
1822519d |
return ret;
is->audio_hw_buf_size = ret;
is->audio_src = is->audio_tgt; |
da7c65f0 |
is->audio_buf_size = 0; |
01310af2 |
is->audio_buf_index = 0; |
638c9d91 |
/* init averaging filter */ |
da7c65f0 |
is->audio_diff_avg_coef = exp(log(0.01) / AUDIO_DIFF_AVG_NB); |
638c9d91 |
is->audio_diff_avg_count = 0;
/* since we do not have a precise anough audio fifo fullness,
we correct audio sync only if larger than this threshold */ |
22505c18 |
is->audio_diff_threshold = 2.0 * is->audio_hw_buf_size / av_samples_get_buffer_size(NULL, is->audio_tgt.channels, is->audio_tgt.freq, is->audio_tgt.fmt, 1); |
638c9d91 |
|
01310af2 |
memset(&is->audio_pkt, 0, sizeof(is->audio_pkt)); |
03095d73 |
memset(&is->audio_pkt_temp, 0, sizeof(is->audio_pkt_temp)); |
782e06e2 |
is->audio_pkt_temp.stream_index = -1; |
1822519d |
is->audio_stream = stream_index;
is->audio_st = ic->streams[stream_index];
|
a687acbb |
packet_queue_start(&is->audioq); |
bb270c08 |
SDL_PauseAudio(0); |
01310af2 |
break; |
72415b2a |
case AVMEDIA_TYPE_VIDEO: |
01310af2 |
is->video_stream = stream_index;
is->video_st = ic->streams[stream_index];
|
a687acbb |
packet_queue_start(&is->videoq); |
01310af2 |
is->video_tid = SDL_CreateThread(video_thread, is); |
1c2ac449 |
is->queue_attachments_req = 1; |
01310af2 |
break; |
72415b2a |
case AVMEDIA_TYPE_SUBTITLE: |
72ce053b |
is->subtitle_stream = stream_index;
is->subtitle_st = ic->streams[stream_index]; |
a687acbb |
packet_queue_start(&is->subtitleq); |
115329f1 |
|
72ce053b |
is->subtitle_tid = SDL_CreateThread(subtitle_thread, is);
break; |
01310af2 |
default:
break;
}
return 0;
}
static void stream_component_close(VideoState *is, int stream_index)
{
AVFormatContext *ic = is->ic; |
fe74099a |
AVCodecContext *avctx; |
115329f1 |
|
72ce053b |
if (stream_index < 0 || stream_index >= ic->nb_streams)
return; |
fe74099a |
avctx = ic->streams[stream_index]->codec; |
01310af2 |
|
da7c65f0 |
switch (avctx->codec_type) { |
72415b2a |
case AVMEDIA_TYPE_AUDIO: |
01310af2 |
packet_queue_abort(&is->audioq);
SDL_CloseAudio();
|
a687acbb |
packet_queue_flush(&is->audioq); |
c74f1f47 |
av_free_packet(&is->audio_pkt); |
4fd07b93 |
swr_free(&is->swr_ctx); |
f199f385 |
av_freep(&is->audio_buf1); |
571ef42d |
is->audio_buf1_size = 0; |
f199f385 |
is->audio_buf = NULL; |
738487f8 |
av_frame_free(&is->frame); |
cb2c4de3 |
if (is->rdft) {
av_rdft_end(is->rdft);
av_freep(&is->rdft_data); |
f9324d5a |
is->rdft = NULL;
is->rdft_bits = 0; |
cb2c4de3 |
} |
e96175ad |
#if CONFIG_AVFILTER
avfilter_graph_free(&is->agraph);
#endif |
01310af2 |
break; |
72415b2a |
case AVMEDIA_TYPE_VIDEO: |
01310af2 |
packet_queue_abort(&is->videoq);
/* note: we also signal this mutex to make sure we deblock the
video thread in all cases */
SDL_LockMutex(is->pictq_mutex);
SDL_CondSignal(is->pictq_cond);
SDL_UnlockMutex(is->pictq_mutex);
SDL_WaitThread(is->video_tid, NULL);
|
a687acbb |
packet_queue_flush(&is->videoq); |
01310af2 |
break; |
72415b2a |
case AVMEDIA_TYPE_SUBTITLE: |
72ce053b |
packet_queue_abort(&is->subtitleq); |
115329f1 |
|
72ce053b |
/* note: we also signal this mutex to make sure we deblock the
video thread in all cases */
SDL_LockMutex(is->subpq_mutex);
SDL_CondSignal(is->subpq_cond);
SDL_UnlockMutex(is->subpq_mutex);
SDL_WaitThread(is->subtitle_tid, NULL);
|
a687acbb |
packet_queue_flush(&is->subtitleq); |
72ce053b |
break; |
01310af2 |
default:
break;
}
|
3f3fe38d |
ic->streams[stream_index]->discard = AVDISCARD_ALL; |
fe74099a |
avcodec_close(avctx); |
da7c65f0 |
switch (avctx->codec_type) { |
72415b2a |
case AVMEDIA_TYPE_AUDIO: |
01310af2 |
is->audio_st = NULL;
is->audio_stream = -1;
break; |
72415b2a |
case AVMEDIA_TYPE_VIDEO: |
01310af2 |
is->video_st = NULL;
is->video_stream = -1;
break; |
72415b2a |
case AVMEDIA_TYPE_SUBTITLE: |
72ce053b |
is->subtitle_st = NULL;
is->subtitle_stream = -1;
break; |
01310af2 |
default:
break;
}
}
|
40972f7c |
static int decode_interrupt_cb(void *ctx) |
416e3508 |
{ |
708df4ac |
VideoState *is = ctx;
return is->abort_request; |
416e3508 |
} |
01310af2 |
|
2e8dcde1 |
static int is_realtime(AVFormatContext *s)
{
if( !strcmp(s->iformat->name, "rtp")
|| !strcmp(s->iformat->name, "rtsp")
|| !strcmp(s->iformat->name, "sdp")
)
return 1;
if(s->pb && ( !strncmp(s->filename, "rtp:", 4)
|| !strncmp(s->filename, "udp:", 4)
)
)
return 1;
return 0;
}
|
01310af2 |
/* this thread gets the stream from the disk or the network */ |
8adf9bb2 |
static int read_thread(void *arg) |
01310af2 |
{
VideoState *is = arg; |
b6bde8c7 |
AVFormatContext *ic = NULL; |
6625a3de |
int err, i, ret; |
72415b2a |
int st_index[AVMEDIA_TYPE_NB]; |
01310af2 |
AVPacket pkt1, *pkt = &pkt1; |
da7c65f0 |
int eof = 0; |
f07cb53a |
int64_t stream_start_time; |
d834d63b |
int pkt_in_play_range = 0; |
b6bde8c7 |
AVDictionaryEntry *t; |
3009f521 |
AVDictionary **opts;
int orig_nb_streams; |
0e5042be |
SDL_mutex *wait_mutex = SDL_CreateMutex(); |
6299a229 |
|
6625a3de |
memset(st_index, -1, sizeof(st_index)); |
8c9971c3 |
is->last_video_stream = is->video_stream = -1;
is->last_audio_stream = is->audio_stream = -1;
is->last_subtitle_stream = is->subtitle_stream = -1; |
01310af2 |
|
40972f7c |
ic = avformat_alloc_context();
ic->interrupt_callback.callback = decode_interrupt_cb; |
708df4ac |
ic->interrupt_callback.opaque = is; |
b6bde8c7 |
err = avformat_open_input(&ic, is->filename, is->iformat, &format_opts); |
638c9d91 |
if (err < 0) {
print_error(is->filename, err);
ret = -1;
goto fail;
} |
b6bde8c7 |
if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
ret = AVERROR_OPTION_NOT_FOUND;
goto fail;
} |
01310af2 |
is->ic = ic; |
30bc6613 |
|
da7c65f0 |
if (genpts) |
30bc6613 |
ic->flags |= AVFMT_FLAG_GENPTS;
|
8ec19f84 |
opts = setup_find_stream_info_opts(ic, codec_opts); |
3009f521 |
orig_nb_streams = ic->nb_streams; |
bc778a0c |
|
3009f521 |
err = avformat_find_stream_info(ic, opts); |
24c07998 |
if (err < 0) { |
3b491c5a |
av_log(NULL, AV_LOG_WARNING,
"%s: could not find codec parameters\n", is->filename); |
24c07998 |
ret = -1;
goto fail;
} |
3009f521 |
for (i = 0; i < orig_nb_streams; i++)
av_dict_free(&opts[i]);
av_freep(&opts);
|
da7c65f0 |
if (ic->pb) |
41211483 |
ic->pb->eof_reached = 0; // FIXME hack, ffplay maybe should not use url_feof() to test for the end |
72ea344b |
|
da7c65f0 |
if (seek_by_bytes < 0) |
7f4b588f |
seek_by_bytes = !!(ic->iformat->flags & AVFMT_TS_DISCONT) && strcmp("ogg", ic->iformat->name); |
70a4764d |
|
f7eb50f3 |
is->max_frame_duration = (ic->iformat->flags & AVFMT_TS_DISCONT) ? 10.0 : 3600.0;
|
9db6aaeb |
if (!window_title && (t = av_dict_get(ic->metadata, "title", NULL, 0)))
window_title = av_asprintf("%s - %s", t->value, input_filename);
|
72ea344b |
/* if seeking requested, we execute it */
if (start_time != AV_NOPTS_VALUE) {
int64_t timestamp;
timestamp = start_time;
/* add the stream start time */
if (ic->start_time != AV_NOPTS_VALUE)
timestamp += ic->start_time; |
4ed29207 |
ret = avformat_seek_file(ic, -1, INT64_MIN, timestamp, INT64_MAX, 0); |
72ea344b |
if (ret < 0) { |
3b491c5a |
av_log(NULL, AV_LOG_WARNING, "%s: could not seek to position %0.3f\n", |
72ea344b |
is->filename, (double)timestamp / AV_TIME_BASE);
}
}
|
747c749d |
is->realtime = is_realtime(ic);
|
406f0f1b |
for (i = 0; i < ic->nb_streams; i++) |
3f3fe38d |
ic->streams[i]->discard = AVDISCARD_ALL; |
be732b70 |
if (!video_disable) |
406f0f1b |
st_index[AVMEDIA_TYPE_VIDEO] =
av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO,
wanted_stream[AVMEDIA_TYPE_VIDEO], -1, NULL, 0); |
be732b70 |
if (!audio_disable) |
406f0f1b |
st_index[AVMEDIA_TYPE_AUDIO] =
av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO,
wanted_stream[AVMEDIA_TYPE_AUDIO],
st_index[AVMEDIA_TYPE_VIDEO],
NULL, 0); |
d0c6ed7d |
if (!video_disable && !subtitle_disable) |
406f0f1b |
st_index[AVMEDIA_TYPE_SUBTITLE] =
av_find_best_stream(ic, AVMEDIA_TYPE_SUBTITLE,
wanted_stream[AVMEDIA_TYPE_SUBTITLE],
(st_index[AVMEDIA_TYPE_AUDIO] >= 0 ?
st_index[AVMEDIA_TYPE_AUDIO] :
st_index[AVMEDIA_TYPE_VIDEO]),
NULL, 0); |
01310af2 |
if (show_status) { |
0ebf4754 |
av_dump_format(ic, 0, is->filename, 0); |
01310af2 |
}
|
f521746b |
is->show_mode = show_mode;
|
01310af2 |
/* open the streams */ |
72415b2a |
if (st_index[AVMEDIA_TYPE_AUDIO] >= 0) {
stream_component_open(is, st_index[AVMEDIA_TYPE_AUDIO]); |
01310af2 |
}
|
da7c65f0 |
ret = -1; |
72415b2a |
if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) { |
da7c65f0 |
ret = stream_component_open(is, st_index[AVMEDIA_TYPE_VIDEO]); |
077a8d61 |
} |
1d6c82d4 |
if (is->show_mode == SHOW_MODE_NONE)
is->show_mode = ret >= 0 ? SHOW_MODE_VIDEO : SHOW_MODE_RDFT; |
01310af2 |
|
72415b2a |
if (st_index[AVMEDIA_TYPE_SUBTITLE] >= 0) {
stream_component_open(is, st_index[AVMEDIA_TYPE_SUBTITLE]); |
16a59a7b |
}
|
01310af2 |
if (is->video_stream < 0 && is->audio_stream < 0) { |
f9f7f4c9 |
av_log(NULL, AV_LOG_FATAL, "Failed to open file '%s' or configure filtergraph\n",
is->filename); |
638c9d91 |
ret = -1; |
01310af2 |
goto fail;
}
|
747c749d |
if (infinite_buffer < 0 && is->realtime) |
2e8dcde1 |
infinite_buffer = 1;
|
da7c65f0 |
for (;;) { |
01310af2 |
if (is->abort_request)
break; |
416e3508 |
if (is->paused != is->last_paused) {
is->last_paused = is->paused; |
72ea344b |
if (is->paused) |
da7c65f0 |
is->read_pause_return = av_read_pause(ic); |
72ea344b |
else
av_read_play(ic); |
416e3508 |
} |
df98446b |
#if CONFIG_RTSP_DEMUXER || CONFIG_MMSH_PROTOCOL
if (is->paused &&
(!strcmp(ic->iformat->name, "rtsp") || |
304ec08f |
(ic->pb && !strncmp(input_filename, "mmsh:", 5)))) { |
416e3508 |
/* wait 10 ms to avoid trying to get another packet */
/* XXX: horrible */
SDL_Delay(10);
continue;
} |
400738b1 |
#endif |
72ea344b |
if (is->seek_req) { |
da7c65f0 |
int64_t seek_target = is->seek_pos;
int64_t seek_min = is->seek_rel > 0 ? seek_target - is->seek_rel + 2: INT64_MIN;
int64_t seek_max = is->seek_rel < 0 ? seek_target - is->seek_rel - 2: INT64_MAX;
// FIXME the +-2 is due to rounding being not done in the correct direction in generation |
4ed29207 |
// of the seek_pos/seek_rel variables |
8e606cc8 |
|
4ed29207 |
ret = avformat_seek_file(is->ic, -1, seek_min, seek_target, seek_max, is->seek_flags); |
72ea344b |
if (ret < 0) { |
3b491c5a |
av_log(NULL, AV_LOG_ERROR,
"%s: error while seeking\n", is->ic->filename); |
da7c65f0 |
} else { |
e6c0297f |
if (is->audio_stream >= 0) {
packet_queue_flush(&is->audioq); |
39c6a118 |
packet_queue_put(&is->audioq, &flush_pkt); |
e6c0297f |
} |
72ce053b |
if (is->subtitle_stream >= 0) {
packet_queue_flush(&is->subtitleq); |
39c6a118 |
packet_queue_put(&is->subtitleq, &flush_pkt); |
72ce053b |
} |
e6c0297f |
if (is->video_stream >= 0) {
packet_queue_flush(&is->videoq); |
39c6a118 |
packet_queue_put(&is->videoq, &flush_pkt); |
e6c0297f |
} |
2efd01a3 |
if (is->seek_flags & AVSEEK_FLAG_BYTE) { |
f2175a62 |
set_clock(&is->extclk, NAN, 0); |
2efd01a3 |
} else { |
f2175a62 |
set_clock(&is->extclk, seek_target / (double)AV_TIME_BASE, 0); |
2efd01a3 |
} |
72ea344b |
}
is->seek_req = 0; |
1c2ac449 |
is->queue_attachments_req = 1; |
da7c65f0 |
eof = 0; |
4ea7fbb2 |
if (is->paused)
step_to_next_frame(is); |
72ea344b |
} |
491ca0e8 |
if (is->queue_attachments_req) { |
1c2ac449 |
if (is->video_st && is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC) {
AVPacket copy;
if ((ret = av_copy_packet(©, &is->video_st->attached_pic)) < 0)
goto fail;
packet_queue_put(&is->videoq, ©); |
0258e4dc |
packet_queue_put_nullpacket(&is->videoq, is->video_stream); |
1c2ac449 |
} |
491ca0e8 |
is->queue_attachments_req = 0; |
c151e0ca |
} |
416e3508 |
|
01310af2 |
/* if the queue are full, no need to read more */ |
cb3fd029 |
if (infinite_buffer<1 && |
296d0da8 |
(is->audioq.size + is->videoq.size + is->subtitleq.size > MAX_QUEUE_SIZE |
a687acbb |
|| ( (is->audioq .nb_packets > MIN_FRAMES || is->audio_stream < 0 || is->audioq.abort_request) |
1c2ac449 |
&& (is->videoq .nb_packets > MIN_FRAMES || is->video_stream < 0 || is->videoq.abort_request
|| (is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC)) |
039e9fe0 |
&& (is->subtitleq.nb_packets > MIN_FRAMES || is->subtitle_stream < 0 || is->subtitleq.abort_request)))) { |
01310af2 |
/* wait 10 ms */ |
0e5042be |
SDL_LockMutex(wait_mutex);
SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
SDL_UnlockMutex(wait_mutex); |
01310af2 |
continue;
} |
e84ca8d3 |
if (!is->paused &&
(!is->audio_st || is->audio_finished == is->audioq.serial) &&
(!is->video_st || (is->video_finished == is->videoq.serial && is->pictq_size == 0))) {
if (loop != 1 && (!loop || --loop)) {
stream_seek(is, start_time != AV_NOPTS_VALUE ? start_time : 0, 0, 0);
} else if (autoexit) {
ret = AVERROR_EOF;
goto fail;
}
} |
da7c65f0 |
if (eof) { |
b118d3e2 |
if (is->video_stream >= 0)
packet_queue_put_nullpacket(&is->videoq, is->video_stream);
if (is->audio_stream >= 0)
packet_queue_put_nullpacket(&is->audioq, is->audio_stream); |
b4083171 |
SDL_Delay(10); |
0b82612d |
eof=0; |
600a331c |
continue;
} |
72ea344b |
ret = av_read_frame(ic, pkt); |
01310af2 |
if (ret < 0) { |
27d97fde |
if (ret == AVERROR_EOF || url_feof(ic->pb)) |
da7c65f0 |
eof = 1; |
eb4d1cb9 |
if (ic->pb && ic->pb->error) |
bb270c08 |
break; |
d8f8e911 |
SDL_LockMutex(wait_mutex);
SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
SDL_UnlockMutex(wait_mutex); |
75bb7b0a |
continue; |
01310af2 |
} |
d834d63b |
/* check if packet is in play range specified by user, then queue, otherwise discard */ |
f07cb53a |
stream_start_time = ic->streams[pkt->stream_index]->start_time; |
d834d63b |
pkt_in_play_range = duration == AV_NOPTS_VALUE || |
f07cb53a |
(pkt->pts - (stream_start_time != AV_NOPTS_VALUE ? stream_start_time : 0)) * |
d834d63b |
av_q2d(ic->streams[pkt->stream_index]->time_base) - |
da7c65f0 |
(double)(start_time != AV_NOPTS_VALUE ? start_time : 0) / 1000000
<= ((double)duration / 1000000); |
d834d63b |
if (pkt->stream_index == is->audio_stream && pkt_in_play_range) { |
01310af2 |
packet_queue_put(&is->audioq, pkt); |
1c2ac449 |
} else if (pkt->stream_index == is->video_stream && pkt_in_play_range
&& !(is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC)) { |
01310af2 |
packet_queue_put(&is->videoq, pkt); |
d834d63b |
} else if (pkt->stream_index == is->subtitle_stream && pkt_in_play_range) { |
72ce053b |
packet_queue_put(&is->subtitleq, pkt); |
01310af2 |
} else {
av_free_packet(pkt);
}
}
/* wait until the end */
while (!is->abort_request) {
SDL_Delay(100);
}
|
638c9d91 |
ret = 0; |
01310af2 |
fail:
/* close each stream */
if (is->audio_stream >= 0)
stream_component_close(is, is->audio_stream);
if (is->video_stream >= 0)
stream_component_close(is, is->video_stream); |
72ce053b |
if (is->subtitle_stream >= 0)
stream_component_close(is, is->subtitle_stream); |
638c9d91 |
if (is->ic) { |
cd3716b9 |
avformat_close_input(&is->ic); |
638c9d91 |
} |
416e3508 |
|
638c9d91 |
if (ret != 0) {
SDL_Event event; |
115329f1 |
|
638c9d91 |
event.type = FF_QUIT_EVENT;
event.user.data1 = is;
SDL_PushEvent(&event);
} |
0e5042be |
SDL_DestroyMutex(wait_mutex); |
01310af2 |
return 0;
}
|
638c9d91 |
static VideoState *stream_open(const char *filename, AVInputFormat *iformat) |
01310af2 |
{
VideoState *is;
is = av_mallocz(sizeof(VideoState));
if (!is)
return NULL; |
f7d78f36 |
av_strlcpy(is->filename, filename, sizeof(is->filename)); |
638c9d91 |
is->iformat = iformat; |
da7c65f0 |
is->ytop = 0;
is->xleft = 0; |
01310af2 |
/* start video display */
is->pictq_mutex = SDL_CreateMutex(); |
da7c65f0 |
is->pictq_cond = SDL_CreateCond(); |
115329f1 |
|
72ce053b |
is->subpq_mutex = SDL_CreateMutex(); |
da7c65f0 |
is->subpq_cond = SDL_CreateCond(); |
115329f1 |
|
a687acbb |
packet_queue_init(&is->videoq);
packet_queue_init(&is->audioq);
packet_queue_init(&is->subtitleq);
|
0e5042be |
is->continue_read_thread = SDL_CreateCond();
|
f2175a62 |
init_clock(&is->vidclk, &is->videoq.serial);
init_clock(&is->audclk, &is->audioq.serial);
init_clock(&is->extclk, &is->extclk.serial); |
26c208cf |
is->audio_clock_serial = -1; |
e96175ad |
is->audio_last_serial = -1; |
638c9d91 |
is->av_sync_type = av_sync_type; |
41211483 |
is->read_tid = SDL_CreateThread(read_thread, is); |
8adf9bb2 |
if (!is->read_tid) { |
01310af2 |
av_free(is);
return NULL;
}
return is;
}
|
7b49ce2e |
static void stream_cycle_channel(VideoState *is, int codec_type) |
638c9d91 |
{
AVFormatContext *ic = is->ic;
int start_index, stream_index; |
8c9971c3 |
int old_index; |
638c9d91 |
AVStream *st; |
543d81a7 |
AVProgram *p = NULL;
int nb_streams = is->ic->nb_streams; |
638c9d91 |
|
8c9971c3 |
if (codec_type == AVMEDIA_TYPE_VIDEO) {
start_index = is->last_video_stream;
old_index = is->video_stream;
} else if (codec_type == AVMEDIA_TYPE_AUDIO) {
start_index = is->last_audio_stream;
old_index = is->audio_stream;
} else {
start_index = is->last_subtitle_stream;
old_index = is->subtitle_stream;
} |
638c9d91 |
stream_index = start_index; |
543d81a7 |
if (codec_type != AVMEDIA_TYPE_VIDEO && is->video_stream != -1) {
p = av_find_program_from_stream(ic, NULL, is->video_stream);
if (p) {
nb_streams = p->nb_stream_indexes;
for (start_index = 0; start_index < nb_streams; start_index++)
if (p->stream_index[start_index] == stream_index)
break;
if (start_index == nb_streams)
start_index = -1;
stream_index = start_index;
}
}
|
da7c65f0 |
for (;;) { |
543d81a7 |
if (++stream_index >= nb_streams) |
72ce053b |
{ |
72415b2a |
if (codec_type == AVMEDIA_TYPE_SUBTITLE) |
72ce053b |
{
stream_index = -1; |
8c9971c3 |
is->last_subtitle_stream = -1; |
72ce053b |
goto the_end; |
8c9971c3 |
}
if (start_index == -1)
return;
stream_index = 0; |
72ce053b |
} |
638c9d91 |
if (stream_index == start_index)
return; |
543d81a7 |
st = is->ic->streams[p ? p->stream_index[stream_index] : stream_index]; |
01f4895c |
if (st->codec->codec_type == codec_type) { |
638c9d91 |
/* check that parameters are OK */ |
da7c65f0 |
switch (codec_type) { |
72415b2a |
case AVMEDIA_TYPE_AUDIO: |
01f4895c |
if (st->codec->sample_rate != 0 &&
st->codec->channels != 0) |
638c9d91 |
goto the_end;
break; |
72415b2a |
case AVMEDIA_TYPE_VIDEO:
case AVMEDIA_TYPE_SUBTITLE: |
638c9d91 |
goto the_end;
default:
break;
}
}
}
the_end: |
543d81a7 |
if (p && stream_index != -1)
stream_index = p->stream_index[stream_index]; |
8c9971c3 |
stream_component_close(is, old_index); |
638c9d91 |
stream_component_open(is, stream_index);
}
|
84506ebd |
static void toggle_full_screen(VideoState *is) |
01310af2 |
{ |
8085a5b7 |
#if defined(__APPLE__) && SDL_VERSION_ATLEAST(1, 2, 14) |
bed04e2b |
/* OS X needs to reallocate the SDL overlays */ |
d246c18e |
int i;
for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++) |
8085a5b7 |
is->pictq[i].reallocate = 1;
#endif |
d246c18e |
is_full_screen = !is_full_screen; |
5de3f724 |
video_open(is, 1, NULL); |
01310af2 |
}
|
84506ebd |
static void toggle_audio_display(VideoState *is) |
01310af2 |
{ |
84506ebd |
int bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00); |
ac37d21f |
int next = is->show_mode;
do {
next = (next + 1) % SHOW_MODE_NB;
} while (next != is->show_mode && (next == SHOW_MODE_VIDEO && !is->video_st || next != SHOW_MODE_VIDEO && !is->audio_st));
if (is->show_mode != next) {
fill_rectangle(screen,
is->xleft, is->ytop, is->width, is->height,
bgcolor, 1);
is->force_refresh = 1;
is->show_mode = next;
} |
01310af2 |
}
|
b853cfe7 |
static void refresh_loop_wait_event(VideoState *is, SDL_Event *event) { |
77ab702a |
double remaining_time = 0.0;
SDL_PumpEvents();
while (!SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS)) { |
b853cfe7 |
if (!cursor_hidden && av_gettime() - cursor_last_shown > CURSOR_HIDE_DELAY) {
SDL_ShowCursor(0);
cursor_hidden = 1;
} |
77ab702a |
if (remaining_time > 0.0)
av_usleep((int64_t)(remaining_time * 1000000.0)); |
b853cfe7 |
remaining_time = REFRESH_RATE;
if (is->show_mode != SHOW_MODE_NONE && (!is->paused || is->force_refresh))
video_refresh(is, &remaining_time);
SDL_PumpEvents(); |
77ab702a |
} |
b853cfe7 |
}
|
01310af2 |
/* handle an event sent by the GUI */ |
84506ebd |
static void event_loop(VideoState *cur_stream) |
01310af2 |
{
SDL_Event event; |
a11d11aa |
double incr, pos, frac; |
01310af2 |
|
da7c65f0 |
for (;;) { |
d52ec002 |
double x; |
b853cfe7 |
refresh_loop_wait_event(cur_stream, &event); |
da7c65f0 |
switch (event.type) { |
01310af2 |
case SDL_KEYDOWN: |
066ce8c9 |
if (exit_on_keydown) { |
84506ebd |
do_exit(cur_stream); |
066ce8c9 |
break;
} |
da7c65f0 |
switch (event.key.keysym.sym) { |
01310af2 |
case SDLK_ESCAPE:
case SDLK_q: |
84506ebd |
do_exit(cur_stream); |
01310af2 |
break;
case SDLK_f: |
84506ebd |
toggle_full_screen(cur_stream); |
dbe7170e |
cur_stream->force_refresh = 1; |
01310af2 |
break;
case SDLK_p:
case SDLK_SPACE: |
ba571f6b |
toggle_pause(cur_stream); |
01310af2 |
break; |
da7c65f0 |
case SDLK_s: // S: Step to next frame |
ba571f6b |
step_to_next_frame(cur_stream); |
bba04f1e |
break; |
01310af2 |
case SDLK_a: |
ba571f6b |
stream_cycle_channel(cur_stream, AVMEDIA_TYPE_AUDIO); |
638c9d91 |
break;
case SDLK_v: |
ba571f6b |
stream_cycle_channel(cur_stream, AVMEDIA_TYPE_VIDEO); |
638c9d91 |
break; |
3130416a |
case SDLK_c:
stream_cycle_channel(cur_stream, AVMEDIA_TYPE_VIDEO);
stream_cycle_channel(cur_stream, AVMEDIA_TYPE_AUDIO);
stream_cycle_channel(cur_stream, AVMEDIA_TYPE_SUBTITLE);
break; |
72ce053b |
case SDLK_t: |
ba571f6b |
stream_cycle_channel(cur_stream, AVMEDIA_TYPE_SUBTITLE); |
72ce053b |
break; |
638c9d91 |
case SDLK_w: |
ba571f6b |
toggle_audio_display(cur_stream); |
01310af2 |
break; |
91a3ea67 |
case SDLK_PAGEUP:
incr = 600.0;
goto do_seek;
case SDLK_PAGEDOWN:
incr = -600.0;
goto do_seek; |
72ea344b |
case SDLK_LEFT:
incr = -10.0;
goto do_seek;
case SDLK_RIGHT:
incr = 10.0;
goto do_seek;
case SDLK_UP:
incr = 60.0;
goto do_seek;
case SDLK_DOWN:
incr = -60.0;
do_seek: |
94b594c6 |
if (seek_by_bytes) { |
da7c65f0 |
if (cur_stream->video_stream >= 0 && cur_stream->video_current_pos >= 0) {
pos = cur_stream->video_current_pos;
} else if (cur_stream->audio_stream >= 0 && cur_stream->audio_pkt.pos >= 0) {
pos = cur_stream->audio_pkt.pos;
} else |
a2704c97 |
pos = avio_tell(cur_stream->ic->pb); |
94b594c6 |
if (cur_stream->ic->bit_rate) |
566cd2cb |
incr *= cur_stream->ic->bit_rate / 8.0; |
94b594c6 |
else
incr *= 180000.0;
pos += incr; |
2ef46053 |
stream_seek(cur_stream, pos, incr, 1); |
94b594c6 |
} else {
pos = get_master_clock(cur_stream); |
26c208cf |
if (isnan(pos))
pos = (double)cur_stream->seek_pos / AV_TIME_BASE; |
94b594c6 |
pos += incr; |
fc38bbcd |
if (cur_stream->ic->start_time != AV_NOPTS_VALUE && pos < cur_stream->ic->start_time / (double)AV_TIME_BASE)
pos = cur_stream->ic->start_time / (double)AV_TIME_BASE; |
2ef46053 |
stream_seek(cur_stream, (int64_t)(pos * AV_TIME_BASE), (int64_t)(incr * AV_TIME_BASE), 0); |
94b594c6 |
} |
72ea344b |
break; |
01310af2 |
default:
break;
}
break; |
dbe7170e |
case SDL_VIDEOEXPOSE:
cur_stream->force_refresh = 1;
break; |
a11d11aa |
case SDL_MOUSEBUTTONDOWN: |
066ce8c9 |
if (exit_on_mousedown) { |
84506ebd |
do_exit(cur_stream); |
066ce8c9 |
break;
} |
d52ec002 |
case SDL_MOUSEMOTION: |
ef7f3b08 |
if (cursor_hidden) {
SDL_ShowCursor(1);
cursor_hidden = 0;
}
cursor_last_shown = av_gettime(); |
da7c65f0 |
if (event.type == SDL_MOUSEBUTTONDOWN) {
x = event.button.x;
} else {
if (event.motion.state != SDL_PRESSED) |
d52ec002 |
break; |
da7c65f0 |
x = event.motion.x; |
bb270c08 |
} |
da7c65f0 |
if (seek_by_bytes || cur_stream->ic->duration <= 0) {
uint64_t size = avio_size(cur_stream->ic->pb); |
d52ec002 |
stream_seek(cur_stream, size*x/cur_stream->width, 0, 1); |
da7c65f0 |
} else { |
6371c81a |
int64_t ts;
int ns, hh, mm, ss;
int tns, thh, tmm, tss; |
da7c65f0 |
tns = cur_stream->ic->duration / 1000000LL;
thh = tns / 3600;
tmm = (tns % 3600) / 60;
tss = (tns % 60);
frac = x / cur_stream->width;
ns = frac * tns;
hh = ns / 3600;
mm = (ns % 3600) / 60;
ss = (ns % 60); |
3b491c5a |
av_log(NULL, AV_LOG_INFO,
"Seek to %2.0f%% (%2d:%02d:%02d) of total duration (%2d:%02d:%02d) \n", frac*100, |
6371c81a |
hh, mm, ss, thh, tmm, tss); |
da7c65f0 |
ts = frac * cur_stream->ic->duration; |
6371c81a |
if (cur_stream->ic->start_time != AV_NOPTS_VALUE)
ts += cur_stream->ic->start_time;
stream_seek(cur_stream, ts, 0, 0); |
2ef46053 |
} |
bb270c08 |
break; |
01310af2 |
case SDL_VIDEORESIZE: |
87917a32 |
screen = SDL_SetVideoMode(FFMIN(16383, event.resize.w), event.resize.h, 0, |
01310af2 |
SDL_HWSURFACE|SDL_RESIZABLE|SDL_ASYNCBLIT|SDL_HWACCEL); |
87917a32 |
if (!screen) { |
3b491c5a |
av_log(NULL, AV_LOG_FATAL, "Failed to set video mode\n"); |
87917a32 |
do_exit(cur_stream);
}
screen_width = cur_stream->width = screen->w;
screen_height = cur_stream->height = screen->h; |
dbe7170e |
cur_stream->force_refresh = 1; |
01310af2 |
break;
case SDL_QUIT: |
638c9d91 |
case FF_QUIT_EVENT: |
84506ebd |
do_exit(cur_stream); |
01310af2 |
break;
case FF_ALLOC_EVENT:
alloc_picture(event.user.data1);
break;
default:
break;
}
}
}
|
b1034505 |
static int opt_frame_size(void *optctx, const char *opt, const char *arg) |
e4b89522 |
{ |
940a116b |
av_log(NULL, AV_LOG_WARNING, "Option -s is deprecated, use -video_size.\n"); |
98298eb1 |
return opt_default(NULL, "video_size", arg); |
e4b89522 |
}
|
b1034505 |
static int opt_width(void *optctx, const char *opt, const char *arg) |
01310af2 |
{ |
a5b3b5f6 |
screen_width = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
return 0; |
01310af2 |
}
|
b1034505 |
static int opt_height(void *optctx, const char *opt, const char *arg) |
01310af2 |
{ |
a5b3b5f6 |
screen_height = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
return 0; |
01310af2 |
}
|
b1034505 |
static int opt_format(void *optctx, const char *opt, const char *arg) |
01310af2 |
{
file_iformat = av_find_input_format(arg);
if (!file_iformat) { |
3b491c5a |
av_log(NULL, AV_LOG_FATAL, "Unknown input format: %s\n", arg); |
eb8bc572 |
return AVERROR(EINVAL); |
01310af2 |
} |
eb8bc572 |
return 0; |
01310af2 |
} |
61890b02 |
|
b1034505 |
static int opt_frame_pix_fmt(void *optctx, const char *opt, const char *arg) |
e4b89522 |
{ |
940a116b |
av_log(NULL, AV_LOG_WARNING, "Option -pix_fmt is deprecated, use -pixel_format.\n"); |
98298eb1 |
return opt_default(NULL, "pixel_format", arg); |
e4b89522 |
}
|
b1034505 |
static int opt_sync(void *optctx, const char *opt, const char *arg) |
638c9d91 |
{
if (!strcmp(arg, "audio"))
av_sync_type = AV_SYNC_AUDIO_MASTER;
else if (!strcmp(arg, "video"))
av_sync_type = AV_SYNC_VIDEO_MASTER;
else if (!strcmp(arg, "ext"))
av_sync_type = AV_SYNC_EXTERNAL_CLOCK; |
aab1b7e5 |
else { |
3b491c5a |
av_log(NULL, AV_LOG_ERROR, "Unknown value for %s: %s\n", opt, arg); |
aab1b7e5 |
exit(1);
} |
b81d6235 |
return 0; |
638c9d91 |
}
|
b1034505 |
static int opt_seek(void *optctx, const char *opt, const char *arg) |
72ea344b |
{ |
e11bc2c6 |
start_time = parse_time_or_die(opt, arg, 1);
return 0; |
72ea344b |
}
|
98298eb1 |
static int opt_duration(void *optctx, const char *opt, const char *arg) |
d834d63b |
{
duration = parse_time_or_die(opt, arg, 1);
return 0;
}
|
b1034505 |
static int opt_show_mode(void *optctx, const char *opt, const char *arg) |
f521746b |
{
show_mode = !strcmp(arg, "video") ? SHOW_MODE_VIDEO :
!strcmp(arg, "waves") ? SHOW_MODE_WAVES :
!strcmp(arg, "rdft" ) ? SHOW_MODE_RDFT :
parse_number_or_die(opt, arg, OPT_INT, 0, SHOW_MODE_NB-1);
return 0;
}
|
d2084402 |
static void opt_input_file(void *optctx, const char *filename) |
b4af3cf3 |
{
if (input_filename) { |
3b491c5a |
av_log(NULL, AV_LOG_FATAL,
"Argument '%s' provided as input filename, but '%s' was already specified.\n", |
b4af3cf3 |
filename, input_filename); |
032ba74e |
exit(1); |
b4af3cf3 |
}
if (!strcmp(filename, "-"))
filename = "pipe:";
input_filename = filename;
}
|
cb0f97b5 |
static int opt_codec(void *optctx, const char *opt, const char *arg) |
5eda0967 |
{ |
cb0f97b5 |
const char *spec = strchr(opt, ':');
if (!spec) { |
3b491c5a |
av_log(NULL, AV_LOG_ERROR,
"No media specifier was specified in '%s' in option '%s'\n", |
cb0f97b5 |
arg, opt);
return AVERROR(EINVAL);
}
spec++;
switch (spec[0]) {
case 'a' : audio_codec_name = arg; break;
case 's' : subtitle_codec_name = arg; break;
case 'v' : video_codec_name = arg; break;
default: |
3b491c5a |
av_log(NULL, AV_LOG_ERROR,
"Invalid media specifier '%s' in option '%s'\n", spec, opt); |
cb0f97b5 |
return AVERROR(EINVAL);
}
return 0; |
5eda0967 |
}
|
d2084402 |
static int dummy;
|
358061f6 |
static const OptionDef options[] = { |
992f8eae |
#include "cmdutils_common_opts.h" |
a9a1bc56 |
{ "x", HAS_ARG, { .func_arg = opt_width }, "force displayed width", "width" },
{ "y", HAS_ARG, { .func_arg = opt_height }, "force displayed height", "height" },
{ "s", HAS_ARG | OPT_VIDEO, { .func_arg = opt_frame_size }, "set frame size (WxH or abbreviation)", "size" },
{ "fs", OPT_BOOL, { &is_full_screen }, "force full screen" },
{ "an", OPT_BOOL, { &audio_disable }, "disable audio" },
{ "vn", OPT_BOOL, { &video_disable }, "disable video" }, |
d0c6ed7d |
{ "sn", OPT_BOOL, { &subtitle_disable }, "disable subtitling" }, |
a9a1bc56 |
{ "ast", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_AUDIO] }, "select desired audio stream", "stream_number" },
{ "vst", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_VIDEO] }, "select desired video stream", "stream_number" },
{ "sst", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_SUBTITLE] }, "select desired subtitle stream", "stream_number" },
{ "ss", HAS_ARG, { .func_arg = opt_seek }, "seek to a given position in seconds", "pos" },
{ "t", HAS_ARG, { .func_arg = opt_duration }, "play \"duration\" seconds of audio/video", "duration" },
{ "bytes", OPT_INT | HAS_ARG, { &seek_by_bytes }, "seek by bytes 0=off 1=on -1=auto", "val" },
{ "nodisp", OPT_BOOL, { &display_disable }, "disable graphical display" },
{ "f", HAS_ARG, { .func_arg = opt_format }, "force format", "fmt" },
{ "pix_fmt", HAS_ARG | OPT_EXPERT | OPT_VIDEO, { .func_arg = opt_frame_pix_fmt }, "set pixel format", "format" },
{ "stats", OPT_BOOL | OPT_EXPERT, { &show_status }, "show status", "" },
{ "bug", OPT_INT | HAS_ARG | OPT_EXPERT, { &workaround_bugs }, "workaround bugs", "" },
{ "fast", OPT_BOOL | OPT_EXPERT, { &fast }, "non spec compliant optimizations", "" },
{ "genpts", OPT_BOOL | OPT_EXPERT, { &genpts }, "generate pts", "" },
{ "drp", OPT_INT | HAS_ARG | OPT_EXPERT, { &decoder_reorder_pts }, "let decoder reorder pts 0=off 1=on -1=auto", ""},
{ "lowres", OPT_INT | HAS_ARG | OPT_EXPERT, { &lowres }, "", "" },
{ "ec", OPT_INT | HAS_ARG | OPT_EXPERT, { &error_concealment }, "set error concealment options", "bit_mask" },
{ "sync", HAS_ARG | OPT_EXPERT, { .func_arg = opt_sync }, "set audio-video sync. type (type=audio/video/ext)", "type" },
{ "autoexit", OPT_BOOL | OPT_EXPERT, { &autoexit }, "exit at the end", "" },
{ "exitonkeydown", OPT_BOOL | OPT_EXPERT, { &exit_on_keydown }, "exit on key down", "" },
{ "exitonmousedown", OPT_BOOL | OPT_EXPERT, { &exit_on_mousedown }, "exit on mouse down", "" },
{ "loop", OPT_INT | HAS_ARG | OPT_EXPERT, { &loop }, "set number of times the playback shall be looped", "loop count" },
{ "framedrop", OPT_BOOL | OPT_EXPERT, { &framedrop }, "drop frames when cpu is too slow", "" },
{ "infbuf", OPT_BOOL | OPT_EXPERT, { &infinite_buffer }, "don't limit the input buffer size (useful with realtime streams)", "" },
{ "window_title", OPT_STRING | HAS_ARG, { &window_title }, "set window title", "window title" }, |
917d2bb3 |
#if CONFIG_AVFILTER |
e8bab4c6 |
{ "vf", OPT_STRING | HAS_ARG, { &vfilters }, "set video filters", "filter_graph" }, |
e96175ad |
{ "af", OPT_STRING | HAS_ARG, { &afilters }, "set audio filters", "filter_graph" }, |
917d2bb3 |
#endif |
a9a1bc56 |
{ "rdftspeed", OPT_INT | HAS_ARG| OPT_AUDIO | OPT_EXPERT, { &rdftspeed }, "rdft speed", "msecs" },
{ "showmode", HAS_ARG, { .func_arg = opt_show_mode}, "select show mode (0 = video, 1 = waves, 2 = RDFT)", "mode" },
{ "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, { .func_arg = opt_default }, "generic catch all option", "" },
{ "i", OPT_BOOL, { &dummy}, "read specified file", "input_file"}, |
cb0f97b5 |
{ "codec", HAS_ARG, { .func_arg = opt_codec}, "force decoder", "decoder_name" },
{ "acodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &audio_codec_name }, "force audio decoder", "decoder_name" },
{ "scodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &subtitle_codec_name }, "force subtitle decoder", "decoder_name" },
{ "vcodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &video_codec_name }, "force video decoder", "decoder_name" }, |
01310af2 |
{ NULL, },
};
|
0c2a18cb |
static void show_usage(void) |
01310af2 |
{ |
ceef1ee7 |
av_log(NULL, AV_LOG_INFO, "Simple media player\n");
av_log(NULL, AV_LOG_INFO, "usage: %s [options] input_file\n", program_name);
av_log(NULL, AV_LOG_INFO, "\n"); |
0c2a18cb |
}
|
a3ad68d3 |
void show_help_default(const char *opt, const char *arg) |
0c2a18cb |
{ |
f66eb58e |
av_log_set_callback(log_callback_help); |
0c2a18cb |
show_usage(); |
f9fada27 |
show_help_options(options, "Main options:", 0, OPT_EXPERT, 0);
show_help_options(options, "Advanced options:", OPT_EXPERT, 0, 0); |
f66eb58e |
printf("\n"); |
7a6cd995 |
show_help_children(avcodec_get_class(), AV_OPT_FLAG_DECODING_PARAM);
show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM); |
f66eb58e |
#if !CONFIG_AVFILTER |
7a6cd995 |
show_help_children(sws_get_class(), AV_OPT_FLAG_ENCODING_PARAM); |
5296d7b9 |
#else
show_help_children(avfilter_get_class(), AV_OPT_FLAG_FILTERING_PARAM); |
f66eb58e |
#endif |
01310af2 |
printf("\nWhile playing:\n"
"q, ESC quit\n"
"f toggle full screen\n"
"p, SPC pause\n" |
060c42bc |
"a cycle audio channel in the current program\n" |
638c9d91 |
"v cycle video channel\n" |
060c42bc |
"t cycle subtitle channel in the current program\n" |
3130416a |
"c cycle program\n" |
638c9d91 |
"w show audio waves\n" |
79f8b328 |
"s activate frame-step mode\n" |
72ea344b |
"left/right seek backward/forward 10 seconds\n"
"down/up seek backward/forward 1 minute\n" |
91a3ea67 |
"page down/page up seek backward/forward 10 minutes\n" |
a11d11aa |
"mouse click seek to percentage in file corresponding to fraction of width\n" |
01310af2 |
);
}
|
ee0ff051 |
static int lockmgr(void **mtx, enum AVLockOp op)
{
switch(op) {
case AV_LOCK_CREATE:
*mtx = SDL_CreateMutex();
if(!*mtx)
return 1;
return 0;
case AV_LOCK_OBTAIN:
return !!SDL_LockMutex(*mtx);
case AV_LOCK_RELEASE:
return !!SDL_UnlockMutex(*mtx);
case AV_LOCK_DESTROY:
SDL_DestroyMutex(*mtx);
return 0;
}
return 1;
}
|
01310af2 |
/* Called from the main */
int main(int argc, char **argv)
{ |
a5c33faa |
int flags; |
84506ebd |
VideoState *is; |
1372c826 |
char dummy_videodriver[] = "SDL_VIDEODRIVER=dummy"; |
115329f1 |
|
6b6bca64 |
av_log_set_flags(AV_LOG_SKIP_REPEATED); |
182cbe43 |
parse_loglevel(argc, argv, options); |
6b6bca64 |
|
01310af2 |
/* register all codecs, demux and protocols */ |
c721d803 |
avcodec_register_all(); |
9b157b0c |
#if CONFIG_AVDEVICE |
c721d803 |
avdevice_register_all(); |
9b157b0c |
#endif |
917d2bb3 |
#if CONFIG_AVFILTER
avfilter_register_all();
#endif |
01310af2 |
av_register_all(); |
776f2bb9 |
avformat_network_init(); |
01310af2 |
|
a5c33faa |
init_opts(); |
e43d7a18 |
|
73f2cf4e |
signal(SIGINT , sigterm_handler); /* Interrupt (ANSI). */
signal(SIGTERM, sigterm_handler); /* Termination (ANSI). */
|
452406bd |
show_banner(argc, argv, options); |
4cfac5bc |
|
7cc8d638 |
parse_options(NULL, argc, argv, options, opt_input_file); |
01310af2 |
|
aab1b7e5 |
if (!input_filename) { |
7f11e745 |
show_usage(); |
3b491c5a |
av_log(NULL, AV_LOG_FATAL, "An input file must be specified\n");
av_log(NULL, AV_LOG_FATAL,
"Use -h to get full help or, even better, run 'man %s'\n", program_name); |
aab1b7e5 |
exit(1);
} |
01310af2 |
if (display_disable) {
video_disable = 1;
} |
31319a8c |
flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER; |
86824c1d |
if (audio_disable)
flags &= ~SDL_INIT_AUDIO; |
1372c826 |
if (display_disable)
SDL_putenv(dummy_videodriver); /* For the event queue, we always need a video driver. */ |
c97f5402 |
#if !defined(__MINGW32__) && !defined(__APPLE__)
flags |= SDL_INIT_EVENTTHREAD; /* Not supported on Windows or Mac OS X */ |
31319a8c |
#endif |
01310af2 |
if (SDL_Init (flags)) { |
3b491c5a |
av_log(NULL, AV_LOG_FATAL, "Could not initialize SDL - %s\n", SDL_GetError());
av_log(NULL, AV_LOG_FATAL, "(Did you set the DISPLAY variable?)\n"); |
01310af2 |
exit(1);
}
if (!display_disable) { |
3ef17d62 |
const SDL_VideoInfo *vi = SDL_GetVideoInfo();
fs_screen_width = vi->current_w;
fs_screen_height = vi->current_h; |
01310af2 |
}
SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
|
ee0ff051 |
if (av_lockmgr_register(lockmgr)) { |
3b491c5a |
av_log(NULL, AV_LOG_FATAL, "Could not initialize lock manager!\n"); |
ee0ff051 |
do_exit(NULL);
}
|
39c6a118 |
av_init_packet(&flush_pkt); |
26a44143 |
flush_pkt.data = (uint8_t *)&flush_pkt; |
39c6a118 |
|
84506ebd |
is = stream_open(input_filename, file_iformat);
if (!is) { |
3b491c5a |
av_log(NULL, AV_LOG_FATAL, "Failed to initialize VideoState!\n"); |
84506ebd |
do_exit(NULL);
} |
01310af2 |
|
84506ebd |
event_loop(is); |
01310af2 |
/* never returns */
return 0;
} |