2 * gstvaapidecoder_objects.c - VA decoder objects helpers
4 * Copyright (C) 2010-2011 Splitted-Desktop Systems
5 * Copyright (C) 2011-2013 Intel Corporation
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * as published by the Free Software Foundation; either version 2.1
10 * of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301 USA
25 #include <gst/vaapi/gstvaapicontext.h>
26 #include "gstvaapidecoder_objects.h"
27 #include "gstvaapidecoder_priv.h"
28 #include "gstvaapisurfaceproxy_priv.h"
29 #include "gstvaapicompat.h"
30 #include "gstvaapiutils.h"
33 #include "gstvaapidebug.h"
35 #define GET_DECODER(obj) GST_VAAPI_DECODER_CAST((obj)->parent_instance.codec)
36 #define GET_CONTEXT(obj) GET_DECODER(obj)->context
37 #define GET_VA_DISPLAY(obj) GET_DECODER(obj)->va_display
38 #define GET_VA_CONTEXT(obj) GET_DECODER(obj)->va_context
41 gst_video_codec_frame_clear(GstVideoCodecFrame **frame_ptr)
45 gst_video_codec_frame_unref(*frame_ptr);
49 /* ------------------------------------------------------------------------- */
50 /* --- Pictures --- */
51 /* ------------------------------------------------------------------------- */
53 GST_VAAPI_CODEC_DEFINE_TYPE(GstVaapiPicture, gst_vaapi_picture);
56 GST_VAAPI_CREATE_PICTURE_FLAG_CLONE = 1 << 0,
57 GST_VAAPI_CREATE_PICTURE_FLAG_FIELD = 1 << 1,
61 destroy_slice_cb(gpointer data, gpointer user_data)
63 GstVaapiMiniObject * const object = data;
65 gst_vaapi_mini_object_unref(object);
69 gst_vaapi_picture_destroy(GstVaapiPicture *picture)
71 if (picture->slices) {
72 g_ptr_array_foreach(picture->slices, destroy_slice_cb, NULL);
73 g_ptr_array_free(picture->slices, TRUE);
74 picture->slices = NULL;
77 gst_vaapi_mini_object_replace((GstVaapiMiniObject **)&picture->iq_matrix,
79 gst_vaapi_mini_object_replace((GstVaapiMiniObject **)&picture->huf_table,
81 gst_vaapi_mini_object_replace((GstVaapiMiniObject **)&picture->bitplane,
85 gst_vaapi_surface_proxy_unref(picture->proxy);
86 picture->proxy = NULL;
88 picture->surface_id = VA_INVALID_ID;
89 picture->surface = NULL;
91 vaapi_destroy_buffer(GET_VA_DISPLAY(picture), &picture->param_id);
92 picture->param = NULL;
94 gst_video_codec_frame_clear(&picture->frame);
95 gst_vaapi_picture_replace(&picture->parent_picture, NULL);
99 gst_vaapi_picture_create(
100 GstVaapiPicture *picture,
101 const GstVaapiCodecObjectConstructorArgs *args
106 if (args->flags & GST_VAAPI_CREATE_PICTURE_FLAG_CLONE) {
107 GstVaapiPicture * const parent_picture = GST_VAAPI_PICTURE(args->data);
109 picture->parent_picture = gst_vaapi_picture_ref(parent_picture);
111 picture->proxy = gst_vaapi_surface_proxy_ref(parent_picture->proxy);
112 picture->type = parent_picture->type;
113 picture->pts = parent_picture->pts;
114 picture->poc = parent_picture->poc;
116 // Copy all picture flags but "output"
117 GST_VAAPI_PICTURE_FLAG_SET(
119 GST_VAAPI_PICTURE_FLAGS(parent_picture) &
120 (GST_VAAPI_PICTURE_FLAG_SKIPPED |
121 GST_VAAPI_PICTURE_FLAG_REFERENCE |
122 GST_VAAPI_PICTURE_FLAG_INTERLACED |
123 GST_VAAPI_PICTURE_FLAG_FF |
124 GST_VAAPI_PICTURE_FLAG_TFF)
127 picture->structure = parent_picture->structure;
128 if ((args->flags & GST_VAAPI_CREATE_PICTURE_FLAG_FIELD) &&
129 GST_VAAPI_PICTURE_IS_INTERLACED(picture)) {
130 switch (picture->structure) {
131 case GST_VAAPI_PICTURE_STRUCTURE_TOP_FIELD:
132 picture->structure = GST_VAAPI_PICTURE_STRUCTURE_BOTTOM_FIELD;
134 case GST_VAAPI_PICTURE_STRUCTURE_BOTTOM_FIELD:
135 picture->structure = GST_VAAPI_PICTURE_STRUCTURE_TOP_FIELD;
138 GST_VAAPI_PICTURE_FLAG_UNSET(picture, GST_VAAPI_PICTURE_FLAG_FF);
141 if (parent_picture->has_crop_rect) {
142 picture->has_crop_rect = TRUE;
143 picture->crop_rect = parent_picture->crop_rect;
147 picture->type = GST_VAAPI_PICTURE_TYPE_NONE;
148 picture->pts = GST_CLOCK_TIME_NONE;
151 gst_vaapi_context_get_surface_proxy(GET_CONTEXT(picture));
155 picture->structure = GST_VAAPI_PICTURE_STRUCTURE_FRAME;
156 GST_VAAPI_PICTURE_FLAG_SET(picture, GST_VAAPI_PICTURE_FLAG_FF);
158 picture->surface = GST_VAAPI_SURFACE_PROXY_SURFACE(picture->proxy);
159 picture->surface_id = GST_VAAPI_SURFACE_PROXY_SURFACE_ID(picture->proxy);
161 picture->param_id = VA_INVALID_ID;
162 success = vaapi_create_buffer(
163 GET_VA_DISPLAY(picture),
164 GET_VA_CONTEXT(picture),
165 VAPictureParameterBufferType,
173 picture->param_size = args->param_size;
175 picture->slices = g_ptr_array_new();
176 if (!picture->slices)
179 picture->frame = gst_video_codec_frame_ref(
180 GST_VAAPI_DECODER_CODEC_FRAME(GET_DECODER(picture)));
185 gst_vaapi_picture_new(
186 GstVaapiDecoder *decoder,
191 GstVaapiCodecObject *object;
193 object = gst_vaapi_codec_object_new(
194 &GstVaapiPictureClass,
195 GST_VAAPI_CODEC_BASE(decoder),
202 return GST_VAAPI_PICTURE_CAST(object);
206 gst_vaapi_picture_new_field(GstVaapiPicture *picture)
208 GstVaapiDecoder * const decoder = GET_DECODER(picture);
209 GstVaapiCodecObject *object;
211 object = gst_vaapi_codec_object_new(
212 gst_vaapi_codec_object_get_class(&picture->parent_instance),
213 GST_VAAPI_CODEC_BASE(decoder),
214 NULL, picture->param_size,
216 (GST_VAAPI_CREATE_PICTURE_FLAG_CLONE|
217 GST_VAAPI_CREATE_PICTURE_FLAG_FIELD)
221 return GST_VAAPI_PICTURE_CAST(object);
225 gst_vaapi_picture_add_slice(GstVaapiPicture *picture, GstVaapiSlice *slice)
227 g_return_if_fail(GST_VAAPI_IS_PICTURE(picture));
228 g_return_if_fail(GST_VAAPI_IS_SLICE(slice));
230 g_ptr_array_add(picture->slices, slice);
234 do_decode(VADisplay dpy, VAContextID ctx, VABufferID *buf_id, void **buf_ptr)
238 vaapi_unmap_buffer(dpy, *buf_id, buf_ptr);
240 status = vaRenderPicture(dpy, ctx, buf_id, 1);
241 if (!vaapi_check_status(status, "vaRenderPicture()"))
244 /* XXX: vaRenderPicture() is meant to destroy the VA buffer implicitly */
245 vaapi_destroy_buffer(dpy, buf_id);
250 gst_vaapi_picture_decode(GstVaapiPicture *picture)
252 GstVaapiIqMatrix *iq_matrix;
253 GstVaapiBitPlane *bitplane;
254 GstVaapiHuffmanTable *huf_table;
255 VADisplay va_display;
256 VAContextID va_context;
260 g_return_val_if_fail(GST_VAAPI_IS_PICTURE(picture), FALSE);
262 va_display = GET_VA_DISPLAY(picture);
263 va_context = GET_VA_CONTEXT(picture);
265 GST_DEBUG("decode picture 0x%08x", picture->surface_id);
267 status = vaBeginPicture(va_display, va_context, picture->surface_id);
268 if (!vaapi_check_status(status, "vaBeginPicture()"))
271 if (!do_decode(va_display, va_context, &picture->param_id, &picture->param))
274 iq_matrix = picture->iq_matrix;
275 if (iq_matrix && !do_decode(va_display, va_context,
276 &iq_matrix->param_id, &iq_matrix->param))
279 bitplane = picture->bitplane;
280 if (bitplane && !do_decode(va_display, va_context,
281 &bitplane->data_id, (void **)&bitplane->data))
284 huf_table = picture->huf_table;
285 if (huf_table && !do_decode(va_display, va_context,
286 &huf_table->param_id,
287 (void **)&huf_table->param))
290 for (i = 0; i < picture->slices->len; i++) {
291 GstVaapiSlice * const slice = g_ptr_array_index(picture->slices, i);
292 VABufferID va_buffers[2];
294 huf_table = slice->huf_table;
295 if (huf_table && !do_decode(va_display, va_context,
296 &huf_table->param_id, (void **)&huf_table->param))
299 vaapi_unmap_buffer(va_display, slice->param_id, NULL);
300 va_buffers[0] = slice->param_id;
301 va_buffers[1] = slice->data_id;
303 status = vaRenderPicture(va_display, va_context, va_buffers, 2);
304 if (!vaapi_check_status(status, "vaRenderPicture()"))
307 vaapi_destroy_buffer(va_display, &slice->param_id);
308 vaapi_destroy_buffer(va_display, &slice->data_id);
311 status = vaEndPicture(va_display, va_context);
312 if (!vaapi_check_status(status, "vaEndPicture()"))
318 do_output(GstVaapiPicture *picture)
320 GstVideoCodecFrame * const out_frame = picture->frame;
321 GstVaapiSurfaceProxy *proxy;
324 if (GST_VAAPI_PICTURE_IS_OUTPUT(picture))
330 proxy = gst_vaapi_surface_proxy_ref(picture->proxy);
332 if (picture->has_crop_rect)
333 gst_vaapi_surface_proxy_set_crop_rect(proxy, &picture->crop_rect);
335 gst_video_codec_frame_set_user_data(out_frame,
336 proxy, (GDestroyNotify)gst_vaapi_mini_object_unref);
338 out_frame->pts = picture->pts;
340 if (GST_VAAPI_PICTURE_IS_SKIPPED(picture))
341 GST_VIDEO_CODEC_FRAME_FLAG_SET(out_frame,
342 GST_VIDEO_CODEC_FRAME_FLAG_DECODE_ONLY);
344 if (GST_VAAPI_PICTURE_IS_INTERLACED(picture)) {
345 flags |= GST_VAAPI_SURFACE_PROXY_FLAG_INTERLACED;
346 if (GST_VAAPI_PICTURE_IS_TFF(picture))
347 flags |= GST_VAAPI_SURFACE_PROXY_FLAG_TFF;
349 GST_VAAPI_SURFACE_PROXY_FLAG_SET(proxy, flags);
351 gst_vaapi_decoder_push_frame(GET_DECODER(picture), out_frame);
352 gst_video_codec_frame_clear(&picture->frame);
354 GST_VAAPI_PICTURE_FLAG_SET(picture, GST_VAAPI_PICTURE_FLAG_OUTPUT);
359 gst_vaapi_picture_output(GstVaapiPicture *picture)
361 g_return_val_if_fail(GST_VAAPI_IS_PICTURE(picture), FALSE);
363 if (G_UNLIKELY(picture->parent_picture)) {
364 /* Emit the first field to GstVideoDecoder so that to release
365 the underlying GstVideoCodecFrame. However, mark this
366 picture as skipped so that to not display it */
367 GstVaapiPicture * const parent_picture = picture->parent_picture;
369 if (!GST_VAAPI_PICTURE_IS_INTERLACED(parent_picture))
371 if (!GST_VAAPI_PICTURE_IS_FIRST_FIELD(parent_picture))
373 GST_VAAPI_PICTURE_FLAG_SET(parent_picture,
374 GST_VAAPI_PICTURE_FLAG_SKIPPED);
375 if (!do_output(parent_picture))
379 return do_output(picture);
383 gst_vaapi_picture_set_crop_rect(GstVaapiPicture *picture,
384 const GstVaapiRectangle *crop_rect)
386 g_return_if_fail(GST_VAAPI_IS_PICTURE(picture));
388 picture->has_crop_rect = crop_rect != NULL;
389 if (picture->has_crop_rect)
390 picture->crop_rect = *crop_rect;
393 /* ------------------------------------------------------------------------- */
395 /* ------------------------------------------------------------------------- */
397 GST_VAAPI_CODEC_DEFINE_TYPE(GstVaapiSlice, gst_vaapi_slice);
400 gst_vaapi_slice_destroy(GstVaapiSlice *slice)
402 VADisplay const va_display = GET_VA_DISPLAY(slice);
404 gst_vaapi_mini_object_replace((GstVaapiMiniObject **)&slice->huf_table,
407 vaapi_destroy_buffer(va_display, &slice->data_id);
408 vaapi_destroy_buffer(va_display, &slice->param_id);
413 gst_vaapi_slice_create(
414 GstVaapiSlice *slice,
415 const GstVaapiCodecObjectConstructorArgs *args
418 VASliceParameterBufferBase *slice_param;
421 slice->data_id = VA_INVALID_ID;
422 success = vaapi_create_buffer(
423 GET_VA_DISPLAY(slice),
424 GET_VA_CONTEXT(slice),
425 VASliceDataBufferType,
434 slice->param_id = VA_INVALID_ID;
435 success = vaapi_create_buffer(
436 GET_VA_DISPLAY(slice),
437 GET_VA_CONTEXT(slice),
438 VASliceParameterBufferType,
447 slice_param = slice->param;
448 slice_param->slice_data_size = args->data_size;
449 slice_param->slice_data_offset = 0;
450 slice_param->slice_data_flag = VA_SLICE_DATA_FLAG_ALL;
456 GstVaapiDecoder *decoder,
463 GstVaapiCodecObject *object;
465 object = gst_vaapi_codec_object_new(
467 GST_VAAPI_CODEC_BASE(decoder),
472 return GST_VAAPI_SLICE_CAST(object);