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.

3142 lines
98KB

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