ffmpeg_dec.c 55 KB

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