You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

5025 lines
175KB

  1. /*
  2. * Copyright (c) 2000-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. * multimedia converter based on the FFmpeg libraries
  23. */
  24. #include "config.h"
  25. #include <ctype.h>
  26. #include <string.h>
  27. #include <math.h>
  28. #include <stdlib.h>
  29. #include <errno.h>
  30. #include <limits.h>
  31. #include <stdatomic.h>
  32. #include <stdint.h>
  33. #if HAVE_IO_H
  34. #include <io.h>
  35. #endif
  36. #if HAVE_UNISTD_H
  37. #include <unistd.h>
  38. #endif
  39. #include "libavformat/avformat.h"
  40. #include "libavdevice/avdevice.h"
  41. #include "libswresample/swresample.h"
  42. #include "libavutil/opt.h"
  43. #include "libavutil/channel_layout.h"
  44. #include "libavutil/parseutils.h"
  45. #include "libavutil/samplefmt.h"
  46. #include "libavutil/fifo.h"
  47. #include "libavutil/hwcontext.h"
  48. #include "libavutil/internal.h"
  49. #include "libavutil/intreadwrite.h"
  50. #include "libavutil/dict.h"
  51. #include "libavutil/display.h"
  52. #include "libavutil/mathematics.h"
  53. #include "libavutil/pixdesc.h"
  54. #include "libavutil/avstring.h"
  55. #include "libavutil/libm.h"
  56. #include "libavutil/imgutils.h"
  57. #include "libavutil/timestamp.h"
  58. #include "libavutil/bprint.h"
  59. #include "libavutil/time.h"
  60. #include "libavutil/thread.h"
  61. #include "libavutil/threadmessage.h"
  62. #include "libavcodec/mathops.h"
  63. #include "libavformat/os_support.h"
  64. # include "libavfilter/avfilter.h"
  65. # include "libavfilter/buffersrc.h"
  66. # include "libavfilter/buffersink.h"
  67. #if HAVE_SYS_RESOURCE_H
  68. #include <sys/time.h>
  69. #include <sys/types.h>
  70. #include <sys/resource.h>
  71. #elif HAVE_GETPROCESSTIMES
  72. #include <windows.h>
  73. #endif
  74. #if HAVE_GETPROCESSMEMORYINFO
  75. #include <windows.h>
  76. #include <psapi.h>
  77. #endif
  78. #if HAVE_SETCONSOLECTRLHANDLER
  79. #include <windows.h>
  80. #endif
  81. #if HAVE_SYS_SELECT_H
  82. #include <sys/select.h>
  83. #endif
  84. #if HAVE_TERMIOS_H
  85. #include <fcntl.h>
  86. #include <sys/ioctl.h>
  87. #include <sys/time.h>
  88. #include <termios.h>
  89. #elif HAVE_KBHIT
  90. #include <conio.h>
  91. #endif
  92. #include <time.h>
  93. #include "ffmpeg.h"
  94. #include "cmdutils.h"
  95. #include "libavutil/avassert.h"
  96. const char program_name[] = "ffmpeg";
  97. const int program_birth_year = 2000;
  98. static FILE *vstats_file;
  99. const char *const forced_keyframes_const_names[] = {
  100. "n",
  101. "n_forced",
  102. "prev_forced_n",
  103. "prev_forced_t",
  104. "t",
  105. NULL
  106. };
  107. typedef struct BenchmarkTimeStamps {
  108. int64_t real_usec;
  109. int64_t user_usec;
  110. int64_t sys_usec;
  111. } BenchmarkTimeStamps;
  112. static void do_video_stats(OutputStream *ost, int frame_size);
  113. static BenchmarkTimeStamps get_benchmark_time_stamps(void);
  114. static int64_t getmaxrss(void);
  115. static int ifilter_has_all_input_formats(FilterGraph *fg);
  116. static int run_as_daemon = 0;
  117. static int nb_frames_dup = 0;
  118. static unsigned dup_warning = 1000;
  119. static int nb_frames_drop = 0;
  120. static int64_t decode_error_stat[2];
  121. static unsigned nb_output_dumped = 0;
  122. static int want_sdp = 1;
  123. static BenchmarkTimeStamps current_time;
  124. AVIOContext *progress_avio = NULL;
  125. static uint8_t *subtitle_out;
  126. InputStream **input_streams = NULL;
  127. int nb_input_streams = 0;
  128. InputFile **input_files = NULL;
  129. int nb_input_files = 0;
  130. OutputStream **output_streams = NULL;
  131. int nb_output_streams = 0;
  132. OutputFile **output_files = NULL;
  133. int nb_output_files = 0;
  134. FilterGraph **filtergraphs;
  135. int nb_filtergraphs;
  136. #if HAVE_TERMIOS_H
  137. /* init terminal so that we can grab keys */
  138. static struct termios oldtty;
  139. static int restore_tty;
  140. #endif
  141. #if HAVE_THREADS
  142. static void free_input_threads(void);
  143. #endif
  144. /* sub2video hack:
  145. Convert subtitles to video with alpha to insert them in filter graphs.
  146. This is a temporary solution until libavfilter gets real subtitles support.
  147. */
  148. static int sub2video_get_blank_frame(InputStream *ist)
  149. {
  150. int ret;
  151. AVFrame *frame = ist->sub2video.frame;
  152. av_frame_unref(frame);
  153. ist->sub2video.frame->width = ist->dec_ctx->width ? ist->dec_ctx->width : ist->sub2video.w;
  154. ist->sub2video.frame->height = ist->dec_ctx->height ? ist->dec_ctx->height : ist->sub2video.h;
  155. ist->sub2video.frame->format = AV_PIX_FMT_RGB32;
  156. if ((ret = av_frame_get_buffer(frame, 0)) < 0)
  157. return ret;
  158. memset(frame->data[0], 0, frame->height * frame->linesize[0]);
  159. return 0;
  160. }
  161. static void sub2video_copy_rect(uint8_t *dst, int dst_linesize, int w, int h,
  162. AVSubtitleRect *r)
  163. {
  164. uint32_t *pal, *dst2;
  165. uint8_t *src, *src2;
  166. int x, y;
  167. if (r->type != SUBTITLE_BITMAP) {
  168. av_log(NULL, AV_LOG_WARNING, "sub2video: non-bitmap subtitle\n");
  169. return;
  170. }
  171. if (r->x < 0 || r->x + r->w > w || r->y < 0 || r->y + r->h > h) {
  172. av_log(NULL, AV_LOG_WARNING, "sub2video: rectangle (%d %d %d %d) overflowing %d %d\n",
  173. r->x, r->y, r->w, r->h, w, h
  174. );
  175. return;
  176. }
  177. dst += r->y * dst_linesize + r->x * 4;
  178. src = r->data[0];
  179. pal = (uint32_t *)r->data[1];
  180. for (y = 0; y < r->h; y++) {
  181. dst2 = (uint32_t *)dst;
  182. src2 = src;
  183. for (x = 0; x < r->w; x++)
  184. *(dst2++) = pal[*(src2++)];
  185. dst += dst_linesize;
  186. src += r->linesize[0];
  187. }
  188. }
  189. static void sub2video_push_ref(InputStream *ist, int64_t pts)
  190. {
  191. AVFrame *frame = ist->sub2video.frame;
  192. int i;
  193. int ret;
  194. av_assert1(frame->data[0]);
  195. ist->sub2video.last_pts = frame->pts = pts;
  196. for (i = 0; i < ist->nb_filters; i++) {
  197. ret = av_buffersrc_add_frame_flags(ist->filters[i]->filter, frame,
  198. AV_BUFFERSRC_FLAG_KEEP_REF |
  199. AV_BUFFERSRC_FLAG_PUSH);
  200. if (ret != AVERROR_EOF && ret < 0)
  201. av_log(NULL, AV_LOG_WARNING, "Error while add the frame to buffer source(%s).\n",
  202. av_err2str(ret));
  203. }
  204. }
  205. void sub2video_update(InputStream *ist, int64_t heartbeat_pts, AVSubtitle *sub)
  206. {
  207. AVFrame *frame = ist->sub2video.frame;
  208. int8_t *dst;
  209. int dst_linesize;
  210. int num_rects, i;
  211. int64_t pts, end_pts;
  212. if (!frame)
  213. return;
  214. if (sub) {
  215. pts = av_rescale_q(sub->pts + sub->start_display_time * 1000LL,
  216. AV_TIME_BASE_Q, ist->st->time_base);
  217. end_pts = av_rescale_q(sub->pts + sub->end_display_time * 1000LL,
  218. AV_TIME_BASE_Q, ist->st->time_base);
  219. num_rects = sub->num_rects;
  220. } else {
  221. /* If we are initializing the system, utilize current heartbeat
  222. PTS as the start time, and show until the following subpicture
  223. is received. Otherwise, utilize the previous subpicture's end time
  224. as the fall-back value. */
  225. pts = ist->sub2video.initialize ?
  226. heartbeat_pts : ist->sub2video.end_pts;
  227. end_pts = INT64_MAX;
  228. num_rects = 0;
  229. }
  230. if (sub2video_get_blank_frame(ist) < 0) {
  231. av_log(ist->dec_ctx, AV_LOG_ERROR,
  232. "Impossible to get a blank canvas.\n");
  233. return;
  234. }
  235. dst = frame->data [0];
  236. dst_linesize = frame->linesize[0];
  237. for (i = 0; i < num_rects; i++)
  238. sub2video_copy_rect(dst, dst_linesize, frame->width, frame->height, sub->rects[i]);
  239. sub2video_push_ref(ist, pts);
  240. ist->sub2video.end_pts = end_pts;
  241. ist->sub2video.initialize = 0;
  242. }
  243. static void sub2video_heartbeat(InputStream *ist, int64_t pts)
  244. {
  245. InputFile *infile = input_files[ist->file_index];
  246. int i, j, nb_reqs;
  247. int64_t pts2;
  248. /* When a frame is read from a file, examine all sub2video streams in
  249. the same file and send the sub2video frame again. Otherwise, decoded
  250. video frames could be accumulating in the filter graph while a filter
  251. (possibly overlay) is desperately waiting for a subtitle frame. */
  252. for (i = 0; i < infile->nb_streams; i++) {
  253. InputStream *ist2 = input_streams[infile->ist_index + i];
  254. if (!ist2->sub2video.frame)
  255. continue;
  256. /* subtitles seem to be usually muxed ahead of other streams;
  257. if not, subtracting a larger time here is necessary */
  258. pts2 = av_rescale_q(pts, ist->st->time_base, ist2->st->time_base) - 1;
  259. /* do not send the heartbeat frame if the subtitle is already ahead */
  260. if (pts2 <= ist2->sub2video.last_pts)
  261. continue;
  262. if (pts2 >= ist2->sub2video.end_pts || ist2->sub2video.initialize)
  263. /* if we have hit the end of the current displayed subpicture,
  264. or if we need to initialize the system, update the
  265. overlayed subpicture and its start/end times */
  266. sub2video_update(ist2, pts2 + 1, NULL);
  267. for (j = 0, nb_reqs = 0; j < ist2->nb_filters; j++)
  268. nb_reqs += av_buffersrc_get_nb_failed_requests(ist2->filters[j]->filter);
  269. if (nb_reqs)
  270. sub2video_push_ref(ist2, pts2);
  271. }
  272. }
  273. static void sub2video_flush(InputStream *ist)
  274. {
  275. int i;
  276. int ret;
  277. if (ist->sub2video.end_pts < INT64_MAX)
  278. sub2video_update(ist, INT64_MAX, NULL);
  279. for (i = 0; i < ist->nb_filters; i++) {
  280. ret = av_buffersrc_add_frame(ist->filters[i]->filter, NULL);
  281. if (ret != AVERROR_EOF && ret < 0)
  282. av_log(NULL, AV_LOG_WARNING, "Flush the frame error.\n");
  283. }
  284. }
  285. /* end of sub2video hack */
  286. static void term_exit_sigsafe(void)
  287. {
  288. #if HAVE_TERMIOS_H
  289. if(restore_tty)
  290. tcsetattr (0, TCSANOW, &oldtty);
  291. #endif
  292. }
  293. void term_exit(void)
  294. {
  295. av_log(NULL, AV_LOG_QUIET, "%s", "");
  296. term_exit_sigsafe();
  297. }
  298. static volatile int received_sigterm = 0;
  299. static volatile int received_nb_signals = 0;
  300. static atomic_int transcode_init_done = ATOMIC_VAR_INIT(0);
  301. static volatile int ffmpeg_exited = 0;
  302. static int main_return_code = 0;
  303. static int64_t copy_ts_first_pts = AV_NOPTS_VALUE;
  304. static void
  305. sigterm_handler(int sig)
  306. {
  307. int ret;
  308. received_sigterm = sig;
  309. received_nb_signals++;
  310. term_exit_sigsafe();
  311. if(received_nb_signals > 3) {
  312. ret = write(2/*STDERR_FILENO*/, "Received > 3 system signals, hard exiting\n",
  313. strlen("Received > 3 system signals, hard exiting\n"));
  314. if (ret < 0) { /* Do nothing */ };
  315. exit(123);
  316. }
  317. }
  318. #if HAVE_SETCONSOLECTRLHANDLER
  319. static BOOL WINAPI CtrlHandler(DWORD fdwCtrlType)
  320. {
  321. av_log(NULL, AV_LOG_DEBUG, "\nReceived windows signal %ld\n", fdwCtrlType);
  322. switch (fdwCtrlType)
  323. {
  324. case CTRL_C_EVENT:
  325. case CTRL_BREAK_EVENT:
  326. sigterm_handler(SIGINT);
  327. return TRUE;
  328. case CTRL_CLOSE_EVENT:
  329. case CTRL_LOGOFF_EVENT:
  330. case CTRL_SHUTDOWN_EVENT:
  331. sigterm_handler(SIGTERM);
  332. /* Basically, with these 3 events, when we return from this method the
  333. process is hard terminated, so stall as long as we need to
  334. to try and let the main thread(s) clean up and gracefully terminate
  335. (we have at most 5 seconds, but should be done far before that). */
  336. while (!ffmpeg_exited) {
  337. Sleep(0);
  338. }
  339. return TRUE;
  340. default:
  341. av_log(NULL, AV_LOG_ERROR, "Received unknown windows signal %ld\n", fdwCtrlType);
  342. return FALSE;
  343. }
  344. }
  345. #endif
  346. #ifdef __linux__
  347. #define SIGNAL(sig, func) \
  348. do { \
  349. action.sa_handler = func; \
  350. sigaction(sig, &action, NULL); \
  351. } while (0)
  352. #else
  353. #define SIGNAL(sig, func) \
  354. signal(sig, func)
  355. #endif
  356. void term_init(void)
  357. {
  358. #if defined __linux__
  359. struct sigaction action = {0};
  360. action.sa_handler = sigterm_handler;
  361. /* block other interrupts while processing this one */
  362. sigfillset(&action.sa_mask);
  363. /* restart interruptible functions (i.e. don't fail with EINTR) */
  364. action.sa_flags = SA_RESTART;
  365. #endif
  366. #if HAVE_TERMIOS_H
  367. if (!run_as_daemon && stdin_interaction) {
  368. struct termios tty;
  369. if (tcgetattr (0, &tty) == 0) {
  370. oldtty = tty;
  371. restore_tty = 1;
  372. tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
  373. |INLCR|IGNCR|ICRNL|IXON);
  374. tty.c_oflag |= OPOST;
  375. tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
  376. tty.c_cflag &= ~(CSIZE|PARENB);
  377. tty.c_cflag |= CS8;
  378. tty.c_cc[VMIN] = 1;
  379. tty.c_cc[VTIME] = 0;
  380. tcsetattr (0, TCSANOW, &tty);
  381. }
  382. SIGNAL(SIGQUIT, sigterm_handler); /* Quit (POSIX). */
  383. }
  384. #endif
  385. SIGNAL(SIGINT , sigterm_handler); /* Interrupt (ANSI). */
  386. SIGNAL(SIGTERM, sigterm_handler); /* Termination (ANSI). */
  387. #ifdef SIGXCPU
  388. SIGNAL(SIGXCPU, sigterm_handler);
  389. #endif
  390. #ifdef SIGPIPE
  391. signal(SIGPIPE, SIG_IGN); /* Broken pipe (POSIX). */
  392. #endif
  393. #if HAVE_SETCONSOLECTRLHANDLER
  394. SetConsoleCtrlHandler((PHANDLER_ROUTINE) CtrlHandler, TRUE);
  395. #endif
  396. }
  397. /* read a key without blocking */
  398. static int read_key(void)
  399. {
  400. unsigned char ch;
  401. #if HAVE_TERMIOS_H
  402. int n = 1;
  403. struct timeval tv;
  404. fd_set rfds;
  405. FD_ZERO(&rfds);
  406. FD_SET(0, &rfds);
  407. tv.tv_sec = 0;
  408. tv.tv_usec = 0;
  409. n = select(1, &rfds, NULL, NULL, &tv);
  410. if (n > 0) {
  411. n = read(0, &ch, 1);
  412. if (n == 1)
  413. return ch;
  414. return n;
  415. }
  416. #elif HAVE_KBHIT
  417. # if HAVE_PEEKNAMEDPIPE
  418. static int is_pipe;
  419. static HANDLE input_handle;
  420. DWORD dw, nchars;
  421. if(!input_handle){
  422. input_handle = GetStdHandle(STD_INPUT_HANDLE);
  423. is_pipe = !GetConsoleMode(input_handle, &dw);
  424. }
  425. if (is_pipe) {
  426. /* When running under a GUI, you will end here. */
  427. if (!PeekNamedPipe(input_handle, NULL, 0, NULL, &nchars, NULL)) {
  428. // input pipe may have been closed by the program that ran ffmpeg
  429. return -1;
  430. }
  431. //Read it
  432. if(nchars != 0) {
  433. read(0, &ch, 1);
  434. return ch;
  435. }else{
  436. return -1;
  437. }
  438. }
  439. # endif
  440. if(kbhit())
  441. return(getch());
  442. #endif
  443. return -1;
  444. }
  445. static int decode_interrupt_cb(void *ctx)
  446. {
  447. return received_nb_signals > atomic_load(&transcode_init_done);
  448. }
  449. const AVIOInterruptCB int_cb = { decode_interrupt_cb, NULL };
  450. static void ffmpeg_cleanup(int ret)
  451. {
  452. int i, j;
  453. if (do_benchmark) {
  454. int maxrss = getmaxrss() / 1024;
  455. av_log(NULL, AV_LOG_INFO, "bench: maxrss=%ikB\n", maxrss);
  456. }
  457. for (i = 0; i < nb_filtergraphs; i++) {
  458. FilterGraph *fg = filtergraphs[i];
  459. avfilter_graph_free(&fg->graph);
  460. for (j = 0; j < fg->nb_inputs; j++) {
  461. InputFilter *ifilter = fg->inputs[j];
  462. struct InputStream *ist = ifilter->ist;
  463. while (av_fifo_size(ifilter->frame_queue)) {
  464. AVFrame *frame;
  465. av_fifo_generic_read(ifilter->frame_queue, &frame,
  466. sizeof(frame), NULL);
  467. av_frame_free(&frame);
  468. }
  469. av_fifo_freep(&ifilter->frame_queue);
  470. if (ist->sub2video.sub_queue) {
  471. while (av_fifo_size(ist->sub2video.sub_queue)) {
  472. AVSubtitle sub;
  473. av_fifo_generic_read(ist->sub2video.sub_queue,
  474. &sub, sizeof(sub), NULL);
  475. avsubtitle_free(&sub);
  476. }
  477. av_fifo_freep(&ist->sub2video.sub_queue);
  478. }
  479. av_buffer_unref(&ifilter->hw_frames_ctx);
  480. av_freep(&ifilter->name);
  481. av_freep(&fg->inputs[j]);
  482. }
  483. av_freep(&fg->inputs);
  484. for (j = 0; j < fg->nb_outputs; j++) {
  485. OutputFilter *ofilter = fg->outputs[j];
  486. avfilter_inout_free(&ofilter->out_tmp);
  487. av_freep(&ofilter->name);
  488. av_freep(&ofilter->formats);
  489. av_freep(&ofilter->channel_layouts);
  490. av_freep(&ofilter->sample_rates);
  491. av_freep(&fg->outputs[j]);
  492. }
  493. av_freep(&fg->outputs);
  494. av_freep(&fg->graph_desc);
  495. av_freep(&filtergraphs[i]);
  496. }
  497. av_freep(&filtergraphs);
  498. av_freep(&subtitle_out);
  499. /* close files */
  500. for (i = 0; i < nb_output_files; i++) {
  501. OutputFile *of = output_files[i];
  502. AVFormatContext *s;
  503. if (!of)
  504. continue;
  505. s = of->ctx;
  506. if (s && s->oformat && !(s->oformat->flags & AVFMT_NOFILE))
  507. avio_closep(&s->pb);
  508. avformat_free_context(s);
  509. av_dict_free(&of->opts);
  510. av_freep(&output_files[i]);
  511. }
  512. for (i = 0; i < nb_output_streams; i++) {
  513. OutputStream *ost = output_streams[i];
  514. if (!ost)
  515. continue;
  516. av_bsf_free(&ost->bsf_ctx);
  517. av_frame_free(&ost->filtered_frame);
  518. av_frame_free(&ost->last_frame);
  519. av_packet_free(&ost->pkt);
  520. av_dict_free(&ost->encoder_opts);
  521. av_freep(&ost->forced_keyframes);
  522. av_expr_free(ost->forced_keyframes_pexpr);
  523. av_freep(&ost->avfilter);
  524. av_freep(&ost->logfile_prefix);
  525. av_freep(&ost->audio_channels_map);
  526. ost->audio_channels_mapped = 0;
  527. av_dict_free(&ost->sws_dict);
  528. av_dict_free(&ost->swr_opts);
  529. avcodec_free_context(&ost->enc_ctx);
  530. avcodec_parameters_free(&ost->ref_par);
  531. if (ost->muxing_queue) {
  532. while (av_fifo_size(ost->muxing_queue)) {
  533. AVPacket *pkt;
  534. av_fifo_generic_read(ost->muxing_queue, &pkt, sizeof(pkt), NULL);
  535. av_packet_free(&pkt);
  536. }
  537. av_fifo_freep(&ost->muxing_queue);
  538. }
  539. av_freep(&output_streams[i]);
  540. }
  541. #if HAVE_THREADS
  542. free_input_threads();
  543. #endif
  544. for (i = 0; i < nb_input_files; i++) {
  545. avformat_close_input(&input_files[i]->ctx);
  546. av_packet_free(&input_files[i]->pkt);
  547. av_freep(&input_files[i]);
  548. }
  549. for (i = 0; i < nb_input_streams; i++) {
  550. InputStream *ist = input_streams[i];
  551. av_frame_free(&ist->decoded_frame);
  552. av_frame_free(&ist->filter_frame);
  553. av_packet_free(&ist->pkt);
  554. av_dict_free(&ist->decoder_opts);
  555. avsubtitle_free(&ist->prev_sub.subtitle);
  556. av_frame_free(&ist->sub2video.frame);
  557. av_freep(&ist->filters);
  558. av_freep(&ist->hwaccel_device);
  559. av_freep(&ist->dts_buffer);
  560. avcodec_free_context(&ist->dec_ctx);
  561. av_freep(&input_streams[i]);
  562. }
  563. if (vstats_file) {
  564. if (fclose(vstats_file))
  565. av_log(NULL, AV_LOG_ERROR,
  566. "Error closing vstats file, loss of information possible: %s\n",
  567. av_err2str(AVERROR(errno)));
  568. }
  569. av_freep(&vstats_filename);
  570. av_freep(&input_streams);
  571. av_freep(&input_files);
  572. av_freep(&output_streams);
  573. av_freep(&output_files);
  574. uninit_opts();
  575. avformat_network_deinit();
  576. if (received_sigterm) {
  577. av_log(NULL, AV_LOG_INFO, "Exiting normally, received signal %d.\n",
  578. (int) received_sigterm);
  579. } else if (ret && atomic_load(&transcode_init_done)) {
  580. av_log(NULL, AV_LOG_INFO, "Conversion failed!\n");
  581. }
  582. term_exit();
  583. ffmpeg_exited = 1;
  584. }
  585. void remove_avoptions(AVDictionary **a, AVDictionary *b)
  586. {
  587. AVDictionaryEntry *t = NULL;
  588. while ((t = av_dict_get(b, "", t, AV_DICT_IGNORE_SUFFIX))) {
  589. av_dict_set(a, t->key, NULL, AV_DICT_MATCH_CASE);
  590. }
  591. }
  592. void assert_avoptions(AVDictionary *m)
  593. {
  594. AVDictionaryEntry *t;
  595. if ((t = av_dict_get(m, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
  596. av_log(NULL, AV_LOG_FATAL, "Option %s not found.\n", t->key);
  597. exit_program(1);
  598. }
  599. }
  600. static void abort_codec_experimental(const AVCodec *c, int encoder)
  601. {
  602. exit_program(1);
  603. }
  604. static void update_benchmark(const char *fmt, ...)
  605. {
  606. if (do_benchmark_all) {
  607. BenchmarkTimeStamps t = get_benchmark_time_stamps();
  608. va_list va;
  609. char buf[1024];
  610. if (fmt) {
  611. va_start(va, fmt);
  612. vsnprintf(buf, sizeof(buf), fmt, va);
  613. va_end(va);
  614. av_log(NULL, AV_LOG_INFO,
  615. "bench: %8" PRIu64 " user %8" PRIu64 " sys %8" PRIu64 " real %s \n",
  616. t.user_usec - current_time.user_usec,
  617. t.sys_usec - current_time.sys_usec,
  618. t.real_usec - current_time.real_usec, buf);
  619. }
  620. current_time = t;
  621. }
  622. }
  623. static void close_all_output_streams(OutputStream *ost, OSTFinished this_stream, OSTFinished others)
  624. {
  625. int i;
  626. for (i = 0; i < nb_output_streams; i++) {
  627. OutputStream *ost2 = output_streams[i];
  628. ost2->finished |= ost == ost2 ? this_stream : others;
  629. }
  630. }
  631. static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
  632. {
  633. AVFormatContext *s = of->ctx;
  634. AVStream *st = ost->st;
  635. int ret;
  636. /*
  637. * Audio encoders may split the packets -- #frames in != #packets out.
  638. * But there is no reordering, so we can limit the number of output packets
  639. * by simply dropping them here.
  640. * Counting encoded video frames needs to be done separately because of
  641. * reordering, see do_video_out().
  642. * Do not count the packet when unqueued because it has been counted when queued.
  643. */
  644. if (!(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->encoding_needed) && !unqueue) {
  645. if (ost->frame_number >= ost->max_frames) {
  646. av_packet_unref(pkt);
  647. return;
  648. }
  649. ost->frame_number++;
  650. }
  651. if (!of->header_written) {
  652. AVPacket *tmp_pkt;
  653. /* the muxer is not initialized yet, buffer the packet */
  654. if (!av_fifo_space(ost->muxing_queue)) {
  655. unsigned int are_we_over_size =
  656. (ost->muxing_queue_data_size + pkt->size) > ost->muxing_queue_data_threshold;
  657. int new_size = are_we_over_size ?
  658. FFMIN(2 * av_fifo_size(ost->muxing_queue),
  659. ost->max_muxing_queue_size) :
  660. 2 * av_fifo_size(ost->muxing_queue);
  661. if (new_size <= av_fifo_size(ost->muxing_queue)) {
  662. av_log(NULL, AV_LOG_ERROR,
  663. "Too many packets buffered for output stream %d:%d.\n",
  664. ost->file_index, ost->st->index);
  665. exit_program(1);
  666. }
  667. ret = av_fifo_realloc2(ost->muxing_queue, new_size);
  668. if (ret < 0)
  669. exit_program(1);
  670. }
  671. ret = av_packet_make_refcounted(pkt);
  672. if (ret < 0)
  673. exit_program(1);
  674. tmp_pkt = av_packet_alloc();
  675. if (!tmp_pkt)
  676. exit_program(1);
  677. av_packet_move_ref(tmp_pkt, pkt);
  678. ost->muxing_queue_data_size += tmp_pkt->size;
  679. av_fifo_generic_write(ost->muxing_queue, &tmp_pkt, sizeof(tmp_pkt), NULL);
  680. return;
  681. }
  682. if ((st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && video_sync_method == VSYNC_DROP) ||
  683. (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && audio_sync_method < 0))
  684. pkt->pts = pkt->dts = AV_NOPTS_VALUE;
  685. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  686. int i;
  687. uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
  688. NULL);
  689. ost->quality = sd ? AV_RL32(sd) : -1;
  690. ost->pict_type = sd ? sd[4] : AV_PICTURE_TYPE_NONE;
  691. for (i = 0; i<FF_ARRAY_ELEMS(ost->error); i++) {
  692. if (sd && i < sd[5])
  693. ost->error[i] = AV_RL64(sd + 8 + 8*i);
  694. else
  695. ost->error[i] = -1;
  696. }
  697. if (ost->frame_rate.num && ost->is_cfr) {
  698. if (pkt->duration > 0)
  699. av_log(NULL, AV_LOG_WARNING, "Overriding packet duration by frame rate, this should not happen\n");
  700. pkt->duration = av_rescale_q(1, av_inv_q(ost->frame_rate),
  701. ost->mux_timebase);
  702. }
  703. }
  704. av_packet_rescale_ts(pkt, ost->mux_timebase, ost->st->time_base);
  705. if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
  706. if (pkt->dts != AV_NOPTS_VALUE &&
  707. pkt->pts != AV_NOPTS_VALUE &&
  708. pkt->dts > pkt->pts) {
  709. av_log(s, AV_LOG_WARNING, "Invalid DTS: %"PRId64" PTS: %"PRId64" in output stream %d:%d, replacing by guess\n",
  710. pkt->dts, pkt->pts,
  711. ost->file_index, ost->st->index);
  712. pkt->pts =
  713. pkt->dts = pkt->pts + pkt->dts + ost->last_mux_dts + 1
  714. - FFMIN3(pkt->pts, pkt->dts, ost->last_mux_dts + 1)
  715. - FFMAX3(pkt->pts, pkt->dts, ost->last_mux_dts + 1);
  716. }
  717. if ((st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO || st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) &&
  718. pkt->dts != AV_NOPTS_VALUE &&
  719. !(st->codecpar->codec_id == AV_CODEC_ID_VP9 && ost->stream_copy) &&
  720. ost->last_mux_dts != AV_NOPTS_VALUE) {
  721. int64_t max = ost->last_mux_dts + !(s->oformat->flags & AVFMT_TS_NONSTRICT);
  722. if (pkt->dts < max) {
  723. int loglevel = max - pkt->dts > 2 || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ? AV_LOG_WARNING : AV_LOG_DEBUG;
  724. if (exit_on_error)
  725. loglevel = AV_LOG_ERROR;
  726. av_log(s, loglevel, "Non-monotonous DTS in output stream "
  727. "%d:%d; previous: %"PRId64", current: %"PRId64"; ",
  728. ost->file_index, ost->st->index, ost->last_mux_dts, pkt->dts);
  729. if (exit_on_error) {
  730. av_log(NULL, AV_LOG_FATAL, "aborting.\n");
  731. exit_program(1);
  732. }
  733. av_log(s, loglevel, "changing to %"PRId64". This may result "
  734. "in incorrect timestamps in the output file.\n",
  735. max);
  736. if (pkt->pts >= pkt->dts)
  737. pkt->pts = FFMAX(pkt->pts, max);
  738. pkt->dts = max;
  739. }
  740. }
  741. }
  742. ost->last_mux_dts = pkt->dts;
  743. ost->data_size += pkt->size;
  744. ost->packets_written++;
  745. pkt->stream_index = ost->index;
  746. if (debug_ts) {
  747. av_log(NULL, AV_LOG_INFO, "muxer <- type:%s "
  748. "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s size:%d\n",
  749. av_get_media_type_string(ost->enc_ctx->codec_type),
  750. av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->st->time_base),
  751. av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->st->time_base),
  752. pkt->size
  753. );
  754. }
  755. ret = av_interleaved_write_frame(s, pkt);
  756. if (ret < 0) {
  757. print_error("av_interleaved_write_frame()", ret);
  758. main_return_code = 1;
  759. close_all_output_streams(ost, MUXER_FINISHED | ENCODER_FINISHED, ENCODER_FINISHED);
  760. }
  761. av_packet_unref(pkt);
  762. }
  763. static void close_output_stream(OutputStream *ost)
  764. {
  765. OutputFile *of = output_files[ost->file_index];
  766. ost->finished |= ENCODER_FINISHED;
  767. if (of->shortest) {
  768. int64_t end = av_rescale_q(ost->sync_opts - ost->first_pts, ost->enc_ctx->time_base, AV_TIME_BASE_Q);
  769. of->recording_time = FFMIN(of->recording_time, end);
  770. }
  771. }
  772. /*
  773. * Send a single packet to the output, applying any bitstream filters
  774. * associated with the output stream. This may result in any number
  775. * of packets actually being written, depending on what bitstream
  776. * filters are applied. The supplied packet is consumed and will be
  777. * blank (as if newly-allocated) when this function returns.
  778. *
  779. * If eof is set, instead indicate EOF to all bitstream filters and
  780. * therefore flush any delayed packets to the output. A blank packet
  781. * must be supplied in this case.
  782. */
  783. static void output_packet(OutputFile *of, AVPacket *pkt,
  784. OutputStream *ost, int eof)
  785. {
  786. int ret = 0;
  787. /* apply the output bitstream filters */
  788. if (ost->bsf_ctx) {
  789. ret = av_bsf_send_packet(ost->bsf_ctx, eof ? NULL : pkt);
  790. if (ret < 0)
  791. goto finish;
  792. while ((ret = av_bsf_receive_packet(ost->bsf_ctx, pkt)) >= 0)
  793. write_packet(of, pkt, ost, 0);
  794. if (ret == AVERROR(EAGAIN))
  795. ret = 0;
  796. } else if (!eof)
  797. write_packet(of, pkt, ost, 0);
  798. finish:
  799. if (ret < 0 && ret != AVERROR_EOF) {
  800. av_log(NULL, AV_LOG_ERROR, "Error applying bitstream filters to an output "
  801. "packet for stream #%d:%d.\n", ost->file_index, ost->index);
  802. if(exit_on_error)
  803. exit_program(1);
  804. }
  805. }
  806. static int check_recording_time(OutputStream *ost)
  807. {
  808. OutputFile *of = output_files[ost->file_index];
  809. if (of->recording_time != INT64_MAX &&
  810. av_compare_ts(ost->sync_opts - ost->first_pts, ost->enc_ctx->time_base, of->recording_time,
  811. AV_TIME_BASE_Q) >= 0) {
  812. close_output_stream(ost);
  813. return 0;
  814. }
  815. return 1;
  816. }
  817. static double adjust_frame_pts_to_encoder_tb(OutputFile *of, OutputStream *ost,
  818. AVFrame *frame)
  819. {
  820. double float_pts = AV_NOPTS_VALUE; // this is identical to frame.pts but with higher precision
  821. AVCodecContext *enc = ost->enc_ctx;
  822. if (!frame || frame->pts == AV_NOPTS_VALUE ||
  823. !enc || !ost->filter || !ost->filter->graph->graph)
  824. goto early_exit;
  825. {
  826. AVFilterContext *filter = ost->filter->filter;
  827. int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
  828. AVRational filter_tb = av_buffersink_get_time_base(filter);
  829. AVRational tb = enc->time_base;
  830. int extra_bits = av_clip(29 - av_log2(tb.den), 0, 16);
  831. tb.den <<= extra_bits;
  832. float_pts =
  833. av_rescale_q(frame->pts, filter_tb, tb) -
  834. av_rescale_q(start_time, AV_TIME_BASE_Q, tb);
  835. float_pts /= 1 << extra_bits;
  836. // avoid exact midoints to reduce the chance of rounding differences, this can be removed in case the fps code is changed to work with integers
  837. float_pts += FFSIGN(float_pts) * 1.0 / (1<<17);
  838. frame->pts =
  839. av_rescale_q(frame->pts, filter_tb, enc->time_base) -
  840. av_rescale_q(start_time, AV_TIME_BASE_Q, enc->time_base);
  841. }
  842. early_exit:
  843. if (debug_ts) {
  844. av_log(NULL, AV_LOG_INFO, "filter -> pts:%s pts_time:%s exact:%f time_base:%d/%d\n",
  845. frame ? av_ts2str(frame->pts) : "NULL",
  846. frame ? av_ts2timestr(frame->pts, &enc->time_base) : "NULL",
  847. float_pts,
  848. enc ? enc->time_base.num : -1,
  849. enc ? enc->time_base.den : -1);
  850. }
  851. return float_pts;
  852. }
  853. static int init_output_stream(OutputStream *ost, AVFrame *frame,
  854. char *error, int error_len);
  855. static int init_output_stream_wrapper(OutputStream *ost, AVFrame *frame,
  856. unsigned int fatal)
  857. {
  858. int ret = AVERROR_BUG;
  859. char error[1024] = {0};
  860. if (ost->initialized)
  861. return 0;
  862. ret = init_output_stream(ost, frame, error, sizeof(error));
  863. if (ret < 0) {
  864. av_log(NULL, AV_LOG_ERROR, "Error initializing output stream %d:%d -- %s\n",
  865. ost->file_index, ost->index, error);
  866. if (fatal)
  867. exit_program(1);
  868. }
  869. return ret;
  870. }
  871. static void do_audio_out(OutputFile *of, OutputStream *ost,
  872. AVFrame *frame)
  873. {
  874. AVCodecContext *enc = ost->enc_ctx;
  875. AVPacket *pkt = ost->pkt;
  876. int ret;
  877. adjust_frame_pts_to_encoder_tb(of, ost, frame);
  878. if (!check_recording_time(ost))
  879. return;
  880. if (frame->pts == AV_NOPTS_VALUE || audio_sync_method < 0)
  881. frame->pts = ost->sync_opts;
  882. ost->sync_opts = frame->pts + frame->nb_samples;
  883. ost->samples_encoded += frame->nb_samples;
  884. ost->frames_encoded++;
  885. update_benchmark(NULL);
  886. if (debug_ts) {
  887. av_log(NULL, AV_LOG_INFO, "encoder <- type:audio "
  888. "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n",
  889. av_ts2str(frame->pts), av_ts2timestr(frame->pts, &enc->time_base),
  890. enc->time_base.num, enc->time_base.den);
  891. }
  892. ret = avcodec_send_frame(enc, frame);
  893. if (ret < 0)
  894. goto error;
  895. while (1) {
  896. av_packet_unref(pkt);
  897. ret = avcodec_receive_packet(enc, pkt);
  898. if (ret == AVERROR(EAGAIN))
  899. break;
  900. if (ret < 0)
  901. goto error;
  902. update_benchmark("encode_audio %d.%d", ost->file_index, ost->index);
  903. av_packet_rescale_ts(pkt, enc->time_base, ost->mux_timebase);
  904. if (debug_ts) {
  905. av_log(NULL, AV_LOG_INFO, "encoder -> type:audio "
  906. "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",
  907. av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &enc->time_base),
  908. av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &enc->time_base));
  909. }
  910. output_packet(of, pkt, ost, 0);
  911. }
  912. return;
  913. error:
  914. av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n");
  915. exit_program(1);
  916. }
  917. static void do_subtitle_out(OutputFile *of,
  918. OutputStream *ost,
  919. AVSubtitle *sub)
  920. {
  921. int subtitle_out_max_size = 1024 * 1024;
  922. int subtitle_out_size, nb, i;
  923. AVCodecContext *enc;
  924. AVPacket *pkt = ost->pkt;
  925. int64_t pts;
  926. if (sub->pts == AV_NOPTS_VALUE) {
  927. av_log(NULL, AV_LOG_ERROR, "Subtitle packets must have a pts\n");
  928. if (exit_on_error)
  929. exit_program(1);
  930. return;
  931. }
  932. enc = ost->enc_ctx;
  933. if (!subtitle_out) {
  934. subtitle_out = av_malloc(subtitle_out_max_size);
  935. if (!subtitle_out) {
  936. av_log(NULL, AV_LOG_FATAL, "Failed to allocate subtitle_out\n");
  937. exit_program(1);
  938. }
  939. }
  940. /* Note: DVB subtitle need one packet to draw them and one other
  941. packet to clear them */
  942. /* XXX: signal it in the codec context ? */
  943. if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE)
  944. nb = 2;
  945. else
  946. nb = 1;
  947. /* shift timestamp to honor -ss and make check_recording_time() work with -t */
  948. pts = sub->pts;
  949. if (output_files[ost->file_index]->start_time != AV_NOPTS_VALUE)
  950. pts -= output_files[ost->file_index]->start_time;
  951. for (i = 0; i < nb; i++) {
  952. unsigned save_num_rects = sub->num_rects;
  953. ost->sync_opts = av_rescale_q(pts, AV_TIME_BASE_Q, enc->time_base);
  954. if (!check_recording_time(ost))
  955. return;
  956. sub->pts = pts;
  957. // start_display_time is required to be 0
  958. sub->pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, AV_TIME_BASE_Q);
  959. sub->end_display_time -= sub->start_display_time;
  960. sub->start_display_time = 0;
  961. if (i == 1)
  962. sub->num_rects = 0;
  963. ost->frames_encoded++;
  964. subtitle_out_size = avcodec_encode_subtitle(enc, subtitle_out,
  965. subtitle_out_max_size, sub);
  966. if (i == 1)
  967. sub->num_rects = save_num_rects;
  968. if (subtitle_out_size < 0) {
  969. av_log(NULL, AV_LOG_FATAL, "Subtitle encoding failed\n");
  970. exit_program(1);
  971. }
  972. av_packet_unref(pkt);
  973. pkt->data = subtitle_out;
  974. pkt->size = subtitle_out_size;
  975. pkt->pts = av_rescale_q(sub->pts, AV_TIME_BASE_Q, ost->mux_timebase);
  976. pkt->duration = av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, ost->mux_timebase);
  977. if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
  978. /* XXX: the pts correction is handled here. Maybe handling
  979. it in the codec would be better */
  980. if (i == 0)
  981. pkt->pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, ost->mux_timebase);
  982. else
  983. pkt->pts += av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, ost->mux_timebase);
  984. }
  985. pkt->dts = pkt->pts;
  986. output_packet(of, pkt, ost, 0);
  987. }
  988. }
  989. static void do_video_out(OutputFile *of,
  990. OutputStream *ost,
  991. AVFrame *next_picture)
  992. {
  993. int ret, format_video_sync;
  994. AVPacket *pkt = ost->pkt;
  995. AVCodecContext *enc = ost->enc_ctx;
  996. AVRational frame_rate;
  997. int nb_frames, nb0_frames, i;
  998. double delta, delta0;
  999. double duration = 0;
  1000. double sync_ipts = AV_NOPTS_VALUE;
  1001. int frame_size = 0;
  1002. InputStream *ist = NULL;
  1003. AVFilterContext *filter = ost->filter->filter;
  1004. init_output_stream_wrapper(ost, next_picture, 1);
  1005. sync_ipts = adjust_frame_pts_to_encoder_tb(of, ost, next_picture);
  1006. if (ost->source_index >= 0)
  1007. ist = input_streams[ost->source_index];
  1008. frame_rate = av_buffersink_get_frame_rate(filter);
  1009. if (frame_rate.num > 0 && frame_rate.den > 0)
  1010. duration = 1/(av_q2d(frame_rate) * av_q2d(enc->time_base));
  1011. if(ist && ist->st->start_time != AV_NOPTS_VALUE && ist->st->first_dts != AV_NOPTS_VALUE && ost->frame_rate.num)
  1012. duration = FFMIN(duration, 1/(av_q2d(ost->frame_rate) * av_q2d(enc->time_base)));
  1013. if (!ost->filters_script &&
  1014. !ost->filters &&
  1015. (nb_filtergraphs == 0 || !filtergraphs[0]->graph_desc) &&
  1016. next_picture &&
  1017. ist &&
  1018. lrintf(next_picture->pkt_duration * av_q2d(ist->st->time_base) / av_q2d(enc->time_base)) > 0) {
  1019. duration = lrintf(next_picture->pkt_duration * av_q2d(ist->st->time_base) / av_q2d(enc->time_base));
  1020. }
  1021. if (!next_picture) {
  1022. //end, flushing
  1023. nb0_frames = nb_frames = mid_pred(ost->last_nb0_frames[0],
  1024. ost->last_nb0_frames[1],
  1025. ost->last_nb0_frames[2]);
  1026. } else {
  1027. delta0 = sync_ipts - ost->sync_opts; // delta0 is the "drift" between the input frame (next_picture) and where it would fall in the output.
  1028. delta = delta0 + duration;
  1029. /* by default, we output a single frame */
  1030. nb0_frames = 0; // tracks the number of times the PREVIOUS frame should be duplicated, mostly for variable framerate (VFR)
  1031. nb_frames = 1;
  1032. format_video_sync = video_sync_method;
  1033. if (format_video_sync == VSYNC_AUTO) {
  1034. if(!strcmp(of->ctx->oformat->name, "avi")) {
  1035. format_video_sync = VSYNC_VFR;
  1036. } else
  1037. format_video_sync = (of->ctx->oformat->flags & AVFMT_VARIABLE_FPS) ? ((of->ctx->oformat->flags & AVFMT_NOTIMESTAMPS) ? VSYNC_PASSTHROUGH : VSYNC_VFR) : VSYNC_CFR;
  1038. if ( ist
  1039. && format_video_sync == VSYNC_CFR
  1040. && input_files[ist->file_index]->ctx->nb_streams == 1
  1041. && input_files[ist->file_index]->input_ts_offset == 0) {
  1042. format_video_sync = VSYNC_VSCFR;
  1043. }
  1044. if (format_video_sync == VSYNC_CFR && copy_ts) {
  1045. format_video_sync = VSYNC_VSCFR;
  1046. }
  1047. }
  1048. ost->is_cfr = (format_video_sync == VSYNC_CFR || format_video_sync == VSYNC_VSCFR);
  1049. if (delta0 < 0 &&
  1050. delta > 0 &&
  1051. format_video_sync != VSYNC_PASSTHROUGH &&
  1052. format_video_sync != VSYNC_DROP) {
  1053. if (delta0 < -0.6) {
  1054. av_log(NULL, AV_LOG_VERBOSE, "Past duration %f too large\n", -delta0);
  1055. } else
  1056. av_log(NULL, AV_LOG_DEBUG, "Clipping frame in rate conversion by %f\n", -delta0);
  1057. sync_ipts = ost->sync_opts;
  1058. duration += delta0;
  1059. delta0 = 0;
  1060. }
  1061. switch (format_video_sync) {
  1062. case VSYNC_VSCFR:
  1063. if (ost->frame_number == 0 && delta0 >= 0.5) {
  1064. av_log(NULL, AV_LOG_DEBUG, "Not duplicating %d initial frames\n", (int)lrintf(delta0));
  1065. delta = duration;
  1066. delta0 = 0;
  1067. ost->sync_opts = llrint(sync_ipts);
  1068. }
  1069. case VSYNC_CFR:
  1070. // FIXME set to 0.5 after we fix some dts/pts bugs like in avidec.c
  1071. if (frame_drop_threshold && delta < frame_drop_threshold && ost->frame_number) {
  1072. nb_frames = 0;
  1073. } else if (delta < -1.1)
  1074. nb_frames = 0;
  1075. else if (delta > 1.1) {
  1076. nb_frames = lrintf(delta);
  1077. if (delta0 > 1.1)
  1078. nb0_frames = llrintf(delta0 - 0.6);
  1079. }
  1080. break;
  1081. case VSYNC_VFR:
  1082. if (delta <= -0.6)
  1083. nb_frames = 0;
  1084. else if (delta > 0.6)
  1085. ost->sync_opts = llrint(sync_ipts);
  1086. break;
  1087. case VSYNC_DROP:
  1088. case VSYNC_PASSTHROUGH:
  1089. ost->sync_opts = llrint(sync_ipts);
  1090. break;
  1091. default:
  1092. av_assert0(0);
  1093. }
  1094. }
  1095. nb_frames = FFMIN(nb_frames, ost->max_frames - ost->frame_number);
  1096. nb0_frames = FFMIN(nb0_frames, nb_frames);
  1097. memmove(ost->last_nb0_frames + 1,
  1098. ost->last_nb0_frames,
  1099. sizeof(ost->last_nb0_frames[0]) * (FF_ARRAY_ELEMS(ost->last_nb0_frames) - 1));
  1100. ost->last_nb0_frames[0] = nb0_frames;
  1101. if (nb0_frames == 0 && ost->last_dropped) {
  1102. nb_frames_drop++;
  1103. av_log(NULL, AV_LOG_VERBOSE,
  1104. "*** dropping frame %d from stream %d at ts %"PRId64"\n",
  1105. ost->frame_number, ost->st->index, ost->last_frame->pts);
  1106. }
  1107. if (nb_frames > (nb0_frames && ost->last_dropped) + (nb_frames > nb0_frames)) {
  1108. if (nb_frames > dts_error_threshold * 30) {
  1109. av_log(NULL, AV_LOG_ERROR, "%d frame duplication too large, skipping\n", nb_frames - 1);
  1110. nb_frames_drop++;
  1111. return;
  1112. }
  1113. nb_frames_dup += nb_frames - (nb0_frames && ost->last_dropped) - (nb_frames > nb0_frames);
  1114. av_log(NULL, AV_LOG_VERBOSE, "*** %d dup!\n", nb_frames - 1);
  1115. if (nb_frames_dup > dup_warning) {
  1116. av_log(NULL, AV_LOG_WARNING, "More than %d frames duplicated\n", dup_warning);
  1117. dup_warning *= 10;
  1118. }
  1119. }
  1120. ost->last_dropped = nb_frames == nb0_frames && next_picture;
  1121. /* duplicates frame if needed */
  1122. for (i = 0; i < nb_frames; i++) {
  1123. AVFrame *in_picture;
  1124. int forced_keyframe = 0;
  1125. double pts_time;
  1126. if (i < nb0_frames && ost->last_frame) {
  1127. in_picture = ost->last_frame;
  1128. } else
  1129. in_picture = next_picture;
  1130. if (!in_picture)
  1131. return;
  1132. in_picture->pts = ost->sync_opts;
  1133. if (!check_recording_time(ost))
  1134. return;
  1135. in_picture->quality = enc->global_quality;
  1136. in_picture->pict_type = 0;
  1137. if (ost->forced_kf_ref_pts == AV_NOPTS_VALUE &&
  1138. in_picture->pts != AV_NOPTS_VALUE)
  1139. ost->forced_kf_ref_pts = in_picture->pts;
  1140. pts_time = in_picture->pts != AV_NOPTS_VALUE ?
  1141. (in_picture->pts - ost->forced_kf_ref_pts) * av_q2d(enc->time_base) : NAN;
  1142. if (ost->forced_kf_index < ost->forced_kf_count &&
  1143. in_picture->pts >= ost->forced_kf_pts[ost->forced_kf_index]) {
  1144. ost->forced_kf_index++;
  1145. forced_keyframe = 1;
  1146. } else if (ost->forced_keyframes_pexpr) {
  1147. double res;
  1148. ost->forced_keyframes_expr_const_values[FKF_T] = pts_time;
  1149. res = av_expr_eval(ost->forced_keyframes_pexpr,
  1150. ost->forced_keyframes_expr_const_values, NULL);
  1151. ff_dlog(NULL, "force_key_frame: n:%f n_forced:%f prev_forced_n:%f t:%f prev_forced_t:%f -> res:%f\n",
  1152. ost->forced_keyframes_expr_const_values[FKF_N],
  1153. ost->forced_keyframes_expr_const_values[FKF_N_FORCED],
  1154. ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_N],
  1155. ost->forced_keyframes_expr_const_values[FKF_T],
  1156. ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_T],
  1157. res);
  1158. if (res) {
  1159. forced_keyframe = 1;
  1160. ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_N] =
  1161. ost->forced_keyframes_expr_const_values[FKF_N];
  1162. ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_T] =
  1163. ost->forced_keyframes_expr_const_values[FKF_T];
  1164. ost->forced_keyframes_expr_const_values[FKF_N_FORCED] += 1;
  1165. }
  1166. ost->forced_keyframes_expr_const_values[FKF_N] += 1;
  1167. } else if ( ost->forced_keyframes
  1168. && !strncmp(ost->forced_keyframes, "source", 6)
  1169. && in_picture->key_frame==1
  1170. && !i) {
  1171. forced_keyframe = 1;
  1172. }
  1173. if (forced_keyframe) {
  1174. in_picture->pict_type = AV_PICTURE_TYPE_I;
  1175. av_log(NULL, AV_LOG_DEBUG, "Forced keyframe at time %f\n", pts_time);
  1176. }
  1177. update_benchmark(NULL);
  1178. if (debug_ts) {
  1179. av_log(NULL, AV_LOG_INFO, "encoder <- type:video "
  1180. "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n",
  1181. av_ts2str(in_picture->pts), av_ts2timestr(in_picture->pts, &enc->time_base),
  1182. enc->time_base.num, enc->time_base.den);
  1183. }
  1184. ost->frames_encoded++;
  1185. ret = avcodec_send_frame(enc, in_picture);
  1186. if (ret < 0)
  1187. goto error;
  1188. // Make sure Closed Captions will not be duplicated
  1189. av_frame_remove_side_data(in_picture, AV_FRAME_DATA_A53_CC);
  1190. while (1) {
  1191. av_packet_unref(pkt);
  1192. ret = avcodec_receive_packet(enc, pkt);
  1193. update_benchmark("encode_video %d.%d", ost->file_index, ost->index);
  1194. if (ret == AVERROR(EAGAIN))
  1195. break;
  1196. if (ret < 0)
  1197. goto error;
  1198. if (debug_ts) {
  1199. av_log(NULL, AV_LOG_INFO, "encoder -> type:video "
  1200. "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",
  1201. av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &enc->time_base),
  1202. av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &enc->time_base));
  1203. }
  1204. if (pkt->pts == AV_NOPTS_VALUE && !(enc->codec->capabilities & AV_CODEC_CAP_DELAY))
  1205. pkt->pts = ost->sync_opts;
  1206. av_packet_rescale_ts(pkt, enc->time_base, ost->mux_timebase);
  1207. if (debug_ts) {
  1208. av_log(NULL, AV_LOG_INFO, "encoder -> type:video "
  1209. "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",
  1210. av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->mux_timebase),
  1211. av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->mux_timebase));
  1212. }
  1213. frame_size = pkt->size;
  1214. output_packet(of, pkt, ost, 0);
  1215. /* if two pass, output log */
  1216. if (ost->logfile && enc->stats_out) {
  1217. fprintf(ost->logfile, "%s", enc->stats_out);
  1218. }
  1219. }
  1220. ost->sync_opts++;
  1221. /*
  1222. * For video, number of frames in == number of packets out.
  1223. * But there may be reordering, so we can't throw away frames on encoder
  1224. * flush, we need to limit them here, before they go into encoder.
  1225. */
  1226. ost->frame_number++;
  1227. if (vstats_filename && frame_size)
  1228. do_video_stats(ost, frame_size);
  1229. }
  1230. if (!ost->last_frame)
  1231. ost->last_frame = av_frame_alloc();
  1232. av_frame_unref(ost->last_frame);
  1233. if (next_picture && ost->last_frame)
  1234. av_frame_ref(ost->last_frame, next_picture);
  1235. else
  1236. av_frame_free(&ost->last_frame);
  1237. return;
  1238. error:
  1239. av_log(NULL, AV_LOG_FATAL, "Video encoding failed\n");
  1240. exit_program(1);
  1241. }
  1242. static double psnr(double d)
  1243. {
  1244. return -10.0 * log10(d);
  1245. }
  1246. static void do_video_stats(OutputStream *ost, int frame_size)
  1247. {
  1248. AVCodecContext *enc;
  1249. int frame_number;
  1250. double ti1, bitrate, avg_bitrate;
  1251. /* this is executed just the first time do_video_stats is called */
  1252. if (!vstats_file) {
  1253. vstats_file = fopen(vstats_filename, "w");
  1254. if (!vstats_file) {
  1255. perror("fopen");
  1256. exit_program(1);
  1257. }
  1258. }
  1259. enc = ost->enc_ctx;
  1260. if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
  1261. frame_number = ost->st->nb_frames;
  1262. if (vstats_version <= 1) {
  1263. fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number,
  1264. ost->quality / (float)FF_QP2LAMBDA);
  1265. } else {
  1266. fprintf(vstats_file, "out= %2d st= %2d frame= %5d q= %2.1f ", ost->file_index, ost->index, frame_number,
  1267. ost->quality / (float)FF_QP2LAMBDA);
  1268. }
  1269. if (ost->error[0]>=0 && (enc->flags & AV_CODEC_FLAG_PSNR))
  1270. fprintf(vstats_file, "PSNR= %6.2f ", psnr(ost->error[0] / (enc->width * enc->height * 255.0 * 255.0)));
  1271. fprintf(vstats_file,"f_size= %6d ", frame_size);
  1272. /* compute pts value */
  1273. ti1 = av_stream_get_end_pts(ost->st) * av_q2d(ost->st->time_base);
  1274. if (ti1 < 0.01)
  1275. ti1 = 0.01;
  1276. bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;
  1277. avg_bitrate = (double)(ost->data_size * 8) / ti1 / 1000.0;
  1278. fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
  1279. (double)ost->data_size / 1024, ti1, bitrate, avg_bitrate);
  1280. fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(ost->pict_type));
  1281. }
  1282. }
  1283. static void finish_output_stream(OutputStream *ost)
  1284. {
  1285. OutputFile *of = output_files[ost->file_index];
  1286. int i;
  1287. ost->finished = ENCODER_FINISHED | MUXER_FINISHED;
  1288. if (of->shortest) {
  1289. for (i = 0; i < of->ctx->nb_streams; i++)
  1290. output_streams[of->ost_index + i]->finished = ENCODER_FINISHED | MUXER_FINISHED;
  1291. }
  1292. }
  1293. /**
  1294. * Get and encode new output from any of the filtergraphs, without causing
  1295. * activity.
  1296. *
  1297. * @return 0 for success, <0 for severe errors
  1298. */
  1299. static int reap_filters(int flush)
  1300. {
  1301. AVFrame *filtered_frame = NULL;
  1302. int i;
  1303. /* Reap all buffers present in the buffer sinks */
  1304. for (i = 0; i < nb_output_streams; i++) {
  1305. OutputStream *ost = output_streams[i];
  1306. OutputFile *of = output_files[ost->file_index];
  1307. AVFilterContext *filter;
  1308. AVCodecContext *enc = ost->enc_ctx;
  1309. int ret = 0;
  1310. if (!ost->filter || !ost->filter->graph->graph)
  1311. continue;
  1312. filter = ost->filter->filter;
  1313. /*
  1314. * Unlike video, with audio the audio frame size matters.
  1315. * Currently we are fully reliant on the lavfi filter chain to
  1316. * do the buffering deed for us, and thus the frame size parameter
  1317. * needs to be set accordingly. Where does one get the required
  1318. * frame size? From the initialized AVCodecContext of an audio
  1319. * encoder. Thus, if we have gotten to an audio stream, initialize
  1320. * the encoder earlier than receiving the first AVFrame.
  1321. */
  1322. if (av_buffersink_get_type(filter) == AVMEDIA_TYPE_AUDIO)
  1323. init_output_stream_wrapper(ost, NULL, 1);
  1324. if (!ost->pkt && !(ost->pkt = av_packet_alloc())) {
  1325. return AVERROR(ENOMEM);
  1326. }
  1327. if (!ost->filtered_frame && !(ost->filtered_frame = av_frame_alloc())) {
  1328. return AVERROR(ENOMEM);
  1329. }
  1330. filtered_frame = ost->filtered_frame;
  1331. while (1) {
  1332. ret = av_buffersink_get_frame_flags(filter, filtered_frame,
  1333. AV_BUFFERSINK_FLAG_NO_REQUEST);
  1334. if (ret < 0) {
  1335. if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
  1336. av_log(NULL, AV_LOG_WARNING,
  1337. "Error in av_buffersink_get_frame_flags(): %s\n", av_err2str(ret));
  1338. } else if (flush && ret == AVERROR_EOF) {
  1339. if (av_buffersink_get_type(filter) == AVMEDIA_TYPE_VIDEO)
  1340. do_video_out(of, ost, NULL);
  1341. }
  1342. break;
  1343. }
  1344. if (ost->finished) {
  1345. av_frame_unref(filtered_frame);
  1346. continue;
  1347. }
  1348. switch (av_buffersink_get_type(filter)) {
  1349. case AVMEDIA_TYPE_VIDEO:
  1350. if (!ost->frame_aspect_ratio.num)
  1351. enc->sample_aspect_ratio = filtered_frame->sample_aspect_ratio;
  1352. do_video_out(of, ost, filtered_frame);
  1353. break;
  1354. case AVMEDIA_TYPE_AUDIO:
  1355. if (!(enc->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE) &&
  1356. enc->channels != filtered_frame->channels) {
  1357. av_log(NULL, AV_LOG_ERROR,
  1358. "Audio filter graph output is not normalized and encoder does not support parameter changes\n");
  1359. break;
  1360. }
  1361. do_audio_out(of, ost, filtered_frame);
  1362. break;
  1363. default:
  1364. // TODO support subtitle filters
  1365. av_assert0(0);
  1366. }
  1367. av_frame_unref(filtered_frame);
  1368. }
  1369. }
  1370. return 0;
  1371. }
  1372. static void print_final_stats(int64_t total_size)
  1373. {
  1374. uint64_t video_size = 0, audio_size = 0, extra_size = 0, other_size = 0;
  1375. uint64_t subtitle_size = 0;
  1376. uint64_t data_size = 0;
  1377. float percent = -1.0;
  1378. int i, j;
  1379. int pass1_used = 1;
  1380. for (i = 0; i < nb_output_streams; i++) {
  1381. OutputStream *ost = output_streams[i];
  1382. switch (ost->enc_ctx->codec_type) {
  1383. case AVMEDIA_TYPE_VIDEO: video_size += ost->data_size; break;
  1384. case AVMEDIA_TYPE_AUDIO: audio_size += ost->data_size; break;
  1385. case AVMEDIA_TYPE_SUBTITLE: subtitle_size += ost->data_size; break;
  1386. default: other_size += ost->data_size; break;
  1387. }
  1388. extra_size += ost->enc_ctx->extradata_size;
  1389. data_size += ost->data_size;
  1390. if ( (ost->enc_ctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2))
  1391. != AV_CODEC_FLAG_PASS1)
  1392. pass1_used = 0;
  1393. }
  1394. if (data_size && total_size>0 && total_size >= data_size)
  1395. percent = 100.0 * (total_size - data_size) / data_size;
  1396. av_log(NULL, AV_LOG_INFO, "video:%1.0fkB audio:%1.0fkB subtitle:%1.0fkB other streams:%1.0fkB global headers:%1.0fkB muxing overhead: ",
  1397. video_size / 1024.0,
  1398. audio_size / 1024.0,
  1399. subtitle_size / 1024.0,
  1400. other_size / 1024.0,
  1401. extra_size / 1024.0);
  1402. if (percent >= 0.0)
  1403. av_log(NULL, AV_LOG_INFO, "%f%%", percent);
  1404. else
  1405. av_log(NULL, AV_LOG_INFO, "unknown");
  1406. av_log(NULL, AV_LOG_INFO, "\n");
  1407. /* print verbose per-stream stats */
  1408. for (i = 0; i < nb_input_files; i++) {
  1409. InputFile *f = input_files[i];
  1410. uint64_t total_packets = 0, total_size = 0;
  1411. av_log(NULL, AV_LOG_VERBOSE, "Input file #%d (%s):\n",
  1412. i, f->ctx->url);
  1413. for (j = 0; j < f->nb_streams; j++) {
  1414. InputStream *ist = input_streams[f->ist_index + j];
  1415. enum AVMediaType type = ist->dec_ctx->codec_type;
  1416. total_size += ist->data_size;
  1417. total_packets += ist->nb_packets;
  1418. av_log(NULL, AV_LOG_VERBOSE, " Input stream #%d:%d (%s): ",
  1419. i, j, media_type_string(type));
  1420. av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" packets read (%"PRIu64" bytes); ",
  1421. ist->nb_packets, ist->data_size);
  1422. if (ist->decoding_needed) {
  1423. av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" frames decoded",
  1424. ist->frames_decoded);
  1425. if (type == AVMEDIA_TYPE_AUDIO)
  1426. av_log(NULL, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ist->samples_decoded);
  1427. av_log(NULL, AV_LOG_VERBOSE, "; ");
  1428. }
  1429. av_log(NULL, AV_LOG_VERBOSE, "\n");
  1430. }
  1431. av_log(NULL, AV_LOG_VERBOSE, " Total: %"PRIu64" packets (%"PRIu64" bytes) demuxed\n",
  1432. total_packets, total_size);
  1433. }
  1434. for (i = 0; i < nb_output_files; i++) {
  1435. OutputFile *of = output_files[i];
  1436. uint64_t total_packets = 0, total_size = 0;
  1437. av_log(NULL, AV_LOG_VERBOSE, "Output file #%d (%s):\n",
  1438. i, of->ctx->url);
  1439. for (j = 0; j < of->ctx->nb_streams; j++) {
  1440. OutputStream *ost = output_streams[of->ost_index + j];
  1441. enum AVMediaType type = ost->enc_ctx->codec_type;
  1442. total_size += ost->data_size;
  1443. total_packets += ost->packets_written;
  1444. av_log(NULL, AV_LOG_VERBOSE, " Output stream #%d:%d (%s): ",
  1445. i, j, media_type_string(type));
  1446. if (ost->encoding_needed) {
  1447. av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" frames encoded",
  1448. ost->frames_encoded);
  1449. if (type == AVMEDIA_TYPE_AUDIO)
  1450. av_log(NULL, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ost->samples_encoded);
  1451. av_log(NULL, AV_LOG_VERBOSE, "; ");
  1452. }
  1453. av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" packets muxed (%"PRIu64" bytes); ",
  1454. ost->packets_written, ost->data_size);
  1455. av_log(NULL, AV_LOG_VERBOSE, "\n");
  1456. }
  1457. av_log(NULL, AV_LOG_VERBOSE, " Total: %"PRIu64" packets (%"PRIu64" bytes) muxed\n",
  1458. total_packets, total_size);
  1459. }
  1460. if(video_size + data_size + audio_size + subtitle_size + extra_size == 0){
  1461. av_log(NULL, AV_LOG_WARNING, "Output file is empty, nothing was encoded ");
  1462. if (pass1_used) {
  1463. av_log(NULL, AV_LOG_WARNING, "\n");
  1464. } else {
  1465. av_log(NULL, AV_LOG_WARNING, "(check -ss / -t / -frames parameters if used)\n");
  1466. }
  1467. }
  1468. }
  1469. static void print_report(int is_last_report, int64_t timer_start, int64_t cur_time)
  1470. {
  1471. AVBPrint buf, buf_script;
  1472. OutputStream *ost;
  1473. AVFormatContext *oc;
  1474. int64_t total_size;
  1475. AVCodecContext *enc;
  1476. int frame_number, vid, i;
  1477. double bitrate;
  1478. double speed;
  1479. int64_t pts = INT64_MIN + 1;
  1480. static int64_t last_time = -1;
  1481. static int first_report = 1;
  1482. static int qp_histogram[52];
  1483. int hours, mins, secs, us;
  1484. const char *hours_sign;
  1485. int ret;
  1486. float t;
  1487. if (!print_stats && !is_last_report && !progress_avio)
  1488. return;
  1489. if (!is_last_report) {
  1490. if (last_time == -1) {
  1491. last_time = cur_time;
  1492. }
  1493. if (((cur_time - last_time) < stats_period && !first_report) ||
  1494. (first_report && nb_output_dumped < nb_output_files))
  1495. return;
  1496. last_time = cur_time;
  1497. }
  1498. t = (cur_time-timer_start) / 1000000.0;
  1499. oc = output_files[0]->ctx;
  1500. total_size = avio_size(oc->pb);
  1501. if (total_size <= 0) // FIXME improve avio_size() so it works with non seekable output too
  1502. total_size = avio_tell(oc->pb);
  1503. vid = 0;
  1504. av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC);
  1505. av_bprint_init(&buf_script, 0, AV_BPRINT_SIZE_AUTOMATIC);
  1506. for (i = 0; i < nb_output_streams; i++) {
  1507. float q = -1;
  1508. ost = output_streams[i];
  1509. enc = ost->enc_ctx;
  1510. if (!ost->stream_copy)
  1511. q = ost->quality / (float) FF_QP2LAMBDA;
  1512. if (vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
  1513. av_bprintf(&buf, "q=%2.1f ", q);
  1514. av_bprintf(&buf_script, "stream_%d_%d_q=%.1f\n",
  1515. ost->file_index, ost->index, q);
  1516. }
  1517. if (!vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
  1518. float fps;
  1519. frame_number = ost->frame_number;
  1520. fps = t > 1 ? frame_number / t : 0;
  1521. av_bprintf(&buf, "frame=%5d fps=%3.*f q=%3.1f ",
  1522. frame_number, fps < 9.95, fps, q);
  1523. av_bprintf(&buf_script, "frame=%d\n", frame_number);
  1524. av_bprintf(&buf_script, "fps=%.2f\n", fps);
  1525. av_bprintf(&buf_script, "stream_%d_%d_q=%.1f\n",
  1526. ost->file_index, ost->index, q);
  1527. if (is_last_report)
  1528. av_bprintf(&buf, "L");
  1529. if (qp_hist) {
  1530. int j;
  1531. int qp = lrintf(q);
  1532. if (qp >= 0 && qp < FF_ARRAY_ELEMS(qp_histogram))
  1533. qp_histogram[qp]++;
  1534. for (j = 0; j < 32; j++)
  1535. av_bprintf(&buf, "%X", av_log2(qp_histogram[j] + 1));
  1536. }
  1537. if ((enc->flags & AV_CODEC_FLAG_PSNR) && (ost->pict_type != AV_PICTURE_TYPE_NONE || is_last_report)) {
  1538. int j;
  1539. double error, error_sum = 0;
  1540. double scale, scale_sum = 0;
  1541. double p;
  1542. char type[3] = { 'Y','U','V' };
  1543. av_bprintf(&buf, "PSNR=");
  1544. for (j = 0; j < 3; j++) {
  1545. if (is_last_report) {
  1546. error = enc->error[j];
  1547. scale = enc->width * enc->height * 255.0 * 255.0 * frame_number;
  1548. } else {
  1549. error = ost->error[j];
  1550. scale = enc->width * enc->height * 255.0 * 255.0;
  1551. }
  1552. if (j)
  1553. scale /= 4;
  1554. error_sum += error;
  1555. scale_sum += scale;
  1556. p = psnr(error / scale);
  1557. av_bprintf(&buf, "%c:%2.2f ", type[j], p);
  1558. av_bprintf(&buf_script, "stream_%d_%d_psnr_%c=%2.2f\n",
  1559. ost->file_index, ost->index, type[j] | 32, p);
  1560. }
  1561. p = psnr(error_sum / scale_sum);
  1562. av_bprintf(&buf, "*:%2.2f ", psnr(error_sum / scale_sum));
  1563. av_bprintf(&buf_script, "stream_%d_%d_psnr_all=%2.2f\n",
  1564. ost->file_index, ost->index, p);
  1565. }
  1566. vid = 1;
  1567. }
  1568. /* compute min output value */
  1569. if (av_stream_get_end_pts(ost->st) != AV_NOPTS_VALUE) {
  1570. pts = FFMAX(pts, av_rescale_q(av_stream_get_end_pts(ost->st),
  1571. ost->st->time_base, AV_TIME_BASE_Q));
  1572. if (copy_ts) {
  1573. if (copy_ts_first_pts == AV_NOPTS_VALUE && pts > 1)
  1574. copy_ts_first_pts = pts;
  1575. if (copy_ts_first_pts != AV_NOPTS_VALUE)
  1576. pts -= copy_ts_first_pts;
  1577. }
  1578. }
  1579. if (is_last_report)
  1580. nb_frames_drop += ost->last_dropped;
  1581. }
  1582. secs = FFABS(pts) / AV_TIME_BASE;
  1583. us = FFABS(pts) % AV_TIME_BASE;
  1584. mins = secs / 60;
  1585. secs %= 60;
  1586. hours = mins / 60;
  1587. mins %= 60;
  1588. hours_sign = (pts < 0) ? "-" : "";
  1589. bitrate = pts && total_size >= 0 ? total_size * 8 / (pts / 1000.0) : -1;
  1590. speed = t != 0.0 ? (double)pts / AV_TIME_BASE / t : -1;
  1591. if (total_size < 0) av_bprintf(&buf, "size=N/A time=");
  1592. else av_bprintf(&buf, "size=%8.0fkB time=", total_size / 1024.0);
  1593. if (pts == AV_NOPTS_VALUE) {
  1594. av_bprintf(&buf, "N/A ");
  1595. } else {
  1596. av_bprintf(&buf, "%s%02d:%02d:%02d.%02d ",
  1597. hours_sign, hours, mins, secs, (100 * us) / AV_TIME_BASE);
  1598. }
  1599. if (bitrate < 0) {
  1600. av_bprintf(&buf, "bitrate=N/A");
  1601. av_bprintf(&buf_script, "bitrate=N/A\n");
  1602. }else{
  1603. av_bprintf(&buf, "bitrate=%6.1fkbits/s", bitrate);
  1604. av_bprintf(&buf_script, "bitrate=%6.1fkbits/s\n", bitrate);
  1605. }
  1606. if (total_size < 0) av_bprintf(&buf_script, "total_size=N/A\n");
  1607. else av_bprintf(&buf_script, "total_size=%"PRId64"\n", total_size);
  1608. if (pts == AV_NOPTS_VALUE) {
  1609. av_bprintf(&buf_script, "out_time_us=N/A\n");
  1610. av_bprintf(&buf_script, "out_time_ms=N/A\n");
  1611. av_bprintf(&buf_script, "out_time=N/A\n");
  1612. } else {
  1613. av_bprintf(&buf_script, "out_time_us=%"PRId64"\n", pts);
  1614. av_bprintf(&buf_script, "out_time_ms=%"PRId64"\n", pts);
  1615. av_bprintf(&buf_script, "out_time=%s%02d:%02d:%02d.%06d\n",
  1616. hours_sign, hours, mins, secs, us);
  1617. }
  1618. if (nb_frames_dup || nb_frames_drop)
  1619. av_bprintf(&buf, " dup=%d drop=%d", nb_frames_dup, nb_frames_drop);
  1620. av_bprintf(&buf_script, "dup_frames=%d\n", nb_frames_dup);
  1621. av_bprintf(&buf_script, "drop_frames=%d\n", nb_frames_drop);
  1622. if (speed < 0) {
  1623. av_bprintf(&buf, " speed=N/A");
  1624. av_bprintf(&buf_script, "speed=N/A\n");
  1625. } else {
  1626. av_bprintf(&buf, " speed=%4.3gx", speed);
  1627. av_bprintf(&buf_script, "speed=%4.3gx\n", speed);
  1628. }
  1629. if (print_stats || is_last_report) {
  1630. const char end = is_last_report ? '\n' : '\r';
  1631. if (print_stats==1 && AV_LOG_INFO > av_log_get_level()) {
  1632. fprintf(stderr, "%s %c", buf.str, end);
  1633. } else
  1634. av_log(NULL, AV_LOG_INFO, "%s %c", buf.str, end);
  1635. fflush(stderr);
  1636. }
  1637. av_bprint_finalize(&buf, NULL);
  1638. if (progress_avio) {
  1639. av_bprintf(&buf_script, "progress=%s\n",
  1640. is_last_report ? "end" : "continue");
  1641. avio_write(progress_avio, buf_script.str,
  1642. FFMIN(buf_script.len, buf_script.size - 1));
  1643. avio_flush(progress_avio);
  1644. av_bprint_finalize(&buf_script, NULL);
  1645. if (is_last_report) {
  1646. if ((ret = avio_closep(&progress_avio)) < 0)
  1647. av_log(NULL, AV_LOG_ERROR,
  1648. "Error closing progress log, loss of information possible: %s\n", av_err2str(ret));
  1649. }
  1650. }
  1651. first_report = 0;
  1652. if (is_last_report)
  1653. print_final_stats(total_size);
  1654. }
  1655. static void ifilter_parameters_from_codecpar(InputFilter *ifilter, AVCodecParameters *par)
  1656. {
  1657. // We never got any input. Set a fake format, which will
  1658. // come from libavformat.
  1659. ifilter->format = par->format;
  1660. ifilter->sample_rate = par->sample_rate;
  1661. ifilter->channels = par->channels;
  1662. ifilter->channel_layout = par->channel_layout;
  1663. ifilter->width = par->width;
  1664. ifilter->height = par->height;
  1665. ifilter->sample_aspect_ratio = par->sample_aspect_ratio;
  1666. }
  1667. static void flush_encoders(void)
  1668. {
  1669. int i, ret;
  1670. for (i = 0; i < nb_output_streams; i++) {
  1671. OutputStream *ost = output_streams[i];
  1672. AVCodecContext *enc = ost->enc_ctx;
  1673. OutputFile *of = output_files[ost->file_index];
  1674. if (!ost->encoding_needed)
  1675. continue;
  1676. // Try to enable encoding with no input frames.
  1677. // Maybe we should just let encoding fail instead.
  1678. if (!ost->initialized) {
  1679. FilterGraph *fg = ost->filter->graph;
  1680. av_log(NULL, AV_LOG_WARNING,
  1681. "Finishing stream %d:%d without any data written to it.\n",
  1682. ost->file_index, ost->st->index);
  1683. if (ost->filter && !fg->graph) {
  1684. int x;
  1685. for (x = 0; x < fg->nb_inputs; x++) {
  1686. InputFilter *ifilter = fg->inputs[x];
  1687. if (ifilter->format < 0)
  1688. ifilter_parameters_from_codecpar(ifilter, ifilter->ist->st->codecpar);
  1689. }
  1690. if (!ifilter_has_all_input_formats(fg))
  1691. continue;
  1692. ret = configure_filtergraph(fg);
  1693. if (ret < 0) {
  1694. av_log(NULL, AV_LOG_ERROR, "Error configuring filter graph\n");
  1695. exit_program(1);
  1696. }
  1697. finish_output_stream(ost);
  1698. }
  1699. init_output_stream_wrapper(ost, NULL, 1);
  1700. }
  1701. if (enc->codec_type != AVMEDIA_TYPE_VIDEO && enc->codec_type != AVMEDIA_TYPE_AUDIO)
  1702. continue;
  1703. for (;;) {
  1704. const char *desc = NULL;
  1705. AVPacket *pkt = ost->pkt;
  1706. int pkt_size;
  1707. switch (enc->codec_type) {
  1708. case AVMEDIA_TYPE_AUDIO:
  1709. desc = "audio";
  1710. break;
  1711. case AVMEDIA_TYPE_VIDEO:
  1712. desc = "video";
  1713. break;
  1714. default:
  1715. av_assert0(0);
  1716. }
  1717. update_benchmark(NULL);
  1718. av_packet_unref(pkt);
  1719. while ((ret = avcodec_receive_packet(enc, pkt)) == AVERROR(EAGAIN)) {
  1720. ret = avcodec_send_frame(enc, NULL);
  1721. if (ret < 0) {
  1722. av_log(NULL, AV_LOG_FATAL, "%s encoding failed: %s\n",
  1723. desc,
  1724. av_err2str(ret));
  1725. exit_program(1);
  1726. }
  1727. }
  1728. update_benchmark("flush_%s %d.%d", desc, ost->file_index, ost->index);
  1729. if (ret < 0 && ret != AVERROR_EOF) {
  1730. av_log(NULL, AV_LOG_FATAL, "%s encoding failed: %s\n",
  1731. desc,
  1732. av_err2str(ret));
  1733. exit_program(1);
  1734. }
  1735. if (ost->logfile && enc->stats_out) {
  1736. fprintf(ost->logfile, "%s", enc->stats_out);
  1737. }
  1738. if (ret == AVERROR_EOF) {
  1739. output_packet(of, pkt, ost, 1);
  1740. break;
  1741. }
  1742. if (ost->finished & MUXER_FINISHED) {
  1743. av_packet_unref(pkt);
  1744. continue;
  1745. }
  1746. av_packet_rescale_ts(pkt, enc->time_base, ost->mux_timebase);
  1747. pkt_size = pkt->size;
  1748. output_packet(of, pkt, ost, 0);
  1749. if (ost->enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO && vstats_filename) {
  1750. do_video_stats(ost, pkt_size);
  1751. }
  1752. }
  1753. }
  1754. }
  1755. /*
  1756. * Check whether a packet from ist should be written into ost at this time
  1757. */
  1758. static int check_output_constraints(InputStream *ist, OutputStream *ost)
  1759. {
  1760. OutputFile *of = output_files[ost->file_index];
  1761. int ist_index = input_files[ist->file_index]->ist_index + ist->st->index;
  1762. if (ost->source_index != ist_index)
  1763. return 0;
  1764. if (ost->finished)
  1765. return 0;
  1766. if (of->start_time != AV_NOPTS_VALUE && ist->pts < of->start_time)
  1767. return 0;
  1768. return 1;
  1769. }
  1770. static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *pkt)
  1771. {
  1772. OutputFile *of = output_files[ost->file_index];
  1773. InputFile *f = input_files [ist->file_index];
  1774. int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
  1775. int64_t ost_tb_start_time = av_rescale_q(start_time, AV_TIME_BASE_Q, ost->mux_timebase);
  1776. AVPacket *opkt = ost->pkt;
  1777. av_packet_unref(opkt);
  1778. // EOF: flush output bitstream filters.
  1779. if (!pkt) {
  1780. output_packet(of, opkt, ost, 1);
  1781. return;
  1782. }
  1783. if ((!ost->frame_number && !(pkt->flags & AV_PKT_FLAG_KEY)) &&
  1784. !ost->copy_initial_nonkeyframes)
  1785. return;
  1786. if (!ost->frame_number && !ost->copy_prior_start) {
  1787. int64_t comp_start = start_time;
  1788. if (copy_ts && f->start_time != AV_NOPTS_VALUE)
  1789. comp_start = FFMAX(start_time, f->start_time + f->ts_offset);
  1790. if (pkt->pts == AV_NOPTS_VALUE ?
  1791. ist->pts < comp_start :
  1792. pkt->pts < av_rescale_q(comp_start, AV_TIME_BASE_Q, ist->st->time_base))
  1793. return;
  1794. }
  1795. if (of->recording_time != INT64_MAX &&
  1796. ist->pts >= of->recording_time + start_time) {
  1797. close_output_stream(ost);
  1798. return;
  1799. }
  1800. if (f->recording_time != INT64_MAX) {
  1801. start_time = f->ctx->start_time;
  1802. if (f->start_time != AV_NOPTS_VALUE && copy_ts)
  1803. start_time += f->start_time;
  1804. if (ist->pts >= f->recording_time + start_time) {
  1805. close_output_stream(ost);
  1806. return;
  1807. }
  1808. }
  1809. /* force the input stream PTS */
  1810. if (ost->enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO)
  1811. ost->sync_opts++;
  1812. if (av_packet_ref(opkt, pkt) < 0)
  1813. exit_program(1);
  1814. if (pkt->pts != AV_NOPTS_VALUE)
  1815. opkt->pts = av_rescale_q(pkt->pts, ist->st->time_base, ost->mux_timebase) - ost_tb_start_time;
  1816. if (pkt->dts == AV_NOPTS_VALUE) {
  1817. opkt->dts = av_rescale_q(ist->dts, AV_TIME_BASE_Q, ost->mux_timebase);
  1818. } else if (ost->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
  1819. int duration = av_get_audio_frame_duration(ist->dec_ctx, pkt->size);
  1820. if(!duration)
  1821. duration = ist->dec_ctx->frame_size;
  1822. opkt->dts = av_rescale_delta(ist->st->time_base, pkt->dts,
  1823. (AVRational){1, ist->dec_ctx->sample_rate}, duration,
  1824. &ist->filter_in_rescale_delta_last, ost->mux_timebase);
  1825. /* dts will be set immediately afterwards to what pts is now */
  1826. opkt->pts = opkt->dts - ost_tb_start_time;
  1827. } else
  1828. opkt->dts = av_rescale_q(pkt->dts, ist->st->time_base, ost->mux_timebase);
  1829. opkt->dts -= ost_tb_start_time;
  1830. opkt->duration = av_rescale_q(pkt->duration, ist->st->time_base, ost->mux_timebase);
  1831. output_packet(of, opkt, ost, 0);
  1832. }
  1833. int guess_input_channel_layout(InputStream *ist)
  1834. {
  1835. AVCodecContext *dec = ist->dec_ctx;
  1836. if (!dec->channel_layout) {
  1837. char layout_name[256];
  1838. if (dec->channels > ist->guess_layout_max)
  1839. return 0;
  1840. dec->channel_layout = av_get_default_channel_layout(dec->channels);
  1841. if (!dec->channel_layout)
  1842. return 0;
  1843. av_get_channel_layout_string(layout_name, sizeof(layout_name),
  1844. dec->channels, dec->channel_layout);
  1845. av_log(NULL, AV_LOG_WARNING, "Guessed Channel Layout for Input Stream "
  1846. "#%d.%d : %s\n", ist->file_index, ist->st->index, layout_name);
  1847. }
  1848. return 1;
  1849. }
  1850. static void check_decode_result(InputStream *ist, int *got_output, int ret)
  1851. {
  1852. if (*got_output || ret<0)
  1853. decode_error_stat[ret<0] ++;
  1854. if (ret < 0 && exit_on_error)
  1855. exit_program(1);
  1856. if (*got_output && ist) {
  1857. if (ist->decoded_frame->decode_error_flags || (ist->decoded_frame->flags & AV_FRAME_FLAG_CORRUPT)) {
  1858. av_log(NULL, exit_on_error ? AV_LOG_FATAL : AV_LOG_WARNING,
  1859. "%s: corrupt decoded frame in stream %d\n", input_files[ist->file_index]->ctx->url, ist->st->index);
  1860. if (exit_on_error)
  1861. exit_program(1);
  1862. }
  1863. }
  1864. }
  1865. // Filters can be configured only if the formats of all inputs are known.
  1866. static int ifilter_has_all_input_formats(FilterGraph *fg)
  1867. {
  1868. int i;
  1869. for (i = 0; i < fg->nb_inputs; i++) {
  1870. if (fg->inputs[i]->format < 0 && (fg->inputs[i]->type == AVMEDIA_TYPE_AUDIO ||
  1871. fg->inputs[i]->type == AVMEDIA_TYPE_VIDEO))
  1872. return 0;
  1873. }
  1874. return 1;
  1875. }
  1876. static int ifilter_send_frame(InputFilter *ifilter, AVFrame *frame)
  1877. {
  1878. FilterGraph *fg = ifilter->graph;
  1879. int need_reinit, ret, i;
  1880. /* determine if the parameters for this input changed */
  1881. need_reinit = ifilter->format != frame->format;
  1882. switch (ifilter->ist->st->codecpar->codec_type) {
  1883. case AVMEDIA_TYPE_AUDIO:
  1884. need_reinit |= ifilter->sample_rate != frame->sample_rate ||
  1885. ifilter->channels != frame->channels ||
  1886. ifilter->channel_layout != frame->channel_layout;
  1887. break;
  1888. case AVMEDIA_TYPE_VIDEO:
  1889. need_reinit |= ifilter->width != frame->width ||
  1890. ifilter->height != frame->height;
  1891. break;
  1892. }
  1893. if (!ifilter->ist->reinit_filters && fg->graph)
  1894. need_reinit = 0;
  1895. if (!!ifilter->hw_frames_ctx != !!frame->hw_frames_ctx ||
  1896. (ifilter->hw_frames_ctx && ifilter->hw_frames_ctx->data != frame->hw_frames_ctx->data))
  1897. need_reinit = 1;
  1898. if (need_reinit) {
  1899. ret = ifilter_parameters_from_frame(ifilter, frame);
  1900. if (ret < 0)
  1901. return ret;
  1902. }
  1903. /* (re)init the graph if possible, otherwise buffer the frame and return */
  1904. if (need_reinit || !fg->graph) {
  1905. for (i = 0; i < fg->nb_inputs; i++) {
  1906. if (!ifilter_has_all_input_formats(fg)) {
  1907. AVFrame *tmp = av_frame_clone(frame);
  1908. if (!tmp)
  1909. return AVERROR(ENOMEM);
  1910. av_frame_unref(frame);
  1911. if (!av_fifo_space(ifilter->frame_queue)) {
  1912. ret = av_fifo_realloc2(ifilter->frame_queue, 2 * av_fifo_size(ifilter->frame_queue));
  1913. if (ret < 0) {
  1914. av_frame_free(&tmp);
  1915. return ret;
  1916. }
  1917. }
  1918. av_fifo_generic_write(ifilter->frame_queue, &tmp, sizeof(tmp), NULL);
  1919. return 0;
  1920. }
  1921. }
  1922. ret = reap_filters(1);
  1923. if (ret < 0 && ret != AVERROR_EOF) {
  1924. av_log(NULL, AV_LOG_ERROR, "Error while filtering: %s\n", av_err2str(ret));
  1925. return ret;
  1926. }
  1927. ret = configure_filtergraph(fg);
  1928. if (ret < 0) {
  1929. av_log(NULL, AV_LOG_ERROR, "Error reinitializing filters!\n");
  1930. return ret;
  1931. }
  1932. }
  1933. ret = av_buffersrc_add_frame_flags(ifilter->filter, frame, AV_BUFFERSRC_FLAG_PUSH);
  1934. if (ret < 0) {
  1935. if (ret != AVERROR_EOF)
  1936. av_log(NULL, AV_LOG_ERROR, "Error while filtering: %s\n", av_err2str(ret));
  1937. return ret;
  1938. }
  1939. return 0;
  1940. }
  1941. static int ifilter_send_eof(InputFilter *ifilter, int64_t pts)
  1942. {
  1943. int ret;
  1944. ifilter->eof = 1;
  1945. if (ifilter->filter) {
  1946. ret = av_buffersrc_close(ifilter->filter, pts, AV_BUFFERSRC_FLAG_PUSH);
  1947. if (ret < 0)
  1948. return ret;
  1949. } else {
  1950. // the filtergraph was never configured
  1951. if (ifilter->format < 0)
  1952. ifilter_parameters_from_codecpar(ifilter, ifilter->ist->st->codecpar);
  1953. if (ifilter->format < 0 && (ifilter->type == AVMEDIA_TYPE_AUDIO || ifilter->type == AVMEDIA_TYPE_VIDEO)) {
  1954. av_log(NULL, AV_LOG_ERROR, "Cannot determine format of input stream %d:%d after EOF\n", ifilter->ist->file_index, ifilter->ist->st->index);
  1955. return AVERROR_INVALIDDATA;
  1956. }
  1957. }
  1958. return 0;
  1959. }
  1960. // This does not quite work like avcodec_decode_audio4/avcodec_decode_video2.
  1961. // There is the following difference: if you got a frame, you must call
  1962. // it again with pkt=NULL. pkt==NULL is treated differently from pkt->size==0
  1963. // (pkt==NULL means get more output, pkt->size==0 is a flush/drain packet)
  1964. static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
  1965. {
  1966. int ret;
  1967. *got_frame = 0;
  1968. if (pkt) {
  1969. ret = avcodec_send_packet(avctx, pkt);
  1970. // In particular, we don't expect AVERROR(EAGAIN), because we read all
  1971. // decoded frames with avcodec_receive_frame() until done.
  1972. if (ret < 0 && ret != AVERROR_EOF)
  1973. return ret;
  1974. }
  1975. ret = avcodec_receive_frame(avctx, frame);
  1976. if (ret < 0 && ret != AVERROR(EAGAIN))
  1977. return ret;
  1978. if (ret >= 0)
  1979. *got_frame = 1;
  1980. return 0;
  1981. }
  1982. static int send_frame_to_filters(InputStream *ist, AVFrame *decoded_frame)
  1983. {
  1984. int i, ret;
  1985. AVFrame *f;
  1986. av_assert1(ist->nb_filters > 0); /* ensure ret is initialized */
  1987. for (i = 0; i < ist->nb_filters; i++) {
  1988. if (i < ist->nb_filters - 1) {
  1989. f = ist->filter_frame;
  1990. ret = av_frame_ref(f, decoded_frame);
  1991. if (ret < 0)
  1992. break;
  1993. } else
  1994. f = decoded_frame;
  1995. ret = ifilter_send_frame(ist->filters[i], f);
  1996. if (ret == AVERROR_EOF)
  1997. ret = 0; /* ignore */
  1998. if (ret < 0) {
  1999. av_log(NULL, AV_LOG_ERROR,
  2000. "Failed to inject frame into filter network: %s\n", av_err2str(ret));
  2001. break;
  2002. }
  2003. }
  2004. return ret;
  2005. }
  2006. static int decode_audio(InputStream *ist, AVPacket *pkt, int *got_output,
  2007. int *decode_failed)
  2008. {
  2009. AVFrame *decoded_frame;
  2010. AVCodecContext *avctx = ist->dec_ctx;
  2011. int ret, err = 0;
  2012. AVRational decoded_frame_tb;
  2013. if (!ist->decoded_frame && !(ist->decoded_frame = av_frame_alloc()))
  2014. return AVERROR(ENOMEM);
  2015. if (!ist->filter_frame && !(ist->filter_frame = av_frame_alloc()))
  2016. return AVERROR(ENOMEM);
  2017. decoded_frame = ist->decoded_frame;
  2018. update_benchmark(NULL);
  2019. ret = decode(avctx, decoded_frame, got_output, pkt);
  2020. update_benchmark("decode_audio %d.%d", ist->file_index, ist->st->index);
  2021. if (ret < 0)
  2022. *decode_failed = 1;
  2023. if (ret >= 0 && avctx->sample_rate <= 0) {
  2024. av_log(avctx, AV_LOG_ERROR, "Sample rate %d invalid\n", avctx->sample_rate);
  2025. ret = AVERROR_INVALIDDATA;
  2026. }
  2027. if (ret != AVERROR_EOF)
  2028. check_decode_result(ist, got_output, ret);
  2029. if (!*got_output || ret < 0)
  2030. return ret;
  2031. ist->samples_decoded += decoded_frame->nb_samples;
  2032. ist->frames_decoded++;
  2033. /* increment next_dts to use for the case where the input stream does not
  2034. have timestamps or there are multiple frames in the packet */
  2035. ist->next_pts += ((int64_t)AV_TIME_BASE * decoded_frame->nb_samples) /
  2036. avctx->sample_rate;
  2037. ist->next_dts += ((int64_t)AV_TIME_BASE * decoded_frame->nb_samples) /
  2038. avctx->sample_rate;
  2039. if (decoded_frame->pts != AV_NOPTS_VALUE) {
  2040. decoded_frame_tb = ist->st->time_base;
  2041. } else if (pkt && pkt->pts != AV_NOPTS_VALUE) {
  2042. decoded_frame->pts = pkt->pts;
  2043. decoded_frame_tb = ist->st->time_base;
  2044. }else {
  2045. decoded_frame->pts = ist->dts;
  2046. decoded_frame_tb = AV_TIME_BASE_Q;
  2047. }
  2048. if (decoded_frame->pts != AV_NOPTS_VALUE)
  2049. decoded_frame->pts = av_rescale_delta(decoded_frame_tb, decoded_frame->pts,
  2050. (AVRational){1, avctx->sample_rate}, decoded_frame->nb_samples, &ist->filter_in_rescale_delta_last,
  2051. (AVRational){1, avctx->sample_rate});
  2052. ist->nb_samples = decoded_frame->nb_samples;
  2053. err = send_frame_to_filters(ist, decoded_frame);
  2054. av_frame_unref(ist->filter_frame);
  2055. av_frame_unref(decoded_frame);
  2056. return err < 0 ? err : ret;
  2057. }
  2058. static int decode_video(InputStream *ist, AVPacket *pkt, int *got_output, int64_t *duration_pts, int eof,
  2059. int *decode_failed)
  2060. {
  2061. AVFrame *decoded_frame;
  2062. int i, ret = 0, err = 0;
  2063. int64_t best_effort_timestamp;
  2064. int64_t dts = AV_NOPTS_VALUE;
  2065. // With fate-indeo3-2, we're getting 0-sized packets before EOF for some
  2066. // reason. This seems like a semi-critical bug. Don't trigger EOF, and
  2067. // skip the packet.
  2068. if (!eof && pkt && pkt->size == 0)
  2069. return 0;
  2070. if (!ist->decoded_frame && !(ist->decoded_frame = av_frame_alloc()))
  2071. return AVERROR(ENOMEM);
  2072. if (!ist->filter_frame && !(ist->filter_frame = av_frame_alloc()))
  2073. return AVERROR(ENOMEM);
  2074. decoded_frame = ist->decoded_frame;
  2075. if (ist->dts != AV_NOPTS_VALUE)
  2076. dts = av_rescale_q(ist->dts, AV_TIME_BASE_Q, ist->st->time_base);
  2077. if (pkt) {
  2078. pkt->dts = dts; // ffmpeg.c probably shouldn't do this
  2079. }
  2080. // The old code used to set dts on the drain packet, which does not work
  2081. // with the new API anymore.
  2082. if (eof) {
  2083. void *new = av_realloc_array(ist->dts_buffer, ist->nb_dts_buffer + 1, sizeof(ist->dts_buffer[0]));
  2084. if (!new)
  2085. return AVERROR(ENOMEM);
  2086. ist->dts_buffer = new;
  2087. ist->dts_buffer[ist->nb_dts_buffer++] = dts;
  2088. }
  2089. update_benchmark(NULL);
  2090. ret = decode(ist->dec_ctx, decoded_frame, got_output, pkt);
  2091. update_benchmark("decode_video %d.%d", ist->file_index, ist->st->index);
  2092. if (ret < 0)
  2093. *decode_failed = 1;
  2094. // The following line may be required in some cases where there is no parser
  2095. // or the parser does not has_b_frames correctly
  2096. if (ist->st->codecpar->video_delay < ist->dec_ctx->has_b_frames) {
  2097. if (ist->dec_ctx->codec_id == AV_CODEC_ID_H264) {
  2098. ist->st->codecpar->video_delay = ist->dec_ctx->has_b_frames;
  2099. } else
  2100. av_log(ist->dec_ctx, AV_LOG_WARNING,
  2101. "video_delay is larger in decoder than demuxer %d > %d.\n"
  2102. "If you want to help, upload a sample "
  2103. "of this file to https://streams.videolan.org/upload/ "
  2104. "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)\n",
  2105. ist->dec_ctx->has_b_frames,
  2106. ist->st->codecpar->video_delay);
  2107. }
  2108. if (ret != AVERROR_EOF)
  2109. check_decode_result(ist, got_output, ret);
  2110. if (*got_output && ret >= 0) {
  2111. if (ist->dec_ctx->width != decoded_frame->width ||
  2112. ist->dec_ctx->height != decoded_frame->height ||
  2113. ist->dec_ctx->pix_fmt != decoded_frame->format) {
  2114. av_log(NULL, AV_LOG_DEBUG, "Frame parameters mismatch context %d,%d,%d != %d,%d,%d\n",
  2115. decoded_frame->width,
  2116. decoded_frame->height,
  2117. decoded_frame->format,
  2118. ist->dec_ctx->width,
  2119. ist->dec_ctx->height,
  2120. ist->dec_ctx->pix_fmt);
  2121. }
  2122. }
  2123. if (!*got_output || ret < 0)
  2124. return ret;
  2125. if(ist->top_field_first>=0)
  2126. decoded_frame->top_field_first = ist->top_field_first;
  2127. ist->frames_decoded++;
  2128. if (ist->hwaccel_retrieve_data && decoded_frame->format == ist->hwaccel_pix_fmt) {
  2129. err = ist->hwaccel_retrieve_data(ist->dec_ctx, decoded_frame);
  2130. if (err < 0)
  2131. goto fail;
  2132. }
  2133. ist->hwaccel_retrieved_pix_fmt = decoded_frame->format;
  2134. best_effort_timestamp= decoded_frame->best_effort_timestamp;
  2135. *duration_pts = decoded_frame->pkt_duration;
  2136. if (ist->framerate.num)
  2137. best_effort_timestamp = ist->cfr_next_pts++;
  2138. if (eof && best_effort_timestamp == AV_NOPTS_VALUE && ist->nb_dts_buffer > 0) {
  2139. best_effort_timestamp = ist->dts_buffer[0];
  2140. for (i = 0; i < ist->nb_dts_buffer - 1; i++)
  2141. ist->dts_buffer[i] = ist->dts_buffer[i + 1];
  2142. ist->nb_dts_buffer--;
  2143. }
  2144. if(best_effort_timestamp != AV_NOPTS_VALUE) {
  2145. int64_t ts = av_rescale_q(decoded_frame->pts = best_effort_timestamp, ist->st->time_base, AV_TIME_BASE_Q);
  2146. if (ts != AV_NOPTS_VALUE)
  2147. ist->next_pts = ist->pts = ts;
  2148. }
  2149. if (debug_ts) {
  2150. av_log(NULL, AV_LOG_INFO, "decoder -> ist_index:%d type:video "
  2151. "frame_pts:%s frame_pts_time:%s best_effort_ts:%"PRId64" best_effort_ts_time:%s keyframe:%d frame_type:%d time_base:%d/%d\n",
  2152. ist->st->index, av_ts2str(decoded_frame->pts),
  2153. av_ts2timestr(decoded_frame->pts, &ist->st->time_base),
  2154. best_effort_timestamp,
  2155. av_ts2timestr(best_effort_timestamp, &ist->st->time_base),
  2156. decoded_frame->key_frame, decoded_frame->pict_type,
  2157. ist->st->time_base.num, ist->st->time_base.den);
  2158. }
  2159. if (ist->st->sample_aspect_ratio.num)
  2160. decoded_frame->sample_aspect_ratio = ist->st->sample_aspect_ratio;
  2161. err = send_frame_to_filters(ist, decoded_frame);
  2162. fail:
  2163. av_frame_unref(ist->filter_frame);
  2164. av_frame_unref(decoded_frame);
  2165. return err < 0 ? err : ret;
  2166. }
  2167. static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output,
  2168. int *decode_failed)
  2169. {
  2170. AVSubtitle subtitle;
  2171. int free_sub = 1;
  2172. int i, ret = avcodec_decode_subtitle2(ist->dec_ctx,
  2173. &subtitle, got_output, pkt);
  2174. check_decode_result(NULL, got_output, ret);
  2175. if (ret < 0 || !*got_output) {
  2176. *decode_failed = 1;
  2177. if (!pkt->size)
  2178. sub2video_flush(ist);
  2179. return ret;
  2180. }
  2181. if (ist->fix_sub_duration) {
  2182. int end = 1;
  2183. if (ist->prev_sub.got_output) {
  2184. end = av_rescale(subtitle.pts - ist->prev_sub.subtitle.pts,
  2185. 1000, AV_TIME_BASE);
  2186. if (end < ist->prev_sub.subtitle.end_display_time) {
  2187. av_log(ist->dec_ctx, AV_LOG_DEBUG,
  2188. "Subtitle duration reduced from %"PRId32" to %d%s\n",
  2189. ist->prev_sub.subtitle.end_display_time, end,
  2190. end <= 0 ? ", dropping it" : "");
  2191. ist->prev_sub.subtitle.end_display_time = end;
  2192. }
  2193. }
  2194. FFSWAP(int, *got_output, ist->prev_sub.got_output);
  2195. FFSWAP(int, ret, ist->prev_sub.ret);
  2196. FFSWAP(AVSubtitle, subtitle, ist->prev_sub.subtitle);
  2197. if (end <= 0)
  2198. goto out;
  2199. }
  2200. if (!*got_output)
  2201. return ret;
  2202. if (ist->sub2video.frame) {
  2203. sub2video_update(ist, INT64_MIN, &subtitle);
  2204. } else if (ist->nb_filters) {
  2205. if (!ist->sub2video.sub_queue)
  2206. ist->sub2video.sub_queue = av_fifo_alloc(8 * sizeof(AVSubtitle));
  2207. if (!ist->sub2video.sub_queue)
  2208. exit_program(1);
  2209. if (!av_fifo_space(ist->sub2video.sub_queue)) {
  2210. ret = av_fifo_realloc2(ist->sub2video.sub_queue, 2 * av_fifo_size(ist->sub2video.sub_queue));
  2211. if (ret < 0)
  2212. exit_program(1);
  2213. }
  2214. av_fifo_generic_write(ist->sub2video.sub_queue, &subtitle, sizeof(subtitle), NULL);
  2215. free_sub = 0;
  2216. }
  2217. if (!subtitle.num_rects)
  2218. goto out;
  2219. ist->frames_decoded++;
  2220. for (i = 0; i < nb_output_streams; i++) {
  2221. OutputStream *ost = output_streams[i];
  2222. if (!ost->pkt && !(ost->pkt = av_packet_alloc()))
  2223. exit_program(1);
  2224. if (!check_output_constraints(ist, ost) || !ost->encoding_needed
  2225. || ost->enc->type != AVMEDIA_TYPE_SUBTITLE)
  2226. continue;
  2227. do_subtitle_out(output_files[ost->file_index], ost, &subtitle);
  2228. }
  2229. out:
  2230. if (free_sub)
  2231. avsubtitle_free(&subtitle);
  2232. return ret;
  2233. }
  2234. static int send_filter_eof(InputStream *ist)
  2235. {
  2236. int i, ret;
  2237. /* TODO keep pts also in stream time base to avoid converting back */
  2238. int64_t pts = av_rescale_q_rnd(ist->pts, AV_TIME_BASE_Q, ist->st->time_base,
  2239. AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
  2240. for (i = 0; i < ist->nb_filters; i++) {
  2241. ret = ifilter_send_eof(ist->filters[i], pts);
  2242. if (ret < 0)
  2243. return ret;
  2244. }
  2245. return 0;
  2246. }
  2247. /* pkt = NULL means EOF (needed to flush decoder buffers) */
  2248. static int process_input_packet(InputStream *ist, const AVPacket *pkt, int no_eof)
  2249. {
  2250. int ret = 0, i;
  2251. int repeating = 0;
  2252. int eof_reached = 0;
  2253. AVPacket *avpkt;
  2254. if (!ist->pkt && !(ist->pkt = av_packet_alloc()))
  2255. return AVERROR(ENOMEM);
  2256. avpkt = ist->pkt;
  2257. if (!ist->saw_first_ts) {
  2258. ist->dts = ist->st->avg_frame_rate.num ? - ist->dec_ctx->has_b_frames * AV_TIME_BASE / av_q2d(ist->st->avg_frame_rate) : 0;
  2259. ist->pts = 0;
  2260. if (pkt && pkt->pts != AV_NOPTS_VALUE && !ist->decoding_needed) {
  2261. ist->dts += av_rescale_q(pkt->pts, ist->st->time_base, AV_TIME_BASE_Q);
  2262. ist->pts = ist->dts; //unused but better to set it to a value thats not totally wrong
  2263. }
  2264. ist->saw_first_ts = 1;
  2265. }
  2266. if (ist->next_dts == AV_NOPTS_VALUE)
  2267. ist->next_dts = ist->dts;
  2268. if (ist->next_pts == AV_NOPTS_VALUE)
  2269. ist->next_pts = ist->pts;
  2270. if (pkt) {
  2271. av_packet_unref(avpkt);
  2272. ret = av_packet_ref(avpkt, pkt);
  2273. if (ret < 0)
  2274. return ret;
  2275. }
  2276. if (pkt && pkt->dts != AV_NOPTS_VALUE) {
  2277. ist->next_dts = ist->dts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);
  2278. if (ist->dec_ctx->codec_type != AVMEDIA_TYPE_VIDEO || !ist->decoding_needed)
  2279. ist->next_pts = ist->pts = ist->dts;
  2280. }
  2281. // while we have more to decode or while the decoder did output something on EOF
  2282. while (ist->decoding_needed) {
  2283. int64_t duration_dts = 0;
  2284. int64_t duration_pts = 0;
  2285. int got_output = 0;
  2286. int decode_failed = 0;
  2287. ist->pts = ist->next_pts;
  2288. ist->dts = ist->next_dts;
  2289. switch (ist->dec_ctx->codec_type) {
  2290. case AVMEDIA_TYPE_AUDIO:
  2291. ret = decode_audio (ist, repeating ? NULL : avpkt, &got_output,
  2292. &decode_failed);
  2293. av_packet_unref(avpkt);
  2294. break;
  2295. case AVMEDIA_TYPE_VIDEO:
  2296. ret = decode_video (ist, repeating ? NULL : avpkt, &got_output, &duration_pts, !pkt,
  2297. &decode_failed);
  2298. if (!repeating || !pkt || got_output) {
  2299. if (pkt && pkt->duration) {
  2300. duration_dts = av_rescale_q(pkt->duration, ist->st->time_base, AV_TIME_BASE_Q);
  2301. } else if(ist->dec_ctx->framerate.num != 0 && ist->dec_ctx->framerate.den != 0) {
  2302. int ticks= av_stream_get_parser(ist->st) ? av_stream_get_parser(ist->st)->repeat_pict+1 : ist->dec_ctx->ticks_per_frame;
  2303. duration_dts = ((int64_t)AV_TIME_BASE *
  2304. ist->dec_ctx->framerate.den * ticks) /
  2305. ist->dec_ctx->framerate.num / ist->dec_ctx->ticks_per_frame;
  2306. }
  2307. if(ist->dts != AV_NOPTS_VALUE && duration_dts) {
  2308. ist->next_dts += duration_dts;
  2309. }else
  2310. ist->next_dts = AV_NOPTS_VALUE;
  2311. }
  2312. if (got_output) {
  2313. if (duration_pts > 0) {
  2314. ist->next_pts += av_rescale_q(duration_pts, ist->st->time_base, AV_TIME_BASE_Q);
  2315. } else {
  2316. ist->next_pts += duration_dts;
  2317. }
  2318. }
  2319. av_packet_unref(avpkt);
  2320. break;
  2321. case AVMEDIA_TYPE_SUBTITLE:
  2322. if (repeating)
  2323. break;
  2324. ret = transcode_subtitles(ist, avpkt, &got_output, &decode_failed);
  2325. if (!pkt && ret >= 0)
  2326. ret = AVERROR_EOF;
  2327. av_packet_unref(avpkt);
  2328. break;
  2329. default:
  2330. return -1;
  2331. }
  2332. if (ret == AVERROR_EOF) {
  2333. eof_reached = 1;
  2334. break;
  2335. }
  2336. if (ret < 0) {
  2337. if (decode_failed) {
  2338. av_log(NULL, AV_LOG_ERROR, "Error while decoding stream #%d:%d: %s\n",
  2339. ist->file_index, ist->st->index, av_err2str(ret));
  2340. } else {
  2341. av_log(NULL, AV_LOG_FATAL, "Error while processing the decoded "
  2342. "data for stream #%d:%d\n", ist->file_index, ist->st->index);
  2343. }
  2344. if (!decode_failed || exit_on_error)
  2345. exit_program(1);
  2346. break;
  2347. }
  2348. if (got_output)
  2349. ist->got_output = 1;
  2350. if (!got_output)
  2351. break;
  2352. // During draining, we might get multiple output frames in this loop.
  2353. // ffmpeg.c does not drain the filter chain on configuration changes,
  2354. // which means if we send multiple frames at once to the filters, and
  2355. // one of those frames changes configuration, the buffered frames will
  2356. // be lost. This can upset certain FATE tests.
  2357. // Decode only 1 frame per call on EOF to appease these FATE tests.
  2358. // The ideal solution would be to rewrite decoding to use the new
  2359. // decoding API in a better way.
  2360. if (!pkt)
  2361. break;
  2362. repeating = 1;
  2363. }
  2364. /* after flushing, send an EOF on all the filter inputs attached to the stream */
  2365. /* except when looping we need to flush but not to send an EOF */
  2366. if (!pkt && ist->decoding_needed && eof_reached && !no_eof) {
  2367. int ret = send_filter_eof(ist);
  2368. if (ret < 0) {
  2369. av_log(NULL, AV_LOG_FATAL, "Error marking filters as finished\n");
  2370. exit_program(1);
  2371. }
  2372. }
  2373. /* handle stream copy */
  2374. if (!ist->decoding_needed && pkt) {
  2375. ist->dts = ist->next_dts;
  2376. switch (ist->dec_ctx->codec_type) {
  2377. case AVMEDIA_TYPE_AUDIO:
  2378. av_assert1(pkt->duration >= 0);
  2379. if (ist->dec_ctx->sample_rate) {
  2380. ist->next_dts += ((int64_t)AV_TIME_BASE * ist->dec_ctx->frame_size) /
  2381. ist->dec_ctx->sample_rate;
  2382. } else {
  2383. ist->next_dts += av_rescale_q(pkt->duration, ist->st->time_base, AV_TIME_BASE_Q);
  2384. }
  2385. break;
  2386. case AVMEDIA_TYPE_VIDEO:
  2387. if (ist->framerate.num) {
  2388. // TODO: Remove work-around for c99-to-c89 issue 7
  2389. AVRational time_base_q = AV_TIME_BASE_Q;
  2390. int64_t next_dts = av_rescale_q(ist->next_dts, time_base_q, av_inv_q(ist->framerate));
  2391. ist->next_dts = av_rescale_q(next_dts + 1, av_inv_q(ist->framerate), time_base_q);
  2392. } else if (pkt->duration) {
  2393. ist->next_dts += av_rescale_q(pkt->duration, ist->st->time_base, AV_TIME_BASE_Q);
  2394. } else if(ist->dec_ctx->framerate.num != 0) {
  2395. int ticks= av_stream_get_parser(ist->st) ? av_stream_get_parser(ist->st)->repeat_pict + 1 : ist->dec_ctx->ticks_per_frame;
  2396. ist->next_dts += ((int64_t)AV_TIME_BASE *
  2397. ist->dec_ctx->framerate.den * ticks) /
  2398. ist->dec_ctx->framerate.num / ist->dec_ctx->ticks_per_frame;
  2399. }
  2400. break;
  2401. }
  2402. ist->pts = ist->dts;
  2403. ist->next_pts = ist->next_dts;
  2404. }
  2405. for (i = 0; i < nb_output_streams; i++) {
  2406. OutputStream *ost = output_streams[i];
  2407. if (!ost->pkt && !(ost->pkt = av_packet_alloc()))
  2408. exit_program(1);
  2409. if (!check_output_constraints(ist, ost) || ost->encoding_needed)
  2410. continue;
  2411. do_streamcopy(ist, ost, pkt);
  2412. }
  2413. return !eof_reached;
  2414. }
  2415. static void print_sdp(void)
  2416. {
  2417. char sdp[16384];
  2418. int i;
  2419. int j;
  2420. AVIOContext *sdp_pb;
  2421. AVFormatContext **avc;
  2422. for (i = 0; i < nb_output_files; i++) {
  2423. if (!output_files[i]->header_written)
  2424. return;
  2425. }
  2426. avc = av_malloc_array(nb_output_files, sizeof(*avc));
  2427. if (!avc)
  2428. exit_program(1);
  2429. for (i = 0, j = 0; i < nb_output_files; i++) {
  2430. if (!strcmp(output_files[i]->ctx->oformat->name, "rtp")) {
  2431. avc[j] = output_files[i]->ctx;
  2432. j++;
  2433. }
  2434. }
  2435. if (!j)
  2436. goto fail;
  2437. av_sdp_create(avc, j, sdp, sizeof(sdp));
  2438. if (!sdp_filename) {
  2439. printf("SDP:\n%s\n", sdp);
  2440. fflush(stdout);
  2441. } else {
  2442. if (avio_open2(&sdp_pb, sdp_filename, AVIO_FLAG_WRITE, &int_cb, NULL) < 0) {
  2443. av_log(NULL, AV_LOG_ERROR, "Failed to open sdp file '%s'\n", sdp_filename);
  2444. } else {
  2445. avio_print(sdp_pb, sdp);
  2446. avio_closep(&sdp_pb);
  2447. av_freep(&sdp_filename);
  2448. }
  2449. }
  2450. fail:
  2451. av_freep(&avc);
  2452. }
  2453. static enum AVPixelFormat get_format(AVCodecContext *s, const enum AVPixelFormat *pix_fmts)
  2454. {
  2455. InputStream *ist = s->opaque;
  2456. const enum AVPixelFormat *p;
  2457. int ret;
  2458. for (p = pix_fmts; *p != AV_PIX_FMT_NONE; p++) {
  2459. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(*p);
  2460. const AVCodecHWConfig *config = NULL;
  2461. int i;
  2462. if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
  2463. break;
  2464. if (ist->hwaccel_id == HWACCEL_GENERIC ||
  2465. ist->hwaccel_id == HWACCEL_AUTO) {
  2466. for (i = 0;; i++) {
  2467. config = avcodec_get_hw_config(s->codec, i);
  2468. if (!config)
  2469. break;
  2470. if (!(config->methods &
  2471. AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
  2472. continue;
  2473. if (config->pix_fmt == *p)
  2474. break;
  2475. }
  2476. }
  2477. if (config) {
  2478. if (config->device_type != ist->hwaccel_device_type) {
  2479. // Different hwaccel offered, ignore.
  2480. continue;
  2481. }
  2482. ret = hwaccel_decode_init(s);
  2483. if (ret < 0) {
  2484. if (ist->hwaccel_id == HWACCEL_GENERIC) {
  2485. av_log(NULL, AV_LOG_FATAL,
  2486. "%s hwaccel requested for input stream #%d:%d, "
  2487. "but cannot be initialized.\n",
  2488. av_hwdevice_get_type_name(config->device_type),
  2489. ist->file_index, ist->st->index);
  2490. return AV_PIX_FMT_NONE;
  2491. }
  2492. continue;
  2493. }
  2494. } else {
  2495. const HWAccel *hwaccel = NULL;
  2496. int i;
  2497. for (i = 0; hwaccels[i].name; i++) {
  2498. if (hwaccels[i].pix_fmt == *p) {
  2499. hwaccel = &hwaccels[i];
  2500. break;
  2501. }
  2502. }
  2503. if (!hwaccel) {
  2504. // No hwaccel supporting this pixfmt.
  2505. continue;
  2506. }
  2507. if (hwaccel->id != ist->hwaccel_id) {
  2508. // Does not match requested hwaccel.
  2509. continue;
  2510. }
  2511. ret = hwaccel->init(s);
  2512. if (ret < 0) {
  2513. av_log(NULL, AV_LOG_FATAL,
  2514. "%s hwaccel requested for input stream #%d:%d, "
  2515. "but cannot be initialized.\n", hwaccel->name,
  2516. ist->file_index, ist->st->index);
  2517. return AV_PIX_FMT_NONE;
  2518. }
  2519. }
  2520. if (ist->hw_frames_ctx) {
  2521. s->hw_frames_ctx = av_buffer_ref(ist->hw_frames_ctx);
  2522. if (!s->hw_frames_ctx)
  2523. return AV_PIX_FMT_NONE;
  2524. }
  2525. ist->hwaccel_pix_fmt = *p;
  2526. break;
  2527. }
  2528. return *p;
  2529. }
  2530. static int get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
  2531. {
  2532. InputStream *ist = s->opaque;
  2533. if (ist->hwaccel_get_buffer && frame->format == ist->hwaccel_pix_fmt)
  2534. return ist->hwaccel_get_buffer(s, frame, flags);
  2535. return avcodec_default_get_buffer2(s, frame, flags);
  2536. }
  2537. static int init_input_stream(int ist_index, char *error, int error_len)
  2538. {
  2539. int ret;
  2540. InputStream *ist = input_streams[ist_index];
  2541. if (ist->decoding_needed) {
  2542. const AVCodec *codec = ist->dec;
  2543. if (!codec) {
  2544. snprintf(error, error_len, "Decoder (codec %s) not found for input stream #%d:%d",
  2545. avcodec_get_name(ist->dec_ctx->codec_id), ist->file_index, ist->st->index);
  2546. return AVERROR(EINVAL);
  2547. }
  2548. ist->dec_ctx->opaque = ist;
  2549. ist->dec_ctx->get_format = get_format;
  2550. ist->dec_ctx->get_buffer2 = get_buffer;
  2551. #if LIBAVCODEC_VERSION_MAJOR < 60
  2552. ist->dec_ctx->thread_safe_callbacks = 1;
  2553. #endif
  2554. if (ist->dec_ctx->codec_id == AV_CODEC_ID_DVB_SUBTITLE &&
  2555. (ist->decoding_needed & DECODING_FOR_OST)) {
  2556. av_dict_set(&ist->decoder_opts, "compute_edt", "1", AV_DICT_DONT_OVERWRITE);
  2557. if (ist->decoding_needed & DECODING_FOR_FILTER)
  2558. av_log(NULL, AV_LOG_WARNING, "Warning using DVB subtitles for filtering and output at the same time is not fully supported, also see -compute_edt [0|1]\n");
  2559. }
  2560. av_dict_set(&ist->decoder_opts, "sub_text_format", "ass", AV_DICT_DONT_OVERWRITE);
  2561. /* Useful for subtitles retiming by lavf (FIXME), skipping samples in
  2562. * audio, and video decoders such as cuvid or mediacodec */
  2563. ist->dec_ctx->pkt_timebase = ist->st->time_base;
  2564. if (!av_dict_get(ist->decoder_opts, "threads", NULL, 0))
  2565. av_dict_set(&ist->decoder_opts, "threads", "auto", 0);
  2566. /* Attached pics are sparse, therefore we would not want to delay their decoding till EOF. */
  2567. if (ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC)
  2568. av_dict_set(&ist->decoder_opts, "threads", "1", 0);
  2569. ret = hw_device_setup_for_decode(ist);
  2570. if (ret < 0) {
  2571. snprintf(error, error_len, "Device setup failed for "
  2572. "decoder on input stream #%d:%d : %s",
  2573. ist->file_index, ist->st->index, av_err2str(ret));
  2574. return ret;
  2575. }
  2576. if ((ret = avcodec_open2(ist->dec_ctx, codec, &ist->decoder_opts)) < 0) {
  2577. if (ret == AVERROR_EXPERIMENTAL)
  2578. abort_codec_experimental(codec, 0);
  2579. snprintf(error, error_len,
  2580. "Error while opening decoder for input stream "
  2581. "#%d:%d : %s",
  2582. ist->file_index, ist->st->index, av_err2str(ret));
  2583. return ret;
  2584. }
  2585. assert_avoptions(ist->decoder_opts);
  2586. }
  2587. ist->next_pts = AV_NOPTS_VALUE;
  2588. ist->next_dts = AV_NOPTS_VALUE;
  2589. return 0;
  2590. }
  2591. static InputStream *get_input_stream(OutputStream *ost)
  2592. {
  2593. if (ost->source_index >= 0)
  2594. return input_streams[ost->source_index];
  2595. return NULL;
  2596. }
  2597. static int compare_int64(const void *a, const void *b)
  2598. {
  2599. return FFDIFFSIGN(*(const int64_t *)a, *(const int64_t *)b);
  2600. }
  2601. /* open the muxer when all the streams are initialized */
  2602. static int check_init_output_file(OutputFile *of, int file_index)
  2603. {
  2604. int ret, i;
  2605. for (i = 0; i < of->ctx->nb_streams; i++) {
  2606. OutputStream *ost = output_streams[of->ost_index + i];
  2607. if (!ost->initialized)
  2608. return 0;
  2609. }
  2610. of->ctx->interrupt_callback = int_cb;
  2611. ret = avformat_write_header(of->ctx, &of->opts);
  2612. if (ret < 0) {
  2613. av_log(NULL, AV_LOG_ERROR,
  2614. "Could not write header for output file #%d "
  2615. "(incorrect codec parameters ?): %s\n",
  2616. file_index, av_err2str(ret));
  2617. return ret;
  2618. }
  2619. //assert_avoptions(of->opts);
  2620. of->header_written = 1;
  2621. av_dump_format(of->ctx, file_index, of->ctx->url, 1);
  2622. nb_output_dumped++;
  2623. if (sdp_filename || want_sdp)
  2624. print_sdp();
  2625. /* flush the muxing queues */
  2626. for (i = 0; i < of->ctx->nb_streams; i++) {
  2627. OutputStream *ost = output_streams[of->ost_index + i];
  2628. /* try to improve muxing time_base (only possible if nothing has been written yet) */
  2629. if (!av_fifo_size(ost->muxing_queue))
  2630. ost->mux_timebase = ost->st->time_base;
  2631. while (av_fifo_size(ost->muxing_queue)) {
  2632. AVPacket *pkt;
  2633. av_fifo_generic_read(ost->muxing_queue, &pkt, sizeof(pkt), NULL);
  2634. ost->muxing_queue_data_size -= pkt->size;
  2635. write_packet(of, pkt, ost, 1);
  2636. av_packet_free(&pkt);
  2637. }
  2638. }
  2639. return 0;
  2640. }
  2641. static int init_output_bsfs(OutputStream *ost)
  2642. {
  2643. AVBSFContext *ctx = ost->bsf_ctx;
  2644. int ret;
  2645. if (!ctx)
  2646. return 0;
  2647. ret = avcodec_parameters_copy(ctx->par_in, ost->st->codecpar);
  2648. if (ret < 0)
  2649. return ret;
  2650. ctx->time_base_in = ost->st->time_base;
  2651. ret = av_bsf_init(ctx);
  2652. if (ret < 0) {
  2653. av_log(NULL, AV_LOG_ERROR, "Error initializing bitstream filter: %s\n",
  2654. ctx->filter->name);
  2655. return ret;
  2656. }
  2657. ret = avcodec_parameters_copy(ost->st->codecpar, ctx->par_out);
  2658. if (ret < 0)
  2659. return ret;
  2660. ost->st->time_base = ctx->time_base_out;
  2661. return 0;
  2662. }
  2663. static int init_output_stream_streamcopy(OutputStream *ost)
  2664. {
  2665. OutputFile *of = output_files[ost->file_index];
  2666. InputStream *ist = get_input_stream(ost);
  2667. AVCodecParameters *par_dst = ost->st->codecpar;
  2668. AVCodecParameters *par_src = ost->ref_par;
  2669. AVRational sar;
  2670. int i, ret;
  2671. uint32_t codec_tag = par_dst->codec_tag;
  2672. av_assert0(ist && !ost->filter);
  2673. ret = avcodec_parameters_to_context(ost->enc_ctx, ist->st->codecpar);
  2674. if (ret >= 0)
  2675. ret = av_opt_set_dict(ost->enc_ctx, &ost->encoder_opts);
  2676. if (ret < 0) {
  2677. av_log(NULL, AV_LOG_FATAL,
  2678. "Error setting up codec context options.\n");
  2679. return ret;
  2680. }
  2681. ret = avcodec_parameters_from_context(par_src, ost->enc_ctx);
  2682. if (ret < 0) {
  2683. av_log(NULL, AV_LOG_FATAL,
  2684. "Error getting reference codec parameters.\n");
  2685. return ret;
  2686. }
  2687. if (!codec_tag) {
  2688. unsigned int codec_tag_tmp;
  2689. if (!of->ctx->oformat->codec_tag ||
  2690. av_codec_get_id (of->ctx->oformat->codec_tag, par_src->codec_tag) == par_src->codec_id ||
  2691. !av_codec_get_tag2(of->ctx->oformat->codec_tag, par_src->codec_id, &codec_tag_tmp))
  2692. codec_tag = par_src->codec_tag;
  2693. }
  2694. ret = avcodec_parameters_copy(par_dst, par_src);
  2695. if (ret < 0)
  2696. return ret;
  2697. par_dst->codec_tag = codec_tag;
  2698. if (!ost->frame_rate.num)
  2699. ost->frame_rate = ist->framerate;
  2700. ost->st->avg_frame_rate = ost->frame_rate;
  2701. ret = avformat_transfer_internal_stream_timing_info(of->ctx->oformat, ost->st, ist->st, copy_tb);
  2702. if (ret < 0)
  2703. return ret;
  2704. // copy timebase while removing common factors
  2705. if (ost->st->time_base.num <= 0 || ost->st->time_base.den <= 0)
  2706. ost->st->time_base = av_add_q(av_stream_get_codec_timebase(ost->st), (AVRational){0, 1});
  2707. // copy estimated duration as a hint to the muxer
  2708. if (ost->st->duration <= 0 && ist->st->duration > 0)
  2709. ost->st->duration = av_rescale_q(ist->st->duration, ist->st->time_base, ost->st->time_base);
  2710. // copy disposition
  2711. ost->st->disposition = ist->st->disposition;
  2712. if (ist->st->nb_side_data) {
  2713. for (i = 0; i < ist->st->nb_side_data; i++) {
  2714. const AVPacketSideData *sd_src = &ist->st->side_data[i];
  2715. uint8_t *dst_data;
  2716. dst_data = av_stream_new_side_data(ost->st, sd_src->type, sd_src->size);
  2717. if (!dst_data)
  2718. return AVERROR(ENOMEM);
  2719. memcpy(dst_data, sd_src->data, sd_src->size);
  2720. }
  2721. }
  2722. if (ost->rotate_overridden) {
  2723. uint8_t *sd = av_stream_new_side_data(ost->st, AV_PKT_DATA_DISPLAYMATRIX,
  2724. sizeof(int32_t) * 9);
  2725. if (sd)
  2726. av_display_rotation_set((int32_t *)sd, -ost->rotate_override_value);
  2727. }
  2728. switch (par_dst->codec_type) {
  2729. case AVMEDIA_TYPE_AUDIO:
  2730. if (audio_volume != 256) {
  2731. av_log(NULL, AV_LOG_FATAL, "-acodec copy and -vol are incompatible (frames are not decoded)\n");
  2732. exit_program(1);
  2733. }
  2734. if((par_dst->block_align == 1 || par_dst->block_align == 1152 || par_dst->block_align == 576) && par_dst->codec_id == AV_CODEC_ID_MP3)
  2735. par_dst->block_align= 0;
  2736. if(par_dst->codec_id == AV_CODEC_ID_AC3)
  2737. par_dst->block_align= 0;
  2738. break;
  2739. case AVMEDIA_TYPE_VIDEO:
  2740. if (ost->frame_aspect_ratio.num) { // overridden by the -aspect cli option
  2741. sar =
  2742. av_mul_q(ost->frame_aspect_ratio,
  2743. (AVRational){ par_dst->height, par_dst->width });
  2744. av_log(NULL, AV_LOG_WARNING, "Overriding aspect ratio "
  2745. "with stream copy may produce invalid files\n");
  2746. }
  2747. else if (ist->st->sample_aspect_ratio.num)
  2748. sar = ist->st->sample_aspect_ratio;
  2749. else
  2750. sar = par_src->sample_aspect_ratio;
  2751. ost->st->sample_aspect_ratio = par_dst->sample_aspect_ratio = sar;
  2752. ost->st->avg_frame_rate = ist->st->avg_frame_rate;
  2753. ost->st->r_frame_rate = ist->st->r_frame_rate;
  2754. break;
  2755. }
  2756. ost->mux_timebase = ist->st->time_base;
  2757. return 0;
  2758. }
  2759. static void set_encoder_id(OutputFile *of, OutputStream *ost)
  2760. {
  2761. AVDictionaryEntry *e;
  2762. uint8_t *encoder_string;
  2763. int encoder_string_len;
  2764. int format_flags = 0;
  2765. int codec_flags = ost->enc_ctx->flags;
  2766. if (av_dict_get(ost->st->metadata, "encoder", NULL, 0))
  2767. return;
  2768. e = av_dict_get(of->opts, "fflags", NULL, 0);
  2769. if (e) {
  2770. const AVOption *o = av_opt_find(of->ctx, "fflags", NULL, 0, 0);
  2771. if (!o)
  2772. return;
  2773. av_opt_eval_flags(of->ctx, o, e->value, &format_flags);
  2774. }
  2775. e = av_dict_get(ost->encoder_opts, "flags", NULL, 0);
  2776. if (e) {
  2777. const AVOption *o = av_opt_find(ost->enc_ctx, "flags", NULL, 0, 0);
  2778. if (!o)
  2779. return;
  2780. av_opt_eval_flags(ost->enc_ctx, o, e->value, &codec_flags);
  2781. }
  2782. encoder_string_len = sizeof(LIBAVCODEC_IDENT) + strlen(ost->enc->name) + 2;
  2783. encoder_string = av_mallocz(encoder_string_len);
  2784. if (!encoder_string)
  2785. exit_program(1);
  2786. if (!(format_flags & AVFMT_FLAG_BITEXACT) && !(codec_flags & AV_CODEC_FLAG_BITEXACT))
  2787. av_strlcpy(encoder_string, LIBAVCODEC_IDENT " ", encoder_string_len);
  2788. else
  2789. av_strlcpy(encoder_string, "Lavc ", encoder_string_len);
  2790. av_strlcat(encoder_string, ost->enc->name, encoder_string_len);
  2791. av_dict_set(&ost->st->metadata, "encoder", encoder_string,
  2792. AV_DICT_DONT_STRDUP_VAL | AV_DICT_DONT_OVERWRITE);
  2793. }
  2794. static void parse_forced_key_frames(char *kf, OutputStream *ost,
  2795. AVCodecContext *avctx)
  2796. {
  2797. char *p;
  2798. int n = 1, i, size, index = 0;
  2799. int64_t t, *pts;
  2800. for (p = kf; *p; p++)
  2801. if (*p == ',')
  2802. n++;
  2803. size = n;
  2804. pts = av_malloc_array(size, sizeof(*pts));
  2805. if (!pts) {
  2806. av_log(NULL, AV_LOG_FATAL, "Could not allocate forced key frames array.\n");
  2807. exit_program(1);
  2808. }
  2809. p = kf;
  2810. for (i = 0; i < n; i++) {
  2811. char *next = strchr(p, ',');
  2812. if (next)
  2813. *next++ = 0;
  2814. if (!memcmp(p, "chapters", 8)) {
  2815. AVFormatContext *avf = output_files[ost->file_index]->ctx;
  2816. int j;
  2817. if (avf->nb_chapters > INT_MAX - size ||
  2818. !(pts = av_realloc_f(pts, size += avf->nb_chapters - 1,
  2819. sizeof(*pts)))) {
  2820. av_log(NULL, AV_LOG_FATAL,
  2821. "Could not allocate forced key frames array.\n");
  2822. exit_program(1);
  2823. }
  2824. t = p[8] ? parse_time_or_die("force_key_frames", p + 8, 1) : 0;
  2825. t = av_rescale_q(t, AV_TIME_BASE_Q, avctx->time_base);
  2826. for (j = 0; j < avf->nb_chapters; j++) {
  2827. AVChapter *c = avf->chapters[j];
  2828. av_assert1(index < size);
  2829. pts[index++] = av_rescale_q(c->start, c->time_base,
  2830. avctx->time_base) + t;
  2831. }
  2832. } else {
  2833. t = parse_time_or_die("force_key_frames", p, 1);
  2834. av_assert1(index < size);
  2835. pts[index++] = av_rescale_q(t, AV_TIME_BASE_Q, avctx->time_base);
  2836. }
  2837. p = next;
  2838. }
  2839. av_assert0(index == size);
  2840. qsort(pts, size, sizeof(*pts), compare_int64);
  2841. ost->forced_kf_count = size;
  2842. ost->forced_kf_pts = pts;
  2843. }
  2844. static void init_encoder_time_base(OutputStream *ost, AVRational default_time_base)
  2845. {
  2846. InputStream *ist = get_input_stream(ost);
  2847. AVCodecContext *enc_ctx = ost->enc_ctx;
  2848. AVFormatContext *oc;
  2849. if (ost->enc_timebase.num > 0) {
  2850. enc_ctx->time_base = ost->enc_timebase;
  2851. return;
  2852. }
  2853. if (ost->enc_timebase.num < 0) {
  2854. if (ist) {
  2855. enc_ctx->time_base = ist->st->time_base;
  2856. return;
  2857. }
  2858. oc = output_files[ost->file_index]->ctx;
  2859. av_log(oc, AV_LOG_WARNING, "Input stream data not available, using default time base\n");
  2860. }
  2861. enc_ctx->time_base = default_time_base;
  2862. }
  2863. static int init_output_stream_encode(OutputStream *ost, AVFrame *frame)
  2864. {
  2865. InputStream *ist = get_input_stream(ost);
  2866. AVCodecContext *enc_ctx = ost->enc_ctx;
  2867. AVCodecContext *dec_ctx = NULL;
  2868. AVFormatContext *oc = output_files[ost->file_index]->ctx;
  2869. int j, ret;
  2870. set_encoder_id(output_files[ost->file_index], ost);
  2871. // Muxers use AV_PKT_DATA_DISPLAYMATRIX to signal rotation. On the other
  2872. // hand, the legacy API makes demuxers set "rotate" metadata entries,
  2873. // which have to be filtered out to prevent leaking them to output files.
  2874. av_dict_set(&ost->st->metadata, "rotate", NULL, 0);
  2875. if (ist) {
  2876. ost->st->disposition = ist->st->disposition;
  2877. dec_ctx = ist->dec_ctx;
  2878. enc_ctx->chroma_sample_location = dec_ctx->chroma_sample_location;
  2879. } else {
  2880. for (j = 0; j < oc->nb_streams; j++) {
  2881. AVStream *st = oc->streams[j];
  2882. if (st != ost->st && st->codecpar->codec_type == ost->st->codecpar->codec_type)
  2883. break;
  2884. }
  2885. if (j == oc->nb_streams)
  2886. if (ost->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO ||
  2887. ost->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
  2888. ost->st->disposition = AV_DISPOSITION_DEFAULT;
  2889. }
  2890. if (enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  2891. if (!ost->frame_rate.num)
  2892. ost->frame_rate = av_buffersink_get_frame_rate(ost->filter->filter);
  2893. if (ist && !ost->frame_rate.num)
  2894. ost->frame_rate = ist->framerate;
  2895. if (ist && !ost->frame_rate.num)
  2896. ost->frame_rate = ist->st->r_frame_rate;
  2897. if (ist && !ost->frame_rate.num && !ost->max_frame_rate.num) {
  2898. ost->frame_rate = (AVRational){25, 1};
  2899. av_log(NULL, AV_LOG_WARNING,
  2900. "No information "
  2901. "about the input framerate is available. Falling "
  2902. "back to a default value of 25fps for output stream #%d:%d. Use the -r option "
  2903. "if you want a different framerate.\n",
  2904. ost->file_index, ost->index);
  2905. }
  2906. if (ost->max_frame_rate.num &&
  2907. (av_q2d(ost->frame_rate) > av_q2d(ost->max_frame_rate) ||
  2908. !ost->frame_rate.den))
  2909. ost->frame_rate = ost->max_frame_rate;
  2910. if (ost->enc->supported_framerates && !ost->force_fps) {
  2911. int idx = av_find_nearest_q_idx(ost->frame_rate, ost->enc->supported_framerates);
  2912. ost->frame_rate = ost->enc->supported_framerates[idx];
  2913. }
  2914. // reduce frame rate for mpeg4 to be within the spec limits
  2915. if (enc_ctx->codec_id == AV_CODEC_ID_MPEG4) {
  2916. av_reduce(&ost->frame_rate.num, &ost->frame_rate.den,
  2917. ost->frame_rate.num, ost->frame_rate.den, 65535);
  2918. }
  2919. }
  2920. switch (enc_ctx->codec_type) {
  2921. case AVMEDIA_TYPE_AUDIO:
  2922. enc_ctx->sample_fmt = av_buffersink_get_format(ost->filter->filter);
  2923. if (dec_ctx)
  2924. enc_ctx->bits_per_raw_sample = FFMIN(dec_ctx->bits_per_raw_sample,
  2925. av_get_bytes_per_sample(enc_ctx->sample_fmt) << 3);
  2926. enc_ctx->sample_rate = av_buffersink_get_sample_rate(ost->filter->filter);
  2927. enc_ctx->channel_layout = av_buffersink_get_channel_layout(ost->filter->filter);
  2928. enc_ctx->channels = av_buffersink_get_channels(ost->filter->filter);
  2929. init_encoder_time_base(ost, av_make_q(1, enc_ctx->sample_rate));
  2930. break;
  2931. case AVMEDIA_TYPE_VIDEO:
  2932. init_encoder_time_base(ost, av_inv_q(ost->frame_rate));
  2933. if (!(enc_ctx->time_base.num && enc_ctx->time_base.den))
  2934. enc_ctx->time_base = av_buffersink_get_time_base(ost->filter->filter);
  2935. if ( av_q2d(enc_ctx->time_base) < 0.001 && video_sync_method != VSYNC_PASSTHROUGH
  2936. && (video_sync_method == VSYNC_CFR || video_sync_method == VSYNC_VSCFR || (video_sync_method == VSYNC_AUTO && !(oc->oformat->flags & AVFMT_VARIABLE_FPS)))){
  2937. av_log(oc, AV_LOG_WARNING, "Frame rate very high for a muxer not efficiently supporting it.\n"
  2938. "Please consider specifying a lower framerate, a different muxer or -vsync 2\n");
  2939. }
  2940. enc_ctx->width = av_buffersink_get_w(ost->filter->filter);
  2941. enc_ctx->height = av_buffersink_get_h(ost->filter->filter);
  2942. enc_ctx->sample_aspect_ratio = ost->st->sample_aspect_ratio =
  2943. ost->frame_aspect_ratio.num ? // overridden by the -aspect cli option
  2944. av_mul_q(ost->frame_aspect_ratio, (AVRational){ enc_ctx->height, enc_ctx->width }) :
  2945. av_buffersink_get_sample_aspect_ratio(ost->filter->filter);
  2946. enc_ctx->pix_fmt = av_buffersink_get_format(ost->filter->filter);
  2947. if (dec_ctx)
  2948. enc_ctx->bits_per_raw_sample = FFMIN(dec_ctx->bits_per_raw_sample,
  2949. av_pix_fmt_desc_get(enc_ctx->pix_fmt)->comp[0].depth);
  2950. if (frame) {
  2951. enc_ctx->color_range = frame->color_range;
  2952. enc_ctx->color_primaries = frame->color_primaries;
  2953. enc_ctx->color_trc = frame->color_trc;
  2954. enc_ctx->colorspace = frame->colorspace;
  2955. enc_ctx->chroma_sample_location = frame->chroma_location;
  2956. }
  2957. enc_ctx->framerate = ost->frame_rate;
  2958. ost->st->avg_frame_rate = ost->frame_rate;
  2959. if (!dec_ctx ||
  2960. enc_ctx->width != dec_ctx->width ||
  2961. enc_ctx->height != dec_ctx->height ||
  2962. enc_ctx->pix_fmt != dec_ctx->pix_fmt) {
  2963. enc_ctx->bits_per_raw_sample = frame_bits_per_raw_sample;
  2964. }
  2965. if (ost->top_field_first == 0) {
  2966. enc_ctx->field_order = AV_FIELD_BB;
  2967. } else if (ost->top_field_first == 1) {
  2968. enc_ctx->field_order = AV_FIELD_TT;
  2969. }
  2970. if (frame) {
  2971. if (enc_ctx->flags & (AV_CODEC_FLAG_INTERLACED_DCT | AV_CODEC_FLAG_INTERLACED_ME) &&
  2972. ost->top_field_first >= 0)
  2973. frame->top_field_first = !!ost->top_field_first;
  2974. if (frame->interlaced_frame) {
  2975. if (enc_ctx->codec->id == AV_CODEC_ID_MJPEG)
  2976. enc_ctx->field_order = frame->top_field_first ? AV_FIELD_TT:AV_FIELD_BB;
  2977. else
  2978. enc_ctx->field_order = frame->top_field_first ? AV_FIELD_TB:AV_FIELD_BT;
  2979. } else
  2980. enc_ctx->field_order = AV_FIELD_PROGRESSIVE;
  2981. }
  2982. if (ost->forced_keyframes) {
  2983. if (!strncmp(ost->forced_keyframes, "expr:", 5)) {
  2984. ret = av_expr_parse(&ost->forced_keyframes_pexpr, ost->forced_keyframes+5,
  2985. forced_keyframes_const_names, NULL, NULL, NULL, NULL, 0, NULL);
  2986. if (ret < 0) {
  2987. av_log(NULL, AV_LOG_ERROR,
  2988. "Invalid force_key_frames expression '%s'\n", ost->forced_keyframes+5);
  2989. return ret;
  2990. }
  2991. ost->forced_keyframes_expr_const_values[FKF_N] = 0;
  2992. ost->forced_keyframes_expr_const_values[FKF_N_FORCED] = 0;
  2993. ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_N] = NAN;
  2994. ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_T] = NAN;
  2995. // Don't parse the 'forced_keyframes' in case of 'keep-source-keyframes',
  2996. // parse it only for static kf timings
  2997. } else if(strncmp(ost->forced_keyframes, "source", 6)) {
  2998. parse_forced_key_frames(ost->forced_keyframes, ost, ost->enc_ctx);
  2999. }
  3000. }
  3001. break;
  3002. case AVMEDIA_TYPE_SUBTITLE:
  3003. enc_ctx->time_base = AV_TIME_BASE_Q;
  3004. if (!enc_ctx->width) {
  3005. enc_ctx->width = input_streams[ost->source_index]->st->codecpar->width;
  3006. enc_ctx->height = input_streams[ost->source_index]->st->codecpar->height;
  3007. }
  3008. break;
  3009. case AVMEDIA_TYPE_DATA:
  3010. break;
  3011. default:
  3012. abort();
  3013. break;
  3014. }
  3015. ost->mux_timebase = enc_ctx->time_base;
  3016. return 0;
  3017. }
  3018. static int init_output_stream(OutputStream *ost, AVFrame *frame,
  3019. char *error, int error_len)
  3020. {
  3021. int ret = 0;
  3022. if (ost->encoding_needed) {
  3023. const AVCodec *codec = ost->enc;
  3024. AVCodecContext *dec = NULL;
  3025. InputStream *ist;
  3026. ret = init_output_stream_encode(ost, frame);
  3027. if (ret < 0)
  3028. return ret;
  3029. if ((ist = get_input_stream(ost)))
  3030. dec = ist->dec_ctx;
  3031. if (dec && dec->subtitle_header) {
  3032. /* ASS code assumes this buffer is null terminated so add extra byte. */
  3033. ost->enc_ctx->subtitle_header = av_mallocz(dec->subtitle_header_size + 1);
  3034. if (!ost->enc_ctx->subtitle_header)
  3035. return AVERROR(ENOMEM);
  3036. memcpy(ost->enc_ctx->subtitle_header, dec->subtitle_header, dec->subtitle_header_size);
  3037. ost->enc_ctx->subtitle_header_size = dec->subtitle_header_size;
  3038. }
  3039. if (!av_dict_get(ost->encoder_opts, "threads", NULL, 0))
  3040. av_dict_set(&ost->encoder_opts, "threads", "auto", 0);
  3041. if (ost->enc->type == AVMEDIA_TYPE_AUDIO &&
  3042. !codec->defaults &&
  3043. !av_dict_get(ost->encoder_opts, "b", NULL, 0) &&
  3044. !av_dict_get(ost->encoder_opts, "ab", NULL, 0))
  3045. av_dict_set(&ost->encoder_opts, "b", "128000", 0);
  3046. ret = hw_device_setup_for_encode(ost);
  3047. if (ret < 0) {
  3048. snprintf(error, error_len, "Device setup failed for "
  3049. "encoder on output stream #%d:%d : %s",
  3050. ost->file_index, ost->index, av_err2str(ret));
  3051. return ret;
  3052. }
  3053. if (ist && ist->dec->type == AVMEDIA_TYPE_SUBTITLE && ost->enc->type == AVMEDIA_TYPE_SUBTITLE) {
  3054. int input_props = 0, output_props = 0;
  3055. AVCodecDescriptor const *input_descriptor =
  3056. avcodec_descriptor_get(dec->codec_id);
  3057. AVCodecDescriptor const *output_descriptor =
  3058. avcodec_descriptor_get(ost->enc_ctx->codec_id);
  3059. if (input_descriptor)
  3060. input_props = input_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
  3061. if (output_descriptor)
  3062. output_props = output_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
  3063. if (input_props && output_props && input_props != output_props) {
  3064. snprintf(error, error_len,
  3065. "Subtitle encoding currently only possible from text to text "
  3066. "or bitmap to bitmap");
  3067. return AVERROR_INVALIDDATA;
  3068. }
  3069. }
  3070. if ((ret = avcodec_open2(ost->enc_ctx, codec, &ost->encoder_opts)) < 0) {
  3071. if (ret == AVERROR_EXPERIMENTAL)
  3072. abort_codec_experimental(codec, 1);
  3073. snprintf(error, error_len,
  3074. "Error while opening encoder for output stream #%d:%d - "
  3075. "maybe incorrect parameters such as bit_rate, rate, width or height",
  3076. ost->file_index, ost->index);
  3077. return ret;
  3078. }
  3079. if (ost->enc->type == AVMEDIA_TYPE_AUDIO &&
  3080. !(ost->enc->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE))
  3081. av_buffersink_set_frame_size(ost->filter->filter,
  3082. ost->enc_ctx->frame_size);
  3083. assert_avoptions(ost->encoder_opts);
  3084. if (ost->enc_ctx->bit_rate && ost->enc_ctx->bit_rate < 1000 &&
  3085. ost->enc_ctx->codec_id != AV_CODEC_ID_CODEC2 /* don't complain about 700 bit/s modes */)
  3086. av_log(NULL, AV_LOG_WARNING, "The bitrate parameter is set too low."
  3087. " It takes bits/s as argument, not kbits/s\n");
  3088. ret = avcodec_parameters_from_context(ost->st->codecpar, ost->enc_ctx);
  3089. if (ret < 0) {
  3090. av_log(NULL, AV_LOG_FATAL,
  3091. "Error initializing the output stream codec context.\n");
  3092. exit_program(1);
  3093. }
  3094. if (ost->enc_ctx->nb_coded_side_data) {
  3095. int i;
  3096. for (i = 0; i < ost->enc_ctx->nb_coded_side_data; i++) {
  3097. const AVPacketSideData *sd_src = &ost->enc_ctx->coded_side_data[i];
  3098. uint8_t *dst_data;
  3099. dst_data = av_stream_new_side_data(ost->st, sd_src->type, sd_src->size);
  3100. if (!dst_data)
  3101. return AVERROR(ENOMEM);
  3102. memcpy(dst_data, sd_src->data, sd_src->size);
  3103. }
  3104. }
  3105. /*
  3106. * Add global input side data. For now this is naive, and copies it
  3107. * from the input stream's global side data. All side data should
  3108. * really be funneled over AVFrame and libavfilter, then added back to
  3109. * packet side data, and then potentially using the first packet for
  3110. * global side data.
  3111. */
  3112. if (ist) {
  3113. int i;
  3114. for (i = 0; i < ist->st->nb_side_data; i++) {
  3115. AVPacketSideData *sd = &ist->st->side_data[i];
  3116. if (sd->type != AV_PKT_DATA_CPB_PROPERTIES) {
  3117. uint8_t *dst = av_stream_new_side_data(ost->st, sd->type, sd->size);
  3118. if (!dst)
  3119. return AVERROR(ENOMEM);
  3120. memcpy(dst, sd->data, sd->size);
  3121. if (ist->autorotate && sd->type == AV_PKT_DATA_DISPLAYMATRIX)
  3122. av_display_rotation_set((uint32_t *)dst, 0);
  3123. }
  3124. }
  3125. }
  3126. // copy timebase while removing common factors
  3127. if (ost->st->time_base.num <= 0 || ost->st->time_base.den <= 0)
  3128. ost->st->time_base = av_add_q(ost->enc_ctx->time_base, (AVRational){0, 1});
  3129. // copy estimated duration as a hint to the muxer
  3130. if (ost->st->duration <= 0 && ist && ist->st->duration > 0)
  3131. ost->st->duration = av_rescale_q(ist->st->duration, ist->st->time_base, ost->st->time_base);
  3132. } else if (ost->stream_copy) {
  3133. ret = init_output_stream_streamcopy(ost);
  3134. if (ret < 0)
  3135. return ret;
  3136. }
  3137. // parse user provided disposition, and update stream values
  3138. if (ost->disposition) {
  3139. static const AVOption opts[] = {
  3140. { "disposition" , NULL, 0, AV_OPT_TYPE_FLAGS, { .i64 = 0 }, INT64_MIN, INT64_MAX, .unit = "flags" },
  3141. { "default" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DEFAULT }, .unit = "flags" },
  3142. { "dub" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DUB }, .unit = "flags" },
  3143. { "original" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_ORIGINAL }, .unit = "flags" },
  3144. { "comment" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_COMMENT }, .unit = "flags" },
  3145. { "lyrics" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_LYRICS }, .unit = "flags" },
  3146. { "karaoke" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_KARAOKE }, .unit = "flags" },
  3147. { "forced" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_FORCED }, .unit = "flags" },
  3148. { "hearing_impaired" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_HEARING_IMPAIRED }, .unit = "flags" },
  3149. { "visual_impaired" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_VISUAL_IMPAIRED }, .unit = "flags" },
  3150. { "clean_effects" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_CLEAN_EFFECTS }, .unit = "flags" },
  3151. { "attached_pic" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_ATTACHED_PIC }, .unit = "flags" },
  3152. { "captions" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_CAPTIONS }, .unit = "flags" },
  3153. { "descriptions" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DESCRIPTIONS }, .unit = "flags" },
  3154. { "dependent" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DEPENDENT }, .unit = "flags" },
  3155. { "metadata" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_METADATA }, .unit = "flags" },
  3156. { NULL },
  3157. };
  3158. static const AVClass class = {
  3159. .class_name = "",
  3160. .item_name = av_default_item_name,
  3161. .option = opts,
  3162. .version = LIBAVUTIL_VERSION_INT,
  3163. };
  3164. const AVClass *pclass = &class;
  3165. ret = av_opt_eval_flags(&pclass, &opts[0], ost->disposition, &ost->st->disposition);
  3166. if (ret < 0)
  3167. return ret;
  3168. }
  3169. /* initialize bitstream filters for the output stream
  3170. * needs to be done here, because the codec id for streamcopy is not
  3171. * known until now */
  3172. ret = init_output_bsfs(ost);
  3173. if (ret < 0)
  3174. return ret;
  3175. ost->initialized = 1;
  3176. ret = check_init_output_file(output_files[ost->file_index], ost->file_index);
  3177. if (ret < 0)
  3178. return ret;
  3179. return ret;
  3180. }
  3181. static void report_new_stream(int input_index, AVPacket *pkt)
  3182. {
  3183. InputFile *file = input_files[input_index];
  3184. AVStream *st = file->ctx->streams[pkt->stream_index];
  3185. if (pkt->stream_index < file->nb_streams_warn)
  3186. return;
  3187. av_log(file->ctx, AV_LOG_WARNING,
  3188. "New %s stream %d:%d at pos:%"PRId64" and DTS:%ss\n",
  3189. av_get_media_type_string(st->codecpar->codec_type),
  3190. input_index, pkt->stream_index,
  3191. pkt->pos, av_ts2timestr(pkt->dts, &st->time_base));
  3192. file->nb_streams_warn = pkt->stream_index + 1;
  3193. }
  3194. static int transcode_init(void)
  3195. {
  3196. int ret = 0, i, j, k;
  3197. AVFormatContext *oc;
  3198. OutputStream *ost;
  3199. InputStream *ist;
  3200. char error[1024] = {0};
  3201. for (i = 0; i < nb_filtergraphs; i++) {
  3202. FilterGraph *fg = filtergraphs[i];
  3203. for (j = 0; j < fg->nb_outputs; j++) {
  3204. OutputFilter *ofilter = fg->outputs[j];
  3205. if (!ofilter->ost || ofilter->ost->source_index >= 0)
  3206. continue;
  3207. if (fg->nb_inputs != 1)
  3208. continue;
  3209. for (k = nb_input_streams-1; k >= 0 ; k--)
  3210. if (fg->inputs[0]->ist == input_streams[k])
  3211. break;
  3212. ofilter->ost->source_index = k;
  3213. }
  3214. }
  3215. /* init framerate emulation */
  3216. for (i = 0; i < nb_input_files; i++) {
  3217. InputFile *ifile = input_files[i];
  3218. if (ifile->rate_emu)
  3219. for (j = 0; j < ifile->nb_streams; j++)
  3220. input_streams[j + ifile->ist_index]->start = av_gettime_relative();
  3221. }
  3222. /* init input streams */
  3223. for (i = 0; i < nb_input_streams; i++)
  3224. if ((ret = init_input_stream(i, error, sizeof(error))) < 0) {
  3225. for (i = 0; i < nb_output_streams; i++) {
  3226. ost = output_streams[i];
  3227. avcodec_close(ost->enc_ctx);
  3228. }
  3229. goto dump_format;
  3230. }
  3231. /*
  3232. * initialize stream copy and subtitle/data streams.
  3233. * Encoded AVFrame based streams will get initialized as follows:
  3234. * - when the first AVFrame is received in do_video_out
  3235. * - just before the first AVFrame is received in either transcode_step
  3236. * or reap_filters due to us requiring the filter chain buffer sink
  3237. * to be configured with the correct audio frame size, which is only
  3238. * known after the encoder is initialized.
  3239. */
  3240. for (i = 0; i < nb_output_streams; i++) {
  3241. if (!output_streams[i]->stream_copy &&
  3242. (output_streams[i]->enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO ||
  3243. output_streams[i]->enc_ctx->codec_type == AVMEDIA_TYPE_AUDIO))
  3244. continue;
  3245. ret = init_output_stream_wrapper(output_streams[i], NULL, 0);
  3246. if (ret < 0)
  3247. goto dump_format;
  3248. }
  3249. /* discard unused programs */
  3250. for (i = 0; i < nb_input_files; i++) {
  3251. InputFile *ifile = input_files[i];
  3252. for (j = 0; j < ifile->ctx->nb_programs; j++) {
  3253. AVProgram *p = ifile->ctx->programs[j];
  3254. int discard = AVDISCARD_ALL;
  3255. for (k = 0; k < p->nb_stream_indexes; k++)
  3256. if (!input_streams[ifile->ist_index + p->stream_index[k]]->discard) {
  3257. discard = AVDISCARD_DEFAULT;
  3258. break;
  3259. }
  3260. p->discard = discard;
  3261. }
  3262. }
  3263. /* write headers for files with no streams */
  3264. for (i = 0; i < nb_output_files; i++) {
  3265. oc = output_files[i]->ctx;
  3266. if (oc->oformat->flags & AVFMT_NOSTREAMS && oc->nb_streams == 0) {
  3267. ret = check_init_output_file(output_files[i], i);
  3268. if (ret < 0)
  3269. goto dump_format;
  3270. }
  3271. }
  3272. dump_format:
  3273. /* dump the stream mapping */
  3274. av_log(NULL, AV_LOG_INFO, "Stream mapping:\n");
  3275. for (i = 0; i < nb_input_streams; i++) {
  3276. ist = input_streams[i];
  3277. for (j = 0; j < ist->nb_filters; j++) {
  3278. if (!filtergraph_is_simple(ist->filters[j]->graph)) {
  3279. av_log(NULL, AV_LOG_INFO, " Stream #%d:%d (%s) -> %s",
  3280. ist->file_index, ist->st->index, ist->dec ? ist->dec->name : "?",
  3281. ist->filters[j]->name);
  3282. if (nb_filtergraphs > 1)
  3283. av_log(NULL, AV_LOG_INFO, " (graph %d)", ist->filters[j]->graph->index);
  3284. av_log(NULL, AV_LOG_INFO, "\n");
  3285. }
  3286. }
  3287. }
  3288. for (i = 0; i < nb_output_streams; i++) {
  3289. ost = output_streams[i];
  3290. if (ost->attachment_filename) {
  3291. /* an attached file */
  3292. av_log(NULL, AV_LOG_INFO, " File %s -> Stream #%d:%d\n",
  3293. ost->attachment_filename, ost->file_index, ost->index);
  3294. continue;
  3295. }
  3296. if (ost->filter && !filtergraph_is_simple(ost->filter->graph)) {
  3297. /* output from a complex graph */
  3298. av_log(NULL, AV_LOG_INFO, " %s", ost->filter->name);
  3299. if (nb_filtergraphs > 1)
  3300. av_log(NULL, AV_LOG_INFO, " (graph %d)", ost->filter->graph->index);
  3301. av_log(NULL, AV_LOG_INFO, " -> Stream #%d:%d (%s)\n", ost->file_index,
  3302. ost->index, ost->enc ? ost->enc->name : "?");
  3303. continue;
  3304. }
  3305. av_log(NULL, AV_LOG_INFO, " Stream #%d:%d -> #%d:%d",
  3306. input_streams[ost->source_index]->file_index,
  3307. input_streams[ost->source_index]->st->index,
  3308. ost->file_index,
  3309. ost->index);
  3310. if (ost->sync_ist != input_streams[ost->source_index])
  3311. av_log(NULL, AV_LOG_INFO, " [sync #%d:%d]",
  3312. ost->sync_ist->file_index,
  3313. ost->sync_ist->st->index);
  3314. if (ost->stream_copy)
  3315. av_log(NULL, AV_LOG_INFO, " (copy)");
  3316. else {
  3317. const AVCodec *in_codec = input_streams[ost->source_index]->dec;
  3318. const AVCodec *out_codec = ost->enc;
  3319. const char *decoder_name = "?";
  3320. const char *in_codec_name = "?";
  3321. const char *encoder_name = "?";
  3322. const char *out_codec_name = "?";
  3323. const AVCodecDescriptor *desc;
  3324. if (in_codec) {
  3325. decoder_name = in_codec->name;
  3326. desc = avcodec_descriptor_get(in_codec->id);
  3327. if (desc)
  3328. in_codec_name = desc->name;
  3329. if (!strcmp(decoder_name, in_codec_name))
  3330. decoder_name = "native";
  3331. }
  3332. if (out_codec) {
  3333. encoder_name = out_codec->name;
  3334. desc = avcodec_descriptor_get(out_codec->id);
  3335. if (desc)
  3336. out_codec_name = desc->name;
  3337. if (!strcmp(encoder_name, out_codec_name))
  3338. encoder_name = "native";
  3339. }
  3340. av_log(NULL, AV_LOG_INFO, " (%s (%s) -> %s (%s))",
  3341. in_codec_name, decoder_name,
  3342. out_codec_name, encoder_name);
  3343. }
  3344. av_log(NULL, AV_LOG_INFO, "\n");
  3345. }
  3346. if (ret) {
  3347. av_log(NULL, AV_LOG_ERROR, "%s\n", error);
  3348. return ret;
  3349. }
  3350. atomic_store(&transcode_init_done, 1);
  3351. return 0;
  3352. }
  3353. /* Return 1 if there remain streams where more output is wanted, 0 otherwise. */
  3354. static int need_output(void)
  3355. {
  3356. int i;
  3357. for (i = 0; i < nb_output_streams; i++) {
  3358. OutputStream *ost = output_streams[i];
  3359. OutputFile *of = output_files[ost->file_index];
  3360. AVFormatContext *os = output_files[ost->file_index]->ctx;
  3361. if (ost->finished ||
  3362. (os->pb && avio_tell(os->pb) >= of->limit_filesize))
  3363. continue;
  3364. if (ost->frame_number >= ost->max_frames) {
  3365. int j;
  3366. for (j = 0; j < of->ctx->nb_streams; j++)
  3367. close_output_stream(output_streams[of->ost_index + j]);
  3368. continue;
  3369. }
  3370. return 1;
  3371. }
  3372. return 0;
  3373. }
  3374. /**
  3375. * Select the output stream to process.
  3376. *
  3377. * @return selected output stream, or NULL if none available
  3378. */
  3379. static OutputStream *choose_output(void)
  3380. {
  3381. int i;
  3382. int64_t opts_min = INT64_MAX;
  3383. OutputStream *ost_min = NULL;
  3384. for (i = 0; i < nb_output_streams; i++) {
  3385. OutputStream *ost = output_streams[i];
  3386. int64_t opts = ost->st->cur_dts == AV_NOPTS_VALUE ? INT64_MIN :
  3387. av_rescale_q(ost->st->cur_dts, ost->st->time_base,
  3388. AV_TIME_BASE_Q);
  3389. if (ost->st->cur_dts == AV_NOPTS_VALUE)
  3390. av_log(NULL, AV_LOG_DEBUG,
  3391. "cur_dts is invalid st:%d (%d) [init:%d i_done:%d finish:%d] (this is harmless if it occurs once at the start per stream)\n",
  3392. ost->st->index, ost->st->id, ost->initialized, ost->inputs_done, ost->finished);
  3393. if (!ost->initialized && !ost->inputs_done)
  3394. return ost;
  3395. if (!ost->finished && opts < opts_min) {
  3396. opts_min = opts;
  3397. ost_min = ost->unavailable ? NULL : ost;
  3398. }
  3399. }
  3400. return ost_min;
  3401. }
  3402. static void set_tty_echo(int on)
  3403. {
  3404. #if HAVE_TERMIOS_H
  3405. struct termios tty;
  3406. if (tcgetattr(0, &tty) == 0) {
  3407. if (on) tty.c_lflag |= ECHO;
  3408. else tty.c_lflag &= ~ECHO;
  3409. tcsetattr(0, TCSANOW, &tty);
  3410. }
  3411. #endif
  3412. }
  3413. static int check_keyboard_interaction(int64_t cur_time)
  3414. {
  3415. int i, ret, key;
  3416. static int64_t last_time;
  3417. if (received_nb_signals)
  3418. return AVERROR_EXIT;
  3419. /* read_key() returns 0 on EOF */
  3420. if(cur_time - last_time >= 100000 && !run_as_daemon){
  3421. key = read_key();
  3422. last_time = cur_time;
  3423. }else
  3424. key = -1;
  3425. if (key == 'q')
  3426. return AVERROR_EXIT;
  3427. if (key == '+') av_log_set_level(av_log_get_level()+10);
  3428. if (key == '-') av_log_set_level(av_log_get_level()-10);
  3429. if (key == 's') qp_hist ^= 1;
  3430. if (key == 'h'){
  3431. if (do_hex_dump){
  3432. do_hex_dump = do_pkt_dump = 0;
  3433. } else if(do_pkt_dump){
  3434. do_hex_dump = 1;
  3435. } else
  3436. do_pkt_dump = 1;
  3437. av_log_set_level(AV_LOG_DEBUG);
  3438. }
  3439. if (key == 'c' || key == 'C'){
  3440. char buf[4096], target[64], command[256], arg[256] = {0};
  3441. double time;
  3442. int k, n = 0;
  3443. fprintf(stderr, "\nEnter command: <target>|all <time>|-1 <command>[ <argument>]\n");
  3444. i = 0;
  3445. set_tty_echo(1);
  3446. while ((k = read_key()) != '\n' && k != '\r' && i < sizeof(buf)-1)
  3447. if (k > 0)
  3448. buf[i++] = k;
  3449. buf[i] = 0;
  3450. set_tty_echo(0);
  3451. fprintf(stderr, "\n");
  3452. if (k > 0 &&
  3453. (n = sscanf(buf, "%63[^ ] %lf %255[^ ] %255[^\n]", target, &time, command, arg)) >= 3) {
  3454. av_log(NULL, AV_LOG_DEBUG, "Processing command target:%s time:%f command:%s arg:%s",
  3455. target, time, command, arg);
  3456. for (i = 0; i < nb_filtergraphs; i++) {
  3457. FilterGraph *fg = filtergraphs[i];
  3458. if (fg->graph) {
  3459. if (time < 0) {
  3460. ret = avfilter_graph_send_command(fg->graph, target, command, arg, buf, sizeof(buf),
  3461. key == 'c' ? AVFILTER_CMD_FLAG_ONE : 0);
  3462. fprintf(stderr, "Command reply for stream %d: ret:%d res:\n%s", i, ret, buf);
  3463. } else if (key == 'c') {
  3464. fprintf(stderr, "Queuing commands only on filters supporting the specific command is unsupported\n");
  3465. ret = AVERROR_PATCHWELCOME;
  3466. } else {
  3467. ret = avfilter_graph_queue_command(fg->graph, target, command, arg, 0, time);
  3468. if (ret < 0)
  3469. fprintf(stderr, "Queuing command failed with error %s\n", av_err2str(ret));
  3470. }
  3471. }
  3472. }
  3473. } else {
  3474. av_log(NULL, AV_LOG_ERROR,
  3475. "Parse error, at least 3 arguments were expected, "
  3476. "only %d given in string '%s'\n", n, buf);
  3477. }
  3478. }
  3479. if (key == 'd' || key == 'D'){
  3480. int debug=0;
  3481. if(key == 'D') {
  3482. debug = input_streams[0]->dec_ctx->debug << 1;
  3483. if(!debug) debug = 1;
  3484. while (debug & FF_DEBUG_DCT_COEFF) //unsupported, would just crash
  3485. debug += debug;
  3486. }else{
  3487. char buf[32];
  3488. int k = 0;
  3489. i = 0;
  3490. set_tty_echo(1);
  3491. while ((k = read_key()) != '\n' && k != '\r' && i < sizeof(buf)-1)
  3492. if (k > 0)
  3493. buf[i++] = k;
  3494. buf[i] = 0;
  3495. set_tty_echo(0);
  3496. fprintf(stderr, "\n");
  3497. if (k <= 0 || sscanf(buf, "%d", &debug)!=1)
  3498. fprintf(stderr,"error parsing debug value\n");
  3499. }
  3500. for(i=0;i<nb_input_streams;i++) {
  3501. input_streams[i]->dec_ctx->debug = debug;
  3502. }
  3503. for(i=0;i<nb_output_streams;i++) {
  3504. OutputStream *ost = output_streams[i];
  3505. ost->enc_ctx->debug = debug;
  3506. }
  3507. if(debug) av_log_set_level(AV_LOG_DEBUG);
  3508. fprintf(stderr,"debug=%d\n", debug);
  3509. }
  3510. if (key == '?'){
  3511. fprintf(stderr, "key function\n"
  3512. "? show this help\n"
  3513. "+ increase verbosity\n"
  3514. "- decrease verbosity\n"
  3515. "c Send command to first matching filter supporting it\n"
  3516. "C Send/Queue command to all matching filters\n"
  3517. "D cycle through available debug modes\n"
  3518. "h dump packets/hex press to cycle through the 3 states\n"
  3519. "q quit\n"
  3520. "s Show QP histogram\n"
  3521. );
  3522. }
  3523. return 0;
  3524. }
  3525. #if HAVE_THREADS
  3526. static void *input_thread(void *arg)
  3527. {
  3528. InputFile *f = arg;
  3529. AVPacket *pkt = f->pkt, *queue_pkt;
  3530. unsigned flags = f->non_blocking ? AV_THREAD_MESSAGE_NONBLOCK : 0;
  3531. int ret = 0;
  3532. while (1) {
  3533. ret = av_read_frame(f->ctx, pkt);
  3534. if (ret == AVERROR(EAGAIN)) {
  3535. av_usleep(10000);
  3536. continue;
  3537. }
  3538. if (ret < 0) {
  3539. av_thread_message_queue_set_err_recv(f->in_thread_queue, ret);
  3540. break;
  3541. }
  3542. queue_pkt = av_packet_alloc();
  3543. if (!queue_pkt) {
  3544. av_packet_unref(pkt);
  3545. av_thread_message_queue_set_err_recv(f->in_thread_queue, AVERROR(ENOMEM));
  3546. break;
  3547. }
  3548. av_packet_move_ref(queue_pkt, pkt);
  3549. ret = av_thread_message_queue_send(f->in_thread_queue, &queue_pkt, flags);
  3550. if (flags && ret == AVERROR(EAGAIN)) {
  3551. flags = 0;
  3552. ret = av_thread_message_queue_send(f->in_thread_queue, &queue_pkt, flags);
  3553. av_log(f->ctx, AV_LOG_WARNING,
  3554. "Thread message queue blocking; consider raising the "
  3555. "thread_queue_size option (current value: %d)\n",
  3556. f->thread_queue_size);
  3557. }
  3558. if (ret < 0) {
  3559. if (ret != AVERROR_EOF)
  3560. av_log(f->ctx, AV_LOG_ERROR,
  3561. "Unable to send packet to main thread: %s\n",
  3562. av_err2str(ret));
  3563. av_packet_free(&queue_pkt);
  3564. av_thread_message_queue_set_err_recv(f->in_thread_queue, ret);
  3565. break;
  3566. }
  3567. }
  3568. return NULL;
  3569. }
  3570. static void free_input_thread(int i)
  3571. {
  3572. InputFile *f = input_files[i];
  3573. AVPacket *pkt;
  3574. if (!f || !f->in_thread_queue)
  3575. return;
  3576. av_thread_message_queue_set_err_send(f->in_thread_queue, AVERROR_EOF);
  3577. while (av_thread_message_queue_recv(f->in_thread_queue, &pkt, 0) >= 0)
  3578. av_packet_free(&pkt);
  3579. pthread_join(f->thread, NULL);
  3580. f->joined = 1;
  3581. av_thread_message_queue_free(&f->in_thread_queue);
  3582. }
  3583. static void free_input_threads(void)
  3584. {
  3585. int i;
  3586. for (i = 0; i < nb_input_files; i++)
  3587. free_input_thread(i);
  3588. }
  3589. static int init_input_thread(int i)
  3590. {
  3591. int ret;
  3592. InputFile *f = input_files[i];
  3593. if (f->thread_queue_size < 0)
  3594. f->thread_queue_size = (nb_input_files > 1 ? 8 : 0);
  3595. if (!f->thread_queue_size)
  3596. return 0;
  3597. if (f->ctx->pb ? !f->ctx->pb->seekable :
  3598. strcmp(f->ctx->iformat->name, "lavfi"))
  3599. f->non_blocking = 1;
  3600. ret = av_thread_message_queue_alloc(&f->in_thread_queue,
  3601. f->thread_queue_size, sizeof(f->pkt));
  3602. if (ret < 0)
  3603. return ret;
  3604. if ((ret = pthread_create(&f->thread, NULL, input_thread, f))) {
  3605. av_log(NULL, AV_LOG_ERROR, "pthread_create failed: %s. Try to increase `ulimit -v` or decrease `ulimit -s`.\n", strerror(ret));
  3606. av_thread_message_queue_free(&f->in_thread_queue);
  3607. return AVERROR(ret);
  3608. }
  3609. return 0;
  3610. }
  3611. static int init_input_threads(void)
  3612. {
  3613. int i, ret;
  3614. for (i = 0; i < nb_input_files; i++) {
  3615. ret = init_input_thread(i);
  3616. if (ret < 0)
  3617. return ret;
  3618. }
  3619. return 0;
  3620. }
  3621. static int get_input_packet_mt(InputFile *f, AVPacket **pkt)
  3622. {
  3623. return av_thread_message_queue_recv(f->in_thread_queue, pkt,
  3624. f->non_blocking ?
  3625. AV_THREAD_MESSAGE_NONBLOCK : 0);
  3626. }
  3627. #endif
  3628. static int get_input_packet(InputFile *f, AVPacket **pkt)
  3629. {
  3630. if (f->rate_emu) {
  3631. int i;
  3632. for (i = 0; i < f->nb_streams; i++) {
  3633. InputStream *ist = input_streams[f->ist_index + i];
  3634. int64_t pts = av_rescale(ist->dts, 1000000, AV_TIME_BASE);
  3635. int64_t now = av_gettime_relative() - ist->start;
  3636. if (pts > now)
  3637. return AVERROR(EAGAIN);
  3638. }
  3639. }
  3640. #if HAVE_THREADS
  3641. if (f->thread_queue_size)
  3642. return get_input_packet_mt(f, pkt);
  3643. #endif
  3644. *pkt = f->pkt;
  3645. return av_read_frame(f->ctx, *pkt);
  3646. }
  3647. static int got_eagain(void)
  3648. {
  3649. int i;
  3650. for (i = 0; i < nb_output_streams; i++)
  3651. if (output_streams[i]->unavailable)
  3652. return 1;
  3653. return 0;
  3654. }
  3655. static void reset_eagain(void)
  3656. {
  3657. int i;
  3658. for (i = 0; i < nb_input_files; i++)
  3659. input_files[i]->eagain = 0;
  3660. for (i = 0; i < nb_output_streams; i++)
  3661. output_streams[i]->unavailable = 0;
  3662. }
  3663. // set duration to max(tmp, duration) in a proper time base and return duration's time_base
  3664. static AVRational duration_max(int64_t tmp, int64_t *duration, AVRational tmp_time_base,
  3665. AVRational time_base)
  3666. {
  3667. int ret;
  3668. if (!*duration) {
  3669. *duration = tmp;
  3670. return tmp_time_base;
  3671. }
  3672. ret = av_compare_ts(*duration, time_base, tmp, tmp_time_base);
  3673. if (ret < 0) {
  3674. *duration = tmp;
  3675. return tmp_time_base;
  3676. }
  3677. return time_base;
  3678. }
  3679. static int seek_to_start(InputFile *ifile, AVFormatContext *is)
  3680. {
  3681. InputStream *ist;
  3682. AVCodecContext *avctx;
  3683. int i, ret, has_audio = 0;
  3684. int64_t duration = 0;
  3685. ret = avformat_seek_file(is, -1, INT64_MIN, is->start_time, is->start_time, 0);
  3686. if (ret < 0)
  3687. return ret;
  3688. for (i = 0; i < ifile->nb_streams; i++) {
  3689. ist = input_streams[ifile->ist_index + i];
  3690. avctx = ist->dec_ctx;
  3691. /* duration is the length of the last frame in a stream
  3692. * when audio stream is present we don't care about
  3693. * last video frame length because it's not defined exactly */
  3694. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && ist->nb_samples)
  3695. has_audio = 1;
  3696. }
  3697. for (i = 0; i < ifile->nb_streams; i++) {
  3698. ist = input_streams[ifile->ist_index + i];
  3699. avctx = ist->dec_ctx;
  3700. if (has_audio) {
  3701. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && ist->nb_samples) {
  3702. AVRational sample_rate = {1, avctx->sample_rate};
  3703. duration = av_rescale_q(ist->nb_samples, sample_rate, ist->st->time_base);
  3704. } else {
  3705. continue;
  3706. }
  3707. } else {
  3708. if (ist->framerate.num) {
  3709. duration = av_rescale_q(1, av_inv_q(ist->framerate), ist->st->time_base);
  3710. } else if (ist->st->avg_frame_rate.num) {
  3711. duration = av_rescale_q(1, av_inv_q(ist->st->avg_frame_rate), ist->st->time_base);
  3712. } else {
  3713. duration = 1;
  3714. }
  3715. }
  3716. if (!ifile->duration)
  3717. ifile->time_base = ist->st->time_base;
  3718. /* the total duration of the stream, max_pts - min_pts is
  3719. * the duration of the stream without the last frame */
  3720. if (ist->max_pts > ist->min_pts && ist->max_pts - (uint64_t)ist->min_pts < INT64_MAX - duration)
  3721. duration += ist->max_pts - ist->min_pts;
  3722. ifile->time_base = duration_max(duration, &ifile->duration, ist->st->time_base,
  3723. ifile->time_base);
  3724. }
  3725. if (ifile->loop > 0)
  3726. ifile->loop--;
  3727. return ret;
  3728. }
  3729. /*
  3730. * Return
  3731. * - 0 -- one packet was read and processed
  3732. * - AVERROR(EAGAIN) -- no packets were available for selected file,
  3733. * this function should be called again
  3734. * - AVERROR_EOF -- this function should not be called again
  3735. */
  3736. static int process_input(int file_index)
  3737. {
  3738. InputFile *ifile = input_files[file_index];
  3739. AVFormatContext *is;
  3740. InputStream *ist;
  3741. AVPacket *pkt;
  3742. int ret, thread_ret, i, j;
  3743. int64_t duration;
  3744. int64_t pkt_dts;
  3745. int disable_discontinuity_correction = copy_ts;
  3746. is = ifile->ctx;
  3747. ret = get_input_packet(ifile, &pkt);
  3748. if (ret == AVERROR(EAGAIN)) {
  3749. ifile->eagain = 1;
  3750. return ret;
  3751. }
  3752. if (ret < 0 && ifile->loop) {
  3753. AVCodecContext *avctx;
  3754. for (i = 0; i < ifile->nb_streams; i++) {
  3755. ist = input_streams[ifile->ist_index + i];
  3756. avctx = ist->dec_ctx;
  3757. if (ist->decoding_needed) {
  3758. ret = process_input_packet(ist, NULL, 1);
  3759. if (ret>0)
  3760. return 0;
  3761. avcodec_flush_buffers(avctx);
  3762. }
  3763. }
  3764. #if HAVE_THREADS
  3765. free_input_thread(file_index);
  3766. #endif
  3767. ret = seek_to_start(ifile, is);
  3768. #if HAVE_THREADS
  3769. thread_ret = init_input_thread(file_index);
  3770. if (thread_ret < 0)
  3771. return thread_ret;
  3772. #endif
  3773. if (ret < 0)
  3774. av_log(NULL, AV_LOG_WARNING, "Seek to start failed.\n");
  3775. else
  3776. ret = get_input_packet(ifile, &pkt);
  3777. if (ret == AVERROR(EAGAIN)) {
  3778. ifile->eagain = 1;
  3779. return ret;
  3780. }
  3781. }
  3782. if (ret < 0) {
  3783. if (ret != AVERROR_EOF) {
  3784. print_error(is->url, ret);
  3785. if (exit_on_error)
  3786. exit_program(1);
  3787. }
  3788. for (i = 0; i < ifile->nb_streams; i++) {
  3789. ist = input_streams[ifile->ist_index + i];
  3790. if (ist->decoding_needed) {
  3791. ret = process_input_packet(ist, NULL, 0);
  3792. if (ret>0)
  3793. return 0;
  3794. }
  3795. /* mark all outputs that don't go through lavfi as finished */
  3796. for (j = 0; j < nb_output_streams; j++) {
  3797. OutputStream *ost = output_streams[j];
  3798. if (ost->source_index == ifile->ist_index + i &&
  3799. (ost->stream_copy || ost->enc->type == AVMEDIA_TYPE_SUBTITLE))
  3800. finish_output_stream(ost);
  3801. }
  3802. }
  3803. ifile->eof_reached = 1;
  3804. return AVERROR(EAGAIN);
  3805. }
  3806. reset_eagain();
  3807. if (do_pkt_dump) {
  3808. av_pkt_dump_log2(NULL, AV_LOG_INFO, pkt, do_hex_dump,
  3809. is->streams[pkt->stream_index]);
  3810. }
  3811. /* the following test is needed in case new streams appear
  3812. dynamically in stream : we ignore them */
  3813. if (pkt->stream_index >= ifile->nb_streams) {
  3814. report_new_stream(file_index, pkt);
  3815. goto discard_packet;
  3816. }
  3817. ist = input_streams[ifile->ist_index + pkt->stream_index];
  3818. ist->data_size += pkt->size;
  3819. ist->nb_packets++;
  3820. if (ist->discard)
  3821. goto discard_packet;
  3822. if (pkt->flags & AV_PKT_FLAG_CORRUPT) {
  3823. av_log(NULL, exit_on_error ? AV_LOG_FATAL : AV_LOG_WARNING,
  3824. "%s: corrupt input packet in stream %d\n", is->url, pkt->stream_index);
  3825. if (exit_on_error)
  3826. exit_program(1);
  3827. }
  3828. if (debug_ts) {
  3829. av_log(NULL, AV_LOG_INFO, "demuxer -> ist_index:%d type:%s "
  3830. "next_dts:%s next_dts_time:%s next_pts:%s next_pts_time:%s pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s off:%s off_time:%s\n",
  3831. ifile->ist_index + pkt->stream_index, av_get_media_type_string(ist->dec_ctx->codec_type),
  3832. av_ts2str(ist->next_dts), av_ts2timestr(ist->next_dts, &AV_TIME_BASE_Q),
  3833. av_ts2str(ist->next_pts), av_ts2timestr(ist->next_pts, &AV_TIME_BASE_Q),
  3834. av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ist->st->time_base),
  3835. av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ist->st->time_base),
  3836. av_ts2str(input_files[ist->file_index]->ts_offset),
  3837. av_ts2timestr(input_files[ist->file_index]->ts_offset, &AV_TIME_BASE_Q));
  3838. }
  3839. if(!ist->wrap_correction_done && is->start_time != AV_NOPTS_VALUE && ist->st->pts_wrap_bits < 64){
  3840. int64_t stime, stime2;
  3841. // Correcting starttime based on the enabled streams
  3842. // FIXME this ideally should be done before the first use of starttime but we do not know which are the enabled streams at that point.
  3843. // so we instead do it here as part of discontinuity handling
  3844. if ( ist->next_dts == AV_NOPTS_VALUE
  3845. && ifile->ts_offset == -is->start_time
  3846. && (is->iformat->flags & AVFMT_TS_DISCONT)) {
  3847. int64_t new_start_time = INT64_MAX;
  3848. for (i=0; i<is->nb_streams; i++) {
  3849. AVStream *st = is->streams[i];
  3850. if(st->discard == AVDISCARD_ALL || st->start_time == AV_NOPTS_VALUE)
  3851. continue;
  3852. new_start_time = FFMIN(new_start_time, av_rescale_q(st->start_time, st->time_base, AV_TIME_BASE_Q));
  3853. }
  3854. if (new_start_time > is->start_time) {
  3855. av_log(is, AV_LOG_VERBOSE, "Correcting start time by %"PRId64"\n", new_start_time - is->start_time);
  3856. ifile->ts_offset = -new_start_time;
  3857. }
  3858. }
  3859. stime = av_rescale_q(is->start_time, AV_TIME_BASE_Q, ist->st->time_base);
  3860. stime2= stime + (1ULL<<ist->st->pts_wrap_bits);
  3861. ist->wrap_correction_done = 1;
  3862. if(stime2 > stime && pkt->dts != AV_NOPTS_VALUE && pkt->dts > stime + (1LL<<(ist->st->pts_wrap_bits-1))) {
  3863. pkt->dts -= 1ULL<<ist->st->pts_wrap_bits;
  3864. ist->wrap_correction_done = 0;
  3865. }
  3866. if(stime2 > stime && pkt->pts != AV_NOPTS_VALUE && pkt->pts > stime + (1LL<<(ist->st->pts_wrap_bits-1))) {
  3867. pkt->pts -= 1ULL<<ist->st->pts_wrap_bits;
  3868. ist->wrap_correction_done = 0;
  3869. }
  3870. }
  3871. /* add the stream-global side data to the first packet */
  3872. if (ist->nb_packets == 1) {
  3873. for (i = 0; i < ist->st->nb_side_data; i++) {
  3874. AVPacketSideData *src_sd = &ist->st->side_data[i];
  3875. uint8_t *dst_data;
  3876. if (src_sd->type == AV_PKT_DATA_DISPLAYMATRIX)
  3877. continue;
  3878. if (av_packet_get_side_data(pkt, src_sd->type, NULL))
  3879. continue;
  3880. dst_data = av_packet_new_side_data(pkt, src_sd->type, src_sd->size);
  3881. if (!dst_data)
  3882. exit_program(1);
  3883. memcpy(dst_data, src_sd->data, src_sd->size);
  3884. }
  3885. }
  3886. if (pkt->dts != AV_NOPTS_VALUE)
  3887. pkt->dts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
  3888. if (pkt->pts != AV_NOPTS_VALUE)
  3889. pkt->pts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
  3890. if (pkt->pts != AV_NOPTS_VALUE)
  3891. pkt->pts *= ist->ts_scale;
  3892. if (pkt->dts != AV_NOPTS_VALUE)
  3893. pkt->dts *= ist->ts_scale;
  3894. pkt_dts = av_rescale_q_rnd(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
  3895. if ((ist->dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO ||
  3896. ist->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) &&
  3897. pkt_dts != AV_NOPTS_VALUE && ist->next_dts == AV_NOPTS_VALUE && !copy_ts
  3898. && (is->iformat->flags & AVFMT_TS_DISCONT) && ifile->last_ts != AV_NOPTS_VALUE) {
  3899. int64_t delta = pkt_dts - ifile->last_ts;
  3900. if (delta < -1LL*dts_delta_threshold*AV_TIME_BASE ||
  3901. delta > 1LL*dts_delta_threshold*AV_TIME_BASE){
  3902. ifile->ts_offset -= delta;
  3903. av_log(NULL, AV_LOG_DEBUG,
  3904. "Inter stream timestamp discontinuity %"PRId64", new offset= %"PRId64"\n",
  3905. delta, ifile->ts_offset);
  3906. pkt->dts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
  3907. if (pkt->pts != AV_NOPTS_VALUE)
  3908. pkt->pts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
  3909. }
  3910. }
  3911. duration = av_rescale_q(ifile->duration, ifile->time_base, ist->st->time_base);
  3912. if (pkt->pts != AV_NOPTS_VALUE) {
  3913. pkt->pts += duration;
  3914. ist->max_pts = FFMAX(pkt->pts, ist->max_pts);
  3915. ist->min_pts = FFMIN(pkt->pts, ist->min_pts);
  3916. }
  3917. if (pkt->dts != AV_NOPTS_VALUE)
  3918. pkt->dts += duration;
  3919. pkt_dts = av_rescale_q_rnd(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
  3920. if (copy_ts && pkt_dts != AV_NOPTS_VALUE && ist->next_dts != AV_NOPTS_VALUE &&
  3921. (is->iformat->flags & AVFMT_TS_DISCONT) && ist->st->pts_wrap_bits < 60) {
  3922. int64_t wrap_dts = av_rescale_q_rnd(pkt->dts + (1LL<<ist->st->pts_wrap_bits),
  3923. ist->st->time_base, AV_TIME_BASE_Q,
  3924. AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
  3925. if (FFABS(wrap_dts - ist->next_dts) < FFABS(pkt_dts - ist->next_dts)/10)
  3926. disable_discontinuity_correction = 0;
  3927. }
  3928. if ((ist->dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO ||
  3929. ist->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) &&
  3930. pkt_dts != AV_NOPTS_VALUE && ist->next_dts != AV_NOPTS_VALUE &&
  3931. !disable_discontinuity_correction) {
  3932. int64_t delta = pkt_dts - ist->next_dts;
  3933. if (is->iformat->flags & AVFMT_TS_DISCONT) {
  3934. if (delta < -1LL*dts_delta_threshold*AV_TIME_BASE ||
  3935. delta > 1LL*dts_delta_threshold*AV_TIME_BASE ||
  3936. pkt_dts + AV_TIME_BASE/10 < FFMAX(ist->pts, ist->dts)) {
  3937. ifile->ts_offset -= delta;
  3938. av_log(NULL, AV_LOG_DEBUG,
  3939. "timestamp discontinuity for stream #%d:%d "
  3940. "(id=%d, type=%s): %"PRId64", new offset= %"PRId64"\n",
  3941. ist->file_index, ist->st->index, ist->st->id,
  3942. av_get_media_type_string(ist->dec_ctx->codec_type),
  3943. delta, ifile->ts_offset);
  3944. pkt->dts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
  3945. if (pkt->pts != AV_NOPTS_VALUE)
  3946. pkt->pts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
  3947. }
  3948. } else {
  3949. if ( delta < -1LL*dts_error_threshold*AV_TIME_BASE ||
  3950. delta > 1LL*dts_error_threshold*AV_TIME_BASE) {
  3951. av_log(NULL, AV_LOG_WARNING, "DTS %"PRId64", next:%"PRId64" st:%d invalid dropping\n", pkt->dts, ist->next_dts, pkt->stream_index);
  3952. pkt->dts = AV_NOPTS_VALUE;
  3953. }
  3954. if (pkt->pts != AV_NOPTS_VALUE){
  3955. int64_t pkt_pts = av_rescale_q(pkt->pts, ist->st->time_base, AV_TIME_BASE_Q);
  3956. delta = pkt_pts - ist->next_dts;
  3957. if ( delta < -1LL*dts_error_threshold*AV_TIME_BASE ||
  3958. delta > 1LL*dts_error_threshold*AV_TIME_BASE) {
  3959. av_log(NULL, AV_LOG_WARNING, "PTS %"PRId64", next:%"PRId64" invalid dropping st:%d\n", pkt->pts, ist->next_dts, pkt->stream_index);
  3960. pkt->pts = AV_NOPTS_VALUE;
  3961. }
  3962. }
  3963. }
  3964. }
  3965. if (pkt->dts != AV_NOPTS_VALUE)
  3966. ifile->last_ts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);
  3967. if (debug_ts) {
  3968. av_log(NULL, AV_LOG_INFO, "demuxer+ffmpeg -> ist_index:%d type:%s pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s off:%s off_time:%s\n",
  3969. ifile->ist_index + pkt->stream_index, av_get_media_type_string(ist->dec_ctx->codec_type),
  3970. av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ist->st->time_base),
  3971. av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ist->st->time_base),
  3972. av_ts2str(input_files[ist->file_index]->ts_offset),
  3973. av_ts2timestr(input_files[ist->file_index]->ts_offset, &AV_TIME_BASE_Q));
  3974. }
  3975. sub2video_heartbeat(ist, pkt->pts);
  3976. process_input_packet(ist, pkt, 0);
  3977. discard_packet:
  3978. #if HAVE_THREADS
  3979. if (ifile->thread_queue_size)
  3980. av_packet_free(&pkt);
  3981. else
  3982. #endif
  3983. av_packet_unref(pkt);
  3984. return 0;
  3985. }
  3986. /**
  3987. * Perform a step of transcoding for the specified filter graph.
  3988. *
  3989. * @param[in] graph filter graph to consider
  3990. * @param[out] best_ist input stream where a frame would allow to continue
  3991. * @return 0 for success, <0 for error
  3992. */
  3993. static int transcode_from_filter(FilterGraph *graph, InputStream **best_ist)
  3994. {
  3995. int i, ret;
  3996. int nb_requests, nb_requests_max = 0;
  3997. InputFilter *ifilter;
  3998. InputStream *ist;
  3999. *best_ist = NULL;
  4000. ret = avfilter_graph_request_oldest(graph->graph);
  4001. if (ret >= 0)
  4002. return reap_filters(0);
  4003. if (ret == AVERROR_EOF) {
  4004. ret = reap_filters(1);
  4005. for (i = 0; i < graph->nb_outputs; i++)
  4006. close_output_stream(graph->outputs[i]->ost);
  4007. return ret;
  4008. }
  4009. if (ret != AVERROR(EAGAIN))
  4010. return ret;
  4011. for (i = 0; i < graph->nb_inputs; i++) {
  4012. ifilter = graph->inputs[i];
  4013. ist = ifilter->ist;
  4014. if (input_files[ist->file_index]->eagain ||
  4015. input_files[ist->file_index]->eof_reached)
  4016. continue;
  4017. nb_requests = av_buffersrc_get_nb_failed_requests(ifilter->filter);
  4018. if (nb_requests > nb_requests_max) {
  4019. nb_requests_max = nb_requests;
  4020. *best_ist = ist;
  4021. }
  4022. }
  4023. if (!*best_ist)
  4024. for (i = 0; i < graph->nb_outputs; i++)
  4025. graph->outputs[i]->ost->unavailable = 1;
  4026. return 0;
  4027. }
  4028. /**
  4029. * Run a single step of transcoding.
  4030. *
  4031. * @return 0 for success, <0 for error
  4032. */
  4033. static int transcode_step(void)
  4034. {
  4035. OutputStream *ost;
  4036. InputStream *ist = NULL;
  4037. int ret;
  4038. ost = choose_output();
  4039. if (!ost) {
  4040. if (got_eagain()) {
  4041. reset_eagain();
  4042. av_usleep(10000);
  4043. return 0;
  4044. }
  4045. av_log(NULL, AV_LOG_VERBOSE, "No more inputs to read from, finishing.\n");
  4046. return AVERROR_EOF;
  4047. }
  4048. if (ost->filter && !ost->filter->graph->graph) {
  4049. if (ifilter_has_all_input_formats(ost->filter->graph)) {
  4050. ret = configure_filtergraph(ost->filter->graph);
  4051. if (ret < 0) {
  4052. av_log(NULL, AV_LOG_ERROR, "Error reinitializing filters!\n");
  4053. return ret;
  4054. }
  4055. }
  4056. }
  4057. if (ost->filter && ost->filter->graph->graph) {
  4058. /*
  4059. * Similar case to the early audio initialization in reap_filters.
  4060. * Audio is special in ffmpeg.c currently as we depend on lavfi's
  4061. * audio frame buffering/creation to get the output audio frame size
  4062. * in samples correct. The audio frame size for the filter chain is
  4063. * configured during the output stream initialization.
  4064. *
  4065. * Apparently avfilter_graph_request_oldest (called in
  4066. * transcode_from_filter just down the line) peeks. Peeking already
  4067. * puts one frame "ready to be given out", which means that any
  4068. * update in filter buffer sink configuration afterwards will not
  4069. * help us. And yes, even if it would be utilized,
  4070. * av_buffersink_get_samples is affected, as it internally utilizes
  4071. * the same early exit for peeked frames.
  4072. *
  4073. * In other words, if avfilter_graph_request_oldest would not make
  4074. * further filter chain configuration or usage of
  4075. * av_buffersink_get_samples useless (by just causing the return
  4076. * of the peeked AVFrame as-is), we could get rid of this additional
  4077. * early encoder initialization.
  4078. */
  4079. if (av_buffersink_get_type(ost->filter->filter) == AVMEDIA_TYPE_AUDIO)
  4080. init_output_stream_wrapper(ost, NULL, 1);
  4081. if ((ret = transcode_from_filter(ost->filter->graph, &ist)) < 0)
  4082. return ret;
  4083. if (!ist)
  4084. return 0;
  4085. } else if (ost->filter) {
  4086. int i;
  4087. for (i = 0; i < ost->filter->graph->nb_inputs; i++) {
  4088. InputFilter *ifilter = ost->filter->graph->inputs[i];
  4089. if (!ifilter->ist->got_output && !input_files[ifilter->ist->file_index]->eof_reached) {
  4090. ist = ifilter->ist;
  4091. break;
  4092. }
  4093. }
  4094. if (!ist) {
  4095. ost->inputs_done = 1;
  4096. return 0;
  4097. }
  4098. } else {
  4099. av_assert0(ost->source_index >= 0);
  4100. ist = input_streams[ost->source_index];
  4101. }
  4102. ret = process_input(ist->file_index);
  4103. if (ret == AVERROR(EAGAIN)) {
  4104. if (input_files[ist->file_index]->eagain)
  4105. ost->unavailable = 1;
  4106. return 0;
  4107. }
  4108. if (ret < 0)
  4109. return ret == AVERROR_EOF ? 0 : ret;
  4110. return reap_filters(0);
  4111. }
  4112. /*
  4113. * The following code is the main loop of the file converter
  4114. */
  4115. static int transcode(void)
  4116. {
  4117. int ret, i;
  4118. AVFormatContext *os;
  4119. OutputStream *ost;
  4120. InputStream *ist;
  4121. int64_t timer_start;
  4122. int64_t total_packets_written = 0;
  4123. ret = transcode_init();
  4124. if (ret < 0)
  4125. goto fail;
  4126. if (stdin_interaction) {
  4127. av_log(NULL, AV_LOG_INFO, "Press [q] to stop, [?] for help\n");
  4128. }
  4129. timer_start = av_gettime_relative();
  4130. #if HAVE_THREADS
  4131. if ((ret = init_input_threads()) < 0)
  4132. goto fail;
  4133. #endif
  4134. while (!received_sigterm) {
  4135. int64_t cur_time= av_gettime_relative();
  4136. /* if 'q' pressed, exits */
  4137. if (stdin_interaction)
  4138. if (check_keyboard_interaction(cur_time) < 0)
  4139. break;
  4140. /* check if there's any stream where output is still needed */
  4141. if (!need_output()) {
  4142. av_log(NULL, AV_LOG_VERBOSE, "No more output streams to write to, finishing.\n");
  4143. break;
  4144. }
  4145. ret = transcode_step();
  4146. if (ret < 0 && ret != AVERROR_EOF) {
  4147. av_log(NULL, AV_LOG_ERROR, "Error while filtering: %s\n", av_err2str(ret));
  4148. break;
  4149. }
  4150. /* dump report by using the output first video and audio streams */
  4151. print_report(0, timer_start, cur_time);
  4152. }
  4153. #if HAVE_THREADS
  4154. free_input_threads();
  4155. #endif
  4156. /* at the end of stream, we must flush the decoder buffers */
  4157. for (i = 0; i < nb_input_streams; i++) {
  4158. ist = input_streams[i];
  4159. if (!input_files[ist->file_index]->eof_reached) {
  4160. process_input_packet(ist, NULL, 0);
  4161. }
  4162. }
  4163. flush_encoders();
  4164. term_exit();
  4165. /* write the trailer if needed and close file */
  4166. for (i = 0; i < nb_output_files; i++) {
  4167. os = output_files[i]->ctx;
  4168. if (!output_files[i]->header_written) {
  4169. av_log(NULL, AV_LOG_ERROR,
  4170. "Nothing was written into output file %d (%s), because "
  4171. "at least one of its streams received no packets.\n",
  4172. i, os->url);
  4173. continue;
  4174. }
  4175. if ((ret = av_write_trailer(os)) < 0) {
  4176. av_log(NULL, AV_LOG_ERROR, "Error writing trailer of %s: %s\n", os->url, av_err2str(ret));
  4177. if (exit_on_error)
  4178. exit_program(1);
  4179. }
  4180. }
  4181. /* dump report by using the first video and audio streams */
  4182. print_report(1, timer_start, av_gettime_relative());
  4183. /* close each encoder */
  4184. for (i = 0; i < nb_output_streams; i++) {
  4185. ost = output_streams[i];
  4186. if (ost->encoding_needed) {
  4187. av_freep(&ost->enc_ctx->stats_in);
  4188. }
  4189. total_packets_written += ost->packets_written;
  4190. if (!ost->packets_written && (abort_on_flags & ABORT_ON_FLAG_EMPTY_OUTPUT_STREAM)) {
  4191. av_log(NULL, AV_LOG_FATAL, "Empty output on stream %d.\n", i);
  4192. exit_program(1);
  4193. }
  4194. }
  4195. if (!total_packets_written && (abort_on_flags & ABORT_ON_FLAG_EMPTY_OUTPUT)) {
  4196. av_log(NULL, AV_LOG_FATAL, "Empty output\n");
  4197. exit_program(1);
  4198. }
  4199. /* close each decoder */
  4200. for (i = 0; i < nb_input_streams; i++) {
  4201. ist = input_streams[i];
  4202. if (ist->decoding_needed) {
  4203. avcodec_close(ist->dec_ctx);
  4204. if (ist->hwaccel_uninit)
  4205. ist->hwaccel_uninit(ist->dec_ctx);
  4206. }
  4207. }
  4208. hw_device_free_all();
  4209. /* finished ! */
  4210. ret = 0;
  4211. fail:
  4212. #if HAVE_THREADS
  4213. free_input_threads();
  4214. #endif
  4215. if (output_streams) {
  4216. for (i = 0; i < nb_output_streams; i++) {
  4217. ost = output_streams[i];
  4218. if (ost) {
  4219. if (ost->logfile) {
  4220. if (fclose(ost->logfile))
  4221. av_log(NULL, AV_LOG_ERROR,
  4222. "Error closing logfile, loss of information possible: %s\n",
  4223. av_err2str(AVERROR(errno)));
  4224. ost->logfile = NULL;
  4225. }
  4226. av_freep(&ost->forced_kf_pts);
  4227. av_freep(&ost->apad);
  4228. av_freep(&ost->disposition);
  4229. av_dict_free(&ost->encoder_opts);
  4230. av_dict_free(&ost->sws_dict);
  4231. av_dict_free(&ost->swr_opts);
  4232. av_dict_free(&ost->resample_opts);
  4233. }
  4234. }
  4235. }
  4236. return ret;
  4237. }
  4238. static BenchmarkTimeStamps get_benchmark_time_stamps(void)
  4239. {
  4240. BenchmarkTimeStamps time_stamps = { av_gettime_relative() };
  4241. #if HAVE_GETRUSAGE
  4242. struct rusage rusage;
  4243. getrusage(RUSAGE_SELF, &rusage);
  4244. time_stamps.user_usec =
  4245. (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
  4246. time_stamps.sys_usec =
  4247. (rusage.ru_stime.tv_sec * 1000000LL) + rusage.ru_stime.tv_usec;
  4248. #elif HAVE_GETPROCESSTIMES
  4249. HANDLE proc;
  4250. FILETIME c, e, k, u;
  4251. proc = GetCurrentProcess();
  4252. GetProcessTimes(proc, &c, &e, &k, &u);
  4253. time_stamps.user_usec =
  4254. ((int64_t)u.dwHighDateTime << 32 | u.dwLowDateTime) / 10;
  4255. time_stamps.sys_usec =
  4256. ((int64_t)k.dwHighDateTime << 32 | k.dwLowDateTime) / 10;
  4257. #else
  4258. time_stamps.user_usec = time_stamps.sys_usec = 0;
  4259. #endif
  4260. return time_stamps;
  4261. }
  4262. static int64_t getmaxrss(void)
  4263. {
  4264. #if HAVE_GETRUSAGE && HAVE_STRUCT_RUSAGE_RU_MAXRSS
  4265. struct rusage rusage;
  4266. getrusage(RUSAGE_SELF, &rusage);
  4267. return (int64_t)rusage.ru_maxrss * 1024;
  4268. #elif HAVE_GETPROCESSMEMORYINFO
  4269. HANDLE proc;
  4270. PROCESS_MEMORY_COUNTERS memcounters;
  4271. proc = GetCurrentProcess();
  4272. memcounters.cb = sizeof(memcounters);
  4273. GetProcessMemoryInfo(proc, &memcounters, sizeof(memcounters));
  4274. return memcounters.PeakPagefileUsage;
  4275. #else
  4276. return 0;
  4277. #endif
  4278. }
  4279. static void log_callback_null(void *ptr, int level, const char *fmt, va_list vl)
  4280. {
  4281. }
  4282. int main(int argc, char **argv)
  4283. {
  4284. int i, ret;
  4285. BenchmarkTimeStamps ti;
  4286. init_dynload();
  4287. register_exit(ffmpeg_cleanup);
  4288. setvbuf(stderr,NULL,_IONBF,0); /* win32 runtime needs this */
  4289. av_log_set_flags(AV_LOG_SKIP_REPEATED);
  4290. parse_loglevel(argc, argv, options);
  4291. if(argc>1 && !strcmp(argv[1], "-d")){
  4292. run_as_daemon=1;
  4293. av_log_set_callback(log_callback_null);
  4294. argc--;
  4295. argv++;
  4296. }
  4297. #if CONFIG_AVDEVICE
  4298. avdevice_register_all();
  4299. #endif
  4300. avformat_network_init();
  4301. show_banner(argc, argv, options);
  4302. /* parse options and open all input/output files */
  4303. ret = ffmpeg_parse_options(argc, argv);
  4304. if (ret < 0)
  4305. exit_program(1);
  4306. if (nb_output_files <= 0 && nb_input_files == 0) {
  4307. show_usage();
  4308. av_log(NULL, AV_LOG_WARNING, "Use -h to get full help or, even better, run 'man %s'\n", program_name);
  4309. exit_program(1);
  4310. }
  4311. /* file converter / grab */
  4312. if (nb_output_files <= 0) {
  4313. av_log(NULL, AV_LOG_FATAL, "At least one output file must be specified\n");
  4314. exit_program(1);
  4315. }
  4316. for (i = 0; i < nb_output_files; i++) {
  4317. if (strcmp(output_files[i]->ctx->oformat->name, "rtp"))
  4318. want_sdp = 0;
  4319. }
  4320. current_time = ti = get_benchmark_time_stamps();
  4321. if (transcode() < 0)
  4322. exit_program(1);
  4323. if (do_benchmark) {
  4324. int64_t utime, stime, rtime;
  4325. current_time = get_benchmark_time_stamps();
  4326. utime = current_time.user_usec - ti.user_usec;
  4327. stime = current_time.sys_usec - ti.sys_usec;
  4328. rtime = current_time.real_usec - ti.real_usec;
  4329. av_log(NULL, AV_LOG_INFO,
  4330. "bench: utime=%0.3fs stime=%0.3fs rtime=%0.3fs\n",
  4331. utime / 1000000.0, stime / 1000000.0, rtime / 1000000.0);
  4332. }
  4333. av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" frames successfully decoded, %"PRIu64" decoding errors\n",
  4334. decode_error_stat[0], decode_error_stat[1]);
  4335. if ((decode_error_stat[0] + decode_error_stat[1]) * max_error_rate < decode_error_stat[1])
  4336. exit_program(69);
  4337. exit_program(received_nb_signals ? 255 : main_return_code);
  4338. return main_return_code;
  4339. }