STDMETHODIMP CDecAvcodec::Flush() { if (m_pAVCtx && avcodec_is_open(m_pAVCtx)) { avcodec_flush_buffers(m_pAVCtx); } if (m_pParser) { av_parser_close(m_pParser); m_pParser = av_parser_init(m_nCodecId); } m_CurrentThread = 0; m_rtStartCache = AV_NOPTS_VALUE; m_bWaitingForKeyFrame = TRUE; m_nSoftTelecine = 0; m_nBFramePos = 0; m_tcBFrameDelay[0].rtStart = m_tcBFrameDelay[0].rtStop = AV_NOPTS_VALUE; m_tcBFrameDelay[1].rtStart = m_tcBFrameDelay[1].rtStop = AV_NOPTS_VALUE; if (!(m_pCallback->GetDecodeFlags() & LAV_VIDEO_DEC_FLAG_DVD) && (m_nCodecId == AV_CODEC_ID_H264 || m_nCodecId == AV_CODEC_ID_MPEG2VIDEO)) { CDecAvcodec::InitDecoder(m_nCodecId, &m_pCallback->GetInputMediaType()); } return __super::Flush(); }
bool FF_Format_Stream_Decoder::InitializeDecoder(int index,int threads) { if (index == -1 || pFormatContext == NULL) return false; if (index > pFormatContext->nb_streams) return false; if (pCodec != NULL) if (avcodec_is_open(pCodecContext) >= 0) return false; pStream = pFormatContext->streams[index]; if (pStream == NULL) return false; pCodecContext = pStream->codec; if (pCodecContext == NULL) return false; pCodec = avcodec_find_decoder(pCodecContext->codec_id); if (pCodec == NULL) return false; if (threads > 0) { pCodecContext->thread_count = threads; pCodecContext->active_thread_type = FF_THREAD_FRAME; } int result = avcodec_open2(pCodecContext,pCodec,NULL); return result < 0 ? false:true; }
int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame) { AVCodecInternal *avci = avctx->internal; int ret; av_frame_unref(frame); if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec)) return AVERROR(EINVAL); ret = bsfs_init(avctx); if (ret < 0) return ret; if (avci->buffer_frame->buf[0]) { av_frame_move_ref(frame, avci->buffer_frame); } else { ret = decode_receive_frame_internal(avctx, frame); if (ret < 0) return ret; } if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) { ret = apply_cropping(avctx, frame); if (ret < 0) { av_frame_unref(frame); return ret; } } avctx->frame_number++; return 0; }
int AudioEncode::encode(AVPacket * pkt, const uint8_t ** data, int nb_samples) { if (mBitRate == 0) mBitRate = defaultBitRate(); if (mChannelLayout == 0) { mChannelLayout = defaultChannelLayout(); } else { mChannelLayout = checkChannelLayout(mChannelLayout); } if (mSampleFmt == AV_SAMPLE_FMT_NONE) { mSampleFmt = defaultSampleFmt(); } else { mSampleFmt = checkSampleFmt(mSampleFmt); } if (mSampleRate == 0) mSampleRate = defaultSampleRate(); mCodecCtx->bit_rate = mBitRate; mCodecCtx->channel_layout = mChannelLayout; mCodecCtx->sample_fmt = mSampleFmt; mCodecCtx->sample_rate = mSampleRate; mCodecCtx->flags |= mFlags; if (!avcodec_is_open(mCodecCtx)) { avcodec_open2(mCodecCtx, mCodec, NULL); } mState = ENCODE_STATE_RUN; return doEncode(pkt, data, nb_samples); }
int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src) { if (avcodec_is_open(dest)) { // check that the dest context is uninitialized av_log(dest, AV_LOG_ERROR, "Tried to copy AVCodecContext %p into already-initialized %p\n", src, dest); return AVERROR(EINVAL); } av_opt_free(dest); av_free(dest->priv_data); memcpy(dest, src, sizeof(*dest)); /* set values specific to opened codecs back to their default state */ dest->priv_data = NULL; dest->codec = NULL; dest->slice_offset = NULL; dest->hwaccel = NULL; dest->internal = NULL; /* reallocate values that should be allocated separately */ dest->rc_eq = NULL; dest->extradata = NULL; dest->intra_matrix = NULL; dest->inter_matrix = NULL; dest->rc_override = NULL; if (src->rc_eq) { dest->rc_eq = av_strdup(src->rc_eq); if (!dest->rc_eq) return AVERROR(ENOMEM); } #define alloc_and_copy_or_fail(obj, size, pad) \ if (src->obj && size > 0) { \ dest->obj = av_malloc(size + pad); \ if (!dest->obj) \ goto fail; \ memcpy(dest->obj, src->obj, size); \ if (pad) \ memset(((uint8_t *) dest->obj) + size, 0, pad); \ } alloc_and_copy_or_fail(extradata, src->extradata_size, FF_INPUT_BUFFER_PADDING_SIZE); alloc_and_copy_or_fail(intra_matrix, 64 * sizeof(int16_t), 0); alloc_and_copy_or_fail(inter_matrix, 64 * sizeof(int16_t), 0); alloc_and_copy_or_fail(rc_override, src->rc_override_count * sizeof(*src->rc_override), 0); alloc_and_copy_or_fail(subtitle_header, src->subtitle_header_size, 1); #undef alloc_and_copy_or_fail return 0; fail: av_freep(&dest->rc_override); av_freep(&dest->intra_matrix); av_freep(&dest->inter_matrix); av_freep(&dest->extradata); av_freep(&dest->rc_eq); return AVERROR(ENOMEM); }
void AudioEncode::close() { mState = ENCODE_STATE_CLOSE; if (mCodecCtx && avcodec_is_open(mCodecCtx)) { avcodec_close(mCodecCtx); } av_freep(&mData[0]); av_free(mCodecCtx); av_audio_fifo_free(mFifo); }
FF_Format_Stream_Decoder::~FF_Format_Stream_Decoder() { if (pCodec) { if (avcodec_is_open(pCodecContext) >= 0) avcodec_close(pCodecContext); } if (pMyWrapper) pMyWrapper->Unref(); }
int ff_mp4_read_dec_config_descr(AVFormatContext *fc, AVStream *st, AVIOContext *pb) { enum AVCodecID codec_id; unsigned v; int len, tag; int ret; int object_type_id = avio_r8(pb); avio_r8(pb); /* stream type */ avio_rb24(pb); /* buffer size db */ if(avcodec_is_open(st->codec)) { av_log(fc, AV_LOG_DEBUG, "codec open in read_dec_config_descr\n"); return -1; } v = avio_rb32(pb); if (v < INT32_MAX) st->codec->rc_max_rate = v; st->codec->bit_rate = avio_rb32(pb); /* avg bitrate */ codec_id= ff_codec_get_id(ff_mp4_obj_type, object_type_id); if (codec_id) st->codec->codec_id= codec_id; av_log(fc, AV_LOG_TRACE, "esds object type id 0x%02x\n", object_type_id); len = ff_mp4_read_descr(fc, pb, &tag); if (tag == MP4DecSpecificDescrTag) { av_log(fc, AV_LOG_TRACE, "Specific MPEG4 header len=%d\n", len); if (!len || (uint64_t)len > (1<<30)) return -1; av_free(st->codec->extradata); if ((ret = ff_get_extradata(st->codec, pb, len)) < 0) return ret; if (st->codec->codec_id == AV_CODEC_ID_AAC) { MPEG4AudioConfig cfg = {0}; avpriv_mpeg4audio_get_config(&cfg, st->codec->extradata, st->codec->extradata_size * 8, 1); st->codec->channels = cfg.channels; if (cfg.object_type == 29 && cfg.sampling_index < 3) // old mp3on4 st->codec->sample_rate = avpriv_mpa_freq_tab[cfg.sampling_index]; else if (cfg.ext_sample_rate) st->codec->sample_rate = cfg.ext_sample_rate; else st->codec->sample_rate = cfg.sample_rate; av_log(fc, AV_LOG_TRACE, "mp4a config channels %d obj %d ext obj %d " "sample rate %d ext sample rate %d\n", st->codec->channels, cfg.object_type, cfg.ext_object_type, cfg.sample_rate, cfg.ext_sample_rate); if (!(st->codec->codec_id = ff_codec_get_id(mp4_audio_types, cfg.object_type))) st->codec->codec_id = AV_CODEC_ID_AAC; } } return 0; }
STDMETHODIMP CDecD3D11::BreakConnect() { if (m_bReadBackFallback) return S_FALSE; // release any resources held by the core m_pCallback->ReleaseAllDXVAResources(); // flush all buffers out of the decoder to ensure the allocator can be properly de-allocated if (m_pAVCtx && avcodec_is_open(m_pAVCtx)) avcodec_flush_buffers(m_pAVCtx); return S_OK; }
int ff_mp4_read_dec_config_descr(AVFormatContext *fc, AVStream *st, AVIOContext *pb) { int len, tag; int object_type_id = avio_r8(pb); avio_r8(pb); /* stream type */ avio_rb24(pb); /* buffer size db */ avio_rb32(pb); /* max bitrate */ avio_rb32(pb); /* avg bitrate */ if(avcodec_is_open(st->codec)) { av_log(fc, AV_LOG_DEBUG, "codec open in read_dec_config_descr\n"); return -1; } st->codec->codec_id= ff_codec_get_id(ff_mp4_obj_type, object_type_id); av_dlog(fc, "esds object type id 0x%02x\n", object_type_id); len = ff_mp4_read_descr(fc, pb, &tag); if (tag == MP4DecSpecificDescrTag) { av_dlog(fc, "Specific MPEG4 header len=%d\n", len); if (!len || (uint64_t)len > (1<<30)) return -1; av_free(st->codec->extradata); st->codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE); if (!st->codec->extradata) return AVERROR(ENOMEM); avio_read(pb, st->codec->extradata, len); st->codec->extradata_size = len; if (st->codec->codec_id == AV_CODEC_ID_AAC) { MPEG4AudioConfig cfg; avpriv_mpeg4audio_get_config(&cfg, st->codec->extradata, st->codec->extradata_size * 8, 1); st->codec->channels = cfg.channels; if (cfg.object_type == 29 && cfg.sampling_index < 3) // old mp3on4 st->codec->sample_rate = avpriv_mpa_freq_tab[cfg.sampling_index]; else if (cfg.ext_sample_rate) st->codec->sample_rate = cfg.ext_sample_rate; else st->codec->sample_rate = cfg.sample_rate; av_dlog(fc, "mp4a config channels %d obj %d ext obj %d " "sample rate %d ext sample rate %d\n", st->codec->channels, cfg.object_type, cfg.ext_object_type, cfg.sample_rate, cfg.ext_sample_rate); if (!(st->codec->codec_id = ff_codec_get_id(mp4_audio_types, cfg.object_type))) st->codec->codec_id = AV_CODEC_ID_AAC; } } return 0; }
ffdec_error ffdec_close(ffdec_context *ffd_context) { AVCodecContext *codec_context = ffd_context->codec_context; if (codec_context) { if (avcodec_is_open(codec_context)) { avcodec_close(codec_context); } av_free(codec_context); codec_context = ffd_context->codec_context = NULL; } return FFDEC_OK; }
int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt) { AVCodecInternal *avci = avctx->internal; int ret = 0; if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec)) return AVERROR(EINVAL); if (avctx->internal->draining) return AVERROR_EOF; ret = bsfs_init(avctx); if (ret < 0) return ret; av_packet_unref(avci->buffer_pkt); if (avpkt && (avpkt->data || avpkt->side_data_elems)) { ret = av_packet_ref(avci->buffer_pkt, avpkt); if (ret < 0) return ret; } ret = av_bsf_send_packet(avci->filter.bsfs[0], avci->buffer_pkt); if (ret < 0) { av_packet_unref(avci->buffer_pkt); return ret; } if (!avci->buffer_frame->buf[0]) { ret = decode_receive_frame_internal(avctx, avci->buffer_frame); if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) return ret; } return 0; }
// Encode frame and pass to block VALUE audio_stream_decode(VALUE self) { AudioStreamInternal * internal; Data_Get_Struct(self, AudioStreamInternal, internal); // Prepare codec if (!avcodec_is_open(internal->base.stream->codec)) { AVCodec * codec = internal->base.stream->codec->codec; if (!codec) { codec = avcodec_find_decoder(internal->base.stream->codec->codec_id); } avcodec_open2(internal->base.stream->codec, codec, NULL); } // Find and decode next audio frame AVFrame * frame = avcodec_alloc_frame(); for (;;) { // Find next packet for this stream AVPacket packet; int found = reader_find_next_stream_packet(internal->base.reader, &packet, internal->base.stream->index); if (!found) { // No more packets av_free(frame); return Qnil; } // Decode audio frame int decoded = 0; int err = avcodec_decode_audio4(internal->base.stream->codec, frame, &decoded, &packet); if (err < 0) rb_raise_av_error(rb_eLoadError, err); if (decoded) { return audio_frame_new(frame, internal->base.stream->codec); } } }
bool ffStream::isOpen() { return avcodec_is_open(m_pAVStream->codec); }
int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src) { const AVCodec *orig_codec = dest->codec; uint8_t *orig_priv_data = dest->priv_data; if (avcodec_is_open(dest)) { // check that the dest context is uninitialized av_log(dest, AV_LOG_ERROR, "Tried to copy AVCodecContext %p into already-initialized %p\n", src, dest); return AVERROR(EINVAL); } av_opt_free(dest); av_freep(&dest->rc_override); av_freep(&dest->intra_matrix); av_freep(&dest->inter_matrix); av_freep(&dest->extradata); av_freep(&dest->subtitle_header); memcpy(dest, src, sizeof(*dest)); av_opt_copy(dest, src); dest->priv_data = orig_priv_data; dest->codec = orig_codec; if (orig_priv_data && src->codec && src->codec->priv_class && dest->codec && dest->codec->priv_class) av_opt_copy(orig_priv_data, src->priv_data); /* set values specific to opened codecs back to their default state */ dest->slice_offset = NULL; dest->hwaccel = NULL; dest->internal = NULL; #if FF_API_CODED_FRAME FF_DISABLE_DEPRECATION_WARNINGS dest->coded_frame = NULL; FF_ENABLE_DEPRECATION_WARNINGS #endif /* reallocate values that should be allocated separately */ dest->extradata = NULL; dest->intra_matrix = NULL; dest->inter_matrix = NULL; dest->rc_override = NULL; dest->subtitle_header = NULL; dest->hw_frames_ctx = NULL; #define alloc_and_copy_or_fail(obj, size, pad) \ if (src->obj && size > 0) { \ dest->obj = av_malloc(size + pad); \ if (!dest->obj) \ goto fail; \ memcpy(dest->obj, src->obj, size); \ if (pad) \ memset(((uint8_t *) dest->obj) + size, 0, pad); \ } alloc_and_copy_or_fail(extradata, src->extradata_size, AV_INPUT_BUFFER_PADDING_SIZE); dest->extradata_size = src->extradata_size; alloc_and_copy_or_fail(intra_matrix, 64 * sizeof(int16_t), 0); alloc_and_copy_or_fail(inter_matrix, 64 * sizeof(int16_t), 0); alloc_and_copy_or_fail(rc_override, src->rc_override_count * sizeof(*src->rc_override), 0); alloc_and_copy_or_fail(subtitle_header, src->subtitle_header_size, 1); av_assert0(dest->subtitle_header_size == src->subtitle_header_size); #undef alloc_and_copy_or_fail if (src->hw_frames_ctx) { dest->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx); if (!dest->hw_frames_ctx) goto fail; } return 0; fail: av_freep(&dest->subtitle_header); av_freep(&dest->rc_override); av_freep(&dest->intra_matrix); av_freep(&dest->inter_matrix); av_freep(&dest->extradata); av_buffer_unref(&dest->hw_frames_ctx); dest->subtitle_header_size = 0; dest->extradata_size = 0; av_opt_free(dest); return AVERROR(ENOMEM); }
void ffStream::Close() { if (avcodec_is_open(m_pAVStream->codec)) avcodec_close(m_pAVStream->codec); }
// Initialization and runtime control static int control(struct af_instance *af, int cmd, void *arg) { af_ac3enc_t *s = af->priv; static const int default_bit_rate[AC3_MAX_CHANNELS+1] = \ {0, 96000, 192000, 256000, 384000, 448000, 448000}; switch (cmd){ case AF_CONTROL_REINIT: { struct mp_audio *in = arg; struct mp_audio orig_in = *in; if (!af_fmt_is_pcm(in->format) || in->nch < s->cfg_min_channel_num) return AF_DETACH; // At least currently, the AC3 encoder doesn't export sample rates. in->rate = 48000; select_encode_format(s->lavc_actx, in); af->data->rate = in->rate; mp_audio_set_format(af->data, AF_FORMAT_S_AC3); mp_audio_set_num_channels(af->data, 2); if (!mp_audio_config_equals(in, &orig_in)) return AF_FALSE; if (s->cfg_add_iec61937_header) { s->out_samples = AC3_FRAME_SIZE; } else { s->out_samples = AC3_MAX_CODED_FRAME_SIZE / af->data->sstride; } mp_audio_copy_config(s->input, in); talloc_free(s->pending); s->pending = NULL; MP_DBG(af, "reinit: %d, %d, %d.\n", in->nch, in->rate, s->in_samples); int bit_rate = s->bit_rate ? s->bit_rate : default_bit_rate[in->nch]; if (s->lavc_actx->channels != in->nch || s->lavc_actx->sample_rate != in->rate || s->lavc_actx->bit_rate != bit_rate) { avcodec_close(s->lavc_actx); // Put sample parameters s->lavc_actx->sample_fmt = af_to_avformat(in->format); s->lavc_actx->channels = in->nch; s->lavc_actx->channel_layout = mp_chmap_to_lavc(&in->channels); s->lavc_actx->sample_rate = in->rate; s->lavc_actx->bit_rate = bit_rate; if (avcodec_open2(s->lavc_actx, s->lavc_acodec, NULL) < 0) { MP_ERR(af, "Couldn't open codec %s, br=%d.\n", "ac3", bit_rate); return AF_ERROR; } if (s->lavc_actx->frame_size < 1) { MP_ERR(af, "encoder didn't specify input frame size\n"); return AF_ERROR; } } s->in_samples = s->lavc_actx->frame_size; mp_audio_realloc(s->input, s->in_samples); s->input->samples = 0; s->encoder_buffered = 0; return AF_OK; } case AF_CONTROL_RESET: if (avcodec_is_open(s->lavc_actx)) avcodec_flush_buffers(s->lavc_actx); talloc_free(s->pending); s->pending = NULL; s->input->samples = 0; s->encoder_buffered = 0; return AF_OK; } return AF_UNKNOWN; }
ffdec_error ffdec_create_view(ffdec_context *ffd_context, QString group, QString id, screen_window_t *window) { ffdec_reserved *ffd_reserved = (ffdec_reserved*) ffd_context->reserved; if (!ffd_reserved) return FFDEC_NOT_INITIALIZED; if (ffd_reserved->view) { *window = ffd_reserved->view->screen_window; return FFDEC_OK; } AVCodecContext *codec_context = ffd_context->codec_context; if (!codec_context) return FFDEC_NO_CODEC_SPECIFIED; if (!avcodec_is_open(codec_context)) return FFDEC_CODEC_NOT_OPEN; ffdec_view *view = (ffdec_view*) malloc(sizeof(ffdec_view)); memset(view, 0, sizeof(ffdec_view)); QByteArray groupArr = group.toAscii(); QByteArray idArr = id.toAscii(); screen_context_t screen_context; screen_create_context(&screen_context, SCREEN_APPLICATION_CONTEXT); screen_window_t screen_window; screen_create_window_type(&screen_window, screen_context, SCREEN_CHILD_WINDOW); screen_join_window_group(screen_window, groupArr.constData()); screen_set_window_property_cv(screen_window, SCREEN_PROPERTY_ID_STRING, idArr.length(), idArr.constData()); int usage = SCREEN_USAGE_NATIVE; screen_set_window_property_iv(screen_window, SCREEN_PROPERTY_USAGE, &usage); int video_size[] = { codec_context->width, codec_context->height }; screen_set_window_property_iv(screen_window, SCREEN_PROPERTY_BUFFER_SIZE, video_size); screen_set_window_property_iv(screen_window, SCREEN_PROPERTY_SOURCE_SIZE, video_size); int z = -1; screen_set_window_property_iv(screen_window, SCREEN_PROPERTY_ZORDER, &z); int pos[] = { 0, 0 }; screen_set_window_property_iv(screen_window, SCREEN_PROPERTY_POSITION, pos); screen_create_window_buffers(screen_window, 1); screen_pixmap_t screen_pix; screen_create_pixmap(&screen_pix, screen_context); usage = SCREEN_USAGE_WRITE | SCREEN_USAGE_NATIVE; screen_set_pixmap_property_iv(screen_pix, SCREEN_PROPERTY_USAGE, &usage); int format = SCREEN_FORMAT_YUV420; screen_set_pixmap_property_iv(screen_pix, SCREEN_PROPERTY_FORMAT, &format); screen_set_pixmap_property_iv(screen_pix, SCREEN_PROPERTY_BUFFER_SIZE, video_size); screen_create_pixmap_buffer(screen_pix); screen_buffer_t screen_pixel_buffer; screen_get_pixmap_property_pv(screen_pix, SCREEN_PROPERTY_RENDER_BUFFERS, (void**) &screen_pixel_buffer); int stride; screen_get_buffer_property_iv(screen_pixel_buffer, SCREEN_PROPERTY_STRIDE, &stride); view->screen_context = screen_context; *window = view->screen_window = screen_window; view->screen_pixel_buffer = screen_pixel_buffer; view->stride = stride; ffd_reserved->view = view; return FFDEC_OK; }