• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) <2017> Sean DuBois <sean@siobud.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  * SECTION:element-av1dec
21  *
22  * AV1 Decoder.
23  *
24  * ## Example launch line
25  *
26  * |[
27  * gst-launch-1.0 -v filesrc location=videotestsrc.webm ! matroskademux ! av1dec ! videoconvert ! videoscale ! autovideosink
28  * ]|
29  */
30 
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34 
35 #include <string.h>
36 #include "gstav1dec.h"
37 
38 enum
39 {
40   PROP_0,
41 };
42 
43 static GstStaticPadTemplate gst_av1_dec_sink_pad_template =
44 GST_STATIC_PAD_TEMPLATE ("sink",
45     GST_PAD_SINK,
46     GST_PAD_ALWAYS,
47     GST_STATIC_CAPS ("video/x-av1")
48     );
49 
50 static GstStaticPadTemplate gst_av1_dec_src_pad_template =
51 GST_STATIC_PAD_TEMPLATE ("src",
52     GST_PAD_SRC,
53     GST_PAD_ALWAYS,
54     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ I420, YV12, Y42B, Y444"
55 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
56             ", I420_10LE, I420_12LE, I422_10LE, I422_12LE, Y444_10LE, Y444_12LE"
57 #else
58             ", I420_10BE, I420_12BE, I422_10BE, I422_12BE, Y444_10BE, Y444_12BE"
59 #endif
60             " }"))
61     );
62 
63 GST_DEBUG_CATEGORY_STATIC (av1_dec_debug);
64 #define GST_CAT_DEFAULT av1_dec_debug
65 
66 #define GST_VIDEO_FORMAT_WITH_ENDIAN(fmt,endian) GST_VIDEO_FORMAT_##fmt##endian
67 
68 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
69 #define AOM_FMT_TO_GST(fmt) GST_VIDEO_FORMAT_WITH_ENDIAN(fmt,LE)
70 #else
71 #define AOM_FMT_TO_GST(fmt) GST_VIDEO_FORMAT_WITH_ENDIAN(fmt,BE)
72 #endif
73 
74 static void gst_av1_dec_set_property (GObject * object, guint prop_id,
75     const GValue * value, GParamSpec * pspec);
76 static void gst_av1_dec_get_property (GObject * object, guint prop_id,
77     GValue * value, GParamSpec * pspec);
78 
79 static gboolean gst_av1_dec_start (GstVideoDecoder * dec);
80 static gboolean gst_av1_dec_stop (GstVideoDecoder * dec);
81 static gboolean gst_av1_dec_set_format (GstVideoDecoder * dec,
82     GstVideoCodecState * state);
83 static gboolean gst_av1_dec_flush (GstVideoDecoder * dec);
84 static GstFlowReturn
85 gst_av1_dec_handle_frame (GstVideoDecoder * decoder,
86     GstVideoCodecFrame * frame);
87 
88 static void gst_av1_dec_image_to_buffer (GstAV1Dec * dec,
89     const aom_image_t * img, GstBuffer * buffer);
90 static GstFlowReturn gst_av1_dec_open_codec (GstAV1Dec * av1dec,
91     GstVideoCodecFrame * frame);
92 static gboolean gst_av1_dec_get_valid_format (GstAV1Dec * dec,
93     const aom_image_t * img, GstVideoFormat * fmt);
94 
95 #define gst_av1_dec_parent_class parent_class
96 G_DEFINE_TYPE (GstAV1Dec, gst_av1_dec, GST_TYPE_VIDEO_DECODER);
97 GST_ELEMENT_REGISTER_DEFINE (av1dec, "av1dec", GST_RANK_PRIMARY,
98     GST_TYPE_AV1_DEC);
99 
100 static void
gst_av1_dec_class_init(GstAV1DecClass * klass)101 gst_av1_dec_class_init (GstAV1DecClass * klass)
102 {
103   GObjectClass *gobject_class;
104   GstElementClass *element_class;
105   GstVideoDecoderClass *vdec_class;
106 
107   gobject_class = (GObjectClass *) klass;
108   element_class = (GstElementClass *) klass;
109   vdec_class = (GstVideoDecoderClass *) klass;
110 
111 
112   parent_class = g_type_class_peek_parent (klass);
113 
114   gobject_class->set_property = gst_av1_dec_set_property;
115   gobject_class->get_property = gst_av1_dec_get_property;
116 
117   gst_element_class_add_static_pad_template (element_class,
118       &gst_av1_dec_src_pad_template);
119   gst_element_class_add_static_pad_template (element_class,
120       &gst_av1_dec_sink_pad_template);
121   gst_element_class_set_static_metadata (element_class, "AV1 Decoder",
122       "Codec/Decoder/Video", "Decode AV1 video streams",
123       "Sean DuBois <sean@siobud.com>");
124 
125   vdec_class->start = GST_DEBUG_FUNCPTR (gst_av1_dec_start);
126   vdec_class->stop = GST_DEBUG_FUNCPTR (gst_av1_dec_stop);
127   vdec_class->flush = GST_DEBUG_FUNCPTR (gst_av1_dec_flush);
128 
129   vdec_class->set_format = GST_DEBUG_FUNCPTR (gst_av1_dec_set_format);
130   vdec_class->handle_frame = GST_DEBUG_FUNCPTR (gst_av1_dec_handle_frame);
131 
132   klass->codec_algo = &aom_codec_av1_dx_algo;
133   GST_DEBUG_CATEGORY_INIT (av1_dec_debug, "av1dec", 0, "AV1 decoding element");
134 }
135 
136 static void
gst_av1_dec_init(GstAV1Dec * av1dec)137 gst_av1_dec_init (GstAV1Dec * av1dec)
138 {
139   GstVideoDecoder *dec = (GstVideoDecoder *) av1dec;
140 
141   GST_DEBUG_OBJECT (dec, "gst_av1_dec_init");
142   gst_video_decoder_set_packetized (dec, TRUE);
143   gst_video_decoder_set_needs_format (dec, TRUE);
144   gst_video_decoder_set_use_default_pad_acceptcaps (dec, TRUE);
145   GST_PAD_SET_ACCEPT_TEMPLATE (GST_VIDEO_DECODER_SINK_PAD (dec));
146 }
147 
148 static void
gst_av1_dec_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)149 gst_av1_dec_set_property (GObject * object, guint prop_id,
150     const GValue * value, GParamSpec * pspec)
151 {
152   switch (prop_id) {
153     default:
154       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
155       break;
156   }
157 }
158 
159 static void
gst_av1_dec_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)160 gst_av1_dec_get_property (GObject * object, guint prop_id, GValue * value,
161     GParamSpec * pspec)
162 {
163   switch (prop_id) {
164     default:
165       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
166       break;
167   }
168 }
169 
170 static gboolean
gst_av1_dec_start(GstVideoDecoder * dec)171 gst_av1_dec_start (GstVideoDecoder * dec)
172 {
173   GstAV1Dec *av1dec = GST_AV1_DEC_CAST (dec);
174 
175   av1dec->decoder_inited = FALSE;
176   av1dec->output_state = NULL;
177   av1dec->input_state = NULL;
178 
179   return TRUE;
180 }
181 
182 static gboolean
gst_av1_dec_stop(GstVideoDecoder * dec)183 gst_av1_dec_stop (GstVideoDecoder * dec)
184 {
185   GstAV1Dec *av1dec = GST_AV1_DEC_CAST (dec);
186 
187   if (av1dec->output_state) {
188     gst_video_codec_state_unref (av1dec->output_state);
189     av1dec->output_state = NULL;
190   }
191 
192   if (av1dec->input_state) {
193     gst_video_codec_state_unref (av1dec->input_state);
194     av1dec->input_state = NULL;
195   }
196 
197   if (av1dec->decoder_inited) {
198     aom_codec_destroy (&av1dec->decoder);
199   }
200   av1dec->decoder_inited = FALSE;
201 
202   return TRUE;
203 }
204 
205 static gboolean
gst_av1_dec_set_format(GstVideoDecoder * dec,GstVideoCodecState * state)206 gst_av1_dec_set_format (GstVideoDecoder * dec, GstVideoCodecState * state)
207 {
208   GstAV1Dec *av1dec = GST_AV1_DEC_CAST (dec);
209 
210   if (av1dec->decoder_inited) {
211     aom_codec_destroy (&av1dec->decoder);
212   }
213   av1dec->decoder_inited = FALSE;
214 
215   if (av1dec->output_state) {
216     gst_video_codec_state_unref (av1dec->output_state);
217     av1dec->output_state = NULL;
218   }
219 
220   if (av1dec->input_state) {
221     gst_video_codec_state_unref (av1dec->input_state);
222   }
223 
224   av1dec->input_state = gst_video_codec_state_ref (state);
225 
226   return TRUE;
227 }
228 
229 static gboolean
gst_av1_dec_flush(GstVideoDecoder * dec)230 gst_av1_dec_flush (GstVideoDecoder * dec)
231 {
232   GstAV1Dec *av1dec = GST_AV1_DEC_CAST (dec);
233 
234   if (av1dec->output_state) {
235     gst_video_codec_state_unref (av1dec->output_state);
236     av1dec->output_state = NULL;
237   }
238 
239   if (av1dec->decoder_inited) {
240     aom_codec_destroy (&av1dec->decoder);
241   }
242   av1dec->decoder_inited = FALSE;
243 
244   return TRUE;
245 }
246 
247 static GstFlowReturn
gst_av1_dec_open_codec(GstAV1Dec * av1dec,GstVideoCodecFrame * frame)248 gst_av1_dec_open_codec (GstAV1Dec * av1dec, GstVideoCodecFrame * frame)
249 {
250   aom_codec_err_t status;
251   GstAV1DecClass *av1class = GST_AV1_DEC_GET_CLASS (av1dec);
252 
253   status = aom_codec_dec_init (&av1dec->decoder, av1class->codec_algo, NULL, 0);
254   if (status != AOM_CODEC_OK) {
255     GST_ELEMENT_ERROR (av1dec, LIBRARY, INIT,
256         ("Failed to initialize AOM decoder"), ("%s", ""));
257     return GST_FLOW_ERROR;
258   }
259 
260   av1dec->decoder_inited = TRUE;
261   return GST_FLOW_OK;
262 }
263 
264 static void
gst_av1_dec_handle_resolution_change(GstAV1Dec * av1dec,aom_image_t * img,GstVideoFormat fmt)265 gst_av1_dec_handle_resolution_change (GstAV1Dec * av1dec, aom_image_t * img,
266     GstVideoFormat fmt)
267 {
268   if (!av1dec->output_state ||
269       av1dec->output_state->info.finfo->format != fmt ||
270       av1dec->output_state->info.width != img->d_w ||
271       av1dec->output_state->info.height != img->d_h) {
272 
273     if (av1dec->output_state)
274       gst_video_codec_state_unref (av1dec->output_state);
275 
276     av1dec->output_state =
277         gst_video_decoder_set_output_state (GST_VIDEO_DECODER (av1dec),
278         fmt, img->d_w, img->d_h, av1dec->input_state);
279     gst_video_decoder_negotiate (GST_VIDEO_DECODER (av1dec));
280   }
281 
282 
283 }
284 
285 static void
gst_av1_dec_image_to_buffer(GstAV1Dec * dec,const aom_image_t * img,GstBuffer * buffer)286 gst_av1_dec_image_to_buffer (GstAV1Dec * dec, const aom_image_t * img,
287     GstBuffer * buffer)
288 {
289   int deststride, srcstride, height, width, line, comp, y;
290   guint8 *dest, *src;
291   GstVideoFrame frame;
292   GstVideoInfo *info = &dec->output_state->info;
293 
294   if (!gst_video_frame_map (&frame, info, buffer, GST_MAP_WRITE)) {
295     GST_ERROR_OBJECT (dec, "Could not map video buffer");
296     return;
297   }
298 
299   for (comp = 0; comp < 3; comp++) {
300     dest = GST_VIDEO_FRAME_COMP_DATA (&frame, comp);
301     src = img->planes[comp];
302     width =
303         GST_VIDEO_FRAME_COMP_WIDTH (&frame,
304         comp) * GST_VIDEO_FRAME_COMP_PSTRIDE (&frame, comp);
305     height = GST_VIDEO_FRAME_COMP_HEIGHT (&frame, comp);
306     deststride = GST_VIDEO_FRAME_COMP_STRIDE (&frame, comp);
307     srcstride = img->stride[comp];
308 
309     if ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) && img->bit_depth == 8) {
310       GST_TRACE_OBJECT (dec,
311           "HIGHBITDEPTH image with 8 bit_depth. Comp %d: %d != %d, copying "
312           "line by line.", comp, srcstride, deststride);
313       for (line = 0; line < height; line++) {
314         for (y = 0; y < width; y++) {
315           dest[y] = src[y * 2];
316         }
317         dest += deststride;
318         src += srcstride;
319       }
320     } else if (srcstride == deststride) {
321       GST_TRACE_OBJECT (dec, "Stride matches. Comp %d: %d, copying full plane",
322           comp, srcstride);
323       memcpy (dest, src, srcstride * height);
324     } else {
325       GST_TRACE_OBJECT (dec, "Stride mismatch. Comp %d: %d != %d, copying "
326           "line by line.", comp, srcstride, deststride);
327       for (line = 0; line < height; line++) {
328         memcpy (dest, src, width);
329         dest += deststride;
330         src += srcstride;
331       }
332     }
333   }
334 
335   gst_video_frame_unmap (&frame);
336 }
337 
338 gboolean
gst_av1_dec_get_valid_format(GstAV1Dec * dec,const aom_image_t * img,GstVideoFormat * fmt)339 gst_av1_dec_get_valid_format (GstAV1Dec * dec, const aom_image_t * img,
340     GstVideoFormat * fmt)
341 {
342   switch (img->fmt) {
343     case AOM_IMG_FMT_I420:
344     case AOM_IMG_FMT_I42016:
345       if (img->bit_depth == 8) {
346         *fmt = img->monochrome ? GST_VIDEO_FORMAT_GRAY8 : GST_VIDEO_FORMAT_I420;
347         return TRUE;
348       } else if (img->bit_depth == 10) {
349         *fmt = AOM_FMT_TO_GST (I420_10);
350         return TRUE;
351       } else if (img->bit_depth == 12) {
352         *fmt = AOM_FMT_TO_GST (I420_12);
353         return TRUE;
354       }
355 
356       GST_FIXME_OBJECT (dec,
357           "Please add a 4:2:0 planar %u bit depth frame format",
358           img->bit_depth);
359       GST_ELEMENT_WARNING (dec, STREAM, NOT_IMPLEMENTED, (NULL),
360           ("Unsupported frame format - 4:2:0 planar %u bit depth",
361               img->bit_depth));
362       return FALSE;
363 
364     case AOM_IMG_FMT_I422:
365     case AOM_IMG_FMT_I42216:
366       if (img->bit_depth == 8) {
367         *fmt = GST_VIDEO_FORMAT_Y42B;
368         return TRUE;
369       } else if (img->bit_depth == 10) {
370         *fmt = AOM_FMT_TO_GST (I422_10);
371         return TRUE;
372       } else if (img->bit_depth == 12) {
373         *fmt = AOM_FMT_TO_GST (I422_12);
374         return TRUE;
375       }
376       GST_FIXME_OBJECT (dec,
377           "Please add a 4:2:2 planar %u bit depth frame format",
378           img->bit_depth);
379       GST_ELEMENT_WARNING (dec, STREAM, NOT_IMPLEMENTED, (NULL),
380           ("Unsupported frame format - 4:2:2 planar %u bit depth",
381               img->bit_depth));
382       return FALSE;
383 
384     case AOM_IMG_FMT_I444:
385     case AOM_IMG_FMT_I44416:
386       if (img->bit_depth == 8) {
387         *fmt = GST_VIDEO_FORMAT_Y444;
388         return TRUE;
389       } else if (img->bit_depth == 10) {
390         *fmt = AOM_FMT_TO_GST (Y444_10);
391         return TRUE;
392       } else if (img->bit_depth == 12) {
393         *fmt = AOM_FMT_TO_GST (Y444_12);
394         return TRUE;
395       }
396       GST_FIXME_OBJECT (dec,
397           "Please add a 4:4:4 planar %u bit depth frame format",
398           img->bit_depth);
399       GST_ELEMENT_WARNING (dec, STREAM, NOT_IMPLEMENTED, (NULL),
400           ("Unsupported frame format - 4:4:4 planar %u bit depth",
401               img->bit_depth));
402       return FALSE;
403 
404     case AOM_IMG_FMT_YV12:
405       *fmt = GST_VIDEO_FORMAT_YV12;
406       return TRUE;
407 
408     default:
409       return FALSE;
410   }
411 }
412 
413 static GstFlowReturn
gst_av1_dec_handle_frame(GstVideoDecoder * dec,GstVideoCodecFrame * frame)414 gst_av1_dec_handle_frame (GstVideoDecoder * dec, GstVideoCodecFrame * frame)
415 {
416   GstAV1Dec *av1dec = GST_AV1_DEC_CAST (dec);
417   GstFlowReturn ret;
418   GstMapInfo minfo;
419   aom_codec_err_t status;
420   aom_image_t *img;
421   aom_codec_iter_t iter = NULL;
422   GstVideoFormat fmt;
423 
424   if (!av1dec->decoder_inited) {
425     ret = gst_av1_dec_open_codec (av1dec, frame);
426     if (ret == GST_FLOW_CUSTOM_SUCCESS_1) {
427       gst_video_decoder_drop_frame (dec, frame);
428       return GST_FLOW_OK;
429     } else if (ret != GST_FLOW_OK) {
430       gst_video_codec_frame_unref (frame);
431       return ret;
432     }
433   }
434 
435   if (!gst_buffer_map (frame->input_buffer, &minfo, GST_MAP_READ)) {
436     GST_ERROR_OBJECT (dec, "Failed to map input buffer");
437     gst_video_codec_frame_unref (frame);
438     return GST_FLOW_ERROR;
439   }
440 
441   status = aom_codec_decode (&av1dec->decoder, minfo.data, minfo.size, NULL);
442 
443   gst_buffer_unmap (frame->input_buffer, &minfo);
444 
445   if (status) {
446     GST_ELEMENT_ERROR (av1dec, LIBRARY, INIT,
447         ("Failed to decode frame"), ("%s", ""));
448     gst_video_codec_frame_unref (frame);
449     return ret;
450   }
451 
452   img = aom_codec_get_frame (&av1dec->decoder, &iter);
453   if (img) {
454     if (gst_av1_dec_get_valid_format (av1dec, img, &fmt) == FALSE) {
455       aom_img_free (img);
456       GST_ELEMENT_ERROR (dec, LIBRARY, ENCODE,
457           ("Failed to decode frame"), ("Unsupported color format %d",
458               img->fmt));
459       gst_video_codec_frame_unref (frame);
460       return GST_FLOW_ERROR;
461     }
462 
463     gst_av1_dec_handle_resolution_change (av1dec, img, fmt);
464 
465     ret = gst_video_decoder_allocate_output_frame (dec, frame);
466     if (ret == GST_FLOW_OK) {
467       gst_av1_dec_image_to_buffer (av1dec, img, frame->output_buffer);
468       ret = gst_video_decoder_finish_frame (dec, frame);
469     } else {
470       gst_video_decoder_drop_frame (dec, frame);
471     }
472 
473     aom_img_free (img);
474     while ((img = aom_codec_get_frame (&av1dec->decoder, &iter))) {
475       GST_WARNING_OBJECT (dec, "Multiple decoded frames... dropping");
476       aom_img_free (img);
477     }
478   } else {
479     GST_VIDEO_CODEC_FRAME_SET_DECODE_ONLY (frame);
480     gst_video_decoder_finish_frame (dec, frame);
481   }
482 
483 
484   return ret;
485 }
486