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.

1754 lines
51KB

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