ffplay.c 93 KB

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