You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3057 lines
97KB

  1. /*
  2. * ffplay : Simple Media Player based on the FFmpeg libraries
  3. * Copyright (c) 2003 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "config.h"
  22. #include <inttypes.h>
  23. #include <math.h>
  24. #include <limits.h>
  25. #include "libavutil/avstring.h"
  26. #include "libavutil/colorspace.h"
  27. #include "libavutil/mathematics.h"
  28. #include "libavutil/pixdesc.h"
  29. #include "libavutil/imgutils.h"
  30. #include "libavutil/dict.h"
  31. #include "libavutil/parseutils.h"
  32. #include "libavutil/samplefmt.h"
  33. #include "libavutil/avassert.h"
  34. #include "libavformat/avformat.h"
  35. #include "libavdevice/avdevice.h"
  36. #include "libswscale/swscale.h"
  37. #include "libavcodec/audioconvert.h"
  38. #include "libavutil/opt.h"
  39. #include "libavcodec/avfft.h"
  40. #if CONFIG_AVFILTER
  41. # include "libavfilter/avcodec.h"
  42. # include "libavfilter/avfilter.h"
  43. # include "libavfilter/avfiltergraph.h"
  44. # include "libavfilter/vsink_buffer.h"
  45. #endif
  46. #include <SDL.h>
  47. #include <SDL_thread.h>
  48. #include "cmdutils.h"
  49. #include <unistd.h>
  50. #include <assert.h>
  51. const char program_name[] = "ffplay";
  52. const int program_birth_year = 2003;
  53. #define MAX_QUEUE_SIZE (15 * 1024 * 1024)
  54. #define MIN_AUDIOQ_SIZE (20 * 16 * 1024)
  55. #define MIN_FRAMES 5
  56. /* SDL audio buffer size, in samples. Should be small to have precise
  57. A/V sync as SDL does not have hardware buffer fullness info. */
  58. #define SDL_AUDIO_BUFFER_SIZE 1024
  59. /* no AV sync correction is done if below the AV sync threshold */
  60. #define AV_SYNC_THRESHOLD 0.01
  61. /* no AV correction is done if too big error */
  62. #define AV_NOSYNC_THRESHOLD 10.0
  63. #define FRAME_SKIP_FACTOR 0.05
  64. /* maximum audio speed change to get correct sync */
  65. #define SAMPLE_CORRECTION_PERCENT_MAX 10
  66. /* we use about AUDIO_DIFF_AVG_NB A-V differences to make the average */
  67. #define AUDIO_DIFF_AVG_NB 20
  68. /* NOTE: the size must be big enough to compensate the hardware audio buffersize size */
  69. #define SAMPLE_ARRAY_SIZE (2*65536)
  70. static int sws_flags = SWS_BICUBIC;
  71. typedef struct PacketQueue {
  72. AVPacketList *first_pkt, *last_pkt;
  73. int nb_packets;
  74. int size;
  75. int abort_request;
  76. SDL_mutex *mutex;
  77. SDL_cond *cond;
  78. } PacketQueue;
  79. #define VIDEO_PICTURE_QUEUE_SIZE 2
  80. #define SUBPICTURE_QUEUE_SIZE 4
  81. typedef struct VideoPicture {
  82. double pts; ///<presentation time stamp for this picture
  83. double target_clock; ///<av_gettime() time at which this should be displayed ideally
  84. int64_t pos; ///<byte position in file
  85. SDL_Overlay *bmp;
  86. int width, height; /* source height & width */
  87. int allocated;
  88. enum PixelFormat pix_fmt;
  89. #if CONFIG_AVFILTER
  90. AVFilterBufferRef *picref;
  91. #endif
  92. } VideoPicture;
  93. typedef struct SubPicture {
  94. double pts; /* presentation time stamp for this picture */
  95. AVSubtitle sub;
  96. } SubPicture;
  97. enum {
  98. AV_SYNC_AUDIO_MASTER, /* default choice */
  99. AV_SYNC_VIDEO_MASTER,
  100. AV_SYNC_EXTERNAL_CLOCK, /* synchronize to an external clock */
  101. };
  102. typedef struct VideoState {
  103. SDL_Thread *read_tid;
  104. SDL_Thread *video_tid;
  105. SDL_Thread *refresh_tid;
  106. AVInputFormat *iformat;
  107. int no_background;
  108. int abort_request;
  109. int paused;
  110. int last_paused;
  111. int seek_req;
  112. int seek_flags;
  113. int64_t seek_pos;
  114. int64_t seek_rel;
  115. int read_pause_return;
  116. AVFormatContext *ic;
  117. int audio_stream;
  118. int av_sync_type;
  119. double external_clock; /* external clock base */
  120. int64_t external_clock_time;
  121. double audio_clock;
  122. double audio_diff_cum; /* used for AV difference average computation */
  123. double audio_diff_avg_coef;
  124. double audio_diff_threshold;
  125. int audio_diff_avg_count;
  126. AVStream *audio_st;
  127. PacketQueue audioq;
  128. int audio_hw_buf_size;
  129. /* samples output by the codec. we reserve more space for avsync
  130. compensation */
  131. DECLARE_ALIGNED(16,uint8_t,audio_buf1)[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2];
  132. DECLARE_ALIGNED(16,uint8_t,audio_buf2)[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2];
  133. uint8_t *audio_buf;
  134. unsigned int audio_buf_size; /* in bytes */
  135. int audio_buf_index; /* in bytes */
  136. AVPacket audio_pkt_temp;
  137. AVPacket audio_pkt;
  138. enum AVSampleFormat audio_src_fmt;
  139. AVAudioConvert *reformat_ctx;
  140. enum ShowMode {
  141. SHOW_MODE_NONE = -1, SHOW_MODE_VIDEO = 0, SHOW_MODE_WAVES, SHOW_MODE_RDFT, SHOW_MODE_NB
  142. } show_mode;
  143. int16_t sample_array[SAMPLE_ARRAY_SIZE];
  144. int sample_array_index;
  145. int last_i_start;
  146. RDFTContext *rdft;
  147. int rdft_bits;
  148. FFTSample *rdft_data;
  149. int xpos;
  150. SDL_Thread *subtitle_tid;
  151. int subtitle_stream;
  152. int subtitle_stream_changed;
  153. AVStream *subtitle_st;
  154. PacketQueue subtitleq;
  155. SubPicture subpq[SUBPICTURE_QUEUE_SIZE];
  156. int subpq_size, subpq_rindex, subpq_windex;
  157. SDL_mutex *subpq_mutex;
  158. SDL_cond *subpq_cond;
  159. double frame_timer;
  160. double frame_last_pts;
  161. double frame_last_delay;
  162. double video_clock; ///<pts of last decoded frame / predicted pts of next decoded frame
  163. int video_stream;
  164. AVStream *video_st;
  165. PacketQueue videoq;
  166. double video_current_pts; ///<current displayed pts (different from video_clock if frame fifos are used)
  167. double video_current_pts_drift; ///<video_current_pts - time (av_gettime) at which we updated video_current_pts - used to have running video pts
  168. int64_t video_current_pos; ///<current displayed file pos
  169. VideoPicture pictq[VIDEO_PICTURE_QUEUE_SIZE];
  170. int pictq_size, pictq_rindex, pictq_windex;
  171. SDL_mutex *pictq_mutex;
  172. SDL_cond *pictq_cond;
  173. #if !CONFIG_AVFILTER
  174. struct SwsContext *img_convert_ctx;
  175. #endif
  176. char filename[1024];
  177. int width, height, xleft, ytop;
  178. #if CONFIG_AVFILTER
  179. AVFilterContext *out_video_filter; ///<the last filter in the video chain
  180. #endif
  181. float skip_frames;
  182. float skip_frames_index;
  183. int refresh;
  184. } VideoState;
  185. static int opt_help(const char *opt, const char *arg);
  186. /* options specified by the user */
  187. static AVInputFormat *file_iformat;
  188. static const char *input_filename;
  189. static const char *window_title;
  190. static int fs_screen_width;
  191. static int fs_screen_height;
  192. static int screen_width = 0;
  193. static int screen_height = 0;
  194. static int frame_width = 0;
  195. static int frame_height = 0;
  196. static enum PixelFormat frame_pix_fmt = PIX_FMT_NONE;
  197. static int audio_disable;
  198. static int video_disable;
  199. static int wanted_stream[AVMEDIA_TYPE_NB]={
  200. [AVMEDIA_TYPE_AUDIO]=-1,
  201. [AVMEDIA_TYPE_VIDEO]=-1,
  202. [AVMEDIA_TYPE_SUBTITLE]=-1,
  203. };
  204. static int seek_by_bytes=-1;
  205. static int display_disable;
  206. static int show_status = 1;
  207. static int av_sync_type = AV_SYNC_AUDIO_MASTER;
  208. static int64_t start_time = AV_NOPTS_VALUE;
  209. static int64_t duration = AV_NOPTS_VALUE;
  210. static int step = 0;
  211. static int thread_count = 1;
  212. static int workaround_bugs = 1;
  213. static int fast = 0;
  214. static int genpts = 0;
  215. static int lowres = 0;
  216. static int idct = FF_IDCT_AUTO;
  217. static enum AVDiscard skip_frame= AVDISCARD_DEFAULT;
  218. static enum AVDiscard skip_idct= AVDISCARD_DEFAULT;
  219. static enum AVDiscard skip_loop_filter= AVDISCARD_DEFAULT;
  220. static int error_recognition = FF_ER_CAREFUL;
  221. static int error_concealment = 3;
  222. static int decoder_reorder_pts= -1;
  223. static int autoexit;
  224. static int exit_on_keydown;
  225. static int exit_on_mousedown;
  226. static int loop=1;
  227. static int framedrop=-1;
  228. static enum ShowMode show_mode = SHOW_MODE_NONE;
  229. static int rdftspeed=20;
  230. #if CONFIG_AVFILTER
  231. static char *vfilters = NULL;
  232. #endif
  233. /* current context */
  234. static int is_full_screen;
  235. static VideoState *cur_stream;
  236. static int64_t audio_callback_time;
  237. static AVPacket flush_pkt;
  238. #define FF_ALLOC_EVENT (SDL_USEREVENT)
  239. #define FF_REFRESH_EVENT (SDL_USEREVENT + 1)
  240. #define FF_QUIT_EVENT (SDL_USEREVENT + 2)
  241. static SDL_Surface *screen;
  242. static int packet_queue_put(PacketQueue *q, AVPacket *pkt)
  243. {
  244. AVPacketList *pkt1;
  245. /* duplicate the packet */
  246. if (pkt!=&flush_pkt && av_dup_packet(pkt) < 0)
  247. return -1;
  248. pkt1 = av_malloc(sizeof(AVPacketList));
  249. if (!pkt1)
  250. return -1;
  251. pkt1->pkt = *pkt;
  252. pkt1->next = NULL;
  253. SDL_LockMutex(q->mutex);
  254. if (!q->last_pkt)
  255. q->first_pkt = pkt1;
  256. else
  257. q->last_pkt->next = pkt1;
  258. q->last_pkt = pkt1;
  259. q->nb_packets++;
  260. q->size += pkt1->pkt.size + sizeof(*pkt1);
  261. /* XXX: should duplicate packet data in DV case */
  262. SDL_CondSignal(q->cond);
  263. SDL_UnlockMutex(q->mutex);
  264. return 0;
  265. }
  266. /* packet queue handling */
  267. static void packet_queue_init(PacketQueue *q)
  268. {
  269. memset(q, 0, sizeof(PacketQueue));
  270. q->mutex = SDL_CreateMutex();
  271. q->cond = SDL_CreateCond();
  272. packet_queue_put(q, &flush_pkt);
  273. }
  274. static void packet_queue_flush(PacketQueue *q)
  275. {
  276. AVPacketList *pkt, *pkt1;
  277. SDL_LockMutex(q->mutex);
  278. for(pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
  279. pkt1 = pkt->next;
  280. av_free_packet(&pkt->pkt);
  281. av_freep(&pkt);
  282. }
  283. q->last_pkt = NULL;
  284. q->first_pkt = NULL;
  285. q->nb_packets = 0;
  286. q->size = 0;
  287. SDL_UnlockMutex(q->mutex);
  288. }
  289. static void packet_queue_end(PacketQueue *q)
  290. {
  291. packet_queue_flush(q);
  292. SDL_DestroyMutex(q->mutex);
  293. SDL_DestroyCond(q->cond);
  294. }
  295. static void packet_queue_abort(PacketQueue *q)
  296. {
  297. SDL_LockMutex(q->mutex);
  298. q->abort_request = 1;
  299. SDL_CondSignal(q->cond);
  300. SDL_UnlockMutex(q->mutex);
  301. }
  302. /* return < 0 if aborted, 0 if no packet and > 0 if packet. */
  303. static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block)
  304. {
  305. AVPacketList *pkt1;
  306. int ret;
  307. SDL_LockMutex(q->mutex);
  308. for(;;) {
  309. if (q->abort_request) {
  310. ret = -1;
  311. break;
  312. }
  313. pkt1 = q->first_pkt;
  314. if (pkt1) {
  315. q->first_pkt = pkt1->next;
  316. if (!q->first_pkt)
  317. q->last_pkt = NULL;
  318. q->nb_packets--;
  319. q->size -= pkt1->pkt.size + sizeof(*pkt1);
  320. *pkt = pkt1->pkt;
  321. av_free(pkt1);
  322. ret = 1;
  323. break;
  324. } else if (!block) {
  325. ret = 0;
  326. break;
  327. } else {
  328. SDL_CondWait(q->cond, q->mutex);
  329. }
  330. }
  331. SDL_UnlockMutex(q->mutex);
  332. return ret;
  333. }
  334. static inline void fill_rectangle(SDL_Surface *screen,
  335. int x, int y, int w, int h, int color)
  336. {
  337. SDL_Rect rect;
  338. rect.x = x;
  339. rect.y = y;
  340. rect.w = w;
  341. rect.h = h;
  342. SDL_FillRect(screen, &rect, color);
  343. }
  344. #define ALPHA_BLEND(a, oldp, newp, s)\
  345. ((((oldp << s) * (255 - (a))) + (newp * (a))) / (255 << s))
  346. #define RGBA_IN(r, g, b, a, s)\
  347. {\
  348. unsigned int v = ((const uint32_t *)(s))[0];\
  349. a = (v >> 24) & 0xff;\
  350. r = (v >> 16) & 0xff;\
  351. g = (v >> 8) & 0xff;\
  352. b = v & 0xff;\
  353. }
  354. #define YUVA_IN(y, u, v, a, s, pal)\
  355. {\
  356. unsigned int val = ((const uint32_t *)(pal))[*(const uint8_t*)(s)];\
  357. a = (val >> 24) & 0xff;\
  358. y = (val >> 16) & 0xff;\
  359. u = (val >> 8) & 0xff;\
  360. v = val & 0xff;\
  361. }
  362. #define YUVA_OUT(d, y, u, v, a)\
  363. {\
  364. ((uint32_t *)(d))[0] = (a << 24) | (y << 16) | (u << 8) | v;\
  365. }
  366. #define BPP 1
  367. static void blend_subrect(AVPicture *dst, const AVSubtitleRect *rect, int imgw, int imgh)
  368. {
  369. int wrap, wrap3, width2, skip2;
  370. int y, u, v, a, u1, v1, a1, w, h;
  371. uint8_t *lum, *cb, *cr;
  372. const uint8_t *p;
  373. const uint32_t *pal;
  374. int dstx, dsty, dstw, dsth;
  375. dstw = av_clip(rect->w, 0, imgw);
  376. dsth = av_clip(rect->h, 0, imgh);
  377. dstx = av_clip(rect->x, 0, imgw - dstw);
  378. dsty = av_clip(rect->y, 0, imgh - dsth);
  379. lum = dst->data[0] + dsty * dst->linesize[0];
  380. cb = dst->data[1] + (dsty >> 1) * dst->linesize[1];
  381. cr = dst->data[2] + (dsty >> 1) * dst->linesize[2];
  382. width2 = ((dstw + 1) >> 1) + (dstx & ~dstw & 1);
  383. skip2 = dstx >> 1;
  384. wrap = dst->linesize[0];
  385. wrap3 = rect->pict.linesize[0];
  386. p = rect->pict.data[0];
  387. pal = (const uint32_t *)rect->pict.data[1]; /* Now in YCrCb! */
  388. if (dsty & 1) {
  389. lum += dstx;
  390. cb += skip2;
  391. cr += skip2;
  392. if (dstx & 1) {
  393. YUVA_IN(y, u, v, a, p, pal);
  394. lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
  395. cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
  396. cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
  397. cb++;
  398. cr++;
  399. lum++;
  400. p += BPP;
  401. }
  402. for(w = dstw - (dstx & 1); w >= 2; w -= 2) {
  403. YUVA_IN(y, u, v, a, p, pal);
  404. u1 = u;
  405. v1 = v;
  406. a1 = a;
  407. lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
  408. YUVA_IN(y, u, v, a, p + BPP, pal);
  409. u1 += u;
  410. v1 += v;
  411. a1 += a;
  412. lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
  413. cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
  414. cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
  415. cb++;
  416. cr++;
  417. p += 2 * BPP;
  418. lum += 2;
  419. }
  420. if (w) {
  421. YUVA_IN(y, u, v, a, p, pal);
  422. lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
  423. cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
  424. cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
  425. p++;
  426. lum++;
  427. }
  428. p += wrap3 - dstw * BPP;
  429. lum += wrap - dstw - dstx;
  430. cb += dst->linesize[1] - width2 - skip2;
  431. cr += dst->linesize[2] - width2 - skip2;
  432. }
  433. for(h = dsth - (dsty & 1); h >= 2; h -= 2) {
  434. lum += dstx;
  435. cb += skip2;
  436. cr += skip2;
  437. if (dstx & 1) {
  438. YUVA_IN(y, u, v, a, p, pal);
  439. u1 = u;
  440. v1 = v;
  441. a1 = a;
  442. lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
  443. p += wrap3;
  444. lum += wrap;
  445. YUVA_IN(y, u, v, a, p, pal);
  446. u1 += u;
  447. v1 += v;
  448. a1 += a;
  449. lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
  450. cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
  451. cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
  452. cb++;
  453. cr++;
  454. p += -wrap3 + BPP;
  455. lum += -wrap + 1;
  456. }
  457. for(w = dstw - (dstx & 1); w >= 2; w -= 2) {
  458. YUVA_IN(y, u, v, a, p, pal);
  459. u1 = u;
  460. v1 = v;
  461. a1 = a;
  462. lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
  463. YUVA_IN(y, u, v, a, p + BPP, pal);
  464. u1 += u;
  465. v1 += v;
  466. a1 += a;
  467. lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
  468. p += wrap3;
  469. lum += wrap;
  470. YUVA_IN(y, u, v, a, p, pal);
  471. u1 += u;
  472. v1 += v;
  473. a1 += a;
  474. lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
  475. YUVA_IN(y, u, v, a, p + BPP, pal);
  476. u1 += u;
  477. v1 += v;
  478. a1 += a;
  479. lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
  480. cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 2);
  481. cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 2);
  482. cb++;
  483. cr++;
  484. p += -wrap3 + 2 * BPP;
  485. lum += -wrap + 2;
  486. }
  487. if (w) {
  488. YUVA_IN(y, u, v, a, p, pal);
  489. u1 = u;
  490. v1 = v;
  491. a1 = a;
  492. lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
  493. p += wrap3;
  494. lum += wrap;
  495. YUVA_IN(y, u, v, a, p, pal);
  496. u1 += u;
  497. v1 += v;
  498. a1 += a;
  499. lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
  500. cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
  501. cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
  502. cb++;
  503. cr++;
  504. p += -wrap3 + BPP;
  505. lum += -wrap + 1;
  506. }
  507. p += wrap3 + (wrap3 - dstw * BPP);
  508. lum += wrap + (wrap - dstw - dstx);
  509. cb += dst->linesize[1] - width2 - skip2;
  510. cr += dst->linesize[2] - width2 - skip2;
  511. }
  512. /* handle odd height */
  513. if (h) {
  514. lum += dstx;
  515. cb += skip2;
  516. cr += skip2;
  517. if (dstx & 1) {
  518. YUVA_IN(y, u, v, a, p, pal);
  519. lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
  520. cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
  521. cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
  522. cb++;
  523. cr++;
  524. lum++;
  525. p += BPP;
  526. }
  527. for(w = dstw - (dstx & 1); w >= 2; w -= 2) {
  528. YUVA_IN(y, u, v, a, p, pal);
  529. u1 = u;
  530. v1 = v;
  531. a1 = a;
  532. lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
  533. YUVA_IN(y, u, v, a, p + BPP, pal);
  534. u1 += u;
  535. v1 += v;
  536. a1 += a;
  537. lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
  538. cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u, 1);
  539. cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v, 1);
  540. cb++;
  541. cr++;
  542. p += 2 * BPP;
  543. lum += 2;
  544. }
  545. if (w) {
  546. YUVA_IN(y, u, v, a, p, pal);
  547. lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
  548. cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
  549. cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
  550. }
  551. }
  552. }
  553. static void free_subpicture(SubPicture *sp)
  554. {
  555. avsubtitle_free(&sp->sub);
  556. }
  557. static void video_image_display(VideoState *is)
  558. {
  559. VideoPicture *vp;
  560. SubPicture *sp;
  561. AVPicture pict;
  562. float aspect_ratio;
  563. int width, height, x, y;
  564. SDL_Rect rect;
  565. int i;
  566. vp = &is->pictq[is->pictq_rindex];
  567. if (vp->bmp) {
  568. #if CONFIG_AVFILTER
  569. if (vp->picref->video->sample_aspect_ratio.num == 0)
  570. aspect_ratio = 0;
  571. else
  572. aspect_ratio = av_q2d(vp->picref->video->sample_aspect_ratio);
  573. #else
  574. /* XXX: use variable in the frame */
  575. if (is->video_st->sample_aspect_ratio.num)
  576. aspect_ratio = av_q2d(is->video_st->sample_aspect_ratio);
  577. else if (is->video_st->codec->sample_aspect_ratio.num)
  578. aspect_ratio = av_q2d(is->video_st->codec->sample_aspect_ratio);
  579. else
  580. aspect_ratio = 0;
  581. #endif
  582. if (aspect_ratio <= 0.0)
  583. aspect_ratio = 1.0;
  584. aspect_ratio *= (float)vp->width / (float)vp->height;
  585. if (is->subtitle_st) {
  586. if (is->subpq_size > 0) {
  587. sp = &is->subpq[is->subpq_rindex];
  588. if (vp->pts >= sp->pts + ((float) sp->sub.start_display_time / 1000)) {
  589. SDL_LockYUVOverlay (vp->bmp);
  590. pict.data[0] = vp->bmp->pixels[0];
  591. pict.data[1] = vp->bmp->pixels[2];
  592. pict.data[2] = vp->bmp->pixels[1];
  593. pict.linesize[0] = vp->bmp->pitches[0];
  594. pict.linesize[1] = vp->bmp->pitches[2];
  595. pict.linesize[2] = vp->bmp->pitches[1];
  596. for (i = 0; i < sp->sub.num_rects; i++)
  597. blend_subrect(&pict, sp->sub.rects[i],
  598. vp->bmp->w, vp->bmp->h);
  599. SDL_UnlockYUVOverlay (vp->bmp);
  600. }
  601. }
  602. }
  603. /* XXX: we suppose the screen has a 1.0 pixel ratio */
  604. height = is->height;
  605. width = ((int)rint(height * aspect_ratio)) & ~1;
  606. if (width > is->width) {
  607. width = is->width;
  608. height = ((int)rint(width / aspect_ratio)) & ~1;
  609. }
  610. x = (is->width - width) / 2;
  611. y = (is->height - height) / 2;
  612. is->no_background = 0;
  613. rect.x = is->xleft + x;
  614. rect.y = is->ytop + y;
  615. rect.w = FFMAX(width, 1);
  616. rect.h = FFMAX(height, 1);
  617. SDL_DisplayYUVOverlay(vp->bmp, &rect);
  618. }
  619. }
  620. /* get the current audio output buffer size, in samples. With SDL, we
  621. cannot have a precise information */
  622. static int audio_write_get_buf_size(VideoState *is)
  623. {
  624. return is->audio_buf_size - is->audio_buf_index;
  625. }
  626. static inline int compute_mod(int a, int b)
  627. {
  628. return a < 0 ? a%b + b : a%b;
  629. }
  630. static void video_audio_display(VideoState *s)
  631. {
  632. int i, i_start, x, y1, y, ys, delay, n, nb_display_channels;
  633. int ch, channels, h, h2, bgcolor, fgcolor;
  634. int16_t time_diff;
  635. int rdft_bits, nb_freq;
  636. for(rdft_bits=1; (1<<rdft_bits)<2*s->height; rdft_bits++)
  637. ;
  638. nb_freq= 1<<(rdft_bits-1);
  639. /* compute display index : center on currently output samples */
  640. channels = s->audio_st->codec->channels;
  641. nb_display_channels = channels;
  642. if (!s->paused) {
  643. int data_used= s->show_mode == SHOW_MODE_WAVES ? s->width : (2*nb_freq);
  644. n = 2 * channels;
  645. delay = audio_write_get_buf_size(s);
  646. delay /= n;
  647. /* to be more precise, we take into account the time spent since
  648. the last buffer computation */
  649. if (audio_callback_time) {
  650. time_diff = av_gettime() - audio_callback_time;
  651. delay -= (time_diff * s->audio_st->codec->sample_rate) / 1000000;
  652. }
  653. delay += 2*data_used;
  654. if (delay < data_used)
  655. delay = data_used;
  656. i_start= x = compute_mod(s->sample_array_index - delay * channels, SAMPLE_ARRAY_SIZE);
  657. if (s->show_mode == SHOW_MODE_WAVES) {
  658. h= INT_MIN;
  659. for(i=0; i<1000; i+=channels){
  660. int idx= (SAMPLE_ARRAY_SIZE + x - i) % SAMPLE_ARRAY_SIZE;
  661. int a= s->sample_array[idx];
  662. int b= s->sample_array[(idx + 4*channels)%SAMPLE_ARRAY_SIZE];
  663. int c= s->sample_array[(idx + 5*channels)%SAMPLE_ARRAY_SIZE];
  664. int d= s->sample_array[(idx + 9*channels)%SAMPLE_ARRAY_SIZE];
  665. int score= a-d;
  666. if(h<score && (b^c)<0){
  667. h= score;
  668. i_start= idx;
  669. }
  670. }
  671. }
  672. s->last_i_start = i_start;
  673. } else {
  674. i_start = s->last_i_start;
  675. }
  676. bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
  677. if (s->show_mode == SHOW_MODE_WAVES) {
  678. fill_rectangle(screen,
  679. s->xleft, s->ytop, s->width, s->height,
  680. bgcolor);
  681. fgcolor = SDL_MapRGB(screen->format, 0xff, 0xff, 0xff);
  682. /* total height for one channel */
  683. h = s->height / nb_display_channels;
  684. /* graph height / 2 */
  685. h2 = (h * 9) / 20;
  686. for(ch = 0;ch < nb_display_channels; ch++) {
  687. i = i_start + ch;
  688. y1 = s->ytop + ch * h + (h / 2); /* position of center line */
  689. for(x = 0; x < s->width; x++) {
  690. y = (s->sample_array[i] * h2) >> 15;
  691. if (y < 0) {
  692. y = -y;
  693. ys = y1 - y;
  694. } else {
  695. ys = y1;
  696. }
  697. fill_rectangle(screen,
  698. s->xleft + x, ys, 1, y,
  699. fgcolor);
  700. i += channels;
  701. if (i >= SAMPLE_ARRAY_SIZE)
  702. i -= SAMPLE_ARRAY_SIZE;
  703. }
  704. }
  705. fgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0xff);
  706. for(ch = 1;ch < nb_display_channels; ch++) {
  707. y = s->ytop + ch * h;
  708. fill_rectangle(screen,
  709. s->xleft, y, s->width, 1,
  710. fgcolor);
  711. }
  712. SDL_UpdateRect(screen, s->xleft, s->ytop, s->width, s->height);
  713. }else{
  714. nb_display_channels= FFMIN(nb_display_channels, 2);
  715. if(rdft_bits != s->rdft_bits){
  716. av_rdft_end(s->rdft);
  717. av_free(s->rdft_data);
  718. s->rdft = av_rdft_init(rdft_bits, DFT_R2C);
  719. s->rdft_bits= rdft_bits;
  720. s->rdft_data= av_malloc(4*nb_freq*sizeof(*s->rdft_data));
  721. }
  722. {
  723. FFTSample *data[2];
  724. for(ch = 0;ch < nb_display_channels; ch++) {
  725. data[ch] = s->rdft_data + 2*nb_freq*ch;
  726. i = i_start + ch;
  727. for(x = 0; x < 2*nb_freq; x++) {
  728. double w= (x-nb_freq)*(1.0/nb_freq);
  729. data[ch][x]= s->sample_array[i]*(1.0-w*w);
  730. i += channels;
  731. if (i >= SAMPLE_ARRAY_SIZE)
  732. i -= SAMPLE_ARRAY_SIZE;
  733. }
  734. av_rdft_calc(s->rdft, data[ch]);
  735. }
  736. //least efficient way to do this, we should of course directly access it but its more than fast enough
  737. for(y=0; y<s->height; y++){
  738. double w= 1/sqrt(nb_freq);
  739. 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]));
  740. int b= (nb_display_channels == 2 ) ? sqrt(w*sqrt(data[1][2*y+0]*data[1][2*y+0]
  741. + data[1][2*y+1]*data[1][2*y+1])) : a;
  742. a= FFMIN(a,255);
  743. b= FFMIN(b,255);
  744. fgcolor = SDL_MapRGB(screen->format, a, b, (a+b)/2);
  745. fill_rectangle(screen,
  746. s->xpos, s->height-y, 1, 1,
  747. fgcolor);
  748. }
  749. }
  750. SDL_UpdateRect(screen, s->xpos, s->ytop, 1, s->height);
  751. s->xpos++;
  752. if(s->xpos >= s->width)
  753. s->xpos= s->xleft;
  754. }
  755. }
  756. static void stream_close(VideoState *is)
  757. {
  758. VideoPicture *vp;
  759. int i;
  760. /* XXX: use a special url_shutdown call to abort parse cleanly */
  761. is->abort_request = 1;
  762. SDL_WaitThread(is->read_tid, NULL);
  763. SDL_WaitThread(is->refresh_tid, NULL);
  764. /* free all pictures */
  765. for(i=0;i<VIDEO_PICTURE_QUEUE_SIZE; i++) {
  766. vp = &is->pictq[i];
  767. #if CONFIG_AVFILTER
  768. if (vp->picref) {
  769. avfilter_unref_buffer(vp->picref);
  770. vp->picref = NULL;
  771. }
  772. #endif
  773. if (vp->bmp) {
  774. SDL_FreeYUVOverlay(vp->bmp);
  775. vp->bmp = NULL;
  776. }
  777. }
  778. SDL_DestroyMutex(is->pictq_mutex);
  779. SDL_DestroyCond(is->pictq_cond);
  780. SDL_DestroyMutex(is->subpq_mutex);
  781. SDL_DestroyCond(is->subpq_cond);
  782. #if !CONFIG_AVFILTER
  783. if (is->img_convert_ctx)
  784. sws_freeContext(is->img_convert_ctx);
  785. #endif
  786. av_free(is);
  787. }
  788. static void do_exit(void)
  789. {
  790. if (cur_stream) {
  791. stream_close(cur_stream);
  792. cur_stream = NULL;
  793. }
  794. uninit_opts();
  795. #if CONFIG_AVFILTER
  796. avfilter_uninit();
  797. #endif
  798. if (show_status)
  799. printf("\n");
  800. SDL_Quit();
  801. av_log(NULL, AV_LOG_QUIET, "%s", "");
  802. exit(0);
  803. }
  804. static int video_open(VideoState *is){
  805. int flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
  806. int w,h;
  807. if(is_full_screen) flags |= SDL_FULLSCREEN;
  808. else flags |= SDL_RESIZABLE;
  809. if (is_full_screen && fs_screen_width) {
  810. w = fs_screen_width;
  811. h = fs_screen_height;
  812. } else if(!is_full_screen && screen_width){
  813. w = screen_width;
  814. h = screen_height;
  815. #if CONFIG_AVFILTER
  816. }else if (is->out_video_filter && is->out_video_filter->inputs[0]){
  817. w = is->out_video_filter->inputs[0]->w;
  818. h = is->out_video_filter->inputs[0]->h;
  819. #else
  820. }else if (is->video_st && is->video_st->codec->width){
  821. w = is->video_st->codec->width;
  822. h = is->video_st->codec->height;
  823. #endif
  824. } else {
  825. w = 640;
  826. h = 480;
  827. }
  828. if(screen && is->width == screen->w && screen->w == w
  829. && is->height== screen->h && screen->h == h)
  830. return 0;
  831. #ifndef __APPLE__
  832. screen = SDL_SetVideoMode(w, h, 0, flags);
  833. #else
  834. /* setting bits_per_pixel = 0 or 32 causes blank video on OS X */
  835. screen = SDL_SetVideoMode(w, h, 24, flags);
  836. #endif
  837. if (!screen) {
  838. fprintf(stderr, "SDL: could not set video mode - exiting\n");
  839. do_exit();
  840. }
  841. if (!window_title)
  842. window_title = input_filename;
  843. SDL_WM_SetCaption(window_title, window_title);
  844. is->width = screen->w;
  845. is->height = screen->h;
  846. return 0;
  847. }
  848. /* display the current picture, if any */
  849. static void video_display(VideoState *is)
  850. {
  851. if(!screen)
  852. video_open(cur_stream);
  853. if (is->audio_st && is->show_mode != SHOW_MODE_VIDEO)
  854. video_audio_display(is);
  855. else if (is->video_st)
  856. video_image_display(is);
  857. }
  858. static int refresh_thread(void *opaque)
  859. {
  860. VideoState *is= opaque;
  861. while(!is->abort_request){
  862. SDL_Event event;
  863. event.type = FF_REFRESH_EVENT;
  864. event.user.data1 = opaque;
  865. if(!is->refresh){
  866. is->refresh=1;
  867. SDL_PushEvent(&event);
  868. }
  869. //FIXME ideally we should wait the correct time but SDLs event passing is so slow it would be silly
  870. usleep(is->audio_st && is->show_mode != SHOW_MODE_VIDEO ? rdftspeed*1000 : 5000);
  871. }
  872. return 0;
  873. }
  874. /* get the current audio clock value */
  875. static double get_audio_clock(VideoState *is)
  876. {
  877. double pts;
  878. int hw_buf_size, bytes_per_sec;
  879. pts = is->audio_clock;
  880. hw_buf_size = audio_write_get_buf_size(is);
  881. bytes_per_sec = 0;
  882. if (is->audio_st) {
  883. bytes_per_sec = is->audio_st->codec->sample_rate *
  884. 2 * is->audio_st->codec->channels;
  885. }
  886. if (bytes_per_sec)
  887. pts -= (double)hw_buf_size / bytes_per_sec;
  888. return pts;
  889. }
  890. /* get the current video clock value */
  891. static double get_video_clock(VideoState *is)
  892. {
  893. if (is->paused) {
  894. return is->video_current_pts;
  895. } else {
  896. return is->video_current_pts_drift + av_gettime() / 1000000.0;
  897. }
  898. }
  899. /* get the current external clock value */
  900. static double get_external_clock(VideoState *is)
  901. {
  902. int64_t ti;
  903. ti = av_gettime();
  904. return is->external_clock + ((ti - is->external_clock_time) * 1e-6);
  905. }
  906. /* get the current master clock value */
  907. static double get_master_clock(VideoState *is)
  908. {
  909. double val;
  910. if (is->av_sync_type == AV_SYNC_VIDEO_MASTER) {
  911. if (is->video_st)
  912. val = get_video_clock(is);
  913. else
  914. val = get_audio_clock(is);
  915. } else if (is->av_sync_type == AV_SYNC_AUDIO_MASTER) {
  916. if (is->audio_st)
  917. val = get_audio_clock(is);
  918. else
  919. val = get_video_clock(is);
  920. } else {
  921. val = get_external_clock(is);
  922. }
  923. return val;
  924. }
  925. /* seek in the stream */
  926. static void stream_seek(VideoState *is, int64_t pos, int64_t rel, int seek_by_bytes)
  927. {
  928. if (!is->seek_req) {
  929. is->seek_pos = pos;
  930. is->seek_rel = rel;
  931. is->seek_flags &= ~AVSEEK_FLAG_BYTE;
  932. if (seek_by_bytes)
  933. is->seek_flags |= AVSEEK_FLAG_BYTE;
  934. is->seek_req = 1;
  935. }
  936. }
  937. /* pause or resume the video */
  938. static void stream_toggle_pause(VideoState *is)
  939. {
  940. if (is->paused) {
  941. is->frame_timer += av_gettime() / 1000000.0 + is->video_current_pts_drift - is->video_current_pts;
  942. if(is->read_pause_return != AVERROR(ENOSYS)){
  943. is->video_current_pts = is->video_current_pts_drift + av_gettime() / 1000000.0;
  944. }
  945. is->video_current_pts_drift = is->video_current_pts - av_gettime() / 1000000.0;
  946. }
  947. is->paused = !is->paused;
  948. }
  949. static double compute_target_time(double frame_current_pts, VideoState *is)
  950. {
  951. double delay, sync_threshold, diff;
  952. /* compute nominal delay */
  953. delay = frame_current_pts - is->frame_last_pts;
  954. if (delay <= 0 || delay >= 10.0) {
  955. /* if incorrect delay, use previous one */
  956. delay = is->frame_last_delay;
  957. } else {
  958. is->frame_last_delay = delay;
  959. }
  960. is->frame_last_pts = frame_current_pts;
  961. /* update delay to follow master synchronisation source */
  962. if (((is->av_sync_type == AV_SYNC_AUDIO_MASTER && is->audio_st) ||
  963. is->av_sync_type == AV_SYNC_EXTERNAL_CLOCK)) {
  964. /* if video is slave, we try to correct big delays by
  965. duplicating or deleting a frame */
  966. diff = get_video_clock(is) - get_master_clock(is);
  967. /* skip or repeat frame. We take into account the
  968. delay to compute the threshold. I still don't know
  969. if it is the best guess */
  970. sync_threshold = FFMAX(AV_SYNC_THRESHOLD, delay);
  971. if (fabs(diff) < AV_NOSYNC_THRESHOLD) {
  972. if (diff <= -sync_threshold)
  973. delay = 0;
  974. else if (diff >= sync_threshold)
  975. delay = 2 * delay;
  976. }
  977. }
  978. is->frame_timer += delay;
  979. av_dlog(NULL, "video: delay=%0.3f pts=%0.3f A-V=%f\n",
  980. delay, frame_current_pts, -diff);
  981. return is->frame_timer;
  982. }
  983. /* called to display each frame */
  984. static void video_refresh(void *opaque)
  985. {
  986. VideoState *is = opaque;
  987. VideoPicture *vp;
  988. SubPicture *sp, *sp2;
  989. if (is->video_st) {
  990. retry:
  991. if (is->pictq_size == 0) {
  992. //nothing to do, no picture to display in the que
  993. } else {
  994. double time= av_gettime()/1000000.0;
  995. double next_target;
  996. /* dequeue the picture */
  997. vp = &is->pictq[is->pictq_rindex];
  998. if(time < vp->target_clock)
  999. return;
  1000. /* update current video pts */
  1001. is->video_current_pts = vp->pts;
  1002. is->video_current_pts_drift = is->video_current_pts - time;
  1003. is->video_current_pos = vp->pos;
  1004. if(is->pictq_size > 1){
  1005. VideoPicture *nextvp= &is->pictq[(is->pictq_rindex+1)%VIDEO_PICTURE_QUEUE_SIZE];
  1006. assert(nextvp->target_clock >= vp->target_clock);
  1007. next_target= nextvp->target_clock;
  1008. }else{
  1009. next_target= vp->target_clock + is->video_clock - vp->pts; //FIXME pass durations cleanly
  1010. }
  1011. if((framedrop>0 || (framedrop && is->audio_st)) && time > next_target){
  1012. is->skip_frames *= 1.0 + FRAME_SKIP_FACTOR;
  1013. if(is->pictq_size > 1 || time > next_target + 0.5){
  1014. /* update queue size and signal for next picture */
  1015. if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE)
  1016. is->pictq_rindex = 0;
  1017. SDL_LockMutex(is->pictq_mutex);
  1018. is->pictq_size--;
  1019. SDL_CondSignal(is->pictq_cond);
  1020. SDL_UnlockMutex(is->pictq_mutex);
  1021. goto retry;
  1022. }
  1023. }
  1024. if(is->subtitle_st) {
  1025. if (is->subtitle_stream_changed) {
  1026. SDL_LockMutex(is->subpq_mutex);
  1027. while (is->subpq_size) {
  1028. free_subpicture(&is->subpq[is->subpq_rindex]);
  1029. /* update queue size and signal for next picture */
  1030. if (++is->subpq_rindex == SUBPICTURE_QUEUE_SIZE)
  1031. is->subpq_rindex = 0;
  1032. is->subpq_size--;
  1033. }
  1034. is->subtitle_stream_changed = 0;
  1035. SDL_CondSignal(is->subpq_cond);
  1036. SDL_UnlockMutex(is->subpq_mutex);
  1037. } else {
  1038. if (is->subpq_size > 0) {
  1039. sp = &is->subpq[is->subpq_rindex];
  1040. if (is->subpq_size > 1)
  1041. sp2 = &is->subpq[(is->subpq_rindex + 1) % SUBPICTURE_QUEUE_SIZE];
  1042. else
  1043. sp2 = NULL;
  1044. if ((is->video_current_pts > (sp->pts + ((float) sp->sub.end_display_time / 1000)))
  1045. || (sp2 && is->video_current_pts > (sp2->pts + ((float) sp2->sub.start_display_time / 1000))))
  1046. {
  1047. free_subpicture(sp);
  1048. /* update queue size and signal for next picture */
  1049. if (++is->subpq_rindex == SUBPICTURE_QUEUE_SIZE)
  1050. is->subpq_rindex = 0;
  1051. SDL_LockMutex(is->subpq_mutex);
  1052. is->subpq_size--;
  1053. SDL_CondSignal(is->subpq_cond);
  1054. SDL_UnlockMutex(is->subpq_mutex);
  1055. }
  1056. }
  1057. }
  1058. }
  1059. /* display picture */
  1060. if (!display_disable)
  1061. video_display(is);
  1062. /* update queue size and signal for next picture */
  1063. if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE)
  1064. is->pictq_rindex = 0;
  1065. SDL_LockMutex(is->pictq_mutex);
  1066. is->pictq_size--;
  1067. SDL_CondSignal(is->pictq_cond);
  1068. SDL_UnlockMutex(is->pictq_mutex);
  1069. }
  1070. } else if (is->audio_st) {
  1071. /* draw the next audio frame */
  1072. /* if only audio stream, then display the audio bars (better
  1073. than nothing, just to test the implementation */
  1074. /* display picture */
  1075. if (!display_disable)
  1076. video_display(is);
  1077. }
  1078. if (show_status) {
  1079. static int64_t last_time;
  1080. int64_t cur_time;
  1081. int aqsize, vqsize, sqsize;
  1082. double av_diff;
  1083. cur_time = av_gettime();
  1084. if (!last_time || (cur_time - last_time) >= 30000) {
  1085. aqsize = 0;
  1086. vqsize = 0;
  1087. sqsize = 0;
  1088. if (is->audio_st)
  1089. aqsize = is->audioq.size;
  1090. if (is->video_st)
  1091. vqsize = is->videoq.size;
  1092. if (is->subtitle_st)
  1093. sqsize = is->subtitleq.size;
  1094. av_diff = 0;
  1095. if (is->audio_st && is->video_st)
  1096. av_diff = get_audio_clock(is) - get_video_clock(is);
  1097. printf("%7.2f A-V:%7.3f s:%3.1f aq=%5dKB vq=%5dKB sq=%5dB f=%"PRId64"/%"PRId64" \r",
  1098. get_master_clock(is),
  1099. av_diff,
  1100. FFMAX(is->skip_frames-1, 0),
  1101. aqsize / 1024,
  1102. vqsize / 1024,
  1103. sqsize,
  1104. is->video_st ? is->video_st->codec->pts_correction_num_faulty_dts : 0,
  1105. is->video_st ? is->video_st->codec->pts_correction_num_faulty_pts : 0);
  1106. fflush(stdout);
  1107. last_time = cur_time;
  1108. }
  1109. }
  1110. }
  1111. /* allocate a picture (needs to do that in main thread to avoid
  1112. potential locking problems */
  1113. static void alloc_picture(void *opaque)
  1114. {
  1115. VideoState *is = opaque;
  1116. VideoPicture *vp;
  1117. vp = &is->pictq[is->pictq_windex];
  1118. if (vp->bmp)
  1119. SDL_FreeYUVOverlay(vp->bmp);
  1120. #if CONFIG_AVFILTER
  1121. if (vp->picref)
  1122. avfilter_unref_buffer(vp->picref);
  1123. vp->picref = NULL;
  1124. vp->width = is->out_video_filter->inputs[0]->w;
  1125. vp->height = is->out_video_filter->inputs[0]->h;
  1126. vp->pix_fmt = is->out_video_filter->inputs[0]->format;
  1127. #else
  1128. vp->width = is->video_st->codec->width;
  1129. vp->height = is->video_st->codec->height;
  1130. vp->pix_fmt = is->video_st->codec->pix_fmt;
  1131. #endif
  1132. vp->bmp = SDL_CreateYUVOverlay(vp->width, vp->height,
  1133. SDL_YV12_OVERLAY,
  1134. screen);
  1135. if (!vp->bmp || vp->bmp->pitches[0] < vp->width) {
  1136. /* SDL allocates a buffer smaller than requested if the video
  1137. * overlay hardware is unable to support the requested size. */
  1138. fprintf(stderr, "Error: the video system does not support an image\n"
  1139. "size of %dx%d pixels. Try using -lowres or -vf \"scale=w:h\"\n"
  1140. "to reduce the image size.\n", vp->width, vp->height );
  1141. do_exit();
  1142. }
  1143. SDL_LockMutex(is->pictq_mutex);
  1144. vp->allocated = 1;
  1145. SDL_CondSignal(is->pictq_cond);
  1146. SDL_UnlockMutex(is->pictq_mutex);
  1147. }
  1148. static int queue_picture(VideoState *is, AVFrame *src_frame, double pts1, int64_t pos)
  1149. {
  1150. VideoPicture *vp;
  1151. double frame_delay, pts = pts1;
  1152. /* compute the exact PTS for the picture if it is omitted in the stream
  1153. * pts1 is the dts of the pkt / pts of the frame */
  1154. if (pts != 0) {
  1155. /* update video clock with pts, if present */
  1156. is->video_clock = pts;
  1157. } else {
  1158. pts = is->video_clock;
  1159. }
  1160. /* update video clock for next frame */
  1161. frame_delay = av_q2d(is->video_st->codec->time_base);
  1162. /* for MPEG2, the frame can be repeated, so we update the
  1163. clock accordingly */
  1164. frame_delay += src_frame->repeat_pict * (frame_delay * 0.5);
  1165. is->video_clock += frame_delay;
  1166. #if defined(DEBUG_SYNC) && 0
  1167. printf("frame_type=%c clock=%0.3f pts=%0.3f\n",
  1168. av_get_picture_type_char(src_frame->pict_type), pts, pts1);
  1169. #endif
  1170. /* wait until we have space to put a new picture */
  1171. SDL_LockMutex(is->pictq_mutex);
  1172. if(is->pictq_size>=VIDEO_PICTURE_QUEUE_SIZE && !is->refresh)
  1173. is->skip_frames= FFMAX(1.0 - FRAME_SKIP_FACTOR, is->skip_frames * (1.0-FRAME_SKIP_FACTOR));
  1174. while (is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE &&
  1175. !is->videoq.abort_request) {
  1176. SDL_CondWait(is->pictq_cond, is->pictq_mutex);
  1177. }
  1178. SDL_UnlockMutex(is->pictq_mutex);
  1179. if (is->videoq.abort_request)
  1180. return -1;
  1181. vp = &is->pictq[is->pictq_windex];
  1182. /* alloc or resize hardware picture buffer */
  1183. if (!vp->bmp ||
  1184. #if CONFIG_AVFILTER
  1185. vp->width != is->out_video_filter->inputs[0]->w ||
  1186. vp->height != is->out_video_filter->inputs[0]->h) {
  1187. #else
  1188. vp->width != is->video_st->codec->width ||
  1189. vp->height != is->video_st->codec->height) {
  1190. #endif
  1191. SDL_Event event;
  1192. vp->allocated = 0;
  1193. /* the allocation must be done in the main thread to avoid
  1194. locking problems */
  1195. event.type = FF_ALLOC_EVENT;
  1196. event.user.data1 = is;
  1197. SDL_PushEvent(&event);
  1198. /* wait until the picture is allocated */
  1199. SDL_LockMutex(is->pictq_mutex);
  1200. while (!vp->allocated && !is->videoq.abort_request) {
  1201. SDL_CondWait(is->pictq_cond, is->pictq_mutex);
  1202. }
  1203. SDL_UnlockMutex(is->pictq_mutex);
  1204. if (is->videoq.abort_request)
  1205. return -1;
  1206. }
  1207. /* if the frame is not skipped, then display it */
  1208. if (vp->bmp) {
  1209. AVPicture pict;
  1210. #if CONFIG_AVFILTER
  1211. if(vp->picref)
  1212. avfilter_unref_buffer(vp->picref);
  1213. vp->picref = src_frame->opaque;
  1214. #endif
  1215. /* get a pointer on the bitmap */
  1216. SDL_LockYUVOverlay (vp->bmp);
  1217. memset(&pict,0,sizeof(AVPicture));
  1218. pict.data[0] = vp->bmp->pixels[0];
  1219. pict.data[1] = vp->bmp->pixels[2];
  1220. pict.data[2] = vp->bmp->pixels[1];
  1221. pict.linesize[0] = vp->bmp->pitches[0];
  1222. pict.linesize[1] = vp->bmp->pitches[2];
  1223. pict.linesize[2] = vp->bmp->pitches[1];
  1224. #if CONFIG_AVFILTER
  1225. //FIXME use direct rendering
  1226. av_picture_copy(&pict, (AVPicture *)src_frame,
  1227. vp->pix_fmt, vp->width, vp->height);
  1228. #else
  1229. sws_flags = av_get_int(sws_opts, "sws_flags", NULL);
  1230. is->img_convert_ctx = sws_getCachedContext(is->img_convert_ctx,
  1231. vp->width, vp->height, vp->pix_fmt, vp->width, vp->height,
  1232. PIX_FMT_YUV420P, sws_flags, NULL, NULL, NULL);
  1233. if (is->img_convert_ctx == NULL) {
  1234. fprintf(stderr, "Cannot initialize the conversion context\n");
  1235. exit(1);
  1236. }
  1237. sws_scale(is->img_convert_ctx, src_frame->data, src_frame->linesize,
  1238. 0, vp->height, pict.data, pict.linesize);
  1239. #endif
  1240. /* update the bitmap content */
  1241. SDL_UnlockYUVOverlay(vp->bmp);
  1242. vp->pts = pts;
  1243. vp->pos = pos;
  1244. /* now we can update the picture count */
  1245. if (++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE)
  1246. is->pictq_windex = 0;
  1247. SDL_LockMutex(is->pictq_mutex);
  1248. vp->target_clock= compute_target_time(vp->pts, is);
  1249. is->pictq_size++;
  1250. SDL_UnlockMutex(is->pictq_mutex);
  1251. }
  1252. return 0;
  1253. }
  1254. static int get_video_frame(VideoState *is, AVFrame *frame, int64_t *pts, AVPacket *pkt)
  1255. {
  1256. int len1 av_unused, got_picture, i;
  1257. if (packet_queue_get(&is->videoq, pkt, 1) < 0)
  1258. return -1;
  1259. if (pkt->data == flush_pkt.data) {
  1260. avcodec_flush_buffers(is->video_st->codec);
  1261. SDL_LockMutex(is->pictq_mutex);
  1262. //Make sure there are no long delay timers (ideally we should just flush the que but thats harder)
  1263. for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++) {
  1264. is->pictq[i].target_clock= 0;
  1265. }
  1266. while (is->pictq_size && !is->videoq.abort_request) {
  1267. SDL_CondWait(is->pictq_cond, is->pictq_mutex);
  1268. }
  1269. is->video_current_pos = -1;
  1270. SDL_UnlockMutex(is->pictq_mutex);
  1271. is->frame_last_pts = AV_NOPTS_VALUE;
  1272. is->frame_last_delay = 0;
  1273. is->frame_timer = (double)av_gettime() / 1000000.0;
  1274. is->skip_frames = 1;
  1275. is->skip_frames_index = 0;
  1276. return 0;
  1277. }
  1278. len1 = avcodec_decode_video2(is->video_st->codec,
  1279. frame, &got_picture,
  1280. pkt);
  1281. if (got_picture) {
  1282. if (decoder_reorder_pts == -1) {
  1283. *pts = frame->best_effort_timestamp;
  1284. } else if (decoder_reorder_pts) {
  1285. *pts = frame->pkt_pts;
  1286. } else {
  1287. *pts = frame->pkt_dts;
  1288. }
  1289. if (*pts == AV_NOPTS_VALUE) {
  1290. *pts = 0;
  1291. }
  1292. is->skip_frames_index += 1;
  1293. if(is->skip_frames_index >= is->skip_frames){
  1294. is->skip_frames_index -= FFMAX(is->skip_frames, 1.0);
  1295. return 1;
  1296. }
  1297. }
  1298. return 0;
  1299. }
  1300. #if CONFIG_AVFILTER
  1301. typedef struct {
  1302. VideoState *is;
  1303. AVFrame *frame;
  1304. int use_dr1;
  1305. } FilterPriv;
  1306. static int input_get_buffer(AVCodecContext *codec, AVFrame *pic)
  1307. {
  1308. AVFilterContext *ctx = codec->opaque;
  1309. AVFilterBufferRef *ref;
  1310. int perms = AV_PERM_WRITE;
  1311. int i, w, h, stride[4];
  1312. unsigned edge;
  1313. int pixel_size;
  1314. av_assert0(codec->flags & CODEC_FLAG_EMU_EDGE);
  1315. if (codec->codec->capabilities & CODEC_CAP_NEG_LINESIZES)
  1316. perms |= AV_PERM_NEG_LINESIZES;
  1317. if(pic->buffer_hints & FF_BUFFER_HINTS_VALID) {
  1318. if(pic->buffer_hints & FF_BUFFER_HINTS_READABLE) perms |= AV_PERM_READ;
  1319. if(pic->buffer_hints & FF_BUFFER_HINTS_PRESERVE) perms |= AV_PERM_PRESERVE;
  1320. if(pic->buffer_hints & FF_BUFFER_HINTS_REUSABLE) perms |= AV_PERM_REUSE2;
  1321. }
  1322. if(pic->reference) perms |= AV_PERM_READ | AV_PERM_PRESERVE;
  1323. w = codec->width;
  1324. h = codec->height;
  1325. if(av_image_check_size(w, h, 0, codec))
  1326. return -1;
  1327. avcodec_align_dimensions2(codec, &w, &h, stride);
  1328. edge = codec->flags & CODEC_FLAG_EMU_EDGE ? 0 : avcodec_get_edge_width();
  1329. w += edge << 1;
  1330. h += edge << 1;
  1331. if(!(ref = avfilter_get_video_buffer(ctx->outputs[0], perms, w, h)))
  1332. return -1;
  1333. pixel_size = av_pix_fmt_descriptors[ref->format].comp[0].step_minus1+1;
  1334. ref->video->w = codec->width;
  1335. ref->video->h = codec->height;
  1336. for(i = 0; i < 4; i ++) {
  1337. unsigned hshift = (i == 1 || i == 2) ? av_pix_fmt_descriptors[ref->format].log2_chroma_w : 0;
  1338. unsigned vshift = (i == 1 || i == 2) ? av_pix_fmt_descriptors[ref->format].log2_chroma_h : 0;
  1339. if (ref->data[i]) {
  1340. ref->data[i] += ((edge * pixel_size) >> hshift) + ((edge * ref->linesize[i]) >> vshift);
  1341. }
  1342. pic->data[i] = ref->data[i];
  1343. pic->linesize[i] = ref->linesize[i];
  1344. }
  1345. pic->opaque = ref;
  1346. pic->age = INT_MAX;
  1347. pic->type = FF_BUFFER_TYPE_USER;
  1348. pic->reordered_opaque = codec->reordered_opaque;
  1349. if(codec->pkt) pic->pkt_pts = codec->pkt->pts;
  1350. else pic->pkt_pts = AV_NOPTS_VALUE;
  1351. return 0;
  1352. }
  1353. static void input_release_buffer(AVCodecContext *codec, AVFrame *pic)
  1354. {
  1355. memset(pic->data, 0, sizeof(pic->data));
  1356. avfilter_unref_buffer(pic->opaque);
  1357. }
  1358. static int input_reget_buffer(AVCodecContext *codec, AVFrame *pic)
  1359. {
  1360. AVFilterBufferRef *ref = pic->opaque;
  1361. if (pic->data[0] == NULL) {
  1362. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  1363. return codec->get_buffer(codec, pic);
  1364. }
  1365. if ((codec->width != ref->video->w) || (codec->height != ref->video->h) ||
  1366. (codec->pix_fmt != ref->format)) {
  1367. av_log(codec, AV_LOG_ERROR, "Picture properties changed.\n");
  1368. return -1;
  1369. }
  1370. pic->reordered_opaque = codec->reordered_opaque;
  1371. if(codec->pkt) pic->pkt_pts = codec->pkt->pts;
  1372. else pic->pkt_pts = AV_NOPTS_VALUE;
  1373. return 0;
  1374. }
  1375. static int input_init(AVFilterContext *ctx, const char *args, void *opaque)
  1376. {
  1377. FilterPriv *priv = ctx->priv;
  1378. AVCodecContext *codec;
  1379. if(!opaque) return -1;
  1380. priv->is = opaque;
  1381. codec = priv->is->video_st->codec;
  1382. codec->opaque = ctx;
  1383. if((codec->codec->capabilities & CODEC_CAP_DR1)
  1384. ) {
  1385. av_assert0(codec->flags & CODEC_FLAG_EMU_EDGE);
  1386. priv->use_dr1 = 1;
  1387. codec->get_buffer = input_get_buffer;
  1388. codec->release_buffer = input_release_buffer;
  1389. codec->reget_buffer = input_reget_buffer;
  1390. codec->thread_safe_callbacks = 1;
  1391. }
  1392. priv->frame = avcodec_alloc_frame();
  1393. return 0;
  1394. }
  1395. static void input_uninit(AVFilterContext *ctx)
  1396. {
  1397. FilterPriv *priv = ctx->priv;
  1398. av_free(priv->frame);
  1399. }
  1400. static int input_request_frame(AVFilterLink *link)
  1401. {
  1402. FilterPriv *priv = link->src->priv;
  1403. AVFilterBufferRef *picref;
  1404. int64_t pts = 0;
  1405. AVPacket pkt;
  1406. int ret;
  1407. while (!(ret = get_video_frame(priv->is, priv->frame, &pts, &pkt)))
  1408. av_free_packet(&pkt);
  1409. if (ret < 0)
  1410. return -1;
  1411. if(priv->use_dr1 && priv->frame->opaque) {
  1412. picref = avfilter_ref_buffer(priv->frame->opaque, ~0);
  1413. } else {
  1414. picref = avfilter_get_video_buffer(link, AV_PERM_WRITE, link->w, link->h);
  1415. av_image_copy(picref->data, picref->linesize,
  1416. priv->frame->data, priv->frame->linesize,
  1417. picref->format, link->w, link->h);
  1418. }
  1419. av_free_packet(&pkt);
  1420. avfilter_copy_frame_props(picref, priv->frame);
  1421. picref->pts = pts;
  1422. avfilter_start_frame(link, picref);
  1423. avfilter_draw_slice(link, 0, link->h, 1);
  1424. avfilter_end_frame(link);
  1425. return 0;
  1426. }
  1427. static int input_query_formats(AVFilterContext *ctx)
  1428. {
  1429. FilterPriv *priv = ctx->priv;
  1430. enum PixelFormat pix_fmts[] = {
  1431. priv->is->video_st->codec->pix_fmt, PIX_FMT_NONE
  1432. };
  1433. avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
  1434. return 0;
  1435. }
  1436. static int input_config_props(AVFilterLink *link)
  1437. {
  1438. FilterPriv *priv = link->src->priv;
  1439. AVCodecContext *c = priv->is->video_st->codec;
  1440. link->w = c->width;
  1441. link->h = c->height;
  1442. link->time_base = priv->is->video_st->time_base;
  1443. return 0;
  1444. }
  1445. static AVFilter input_filter =
  1446. {
  1447. .name = "ffplay_input",
  1448. .priv_size = sizeof(FilterPriv),
  1449. .init = input_init,
  1450. .uninit = input_uninit,
  1451. .query_formats = input_query_formats,
  1452. .inputs = (AVFilterPad[]) {{ .name = NULL }},
  1453. .outputs = (AVFilterPad[]) {{ .name = "default",
  1454. .type = AVMEDIA_TYPE_VIDEO,
  1455. .request_frame = input_request_frame,
  1456. .config_props = input_config_props, },
  1457. { .name = NULL }},
  1458. };
  1459. static int configure_video_filters(AVFilterGraph *graph, VideoState *is, const char *vfilters)
  1460. {
  1461. char sws_flags_str[128];
  1462. int ret;
  1463. enum PixelFormat pix_fmts[] = { PIX_FMT_YUV420P, PIX_FMT_NONE };
  1464. AVFilterContext *filt_src = NULL, *filt_out = NULL;
  1465. snprintf(sws_flags_str, sizeof(sws_flags_str), "flags=%d", sws_flags);
  1466. graph->scale_sws_opts = av_strdup(sws_flags_str);
  1467. if ((ret = avfilter_graph_create_filter(&filt_src, &input_filter, "src",
  1468. NULL, is, graph)) < 0)
  1469. goto the_end;
  1470. if ((ret = avfilter_graph_create_filter(&filt_out, avfilter_get_by_name("buffersink"), "out",
  1471. NULL, pix_fmts, graph)) < 0)
  1472. goto the_end;
  1473. if(vfilters) {
  1474. AVFilterInOut *outputs = avfilter_inout_alloc();
  1475. AVFilterInOut *inputs = avfilter_inout_alloc();
  1476. outputs->name = av_strdup("in");
  1477. outputs->filter_ctx = filt_src;
  1478. outputs->pad_idx = 0;
  1479. outputs->next = NULL;
  1480. inputs->name = av_strdup("out");
  1481. inputs->filter_ctx = filt_out;
  1482. inputs->pad_idx = 0;
  1483. inputs->next = NULL;
  1484. if ((ret = avfilter_graph_parse(graph, vfilters, &inputs, &outputs, NULL)) < 0)
  1485. goto the_end;
  1486. av_freep(&vfilters);
  1487. } else {
  1488. if ((ret = avfilter_link(filt_src, 0, filt_out, 0)) < 0)
  1489. goto the_end;
  1490. }
  1491. if ((ret = avfilter_graph_config(graph, NULL)) < 0)
  1492. goto the_end;
  1493. is->out_video_filter = filt_out;
  1494. the_end:
  1495. return ret;
  1496. }
  1497. #endif /* CONFIG_AVFILTER */
  1498. static int video_thread(void *arg)
  1499. {
  1500. VideoState *is = arg;
  1501. AVFrame *frame= avcodec_alloc_frame();
  1502. int64_t pts_int = AV_NOPTS_VALUE, pos = -1;
  1503. double pts;
  1504. int ret;
  1505. #if CONFIG_AVFILTER
  1506. AVFilterGraph *graph = avfilter_graph_alloc();
  1507. AVFilterContext *filt_out = NULL;
  1508. if ((ret = configure_video_filters(graph, is, vfilters)) < 0)
  1509. goto the_end;
  1510. filt_out = is->out_video_filter;
  1511. #endif
  1512. for(;;) {
  1513. #if !CONFIG_AVFILTER
  1514. AVPacket pkt;
  1515. #else
  1516. AVFilterBufferRef *picref;
  1517. AVRational tb = filt_out->inputs[0]->time_base;
  1518. #endif
  1519. while (is->paused && !is->videoq.abort_request)
  1520. SDL_Delay(10);
  1521. #if CONFIG_AVFILTER
  1522. ret = av_vsink_buffer_get_video_buffer_ref(filt_out, &picref, 0);
  1523. if (picref) {
  1524. avfilter_fill_frame_from_video_buffer_ref(frame, picref);
  1525. pts_int = picref->pts;
  1526. pos = picref->pos;
  1527. frame->opaque = picref;
  1528. }
  1529. if (av_cmp_q(tb, is->video_st->time_base)) {
  1530. av_unused int64_t pts1 = pts_int;
  1531. pts_int = av_rescale_q(pts_int, tb, is->video_st->time_base);
  1532. av_dlog(NULL, "video_thread(): "
  1533. "tb:%d/%d pts:%"PRId64" -> tb:%d/%d pts:%"PRId64"\n",
  1534. tb.num, tb.den, pts1,
  1535. is->video_st->time_base.num, is->video_st->time_base.den, pts_int);
  1536. }
  1537. #else
  1538. ret = get_video_frame(is, frame, &pts_int, &pkt);
  1539. pos = pkt.pos;
  1540. av_free_packet(&pkt);
  1541. #endif
  1542. if (ret < 0) goto the_end;
  1543. if (!picref)
  1544. continue;
  1545. pts = pts_int*av_q2d(is->video_st->time_base);
  1546. ret = queue_picture(is, frame, pts, pos);
  1547. if (ret < 0)
  1548. goto the_end;
  1549. if (step)
  1550. if (cur_stream)
  1551. stream_toggle_pause(cur_stream);
  1552. }
  1553. the_end:
  1554. #if CONFIG_AVFILTER
  1555. avfilter_graph_free(&graph);
  1556. #endif
  1557. av_free(frame);
  1558. return 0;
  1559. }
  1560. static int subtitle_thread(void *arg)
  1561. {
  1562. VideoState *is = arg;
  1563. SubPicture *sp;
  1564. AVPacket pkt1, *pkt = &pkt1;
  1565. int len1 av_unused, got_subtitle;
  1566. double pts;
  1567. int i, j;
  1568. int r, g, b, y, u, v, a;
  1569. for(;;) {
  1570. while (is->paused && !is->subtitleq.abort_request) {
  1571. SDL_Delay(10);
  1572. }
  1573. if (packet_queue_get(&is->subtitleq, pkt, 1) < 0)
  1574. break;
  1575. if(pkt->data == flush_pkt.data){
  1576. avcodec_flush_buffers(is->subtitle_st->codec);
  1577. continue;
  1578. }
  1579. SDL_LockMutex(is->subpq_mutex);
  1580. while (is->subpq_size >= SUBPICTURE_QUEUE_SIZE &&
  1581. !is->subtitleq.abort_request) {
  1582. SDL_CondWait(is->subpq_cond, is->subpq_mutex);
  1583. }
  1584. SDL_UnlockMutex(is->subpq_mutex);
  1585. if (is->subtitleq.abort_request)
  1586. goto the_end;
  1587. sp = &is->subpq[is->subpq_windex];
  1588. /* NOTE: ipts is the PTS of the _first_ picture beginning in
  1589. this packet, if any */
  1590. pts = 0;
  1591. if (pkt->pts != AV_NOPTS_VALUE)
  1592. pts = av_q2d(is->subtitle_st->time_base)*pkt->pts;
  1593. len1 = avcodec_decode_subtitle2(is->subtitle_st->codec,
  1594. &sp->sub, &got_subtitle,
  1595. pkt);
  1596. if (got_subtitle && sp->sub.format == 0) {
  1597. sp->pts = pts;
  1598. for (i = 0; i < sp->sub.num_rects; i++)
  1599. {
  1600. for (j = 0; j < sp->sub.rects[i]->nb_colors; j++)
  1601. {
  1602. RGBA_IN(r, g, b, a, (uint32_t*)sp->sub.rects[i]->pict.data[1] + j);
  1603. y = RGB_TO_Y_CCIR(r, g, b);
  1604. u = RGB_TO_U_CCIR(r, g, b, 0);
  1605. v = RGB_TO_V_CCIR(r, g, b, 0);
  1606. YUVA_OUT((uint32_t*)sp->sub.rects[i]->pict.data[1] + j, y, u, v, a);
  1607. }
  1608. }
  1609. /* now we can update the picture count */
  1610. if (++is->subpq_windex == SUBPICTURE_QUEUE_SIZE)
  1611. is->subpq_windex = 0;
  1612. SDL_LockMutex(is->subpq_mutex);
  1613. is->subpq_size++;
  1614. SDL_UnlockMutex(is->subpq_mutex);
  1615. }
  1616. av_free_packet(pkt);
  1617. }
  1618. the_end:
  1619. return 0;
  1620. }
  1621. /* copy samples for viewing in editor window */
  1622. static void update_sample_display(VideoState *is, short *samples, int samples_size)
  1623. {
  1624. int size, len;
  1625. size = samples_size / sizeof(short);
  1626. while (size > 0) {
  1627. len = SAMPLE_ARRAY_SIZE - is->sample_array_index;
  1628. if (len > size)
  1629. len = size;
  1630. memcpy(is->sample_array + is->sample_array_index, samples, len * sizeof(short));
  1631. samples += len;
  1632. is->sample_array_index += len;
  1633. if (is->sample_array_index >= SAMPLE_ARRAY_SIZE)
  1634. is->sample_array_index = 0;
  1635. size -= len;
  1636. }
  1637. }
  1638. /* return the new audio buffer size (samples can be added or deleted
  1639. to get better sync if video or external master clock) */
  1640. static int synchronize_audio(VideoState *is, short *samples,
  1641. int samples_size1, double pts)
  1642. {
  1643. int n, samples_size;
  1644. double ref_clock;
  1645. n = 2 * is->audio_st->codec->channels;
  1646. samples_size = samples_size1;
  1647. /* if not master, then we try to remove or add samples to correct the clock */
  1648. if (((is->av_sync_type == AV_SYNC_VIDEO_MASTER && is->video_st) ||
  1649. is->av_sync_type == AV_SYNC_EXTERNAL_CLOCK)) {
  1650. double diff, avg_diff;
  1651. int wanted_size, min_size, max_size, nb_samples;
  1652. ref_clock = get_master_clock(is);
  1653. diff = get_audio_clock(is) - ref_clock;
  1654. if (diff < AV_NOSYNC_THRESHOLD) {
  1655. is->audio_diff_cum = diff + is->audio_diff_avg_coef * is->audio_diff_cum;
  1656. if (is->audio_diff_avg_count < AUDIO_DIFF_AVG_NB) {
  1657. /* not enough measures to have a correct estimate */
  1658. is->audio_diff_avg_count++;
  1659. } else {
  1660. /* estimate the A-V difference */
  1661. avg_diff = is->audio_diff_cum * (1.0 - is->audio_diff_avg_coef);
  1662. if (fabs(avg_diff) >= is->audio_diff_threshold) {
  1663. wanted_size = samples_size + ((int)(diff * is->audio_st->codec->sample_rate) * n);
  1664. nb_samples = samples_size / n;
  1665. min_size = ((nb_samples * (100 - SAMPLE_CORRECTION_PERCENT_MAX)) / 100) * n;
  1666. max_size = ((nb_samples * (100 + SAMPLE_CORRECTION_PERCENT_MAX)) / 100) * n;
  1667. if (wanted_size < min_size)
  1668. wanted_size = min_size;
  1669. else if (wanted_size > max_size)
  1670. wanted_size = max_size;
  1671. /* add or remove samples to correction the synchro */
  1672. if (wanted_size < samples_size) {
  1673. /* remove samples */
  1674. samples_size = wanted_size;
  1675. } else if (wanted_size > samples_size) {
  1676. uint8_t *samples_end, *q;
  1677. int nb;
  1678. /* add samples */
  1679. nb = (samples_size - wanted_size);
  1680. samples_end = (uint8_t *)samples + samples_size - n;
  1681. q = samples_end + n;
  1682. while (nb > 0) {
  1683. memcpy(q, samples_end, n);
  1684. q += n;
  1685. nb -= n;
  1686. }
  1687. samples_size = wanted_size;
  1688. }
  1689. }
  1690. #if 0
  1691. printf("diff=%f adiff=%f sample_diff=%d apts=%0.3f vpts=%0.3f %f\n",
  1692. diff, avg_diff, samples_size - samples_size1,
  1693. is->audio_clock, is->video_clock, is->audio_diff_threshold);
  1694. #endif
  1695. }
  1696. } else {
  1697. /* too big difference : may be initial PTS errors, so
  1698. reset A-V filter */
  1699. is->audio_diff_avg_count = 0;
  1700. is->audio_diff_cum = 0;
  1701. }
  1702. }
  1703. return samples_size;
  1704. }
  1705. /* decode one audio frame and returns its uncompressed size */
  1706. static int audio_decode_frame(VideoState *is, double *pts_ptr)
  1707. {
  1708. AVPacket *pkt_temp = &is->audio_pkt_temp;
  1709. AVPacket *pkt = &is->audio_pkt;
  1710. AVCodecContext *dec= is->audio_st->codec;
  1711. int n, len1, data_size;
  1712. double pts;
  1713. for(;;) {
  1714. /* NOTE: the audio packet can contain several frames */
  1715. while (pkt_temp->size > 0) {
  1716. data_size = sizeof(is->audio_buf1);
  1717. len1 = avcodec_decode_audio3(dec,
  1718. (int16_t *)is->audio_buf1, &data_size,
  1719. pkt_temp);
  1720. if (len1 < 0) {
  1721. /* if error, we skip the frame */
  1722. pkt_temp->size = 0;
  1723. break;
  1724. }
  1725. pkt_temp->data += len1;
  1726. pkt_temp->size -= len1;
  1727. if (data_size <= 0)
  1728. continue;
  1729. if (dec->sample_fmt != is->audio_src_fmt) {
  1730. if (is->reformat_ctx)
  1731. av_audio_convert_free(is->reformat_ctx);
  1732. is->reformat_ctx= av_audio_convert_alloc(AV_SAMPLE_FMT_S16, 1,
  1733. dec->sample_fmt, 1, NULL, 0);
  1734. if (!is->reformat_ctx) {
  1735. fprintf(stderr, "Cannot convert %s sample format to %s sample format\n",
  1736. av_get_sample_fmt_name(dec->sample_fmt),
  1737. av_get_sample_fmt_name(AV_SAMPLE_FMT_S16));
  1738. break;
  1739. }
  1740. is->audio_src_fmt= dec->sample_fmt;
  1741. }
  1742. if (is->reformat_ctx) {
  1743. const void *ibuf[6]= {is->audio_buf1};
  1744. void *obuf[6]= {is->audio_buf2};
  1745. int istride[6]= {av_get_bytes_per_sample(dec->sample_fmt)};
  1746. int ostride[6]= {2};
  1747. int len= data_size/istride[0];
  1748. if (av_audio_convert(is->reformat_ctx, obuf, ostride, ibuf, istride, len)<0) {
  1749. printf("av_audio_convert() failed\n");
  1750. break;
  1751. }
  1752. is->audio_buf= is->audio_buf2;
  1753. /* FIXME: existing code assume that data_size equals framesize*channels*2
  1754. remove this legacy cruft */
  1755. data_size= len*2;
  1756. }else{
  1757. is->audio_buf= is->audio_buf1;
  1758. }
  1759. /* if no pts, then compute it */
  1760. pts = is->audio_clock;
  1761. *pts_ptr = pts;
  1762. n = 2 * dec->channels;
  1763. is->audio_clock += (double)data_size /
  1764. (double)(n * dec->sample_rate);
  1765. #ifdef DEBUG
  1766. {
  1767. static double last_clock;
  1768. printf("audio: delay=%0.3f clock=%0.3f pts=%0.3f\n",
  1769. is->audio_clock - last_clock,
  1770. is->audio_clock, pts);
  1771. last_clock = is->audio_clock;
  1772. }
  1773. #endif
  1774. return data_size;
  1775. }
  1776. /* free the current packet */
  1777. if (pkt->data)
  1778. av_free_packet(pkt);
  1779. if (is->paused || is->audioq.abort_request) {
  1780. return -1;
  1781. }
  1782. /* read next packet */
  1783. if (packet_queue_get(&is->audioq, pkt, 1) < 0)
  1784. return -1;
  1785. if(pkt->data == flush_pkt.data){
  1786. avcodec_flush_buffers(dec);
  1787. continue;
  1788. }
  1789. pkt_temp->data = pkt->data;
  1790. pkt_temp->size = pkt->size;
  1791. /* if update the audio clock with the pts */
  1792. if (pkt->pts != AV_NOPTS_VALUE) {
  1793. is->audio_clock = av_q2d(is->audio_st->time_base)*pkt->pts;
  1794. }
  1795. }
  1796. }
  1797. /* prepare a new audio buffer */
  1798. static void sdl_audio_callback(void *opaque, Uint8 *stream, int len)
  1799. {
  1800. VideoState *is = opaque;
  1801. int audio_size, len1;
  1802. double pts;
  1803. audio_callback_time = av_gettime();
  1804. while (len > 0) {
  1805. if (is->audio_buf_index >= is->audio_buf_size) {
  1806. audio_size = audio_decode_frame(is, &pts);
  1807. if (audio_size < 0) {
  1808. /* if error, just output silence */
  1809. is->audio_buf = is->audio_buf1;
  1810. is->audio_buf_size = 1024;
  1811. memset(is->audio_buf, 0, is->audio_buf_size);
  1812. } else {
  1813. if (is->show_mode != SHOW_MODE_VIDEO)
  1814. update_sample_display(is, (int16_t *)is->audio_buf, audio_size);
  1815. audio_size = synchronize_audio(is, (int16_t *)is->audio_buf, audio_size,
  1816. pts);
  1817. is->audio_buf_size = audio_size;
  1818. }
  1819. is->audio_buf_index = 0;
  1820. }
  1821. len1 = is->audio_buf_size - is->audio_buf_index;
  1822. if (len1 > len)
  1823. len1 = len;
  1824. memcpy(stream, (uint8_t *)is->audio_buf + is->audio_buf_index, len1);
  1825. len -= len1;
  1826. stream += len1;
  1827. is->audio_buf_index += len1;
  1828. }
  1829. }
  1830. /* open a given stream. Return 0 if OK */
  1831. static int stream_component_open(VideoState *is, int stream_index)
  1832. {
  1833. AVFormatContext *ic = is->ic;
  1834. AVCodecContext *avctx;
  1835. AVCodec *codec;
  1836. SDL_AudioSpec wanted_spec, spec;
  1837. if (stream_index < 0 || stream_index >= ic->nb_streams)
  1838. return -1;
  1839. avctx = ic->streams[stream_index]->codec;
  1840. /* prepare audio output */
  1841. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
  1842. if (avctx->channels > 0) {
  1843. avctx->request_channels = FFMIN(2, avctx->channels);
  1844. } else {
  1845. avctx->request_channels = 2;
  1846. }
  1847. }
  1848. codec = avcodec_find_decoder(avctx->codec_id);
  1849. if (!codec)
  1850. return -1;
  1851. avctx->workaround_bugs = workaround_bugs;
  1852. avctx->lowres = lowres;
  1853. if(lowres) avctx->flags |= CODEC_FLAG_EMU_EDGE;
  1854. avctx->idct_algo= idct;
  1855. if(fast) avctx->flags2 |= CODEC_FLAG2_FAST;
  1856. avctx->skip_frame= skip_frame;
  1857. avctx->skip_idct= skip_idct;
  1858. avctx->skip_loop_filter= skip_loop_filter;
  1859. avctx->error_recognition= error_recognition;
  1860. avctx->error_concealment= error_concealment;
  1861. avctx->thread_count= thread_count;
  1862. set_context_opts(avctx, avcodec_opts[avctx->codec_type], 0, codec);
  1863. if(codec->capabilities & CODEC_CAP_DR1)
  1864. avctx->flags |= CODEC_FLAG_EMU_EDGE;
  1865. if (avcodec_open(avctx, codec) < 0)
  1866. return -1;
  1867. /* prepare audio output */
  1868. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
  1869. if(avctx->sample_rate <= 0 || avctx->channels <= 0){
  1870. fprintf(stderr, "Invalid sample rate or channel count\n");
  1871. return -1;
  1872. }
  1873. wanted_spec.freq = avctx->sample_rate;
  1874. wanted_spec.format = AUDIO_S16SYS;
  1875. wanted_spec.channels = avctx->channels;
  1876. wanted_spec.silence = 0;
  1877. wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE;
  1878. wanted_spec.callback = sdl_audio_callback;
  1879. wanted_spec.userdata = is;
  1880. if (SDL_OpenAudio(&wanted_spec, &spec) < 0) {
  1881. fprintf(stderr, "SDL_OpenAudio: %s\n", SDL_GetError());
  1882. return -1;
  1883. }
  1884. is->audio_hw_buf_size = spec.size;
  1885. is->audio_src_fmt= AV_SAMPLE_FMT_S16;
  1886. }
  1887. ic->streams[stream_index]->discard = AVDISCARD_DEFAULT;
  1888. switch(avctx->codec_type) {
  1889. case AVMEDIA_TYPE_AUDIO:
  1890. is->audio_stream = stream_index;
  1891. is->audio_st = ic->streams[stream_index];
  1892. is->audio_buf_size = 0;
  1893. is->audio_buf_index = 0;
  1894. /* init averaging filter */
  1895. is->audio_diff_avg_coef = exp(log(0.01) / AUDIO_DIFF_AVG_NB);
  1896. is->audio_diff_avg_count = 0;
  1897. /* since we do not have a precise anough audio fifo fullness,
  1898. we correct audio sync only if larger than this threshold */
  1899. is->audio_diff_threshold = 2.0 * SDL_AUDIO_BUFFER_SIZE / avctx->sample_rate;
  1900. memset(&is->audio_pkt, 0, sizeof(is->audio_pkt));
  1901. packet_queue_init(&is->audioq);
  1902. SDL_PauseAudio(0);
  1903. break;
  1904. case AVMEDIA_TYPE_VIDEO:
  1905. is->video_stream = stream_index;
  1906. is->video_st = ic->streams[stream_index];
  1907. packet_queue_init(&is->videoq);
  1908. is->video_tid = SDL_CreateThread(video_thread, is);
  1909. break;
  1910. case AVMEDIA_TYPE_SUBTITLE:
  1911. is->subtitle_stream = stream_index;
  1912. is->subtitle_st = ic->streams[stream_index];
  1913. packet_queue_init(&is->subtitleq);
  1914. is->subtitle_tid = SDL_CreateThread(subtitle_thread, is);
  1915. break;
  1916. default:
  1917. break;
  1918. }
  1919. return 0;
  1920. }
  1921. static void stream_component_close(VideoState *is, int stream_index)
  1922. {
  1923. AVFormatContext *ic = is->ic;
  1924. AVCodecContext *avctx;
  1925. if (stream_index < 0 || stream_index >= ic->nb_streams)
  1926. return;
  1927. avctx = ic->streams[stream_index]->codec;
  1928. switch(avctx->codec_type) {
  1929. case AVMEDIA_TYPE_AUDIO:
  1930. packet_queue_abort(&is->audioq);
  1931. SDL_CloseAudio();
  1932. packet_queue_end(&is->audioq);
  1933. if (is->reformat_ctx)
  1934. av_audio_convert_free(is->reformat_ctx);
  1935. is->reformat_ctx = NULL;
  1936. break;
  1937. case AVMEDIA_TYPE_VIDEO:
  1938. packet_queue_abort(&is->videoq);
  1939. /* note: we also signal this mutex to make sure we deblock the
  1940. video thread in all cases */
  1941. SDL_LockMutex(is->pictq_mutex);
  1942. SDL_CondSignal(is->pictq_cond);
  1943. SDL_UnlockMutex(is->pictq_mutex);
  1944. SDL_WaitThread(is->video_tid, NULL);
  1945. packet_queue_end(&is->videoq);
  1946. break;
  1947. case AVMEDIA_TYPE_SUBTITLE:
  1948. packet_queue_abort(&is->subtitleq);
  1949. /* note: we also signal this mutex to make sure we deblock the
  1950. video thread in all cases */
  1951. SDL_LockMutex(is->subpq_mutex);
  1952. is->subtitle_stream_changed = 1;
  1953. SDL_CondSignal(is->subpq_cond);
  1954. SDL_UnlockMutex(is->subpq_mutex);
  1955. SDL_WaitThread(is->subtitle_tid, NULL);
  1956. packet_queue_end(&is->subtitleq);
  1957. break;
  1958. default:
  1959. break;
  1960. }
  1961. ic->streams[stream_index]->discard = AVDISCARD_ALL;
  1962. avcodec_close(avctx);
  1963. switch(avctx->codec_type) {
  1964. case AVMEDIA_TYPE_AUDIO:
  1965. is->audio_st = NULL;
  1966. is->audio_stream = -1;
  1967. break;
  1968. case AVMEDIA_TYPE_VIDEO:
  1969. is->video_st = NULL;
  1970. is->video_stream = -1;
  1971. break;
  1972. case AVMEDIA_TYPE_SUBTITLE:
  1973. is->subtitle_st = NULL;
  1974. is->subtitle_stream = -1;
  1975. break;
  1976. default:
  1977. break;
  1978. }
  1979. }
  1980. /* since we have only one decoding thread, we can use a global
  1981. variable instead of a thread local variable */
  1982. static VideoState *global_video_state;
  1983. static int decode_interrupt_cb(void)
  1984. {
  1985. return (global_video_state && global_video_state->abort_request);
  1986. }
  1987. /* this thread gets the stream from the disk or the network */
  1988. static int read_thread(void *arg)
  1989. {
  1990. VideoState *is = arg;
  1991. AVFormatContext *ic = NULL;
  1992. int err, i, ret;
  1993. int st_index[AVMEDIA_TYPE_NB];
  1994. AVPacket pkt1, *pkt = &pkt1;
  1995. int eof=0;
  1996. int pkt_in_play_range = 0;
  1997. AVDictionaryEntry *t;
  1998. memset(st_index, -1, sizeof(st_index));
  1999. is->video_stream = -1;
  2000. is->audio_stream = -1;
  2001. is->subtitle_stream = -1;
  2002. global_video_state = is;
  2003. avio_set_interrupt_cb(decode_interrupt_cb);
  2004. err = avformat_open_input(&ic, is->filename, is->iformat, &format_opts);
  2005. if (err < 0) {
  2006. print_error(is->filename, err);
  2007. ret = -1;
  2008. goto fail;
  2009. }
  2010. if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
  2011. av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
  2012. ret = AVERROR_OPTION_NOT_FOUND;
  2013. goto fail;
  2014. }
  2015. is->ic = ic;
  2016. if(genpts)
  2017. ic->flags |= AVFMT_FLAG_GENPTS;
  2018. err = av_find_stream_info(ic);
  2019. if (err < 0) {
  2020. fprintf(stderr, "%s: could not find codec parameters\n", is->filename);
  2021. ret = -1;
  2022. goto fail;
  2023. }
  2024. if(ic->pb)
  2025. ic->pb->eof_reached= 0; //FIXME hack, ffplay maybe should not use url_feof() to test for the end
  2026. if(seek_by_bytes<0)
  2027. seek_by_bytes= !!(ic->iformat->flags & AVFMT_TS_DISCONT);
  2028. /* if seeking requested, we execute it */
  2029. if (start_time != AV_NOPTS_VALUE) {
  2030. int64_t timestamp;
  2031. timestamp = start_time;
  2032. /* add the stream start time */
  2033. if (ic->start_time != AV_NOPTS_VALUE)
  2034. timestamp += ic->start_time;
  2035. ret = avformat_seek_file(ic, -1, INT64_MIN, timestamp, INT64_MAX, 0);
  2036. if (ret < 0) {
  2037. fprintf(stderr, "%s: could not seek to position %0.3f\n",
  2038. is->filename, (double)timestamp / AV_TIME_BASE);
  2039. }
  2040. }
  2041. for (i = 0; i < ic->nb_streams; i++)
  2042. ic->streams[i]->discard = AVDISCARD_ALL;
  2043. if (!video_disable)
  2044. st_index[AVMEDIA_TYPE_VIDEO] =
  2045. av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO,
  2046. wanted_stream[AVMEDIA_TYPE_VIDEO], -1, NULL, 0);
  2047. if (!audio_disable)
  2048. st_index[AVMEDIA_TYPE_AUDIO] =
  2049. av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO,
  2050. wanted_stream[AVMEDIA_TYPE_AUDIO],
  2051. st_index[AVMEDIA_TYPE_VIDEO],
  2052. NULL, 0);
  2053. if (!video_disable)
  2054. st_index[AVMEDIA_TYPE_SUBTITLE] =
  2055. av_find_best_stream(ic, AVMEDIA_TYPE_SUBTITLE,
  2056. wanted_stream[AVMEDIA_TYPE_SUBTITLE],
  2057. (st_index[AVMEDIA_TYPE_AUDIO] >= 0 ?
  2058. st_index[AVMEDIA_TYPE_AUDIO] :
  2059. st_index[AVMEDIA_TYPE_VIDEO]),
  2060. NULL, 0);
  2061. if (show_status) {
  2062. av_dump_format(ic, 0, is->filename, 0);
  2063. }
  2064. is->show_mode = show_mode;
  2065. /* open the streams */
  2066. if (st_index[AVMEDIA_TYPE_AUDIO] >= 0) {
  2067. stream_component_open(is, st_index[AVMEDIA_TYPE_AUDIO]);
  2068. }
  2069. ret=-1;
  2070. if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) {
  2071. ret= stream_component_open(is, st_index[AVMEDIA_TYPE_VIDEO]);
  2072. }
  2073. is->refresh_tid = SDL_CreateThread(refresh_thread, is);
  2074. if (is->show_mode == SHOW_MODE_NONE)
  2075. is->show_mode = ret >= 0 ? SHOW_MODE_VIDEO : SHOW_MODE_RDFT;
  2076. if (st_index[AVMEDIA_TYPE_SUBTITLE] >= 0) {
  2077. stream_component_open(is, st_index[AVMEDIA_TYPE_SUBTITLE]);
  2078. }
  2079. if (is->video_stream < 0 && is->audio_stream < 0) {
  2080. fprintf(stderr, "%s: could not open codecs\n", is->filename);
  2081. ret = -1;
  2082. goto fail;
  2083. }
  2084. for(;;) {
  2085. if (is->abort_request)
  2086. break;
  2087. if (is->paused != is->last_paused) {
  2088. is->last_paused = is->paused;
  2089. if (is->paused)
  2090. is->read_pause_return= av_read_pause(ic);
  2091. else
  2092. av_read_play(ic);
  2093. }
  2094. #if CONFIG_RTSP_DEMUXER
  2095. if (is->paused && !strcmp(ic->iformat->name, "rtsp")) {
  2096. /* wait 10 ms to avoid trying to get another packet */
  2097. /* XXX: horrible */
  2098. SDL_Delay(10);
  2099. continue;
  2100. }
  2101. #endif
  2102. if (is->seek_req) {
  2103. int64_t seek_target= is->seek_pos;
  2104. int64_t seek_min= is->seek_rel > 0 ? seek_target - is->seek_rel + 2: INT64_MIN;
  2105. int64_t seek_max= is->seek_rel < 0 ? seek_target - is->seek_rel - 2: INT64_MAX;
  2106. //FIXME the +-2 is due to rounding being not done in the correct direction in generation
  2107. // of the seek_pos/seek_rel variables
  2108. ret = avformat_seek_file(is->ic, -1, seek_min, seek_target, seek_max, is->seek_flags);
  2109. if (ret < 0) {
  2110. fprintf(stderr, "%s: error while seeking\n", is->ic->filename);
  2111. }else{
  2112. if (is->audio_stream >= 0) {
  2113. packet_queue_flush(&is->audioq);
  2114. packet_queue_put(&is->audioq, &flush_pkt);
  2115. }
  2116. if (is->subtitle_stream >= 0) {
  2117. packet_queue_flush(&is->subtitleq);
  2118. packet_queue_put(&is->subtitleq, &flush_pkt);
  2119. }
  2120. if (is->video_stream >= 0) {
  2121. packet_queue_flush(&is->videoq);
  2122. packet_queue_put(&is->videoq, &flush_pkt);
  2123. }
  2124. }
  2125. is->seek_req = 0;
  2126. eof= 0;
  2127. }
  2128. /* if the queue are full, no need to read more */
  2129. if ( is->audioq.size + is->videoq.size + is->subtitleq.size > MAX_QUEUE_SIZE
  2130. || ( (is->audioq .size > MIN_AUDIOQ_SIZE || is->audio_stream<0)
  2131. && (is->videoq .nb_packets > MIN_FRAMES || is->video_stream<0)
  2132. && (is->subtitleq.nb_packets > MIN_FRAMES || is->subtitle_stream<0))) {
  2133. /* wait 10 ms */
  2134. SDL_Delay(10);
  2135. continue;
  2136. }
  2137. if(eof) {
  2138. if(is->video_stream >= 0){
  2139. av_init_packet(pkt);
  2140. pkt->data=NULL;
  2141. pkt->size=0;
  2142. pkt->stream_index= is->video_stream;
  2143. packet_queue_put(&is->videoq, pkt);
  2144. }
  2145. SDL_Delay(10);
  2146. if(is->audioq.size + is->videoq.size + is->subtitleq.size ==0){
  2147. if(loop!=1 && (!loop || --loop)){
  2148. stream_seek(cur_stream, start_time != AV_NOPTS_VALUE ? start_time : 0, 0, 0);
  2149. }else if(autoexit){
  2150. ret=AVERROR_EOF;
  2151. goto fail;
  2152. }
  2153. }
  2154. eof=0;
  2155. continue;
  2156. }
  2157. ret = av_read_frame(ic, pkt);
  2158. if (ret < 0) {
  2159. if (ret == AVERROR_EOF || url_feof(ic->pb))
  2160. eof=1;
  2161. if (ic->pb && ic->pb->error)
  2162. break;
  2163. SDL_Delay(100); /* wait for user event */
  2164. continue;
  2165. }
  2166. /* check if packet is in play range specified by user, then queue, otherwise discard */
  2167. pkt_in_play_range = duration == AV_NOPTS_VALUE ||
  2168. (pkt->pts - ic->streams[pkt->stream_index]->start_time) *
  2169. av_q2d(ic->streams[pkt->stream_index]->time_base) -
  2170. (double)(start_time != AV_NOPTS_VALUE ? start_time : 0)/1000000
  2171. <= ((double)duration/1000000);
  2172. if (pkt->stream_index == is->audio_stream && pkt_in_play_range) {
  2173. packet_queue_put(&is->audioq, pkt);
  2174. } else if (pkt->stream_index == is->video_stream && pkt_in_play_range) {
  2175. packet_queue_put(&is->videoq, pkt);
  2176. } else if (pkt->stream_index == is->subtitle_stream && pkt_in_play_range) {
  2177. packet_queue_put(&is->subtitleq, pkt);
  2178. } else {
  2179. av_free_packet(pkt);
  2180. }
  2181. }
  2182. /* wait until the end */
  2183. while (!is->abort_request) {
  2184. SDL_Delay(100);
  2185. }
  2186. ret = 0;
  2187. fail:
  2188. /* disable interrupting */
  2189. global_video_state = NULL;
  2190. /* close each stream */
  2191. if (is->audio_stream >= 0)
  2192. stream_component_close(is, is->audio_stream);
  2193. if (is->video_stream >= 0)
  2194. stream_component_close(is, is->video_stream);
  2195. if (is->subtitle_stream >= 0)
  2196. stream_component_close(is, is->subtitle_stream);
  2197. if (is->ic) {
  2198. av_close_input_file(is->ic);
  2199. is->ic = NULL; /* safety */
  2200. }
  2201. avio_set_interrupt_cb(NULL);
  2202. if (ret != 0) {
  2203. SDL_Event event;
  2204. event.type = FF_QUIT_EVENT;
  2205. event.user.data1 = is;
  2206. SDL_PushEvent(&event);
  2207. }
  2208. return 0;
  2209. }
  2210. static VideoState *stream_open(const char *filename, AVInputFormat *iformat)
  2211. {
  2212. VideoState *is;
  2213. is = av_mallocz(sizeof(VideoState));
  2214. if (!is)
  2215. return NULL;
  2216. av_strlcpy(is->filename, filename, sizeof(is->filename));
  2217. is->iformat = iformat;
  2218. is->ytop = 0;
  2219. is->xleft = 0;
  2220. /* start video display */
  2221. is->pictq_mutex = SDL_CreateMutex();
  2222. is->pictq_cond = SDL_CreateCond();
  2223. is->subpq_mutex = SDL_CreateMutex();
  2224. is->subpq_cond = SDL_CreateCond();
  2225. is->av_sync_type = av_sync_type;
  2226. is->read_tid = SDL_CreateThread(read_thread, is);
  2227. if (!is->read_tid) {
  2228. av_free(is);
  2229. return NULL;
  2230. }
  2231. return is;
  2232. }
  2233. static void stream_cycle_channel(VideoState *is, int codec_type)
  2234. {
  2235. AVFormatContext *ic = is->ic;
  2236. int start_index, stream_index;
  2237. AVStream *st;
  2238. if (codec_type == AVMEDIA_TYPE_VIDEO)
  2239. start_index = is->video_stream;
  2240. else if (codec_type == AVMEDIA_TYPE_AUDIO)
  2241. start_index = is->audio_stream;
  2242. else
  2243. start_index = is->subtitle_stream;
  2244. if (start_index < (codec_type == AVMEDIA_TYPE_SUBTITLE ? -1 : 0))
  2245. return;
  2246. stream_index = start_index;
  2247. for(;;) {
  2248. if (++stream_index >= is->ic->nb_streams)
  2249. {
  2250. if (codec_type == AVMEDIA_TYPE_SUBTITLE)
  2251. {
  2252. stream_index = -1;
  2253. goto the_end;
  2254. } else
  2255. stream_index = 0;
  2256. }
  2257. if (stream_index == start_index)
  2258. return;
  2259. st = ic->streams[stream_index];
  2260. if (st->codec->codec_type == codec_type) {
  2261. /* check that parameters are OK */
  2262. switch(codec_type) {
  2263. case AVMEDIA_TYPE_AUDIO:
  2264. if (st->codec->sample_rate != 0 &&
  2265. st->codec->channels != 0)
  2266. goto the_end;
  2267. break;
  2268. case AVMEDIA_TYPE_VIDEO:
  2269. case AVMEDIA_TYPE_SUBTITLE:
  2270. goto the_end;
  2271. default:
  2272. break;
  2273. }
  2274. }
  2275. }
  2276. the_end:
  2277. stream_component_close(is, start_index);
  2278. stream_component_open(is, stream_index);
  2279. }
  2280. static void toggle_full_screen(void)
  2281. {
  2282. is_full_screen = !is_full_screen;
  2283. video_open(cur_stream);
  2284. }
  2285. static void toggle_pause(void)
  2286. {
  2287. if (cur_stream)
  2288. stream_toggle_pause(cur_stream);
  2289. step = 0;
  2290. }
  2291. static void step_to_next_frame(void)
  2292. {
  2293. if (cur_stream) {
  2294. /* if the stream is paused unpause it, then step */
  2295. if (cur_stream->paused)
  2296. stream_toggle_pause(cur_stream);
  2297. }
  2298. step = 1;
  2299. }
  2300. static void toggle_audio_display(void)
  2301. {
  2302. if (cur_stream) {
  2303. int bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
  2304. cur_stream->show_mode = (cur_stream->show_mode + 1) % SHOW_MODE_NB;
  2305. fill_rectangle(screen,
  2306. cur_stream->xleft, cur_stream->ytop, cur_stream->width, cur_stream->height,
  2307. bgcolor);
  2308. SDL_UpdateRect(screen, cur_stream->xleft, cur_stream->ytop, cur_stream->width, cur_stream->height);
  2309. }
  2310. }
  2311. /* handle an event sent by the GUI */
  2312. static void event_loop(void)
  2313. {
  2314. SDL_Event event;
  2315. double incr, pos, frac;
  2316. for(;;) {
  2317. double x;
  2318. SDL_WaitEvent(&event);
  2319. switch(event.type) {
  2320. case SDL_KEYDOWN:
  2321. if (exit_on_keydown) {
  2322. do_exit();
  2323. break;
  2324. }
  2325. switch(event.key.keysym.sym) {
  2326. case SDLK_ESCAPE:
  2327. case SDLK_q:
  2328. do_exit();
  2329. break;
  2330. case SDLK_f:
  2331. toggle_full_screen();
  2332. break;
  2333. case SDLK_p:
  2334. case SDLK_SPACE:
  2335. toggle_pause();
  2336. break;
  2337. case SDLK_s: //S: Step to next frame
  2338. step_to_next_frame();
  2339. break;
  2340. case SDLK_a:
  2341. if (cur_stream)
  2342. stream_cycle_channel(cur_stream, AVMEDIA_TYPE_AUDIO);
  2343. break;
  2344. case SDLK_v:
  2345. if (cur_stream)
  2346. stream_cycle_channel(cur_stream, AVMEDIA_TYPE_VIDEO);
  2347. break;
  2348. case SDLK_t:
  2349. if (cur_stream)
  2350. stream_cycle_channel(cur_stream, AVMEDIA_TYPE_SUBTITLE);
  2351. break;
  2352. case SDLK_w:
  2353. toggle_audio_display();
  2354. break;
  2355. case SDLK_LEFT:
  2356. incr = -10.0;
  2357. goto do_seek;
  2358. case SDLK_RIGHT:
  2359. incr = 10.0;
  2360. goto do_seek;
  2361. case SDLK_UP:
  2362. incr = 60.0;
  2363. goto do_seek;
  2364. case SDLK_DOWN:
  2365. incr = -60.0;
  2366. do_seek:
  2367. if (cur_stream) {
  2368. if (seek_by_bytes) {
  2369. if (cur_stream->video_stream >= 0 && cur_stream->video_current_pos>=0){
  2370. pos= cur_stream->video_current_pos;
  2371. }else if(cur_stream->audio_stream >= 0 && cur_stream->audio_pkt.pos>=0){
  2372. pos= cur_stream->audio_pkt.pos;
  2373. }else
  2374. pos = avio_tell(cur_stream->ic->pb);
  2375. if (cur_stream->ic->bit_rate)
  2376. incr *= cur_stream->ic->bit_rate / 8.0;
  2377. else
  2378. incr *= 180000.0;
  2379. pos += incr;
  2380. stream_seek(cur_stream, pos, incr, 1);
  2381. } else {
  2382. pos = get_master_clock(cur_stream);
  2383. pos += incr;
  2384. stream_seek(cur_stream, (int64_t)(pos * AV_TIME_BASE), (int64_t)(incr * AV_TIME_BASE), 0);
  2385. }
  2386. }
  2387. break;
  2388. default:
  2389. break;
  2390. }
  2391. break;
  2392. case SDL_MOUSEBUTTONDOWN:
  2393. if (exit_on_mousedown) {
  2394. do_exit();
  2395. break;
  2396. }
  2397. case SDL_MOUSEMOTION:
  2398. if(event.type ==SDL_MOUSEBUTTONDOWN){
  2399. x= event.button.x;
  2400. }else{
  2401. if(event.motion.state != SDL_PRESSED)
  2402. break;
  2403. x= event.motion.x;
  2404. }
  2405. if (cur_stream) {
  2406. if(seek_by_bytes || cur_stream->ic->duration<=0){
  2407. uint64_t size= avio_size(cur_stream->ic->pb);
  2408. stream_seek(cur_stream, size*x/cur_stream->width, 0, 1);
  2409. }else{
  2410. int64_t ts;
  2411. int ns, hh, mm, ss;
  2412. int tns, thh, tmm, tss;
  2413. tns = cur_stream->ic->duration/1000000LL;
  2414. thh = tns/3600;
  2415. tmm = (tns%3600)/60;
  2416. tss = (tns%60);
  2417. frac = x/cur_stream->width;
  2418. ns = frac*tns;
  2419. hh = ns/3600;
  2420. mm = (ns%3600)/60;
  2421. ss = (ns%60);
  2422. fprintf(stderr, "Seek to %2.0f%% (%2d:%02d:%02d) of total duration (%2d:%02d:%02d) \n", frac*100,
  2423. hh, mm, ss, thh, tmm, tss);
  2424. ts = frac*cur_stream->ic->duration;
  2425. if (cur_stream->ic->start_time != AV_NOPTS_VALUE)
  2426. ts += cur_stream->ic->start_time;
  2427. stream_seek(cur_stream, ts, 0, 0);
  2428. }
  2429. }
  2430. break;
  2431. case SDL_VIDEORESIZE:
  2432. if (cur_stream) {
  2433. screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 0,
  2434. SDL_HWSURFACE|SDL_RESIZABLE|SDL_ASYNCBLIT|SDL_HWACCEL);
  2435. screen_width = cur_stream->width = event.resize.w;
  2436. screen_height= cur_stream->height= event.resize.h;
  2437. }
  2438. break;
  2439. case SDL_QUIT:
  2440. case FF_QUIT_EVENT:
  2441. do_exit();
  2442. break;
  2443. case FF_ALLOC_EVENT:
  2444. video_open(event.user.data1);
  2445. alloc_picture(event.user.data1);
  2446. break;
  2447. case FF_REFRESH_EVENT:
  2448. video_refresh(event.user.data1);
  2449. cur_stream->refresh=0;
  2450. break;
  2451. default:
  2452. break;
  2453. }
  2454. }
  2455. }
  2456. static int opt_frame_size(const char *opt, const char *arg)
  2457. {
  2458. if (av_parse_video_size(&frame_width, &frame_height, arg) < 0) {
  2459. fprintf(stderr, "Incorrect frame size\n");
  2460. return AVERROR(EINVAL);
  2461. }
  2462. if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
  2463. fprintf(stderr, "Frame size must be a multiple of 2\n");
  2464. return AVERROR(EINVAL);
  2465. }
  2466. return 0;
  2467. }
  2468. static int opt_width(const char *opt, const char *arg)
  2469. {
  2470. screen_width = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
  2471. return 0;
  2472. }
  2473. static int opt_height(const char *opt, const char *arg)
  2474. {
  2475. screen_height = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
  2476. return 0;
  2477. }
  2478. static int opt_format(const char *opt, const char *arg)
  2479. {
  2480. file_iformat = av_find_input_format(arg);
  2481. if (!file_iformat) {
  2482. fprintf(stderr, "Unknown input format: %s\n", arg);
  2483. return AVERROR(EINVAL);
  2484. }
  2485. return 0;
  2486. }
  2487. static int opt_frame_pix_fmt(const char *opt, const char *arg)
  2488. {
  2489. frame_pix_fmt = av_get_pix_fmt(arg);
  2490. return 0;
  2491. }
  2492. static int opt_sync(const char *opt, const char *arg)
  2493. {
  2494. if (!strcmp(arg, "audio"))
  2495. av_sync_type = AV_SYNC_AUDIO_MASTER;
  2496. else if (!strcmp(arg, "video"))
  2497. av_sync_type = AV_SYNC_VIDEO_MASTER;
  2498. else if (!strcmp(arg, "ext"))
  2499. av_sync_type = AV_SYNC_EXTERNAL_CLOCK;
  2500. else {
  2501. fprintf(stderr, "Unknown value for %s: %s\n", opt, arg);
  2502. exit(1);
  2503. }
  2504. return 0;
  2505. }
  2506. static int opt_seek(const char *opt, const char *arg)
  2507. {
  2508. start_time = parse_time_or_die(opt, arg, 1);
  2509. return 0;
  2510. }
  2511. static int opt_duration(const char *opt, const char *arg)
  2512. {
  2513. duration = parse_time_or_die(opt, arg, 1);
  2514. return 0;
  2515. }
  2516. static int opt_thread_count(const char *opt, const char *arg)
  2517. {
  2518. thread_count= parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
  2519. #if !HAVE_THREADS
  2520. fprintf(stderr, "Warning: not compiled with thread support, using thread emulation\n");
  2521. #endif
  2522. return 0;
  2523. }
  2524. static int opt_show_mode(const char *opt, const char *arg)
  2525. {
  2526. show_mode = !strcmp(arg, "video") ? SHOW_MODE_VIDEO :
  2527. !strcmp(arg, "waves") ? SHOW_MODE_WAVES :
  2528. !strcmp(arg, "rdft" ) ? SHOW_MODE_RDFT :
  2529. parse_number_or_die(opt, arg, OPT_INT, 0, SHOW_MODE_NB-1);
  2530. return 0;
  2531. }
  2532. static int opt_input_file(const char *opt, const char *filename)
  2533. {
  2534. if (input_filename) {
  2535. fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n",
  2536. filename, input_filename);
  2537. exit(1);
  2538. }
  2539. if (!strcmp(filename, "-"))
  2540. filename = "pipe:";
  2541. input_filename = filename;
  2542. return 0;
  2543. }
  2544. static const OptionDef options[] = {
  2545. #include "cmdutils_common_opts.h"
  2546. { "x", HAS_ARG, {(void*)opt_width}, "force displayed width", "width" },
  2547. { "y", HAS_ARG, {(void*)opt_height}, "force displayed height", "height" },
  2548. { "s", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_size}, "set frame size (WxH or abbreviation)", "size" },
  2549. { "fs", OPT_BOOL, {(void*)&is_full_screen}, "force full screen" },
  2550. { "an", OPT_BOOL, {(void*)&audio_disable}, "disable audio" },
  2551. { "vn", OPT_BOOL, {(void*)&video_disable}, "disable video" },
  2552. { "ast", OPT_INT | HAS_ARG | OPT_EXPERT, {(void*)&wanted_stream[AVMEDIA_TYPE_AUDIO]}, "select desired audio stream", "stream_number" },
  2553. { "vst", OPT_INT | HAS_ARG | OPT_EXPERT, {(void*)&wanted_stream[AVMEDIA_TYPE_VIDEO]}, "select desired video stream", "stream_number" },
  2554. { "sst", OPT_INT | HAS_ARG | OPT_EXPERT, {(void*)&wanted_stream[AVMEDIA_TYPE_SUBTITLE]}, "select desired subtitle stream", "stream_number" },
  2555. { "ss", HAS_ARG, {(void*)&opt_seek}, "seek to a given position in seconds", "pos" },
  2556. { "t", HAS_ARG, {(void*)&opt_duration}, "play \"duration\" seconds of audio/video", "duration" },
  2557. { "bytes", OPT_INT | HAS_ARG, {(void*)&seek_by_bytes}, "seek by bytes 0=off 1=on -1=auto", "val" },
  2558. { "nodisp", OPT_BOOL, {(void*)&display_disable}, "disable graphical display" },
  2559. { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
  2560. { "pix_fmt", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_frame_pix_fmt}, "set pixel format", "format" },
  2561. { "stats", OPT_BOOL | OPT_EXPERT, {(void*)&show_status}, "show status", "" },
  2562. { "bug", OPT_INT | HAS_ARG | OPT_EXPERT, {(void*)&workaround_bugs}, "workaround bugs", "" },
  2563. { "fast", OPT_BOOL | OPT_EXPERT, {(void*)&fast}, "non spec compliant optimizations", "" },
  2564. { "genpts", OPT_BOOL | OPT_EXPERT, {(void*)&genpts}, "generate pts", "" },
  2565. { "drp", OPT_INT | HAS_ARG | OPT_EXPERT, {(void*)&decoder_reorder_pts}, "let decoder reorder pts 0=off 1=on -1=auto", ""},
  2566. { "lowres", OPT_INT | HAS_ARG | OPT_EXPERT, {(void*)&lowres}, "", "" },
  2567. { "skiploop", OPT_INT | HAS_ARG | OPT_EXPERT, {(void*)&skip_loop_filter}, "", "" },
  2568. { "skipframe", OPT_INT | HAS_ARG | OPT_EXPERT, {(void*)&skip_frame}, "", "" },
  2569. { "skipidct", OPT_INT | HAS_ARG | OPT_EXPERT, {(void*)&skip_idct}, "", "" },
  2570. { "idct", OPT_INT | HAS_ARG | OPT_EXPERT, {(void*)&idct}, "set idct algo", "algo" },
  2571. { "er", OPT_INT | HAS_ARG | OPT_EXPERT, {(void*)&error_recognition}, "set error detection threshold (0-4)", "threshold" },
  2572. { "ec", OPT_INT | HAS_ARG | OPT_EXPERT, {(void*)&error_concealment}, "set error concealment options", "bit_mask" },
  2573. { "sync", HAS_ARG | OPT_EXPERT, {(void*)opt_sync}, "set audio-video sync. type (type=audio/video/ext)", "type" },
  2574. { "threads", HAS_ARG | OPT_EXPERT, {(void*)opt_thread_count}, "thread count", "count" },
  2575. { "autoexit", OPT_BOOL | OPT_EXPERT, {(void*)&autoexit}, "exit at the end", "" },
  2576. { "exitonkeydown", OPT_BOOL | OPT_EXPERT, {(void*)&exit_on_keydown}, "exit on key down", "" },
  2577. { "exitonmousedown", OPT_BOOL | OPT_EXPERT, {(void*)&exit_on_mousedown}, "exit on mouse down", "" },
  2578. { "loop", OPT_INT | HAS_ARG | OPT_EXPERT, {(void*)&loop}, "set number of times the playback shall be looped", "loop count" },
  2579. { "framedrop", OPT_BOOL | OPT_EXPERT, {(void*)&framedrop}, "drop frames when cpu is too slow", "" },
  2580. { "window_title", OPT_STRING | HAS_ARG, {(void*)&window_title}, "set window title", "window title" },
  2581. #if CONFIG_AVFILTER
  2582. { "vf", OPT_STRING | HAS_ARG, {(void*)&vfilters}, "video filters", "filter list" },
  2583. #endif
  2584. { "rdftspeed", OPT_INT | HAS_ARG| OPT_AUDIO | OPT_EXPERT, {(void*)&rdftspeed}, "rdft speed", "msecs" },
  2585. { "showmode", HAS_ARG, {(void*)opt_show_mode}, "select show mode (0 = video, 1 = waves, 2 = RDFT)", "mode" },
  2586. { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {(void*)opt_default}, "generic catch all option", "" },
  2587. { "i", HAS_ARG, {(void *)opt_input_file}, "read specified file", "input_file"},
  2588. { NULL, },
  2589. };
  2590. static void show_usage(void)
  2591. {
  2592. printf("Simple media player\n");
  2593. printf("usage: ffplay [options] input_file\n");
  2594. printf("\n");
  2595. }
  2596. static int opt_help(const char *opt, const char *arg)
  2597. {
  2598. av_log_set_callback(log_callback_help);
  2599. show_usage();
  2600. show_help_options(options, "Main options:\n",
  2601. OPT_EXPERT, 0);
  2602. show_help_options(options, "\nAdvanced options:\n",
  2603. OPT_EXPERT, OPT_EXPERT);
  2604. printf("\n");
  2605. av_opt_show2(avcodec_opts[0], NULL,
  2606. AV_OPT_FLAG_DECODING_PARAM, 0);
  2607. printf("\n");
  2608. av_opt_show2(avformat_opts, NULL,
  2609. AV_OPT_FLAG_DECODING_PARAM, 0);
  2610. #if !CONFIG_AVFILTER
  2611. printf("\n");
  2612. av_opt_show2(sws_opts, NULL,
  2613. AV_OPT_FLAG_ENCODING_PARAM, 0);
  2614. #endif
  2615. printf("\nWhile playing:\n"
  2616. "q, ESC quit\n"
  2617. "f toggle full screen\n"
  2618. "p, SPC pause\n"
  2619. "a cycle audio channel\n"
  2620. "v cycle video channel\n"
  2621. "t cycle subtitle channel\n"
  2622. "w show audio waves\n"
  2623. "s activate frame-step mode\n"
  2624. "left/right seek backward/forward 10 seconds\n"
  2625. "down/up seek backward/forward 1 minute\n"
  2626. "mouse click seek to percentage in file corresponding to fraction of width\n"
  2627. );
  2628. return 0;
  2629. }
  2630. /* Called from the main */
  2631. int main(int argc, char **argv)
  2632. {
  2633. int flags;
  2634. av_log_set_flags(AV_LOG_SKIP_REPEATED);
  2635. /* register all codecs, demux and protocols */
  2636. avcodec_register_all();
  2637. #if CONFIG_AVDEVICE
  2638. avdevice_register_all();
  2639. #endif
  2640. #if CONFIG_AVFILTER
  2641. avfilter_register_all();
  2642. #endif
  2643. av_register_all();
  2644. init_opts();
  2645. show_banner();
  2646. parse_options(argc, argv, options, opt_input_file);
  2647. if (!input_filename) {
  2648. show_usage();
  2649. fprintf(stderr, "An input file must be specified\n");
  2650. fprintf(stderr, "Use -h to get full help or, even better, run 'man ffplay'\n");
  2651. exit(1);
  2652. }
  2653. if (display_disable) {
  2654. video_disable = 1;
  2655. }
  2656. flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER;
  2657. if (audio_disable)
  2658. flags &= ~SDL_INIT_AUDIO;
  2659. #if !defined(__MINGW32__) && !defined(__APPLE__)
  2660. flags |= SDL_INIT_EVENTTHREAD; /* Not supported on Windows or Mac OS X */
  2661. #endif
  2662. if (SDL_Init (flags)) {
  2663. fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
  2664. fprintf(stderr, "(Did you set the DISPLAY variable?)\n");
  2665. exit(1);
  2666. }
  2667. if (!display_disable) {
  2668. #if HAVE_SDL_VIDEO_SIZE
  2669. const SDL_VideoInfo *vi = SDL_GetVideoInfo();
  2670. fs_screen_width = vi->current_w;
  2671. fs_screen_height = vi->current_h;
  2672. #endif
  2673. }
  2674. SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
  2675. SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
  2676. SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
  2677. av_init_packet(&flush_pkt);
  2678. flush_pkt.data= "FLUSH";
  2679. cur_stream = stream_open(input_filename, file_iformat);
  2680. event_loop();
  2681. /* never returns */
  2682. return 0;
  2683. }