vdpau: Add an allocation function for AVVDPAUContext.
[ffmpeg.git] / libavcodec / vdpau.c
1 /*
2  * Video Decode and Presentation API for UNIX (VDPAU) is used for
3  * HW decode acceleration for MPEG-1/2, MPEG-4 ASP, H.264 and VC-1.
4  *
5  * Copyright (c) 2008 NVIDIA
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23
24 #include <limits.h>
25 #include "avcodec.h"
26 #include "h264.h"
27 #include "vc1.h"
28
29 #undef NDEBUG
30 #include <assert.h>
31
32 #include "vdpau.h"
33 #include "vdpau_internal.h"
34
35 /**
36  * @addtogroup VDPAU_Decoding
37  *
38  * @{
39  */
40
41 AVVDPAUContext *av_alloc_vdpaucontext(void)
42 {
43     return av_mallocz(sizeof(AVVDPAUContext));
44 }
45
46 int ff_vdpau_common_start_frame(Picture *pic,
47                                 av_unused const uint8_t *buffer,
48                                 av_unused uint32_t size)
49 {
50     struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
51
52     pic_ctx->bitstream_buffers_allocated = 0;
53     pic_ctx->bitstream_buffers_used      = 0;
54     pic_ctx->bitstream_buffers           = NULL;
55     return 0;
56 }
57
58 #if CONFIG_H263_VDPAU_HWACCEL  || CONFIG_MPEG1_VDPAU_HWACCEL || \
59     CONFIG_MPEG2_VDPAU_HWACCEL || CONFIG_MPEG4_VDPAU_HWACCEL || \
60     CONFIG_VC1_VDPAU_HWACCEL   || CONFIG_WMV3_VDPAU_HWACCEL
61 int ff_vdpau_mpeg_end_frame(AVCodecContext *avctx)
62 {
63     AVVDPAUContext *hwctx = avctx->hwaccel_context;
64     MpegEncContext *s = avctx->priv_data;
65     Picture *pic = s->current_picture_ptr;
66     struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
67     VdpVideoSurface surf = ff_vdpau_get_surface_id(pic);
68
69     hwctx->render(hwctx->decoder, surf, (void *)&pic_ctx->info,
70                   pic_ctx->bitstream_buffers_used, pic_ctx->bitstream_buffers);
71
72     ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
73     av_freep(&pic_ctx->bitstream_buffers);
74
75     return 0;
76 }
77 #endif
78
79 int ff_vdpau_add_buffer(Picture *pic, const uint8_t *buf, uint32_t size)
80 {
81     struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
82     VdpBitstreamBuffer *buffers = pic_ctx->bitstream_buffers;
83
84     buffers = av_fast_realloc(buffers, &pic_ctx->bitstream_buffers_allocated,
85                               (pic_ctx->bitstream_buffers_used + 1) * sizeof(*buffers));
86     if (!buffers)
87         return AVERROR(ENOMEM);
88
89     pic_ctx->bitstream_buffers = buffers;
90     buffers += pic_ctx->bitstream_buffers_used++;
91
92     buffers->struct_version  = VDP_BITSTREAM_BUFFER_VERSION;
93     buffers->bitstream       = buf;
94     buffers->bitstream_bytes = size;
95     return 0;
96 }
97 /* @}*/