decoder: get rid of GstVaapiDecoderUnit::buffer field.
[vaapi:gstreamer-vaapi.git] / gst-libs / gst / vaapi / gstvaapidecoder_mpeg4.c
1 /*
2  *  gstvaapidecoder_mpeg4.c - MPEG-4 decoder
3  *
4  *  Copyright (C) 2011-2013 Intel Corporation
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public License
8  *  as published by the Free Software Foundation; either version 2.1
9  *  of the License, or (at your option) any later version.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free
18  *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  *  Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * SECTION:gstvaapidecoder_mpeg4
24  * @short_description: MPEG-4 decoder, include h263/divx/xvid support
25  */
26
27 #include "sysdeps.h"
28 #include <string.h>
29 #include <gst/base/gstbitreader.h>
30 #include <gst/codecparsers/gstmpeg4parser.h>
31 #include "gstvaapidecoder_mpeg4.h"
32 #include "gstvaapidecoder_objects.h"
33 #include "gstvaapidecoder_priv.h"
34 #include "gstvaapidisplay_priv.h"
35 #include "gstvaapiobject_priv.h"
36
37 #define DEBUG 1
38 #include "gstvaapidebug.h"
39
40 G_DEFINE_TYPE(GstVaapiDecoderMpeg4,
41               gst_vaapi_decoder_mpeg4,
42               GST_VAAPI_TYPE_DECODER)
43
44 #define GST_VAAPI_DECODER_MPEG4_GET_PRIVATE(obj)                \
45     (G_TYPE_INSTANCE_GET_PRIVATE((obj),                         \
46                                  GST_VAAPI_TYPE_DECODER_MPEG4,  \
47                                  GstVaapiDecoderMpeg4Private))
48
49 struct _GstVaapiDecoderMpeg4Private {
50     GstVaapiProfile                 profile;
51     guint                           level; 
52     guint                           width; 
53     guint                           height;
54     guint                           fps_n;
55     guint                           fps_d; 
56     guint                           coding_type;
57     GstMpeg4VisualObjectSequence    vos_hdr;
58     GstMpeg4VisualObject            vo_hdr;
59     GstMpeg4VideoSignalType         signal_type;
60     GstMpeg4VideoObjectLayer        vol_hdr;
61     GstMpeg4VideoObjectPlane        vop_hdr;
62     GstMpeg4VideoPlaneShortHdr      svh_hdr;
63     GstMpeg4VideoPacketHdr          packet_hdr;
64     GstMpeg4SpriteTrajectory        sprite_trajectory;
65     VAIQMatrixBufferMPEG4           iq_matrix;
66     GstVaapiPicture                *curr_picture;
67     // forward reference pic
68     GstVaapiPicture                *next_picture;
69     // backward reference pic
70     GstVaapiPicture                *prev_picture;
71     GstClockTime                    seq_pts;
72     GstClockTime                    gop_pts;
73     GstClockTime                    pts_diff;
74     GstClockTime                    max_pts;
75     // anchor sync time base for any picture type, 
76     // it is time base of backward reference frame
77     GstClockTime                    last_sync_time; 
78     // time base for recent I/P/S frame, 
79     // it is time base of forward reference frame for B frame
80     GstClockTime                    sync_time; 
81
82     /* last non-b-frame time by resolution */
83     GstClockTime                    last_non_b_scale_time;
84     GstClockTime                    non_b_scale_time;
85     GstClockTime                    trb;
86     GstClockTime                    trd;
87     // temporal_reference of previous frame of svh
88     guint8                          prev_t_ref;
89     guint                           is_constructed          : 1;
90     guint                           is_opened               : 1;
91     guint                           is_first_field          : 1;
92     guint                           size_changed            : 1;
93     guint                           profile_changed         : 1;
94     guint                           progressive_sequence    : 1;
95     guint                           closed_gop              : 1;
96     guint                           broken_link             : 1;
97     guint                           calculate_pts_diff      : 1;
98     guint                           is_svh                  : 1;
99 };
100
101 static void
102 gst_vaapi_decoder_mpeg4_close(GstVaapiDecoderMpeg4 *decoder)
103 {
104     GstVaapiDecoderMpeg4Private * const priv = decoder->priv;
105
106     gst_vaapi_picture_replace(&priv->curr_picture, NULL);
107     gst_vaapi_picture_replace(&priv->next_picture, NULL);
108     gst_vaapi_picture_replace(&priv->prev_picture, NULL);
109 }
110
111 static gboolean
112 gst_vaapi_decoder_mpeg4_open(GstVaapiDecoderMpeg4 *decoder)
113 {
114     GstVaapiDecoder *const base_decoder = GST_VAAPI_DECODER(decoder);
115     GstVaapiDecoderMpeg4Private * const priv = decoder->priv;
116     GstCaps *caps = NULL;
117     GstStructure *structure = NULL;
118
119     gst_vaapi_decoder_mpeg4_close(decoder);
120
121     priv->is_svh = 0;
122     caps = gst_vaapi_decoder_get_caps(base_decoder);
123     if (caps) {
124         structure = gst_caps_get_structure(caps, 0);
125         if (structure) {
126             if (gst_structure_has_name(structure, "video/x-h263")) {
127                 priv->is_svh = 1;
128                 priv->profile = GST_VAAPI_PROFILE_MPEG4_SIMPLE;
129                 priv->prev_t_ref = -1;
130             }
131         }
132     }
133     return TRUE;
134 }
135
136 static void
137 gst_vaapi_decoder_mpeg4_destroy(GstVaapiDecoderMpeg4 *decoder)
138 {
139     gst_vaapi_decoder_mpeg4_close(decoder);
140 }
141
142 static gboolean
143 gst_vaapi_decoder_mpeg4_create(GstVaapiDecoderMpeg4 *decoder)
144 {
145     if (!GST_VAAPI_DECODER_CODEC(decoder))
146         return FALSE;
147     return TRUE;
148 }
149
150 static inline void
151 copy_quant_matrix(guint8 dst[64], const guint8 src[64])
152 {
153     memcpy(dst, src, 64);
154 }
155
156 static GstVaapiDecoderStatus
157 ensure_context(GstVaapiDecoderMpeg4 *decoder)
158 {
159     GstVaapiDecoderMpeg4Private * const priv = decoder->priv;
160     GstVaapiProfile profiles[2];
161     GstVaapiEntrypoint entrypoint = GST_VAAPI_ENTRYPOINT_VLD;
162     guint i, n_profiles = 0;
163     gboolean reset_context = FALSE;
164
165     if (priv->profile_changed) {
166         GST_DEBUG("profile changed");
167         priv->profile_changed = FALSE;
168         reset_context         = TRUE;
169
170         profiles[n_profiles++] = priv->profile;
171         if (priv->profile == GST_VAAPI_PROFILE_MPEG4_SIMPLE)
172             profiles[n_profiles++] = GST_VAAPI_PROFILE_MPEG4_ADVANCED_SIMPLE;
173
174         for (i = 0; i < n_profiles; i++) {
175             if (gst_vaapi_display_has_decoder(GST_VAAPI_DECODER_DISPLAY(decoder),
176                                               profiles[i], entrypoint))
177                 break;
178         }
179         if (i == n_profiles)
180             return GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_PROFILE;
181         priv->profile = profiles[i];
182     }
183
184     if (priv->size_changed) {
185         GST_DEBUG("size changed");
186         priv->size_changed = FALSE;
187         reset_context      = TRUE;
188     }
189
190     if (reset_context) {
191         GstVaapiContextInfo info;
192
193         info.profile    = priv->profile;
194         info.entrypoint = entrypoint;
195         info.width      = priv->width;
196         info.height     = priv->height;
197         info.ref_frames = 2;
198         reset_context   = gst_vaapi_decoder_ensure_context(
199             GST_VAAPI_DECODER(decoder),
200             &info
201         );
202         if (!reset_context)
203             return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
204     }
205     return GST_VAAPI_DECODER_STATUS_SUCCESS;
206 }
207
208 static GstVaapiDecoderStatus
209 ensure_quant_matrix(GstVaapiDecoderMpeg4 *decoder, GstVaapiPicture *picture)
210 {
211     GstVaapiDecoderMpeg4Private * const priv = decoder->priv;
212     VAIQMatrixBufferMPEG4 *iq_matrix;
213
214     if (!priv->vol_hdr.load_intra_quant_mat && !priv->vol_hdr.load_non_intra_quant_mat) {
215             return GST_VAAPI_DECODER_STATUS_SUCCESS;
216     }
217
218     picture->iq_matrix = GST_VAAPI_IQ_MATRIX_NEW(MPEG4, decoder);
219     if (!picture->iq_matrix) {
220         GST_DEBUG("failed to allocate IQ matrix");
221         return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
222     }
223     iq_matrix = picture->iq_matrix->param;
224
225     if (priv->vol_hdr.load_intra_quant_mat) {
226         iq_matrix->load_intra_quant_mat = 1;
227         copy_quant_matrix(iq_matrix->intra_quant_mat,
228                           priv->vol_hdr.intra_quant_mat);
229     }
230     else
231         iq_matrix->load_intra_quant_mat = 0;
232
233     if (priv->vol_hdr.load_non_intra_quant_mat) {
234         iq_matrix->load_non_intra_quant_mat = 1;
235         copy_quant_matrix(iq_matrix->non_intra_quant_mat,
236                       priv->vol_hdr.non_intra_quant_mat);
237     }
238     else
239         iq_matrix->load_non_intra_quant_mat = 0;
240     
241
242     return GST_VAAPI_DECODER_STATUS_SUCCESS;
243 }
244
245 static inline GstVaapiDecoderStatus
246 render_picture(GstVaapiDecoderMpeg4 *decoder, GstVaapiPicture *picture)
247 {
248     if (!gst_vaapi_picture_output(picture))
249         return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
250     return GST_VAAPI_DECODER_STATUS_SUCCESS;
251 }
252
253 /* decode_picture() start to decode a frame/picture
254  * decode_current_picture() finishe decoding a frame/picture 
255  * (commit buffer to driver for decoding)
256  */
257 static GstVaapiDecoderStatus
258 decode_current_picture(GstVaapiDecoderMpeg4 *decoder)
259 {
260     GstVaapiDecoderMpeg4Private * const priv = decoder->priv;
261     GstVaapiPicture * const picture = priv->curr_picture;
262     GstVaapiDecoderStatus status = GST_VAAPI_DECODER_STATUS_SUCCESS;
263
264     if (picture) {
265         if (!gst_vaapi_picture_decode(picture))
266             status = GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
267         if (!GST_VAAPI_PICTURE_IS_REFERENCE(picture)) {
268             if ((priv->prev_picture && priv->next_picture) ||
269                 (priv->closed_gop && priv->next_picture))
270                 status = render_picture(decoder, picture);
271         }
272         gst_vaapi_picture_replace(&priv->curr_picture, NULL);
273     }
274     return status;
275 }
276
277 static GstVaapiDecoderStatus
278 decode_sequence(GstVaapiDecoderMpeg4 *decoder, const guint8 *buf, guint buf_size)
279 {
280     GstVaapiDecoderMpeg4Private * const priv = decoder->priv;
281     GstMpeg4VisualObjectSequence * const vos_hdr = &priv->vos_hdr;
282     GstVaapiProfile profile;
283
284     if (gst_mpeg4_parse_visual_object_sequence(vos_hdr, buf, buf_size) != GST_MPEG4_PARSER_OK) {
285         GST_DEBUG("failed to parse sequence header");
286         return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
287     }
288
289     priv->level = vos_hdr->level;
290     switch (vos_hdr->profile) {
291     case GST_MPEG4_PROFILE_SIMPLE:
292         profile = GST_VAAPI_PROFILE_MPEG4_SIMPLE;
293         break;
294     case GST_MPEG4_PROFILE_ADVANCED_SIMPLE:
295     case GST_MPEG4_PROFILE_SIMPLE_SCALABLE: /* shared profile with ADVANCED_SIMPLE */
296         profile = GST_VAAPI_PROFILE_MPEG4_ADVANCED_SIMPLE;
297         break;
298     default:
299         GST_DEBUG("unsupported profile %d", vos_hdr->profile);
300         return GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_PROFILE;
301     }
302     if (priv->profile != profile) {
303         priv->profile = profile;
304         priv->profile_changed = TRUE;
305     }
306     priv->seq_pts = GST_VAAPI_DECODER_CODEC_FRAME(decoder)->pts;
307     priv->size_changed          = TRUE;
308
309     return GST_VAAPI_DECODER_STATUS_SUCCESS;
310 }
311
312 static GstVaapiDecoderStatus
313 decode_sequence_end(GstVaapiDecoderMpeg4 *decoder)
314 {
315     GstVaapiDecoderMpeg4Private * const priv = decoder->priv;
316     GstVaapiDecoderStatus status;
317
318     if (priv->curr_picture) {
319         status = decode_current_picture(decoder);
320         if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
321             return status;
322         status = render_picture(decoder, priv->curr_picture);
323         if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
324             return status;
325     }
326
327     if (priv->next_picture) {
328         status = render_picture(decoder, priv->next_picture);
329         if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
330             return status;
331     }
332     return GST_VAAPI_DECODER_STATUS_END_OF_STREAM;
333 }
334
335 static GstVaapiDecoderStatus
336 decode_visual_object(GstVaapiDecoderMpeg4 *decoder, const guint8 *buf, guint buf_size)
337 {
338     GstVaapiDecoderMpeg4Private * const priv = decoder->priv;
339     GstMpeg4VisualObject * vo_hdr = &priv->vo_hdr;
340     GstMpeg4VideoSignalType * signal_type = &priv->signal_type;
341
342     if (gst_mpeg4_parse_visual_object (vo_hdr, signal_type, buf, buf_size) != GST_MPEG4_PARSER_OK) {
343         GST_DEBUG("failed to parse visual object");
344         return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
345     }
346
347     /* XXX: video_signal_type isn't used for decoding */
348     return GST_VAAPI_DECODER_STATUS_SUCCESS;
349 }
350
351 static GstVaapiDecoderStatus
352 decode_video_object_layer(GstVaapiDecoderMpeg4 *decoder, const guint8 *buf, guint buf_size)
353 {
354     GstVaapiDecoder * const base_decoder = GST_VAAPI_DECODER(decoder);
355     GstVaapiDecoderMpeg4Private * const priv = decoder->priv;
356     GstMpeg4VisualObject * vo_hdr = &priv->vo_hdr;
357     GstMpeg4VideoObjectLayer * vol_hdr = &priv->vol_hdr;
358
359     if (gst_mpeg4_parse_video_object_layer (vol_hdr, vo_hdr, buf, buf_size) != GST_MPEG4_PARSER_OK) {
360         GST_DEBUG("failed to parse video object layer");
361         return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
362     }
363
364     priv->width  = vol_hdr->width;
365     priv->height = vol_hdr->height;
366
367     priv->progressive_sequence  = !vol_hdr->interlaced;
368
369     if (vol_hdr->fixed_vop_rate) {
370         priv->fps_n = vol_hdr->vop_time_increment_resolution;
371         priv->fps_d = vol_hdr->fixed_vop_time_increment;
372         gst_vaapi_decoder_set_framerate(base_decoder, priv->fps_n, priv->fps_d);    
373     }
374
375     gst_vaapi_decoder_set_pixel_aspect_ratio(base_decoder, priv->vol_hdr.par_width, priv->vol_hdr.par_height);
376     gst_vaapi_decoder_set_picture_size(base_decoder, priv->width, priv->height);
377
378     return GST_VAAPI_DECODER_STATUS_SUCCESS;
379 }
380
381 static GstVaapiDecoderStatus
382 decode_gop(GstVaapiDecoderMpeg4 *decoder, const guint8 *buf, guint buf_size)
383 {
384     GstVaapiDecoderMpeg4Private * const priv = decoder->priv;
385     GstMpeg4GroupOfVOP gop;
386     GstClockTime gop_time;
387
388     if (buf_size >4) {
389         if (gst_mpeg4_parse_group_of_vop(&gop, buf, buf_size) != GST_MPEG4_PARSER_OK) {
390         GST_DEBUG("failed to parse GOP");
391         return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
392         }
393     }
394     else {
395         gop.closed          = 1;
396         gop.broken_link     = 0;
397         gop.hours           = 0;
398         gop.minutes         = 0;
399         gop.seconds         = 0;
400     }
401
402     priv->closed_gop  = gop.closed;
403     priv->broken_link = gop.broken_link;
404
405     GST_DEBUG("GOP %02u:%02u:%02u (closed_gop %d, broken_link %d)",
406               gop.hours, gop.minutes, gop.seconds,
407               priv->closed_gop, priv->broken_link);
408
409     gop_time             = gop.hours * 3600 + gop.minutes * 60 + gop.seconds;
410     priv->last_sync_time = gop_time;
411     priv->sync_time      = gop_time;
412     
413     if (priv->gop_pts != GST_CLOCK_TIME_NONE)
414         priv->pts_diff += gop_time * GST_SECOND - priv->gop_pts;
415     priv->gop_pts = gop_time * GST_SECOND;
416     priv->calculate_pts_diff = TRUE;
417     priv->is_first_field = TRUE;
418
419     return GST_VAAPI_DECODER_STATUS_SUCCESS;
420 }
421
422 void
423 calculate_pts_diff(GstVaapiDecoderMpeg4 *decoder,
424                       GstMpeg4VideoObjectLayer *vol_hdr,
425                       GstMpeg4VideoObjectPlane *vop_hdr)
426 {
427     GstVaapiDecoderMpeg4Private * const priv = decoder->priv;
428     GstClockTime frame_timestamp;
429
430     frame_timestamp = GST_VAAPI_DECODER_CODEC_FRAME(decoder)->pts;
431     if (frame_timestamp && frame_timestamp != GST_CLOCK_TIME_NONE) {
432         /* Buffer with timestamp */
433         if (priv->max_pts != GST_CLOCK_TIME_NONE &&
434             frame_timestamp < priv->max_pts) {
435             frame_timestamp = priv->max_pts +
436                 gst_util_uint64_scale((vol_hdr->fixed_vop_rate ?
437                                        vol_hdr->fixed_vop_time_increment : 1),
438                                       GST_SECOND,
439                                       vol_hdr->vop_time_increment_resolution);
440         }
441     } else {
442         /* Buffer without timestamp set */
443         if (priv->max_pts == GST_CLOCK_TIME_NONE) /* first buffer */
444             frame_timestamp = 0;
445         else {
446             GstClockTime tmp_pts;
447             tmp_pts = priv->pts_diff + priv->gop_pts +
448                 vop_hdr->modulo_time_base * GST_SECOND +
449                 gst_util_uint64_scale(vop_hdr->time_increment,
450                                       GST_SECOND,
451                                       vol_hdr->vop_time_increment_resolution);
452             if (tmp_pts > priv->max_pts)
453                 frame_timestamp = tmp_pts;
454             else
455                 frame_timestamp = priv->max_pts +
456                     gst_util_uint64_scale((vol_hdr->fixed_vop_rate ?
457                                            vol_hdr->fixed_vop_time_increment : 1),
458                                            GST_SECOND,
459                                           vol_hdr->vop_time_increment_resolution);
460         }
461     }
462
463     priv->pts_diff = frame_timestamp -
464         (priv->gop_pts + vop_hdr->modulo_time_base * GST_SECOND +
465          gst_util_uint64_scale(vop_hdr->time_increment, GST_SECOND,
466                                vol_hdr->vop_time_increment_resolution));
467 }
468  
469 static GstVaapiDecoderStatus
470 decode_picture(GstVaapiDecoderMpeg4 *decoder, const guint8 *buf, guint buf_size)
471 {
472     GstMpeg4ParseResult parser_result = GST_MPEG4_PARSER_OK;
473     GstVaapiDecoderMpeg4Private * const priv = decoder->priv;
474     GstMpeg4VideoObjectPlane * const vop_hdr = &priv->vop_hdr;
475     GstMpeg4VideoObjectLayer * const vol_hdr = &priv->vol_hdr;
476     GstMpeg4SpriteTrajectory * const sprite_trajectory = &priv->sprite_trajectory;
477     GstVaapiPicture *picture;
478     GstVaapiDecoderStatus status;
479     GstClockTime pts;
480
481     // context depends on priv->width and priv->height, so we move parse_vop a little earlier
482     if (priv->is_svh) {
483         parser_result = gst_mpeg4_parse_video_plane_short_header(&priv->svh_hdr, buf, buf_size);
484
485     }
486     else {
487         parser_result = gst_mpeg4_parse_video_object_plane(vop_hdr, sprite_trajectory, vol_hdr, buf, buf_size);
488         /* Need to skip this frame if VOP was not coded */
489         if (GST_MPEG4_PARSER_OK == parser_result && !vop_hdr->coded)
490             return GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA;
491     }
492
493     if (parser_result != GST_MPEG4_PARSER_OK) {
494         GST_DEBUG("failed to parse picture header");
495         return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
496     }
497
498     if (priv->is_svh) {
499         priv->width = priv->svh_hdr.vop_width;
500         priv->height = priv->svh_hdr.vop_height;
501     }
502     else {
503         if (!vop_hdr->width && !vop_hdr->height) {
504             vop_hdr->width = vol_hdr->width;
505             vop_hdr->height = vol_hdr->height;
506         }
507         priv->width = vop_hdr->width;
508         priv->height = vop_hdr->height;
509     }
510
511     status = ensure_context(decoder);
512     if (status != GST_VAAPI_DECODER_STATUS_SUCCESS) {
513         GST_DEBUG("failed to reset context");
514         return status;
515     }
516
517     if (priv->curr_picture) {
518         status = decode_current_picture(decoder);
519         if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
520             return status;
521     }
522
523     priv->curr_picture = GST_VAAPI_PICTURE_NEW(MPEG4, decoder);
524     if (!priv->curr_picture) {
525         GST_DEBUG("failed to allocate picture");
526         return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
527     }
528     picture = priv->curr_picture;
529
530     status = ensure_quant_matrix(decoder, picture);
531     if (status != GST_VAAPI_DECODER_STATUS_SUCCESS) {
532         GST_DEBUG("failed to reset quantizer matrix");
533         return status;
534     }
535
536     /* 7.6.7 Temporal prediction structure
537      * forward reference frame     B B B B B B      backward reference frame
538      *            |                                              |
539      *  nearest I/P/S in the past with vop_coded ==1             |
540      *                         nearest I/P/S in the future with any vop_coded
541      * fixme, it said that B frame shouldn't use backward reference frame 
542      *        when backward reference frame coded is 0
543      */
544     if (priv->is_svh) {
545         priv->coding_type = priv->svh_hdr.picture_coding_type;
546     }
547     else {
548         priv->coding_type = priv->vop_hdr.coding_type;
549     }
550     switch (priv->coding_type) {
551     case GST_MPEG4_I_VOP:
552         picture->type = GST_VAAPI_PICTURE_TYPE_I;
553         if (priv->is_svh || vop_hdr->coded) 
554             GST_VAAPI_PICTURE_FLAG_SET(picture, GST_VAAPI_PICTURE_FLAG_REFERENCE);
555         break;
556     case GST_MPEG4_P_VOP:
557         picture->type = GST_VAAPI_PICTURE_TYPE_P;
558         if (priv->is_svh || vop_hdr->coded) 
559             GST_VAAPI_PICTURE_FLAG_SET(picture, GST_VAAPI_PICTURE_FLAG_REFERENCE);
560         break;
561     case GST_MPEG4_B_VOP:
562         picture->type = GST_VAAPI_PICTURE_TYPE_B;
563         break;
564     case GST_MPEG4_S_VOP:
565         picture->type = GST_VAAPI_PICTURE_TYPE_S;
566         // see 3.175 reference VOP
567         if (vop_hdr->coded) 
568             GST_VAAPI_PICTURE_FLAG_SET(picture, GST_VAAPI_PICTURE_FLAG_REFERENCE);
569         break;
570     default:
571         GST_DEBUG("unsupported picture type %d", priv->coding_type);
572         return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
573     }
574
575     if (!priv->is_svh && !vop_hdr->coded) {
576         status = render_picture(decoder, priv->prev_picture);
577         return status;
578     }
579
580     if (priv->is_svh) {
581         guint temp_ref = priv->svh_hdr.temporal_reference;
582         if (temp_ref < priv->prev_t_ref) {
583             temp_ref += 256;
584         }
585         guint delta_ref = temp_ref - priv->prev_t_ref;
586
587         pts = priv->sync_time;
588         // see temporal_reference definition in spec, 30000/1001Hz
589         pts += gst_util_uint64_scale(delta_ref, GST_SECOND*1001, 30000);
590         priv->sync_time = pts;
591         priv->prev_t_ref = priv->svh_hdr.temporal_reference;
592     }
593     else {
594         /* Update priv->pts_diff */
595         if (priv->calculate_pts_diff) {
596             calculate_pts_diff(decoder, vol_hdr, vop_hdr);
597             priv->calculate_pts_diff = FALSE;
598         }
599
600         /* Update presentation time, 6.3.5 */
601         if(vop_hdr->coding_type != GST_MPEG4_B_VOP) {
602             // increment basing on decoding order
603             priv->last_sync_time = priv->sync_time;
604             priv->sync_time = priv->last_sync_time + vop_hdr->modulo_time_base;
605             pts = priv->sync_time * GST_SECOND;
606             pts += gst_util_uint64_scale(vop_hdr->time_increment, GST_SECOND, vol_hdr->vop_time_increment_resolution);
607             priv->last_non_b_scale_time = priv->non_b_scale_time;
608             priv->non_b_scale_time = priv->sync_time * vol_hdr->vop_time_increment_resolution + vop_hdr->time_increment;
609             priv->trd  = priv->non_b_scale_time - priv->last_non_b_scale_time;
610         }
611         else {
612             // increment basing on display oder
613             pts = (priv->last_sync_time + vop_hdr->modulo_time_base) * GST_SECOND;
614             pts += gst_util_uint64_scale(vop_hdr->time_increment, GST_SECOND, vol_hdr->vop_time_increment_resolution);
615             priv->trb = (priv->last_sync_time + vop_hdr->modulo_time_base) * vol_hdr->vop_time_increment_resolution +
616                 vop_hdr->time_increment - priv->last_non_b_scale_time;
617         }
618     }
619     picture->pts = pts + priv->pts_diff;
620     if (priv->max_pts == GST_CLOCK_TIME_NONE || priv->max_pts < picture->pts)
621         priv->max_pts = picture->pts;
622
623     /* Update reference pictures */
624     /* XXX: consider priv->vol_hdr.low_delay, consider packed video frames for DivX/XviD */
625     if (GST_VAAPI_PICTURE_IS_REFERENCE(picture)) {
626         if (priv->next_picture)
627             status = render_picture(decoder, priv->next_picture);
628         gst_vaapi_picture_replace(&priv->prev_picture, priv->next_picture);
629         gst_vaapi_picture_replace(&priv->next_picture, picture);
630     }
631     return status;
632 }
633
634 static inline guint
635 get_vop_coding_type(GstVaapiPicture *picture)
636 {
637     return picture->type - GST_VAAPI_PICTURE_TYPE_I;
638 }
639
640 static gboolean
641 fill_picture(GstVaapiDecoderMpeg4 *decoder, GstVaapiPicture *picture)
642 {
643     GstVaapiDecoderMpeg4Private * const priv = decoder->priv;
644     VAPictureParameterBufferMPEG4 * const pic_param = picture->param;
645     GstMpeg4VideoObjectPlane * const vop_hdr = &priv->vop_hdr;
646
647     /* Fill in VAPictureParameterBufferMPEG4 */
648     pic_param->forward_reference_picture                        = VA_INVALID_ID;
649     pic_param->backward_reference_picture                       = VA_INVALID_ID;
650
651     pic_param->vol_fields.value                                 = 0;
652     pic_param->vop_fields.value                                 = 0;
653     if(priv->is_svh) {
654         // vol_hdr Parameters
655         pic_param->vol_fields.bits.short_video_header           = 1; 
656         // does the following vol_hdr parameters matter for short video header?
657         pic_param->vol_fields.bits.chroma_format                = 1; // I420, see table 6-15. 
658         pic_param->vol_fields.bits.interlaced                   = 0; 
659         pic_param->vol_fields.bits.obmc_disable                 = 1;
660         pic_param->vol_fields.bits.sprite_enable                = 0;
661         pic_param->vol_fields.bits.sprite_warping_accuracy      = 0;
662         pic_param->vol_fields.bits.quant_type                   = 0; //method 1; $7.4.4
663         pic_param->vol_fields.bits.quarter_sample               = 0; 
664         pic_param->vol_fields.bits.data_partitioned             = 0; 
665         pic_param->vol_fields.bits.reversible_vlc               = 0; 
666         pic_param->vol_fields.bits.resync_marker_disable        = 1; 
667         pic_param->no_of_sprite_warping_points                  = 0; 
668         pic_param->quant_precision                              = 5;
669         // VOP parameters    
670         pic_param->vop_width                                    = priv->svh_hdr.vop_width;
671         pic_param->vop_height                                   = priv->svh_hdr.vop_height;
672         pic_param->vop_fields.bits.vop_coding_type              = priv->svh_hdr.picture_coding_type;
673         pic_param->vop_time_increment_resolution                = priv->vol_hdr.vop_time_increment_resolution; 
674         
675         pic_param->num_gobs_in_vop                              = priv->svh_hdr.num_gobs_in_vop;
676         pic_param->num_macroblocks_in_gob                       = priv->svh_hdr.num_macroblocks_in_gob;
677     }
678     else {
679         // VOL parameters
680         pic_param->vol_fields.bits.short_video_header           = 0; 
681         pic_param->vol_fields.bits.chroma_format                = priv->vol_hdr.chroma_format;
682         pic_param->vol_fields.bits.interlaced                   = priv->vol_hdr.interlaced;
683         pic_param->vol_fields.bits.obmc_disable                 = priv->vol_hdr.obmc_disable;
684         pic_param->vol_fields.bits.sprite_enable                = priv->vol_hdr.sprite_enable;
685         pic_param->vol_fields.bits.sprite_warping_accuracy      = priv->vol_hdr.sprite_warping_accuracy; 
686         pic_param->vol_fields.bits.quant_type                   = priv->vol_hdr.quant_type;
687         pic_param->vol_fields.bits.quarter_sample               = priv->vol_hdr.quarter_sample;
688         pic_param->vol_fields.bits.data_partitioned             = priv->vol_hdr.data_partitioned;
689         pic_param->vol_fields.bits.reversible_vlc               = priv->vol_hdr.reversible_vlc;
690         pic_param->vol_fields.bits.resync_marker_disable        = priv->vol_hdr.resync_marker_disable;
691         pic_param->no_of_sprite_warping_points                  = priv->vol_hdr.no_of_sprite_warping_points;
692         int i =0;
693         for (i=0; i<3 && i<priv->vol_hdr.no_of_sprite_warping_points ; i++) {
694             pic_param->sprite_trajectory_du[i]                  = priv->sprite_trajectory.vop_ref_points[i];
695             pic_param->sprite_trajectory_dv[i]                  = priv->sprite_trajectory.sprite_ref_points[i];
696         }
697         pic_param->quant_precision                              = priv->vol_hdr.quant_precision;
698         
699         // VOP parameters    
700         pic_param->vop_width                                    = vop_hdr->width;
701         pic_param->vop_height                                   = vop_hdr->height;
702         pic_param->vop_fields.bits.vop_coding_type              = vop_hdr->coding_type;
703         pic_param->vop_fields.bits.vop_rounding_type            = vop_hdr->rounding_type;
704         pic_param->vop_fields.bits.intra_dc_vlc_thr             = vop_hdr->intra_dc_vlc_thr;
705         pic_param->vop_fields.bits.top_field_first              = vop_hdr->top_field_first;
706         pic_param->vop_fields.bits.alternate_vertical_scan_flag = vop_hdr->alternate_vertical_scan_flag;
707
708         pic_param->vop_fcode_forward                            = vop_hdr->fcode_forward;
709         pic_param->vop_fcode_backward                           = vop_hdr->fcode_backward;
710         pic_param->vop_time_increment_resolution                = priv->vol_hdr.vop_time_increment_resolution;
711     }    
712
713     pic_param->TRB = 0;
714     pic_param->TRD = 0;
715     switch (priv->coding_type) {
716     case GST_MPEG4_B_VOP:
717         pic_param->TRB                                          = priv->trb;
718         pic_param->backward_reference_picture                   = priv->next_picture->surface_id;
719         pic_param->vop_fields.bits.backward_reference_vop_coding_type = get_vop_coding_type(priv->next_picture);
720         // fall-through
721     case GST_MPEG4_P_VOP:
722         pic_param->TRD                                          = priv->trd;
723         if (priv->prev_picture)
724             pic_param->forward_reference_picture                = priv->prev_picture->surface_id;
725         break;
726     }
727
728     if (priv->vol_hdr.interlaced) {
729         priv->is_first_field ^= 1;
730     }
731     return TRUE;
732 }
733
734 static GstVaapiDecoderStatus
735 decode_slice(
736     GstVaapiDecoderMpeg4 *decoder,
737     const guint8          *buf,
738     guint                 buf_size,
739     gboolean              has_packet_header
740 )
741 {
742     GstVaapiDecoderMpeg4Private * const priv = decoder->priv;
743     GstVaapiPicture * const picture = priv->curr_picture;
744     GstVaapiSlice *slice;
745     VASliceParameterBufferMPEG4 *slice_param;
746
747     GST_DEBUG("decoder silce: %p, %u bytes)", buf, buf_size);
748
749     // has_packet_header is ture for the 2+ slice
750     if (!has_packet_header && !fill_picture(decoder, picture))
751         return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
752
753     slice = GST_VAAPI_SLICE_NEW(MPEG4, decoder, buf, buf_size);
754     if (!slice) {
755         GST_DEBUG("failed to allocate slice");
756         return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
757     }
758     gst_vaapi_picture_add_slice(picture, slice);
759
760     /* Fill in VASliceParameterBufferMPEG4 */
761     slice_param = slice->param;
762     if (priv->is_svh) {
763         slice_param->macroblock_offset         = (priv->svh_hdr.size)%8;
764         slice_param->macroblock_number         = 0; 
765         // the header of first gob_layer is empty (gob_header_empty=1), use vop_quant
766         slice_param->quant_scale               = priv->svh_hdr.vop_quant; 
767     }
768     else {
769         if (has_packet_header) {
770             slice_param->macroblock_offset     = priv->packet_hdr.size % 8;
771             slice_param->macroblock_number     = priv->packet_hdr.macroblock_number;
772             slice_param->quant_scale           = priv->packet_hdr.quant_scale;
773        }    
774         else {
775             slice_param->macroblock_offset     = priv->vop_hdr.size % 8;
776             slice_param->macroblock_number     = 0;
777             slice_param->quant_scale           = priv->vop_hdr.quant;
778         }
779     }
780     return GST_VAAPI_DECODER_STATUS_SUCCESS;
781 }
782
783 static GstVaapiDecoderStatus
784 decode_packet(GstVaapiDecoderMpeg4 *decoder, GstMpeg4Packet packet)
785 {
786     GstVaapiDecoderMpeg4Private * const priv = decoder->priv;
787     GstMpeg4Packet *tos = &packet;
788     GstVaapiDecoderStatus status;
789
790     if (tos->size < 0)
791         return GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA;
792
793     // packet.size is the size from current marker to the next.
794     if (tos->type == GST_MPEG4_VISUAL_OBJ_SEQ_START) {
795         status = decode_sequence(decoder, packet.data + packet.offset, packet.size);
796     }
797     else if (tos->type == GST_MPEG4_VISUAL_OBJ_SEQ_END) {
798         status = decode_sequence_end(decoder);
799     }
800     else if (tos->type == GST_MPEG4_VISUAL_OBJ) {
801         status = decode_visual_object(decoder, packet.data + packet.offset, packet.size);
802     }
803     else if (tos->type >= GST_MPEG4_VIDEO_OBJ_FIRST && tos->type <= GST_MPEG4_VIDEO_OBJ_LAST) {
804         GST_WARNING("unexpected marker: (GST_MPEG4_VIDEO_OBJ_FIRST, GST_MPEG4_VIDEO_OBJ_LAST)");
805         status = GST_VAAPI_DECODER_STATUS_SUCCESS;
806     }
807     else if (tos->type >= GST_MPEG4_VIDEO_LAYER_FIRST && tos->type <= GST_MPEG4_VIDEO_LAYER_LAST) {
808         status = decode_video_object_layer(decoder, packet.data + packet.offset, packet.size);
809     }
810     else if (tos->type == GST_MPEG4_GROUP_OF_VOP) {
811         status = decode_gop(decoder, packet.data + packet.offset, packet.size);
812     }
813     else if (tos->type == GST_MPEG4_VIDEO_OBJ_PLANE) {
814         status = decode_picture(decoder, packet.data + packet.offset, packet.size);
815         if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
816             return status;
817
818         /* decode slice
819          * A resync marker shall only be located immediately before a macroblock 
820          * (or video packet header if exists) and aligned with a byte
821          * either start_code or resync_marker are scaned/measured by byte, 
822          * while the header itself are parsed/measured in bit
823          * it means: resync_marker(video_packet_header) start from byte boundary, 
824          * while MB doesn't start from byte boundary -- it is what 'macroblock_offset' 
825          * in slice refer to
826          */
827         const guint8 *_data = packet.data + packet.offset + priv->vop_hdr.size/8; 
828         gint  _data_size = packet.size - (priv->vop_hdr.size/8); 
829         GstMpeg4Packet video_packet;
830         
831         if (priv->vol_hdr.resync_marker_disable) {
832             status = decode_slice(decoder, _data, _data_size, FALSE);
833             if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
834                 return status;
835         }
836         else {
837             // next start_code is required to determine the end of last slice
838             _data_size += 4;
839             GstMpeg4ParseResult ret = GST_MPEG4_PARSER_OK;
840
841             gboolean first_slice = TRUE;
842             while (_data_size > 0) {
843                 // we can skip user data here
844                 ret = gst_mpeg4_parse(&video_packet, TRUE, &priv->vop_hdr, _data, 0,  _data_size);
845                 if(ret != GST_MPEG4_PARSER_OK) {
846                     break;
847                 }
848
849                 if (first_slice) {
850                     status = decode_slice(decoder, _data, video_packet.size, FALSE);
851                     first_slice = FALSE;
852                 }
853                 else {
854                     _data += video_packet.offset;
855                     _data_size -= video_packet.offset;
856
857                     ret = gst_mpeg4_parse_video_packet_header (&priv->packet_hdr, &priv->vol_hdr, &priv->vop_hdr, &priv->sprite_trajectory, _data, _data_size);
858                     status = decode_slice(decoder,_data + priv->packet_hdr.size/8, video_packet.size - priv->packet_hdr.size/8, TRUE); 
859                 }
860
861                 _data += video_packet.size;
862                 _data_size -= video_packet.size;
863             }
864         }
865         status = decode_current_picture(decoder);
866     }
867     else if (tos->type == GST_MPEG4_USER_DATA
868           || tos->type == GST_MPEG4_VIDEO_SESSION_ERR 
869           || tos->type == GST_MPEG4_FBA 
870           || tos->type == GST_MPEG4_FBA_PLAN 
871           || tos->type == GST_MPEG4_MESH 
872           || tos->type == GST_MPEG4_MESH_PLAN 
873           || tos->type == GST_MPEG4_STILL_TEXTURE_OBJ 
874           || tos->type == GST_MPEG4_TEXTURE_SPATIAL 
875           || tos->type == GST_MPEG4_TEXTURE_SNR_LAYER 
876           || tos->type == GST_MPEG4_TEXTURE_TILE 
877           || tos->type == GST_MPEG4_SHAPE_LAYER 
878           || tos->type == GST_MPEG4_STUFFING 
879           || tos->type == GST_MPEG4_SYSTEM_FIRST 
880           || tos->type == GST_MPEG4_SYSTEM_LAST) {
881         GST_WARNING("Ignore marker: %x\n", tos->type);
882         status = GST_VAAPI_DECODER_STATUS_SUCCESS;
883     }
884     else {
885         GST_ERROR("unsupported start code %x\n", tos->type);
886         status = GST_VAAPI_DECODER_STATUS_SUCCESS;
887     }
888     
889     return status;
890 }
891
892 static GstVaapiDecoderStatus
893 decode_buffer(GstVaapiDecoderMpeg4 *decoder, const guchar *buf, guint buf_size)
894 {
895     GstVaapiDecoderMpeg4Private * const priv = decoder->priv;
896     GstVaapiDecoderStatus status;
897     GstMpeg4Packet packet;
898     guint ofs;
899
900     if (priv->is_svh) {
901         status = decode_picture(decoder, buf, buf_size);
902         if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
903             return status;
904
905         ofs = priv->svh_hdr.size / 8;
906         status = decode_slice(decoder, buf + ofs, buf_size - ofs, FALSE);
907         if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
908             return status;
909     }
910     else {
911         packet.data   = buf;
912         packet.offset = 0;
913         packet.size   = buf_size;
914         packet.type   = (GstMpeg4StartCode)packet.data[0];
915
916         status = decode_packet(decoder, packet);
917         if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
918             return status;
919     }
920     return GST_VAAPI_DECODER_STATUS_SUCCESS;
921 }
922
923 static GstVaapiDecoderStatus
924 decode_codec_data(GstVaapiDecoderMpeg4 *decoder, GstBuffer *buffer)
925 {
926     GstVaapiDecoderStatus status;
927     guchar *buf, *_buf;
928     guint pos, buf_size, _buf_size;
929
930     _buf      = GST_BUFFER_DATA(buffer);
931     _buf_size = GST_BUFFER_SIZE(buffer);
932     // add additional 0x000001b2 to enclose the last header
933     buf_size = _buf_size + 4;
934     buf = malloc(buf_size);
935     memcpy(buf, _buf, buf_size);
936     buf[buf_size-4] = 0;
937     buf[buf_size-3] = 0;
938     buf[buf_size-2] = 1;
939     buf[buf_size-1] = 0xb2;
940
941     pos = 0;
942     GstMpeg4Packet packet;
943     GstMpeg4ParseResult result = GST_MPEG4_PARSER_OK;
944
945     while (result == GST_MPEG4_PARSER_OK && pos < buf_size) {
946         result = gst_mpeg4_parse(&packet, FALSE, NULL, buf, pos, buf_size);
947         if (result != GST_MPEG4_PARSER_OK) {
948             break;
949         }
950         status = decode_packet(decoder, packet);
951         if (GST_VAAPI_DECODER_STATUS_SUCCESS == status) {
952             pos = packet.offset + packet.size; 
953         }
954         else {
955             GST_WARNING("decode mp4 packet failed when decoding codec data\n");
956             break;
957         }
958     }
959     free(buf);
960     return status;
961 }
962
963 static GstVaapiDecoderStatus
964 ensure_decoder(GstVaapiDecoderMpeg4 *decoder)
965 {
966     GstVaapiDecoderMpeg4Private * const priv = decoder->priv;
967     GstVaapiDecoderStatus status;
968     GstBuffer *codec_data;
969
970     g_return_val_if_fail(priv->is_constructed,
971                          GST_VAAPI_DECODER_STATUS_ERROR_INIT_FAILED);
972
973     if (!priv->is_opened) {
974         priv->is_opened = gst_vaapi_decoder_mpeg4_open(decoder);
975         if (!priv->is_opened)
976             return GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_CODEC;
977
978         codec_data = GST_VAAPI_DECODER_CODEC_DATA(decoder);
979         if (codec_data) {
980             status = decode_codec_data(decoder, codec_data);
981             if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
982                 return status;
983         }
984     }
985     return GST_VAAPI_DECODER_STATUS_SUCCESS;
986 }
987
988 static GstVaapiDecoderStatus
989 gst_vaapi_decoder_mpeg4_parse(GstVaapiDecoder *base_decoder,
990     GstAdapter *adapter, gboolean at_eos, GstVaapiDecoderUnit *unit)
991 {
992     GstVaapiDecoderMpeg4 * const decoder =
993         GST_VAAPI_DECODER_MPEG4(base_decoder);
994     GstVaapiDecoderMpeg4Private * const priv = decoder->priv;
995     GstVaapiDecoderStatus status;
996     GstMpeg4Packet packet;
997     GstMpeg4ParseResult result;
998     const guchar *buf;
999     guint size, buf_size, flags = 0;
1000
1001     status = ensure_decoder(decoder);
1002     if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
1003         return status;
1004
1005     size = gst_adapter_available(adapter);
1006     buf = gst_adapter_peek(adapter, size);
1007     if (!buf)
1008         return GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA;
1009
1010     if (priv->is_svh)
1011         result = gst_h263_parse(&packet, buf, 0, size);
1012     else
1013         result = gst_mpeg4_parse(&packet, FALSE, NULL, buf, 0, size);
1014     if (result == GST_MPEG4_PARSER_NO_PACKET_END && at_eos)
1015         packet.size = size - packet.offset;
1016     else if (result == GST_MPEG4_PARSER_ERROR)
1017         return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
1018     else if (result != GST_MPEG4_PARSER_OK)
1019         return GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA;
1020
1021     buf_size = packet.size;
1022     gst_adapter_flush(adapter, packet.offset);
1023     unit->size = buf_size;
1024
1025     /* Check for start of new picture */
1026     switch (packet.type) {
1027     case GST_MPEG4_VIDEO_SESSION_ERR:
1028     case GST_MPEG4_FBA:
1029     case GST_MPEG4_FBA_PLAN:
1030     case GST_MPEG4_MESH:
1031     case GST_MPEG4_MESH_PLAN:
1032     case GST_MPEG4_STILL_TEXTURE_OBJ:
1033     case GST_MPEG4_TEXTURE_SPATIAL:
1034     case GST_MPEG4_TEXTURE_SNR_LAYER:
1035     case GST_MPEG4_TEXTURE_TILE:
1036     case GST_MPEG4_SHAPE_LAYER:
1037     case GST_MPEG4_STUFFING:
1038         gst_adapter_flush(adapter, packet.size);
1039         return GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA;
1040     case GST_MPEG4_USER_DATA:
1041         flags |= GST_VAAPI_DECODER_UNIT_FLAG_SKIP;
1042         break;
1043     case GST_MPEG4_VISUAL_OBJ_SEQ_END:
1044         flags |= GST_VAAPI_DECODER_UNIT_FLAG_FRAME_END;
1045         flags |= GST_VAAPI_DECODER_UNIT_FLAG_STREAM_END;
1046         break;
1047     case GST_MPEG4_VIDEO_OBJ_PLANE:
1048         flags |= GST_VAAPI_DECODER_UNIT_FLAG_SLICE;
1049         flags |= GST_VAAPI_DECODER_UNIT_FLAG_FRAME_END;
1050         /* fall-through */
1051     case GST_MPEG4_VISUAL_OBJ_SEQ_START:
1052     case GST_MPEG4_VISUAL_OBJ:
1053     case GST_MPEG4_GROUP_OF_VOP:
1054         flags |= GST_VAAPI_DECODER_UNIT_FLAG_FRAME_START;
1055         break;
1056     default:
1057         if (packet.type >= GST_MPEG4_VIDEO_OBJ_FIRST &&
1058             packet.type <= GST_MPEG4_VIDEO_OBJ_LAST) {
1059             gst_adapter_flush(adapter, packet.size);
1060             return GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA;
1061         }
1062         if (packet.type >= GST_MPEG4_VIDEO_LAYER_FIRST &&
1063             packet.type <= GST_MPEG4_VIDEO_LAYER_LAST) {
1064             break;
1065         }
1066         if (packet.type >= GST_MPEG4_SYSTEM_FIRST &&
1067             packet.type <= GST_MPEG4_SYSTEM_LAST) {
1068             flags |= GST_VAAPI_DECODER_UNIT_FLAG_SKIP;
1069             break;
1070         }
1071         GST_WARNING("unsupported start code (0x%02x)", packet.type);
1072         return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
1073     }
1074     GST_VAAPI_DECODER_UNIT_FLAG_SET(unit, flags);
1075     return GST_VAAPI_DECODER_STATUS_SUCCESS;
1076 }
1077
1078 static GstVaapiDecoderStatus
1079 gst_vaapi_decoder_mpeg4_decode(GstVaapiDecoder *base_decoder,
1080     GstVaapiDecoderUnit *unit)
1081 {
1082     GstVaapiDecoderMpeg4 * const decoder =
1083         GST_VAAPI_DECODER_MPEG4(base_decoder);
1084     GstVaapiDecoderStatus status;
1085     GstBuffer * const buffer =
1086         GST_VAAPI_DECODER_CODEC_FRAME(decoder)->input_buffer;
1087     const guchar *buf;
1088     guint buf_size;
1089
1090     status = ensure_decoder(decoder);
1091     if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
1092         return status;
1093
1094     buf      = GST_BUFFER_DATA(buffer) + unit->offset;
1095     buf_size = unit->size;
1096
1097     status = decode_buffer(decoder, buf, buf_size);
1098     if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
1099         return status;
1100     return GST_VAAPI_DECODER_STATUS_SUCCESS;
1101 }
1102
1103 static void
1104 gst_vaapi_decoder_mpeg4_finalize(GObject *object)
1105 {
1106     GstVaapiDecoderMpeg4 * const decoder = GST_VAAPI_DECODER_MPEG4(object);
1107
1108     gst_vaapi_decoder_mpeg4_destroy(decoder);
1109
1110     G_OBJECT_CLASS(gst_vaapi_decoder_mpeg4_parent_class)->finalize(object);
1111 }
1112
1113 static void
1114 gst_vaapi_decoder_mpeg4_constructed(GObject *object)
1115 {
1116     GstVaapiDecoderMpeg4 * const decoder = GST_VAAPI_DECODER_MPEG4(object);
1117     GstVaapiDecoderMpeg4Private * const priv = decoder->priv;
1118     GObjectClass *parent_class;
1119
1120     parent_class = G_OBJECT_CLASS(gst_vaapi_decoder_mpeg4_parent_class);
1121     if (parent_class->constructed)
1122         parent_class->constructed(object);
1123
1124     priv->is_constructed = gst_vaapi_decoder_mpeg4_create(decoder);
1125 }
1126
1127 static void
1128 gst_vaapi_decoder_mpeg4_class_init(GstVaapiDecoderMpeg4Class *klass)
1129 {
1130     GObjectClass * const object_class = G_OBJECT_CLASS(klass);
1131     GstVaapiDecoderClass * const decoder_class = GST_VAAPI_DECODER_CLASS(klass);
1132
1133     g_type_class_add_private(klass, sizeof(GstVaapiDecoderMpeg4Private));
1134
1135     object_class->finalize      = gst_vaapi_decoder_mpeg4_finalize;
1136     object_class->constructed   = gst_vaapi_decoder_mpeg4_constructed;
1137
1138     decoder_class->parse        = gst_vaapi_decoder_mpeg4_parse;
1139     decoder_class->decode       = gst_vaapi_decoder_mpeg4_decode;
1140 }
1141
1142 static void
1143 gst_vaapi_decoder_mpeg4_init(GstVaapiDecoderMpeg4 *decoder)
1144 {
1145     GstVaapiDecoderMpeg4Private *priv;
1146
1147     priv                        = GST_VAAPI_DECODER_MPEG4_GET_PRIVATE(decoder);
1148     decoder->priv               = priv;
1149     priv->width                 = 0;
1150     priv->height                = 0;
1151     priv->fps_n                 = 0;
1152     priv->fps_d                 = 0;
1153     priv->profile               = GST_VAAPI_PROFILE_MPEG4_SIMPLE;
1154     priv->curr_picture          = NULL;
1155     priv->next_picture          = NULL;
1156     priv->prev_picture          = NULL;
1157     priv->seq_pts               = GST_CLOCK_TIME_NONE;
1158     priv->gop_pts               = GST_CLOCK_TIME_NONE;
1159     priv->max_pts               = GST_CLOCK_TIME_NONE;
1160     priv->pts_diff              = 0;
1161     priv->calculate_pts_diff    = TRUE;
1162     priv->is_constructed        = FALSE;
1163     priv->is_opened             = FALSE;
1164     priv->is_first_field        = FALSE;
1165     priv->size_changed          = TRUE;
1166     priv->profile_changed       = TRUE;
1167     priv->progressive_sequence  = FALSE;
1168     priv->closed_gop            = FALSE;
1169     priv->broken_link           = FALSE;
1170     priv->last_non_b_scale_time = 0;
1171     priv->non_b_scale_time      = 0;
1172     priv->trb                   = 0;
1173     priv->trd                   = 0;
1174 }
1175
1176 /**
1177  * gst_vaapi_decoder_mpeg4_new:
1178  * @display: a #GstVaapiDisplay
1179  * @caps: a #GstCaps holding codec information
1180  *
1181  * Creates a new #GstVaapiDecoder for MPEG-2 decoding.  The @caps can
1182  * hold extra information like codec-data and pictured coded size.
1183  *
1184  * Return value: the newly allocated #GstVaapiDecoder object
1185  */
1186 GstVaapiDecoder *
1187 gst_vaapi_decoder_mpeg4_new(GstVaapiDisplay *display, GstCaps *caps)
1188 {
1189     GstVaapiDecoderMpeg4 *decoder;
1190
1191     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
1192     g_return_val_if_fail(GST_IS_CAPS(caps), NULL);
1193
1194     decoder = g_object_new(
1195         GST_VAAPI_TYPE_DECODER_MPEG4,
1196         "display",      display,
1197         "caps",         caps,
1198         NULL
1199     );
1200     if (!decoder->priv->is_constructed) {
1201         g_object_unref(decoder);
1202         return NULL;
1203     }
1204     return GST_VAAPI_DECODER_CAST(decoder);
1205 }