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.

1727 lines
50KB

  1. /*
  2. * FFplay : Simple Media Player based on the ffmpeg libraries
  3. * Copyright (c) 2003 Fabrice Bellard
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #define HAVE_AV_CONFIG_H
  20. #include "common.h"
  21. #include "avformat.h"
  22. #include "cmdutils.h"
  23. #include <SDL.h>
  24. #include <SDL_thread.h>
  25. #ifdef CONFIG_WIN32
  26. #undef main /* We don't want SDL to override our main() */
  27. #endif
  28. #if defined(__linux__)
  29. #define HAVE_X11
  30. #endif
  31. #ifdef HAVE_X11
  32. #include <X11/Xlib.h>
  33. #endif
  34. //#define DEBUG_SYNC
  35. #define MAX_VIDEOQ_SIZE (5 * 256 * 1024)
  36. #define MAX_AUDIOQ_SIZE (5 * 16 * 1024)
  37. /* SDL audio buffer size, in samples. Should be small to have precise
  38. A/V sync as SDL does not have hardware buffer fullness info. */
  39. #define SDL_AUDIO_BUFFER_SIZE 1024
  40. /* no AV sync correction is done if below the AV sync threshold */
  41. #define AV_SYNC_THRESHOLD 0.08
  42. /* no AV correction is done if too big error */
  43. #define AV_NOSYNC_THRESHOLD 10.0
  44. /* maximum audio speed change to get correct sync */
  45. #define SAMPLE_CORRECTION_PERCENT_MAX 10
  46. /* we use about AUDIO_DIFF_AVG_NB A-V differences to make the average */
  47. #define AUDIO_DIFF_AVG_NB 20
  48. /* NOTE: the size must be big enough to compensate the hardware audio buffersize size */
  49. #define SAMPLE_ARRAY_SIZE (2*65536)
  50. typedef struct PacketQueue {
  51. AVPacketList *first_pkt, *last_pkt;
  52. int nb_packets;
  53. int size;
  54. int abort_request;
  55. SDL_mutex *mutex;
  56. SDL_cond *cond;
  57. } PacketQueue;
  58. #define VIDEO_PICTURE_QUEUE_SIZE 1
  59. typedef struct VideoPicture {
  60. double pts; /* presentation time stamp for this picture */
  61. SDL_Overlay *bmp;
  62. int width, height; /* source height & width */
  63. int allocated;
  64. } VideoPicture;
  65. enum {
  66. AV_SYNC_AUDIO_MASTER, /* default choice */
  67. AV_SYNC_VIDEO_MASTER,
  68. AV_SYNC_EXTERNAL_CLOCK, /* synchronize to an external clock */
  69. };
  70. typedef struct VideoState {
  71. SDL_Thread *parse_tid;
  72. SDL_Thread *video_tid;
  73. AVInputFormat *iformat;
  74. int no_background;
  75. int abort_request;
  76. int paused;
  77. int last_paused;
  78. AVFormatContext *ic;
  79. int dtg_active_format;
  80. int audio_stream;
  81. int av_sync_type;
  82. double external_clock; /* external clock base */
  83. int64_t external_clock_time;
  84. double audio_clock;
  85. double audio_diff_cum; /* used for AV difference average computation */
  86. double audio_diff_avg_coef;
  87. double audio_diff_threshold;
  88. int audio_diff_avg_count;
  89. AVStream *audio_st;
  90. PacketQueue audioq;
  91. int audio_hw_buf_size;
  92. /* samples output by the codec. we reserve more space for avsync
  93. compensation */
  94. uint8_t audio_buf[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2];
  95. int audio_buf_size; /* in bytes */
  96. int audio_buf_index; /* in bytes */
  97. AVPacket audio_pkt;
  98. uint8_t *audio_pkt_data;
  99. int audio_pkt_size;
  100. int64_t audio_pkt_ipts;
  101. int show_audio; /* if true, display audio samples */
  102. int16_t sample_array[SAMPLE_ARRAY_SIZE];
  103. int sample_array_index;
  104. int last_i_start;
  105. double frame_timer;
  106. double frame_last_pts;
  107. double frame_last_delay;
  108. double video_clock;
  109. int video_stream;
  110. AVStream *video_st;
  111. PacketQueue videoq;
  112. int64_t ipts;
  113. int picture_start; /* true if picture starts */
  114. double video_last_P_pts; /* pts of the last P picture (needed if B
  115. frames are present) */
  116. double video_current_pts; /* current displayed pts (different from
  117. video_clock if frame fifos are used) */
  118. int64_t video_current_pts_time; /* time at which we updated
  119. video_current_pts - used to
  120. have running video pts */
  121. VideoPicture pictq[VIDEO_PICTURE_QUEUE_SIZE];
  122. int pictq_size, pictq_rindex, pictq_windex;
  123. SDL_mutex *pictq_mutex;
  124. SDL_cond *pictq_cond;
  125. // QETimer *video_timer;
  126. char filename[1024];
  127. int width, height, xleft, ytop;
  128. } VideoState;
  129. void show_help(void);
  130. static int audio_write_get_buf_size(VideoState *is);
  131. /* options specified by the user */
  132. static AVInputFormat *file_iformat;
  133. static const char *input_filename;
  134. static int fs_screen_width;
  135. static int fs_screen_height;
  136. static int screen_width = 640;
  137. static int screen_height = 480;
  138. static int audio_disable;
  139. static int video_disable;
  140. static int display_disable;
  141. static int show_status;
  142. static int av_sync_type = AV_SYNC_AUDIO_MASTER;
  143. /* current context */
  144. static int is_full_screen;
  145. static VideoState *cur_stream;
  146. static int64_t audio_callback_time;
  147. #define FF_ALLOC_EVENT (SDL_USEREVENT)
  148. #define FF_REFRESH_EVENT (SDL_USEREVENT + 1)
  149. #define FF_QUIT_EVENT (SDL_USEREVENT + 2)
  150. SDL_Surface *screen;
  151. /* packet queue handling */
  152. static void packet_queue_init(PacketQueue *q)
  153. {
  154. memset(q, 0, sizeof(PacketQueue));
  155. q->mutex = SDL_CreateMutex();
  156. q->cond = SDL_CreateCond();
  157. }
  158. static void packet_queue_end(PacketQueue *q)
  159. {
  160. AVPacketList *pkt, *pkt1;
  161. for(pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
  162. pkt1 = pkt->next;
  163. av_free_packet(&pkt->pkt);
  164. }
  165. SDL_DestroyMutex(q->mutex);
  166. SDL_DestroyCond(q->cond);
  167. }
  168. static int packet_queue_put(PacketQueue *q, AVPacket *pkt)
  169. {
  170. AVPacketList *pkt1;
  171. pkt1 = av_malloc(sizeof(AVPacketList));
  172. if (!pkt1)
  173. return -1;
  174. pkt1->pkt = *pkt;
  175. pkt1->next = NULL;
  176. SDL_LockMutex(q->mutex);
  177. if (!q->last_pkt)
  178. q->first_pkt = pkt1;
  179. else
  180. q->last_pkt->next = pkt1;
  181. q->last_pkt = pkt1;
  182. q->nb_packets++;
  183. q->size += pkt1->pkt.size;
  184. /* XXX: should duplicate packet data in DV case */
  185. SDL_CondSignal(q->cond);
  186. SDL_UnlockMutex(q->mutex);
  187. return 0;
  188. }
  189. static void packet_queue_abort(PacketQueue *q)
  190. {
  191. SDL_LockMutex(q->mutex);
  192. q->abort_request = 1;
  193. SDL_CondSignal(q->cond);
  194. SDL_UnlockMutex(q->mutex);
  195. }
  196. /* return < 0 if aborted, 0 if no packet and > 0 if packet. */
  197. static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block)
  198. {
  199. AVPacketList *pkt1;
  200. int ret;
  201. SDL_LockMutex(q->mutex);
  202. for(;;) {
  203. if (q->abort_request) {
  204. ret = -1;
  205. break;
  206. }
  207. pkt1 = q->first_pkt;
  208. if (pkt1) {
  209. q->first_pkt = pkt1->next;
  210. if (!q->first_pkt)
  211. q->last_pkt = NULL;
  212. q->nb_packets--;
  213. q->size -= pkt1->pkt.size;
  214. *pkt = pkt1->pkt;
  215. av_free(pkt1);
  216. ret = 1;
  217. break;
  218. } else if (!block) {
  219. ret = 0;
  220. break;
  221. } else {
  222. SDL_CondWait(q->cond, q->mutex);
  223. }
  224. }
  225. SDL_UnlockMutex(q->mutex);
  226. return ret;
  227. }
  228. static inline void fill_rectangle(SDL_Surface *screen,
  229. int x, int y, int w, int h, int color)
  230. {
  231. SDL_Rect rect;
  232. rect.x = x;
  233. rect.y = y;
  234. rect.w = w;
  235. rect.h = h;
  236. SDL_FillRect(screen, &rect, color);
  237. }
  238. #if 0
  239. /* draw only the border of a rectangle */
  240. void fill_border(VideoState *s, int x, int y, int w, int h, int color)
  241. {
  242. int w1, w2, h1, h2;
  243. /* fill the background */
  244. w1 = x;
  245. if (w1 < 0)
  246. w1 = 0;
  247. w2 = s->width - (x + w);
  248. if (w2 < 0)
  249. w2 = 0;
  250. h1 = y;
  251. if (h1 < 0)
  252. h1 = 0;
  253. h2 = s->height - (y + h);
  254. if (h2 < 0)
  255. h2 = 0;
  256. fill_rectangle(screen,
  257. s->xleft, s->ytop,
  258. w1, s->height,
  259. color);
  260. fill_rectangle(screen,
  261. s->xleft + s->width - w2, s->ytop,
  262. w2, s->height,
  263. color);
  264. fill_rectangle(screen,
  265. s->xleft + w1, s->ytop,
  266. s->width - w1 - w2, h1,
  267. color);
  268. fill_rectangle(screen,
  269. s->xleft + w1, s->ytop + s->height - h2,
  270. s->width - w1 - w2, h2,
  271. color);
  272. }
  273. #endif
  274. static void video_image_display(VideoState *is)
  275. {
  276. VideoPicture *vp;
  277. float aspect_ratio;
  278. int width, height, x, y;
  279. SDL_Rect rect;
  280. vp = &is->pictq[is->pictq_rindex];
  281. if (vp->bmp) {
  282. /* XXX: use variable in the frame */
  283. aspect_ratio = is->video_st->codec.aspect_ratio;
  284. if (aspect_ratio <= 0.0)
  285. aspect_ratio = (float)is->video_st->codec.width /
  286. (float)is->video_st->codec.height;
  287. /* if an active format is indicated, then it overrides the
  288. mpeg format */
  289. #if 0
  290. if (is->video_st->codec.dtg_active_format != is->dtg_active_format) {
  291. is->dtg_active_format = is->video_st->codec.dtg_active_format;
  292. printf("dtg_active_format=%d\n", is->dtg_active_format);
  293. }
  294. #endif
  295. #if 0
  296. switch(is->video_st->codec.dtg_active_format) {
  297. case FF_DTG_AFD_SAME:
  298. default:
  299. /* nothing to do */
  300. break;
  301. case FF_DTG_AFD_4_3:
  302. aspect_ratio = 4.0 / 3.0;
  303. break;
  304. case FF_DTG_AFD_16_9:
  305. aspect_ratio = 16.0 / 9.0;
  306. break;
  307. case FF_DTG_AFD_14_9:
  308. aspect_ratio = 14.0 / 9.0;
  309. break;
  310. case FF_DTG_AFD_4_3_SP_14_9:
  311. aspect_ratio = 14.0 / 9.0;
  312. break;
  313. case FF_DTG_AFD_16_9_SP_14_9:
  314. aspect_ratio = 14.0 / 9.0;
  315. break;
  316. case FF_DTG_AFD_SP_4_3:
  317. aspect_ratio = 4.0 / 3.0;
  318. break;
  319. }
  320. #endif
  321. /* XXX: we suppose the screen has a 1.0 pixel ratio */
  322. height = is->height;
  323. width = ((int)rint(height * aspect_ratio)) & -3;
  324. if (width > is->width) {
  325. width = is->width;
  326. height = ((int)rint(width / aspect_ratio)) & -3;
  327. }
  328. x = (is->width - width) / 2;
  329. y = (is->height - height) / 2;
  330. if (!is->no_background) {
  331. /* fill the background */
  332. // fill_border(is, x, y, width, height, QERGB(0x00, 0x00, 0x00));
  333. } else {
  334. is->no_background = 0;
  335. }
  336. rect.x = is->xleft + x;
  337. rect.y = is->xleft + y;
  338. rect.w = width;
  339. rect.h = height;
  340. SDL_DisplayYUVOverlay(vp->bmp, &rect);
  341. } else {
  342. #if 0
  343. fill_rectangle(screen,
  344. is->xleft, is->ytop, is->width, is->height,
  345. QERGB(0x00, 0x00, 0x00));
  346. #endif
  347. }
  348. }
  349. static inline int compute_mod(int a, int b)
  350. {
  351. a = a % b;
  352. if (a >= 0)
  353. return a;
  354. else
  355. return a + b;
  356. }
  357. static void video_audio_display(VideoState *s)
  358. {
  359. int i, i_start, x, y1, y, ys, delay, n, nb_display_channels;
  360. int ch, channels, h, h2, bgcolor, fgcolor;
  361. int16_t time_diff;
  362. /* compute display index : center on currently output samples */
  363. channels = s->audio_st->codec.channels;
  364. nb_display_channels = channels;
  365. if (!s->paused) {
  366. n = 2 * channels;
  367. delay = audio_write_get_buf_size(s);
  368. delay /= n;
  369. /* to be more precise, we take into account the time spent since
  370. the last buffer computation */
  371. if (audio_callback_time) {
  372. time_diff = av_gettime() - audio_callback_time;
  373. delay += (time_diff * s->audio_st->codec.sample_rate) / 1000000;
  374. }
  375. delay -= s->width / 2;
  376. if (delay < s->width)
  377. delay = s->width;
  378. i_start = compute_mod(s->sample_array_index - delay * channels, SAMPLE_ARRAY_SIZE);
  379. s->last_i_start = i_start;
  380. } else {
  381. i_start = s->last_i_start;
  382. }
  383. bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
  384. fill_rectangle(screen,
  385. s->xleft, s->ytop, s->width, s->height,
  386. bgcolor);
  387. fgcolor = SDL_MapRGB(screen->format, 0xff, 0xff, 0xff);
  388. /* total height for one channel */
  389. h = s->height / nb_display_channels;
  390. /* graph height / 2 */
  391. h2 = (h * 9) / 20;
  392. for(ch = 0;ch < nb_display_channels; ch++) {
  393. i = i_start + ch;
  394. y1 = s->ytop + ch * h + (h / 2); /* position of center line */
  395. for(x = 0; x < s->width; x++) {
  396. y = (s->sample_array[i] * h2) >> 15;
  397. if (y < 0) {
  398. y = -y;
  399. ys = y1 - y;
  400. } else {
  401. ys = y1;
  402. }
  403. fill_rectangle(screen,
  404. s->xleft + x, ys, 1, y,
  405. fgcolor);
  406. i += channels;
  407. if (i >= SAMPLE_ARRAY_SIZE)
  408. i -= SAMPLE_ARRAY_SIZE;
  409. }
  410. }
  411. fgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0xff);
  412. for(ch = 1;ch < nb_display_channels; ch++) {
  413. y = s->ytop + ch * h;
  414. fill_rectangle(screen,
  415. s->xleft, y, s->width, 1,
  416. fgcolor);
  417. }
  418. SDL_UpdateRect(screen, s->xleft, s->ytop, s->width, s->height);
  419. }
  420. /* display the current picture, if any */
  421. static void video_display(VideoState *is)
  422. {
  423. if (is->audio_st && is->show_audio)
  424. video_audio_display(is);
  425. else if (is->video_st)
  426. video_image_display(is);
  427. }
  428. static Uint32 sdl_refresh_timer_cb(Uint32 interval, void *opaque)
  429. {
  430. SDL_Event event;
  431. event.type = FF_REFRESH_EVENT;
  432. event.user.data1 = opaque;
  433. SDL_PushEvent(&event);
  434. return 0; /* 0 means stop timer */
  435. }
  436. /* schedule a video refresh in 'delay' ms */
  437. static void schedule_refresh(VideoState *is, int delay)
  438. {
  439. SDL_AddTimer(delay, sdl_refresh_timer_cb, is);
  440. }
  441. /* get the current audio clock value */
  442. static double get_audio_clock(VideoState *is)
  443. {
  444. double pts;
  445. int hw_buf_size, bytes_per_sec;
  446. pts = is->audio_clock;
  447. hw_buf_size = audio_write_get_buf_size(is);
  448. bytes_per_sec = 0;
  449. if (is->audio_st) {
  450. bytes_per_sec = is->audio_st->codec.sample_rate *
  451. 2 * is->audio_st->codec.channels;
  452. }
  453. if (bytes_per_sec)
  454. pts -= (double)hw_buf_size / bytes_per_sec;
  455. return pts;
  456. }
  457. /* get the current video clock value */
  458. static double get_video_clock(VideoState *is)
  459. {
  460. double delta;
  461. delta = (av_gettime() - is->video_current_pts_time) / 1000000.0;
  462. return is->video_current_pts + delta;
  463. }
  464. /* get the current external clock value */
  465. static double get_external_clock(VideoState *is)
  466. {
  467. int64_t ti;
  468. ti = av_gettime();
  469. return is->external_clock + ((ti - is->external_clock_time) * 1e-6);
  470. }
  471. /* get the current master clock value */
  472. static double get_master_clock(VideoState *is)
  473. {
  474. double val;
  475. if (is->av_sync_type == AV_SYNC_VIDEO_MASTER && is->video_st)
  476. val = get_video_clock(is);
  477. else if (is->av_sync_type == AV_SYNC_AUDIO_MASTER && is->audio_st)
  478. val = get_audio_clock(is);
  479. else
  480. val = get_external_clock(is);
  481. return val;
  482. }
  483. /* called to display each frame */
  484. static void video_refresh_timer(void *opaque)
  485. {
  486. VideoState *is = opaque;
  487. VideoPicture *vp;
  488. double actual_delay, delay, sync_threshold, ref_clock, diff;
  489. if (is->video_st) {
  490. if (is->pictq_size == 0) {
  491. /* if no picture, need to wait */
  492. schedule_refresh(is, 40);
  493. } else {
  494. /* dequeue the picture */
  495. vp = &is->pictq[is->pictq_rindex];
  496. /* update current video pts */
  497. is->video_current_pts = vp->pts;
  498. is->video_current_pts_time = av_gettime();
  499. /* compute nominal delay */
  500. delay = vp->pts - is->frame_last_pts;
  501. if (delay <= 0 || delay >= 1.0) {
  502. /* if incorrect delay, use previous one */
  503. delay = is->frame_last_delay;
  504. }
  505. is->frame_last_delay = delay;
  506. is->frame_last_pts = vp->pts;
  507. /* update delay to follow master synchronisation source */
  508. if (((is->av_sync_type == AV_SYNC_AUDIO_MASTER && is->audio_st) ||
  509. is->av_sync_type == AV_SYNC_EXTERNAL_CLOCK)) {
  510. /* if video is slave, we try to correct big delays by
  511. duplicating or deleting a frame */
  512. ref_clock = get_master_clock(is);
  513. diff = vp->pts - ref_clock;
  514. /* skip or repeat frame. We take into account the
  515. delay to compute the threshold. I still don't know
  516. if it is the best guess */
  517. sync_threshold = AV_SYNC_THRESHOLD;
  518. if (delay > sync_threshold)
  519. sync_threshold = delay;
  520. if (fabs(diff) < AV_NOSYNC_THRESHOLD) {
  521. if (diff <= -sync_threshold)
  522. delay = 0;
  523. else if (diff >= sync_threshold)
  524. delay = 2 * delay;
  525. }
  526. }
  527. is->frame_timer += delay;
  528. /* compute the REAL delay (we need to do that to avoid
  529. long term errors */
  530. actual_delay = is->frame_timer - (av_gettime() / 1000000.0);
  531. if (actual_delay < 0.010) {
  532. /* XXX: should skip picture */
  533. actual_delay = 0.010;
  534. }
  535. /* launch timer for next picture */
  536. schedule_refresh(is, (int)(actual_delay * 1000 + 0.5));
  537. #if defined(DEBUG_SYNC)
  538. printf("video: delay=%0.3f actual_delay=%0.3f pts=%0.3f A-V=%f\n",
  539. delay, actual_delay, vp->pts, -diff);
  540. #endif
  541. /* display picture */
  542. video_display(is);
  543. /* update queue size and signal for next picture */
  544. if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE)
  545. is->pictq_rindex = 0;
  546. SDL_LockMutex(is->pictq_mutex);
  547. is->pictq_size--;
  548. SDL_CondSignal(is->pictq_cond);
  549. SDL_UnlockMutex(is->pictq_mutex);
  550. }
  551. } else if (is->audio_st) {
  552. /* draw the next audio frame */
  553. schedule_refresh(is, 40);
  554. /* if only audio stream, then display the audio bars (better
  555. than nothing, just to test the implementation */
  556. /* display picture */
  557. video_display(is);
  558. } else {
  559. schedule_refresh(is, 100);
  560. }
  561. if (show_status) {
  562. static int64_t last_time;
  563. int64_t cur_time;
  564. int aqsize, vqsize;
  565. double av_diff;
  566. cur_time = av_gettime();
  567. if (!last_time || (cur_time - last_time) >= 500 * 1000) {
  568. aqsize = 0;
  569. vqsize = 0;
  570. if (is->audio_st)
  571. aqsize = is->audioq.size;
  572. if (is->video_st)
  573. vqsize = is->videoq.size;
  574. av_diff = 0;
  575. if (is->audio_st && is->video_st)
  576. av_diff = get_audio_clock(is) - get_video_clock(is);
  577. printf("%7.2f A-V:%7.3f aq=%5dKB vq=%5dKB \r",
  578. get_master_clock(is), av_diff, aqsize / 1024, vqsize / 1024);
  579. fflush(stdout);
  580. last_time = cur_time;
  581. }
  582. }
  583. }
  584. /* allocate a picture (needs to do that in main thread to avoid
  585. potential locking problems */
  586. static void alloc_picture(void *opaque)
  587. {
  588. VideoState *is = opaque;
  589. VideoPicture *vp;
  590. int is_yuv;
  591. vp = &is->pictq[is->pictq_windex];
  592. if (vp->bmp)
  593. SDL_FreeYUVOverlay(vp->bmp);
  594. /* XXX: use generic function */
  595. switch(is->video_st->codec.pix_fmt) {
  596. case PIX_FMT_YUV420P:
  597. case PIX_FMT_YUV422P:
  598. case PIX_FMT_YUV444P:
  599. case PIX_FMT_YUV422:
  600. case PIX_FMT_YUV410P:
  601. case PIX_FMT_YUV411P:
  602. is_yuv = 1;
  603. break;
  604. default:
  605. is_yuv = 0;
  606. break;
  607. }
  608. if (is_yuv) {
  609. vp->bmp = SDL_CreateYUVOverlay(is->video_st->codec.width,
  610. is->video_st->codec.height,
  611. SDL_YV12_OVERLAY,
  612. screen);
  613. } else {
  614. #if 0
  615. vp->bmp = bmp_alloc(screen,
  616. is->video_st->codec.width,
  617. is->video_st->codec.height,
  618. screen->bitmap_format,
  619. 0);
  620. #endif
  621. vp->bmp = NULL;
  622. }
  623. vp->width = is->video_st->codec.width;
  624. vp->height = is->video_st->codec.height;
  625. SDL_LockMutex(is->pictq_mutex);
  626. vp->allocated = 1;
  627. SDL_CondSignal(is->pictq_cond);
  628. SDL_UnlockMutex(is->pictq_mutex);
  629. }
  630. static int queue_picture(VideoState *is, AVFrame *src_frame, double pts)
  631. {
  632. VideoPicture *vp;
  633. int dst_pix_fmt;
  634. AVPicture pict;
  635. /* wait until we have space to put a new picture */
  636. SDL_LockMutex(is->pictq_mutex);
  637. while (is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE &&
  638. !is->videoq.abort_request) {
  639. SDL_CondWait(is->pictq_cond, is->pictq_mutex);
  640. }
  641. SDL_UnlockMutex(is->pictq_mutex);
  642. if (is->videoq.abort_request)
  643. return -1;
  644. vp = &is->pictq[is->pictq_windex];
  645. /* alloc or resize hardware picture buffer */
  646. if (!vp->bmp ||
  647. vp->width != is->video_st->codec.width ||
  648. vp->height != is->video_st->codec.height) {
  649. SDL_Event event;
  650. vp->allocated = 0;
  651. /* the allocation must be done in the main thread to avoid
  652. locking problems */
  653. event.type = FF_ALLOC_EVENT;
  654. event.user.data1 = is;
  655. SDL_PushEvent(&event);
  656. /* wait until the picture is allocated */
  657. SDL_LockMutex(is->pictq_mutex);
  658. while (!vp->allocated && !is->videoq.abort_request) {
  659. SDL_CondWait(is->pictq_cond, is->pictq_mutex);
  660. }
  661. SDL_UnlockMutex(is->pictq_mutex);
  662. if (is->videoq.abort_request)
  663. return -1;
  664. }
  665. /* if the frame is not skipped, then display it */
  666. if (vp->bmp) {
  667. /* get a pointer on the bitmap */
  668. SDL_LockYUVOverlay (vp->bmp);
  669. dst_pix_fmt = PIX_FMT_YUV420P;
  670. pict.data[0] = vp->bmp->pixels[0];
  671. pict.data[1] = vp->bmp->pixels[2];
  672. pict.data[2] = vp->bmp->pixels[1];
  673. pict.linesize[0] = vp->bmp->pitches[0];
  674. pict.linesize[1] = vp->bmp->pitches[2];
  675. pict.linesize[2] = vp->bmp->pitches[1];
  676. img_convert(&pict, dst_pix_fmt,
  677. (AVPicture *)src_frame, is->video_st->codec.pix_fmt,
  678. is->video_st->codec.width, is->video_st->codec.height);
  679. /* update the bitmap content */
  680. SDL_UnlockYUVOverlay(vp->bmp);
  681. vp->pts = pts;
  682. /* now we can update the picture count */
  683. if (++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE)
  684. is->pictq_windex = 0;
  685. SDL_LockMutex(is->pictq_mutex);
  686. is->pictq_size++;
  687. SDL_UnlockMutex(is->pictq_mutex);
  688. }
  689. return 0;
  690. }
  691. /* compute the exact PTS for the picture if it is omitted in the stream */
  692. static int output_picture2(VideoState *is, AVFrame *src_frame, double pts1)
  693. {
  694. double frame_delay, pts;
  695. pts = pts1;
  696. /* if B frames are present, and if the current picture is a I
  697. or P frame, we use the last pts */
  698. if (is->video_st->codec.has_b_frames &&
  699. src_frame->pict_type != FF_B_TYPE) {
  700. /* use last pts */
  701. pts = is->video_last_P_pts;
  702. /* get the pts for the next I or P frame if present */
  703. is->video_last_P_pts = pts1;
  704. }
  705. if (pts != 0) {
  706. /* update video clock with pts, if present */
  707. is->video_clock = pts;
  708. } else {
  709. frame_delay = (double)is->video_st->codec.frame_rate_base /
  710. (double)is->video_st->codec.frame_rate;
  711. is->video_clock += frame_delay;
  712. /* for MPEG2, the frame can be repeated, so we update the
  713. clock accordingly */
  714. if (src_frame->repeat_pict) {
  715. is->video_clock += src_frame->repeat_pict * (frame_delay * 0.5);
  716. }
  717. }
  718. #if defined(DEBUG_SYNC) && 0
  719. {
  720. int ftype;
  721. if (src_frame->pict_type == FF_B_TYPE)
  722. ftype = 'B';
  723. else if (src_frame->pict_type == FF_I_TYPE)
  724. ftype = 'I';
  725. else
  726. ftype = 'P';
  727. printf("frame_type=%c clock=%0.3f pts=%0.3f\n",
  728. ftype, is->video_clock, pts1);
  729. }
  730. #endif
  731. return queue_picture(is, src_frame, is->video_clock);
  732. }
  733. static int video_thread(void *arg)
  734. {
  735. VideoState *is = arg;
  736. AVPacket pkt1, *pkt = &pkt1;
  737. unsigned char *ptr;
  738. int len, len1, got_picture;
  739. AVFrame frame;
  740. int64_t ipts;
  741. double pts;
  742. for(;;) {
  743. while (is->paused && !is->videoq.abort_request) {
  744. SDL_Delay(10);
  745. }
  746. if (packet_queue_get(&is->videoq, pkt, 1) < 0)
  747. break;
  748. /* NOTE: ipts is the PTS of the _first_ picture beginning in
  749. this packet, if any */
  750. ipts = pkt->pts;
  751. ptr = pkt->data;
  752. if (is->video_st->codec.codec_id == CODEC_ID_RAWVIDEO) {
  753. avpicture_fill((AVPicture *)&frame, ptr,
  754. is->video_st->codec.pix_fmt,
  755. is->video_st->codec.width,
  756. is->video_st->codec.height);
  757. pts = 0;
  758. if (ipts != AV_NOPTS_VALUE)
  759. pts = (double)ipts * is->ic->pts_num / is->ic->pts_den;
  760. frame.pict_type = FF_I_TYPE;
  761. if (output_picture2(is, &frame, pts) < 0)
  762. goto the_end;
  763. } else {
  764. len = pkt->size;
  765. while (len > 0) {
  766. if (is->picture_start) {
  767. is->ipts = ipts;
  768. is->picture_start = 0;
  769. ipts = AV_NOPTS_VALUE;
  770. }
  771. len1 = avcodec_decode_video(&is->video_st->codec,
  772. &frame, &got_picture, ptr, len);
  773. if (len1 < 0)
  774. break;
  775. if (got_picture) {
  776. pts = 0;
  777. if (is->ipts != AV_NOPTS_VALUE)
  778. pts = (double)is->ipts * is->ic->pts_num / is->ic->pts_den;
  779. if (output_picture2(is, &frame, pts) < 0)
  780. goto the_end;
  781. is->picture_start = 1;
  782. }
  783. ptr += len1;
  784. len -= len1;
  785. }
  786. }
  787. av_free_packet(pkt);
  788. }
  789. the_end:
  790. return 0;
  791. }
  792. /* copy samples for viewing in editor window */
  793. static void update_sample_display(VideoState *is, short *samples, int samples_size)
  794. {
  795. int size, len, channels;
  796. channels = is->audio_st->codec.channels;
  797. size = samples_size / sizeof(short);
  798. while (size > 0) {
  799. len = SAMPLE_ARRAY_SIZE - is->sample_array_index;
  800. if (len > size)
  801. len = size;
  802. memcpy(is->sample_array + is->sample_array_index, samples, len * sizeof(short));
  803. samples += len;
  804. is->sample_array_index += len;
  805. if (is->sample_array_index >= SAMPLE_ARRAY_SIZE)
  806. is->sample_array_index = 0;
  807. size -= len;
  808. }
  809. }
  810. /* return the new audio buffer size (samples can be added or deleted
  811. to get better sync if video or external master clock) */
  812. static int synchronize_audio(VideoState *is, short *samples,
  813. int samples_size1, double pts)
  814. {
  815. int n, samples_size;
  816. double ref_clock;
  817. n = 2 * is->audio_st->codec.channels;
  818. samples_size = samples_size1;
  819. /* if not master, then we try to remove or add samples to correct the clock */
  820. if (((is->av_sync_type == AV_SYNC_VIDEO_MASTER && is->video_st) ||
  821. is->av_sync_type == AV_SYNC_EXTERNAL_CLOCK)) {
  822. double diff, avg_diff;
  823. int wanted_size, min_size, max_size, nb_samples;
  824. ref_clock = get_master_clock(is);
  825. diff = get_audio_clock(is) - ref_clock;
  826. if (diff < AV_NOSYNC_THRESHOLD) {
  827. is->audio_diff_cum = diff + is->audio_diff_avg_coef * is->audio_diff_cum;
  828. if (is->audio_diff_avg_count < AUDIO_DIFF_AVG_NB) {
  829. /* not enough measures to have a correct estimate */
  830. is->audio_diff_avg_count++;
  831. } else {
  832. /* estimate the A-V difference */
  833. avg_diff = is->audio_diff_cum * (1.0 - is->audio_diff_avg_coef);
  834. if (fabs(avg_diff) >= is->audio_diff_threshold) {
  835. wanted_size = samples_size + ((int)(diff * is->audio_st->codec.sample_rate) * n);
  836. nb_samples = samples_size / n;
  837. min_size = ((nb_samples * (100 - SAMPLE_CORRECTION_PERCENT_MAX)) / 100) * n;
  838. max_size = ((nb_samples * (100 + SAMPLE_CORRECTION_PERCENT_MAX)) / 100) * n;
  839. if (wanted_size < min_size)
  840. wanted_size = min_size;
  841. else if (wanted_size > max_size)
  842. wanted_size = max_size;
  843. /* add or remove samples to correction the synchro */
  844. if (wanted_size < samples_size) {
  845. /* remove samples */
  846. samples_size = wanted_size;
  847. } else if (wanted_size > samples_size) {
  848. uint8_t *samples_end, *q;
  849. int nb;
  850. /* add samples */
  851. nb = (samples_size - wanted_size);
  852. samples_end = (uint8_t *)samples + samples_size - n;
  853. q = samples_end + n;
  854. while (nb > 0) {
  855. memcpy(q, samples_end, n);
  856. q += n;
  857. nb -= n;
  858. }
  859. samples_size = wanted_size;
  860. }
  861. }
  862. #if 0
  863. printf("diff=%f adiff=%f sample_diff=%d apts=%0.3f vpts=%0.3f %f\n",
  864. diff, avg_diff, samples_size - samples_size1,
  865. is->audio_clock, is->video_clock, is->audio_diff_threshold);
  866. #endif
  867. }
  868. } else {
  869. /* too big difference : may be initial PTS errors, so
  870. reset A-V filter */
  871. is->audio_diff_avg_count = 0;
  872. is->audio_diff_cum = 0;
  873. }
  874. }
  875. return samples_size;
  876. }
  877. /* decode one audio frame and returns its uncompressed size */
  878. static int audio_decode_frame(VideoState *is, uint8_t *audio_buf, double *pts_ptr)
  879. {
  880. AVPacket *pkt = &is->audio_pkt;
  881. int len1, data_size;
  882. double pts;
  883. for(;;) {
  884. if (is->paused || is->audioq.abort_request) {
  885. return -1;
  886. }
  887. while (is->audio_pkt_size > 0) {
  888. len1 = avcodec_decode_audio(&is->audio_st->codec,
  889. (int16_t *)audio_buf, &data_size,
  890. is->audio_pkt_data, is->audio_pkt_size);
  891. if (len1 < 0)
  892. break;
  893. is->audio_pkt_data += len1;
  894. is->audio_pkt_size -= len1;
  895. if (data_size > 0) {
  896. pts = 0;
  897. if (is->audio_pkt_ipts != AV_NOPTS_VALUE)
  898. pts = (double)is->audio_pkt_ipts * is->ic->pts_num / is->ic->pts_den;
  899. /* if no pts, then compute it */
  900. if (pts != 0) {
  901. is->audio_clock = pts;
  902. } else {
  903. int n;
  904. n = 2 * is->audio_st->codec.channels;
  905. is->audio_clock += (double)data_size / (double)(n * is->audio_st->codec.sample_rate);
  906. }
  907. #if defined(DEBUG_SYNC)
  908. {
  909. static double last_clock;
  910. printf("audio: delay=%0.3f clock=%0.3f pts=%0.3f\n",
  911. is->audio_clock - last_clock,
  912. is->audio_clock, pts);
  913. last_clock = is->audio_clock;
  914. }
  915. #endif
  916. *pts_ptr = is->audio_clock;
  917. is->audio_pkt_ipts = AV_NOPTS_VALUE;
  918. /* we got samples : we can exit now */
  919. return data_size;
  920. }
  921. }
  922. /* free previous packet if any */
  923. if (pkt->destruct)
  924. av_free_packet(pkt);
  925. /* read next packet */
  926. if (packet_queue_get(&is->audioq, pkt, 1) < 0)
  927. return -1;
  928. is->audio_pkt_data = pkt->data;
  929. is->audio_pkt_size = pkt->size;
  930. is->audio_pkt_ipts = pkt->pts;
  931. }
  932. }
  933. /* get the current audio output buffer size, in samples. With SDL, we
  934. cannot have a precise information */
  935. static int audio_write_get_buf_size(VideoState *is)
  936. {
  937. return is->audio_hw_buf_size - is->audio_buf_index;
  938. }
  939. /* prepare a new audio buffer */
  940. void sdl_audio_callback(void *opaque, Uint8 *stream, int len)
  941. {
  942. VideoState *is = opaque;
  943. int audio_size, len1;
  944. double pts;
  945. audio_callback_time = av_gettime();
  946. while (len > 0) {
  947. if (is->audio_buf_index >= is->audio_buf_size) {
  948. audio_size = audio_decode_frame(is, is->audio_buf, &pts);
  949. if (audio_size < 0) {
  950. /* if error, just output silence */
  951. is->audio_buf_size = 1024;
  952. memset(is->audio_buf, 0, is->audio_buf_size);
  953. } else {
  954. if (is->show_audio)
  955. update_sample_display(is, (int16_t *)is->audio_buf, audio_size);
  956. audio_size = synchronize_audio(is, (int16_t *)is->audio_buf, audio_size,
  957. pts);
  958. is->audio_buf_size = audio_size;
  959. }
  960. is->audio_buf_index = 0;
  961. }
  962. len1 = is->audio_buf_size - is->audio_buf_index;
  963. if (len1 > len)
  964. len1 = len;
  965. memcpy(stream, (uint8_t *)is->audio_buf + is->audio_buf_index, len1);
  966. len -= len1;
  967. stream += len1;
  968. is->audio_buf_index += len1;
  969. }
  970. }
  971. /* open a given stream. Return 0 if OK */
  972. static int stream_component_open(VideoState *is, int stream_index)
  973. {
  974. AVFormatContext *ic = is->ic;
  975. AVCodecContext *enc;
  976. AVCodec *codec;
  977. SDL_AudioSpec wanted_spec, spec;
  978. if (stream_index < 0 || stream_index >= ic->nb_streams)
  979. return -1;
  980. enc = &ic->streams[stream_index]->codec;
  981. /* prepare audio output */
  982. if (enc->codec_type == CODEC_TYPE_AUDIO) {
  983. wanted_spec.freq = enc->sample_rate;
  984. wanted_spec.format = AUDIO_S16SYS;
  985. /* hack for AC3. XXX: suppress that */
  986. if (enc->channels > 2)
  987. enc->channels = 2;
  988. wanted_spec.channels = enc->channels;
  989. wanted_spec.silence = 0;
  990. wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE;
  991. wanted_spec.callback = sdl_audio_callback;
  992. wanted_spec.userdata = is;
  993. if (SDL_OpenAudio(&wanted_spec, &spec) < 0) {
  994. fprintf(stderr, "SDL_OpenAudio: %s\n", SDL_GetError());
  995. return -1;
  996. }
  997. is->audio_hw_buf_size = spec.size;
  998. }
  999. codec = avcodec_find_decoder(enc->codec_id);
  1000. if (!codec ||
  1001. avcodec_open(enc, codec) < 0)
  1002. return -1;
  1003. switch(enc->codec_type) {
  1004. case CODEC_TYPE_AUDIO:
  1005. is->audio_stream = stream_index;
  1006. is->audio_st = ic->streams[stream_index];
  1007. is->audio_buf_size = 0;
  1008. is->audio_buf_index = 0;
  1009. is->audio_pkt_size = 0;
  1010. /* init averaging filter */
  1011. is->audio_diff_avg_coef = exp(log(0.01) / AUDIO_DIFF_AVG_NB);
  1012. is->audio_diff_avg_count = 0;
  1013. /* since we do not have a precise anough audio fifo fullness,
  1014. we correct audio sync only if larger than this threshold */
  1015. is->audio_diff_threshold = 2.0 * SDL_AUDIO_BUFFER_SIZE / enc->sample_rate;
  1016. memset(&is->audio_pkt, 0, sizeof(is->audio_pkt));
  1017. packet_queue_init(&is->audioq);
  1018. SDL_PauseAudio(0);
  1019. break;
  1020. case CODEC_TYPE_VIDEO:
  1021. is->video_stream = stream_index;
  1022. is->video_st = ic->streams[stream_index];
  1023. is->frame_last_delay = 40e-3;
  1024. is->frame_timer = (double)av_gettime() / 1000000.0;
  1025. is->picture_start = 1;
  1026. is->video_current_pts_time = av_gettime();
  1027. packet_queue_init(&is->videoq);
  1028. is->video_tid = SDL_CreateThread(video_thread, is);
  1029. break;
  1030. default:
  1031. break;
  1032. }
  1033. return 0;
  1034. }
  1035. static void stream_component_close(VideoState *is, int stream_index)
  1036. {
  1037. AVFormatContext *ic = is->ic;
  1038. AVCodecContext *enc;
  1039. enc = &ic->streams[stream_index]->codec;
  1040. switch(enc->codec_type) {
  1041. case CODEC_TYPE_AUDIO:
  1042. packet_queue_abort(&is->audioq);
  1043. SDL_CloseAudio();
  1044. packet_queue_end(&is->audioq);
  1045. break;
  1046. case CODEC_TYPE_VIDEO:
  1047. packet_queue_abort(&is->videoq);
  1048. /* note: we also signal this mutex to make sure we deblock the
  1049. video thread in all cases */
  1050. SDL_LockMutex(is->pictq_mutex);
  1051. SDL_CondSignal(is->pictq_cond);
  1052. SDL_UnlockMutex(is->pictq_mutex);
  1053. SDL_WaitThread(is->video_tid, NULL);
  1054. packet_queue_end(&is->videoq);
  1055. break;
  1056. default:
  1057. break;
  1058. }
  1059. avcodec_close(enc);
  1060. switch(enc->codec_type) {
  1061. case CODEC_TYPE_AUDIO:
  1062. is->audio_st = NULL;
  1063. is->audio_stream = -1;
  1064. break;
  1065. case CODEC_TYPE_VIDEO:
  1066. is->video_st = NULL;
  1067. is->video_stream = -1;
  1068. break;
  1069. default:
  1070. break;
  1071. }
  1072. }
  1073. /* since we have only one decoding thread, we can use a global
  1074. variable instead of a thread local variable */
  1075. static VideoState *global_video_state;
  1076. static int decode_interrupt_cb(void)
  1077. {
  1078. return (global_video_state && global_video_state->abort_request);
  1079. }
  1080. /* this thread gets the stream from the disk or the network */
  1081. static int decode_thread(void *arg)
  1082. {
  1083. VideoState *is = arg;
  1084. AVFormatContext *ic;
  1085. int err, i, ret, video_index, audio_index;
  1086. AVPacket pkt1, *pkt = &pkt1;
  1087. video_index = -1;
  1088. audio_index = -1;
  1089. is->video_stream = -1;
  1090. is->audio_stream = -1;
  1091. global_video_state = is;
  1092. url_set_interrupt_cb(decode_interrupt_cb);
  1093. err = av_open_input_file(&ic, is->filename, is->iformat, 0, NULL);
  1094. if (err < 0) {
  1095. print_error(is->filename, err);
  1096. ret = -1;
  1097. goto fail;
  1098. }
  1099. is->ic = ic;
  1100. err = av_find_stream_info(ic);
  1101. if (err < 0) {
  1102. fprintf(stderr, "%s: could not find codec parameters\n", is->filename);
  1103. ret = -1;
  1104. goto fail;
  1105. }
  1106. for(i = 0; i < ic->nb_streams; i++) {
  1107. AVCodecContext *enc = &ic->streams[i]->codec;
  1108. switch(enc->codec_type) {
  1109. case CODEC_TYPE_AUDIO:
  1110. if (audio_index < 0 && !audio_disable)
  1111. audio_index = i;
  1112. break;
  1113. case CODEC_TYPE_VIDEO:
  1114. if (video_index < 0 && !video_disable)
  1115. video_index = i;
  1116. break;
  1117. default:
  1118. break;
  1119. }
  1120. }
  1121. if (show_status) {
  1122. dump_format(ic, 0, is->filename, 0);
  1123. }
  1124. /* open the streams */
  1125. if (audio_index >= 0) {
  1126. stream_component_open(is, audio_index);
  1127. }
  1128. if (video_index >= 0) {
  1129. stream_component_open(is, video_index);
  1130. } else {
  1131. if (!display_disable)
  1132. is->show_audio = 1;
  1133. }
  1134. if (is->video_stream < 0 && is->audio_stream < 0) {
  1135. fprintf(stderr, "%s: could not open codecs\n", is->filename);
  1136. ret = -1;
  1137. goto fail;
  1138. }
  1139. for(;;) {
  1140. if (is->abort_request)
  1141. break;
  1142. if (is->paused != is->last_paused) {
  1143. is->last_paused = is->paused;
  1144. if (ic->iformat == &rtsp_demux) {
  1145. if (is->paused)
  1146. rtsp_pause(ic);
  1147. else
  1148. rtsp_resume(ic);
  1149. }
  1150. }
  1151. if (is->paused && ic->iformat == &rtsp_demux) {
  1152. /* wait 10 ms to avoid trying to get another packet */
  1153. /* XXX: horrible */
  1154. SDL_Delay(10);
  1155. continue;
  1156. }
  1157. /* if the queue are full, no need to read more */
  1158. if (is->audioq.size > MAX_AUDIOQ_SIZE ||
  1159. is->videoq.size > MAX_VIDEOQ_SIZE) {
  1160. /* wait 10 ms */
  1161. SDL_Delay(10);
  1162. continue;
  1163. }
  1164. ret = av_read_packet(ic, pkt);
  1165. if (ret < 0) {
  1166. break;
  1167. }
  1168. if (pkt->stream_index == is->audio_stream) {
  1169. packet_queue_put(&is->audioq, pkt);
  1170. } else if (pkt->stream_index == is->video_stream) {
  1171. packet_queue_put(&is->videoq, pkt);
  1172. } else {
  1173. av_free_packet(pkt);
  1174. }
  1175. }
  1176. /* wait until the end */
  1177. while (!is->abort_request) {
  1178. SDL_Delay(100);
  1179. }
  1180. ret = 0;
  1181. fail:
  1182. /* disable interrupting */
  1183. global_video_state = NULL;
  1184. /* close each stream */
  1185. if (is->audio_stream >= 0)
  1186. stream_component_close(is, is->audio_stream);
  1187. if (is->video_stream >= 0)
  1188. stream_component_close(is, is->video_stream);
  1189. if (is->ic) {
  1190. av_close_input_file(is->ic);
  1191. is->ic = NULL; /* safety */
  1192. }
  1193. url_set_interrupt_cb(NULL);
  1194. if (ret != 0) {
  1195. SDL_Event event;
  1196. event.type = FF_QUIT_EVENT;
  1197. event.user.data1 = is;
  1198. SDL_PushEvent(&event);
  1199. }
  1200. return 0;
  1201. }
  1202. /* pause or resume the video */
  1203. static void stream_pause(VideoState *is)
  1204. {
  1205. is->paused = !is->paused;
  1206. }
  1207. static VideoState *stream_open(const char *filename, AVInputFormat *iformat)
  1208. {
  1209. VideoState *is;
  1210. is = av_mallocz(sizeof(VideoState));
  1211. if (!is)
  1212. return NULL;
  1213. pstrcpy(is->filename, sizeof(is->filename), filename);
  1214. is->iformat = iformat;
  1215. if (screen) {
  1216. is->width = screen->w;
  1217. is->height = screen->h;
  1218. }
  1219. is->ytop = 0;
  1220. is->xleft = 0;
  1221. /* start video display */
  1222. is->pictq_mutex = SDL_CreateMutex();
  1223. is->pictq_cond = SDL_CreateCond();
  1224. /* add the refresh timer to draw the picture */
  1225. schedule_refresh(is, 40);
  1226. is->av_sync_type = av_sync_type;
  1227. is->parse_tid = SDL_CreateThread(decode_thread, is);
  1228. if (!is->parse_tid) {
  1229. av_free(is);
  1230. return NULL;
  1231. }
  1232. return is;
  1233. }
  1234. static void stream_close(VideoState *is)
  1235. {
  1236. VideoPicture *vp;
  1237. int i;
  1238. /* XXX: use a special url_shutdown call to abort parse cleanly */
  1239. is->abort_request = 1;
  1240. SDL_WaitThread(is->parse_tid, NULL);
  1241. /* free all pictures */
  1242. for(i=0;i<VIDEO_PICTURE_QUEUE_SIZE; i++) {
  1243. vp = &is->pictq[i];
  1244. if (vp->bmp) {
  1245. SDL_FreeYUVOverlay(vp->bmp);
  1246. vp->bmp = NULL;
  1247. }
  1248. }
  1249. SDL_DestroyMutex(is->pictq_mutex);
  1250. SDL_DestroyCond(is->pictq_cond);
  1251. }
  1252. void stream_cycle_channel(VideoState *is, int codec_type)
  1253. {
  1254. AVFormatContext *ic = is->ic;
  1255. int start_index, stream_index;
  1256. AVStream *st;
  1257. if (codec_type == CODEC_TYPE_VIDEO)
  1258. start_index = is->video_stream;
  1259. else
  1260. start_index = is->audio_stream;
  1261. if (start_index < 0)
  1262. return;
  1263. stream_index = start_index;
  1264. for(;;) {
  1265. if (++stream_index >= is->ic->nb_streams)
  1266. stream_index = 0;
  1267. if (stream_index == start_index)
  1268. return;
  1269. st = ic->streams[stream_index];
  1270. if (st->codec.codec_type == codec_type) {
  1271. /* check that parameters are OK */
  1272. switch(codec_type) {
  1273. case CODEC_TYPE_AUDIO:
  1274. if (st->codec.sample_rate != 0 &&
  1275. st->codec.channels != 0)
  1276. goto the_end;
  1277. break;
  1278. case CODEC_TYPE_VIDEO:
  1279. goto the_end;
  1280. default:
  1281. break;
  1282. }
  1283. }
  1284. }
  1285. the_end:
  1286. stream_component_close(is, start_index);
  1287. stream_component_open(is, stream_index);
  1288. }
  1289. void toggle_full_screen(void)
  1290. {
  1291. int w, h, flags;
  1292. is_full_screen = !is_full_screen;
  1293. if (!fs_screen_width) {
  1294. /* use default SDL method */
  1295. SDL_WM_ToggleFullScreen(screen);
  1296. } else {
  1297. /* use the recorded resolution */
  1298. flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
  1299. if (is_full_screen) {
  1300. w = fs_screen_width;
  1301. h = fs_screen_height;
  1302. flags |= SDL_FULLSCREEN;
  1303. } else {
  1304. w = screen_width;
  1305. h = screen_height;
  1306. flags |= SDL_RESIZABLE;
  1307. }
  1308. screen = SDL_SetVideoMode(w, h, 0, flags);
  1309. cur_stream->width = w;
  1310. cur_stream->height = h;
  1311. }
  1312. }
  1313. void toggle_pause(void)
  1314. {
  1315. if (cur_stream)
  1316. stream_pause(cur_stream);
  1317. }
  1318. void do_exit(void)
  1319. {
  1320. if (cur_stream) {
  1321. stream_close(cur_stream);
  1322. cur_stream = NULL;
  1323. }
  1324. if (show_status)
  1325. printf("\n");
  1326. SDL_Quit();
  1327. exit(0);
  1328. }
  1329. void toggle_audio_display(void)
  1330. {
  1331. if (cur_stream) {
  1332. cur_stream->show_audio = !cur_stream->show_audio;
  1333. }
  1334. }
  1335. /* handle an event sent by the GUI */
  1336. void event_loop(void)
  1337. {
  1338. SDL_Event event;
  1339. for(;;) {
  1340. SDL_WaitEvent(&event);
  1341. switch(event.type) {
  1342. case SDL_KEYDOWN:
  1343. switch(event.key.keysym.sym) {
  1344. case SDLK_ESCAPE:
  1345. case SDLK_q:
  1346. do_exit();
  1347. break;
  1348. case SDLK_f:
  1349. toggle_full_screen();
  1350. break;
  1351. case SDLK_p:
  1352. case SDLK_SPACE:
  1353. toggle_pause();
  1354. break;
  1355. case SDLK_a:
  1356. if (cur_stream)
  1357. stream_cycle_channel(cur_stream, CODEC_TYPE_AUDIO);
  1358. break;
  1359. case SDLK_v:
  1360. if (cur_stream)
  1361. stream_cycle_channel(cur_stream, CODEC_TYPE_VIDEO);
  1362. break;
  1363. case SDLK_w:
  1364. toggle_audio_display();
  1365. break;
  1366. default:
  1367. break;
  1368. }
  1369. break;
  1370. case SDL_VIDEORESIZE:
  1371. if (cur_stream) {
  1372. screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 0,
  1373. SDL_HWSURFACE|SDL_RESIZABLE|SDL_ASYNCBLIT|SDL_HWACCEL);
  1374. cur_stream->width = event.resize.w;
  1375. cur_stream->height = event.resize.h;
  1376. }
  1377. break;
  1378. case SDL_QUIT:
  1379. case FF_QUIT_EVENT:
  1380. do_exit();
  1381. break;
  1382. case FF_ALLOC_EVENT:
  1383. alloc_picture(event.user.data1);
  1384. break;
  1385. case FF_REFRESH_EVENT:
  1386. video_refresh_timer(event.user.data1);
  1387. break;
  1388. default:
  1389. break;
  1390. }
  1391. }
  1392. }
  1393. void opt_width(const char *arg)
  1394. {
  1395. screen_width = atoi(arg);
  1396. }
  1397. void opt_height(const char *arg)
  1398. {
  1399. screen_height = atoi(arg);
  1400. }
  1401. static void opt_format(const char *arg)
  1402. {
  1403. file_iformat = av_find_input_format(arg);
  1404. if (!file_iformat) {
  1405. fprintf(stderr, "Unknown input format: %s\n", arg);
  1406. exit(1);
  1407. }
  1408. }
  1409. void opt_rtp_tcp(void)
  1410. {
  1411. /* only tcp protocol */
  1412. rtsp_default_protocols = (1 << RTSP_PROTOCOL_RTP_TCP);
  1413. }
  1414. void opt_sync(const char *arg)
  1415. {
  1416. if (!strcmp(arg, "audio"))
  1417. av_sync_type = AV_SYNC_AUDIO_MASTER;
  1418. else if (!strcmp(arg, "video"))
  1419. av_sync_type = AV_SYNC_VIDEO_MASTER;
  1420. else if (!strcmp(arg, "ext"))
  1421. av_sync_type = AV_SYNC_EXTERNAL_CLOCK;
  1422. else
  1423. show_help();
  1424. }
  1425. const OptionDef options[] = {
  1426. { "h", 0, {(void*)show_help}, "show help" },
  1427. { "x", HAS_ARG, {(void*)opt_width}, "force displayed width", "width" },
  1428. { "y", HAS_ARG, {(void*)opt_height}, "force displayed height", "height" },
  1429. #if 0
  1430. /* disabled as SDL/X11 does not support it correctly on application launch */
  1431. { "fs", OPT_BOOL, {(void*)&is_full_screen}, "force full screen" },
  1432. #endif
  1433. { "an", OPT_BOOL, {(void*)&audio_disable}, "disable audio" },
  1434. { "vn", OPT_BOOL, {(void*)&video_disable}, "disable video" },
  1435. { "nodisp", OPT_BOOL, {(void*)&display_disable}, "disable graphical display" },
  1436. { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
  1437. { "stats", OPT_BOOL | OPT_EXPERT, {(void*)&show_status}, "show status", "" },
  1438. { "rtp_tcp", OPT_EXPERT, {(void*)&opt_rtp_tcp}, "force RTP/TCP protocol usage", "" },
  1439. { "sync", HAS_ARG | OPT_EXPERT, {(void*)&opt_sync}, "set audio-video sync. type (type=audio/video/ext)", "type" },
  1440. { NULL, },
  1441. };
  1442. void show_help(void)
  1443. {
  1444. printf("usage: ffplay [options] input_file\n"
  1445. "Simple media player\n");
  1446. printf("\n");
  1447. show_help_options(options);
  1448. printf("\nWhile playing:\n"
  1449. "q, ESC quit\n"
  1450. "f toggle full screen\n"
  1451. "p, SPC pause\n"
  1452. "a cycle audio channel\n"
  1453. "v cycle video channel\n"
  1454. "w show audio waves\n"
  1455. );
  1456. exit(1);
  1457. }
  1458. void parse_arg_file(const char *filename)
  1459. {
  1460. input_filename = filename;
  1461. }
  1462. /* Called from the main */
  1463. int main(int argc, char **argv)
  1464. {
  1465. int flags, w, h;
  1466. /* register all codecs, demux and protocols */
  1467. av_register_all();
  1468. parse_options(argc, argv, options);
  1469. if (!input_filename)
  1470. show_help();
  1471. if (display_disable) {
  1472. video_disable = 1;
  1473. }
  1474. flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER;
  1475. #ifndef CONFIG_WIN32
  1476. flags |= SDL_INIT_EVENTTHREAD; /* Not supported on win32 */
  1477. #endif
  1478. if (SDL_Init (flags)) {
  1479. fprintf(stderr, "Could not initialize SDL - exiting\n");
  1480. exit(1);
  1481. }
  1482. if (!display_disable) {
  1483. #ifdef HAVE_X11
  1484. /* save the screen resolution... SDL should allow full screen
  1485. by resizing the window */
  1486. {
  1487. Display *dpy;
  1488. dpy = XOpenDisplay(NULL);
  1489. if (dpy) {
  1490. fs_screen_width = DisplayWidth(dpy, DefaultScreen(dpy));
  1491. fs_screen_height = DisplayHeight(dpy, DefaultScreen(dpy));
  1492. XCloseDisplay(dpy);
  1493. }
  1494. }
  1495. #endif
  1496. flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
  1497. if (is_full_screen && fs_screen_width) {
  1498. w = fs_screen_width;
  1499. h = fs_screen_height;
  1500. flags |= SDL_FULLSCREEN;
  1501. } else {
  1502. w = screen_width;
  1503. h = screen_height;
  1504. flags |= SDL_RESIZABLE;
  1505. }
  1506. screen = SDL_SetVideoMode(w, h, 0, flags);
  1507. if (!screen) {
  1508. fprintf(stderr, "SDL: could not set video mode - exiting\n");
  1509. exit(1);
  1510. }
  1511. SDL_WM_SetCaption("FFplay", "FFplay");
  1512. }
  1513. SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
  1514. SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
  1515. SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
  1516. SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
  1517. cur_stream = stream_open(input_filename, file_iformat);
  1518. event_loop();
  1519. /* never returns */
  1520. return 0;
  1521. }