FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "libavutil/opt.h"
23 #include "libavutil/time.h"
24 #include "libavutil/thread.h"
26 #include "avformat.h"
27 #include "internal.h"
28 
29 #define FIFO_DEFAULT_QUEUE_SIZE 60
30 #define FIFO_DEFAULT_MAX_RECOVERY_ATTEMPTS 0
31 #define FIFO_DEFAULT_RECOVERY_WAIT_TIME_USEC 5000000 // 5 seconds
32 
33 typedef struct FifoContext {
34  const AVClass *class;
36 
37  char *format;
40 
43 
45 
46  /* Return value of last write_trailer_call */
48 
49  /* Time to wait before next recovery attempt
50  * This can refer to the time in processed stream,
51  * or real time. */
53 
54  /* Maximal number of unsuccessful successive recovery attempts */
56 
57  /* Whether to attempt recovery from failure */
59 
60  /* If >0 stream time will be used when waiting
61  * for the recovery attempt instead of real time */
63 
64  /* If >0 recovery will be attempted regardless of error code
65  * (except AVERROR_EXIT, so exit request is never ignored) */
67 
68  /* Whether to drop packets in case the queue is full. */
70 
71  /* Whether to wait for keyframe when recovering
72  * from failure or queue overflow */
74 
76  /* Value > 0 signals queue overflow */
78 
79 } FifoContext;
80 
81 typedef struct FifoThreadContext {
83 
84  /* Timestamp of last failure.
85  * This is either pts in case stream time is used,
86  * or microseconds as returned by av_getttime_relative() */
88 
89  /* Number of current recovery process
90  * Value > 0 means we are in recovery process */
92 
93  /* If > 0 all frames will be dropped until keyframe is received */
95 
96  /* Value > 0 means that the previous write_header call was successful
97  * so finalization by calling write_trailer and ff_io_close must be done
98  * before exiting / reinitialization of underlying muxer */
101 
102 typedef enum FifoMessageType {
107 
108 typedef struct FifoMessage {
111 } FifoMessage;
112 
114 {
115  AVFormatContext *avf = ctx->avf;
116  FifoContext *fifo = avf->priv_data;
117  AVFormatContext *avf2 = fifo->avf;
118  AVDictionary *format_options = NULL;
119  int ret, i;
120 
121  ret = av_dict_copy(&format_options, fifo->format_options, 0);
122  if (ret < 0)
123  return ret;
124 
125  ret = ff_format_output_open(avf2, avf->filename, &format_options);
126  if (ret < 0) {
127  av_log(avf, AV_LOG_ERROR, "Error opening %s: %s\n", avf->filename,
128  av_err2str(ret));
129  goto end;
130  }
131 
132  for (i = 0;i < avf2->nb_streams; i++)
133  avf2->streams[i]->cur_dts = 0;
134 
135  ret = avformat_write_header(avf2, &format_options);
136  if (!ret)
137  ctx->header_written = 1;
138 
139  // Check for options unrecognized by underlying muxer
140  if (format_options) {
141  AVDictionaryEntry *entry = NULL;
142  while ((entry = av_dict_get(format_options, "", entry, AV_DICT_IGNORE_SUFFIX)))
143  av_log(avf2, AV_LOG_ERROR, "Unknown option '%s'\n", entry->key);
144  ret = AVERROR(EINVAL);
145  }
146 
147 end:
148  av_dict_free(&format_options);
149  return ret;
150 }
151 
153 {
154  AVFormatContext *avf = ctx->avf;
155  FifoContext *fifo = avf->priv_data;
156  AVFormatContext *avf2 = fifo->avf;
157 
158  return av_write_frame(avf2, NULL);
159 }
160 
162 {
163  AVFormatContext *avf = ctx->avf;
164  FifoContext *fifo = avf->priv_data;
165  AVFormatContext *avf2 = fifo->avf;
166  AVRational src_tb, dst_tb;
167  int ret, s_idx;
168 
169  if (ctx->drop_until_keyframe) {
170  if (pkt->flags & AV_PKT_FLAG_KEY) {
171  ctx->drop_until_keyframe = 0;
172  av_log(avf, AV_LOG_VERBOSE, "Keyframe received, recovering...\n");
173  } else {
174  av_log(avf, AV_LOG_VERBOSE, "Dropping non-keyframe packet\n");
175  av_packet_unref(pkt);
176  return 0;
177  }
178  }
179 
180  s_idx = pkt->stream_index;
181  src_tb = avf->streams[s_idx]->time_base;
182  dst_tb = avf2->streams[s_idx]->time_base;
183  av_packet_rescale_ts(pkt, src_tb, dst_tb);
184 
185  ret = av_write_frame(avf2, pkt);
186  if (ret >= 0)
187  av_packet_unref(pkt);
188  return ret;
189 }
190 
192 {
193  AVFormatContext *avf = ctx->avf;
194  FifoContext *fifo = avf->priv_data;
195  AVFormatContext *avf2 = fifo->avf;
196  int ret;
197 
198  if (!ctx->header_written)
199  return 0;
200 
201  ret = av_write_trailer(avf2);
202  ff_format_io_close(avf2, &avf2->pb);
203 
204  return ret;
205 }
206 
208 {
209  int ret;
210 
211  if (!ctx->header_written) {
212  ret = fifo_thread_write_header(ctx);
213  if (ret < 0)
214  return ret;
215  }
216 
217  switch(msg->type) {
218  case FIFO_WRITE_HEADER:
219  return ret;
220  case FIFO_WRITE_PACKET:
221  return fifo_thread_write_packet(ctx, &msg->pkt);
222  case FIFO_FLUSH_OUTPUT:
223  return fifo_thread_flush_output(ctx);
224  }
225 
226  return AVERROR(EINVAL);
227 }
228 
229 static int is_recoverable(const FifoContext *fifo, int err_no) {
230  if (!fifo->attempt_recovery)
231  return 0;
232 
233  if (fifo->recover_any_error)
234  return err_no != AVERROR_EXIT;
235 
236  switch (err_no) {
237  case AVERROR(EINVAL):
238  case AVERROR(ENOSYS):
239  case AVERROR_EOF:
240  case AVERROR_EXIT:
242  return 0;
243  default:
244  return 1;
245  }
246 }
247 
248 static void free_message(void *msg)
249 {
250  FifoMessage *fifo_msg = msg;
251 
252  if (fifo_msg->type == FIFO_WRITE_PACKET)
253  av_packet_unref(&fifo_msg->pkt);
254 }
255 
257  int err_no)
258 {
259  AVFormatContext *avf = ctx->avf;
260  FifoContext *fifo = avf->priv_data;
261  int ret;
262 
263  av_log(avf, AV_LOG_INFO, "Recovery failed: %s\n",
264  av_err2str(err_no));
265 
266  if (fifo->recovery_wait_streamtime) {
267  if (pkt->pts == AV_NOPTS_VALUE)
268  av_log(avf, AV_LOG_WARNING, "Packet does not contain presentation"
269  " timestamp, recovery will be attempted immediately");
270  ctx->last_recovery_ts = pkt->pts;
271  } else {
273  }
274 
275  if (fifo->max_recovery_attempts &&
276  ctx->recovery_nr >= fifo->max_recovery_attempts) {
277  av_log(avf, AV_LOG_ERROR,
278  "Maximal number of %d recovery attempts reached.\n",
279  fifo->max_recovery_attempts);
280  ret = err_no;
281  } else {
282  ret = AVERROR(EAGAIN);
283  }
284 
285  return ret;
286 }
287 
289 {
290  AVFormatContext *avf = ctx->avf;
291  FifoContext *fifo = avf->priv_data;
292  AVPacket *pkt = &msg->pkt;
293  int64_t time_since_recovery;
294  int ret;
295 
296  if (!is_recoverable(fifo, err_no)) {
297  ret = err_no;
298  goto fail;
299  }
300 
301  if (ctx->header_written) {
303  ctx->header_written = 0;
304  }
305 
306  if (!ctx->recovery_nr) {
308  AV_NOPTS_VALUE : 0;
309  } else {
310  if (fifo->recovery_wait_streamtime) {
311  if (ctx->last_recovery_ts == AV_NOPTS_VALUE) {
313  time_since_recovery = av_rescale_q(pkt->pts - ctx->last_recovery_ts,
314  tb, AV_TIME_BASE_Q);
315  } else {
316  /* Enforce recovery immediately */
317  time_since_recovery = fifo->recovery_wait_time;
318  }
319  } else {
320  time_since_recovery = av_gettime_relative() - ctx->last_recovery_ts;
321  }
322 
323  if (time_since_recovery < fifo->recovery_wait_time)
324  return AVERROR(EAGAIN);
325  }
326 
327  ctx->recovery_nr++;
328 
329  if (fifo->max_recovery_attempts) {
330  av_log(avf, AV_LOG_VERBOSE, "Recovery attempt #%d/%d\n",
331  ctx->recovery_nr, fifo->max_recovery_attempts);
332  } else {
333  av_log(avf, AV_LOG_VERBOSE, "Recovery attempt #%d\n",
334  ctx->recovery_nr);
335  }
336 
337  if (fifo->restart_with_keyframe && fifo->drop_pkts_on_overflow)
338  ctx->drop_until_keyframe = 1;
339 
340  ret = fifo_thread_dispatch_message(ctx, msg);
341  if (ret < 0) {
342  if (is_recoverable(fifo, ret)) {
343  return fifo_thread_process_recovery_failure(ctx, pkt, ret);
344  } else {
345  goto fail;
346  }
347  } else {
348  av_log(avf, AV_LOG_INFO, "Recovery successful\n");
349  ctx->recovery_nr = 0;
350  }
351 
352  return 0;
353 
354 fail:
355  free_message(msg);
356  return ret;
357 }
358 
359 static int fifo_thread_recover(FifoThreadContext *ctx, FifoMessage *msg, int err_no)
360 {
361  AVFormatContext *avf = ctx->avf;
362  FifoContext *fifo = avf->priv_data;
363  int ret;
364 
365  do {
366  if (!fifo->recovery_wait_streamtime && ctx->recovery_nr > 0) {
367  int64_t time_since_recovery = av_gettime_relative() - ctx->last_recovery_ts;
368  int64_t time_to_wait = FFMAX(0, fifo->recovery_wait_time - time_since_recovery);
369  if (time_to_wait)
370  av_usleep(FFMIN(10000, time_to_wait));
371  }
372 
373  ret = fifo_thread_attempt_recovery(ctx, msg, err_no);
374  } while (ret == AVERROR(EAGAIN) && !fifo->drop_pkts_on_overflow);
375 
376  if (ret == AVERROR(EAGAIN) && fifo->drop_pkts_on_overflow) {
377  if (msg->type == FIFO_WRITE_PACKET)
378  av_packet_unref(&msg->pkt);
379  ret = 0;
380  }
381 
382  return ret;
383 }
384 
385 static void *fifo_consumer_thread(void *data)
386 {
387  AVFormatContext *avf = data;
388  FifoContext *fifo = avf->priv_data;
389  AVThreadMessageQueue *queue = fifo->queue;
390  FifoMessage msg = {FIFO_WRITE_HEADER, {0}};
391  int ret;
392 
393  FifoThreadContext fifo_thread_ctx;
394  memset(&fifo_thread_ctx, 0, sizeof(FifoThreadContext));
395  fifo_thread_ctx.avf = avf;
396 
397  while (1) {
398  uint8_t just_flushed = 0;
399 
400  if (!fifo_thread_ctx.recovery_nr)
401  ret = fifo_thread_dispatch_message(&fifo_thread_ctx, &msg);
402 
403  if (ret < 0 || fifo_thread_ctx.recovery_nr > 0) {
404  int rec_ret = fifo_thread_recover(&fifo_thread_ctx, &msg, ret);
405  if (rec_ret < 0) {
406  av_thread_message_queue_set_err_send(queue, rec_ret);
407  break;
408  }
409  }
410 
411  /* If the queue is full at the moment when fifo_write_packet
412  * attempts to insert new message (packet) to the queue,
413  * it sets the fifo->overflow_flag to 1 and drops packet.
414  * Here in consumer thread, the flag is checked and if it is
415  * set, the queue is flushed and flag cleared. */
417  if (fifo->overflow_flag) {
419  if (fifo->restart_with_keyframe)
420  fifo_thread_ctx.drop_until_keyframe = 1;
421  fifo->overflow_flag = 0;
422  just_flushed = 1;
423  }
425 
426  if (just_flushed)
427  av_log(avf, AV_LOG_INFO, "FIFO queue flushed\n");
428 
429  ret = av_thread_message_queue_recv(queue, &msg, 0);
430  if (ret < 0) {
432  break;
433  }
434  }
435 
436  fifo->write_trailer_ret = fifo_thread_write_trailer(&fifo_thread_ctx);
437 
438  return NULL;
439 }
440 
441 static int fifo_mux_init(AVFormatContext *avf, AVOutputFormat *oformat)
442 {
443  FifoContext *fifo = avf->priv_data;
444  AVFormatContext *avf2;
445  int ret = 0, i;
446 
447  ret = avformat_alloc_output_context2(&avf2, oformat, NULL, NULL);
448  if (ret < 0)
449  return ret;
450 
451  fifo->avf = avf2;
452 
454  avf2->max_delay = avf->max_delay;
455  ret = av_dict_copy(&avf2->metadata, avf->metadata, 0);
456  if (ret < 0)
457  return ret;
458  avf2->opaque = avf->opaque;
459  avf2->io_close = avf->io_close;
460  avf2->io_open = avf->io_open;
461  avf2->flags = avf->flags;
462 
463  for (i = 0; i < avf->nb_streams; ++i) {
464  AVStream *st = avformat_new_stream(avf2, NULL);
465  if (!st)
466  return AVERROR(ENOMEM);
467 
468  ret = ff_stream_encode_params_copy(st, avf->streams[i]);
469  if (ret < 0)
470  return ret;
471  }
472 
473  return 0;
474 }
475 
476 static int fifo_init(AVFormatContext *avf)
477 {
478  FifoContext *fifo = avf->priv_data;
479  AVOutputFormat *oformat;
480  int ret = 0;
481 
482  if (fifo->recovery_wait_streamtime && !fifo->drop_pkts_on_overflow) {
483  av_log(avf, AV_LOG_ERROR, "recovery_wait_streamtime can be turned on"
484  " only when drop_pkts_on_overflow is also turned on\n");
485  return AVERROR(EINVAL);
486  }
487 
488  if (fifo->format_options_str) {
490  "=", ":", 0);
491  if (ret < 0) {
492  av_log(avf, AV_LOG_ERROR, "Could not parse format options list '%s'\n",
493  fifo->format_options_str);
494  return ret;
495  }
496  }
497 
498  oformat = av_guess_format(fifo->format, avf->filename, NULL);
499  if (!oformat) {
501  return ret;
502  }
503 
504  ret = fifo_mux_init(avf, oformat);
505  if (ret < 0)
506  return ret;
507 
508  ret = av_thread_message_queue_alloc(&fifo->queue, (unsigned) fifo->queue_size,
509  sizeof(FifoMessage));
510  if (ret < 0)
511  return ret;
512 
514 
516  if (ret < 0)
517  return AVERROR(ret);
518 
519  return 0;
520 }
521 
523 {
524  FifoContext * fifo = avf->priv_data;
525  int ret;
526 
528  if (ret) {
529  av_log(avf, AV_LOG_ERROR, "Failed to start thread: %s\n",
530  av_err2str(AVERROR(ret)));
531  ret = AVERROR(ret);
532  }
533 
534  return ret;
535 }
536 
538 {
539  FifoContext *fifo = avf->priv_data;
541  int ret;
542 
543  if (pkt) {
544  av_init_packet(&msg.pkt);
545  ret = av_packet_ref(&msg.pkt,pkt);
546  if (ret < 0)
547  return ret;
548  }
549 
550  ret = av_thread_message_queue_send(fifo->queue, &msg,
551  fifo->drop_pkts_on_overflow ?
553  if (ret == AVERROR(EAGAIN)) {
554  uint8_t overflow_set = 0;
555 
556  /* Queue is full, set fifo->overflow_flag to 1
557  * to let consumer thread know the queue should
558  * be flushed. */
560  if (!fifo->overflow_flag)
561  fifo->overflow_flag = overflow_set = 1;
563 
564  if (overflow_set)
565  av_log(avf, AV_LOG_WARNING, "FIFO queue full\n");
566  ret = 0;
567  goto fail;
568  } else if (ret < 0) {
569  goto fail;
570  }
571 
572  return ret;
573 fail:
574  if (pkt)
575  av_packet_unref(&msg.pkt);
576  return ret;
577 }
578 
580 {
581  FifoContext *fifo= avf->priv_data;
582  int ret;
583 
585 
586  ret = pthread_join(fifo->writer_thread, NULL);
587  if (ret < 0) {
588  av_log(avf, AV_LOG_ERROR, "pthread join error: %s\n",
589  av_err2str(AVERROR(ret)));
590  return AVERROR(ret);
591  }
592 
593  ret = fifo->write_trailer_ret;
594  return ret;
595 }
596 
597 static void fifo_deinit(AVFormatContext *avf)
598 {
599  FifoContext *fifo = avf->priv_data;
600 
602  avformat_free_context(fifo->avf);
605 }
606 
607 #define OFFSET(x) offsetof(FifoContext, x)
608 static const AVOption options[] = {
609  {"fifo_format", "Target muxer", OFFSET(format),
611 
612  {"queue_size", "Size of fifo queue", OFFSET(queue_size),
614 
615  {"format_opts", "Options to be passed to underlying muxer", OFFSET(format_options_str),
617 
618  {"drop_pkts_on_overflow", "Drop packets on fifo queue overflow not to block encoder", OFFSET(drop_pkts_on_overflow),
619  AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
620 
621  {"restart_with_keyframe", "Wait for keyframe when restarting output", OFFSET(restart_with_keyframe),
622  AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
623 
624  {"attempt_recovery", "Attempt recovery in case of failure", OFFSET(attempt_recovery),
625  AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
626 
627  {"max_recovery_attempts", "Maximal number of recovery attempts", OFFSET(max_recovery_attempts),
629 
630  {"recovery_wait_time", "Waiting time between recovery attempts", OFFSET(recovery_wait_time),
632 
633  {"recovery_wait_streamtime", "Use stream time instead of real time while waiting for recovery",
634  OFFSET(recovery_wait_streamtime), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
635 
636  {"recover_any_error", "Attempt recovery regardless of type of the error", OFFSET(recover_any_error),
637  AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
638 
639  {NULL},
640 };
641 
642 static const AVClass fifo_muxer_class = {
643  .class_name = "Fifo muxer",
644  .item_name = av_default_item_name,
645  .option = options,
646  .version = LIBAVUTIL_VERSION_INT,
647 };
648 
650  .name = "fifo",
651  .long_name = NULL_IF_CONFIG_SMALL("FIFO queue pseudo-muxer"),
652  .priv_data_size = sizeof(FifoContext),
653  .init = fifo_init,
657  .deinit = fifo_deinit,
658  .priv_class = &fifo_muxer_class,
660 };
#define NULL
Definition: coverity.c:32
AVFormatContext * avf
Definition: fifo.c:82
static int fifo_mux_init(AVFormatContext *avf, AVOutputFormat *oformat)
Definition: fifo.c:441
static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
Definition: os2threads.h:106
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition: avformat.h:1592
AVOption.
Definition: opt.h:245
void av_thread_message_queue_set_err_recv(AVThreadMessageQueue *mq, int err)
Set the receiving error code.
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
Definition: mux.c:919
static av_cold int init(AVFilterContext *ctx)
Definition: fifo.c:54
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
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
static const AVOption options[]
Definition: fifo.c:608
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:217
uint8_t drop_until_keyframe
Definition: fifo.c:94
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition: time.c:76
static AVPacket pkt
static void free_message(void *msg)
Definition: fifo.c:248
static const AVClass fifo_muxer_class
Definition: fifo.c:642
#define AVFMT_ALLOW_FLUSH
Format allows flushing.
Definition: avformat.h:495
pthread_mutex_t overflow_flag_lock
Definition: fifo.c:75
char * format
Definition: fifo.c:37
uint8_t header_written
Definition: fifo.c:99
Format I/O context.
Definition: avformat.h:1338
int64_t cur_dts
Definition: avformat.h:1061
AVDictionary * format_options
Definition: fifo.c:39
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
HMTX pthread_mutex_t
Definition: os2threads.h:49
AVOutputFormat ff_fifo_muxer
Definition: fifo.c:649
uint8_t
static void fifo_deinit(AVFormatContext *avf)
Definition: fifo.c:597
int max_recovery_attempts
Definition: fifo.c:55
int av_thread_message_queue_recv(AVThreadMessageQueue *mq, void *msg, unsigned flags)
Receive a message from the queue.
AVFormatContext * avf
Definition: fifo.c:35
AVOptions.
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
int drop_pkts_on_overflow
Definition: fifo.c:69
int av_thread_message_queue_send(AVThreadMessageQueue *mq, void *msg, unsigned flags)
Send a message on the queue.
void ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
Definition: utils.c:5255
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4193
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1406
int64_t last_recovery_ts
Definition: fifo.c:87
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
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1449
int ff_format_output_open(AVFormatContext *s, const char *url, AVDictionary **options)
Utility function to open IO stream of output format.
Definition: utils.c:5245
int ff_stream_encode_params_copy(AVStream *dst, const AVStream *src)
Copy encoding parameters from source to destination stream.
Definition: utils.c:4016
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
FifoMessageType
Definition: fifo.c:102
#define av_log(a,...)
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:275
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet.
Definition: avpacket.c:576
int recovery_wait_streamtime
Definition: fifo.c:62
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1633
static int fifo_thread_flush_output(FifoThreadContext *ctx)
Definition: fifo.c:152
static int fifo_thread_write_trailer(FifoThreadContext *ctx)
Definition: fifo.c:191
int avformat_alloc_output_context2(AVFormatContext **ctx, AVOutputFormat *oformat, const char *format_name, const char *filename)
Allocate an AVFormatContext for an output format.
Definition: mux.c:148
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
static int fifo_init(AVFormatContext *avf)
Definition: fifo.c:476
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1554
void av_packet_rescale_ts(AVPacket *pkt, AVRational tb_src, AVRational tb_dst)
Convert valid timing fields (timestamps / durations) in a packet from one timebase to another...
Definition: avpacket.c:630
static int fifo_thread_write_header(FifoThreadContext *ctx)
Definition: fifo.c:113
void av_thread_message_flush(AVThreadMessageQueue *mq)
Flush the message queue.
static int fifo_write_header(AVFormatContext *avf)
Definition: fifo.c:522
FifoMessageType type
Definition: fifo.c:109
av_default_item_name
int64_t recovery_wait_time
Definition: fifo.c:52
#define AVERROR(e)
Definition: error.h:43
pthread_t writer_thread
Definition: fifo.c:44
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
volatile uint8_t overflow_flag
Definition: fifo.c:77
#define OFFSET(x)
Definition: fifo.c:607
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:203
#define FIFO_DEFAULT_MAX_RECOVERY_ATTEMPTS
Definition: fifo.c:30
#define FFMAX(a, b)
Definition: common.h:94
#define fail()
Definition: checkasm.h:83
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1607
void * opaque
User data.
Definition: avformat.h:1820
AVPacket pkt
Definition: fifo.c:110
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1394
int queue_size
Definition: fifo.c:41
char filename[1024]
input or output filename
Definition: avformat.h:1414
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:527
#define FFMIN(a, b)
Definition: common.h:96
int recovery_nr
Definition: fifo.c:91
static int fifo_thread_write_packet(FifoThreadContext *ctx, AVPacket *pkt)
Definition: fifo.c:161
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
const char * name
Definition: avformat.h:524
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:119
AVFormatContext * ctx
Definition: movenc.c:48
static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
Definition: os2threads.h:88
static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
Definition: os2threads.h:98
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:98
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition: error.h:56
#define FIFO_DEFAULT_QUEUE_SIZE
Definition: fifo.c:29
int restart_with_keyframe
Definition: fifo.c:73
Stream structure.
Definition: avformat.h:889
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
int av_dict_parse_string(AVDictionary **pm, const char *str, const char *key_val_sep, const char *pairs_sep, int flags)
Parse the key/value pairs list and add the parsed entries to a dictionary.
Definition: dict.c:180
static av_always_inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
Definition: os2threads.h:74
static int fifo_write_packet(AVFormatContext *avf, AVPacket *pkt)
Definition: fifo.c:537
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
void av_thread_message_queue_set_err_send(AVThreadMessageQueue *mq, int err)
Set the sending error code.
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:254
AVIOContext * pb
I/O context.
Definition: avformat.h:1380
static int fifo_write_trailer(AVFormatContext *avf)
Definition: fifo.c:579
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:567
Perform non-blocking operation.
Definition: threadmessage.h:31
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost)
Definition: ffmpeg.c:645
static const char * format
Definition: movenc.c:47
Describe the class of an AVClass context structure.
Definition: log.h:67
static int fifo_thread_process_recovery_failure(FifoThreadContext *ctx, AVPacket *pkt, int err_no)
Definition: fifo.c:256
Rational number (pair of numerator and denominator).
Definition: rational.h:58
int av_thread_message_queue_alloc(AVThreadMessageQueue **mq, unsigned nelem, unsigned elsize)
Allocate a new message queue.
Definition: threadmessage.c:40
AVThreadMessageQueue * queue
Definition: fifo.c:42
int write_trailer_ret
Definition: fifo.c:47
static int fifo_thread_attempt_recovery(FifoThreadContext *ctx, FifoMessage *msg, int err_no)
Definition: fifo.c:288
static int fifo_thread_dispatch_message(FifoThreadContext *ctx, FifoMessage *msg)
Definition: fifo.c:207
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:4129
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
Definition: time.c:56
void av_thread_message_queue_free(AVThreadMessageQueue **mq)
Free a message queue.
Definition: threadmessage.c:91
static int flags
Definition: cpu.c:47
#define FIFO_DEFAULT_RECOVERY_WAIT_TIME_USEC
Definition: fifo.c:31
Main libavformat public API header.
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:478
char * format_options_str
Definition: fifo.c:38
int attempt_recovery
Definition: fifo.c:58
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:33
char * key
Definition: dict.h:86
static av_always_inline int pthread_mutex_unlock(pthread_mutex_t *mutex)
Definition: os2threads.h:120
void * priv_data
Format private data.
Definition: avformat.h:1366
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:344
static void * fifo_consumer_thread(void *data)
Definition: fifo.c:385
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition: mux.c:1287
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key, ignoring the suffix of the found key string.
Definition: dict.h:70
static int fifo_thread_recover(FifoThreadContext *ctx, FifoMessage *msg, int err_no)
Definition: fifo.c:359
#define AVERROR_MUXER_NOT_FOUND
Muxer not found.
Definition: error.h:60
static av_always_inline int pthread_mutex_lock(pthread_mutex_t *mutex)
Definition: os2threads.h:113
int stream_index
Definition: avcodec.h:1603
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:926
static int is_recoverable(const FifoContext *fifo, int err_no)
Definition: fifo.c:229
#define AVFMT_TS_NEGATIVE
Format allows muxing negative timestamps.
Definition: avformat.h:501
int(* io_open)(struct AVFormatContext *s, AVIOContext **pb, const char *url, int flags, AVDictionary **options)
Definition: avformat.h:1898
This structure stores compressed data.
Definition: avcodec.h:1578
void(* io_close)(struct AVFormatContext *s, AVIOContext *pb)
A callback for closing the streams opened with AVFormatContext.io_open().
Definition: avformat.h:1904
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1594
int recover_any_error
Definition: fifo.c:66
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:242
#define tb
Definition: regdef.h:68