• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* -*- Mode: c; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * GStreamer IVF parser
4  * (c) 2010 Opera Software ASA, Philip Jägenstedt <philipj@opera.com>
5  *
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.
10  *
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.
15  *
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.
20  */
21 
22 /* Snippets of source code copied freely from wavparse,
23  * aviparse and auparse. */
24 
25 /* File format as written by libvpx ivfenc:
26  *
27  * All fields are little endian.
28  *
29  * 32 byte file header format:
30  *
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)
40  * 28-31: unused
41  *
42  * 12 byte frame header format:
43  *
44  * 0-3: frame size in bytes (uint32)
45  * 4-11: time stamp (uint64)
46  */
47 
48 #ifdef HAVE_CONFIG_H
49 #  include <config.h>
50 #endif
51 
52 #include "gstivfparse.h"
53 
54 #define IVF_FILE_HEADER_SIZE 32
55 #define IVF_FRAME_HEADER_SIZE 12
56 
57 GST_DEBUG_CATEGORY_STATIC (gst_ivf_parse_debug);
58 #define GST_CAT_DEFAULT gst_ivf_parse_debug
59 
60 /* sink and src pad templates */
61 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
62     GST_PAD_SINK,
63     GST_PAD_ALWAYS,
64     GST_STATIC_CAPS ("video/x-ivf")
65     );
66 
67 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
68     GST_PAD_SRC,
69     GST_PAD_ALWAYS,
70     GST_STATIC_CAPS ("ANY")
71     );
72 
73 #define gst_ivf_parse_parent_class parent_class
74 G_DEFINE_TYPE (GstIvfParse, gst_ivf_parse, GST_TYPE_BASE_PARSE);
75 GST_ELEMENT_REGISTER_DEFINE (ivfparse, "ivfparse", GST_RANK_PRIMARY,
76     GST_TYPE_IVF_PARSE);
77 
78 static void gst_ivf_parse_finalize (GObject * object);
79 static gboolean gst_ivf_parse_start (GstBaseParse * parse);
80 static gboolean gst_ivf_parse_stop (GstBaseParse * parse);
81 
82 static GstFlowReturn
83 gst_ivf_parse_handle_frame (GstBaseParse * parse,
84     GstBaseParseFrame * frame, gint * skipsize);
85 
86 /* initialize the ivfparse's class */
87 static void
gst_ivf_parse_class_init(GstIvfParseClass * klass)88 gst_ivf_parse_class_init (GstIvfParseClass * klass)
89 {
90   GObjectClass *gobject_class;
91   GstElementClass *gstelement_class;
92   GstBaseParseClass *gstbaseparse_class;
93 
94   gobject_class = (GObjectClass *) klass;
95   gstelement_class = (GstElementClass *) klass;
96   gstbaseparse_class = (GstBaseParseClass *) klass;
97 
98   gobject_class->finalize = gst_ivf_parse_finalize;
99 
100   gstbaseparse_class->start = gst_ivf_parse_start;
101   gstbaseparse_class->stop = gst_ivf_parse_stop;
102   gstbaseparse_class->handle_frame = gst_ivf_parse_handle_frame;
103 
104   gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
105   gst_element_class_add_static_pad_template (gstelement_class, &sink_factory);
106 
107   gst_element_class_set_static_metadata (gstelement_class,
108       "IVF parser", "Codec/Demuxer",
109       "Demuxes a IVF stream", "Philip Jägenstedt <philipj@opera.com>");
110 
111   /* debug category for filtering log messages */
112   GST_DEBUG_CATEGORY_INIT (gst_ivf_parse_debug, "ivfparse", 0, "IVF parser");
113 }
114 
115 static void
gst_ivf_parse_reset(GstIvfParse * ivf)116 gst_ivf_parse_reset (GstIvfParse * ivf)
117 {
118   ivf->state = GST_IVF_PARSE_START;
119   ivf->width = 0;
120   ivf->height = 0;
121   ivf->fps_n = 0;
122   ivf->fps_d = 0;
123   ivf->update_caps = FALSE;
124 }
125 
126 /* initialize the new element
127  * instantiate pads and add them to element
128  * set pad callback functions
129  * initialize instance structure
130  */
131 static void
gst_ivf_parse_init(GstIvfParse * ivf)132 gst_ivf_parse_init (GstIvfParse * ivf)
133 {
134   gst_ivf_parse_reset (ivf);
135 }
136 
137 static void
gst_ivf_parse_finalize(GObject * object)138 gst_ivf_parse_finalize (GObject * object)
139 {
140   GstIvfParse *const ivf = GST_IVF_PARSE (object);
141 
142   GST_DEBUG_OBJECT (ivf, "finalizing");
143   gst_ivf_parse_reset (ivf);
144 
145   G_OBJECT_CLASS (parent_class)->finalize (object);
146 }
147 
148 static gboolean
gst_ivf_parse_start(GstBaseParse * parse)149 gst_ivf_parse_start (GstBaseParse * parse)
150 {
151   GstIvfParse *const ivf = GST_IVF_PARSE (parse);
152 
153   gst_ivf_parse_reset (ivf);
154 
155   /* Minimal file header size needed at start */
156   gst_base_parse_set_min_frame_size (parse, IVF_FILE_HEADER_SIZE);
157 
158   /* No sync code to detect frame boundaries */
159   gst_base_parse_set_syncable (parse, FALSE);
160 
161   return TRUE;
162 }
163 
164 static gboolean
gst_ivf_parse_stop(GstBaseParse * parse)165 gst_ivf_parse_stop (GstBaseParse * parse)
166 {
167   GstIvfParse *const ivf = GST_IVF_PARSE (parse);
168 
169   gst_ivf_parse_reset (ivf);
170 
171   return TRUE;
172 }
173 
174 static void
gst_ivf_parse_set_size(GstIvfParse * ivf,guint width,guint height)175 gst_ivf_parse_set_size (GstIvfParse * ivf, guint width, guint height)
176 {
177   if (ivf->width != width || ivf->height != height) {
178     GST_INFO_OBJECT (ivf, "resolution changed to %ux%u", width, height);
179     ivf->width = width;
180     ivf->height = height;
181     ivf->update_caps = TRUE;
182   }
183 }
184 
185 static void
gst_ivf_parse_set_framerate(GstIvfParse * ivf,guint fps_n,guint fps_d)186 gst_ivf_parse_set_framerate (GstIvfParse * ivf, guint fps_n, guint fps_d)
187 {
188   if (ivf->fps_n != fps_n || ivf->fps_d != fps_d) {
189     GST_INFO_OBJECT (ivf, "framerate changed to %u/%u", fps_n, fps_d);
190     ivf->fps_n = fps_n;
191     ivf->fps_d = fps_d;
192     ivf->update_caps = TRUE;
193   }
194 }
195 
196 static const gchar *
fourcc_to_media_type(guint32 fourcc)197 fourcc_to_media_type (guint32 fourcc)
198 {
199   switch (fourcc) {
200     case GST_MAKE_FOURCC ('V', 'P', '8', '0'):
201       return "video/x-vp8";
202       break;
203     case GST_MAKE_FOURCC ('V', 'P', '9', '0'):
204       return "video/x-vp9";
205       break;
206     case GST_MAKE_FOURCC ('A', 'V', '0', '1'):
207       return "video/x-av1";
208       break;
209     default:
210       return NULL;
211   }
212   return NULL;
213 }
214 
215 static void
gst_ivf_parse_update_src_caps(GstIvfParse * ivf)216 gst_ivf_parse_update_src_caps (GstIvfParse * ivf)
217 {
218   GstCaps *caps;
219   const gchar *media_type;
220   if (!ivf->update_caps &&
221       G_LIKELY (gst_pad_has_current_caps (GST_BASE_PARSE_SRC_PAD (ivf))))
222     return;
223   ivf->update_caps = FALSE;
224 
225   media_type = fourcc_to_media_type (ivf->fourcc);
226 
227   /* Create src pad caps */
228   caps = gst_caps_new_empty_simple (media_type);
229   if (ivf->width > 0 && ivf->height > 0) {
230     gst_caps_set_simple (caps, "width", G_TYPE_INT, ivf->width,
231         "height", G_TYPE_INT, ivf->height, NULL);
232   }
233 
234   if (ivf->fps_n > 0 && ivf->fps_d > 0) {
235     gst_base_parse_set_frame_rate (GST_BASE_PARSE_CAST (ivf),
236         ivf->fps_n, ivf->fps_d, 0, 0);
237     gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION, ivf->fps_n,
238         ivf->fps_d, NULL);
239   }
240 
241   gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (ivf), caps);
242   gst_caps_unref (caps);
243 }
244 
245 static GstFlowReturn
gst_ivf_parse_handle_frame_start(GstIvfParse * ivf,GstBaseParseFrame * frame,gint * skipsize)246 gst_ivf_parse_handle_frame_start (GstIvfParse * ivf, GstBaseParseFrame * frame,
247     gint * skipsize)
248 {
249   GstBuffer *const buffer = frame->buffer;
250   GstMapInfo map;
251   GstFlowReturn ret = GST_FLOW_OK;
252 
253   gst_buffer_map (buffer, &map, GST_MAP_READ);
254   if (map.size >= IVF_FILE_HEADER_SIZE) {
255     guint32 magic = GST_READ_UINT32_LE (map.data);
256     guint16 version = GST_READ_UINT16_LE (map.data + 4);
257     guint16 header_size = GST_READ_UINT16_LE (map.data + 6);
258     guint32 fourcc = GST_READ_UINT32_LE (map.data + 8);
259     guint16 width = GST_READ_UINT16_LE (map.data + 12);
260     guint16 height = GST_READ_UINT16_LE (map.data + 14);
261     guint32 fps_n = GST_READ_UINT32_LE (map.data + 16);
262     guint32 fps_d = GST_READ_UINT32_LE (map.data + 20);
263 #ifndef GST_DISABLE_GST_DEBUG
264     guint32 num_frames = GST_READ_UINT32_LE (map.data + 24);
265 #endif
266 
267     if (magic != GST_MAKE_FOURCC ('D', 'K', 'I', 'F') ||
268         version != 0 || header_size != 32 ||
269         fourcc_to_media_type (fourcc) == NULL) {
270       GST_ELEMENT_ERROR (ivf, STREAM, WRONG_TYPE, (NULL), (NULL));
271       ret = GST_FLOW_ERROR;
272       goto end;
273     }
274 
275     ivf->fourcc = fourcc;
276 
277     gst_ivf_parse_set_size (ivf, width, height);
278     gst_ivf_parse_set_framerate (ivf, fps_n, fps_d);
279 
280     GST_LOG_OBJECT (ivf, "Stream has %d frames", num_frames);
281 
282     /* move along */
283     ivf->state = GST_IVF_PARSE_DATA;
284     gst_base_parse_set_min_frame_size (GST_BASE_PARSE_CAST (ivf),
285         IVF_FRAME_HEADER_SIZE);
286     *skipsize = IVF_FILE_HEADER_SIZE;
287   } else {
288     GST_LOG_OBJECT (ivf, "Header data not yet available.");
289     *skipsize = 0;
290   }
291 
292 end:
293   gst_buffer_unmap (buffer, &map);
294   return ret;
295 }
296 
297 static GstFlowReturn
gst_ivf_parse_handle_frame_data(GstIvfParse * ivf,GstBaseParseFrame * frame,gint * skipsize)298 gst_ivf_parse_handle_frame_data (GstIvfParse * ivf, GstBaseParseFrame * frame,
299     gint * skipsize)
300 {
301   GstBuffer *const buffer = frame->buffer;
302   GstMapInfo map;
303   GstFlowReturn ret = GST_FLOW_OK;
304   GstBuffer *out_buffer;
305 
306   gst_buffer_map (buffer, &map, GST_MAP_READ);
307   if (map.size >= IVF_FRAME_HEADER_SIZE) {
308     guint32 frame_size = GST_READ_UINT32_LE (map.data);
309     guint64 frame_pts = GST_READ_UINT64_LE (map.data + 4);
310 
311     GST_LOG_OBJECT (ivf,
312         "Read frame header: size %u, pts %" G_GUINT64_FORMAT, frame_size,
313         frame_pts);
314 
315     if (map.size < IVF_FRAME_HEADER_SIZE + frame_size) {
316       gst_base_parse_set_min_frame_size (GST_BASE_PARSE_CAST (ivf),
317           IVF_FRAME_HEADER_SIZE + frame_size);
318       gst_buffer_unmap (buffer, &map);
319       *skipsize = 0;
320       goto end;
321     }
322 
323     gst_buffer_unmap (buffer, &map);
324 
325     /* Eventually, we would need the buffer memory in a merged state anyway */
326     out_buffer = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_FLAGS |
327         GST_BUFFER_COPY_TIMESTAMPS | GST_BUFFER_COPY_META |
328         GST_BUFFER_COPY_MEMORY | GST_BUFFER_COPY_MERGE,
329         IVF_FRAME_HEADER_SIZE, frame_size);
330     if (!out_buffer) {
331       GST_ERROR_OBJECT (ivf, "Failed to copy frame buffer");
332       ret = GST_FLOW_ERROR;
333       *skipsize = IVF_FRAME_HEADER_SIZE + frame_size;
334       goto end;
335     }
336     gst_buffer_replace (&frame->out_buffer, out_buffer);
337     gst_buffer_unref (out_buffer);
338 
339     /* Detect resolution changes on key frames */
340     if (gst_buffer_map (frame->out_buffer, &map, GST_MAP_READ)) {
341       guint32 width, height;
342 
343       if (ivf->fourcc == GST_MAKE_FOURCC ('V', 'P', '8', '0')) {
344         guint32 frame_tag;
345         frame_tag = GST_READ_UINT24_LE (map.data);
346         if (!(frame_tag & 0x01) && map.size >= 10) {    /* key frame */
347           GST_DEBUG_OBJECT (ivf, "key frame detected");
348 
349           width = GST_READ_UINT16_LE (map.data + 6) & 0x3fff;
350           height = GST_READ_UINT16_LE (map.data + 8) & 0x3fff;
351           gst_ivf_parse_set_size (ivf, width, height);
352         }
353       } else if (ivf->fourcc == GST_MAKE_FOURCC ('V', 'P', '9', '0')) {
354         /* Fixme: Add vp9 frame header parsing? */
355       } else if (ivf->fourcc == GST_MAKE_FOURCC ('A', 'V', '0', '1')) {
356         /* Fixme: Add av1 frame header parsing? */
357         /* This would allow to parse dynamic resolution changes */
358         /* implement when gstav1parser is ready */
359       }
360       gst_buffer_unmap (frame->out_buffer, &map);
361     }
362 
363     if (ivf->fps_n > 0) {
364       GST_BUFFER_TIMESTAMP (out_buffer) =
365           gst_util_uint64_scale_int (GST_SECOND * frame_pts, ivf->fps_d,
366           ivf->fps_n);
367     }
368 
369     gst_ivf_parse_update_src_caps (ivf);
370 
371     ret = gst_base_parse_finish_frame (GST_BASE_PARSE_CAST (ivf), frame,
372         IVF_FRAME_HEADER_SIZE + frame_size);
373     *skipsize = 0;
374   } else {
375     GST_LOG_OBJECT (ivf, "Frame data not yet available.");
376     gst_buffer_unmap (buffer, &map);
377     *skipsize = 0;
378   }
379 
380 end:
381   return ret;
382 }
383 
384 static GstFlowReturn
gst_ivf_parse_handle_frame(GstBaseParse * parse,GstBaseParseFrame * frame,gint * skipsize)385 gst_ivf_parse_handle_frame (GstBaseParse * parse,
386     GstBaseParseFrame * frame, gint * skipsize)
387 {
388   GstIvfParse *const ivf = GST_IVF_PARSE (parse);
389 
390   switch (ivf->state) {
391     case GST_IVF_PARSE_START:
392       return gst_ivf_parse_handle_frame_start (ivf, frame, skipsize);
393     case GST_IVF_PARSE_DATA:
394       return gst_ivf_parse_handle_frame_data (ivf, frame, skipsize);
395     default:
396       break;
397   }
398 
399   g_assert_not_reached ();
400   return GST_FLOW_ERROR;
401 }
402 
403 /* entry point to initialize the plug-in */
404 static gboolean
ivfparse_init(GstPlugin * plugin)405 ivfparse_init (GstPlugin * plugin)
406 {
407   return GST_ELEMENT_REGISTER (ivfparse, plugin);
408 }
409 
410 /* gstreamer looks for this structure to register plugins */
411 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
412     GST_VERSION_MINOR,
413     ivfparse,
414     "IVF parser",
415     ivfparse_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
416