ffmpeg_demux.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685
  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. if (debug_ts) {
  406. 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",
  407. f->index, pkt->stream_index,
  408. av_get_media_type_string(ist->par->codec_type),
  409. av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &pkt->time_base),
  410. av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &pkt->time_base),
  411. av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &pkt->time_base),
  412. av_ts2str(input_files[ist->file_index]->ts_offset),
  413. av_ts2timestr(input_files[ist->file_index]->ts_offset, &AV_TIME_BASE_Q));
  414. }
  415. msg->pkt = pkt;
  416. pkt = NULL;
  417. fail:
  418. av_packet_free(&pkt);
  419. return ret;
  420. }
  421. static void readrate_sleep(Demuxer *d)
  422. {
  423. InputFile *f = &d->f;
  424. int64_t file_start = copy_ts * (
  425. (f->start_time_effective != AV_NOPTS_VALUE ? f->start_time_effective * !start_at_zero : 0) +
  426. (f->start_time != AV_NOPTS_VALUE ? f->start_time : 0)
  427. );
  428. int64_t burst_until = AV_TIME_BASE * d->readrate_initial_burst;
  429. for (int i = 0; i < f->nb_streams; i++) {
  430. InputStream *ist = f->streams[i];
  431. DemuxStream *ds = ds_from_ist(ist);
  432. int64_t stream_ts_offset, pts, now;
  433. stream_ts_offset = FFMAX(ds->first_dts != AV_NOPTS_VALUE ? ds->first_dts : 0, file_start);
  434. pts = av_rescale(ds->dts, 1000000, AV_TIME_BASE);
  435. now = (av_gettime_relative() - d->wallclock_start) * f->readrate + stream_ts_offset;
  436. if (pts - burst_until > now)
  437. av_usleep(pts - burst_until - now);
  438. }
  439. }
  440. static void discard_unused_programs(InputFile *ifile)
  441. {
  442. for (int j = 0; j < ifile->ctx->nb_programs; j++) {
  443. AVProgram *p = ifile->ctx->programs[j];
  444. int discard = AVDISCARD_ALL;
  445. for (int k = 0; k < p->nb_stream_indexes; k++)
  446. if (!ifile->streams[p->stream_index[k]]->discard) {
  447. discard = AVDISCARD_DEFAULT;
  448. break;
  449. }
  450. p->discard = discard;
  451. }
  452. }
  453. static void thread_set_name(InputFile *f)
  454. {
  455. char name[16];
  456. snprintf(name, sizeof(name), "dmx%d:%s", f->index, f->ctx->iformat->name);
  457. ff_thread_setname(name);
  458. }
  459. static void *input_thread(void *arg)
  460. {
  461. Demuxer *d = arg;
  462. InputFile *f = &d->f;
  463. AVPacket *pkt;
  464. unsigned flags = d->non_blocking ? AV_THREAD_MESSAGE_NONBLOCK : 0;
  465. int ret = 0;
  466. pkt = av_packet_alloc();
  467. if (!pkt) {
  468. ret = AVERROR(ENOMEM);
  469. goto finish;
  470. }
  471. thread_set_name(f);
  472. discard_unused_programs(f);
  473. d->wallclock_start = av_gettime_relative();
  474. while (1) {
  475. DemuxMsg msg = { NULL };
  476. ret = av_read_frame(f->ctx, pkt);
  477. if (ret == AVERROR(EAGAIN)) {
  478. av_usleep(10000);
  479. continue;
  480. }
  481. if (ret < 0) {
  482. if (d->loop) {
  483. /* signal looping to the consumer thread */
  484. msg.looping = 1;
  485. ret = av_thread_message_queue_send(d->in_thread_queue, &msg, 0);
  486. if (ret >= 0)
  487. ret = seek_to_start(d);
  488. if (ret >= 0)
  489. continue;
  490. /* fallthrough to the error path */
  491. }
  492. if (ret == AVERROR_EOF)
  493. av_log(d, AV_LOG_VERBOSE, "EOF while reading input\n");
  494. else
  495. av_log(d, AV_LOG_ERROR, "Error during demuxing: %s\n",
  496. av_err2str(ret));
  497. break;
  498. }
  499. if (do_pkt_dump) {
  500. av_pkt_dump_log2(NULL, AV_LOG_INFO, pkt, do_hex_dump,
  501. f->ctx->streams[pkt->stream_index]);
  502. }
  503. /* the following test is needed in case new streams appear
  504. dynamically in stream : we ignore them */
  505. if (pkt->stream_index >= f->nb_streams ||
  506. f->streams[pkt->stream_index]->discard) {
  507. report_new_stream(d, pkt);
  508. av_packet_unref(pkt);
  509. continue;
  510. }
  511. if (pkt->flags & AV_PKT_FLAG_CORRUPT) {
  512. av_log(d, exit_on_error ? AV_LOG_FATAL : AV_LOG_WARNING,
  513. "corrupt input packet in stream %d\n",
  514. pkt->stream_index);
  515. if (exit_on_error) {
  516. av_packet_unref(pkt);
  517. ret = AVERROR_INVALIDDATA;
  518. break;
  519. }
  520. }
  521. ret = input_packet_process(d, &msg, pkt);
  522. if (ret < 0)
  523. break;
  524. if (f->readrate)
  525. readrate_sleep(d);
  526. ret = av_thread_message_queue_send(d->in_thread_queue, &msg, flags);
  527. if (flags && ret == AVERROR(EAGAIN)) {
  528. flags = 0;
  529. ret = av_thread_message_queue_send(d->in_thread_queue, &msg, flags);
  530. av_log(f, AV_LOG_WARNING,
  531. "Thread message queue blocking; consider raising the "
  532. "thread_queue_size option (current value: %d)\n",
  533. d->thread_queue_size);
  534. }
  535. if (ret < 0) {
  536. if (ret != AVERROR_EOF)
  537. av_log(f, AV_LOG_ERROR,
  538. "Unable to send packet to main thread: %s\n",
  539. av_err2str(ret));
  540. av_packet_free(&msg.pkt);
  541. break;
  542. }
  543. }
  544. finish:
  545. av_assert0(ret < 0);
  546. av_thread_message_queue_set_err_recv(d->in_thread_queue, ret);
  547. av_packet_free(&pkt);
  548. av_log(d, AV_LOG_VERBOSE, "Terminating demuxer thread\n");
  549. return NULL;
  550. }
  551. static void thread_stop(Demuxer *d)
  552. {
  553. InputFile *f = &d->f;
  554. DemuxMsg msg;
  555. if (!d->in_thread_queue)
  556. return;
  557. av_thread_message_queue_set_err_send(d->in_thread_queue, AVERROR_EOF);
  558. while (av_thread_message_queue_recv(d->in_thread_queue, &msg, 0) >= 0)
  559. av_packet_free(&msg.pkt);
  560. pthread_join(d->thread, NULL);
  561. av_thread_message_queue_free(&d->in_thread_queue);
  562. av_thread_message_queue_free(&f->audio_duration_queue);
  563. }
  564. static int thread_start(Demuxer *d)
  565. {
  566. int ret;
  567. InputFile *f = &d->f;
  568. if (d->thread_queue_size <= 0)
  569. d->thread_queue_size = (nb_input_files > 1 ? 8 : 1);
  570. if (nb_input_files > 1 &&
  571. (f->ctx->pb ? !f->ctx->pb->seekable :
  572. strcmp(f->ctx->iformat->name, "lavfi")))
  573. d->non_blocking = 1;
  574. ret = av_thread_message_queue_alloc(&d->in_thread_queue,
  575. d->thread_queue_size, sizeof(DemuxMsg));
  576. if (ret < 0)
  577. return ret;
  578. if (d->loop) {
  579. int nb_audio_dec = 0;
  580. for (int i = 0; i < f->nb_streams; i++) {
  581. InputStream *ist = f->streams[i];
  582. nb_audio_dec += !!(ist->decoding_needed &&
  583. ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO);
  584. }
  585. if (nb_audio_dec) {
  586. ret = av_thread_message_queue_alloc(&f->audio_duration_queue,
  587. nb_audio_dec, sizeof(LastFrameDuration));
  588. if (ret < 0)
  589. goto fail;
  590. f->audio_duration_queue_size = nb_audio_dec;
  591. }
  592. }
  593. if ((ret = pthread_create(&d->thread, NULL, input_thread, d))) {
  594. av_log(d, AV_LOG_ERROR, "pthread_create failed: %s. Try to increase `ulimit -v` or decrease `ulimit -s`.\n", strerror(ret));
  595. ret = AVERROR(ret);
  596. goto fail;
  597. }
  598. d->read_started = 1;
  599. return 0;
  600. fail:
  601. av_thread_message_queue_free(&d->in_thread_queue);
  602. return ret;
  603. }
  604. int ifile_get_packet(InputFile *f, AVPacket **pkt)
  605. {
  606. Demuxer *d = demuxer_from_ifile(f);
  607. DemuxMsg msg;
  608. int ret;
  609. if (!d->in_thread_queue) {
  610. ret = thread_start(d);
  611. if (ret < 0)
  612. return ret;
  613. }
  614. ret = av_thread_message_queue_recv(d->in_thread_queue, &msg,
  615. d->non_blocking ?
  616. AV_THREAD_MESSAGE_NONBLOCK : 0);
  617. if (ret < 0)
  618. return ret;
  619. if (msg.looping)
  620. return 1;
  621. *pkt = msg.pkt;
  622. return 0;
  623. }
  624. static void demux_final_stats(Demuxer *d)
  625. {
  626. InputFile *f = &d->f;
  627. uint64_t total_packets = 0, total_size = 0;
  628. av_log(f, AV_LOG_VERBOSE, "Input file #%d (%s):\n",
  629. f->index, f->ctx->url);
  630. for (int j = 0; j < f->nb_streams; j++) {
  631. InputStream *ist = f->streams[j];
  632. DemuxStream *ds = ds_from_ist(ist);
  633. enum AVMediaType type = ist->par->codec_type;
  634. if (ist->discard || type == AVMEDIA_TYPE_ATTACHMENT)
  635. continue;
  636. total_size += ds->data_size;
  637. total_packets += ds->nb_packets;
  638. av_log(f, AV_LOG_VERBOSE, " Input stream #%d:%d (%s): ",
  639. f->index, j, av_get_media_type_string(type));
  640. av_log(f, AV_LOG_VERBOSE, "%"PRIu64" packets read (%"PRIu64" bytes); ",
  641. ds->nb_packets, ds->data_size);
  642. if (ist->decoding_needed) {
  643. av_log(f, AV_LOG_VERBOSE,
  644. "%"PRIu64" frames decoded; %"PRIu64" decode errors",
  645. ist->frames_decoded, ist->decode_errors);
  646. if (type == AVMEDIA_TYPE_AUDIO)
  647. av_log(f, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ist->samples_decoded);
  648. av_log(f, AV_LOG_VERBOSE, "; ");
  649. }
  650. av_log(f, AV_LOG_VERBOSE, "\n");
  651. }
  652. av_log(f, AV_LOG_VERBOSE, " Total: %"PRIu64" packets (%"PRIu64" bytes) demuxed\n",
  653. total_packets, total_size);
  654. }
  655. static void ist_free(InputStream **pist)
  656. {
  657. InputStream *ist = *pist;
  658. if (!ist)
  659. return;
  660. dec_free(&ist->decoder);
  661. av_dict_free(&ist->decoder_opts);
  662. av_freep(&ist->filters);
  663. av_freep(&ist->outputs);
  664. av_freep(&ist->hwaccel_device);
  665. avcodec_free_context(&ist->dec_ctx);
  666. avcodec_parameters_free(&ist->par);
  667. av_freep(pist);
  668. }
  669. void ifile_close(InputFile **pf)
  670. {
  671. InputFile *f = *pf;
  672. Demuxer *d = demuxer_from_ifile(f);
  673. if (!f)
  674. return;
  675. thread_stop(d);
  676. if (d->read_started)
  677. demux_final_stats(d);
  678. for (int i = 0; i < f->nb_streams; i++)
  679. ist_free(&f->streams[i]);
  680. av_freep(&f->streams);
  681. avformat_close_input(&f->ctx);
  682. av_freep(pf);
  683. }
  684. static int ist_use(InputStream *ist, int decoding_needed)
  685. {
  686. DemuxStream *ds = ds_from_ist(ist);
  687. if (ist->user_set_discard == AVDISCARD_ALL) {
  688. av_log(ist, AV_LOG_ERROR, "Cannot %s a disabled input stream\n",
  689. decoding_needed ? "decode" : "streamcopy");
  690. return AVERROR(EINVAL);
  691. }
  692. ist->discard = 0;
  693. ist->st->discard = ist->user_set_discard;
  694. ist->decoding_needed |= decoding_needed;
  695. ds->streamcopy_needed |= !decoding_needed;
  696. if (decoding_needed && !avcodec_is_open(ist->dec_ctx)) {
  697. int ret = dec_open(ist);
  698. if (ret < 0)
  699. return ret;
  700. }
  701. return 0;
  702. }
  703. int ist_output_add(InputStream *ist, OutputStream *ost)
  704. {
  705. int ret;
  706. ret = ist_use(ist, ost->enc ? DECODING_FOR_OST : 0);
  707. if (ret < 0)
  708. return ret;
  709. ret = GROW_ARRAY(ist->outputs, ist->nb_outputs);
  710. if (ret < 0)
  711. return ret;
  712. ist->outputs[ist->nb_outputs - 1] = ost;
  713. return 0;
  714. }
  715. int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple)
  716. {
  717. int ret;
  718. ret = ist_use(ist, is_simple ? DECODING_FOR_OST : DECODING_FOR_FILTER);
  719. if (ret < 0)
  720. return ret;
  721. ret = GROW_ARRAY(ist->filters, ist->nb_filters);
  722. if (ret < 0)
  723. return ret;
  724. ist->filters[ist->nb_filters - 1] = ifilter;
  725. // initialize fallback parameters for filtering
  726. ret = ifilter_parameters_from_dec(ifilter, ist->dec_ctx);
  727. if (ret < 0)
  728. return ret;
  729. return 0;
  730. }
  731. static int choose_decoder(const OptionsContext *o, AVFormatContext *s, AVStream *st,
  732. enum HWAccelID hwaccel_id, enum AVHWDeviceType hwaccel_device_type,
  733. const AVCodec **pcodec)
  734. {
  735. char *codec_name = NULL;
  736. MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, st);
  737. if (codec_name) {
  738. int ret = find_codec(NULL, codec_name, st->codecpar->codec_type, 0, pcodec);
  739. if (ret < 0)
  740. return ret;
  741. st->codecpar->codec_id = (*pcodec)->id;
  742. if (recast_media && st->codecpar->codec_type != (*pcodec)->type)
  743. st->codecpar->codec_type = (*pcodec)->type;
  744. return 0;
  745. } else {
  746. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
  747. hwaccel_id == HWACCEL_GENERIC &&
  748. hwaccel_device_type != AV_HWDEVICE_TYPE_NONE) {
  749. const AVCodec *c;
  750. void *i = NULL;
  751. while ((c = av_codec_iterate(&i))) {
  752. const AVCodecHWConfig *config;
  753. if (c->id != st->codecpar->codec_id ||
  754. !av_codec_is_decoder(c))
  755. continue;
  756. for (int j = 0; config = avcodec_get_hw_config(c, j); j++) {
  757. if (config->device_type == hwaccel_device_type) {
  758. av_log(NULL, AV_LOG_VERBOSE, "Selecting decoder '%s' because of requested hwaccel method %s\n",
  759. c->name, av_hwdevice_get_type_name(hwaccel_device_type));
  760. *pcodec = c;
  761. return 0;
  762. }
  763. }
  764. }
  765. }
  766. *pcodec = avcodec_find_decoder(st->codecpar->codec_id);
  767. return 0;
  768. }
  769. }
  770. static int guess_input_channel_layout(InputStream *ist, int guess_layout_max)
  771. {
  772. AVCodecContext *dec = ist->dec_ctx;
  773. if (dec->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC) {
  774. char layout_name[256];
  775. if (dec->ch_layout.nb_channels > guess_layout_max)
  776. return 0;
  777. av_channel_layout_default(&dec->ch_layout, dec->ch_layout.nb_channels);
  778. if (dec->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC)
  779. return 0;
  780. av_channel_layout_describe(&dec->ch_layout, layout_name, sizeof(layout_name));
  781. av_log(ist, AV_LOG_WARNING, "Guessed Channel Layout: %s\n", layout_name);
  782. }
  783. return 1;
  784. }
  785. static int add_display_matrix_to_stream(const OptionsContext *o,
  786. AVFormatContext *ctx, InputStream *ist)
  787. {
  788. AVStream *st = ist->st;
  789. AVPacketSideData *sd;
  790. double rotation = DBL_MAX;
  791. int hflip = -1, vflip = -1;
  792. int hflip_set = 0, vflip_set = 0, rotation_set = 0;
  793. int32_t *buf;
  794. MATCH_PER_STREAM_OPT(display_rotations, dbl, rotation, ctx, st);
  795. MATCH_PER_STREAM_OPT(display_hflips, i, hflip, ctx, st);
  796. MATCH_PER_STREAM_OPT(display_vflips, i, vflip, ctx, st);
  797. rotation_set = rotation != DBL_MAX;
  798. hflip_set = hflip != -1;
  799. vflip_set = vflip != -1;
  800. if (!rotation_set && !hflip_set && !vflip_set)
  801. return 0;
  802. sd = av_packet_side_data_new(&st->codecpar->coded_side_data,
  803. &st->codecpar->nb_coded_side_data,
  804. AV_PKT_DATA_DISPLAYMATRIX,
  805. sizeof(int32_t) * 9, 0);
  806. if (!sd) {
  807. av_log(ist, AV_LOG_FATAL, "Failed to generate a display matrix!\n");
  808. return AVERROR(ENOMEM);
  809. }
  810. buf = (int32_t *)sd->data;
  811. av_display_rotation_set(buf,
  812. rotation_set ? -(rotation) : -0.0f);
  813. av_display_matrix_flip(buf,
  814. hflip_set ? hflip : 0,
  815. vflip_set ? vflip : 0);
  816. return 0;
  817. }
  818. static const char *input_stream_item_name(void *obj)
  819. {
  820. const DemuxStream *ds = obj;
  821. return ds->log_name;
  822. }
  823. static const AVClass input_stream_class = {
  824. .class_name = "InputStream",
  825. .version = LIBAVUTIL_VERSION_INT,
  826. .item_name = input_stream_item_name,
  827. .category = AV_CLASS_CATEGORY_DEMUXER,
  828. };
  829. static DemuxStream *demux_stream_alloc(Demuxer *d, AVStream *st)
  830. {
  831. const char *type_str = av_get_media_type_string(st->codecpar->codec_type);
  832. InputFile *f = &d->f;
  833. DemuxStream *ds;
  834. ds = allocate_array_elem(&f->streams, sizeof(*ds), &f->nb_streams);
  835. if (!ds)
  836. return NULL;
  837. ds->ist.st = st;
  838. ds->ist.file_index = f->index;
  839. ds->ist.index = st->index;
  840. ds->ist.class = &input_stream_class;
  841. snprintf(ds->log_name, sizeof(ds->log_name), "%cist#%d:%d/%s",
  842. type_str ? *type_str : '?', d->f.index, st->index,
  843. avcodec_get_name(st->codecpar->codec_id));
  844. return ds;
  845. }
  846. static int ist_add(const OptionsContext *o, Demuxer *d, AVStream *st)
  847. {
  848. AVFormatContext *ic = d->f.ctx;
  849. AVCodecParameters *par = st->codecpar;
  850. DemuxStream *ds;
  851. InputStream *ist;
  852. char *framerate = NULL, *hwaccel_device = NULL;
  853. const char *hwaccel = NULL;
  854. char *hwaccel_output_format = NULL;
  855. char *codec_tag = NULL;
  856. char *next;
  857. char *discard_str = NULL;
  858. int ret;
  859. ds = demux_stream_alloc(d, st);
  860. if (!ds)
  861. return AVERROR(ENOMEM);
  862. ist = &ds->ist;
  863. ist->discard = 1;
  864. st->discard = AVDISCARD_ALL;
  865. ist->nb_samples = 0;
  866. ds->first_dts = AV_NOPTS_VALUE;
  867. ds->next_dts = AV_NOPTS_VALUE;
  868. ds->min_pts = INT64_MAX;
  869. ds->max_pts = INT64_MIN;
  870. ds->ts_scale = 1.0;
  871. MATCH_PER_STREAM_OPT(ts_scale, dbl, ds->ts_scale, ic, st);
  872. ist->autorotate = 1;
  873. MATCH_PER_STREAM_OPT(autorotate, i, ist->autorotate, ic, st);
  874. MATCH_PER_STREAM_OPT(codec_tags, str, codec_tag, ic, st);
  875. if (codec_tag) {
  876. uint32_t tag = strtol(codec_tag, &next, 0);
  877. if (*next) {
  878. uint8_t buf[4] = { 0 };
  879. memcpy(buf, codec_tag, FFMIN(sizeof(buf), strlen(codec_tag)));
  880. tag = AV_RL32(buf);
  881. }
  882. st->codecpar->codec_tag = tag;
  883. }
  884. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  885. ret = add_display_matrix_to_stream(o, ic, ist);
  886. if (ret < 0)
  887. return ret;
  888. MATCH_PER_STREAM_OPT(hwaccels, str, hwaccel, ic, st);
  889. MATCH_PER_STREAM_OPT(hwaccel_output_formats, str,
  890. hwaccel_output_format, ic, st);
  891. if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel, "cuvid")) {
  892. av_log(ist, AV_LOG_WARNING,
  893. "WARNING: defaulting hwaccel_output_format to cuda for compatibility "
  894. "with old commandlines. This behaviour is DEPRECATED and will be removed "
  895. "in the future. Please explicitly set \"-hwaccel_output_format cuda\".\n");
  896. ist->hwaccel_output_format = AV_PIX_FMT_CUDA;
  897. } else if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel, "qsv")) {
  898. av_log(ist, AV_LOG_WARNING,
  899. "WARNING: defaulting hwaccel_output_format to qsv for compatibility "
  900. "with old commandlines. This behaviour is DEPRECATED and will be removed "
  901. "in the future. Please explicitly set \"-hwaccel_output_format qsv\".\n");
  902. ist->hwaccel_output_format = AV_PIX_FMT_QSV;
  903. } else if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel, "mediacodec")) {
  904. // There is no real AVHWFrameContext implementation. Set
  905. // hwaccel_output_format to avoid av_hwframe_transfer_data error.
  906. ist->hwaccel_output_format = AV_PIX_FMT_MEDIACODEC;
  907. } else if (hwaccel_output_format) {
  908. ist->hwaccel_output_format = av_get_pix_fmt(hwaccel_output_format);
  909. if (ist->hwaccel_output_format == AV_PIX_FMT_NONE) {
  910. av_log(ist, AV_LOG_FATAL, "Unrecognised hwaccel output "
  911. "format: %s", hwaccel_output_format);
  912. }
  913. } else {
  914. ist->hwaccel_output_format = AV_PIX_FMT_NONE;
  915. }
  916. if (hwaccel) {
  917. // The NVDEC hwaccels use a CUDA device, so remap the name here.
  918. if (!strcmp(hwaccel, "nvdec") || !strcmp(hwaccel, "cuvid"))
  919. hwaccel = "cuda";
  920. if (!strcmp(hwaccel, "none"))
  921. ist->hwaccel_id = HWACCEL_NONE;
  922. else if (!strcmp(hwaccel, "auto"))
  923. ist->hwaccel_id = HWACCEL_AUTO;
  924. else {
  925. enum AVHWDeviceType type = av_hwdevice_find_type_by_name(hwaccel);
  926. if (type != AV_HWDEVICE_TYPE_NONE) {
  927. ist->hwaccel_id = HWACCEL_GENERIC;
  928. ist->hwaccel_device_type = type;
  929. }
  930. if (!ist->hwaccel_id) {
  931. av_log(ist, AV_LOG_FATAL, "Unrecognized hwaccel: %s.\n",
  932. hwaccel);
  933. av_log(ist, AV_LOG_FATAL, "Supported hwaccels: ");
  934. type = AV_HWDEVICE_TYPE_NONE;
  935. while ((type = av_hwdevice_iterate_types(type)) !=
  936. AV_HWDEVICE_TYPE_NONE)
  937. av_log(ist, AV_LOG_FATAL, "%s ",
  938. av_hwdevice_get_type_name(type));
  939. av_log(ist, AV_LOG_FATAL, "\n");
  940. return AVERROR(EINVAL);
  941. }
  942. }
  943. }
  944. MATCH_PER_STREAM_OPT(hwaccel_devices, str, hwaccel_device, ic, st);
  945. if (hwaccel_device) {
  946. ist->hwaccel_device = av_strdup(hwaccel_device);
  947. if (!ist->hwaccel_device)
  948. return AVERROR(ENOMEM);
  949. }
  950. }
  951. ret = choose_decoder(o, ic, st, ist->hwaccel_id, ist->hwaccel_device_type,
  952. &ist->dec);
  953. if (ret < 0)
  954. return ret;
  955. ret = filter_codec_opts(o->g->codec_opts, ist->st->codecpar->codec_id,
  956. ic, st, ist->dec, &ist->decoder_opts);
  957. if (ret < 0)
  958. return ret;
  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. ist->dec_ctx = avcodec_alloc_context3(ist->dec);
  969. if (!ist->dec_ctx)
  970. return AVERROR(ENOMEM);
  971. if (discard_str) {
  972. const AVOption *discard_opt = av_opt_find(ist->dec_ctx, "skip_frame",
  973. NULL, 0, 0);
  974. ret = av_opt_eval_int(ist->dec_ctx, discard_opt, discard_str, &ist->user_set_discard);
  975. if (ret < 0) {
  976. av_log(ist, AV_LOG_ERROR, "Error parsing discard %s.\n", discard_str);
  977. return ret;
  978. }
  979. }
  980. ret = avcodec_parameters_to_context(ist->dec_ctx, par);
  981. if (ret < 0) {
  982. av_log(ist, AV_LOG_ERROR, "Error initializing the decoder context.\n");
  983. return ret;
  984. }
  985. if (o->bitexact)
  986. ist->dec_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
  987. switch (par->codec_type) {
  988. case AVMEDIA_TYPE_VIDEO:
  989. MATCH_PER_STREAM_OPT(frame_rates, str, framerate, ic, st);
  990. if (framerate) {
  991. ret = av_parse_video_rate(&ist->framerate, framerate);
  992. if (ret < 0) {
  993. av_log(ist, AV_LOG_ERROR, "Error parsing framerate %s.\n",
  994. framerate);
  995. return ret;
  996. }
  997. }
  998. #if FFMPEG_OPT_TOP
  999. ist->top_field_first = -1;
  1000. MATCH_PER_STREAM_OPT(top_field_first, i, ist->top_field_first, ic, st);
  1001. #endif
  1002. ist->framerate_guessed = av_guess_frame_rate(ic, st, NULL);
  1003. break;
  1004. case AVMEDIA_TYPE_AUDIO: {
  1005. int guess_layout_max = INT_MAX;
  1006. MATCH_PER_STREAM_OPT(guess_layout_max, i, guess_layout_max, ic, st);
  1007. guess_input_channel_layout(ist, guess_layout_max);
  1008. break;
  1009. }
  1010. case AVMEDIA_TYPE_DATA:
  1011. case AVMEDIA_TYPE_SUBTITLE: {
  1012. char *canvas_size = NULL;
  1013. MATCH_PER_STREAM_OPT(fix_sub_duration, i, ist->fix_sub_duration, ic, st);
  1014. MATCH_PER_STREAM_OPT(canvas_sizes, str, canvas_size, ic, st);
  1015. if (canvas_size) {
  1016. ret = av_parse_video_size(&ist->dec_ctx->width, &ist->dec_ctx->height,
  1017. canvas_size);
  1018. if (ret < 0) {
  1019. av_log(ist, AV_LOG_FATAL, "Invalid canvas size: %s.\n", canvas_size);
  1020. return ret;
  1021. }
  1022. }
  1023. /* Compute the size of the canvas for the subtitles stream.
  1024. If the subtitles codecpar has set a size, use it. Otherwise use the
  1025. maximum dimensions of the video streams in the same file. */
  1026. ist->sub2video.w = ist->dec_ctx->width;
  1027. ist->sub2video.h = ist->dec_ctx->height;
  1028. if (!(ist->sub2video.w && ist->sub2video.h)) {
  1029. for (int j = 0; j < ic->nb_streams; j++) {
  1030. AVCodecParameters *par1 = ic->streams[j]->codecpar;
  1031. if (par1->codec_type == AVMEDIA_TYPE_VIDEO) {
  1032. ist->sub2video.w = FFMAX(ist->sub2video.w, par1->width);
  1033. ist->sub2video.h = FFMAX(ist->sub2video.h, par1->height);
  1034. }
  1035. }
  1036. }
  1037. if (!(ist->sub2video.w && ist->sub2video.h)) {
  1038. ist->sub2video.w = FFMAX(ist->sub2video.w, 720);
  1039. ist->sub2video.h = FFMAX(ist->sub2video.h, 576);
  1040. }
  1041. break;
  1042. }
  1043. case AVMEDIA_TYPE_ATTACHMENT:
  1044. case AVMEDIA_TYPE_UNKNOWN:
  1045. break;
  1046. default:
  1047. abort();
  1048. }
  1049. ist->par = avcodec_parameters_alloc();
  1050. if (!ist->par)
  1051. return AVERROR(ENOMEM);
  1052. ret = avcodec_parameters_from_context(ist->par, ist->dec_ctx);
  1053. if (ret < 0) {
  1054. av_log(ist, AV_LOG_ERROR, "Error initializing the decoder context.\n");
  1055. return ret;
  1056. }
  1057. ist->codec_desc = avcodec_descriptor_get(ist->par->codec_id);
  1058. return 0;
  1059. }
  1060. static int dump_attachment(InputStream *ist, const char *filename)
  1061. {
  1062. AVStream *st = ist->st;
  1063. int ret;
  1064. AVIOContext *out = NULL;
  1065. const AVDictionaryEntry *e;
  1066. if (!st->codecpar->extradata_size) {
  1067. av_log(ist, AV_LOG_WARNING, "No extradata to dump.\n");
  1068. return 0;
  1069. }
  1070. if (!*filename && (e = av_dict_get(st->metadata, "filename", NULL, 0)))
  1071. filename = e->value;
  1072. if (!*filename) {
  1073. av_log(ist, AV_LOG_FATAL, "No filename specified and no 'filename' tag");
  1074. return AVERROR(EINVAL);
  1075. }
  1076. ret = assert_file_overwrite(filename);
  1077. if (ret < 0)
  1078. return ret;
  1079. if ((ret = avio_open2(&out, filename, AVIO_FLAG_WRITE, &int_cb, NULL)) < 0) {
  1080. av_log(ist, AV_LOG_FATAL, "Could not open file %s for writing.\n",
  1081. filename);
  1082. return ret;
  1083. }
  1084. avio_write(out, st->codecpar->extradata, st->codecpar->extradata_size);
  1085. ret = avio_close(out);
  1086. if (ret >= 0)
  1087. av_log(ist, AV_LOG_INFO, "Wrote attachment (%d bytes) to '%s'\n",
  1088. st->codecpar->extradata_size, filename);
  1089. return ret;
  1090. }
  1091. static const char *input_file_item_name(void *obj)
  1092. {
  1093. const Demuxer *d = obj;
  1094. return d->log_name;
  1095. }
  1096. static const AVClass input_file_class = {
  1097. .class_name = "InputFile",
  1098. .version = LIBAVUTIL_VERSION_INT,
  1099. .item_name = input_file_item_name,
  1100. .category = AV_CLASS_CATEGORY_DEMUXER,
  1101. };
  1102. static Demuxer *demux_alloc(void)
  1103. {
  1104. Demuxer *d = allocate_array_elem(&input_files, sizeof(*d), &nb_input_files);
  1105. if (!d)
  1106. return NULL;
  1107. d->f.class = &input_file_class;
  1108. d->f.index = nb_input_files - 1;
  1109. snprintf(d->log_name, sizeof(d->log_name), "in#%d", d->f.index);
  1110. return d;
  1111. }
  1112. int ifile_open(const OptionsContext *o, const char *filename)
  1113. {
  1114. Demuxer *d;
  1115. InputFile *f;
  1116. AVFormatContext *ic;
  1117. const AVInputFormat *file_iformat = NULL;
  1118. int err, i, ret = 0;
  1119. int64_t timestamp;
  1120. AVDictionary *unused_opts = NULL;
  1121. const AVDictionaryEntry *e = NULL;
  1122. char * video_codec_name = NULL;
  1123. char * audio_codec_name = NULL;
  1124. char *subtitle_codec_name = NULL;
  1125. char * data_codec_name = NULL;
  1126. int scan_all_pmts_set = 0;
  1127. int64_t start_time = o->start_time;
  1128. int64_t start_time_eof = o->start_time_eof;
  1129. int64_t stop_time = o->stop_time;
  1130. int64_t recording_time = o->recording_time;
  1131. d = demux_alloc();
  1132. if (!d)
  1133. return AVERROR(ENOMEM);
  1134. f = &d->f;
  1135. if (stop_time != INT64_MAX && recording_time != INT64_MAX) {
  1136. stop_time = INT64_MAX;
  1137. av_log(d, AV_LOG_WARNING, "-t and -to cannot be used together; using -t.\n");
  1138. }
  1139. if (stop_time != INT64_MAX && recording_time == INT64_MAX) {
  1140. int64_t start = start_time == AV_NOPTS_VALUE ? 0 : start_time;
  1141. if (stop_time <= start) {
  1142. av_log(d, AV_LOG_ERROR, "-to value smaller than -ss; aborting.\n");
  1143. return AVERROR(EINVAL);
  1144. } else {
  1145. recording_time = stop_time - start;
  1146. }
  1147. }
  1148. if (o->format) {
  1149. if (!(file_iformat = av_find_input_format(o->format))) {
  1150. av_log(d, AV_LOG_FATAL, "Unknown input format: '%s'\n", o->format);
  1151. return AVERROR(EINVAL);
  1152. }
  1153. }
  1154. if (!strcmp(filename, "-"))
  1155. filename = "fd:";
  1156. stdin_interaction &= strncmp(filename, "pipe:", 5) &&
  1157. strcmp(filename, "fd:") &&
  1158. strcmp(filename, "/dev/stdin");
  1159. /* get default parameters from command line */
  1160. ic = avformat_alloc_context();
  1161. if (!ic)
  1162. return AVERROR(ENOMEM);
  1163. if (o->nb_audio_sample_rate) {
  1164. av_dict_set_int(&o->g->format_opts, "sample_rate", o->audio_sample_rate[o->nb_audio_sample_rate - 1].u.i, 0);
  1165. }
  1166. if (o->nb_audio_channels) {
  1167. const AVClass *priv_class;
  1168. if (file_iformat && (priv_class = file_iformat->priv_class) &&
  1169. av_opt_find(&priv_class, "ch_layout", NULL, 0,
  1170. AV_OPT_SEARCH_FAKE_OBJ)) {
  1171. char buf[32];
  1172. snprintf(buf, sizeof(buf), "%dC", o->audio_channels[o->nb_audio_channels - 1].u.i);
  1173. av_dict_set(&o->g->format_opts, "ch_layout", buf, 0);
  1174. }
  1175. }
  1176. if (o->nb_audio_ch_layouts) {
  1177. const AVClass *priv_class;
  1178. if (file_iformat && (priv_class = file_iformat->priv_class) &&
  1179. av_opt_find(&priv_class, "ch_layout", NULL, 0,
  1180. AV_OPT_SEARCH_FAKE_OBJ)) {
  1181. av_dict_set(&o->g->format_opts, "ch_layout", o->audio_ch_layouts[o->nb_audio_ch_layouts - 1].u.str, 0);
  1182. }
  1183. }
  1184. if (o->nb_frame_rates) {
  1185. const AVClass *priv_class;
  1186. /* set the format-level framerate option;
  1187. * this is important for video grabbers, e.g. x11 */
  1188. if (file_iformat && (priv_class = file_iformat->priv_class) &&
  1189. av_opt_find(&priv_class, "framerate", NULL, 0,
  1190. AV_OPT_SEARCH_FAKE_OBJ)) {
  1191. av_dict_set(&o->g->format_opts, "framerate",
  1192. o->frame_rates[o->nb_frame_rates - 1].u.str, 0);
  1193. }
  1194. }
  1195. if (o->nb_frame_sizes) {
  1196. av_dict_set(&o->g->format_opts, "video_size", o->frame_sizes[o->nb_frame_sizes - 1].u.str, 0);
  1197. }
  1198. if (o->nb_frame_pix_fmts)
  1199. av_dict_set(&o->g->format_opts, "pixel_format", o->frame_pix_fmts[o->nb_frame_pix_fmts - 1].u.str, 0);
  1200. MATCH_PER_TYPE_OPT(codec_names, str, video_codec_name, ic, "v");
  1201. MATCH_PER_TYPE_OPT(codec_names, str, audio_codec_name, ic, "a");
  1202. MATCH_PER_TYPE_OPT(codec_names, str, subtitle_codec_name, ic, "s");
  1203. MATCH_PER_TYPE_OPT(codec_names, str, data_codec_name, ic, "d");
  1204. if (video_codec_name)
  1205. ret = err_merge(ret, find_codec(NULL, video_codec_name , AVMEDIA_TYPE_VIDEO , 0,
  1206. &ic->video_codec));
  1207. if (audio_codec_name)
  1208. ret = err_merge(ret, find_codec(NULL, audio_codec_name , AVMEDIA_TYPE_AUDIO , 0,
  1209. &ic->audio_codec));
  1210. if (subtitle_codec_name)
  1211. ret = err_merge(ret, find_codec(NULL, subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, 0,
  1212. &ic->subtitle_codec));
  1213. if (data_codec_name)
  1214. ret = err_merge(ret, find_codec(NULL, data_codec_name , AVMEDIA_TYPE_DATA, 0,
  1215. &ic->data_codec));
  1216. if (ret < 0) {
  1217. avformat_free_context(ic);
  1218. return ret;
  1219. }
  1220. ic->video_codec_id = video_codec_name ? ic->video_codec->id : AV_CODEC_ID_NONE;
  1221. ic->audio_codec_id = audio_codec_name ? ic->audio_codec->id : AV_CODEC_ID_NONE;
  1222. ic->subtitle_codec_id = subtitle_codec_name ? ic->subtitle_codec->id : AV_CODEC_ID_NONE;
  1223. ic->data_codec_id = data_codec_name ? ic->data_codec->id : AV_CODEC_ID_NONE;
  1224. ic->flags |= AVFMT_FLAG_NONBLOCK;
  1225. if (o->bitexact)
  1226. ic->flags |= AVFMT_FLAG_BITEXACT;
  1227. ic->interrupt_callback = int_cb;
  1228. if (!av_dict_get(o->g->format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE)) {
  1229. av_dict_set(&o->g->format_opts, "scan_all_pmts", "1", AV_DICT_DONT_OVERWRITE);
  1230. scan_all_pmts_set = 1;
  1231. }
  1232. /* open the input file with generic avformat function */
  1233. err = avformat_open_input(&ic, filename, file_iformat, &o->g->format_opts);
  1234. if (err < 0) {
  1235. av_log(d, AV_LOG_ERROR,
  1236. "Error opening input: %s\n", av_err2str(err));
  1237. if (err == AVERROR_PROTOCOL_NOT_FOUND)
  1238. av_log(d, AV_LOG_ERROR, "Did you mean file:%s?\n", filename);
  1239. return err;
  1240. }
  1241. f->ctx = ic;
  1242. av_strlcat(d->log_name, "/", sizeof(d->log_name));
  1243. av_strlcat(d->log_name, ic->iformat->name, sizeof(d->log_name));
  1244. if (scan_all_pmts_set)
  1245. av_dict_set(&o->g->format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE);
  1246. remove_avoptions(&o->g->format_opts, o->g->codec_opts);
  1247. ret = check_avoptions(o->g->format_opts);
  1248. if (ret < 0)
  1249. return ret;
  1250. /* apply forced codec ids */
  1251. for (i = 0; i < ic->nb_streams; i++) {
  1252. const AVCodec *dummy;
  1253. ret = choose_decoder(o, ic, ic->streams[i], HWACCEL_NONE, AV_HWDEVICE_TYPE_NONE,
  1254. &dummy);
  1255. if (ret < 0)
  1256. return ret;
  1257. }
  1258. if (o->find_stream_info) {
  1259. AVDictionary **opts;
  1260. int orig_nb_streams = ic->nb_streams;
  1261. ret = setup_find_stream_info_opts(ic, o->g->codec_opts, &opts);
  1262. if (ret < 0)
  1263. return ret;
  1264. /* If not enough info to get the stream parameters, we decode the
  1265. first frames to get it. (used in mpeg case for example) */
  1266. ret = avformat_find_stream_info(ic, opts);
  1267. for (i = 0; i < orig_nb_streams; i++)
  1268. av_dict_free(&opts[i]);
  1269. av_freep(&opts);
  1270. if (ret < 0) {
  1271. av_log(d, AV_LOG_FATAL, "could not find codec parameters\n");
  1272. if (ic->nb_streams == 0)
  1273. return ret;
  1274. }
  1275. }
  1276. if (start_time != AV_NOPTS_VALUE && start_time_eof != AV_NOPTS_VALUE) {
  1277. av_log(d, AV_LOG_WARNING, "Cannot use -ss and -sseof both, using -ss\n");
  1278. start_time_eof = AV_NOPTS_VALUE;
  1279. }
  1280. if (start_time_eof != AV_NOPTS_VALUE) {
  1281. if (start_time_eof >= 0) {
  1282. av_log(d, AV_LOG_ERROR, "-sseof value must be negative; aborting\n");
  1283. return AVERROR(EINVAL);
  1284. }
  1285. if (ic->duration > 0) {
  1286. start_time = start_time_eof + ic->duration;
  1287. if (start_time < 0) {
  1288. av_log(d, AV_LOG_WARNING, "-sseof value seeks to before start of file; ignored\n");
  1289. start_time = AV_NOPTS_VALUE;
  1290. }
  1291. } else
  1292. av_log(d, AV_LOG_WARNING, "Cannot use -sseof, file duration not known\n");
  1293. }
  1294. timestamp = (start_time == AV_NOPTS_VALUE) ? 0 : start_time;
  1295. /* add the stream start time */
  1296. if (!o->seek_timestamp && ic->start_time != AV_NOPTS_VALUE)
  1297. timestamp += ic->start_time;
  1298. /* if seeking requested, we execute it */
  1299. if (start_time != AV_NOPTS_VALUE) {
  1300. int64_t seek_timestamp = timestamp;
  1301. if (!(ic->iformat->flags & AVFMT_SEEK_TO_PTS)) {
  1302. int dts_heuristic = 0;
  1303. for (i=0; i<ic->nb_streams; i++) {
  1304. const AVCodecParameters *par = ic->streams[i]->codecpar;
  1305. if (par->video_delay) {
  1306. dts_heuristic = 1;
  1307. break;
  1308. }
  1309. }
  1310. if (dts_heuristic) {
  1311. seek_timestamp -= 3*AV_TIME_BASE / 23;
  1312. }
  1313. }
  1314. ret = avformat_seek_file(ic, -1, INT64_MIN, seek_timestamp, seek_timestamp, 0);
  1315. if (ret < 0) {
  1316. av_log(d, AV_LOG_WARNING, "could not seek to position %0.3f\n",
  1317. (double)timestamp / AV_TIME_BASE);
  1318. }
  1319. }
  1320. f->start_time = start_time;
  1321. f->recording_time = recording_time;
  1322. f->input_sync_ref = o->input_sync_ref;
  1323. f->input_ts_offset = o->input_ts_offset;
  1324. f->ts_offset = o->input_ts_offset - (copy_ts ? (start_at_zero && ic->start_time != AV_NOPTS_VALUE ? ic->start_time : 0) : timestamp);
  1325. f->accurate_seek = o->accurate_seek;
  1326. d->loop = o->loop;
  1327. d->duration = 0;
  1328. d->time_base = (AVRational){ 1, 1 };
  1329. d->nb_streams_warn = ic->nb_streams;
  1330. f->format_nots = !!(ic->iformat->flags & AVFMT_NOTIMESTAMPS);
  1331. f->readrate = o->readrate ? o->readrate : 0.0;
  1332. if (f->readrate < 0.0f) {
  1333. av_log(d, AV_LOG_ERROR, "Option -readrate is %0.3f; it must be non-negative.\n", f->readrate);
  1334. return AVERROR(EINVAL);
  1335. }
  1336. if (o->rate_emu) {
  1337. if (f->readrate) {
  1338. av_log(d, AV_LOG_WARNING, "Both -readrate and -re set. Using -readrate %0.3f.\n", f->readrate);
  1339. } else
  1340. f->readrate = 1.0f;
  1341. }
  1342. if (f->readrate) {
  1343. d->readrate_initial_burst = o->readrate_initial_burst ? o->readrate_initial_burst : 0.5;
  1344. if (d->readrate_initial_burst < 0.0) {
  1345. av_log(d, AV_LOG_ERROR,
  1346. "Option -readrate_initial_burst is %0.3f; it must be non-negative.\n",
  1347. d->readrate_initial_burst);
  1348. return AVERROR(EINVAL);
  1349. }
  1350. } else if (o->readrate_initial_burst) {
  1351. av_log(d, AV_LOG_WARNING, "Option -readrate_initial_burst ignored "
  1352. "since neither -readrate nor -re were given\n");
  1353. }
  1354. d->thread_queue_size = o->thread_queue_size;
  1355. /* Add all the streams from the given input file to the demuxer */
  1356. for (int i = 0; i < ic->nb_streams; i++) {
  1357. ret = ist_add(o, d, ic->streams[i]);
  1358. if (ret < 0)
  1359. return ret;
  1360. }
  1361. /* dump the file content */
  1362. av_dump_format(ic, f->index, filename, 0);
  1363. /* check if all codec options have been used */
  1364. unused_opts = strip_specifiers(o->g->codec_opts);
  1365. for (i = 0; i < f->nb_streams; i++) {
  1366. e = NULL;
  1367. while ((e = av_dict_iterate(f->streams[i]->decoder_opts, e)))
  1368. av_dict_set(&unused_opts, e->key, NULL, 0);
  1369. }
  1370. e = NULL;
  1371. while ((e = av_dict_iterate(unused_opts, e))) {
  1372. const AVClass *class = avcodec_get_class();
  1373. const AVOption *option = av_opt_find(&class, e->key, NULL, 0,
  1374. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
  1375. const AVClass *fclass = avformat_get_class();
  1376. const AVOption *foption = av_opt_find(&fclass, e->key, NULL, 0,
  1377. AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
  1378. if (!option || foption)
  1379. continue;
  1380. if (!(option->flags & AV_OPT_FLAG_DECODING_PARAM)) {
  1381. av_log(d, AV_LOG_ERROR, "Codec AVOption %s (%s) is not a decoding "
  1382. "option.\n", e->key, option->help ? option->help : "");
  1383. return AVERROR(EINVAL);
  1384. }
  1385. av_log(d, AV_LOG_WARNING, "Codec AVOption %s (%s) has not been used "
  1386. "for any stream. The most likely reason is either wrong type "
  1387. "(e.g. a video option with no video streams) or that it is a "
  1388. "private option of some decoder which was not actually used "
  1389. "for any stream.\n", e->key, option->help ? option->help : "");
  1390. }
  1391. av_dict_free(&unused_opts);
  1392. for (i = 0; i < o->nb_dump_attachment; i++) {
  1393. int j;
  1394. for (j = 0; j < f->nb_streams; j++) {
  1395. InputStream *ist = f->streams[j];
  1396. if (check_stream_specifier(ic, ist->st, o->dump_attachment[i].specifier) == 1) {
  1397. ret = dump_attachment(ist, o->dump_attachment[i].u.str);
  1398. if (ret < 0)
  1399. return ret;
  1400. }
  1401. }
  1402. }
  1403. return 0;
  1404. }