FFmpeg  4.4.9
fifo.c
Go to the documentation of this file.
1 /*
2  * FIFO pseudo-muxer
3  * Copyright (c) 2016 Jan Sebechlebsky
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with FFmpeg; if not, write to the Free Software * Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <stdatomic.h>
23 
24 #include "libavutil/avassert.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/time.h"
27 #include "libavutil/thread.h"
29 #include "avformat.h"
30 #include "internal.h"
31 
32 #define FIFO_DEFAULT_QUEUE_SIZE 60
33 #define FIFO_DEFAULT_MAX_RECOVERY_ATTEMPTS 0
34 #define FIFO_DEFAULT_RECOVERY_WAIT_TIME_USEC 5000000 // 5 seconds
35 
36 typedef struct FifoContext {
37  const AVClass *class;
39 
40  char *format;
42 
45 
47 
48  /* Return value of last write_trailer_call */
50 
51  /* Time to wait before next recovery attempt
52  * This can refer to the time in processed stream,
53  * or real time. */
55 
56  /* Maximal number of unsuccessful successive recovery attempts */
58 
59  /* Whether to attempt recovery from failure */
61 
62  /* If >0 stream time will be used when waiting
63  * for the recovery attempt instead of real time */
65 
66  /* If >0 recovery will be attempted regardless of error code
67  * (except AVERROR_EXIT, so exit request is never ignored) */
69 
70  /* Whether to drop packets in case the queue is full. */
72 
73  /* Whether to wait for keyframe when recovering
74  * from failure or queue overflow */
76 
79  /* Value > 0 signals queue overflow */
81 
85 } FifoContext;
86 
87 typedef struct FifoThreadContext {
89 
90  /* Timestamp of last failure.
91  * This is either pts in case stream time is used,
92  * or microseconds as returned by av_getttime_relative() */
94 
95  /* Number of current recovery process
96  * Value > 0 means we are in recovery process */
98 
99  /* If > 0 all frames will be dropped until keyframe is received */
101 
102  /* Value > 0 means that the previous write_header call was successful
103  * so finalization by calling write_trailer and ff_io_close must be done
104  * before exiting / reinitialization of underlying muxer */
106 
109 
110 typedef enum FifoMessageType {
116 
117 typedef struct FifoMessage {
120 } FifoMessage;
121 
123 {
124  AVFormatContext *avf = ctx->avf;
125  FifoContext *fifo = avf->priv_data;
126  AVFormatContext *avf2 = fifo->avf;
127  AVDictionary *format_options = NULL;
128  int ret, i;
129 
130  ret = av_dict_copy(&format_options, fifo->format_options, 0);
131  if (ret < 0)
132  goto end;
133 
134  ret = ff_format_output_open(avf2, avf->url, &format_options);
135  if (ret < 0) {
136  av_log(avf, AV_LOG_ERROR, "Error opening %s: %s\n", avf->url,
137  av_err2str(ret));
138  goto end;
139  }
140 
141  for (i = 0;i < avf2->nb_streams; i++)
142  avf2->streams[i]->cur_dts = 0;
143 
144  ret = avformat_write_header(avf2, &format_options);
145  if (!ret) {
146  ctx->header_written = 1;
147  } else {
148  ff_format_io_close(avf2, &avf2->pb);
149  }
150 
151  // Check for options unrecognized by underlying muxer
152  if (format_options) {
153  AVDictionaryEntry *entry = NULL;
154  while ((entry = av_dict_get(format_options, "", entry, AV_DICT_IGNORE_SUFFIX)))
155  av_log(avf2, AV_LOG_ERROR, "Unknown option '%s'\n", entry->key);
156  ret = AVERROR(EINVAL);
157  }
158 
159 end:
160  av_dict_free(&format_options);
161  return ret;
162 }
163 
165 {
166  AVFormatContext *avf = ctx->avf;
167  FifoContext *fifo = avf->priv_data;
168  AVFormatContext *avf2 = fifo->avf;
169 
170  return av_write_frame(avf2, NULL);
171 }
172 
174 {
175  AVStream *st = avf->streams[pkt->stream_index];
177  int64_t duration = (*last_dts == AV_NOPTS_VALUE ? 0 : dts - *last_dts);
178  *last_dts = dts;
179  return duration;
180 }
181 
183 {
184  AVFormatContext *avf = ctx->avf;
185  FifoContext *fifo = avf->priv_data;
186  AVFormatContext *avf2 = fifo->avf;
187  AVRational src_tb, dst_tb;
188  int ret, s_idx;
189  int64_t orig_pts, orig_dts, orig_duration;
190 
191  if (fifo->timeshift && pkt->dts != AV_NOPTS_VALUE)
192  atomic_fetch_sub_explicit(&fifo->queue_duration, next_duration(avf, pkt, &ctx->last_received_dts), memory_order_relaxed);
193 
194  if (ctx->drop_until_keyframe) {
195  if (pkt->flags & AV_PKT_FLAG_KEY) {
196  ctx->drop_until_keyframe = 0;
197  av_log(avf, AV_LOG_VERBOSE, "Keyframe received, recovering...\n");
198  } else {
199  av_log(avf, AV_LOG_VERBOSE, "Dropping non-keyframe packet\n");
201  return 0;
202  }
203  }
204 
205  orig_pts = pkt->pts;
206  orig_dts = pkt->dts;
207  orig_duration = pkt->duration;
208  s_idx = pkt->stream_index;
209  src_tb = avf->streams[s_idx]->time_base;
210  dst_tb = avf2->streams[s_idx]->time_base;
211  av_packet_rescale_ts(pkt, src_tb, dst_tb);
212 
213  ret = av_write_frame(avf2, pkt);
214  if (ret >= 0) {
216  } else {
217  // avoid scaling twice
218  pkt->pts = orig_pts;
219  pkt->dts = orig_dts;
220  pkt->duration = orig_duration;
221  }
222  return ret;
223 }
224 
226 {
227  AVFormatContext *avf = ctx->avf;
228  FifoContext *fifo = avf->priv_data;
229  AVFormatContext *avf2 = fifo->avf;
230  int ret;
231 
232  if (!ctx->header_written)
233  return 0;
234 
235  ret = av_write_trailer(avf2);
236  ff_format_io_close(avf2, &avf2->pb);
237 
238  return ret;
239 }
240 
242 {
243  int ret = AVERROR(EINVAL);
244 
245  if (msg->type == FIFO_NOOP)
246  return 0;
247 
248  if (!ctx->header_written) {
250  if (ret < 0)
251  return ret;
252  }
253 
254  switch(msg->type) {
255  case FIFO_WRITE_HEADER:
256  av_assert0(ret >= 0);
257  return ret;
258  case FIFO_WRITE_PACKET:
259  return fifo_thread_write_packet(ctx, &msg->pkt);
260  case FIFO_FLUSH_OUTPUT:
262  }
263 
264  av_assert0(0);
265  return AVERROR(EINVAL);
266 }
267 
268 static int is_recoverable(const FifoContext *fifo, int err_no) {
269  if (!fifo->attempt_recovery)
270  return 0;
271 
272  if (fifo->recover_any_error)
273  return err_no != AVERROR_EXIT;
274 
275  switch (err_no) {
276  case AVERROR(EINVAL):
277  case AVERROR(ENOSYS):
278  case AVERROR_EOF:
279  case AVERROR_EXIT:
281  return 0;
282  default:
283  return 1;
284  }
285 }
286 
287 static void free_message(void *msg)
288 {
289  FifoMessage *fifo_msg = msg;
290 
291  if (fifo_msg->type == FIFO_WRITE_PACKET)
292  av_packet_unref(&fifo_msg->pkt);
293 }
294 
296  int err_no)
297 {
298  AVFormatContext *avf = ctx->avf;
299  FifoContext *fifo = avf->priv_data;
300  int ret;
301 
302  av_log(avf, AV_LOG_INFO, "Recovery failed: %s\n",
303  av_err2str(err_no));
304 
305  if (fifo->recovery_wait_streamtime) {
306  if (pkt->pts == AV_NOPTS_VALUE)
307  av_log(avf, AV_LOG_WARNING, "Packet does not contain presentation"
308  " timestamp, recovery will be attempted immediately");
309  ctx->last_recovery_ts = pkt->pts;
310  } else {
311  ctx->last_recovery_ts = av_gettime_relative();
312  }
313 
314  if (fifo->max_recovery_attempts &&
315  ctx->recovery_nr >= fifo->max_recovery_attempts) {
316  av_log(avf, AV_LOG_ERROR,
317  "Maximal number of %d recovery attempts reached.\n",
318  fifo->max_recovery_attempts);
319  ret = err_no;
320  } else {
321  ret = AVERROR(EAGAIN);
322  }
323 
324  return ret;
325 }
326 
328 {
329  AVFormatContext *avf = ctx->avf;
330  FifoContext *fifo = avf->priv_data;
331  AVPacket *pkt = &msg->pkt;
332  int64_t time_since_recovery;
333  int ret;
334 
335  if (!is_recoverable(fifo, err_no)) {
336  ret = err_no;
337  goto fail;
338  }
339 
340  if (ctx->header_written) {
342  ctx->header_written = 0;
343  }
344 
345  if (!ctx->recovery_nr) {
346  ctx->last_recovery_ts = fifo->recovery_wait_streamtime ?
347  AV_NOPTS_VALUE : 0;
348  } else {
349  if (fifo->recovery_wait_streamtime) {
350  if (ctx->last_recovery_ts == AV_NOPTS_VALUE) {
352  time_since_recovery = av_rescale_q(pkt->pts - ctx->last_recovery_ts,
353  tb, AV_TIME_BASE_Q);
354  } else {
355  /* Enforce recovery immediately */
356  time_since_recovery = fifo->recovery_wait_time;
357  }
358  } else {
359  time_since_recovery = av_gettime_relative() - ctx->last_recovery_ts;
360  }
361 
362  if (time_since_recovery < fifo->recovery_wait_time)
363  return AVERROR(EAGAIN);
364  }
365 
366  ctx->recovery_nr++;
367 
368  if (fifo->max_recovery_attempts) {
369  av_log(avf, AV_LOG_VERBOSE, "Recovery attempt #%d/%d\n",
370  ctx->recovery_nr, fifo->max_recovery_attempts);
371  } else {
372  av_log(avf, AV_LOG_VERBOSE, "Recovery attempt #%d\n",
373  ctx->recovery_nr);
374  }
375 
376  if (fifo->restart_with_keyframe && fifo->drop_pkts_on_overflow)
377  ctx->drop_until_keyframe = 1;
378 
379  ret = fifo_thread_dispatch_message(ctx, msg);
380  if (ret < 0) {
381  if (is_recoverable(fifo, ret)) {
383  } else {
384  goto fail;
385  }
386  } else {
387  av_log(avf, AV_LOG_INFO, "Recovery successful\n");
388  ctx->recovery_nr = 0;
389  }
390 
391  return 0;
392 
393 fail:
394  free_message(msg);
395  return ret;
396 }
397 
398 static int fifo_thread_recover(FifoThreadContext *ctx, FifoMessage *msg, int err_no)
399 {
400  AVFormatContext *avf = ctx->avf;
401  FifoContext *fifo = avf->priv_data;
402  int ret;
403 
404  do {
405  if (!fifo->recovery_wait_streamtime && ctx->recovery_nr > 0) {
406  int64_t time_since_recovery = av_gettime_relative() - ctx->last_recovery_ts;
407  int64_t time_to_wait = FFMAX(0, fifo->recovery_wait_time - time_since_recovery);
408  if (time_to_wait)
409  av_usleep(FFMIN(10000, time_to_wait));
410  }
411 
412  ret = fifo_thread_attempt_recovery(ctx, msg, err_no);
413  } while (ret == AVERROR(EAGAIN) && !fifo->drop_pkts_on_overflow);
414 
415  if (ret == AVERROR(EAGAIN) && fifo->drop_pkts_on_overflow) {
416  if (msg->type == FIFO_WRITE_PACKET)
417  av_packet_unref(&msg->pkt);
418  ret = 0;
419  }
420 
421  return ret;
422 }
423 
424 static void *fifo_consumer_thread(void *data)
425 {
426  AVFormatContext *avf = data;
427  FifoContext *fifo = avf->priv_data;
428  AVThreadMessageQueue *queue = fifo->queue;
429  FifoMessage msg = {fifo->timeshift ? FIFO_NOOP : FIFO_WRITE_HEADER, {0}};
430  int ret;
431 
432  FifoThreadContext fifo_thread_ctx;
433  memset(&fifo_thread_ctx, 0, sizeof(FifoThreadContext));
434  fifo_thread_ctx.avf = avf;
435  fifo_thread_ctx.last_received_dts = AV_NOPTS_VALUE;
436 
437  while (1) {
438  uint8_t just_flushed = 0;
439 
440  if (!fifo_thread_ctx.recovery_nr)
441  ret = fifo_thread_dispatch_message(&fifo_thread_ctx, &msg);
442 
443  if (ret < 0 || fifo_thread_ctx.recovery_nr > 0) {
444  int rec_ret = fifo_thread_recover(&fifo_thread_ctx, &msg, ret);
445  if (rec_ret < 0) {
446  av_thread_message_queue_set_err_send(queue, rec_ret);
447  break;
448  }
449  }
450 
451  /* If the queue is full at the moment when fifo_write_packet
452  * attempts to insert new message (packet) to the queue,
453  * it sets the fifo->overflow_flag to 1 and drops packet.
454  * Here in consumer thread, the flag is checked and if it is
455  * set, the queue is flushed and flag cleared. */
457  if (fifo->overflow_flag) {
459  if (fifo->restart_with_keyframe)
460  fifo_thread_ctx.drop_until_keyframe = 1;
461  fifo->overflow_flag = 0;
462  just_flushed = 1;
463  }
465 
466  if (just_flushed)
467  av_log(avf, AV_LOG_INFO, "FIFO queue flushed\n");
468 
469  if (fifo->timeshift)
470  while (atomic_load_explicit(&fifo->queue_duration, memory_order_relaxed) < fifo->timeshift)
471  av_usleep(10000);
472 
473  ret = av_thread_message_queue_recv(queue, &msg, 0);
474  if (ret < 0) {
476  break;
477  }
478  }
479 
480  fifo->write_trailer_ret = fifo_thread_write_trailer(&fifo_thread_ctx);
481 
482  return NULL;
483 }
484 
486  const char *filename)
487 {
488  FifoContext *fifo = avf->priv_data;
489  AVFormatContext *avf2;
490  int ret = 0, i;
491 
492  ret = avformat_alloc_output_context2(&avf2, oformat, NULL, filename);
493  if (ret < 0)
494  return ret;
495 
496  fifo->avf = avf2;
497 
499  avf2->max_delay = avf->max_delay;
500  ret = av_dict_copy(&avf2->metadata, avf->metadata, 0);
501  if (ret < 0)
502  return ret;
503  avf2->opaque = avf->opaque;
504  avf2->io_close = avf->io_close;
505  avf2->io_open = avf->io_open;
506  avf2->flags = avf->flags;
507 
508  for (i = 0; i < avf->nb_streams; ++i) {
509  AVStream *st = avformat_new_stream(avf2, NULL);
510  if (!st)
511  return AVERROR(ENOMEM);
512 
513  ret = ff_stream_encode_params_copy(st, avf->streams[i]);
514  if (ret < 0)
515  return ret;
516  }
517 
518  return 0;
519 }
520 
521 static int fifo_init(AVFormatContext *avf)
522 {
523  FifoContext *fifo = avf->priv_data;
524  ff_const59 AVOutputFormat *oformat;
525  int ret = 0;
526 
527  if (fifo->recovery_wait_streamtime && !fifo->drop_pkts_on_overflow) {
528  av_log(avf, AV_LOG_ERROR, "recovery_wait_streamtime can be turned on"
529  " only when drop_pkts_on_overflow is also turned on\n");
530  return AVERROR(EINVAL);
531  }
532  atomic_init(&fifo->queue_duration, 0);
534 
535  oformat = av_guess_format(fifo->format, avf->url, NULL);
536  if (!oformat) {
538  return ret;
539  }
540 
541  ret = fifo_mux_init(avf, oformat, avf->url);
542  if (ret < 0)
543  return ret;
544 
545  ret = av_thread_message_queue_alloc(&fifo->queue, (unsigned) fifo->queue_size,
546  sizeof(FifoMessage));
547  if (ret < 0)
548  return ret;
549 
551 
553  if (ret < 0)
554  return AVERROR(ret);
556 
557  return 0;
558 }
559 
561 {
562  FifoContext * fifo = avf->priv_data;
563  int ret;
564 
566  if (ret) {
567  av_log(avf, AV_LOG_ERROR, "Failed to start thread: %s\n",
568  av_err2str(AVERROR(ret)));
569  ret = AVERROR(ret);
570  }
571 
572  return ret;
573 }
574 
576 {
577  FifoContext *fifo = avf->priv_data;
579  int ret;
580 
581  if (pkt) {
582  ret = av_packet_ref(&msg.pkt,pkt);
583  if (ret < 0)
584  return ret;
585  }
586 
587  ret = av_thread_message_queue_send(fifo->queue, &msg,
588  fifo->drop_pkts_on_overflow ?
590  if (ret == AVERROR(EAGAIN)) {
591  uint8_t overflow_set = 0;
592 
593  /* Queue is full, set fifo->overflow_flag to 1
594  * to let consumer thread know the queue should
595  * be flushed. */
597  if (!fifo->overflow_flag)
598  fifo->overflow_flag = overflow_set = 1;
600 
601  if (overflow_set)
602  av_log(avf, AV_LOG_WARNING, "FIFO queue full\n");
603  ret = 0;
604  goto fail;
605  } else if (ret < 0) {
606  goto fail;
607  }
608 
609  if (fifo->timeshift && pkt && pkt->dts != AV_NOPTS_VALUE)
610  atomic_fetch_add_explicit(&fifo->queue_duration, next_duration(avf, pkt, &fifo->last_sent_dts), memory_order_relaxed);
611 
612  return ret;
613 fail:
614  if (pkt)
615  av_packet_unref(&msg.pkt);
616  return ret;
617 }
618 
620 {
621  FifoContext *fifo= avf->priv_data;
622  int ret;
623 
625  if (fifo->timeshift) {
627  int64_t elapsed = 0;
628  FifoMessage msg = {FIFO_NOOP};
629  do {
630  int64_t delay = av_gettime_relative() - now;
631  if (delay < 0) { // Discontinuity?
632  delay = 10000;
633  now = av_gettime_relative();
634  } else {
635  now += delay;
636  }
637  atomic_fetch_add_explicit(&fifo->queue_duration, delay, memory_order_relaxed);
638  elapsed += delay;
639  if (elapsed > fifo->timeshift)
640  break;
641  av_usleep(10000);
643  } while (ret >= 0 || ret == AVERROR(EAGAIN));
644  atomic_store(&fifo->queue_duration, INT64_MAX);
645  }
646 
647  ret = pthread_join(fifo->writer_thread, NULL);
648  if (ret < 0) {
649  av_log(avf, AV_LOG_ERROR, "pthread join error: %s\n",
650  av_err2str(AVERROR(ret)));
651  return AVERROR(ret);
652  }
653 
654  ret = fifo->write_trailer_ret;
655  return ret;
656 }
657 
658 static void fifo_deinit(AVFormatContext *avf)
659 {
660  FifoContext *fifo = avf->priv_data;
661 
662  avformat_free_context(fifo->avf);
666 }
667 
668 #define OFFSET(x) offsetof(FifoContext, x)
669 static const AVOption options[] = {
670  {"fifo_format", "Target muxer", OFFSET(format),
672 
673  {"queue_size", "Size of fifo queue", OFFSET(queue_size),
675 
676  {"format_opts", "Options to be passed to underlying muxer", OFFSET(format_options),
678 
679  {"drop_pkts_on_overflow", "Drop packets on fifo queue overflow not to block encoder", OFFSET(drop_pkts_on_overflow),
680  AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
681 
682  {"restart_with_keyframe", "Wait for keyframe when restarting output", OFFSET(restart_with_keyframe),
683  AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
684 
685  {"attempt_recovery", "Attempt recovery in case of failure", OFFSET(attempt_recovery),
686  AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
687 
688  {"max_recovery_attempts", "Maximal number of recovery attempts", OFFSET(max_recovery_attempts),
690 
691  {"recovery_wait_time", "Waiting time between recovery attempts", OFFSET(recovery_wait_time),
693 
694  {"recovery_wait_streamtime", "Use stream time instead of real time while waiting for recovery",
695  OFFSET(recovery_wait_streamtime), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
696 
697  {"recover_any_error", "Attempt recovery regardless of type of the error", OFFSET(recover_any_error),
698  AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
699 
700  {"timeshift", "Delay fifo output", OFFSET(timeshift),
701  AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX, AV_OPT_FLAG_ENCODING_PARAM},
702 
703  {NULL},
704 };
705 
706 static const AVClass fifo_muxer_class = {
707  .class_name = "Fifo muxer",
708  .item_name = av_default_item_name,
709  .option = options,
710  .version = LIBAVUTIL_VERSION_INT,
711 };
712 
714  .name = "fifo",
715  .long_name = NULL_IF_CONFIG_SMALL("FIFO queue pseudo-muxer"),
716  .priv_data_size = sizeof(FifoContext),
717  .init = fifo_init,
721  .deinit = fifo_deinit,
722  .priv_class = &fifo_muxer_class,
724 };
static const char *const format[]
Definition: af_aiir.c:457
uint8_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
Main libavformat public API header.
int avformat_alloc_output_context2(AVFormatContext **ctx, ff_const59 AVOutputFormat *oformat, const char *format_name, const char *filename)
Allocate an AVFormatContext for an output format.
Definition: mux.c:136
#define AVFMT_ALLOW_FLUSH
Format allows flushing.
Definition: avformat.h:471
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:458
#define AVFMT_TS_NEGATIVE
Format allows muxing negative timestamps.
Definition: avformat.h:475
#define ff_const59
The ff_const59 define is not part of the public API and will be removed without further warning.
Definition: avformat.h:533
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define fail()
Definition: checkasm.h:133
static void deinit(AVFormatContext *s)
Definition: chromaprint.c:50
#define FFMIN(a, b)
Definition: common.h:105
#define FFMAX(a, b)
Definition: common.h:103
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
#define atomic_store(object, desired)
Definition: stdatomic.h:85
#define atomic_fetch_add_explicit(object, operand, order)
Definition: stdatomic.h:149
#define atomic_load_explicit(object, order)
Definition: stdatomic.h:96
intptr_t atomic_int_least64_t
Definition: stdatomic.h:68
#define atomic_init(obj, value)
Definition: stdatomic.h:33
#define atomic_fetch_sub_explicit(object, operand, order)
Definition: stdatomic.h:152
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:740
#define pthread_mutex_lock(a)
Definition: ffprobe.c:63
#define pthread_mutex_unlock(a)
Definition: ffprobe.c:67
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:348
@ AV_OPT_TYPE_DURATION
Definition: opt.h:239
@ AV_OPT_TYPE_INT
Definition: opt.h:225
@ AV_OPT_TYPE_DICT
Definition: opt.h:232
@ AV_OPT_TYPE_BOOL
Definition: opt.h:242
@ AV_OPT_TYPE_STRING
Definition: opt.h:229
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:634
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:410
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet.
Definition: avpacket.c:641
void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
Convert valid timing fields (timestamps / durations) in a packet from one timebase to another.
Definition: avpacket.c:737
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:4436
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4508
av_warn_unused_result int avformat_write_header(AVFormatContext *s, AVDictionary **options)
Allocate the stream private data and write the stream header to an output media file.
Definition: mux.c:506
ff_const59 AVOutputFormat * av_guess_format(const char *short_name, const char *filename, const char *mime_type)
Return the output format in the list of registered output formats which best matches the provided par...
Definition: format.c:76
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition: mux.c:1281
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
Definition: mux.c:1219
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition: dict.c:199
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key,...
Definition: dict.h:70
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:213
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:40
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition: error.h:56
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define AVERROR_MUXER_NOT_FOUND
Muxer not found.
Definition: error.h:60
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:119
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:210
#define AV_LOG_INFO
Standard information.
Definition: log.h:205
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
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
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:260
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
int i
Definition: input.c:407
static av_cold int init(AVFilterContext *ctx)
Definition: fifo.c:50
static int fifo_thread_write_header(FifoThreadContext *ctx)
Definition: fifo.c:122
static const AVClass fifo_muxer_class
Definition: fifo.c:706
FifoMessageType
Definition: fifo.c:110
@ FIFO_WRITE_HEADER
Definition: fifo.c:112
@ FIFO_NOOP
Definition: fifo.c:111
@ FIFO_FLUSH_OUTPUT
Definition: fifo.c:114
@ FIFO_WRITE_PACKET
Definition: fifo.c:113
static int fifo_init(AVFormatContext *avf)
Definition: fifo.c:521
static const AVOption options[]
Definition: fifo.c:669
static int fifo_write_header(AVFormatContext *avf)
Definition: fifo.c:560
static int fifo_thread_process_recovery_failure(FifoThreadContext *ctx, AVPacket *pkt, int err_no)
Definition: fifo.c:295
static int fifo_thread_flush_output(FifoThreadContext *ctx)
Definition: fifo.c:164
#define FIFO_DEFAULT_MAX_RECOVERY_ATTEMPTS
Definition: fifo.c:33
static int fifo_thread_recover(FifoThreadContext *ctx, FifoMessage *msg, int err_no)
Definition: fifo.c:398
AVOutputFormat ff_fifo_muxer
Definition: fifo.c:713
static int fifo_write_packet(AVFormatContext *avf, AVPacket *pkt)
Definition: fifo.c:575
static int fifo_thread_attempt_recovery(FifoThreadContext *ctx, FifoMessage *msg, int err_no)
Definition: fifo.c:327
#define FIFO_DEFAULT_QUEUE_SIZE
Definition: fifo.c:32
static void * fifo_consumer_thread(void *data)
Definition: fifo.c:424
static int fifo_mux_init(AVFormatContext *avf, ff_const59 AVOutputFormat *oformat, const char *filename)
Definition: fifo.c:485
static int fifo_thread_write_packet(FifoThreadContext *ctx, AVPacket *pkt)
Definition: fifo.c:182
static int is_recoverable(const FifoContext *fifo, int err_no)
Definition: fifo.c:268
#define OFFSET(x)
Definition: fifo.c:668
static void fifo_deinit(AVFormatContext *avf)
Definition: fifo.c:658
static int fifo_write_trailer(AVFormatContext *avf)
Definition: fifo.c:619
static void free_message(void *msg)
Definition: fifo.c:287
static int64_t next_duration(AVFormatContext *avf, AVPacket *pkt, int64_t *last_dts)
Definition: fifo.c:173
static int fifo_thread_write_trailer(FifoThreadContext *ctx)
Definition: fifo.c:225
static int fifo_thread_dispatch_message(FifoThreadContext *ctx, FifoMessage *msg)
Definition: fifo.c:241
#define FIFO_DEFAULT_RECOVERY_WAIT_TIME_USEC
Definition: fifo.c:34
int ff_format_output_open(AVFormatContext *s, const char *url, AVDictionary **options)
Utility function to open IO stream of output format.
Definition: utils.c:5681
int ff_stream_encode_params_copy(AVStream *dst, const AVStream *src)
Copy encoding parameters from source to destination stream.
Definition: utils.c:4316
void ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
Definition: utils.c:5691
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
const char data[16]
Definition: mxf.c:142
AVOptions.
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:278
static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
Definition: os2threads.h:104
static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
Definition: os2threads.h:94
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
_fmutex pthread_mutex_t
Definition: os2threads.h:53
static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
Definition: os2threads.h:112
#define tb
Definition: regdef.h:68
Describe the class of an AVClass context structure.
Definition: log.h:67
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
char * key
Definition: dict.h:82
Format I/O context.
Definition: avformat.h:1232
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1288
AVIOContext * pb
I/O context.
Definition: avformat.h:1274
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1474
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1363
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition: avformat.h:1512
char * url
input or output URL.
Definition: avformat.h:1328
void * opaque
User data.
Definition: avformat.h:1752
void(* io_close)(struct AVFormatContext *s, AVIOContext *pb)
A callback for closing the streams opened with AVFormatContext.io_open().
Definition: avformat.h:1834
void * priv_data
Format private data.
Definition: avformat.h:1260
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1300
int(* io_open)(struct AVFormatContext *s, AVIOContext **pb, const char *url, int flags, AVDictionary **options)
A callback for opening new IO streams.
Definition: avformat.h:1828
AVOption.
Definition: opt.h:248
const char * name
Definition: avformat.h:491
This structure stores compressed data.
Definition: packet.h:346
int stream_index
Definition: packet.h:371
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:375
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:387
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:362
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:368
Rational number (pair of numerator and denominator).
Definition: rational.h:58
Stream structure.
Definition: avformat.h:873
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:902
int64_t cur_dts
Definition: avformat.h:1066
volatile uint8_t overflow_flag
Definition: fifo.c:80
int64_t timeshift
Definition: fifo.c:84
int overflow_flag_lock_initialized
Definition: fifo.c:78
atomic_int_least64_t queue_duration
Definition: fifo.c:82
int64_t last_sent_dts
Definition: fifo.c:83
int recover_any_error
Definition: fifo.c:68
int recovery_wait_streamtime
Definition: fifo.c:64
int write_trailer_ret
Definition: fifo.c:49
pthread_mutex_t overflow_flag_lock
Definition: fifo.c:77
AVFormatContext * avf
Definition: fifo.c:38
int drop_pkts_on_overflow
Definition: fifo.c:71
int queue_size
Definition: fifo.c:43
pthread_t writer_thread
Definition: fifo.c:46
int attempt_recovery
Definition: fifo.c:60
char * format
Definition: fifo.c:40
int max_recovery_attempts
Definition: fifo.c:57
int restart_with_keyframe
Definition: fifo.c:75
int64_t recovery_wait_time
Definition: fifo.c:54
AVThreadMessageQueue * queue
Definition: fifo.c:44
AVDictionary * format_options
Definition: fifo.c:41
FifoMessageType type
Definition: fifo.c:118
AVPacket pkt
Definition: fifo.c:119
int64_t last_received_dts
Definition: fifo.c:107
uint8_t header_written
Definition: fifo.c:105
uint8_t drop_until_keyframe
Definition: fifo.c:100
int recovery_nr
Definition: fifo.c:97
int64_t last_recovery_ts
Definition: fifo.c:93
AVFormatContext * avf
Definition: fifo.c:88
#define av_log(a,...)
int64_t duration
Definition: movenc.c:64
AVPacket * pkt
Definition: movenc.c:59
AVFormatContext * ctx
Definition: movenc.c:48
void av_thread_message_queue_set_err_send(AVThreadMessageQueue *mq, int err)
Set the sending error code.
int av_thread_message_queue_recv(AVThreadMessageQueue *mq, void *msg, unsigned flags)
Receive a message from the queue.
int av_thread_message_queue_alloc(AVThreadMessageQueue **mq, unsigned nelem, unsigned elsize)
Allocate a new message queue.
Definition: threadmessage.c:40
void av_thread_message_queue_set_free_func(AVThreadMessageQueue *mq, void(*free_func)(void *msg))
Set the optional free message callback function which will be called if an operation is removing mess...
Definition: threadmessage.c:83
void av_thread_message_queue_set_err_recv(AVThreadMessageQueue *mq, int err)
Set the receiving error code.
int av_thread_message_queue_send(AVThreadMessageQueue *mq, void *msg, unsigned flags)
Send a message on the queue.
void av_thread_message_flush(AVThreadMessageQueue *mq)
Flush the message queue.
void av_thread_message_queue_free(AVThreadMessageQueue **mq)
Free a message queue.
Definition: threadmessage.c:91
@ AV_THREAD_MESSAGE_NONBLOCK
Perform non-blocking operation.
Definition: threadmessage.h:31
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition: time.c:84
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
Definition: time.c:56
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:98