Merging gst-plugins-bad
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / gst-libs / gst / codecs / gstmpeg2decoder.c
1 /* GStreamer
2  * Copyright (C) 2020 Intel Corporation
3  *     Author: He Junyan <junyan.he@intel.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 /**
21  * SECTION:gstmpeg2decoder
22  * @title: GstMpeg2Decoder
23  * @short_description: Base class to implement stateless MPEG2 decoders
24  * @sources:
25  * - gstmpeg2picture.h
26  */
27
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31
32 #include "gstmpeg2decoder.h"
33
34 GST_DEBUG_CATEGORY (gst_mpeg2_decoder_debug);
35 #define GST_CAT_DEFAULT gst_mpeg2_decoder_debug
36
37 /* ------------------------------------------------------------------------- */
38 /* --- PTS Generator                                                     --- */
39 /* ------------------------------------------------------------------------- */
40 typedef struct _PTSGenerator PTSGenerator;
41 struct _PTSGenerator
42 {
43   /* The current GOP PTS */
44   GstClockTime gop_pts;
45   /* Max picture PTS */
46   GstClockTime max_pts;
47   /* Absolute GOP TSN */
48   guint gop_tsn;
49   /* Max picture TSN, relative to last GOP TSN */
50   guint max_tsn;
51   /* How many times TSN overflowed since GOP */
52   guint ovl_tsn;
53   /* Last picture TSN */
54   guint lst_tsn;
55   guint fps_n;
56   guint fps_d;
57 };
58
59 static void
60 _pts_init (PTSGenerator * tsg)
61 {
62   tsg->gop_pts = GST_CLOCK_TIME_NONE;
63   tsg->max_pts = GST_CLOCK_TIME_NONE;
64   tsg->gop_tsn = 0;
65   tsg->max_tsn = 0;
66   tsg->ovl_tsn = 0;
67   tsg->lst_tsn = 0;
68   tsg->fps_n = 0;
69   tsg->fps_d = 0;
70 }
71
72 static inline GstClockTime
73 _pts_get_duration (PTSGenerator * tsg, guint num_frames)
74 {
75   return gst_util_uint64_scale (num_frames, GST_SECOND * tsg->fps_d,
76       tsg->fps_n);
77 }
78
79 static inline guint
80 _pts_get_poc (PTSGenerator * tsg)
81 {
82   return tsg->gop_tsn + tsg->ovl_tsn * 1024 + tsg->lst_tsn;
83 }
84
85 static void
86 _pts_set_framerate (PTSGenerator * tsg, guint fps_n, guint fps_d)
87 {
88   tsg->fps_n = fps_n;
89   tsg->fps_d = fps_d;
90 }
91
92 static void
93 _pts_sync (PTSGenerator * tsg, GstClockTime gop_pts)
94 {
95   guint gop_tsn;
96
97   if (!GST_CLOCK_TIME_IS_VALID (gop_pts) ||
98       (GST_CLOCK_TIME_IS_VALID (tsg->max_pts) && tsg->max_pts >= gop_pts)) {
99     /* Invalid GOP PTS, interpolate from the last known picture PTS */
100     if (GST_CLOCK_TIME_IS_VALID (tsg->max_pts)) {
101       gop_pts = tsg->max_pts + _pts_get_duration (tsg, 1);
102       gop_tsn = tsg->gop_tsn + tsg->ovl_tsn * 1024 + tsg->max_tsn + 1;
103     } else {
104       gop_pts = 0;
105       gop_tsn = 0;
106     }
107   } else {
108     /* Interpolate GOP TSN from this valid PTS */
109     if (GST_CLOCK_TIME_IS_VALID (tsg->gop_pts))
110       gop_tsn = tsg->gop_tsn + gst_util_uint64_scale (gop_pts - tsg->gop_pts +
111           _pts_get_duration (tsg, 1) - 1, tsg->fps_n, GST_SECOND * tsg->fps_d);
112     else
113       gop_tsn = 0;
114   }
115
116   tsg->gop_pts = gop_pts;
117   tsg->gop_tsn = gop_tsn;
118   tsg->max_tsn = 0;
119   tsg->ovl_tsn = 0;
120   tsg->lst_tsn = 0;
121 }
122
123 static GstClockTime
124 _pts_eval (PTSGenerator * tsg, GstClockTime pic_pts, guint pic_tsn)
125 {
126   GstClockTime pts;
127
128   if (!GST_CLOCK_TIME_IS_VALID (tsg->gop_pts))
129     tsg->gop_pts = _pts_get_duration (tsg, pic_tsn);
130
131   pts = pic_pts;
132   if (!GST_CLOCK_TIME_IS_VALID (pts))
133     pts = tsg->gop_pts + _pts_get_duration (tsg, tsg->ovl_tsn * 1024 + pic_tsn);
134   else if (pts == tsg->gop_pts) {
135     /* The picture following the GOP header shall be an I-frame.
136        So we can compensate for the GOP start time from here */
137     tsg->gop_pts -= _pts_get_duration (tsg, pic_tsn);
138   }
139
140   if (!GST_CLOCK_TIME_IS_VALID (tsg->max_pts) || tsg->max_pts < pts)
141     tsg->max_pts = pts;
142
143   if (tsg->max_tsn < pic_tsn)
144     tsg->max_tsn = pic_tsn;
145   else if (tsg->max_tsn == 1023 && pic_tsn < tsg->lst_tsn) {    /* TSN wrapped */
146     tsg->max_tsn = pic_tsn;
147     tsg->ovl_tsn++;
148   }
149   tsg->lst_tsn = pic_tsn;
150
151   return pts;
152 }
153
154 static inline gboolean
155 _seq_hdr_is_valid (GstMpegVideoSequenceHdr * hdr)
156 {
157   return hdr->width > 0 && hdr->height > 0;
158 }
159
160 #define SEQ_HDR_INIT (GstMpegVideoSequenceHdr) { 0, }
161
162 static inline gboolean
163 _seq_ext_is_valid (GstMpegVideoSequenceExt * ext)
164 {
165   return ext->profile >= GST_MPEG_VIDEO_PROFILE_422
166       && ext->profile <= GST_MPEG_VIDEO_PROFILE_SIMPLE;
167 }
168
169 #define SEQ_EXT_INIT (GstMpegVideoSequenceExt) { 0xff, 0, }
170
171 static inline gboolean
172 _seq_display_ext_is_valid (GstMpegVideoSequenceDisplayExt * ext)
173 {
174   return ext->video_format != 0xff;
175 }
176
177 #define SEQ_DISPLAY_EXT_INIT (GstMpegVideoSequenceDisplayExt) { 0xff, 0, }
178
179 static inline gboolean
180 _seq_scalable_ext_is_valid (GstMpegVideoSequenceScalableExt * ext)
181 {
182   return ext->scalable_mode != 0xff;
183 }
184
185 #define SEQ_SCALABLE_EXT_INIT (GstMpegVideoSequenceScalableExt) { 0xff, 0, }
186
187 static inline gboolean
188 _quant_matrix_ext_is_valid (GstMpegVideoQuantMatrixExt * ext)
189 {
190   return ext->load_intra_quantiser_matrix != 0xff;
191 }
192
193 #define QUANT_MATRIX_EXT_INIT (GstMpegVideoQuantMatrixExt) { 0xff, { 0, } }
194
195 static inline gboolean
196 _pic_hdr_is_valid (GstMpegVideoPictureHdr * hdr)
197 {
198   return hdr->tsn != 0xffff;
199 }
200
201 #define PIC_HDR_INIT (GstMpegVideoPictureHdr) { 0xffff, 0, }
202
203 static inline gboolean
204 _pic_hdr_ext_is_valid (GstMpegVideoPictureExt * ext)
205 {
206   return ext->f_code[0][0] != 0xff;
207 }
208
209 #define PIC_HDR_EXT_INIT                                        \
210     (GstMpegVideoPictureExt) { { { 0xff, 0, }, { 0, } }, 0, }
211
212 typedef enum
213 {
214   GST_MPEG2_DECODER_STATE_GOT_SEQ_HDR = 1 << 0,
215   GST_MPEG2_DECODER_STATE_GOT_SEQ_EXT = 1 << 1,
216   GST_MPEG2_DECODER_STATE_GOT_PIC_HDR = 1 << 2,
217   GST_MPEG2_DECODER_STATE_GOT_PIC_EXT = 1 << 3,
218   GST_MPEG2_DECODER_STATE_GOT_SLICE = 1 << 4,
219
220   GST_MPEG2_DECODER_STATE_VALID_SEQ_HEADERS =
221       (GST_MPEG2_DECODER_STATE_GOT_SEQ_HDR |
222       GST_MPEG2_DECODER_STATE_GOT_SEQ_EXT),
223   GST_MPEG2_DECODER_STATE_VALID_PIC_HEADERS =
224       (GST_MPEG2_DECODER_STATE_GOT_PIC_HDR |
225       GST_MPEG2_DECODER_STATE_GOT_PIC_EXT),
226   GST_MPEG2_DECODER_STATE_VALID_PICTURE =
227       (GST_MPEG2_DECODER_STATE_VALID_SEQ_HEADERS |
228       GST_MPEG2_DECODER_STATE_VALID_PIC_HEADERS |
229       GST_MPEG2_DECODER_STATE_GOT_SLICE)
230 } GstMpeg2DecoderState;
231
232 struct _GstMpeg2DecoderPrivate
233 {
234   gint width;
235   gint height;
236   gint display_width;
237   gint display_height;
238   GstMpegVideoProfile profile;
239   gboolean progressive;
240
241   GstMpegVideoSequenceHdr seq_hdr;
242   GstMpegVideoSequenceExt seq_ext;
243   GstMpegVideoSequenceDisplayExt seq_display_ext;
244   GstMpegVideoSequenceScalableExt seq_scalable_ext;
245
246   /* some sequence info changed after last new_sequence () */
247   gboolean seq_changed;
248   GstMpegVideoGop gop;
249   GstMpegVideoQuantMatrixExt quant_matrix;
250   GstMpegVideoPictureHdr pic_hdr;
251   GstMpegVideoPictureExt pic_ext;
252
253   GstMpeg2Dpb *dpb;
254   GstMpeg2DecoderState state;
255   PTSGenerator tsg;
256   GstClockTime current_pts;
257
258   GstMpeg2Picture *current_picture;
259   GstVideoCodecFrame *current_frame;
260   GstMpeg2Picture *first_field;
261 };
262
263 #define parent_class gst_mpeg2_decoder_parent_class
264 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GstMpeg2Decoder, gst_mpeg2_decoder,
265     GST_TYPE_VIDEO_DECODER,
266     G_ADD_PRIVATE (GstMpeg2Decoder);
267     GST_DEBUG_CATEGORY_INIT (gst_mpeg2_decoder_debug, "mpeg2decoder", 0,
268         "MPEG2 Video Decoder"));
269
270 static gboolean gst_mpeg2_decoder_start (GstVideoDecoder * decoder);
271 static gboolean gst_mpeg2_decoder_stop (GstVideoDecoder * decoder);
272 static gboolean gst_mpeg2_decoder_set_format (GstVideoDecoder * decoder,
273     GstVideoCodecState * state);
274 static GstFlowReturn gst_mpeg2_decoder_finish (GstVideoDecoder * decoder);
275 static gboolean gst_mpeg2_decoder_flush (GstVideoDecoder * decoder);
276 static GstFlowReturn gst_mpeg2_decoder_drain (GstVideoDecoder * decoder);
277 static GstFlowReturn gst_mpeg2_decoder_handle_frame (GstVideoDecoder * decoder,
278     GstVideoCodecFrame * frame);
279 static gboolean gst_mpeg2_decoder_do_output_picture (GstMpeg2Decoder * self,
280     GstMpeg2Picture * picture);
281
282 static void
283 gst_mpeg2_decoder_class_init (GstMpeg2DecoderClass * klass)
284 {
285   GstVideoDecoderClass *decoder_class = GST_VIDEO_DECODER_CLASS (klass);
286
287   decoder_class->start = GST_DEBUG_FUNCPTR (gst_mpeg2_decoder_start);
288   decoder_class->stop = GST_DEBUG_FUNCPTR (gst_mpeg2_decoder_stop);
289   decoder_class->set_format = GST_DEBUG_FUNCPTR (gst_mpeg2_decoder_set_format);
290   decoder_class->finish = GST_DEBUG_FUNCPTR (gst_mpeg2_decoder_finish);
291   decoder_class->flush = GST_DEBUG_FUNCPTR (gst_mpeg2_decoder_flush);
292   decoder_class->drain = GST_DEBUG_FUNCPTR (gst_mpeg2_decoder_drain);
293   decoder_class->handle_frame =
294       GST_DEBUG_FUNCPTR (gst_mpeg2_decoder_handle_frame);
295 }
296
297 static void
298 gst_mpeg2_decoder_init (GstMpeg2Decoder * self)
299 {
300   gst_video_decoder_set_packetized (GST_VIDEO_DECODER (self), TRUE);
301
302   self->priv = gst_mpeg2_decoder_get_instance_private (self);
303
304   self->priv->seq_hdr = SEQ_HDR_INIT;
305   self->priv->seq_ext = SEQ_EXT_INIT;
306   self->priv->seq_display_ext = SEQ_DISPLAY_EXT_INIT;
307   self->priv->seq_scalable_ext = SEQ_SCALABLE_EXT_INIT;
308   self->priv->quant_matrix = QUANT_MATRIX_EXT_INIT;
309   self->priv->pic_hdr = PIC_HDR_INIT;
310   self->priv->pic_ext = PIC_HDR_EXT_INIT;
311 }
312
313 static gboolean
314 gst_mpeg2_decoder_start (GstVideoDecoder * decoder)
315 {
316   GstMpeg2Decoder *self = GST_MPEG2_DECODER (decoder);
317   GstMpeg2DecoderPrivate *priv = self->priv;
318
319   _pts_init (&priv->tsg);
320   priv->dpb = gst_mpeg2_dpb_new ();
321   priv->profile = -1;
322   priv->progressive = TRUE;
323
324   return TRUE;
325 }
326
327 static gboolean
328 gst_mpeg2_decoder_stop (GstVideoDecoder * decoder)
329 {
330   GstMpeg2Decoder *self = GST_MPEG2_DECODER (decoder);
331   GstMpeg2DecoderPrivate *priv = self->priv;
332
333   g_clear_pointer (&self->input_state, gst_video_codec_state_unref);
334   g_clear_pointer (&priv->dpb, gst_mpeg2_dpb_free);
335
336   return TRUE;
337 }
338
339 static gboolean
340 gst_mpeg2_decoder_set_format (GstVideoDecoder * decoder,
341     GstVideoCodecState * state)
342 {
343   GstMpeg2Decoder *self = GST_MPEG2_DECODER (decoder);
344   GstMpeg2DecoderPrivate *priv = self->priv;
345
346   GST_DEBUG_OBJECT (decoder, "Set format");
347
348   if (self->input_state)
349     gst_video_codec_state_unref (self->input_state);
350
351   self->input_state = gst_video_codec_state_ref (state);
352
353   priv->width = GST_VIDEO_INFO_WIDTH (&state->info);
354   priv->height = GST_VIDEO_INFO_HEIGHT (&state->info);
355
356   return TRUE;
357 }
358
359 static GstFlowReturn
360 gst_mpeg2_decoder_drain (GstVideoDecoder * decoder)
361 {
362   GstMpeg2Decoder *self = GST_MPEG2_DECODER (decoder);
363   GstMpeg2DecoderPrivate *priv = self->priv;
364   GstMpeg2Picture *picture;
365
366   while ((picture = gst_mpeg2_dpb_bump (priv->dpb)) != NULL) {
367     gst_mpeg2_decoder_do_output_picture (self, picture);
368   }
369
370   gst_mpeg2_dpb_clear (priv->dpb);
371   return GST_FLOW_OK;
372 }
373
374 static GstFlowReturn
375 gst_mpeg2_decoder_finish (GstVideoDecoder * decoder)
376 {
377   return gst_mpeg2_decoder_drain (decoder);
378 }
379
380 static gboolean
381 gst_mpeg2_decoder_flush (GstVideoDecoder * decoder)
382 {
383   GstMpeg2Decoder *self = GST_MPEG2_DECODER (decoder);
384   GstMpeg2DecoderPrivate *priv = self->priv;
385
386   gst_mpeg2_dpb_clear (priv->dpb);
387   priv->state &= GST_MPEG2_DECODER_STATE_VALID_SEQ_HEADERS;
388   priv->pic_hdr = PIC_HDR_INIT;
389   priv->pic_ext = PIC_HDR_EXT_INIT;
390
391   return TRUE;
392 }
393
394 static inline gboolean
395 _is_valid_state (GstMpeg2Decoder * decoder, GstMpeg2DecoderState state)
396 {
397   GstMpeg2DecoderPrivate *priv = decoder->priv;
398
399   return (priv->state & state) == state;
400 }
401
402 static void
403 gst_mpeg2_decoder_set_latency (GstMpeg2Decoder * decoder)
404 {
405   GstCaps *caps;
406   GstClockTime min, max;
407   GstMpeg2DecoderPrivate *priv = decoder->priv;
408   GstStructure *structure;
409   gint fps_d = 1, fps_n = 0;
410
411   if (priv->tsg.fps_d > 0 && priv->tsg.fps_n > 0) {
412     fps_n = priv->tsg.fps_n;
413     fps_d = priv->tsg.fps_d;
414   } else {
415     caps = gst_pad_get_current_caps (GST_VIDEO_DECODER_SINK_PAD (decoder));
416     if (caps) {
417       structure = gst_caps_get_structure (caps, 0);
418       if (gst_structure_get_fraction (structure, "framerate", &fps_n, &fps_d)) {
419         if (fps_n == 0) {
420           /* variable framerate: see if we have a max-framerate */
421           gst_structure_get_fraction (structure, "max-framerate", &fps_n,
422               &fps_d);
423         }
424       }
425       gst_caps_unref (caps);
426     }
427   }
428
429   /* if no fps or variable, then 25/1 */
430   if (fps_n == 0) {
431     fps_n = 25;
432     fps_d = 1;
433   }
434
435   max = gst_util_uint64_scale (2 * GST_SECOND, fps_d, fps_n);
436   min = gst_util_uint64_scale (1 * GST_SECOND, fps_d, fps_n);
437
438   GST_LOG_OBJECT (decoder,
439       "latency min %" G_GUINT64_FORMAT " max %" G_GUINT64_FORMAT, min, max);
440
441   gst_video_decoder_set_latency (GST_VIDEO_DECODER (decoder), min, max);
442 }
443
444 static gboolean
445 gst_mpeg2_decoder_handle_sequence (GstMpeg2Decoder * decoder,
446     GstMpegVideoPacket * packet)
447 {
448   GstMpeg2DecoderPrivate *priv = decoder->priv;
449   GstMpegVideoSequenceHdr seq_hdr = { 0, };
450
451   if (!gst_mpeg_video_packet_parse_sequence_header (packet, &seq_hdr)) {
452     GST_ERROR_OBJECT (decoder, "failed to parse sequence header");
453     return FALSE;
454   }
455
456   /* 6.1.1.6 Sequence header
457      The quantisation matrices may be redefined each time that a sequence
458      header occurs in the bitstream */
459   priv->quant_matrix = QUANT_MATRIX_EXT_INIT;
460
461   if (_seq_hdr_is_valid (&priv->seq_hdr) &&
462       memcmp (&priv->seq_hdr, &seq_hdr, sizeof (seq_hdr)) == 0)
463     return TRUE;
464
465   priv->seq_ext = SEQ_EXT_INIT;
466   priv->seq_display_ext = SEQ_DISPLAY_EXT_INIT;
467   priv->seq_scalable_ext = SEQ_SCALABLE_EXT_INIT;
468   priv->pic_ext = PIC_HDR_EXT_INIT;
469
470   priv->seq_hdr = seq_hdr;
471   priv->seq_changed = TRUE;
472
473   priv->width = seq_hdr.width;
474   priv->height = seq_hdr.height;
475   priv->display_width = priv->width;
476   priv->display_height = priv->height;
477
478   _pts_set_framerate (&priv->tsg, seq_hdr.fps_n, seq_hdr.fps_d);
479
480   gst_mpeg2_decoder_set_latency (decoder);
481
482   priv->state = GST_MPEG2_DECODER_STATE_GOT_SEQ_HDR;
483
484   return TRUE;
485 }
486
487 static gboolean
488 gst_mpeg2_decoder_handle_sequence_ext (GstMpeg2Decoder * decoder,
489     GstMpegVideoPacket * packet)
490 {
491   GstMpeg2DecoderPrivate *priv = decoder->priv;
492   GstMpegVideoSequenceExt seq_ext = { 0, };
493   guint width, height;
494
495   if (!_is_valid_state (decoder, GST_MPEG2_DECODER_STATE_GOT_SEQ_HDR)) {
496     GST_ERROR_OBJECT (decoder, "no sequence before parsing sequence-extension");
497     return FALSE;
498   }
499
500   if (!gst_mpeg_video_packet_parse_sequence_extension (packet, &seq_ext)) {
501     GST_ERROR_OBJECT (decoder, "failed to parse sequence-extension");
502     return FALSE;
503   }
504
505   if (_seq_ext_is_valid (&priv->seq_ext) &&
506       memcmp (&priv->seq_ext, &seq_ext, sizeof (seq_ext)) == 0)
507     return TRUE;
508
509   priv->seq_ext = seq_ext;
510   priv->seq_changed = TRUE;
511
512   priv->progressive = seq_ext.progressive;
513
514   if (seq_ext.fps_n_ext && seq_ext.fps_d_ext) {
515     guint fps_n = priv->tsg.fps_n;
516     guint fps_d = priv->tsg.fps_d;
517     fps_n *= seq_ext.fps_n_ext + 1;
518     fps_d *= seq_ext.fps_d_ext + 1;
519     _pts_set_framerate (&priv->tsg, fps_n, fps_d);
520     gst_mpeg2_decoder_set_latency (decoder);
521   }
522
523   width = (priv->width & 0x0fff) | ((guint32) seq_ext.horiz_size_ext << 12);
524   height = (priv->height & 0x0fff) | ((guint32) seq_ext.vert_size_ext << 12);
525   GST_DEBUG_OBJECT (decoder, "video resolution %ux%u", width, height);
526   priv->width = width;
527   priv->height = height;
528
529   priv->profile = seq_ext.profile;
530
531   priv->state |= GST_MPEG2_DECODER_STATE_GOT_SEQ_EXT;
532
533   return TRUE;
534 }
535
536 static gboolean
537 gst_mpeg2_decoder_handle_sequence_display_ext (GstMpeg2Decoder * decoder,
538     GstMpegVideoPacket * packet)
539 {
540   GstMpeg2DecoderPrivate *priv = decoder->priv;
541   GstMpegVideoSequenceDisplayExt seq_display_ext = { 0, };
542
543   if (!_is_valid_state (decoder, GST_MPEG2_DECODER_STATE_GOT_SEQ_HDR)) {
544     GST_ERROR_OBJECT (decoder,
545         "no sequence before parsing sequence-display-extension");
546     return FALSE;
547   }
548
549   if (!gst_mpeg_video_packet_parse_sequence_display_extension (packet,
550           &seq_display_ext)) {
551     GST_ERROR_OBJECT (decoder, "failed to parse sequence-display-extension");
552     return FALSE;
553   }
554
555   if (_seq_display_ext_is_valid (&priv->seq_display_ext) &&
556       memcmp (&priv->seq_display_ext, &seq_display_ext,
557           sizeof (seq_display_ext)) == 0)
558     return TRUE;
559
560   priv->seq_display_ext = seq_display_ext;
561   priv->seq_changed = TRUE;
562
563   priv->display_width = seq_display_ext.display_horizontal_size;
564   priv->display_height = seq_display_ext.display_vertical_size;
565
566   return TRUE;
567 }
568
569 static gboolean
570 gst_mpeg2_decoder_handle_sequence_scalable_ext (GstMpeg2Decoder * decoder,
571     GstMpegVideoPacket * packet)
572 {
573   GstMpeg2DecoderPrivate *priv = decoder->priv;
574   GstMpegVideoSequenceScalableExt seq_scalable_ext = { 0, };
575
576   if (!_is_valid_state (decoder, GST_MPEG2_DECODER_STATE_GOT_SEQ_HDR)) {
577     GST_ERROR_OBJECT (decoder,
578         "no sequence before parsing sequence-scalable-extension");
579     return FALSE;
580   }
581
582   if (!gst_mpeg_video_packet_parse_sequence_scalable_extension (packet,
583           &seq_scalable_ext)) {
584     GST_ERROR_OBJECT (decoder, "failed to parse sequence-scalable-extension");
585     return FALSE;
586   }
587
588   if (_seq_scalable_ext_is_valid (&priv->seq_scalable_ext) &&
589       memcmp (&priv->seq_scalable_ext, &seq_scalable_ext,
590           sizeof (seq_scalable_ext)) == 0)
591     return TRUE;
592
593   priv->seq_scalable_ext = seq_scalable_ext;
594   priv->seq_changed = TRUE;
595
596   return TRUE;
597 }
598
599 static gboolean
600 gst_mpeg2_decoder_handle_quant_matrix_ext (GstMpeg2Decoder * decoder,
601     GstMpegVideoPacket * packet)
602 {
603   GstMpeg2DecoderPrivate *priv = decoder->priv;
604   GstMpegVideoQuantMatrixExt matrix_ext = { 0, };
605
606   if (!gst_mpeg_video_packet_parse_quant_matrix_extension (packet, &matrix_ext)) {
607     GST_ERROR_OBJECT (decoder, "failed to parse sequence-scalable-extension");
608     return FALSE;
609   }
610
611   priv->quant_matrix = matrix_ext;
612
613   return TRUE;
614 }
615
616 static gboolean
617 gst_mpeg2_decoder_handle_picture_ext (GstMpeg2Decoder * decoder,
618     GstMpegVideoPacket * packet)
619 {
620   GstMpeg2DecoderPrivate *priv = decoder->priv;
621   GstMpegVideoPictureExt pic_ext = { {{0,},}, };
622
623   if (!_is_valid_state (decoder,
624           GST_MPEG2_DECODER_STATE_VALID_SEQ_HEADERS |
625           GST_MPEG2_DECODER_STATE_GOT_PIC_HDR)) {
626     GST_ERROR_OBJECT (decoder,
627         "no sequence before parsing sequence-scalable-extension");
628     return FALSE;
629   }
630
631   if (!gst_mpeg_video_packet_parse_picture_extension (packet, &pic_ext)) {
632     GST_ERROR_OBJECT (decoder, "failed to parse picture-extension");
633     return FALSE;
634   }
635
636   if (priv->progressive && !pic_ext.progressive_frame) {
637     GST_WARNING_OBJECT (decoder,
638         "invalid interlaced frame in progressive sequence, fixing");
639     pic_ext.progressive_frame = 1;
640   }
641
642   if (pic_ext.picture_structure == 0 ||
643       (pic_ext.progressive_frame &&
644           pic_ext.picture_structure !=
645           GST_MPEG_VIDEO_PICTURE_STRUCTURE_FRAME)) {
646     GST_WARNING_OBJECT (decoder,
647         "invalid picture_structure %d, replacing with \"frame\"",
648         pic_ext.picture_structure);
649     pic_ext.picture_structure = GST_MPEG_VIDEO_PICTURE_STRUCTURE_FRAME;
650   }
651
652   priv->pic_ext = pic_ext;
653
654   priv->state |= GST_MPEG2_DECODER_STATE_GOT_PIC_EXT;
655
656   return TRUE;
657 }
658
659 static gboolean
660 gst_mpeg2_decoder_handle_gop (GstMpeg2Decoder * decoder,
661     GstMpegVideoPacket * packet)
662 {
663   GstMpeg2DecoderPrivate *priv = decoder->priv;
664   GstMpegVideoGop gop = { 0, };
665
666   if (!gst_mpeg_video_packet_parse_gop (packet, &gop)) {
667     GST_ERROR_OBJECT (decoder, "failed to parse GOP");
668     return FALSE;
669   }
670
671   GST_DEBUG_OBJECT (decoder,
672       "GOP %02u:%02u:%02u:%02u (closed_gop %d, broken_link %d)", gop.hour,
673       gop.minute, gop.second, gop.frame, gop.closed_gop, gop.broken_link);
674
675   priv->gop = gop;
676
677   _pts_sync (&priv->tsg, priv->current_frame->pts);
678
679   return TRUE;
680 }
681
682 static gboolean
683 gst_mpeg2_decoder_handle_picture (GstMpeg2Decoder * decoder,
684     GstMpegVideoPacket * packet)
685 {
686   GstMpeg2DecoderPrivate *priv = decoder->priv;
687   GstMpegVideoPictureHdr pic_hdr = { 0, };
688   GstMpeg2DecoderClass *klass = GST_MPEG2_DECODER_GET_CLASS (decoder);
689
690   if (!_is_valid_state (decoder, GST_MPEG2_DECODER_STATE_VALID_SEQ_HEADERS)) {
691     GST_ERROR_OBJECT (decoder, "no sequence before parsing picture header");
692     return FALSE;
693   }
694
695   /* 6.1.1.6: Conversely if no sequence_xxx_extension() occurs between
696      the first sequence_header() and the first picture_header() then
697      sequence_xxx_extension() shall not occur in the bitstream. */
698   if (priv->seq_changed && klass->new_sequence) {
699     priv->seq_changed = FALSE;
700     if (!klass->new_sequence (decoder, &priv->seq_hdr,
701             _seq_ext_is_valid (&priv->seq_ext) ? &priv->seq_ext : NULL,
702             _seq_display_ext_is_valid (&priv->seq_display_ext) ?
703             &priv->seq_display_ext : NULL,
704             _seq_scalable_ext_is_valid (&priv->seq_scalable_ext) ?
705             &priv->seq_scalable_ext : NULL)) {
706       GST_ERROR_OBJECT (decoder, "new sequence error");
707       return FALSE;
708     }
709   }
710
711   priv->state &= (GST_MPEG2_DECODER_STATE_GOT_SEQ_HDR |
712       GST_MPEG2_DECODER_STATE_GOT_SEQ_EXT);
713
714   if (!gst_mpeg_video_packet_parse_picture_header (packet, &pic_hdr)) {
715     GST_ERROR_OBJECT (decoder, "failed to parse picture header");
716     return FALSE;
717   }
718
719   priv->pic_hdr = pic_hdr;
720
721   priv->state |= GST_MPEG2_DECODER_STATE_GOT_PIC_HDR;
722
723   return TRUE;
724 }
725
726 static gboolean
727 gst_mpeg2_decoder_start_current_picture (GstMpeg2Decoder * decoder,
728     GstMpeg2Slice * slice)
729 {
730   GstMpeg2DecoderPrivate *priv = decoder->priv;
731   GstMpeg2DecoderClass *klass = GST_MPEG2_DECODER_GET_CLASS (decoder);
732   GstMpeg2Picture *prev_picture, *next_picture;
733   gboolean ret;
734
735   if (!klass->start_picture)
736     return TRUE;
737
738   gst_mpeg2_dpb_get_neighbours (priv->dpb, priv->current_picture,
739       &prev_picture, &next_picture);
740
741   if (priv->current_picture->type == GST_MPEG_VIDEO_PICTURE_TYPE_B
742       && !prev_picture && !priv->gop.closed_gop) {
743     GST_VIDEO_CODEC_FRAME_FLAG_SET (priv->current_frame,
744         GST_VIDEO_CODEC_FRAME_FLAG_DECODE_ONLY);
745   }
746
747   ret = klass->start_picture (decoder, priv->current_picture, slice,
748       prev_picture, next_picture);
749
750   if (!ret) {
751     GST_ERROR_OBJECT (decoder, "subclass does not want to start picture");
752     return FALSE;
753   }
754
755   return TRUE;
756 }
757
758 static gboolean
759 gst_mpeg2_decoder_ensure_current_picture (GstMpeg2Decoder * decoder,
760     GstMpeg2Slice * slice)
761 {
762   GstMpeg2DecoderPrivate *priv = decoder->priv;
763   GstMpeg2DecoderClass *klass = GST_MPEG2_DECODER_GET_CLASS (decoder);
764   GstMpeg2Picture *picture = NULL;
765   gboolean ret = TRUE;
766
767   if (priv->current_picture) {
768     g_assert (_is_valid_state (decoder, GST_MPEG2_DECODER_STATE_GOT_SLICE));
769     return TRUE;
770   }
771
772   if (priv->progressive ||
773       priv->pic_ext.picture_structure ==
774       GST_MPEG_VIDEO_PICTURE_STRUCTURE_FRAME) {
775     g_assert (!_is_valid_state (decoder, GST_MPEG2_DECODER_STATE_GOT_SLICE));
776
777     if (priv->first_field) {
778       GST_WARNING_OBJECT (decoder, "An unmatched first field");
779       gst_mpeg2_picture_clear (&priv->first_field);
780     }
781
782     picture = gst_mpeg2_picture_new ();
783     if (klass->new_picture)
784       ret = klass->new_picture (decoder, priv->current_frame, picture);
785
786     if (!ret) {
787       GST_ERROR_OBJECT (decoder, "subclass does not want accept new picture");
788       gst_mpeg2_picture_unref (picture);
789       return FALSE;
790     }
791
792     picture->structure = GST_MPEG_VIDEO_PICTURE_STRUCTURE_FRAME;
793   } else {
794     if (!priv->first_field) {
795       picture = gst_mpeg2_picture_new ();
796       if (klass->new_picture)
797         ret = klass->new_picture (decoder, priv->current_frame, picture);
798
799       if (!ret) {
800         GST_ERROR_OBJECT (decoder, "subclass does not want accept new picture");
801         gst_mpeg2_picture_unref (picture);
802         return FALSE;
803       }
804     } else {
805       picture = gst_mpeg2_picture_new ();
806
807       if (klass->new_field_picture)
808         ret = klass->new_field_picture (decoder, priv->first_field, picture);
809
810       if (!ret) {
811         GST_ERROR_OBJECT (decoder,
812             "Subclass couldn't handle new field picture");
813         gst_mpeg2_picture_unref (picture);
814         return FALSE;
815       }
816
817       picture->first_field = gst_mpeg2_picture_ref (priv->first_field);
818
819       /* At this moment, this picture should be interlaced */
820       picture->buffer_flags |= GST_VIDEO_BUFFER_FLAG_INTERLACED;
821       if (priv->pic_ext.top_field_first)
822         picture->buffer_flags |= GST_VIDEO_BUFFER_FLAG_TFF;
823     }
824
825     picture->structure = priv->pic_ext.picture_structure;
826   }
827
828   picture->needed_for_output = TRUE;
829   /* This allows accessing the frame from the picture. */
830   picture->system_frame_number = priv->current_frame->system_frame_number;
831   picture->type = priv->pic_hdr.pic_type;
832   picture->tsn = priv->pic_hdr.tsn;
833   priv->current_pts =
834       _pts_eval (&priv->tsg, priv->current_frame->pts, picture->tsn);
835   picture->pic_order_cnt = _pts_get_poc (&priv->tsg);
836
837   priv->current_picture = picture;
838   GST_LOG_OBJECT (decoder,
839       "Create new picture %p(%s), system number: %d, poc: %d,"
840       " type: 0x%d, first field %p",
841       picture,
842       (picture->structure == GST_MPEG_VIDEO_PICTURE_STRUCTURE_FRAME) ?
843       "frame" : "field",
844       picture->system_frame_number, picture->pic_order_cnt, picture->type,
845       picture->first_field);
846
847   if (!gst_mpeg2_decoder_start_current_picture (decoder, slice))
848     return FALSE;
849
850   return TRUE;
851 }
852
853 static gboolean
854 gst_mpeg2_decoder_finish_current_field (GstMpeg2Decoder * decoder)
855 {
856   GstMpeg2DecoderPrivate *priv = decoder->priv;
857   GstMpeg2DecoderClass *klass = GST_MPEG2_DECODER_GET_CLASS (decoder);
858   gboolean ret;
859
860   if (priv->current_picture == NULL)
861     return TRUE;
862
863   ret = klass->end_picture (decoder, priv->current_picture);
864   if (!ret) {
865     GST_ERROR_OBJECT (decoder, "subclass end_picture failed");
866     return FALSE;
867   }
868
869   if (priv->current_picture->structure !=
870       GST_MPEG_VIDEO_PICTURE_STRUCTURE_FRAME &&
871       !priv->current_picture->first_field) {
872     priv->first_field = priv->current_picture;
873     priv->current_picture = NULL;
874   } else {
875     GST_WARNING_OBJECT (decoder, "The current picture %p is not %s, should not "
876         "begin another picture. Just discard this.",
877         priv->current_picture, priv->current_picture->structure ==
878         GST_MPEG_VIDEO_PICTURE_STRUCTURE_FRAME ?
879         " a field" : "the first field");
880     gst_mpeg2_picture_clear (&priv->current_picture);
881   }
882
883   return TRUE;
884 }
885
886 static gboolean
887 gst_mpeg2_decoder_finish_current_picture (GstMpeg2Decoder * decoder)
888 {
889   GstMpeg2DecoderPrivate *priv = decoder->priv;
890   GstMpeg2DecoderClass *klass = GST_MPEG2_DECODER_GET_CLASS (decoder);
891   gboolean ret;
892
893   g_assert (priv->current_picture != NULL);
894
895   ret = klass->end_picture (decoder, priv->current_picture);
896   if (!ret) {
897     GST_ERROR_OBJECT (decoder, "subclass end_picture failed");
898     return FALSE;
899   }
900
901   if (priv->current_picture->structure !=
902       GST_MPEG_VIDEO_PICTURE_STRUCTURE_FRAME &&
903       !priv->current_picture->first_field) {
904     priv->first_field = priv->current_picture;
905     priv->current_picture = NULL;
906   }
907
908   return TRUE;
909 }
910
911 static gboolean
912 gst_mpeg2_decoder_handle_slice (GstMpeg2Decoder * decoder,
913     GstMpegVideoPacket * packet)
914 {
915   GstMpeg2DecoderPrivate *priv = decoder->priv;
916   GstMpegVideoSliceHdr slice_hdr;
917   GstMpeg2DecoderClass *klass = GST_MPEG2_DECODER_GET_CLASS (decoder);
918   GstMpeg2Slice slice;
919   gboolean ret;
920
921   if (!_is_valid_state (decoder, GST_MPEG2_DECODER_STATE_VALID_PIC_HEADERS)) {
922     GST_ERROR_OBJECT (decoder,
923         "no sequence or picture header before parsing picture header");
924     return FALSE;
925   }
926
927   if (!gst_mpeg_video_packet_parse_slice_header (packet, &slice_hdr,
928           &priv->seq_hdr,
929           _seq_scalable_ext_is_valid (&priv->seq_scalable_ext) ?
930           &priv->seq_scalable_ext : NULL)) {
931     GST_ERROR_OBJECT (decoder, "failed to parse slice header");
932     return FALSE;
933   }
934
935   slice.header = slice_hdr;
936   slice.packet = *packet;
937   slice.quant_matrix = _quant_matrix_ext_is_valid (&priv->quant_matrix) ?
938       &priv->quant_matrix : NULL;
939   g_assert (_pic_hdr_is_valid (&priv->pic_hdr));
940   slice.pic_hdr = &priv->pic_hdr;
941   slice.pic_ext = _pic_hdr_ext_is_valid (&priv->pic_ext) ?
942       &priv->pic_ext : NULL;
943
944   if (!gst_mpeg2_decoder_ensure_current_picture (decoder, &slice)) {
945     GST_ERROR_OBJECT (decoder, "failed to start current picture");
946     return FALSE;
947   }
948
949   g_assert (klass->decode_slice);
950   ret = klass->decode_slice (decoder, priv->current_picture, &slice);
951   if (!ret) {
952     GST_ERROR_OBJECT (decoder,
953         "Subclass didn't want to decode picture %p (frame_num %d, poc %d)",
954         priv->current_picture, priv->current_picture->system_frame_number,
955         priv->current_picture->pic_order_cnt);
956     return FALSE;
957   }
958
959   priv->state |= GST_MPEG2_DECODER_STATE_GOT_SLICE;
960
961   return TRUE;
962 }
963
964 static gboolean
965 gst_mpeg2_decoder_decode_packet (GstMpeg2Decoder * decoder,
966     GstMpegVideoPacket * packet)
967 {
968   GstMpegVideoPacketExtensionCode ext_type;
969   gboolean ret = TRUE;
970
971   GST_LOG_OBJECT (decoder, "Parsing the packet 0x%x, size %d",
972       packet->type, packet->size);
973   switch (packet->type) {
974     case GST_MPEG_VIDEO_PACKET_PICTURE:{
975       ret = gst_mpeg2_decoder_finish_current_field (decoder);
976       if (!ret)
977         break;
978
979       ret = gst_mpeg2_decoder_handle_picture (decoder, packet);
980       break;
981     }
982     case GST_MPEG_VIDEO_PACKET_SEQUENCE:
983       ret = gst_mpeg2_decoder_handle_sequence (decoder, packet);
984       break;
985     case GST_MPEG_VIDEO_PACKET_EXTENSION:
986       ext_type = packet->data[packet->offset] >> 4;
987       GST_LOG_OBJECT (decoder, "  Parsing the ext packet 0x%x", ext_type);
988       switch (ext_type) {
989         case GST_MPEG_VIDEO_PACKET_EXT_SEQUENCE:
990           ret = gst_mpeg2_decoder_handle_sequence_ext (decoder, packet);
991           break;
992         case GST_MPEG_VIDEO_PACKET_EXT_SEQUENCE_DISPLAY:
993           ret = gst_mpeg2_decoder_handle_sequence_display_ext (decoder, packet);
994           break;
995         case GST_MPEG_VIDEO_PACKET_EXT_SEQUENCE_SCALABLE:
996           ret =
997               gst_mpeg2_decoder_handle_sequence_scalable_ext (decoder, packet);
998           break;
999         case GST_MPEG_VIDEO_PACKET_EXT_QUANT_MATRIX:
1000           ret = gst_mpeg2_decoder_handle_quant_matrix_ext (decoder, packet);
1001           break;
1002         case GST_MPEG_VIDEO_PACKET_EXT_PICTURE:
1003           ret = gst_mpeg2_decoder_handle_picture_ext (decoder, packet);
1004           break;
1005         default:
1006           /* Ignore unknown start-code extensions */
1007           ret = TRUE;
1008           break;
1009       }
1010       break;
1011     case GST_MPEG_VIDEO_PACKET_SEQUENCE_END:
1012       ret = TRUE;
1013       break;
1014     case GST_MPEG_VIDEO_PACKET_GOP:
1015       ret = gst_mpeg2_decoder_handle_gop (decoder, packet);
1016       break;
1017     case GST_MPEG_VIDEO_PACKET_USER_DATA:
1018       ret = TRUE;
1019       break;
1020     default:
1021       if (packet->type >= GST_MPEG_VIDEO_PACKET_SLICE_MIN &&
1022           packet->type <= GST_MPEG_VIDEO_PACKET_SLICE_MAX) {
1023         ret = gst_mpeg2_decoder_handle_slice (decoder, packet);
1024         break;
1025       }
1026       GST_WARNING_OBJECT (decoder, "unsupported packet type 0x%02x, ignore",
1027           packet->type);
1028       ret = TRUE;
1029       break;
1030   }
1031
1032   return ret;
1033 }
1034
1035 static GstFlowReturn
1036 gst_mpeg2_decoder_do_output_picture (GstMpeg2Decoder * decoder,
1037     GstMpeg2Picture * to_output)
1038 {
1039   GstMpeg2DecoderClass *klass = GST_MPEG2_DECODER_GET_CLASS (decoder);
1040   GstVideoCodecFrame *frame = NULL;
1041   GstFlowReturn ret = GST_FLOW_OK;
1042
1043   frame =
1044       gst_video_decoder_get_frame (GST_VIDEO_DECODER (decoder),
1045       to_output->system_frame_number);
1046   if (!frame) {
1047     GST_ERROR_OBJECT (decoder,
1048         "No available codec frame with frame number %d",
1049         to_output->system_frame_number);
1050     gst_mpeg2_picture_unref (to_output);
1051     return GST_FLOW_ERROR;
1052   }
1053
1054   g_assert (klass->output_picture);
1055   GST_LOG_OBJECT (decoder,
1056       "Output picture %p (frame_num %d, poc %d, pts: %" GST_TIME_FORMAT
1057       "), from DPB",
1058       to_output, to_output->system_frame_number, to_output->pic_order_cnt,
1059       GST_TIME_ARGS (frame->pts));
1060   ret = klass->output_picture (decoder, frame, to_output);
1061
1062   return ret;
1063 }
1064
1065 static GstFlowReturn
1066 gst_mpeg2_decoder_output_current_picture (GstMpeg2Decoder * decoder)
1067 {
1068   GstMpeg2DecoderPrivate *priv = decoder->priv;
1069   GstMpeg2Picture *picture = priv->current_picture;
1070   GstFlowReturn ret = GST_FLOW_OK;
1071
1072   if (!picture && priv->first_field) {
1073     GST_WARNING_OBJECT (decoder, "Missing the second field");
1074     picture = priv->first_field;
1075   }
1076
1077   g_assert (picture);
1078
1079   /* Update the presentation time */
1080   priv->current_frame->pts = priv->current_pts;
1081
1082   gst_mpeg2_dpb_add (priv->dpb, picture);
1083
1084   GST_LOG_OBJECT (decoder,
1085       "Add picture %p (frame_num %d, poc %d, type 0x%x), into DPB", picture,
1086       picture->system_frame_number, picture->pic_order_cnt, picture->type);
1087
1088   while (gst_mpeg2_dpb_need_bump (priv->dpb)) {
1089     GstMpeg2Picture *to_output;
1090
1091     to_output = gst_mpeg2_dpb_bump (priv->dpb);
1092     g_assert (to_output);
1093     ret = gst_mpeg2_decoder_do_output_picture (decoder, to_output);
1094     if (ret != GST_FLOW_OK)
1095       break;
1096   }
1097
1098   return ret;
1099 }
1100
1101 static GstFlowReturn
1102 gst_mpeg2_decoder_handle_frame (GstVideoDecoder * decoder,
1103     GstVideoCodecFrame * frame)
1104 {
1105   GstMpeg2Decoder *self = GST_MPEG2_DECODER (decoder);
1106   GstMpeg2DecoderPrivate *priv = self->priv;
1107   GstBuffer *in_buf = frame->input_buffer;
1108   GstMapInfo map_info;
1109   GstMpegVideoPacket packet;
1110   GstFlowReturn ret = GST_FLOW_OK;
1111   guint offset;
1112   gboolean last_one;
1113
1114   GST_LOG_OBJECT (self, "handle frame, PTS: %" GST_TIME_FORMAT
1115       ", DTS: %" GST_TIME_FORMAT " system frame number is %d",
1116       GST_TIME_ARGS (GST_BUFFER_PTS (in_buf)),
1117       GST_TIME_ARGS (GST_BUFFER_DTS (in_buf)), frame->system_frame_number);
1118
1119   priv->state &= ~GST_MPEG2_DECODER_STATE_GOT_SLICE;
1120
1121   priv->current_frame = frame;
1122   gst_buffer_map (in_buf, &map_info, GST_MAP_READ);
1123
1124   offset = 0;
1125   last_one = FALSE;
1126   while (gst_mpeg_video_parse (&packet, map_info.data, map_info.size, offset)) {
1127     /* The packet is the last one */
1128     if (packet.size == -1) {
1129       if (packet.offset < map_info.size) {
1130         packet.size = map_info.size - packet.offset;
1131         last_one = TRUE;
1132       } else {
1133         GST_WARNING_OBJECT (decoder, "Get a packet with wrong size");
1134         break;
1135       }
1136     }
1137
1138     if (!gst_mpeg2_decoder_decode_packet (self, &packet)) {
1139       gst_buffer_unmap (in_buf, &map_info);
1140       GST_ERROR_OBJECT (decoder, "failed to handle the packet type 0x%x",
1141           packet.type);
1142       goto failed;
1143     }
1144
1145     if (last_one)
1146       break;
1147
1148     offset = packet.offset;
1149   }
1150
1151   gst_buffer_unmap (in_buf, &map_info);
1152
1153   if (!priv->current_picture) {
1154     GST_ERROR_OBJECT (decoder, "no valid picture created");
1155     goto failed;
1156   }
1157
1158   if (!gst_mpeg2_decoder_finish_current_picture (self)) {
1159     GST_ERROR_OBJECT (decoder, "failed to decode the current picture");
1160     goto failed;
1161   }
1162
1163   ret = gst_mpeg2_decoder_output_current_picture (self);
1164   gst_mpeg2_picture_clear (&priv->current_picture);
1165   gst_mpeg2_picture_clear (&priv->first_field);
1166   gst_video_codec_frame_unref (priv->current_frame);
1167   priv->current_frame = NULL;
1168   return ret;
1169
1170 failed:
1171   {
1172     GST_VIDEO_DECODER_ERROR (decoder, 1, STREAM, DECODE,
1173         ("failed to handle the frame %d", frame->system_frame_number), (NULL),
1174         ret);
1175     gst_video_decoder_drop_frame (decoder, frame);
1176     gst_mpeg2_picture_clear (&priv->current_picture);
1177     gst_mpeg2_picture_clear (&priv->first_field);
1178     priv->current_frame = NULL;
1179     return ret;
1180   }
1181 }