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/timestamp.h"
26 
27 #include "libavcodec/avcodec.h"
28 #include "libavcodec/codec.h"
29 
30 #include "libavfilter/buffersrc.h"
31 
32 #include "ffmpeg.h"
33 #include "thread_queue.h"
34 
35 struct Decoder {
38 
40 
41  // pts/estimated duration of the last decoded frame
42  // * in decoder timebase for video,
43  // * in last_frame_tb (may change during decoding) for audio
44  int64_t last_frame_pts;
49 
50  /* previous decoded subtitles */
53 
55  /**
56  * Queue for sending coded packets from the main thread to
57  * the decoder thread.
58  *
59  * An empty packet is sent to flush the decoder without terminating
60  * decoding.
61  */
63  /**
64  * Queue for sending decoded frames from the decoder thread
65  * to the main thread.
66  *
67  * An empty frame is sent to signal that a single packet has been fully
68  * processed.
69  */
71 };
72 
73 // data that is local to the decoder thread and not visible outside of it
74 typedef struct DecThreadContext {
78 
80 {
81  void *ret;
82 
83  if (!d->queue_in)
84  return 0;
85 
86  tq_send_finish(d->queue_in, 0);
87  tq_receive_finish(d->queue_out, 0);
88 
89  pthread_join(d->thread, &ret);
90 
91  tq_free(&d->queue_in);
92  tq_free(&d->queue_out);
93 
94  return (intptr_t)ret;
95 }
96 
97 void dec_free(Decoder **pdec)
98 {
99  Decoder *dec = *pdec;
100 
101  if (!dec)
102  return;
103 
104  dec_thread_stop(dec);
105 
106  av_frame_free(&dec->frame);
107  av_packet_free(&dec->pkt);
108 
109  for (int i = 0; i < FF_ARRAY_ELEMS(dec->sub_prev); i++)
110  av_frame_free(&dec->sub_prev[i]);
112 
113  av_freep(pdec);
114 }
115 
116 static int dec_alloc(Decoder **pdec)
117 {
118  Decoder *dec;
119 
120  *pdec = NULL;
121 
122  dec = av_mallocz(sizeof(*dec));
123  if (!dec)
124  return AVERROR(ENOMEM);
125 
126  dec->frame = av_frame_alloc();
127  if (!dec->frame)
128  goto fail;
129 
130  dec->pkt = av_packet_alloc();
131  if (!dec->pkt)
132  goto fail;
133 
136  dec->last_frame_tb = (AVRational){ 1, 1 };
138 
139  *pdec = dec;
140 
141  return 0;
142 fail:
143  dec_free(&dec);
144  return AVERROR(ENOMEM);
145 }
146 
147 static int send_frame_to_filters(InputStream *ist, AVFrame *decoded_frame)
148 {
149  int i, ret;
150 
151  av_assert1(ist->nb_filters > 0); /* ensure ret is initialized */
152  for (i = 0; i < ist->nb_filters; i++) {
153  ret = ifilter_send_frame(ist->filters[i], decoded_frame, i < ist->nb_filters - 1);
154  if (ret == AVERROR_EOF)
155  ret = 0; /* ignore */
156  if (ret < 0) {
158  "Failed to inject frame into filter network: %s\n", av_err2str(ret));
159  break;
160  }
161  }
162  return ret;
163 }
164 
166  const AVFrame *frame)
167 {
168  const int prev = d->last_frame_tb.den;
169  const int sr = frame->sample_rate;
170 
171  AVRational tb_new;
172  int64_t gcd;
173 
174  if (frame->sample_rate == d->last_frame_sample_rate)
175  goto finish;
176 
177  gcd = av_gcd(prev, sr);
178 
179  if (prev / gcd >= INT_MAX / sr) {
180  av_log(logctx, AV_LOG_WARNING,
181  "Audio timestamps cannot be represented exactly after "
182  "sample rate change: %d -> %d\n", prev, sr);
183 
184  // LCM of 192000, 44100, allows to represent all common samplerates
185  tb_new = (AVRational){ 1, 28224000 };
186  } else
187  tb_new = (AVRational){ 1, prev / gcd * sr };
188 
189  // keep the frame timebase if it is strictly better than
190  // the samplerate-defined one
191  if (frame->time_base.num == 1 && frame->time_base.den > tb_new.den &&
192  !(frame->time_base.den % tb_new.den))
193  tb_new = frame->time_base;
194 
195  if (d->last_frame_pts != AV_NOPTS_VALUE)
196  d->last_frame_pts = av_rescale_q(d->last_frame_pts,
197  d->last_frame_tb, tb_new);
198  d->last_frame_duration_est = av_rescale_q(d->last_frame_duration_est,
199  d->last_frame_tb, tb_new);
200 
201  d->last_frame_tb = tb_new;
202  d->last_frame_sample_rate = frame->sample_rate;
203 
204 finish:
205  return d->last_frame_tb;
206 }
207 
208 static void audio_ts_process(void *logctx, Decoder *d, AVFrame *frame)
209 {
210  AVRational tb_filter = (AVRational){1, frame->sample_rate};
211  AVRational tb;
212  int64_t pts_pred;
213 
214  // on samplerate change, choose a new internal timebase for timestamp
215  // generation that can represent timestamps from all the samplerates
216  // seen so far
217  tb = audio_samplerate_update(logctx, d, frame);
218  pts_pred = d->last_frame_pts == AV_NOPTS_VALUE ? 0 :
219  d->last_frame_pts + d->last_frame_duration_est;
220 
221  if (frame->pts == AV_NOPTS_VALUE) {
222  frame->pts = pts_pred;
223  frame->time_base = tb;
224  } else if (d->last_frame_pts != AV_NOPTS_VALUE &&
225  frame->pts > av_rescale_q_rnd(pts_pred, tb, frame->time_base,
226  AV_ROUND_UP)) {
227  // there was a gap in timestamps, reset conversion state
228  d->last_filter_in_rescale_delta = AV_NOPTS_VALUE;
229  }
230 
232  tb, frame->nb_samples,
233  &d->last_filter_in_rescale_delta, tb);
234 
235  d->last_frame_pts = frame->pts;
236  d->last_frame_duration_est = av_rescale_q(frame->nb_samples,
237  tb_filter, tb);
238 
239  // finally convert to filtering timebase
240  frame->pts = av_rescale_q(frame->pts, tb, tb_filter);
242  frame->time_base = tb_filter;
243 }
244 
245 static int64_t video_duration_estimate(const InputStream *ist, const AVFrame *frame)
246 {
247  const Decoder *d = ist->decoder;
248  const InputFile *ifile = input_files[ist->file_index];
249  int64_t codec_duration = 0;
250 
251  // XXX lavf currently makes up frame durations when they are not provided by
252  // the container. As there is no way to reliably distinguish real container
253  // durations from the fake made-up ones, we use heuristics based on whether
254  // the container has timestamps. Eventually lavf should stop making up
255  // durations, then this should be simplified.
256 
257  // prefer frame duration for containers with timestamps
258  if (frame->duration > 0 && (!ifile->format_nots || ist->framerate.num))
259  return frame->duration;
260 
261  if (ist->dec_ctx->framerate.den && ist->dec_ctx->framerate.num) {
262  int fields = frame->repeat_pict + 2;
263  AVRational field_rate = av_mul_q(ist->dec_ctx->framerate,
264  (AVRational){ 2, 1 });
265  codec_duration = av_rescale_q(fields, av_inv_q(field_rate),
266  frame->time_base);
267  }
268 
269  // prefer codec-layer duration for containers without timestamps
270  if (codec_duration > 0 && ifile->format_nots)
271  return codec_duration;
272 
273  // when timestamps are available, repeat last frame's actual duration
274  // (i.e. pts difference between this and last frame)
275  if (frame->pts != AV_NOPTS_VALUE && d->last_frame_pts != AV_NOPTS_VALUE &&
276  frame->pts > d->last_frame_pts)
277  return frame->pts - d->last_frame_pts;
278 
279  // try frame/codec duration
280  if (frame->duration > 0)
281  return frame->duration;
282  if (codec_duration > 0)
283  return codec_duration;
284 
285  // try average framerate
286  if (ist->st->avg_frame_rate.num && ist->st->avg_frame_rate.den) {
287  int64_t d = av_rescale_q(1, av_inv_q(ist->st->avg_frame_rate),
288  frame->time_base);
289  if (d > 0)
290  return d;
291  }
292 
293  // last resort is last frame's estimated duration, and 1
294  return FFMAX(d->last_frame_duration_est, 1);
295 }
296 
298 {
299  Decoder *d = ist->decoder;
300 
301  // The following line may be required in some cases where there is no parser
302  // or the parser does not has_b_frames correctly
303  if (ist->par->video_delay < ist->dec_ctx->has_b_frames) {
304  if (ist->dec_ctx->codec_id == AV_CODEC_ID_H264) {
305  ist->par->video_delay = ist->dec_ctx->has_b_frames;
306  } else
308  "video_delay is larger in decoder than demuxer %d > %d.\n"
309  "If you want to help, upload a sample "
310  "of this file to https://streams.videolan.org/upload/ "
311  "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)\n",
312  ist->dec_ctx->has_b_frames,
313  ist->par->video_delay);
314  }
315 
316  if (ist->dec_ctx->width != frame->width ||
317  ist->dec_ctx->height != frame->height ||
318  ist->dec_ctx->pix_fmt != frame->format) {
319  av_log(NULL, AV_LOG_DEBUG, "Frame parameters mismatch context %d,%d,%d != %d,%d,%d\n",
320  frame->width,
321  frame->height,
322  frame->format,
323  ist->dec_ctx->width,
324  ist->dec_ctx->height,
325  ist->dec_ctx->pix_fmt);
326  }
327 
328 #if FFMPEG_OPT_TOP
329  if(ist->top_field_first>=0) {
330  av_log(ist, AV_LOG_WARNING, "-top is deprecated, use the setfield filter instead\n");
332  }
333 #endif
334 
335  if (frame->format == d->hwaccel_pix_fmt) {
336  int err = hwaccel_retrieve_data(ist->dec_ctx, frame);
337  if (err < 0)
338  return err;
339  }
340 
342 
343  // forced fixed framerate
344  if (ist->framerate.num) {
346  frame->duration = 1;
348  }
349 
350  // no timestamp available - extrapolate from previous frame duration
351  if (frame->pts == AV_NOPTS_VALUE)
352  frame->pts = d->last_frame_pts == AV_NOPTS_VALUE ? 0 :
353  d->last_frame_pts + d->last_frame_duration_est;
354 
355  // update timestamp history
356  d->last_frame_duration_est = video_duration_estimate(ist, frame);
357  d->last_frame_pts = frame->pts;
358  d->last_frame_tb = frame->time_base;
359 
360  if (debug_ts) {
361  av_log(ist, AV_LOG_INFO,
362  "decoder -> pts:%s pts_time:%s "
363  "pkt_dts:%s pkt_dts_time:%s "
364  "duration:%s duration_time:%s "
365  "keyframe:%d frame_type:%d time_base:%d/%d\n",
366  av_ts2str(frame->pts),
374  }
375 
376  if (ist->st->sample_aspect_ratio.num)
378 
379  return 0;
380 }
381 
382 static void sub2video_flush(InputStream *ist)
383 {
384  for (int i = 0; i < ist->nb_filters; i++) {
385  int ret = ifilter_sub2video(ist->filters[i], NULL);
386  if (ret != AVERROR_EOF && ret < 0)
387  av_log(NULL, AV_LOG_WARNING, "Flush the frame error.\n");
388  }
389 }
390 
392 {
393  Decoder *d = ist->decoder;
394  const AVSubtitle *subtitle = (AVSubtitle*)frame->buf[0]->data;
395  int ret = 0;
396 
397  if (ist->fix_sub_duration) {
398  AVSubtitle *sub_prev = d->sub_prev[0]->buf[0] ?
399  (AVSubtitle*)d->sub_prev[0]->buf[0]->data : NULL;
400  int end = 1;
401  if (sub_prev) {
402  end = av_rescale(subtitle->pts - sub_prev->pts,
403  1000, AV_TIME_BASE);
404  if (end < sub_prev->end_display_time) {
406  "Subtitle duration reduced from %"PRId32" to %d%s\n",
407  sub_prev->end_display_time, end,
408  end <= 0 ? ", dropping it" : "");
409  sub_prev->end_display_time = end;
410  }
411  }
412 
413  av_frame_unref(d->sub_prev[1]);
414  av_frame_move_ref(d->sub_prev[1], frame);
415 
416  frame = d->sub_prev[0];
417  subtitle = frame->buf[0] ? (AVSubtitle*)frame->buf[0]->data : NULL;
418 
419  FFSWAP(AVFrame*, d->sub_prev[0], d->sub_prev[1]);
420 
421  if (end <= 0)
422  return 0;
423  }
424 
425  if (!subtitle)
426  return 0;
427 
428  for (int i = 0; i < ist->nb_filters; i++) {
429  ret = ifilter_sub2video(ist->filters[i], frame);
430  if (ret < 0) {
431  av_log(ist, AV_LOG_ERROR, "Error sending a subtitle for filtering: %s\n",
432  av_err2str(ret));
433  return ret;
434  }
435  }
436 
437  subtitle = (AVSubtitle*)frame->buf[0]->data;
438  if (!subtitle->num_rects)
439  return 0;
440 
441  for (int oidx = 0; oidx < ist->nb_outputs; oidx++) {
442  OutputStream *ost = ist->outputs[oidx];
443  if (!ost->enc || ost->type != AVMEDIA_TYPE_SUBTITLE)
444  continue;
445 
446  ret = enc_subtitle(output_files[ost->file_index], ost, subtitle);
447  if (ret < 0)
448  return ret;
449  }
450 
451  return 0;
452 }
453 
454 int fix_sub_duration_heartbeat(InputStream *ist, int64_t signal_pts)
455 {
456  Decoder *d = ist->decoder;
457  int ret = AVERROR_BUG;
458  AVSubtitle *prev_subtitle = d->sub_prev[0]->buf[0] ?
459  (AVSubtitle*)d->sub_prev[0]->buf[0]->data : NULL;
460  AVSubtitle *subtitle;
461 
462  if (!ist->fix_sub_duration || !prev_subtitle ||
463  !prev_subtitle->num_rects || signal_pts <= prev_subtitle->pts)
464  return 0;
465 
466  av_frame_unref(d->sub_heartbeat);
467  ret = subtitle_wrap_frame(d->sub_heartbeat, prev_subtitle, 1);
468  if (ret < 0)
469  return ret;
470 
471  subtitle = (AVSubtitle*)d->sub_heartbeat->buf[0]->data;
472  subtitle->pts = signal_pts;
473 
474  return process_subtitle(ist, d->sub_heartbeat);
475 }
476 
477 static int transcode_subtitles(InputStream *ist, const AVPacket *pkt,
478  AVFrame *frame)
479 {
480  Decoder *d = ist->decoder;
481  AVPacket *flush_pkt = NULL;
482  AVSubtitle subtitle;
483  int got_output;
484  int ret;
485 
486  if (!pkt) {
487  flush_pkt = av_packet_alloc();
488  if (!flush_pkt)
489  return AVERROR(ENOMEM);
490  }
491 
492  ret = avcodec_decode_subtitle2(ist->dec_ctx, &subtitle, &got_output,
493  pkt ? pkt : flush_pkt);
494  av_packet_free(&flush_pkt);
495 
496  if (ret < 0) {
497  av_log(ist, AV_LOG_ERROR, "Error decoding subtitles: %s\n",
498  av_err2str(ret));
499  ist->decode_errors++;
500  return exit_on_error ? ret : 0;
501  }
502 
503  if (!got_output)
504  return pkt ? 0 : AVERROR_EOF;
505 
506  ist->frames_decoded++;
507 
508  // XXX the queue for transferring data back to the main thread runs
509  // on AVFrames, so we wrap AVSubtitle in an AVBufferRef and put that
510  // inside the frame
511  // eventually, subtitles should be switched to use AVFrames natively
512  ret = subtitle_wrap_frame(frame, &subtitle, 0);
513  if (ret < 0) {
514  avsubtitle_free(&subtitle);
515  return ret;
516  }
517 
518  frame->width = ist->dec_ctx->width;
519  frame->height = ist->dec_ctx->height;
520 
521  ret = tq_send(d->queue_out, 0, frame);
522  if (ret < 0)
524 
525  return ret;
526 }
527 
528 static int send_filter_eof(InputStream *ist)
529 {
530  Decoder *d = ist->decoder;
531  int i, ret;
532 
533  for (i = 0; i < ist->nb_filters; i++) {
534  int64_t end_pts = d->last_frame_pts == AV_NOPTS_VALUE ? AV_NOPTS_VALUE :
535  d->last_frame_pts + d->last_frame_duration_est;
536  ret = ifilter_send_eof(ist->filters[i], end_pts, d->last_frame_tb);
537  if (ret < 0)
538  return ret;
539  }
540  return 0;
541 }
542 
544 {
545  const InputFile *ifile = input_files[ist->file_index];
546  Decoder *d = ist->decoder;
547  AVCodecContext *dec = ist->dec_ctx;
548  const char *type_desc = av_get_media_type_string(dec->codec_type);
549  int ret;
550 
551  if (dec->codec_type == AVMEDIA_TYPE_SUBTITLE)
552  return transcode_subtitles(ist, pkt, frame);
553 
554  // With fate-indeo3-2, we're getting 0-sized packets before EOF for some
555  // reason. This seems like a semi-critical bug. Don't trigger EOF, and
556  // skip the packet.
557  if (pkt && pkt->size == 0)
558  return 0;
559 
560  if (pkt && ifile->format_nots) {
563  }
564 
565  ret = avcodec_send_packet(dec, pkt);
566  if (ret < 0 && !(ret == AVERROR_EOF && !pkt)) {
567  // In particular, we don't expect AVERROR(EAGAIN), because we read all
568  // decoded frames with avcodec_receive_frame() until done.
569  if (ret == AVERROR(EAGAIN)) {
570  av_log(ist, AV_LOG_FATAL, "A decoder returned an unexpected error code. "
571  "This is a bug, please report it.\n");
572  return AVERROR_BUG;
573  }
574  av_log(ist, AV_LOG_ERROR, "Error submitting %s to decoder: %s\n",
575  pkt ? "packet" : "EOF", av_err2str(ret));
576 
577  if (ret != AVERROR_EOF) {
578  ist->decode_errors++;
579  if (!exit_on_error)
580  ret = 0;
581  }
582 
583  return ret;
584  }
585 
586  while (1) {
587  FrameData *fd;
588 
590 
593  update_benchmark("decode_%s %d.%d", type_desc,
594  ist->file_index, ist->index);
595 
596  if (ret == AVERROR(EAGAIN)) {
597  av_assert0(pkt); // should never happen during flushing
598  return 0;
599  } else if (ret == AVERROR_EOF) {
600  return ret;
601  } else if (ret < 0) {
602  av_log(ist, AV_LOG_ERROR, "Decoding error: %s\n", av_err2str(ret));
603  ist->decode_errors++;
604 
605  if (exit_on_error)
606  return ret;
607 
608  continue;
609  }
610 
613  "corrupt decoded frame\n");
614  if (exit_on_error)
615  return AVERROR_INVALIDDATA;
616  }
617 
618 
620  fd = frame_data(frame);
621  if (!fd) {
623  return AVERROR(ENOMEM);
624  }
625  fd->dec.pts = frame->pts;
626  fd->dec.tb = dec->pkt_timebase;
627  fd->dec.frame_num = dec->frame_num - 1;
629 
630  frame->time_base = dec->pkt_timebase;
631 
632  if (dec->codec_type == AVMEDIA_TYPE_AUDIO) {
634  ist->nb_samples = frame->nb_samples;
635 
636  audio_ts_process(ist, ist->decoder, frame);
637  } else {
638  ret = video_frame_process(ist, frame);
639  if (ret < 0) {
640  av_log(NULL, AV_LOG_FATAL, "Error while processing the decoded "
641  "data for stream #%d:%d\n", ist->file_index, ist->index);
642  return ret;
643  }
644  }
645 
646  ist->frames_decoded++;
647 
648  ret = tq_send(d->queue_out, 0, frame);
649  if (ret < 0)
650  return ret;
651  }
652 }
653 
654 static void dec_thread_set_name(const InputStream *ist)
655 {
656  char name[16];
657  snprintf(name, sizeof(name), "dec%d:%d:%s", ist->file_index, ist->index,
658  ist->dec_ctx->codec->name);
660 }
661 
663 {
664  av_packet_free(&dt->pkt);
665  av_frame_free(&dt->frame);
666 
667  memset(dt, 0, sizeof(*dt));
668 }
669 
671 {
672  memset(dt, 0, sizeof(*dt));
673 
674  dt->frame = av_frame_alloc();
675  if (!dt->frame)
676  goto fail;
677 
678  dt->pkt = av_packet_alloc();
679  if (!dt->pkt)
680  goto fail;
681 
682  return 0;
683 
684 fail:
685  dec_thread_uninit(dt);
686  return AVERROR(ENOMEM);
687 }
688 
689 static void *decoder_thread(void *arg)
690 {
691  InputStream *ist = arg;
692  InputFile *ifile = input_files[ist->file_index];
693  Decoder *d = ist->decoder;
694  DecThreadContext dt;
695  int ret = 0, input_status = 0;
696 
697  ret = dec_thread_init(&dt);
698  if (ret < 0)
699  goto finish;
700 
701  dec_thread_set_name(ist);
702 
703  while (!input_status) {
704  int dummy, flush_buffers;
705 
706  input_status = tq_receive(d->queue_in, &dummy, dt.pkt);
707  flush_buffers = input_status >= 0 && !dt.pkt->buf;
708  if (!dt.pkt->buf)
709  av_log(ist, AV_LOG_VERBOSE, "Decoder thread received %s packet\n",
710  flush_buffers ? "flush" : "EOF");
711 
712  ret = packet_decode(ist, dt.pkt->buf ? dt.pkt : NULL, dt.frame);
713 
714  av_packet_unref(dt.pkt);
715  av_frame_unref(dt.frame);
716 
717  if (ret == AVERROR_EOF) {
718  av_log(ist, AV_LOG_VERBOSE, "Decoder returned EOF, %s\n",
719  flush_buffers ? "resetting" : "finishing");
720 
721  if (!flush_buffers)
722  break;
723 
724  /* report last frame duration to the demuxer thread */
725  if (ist->dec->type == AVMEDIA_TYPE_AUDIO) {
726  LastFrameDuration dur;
727 
728  dur.stream_idx = ist->index;
729  dur.duration = av_rescale_q(ist->nb_samples,
730  (AVRational){ 1, ist->dec_ctx->sample_rate},
731  ist->st->time_base);
732 
734  }
735 
737  } else if (ret < 0) {
738  av_log(ist, AV_LOG_ERROR, "Error processing packet in decoder: %s\n",
739  av_err2str(ret));
740  break;
741  }
742 
743  // signal to the consumer thread that the entire packet was processed
744  ret = tq_send(d->queue_out, 0, dt.frame);
745  if (ret < 0) {
746  if (ret != AVERROR_EOF)
747  av_log(ist, AV_LOG_ERROR, "Error communicating with the main thread\n");
748  break;
749  }
750  }
751 
752  // EOF is normal thread termination
753  if (ret == AVERROR_EOF)
754  ret = 0;
755 
756 finish:
757  tq_receive_finish(d->queue_in, 0);
758  tq_send_finish (d->queue_out, 0);
759 
760  // make sure the demuxer does not get stuck waiting for audio durations
761  // that will never arrive
762  if (ifile->audio_duration_queue && ist->dec->type == AVMEDIA_TYPE_AUDIO)
764 
765  dec_thread_uninit(&dt);
766 
767  av_log(ist, AV_LOG_VERBOSE, "Terminating decoder thread\n");
768 
769  return (void*)(intptr_t)ret;
770 }
771 
772 int dec_packet(InputStream *ist, const AVPacket *pkt, int no_eof)
773 {
774  Decoder *d = ist->decoder;
775  int ret = 0, thread_ret;
776 
777  // thread already joined
778  if (!d->queue_in)
779  return AVERROR_EOF;
780 
781  // send the packet/flush request/EOF to the decoder thread
782  if (pkt || no_eof) {
783  av_packet_unref(d->pkt);
784 
785  if (pkt) {
786  ret = av_packet_ref(d->pkt, pkt);
787  if (ret < 0)
788  goto finish;
789  }
790 
791  ret = tq_send(d->queue_in, 0, d->pkt);
792  if (ret < 0)
793  goto finish;
794  } else
795  tq_send_finish(d->queue_in, 0);
796 
797  // retrieve all decoded data for the packet
798  while (1) {
799  int dummy;
800 
801  ret = tq_receive(d->queue_out, &dummy, d->frame);
802  if (ret < 0)
803  goto finish;
804 
805  // packet fully processed
806  if (!d->frame->buf[0])
807  return 0;
808 
809  // process the decoded frame
810  if (ist->dec->type == AVMEDIA_TYPE_SUBTITLE) {
811  ret = process_subtitle(ist, d->frame);
812  } else {
813  ret = send_frame_to_filters(ist, d->frame);
814  }
815  av_frame_unref(d->frame);
816  if (ret < 0)
817  goto finish;
818  }
819 
820 finish:
821  thread_ret = dec_thread_stop(d);
822  if (thread_ret < 0) {
823  av_log(ist, AV_LOG_ERROR, "Decoder thread returned error: %s\n",
824  av_err2str(thread_ret));
825  ret = err_merge(ret, thread_ret);
826  }
827  // non-EOF errors here are all fatal
828  if (ret < 0 && ret != AVERROR_EOF)
829  return ret;
830 
831  // signal EOF to our downstreams
832  if (ist->dec->type == AVMEDIA_TYPE_SUBTITLE)
833  sub2video_flush(ist);
834  else {
835  ret = send_filter_eof(ist);
836  if (ret < 0) {
837  av_log(NULL, AV_LOG_FATAL, "Error marking filters as finished\n");
838  return ret;
839  }
840  }
841 
842  return AVERROR_EOF;
843 }
844 
846 {
847  Decoder *d = ist->decoder;
848  ObjPool *op;
849  int ret = 0;
850 
852  if (!op)
853  return AVERROR(ENOMEM);
854 
855  d->queue_in = tq_alloc(1, 1, op, pkt_move);
856  if (!d->queue_in) {
857  objpool_free(&op);
858  return AVERROR(ENOMEM);
859  }
860 
862  if (!op)
863  goto fail;
864 
865  d->queue_out = tq_alloc(1, 4, op, frame_move);
866  if (!d->queue_out) {
867  objpool_free(&op);
868  goto fail;
869  }
870 
871  ret = pthread_create(&d->thread, NULL, decoder_thread, ist);
872  if (ret) {
873  ret = AVERROR(ret);
874  av_log(ist, AV_LOG_ERROR, "pthread_create() failed: %s\n",
875  av_err2str(ret));
876  goto fail;
877  }
878 
879  return 0;
880 fail:
881  if (ret >= 0)
882  ret = AVERROR(ENOMEM);
883 
884  tq_free(&d->queue_in);
885  tq_free(&d->queue_out);
886  return ret;
887 }
888 
890 {
891  InputStream *ist = s->opaque;
892  Decoder *d = ist->decoder;
893  const enum AVPixelFormat *p;
894 
895  for (p = pix_fmts; *p != AV_PIX_FMT_NONE; p++) {
897  const AVCodecHWConfig *config = NULL;
898  int i;
899 
900  if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
901  break;
902 
903  if (ist->hwaccel_id == HWACCEL_GENERIC ||
904  ist->hwaccel_id == HWACCEL_AUTO) {
905  for (i = 0;; i++) {
906  config = avcodec_get_hw_config(s->codec, i);
907  if (!config)
908  break;
909  if (!(config->methods &
911  continue;
912  if (config->pix_fmt == *p)
913  break;
914  }
915  }
916  if (config && config->device_type == ist->hwaccel_device_type) {
917  d->hwaccel_pix_fmt = *p;
918  break;
919  }
920  }
921 
922  return *p;
923 }
924 
926 {
927  const AVCodecHWConfig *config;
928  HWDevice *dev;
929  int i;
930  for (i = 0;; i++) {
931  config = avcodec_get_hw_config(codec, i);
932  if (!config)
933  return NULL;
935  continue;
936  dev = hw_device_get_by_type(config->device_type);
937  if (dev)
938  return dev;
939  }
940 }
941 
943 {
944  const AVCodecHWConfig *config;
945  enum AVHWDeviceType type;
946  HWDevice *dev = NULL;
947  int err, auto_device = 0;
948 
949  if (ist->hwaccel_device) {
951  if (!dev) {
952  if (ist->hwaccel_id == HWACCEL_AUTO) {
953  auto_device = 1;
954  } else if (ist->hwaccel_id == HWACCEL_GENERIC) {
955  type = ist->hwaccel_device_type;
957  &dev);
958  } else {
959  // This will be dealt with by API-specific initialisation
960  // (using hwaccel_device), so nothing further needed here.
961  return 0;
962  }
963  } else {
964  if (ist->hwaccel_id == HWACCEL_AUTO) {
965  ist->hwaccel_device_type = dev->type;
966  } else if (ist->hwaccel_device_type != dev->type) {
967  av_log(NULL, AV_LOG_ERROR, "Invalid hwaccel device "
968  "specified for decoder: device %s of type %s is not "
969  "usable with hwaccel %s.\n", dev->name,
972  return AVERROR(EINVAL);
973  }
974  }
975  } else {
976  if (ist->hwaccel_id == HWACCEL_AUTO) {
977  auto_device = 1;
978  } else if (ist->hwaccel_id == HWACCEL_GENERIC) {
979  type = ist->hwaccel_device_type;
981 
982  // When "-qsv_device device" is used, an internal QSV device named
983  // as "__qsv_device" is created. Another QSV device is created too
984  // if "-init_hw_device qsv=name:device" is used. There are 2 QSV devices
985  // if both "-qsv_device device" and "-init_hw_device qsv=name:device"
986  // are used, hw_device_get_by_type(AV_HWDEVICE_TYPE_QSV) returns NULL.
987  // To keep back-compatibility with the removed ad-hoc libmfx setup code,
988  // call hw_device_get_by_name("__qsv_device") to select the internal QSV
989  // device.
990  if (!dev && type == AV_HWDEVICE_TYPE_QSV)
991  dev = hw_device_get_by_name("__qsv_device");
992 
993  if (!dev)
994  err = hw_device_init_from_type(type, NULL, &dev);
995  } else {
996  dev = hw_device_match_by_codec(ist->dec);
997  if (!dev) {
998  // No device for this codec, but not using generic hwaccel
999  // and therefore may well not need one - ignore.
1000  return 0;
1001  }
1002  }
1003  }
1004 
1005  if (auto_device) {
1006  int i;
1007  if (!avcodec_get_hw_config(ist->dec, 0)) {
1008  // Decoder does not support any hardware devices.
1009  return 0;
1010  }
1011  for (i = 0; !dev; i++) {
1012  config = avcodec_get_hw_config(ist->dec, i);
1013  if (!config)
1014  break;
1015  type = config->device_type;
1016  dev = hw_device_get_by_type(type);
1017  if (dev) {
1018  av_log(NULL, AV_LOG_INFO, "Using auto "
1019  "hwaccel type %s with existing device %s.\n",
1021  }
1022  }
1023  for (i = 0; !dev; i++) {
1024  config = avcodec_get_hw_config(ist->dec, i);
1025  if (!config)
1026  break;
1027  type = config->device_type;
1028  // Try to make a new device of this type.
1030  &dev);
1031  if (err < 0) {
1032  // Can't make a device of this type.
1033  continue;
1034  }
1035  if (ist->hwaccel_device) {
1036  av_log(NULL, AV_LOG_INFO, "Using auto "
1037  "hwaccel type %s with new device created "
1038  "from %s.\n", av_hwdevice_get_type_name(type),
1039  ist->hwaccel_device);
1040  } else {
1041  av_log(NULL, AV_LOG_INFO, "Using auto "
1042  "hwaccel type %s with new default device.\n",
1044  }
1045  }
1046  if (dev) {
1047  ist->hwaccel_device_type = type;
1048  } else {
1049  av_log(NULL, AV_LOG_INFO, "Auto hwaccel "
1050  "disabled: no device found.\n");
1051  ist->hwaccel_id = HWACCEL_NONE;
1052  return 0;
1053  }
1054  }
1055 
1056  if (!dev) {
1057  av_log(NULL, AV_LOG_ERROR, "No device available "
1058  "for decoder: device type %s needed for codec %s.\n",
1060  return err;
1061  }
1062 
1064  if (!ist->dec_ctx->hw_device_ctx)
1065  return AVERROR(ENOMEM);
1066 
1067  return 0;
1068 }
1069 
1071 {
1072  Decoder *d;
1073  const AVCodec *codec = ist->dec;
1074  int ret;
1075 
1076  if (!codec) {
1077  av_log(ist, AV_LOG_ERROR,
1078  "Decoding requested, but no decoder found for: %s\n",
1080  return AVERROR(EINVAL);
1081  }
1082 
1083  ret = dec_alloc(&ist->decoder);
1084  if (ret < 0)
1085  return ret;
1086  d = ist->decoder;
1087 
1088  if (codec->type == AVMEDIA_TYPE_SUBTITLE && ist->fix_sub_duration) {
1089  for (int i = 0; i < FF_ARRAY_ELEMS(d->sub_prev); i++) {
1090  d->sub_prev[i] = av_frame_alloc();
1091  if (!d->sub_prev[i])
1092  return AVERROR(ENOMEM);
1093  }
1094  d->sub_heartbeat = av_frame_alloc();
1095  if (!d->sub_heartbeat)
1096  return AVERROR(ENOMEM);
1097  }
1098 
1099  ist->dec_ctx->opaque = ist;
1100  ist->dec_ctx->get_format = get_format;
1101 
1102  if (ist->dec_ctx->codec_id == AV_CODEC_ID_DVB_SUBTITLE &&
1103  (ist->decoding_needed & DECODING_FOR_OST)) {
1104  av_dict_set(&ist->decoder_opts, "compute_edt", "1", AV_DICT_DONT_OVERWRITE);
1106  av_log(NULL, AV_LOG_WARNING, "Warning using DVB subtitles for filtering and output at the same time is not fully supported, also see -compute_edt [0|1]\n");
1107  }
1108 
1109  /* Useful for subtitles retiming by lavf (FIXME), skipping samples in
1110  * audio, and video decoders such as cuvid or mediacodec */
1111  ist->dec_ctx->pkt_timebase = ist->st->time_base;
1112 
1113  if (!av_dict_get(ist->decoder_opts, "threads", NULL, 0))
1114  av_dict_set(&ist->decoder_opts, "threads", "auto", 0);
1115  /* Attached pics are sparse, therefore we would not want to delay their decoding till EOF. */
1117  av_dict_set(&ist->decoder_opts, "threads", "1", 0);
1118 
1120  if (ret < 0) {
1121  av_log(ist, AV_LOG_ERROR,
1122  "Hardware device setup failed for decoder: %s\n",
1123  av_err2str(ret));
1124  return ret;
1125  }
1126 
1127  if ((ret = avcodec_open2(ist->dec_ctx, codec, &ist->decoder_opts)) < 0) {
1128  av_log(ist, AV_LOG_ERROR, "Error while opening decoder: %s\n",
1129  av_err2str(ret));
1130  return ret;
1131  }
1132 
1134  if (ret < 0)
1135  return ret;
1136 
1137  ret = dec_thread_start(ist);
1138  if (ret < 0) {
1139  av_log(ist, AV_LOG_ERROR, "Error starting decoder thread: %s\n",
1140  av_err2str(ret));
1141  return ret;
1142  }
1143 
1144  return 0;
1145 }
AVSubtitle
Definition: avcodec.h:2269
process_subtitle
static int process_subtitle(InputStream *ist, AVFrame *frame)
Definition: ffmpeg_dec.c:391
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:423
AVCodec
AVCodec.
Definition: codec.h:187
InputFile::format_nots
int format_nots
Definition: ffmpeg.h:405
pthread_join
static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
Definition: os2threads.h:94
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
send_filter_eof
static int send_filter_eof(InputStream *ist)
Definition: ffmpeg_dec.c:528
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
InputStream::hwaccel_device
char * hwaccel_device
Definition: ffmpeg.h:384
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:712
audio_samplerate_update
static AVRational audio_samplerate_update(void *logctx, Decoder *d, const AVFrame *frame)
Definition: ffmpeg_dec.c:165
FrameData
Definition: ffmpeg.h:636
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:2964
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
AV_DISPOSITION_ATTACHED_PIC
#define AV_DISPOSITION_ATTACHED_PIC
The stream is stored in the file as an attached picture/"cover art" (e.g.
Definition: avformat.h:772
dec_thread_uninit
static void dec_thread_uninit(DecThreadContext *dt)
Definition: ffmpeg_dec.c:662
InputStream::outputs
struct OutputStream ** outputs
Definition: ffmpeg.h:376
audio_ts_process
static void audio_ts_process(void *logctx, Decoder *d, AVFrame *frame)
Definition: ffmpeg_dec.c:208
InputStream::dec_ctx
AVCodecContext * dec_ctx
Definition: ffmpeg.h:344
AVSubtitle::num_rects
unsigned num_rects
Definition: avcodec.h:2273
hw_device_match_by_codec
static HWDevice * hw_device_match_by_codec(const AVCodec *codec)
Definition: ffmpeg_dec.c:925
DecThreadContext::pkt
AVPacket * pkt
Definition: ffmpeg_dec.c:76
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
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
pixdesc.h
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
LastFrameDuration::stream_idx
int stream_idx
Definition: ffmpeg.h:395
dec_thread_init
static int dec_thread_init(DecThreadContext *dt)
Definition: ffmpeg_dec.c:670
AVStream::avg_frame_rate
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:930
InputStream::nb_filters
int nb_filters
Definition: ffmpeg.h:369
objpool_free
void objpool_free(ObjPool **pop)
Definition: objpool.c:54
ffmpeg.h
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
video_duration_estimate
static int64_t video_duration_estimate(const InputStream *ist, const AVFrame *frame)
Definition: ffmpeg_dec.c:245
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:83
av_buffer_ref
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:103
objpool_alloc_packets
ObjPool * objpool_alloc_packets(void)
Definition: objpool.c:124
InputStream::decoding_needed
int decoding_needed
Definition: ffmpeg.h:333
LastFrameDuration
Definition: ffmpeg.h:394
AVFrame::buf
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:590
ost
static AVStream * ost
Definition: vaapi_transcode.c:42
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:97
InputStream::nb_outputs
int nb_outputs
Definition: ffmpeg.h:377
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
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:639
AVFrame::opaque_ref
AVBufferRef * opaque_ref
Frame owner's private data.
Definition: frame.h:768
InputStream
Definition: ffmpeg.h:324
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:1803
debug_ts
int debug_ts
Definition: ffmpeg_opt.c:81
hw_device_setup_for_decode
static int hw_device_setup_for_decode(InputStream *ist)
Definition: ffmpeg_dec.c:942
finish
static void finish(void)
Definition: movenc.c:342
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:450
fail
#define fail()
Definition: checkasm.h:138
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:889
dummy
int dummy
Definition: motion.c:66
InputStream::decoder_opts
AVDictionary * decoder_opts
Definition: ffmpeg.h:352
update_benchmark
void update_benchmark(const char *fmt,...)
Definition: ffmpeg.c:466
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
pts
static int64_t pts
Definition: transcode_aac.c:643
AV_ROUND_UP
@ AV_ROUND_UP
Round toward +infinity.
Definition: mathematics.h:134
FrameData::tb
AVRational tb
Definition: ffmpeg.h:642
codec.h
AVRational::num
int num
Numerator.
Definition: rational.h:59
InputFile
Definition: ffmpeg.h:399
avsubtitle_free
void avsubtitle_free(AVSubtitle *sub)
Free all allocated data in the given subtitle struct.
Definition: avcodec.c:413
dec_alloc
static int dec_alloc(Decoder **pdec)
Definition: ffmpeg_dec.c:116
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:88
AV_CODEC_ID_DVB_SUBTITLE
@ AV_CODEC_ID_DVB_SUBTITLE
Definition: codec_id.h:551
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:74
enc_subtitle
int enc_subtitle(OutputFile *of, OutputStream *ost, const AVSubtitle *sub)
Definition: ffmpeg_enc.c:395
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
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_thread_message_queue_send
int av_thread_message_queue_send(AVThreadMessageQueue *mq, void *msg, unsigned flags)
Send a message on the queue.
Definition: threadmessage.c:161
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:75
dec_packet
int dec_packet(InputStream *ist, const AVPacket *pkt, int no_eof)
Submit a packet for decoding.
Definition: ffmpeg_dec.c:772
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:80
AVCodecContext::has_b_frames
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:744
Decoder::last_frame_sample_rate
int last_frame_sample_rate
Definition: ffmpeg_dec.c:48
s
#define s(width, name)
Definition: cbs_vp9.c:198
InputStream::framerate
AVRational framerate
Definition: ffmpeg.h:353
Decoder::queue_in
ThreadQueue * queue_in
Queue for sending coded packets from the main thread to the decoder thread.
Definition: ffmpeg_dec.c:62
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
op
static int op(uint8_t **dst, const uint8_t *dst_end, GetByteContext *gb, int pixel, int count, int *x, int width, int linesize)
Perform decode operation.
Definition: anm.c:76
subtitle_wrap_frame
int subtitle_wrap_frame(AVFrame *frame, AVSubtitle *subtitle, int copy)
Definition: ffmpeg.c:738
hwaccel_retrieve_data
int hwaccel_retrieve_data(AVCodecContext *avctx, AVFrame *input)
Definition: ffmpeg_hw.c:300
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:713
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:1517
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
InputStream::filters
InputFilter ** filters
Definition: ffmpeg.h:368
dec_thread_set_name
static void dec_thread_set_name(const InputStream *ist)
Definition: ffmpeg_dec.c:654
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
AVSubtitle::pts
int64_t pts
Same as packet pts, in AV_TIME_BASE.
Definition: avcodec.h:2275
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:93
objpool_alloc_frames
ObjPool * objpool_alloc_frames(void)
Definition: objpool.c:128
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:79
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
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:451
arg
const char * arg
Definition: jacosubdec.c:67
pthread_create
static av_always_inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
Definition: os2threads.h:80
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
AVPacket::buf
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: packet.h:474
tq_free
void tq_free(ThreadQueue **ptq)
Definition: thread_queue.c:55
AVStream::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:880
NULL
#define NULL
Definition: coverity.c:32
InputStream::top_field_first
int top_field_first
Definition: ffmpeg.h:355
InputStream::st
AVStream * st
Definition: ffmpeg.h:330
tq_receive_finish
void tq_receive_finish(ThreadQueue *tq, unsigned int stream_idx)
Mark the given stream finished from the receiving side.
Definition: thread_queue.c:232
AVCodec::type
enum AVMediaType type
Definition: codec.h:200
Decoder
Definition: ffmpeg_dec.c:35
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
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
InputStream::hwaccel_id
enum HWAccelID hwaccel_id
Definition: ffmpeg.h:382
InputStream::fix_sub_duration
int fix_sub_duration
Definition: ffmpeg.h:360
AV_DICT_DONT_OVERWRITE
#define AV_DICT_DONT_OVERWRITE
Don't overwrite existing entries.
Definition: dict.h:81
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
av_packet_ref
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet.
Definition: avpacket.c:431
InputStream::par
AVCodecParameters * par
Codec parameters - to be used by the decoding/streamcopy code.
Definition: ffmpeg.h:342
input_files
InputFile ** input_files
Definition: ffmpeg.c:123
error.h
InputStream::frames_decoded
uint64_t frames_decoded
Definition: ffmpeg.h:389
frame_move
static void frame_move(void *dst, void *src)
Definition: ffmpeg.h:950
AVFrame::best_effort_timestamp
int64_t best_effort_timestamp
frame timestamp estimated using various heuristics, in stream time base
Definition: frame.h:676
tq_send
int tq_send(ThreadQueue *tq, unsigned int stream_idx, void *data)
Send an item for the given stream to the queue.
Definition: thread_queue.c:120
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
InputStream::hwaccel_device_type
enum AVHWDeviceType hwaccel_device_type
Definition: ffmpeg.h:383
Decoder::last_frame_pts
int64_t last_frame_pts
Definition: ffmpeg_dec.c:44
AVPacket::size
int size
Definition: packet.h:492
InputStream::file_index
int file_index
Definition: ffmpeg.h:327
output_files
OutputFile ** output_files
Definition: ffmpeg.c:126
FrameData::dec
struct FrameData::@3 dec
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
ifilter_send_frame
int ifilter_send_frame(InputFilter *ifilter, AVFrame *frame, int keep_reference)
Definition: ffmpeg_filter.c:2355
AVCodecContext::pkt_timebase
AVRational pkt_timebase
Timebase in which pkt_dts/pts and AVPacket.dts/pts are expressed.
Definition: avcodec.h:1817
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
check_avoptions
int check_avoptions(AVDictionary *m)
Definition: ffmpeg.c:455
dec_open
int dec_open(InputStream *ist)
Definition: ffmpeg_dec.c:1070
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:86
hw_device_get_by_type
HWDevice * hw_device_get_by_type(enum AVHWDeviceType type)
Definition: ffmpeg_hw.c:30
DECODING_FOR_OST
#define DECODING_FOR_OST
Definition: ffmpeg.h:334
decoder_thread
static void * decoder_thread(void *arg)
Definition: ffmpeg_dec.c:689
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:429
AVStream::sample_aspect_ratio
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:919
AVSubtitle::end_display_time
uint32_t end_display_time
Definition: avcodec.h:2272
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:490
ObjPool
Definition: objpool.c:30
InputStream::samples_decoded
uint64_t samples_decoded
Definition: ffmpeg.h:390
FrameData::pts
int64_t pts
Definition: ffmpeg.h:641
fix_sub_duration_heartbeat
int fix_sub_duration_heartbeat(InputStream *ist, int64_t signal_pts)
Definition: ffmpeg_dec.c:454
pkt_move
static void pkt_move(void *dst, void *src)
Definition: ffmpeg.h:945
av_packet_alloc
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:63
tq_alloc
ThreadQueue * tq_alloc(unsigned int nb_streams, size_t queue_size, ObjPool *obj_pool, void(*obj_move)(void *dst, void *src))
Allocate a queue for sending data between threads.
Definition: thread_queue.c:79
dec_thread_stop
static int dec_thread_stop(Decoder *d)
Definition: ffmpeg_dec.c:79
pthread_t
Definition: os2threads.h:44
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
sub2video_flush
static void sub2video_flush(InputStream *ist)
Definition: ffmpeg_dec.c:382
Decoder::last_frame_tb
AVRational last_frame_tb
Definition: ffmpeg_dec.c:46
Decoder::frame
AVFrame * frame
Definition: ffmpeg_dec.c:36
avcodec_get_name
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition: utils.c:417
HWACCEL_AUTO
@ HWACCEL_AUTO
Definition: ffmpeg.h:79
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
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:420
DECODING_FOR_FILTER
#define DECODING_FOR_FILTER
Definition: ffmpeg.h:335
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:484
FrameData::bits_per_raw_sample
int bits_per_raw_sample
Definition: ffmpeg.h:647
AV_FRAME_FLAG_CORRUPT
#define AV_FRAME_FLAG_CORRUPT
The frame data may be corrupted, e.g.
Definition: frame.h:624
AV_TIME_BASE
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
exit_on_error
int exit_on_error
Definition: ffmpeg_opt.c:82
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:56
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
Decoder::last_frame_duration_est
int64_t last_frame_duration_est
Definition: ffmpeg_dec.c:45
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:1981
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:621
tq_receive
int tq_receive(ThreadQueue *tq, int *stream_idx, void *data)
Read the next item from the queue.
Definition: thread_queue.c:191
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:658
transcode_subtitles
static int transcode_subtitles(InputStream *ist, const AVPacket *pkt, AVFrame *frame)
Definition: ffmpeg_dec.c:477
avcodec.h
InputStream::decoder
Decoder * decoder
Definition: ffmpeg.h:343
AVCodecContext::frame_num
int64_t frame_num
Frame counter, set by libavcodec.
Definition: avcodec.h:2118
AVStream::disposition
int disposition
Stream disposition - a combination of AV_DISPOSITION_* flags.
Definition: avformat.h:910
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
pixfmt.h
FFSWAP
#define FFSWAP(type, a, b)
Definition: macros.h:52
avcodec_flush_buffers
void avcodec_flush_buffers(AVCodecContext *avctx)
Reset the internal codec state / flush internal buffers.
Definition: avcodec.c:384
ThreadQueue
Definition: thread_queue.c:42
AVCodecContext::opaque
void * opaque
Private data of the user, can be used to carry app specific stuff.
Definition: avcodec.h:483
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_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:441
AVFrame::height
int height
Definition: frame.h:412
packet_decode
static int packet_decode(InputStream *ist, AVPacket *pkt, AVFrame *frame)
Definition: ffmpeg_dec.c:543
HWDevice::name
const char * name
Definition: ffmpeg.h:84
InputFile::audio_duration_queue
AVThreadMessageQueue * audio_duration_queue
Definition: ffmpeg.h:431
AVRational::den
int den
Denominator.
Definition: rational.h:60
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
InputStream::nb_samples
int64_t nb_samples
Definition: ffmpeg.h:350
video_frame_process
static int video_frame_process(InputStream *ist, AVFrame *frame)
Definition: ffmpeg_dec.c:297
thread_queue.h
av_mul_q
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
ifilter_send_eof
int ifilter_send_eof(InputFilter *ifilter, int64_t pts, AVRational tb)
Definition: ffmpeg_filter.c:2307
HWACCEL_NONE
@ HWACCEL_NONE
Definition: ffmpeg.h:78
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
InputStream::decode_errors
uint64_t decode_errors
Definition: ffmpeg.h:391
AVCodecContext::codec_type
enum AVMediaType codec_type
Definition: avcodec.h:449
desc
const char * desc
Definition: libsvtav1.c:83
AVCodecParameters::video_delay
int video_delay
Video only.
Definition: codec_par.h:150
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
InputStream::index
int index
Definition: ffmpeg.h:328
Decoder::last_filter_in_rescale_delta
int64_t last_filter_in_rescale_delta
Definition: ffmpeg_dec.c:47
Decoder::thread
pthread_t thread
Definition: ffmpeg_dec.c:54
LastFrameDuration::duration
int64_t duration
Definition: ffmpeg.h:396
AVPacket
This structure stores compressed data.
Definition: packet.h:468
ifilter_sub2video
int ifilter_sub2video(InputFilter *ifilter, const AVFrame *frame)
Definition: ffmpeg_filter.c:2274
Decoder::queue_out
ThreadQueue * queue_out
Queue for sending decoded frames from the decoder thread to the main thread.
Definition: ffmpeg_dec.c:70
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
Decoder::sub_heartbeat
AVFrame * sub_heartbeat
Definition: ffmpeg_dec.c:52
HWDevice::type
enum AVHWDeviceType type
Definition: ffmpeg.h:85
Decoder::sub_prev
AVFrame * sub_prev[2]
Definition: ffmpeg_dec.c:51
d
d
Definition: ffmpeg_filter.c:368
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:621
timestamp.h
OutputStream
Definition: mux.c:53
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:52
av_thread_message_queue_set_err_recv
void av_thread_message_queue_set_err_recv(AVThreadMessageQueue *mq, int err)
Set the receiving error code.
Definition: threadmessage.c:204
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
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
err_merge
static int err_merge(int err0, int err1)
Merge two return codes - return one of the error codes if at least one of them was negative,...
Definition: ffmpeg.h:889
dec_thread_start
static int dec_thread_start(InputStream *ist)
Definition: ffmpeg_dec.c:845
hw_device_get_by_name
HWDevice * hw_device_get_by_name(const char *name)
Definition: ffmpeg_hw.c:44
InputStream::dec
const AVCodec * dec
Definition: ffmpeg.h:345
Decoder::hwaccel_pix_fmt
enum AVPixelFormat hwaccel_pix_fmt
Definition: ffmpeg_dec.c:39
snprintf
#define snprintf
Definition: snprintf.h:34
Decoder::pkt
AVPacket * pkt
Definition: ffmpeg_dec.c:37
buffersrc.h
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
send_frame_to_filters
static int send_frame_to_filters(InputStream *ist, AVFrame *decoded_frame)
Definition: ffmpeg_dec.c:147
ff_thread_setname
static int ff_thread_setname(const char *name)
Definition: thread.h:214
tq_send_finish
void tq_send_finish(ThreadQueue *tq, unsigned int stream_idx)
Mark the given stream finished from the sending side.
Definition: thread_queue.c:217