FFmpeg
ffmpeg_dec.c
Go to the documentation of this file.
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 
19 #include "libavutil/avassert.h"
20 #include "libavutil/dict.h"
21 #include "libavutil/error.h"
22 #include "libavutil/log.h"
23 #include "libavutil/pixdesc.h"
24 #include "libavutil/pixfmt.h"
25 #include "libavutil/time.h"
26 #include "libavutil/timestamp.h"
27 
28 #include "libavcodec/avcodec.h"
29 #include "libavcodec/codec.h"
30 
31 #include "libavfilter/buffersrc.h"
32 
33 #include "ffmpeg.h"
34 #include "ffmpeg_utils.h"
35 #include "thread_queue.h"
36 
37 typedef struct DecoderPriv {
39 
41 
44 
45  // override output video sample aspect ratio with this value
47 
49 
50  // a combination of DECODER_FLAG_*, provided to dec_open()
51  int flags;
52 
57 
58  // pts/estimated duration of the last decoded frame
59  // * in decoder timebase for video,
60  // * in last_frame_tb (may change during decoding) for audio
61  int64_t last_frame_pts;
66 
67  /* previous decoded subtitles */
70 
72  unsigned sch_idx;
73 
74  void *log_parent;
75  char log_name[32];
76  char *parent_name;
77 } DecoderPriv;
78 
80 {
81  return (DecoderPriv*)d;
82 }
83 
84 // data that is local to the decoder thread and not visible outside of it
85 typedef struct DecThreadContext {
89 
90 void dec_free(Decoder **pdec)
91 {
92  Decoder *dec = *pdec;
93  DecoderPriv *dp;
94 
95  if (!dec)
96  return;
97  dp = dp_from_dec(dec);
98 
100 
101  av_frame_free(&dp->frame);
102  av_packet_free(&dp->pkt);
103 
104  for (int i = 0; i < FF_ARRAY_ELEMS(dp->sub_prev); i++)
105  av_frame_free(&dp->sub_prev[i]);
107 
108  av_freep(&dp->parent_name);
109 
110  av_freep(pdec);
111 }
112 
113 static int dec_alloc(DecoderPriv **pdec)
114 {
115  DecoderPriv *dp;
116 
117  *pdec = NULL;
118 
119  dp = av_mallocz(sizeof(*dp));
120  if (!dp)
121  return AVERROR(ENOMEM);
122 
123  dp->frame = av_frame_alloc();
124  if (!dp->frame)
125  goto fail;
126 
127  dp->pkt = av_packet_alloc();
128  if (!dp->pkt)
129  goto fail;
130 
133  dp->last_frame_tb = (AVRational){ 1, 1 };
135 
136  *pdec = dp;
137 
138  return 0;
139 fail:
140  dec_free((Decoder**)&dp);
141  return AVERROR(ENOMEM);
142 }
143 
145  const AVFrame *frame)
146 {
147  const int prev = dp->last_frame_tb.den;
148  const int sr = frame->sample_rate;
149 
150  AVRational tb_new;
151  int64_t gcd;
152 
154  goto finish;
155 
156  gcd = av_gcd(prev, sr);
157 
158  if (prev / gcd >= INT_MAX / sr) {
160  "Audio timestamps cannot be represented exactly after "
161  "sample rate change: %d -> %d\n", prev, sr);
162 
163  // LCM of 192000, 44100, allows to represent all common samplerates
164  tb_new = (AVRational){ 1, 28224000 };
165  } else
166  tb_new = (AVRational){ 1, prev / gcd * sr };
167 
168  // keep the frame timebase if it is strictly better than
169  // the samplerate-defined one
170  if (frame->time_base.num == 1 && frame->time_base.den > tb_new.den &&
171  !(frame->time_base.den % tb_new.den))
172  tb_new = frame->time_base;
173 
176  dp->last_frame_tb, tb_new);
178  dp->last_frame_tb, tb_new);
179 
180  dp->last_frame_tb = tb_new;
182 
183 finish:
184  return dp->last_frame_tb;
185 }
186 
188 {
189  AVRational tb_filter = (AVRational){1, frame->sample_rate};
190  AVRational tb;
191  int64_t pts_pred;
192 
193  // on samplerate change, choose a new internal timebase for timestamp
194  // generation that can represent timestamps from all the samplerates
195  // seen so far
197  pts_pred = dp->last_frame_pts == AV_NOPTS_VALUE ? 0 :
199 
200  if (frame->pts == AV_NOPTS_VALUE) {
201  frame->pts = pts_pred;
202  frame->time_base = tb;
203  } else if (dp->last_frame_pts != AV_NOPTS_VALUE &&
204  frame->pts > av_rescale_q_rnd(pts_pred, tb, frame->time_base,
205  AV_ROUND_UP)) {
206  // there was a gap in timestamps, reset conversion state
208  }
209 
211  tb, frame->nb_samples,
213 
214  dp->last_frame_pts = frame->pts;
216  tb_filter, tb);
217 
218  // finally convert to filtering timebase
219  frame->pts = av_rescale_q(frame->pts, tb, tb_filter);
221  frame->time_base = tb_filter;
222 }
223 
224 static int64_t video_duration_estimate(const DecoderPriv *dp, const AVFrame *frame)
225 {
226  const int ts_unreliable = dp->flags & DECODER_FLAG_TS_UNRELIABLE;
227  const int fr_forced = dp->flags & DECODER_FLAG_FRAMERATE_FORCED;
228  int64_t codec_duration = 0;
229 
230  // XXX lavf currently makes up frame durations when they are not provided by
231  // the container. As there is no way to reliably distinguish real container
232  // durations from the fake made-up ones, we use heuristics based on whether
233  // the container has timestamps. Eventually lavf should stop making up
234  // durations, then this should be simplified.
235 
236  // prefer frame duration for containers with timestamps
237  if (frame->duration > 0 && (!ts_unreliable || fr_forced))
238  return frame->duration;
239 
240  if (dp->dec_ctx->framerate.den && dp->dec_ctx->framerate.num) {
241  int fields = frame->repeat_pict + 2;
242  AVRational field_rate = av_mul_q(dp->dec_ctx->framerate,
243  (AVRational){ 2, 1 });
244  codec_duration = av_rescale_q(fields, av_inv_q(field_rate),
245  frame->time_base);
246  }
247 
248  // prefer codec-layer duration for containers without timestamps
249  if (codec_duration > 0 && ts_unreliable)
250  return codec_duration;
251 
252  // when timestamps are available, repeat last frame's actual duration
253  // (i.e. pts difference between this and last frame)
255  frame->pts > dp->last_frame_pts)
256  return frame->pts - dp->last_frame_pts;
257 
258  // try frame/codec duration
259  if (frame->duration > 0)
260  return frame->duration;
261  if (codec_duration > 0)
262  return codec_duration;
263 
264  // try average framerate
265  if (dp->framerate_in.num && dp->framerate_in.den) {
266  int64_t d = av_rescale_q(1, av_inv_q(dp->framerate_in),
267  frame->time_base);
268  if (d > 0)
269  return d;
270  }
271 
272  // last resort is last frame's estimated duration, and 1
273  return FFMAX(dp->last_frame_duration_est, 1);
274 }
275 
277 {
278  DecoderPriv *dp = avctx->opaque;
279  AVFrame *output = NULL;
281  int err;
282 
283  if (input->format == output_format) {
284  // Nothing to do.
285  return 0;
286  }
287 
289  if (!output)
290  return AVERROR(ENOMEM);
291 
292  output->format = output_format;
293 
295  if (err < 0) {
296  av_log(avctx, AV_LOG_ERROR, "Failed to transfer data to "
297  "output frame: %d.\n", err);
298  goto fail;
299  }
300 
302  if (err < 0) {
304  goto fail;
305  }
306 
310 
311  return 0;
312 
313 fail:
315  return err;
316 }
317 
319 {
320 #if FFMPEG_OPT_TOP
322  av_log(dp, AV_LOG_WARNING, "-top is deprecated, use the setfield filter instead\n");
324  }
325 #endif
326 
327  if (frame->format == dp->hwaccel_pix_fmt) {
328  int err = hwaccel_retrieve_data(dp->dec_ctx, frame);
329  if (err < 0)
330  return err;
331  }
332 
334 
335  // forced fixed framerate
338  frame->duration = 1;
340  }
341 
342  // no timestamp available - extrapolate from previous frame duration
343  if (frame->pts == AV_NOPTS_VALUE)
344  frame->pts = dp->last_frame_pts == AV_NOPTS_VALUE ? 0 :
346 
347  // update timestamp history
349  dp->last_frame_pts = frame->pts;
351 
352  if (debug_ts) {
353  av_log(dp, AV_LOG_INFO,
354  "decoder -> pts:%s pts_time:%s "
355  "pkt_dts:%s pkt_dts_time:%s "
356  "duration:%s duration_time:%s "
357  "keyframe:%d frame_type:%d time_base:%d/%d\n",
358  av_ts2str(frame->pts),
366  }
367 
368  if (dp->sar_override.num)
370 
371  return 0;
372 }
373 
374 static int copy_av_subtitle(AVSubtitle *dst, const AVSubtitle *src)
375 {
376  int ret = AVERROR_BUG;
377  AVSubtitle tmp = {
378  .format = src->format,
379  .start_display_time = src->start_display_time,
380  .end_display_time = src->end_display_time,
381  .num_rects = 0,
382  .rects = NULL,
383  .pts = src->pts
384  };
385 
386  if (!src->num_rects)
387  goto success;
388 
389  if (!(tmp.rects = av_calloc(src->num_rects, sizeof(*tmp.rects))))
390  return AVERROR(ENOMEM);
391 
392  for (int i = 0; i < src->num_rects; i++) {
393  AVSubtitleRect *src_rect = src->rects[i];
394  AVSubtitleRect *dst_rect;
395 
396  if (!(dst_rect = tmp.rects[i] = av_mallocz(sizeof(*tmp.rects[0])))) {
397  ret = AVERROR(ENOMEM);
398  goto cleanup;
399  }
400 
401  tmp.num_rects++;
402 
403  dst_rect->type = src_rect->type;
404  dst_rect->flags = src_rect->flags;
405 
406  dst_rect->x = src_rect->x;
407  dst_rect->y = src_rect->y;
408  dst_rect->w = src_rect->w;
409  dst_rect->h = src_rect->h;
410  dst_rect->nb_colors = src_rect->nb_colors;
411 
412  if (src_rect->text)
413  if (!(dst_rect->text = av_strdup(src_rect->text))) {
414  ret = AVERROR(ENOMEM);
415  goto cleanup;
416  }
417 
418  if (src_rect->ass)
419  if (!(dst_rect->ass = av_strdup(src_rect->ass))) {
420  ret = AVERROR(ENOMEM);
421  goto cleanup;
422  }
423 
424  for (int j = 0; j < 4; j++) {
425  // SUBTITLE_BITMAP images are special in the sense that they
426  // are like PAL8 images. first pointer to data, second to
427  // palette. This makes the size calculation match this.
428  size_t buf_size = src_rect->type == SUBTITLE_BITMAP && j == 1 ?
430  src_rect->h * src_rect->linesize[j];
431 
432  if (!src_rect->data[j])
433  continue;
434 
435  if (!(dst_rect->data[j] = av_memdup(src_rect->data[j], buf_size))) {
436  ret = AVERROR(ENOMEM);
437  goto cleanup;
438  }
439  dst_rect->linesize[j] = src_rect->linesize[j];
440  }
441  }
442 
443 success:
444  *dst = tmp;
445 
446  return 0;
447 
448 cleanup:
450 
451  return ret;
452 }
453 
454 static void subtitle_free(void *opaque, uint8_t *data)
455 {
456  AVSubtitle *sub = (AVSubtitle*)data;
457  avsubtitle_free(sub);
458  av_free(sub);
459 }
460 
461 static int subtitle_wrap_frame(AVFrame *frame, AVSubtitle *subtitle, int copy)
462 {
463  AVBufferRef *buf;
464  AVSubtitle *sub;
465  int ret;
466 
467  if (copy) {
468  sub = av_mallocz(sizeof(*sub));
469  ret = sub ? copy_av_subtitle(sub, subtitle) : AVERROR(ENOMEM);
470  if (ret < 0) {
471  av_freep(&sub);
472  return ret;
473  }
474  } else {
475  sub = av_memdup(subtitle, sizeof(*subtitle));
476  if (!sub)
477  return AVERROR(ENOMEM);
478  memset(subtitle, 0, sizeof(*subtitle));
479  }
480 
481  buf = av_buffer_create((uint8_t*)sub, sizeof(*sub),
482  subtitle_free, NULL, 0);
483  if (!buf) {
484  avsubtitle_free(sub);
485  av_freep(&sub);
486  return AVERROR(ENOMEM);
487  }
488 
489  frame->buf[0] = buf;
490 
491  return 0;
492 }
493 
495 {
496  const AVSubtitle *subtitle = (AVSubtitle*)frame->buf[0]->data;
497  int ret = 0;
498 
500  AVSubtitle *sub_prev = dp->sub_prev[0]->buf[0] ?
501  (AVSubtitle*)dp->sub_prev[0]->buf[0]->data : NULL;
502  int end = 1;
503  if (sub_prev) {
504  end = av_rescale(subtitle->pts - sub_prev->pts,
505  1000, AV_TIME_BASE);
506  if (end < sub_prev->end_display_time) {
507  av_log(dp, AV_LOG_DEBUG,
508  "Subtitle duration reduced from %"PRId32" to %d%s\n",
509  sub_prev->end_display_time, end,
510  end <= 0 ? ", dropping it" : "");
511  sub_prev->end_display_time = end;
512  }
513  }
514 
515  av_frame_unref(dp->sub_prev[1]);
517 
518  frame = dp->sub_prev[0];
519  subtitle = frame->buf[0] ? (AVSubtitle*)frame->buf[0]->data : NULL;
520 
521  FFSWAP(AVFrame*, dp->sub_prev[0], dp->sub_prev[1]);
522 
523  if (end <= 0)
524  return 0;
525  }
526 
527  if (!subtitle)
528  return 0;
529 
530  ret = sch_dec_send(dp->sch, dp->sch_idx, frame);
531  if (ret < 0)
533 
534  return ret == AVERROR_EOF ? AVERROR_EXIT : ret;
535 }
536 
537 static int fix_sub_duration_heartbeat(DecoderPriv *dp, int64_t signal_pts)
538 {
539  int ret = AVERROR_BUG;
540  AVSubtitle *prev_subtitle = dp->sub_prev[0]->buf[0] ?
541  (AVSubtitle*)dp->sub_prev[0]->buf[0]->data : NULL;
542  AVSubtitle *subtitle;
543 
544  if (!(dp->flags & DECODER_FLAG_FIX_SUB_DURATION) || !prev_subtitle ||
545  !prev_subtitle->num_rects || signal_pts <= prev_subtitle->pts)
546  return 0;
547 
549  ret = subtitle_wrap_frame(dp->sub_heartbeat, prev_subtitle, 1);
550  if (ret < 0)
551  return ret;
552 
553  subtitle = (AVSubtitle*)dp->sub_heartbeat->buf[0]->data;
554  subtitle->pts = signal_pts;
555 
556  return process_subtitle(dp, dp->sub_heartbeat);
557 }
558 
560  AVFrame *frame)
561 {
562  AVPacket *flush_pkt = NULL;
563  AVSubtitle subtitle;
564  int got_output;
565  int ret;
566 
567  if (pkt && (intptr_t)pkt->opaque == PKT_OPAQUE_SUB_HEARTBEAT) {
568  frame->pts = pkt->pts;
570  frame->opaque = (void*)(intptr_t)FRAME_OPAQUE_SUB_HEARTBEAT;
571 
572  ret = sch_dec_send(dp->sch, dp->sch_idx, frame);
573  return ret == AVERROR_EOF ? AVERROR_EXIT : ret;
574  } else if (pkt && (intptr_t)pkt->opaque == PKT_OPAQUE_FIX_SUB_DURATION) {
576  AV_TIME_BASE_Q));
577  }
578 
579  if (!pkt) {
580  flush_pkt = av_packet_alloc();
581  if (!flush_pkt)
582  return AVERROR(ENOMEM);
583  }
584 
585  ret = avcodec_decode_subtitle2(dp->dec_ctx, &subtitle, &got_output,
586  pkt ? pkt : flush_pkt);
587  av_packet_free(&flush_pkt);
588 
589  if (ret < 0) {
590  av_log(dp, AV_LOG_ERROR, "Error decoding subtitles: %s\n",
591  av_err2str(ret));
592  dp->dec.decode_errors++;
593  return exit_on_error ? ret : 0;
594  }
595 
596  if (!got_output)
597  return pkt ? 0 : AVERROR_EOF;
598 
599  dp->dec.frames_decoded++;
600 
601  // XXX the queue for transferring data to consumers runs
602  // on AVFrames, so we wrap AVSubtitle in an AVBufferRef and put that
603  // inside the frame
604  // eventually, subtitles should be switched to use AVFrames natively
605  ret = subtitle_wrap_frame(frame, &subtitle, 0);
606  if (ret < 0) {
607  avsubtitle_free(&subtitle);
608  return ret;
609  }
610 
611  frame->width = dp->dec_ctx->width;
612  frame->height = dp->dec_ctx->height;
613 
614  return process_subtitle(dp, frame);
615 }
616 
618 {
619  AVCodecContext *dec = dp->dec_ctx;
620  const char *type_desc = av_get_media_type_string(dec->codec_type);
621  int ret;
622 
623  if (dec->codec_type == AVMEDIA_TYPE_SUBTITLE)
624  return transcode_subtitles(dp, pkt, frame);
625 
626  // With fate-indeo3-2, we're getting 0-sized packets before EOF for some
627  // reason. This seems like a semi-critical bug. Don't trigger EOF, and
628  // skip the packet.
629  if (pkt && pkt->size == 0)
630  return 0;
631 
632  if (pkt && (dp->flags & DECODER_FLAG_TS_UNRELIABLE)) {
635  }
636 
637  if (pkt) {
638  FrameData *fd = packet_data(pkt);
639  if (!fd)
640  return AVERROR(ENOMEM);
642  }
643 
644  ret = avcodec_send_packet(dec, pkt);
645  if (ret < 0 && !(ret == AVERROR_EOF && !pkt)) {
646  // In particular, we don't expect AVERROR(EAGAIN), because we read all
647  // decoded frames with avcodec_receive_frame() until done.
648  if (ret == AVERROR(EAGAIN)) {
649  av_log(dp, AV_LOG_FATAL, "A decoder returned an unexpected error code. "
650  "This is a bug, please report it.\n");
651  return AVERROR_BUG;
652  }
653  av_log(dp, AV_LOG_ERROR, "Error submitting %s to decoder: %s\n",
654  pkt ? "packet" : "EOF", av_err2str(ret));
655 
656  if (ret != AVERROR_EOF) {
657  dp->dec.decode_errors++;
658  if (!exit_on_error)
659  ret = 0;
660  }
661 
662  return ret;
663  }
664 
665  while (1) {
666  FrameData *fd;
667 
669 
672  update_benchmark("decode_%s %s", type_desc, dp->parent_name);
673 
674  if (ret == AVERROR(EAGAIN)) {
675  av_assert0(pkt); // should never happen during flushing
676  return 0;
677  } else if (ret == AVERROR_EOF) {
678  return ret;
679  } else if (ret < 0) {
680  av_log(dp, AV_LOG_ERROR, "Decoding error: %s\n", av_err2str(ret));
681  dp->dec.decode_errors++;
682 
683  if (exit_on_error)
684  return ret;
685 
686  continue;
687  }
688 
691  "corrupt decoded frame\n");
692  if (exit_on_error)
693  return AVERROR_INVALIDDATA;
694  }
695 
696  fd = frame_data(frame);
697  if (!fd) {
699  return AVERROR(ENOMEM);
700  }
701  fd->dec.pts = frame->pts;
702  fd->dec.tb = dec->pkt_timebase;
703  fd->dec.frame_num = dec->frame_num - 1;
705 
707 
708  frame->time_base = dec->pkt_timebase;
709 
710  if (dec->codec_type == AVMEDIA_TYPE_AUDIO) {
712 
713  audio_ts_process(dp, frame);
714  } else {
716  if (ret < 0) {
717  av_log(dp, AV_LOG_FATAL,
718  "Error while processing the decoded data\n");
719  return ret;
720  }
721  }
722 
723  dp->dec.frames_decoded++;
724 
725  ret = sch_dec_send(dp->sch, dp->sch_idx, frame);
726  if (ret < 0) {
728  return ret == AVERROR_EOF ? AVERROR_EXIT : ret;
729  }
730  }
731 }
732 
733 static void dec_thread_set_name(const DecoderPriv *dp)
734 {
735  char name[16];
736  snprintf(name, sizeof(name), "dec%s:%s", dp->parent_name,
737  dp->dec_ctx->codec->name);
739 }
740 
742 {
743  av_packet_free(&dt->pkt);
744  av_frame_free(&dt->frame);
745 
746  memset(dt, 0, sizeof(*dt));
747 }
748 
750 {
751  memset(dt, 0, sizeof(*dt));
752 
753  dt->frame = av_frame_alloc();
754  if (!dt->frame)
755  goto fail;
756 
757  dt->pkt = av_packet_alloc();
758  if (!dt->pkt)
759  goto fail;
760 
761  return 0;
762 
763 fail:
764  dec_thread_uninit(dt);
765  return AVERROR(ENOMEM);
766 }
767 
768 static void *decoder_thread(void *arg)
769 {
770  DecoderPriv *dp = arg;
771  DecThreadContext dt;
772  int ret = 0, input_status = 0;
773 
774  ret = dec_thread_init(&dt);
775  if (ret < 0)
776  goto finish;
777 
779 
780  while (!input_status) {
781  int flush_buffers, have_data;
782 
783  input_status = sch_dec_receive(dp->sch, dp->sch_idx, dt.pkt);
784  have_data = input_status >= 0 &&
785  (dt.pkt->buf || dt.pkt->side_data_elems ||
786  (intptr_t)dt.pkt->opaque == PKT_OPAQUE_SUB_HEARTBEAT ||
787  (intptr_t)dt.pkt->opaque == PKT_OPAQUE_FIX_SUB_DURATION);
788  flush_buffers = input_status >= 0 && !have_data;
789  if (!have_data)
790  av_log(dp, AV_LOG_VERBOSE, "Decoder thread received %s packet\n",
791  flush_buffers ? "flush" : "EOF");
792 
793  ret = packet_decode(dp, have_data ? dt.pkt : NULL, dt.frame);
794 
795  av_packet_unref(dt.pkt);
796  av_frame_unref(dt.frame);
797 
798  // AVERROR_EOF - EOF from the decoder
799  // AVERROR_EXIT - EOF from the scheduler
800  // we treat them differently when flushing
801  if (ret == AVERROR_EXIT) {
802  ret = AVERROR_EOF;
803  flush_buffers = 0;
804  }
805 
806  if (ret == AVERROR_EOF) {
807  av_log(dp, AV_LOG_VERBOSE, "Decoder returned EOF, %s\n",
808  flush_buffers ? "resetting" : "finishing");
809 
810  if (!flush_buffers)
811  break;
812 
813  /* report last frame duration to the scheduler */
814  if (dp->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
816  dt.pkt->time_base = dp->last_frame_tb;
817  }
818 
820  } else if (ret < 0) {
821  av_log(dp, AV_LOG_ERROR, "Error processing packet in decoder: %s\n",
822  av_err2str(ret));
823  break;
824  }
825  }
826 
827  // EOF is normal thread termination
828  if (ret == AVERROR_EOF)
829  ret = 0;
830 
831  // on success send EOF timestamp to our downstreams
832  if (ret >= 0) {
833  float err_rate;
834 
835  av_frame_unref(dt.frame);
836 
837  dt.frame->opaque = (void*)(intptr_t)FRAME_OPAQUE_EOF;
840  dt.frame->time_base = dp->last_frame_tb;
841 
842  ret = sch_dec_send(dp->sch, dp->sch_idx, dt.frame);
843  if (ret < 0 && ret != AVERROR_EOF) {
844  av_log(dp, AV_LOG_FATAL,
845  "Error signalling EOF timestamp: %s\n", av_err2str(ret));
846  goto finish;
847  }
848  ret = 0;
849 
850  err_rate = (dp->dec.frames_decoded || dp->dec.decode_errors) ?
851  dp->dec.decode_errors / (dp->dec.frames_decoded + dp->dec.decode_errors) : 0.f;
852  if (err_rate > max_error_rate) {
853  av_log(dp, AV_LOG_FATAL, "Decode error rate %g exceeds maximum %g\n",
854  err_rate, max_error_rate);
856  } else if (err_rate)
857  av_log(dp, AV_LOG_VERBOSE, "Decode error rate %g\n", err_rate);
858  }
859 
860 finish:
861  dec_thread_uninit(&dt);
862 
863  return (void*)(intptr_t)ret;
864 }
865 
867 {
868  DecoderPriv *dp = s->opaque;
869  const enum AVPixelFormat *p;
870 
871  for (p = pix_fmts; *p != AV_PIX_FMT_NONE; p++) {
873  const AVCodecHWConfig *config = NULL;
874  int i;
875 
876  if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
877  break;
878 
879  if (dp->hwaccel_id == HWACCEL_GENERIC ||
880  dp->hwaccel_id == HWACCEL_AUTO) {
881  for (i = 0;; i++) {
882  config = avcodec_get_hw_config(s->codec, i);
883  if (!config)
884  break;
885  if (!(config->methods &
887  continue;
888  if (config->pix_fmt == *p)
889  break;
890  }
891  }
892  if (config && config->device_type == dp->hwaccel_device_type) {
893  dp->hwaccel_pix_fmt = *p;
894  break;
895  }
896  }
897 
898  return *p;
899 }
900 
902 {
903  const AVCodecHWConfig *config;
904  HWDevice *dev;
905  int i;
906  for (i = 0;; i++) {
907  config = avcodec_get_hw_config(codec, i);
908  if (!config)
909  return NULL;
911  continue;
912  dev = hw_device_get_by_type(config->device_type);
913  if (dev)
914  return dev;
915  }
916 }
917 
919  const AVCodec *codec,
920  const char *hwaccel_device)
921 {
922  const AVCodecHWConfig *config;
923  enum AVHWDeviceType type;
924  HWDevice *dev = NULL;
925  int err, auto_device = 0;
926 
927  if (hwaccel_device) {
928  dev = hw_device_get_by_name(hwaccel_device);
929  if (!dev) {
930  if (dp->hwaccel_id == HWACCEL_AUTO) {
931  auto_device = 1;
932  } else if (dp->hwaccel_id == HWACCEL_GENERIC) {
934  err = hw_device_init_from_type(type, hwaccel_device,
935  &dev);
936  } else {
937  // This will be dealt with by API-specific initialisation
938  // (using hwaccel_device), so nothing further needed here.
939  return 0;
940  }
941  } else {
942  if (dp->hwaccel_id == HWACCEL_AUTO) {
943  dp->hwaccel_device_type = dev->type;
944  } else if (dp->hwaccel_device_type != dev->type) {
945  av_log(dp, AV_LOG_ERROR, "Invalid hwaccel device "
946  "specified for decoder: device %s of type %s is not "
947  "usable with hwaccel %s.\n", dev->name,
950  return AVERROR(EINVAL);
951  }
952  }
953  } else {
954  if (dp->hwaccel_id == HWACCEL_AUTO) {
955  auto_device = 1;
956  } else if (dp->hwaccel_id == HWACCEL_GENERIC) {
959 
960  // When "-qsv_device device" is used, an internal QSV device named
961  // as "__qsv_device" is created. Another QSV device is created too
962  // if "-init_hw_device qsv=name:device" is used. There are 2 QSV devices
963  // if both "-qsv_device device" and "-init_hw_device qsv=name:device"
964  // are used, hw_device_get_by_type(AV_HWDEVICE_TYPE_QSV) returns NULL.
965  // To keep back-compatibility with the removed ad-hoc libmfx setup code,
966  // call hw_device_get_by_name("__qsv_device") to select the internal QSV
967  // device.
968  if (!dev && type == AV_HWDEVICE_TYPE_QSV)
969  dev = hw_device_get_by_name("__qsv_device");
970 
971  if (!dev)
972  err = hw_device_init_from_type(type, NULL, &dev);
973  } else {
974  dev = hw_device_match_by_codec(codec);
975  if (!dev) {
976  // No device for this codec, but not using generic hwaccel
977  // and therefore may well not need one - ignore.
978  return 0;
979  }
980  }
981  }
982 
983  if (auto_device) {
984  int i;
985  if (!avcodec_get_hw_config(codec, 0)) {
986  // Decoder does not support any hardware devices.
987  return 0;
988  }
989  for (i = 0; !dev; i++) {
990  config = avcodec_get_hw_config(codec, i);
991  if (!config)
992  break;
993  type = config->device_type;
995  if (dev) {
996  av_log(dp, AV_LOG_INFO, "Using auto "
997  "hwaccel type %s with existing device %s.\n",
999  }
1000  }
1001  for (i = 0; !dev; i++) {
1002  config = avcodec_get_hw_config(codec, i);
1003  if (!config)
1004  break;
1005  type = config->device_type;
1006  // Try to make a new device of this type.
1007  err = hw_device_init_from_type(type, hwaccel_device,
1008  &dev);
1009  if (err < 0) {
1010  // Can't make a device of this type.
1011  continue;
1012  }
1013  if (hwaccel_device) {
1014  av_log(dp, AV_LOG_INFO, "Using auto "
1015  "hwaccel type %s with new device created "
1016  "from %s.\n", av_hwdevice_get_type_name(type),
1017  hwaccel_device);
1018  } else {
1019  av_log(dp, AV_LOG_INFO, "Using auto "
1020  "hwaccel type %s with new default device.\n",
1022  }
1023  }
1024  if (dev) {
1025  dp->hwaccel_device_type = type;
1026  } else {
1027  av_log(dp, AV_LOG_INFO, "Auto hwaccel "
1028  "disabled: no device found.\n");
1029  dp->hwaccel_id = HWACCEL_NONE;
1030  return 0;
1031  }
1032  }
1033 
1034  if (!dev) {
1035  av_log(dp, AV_LOG_ERROR, "No device available "
1036  "for decoder: device type %s needed for codec %s.\n",
1038  return err;
1039  }
1040 
1042  if (!dp->dec_ctx->hw_device_ctx)
1043  return AVERROR(ENOMEM);
1044 
1045  return 0;
1046 }
1047 
1048 static const char *dec_item_name(void *obj)
1049 {
1050  const DecoderPriv *dp = obj;
1051 
1052  return dp->log_name;
1053 }
1054 
1055 static const AVClass dec_class = {
1056  .class_name = "Decoder",
1057  .version = LIBAVUTIL_VERSION_INT,
1058  .parent_log_context_offset = offsetof(DecoderPriv, log_parent),
1059  .item_name = dec_item_name,
1060 };
1061 
1062 int dec_open(Decoder **pdec, Scheduler *sch,
1063  AVDictionary **dec_opts, const DecoderOpts *o)
1064 {
1065  DecoderPriv *dp;
1066  const AVCodec *codec = o->codec;
1067  int ret;
1068 
1069  *pdec = NULL;
1070 
1071  ret = dec_alloc(&dp);
1072  if (ret < 0)
1073  return ret;
1074 
1076  if (ret < 0)
1077  return ret;
1078  dp->sch = sch;
1079  dp->sch_idx = ret;
1080 
1081  dp->flags = o->flags;
1082  dp->dec.class = &dec_class;
1083  dp->log_parent = o->log_parent;
1084 
1085  dp->framerate_in = o->framerate;
1086 
1087  dp->hwaccel_id = o->hwaccel_id;
1090 
1091  snprintf(dp->log_name, sizeof(dp->log_name), "dec:%s", codec->name);
1092 
1093  dp->parent_name = av_strdup(o->name ? o->name : "");
1094  if (!dp->parent_name) {
1095  ret = AVERROR(ENOMEM);
1096  goto fail;
1097  }
1098 
1099  if (codec->type == AVMEDIA_TYPE_SUBTITLE &&
1101  for (int i = 0; i < FF_ARRAY_ELEMS(dp->sub_prev); i++) {
1102  dp->sub_prev[i] = av_frame_alloc();
1103  if (!dp->sub_prev[i]) {
1104  ret = AVERROR(ENOMEM);
1105  goto fail;
1106  }
1107  }
1108  dp->sub_heartbeat = av_frame_alloc();
1109  if (!dp->sub_heartbeat) {
1110  ret = AVERROR(ENOMEM);
1111  goto fail;
1112  }
1113  }
1114 
1116 
1117  dp->dec_ctx = avcodec_alloc_context3(codec);
1118  if (!dp->dec_ctx) {
1119  ret = AVERROR(ENOMEM);
1120  goto fail;
1121  }
1122 
1124  if (ret < 0) {
1125  av_log(dp, AV_LOG_ERROR, "Error initializing the decoder context.\n");
1126  goto fail;
1127  }
1128 
1129  dp->dec_ctx->opaque = dp;
1130  dp->dec_ctx->get_format = get_format;
1131  dp->dec_ctx->pkt_timebase = o->time_base;
1132 
1133  if (!av_dict_get(*dec_opts, "threads", NULL, 0))
1134  av_dict_set(dec_opts, "threads", "auto", 0);
1135 
1136  av_dict_set(dec_opts, "flags", "+copy_opaque", AV_DICT_MULTIKEY);
1137 
1139  if (ret < 0) {
1140  av_log(dp, AV_LOG_ERROR,
1141  "Hardware device setup failed for decoder: %s\n",
1142  av_err2str(ret));
1143  goto fail;
1144  }
1145 
1146  if ((ret = avcodec_open2(dp->dec_ctx, codec, dec_opts)) < 0) {
1147  av_log(dp, AV_LOG_ERROR, "Error while opening decoder: %s\n",
1148  av_err2str(ret));
1149  goto fail;
1150  }
1151 
1152  ret = check_avoptions(*dec_opts);
1153  if (ret < 0)
1154  goto fail;
1155 
1158 
1159  *pdec = &dp->dec;
1160 
1161  return dp->sch_idx;
1162 fail:
1163  dec_free((Decoder**)&dp);
1164  return ret;
1165 }
1166 
1168 {
1169  DecoderPriv *dp = dp_from_dec(dec);
1170 
1171  // initialize fallback parameters for filtering
1172  return ifilter_parameters_from_dec(ifilter, dp->dec_ctx);
1173 }
DecoderPriv::last_frame_tb
AVRational last_frame_tb
Definition: ffmpeg_dec.c:63
AVSubtitle
Definition: avcodec.h:2273
Decoder::subtitle_header
const uint8_t * subtitle_header
Definition: ffmpeg.h:347
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:427
AVCodec
AVCodec.
Definition: codec.h:187
copy_av_subtitle
static int copy_av_subtitle(AVSubtitle *dst, const AVSubtitle *src)
Definition: ffmpeg_dec.c:374
fix_sub_duration_heartbeat
static int fix_sub_duration_heartbeat(DecoderPriv *dp, int64_t signal_pts)
Definition: ffmpeg_dec.c:537
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
av_gettime_relative
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
Definition: time.c:56
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
name
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
dec_class
static const AVClass dec_class
Definition: ffmpeg_dec.c:1055
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVCodecContext::get_format
enum AVPixelFormat(* get_format)(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
Callback to negotiate the pixel format.
Definition: avcodec.h:716
FrameData
Definition: ffmpeg.h:616
DecoderPriv::last_frame_duration_est
int64_t last_frame_duration_est
Definition: ffmpeg_dec.c:62
DecoderOpts
Definition: ffmpeg.h:322
audio_samplerate_update
static AVRational audio_samplerate_update(DecoderPriv *dp, const AVFrame *frame)
Definition: ffmpeg_dec.c:144
DECODER_FLAG_SEND_END_TS
@ DECODER_FLAG_SEND_END_TS
Definition: ffmpeg.h:319
AVFrame::duration
int64_t duration
Duration of the frame, in the same units as pts.
Definition: frame.h:807
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2968
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
LATENCY_PROBE_DEC_POST
@ LATENCY_PROBE_DEC_POST
Definition: ffmpeg.h:105
DecoderPriv::last_frame_pts
int64_t last_frame_pts
Definition: ffmpeg_dec.c:61
dec_thread_uninit
static void dec_thread_uninit(DecThreadContext *dt)
Definition: ffmpeg_dec.c:741
AV_TIME_BASE_Q
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:264
output
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
Definition: filter_design.txt:225
AVSubtitleRect
Definition: avcodec.h:2245
AVSubtitle::num_rects
unsigned num_rects
Definition: avcodec.h:2277
hw_device_match_by_codec
static HWDevice * hw_device_match_by_codec(const AVCodec *codec)
Definition: ffmpeg_dec.c:901
DecThreadContext::pkt
AVPacket * pkt
Definition: ffmpeg_dec.c:87
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:100
AVFrame::opaque
void * opaque
Frame owner's private data.
Definition: frame.h:501
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
pixdesc.h
cleanup
static av_cold void cleanup(FlashSV2Context *s)
Definition: flashsv2enc.c:130
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:452
AVFrame::width
int width
Definition: frame.h:412
DECODER_FLAG_FRAMERATE_FORCED
@ DECODER_FLAG_FRAMERATE_FORCED
Definition: ffmpeg.h:315
DecoderOpts::par
const AVCodecParameters * par
Definition: ffmpeg.h:329
dec_item_name
static const char * dec_item_name(void *obj)
Definition: ffmpeg_dec.c:1048
ifilter_parameters_from_dec
int ifilter_parameters_from_dec(InputFilter *ifilter, const AVCodecContext *dec)
Set up fallback filtering parameters from a decoder context.
Definition: ffmpeg_filter.c:1820
dec_thread_init
static int dec_thread_init(DecThreadContext *dt)
Definition: ffmpeg_dec.c:749
DecoderPriv::hwaccel_device_type
enum AVHWDeviceType hwaccel_device_type
Definition: ffmpeg_dec.c:55
DecoderPriv::sub_prev
AVFrame * sub_prev[2]
Definition: ffmpeg_dec.c:68
DecoderPriv::hwaccel_output_format
enum AVPixelFormat hwaccel_output_format
Definition: ffmpeg_dec.c:56
data
const char data[16]
Definition: mxf.c:148
DecoderOpts::hwaccel_id
enum HWAccelID hwaccel_id
Definition: ffmpeg.h:332
DecoderPriv::pkt
AVPacket * pkt
Definition: ffmpeg_dec.c:43
AVCodecContext::subtitle_header
uint8_t * subtitle_header
Header containing style information for text subtitles.
Definition: avcodec.h:1780
AVSubtitleRect::linesize
int linesize[4]
Definition: avcodec.h:2257
ffmpeg.h
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
DecoderPriv::sub_heartbeat
AVFrame * sub_heartbeat
Definition: ffmpeg_dec.c:69
dec_add_filter
int dec_add_filter(Decoder *dec, InputFilter *ifilter)
Definition: ffmpeg_dec.c:1167
AVDictionary
Definition: dict.c:34
AVFrame::flags
int flags
Frame flags, a combination of AV_FRAME_FLAGS.
Definition: frame.h:649
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
HWDevice
Definition: ffmpeg.h:113
av_buffer_ref
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:103
AVFrame::buf
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:590
DecoderPriv::hwaccel_id
enum HWAccelID hwaccel_id
Definition: ffmpeg_dec.c:54
tf_sess_config.config
config
Definition: tf_sess_config.py:33
av_packet_free
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:74
dec_free
void dec_free(Decoder **pdec)
Definition: ffmpeg_dec.c:90
av_gcd
int64_t av_gcd(int64_t a, int64_t b)
Compute the greatest common divisor of two integer operands.
Definition: mathematics.c:37
AV_FRAME_FLAG_TOP_FIELD_FIRST
#define AV_FRAME_FLAG_TOP_FIELD_FIRST
A flag to mark frames where the top field is displayed first if the content is interlaced.
Definition: frame.h:641
FrameData::frame_num
uint64_t frame_num
Definition: ffmpeg.h:623
av_memdup
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition: mem.c:302
DecoderPriv::dec_ctx
AVCodecContext * dec_ctx
Definition: ffmpeg_dec.c:40
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:1807
DecoderOpts::hwaccel_output_format
enum AVPixelFormat hwaccel_output_format
Definition: ffmpeg.h:335
debug_ts
int debug_ts
Definition: ffmpeg_opt.c:76
finish
static void finish(void)
Definition: movenc.c:342
FRAME_OPAQUE_SUB_HEARTBEAT
@ FRAME_OPAQUE_SUB_HEARTBEAT
Definition: ffmpeg.h:92
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:454
DecoderPriv
Definition: ffmpeg_dec.c:37
Decoder::frames_decoded
uint64_t frames_decoded
Definition: ffmpeg.h:351
fail
#define fail()
Definition: checkasm.h:179
sch_dec_send
int sch_dec_send(Scheduler *sch, unsigned dec_idx, AVFrame *frame)
Called by decoder tasks to send a decoded frame downstream.
Definition: ffmpeg_sched.c:2005
AV_PIX_FMT_FLAG_HWACCEL
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition: pixdesc.h:128
get_format
static enum AVPixelFormat get_format(AVCodecContext *s, const enum AVPixelFormat *pix_fmts)
Definition: ffmpeg_dec.c:866
AVSubtitleRect::x
int x
top left corner of pict, undefined when pict is not set
Definition: avcodec.h:2246
DecoderPriv::log_parent
void * log_parent
Definition: ffmpeg_dec.c:74
DecoderOpts::log_parent
void * log_parent
Definition: ffmpeg.h:326
DecoderPriv::dec
Decoder dec
Definition: ffmpeg_dec.c:38
update_benchmark
void update_benchmark(const char *fmt,...)
Definition: ffmpeg.c:472
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
AV_ROUND_UP
@ AV_ROUND_UP
Round toward +infinity.
Definition: mathematics.h:134
FrameData::tb
AVRational tb
Definition: ffmpeg.h:626
codec.h
AVRational::num
int num
Numerator.
Definition: rational.h:59
DecoderPriv::parent_name
char * parent_name
Definition: ffmpeg_dec.c:76
Decoder::samples_decoded
uint64_t samples_decoded
Definition: ffmpeg.h:352
AVSubtitleRect::ass
char * ass
0 terminated ASS/SSA compatible event line.
Definition: avcodec.h:2268
avsubtitle_free
void avsubtitle_free(AVSubtitle *sub)
Free all allocated data in the given subtitle struct.
Definition: avcodec.c:413
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:88
avcodec_decode_subtitle2
int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub, int *got_sub_ptr, const AVPacket *avpkt)
Decode a subtitle message.
Definition: decode.c:969
avassert.h
DecThreadContext
Definition: ffmpeg_dec.c:85
DecoderPriv::log_name
char log_name[32]
Definition: ffmpeg_dec.c:75
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
dec_open
int dec_open(Decoder **pdec, Scheduler *sch, AVDictionary **dec_opts, const DecoderOpts *o)
Definition: ffmpeg_dec.c:1062
DecoderPriv::frame
AVFrame * frame
Definition: ffmpeg_dec.c:42
hwaccel_retrieve_data
static int hwaccel_retrieve_data(AVCodecContext *avctx, AVFrame *input)
Definition: ffmpeg_dec.c:276
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:628
DecThreadContext::frame
AVFrame * frame
Definition: ffmpeg_dec.c:86
av_dict_get
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:62
HWACCEL_GENERIC
@ HWACCEL_GENERIC
Definition: ffmpeg.h:88
avcodec_alloc_context3
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:154
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVCodecParameters::sample_aspect_ratio
AVRational sample_aspect_ratio
Video only.
Definition: codec_par.h:131
subtitle_wrap_frame
static int subtitle_wrap_frame(AVFrame *frame, AVSubtitle *subtitle, int copy)
Definition: ffmpeg_dec.c:461
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVSubtitleRect::y
int y
top left corner of pict, undefined when pict is not set
Definition: avcodec.h:2247
InputFilter
Definition: ffmpeg.h:279
avcodec_receive_frame
int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Return decoded output data from a decoder or encoder (when the AV_CODEC_FLAG_RECON_FRAME flag is used...
Definition: avcodec.c:715
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
AVHWDeviceType
AVHWDeviceType
Definition: hwcontext.h:27
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:304
AVCodecContext::bits_per_raw_sample
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1521
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
FrameData::dec
struct FrameData::@4 dec
av_rescale_q
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
max_error_rate
float max_error_rate
Definition: ffmpeg_opt.c:81
AVSubtitle::pts
int64_t pts
Same as packet pts, in AV_TIME_BASE.
Definition: avcodec.h:2279
DecoderOpts::hwaccel_device
char * hwaccel_device
Definition: ffmpeg.h:334
ffmpeg_utils.h
DecoderPriv::last_filter_in_rescale_delta
int64_t last_filter_in_rescale_delta
Definition: ffmpeg_dec.c:64
av_hwdevice_get_type_name
const char * av_hwdevice_get_type_name(enum AVHWDeviceType type)
Get the string name of an AVHWDeviceType.
Definition: hwcontext.c:97
AVPacket::opaque
void * opaque
for some private data of the user
Definition: packet.h:547
AVSubtitleRect::text
char * text
0 terminated plain UTF-8 text
Definition: avcodec.h:2261
av_rescale_delta
int64_t av_rescale_delta(AVRational in_tb, int64_t in_ts, AVRational fs_tb, int duration, int64_t *last, AVRational out_tb)
Rescale a timestamp while preserving known durations.
Definition: mathematics.c:168
frame
static AVFrame * frame
Definition: demux_decode.c:54
arg
const char * arg
Definition: jacosubdec.c:67
fields
the definition of that something depends on the semantic of the filter The callback must examine the status of the filter s links and proceed accordingly The status of output links is stored in the status_in and status_out fields and tested by the then the processing requires a frame on this link and the filter is expected to make efforts in that direction The status of input links is stored by the fifo and status_out fields
Definition: filter_design.txt:155
if
if(ret)
Definition: filter_design.txt:179
audio_ts_process
static void audio_ts_process(DecoderPriv *dp, AVFrame *frame)
Definition: ffmpeg_dec.c:187
AVPacket::buf
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: packet.h:505
avcodec_parameters_to_context
int avcodec_parameters_to_context(AVCodecContext *codec, const struct AVCodecParameters *par)
Fill the codec context based on the values from the supplied codec parameters.
AVSubtitleRect::w
int w
width of pict, undefined when pict is not set
Definition: avcodec.h:2248
hw_device_setup_for_decode
static int hw_device_setup_for_decode(DecoderPriv *dp, const AVCodec *codec, const char *hwaccel_device)
Definition: ffmpeg_dec.c:918
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
Decoder::decode_errors
uint64_t decode_errors
Definition: ffmpeg.h:353
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:736
AV_DICT_MULTIKEY
#define AV_DICT_MULTIKEY
Allow to store several equal keys in the dictionary.
Definition: dict.h:84
DecoderPriv::framerate_in
AVRational framerate_in
Definition: ffmpeg_dec.c:48
AVCodec::type
enum AVMediaType type
Definition: codec.h:200
Decoder
Definition: ffmpeg.h:344
dec_thread_set_name
static void dec_thread_set_name(const DecoderPriv *dp)
Definition: ffmpeg_dec.c:733
hw_device_init_from_type
int hw_device_init_from_type(enum AVHWDeviceType type, const char *device, HWDevice **dev_out)
Definition: ffmpeg_hw.c:245
avcodec_free_context
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer.
Definition: options.c:169
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
transcode_subtitles
static int transcode_subtitles(DecoderPriv *dp, const AVPacket *pkt, AVFrame *frame)
Definition: ffmpeg_dec.c:559
AVPALETTE_SIZE
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
AVCodecContext::subtitle_header_size
int subtitle_header_size
Definition: avcodec.h:1781
AVSubtitleRect::data
uint8_t * data[4]
data+linesize for the bitmap of this subtitle.
Definition: avcodec.h:2256
sch_add_dec
int sch_add_dec(Scheduler *sch, SchThreadFunc func, void *ctx, int send_end_ts)
Add a decoder to the scheduler.
Definition: ffmpeg_sched.c:761
FrameData::wallclock
int64_t wallclock[LATENCY_PROBE_NB]
Definition: ffmpeg.h:633
AVFrame::pkt_dts
int64_t pkt_dts
DTS copied from the AVPacket that triggered returning this frame.
Definition: frame.h:459
avcodec_open2
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: avcodec.c:128
time.h
DecoderPriv::hwaccel_pix_fmt
enum AVPixelFormat hwaccel_pix_fmt
Definition: ffmpeg_dec.c:53
DECODER_FLAG_FIX_SUB_DURATION
@ DECODER_FLAG_FIX_SUB_DURATION
Definition: ffmpeg.h:310
DecoderPriv::last_frame_sample_rate
int last_frame_sample_rate
Definition: ffmpeg_dec.c:65
av_buffer_create
AVBufferRef * av_buffer_create(uint8_t *data, size_t size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition: buffer.c:55
error.h
AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX
@ AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX
The codec supports this format via the hw_device_ctx interface.
Definition: codec.h:309
Scheduler
Definition: ffmpeg_sched.c:259
AVFrame::best_effort_timestamp
int64_t best_effort_timestamp
frame timestamp estimated using various heuristics, in stream time base
Definition: frame.h:676
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:442
av_ts2timestr
#define av_ts2timestr(ts, tb)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:76
AVPacket::size
int size
Definition: packet.h:523
DecoderPriv::sch
Scheduler * sch
Definition: ffmpeg_dec.c:71
copy
static void copy(const float *p1, float *p2, const int length)
Definition: vf_vaguedenoiser.c:185
av_err2str
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:121
AVFrame::sample_rate
int sample_rate
Sample rate of the audio data.
Definition: frame.h:567
AVCodecContext::pkt_timebase
AVRational pkt_timebase
Timebase in which pkt_dts/pts and AVPacket.dts/pts are expressed.
Definition: avcodec.h:1821
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
check_avoptions
int check_avoptions(AVDictionary *m)
Definition: ffmpeg.c:461
AVFrame::time_base
AVRational time_base
Time base for the timestamps in this frame.
Definition: frame.h:467
HWDevice::device_ref
AVBufferRef * device_ref
Definition: ffmpeg.h:116
hw_device_get_by_type
HWDevice * hw_device_get_by_type(enum AVHWDeviceType type)
Definition: ffmpeg_hw.c:30
decoder_thread
static void * decoder_thread(void *arg)
Definition: ffmpeg_dec.c:768
AVFrame::format
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:427
frame_data
FrameData * frame_data(AVFrame *frame)
Get our axiliary frame data attached to the frame, allocating it if needed.
Definition: ffmpeg.c:428
AVSubtitle::end_display_time
uint32_t end_display_time
Definition: avcodec.h:2276
AVSubtitleRect::type
enum AVSubtitleType type
Definition: avcodec.h:2259
LATENCY_PROBE_DEC_PRE
@ LATENCY_PROBE_DEC_PRE
Definition: ffmpeg.h:104
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:521
FrameData::pts
int64_t pts
Definition: ffmpeg.h:625
video_frame_process
static int video_frame_process(DecoderPriv *dp, AVFrame *frame)
Definition: ffmpeg_dec.c:318
av_packet_alloc
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:63
input
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some input
Definition: filter_design.txt:172
SUBTITLE_BITMAP
@ SUBTITLE_BITMAP
A bitmap, pict will be set.
Definition: avcodec.h:2228
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
HWACCEL_AUTO
@ HWACCEL_AUTO
Definition: ffmpeg.h:87
AVSubtitleRect::flags
int flags
Definition: avcodec.h:2270
avcodec_send_packet
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
Definition: decode.c:709
dec_alloc
static int dec_alloc(DecoderPriv **pdec)
Definition: ffmpeg_dec.c:113
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:420
video_duration_estimate
static int64_t video_duration_estimate(const DecoderPriv *dp, const AVFrame *frame)
Definition: ffmpeg_dec.c:224
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:245
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:515
process_subtitle
static int process_subtitle(DecoderPriv *dp, AVFrame *frame)
Definition: ffmpeg_dec.c:494
FrameData::bits_per_raw_sample
int bits_per_raw_sample
Definition: ffmpeg.h:631
AV_FRAME_FLAG_CORRUPT
#define AV_FRAME_FLAG_CORRUPT
The frame data may be corrupted, e.g.
Definition: frame.h:624
AVSubtitleRect::nb_colors
int nb_colors
number of colors in pict, undefined when pict is not set
Definition: avcodec.h:2250
AV_TIME_BASE
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
packet_decode
static int packet_decode(DecoderPriv *dp, AVPacket *pkt, AVFrame *frame)
Definition: ffmpeg_dec.c:617
DecoderOpts::time_base
AVRational time_base
Definition: ffmpeg.h:337
exit_on_error
int exit_on_error
Definition: ffmpeg_opt.c:77
FRAME_OPAQUE_EOF
@ FRAME_OPAQUE_EOF
Definition: ffmpeg.h:93
av_frame_move_ref
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition: frame.c:649
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:622
tb
#define tb
Definition: regdef.h:68
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:254
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
av_inv_q
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
AVCodecContext::hw_device_ctx
AVBufferRef * hw_device_ctx
A reference to the AVHWDeviceContext describing the device which will be used by a hardware encoder/d...
Definition: avcodec.h:1985
av_rescale
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
AVCodecContext::height
int height
Definition: avcodec.h:625
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:262
avcodec.h
AVCodecContext::frame_num
int64_t frame_num
Frame counter, set by libavcodec.
Definition: avcodec.h:2122
AVFrame::decode_error_flags
int decode_error_flags
decode error flags of the frame, set to a combination of FF_DECODE_ERROR_xxx flags if the decoder pro...
Definition: frame.h:717
ret
ret
Definition: filter_design.txt:187
AV_LOG_FATAL
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition: log.h:174
AVSubtitleRect::h
int h
height of pict, undefined when pict is not set
Definition: avcodec.h:2249
pixfmt.h
sch_dec_receive
int sch_dec_receive(Scheduler *sch, unsigned dec_idx, AVPacket *pkt)
Called by decoder tasks to receive a packet for decoding.
Definition: ffmpeg_sched.c:1929
FFSWAP
#define FFSWAP(type, a, b)
Definition: macros.h:52
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
avcodec_flush_buffers
void avcodec_flush_buffers(AVCodecContext *avctx)
Reset the internal codec state / flush internal buffers.
Definition: avcodec.c:384
DecoderPriv::flags
int flags
Definition: ffmpeg_dec.c:51
AVCodecContext::opaque
void * opaque
Private data of the user, can be used to carry app specific stuff.
Definition: avcodec.h:487
DECODER_FLAG_TOP_FIELD_FIRST
@ DECODER_FLAG_TOP_FIELD_FIRST
Definition: ffmpeg.h:317
HWAccelID
HWAccelID
Definition: ffmpeg.h:85
dict.h
AVFrame::sample_aspect_ratio
AVRational sample_aspect_ratio
Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
Definition: frame.h:447
av_hwframe_transfer_data
int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
Copy data to or from a hw surface.
Definition: hwcontext.c:452
DecoderPriv::sar_override
AVRational sar_override
Definition: ffmpeg_dec.c:46
AV_HWDEVICE_TYPE_QSV
@ AV_HWDEVICE_TYPE_QSV
Definition: hwcontext.h:33
av_get_media_type_string
const char * av_get_media_type_string(enum AVMediaType media_type)
Return a string describing the media_type enum, NULL if media_type is unknown.
Definition: utils.c:28
AVCodecContext
main external API structure.
Definition: avcodec.h:445
AVFrame::height
int height
Definition: frame.h:412
PKT_OPAQUE_SUB_HEARTBEAT
@ PKT_OPAQUE_SUB_HEARTBEAT
Definition: ffmpeg.h:98
HWDevice::name
const char * name
Definition: ffmpeg.h:114
dp_from_dec
static DecoderPriv * dp_from_dec(Decoder *d)
Definition: ffmpeg_dec.c:79
AVRational::den
int den
Denominator.
Definition: rational.h:60
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
output_format
static char * output_format
Definition: ffprobe.c:143
Decoder::subtitle_header_size
int subtitle_header_size
Definition: ffmpeg.h:348
thread_queue.h
av_mul_q
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
HWACCEL_NONE
@ HWACCEL_NONE
Definition: ffmpeg.h:86
avcodec_get_hw_config
const AVCodecHWConfig * avcodec_get_hw_config(const AVCodec *codec, int index)
Retrieve supported hardware configurations for a codec.
Definition: utils.c:859
DecoderOpts::flags
int flags
Definition: ffmpeg.h:323
AVCodecContext::codec_type
enum AVMediaType codec_type
Definition: avcodec.h:453
av_strdup
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:270
desc
const char * desc
Definition: libsvtav1.c:83
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
DECODER_FLAG_TS_UNRELIABLE
@ DECODER_FLAG_TS_UNRELIABLE
Definition: ffmpeg.h:312
DecoderOpts::codec
const AVCodec * codec
Definition: ffmpeg.h:328
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
FFMPEG_ERROR_RATE_EXCEEDED
#define FFMPEG_ERROR_RATE_EXCEEDED
Definition: ffmpeg.h:67
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
AVPacket
This structure stores compressed data.
Definition: packet.h:499
Decoder::class
const AVClass * class
Definition: ffmpeg.h:345
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
av_dict_set
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:88
HWDevice::type
enum AVHWDeviceType type
Definition: ffmpeg.h:115
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
packet_data
FrameData * packet_data(AVPacket *pkt)
Definition: ffmpeg.c:440
d
d
Definition: ffmpeg_filter.c:424
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:625
timestamp.h
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:52
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
DecoderOpts::framerate
AVRational framerate
Definition: ffmpeg.h:341
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
av_ts2str
#define av_ts2str(ts)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:54
AVCodecHWConfig
Definition: codec.h:341
AVERROR_EXIT
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition: error.h:58
subtitle_free
static void subtitle_free(void *opaque, uint8_t *data)
Definition: ffmpeg_dec.c:454
DecoderPriv::sch_idx
unsigned sch_idx
Definition: ffmpeg_dec.c:72
hw_device_get_by_name
HWDevice * hw_device_get_by_name(const char *name)
Definition: ffmpeg_hw.c:44
snprintf
#define snprintf
Definition: snprintf.h:34
PKT_OPAQUE_FIX_SUB_DURATION
@ PKT_OPAQUE_FIX_SUB_DURATION
Definition: ffmpeg.h:99
buffersrc.h
DecoderOpts::hwaccel_device_type
enum AVHWDeviceType hwaccel_device_type
Definition: ffmpeg.h:333
av_rescale_q_rnd
int64_t av_rescale_q_rnd(int64_t a, AVRational bq, AVRational cq, enum AVRounding rnd)
Rescale a 64-bit integer by 2 rational numbers with specified rounding.
Definition: mathematics.c:134
AVFrame::repeat_pict
int repeat_pict
Number of fields in this frame which should be repeated, i.e.
Definition: frame.h:521
AVPacket::time_base
AVRational time_base
Time base of the packet's timestamps.
Definition: packet.h:566
AVPacket::side_data_elems
int side_data_elems
Definition: packet.h:534
ff_thread_setname
static int ff_thread_setname(const char *name)
Definition: thread.h:216
DecoderOpts::name
char * name
Definition: ffmpeg.h:325