/* * Copyright (c) 2004 Michael Niedermayer * * The default get_buffer2() implementation * * This file is part of FFmpeg. * * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include #include "libavutil/avassert.h" #include "libavutil/avutil.h" #include "libavutil/buffer.h" #include "libavutil/frame.h" //#include "libavutil/hwcontext.h" //#include "libavutil/imgutils.h" #include "libavutil/mem.h" #include "libavutil/samplefmt.h" #include "libavutil/version.h" #include "avcodec.h" #include "internal.h" #include "refstruct.h" #ifdef ANDROID_BUILD #include #define aml_print(fmt, ...) \ do { \ struct timespec ts; \ clock_gettime(CLOCK_MONOTONIC_RAW, &ts); \ printf("[%lld.%09ld]" fmt "\n",(long long)ts.tv_sec, ts.tv_nsec, ##__VA_ARGS__); \ } while(0) #define ALOGD(fmt, ...) aml_print(fmt, ##__VA_ARGS__) #define ALOGI(fmt, ...) aml_print(fmt, ##__VA_ARGS__) #define ALOGW(fmt, ...) aml_print(fmt, ##__VA_ARGS__) #define ALOGE(fmt, ...) aml_print(fmt, ##__VA_ARGS__) #else #include #endif typedef struct FramePool { /** * Pools for each data plane. For audio all the planes have the same size, * so only pools[0] is used. */ AVBufferPool *pools[4]; /* * Pool parameters */ int format; int width, height; int stride_align[AV_NUM_DATA_POINTERS]; int linesize[4]; int planes; int channels; int samples; } FramePool; static void frame_pool_free(FFRefStructOpaque unused, void *obj) { FramePool *pool = obj; int i; for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++) av_buffer_pool_uninit(&pool->pools[i]); } static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame) { FramePool *pool = avctx->internal->pool; int i, ret; if (pool && pool->format == frame->format) { if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && pool->width == frame->width && pool->height == frame->height) return 0; if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && pool->channels == frame->ch_layout.nb_channels && frame->nb_samples == pool->samples) return 0; } pool = ff_refstruct_alloc_ext(sizeof(*pool), 0, NULL, frame_pool_free); if (!pool) return AVERROR(ENOMEM); switch (avctx->codec_type) { case AVMEDIA_TYPE_AUDIO: { ret = av_samples_get_buffer_size(&pool->linesize[0], frame->ch_layout.nb_channels, frame->nb_samples, frame->format, 0); if (ret < 0) goto fail; pool->pools[0] = av_buffer_pool_init(pool->linesize[0], CONFIG_MEMORY_POISONING ? NULL : av_buffer_allocz); if (!pool->pools[0]) { ret = AVERROR(ENOMEM); goto fail; } pool->format = frame->format; pool->channels = frame->ch_layout.nb_channels; pool->samples = frame->nb_samples; pool->planes = av_sample_fmt_is_planar(pool->format) ? pool->channels : 1; break; } default: av_assert0(0); } ff_refstruct_unref(&avctx->internal->pool); avctx->internal->pool = pool; return 0; fail: ff_refstruct_unref(&pool); return ret; } static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame) { FramePool *pool = avctx->internal->pool; int planes = pool->planes; int i; frame->linesize[0] = pool->linesize[0]; if (planes > AV_NUM_DATA_POINTERS) { frame->extended_data = av_calloc(planes, sizeof(*frame->extended_data)); frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS; frame->extended_buf = av_calloc(frame->nb_extended_buf, sizeof(*frame->extended_buf)); if (!frame->extended_data || !frame->extended_buf) { av_freep(&frame->extended_data); av_freep(&frame->extended_buf); return AVERROR(ENOMEM); } } else { frame->extended_data = frame->data; av_assert0(frame->nb_extended_buf == 0); } for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) { frame->buf[i] = av_buffer_pool_get(pool->pools[0]); if (!frame->buf[i]) goto fail; frame->extended_data[i] = frame->data[i] = frame->buf[i]->data; } for (i = 0; i < frame->nb_extended_buf; i++) { frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]); if (!frame->extended_buf[i]) goto fail; frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data; } if (avctx->debug & FF_DEBUG_BUFFERS) av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame); return 0; fail: av_frame_unref(frame); return AVERROR(ENOMEM); } static int video_get_buffer(AVCodecContext *s, AVFrame *pic) { FramePool *pool = s->internal->pool; int i; if (pic->data[0] || pic->data[1] || pic->data[2] || pic->data[3]) { av_log(s, AV_LOG_ERROR, "pic->data[*]!=NULL in avcodec_default_get_buffer\n"); return -1; } memset(pic->data, 0, sizeof(pic->data)); pic->extended_data = pic->data; for (i = 0; i < 4 && pool->pools[i]; i++) { pic->linesize[i] = pool->linesize[i]; pic->buf[i] = av_buffer_pool_get(pool->pools[i]); if (!pic->buf[i]) goto fail; pic->data[i] = pic->buf[i]->data; } for (; i < AV_NUM_DATA_POINTERS; i++) { pic->data[i] = NULL; pic->linesize[i] = 0; } if (s->debug & FF_DEBUG_BUFFERS) av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic); return 0; fail: av_frame_unref(pic); return AVERROR(ENOMEM); } int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags) { int ret; if ((ret = update_frame_pool(avctx, frame)) < 0) { return ret; } switch (avctx->codec_type) { case AVMEDIA_TYPE_VIDEO: return video_get_buffer(avctx, frame); case AVMEDIA_TYPE_AUDIO: return audio_get_buffer(avctx, frame); default: return -1; } }