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 "ffmpeg_utils.h"
34 #include "thread_queue.h"
35 
36 struct Decoder {
39 
41 
42  // pts/estimated duration of the last decoded frame
43  // * in decoder timebase for video,
44  // * in last_frame_tb (may change during decoding) for audio
45  int64_t last_frame_pts;
50 
51  /* previous decoded subtitles */
54 
56  /**
57  * Queue for sending coded packets from the main thread to
58  * the decoder thread.
59  *
60  * An empty packet is sent to flush the decoder without terminating
61  * decoding.
62  */
64  /**
65  * Queue for sending decoded frames from the decoder thread
66  * to the main thread.
67  *
68  * An empty frame is sent to signal that a single packet has been fully
69  * processed.
70  */
72 };
73 
74 // data that is local to the decoder thread and not visible outside of it
75 typedef struct DecThreadContext {
79 
81 {
82  void *ret;
83 
84  if (!d->queue_in)
85  return 0;
86 
87  tq_send_finish(d->queue_in, 0);
88  tq_receive_finish(d->queue_out, 0);
89 
90  pthread_join(d->thread, &ret);
91 
92  tq_free(&d->queue_in);
93  tq_free(&d->queue_out);
94 
95  return (intptr_t)ret;
96 }
97 
98 void dec_free(Decoder **pdec)
99 {
100  Decoder *dec = *pdec;
101 
102  if (!dec)
103  return;
104 
105  dec_thread_stop(dec);
106 
107  av_frame_free(&dec->frame);
108  av_packet_free(&dec->pkt);
109 
110  for (int i = 0; i < FF_ARRAY_ELEMS(dec->sub_prev); i++)
111  av_frame_free(&dec->sub_prev[i]);
113 
114  av_freep(pdec);
115 }
116 
117 static int dec_alloc(Decoder **pdec)
118 {
119  Decoder *dec;
120 
121  *pdec = NULL;
122 
123  dec = av_mallocz(sizeof(*dec));
124  if (!dec)
125  return AVERROR(ENOMEM);
126 
127  dec->frame = av_frame_alloc();
128  if (!dec->frame)
129  goto fail;
130 
131  dec->pkt = av_packet_alloc();
132  if (!dec->pkt)
133  goto fail;
134 
137  dec->last_frame_tb = (AVRational){ 1, 1 };
139 
140  *pdec = dec;
141 
142  return 0;
143 fail:
144  dec_free(&dec);
145  return AVERROR(ENOMEM);
146 }
147 
148 static int send_frame_to_filters(InputStream *ist, AVFrame *decoded_frame)
149 {
150  int i, ret;
151 
152  av_assert1(ist->nb_filters > 0); /* ensure ret is initialized */
153  for (i = 0; i < ist->nb_filters; i++) {
154  ret = ifilter_send_frame(ist->filters[i], decoded_frame, i < ist->nb_filters - 1);
155  if (ret == AVERROR_EOF)
156  ret = 0; /* ignore */
157  if (ret < 0) {
159  "Failed to inject frame into filter network: %s\n", av_err2str(ret));
160  break;
161  }
162  }
163  return ret;
164 }
165 
167  const AVFrame *frame)
168 {
169  const int prev = d->last_frame_tb.den;
170  const int sr = frame->sample_rate;
171 
172  AVRational tb_new;
173  int64_t gcd;
174 
175  if (frame->sample_rate == d->last_frame_sample_rate)
176  goto finish;
177 
178  gcd = av_gcd(prev, sr);
179 
180  if (prev / gcd >= INT_MAX / sr) {
181  av_log(logctx, AV_LOG_WARNING,
182  "Audio timestamps cannot be represented exactly after "
183  "sample rate change: %d -> %d\n", prev, sr);
184 
185  // LCM of 192000, 44100, allows to represent all common samplerates
186  tb_new = (AVRational){ 1, 28224000 };
187  } else
188  tb_new = (AVRational){ 1, prev / gcd * sr };
189 
190  // keep the frame timebase if it is strictly better than
191  // the samplerate-defined one
192  if (frame->time_base.num == 1 && frame->time_base.den > tb_new.den &&
193  !(frame->time_base.den % tb_new.den))
194  tb_new = frame->time_base;
195 
196  if (d->last_frame_pts != AV_NOPTS_VALUE)
197  d->last_frame_pts = av_rescale_q(d->last_frame_pts,
198  d->last_frame_tb, tb_new);
199  d->last_frame_duration_est = av_rescale_q(d->last_frame_duration_est,
200  d->last_frame_tb, tb_new);
201 
202  d->last_frame_tb = tb_new;
203  d->last_frame_sample_rate = frame->sample_rate;
204 
205 finish:
206  return d->last_frame_tb;
207 }
208 
209 static void audio_ts_process(void *logctx, Decoder *d, AVFrame *frame)
210 {
211  AVRational tb_filter = (AVRational){1, frame->sample_rate};
212  AVRational tb;
213  int64_t pts_pred;
214 
215  // on samplerate change, choose a new internal timebase for timestamp
216  // generation that can represent timestamps from all the samplerates
217  // seen so far
218  tb = audio_samplerate_update(logctx, d, frame);
219  pts_pred = d->last_frame_pts == AV_NOPTS_VALUE ? 0 :
220  d->last_frame_pts + d->last_frame_duration_est;
221 
222  if (frame->pts == AV_NOPTS_VALUE) {
223  frame->pts = pts_pred;
224  frame->time_base = tb;
225  } else if (d->last_frame_pts != AV_NOPTS_VALUE &&
226  frame->pts > av_rescale_q_rnd(pts_pred, tb, frame->time_base,
227  AV_ROUND_UP)) {
228  // there was a gap in timestamps, reset conversion state
229  d->last_filter_in_rescale_delta = AV_NOPTS_VALUE;
230  }
231 
233  tb, frame->nb_samples,
234  &d->last_filter_in_rescale_delta, tb);
235 
236  d->last_frame_pts = frame->pts;
237  d->last_frame_duration_est = av_rescale_q(frame->nb_samples,
238  tb_filter, tb);
239 
240  // finally convert to filtering timebase
241  frame->pts = av_rescale_q(frame->pts, tb, tb_filter);
243  frame->time_base = tb_filter;
244 }
245 
246 static int64_t video_duration_estimate(const InputStream *ist, const AVFrame *frame)
247 {
248  const Decoder *d = ist->decoder;
249  const InputFile *ifile = input_files[ist->file_index];
250  int64_t codec_duration = 0;
251 
252  // XXX lavf currently makes up frame durations when they are not provided by
253  // the container. As there is no way to reliably distinguish real container
254  // durations from the fake made-up ones, we use heuristics based on whether
255  // the container has timestamps. Eventually lavf should stop making up
256  // durations, then this should be simplified.
257 
258  // prefer frame duration for containers with timestamps
259  if (frame->duration > 0 && (!ifile->format_nots || ist->framerate.num))
260  return frame->duration;
261 
262  if (ist->dec_ctx->framerate.den && ist->dec_ctx->framerate.num) {
263  int fields = frame->repeat_pict + 2;
264  AVRational field_rate = av_mul_q(ist->dec_ctx->framerate,
265  (AVRational){ 2, 1 });
266  codec_duration = av_rescale_q(fields, av_inv_q(field_rate),
267  frame->time_base);
268  }
269 
270  // prefer codec-layer duration for containers without timestamps
271  if (codec_duration > 0 && ifile->format_nots)
272  return codec_duration;
273 
274  // when timestamps are available, repeat last frame's actual duration
275  // (i.e. pts difference between this and last frame)
276  if (frame->pts != AV_NOPTS_VALUE && d->last_frame_pts != AV_NOPTS_VALUE &&
277  frame->pts > d->last_frame_pts)
278  return frame->pts - d->last_frame_pts;
279 
280  // try frame/codec duration
281  if (frame->duration > 0)
282  return frame->duration;
283  if (codec_duration > 0)
284  return codec_duration;
285 
286  // try average framerate
287  if (ist->st->avg_frame_rate.num && ist->st->avg_frame_rate.den) {
288  int64_t d = av_rescale_q(1, av_inv_q(ist->st->avg_frame_rate),
289  frame->time_base);
290  if (d > 0)
291  return d;
292  }
293 
294  // last resort is last frame's estimated duration, and 1
295  return FFMAX(d->last_frame_duration_est, 1);
296 }
297 
299 {
300  Decoder *d = ist->decoder;
301 
302  // The following line may be required in some cases where there is no parser
303  // or the parser does not has_b_frames correctly
304  if (ist->par->video_delay < ist->dec_ctx->has_b_frames) {
305  if (ist->dec_ctx->codec_id == AV_CODEC_ID_H264) {
306  ist->par->video_delay = ist->dec_ctx->has_b_frames;
307  } else
309  "video_delay is larger in decoder than demuxer %d > %d.\n"
310  "If you want to help, upload a sample "
311  "of this file to https://streams.videolan.org/upload/ "
312  "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)\n",
313  ist->dec_ctx->has_b_frames,
314  ist->par->video_delay);
315  }
316 
317  if (ist->dec_ctx->width != frame->width ||
318  ist->dec_ctx->height != frame->height ||
319  ist->dec_ctx->pix_fmt != frame->format) {
320  av_log(NULL, AV_LOG_DEBUG, "Frame parameters mismatch context %d,%d,%d != %d,%d,%d\n",
321  frame->width,
322  frame->height,
323  frame->format,
324  ist->dec_ctx->width,
325  ist->dec_ctx->height,
326  ist->dec_ctx->pix_fmt);
327  }
328 
329 #if FFMPEG_OPT_TOP
330  if(ist->top_field_first>=0) {
331  av_log(ist, AV_LOG_WARNING, "-top is deprecated, use the setfield filter instead\n");
333  }
334 #endif
335 
336  if (frame->format == d->hwaccel_pix_fmt) {
337  int err = hwaccel_retrieve_data(ist->dec_ctx, frame);
338  if (err < 0)
339  return err;
340  }
341 
343 
344  // forced fixed framerate
345  if (ist->framerate.num) {
347  frame->duration = 1;
349  }
350 
351  // no timestamp available - extrapolate from previous frame duration
352  if (frame->pts == AV_NOPTS_VALUE)
353  frame->pts = d->last_frame_pts == AV_NOPTS_VALUE ? 0 :
354  d->last_frame_pts + d->last_frame_duration_est;
355 
356  // update timestamp history
357  d->last_frame_duration_est = video_duration_estimate(ist, frame);
358  d->last_frame_pts = frame->pts;
359  d->last_frame_tb = frame->time_base;
360 
361  if (debug_ts) {
362  av_log(ist, AV_LOG_INFO,
363  "decoder -> pts:%s pts_time:%s "
364  "pkt_dts:%s pkt_dts_time:%s "
365  "duration:%s duration_time:%s "
366  "keyframe:%d frame_type:%d time_base:%d/%d\n",
367  av_ts2str(frame->pts),
375  }
376 
377  if (ist->st->sample_aspect_ratio.num)
379 
380  return 0;
381 }
382 
383 static void sub2video_flush(InputStream *ist)
384 {
385  for (int i = 0; i < ist->nb_filters; i++) {
386  int ret = ifilter_sub2video(ist->filters[i], NULL);
387  if (ret != AVERROR_EOF && ret < 0)
388  av_log(NULL, AV_LOG_WARNING, "Flush the frame error.\n");
389  }
390 }
391 
393 {
394  Decoder *d = ist->decoder;
395  const AVSubtitle *subtitle = (AVSubtitle*)frame->buf[0]->data;
396  int ret = 0;
397 
398  if (ist->fix_sub_duration) {
399  AVSubtitle *sub_prev = d->sub_prev[0]->buf[0] ?
400  (AVSubtitle*)d->sub_prev[0]->buf[0]->data : NULL;
401  int end = 1;
402  if (sub_prev) {
403  end = av_rescale(subtitle->pts - sub_prev->pts,
404  1000, AV_TIME_BASE);
405  if (end < sub_prev->end_display_time) {
407  "Subtitle duration reduced from %"PRId32" to %d%s\n",
408  sub_prev->end_display_time, end,
409  end <= 0 ? ", dropping it" : "");
410  sub_prev->end_display_time = end;
411  }
412  }
413 
414  av_frame_unref(d->sub_prev[1]);
415  av_frame_move_ref(d->sub_prev[1], frame);
416 
417  frame = d->sub_prev[0];
418  subtitle = frame->buf[0] ? (AVSubtitle*)frame->buf[0]->data : NULL;
419 
420  FFSWAP(AVFrame*, d->sub_prev[0], d->sub_prev[1]);
421 
422  if (end <= 0)
423  return 0;
424  }
425 
426  if (!subtitle)
427  return 0;
428 
429  for (int i = 0; i < ist->nb_filters; i++) {
430  ret = ifilter_sub2video(ist->filters[i], frame);
431  if (ret < 0) {
432  av_log(ist, AV_LOG_ERROR, "Error sending a subtitle for filtering: %s\n",
433  av_err2str(ret));
434  return ret;
435  }
436  }
437 
438  subtitle = (AVSubtitle*)frame->buf[0]->data;
439  if (!subtitle->num_rects)
440  return 0;
441 
442  for (int oidx = 0; oidx < ist->nb_outputs; oidx++) {
443  OutputStream *ost = ist->outputs[oidx];
444  if (!ost->enc || ost->type != AVMEDIA_TYPE_SUBTITLE)
445  continue;
446 
447  ret = enc_subtitle(output_files[ost->file_index], ost, subtitle);
448  if (ret < 0)
449  return ret;
450  }
451 
452  return 0;
453 }
454 
455 int fix_sub_duration_heartbeat(InputStream *ist, int64_t signal_pts)
456 {
457  Decoder *d = ist->decoder;
458  int ret = AVERROR_BUG;
459  AVSubtitle *prev_subtitle = d->sub_prev[0]->buf[0] ?
460  (AVSubtitle*)d->sub_prev[0]->buf[0]->data : NULL;
461  AVSubtitle *subtitle;
462 
463  if (!ist->fix_sub_duration || !prev_subtitle ||
464  !prev_subtitle->num_rects || signal_pts <= prev_subtitle->pts)
465  return 0;
466 
467  av_frame_unref(d->sub_heartbeat);
468  ret = subtitle_wrap_frame(d->sub_heartbeat, prev_subtitle, 1);
469  if (ret < 0)
470  return ret;
471 
472  subtitle = (AVSubtitle*)d->sub_heartbeat->buf[0]->data;
473  subtitle->pts = signal_pts;
474 
475  return process_subtitle(ist, d->sub_heartbeat);
476 }
477 
478 static int transcode_subtitles(InputStream *ist, const AVPacket *pkt,
479  AVFrame *frame)
480 {
481  Decoder *d = ist->decoder;
482  AVPacket *flush_pkt = NULL;
483  AVSubtitle subtitle;
484  int got_output;
485  int ret;
486 
487  if (!pkt) {
488  flush_pkt = av_packet_alloc();
489  if (!flush_pkt)
490  return AVERROR(ENOMEM);
491  }
492 
493  ret = avcodec_decode_subtitle2(ist->dec_ctx, &subtitle, &got_output,
494  pkt ? pkt : flush_pkt);
495  av_packet_free(&flush_pkt);
496 
497  if (ret < 0) {
498  av_log(ist, AV_LOG_ERROR, "Error decoding subtitles: %s\n",
499  av_err2str(ret));
500  ist->decode_errors++;
501  return exit_on_error ? ret : 0;
502  }
503 
504  if (!got_output)
505  return pkt ? 0 : AVERROR_EOF;
506 
507  ist->frames_decoded++;
508 
509  // XXX the queue for transferring data back to the main thread runs
510  // on AVFrames, so we wrap AVSubtitle in an AVBufferRef and put that
511  // inside the frame
512  // eventually, subtitles should be switched to use AVFrames natively
513  ret = subtitle_wrap_frame(frame, &subtitle, 0);
514  if (ret < 0) {
515  avsubtitle_free(&subtitle);
516  return ret;
517  }
518 
519  frame->width = ist->dec_ctx->width;
520  frame->height = ist->dec_ctx->height;
521 
522  ret = tq_send(d->queue_out, 0, frame);
523  if (ret < 0)
525 
526  return ret;
527 }
528 
529 static int send_filter_eof(InputStream *ist)
530 {
531  Decoder *d = ist->decoder;
532  int i, ret;
533 
534  for (i = 0; i < ist->nb_filters; i++) {
535  int64_t end_pts = d->last_frame_pts == AV_NOPTS_VALUE ? AV_NOPTS_VALUE :
536  d->last_frame_pts + d->last_frame_duration_est;
537  ret = ifilter_send_eof(ist->filters[i], end_pts, d->last_frame_tb);
538  if (ret < 0)
539  return ret;
540  }
541  return 0;
542 }
543 
545 {
546  const InputFile *ifile = input_files[ist->file_index];
547  Decoder *d = ist->decoder;
548  AVCodecContext *dec = ist->dec_ctx;
549  const char *type_desc = av_get_media_type_string(dec->codec_type);
550  int ret;
551 
552  if (dec->codec_type == AVMEDIA_TYPE_SUBTITLE)
553  return transcode_subtitles(ist, pkt, frame);
554 
555  // With fate-indeo3-2, we're getting 0-sized packets before EOF for some
556  // reason. This seems like a semi-critical bug. Don't trigger EOF, and
557  // skip the packet.
558  if (pkt && pkt->size == 0)
559  return 0;
560 
561  if (pkt && ifile->format_nots) {
564  }
565 
566  ret = avcodec_send_packet(dec, pkt);
567  if (ret < 0 && !(ret == AVERROR_EOF && !pkt)) {
568  // In particular, we don't expect AVERROR(EAGAIN), because we read all
569  // decoded frames with avcodec_receive_frame() until done.
570  if (ret == AVERROR(EAGAIN)) {
571  av_log(ist, AV_LOG_FATAL, "A decoder returned an unexpected error code. "
572  "This is a bug, please report it.\n");
573  return AVERROR_BUG;
574  }
575  av_log(ist, AV_LOG_ERROR, "Error submitting %s to decoder: %s\n",
576  pkt ? "packet" : "EOF", av_err2str(ret));
577 
578  if (ret != AVERROR_EOF) {
579  ist->decode_errors++;
580  if (!exit_on_error)
581  ret = 0;
582  }
583 
584  return ret;
585  }
586 
587  while (1) {
588  FrameData *fd;
589 
591 
594  update_benchmark("decode_%s %d.%d", type_desc,
595  ist->file_index, ist->index);
596 
597  if (ret == AVERROR(EAGAIN)) {
598  av_assert0(pkt); // should never happen during flushing
599  return 0;
600  } else if (ret == AVERROR_EOF) {
601  return ret;
602  } else if (ret < 0) {
603  av_log(ist, AV_LOG_ERROR, "Decoding error: %s\n", av_err2str(ret));
604  ist->decode_errors++;
605 
606  if (exit_on_error)
607  return ret;
608 
609  continue;
610  }
611 
614  "corrupt decoded frame\n");
615  if (exit_on_error)
616  return AVERROR_INVALIDDATA;
617  }
618 
619 
621  fd = frame_data(frame);
622  if (!fd) {
624  return AVERROR(ENOMEM);
625  }
626  fd->dec.pts = frame->pts;
627  fd->dec.tb = dec->pkt_timebase;
628  fd->dec.frame_num = dec->frame_num - 1;
630 
631  frame->time_base = dec->pkt_timebase;
632 
633  if (dec->codec_type == AVMEDIA_TYPE_AUDIO) {
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  Timestamp ts = { .ts = d->last_frame_pts + d->last_frame_duration_est,
727  .tb = d->last_frame_tb };
729  }
730 
732  } else if (ret < 0) {
733  av_log(ist, AV_LOG_ERROR, "Error processing packet in decoder: %s\n",
734  av_err2str(ret));
735  break;
736  }
737 
738  // signal to the consumer thread that the entire packet was processed
739  ret = tq_send(d->queue_out, 0, dt.frame);
740  if (ret < 0) {
741  if (ret != AVERROR_EOF)
742  av_log(ist, AV_LOG_ERROR, "Error communicating with the main thread\n");
743  break;
744  }
745  }
746 
747  // EOF is normal thread termination
748  if (ret == AVERROR_EOF)
749  ret = 0;
750 
751 finish:
752  tq_receive_finish(d->queue_in, 0);
753  tq_send_finish (d->queue_out, 0);
754 
755  // make sure the demuxer does not get stuck waiting for audio durations
756  // that will never arrive
757  if (ifile->audio_ts_queue && ist->dec->type == AVMEDIA_TYPE_AUDIO)
759 
760  dec_thread_uninit(&dt);
761 
762  av_log(ist, AV_LOG_VERBOSE, "Terminating decoder thread\n");
763 
764  return (void*)(intptr_t)ret;
765 }
766 
767 int dec_packet(InputStream *ist, const AVPacket *pkt, int no_eof)
768 {
769  Decoder *d = ist->decoder;
770  int ret = 0, thread_ret;
771 
772  // thread already joined
773  if (!d->queue_in)
774  return AVERROR_EOF;
775 
776  // send the packet/flush request/EOF to the decoder thread
777  if (pkt || no_eof) {
778  av_packet_unref(d->pkt);
779 
780  if (pkt) {
781  ret = av_packet_ref(d->pkt, pkt);
782  if (ret < 0)
783  goto finish;
784  }
785 
786  ret = tq_send(d->queue_in, 0, d->pkt);
787  if (ret < 0)
788  goto finish;
789  } else
790  tq_send_finish(d->queue_in, 0);
791 
792  // retrieve all decoded data for the packet
793  while (1) {
794  int dummy;
795 
796  ret = tq_receive(d->queue_out, &dummy, d->frame);
797  if (ret < 0)
798  goto finish;
799 
800  // packet fully processed
801  if (!d->frame->buf[0])
802  return 0;
803 
804  // process the decoded frame
805  if (ist->dec->type == AVMEDIA_TYPE_SUBTITLE) {
806  ret = process_subtitle(ist, d->frame);
807  } else {
808  ret = send_frame_to_filters(ist, d->frame);
809  }
810  av_frame_unref(d->frame);
811  if (ret < 0)
812  goto finish;
813  }
814 
815 finish:
816  thread_ret = dec_thread_stop(d);
817  if (thread_ret < 0) {
818  av_log(ist, AV_LOG_ERROR, "Decoder thread returned error: %s\n",
819  av_err2str(thread_ret));
820  ret = err_merge(ret, thread_ret);
821  }
822  // non-EOF errors here are all fatal
823  if (ret < 0 && ret != AVERROR_EOF)
824  return ret;
825 
826  // signal EOF to our downstreams
827  if (ist->dec->type == AVMEDIA_TYPE_SUBTITLE)
828  sub2video_flush(ist);
829  else {
830  ret = send_filter_eof(ist);
831  if (ret < 0) {
832  av_log(NULL, AV_LOG_FATAL, "Error marking filters as finished\n");
833  return ret;
834  }
835  }
836 
837  return AVERROR_EOF;
838 }
839 
841 {
842  Decoder *d = ist->decoder;
843  ObjPool *op;
844  int ret = 0;
845 
847  if (!op)
848  return AVERROR(ENOMEM);
849 
850  d->queue_in = tq_alloc(1, 1, op, pkt_move);
851  if (!d->queue_in) {
852  objpool_free(&op);
853  return AVERROR(ENOMEM);
854  }
855 
857  if (!op)
858  goto fail;
859 
860  d->queue_out = tq_alloc(1, 4, op, frame_move);
861  if (!d->queue_out) {
862  objpool_free(&op);
863  goto fail;
864  }
865 
866  ret = pthread_create(&d->thread, NULL, decoder_thread, ist);
867  if (ret) {
868  ret = AVERROR(ret);
869  av_log(ist, AV_LOG_ERROR, "pthread_create() failed: %s\n",
870  av_err2str(ret));
871  goto fail;
872  }
873 
874  return 0;
875 fail:
876  if (ret >= 0)
877  ret = AVERROR(ENOMEM);
878 
879  tq_free(&d->queue_in);
880  tq_free(&d->queue_out);
881  return ret;
882 }
883 
885 {
886  InputStream *ist = s->opaque;
887  Decoder *d = ist->decoder;
888  const enum AVPixelFormat *p;
889 
890  for (p = pix_fmts; *p != AV_PIX_FMT_NONE; p++) {
892  const AVCodecHWConfig *config = NULL;
893  int i;
894 
895  if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
896  break;
897 
898  if (ist->hwaccel_id == HWACCEL_GENERIC ||
899  ist->hwaccel_id == HWACCEL_AUTO) {
900  for (i = 0;; i++) {
901  config = avcodec_get_hw_config(s->codec, i);
902  if (!config)
903  break;
904  if (!(config->methods &
906  continue;
907  if (config->pix_fmt == *p)
908  break;
909  }
910  }
911  if (config && config->device_type == ist->hwaccel_device_type) {
912  d->hwaccel_pix_fmt = *p;
913  break;
914  }
915  }
916 
917  return *p;
918 }
919 
921 {
922  const AVCodecHWConfig *config;
923  HWDevice *dev;
924  int i;
925  for (i = 0;; i++) {
926  config = avcodec_get_hw_config(codec, i);
927  if (!config)
928  return NULL;
930  continue;
931  dev = hw_device_get_by_type(config->device_type);
932  if (dev)
933  return dev;
934  }
935 }
936 
938 {
939  const AVCodecHWConfig *config;
940  enum AVHWDeviceType type;
941  HWDevice *dev = NULL;
942  int err, auto_device = 0;
943 
944  if (ist->hwaccel_device) {
946  if (!dev) {
947  if (ist->hwaccel_id == HWACCEL_AUTO) {
948  auto_device = 1;
949  } else if (ist->hwaccel_id == HWACCEL_GENERIC) {
950  type = ist->hwaccel_device_type;
952  &dev);
953  } else {
954  // This will be dealt with by API-specific initialisation
955  // (using hwaccel_device), so nothing further needed here.
956  return 0;
957  }
958  } else {
959  if (ist->hwaccel_id == HWACCEL_AUTO) {
960  ist->hwaccel_device_type = dev->type;
961  } else if (ist->hwaccel_device_type != dev->type) {
962  av_log(NULL, AV_LOG_ERROR, "Invalid hwaccel device "
963  "specified for decoder: device %s of type %s is not "
964  "usable with hwaccel %s.\n", dev->name,
967  return AVERROR(EINVAL);
968  }
969  }
970  } else {
971  if (ist->hwaccel_id == HWACCEL_AUTO) {
972  auto_device = 1;
973  } else if (ist->hwaccel_id == HWACCEL_GENERIC) {
974  type = ist->hwaccel_device_type;
976 
977  // When "-qsv_device device" is used, an internal QSV device named
978  // as "__qsv_device" is created. Another QSV device is created too
979  // if "-init_hw_device qsv=name:device" is used. There are 2 QSV devices
980  // if both "-qsv_device device" and "-init_hw_device qsv=name:device"
981  // are used, hw_device_get_by_type(AV_HWDEVICE_TYPE_QSV) returns NULL.
982  // To keep back-compatibility with the removed ad-hoc libmfx setup code,
983  // call hw_device_get_by_name("__qsv_device") to select the internal QSV
984  // device.
985  if (!dev && type == AV_HWDEVICE_TYPE_QSV)
986  dev = hw_device_get_by_name("__qsv_device");
987 
988  if (!dev)
989  err = hw_device_init_from_type(type, NULL, &dev);
990  } else {
991  dev = hw_device_match_by_codec(ist->dec);
992  if (!dev) {
993  // No device for this codec, but not using generic hwaccel
994  // and therefore may well not need one - ignore.
995  return 0;
996  }
997  }
998  }
999 
1000  if (auto_device) {
1001  int i;
1002  if (!avcodec_get_hw_config(ist->dec, 0)) {
1003  // Decoder does not support any hardware devices.
1004  return 0;
1005  }
1006  for (i = 0; !dev; i++) {
1007  config = avcodec_get_hw_config(ist->dec, i);
1008  if (!config)
1009  break;
1010  type = config->device_type;
1011  dev = hw_device_get_by_type(type);
1012  if (dev) {
1013  av_log(NULL, AV_LOG_INFO, "Using auto "
1014  "hwaccel type %s with existing device %s.\n",
1016  }
1017  }
1018  for (i = 0; !dev; i++) {
1019  config = avcodec_get_hw_config(ist->dec, i);
1020  if (!config)
1021  break;
1022  type = config->device_type;
1023  // Try to make a new device of this type.
1025  &dev);
1026  if (err < 0) {
1027  // Can't make a device of this type.
1028  continue;
1029  }
1030  if (ist->hwaccel_device) {
1031  av_log(NULL, AV_LOG_INFO, "Using auto "
1032  "hwaccel type %s with new device created "
1033  "from %s.\n", av_hwdevice_get_type_name(type),
1034  ist->hwaccel_device);
1035  } else {
1036  av_log(NULL, AV_LOG_INFO, "Using auto "
1037  "hwaccel type %s with new default device.\n",
1039  }
1040  }
1041  if (dev) {
1042  ist->hwaccel_device_type = type;
1043  } else {
1044  av_log(NULL, AV_LOG_INFO, "Auto hwaccel "
1045  "disabled: no device found.\n");
1046  ist->hwaccel_id = HWACCEL_NONE;
1047  return 0;
1048  }
1049  }
1050 
1051  if (!dev) {
1052  av_log(NULL, AV_LOG_ERROR, "No device available "
1053  "for decoder: device type %s needed for codec %s.\n",
1055  return err;
1056  }
1057 
1059  if (!ist->dec_ctx->hw_device_ctx)
1060  return AVERROR(ENOMEM);
1061 
1062  return 0;
1063 }
1064 
1066 {
1067  Decoder *d;
1068  const AVCodec *codec = ist->dec;
1069  int ret;
1070 
1071  if (!codec) {
1072  av_log(ist, AV_LOG_ERROR,
1073  "Decoding requested, but no decoder found for: %s\n",
1075  return AVERROR(EINVAL);
1076  }
1077 
1078  ret = dec_alloc(&ist->decoder);
1079  if (ret < 0)
1080  return ret;
1081  d = ist->decoder;
1082 
1083  if (codec->type == AVMEDIA_TYPE_SUBTITLE && ist->fix_sub_duration) {
1084  for (int i = 0; i < FF_ARRAY_ELEMS(d->sub_prev); i++) {
1085  d->sub_prev[i] = av_frame_alloc();
1086  if (!d->sub_prev[i])
1087  return AVERROR(ENOMEM);
1088  }
1089  d->sub_heartbeat = av_frame_alloc();
1090  if (!d->sub_heartbeat)
1091  return AVERROR(ENOMEM);
1092  }
1093 
1094  ist->dec_ctx->opaque = ist;
1095  ist->dec_ctx->get_format = get_format;
1096 
1097  if (ist->dec_ctx->codec_id == AV_CODEC_ID_DVB_SUBTITLE &&
1098  (ist->decoding_needed & DECODING_FOR_OST)) {
1099  av_dict_set(&ist->decoder_opts, "compute_edt", "1", AV_DICT_DONT_OVERWRITE);
1101  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");
1102  }
1103 
1104  /* Useful for subtitles retiming by lavf (FIXME), skipping samples in
1105  * audio, and video decoders such as cuvid or mediacodec */
1106  ist->dec_ctx->pkt_timebase = ist->st->time_base;
1107 
1108  if (!av_dict_get(ist->decoder_opts, "threads", NULL, 0))
1109  av_dict_set(&ist->decoder_opts, "threads", "auto", 0);
1110  /* Attached pics are sparse, therefore we would not want to delay their decoding till EOF. */
1112  av_dict_set(&ist->decoder_opts, "threads", "1", 0);
1113 
1115  if (ret < 0) {
1116  av_log(ist, AV_LOG_ERROR,
1117  "Hardware device setup failed for decoder: %s\n",
1118  av_err2str(ret));
1119  return ret;
1120  }
1121 
1122  if ((ret = avcodec_open2(ist->dec_ctx, codec, &ist->decoder_opts)) < 0) {
1123  av_log(ist, AV_LOG_ERROR, "Error while opening decoder: %s\n",
1124  av_err2str(ret));
1125  return ret;
1126  }
1127 
1129  if (ret < 0)
1130  return ret;
1131 
1132  ret = dec_thread_start(ist);
1133  if (ret < 0) {
1134  av_log(ist, AV_LOG_ERROR, "Error starting decoder thread: %s\n",
1135  av_err2str(ret));
1136  return ret;
1137  }
1138 
1139  return 0;
1140 }
AVSubtitle
Definition: avcodec.h:2269
process_subtitle
static int process_subtitle(InputStream *ist, AVFrame *frame)
Definition: ffmpeg_dec.c:392
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:398
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:529
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:382
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_utils.h:41
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
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:166
FrameData
Definition: ffmpeg.h:629
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:374
audio_ts_process
static void audio_ts_process(void *logctx, Decoder *d, AVFrame *frame)
Definition: ffmpeg_dec.c:209
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:920
DecThreadContext::pkt
AVPacket * pkt
Definition: ffmpeg_dec.c:77
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
frame_move
static void frame_move(void *dst, void *src)
Definition: ffmpeg_utils.h:51
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:367
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:246
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
AVFrame::buf
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:590
Timestamp::ts
int64_t ts
Definition: ffmpeg_utils.h:31
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:98
InputStream::nb_outputs
int nb_outputs
Definition: ffmpeg.h:375
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:632
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:937
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:141
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:884
dummy
int dummy
Definition: motion.c:66
InputStream::decoder_opts
AVDictionary * decoder_opts
Definition: ffmpeg.h:350
update_benchmark
void update_benchmark(const char *fmt,...)
Definition: ffmpeg.c:467
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:635
codec.h
AVRational::num
int num
Numerator.
Definition: rational.h:59
InputFile
Definition: ffmpeg.h:392
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:117
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:553
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:75
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:76
dec_packet
int dec_packet(InputStream *ist, const AVPacket *pkt, int no_eof)
Submit a packet for decoding.
Definition: ffmpeg_dec.c:767
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:49
s
#define s(width, name)
Definition: cbs_vp9.c:198
InputStream::framerate
AVRational framerate
Definition: ffmpeg.h:351
Decoder::queue_in
ThreadQueue * queue_in
Queue for sending coded packets from the main thread to the decoder thread.
Definition: ffmpeg_dec.c:63
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:739
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:366
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
ffmpeg_utils.h
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
pkt_move
static void pkt_move(void *dst, void *src)
Definition: ffmpeg_utils.h:46
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:353
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:241
AVCodec::type
enum AVMediaType type
Definition: codec.h:200
Decoder
Definition: ffmpeg_dec.c:36
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:380
InputStream::fix_sub_duration
int fix_sub_duration
Definition: ffmpeg.h:358
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:124
error.h
InputStream::frames_decoded
uint64_t frames_decoded
Definition: ffmpeg.h:387
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:381
Decoder::last_frame_pts
int64_t last_frame_pts
Definition: ffmpeg_dec.c:45
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:127
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:2363
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:456
dec_open
int dec_open(InputStream *ist)
Definition: ffmpeg_dec.c:1065
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:430
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:388
FrameData::pts
int64_t pts
Definition: ffmpeg.h:634
fix_sub_duration_heartbeat
int fix_sub_duration_heartbeat(InputStream *ist, int64_t signal_pts)
Definition: ffmpeg_dec.c:455
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:80
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:383
Decoder::last_frame_tb
AVRational last_frame_tb
Definition: ffmpeg_dec.c:47
Decoder::frame
AVFrame * frame
Definition: ffmpeg_dec.c:37
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: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:484
FrameData::bits_per_raw_sample
int bits_per_raw_sample
Definition: ffmpeg.h:640
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:46
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:196
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:478
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:544
HWDevice::name
const char * name
Definition: ffmpeg.h:84
AVRational::den
int den
Denominator.
Definition: rational.h:60
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
video_frame_process
static int video_frame_process(InputStream *ist, AVFrame *frame)
Definition: ffmpeg_dec.c:298
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:2315
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:389
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
InputFile::audio_ts_queue
AVThreadMessageQueue * audio_ts_queue
Definition: ffmpeg.h:424
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:48
Decoder::thread
pthread_t thread
Definition: ffmpeg_dec.c:55
AVPacket
This structure stores compressed data.
Definition: packet.h:468
ifilter_sub2video
int ifilter_sub2video(InputFilter *ifilter, const AVFrame *frame)
Definition: ffmpeg_filter.c:2282
Decoder::queue_out
ThreadQueue * queue_out
Queue for sending decoded frames from the decoder thread to the main thread.
Definition: ffmpeg_dec.c:71
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:53
HWDevice::type
enum AVHWDeviceType type
Definition: ffmpeg.h:85
Decoder::sub_prev
AVFrame * sub_prev[2]
Definition: ffmpeg_dec.c:52
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
dec_thread_start
static int dec_thread_start(InputStream *ist)
Definition: ffmpeg_dec.c:840
Timestamp
Definition: ffmpeg_utils.h:30
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:40
snprintf
#define snprintf
Definition: snprintf.h:34
Decoder::pkt
AVPacket * pkt
Definition: ffmpeg_dec.c:38
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:148
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:226