ivfparse: Check the data size against IVF_FRAME_HEADER_SIZE
[platform/upstream/gst-plugins-bad.git] / gst / ivfparse / gstivfparse.c
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
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);
79
80 static GstFlowReturn
81 gst_ivf_parse_handle_frame (GstBaseParse * parse,
82     GstBaseParseFrame * frame, gint * skipsize);
83
84 /* initialize the ivfparse's class */
85 static void
86 gst_ivf_parse_class_init (GstIvfParseClass * klass)
87 {
88   GObjectClass *gobject_class;
89   GstElementClass *gstelement_class;
90   GstBaseParseClass *gstbaseparse_class;
91
92   gobject_class = (GObjectClass *) klass;
93   gstelement_class = (GstElementClass *) klass;
94   gstbaseparse_class = (GstBaseParseClass *) klass;
95
96   gobject_class->finalize = gst_ivf_parse_finalize;
97
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;
101
102   gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
103   gst_element_class_add_static_pad_template (gstelement_class, &sink_factory);
104
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>");
108
109   /* debug category for filtering log messages */
110   GST_DEBUG_CATEGORY_INIT (gst_ivf_parse_debug, "ivfparse", 0, "IVF parser");
111 }
112
113 static void
114 gst_ivf_parse_reset (GstIvfParse * ivf)
115 {
116   ivf->state = GST_IVF_PARSE_START;
117   ivf->width = 0;
118   ivf->height = 0;
119   ivf->fps_n = 0;
120   ivf->fps_d = 0;
121   ivf->update_caps = FALSE;
122 }
123
124 /* initialize the new element
125  * instantiate pads and add them to element
126  * set pad calback functions
127  * initialize instance structure
128  */
129 static void
130 gst_ivf_parse_init (GstIvfParse * ivf)
131 {
132   gst_ivf_parse_reset (ivf);
133 }
134
135 static void
136 gst_ivf_parse_finalize (GObject * object)
137 {
138   GstIvfParse *const ivf = GST_IVF_PARSE (object);
139
140   GST_DEBUG_OBJECT (ivf, "finalizing");
141   gst_ivf_parse_reset (ivf);
142
143   G_OBJECT_CLASS (parent_class)->finalize (object);
144 }
145
146 static gboolean
147 gst_ivf_parse_start (GstBaseParse * parse)
148 {
149   GstIvfParse *const ivf = GST_IVF_PARSE (parse);
150
151   gst_ivf_parse_reset (ivf);
152
153   /* Minimal file header size needed at start */
154   gst_base_parse_set_min_frame_size (parse, IVF_FILE_HEADER_SIZE);
155
156   /* No sync code to detect frame boundaries */
157   gst_base_parse_set_syncable (parse, FALSE);
158
159   return TRUE;
160 }
161
162 static gboolean
163 gst_ivf_parse_stop (GstBaseParse * parse)
164 {
165   GstIvfParse *const ivf = GST_IVF_PARSE (parse);
166
167   gst_ivf_parse_reset (ivf);
168
169   return TRUE;
170 }
171
172 static void
173 gst_ivf_parse_set_size (GstIvfParse * ivf, guint width, guint height)
174 {
175   if (ivf->width != width || ivf->height != height) {
176     GST_INFO_OBJECT (ivf, "resolution changed to %ux%u", width, height);
177     ivf->width = width;
178     ivf->height = height;
179     ivf->update_caps = TRUE;
180   }
181 }
182
183 static void
184 gst_ivf_parse_set_framerate (GstIvfParse * ivf, guint fps_n, guint fps_d)
185 {
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);
188     ivf->fps_n = fps_n;
189     ivf->fps_d = fps_d;
190     ivf->update_caps = TRUE;
191   }
192 }
193
194 static const gchar *
195 fourcc_to_media_type (guint32 fourcc)
196 {
197   switch (fourcc) {
198     case GST_MAKE_FOURCC ('V', 'P', '8', '0'):
199       return "video/x-vp8";
200       break;
201     case GST_MAKE_FOURCC ('V', 'P', '9', '0'):
202       return "video/x-vp9";
203       break;
204     case GST_MAKE_FOURCC ('A', 'V', '0', '1'):
205       return "video/x-av1";
206       break;
207     default:
208       return NULL;
209   }
210   return NULL;
211 }
212
213 static void
214 gst_ivf_parse_update_src_caps (GstIvfParse * ivf)
215 {
216   GstCaps *caps;
217   const gchar *media_type;
218   if (!ivf->update_caps &&
219       G_LIKELY (gst_pad_has_current_caps (GST_BASE_PARSE_SRC_PAD (ivf))))
220     return;
221   ivf->update_caps = FALSE;
222
223   media_type = fourcc_to_media_type (ivf->fourcc);
224
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);
228
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,
233         ivf->fps_d, NULL);
234   }
235
236   gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (ivf), caps);
237   gst_caps_unref (caps);
238 }
239
240 static GstFlowReturn
241 gst_ivf_parse_handle_frame_start (GstIvfParse * ivf, GstBaseParseFrame * frame,
242     gint * skipsize)
243 {
244   GstBuffer *const buffer = frame->buffer;
245   GstMapInfo map;
246   GstFlowReturn ret = GST_FLOW_OK;
247
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);
260 #endif
261
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;
267       goto end;
268     }
269
270     ivf->fourcc = fourcc;
271
272     gst_ivf_parse_set_size (ivf, width, height);
273     gst_ivf_parse_set_framerate (ivf, fps_n, fps_d);
274
275     GST_LOG_OBJECT (ivf, "Stream has %d frames", num_frames);
276
277     /* move along */
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;
282   } else {
283     GST_LOG_OBJECT (ivf, "Header data not yet available.");
284     *skipsize = 0;
285   }
286
287 end:
288   gst_buffer_unmap (buffer, &map);
289   return ret;
290 }
291
292 static GstFlowReturn
293 gst_ivf_parse_handle_frame_data (GstIvfParse * ivf, GstBaseParseFrame * frame,
294     gint * skipsize)
295 {
296   GstBuffer *const buffer = frame->buffer;
297   GstMapInfo map;
298   GstFlowReturn ret = GST_FLOW_OK;
299   GstBuffer *out_buffer;
300
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);
305
306     GST_LOG_OBJECT (ivf,
307         "Read frame header: size %u, pts %" G_GUINT64_FORMAT, frame_size,
308         frame_pts);
309
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);
314       *skipsize = 0;
315       goto end;
316     }
317
318     gst_buffer_unmap (buffer, &map);
319
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);
325     if (!out_buffer) {
326       GST_ERROR_OBJECT (ivf, "Failed to copy frame buffer");
327       ret = GST_FLOW_ERROR;
328       *skipsize = IVF_FRAME_HEADER_SIZE + frame_size;
329       goto end;
330     }
331     gst_buffer_replace (&frame->out_buffer, out_buffer);
332     gst_buffer_unref (out_buffer);
333
334     /* Detect resolution changes on key frames */
335     if (gst_buffer_map (frame->out_buffer, &map, GST_MAP_READ)) {
336       guint32 width, height;
337
338       if (ivf->fourcc == GST_MAKE_FOURCC ('V', 'P', '8', '0')) {
339         guint32 frame_tag;
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");
343
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);
347         }
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 */
354       }
355       gst_buffer_unmap (frame->out_buffer, &map);
356     }
357
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,
361           ivf->fps_n);
362     }
363
364     gst_ivf_parse_update_src_caps (ivf);
365
366     ret = gst_base_parse_finish_frame (GST_BASE_PARSE_CAST (ivf), frame,
367         IVF_FRAME_HEADER_SIZE + frame_size);
368     *skipsize = 0;
369   } else {
370     GST_LOG_OBJECT (ivf, "Frame data not yet available.");
371     gst_buffer_unmap (buffer, &map);
372     *skipsize = 0;
373   }
374
375 end:
376   return ret;
377 }
378
379 static GstFlowReturn
380 gst_ivf_parse_handle_frame (GstBaseParse * parse,
381     GstBaseParseFrame * frame, gint * skipsize)
382 {
383   GstIvfParse *const ivf = GST_IVF_PARSE (parse);
384
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);
390     default:
391       break;
392   }
393
394   g_assert_not_reached ();
395   return GST_FLOW_ERROR;
396 }
397
398 /* entry point to initialize the plug-in */
399 static gboolean
400 ivfparse_init (GstPlugin * ivfparse)
401 {
402   /* register parser element */
403   if (!gst_element_register (ivfparse, "ivfparse", GST_RANK_PRIMARY,
404           GST_TYPE_IVF_PARSE))
405     return FALSE;
406
407   return TRUE;
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)