1 /*****************************************************************************
2 * vda.c: VDA helpers for the libavcodec decoder
3 *****************************************************************************
4 * Copyright © 2012 VideoLAN
6 * Authors: Sebastien Zwickert <dilaroga@free.fr>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program 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
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
27 #include <vlc_common.h>
31 #include <libavcodec/avcodec.h>
37 #ifdef HAVE_AVCODEC_VDA
39 #include <libavcodec/vda.h>
40 #include <VideoDecodeAcceleration/VDADecoder.h>
45 struct vda_context hw_ctx;
50 vlc_fourcc_t i_chroma;
52 copy_cache_t image_cache;
58 static vlc_va_vda_t *vlc_va_vda_Get( void *p_va )
63 /*****************************************************************************
64 * vda_Copy420YpCbCr8Planar: copy y420 CVPixelBuffer to picture_t
65 *****************************************************************************/
66 static void vda_Copy420YpCbCr8Planar( picture_t *p_pic,
67 CVPixelBufferRef buffer,
75 CVPixelBufferLockBaseAddress( buffer, 0 );
77 for( int i = 0; i < 3; i++ )
79 pp_plane[i] = CVPixelBufferGetBaseAddressOfPlane( buffer, i );
80 pi_pitch[i] = CVPixelBufferGetBytesPerRowOfPlane( buffer, i );
83 CopyFromYv12( p_pic, pp_plane, pi_pitch,
84 i_width, i_height, cache );
86 CVPixelBufferUnlockBaseAddress( buffer, 0 );
89 /*****************************************************************************
90 * vda_Copy422YpCbCr8: copy 2vuy CVPixelBuffer to picture_t
91 *****************************************************************************/
92 static void vda_Copy422YpCbCr8( picture_t *p_pic,
93 CVPixelBufferRef buffer )
95 int i_plane, i_line, i_dst_stride, i_src_stride;
96 uint8_t *p_dst, *p_src;
98 CVPixelBufferLockBaseAddress( buffer, 0 );
100 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
102 p_dst = p_pic->p[i_plane].p_pixels;
103 p_src = CVPixelBufferGetBaseAddressOfPlane( buffer, i_plane );
104 i_dst_stride = p_pic->p[i_plane].i_pitch;
105 i_src_stride = CVPixelBufferGetBytesPerRowOfPlane( buffer, i_plane );
107 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines ; i_line++ )
109 memcpy( p_dst, p_src, i_src_stride );
111 p_src += i_src_stride;
112 p_dst += i_dst_stride;
116 CVPixelBufferUnlockBaseAddress( buffer, 0 );
119 static int Setup( vlc_va_t *p_external, void **pp_hw_ctx, vlc_fourcc_t *pi_chroma,
120 int i_width, int i_height )
123 vlc_va_vda_t *p_va = vlc_va_vda_Get( p_external );
125 if( p_va->hw_ctx.width == i_width
126 && p_va->hw_ctx.height == i_height
127 && p_va->hw_ctx.decoder )
129 *pp_hw_ctx = &p_va->hw_ctx;
130 *pi_chroma = p_va->i_chroma;
134 if( p_va->hw_ctx.decoder )
136 ff_vda_destroy_decoder( &p_va->hw_ctx );
140 memset( &p_va->hw_ctx, 0, sizeof(p_va->hw_ctx) );
141 p_va->hw_ctx.width = i_width;
142 p_va->hw_ctx.height = i_height;
143 p_va->hw_ctx.format = 'avc1';
144 p_va->hw_ctx.use_sync_decoding = 1;
146 int i_pix_fmt = var_CreateGetInteger( p_va->p_log, "avcodec-vda-pix-fmt" );
151 p_va->hw_ctx.cv_pix_fmt_type = kCVPixelFormatType_422YpCbCr8;
152 p_va->i_chroma = VLC_CODEC_UYVY;
156 p_va->hw_ctx.cv_pix_fmt_type = kCVPixelFormatType_420YpCbCr8Planar;
157 p_va->i_chroma = VLC_CODEC_I420;
158 CopyInitCache( &p_va->image_cache, i_width );
162 /* Setup the libavcodec hardware context */
163 *pp_hw_ctx = &p_va->hw_ctx;
164 *pi_chroma = p_va->i_chroma;
166 /* create the decoder */
167 int status = ff_vda_create_decoder( &p_va->hw_ctx,
172 msg_Err( p_va->p_log, "Failed to create the decoder : %i", status );
179 static int Get( vlc_va_t *p_external, AVFrame *p_ff )
181 VLC_UNUSED( p_external );
184 for( int i = 0; i < 4; i++ )
186 p_ff->data[i] = NULL;
187 p_ff->linesize[i] = 0;
189 if( i == 0 || i == 3 )
190 p_ff->data[i] = 1; // dummy
196 static int Extract( vlc_va_t *p_external, picture_t *p_picture, AVFrame *p_ff )
198 vlc_va_vda_t *p_va = vlc_va_vda_Get( p_external );
199 CVPixelBufferRef cv_buffer = ( CVPixelBufferRef )p_ff->data[3];
203 msg_Dbg( p_va->p_log, "Frame buffer is empty.");
207 if( p_va->hw_ctx.cv_pix_fmt_type == kCVPixelFormatType_420YpCbCr8Planar )
209 if( !p_va->image_cache.buffer )
212 vda_Copy420YpCbCr8Planar( p_picture,
216 &p_va->image_cache );
219 vda_Copy422YpCbCr8( p_picture, cv_buffer );
224 static void Release( vlc_va_t *p_external, AVFrame *p_ff )
226 VLC_UNUSED( p_external );
227 CVPixelBufferRef cv_buffer = ( CVPixelBufferRef )p_ff->data[3];
230 CFRelease( cv_buffer );
233 static void Close( vlc_va_t *p_external )
235 vlc_va_vda_t *p_va = vlc_va_vda_Get( p_external );
237 ff_vda_destroy_decoder( &p_va->hw_ctx ) ;
239 if( p_va->hw_ctx.cv_pix_fmt_type == kCVPixelFormatType_420YpCbCr8Planar )
240 CopyCleanCache( &p_va->image_cache );
245 vlc_va_t *vlc_va_NewVDA( vlc_object_t *p_log, int i_codec_id, void *p_extra, int i_extra )
247 if( i_codec_id != CODEC_ID_H264 )
250 if( !p_extra || i_extra < 7 )
252 msg_Warn( p_log, "VDA requires extradata." );
256 vlc_va_vda_t *p_va = calloc( 1, sizeof(*p_va) );
261 p_va->p_extradata = p_extra;
262 p_va->i_extradata = i_extra;
264 p_va->va.setup = Setup;
266 p_va->va.release = Release;
267 p_va->va.extract = Extract;
268 p_va->va.close = Close;
274 vlc_va_t *vlc_va_NewVDA( vlc_object_t *p_log, int i_codec_id, void *p_extra, int i_extra )
277 VLC_UNUSED( i_codec_id );
278 VLC_UNUSED( p_extra );
279 VLC_UNUSED( i_extra );