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.

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