[FFmpeg-devel] [PATCH 2/2] hwaccel: add VideoToolbox support.

Michael Niedermayer michaelni at gmx.at
Tue Sep 18 05:04:20 CEST 2012


On Sun, Sep 16, 2012 at 08:38:05PM +0200, Sebastien Zwickert wrote:
> ---
>  Changelog                |    1 +
>  MAINTAINERS              |    1 +
>  configure                |   12 ++
>  libavcodec/Makefile      |    7 +
>  libavcodec/allcodecs.c   |    4 +
>  libavcodec/h264.c        |    1 +
>  libavcodec/mpeg12.c      |    3 +
>  libavcodec/mpegvideo.c   |    1 +
>  libavcodec/vt.c          |  365 ++++++++++++++++++++++++++++++++++++++++++++++
>  libavcodec/vt.h          |  186 +++++++++++++++++++++++
>  libavcodec/vt_h264.c     |   79 ++++++++++
>  libavcodec/vt_internal.h |   42 ++++++
>  libavcodec/vt_mpeg2.c    |   62 ++++++++
>  libavcodec/vt_mpeg4.c    |   77 ++++++++++
>  libavutil/pixdesc.c      |    6 +
>  libavutil/pixfmt.h       |    1 +
>  16 files changed, 848 insertions(+)
>  create mode 100644 libavcodec/vt.c
>  create mode 100644 libavcodec/vt.h
>  create mode 100644 libavcodec/vt_h264.c
>  create mode 100644 libavcodec/vt_internal.h
>  create mode 100644 libavcodec/vt_mpeg2.c
>  create mode 100644 libavcodec/vt_mpeg4.c
[...]
> diff --git a/libavcodec/vt.c b/libavcodec/vt.c
> new file mode 100644
> index 0000000..e357b56
> --- /dev/null
> +++ b/libavcodec/vt.c
> @@ -0,0 +1,365 @@
> +/*
> + * VideoToolbox hardware acceleration
> + *
> + * copyright (c) 2012 Sebastien Zwickert
> + *
> + * 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 "libavutil/avutil.h"
> +#include "libavformat/avio.h"
> +#include "libavformat/movenc.h"
> +
> +#include "vt_internal.h"
> +
> +static CFDataRef vt_esds_extradata_create(AVCodecContext *avctx)
> +{
> +    CFDataRef data  = NULL;
> +    MOVTrack *track = NULL;
> +    AVIOContext *pb = NULL;
> +    uint8_t *buf    = NULL;
> +    int sb = 8; // skip first atom info : size and fourcc
> +    int buf_size = 3 + 5 + 13 + 5 + avctx->extradata_size + 3;
> +    // ES_DescrTag data + DecoderConfigDescrTag + data + DecSpecificInfoTag + size + SLConfigDescriptor
> +    int padding = 12 + sb;
> +    int s;
> +
> +    if (!(buf = av_mallocz(buf_size + padding)))
> +        goto end;
> +
> +    if (!(track = av_mallocz(sizeof(MOVTrack))))
> +        goto end;
> +

> +    pb = avio_alloc_context(buf, buf_size + padding, 1, NULL, NULL, NULL, NULL);

the use of libavformat from libavcodec is problematic as it would make
libavcodec depend on libavformat if VideoToolbox support is enabled.

I think many people would be opposed to such dependancy.

the solution here is either to undo the factorization or to change
the code to use only functions available in libavcodec/libavutil
and then change the libavformat part to be based on top of this.


[...]
> +static void vt_decoder_callback(void *vt_hw_ctx,
> +                                void *sourceFrameRefCon,
> +                                OSStatus status,
> +                                VTDecodeInfoFlags flags,
> +                                CVImageBufferRef image_buffer,
> +                                CMTime pts,
> +                                CMTime duration)
> +{
> +    struct vt_context *vt_ctx = vt_hw_ctx;
> +    vt_ctx->cv_buffer = NULL;
> +
> +    if (!image_buffer)
> +        return;
> +
> +    if (vt_ctx->cv_pix_fmt != CVPixelBufferGetPixelFormatType(image_buffer))
> +        return;

Are these return cases normal occurances or are these error/warning
conditions ? if later it might make sense to at least produce a
av_log(X, AV_LOG_DEBUG,...) in the case


> +
> +    vt_ctx->cv_buffer = CVPixelBufferRetain(image_buffer);
> +}
> +
> +static int vt_session_decode_frame(struct vt_context *vt_ctx)
> +{
> +    OSStatus status;
> +    CMSampleBufferRef sample_buf;
> +
> +    sample_buf = vt_sample_buffer_create(vt_ctx->cm_fmt_desc,
> +                                         vt_ctx->priv_bitstream,
> +                                         vt_ctx->priv_bitstream_size);
> +
> +    if (!sample_buf)
> +        return -1;
> +
> +    status = VTDecompressionSessionDecodeFrame(vt_ctx->session,
> +                                               sample_buf,
> +                                               0,               // decodeFlags
> +                                               NULL,            // sourceFrameRefCon
> +                                               0);              // infoFlagsOut
> +    if (status == noErr)
> +        status = VTDecompressionSessionWaitForAsynchronousFrames(vt_ctx->session);
> +
> +    CFRelease(sample_buf);
> +
> +    return status;
> +}

is a OSStatus guranteed to fit in a int ? it is implicitly cast to
one here.
also the error code is propagated down to hwaccel end_frame, later
needs negative values for errors, are errors in OSStatus guranteed
to be negative ? (i tried to find some docs but failed)


[...]
> +/**
> + * This structure is used to provide the necessary configuration and data
> + * to the VideoToolbox FFmpeg HWAccel implementation.
> + *
> + * The application must make it available as AVCodecContext.hwaccel_context.
> + */
> +struct vt_context {
> +    /**
> +     * VideoToolbox decompression session.
> +     *
> +     * - encoding: unused.
> +     * - decoding: Set/Unset by libavcodec.
> +     */
> +    VTDecompressionSessionRef   session;
> +


> +    /**
> +     * The width of encoded video.
> +     *
> +     * - encoding: unused.
> +     * - decoding: Set/Unset by user.
> +     */
> +    int                 width;
> +
> +    /**
> +     * The height of encoded video.
> +     *
> +     * - encoding: unused.
> +     * - decoding: Set/Unset by user.
> +     */
> +    int                 height;
> +

these 2 seem unused


[...]

> +static int vt_mpeg2_end_frame(AVCodecContext *avctx)
> +{
> +    return ff_vt_end_frame(avctx);
> +}
> +
> +AVHWAccel ff_mpeg2_vt_hwaccel = {
> +    .name           = "mpeg2_vt",
> +    .type           = AVMEDIA_TYPE_VIDEO,
> +    .id             = AV_CODEC_ID_MPEG2VIDEO,
> +    .pix_fmt        = PIX_FMT_VT_VLD,
> +    .start_frame    = vt_mpeg2_start_frame,
> +    .end_frame      = vt_mpeg2_end_frame,
> +    .decode_slice   = vt_mpeg2_decode_slice,
> +};

the vt_mpeg2_end_frame wraper function seems unneeded ff_vt_end_frame
could directly be used in the struct
the same applies to vt_h264_end_frame() and vt_mpeg4_end_frame()


[...]

> +static int vt_mpeg4_start_frame(AVCodecContext *avctx,
> +                                const uint8_t *buffer,
> +                                uint32_t size)
> +{
> +    struct vt_context *vt_ctx = avctx->hwaccel_context;
> +
> +    if (!vt_ctx->session)
> +        return -1;
> +
> +    return ff_vt_buffer_copy(vt_ctx, buffer, size);
> +}
> +
> +static int vt_mpeg4_decode_slice(AVCodecContext *avctx,
> +                                 const uint8_t *buffer,
> +                                 uint32_t size)
> +{
> +    struct vt_context *vt_ctx = avctx->hwaccel_context;
> +
> +    if (!vt_ctx->session)
> +        return -1;
> +
> +    return 0;
> +}

these 2 look redundant relative to the mpeg2 variants


[...]
-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Opposition brings concord. Out of discord comes the fairest harmony.
-- Heraclitus
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20120918/cd80ef10/attachment.asc>


More information about the ffmpeg-devel mailing list