ffmpeg_demux.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <float.h>
  19. #include <stdint.h>
  20. #include "ffmpeg.h"
  21. #include "libavutil/avassert.h"
  22. #include "libavutil/avstring.h"
  23. #include "libavutil/display.h"
  24. #include "libavutil/error.h"
  25. #include "libavutil/intreadwrite.h"
  26. #include "libavutil/opt.h"
  27. #include "libavutil/parseutils.h"
  28. #include "libavutil/pixdesc.h"
  29. #include "libavutil/time.h"
  30. #include "libavutil/timestamp.h"
  31. #include "libavutil/thread.h"
  32. #include "libavutil/threadmessage.h"
  33. #include "libavcodec/packet.h"
  34. #include "libavformat/avformat.h"
  35. static const char *const opt_name_discard[] = {"discard", NULL};
  36. static const char *const opt_name_reinit_filters[] = {"reinit_filter", NULL};
  37. static const char *const opt_name_fix_sub_duration[] = {"fix_sub_duration", NULL};
  38. static const char *const opt_name_canvas_sizes[] = {"canvas_size", NULL};
  39. static const char *const opt_name_guess_layout_max[] = {"guess_layout_max", NULL};
  40. static const char *const opt_name_ts_scale[] = {"itsscale", NULL};
  41. static const char *const opt_name_hwaccels[] = {"hwaccel", NULL};
  42. static const char *const opt_name_hwaccel_devices[] = {"hwaccel_device", NULL};
  43. static const char *const opt_name_hwaccel_output_formats[] = {"hwaccel_output_format", NULL};
  44. static const char *const opt_name_autorotate[] = {"autorotate", NULL};
  45. static const char *const opt_name_display_rotations[] = {"display_rotation", NULL};
  46. static const char *const opt_name_display_hflips[] = {"display_hflip", NULL};
  47. static const char *const opt_name_display_vflips[] = {"display_vflip", NULL};
  48. typedef struct Demuxer {
  49. InputFile f;
  50. /* number of times input stream should be looped */
  51. int loop;
  52. /* actual duration of the longest stream in a file at the moment when
  53. * looping happens */
  54. int64_t duration;
  55. /* time base of the duration */
  56. AVRational time_base;
  57. /* number of streams that the user was warned of */
  58. int nb_streams_warn;
  59. AVThreadMessageQueue *in_thread_queue;
  60. int thread_queue_size;
  61. pthread_t thread;
  62. int non_blocking;
  63. } Demuxer;
  64. typedef struct DemuxMsg {
  65. AVPacket *pkt;
  66. int looping;
  67. // repeat_pict from the demuxer-internal parser
  68. int repeat_pict;
  69. } DemuxMsg;
  70. static Demuxer *demuxer_from_ifile(InputFile *f)
  71. {
  72. return (Demuxer*)f;
  73. }
  74. static void report_new_stream(Demuxer *d, const AVPacket *pkt)
  75. {
  76. AVStream *st = d->f.ctx->streams[pkt->stream_index];
  77. if (pkt->stream_index < d->nb_streams_warn)
  78. return;
  79. av_log(NULL, AV_LOG_WARNING,
  80. "New %s stream %d:%d at pos:%"PRId64" and DTS:%ss\n",
  81. av_get_media_type_string(st->codecpar->codec_type),
  82. d->f.index, pkt->stream_index,
  83. pkt->pos, av_ts2timestr(pkt->dts, &st->time_base));
  84. d->nb_streams_warn = pkt->stream_index + 1;
  85. }
  86. static void ifile_duration_update(Demuxer *d, InputStream *ist,
  87. int64_t last_duration)
  88. {
  89. /* the total duration of the stream, max_pts - min_pts is
  90. * the duration of the stream without the last frame */
  91. if (ist->max_pts > ist->min_pts &&
  92. ist->max_pts - (uint64_t)ist->min_pts < INT64_MAX - last_duration)
  93. last_duration += ist->max_pts - ist->min_pts;
  94. if (!d->duration ||
  95. av_compare_ts(d->duration, d->time_base,
  96. last_duration, ist->st->time_base) < 0) {
  97. d->duration = last_duration;
  98. d->time_base = ist->st->time_base;
  99. }
  100. }
  101. static int seek_to_start(Demuxer *d)
  102. {
  103. InputFile *ifile = &d->f;
  104. AVFormatContext *is = ifile->ctx;
  105. InputStream *ist;
  106. int ret;
  107. ret = avformat_seek_file(is, -1, INT64_MIN, is->start_time, is->start_time, 0);
  108. if (ret < 0)
  109. return ret;
  110. if (ifile->audio_duration_queue_size) {
  111. /* duration is the length of the last frame in a stream
  112. * when audio stream is present we don't care about
  113. * last video frame length because it's not defined exactly */
  114. int got_durations = 0;
  115. while (got_durations < ifile->audio_duration_queue_size) {
  116. LastFrameDuration dur;
  117. ret = av_thread_message_queue_recv(ifile->audio_duration_queue, &dur, 0);
  118. if (ret < 0)
  119. return ret;
  120. got_durations++;
  121. ist = ifile->streams[dur.stream_idx];
  122. ifile_duration_update(d, ist, dur.duration);
  123. }
  124. } else {
  125. for (int i = 0; i < ifile->nb_streams; i++) {
  126. int64_t duration = 0;
  127. ist = ifile->streams[i];
  128. if (ist->framerate.num) {
  129. duration = av_rescale_q(1, av_inv_q(ist->framerate), ist->st->time_base);
  130. } else if (ist->st->avg_frame_rate.num) {
  131. duration = av_rescale_q(1, av_inv_q(ist->st->avg_frame_rate), ist->st->time_base);
  132. } else {
  133. duration = 1;
  134. }
  135. ifile_duration_update(d, ist, duration);
  136. }
  137. }
  138. if (d->loop > 0)
  139. d->loop--;
  140. return ret;
  141. }
  142. static void ts_fixup(Demuxer *d, AVPacket *pkt, int *repeat_pict)
  143. {
  144. InputFile *ifile = &d->f;
  145. InputStream *ist = ifile->streams[pkt->stream_index];
  146. const int64_t start_time = ifile->start_time_effective;
  147. int64_t duration;
  148. if (debug_ts) {
  149. av_log(NULL, AV_LOG_INFO, "demuxer -> ist_index:%d:%d type:%s "
  150. "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s\n",
  151. ifile->index, pkt->stream_index,
  152. av_get_media_type_string(ist->st->codecpar->codec_type),
  153. av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ist->st->time_base),
  154. av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ist->st->time_base),
  155. av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &ist->st->time_base));
  156. }
  157. if (!ist->wrap_correction_done && start_time != AV_NOPTS_VALUE &&
  158. ist->st->pts_wrap_bits < 64) {
  159. int64_t stime, stime2;
  160. stime = av_rescale_q(start_time, AV_TIME_BASE_Q, ist->st->time_base);
  161. stime2= stime + (1ULL<<ist->st->pts_wrap_bits);
  162. ist->wrap_correction_done = 1;
  163. if(stime2 > stime && pkt->dts != AV_NOPTS_VALUE && pkt->dts > stime + (1LL<<(ist->st->pts_wrap_bits-1))) {
  164. pkt->dts -= 1ULL<<ist->st->pts_wrap_bits;
  165. ist->wrap_correction_done = 0;
  166. }
  167. if(stime2 > stime && pkt->pts != AV_NOPTS_VALUE && pkt->pts > stime + (1LL<<(ist->st->pts_wrap_bits-1))) {
  168. pkt->pts -= 1ULL<<ist->st->pts_wrap_bits;
  169. ist->wrap_correction_done = 0;
  170. }
  171. }
  172. if (pkt->dts != AV_NOPTS_VALUE)
  173. pkt->dts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
  174. if (pkt->pts != AV_NOPTS_VALUE)
  175. pkt->pts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
  176. if (pkt->pts != AV_NOPTS_VALUE)
  177. pkt->pts *= ist->ts_scale;
  178. if (pkt->dts != AV_NOPTS_VALUE)
  179. pkt->dts *= ist->ts_scale;
  180. duration = av_rescale_q(d->duration, d->time_base, ist->st->time_base);
  181. if (pkt->pts != AV_NOPTS_VALUE) {
  182. pkt->pts += duration;
  183. ist->max_pts = FFMAX(pkt->pts, ist->max_pts);
  184. ist->min_pts = FFMIN(pkt->pts, ist->min_pts);
  185. }
  186. if (pkt->dts != AV_NOPTS_VALUE)
  187. pkt->dts += duration;
  188. *repeat_pict = -1;
  189. if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
  190. av_stream_get_parser(ist->st))
  191. *repeat_pict = av_stream_get_parser(ist->st)->repeat_pict;
  192. }
  193. static void thread_set_name(InputFile *f)
  194. {
  195. char name[16];
  196. snprintf(name, sizeof(name), "dmx%d:%s", f->index, f->ctx->iformat->name);
  197. ff_thread_setname(name);
  198. }
  199. static void *input_thread(void *arg)
  200. {
  201. Demuxer *d = arg;
  202. InputFile *f = &d->f;
  203. AVPacket *pkt;
  204. unsigned flags = d->non_blocking ? AV_THREAD_MESSAGE_NONBLOCK : 0;
  205. int ret = 0;
  206. pkt = av_packet_alloc();
  207. if (!pkt) {
  208. ret = AVERROR(ENOMEM);
  209. goto finish;
  210. }
  211. thread_set_name(f);
  212. while (1) {
  213. DemuxMsg msg = { NULL };
  214. ret = av_read_frame(f->ctx, pkt);
  215. if (ret == AVERROR(EAGAIN)) {
  216. av_usleep(10000);
  217. continue;
  218. }
  219. if (ret < 0) {
  220. if (d->loop) {
  221. /* signal looping to the consumer thread */
  222. msg.looping = 1;
  223. ret = av_thread_message_queue_send(d->in_thread_queue, &msg, 0);
  224. if (ret >= 0)
  225. ret = seek_to_start(d);
  226. if (ret >= 0)
  227. continue;
  228. /* fallthrough to the error path */
  229. }
  230. if (ret == AVERROR_EOF)
  231. av_log(NULL, AV_LOG_VERBOSE, "EOF in input file %d\n", f->index);
  232. else
  233. av_log(NULL, AV_LOG_ERROR, "Error demuxing input file %d: %s\n",
  234. f->index, av_err2str(ret));
  235. break;
  236. }
  237. if (do_pkt_dump) {
  238. av_pkt_dump_log2(NULL, AV_LOG_INFO, pkt, do_hex_dump,
  239. f->ctx->streams[pkt->stream_index]);
  240. }
  241. /* the following test is needed in case new streams appear
  242. dynamically in stream : we ignore them */
  243. if (pkt->stream_index >= f->nb_streams) {
  244. report_new_stream(d, pkt);
  245. av_packet_unref(pkt);
  246. continue;
  247. }
  248. if (pkt->flags & AV_PKT_FLAG_CORRUPT) {
  249. av_log(NULL, exit_on_error ? AV_LOG_FATAL : AV_LOG_WARNING,
  250. "%s: corrupt input packet in stream %d\n",
  251. f->ctx->url, pkt->stream_index);
  252. if (exit_on_error) {
  253. av_packet_unref(pkt);
  254. ret = AVERROR_INVALIDDATA;
  255. break;
  256. }
  257. }
  258. ts_fixup(d, pkt, &msg.repeat_pict);
  259. msg.pkt = av_packet_alloc();
  260. if (!msg.pkt) {
  261. av_packet_unref(pkt);
  262. ret = AVERROR(ENOMEM);
  263. break;
  264. }
  265. av_packet_move_ref(msg.pkt, pkt);
  266. ret = av_thread_message_queue_send(d->in_thread_queue, &msg, flags);
  267. if (flags && ret == AVERROR(EAGAIN)) {
  268. flags = 0;
  269. ret = av_thread_message_queue_send(d->in_thread_queue, &msg, flags);
  270. av_log(f->ctx, AV_LOG_WARNING,
  271. "Thread message queue blocking; consider raising the "
  272. "thread_queue_size option (current value: %d)\n",
  273. d->thread_queue_size);
  274. }
  275. if (ret < 0) {
  276. if (ret != AVERROR_EOF)
  277. av_log(f->ctx, AV_LOG_ERROR,
  278. "Unable to send packet to main thread: %s\n",
  279. av_err2str(ret));
  280. av_packet_free(&msg.pkt);
  281. break;
  282. }
  283. }
  284. finish:
  285. av_assert0(ret < 0);
  286. av_thread_message_queue_set_err_recv(d->in_thread_queue, ret);
  287. av_packet_free(&pkt);
  288. av_log(NULL, AV_LOG_VERBOSE, "Terminating demuxer thread %d\n", f->index);
  289. return NULL;
  290. }
  291. static void thread_stop(Demuxer *d)
  292. {
  293. InputFile *f = &d->f;
  294. DemuxMsg msg;
  295. if (!d->in_thread_queue)
  296. return;
  297. av_thread_message_queue_set_err_send(d->in_thread_queue, AVERROR_EOF);
  298. while (av_thread_message_queue_recv(d->in_thread_queue, &msg, 0) >= 0)
  299. av_packet_free(&msg.pkt);
  300. pthread_join(d->thread, NULL);
  301. av_thread_message_queue_free(&d->in_thread_queue);
  302. av_thread_message_queue_free(&f->audio_duration_queue);
  303. }
  304. static int thread_start(Demuxer *d)
  305. {
  306. int ret;
  307. InputFile *f = &d->f;
  308. if (d->thread_queue_size <= 0)
  309. d->thread_queue_size = (nb_input_files > 1 ? 8 : 1);
  310. if (nb_input_files > 1 &&
  311. (f->ctx->pb ? !f->ctx->pb->seekable :
  312. strcmp(f->ctx->iformat->name, "lavfi")))
  313. d->non_blocking = 1;
  314. ret = av_thread_message_queue_alloc(&d->in_thread_queue,
  315. d->thread_queue_size, sizeof(DemuxMsg));
  316. if (ret < 0)
  317. return ret;
  318. if (d->loop) {
  319. int nb_audio_dec = 0;
  320. for (int i = 0; i < f->nb_streams; i++) {
  321. InputStream *ist = f->streams[i];
  322. nb_audio_dec += !!(ist->decoding_needed &&
  323. ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO);
  324. }
  325. if (nb_audio_dec) {
  326. ret = av_thread_message_queue_alloc(&f->audio_duration_queue,
  327. nb_audio_dec, sizeof(LastFrameDuration));
  328. if (ret < 0)
  329. goto fail;
  330. f->audio_duration_queue_size = nb_audio_dec;
  331. }
  332. }
  333. if ((ret = pthread_create(&d->thread, NULL, input_thread, d))) {
  334. av_log(NULL, AV_LOG_ERROR, "pthread_create failed: %s. Try to increase `ulimit -v` or decrease `ulimit -s`.\n", strerror(ret));
  335. ret = AVERROR(ret);
  336. goto fail;
  337. }
  338. return 0;
  339. fail:
  340. av_thread_message_queue_free(&d->in_thread_queue);
  341. return ret;
  342. }
  343. int ifile_get_packet(InputFile *f, AVPacket **pkt)
  344. {
  345. Demuxer *d = demuxer_from_ifile(f);
  346. InputStream *ist;
  347. DemuxMsg msg;
  348. int ret;
  349. if (!d->in_thread_queue) {
  350. ret = thread_start(d);
  351. if (ret < 0)
  352. return ret;
  353. }
  354. if (f->readrate || f->rate_emu) {
  355. int i;
  356. int64_t file_start = copy_ts * (
  357. (f->start_time_effective != AV_NOPTS_VALUE ? f->start_time_effective * !start_at_zero : 0) +
  358. (f->start_time != AV_NOPTS_VALUE ? f->start_time : 0)
  359. );
  360. float scale = f->rate_emu ? 1.0 : f->readrate;
  361. for (i = 0; i < f->nb_streams; i++) {
  362. InputStream *ist = f->streams[i];
  363. int64_t stream_ts_offset, pts, now;
  364. if (!ist->nb_packets || (ist->decoding_needed && !ist->got_output)) continue;
  365. stream_ts_offset = FFMAX(ist->first_dts != AV_NOPTS_VALUE ? ist->first_dts : 0, file_start);
  366. pts = av_rescale(ist->dts, 1000000, AV_TIME_BASE);
  367. now = (av_gettime_relative() - ist->start) * scale + stream_ts_offset;
  368. if (pts > now)
  369. return AVERROR(EAGAIN);
  370. }
  371. }
  372. ret = av_thread_message_queue_recv(d->in_thread_queue, &msg,
  373. d->non_blocking ?
  374. AV_THREAD_MESSAGE_NONBLOCK : 0);
  375. if (ret < 0)
  376. return ret;
  377. if (msg.looping)
  378. return 1;
  379. ist = f->streams[msg.pkt->stream_index];
  380. ist->last_pkt_repeat_pict = msg.repeat_pict;
  381. *pkt = msg.pkt;
  382. return 0;
  383. }
  384. static void ist_free(InputStream **pist)
  385. {
  386. InputStream *ist = *pist;
  387. if (!ist)
  388. return;
  389. av_frame_free(&ist->decoded_frame);
  390. av_packet_free(&ist->pkt);
  391. av_dict_free(&ist->decoder_opts);
  392. avsubtitle_free(&ist->prev_sub.subtitle);
  393. av_frame_free(&ist->sub2video.frame);
  394. av_freep(&ist->filters);
  395. av_freep(&ist->hwaccel_device);
  396. av_freep(&ist->dts_buffer);
  397. avcodec_free_context(&ist->dec_ctx);
  398. avcodec_parameters_free(&ist->par);
  399. av_freep(pist);
  400. }
  401. void ifile_close(InputFile **pf)
  402. {
  403. InputFile *f = *pf;
  404. Demuxer *d = demuxer_from_ifile(f);
  405. if (!f)
  406. return;
  407. thread_stop(d);
  408. for (int i = 0; i < f->nb_streams; i++)
  409. ist_free(&f->streams[i]);
  410. av_freep(&f->streams);
  411. avformat_close_input(&f->ctx);
  412. av_freep(pf);
  413. }
  414. static const AVCodec *choose_decoder(const OptionsContext *o, AVFormatContext *s, AVStream *st,
  415. enum HWAccelID hwaccel_id, enum AVHWDeviceType hwaccel_device_type)
  416. {
  417. char *codec_name = NULL;
  418. MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, st);
  419. if (codec_name) {
  420. const AVCodec *codec = find_codec_or_die(NULL, codec_name, st->codecpar->codec_type, 0);
  421. st->codecpar->codec_id = codec->id;
  422. if (recast_media && st->codecpar->codec_type != codec->type)
  423. st->codecpar->codec_type = codec->type;
  424. return codec;
  425. } else {
  426. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
  427. hwaccel_id == HWACCEL_GENERIC &&
  428. hwaccel_device_type != AV_HWDEVICE_TYPE_NONE) {
  429. const AVCodec *c;
  430. void *i = NULL;
  431. while ((c = av_codec_iterate(&i))) {
  432. const AVCodecHWConfig *config;
  433. if (c->id != st->codecpar->codec_id ||
  434. !av_codec_is_decoder(c))
  435. continue;
  436. for (int j = 0; config = avcodec_get_hw_config(c, j); j++) {
  437. if (config->device_type == hwaccel_device_type) {
  438. av_log(NULL, AV_LOG_VERBOSE, "Selecting decoder '%s' because of requested hwaccel method %s\n",
  439. c->name, av_hwdevice_get_type_name(hwaccel_device_type));
  440. return c;
  441. }
  442. }
  443. }
  444. }
  445. return avcodec_find_decoder(st->codecpar->codec_id);
  446. }
  447. }
  448. static int guess_input_channel_layout(InputStream *ist)
  449. {
  450. AVCodecContext *dec = ist->dec_ctx;
  451. if (dec->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC) {
  452. char layout_name[256];
  453. if (dec->ch_layout.nb_channels > ist->guess_layout_max)
  454. return 0;
  455. av_channel_layout_default(&dec->ch_layout, dec->ch_layout.nb_channels);
  456. if (dec->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC)
  457. return 0;
  458. av_channel_layout_describe(&dec->ch_layout, layout_name, sizeof(layout_name));
  459. av_log(NULL, AV_LOG_WARNING, "Guessed Channel Layout for Input Stream "
  460. "#%d.%d : %s\n", ist->file_index, ist->st->index, layout_name);
  461. }
  462. return 1;
  463. }
  464. static void add_display_matrix_to_stream(const OptionsContext *o,
  465. AVFormatContext *ctx, AVStream *st)
  466. {
  467. double rotation = DBL_MAX;
  468. int hflip = -1, vflip = -1;
  469. int hflip_set = 0, vflip_set = 0, rotation_set = 0;
  470. int32_t *buf;
  471. MATCH_PER_STREAM_OPT(display_rotations, dbl, rotation, ctx, st);
  472. MATCH_PER_STREAM_OPT(display_hflips, i, hflip, ctx, st);
  473. MATCH_PER_STREAM_OPT(display_vflips, i, vflip, ctx, st);
  474. rotation_set = rotation != DBL_MAX;
  475. hflip_set = hflip != -1;
  476. vflip_set = vflip != -1;
  477. if (!rotation_set && !hflip_set && !vflip_set)
  478. return;
  479. buf = (int32_t *)av_stream_new_side_data(st, AV_PKT_DATA_DISPLAYMATRIX, sizeof(int32_t) * 9);
  480. if (!buf) {
  481. av_log(NULL, AV_LOG_FATAL, "Failed to generate a display matrix!\n");
  482. exit_program(1);
  483. }
  484. av_display_rotation_set(buf,
  485. rotation_set ? -(rotation) : -0.0f);
  486. av_display_matrix_flip(buf,
  487. hflip_set ? hflip : 0,
  488. vflip_set ? vflip : 0);
  489. }
  490. /* Add all the streams from the given input file to the demuxer */
  491. static void add_input_streams(const OptionsContext *o, Demuxer *d)
  492. {
  493. InputFile *f = &d->f;
  494. AVFormatContext *ic = f->ctx;
  495. int i, ret;
  496. for (i = 0; i < ic->nb_streams; i++) {
  497. AVStream *st = ic->streams[i];
  498. AVCodecParameters *par = st->codecpar;
  499. InputStream *ist;
  500. char *framerate = NULL, *hwaccel_device = NULL;
  501. const char *hwaccel = NULL;
  502. char *hwaccel_output_format = NULL;
  503. char *codec_tag = NULL;
  504. char *next;
  505. char *discard_str = NULL;
  506. const AVClass *cc = avcodec_get_class();
  507. const AVOption *discard_opt = av_opt_find(&cc, "skip_frame", NULL,
  508. 0, AV_OPT_SEARCH_FAKE_OBJ);
  509. ist = ALLOC_ARRAY_ELEM(f->streams, f->nb_streams);
  510. ist->st = st;
  511. ist->file_index = f->index;
  512. ist->discard = 1;
  513. st->discard = AVDISCARD_ALL;
  514. ist->nb_samples = 0;
  515. ist->first_dts = AV_NOPTS_VALUE;
  516. ist->min_pts = INT64_MAX;
  517. ist->max_pts = INT64_MIN;
  518. ist->ts_scale = 1.0;
  519. MATCH_PER_STREAM_OPT(ts_scale, dbl, ist->ts_scale, ic, st);
  520. ist->autorotate = 1;
  521. MATCH_PER_STREAM_OPT(autorotate, i, ist->autorotate, ic, st);
  522. MATCH_PER_STREAM_OPT(codec_tags, str, codec_tag, ic, st);
  523. if (codec_tag) {
  524. uint32_t tag = strtol(codec_tag, &next, 0);
  525. if (*next) {
  526. uint8_t buf[4] = { 0 };
  527. memcpy(buf, codec_tag, FFMIN(sizeof(buf), strlen(codec_tag)));
  528. tag = AV_RL32(buf);
  529. }
  530. st->codecpar->codec_tag = tag;
  531. }
  532. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  533. add_display_matrix_to_stream(o, ic, st);
  534. MATCH_PER_STREAM_OPT(hwaccels, str, hwaccel, ic, st);
  535. MATCH_PER_STREAM_OPT(hwaccel_output_formats, str,
  536. hwaccel_output_format, ic, st);
  537. if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel, "cuvid")) {
  538. av_log(NULL, AV_LOG_WARNING,
  539. "WARNING: defaulting hwaccel_output_format to cuda for compatibility "
  540. "with old commandlines. This behaviour is DEPRECATED and will be removed "
  541. "in the future. Please explicitly set \"-hwaccel_output_format cuda\".\n");
  542. ist->hwaccel_output_format = AV_PIX_FMT_CUDA;
  543. } else if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel, "qsv")) {
  544. av_log(NULL, AV_LOG_WARNING,
  545. "WARNING: defaulting hwaccel_output_format to qsv for compatibility "
  546. "with old commandlines. This behaviour is DEPRECATED and will be removed "
  547. "in the future. Please explicitly set \"-hwaccel_output_format qsv\".\n");
  548. ist->hwaccel_output_format = AV_PIX_FMT_QSV;
  549. } else if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel, "mediacodec")) {
  550. // There is no real AVHWFrameContext implementation. Set
  551. // hwaccel_output_format to avoid av_hwframe_transfer_data error.
  552. ist->hwaccel_output_format = AV_PIX_FMT_MEDIACODEC;
  553. } else if (hwaccel_output_format) {
  554. ist->hwaccel_output_format = av_get_pix_fmt(hwaccel_output_format);
  555. if (ist->hwaccel_output_format == AV_PIX_FMT_NONE) {
  556. av_log(NULL, AV_LOG_FATAL, "Unrecognised hwaccel output "
  557. "format: %s", hwaccel_output_format);
  558. }
  559. } else {
  560. ist->hwaccel_output_format = AV_PIX_FMT_NONE;
  561. }
  562. if (hwaccel) {
  563. // The NVDEC hwaccels use a CUDA device, so remap the name here.
  564. if (!strcmp(hwaccel, "nvdec") || !strcmp(hwaccel, "cuvid"))
  565. hwaccel = "cuda";
  566. if (!strcmp(hwaccel, "none"))
  567. ist->hwaccel_id = HWACCEL_NONE;
  568. else if (!strcmp(hwaccel, "auto"))
  569. ist->hwaccel_id = HWACCEL_AUTO;
  570. else {
  571. enum AVHWDeviceType type = av_hwdevice_find_type_by_name(hwaccel);
  572. if (type != AV_HWDEVICE_TYPE_NONE) {
  573. ist->hwaccel_id = HWACCEL_GENERIC;
  574. ist->hwaccel_device_type = type;
  575. }
  576. if (!ist->hwaccel_id) {
  577. av_log(NULL, AV_LOG_FATAL, "Unrecognized hwaccel: %s.\n",
  578. hwaccel);
  579. av_log(NULL, AV_LOG_FATAL, "Supported hwaccels: ");
  580. type = AV_HWDEVICE_TYPE_NONE;
  581. while ((type = av_hwdevice_iterate_types(type)) !=
  582. AV_HWDEVICE_TYPE_NONE)
  583. av_log(NULL, AV_LOG_FATAL, "%s ",
  584. av_hwdevice_get_type_name(type));
  585. av_log(NULL, AV_LOG_FATAL, "\n");
  586. exit_program(1);
  587. }
  588. }
  589. }
  590. MATCH_PER_STREAM_OPT(hwaccel_devices, str, hwaccel_device, ic, st);
  591. if (hwaccel_device) {
  592. ist->hwaccel_device = av_strdup(hwaccel_device);
  593. if (!ist->hwaccel_device)
  594. report_and_exit(AVERROR(ENOMEM));
  595. }
  596. ist->hwaccel_pix_fmt = AV_PIX_FMT_NONE;
  597. }
  598. ist->dec = choose_decoder(o, ic, st, ist->hwaccel_id, ist->hwaccel_device_type);
  599. ist->decoder_opts = filter_codec_opts(o->g->codec_opts, ist->st->codecpar->codec_id, ic, st, ist->dec);
  600. ist->reinit_filters = -1;
  601. MATCH_PER_STREAM_OPT(reinit_filters, i, ist->reinit_filters, ic, st);
  602. MATCH_PER_STREAM_OPT(discard, str, discard_str, ic, st);
  603. ist->user_set_discard = AVDISCARD_NONE;
  604. if ((o->video_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) ||
  605. (o->audio_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) ||
  606. (o->subtitle_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) ||
  607. (o->data_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_DATA))
  608. ist->user_set_discard = AVDISCARD_ALL;
  609. if (discard_str && av_opt_eval_int(&cc, discard_opt, discard_str, &ist->user_set_discard) < 0) {
  610. av_log(NULL, AV_LOG_ERROR, "Error parsing discard %s.\n",
  611. discard_str);
  612. exit_program(1);
  613. }
  614. ist->filter_in_rescale_delta_last = AV_NOPTS_VALUE;
  615. ist->prev_pkt_pts = AV_NOPTS_VALUE;
  616. ist->dec_ctx = avcodec_alloc_context3(ist->dec);
  617. if (!ist->dec_ctx)
  618. report_and_exit(AVERROR(ENOMEM));
  619. ret = avcodec_parameters_to_context(ist->dec_ctx, par);
  620. if (ret < 0) {
  621. av_log(NULL, AV_LOG_ERROR, "Error initializing the decoder context.\n");
  622. exit_program(1);
  623. }
  624. ist->decoded_frame = av_frame_alloc();
  625. if (!ist->decoded_frame)
  626. report_and_exit(AVERROR(ENOMEM));
  627. ist->pkt = av_packet_alloc();
  628. if (!ist->pkt)
  629. report_and_exit(AVERROR(ENOMEM));
  630. if (o->bitexact)
  631. ist->dec_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
  632. switch (par->codec_type) {
  633. case AVMEDIA_TYPE_VIDEO:
  634. // avformat_find_stream_info() doesn't set this for us anymore.
  635. ist->dec_ctx->framerate = st->avg_frame_rate;
  636. MATCH_PER_STREAM_OPT(frame_rates, str, framerate, ic, st);
  637. if (framerate && av_parse_video_rate(&ist->framerate,
  638. framerate) < 0) {
  639. av_log(NULL, AV_LOG_ERROR, "Error parsing framerate %s.\n",
  640. framerate);
  641. exit_program(1);
  642. }
  643. ist->top_field_first = -1;
  644. MATCH_PER_STREAM_OPT(top_field_first, i, ist->top_field_first, ic, st);
  645. ist->framerate_guessed = av_guess_frame_rate(ic, st, NULL);
  646. break;
  647. case AVMEDIA_TYPE_AUDIO:
  648. ist->guess_layout_max = INT_MAX;
  649. MATCH_PER_STREAM_OPT(guess_layout_max, i, ist->guess_layout_max, ic, st);
  650. guess_input_channel_layout(ist);
  651. break;
  652. case AVMEDIA_TYPE_DATA:
  653. case AVMEDIA_TYPE_SUBTITLE: {
  654. char *canvas_size = NULL;
  655. MATCH_PER_STREAM_OPT(fix_sub_duration, i, ist->fix_sub_duration, ic, st);
  656. MATCH_PER_STREAM_OPT(canvas_sizes, str, canvas_size, ic, st);
  657. if (canvas_size &&
  658. av_parse_video_size(&ist->dec_ctx->width, &ist->dec_ctx->height, canvas_size) < 0) {
  659. av_log(NULL, AV_LOG_FATAL, "Invalid canvas size: %s.\n", canvas_size);
  660. exit_program(1);
  661. }
  662. break;
  663. }
  664. case AVMEDIA_TYPE_ATTACHMENT:
  665. case AVMEDIA_TYPE_UNKNOWN:
  666. break;
  667. default:
  668. abort();
  669. }
  670. ist->par = avcodec_parameters_alloc();
  671. if (!ist->par)
  672. report_and_exit(AVERROR(ENOMEM));
  673. ret = avcodec_parameters_from_context(ist->par, ist->dec_ctx);
  674. if (ret < 0) {
  675. av_log(NULL, AV_LOG_ERROR, "Error initializing the decoder context.\n");
  676. exit_program(1);
  677. }
  678. }
  679. }
  680. static void dump_attachment(AVStream *st, const char *filename)
  681. {
  682. int ret;
  683. AVIOContext *out = NULL;
  684. const AVDictionaryEntry *e;
  685. if (!st->codecpar->extradata_size) {
  686. av_log(NULL, AV_LOG_WARNING, "No extradata to dump in stream #%d:%d.\n",
  687. nb_input_files - 1, st->index);
  688. return;
  689. }
  690. if (!*filename && (e = av_dict_get(st->metadata, "filename", NULL, 0)))
  691. filename = e->value;
  692. if (!*filename) {
  693. av_log(NULL, AV_LOG_FATAL, "No filename specified and no 'filename' tag"
  694. "in stream #%d:%d.\n", nb_input_files - 1, st->index);
  695. exit_program(1);
  696. }
  697. assert_file_overwrite(filename);
  698. if ((ret = avio_open2(&out, filename, AVIO_FLAG_WRITE, &int_cb, NULL)) < 0) {
  699. av_log(NULL, AV_LOG_FATAL, "Could not open file %s for writing.\n",
  700. filename);
  701. exit_program(1);
  702. }
  703. avio_write(out, st->codecpar->extradata, st->codecpar->extradata_size);
  704. avio_flush(out);
  705. avio_close(out);
  706. }
  707. int ifile_open(const OptionsContext *o, const char *filename)
  708. {
  709. Demuxer *d;
  710. InputFile *f;
  711. AVFormatContext *ic;
  712. const AVInputFormat *file_iformat = NULL;
  713. int err, i, ret;
  714. int64_t timestamp;
  715. AVDictionary *unused_opts = NULL;
  716. const AVDictionaryEntry *e = NULL;
  717. char * video_codec_name = NULL;
  718. char * audio_codec_name = NULL;
  719. char *subtitle_codec_name = NULL;
  720. char * data_codec_name = NULL;
  721. int scan_all_pmts_set = 0;
  722. int64_t start_time = o->start_time;
  723. int64_t start_time_eof = o->start_time_eof;
  724. int64_t stop_time = o->stop_time;
  725. int64_t recording_time = o->recording_time;
  726. if (stop_time != INT64_MAX && recording_time != INT64_MAX) {
  727. stop_time = INT64_MAX;
  728. av_log(NULL, AV_LOG_WARNING, "-t and -to cannot be used together; using -t.\n");
  729. }
  730. if (stop_time != INT64_MAX && recording_time == INT64_MAX) {
  731. int64_t start = start_time == AV_NOPTS_VALUE ? 0 : start_time;
  732. if (stop_time <= start) {
  733. av_log(NULL, AV_LOG_ERROR, "-to value smaller than -ss; aborting.\n");
  734. exit_program(1);
  735. } else {
  736. recording_time = stop_time - start;
  737. }
  738. }
  739. if (o->format) {
  740. if (!(file_iformat = av_find_input_format(o->format))) {
  741. av_log(NULL, AV_LOG_FATAL, "Unknown input format: '%s'\n", o->format);
  742. exit_program(1);
  743. }
  744. }
  745. if (!strcmp(filename, "-"))
  746. filename = "fd:";
  747. stdin_interaction &= strncmp(filename, "pipe:", 5) &&
  748. strcmp(filename, "fd:") &&
  749. strcmp(filename, "/dev/stdin");
  750. /* get default parameters from command line */
  751. ic = avformat_alloc_context();
  752. if (!ic)
  753. report_and_exit(AVERROR(ENOMEM));
  754. if (o->nb_audio_sample_rate) {
  755. av_dict_set_int(&o->g->format_opts, "sample_rate", o->audio_sample_rate[o->nb_audio_sample_rate - 1].u.i, 0);
  756. }
  757. if (o->nb_audio_channels) {
  758. const AVClass *priv_class;
  759. if (file_iformat && (priv_class = file_iformat->priv_class) &&
  760. av_opt_find(&priv_class, "ch_layout", NULL, 0,
  761. AV_OPT_SEARCH_FAKE_OBJ)) {
  762. char buf[32];
  763. snprintf(buf, sizeof(buf), "%dC", o->audio_channels[o->nb_audio_channels - 1].u.i);
  764. av_dict_set(&o->g->format_opts, "ch_layout", buf, 0);
  765. }
  766. }
  767. if (o->nb_audio_ch_layouts) {
  768. const AVClass *priv_class;
  769. if (file_iformat && (priv_class = file_iformat->priv_class) &&
  770. av_opt_find(&priv_class, "ch_layout", NULL, 0,
  771. AV_OPT_SEARCH_FAKE_OBJ)) {
  772. av_dict_set(&o->g->format_opts, "ch_layout", o->audio_ch_layouts[o->nb_audio_ch_layouts - 1].u.str, 0);
  773. }
  774. }
  775. if (o->nb_frame_rates) {
  776. const AVClass *priv_class;
  777. /* set the format-level framerate option;
  778. * this is important for video grabbers, e.g. x11 */
  779. if (file_iformat && (priv_class = file_iformat->priv_class) &&
  780. av_opt_find(&priv_class, "framerate", NULL, 0,
  781. AV_OPT_SEARCH_FAKE_OBJ)) {
  782. av_dict_set(&o->g->format_opts, "framerate",
  783. o->frame_rates[o->nb_frame_rates - 1].u.str, 0);
  784. }
  785. }
  786. if (o->nb_frame_sizes) {
  787. av_dict_set(&o->g->format_opts, "video_size", o->frame_sizes[o->nb_frame_sizes - 1].u.str, 0);
  788. }
  789. if (o->nb_frame_pix_fmts)
  790. av_dict_set(&o->g->format_opts, "pixel_format", o->frame_pix_fmts[o->nb_frame_pix_fmts - 1].u.str, 0);
  791. MATCH_PER_TYPE_OPT(codec_names, str, video_codec_name, ic, "v");
  792. MATCH_PER_TYPE_OPT(codec_names, str, audio_codec_name, ic, "a");
  793. MATCH_PER_TYPE_OPT(codec_names, str, subtitle_codec_name, ic, "s");
  794. MATCH_PER_TYPE_OPT(codec_names, str, data_codec_name, ic, "d");
  795. if (video_codec_name)
  796. ic->video_codec = find_codec_or_die(NULL, video_codec_name , AVMEDIA_TYPE_VIDEO , 0);
  797. if (audio_codec_name)
  798. ic->audio_codec = find_codec_or_die(NULL, audio_codec_name , AVMEDIA_TYPE_AUDIO , 0);
  799. if (subtitle_codec_name)
  800. ic->subtitle_codec = find_codec_or_die(NULL, subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, 0);
  801. if (data_codec_name)
  802. ic->data_codec = find_codec_or_die(NULL, data_codec_name , AVMEDIA_TYPE_DATA , 0);
  803. ic->video_codec_id = video_codec_name ? ic->video_codec->id : AV_CODEC_ID_NONE;
  804. ic->audio_codec_id = audio_codec_name ? ic->audio_codec->id : AV_CODEC_ID_NONE;
  805. ic->subtitle_codec_id = subtitle_codec_name ? ic->subtitle_codec->id : AV_CODEC_ID_NONE;
  806. ic->data_codec_id = data_codec_name ? ic->data_codec->id : AV_CODEC_ID_NONE;
  807. ic->flags |= AVFMT_FLAG_NONBLOCK;
  808. if (o->bitexact)
  809. ic->flags |= AVFMT_FLAG_BITEXACT;
  810. ic->interrupt_callback = int_cb;
  811. if (!av_dict_get(o->g->format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE)) {
  812. av_dict_set(&o->g->format_opts, "scan_all_pmts", "1", AV_DICT_DONT_OVERWRITE);
  813. scan_all_pmts_set = 1;
  814. }
  815. /* open the input file with generic avformat function */
  816. err = avformat_open_input(&ic, filename, file_iformat, &o->g->format_opts);
  817. if (err < 0) {
  818. print_error(filename, err);
  819. if (err == AVERROR_PROTOCOL_NOT_FOUND)
  820. av_log(NULL, AV_LOG_ERROR, "Did you mean file:%s?\n", filename);
  821. exit_program(1);
  822. }
  823. if (scan_all_pmts_set)
  824. av_dict_set(&o->g->format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE);
  825. remove_avoptions(&o->g->format_opts, o->g->codec_opts);
  826. assert_avoptions(o->g->format_opts);
  827. /* apply forced codec ids */
  828. for (i = 0; i < ic->nb_streams; i++)
  829. choose_decoder(o, ic, ic->streams[i], HWACCEL_NONE, AV_HWDEVICE_TYPE_NONE);
  830. if (o->find_stream_info) {
  831. AVDictionary **opts = setup_find_stream_info_opts(ic, o->g->codec_opts);
  832. int orig_nb_streams = ic->nb_streams;
  833. /* If not enough info to get the stream parameters, we decode the
  834. first frames to get it. (used in mpeg case for example) */
  835. ret = avformat_find_stream_info(ic, opts);
  836. for (i = 0; i < orig_nb_streams; i++)
  837. av_dict_free(&opts[i]);
  838. av_freep(&opts);
  839. if (ret < 0) {
  840. av_log(NULL, AV_LOG_FATAL, "%s: could not find codec parameters\n", filename);
  841. if (ic->nb_streams == 0) {
  842. avformat_close_input(&ic);
  843. exit_program(1);
  844. }
  845. }
  846. }
  847. if (start_time != AV_NOPTS_VALUE && start_time_eof != AV_NOPTS_VALUE) {
  848. av_log(NULL, AV_LOG_WARNING, "Cannot use -ss and -sseof both, using -ss for %s\n", filename);
  849. start_time_eof = AV_NOPTS_VALUE;
  850. }
  851. if (start_time_eof != AV_NOPTS_VALUE) {
  852. if (start_time_eof >= 0) {
  853. av_log(NULL, AV_LOG_ERROR, "-sseof value must be negative; aborting\n");
  854. exit_program(1);
  855. }
  856. if (ic->duration > 0) {
  857. start_time = start_time_eof + ic->duration;
  858. if (start_time < 0) {
  859. av_log(NULL, AV_LOG_WARNING, "-sseof value seeks to before start of file %s; ignored\n", filename);
  860. start_time = AV_NOPTS_VALUE;
  861. }
  862. } else
  863. av_log(NULL, AV_LOG_WARNING, "Cannot use -sseof, duration of %s not known\n", filename);
  864. }
  865. timestamp = (start_time == AV_NOPTS_VALUE) ? 0 : start_time;
  866. /* add the stream start time */
  867. if (!o->seek_timestamp && ic->start_time != AV_NOPTS_VALUE)
  868. timestamp += ic->start_time;
  869. /* if seeking requested, we execute it */
  870. if (start_time != AV_NOPTS_VALUE) {
  871. int64_t seek_timestamp = timestamp;
  872. if (!(ic->iformat->flags & AVFMT_SEEK_TO_PTS)) {
  873. int dts_heuristic = 0;
  874. for (i=0; i<ic->nb_streams; i++) {
  875. const AVCodecParameters *par = ic->streams[i]->codecpar;
  876. if (par->video_delay) {
  877. dts_heuristic = 1;
  878. break;
  879. }
  880. }
  881. if (dts_heuristic) {
  882. seek_timestamp -= 3*AV_TIME_BASE / 23;
  883. }
  884. }
  885. ret = avformat_seek_file(ic, -1, INT64_MIN, seek_timestamp, seek_timestamp, 0);
  886. if (ret < 0) {
  887. av_log(NULL, AV_LOG_WARNING, "%s: could not seek to position %0.3f\n",
  888. filename, (double)timestamp / AV_TIME_BASE);
  889. }
  890. }
  891. d = allocate_array_elem(&input_files, sizeof(*d), &nb_input_files);
  892. f = &d->f;
  893. f->ctx = ic;
  894. f->index = nb_input_files - 1;
  895. f->start_time = start_time;
  896. f->recording_time = recording_time;
  897. f->input_sync_ref = o->input_sync_ref;
  898. f->input_ts_offset = o->input_ts_offset;
  899. f->ts_offset = o->input_ts_offset - (copy_ts ? (start_at_zero && ic->start_time != AV_NOPTS_VALUE ? ic->start_time : 0) : timestamp);
  900. f->rate_emu = o->rate_emu;
  901. f->accurate_seek = o->accurate_seek;
  902. d->loop = o->loop;
  903. d->duration = 0;
  904. d->time_base = (AVRational){ 1, 1 };
  905. f->readrate = o->readrate ? o->readrate : 0.0;
  906. if (f->readrate < 0.0f) {
  907. av_log(NULL, AV_LOG_ERROR, "Option -readrate for Input #%d is %0.3f; it must be non-negative.\n", f->index, f->readrate);
  908. exit_program(1);
  909. }
  910. if (f->readrate && f->rate_emu) {
  911. av_log(NULL, AV_LOG_WARNING, "Both -readrate and -re set for Input #%d. Using -readrate %0.3f.\n", f->index, f->readrate);
  912. f->rate_emu = 0;
  913. }
  914. d->thread_queue_size = o->thread_queue_size;
  915. /* update the current parameters so that they match the one of the input stream */
  916. add_input_streams(o, d);
  917. /* dump the file content */
  918. av_dump_format(ic, f->index, filename, 0);
  919. /* check if all codec options have been used */
  920. unused_opts = strip_specifiers(o->g->codec_opts);
  921. for (i = 0; i < f->nb_streams; i++) {
  922. e = NULL;
  923. while ((e = av_dict_iterate(f->streams[i]->decoder_opts, e)))
  924. av_dict_set(&unused_opts, e->key, NULL, 0);
  925. }
  926. e = NULL;
  927. while ((e = av_dict_iterate(unused_opts, e))) {
  928. const AVClass *class = avcodec_get_class();
  929. const AVOption *option = av_opt_find(&class, e->key, NULL, 0,
  930. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
  931. const AVClass *fclass = avformat_get_class();
  932. const AVOption *foption = av_opt_find(&fclass, e->key, NULL, 0,
  933. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
  934. if (!option || foption)
  935. continue;
  936. if (!(option->flags & AV_OPT_FLAG_DECODING_PARAM)) {
  937. av_log(NULL, AV_LOG_ERROR, "Codec AVOption %s (%s) specified for "
  938. "input file #%d (%s) is not a decoding option.\n", e->key,
  939. option->help ? option->help : "", f->index,
  940. filename);
  941. exit_program(1);
  942. }
  943. av_log(NULL, AV_LOG_WARNING, "Codec AVOption %s (%s) specified for "
  944. "input file #%d (%s) has not been used for any stream. The most "
  945. "likely reason is either wrong type (e.g. a video option with "
  946. "no video streams) or that it is a private option of some decoder "
  947. "which was not actually used for any stream.\n", e->key,
  948. option->help ? option->help : "", f->index, filename);
  949. }
  950. av_dict_free(&unused_opts);
  951. for (i = 0; i < o->nb_dump_attachment; i++) {
  952. int j;
  953. for (j = 0; j < ic->nb_streams; j++) {
  954. AVStream *st = ic->streams[j];
  955. if (check_stream_specifier(ic, st, o->dump_attachment[i].specifier) == 1)
  956. dump_attachment(st, o->dump_attachment[i].u.str);
  957. }
  958. }
  959. return 0;
  960. }