]> git.sesse.net Git - ffmpeg/blob - libavcodec/vda_h264.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / vda_h264.c
1 /*
2  * VDA H264 HW acceleration.
3  *
4  * copyright (c) 2011 Sebastien Zwickert
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include "vda_internal.h"
24
25 /* This structure is used to store the bitstream of the current frame. */
26 struct vda_picture_context {
27     uint8_t         *bitstream;
28     int              bitstream_size;
29 };
30
31 static int start_frame(AVCodecContext *avctx,
32                        av_unused const uint8_t *buffer,
33                        av_unused uint32_t size)
34 {
35     const H264Context *h = avctx->priv_data;
36     struct vda_context *vda_ctx = avctx->hwaccel_context;
37     struct vda_picture_context *pic_ctx = h->s.current_picture_ptr->f.hwaccel_picture_private;
38
39     if (!vda_ctx->decoder)
40         return -1;
41
42     pic_ctx->bitstream = NULL;
43     pic_ctx->bitstream_size = 0;
44
45     return 0;
46 }
47
48 static int decode_slice(AVCodecContext *avctx,
49                         const uint8_t *buffer,
50                         uint32_t size)
51 {
52     H264Context *h = avctx->priv_data;
53     struct vda_context *vda_ctx = avctx->hwaccel_context;
54     struct vda_picture_context *pic_ctx = h->s.current_picture_ptr->f.hwaccel_picture_private;
55     void *tmp;
56
57     if (!vda_ctx->decoder)
58         return -1;
59
60     tmp = av_realloc(pic_ctx->bitstream, pic_ctx->bitstream_size+size+4);
61     if (!tmp)
62         return AVERROR(ENOMEM);
63
64     pic_ctx->bitstream = tmp;
65
66     AV_WB32(pic_ctx->bitstream+pic_ctx->bitstream_size, size);
67     memcpy(pic_ctx->bitstream+pic_ctx->bitstream_size+4, buffer, size);
68
69     pic_ctx->bitstream_size += size + 4;
70
71     return 0;
72 }
73
74 static int end_frame(AVCodecContext *avctx)
75 {
76     H264Context *h = avctx->priv_data;
77     struct vda_context *vda_ctx = avctx->hwaccel_context;
78     struct vda_picture_context *pic_ctx = h->s.current_picture_ptr->f.hwaccel_picture_private;
79     AVFrame *frame = (AVFrame*)h->s.current_picture_ptr;
80     int status;
81
82     if (!vda_ctx->decoder || !pic_ctx->bitstream)
83         return -1;
84
85     status = ff_vda_decoder_decode(vda_ctx, pic_ctx->bitstream,
86                                    pic_ctx->bitstream_size,
87                                    frame->reordered_opaque);
88
89     if (status)
90         av_log(avctx, AV_LOG_ERROR, "Failed to decode frame (%d)\n", status);
91
92     av_freep(&pic_ctx->bitstream);
93
94     return status;
95 }
96
97 AVHWAccel ff_h264_vda_hwaccel = {
98     .name           = "h264_vda",
99     .type           = AVMEDIA_TYPE_VIDEO,
100     .id             = CODEC_ID_H264,
101     .pix_fmt        = PIX_FMT_VDA_VLD,
102     .capabilities   = 0,
103     .start_frame    = start_frame,
104     .decode_slice   = decode_slice,
105     .end_frame      = end_frame,
106     .priv_data_size = sizeof(struct vda_picture_context),
107 };