ffplay.c 97 KB

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