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.

1404 lines
39KB

  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. #if defined(__linux__)
  26. #define HAVE_X11
  27. #endif
  28. #ifdef HAVE_X11
  29. #include <X11/Xlib.h>
  30. #endif
  31. #define MAX_VIDEOQ_SIZE (5 * 256 * 1024)
  32. #define MAX_AUDIOQ_SIZE (5 * 16 * 1024)
  33. /* NOTE: the size must be big enough to compensate the hardware audio buffersize size */
  34. #define SAMPLE_ARRAY_SIZE (2*65536)
  35. typedef struct PacketQueue {
  36. AVPacketList *first_pkt, *last_pkt;
  37. int nb_packets;
  38. int size;
  39. int abort_request;
  40. SDL_mutex *mutex;
  41. SDL_cond *cond;
  42. } PacketQueue;
  43. #define VIDEO_PICTURE_QUEUE_SIZE 1
  44. typedef struct VideoPicture {
  45. int delay; /* delay before showing the next picture */
  46. SDL_Overlay *bmp;
  47. int width, height; /* source height & width */
  48. int allocated;
  49. } VideoPicture;
  50. enum {
  51. AV_SYNC_AUDIO_MASTER, /* default choice */
  52. AV_SYNC_VIDEO_MASTER,
  53. AV_SYNC_EXTERNAL_CLOCK, /* if external clock, then you must update external_clock yourself */
  54. };
  55. typedef struct VideoState {
  56. SDL_Thread *parse_tid;
  57. SDL_Thread *video_tid;
  58. int no_background;
  59. int abort_request;
  60. int paused;
  61. AVFormatContext *ic;
  62. int dtg_active_format;
  63. int audio_stream;
  64. int av_sync_type;
  65. double external_clock; /* external clock */
  66. double audio_clock; /* current audio clock value */
  67. AVStream *audio_st;
  68. PacketQueue audioq;
  69. int audio_hw_buf_size;
  70. /* samples output by the codec. we reserve more space for avsync
  71. compensation */
  72. uint8_t audio_buf[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2];
  73. int audio_buf_size; /* in bytes */
  74. int audio_buf_index; /* in bytes */
  75. AVPacket audio_pkt;
  76. uint8_t *audio_pkt_data;
  77. int audio_pkt_size;
  78. int64_t audio_pkt_ipts;
  79. int show_audio; /* if true, display audio samples */
  80. int16_t sample_array[SAMPLE_ARRAY_SIZE];
  81. int sample_array_index;
  82. int last_i_start;
  83. double video_clock; /* current video clock value */
  84. int video_stream;
  85. AVStream *video_st;
  86. PacketQueue videoq;
  87. VideoPicture pictq[VIDEO_PICTURE_QUEUE_SIZE];
  88. int pictq_size, pictq_rindex, pictq_windex;
  89. SDL_mutex *pictq_mutex;
  90. SDL_cond *pictq_cond;
  91. // QETimer *video_timer;
  92. char filename[1024];
  93. int width, height, xleft, ytop;
  94. } VideoState;
  95. void show_help(void);
  96. int audio_write_get_buf_size(VideoState *is);
  97. /* options specified by the user */
  98. static AVInputFormat *file_iformat;
  99. static const char *input_filename;
  100. static int fs_screen_width;
  101. static int fs_screen_height;
  102. static int screen_width = 640;
  103. static int screen_height = 480;
  104. static int audio_disable;
  105. static int video_disable;
  106. static int display_disable;
  107. static int show_status;
  108. /* current context */
  109. static int is_full_screen;
  110. static VideoState *cur_stream;
  111. static int64_t audio_callback_time;
  112. #define FF_ALLOC_EVENT (SDL_USEREVENT)
  113. #define FF_REFRESH_EVENT (SDL_USEREVENT + 1)
  114. SDL_Surface *screen;
  115. /* packet queue handling */
  116. static void packet_queue_init(PacketQueue *q)
  117. {
  118. memset(q, 0, sizeof(PacketQueue));
  119. q->mutex = SDL_CreateMutex();
  120. q->cond = SDL_CreateCond();
  121. }
  122. static void packet_queue_end(PacketQueue *q)
  123. {
  124. AVPacketList *pkt, *pkt1;
  125. for(pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
  126. pkt1 = pkt->next;
  127. av_free_packet(&pkt->pkt);
  128. }
  129. SDL_DestroyMutex(q->mutex);
  130. SDL_DestroyCond(q->cond);
  131. }
  132. static int packet_queue_put(PacketQueue *q, AVPacket *pkt)
  133. {
  134. AVPacketList *pkt1;
  135. pkt1 = av_malloc(sizeof(AVPacketList));
  136. if (!pkt1)
  137. return -1;
  138. pkt1->pkt = *pkt;
  139. pkt1->next = NULL;
  140. SDL_LockMutex(q->mutex);
  141. if (!q->last_pkt)
  142. q->first_pkt = pkt1;
  143. else
  144. q->last_pkt->next = pkt1;
  145. q->last_pkt = pkt1;
  146. q->nb_packets++;
  147. q->size += pkt1->pkt.size;
  148. /* XXX: should duplicate packet data in DV case */
  149. SDL_CondSignal(q->cond);
  150. SDL_UnlockMutex(q->mutex);
  151. return 0;
  152. }
  153. static void packet_queue_abort(PacketQueue *q)
  154. {
  155. SDL_LockMutex(q->mutex);
  156. q->abort_request = 1;
  157. SDL_CondSignal(q->cond);
  158. SDL_UnlockMutex(q->mutex);
  159. }
  160. /* return < 0 if aborted, 0 if no packet and > 0 if packet. */
  161. static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block)
  162. {
  163. AVPacketList *pkt1;
  164. int ret;
  165. SDL_LockMutex(q->mutex);
  166. for(;;) {
  167. if (q->abort_request) {
  168. ret = -1;
  169. break;
  170. }
  171. pkt1 = q->first_pkt;
  172. if (pkt1) {
  173. q->first_pkt = pkt1->next;
  174. if (!q->first_pkt)
  175. q->last_pkt = NULL;
  176. q->nb_packets--;
  177. q->size -= pkt1->pkt.size;
  178. *pkt = pkt1->pkt;
  179. av_free(pkt1);
  180. ret = 1;
  181. break;
  182. } else if (!block) {
  183. ret = 0;
  184. break;
  185. } else {
  186. SDL_CondWait(q->cond, q->mutex);
  187. }
  188. }
  189. SDL_UnlockMutex(q->mutex);
  190. return ret;
  191. }
  192. static inline void fill_rectangle(SDL_Surface *screen,
  193. int x, int y, int w, int h, int color)
  194. {
  195. SDL_Rect rect;
  196. rect.x = x;
  197. rect.y = y;
  198. rect.w = w;
  199. rect.h = h;
  200. SDL_FillRect(screen, &rect, color);
  201. }
  202. #if 0
  203. /* draw only the border of a rectangle */
  204. void fill_border(VideoState *s, int x, int y, int w, int h, int color)
  205. {
  206. int w1, w2, h1, h2;
  207. /* fill the background */
  208. w1 = x;
  209. if (w1 < 0)
  210. w1 = 0;
  211. w2 = s->width - (x + w);
  212. if (w2 < 0)
  213. w2 = 0;
  214. h1 = y;
  215. if (h1 < 0)
  216. h1 = 0;
  217. h2 = s->height - (y + h);
  218. if (h2 < 0)
  219. h2 = 0;
  220. fill_rectangle(screen,
  221. s->xleft, s->ytop,
  222. w1, s->height,
  223. color);
  224. fill_rectangle(screen,
  225. s->xleft + s->width - w2, s->ytop,
  226. w2, s->height,
  227. color);
  228. fill_rectangle(screen,
  229. s->xleft + w1, s->ytop,
  230. s->width - w1 - w2, h1,
  231. color);
  232. fill_rectangle(screen,
  233. s->xleft + w1, s->ytop + s->height - h2,
  234. s->width - w1 - w2, h2,
  235. color);
  236. }
  237. #endif
  238. static void video_image_display(VideoState *is)
  239. {
  240. VideoPicture *vp;
  241. float aspect_ratio;
  242. int width, height, x, y;
  243. SDL_Rect rect;
  244. vp = &is->pictq[is->pictq_rindex];
  245. if (vp->bmp) {
  246. /* XXX: use variable in the frame */
  247. aspect_ratio = is->video_st->codec.aspect_ratio;
  248. if (aspect_ratio <= 0.0)
  249. aspect_ratio = (float)is->video_st->codec.width /
  250. (float)is->video_st->codec.height;
  251. /* if an active format is indicated, then it overrides the
  252. mpeg format */
  253. #if 0
  254. if (is->video_st->codec.dtg_active_format != is->dtg_active_format) {
  255. is->dtg_active_format = is->video_st->codec.dtg_active_format;
  256. printf("dtg_active_format=%d\n", is->dtg_active_format);
  257. }
  258. #endif
  259. #if 0
  260. switch(is->video_st->codec.dtg_active_format) {
  261. case FF_DTG_AFD_SAME:
  262. default:
  263. /* nothing to do */
  264. break;
  265. case FF_DTG_AFD_4_3:
  266. aspect_ratio = 4.0 / 3.0;
  267. break;
  268. case FF_DTG_AFD_16_9:
  269. aspect_ratio = 16.0 / 9.0;
  270. break;
  271. case FF_DTG_AFD_14_9:
  272. aspect_ratio = 14.0 / 9.0;
  273. break;
  274. case FF_DTG_AFD_4_3_SP_14_9:
  275. aspect_ratio = 14.0 / 9.0;
  276. break;
  277. case FF_DTG_AFD_16_9_SP_14_9:
  278. aspect_ratio = 14.0 / 9.0;
  279. break;
  280. case FF_DTG_AFD_SP_4_3:
  281. aspect_ratio = 4.0 / 3.0;
  282. break;
  283. }
  284. #endif
  285. /* XXX: we suppose the screen has a 1.0 pixel ratio */
  286. height = is->height;
  287. width = ((int)rint(height * aspect_ratio)) & -3;
  288. if (width > is->width) {
  289. width = is->width;
  290. height = ((int)rint(width / aspect_ratio)) & -3;
  291. }
  292. x = (is->width - width) / 2;
  293. y = (is->height - height) / 2;
  294. if (!is->no_background) {
  295. /* fill the background */
  296. // fill_border(is, x, y, width, height, QERGB(0x00, 0x00, 0x00));
  297. } else {
  298. is->no_background = 0;
  299. }
  300. rect.x = is->xleft + x;
  301. rect.y = is->xleft + y;
  302. rect.w = width;
  303. rect.h = height;
  304. SDL_DisplayYUVOverlay(vp->bmp, &rect);
  305. } else {
  306. #if 0
  307. fill_rectangle(screen,
  308. is->xleft, is->ytop, is->width, is->height,
  309. QERGB(0x00, 0x00, 0x00));
  310. #endif
  311. }
  312. }
  313. static inline int compute_mod(int a, int b)
  314. {
  315. a = a % b;
  316. if (a >= 0)
  317. return a;
  318. else
  319. return a + b;
  320. }
  321. static void video_audio_display(VideoState *s)
  322. {
  323. int i, i_start, x, y1, y, ys, delay, n, nb_display_channels;
  324. int ch, channels, h, h2, bgcolor, fgcolor;
  325. int16_t time_diff;
  326. /* compute display index : center on currently output samples */
  327. channels = s->audio_st->codec.channels;
  328. nb_display_channels = channels;
  329. if (!s->paused) {
  330. n = 2 * channels;
  331. delay = audio_write_get_buf_size(s);
  332. delay /= n;
  333. /* to be more precise, we take into account the time spent since
  334. the last buffer computation */
  335. if (audio_callback_time) {
  336. time_diff = av_gettime() - audio_callback_time;
  337. delay += (time_diff * s->audio_st->codec.sample_rate) / 1000000;
  338. }
  339. delay -= s->width / 2;
  340. if (delay < s->width)
  341. delay = s->width;
  342. i_start = compute_mod(s->sample_array_index - delay * channels, SAMPLE_ARRAY_SIZE);
  343. s->last_i_start = i_start;
  344. } else {
  345. i_start = s->last_i_start;
  346. }
  347. bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
  348. fill_rectangle(screen,
  349. s->xleft, s->ytop, s->width, s->height,
  350. bgcolor);
  351. fgcolor = SDL_MapRGB(screen->format, 0xff, 0xff, 0xff);
  352. /* total height for one channel */
  353. h = s->height / nb_display_channels;
  354. /* graph height / 2 */
  355. h2 = (h * 9) / 20;
  356. for(ch = 0;ch < nb_display_channels; ch++) {
  357. i = i_start + ch;
  358. y1 = s->ytop + ch * h + (h / 2); /* position of center line */
  359. for(x = 0; x < s->width; x++) {
  360. y = (s->sample_array[i] * h2) >> 15;
  361. if (y < 0) {
  362. y = -y;
  363. ys = y1 - y;
  364. } else {
  365. ys = y1;
  366. }
  367. fill_rectangle(screen,
  368. s->xleft + x, ys, 1, y,
  369. fgcolor);
  370. i += channels;
  371. if (i >= SAMPLE_ARRAY_SIZE)
  372. i -= SAMPLE_ARRAY_SIZE;
  373. }
  374. }
  375. fgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0xff);
  376. for(ch = 1;ch < nb_display_channels; ch++) {
  377. y = s->ytop + ch * h;
  378. fill_rectangle(screen,
  379. s->xleft, y, s->width, 1,
  380. fgcolor);
  381. }
  382. SDL_UpdateRect(screen, s->xleft, s->ytop, s->width, s->height);
  383. }
  384. /* display the current picture, if any */
  385. static void video_display(VideoState *is)
  386. {
  387. if (is->audio_st && is->show_audio)
  388. video_audio_display(is);
  389. else if (is->video_st)
  390. video_image_display(is);
  391. }
  392. static Uint32 sdl_refresh_timer_cb(Uint32 interval, void *opaque)
  393. {
  394. SDL_Event event;
  395. event.type = FF_REFRESH_EVENT;
  396. event.user.data1 = opaque;
  397. SDL_PushEvent(&event);
  398. return 0; /* 0 means stop timer */
  399. }
  400. /* schedule a video refresh in 'delay' ms */
  401. static void schedule_refresh(VideoState *is, int delay)
  402. {
  403. SDL_AddTimer(delay, sdl_refresh_timer_cb, is);
  404. }
  405. /* called to display each frame */
  406. static void video_refresh_timer(void *opaque)
  407. {
  408. VideoState *is = opaque;
  409. VideoPicture *vp;
  410. if (is->video_st) {
  411. if (is->pictq_size == 0) {
  412. /* if no picture, need to wait */
  413. schedule_refresh(is, 40);
  414. } else {
  415. vp = &is->pictq[is->pictq_rindex];
  416. /* launch timer for next picture */
  417. schedule_refresh(is, vp->delay);
  418. /* display picture */
  419. video_display(is);
  420. /* update queue size and signal for next picture */
  421. if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE)
  422. is->pictq_rindex = 0;
  423. SDL_LockMutex(is->pictq_mutex);
  424. is->pictq_size--;
  425. SDL_CondSignal(is->pictq_cond);
  426. SDL_UnlockMutex(is->pictq_mutex);
  427. }
  428. } else if (is->audio_st) {
  429. /* draw the next audio frame */
  430. schedule_refresh(is, 40);
  431. /* if only audio stream, then display the audio bars (better
  432. than nothing, just to test the implementation */
  433. /* display picture */
  434. video_display(is);
  435. } else {
  436. schedule_refresh(is, 100);
  437. }
  438. if (show_status) {
  439. static int64_t last_time;
  440. int64_t cur_time;
  441. int aqsize, vqsize;
  442. cur_time = av_gettime();
  443. if (!last_time || (cur_time - last_time) >= 500 * 1000) {
  444. aqsize = 0;
  445. vqsize = 0;
  446. if (is->audio_st)
  447. aqsize = is->audioq.size;
  448. if (is->video_st)
  449. vqsize = is->videoq.size;
  450. printf("A:%7.2f V:%7.2f aq=%5dKB vq=%5dKB \r",
  451. is->audio_clock, is->video_clock, aqsize / 1024, vqsize / 1024);
  452. fflush(stdout);
  453. last_time = cur_time;
  454. }
  455. }
  456. }
  457. /* allocate a picture (needs to do that in main thread to avoid
  458. potential locking problems */
  459. static void alloc_picture(void *opaque)
  460. {
  461. VideoState *is = opaque;
  462. VideoPicture *vp;
  463. int is_yuv;
  464. vp = &is->pictq[is->pictq_windex];
  465. if (vp->bmp)
  466. SDL_FreeYUVOverlay(vp->bmp);
  467. /* XXX: use generic function */
  468. switch(is->video_st->codec.pix_fmt) {
  469. case PIX_FMT_YUV420P:
  470. case PIX_FMT_YUV422P:
  471. case PIX_FMT_YUV444P:
  472. case PIX_FMT_YUV422:
  473. case PIX_FMT_YUV410P:
  474. case PIX_FMT_YUV411P:
  475. is_yuv = 1;
  476. break;
  477. default:
  478. is_yuv = 0;
  479. break;
  480. }
  481. if (is_yuv) {
  482. vp->bmp = SDL_CreateYUVOverlay(is->video_st->codec.width,
  483. is->video_st->codec.height,
  484. SDL_YV12_OVERLAY,
  485. screen);
  486. } else {
  487. #if 0
  488. vp->bmp = bmp_alloc(screen,
  489. is->video_st->codec.width,
  490. is->video_st->codec.height,
  491. screen->bitmap_format,
  492. 0);
  493. #endif
  494. vp->bmp = NULL;
  495. }
  496. vp->width = is->video_st->codec.width;
  497. vp->height = is->video_st->codec.height;
  498. SDL_LockMutex(is->pictq_mutex);
  499. vp->allocated = 1;
  500. SDL_CondSignal(is->pictq_cond);
  501. SDL_UnlockMutex(is->pictq_mutex);
  502. }
  503. #define VIDEO_CORRECTION_THRESHOLD 0.2
  504. static int output_picture(VideoState *is, AVPicture *src_pict, double pts)
  505. {
  506. VideoPicture *vp;
  507. int dst_pix_fmt;
  508. AVPicture pict;
  509. double delay, ref_clock, diff;
  510. /* wait until we have space to put a new picture */
  511. SDL_LockMutex(is->pictq_mutex);
  512. while (is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE &&
  513. !is->videoq.abort_request) {
  514. SDL_CondWait(is->pictq_cond, is->pictq_mutex);
  515. }
  516. SDL_UnlockMutex(is->pictq_mutex);
  517. if (is->videoq.abort_request)
  518. return -1;
  519. vp = &is->pictq[is->pictq_windex];
  520. /* alloc or resize hardware picture buffer */
  521. if (!vp->bmp ||
  522. vp->width != is->video_st->codec.width ||
  523. vp->height != is->video_st->codec.height) {
  524. SDL_Event event;
  525. vp->allocated = 0;
  526. /* the allocation must be done in the main thread to avoid
  527. locking problems */
  528. event.type = FF_ALLOC_EVENT;
  529. event.user.data1 = is;
  530. SDL_PushEvent(&event);
  531. /* wait until the picture is allocated */
  532. SDL_LockMutex(is->pictq_mutex);
  533. while (!vp->allocated && !is->videoq.abort_request) {
  534. SDL_CondWait(is->pictq_cond, is->pictq_mutex);
  535. }
  536. SDL_UnlockMutex(is->pictq_mutex);
  537. if (is->videoq.abort_request)
  538. return -1;
  539. }
  540. if (vp->bmp) {
  541. /* get a pointer on the bitmap */
  542. SDL_LockYUVOverlay (vp->bmp);
  543. dst_pix_fmt = PIX_FMT_YUV420P;
  544. pict.data[0] = vp->bmp->pixels[0];
  545. pict.data[1] = vp->bmp->pixels[2];
  546. pict.data[2] = vp->bmp->pixels[1];
  547. pict.linesize[0] = vp->bmp->pitches[0];
  548. pict.linesize[1] = vp->bmp->pitches[2];
  549. pict.linesize[2] = vp->bmp->pitches[1];
  550. img_convert(&pict, dst_pix_fmt,
  551. src_pict, is->video_st->codec.pix_fmt,
  552. is->video_st->codec.width, is->video_st->codec.height);
  553. /* update the bitmap content */
  554. SDL_UnlockYUVOverlay(vp->bmp);
  555. /* compute delay for the next frame and take into account the
  556. pts if needed to make a correction. Since we do not support
  557. correct MPEG B frame PTS, we put a high threshold */
  558. if (is->av_sync_type == AV_SYNC_VIDEO_MASTER) {
  559. ref_clock = is->video_clock;
  560. } else if (is->av_sync_type == AV_SYNC_AUDIO_MASTER) {
  561. /* cannot use audio master if no audio, so fall back to no sync */
  562. if (!is->audio_st)
  563. ref_clock = is->video_clock;
  564. else
  565. ref_clock = is->audio_clock;
  566. } else {
  567. ref_clock = is->external_clock;
  568. }
  569. diff = is->video_clock - ref_clock;
  570. delay = (double)is->video_st->codec.frame_rate_base /
  571. (double)is->video_st->codec.frame_rate;
  572. if (fabs(diff) > VIDEO_CORRECTION_THRESHOLD) {
  573. /* if too big difference, then we adjust */
  574. delay += diff;
  575. /* compute the difference */
  576. if (delay < 0.01)
  577. delay = 0.01;
  578. else if (delay > 1.0)
  579. delay = 1.0;
  580. }
  581. vp->delay = (int)(delay * 1000 + 0.5);
  582. /* now we can update the picture count */
  583. if (++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE)
  584. is->pictq_windex = 0;
  585. SDL_LockMutex(is->pictq_mutex);
  586. is->pictq_size++;
  587. SDL_UnlockMutex(is->pictq_mutex);
  588. }
  589. /* update video clock */
  590. if (pts != 0) {
  591. is->video_clock = pts;
  592. } else {
  593. is->video_clock += (double)is->video_st->codec.frame_rate_base /
  594. (double)is->video_st->codec.frame_rate;
  595. }
  596. return 0;
  597. }
  598. static int video_thread(void *arg)
  599. {
  600. VideoState *is = arg;
  601. AVPacket pkt1, *pkt = &pkt1;
  602. unsigned char *ptr;
  603. int len, len1, got_picture, i;
  604. AVFrame frame;
  605. AVPicture pict;
  606. int64_t ipts;
  607. double pts;
  608. for(;;) {
  609. while (is->paused && !is->videoq.abort_request) {
  610. SDL_Delay(10);
  611. }
  612. if (packet_queue_get(&is->videoq, pkt, 1) < 0)
  613. break;
  614. ipts = pkt->pts;
  615. ptr = pkt->data;
  616. if (is->video_st->codec.codec_id == CODEC_ID_RAWVIDEO) {
  617. avpicture_fill(&pict, ptr,
  618. is->video_st->codec.pix_fmt,
  619. is->video_st->codec.width,
  620. is->video_st->codec.height);
  621. pts = 0;
  622. if (ipts != AV_NOPTS_VALUE)
  623. pts = (double)ipts * is->ic->pts_num / is->ic->pts_den;
  624. if (output_picture(is, &pict, pts) < 0)
  625. goto the_end;
  626. } else {
  627. len = pkt->size;
  628. while (len > 0) {
  629. len1 = avcodec_decode_video(&is->video_st->codec,
  630. &frame, &got_picture, ptr, len);
  631. if (len1 < 0)
  632. break;
  633. if (got_picture) {
  634. for(i=0;i<4;i++) {
  635. pict.data[i] = frame.data[i];
  636. pict.linesize[i] = frame.linesize[i];
  637. }
  638. pts = 0;
  639. if (ipts != AV_NOPTS_VALUE)
  640. pts = (double)ipts * is->ic->pts_num / is->ic->pts_den;
  641. ipts = AV_NOPTS_VALUE;
  642. if (output_picture(is, &pict, pts) < 0)
  643. goto the_end;
  644. }
  645. ptr += len1;
  646. len -= len1;
  647. }
  648. }
  649. av_free_packet(pkt);
  650. }
  651. the_end:
  652. return 0;
  653. }
  654. /* copy samples for viewing in editor window */
  655. static void update_sample_display(VideoState *is, short *samples, int samples_size)
  656. {
  657. int size, len, channels;
  658. channels = is->audio_st->codec.channels;
  659. size = samples_size / sizeof(short);
  660. while (size > 0) {
  661. len = SAMPLE_ARRAY_SIZE - is->sample_array_index;
  662. if (len > size)
  663. len = size;
  664. memcpy(is->sample_array + is->sample_array_index, samples, len * sizeof(short));
  665. samples += len;
  666. is->sample_array_index += len;
  667. if (is->sample_array_index >= SAMPLE_ARRAY_SIZE)
  668. is->sample_array_index = 0;
  669. size -= len;
  670. }
  671. }
  672. /* maximum audio speed change to get correct sync */
  673. #define SAMPLE_CORRECTION_PERCENT_MAX 2
  674. /* return the new audio buffer size (samples can be added or deleted
  675. to get better sync if video or external master clock) */
  676. static int synchronize_audio(VideoState *is, short *samples,
  677. int samples_size, double pts)
  678. {
  679. int n, delay;
  680. double ref_clock;
  681. n = 2 * is->audio_st->codec.channels;
  682. if (is->av_sync_type == AV_SYNC_EXTERNAL_CLOCK)
  683. ref_clock = is->external_clock;
  684. else if (is->av_sync_type == AV_SYNC_VIDEO_MASTER && is->video_st)
  685. ref_clock = is->video_clock;
  686. else
  687. ref_clock = is->audio_clock;
  688. /* if not master, then we try to remove or add samples to correct the clock */
  689. if (((is->av_sync_type == AV_SYNC_VIDEO_MASTER && is->video_st) ||
  690. is->av_sync_type == AV_SYNC_EXTERNAL_CLOCK) && pts != 0) {
  691. double diff;
  692. int wanted_size, min_size, max_size, nb_samples;
  693. delay = audio_write_get_buf_size(is);
  694. diff = pts - (double)delay / (double)(n * is->audio_st->codec.sample_rate) - ref_clock;
  695. wanted_size = (int)(diff * is->audio_st->codec.sample_rate) * n;
  696. nb_samples = samples_size / n;
  697. min_size = ((nb_samples * (100 - SAMPLE_CORRECTION_PERCENT_MAX)) / 100) * n;
  698. max_size = ((nb_samples * (100 + SAMPLE_CORRECTION_PERCENT_MAX)) / 100) * n;
  699. if (wanted_size < min_size)
  700. wanted_size = min_size;
  701. else if (wanted_size > max_size)
  702. wanted_size = max_size;
  703. /* do the correct */
  704. /* XXX: do it better with sample interpolation */
  705. if (wanted_size < samples_size) {
  706. /* remove samples */
  707. samples_size = wanted_size;
  708. } else if (wanted_size > samples_size) {
  709. uint8_t *samples_end, *q;
  710. int nb;
  711. /* add samples */
  712. nb = (samples_size - wanted_size);
  713. samples_end = (uint8_t *)samples + samples_size - n;
  714. q = samples_end + n;
  715. while (nb > 0) {
  716. memcpy(q, samples_end, n);
  717. q += n;
  718. nb -= n;
  719. }
  720. samples_size = wanted_size;
  721. }
  722. }
  723. /* update audio clock */
  724. if (is->av_sync_type == AV_SYNC_AUDIO_MASTER && pts != 0) {
  725. /* a pts is given: we update the audio clock precisely */
  726. delay = audio_write_get_buf_size(is);
  727. is->audio_clock = pts - (double)delay / (double)(n * is->audio_st->codec.sample_rate);
  728. } else {
  729. is->audio_clock += (double)samples_size / (double)(n * is->audio_st->codec.sample_rate);
  730. }
  731. return samples_size;
  732. }
  733. /* decode one audio frame and returns its uncompressed size */
  734. static int audio_decode_frame(VideoState *is, uint8_t *audio_buf, double *pts_ptr)
  735. {
  736. AVPacket *pkt = &is->audio_pkt;
  737. int len1, data_size;
  738. double pts;
  739. for(;;) {
  740. if (is->paused || is->audioq.abort_request) {
  741. return -1;
  742. }
  743. while (is->audio_pkt_size > 0) {
  744. len1 = avcodec_decode_audio(&is->audio_st->codec,
  745. (int16_t *)audio_buf, &data_size,
  746. is->audio_pkt_data, is->audio_pkt_size);
  747. if (len1 < 0)
  748. break;
  749. is->audio_pkt_data += len1;
  750. is->audio_pkt_size -= len1;
  751. if (data_size > 0) {
  752. pts = 0;
  753. if (is->audio_pkt_ipts != AV_NOPTS_VALUE)
  754. pts = (double)is->audio_pkt_ipts * is->ic->pts_num / is->ic->pts_den;
  755. *pts_ptr = pts;
  756. is->audio_pkt_ipts = AV_NOPTS_VALUE;
  757. /* we got samples : we can exit now */
  758. return data_size;
  759. }
  760. }
  761. /* free previous packet if any */
  762. if (pkt->destruct)
  763. av_free_packet(pkt);
  764. /* read next packet */
  765. if (packet_queue_get(&is->audioq, pkt, 1) < 0)
  766. return -1;
  767. is->audio_pkt_data = pkt->data;
  768. is->audio_pkt_size = pkt->size;
  769. is->audio_pkt_ipts = pkt->pts;
  770. }
  771. }
  772. int audio_write_get_buf_size(VideoState *is)
  773. {
  774. int delay;
  775. delay = is->audio_hw_buf_size;
  776. #if 0
  777. /* just a test to check if the estimated delay is OK */
  778. {
  779. int val;
  780. if (ioctl(sdl_audio_fd, SNDCTL_DSP_GETODELAY, &val) < 0)
  781. perror("SNDCTL_DSP_GETODELAY");
  782. printf("real_delay=%d delay=%d\n", val, delay);
  783. }
  784. #endif
  785. return delay;
  786. }
  787. /* prepare a new audio buffer */
  788. void sdl_audio_callback(void *opaque, Uint8 *stream, int len)
  789. {
  790. VideoState *is = opaque;
  791. int audio_size, len1;
  792. double pts;
  793. audio_callback_time = av_gettime();
  794. while (len > 0) {
  795. if (is->audio_buf_index >= is->audio_buf_size) {
  796. audio_size = audio_decode_frame(is, is->audio_buf, &pts);
  797. if (audio_size < 0) {
  798. /* if error, just output silence */
  799. is->audio_buf_size = 1024;
  800. memset(is->audio_buf, 0, is->audio_buf_size);
  801. } else {
  802. if (is->show_audio)
  803. update_sample_display(is, (int16_t *)is->audio_buf, audio_size);
  804. audio_size = synchronize_audio(is, (int16_t *)is->audio_buf, audio_size,
  805. pts);
  806. is->audio_buf_size = audio_size;
  807. }
  808. is->audio_buf_index = 0;
  809. }
  810. len1 = is->audio_buf_size - is->audio_buf_index;
  811. if (len1 > len)
  812. len1 = len;
  813. memcpy(stream, (uint8_t *)is->audio_buf + is->audio_buf_index, len1);
  814. len -= len1;
  815. stream += len1;
  816. is->audio_buf_index += len1;
  817. }
  818. }
  819. /* open a given stream. Return 0 if OK */
  820. static int stream_component_open(VideoState *is, int stream_index)
  821. {
  822. AVFormatContext *ic = is->ic;
  823. AVCodecContext *enc;
  824. AVCodec *codec;
  825. SDL_AudioSpec wanted_spec, spec;
  826. if (stream_index < 0 || stream_index >= ic->nb_streams)
  827. return -1;
  828. enc = &ic->streams[stream_index]->codec;
  829. /* prepare audio output */
  830. if (enc->codec_type == CODEC_TYPE_AUDIO) {
  831. wanted_spec.freq = enc->sample_rate;
  832. wanted_spec.format = AUDIO_S16SYS;
  833. wanted_spec.channels = enc->channels;
  834. wanted_spec.silence = 0;
  835. wanted_spec.samples = 8192;
  836. wanted_spec.callback = sdl_audio_callback;
  837. wanted_spec.userdata = is;
  838. if (SDL_OpenAudio(&wanted_spec, &spec) < 0)
  839. return -1;
  840. is->audio_hw_buf_size = spec.size;
  841. }
  842. codec = avcodec_find_decoder(enc->codec_id);
  843. if (!codec ||
  844. avcodec_open(enc, codec) < 0)
  845. return -1;
  846. switch(enc->codec_type) {
  847. case CODEC_TYPE_AUDIO:
  848. is->audio_stream = stream_index;
  849. is->audio_st = ic->streams[stream_index];
  850. is->audio_buf_size = 0;
  851. is->audio_buf_index = 0;
  852. is->audio_pkt_size = 0;
  853. memset(&is->audio_pkt, 0, sizeof(is->audio_pkt));
  854. packet_queue_init(&is->audioq);
  855. SDL_PauseAudio(0);
  856. break;
  857. case CODEC_TYPE_VIDEO:
  858. is->video_stream = stream_index;
  859. is->video_st = ic->streams[stream_index];
  860. packet_queue_init(&is->videoq);
  861. is->video_tid = SDL_CreateThread(video_thread, is);
  862. break;
  863. default:
  864. break;
  865. }
  866. return 0;
  867. }
  868. static void stream_component_close(VideoState *is, int stream_index)
  869. {
  870. AVFormatContext *ic = is->ic;
  871. AVCodecContext *enc;
  872. enc = &ic->streams[stream_index]->codec;
  873. switch(enc->codec_type) {
  874. case CODEC_TYPE_AUDIO:
  875. packet_queue_abort(&is->audioq);
  876. SDL_CloseAudio();
  877. packet_queue_end(&is->audioq);
  878. break;
  879. case CODEC_TYPE_VIDEO:
  880. packet_queue_abort(&is->videoq);
  881. /* note: we also signal this mutex to make sure we deblock the
  882. video thread in all cases */
  883. SDL_LockMutex(is->pictq_mutex);
  884. SDL_CondSignal(is->pictq_cond);
  885. SDL_UnlockMutex(is->pictq_mutex);
  886. SDL_WaitThread(is->video_tid, NULL);
  887. packet_queue_end(&is->videoq);
  888. break;
  889. default:
  890. break;
  891. }
  892. avcodec_close(enc);
  893. switch(enc->codec_type) {
  894. case CODEC_TYPE_AUDIO:
  895. is->audio_st = NULL;
  896. is->audio_stream = -1;
  897. break;
  898. case CODEC_TYPE_VIDEO:
  899. is->video_st = NULL;
  900. is->video_stream = -1;
  901. break;
  902. default:
  903. break;
  904. }
  905. }
  906. /* this thread gets the stream from the disk or the network */
  907. static int decode_thread(void *arg)
  908. {
  909. VideoState *is = arg;
  910. AVFormatContext *ic;
  911. int err, i, ret, video_index, audio_index;
  912. AVPacket pkt1, *pkt = &pkt1;
  913. video_index = -1;
  914. audio_index = -1;
  915. is->video_stream = -1;
  916. is->audio_stream = -1;
  917. err = av_open_input_file(&ic, is->filename, NULL, 0, NULL);
  918. if (err < 0)
  919. return 0;
  920. is->ic = ic;
  921. err = av_find_stream_info(ic);
  922. if (err < 0)
  923. goto fail;
  924. for(i = 0; i < ic->nb_streams; i++) {
  925. AVCodecContext *enc = &ic->streams[i]->codec;
  926. switch(enc->codec_type) {
  927. case CODEC_TYPE_AUDIO:
  928. if (audio_index < 0 && !audio_disable)
  929. audio_index = i;
  930. break;
  931. case CODEC_TYPE_VIDEO:
  932. if (video_index < 0 && !video_disable)
  933. video_index = i;
  934. break;
  935. default:
  936. break;
  937. }
  938. }
  939. if (show_status) {
  940. dump_format(ic, 0, is->filename, 0);
  941. }
  942. /* open the streams */
  943. if (audio_index >= 0) {
  944. stream_component_open(is, audio_index);
  945. }
  946. if (video_index >= 0) {
  947. stream_component_open(is, video_index);
  948. } else {
  949. if (!display_disable)
  950. is->show_audio = 1;
  951. }
  952. if (is->video_stream < 0 && is->audio_stream < 0) {
  953. goto fail;
  954. }
  955. for(;;) {
  956. if (is->abort_request)
  957. break;
  958. /* if the queue are full, no need to read more */
  959. if (is->audioq.size > MAX_AUDIOQ_SIZE ||
  960. is->videoq.size > MAX_VIDEOQ_SIZE) {
  961. /* wait 10 ms */
  962. SDL_Delay(10);
  963. continue;
  964. }
  965. ret = av_read_packet(ic, pkt);
  966. if (ret < 0) {
  967. break;
  968. }
  969. if (pkt->stream_index == is->audio_stream) {
  970. packet_queue_put(&is->audioq, pkt);
  971. } else if (pkt->stream_index == is->video_stream) {
  972. packet_queue_put(&is->videoq, pkt);
  973. } else {
  974. av_free_packet(pkt);
  975. }
  976. }
  977. /* wait until the end */
  978. while (!is->abort_request) {
  979. SDL_Delay(100);
  980. }
  981. fail:
  982. /* close each stream */
  983. if (is->audio_stream >= 0)
  984. stream_component_close(is, is->audio_stream);
  985. if (is->video_stream >= 0)
  986. stream_component_close(is, is->video_stream);
  987. av_close_input_file(is->ic);
  988. is->ic = NULL; /* safety */
  989. return 0;
  990. }
  991. /* pause or resume the video */
  992. static void stream_pause(VideoState *is)
  993. {
  994. is->paused = !is->paused;
  995. }
  996. static VideoState *stream_open(const char *filename)
  997. {
  998. VideoState *is;
  999. is = av_mallocz(sizeof(VideoState));
  1000. if (!is)
  1001. return NULL;
  1002. pstrcpy(is->filename, sizeof(is->filename), filename);
  1003. if (screen) {
  1004. is->width = screen->w;
  1005. is->height = screen->h;
  1006. }
  1007. is->ytop = 0;
  1008. is->xleft = 0;
  1009. /* start video display */
  1010. is->pictq_mutex = SDL_CreateMutex();
  1011. is->pictq_cond = SDL_CreateCond();
  1012. /* add the refresh timer to draw the picture */
  1013. schedule_refresh(is, 40);
  1014. is->av_sync_type = AV_SYNC_AUDIO_MASTER;
  1015. is->parse_tid = SDL_CreateThread(decode_thread, is);
  1016. if (!is->parse_tid) {
  1017. av_free(is);
  1018. return NULL;
  1019. }
  1020. return is;
  1021. }
  1022. static void stream_close(VideoState *is)
  1023. {
  1024. VideoPicture *vp;
  1025. int i;
  1026. /* XXX: use a special url_shutdown call to abort parse cleanly */
  1027. is->abort_request = 1;
  1028. SDL_WaitThread(is->parse_tid, NULL);
  1029. /* free all pictures */
  1030. for(i=0;i<VIDEO_PICTURE_QUEUE_SIZE; i++) {
  1031. vp = &is->pictq[i];
  1032. if (vp->bmp) {
  1033. SDL_FreeYUVOverlay(vp->bmp);
  1034. vp->bmp = NULL;
  1035. }
  1036. }
  1037. SDL_DestroyMutex(is->pictq_mutex);
  1038. SDL_DestroyCond(is->pictq_cond);
  1039. }
  1040. void toggle_full_screen(void)
  1041. {
  1042. int w, h, flags;
  1043. is_full_screen = !is_full_screen;
  1044. if (!fs_screen_width) {
  1045. /* use default SDL method */
  1046. SDL_WM_ToggleFullScreen(screen);
  1047. } else {
  1048. /* use the recorded resolution */
  1049. flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
  1050. if (is_full_screen) {
  1051. w = fs_screen_width;
  1052. h = fs_screen_height;
  1053. flags |= SDL_FULLSCREEN;
  1054. } else {
  1055. w = screen_width;
  1056. h = screen_height;
  1057. flags |= SDL_RESIZABLE;
  1058. }
  1059. screen = SDL_SetVideoMode(w, h, 0, flags);
  1060. cur_stream->width = w;
  1061. cur_stream->height = h;
  1062. }
  1063. }
  1064. void toggle_pause(void)
  1065. {
  1066. if (cur_stream)
  1067. stream_pause(cur_stream);
  1068. }
  1069. void do_exit(void)
  1070. {
  1071. if (cur_stream) {
  1072. stream_close(cur_stream);
  1073. cur_stream = NULL;
  1074. }
  1075. if (show_status)
  1076. printf("\n");
  1077. SDL_Quit();
  1078. exit(0);
  1079. }
  1080. void toggle_audio_display(void)
  1081. {
  1082. if (cur_stream) {
  1083. cur_stream->show_audio = !cur_stream->show_audio;
  1084. }
  1085. }
  1086. /* handle an event sent by the GUI */
  1087. void event_loop(void)
  1088. {
  1089. SDL_Event event;
  1090. for(;;) {
  1091. SDL_WaitEvent(&event);
  1092. switch(event.type) {
  1093. case SDL_KEYDOWN:
  1094. switch(event.key.keysym.sym) {
  1095. case SDLK_ESCAPE:
  1096. case SDLK_q:
  1097. do_exit();
  1098. break;
  1099. case SDLK_f:
  1100. toggle_full_screen();
  1101. break;
  1102. case SDLK_p:
  1103. case SDLK_SPACE:
  1104. toggle_pause();
  1105. break;
  1106. case SDLK_a:
  1107. toggle_audio_display();
  1108. break;
  1109. default:
  1110. break;
  1111. }
  1112. break;
  1113. case SDL_VIDEORESIZE:
  1114. if (cur_stream) {
  1115. screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 0,
  1116. SDL_HWSURFACE|SDL_RESIZABLE|SDL_ASYNCBLIT|SDL_HWACCEL);
  1117. cur_stream->width = event.resize.w;
  1118. cur_stream->height = event.resize.h;
  1119. }
  1120. break;
  1121. case SDL_QUIT:
  1122. do_exit();
  1123. break;
  1124. case FF_ALLOC_EVENT:
  1125. alloc_picture(event.user.data1);
  1126. break;
  1127. case FF_REFRESH_EVENT:
  1128. video_refresh_timer(event.user.data1);
  1129. break;
  1130. default:
  1131. break;
  1132. }
  1133. }
  1134. }
  1135. void opt_width(const char *arg)
  1136. {
  1137. screen_width = atoi(arg);
  1138. }
  1139. void opt_height(const char *arg)
  1140. {
  1141. screen_height = atoi(arg);
  1142. }
  1143. static void opt_format(const char *arg)
  1144. {
  1145. file_iformat = av_find_input_format(arg);
  1146. if (!file_iformat) {
  1147. fprintf(stderr, "Unknown input format: %s\n", arg);
  1148. exit(1);
  1149. }
  1150. }
  1151. const OptionDef options[] = {
  1152. { "h", 0, {(void*)show_help}, "show help" },
  1153. { "x", HAS_ARG, {(void*)opt_width}, "force displayed width", "width" },
  1154. { "y", HAS_ARG, {(void*)opt_height}, "force displayed height", "height" },
  1155. { "an", OPT_BOOL, {(void*)&audio_disable}, "disable audio" },
  1156. { "vn", OPT_BOOL, {(void*)&video_disable}, "disable video" },
  1157. { "nodisp", OPT_BOOL, {(void*)&display_disable}, "disable graphical display" },
  1158. { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
  1159. { "stats", OPT_BOOL | OPT_EXPERT, {(void*)&show_status}, "show status", "" },
  1160. { NULL, },
  1161. };
  1162. void show_help(void)
  1163. {
  1164. printf("usage: ffplay [options] input_file\n"
  1165. "Simple media player\n");
  1166. printf("\n");
  1167. show_help_options(options);
  1168. printf("\nWhile playing:\n"
  1169. "q, ESC quit\n"
  1170. "f toggle full screen\n"
  1171. "p, SPC pause\n"
  1172. "a show audio waves\n"
  1173. );
  1174. exit(1);
  1175. }
  1176. void parse_arg_file(const char *filename)
  1177. {
  1178. input_filename = filename;
  1179. }
  1180. /* Called from the main */
  1181. int main(int argc, char **argv)
  1182. {
  1183. int flags;
  1184. /* register all codecs, demux and protocols */
  1185. av_register_all();
  1186. parse_options(argc, argv, options);
  1187. if (!input_filename)
  1188. show_help();
  1189. if (display_disable) {
  1190. video_disable = 1;
  1191. }
  1192. flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER | SDL_INIT_EVENTTHREAD;
  1193. if (SDL_Init (flags)) {
  1194. fprintf(stderr, "Could not initialize SDL - exiting\n");
  1195. exit(1);
  1196. }
  1197. if (!display_disable) {
  1198. screen = SDL_SetVideoMode(screen_width, screen_height, 0,
  1199. SDL_HWSURFACE|SDL_RESIZABLE|SDL_ASYNCBLIT|SDL_HWACCEL);
  1200. if (!screen) {
  1201. fprintf(stderr, "SDL: could not set video mode - exiting\n");
  1202. exit(1);
  1203. }
  1204. SDL_WM_SetCaption("FFplay", "FFplay");
  1205. #ifdef HAVE_X11
  1206. /* save the screen resolution... SDL should allow full screen
  1207. by resizing the window */
  1208. {
  1209. Display *dpy;
  1210. dpy = XOpenDisplay(NULL);
  1211. if (dpy) {
  1212. fs_screen_width = DisplayWidth(dpy, DefaultScreen(dpy));
  1213. fs_screen_height = DisplayHeight(dpy, DefaultScreen(dpy));
  1214. XCloseDisplay(dpy);
  1215. }
  1216. }
  1217. #endif
  1218. }
  1219. SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
  1220. SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
  1221. SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
  1222. SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
  1223. cur_stream = stream_open(input_filename);
  1224. event_loop();
  1225. /* never returns */
  1226. return 0;
  1227. }