static int CUDAAPI cuvid_handle_picture_display(void *opaque, CUVIDPARSERDISPINFO* dispinfo) { AVCodecContext *avctx = opaque; CuvidContext *ctx = avctx->priv_data; CuvidParsedFrame parsed_frame = { { 0 } }; parsed_frame.dispinfo = *dispinfo; ctx->internal_error = 0; // For some reason, dispinfo->progressive_frame is sometimes wrong. parsed_frame.dispinfo.progressive_frame = ctx->progressive_sequence; if (ctx->deint_mode_current == cudaVideoDeinterlaceMode_Weave) { av_fifo_generic_write(ctx->frame_queue, &parsed_frame, sizeof(CuvidParsedFrame), NULL); } else { parsed_frame.is_deinterlacing = 1; av_fifo_generic_write(ctx->frame_queue, &parsed_frame, sizeof(CuvidParsedFrame), NULL); if (!ctx->drop_second_field) { parsed_frame.second_field = 1; av_fifo_generic_write(ctx->frame_queue, &parsed_frame, sizeof(CuvidParsedFrame), NULL); } } return 1; }
int ff_qsv_decode(AVCodecContext *avctx, QSVContext *q, AVFrame *frame, int *got_frame, AVPacket *avpkt) { AVPacket pkt_ref = { 0 }; int ret = 0; if (q->pkt_fifo && av_fifo_size(q->pkt_fifo) >= sizeof(AVPacket)) { /* we already have got some buffered packets. so add new to tail */ ret = av_packet_ref(&pkt_ref, avpkt); if (ret < 0) return ret; av_fifo_generic_write(q->pkt_fifo, &pkt_ref, sizeof(pkt_ref), NULL); } if (q->reinit_pending) { ret = do_qsv_decode(avctx, q, frame, got_frame, avpkt); if (!*got_frame) { /* Flushing complete, no more frames */ close_decoder(q); //return ff_qsv_decode(avctx, q, frame, got_frame, avpkt); } } if (!q->reinit_pending) { if (q->pkt_fifo && av_fifo_size(q->pkt_fifo) >= sizeof(AVPacket)) { /* process buffered packets */ while (!*got_frame && av_fifo_size(q->pkt_fifo) >= sizeof(AVPacket)) { av_fifo_generic_read(q->pkt_fifo, &pkt_ref, sizeof(pkt_ref), NULL); ret = do_qsv_decode(avctx, q, frame, got_frame, &pkt_ref); if (q->reinit_pending) { /* A rare case: new reinit pending when buffering existing. We should to return the pkt_ref back to same place of fifo */ qsv_packet_push_front(q, &pkt_ref); } else { av_packet_unref(&pkt_ref); } } } else { /* general decoding */ ret = do_qsv_decode(avctx, q, frame, got_frame, avpkt); if (q->reinit_pending) { ret = av_packet_ref(&pkt_ref, avpkt); if (ret < 0) return ret; av_fifo_generic_write(q->pkt_fifo, &pkt_ref, sizeof(pkt_ref), NULL); } } } return ret; }
int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples) { int i, ret, size; /* automatically reallocate buffers if needed */ if (av_audio_fifo_space(af) < nb_samples) { int current_size = av_audio_fifo_size(af); /* check for integer overflow in new size calculation */ if (INT_MAX / 2 - current_size < nb_samples) return AVERROR(EINVAL); /* reallocate buffers */ if ((ret = av_audio_fifo_realloc(af, 2 * (current_size + nb_samples))) < 0) return ret; } size = nb_samples * af->sample_size; for (i = 0; i < af->nb_buffers; i++) { ret = av_fifo_generic_write(af->buf[i], data[i], size, NULL); if (ret != size) return AVERROR_BUG; } af->nb_samples += nb_samples; return nb_samples; }
bool ExportFFmpeg::EncodeAudioFrame(int16_t *pFrame, int frameSize) { AVPacket pkt; int nBytesToWrite = 0; uint8_t *pRawSamples = NULL; int nAudioFrameSizeOut = default_frame_size * mEncAudioCodecCtx->channels * sizeof(int16_t); int ret; nBytesToWrite = frameSize; pRawSamples = (uint8_t*)pFrame; av_fifo_realloc2(mEncAudioFifo, av_fifo_size(mEncAudioFifo) + frameSize); // Put the raw audio samples into the FIFO. ret = av_fifo_generic_write(mEncAudioFifo, pRawSamples, nBytesToWrite,NULL); wxASSERT(ret == nBytesToWrite); if (nAudioFrameSizeOut > mEncAudioFifoOutBufSiz) { wxLogError(wxT("FFmpeg : ERROR - nAudioFrameSizeOut too large.")); return false; } // Read raw audio samples out of the FIFO in nAudioFrameSizeOut byte-sized groups to encode. while ((ret = av_fifo_size(mEncAudioFifo)) >= nAudioFrameSizeOut) { ret = av_fifo_generic_read(mEncAudioFifo, mEncAudioFifoOutBuf, nAudioFrameSizeOut, NULL); av_init_packet(&pkt); int ret= encode_audio(mEncAudioCodecCtx, &pkt, // out (int16_t*)mEncAudioFifoOutBuf, // in default_frame_size); if (ret < 0) { wxLogError(wxT("FFmpeg : ERROR - Can't encode audio frame.")); return false; } if (ret == 0) continue; // Rescale from the codec time_base to the AVStream time_base. if (pkt.pts != int64_t(AV_NOPTS_VALUE)) pkt.pts = av_rescale_q(pkt.pts, mEncAudioCodecCtx->time_base, mEncAudioStream->time_base); if (pkt.dts != int64_t(AV_NOPTS_VALUE)) pkt.dts = av_rescale_q(pkt.dts, mEncAudioCodecCtx->time_base, mEncAudioStream->time_base); //wxLogDebug(wxT("FFmpeg : (%d) Writing audio frame with PTS: %lld."), mEncAudioCodecCtx->frame_number, (long long) pkt.pts); pkt.stream_index = mEncAudioStream->index; // Write the encoded audio frame to the output file. if ((ret = av_interleaved_write_frame(mEncFormatCtx, &pkt)) < 0) { wxLogError(wxT("FFmpeg : ERROR - Failed to write audio frame to file.")); return false; } av_free_packet(&pkt); } return true; }
int dc_audio_encoder_read(AudioOutputFile * p_aout, AudioInputData * p_aind) { int ret; AudioDataNode * p_adn; ret = dc_consumer_lock(&p_aout->acon, &p_aind->p_cb); if (ret < 0) { #ifdef DEBUG printf("Audio encoder got to end of buffer!\n"); #endif return -2; } dc_consumer_unlock_previous(&p_aout->acon, &p_aind->p_cb); p_adn = (AudioDataNode *) dc_consumer_consume(&p_aout->acon, &p_aind->p_cb); /* Write audio sample on fifo */ // av_fifo_generic_write(p_aout->p_fifo, p_adn->p_aframe->data[0], // p_adn->p_aframe->linesize[0], NULL); av_fifo_generic_write(p_aout->p_fifo, p_adn->p_abuf, p_adn->i_abuf_size, NULL); dc_consumer_advance(&p_aout->acon); return 0; }
static int end_frame(AVFilterLink *inlink) { AVFilterContext *ctx = inlink->dst; BufferSinkContext *buf = inlink->dst->priv; av_assert1(inlink->cur_buf); if (av_fifo_space(buf->fifo) < sizeof(AVFilterBufferRef *)) { /* realloc fifo size */ if (av_fifo_realloc2(buf->fifo, av_fifo_size(buf->fifo) * 2) < 0) { av_log(ctx, AV_LOG_ERROR, "Cannot buffer more frames. Consume some available frames " "before adding new ones.\n"); return AVERROR(ENOMEM); } } /* cache frame */ av_fifo_generic_write(buf->fifo, &inlink->cur_buf, sizeof(AVFilterBufferRef *), NULL); inlink->cur_buf = NULL; if (buf->warning_limit && av_fifo_size(buf->fifo) / sizeof(AVFilterBufferRef *) >= buf->warning_limit) { av_log(ctx, AV_LOG_WARNING, "%d buffers queued in %s, something may be wrong.\n", buf->warning_limit, (char *)av_x_if_null(ctx->name, ctx->filter->name)); buf->warning_limit *= 10; } return 0; }
int dc_audio_encoder_read(AudioOutputFile *audio_output_file, AudioInputData *audio_input_data) { int ret; AudioDataNode *audio_data_node; ret = dc_consumer_lock(&audio_output_file->consumer, &audio_input_data->circular_buf); if (ret < 0) { GF_LOG(GF_LOG_ERROR, GF_LOG_DASH, ("Audio encoder got an end of buffer!\n")); return -2; } dc_consumer_unlock_previous(&audio_output_file->consumer, &audio_input_data->circular_buf); audio_data_node = (AudioDataNode *) dc_consumer_consume(&audio_output_file->consumer, &audio_input_data->circular_buf); #ifndef GPAC_USE_LIBAV audio_output_file->aframe->channels = audio_output_file->codec_ctx->channels; #endif #ifndef LIBAV_FRAME_OLD audio_output_file->aframe->channel_layout = audio_output_file->codec_ctx->channel_layout; audio_output_file->aframe->sample_rate = audio_output_file->codec_ctx->sample_rate; #endif audio_output_file->aframe->format = audio_output_file->codec_ctx->sample_fmt; /* Write audio sample on fifo */ av_fifo_generic_write(audio_output_file->fifo, audio_data_node->abuf, audio_data_node->abuf_size, NULL); dc_consumer_advance(&audio_output_file->consumer); return 0; }
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; }
static int gxf_interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush) { GXFContext *gxf = s->priv_data; AVPacket new_pkt; int i; for (i = 0; i < s->nb_streams; i++) { AVStream *st = s->streams[i]; GXFStreamContext *sc = &gxf->streams[i]; if (st->codec->codec_type == CODEC_TYPE_AUDIO) { if (pkt && pkt->stream_index == i) { av_fifo_generic_write(&sc->audio_buffer, pkt->data, pkt->size, NULL); pkt = NULL; } if (flush || av_fifo_size(&sc->audio_buffer) >= GXF_AUDIO_PACKET_SIZE) { if (!pkt && gxf_new_audio_packet(gxf, sc, &new_pkt, flush) > 0) { pkt = &new_pkt; break; /* add pkt right now into list */ } } } else if (pkt && pkt->stream_index == i) { if (sc->dts_delay == GXF_NODELAY) /* adjust dts if needed */ sc->dts_delay = pkt->dts; pkt->dts -= sc->dts_delay; } } return av_interleave_packet_per_dts(s, out, pkt, flush); }
status_t AVCodecEncoder::_EncodeAudio(const void* _buffer, int64 frameCount, media_encode_info* info) { TRACE("AVCodecEncoder::_EncodeAudio(%p, %lld, %p)\n", _buffer, frameCount, info); if (fChunkBuffer == NULL) return B_NO_MEMORY; status_t ret = B_OK; const uint8* buffer = reinterpret_cast<const uint8*>(_buffer); size_t inputSampleSize = fInputFormat.u.raw_audio.format & media_raw_audio_format::B_AUDIO_SIZE_MASK; size_t inputFrameSize = inputSampleSize * fInputFormat.u.raw_audio.channel_count; size_t bufferSize = frameCount * inputFrameSize; bufferSize = min_c(bufferSize, kDefaultChunkBufferSize); if (fContext->frame_size > 1) { // Encoded audio. Things work differently from raw audio. We need // the fAudioFifo to pipe data. if (av_fifo_realloc2(fAudioFifo, av_fifo_size(fAudioFifo) + bufferSize) < 0) { TRACE(" av_fifo_realloc2() failed\n"); return B_NO_MEMORY; } av_fifo_generic_write(fAudioFifo, const_cast<uint8*>(buffer), bufferSize, NULL); int frameBytes = fContext->frame_size * inputFrameSize; uint8* tempBuffer = new(std::nothrow) uint8[frameBytes]; if (tempBuffer == NULL) return B_NO_MEMORY; // Encode as many chunks as can be read from the FIFO. while (av_fifo_size(fAudioFifo) >= frameBytes) { av_fifo_generic_read(fAudioFifo, tempBuffer, frameBytes, NULL); ret = _EncodeAudio(tempBuffer, frameBytes, fContext->frame_size, info); if (ret != B_OK) break; } delete[] tempBuffer; } else { // Raw audio. The number of bytes returned from avcodec_encode_audio() // is always the same as the number of input bytes. return _EncodeAudio(buffer, bufferSize, frameCount, info); } return ret; }
static void *circular_buffer_task( void *_URLContext) { URLContext *h = _URLContext; UDPContext *s = h->priv_data; int old_cancelstate; pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate); pthread_mutex_lock(&s->mutex); if (ff_socket_nonblock(s->udp_fd, 0) < 0) { av_log(h, AV_LOG_ERROR, "Failed to set blocking mode"); s->circular_buffer_error = AVERROR(EIO); goto end; } while(1) { int len; pthread_mutex_unlock(&s->mutex); /* Blocking operations are always cancellation points; see "General Information" / "Thread Cancelation Overview" in Single Unix. */ pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_cancelstate); len = recv(s->udp_fd, s->tmp+4, sizeof(s->tmp)-4, 0); pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate); pthread_mutex_lock(&s->mutex); if (len < 0) { if (ff_neterrno() != AVERROR(EAGAIN) && ff_neterrno() != AVERROR(EINTR)) { s->circular_buffer_error = ff_neterrno(); goto end; } continue; } AV_WL32(s->tmp, len); if(av_fifo_space(s->fifo) < len + 4) { /* No Space left */ if (s->overrun_nonfatal) { av_log(h, AV_LOG_WARNING, "Circular buffer overrun. " "Surviving due to overrun_nonfatal option\n"); continue; } else { av_log(h, AV_LOG_ERROR, "Circular buffer overrun. " "To avoid, increase fifo_size URL option. " "To survive in such case, use overrun_nonfatal option\n"); s->circular_buffer_error = AVERROR(EIO); goto end; } } av_fifo_generic_write(s->fifo, s->tmp, len+4, NULL); pthread_cond_signal(&s->cond); } end: pthread_cond_signal(&s->cond); pthread_mutex_unlock(&s->mutex); return NULL; }
static int CUDAAPI cuvid_handle_picture_display(void *opaque, CUVIDPARSERDISPINFO* dispinfo) { AVCodecContext *avctx = opaque; CuvidContext *ctx = avctx->priv_data; CuvidParsedFrame parsed_frame = { *dispinfo, 0, 0 }; ctx->internal_error = 0; if (ctx->deint_mode == cudaVideoDeinterlaceMode_Weave) { av_fifo_generic_write(ctx->frame_queue, &parsed_frame, sizeof(CuvidParsedFrame), NULL); } else { parsed_frame.is_deinterlacing = 1; av_fifo_generic_write(ctx->frame_queue, &parsed_frame, sizeof(CuvidParsedFrame), NULL); parsed_frame.second_field = 1; av_fifo_generic_write(ctx->frame_queue, &parsed_frame, sizeof(CuvidParsedFrame), NULL); } return 1; }
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; }
/* 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 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 dv_assemble_frame(DVMuxContext *c, AVStream* st, uint8_t* data, int data_size, uint8_t** frame) { int i, reqasize; *frame = &c->frame_buf[0]; reqasize = 4 * dv_audio_frame_size(c->sys, c->frames); switch (st->codec->codec_type) { case AVMEDIA_TYPE_VIDEO: /* FIXME: we have to have more sensible approach than this one */ if (c->has_video) av_log(st->codec, AV_LOG_ERROR, "Can't process DV frame #%d. Insufficient audio data or severe sync problem.\n", c->frames); memcpy(*frame, data, c->sys->frame_size); c->has_video = 1; break; case AVMEDIA_TYPE_AUDIO: for (i = 0; i < c->n_ast && st != c->ast[i]; i++); /* FIXME: we have to have more sensible approach than this one */ if (av_fifo_size(c->audio_data[i]) + data_size >= 100*AVCODEC_MAX_AUDIO_FRAME_SIZE) av_log(st->codec, AV_LOG_ERROR, "Can't process DV frame #%d. Insufficient video data or severe sync problem.\n", c->frames); av_fifo_generic_write(c->audio_data[i], data, data_size, NULL); /* Let us see if we've got enough audio for one DV frame. */ c->has_audio |= ((reqasize <= av_fifo_size(c->audio_data[i])) << i); break; default: break; } /* Let us see if we have enough data to construct one DV frame. */ if (c->has_video == 1 && c->has_audio + 1 == 1 << c->n_ast) { dv_inject_metadata(c, *frame); c->has_audio = 0; for (i=0; i < c->n_ast; i++) { dv_inject_audio(c, i, *frame); av_fifo_drain(c->audio_data[i], reqasize); c->has_audio |= ((reqasize <= av_fifo_size(c->audio_data[i])) << i); } c->has_video = 0; c->frames++; return c->sys->frame_size; } return 0; }
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; }
static int CUDAAPI cuvid_handle_picture_display(void *opaque, CUVIDPARSERDISPINFO* dispinfo) { AVCodecContext *avctx = opaque; CuvidContext *ctx = avctx->priv_data; av_log(avctx, AV_LOG_TRACE, "pfnDisplayPicture\n"); ctx->internal_error = 0; av_fifo_generic_write(ctx->frame_queue, dispinfo, sizeof(CUVIDPARSERDISPINFO), NULL); return 1; }
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; }
static int play(struct ao *ao, void *data, int len, int flags) { struct priv *priv = ao->priv; SDL_LockMutex(priv->buffer_mutex); int free = av_fifo_space(priv->buffer); if (len > free) len = free; av_fifo_generic_write(priv->buffer, data, len, NULL); SDL_CondSignal(priv->underrun_cond); SDL_UnlockMutex(priv->buffer_mutex); if (priv->unpause) { priv->unpause = 0; do_resume(ao); } return len; }
static int av_thread_message_queue_send_locked(AVThreadMessageQueue *mq, void *msg, unsigned flags) { while (!mq->err_send && av_fifo_space(mq->fifo) < mq->elsize) { if ((flags & AV_THREAD_MESSAGE_NONBLOCK)) return AVERROR(EAGAIN); pthread_cond_wait(&mq->cond, &mq->lock); } if (mq->err_send) return mq->err_send; av_fifo_generic_write(mq->fifo, msg, mq->elsize, NULL); pthread_cond_signal(&mq->cond); return 0; }
int FFMpegFifo::write(unsigned char* src,int writeSize) { //SDL_LockMutex(mutex); if (av_fifo_space(fifo) < writeSize) { waitWriteBytes = writeSize; //SDL_CondWait(condWrite,mutex); } int ret = av_fifo_generic_write(fifo,src,writeSize,NULL); /* if (av_fifo_size(fifo) >= waitReadBytes) SDL_CondSignal(condRead); */ //SDL_UnlockMutex(mutex); return ret; }
static int supply_new_packets(JackData *self, AVFormatContext *context) { AVPacket pkt; int test, pkt_size = self->buffer_size * self->nports * sizeof(float); /* Supply the process callback with new empty packets, by filling the new * packets FIFO buffer with as many packets as possible. process_callback() * can't do this by itself, because it can't allocate memory in realtime. */ while (av_fifo_space(self->new_pkts) >= sizeof(pkt)) { if ((test = av_new_packet(&pkt, pkt_size)) < 0) { av_log(context, AV_LOG_ERROR, "Could not create packet of size %d\n", pkt_size); return test; } av_fifo_generic_write(self->new_pkts, &pkt, sizeof(pkt), NULL); } return 0; }
int av_buffersrc_buffer(AVFilterContext *s, AVFilterBufferRef *buf) { BufferSourceContext *c = s->priv; int ret; if (!av_fifo_space(c->fifo) && (ret = av_fifo_realloc2(c->fifo, av_fifo_size(c->fifo) + sizeof(buf))) < 0) return ret; // CHECK_PARAM_CHANGE(s, c, buf->video->w, buf->video->h, buf->format); if ((ret = av_fifo_generic_write(c->fifo, &buf, sizeof(buf), NULL)) < 0) return ret; return 0; }
static int add_buffer_ref(AVFilterContext *ctx, AVFrame *ref) { BufferSinkContext *buf = ctx->priv; if (av_fifo_space(buf->fifo) < sizeof(AVFilterBufferRef *)) { /* realloc fifo size */ if (av_fifo_realloc2(buf->fifo, av_fifo_size(buf->fifo) * 2) < 0) { av_log(ctx, AV_LOG_ERROR, "Cannot buffer more frames. Consume some available frames " "before adding new ones.\n"); return AVERROR(ENOMEM); } } /* cache frame */ av_fifo_generic_write(buf->fifo, &ref, sizeof(AVFilterBufferRef *), NULL); return 0; }
static void end_frame(AVFilterLink *inlink) { AVFilterContext *ctx = inlink->dst; BufferSinkContext *buf = inlink->dst->priv; if (av_fifo_space(buf->fifo) < sizeof(AVFilterBufferRef *)) { /* realloc fifo size */ if (av_fifo_realloc2(buf->fifo, av_fifo_size(buf->fifo) * 2) < 0) { av_log(ctx, AV_LOG_ERROR, "Cannot buffer more frames. Consume some available frames " "before adding new ones.\n"); return; } } /* cache frame */ av_fifo_generic_write(buf->fifo, &inlink->cur_buf, sizeof(AVFilterBufferRef *), NULL); }
static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref) { SelectContext *select = inlink->dst->priv; select->select = select_frame(inlink->dst, picref); if (select->select) { /* frame was requested through poll_frame */ if (select->cache_frames) { if (!av_fifo_space(select->pending_frames)) av_log(inlink->dst, AV_LOG_ERROR, "Buffering limit reached, cannot cache more frames\n"); else av_fifo_generic_write(select->pending_frames, &picref, sizeof(picref), NULL); return; } avfilter_start_frame(inlink->dst->outputs[0], avfilter_ref_buffer(picref, ~0)); } }
int ff_audio_rechunk_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush, int (*get_packet)(AVFormatContext *, AVPacket *, AVPacket *, int), int (*compare_ts)(AVFormatContext *, AVPacket *, AVPacket *)) { int i, ret; if (pkt) { AVStream *st = s->streams[pkt->stream_index]; AudioInterleaveContext *aic = st->priv_data; if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) { unsigned new_size = av_fifo_size(aic->fifo) + pkt->size; if (new_size > aic->fifo_size) { if (av_fifo_realloc2(aic->fifo, new_size) < 0) return AVERROR(ENOMEM); aic->fifo_size = new_size; } av_fifo_generic_write(aic->fifo, pkt->data, pkt->size, NULL); } else { // rewrite pts and dts to be decoded time line position pkt->pts = pkt->dts = aic->dts; aic->dts += pkt->duration; if ((ret = ff_interleave_add_packet(s, pkt, compare_ts)) < 0) return ret; } pkt = NULL; } for (i = 0; i < s->nb_streams; i++) { AVStream *st = s->streams[i]; if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) { AVPacket new_pkt = { 0 }; while ((ret = interleave_new_audio_packet(s, &new_pkt, i, flush)) > 0) { if ((ret = ff_interleave_add_packet(s, &new_pkt, compare_ts)) < 0) return ret; } if (ret < 0) return ret; } } return get_packet(s, out, NULL, flush); }
static int swf_write_audio(AVFormatContext *s, AVCodecContext *enc, uint8_t *buf, int size) { SWFContext *swf = s->priv_data; /* Flash Player limit */ if (swf->swf_frame_number == 16000) av_log(enc, AV_LOG_INFO, "warning: Flash Player limit of 16000 frames reached\n"); if (av_fifo_size(&swf->audio_fifo) + size > AUDIO_FIFO_SIZE) { av_log(s, AV_LOG_ERROR, "audio fifo too small to mux audio essence\n"); return -1; } av_fifo_generic_write(&swf->audio_fifo, buf, size, NULL); swf->sound_samples += enc->frame_size; /* if audio only stream make sure we add swf frames */ if (!swf->video_enc) swf_write_video(s, enc, 0, 0); return 0; }