ffmpeg_dec.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770
  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 <stdbit.h>
  19. #include "libavutil/avassert.h"
  20. #include "libavutil/avstring.h"
  21. #include "libavutil/dict.h"
  22. #include "libavutil/error.h"
  23. #include "libavutil/log.h"
  24. #include "libavutil/mem.h"
  25. #include "libavutil/opt.h"
  26. #include "libavutil/pixdesc.h"
  27. #include "libavutil/pixfmt.h"
  28. #include "libavutil/stereo3d.h"
  29. #include "libavutil/time.h"
  30. #include "libavutil/timestamp.h"
  31. #include "libavcodec/avcodec.h"
  32. #include "libavcodec/codec.h"
  33. #include "ffmpeg.h"
  34. typedef struct DecoderPriv {
  35. Decoder dec;
  36. AVCodecContext *dec_ctx;
  37. AVFrame *frame;
  38. AVFrame *frame_tmp_ref;
  39. AVPacket *pkt;
  40. // override output video sample aspect ratio with this value
  41. AVRational sar_override;
  42. AVRational framerate_in;
  43. // a combination of DECODER_FLAG_*, provided to dec_open()
  44. int flags;
  45. int apply_cropping;
  46. enum AVPixelFormat hwaccel_pix_fmt;
  47. enum HWAccelID hwaccel_id;
  48. enum AVHWDeviceType hwaccel_device_type;
  49. enum AVPixelFormat hwaccel_output_format;
  50. // pts/estimated duration of the last decoded frame
  51. // * in decoder timebase for video,
  52. // * in last_frame_tb (may change during decoding) for audio
  53. int64_t last_frame_pts;
  54. int64_t last_frame_duration_est;
  55. AVRational last_frame_tb;
  56. int64_t last_filter_in_rescale_delta;
  57. int last_frame_sample_rate;
  58. /* previous decoded subtitles */
  59. AVFrame *sub_prev[2];
  60. AVFrame *sub_heartbeat;
  61. Scheduler *sch;
  62. unsigned sch_idx;
  63. // this decoder's index in decoders or -1
  64. int index;
  65. void *log_parent;
  66. char log_name[32];
  67. char *parent_name;
  68. // user specified decoder multiview options manually
  69. int multiview_user_config;
  70. struct {
  71. ViewSpecifier vs;
  72. unsigned out_idx;
  73. } *views_requested;
  74. int nb_views_requested;
  75. /* A map of view ID to decoder outputs.
  76. * MUST NOT be accessed outside of get_format()/get_buffer() */
  77. struct {
  78. unsigned id;
  79. uintptr_t out_mask;
  80. } *view_map;
  81. int nb_view_map;
  82. struct {
  83. AVDictionary *opts;
  84. const AVCodec *codec;
  85. } standalone_init;
  86. } DecoderPriv;
  87. static DecoderPriv *dp_from_dec(Decoder *d)
  88. {
  89. return (DecoderPriv*)d;
  90. }
  91. // data that is local to the decoder thread and not visible outside of it
  92. typedef struct DecThreadContext {
  93. AVFrame *frame;
  94. AVPacket *pkt;
  95. } DecThreadContext;
  96. void dec_free(Decoder **pdec)
  97. {
  98. Decoder *dec = *pdec;
  99. DecoderPriv *dp;
  100. if (!dec)
  101. return;
  102. dp = dp_from_dec(dec);
  103. avcodec_free_context(&dp->dec_ctx);
  104. av_frame_free(&dp->frame);
  105. av_frame_free(&dp->frame_tmp_ref);
  106. av_packet_free(&dp->pkt);
  107. av_dict_free(&dp->standalone_init.opts);
  108. for (int i = 0; i < FF_ARRAY_ELEMS(dp->sub_prev); i++)
  109. av_frame_free(&dp->sub_prev[i]);
  110. av_frame_free(&dp->sub_heartbeat);
  111. av_freep(&dp->parent_name);
  112. av_freep(&dp->views_requested);
  113. av_freep(&dp->view_map);
  114. av_freep(pdec);
  115. }
  116. static const char *dec_item_name(void *obj)
  117. {
  118. const DecoderPriv *dp = obj;
  119. return dp->log_name;
  120. }
  121. static const AVClass dec_class = {
  122. .class_name = "Decoder",
  123. .version = LIBAVUTIL_VERSION_INT,
  124. .parent_log_context_offset = offsetof(DecoderPriv, log_parent),
  125. .item_name = dec_item_name,
  126. };
  127. static int decoder_thread(void *arg);
  128. static int dec_alloc(DecoderPriv **pdec, Scheduler *sch, int send_end_ts)
  129. {
  130. DecoderPriv *dp;
  131. int ret = 0;
  132. *pdec = NULL;
  133. dp = av_mallocz(sizeof(*dp));
  134. if (!dp)
  135. return AVERROR(ENOMEM);
  136. dp->frame = av_frame_alloc();
  137. if (!dp->frame)
  138. goto fail;
  139. dp->pkt = av_packet_alloc();
  140. if (!dp->pkt)
  141. goto fail;
  142. dp->index = -1;
  143. dp->dec.class = &dec_class;
  144. dp->last_filter_in_rescale_delta = AV_NOPTS_VALUE;
  145. dp->last_frame_pts = AV_NOPTS_VALUE;
  146. dp->last_frame_tb = (AVRational){ 1, 1 };
  147. dp->hwaccel_pix_fmt = AV_PIX_FMT_NONE;
  148. ret = sch_add_dec(sch, decoder_thread, dp, send_end_ts);
  149. if (ret < 0)
  150. goto fail;
  151. dp->sch = sch;
  152. dp->sch_idx = ret;
  153. *pdec = dp;
  154. return 0;
  155. fail:
  156. dec_free((Decoder**)&dp);
  157. return ret >= 0 ? AVERROR(ENOMEM) : ret;
  158. }
  159. static AVRational audio_samplerate_update(DecoderPriv *dp,
  160. const AVFrame *frame)
  161. {
  162. const int prev = dp->last_frame_tb.den;
  163. const int sr = frame->sample_rate;
  164. AVRational tb_new;
  165. int64_t gcd;
  166. if (frame->sample_rate == dp->last_frame_sample_rate)
  167. goto finish;
  168. gcd = av_gcd(prev, sr);
  169. if (prev / gcd >= INT_MAX / sr) {
  170. av_log(dp, AV_LOG_WARNING,
  171. "Audio timestamps cannot be represented exactly after "
  172. "sample rate change: %d -> %d\n", prev, sr);
  173. // LCM of 192000, 44100, allows to represent all common samplerates
  174. tb_new = (AVRational){ 1, 28224000 };
  175. } else
  176. tb_new = (AVRational){ 1, prev / gcd * sr };
  177. // keep the frame timebase if it is strictly better than
  178. // the samplerate-defined one
  179. if (frame->time_base.num == 1 && frame->time_base.den > tb_new.den &&
  180. !(frame->time_base.den % tb_new.den))
  181. tb_new = frame->time_base;
  182. if (dp->last_frame_pts != AV_NOPTS_VALUE)
  183. dp->last_frame_pts = av_rescale_q(dp->last_frame_pts,
  184. dp->last_frame_tb, tb_new);
  185. dp->last_frame_duration_est = av_rescale_q(dp->last_frame_duration_est,
  186. dp->last_frame_tb, tb_new);
  187. dp->last_frame_tb = tb_new;
  188. dp->last_frame_sample_rate = frame->sample_rate;
  189. finish:
  190. return dp->last_frame_tb;
  191. }
  192. static void audio_ts_process(DecoderPriv *dp, AVFrame *frame)
  193. {
  194. AVRational tb_filter = (AVRational){1, frame->sample_rate};
  195. AVRational tb;
  196. int64_t pts_pred;
  197. // on samplerate change, choose a new internal timebase for timestamp
  198. // generation that can represent timestamps from all the samplerates
  199. // seen so far
  200. tb = audio_samplerate_update(dp, frame);
  201. pts_pred = dp->last_frame_pts == AV_NOPTS_VALUE ? 0 :
  202. dp->last_frame_pts + dp->last_frame_duration_est;
  203. if (frame->pts == AV_NOPTS_VALUE) {
  204. frame->pts = pts_pred;
  205. frame->time_base = tb;
  206. } else if (dp->last_frame_pts != AV_NOPTS_VALUE &&
  207. frame->pts > av_rescale_q_rnd(pts_pred, tb, frame->time_base,
  208. AV_ROUND_UP)) {
  209. // there was a gap in timestamps, reset conversion state
  210. dp->last_filter_in_rescale_delta = AV_NOPTS_VALUE;
  211. }
  212. frame->pts = av_rescale_delta(frame->time_base, frame->pts,
  213. tb, frame->nb_samples,
  214. &dp->last_filter_in_rescale_delta, tb);
  215. dp->last_frame_pts = frame->pts;
  216. dp->last_frame_duration_est = av_rescale_q(frame->nb_samples,
  217. tb_filter, tb);
  218. // finally convert to filtering timebase
  219. frame->pts = av_rescale_q(frame->pts, tb, tb_filter);
  220. frame->duration = frame->nb_samples;
  221. frame->time_base = tb_filter;
  222. }
  223. static int64_t video_duration_estimate(const DecoderPriv *dp, const AVFrame *frame)
  224. {
  225. const int ts_unreliable = dp->flags & DECODER_FLAG_TS_UNRELIABLE;
  226. const int fr_forced = dp->flags & DECODER_FLAG_FRAMERATE_FORCED;
  227. int64_t codec_duration = 0;
  228. // difference between this and last frame's timestamps
  229. const int64_t ts_diff =
  230. (frame->pts != AV_NOPTS_VALUE && dp->last_frame_pts != AV_NOPTS_VALUE) ?
  231. frame->pts - dp->last_frame_pts : -1;
  232. // XXX lavf currently makes up frame durations when they are not provided by
  233. // the container. As there is no way to reliably distinguish real container
  234. // durations from the fake made-up ones, we use heuristics based on whether
  235. // the container has timestamps. Eventually lavf should stop making up
  236. // durations, then this should be simplified.
  237. // frame duration is unreliable (typically guessed by lavf) when it is equal
  238. // to 1 and the actual duration of the last frame is more than 2x larger
  239. const int duration_unreliable = frame->duration == 1 && ts_diff > 2 * frame->duration;
  240. // prefer frame duration for containers with timestamps
  241. if (fr_forced ||
  242. (frame->duration > 0 && !ts_unreliable && !duration_unreliable))
  243. return frame->duration;
  244. if (dp->dec_ctx->framerate.den && dp->dec_ctx->framerate.num) {
  245. int fields = frame->repeat_pict + 2;
  246. AVRational field_rate = av_mul_q(dp->dec_ctx->framerate,
  247. (AVRational){ 2, 1 });
  248. codec_duration = av_rescale_q(fields, av_inv_q(field_rate),
  249. frame->time_base);
  250. }
  251. // prefer codec-layer duration for containers without timestamps
  252. if (codec_duration > 0 && ts_unreliable)
  253. return codec_duration;
  254. // when timestamps are available, repeat last frame's actual duration
  255. // (i.e. pts difference between this and last frame)
  256. if (ts_diff > 0)
  257. return ts_diff;
  258. // try frame/codec duration
  259. if (frame->duration > 0)
  260. return frame->duration;
  261. if (codec_duration > 0)
  262. return codec_duration;
  263. // try average framerate
  264. if (dp->framerate_in.num && dp->framerate_in.den) {
  265. int64_t d = av_rescale_q(1, av_inv_q(dp->framerate_in),
  266. frame->time_base);
  267. if (d > 0)
  268. return d;
  269. }
  270. // last resort is last frame's estimated duration, and 1
  271. return FFMAX(dp->last_frame_duration_est, 1);
  272. }
  273. static int hwaccel_retrieve_data(AVCodecContext *avctx, AVFrame *input)
  274. {
  275. DecoderPriv *dp = avctx->opaque;
  276. AVFrame *output = NULL;
  277. enum AVPixelFormat output_format = dp->hwaccel_output_format;
  278. int err;
  279. if (input->format == output_format) {
  280. // Nothing to do.
  281. return 0;
  282. }
  283. output = av_frame_alloc();
  284. if (!output)
  285. return AVERROR(ENOMEM);
  286. output->format = output_format;
  287. err = av_hwframe_transfer_data(output, input, 0);
  288. if (err < 0) {
  289. av_log(avctx, AV_LOG_ERROR, "Failed to transfer data to "
  290. "output frame: %d.\n", err);
  291. goto fail;
  292. }
  293. err = av_frame_copy_props(output, input);
  294. if (err < 0) {
  295. av_frame_unref(output);
  296. goto fail;
  297. }
  298. av_frame_unref(input);
  299. av_frame_move_ref(input, output);
  300. av_frame_free(&output);
  301. return 0;
  302. fail:
  303. av_frame_free(&output);
  304. return err;
  305. }
  306. static int video_frame_process(DecoderPriv *dp, AVFrame *frame,
  307. unsigned *outputs_mask)
  308. {
  309. #if FFMPEG_OPT_TOP
  310. if (dp->flags & DECODER_FLAG_TOP_FIELD_FIRST) {
  311. av_log(dp, AV_LOG_WARNING, "-top is deprecated, use the setfield filter instead\n");
  312. frame->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
  313. }
  314. #endif
  315. if (frame->format == dp->hwaccel_pix_fmt) {
  316. int err = hwaccel_retrieve_data(dp->dec_ctx, frame);
  317. if (err < 0)
  318. return err;
  319. }
  320. frame->pts = frame->best_effort_timestamp;
  321. // forced fixed framerate
  322. if (dp->flags & DECODER_FLAG_FRAMERATE_FORCED) {
  323. frame->pts = AV_NOPTS_VALUE;
  324. frame->duration = 1;
  325. frame->time_base = av_inv_q(dp->framerate_in);
  326. }
  327. // no timestamp available - extrapolate from previous frame duration
  328. if (frame->pts == AV_NOPTS_VALUE)
  329. frame->pts = dp->last_frame_pts == AV_NOPTS_VALUE ? 0 :
  330. dp->last_frame_pts + dp->last_frame_duration_est;
  331. // update timestamp history
  332. dp->last_frame_duration_est = video_duration_estimate(dp, frame);
  333. dp->last_frame_pts = frame->pts;
  334. dp->last_frame_tb = frame->time_base;
  335. if (debug_ts) {
  336. av_log(dp, AV_LOG_INFO,
  337. "decoder -> pts:%s pts_time:%s "
  338. "pkt_dts:%s pkt_dts_time:%s "
  339. "duration:%s duration_time:%s "
  340. "keyframe:%d frame_type:%d time_base:%d/%d\n",
  341. av_ts2str(frame->pts),
  342. av_ts2timestr(frame->pts, &frame->time_base),
  343. av_ts2str(frame->pkt_dts),
  344. av_ts2timestr(frame->pkt_dts, &frame->time_base),
  345. av_ts2str(frame->duration),
  346. av_ts2timestr(frame->duration, &frame->time_base),
  347. !!(frame->flags & AV_FRAME_FLAG_KEY), frame->pict_type,
  348. frame->time_base.num, frame->time_base.den);
  349. }
  350. if (dp->sar_override.num)
  351. frame->sample_aspect_ratio = dp->sar_override;
  352. if (dp->apply_cropping) {
  353. // lavfi does not require aligned frame data
  354. int ret = av_frame_apply_cropping(frame, AV_FRAME_CROP_UNALIGNED);
  355. if (ret < 0) {
  356. av_log(dp, AV_LOG_ERROR, "Error applying decoder cropping\n");
  357. return ret;
  358. }
  359. }
  360. if (frame->opaque)
  361. *outputs_mask = (uintptr_t)frame->opaque;
  362. return 0;
  363. }
  364. static int copy_av_subtitle(AVSubtitle *dst, const AVSubtitle *src)
  365. {
  366. int ret = AVERROR_BUG;
  367. AVSubtitle tmp = {
  368. .format = src->format,
  369. .start_display_time = src->start_display_time,
  370. .end_display_time = src->end_display_time,
  371. .num_rects = 0,
  372. .rects = NULL,
  373. .pts = src->pts
  374. };
  375. if (!src->num_rects)
  376. goto success;
  377. if (!(tmp.rects = av_calloc(src->num_rects, sizeof(*tmp.rects))))
  378. return AVERROR(ENOMEM);
  379. for (int i = 0; i < src->num_rects; i++) {
  380. AVSubtitleRect *src_rect = src->rects[i];
  381. AVSubtitleRect *dst_rect;
  382. if (!(dst_rect = tmp.rects[i] = av_mallocz(sizeof(*tmp.rects[0])))) {
  383. ret = AVERROR(ENOMEM);
  384. goto cleanup;
  385. }
  386. tmp.num_rects++;
  387. dst_rect->type = src_rect->type;
  388. dst_rect->flags = src_rect->flags;
  389. dst_rect->x = src_rect->x;
  390. dst_rect->y = src_rect->y;
  391. dst_rect->w = src_rect->w;
  392. dst_rect->h = src_rect->h;
  393. dst_rect->nb_colors = src_rect->nb_colors;
  394. if (src_rect->text)
  395. if (!(dst_rect->text = av_strdup(src_rect->text))) {
  396. ret = AVERROR(ENOMEM);
  397. goto cleanup;
  398. }
  399. if (src_rect->ass)
  400. if (!(dst_rect->ass = av_strdup(src_rect->ass))) {
  401. ret = AVERROR(ENOMEM);
  402. goto cleanup;
  403. }
  404. for (int j = 0; j < 4; j++) {
  405. // SUBTITLE_BITMAP images are special in the sense that they
  406. // are like PAL8 images. first pointer to data, second to
  407. // palette. This makes the size calculation match this.
  408. size_t buf_size = src_rect->type == SUBTITLE_BITMAP && j == 1 ?
  409. AVPALETTE_SIZE :
  410. src_rect->h * src_rect->linesize[j];
  411. if (!src_rect->data[j])
  412. continue;
  413. if (!(dst_rect->data[j] = av_memdup(src_rect->data[j], buf_size))) {
  414. ret = AVERROR(ENOMEM);
  415. goto cleanup;
  416. }
  417. dst_rect->linesize[j] = src_rect->linesize[j];
  418. }
  419. }
  420. success:
  421. *dst = tmp;
  422. return 0;
  423. cleanup:
  424. avsubtitle_free(&tmp);
  425. return ret;
  426. }
  427. static void subtitle_free(void *opaque, uint8_t *data)
  428. {
  429. AVSubtitle *sub = (AVSubtitle*)data;
  430. avsubtitle_free(sub);
  431. av_free(sub);
  432. }
  433. static int subtitle_wrap_frame(AVFrame *frame, AVSubtitle *subtitle, int copy)
  434. {
  435. AVBufferRef *buf;
  436. AVSubtitle *sub;
  437. int ret;
  438. if (copy) {
  439. sub = av_mallocz(sizeof(*sub));
  440. ret = sub ? copy_av_subtitle(sub, subtitle) : AVERROR(ENOMEM);
  441. if (ret < 0) {
  442. av_freep(&sub);
  443. return ret;
  444. }
  445. } else {
  446. sub = av_memdup(subtitle, sizeof(*subtitle));
  447. if (!sub)
  448. return AVERROR(ENOMEM);
  449. memset(subtitle, 0, sizeof(*subtitle));
  450. }
  451. buf = av_buffer_create((uint8_t*)sub, sizeof(*sub),
  452. subtitle_free, NULL, 0);
  453. if (!buf) {
  454. avsubtitle_free(sub);
  455. av_freep(&sub);
  456. return AVERROR(ENOMEM);
  457. }
  458. frame->buf[0] = buf;
  459. return 0;
  460. }
  461. static int process_subtitle(DecoderPriv *dp, AVFrame *frame)
  462. {
  463. const AVSubtitle *subtitle = (AVSubtitle*)frame->buf[0]->data;
  464. int ret = 0;
  465. if (dp->flags & DECODER_FLAG_FIX_SUB_DURATION) {
  466. AVSubtitle *sub_prev = dp->sub_prev[0]->buf[0] ?
  467. (AVSubtitle*)dp->sub_prev[0]->buf[0]->data : NULL;
  468. int end = 1;
  469. if (sub_prev) {
  470. end = av_rescale(subtitle->pts - sub_prev->pts,
  471. 1000, AV_TIME_BASE);
  472. if (end < sub_prev->end_display_time) {
  473. av_log(dp, AV_LOG_DEBUG,
  474. "Subtitle duration reduced from %"PRId32" to %d%s\n",
  475. sub_prev->end_display_time, end,
  476. end <= 0 ? ", dropping it" : "");
  477. sub_prev->end_display_time = end;
  478. }
  479. }
  480. av_frame_unref(dp->sub_prev[1]);
  481. av_frame_move_ref(dp->sub_prev[1], frame);
  482. frame = dp->sub_prev[0];
  483. subtitle = frame->buf[0] ? (AVSubtitle*)frame->buf[0]->data : NULL;
  484. FFSWAP(AVFrame*, dp->sub_prev[0], dp->sub_prev[1]);
  485. if (end <= 0)
  486. return 0;
  487. }
  488. if (!subtitle)
  489. return 0;
  490. ret = sch_dec_send(dp->sch, dp->sch_idx, 0, frame);
  491. if (ret < 0)
  492. av_frame_unref(frame);
  493. return ret == AVERROR_EOF ? AVERROR_EXIT : ret;
  494. }
  495. static int fix_sub_duration_heartbeat(DecoderPriv *dp, int64_t signal_pts)
  496. {
  497. int ret = AVERROR_BUG;
  498. AVSubtitle *prev_subtitle = dp->sub_prev[0]->buf[0] ?
  499. (AVSubtitle*)dp->sub_prev[0]->buf[0]->data : NULL;
  500. AVSubtitle *subtitle;
  501. if (!(dp->flags & DECODER_FLAG_FIX_SUB_DURATION) || !prev_subtitle ||
  502. !prev_subtitle->num_rects || signal_pts <= prev_subtitle->pts)
  503. return 0;
  504. av_frame_unref(dp->sub_heartbeat);
  505. ret = subtitle_wrap_frame(dp->sub_heartbeat, prev_subtitle, 1);
  506. if (ret < 0)
  507. return ret;
  508. subtitle = (AVSubtitle*)dp->sub_heartbeat->buf[0]->data;
  509. subtitle->pts = signal_pts;
  510. return process_subtitle(dp, dp->sub_heartbeat);
  511. }
  512. static int transcode_subtitles(DecoderPriv *dp, const AVPacket *pkt,
  513. AVFrame *frame)
  514. {
  515. AVPacket *flush_pkt = NULL;
  516. AVSubtitle subtitle;
  517. int got_output;
  518. int ret;
  519. if (pkt && (intptr_t)pkt->opaque == PKT_OPAQUE_SUB_HEARTBEAT) {
  520. frame->pts = pkt->pts;
  521. frame->time_base = pkt->time_base;
  522. frame->opaque = (void*)(intptr_t)FRAME_OPAQUE_SUB_HEARTBEAT;
  523. ret = sch_dec_send(dp->sch, dp->sch_idx, 0, frame);
  524. return ret == AVERROR_EOF ? AVERROR_EXIT : ret;
  525. } else if (pkt && (intptr_t)pkt->opaque == PKT_OPAQUE_FIX_SUB_DURATION) {
  526. return fix_sub_duration_heartbeat(dp, av_rescale_q(pkt->pts, pkt->time_base,
  527. AV_TIME_BASE_Q));
  528. }
  529. if (!pkt) {
  530. flush_pkt = av_packet_alloc();
  531. if (!flush_pkt)
  532. return AVERROR(ENOMEM);
  533. }
  534. ret = avcodec_decode_subtitle2(dp->dec_ctx, &subtitle, &got_output,
  535. pkt ? pkt : flush_pkt);
  536. av_packet_free(&flush_pkt);
  537. if (ret < 0) {
  538. av_log(dp, AV_LOG_ERROR, "Error decoding subtitles: %s\n",
  539. av_err2str(ret));
  540. dp->dec.decode_errors++;
  541. return exit_on_error ? ret : 0;
  542. }
  543. if (!got_output)
  544. return pkt ? 0 : AVERROR_EOF;
  545. dp->dec.frames_decoded++;
  546. // XXX the queue for transferring data to consumers runs
  547. // on AVFrames, so we wrap AVSubtitle in an AVBufferRef and put that
  548. // inside the frame
  549. // eventually, subtitles should be switched to use AVFrames natively
  550. ret = subtitle_wrap_frame(frame, &subtitle, 0);
  551. if (ret < 0) {
  552. avsubtitle_free(&subtitle);
  553. return ret;
  554. }
  555. frame->width = dp->dec_ctx->width;
  556. frame->height = dp->dec_ctx->height;
  557. return process_subtitle(dp, frame);
  558. }
  559. static int packet_decode(DecoderPriv *dp, AVPacket *pkt, AVFrame *frame)
  560. {
  561. AVCodecContext *dec = dp->dec_ctx;
  562. const char *type_desc = av_get_media_type_string(dec->codec_type);
  563. int ret;
  564. if (dec->codec_type == AVMEDIA_TYPE_SUBTITLE)
  565. return transcode_subtitles(dp, pkt, frame);
  566. // With fate-indeo3-2, we're getting 0-sized packets before EOF for some
  567. // reason. This seems like a semi-critical bug. Don't trigger EOF, and
  568. // skip the packet.
  569. if (pkt && pkt->size == 0)
  570. return 0;
  571. if (pkt && (dp->flags & DECODER_FLAG_TS_UNRELIABLE)) {
  572. pkt->pts = AV_NOPTS_VALUE;
  573. pkt->dts = AV_NOPTS_VALUE;
  574. }
  575. if (pkt) {
  576. FrameData *fd = packet_data(pkt);
  577. if (!fd)
  578. return AVERROR(ENOMEM);
  579. fd->wallclock[LATENCY_PROBE_DEC_PRE] = av_gettime_relative();
  580. }
  581. ret = avcodec_send_packet(dec, pkt);
  582. if (ret < 0 && !(ret == AVERROR_EOF && !pkt)) {
  583. // In particular, we don't expect AVERROR(EAGAIN), because we read all
  584. // decoded frames with avcodec_receive_frame() until done.
  585. if (ret == AVERROR(EAGAIN)) {
  586. av_log(dp, AV_LOG_FATAL, "A decoder returned an unexpected error code. "
  587. "This is a bug, please report it.\n");
  588. return AVERROR_BUG;
  589. }
  590. av_log(dp, AV_LOG_ERROR, "Error submitting %s to decoder: %s\n",
  591. pkt ? "packet" : "EOF", av_err2str(ret));
  592. if (ret != AVERROR_EOF) {
  593. dp->dec.decode_errors++;
  594. if (!exit_on_error)
  595. ret = 0;
  596. }
  597. return ret;
  598. }
  599. while (1) {
  600. FrameData *fd;
  601. unsigned outputs_mask = 1;
  602. av_frame_unref(frame);
  603. update_benchmark(NULL);
  604. ret = avcodec_receive_frame(dec, frame);
  605. update_benchmark("decode_%s %s", type_desc, dp->parent_name);
  606. if (ret == AVERROR(EAGAIN)) {
  607. av_assert0(pkt); // should never happen during flushing
  608. return 0;
  609. } else if (ret == AVERROR_EOF) {
  610. return ret;
  611. } else if (ret < 0) {
  612. av_log(dp, AV_LOG_ERROR, "Decoding error: %s\n", av_err2str(ret));
  613. dp->dec.decode_errors++;
  614. if (exit_on_error)
  615. return ret;
  616. continue;
  617. }
  618. if (frame->decode_error_flags || (frame->flags & AV_FRAME_FLAG_CORRUPT)) {
  619. av_log(dp, exit_on_error ? AV_LOG_FATAL : AV_LOG_WARNING,
  620. "corrupt decoded frame\n");
  621. if (exit_on_error)
  622. return AVERROR_INVALIDDATA;
  623. }
  624. fd = frame_data(frame);
  625. if (!fd) {
  626. av_frame_unref(frame);
  627. return AVERROR(ENOMEM);
  628. }
  629. fd->dec.pts = frame->pts;
  630. fd->dec.tb = dec->pkt_timebase;
  631. fd->dec.frame_num = dec->frame_num - 1;
  632. fd->bits_per_raw_sample = dec->bits_per_raw_sample;
  633. fd->wallclock[LATENCY_PROBE_DEC_POST] = av_gettime_relative();
  634. frame->time_base = dec->pkt_timebase;
  635. ret = clone_side_data(&frame->side_data, &frame->nb_side_data,
  636. dec->decoded_side_data, dec->nb_decoded_side_data, 0);
  637. if (ret < 0)
  638. return ret;
  639. if (dec->codec_type == AVMEDIA_TYPE_AUDIO) {
  640. dp->dec.samples_decoded += frame->nb_samples;
  641. audio_ts_process(dp, frame);
  642. } else {
  643. ret = video_frame_process(dp, frame, &outputs_mask);
  644. if (ret < 0) {
  645. av_log(dp, AV_LOG_FATAL,
  646. "Error while processing the decoded data\n");
  647. return ret;
  648. }
  649. }
  650. dp->dec.frames_decoded++;
  651. for (int i = 0; i < stdc_count_ones(outputs_mask); i++) {
  652. AVFrame *to_send = frame;
  653. int pos;
  654. av_assert0(outputs_mask);
  655. pos = stdc_trailing_zeros(outputs_mask);
  656. outputs_mask &= ~(1U << pos);
  657. // this is not the last output and sch_dec_send() consumes the frame
  658. // given to it, so make a temporary reference
  659. if (outputs_mask) {
  660. to_send = dp->frame_tmp_ref;
  661. ret = av_frame_ref(to_send, frame);
  662. if (ret < 0)
  663. return ret;
  664. }
  665. ret = sch_dec_send(dp->sch, dp->sch_idx, pos, to_send);
  666. if (ret < 0) {
  667. av_frame_unref(to_send);
  668. return ret == AVERROR_EOF ? AVERROR_EXIT : ret;
  669. }
  670. }
  671. }
  672. }
  673. static int dec_open(DecoderPriv *dp, AVDictionary **dec_opts,
  674. const DecoderOpts *o, AVFrame *param_out);
  675. static int dec_standalone_open(DecoderPriv *dp, const AVPacket *pkt)
  676. {
  677. DecoderOpts o;
  678. const FrameData *fd;
  679. char name[16];
  680. if (!pkt->opaque_ref)
  681. return AVERROR_BUG;
  682. fd = (FrameData *)pkt->opaque_ref->data;
  683. if (!fd->par_enc)
  684. return AVERROR_BUG;
  685. memset(&o, 0, sizeof(o));
  686. o.par = fd->par_enc;
  687. o.time_base = pkt->time_base;
  688. o.codec = dp->standalone_init.codec;
  689. if (!o.codec)
  690. o.codec = avcodec_find_decoder(o.par->codec_id);
  691. if (!o.codec) {
  692. const AVCodecDescriptor *desc = avcodec_descriptor_get(o.par->codec_id);
  693. av_log(dp, AV_LOG_ERROR, "Cannot find a decoder for codec ID '%s'\n",
  694. desc ? desc->name : "?");
  695. return AVERROR_DECODER_NOT_FOUND;
  696. }
  697. snprintf(name, sizeof(name), "dec%d", dp->index);
  698. o.name = name;
  699. return dec_open(dp, &dp->standalone_init.opts, &o, NULL);
  700. }
  701. static void dec_thread_set_name(const DecoderPriv *dp)
  702. {
  703. char name[16] = "dec";
  704. if (dp->index >= 0)
  705. av_strlcatf(name, sizeof(name), "%d", dp->index);
  706. else if (dp->parent_name)
  707. av_strlcat(name, dp->parent_name, sizeof(name));
  708. if (dp->dec_ctx)
  709. av_strlcatf(name, sizeof(name), ":%s", dp->dec_ctx->codec->name);
  710. ff_thread_setname(name);
  711. }
  712. static void dec_thread_uninit(DecThreadContext *dt)
  713. {
  714. av_packet_free(&dt->pkt);
  715. av_frame_free(&dt->frame);
  716. memset(dt, 0, sizeof(*dt));
  717. }
  718. static int dec_thread_init(DecThreadContext *dt)
  719. {
  720. memset(dt, 0, sizeof(*dt));
  721. dt->frame = av_frame_alloc();
  722. if (!dt->frame)
  723. goto fail;
  724. dt->pkt = av_packet_alloc();
  725. if (!dt->pkt)
  726. goto fail;
  727. return 0;
  728. fail:
  729. dec_thread_uninit(dt);
  730. return AVERROR(ENOMEM);
  731. }
  732. static int decoder_thread(void *arg)
  733. {
  734. DecoderPriv *dp = arg;
  735. DecThreadContext dt;
  736. int ret = 0, input_status = 0;
  737. ret = dec_thread_init(&dt);
  738. if (ret < 0)
  739. goto finish;
  740. dec_thread_set_name(dp);
  741. while (!input_status) {
  742. int flush_buffers, have_data;
  743. input_status = sch_dec_receive(dp->sch, dp->sch_idx, dt.pkt);
  744. have_data = input_status >= 0 &&
  745. (dt.pkt->buf || dt.pkt->side_data_elems ||
  746. (intptr_t)dt.pkt->opaque == PKT_OPAQUE_SUB_HEARTBEAT ||
  747. (intptr_t)dt.pkt->opaque == PKT_OPAQUE_FIX_SUB_DURATION);
  748. flush_buffers = input_status >= 0 && !have_data;
  749. if (!have_data)
  750. av_log(dp, AV_LOG_VERBOSE, "Decoder thread received %s packet\n",
  751. flush_buffers ? "flush" : "EOF");
  752. // this is a standalone decoder that has not been initialized yet
  753. if (!dp->dec_ctx) {
  754. if (flush_buffers)
  755. continue;
  756. if (input_status < 0) {
  757. av_log(dp, AV_LOG_ERROR,
  758. "Cannot initialize a standalone decoder\n");
  759. ret = input_status;
  760. goto finish;
  761. }
  762. ret = dec_standalone_open(dp, dt.pkt);
  763. if (ret < 0)
  764. goto finish;
  765. }
  766. ret = packet_decode(dp, have_data ? dt.pkt : NULL, dt.frame);
  767. av_packet_unref(dt.pkt);
  768. av_frame_unref(dt.frame);
  769. // AVERROR_EOF - EOF from the decoder
  770. // AVERROR_EXIT - EOF from the scheduler
  771. // we treat them differently when flushing
  772. if (ret == AVERROR_EXIT) {
  773. ret = AVERROR_EOF;
  774. flush_buffers = 0;
  775. }
  776. if (ret == AVERROR_EOF) {
  777. av_log(dp, AV_LOG_VERBOSE, "Decoder returned EOF, %s\n",
  778. flush_buffers ? "resetting" : "finishing");
  779. if (!flush_buffers)
  780. break;
  781. /* report last frame duration to the scheduler */
  782. if (dp->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
  783. dt.pkt->pts = dp->last_frame_pts + dp->last_frame_duration_est;
  784. dt.pkt->time_base = dp->last_frame_tb;
  785. }
  786. avcodec_flush_buffers(dp->dec_ctx);
  787. } else if (ret < 0) {
  788. av_log(dp, AV_LOG_ERROR, "Error processing packet in decoder: %s\n",
  789. av_err2str(ret));
  790. break;
  791. }
  792. }
  793. // EOF is normal thread termination
  794. if (ret == AVERROR_EOF)
  795. ret = 0;
  796. // on success send EOF timestamp to our downstreams
  797. if (ret >= 0) {
  798. float err_rate;
  799. av_frame_unref(dt.frame);
  800. dt.frame->opaque = (void*)(intptr_t)FRAME_OPAQUE_EOF;
  801. dt.frame->pts = dp->last_frame_pts == AV_NOPTS_VALUE ? AV_NOPTS_VALUE :
  802. dp->last_frame_pts + dp->last_frame_duration_est;
  803. dt.frame->time_base = dp->last_frame_tb;
  804. ret = sch_dec_send(dp->sch, dp->sch_idx, 0, dt.frame);
  805. if (ret < 0 && ret != AVERROR_EOF) {
  806. av_log(dp, AV_LOG_FATAL,
  807. "Error signalling EOF timestamp: %s\n", av_err2str(ret));
  808. goto finish;
  809. }
  810. ret = 0;
  811. err_rate = (dp->dec.frames_decoded || dp->dec.decode_errors) ?
  812. dp->dec.decode_errors / (dp->dec.frames_decoded + dp->dec.decode_errors) : 0.f;
  813. if (err_rate > max_error_rate) {
  814. av_log(dp, AV_LOG_FATAL, "Decode error rate %g exceeds maximum %g\n",
  815. err_rate, max_error_rate);
  816. ret = FFMPEG_ERROR_RATE_EXCEEDED;
  817. } else if (err_rate)
  818. av_log(dp, AV_LOG_VERBOSE, "Decode error rate %g\n", err_rate);
  819. }
  820. finish:
  821. dec_thread_uninit(&dt);
  822. return ret;
  823. }
  824. int dec_request_view(Decoder *d, const ViewSpecifier *vs,
  825. SchedulerNode *src)
  826. {
  827. DecoderPriv *dp = dp_from_dec(d);
  828. unsigned out_idx = 0;
  829. int ret;
  830. if (dp->multiview_user_config) {
  831. if (!vs || vs->type == VIEW_SPECIFIER_TYPE_NONE) {
  832. *src = SCH_DEC_OUT(dp->sch_idx, 0);
  833. return 0;
  834. }
  835. av_log(dp, AV_LOG_ERROR,
  836. "Manually selecting views with -view_ids cannot be combined "
  837. "with view selection via stream specifiers. It is strongly "
  838. "recommended you always use stream specifiers only.\n");
  839. return AVERROR(EINVAL);
  840. }
  841. // when multiview_user_config is not set, NONE specifier is treated
  842. // as requesting the base view
  843. vs = (vs && vs->type != VIEW_SPECIFIER_TYPE_NONE) ? vs :
  844. &(ViewSpecifier){ .type = VIEW_SPECIFIER_TYPE_IDX, .val = 0 };
  845. // check if the specifier matches an already-existing one
  846. for (int i = 0; i < dp->nb_views_requested; i++) {
  847. const ViewSpecifier *vs1 = &dp->views_requested[i].vs;
  848. if (vs->type == vs1->type &&
  849. (vs->type == VIEW_SPECIFIER_TYPE_ALL || vs->val == vs1->val)) {
  850. *src = SCH_DEC_OUT(dp->sch_idx, dp->views_requested[i].out_idx);
  851. return 0;
  852. }
  853. }
  854. // we use a bitmask to map view IDs to decoder outputs, which
  855. // limits the number of outputs allowed
  856. if (dp->nb_views_requested >= sizeof(dp->view_map[0].out_mask) * 8) {
  857. av_log(dp, AV_LOG_ERROR, "Too many view specifiers\n");
  858. return AVERROR(ENOSYS);
  859. }
  860. ret = GROW_ARRAY(dp->views_requested, dp->nb_views_requested);
  861. if (ret < 0)
  862. return ret;
  863. if (dp->nb_views_requested > 1) {
  864. ret = sch_add_dec_output(dp->sch, dp->sch_idx);
  865. if (ret < 0)
  866. return ret;
  867. out_idx = ret;
  868. }
  869. dp->views_requested[dp->nb_views_requested - 1].out_idx = out_idx;
  870. dp->views_requested[dp->nb_views_requested - 1].vs = *vs;
  871. *src = SCH_DEC_OUT(dp->sch_idx,
  872. dp->views_requested[dp->nb_views_requested - 1].out_idx);
  873. return 0;
  874. }
  875. static int multiview_setup(DecoderPriv *dp, AVCodecContext *dec_ctx)
  876. {
  877. unsigned views_wanted = 0;
  878. unsigned nb_view_ids_av, nb_view_ids;
  879. unsigned *view_ids_av = NULL, *view_pos_av = NULL;
  880. int *view_ids = NULL;
  881. int ret;
  882. // no views/only base view were requested - do nothing
  883. if (!dp->nb_views_requested ||
  884. (dp->nb_views_requested == 1 &&
  885. dp->views_requested[0].vs.type == VIEW_SPECIFIER_TYPE_IDX &&
  886. dp->views_requested[0].vs.val == 0))
  887. return 0;
  888. av_freep(&dp->view_map);
  889. dp->nb_view_map = 0;
  890. // retrieve views available in current CVS
  891. ret = av_opt_get_array_size(dec_ctx, "view_ids_available",
  892. AV_OPT_SEARCH_CHILDREN, &nb_view_ids_av);
  893. if (ret < 0) {
  894. av_log(dp, AV_LOG_ERROR,
  895. "Multiview decoding requested, but decoder '%s' does not "
  896. "support it\n", dec_ctx->codec->name);
  897. return AVERROR(ENOSYS);
  898. }
  899. if (nb_view_ids_av) {
  900. unsigned nb_view_pos_av;
  901. if (nb_view_ids_av >= sizeof(views_wanted) * 8) {
  902. av_log(dp, AV_LOG_ERROR, "Too many views in video: %u\n", nb_view_ids_av);
  903. ret = AVERROR(ENOSYS);
  904. goto fail;
  905. }
  906. view_ids_av = av_calloc(nb_view_ids_av, sizeof(*view_ids_av));
  907. if (!view_ids_av) {
  908. ret = AVERROR(ENOMEM);
  909. goto fail;
  910. }
  911. ret = av_opt_get_array(dec_ctx, "view_ids_available",
  912. AV_OPT_SEARCH_CHILDREN, 0, nb_view_ids_av,
  913. AV_OPT_TYPE_UINT, view_ids_av);
  914. if (ret < 0)
  915. goto fail;
  916. ret = av_opt_get_array_size(dec_ctx, "view_pos_available",
  917. AV_OPT_SEARCH_CHILDREN, &nb_view_pos_av);
  918. if (ret >= 0 && nb_view_pos_av == nb_view_ids_av) {
  919. view_pos_av = av_calloc(nb_view_ids_av, sizeof(*view_pos_av));
  920. if (!view_pos_av) {
  921. ret = AVERROR(ENOMEM);
  922. goto fail;
  923. }
  924. ret = av_opt_get_array(dec_ctx, "view_pos_available",
  925. AV_OPT_SEARCH_CHILDREN, 0, nb_view_ids_av,
  926. AV_OPT_TYPE_UINT, view_pos_av);
  927. if (ret < 0)
  928. goto fail;
  929. }
  930. } else {
  931. // assume there is a single view with ID=0
  932. nb_view_ids_av = 1;
  933. view_ids_av = av_calloc(nb_view_ids_av, sizeof(*view_ids_av));
  934. view_pos_av = av_calloc(nb_view_ids_av, sizeof(*view_pos_av));
  935. if (!view_ids_av || !view_pos_av) {
  936. ret = AVERROR(ENOMEM);
  937. goto fail;
  938. }
  939. view_pos_av[0] = AV_STEREO3D_VIEW_UNSPEC;
  940. }
  941. dp->view_map = av_calloc(nb_view_ids_av, sizeof(*dp->view_map));
  942. if (!dp->view_map) {
  943. ret = AVERROR(ENOMEM);
  944. goto fail;
  945. }
  946. dp->nb_view_map = nb_view_ids_av;
  947. for (int i = 0; i < dp->nb_view_map; i++)
  948. dp->view_map[i].id = view_ids_av[i];
  949. // figure out which views should go to which output
  950. for (int i = 0; i < dp->nb_views_requested; i++) {
  951. const ViewSpecifier *vs = &dp->views_requested[i].vs;
  952. switch (vs->type) {
  953. case VIEW_SPECIFIER_TYPE_IDX:
  954. if (vs->val >= nb_view_ids_av) {
  955. av_log(dp, exit_on_error ? AV_LOG_ERROR : AV_LOG_WARNING,
  956. "View with index %u requested, but only %u views available "
  957. "in current video sequence (more views may or may not be "
  958. "available in later sequences).\n",
  959. vs->val, nb_view_ids_av);
  960. if (exit_on_error) {
  961. ret = AVERROR(EINVAL);
  962. goto fail;
  963. }
  964. continue;
  965. }
  966. views_wanted |= 1U << vs->val;
  967. dp->view_map[vs->val].out_mask |= 1ULL << i;
  968. break;
  969. case VIEW_SPECIFIER_TYPE_ID: {
  970. int view_idx = -1;
  971. for (unsigned j = 0; j < nb_view_ids_av; j++) {
  972. if (view_ids_av[j] == vs->val) {
  973. view_idx = j;
  974. break;
  975. }
  976. }
  977. if (view_idx < 0) {
  978. av_log(dp, exit_on_error ? AV_LOG_ERROR : AV_LOG_WARNING,
  979. "View with ID %u requested, but is not available "
  980. "in the video sequence\n", vs->val);
  981. if (exit_on_error) {
  982. ret = AVERROR(EINVAL);
  983. goto fail;
  984. }
  985. continue;
  986. }
  987. views_wanted |= 1U << view_idx;
  988. dp->view_map[view_idx].out_mask |= 1ULL << i;
  989. break;
  990. }
  991. case VIEW_SPECIFIER_TYPE_POS: {
  992. int view_idx = -1;
  993. for (unsigned j = 0; view_pos_av && j < nb_view_ids_av; j++) {
  994. if (view_pos_av[j] == vs->val) {
  995. view_idx = j;
  996. break;
  997. }
  998. }
  999. if (view_idx < 0) {
  1000. av_log(dp, exit_on_error ? AV_LOG_ERROR : AV_LOG_WARNING,
  1001. "View position '%s' requested, but is not available "
  1002. "in the video sequence\n", av_stereo3d_view_name(vs->val));
  1003. if (exit_on_error) {
  1004. ret = AVERROR(EINVAL);
  1005. goto fail;
  1006. }
  1007. continue;
  1008. }
  1009. views_wanted |= 1U << view_idx;
  1010. dp->view_map[view_idx].out_mask |= 1ULL << i;
  1011. break;
  1012. }
  1013. case VIEW_SPECIFIER_TYPE_ALL:
  1014. views_wanted |= (1U << nb_view_ids_av) - 1;
  1015. for (int j = 0; j < dp->nb_view_map; j++)
  1016. dp->view_map[j].out_mask |= 1ULL << i;
  1017. break;
  1018. }
  1019. }
  1020. if (!views_wanted) {
  1021. av_log(dp, AV_LOG_ERROR, "No views were selected for decoding\n");
  1022. ret = AVERROR(EINVAL);
  1023. goto fail;
  1024. }
  1025. // signal to decoder which views we want
  1026. nb_view_ids = stdc_count_ones(views_wanted);
  1027. view_ids = av_malloc_array(nb_view_ids, sizeof(*view_ids));
  1028. if (!view_ids) {
  1029. ret = AVERROR(ENOMEM);
  1030. goto fail;
  1031. }
  1032. for (unsigned i = 0; i < nb_view_ids; i++) {
  1033. int pos;
  1034. av_assert0(views_wanted);
  1035. pos = stdc_trailing_zeros(views_wanted);
  1036. views_wanted &= ~(1U << pos);
  1037. view_ids[i] = view_ids_av[pos];
  1038. }
  1039. // unset view_ids in case we set it earlier
  1040. av_opt_set(dec_ctx, "view_ids", NULL, AV_OPT_SEARCH_CHILDREN);
  1041. ret = av_opt_set_array(dec_ctx, "view_ids", AV_OPT_SEARCH_CHILDREN,
  1042. 0, nb_view_ids, AV_OPT_TYPE_INT, view_ids);
  1043. if (ret < 0)
  1044. goto fail;
  1045. if (!dp->frame_tmp_ref) {
  1046. dp->frame_tmp_ref = av_frame_alloc();
  1047. if (!dp->frame_tmp_ref) {
  1048. ret = AVERROR(ENOMEM);
  1049. goto fail;
  1050. }
  1051. }
  1052. fail:
  1053. av_freep(&view_ids_av);
  1054. av_freep(&view_pos_av);
  1055. av_freep(&view_ids);
  1056. return ret;
  1057. }
  1058. static void multiview_check_manual(DecoderPriv *dp, const AVDictionary *dec_opts)
  1059. {
  1060. if (av_dict_get(dec_opts, "view_ids", NULL, 0)) {
  1061. av_log(dp, AV_LOG_WARNING, "Manually selecting views with -view_ids "
  1062. "is not recommended, use view specifiers instead\n");
  1063. dp->multiview_user_config = 1;
  1064. }
  1065. }
  1066. static enum AVPixelFormat get_format(AVCodecContext *s, const enum AVPixelFormat *pix_fmts)
  1067. {
  1068. DecoderPriv *dp = s->opaque;
  1069. const enum AVPixelFormat *p;
  1070. int ret;
  1071. ret = multiview_setup(dp, s);
  1072. if (ret < 0) {
  1073. av_log(dp, AV_LOG_ERROR, "Error setting up multiview decoding: %s\n",
  1074. av_err2str(ret));
  1075. return AV_PIX_FMT_NONE;
  1076. }
  1077. for (p = pix_fmts; *p != AV_PIX_FMT_NONE; p++) {
  1078. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(*p);
  1079. const AVCodecHWConfig *config = NULL;
  1080. if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
  1081. break;
  1082. if (dp->hwaccel_id == HWACCEL_GENERIC ||
  1083. dp->hwaccel_id == HWACCEL_AUTO) {
  1084. for (int i = 0;; i++) {
  1085. config = avcodec_get_hw_config(s->codec, i);
  1086. if (!config)
  1087. break;
  1088. if (!(config->methods &
  1089. AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
  1090. continue;
  1091. if (config->pix_fmt == *p)
  1092. break;
  1093. }
  1094. }
  1095. if (config && config->device_type == dp->hwaccel_device_type) {
  1096. dp->hwaccel_pix_fmt = *p;
  1097. break;
  1098. }
  1099. }
  1100. return *p;
  1101. }
  1102. static int get_buffer(AVCodecContext *dec_ctx, AVFrame *frame, int flags)
  1103. {
  1104. DecoderPriv *dp = dec_ctx->opaque;
  1105. // for multiview video, store the output mask in frame opaque
  1106. if (dp->nb_view_map) {
  1107. const AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_VIEW_ID);
  1108. int view_id = sd ? *(int*)sd->data : 0;
  1109. for (int i = 0; i < dp->nb_view_map; i++) {
  1110. if (dp->view_map[i].id == view_id) {
  1111. frame->opaque = (void*)dp->view_map[i].out_mask;
  1112. break;
  1113. }
  1114. }
  1115. }
  1116. return avcodec_default_get_buffer2(dec_ctx, frame, flags);
  1117. }
  1118. static HWDevice *hw_device_match_by_codec(const AVCodec *codec)
  1119. {
  1120. const AVCodecHWConfig *config;
  1121. HWDevice *dev;
  1122. for (int i = 0;; i++) {
  1123. config = avcodec_get_hw_config(codec, i);
  1124. if (!config)
  1125. return NULL;
  1126. if (!(config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
  1127. continue;
  1128. dev = hw_device_get_by_type(config->device_type);
  1129. if (dev)
  1130. return dev;
  1131. }
  1132. }
  1133. static int hw_device_setup_for_decode(DecoderPriv *dp,
  1134. const AVCodec *codec,
  1135. const char *hwaccel_device)
  1136. {
  1137. const AVCodecHWConfig *config;
  1138. enum AVHWDeviceType type;
  1139. HWDevice *dev = NULL;
  1140. int err, auto_device = 0;
  1141. if (hwaccel_device) {
  1142. dev = hw_device_get_by_name(hwaccel_device);
  1143. if (!dev) {
  1144. if (dp->hwaccel_id == HWACCEL_AUTO) {
  1145. auto_device = 1;
  1146. } else if (dp->hwaccel_id == HWACCEL_GENERIC) {
  1147. type = dp->hwaccel_device_type;
  1148. err = hw_device_init_from_type(type, hwaccel_device,
  1149. &dev);
  1150. } else {
  1151. // This will be dealt with by API-specific initialisation
  1152. // (using hwaccel_device), so nothing further needed here.
  1153. return 0;
  1154. }
  1155. } else {
  1156. if (dp->hwaccel_id == HWACCEL_AUTO) {
  1157. dp->hwaccel_device_type = dev->type;
  1158. } else if (dp->hwaccel_device_type != dev->type) {
  1159. av_log(dp, AV_LOG_ERROR, "Invalid hwaccel device "
  1160. "specified for decoder: device %s of type %s is not "
  1161. "usable with hwaccel %s.\n", dev->name,
  1162. av_hwdevice_get_type_name(dev->type),
  1163. av_hwdevice_get_type_name(dp->hwaccel_device_type));
  1164. return AVERROR(EINVAL);
  1165. }
  1166. }
  1167. } else {
  1168. if (dp->hwaccel_id == HWACCEL_AUTO) {
  1169. auto_device = 1;
  1170. } else if (dp->hwaccel_id == HWACCEL_GENERIC) {
  1171. type = dp->hwaccel_device_type;
  1172. dev = hw_device_get_by_type(type);
  1173. // When "-qsv_device device" is used, an internal QSV device named
  1174. // as "__qsv_device" is created. Another QSV device is created too
  1175. // if "-init_hw_device qsv=name:device" is used. There are 2 QSV devices
  1176. // if both "-qsv_device device" and "-init_hw_device qsv=name:device"
  1177. // are used, hw_device_get_by_type(AV_HWDEVICE_TYPE_QSV) returns NULL.
  1178. // To keep back-compatibility with the removed ad-hoc libmfx setup code,
  1179. // call hw_device_get_by_name("__qsv_device") to select the internal QSV
  1180. // device.
  1181. if (!dev && type == AV_HWDEVICE_TYPE_QSV)
  1182. dev = hw_device_get_by_name("__qsv_device");
  1183. if (!dev)
  1184. err = hw_device_init_from_type(type, NULL, &dev);
  1185. } else {
  1186. dev = hw_device_match_by_codec(codec);
  1187. if (!dev) {
  1188. // No device for this codec, but not using generic hwaccel
  1189. // and therefore may well not need one - ignore.
  1190. return 0;
  1191. }
  1192. }
  1193. }
  1194. if (auto_device) {
  1195. if (!avcodec_get_hw_config(codec, 0)) {
  1196. // Decoder does not support any hardware devices.
  1197. return 0;
  1198. }
  1199. for (int i = 0; !dev; i++) {
  1200. config = avcodec_get_hw_config(codec, i);
  1201. if (!config)
  1202. break;
  1203. type = config->device_type;
  1204. dev = hw_device_get_by_type(type);
  1205. if (dev) {
  1206. av_log(dp, AV_LOG_INFO, "Using auto "
  1207. "hwaccel type %s with existing device %s.\n",
  1208. av_hwdevice_get_type_name(type), dev->name);
  1209. }
  1210. }
  1211. for (int i = 0; !dev; i++) {
  1212. config = avcodec_get_hw_config(codec, i);
  1213. if (!config)
  1214. break;
  1215. type = config->device_type;
  1216. // Try to make a new device of this type.
  1217. err = hw_device_init_from_type(type, hwaccel_device,
  1218. &dev);
  1219. if (err < 0) {
  1220. // Can't make a device of this type.
  1221. continue;
  1222. }
  1223. if (hwaccel_device) {
  1224. av_log(dp, AV_LOG_INFO, "Using auto "
  1225. "hwaccel type %s with new device created "
  1226. "from %s.\n", av_hwdevice_get_type_name(type),
  1227. hwaccel_device);
  1228. } else {
  1229. av_log(dp, AV_LOG_INFO, "Using auto "
  1230. "hwaccel type %s with new default device.\n",
  1231. av_hwdevice_get_type_name(type));
  1232. }
  1233. }
  1234. if (dev) {
  1235. dp->hwaccel_device_type = type;
  1236. } else {
  1237. av_log(dp, AV_LOG_INFO, "Auto hwaccel "
  1238. "disabled: no device found.\n");
  1239. dp->hwaccel_id = HWACCEL_NONE;
  1240. return 0;
  1241. }
  1242. }
  1243. if (!dev) {
  1244. av_log(dp, AV_LOG_ERROR, "No device available "
  1245. "for decoder: device type %s needed for codec %s.\n",
  1246. av_hwdevice_get_type_name(type), codec->name);
  1247. return err;
  1248. }
  1249. dp->dec_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
  1250. if (!dp->dec_ctx->hw_device_ctx)
  1251. return AVERROR(ENOMEM);
  1252. return 0;
  1253. }
  1254. static int dec_open(DecoderPriv *dp, AVDictionary **dec_opts,
  1255. const DecoderOpts *o, AVFrame *param_out)
  1256. {
  1257. const AVCodec *codec = o->codec;
  1258. int ret;
  1259. dp->flags = o->flags;
  1260. dp->log_parent = o->log_parent;
  1261. dp->dec.type = codec->type;
  1262. dp->framerate_in = o->framerate;
  1263. dp->hwaccel_id = o->hwaccel_id;
  1264. dp->hwaccel_device_type = o->hwaccel_device_type;
  1265. dp->hwaccel_output_format = o->hwaccel_output_format;
  1266. snprintf(dp->log_name, sizeof(dp->log_name), "dec:%s", codec->name);
  1267. dp->parent_name = av_strdup(o->name ? o->name : "");
  1268. if (!dp->parent_name)
  1269. return AVERROR(ENOMEM);
  1270. if (codec->type == AVMEDIA_TYPE_SUBTITLE &&
  1271. (dp->flags & DECODER_FLAG_FIX_SUB_DURATION)) {
  1272. for (int i = 0; i < FF_ARRAY_ELEMS(dp->sub_prev); i++) {
  1273. dp->sub_prev[i] = av_frame_alloc();
  1274. if (!dp->sub_prev[i])
  1275. return AVERROR(ENOMEM);
  1276. }
  1277. dp->sub_heartbeat = av_frame_alloc();
  1278. if (!dp->sub_heartbeat)
  1279. return AVERROR(ENOMEM);
  1280. }
  1281. dp->sar_override = o->par->sample_aspect_ratio;
  1282. dp->dec_ctx = avcodec_alloc_context3(codec);
  1283. if (!dp->dec_ctx)
  1284. return AVERROR(ENOMEM);
  1285. ret = avcodec_parameters_to_context(dp->dec_ctx, o->par);
  1286. if (ret < 0) {
  1287. av_log(dp, AV_LOG_ERROR, "Error initializing the decoder context.\n");
  1288. return ret;
  1289. }
  1290. dp->dec_ctx->opaque = dp;
  1291. dp->dec_ctx->get_format = get_format;
  1292. dp->dec_ctx->get_buffer2 = get_buffer;
  1293. dp->dec_ctx->pkt_timebase = o->time_base;
  1294. if (!av_dict_get(*dec_opts, "threads", NULL, 0))
  1295. av_dict_set(dec_opts, "threads", "auto", 0);
  1296. ret = hw_device_setup_for_decode(dp, codec, o->hwaccel_device);
  1297. if (ret < 0) {
  1298. av_log(dp, AV_LOG_ERROR,
  1299. "Hardware device setup failed for decoder: %s\n",
  1300. av_err2str(ret));
  1301. return ret;
  1302. }
  1303. ret = av_opt_set_dict2(dp->dec_ctx, dec_opts, AV_OPT_SEARCH_CHILDREN);
  1304. if (ret < 0) {
  1305. av_log(dp, AV_LOG_ERROR, "Error applying decoder options: %s\n",
  1306. av_err2str(ret));
  1307. return ret;
  1308. }
  1309. ret = check_avoptions(*dec_opts);
  1310. if (ret < 0)
  1311. return ret;
  1312. dp->dec_ctx->flags |= AV_CODEC_FLAG_COPY_OPAQUE;
  1313. if (o->flags & DECODER_FLAG_BITEXACT)
  1314. dp->dec_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
  1315. // we apply cropping outselves
  1316. dp->apply_cropping = dp->dec_ctx->apply_cropping;
  1317. dp->dec_ctx->apply_cropping = 0;
  1318. if ((ret = avcodec_open2(dp->dec_ctx, codec, NULL)) < 0) {
  1319. av_log(dp, AV_LOG_ERROR, "Error while opening decoder: %s\n",
  1320. av_err2str(ret));
  1321. return ret;
  1322. }
  1323. if (dp->dec_ctx->hw_device_ctx) {
  1324. // Update decoder extra_hw_frames option to account for the
  1325. // frames held in queues inside the ffmpeg utility. This is
  1326. // called after avcodec_open2() because the user-set value of
  1327. // extra_hw_frames becomes valid in there, and we need to add
  1328. // this on top of it.
  1329. int extra_frames = DEFAULT_FRAME_THREAD_QUEUE_SIZE;
  1330. if (dp->dec_ctx->extra_hw_frames >= 0)
  1331. dp->dec_ctx->extra_hw_frames += extra_frames;
  1332. else
  1333. dp->dec_ctx->extra_hw_frames = extra_frames;
  1334. }
  1335. dp->dec.subtitle_header = dp->dec_ctx->subtitle_header;
  1336. dp->dec.subtitle_header_size = dp->dec_ctx->subtitle_header_size;
  1337. if (param_out) {
  1338. if (dp->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
  1339. param_out->format = dp->dec_ctx->sample_fmt;
  1340. param_out->sample_rate = dp->dec_ctx->sample_rate;
  1341. ret = av_channel_layout_copy(&param_out->ch_layout, &dp->dec_ctx->ch_layout);
  1342. if (ret < 0)
  1343. return ret;
  1344. } else if (dp->dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  1345. param_out->format = dp->dec_ctx->pix_fmt;
  1346. param_out->width = dp->dec_ctx->width;
  1347. param_out->height = dp->dec_ctx->height;
  1348. param_out->sample_aspect_ratio = dp->dec_ctx->sample_aspect_ratio;
  1349. param_out->colorspace = dp->dec_ctx->colorspace;
  1350. param_out->color_range = dp->dec_ctx->color_range;
  1351. }
  1352. av_frame_side_data_free(&param_out->side_data, &param_out->nb_side_data);
  1353. ret = clone_side_data(&param_out->side_data, &param_out->nb_side_data,
  1354. dp->dec_ctx->decoded_side_data, dp->dec_ctx->nb_decoded_side_data, 0);
  1355. if (ret < 0)
  1356. return ret;
  1357. param_out->time_base = dp->dec_ctx->pkt_timebase;
  1358. }
  1359. return 0;
  1360. }
  1361. int dec_init(Decoder **pdec, Scheduler *sch,
  1362. AVDictionary **dec_opts, const DecoderOpts *o,
  1363. AVFrame *param_out)
  1364. {
  1365. DecoderPriv *dp;
  1366. int ret;
  1367. *pdec = NULL;
  1368. ret = dec_alloc(&dp, sch, !!(o->flags & DECODER_FLAG_SEND_END_TS));
  1369. if (ret < 0)
  1370. return ret;
  1371. multiview_check_manual(dp, *dec_opts);
  1372. ret = dec_open(dp, dec_opts, o, param_out);
  1373. if (ret < 0)
  1374. goto fail;
  1375. *pdec = &dp->dec;
  1376. return dp->sch_idx;
  1377. fail:
  1378. dec_free((Decoder**)&dp);
  1379. return ret;
  1380. }
  1381. int dec_create(const OptionsContext *o, const char *arg, Scheduler *sch)
  1382. {
  1383. DecoderPriv *dp;
  1384. OutputFile *of;
  1385. OutputStream *ost;
  1386. int of_index, ost_index;
  1387. char *p;
  1388. unsigned enc_idx;
  1389. int ret;
  1390. ret = dec_alloc(&dp, sch, 0);
  1391. if (ret < 0)
  1392. return ret;
  1393. dp->index = nb_decoders;
  1394. ret = GROW_ARRAY(decoders, nb_decoders);
  1395. if (ret < 0) {
  1396. dec_free((Decoder **)&dp);
  1397. return ret;
  1398. }
  1399. decoders[nb_decoders - 1] = (Decoder *)dp;
  1400. of_index = strtol(arg, &p, 0);
  1401. if (of_index < 0 || of_index >= nb_output_files) {
  1402. av_log(dp, AV_LOG_ERROR, "Invalid output file index '%d' in %s\n", of_index, arg);
  1403. return AVERROR(EINVAL);
  1404. }
  1405. of = output_files[of_index];
  1406. ost_index = strtol(p + 1, NULL, 0);
  1407. if (ost_index < 0 || ost_index >= of->nb_streams) {
  1408. av_log(dp, AV_LOG_ERROR, "Invalid output stream index '%d' in %s\n", ost_index, arg);
  1409. return AVERROR(EINVAL);
  1410. }
  1411. ost = of->streams[ost_index];
  1412. if (!ost->enc) {
  1413. av_log(dp, AV_LOG_ERROR, "Output stream %s has no encoder\n", arg);
  1414. return AVERROR(EINVAL);
  1415. }
  1416. dp->dec.type = ost->type;
  1417. ret = enc_loopback(ost->enc);
  1418. if (ret < 0)
  1419. return ret;
  1420. enc_idx = ret;
  1421. ret = sch_connect(sch, SCH_ENC(enc_idx), SCH_DEC_IN(dp->sch_idx));
  1422. if (ret < 0)
  1423. return ret;
  1424. ret = av_dict_copy(&dp->standalone_init.opts, o->g->codec_opts, 0);
  1425. if (ret < 0)
  1426. return ret;
  1427. multiview_check_manual(dp, dp->standalone_init.opts);
  1428. if (o->codec_names.nb_opt) {
  1429. const char *name = o->codec_names.opt[o->codec_names.nb_opt - 1].u.str;
  1430. dp->standalone_init.codec = avcodec_find_decoder_by_name(name);
  1431. if (!dp->standalone_init.codec) {
  1432. av_log(dp, AV_LOG_ERROR, "No such decoder: %s\n", name);
  1433. return AVERROR_DECODER_NOT_FOUND;
  1434. }
  1435. }
  1436. return 0;
  1437. }
  1438. int dec_filter_add(Decoder *d, InputFilter *ifilter, InputFilterOptions *opts,
  1439. const ViewSpecifier *vs, SchedulerNode *src)
  1440. {
  1441. DecoderPriv *dp = dp_from_dec(d);
  1442. char name[16];
  1443. snprintf(name, sizeof(name), "dec%d", dp->index);
  1444. opts->name = av_strdup(name);
  1445. if (!opts->name)
  1446. return AVERROR(ENOMEM);
  1447. return dec_request_view(d, vs, src);
  1448. }