]> git.sesse.net Git - vlc/blob - modules/codec/avcodec/vda.c
avcodec: look up pix fmt only once during deinterlace initialization
[vlc] / modules / codec / avcodec / vda.c
1 /*****************************************************************************
2  * vda.c: VDA helpers for the libavcodec decoder
3  *****************************************************************************
4  * Copyright © 2012 VideoLAN
5  *
6  * Authors: Sebastien Zwickert <dilaroga@free.fr>
7  *
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.
12  *
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.
17  *
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  *****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include <assert.h>
28
29 #include <vlc_common.h>
30 #include <vlc_vout.h>
31 #include <vlc_plugin.h>
32
33 #include <libavcodec/avcodec.h>
34
35 #include "avcodec.h"
36 #include "va.h"
37 #include "copy.h"
38
39 #include <libavcodec/vda.h>
40 #include <VideoDecodeAcceleration/VDADecoder.h>
41
42 static int Open( vlc_va_t *, int, const es_format_t * );
43 static void Close( vlc_va_t * );
44
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") };
48
49 vlc_module_begin ()
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_shortcut ( "vdadecoder" )
56     add_integer ( "avcodec-vda-pix-fmt", 0, VDA_PIX_FMT_TEXT,
57                   VDA_PIX_FMT_LONGTEXT, false)
58         change_integer_list( nvda_pix_fmt_list, nvda_pix_fmt_list_text )
59 vlc_module_end ()
60
61 struct vlc_va_sys_t
62 {
63     struct vda_context  hw_ctx;
64
65     const uint8_t       *p_extradata;
66     int                 i_extradata;
67
68     vlc_fourcc_t        i_chroma;
69
70     copy_cache_t        image_cache;
71
72     vlc_object_t        *p_log;
73
74 };
75
76 typedef struct vlc_va_sys_t vlc_va_vda_t;
77
78 static vlc_va_vda_t *vlc_va_vda_Get( vlc_va_t *p_va )
79 {
80     return p_va->sys;
81 }
82
83 /*****************************************************************************
84  * vda_Copy420YpCbCr8Planar: copy y420 CVPixelBuffer to picture_t
85  *****************************************************************************/
86 static void vda_Copy420YpCbCr8Planar( picture_t *p_pic,
87                                       CVPixelBufferRef buffer,
88                                       unsigned i_width,
89                                       unsigned i_height,
90                                       copy_cache_t *cache )
91 {
92     uint8_t *pp_plane[3];
93     size_t  pi_pitch[3];
94
95     CVPixelBufferLockBaseAddress( buffer, 0 );
96
97     for( int i = 0; i < 3; i++ )
98     {
99         pp_plane[i] = CVPixelBufferGetBaseAddressOfPlane( buffer, i );
100         pi_pitch[i] = CVPixelBufferGetBytesPerRowOfPlane( buffer, i );
101     }
102
103     CopyFromYv12( p_pic, pp_plane, pi_pitch,
104                   i_width, i_height, cache );
105
106     CVPixelBufferUnlockBaseAddress( buffer, 0 );
107 }
108
109 /*****************************************************************************
110  * vda_Copy422YpCbCr8: copy 2vuy CVPixelBuffer to picture_t
111  *****************************************************************************/
112 static void vda_Copy422YpCbCr8( picture_t *p_pic,
113                                 CVPixelBufferRef buffer )
114 {
115     int i_plane, i_line, i_dst_stride, i_src_stride;
116     uint8_t *p_dst, *p_src;
117
118     CVPixelBufferLockBaseAddress( buffer, 0 );
119
120     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
121     {
122         p_dst = p_pic->p[i_plane].p_pixels;
123         p_src = CVPixelBufferGetBaseAddressOfPlane( buffer, i_plane );
124         i_dst_stride  = p_pic->p[i_plane].i_pitch;
125         i_src_stride  = CVPixelBufferGetBytesPerRowOfPlane( buffer, i_plane );
126
127         for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines ; i_line++ )
128         {
129             memcpy( p_dst, p_src, i_src_stride );
130
131             p_src += i_src_stride;
132             p_dst += i_dst_stride;
133         }
134     }
135
136     CVPixelBufferUnlockBaseAddress( buffer, 0 );
137 }
138
139 static int Setup( vlc_va_t *p_external, void **pp_hw_ctx, vlc_fourcc_t *pi_chroma,
140                   int i_width, int i_height )
141 {
142
143     vlc_va_vda_t *p_va = vlc_va_vda_Get( p_external );
144
145     if( p_va->hw_ctx.width == i_width
146         && p_va->hw_ctx.height == i_height
147         && p_va->hw_ctx.decoder )
148     {
149         *pp_hw_ctx = &p_va->hw_ctx;
150         *pi_chroma = p_va->i_chroma;
151         return VLC_SUCCESS;
152     }
153
154     if( p_va->hw_ctx.decoder )
155     {
156         ff_vda_destroy_decoder( &p_va->hw_ctx );
157         goto ok;
158     }
159
160     memset( &p_va->hw_ctx, 0, sizeof(p_va->hw_ctx) );
161     p_va->hw_ctx.width = i_width;
162     p_va->hw_ctx.height = i_height;
163     p_va->hw_ctx.format = 'avc1';
164     p_va->hw_ctx.use_sync_decoding = 1;
165
166     int i_pix_fmt = var_CreateGetInteger( p_va->p_log, "avcodec-vda-pix-fmt" );
167
168     switch( i_pix_fmt )
169     {
170         case 1 :
171             p_va->hw_ctx.cv_pix_fmt_type = kCVPixelFormatType_422YpCbCr8;
172             p_va->i_chroma = VLC_CODEC_UYVY;
173             break;
174         case 0 :
175         default :
176             p_va->hw_ctx.cv_pix_fmt_type = kCVPixelFormatType_420YpCbCr8Planar;
177             p_va->i_chroma = VLC_CODEC_I420;
178             CopyInitCache( &p_va->image_cache, i_width );
179     }
180
181 ok:
182     /* Setup the libavcodec hardware context */
183     *pp_hw_ctx = &p_va->hw_ctx;
184     *pi_chroma = p_va->i_chroma;
185
186     /* create the decoder */
187     int status = ff_vda_create_decoder( &p_va->hw_ctx,
188                                         p_va->p_extradata,
189                                         p_va->i_extradata );
190     if( status )
191     {
192         msg_Err( p_va->p_log, "Failed to create the decoder : %i", status );
193         return VLC_EGENERIC;
194     }
195
196     return VLC_SUCCESS;
197 }
198
199 static int Get( vlc_va_t *p_external, AVFrame *p_ff )
200 {
201     VLC_UNUSED( p_external );
202
203     /* */
204     for( int i = 0; i < 4; i++ )
205     {
206         p_ff->data[i] = NULL;
207         p_ff->linesize[i] = 0;
208
209         if( i == 0 || i == 3 )
210         p_ff->data[i] = 1; // dummy
211     }
212
213     return VLC_SUCCESS;
214 }
215
216 static int Extract( vlc_va_t *p_external, picture_t *p_picture, AVFrame *p_ff )
217 {
218     vlc_va_vda_t *p_va = vlc_va_vda_Get( p_external );
219     CVPixelBufferRef cv_buffer = ( CVPixelBufferRef )p_ff->data[3];
220
221     if( !cv_buffer )
222     {
223         msg_Dbg( p_va->p_log, "Frame buffer is empty.");
224         return VLC_EGENERIC;
225     }
226
227     if( p_va->hw_ctx.cv_pix_fmt_type == kCVPixelFormatType_420YpCbCr8Planar )
228     {
229         if( !p_va->image_cache.buffer )
230             return VLC_EGENERIC;
231
232         vda_Copy420YpCbCr8Planar( p_picture,
233                                   cv_buffer,
234                                   p_va->hw_ctx.width,
235                                   p_va->hw_ctx.height,
236                                   &p_va->image_cache );
237     }
238     else
239         vda_Copy422YpCbCr8( p_picture, cv_buffer );
240
241     return VLC_SUCCESS;
242 }
243
244 static void Release( vlc_va_t *p_external, AVFrame *p_ff )
245 {
246     VLC_UNUSED( p_external );
247     CVPixelBufferRef cv_buffer = ( CVPixelBufferRef )p_ff->data[3];
248
249     if ( cv_buffer )
250         CFRelease( cv_buffer );
251 }
252
253 static void Close( vlc_va_t *p_external )
254 {
255     vlc_va_vda_t *p_va = vlc_va_vda_Get( p_external );
256
257     ff_vda_destroy_decoder( &p_va->hw_ctx ) ;
258
259     if( p_va->hw_ctx.cv_pix_fmt_type == kCVPixelFormatType_420YpCbCr8Planar )
260         CopyCleanCache( &p_va->image_cache );
261
262     free( p_va );
263 }
264
265 static int Open( vlc_va_t *external, int i_codec_id, const es_format_t *fmt )
266 {
267     if( i_codec_id != CODEC_ID_H264 )
268         return NULL;
269
270     if( fmt->p_extra == NULL || fmt->i_extra < 7 )
271     {
272         msg_Warn( external, "VDA requires extradata." );
273         return NULL;
274     }
275
276     vlc_va_vda_t *p_va = calloc( 1, sizeof(*p_va) );
277     if( !p_va )
278         return NULL;
279
280     p_va->p_log = VLC_OBJECT(external);
281     p_va->p_extradata = fmt->p_extra;
282     p_va->i_extradata = fmt->i_extra;
283
284     external->sys = p_va;
285     external->description = (char *)"VDA";
286     external->pix_fmt = PIX_FMT_VDA_VLD;
287     external->setup = Setup;
288     external->get = Get;
289     external->release = Release;
290     external->extract = Extract;
291
292     return VLC_SUCCESS;
293 }