ffplay.c 97 KB

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