ffplay.c 97 KB

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