ffmpeg.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488
  1. /*
  2. * FFmpeg main
  3. * Copyright (c) 2000,2001 Gerard Lantau
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #define HAVE_AV_CONFIG_H
  20. #include "avformat.h"
  21. #include "tick.h"
  22. #ifndef CONFIG_WIN32
  23. #include <unistd.h>
  24. #include <fcntl.h>
  25. #include <sys/ioctl.h>
  26. #include <sys/time.h>
  27. #include <termios.h>
  28. #include <time.h>
  29. #include <sys/resource.h>
  30. #include <ctype.h>
  31. #endif
  32. #define MAXINT64 INT64_C(0x7fffffffffffffff)
  33. typedef struct {
  34. const char *name;
  35. int flags;
  36. #define HAS_ARG 0x0001
  37. #define OPT_BOOL 0x0002
  38. #define OPT_EXPERT 0x0004
  39. #define OPT_STRING 0x0008
  40. union {
  41. void (*func_arg)();
  42. int *int_arg;
  43. char **str_arg;
  44. } u;
  45. const char *help;
  46. const char *argname;
  47. } OptionDef;
  48. /* select an input stream for an output stream */
  49. typedef struct AVStreamMap {
  50. int file_index;
  51. int stream_index;
  52. } AVStreamMap;
  53. extern const OptionDef options[];
  54. void show_help(void);
  55. #define MAX_FILES 20
  56. static AVFormatContext *input_files[MAX_FILES];
  57. static int nb_input_files = 0;
  58. static AVFormatContext *output_files[MAX_FILES];
  59. static int nb_output_files = 0;
  60. static AVStreamMap stream_maps[MAX_FILES];
  61. static int nb_stream_maps;
  62. static AVFormat *file_format;
  63. static int frame_width = 160;
  64. static int frame_height = 128;
  65. static int frame_rate = 25 * FRAME_RATE_BASE;
  66. static int video_bit_rate = 200000;
  67. static int video_bit_rate_tolerance = 200000;
  68. static int video_qscale = 0;
  69. static int video_qmin = 3;
  70. static int video_qmax = 15;
  71. static int video_qdiff = 3;
  72. static float video_qblur = 0.5;
  73. static float video_qcomp = 0.5;
  74. static int me_method = 0;
  75. static int video_disable = 0;
  76. static int video_codec_id = CODEC_ID_NONE;
  77. static int same_quality = 0;
  78. static int b_frames = 0;
  79. static int use_hq = 0;
  80. static int use_4mv = 0;
  81. static int do_deinterlace = 0;
  82. static int gop_size = 12;
  83. static int intra_only = 0;
  84. static int audio_sample_rate = 44100;
  85. static int audio_bit_rate = 64000;
  86. static int audio_disable = 0;
  87. static int audio_channels = 1;
  88. static int audio_codec_id = CODEC_ID_NONE;
  89. static INT64 recording_time = 0;
  90. static int file_overwrite = 0;
  91. static char *str_title = NULL;
  92. static char *str_author = NULL;
  93. static char *str_copyright = NULL;
  94. static char *str_comment = NULL;
  95. static int do_benchmark = 0;
  96. static int do_hex_dump = 0;
  97. static int do_play = 0;
  98. static int do_psnr = 0;
  99. static int do_vstats = 0;
  100. static int mpeg_vcd = 0;
  101. typedef struct AVOutputStream {
  102. int file_index; /* file index */
  103. int index; /* stream index in the output file */
  104. int source_index; /* AVInputStream index */
  105. AVStream *st; /* stream in the output file */
  106. int encoding_needed; /* true if encoding needed for this stream */
  107. int fifo_packet_rptr; /* read index in the corresponding
  108. avinputstream packet fifo */
  109. /* video only */
  110. AVPicture pict_tmp; /* temporary image for resizing */
  111. int video_resample;
  112. ImgReSampleContext *img_resample_ctx; /* for image resampling */
  113. /* audio only */
  114. int audio_resample;
  115. ReSampleContext *resample; /* for audio resampling */
  116. FifoBuffer fifo; /* for compression: one audio fifo per codec */
  117. } AVOutputStream;
  118. typedef struct AVInputStream {
  119. int file_index;
  120. int index;
  121. AVStream *st;
  122. int discard; /* true if stream data should be discarded */
  123. int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
  124. Ticker pts_ticker; /* Ticker for PTS calculation */
  125. int ticker_inited; /* to signal if the ticker was initialized */
  126. INT64 pts; /* current pts */
  127. int pts_increment; /* expected pts increment for next packet */
  128. int frame_number; /* current frame */
  129. INT64 sample_index; /* current sample */
  130. } AVInputStream;
  131. typedef struct AVInputFile {
  132. int eof_reached; /* true if eof reached */
  133. int ist_index; /* index of first stream in ist_table */
  134. int buffer_size; /* current total buffer size */
  135. int buffer_size_max; /* buffer size at which we consider we can stop
  136. buffering */
  137. } AVInputFile;
  138. #ifndef CONFIG_WIN32
  139. /* init terminal so that we can grab keys */
  140. static struct termios oldtty;
  141. static void term_exit(void)
  142. {
  143. tcsetattr (0, TCSANOW, &oldtty);
  144. }
  145. static void term_init(void)
  146. {
  147. struct termios tty;
  148. tcgetattr (0, &tty);
  149. oldtty = tty;
  150. tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
  151. |INLCR|IGNCR|ICRNL|IXON);
  152. tty.c_oflag |= OPOST;
  153. tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
  154. tty.c_cflag &= ~(CSIZE|PARENB);
  155. tty.c_cflag |= CS8;
  156. tty.c_cc[VMIN] = 1;
  157. tty.c_cc[VTIME] = 0;
  158. tcsetattr (0, TCSANOW, &tty);
  159. atexit(term_exit);
  160. }
  161. /* read a key without blocking */
  162. static int read_key(void)
  163. {
  164. struct timeval tv;
  165. int n;
  166. unsigned char ch;
  167. fd_set rfds;
  168. FD_ZERO(&rfds);
  169. FD_SET(0, &rfds);
  170. tv.tv_sec = 0;
  171. tv.tv_usec = 0;
  172. n = select(1, &rfds, NULL, NULL, &tv);
  173. if (n > 0) {
  174. if (read(0, &ch, 1) == 1)
  175. return ch;
  176. }
  177. return -1;
  178. }
  179. #else
  180. /* no interactive support */
  181. static void term_exit(void)
  182. {
  183. }
  184. static void term_init(void)
  185. {
  186. }
  187. static int read_key(void)
  188. {
  189. return -1;
  190. }
  191. #endif
  192. int read_ffserver_streams(AVFormatContext *s, const char *filename)
  193. {
  194. int i;
  195. AVFormatContext *ic;
  196. ic = av_open_input_file(filename, NULL, FFM_PACKET_SIZE, NULL);
  197. if (!ic)
  198. return -EIO;
  199. /* copy stream format */
  200. s->nb_streams = ic->nb_streams;
  201. for(i=0;i<ic->nb_streams;i++) {
  202. AVStream *st;
  203. st = av_mallocz(sizeof(AVFormatContext));
  204. memcpy(st, ic->streams[i], sizeof(AVStream));
  205. s->streams[i] = st;
  206. }
  207. av_close_input_file(ic);
  208. return 0;
  209. }
  210. #define MAX_AUDIO_PACKET_SIZE 16384
  211. static void do_audio_out(AVFormatContext *s,
  212. AVOutputStream *ost,
  213. AVInputStream *ist,
  214. unsigned char *buf, int size)
  215. {
  216. UINT8 *buftmp;
  217. UINT8 audio_buf[2*MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it */
  218. UINT8 audio_out[MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it */
  219. int size_out, frame_bytes, ret;
  220. AVCodecContext *enc;
  221. enc = &ost->st->codec;
  222. if (ost->audio_resample) {
  223. buftmp = audio_buf;
  224. size_out = audio_resample(ost->resample,
  225. (short *)buftmp, (short *)buf,
  226. size / (ist->st->codec.channels * 2));
  227. size_out = size_out * enc->channels * 2;
  228. } else {
  229. buftmp = buf;
  230. size_out = size;
  231. }
  232. /* now encode as many frames as possible */
  233. if (enc->frame_size > 1) {
  234. /* output resampled raw samples */
  235. fifo_write(&ost->fifo, buftmp, size_out,
  236. &ost->fifo.wptr);
  237. frame_bytes = enc->frame_size * 2 * enc->channels;
  238. while (fifo_read(&ost->fifo, audio_buf, frame_bytes,
  239. &ost->fifo.rptr) == 0) {
  240. ret = avcodec_encode_audio(enc, audio_out, sizeof(audio_out),
  241. (short *)audio_buf);
  242. s->format->write_packet(s, ost->index, audio_out, ret, 0);
  243. }
  244. } else {
  245. /* output a pcm frame */
  246. /* XXX: change encoding codec API to avoid this ? */
  247. switch(enc->codec->id) {
  248. case CODEC_ID_PCM_S16LE:
  249. case CODEC_ID_PCM_S16BE:
  250. case CODEC_ID_PCM_U16LE:
  251. case CODEC_ID_PCM_U16BE:
  252. break;
  253. default:
  254. size_out = size_out >> 1;
  255. break;
  256. }
  257. ret = avcodec_encode_audio(enc, audio_out, size_out,
  258. (short *)buftmp);
  259. s->format->write_packet(s, ost->index, audio_out, ret, 0);
  260. }
  261. }
  262. /* write a picture to a raw mux */
  263. static void write_picture(AVFormatContext *s, int index, AVPicture *picture,
  264. int pix_fmt, int w, int h)
  265. {
  266. UINT8 *buf, *src, *dest;
  267. int size, j, i;
  268. /* XXX: not efficient, should add test if we can take
  269. directly the AVPicture */
  270. switch(pix_fmt) {
  271. case PIX_FMT_YUV420P:
  272. size = avpicture_get_size(pix_fmt, w, h);
  273. buf = malloc(size);
  274. if (!buf)
  275. return;
  276. dest = buf;
  277. for(i=0;i<3;i++) {
  278. if (i == 1) {
  279. w >>= 1;
  280. h >>= 1;
  281. }
  282. src = picture->data[i];
  283. for(j=0;j<h;j++) {
  284. memcpy(dest, src, w);
  285. dest += w;
  286. src += picture->linesize[i];
  287. }
  288. }
  289. break;
  290. case PIX_FMT_YUV422P:
  291. size = (w * h) * 2;
  292. buf = malloc(size);
  293. if (!buf)
  294. return;
  295. dest = buf;
  296. for(i=0;i<3;i++) {
  297. if (i == 1) {
  298. w >>= 1;
  299. }
  300. src = picture->data[i];
  301. for(j=0;j<h;j++) {
  302. memcpy(dest, src, w);
  303. dest += w;
  304. src += picture->linesize[i];
  305. }
  306. }
  307. break;
  308. case PIX_FMT_YUV444P:
  309. size = (w * h) * 3;
  310. buf = malloc(size);
  311. if (!buf)
  312. return;
  313. dest = buf;
  314. for(i=0;i<3;i++) {
  315. src = picture->data[i];
  316. for(j=0;j<h;j++) {
  317. memcpy(dest, src, w);
  318. dest += w;
  319. src += picture->linesize[i];
  320. }
  321. }
  322. break;
  323. case PIX_FMT_YUV422:
  324. size = (w * h) * 2;
  325. buf = malloc(size);
  326. if (!buf)
  327. return;
  328. dest = buf;
  329. src = picture->data[0];
  330. for(j=0;j<h;j++) {
  331. memcpy(dest, src, w * 2);
  332. dest += w * 2;
  333. src += picture->linesize[0];
  334. }
  335. break;
  336. case PIX_FMT_RGB24:
  337. case PIX_FMT_BGR24:
  338. size = (w * h) * 3;
  339. buf = malloc(size);
  340. if (!buf)
  341. return;
  342. dest = buf;
  343. src = picture->data[0];
  344. for(j=0;j<h;j++) {
  345. memcpy(dest, src, w * 3);
  346. dest += w * 3;
  347. src += picture->linesize[0];
  348. }
  349. break;
  350. default:
  351. return;
  352. }
  353. s->format->write_packet(s, index, buf, size, 0);
  354. free(buf);
  355. }
  356. static void do_video_out(AVFormatContext *s,
  357. AVOutputStream *ost,
  358. AVInputStream *ist,
  359. AVPicture *picture1,
  360. int *frame_size)
  361. {
  362. int n1, n2, nb, i, ret, frame_number, dec_frame_rate;
  363. AVPicture *picture, *picture2, *pict;
  364. AVPicture picture_tmp1, picture_tmp2;
  365. UINT8 video_buffer[1024*1024];
  366. UINT8 *buf = NULL, *buf1 = NULL;
  367. AVCodecContext *enc, *dec;
  368. enc = &ost->st->codec;
  369. dec = &ist->st->codec;
  370. frame_number = ist->frame_number;
  371. dec_frame_rate = ist->st->r_frame_rate;
  372. //fprintf(stderr, "\n%d", dec_frame_rate);
  373. /* first drop frame if needed */
  374. n1 = ((INT64)frame_number * enc->frame_rate) / dec_frame_rate;
  375. n2 = (((INT64)frame_number + 1) * enc->frame_rate) / dec_frame_rate;
  376. nb = n2 - n1;
  377. if (nb <= 0)
  378. return;
  379. /* deinterlace : must be done before any resize */
  380. if (do_deinterlace) {
  381. int size;
  382. /* create temporary picture */
  383. size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
  384. buf1 = malloc(size);
  385. if (!buf1)
  386. return;
  387. picture2 = &picture_tmp2;
  388. avpicture_fill(picture2, buf1, dec->pix_fmt, dec->width, dec->height);
  389. if (avpicture_deinterlace(picture2, picture1,
  390. dec->pix_fmt, dec->width, dec->height) < 0) {
  391. /* if error, do not deinterlace */
  392. free(buf1);
  393. buf1 = NULL;
  394. picture2 = picture1;
  395. }
  396. } else {
  397. picture2 = picture1;
  398. }
  399. /* convert pixel format if needed */
  400. if (enc->pix_fmt != dec->pix_fmt) {
  401. int size;
  402. /* create temporary picture */
  403. size = avpicture_get_size(enc->pix_fmt, dec->width, dec->height);
  404. buf = malloc(size);
  405. if (!buf)
  406. return;
  407. pict = &picture_tmp1;
  408. avpicture_fill(pict, buf, enc->pix_fmt, dec->width, dec->height);
  409. if (img_convert(pict, enc->pix_fmt,
  410. picture2, dec->pix_fmt,
  411. dec->width, dec->height) < 0) {
  412. fprintf(stderr, "pixel format conversion not handled\n");
  413. goto the_end;
  414. }
  415. } else {
  416. pict = picture2;
  417. }
  418. /* XXX: resampling could be done before raw format convertion in
  419. some cases to go faster */
  420. /* XXX: only works for YUV420P */
  421. if (ost->video_resample) {
  422. picture = &ost->pict_tmp;
  423. img_resample(ost->img_resample_ctx, picture, pict);
  424. } else {
  425. picture = pict;
  426. }
  427. nb=1;
  428. /* duplicates frame if needed */
  429. /* XXX: pb because no interleaving */
  430. for(i=0;i<nb;i++) {
  431. if (enc->codec_id != CODEC_ID_RAWVIDEO) {
  432. /* handles sameq here. This is not correct because it may
  433. not be a global option */
  434. if (same_quality) {
  435. enc->quality = dec->quality;
  436. }
  437. ret = avcodec_encode_video(enc,
  438. video_buffer, sizeof(video_buffer),
  439. picture);
  440. //enc->frame_number = enc->real_pict_num;
  441. s->format->write_packet(s, ost->index, video_buffer, ret, 0);
  442. *frame_size = ret;
  443. //fprintf(stderr,"\nFrame: %3d %3d size: %5d type: %d",
  444. // enc->frame_number-1, enc->real_pict_num, ret,
  445. // enc->pict_type);
  446. } else {
  447. write_picture(s, ost->index, picture, enc->pix_fmt, enc->width, enc->height);
  448. }
  449. }
  450. the_end:
  451. if (buf)
  452. free(buf);
  453. if (buf1)
  454. free(buf1);
  455. }
  456. static void do_video_stats(AVOutputStream *ost,
  457. AVInputStream *ist,
  458. int frame_size)
  459. {
  460. static FILE *fvstats=NULL;
  461. static INT64 total_size = 0;
  462. struct tm *today;
  463. time_t today2;
  464. char filename[40];
  465. AVCodecContext *enc;
  466. int frame_number;
  467. INT64 ti;
  468. double ti1, bitrate, avg_bitrate;
  469. if (!fvstats) {
  470. today2 = time(NULL);
  471. today = localtime(&today2);
  472. sprintf(filename, "vstats_%02d%02d%02d.log", today->tm_hour,
  473. today->tm_min,
  474. today->tm_sec);
  475. fvstats = fopen(filename,"w");
  476. if (!fvstats) {
  477. perror("fopen");
  478. exit(1);
  479. }
  480. }
  481. ti = MAXINT64;
  482. enc = &ost->st->codec;
  483. total_size += frame_size;
  484. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  485. frame_number = ist->frame_number;
  486. fprintf(fvstats, "frame= %5d q= %2d ", frame_number, enc->quality);
  487. if (do_psnr)
  488. fprintf(fvstats, "PSNR= %6.2f ", enc->psnr_y);
  489. fprintf(fvstats,"f_size= %6d ", frame_size);
  490. /* compute min pts value */
  491. if (!ist->discard && ist->pts < ti) {
  492. ti = ist->pts;
  493. }
  494. ti1 = (double)ti / 1000000.0;
  495. if (ti1 < 0.01)
  496. ti1 = 0.01;
  497. bitrate = (double)(frame_size * 8) * enc->frame_rate / FRAME_RATE_BASE / 1000.0;
  498. avg_bitrate = (double)(total_size * 8) / ti1 / 1000.0;
  499. fprintf(fvstats, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
  500. (double)total_size / 1024, ti1, bitrate, avg_bitrate);
  501. fprintf(fvstats,"type= %s\n", enc->key_frame == 1 ? "I" : "P");
  502. }
  503. }
  504. static void hex_dump(UINT8 *buf, int size)
  505. {
  506. int len, i, j, c;
  507. for(i=0;i<size;i+=16) {
  508. len = size - i;
  509. if (len > 16)
  510. len = 16;
  511. printf("%08x ", i);
  512. for(j=0;j<16;j++) {
  513. if (j < len)
  514. printf(" %02x", buf[i+j]);
  515. else
  516. printf(" ");
  517. }
  518. printf(" ");
  519. for(j=0;j<len;j++) {
  520. c = buf[i+j];
  521. if (c < ' ' || c > '~')
  522. c = '.';
  523. printf("%c", c);
  524. }
  525. printf("\n");
  526. }
  527. }
  528. /*
  529. * The following code is the main loop of the file converter
  530. */
  531. static int av_encode(AVFormatContext **output_files,
  532. int nb_output_files,
  533. AVFormatContext **input_files,
  534. int nb_input_files,
  535. AVStreamMap *stream_maps, int nb_stream_maps)
  536. {
  537. int ret, i, j, k, n, nb_istreams = 0, nb_ostreams = 0;
  538. AVFormatContext *is, *os;
  539. AVCodecContext *codec, *icodec;
  540. AVOutputStream *ost, **ost_table = NULL;
  541. AVInputStream *ist, **ist_table = NULL;
  542. INT64 min_pts, start_time;
  543. AVInputFile *file_table;
  544. AVFormatContext *stream_no_data;
  545. file_table= (AVInputFile*) av_mallocz(nb_input_files * sizeof(AVInputFile));
  546. if (!file_table)
  547. goto fail;
  548. /* input stream init */
  549. j = 0;
  550. for(i=0;i<nb_input_files;i++) {
  551. is = input_files[i];
  552. file_table[i].ist_index = j;
  553. j += is->nb_streams;
  554. }
  555. nb_istreams = j;
  556. ist_table = av_mallocz(nb_istreams * sizeof(AVInputStream *));
  557. if (!ist_table)
  558. goto fail;
  559. for(i=0;i<nb_istreams;i++) {
  560. ist = av_mallocz(sizeof(AVInputStream));
  561. if (!ist)
  562. goto fail;
  563. ist_table[i] = ist;
  564. }
  565. j = 0;
  566. for(i=0;i<nb_input_files;i++) {
  567. is = input_files[i];
  568. for(k=0;k<is->nb_streams;k++) {
  569. ist = ist_table[j++];
  570. ist->st = is->streams[k];
  571. ist->file_index = i;
  572. ist->index = k;
  573. ist->discard = 1; /* the stream is discarded by default
  574. (changed later) */
  575. }
  576. }
  577. /* output stream init */
  578. nb_ostreams = 0;
  579. for(i=0;i<nb_output_files;i++) {
  580. os = output_files[i];
  581. nb_ostreams += os->nb_streams;
  582. if (mpeg_vcd)
  583. os->flags |= AVF_FLAG_VCD;
  584. }
  585. if (nb_stream_maps > 0 && nb_stream_maps != nb_ostreams) {
  586. fprintf(stderr, "Number of stream maps must match number of output streams\n");
  587. exit(1);
  588. }
  589. ost_table = av_mallocz(sizeof(AVOutputStream *) * nb_ostreams);
  590. if (!ost_table)
  591. goto fail;
  592. for(i=0;i<nb_ostreams;i++) {
  593. ost = av_mallocz(sizeof(AVOutputStream));
  594. if (!ost)
  595. goto fail;
  596. ost_table[i] = ost;
  597. }
  598. n = 0;
  599. for(k=0;k<nb_output_files;k++) {
  600. os = output_files[k];
  601. for(i=0;i<os->nb_streams;i++) {
  602. int found;
  603. ost = ost_table[n++];
  604. ost->file_index = k;
  605. ost->index = i;
  606. ost->st = os->streams[i];
  607. if (nb_stream_maps > 0) {
  608. ost->source_index = file_table[stream_maps[n-1].file_index].ist_index +
  609. stream_maps[n-1].stream_index;
  610. } else {
  611. /* get corresponding input stream index : we select the first one with the right type */
  612. found = 0;
  613. for(j=0;j<nb_istreams;j++) {
  614. ist = ist_table[j];
  615. if (ist->discard &&
  616. ist->st->codec.codec_type == ost->st->codec.codec_type) {
  617. ost->source_index = j;
  618. found = 1;
  619. }
  620. }
  621. if (!found) {
  622. /* try again and reuse existing stream */
  623. for(j=0;j<nb_istreams;j++) {
  624. ist = ist_table[j];
  625. if (ist->st->codec.codec_type == ost->st->codec.codec_type) {
  626. ost->source_index = j;
  627. found = 1;
  628. }
  629. }
  630. if (!found) {
  631. fprintf(stderr, "Could not find input stream matching output stream #%d.%d\n",
  632. ost->file_index, ost->index);
  633. exit(1);
  634. }
  635. }
  636. }
  637. ist = ist_table[ost->source_index];
  638. ist->discard = 0;
  639. }
  640. }
  641. /* dump the stream mapping */
  642. fprintf(stderr, "Stream mapping:\n");
  643. for(i=0;i<nb_ostreams;i++) {
  644. ost = ost_table[i];
  645. fprintf(stderr, " Stream #%d.%d -> #%d.%d\n",
  646. ist_table[ost->source_index]->file_index,
  647. ist_table[ost->source_index]->index,
  648. ost->file_index,
  649. ost->index);
  650. }
  651. /* for each output stream, we compute the right encoding parameters */
  652. for(i=0;i<nb_ostreams;i++) {
  653. ost = ost_table[i];
  654. ist = ist_table[ost->source_index];
  655. codec = &ost->st->codec;
  656. icodec = &ist->st->codec;
  657. switch(codec->codec_type) {
  658. case CODEC_TYPE_AUDIO:
  659. /* check if same codec with same parameters. If so, no
  660. reencoding is needed */
  661. if (codec->codec_id == icodec->codec_id &&
  662. codec->bit_rate == icodec->bit_rate &&
  663. codec->sample_rate == icodec->sample_rate &&
  664. codec->channels == icodec->channels) {
  665. /* no reencoding */
  666. /* use the same frame size */
  667. codec->frame_size = icodec->frame_size;
  668. //codec->frame_size = 8*icodec->sample_rate*icodec->frame_size/
  669. // icodec->bit_rate;
  670. //fprintf(stderr,"\nFrame size: %d", codec->frame_size);
  671. } else {
  672. if (fifo_init(&ost->fifo, 2 * MAX_AUDIO_PACKET_SIZE))
  673. goto fail;
  674. if (codec->channels == icodec->channels &&
  675. codec->sample_rate == icodec->sample_rate) {
  676. ost->audio_resample = 0;
  677. } else {
  678. if (codec->channels != icodec->channels &&
  679. icodec->codec_id == CODEC_ID_AC3) {
  680. /* Special case for 5:1 AC3 input */
  681. /* and mono or stereo output */
  682. /* Request specific number of channels */
  683. icodec->channels = codec->channels;
  684. if (codec->sample_rate == icodec->sample_rate)
  685. ost->audio_resample = 0;
  686. else {
  687. ost->audio_resample = 1;
  688. ost->resample = audio_resample_init(codec->channels, icodec->channels,
  689. codec->sample_rate,
  690. icodec->sample_rate);
  691. }
  692. /* Request specific number of channels */
  693. icodec->channels = codec->channels;
  694. } else {
  695. ost->audio_resample = 1;
  696. ost->resample = audio_resample_init(codec->channels, icodec->channels,
  697. codec->sample_rate,
  698. icodec->sample_rate);
  699. }
  700. }
  701. ist->decoding_needed = 1;
  702. ost->encoding_needed = 1;
  703. }
  704. break;
  705. case CODEC_TYPE_VIDEO:
  706. /* check if same codec with same parameters. If so, no
  707. reencoding is needed */
  708. if (codec->codec_id == icodec->codec_id &&
  709. codec->bit_rate == icodec->bit_rate &&
  710. codec->frame_rate == icodec->frame_rate &&
  711. codec->width == icodec->width &&
  712. codec->height == icodec->height) {
  713. /* no reencoding */
  714. } else {
  715. if (codec->width == icodec->width &&
  716. codec->height == icodec->height) {
  717. ost->video_resample = 0;
  718. } else {
  719. UINT8 *buf;
  720. ost->video_resample = 1;
  721. buf = malloc((codec->width * codec->height * 3) / 2);
  722. if (!buf)
  723. goto fail;
  724. ost->pict_tmp.data[0] = buf;
  725. ost->pict_tmp.data[1] = ost->pict_tmp.data[0] + (codec->width * codec->height);
  726. ost->pict_tmp.data[2] = ost->pict_tmp.data[1] + (codec->width * codec->height) / 4;
  727. ost->pict_tmp.linesize[0] = codec->width;
  728. ost->pict_tmp.linesize[1] = codec->width / 2;
  729. ost->pict_tmp.linesize[2] = codec->width / 2;
  730. ost->img_resample_ctx = img_resample_init(
  731. ost->st->codec.width, ost->st->codec.height,
  732. ist->st->codec.width, ist->st->codec.height);
  733. }
  734. ost->encoding_needed = 1;
  735. ist->decoding_needed = 1;
  736. }
  737. break;
  738. default:
  739. abort();
  740. }
  741. }
  742. /* open each encoder */
  743. for(i=0;i<nb_ostreams;i++) {
  744. ost = ost_table[i];
  745. if (ost->encoding_needed) {
  746. AVCodec *codec;
  747. codec = avcodec_find_encoder(ost->st->codec.codec_id);
  748. if (!codec) {
  749. fprintf(stderr, "Unsupported codec for output stream #%d.%d\n",
  750. ost->file_index, ost->index);
  751. exit(1);
  752. }
  753. if (avcodec_open(&ost->st->codec, codec) < 0) {
  754. fprintf(stderr, "Error while opening codec for stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height\n",
  755. ost->file_index, ost->index);
  756. exit(1);
  757. }
  758. }
  759. }
  760. /* open each decoder */
  761. for(i=0;i<nb_istreams;i++) {
  762. ist = ist_table[i];
  763. if (ist->decoding_needed) {
  764. AVCodec *codec;
  765. codec = avcodec_find_decoder(ist->st->codec.codec_id);
  766. if (!codec) {
  767. fprintf(stderr, "Unsupported codec for input stream #%d.%d\n",
  768. ist->file_index, ist->index);
  769. exit(1);
  770. }
  771. if (avcodec_open(&ist->st->codec, codec) < 0) {
  772. fprintf(stderr, "Error while opening codec for input stream #%d.%d\n",
  773. ist->file_index, ist->index);
  774. exit(1);
  775. }
  776. //if (ist->st->codec.codec_type == CODEC_TYPE_VIDEO)
  777. // ist->st->codec.flags |= CODEC_FLAG_REPEAT_FIELD;
  778. }
  779. }
  780. /* init pts */
  781. for(i=0;i<nb_istreams;i++) {
  782. ist = ist_table[i];
  783. ist->pts = 0;
  784. ist->frame_number = 0;
  785. }
  786. /* compute buffer size max (should use a complete heuristic) */
  787. for(i=0;i<nb_input_files;i++) {
  788. file_table[i].buffer_size_max = 2048;
  789. }
  790. /* open files and write file headers */
  791. for(i=0;i<nb_output_files;i++) {
  792. os = output_files[i];
  793. if (os->format->write_header(os) < 0) {
  794. fprintf(stderr, "Could not write header for output file #%d (incorrect codec paramters ?)\n", i);
  795. ret = -EINVAL;
  796. goto fail;
  797. }
  798. }
  799. #ifndef CONFIG_WIN32
  800. if (!do_play) {
  801. fprintf(stderr, "Press [q] to stop encoding\n");
  802. } else {
  803. fprintf(stderr, "Press [q] to stop playing\n");
  804. }
  805. #endif
  806. term_init();
  807. start_time = gettime();
  808. min_pts = 0;
  809. stream_no_data = 0;
  810. for(;;) {
  811. int file_index, ist_index;
  812. AVPacket pkt;
  813. UINT8 *ptr;
  814. int len;
  815. UINT8 *data_buf;
  816. int data_size, got_picture;
  817. AVPicture picture;
  818. short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
  819. redo:
  820. /* if 'q' pressed, exits */
  821. if (read_key() == 'q')
  822. break;
  823. /* select the input file with the smallest pts */
  824. file_index = -1;
  825. min_pts = MAXINT64;
  826. for(i=0;i<nb_istreams;i++) {
  827. ist = ist_table[i];
  828. /* For some reason, the pts_increment code breaks q estimation?!? */
  829. if (!ist->discard && !file_table[ist->file_index].eof_reached &&
  830. ist->pts /* + ist->pts_increment */ < min_pts && input_files[ist->file_index] != stream_no_data) {
  831. min_pts = ist->pts /* + ist->pts_increment */;
  832. file_index = ist->file_index;
  833. }
  834. }
  835. /* if none, if is finished */
  836. if (file_index < 0) {
  837. if (stream_no_data) {
  838. struct timespec ts;
  839. ts.tv_sec = 0;
  840. ts.tv_nsec = 1000 * 1000 * 10;
  841. nanosleep(&ts, 0);
  842. stream_no_data = 0;
  843. continue;
  844. }
  845. break;
  846. }
  847. /* finish if recording time exhausted */
  848. if (recording_time > 0 && min_pts >= recording_time)
  849. break;
  850. /* read a packet from it and output it in the fifo */
  851. is = input_files[file_index];
  852. if (av_read_packet(is, &pkt) < 0) {
  853. file_table[file_index].eof_reached = 1;
  854. continue;
  855. }
  856. if (!pkt.size) {
  857. stream_no_data = is;
  858. } else {
  859. stream_no_data = 0;
  860. }
  861. ist_index = file_table[file_index].ist_index + pkt.stream_index;
  862. ist = ist_table[ist_index];
  863. if (ist->discard) {
  864. continue;
  865. }
  866. if (pkt.flags & PKT_FLAG_DROPPED_FRAME)
  867. ist->frame_number++;
  868. if (do_hex_dump) {
  869. printf("stream #%d, size=%d:\n", pkt.stream_index, pkt.size);
  870. hex_dump(pkt.data, pkt.size);
  871. }
  872. // printf("read #%d.%d size=%d\n", ist->file_index, ist->index, pkt.size);
  873. len = pkt.size;
  874. ptr = pkt.data;
  875. while (len > 0) {
  876. /* decode the packet if needed */
  877. data_buf = NULL; /* fail safe */
  878. data_size = 0;
  879. if (ist->decoding_needed) {
  880. switch(ist->st->codec.codec_type) {
  881. case CODEC_TYPE_AUDIO:
  882. /* XXX: could avoid copy if PCM 16 bits with same
  883. endianness as CPU */
  884. ret = avcodec_decode_audio(&ist->st->codec, samples, &data_size,
  885. ptr, len);
  886. if (ret < 0)
  887. goto fail_decode;
  888. /* Some bug in mpeg audio decoder gives */
  889. /* data_size < 0, it seems they are overflows */
  890. if (data_size <= 0) {
  891. /* no audio frame */
  892. ptr += ret;
  893. len -= ret;
  894. continue;
  895. }
  896. data_buf = (UINT8 *)samples;
  897. break;
  898. case CODEC_TYPE_VIDEO:
  899. if (ist->st->codec.codec_id == CODEC_ID_RAWVIDEO) {
  900. int size;
  901. size = (ist->st->codec.width * ist->st->codec.height);
  902. avpicture_fill(&picture, ptr,
  903. ist->st->codec.pix_fmt,
  904. ist->st->codec.width,
  905. ist->st->codec.height);
  906. ret = len;
  907. } else {
  908. data_size = (ist->st->codec.width * ist->st->codec.height * 3) / 2;
  909. ret = avcodec_decode_video(&ist->st->codec,
  910. &picture, &got_picture, ptr, len);
  911. if (ret < 0) {
  912. fail_decode:
  913. fprintf(stderr, "Error while decoding stream #%d.%d\n",
  914. ist->file_index, ist->index);
  915. av_free_packet(&pkt);
  916. goto redo;
  917. }
  918. if (!got_picture) {
  919. /* no picture yet */
  920. ptr += ret;
  921. len -= ret;
  922. continue;
  923. }
  924. }
  925. break;
  926. default:
  927. goto fail_decode;
  928. }
  929. } else {
  930. data_buf = ptr;
  931. data_size = len;
  932. ret = len;
  933. }
  934. /* init tickers */
  935. if (!ist->ticker_inited) {
  936. switch (ist->st->codec.codec_type) {
  937. case CODEC_TYPE_AUDIO:
  938. ticker_init(&ist->pts_ticker,
  939. (INT64)ist->st->codec.sample_rate,
  940. (INT64)(1000000));
  941. ist->ticker_inited = 1;
  942. break;
  943. case CODEC_TYPE_VIDEO:
  944. ticker_init(&ist->pts_ticker,
  945. (INT64)ist->st->r_frame_rate,
  946. ((INT64)1000000 * FRAME_RATE_BASE));
  947. ist->ticker_inited = 1;
  948. break;
  949. default:
  950. abort();
  951. }
  952. }
  953. /* update pts */
  954. switch(ist->st->codec.codec_type) {
  955. case CODEC_TYPE_AUDIO:
  956. //ist->pts = (INT64)1000000 * ist->sample_index / ist->st->codec.sample_rate;
  957. ist->pts = ticker_tick(&ist->pts_ticker, ist->sample_index);
  958. ist->sample_index += data_size / (2 * ist->st->codec.channels);
  959. ist->pts_increment = (INT64) (data_size / (2 * ist->st->codec.channels)) * 1000000 / ist->st->codec.sample_rate;
  960. break;
  961. case CODEC_TYPE_VIDEO:
  962. ist->frame_number++;
  963. //ist->pts = ((INT64)ist->frame_number * 1000000 * FRAME_RATE_BASE) /
  964. // ist->st->codec.frame_rate;
  965. ist->pts = ticker_tick(&ist->pts_ticker, ist->frame_number);
  966. ist->pts_increment = ((INT64) 1000000 * FRAME_RATE_BASE) /
  967. ist->st->codec.frame_rate;
  968. break;
  969. default:
  970. abort();
  971. }
  972. ptr += ret;
  973. len -= ret;
  974. /* transcode raw format, encode packets and output them */
  975. for(i=0;i<nb_ostreams;i++) {
  976. int frame_size;
  977. ost = ost_table[i];
  978. if (ost->source_index == ist_index) {
  979. os = output_files[ost->file_index];
  980. if (ost->encoding_needed) {
  981. switch(ost->st->codec.codec_type) {
  982. case CODEC_TYPE_AUDIO:
  983. do_audio_out(os, ost, ist, data_buf, data_size);
  984. break;
  985. case CODEC_TYPE_VIDEO:
  986. do_video_out(os, ost, ist, &picture, &frame_size);
  987. if (do_vstats)
  988. do_video_stats(ost, ist, frame_size);
  989. break;
  990. default:
  991. abort();
  992. }
  993. } else {
  994. /* no reencoding needed : output the packet directly */
  995. /* force the input stream PTS */
  996. os->format->write_packet(os, ost->index, data_buf, data_size, pkt.pts);
  997. }
  998. }
  999. }
  1000. }
  1001. av_free_packet(&pkt);
  1002. /* dump report by using the first video and audio streams */
  1003. {
  1004. char buf[1024];
  1005. AVFormatContext *oc;
  1006. INT64 total_size, ti;
  1007. AVCodecContext *enc;
  1008. int frame_number, vid;
  1009. double bitrate, ti1;
  1010. static INT64 last_time;
  1011. if ((min_pts - last_time) >= 500000) {
  1012. last_time = min_pts;
  1013. oc = output_files[0];
  1014. total_size = url_ftell(&oc->pb);
  1015. buf[0] = '\0';
  1016. ti = MAXINT64;
  1017. vid = 0;
  1018. for(i=0;i<nb_ostreams;i++) {
  1019. ost = ost_table[i];
  1020. enc = &ost->st->codec;
  1021. ist = ist_table[ost->source_index];
  1022. if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
  1023. frame_number = ist->frame_number;
  1024. sprintf(buf + strlen(buf), "frame=%5d q=%2d ",
  1025. frame_number, enc->quality);
  1026. if (do_psnr)
  1027. sprintf(buf + strlen(buf), "PSNR=%6.2f ", enc->psnr_y);
  1028. vid = 1;
  1029. }
  1030. /* compute min pts value */
  1031. if (!ist->discard && ist->pts < ti) {
  1032. ti = ist->pts;
  1033. }
  1034. }
  1035. ti1 = (double)ti / 1000000.0;
  1036. if (ti1 < 0.01)
  1037. ti1 = 0.01;
  1038. bitrate = (double)(total_size * 8) / ti1 / 1000.0;
  1039. sprintf(buf + strlen(buf),
  1040. "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
  1041. (double)total_size / 1024, ti1, bitrate);
  1042. fprintf(stderr, "%s \r", buf);
  1043. fflush(stderr);
  1044. }
  1045. }
  1046. }
  1047. term_exit();
  1048. /* dump report by using the first video and audio streams */
  1049. {
  1050. char buf[1024];
  1051. AVFormatContext *oc;
  1052. INT64 total_size, ti;
  1053. AVCodecContext *enc;
  1054. int frame_number, vid;
  1055. double bitrate, ti1;
  1056. oc = output_files[0];
  1057. total_size = url_ftell(&oc->pb);
  1058. buf[0] = '\0';
  1059. ti = MAXINT64;
  1060. vid = 0;
  1061. for(i=0;i<nb_ostreams;i++) {
  1062. ost = ost_table[i];
  1063. enc = &ost->st->codec;
  1064. ist = ist_table[ost->source_index];
  1065. if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
  1066. frame_number = ist->frame_number;
  1067. sprintf(buf + strlen(buf), "frame=%5d q=%2d ",
  1068. frame_number, enc->quality);
  1069. if (do_psnr)
  1070. sprintf(buf + strlen(buf), "PSNR=%6.2f ", enc->psnr_y);
  1071. vid = 1;
  1072. }
  1073. /* compute min pts value */
  1074. if (!ist->discard && ist->pts < ti) {
  1075. ti = ist->pts;
  1076. }
  1077. }
  1078. ti1 = ti / 1000000.0;
  1079. if (ti1 < 0.01)
  1080. ti1 = 0.01;
  1081. bitrate = (double)(total_size * 8) / ti1 / 1000.0;
  1082. sprintf(buf + strlen(buf),
  1083. "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
  1084. (double)total_size / 1024, ti1, bitrate);
  1085. fprintf(stderr, "%s \n", buf);
  1086. }
  1087. /* close each encoder */
  1088. for(i=0;i<nb_ostreams;i++) {
  1089. ost = ost_table[i];
  1090. if (ost->encoding_needed) {
  1091. avcodec_close(&ost->st->codec);
  1092. }
  1093. }
  1094. /* close each decoder */
  1095. for(i=0;i<nb_istreams;i++) {
  1096. ist = ist_table[i];
  1097. if (ist->decoding_needed) {
  1098. avcodec_close(&ist->st->codec);
  1099. }
  1100. }
  1101. /* write the trailer if needed and close file */
  1102. for(i=0;i<nb_output_files;i++) {
  1103. os = output_files[i];
  1104. os->format->write_trailer(os);
  1105. }
  1106. /* finished ! */
  1107. ret = 0;
  1108. fail1:
  1109. free(file_table);
  1110. if (ist_table) {
  1111. for(i=0;i<nb_istreams;i++) {
  1112. ist = ist_table[i];
  1113. if (ist) {
  1114. free(ist);
  1115. }
  1116. }
  1117. free(ist_table);
  1118. }
  1119. if (ost_table) {
  1120. for(i=0;i<nb_ostreams;i++) {
  1121. ost = ost_table[i];
  1122. if (ost) {
  1123. if (ost->pict_tmp.data[0])
  1124. free(ost->pict_tmp.data[0]);
  1125. if (ost->video_resample)
  1126. img_resample_close(ost->img_resample_ctx);
  1127. if (ost->audio_resample)
  1128. audio_resample_close(ost->resample);
  1129. free(ost);
  1130. }
  1131. }
  1132. free(ost_table);
  1133. }
  1134. return ret;
  1135. fail:
  1136. ret = -ENOMEM;
  1137. goto fail1;
  1138. }
  1139. #if 0
  1140. int file_read(const char *filename)
  1141. {
  1142. URLContext *h;
  1143. unsigned char buffer[1024];
  1144. int len, i;
  1145. if (url_open(&h, filename, O_RDONLY) < 0) {
  1146. printf("could not open '%s'\n", filename);
  1147. return -1;
  1148. }
  1149. for(;;) {
  1150. len = url_read(h, buffer, sizeof(buffer));
  1151. if (len <= 0)
  1152. break;
  1153. for(i=0;i<len;i++) putchar(buffer[i]);
  1154. }
  1155. url_close(h);
  1156. return 0;
  1157. }
  1158. #endif
  1159. void show_licence(void)
  1160. {
  1161. printf(
  1162. "ffmpeg version " FFMPEG_VERSION "\n"
  1163. "Copyright (c) 2000,2001 Gerard Lantau\n"
  1164. "This program is free software; you can redistribute it and/or modify\n"
  1165. "it under the terms of the GNU General Public License as published by\n"
  1166. "the Free Software Foundation; either version 2 of the License, or\n"
  1167. "(at your option) any later version.\n"
  1168. "\n"
  1169. "This program is distributed in the hope that it will be useful,\n"
  1170. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  1171. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  1172. "GNU General Public License for more details.\n"
  1173. "\n"
  1174. "You should have received a copy of the GNU General Public License\n"
  1175. "along with this program; if not, write to the Free Software\n"
  1176. "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n"
  1177. );
  1178. exit(1);
  1179. }
  1180. void opt_format(const char *arg)
  1181. {
  1182. AVFormat *f;
  1183. f = first_format;
  1184. while (f != NULL && strcmp(f->name, arg) != 0) f = f->next;
  1185. if (f == NULL) {
  1186. fprintf(stderr, "Invalid format: %s\n", arg);
  1187. exit(1);
  1188. }
  1189. file_format = f;
  1190. }
  1191. void opt_video_bitrate(const char *arg)
  1192. {
  1193. video_bit_rate = atoi(arg) * 1000;
  1194. }
  1195. void opt_video_bitrate_tolerance(const char *arg)
  1196. {
  1197. video_bit_rate_tolerance = atoi(arg) * 1000;
  1198. }
  1199. void opt_frame_rate(const char *arg)
  1200. {
  1201. frame_rate = (int)(strtod(arg, 0) * FRAME_RATE_BASE);
  1202. }
  1203. void opt_frame_size(const char *arg)
  1204. {
  1205. parse_image_size(&frame_width, &frame_height, arg);
  1206. if (frame_width <= 0 || frame_height <= 0) {
  1207. fprintf(stderr, "Incorrect frame size\n");
  1208. exit(1);
  1209. }
  1210. if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
  1211. fprintf(stderr, "Frame size must be a multiple of 2\n");
  1212. exit(1);
  1213. }
  1214. }
  1215. void opt_gop_size(const char *arg)
  1216. {
  1217. gop_size = atoi(arg);
  1218. }
  1219. void opt_b_frames(const char *arg)
  1220. {
  1221. b_frames = atoi(arg);
  1222. if (b_frames > FF_MAX_B_FRAMES) {
  1223. fprintf(stderr, "\nCannot have more than %d B frames, increase FF_MAX_B_FRAMES.\n", FF_MAX_B_FRAMES);
  1224. exit(1);
  1225. } else if (b_frames < 1) {
  1226. fprintf(stderr, "\nNumber of B frames must be higher than 0\n");
  1227. exit(1);
  1228. }
  1229. }
  1230. void opt_qscale(const char *arg)
  1231. {
  1232. video_qscale = atoi(arg);
  1233. if (video_qscale < 0 ||
  1234. video_qscale > 31) {
  1235. fprintf(stderr, "qscale must be >= 1 and <= 31\n");
  1236. exit(1);
  1237. }
  1238. }
  1239. void opt_qmin(const char *arg)
  1240. {
  1241. video_qmin = atoi(arg);
  1242. if (video_qmin < 0 ||
  1243. video_qmin > 31) {
  1244. fprintf(stderr, "qmin must be >= 1 and <= 31\n");
  1245. exit(1);
  1246. }
  1247. }
  1248. void opt_qmax(const char *arg)
  1249. {
  1250. video_qmax = atoi(arg);
  1251. if (video_qmax < 0 ||
  1252. video_qmax > 31) {
  1253. fprintf(stderr, "qmax must be >= 1 and <= 31\n");
  1254. exit(1);
  1255. }
  1256. }
  1257. void opt_qdiff(const char *arg)
  1258. {
  1259. video_qdiff = atoi(arg);
  1260. if (video_qdiff < 0 ||
  1261. video_qdiff > 31) {
  1262. fprintf(stderr, "qdiff must be >= 1 and <= 31\n");
  1263. exit(1);
  1264. }
  1265. }
  1266. void opt_qblur(const char *arg)
  1267. {
  1268. video_qblur = atof(arg);
  1269. }
  1270. void opt_qcomp(const char *arg)
  1271. {
  1272. video_qcomp = atof(arg);
  1273. }
  1274. void opt_audio_bitrate(const char *arg)
  1275. {
  1276. audio_bit_rate = atoi(arg) * 1000;
  1277. }
  1278. void opt_audio_rate(const char *arg)
  1279. {
  1280. audio_sample_rate = atoi(arg);
  1281. }
  1282. void opt_audio_channels(const char *arg)
  1283. {
  1284. audio_channels = atoi(arg);
  1285. }
  1286. #ifdef CONFIG_GRAB
  1287. void opt_video_device(const char *arg)
  1288. {
  1289. v4l_device = strdup(arg);
  1290. }
  1291. void opt_audio_device(const char *arg)
  1292. {
  1293. audio_device = strdup(arg);
  1294. }
  1295. #endif
  1296. void opt_audio_codec(const char *arg)
  1297. {
  1298. AVCodec *p;
  1299. p = first_avcodec;
  1300. while (p) {
  1301. if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
  1302. break;
  1303. p = p->next;
  1304. }
  1305. if (p == NULL) {
  1306. fprintf(stderr, "Unknown audio codec '%s'\n", arg);
  1307. exit(1);
  1308. } else {
  1309. audio_codec_id = p->id;
  1310. }
  1311. }
  1312. const char *motion_str[] = {
  1313. "zero",
  1314. "full",
  1315. "log",
  1316. "phods",
  1317. "epzs",
  1318. "x1",
  1319. NULL,
  1320. };
  1321. void opt_motion_estimation(const char *arg)
  1322. {
  1323. const char **p;
  1324. p = motion_str;
  1325. for(;;) {
  1326. if (!*p) {
  1327. fprintf(stderr, "Unknown motion estimation method '%s'\n", arg);
  1328. exit(1);
  1329. }
  1330. if (!strcmp(*p, arg))
  1331. break;
  1332. p++;
  1333. }
  1334. me_method = (p - motion_str) + 1;
  1335. }
  1336. void opt_video_codec(const char *arg)
  1337. {
  1338. AVCodec *p;
  1339. p = first_avcodec;
  1340. while (p) {
  1341. if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
  1342. break;
  1343. p = p->next;
  1344. }
  1345. if (p == NULL) {
  1346. fprintf(stderr, "Unknown video codec '%s'\n", arg);
  1347. exit(1);
  1348. } else {
  1349. video_codec_id = p->id;
  1350. }
  1351. }
  1352. void opt_map(const char *arg)
  1353. {
  1354. AVStreamMap *m;
  1355. const char *p;
  1356. p = arg;
  1357. m = &stream_maps[nb_stream_maps++];
  1358. m->file_index = strtol(arg, (char **)&p, 0);
  1359. if (*p)
  1360. p++;
  1361. m->stream_index = strtol(p, (char **)&p, 0);
  1362. }
  1363. void opt_recording_time(const char *arg)
  1364. {
  1365. recording_time = parse_date(arg, 1);
  1366. }
  1367. /* return the number of packet read to find the codec parameters */
  1368. int find_codec_parameters(AVFormatContext *ic)
  1369. {
  1370. int val, i, count, ret, got_picture, size;
  1371. AVCodec *codec;
  1372. AVCodecContext *enc;
  1373. AVStream *st;
  1374. AVPacket *pkt;
  1375. AVPicture picture;
  1376. AVPacketList *pktl=NULL, **ppktl;
  1377. short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
  1378. UINT8 *ptr;
  1379. count = 0;
  1380. ppktl = &ic->packet_buffer;
  1381. for(;;) {
  1382. for(i=0;i<ic->nb_streams;i++) {
  1383. enc = &ic->streams[i]->codec;
  1384. switch(enc->codec_type) {
  1385. case CODEC_TYPE_AUDIO:
  1386. val = enc->sample_rate;
  1387. break;
  1388. case CODEC_TYPE_VIDEO:
  1389. val = enc->width;
  1390. break;
  1391. default:
  1392. val = 1;
  1393. break;
  1394. }
  1395. /* if no parameters supplied, then we should read it from
  1396. the stream */
  1397. if (val == 0)
  1398. break;
  1399. }
  1400. if (i == ic->nb_streams) {
  1401. ret = count;
  1402. break;
  1403. }
  1404. if (count == 0) {
  1405. /* open each codec */
  1406. for(i=0;i<ic->nb_streams;i++) {
  1407. st = ic->streams[i];
  1408. codec = avcodec_find_decoder(st->codec.codec_id);
  1409. if (codec == NULL) {
  1410. ret = -1;
  1411. goto the_end;
  1412. }
  1413. ret = avcodec_open(&st->codec, codec);
  1414. if (ret < 0)
  1415. goto the_end;
  1416. }
  1417. }
  1418. pktl = av_mallocz(sizeof(AVPacketList));
  1419. if (!pktl) {
  1420. ret = -1;
  1421. break;
  1422. }
  1423. /* add the packet in the buffered packet list */
  1424. *ppktl = pktl;
  1425. ppktl = &pktl->next;
  1426. pkt = &pktl->pkt;
  1427. if (ic->format->read_packet(ic, pkt) < 0) {
  1428. ret = -1;
  1429. break;
  1430. }
  1431. st = ic->streams[pkt->stream_index];
  1432. /* decode the data and update codec parameters */
  1433. ptr = pkt->data;
  1434. size = pkt->size;
  1435. while (size > 0) {
  1436. switch(st->codec.codec_type) {
  1437. case CODEC_TYPE_VIDEO:
  1438. ret = avcodec_decode_video(&st->codec, &picture, &got_picture, ptr, size);
  1439. if (st->codec.codec_id == CODEC_ID_MPEG1VIDEO) {
  1440. //mpegvid = pkt->stream_index;
  1441. //fps = st->codec.frame_rate;
  1442. }
  1443. break;
  1444. case CODEC_TYPE_AUDIO:
  1445. ret = avcodec_decode_audio(&st->codec, samples, &got_picture, ptr, size);
  1446. break;
  1447. default:
  1448. ret = -1;
  1449. break;
  1450. }
  1451. if (ret < 0) {
  1452. ret = -1;
  1453. goto the_end;
  1454. }
  1455. if (got_picture)
  1456. break;
  1457. ptr += ret;
  1458. size -= ret;
  1459. }
  1460. count++;
  1461. }
  1462. the_end:
  1463. if (count > 0) {
  1464. /* close each codec */
  1465. for(i=0;i<ic->nb_streams;i++) {
  1466. st = ic->streams[i];
  1467. avcodec_close(&st->codec);
  1468. }
  1469. }
  1470. return ret;
  1471. }
  1472. /* Returns the real frame rate of telecine streams */
  1473. int get_real_fps(AVFormatContext *ic, AVFormat *fmt, AVFormatParameters *ap, int stream_id)
  1474. {
  1475. int frame_num, r_frames, fps, rfps;
  1476. int ret, got_picture, size;
  1477. UINT8 *ptr;
  1478. AVStream *st;
  1479. AVPacket *pkt;
  1480. AVPicture picture;
  1481. AVPacketList *pktl=NULL, **ppktl=NULL;
  1482. AVCodec *codec;
  1483. AVFormatContext *fc;
  1484. frame_num = 0;
  1485. r_frames = 0;
  1486. fps = rfps = -1;
  1487. if (stream_id < 0 || stream_id >= ic->nb_streams)
  1488. return -1;
  1489. /* We must use another AVFormatContext, and open the
  1490. file again, since we do not have good seeking */
  1491. fc = av_mallocz(sizeof(AVFormatContext));
  1492. if (!fc)
  1493. goto the_end;
  1494. strcpy(fc->filename, ic->filename);
  1495. /* open file */
  1496. if (!(fmt->flags & AVFMT_NOFILE)) {
  1497. if (url_fopen(&fc->pb, fc->filename, URL_RDONLY) < 0) {
  1498. fprintf(stderr, "Could not open '%s'\n", fc->filename);
  1499. exit(1);
  1500. }
  1501. }
  1502. /* check format */
  1503. if (!fmt) {
  1504. goto the_end;
  1505. }
  1506. fc->format = fmt;
  1507. /* Read header */
  1508. ret = fc->format->read_header(fc, ap);
  1509. if (ret < 0) {
  1510. fprintf(stderr, "%s: Error while parsing header\n", fc->filename);
  1511. goto the_end;
  1512. }
  1513. /* Find and open codec */
  1514. st = fc->streams[stream_id];
  1515. codec = avcodec_find_decoder(st->codec.codec_id);
  1516. if (codec == NULL) {
  1517. goto the_end;
  1518. }
  1519. ret = avcodec_open(&st->codec, codec);
  1520. if (ret < 0)
  1521. goto the_end;
  1522. ppktl = &fc->packet_buffer;
  1523. if (stream_id > -1) {
  1524. /* check telecine MPEG video streams, we */
  1525. /* decode 40 frames to get the real fps */
  1526. while(1) {
  1527. pktl = av_mallocz(sizeof(AVPacketList));
  1528. if (!pktl) {
  1529. goto the_end;
  1530. break;
  1531. }
  1532. /* add the packet in the buffered packet list */
  1533. *ppktl = pktl;
  1534. ppktl = &pktl->next;
  1535. pkt = &pktl->pkt;
  1536. if (fc->format->read_packet(fc, pkt) < 0) {
  1537. goto the_end;
  1538. break;
  1539. }
  1540. st = fc->streams[pkt->stream_index];
  1541. /* decode the video */
  1542. ptr = pkt->data;
  1543. size = pkt->size;
  1544. while (size > 0) {
  1545. if (pkt->stream_index == stream_id) {
  1546. ret = avcodec_decode_video(&st->codec, &picture, &got_picture, ptr, size);
  1547. fps = st->codec.frame_rate;
  1548. if (ret < 0) {
  1549. goto the_end;
  1550. }
  1551. if (got_picture) {
  1552. frame_num++;
  1553. r_frames += st->codec.repeat_pict;
  1554. }
  1555. }
  1556. ptr += ret;
  1557. size -= ret;
  1558. }
  1559. if (frame_num > 39)
  1560. break;
  1561. }
  1562. rfps = (fps * frame_num) / (frame_num + (r_frames >> 1));
  1563. /* close codec */
  1564. avcodec_close(&st->codec);
  1565. }
  1566. the_end:
  1567. /* FIXME: leak in packet_buffer */
  1568. if (fc)
  1569. free(fc);
  1570. return rfps;
  1571. }
  1572. int filename_number_test(const char *filename)
  1573. {
  1574. char buf[1024];
  1575. if (get_frame_filename(buf, sizeof(buf), filename, 1) < 0) {
  1576. fprintf(stderr, "%s: Incorrect image filename syntax.\n"
  1577. "Use '%%d' to specify the image number:\n"
  1578. " for img1.jpg, img2.jpg, ..., use 'img%%d.jpg';\n"
  1579. " for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n",
  1580. filename);
  1581. return -1;
  1582. } else {
  1583. return 0;
  1584. }
  1585. }
  1586. void opt_input_file(const char *filename)
  1587. {
  1588. AVFormatContext *ic;
  1589. AVFormatParameters params, *ap = &params;
  1590. URLFormat url_format;
  1591. AVFormat *fmt;
  1592. int err, i, ret, rfps;
  1593. ic = av_mallocz(sizeof(AVFormatContext));
  1594. strcpy(ic->filename, filename);
  1595. /* first format guess to know if we must open file */
  1596. fmt = file_format;
  1597. if (!fmt)
  1598. fmt = guess_format(NULL, filename, NULL);
  1599. if (fmt == NULL || !(fmt->flags & AVFMT_NOFILE)) {
  1600. /* open file */
  1601. if (url_fopen(&ic->pb, filename, URL_RDONLY) < 0) {
  1602. fprintf(stderr, "Could not open '%s'\n", filename);
  1603. exit(1);
  1604. }
  1605. /* find format and set default parameters */
  1606. fmt = file_format;
  1607. err = url_getformat(url_fileno(&ic->pb), &url_format);
  1608. if (err >= 0) {
  1609. if (!fmt)
  1610. fmt = guess_format(url_format.format_name, NULL, NULL);
  1611. ap->sample_rate = url_format.sample_rate;
  1612. ap->frame_rate = url_format.frame_rate;
  1613. ap->channels = url_format.channels;
  1614. ap->width = url_format.width;
  1615. ap->height = url_format.height;
  1616. ap->pix_fmt = url_format.pix_fmt;
  1617. } else {
  1618. if (!fmt)
  1619. fmt = guess_format(NULL, filename, NULL);
  1620. memset(ap, 0, sizeof(*ap));
  1621. }
  1622. } else {
  1623. memset(ap, 0, sizeof(*ap));
  1624. }
  1625. if (!fmt || !fmt->read_header) {
  1626. fprintf(stderr, "%s: Unknown file format\n", filename);
  1627. exit(1);
  1628. }
  1629. ic->format = fmt;
  1630. /* get default parameters from command line */
  1631. if (!ap->sample_rate)
  1632. ap->sample_rate = audio_sample_rate;
  1633. if (!ap->channels)
  1634. ap->channels = audio_channels;
  1635. if (!ap->frame_rate)
  1636. ap->frame_rate = frame_rate;
  1637. if (!ap->width)
  1638. ap->width = frame_width;
  1639. if (!ap->height)
  1640. ap->height = frame_height;
  1641. /* check filename in case of an image number is expected */
  1642. if (ic->format->flags & AVFMT_NEEDNUMBER) {
  1643. if (filename_number_test(ic->filename) < 0)
  1644. exit(1);
  1645. }
  1646. err = ic->format->read_header(ic, ap);
  1647. if (err < 0) {
  1648. fprintf(stderr, "%s: Error while parsing header\n", filename);
  1649. exit(1);
  1650. }
  1651. /* If not enough info for the codecs, we decode the first frames
  1652. to get it. (used in mpeg case for example) */
  1653. ret = find_codec_parameters(ic);
  1654. if (ret < 0) {
  1655. fprintf(stderr, "%s: could not find codec parameters\n", filename);
  1656. exit(1);
  1657. }
  1658. /* update the current parameters so that they match the one of the input stream */
  1659. for(i=0;i<ic->nb_streams;i++) {
  1660. AVCodecContext *enc = &ic->streams[i]->codec;
  1661. switch(enc->codec_type) {
  1662. case CODEC_TYPE_AUDIO:
  1663. //fprintf(stderr, "\nInput Audio channels: %d", enc->channels);
  1664. audio_channels = enc->channels;
  1665. audio_sample_rate = enc->sample_rate;
  1666. break;
  1667. case CODEC_TYPE_VIDEO:
  1668. frame_height = enc->height;
  1669. frame_width = enc->width;
  1670. rfps = enc->frame_rate;
  1671. if (enc->codec_id == CODEC_ID_MPEG1VIDEO) {
  1672. rfps = get_real_fps(ic, fmt, ap, i);
  1673. }
  1674. if (rfps > 0 && rfps != enc->frame_rate) {
  1675. frame_rate = rfps;
  1676. fprintf(stderr,"\nSeems that stream %d comes from film source: %2.2f->%2.2f\n",
  1677. i, (float)enc->frame_rate / FRAME_RATE_BASE,
  1678. (float)rfps / FRAME_RATE_BASE);
  1679. } else
  1680. frame_rate = enc->frame_rate;
  1681. ic->streams[i]->r_frame_rate = rfps;
  1682. break;
  1683. default:
  1684. abort();
  1685. }
  1686. }
  1687. input_files[nb_input_files] = ic;
  1688. /* dump the file content */
  1689. dump_format(ic, nb_input_files, filename, 0);
  1690. nb_input_files++;
  1691. file_format = NULL;
  1692. }
  1693. void check_audio_video_inputs(int *has_video_ptr, int *has_audio_ptr)
  1694. {
  1695. int has_video, has_audio, i, j;
  1696. AVFormatContext *ic;
  1697. has_video = 0;
  1698. has_audio = 0;
  1699. for(j=0;j<nb_input_files;j++) {
  1700. ic = input_files[j];
  1701. for(i=0;i<ic->nb_streams;i++) {
  1702. AVCodecContext *enc = &ic->streams[i]->codec;
  1703. switch(enc->codec_type) {
  1704. case CODEC_TYPE_AUDIO:
  1705. has_audio = 1;
  1706. break;
  1707. case CODEC_TYPE_VIDEO:
  1708. has_video = 1;
  1709. break;
  1710. default:
  1711. abort();
  1712. }
  1713. }
  1714. }
  1715. *has_video_ptr = has_video;
  1716. *has_audio_ptr = has_audio;
  1717. }
  1718. void opt_output_file(const char *filename)
  1719. {
  1720. AVStream *st;
  1721. AVFormatContext *oc;
  1722. int use_video, use_audio, nb_streams, input_has_video, input_has_audio;
  1723. int codec_id;
  1724. if (!strcmp(filename, "-"))
  1725. filename = "pipe:";
  1726. oc = av_mallocz(sizeof(AVFormatContext));
  1727. if (!file_format) {
  1728. file_format = guess_format(NULL, filename, NULL);
  1729. if (!file_format)
  1730. file_format = &mpeg_mux_format;
  1731. }
  1732. oc->format = file_format;
  1733. if (!strcmp(file_format->name, "ffm") &&
  1734. strstart(filename, "http:", NULL)) {
  1735. /* special case for files sent to ffserver: we get the stream
  1736. parameters from ffserver */
  1737. if (read_ffserver_streams(oc, filename) < 0) {
  1738. fprintf(stderr, "Could not read stream parameters from '%s'\n", filename);
  1739. exit(1);
  1740. }
  1741. } else {
  1742. use_video = file_format->video_codec != CODEC_ID_NONE;
  1743. use_audio = file_format->audio_codec != CODEC_ID_NONE;
  1744. /* disable if no corresponding type found and at least one
  1745. input file */
  1746. if (nb_input_files > 0) {
  1747. check_audio_video_inputs(&input_has_video, &input_has_audio);
  1748. if (!input_has_video)
  1749. use_video = 0;
  1750. if (!input_has_audio)
  1751. use_audio = 0;
  1752. }
  1753. /* manual disable */
  1754. if (audio_disable) {
  1755. use_audio = 0;
  1756. }
  1757. if (video_disable) {
  1758. use_video = 0;
  1759. }
  1760. nb_streams = 0;
  1761. if (use_video) {
  1762. AVCodecContext *video_enc;
  1763. st = av_mallocz(sizeof(AVStream));
  1764. if (!st) {
  1765. fprintf(stderr, "Could not alloc stream\n");
  1766. exit(1);
  1767. }
  1768. video_enc = &st->codec;
  1769. codec_id = file_format->video_codec;
  1770. if (video_codec_id != CODEC_ID_NONE)
  1771. codec_id = video_codec_id;
  1772. video_enc->codec_id = codec_id;
  1773. video_enc->codec_type = CODEC_TYPE_VIDEO;
  1774. video_enc->bit_rate = video_bit_rate;
  1775. video_enc->bit_rate_tolerance = video_bit_rate_tolerance;
  1776. video_enc->frame_rate = frame_rate;
  1777. video_enc->width = frame_width;
  1778. video_enc->height = frame_height;
  1779. if (!intra_only)
  1780. video_enc->gop_size = gop_size;
  1781. else
  1782. video_enc->gop_size = 0;
  1783. if (video_qscale || same_quality) {
  1784. video_enc->flags |= CODEC_FLAG_QSCALE;
  1785. video_enc->quality = video_qscale;
  1786. }
  1787. if (use_hq) {
  1788. video_enc->flags |= CODEC_FLAG_HQ;
  1789. }
  1790. if (use_4mv) {
  1791. video_enc->flags |= CODEC_FLAG_HQ;
  1792. video_enc->flags |= CODEC_FLAG_4MV;
  1793. }
  1794. if (b_frames) {
  1795. if (codec_id != CODEC_ID_MPEG4) {
  1796. fprintf(stderr, "\nB frames encoding only supported by MPEG-4.\n");
  1797. exit(1);
  1798. }
  1799. video_enc->max_b_frames = b_frames;
  1800. video_enc->b_frame_strategy = 0;
  1801. video_enc->b_quant_factor = 2.0;
  1802. }
  1803. video_enc->qmin = video_qmin;
  1804. video_enc->qmax = video_qmax;
  1805. video_enc->max_qdiff = video_qdiff;
  1806. video_enc->qblur = video_qblur;
  1807. video_enc->qcompress = video_qcomp;
  1808. if (do_psnr)
  1809. video_enc->get_psnr = 1;
  1810. else
  1811. video_enc->get_psnr = 0;
  1812. video_enc->me_method = me_method;
  1813. /* XXX: need to find a way to set codec parameters */
  1814. if (oc->format == &ppm_format ||
  1815. oc->format == &ppmpipe_format) {
  1816. video_enc->pix_fmt = PIX_FMT_RGB24;
  1817. }
  1818. oc->streams[nb_streams] = st;
  1819. nb_streams++;
  1820. }
  1821. if (use_audio) {
  1822. AVCodecContext *audio_enc;
  1823. st = av_mallocz(sizeof(AVStream));
  1824. if (!st) {
  1825. fprintf(stderr, "Could not alloc stream\n");
  1826. exit(1);
  1827. }
  1828. audio_enc = &st->codec;
  1829. codec_id = file_format->audio_codec;
  1830. if (audio_codec_id != CODEC_ID_NONE)
  1831. codec_id = audio_codec_id;
  1832. audio_enc->codec_id = codec_id;
  1833. audio_enc->codec_type = CODEC_TYPE_AUDIO;
  1834. audio_enc->bit_rate = audio_bit_rate;
  1835. audio_enc->sample_rate = audio_sample_rate;
  1836. /* For audio codecs other than AC3 we limit */
  1837. /* the number of coded channels to stereo */
  1838. if (audio_channels > 2 && codec_id != CODEC_ID_AC3) {
  1839. audio_enc->channels = 2;
  1840. } else
  1841. audio_enc->channels = audio_channels;
  1842. oc->streams[nb_streams] = st;
  1843. nb_streams++;
  1844. }
  1845. oc->nb_streams = nb_streams;
  1846. if (!nb_streams) {
  1847. fprintf(stderr, "No audio or video streams available\n");
  1848. exit(1);
  1849. }
  1850. if (str_title)
  1851. nstrcpy(oc->title, sizeof(oc->title), str_title);
  1852. if (str_author)
  1853. nstrcpy(oc->author, sizeof(oc->author), str_author);
  1854. if (str_copyright)
  1855. nstrcpy(oc->copyright, sizeof(oc->copyright), str_copyright);
  1856. if (str_comment)
  1857. nstrcpy(oc->comment, sizeof(oc->comment), str_comment);
  1858. }
  1859. output_files[nb_output_files] = oc;
  1860. /* dump the file content */
  1861. dump_format(oc, nb_output_files, filename, 1);
  1862. nb_output_files++;
  1863. strcpy(oc->filename, filename);
  1864. /* check filename in case of an image number is expected */
  1865. if (oc->format->flags & AVFMT_NEEDNUMBER) {
  1866. if (filename_number_test(oc->filename) < 0)
  1867. exit(1);
  1868. }
  1869. if (!(oc->format->flags & AVFMT_NOFILE)) {
  1870. /* test if it already exists to avoid loosing precious files */
  1871. if (!file_overwrite &&
  1872. (strchr(filename, ':') == NULL ||
  1873. strstart(filename, "file:", NULL))) {
  1874. if (url_exist(filename)) {
  1875. int c;
  1876. printf("File '%s' already exists. Overwrite ? [y/N] ", filename);
  1877. fflush(stdout);
  1878. c = getchar();
  1879. if (toupper(c) != 'Y') {
  1880. fprintf(stderr, "Not overwriting - exiting\n");
  1881. exit(1);
  1882. }
  1883. }
  1884. }
  1885. /* open the file */
  1886. if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
  1887. fprintf(stderr, "Could not open '%s'\n", filename);
  1888. exit(1);
  1889. }
  1890. }
  1891. /* reset some options */
  1892. file_format = NULL;
  1893. audio_disable = 0;
  1894. video_disable = 0;
  1895. audio_codec_id = CODEC_ID_NONE;
  1896. video_codec_id = CODEC_ID_NONE;
  1897. }
  1898. #ifdef CONFIG_GRAB
  1899. /* prepare dummy protocols for grab */
  1900. void prepare_grab(void)
  1901. {
  1902. int has_video, has_audio, i, j;
  1903. AVFormatContext *oc;
  1904. AVFormatContext *ic;
  1905. AVFormatParameters ap1, *ap = &ap1;
  1906. /* see if audio/video inputs are needed */
  1907. has_video = 0;
  1908. has_audio = 0;
  1909. memset(ap, 0, sizeof(*ap));
  1910. for(j=0;j<nb_output_files;j++) {
  1911. oc = output_files[j];
  1912. for(i=0;i<oc->nb_streams;i++) {
  1913. AVCodecContext *enc = &oc->streams[i]->codec;
  1914. switch(enc->codec_type) {
  1915. case CODEC_TYPE_AUDIO:
  1916. if (enc->sample_rate > ap->sample_rate)
  1917. ap->sample_rate = enc->sample_rate;
  1918. if (enc->channels > ap->channels)
  1919. ap->channels = enc->channels;
  1920. has_audio = 1;
  1921. break;
  1922. case CODEC_TYPE_VIDEO:
  1923. if (enc->width > ap->width)
  1924. ap->width = enc->width;
  1925. if (enc->height > ap->height)
  1926. ap->height = enc->height;
  1927. if (enc->frame_rate > ap->frame_rate)
  1928. ap->frame_rate = enc->frame_rate;
  1929. has_video = 1;
  1930. break;
  1931. default:
  1932. abort();
  1933. }
  1934. }
  1935. }
  1936. if (has_video == 0 && has_audio == 0) {
  1937. fprintf(stderr, "Output file must have at least one audio or video stream\n");
  1938. exit(1);
  1939. }
  1940. if (has_video) {
  1941. ic = av_open_input_file("", "video_grab_device", 0, ap);
  1942. /* by now video grab has one stream */
  1943. ic->streams[0]->r_frame_rate = ap->frame_rate;
  1944. if (!ic) {
  1945. fprintf(stderr, "Could not open video grab device\n");
  1946. exit(1);
  1947. }
  1948. input_files[nb_input_files] = ic;
  1949. dump_format(ic, nb_input_files, v4l_device, 0);
  1950. nb_input_files++;
  1951. }
  1952. if (has_audio) {
  1953. ic = av_open_input_file("", "audio_device", 0, ap);
  1954. if (!ic) {
  1955. fprintf(stderr, "Could not open audio grab device\n");
  1956. exit(1);
  1957. }
  1958. input_files[nb_input_files] = ic;
  1959. dump_format(ic, nb_input_files, audio_device, 0);
  1960. nb_input_files++;
  1961. }
  1962. }
  1963. #else
  1964. void prepare_grab(void)
  1965. {
  1966. fprintf(stderr, "Must supply at least one input file\n");
  1967. exit(1);
  1968. }
  1969. #endif
  1970. /* open the necessary output devices for playing */
  1971. void prepare_play(void)
  1972. {
  1973. #ifndef __BEOS__
  1974. file_format = guess_format("audio_device", NULL, NULL);
  1975. if (!file_format) {
  1976. fprintf(stderr, "Could not find audio device\n");
  1977. exit(1);
  1978. }
  1979. opt_output_file(audio_device);
  1980. #endif
  1981. }
  1982. #ifndef CONFIG_WIN32
  1983. INT64 getutime(void)
  1984. {
  1985. struct rusage rusage;
  1986. getrusage(RUSAGE_SELF, &rusage);
  1987. return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
  1988. }
  1989. #else
  1990. INT64 getutime(void)
  1991. {
  1992. return gettime();
  1993. }
  1994. #endif
  1995. void show_formats(void)
  1996. {
  1997. AVFormat *f;
  1998. URLProtocol *up;
  1999. AVCodec *p;
  2000. const char **pp;
  2001. printf("File formats:\n");
  2002. printf(" Encoding:");
  2003. for(f = first_format; f != NULL; f = f->next) {
  2004. if (f->write_header)
  2005. printf(" %s", f->name);
  2006. }
  2007. printf("\n");
  2008. printf(" Decoding:");
  2009. for(f = first_format; f != NULL; f = f->next) {
  2010. if (f->read_header)
  2011. printf(" %s", f->name);
  2012. }
  2013. printf("\n");
  2014. printf("Codecs:\n");
  2015. printf(" Encoders:");
  2016. for(p = first_avcodec; p != NULL; p = p->next) {
  2017. if (p->encode)
  2018. printf(" %s", p->name);
  2019. }
  2020. printf("\n");
  2021. printf(" Decoders:");
  2022. for(p = first_avcodec; p != NULL; p = p->next) {
  2023. if (p->decode)
  2024. printf(" %s", p->name);
  2025. }
  2026. printf("\n");
  2027. printf("Supported file protocols:");
  2028. for(up = first_protocol; up != NULL; up = up->next)
  2029. printf(" %s:", up->name);
  2030. printf("\n");
  2031. printf("Frame size abbreviations: sqcif qcif cif 4cif\n");
  2032. printf("Motion estimation methods:");
  2033. pp = motion_str;
  2034. while (*pp) {
  2035. printf(" %s", *pp);
  2036. if ((pp - motion_str + 1) == ME_ZERO)
  2037. printf("(fastest)");
  2038. else if ((pp - motion_str + 1) == ME_FULL)
  2039. printf("(slowest)");
  2040. else if ((pp - motion_str + 1) == ME_EPZS)
  2041. printf("(default)");
  2042. pp++;
  2043. }
  2044. printf("\n");
  2045. exit(1);
  2046. }
  2047. void show_help(void)
  2048. {
  2049. const char *prog;
  2050. const OptionDef *po;
  2051. int i, expert;
  2052. prog = do_play ? "ffplay" : "ffmpeg";
  2053. printf("%s version " FFMPEG_VERSION ", Copyright (c) 2000, 2001 Gerard Lantau\n",
  2054. prog);
  2055. if (!do_play) {
  2056. printf("usage: ffmpeg [[options] -i input_file]... {[options] outfile}...\n"
  2057. "Hyper fast MPEG1/MPEG4/H263/RV and AC3/MPEG audio encoder\n");
  2058. } else {
  2059. printf("usage: ffplay [options] input_file...\n"
  2060. "Simple audio player\n");
  2061. }
  2062. printf("\n"
  2063. "Main options are:\n");
  2064. for(i=0;i<2;i++) {
  2065. if (i == 1)
  2066. printf("\nAdvanced options are:\n");
  2067. for(po = options; po->name != NULL; po++) {
  2068. char buf[64];
  2069. expert = (po->flags & OPT_EXPERT) != 0;
  2070. if (expert == i) {
  2071. strcpy(buf, po->name);
  2072. if (po->flags & HAS_ARG) {
  2073. strcat(buf, " ");
  2074. strcat(buf, po->argname);
  2075. }
  2076. printf("-%-17s %s\n", buf, po->help);
  2077. }
  2078. }
  2079. }
  2080. exit(1);
  2081. }
  2082. const OptionDef options[] = {
  2083. { "L", 0, {(void*)show_licence}, "show license" },
  2084. { "h", 0, {(void*)show_help}, "show help" },
  2085. { "formats", 0, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
  2086. { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
  2087. { "vcd", OPT_BOOL, {(void*)&mpeg_vcd}, "output Video CD MPEG-PS compliant file" },
  2088. { "i", HAS_ARG, {(void*)opt_input_file}, "input file name", "filename" },
  2089. { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" },
  2090. { "map", HAS_ARG | OPT_EXPERT, {(void*)opt_map}, "set input stream mapping", "file:stream" },
  2091. { "t", HAS_ARG, {(void*)opt_recording_time}, "set the recording time", "duration" },
  2092. { "title", HAS_ARG | OPT_STRING, {(void*)&str_title}, "set the title", "string" },
  2093. { "author", HAS_ARG | OPT_STRING, {(void*)&str_author}, "set the author", "string" },
  2094. { "copyright", HAS_ARG | OPT_STRING, {(void*)&str_copyright}, "set the copyright", "string" },
  2095. { "comment", HAS_ARG | OPT_STRING, {(void*)&str_comment}, "set the comment", "string" },
  2096. /* video options */
  2097. { "b", HAS_ARG, {(void*)opt_video_bitrate}, "set video bitrate (in kbit/s)", "bitrate" },
  2098. { "r", HAS_ARG, {(void*)opt_frame_rate}, "set frame rate (in Hz)", "rate" },
  2099. { "s", HAS_ARG, {(void*)opt_frame_size}, "set frame size (WxH or abbreviation)", "size" },
  2100. { "g", HAS_ARG | OPT_EXPERT, {(void*)opt_gop_size}, "set the group of picture size", "gop_size" },
  2101. { "intra", OPT_BOOL | OPT_EXPERT, {(void*)&intra_only}, "use only intra frames"},
  2102. { "vn", OPT_BOOL, {(void*)&video_disable}, "disable video" },
  2103. { "qscale", HAS_ARG | OPT_EXPERT, {(void*)opt_qscale}, "use fixed video quantiser scale (VBR)", "q" },
  2104. { "qmin", HAS_ARG | OPT_EXPERT, {(void*)opt_qmin}, "min video quantiser scale (VBR)", "q" },
  2105. { "qmax", HAS_ARG | OPT_EXPERT, {(void*)opt_qmax}, "max video quantiser scale (VBR)", "q" },
  2106. { "qdiff", HAS_ARG | OPT_EXPERT, {(void*)opt_qdiff}, "max difference between the quantiser scale (VBR)", "q" },
  2107. { "qblur", HAS_ARG | OPT_EXPERT, {(void*)opt_qblur}, "video quantiser scale blur (VBR)", "blur" },
  2108. { "qcomp", HAS_ARG | OPT_EXPERT, {(void*)opt_qcomp}, "video quantiser scale compression (VBR)", "compression" },
  2109. { "bt", HAS_ARG, {(void*)opt_video_bitrate_tolerance}, "set video bitrate tolerance (in kbit/s)", "tolerance" },
  2110. #ifdef CONFIG_GRAB
  2111. { "vd", HAS_ARG | OPT_EXPERT, {(void*)opt_video_device}, "set video device", "device" },
  2112. #endif
  2113. { "vcodec", HAS_ARG | OPT_EXPERT, {(void*)opt_video_codec}, "force video codec", "codec" },
  2114. { "me", HAS_ARG | OPT_EXPERT, {(void*)opt_motion_estimation}, "set motion estimation method",
  2115. "method" },
  2116. { "bf", HAS_ARG | OPT_EXPERT, {(void*)opt_b_frames}, "use 'frames' B frames (only MPEG-4)", "frames" },
  2117. { "hq", OPT_BOOL | OPT_EXPERT, {(void*)&use_hq}, "activate high quality settings" },
  2118. { "4mv", OPT_BOOL | OPT_EXPERT, {(void*)&use_4mv}, "use four motion vector by macroblock (only MPEG-4)" },
  2119. { "sameq", OPT_BOOL, {(void*)&same_quality},
  2120. "use same video quality as source (implies VBR)" },
  2121. /* audio options */
  2122. { "ab", HAS_ARG, {(void*)opt_audio_bitrate}, "set audio bitrate (in kbit/s)", "bitrate", },
  2123. { "ar", HAS_ARG, {(void*)opt_audio_rate}, "set audio sampling rate (in Hz)", "rate" },
  2124. { "ac", HAS_ARG, {(void*)opt_audio_channels}, "set number of audio channels", "channels" },
  2125. { "an", OPT_BOOL, {(void*)&audio_disable}, "disable audio" },
  2126. #ifdef CONFIG_GRAB
  2127. { "ad", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_device}, "set audio device", "device" },
  2128. #endif
  2129. { "acodec", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_codec}, "force audio codec", "codec" },
  2130. { "deinterlace", OPT_BOOL | OPT_EXPERT, {(void*)&do_deinterlace},
  2131. "deinterlace pictures" },
  2132. { "benchmark", OPT_BOOL | OPT_EXPERT, {(void*)&do_benchmark},
  2133. "add timings for benchmarking" },
  2134. { "hex", OPT_BOOL | OPT_EXPERT, {(void*)&do_hex_dump},
  2135. "dump each input packet" },
  2136. { "psnr", OPT_BOOL | OPT_EXPERT, {(void*)&do_psnr}, "calculate PSNR of compressed frames" },
  2137. { "vstats", OPT_BOOL | OPT_EXPERT, {(void*)&do_vstats}, "dump video coding statistics to file" },
  2138. { NULL, },
  2139. };
  2140. int main(int argc, char **argv)
  2141. {
  2142. int optindex, i;
  2143. const char *opt, *arg;
  2144. const OptionDef *po;
  2145. INT64 ti;
  2146. register_all();
  2147. /* detect if invoked as player */
  2148. i = strlen(argv[0]);
  2149. if (i >= 6 && !strcmp(argv[0] + i - 6, "ffplay"))
  2150. do_play = 1;
  2151. if (argc <= 1)
  2152. show_help();
  2153. /* parse options */
  2154. optindex = 1;
  2155. while (optindex < argc) {
  2156. opt = argv[optindex++];
  2157. if (opt[0] == '-' && opt[1] != '\0') {
  2158. po = options;
  2159. while (po->name != NULL) {
  2160. if (!strcmp(opt + 1, po->name))
  2161. break;
  2162. po++;
  2163. }
  2164. if (!po->name) {
  2165. fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
  2166. exit(1);
  2167. }
  2168. arg = NULL;
  2169. if (po->flags & HAS_ARG)
  2170. arg = argv[optindex++];
  2171. if (po->flags & OPT_STRING) {
  2172. char *str;
  2173. str = strdup(arg);
  2174. *po->u.str_arg = str;
  2175. } else if (po->flags & OPT_BOOL) {
  2176. *po->u.int_arg = 1;
  2177. } else {
  2178. po->u.func_arg(arg);
  2179. }
  2180. } else {
  2181. if (!do_play) {
  2182. opt_output_file(opt);
  2183. } else {
  2184. opt_input_file(opt);
  2185. }
  2186. }
  2187. }
  2188. if (!do_play) {
  2189. /* file converter / grab */
  2190. if (nb_output_files <= 0) {
  2191. fprintf(stderr, "Must supply at least one output file\n");
  2192. exit(1);
  2193. }
  2194. if (nb_input_files == 0) {
  2195. prepare_grab();
  2196. }
  2197. } else {
  2198. /* player */
  2199. if (nb_input_files <= 0) {
  2200. fprintf(stderr, "Must supply at least one input file\n");
  2201. exit(1);
  2202. }
  2203. prepare_play();
  2204. }
  2205. ti = getutime();
  2206. av_encode(output_files, nb_output_files, input_files, nb_input_files,
  2207. stream_maps, nb_stream_maps);
  2208. ti = getutime() - ti;
  2209. if (do_benchmark) {
  2210. printf("bench: utime=%0.3fs\n", ti / 1000000.0);
  2211. }
  2212. /* close files */
  2213. for(i=0;i<nb_output_files;i++) {
  2214. if (!(output_files[i]->format->flags & AVFMT_NOFILE))
  2215. url_fclose(&output_files[i]->pb);
  2216. }
  2217. for(i=0;i<nb_input_files;i++) {
  2218. if (!(input_files[i]->format->flags & AVFMT_NOFILE))
  2219. url_fclose(&input_files[i]->pb);
  2220. }
  2221. return 0;
  2222. }