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 *****************************************************************************/
29 #include <vlc_common.h>
31 #include <vlc_plugin.h>
33 #include <libavcodec/avcodec.h>
39 #include <libavcodec/vda.h>
40 #include <VideoDecodeAcceleration/VDADecoder.h>
42 static int Open( vlc_va_t *, int, const es_format_t * );
43 static void Close( vlc_va_t * );
45 static const int nvda_pix_fmt_list[] = { 0, 1 };
46 static const char *const nvda_pix_fmt_list_text[] =
47 { N_("420YpCbCr8Planar"), N_("422YpCbCr8") };
50 set_description( N_("Video Decode Acceleration Framework (VDA)") )
51 set_capability( "hw decoder", 50 )
52 set_category( CAT_INPUT )
53 set_subcategory( SUBCAT_INPUT_VCODEC )
54 set_callbacks( Open, Close )
55 add_integer ( "avcodec-vda-pix-fmt", 0, VDA_PIX_FMT_TEXT,
56 VDA_PIX_FMT_LONGTEXT, false)
57 change_integer_list( nvda_pix_fmt_list, nvda_pix_fmt_list_text )
62 struct vda_context hw_ctx;
67 vlc_fourcc_t i_chroma;
69 copy_cache_t image_cache;
75 typedef struct vlc_va_sys_t vlc_va_vda_t;
77 static vlc_va_vda_t *vlc_va_vda_Get( vlc_va_t *p_va )
82 /*****************************************************************************
83 * vda_Copy420YpCbCr8Planar: copy y420 CVPixelBuffer to picture_t
84 *****************************************************************************/
85 static void vda_Copy420YpCbCr8Planar( picture_t *p_pic,
86 CVPixelBufferRef buffer,
94 CVPixelBufferLockBaseAddress( buffer, 0 );
96 for( int i = 0; i < 3; i++ )
98 pp_plane[i] = CVPixelBufferGetBaseAddressOfPlane( buffer, i );
99 pi_pitch[i] = CVPixelBufferGetBytesPerRowOfPlane( buffer, i );
102 CopyFromYv12( p_pic, pp_plane, pi_pitch,
103 i_width, i_height, cache );
105 CVPixelBufferUnlockBaseAddress( buffer, 0 );
108 /*****************************************************************************
109 * vda_Copy422YpCbCr8: copy 2vuy CVPixelBuffer to picture_t
110 *****************************************************************************/
111 static void vda_Copy422YpCbCr8( picture_t *p_pic,
112 CVPixelBufferRef buffer )
114 int i_plane, i_line, i_dst_stride, i_src_stride;
115 uint8_t *p_dst, *p_src;
117 CVPixelBufferLockBaseAddress( buffer, 0 );
119 for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
121 p_dst = p_pic->p[i_plane].p_pixels;
122 p_src = CVPixelBufferGetBaseAddressOfPlane( buffer, i_plane );
123 i_dst_stride = p_pic->p[i_plane].i_pitch;
124 i_src_stride = CVPixelBufferGetBytesPerRowOfPlane( buffer, i_plane );
126 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines ; i_line++ )
128 memcpy( p_dst, p_src, i_src_stride );
130 p_src += i_src_stride;
131 p_dst += i_dst_stride;
135 CVPixelBufferUnlockBaseAddress( buffer, 0 );
138 static int Setup( vlc_va_t *p_external, void **pp_hw_ctx, vlc_fourcc_t *pi_chroma,
139 int i_width, int i_height )
142 vlc_va_vda_t *p_va = vlc_va_vda_Get( p_external );
144 if( p_va->hw_ctx.width == i_width
145 && p_va->hw_ctx.height == i_height
146 && p_va->hw_ctx.decoder )
148 *pp_hw_ctx = &p_va->hw_ctx;
149 *pi_chroma = p_va->i_chroma;
153 if( p_va->hw_ctx.decoder )
155 ff_vda_destroy_decoder( &p_va->hw_ctx );
159 memset( &p_va->hw_ctx, 0, sizeof(p_va->hw_ctx) );
160 p_va->hw_ctx.width = i_width;
161 p_va->hw_ctx.height = i_height;
162 p_va->hw_ctx.format = 'avc1';
164 int i_pix_fmt = var_CreateGetInteger( p_va->p_log, "avcodec-vda-pix-fmt" );
169 p_va->hw_ctx.cv_pix_fmt_type = kCVPixelFormatType_422YpCbCr8;
170 p_va->i_chroma = VLC_CODEC_UYVY;
171 msg_Dbg(p_va->p_log, "using pixel format 422YpCbCr8");
175 p_va->hw_ctx.cv_pix_fmt_type = kCVPixelFormatType_420YpCbCr8Planar;
176 p_va->i_chroma = VLC_CODEC_I420;
177 CopyInitCache( &p_va->image_cache, i_width );
178 msg_Dbg(p_va->p_log, "using pixel format 420YpCbCr8Planar");
182 /* Setup the libavcodec hardware context */
183 *pp_hw_ctx = &p_va->hw_ctx;
184 *pi_chroma = p_va->i_chroma;
186 /* create the decoder */
187 int status = ff_vda_create_decoder( &p_va->hw_ctx,
192 msg_Err( p_va->p_log, "Failed to create decoder: %i", status );
196 msg_Dbg( p_va->p_log, "VDA decoder created");
201 static int Get( vlc_va_t *p_external, AVFrame *p_ff )
203 VLC_UNUSED( p_external );
206 for( int i = 0; i < 4; i++ )
208 p_ff->data[i] = NULL;
209 p_ff->linesize[i] = 0;
211 if( i == 0 || i == 3 )
212 p_ff->data[i] = (uint8_t *)1; // dummy
218 static int Extract( vlc_va_t *p_external, picture_t *p_picture, AVFrame *p_ff )
220 vlc_va_vda_t *p_va = vlc_va_vda_Get( p_external );
221 CVPixelBufferRef cv_buffer = ( CVPixelBufferRef )p_ff->data[3];
225 msg_Dbg( p_va->p_log, "Frame buffer is empty.");
229 if( p_va->hw_ctx.cv_pix_fmt_type == kCVPixelFormatType_420YpCbCr8Planar )
231 if( !p_va->image_cache.buffer )
234 vda_Copy420YpCbCr8Planar( p_picture,
238 &p_va->image_cache );
241 vda_Copy422YpCbCr8( p_picture, cv_buffer );
246 static void Release( vlc_va_t *p_external, AVFrame *p_ff )
248 VLC_UNUSED( p_external );
249 CVPixelBufferRef cv_buffer = ( CVPixelBufferRef )p_ff->data[3];
252 CFRelease( cv_buffer );
255 static void Close( vlc_va_t *p_external )
257 vlc_va_vda_t *p_va = vlc_va_vda_Get( p_external );
259 msg_Dbg(p_va->p_log, "destroying VDA decoder");
261 ff_vda_destroy_decoder( &p_va->hw_ctx ) ;
263 if( p_va->hw_ctx.cv_pix_fmt_type == kCVPixelFormatType_420YpCbCr8Planar )
264 CopyCleanCache( &p_va->image_cache );
269 static int Open( vlc_va_t *external, int i_codec_id, const es_format_t *fmt )
271 msg_Dbg( external, "opening VDA module" );
272 if( i_codec_id != AV_CODEC_ID_H264 )
274 msg_Warn( external, "input codec isn't H264, canceling VDA decoding" );
278 if( fmt->p_extra == NULL || fmt->i_extra < 7 )
280 msg_Warn( external, "VDA requires extradata." );
284 vlc_va_vda_t *p_va = calloc( 1, sizeof(*p_va) );
288 p_va->p_log = VLC_OBJECT(external);
289 p_va->p_extradata = fmt->p_extra;
290 p_va->i_extradata = fmt->i_extra;
292 external->sys = p_va;
293 external->description = (char *)"VDA";
294 external->pix_fmt = PIX_FMT_VDA_VLD;
295 external->setup = Setup;
297 external->release = Release;
298 external->extract = Extract;