• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) 2020 He Junyan <junyan.he@intel.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 /**
21  * SECTION:gstav1decoder
22  * @title: Gstav1Decoder
23  * @short_description: Base class to implement stateless AV1 decoders
24  * @sources:
25  * - gstav1picture.h
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 
32 #include "gstav1decoder.h"
33 
34 GST_DEBUG_CATEGORY (gst_av1_decoder_debug);
35 #define GST_CAT_DEFAULT gst_av1_decoder_debug
36 
37 struct _GstAV1DecoderPrivate
38 {
39   gint max_width;
40   gint max_height;
41   GstAV1Profile profile;
42   GstAV1Parser *parser;
43   GstAV1Dpb *dpb;
44   GstAV1Picture *current_picture;
45   GstVideoCodecFrame *current_frame;
46 };
47 
48 #define parent_class gst_av1_decoder_parent_class
49 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GstAV1Decoder, gst_av1_decoder,
50     GST_TYPE_VIDEO_DECODER,
51     G_ADD_PRIVATE (GstAV1Decoder);
52     GST_DEBUG_CATEGORY_INIT (gst_av1_decoder_debug, "av1decoder", 0,
53         "AV1 Video Decoder"));
54 
55 static gint
_floor_log2(guint32 x)56 _floor_log2 (guint32 x)
57 {
58   gint s = 0;
59 
60   while (x != 0) {
61     x = x >> 1;
62     s++;
63   }
64   return s - 1;
65 }
66 
67 static gboolean gst_av1_decoder_start (GstVideoDecoder * decoder);
68 static gboolean gst_av1_decoder_stop (GstVideoDecoder * decoder);
69 static gboolean gst_av1_decoder_set_format (GstVideoDecoder * decoder,
70     GstVideoCodecState * state);
71 static GstFlowReturn gst_av1_decoder_finish (GstVideoDecoder * decoder);
72 static gboolean gst_av1_decoder_flush (GstVideoDecoder * decoder);
73 static GstFlowReturn gst_av1_decoder_drain (GstVideoDecoder * decoder);
74 static GstFlowReturn gst_av1_decoder_handle_frame (GstVideoDecoder * decoder,
75     GstVideoCodecFrame * frame);
76 
77 static GstAV1Picture *gst_av1_decoder_duplicate_picture_default (GstAV1Decoder *
78     decoder, GstAV1Picture * picture);
79 
80 static void
gst_av1_decoder_class_init(GstAV1DecoderClass * klass)81 gst_av1_decoder_class_init (GstAV1DecoderClass * klass)
82 {
83   GstVideoDecoderClass *decoder_class = GST_VIDEO_DECODER_CLASS (klass);
84 
85   decoder_class->start = GST_DEBUG_FUNCPTR (gst_av1_decoder_start);
86   decoder_class->stop = GST_DEBUG_FUNCPTR (gst_av1_decoder_stop);
87   decoder_class->set_format = GST_DEBUG_FUNCPTR (gst_av1_decoder_set_format);
88   decoder_class->finish = GST_DEBUG_FUNCPTR (gst_av1_decoder_finish);
89   decoder_class->flush = GST_DEBUG_FUNCPTR (gst_av1_decoder_flush);
90   decoder_class->drain = GST_DEBUG_FUNCPTR (gst_av1_decoder_drain);
91   decoder_class->handle_frame =
92       GST_DEBUG_FUNCPTR (gst_av1_decoder_handle_frame);
93 
94   klass->duplicate_picture =
95       GST_DEBUG_FUNCPTR (gst_av1_decoder_duplicate_picture_default);
96 }
97 
98 static void
gst_av1_decoder_init(GstAV1Decoder * self)99 gst_av1_decoder_init (GstAV1Decoder * self)
100 {
101   gst_video_decoder_set_packetized (GST_VIDEO_DECODER (self), TRUE);
102 
103   self->priv = gst_av1_decoder_get_instance_private (self);
104 }
105 
106 static void
gst_av1_decoder_reset(GstAV1Decoder * self)107 gst_av1_decoder_reset (GstAV1Decoder * self)
108 {
109   GstAV1DecoderPrivate *priv = self->priv;
110 
111   priv->max_width = 0;
112   priv->max_height = 0;
113   gst_av1_picture_clear (&priv->current_picture);
114   priv->current_frame = NULL;
115   priv->profile = GST_AV1_PROFILE_UNDEFINED;
116 
117   if (priv->dpb)
118     gst_av1_dpb_clear (priv->dpb);
119   if (priv->parser)
120     gst_av1_parser_reset (priv->parser, FALSE);
121 }
122 
123 static gboolean
gst_av1_decoder_start(GstVideoDecoder * decoder)124 gst_av1_decoder_start (GstVideoDecoder * decoder)
125 {
126   GstAV1Decoder *self = GST_AV1_DECODER (decoder);
127   GstAV1DecoderPrivate *priv = self->priv;
128 
129   priv->parser = gst_av1_parser_new ();
130   priv->dpb = gst_av1_dpb_new ();
131 
132   gst_av1_decoder_reset (self);
133 
134   return TRUE;
135 }
136 
137 static gboolean
gst_av1_decoder_stop(GstVideoDecoder * decoder)138 gst_av1_decoder_stop (GstVideoDecoder * decoder)
139 {
140   GstAV1Decoder *self = GST_AV1_DECODER (decoder);
141   GstAV1DecoderPrivate *priv = self->priv;
142 
143   gst_av1_decoder_reset (self);
144 
145   g_clear_pointer (&self->input_state, gst_video_codec_state_unref);
146   g_clear_pointer (&priv->parser, gst_av1_parser_free);
147   g_clear_pointer (&priv->dpb, gst_av1_dpb_free);
148 
149   return TRUE;
150 }
151 
152 static gboolean
gst_av1_decoder_set_format(GstVideoDecoder * decoder,GstVideoCodecState * state)153 gst_av1_decoder_set_format (GstVideoDecoder * decoder,
154     GstVideoCodecState * state)
155 {
156   GstAV1Decoder *self = GST_AV1_DECODER (decoder);
157   GstAV1DecoderPrivate *priv = self->priv;
158 
159   GST_DEBUG_OBJECT (decoder, "Set format");
160 
161   if (self->input_state)
162     gst_video_codec_state_unref (self->input_state);
163 
164   self->input_state = gst_video_codec_state_ref (state);
165 
166   priv->max_width = GST_VIDEO_INFO_WIDTH (&state->info);
167   priv->max_height = GST_VIDEO_INFO_HEIGHT (&state->info);
168 
169   return TRUE;
170 }
171 
172 static GstFlowReturn
gst_av1_decoder_finish(GstVideoDecoder * decoder)173 gst_av1_decoder_finish (GstVideoDecoder * decoder)
174 {
175   GST_DEBUG_OBJECT (decoder, "finish");
176 
177   gst_av1_decoder_reset (GST_AV1_DECODER (decoder));
178 
179   return GST_FLOW_OK;
180 }
181 
182 static gboolean
gst_av1_decoder_flush(GstVideoDecoder * decoder)183 gst_av1_decoder_flush (GstVideoDecoder * decoder)
184 {
185   GST_DEBUG_OBJECT (decoder, "flush");
186 
187   gst_av1_decoder_reset (GST_AV1_DECODER (decoder));
188 
189   return TRUE;
190 }
191 
192 static GstFlowReturn
gst_av1_decoder_drain(GstVideoDecoder * decoder)193 gst_av1_decoder_drain (GstVideoDecoder * decoder)
194 {
195   GST_DEBUG_OBJECT (decoder, "drain");
196 
197   gst_av1_decoder_reset (GST_AV1_DECODER (decoder));
198 
199   return GST_FLOW_OK;
200 }
201 
202 static GstAV1Picture *
gst_av1_decoder_duplicate_picture_default(GstAV1Decoder * decoder,GstAV1Picture * picture)203 gst_av1_decoder_duplicate_picture_default (GstAV1Decoder * decoder,
204     GstAV1Picture * picture)
205 {
206   GstAV1Picture *new_picture;
207 
208   new_picture = gst_av1_picture_new ();
209 
210   return new_picture;
211 }
212 
213 static const gchar *
get_obu_name(GstAV1OBUType type)214 get_obu_name (GstAV1OBUType type)
215 {
216   switch (type) {
217     case GST_AV1_OBU_SEQUENCE_HEADER:
218       return "sequence header";
219     case GST_AV1_OBU_TEMPORAL_DELIMITER:
220       return "temporal delimiter";
221     case GST_AV1_OBU_FRAME_HEADER:
222       return "frame header";
223     case GST_AV1_OBU_TILE_GROUP:
224       return "tile group";
225     case GST_AV1_OBU_METADATA:
226       return "metadata";
227     case GST_AV1_OBU_FRAME:
228       return "frame";
229     case GST_AV1_OBU_REDUNDANT_FRAME_HEADER:
230       return "redundant frame header";
231     case GST_AV1_OBU_TILE_LIST:
232       return "tile list";
233     case GST_AV1_OBU_PADDING:
234       return "padding";
235     default:
236       return "unknown";
237   }
238 
239   return NULL;
240 }
241 
242 static const gchar *
gst_av1_decoder_profile_to_string(GstAV1Profile profile)243 gst_av1_decoder_profile_to_string (GstAV1Profile profile)
244 {
245   switch (profile) {
246     case GST_AV1_PROFILE_0:
247       return "0";
248     case GST_AV1_PROFILE_1:
249       return "1";
250     case GST_AV1_PROFILE_2:
251       return "2";
252     default:
253       break;
254   }
255 
256   return NULL;
257 }
258 
259 static GstFlowReturn
gst_av1_decoder_process_sequence(GstAV1Decoder * self,GstAV1OBU * obu)260 gst_av1_decoder_process_sequence (GstAV1Decoder * self, GstAV1OBU * obu)
261 {
262   GstAV1ParserResult res;
263   GstAV1DecoderPrivate *priv = self->priv;
264   GstAV1SequenceHeaderOBU seq_header;
265   GstAV1SequenceHeaderOBU old_seq_header = { 0, };
266   GstAV1DecoderClass *klass = GST_AV1_DECODER_GET_CLASS (self);
267   GstFlowReturn ret = GST_FLOW_OK;
268 
269   if (priv->parser->seq_header)
270     old_seq_header = *priv->parser->seq_header;
271 
272   res = gst_av1_parser_parse_sequence_header_obu (priv->parser,
273       obu, &seq_header);
274   if (res != GST_AV1_PARSER_OK) {
275     GST_WARNING_OBJECT (self, "Parsing sequence failed.");
276     return GST_FLOW_ERROR;
277   }
278 
279   if (!memcmp (&old_seq_header, &seq_header, sizeof (GstAV1SequenceHeaderOBU))) {
280     GST_DEBUG_OBJECT (self, "Get same sequence header.");
281     return GST_FLOW_OK;
282   }
283 
284   g_assert (klass->new_sequence);
285 
286   GST_DEBUG_OBJECT (self,
287       "Sequence updated, profile %s -> %s, max resolution: %dx%d -> %dx%d",
288       gst_av1_decoder_profile_to_string (priv->profile),
289       gst_av1_decoder_profile_to_string (seq_header.seq_profile),
290       priv->max_width, priv->max_height, seq_header.max_frame_width_minus_1 + 1,
291       seq_header.max_frame_height_minus_1 + 1);
292 
293   ret = klass->new_sequence (self, &seq_header);
294   if (ret != GST_FLOW_OK) {
295     GST_ERROR_OBJECT (self, "subclass does not want accept new sequence");
296     return ret;
297   }
298 
299   priv->profile = seq_header.seq_profile;
300   priv->max_width = seq_header.max_frame_width_minus_1 + 1;
301   priv->max_height = seq_header.max_frame_height_minus_1 + 1;
302   gst_av1_dpb_clear (priv->dpb);
303 
304   return GST_FLOW_OK;
305 }
306 
307 static GstFlowReturn
gst_av1_decoder_decode_tile_group(GstAV1Decoder * self,GstAV1TileGroupOBU * tile_group,GstAV1OBU * obu)308 gst_av1_decoder_decode_tile_group (GstAV1Decoder * self,
309     GstAV1TileGroupOBU * tile_group, GstAV1OBU * obu)
310 {
311   GstAV1DecoderPrivate *priv = self->priv;
312   GstAV1DecoderClass *klass = GST_AV1_DECODER_GET_CLASS (self);
313   GstAV1Picture *picture = priv->current_picture;
314   GstAV1Tile tile;
315   GstFlowReturn ret = GST_FLOW_OK;
316 
317   if (!picture) {
318     GST_ERROR_OBJECT (self, "No picture has created for current frame");
319     return GST_FLOW_ERROR;
320   }
321 
322   if (picture->frame_hdr.show_existing_frame) {
323     GST_ERROR_OBJECT (self, "Current picture is showing the existing frame.");
324     return GST_FLOW_ERROR;
325   }
326 
327   tile.obu = *obu;
328   tile.tile_group = *tile_group;
329 
330   g_assert (klass->decode_tile);
331   ret = klass->decode_tile (self, picture, &tile);
332   if (ret != GST_FLOW_OK) {
333     GST_WARNING_OBJECT (self, "Decode tile error");
334     return ret;
335   }
336 
337   return GST_FLOW_OK;
338 }
339 
340 static GstFlowReturn
gst_av1_decoder_decode_frame_header(GstAV1Decoder * self,GstAV1FrameHeaderOBU * frame_header)341 gst_av1_decoder_decode_frame_header (GstAV1Decoder * self,
342     GstAV1FrameHeaderOBU * frame_header)
343 {
344   GstAV1DecoderPrivate *priv = self->priv;
345   GstAV1DecoderClass *klass = GST_AV1_DECODER_GET_CLASS (self);
346   GstAV1Picture *picture = NULL;
347   GstFlowReturn ret = GST_FLOW_OK;
348 
349   g_assert (priv->current_frame);
350 
351   if (priv->current_picture != NULL) {
352     GST_ERROR_OBJECT (self, "Already have picture for current frame");
353     return GST_FLOW_ERROR;
354   }
355 
356   if (frame_header->show_existing_frame) {
357     GstAV1Picture *ref_picture;
358 
359     ref_picture = priv->dpb->pic_list[frame_header->frame_to_show_map_idx];
360     if (!ref_picture) {
361       GST_WARNING_OBJECT (self, "Failed to find the frame index %d to show.",
362           frame_header->frame_to_show_map_idx);
363       return GST_FLOW_ERROR;
364     }
365 
366     if (gst_av1_parser_reference_frame_loading (priv->parser,
367             &ref_picture->frame_hdr) != GST_AV1_PARSER_OK) {
368       GST_WARNING_OBJECT (self, "load the reference frame failed");
369       return GST_FLOW_ERROR;
370     }
371 
372     /* FIXME: duplicate picture might be optional feature like that of VP9
373      * decoder baseclass */
374     g_assert (klass->duplicate_picture);
375     picture = klass->duplicate_picture (self, ref_picture);
376     if (!picture) {
377       GST_ERROR_OBJECT (self, "subclass didn't provide duplicated picture");
378       return GST_FLOW_ERROR;
379     }
380 
381     picture->system_frame_number = priv->current_frame->system_frame_number;
382     picture->frame_hdr = *frame_header;
383     picture->frame_hdr.render_width = ref_picture->frame_hdr.render_width;
384     picture->frame_hdr.render_height = ref_picture->frame_hdr.render_height;
385     priv->current_picture = picture;
386   } else {
387     picture = gst_av1_picture_new ();
388     picture->frame_hdr = *frame_header;
389     picture->display_frame_id = frame_header->display_frame_id;
390     picture->show_frame = frame_header->show_frame;
391     picture->showable_frame = frame_header->showable_frame;
392     picture->apply_grain = frame_header->film_grain_params.apply_grain;
393     picture->system_frame_number = priv->current_frame->system_frame_number;
394 
395     if (!frame_header->show_frame && !frame_header->showable_frame)
396       GST_VIDEO_CODEC_FRAME_FLAG_SET (priv->current_frame,
397           GST_VIDEO_CODEC_FRAME_FLAG_DECODE_ONLY);
398 
399     if (klass->new_picture) {
400       ret = klass->new_picture (self, priv->current_frame, picture);
401       if (ret != GST_FLOW_OK) {
402         GST_WARNING_OBJECT (self, "new picture error");
403         return ret;
404       }
405     }
406     priv->current_picture = picture;
407 
408     if (klass->start_picture) {
409       ret = klass->start_picture (self, picture, priv->dpb);
410       if (ret != GST_FLOW_OK) {
411         GST_WARNING_OBJECT (self, "start picture error");
412         return ret;
413       }
414     }
415   }
416 
417   g_assert (priv->current_picture != NULL);
418 
419   return GST_FLOW_OK;
420 }
421 
422 static GstFlowReturn
gst_av1_decoder_process_frame_header(GstAV1Decoder * self,GstAV1OBU * obu)423 gst_av1_decoder_process_frame_header (GstAV1Decoder * self, GstAV1OBU * obu)
424 {
425   GstAV1ParserResult res;
426   GstAV1DecoderPrivate *priv = self->priv;
427   GstAV1FrameHeaderOBU frame_header;
428 
429   res = gst_av1_parser_parse_frame_header_obu (priv->parser, obu,
430       &frame_header);
431   if (res != GST_AV1_PARSER_OK) {
432     GST_WARNING_OBJECT (self, "Parsing frame header failed.");
433     return GST_FLOW_ERROR;
434   }
435 
436   return gst_av1_decoder_decode_frame_header (self, &frame_header);
437 }
438 
439 static GstFlowReturn
gst_av1_decoder_process_tile_group(GstAV1Decoder * self,GstAV1OBU * obu)440 gst_av1_decoder_process_tile_group (GstAV1Decoder * self, GstAV1OBU * obu)
441 {
442   GstAV1ParserResult res;
443   GstAV1DecoderPrivate *priv = self->priv;
444   GstAV1TileGroupOBU tile_group;
445 
446   res = gst_av1_parser_parse_tile_group_obu (priv->parser, obu, &tile_group);
447   if (res != GST_AV1_PARSER_OK) {
448     GST_WARNING_OBJECT (self, "Parsing tile group failed.");
449     return GST_FLOW_ERROR;
450   }
451 
452   return gst_av1_decoder_decode_tile_group (self, &tile_group, obu);
453 }
454 
455 static GstFlowReturn
gst_av1_decoder_process_frame(GstAV1Decoder * self,GstAV1OBU * obu)456 gst_av1_decoder_process_frame (GstAV1Decoder * self, GstAV1OBU * obu)
457 {
458   GstAV1ParserResult res;
459   GstAV1DecoderPrivate *priv = self->priv;
460   GstAV1FrameOBU frame;
461   GstFlowReturn ret = GST_FLOW_OK;
462 
463   res = gst_av1_parser_parse_frame_obu (priv->parser, obu, &frame);
464   if (res != GST_AV1_PARSER_OK) {
465     GST_WARNING_OBJECT (self, "Parsing frame failed.");
466     return GST_FLOW_ERROR;
467   }
468 
469   ret = gst_av1_decoder_decode_frame_header (self, &frame.frame_header);
470   if (ret != GST_FLOW_OK)
471     return ret;
472 
473   return gst_av1_decoder_decode_tile_group (self, &frame.tile_group, obu);
474 }
475 
476 static GstFlowReturn
gst_av1_decoder_temporal_delimiter(GstAV1Decoder * self,GstAV1OBU * obu)477 gst_av1_decoder_temporal_delimiter (GstAV1Decoder * self, GstAV1OBU * obu)
478 {
479   GstAV1DecoderPrivate *priv = self->priv;
480 
481   if (gst_av1_parser_parse_temporal_delimiter_obu (priv->parser, obu) ==
482       GST_AV1_PARSER_OK) {
483     return GST_FLOW_OK;
484   }
485 
486   return GST_FLOW_ERROR;
487 }
488 
489 static GstFlowReturn
gst_av1_decoder_decode_one_obu(GstAV1Decoder * self,GstAV1OBU * obu)490 gst_av1_decoder_decode_one_obu (GstAV1Decoder * self, GstAV1OBU * obu)
491 {
492   GstFlowReturn ret = GST_FLOW_OK;
493 
494   GST_LOG_OBJECT (self, "Decode obu %s", get_obu_name (obu->obu_type));
495   switch (obu->obu_type) {
496     case GST_AV1_OBU_SEQUENCE_HEADER:
497       ret = gst_av1_decoder_process_sequence (self, obu);
498       break;
499     case GST_AV1_OBU_FRAME_HEADER:
500       ret = gst_av1_decoder_process_frame_header (self, obu);
501       break;
502     case GST_AV1_OBU_FRAME:
503       ret = gst_av1_decoder_process_frame (self, obu);
504       break;
505     case GST_AV1_OBU_TILE_GROUP:
506       ret = gst_av1_decoder_process_tile_group (self, obu);
507       break;
508     case GST_AV1_OBU_TEMPORAL_DELIMITER:
509       ret = gst_av1_decoder_temporal_delimiter (self, obu);
510       break;
511       /* TODO: may need to handled. */
512     case GST_AV1_OBU_METADATA:
513     case GST_AV1_OBU_REDUNDANT_FRAME_HEADER:
514     case GST_AV1_OBU_TILE_LIST:
515     case GST_AV1_OBU_PADDING:
516       break;
517     default:
518       GST_WARNING_OBJECT (self, "an unrecognized obu type %d", obu->obu_type);
519       break;
520   }
521 
522   if (ret != GST_FLOW_OK)
523     GST_WARNING_OBJECT (self, "Failed to handle %s OBU",
524         get_obu_name (obu->obu_type));
525 
526   return ret;
527 }
528 
529 static void
gst_av1_decoder_update_state(GstAV1Decoder * self)530 gst_av1_decoder_update_state (GstAV1Decoder * self)
531 {
532   GstAV1DecoderPrivate *priv = self->priv;
533   GstAV1Picture *picture = priv->current_picture;
534   GstAV1ParserResult res;
535   GstAV1FrameHeaderOBU *fh;
536 
537   g_assert (picture);
538   fh = &picture->frame_hdr;
539 
540   /* This is a show_existing_frame case, only update key frame */
541   if (fh->show_existing_frame && fh->frame_type != GST_AV1_KEY_FRAME)
542     return;
543 
544   res = gst_av1_parser_reference_frame_update (priv->parser, fh);
545   if (res != GST_AV1_PARSER_OK) {
546     GST_ERROR_OBJECT (self, "failed to update the reference.");
547     return;
548   }
549 
550   gst_av1_dpb_add (priv->dpb, gst_av1_picture_ref (picture));
551 }
552 
553 static GstFlowReturn
gst_av1_decoder_handle_frame(GstVideoDecoder * decoder,GstVideoCodecFrame * frame)554 gst_av1_decoder_handle_frame (GstVideoDecoder * decoder,
555     GstVideoCodecFrame * frame)
556 {
557   GstAV1Decoder *self = GST_AV1_DECODER (decoder);
558   GstAV1DecoderPrivate *priv = self->priv;
559   GstAV1DecoderClass *klass = GST_AV1_DECODER_GET_CLASS (self);
560   GstBuffer *in_buf = frame->input_buffer;
561   GstMapInfo map;
562   GstFlowReturn ret = GST_FLOW_OK;
563   guint32 total_consumed, consumed;
564   GstAV1OBU obu;
565   GstAV1ParserResult res;
566 
567   GST_LOG_OBJECT (self, "handle frame id %d, buf %" GST_PTR_FORMAT,
568       frame->system_frame_number, in_buf);
569 
570   priv->current_frame = frame;
571   g_assert (!priv->current_picture);
572 
573   if (!gst_buffer_map (in_buf, &map, GST_MAP_READ)) {
574     priv->current_frame = NULL;
575     GST_ERROR_OBJECT (self, "can not map input buffer");
576 
577     return GST_FLOW_ERROR;
578   }
579 
580   total_consumed = 0;
581   while (total_consumed < map.size) {
582     res = gst_av1_parser_identify_one_obu (priv->parser,
583         map.data + total_consumed, map.size, &obu, &consumed);
584     if (res != GST_AV1_PARSER_OK) {
585       ret = GST_FLOW_ERROR;
586       goto out;
587     }
588 
589     ret = gst_av1_decoder_decode_one_obu (self, &obu);
590     if (ret != GST_FLOW_OK) {
591       goto out;
592     }
593 
594     total_consumed += consumed;
595   }
596 
597   if (!priv->current_picture) {
598     GST_ERROR_OBJECT (self, "No valid picture after exhaust input frame");
599     ret = GST_FLOW_ERROR;
600     goto out;
601   }
602 
603   if (!priv->current_picture->frame_hdr.show_existing_frame) {
604     if (klass->end_picture) {
605       ret = klass->end_picture (self, priv->current_picture);
606       if (ret != GST_FLOW_OK) {
607         GST_WARNING_OBJECT (self, "end picture error");
608         goto out;
609       }
610     }
611   }
612 
613   gst_av1_decoder_update_state (self);
614 
615 out:
616   gst_buffer_unmap (in_buf, &map);
617 
618   if (ret == GST_FLOW_OK) {
619     if (priv->current_picture->frame_hdr.show_frame ||
620         priv->current_picture->frame_hdr.show_existing_frame) {
621       /* Only output one frame with the highest spatial id from each TU
622        * when there are multiple spatial layers.
623        */
624       if (priv->parser->state.operating_point_idc &&
625           obu.header.obu_spatial_id <
626           _floor_log2 (priv->parser->state.operating_point_idc >> 8)) {
627         gst_av1_picture_unref (priv->current_picture);
628         gst_video_decoder_release_frame (decoder, frame);
629       } else {
630         g_assert (klass->output_picture);
631         /* transfer ownership of frame and picture */
632         ret = klass->output_picture (self, frame, priv->current_picture);
633       }
634     } else {
635       GST_LOG_OBJECT (self, "Decode only picture %p", priv->current_picture);
636       GST_VIDEO_CODEC_FRAME_SET_DECODE_ONLY (frame);
637       gst_av1_picture_unref (priv->current_picture);
638       ret = gst_video_decoder_finish_frame (GST_VIDEO_DECODER (self), frame);
639     }
640   } else {
641     if (priv->current_picture)
642       gst_av1_picture_unref (priv->current_picture);
643 
644     gst_video_decoder_drop_frame (decoder, frame);
645   }
646 
647   priv->current_picture = NULL;
648   priv->current_frame = NULL;
649 
650   if (ret == GST_FLOW_ERROR) {
651     GST_VIDEO_DECODER_ERROR (decoder, 1, STREAM, DECODE,
652         ("Failed to handle the frame %d", frame->system_frame_number),
653         NULL, ret);
654   }
655 
656   return ret;
657 }
658