1 /* -*- Mode: c; c-basic-offset: 2; indent-tabs-mode: nil -*- */
4 * (c) 2010 Opera Software ASA, Philip Jägenstedt <philipj@opera.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
22 /* Snippets of source code copied freely from wavparse,
23 * aviparse and auparse. */
25 /* File format as written by libvpx ivfenc:
27 * All fields are little endian.
29 * 32 byte file header format:
31 * 0-3: "DKIF" (file magic)
32 * 4-5: version (uint16)
33 * 6-7: header size (uint16)
34 * 8-11: "VP80" (FOURCC)
35 * 12-13: width (uint16)
36 * 14-15: height (uint16)
37 * 16-19: framerate numerator (uint32)
38 * 20-23: framerate denominator (uint32)
39 * 24-27: frame count (uint32)
42 * 12 byte frame header format:
44 * 0-3: frame size in bytes (uint32)
45 * 4-11: time stamp (uint64)
52 #include "gstivfparse.h"
54 #define IVF_FILE_HEADER_SIZE 32
55 #define IVF_FRAME_HEADER_SIZE 12
57 GST_DEBUG_CATEGORY_STATIC (gst_ivf_parse_debug);
58 #define GST_CAT_DEFAULT gst_ivf_parse_debug
60 /* sink and src pad templates */
61 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
64 GST_STATIC_CAPS ("video/x-ivf")
67 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
70 GST_STATIC_CAPS ("ANY")
73 #define gst_ivf_parse_parent_class parent_class
74 G_DEFINE_TYPE (GstIvfParse, gst_ivf_parse, GST_TYPE_BASE_PARSE);
76 static void gst_ivf_parse_finalize (GObject * object);
77 static gboolean gst_ivf_parse_start (GstBaseParse * parse);
78 static gboolean gst_ivf_parse_stop (GstBaseParse * parse);
81 gst_ivf_parse_handle_frame (GstBaseParse * parse,
82 GstBaseParseFrame * frame, gint * skipsize);
84 /* initialize the ivfparse's class */
86 gst_ivf_parse_class_init (GstIvfParseClass * klass)
88 GObjectClass *gobject_class;
89 GstElementClass *gstelement_class;
90 GstBaseParseClass *gstbaseparse_class;
92 gobject_class = (GObjectClass *) klass;
93 gstelement_class = (GstElementClass *) klass;
94 gstbaseparse_class = (GstBaseParseClass *) klass;
96 gobject_class->finalize = gst_ivf_parse_finalize;
98 gstbaseparse_class->start = gst_ivf_parse_start;
99 gstbaseparse_class->stop = gst_ivf_parse_stop;
100 gstbaseparse_class->handle_frame = gst_ivf_parse_handle_frame;
102 gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
103 gst_element_class_add_static_pad_template (gstelement_class, &sink_factory);
105 gst_element_class_set_static_metadata (gstelement_class,
106 "IVF parser", "Codec/Demuxer",
107 "Demuxes a IVF stream", "Philip Jägenstedt <philipj@opera.com>");
109 /* debug category for filtering log messages */
110 GST_DEBUG_CATEGORY_INIT (gst_ivf_parse_debug, "ivfparse", 0, "IVF parser");
114 gst_ivf_parse_reset (GstIvfParse * ivf)
116 ivf->state = GST_IVF_PARSE_START;
121 ivf->update_caps = FALSE;
124 /* initialize the new element
125 * instantiate pads and add them to element
126 * set pad calback functions
127 * initialize instance structure
130 gst_ivf_parse_init (GstIvfParse * ivf)
132 gst_ivf_parse_reset (ivf);
136 gst_ivf_parse_finalize (GObject * object)
138 GstIvfParse *const ivf = GST_IVF_PARSE (object);
140 GST_DEBUG_OBJECT (ivf, "finalizing");
141 gst_ivf_parse_reset (ivf);
143 G_OBJECT_CLASS (parent_class)->finalize (object);
147 gst_ivf_parse_start (GstBaseParse * parse)
149 GstIvfParse *const ivf = GST_IVF_PARSE (parse);
151 gst_ivf_parse_reset (ivf);
153 /* Minimal file header size needed at start */
154 gst_base_parse_set_min_frame_size (parse, IVF_FILE_HEADER_SIZE);
156 /* No sync code to detect frame boundaries */
157 gst_base_parse_set_syncable (parse, FALSE);
163 gst_ivf_parse_stop (GstBaseParse * parse)
165 GstIvfParse *const ivf = GST_IVF_PARSE (parse);
167 gst_ivf_parse_reset (ivf);
173 gst_ivf_parse_set_size (GstIvfParse * ivf, guint width, guint height)
175 if (ivf->width != width || ivf->height != height) {
176 GST_INFO_OBJECT (ivf, "resolution changed to %ux%u", width, height);
178 ivf->height = height;
179 ivf->update_caps = TRUE;
184 gst_ivf_parse_set_framerate (GstIvfParse * ivf, guint fps_n, guint fps_d)
186 if (ivf->fps_n != fps_n || ivf->fps_d != fps_d) {
187 GST_INFO_OBJECT (ivf, "framerate changed to %u/%u", fps_n, fps_d);
190 ivf->update_caps = TRUE;
195 fourcc_to_media_type (guint32 fourcc)
198 case GST_MAKE_FOURCC ('V', 'P', '8', '0'):
199 return "video/x-vp8";
201 case GST_MAKE_FOURCC ('V', 'P', '9', '0'):
202 return "video/x-vp9";
204 case GST_MAKE_FOURCC ('A', 'V', '0', '1'):
205 return "video/x-av1";
214 gst_ivf_parse_update_src_caps (GstIvfParse * ivf)
217 const gchar *media_type;
218 if (!ivf->update_caps &&
219 G_LIKELY (gst_pad_has_current_caps (GST_BASE_PARSE_SRC_PAD (ivf))))
221 ivf->update_caps = FALSE;
223 media_type = fourcc_to_media_type (ivf->fourcc);
225 /* Create src pad caps */
226 caps = gst_caps_new_simple (media_type, "width", G_TYPE_INT, ivf->width,
227 "height", G_TYPE_INT, ivf->height, NULL);
229 if (ivf->fps_n > 0 && ivf->fps_d > 0) {
230 gst_base_parse_set_frame_rate (GST_BASE_PARSE_CAST (ivf),
231 ivf->fps_n, ivf->fps_d, 0, 0);
232 gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION, ivf->fps_n,
236 gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (ivf), caps);
237 gst_caps_unref (caps);
241 gst_ivf_parse_handle_frame_start (GstIvfParse * ivf, GstBaseParseFrame * frame,
244 GstBuffer *const buffer = frame->buffer;
246 GstFlowReturn ret = GST_FLOW_OK;
248 gst_buffer_map (buffer, &map, GST_MAP_READ);
249 if (map.size >= IVF_FILE_HEADER_SIZE) {
250 guint32 magic = GST_READ_UINT32_LE (map.data);
251 guint16 version = GST_READ_UINT16_LE (map.data + 4);
252 guint16 header_size = GST_READ_UINT16_LE (map.data + 6);
253 guint32 fourcc = GST_READ_UINT32_LE (map.data + 8);
254 guint16 width = GST_READ_UINT16_LE (map.data + 12);
255 guint16 height = GST_READ_UINT16_LE (map.data + 14);
256 guint32 fps_n = GST_READ_UINT32_LE (map.data + 16);
257 guint32 fps_d = GST_READ_UINT32_LE (map.data + 20);
258 #ifndef GST_DISABLE_GST_DEBUG
259 guint32 num_frames = GST_READ_UINT32_LE (map.data + 24);
262 if (magic != GST_MAKE_FOURCC ('D', 'K', 'I', 'F') ||
263 version != 0 || header_size != 32 ||
264 fourcc_to_media_type (fourcc) == NULL) {
265 GST_ELEMENT_ERROR (ivf, STREAM, WRONG_TYPE, (NULL), (NULL));
266 ret = GST_FLOW_ERROR;
270 ivf->fourcc = fourcc;
272 gst_ivf_parse_set_size (ivf, width, height);
273 gst_ivf_parse_set_framerate (ivf, fps_n, fps_d);
275 GST_LOG_OBJECT (ivf, "Stream has %d frames", num_frames);
278 ivf->state = GST_IVF_PARSE_DATA;
279 gst_base_parse_set_min_frame_size (GST_BASE_PARSE_CAST (ivf),
280 IVF_FRAME_HEADER_SIZE);
281 *skipsize = IVF_FILE_HEADER_SIZE;
283 GST_LOG_OBJECT (ivf, "Header data not yet available.");
288 gst_buffer_unmap (buffer, &map);
293 gst_ivf_parse_handle_frame_data (GstIvfParse * ivf, GstBaseParseFrame * frame,
296 GstBuffer *const buffer = frame->buffer;
298 GstFlowReturn ret = GST_FLOW_OK;
299 GstBuffer *out_buffer;
301 gst_buffer_map (buffer, &map, GST_MAP_READ);
302 if (map.size >= IVF_FRAME_HEADER_SIZE) {
303 guint32 frame_size = GST_READ_UINT32_LE (map.data);
304 guint64 frame_pts = GST_READ_UINT64_LE (map.data + 4);
307 "Read frame header: size %u, pts %" G_GUINT64_FORMAT, frame_size,
310 if (map.size < IVF_FRAME_HEADER_SIZE + frame_size) {
311 gst_base_parse_set_min_frame_size (GST_BASE_PARSE_CAST (ivf),
312 IVF_FRAME_HEADER_SIZE + frame_size);
313 gst_buffer_unmap (buffer, &map);
318 gst_buffer_unmap (buffer, &map);
320 /* Eventually, we would need the buffer memory in a merged state anyway */
321 out_buffer = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_FLAGS |
322 GST_BUFFER_COPY_TIMESTAMPS | GST_BUFFER_COPY_META |
323 GST_BUFFER_COPY_MEMORY | GST_BUFFER_COPY_MERGE,
324 IVF_FRAME_HEADER_SIZE, frame_size);
326 GST_ERROR_OBJECT (ivf, "Failed to copy frame buffer");
327 ret = GST_FLOW_ERROR;
328 *skipsize = IVF_FRAME_HEADER_SIZE + frame_size;
331 gst_buffer_replace (&frame->out_buffer, out_buffer);
332 gst_buffer_unref (out_buffer);
334 /* Detect resolution changes on key frames */
335 if (gst_buffer_map (frame->out_buffer, &map, GST_MAP_READ)) {
336 guint32 width, height;
338 if (ivf->fourcc == GST_MAKE_FOURCC ('V', 'P', '8', '0')) {
340 frame_tag = GST_READ_UINT24_LE (map.data);
341 if (!(frame_tag & 0x01) && map.size >= 10) { /* key frame */
342 GST_DEBUG_OBJECT (ivf, "key frame detected");
344 width = GST_READ_UINT16_LE (map.data + 6) & 0x3fff;
345 height = GST_READ_UINT16_LE (map.data + 8) & 0x3fff;
346 gst_ivf_parse_set_size (ivf, width, height);
348 } else if (ivf->fourcc == GST_MAKE_FOURCC ('V', 'P', '9', '0')) {
349 /* Fixme: Add vp9 frame header parsing? */
350 } else if (ivf->fourcc == GST_MAKE_FOURCC ('A', 'V', '0', '1')) {
351 /* Fixme: Add av1 frame header parsing? */
352 /* This would allow to parse dynamic resolution changes */
353 /* implement when gstav1parser is ready */
355 gst_buffer_unmap (frame->out_buffer, &map);
358 if (ivf->fps_n > 0) {
359 GST_BUFFER_TIMESTAMP (out_buffer) =
360 gst_util_uint64_scale_int (GST_SECOND * frame_pts, ivf->fps_d,
364 gst_ivf_parse_update_src_caps (ivf);
366 ret = gst_base_parse_finish_frame (GST_BASE_PARSE_CAST (ivf), frame,
367 IVF_FRAME_HEADER_SIZE + frame_size);
370 GST_LOG_OBJECT (ivf, "Frame data not yet available.");
371 gst_buffer_unmap (buffer, &map);
380 gst_ivf_parse_handle_frame (GstBaseParse * parse,
381 GstBaseParseFrame * frame, gint * skipsize)
383 GstIvfParse *const ivf = GST_IVF_PARSE (parse);
385 switch (ivf->state) {
386 case GST_IVF_PARSE_START:
387 return gst_ivf_parse_handle_frame_start (ivf, frame, skipsize);
388 case GST_IVF_PARSE_DATA:
389 return gst_ivf_parse_handle_frame_data (ivf, frame, skipsize);
394 g_assert_not_reached ();
395 return GST_FLOW_ERROR;
398 /* entry point to initialize the plug-in */
400 ivfparse_init (GstPlugin * ivfparse)
402 /* register parser element */
403 if (!gst_element_register (ivfparse, "ivfparse", GST_RANK_PRIMARY,
410 /* gstreamer looks for this structure to register plugins */
411 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
415 ivfparse_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)