ffmpeg_demux.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669
  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 DemuxStream {
  49. InputStream ist;
  50. // name used for logging
  51. char log_name[32];
  52. double ts_scale;
  53. int streamcopy_needed;
  54. int wrap_correction_done;
  55. int saw_first_ts;
  56. ///< dts of the first packet read for this stream (in AV_TIME_BASE units)
  57. int64_t first_dts;
  58. /* predicted dts of the next packet read for this stream or (when there are
  59. * several frames in a packet) of the next frame in current packet (in AV_TIME_BASE units) */
  60. int64_t next_dts;
  61. ///< dts of the last packet read for this stream (in AV_TIME_BASE units)
  62. int64_t dts;
  63. int64_t min_pts; /* pts with the smallest value in a current stream */
  64. int64_t max_pts; /* pts with the higher value in a current stream */
  65. /* number of packets successfully read for this stream */
  66. uint64_t nb_packets;
  67. // combined size of all the packets read
  68. uint64_t data_size;
  69. } DemuxStream;
  70. typedef struct Demuxer {
  71. InputFile f;
  72. // name used for logging
  73. char log_name[32];
  74. int64_t wallclock_start;
  75. /**
  76. * Extra timestamp offset added by discontinuity handling.
  77. */
  78. int64_t ts_offset_discont;
  79. int64_t last_ts;
  80. /* number of times input stream should be looped */
  81. int loop;
  82. /* actual duration of the longest stream in a file at the moment when
  83. * looping happens */
  84. int64_t duration;
  85. /* time base of the duration */
  86. AVRational time_base;
  87. /* number of streams that the user was warned of */
  88. int nb_streams_warn;
  89. double readrate_initial_burst;
  90. AVThreadMessageQueue *in_thread_queue;
  91. int thread_queue_size;
  92. pthread_t thread;
  93. int non_blocking;
  94. int read_started;
  95. } Demuxer;
  96. typedef struct DemuxMsg {
  97. AVPacket *pkt;
  98. int looping;
  99. } DemuxMsg;
  100. static DemuxStream *ds_from_ist(InputStream *ist)
  101. {
  102. return (DemuxStream*)ist;
  103. }
  104. static Demuxer *demuxer_from_ifile(InputFile *f)
  105. {
  106. return (Demuxer*)f;
  107. }
  108. InputStream *ist_find_unused(enum AVMediaType type)
  109. {
  110. for (InputStream *ist = ist_iter(NULL); ist; ist = ist_iter(ist)) {
  111. if (ist->par->codec_type == type && ist->discard &&
  112. ist->user_set_discard != AVDISCARD_ALL)
  113. return ist;
  114. }
  115. return NULL;
  116. }
  117. static void report_new_stream(Demuxer *d, const AVPacket *pkt)
  118. {
  119. AVStream *st = d->f.ctx->streams[pkt->stream_index];
  120. if (pkt->stream_index < d->nb_streams_warn)
  121. return;
  122. av_log(d, AV_LOG_WARNING,
  123. "New %s stream with index %d at pos:%"PRId64" and DTS:%ss\n",
  124. av_get_media_type_string(st->codecpar->codec_type),
  125. pkt->stream_index, pkt->pos, av_ts2timestr(pkt->dts, &st->time_base));
  126. d->nb_streams_warn = pkt->stream_index + 1;
  127. }
  128. static void ifile_duration_update(Demuxer *d, DemuxStream *ds,
  129. int64_t last_duration)
  130. {
  131. /* the total duration of the stream, max_pts - min_pts is
  132. * the duration of the stream without the last frame */
  133. if (ds->max_pts > ds->min_pts &&
  134. ds->max_pts - (uint64_t)ds->min_pts < INT64_MAX - last_duration)
  135. last_duration += ds->max_pts - ds->min_pts;
  136. if (!d->duration ||
  137. av_compare_ts(d->duration, d->time_base,
  138. last_duration, ds->ist.st->time_base) < 0) {
  139. d->duration = last_duration;
  140. d->time_base = ds->ist.st->time_base;
  141. }
  142. }
  143. static int seek_to_start(Demuxer *d)
  144. {
  145. InputFile *ifile = &d->f;
  146. AVFormatContext *is = ifile->ctx;
  147. int ret;
  148. ret = avformat_seek_file(is, -1, INT64_MIN, is->start_time, is->start_time, 0);
  149. if (ret < 0)
  150. return ret;
  151. if (ifile->audio_duration_queue_size) {
  152. /* duration is the length of the last frame in a stream
  153. * when audio stream is present we don't care about
  154. * last video frame length because it's not defined exactly */
  155. int got_durations = 0;
  156. while (got_durations < ifile->audio_duration_queue_size) {
  157. DemuxStream *ds;
  158. LastFrameDuration dur;
  159. ret = av_thread_message_queue_recv(ifile->audio_duration_queue, &dur, 0);
  160. if (ret < 0)
  161. return ret;
  162. got_durations++;
  163. ds = ds_from_ist(ifile->streams[dur.stream_idx]);
  164. ifile_duration_update(d, ds, dur.duration);
  165. }
  166. } else {
  167. for (int i = 0; i < ifile->nb_streams; i++) {
  168. int64_t duration = 0;
  169. InputStream *ist = ifile->streams[i];
  170. DemuxStream *ds = ds_from_ist(ist);
  171. if (ist->framerate.num) {
  172. duration = av_rescale_q(1, av_inv_q(ist->framerate), ist->st->time_base);
  173. } else if (ist->st->avg_frame_rate.num) {
  174. duration = av_rescale_q(1, av_inv_q(ist->st->avg_frame_rate), ist->st->time_base);
  175. } else {
  176. duration = 1;
  177. }
  178. ifile_duration_update(d, ds, duration);
  179. }
  180. }
  181. if (d->loop > 0)
  182. d->loop--;
  183. return ret;
  184. }
  185. static void ts_discontinuity_detect(Demuxer *d, InputStream *ist,
  186. AVPacket *pkt)
  187. {
  188. InputFile *ifile = &d->f;
  189. DemuxStream *ds = ds_from_ist(ist);
  190. const int fmt_is_discont = ifile->ctx->iformat->flags & AVFMT_TS_DISCONT;
  191. int disable_discontinuity_correction = copy_ts;
  192. int64_t pkt_dts = av_rescale_q_rnd(pkt->dts, pkt->time_base, AV_TIME_BASE_Q,
  193. AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
  194. if (copy_ts && ds->next_dts != AV_NOPTS_VALUE &&
  195. fmt_is_discont && ist->st->pts_wrap_bits < 60) {
  196. int64_t wrap_dts = av_rescale_q_rnd(pkt->dts + (1LL<<ist->st->pts_wrap_bits),
  197. pkt->time_base, AV_TIME_BASE_Q,
  198. AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
  199. if (FFABS(wrap_dts - ds->next_dts) < FFABS(pkt_dts - ds->next_dts)/10)
  200. disable_discontinuity_correction = 0;
  201. }
  202. if (ds->next_dts != AV_NOPTS_VALUE && !disable_discontinuity_correction) {
  203. int64_t delta = pkt_dts - ds->next_dts;
  204. if (fmt_is_discont) {
  205. if (FFABS(delta) > 1LL * dts_delta_threshold * AV_TIME_BASE ||
  206. pkt_dts + AV_TIME_BASE/10 < ds->dts) {
  207. d->ts_offset_discont -= delta;
  208. av_log(ist, AV_LOG_WARNING,
  209. "timestamp discontinuity "
  210. "(stream id=%d): %"PRId64", new offset= %"PRId64"\n",
  211. ist->st->id, delta, d->ts_offset_discont);
  212. pkt->dts -= av_rescale_q(delta, AV_TIME_BASE_Q, pkt->time_base);
  213. if (pkt->pts != AV_NOPTS_VALUE)
  214. pkt->pts -= av_rescale_q(delta, AV_TIME_BASE_Q, pkt->time_base);
  215. }
  216. } else {
  217. if (FFABS(delta) > 1LL * dts_error_threshold * AV_TIME_BASE) {
  218. av_log(NULL, AV_LOG_WARNING,
  219. "DTS %"PRId64", next:%"PRId64" st:%d invalid dropping\n",
  220. pkt->dts, ds->next_dts, pkt->stream_index);
  221. pkt->dts = AV_NOPTS_VALUE;
  222. }
  223. if (pkt->pts != AV_NOPTS_VALUE){
  224. int64_t pkt_pts = av_rescale_q(pkt->pts, pkt->time_base, AV_TIME_BASE_Q);
  225. delta = pkt_pts - ds->next_dts;
  226. if (FFABS(delta) > 1LL * dts_error_threshold * AV_TIME_BASE) {
  227. av_log(NULL, AV_LOG_WARNING,
  228. "PTS %"PRId64", next:%"PRId64" invalid dropping st:%d\n",
  229. pkt->pts, ds->next_dts, pkt->stream_index);
  230. pkt->pts = AV_NOPTS_VALUE;
  231. }
  232. }
  233. }
  234. } else if (ds->next_dts == AV_NOPTS_VALUE && !copy_ts &&
  235. fmt_is_discont && d->last_ts != AV_NOPTS_VALUE) {
  236. int64_t delta = pkt_dts - d->last_ts;
  237. if (FFABS(delta) > 1LL * dts_delta_threshold * AV_TIME_BASE) {
  238. d->ts_offset_discont -= delta;
  239. av_log(NULL, AV_LOG_DEBUG,
  240. "Inter stream timestamp discontinuity %"PRId64", new offset= %"PRId64"\n",
  241. delta, d->ts_offset_discont);
  242. pkt->dts -= av_rescale_q(delta, AV_TIME_BASE_Q, pkt->time_base);
  243. if (pkt->pts != AV_NOPTS_VALUE)
  244. pkt->pts -= av_rescale_q(delta, AV_TIME_BASE_Q, pkt->time_base);
  245. }
  246. }
  247. d->last_ts = av_rescale_q(pkt->dts, pkt->time_base, AV_TIME_BASE_Q);
  248. }
  249. static void ts_discontinuity_process(Demuxer *d, InputStream *ist,
  250. AVPacket *pkt)
  251. {
  252. int64_t offset = av_rescale_q(d->ts_offset_discont, AV_TIME_BASE_Q,
  253. pkt->time_base);
  254. // apply previously-detected timestamp-discontinuity offset
  255. // (to all streams, not just audio/video)
  256. if (pkt->dts != AV_NOPTS_VALUE)
  257. pkt->dts += offset;
  258. if (pkt->pts != AV_NOPTS_VALUE)
  259. pkt->pts += offset;
  260. // detect timestamp discontinuities for audio/video
  261. if ((ist->par->codec_type == AVMEDIA_TYPE_VIDEO ||
  262. ist->par->codec_type == AVMEDIA_TYPE_AUDIO) &&
  263. pkt->dts != AV_NOPTS_VALUE)
  264. ts_discontinuity_detect(d, ist, pkt);
  265. }
  266. static int ist_dts_update(DemuxStream *ds, AVPacket *pkt)
  267. {
  268. InputStream *ist = &ds->ist;
  269. const AVCodecParameters *par = ist->par;
  270. if (!ds->saw_first_ts) {
  271. ds->first_dts =
  272. ds->dts = ist->st->avg_frame_rate.num ? - ist->par->video_delay * AV_TIME_BASE / av_q2d(ist->st->avg_frame_rate) : 0;
  273. if (pkt->pts != AV_NOPTS_VALUE) {
  274. ds->first_dts =
  275. ds->dts += av_rescale_q(pkt->pts, pkt->time_base, AV_TIME_BASE_Q);
  276. }
  277. ds->saw_first_ts = 1;
  278. }
  279. if (ds->next_dts == AV_NOPTS_VALUE)
  280. ds->next_dts = ds->dts;
  281. if (pkt->dts != AV_NOPTS_VALUE)
  282. ds->next_dts = ds->dts = av_rescale_q(pkt->dts, pkt->time_base, AV_TIME_BASE_Q);
  283. ds->dts = ds->next_dts;
  284. switch (par->codec_type) {
  285. case AVMEDIA_TYPE_AUDIO:
  286. av_assert1(pkt->duration >= 0);
  287. if (par->sample_rate) {
  288. ds->next_dts += ((int64_t)AV_TIME_BASE * par->frame_size) /
  289. par->sample_rate;
  290. } else {
  291. ds->next_dts += av_rescale_q(pkt->duration, pkt->time_base, AV_TIME_BASE_Q);
  292. }
  293. break;
  294. case AVMEDIA_TYPE_VIDEO:
  295. if (ist->framerate.num) {
  296. // TODO: Remove work-around for c99-to-c89 issue 7
  297. AVRational time_base_q = AV_TIME_BASE_Q;
  298. int64_t next_dts = av_rescale_q(ds->next_dts, time_base_q, av_inv_q(ist->framerate));
  299. ds->next_dts = av_rescale_q(next_dts + 1, av_inv_q(ist->framerate), time_base_q);
  300. } else if (pkt->duration) {
  301. ds->next_dts += av_rescale_q(pkt->duration, pkt->time_base, AV_TIME_BASE_Q);
  302. } else if (ist->par->framerate.num != 0) {
  303. AVRational field_rate = av_mul_q(ist->par->framerate,
  304. (AVRational){ 2, 1 });
  305. int fields = 2;
  306. if (ist->codec_desc &&
  307. (ist->codec_desc->props & AV_CODEC_PROP_FIELDS) &&
  308. av_stream_get_parser(ist->st))
  309. fields = 1 + av_stream_get_parser(ist->st)->repeat_pict;
  310. ds->next_dts += av_rescale_q(fields, av_inv_q(field_rate), AV_TIME_BASE_Q);
  311. }
  312. break;
  313. }
  314. av_assert0(!pkt->opaque_ref);
  315. if (ds->streamcopy_needed) {
  316. DemuxPktData *pd;
  317. pkt->opaque_ref = av_buffer_allocz(sizeof(*pd));
  318. if (!pkt->opaque_ref)
  319. return AVERROR(ENOMEM);
  320. pd = (DemuxPktData*)pkt->opaque_ref->data;
  321. pd->dts_est = ds->dts;
  322. }
  323. return 0;
  324. }
  325. static int ts_fixup(Demuxer *d, AVPacket *pkt)
  326. {
  327. InputFile *ifile = &d->f;
  328. InputStream *ist = ifile->streams[pkt->stream_index];
  329. DemuxStream *ds = ds_from_ist(ist);
  330. const int64_t start_time = ifile->start_time_effective;
  331. int64_t duration;
  332. int ret;
  333. pkt->time_base = ist->st->time_base;
  334. #define SHOW_TS_DEBUG(tag_) \
  335. if (debug_ts) { \
  336. av_log(ist, AV_LOG_INFO, "%s -> ist_index:%d:%d type:%s " \
  337. "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s\n", \
  338. tag_, ifile->index, pkt->stream_index, \
  339. av_get_media_type_string(ist->st->codecpar->codec_type), \
  340. av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &pkt->time_base), \
  341. av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &pkt->time_base), \
  342. av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &pkt->time_base)); \
  343. }
  344. SHOW_TS_DEBUG("demuxer");
  345. if (!ds->wrap_correction_done && start_time != AV_NOPTS_VALUE &&
  346. ist->st->pts_wrap_bits < 64) {
  347. int64_t stime, stime2;
  348. stime = av_rescale_q(start_time, AV_TIME_BASE_Q, pkt->time_base);
  349. stime2= stime + (1ULL<<ist->st->pts_wrap_bits);
  350. ds->wrap_correction_done = 1;
  351. if(stime2 > stime && pkt->dts != AV_NOPTS_VALUE && pkt->dts > stime + (1LL<<(ist->st->pts_wrap_bits-1))) {
  352. pkt->dts -= 1ULL<<ist->st->pts_wrap_bits;
  353. ds->wrap_correction_done = 0;
  354. }
  355. if(stime2 > stime && pkt->pts != AV_NOPTS_VALUE && pkt->pts > stime + (1LL<<(ist->st->pts_wrap_bits-1))) {
  356. pkt->pts -= 1ULL<<ist->st->pts_wrap_bits;
  357. ds->wrap_correction_done = 0;
  358. }
  359. }
  360. if (pkt->dts != AV_NOPTS_VALUE)
  361. pkt->dts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, pkt->time_base);
  362. if (pkt->pts != AV_NOPTS_VALUE)
  363. pkt->pts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, pkt->time_base);
  364. if (pkt->pts != AV_NOPTS_VALUE)
  365. pkt->pts *= ds->ts_scale;
  366. if (pkt->dts != AV_NOPTS_VALUE)
  367. pkt->dts *= ds->ts_scale;
  368. duration = av_rescale_q(d->duration, d->time_base, pkt->time_base);
  369. if (pkt->pts != AV_NOPTS_VALUE) {
  370. pkt->pts += duration;
  371. ds->max_pts = FFMAX(pkt->pts, ds->max_pts);
  372. ds->min_pts = FFMIN(pkt->pts, ds->min_pts);
  373. }
  374. if (pkt->dts != AV_NOPTS_VALUE)
  375. pkt->dts += duration;
  376. SHOW_TS_DEBUG("demuxer+tsfixup");
  377. // detect and try to correct for timestamp discontinuities
  378. ts_discontinuity_process(d, ist, pkt);
  379. // update estimated/predicted dts
  380. ret = ist_dts_update(ds, pkt);
  381. if (ret < 0)
  382. return ret;
  383. return 0;
  384. }
  385. // process an input packet into a message to send to the consumer thread
  386. // src is always cleared by this function
  387. static int input_packet_process(Demuxer *d, DemuxMsg *msg, AVPacket *src)
  388. {
  389. InputFile *f = &d->f;
  390. InputStream *ist = f->streams[src->stream_index];
  391. DemuxStream *ds = ds_from_ist(ist);
  392. AVPacket *pkt;
  393. int ret = 0;
  394. pkt = av_packet_alloc();
  395. if (!pkt) {
  396. av_packet_unref(src);
  397. return AVERROR(ENOMEM);
  398. }
  399. av_packet_move_ref(pkt, src);
  400. ret = ts_fixup(d, pkt);
  401. if (ret < 0)
  402. goto fail;
  403. ds->data_size += pkt->size;
  404. ds->nb_packets++;
  405. /* add the stream-global side data to the first packet */
  406. if (ds->nb_packets == 1) {
  407. for (int i = 0; i < ist->st->nb_side_data; i++) {
  408. AVPacketSideData *src_sd = &ist->st->side_data[i];
  409. uint8_t *dst_data;
  410. if (src_sd->type == AV_PKT_DATA_DISPLAYMATRIX)
  411. continue;
  412. if (av_packet_get_side_data(pkt, src_sd->type, NULL))
  413. continue;
  414. dst_data = av_packet_new_side_data(pkt, src_sd->type, src_sd->size);
  415. if (!dst_data) {
  416. ret = AVERROR(ENOMEM);
  417. goto fail;
  418. }
  419. memcpy(dst_data, src_sd->data, src_sd->size);
  420. }
  421. }
  422. if (debug_ts) {
  423. av_log(NULL, AV_LOG_INFO, "demuxer+ffmpeg -> ist_index:%d:%d type:%s pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s off:%s off_time:%s\n",
  424. f->index, pkt->stream_index,
  425. av_get_media_type_string(ist->par->codec_type),
  426. av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &pkt->time_base),
  427. av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &pkt->time_base),
  428. av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &pkt->time_base),
  429. av_ts2str(input_files[ist->file_index]->ts_offset),
  430. av_ts2timestr(input_files[ist->file_index]->ts_offset, &AV_TIME_BASE_Q));
  431. }
  432. msg->pkt = pkt;
  433. pkt = NULL;
  434. fail:
  435. av_packet_free(&pkt);
  436. return ret;
  437. }
  438. static void readrate_sleep(Demuxer *d)
  439. {
  440. InputFile *f = &d->f;
  441. int64_t file_start = copy_ts * (
  442. (f->start_time_effective != AV_NOPTS_VALUE ? f->start_time_effective * !start_at_zero : 0) +
  443. (f->start_time != AV_NOPTS_VALUE ? f->start_time : 0)
  444. );
  445. int64_t burst_until = AV_TIME_BASE * d->readrate_initial_burst;
  446. for (int i = 0; i < f->nb_streams; i++) {
  447. InputStream *ist = f->streams[i];
  448. DemuxStream *ds = ds_from_ist(ist);
  449. int64_t stream_ts_offset, pts, now;
  450. stream_ts_offset = FFMAX(ds->first_dts != AV_NOPTS_VALUE ? ds->first_dts : 0, file_start);
  451. pts = av_rescale(ds->dts, 1000000, AV_TIME_BASE);
  452. now = (av_gettime_relative() - d->wallclock_start) * f->readrate + stream_ts_offset;
  453. if (pts - burst_until > now)
  454. av_usleep(pts - burst_until - now);
  455. }
  456. }
  457. static void discard_unused_programs(InputFile *ifile)
  458. {
  459. for (int j = 0; j < ifile->ctx->nb_programs; j++) {
  460. AVProgram *p = ifile->ctx->programs[j];
  461. int discard = AVDISCARD_ALL;
  462. for (int k = 0; k < p->nb_stream_indexes; k++)
  463. if (!ifile->streams[p->stream_index[k]]->discard) {
  464. discard = AVDISCARD_DEFAULT;
  465. break;
  466. }
  467. p->discard = discard;
  468. }
  469. }
  470. static void thread_set_name(InputFile *f)
  471. {
  472. char name[16];
  473. snprintf(name, sizeof(name), "dmx%d:%s", f->index, f->ctx->iformat->name);
  474. ff_thread_setname(name);
  475. }
  476. static void *input_thread(void *arg)
  477. {
  478. Demuxer *d = arg;
  479. InputFile *f = &d->f;
  480. AVPacket *pkt;
  481. unsigned flags = d->non_blocking ? AV_THREAD_MESSAGE_NONBLOCK : 0;
  482. int ret = 0;
  483. pkt = av_packet_alloc();
  484. if (!pkt) {
  485. ret = AVERROR(ENOMEM);
  486. goto finish;
  487. }
  488. thread_set_name(f);
  489. discard_unused_programs(f);
  490. d->wallclock_start = av_gettime_relative();
  491. while (1) {
  492. DemuxMsg msg = { NULL };
  493. ret = av_read_frame(f->ctx, pkt);
  494. if (ret == AVERROR(EAGAIN)) {
  495. av_usleep(10000);
  496. continue;
  497. }
  498. if (ret < 0) {
  499. if (d->loop) {
  500. /* signal looping to the consumer thread */
  501. msg.looping = 1;
  502. ret = av_thread_message_queue_send(d->in_thread_queue, &msg, 0);
  503. if (ret >= 0)
  504. ret = seek_to_start(d);
  505. if (ret >= 0)
  506. continue;
  507. /* fallthrough to the error path */
  508. }
  509. if (ret == AVERROR_EOF)
  510. av_log(d, AV_LOG_VERBOSE, "EOF while reading input\n");
  511. else
  512. av_log(d, AV_LOG_ERROR, "Error during demuxing: %s\n",
  513. av_err2str(ret));
  514. break;
  515. }
  516. if (do_pkt_dump) {
  517. av_pkt_dump_log2(NULL, AV_LOG_INFO, pkt, do_hex_dump,
  518. f->ctx->streams[pkt->stream_index]);
  519. }
  520. /* the following test is needed in case new streams appear
  521. dynamically in stream : we ignore them */
  522. if (pkt->stream_index >= f->nb_streams ||
  523. f->streams[pkt->stream_index]->discard) {
  524. report_new_stream(d, pkt);
  525. av_packet_unref(pkt);
  526. continue;
  527. }
  528. if (pkt->flags & AV_PKT_FLAG_CORRUPT) {
  529. av_log(d, exit_on_error ? AV_LOG_FATAL : AV_LOG_WARNING,
  530. "corrupt input packet in stream %d\n",
  531. pkt->stream_index);
  532. if (exit_on_error) {
  533. av_packet_unref(pkt);
  534. ret = AVERROR_INVALIDDATA;
  535. break;
  536. }
  537. }
  538. ret = input_packet_process(d, &msg, pkt);
  539. if (ret < 0)
  540. break;
  541. if (f->readrate)
  542. readrate_sleep(d);
  543. ret = av_thread_message_queue_send(d->in_thread_queue, &msg, flags);
  544. if (flags && ret == AVERROR(EAGAIN)) {
  545. flags = 0;
  546. ret = av_thread_message_queue_send(d->in_thread_queue, &msg, flags);
  547. av_log(f, AV_LOG_WARNING,
  548. "Thread message queue blocking; consider raising the "
  549. "thread_queue_size option (current value: %d)\n",
  550. d->thread_queue_size);
  551. }
  552. if (ret < 0) {
  553. if (ret != AVERROR_EOF)
  554. av_log(f, AV_LOG_ERROR,
  555. "Unable to send packet to main thread: %s\n",
  556. av_err2str(ret));
  557. av_packet_free(&msg.pkt);
  558. break;
  559. }
  560. }
  561. finish:
  562. av_assert0(ret < 0);
  563. av_thread_message_queue_set_err_recv(d->in_thread_queue, ret);
  564. av_packet_free(&pkt);
  565. av_log(d, AV_LOG_VERBOSE, "Terminating demuxer thread\n");
  566. return NULL;
  567. }
  568. static void thread_stop(Demuxer *d)
  569. {
  570. InputFile *f = &d->f;
  571. DemuxMsg msg;
  572. if (!d->in_thread_queue)
  573. return;
  574. av_thread_message_queue_set_err_send(d->in_thread_queue, AVERROR_EOF);
  575. while (av_thread_message_queue_recv(d->in_thread_queue, &msg, 0) >= 0)
  576. av_packet_free(&msg.pkt);
  577. pthread_join(d->thread, NULL);
  578. av_thread_message_queue_free(&d->in_thread_queue);
  579. av_thread_message_queue_free(&f->audio_duration_queue);
  580. }
  581. static int thread_start(Demuxer *d)
  582. {
  583. int ret;
  584. InputFile *f = &d->f;
  585. if (d->thread_queue_size <= 0)
  586. d->thread_queue_size = (nb_input_files > 1 ? 8 : 1);
  587. if (nb_input_files > 1 &&
  588. (f->ctx->pb ? !f->ctx->pb->seekable :
  589. strcmp(f->ctx->iformat->name, "lavfi")))
  590. d->non_blocking = 1;
  591. ret = av_thread_message_queue_alloc(&d->in_thread_queue,
  592. d->thread_queue_size, sizeof(DemuxMsg));
  593. if (ret < 0)
  594. return ret;
  595. if (d->loop) {
  596. int nb_audio_dec = 0;
  597. for (int i = 0; i < f->nb_streams; i++) {
  598. InputStream *ist = f->streams[i];
  599. nb_audio_dec += !!(ist->decoding_needed &&
  600. ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO);
  601. }
  602. if (nb_audio_dec) {
  603. ret = av_thread_message_queue_alloc(&f->audio_duration_queue,
  604. nb_audio_dec, sizeof(LastFrameDuration));
  605. if (ret < 0)
  606. goto fail;
  607. f->audio_duration_queue_size = nb_audio_dec;
  608. }
  609. }
  610. if ((ret = pthread_create(&d->thread, NULL, input_thread, d))) {
  611. av_log(d, AV_LOG_ERROR, "pthread_create failed: %s. Try to increase `ulimit -v` or decrease `ulimit -s`.\n", strerror(ret));
  612. ret = AVERROR(ret);
  613. goto fail;
  614. }
  615. d->read_started = 1;
  616. return 0;
  617. fail:
  618. av_thread_message_queue_free(&d->in_thread_queue);
  619. return ret;
  620. }
  621. int ifile_get_packet(InputFile *f, AVPacket **pkt)
  622. {
  623. Demuxer *d = demuxer_from_ifile(f);
  624. DemuxMsg msg;
  625. int ret;
  626. if (!d->in_thread_queue) {
  627. ret = thread_start(d);
  628. if (ret < 0)
  629. return ret;
  630. }
  631. ret = av_thread_message_queue_recv(d->in_thread_queue, &msg,
  632. d->non_blocking ?
  633. AV_THREAD_MESSAGE_NONBLOCK : 0);
  634. if (ret < 0)
  635. return ret;
  636. if (msg.looping)
  637. return 1;
  638. *pkt = msg.pkt;
  639. return 0;
  640. }
  641. static void demux_final_stats(Demuxer *d)
  642. {
  643. InputFile *f = &d->f;
  644. uint64_t total_packets = 0, total_size = 0;
  645. av_log(f, AV_LOG_VERBOSE, "Input file #%d (%s):\n",
  646. f->index, f->ctx->url);
  647. for (int j = 0; j < f->nb_streams; j++) {
  648. InputStream *ist = f->streams[j];
  649. DemuxStream *ds = ds_from_ist(ist);
  650. enum AVMediaType type = ist->par->codec_type;
  651. if (ist->discard || type == AVMEDIA_TYPE_ATTACHMENT)
  652. continue;
  653. total_size += ds->data_size;
  654. total_packets += ds->nb_packets;
  655. av_log(f, AV_LOG_VERBOSE, " Input stream #%d:%d (%s): ",
  656. f->index, j, av_get_media_type_string(type));
  657. av_log(f, AV_LOG_VERBOSE, "%"PRIu64" packets read (%"PRIu64" bytes); ",
  658. ds->nb_packets, ds->data_size);
  659. if (ist->decoding_needed) {
  660. av_log(f, AV_LOG_VERBOSE,
  661. "%"PRIu64" frames decoded; %"PRIu64" decode errors",
  662. ist->frames_decoded, ist->decode_errors);
  663. if (type == AVMEDIA_TYPE_AUDIO)
  664. av_log(f, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ist->samples_decoded);
  665. av_log(f, AV_LOG_VERBOSE, "; ");
  666. }
  667. av_log(f, AV_LOG_VERBOSE, "\n");
  668. }
  669. av_log(f, AV_LOG_VERBOSE, " Total: %"PRIu64" packets (%"PRIu64" bytes) demuxed\n",
  670. total_packets, total_size);
  671. }
  672. static void ist_free(InputStream **pist)
  673. {
  674. InputStream *ist = *pist;
  675. if (!ist)
  676. return;
  677. dec_free(&ist->decoder);
  678. av_dict_free(&ist->decoder_opts);
  679. av_freep(&ist->filters);
  680. av_freep(&ist->outputs);
  681. av_freep(&ist->hwaccel_device);
  682. avcodec_free_context(&ist->dec_ctx);
  683. avcodec_parameters_free(&ist->par);
  684. av_freep(pist);
  685. }
  686. void ifile_close(InputFile **pf)
  687. {
  688. InputFile *f = *pf;
  689. Demuxer *d = demuxer_from_ifile(f);
  690. if (!f)
  691. return;
  692. thread_stop(d);
  693. if (d->read_started)
  694. demux_final_stats(d);
  695. for (int i = 0; i < f->nb_streams; i++)
  696. ist_free(&f->streams[i]);
  697. av_freep(&f->streams);
  698. avformat_close_input(&f->ctx);
  699. av_freep(pf);
  700. }
  701. static int ist_use(InputStream *ist, int decoding_needed)
  702. {
  703. DemuxStream *ds = ds_from_ist(ist);
  704. if (ist->user_set_discard == AVDISCARD_ALL) {
  705. av_log(ist, AV_LOG_ERROR, "Cannot %s a disabled input stream\n",
  706. decoding_needed ? "decode" : "streamcopy");
  707. return AVERROR(EINVAL);
  708. }
  709. ist->discard = 0;
  710. ist->st->discard = ist->user_set_discard;
  711. ist->decoding_needed |= decoding_needed;
  712. ds->streamcopy_needed |= !decoding_needed;
  713. if (decoding_needed && !avcodec_is_open(ist->dec_ctx)) {
  714. int ret = dec_open(ist);
  715. if (ret < 0)
  716. return ret;
  717. }
  718. return 0;
  719. }
  720. int ist_output_add(InputStream *ist, OutputStream *ost)
  721. {
  722. int ret;
  723. ret = ist_use(ist, ost->enc ? DECODING_FOR_OST : 0);
  724. if (ret < 0)
  725. return ret;
  726. GROW_ARRAY(ist->outputs, ist->nb_outputs);
  727. ist->outputs[ist->nb_outputs - 1] = ost;
  728. return 0;
  729. }
  730. int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple)
  731. {
  732. int ret;
  733. ret = ist_use(ist, is_simple ? DECODING_FOR_OST : DECODING_FOR_FILTER);
  734. if (ret < 0)
  735. return ret;
  736. GROW_ARRAY(ist->filters, ist->nb_filters);
  737. ist->filters[ist->nb_filters - 1] = ifilter;
  738. // initialize fallback parameters for filtering
  739. ret = ifilter_parameters_from_dec(ifilter, ist->dec_ctx);
  740. if (ret < 0)
  741. return ret;
  742. return 0;
  743. }
  744. static const AVCodec *choose_decoder(const OptionsContext *o, AVFormatContext *s, AVStream *st,
  745. enum HWAccelID hwaccel_id, enum AVHWDeviceType hwaccel_device_type)
  746. {
  747. char *codec_name = NULL;
  748. MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, st);
  749. if (codec_name) {
  750. const AVCodec *codec = find_codec_or_die(NULL, codec_name, st->codecpar->codec_type, 0);
  751. st->codecpar->codec_id = codec->id;
  752. if (recast_media && st->codecpar->codec_type != codec->type)
  753. st->codecpar->codec_type = codec->type;
  754. return codec;
  755. } else {
  756. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
  757. hwaccel_id == HWACCEL_GENERIC &&
  758. hwaccel_device_type != AV_HWDEVICE_TYPE_NONE) {
  759. const AVCodec *c;
  760. void *i = NULL;
  761. while ((c = av_codec_iterate(&i))) {
  762. const AVCodecHWConfig *config;
  763. if (c->id != st->codecpar->codec_id ||
  764. !av_codec_is_decoder(c))
  765. continue;
  766. for (int j = 0; config = avcodec_get_hw_config(c, j); j++) {
  767. if (config->device_type == hwaccel_device_type) {
  768. av_log(NULL, AV_LOG_VERBOSE, "Selecting decoder '%s' because of requested hwaccel method %s\n",
  769. c->name, av_hwdevice_get_type_name(hwaccel_device_type));
  770. return c;
  771. }
  772. }
  773. }
  774. }
  775. return avcodec_find_decoder(st->codecpar->codec_id);
  776. }
  777. }
  778. static int guess_input_channel_layout(InputStream *ist, int guess_layout_max)
  779. {
  780. AVCodecContext *dec = ist->dec_ctx;
  781. if (dec->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC) {
  782. char layout_name[256];
  783. if (dec->ch_layout.nb_channels > guess_layout_max)
  784. return 0;
  785. av_channel_layout_default(&dec->ch_layout, dec->ch_layout.nb_channels);
  786. if (dec->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC)
  787. return 0;
  788. av_channel_layout_describe(&dec->ch_layout, layout_name, sizeof(layout_name));
  789. av_log(ist, AV_LOG_WARNING, "Guessed Channel Layout: %s\n", layout_name);
  790. }
  791. return 1;
  792. }
  793. static int add_display_matrix_to_stream(const OptionsContext *o,
  794. AVFormatContext *ctx, InputStream *ist)
  795. {
  796. AVStream *st = ist->st;
  797. double rotation = DBL_MAX;
  798. int hflip = -1, vflip = -1;
  799. int hflip_set = 0, vflip_set = 0, rotation_set = 0;
  800. int32_t *buf;
  801. MATCH_PER_STREAM_OPT(display_rotations, dbl, rotation, ctx, st);
  802. MATCH_PER_STREAM_OPT(display_hflips, i, hflip, ctx, st);
  803. MATCH_PER_STREAM_OPT(display_vflips, i, vflip, ctx, st);
  804. rotation_set = rotation != DBL_MAX;
  805. hflip_set = hflip != -1;
  806. vflip_set = vflip != -1;
  807. if (!rotation_set && !hflip_set && !vflip_set)
  808. return 0;
  809. buf = (int32_t *)av_stream_new_side_data(st, AV_PKT_DATA_DISPLAYMATRIX, sizeof(int32_t) * 9);
  810. if (!buf) {
  811. av_log(ist, AV_LOG_FATAL, "Failed to generate a display matrix!\n");
  812. return AVERROR(ENOMEM);
  813. }
  814. av_display_rotation_set(buf,
  815. rotation_set ? -(rotation) : -0.0f);
  816. av_display_matrix_flip(buf,
  817. hflip_set ? hflip : 0,
  818. vflip_set ? vflip : 0);
  819. return 0;
  820. }
  821. static const char *input_stream_item_name(void *obj)
  822. {
  823. const DemuxStream *ds = obj;
  824. return ds->log_name;
  825. }
  826. static const AVClass input_stream_class = {
  827. .class_name = "InputStream",
  828. .version = LIBAVUTIL_VERSION_INT,
  829. .item_name = input_stream_item_name,
  830. .category = AV_CLASS_CATEGORY_DEMUXER,
  831. };
  832. static DemuxStream *demux_stream_alloc(Demuxer *d, AVStream *st)
  833. {
  834. const char *type_str = av_get_media_type_string(st->codecpar->codec_type);
  835. InputFile *f = &d->f;
  836. DemuxStream *ds;
  837. ds = allocate_array_elem(&f->streams, sizeof(*ds), &f->nb_streams);
  838. if (!ds)
  839. return NULL;
  840. ds->ist.st = st;
  841. ds->ist.file_index = f->index;
  842. ds->ist.index = st->index;
  843. ds->ist.class = &input_stream_class;
  844. snprintf(ds->log_name, sizeof(ds->log_name), "%cist#%d:%d/%s",
  845. type_str ? *type_str : '?', d->f.index, st->index,
  846. avcodec_get_name(st->codecpar->codec_id));
  847. return ds;
  848. }
  849. static int ist_add(const OptionsContext *o, Demuxer *d, AVStream *st)
  850. {
  851. AVFormatContext *ic = d->f.ctx;
  852. AVCodecParameters *par = st->codecpar;
  853. DemuxStream *ds;
  854. InputStream *ist;
  855. char *framerate = NULL, *hwaccel_device = NULL;
  856. const char *hwaccel = NULL;
  857. char *hwaccel_output_format = NULL;
  858. char *codec_tag = NULL;
  859. char *next;
  860. char *discard_str = NULL;
  861. const AVClass *cc = avcodec_get_class();
  862. const AVOption *discard_opt = av_opt_find(&cc, "skip_frame", NULL,
  863. 0, AV_OPT_SEARCH_FAKE_OBJ);
  864. int ret;
  865. ds = demux_stream_alloc(d, st);
  866. if (!ds)
  867. return AVERROR(ENOMEM);
  868. ist = &ds->ist;
  869. ist->discard = 1;
  870. st->discard = AVDISCARD_ALL;
  871. ist->nb_samples = 0;
  872. ds->first_dts = AV_NOPTS_VALUE;
  873. ds->next_dts = AV_NOPTS_VALUE;
  874. ds->min_pts = INT64_MAX;
  875. ds->max_pts = INT64_MIN;
  876. ds->ts_scale = 1.0;
  877. MATCH_PER_STREAM_OPT(ts_scale, dbl, ds->ts_scale, ic, st);
  878. ist->autorotate = 1;
  879. MATCH_PER_STREAM_OPT(autorotate, i, ist->autorotate, ic, st);
  880. MATCH_PER_STREAM_OPT(codec_tags, str, codec_tag, ic, st);
  881. if (codec_tag) {
  882. uint32_t tag = strtol(codec_tag, &next, 0);
  883. if (*next) {
  884. uint8_t buf[4] = { 0 };
  885. memcpy(buf, codec_tag, FFMIN(sizeof(buf), strlen(codec_tag)));
  886. tag = AV_RL32(buf);
  887. }
  888. st->codecpar->codec_tag = tag;
  889. }
  890. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  891. ret = add_display_matrix_to_stream(o, ic, ist);
  892. if (ret < 0)
  893. return ret;
  894. MATCH_PER_STREAM_OPT(hwaccels, str, hwaccel, ic, st);
  895. MATCH_PER_STREAM_OPT(hwaccel_output_formats, str,
  896. hwaccel_output_format, ic, st);
  897. if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel, "cuvid")) {
  898. av_log(ist, AV_LOG_WARNING,
  899. "WARNING: defaulting hwaccel_output_format to cuda for compatibility "
  900. "with old commandlines. This behaviour is DEPRECATED and will be removed "
  901. "in the future. Please explicitly set \"-hwaccel_output_format cuda\".\n");
  902. ist->hwaccel_output_format = AV_PIX_FMT_CUDA;
  903. } else if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel, "qsv")) {
  904. av_log(ist, AV_LOG_WARNING,
  905. "WARNING: defaulting hwaccel_output_format to qsv for compatibility "
  906. "with old commandlines. This behaviour is DEPRECATED and will be removed "
  907. "in the future. Please explicitly set \"-hwaccel_output_format qsv\".\n");
  908. ist->hwaccel_output_format = AV_PIX_FMT_QSV;
  909. } else if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel, "mediacodec")) {
  910. // There is no real AVHWFrameContext implementation. Set
  911. // hwaccel_output_format to avoid av_hwframe_transfer_data error.
  912. ist->hwaccel_output_format = AV_PIX_FMT_MEDIACODEC;
  913. } else if (hwaccel_output_format) {
  914. ist->hwaccel_output_format = av_get_pix_fmt(hwaccel_output_format);
  915. if (ist->hwaccel_output_format == AV_PIX_FMT_NONE) {
  916. av_log(ist, AV_LOG_FATAL, "Unrecognised hwaccel output "
  917. "format: %s", hwaccel_output_format);
  918. }
  919. } else {
  920. ist->hwaccel_output_format = AV_PIX_FMT_NONE;
  921. }
  922. if (hwaccel) {
  923. // The NVDEC hwaccels use a CUDA device, so remap the name here.
  924. if (!strcmp(hwaccel, "nvdec") || !strcmp(hwaccel, "cuvid"))
  925. hwaccel = "cuda";
  926. if (!strcmp(hwaccel, "none"))
  927. ist->hwaccel_id = HWACCEL_NONE;
  928. else if (!strcmp(hwaccel, "auto"))
  929. ist->hwaccel_id = HWACCEL_AUTO;
  930. else {
  931. enum AVHWDeviceType type = av_hwdevice_find_type_by_name(hwaccel);
  932. if (type != AV_HWDEVICE_TYPE_NONE) {
  933. ist->hwaccel_id = HWACCEL_GENERIC;
  934. ist->hwaccel_device_type = type;
  935. }
  936. if (!ist->hwaccel_id) {
  937. av_log(ist, AV_LOG_FATAL, "Unrecognized hwaccel: %s.\n",
  938. hwaccel);
  939. av_log(ist, AV_LOG_FATAL, "Supported hwaccels: ");
  940. type = AV_HWDEVICE_TYPE_NONE;
  941. while ((type = av_hwdevice_iterate_types(type)) !=
  942. AV_HWDEVICE_TYPE_NONE)
  943. av_log(ist, AV_LOG_FATAL, "%s ",
  944. av_hwdevice_get_type_name(type));
  945. av_log(ist, AV_LOG_FATAL, "\n");
  946. return AVERROR(EINVAL);
  947. }
  948. }
  949. }
  950. MATCH_PER_STREAM_OPT(hwaccel_devices, str, hwaccel_device, ic, st);
  951. if (hwaccel_device) {
  952. ist->hwaccel_device = av_strdup(hwaccel_device);
  953. if (!ist->hwaccel_device)
  954. return AVERROR(ENOMEM);
  955. }
  956. }
  957. ist->dec = choose_decoder(o, ic, st, ist->hwaccel_id, ist->hwaccel_device_type);
  958. ist->decoder_opts = filter_codec_opts(o->g->codec_opts, ist->st->codecpar->codec_id, ic, st, ist->dec);
  959. ist->reinit_filters = -1;
  960. MATCH_PER_STREAM_OPT(reinit_filters, i, ist->reinit_filters, ic, st);
  961. MATCH_PER_STREAM_OPT(discard, str, discard_str, ic, st);
  962. ist->user_set_discard = AVDISCARD_NONE;
  963. if ((o->video_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) ||
  964. (o->audio_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) ||
  965. (o->subtitle_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) ||
  966. (o->data_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_DATA))
  967. ist->user_set_discard = AVDISCARD_ALL;
  968. if (discard_str) {
  969. ret = av_opt_eval_int(&cc, discard_opt, discard_str, &ist->user_set_discard);
  970. if (ret < 0) {
  971. av_log(ist, AV_LOG_ERROR, "Error parsing discard %s.\n", discard_str);
  972. return ret;
  973. }
  974. }
  975. ist->dec_ctx = avcodec_alloc_context3(ist->dec);
  976. if (!ist->dec_ctx)
  977. return AVERROR(ENOMEM);
  978. ret = avcodec_parameters_to_context(ist->dec_ctx, par);
  979. if (ret < 0) {
  980. av_log(ist, AV_LOG_ERROR, "Error initializing the decoder context.\n");
  981. return ret;
  982. }
  983. if (o->bitexact)
  984. ist->dec_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
  985. switch (par->codec_type) {
  986. case AVMEDIA_TYPE_VIDEO:
  987. MATCH_PER_STREAM_OPT(frame_rates, str, framerate, ic, st);
  988. if (framerate) {
  989. ret = av_parse_video_rate(&ist->framerate, framerate);
  990. if (ret < 0) {
  991. av_log(ist, AV_LOG_ERROR, "Error parsing framerate %s.\n",
  992. framerate);
  993. return ret;
  994. }
  995. }
  996. ist->top_field_first = -1;
  997. MATCH_PER_STREAM_OPT(top_field_first, i, ist->top_field_first, ic, st);
  998. ist->framerate_guessed = av_guess_frame_rate(ic, st, NULL);
  999. break;
  1000. case AVMEDIA_TYPE_AUDIO: {
  1001. int guess_layout_max = INT_MAX;
  1002. MATCH_PER_STREAM_OPT(guess_layout_max, i, guess_layout_max, ic, st);
  1003. guess_input_channel_layout(ist, guess_layout_max);
  1004. break;
  1005. }
  1006. case AVMEDIA_TYPE_DATA:
  1007. case AVMEDIA_TYPE_SUBTITLE: {
  1008. char *canvas_size = NULL;
  1009. MATCH_PER_STREAM_OPT(fix_sub_duration, i, ist->fix_sub_duration, ic, st);
  1010. MATCH_PER_STREAM_OPT(canvas_sizes, str, canvas_size, ic, st);
  1011. if (canvas_size) {
  1012. ret = av_parse_video_size(&ist->dec_ctx->width, &ist->dec_ctx->height,
  1013. canvas_size);
  1014. if (ret < 0) {
  1015. av_log(ist, AV_LOG_FATAL, "Invalid canvas size: %s.\n", canvas_size);
  1016. return ret;
  1017. }
  1018. }
  1019. /* Compute the size of the canvas for the subtitles stream.
  1020. If the subtitles codecpar has set a size, use it. Otherwise use the
  1021. maximum dimensions of the video streams in the same file. */
  1022. ist->sub2video.w = ist->dec_ctx->width;
  1023. ist->sub2video.h = ist->dec_ctx->height;
  1024. if (!(ist->sub2video.w && ist->sub2video.h)) {
  1025. for (int j = 0; j < ic->nb_streams; j++) {
  1026. AVCodecParameters *par1 = ic->streams[j]->codecpar;
  1027. if (par1->codec_type == AVMEDIA_TYPE_VIDEO) {
  1028. ist->sub2video.w = FFMAX(ist->sub2video.w, par1->width);
  1029. ist->sub2video.h = FFMAX(ist->sub2video.h, par1->height);
  1030. }
  1031. }
  1032. }
  1033. if (!(ist->sub2video.w && ist->sub2video.h)) {
  1034. ist->sub2video.w = FFMAX(ist->sub2video.w, 720);
  1035. ist->sub2video.h = FFMAX(ist->sub2video.h, 576);
  1036. }
  1037. break;
  1038. }
  1039. case AVMEDIA_TYPE_ATTACHMENT:
  1040. case AVMEDIA_TYPE_UNKNOWN:
  1041. break;
  1042. default:
  1043. abort();
  1044. }
  1045. ist->par = avcodec_parameters_alloc();
  1046. if (!ist->par)
  1047. return AVERROR(ENOMEM);
  1048. ret = avcodec_parameters_from_context(ist->par, ist->dec_ctx);
  1049. if (ret < 0) {
  1050. av_log(ist, AV_LOG_ERROR, "Error initializing the decoder context.\n");
  1051. return ret;
  1052. }
  1053. ist->codec_desc = avcodec_descriptor_get(ist->par->codec_id);
  1054. return 0;
  1055. }
  1056. static int dump_attachment(InputStream *ist, const char *filename)
  1057. {
  1058. AVStream *st = ist->st;
  1059. int ret;
  1060. AVIOContext *out = NULL;
  1061. const AVDictionaryEntry *e;
  1062. if (!st->codecpar->extradata_size) {
  1063. av_log(ist, AV_LOG_WARNING, "No extradata to dump.\n");
  1064. return 0;
  1065. }
  1066. if (!*filename && (e = av_dict_get(st->metadata, "filename", NULL, 0)))
  1067. filename = e->value;
  1068. if (!*filename) {
  1069. av_log(ist, AV_LOG_FATAL, "No filename specified and no 'filename' tag");
  1070. return AVERROR(EINVAL);
  1071. }
  1072. ret = assert_file_overwrite(filename);
  1073. if (ret < 0)
  1074. return ret;
  1075. if ((ret = avio_open2(&out, filename, AVIO_FLAG_WRITE, &int_cb, NULL)) < 0) {
  1076. av_log(ist, AV_LOG_FATAL, "Could not open file %s for writing.\n",
  1077. filename);
  1078. return ret;
  1079. }
  1080. avio_write(out, st->codecpar->extradata, st->codecpar->extradata_size);
  1081. ret = avio_close(out);
  1082. if (ret >= 0)
  1083. av_log(ist, AV_LOG_INFO, "Wrote attachment (%d bytes) to '%s'\n",
  1084. st->codecpar->extradata_size, filename);
  1085. return ret;
  1086. }
  1087. static const char *input_file_item_name(void *obj)
  1088. {
  1089. const Demuxer *d = obj;
  1090. return d->log_name;
  1091. }
  1092. static const AVClass input_file_class = {
  1093. .class_name = "InputFile",
  1094. .version = LIBAVUTIL_VERSION_INT,
  1095. .item_name = input_file_item_name,
  1096. .category = AV_CLASS_CATEGORY_DEMUXER,
  1097. };
  1098. static Demuxer *demux_alloc(void)
  1099. {
  1100. Demuxer *d = allocate_array_elem(&input_files, sizeof(*d), &nb_input_files);
  1101. if (!d)
  1102. return NULL;
  1103. d->f.class = &input_file_class;
  1104. d->f.index = nb_input_files - 1;
  1105. snprintf(d->log_name, sizeof(d->log_name), "in#%d", d->f.index);
  1106. return d;
  1107. }
  1108. int ifile_open(const OptionsContext *o, const char *filename)
  1109. {
  1110. Demuxer *d;
  1111. InputFile *f;
  1112. AVFormatContext *ic;
  1113. const AVInputFormat *file_iformat = NULL;
  1114. int err, i, ret;
  1115. int64_t timestamp;
  1116. AVDictionary *unused_opts = NULL;
  1117. const AVDictionaryEntry *e = NULL;
  1118. char * video_codec_name = NULL;
  1119. char * audio_codec_name = NULL;
  1120. char *subtitle_codec_name = NULL;
  1121. char * data_codec_name = NULL;
  1122. int scan_all_pmts_set = 0;
  1123. int64_t start_time = o->start_time;
  1124. int64_t start_time_eof = o->start_time_eof;
  1125. int64_t stop_time = o->stop_time;
  1126. int64_t recording_time = o->recording_time;
  1127. d = demux_alloc();
  1128. if (!d)
  1129. return AVERROR(ENOMEM);
  1130. f = &d->f;
  1131. if (stop_time != INT64_MAX && recording_time != INT64_MAX) {
  1132. stop_time = INT64_MAX;
  1133. av_log(d, AV_LOG_WARNING, "-t and -to cannot be used together; using -t.\n");
  1134. }
  1135. if (stop_time != INT64_MAX && recording_time == INT64_MAX) {
  1136. int64_t start = start_time == AV_NOPTS_VALUE ? 0 : start_time;
  1137. if (stop_time <= start) {
  1138. av_log(d, AV_LOG_ERROR, "-to value smaller than -ss; aborting.\n");
  1139. return AVERROR(EINVAL);
  1140. } else {
  1141. recording_time = stop_time - start;
  1142. }
  1143. }
  1144. if (o->format) {
  1145. if (!(file_iformat = av_find_input_format(o->format))) {
  1146. av_log(d, AV_LOG_FATAL, "Unknown input format: '%s'\n", o->format);
  1147. return AVERROR(EINVAL);
  1148. }
  1149. }
  1150. if (!strcmp(filename, "-"))
  1151. filename = "fd:";
  1152. stdin_interaction &= strncmp(filename, "pipe:", 5) &&
  1153. strcmp(filename, "fd:") &&
  1154. strcmp(filename, "/dev/stdin");
  1155. /* get default parameters from command line */
  1156. ic = avformat_alloc_context();
  1157. if (!ic)
  1158. return AVERROR(ENOMEM);
  1159. if (o->nb_audio_sample_rate) {
  1160. av_dict_set_int(&o->g->format_opts, "sample_rate", o->audio_sample_rate[o->nb_audio_sample_rate - 1].u.i, 0);
  1161. }
  1162. if (o->nb_audio_channels) {
  1163. const AVClass *priv_class;
  1164. if (file_iformat && (priv_class = file_iformat->priv_class) &&
  1165. av_opt_find(&priv_class, "ch_layout", NULL, 0,
  1166. AV_OPT_SEARCH_FAKE_OBJ)) {
  1167. char buf[32];
  1168. snprintf(buf, sizeof(buf), "%dC", o->audio_channels[o->nb_audio_channels - 1].u.i);
  1169. av_dict_set(&o->g->format_opts, "ch_layout", buf, 0);
  1170. }
  1171. }
  1172. if (o->nb_audio_ch_layouts) {
  1173. const AVClass *priv_class;
  1174. if (file_iformat && (priv_class = file_iformat->priv_class) &&
  1175. av_opt_find(&priv_class, "ch_layout", NULL, 0,
  1176. AV_OPT_SEARCH_FAKE_OBJ)) {
  1177. av_dict_set(&o->g->format_opts, "ch_layout", o->audio_ch_layouts[o->nb_audio_ch_layouts - 1].u.str, 0);
  1178. }
  1179. }
  1180. if (o->nb_frame_rates) {
  1181. const AVClass *priv_class;
  1182. /* set the format-level framerate option;
  1183. * this is important for video grabbers, e.g. x11 */
  1184. if (file_iformat && (priv_class = file_iformat->priv_class) &&
  1185. av_opt_find(&priv_class, "framerate", NULL, 0,
  1186. AV_OPT_SEARCH_FAKE_OBJ)) {
  1187. av_dict_set(&o->g->format_opts, "framerate",
  1188. o->frame_rates[o->nb_frame_rates - 1].u.str, 0);
  1189. }
  1190. }
  1191. if (o->nb_frame_sizes) {
  1192. av_dict_set(&o->g->format_opts, "video_size", o->frame_sizes[o->nb_frame_sizes - 1].u.str, 0);
  1193. }
  1194. if (o->nb_frame_pix_fmts)
  1195. av_dict_set(&o->g->format_opts, "pixel_format", o->frame_pix_fmts[o->nb_frame_pix_fmts - 1].u.str, 0);
  1196. MATCH_PER_TYPE_OPT(codec_names, str, video_codec_name, ic, "v");
  1197. MATCH_PER_TYPE_OPT(codec_names, str, audio_codec_name, ic, "a");
  1198. MATCH_PER_TYPE_OPT(codec_names, str, subtitle_codec_name, ic, "s");
  1199. MATCH_PER_TYPE_OPT(codec_names, str, data_codec_name, ic, "d");
  1200. if (video_codec_name)
  1201. ic->video_codec = find_codec_or_die(NULL, video_codec_name , AVMEDIA_TYPE_VIDEO , 0);
  1202. if (audio_codec_name)
  1203. ic->audio_codec = find_codec_or_die(NULL, audio_codec_name , AVMEDIA_TYPE_AUDIO , 0);
  1204. if (subtitle_codec_name)
  1205. ic->subtitle_codec = find_codec_or_die(NULL, subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, 0);
  1206. if (data_codec_name)
  1207. ic->data_codec = find_codec_or_die(NULL, data_codec_name , AVMEDIA_TYPE_DATA , 0);
  1208. ic->video_codec_id = video_codec_name ? ic->video_codec->id : AV_CODEC_ID_NONE;
  1209. ic->audio_codec_id = audio_codec_name ? ic->audio_codec->id : AV_CODEC_ID_NONE;
  1210. ic->subtitle_codec_id = subtitle_codec_name ? ic->subtitle_codec->id : AV_CODEC_ID_NONE;
  1211. ic->data_codec_id = data_codec_name ? ic->data_codec->id : AV_CODEC_ID_NONE;
  1212. ic->flags |= AVFMT_FLAG_NONBLOCK;
  1213. if (o->bitexact)
  1214. ic->flags |= AVFMT_FLAG_BITEXACT;
  1215. ic->interrupt_callback = int_cb;
  1216. if (!av_dict_get(o->g->format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE)) {
  1217. av_dict_set(&o->g->format_opts, "scan_all_pmts", "1", AV_DICT_DONT_OVERWRITE);
  1218. scan_all_pmts_set = 1;
  1219. }
  1220. /* open the input file with generic avformat function */
  1221. err = avformat_open_input(&ic, filename, file_iformat, &o->g->format_opts);
  1222. if (err < 0) {
  1223. av_log(d, AV_LOG_ERROR,
  1224. "Error opening input: %s\n", av_err2str(err));
  1225. if (err == AVERROR_PROTOCOL_NOT_FOUND)
  1226. av_log(d, AV_LOG_ERROR, "Did you mean file:%s?\n", filename);
  1227. return err;
  1228. }
  1229. av_strlcat(d->log_name, "/", sizeof(d->log_name));
  1230. av_strlcat(d->log_name, ic->iformat->name, sizeof(d->log_name));
  1231. if (scan_all_pmts_set)
  1232. av_dict_set(&o->g->format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE);
  1233. remove_avoptions(&o->g->format_opts, o->g->codec_opts);
  1234. assert_avoptions(o->g->format_opts);
  1235. /* apply forced codec ids */
  1236. for (i = 0; i < ic->nb_streams; i++)
  1237. choose_decoder(o, ic, ic->streams[i], HWACCEL_NONE, AV_HWDEVICE_TYPE_NONE);
  1238. if (o->find_stream_info) {
  1239. AVDictionary **opts;
  1240. int orig_nb_streams = ic->nb_streams;
  1241. ret = setup_find_stream_info_opts(ic, o->g->codec_opts, &opts);
  1242. if (ret < 0)
  1243. return ret;
  1244. /* If not enough info to get the stream parameters, we decode the
  1245. first frames to get it. (used in mpeg case for example) */
  1246. ret = avformat_find_stream_info(ic, opts);
  1247. for (i = 0; i < orig_nb_streams; i++)
  1248. av_dict_free(&opts[i]);
  1249. av_freep(&opts);
  1250. if (ret < 0) {
  1251. av_log(d, AV_LOG_FATAL, "could not find codec parameters\n");
  1252. if (ic->nb_streams == 0) {
  1253. avformat_close_input(&ic);
  1254. return ret;
  1255. }
  1256. }
  1257. }
  1258. if (start_time != AV_NOPTS_VALUE && start_time_eof != AV_NOPTS_VALUE) {
  1259. av_log(d, AV_LOG_WARNING, "Cannot use -ss and -sseof both, using -ss\n");
  1260. start_time_eof = AV_NOPTS_VALUE;
  1261. }
  1262. if (start_time_eof != AV_NOPTS_VALUE) {
  1263. if (start_time_eof >= 0) {
  1264. av_log(d, AV_LOG_ERROR, "-sseof value must be negative; aborting\n");
  1265. return AVERROR(EINVAL);
  1266. }
  1267. if (ic->duration > 0) {
  1268. start_time = start_time_eof + ic->duration;
  1269. if (start_time < 0) {
  1270. av_log(d, AV_LOG_WARNING, "-sseof value seeks to before start of file; ignored\n");
  1271. start_time = AV_NOPTS_VALUE;
  1272. }
  1273. } else
  1274. av_log(d, AV_LOG_WARNING, "Cannot use -sseof, file duration not known\n");
  1275. }
  1276. timestamp = (start_time == AV_NOPTS_VALUE) ? 0 : start_time;
  1277. /* add the stream start time */
  1278. if (!o->seek_timestamp && ic->start_time != AV_NOPTS_VALUE)
  1279. timestamp += ic->start_time;
  1280. /* if seeking requested, we execute it */
  1281. if (start_time != AV_NOPTS_VALUE) {
  1282. int64_t seek_timestamp = timestamp;
  1283. if (!(ic->iformat->flags & AVFMT_SEEK_TO_PTS)) {
  1284. int dts_heuristic = 0;
  1285. for (i=0; i<ic->nb_streams; i++) {
  1286. const AVCodecParameters *par = ic->streams[i]->codecpar;
  1287. if (par->video_delay) {
  1288. dts_heuristic = 1;
  1289. break;
  1290. }
  1291. }
  1292. if (dts_heuristic) {
  1293. seek_timestamp -= 3*AV_TIME_BASE / 23;
  1294. }
  1295. }
  1296. ret = avformat_seek_file(ic, -1, INT64_MIN, seek_timestamp, seek_timestamp, 0);
  1297. if (ret < 0) {
  1298. av_log(d, AV_LOG_WARNING, "could not seek to position %0.3f\n",
  1299. (double)timestamp / AV_TIME_BASE);
  1300. }
  1301. }
  1302. f->ctx = ic;
  1303. f->start_time = start_time;
  1304. f->recording_time = recording_time;
  1305. f->input_sync_ref = o->input_sync_ref;
  1306. f->input_ts_offset = o->input_ts_offset;
  1307. f->ts_offset = o->input_ts_offset - (copy_ts ? (start_at_zero && ic->start_time != AV_NOPTS_VALUE ? ic->start_time : 0) : timestamp);
  1308. f->accurate_seek = o->accurate_seek;
  1309. d->loop = o->loop;
  1310. d->duration = 0;
  1311. d->time_base = (AVRational){ 1, 1 };
  1312. d->nb_streams_warn = ic->nb_streams;
  1313. f->format_nots = !!(ic->iformat->flags & AVFMT_NOTIMESTAMPS);
  1314. f->readrate = o->readrate ? o->readrate : 0.0;
  1315. if (f->readrate < 0.0f) {
  1316. av_log(d, AV_LOG_ERROR, "Option -readrate is %0.3f; it must be non-negative.\n", f->readrate);
  1317. return AVERROR(EINVAL);
  1318. }
  1319. if (o->rate_emu) {
  1320. if (f->readrate) {
  1321. av_log(d, AV_LOG_WARNING, "Both -readrate and -re set. Using -readrate %0.3f.\n", f->readrate);
  1322. } else
  1323. f->readrate = 1.0f;
  1324. }
  1325. if (f->readrate) {
  1326. d->readrate_initial_burst = o->readrate_initial_burst ? o->readrate_initial_burst : 0.5;
  1327. if (d->readrate_initial_burst < 0.0) {
  1328. av_log(d, AV_LOG_ERROR,
  1329. "Option -readrate_initial_burst is %0.3f; it must be non-negative.\n",
  1330. d->readrate_initial_burst);
  1331. return AVERROR(EINVAL);
  1332. }
  1333. } else if (o->readrate_initial_burst) {
  1334. av_log(d, AV_LOG_WARNING, "Option -readrate_initial_burst ignored "
  1335. "since neither -readrate nor -re were given\n");
  1336. }
  1337. d->thread_queue_size = o->thread_queue_size;
  1338. /* Add all the streams from the given input file to the demuxer */
  1339. for (int i = 0; i < ic->nb_streams; i++) {
  1340. ret = ist_add(o, d, ic->streams[i]);
  1341. if (ret < 0)
  1342. return ret;
  1343. }
  1344. /* dump the file content */
  1345. av_dump_format(ic, f->index, filename, 0);
  1346. /* check if all codec options have been used */
  1347. unused_opts = strip_specifiers(o->g->codec_opts);
  1348. for (i = 0; i < f->nb_streams; i++) {
  1349. e = NULL;
  1350. while ((e = av_dict_iterate(f->streams[i]->decoder_opts, e)))
  1351. av_dict_set(&unused_opts, e->key, NULL, 0);
  1352. }
  1353. e = NULL;
  1354. while ((e = av_dict_iterate(unused_opts, e))) {
  1355. const AVClass *class = avcodec_get_class();
  1356. const AVOption *option = av_opt_find(&class, e->key, NULL, 0,
  1357. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
  1358. const AVClass *fclass = avformat_get_class();
  1359. const AVOption *foption = av_opt_find(&fclass, e->key, NULL, 0,
  1360. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
  1361. if (!option || foption)
  1362. continue;
  1363. if (!(option->flags & AV_OPT_FLAG_DECODING_PARAM)) {
  1364. av_log(d, AV_LOG_ERROR, "Codec AVOption %s (%s) is not a decoding "
  1365. "option.\n", e->key, option->help ? option->help : "");
  1366. return AVERROR(EINVAL);
  1367. }
  1368. av_log(d, AV_LOG_WARNING, "Codec AVOption %s (%s) has not been used "
  1369. "for any stream. The most likely reason is either wrong type "
  1370. "(e.g. a video option with no video streams) or that it is a "
  1371. "private option of some decoder which was not actually used "
  1372. "for any stream.\n", e->key, option->help ? option->help : "");
  1373. }
  1374. av_dict_free(&unused_opts);
  1375. for (i = 0; i < o->nb_dump_attachment; i++) {
  1376. int j;
  1377. for (j = 0; j < f->nb_streams; j++) {
  1378. InputStream *ist = f->streams[j];
  1379. if (check_stream_specifier(ic, ist->st, o->dump_attachment[i].specifier) == 1) {
  1380. ret = dump_attachment(ist, o->dump_attachment[i].u.str);
  1381. if (ret < 0)
  1382. return ret;
  1383. }
  1384. }
  1385. }
  1386. return 0;
  1387. }