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