static int nvenc_setup_surfaces(AVCodecContext *avctx) { NVENCContext *ctx = avctx->priv_data; int i, ret; ctx->nb_surfaces = FFMAX(4 + avctx->max_b_frames, ctx->nb_surfaces); ctx->in = av_mallocz(ctx->nb_surfaces * sizeof(*ctx->in)); if (!ctx->in) return AVERROR(ENOMEM); ctx->out = av_mallocz(ctx->nb_surfaces * sizeof(*ctx->out)); if (!ctx->out) return AVERROR(ENOMEM); ctx->timestamps = av_fifo_alloc(ctx->nb_surfaces * sizeof(int64_t)); if (!ctx->timestamps) return AVERROR(ENOMEM); ctx->pending = av_fifo_alloc(ctx->nb_surfaces * sizeof(ctx->out)); if (!ctx->pending) return AVERROR(ENOMEM); ctx->ready = av_fifo_alloc(ctx->nb_surfaces * sizeof(ctx->out)); if (!ctx->ready) return AVERROR(ENOMEM); for (i = 0; i < ctx->nb_surfaces; i++) { if ((ret = nvenc_alloc_surface(avctx, i)) < 0) return ret; } return 0; }
static int start_jack(AVFormatContext *context, AVFormatParameters *params) { JackData *self = context->priv_data; jack_status_t status; int i, test; double o, period; /* Register as a JACK client, using the context filename as client name. */ self->client = jack_client_open(context->filename, JackNullOption, &status); if (!self->client) { av_log(context, AV_LOG_ERROR, "Unable to register as a JACK client\n"); return AVERROR(EIO); } sem_init(&self->packet_count, 0, 0); self->sample_rate = jack_get_sample_rate(self->client); self->nports = params->channels; self->ports = av_malloc(self->nports * sizeof(*self->ports)); self->buffer_size = jack_get_buffer_size(self->client); /* Register JACK ports */ for (i = 0; i < self->nports; i++) { char str[16]; snprintf(str, sizeof(str), "input_%d", i + 1); self->ports[i] = jack_port_register(self->client, str, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0); if (!self->ports[i]) { av_log(context, AV_LOG_ERROR, "Unable to register port %s:%s\n", context->filename, str); jack_client_close(self->client); return AVERROR(EIO); } } /* Register JACK callbacks */ jack_set_process_callback(self->client, process_callback, self); jack_on_shutdown(self->client, shutdown_callback, self); jack_set_xrun_callback(self->client, xrun_callback, self); /* Create time filter */ period = (double) self->buffer_size / self->sample_rate; o = 2 * M_PI * 1.5 * period; /// bandwidth: 1.5Hz self->timefilter = ff_timefilter_new (1.0 / self->sample_rate, sqrt(2 * o), o * o); /* Create FIFO buffers */ self->filled_pkts = av_fifo_alloc(FIFO_PACKETS_NUM * sizeof(AVPacket)); /* New packets FIFO with one extra packet for safety against underruns */ self->new_pkts = av_fifo_alloc((FIFO_PACKETS_NUM + 1) * sizeof(AVPacket)); if ((test = supply_new_packets(self, context))) { jack_client_close(self->client); return test; } return 0; }
int CacheHttp_Open(void ** handle,const char* headers) { CacheHttpContext * s = (CacheHttpContext *)av_malloc(sizeof(CacheHttpContext)); if(!s) { *handle = NULL; return AVERROR(EIO); } *handle = (void *)s; s->hd = NULL; s->item_duration = 0; s->item_pos = 0; s->item_starttime = 0; s->finish_flag = 0; s->seek_flag = 0; s->reset_flag = -1; s->have_list_end = -1; s->is_first_read = 1; memset(s->headers, 0x00, sizeof(s->headers)); s->fifo = NULL; float value=0.0; int config_ret; config_ret=am_getconfig_float("libplayer.ffmpeg.hlshttpbufmax",&value); if(config_ret<0 || value < 1024*32) s->fifo = av_fifo_alloc(CIRCULAR_BUFFER_SIZE); else { s->fifo = av_fifo_alloc(value); } pthread_mutex_init(&s->read_mutex,NULL); s->EXIT = 0; s->EXITED = 0; s->RESET = 0; if(headers) { av_strlcpy(s->headers,headers,BUFFER_SIZE); } int ret = am_getconfig_float("libplayer.hls.measured_buffer",&value); int meause_unit_max = 1024*1024*10;//10M if(ret>=0) { meause_unit_max = (int)value*1024; } if(meause_unit_max/TMP_BUFFER_SIZE<100) { s->bandwidth_measure=bandwidth_measure_alloc(100,0); } else { s->bandwidth_measure=bandwidth_measure_alloc(meause_unit_max/TMP_BUFFER_SIZE,0); } ret = ffmpeg_pthread_create(&s->circular_buffer_thread, NULL, circular_buffer_task, s); av_log(NULL, AV_LOG_INFO, "----------- pthread_create ret=%d\n",ret); return ret; }
static av_cold int init_audio(AVFilterContext *ctx) { BufferSourceContext *s = ctx->priv; int ret = 0; s->sample_fmt = av_get_sample_fmt(s->sample_fmt_str); if (s->sample_fmt == AV_SAMPLE_FMT_NONE) { av_log(ctx, AV_LOG_ERROR, "Invalid sample format %s.\n", s->sample_fmt_str); return AVERROR(EINVAL); } s->channel_layout = av_get_channel_layout(s->channel_layout_str); if (!s->channel_layout) { av_log(ctx, AV_LOG_ERROR, "Invalid channel layout %s.\n", s->channel_layout_str); return AVERROR(EINVAL); } if (!(s->fifo = av_fifo_alloc(sizeof(AVFrame*)))) return AVERROR(ENOMEM); if (!s->time_base.num) s->time_base = (AVRational){1, s->sample_rate}; av_log(ctx, AV_LOG_VERBOSE, "tb:%d/%d samplefmt:%s samplerate: %d " "ch layout:%s\n", s->time_base.num, s->time_base.den, s->sample_fmt_str, s->sample_rate, s->channel_layout_str); return ret; }
int main(void) { /* create a FIFO buffer */ AVFifoBuffer *fifo = av_fifo_alloc(13 * sizeof(int)); int i, j, n; /* fill data */ for (i = 0; av_fifo_space(fifo) >= sizeof(int); i++) av_fifo_generic_write(fifo, &i, sizeof(int), NULL); /* peek at FIFO */ n = av_fifo_size(fifo)/sizeof(int); for (i = -n+1; i < n; i++) { int *v = (int *)av_fifo_peek2(fifo, i*sizeof(int)); printf("%d: %d\n", i, *v); } printf("\n"); /* read data */ for (i = 0; av_fifo_size(fifo) >= sizeof(int); i++) { av_fifo_generic_read(fifo, &j, sizeof(int), NULL); printf("%d ", j); } printf("\n"); av_fifo_free(fifo); return 0; }
void AVCodecEncoder::_Init() { fChunkBuffer = new(std::nothrow) uint8[kDefaultChunkBufferSize]; if (fCodecID > 0) { fCodec = avcodec_find_encoder(fCodecID); TRACE(" found AVCodec for %u: %p\n", fCodecID, fCodec); } memset(&fInputFormat, 0, sizeof(media_format)); fAudioFifo = av_fifo_alloc(0); fDstFrame.data[0] = NULL; fDstFrame.data[1] = NULL; fDstFrame.data[2] = NULL; fDstFrame.data[3] = NULL; fDstFrame.linesize[0] = 0; fDstFrame.linesize[1] = 0; fDstFrame.linesize[2] = 0; fDstFrame.linesize[3] = 0; // Initial parameters, so we know if the user changed them fEncodeParameters.avg_field_size = 0; fEncodeParameters.max_field_size = 0; fEncodeParameters.quality = 1.0f; }
int ff_audio_interleave_init(AVFormatContext *s, const int *samples_per_frame, AVRational time_base) { int i; if (!samples_per_frame) return -1; for (i = 0; i < s->nb_streams; i++) { AVStream *st = s->streams[i]; AudioInterleaveContext *aic = st->priv_data; if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { aic->sample_size = (st->codecpar->channels * av_get_bits_per_sample(st->codecpar->codec_id)) / 8; if (!aic->sample_size) { av_log(s, AV_LOG_ERROR, "could not compute sample size\n"); return -1; } aic->samples_per_frame = samples_per_frame; aic->samples = aic->samples_per_frame; aic->time_base = time_base; aic->fifo_size = 100* *aic->samples; aic->fifo= av_fifo_alloc(100 * *aic->samples); } } return 0; }
int av_thread_message_queue_alloc(AVThreadMessageQueue **mq, unsigned nelem, unsigned elsize) { #if HAVE_THREADS AVThreadMessageQueue *rmq; int ret = 0; if (nelem > INT_MAX / elsize) return AVERROR(EINVAL); if (!(rmq = av_mallocz(sizeof(*rmq)))) return AVERROR(ENOMEM); if ((ret = pthread_mutex_init(&rmq->lock, NULL))) { av_free(rmq); return AVERROR(ret); } if ((ret = pthread_cond_init(&rmq->cond, NULL))) { pthread_mutex_destroy(&rmq->lock); av_free(rmq); return AVERROR(ret); } if (!(rmq->fifo = av_fifo_alloc(elsize * nelem))) { pthread_cond_destroy(&rmq->cond); pthread_mutex_destroy(&rmq->lock); av_free(rmq); return AVERROR(ret); } rmq->elsize = elsize; *mq = rmq; return 0; #else *mq = NULL; return AVERROR(ENOSYS); #endif /* HAVE_THREADS */ }
static av_cold int init_video(AVFilterContext *ctx) { BufferSourceContext *c = ctx->priv; if (!c->pix_fmt_str || !c->w || !c->h || av_q2d(c->time_base) <= 0) { av_log(ctx, AV_LOG_ERROR, "Invalid parameters provided.\n"); return AVERROR(EINVAL); } if ((c->pix_fmt = av_get_pix_fmt(c->pix_fmt_str)) == AV_PIX_FMT_NONE) { char *tail; c->pix_fmt = strtol(c->pix_fmt_str, &tail, 10); if (*tail || c->pix_fmt < 0 || !av_pix_fmt_desc_get(c->pix_fmt)) { av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", c->pix_fmt_str); return AVERROR(EINVAL); } } if (!(c->fifo = av_fifo_alloc(sizeof(AVFrame*)))) return AVERROR(ENOMEM); av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d pixfmt:%s tb:%d/%d sar:%d/%d\n", c->w, c->h, av_get_pix_fmt_name(c->pix_fmt), c->time_base.num, c->time_base.den, c->pixel_aspect.num, c->pixel_aspect.den); return 0; }
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) { BufferSourceContext *c = ctx->priv; char pix_fmt_str[128]; int ret, n = 0; *c->sws_param = 0; if (!args || (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d:%255c", &c->w, &c->h, pix_fmt_str, &c->time_base.num, &c->time_base.den, &c->sample_aspect_ratio.num, &c->sample_aspect_ratio.den, c->sws_param)) < 7) { av_log(ctx, AV_LOG_ERROR, "Expected at least 7 arguments, but only %d found in '%s'\n", n, args); return AVERROR(EINVAL); } if ((ret = ff_parse_pixel_format(&c->pix_fmt, pix_fmt_str, ctx)) < 0) return ret; if (!(c->fifo = av_fifo_alloc(sizeof(AVFilterBufferRef*)))) return AVERROR(ENOMEM); av_log(ctx, AV_LOG_INFO, "w:%d h:%d pixfmt:%s tb:%d/%d sar:%d/%d sws_param:%s\n", c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name, c->time_base.num, c->time_base.den, c->sample_aspect_ratio.num, c->sample_aspect_ratio.den, c->sws_param); return 0; }
static int flac_parse_init(AVCodecParserContext *c) { FLACParseContext *fpc = c->priv_data; /* There will generally be FLAC_MIN_HEADERS buffered in the fifo before it drains. This is allocated early to avoid slow reallocation. */ fpc->fifo_buf = av_fifo_alloc(FLAC_AVG_FRAME_SIZE * (FLAC_MIN_HEADERS + 3)); return 0; }
FFMpegFifo::FFMpegFifo(int _size) { size = _size; fifo = av_fifo_alloc(size); /* mutex = SDL_CreateMutex(); condRead = SDL_CreateCond(); condWrite = SDL_CreateCond(); */ }
static av_cold int init(AVFilterContext *ctx) { FPSContext *s = ctx->priv; if (!(s->fifo = av_fifo_alloc(2*sizeof(AVFrame*)))) return AVERROR(ENOMEM); av_log(ctx, AV_LOG_VERBOSE, "fps=%d/%d\n", s->framerate.num, s->framerate.den); return 0; }
FFmpegDecoder::FFmpegDecoder() : av(NULL) { av = new ffmpeg_vars_t; memset(av, 0, sizeof(ffmpeg_vars_t)); av_register_all(); avcodec_register_all(); av->temp_data = reinterpret_cast<uint8_t *>(av_malloc(262144)); av->fifo = av_fifo_alloc(524288); }
static av_cold int common_init(AVFilterContext *ctx) { BufferSinkContext *buf = ctx->priv; buf->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef *)); if (!buf->fifo) { av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n"); return AVERROR(ENOMEM); } return 0; }
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) { BufferSinkContext *sink = ctx->priv; if (!(sink->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef*)))) { av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n"); return AVERROR(ENOMEM); } return 0; }
static av_cold int init_audio(AVFilterContext *ctx) { BufferSourceContext *s = ctx->priv; int ret = 0; if (!(s->sample_fmt != AV_SAMPLE_FMT_NONE || s->got_format_from_params)) { av_log(ctx, AV_LOG_ERROR, "Sample format was not set or was invalid\n"); return AVERROR(EINVAL); } if (s->channel_layout_str || s->channel_layout) { int n; if (!s->channel_layout) { s->channel_layout = av_get_channel_layout(s->channel_layout_str); if (!s->channel_layout) { av_log(ctx, AV_LOG_ERROR, "Invalid channel layout %s.\n", s->channel_layout_str); return AVERROR(EINVAL); } } n = av_get_channel_layout_nb_channels(s->channel_layout); if (s->channels) { if (n != s->channels) { av_log(ctx, AV_LOG_ERROR, "Mismatching channel count %d and layout '%s' " "(%d channels)\n", s->channels, s->channel_layout_str, n); return AVERROR(EINVAL); } } s->channels = n; } else if (!s->channels) { av_log(ctx, AV_LOG_ERROR, "Neither number of channels nor " "channel layout specified\n"); return AVERROR(EINVAL); } if (!(s->fifo = av_fifo_alloc(sizeof(AVFrame*)))) return AVERROR(ENOMEM); if (!s->time_base.num) s->time_base = (AVRational){1, s->sample_rate}; av_log(ctx, AV_LOG_VERBOSE, "tb:%d/%d samplefmt:%s samplerate:%d chlayout:%s\n", s->time_base.num, s->time_base.den, av_get_sample_fmt_name(s->sample_fmt), s->sample_rate, s->channel_layout_str); s->warning_limit = 100; return ret; }
static int qsv_decode_init(AVCodecContext *avctx, QSVContext *q, mfxSession session) { mfxVideoParam param = { { 0 } }; int ret; if (!q->async_fifo) { q->async_fifo = av_fifo_alloc((1 + q->async_depth) * (sizeof(mfxSyncPoint*) + sizeof(QSVFrame*))); if (!q->async_fifo) return AVERROR(ENOMEM); } ret = qsv_init_session(avctx, q, session); if (ret < 0) { av_log(avctx, AV_LOG_ERROR, "Error initializing an MFX session\n"); return ret; } ret = ff_qsv_codec_id_to_mfx(avctx->codec_id); if (ret < 0) return ret; param.mfx.CodecId = ret; param.mfx.CodecProfile = avctx->profile; param.mfx.CodecLevel = avctx->level; param.mfx.FrameInfo.BitDepthLuma = 8; param.mfx.FrameInfo.BitDepthChroma = 8; param.mfx.FrameInfo.Shift = 0; param.mfx.FrameInfo.FourCC = MFX_FOURCC_NV12; param.mfx.FrameInfo.Width = avctx->coded_width; param.mfx.FrameInfo.Height = avctx->coded_height; param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420; param.IOPattern = q->iopattern; param.AsyncDepth = q->async_depth; param.ExtParam = q->ext_buffers; param.NumExtParam = q->nb_ext_buffers; ret = MFXVideoDECODE_Init(q->session, ¶m); if (ret < 0) { av_log(avctx, AV_LOG_ERROR, "Error initializing the MFX video decoder\n"); return ff_qsv_error(ret); } return 0; }
static av_cold int init(AVFilterContext *ctx, const char *args0, void *opaque) { ABufferSourceContext *abuffer = ctx->priv; char *arg = NULL, *ptr, chlayout_str[16]; char *args = av_strdup(args0); int ret; arg = av_strtok(args, ":", &ptr); #define ADD_FORMAT(fmt_name) \ if (!arg) \ goto arg_fail; \ if ((ret = ff_parse_##fmt_name(&abuffer->fmt_name, arg, ctx)) < 0) { \ av_freep(&args); \ return ret; \ } \ if (*args) \ arg = av_strtok(NULL, ":", &ptr) ADD_FORMAT(sample_rate); ADD_FORMAT(sample_format); ADD_FORMAT(channel_layout); ADD_FORMAT(packing_format); abuffer->fifo = av_fifo_alloc(FIFO_SIZE*sizeof(AVFilterBufferRef*)); if (!abuffer->fifo) { av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo, filter init failed.\n"); return AVERROR(ENOMEM); } av_get_channel_layout_string(chlayout_str, sizeof(chlayout_str), -1, abuffer->channel_layout); av_log(ctx, AV_LOG_INFO, "format:%s layout:%s rate:%d\n", av_get_sample_fmt_name(abuffer->sample_format), chlayout_str, abuffer->sample_rate); av_freep(&args); return 0; arg_fail: av_log(ctx, AV_LOG_ERROR, "Invalid arguments, must be of the form " "sample_rate:sample_fmt:channel_layout:packing\n"); av_freep(&args); return AVERROR(EINVAL); }
int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size) { unsigned int old_size= f->end - f->buffer; if(old_size < new_size){ int len= av_fifo_size(f); AVFifoBuffer *f2= av_fifo_alloc(new_size); if (!f2) return -1; av_fifo_generic_read(f, f2->buffer, len, NULL); f2->wptr += len; f2->wndx += len; av_free(f->buffer); *f= *f2; av_free(f2); } return 0; }
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) { SelectContext *select = ctx->priv; int ret; if ((ret = av_expr_parse(&select->expr, args ? args : "1", var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) { av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", args); return ret; } select->pending_frames = av_fifo_alloc(FIFO_SIZE*sizeof(AVFilterBufferRef*)); if (!select->pending_frames) { av_log(ctx, AV_LOG_ERROR, "Failed to allocate pending frames buffer.\n"); return AVERROR(ENOMEM); } return 0; }
static av_cold int init(AVFilterContext *ctx) { FPSContext *s = ctx->priv; int ret; if ((ret = av_parse_video_rate(&s->framerate, s->fps)) < 0) { av_log(ctx, AV_LOG_ERROR, "Error parsing framerate %s.\n", s->fps); return ret; } if (!(s->fifo = av_fifo_alloc(2*sizeof(AVFrame*)))) return AVERROR(ENOMEM); s->pts = AV_NOPTS_VALUE; av_log(ctx, AV_LOG_VERBOSE, "fps=%d/%d\n", s->framerate.num, s->framerate.den); return 0; }
static av_cold int init_video(AVFilterContext *ctx) { BufferSourceContext *c = ctx->priv; if (c->pix_fmt == AV_PIX_FMT_NONE || !c->w || !c->h || av_q2d(c->time_base) <= 0) { av_log(ctx, AV_LOG_ERROR, "Invalid parameters provided.\n"); return AVERROR(EINVAL); } if (!(c->fifo = av_fifo_alloc(sizeof(AVFrame*)))) return AVERROR(ENOMEM); av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d pixfmt:%s tb:%d/%d fr:%d/%d sar:%d/%d sws_param:%s\n", c->w, c->h, av_get_pix_fmt_name(c->pix_fmt), c->time_base.num, c->time_base.den, c->frame_rate.num, c->frame_rate.den, c->pixel_aspect.num, c->pixel_aspect.den, (char *)av_x_if_null(c->sws_param, "")); c->warning_limit = 100; return 0; }
AVCodecEncoder::AVCodecEncoder(uint32 codecID, int bitRateScale) : Encoder(), fBitRateScale(bitRateScale), fCodecID((enum CodecID)codecID), fCodec(NULL), fOwnContext(avcodec_alloc_context()), fContext(fOwnContext), fCodecInitStatus(CODEC_INIT_NEEDED), fFrame(avcodec_alloc_frame()), fSwsContext(NULL), fFramesWritten(0), fChunkBuffer(new(std::nothrow) uint8[kDefaultChunkBufferSize]) { TRACE("AVCodecEncoder::AVCodecEncoder()\n"); if (fCodecID > 0) { fCodec = avcodec_find_encoder(fCodecID); TRACE(" found AVCodec for %u: %p\n", fCodecID, fCodec); } memset(&fInputFormat, 0, sizeof(media_format)); fAudioFifo = av_fifo_alloc(0); fDstFrame.data[0] = NULL; fDstFrame.data[1] = NULL; fDstFrame.data[2] = NULL; fDstFrame.data[3] = NULL; fDstFrame.linesize[0] = 0; fDstFrame.linesize[1] = 0; fDstFrame.linesize[2] = 0; fDstFrame.linesize[3] = 0; // Initial parameters, so we know if the user changed them fEncodeParameters.avg_field_size = 0; fEncodeParameters.max_field_size = 0; fEncodeParameters.quality = 1.0f; }
/* This function inserts a packet at fifo front. */ static void qsv_packet_push_front(QSVContext *q, AVPacket *avpkt) { int fifo_size = av_fifo_size(q->pkt_fifo); if (!fifo_size) { /* easy case fifo is empty */ av_fifo_generic_write(q->pkt_fifo, avpkt, sizeof(*avpkt), NULL); } else { /* realloc necessary */ AVPacket pkt; AVFifoBuffer *fifo = av_fifo_alloc(fifo_size+av_fifo_space(q->pkt_fifo)); av_fifo_generic_write(fifo, avpkt, sizeof(*avpkt), NULL); while (av_fifo_size(q->pkt_fifo)) { av_fifo_generic_read(q->pkt_fifo, &pkt, sizeof(pkt), NULL); av_fifo_generic_write(fifo, &pkt, sizeof(pkt), NULL); } av_fifo_free(q->pkt_fifo); q->pkt_fifo = fifo; } }
static av_cold int init(AVFilterContext *ctx, const char *args) { SelectContext *select = ctx->priv; int ret; if ((ret = av_expr_parse(&select->expr, args ? args : "1", var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) { av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", args); return ret; } select->pending_frames = av_fifo_alloc(FIFO_SIZE*sizeof(AVFilterBufferRef*)); if (!select->pending_frames) { av_log(ctx, AV_LOG_ERROR, "Failed to allocate pending frames buffer.\n"); return AVERROR(ENOMEM); } select->do_scene_detect = args && strstr(args, "scene"); if (select->do_scene_detect && !CONFIG_AVCODEC) { av_log(ctx, AV_LOG_ERROR, "Scene detection is not available without libavcodec.\n"); return AVERROR(EINVAL); } return 0; }
AVAudioFifo *av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples) { AVAudioFifo *af; int buf_size, i; /* get channel buffer size (also validates parameters) */ if (av_samples_get_buffer_size(&buf_size, channels, nb_samples, sample_fmt, 1) < 0) return NULL; af = av_mallocz(sizeof(*af)); if (!af) return NULL; af->channels = channels; af->sample_fmt = sample_fmt; af->sample_size = buf_size / nb_samples; af->nb_buffers = av_sample_fmt_is_planar(sample_fmt) ? channels : 1; af->buf = av_mallocz_array(af->nb_buffers, sizeof(*af->buf)); if (!af->buf) goto error; for (i = 0; i < af->nb_buffers; i++) { af->buf[i] = av_fifo_alloc(buf_size); if (!af->buf[i]) goto error; } af->allocated_samples = nb_samples; return af; error: av_audio_fifo_free(af); return NULL; }
static void *start_smoothing( void *ptr ) { obe_t *h = ptr; int num_muxed_data = 0, buffer_complete = 0; int64_t start_clock = -1, start_pcr, end_pcr, temporal_vbv_size = 0, cur_pcr; obe_muxed_data_t **muxed_data = NULL, *start_data, *end_data; AVFifoBuffer *fifo_data = NULL, *fifo_pcr = NULL; uint8_t *output_buf; struct sched_param param = {0}; param.sched_priority = 99; pthread_setschedparam( pthread_self(), SCHED_FIFO, ¶m ); /* This thread buffers one VBV worth of frames */ fifo_data = av_fifo_alloc( TS_PACKETS_SIZE ); if( !fifo_data ) { fprintf( stderr, "[mux-smoothing] Could not allocate data fifo" ); return NULL; } fifo_pcr = av_fifo_alloc( 7 * sizeof(int64_t) ); if( !fifo_pcr ) { fprintf( stderr, "[mux-smoothing] Could not allocate pcr fifo" ); return NULL; } if( h->obe_system == OBE_SYSTEM_TYPE_GENERIC ) { for( int i = 0; i < h->num_encoders; i++ ) { if( h->encoders[i]->is_video ) { pthread_mutex_lock( &h->encoders[i]->queue.mutex ); while( !h->encoders[i]->is_ready ) pthread_cond_wait( &h->encoders[i]->queue.in_cv, &h->encoders[i]->queue.mutex ); x264_param_t *params = h->encoders[i]->encoder_params; temporal_vbv_size = av_rescale_q_rnd( (int64_t)params->rc.i_vbv_buffer_size * params->rc.f_vbv_buffer_init, (AVRational){1, params->rc.i_vbv_max_bitrate }, (AVRational){ 1, OBE_CLOCK }, AV_ROUND_UP ); pthread_mutex_unlock( &h->encoders[i]->queue.mutex ); break; } } } while( 1 ) { pthread_mutex_lock( &h->mux_smoothing_queue.mutex ); while( h->mux_smoothing_queue.size == num_muxed_data && !h->cancel_mux_smoothing_thread ) pthread_cond_wait( &h->mux_smoothing_queue.in_cv, &h->mux_smoothing_queue.mutex ); if( h->cancel_mux_smoothing_thread ) { pthread_mutex_unlock( &h->mux_smoothing_queue.mutex ); break; } num_muxed_data = h->mux_smoothing_queue.size; /* Refill the buffer after a drop */ pthread_mutex_lock( &h->drop_mutex ); if( h->mux_drop ) { syslog( LOG_INFO, "Mux smoothing buffer reset\n" ); h->mux_drop = 0; av_fifo_reset( fifo_data ); av_fifo_reset( fifo_pcr ); buffer_complete = 0; start_clock = -1; } pthread_mutex_unlock( &h->drop_mutex ); if( !buffer_complete ) { start_data = h->mux_smoothing_queue.queue[0]; end_data = h->mux_smoothing_queue.queue[num_muxed_data-1]; start_pcr = start_data->pcr_list[0]; end_pcr = end_data->pcr_list[(end_data->len / 188)-1]; if( end_pcr - start_pcr >= temporal_vbv_size ) { buffer_complete = 1; start_clock = -1; } else { pthread_mutex_unlock( &h->mux_smoothing_queue.mutex ); continue; } } //printf("\n mux smoothed frames %i \n", num_muxed_data ); muxed_data = malloc( num_muxed_data * sizeof(*muxed_data) ); if( !muxed_data ) { pthread_mutex_unlock( &h->output_queue.mutex ); syslog( LOG_ERR, "Malloc failed\n" ); return NULL; } memcpy( muxed_data, h->mux_smoothing_queue.queue, num_muxed_data * sizeof(*muxed_data) ); pthread_mutex_unlock( &h->mux_smoothing_queue.mutex ); for( int i = 0; i < num_muxed_data; i++ ) { if( av_fifo_realloc2( fifo_data, av_fifo_size( fifo_data ) + muxed_data[i]->len ) < 0 ) { syslog( LOG_ERR, "Malloc failed\n" ); return NULL; } av_fifo_generic_write( fifo_data, muxed_data[i]->data, muxed_data[i]->len, NULL ); if( av_fifo_realloc2( fifo_pcr, av_fifo_size( fifo_pcr ) + ((muxed_data[i]->len * sizeof(int64_t)) / 188) ) < 0 ) { syslog( LOG_ERR, "Malloc failed\n" ); return NULL; } av_fifo_generic_write( fifo_pcr, muxed_data[i]->pcr_list, (muxed_data[i]->len * sizeof(int64_t)) / 188, NULL ); remove_from_queue( &h->mux_smoothing_queue ); destroy_muxed_data( muxed_data[i] ); } free( muxed_data ); muxed_data = NULL; num_muxed_data = 0; while( av_fifo_size( fifo_data ) >= TS_PACKETS_SIZE ) { output_buf = malloc( TS_PACKETS_SIZE + 7 * sizeof(int64_t) ); if( !output_buf ) { syslog( LOG_ERR, "Malloc failed\n" ); return NULL; } av_fifo_generic_read( fifo_pcr, output_buf, 7 * sizeof(int64_t), NULL ); av_fifo_generic_read( fifo_data, &output_buf[7 * sizeof(int64_t)], TS_PACKETS_SIZE, NULL ); cur_pcr = AV_RN64( output_buf ); if( start_clock != -1 ) { sleep_input_clock( h, cur_pcr - start_pcr + start_clock ); } if( start_clock == -1 ) { start_clock = get_input_clock_in_mpeg_ticks( h ); start_pcr = cur_pcr; } if( add_to_queue( &h->output_queue, output_buf ) < 0 ) return NULL; output_buf = NULL; } } av_fifo_free( fifo_data ); av_fifo_free( fifo_pcr ); return NULL; }
static int qsv_decode_init(AVCodecContext *avctx, QSVContext *q) { const AVPixFmtDescriptor *desc; mfxSession session = NULL; int iopattern = 0; mfxVideoParam param = { { 0 } }; int frame_width = avctx->coded_width; int frame_height = avctx->coded_height; int ret; desc = av_pix_fmt_desc_get(avctx->sw_pix_fmt); if (!desc) return AVERROR_BUG; if (!q->async_fifo) { q->async_fifo = av_fifo_alloc((1 + q->async_depth) * (sizeof(mfxSyncPoint*) + sizeof(QSVFrame*))); if (!q->async_fifo) return AVERROR(ENOMEM); } if (avctx->pix_fmt == AV_PIX_FMT_QSV && avctx->hwaccel_context) { AVQSVContext *user_ctx = avctx->hwaccel_context; session = user_ctx->session; iopattern = user_ctx->iopattern; q->ext_buffers = user_ctx->ext_buffers; q->nb_ext_buffers = user_ctx->nb_ext_buffers; } if (avctx->hw_frames_ctx) { AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data; AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx; if (!iopattern) { if (frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME) iopattern = MFX_IOPATTERN_OUT_OPAQUE_MEMORY; else if (frames_hwctx->frame_type & MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET) iopattern = MFX_IOPATTERN_OUT_VIDEO_MEMORY; } frame_width = frames_hwctx->surfaces[0].Info.Width; frame_height = frames_hwctx->surfaces[0].Info.Height; } if (!iopattern) iopattern = MFX_IOPATTERN_OUT_SYSTEM_MEMORY; q->iopattern = iopattern; ret = qsv_init_session(avctx, q, session, avctx->hw_frames_ctx); if (ret < 0) { av_log(avctx, AV_LOG_ERROR, "Error initializing an MFX session\n"); return ret; } ret = ff_qsv_codec_id_to_mfx(avctx->codec_id); if (ret < 0) return ret; param.mfx.CodecId = ret; param.mfx.CodecProfile = avctx->profile; param.mfx.CodecLevel = avctx->level; param.mfx.FrameInfo.BitDepthLuma = desc->comp[0].depth; param.mfx.FrameInfo.BitDepthChroma = desc->comp[0].depth; param.mfx.FrameInfo.Shift = desc->comp[0].depth > 8; param.mfx.FrameInfo.FourCC = q->fourcc; param.mfx.FrameInfo.Width = frame_width; param.mfx.FrameInfo.Height = frame_height; param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420; param.IOPattern = q->iopattern; param.AsyncDepth = q->async_depth; param.ExtParam = q->ext_buffers; param.NumExtParam = q->nb_ext_buffers; ret = MFXVideoDECODE_Init(q->session, ¶m); if (ret < 0) return ff_qsv_print_error(avctx, ret, "Error initializing the MFX video decoder"); q->frame_info = param.mfx.FrameInfo; return 0; }
static int mpeg_mux_init(AVFormatContext *ctx) { MpegMuxContext *s = ctx->priv_data; int bitrate, i, mpa_id, mpv_id, mps_id, ac3_id, dts_id, lpcm_id, j; AVStream *st; StreamInfo *stream; int audio_bitrate; int video_bitrate; s->packet_number = 0; s->is_vcd = (CONFIG_MPEG1VCD_MUXER && ctx->oformat == &ff_mpeg1vcd_muxer); s->is_svcd = (CONFIG_MPEG2SVCD_MUXER && ctx->oformat == &ff_mpeg2svcd_muxer); s->is_mpeg2 = ((CONFIG_MPEG2VOB_MUXER && ctx->oformat == &ff_mpeg2vob_muxer) || (CONFIG_MPEG2DVD_MUXER && ctx->oformat == &ff_mpeg2dvd_muxer) || (CONFIG_MPEG2SVCD_MUXER && ctx->oformat == &ff_mpeg2svcd_muxer)); s->is_dvd = (CONFIG_MPEG2DVD_MUXER && ctx->oformat == &ff_mpeg2dvd_muxer); if(ctx->packet_size) { if (ctx->packet_size < 20 || ctx->packet_size > (1 << 23) + 10) { av_log(ctx, AV_LOG_ERROR, "Invalid packet size %d\n", ctx->packet_size); goto fail; } s->packet_size = ctx->packet_size; } else s->packet_size = 2048; s->vcd_padding_bytes_written = 0; s->vcd_padding_bitrate=0; s->audio_bound = 0; s->video_bound = 0; mpa_id = AUDIO_ID; ac3_id = AC3_ID; dts_id = DTS_ID; mpv_id = VIDEO_ID; mps_id = SUB_ID; lpcm_id = LPCM_ID; for(i=0;i<ctx->nb_streams;i++) { st = ctx->streams[i]; stream = av_mallocz(sizeof(StreamInfo)); if (!stream) goto fail; st->priv_data = stream; av_set_pts_info(st, 64, 1, 90000); switch(st->codec->codec_type) { case AVMEDIA_TYPE_AUDIO: if (st->codec->codec_id == CODEC_ID_AC3) { stream->id = ac3_id++; } else if (st->codec->codec_id == CODEC_ID_DTS) { stream->id = dts_id++; } else if (st->codec->codec_id == CODEC_ID_PCM_S16BE) { stream->id = lpcm_id++; for(j = 0; j < 4; j++) { if (lpcm_freq_tab[j] == st->codec->sample_rate) break; } if (j == 4) goto fail; if (st->codec->channels > 8) return -1; stream->lpcm_header[0] = 0x0c; stream->lpcm_header[1] = (st->codec->channels - 1) | (j << 4); stream->lpcm_header[2] = 0x80; stream->lpcm_align = st->codec->channels * 2; } else { stream->id = mpa_id++; } /* This value HAS to be used for VCD (see VCD standard, p. IV-7). Right now it is also used for everything else.*/ stream->max_buffer_size = 4 * 1024; s->audio_bound++; break; case AVMEDIA_TYPE_VIDEO: stream->id = mpv_id++; if (st->codec->rc_buffer_size) stream->max_buffer_size = 6*1024 + st->codec->rc_buffer_size/8; else { av_log(ctx, AV_LOG_WARNING, "VBV buffer size not set, muxing may fail\n"); stream->max_buffer_size = 230*1024; //FIXME this is probably too small as default } #if 0 /* see VCD standard, p. IV-7*/ stream->max_buffer_size = 46 * 1024; else /* This value HAS to be used for SVCD (see SVCD standard, p. 26 V.2.3.2). Right now it is also used for everything else.*/ stream->max_buffer_size = 230 * 1024; #endif s->video_bound++; break; case AVMEDIA_TYPE_SUBTITLE: stream->id = mps_id++; stream->max_buffer_size = 16 * 1024; break; default: return -1; } stream->fifo= av_fifo_alloc(16); if (!stream->fifo) goto fail; }