static int cuvid_is_buffer_full(AVCodecContext *avctx) { CuvidContext *ctx = avctx->priv_data; int delay = ctx->cuparseinfo.ulMaxDisplayDelay; if (ctx->deint_mode != cudaVideoDeinterlaceMode_Weave && !ctx->drop_second_field) delay *= 2; return (av_fifo_size(ctx->frame_queue) / sizeof(CuvidParsedFrame)) + delay >= ctx->nb_surfaces; }
static float get_delay(void) { int buffered = av_fifo_size(buffer); // could be less float in_jack = jack_latency; if (estimate && callback_interval > 0) { float elapsed = (float)GetTimer() / 1000000.0 - callback_time; in_jack += callback_interval - elapsed; if (in_jack < 0) in_jack = 0; } return (float)buffered / (float)ao_data.bps + in_jack; }
static int read_buffer(unsigned char* data,int len){ int buffered = av_fifo_size(buffer); if (len > buffered) len = buffered; #ifdef USE_SDL_INTERNAL_MIXER av_fifo_generic_read(buffer, data, len, mix_audio); #else av_fifo_generic_read(buffer, data, len, NULL); #endif return len; }
/** * \brief read data from buffer and splitting it into channels * \param bufs num_bufs float buffers, each will contain the data of one channel * \param cnt number of samples to read per channel * \param num_bufs number of channels to split the data into * \return number of samples read per channel, equals cnt unless there was too * little data in the buffer * * Assumes the data in the buffer is of type float, the number of bytes * read is res * num_bufs * sizeof(float), where res is the return value. * If there is not enough data in the buffer remaining parts will be filled * with silence. */ static int read_buffer(float **bufs, int cnt, int num_bufs) { struct deinterleave di = {bufs, num_bufs, 0, 0}; int buffered = av_fifo_size(buffer); if (cnt * sizeof(float) * num_bufs > buffered) { silence(bufs, cnt, num_bufs); cnt = buffered / sizeof(float) / num_bufs; } av_fifo_generic_read(buffer, &di, cnt * num_bufs * sizeof(float), deinterleave); return cnt; }
int attribute_align_arg av_buffersrc_add_frame(AVFilterContext *ctx, AVFrame *frame) { BufferSourceContext *s = ctx->priv; AVFrame *copy; int refcounted, ret; if (!frame) { s->eof = 1; return 0; } else if (s->eof) return AVERROR(EINVAL); refcounted = !!frame->buf[0]; switch (ctx->outputs[0]->type) { case AVMEDIA_TYPE_VIDEO: CHECK_VIDEO_PARAM_CHANGE(ctx, s, frame->width, frame->height, frame->format); break; case AVMEDIA_TYPE_AUDIO: CHECK_AUDIO_PARAM_CHANGE(ctx, s, frame->sample_rate, frame->channel_layout, frame->format); break; default: return AVERROR(EINVAL); } if (!av_fifo_space(s->fifo) && (ret = av_fifo_realloc2(s->fifo, av_fifo_size(s->fifo) + sizeof(copy))) < 0) return ret; if (!(copy = av_frame_alloc())) return AVERROR(ENOMEM); if (refcounted) { av_frame_move_ref(copy, frame); } else { ret = av_frame_ref(copy, frame); if (ret < 0) { av_frame_free(©); return ret; } } if ((ret = av_fifo_generic_write(s->fifo, ©, sizeof(copy), NULL)) < 0) { if (refcounted) av_frame_move_ref(frame, copy); av_frame_free(©); return ret; } return 0; }
static av_cold void uninit(AVFilterContext *ctx) { BufferSourceContext *s = ctx->priv; while (s->fifo && av_fifo_size(s->fifo)) { AVFrame *frame; av_fifo_generic_read(s->fifo, &frame, sizeof(frame), NULL); av_frame_free(&frame); } av_fifo_free(s->fifo); s->fifo = NULL; }
static av_cold void uninit(AVFilterContext *ctx) { BufferSinkContext *sink = ctx->priv; while (sink->fifo && av_fifo_size(sink->fifo)) { AVFilterBufferRef *buf; av_fifo_generic_read(sink->fifo, &buf, sizeof(buf), NULL); avfilter_unref_buffer(buf); } av_fifo_free(sink->fifo); }
/* 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; } }
int dc_audio_encoder_encode(AudioOutputFile * p_aout, AudioInputData * p_aind) { int i_got_pkt; //AVStream * p_audio_stream = p_aout->p_fmt->streams[p_aout->i_astream_idx]; //AVCodecContext * p_audio_codec_ctx = p_audio_stream->codec; AVCodecContext * p_audio_codec_ctx = p_aout->p_codec_ctx; while (av_fifo_size(p_aout->p_fifo) >= p_aout->i_frame_bytes) { av_fifo_generic_read(p_aout->p_fifo, p_aout->p_adata_buf, p_aout->i_frame_bytes, NULL); p_aout->p_aframe->data[0] = p_aout->p_adata_buf; p_aout->p_aframe->linesize[0] = p_aout->i_frame_bytes; av_init_packet(&p_aout->packet); /* * Set PTS (method 1) */ //p_aout->p_aframe->pts = p_aind->next_pts; /* * Set PTS (method 2) * int64_t now = av_gettime(); * outAudioCtx->pOutAudioFrame->pts = av_rescale_q(now,AV_TIME_BASE_Q, * audioEncCtx->time_base); */ /* Encode audio */ if (avcodec_encode_audio2(p_audio_codec_ctx, &p_aout->packet, p_aout->p_aframe, &i_got_pkt) != 0) { fprintf(stderr, "Error while encoding audio.\n"); return -1; } if (i_got_pkt) { //p_aout->acc_samples += p_aout->p_aframe->nb_samples; return 0; } av_free_packet(&p_aout->packet); } return 1; }
static int process_callback(jack_nframes_t nframes, void *arg) { /* Warning: this function runs in realtime. One mustn't allocate memory here * or do any other thing that could block. */ int i, j; JackData *self = arg; float * buffer; jack_nframes_t latency, cycle_delay; AVPacket pkt; float *pkt_data; double cycle_time; if (!self->client) return 0; /* The approximate delay since the hardware interrupt as a number of frames */ cycle_delay = jack_frames_since_cycle_start(self->client); /* Retrieve filtered cycle time */ cycle_time = ff_timefilter_update(self->timefilter, av_gettime() / 1000000.0 - (double) cycle_delay / self->sample_rate, self->buffer_size); /* Check if an empty packet is available, and if there's enough space to send it back once filled */ if ((av_fifo_size(self->new_pkts) < sizeof(pkt)) || (av_fifo_space(self->filled_pkts) < sizeof(pkt))) { self->pkt_xrun = 1; return 0; } /* Retrieve empty (but allocated) packet */ av_fifo_generic_read(self->new_pkts, &pkt, sizeof(pkt), NULL); pkt_data = (float *) pkt.data; latency = 0; /* Copy and interleave audio data from the JACK buffer into the packet */ for (i = 0; i < self->nports; i++) { latency += jack_port_get_total_latency(self->client, self->ports[i]); buffer = jack_port_get_buffer(self->ports[i], self->buffer_size); for (j = 0; j < self->buffer_size; j++) pkt_data[j * self->nports + i] = buffer[j]; } /* Timestamp the packet with the cycle start time minus the average latency */ pkt.pts = (cycle_time - (double) latency / (self->nports * self->sample_rate)) * 1000000.0; /* Send the now filled packet back, and increase packet counter */ av_fifo_generic_write(self->filled_pkts, &pkt, sizeof(pkt), NULL); sem_post(&self->packet_count); return 0; }
static void write_buf(AVFilterContext *ctx, AVFilterBufferRef *buf) { BufferSinkContext *sink = ctx->priv; if (av_fifo_space(sink->fifo) < sizeof(AVFilterBufferRef *) && (av_fifo_realloc2(sink->fifo, av_fifo_size(sink->fifo) * 2) < 0)) { av_log(ctx, AV_LOG_ERROR, "Error reallocating the FIFO.\n"); return; } av_fifo_generic_write(sink->fifo, &buf, sizeof(buf), NULL); }
static int poll_frame(AVFilterLink *outlink) { SelectContext *select = outlink->src->priv; AVFilterLink *inlink = outlink->src->inputs[0]; int count, ret; if (!av_fifo_size(select->pending_frames)) { if ((count = ff_poll_frame(inlink)) <= 0) return count; /* request frame from input, and apply select condition to it */ select->cache_frames = 1; while (count-- && av_fifo_space(select->pending_frames)) { ret = ff_request_frame(inlink); if (ret < 0) break; } select->cache_frames = 0; } return av_fifo_size(select->pending_frames)/sizeof(AVFrame*); }
int attribute_align_arg av_buffersink_poll_frame(AVFilterContext *ctx) { BufferSinkContext *buf = ctx->priv; AVFilterLink *inlink = ctx->inputs[0]; av_assert0( !strcmp(ctx->filter->name, "buffersink") || !strcmp(ctx->filter->name, "abuffersink") || !strcmp(ctx->filter->name, "ffbuffersink") || !strcmp(ctx->filter->name, "ffabuffersink")); return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + ff_poll_frame(inlink); }
static av_cold void uninit(AVFilterContext *ctx) { FPSContext *s = ctx->priv; if (s->fifo) { s->drop += av_fifo_size(s->fifo) / sizeof(AVFilterBufferRef*); flush_fifo(s->fifo); av_fifo_free(s->fifo); } av_log(ctx, AV_LOG_VERBOSE, "%d frames in, %d frames out; %d frames dropped, " "%d frames duplicated.\n", s->frames_in, s->frames_out, s->drop, s->dup); }
static av_cold void uninit(AVFilterContext *ctx) { BufferSourceContext *s = ctx->priv; while (s->fifo && av_fifo_size(s->fifo)) { AVFilterBufferRef *buf; av_fifo_generic_read(s->fifo, &buf, sizeof(buf), NULL); avfilter_unref_buffer(buf); } av_fifo_free(s->fifo); s->fifo = NULL; avfilter_free(s->scale); s->scale = NULL; }
static int write_to_fifo(AVFifoBuffer *fifo, AVFrame *buf) { int ret; if (!av_fifo_space(fifo) && (ret = av_fifo_realloc2(fifo, 2*av_fifo_size(fifo)))) { av_frame_free(&buf); return ret; } av_fifo_generic_write(fifo, &buf, sizeof(buf), NULL); return 0; }
int32 FFmpegDecoder::Decode(uint8* buffer, int32 max_length) { int32 total_bytes_read = 0; uint8* cur = buffer; while (total_bytes_read < max_length) { int32 fifo_size = av_fifo_size(av->fifo); if (!fifo_size) { if (!GetAudio()) break; fifo_size = av_fifo_size(av->fifo); } int bytes_read = std::min(fifo_size, max_length - total_bytes_read); av_fifo_generic_read(av->fifo, cur, bytes_read, NULL); total_bytes_read += bytes_read; cur += bytes_read; } memset(&buffer[total_bytes_read], 0, max_length - total_bytes_read); return total_bytes_read; }
static int gxf_new_audio_packet(GXFContext *gxf, GXFStreamContext *sc, AVPacket *pkt, int flush) { int size = flush ? av_fifo_size(&sc->audio_buffer) : GXF_AUDIO_PACKET_SIZE; if (!size) return 0; av_new_packet(pkt, size); av_fifo_read(&sc->audio_buffer, pkt->data, size); pkt->stream_index = sc->index; pkt->dts = sc->current_dts; sc->current_dts += size / 2; /* we only support 16 bit pcm mono for now */ return size; }
static av_cold void common_uninit(AVFilterContext *ctx) { BufferSinkContext *buf = ctx->priv; AVFilterBufferRef *picref; if (buf->fifo) { while (av_fifo_size(buf->fifo) >= sizeof(AVFilterBufferRef *)) { av_fifo_generic_read(buf->fifo, &picref, sizeof(picref), NULL); avfilter_unref_buffer(picref); } av_fifo_free(buf->fifo); buf->fifo = NULL; } }
static void end_frame(AVFilterLink *link) { AVFilterContext *ctx = link->dst; BufferSinkContext *sink = ctx->priv; if (av_fifo_space(sink->fifo) < sizeof(AVFilterBufferRef *) && (av_fifo_realloc2(sink->fifo, av_fifo_size(sink->fifo) * 2) < 0)) { av_log(ctx, AV_LOG_ERROR, "Error reallocating the FIFO.\n"); return; } av_fifo_generic_write(sink->fifo, &link->cur_buf, sizeof(link->cur_buf), NULL); link->cur_buf = NULL; }
int FFMpegFifo::read(unsigned char* dst,int readSize) { //SDL_LockMutex(mutex); if (av_fifo_size(fifo) < readSize) { waitReadBytes = readSize; //SDL_CondWait(condRead,mutex); } int ret = av_fifo_generic_read(fifo,dst,readSize,NULL); if (av_fifo_space(fifo) >= waitWriteBytes) //SDL_CondSignal(condWrite); //SDL_UnlockMutex(mutex); return readSize; }
/** * Resizes a FIFO. */ void av_fifo_realloc(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_init(&f2, new_size); av_fifo_read(f, f2.buffer, len); f2.wptr += len; av_free(f->buffer); *f= f2; } }
static av_cold void uninit(AVFilterContext *ctx) { SelectContext *select = ctx->priv; AVFilterBufferRef *picref; int i; av_expr_free(select->expr); select->expr = NULL; for (i = 0; i < av_fifo_size(select->pending_frames)/sizeof(picref); i++) { av_fifo_generic_read(select->pending_frames, &picref, sizeof(picref), NULL); avfilter_unref_buffer(picref); } av_fifo_free(select->pending_frames); }
int av_buffersink_get_buffer_ref(AVFilterContext *ctx, AVFilterBufferRef **bufref, int flags) { BufferSinkContext *buf = ctx->priv; AVFilterLink *inlink = ctx->inputs[0]; int ret; *bufref = NULL; /* no picref available, fetch it from the filterchain */ if (!av_fifo_size(buf->fifo)) { if ((ret = avfilter_request_frame(inlink)) < 0) return ret; } if (!av_fifo_size(buf->fifo)) return AVERROR(EINVAL); if (flags & AV_BUFFERSINK_FLAG_PEEK) *bufref = *((AVFilterBufferRef **)av_fifo_peek2(buf->fifo, 0)); else av_fifo_generic_read(buf->fifo, bufref, sizeof(*bufref), NULL); return 0; }
int av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf) { BufferSinkContext *sink = ctx->priv; AVFilterLink *link = ctx->inputs[0]; int ret; if (!buf) { if (av_fifo_size(sink->fifo)) return av_fifo_size(sink->fifo)/sizeof(*buf); else return ff_poll_frame(ctx->inputs[0]); } if (!av_fifo_size(sink->fifo) && (ret = ff_request_frame(link)) < 0) return ret; if (!av_fifo_size(sink->fifo)) return AVERROR(EINVAL); av_fifo_generic_read(sink->fifo, buf, sizeof(*buf), NULL); return 0; }
static void mediacodec_decode_flush(AVCodecContext *avctx) { MediaCodecH264DecContext *s = avctx->priv_data; while (av_fifo_size(s->fifo)) { AVPacket pkt; av_fifo_generic_read(s->fifo, &pkt, sizeof(pkt), NULL); av_packet_unref(&pkt); } av_fifo_reset(s->fifo); av_packet_unref(&s->filtered_pkt); ff_mediacodec_dec_flush(avctx, &s->ctx); }
static int ff_interleave_new_audio_packet(AVFormatContext *s, AVPacket *pkt, int stream_index, int flush) { AVStream *st = s->streams[stream_index]; AudioInterleaveContext *aic = st->priv_data; int size = FFMIN(av_fifo_size(aic->fifo), *aic->samples * aic->sample_size); if (!size || (!flush && size == av_fifo_size(aic->fifo))) return 0; av_new_packet(pkt, size); av_fifo_generic_read(aic->fifo, pkt->data, size, NULL); pkt->dts = pkt->pts = aic->dts; pkt->duration = av_rescale_q(*aic->samples, st->time_base, aic->time_base); pkt->stream_index = stream_index; aic->dts += pkt->duration; aic->samples++; if (!*aic->samples) aic->samples = aic->samples_per_frame; return size; }
int CacheHttp_GetBufferPercentage(void *_handle,int* per) { int ret = 0; int total = 0; if(!_handle) { return AVERROR(EIO); } CacheHttpContext * s = (CacheHttpContext *)_handle; pthread_mutex_lock(&s->read_mutex); total = av_fifo_size(s->fifo); *per = (int)total*100/CIRCULAR_BUFFER_SIZE; *per = ret; pthread_mutex_unlock(&s->read_mutex); return 0; }
static int request_frame(AVFilterLink *link) { BufferSourceContext *c = link->src->priv; AVFrame *frame; if (!av_fifo_size(c->fifo)) { if (c->eof) return AVERROR_EOF; c->nb_failed_requests++; return AVERROR(EAGAIN); } av_fifo_generic_read(c->fifo, &frame, sizeof(frame), NULL); return ff_filter_frame(link, frame); }
static int udp_read(URLContext *h, uint8_t *buf, int size) { UDPContext *s = h->priv_data; int ret; int avail; #if HAVE_PTHREADS if (s->fifo) { pthread_mutex_lock(&s->mutex); do { avail = av_fifo_size(s->fifo); if (avail) { // >=size) { uint8_t tmp[4]; pthread_mutex_unlock(&s->mutex); av_fifo_generic_read(s->fifo, tmp, 4, NULL); avail= AV_RL32(tmp); if(avail > size){ av_log(h, AV_LOG_WARNING, "Part of datagram lost due to insufficient buffer size\n"); avail= size; } av_fifo_generic_read(s->fifo, buf, avail, NULL); av_fifo_drain(s->fifo, AV_RL32(tmp) - avail); return avail; } else if(s->circular_buffer_error){ pthread_mutex_unlock(&s->mutex); return s->circular_buffer_error; } else if(h->flags & AVIO_FLAG_NONBLOCK) { pthread_mutex_unlock(&s->mutex); return AVERROR(EAGAIN); } else { pthread_cond_wait(&s->cond, &s->mutex); } } while( 1); } #endif if (!(h->flags & AVIO_FLAG_NONBLOCK)) { ret = ff_network_wait_fd(s->udp_fd, 0); if (ret < 0) return ret; } ret = recv(s->udp_fd, buf, size, 0); return ret < 0 ? ff_neterrno() : ret; }