ffmpeg_dec.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764
  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. return ret;
  594. dp->dec.decode_errors++;
  595. if (exit_on_error)
  596. return ret;
  597. }
  598. while (1) {
  599. FrameData *fd;
  600. unsigned outputs_mask = 1;
  601. av_frame_unref(frame);
  602. update_benchmark(NULL);
  603. ret = avcodec_receive_frame(dec, frame);
  604. update_benchmark("decode_%s %s", type_desc, dp->parent_name);
  605. if (ret == AVERROR(EAGAIN)) {
  606. av_assert0(pkt); // should never happen during flushing
  607. return 0;
  608. } else if (ret == AVERROR_EOF) {
  609. return ret;
  610. } else if (ret < 0) {
  611. av_log(dp, AV_LOG_ERROR, "Decoding error: %s\n", av_err2str(ret));
  612. dp->dec.decode_errors++;
  613. if (exit_on_error)
  614. return ret;
  615. continue;
  616. }
  617. if (frame->decode_error_flags || (frame->flags & AV_FRAME_FLAG_CORRUPT)) {
  618. av_log(dp, exit_on_error ? AV_LOG_FATAL : AV_LOG_WARNING,
  619. "corrupt decoded frame\n");
  620. if (exit_on_error)
  621. return AVERROR_INVALIDDATA;
  622. }
  623. fd = frame_data(frame);
  624. if (!fd) {
  625. av_frame_unref(frame);
  626. return AVERROR(ENOMEM);
  627. }
  628. fd->dec.pts = frame->pts;
  629. fd->dec.tb = dec->pkt_timebase;
  630. fd->dec.frame_num = dec->frame_num - 1;
  631. fd->bits_per_raw_sample = dec->bits_per_raw_sample;
  632. fd->wallclock[LATENCY_PROBE_DEC_POST] = av_gettime_relative();
  633. frame->time_base = dec->pkt_timebase;
  634. if (dec->codec_type == AVMEDIA_TYPE_AUDIO) {
  635. dp->dec.samples_decoded += frame->nb_samples;
  636. audio_ts_process(dp, frame);
  637. } else {
  638. ret = video_frame_process(dp, frame, &outputs_mask);
  639. if (ret < 0) {
  640. av_log(dp, AV_LOG_FATAL,
  641. "Error while processing the decoded data\n");
  642. return ret;
  643. }
  644. }
  645. dp->dec.frames_decoded++;
  646. for (int i = 0; i < stdc_count_ones(outputs_mask); i++) {
  647. AVFrame *to_send = frame;
  648. int pos;
  649. av_assert0(outputs_mask);
  650. pos = stdc_trailing_zeros(outputs_mask);
  651. outputs_mask &= ~(1U << pos);
  652. // this is not the last output and sch_dec_send() consumes the frame
  653. // given to it, so make a temporary reference
  654. if (outputs_mask) {
  655. to_send = dp->frame_tmp_ref;
  656. ret = av_frame_ref(to_send, frame);
  657. if (ret < 0)
  658. return ret;
  659. }
  660. ret = sch_dec_send(dp->sch, dp->sch_idx, pos, to_send);
  661. if (ret < 0) {
  662. av_frame_unref(to_send);
  663. return ret == AVERROR_EOF ? AVERROR_EXIT : ret;
  664. }
  665. }
  666. }
  667. }
  668. static int dec_open(DecoderPriv *dp, AVDictionary **dec_opts,
  669. const DecoderOpts *o, AVFrame *param_out);
  670. static int dec_standalone_open(DecoderPriv *dp, const AVPacket *pkt)
  671. {
  672. DecoderOpts o;
  673. const FrameData *fd;
  674. char name[16];
  675. if (!pkt->opaque_ref)
  676. return AVERROR_BUG;
  677. fd = (FrameData *)pkt->opaque_ref->data;
  678. if (!fd->par_enc)
  679. return AVERROR_BUG;
  680. memset(&o, 0, sizeof(o));
  681. o.par = fd->par_enc;
  682. o.time_base = pkt->time_base;
  683. o.codec = dp->standalone_init.codec;
  684. if (!o.codec)
  685. o.codec = avcodec_find_decoder(o.par->codec_id);
  686. if (!o.codec) {
  687. const AVCodecDescriptor *desc = avcodec_descriptor_get(o.par->codec_id);
  688. av_log(dp, AV_LOG_ERROR, "Cannot find a decoder for codec ID '%s'\n",
  689. desc ? desc->name : "?");
  690. return AVERROR_DECODER_NOT_FOUND;
  691. }
  692. snprintf(name, sizeof(name), "dec%d", dp->index);
  693. o.name = name;
  694. return dec_open(dp, &dp->standalone_init.opts, &o, NULL);
  695. }
  696. static void dec_thread_set_name(const DecoderPriv *dp)
  697. {
  698. char name[16] = "dec";
  699. if (dp->index >= 0)
  700. av_strlcatf(name, sizeof(name), "%d", dp->index);
  701. else if (dp->parent_name)
  702. av_strlcat(name, dp->parent_name, sizeof(name));
  703. if (dp->dec_ctx)
  704. av_strlcatf(name, sizeof(name), ":%s", dp->dec_ctx->codec->name);
  705. ff_thread_setname(name);
  706. }
  707. static void dec_thread_uninit(DecThreadContext *dt)
  708. {
  709. av_packet_free(&dt->pkt);
  710. av_frame_free(&dt->frame);
  711. memset(dt, 0, sizeof(*dt));
  712. }
  713. static int dec_thread_init(DecThreadContext *dt)
  714. {
  715. memset(dt, 0, sizeof(*dt));
  716. dt->frame = av_frame_alloc();
  717. if (!dt->frame)
  718. goto fail;
  719. dt->pkt = av_packet_alloc();
  720. if (!dt->pkt)
  721. goto fail;
  722. return 0;
  723. fail:
  724. dec_thread_uninit(dt);
  725. return AVERROR(ENOMEM);
  726. }
  727. static int decoder_thread(void *arg)
  728. {
  729. DecoderPriv *dp = arg;
  730. DecThreadContext dt;
  731. int ret = 0, input_status = 0;
  732. ret = dec_thread_init(&dt);
  733. if (ret < 0)
  734. goto finish;
  735. dec_thread_set_name(dp);
  736. while (!input_status) {
  737. int flush_buffers, have_data;
  738. input_status = sch_dec_receive(dp->sch, dp->sch_idx, dt.pkt);
  739. have_data = input_status >= 0 &&
  740. (dt.pkt->buf || dt.pkt->side_data_elems ||
  741. (intptr_t)dt.pkt->opaque == PKT_OPAQUE_SUB_HEARTBEAT ||
  742. (intptr_t)dt.pkt->opaque == PKT_OPAQUE_FIX_SUB_DURATION);
  743. flush_buffers = input_status >= 0 && !have_data;
  744. if (!have_data)
  745. av_log(dp, AV_LOG_VERBOSE, "Decoder thread received %s packet\n",
  746. flush_buffers ? "flush" : "EOF");
  747. // this is a standalone decoder that has not been initialized yet
  748. if (!dp->dec_ctx) {
  749. if (flush_buffers)
  750. continue;
  751. if (input_status < 0) {
  752. av_log(dp, AV_LOG_ERROR,
  753. "Cannot initialize a standalone decoder\n");
  754. ret = input_status;
  755. goto finish;
  756. }
  757. ret = dec_standalone_open(dp, dt.pkt);
  758. if (ret < 0)
  759. goto finish;
  760. }
  761. ret = packet_decode(dp, have_data ? dt.pkt : NULL, dt.frame);
  762. av_packet_unref(dt.pkt);
  763. av_frame_unref(dt.frame);
  764. // AVERROR_EOF - EOF from the decoder
  765. // AVERROR_EXIT - EOF from the scheduler
  766. // we treat them differently when flushing
  767. if (ret == AVERROR_EXIT) {
  768. ret = AVERROR_EOF;
  769. flush_buffers = 0;
  770. }
  771. if (ret == AVERROR_EOF) {
  772. av_log(dp, AV_LOG_VERBOSE, "Decoder returned EOF, %s\n",
  773. flush_buffers ? "resetting" : "finishing");
  774. if (!flush_buffers)
  775. break;
  776. /* report last frame duration to the scheduler */
  777. if (dp->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
  778. dt.pkt->pts = dp->last_frame_pts + dp->last_frame_duration_est;
  779. dt.pkt->time_base = dp->last_frame_tb;
  780. }
  781. avcodec_flush_buffers(dp->dec_ctx);
  782. } else if (ret < 0) {
  783. av_log(dp, AV_LOG_ERROR, "Error processing packet in decoder: %s\n",
  784. av_err2str(ret));
  785. break;
  786. }
  787. }
  788. // EOF is normal thread termination
  789. if (ret == AVERROR_EOF)
  790. ret = 0;
  791. // on success send EOF timestamp to our downstreams
  792. if (ret >= 0) {
  793. float err_rate;
  794. av_frame_unref(dt.frame);
  795. dt.frame->opaque = (void*)(intptr_t)FRAME_OPAQUE_EOF;
  796. dt.frame->pts = dp->last_frame_pts == AV_NOPTS_VALUE ? AV_NOPTS_VALUE :
  797. dp->last_frame_pts + dp->last_frame_duration_est;
  798. dt.frame->time_base = dp->last_frame_tb;
  799. ret = sch_dec_send(dp->sch, dp->sch_idx, 0, dt.frame);
  800. if (ret < 0 && ret != AVERROR_EOF) {
  801. av_log(dp, AV_LOG_FATAL,
  802. "Error signalling EOF timestamp: %s\n", av_err2str(ret));
  803. goto finish;
  804. }
  805. ret = 0;
  806. err_rate = (dp->dec.frames_decoded || dp->dec.decode_errors) ?
  807. dp->dec.decode_errors / (dp->dec.frames_decoded + dp->dec.decode_errors) : 0.f;
  808. if (err_rate > max_error_rate) {
  809. av_log(dp, AV_LOG_FATAL, "Decode error rate %g exceeds maximum %g\n",
  810. err_rate, max_error_rate);
  811. ret = FFMPEG_ERROR_RATE_EXCEEDED;
  812. } else if (err_rate)
  813. av_log(dp, AV_LOG_VERBOSE, "Decode error rate %g\n", err_rate);
  814. }
  815. finish:
  816. dec_thread_uninit(&dt);
  817. return ret;
  818. }
  819. int dec_request_view(Decoder *d, const ViewSpecifier *vs,
  820. SchedulerNode *src)
  821. {
  822. DecoderPriv *dp = dp_from_dec(d);
  823. unsigned out_idx = 0;
  824. int ret;
  825. if (dp->multiview_user_config) {
  826. if (!vs || vs->type == VIEW_SPECIFIER_TYPE_NONE) {
  827. *src = SCH_DEC_OUT(dp->sch_idx, 0);
  828. return 0;
  829. }
  830. av_log(dp, AV_LOG_ERROR,
  831. "Manually selecting views with -view_ids cannot be combined "
  832. "with view selection via stream specifiers. It is strongly "
  833. "recommended you always use stream specifiers only.\n");
  834. return AVERROR(EINVAL);
  835. }
  836. // when multiview_user_config is not set, NONE specifier is treated
  837. // as requesting the base view
  838. vs = (vs && vs->type != VIEW_SPECIFIER_TYPE_NONE) ? vs :
  839. &(ViewSpecifier){ .type = VIEW_SPECIFIER_TYPE_IDX, .val = 0 };
  840. // check if the specifier matches an already-existing one
  841. for (int i = 0; i < dp->nb_views_requested; i++) {
  842. const ViewSpecifier *vs1 = &dp->views_requested[i].vs;
  843. if (vs->type == vs1->type &&
  844. (vs->type == VIEW_SPECIFIER_TYPE_ALL || vs->val == vs1->val)) {
  845. *src = SCH_DEC_OUT(dp->sch_idx, dp->views_requested[i].out_idx);
  846. return 0;
  847. }
  848. }
  849. // we use a bitmask to map view IDs to decoder outputs, which
  850. // limits the number of outputs allowed
  851. if (dp->nb_views_requested >= sizeof(dp->view_map[0].out_mask) * 8) {
  852. av_log(dp, AV_LOG_ERROR, "Too many view specifiers\n");
  853. return AVERROR(ENOSYS);
  854. }
  855. ret = GROW_ARRAY(dp->views_requested, dp->nb_views_requested);
  856. if (ret < 0)
  857. return ret;
  858. if (dp->nb_views_requested > 1) {
  859. ret = sch_add_dec_output(dp->sch, dp->sch_idx);
  860. if (ret < 0)
  861. return ret;
  862. out_idx = ret;
  863. }
  864. dp->views_requested[dp->nb_views_requested - 1].out_idx = out_idx;
  865. dp->views_requested[dp->nb_views_requested - 1].vs = *vs;
  866. *src = SCH_DEC_OUT(dp->sch_idx,
  867. dp->views_requested[dp->nb_views_requested - 1].out_idx);
  868. return 0;
  869. }
  870. static int multiview_setup(DecoderPriv *dp, AVCodecContext *dec_ctx)
  871. {
  872. unsigned views_wanted = 0;
  873. unsigned nb_view_ids_av, nb_view_ids;
  874. unsigned *view_ids_av = NULL, *view_pos_av = NULL;
  875. int *view_ids = NULL;
  876. int ret;
  877. // no views/only base view were requested - do nothing
  878. if (!dp->nb_views_requested ||
  879. (dp->nb_views_requested == 1 &&
  880. dp->views_requested[0].vs.type == VIEW_SPECIFIER_TYPE_IDX &&
  881. dp->views_requested[0].vs.val == 0))
  882. return 0;
  883. av_freep(&dp->view_map);
  884. dp->nb_view_map = 0;
  885. // retrieve views available in current CVS
  886. ret = av_opt_get_array_size(dec_ctx, "view_ids_available",
  887. AV_OPT_SEARCH_CHILDREN, &nb_view_ids_av);
  888. if (ret < 0) {
  889. av_log(dp, AV_LOG_ERROR,
  890. "Multiview decoding requested, but decoder '%s' does not "
  891. "support it\n", dec_ctx->codec->name);
  892. return AVERROR(ENOSYS);
  893. }
  894. if (nb_view_ids_av) {
  895. unsigned nb_view_pos_av;
  896. if (nb_view_ids_av >= sizeof(views_wanted) * 8) {
  897. av_log(dp, AV_LOG_ERROR, "Too many views in video: %u\n", nb_view_ids_av);
  898. ret = AVERROR(ENOSYS);
  899. goto fail;
  900. }
  901. view_ids_av = av_calloc(nb_view_ids_av, sizeof(*view_ids_av));
  902. if (!view_ids_av) {
  903. ret = AVERROR(ENOMEM);
  904. goto fail;
  905. }
  906. ret = av_opt_get_array(dec_ctx, "view_ids_available",
  907. AV_OPT_SEARCH_CHILDREN, 0, nb_view_ids_av,
  908. AV_OPT_TYPE_UINT, view_ids_av);
  909. if (ret < 0)
  910. goto fail;
  911. ret = av_opt_get_array_size(dec_ctx, "view_pos_available",
  912. AV_OPT_SEARCH_CHILDREN, &nb_view_pos_av);
  913. if (ret >= 0 && nb_view_pos_av == nb_view_ids_av) {
  914. view_pos_av = av_calloc(nb_view_ids_av, sizeof(*view_pos_av));
  915. if (!view_pos_av) {
  916. ret = AVERROR(ENOMEM);
  917. goto fail;
  918. }
  919. ret = av_opt_get_array(dec_ctx, "view_pos_available",
  920. AV_OPT_SEARCH_CHILDREN, 0, nb_view_ids_av,
  921. AV_OPT_TYPE_UINT, view_pos_av);
  922. if (ret < 0)
  923. goto fail;
  924. }
  925. } else {
  926. // assume there is a single view with ID=0
  927. nb_view_ids_av = 1;
  928. view_ids_av = av_calloc(nb_view_ids_av, sizeof(*view_ids_av));
  929. view_pos_av = av_calloc(nb_view_ids_av, sizeof(*view_pos_av));
  930. if (!view_ids_av || !view_pos_av) {
  931. ret = AVERROR(ENOMEM);
  932. goto fail;
  933. }
  934. view_pos_av[0] = AV_STEREO3D_VIEW_UNSPEC;
  935. }
  936. dp->view_map = av_calloc(nb_view_ids_av, sizeof(*dp->view_map));
  937. if (!dp->view_map) {
  938. ret = AVERROR(ENOMEM);
  939. goto fail;
  940. }
  941. dp->nb_view_map = nb_view_ids_av;
  942. for (int i = 0; i < dp->nb_view_map; i++)
  943. dp->view_map[i].id = view_ids_av[i];
  944. // figure out which views should go to which output
  945. for (int i = 0; i < dp->nb_views_requested; i++) {
  946. const ViewSpecifier *vs = &dp->views_requested[i].vs;
  947. switch (vs->type) {
  948. case VIEW_SPECIFIER_TYPE_IDX:
  949. if (vs->val >= nb_view_ids_av) {
  950. av_log(dp, exit_on_error ? AV_LOG_ERROR : AV_LOG_WARNING,
  951. "View with index %u requested, but only %u views available "
  952. "in current video sequence (more views may or may not be "
  953. "available in later sequences).\n",
  954. vs->val, nb_view_ids_av);
  955. if (exit_on_error) {
  956. ret = AVERROR(EINVAL);
  957. goto fail;
  958. }
  959. continue;
  960. }
  961. views_wanted |= 1U << vs->val;
  962. dp->view_map[vs->val].out_mask |= 1ULL << i;
  963. break;
  964. case VIEW_SPECIFIER_TYPE_ID: {
  965. int view_idx = -1;
  966. for (unsigned j = 0; j < nb_view_ids_av; j++) {
  967. if (view_ids_av[j] == vs->val) {
  968. view_idx = j;
  969. break;
  970. }
  971. }
  972. if (view_idx < 0) {
  973. av_log(dp, exit_on_error ? AV_LOG_ERROR : AV_LOG_WARNING,
  974. "View with ID %u requested, but is not available "
  975. "in the video sequence\n", vs->val);
  976. if (exit_on_error) {
  977. ret = AVERROR(EINVAL);
  978. goto fail;
  979. }
  980. continue;
  981. }
  982. views_wanted |= 1U << view_idx;
  983. dp->view_map[view_idx].out_mask |= 1ULL << i;
  984. break;
  985. }
  986. case VIEW_SPECIFIER_TYPE_POS: {
  987. int view_idx = -1;
  988. for (unsigned j = 0; view_pos_av && j < nb_view_ids_av; j++) {
  989. if (view_pos_av[j] == vs->val) {
  990. view_idx = j;
  991. break;
  992. }
  993. }
  994. if (view_idx < 0) {
  995. av_log(dp, exit_on_error ? AV_LOG_ERROR : AV_LOG_WARNING,
  996. "View position '%s' requested, but is not available "
  997. "in the video sequence\n", av_stereo3d_view_name(vs->val));
  998. if (exit_on_error) {
  999. ret = AVERROR(EINVAL);
  1000. goto fail;
  1001. }
  1002. continue;
  1003. }
  1004. views_wanted |= 1U << view_idx;
  1005. dp->view_map[view_idx].out_mask |= 1ULL << i;
  1006. break;
  1007. }
  1008. case VIEW_SPECIFIER_TYPE_ALL:
  1009. views_wanted |= (1U << nb_view_ids_av) - 1;
  1010. for (int j = 0; j < dp->nb_view_map; j++)
  1011. dp->view_map[j].out_mask |= 1ULL << i;
  1012. break;
  1013. }
  1014. }
  1015. if (!views_wanted) {
  1016. av_log(dp, AV_LOG_ERROR, "No views were selected for decoding\n");
  1017. ret = AVERROR(EINVAL);
  1018. goto fail;
  1019. }
  1020. // signal to decoder which views we want
  1021. nb_view_ids = stdc_count_ones(views_wanted);
  1022. view_ids = av_malloc_array(nb_view_ids, sizeof(*view_ids));
  1023. if (!view_ids) {
  1024. ret = AVERROR(ENOMEM);
  1025. goto fail;
  1026. }
  1027. for (unsigned i = 0; i < nb_view_ids; i++) {
  1028. int pos;
  1029. av_assert0(views_wanted);
  1030. pos = stdc_trailing_zeros(views_wanted);
  1031. views_wanted &= ~(1U << pos);
  1032. view_ids[i] = view_ids_av[pos];
  1033. }
  1034. // unset view_ids in case we set it earlier
  1035. av_opt_set(dec_ctx, "view_ids", NULL, AV_OPT_SEARCH_CHILDREN);
  1036. ret = av_opt_set_array(dec_ctx, "view_ids", AV_OPT_SEARCH_CHILDREN,
  1037. 0, nb_view_ids, AV_OPT_TYPE_INT, view_ids);
  1038. if (ret < 0)
  1039. goto fail;
  1040. if (!dp->frame_tmp_ref) {
  1041. dp->frame_tmp_ref = av_frame_alloc();
  1042. if (!dp->frame_tmp_ref) {
  1043. ret = AVERROR(ENOMEM);
  1044. goto fail;
  1045. }
  1046. }
  1047. fail:
  1048. av_freep(&view_ids_av);
  1049. av_freep(&view_pos_av);
  1050. av_freep(&view_ids);
  1051. return ret;
  1052. }
  1053. static void multiview_check_manual(DecoderPriv *dp, const AVDictionary *dec_opts)
  1054. {
  1055. if (av_dict_get(dec_opts, "view_ids", NULL, 0)) {
  1056. av_log(dp, AV_LOG_WARNING, "Manually selecting views with -view_ids "
  1057. "is not recommended, use view specifiers instead\n");
  1058. dp->multiview_user_config = 1;
  1059. }
  1060. }
  1061. static enum AVPixelFormat get_format(AVCodecContext *s, const enum AVPixelFormat *pix_fmts)
  1062. {
  1063. DecoderPriv *dp = s->opaque;
  1064. const enum AVPixelFormat *p;
  1065. int ret;
  1066. ret = multiview_setup(dp, s);
  1067. if (ret < 0) {
  1068. av_log(dp, AV_LOG_ERROR, "Error setting up multiview decoding: %s\n",
  1069. av_err2str(ret));
  1070. return AV_PIX_FMT_NONE;
  1071. }
  1072. for (p = pix_fmts; *p != AV_PIX_FMT_NONE; p++) {
  1073. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(*p);
  1074. const AVCodecHWConfig *config = NULL;
  1075. if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
  1076. break;
  1077. if (dp->hwaccel_id == HWACCEL_GENERIC ||
  1078. dp->hwaccel_id == HWACCEL_AUTO) {
  1079. for (int i = 0;; i++) {
  1080. config = avcodec_get_hw_config(s->codec, i);
  1081. if (!config)
  1082. break;
  1083. if (!(config->methods &
  1084. AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
  1085. continue;
  1086. if (config->pix_fmt == *p)
  1087. break;
  1088. }
  1089. }
  1090. if (config && config->device_type == dp->hwaccel_device_type) {
  1091. dp->hwaccel_pix_fmt = *p;
  1092. break;
  1093. }
  1094. }
  1095. return *p;
  1096. }
  1097. static int get_buffer(AVCodecContext *dec_ctx, AVFrame *frame, int flags)
  1098. {
  1099. DecoderPriv *dp = dec_ctx->opaque;
  1100. // for multiview video, store the output mask in frame opaque
  1101. if (dp->nb_view_map) {
  1102. const AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_VIEW_ID);
  1103. int view_id = sd ? *(int*)sd->data : 0;
  1104. for (int i = 0; i < dp->nb_view_map; i++) {
  1105. if (dp->view_map[i].id == view_id) {
  1106. frame->opaque = (void*)dp->view_map[i].out_mask;
  1107. break;
  1108. }
  1109. }
  1110. }
  1111. return avcodec_default_get_buffer2(dec_ctx, frame, flags);
  1112. }
  1113. static HWDevice *hw_device_match_by_codec(const AVCodec *codec)
  1114. {
  1115. const AVCodecHWConfig *config;
  1116. HWDevice *dev;
  1117. for (int i = 0;; i++) {
  1118. config = avcodec_get_hw_config(codec, i);
  1119. if (!config)
  1120. return NULL;
  1121. if (!(config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
  1122. continue;
  1123. dev = hw_device_get_by_type(config->device_type);
  1124. if (dev)
  1125. return dev;
  1126. }
  1127. }
  1128. static int hw_device_setup_for_decode(DecoderPriv *dp,
  1129. const AVCodec *codec,
  1130. const char *hwaccel_device)
  1131. {
  1132. const AVCodecHWConfig *config;
  1133. enum AVHWDeviceType type;
  1134. HWDevice *dev = NULL;
  1135. int err, auto_device = 0;
  1136. if (hwaccel_device) {
  1137. dev = hw_device_get_by_name(hwaccel_device);
  1138. if (!dev) {
  1139. if (dp->hwaccel_id == HWACCEL_AUTO) {
  1140. auto_device = 1;
  1141. } else if (dp->hwaccel_id == HWACCEL_GENERIC) {
  1142. type = dp->hwaccel_device_type;
  1143. err = hw_device_init_from_type(type, hwaccel_device,
  1144. &dev);
  1145. } else {
  1146. // This will be dealt with by API-specific initialisation
  1147. // (using hwaccel_device), so nothing further needed here.
  1148. return 0;
  1149. }
  1150. } else {
  1151. if (dp->hwaccel_id == HWACCEL_AUTO) {
  1152. dp->hwaccel_device_type = dev->type;
  1153. } else if (dp->hwaccel_device_type != dev->type) {
  1154. av_log(dp, AV_LOG_ERROR, "Invalid hwaccel device "
  1155. "specified for decoder: device %s of type %s is not "
  1156. "usable with hwaccel %s.\n", dev->name,
  1157. av_hwdevice_get_type_name(dev->type),
  1158. av_hwdevice_get_type_name(dp->hwaccel_device_type));
  1159. return AVERROR(EINVAL);
  1160. }
  1161. }
  1162. } else {
  1163. if (dp->hwaccel_id == HWACCEL_AUTO) {
  1164. auto_device = 1;
  1165. } else if (dp->hwaccel_id == HWACCEL_GENERIC) {
  1166. type = dp->hwaccel_device_type;
  1167. dev = hw_device_get_by_type(type);
  1168. // When "-qsv_device device" is used, an internal QSV device named
  1169. // as "__qsv_device" is created. Another QSV device is created too
  1170. // if "-init_hw_device qsv=name:device" is used. There are 2 QSV devices
  1171. // if both "-qsv_device device" and "-init_hw_device qsv=name:device"
  1172. // are used, hw_device_get_by_type(AV_HWDEVICE_TYPE_QSV) returns NULL.
  1173. // To keep back-compatibility with the removed ad-hoc libmfx setup code,
  1174. // call hw_device_get_by_name("__qsv_device") to select the internal QSV
  1175. // device.
  1176. if (!dev && type == AV_HWDEVICE_TYPE_QSV)
  1177. dev = hw_device_get_by_name("__qsv_device");
  1178. if (!dev)
  1179. err = hw_device_init_from_type(type, NULL, &dev);
  1180. } else {
  1181. dev = hw_device_match_by_codec(codec);
  1182. if (!dev) {
  1183. // No device for this codec, but not using generic hwaccel
  1184. // and therefore may well not need one - ignore.
  1185. return 0;
  1186. }
  1187. }
  1188. }
  1189. if (auto_device) {
  1190. if (!avcodec_get_hw_config(codec, 0)) {
  1191. // Decoder does not support any hardware devices.
  1192. return 0;
  1193. }
  1194. for (int i = 0; !dev; i++) {
  1195. config = avcodec_get_hw_config(codec, i);
  1196. if (!config)
  1197. break;
  1198. type = config->device_type;
  1199. dev = hw_device_get_by_type(type);
  1200. if (dev) {
  1201. av_log(dp, AV_LOG_INFO, "Using auto "
  1202. "hwaccel type %s with existing device %s.\n",
  1203. av_hwdevice_get_type_name(type), dev->name);
  1204. }
  1205. }
  1206. for (int i = 0; !dev; i++) {
  1207. config = avcodec_get_hw_config(codec, i);
  1208. if (!config)
  1209. break;
  1210. type = config->device_type;
  1211. // Try to make a new device of this type.
  1212. err = hw_device_init_from_type(type, hwaccel_device,
  1213. &dev);
  1214. if (err < 0) {
  1215. // Can't make a device of this type.
  1216. continue;
  1217. }
  1218. if (hwaccel_device) {
  1219. av_log(dp, AV_LOG_INFO, "Using auto "
  1220. "hwaccel type %s with new device created "
  1221. "from %s.\n", av_hwdevice_get_type_name(type),
  1222. hwaccel_device);
  1223. } else {
  1224. av_log(dp, AV_LOG_INFO, "Using auto "
  1225. "hwaccel type %s with new default device.\n",
  1226. av_hwdevice_get_type_name(type));
  1227. }
  1228. }
  1229. if (dev) {
  1230. dp->hwaccel_device_type = type;
  1231. } else {
  1232. av_log(dp, AV_LOG_INFO, "Auto hwaccel "
  1233. "disabled: no device found.\n");
  1234. dp->hwaccel_id = HWACCEL_NONE;
  1235. return 0;
  1236. }
  1237. }
  1238. if (!dev) {
  1239. av_log(dp, AV_LOG_ERROR, "No device available "
  1240. "for decoder: device type %s needed for codec %s.\n",
  1241. av_hwdevice_get_type_name(type), codec->name);
  1242. return err;
  1243. }
  1244. dp->dec_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
  1245. if (!dp->dec_ctx->hw_device_ctx)
  1246. return AVERROR(ENOMEM);
  1247. return 0;
  1248. }
  1249. static int dec_open(DecoderPriv *dp, AVDictionary **dec_opts,
  1250. const DecoderOpts *o, AVFrame *param_out)
  1251. {
  1252. const AVCodec *codec = o->codec;
  1253. int ret;
  1254. dp->flags = o->flags;
  1255. dp->log_parent = o->log_parent;
  1256. dp->dec.type = codec->type;
  1257. dp->framerate_in = o->framerate;
  1258. dp->hwaccel_id = o->hwaccel_id;
  1259. dp->hwaccel_device_type = o->hwaccel_device_type;
  1260. dp->hwaccel_output_format = o->hwaccel_output_format;
  1261. snprintf(dp->log_name, sizeof(dp->log_name), "dec:%s", codec->name);
  1262. dp->parent_name = av_strdup(o->name ? o->name : "");
  1263. if (!dp->parent_name)
  1264. return AVERROR(ENOMEM);
  1265. if (codec->type == AVMEDIA_TYPE_SUBTITLE &&
  1266. (dp->flags & DECODER_FLAG_FIX_SUB_DURATION)) {
  1267. for (int i = 0; i < FF_ARRAY_ELEMS(dp->sub_prev); i++) {
  1268. dp->sub_prev[i] = av_frame_alloc();
  1269. if (!dp->sub_prev[i])
  1270. return AVERROR(ENOMEM);
  1271. }
  1272. dp->sub_heartbeat = av_frame_alloc();
  1273. if (!dp->sub_heartbeat)
  1274. return AVERROR(ENOMEM);
  1275. }
  1276. dp->sar_override = o->par->sample_aspect_ratio;
  1277. dp->dec_ctx = avcodec_alloc_context3(codec);
  1278. if (!dp->dec_ctx)
  1279. return AVERROR(ENOMEM);
  1280. ret = avcodec_parameters_to_context(dp->dec_ctx, o->par);
  1281. if (ret < 0) {
  1282. av_log(dp, AV_LOG_ERROR, "Error initializing the decoder context.\n");
  1283. return ret;
  1284. }
  1285. dp->dec_ctx->opaque = dp;
  1286. dp->dec_ctx->get_format = get_format;
  1287. dp->dec_ctx->get_buffer2 = get_buffer;
  1288. dp->dec_ctx->pkt_timebase = o->time_base;
  1289. if (!av_dict_get(*dec_opts, "threads", NULL, 0))
  1290. av_dict_set(dec_opts, "threads", "auto", 0);
  1291. ret = hw_device_setup_for_decode(dp, codec, o->hwaccel_device);
  1292. if (ret < 0) {
  1293. av_log(dp, AV_LOG_ERROR,
  1294. "Hardware device setup failed for decoder: %s\n",
  1295. av_err2str(ret));
  1296. return ret;
  1297. }
  1298. ret = av_opt_set_dict2(dp->dec_ctx, dec_opts, AV_OPT_SEARCH_CHILDREN);
  1299. if (ret < 0) {
  1300. av_log(dp, AV_LOG_ERROR, "Error applying decoder options: %s\n",
  1301. av_err2str(ret));
  1302. return ret;
  1303. }
  1304. ret = check_avoptions(*dec_opts);
  1305. if (ret < 0)
  1306. return ret;
  1307. dp->dec_ctx->flags |= AV_CODEC_FLAG_COPY_OPAQUE;
  1308. if (o->flags & DECODER_FLAG_BITEXACT)
  1309. dp->dec_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
  1310. // we apply cropping ourselves
  1311. dp->apply_cropping = dp->dec_ctx->apply_cropping;
  1312. dp->dec_ctx->apply_cropping = 0;
  1313. if ((ret = avcodec_open2(dp->dec_ctx, codec, NULL)) < 0) {
  1314. av_log(dp, AV_LOG_ERROR, "Error while opening decoder: %s\n",
  1315. av_err2str(ret));
  1316. return ret;
  1317. }
  1318. if (dp->dec_ctx->hw_device_ctx) {
  1319. // Update decoder extra_hw_frames option to account for the
  1320. // frames held in queues inside the ffmpeg utility. This is
  1321. // called after avcodec_open2() because the user-set value of
  1322. // extra_hw_frames becomes valid in there, and we need to add
  1323. // this on top of it.
  1324. int extra_frames = DEFAULT_FRAME_THREAD_QUEUE_SIZE;
  1325. if (dp->dec_ctx->extra_hw_frames >= 0)
  1326. dp->dec_ctx->extra_hw_frames += extra_frames;
  1327. else
  1328. dp->dec_ctx->extra_hw_frames = extra_frames;
  1329. }
  1330. dp->dec.subtitle_header = dp->dec_ctx->subtitle_header;
  1331. dp->dec.subtitle_header_size = dp->dec_ctx->subtitle_header_size;
  1332. if (param_out) {
  1333. if (dp->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
  1334. param_out->format = dp->dec_ctx->sample_fmt;
  1335. param_out->sample_rate = dp->dec_ctx->sample_rate;
  1336. ret = av_channel_layout_copy(&param_out->ch_layout, &dp->dec_ctx->ch_layout);
  1337. if (ret < 0)
  1338. return ret;
  1339. } else if (dp->dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  1340. param_out->format = dp->dec_ctx->pix_fmt;
  1341. param_out->width = dp->dec_ctx->width;
  1342. param_out->height = dp->dec_ctx->height;
  1343. param_out->sample_aspect_ratio = dp->dec_ctx->sample_aspect_ratio;
  1344. param_out->colorspace = dp->dec_ctx->colorspace;
  1345. param_out->color_range = dp->dec_ctx->color_range;
  1346. }
  1347. av_frame_side_data_free(&param_out->side_data, &param_out->nb_side_data);
  1348. ret = clone_side_data(&param_out->side_data, &param_out->nb_side_data,
  1349. dp->dec_ctx->decoded_side_data, dp->dec_ctx->nb_decoded_side_data, 0);
  1350. if (ret < 0)
  1351. return ret;
  1352. param_out->time_base = dp->dec_ctx->pkt_timebase;
  1353. }
  1354. return 0;
  1355. }
  1356. int dec_init(Decoder **pdec, Scheduler *sch,
  1357. AVDictionary **dec_opts, const DecoderOpts *o,
  1358. AVFrame *param_out)
  1359. {
  1360. DecoderPriv *dp;
  1361. int ret;
  1362. *pdec = NULL;
  1363. ret = dec_alloc(&dp, sch, !!(o->flags & DECODER_FLAG_SEND_END_TS));
  1364. if (ret < 0)
  1365. return ret;
  1366. multiview_check_manual(dp, *dec_opts);
  1367. ret = dec_open(dp, dec_opts, o, param_out);
  1368. if (ret < 0)
  1369. goto fail;
  1370. *pdec = &dp->dec;
  1371. return dp->sch_idx;
  1372. fail:
  1373. dec_free((Decoder**)&dp);
  1374. return ret;
  1375. }
  1376. int dec_create(const OptionsContext *o, const char *arg, Scheduler *sch)
  1377. {
  1378. DecoderPriv *dp;
  1379. OutputFile *of;
  1380. OutputStream *ost;
  1381. int of_index, ost_index;
  1382. char *p;
  1383. unsigned enc_idx;
  1384. int ret;
  1385. ret = dec_alloc(&dp, sch, 0);
  1386. if (ret < 0)
  1387. return ret;
  1388. dp->index = nb_decoders;
  1389. ret = GROW_ARRAY(decoders, nb_decoders);
  1390. if (ret < 0) {
  1391. dec_free((Decoder **)&dp);
  1392. return ret;
  1393. }
  1394. decoders[nb_decoders - 1] = (Decoder *)dp;
  1395. of_index = strtol(arg, &p, 0);
  1396. if (of_index < 0 || of_index >= nb_output_files) {
  1397. av_log(dp, AV_LOG_ERROR, "Invalid output file index '%d' in %s\n", of_index, arg);
  1398. return AVERROR(EINVAL);
  1399. }
  1400. of = output_files[of_index];
  1401. ost_index = strtol(p + 1, NULL, 0);
  1402. if (ost_index < 0 || ost_index >= of->nb_streams) {
  1403. av_log(dp, AV_LOG_ERROR, "Invalid output stream index '%d' in %s\n", ost_index, arg);
  1404. return AVERROR(EINVAL);
  1405. }
  1406. ost = of->streams[ost_index];
  1407. if (!ost->enc) {
  1408. av_log(dp, AV_LOG_ERROR, "Output stream %s has no encoder\n", arg);
  1409. return AVERROR(EINVAL);
  1410. }
  1411. dp->dec.type = ost->type;
  1412. ret = enc_loopback(ost->enc);
  1413. if (ret < 0)
  1414. return ret;
  1415. enc_idx = ret;
  1416. ret = sch_connect(sch, SCH_ENC(enc_idx), SCH_DEC_IN(dp->sch_idx));
  1417. if (ret < 0)
  1418. return ret;
  1419. ret = av_dict_copy(&dp->standalone_init.opts, o->g->codec_opts, 0);
  1420. if (ret < 0)
  1421. return ret;
  1422. multiview_check_manual(dp, dp->standalone_init.opts);
  1423. if (o->codec_names.nb_opt) {
  1424. const char *name = o->codec_names.opt[o->codec_names.nb_opt - 1].u.str;
  1425. dp->standalone_init.codec = avcodec_find_decoder_by_name(name);
  1426. if (!dp->standalone_init.codec) {
  1427. av_log(dp, AV_LOG_ERROR, "No such decoder: %s\n", name);
  1428. return AVERROR_DECODER_NOT_FOUND;
  1429. }
  1430. }
  1431. return 0;
  1432. }
  1433. int dec_filter_add(Decoder *d, InputFilter *ifilter, InputFilterOptions *opts,
  1434. const ViewSpecifier *vs, SchedulerNode *src)
  1435. {
  1436. DecoderPriv *dp = dp_from_dec(d);
  1437. char name[16];
  1438. snprintf(name, sizeof(name), "dec%d", dp->index);
  1439. opts->name = av_strdup(name);
  1440. if (!opts->name)
  1441. return AVERROR(ENOMEM);
  1442. return dec_request_view(d, vs, src);
  1443. }