1/*
2 * VDA H.264 hardware acceleration
3 *
4 * copyright (c) 2011 Sebastien Zwickert
5 *
6 * This file is part of Libav.
7 *
8 * Libav is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * Libav is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#include "h264.h"
24#include "h264data.h"
25
26#include "vda_internal.h"
27
28/* This structure is used to store the bitstream of the current frame. */
29struct vda_picture_context {
30    uint8_t *bitstream;
31    int      bitstream_size;
32};
33
34static int start_frame(AVCodecContext *avctx,
35                       av_unused const uint8_t *buffer,
36                       av_unused uint32_t size)
37{
38    const H264Context *h                = avctx->priv_data;
39    struct vda_context *vda_ctx         = avctx->hwaccel_context;
40    struct vda_picture_context *pic_ctx = h->s.current_picture_ptr->f.hwaccel_picture_private;
41
42    if (!vda_ctx->decoder)
43        return -1;
44
45    pic_ctx->bitstream      = NULL;
46    pic_ctx->bitstream_size = 0;
47
48    return 0;
49}
50
51static int decode_slice(AVCodecContext *avctx,
52                        const uint8_t *buffer,
53                        uint32_t size)
54{
55    H264Context *h                      = avctx->priv_data;
56    struct vda_context *vda_ctx         = avctx->hwaccel_context;
57    struct vda_picture_context *pic_ctx = h->s.current_picture_ptr->f.hwaccel_picture_private;
58    void *tmp;
59
60    if (!vda_ctx->decoder)
61        return -1;
62
63    tmp = av_realloc(pic_ctx->bitstream, pic_ctx->bitstream_size+size+4);
64    if (!tmp)
65        return AVERROR(ENOMEM);
66
67    pic_ctx->bitstream = tmp;
68
69    AV_WB32(pic_ctx->bitstream + pic_ctx->bitstream_size, size);
70    memcpy(pic_ctx->bitstream + pic_ctx->bitstream_size + 4, buffer, size);
71
72    pic_ctx->bitstream_size += size + 4;
73
74    return 0;
75}
76
77static int end_frame(AVCodecContext *avctx)
78{
79    H264Context *h                      = avctx->priv_data;
80    struct vda_context *vda_ctx         = avctx->hwaccel_context;
81    struct vda_picture_context *pic_ctx = h->s.current_picture_ptr->f.hwaccel_picture_private;
82    AVFrame *frame                      = &h->s.current_picture_ptr->f;
83    int status;
84
85    if (!vda_ctx->decoder || !pic_ctx->bitstream)
86        return -1;
87
88    status = ff_vda_decoder_decode(vda_ctx, pic_ctx->bitstream,
89                                   pic_ctx->bitstream_size,
90                                   frame->reordered_opaque);
91
92    if (status)
93        av_log(avctx, AV_LOG_ERROR, "Failed to decode frame (%d)\n", status);
94
95    av_freep(&pic_ctx->bitstream);
96
97    return status;
98}
99
100AVHWAccel ff_h264_vda_hwaccel = {
101    .name           = "h264_vda",
102    .type           = AVMEDIA_TYPE_VIDEO,
103    .id             = CODEC_ID_H264,
104    .pix_fmt        = PIX_FMT_VDA_VLD,
105    .start_frame    = start_frame,
106    .decode_slice   = decode_slice,
107    .end_frame      = end_frame,
108    .priv_data_size = sizeof(struct vda_picture_context),
109};
110