ffmpeg_demux.c 63 KB

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