1 /*****************************************************************************
2 * vda.c: VDA helpers for the ffmpeg 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;
60 static vlc_va_vda_t *vlc_va_vda_Get( void *p_va )
65 /*****************************************************************************
66 * vda_Copy420YpCbCr8Planar: copy y420 CVPixelBuffer to picture_t
67 *****************************************************************************/
68 static void vda_Copy420YpCbCr8Planar( picture_t *p_pic,
69 CVPixelBufferRef buffer,
77 CVPixelBufferLockBaseAddress( buffer, 0 );
79 for( int i = 0; i < 3; i++ )
81 pp_plane[i] = CVPixelBufferGetBaseAddressOfPlane( buffer, i );
82 pi_pitch[i] = CVPixelBufferGetBytesPerRowOfPlane( buffer, i );
85 CopyFromYv12( p_pic, pp_plane, pi_pitch,
86 i_width, i_height, cache );
88 CVPixelBufferUnlockBaseAddress( buffer, 0 );
91 /*****************************************************************************
92 * vda_Copy422YpCbCr8: copy 2vuy CVPixelBuffer to picture_t
93 *****************************************************************************/
94 static void vda_Copy422YpCbCr8( picture_t *p_pic,
95 CVPixelBufferRef buffer )
97 int i_plane, i_line, i_dst_stride, i_src_stride;
98 uint8_t *p_dst, *p_src;
100 CVPixelBufferLockBaseAddress( buffer, 0 );
102 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
104 p_dst = p_pic->p[i_plane].p_pixels;
105 p_src = CVPixelBufferGetBaseAddressOfPlane( buffer, i_plane );
106 i_dst_stride = p_pic->p[i_plane].i_pitch;
107 i_src_stride = CVPixelBufferGetBytesPerRowOfPlane( buffer, i_plane );
109 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines ; i_line++ )
111 memcpy( p_dst, p_src, i_src_stride );
113 p_src += i_src_stride;
114 p_dst += i_dst_stride;
118 CVPixelBufferUnlockBaseAddress( buffer, 0 );
121 static int Setup( vlc_va_t *p_external, void **pp_hw_ctx, vlc_fourcc_t *pi_chroma,
122 int i_width, int i_height )
125 vlc_va_vda_t *p_va = vlc_va_vda_Get( p_external );
127 if( p_va->hw_ctx.width == i_width
128 && p_va->hw_ctx.height == i_height
129 && p_va->hw_ctx.decoder )
131 *pp_hw_ctx = &p_va->hw_ctx;
132 *pi_chroma = p_va->i_chroma;
136 if( p_va->hw_ctx.decoder )
138 ff_vda_destroy_decoder( &p_va->hw_ctx );
142 memset( &p_va->hw_ctx, 0, sizeof(p_va->hw_ctx) );
143 p_va->hw_ctx.width = i_width;
144 p_va->hw_ctx.height = i_height;
145 p_va->hw_ctx.format = 'avc1';
147 int i_pix_fmt = var_CreateGetInteger( p_va->p_log, "avcodec-vda-pix-fmt" );
152 p_va->hw_ctx.cv_pix_fmt_type = kCVPixelFormatType_422YpCbCr8;
153 p_va->i_chroma = VLC_CODEC_UYVY;
157 p_va->hw_ctx.cv_pix_fmt_type = kCVPixelFormatType_420YpCbCr8Planar;
158 p_va->i_chroma = VLC_CODEC_I420;
159 CopyInitCache( &p_va->image_cache, i_width );
163 /* Setup the ffmpeg hardware context */
164 *pp_hw_ctx = &p_va->hw_ctx;
165 *pi_chroma = p_va->i_chroma;
167 /* create the decoder */
168 int status = ff_vda_create_decoder( &p_va->hw_ctx,
173 msg_Err( p_va->p_log, "Failed to create the decoder : %i", status );
180 static int Get( vlc_va_t *p_external, AVFrame *p_ff )
182 vlc_va_vda_t *p_va = vlc_va_vda_Get( p_external );
184 if( p_va->top_frame )
185 ff_vda_release_vda_frame( p_va->top_frame );
187 p_va->top_frame = ff_vda_queue_pop( &p_va->hw_ctx );
190 for( int i = 0; i < 4; i++ )
192 p_ff->data[i] = NULL;
193 p_ff->linesize[i] = 0;
195 if( i == 0 || i == 3 )
196 p_ff->data[i] = 1; // dummy
202 static int Extract( vlc_va_t *p_external, picture_t *p_picture, AVFrame *p_ff )
205 vlc_va_vda_t *p_va = vlc_va_vda_Get( p_external );
207 if( !p_va->top_frame )
209 msg_Dbg( p_va->p_log, "Decoder is buffering...");
213 CVPixelBufferRef cv_buffer = p_va->top_frame->cv_buffer;
215 if( p_va->hw_ctx.cv_pix_fmt_type == kCVPixelFormatType_420YpCbCr8Planar )
217 if( !p_va->image_cache.buffer )
220 vda_Copy420YpCbCr8Planar( p_picture,
224 &p_va->image_cache );
227 vda_Copy422YpCbCr8( p_picture, cv_buffer );
229 p_picture->date = p_va->top_frame->pts;
234 static void Release( vlc_va_t *p_external, AVFrame *p_ff )
237 VLC_UNUSED( p_external );
240 static void Close( vlc_va_t *p_external )
242 vlc_va_vda_t *p_va = vlc_va_vda_Get( p_external );
244 ff_vda_destroy_decoder( &p_va->hw_ctx ) ;
246 if( p_va->top_frame )
247 ff_vda_release_vda_frame( p_va->top_frame );
249 if( p_va->hw_ctx.cv_pix_fmt_type == kCVPixelFormatType_420YpCbCr8Planar )
250 CopyCleanCache( &p_va->image_cache );
255 vlc_va_t *vlc_va_NewVDA( vlc_object_t *p_log, int i_codec_id, void *p_extra, int i_extra )
257 if( i_codec_id != CODEC_ID_H264 )
260 if( !p_extra || i_extra < 7 )
262 msg_Warn( p_log, "VDA requires extradata." );
266 vlc_va_vda_t *p_va = calloc( 1, sizeof(*p_va) );
271 p_va->p_extradata = p_extra;
272 p_va->i_extradata = i_extra;
273 p_va->top_frame = NULL;
275 p_va->va.setup = Setup;
277 p_va->va.release = Release;
278 p_va->va.extract = Extract;
279 p_va->va.close = Close;
285 vlc_va_t *vlc_va_NewVDA( vlc_object_t *p_log, int i_codec_id, void *p_extra, int i_extra )
288 VLC_UNUSED( i_codec_id );
289 VLC_UNUSED( p_extra );
290 VLC_UNUSED( i_extra );