ffplay.c 99 KB

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