ffplay.c 121 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551
  1. /*
  2. * Copyright (c) 2003 Fabrice Bellard
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * simple media player based on the FFmpeg libraries
  23. */
  24. #include "config.h"
  25. #include <inttypes.h>
  26. #include <math.h>
  27. #include <limits.h>
  28. #include <signal.h>
  29. #include "libavutil/avstring.h"
  30. #include "libavutil/colorspace.h"
  31. #include "libavutil/mathematics.h"
  32. #include "libavutil/pixdesc.h"
  33. #include "libavutil/imgutils.h"
  34. #include "libavutil/dict.h"
  35. #include "libavutil/parseutils.h"
  36. #include "libavutil/samplefmt.h"
  37. #include "libavutil/avassert.h"
  38. #include "libavutil/time.h"
  39. #include "libavformat/avformat.h"
  40. #include "libavdevice/avdevice.h"
  41. #include "libswscale/swscale.h"
  42. #include "libavutil/opt.h"
  43. #include "libavcodec/avfft.h"
  44. #include "libswresample/swresample.h"
  45. #if CONFIG_AVFILTER
  46. # include "libavfilter/avcodec.h"
  47. # include "libavfilter/avfilter.h"
  48. # include "libavfilter/buffersink.h"
  49. # include "libavfilter/buffersrc.h"
  50. #endif
  51. #include <SDL.h>
  52. #include <SDL_thread.h>
  53. #include "cmdutils.h"
  54. #include <assert.h>
  55. const char program_name[] = "ffplay";
  56. const int program_birth_year = 2003;
  57. #define MAX_QUEUE_SIZE (15 * 1024 * 1024)
  58. #define MIN_FRAMES 5
  59. /* SDL audio buffer size, in samples. Should be small to have precise
  60. A/V sync as SDL does not have hardware buffer fullness info. */
  61. #define SDL_AUDIO_BUFFER_SIZE 1024
  62. /* no AV sync correction is done if below the AV sync threshold */
  63. #define AV_SYNC_THRESHOLD 0.01
  64. /* no AV correction is done if too big error */
  65. #define AV_NOSYNC_THRESHOLD 10.0
  66. /* maximum audio speed change to get correct sync */
  67. #define SAMPLE_CORRECTION_PERCENT_MAX 10
  68. /* external clock speed adjustment constants for realtime sources based on buffer fullness */
  69. #define EXTERNAL_CLOCK_SPEED_MIN 0.900
  70. #define EXTERNAL_CLOCK_SPEED_MAX 1.010
  71. #define EXTERNAL_CLOCK_SPEED_STEP 0.001
  72. /* we use about AUDIO_DIFF_AVG_NB A-V differences to make the average */
  73. #define AUDIO_DIFF_AVG_NB 20
  74. /* polls for possible required screen refresh at least this often, should be less than 1/fps */
  75. #define REFRESH_RATE 0.01
  76. /* NOTE: the size must be big enough to compensate the hardware audio buffersize size */
  77. /* TODO: We assume that a decoded and resampled frame fits into this buffer */
  78. #define SAMPLE_ARRAY_SIZE (8 * 65536)
  79. #define CURSOR_HIDE_DELAY 1000000
  80. static int64_t sws_flags = SWS_BICUBIC;
  81. typedef struct MyAVPacketList {
  82. AVPacket pkt;
  83. struct MyAVPacketList *next;
  84. int serial;
  85. } MyAVPacketList;
  86. typedef struct PacketQueue {
  87. MyAVPacketList *first_pkt, *last_pkt;
  88. int nb_packets;
  89. int size;
  90. int abort_request;
  91. int serial;
  92. SDL_mutex *mutex;
  93. SDL_cond *cond;
  94. } PacketQueue;
  95. #define VIDEO_PICTURE_QUEUE_SIZE 4
  96. #define SUBPICTURE_QUEUE_SIZE 4
  97. typedef struct VideoPicture {
  98. double pts; // presentation timestamp for this picture
  99. int64_t pos; // byte position in file
  100. SDL_Overlay *bmp;
  101. int width, height; /* source height & width */
  102. int allocated;
  103. int reallocate;
  104. int serial;
  105. AVRational sar;
  106. } VideoPicture;
  107. typedef struct SubPicture {
  108. double pts; /* presentation time stamp for this picture */
  109. AVSubtitle sub;
  110. } SubPicture;
  111. typedef struct AudioParams {
  112. int freq;
  113. int channels;
  114. int64_t channel_layout;
  115. enum AVSampleFormat fmt;
  116. } AudioParams;
  117. enum {
  118. AV_SYNC_AUDIO_MASTER, /* default choice */
  119. AV_SYNC_VIDEO_MASTER,
  120. AV_SYNC_EXTERNAL_CLOCK, /* synchronize to an external clock */
  121. };
  122. typedef struct VideoState {
  123. SDL_Thread *read_tid;
  124. SDL_Thread *video_tid;
  125. AVInputFormat *iformat;
  126. int no_background;
  127. int abort_request;
  128. int force_refresh;
  129. int paused;
  130. int last_paused;
  131. int queue_attachments_req;
  132. int seek_req;
  133. int seek_flags;
  134. int64_t seek_pos;
  135. int64_t seek_rel;
  136. int read_pause_return;
  137. AVFormatContext *ic;
  138. int realtime;
  139. int audio_stream;
  140. int av_sync_type;
  141. double external_clock; ///< external clock base
  142. double external_clock_drift; ///< external clock base - time (av_gettime) at which we updated external_clock
  143. int64_t external_clock_time; ///< last reference time
  144. double external_clock_speed; ///< speed of the external clock
  145. double audio_clock;
  146. int audio_clock_serial;
  147. double audio_diff_cum; /* used for AV difference average computation */
  148. double audio_diff_avg_coef;
  149. double audio_diff_threshold;
  150. int audio_diff_avg_count;
  151. AVStream *audio_st;
  152. PacketQueue audioq;
  153. int audio_hw_buf_size;
  154. uint8_t silence_buf[SDL_AUDIO_BUFFER_SIZE];
  155. uint8_t *audio_buf;
  156. uint8_t *audio_buf1;
  157. unsigned int audio_buf_size; /* in bytes */
  158. unsigned int audio_buf1_size;
  159. int audio_buf_index; /* in bytes */
  160. int audio_write_buf_size;
  161. int audio_buf_frames_pending;
  162. AVPacket audio_pkt_temp;
  163. AVPacket audio_pkt;
  164. int audio_pkt_temp_serial;
  165. int audio_last_serial;
  166. struct AudioParams audio_src;
  167. #if CONFIG_AVFILTER
  168. struct AudioParams audio_filter_src;
  169. #endif
  170. struct AudioParams audio_tgt;
  171. struct SwrContext *swr_ctx;
  172. double audio_current_pts;
  173. double audio_current_pts_drift;
  174. int frame_drops_early;
  175. int frame_drops_late;
  176. AVFrame *frame;
  177. enum ShowMode {
  178. SHOW_MODE_NONE = -1, SHOW_MODE_VIDEO = 0, SHOW_MODE_WAVES, SHOW_MODE_RDFT, SHOW_MODE_NB
  179. } show_mode;
  180. int16_t sample_array[SAMPLE_ARRAY_SIZE];
  181. int sample_array_index;
  182. int last_i_start;
  183. RDFTContext *rdft;
  184. int rdft_bits;
  185. FFTSample *rdft_data;
  186. int xpos;
  187. double last_vis_time;
  188. SDL_Thread *subtitle_tid;
  189. int subtitle_stream;
  190. int subtitle_stream_changed;
  191. AVStream *subtitle_st;
  192. PacketQueue subtitleq;
  193. SubPicture subpq[SUBPICTURE_QUEUE_SIZE];
  194. int subpq_size, subpq_rindex, subpq_windex;
  195. SDL_mutex *subpq_mutex;
  196. SDL_cond *subpq_cond;
  197. double frame_timer;
  198. double frame_last_pts;
  199. double frame_last_duration;
  200. double frame_last_dropped_pts;
  201. double frame_last_returned_time;
  202. double frame_last_filter_delay;
  203. int64_t frame_last_dropped_pos;
  204. int frame_last_dropped_serial;
  205. int video_stream;
  206. AVStream *video_st;
  207. PacketQueue videoq;
  208. double video_current_pts; // current displayed pts
  209. double video_current_pts_drift; // video_current_pts - time (av_gettime) at which we updated video_current_pts - used to have running video pts
  210. int64_t video_current_pos; // current displayed file pos
  211. double max_frame_duration; // maximum duration of a frame - above this, we consider the jump a timestamp discontinuity
  212. int video_clock_serial;
  213. VideoPicture pictq[VIDEO_PICTURE_QUEUE_SIZE];
  214. int pictq_size, pictq_rindex, pictq_windex;
  215. SDL_mutex *pictq_mutex;
  216. SDL_cond *pictq_cond;
  217. #if !CONFIG_AVFILTER
  218. struct SwsContext *img_convert_ctx;
  219. #endif
  220. SDL_Rect last_display_rect;
  221. char filename[1024];
  222. int width, height, xleft, ytop;
  223. int step;
  224. #if CONFIG_AVFILTER
  225. AVFilterContext *in_video_filter; // the first filter in the video chain
  226. AVFilterContext *out_video_filter; // the last filter in the video chain
  227. AVFilterContext *in_audio_filter; // the first filter in the audio chain
  228. AVFilterContext *out_audio_filter; // the last filter in the audio chain
  229. AVFilterGraph *agraph; // audio filter graph
  230. #endif
  231. int last_video_stream, last_audio_stream, last_subtitle_stream;
  232. SDL_cond *continue_read_thread;
  233. } VideoState;
  234. /* options specified by the user */
  235. static AVInputFormat *file_iformat;
  236. static const char *input_filename;
  237. static const char *window_title;
  238. static int fs_screen_width;
  239. static int fs_screen_height;
  240. static int default_width = 640;
  241. static int default_height = 480;
  242. static int screen_width = 0;
  243. static int screen_height = 0;
  244. static int audio_disable;
  245. static int video_disable;
  246. static int subtitle_disable;
  247. static int wanted_stream[AVMEDIA_TYPE_NB] = {
  248. [AVMEDIA_TYPE_AUDIO] = -1,
  249. [AVMEDIA_TYPE_VIDEO] = -1,
  250. [AVMEDIA_TYPE_SUBTITLE] = -1,
  251. };
  252. static int seek_by_bytes = -1;
  253. static int display_disable;
  254. static int show_status = 1;
  255. static int av_sync_type = AV_SYNC_AUDIO_MASTER;
  256. static int64_t start_time = AV_NOPTS_VALUE;
  257. static int64_t duration = AV_NOPTS_VALUE;
  258. static int workaround_bugs = 1;
  259. static int fast = 0;
  260. static int genpts = 0;
  261. static int lowres = 0;
  262. static int idct = FF_IDCT_AUTO;
  263. static int error_concealment = 3;
  264. static int decoder_reorder_pts = -1;
  265. static int autoexit;
  266. static int exit_on_keydown;
  267. static int exit_on_mousedown;
  268. static int loop = 1;
  269. static int framedrop = -1;
  270. static int infinite_buffer = -1;
  271. static enum ShowMode show_mode = SHOW_MODE_NONE;
  272. static const char *audio_codec_name;
  273. static const char *subtitle_codec_name;
  274. static const char *video_codec_name;
  275. double rdftspeed = 0.02;
  276. static int64_t cursor_last_shown;
  277. static int cursor_hidden = 0;
  278. #if CONFIG_AVFILTER
  279. static char *vfilters = NULL;
  280. static char *afilters = NULL;
  281. #endif
  282. /* current context */
  283. static int is_full_screen;
  284. static int64_t audio_callback_time;
  285. static AVPacket flush_pkt;
  286. #define FF_ALLOC_EVENT (SDL_USEREVENT)
  287. #define FF_QUIT_EVENT (SDL_USEREVENT + 2)
  288. static SDL_Surface *screen;
  289. static inline
  290. int cmp_audio_fmts(enum AVSampleFormat fmt1, int64_t channel_count1,
  291. enum AVSampleFormat fmt2, int64_t channel_count2)
  292. {
  293. /* If channel count == 1, planar and non-planar formats are the same */
  294. if (channel_count1 == 1 && channel_count2 == 1)
  295. return av_get_packed_sample_fmt(fmt1) != av_get_packed_sample_fmt(fmt2);
  296. else
  297. return channel_count1 != channel_count2 || fmt1 != fmt2;
  298. }
  299. static inline
  300. int64_t get_valid_channel_layout(int64_t channel_layout, int channels)
  301. {
  302. if (channel_layout && av_get_channel_layout_nb_channels(channel_layout) == channels)
  303. return channel_layout;
  304. else
  305. return 0;
  306. }
  307. static int packet_queue_put(PacketQueue *q, AVPacket *pkt);
  308. static int packet_queue_put_private(PacketQueue *q, AVPacket *pkt)
  309. {
  310. MyAVPacketList *pkt1;
  311. if (q->abort_request)
  312. return -1;
  313. pkt1 = av_malloc(sizeof(MyAVPacketList));
  314. if (!pkt1)
  315. return -1;
  316. pkt1->pkt = *pkt;
  317. pkt1->next = NULL;
  318. if (pkt == &flush_pkt)
  319. q->serial++;
  320. pkt1->serial = q->serial;
  321. if (!q->last_pkt)
  322. q->first_pkt = pkt1;
  323. else
  324. q->last_pkt->next = pkt1;
  325. q->last_pkt = pkt1;
  326. q->nb_packets++;
  327. q->size += pkt1->pkt.size + sizeof(*pkt1);
  328. /* XXX: should duplicate packet data in DV case */
  329. SDL_CondSignal(q->cond);
  330. return 0;
  331. }
  332. static int packet_queue_put(PacketQueue *q, AVPacket *pkt)
  333. {
  334. int ret;
  335. /* duplicate the packet */
  336. if (pkt != &flush_pkt && av_dup_packet(pkt) < 0)
  337. return -1;
  338. SDL_LockMutex(q->mutex);
  339. ret = packet_queue_put_private(q, pkt);
  340. SDL_UnlockMutex(q->mutex);
  341. if (pkt != &flush_pkt && ret < 0)
  342. av_free_packet(pkt);
  343. return ret;
  344. }
  345. /* packet queue handling */
  346. static void packet_queue_init(PacketQueue *q)
  347. {
  348. memset(q, 0, sizeof(PacketQueue));
  349. q->mutex = SDL_CreateMutex();
  350. q->cond = SDL_CreateCond();
  351. q->abort_request = 1;
  352. }
  353. static void packet_queue_flush(PacketQueue *q)
  354. {
  355. MyAVPacketList *pkt, *pkt1;
  356. SDL_LockMutex(q->mutex);
  357. for (pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
  358. pkt1 = pkt->next;
  359. av_free_packet(&pkt->pkt);
  360. av_freep(&pkt);
  361. }
  362. q->last_pkt = NULL;
  363. q->first_pkt = NULL;
  364. q->nb_packets = 0;
  365. q->size = 0;
  366. SDL_UnlockMutex(q->mutex);
  367. }
  368. static void packet_queue_destroy(PacketQueue *q)
  369. {
  370. packet_queue_flush(q);
  371. SDL_DestroyMutex(q->mutex);
  372. SDL_DestroyCond(q->cond);
  373. }
  374. static void packet_queue_abort(PacketQueue *q)
  375. {
  376. SDL_LockMutex(q->mutex);
  377. q->abort_request = 1;
  378. SDL_CondSignal(q->cond);
  379. SDL_UnlockMutex(q->mutex);
  380. }
  381. static void packet_queue_start(PacketQueue *q)
  382. {
  383. SDL_LockMutex(q->mutex);
  384. q->abort_request = 0;
  385. packet_queue_put_private(q, &flush_pkt);
  386. SDL_UnlockMutex(q->mutex);
  387. }
  388. /* return < 0 if aborted, 0 if no packet and > 0 if packet. */
  389. static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block, int *serial)
  390. {
  391. MyAVPacketList *pkt1;
  392. int ret;
  393. SDL_LockMutex(q->mutex);
  394. for (;;) {
  395. if (q->abort_request) {
  396. ret = -1;
  397. break;
  398. }
  399. pkt1 = q->first_pkt;
  400. if (pkt1) {
  401. q->first_pkt = pkt1->next;
  402. if (!q->first_pkt)
  403. q->last_pkt = NULL;
  404. q->nb_packets--;
  405. q->size -= pkt1->pkt.size + sizeof(*pkt1);
  406. *pkt = pkt1->pkt;
  407. if (serial)
  408. *serial = pkt1->serial;
  409. av_free(pkt1);
  410. ret = 1;
  411. break;
  412. } else if (!block) {
  413. ret = 0;
  414. break;
  415. } else {
  416. SDL_CondWait(q->cond, q->mutex);
  417. }
  418. }
  419. SDL_UnlockMutex(q->mutex);
  420. return ret;
  421. }
  422. static inline void fill_rectangle(SDL_Surface *screen,
  423. int x, int y, int w, int h, int color, int update)
  424. {
  425. SDL_Rect rect;
  426. rect.x = x;
  427. rect.y = y;
  428. rect.w = w;
  429. rect.h = h;
  430. SDL_FillRect(screen, &rect, color);
  431. if (update && w > 0 && h > 0)
  432. SDL_UpdateRect(screen, x, y, w, h);
  433. }
  434. /* draw only the border of a rectangle */
  435. static void fill_border(int xleft, int ytop, int width, int height, int x, int y, int w, int h, int color, int update)
  436. {
  437. int w1, w2, h1, h2;
  438. /* fill the background */
  439. w1 = x;
  440. if (w1 < 0)
  441. w1 = 0;
  442. w2 = width - (x + w);
  443. if (w2 < 0)
  444. w2 = 0;
  445. h1 = y;
  446. if (h1 < 0)
  447. h1 = 0;
  448. h2 = height - (y + h);
  449. if (h2 < 0)
  450. h2 = 0;
  451. fill_rectangle(screen,
  452. xleft, ytop,
  453. w1, height,
  454. color, update);
  455. fill_rectangle(screen,
  456. xleft + width - w2, ytop,
  457. w2, height,
  458. color, update);
  459. fill_rectangle(screen,
  460. xleft + w1, ytop,
  461. width - w1 - w2, h1,
  462. color, update);
  463. fill_rectangle(screen,
  464. xleft + w1, ytop + height - h2,
  465. width - w1 - w2, h2,
  466. color, update);
  467. }
  468. #define ALPHA_BLEND(a, oldp, newp, s)\
  469. ((((oldp << s) * (255 - (a))) + (newp * (a))) / (255 << s))
  470. #define RGBA_IN(r, g, b, a, s)\
  471. {\
  472. unsigned int v = ((const uint32_t *)(s))[0];\
  473. a = (v >> 24) & 0xff;\
  474. r = (v >> 16) & 0xff;\
  475. g = (v >> 8) & 0xff;\
  476. b = v & 0xff;\
  477. }
  478. #define YUVA_IN(y, u, v, a, s, pal)\
  479. {\
  480. unsigned int val = ((const uint32_t *)(pal))[*(const uint8_t*)(s)];\
  481. a = (val >> 24) & 0xff;\
  482. y = (val >> 16) & 0xff;\
  483. u = (val >> 8) & 0xff;\
  484. v = val & 0xff;\
  485. }
  486. #define YUVA_OUT(d, y, u, v, a)\
  487. {\
  488. ((uint32_t *)(d))[0] = (a << 24) | (y << 16) | (u << 8) | v;\
  489. }
  490. #define BPP 1
  491. static void blend_subrect(AVPicture *dst, const AVSubtitleRect *rect, int imgw, int imgh)
  492. {
  493. int wrap, wrap3, width2, skip2;
  494. int y, u, v, a, u1, v1, a1, w, h;
  495. uint8_t *lum, *cb, *cr;
  496. const uint8_t *p;
  497. const uint32_t *pal;
  498. int dstx, dsty, dstw, dsth;
  499. dstw = av_clip(rect->w, 0, imgw);
  500. dsth = av_clip(rect->h, 0, imgh);
  501. dstx = av_clip(rect->x, 0, imgw - dstw);
  502. dsty = av_clip(rect->y, 0, imgh - dsth);
  503. lum = dst->data[0] + dsty * dst->linesize[0];
  504. cb = dst->data[1] + (dsty >> 1) * dst->linesize[1];
  505. cr = dst->data[2] + (dsty >> 1) * dst->linesize[2];
  506. width2 = ((dstw + 1) >> 1) + (dstx & ~dstw & 1);
  507. skip2 = dstx >> 1;
  508. wrap = dst->linesize[0];
  509. wrap3 = rect->pict.linesize[0];
  510. p = rect->pict.data[0];
  511. pal = (const uint32_t *)rect->pict.data[1]; /* Now in YCrCb! */
  512. if (dsty & 1) {
  513. lum += dstx;
  514. cb += skip2;
  515. cr += skip2;
  516. if (dstx & 1) {
  517. YUVA_IN(y, u, v, a, p, pal);
  518. lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
  519. cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
  520. cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
  521. cb++;
  522. cr++;
  523. lum++;
  524. p += BPP;
  525. }
  526. for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
  527. YUVA_IN(y, u, v, a, p, pal);
  528. u1 = u;
  529. v1 = v;
  530. a1 = a;
  531. lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
  532. YUVA_IN(y, u, v, a, p + BPP, pal);
  533. u1 += u;
  534. v1 += v;
  535. a1 += a;
  536. lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
  537. cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
  538. cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
  539. cb++;
  540. cr++;
  541. p += 2 * BPP;
  542. lum += 2;
  543. }
  544. if (w) {
  545. YUVA_IN(y, u, v, a, p, pal);
  546. lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
  547. cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
  548. cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
  549. p++;
  550. lum++;
  551. }
  552. p += wrap3 - dstw * BPP;
  553. lum += wrap - dstw - dstx;
  554. cb += dst->linesize[1] - width2 - skip2;
  555. cr += dst->linesize[2] - width2 - skip2;
  556. }
  557. for (h = dsth - (dsty & 1); h >= 2; h -= 2) {
  558. lum += dstx;
  559. cb += skip2;
  560. cr += skip2;
  561. if (dstx & 1) {
  562. YUVA_IN(y, u, v, a, p, pal);
  563. u1 = u;
  564. v1 = v;
  565. a1 = a;
  566. lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
  567. p += wrap3;
  568. lum += wrap;
  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. cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
  575. cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
  576. cb++;
  577. cr++;
  578. p += -wrap3 + BPP;
  579. lum += -wrap + 1;
  580. }
  581. for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
  582. YUVA_IN(y, u, v, a, p, pal);
  583. u1 = u;
  584. v1 = v;
  585. a1 = a;
  586. lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
  587. YUVA_IN(y, u, v, a, p + BPP, pal);
  588. u1 += u;
  589. v1 += v;
  590. a1 += a;
  591. lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
  592. p += wrap3;
  593. lum += wrap;
  594. YUVA_IN(y, u, v, a, p, pal);
  595. u1 += u;
  596. v1 += v;
  597. a1 += a;
  598. lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
  599. YUVA_IN(y, u, v, a, p + BPP, pal);
  600. u1 += u;
  601. v1 += v;
  602. a1 += a;
  603. lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
  604. cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 2);
  605. cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 2);
  606. cb++;
  607. cr++;
  608. p += -wrap3 + 2 * BPP;
  609. lum += -wrap + 2;
  610. }
  611. if (w) {
  612. YUVA_IN(y, u, v, a, p, pal);
  613. u1 = u;
  614. v1 = v;
  615. a1 = a;
  616. lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
  617. p += wrap3;
  618. lum += wrap;
  619. YUVA_IN(y, u, v, a, p, pal);
  620. u1 += u;
  621. v1 += v;
  622. a1 += a;
  623. lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
  624. cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u1, 1);
  625. cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v1, 1);
  626. cb++;
  627. cr++;
  628. p += -wrap3 + BPP;
  629. lum += -wrap + 1;
  630. }
  631. p += wrap3 + (wrap3 - dstw * BPP);
  632. lum += wrap + (wrap - dstw - dstx);
  633. cb += dst->linesize[1] - width2 - skip2;
  634. cr += dst->linesize[2] - width2 - skip2;
  635. }
  636. /* handle odd height */
  637. if (h) {
  638. lum += dstx;
  639. cb += skip2;
  640. cr += skip2;
  641. if (dstx & 1) {
  642. YUVA_IN(y, u, v, a, p, pal);
  643. lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
  644. cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
  645. cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
  646. cb++;
  647. cr++;
  648. lum++;
  649. p += BPP;
  650. }
  651. for (w = dstw - (dstx & 1); w >= 2; w -= 2) {
  652. YUVA_IN(y, u, v, a, p, pal);
  653. u1 = u;
  654. v1 = v;
  655. a1 = a;
  656. lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
  657. YUVA_IN(y, u, v, a, p + BPP, pal);
  658. u1 += u;
  659. v1 += v;
  660. a1 += a;
  661. lum[1] = ALPHA_BLEND(a, lum[1], y, 0);
  662. cb[0] = ALPHA_BLEND(a1 >> 2, cb[0], u, 1);
  663. cr[0] = ALPHA_BLEND(a1 >> 2, cr[0], v, 1);
  664. cb++;
  665. cr++;
  666. p += 2 * BPP;
  667. lum += 2;
  668. }
  669. if (w) {
  670. YUVA_IN(y, u, v, a, p, pal);
  671. lum[0] = ALPHA_BLEND(a, lum[0], y, 0);
  672. cb[0] = ALPHA_BLEND(a >> 2, cb[0], u, 0);
  673. cr[0] = ALPHA_BLEND(a >> 2, cr[0], v, 0);
  674. }
  675. }
  676. }
  677. static void free_subpicture(SubPicture *sp)
  678. {
  679. avsubtitle_free(&sp->sub);
  680. }
  681. static void calculate_display_rect(SDL_Rect *rect, int scr_xleft, int scr_ytop, int scr_width, int scr_height, VideoPicture *vp)
  682. {
  683. float aspect_ratio;
  684. int width, height, x, y;
  685. if (vp->sar.num == 0)
  686. aspect_ratio = 0;
  687. else
  688. aspect_ratio = av_q2d(vp->sar);
  689. if (aspect_ratio <= 0.0)
  690. aspect_ratio = 1.0;
  691. aspect_ratio *= (float)vp->width / (float)vp->height;
  692. /* XXX: we suppose the screen has a 1.0 pixel ratio */
  693. height = scr_height;
  694. width = ((int)rint(height * aspect_ratio)) & ~1;
  695. if (width > scr_width) {
  696. width = scr_width;
  697. height = ((int)rint(width / aspect_ratio)) & ~1;
  698. }
  699. x = (scr_width - width) / 2;
  700. y = (scr_height - height) / 2;
  701. rect->x = scr_xleft + x;
  702. rect->y = scr_ytop + y;
  703. rect->w = FFMAX(width, 1);
  704. rect->h = FFMAX(height, 1);
  705. }
  706. static void video_image_display(VideoState *is)
  707. {
  708. VideoPicture *vp;
  709. SubPicture *sp;
  710. AVPicture pict;
  711. SDL_Rect rect;
  712. int i;
  713. vp = &is->pictq[is->pictq_rindex];
  714. if (vp->bmp) {
  715. if (is->subtitle_st) {
  716. if (is->subpq_size > 0) {
  717. sp = &is->subpq[is->subpq_rindex];
  718. if (vp->pts >= sp->pts + ((float) sp->sub.start_display_time / 1000)) {
  719. SDL_LockYUVOverlay (vp->bmp);
  720. pict.data[0] = vp->bmp->pixels[0];
  721. pict.data[1] = vp->bmp->pixels[2];
  722. pict.data[2] = vp->bmp->pixels[1];
  723. pict.linesize[0] = vp->bmp->pitches[0];
  724. pict.linesize[1] = vp->bmp->pitches[2];
  725. pict.linesize[2] = vp->bmp->pitches[1];
  726. for (i = 0; i < sp->sub.num_rects; i++)
  727. blend_subrect(&pict, sp->sub.rects[i],
  728. vp->bmp->w, vp->bmp->h);
  729. SDL_UnlockYUVOverlay (vp->bmp);
  730. }
  731. }
  732. }
  733. calculate_display_rect(&rect, is->xleft, is->ytop, is->width, is->height, vp);
  734. SDL_DisplayYUVOverlay(vp->bmp, &rect);
  735. if (rect.x != is->last_display_rect.x || rect.y != is->last_display_rect.y || rect.w != is->last_display_rect.w || rect.h != is->last_display_rect.h || is->force_refresh) {
  736. int bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
  737. fill_border(is->xleft, is->ytop, is->width, is->height, rect.x, rect.y, rect.w, rect.h, bgcolor, 1);
  738. is->last_display_rect = rect;
  739. }
  740. }
  741. }
  742. static inline int compute_mod(int a, int b)
  743. {
  744. return a < 0 ? a%b + b : a%b;
  745. }
  746. static void video_audio_display(VideoState *s)
  747. {
  748. int i, i_start, x, y1, y, ys, delay, n, nb_display_channels;
  749. int ch, channels, h, h2, bgcolor, fgcolor;
  750. int64_t time_diff;
  751. int rdft_bits, nb_freq;
  752. for (rdft_bits = 1; (1 << rdft_bits) < 2 * s->height; rdft_bits++)
  753. ;
  754. nb_freq = 1 << (rdft_bits - 1);
  755. /* compute display index : center on currently output samples */
  756. channels = s->audio_tgt.channels;
  757. nb_display_channels = channels;
  758. if (!s->paused) {
  759. int data_used= s->show_mode == SHOW_MODE_WAVES ? s->width : (2*nb_freq);
  760. n = 2 * channels;
  761. delay = s->audio_write_buf_size;
  762. delay /= n;
  763. /* to be more precise, we take into account the time spent since
  764. the last buffer computation */
  765. if (audio_callback_time) {
  766. time_diff = av_gettime() - audio_callback_time;
  767. delay -= (time_diff * s->audio_tgt.freq) / 1000000;
  768. }
  769. delay += 2 * data_used;
  770. if (delay < data_used)
  771. delay = data_used;
  772. i_start= x = compute_mod(s->sample_array_index - delay * channels, SAMPLE_ARRAY_SIZE);
  773. if (s->show_mode == SHOW_MODE_WAVES) {
  774. h = INT_MIN;
  775. for (i = 0; i < 1000; i += channels) {
  776. int idx = (SAMPLE_ARRAY_SIZE + x - i) % SAMPLE_ARRAY_SIZE;
  777. int a = s->sample_array[idx];
  778. int b = s->sample_array[(idx + 4 * channels) % SAMPLE_ARRAY_SIZE];
  779. int c = s->sample_array[(idx + 5 * channels) % SAMPLE_ARRAY_SIZE];
  780. int d = s->sample_array[(idx + 9 * channels) % SAMPLE_ARRAY_SIZE];
  781. int score = a - d;
  782. if (h < score && (b ^ c) < 0) {
  783. h = score;
  784. i_start = idx;
  785. }
  786. }
  787. }
  788. s->last_i_start = i_start;
  789. } else {
  790. i_start = s->last_i_start;
  791. }
  792. bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
  793. if (s->show_mode == SHOW_MODE_WAVES) {
  794. fill_rectangle(screen,
  795. s->xleft, s->ytop, s->width, s->height,
  796. bgcolor, 0);
  797. fgcolor = SDL_MapRGB(screen->format, 0xff, 0xff, 0xff);
  798. /* total height for one channel */
  799. h = s->height / nb_display_channels;
  800. /* graph height / 2 */
  801. h2 = (h * 9) / 20;
  802. for (ch = 0; ch < nb_display_channels; ch++) {
  803. i = i_start + ch;
  804. y1 = s->ytop + ch * h + (h / 2); /* position of center line */
  805. for (x = 0; x < s->width; x++) {
  806. y = (s->sample_array[i] * h2) >> 15;
  807. if (y < 0) {
  808. y = -y;
  809. ys = y1 - y;
  810. } else {
  811. ys = y1;
  812. }
  813. fill_rectangle(screen,
  814. s->xleft + x, ys, 1, y,
  815. fgcolor, 0);
  816. i += channels;
  817. if (i >= SAMPLE_ARRAY_SIZE)
  818. i -= SAMPLE_ARRAY_SIZE;
  819. }
  820. }
  821. fgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0xff);
  822. for (ch = 1; ch < nb_display_channels; ch++) {
  823. y = s->ytop + ch * h;
  824. fill_rectangle(screen,
  825. s->xleft, y, s->width, 1,
  826. fgcolor, 0);
  827. }
  828. SDL_UpdateRect(screen, s->xleft, s->ytop, s->width, s->height);
  829. } else {
  830. nb_display_channels= FFMIN(nb_display_channels, 2);
  831. if (rdft_bits != s->rdft_bits) {
  832. av_rdft_end(s->rdft);
  833. av_free(s->rdft_data);
  834. s->rdft = av_rdft_init(rdft_bits, DFT_R2C);
  835. s->rdft_bits = rdft_bits;
  836. s->rdft_data = av_malloc(4 * nb_freq * sizeof(*s->rdft_data));
  837. }
  838. {
  839. FFTSample *data[2];
  840. for (ch = 0; ch < nb_display_channels; ch++) {
  841. data[ch] = s->rdft_data + 2 * nb_freq * ch;
  842. i = i_start + ch;
  843. for (x = 0; x < 2 * nb_freq; x++) {
  844. double w = (x-nb_freq) * (1.0 / nb_freq);
  845. data[ch][x] = s->sample_array[i] * (1.0 - w * w);
  846. i += channels;
  847. if (i >= SAMPLE_ARRAY_SIZE)
  848. i -= SAMPLE_ARRAY_SIZE;
  849. }
  850. av_rdft_calc(s->rdft, data[ch]);
  851. }
  852. // least efficient way to do this, we should of course directly access it but its more than fast enough
  853. for (y = 0; y < s->height; y++) {
  854. double w = 1 / sqrt(nb_freq);
  855. 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]));
  856. int b = (nb_display_channels == 2 ) ? sqrt(w * sqrt(data[1][2 * y + 0] * data[1][2 * y + 0]
  857. + data[1][2 * y + 1] * data[1][2 * y + 1])) : a;
  858. a = FFMIN(a, 255);
  859. b = FFMIN(b, 255);
  860. fgcolor = SDL_MapRGB(screen->format, a, b, (a + b) / 2);
  861. fill_rectangle(screen,
  862. s->xpos, s->height-y, 1, 1,
  863. fgcolor, 0);
  864. }
  865. }
  866. SDL_UpdateRect(screen, s->xpos, s->ytop, 1, s->height);
  867. if (!s->paused)
  868. s->xpos++;
  869. if (s->xpos >= s->width)
  870. s->xpos= s->xleft;
  871. }
  872. }
  873. static void stream_close(VideoState *is)
  874. {
  875. VideoPicture *vp;
  876. int i;
  877. /* XXX: use a special url_shutdown call to abort parse cleanly */
  878. is->abort_request = 1;
  879. SDL_WaitThread(is->read_tid, NULL);
  880. packet_queue_destroy(&is->videoq);
  881. packet_queue_destroy(&is->audioq);
  882. packet_queue_destroy(&is->subtitleq);
  883. /* free all pictures */
  884. for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++) {
  885. vp = &is->pictq[i];
  886. if (vp->bmp) {
  887. SDL_FreeYUVOverlay(vp->bmp);
  888. vp->bmp = NULL;
  889. }
  890. }
  891. SDL_DestroyMutex(is->pictq_mutex);
  892. SDL_DestroyCond(is->pictq_cond);
  893. SDL_DestroyMutex(is->subpq_mutex);
  894. SDL_DestroyCond(is->subpq_cond);
  895. SDL_DestroyCond(is->continue_read_thread);
  896. #if !CONFIG_AVFILTER
  897. sws_freeContext(is->img_convert_ctx);
  898. #endif
  899. av_free(is);
  900. }
  901. static void do_exit(VideoState *is)
  902. {
  903. if (is) {
  904. stream_close(is);
  905. }
  906. av_lockmgr_register(NULL);
  907. uninit_opts();
  908. #if CONFIG_AVFILTER
  909. av_freep(&vfilters);
  910. #endif
  911. avformat_network_deinit();
  912. if (show_status)
  913. printf("\n");
  914. SDL_Quit();
  915. av_log(NULL, AV_LOG_QUIET, "%s", "");
  916. exit(0);
  917. }
  918. static void sigterm_handler(int sig)
  919. {
  920. exit(123);
  921. }
  922. static int video_open(VideoState *is, int force_set_video_mode, VideoPicture *vp)
  923. {
  924. int flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL;
  925. int w,h;
  926. SDL_Rect rect;
  927. if (is_full_screen) flags |= SDL_FULLSCREEN;
  928. else flags |= SDL_RESIZABLE;
  929. if (vp && vp->width) {
  930. calculate_display_rect(&rect, 0, 0, INT_MAX, vp->height, vp);
  931. default_width = rect.w;
  932. default_height = rect.h;
  933. }
  934. if (is_full_screen && fs_screen_width) {
  935. w = fs_screen_width;
  936. h = fs_screen_height;
  937. } else if (!is_full_screen && screen_width) {
  938. w = screen_width;
  939. h = screen_height;
  940. } else {
  941. w = default_width;
  942. h = default_height;
  943. }
  944. if (screen && is->width == screen->w && screen->w == w
  945. && is->height== screen->h && screen->h == h && !force_set_video_mode)
  946. return 0;
  947. screen = SDL_SetVideoMode(w, h, 0, flags);
  948. if (!screen) {
  949. fprintf(stderr, "SDL: could not set video mode - exiting\n");
  950. do_exit(is);
  951. }
  952. if (!window_title)
  953. window_title = input_filename;
  954. SDL_WM_SetCaption(window_title, window_title);
  955. is->width = screen->w;
  956. is->height = screen->h;
  957. return 0;
  958. }
  959. /* display the current picture, if any */
  960. static void video_display(VideoState *is)
  961. {
  962. if (!screen)
  963. video_open(is, 0, NULL);
  964. if (is->audio_st && is->show_mode != SHOW_MODE_VIDEO)
  965. video_audio_display(is);
  966. else if (is->video_st)
  967. video_image_display(is);
  968. }
  969. /* get the current audio clock value */
  970. static double get_audio_clock(VideoState *is)
  971. {
  972. if (is->audio_clock_serial != is->audioq.serial)
  973. return NAN;
  974. if (is->paused) {
  975. return is->audio_current_pts;
  976. } else {
  977. return is->audio_current_pts_drift + av_gettime() / 1000000.0;
  978. }
  979. }
  980. /* get the current video clock value */
  981. static double get_video_clock(VideoState *is)
  982. {
  983. if (is->video_clock_serial != is->videoq.serial)
  984. return NAN;
  985. if (is->paused) {
  986. return is->video_current_pts;
  987. } else {
  988. return is->video_current_pts_drift + av_gettime() / 1000000.0;
  989. }
  990. }
  991. /* get the current external clock value */
  992. static double get_external_clock(VideoState *is)
  993. {
  994. if (is->paused) {
  995. return is->external_clock;
  996. } else {
  997. double time = av_gettime() / 1000000.0;
  998. return is->external_clock_drift + time - (time - is->external_clock_time / 1000000.0) * (1.0 - is->external_clock_speed);
  999. }
  1000. }
  1001. static int get_master_sync_type(VideoState *is) {
  1002. if (is->av_sync_type == AV_SYNC_VIDEO_MASTER) {
  1003. if (is->video_st)
  1004. return AV_SYNC_VIDEO_MASTER;
  1005. else
  1006. return AV_SYNC_AUDIO_MASTER;
  1007. } else if (is->av_sync_type == AV_SYNC_AUDIO_MASTER) {
  1008. if (is->audio_st)
  1009. return AV_SYNC_AUDIO_MASTER;
  1010. else
  1011. return AV_SYNC_EXTERNAL_CLOCK;
  1012. } else {
  1013. return AV_SYNC_EXTERNAL_CLOCK;
  1014. }
  1015. }
  1016. /* get the current master clock value */
  1017. static double get_master_clock(VideoState *is)
  1018. {
  1019. double val;
  1020. switch (get_master_sync_type(is)) {
  1021. case AV_SYNC_VIDEO_MASTER:
  1022. val = get_video_clock(is);
  1023. break;
  1024. case AV_SYNC_AUDIO_MASTER:
  1025. val = get_audio_clock(is);
  1026. break;
  1027. default:
  1028. val = get_external_clock(is);
  1029. break;
  1030. }
  1031. return val;
  1032. }
  1033. static void update_external_clock_pts(VideoState *is, double pts)
  1034. {
  1035. is->external_clock_time = av_gettime();
  1036. is->external_clock = pts;
  1037. is->external_clock_drift = pts - is->external_clock_time / 1000000.0;
  1038. }
  1039. static void check_external_clock_sync(VideoState *is, double pts) {
  1040. double ext_clock = get_external_clock(is);
  1041. if (isnan(ext_clock) || fabs(ext_clock - pts) > AV_NOSYNC_THRESHOLD) {
  1042. update_external_clock_pts(is, pts);
  1043. }
  1044. }
  1045. static void update_external_clock_speed(VideoState *is, double speed) {
  1046. update_external_clock_pts(is, get_external_clock(is));
  1047. is->external_clock_speed = speed;
  1048. }
  1049. static void check_external_clock_speed(VideoState *is) {
  1050. if (is->video_stream >= 0 && is->videoq.nb_packets <= MIN_FRAMES / 2 ||
  1051. is->audio_stream >= 0 && is->audioq.nb_packets <= MIN_FRAMES / 2) {
  1052. update_external_clock_speed(is, FFMAX(EXTERNAL_CLOCK_SPEED_MIN, is->external_clock_speed - EXTERNAL_CLOCK_SPEED_STEP));
  1053. } else if ((is->video_stream < 0 || is->videoq.nb_packets > MIN_FRAMES * 2) &&
  1054. (is->audio_stream < 0 || is->audioq.nb_packets > MIN_FRAMES * 2)) {
  1055. update_external_clock_speed(is, FFMIN(EXTERNAL_CLOCK_SPEED_MAX, is->external_clock_speed + EXTERNAL_CLOCK_SPEED_STEP));
  1056. } else {
  1057. double speed = is->external_clock_speed;
  1058. if (speed != 1.0)
  1059. update_external_clock_speed(is, speed + EXTERNAL_CLOCK_SPEED_STEP * (1.0 - speed) / fabs(1.0 - speed));
  1060. }
  1061. }
  1062. /* seek in the stream */
  1063. static void stream_seek(VideoState *is, int64_t pos, int64_t rel, int seek_by_bytes)
  1064. {
  1065. if (!is->seek_req) {
  1066. is->seek_pos = pos;
  1067. is->seek_rel = rel;
  1068. is->seek_flags &= ~AVSEEK_FLAG_BYTE;
  1069. if (seek_by_bytes)
  1070. is->seek_flags |= AVSEEK_FLAG_BYTE;
  1071. is->seek_req = 1;
  1072. SDL_CondSignal(is->continue_read_thread);
  1073. }
  1074. }
  1075. /* pause or resume the video */
  1076. static void stream_toggle_pause(VideoState *is)
  1077. {
  1078. if (is->paused) {
  1079. is->frame_timer += av_gettime() / 1000000.0 + is->video_current_pts_drift - is->video_current_pts;
  1080. if (is->read_pause_return != AVERROR(ENOSYS)) {
  1081. is->video_current_pts = is->video_current_pts_drift + av_gettime() / 1000000.0;
  1082. }
  1083. is->video_current_pts_drift = is->video_current_pts - av_gettime() / 1000000.0;
  1084. }
  1085. update_external_clock_pts(is, get_external_clock(is));
  1086. is->paused = !is->paused;
  1087. }
  1088. static void toggle_pause(VideoState *is)
  1089. {
  1090. stream_toggle_pause(is);
  1091. is->step = 0;
  1092. }
  1093. static void step_to_next_frame(VideoState *is)
  1094. {
  1095. /* if the stream is paused unpause it, then step */
  1096. if (is->paused)
  1097. stream_toggle_pause(is);
  1098. is->step = 1;
  1099. }
  1100. static double compute_target_delay(double delay, VideoState *is)
  1101. {
  1102. double sync_threshold, diff;
  1103. /* update delay to follow master synchronisation source */
  1104. if (get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER) {
  1105. /* if video is slave, we try to correct big delays by
  1106. duplicating or deleting a frame */
  1107. diff = get_video_clock(is) - get_master_clock(is);
  1108. /* skip or repeat frame. We take into account the
  1109. delay to compute the threshold. I still don't know
  1110. if it is the best guess */
  1111. sync_threshold = FFMAX(AV_SYNC_THRESHOLD, delay);
  1112. if (!isnan(diff) && fabs(diff) < AV_NOSYNC_THRESHOLD) {
  1113. if (diff <= -sync_threshold)
  1114. delay = 0;
  1115. else if (diff >= sync_threshold)
  1116. delay = 2 * delay;
  1117. }
  1118. }
  1119. av_dlog(NULL, "video: delay=%0.3f A-V=%f\n",
  1120. delay, -diff);
  1121. return delay;
  1122. }
  1123. static void pictq_next_picture(VideoState *is) {
  1124. /* update queue size and signal for next picture */
  1125. if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE)
  1126. is->pictq_rindex = 0;
  1127. SDL_LockMutex(is->pictq_mutex);
  1128. is->pictq_size--;
  1129. SDL_CondSignal(is->pictq_cond);
  1130. SDL_UnlockMutex(is->pictq_mutex);
  1131. }
  1132. static int pictq_prev_picture(VideoState *is) {
  1133. VideoPicture *prevvp;
  1134. int ret = 0;
  1135. /* update queue size and signal for the previous picture */
  1136. prevvp = &is->pictq[(is->pictq_rindex + VIDEO_PICTURE_QUEUE_SIZE - 1) % VIDEO_PICTURE_QUEUE_SIZE];
  1137. if (prevvp->allocated && prevvp->serial == is->videoq.serial) {
  1138. SDL_LockMutex(is->pictq_mutex);
  1139. if (is->pictq_size < VIDEO_PICTURE_QUEUE_SIZE - 1) {
  1140. if (--is->pictq_rindex == -1)
  1141. is->pictq_rindex = VIDEO_PICTURE_QUEUE_SIZE - 1;
  1142. is->pictq_size++;
  1143. ret = 1;
  1144. }
  1145. SDL_CondSignal(is->pictq_cond);
  1146. SDL_UnlockMutex(is->pictq_mutex);
  1147. }
  1148. return ret;
  1149. }
  1150. static void update_video_pts(VideoState *is, double pts, int64_t pos, int serial) {
  1151. double time = av_gettime() / 1000000.0;
  1152. /* update current video pts */
  1153. is->video_current_pts = pts;
  1154. is->video_current_pts_drift = is->video_current_pts - time;
  1155. is->video_current_pos = pos;
  1156. is->frame_last_pts = pts;
  1157. is->video_clock_serial = serial;
  1158. if (is->videoq.serial == serial)
  1159. check_external_clock_sync(is, is->video_current_pts);
  1160. }
  1161. /* called to display each frame */
  1162. static void video_refresh(void *opaque, double *remaining_time)
  1163. {
  1164. VideoState *is = opaque;
  1165. VideoPicture *vp;
  1166. double time;
  1167. SubPicture *sp, *sp2;
  1168. if (!is->paused && get_master_sync_type(is) == AV_SYNC_EXTERNAL_CLOCK && is->realtime)
  1169. check_external_clock_speed(is);
  1170. if (!display_disable && is->show_mode != SHOW_MODE_VIDEO && is->audio_st) {
  1171. time = av_gettime() / 1000000.0;
  1172. if (is->force_refresh || is->last_vis_time + rdftspeed < time) {
  1173. video_display(is);
  1174. is->last_vis_time = time;
  1175. }
  1176. *remaining_time = FFMIN(*remaining_time, is->last_vis_time + rdftspeed - time);
  1177. }
  1178. if (is->video_st) {
  1179. int redisplay = 0;
  1180. if (is->force_refresh)
  1181. redisplay = pictq_prev_picture(is);
  1182. retry:
  1183. if (is->pictq_size == 0) {
  1184. SDL_LockMutex(is->pictq_mutex);
  1185. if (is->frame_last_dropped_pts != AV_NOPTS_VALUE && is->frame_last_dropped_pts > is->frame_last_pts) {
  1186. update_video_pts(is, is->frame_last_dropped_pts, is->frame_last_dropped_pos, is->frame_last_dropped_serial);
  1187. is->frame_last_dropped_pts = AV_NOPTS_VALUE;
  1188. }
  1189. SDL_UnlockMutex(is->pictq_mutex);
  1190. // nothing to do, no picture to display in the queue
  1191. } else {
  1192. double last_duration, duration, delay;
  1193. /* dequeue the picture */
  1194. vp = &is->pictq[is->pictq_rindex];
  1195. if (vp->serial != is->videoq.serial) {
  1196. pictq_next_picture(is);
  1197. redisplay = 0;
  1198. goto retry;
  1199. }
  1200. if (is->paused)
  1201. goto display;
  1202. /* compute nominal last_duration */
  1203. last_duration = vp->pts - is->frame_last_pts;
  1204. if (!isnan(last_duration) && last_duration > 0 && last_duration < is->max_frame_duration) {
  1205. /* if duration of the last frame was sane, update last_duration in video state */
  1206. is->frame_last_duration = last_duration;
  1207. }
  1208. delay = compute_target_delay(is->frame_last_duration, is);
  1209. time= av_gettime()/1000000.0;
  1210. if (time < is->frame_timer + delay) {
  1211. *remaining_time = FFMIN(is->frame_timer + delay - time, *remaining_time);
  1212. return;
  1213. }
  1214. if (delay > 0)
  1215. is->frame_timer += delay * FFMAX(1, floor((time-is->frame_timer) / delay));
  1216. SDL_LockMutex(is->pictq_mutex);
  1217. if (!isnan(vp->pts))
  1218. update_video_pts(is, vp->pts, vp->pos, vp->serial);
  1219. SDL_UnlockMutex(is->pictq_mutex);
  1220. if (is->pictq_size > 1) {
  1221. VideoPicture *nextvp = &is->pictq[(is->pictq_rindex + 1) % VIDEO_PICTURE_QUEUE_SIZE];
  1222. duration = nextvp->pts - vp->pts;
  1223. if(!is->step && (redisplay || framedrop>0 || (framedrop && get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER)) && time > is->frame_timer + duration){
  1224. if (!redisplay)
  1225. is->frame_drops_late++;
  1226. pictq_next_picture(is);
  1227. redisplay = 0;
  1228. goto retry;
  1229. }
  1230. }
  1231. if (is->subtitle_st) {
  1232. if (is->subtitle_stream_changed) {
  1233. SDL_LockMutex(is->subpq_mutex);
  1234. while (is->subpq_size) {
  1235. free_subpicture(&is->subpq[is->subpq_rindex]);
  1236. /* update queue size and signal for next picture */
  1237. if (++is->subpq_rindex == SUBPICTURE_QUEUE_SIZE)
  1238. is->subpq_rindex = 0;
  1239. is->subpq_size--;
  1240. }
  1241. is->subtitle_stream_changed = 0;
  1242. SDL_CondSignal(is->subpq_cond);
  1243. SDL_UnlockMutex(is->subpq_mutex);
  1244. } else {
  1245. if (is->subpq_size > 0) {
  1246. sp = &is->subpq[is->subpq_rindex];
  1247. if (is->subpq_size > 1)
  1248. sp2 = &is->subpq[(is->subpq_rindex + 1) % SUBPICTURE_QUEUE_SIZE];
  1249. else
  1250. sp2 = NULL;
  1251. if ((is->video_current_pts > (sp->pts + ((float) sp->sub.end_display_time / 1000)))
  1252. || (sp2 && is->video_current_pts > (sp2->pts + ((float) sp2->sub.start_display_time / 1000))))
  1253. {
  1254. free_subpicture(sp);
  1255. /* update queue size and signal for next picture */
  1256. if (++is->subpq_rindex == SUBPICTURE_QUEUE_SIZE)
  1257. is->subpq_rindex = 0;
  1258. SDL_LockMutex(is->subpq_mutex);
  1259. is->subpq_size--;
  1260. SDL_CondSignal(is->subpq_cond);
  1261. SDL_UnlockMutex(is->subpq_mutex);
  1262. }
  1263. }
  1264. }
  1265. }
  1266. display:
  1267. /* display picture */
  1268. if (!display_disable && is->show_mode == SHOW_MODE_VIDEO)
  1269. video_display(is);
  1270. pictq_next_picture(is);
  1271. if (is->step && !is->paused)
  1272. stream_toggle_pause(is);
  1273. }
  1274. }
  1275. is->force_refresh = 0;
  1276. if (show_status) {
  1277. static int64_t last_time;
  1278. int64_t cur_time;
  1279. int aqsize, vqsize, sqsize;
  1280. double av_diff;
  1281. cur_time = av_gettime();
  1282. if (!last_time || (cur_time - last_time) >= 30000) {
  1283. aqsize = 0;
  1284. vqsize = 0;
  1285. sqsize = 0;
  1286. if (is->audio_st)
  1287. aqsize = is->audioq.size;
  1288. if (is->video_st)
  1289. vqsize = is->videoq.size;
  1290. if (is->subtitle_st)
  1291. sqsize = is->subtitleq.size;
  1292. av_diff = 0;
  1293. if (is->audio_st && is->video_st)
  1294. av_diff = get_audio_clock(is) - get_video_clock(is);
  1295. printf("%7.2f A-V:%7.3f fd=%4d aq=%5dKB vq=%5dKB sq=%5dB f=%"PRId64"/%"PRId64" \r",
  1296. get_master_clock(is),
  1297. av_diff,
  1298. is->frame_drops_early + is->frame_drops_late,
  1299. aqsize / 1024,
  1300. vqsize / 1024,
  1301. sqsize,
  1302. is->video_st ? is->video_st->codec->pts_correction_num_faulty_dts : 0,
  1303. is->video_st ? is->video_st->codec->pts_correction_num_faulty_pts : 0);
  1304. fflush(stdout);
  1305. last_time = cur_time;
  1306. }
  1307. }
  1308. }
  1309. /* allocate a picture (needs to do that in main thread to avoid
  1310. potential locking problems */
  1311. static void alloc_picture(VideoState *is)
  1312. {
  1313. VideoPicture *vp;
  1314. vp = &is->pictq[is->pictq_windex];
  1315. if (vp->bmp)
  1316. SDL_FreeYUVOverlay(vp->bmp);
  1317. video_open(is, 0, vp);
  1318. vp->bmp = SDL_CreateYUVOverlay(vp->width, vp->height,
  1319. SDL_YV12_OVERLAY,
  1320. screen);
  1321. if (!vp->bmp || vp->bmp->pitches[0] < vp->width) {
  1322. /* SDL allocates a buffer smaller than requested if the video
  1323. * overlay hardware is unable to support the requested size. */
  1324. fprintf(stderr, "Error: the video system does not support an image\n"
  1325. "size of %dx%d pixels. Try using -lowres or -vf \"scale=w:h\"\n"
  1326. "to reduce the image size.\n", vp->width, vp->height );
  1327. do_exit(is);
  1328. }
  1329. SDL_LockMutex(is->pictq_mutex);
  1330. vp->allocated = 1;
  1331. SDL_CondSignal(is->pictq_cond);
  1332. SDL_UnlockMutex(is->pictq_mutex);
  1333. }
  1334. static void duplicate_right_border_pixels(SDL_Overlay *bmp) {
  1335. int i, width, height;
  1336. Uint8 *p, *maxp;
  1337. for (i = 0; i < 3; i++) {
  1338. width = bmp->w;
  1339. height = bmp->h;
  1340. if (i > 0) {
  1341. width >>= 1;
  1342. height >>= 1;
  1343. }
  1344. if (bmp->pitches[i] > width) {
  1345. maxp = bmp->pixels[i] + bmp->pitches[i] * height - 1;
  1346. for (p = bmp->pixels[i] + width - 1; p < maxp; p += bmp->pitches[i])
  1347. *(p+1) = *p;
  1348. }
  1349. }
  1350. }
  1351. static int queue_picture(VideoState *is, AVFrame *src_frame, double pts, int64_t pos, int serial)
  1352. {
  1353. VideoPicture *vp;
  1354. #if defined(DEBUG_SYNC) && 0
  1355. printf("frame_type=%c pts=%0.3f\n",
  1356. av_get_picture_type_char(src_frame->pict_type), pts);
  1357. #endif
  1358. /* wait until we have space to put a new picture */
  1359. SDL_LockMutex(is->pictq_mutex);
  1360. /* keep the last already displayed picture in the queue */
  1361. while (is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE - 2 &&
  1362. !is->videoq.abort_request) {
  1363. SDL_CondWait(is->pictq_cond, is->pictq_mutex);
  1364. }
  1365. SDL_UnlockMutex(is->pictq_mutex);
  1366. if (is->videoq.abort_request)
  1367. return -1;
  1368. vp = &is->pictq[is->pictq_windex];
  1369. vp->sar = src_frame->sample_aspect_ratio;
  1370. /* alloc or resize hardware picture buffer */
  1371. if (!vp->bmp || vp->reallocate || !vp->allocated ||
  1372. vp->width != src_frame->width ||
  1373. vp->height != src_frame->height) {
  1374. SDL_Event event;
  1375. vp->allocated = 0;
  1376. vp->reallocate = 0;
  1377. vp->width = src_frame->width;
  1378. vp->height = src_frame->height;
  1379. /* the allocation must be done in the main thread to avoid
  1380. locking problems. */
  1381. event.type = FF_ALLOC_EVENT;
  1382. event.user.data1 = is;
  1383. SDL_PushEvent(&event);
  1384. /* wait until the picture is allocated */
  1385. SDL_LockMutex(is->pictq_mutex);
  1386. while (!vp->allocated && !is->videoq.abort_request) {
  1387. SDL_CondWait(is->pictq_cond, is->pictq_mutex);
  1388. }
  1389. /* if the queue is aborted, we have to pop the pending ALLOC event or wait for the allocation to complete */
  1390. if (is->videoq.abort_request && SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_EVENTMASK(FF_ALLOC_EVENT)) != 1) {
  1391. while (!vp->allocated) {
  1392. SDL_CondWait(is->pictq_cond, is->pictq_mutex);
  1393. }
  1394. }
  1395. SDL_UnlockMutex(is->pictq_mutex);
  1396. if (is->videoq.abort_request)
  1397. return -1;
  1398. }
  1399. /* if the frame is not skipped, then display it */
  1400. if (vp->bmp) {
  1401. AVPicture pict = { { 0 } };
  1402. /* get a pointer on the bitmap */
  1403. SDL_LockYUVOverlay (vp->bmp);
  1404. pict.data[0] = vp->bmp->pixels[0];
  1405. pict.data[1] = vp->bmp->pixels[2];
  1406. pict.data[2] = vp->bmp->pixels[1];
  1407. pict.linesize[0] = vp->bmp->pitches[0];
  1408. pict.linesize[1] = vp->bmp->pitches[2];
  1409. pict.linesize[2] = vp->bmp->pitches[1];
  1410. #if CONFIG_AVFILTER
  1411. // FIXME use direct rendering
  1412. av_picture_copy(&pict, (AVPicture *)src_frame,
  1413. src_frame->format, vp->width, vp->height);
  1414. #else
  1415. av_opt_get_int(sws_opts, "sws_flags", 0, &sws_flags);
  1416. is->img_convert_ctx = sws_getCachedContext(is->img_convert_ctx,
  1417. vp->width, vp->height, src_frame->format, vp->width, vp->height,
  1418. AV_PIX_FMT_YUV420P, sws_flags, NULL, NULL, NULL);
  1419. if (is->img_convert_ctx == NULL) {
  1420. fprintf(stderr, "Cannot initialize the conversion context\n");
  1421. exit(1);
  1422. }
  1423. sws_scale(is->img_convert_ctx, src_frame->data, src_frame->linesize,
  1424. 0, vp->height, pict.data, pict.linesize);
  1425. #endif
  1426. /* workaround SDL PITCH_WORKAROUND */
  1427. duplicate_right_border_pixels(vp->bmp);
  1428. /* update the bitmap content */
  1429. SDL_UnlockYUVOverlay(vp->bmp);
  1430. vp->pts = pts;
  1431. vp->pos = pos;
  1432. vp->serial = serial;
  1433. /* now we can update the picture count */
  1434. if (++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE)
  1435. is->pictq_windex = 0;
  1436. SDL_LockMutex(is->pictq_mutex);
  1437. is->pictq_size++;
  1438. SDL_UnlockMutex(is->pictq_mutex);
  1439. }
  1440. return 0;
  1441. }
  1442. static int get_video_frame(VideoState *is, AVFrame *frame, AVPacket *pkt, int *serial)
  1443. {
  1444. int got_picture;
  1445. if (packet_queue_get(&is->videoq, pkt, 1, serial) < 0)
  1446. return -1;
  1447. if (pkt->data == flush_pkt.data) {
  1448. avcodec_flush_buffers(is->video_st->codec);
  1449. SDL_LockMutex(is->pictq_mutex);
  1450. // Make sure there are no long delay timers (ideally we should just flush the queue but that's harder)
  1451. while (is->pictq_size && !is->videoq.abort_request) {
  1452. SDL_CondWait(is->pictq_cond, is->pictq_mutex);
  1453. }
  1454. is->video_current_pos = -1;
  1455. is->frame_last_pts = AV_NOPTS_VALUE;
  1456. is->frame_last_duration = 0;
  1457. is->frame_timer = (double)av_gettime() / 1000000.0;
  1458. is->frame_last_dropped_pts = AV_NOPTS_VALUE;
  1459. SDL_UnlockMutex(is->pictq_mutex);
  1460. return 0;
  1461. }
  1462. if(avcodec_decode_video2(is->video_st->codec, frame, &got_picture, pkt) < 0)
  1463. return 0;
  1464. if (got_picture) {
  1465. int ret = 1;
  1466. double dpts = NAN;
  1467. if (decoder_reorder_pts == -1) {
  1468. frame->pts = av_frame_get_best_effort_timestamp(frame);
  1469. } else if (decoder_reorder_pts) {
  1470. frame->pts = frame->pkt_pts;
  1471. } else {
  1472. frame->pts = frame->pkt_dts;
  1473. }
  1474. if (frame->pts != AV_NOPTS_VALUE)
  1475. dpts = av_q2d(is->video_st->time_base) * frame->pts;
  1476. frame->sample_aspect_ratio = av_guess_sample_aspect_ratio(is->ic, is->video_st, frame);
  1477. if (framedrop>0 || (framedrop && get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER)) {
  1478. SDL_LockMutex(is->pictq_mutex);
  1479. if (is->frame_last_pts != AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE) {
  1480. double clockdiff = get_video_clock(is) - get_master_clock(is);
  1481. double ptsdiff = dpts - is->frame_last_pts;
  1482. if (!isnan(clockdiff) && fabs(clockdiff) < AV_NOSYNC_THRESHOLD &&
  1483. !isnan(ptsdiff) && ptsdiff > 0 && ptsdiff < AV_NOSYNC_THRESHOLD &&
  1484. clockdiff + ptsdiff - is->frame_last_filter_delay < 0 &&
  1485. is->videoq.nb_packets) {
  1486. is->frame_last_dropped_pos = pkt->pos;
  1487. is->frame_last_dropped_pts = dpts;
  1488. is->frame_last_dropped_serial = *serial;
  1489. is->frame_drops_early++;
  1490. av_frame_unref(frame);
  1491. ret = 0;
  1492. }
  1493. }
  1494. SDL_UnlockMutex(is->pictq_mutex);
  1495. }
  1496. return ret;
  1497. }
  1498. return 0;
  1499. }
  1500. #if CONFIG_AVFILTER
  1501. static int configure_filtergraph(AVFilterGraph *graph, const char *filtergraph,
  1502. AVFilterContext *source_ctx, AVFilterContext *sink_ctx)
  1503. {
  1504. int ret;
  1505. AVFilterInOut *outputs = NULL, *inputs = NULL;
  1506. if (filtergraph) {
  1507. outputs = avfilter_inout_alloc();
  1508. inputs = avfilter_inout_alloc();
  1509. if (!outputs || !inputs) {
  1510. ret = AVERROR(ENOMEM);
  1511. goto fail;
  1512. }
  1513. outputs->name = av_strdup("in");
  1514. outputs->filter_ctx = source_ctx;
  1515. outputs->pad_idx = 0;
  1516. outputs->next = NULL;
  1517. inputs->name = av_strdup("out");
  1518. inputs->filter_ctx = sink_ctx;
  1519. inputs->pad_idx = 0;
  1520. inputs->next = NULL;
  1521. if ((ret = avfilter_graph_parse(graph, filtergraph, &inputs, &outputs, NULL)) < 0)
  1522. goto fail;
  1523. } else {
  1524. if ((ret = avfilter_link(source_ctx, 0, sink_ctx, 0)) < 0)
  1525. goto fail;
  1526. }
  1527. ret = avfilter_graph_config(graph, NULL);
  1528. fail:
  1529. avfilter_inout_free(&outputs);
  1530. avfilter_inout_free(&inputs);
  1531. return ret;
  1532. }
  1533. static int configure_video_filters(AVFilterGraph *graph, VideoState *is, const char *vfilters, AVFrame *frame)
  1534. {
  1535. static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE };
  1536. char sws_flags_str[128];
  1537. char buffersrc_args[256];
  1538. int ret;
  1539. AVFilterContext *filt_src = NULL, *filt_out = NULL, *filt_crop;
  1540. AVCodecContext *codec = is->video_st->codec;
  1541. AVRational fr = av_guess_frame_rate(is->ic, is->video_st, NULL);
  1542. av_opt_get_int(sws_opts, "sws_flags", 0, &sws_flags);
  1543. snprintf(sws_flags_str, sizeof(sws_flags_str), "flags=%"PRId64, sws_flags);
  1544. graph->scale_sws_opts = av_strdup(sws_flags_str);
  1545. snprintf(buffersrc_args, sizeof(buffersrc_args),
  1546. "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
  1547. frame->width, frame->height, frame->format,
  1548. is->video_st->time_base.num, is->video_st->time_base.den,
  1549. codec->sample_aspect_ratio.num, FFMAX(codec->sample_aspect_ratio.den, 1));
  1550. if (fr.num && fr.den)
  1551. av_strlcatf(buffersrc_args, sizeof(buffersrc_args), ":frame_rate=%d/%d", fr.num, fr.den);
  1552. if ((ret = avfilter_graph_create_filter(&filt_src,
  1553. avfilter_get_by_name("buffer"),
  1554. "ffplay_buffer", buffersrc_args, NULL,
  1555. graph)) < 0)
  1556. goto fail;
  1557. ret = avfilter_graph_create_filter(&filt_out,
  1558. avfilter_get_by_name("buffersink"),
  1559. "ffplay_buffersink", NULL, NULL, graph);
  1560. if (ret < 0)
  1561. goto fail;
  1562. if ((ret = av_opt_set_int_list(filt_out, "pix_fmts", pix_fmts, AV_PIX_FMT_NONE, AV_OPT_SEARCH_CHILDREN)) < 0)
  1563. goto fail;
  1564. /* SDL YUV code is not handling odd width/height for some driver
  1565. * combinations, therefore we crop the picture to an even width/height. */
  1566. if ((ret = avfilter_graph_create_filter(&filt_crop,
  1567. avfilter_get_by_name("crop"),
  1568. "ffplay_crop", "floor(in_w/2)*2:floor(in_h/2)*2", NULL, graph)) < 0)
  1569. goto fail;
  1570. if ((ret = avfilter_link(filt_crop, 0, filt_out, 0)) < 0)
  1571. goto fail;
  1572. if ((ret = configure_filtergraph(graph, vfilters, filt_src, filt_crop)) < 0)
  1573. goto fail;
  1574. is->in_video_filter = filt_src;
  1575. is->out_video_filter = filt_out;
  1576. fail:
  1577. return ret;
  1578. }
  1579. static int configure_audio_filters(VideoState *is, const char *afilters, int force_output_format)
  1580. {
  1581. static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE };
  1582. int sample_rates[2] = { 0, -1 };
  1583. int64_t channel_layouts[2] = { 0, -1 };
  1584. int channels[2] = { 0, -1 };
  1585. AVFilterContext *filt_asrc = NULL, *filt_asink = NULL;
  1586. char asrc_args[256];
  1587. int ret;
  1588. avfilter_graph_free(&is->agraph);
  1589. if (!(is->agraph = avfilter_graph_alloc()))
  1590. return AVERROR(ENOMEM);
  1591. ret = snprintf(asrc_args, sizeof(asrc_args),
  1592. "sample_rate=%d:sample_fmt=%s:channels=%d:time_base=%d/%d",
  1593. is->audio_filter_src.freq, av_get_sample_fmt_name(is->audio_filter_src.fmt),
  1594. is->audio_filter_src.channels,
  1595. 1, is->audio_filter_src.freq);
  1596. if (is->audio_filter_src.channel_layout)
  1597. snprintf(asrc_args + ret, sizeof(asrc_args) - ret,
  1598. ":channel_layout=0x%"PRIx64, is->audio_filter_src.channel_layout);
  1599. ret = avfilter_graph_create_filter(&filt_asrc,
  1600. avfilter_get_by_name("abuffer"), "ffplay_abuffer",
  1601. asrc_args, NULL, is->agraph);
  1602. if (ret < 0)
  1603. goto end;
  1604. ret = avfilter_graph_create_filter(&filt_asink,
  1605. avfilter_get_by_name("abuffersink"), "ffplay_abuffersink",
  1606. NULL, NULL, is->agraph);
  1607. if (ret < 0)
  1608. goto end;
  1609. if ((ret = av_opt_set_int_list(filt_asink, "sample_fmts", sample_fmts, AV_SAMPLE_FMT_NONE, AV_OPT_SEARCH_CHILDREN)) < 0)
  1610. goto end;
  1611. if ((ret = av_opt_set_int(filt_asink, "all_channel_counts", 1, AV_OPT_SEARCH_CHILDREN)) < 0)
  1612. goto end;
  1613. if (force_output_format) {
  1614. channel_layouts[0] = is->audio_tgt.channel_layout;
  1615. channels [0] = is->audio_tgt.channels;
  1616. sample_rates [0] = is->audio_tgt.freq;
  1617. if ((ret = av_opt_set_int(filt_asink, "all_channel_counts", 0, AV_OPT_SEARCH_CHILDREN)) < 0)
  1618. goto end;
  1619. if ((ret = av_opt_set_int_list(filt_asink, "channel_layouts", channel_layouts, -1, AV_OPT_SEARCH_CHILDREN)) < 0)
  1620. goto end;
  1621. if ((ret = av_opt_set_int_list(filt_asink, "channel_counts" , channels , -1, AV_OPT_SEARCH_CHILDREN)) < 0)
  1622. goto end;
  1623. if ((ret = av_opt_set_int_list(filt_asink, "sample_rates" , sample_rates , -1, AV_OPT_SEARCH_CHILDREN)) < 0)
  1624. goto end;
  1625. }
  1626. if ((ret = configure_filtergraph(is->agraph, afilters, filt_asrc, filt_asink)) < 0)
  1627. goto end;
  1628. is->in_audio_filter = filt_asrc;
  1629. is->out_audio_filter = filt_asink;
  1630. end:
  1631. if (ret < 0)
  1632. avfilter_graph_free(&is->agraph);
  1633. return ret;
  1634. }
  1635. #endif /* CONFIG_AVFILTER */
  1636. static int video_thread(void *arg)
  1637. {
  1638. AVPacket pkt = { 0 };
  1639. VideoState *is = arg;
  1640. AVFrame *frame = av_frame_alloc();
  1641. double pts;
  1642. int ret;
  1643. int serial = 0;
  1644. #if CONFIG_AVFILTER
  1645. AVFilterGraph *graph = avfilter_graph_alloc();
  1646. AVFilterContext *filt_out = NULL, *filt_in = NULL;
  1647. int last_w = 0;
  1648. int last_h = 0;
  1649. enum AVPixelFormat last_format = -2;
  1650. int last_serial = -1;
  1651. #endif
  1652. for (;;) {
  1653. while (is->paused && !is->videoq.abort_request)
  1654. SDL_Delay(10);
  1655. avcodec_get_frame_defaults(frame);
  1656. av_free_packet(&pkt);
  1657. ret = get_video_frame(is, frame, &pkt, &serial);
  1658. if (ret < 0)
  1659. goto the_end;
  1660. if (!ret)
  1661. continue;
  1662. #if CONFIG_AVFILTER
  1663. if ( last_w != frame->width
  1664. || last_h != frame->height
  1665. || last_format != frame->format
  1666. || last_serial != serial) {
  1667. av_log(NULL, AV_LOG_DEBUG,
  1668. "Video frame changed from size:%dx%d format:%s serial:%d to size:%dx%d format:%s serial:%d\n",
  1669. last_w, last_h,
  1670. (const char *)av_x_if_null(av_get_pix_fmt_name(last_format), "none"), last_serial,
  1671. frame->width, frame->height,
  1672. (const char *)av_x_if_null(av_get_pix_fmt_name(frame->format), "none"), serial);
  1673. avfilter_graph_free(&graph);
  1674. graph = avfilter_graph_alloc();
  1675. if ((ret = configure_video_filters(graph, is, vfilters, frame)) < 0) {
  1676. SDL_Event event;
  1677. event.type = FF_QUIT_EVENT;
  1678. event.user.data1 = is;
  1679. SDL_PushEvent(&event);
  1680. av_free_packet(&pkt);
  1681. goto the_end;
  1682. }
  1683. filt_in = is->in_video_filter;
  1684. filt_out = is->out_video_filter;
  1685. last_w = frame->width;
  1686. last_h = frame->height;
  1687. last_format = frame->format;
  1688. last_serial = serial;
  1689. }
  1690. ret = av_buffersrc_add_frame(filt_in, frame);
  1691. if (ret < 0)
  1692. goto the_end;
  1693. av_frame_unref(frame);
  1694. avcodec_get_frame_defaults(frame);
  1695. av_free_packet(&pkt);
  1696. while (ret >= 0) {
  1697. is->frame_last_returned_time = av_gettime() / 1000000.0;
  1698. ret = av_buffersink_get_frame_flags(filt_out, frame, 0);
  1699. if (ret < 0) {
  1700. ret = 0;
  1701. break;
  1702. }
  1703. is->frame_last_filter_delay = av_gettime() / 1000000.0 - is->frame_last_returned_time;
  1704. if (fabs(is->frame_last_filter_delay) > AV_NOSYNC_THRESHOLD / 10.0)
  1705. is->frame_last_filter_delay = 0;
  1706. pts = (frame->pts == AV_NOPTS_VALUE) ? NAN : frame->pts * av_q2d(filt_out->inputs[0]->time_base);
  1707. ret = queue_picture(is, frame, pts, av_frame_get_pkt_pos(frame), serial);
  1708. av_frame_unref(frame);
  1709. }
  1710. #else
  1711. pts = (frame->pts == AV_NOPTS_VALUE) ? NAN : frame->pts * av_q2d(is->video_st->time_base);
  1712. ret = queue_picture(is, frame, pts, pkt.pos, serial);
  1713. av_frame_unref(frame);
  1714. #endif
  1715. if (ret < 0)
  1716. goto the_end;
  1717. }
  1718. the_end:
  1719. avcodec_flush_buffers(is->video_st->codec);
  1720. #if CONFIG_AVFILTER
  1721. avfilter_graph_free(&graph);
  1722. #endif
  1723. av_free_packet(&pkt);
  1724. av_frame_free(&frame);
  1725. return 0;
  1726. }
  1727. static int subtitle_thread(void *arg)
  1728. {
  1729. VideoState *is = arg;
  1730. SubPicture *sp;
  1731. AVPacket pkt1, *pkt = &pkt1;
  1732. int got_subtitle;
  1733. double pts;
  1734. int i, j;
  1735. int r, g, b, y, u, v, a;
  1736. for (;;) {
  1737. while (is->paused && !is->subtitleq.abort_request) {
  1738. SDL_Delay(10);
  1739. }
  1740. if (packet_queue_get(&is->subtitleq, pkt, 1, NULL) < 0)
  1741. break;
  1742. if (pkt->data == flush_pkt.data) {
  1743. avcodec_flush_buffers(is->subtitle_st->codec);
  1744. continue;
  1745. }
  1746. SDL_LockMutex(is->subpq_mutex);
  1747. while (is->subpq_size >= SUBPICTURE_QUEUE_SIZE &&
  1748. !is->subtitleq.abort_request) {
  1749. SDL_CondWait(is->subpq_cond, is->subpq_mutex);
  1750. }
  1751. SDL_UnlockMutex(is->subpq_mutex);
  1752. if (is->subtitleq.abort_request)
  1753. return 0;
  1754. sp = &is->subpq[is->subpq_windex];
  1755. /* NOTE: ipts is the PTS of the _first_ picture beginning in
  1756. this packet, if any */
  1757. pts = 0;
  1758. if (pkt->pts != AV_NOPTS_VALUE)
  1759. pts = av_q2d(is->subtitle_st->time_base) * pkt->pts;
  1760. avcodec_decode_subtitle2(is->subtitle_st->codec, &sp->sub,
  1761. &got_subtitle, pkt);
  1762. if (got_subtitle && sp->sub.format == 0) {
  1763. if (sp->sub.pts != AV_NOPTS_VALUE)
  1764. pts = sp->sub.pts / (double)AV_TIME_BASE;
  1765. sp->pts = pts;
  1766. for (i = 0; i < sp->sub.num_rects; i++)
  1767. {
  1768. for (j = 0; j < sp->sub.rects[i]->nb_colors; j++)
  1769. {
  1770. RGBA_IN(r, g, b, a, (uint32_t*)sp->sub.rects[i]->pict.data[1] + j);
  1771. y = RGB_TO_Y_CCIR(r, g, b);
  1772. u = RGB_TO_U_CCIR(r, g, b, 0);
  1773. v = RGB_TO_V_CCIR(r, g, b, 0);
  1774. YUVA_OUT((uint32_t*)sp->sub.rects[i]->pict.data[1] + j, y, u, v, a);
  1775. }
  1776. }
  1777. /* now we can update the picture count */
  1778. if (++is->subpq_windex == SUBPICTURE_QUEUE_SIZE)
  1779. is->subpq_windex = 0;
  1780. SDL_LockMutex(is->subpq_mutex);
  1781. is->subpq_size++;
  1782. SDL_UnlockMutex(is->subpq_mutex);
  1783. }
  1784. av_free_packet(pkt);
  1785. }
  1786. return 0;
  1787. }
  1788. /* copy samples for viewing in editor window */
  1789. static void update_sample_display(VideoState *is, short *samples, int samples_size)
  1790. {
  1791. int size, len;
  1792. size = samples_size / sizeof(short);
  1793. while (size > 0) {
  1794. len = SAMPLE_ARRAY_SIZE - is->sample_array_index;
  1795. if (len > size)
  1796. len = size;
  1797. memcpy(is->sample_array + is->sample_array_index, samples, len * sizeof(short));
  1798. samples += len;
  1799. is->sample_array_index += len;
  1800. if (is->sample_array_index >= SAMPLE_ARRAY_SIZE)
  1801. is->sample_array_index = 0;
  1802. size -= len;
  1803. }
  1804. }
  1805. /* return the wanted number of samples to get better sync if sync_type is video
  1806. * or external master clock */
  1807. static int synchronize_audio(VideoState *is, int nb_samples)
  1808. {
  1809. int wanted_nb_samples = nb_samples;
  1810. /* if not master, then we try to remove or add samples to correct the clock */
  1811. if (get_master_sync_type(is) != AV_SYNC_AUDIO_MASTER) {
  1812. double diff, avg_diff;
  1813. int min_nb_samples, max_nb_samples;
  1814. diff = get_audio_clock(is) - get_master_clock(is);
  1815. if (!isnan(diff) && fabs(diff) < AV_NOSYNC_THRESHOLD) {
  1816. is->audio_diff_cum = diff + is->audio_diff_avg_coef * is->audio_diff_cum;
  1817. if (is->audio_diff_avg_count < AUDIO_DIFF_AVG_NB) {
  1818. /* not enough measures to have a correct estimate */
  1819. is->audio_diff_avg_count++;
  1820. } else {
  1821. /* estimate the A-V difference */
  1822. avg_diff = is->audio_diff_cum * (1.0 - is->audio_diff_avg_coef);
  1823. if (fabs(avg_diff) >= is->audio_diff_threshold) {
  1824. wanted_nb_samples = nb_samples + (int)(diff * is->audio_src.freq);
  1825. min_nb_samples = ((nb_samples * (100 - SAMPLE_CORRECTION_PERCENT_MAX) / 100));
  1826. max_nb_samples = ((nb_samples * (100 + SAMPLE_CORRECTION_PERCENT_MAX) / 100));
  1827. wanted_nb_samples = FFMIN(FFMAX(wanted_nb_samples, min_nb_samples), max_nb_samples);
  1828. }
  1829. av_dlog(NULL, "diff=%f adiff=%f sample_diff=%d apts=%0.3f %f\n",
  1830. diff, avg_diff, wanted_nb_samples - nb_samples,
  1831. is->audio_clock, is->audio_diff_threshold);
  1832. }
  1833. } else {
  1834. /* too big difference : may be initial PTS errors, so
  1835. reset A-V filter */
  1836. is->audio_diff_avg_count = 0;
  1837. is->audio_diff_cum = 0;
  1838. }
  1839. }
  1840. return wanted_nb_samples;
  1841. }
  1842. /**
  1843. * Decode one audio frame and return its uncompressed size.
  1844. *
  1845. * The processed audio frame is decoded, converted if required, and
  1846. * stored in is->audio_buf, with size in bytes given by the return
  1847. * value.
  1848. */
  1849. static int audio_decode_frame(VideoState *is)
  1850. {
  1851. AVPacket *pkt_temp = &is->audio_pkt_temp;
  1852. AVPacket *pkt = &is->audio_pkt;
  1853. AVCodecContext *dec = is->audio_st->codec;
  1854. int len1, data_size, resampled_data_size;
  1855. int64_t dec_channel_layout;
  1856. int got_frame;
  1857. av_unused double audio_clock0;
  1858. int new_packet = 0;
  1859. int flush_complete = 0;
  1860. int wanted_nb_samples;
  1861. AVRational tb;
  1862. int ret;
  1863. int reconfigure;
  1864. for (;;) {
  1865. /* NOTE: the audio packet can contain several frames */
  1866. while (pkt_temp->size > 0 || (!pkt_temp->data && new_packet) || is->audio_buf_frames_pending) {
  1867. if (!is->frame) {
  1868. if (!(is->frame = avcodec_alloc_frame()))
  1869. return AVERROR(ENOMEM);
  1870. } else {
  1871. av_frame_unref(is->frame);
  1872. avcodec_get_frame_defaults(is->frame);
  1873. }
  1874. if (is->audioq.serial != is->audio_pkt_temp_serial)
  1875. break;
  1876. if (is->paused)
  1877. return -1;
  1878. if (!is->audio_buf_frames_pending) {
  1879. if (flush_complete)
  1880. break;
  1881. new_packet = 0;
  1882. len1 = avcodec_decode_audio4(dec, is->frame, &got_frame, pkt_temp);
  1883. if (len1 < 0) {
  1884. /* if error, we skip the frame */
  1885. pkt_temp->size = 0;
  1886. break;
  1887. }
  1888. pkt_temp->data += len1;
  1889. pkt_temp->size -= len1;
  1890. if (!got_frame) {
  1891. /* stop sending empty packets if the decoder is finished */
  1892. if (!pkt_temp->data && dec->codec->capabilities & CODEC_CAP_DELAY)
  1893. flush_complete = 1;
  1894. continue;
  1895. }
  1896. tb = (AVRational){1, is->frame->sample_rate};
  1897. if (is->frame->pts != AV_NOPTS_VALUE)
  1898. is->frame->pts = av_rescale_q(is->frame->pts, dec->time_base, tb);
  1899. if (is->frame->pts == AV_NOPTS_VALUE && pkt_temp->pts != AV_NOPTS_VALUE)
  1900. is->frame->pts = av_rescale_q(pkt_temp->pts, is->audio_st->time_base, tb);
  1901. if (pkt_temp->pts != AV_NOPTS_VALUE)
  1902. pkt_temp->pts += (double) is->frame->nb_samples / is->frame->sample_rate / av_q2d(is->audio_st->time_base);
  1903. #if CONFIG_AVFILTER
  1904. dec_channel_layout = get_valid_channel_layout(is->frame->channel_layout, av_frame_get_channels(is->frame));
  1905. reconfigure =
  1906. cmp_audio_fmts(is->audio_filter_src.fmt, is->audio_filter_src.channels,
  1907. is->frame->format, av_frame_get_channels(is->frame)) ||
  1908. is->audio_filter_src.channel_layout != dec_channel_layout ||
  1909. is->audio_filter_src.freq != is->frame->sample_rate ||
  1910. is->audio_pkt_temp_serial != is->audio_last_serial;
  1911. if (reconfigure) {
  1912. char buf1[1024], buf2[1024];
  1913. av_get_channel_layout_string(buf1, sizeof(buf1), -1, is->audio_filter_src.channel_layout);
  1914. av_get_channel_layout_string(buf2, sizeof(buf2), -1, dec_channel_layout);
  1915. av_log(NULL, AV_LOG_DEBUG,
  1916. "Audio frame changed from rate:%d ch:%d fmt:%s layout:%s serial:%d to rate:%d ch:%d fmt:%s layout:%s serial:%d\n",
  1917. is->audio_filter_src.freq, is->audio_filter_src.channels, av_get_sample_fmt_name(is->audio_filter_src.fmt), buf1, is->audio_last_serial,
  1918. is->frame->sample_rate, av_frame_get_channels(is->frame), av_get_sample_fmt_name(is->frame->format), buf2, is->audio_pkt_temp_serial);
  1919. is->audio_filter_src.fmt = is->frame->format;
  1920. is->audio_filter_src.channels = av_frame_get_channels(is->frame);
  1921. is->audio_filter_src.channel_layout = dec_channel_layout;
  1922. is->audio_filter_src.freq = is->frame->sample_rate;
  1923. is->audio_last_serial = is->audio_pkt_temp_serial;
  1924. if ((ret = configure_audio_filters(is, afilters, 1)) < 0)
  1925. return ret;
  1926. }
  1927. if ((ret = av_buffersrc_add_frame(is->in_audio_filter, is->frame)) < 0)
  1928. return ret;
  1929. av_frame_unref(is->frame);
  1930. #endif
  1931. }
  1932. #if CONFIG_AVFILTER
  1933. if ((ret = av_buffersink_get_frame_flags(is->out_audio_filter, is->frame, 0)) < 0) {
  1934. if (ret == AVERROR(EAGAIN)) {
  1935. is->audio_buf_frames_pending = 0;
  1936. continue;
  1937. }
  1938. return ret;
  1939. }
  1940. is->audio_buf_frames_pending = 1;
  1941. tb = is->out_audio_filter->inputs[0]->time_base;
  1942. #endif
  1943. data_size = av_samples_get_buffer_size(NULL, av_frame_get_channels(is->frame),
  1944. is->frame->nb_samples,
  1945. is->frame->format, 1);
  1946. dec_channel_layout =
  1947. (is->frame->channel_layout && av_frame_get_channels(is->frame) == av_get_channel_layout_nb_channels(is->frame->channel_layout)) ?
  1948. is->frame->channel_layout : av_get_default_channel_layout(av_frame_get_channels(is->frame));
  1949. wanted_nb_samples = synchronize_audio(is, is->frame->nb_samples);
  1950. if (is->frame->format != is->audio_src.fmt ||
  1951. dec_channel_layout != is->audio_src.channel_layout ||
  1952. is->frame->sample_rate != is->audio_src.freq ||
  1953. (wanted_nb_samples != is->frame->nb_samples && !is->swr_ctx)) {
  1954. swr_free(&is->swr_ctx);
  1955. is->swr_ctx = swr_alloc_set_opts(NULL,
  1956. is->audio_tgt.channel_layout, is->audio_tgt.fmt, is->audio_tgt.freq,
  1957. dec_channel_layout, is->frame->format, is->frame->sample_rate,
  1958. 0, NULL);
  1959. if (!is->swr_ctx || swr_init(is->swr_ctx) < 0) {
  1960. fprintf(stderr, "Cannot create sample rate converter for conversion of %d Hz %s %d channels to %d Hz %s %d channels!\n",
  1961. is->frame->sample_rate, av_get_sample_fmt_name(is->frame->format), av_frame_get_channels(is->frame),
  1962. is->audio_tgt.freq, av_get_sample_fmt_name(is->audio_tgt.fmt), is->audio_tgt.channels);
  1963. break;
  1964. }
  1965. is->audio_src.channel_layout = dec_channel_layout;
  1966. is->audio_src.channels = av_frame_get_channels(is->frame);
  1967. is->audio_src.freq = is->frame->sample_rate;
  1968. is->audio_src.fmt = is->frame->format;
  1969. }
  1970. if (is->swr_ctx) {
  1971. const uint8_t **in = (const uint8_t **)is->frame->extended_data;
  1972. uint8_t **out = &is->audio_buf1;
  1973. int out_count = (int64_t)wanted_nb_samples * is->audio_tgt.freq / is->frame->sample_rate + 256;
  1974. int out_size = av_samples_get_buffer_size(NULL, is->audio_tgt.channels, out_count, is->audio_tgt.fmt, 0);
  1975. int len2;
  1976. if (out_size < 0) {
  1977. fprintf(stderr, "av_samples_get_buffer_size() failed\n");
  1978. break;
  1979. }
  1980. if (wanted_nb_samples != is->frame->nb_samples) {
  1981. if (swr_set_compensation(is->swr_ctx, (wanted_nb_samples - is->frame->nb_samples) * is->audio_tgt.freq / is->frame->sample_rate,
  1982. wanted_nb_samples * is->audio_tgt.freq / is->frame->sample_rate) < 0) {
  1983. fprintf(stderr, "swr_set_compensation() failed\n");
  1984. break;
  1985. }
  1986. }
  1987. av_fast_malloc(&is->audio_buf1, &is->audio_buf1_size, out_size);
  1988. if (!is->audio_buf1)
  1989. return AVERROR(ENOMEM);
  1990. len2 = swr_convert(is->swr_ctx, out, out_count, in, is->frame->nb_samples);
  1991. if (len2 < 0) {
  1992. fprintf(stderr, "swr_convert() failed\n");
  1993. break;
  1994. }
  1995. if (len2 == out_count) {
  1996. fprintf(stderr, "warning: audio buffer is probably too small\n");
  1997. swr_init(is->swr_ctx);
  1998. }
  1999. is->audio_buf = is->audio_buf1;
  2000. resampled_data_size = len2 * is->audio_tgt.channels * av_get_bytes_per_sample(is->audio_tgt.fmt);
  2001. } else {
  2002. is->audio_buf = is->frame->data[0];
  2003. resampled_data_size = data_size;
  2004. }
  2005. audio_clock0 = is->audio_clock;
  2006. /* update the audio clock with the pts */
  2007. if (is->frame->pts != AV_NOPTS_VALUE) {
  2008. is->audio_clock = is->frame->pts * av_q2d(tb) + (double) is->frame->nb_samples / is->frame->sample_rate;
  2009. is->audio_clock_serial = is->audio_pkt_temp_serial;
  2010. }
  2011. #ifdef DEBUG
  2012. {
  2013. static double last_clock;
  2014. printf("audio: delay=%0.3f clock=%0.3f clock0=%0.3f\n",
  2015. is->audio_clock - last_clock,
  2016. is->audio_clock, audio_clock0);
  2017. last_clock = is->audio_clock;
  2018. }
  2019. #endif
  2020. return resampled_data_size;
  2021. }
  2022. /* free the current packet */
  2023. if (pkt->data)
  2024. av_free_packet(pkt);
  2025. memset(pkt_temp, 0, sizeof(*pkt_temp));
  2026. if (is->audioq.abort_request) {
  2027. return -1;
  2028. }
  2029. if (is->audioq.nb_packets == 0)
  2030. SDL_CondSignal(is->continue_read_thread);
  2031. /* read next packet */
  2032. if ((new_packet = packet_queue_get(&is->audioq, pkt, 1, &is->audio_pkt_temp_serial)) < 0)
  2033. return -1;
  2034. if (pkt->data == flush_pkt.data) {
  2035. avcodec_flush_buffers(dec);
  2036. flush_complete = 0;
  2037. is->audio_buf_frames_pending = 0;
  2038. }
  2039. *pkt_temp = *pkt;
  2040. }
  2041. }
  2042. /* prepare a new audio buffer */
  2043. static void sdl_audio_callback(void *opaque, Uint8 *stream, int len)
  2044. {
  2045. VideoState *is = opaque;
  2046. int audio_size, len1;
  2047. int bytes_per_sec;
  2048. int frame_size = av_samples_get_buffer_size(NULL, is->audio_tgt.channels, 1, is->audio_tgt.fmt, 1);
  2049. audio_callback_time = av_gettime();
  2050. while (len > 0) {
  2051. if (is->audio_buf_index >= is->audio_buf_size) {
  2052. audio_size = audio_decode_frame(is);
  2053. if (audio_size < 0) {
  2054. /* if error, just output silence */
  2055. is->audio_buf = is->silence_buf;
  2056. is->audio_buf_size = sizeof(is->silence_buf) / frame_size * frame_size;
  2057. } else {
  2058. if (is->show_mode != SHOW_MODE_VIDEO)
  2059. update_sample_display(is, (int16_t *)is->audio_buf, audio_size);
  2060. is->audio_buf_size = audio_size;
  2061. }
  2062. is->audio_buf_index = 0;
  2063. }
  2064. len1 = is->audio_buf_size - is->audio_buf_index;
  2065. if (len1 > len)
  2066. len1 = len;
  2067. memcpy(stream, (uint8_t *)is->audio_buf + is->audio_buf_index, len1);
  2068. len -= len1;
  2069. stream += len1;
  2070. is->audio_buf_index += len1;
  2071. }
  2072. bytes_per_sec = is->audio_tgt.freq * is->audio_tgt.channels * av_get_bytes_per_sample(is->audio_tgt.fmt);
  2073. is->audio_write_buf_size = is->audio_buf_size - is->audio_buf_index;
  2074. /* Let's assume the audio driver that is used by SDL has two periods. */
  2075. is->audio_current_pts = is->audio_clock - (double)(2 * is->audio_hw_buf_size + is->audio_write_buf_size) / bytes_per_sec;
  2076. is->audio_current_pts_drift = is->audio_current_pts - audio_callback_time / 1000000.0;
  2077. if (is->audioq.serial == is->audio_clock_serial)
  2078. check_external_clock_sync(is, is->audio_current_pts);
  2079. }
  2080. static int audio_open(void *opaque, int64_t wanted_channel_layout, int wanted_nb_channels, int wanted_sample_rate, struct AudioParams *audio_hw_params)
  2081. {
  2082. SDL_AudioSpec wanted_spec, spec;
  2083. const char *env;
  2084. const int next_nb_channels[] = {0, 0, 1, 6, 2, 6, 4, 6};
  2085. env = SDL_getenv("SDL_AUDIO_CHANNELS");
  2086. if (env) {
  2087. wanted_nb_channels = atoi(env);
  2088. wanted_channel_layout = av_get_default_channel_layout(wanted_nb_channels);
  2089. }
  2090. if (!wanted_channel_layout || wanted_nb_channels != av_get_channel_layout_nb_channels(wanted_channel_layout)) {
  2091. wanted_channel_layout = av_get_default_channel_layout(wanted_nb_channels);
  2092. wanted_channel_layout &= ~AV_CH_LAYOUT_STEREO_DOWNMIX;
  2093. }
  2094. wanted_spec.channels = av_get_channel_layout_nb_channels(wanted_channel_layout);
  2095. wanted_spec.freq = wanted_sample_rate;
  2096. if (wanted_spec.freq <= 0 || wanted_spec.channels <= 0) {
  2097. fprintf(stderr, "Invalid sample rate or channel count!\n");
  2098. return -1;
  2099. }
  2100. wanted_spec.format = AUDIO_S16SYS;
  2101. wanted_spec.silence = 0;
  2102. wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE;
  2103. wanted_spec.callback = sdl_audio_callback;
  2104. wanted_spec.userdata = opaque;
  2105. while (SDL_OpenAudio(&wanted_spec, &spec) < 0) {
  2106. fprintf(stderr, "SDL_OpenAudio (%d channels): %s\n", wanted_spec.channels, SDL_GetError());
  2107. wanted_spec.channels = next_nb_channels[FFMIN(7, wanted_spec.channels)];
  2108. if (!wanted_spec.channels) {
  2109. fprintf(stderr, "No more channel combinations to try, audio open failed\n");
  2110. return -1;
  2111. }
  2112. wanted_channel_layout = av_get_default_channel_layout(wanted_spec.channels);
  2113. }
  2114. if (spec.format != AUDIO_S16SYS) {
  2115. fprintf(stderr, "SDL advised audio format %d is not supported!\n", spec.format);
  2116. return -1;
  2117. }
  2118. if (spec.channels != wanted_spec.channels) {
  2119. wanted_channel_layout = av_get_default_channel_layout(spec.channels);
  2120. if (!wanted_channel_layout) {
  2121. fprintf(stderr, "SDL advised channel count %d is not supported!\n", spec.channels);
  2122. return -1;
  2123. }
  2124. }
  2125. audio_hw_params->fmt = AV_SAMPLE_FMT_S16;
  2126. audio_hw_params->freq = spec.freq;
  2127. audio_hw_params->channel_layout = wanted_channel_layout;
  2128. audio_hw_params->channels = spec.channels;
  2129. return spec.size;
  2130. }
  2131. /* open a given stream. Return 0 if OK */
  2132. static int stream_component_open(VideoState *is, int stream_index)
  2133. {
  2134. AVFormatContext *ic = is->ic;
  2135. AVCodecContext *avctx;
  2136. AVCodec *codec;
  2137. const char *forced_codec_name = NULL;
  2138. AVDictionary *opts;
  2139. AVDictionaryEntry *t = NULL;
  2140. int sample_rate, nb_channels;
  2141. int64_t channel_layout;
  2142. int ret;
  2143. if (stream_index < 0 || stream_index >= ic->nb_streams)
  2144. return -1;
  2145. avctx = ic->streams[stream_index]->codec;
  2146. codec = avcodec_find_decoder(avctx->codec_id);
  2147. switch(avctx->codec_type){
  2148. case AVMEDIA_TYPE_AUDIO : is->last_audio_stream = stream_index; forced_codec_name = audio_codec_name; break;
  2149. case AVMEDIA_TYPE_SUBTITLE: is->last_subtitle_stream = stream_index; forced_codec_name = subtitle_codec_name; break;
  2150. case AVMEDIA_TYPE_VIDEO : is->last_video_stream = stream_index; forced_codec_name = video_codec_name; break;
  2151. }
  2152. if (forced_codec_name)
  2153. codec = avcodec_find_decoder_by_name(forced_codec_name);
  2154. if (!codec) {
  2155. if (forced_codec_name) fprintf(stderr, "No codec could be found with name '%s'\n", forced_codec_name);
  2156. else fprintf(stderr, "No codec could be found with id %d\n", avctx->codec_id);
  2157. return -1;
  2158. }
  2159. avctx->codec_id = codec->id;
  2160. avctx->workaround_bugs = workaround_bugs;
  2161. avctx->lowres = lowres;
  2162. if(avctx->lowres > codec->max_lowres){
  2163. av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n",
  2164. codec->max_lowres);
  2165. avctx->lowres= codec->max_lowres;
  2166. }
  2167. avctx->idct_algo = idct;
  2168. avctx->error_concealment = error_concealment;
  2169. if(avctx->lowres) avctx->flags |= CODEC_FLAG_EMU_EDGE;
  2170. if (fast) avctx->flags2 |= CODEC_FLAG2_FAST;
  2171. if(codec->capabilities & CODEC_CAP_DR1)
  2172. avctx->flags |= CODEC_FLAG_EMU_EDGE;
  2173. opts = filter_codec_opts(codec_opts, avctx->codec_id, ic, ic->streams[stream_index], codec);
  2174. if (!av_dict_get(opts, "threads", NULL, 0))
  2175. av_dict_set(&opts, "threads", "auto", 0);
  2176. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
  2177. av_dict_set(&opts, "refcounted_frames", "1", 0);
  2178. if (avcodec_open2(avctx, codec, &opts) < 0)
  2179. return -1;
  2180. if ((t = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
  2181. av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
  2182. return AVERROR_OPTION_NOT_FOUND;
  2183. }
  2184. ic->streams[stream_index]->discard = AVDISCARD_DEFAULT;
  2185. switch (avctx->codec_type) {
  2186. case AVMEDIA_TYPE_AUDIO:
  2187. #if CONFIG_AVFILTER
  2188. {
  2189. AVFilterLink *link;
  2190. is->audio_filter_src.freq = avctx->sample_rate;
  2191. is->audio_filter_src.channels = avctx->channels;
  2192. is->audio_filter_src.channel_layout = get_valid_channel_layout(avctx->channel_layout, avctx->channels);
  2193. is->audio_filter_src.fmt = avctx->sample_fmt;
  2194. if ((ret = configure_audio_filters(is, afilters, 0)) < 0)
  2195. return ret;
  2196. link = is->out_audio_filter->inputs[0];
  2197. sample_rate = link->sample_rate;
  2198. nb_channels = link->channels;
  2199. channel_layout = link->channel_layout;
  2200. }
  2201. #else
  2202. sample_rate = avctx->sample_rate;
  2203. nb_channels = avctx->channels;
  2204. channel_layout = avctx->channel_layout;
  2205. #endif
  2206. /* prepare audio output */
  2207. if ((ret = audio_open(is, channel_layout, nb_channels, sample_rate, &is->audio_tgt)) < 0)
  2208. return ret;
  2209. is->audio_hw_buf_size = ret;
  2210. is->audio_src = is->audio_tgt;
  2211. is->audio_buf_size = 0;
  2212. is->audio_buf_index = 0;
  2213. /* init averaging filter */
  2214. is->audio_diff_avg_coef = exp(log(0.01) / AUDIO_DIFF_AVG_NB);
  2215. is->audio_diff_avg_count = 0;
  2216. /* since we do not have a precise anough audio fifo fullness,
  2217. we correct audio sync only if larger than this threshold */
  2218. is->audio_diff_threshold = 2.0 * is->audio_hw_buf_size / av_samples_get_buffer_size(NULL, is->audio_tgt.channels, is->audio_tgt.freq, is->audio_tgt.fmt, 1);
  2219. memset(&is->audio_pkt, 0, sizeof(is->audio_pkt));
  2220. memset(&is->audio_pkt_temp, 0, sizeof(is->audio_pkt_temp));
  2221. is->audio_stream = stream_index;
  2222. is->audio_st = ic->streams[stream_index];
  2223. packet_queue_start(&is->audioq);
  2224. SDL_PauseAudio(0);
  2225. break;
  2226. case AVMEDIA_TYPE_VIDEO:
  2227. is->video_stream = stream_index;
  2228. is->video_st = ic->streams[stream_index];
  2229. packet_queue_start(&is->videoq);
  2230. is->video_tid = SDL_CreateThread(video_thread, is);
  2231. is->queue_attachments_req = 1;
  2232. break;
  2233. case AVMEDIA_TYPE_SUBTITLE:
  2234. is->subtitle_stream = stream_index;
  2235. is->subtitle_st = ic->streams[stream_index];
  2236. packet_queue_start(&is->subtitleq);
  2237. is->subtitle_tid = SDL_CreateThread(subtitle_thread, is);
  2238. break;
  2239. default:
  2240. break;
  2241. }
  2242. return 0;
  2243. }
  2244. static void stream_component_close(VideoState *is, int stream_index)
  2245. {
  2246. AVFormatContext *ic = is->ic;
  2247. AVCodecContext *avctx;
  2248. if (stream_index < 0 || stream_index >= ic->nb_streams)
  2249. return;
  2250. avctx = ic->streams[stream_index]->codec;
  2251. switch (avctx->codec_type) {
  2252. case AVMEDIA_TYPE_AUDIO:
  2253. packet_queue_abort(&is->audioq);
  2254. SDL_CloseAudio();
  2255. packet_queue_flush(&is->audioq);
  2256. av_free_packet(&is->audio_pkt);
  2257. swr_free(&is->swr_ctx);
  2258. av_freep(&is->audio_buf1);
  2259. is->audio_buf1_size = 0;
  2260. is->audio_buf = NULL;
  2261. av_frame_free(&is->frame);
  2262. if (is->rdft) {
  2263. av_rdft_end(is->rdft);
  2264. av_freep(&is->rdft_data);
  2265. is->rdft = NULL;
  2266. is->rdft_bits = 0;
  2267. }
  2268. #if CONFIG_AVFILTER
  2269. avfilter_graph_free(&is->agraph);
  2270. #endif
  2271. break;
  2272. case AVMEDIA_TYPE_VIDEO:
  2273. packet_queue_abort(&is->videoq);
  2274. /* note: we also signal this mutex to make sure we deblock the
  2275. video thread in all cases */
  2276. SDL_LockMutex(is->pictq_mutex);
  2277. SDL_CondSignal(is->pictq_cond);
  2278. SDL_UnlockMutex(is->pictq_mutex);
  2279. SDL_WaitThread(is->video_tid, NULL);
  2280. packet_queue_flush(&is->videoq);
  2281. break;
  2282. case AVMEDIA_TYPE_SUBTITLE:
  2283. packet_queue_abort(&is->subtitleq);
  2284. /* note: we also signal this mutex to make sure we deblock the
  2285. video thread in all cases */
  2286. SDL_LockMutex(is->subpq_mutex);
  2287. is->subtitle_stream_changed = 1;
  2288. SDL_CondSignal(is->subpq_cond);
  2289. SDL_UnlockMutex(is->subpq_mutex);
  2290. SDL_WaitThread(is->subtitle_tid, NULL);
  2291. packet_queue_flush(&is->subtitleq);
  2292. break;
  2293. default:
  2294. break;
  2295. }
  2296. ic->streams[stream_index]->discard = AVDISCARD_ALL;
  2297. avcodec_close(avctx);
  2298. switch (avctx->codec_type) {
  2299. case AVMEDIA_TYPE_AUDIO:
  2300. is->audio_st = NULL;
  2301. is->audio_stream = -1;
  2302. break;
  2303. case AVMEDIA_TYPE_VIDEO:
  2304. is->video_st = NULL;
  2305. is->video_stream = -1;
  2306. break;
  2307. case AVMEDIA_TYPE_SUBTITLE:
  2308. is->subtitle_st = NULL;
  2309. is->subtitle_stream = -1;
  2310. break;
  2311. default:
  2312. break;
  2313. }
  2314. }
  2315. static int decode_interrupt_cb(void *ctx)
  2316. {
  2317. VideoState *is = ctx;
  2318. return is->abort_request;
  2319. }
  2320. static int is_realtime(AVFormatContext *s)
  2321. {
  2322. if( !strcmp(s->iformat->name, "rtp")
  2323. || !strcmp(s->iformat->name, "rtsp")
  2324. || !strcmp(s->iformat->name, "sdp")
  2325. )
  2326. return 1;
  2327. if(s->pb && ( !strncmp(s->filename, "rtp:", 4)
  2328. || !strncmp(s->filename, "udp:", 4)
  2329. )
  2330. )
  2331. return 1;
  2332. return 0;
  2333. }
  2334. /* this thread gets the stream from the disk or the network */
  2335. static int read_thread(void *arg)
  2336. {
  2337. VideoState *is = arg;
  2338. AVFormatContext *ic = NULL;
  2339. int err, i, ret;
  2340. int st_index[AVMEDIA_TYPE_NB];
  2341. AVPacket pkt1, *pkt = &pkt1;
  2342. int eof = 0;
  2343. int pkt_in_play_range = 0;
  2344. AVDictionaryEntry *t;
  2345. AVDictionary **opts;
  2346. int orig_nb_streams;
  2347. SDL_mutex *wait_mutex = SDL_CreateMutex();
  2348. memset(st_index, -1, sizeof(st_index));
  2349. is->last_video_stream = is->video_stream = -1;
  2350. is->last_audio_stream = is->audio_stream = -1;
  2351. is->last_subtitle_stream = is->subtitle_stream = -1;
  2352. ic = avformat_alloc_context();
  2353. ic->interrupt_callback.callback = decode_interrupt_cb;
  2354. ic->interrupt_callback.opaque = is;
  2355. err = avformat_open_input(&ic, is->filename, is->iformat, &format_opts);
  2356. if (err < 0) {
  2357. print_error(is->filename, err);
  2358. ret = -1;
  2359. goto fail;
  2360. }
  2361. if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
  2362. av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
  2363. ret = AVERROR_OPTION_NOT_FOUND;
  2364. goto fail;
  2365. }
  2366. is->ic = ic;
  2367. if (genpts)
  2368. ic->flags |= AVFMT_FLAG_GENPTS;
  2369. opts = setup_find_stream_info_opts(ic, codec_opts);
  2370. orig_nb_streams = ic->nb_streams;
  2371. err = avformat_find_stream_info(ic, opts);
  2372. if (err < 0) {
  2373. fprintf(stderr, "%s: could not find codec parameters\n", is->filename);
  2374. ret = -1;
  2375. goto fail;
  2376. }
  2377. for (i = 0; i < orig_nb_streams; i++)
  2378. av_dict_free(&opts[i]);
  2379. av_freep(&opts);
  2380. if (ic->pb)
  2381. ic->pb->eof_reached = 0; // FIXME hack, ffplay maybe should not use url_feof() to test for the end
  2382. if (seek_by_bytes < 0)
  2383. seek_by_bytes = !!(ic->iformat->flags & AVFMT_TS_DISCONT) && strcmp("ogg", ic->iformat->name);
  2384. is->max_frame_duration = (ic->iformat->flags & AVFMT_TS_DISCONT) ? 10.0 : 3600.0;
  2385. if (!window_title && (t = av_dict_get(ic->metadata, "title", NULL, 0)))
  2386. window_title = av_asprintf("%s - %s", t->value, input_filename);
  2387. /* if seeking requested, we execute it */
  2388. if (start_time != AV_NOPTS_VALUE) {
  2389. int64_t timestamp;
  2390. timestamp = start_time;
  2391. /* add the stream start time */
  2392. if (ic->start_time != AV_NOPTS_VALUE)
  2393. timestamp += ic->start_time;
  2394. ret = avformat_seek_file(ic, -1, INT64_MIN, timestamp, INT64_MAX, 0);
  2395. if (ret < 0) {
  2396. fprintf(stderr, "%s: could not seek to position %0.3f\n",
  2397. is->filename, (double)timestamp / AV_TIME_BASE);
  2398. }
  2399. }
  2400. is->realtime = is_realtime(ic);
  2401. for (i = 0; i < ic->nb_streams; i++)
  2402. ic->streams[i]->discard = AVDISCARD_ALL;
  2403. if (!video_disable)
  2404. st_index[AVMEDIA_TYPE_VIDEO] =
  2405. av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO,
  2406. wanted_stream[AVMEDIA_TYPE_VIDEO], -1, NULL, 0);
  2407. if (!audio_disable)
  2408. st_index[AVMEDIA_TYPE_AUDIO] =
  2409. av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO,
  2410. wanted_stream[AVMEDIA_TYPE_AUDIO],
  2411. st_index[AVMEDIA_TYPE_VIDEO],
  2412. NULL, 0);
  2413. if (!video_disable && !subtitle_disable)
  2414. st_index[AVMEDIA_TYPE_SUBTITLE] =
  2415. av_find_best_stream(ic, AVMEDIA_TYPE_SUBTITLE,
  2416. wanted_stream[AVMEDIA_TYPE_SUBTITLE],
  2417. (st_index[AVMEDIA_TYPE_AUDIO] >= 0 ?
  2418. st_index[AVMEDIA_TYPE_AUDIO] :
  2419. st_index[AVMEDIA_TYPE_VIDEO]),
  2420. NULL, 0);
  2421. if (show_status) {
  2422. av_dump_format(ic, 0, is->filename, 0);
  2423. }
  2424. is->show_mode = show_mode;
  2425. /* open the streams */
  2426. if (st_index[AVMEDIA_TYPE_AUDIO] >= 0) {
  2427. stream_component_open(is, st_index[AVMEDIA_TYPE_AUDIO]);
  2428. }
  2429. ret = -1;
  2430. if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) {
  2431. ret = stream_component_open(is, st_index[AVMEDIA_TYPE_VIDEO]);
  2432. }
  2433. if (is->show_mode == SHOW_MODE_NONE)
  2434. is->show_mode = ret >= 0 ? SHOW_MODE_VIDEO : SHOW_MODE_RDFT;
  2435. if (st_index[AVMEDIA_TYPE_SUBTITLE] >= 0) {
  2436. stream_component_open(is, st_index[AVMEDIA_TYPE_SUBTITLE]);
  2437. }
  2438. if (is->video_stream < 0 && is->audio_stream < 0) {
  2439. fprintf(stderr, "%s: could not open codecs\n", is->filename);
  2440. ret = -1;
  2441. goto fail;
  2442. }
  2443. if (infinite_buffer < 0 && is->realtime)
  2444. infinite_buffer = 1;
  2445. for (;;) {
  2446. if (is->abort_request)
  2447. break;
  2448. if (is->paused != is->last_paused) {
  2449. is->last_paused = is->paused;
  2450. if (is->paused)
  2451. is->read_pause_return = av_read_pause(ic);
  2452. else
  2453. av_read_play(ic);
  2454. }
  2455. #if CONFIG_RTSP_DEMUXER || CONFIG_MMSH_PROTOCOL
  2456. if (is->paused &&
  2457. (!strcmp(ic->iformat->name, "rtsp") ||
  2458. (ic->pb && !strncmp(input_filename, "mmsh:", 5)))) {
  2459. /* wait 10 ms to avoid trying to get another packet */
  2460. /* XXX: horrible */
  2461. SDL_Delay(10);
  2462. continue;
  2463. }
  2464. #endif
  2465. if (is->seek_req) {
  2466. int64_t seek_target = is->seek_pos;
  2467. int64_t seek_min = is->seek_rel > 0 ? seek_target - is->seek_rel + 2: INT64_MIN;
  2468. int64_t seek_max = is->seek_rel < 0 ? seek_target - is->seek_rel - 2: INT64_MAX;
  2469. // FIXME the +-2 is due to rounding being not done in the correct direction in generation
  2470. // of the seek_pos/seek_rel variables
  2471. ret = avformat_seek_file(is->ic, -1, seek_min, seek_target, seek_max, is->seek_flags);
  2472. if (ret < 0) {
  2473. fprintf(stderr, "%s: error while seeking\n", is->ic->filename);
  2474. } else {
  2475. if (is->audio_stream >= 0) {
  2476. packet_queue_flush(&is->audioq);
  2477. packet_queue_put(&is->audioq, &flush_pkt);
  2478. }
  2479. if (is->subtitle_stream >= 0) {
  2480. packet_queue_flush(&is->subtitleq);
  2481. packet_queue_put(&is->subtitleq, &flush_pkt);
  2482. }
  2483. if (is->video_stream >= 0) {
  2484. packet_queue_flush(&is->videoq);
  2485. packet_queue_put(&is->videoq, &flush_pkt);
  2486. }
  2487. if (is->seek_flags & AVSEEK_FLAG_BYTE) {
  2488. update_external_clock_pts(is, NAN);
  2489. } else {
  2490. update_external_clock_pts(is, seek_target / (double)AV_TIME_BASE);
  2491. }
  2492. }
  2493. is->seek_req = 0;
  2494. is->queue_attachments_req = 1;
  2495. eof = 0;
  2496. if (is->paused)
  2497. step_to_next_frame(is);
  2498. }
  2499. if (is->queue_attachments_req) {
  2500. if (is->video_st && is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC) {
  2501. AVPacket copy;
  2502. if ((ret = av_copy_packet(&copy, &is->video_st->attached_pic)) < 0)
  2503. goto fail;
  2504. packet_queue_put(&is->videoq, &copy);
  2505. }
  2506. is->queue_attachments_req = 0;
  2507. }
  2508. /* if the queue are full, no need to read more */
  2509. if (infinite_buffer<1 &&
  2510. (is->audioq.size + is->videoq.size + is->subtitleq.size > MAX_QUEUE_SIZE
  2511. || ( (is->audioq .nb_packets > MIN_FRAMES || is->audio_stream < 0 || is->audioq.abort_request)
  2512. && (is->videoq .nb_packets > MIN_FRAMES || is->video_stream < 0 || is->videoq.abort_request
  2513. || (is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC))
  2514. && (is->subtitleq.nb_packets > MIN_FRAMES || is->subtitle_stream < 0 || is->subtitleq.abort_request)))) {
  2515. /* wait 10 ms */
  2516. SDL_LockMutex(wait_mutex);
  2517. SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
  2518. SDL_UnlockMutex(wait_mutex);
  2519. continue;
  2520. }
  2521. if (eof) {
  2522. if (is->video_stream >= 0) {
  2523. av_init_packet(pkt);
  2524. pkt->data = NULL;
  2525. pkt->size = 0;
  2526. pkt->stream_index = is->video_stream;
  2527. packet_queue_put(&is->videoq, pkt);
  2528. }
  2529. if (is->audio_stream >= 0 &&
  2530. is->audio_st->codec->codec->capabilities & CODEC_CAP_DELAY) {
  2531. av_init_packet(pkt);
  2532. pkt->data = NULL;
  2533. pkt->size = 0;
  2534. pkt->stream_index = is->audio_stream;
  2535. packet_queue_put(&is->audioq, pkt);
  2536. }
  2537. SDL_Delay(10);
  2538. if (is->audioq.size + is->videoq.size + is->subtitleq.size == 0) {
  2539. if (loop != 1 && (!loop || --loop)) {
  2540. stream_seek(is, start_time != AV_NOPTS_VALUE ? start_time : 0, 0, 0);
  2541. } else if (autoexit) {
  2542. ret = AVERROR_EOF;
  2543. goto fail;
  2544. }
  2545. }
  2546. eof=0;
  2547. continue;
  2548. }
  2549. ret = av_read_frame(ic, pkt);
  2550. if (ret < 0) {
  2551. if (ret == AVERROR_EOF || url_feof(ic->pb))
  2552. eof = 1;
  2553. if (ic->pb && ic->pb->error)
  2554. break;
  2555. SDL_LockMutex(wait_mutex);
  2556. SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
  2557. SDL_UnlockMutex(wait_mutex);
  2558. continue;
  2559. }
  2560. /* check if packet is in play range specified by user, then queue, otherwise discard */
  2561. pkt_in_play_range = duration == AV_NOPTS_VALUE ||
  2562. (pkt->pts - ic->streams[pkt->stream_index]->start_time) *
  2563. av_q2d(ic->streams[pkt->stream_index]->time_base) -
  2564. (double)(start_time != AV_NOPTS_VALUE ? start_time : 0) / 1000000
  2565. <= ((double)duration / 1000000);
  2566. if (pkt->stream_index == is->audio_stream && pkt_in_play_range) {
  2567. packet_queue_put(&is->audioq, pkt);
  2568. } else if (pkt->stream_index == is->video_stream && pkt_in_play_range
  2569. && !(is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC)) {
  2570. packet_queue_put(&is->videoq, pkt);
  2571. } else if (pkt->stream_index == is->subtitle_stream && pkt_in_play_range) {
  2572. packet_queue_put(&is->subtitleq, pkt);
  2573. } else {
  2574. av_free_packet(pkt);
  2575. }
  2576. }
  2577. /* wait until the end */
  2578. while (!is->abort_request) {
  2579. SDL_Delay(100);
  2580. }
  2581. ret = 0;
  2582. fail:
  2583. /* close each stream */
  2584. if (is->audio_stream >= 0)
  2585. stream_component_close(is, is->audio_stream);
  2586. if (is->video_stream >= 0)
  2587. stream_component_close(is, is->video_stream);
  2588. if (is->subtitle_stream >= 0)
  2589. stream_component_close(is, is->subtitle_stream);
  2590. if (is->ic) {
  2591. avformat_close_input(&is->ic);
  2592. }
  2593. if (ret != 0) {
  2594. SDL_Event event;
  2595. event.type = FF_QUIT_EVENT;
  2596. event.user.data1 = is;
  2597. SDL_PushEvent(&event);
  2598. }
  2599. SDL_DestroyMutex(wait_mutex);
  2600. return 0;
  2601. }
  2602. static VideoState *stream_open(const char *filename, AVInputFormat *iformat)
  2603. {
  2604. VideoState *is;
  2605. is = av_mallocz(sizeof(VideoState));
  2606. if (!is)
  2607. return NULL;
  2608. av_strlcpy(is->filename, filename, sizeof(is->filename));
  2609. is->iformat = iformat;
  2610. is->ytop = 0;
  2611. is->xleft = 0;
  2612. /* start video display */
  2613. is->pictq_mutex = SDL_CreateMutex();
  2614. is->pictq_cond = SDL_CreateCond();
  2615. is->subpq_mutex = SDL_CreateMutex();
  2616. is->subpq_cond = SDL_CreateCond();
  2617. packet_queue_init(&is->videoq);
  2618. packet_queue_init(&is->audioq);
  2619. packet_queue_init(&is->subtitleq);
  2620. is->continue_read_thread = SDL_CreateCond();
  2621. update_external_clock_pts(is, NAN);
  2622. update_external_clock_speed(is, 1.0);
  2623. is->audio_current_pts_drift = -av_gettime() / 1000000.0;
  2624. is->video_current_pts_drift = is->audio_current_pts_drift;
  2625. is->audio_clock_serial = -1;
  2626. is->video_clock_serial = -1;
  2627. is->audio_last_serial = -1;
  2628. is->av_sync_type = av_sync_type;
  2629. is->read_tid = SDL_CreateThread(read_thread, is);
  2630. if (!is->read_tid) {
  2631. av_free(is);
  2632. return NULL;
  2633. }
  2634. return is;
  2635. }
  2636. static void stream_cycle_channel(VideoState *is, int codec_type)
  2637. {
  2638. AVFormatContext *ic = is->ic;
  2639. int start_index, stream_index;
  2640. int old_index;
  2641. AVStream *st;
  2642. if (codec_type == AVMEDIA_TYPE_VIDEO) {
  2643. start_index = is->last_video_stream;
  2644. old_index = is->video_stream;
  2645. } else if (codec_type == AVMEDIA_TYPE_AUDIO) {
  2646. start_index = is->last_audio_stream;
  2647. old_index = is->audio_stream;
  2648. } else {
  2649. start_index = is->last_subtitle_stream;
  2650. old_index = is->subtitle_stream;
  2651. }
  2652. stream_index = start_index;
  2653. for (;;) {
  2654. if (++stream_index >= is->ic->nb_streams)
  2655. {
  2656. if (codec_type == AVMEDIA_TYPE_SUBTITLE)
  2657. {
  2658. stream_index = -1;
  2659. is->last_subtitle_stream = -1;
  2660. goto the_end;
  2661. }
  2662. if (start_index == -1)
  2663. return;
  2664. stream_index = 0;
  2665. }
  2666. if (stream_index == start_index)
  2667. return;
  2668. st = ic->streams[stream_index];
  2669. if (st->codec->codec_type == codec_type) {
  2670. /* check that parameters are OK */
  2671. switch (codec_type) {
  2672. case AVMEDIA_TYPE_AUDIO:
  2673. if (st->codec->sample_rate != 0 &&
  2674. st->codec->channels != 0)
  2675. goto the_end;
  2676. break;
  2677. case AVMEDIA_TYPE_VIDEO:
  2678. case AVMEDIA_TYPE_SUBTITLE:
  2679. goto the_end;
  2680. default:
  2681. break;
  2682. }
  2683. }
  2684. }
  2685. the_end:
  2686. stream_component_close(is, old_index);
  2687. stream_component_open(is, stream_index);
  2688. }
  2689. static void toggle_full_screen(VideoState *is)
  2690. {
  2691. #if defined(__APPLE__) && SDL_VERSION_ATLEAST(1, 2, 14)
  2692. /* OS X needs to reallocate the SDL overlays */
  2693. int i;
  2694. for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++)
  2695. is->pictq[i].reallocate = 1;
  2696. #endif
  2697. is_full_screen = !is_full_screen;
  2698. video_open(is, 1, NULL);
  2699. }
  2700. static void toggle_audio_display(VideoState *is)
  2701. {
  2702. int bgcolor = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
  2703. int next = is->show_mode;
  2704. do {
  2705. next = (next + 1) % SHOW_MODE_NB;
  2706. } while (next != is->show_mode && (next == SHOW_MODE_VIDEO && !is->video_st || next != SHOW_MODE_VIDEO && !is->audio_st));
  2707. if (is->show_mode != next) {
  2708. fill_rectangle(screen,
  2709. is->xleft, is->ytop, is->width, is->height,
  2710. bgcolor, 1);
  2711. is->force_refresh = 1;
  2712. is->show_mode = next;
  2713. }
  2714. }
  2715. static void refresh_loop_wait_event(VideoState *is, SDL_Event *event) {
  2716. double remaining_time = 0.0;
  2717. SDL_PumpEvents();
  2718. while (!SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_ALLEVENTS)) {
  2719. if (!cursor_hidden && av_gettime() - cursor_last_shown > CURSOR_HIDE_DELAY) {
  2720. SDL_ShowCursor(0);
  2721. cursor_hidden = 1;
  2722. }
  2723. if (remaining_time > 0.0)
  2724. av_usleep((int64_t)(remaining_time * 1000000.0));
  2725. remaining_time = REFRESH_RATE;
  2726. if (is->show_mode != SHOW_MODE_NONE && (!is->paused || is->force_refresh))
  2727. video_refresh(is, &remaining_time);
  2728. SDL_PumpEvents();
  2729. }
  2730. }
  2731. /* handle an event sent by the GUI */
  2732. static void event_loop(VideoState *cur_stream)
  2733. {
  2734. SDL_Event event;
  2735. double incr, pos, frac;
  2736. for (;;) {
  2737. double x;
  2738. refresh_loop_wait_event(cur_stream, &event);
  2739. switch (event.type) {
  2740. case SDL_KEYDOWN:
  2741. if (exit_on_keydown) {
  2742. do_exit(cur_stream);
  2743. break;
  2744. }
  2745. switch (event.key.keysym.sym) {
  2746. case SDLK_ESCAPE:
  2747. case SDLK_q:
  2748. do_exit(cur_stream);
  2749. break;
  2750. case SDLK_f:
  2751. toggle_full_screen(cur_stream);
  2752. cur_stream->force_refresh = 1;
  2753. break;
  2754. case SDLK_p:
  2755. case SDLK_SPACE:
  2756. toggle_pause(cur_stream);
  2757. break;
  2758. case SDLK_s: // S: Step to next frame
  2759. step_to_next_frame(cur_stream);
  2760. break;
  2761. case SDLK_a:
  2762. stream_cycle_channel(cur_stream, AVMEDIA_TYPE_AUDIO);
  2763. break;
  2764. case SDLK_v:
  2765. stream_cycle_channel(cur_stream, AVMEDIA_TYPE_VIDEO);
  2766. break;
  2767. case SDLK_t:
  2768. stream_cycle_channel(cur_stream, AVMEDIA_TYPE_SUBTITLE);
  2769. break;
  2770. case SDLK_w:
  2771. toggle_audio_display(cur_stream);
  2772. break;
  2773. case SDLK_PAGEUP:
  2774. incr = 600.0;
  2775. goto do_seek;
  2776. case SDLK_PAGEDOWN:
  2777. incr = -600.0;
  2778. goto do_seek;
  2779. case SDLK_LEFT:
  2780. incr = -10.0;
  2781. goto do_seek;
  2782. case SDLK_RIGHT:
  2783. incr = 10.0;
  2784. goto do_seek;
  2785. case SDLK_UP:
  2786. incr = 60.0;
  2787. goto do_seek;
  2788. case SDLK_DOWN:
  2789. incr = -60.0;
  2790. do_seek:
  2791. if (seek_by_bytes) {
  2792. if (cur_stream->video_stream >= 0 && cur_stream->video_current_pos >= 0) {
  2793. pos = cur_stream->video_current_pos;
  2794. } else if (cur_stream->audio_stream >= 0 && cur_stream->audio_pkt.pos >= 0) {
  2795. pos = cur_stream->audio_pkt.pos;
  2796. } else
  2797. pos = avio_tell(cur_stream->ic->pb);
  2798. if (cur_stream->ic->bit_rate)
  2799. incr *= cur_stream->ic->bit_rate / 8.0;
  2800. else
  2801. incr *= 180000.0;
  2802. pos += incr;
  2803. stream_seek(cur_stream, pos, incr, 1);
  2804. } else {
  2805. pos = get_master_clock(cur_stream);
  2806. if (isnan(pos))
  2807. pos = (double)cur_stream->seek_pos / AV_TIME_BASE;
  2808. pos += incr;
  2809. if (cur_stream->ic->start_time != AV_NOPTS_VALUE && pos < cur_stream->ic->start_time / (double)AV_TIME_BASE)
  2810. pos = cur_stream->ic->start_time / (double)AV_TIME_BASE;
  2811. stream_seek(cur_stream, (int64_t)(pos * AV_TIME_BASE), (int64_t)(incr * AV_TIME_BASE), 0);
  2812. }
  2813. break;
  2814. default:
  2815. break;
  2816. }
  2817. break;
  2818. case SDL_VIDEOEXPOSE:
  2819. cur_stream->force_refresh = 1;
  2820. break;
  2821. case SDL_MOUSEBUTTONDOWN:
  2822. if (exit_on_mousedown) {
  2823. do_exit(cur_stream);
  2824. break;
  2825. }
  2826. case SDL_MOUSEMOTION:
  2827. if (cursor_hidden) {
  2828. SDL_ShowCursor(1);
  2829. cursor_hidden = 0;
  2830. }
  2831. cursor_last_shown = av_gettime();
  2832. if (event.type == SDL_MOUSEBUTTONDOWN) {
  2833. x = event.button.x;
  2834. } else {
  2835. if (event.motion.state != SDL_PRESSED)
  2836. break;
  2837. x = event.motion.x;
  2838. }
  2839. if (seek_by_bytes || cur_stream->ic->duration <= 0) {
  2840. uint64_t size = avio_size(cur_stream->ic->pb);
  2841. stream_seek(cur_stream, size*x/cur_stream->width, 0, 1);
  2842. } else {
  2843. int64_t ts;
  2844. int ns, hh, mm, ss;
  2845. int tns, thh, tmm, tss;
  2846. tns = cur_stream->ic->duration / 1000000LL;
  2847. thh = tns / 3600;
  2848. tmm = (tns % 3600) / 60;
  2849. tss = (tns % 60);
  2850. frac = x / cur_stream->width;
  2851. ns = frac * tns;
  2852. hh = ns / 3600;
  2853. mm = (ns % 3600) / 60;
  2854. ss = (ns % 60);
  2855. fprintf(stderr, "Seek to %2.0f%% (%2d:%02d:%02d) of total duration (%2d:%02d:%02d) \n", frac*100,
  2856. hh, mm, ss, thh, tmm, tss);
  2857. ts = frac * cur_stream->ic->duration;
  2858. if (cur_stream->ic->start_time != AV_NOPTS_VALUE)
  2859. ts += cur_stream->ic->start_time;
  2860. stream_seek(cur_stream, ts, 0, 0);
  2861. }
  2862. break;
  2863. case SDL_VIDEORESIZE:
  2864. screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 0,
  2865. SDL_HWSURFACE|SDL_RESIZABLE|SDL_ASYNCBLIT|SDL_HWACCEL);
  2866. screen_width = cur_stream->width = event.resize.w;
  2867. screen_height = cur_stream->height = event.resize.h;
  2868. cur_stream->force_refresh = 1;
  2869. break;
  2870. case SDL_QUIT:
  2871. case FF_QUIT_EVENT:
  2872. do_exit(cur_stream);
  2873. break;
  2874. case FF_ALLOC_EVENT:
  2875. alloc_picture(event.user.data1);
  2876. break;
  2877. default:
  2878. break;
  2879. }
  2880. }
  2881. }
  2882. static int opt_frame_size(void *optctx, const char *opt, const char *arg)
  2883. {
  2884. av_log(NULL, AV_LOG_WARNING, "Option -s is deprecated, use -video_size.\n");
  2885. return opt_default(NULL, "video_size", arg);
  2886. }
  2887. static int opt_width(void *optctx, const char *opt, const char *arg)
  2888. {
  2889. screen_width = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
  2890. return 0;
  2891. }
  2892. static int opt_height(void *optctx, const char *opt, const char *arg)
  2893. {
  2894. screen_height = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
  2895. return 0;
  2896. }
  2897. static int opt_format(void *optctx, const char *opt, const char *arg)
  2898. {
  2899. file_iformat = av_find_input_format(arg);
  2900. if (!file_iformat) {
  2901. fprintf(stderr, "Unknown input format: %s\n", arg);
  2902. return AVERROR(EINVAL);
  2903. }
  2904. return 0;
  2905. }
  2906. static int opt_frame_pix_fmt(void *optctx, const char *opt, const char *arg)
  2907. {
  2908. av_log(NULL, AV_LOG_WARNING, "Option -pix_fmt is deprecated, use -pixel_format.\n");
  2909. return opt_default(NULL, "pixel_format", arg);
  2910. }
  2911. static int opt_sync(void *optctx, const char *opt, const char *arg)
  2912. {
  2913. if (!strcmp(arg, "audio"))
  2914. av_sync_type = AV_SYNC_AUDIO_MASTER;
  2915. else if (!strcmp(arg, "video"))
  2916. av_sync_type = AV_SYNC_VIDEO_MASTER;
  2917. else if (!strcmp(arg, "ext"))
  2918. av_sync_type = AV_SYNC_EXTERNAL_CLOCK;
  2919. else {
  2920. fprintf(stderr, "Unknown value for %s: %s\n", opt, arg);
  2921. exit(1);
  2922. }
  2923. return 0;
  2924. }
  2925. static int opt_seek(void *optctx, const char *opt, const char *arg)
  2926. {
  2927. start_time = parse_time_or_die(opt, arg, 1);
  2928. return 0;
  2929. }
  2930. static int opt_duration(void *optctx, const char *opt, const char *arg)
  2931. {
  2932. duration = parse_time_or_die(opt, arg, 1);
  2933. return 0;
  2934. }
  2935. static int opt_show_mode(void *optctx, const char *opt, const char *arg)
  2936. {
  2937. show_mode = !strcmp(arg, "video") ? SHOW_MODE_VIDEO :
  2938. !strcmp(arg, "waves") ? SHOW_MODE_WAVES :
  2939. !strcmp(arg, "rdft" ) ? SHOW_MODE_RDFT :
  2940. parse_number_or_die(opt, arg, OPT_INT, 0, SHOW_MODE_NB-1);
  2941. return 0;
  2942. }
  2943. static void opt_input_file(void *optctx, const char *filename)
  2944. {
  2945. if (input_filename) {
  2946. fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n",
  2947. filename, input_filename);
  2948. exit(1);
  2949. }
  2950. if (!strcmp(filename, "-"))
  2951. filename = "pipe:";
  2952. input_filename = filename;
  2953. }
  2954. static int opt_codec(void *optctx, const char *opt, const char *arg)
  2955. {
  2956. const char *spec = strchr(opt, ':');
  2957. if (!spec) {
  2958. fprintf(stderr, "No media specifier was specified in '%s' in option '%s'\n",
  2959. arg, opt);
  2960. return AVERROR(EINVAL);
  2961. }
  2962. spec++;
  2963. switch (spec[0]) {
  2964. case 'a' : audio_codec_name = arg; break;
  2965. case 's' : subtitle_codec_name = arg; break;
  2966. case 'v' : video_codec_name = arg; break;
  2967. default:
  2968. fprintf(stderr, "Invalid media specifier '%s' in option '%s'\n", spec, opt);
  2969. return AVERROR(EINVAL);
  2970. }
  2971. return 0;
  2972. }
  2973. static int dummy;
  2974. static const OptionDef options[] = {
  2975. #include "cmdutils_common_opts.h"
  2976. { "x", HAS_ARG, { .func_arg = opt_width }, "force displayed width", "width" },
  2977. { "y", HAS_ARG, { .func_arg = opt_height }, "force displayed height", "height" },
  2978. { "s", HAS_ARG | OPT_VIDEO, { .func_arg = opt_frame_size }, "set frame size (WxH or abbreviation)", "size" },
  2979. { "fs", OPT_BOOL, { &is_full_screen }, "force full screen" },
  2980. { "an", OPT_BOOL, { &audio_disable }, "disable audio" },
  2981. { "vn", OPT_BOOL, { &video_disable }, "disable video" },
  2982. { "sn", OPT_BOOL, { &subtitle_disable }, "disable subtitling" },
  2983. { "ast", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_AUDIO] }, "select desired audio stream", "stream_number" },
  2984. { "vst", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_VIDEO] }, "select desired video stream", "stream_number" },
  2985. { "sst", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_SUBTITLE] }, "select desired subtitle stream", "stream_number" },
  2986. { "ss", HAS_ARG, { .func_arg = opt_seek }, "seek to a given position in seconds", "pos" },
  2987. { "t", HAS_ARG, { .func_arg = opt_duration }, "play \"duration\" seconds of audio/video", "duration" },
  2988. { "bytes", OPT_INT | HAS_ARG, { &seek_by_bytes }, "seek by bytes 0=off 1=on -1=auto", "val" },
  2989. { "nodisp", OPT_BOOL, { &display_disable }, "disable graphical display" },
  2990. { "f", HAS_ARG, { .func_arg = opt_format }, "force format", "fmt" },
  2991. { "pix_fmt", HAS_ARG | OPT_EXPERT | OPT_VIDEO, { .func_arg = opt_frame_pix_fmt }, "set pixel format", "format" },
  2992. { "stats", OPT_BOOL | OPT_EXPERT, { &show_status }, "show status", "" },
  2993. { "bug", OPT_INT | HAS_ARG | OPT_EXPERT, { &workaround_bugs }, "workaround bugs", "" },
  2994. { "fast", OPT_BOOL | OPT_EXPERT, { &fast }, "non spec compliant optimizations", "" },
  2995. { "genpts", OPT_BOOL | OPT_EXPERT, { &genpts }, "generate pts", "" },
  2996. { "drp", OPT_INT | HAS_ARG | OPT_EXPERT, { &decoder_reorder_pts }, "let decoder reorder pts 0=off 1=on -1=auto", ""},
  2997. { "lowres", OPT_INT | HAS_ARG | OPT_EXPERT, { &lowres }, "", "" },
  2998. { "idct", OPT_INT | HAS_ARG | OPT_EXPERT, { &idct }, "set idct algo", "algo" },
  2999. { "ec", OPT_INT | HAS_ARG | OPT_EXPERT, { &error_concealment }, "set error concealment options", "bit_mask" },
  3000. { "sync", HAS_ARG | OPT_EXPERT, { .func_arg = opt_sync }, "set audio-video sync. type (type=audio/video/ext)", "type" },
  3001. { "autoexit", OPT_BOOL | OPT_EXPERT, { &autoexit }, "exit at the end", "" },
  3002. { "exitonkeydown", OPT_BOOL | OPT_EXPERT, { &exit_on_keydown }, "exit on key down", "" },
  3003. { "exitonmousedown", OPT_BOOL | OPT_EXPERT, { &exit_on_mousedown }, "exit on mouse down", "" },
  3004. { "loop", OPT_INT | HAS_ARG | OPT_EXPERT, { &loop }, "set number of times the playback shall be looped", "loop count" },
  3005. { "framedrop", OPT_BOOL | OPT_EXPERT, { &framedrop }, "drop frames when cpu is too slow", "" },
  3006. { "infbuf", OPT_BOOL | OPT_EXPERT, { &infinite_buffer }, "don't limit the input buffer size (useful with realtime streams)", "" },
  3007. { "window_title", OPT_STRING | HAS_ARG, { &window_title }, "set window title", "window title" },
  3008. #if CONFIG_AVFILTER
  3009. { "vf", OPT_STRING | HAS_ARG, { &vfilters }, "set video filters", "filter_graph" },
  3010. { "af", OPT_STRING | HAS_ARG, { &afilters }, "set audio filters", "filter_graph" },
  3011. #endif
  3012. { "rdftspeed", OPT_INT | HAS_ARG| OPT_AUDIO | OPT_EXPERT, { &rdftspeed }, "rdft speed", "msecs" },
  3013. { "showmode", HAS_ARG, { .func_arg = opt_show_mode}, "select show mode (0 = video, 1 = waves, 2 = RDFT)", "mode" },
  3014. { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, { .func_arg = opt_default }, "generic catch all option", "" },
  3015. { "i", OPT_BOOL, { &dummy}, "read specified file", "input_file"},
  3016. { "codec", HAS_ARG, { .func_arg = opt_codec}, "force decoder", "decoder_name" },
  3017. { "acodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &audio_codec_name }, "force audio decoder", "decoder_name" },
  3018. { "scodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &subtitle_codec_name }, "force subtitle decoder", "decoder_name" },
  3019. { "vcodec", HAS_ARG | OPT_STRING | OPT_EXPERT, { &video_codec_name }, "force video decoder", "decoder_name" },
  3020. { NULL, },
  3021. };
  3022. static void show_usage(void)
  3023. {
  3024. av_log(NULL, AV_LOG_INFO, "Simple media player\n");
  3025. av_log(NULL, AV_LOG_INFO, "usage: %s [options] input_file\n", program_name);
  3026. av_log(NULL, AV_LOG_INFO, "\n");
  3027. }
  3028. void show_help_default(const char *opt, const char *arg)
  3029. {
  3030. av_log_set_callback(log_callback_help);
  3031. show_usage();
  3032. show_help_options(options, "Main options:", 0, OPT_EXPERT, 0);
  3033. show_help_options(options, "Advanced options:", OPT_EXPERT, 0, 0);
  3034. printf("\n");
  3035. show_help_children(avcodec_get_class(), AV_OPT_FLAG_DECODING_PARAM);
  3036. show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
  3037. #if !CONFIG_AVFILTER
  3038. show_help_children(sws_get_class(), AV_OPT_FLAG_ENCODING_PARAM);
  3039. #else
  3040. show_help_children(avfilter_get_class(), AV_OPT_FLAG_FILTERING_PARAM);
  3041. #endif
  3042. printf("\nWhile playing:\n"
  3043. "q, ESC quit\n"
  3044. "f toggle full screen\n"
  3045. "p, SPC pause\n"
  3046. "a cycle audio channel\n"
  3047. "v cycle video channel\n"
  3048. "t cycle subtitle channel\n"
  3049. "w show audio waves\n"
  3050. "s activate frame-step mode\n"
  3051. "left/right seek backward/forward 10 seconds\n"
  3052. "down/up seek backward/forward 1 minute\n"
  3053. "page down/page up seek backward/forward 10 minutes\n"
  3054. "mouse click seek to percentage in file corresponding to fraction of width\n"
  3055. );
  3056. }
  3057. static int lockmgr(void **mtx, enum AVLockOp op)
  3058. {
  3059. switch(op) {
  3060. case AV_LOCK_CREATE:
  3061. *mtx = SDL_CreateMutex();
  3062. if(!*mtx)
  3063. return 1;
  3064. return 0;
  3065. case AV_LOCK_OBTAIN:
  3066. return !!SDL_LockMutex(*mtx);
  3067. case AV_LOCK_RELEASE:
  3068. return !!SDL_UnlockMutex(*mtx);
  3069. case AV_LOCK_DESTROY:
  3070. SDL_DestroyMutex(*mtx);
  3071. return 0;
  3072. }
  3073. return 1;
  3074. }
  3075. /* Called from the main */
  3076. int main(int argc, char **argv)
  3077. {
  3078. int flags;
  3079. VideoState *is;
  3080. char dummy_videodriver[] = "SDL_VIDEODRIVER=dummy";
  3081. av_log_set_flags(AV_LOG_SKIP_REPEATED);
  3082. parse_loglevel(argc, argv, options);
  3083. /* register all codecs, demux and protocols */
  3084. avcodec_register_all();
  3085. #if CONFIG_AVDEVICE
  3086. avdevice_register_all();
  3087. #endif
  3088. #if CONFIG_AVFILTER
  3089. avfilter_register_all();
  3090. #endif
  3091. av_register_all();
  3092. avformat_network_init();
  3093. init_opts();
  3094. signal(SIGINT , sigterm_handler); /* Interrupt (ANSI). */
  3095. signal(SIGTERM, sigterm_handler); /* Termination (ANSI). */
  3096. show_banner(argc, argv, options);
  3097. parse_options(NULL, argc, argv, options, opt_input_file);
  3098. if (!input_filename) {
  3099. show_usage();
  3100. fprintf(stderr, "An input file must be specified\n");
  3101. fprintf(stderr, "Use -h to get full help or, even better, run 'man %s'\n", program_name);
  3102. exit(1);
  3103. }
  3104. if (display_disable) {
  3105. video_disable = 1;
  3106. }
  3107. flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER;
  3108. if (audio_disable)
  3109. flags &= ~SDL_INIT_AUDIO;
  3110. if (display_disable)
  3111. SDL_putenv(dummy_videodriver); /* For the event queue, we always need a video driver. */
  3112. #if !defined(__MINGW32__) && !defined(__APPLE__)
  3113. flags |= SDL_INIT_EVENTTHREAD; /* Not supported on Windows or Mac OS X */
  3114. #endif
  3115. if (SDL_Init (flags)) {
  3116. fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
  3117. fprintf(stderr, "(Did you set the DISPLAY variable?)\n");
  3118. exit(1);
  3119. }
  3120. if (!display_disable) {
  3121. const SDL_VideoInfo *vi = SDL_GetVideoInfo();
  3122. fs_screen_width = vi->current_w;
  3123. fs_screen_height = vi->current_h;
  3124. }
  3125. SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
  3126. SDL_EventState(SDL_SYSWMEVENT, SDL_IGNORE);
  3127. SDL_EventState(SDL_USEREVENT, SDL_IGNORE);
  3128. if (av_lockmgr_register(lockmgr)) {
  3129. fprintf(stderr, "Could not initialize lock manager!\n");
  3130. do_exit(NULL);
  3131. }
  3132. av_init_packet(&flush_pkt);
  3133. flush_pkt.data = (uint8_t *)&flush_pkt;
  3134. is = stream_open(input_filename, file_iformat);
  3135. if (!is) {
  3136. fprintf(stderr, "Failed to initialize VideoState!\n");
  3137. do_exit(NULL);
  3138. }
  3139. event_loop(is);
  3140. /* never returns */
  3141. return 0;
  3142. }